Deleted Added
full compact
dsl_dir.c (177698) dsl_dir.c (185029)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 5 unchanged lines hidden (view full) ---

14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 5 unchanged lines hidden (view full) ---

14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
23 * Use is subject to license terms.
24 */
25
26#pragma ident "%Z%%M% %I% %E% SMI"
27
28#include <sys/dmu.h>
26#include <sys/dmu.h>
27#include <sys/dmu_objset.h>
29#include <sys/dmu_tx.h>
30#include <sys/dsl_dataset.h>
31#include <sys/dsl_dir.h>
32#include <sys/dsl_prop.h>
33#include <sys/dsl_synctask.h>
28#include <sys/dmu_tx.h>
29#include <sys/dsl_dataset.h>
30#include <sys/dsl_dir.h>
31#include <sys/dsl_prop.h>
32#include <sys/dsl_synctask.h>
33#include <sys/dsl_deleg.h>
34#include <sys/spa.h>
35#include <sys/zap.h>
36#include <sys/zio.h>
37#include <sys/arc.h>
34#include <sys/spa.h>
35#include <sys/zap.h>
36#include <sys/zio.h>
37#include <sys/arc.h>
38#include <sys/sunddi.h>
38#include "zfs_namecheck.h"
39
39#include "zfs_namecheck.h"
40
40static uint64_t dsl_dir_estimated_space(dsl_dir_t *dd);
41static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx);
41static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
42static void dsl_dir_set_reservation_sync(void *arg1, void *arg2,
43 cred_t *cr, dmu_tx_t *tx);
42
43
44/* ARGSUSED */
45static void
46dsl_dir_evict(dmu_buf_t *db, void *arg)
47{
48 dsl_dir_t *dd = arg;
49 dsl_pool_t *dp = dd->dd_pool;
50 int t;
51
52 for (t = 0; t < TXG_SIZE; t++) {
53 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
54 ASSERT(dd->dd_tempreserved[t] == 0);
55 ASSERT(dd->dd_space_towrite[t] == 0);
56 }
57
44
45
46/* ARGSUSED */
47static void
48dsl_dir_evict(dmu_buf_t *db, void *arg)
49{
50 dsl_dir_t *dd = arg;
51 dsl_pool_t *dp = dd->dd_pool;
52 int t;
53
54 for (t = 0; t < TXG_SIZE; t++) {
55 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
56 ASSERT(dd->dd_tempreserved[t] == 0);
57 ASSERT(dd->dd_space_towrite[t] == 0);
58 }
59
58 ASSERT3U(dd->dd_used_bytes, ==, dd->dd_phys->dd_used_bytes);
59
60 if (dd->dd_parent)
61 dsl_dir_close(dd->dd_parent, dd);
62
63 spa_close(dd->dd_pool->dp_spa, dd);
64
65 /*
66 * The props callback list should be empty since they hold the
67 * dir open.

--- 18 unchanged lines hidden (view full) ---

86 if (err)
87 return (err);
88 dd = dmu_buf_get_user(dbuf);
89#ifdef ZFS_DEBUG
90 {
91 dmu_object_info_t doi;
92 dmu_object_info_from_db(dbuf, &doi);
93 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
60 if (dd->dd_parent)
61 dsl_dir_close(dd->dd_parent, dd);
62
63 spa_close(dd->dd_pool->dp_spa, dd);
64
65 /*
66 * The props callback list should be empty since they hold the
67 * dir open.

--- 18 unchanged lines hidden (view full) ---

86 if (err)
87 return (err);
88 dd = dmu_buf_get_user(dbuf);
89#ifdef ZFS_DEBUG
90 {
91 dmu_object_info_t doi;
92 dmu_object_info_from_db(dbuf, &doi);
93 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
94 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
94 }
95#endif
95 }
96#endif
96 /* XXX assert bonus buffer size is correct */
97 if (dd == NULL) {
98 dsl_dir_t *winner;
99 int err;
100
101 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
102 dd->dd_object = ddobj;
103 dd->dd_dbuf = dbuf;
104 dd->dd_pool = dp;
105 dd->dd_phys = dbuf->db_data;
97 if (dd == NULL) {
98 dsl_dir_t *winner;
99 int err;
100
101 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
102 dd->dd_object = ddobj;
103 dd->dd_dbuf = dbuf;
104 dd->dd_pool = dp;
105 dd->dd_phys = dbuf->db_data;
106 dd->dd_used_bytes = dd->dd_phys->dd_used_bytes;
107 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
108
109 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
110 offsetof(dsl_prop_cb_record_t, cbr_node));
111
112 if (dd->dd_phys->dd_parent_obj) {
113 err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
114 NULL, dd, &dd->dd_parent);
106 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
107
108 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
109 offsetof(dsl_prop_cb_record_t, cbr_node));
110
111 if (dd->dd_phys->dd_parent_obj) {
112 err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
113 NULL, dd, &dd->dd_parent);
115 if (err) {
116 mutex_destroy(&dd->dd_lock);
117 kmem_free(dd, sizeof (dsl_dir_t));
118 dmu_buf_rele(dbuf, tag);
119 return (err);
120 }
114 if (err)
115 goto errout;
121 if (tail) {
122#ifdef ZFS_DEBUG
123 uint64_t foundobj;
124
125 err = zap_lookup(dp->dp_meta_objset,
116 if (tail) {
117#ifdef ZFS_DEBUG
118 uint64_t foundobj;
119
120 err = zap_lookup(dp->dp_meta_objset,
126 dd->dd_parent->dd_phys->
127 dd_child_dir_zapobj,
121 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
128 tail, sizeof (foundobj), 1, &foundobj);
129 ASSERT(err || foundobj == ddobj);
130#endif
131 (void) strcpy(dd->dd_myname, tail);
132 } else {
133 err = zap_value_search(dp->dp_meta_objset,
122 tail, sizeof (foundobj), 1, &foundobj);
123 ASSERT(err || foundobj == ddobj);
124#endif
125 (void) strcpy(dd->dd_myname, tail);
126 } else {
127 err = zap_value_search(dp->dp_meta_objset,
134 dd->dd_parent->dd_phys->
135 dd_child_dir_zapobj,
136 ddobj, dd->dd_myname);
128 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
129 ddobj, 0, dd->dd_myname);
137 }
130 }
138 if (err) {
139 dsl_dir_close(dd->dd_parent, dd);
140 mutex_destroy(&dd->dd_lock);
141 kmem_free(dd, sizeof (dsl_dir_t));
142 dmu_buf_rele(dbuf, tag);
143 return (err);
144 }
131 if (err)
132 goto errout;
145 } else {
146 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
147 }
148
149 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
150 dsl_dir_evict);
151 if (winner) {
152 if (dd->dd_parent)

--- 16 unchanged lines hidden (view full) ---

169 * pointer to the dd_pool, which has a pointer to the spa_t.
170 */
171 spa_open_ref(dp->dp_spa, tag);
172 ASSERT3P(dd->dd_pool, ==, dp);
173 ASSERT3U(dd->dd_object, ==, ddobj);
174 ASSERT3P(dd->dd_dbuf, ==, dbuf);
175 *ddp = dd;
176 return (0);
133 } else {
134 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
135 }
136
137 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
138 dsl_dir_evict);
139 if (winner) {
140 if (dd->dd_parent)

--- 16 unchanged lines hidden (view full) ---

157 * pointer to the dd_pool, which has a pointer to the spa_t.
158 */
159 spa_open_ref(dp->dp_spa, tag);
160 ASSERT3P(dd->dd_pool, ==, dp);
161 ASSERT3U(dd->dd_object, ==, ddobj);
162 ASSERT3P(dd->dd_dbuf, ==, dbuf);
163 *ddp = dd;
164 return (0);
165
166errout:
167 if (dd->dd_parent)
168 dsl_dir_close(dd->dd_parent, dd);
169 mutex_destroy(&dd->dd_lock);
170 kmem_free(dd, sizeof (dsl_dir_t));
171 dmu_buf_rele(dbuf, tag);
172 return (err);
173
177}
178
179void
180dsl_dir_close(dsl_dir_t *dd, void *tag)
181{
182 dprintf_dd(dd, "%s\n", "");
183 spa_close(dd->dd_pool->dp_spa, tag);
184 dmu_buf_rele(dd->dd_dbuf, tag);

--- 214 unchanged lines hidden (view full) ---

399 */
400int
401dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
402{
403 return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
404}
405
406uint64_t
174}
175
176void
177dsl_dir_close(dsl_dir_t *dd, void *tag)
178{
179 dprintf_dd(dd, "%s\n", "");
180 spa_close(dd->dd_pool->dp_spa, tag);
181 dmu_buf_rele(dd->dd_dbuf, tag);

--- 214 unchanged lines hidden (view full) ---

396 */
397int
398dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
399{
400 return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
401}
402
403uint64_t
407dsl_dir_create_sync(dsl_dir_t *pds, const char *name, dmu_tx_t *tx)
404dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
405 dmu_tx_t *tx)
408{
406{
409 objset_t *mos = pds->dd_pool->dp_meta_objset;
407 objset_t *mos = dp->dp_meta_objset;
410 uint64_t ddobj;
411 dsl_dir_phys_t *dsphys;
412 dmu_buf_t *dbuf;
413
414 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
415 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
408 uint64_t ddobj;
409 dsl_dir_phys_t *dsphys;
410 dmu_buf_t *dbuf;
411
412 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
413 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
416 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
417 name, sizeof (uint64_t), 1, &ddobj, tx));
414 if (pds) {
415 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
416 name, sizeof (uint64_t), 1, &ddobj, tx));
417 } else {
418 /* it's the root dir */
419 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
420 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
421 }
418 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
419 dmu_buf_will_dirty(dbuf, tx);
420 dsphys = dbuf->db_data;
421
422 dsphys->dd_creation_time = gethrestime_sec();
422 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
423 dmu_buf_will_dirty(dbuf, tx);
424 dsphys = dbuf->db_data;
425
426 dsphys->dd_creation_time = gethrestime_sec();
423 dsphys->dd_parent_obj = pds->dd_object;
427 if (pds)
428 dsphys->dd_parent_obj = pds->dd_object;
424 dsphys->dd_props_zapobj = zap_create(mos,
425 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
426 dsphys->dd_child_dir_zapobj = zap_create(mos,
427 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
429 dsphys->dd_props_zapobj = zap_create(mos,
430 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
431 dsphys->dd_child_dir_zapobj = zap_create(mos,
432 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
433 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
434 dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
428 dmu_buf_rele(dbuf, FTAG);
429
430 return (ddobj);
431}
432
433/* ARGSUSED */
434int
435dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)

--- 20 unchanged lines hidden (view full) ---

456 return (err);
457 if (count != 0)
458 return (EEXIST);
459
460 return (0);
461}
462
463void
435 dmu_buf_rele(dbuf, FTAG);
436
437 return (ddobj);
438}
439
440/* ARGSUSED */
441int
442dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)

--- 20 unchanged lines hidden (view full) ---

463 return (err);
464 if (count != 0)
465 return (EEXIST);
466
467 return (0);
468}
469
470void
464dsl_dir_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
471dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
465{
466 dsl_dir_t *dd = arg1;
467 objset_t *mos = dd->dd_pool->dp_meta_objset;
468 uint64_t val, obj;
472{
473 dsl_dir_t *dd = arg1;
474 objset_t *mos = dd->dd_pool->dp_meta_objset;
475 uint64_t val, obj;
476 dd_used_t t;
469
470 ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
471 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
472
473 /* Remove our reservation. */
474 val = 0;
477
478 ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
479 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
480
481 /* Remove our reservation. */
482 val = 0;
475 dsl_dir_set_reservation_sync(dd, &val, tx);
476 ASSERT3U(dd->dd_used_bytes, ==, 0);
483 dsl_dir_set_reservation_sync(dd, &val, cr, tx);
484 ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0);
477 ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
485 ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
486 for (t = 0; t < DD_USED_NUM; t++)
487 ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0);
478
479 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
480 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
488
489 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
490 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
491 VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
481 VERIFY(0 == zap_remove(mos,
482 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
483
484 obj = dd->dd_object;
485 dsl_dir_close(dd, tag);
486 VERIFY(0 == dmu_object_free(mos, obj, tx));
487}
488
492 VERIFY(0 == zap_remove(mos,
493 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
494
495 obj = dd->dd_object;
496 dsl_dir_close(dd, tag);
497 VERIFY(0 == dmu_object_free(mos, obj, tx));
498}
499
489void
490dsl_dir_create_root(objset_t *mos, uint64_t *ddobjp, dmu_tx_t *tx)
500boolean_t
501dsl_dir_is_clone(dsl_dir_t *dd)
491{
502{
492 dsl_dir_phys_t *dsp;
493 dmu_buf_t *dbuf;
494 int error;
495
496 *ddobjp = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
497 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
498
499 error = zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ROOT_DATASET,
500 sizeof (uint64_t), 1, ddobjp, tx);
501 ASSERT3U(error, ==, 0);
502
503 VERIFY(0 == dmu_bonus_hold(mos, *ddobjp, FTAG, &dbuf));
504 dmu_buf_will_dirty(dbuf, tx);
505 dsp = dbuf->db_data;
506
507 dsp->dd_creation_time = gethrestime_sec();
508 dsp->dd_props_zapobj = zap_create(mos,
509 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
510 dsp->dd_child_dir_zapobj = zap_create(mos,
511 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
512
513 dmu_buf_rele(dbuf, FTAG);
503 return (dd->dd_phys->dd_origin_obj &&
504 (dd->dd_pool->dp_origin_snap == NULL ||
505 dd->dd_phys->dd_origin_obj !=
506 dd->dd_pool->dp_origin_snap->ds_object));
514}
515
516void
517dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
518{
507}
508
509void
510dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
511{
519 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
520 dsl_dir_space_available(dd, NULL, 0, TRUE));
521
522 mutex_enter(&dd->dd_lock);
512 mutex_enter(&dd->dd_lock);
523 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, dd->dd_used_bytes);
524 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
525 dd->dd_phys->dd_quota);
513 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
514 dd->dd_phys->dd_used_bytes);
515 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
526 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
527 dd->dd_phys->dd_reserved);
528 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
529 dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
530 (dd->dd_phys->dd_uncompressed_bytes * 100 /
531 dd->dd_phys->dd_compressed_bytes));
516 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
517 dd->dd_phys->dd_reserved);
518 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
519 dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
520 (dd->dd_phys->dd_uncompressed_bytes * 100 /
521 dd->dd_phys->dd_compressed_bytes));
522 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
523 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
524 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
525 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
526 dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
527 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
528 dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
529 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
530 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
531 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
532 }
532 mutex_exit(&dd->dd_lock);
533
533 mutex_exit(&dd->dd_lock);
534
534 if (dd->dd_phys->dd_clone_parent_obj) {
535 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
536 if (dsl_dir_is_clone(dd)) {
535 dsl_dataset_t *ds;
536 char buf[MAXNAMELEN];
537
537 dsl_dataset_t *ds;
538 char buf[MAXNAMELEN];
539
538 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
539 VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
540 dd->dd_phys->dd_clone_parent_obj,
541 NULL, DS_MODE_NONE, FTAG, &ds));
540 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
541 dd->dd_phys->dd_origin_obj, FTAG, &ds));
542 dsl_dataset_name(ds, buf);
542 dsl_dataset_name(ds, buf);
543 dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
544 rw_exit(&dd->dd_pool->dp_config_rwlock);
545
543 dsl_dataset_rele(ds, FTAG);
546 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
547 }
544 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
545 }
546 rw_exit(&dd->dd_pool->dp_config_rwlock);
548}
549
550void
551dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
552{
553 dsl_pool_t *dp = dd->dd_pool;
554
555 ASSERT(dd->dd_phys);

--- 19 unchanged lines hidden (view full) ---

575
576 dmu_buf_will_dirty(dd->dd_dbuf, tx);
577
578 mutex_enter(&dd->dd_lock);
579 ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
580 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
581 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
582 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
547}
548
549void
550dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
551{
552 dsl_pool_t *dp = dd->dd_pool;
553
554 ASSERT(dd->dd_phys);

--- 19 unchanged lines hidden (view full) ---

574
575 dmu_buf_will_dirty(dd->dd_dbuf, tx);
576
577 mutex_enter(&dd->dd_lock);
578 ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
579 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
580 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
581 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
583 dd->dd_phys->dd_used_bytes = dd->dd_used_bytes;
584 mutex_exit(&dd->dd_lock);
585
586 /* release the hold from dsl_dir_dirty */
587 dmu_buf_rele(dd->dd_dbuf, dd);
588}
589
590static uint64_t
582 mutex_exit(&dd->dd_lock);
583
584 /* release the hold from dsl_dir_dirty */
585 dmu_buf_rele(dd->dd_dbuf, dd);
586}
587
588static uint64_t
591dsl_dir_estimated_space(dsl_dir_t *dd)
589dsl_dir_space_towrite(dsl_dir_t *dd)
592{
590{
593 int64_t space;
591 uint64_t space = 0;
594 int i;
595
596 ASSERT(MUTEX_HELD(&dd->dd_lock));
597
592 int i;
593
594 ASSERT(MUTEX_HELD(&dd->dd_lock));
595
598 space = dd->dd_phys->dd_used_bytes;
599 ASSERT(space >= 0);
600 for (i = 0; i < TXG_SIZE; i++) {
601 space += dd->dd_space_towrite[i&TXG_MASK];
602 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
603 }
604 return (space);
605}
606
607/*

--- 17 unchanged lines hidden (view full) ---

625 if (dd->dd_parent != NULL) {
626 parentspace = dsl_dir_space_available(dd->dd_parent,
627 ancestor, delta, ondiskonly);
628 }
629
630 mutex_enter(&dd->dd_lock);
631 if (dd->dd_phys->dd_quota != 0)
632 quota = dd->dd_phys->dd_quota;
596 for (i = 0; i < TXG_SIZE; i++) {
597 space += dd->dd_space_towrite[i&TXG_MASK];
598 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
599 }
600 return (space);
601}
602
603/*

--- 17 unchanged lines hidden (view full) ---

621 if (dd->dd_parent != NULL) {
622 parentspace = dsl_dir_space_available(dd->dd_parent,
623 ancestor, delta, ondiskonly);
624 }
625
626 mutex_enter(&dd->dd_lock);
627 if (dd->dd_phys->dd_quota != 0)
628 quota = dd->dd_phys->dd_quota;
633 if (ondiskonly) {
634 used = dd->dd_used_bytes;
635 } else {
636 used = dsl_dir_estimated_space(dd);
637 }
638 if (dd == ancestor)
639 used += delta;
629 used = dd->dd_phys->dd_used_bytes;
630 if (!ondiskonly)
631 used += dsl_dir_space_towrite(dd);
640
641 if (dd->dd_parent == NULL) {
642 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
643 quota = MIN(quota, poolsize);
644 }
645
646 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
647 /*
648 * We have some space reserved, in addition to what our
649 * parent gave us.
650 */
651 parentspace += dd->dd_phys->dd_reserved - used;
652 }
653
632
633 if (dd->dd_parent == NULL) {
634 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
635 quota = MIN(quota, poolsize);
636 }
637
638 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
639 /*
640 * We have some space reserved, in addition to what our
641 * parent gave us.
642 */
643 parentspace += dd->dd_phys->dd_reserved - used;
644 }
645
646 if (dd == ancestor) {
647 ASSERT(delta <= 0);
648 ASSERT(used >= -delta);
649 used += delta;
650 if (parentspace != UINT64_MAX)
651 parentspace -= delta;
652 }
653
654 if (used > quota) {
655 /* over quota */
656 myspace = 0;
657
658 /*
659 * While it's OK to be a little over quota, if
660 * we think we are using more space than there
661 * is in the pool (which is already 1.6% more than

--- 11 unchanged lines hidden (view full) ---

673
674 mutex_exit(&dd->dd_lock);
675
676 return (myspace);
677}
678
679struct tempreserve {
680 list_node_t tr_node;
654 if (used > quota) {
655 /* over quota */
656 myspace = 0;
657
658 /*
659 * While it's OK to be a little over quota, if
660 * we think we are using more space than there
661 * is in the pool (which is already 1.6% more than

--- 11 unchanged lines hidden (view full) ---

673
674 mutex_exit(&dd->dd_lock);
675
676 return (myspace);
677}
678
679struct tempreserve {
680 list_node_t tr_node;
681 dsl_pool_t *tr_dp;
681 dsl_dir_t *tr_ds;
682 uint64_t tr_size;
683};
684
682 dsl_dir_t *tr_ds;
683 uint64_t tr_size;
684};
685
685/*
686 * Reserve space in this dsl_dir, to be used in this tx's txg.
687 * After the space has been dirtied (and thus
688 * dsl_dir_willuse_space() has been called), the reservation should
689 * be canceled, using dsl_dir_tempreserve_clear().
690 */
691static int
686static int
692dsl_dir_tempreserve_impl(dsl_dir_t *dd,
693 uint64_t asize, boolean_t netfree, list_t *tr_list, dmu_tx_t *tx)
687dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
688 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
689 dmu_tx_t *tx, boolean_t first)
694{
695 uint64_t txg = tx->tx_txg;
690{
691 uint64_t txg = tx->tx_txg;
696 uint64_t est_used, quota, parent_rsrv;
697 int edquot = EDQUOT;
692 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
693 struct tempreserve *tr;
694 int enospc = EDQUOT;
698 int txgidx = txg & TXG_MASK;
699 int i;
695 int txgidx = txg & TXG_MASK;
696 int i;
700 struct tempreserve *tr;
697 uint64_t ref_rsrv = 0;
701
702 ASSERT3U(txg, !=, 0);
698
699 ASSERT3U(txg, !=, 0);
703 ASSERT3S(asize, >=, 0);
700 ASSERT3S(asize, >, 0);
704
705 mutex_enter(&dd->dd_lock);
701
702 mutex_enter(&dd->dd_lock);
703
706 /*
707 * Check against the dsl_dir's quota. We don't add in the delta
708 * when checking for over-quota because they get one free hit.
709 */
704 /*
705 * Check against the dsl_dir's quota. We don't add in the delta
706 * when checking for over-quota because they get one free hit.
707 */
710 est_used = dsl_dir_estimated_space(dd);
708 est_inflight = dsl_dir_space_towrite(dd);
711 for (i = 0; i < TXG_SIZE; i++)
709 for (i = 0; i < TXG_SIZE; i++)
712 est_used += dd->dd_tempreserved[i];
710 est_inflight += dd->dd_tempreserved[i];
711 used_on_disk = dd->dd_phys->dd_used_bytes;
713
712
714 quota = UINT64_MAX;
713 /*
714 * On the first iteration, fetch the dataset's used-on-disk and
715 * refreservation values. Also, if checkrefquota is set, test if
716 * allocating this space would exceed the dataset's refquota.
717 */
718 if (first && tx->tx_objset) {
719 int error;
720 dsl_dataset_t *ds = tx->tx_objset->os->os_dsl_dataset;
715
721
716 if (dd->dd_phys->dd_quota)
722 error = dsl_dataset_check_quota(ds, checkrefquota,
723 asize, est_inflight, &used_on_disk, &ref_rsrv);
724 if (error) {
725 mutex_exit(&dd->dd_lock);
726 return (error);
727 }
728 }
729
730 /*
731 * If this transaction will result in a net free of space,
732 * we want to let it through.
733 */
734 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
735 quota = UINT64_MAX;
736 else
717 quota = dd->dd_phys->dd_quota;
718
719 /*
737 quota = dd->dd_phys->dd_quota;
738
739 /*
720 * If this transaction will result in a net free of space, we want
721 * to let it through, but we have to be careful: the space that it
722 * frees won't become available until *after* this txg syncs.
723 * Therefore, to ensure that it's possible to remove files from
724 * a full pool without inducing transient overcommits, we throttle
740 * Adjust the quota against the actual pool size at the root.
741 * To ensure that it's possible to remove files from a full
742 * pool without inducing transient overcommits, we throttle
725 * netfree transactions against a quota that is slightly larger,
726 * but still within the pool's allocation slop. In cases where
727 * we're very close to full, this will allow a steady trickle of
728 * removes to get through.
729 */
730 if (dd->dd_parent == NULL) {
731 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
732 if (poolsize < quota) {
733 quota = poolsize;
743 * netfree transactions against a quota that is slightly larger,
744 * but still within the pool's allocation slop. In cases where
745 * we're very close to full, this will allow a steady trickle of
746 * removes to get through.
747 */
748 if (dd->dd_parent == NULL) {
749 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
750 if (poolsize < quota) {
751 quota = poolsize;
734 edquot = ENOSPC;
752 enospc = ENOSPC;
735 }
753 }
736 } else if (netfree) {
737 quota = UINT64_MAX;
738 }
739
740 /*
741 * If they are requesting more space, and our current estimate
754 }
755
756 /*
757 * If they are requesting more space, and our current estimate
742 * is over quota. They get to try again unless the actual
758 * is over quota, they get to try again unless the actual
743 * on-disk is over quota and there are no pending changes (which
744 * may free up space for us).
745 */
759 * on-disk is over quota and there are no pending changes (which
760 * may free up space for us).
761 */
746 if (asize > 0 && est_used > quota) {
747 if (dd->dd_space_towrite[txg & TXG_MASK] != 0 ||
748 dd->dd_space_towrite[(txg-1) & TXG_MASK] != 0 ||
749 dd->dd_space_towrite[(txg-2) & TXG_MASK] != 0 ||
750 dd->dd_used_bytes < quota)
751 edquot = ERESTART;
752 dprintf_dd(dd, "failing: used=%lluK est_used = %lluK "
762 if (used_on_disk + est_inflight > quota) {
763 if (est_inflight > 0 || used_on_disk < quota)
764 enospc = ERESTART;
765 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
753 "quota=%lluK tr=%lluK err=%d\n",
766 "quota=%lluK tr=%lluK err=%d\n",
754 dd->dd_used_bytes>>10, est_used>>10,
755 quota>>10, asize>>10, edquot);
767 used_on_disk>>10, est_inflight>>10,
768 quota>>10, asize>>10, enospc);
756 mutex_exit(&dd->dd_lock);
769 mutex_exit(&dd->dd_lock);
757 return (edquot);
770 return (enospc);
758 }
759
760 /* We need to up our estimated delta before dropping dd_lock */
761 dd->dd_tempreserved[txgidx] += asize;
762
771 }
772
773 /* We need to up our estimated delta before dropping dd_lock */
774 dd->dd_tempreserved[txgidx] += asize;
775
763 parent_rsrv = parent_delta(dd, est_used, asize);
776 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
777 asize - ref_rsrv);
764 mutex_exit(&dd->dd_lock);
765
778 mutex_exit(&dd->dd_lock);
779
766 tr = kmem_alloc(sizeof (struct tempreserve), KM_SLEEP);
780 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
767 tr->tr_ds = dd;
768 tr->tr_size = asize;
769 list_insert_tail(tr_list, tr);
770
771 /* see if it's OK with our parent */
772 if (dd->dd_parent && parent_rsrv) {
781 tr->tr_ds = dd;
782 tr->tr_size = asize;
783 list_insert_tail(tr_list, tr);
784
785 /* see if it's OK with our parent */
786 if (dd->dd_parent && parent_rsrv) {
787 boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
788
773 return (dsl_dir_tempreserve_impl(dd->dd_parent,
789 return (dsl_dir_tempreserve_impl(dd->dd_parent,
774 parent_rsrv, netfree, tr_list, tx));
790 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
775 } else {
776 return (0);
777 }
778}
779
780/*
781 * Reserve space in this dsl_dir, to be used in this tx's txg.
791 } else {
792 return (0);
793 }
794}
795
796/*
797 * Reserve space in this dsl_dir, to be used in this tx's txg.
782 * After the space has been dirtied (and thus
783 * dsl_dir_willuse_space() has been called), the reservation should
784 * be canceled, using dsl_dir_tempreserve_clear().
798 * After the space has been dirtied (and dsl_dir_willuse_space()
799 * has been called), the reservation should be canceled, using
800 * dsl_dir_tempreserve_clear().
785 */
786int
801 */
802int
787dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize,
788 uint64_t asize, uint64_t fsize, void **tr_cookiep, dmu_tx_t *tx)
803dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
804 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
789{
805{
790 int err = 0;
806 int err;
791 list_t *tr_list;
792
807 list_t *tr_list;
808
809 if (asize == 0) {
810 *tr_cookiep = NULL;
811 return (0);
812 }
813
793 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
794 list_create(tr_list, sizeof (struct tempreserve),
795 offsetof(struct tempreserve, tr_node));
814 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
815 list_create(tr_list, sizeof (struct tempreserve),
816 offsetof(struct tempreserve, tr_node));
796 ASSERT3S(asize, >=, 0);
817 ASSERT3S(asize, >, 0);
797 ASSERT3S(fsize, >=, 0);
798
818 ASSERT3S(fsize, >=, 0);
819
799 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
800 tr_list, tx);
801
820 err = arc_tempreserve_space(lsize, tx->tx_txg);
802 if (err == 0) {
803 struct tempreserve *tr;
804
821 if (err == 0) {
822 struct tempreserve *tr;
823
805 err = arc_tempreserve_space(lsize);
806 if (err == 0) {
807 tr = kmem_alloc(sizeof (struct tempreserve), KM_SLEEP);
808 tr->tr_ds = NULL;
809 tr->tr_size = lsize;
810 list_insert_tail(tr_list, tr);
824 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
825 tr->tr_size = lsize;
826 list_insert_tail(tr_list, tr);
827
828 err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
829 } else {
830 if (err == EAGAIN) {
831 txg_delay(dd->dd_pool, tx->tx_txg, 1);
832 err = ERESTART;
811 }
833 }
834 dsl_pool_memory_pressure(dd->dd_pool);
812 }
813
835 }
836
837 if (err == 0) {
838 struct tempreserve *tr;
839
840 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
841 tr->tr_dp = dd->dd_pool;
842 tr->tr_size = asize;
843 list_insert_tail(tr_list, tr);
844
845 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
846 FALSE, asize > usize, tr_list, tx, TRUE);
847 }
848
814 if (err)
815 dsl_dir_tempreserve_clear(tr_list, tx);
816 else
817 *tr_cookiep = tr_list;
849 if (err)
850 dsl_dir_tempreserve_clear(tr_list, tx);
851 else
852 *tr_cookiep = tr_list;
853
818 return (err);
819}
820
821/*
822 * Clear a temporary reservation that we previously made with
823 * dsl_dir_tempreserve_space().
824 */
825void
826dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
827{
828 int txgidx = tx->tx_txg & TXG_MASK;
829 list_t *tr_list = tr_cookie;
830 struct tempreserve *tr;
831
832 ASSERT3U(tx->tx_txg, !=, 0);
833
854 return (err);
855}
856
857/*
858 * Clear a temporary reservation that we previously made with
859 * dsl_dir_tempreserve_space().
860 */
861void
862dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
863{
864 int txgidx = tx->tx_txg & TXG_MASK;
865 list_t *tr_list = tr_cookie;
866 struct tempreserve *tr;
867
868 ASSERT3U(tx->tx_txg, !=, 0);
869
870 if (tr_cookie == NULL)
871 return;
872
834 while (tr = list_head(tr_list)) {
873 while (tr = list_head(tr_list)) {
835 if (tr->tr_ds == NULL) {
836 arc_tempreserve_clear(tr->tr_size);
837 } else {
874 if (tr->tr_dp) {
875 dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
876 } else if (tr->tr_ds) {
838 mutex_enter(&tr->tr_ds->dd_lock);
839 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
840 tr->tr_size);
841 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
842 mutex_exit(&tr->tr_ds->dd_lock);
877 mutex_enter(&tr->tr_ds->dd_lock);
878 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
879 tr->tr_size);
880 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
881 mutex_exit(&tr->tr_ds->dd_lock);
882 } else {
883 arc_tempreserve_clear(tr->tr_size);
843 }
844 list_remove(tr_list, tr);
845 kmem_free(tr, sizeof (struct tempreserve));
846 }
847
848 kmem_free(tr_list, sizeof (list_t));
849}
850
884 }
885 list_remove(tr_list, tr);
886 kmem_free(tr, sizeof (struct tempreserve));
887 }
888
889 kmem_free(tr_list, sizeof (list_t));
890}
891
851/*
852 * Call in open context when we think we're going to write/free space,
853 * eg. when dirtying data. Be conservative (ie. OK to write less than
854 * this or free more than this, but don't write more or free less).
855 */
856void
857dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
892static void
893dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
858{
859 int64_t parent_space;
860 uint64_t est_used;
861
862 mutex_enter(&dd->dd_lock);
863 if (space > 0)
864 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
865
894{
895 int64_t parent_space;
896 uint64_t est_used;
897
898 mutex_enter(&dd->dd_lock);
899 if (space > 0)
900 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
901
866 est_used = dsl_dir_estimated_space(dd);
902 est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
867 parent_space = parent_delta(dd, est_used, space);
868 mutex_exit(&dd->dd_lock);
869
870 /* Make sure that we clean up dd_space_to* */
871 dsl_dir_dirty(dd, tx);
872
873 /* XXX this is potentially expensive and unnecessary... */
874 if (parent_space && dd->dd_parent)
903 parent_space = parent_delta(dd, est_used, space);
904 mutex_exit(&dd->dd_lock);
905
906 /* Make sure that we clean up dd_space_to* */
907 dsl_dir_dirty(dd, tx);
908
909 /* XXX this is potentially expensive and unnecessary... */
910 if (parent_space && dd->dd_parent)
875 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
911 dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
876}
877
912}
913
914/*
915 * Call in open context when we think we're going to write/free space,
916 * eg. when dirtying data. Be conservative (ie. OK to write less than
917 * this or free more than this, but don't write more or free less).
918 */
919void
920dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
921{
922 dsl_pool_willuse_space(dd->dd_pool, space, tx);
923 dsl_dir_willuse_space_impl(dd, space, tx);
924}
925
878/* call from syncing context when we actually write/free space for this dd */
879void
926/* call from syncing context when we actually write/free space for this dd */
927void
880dsl_dir_diduse_space(dsl_dir_t *dd,
928dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
881 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
882{
883 int64_t accounted_delta;
929 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
930{
931 int64_t accounted_delta;
932 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
884
885 ASSERT(dmu_tx_is_syncing(tx));
933
934 ASSERT(dmu_tx_is_syncing(tx));
935 ASSERT(type < DD_USED_NUM);
886
887 dsl_dir_dirty(dd, tx);
888
936
937 dsl_dir_dirty(dd, tx);
938
889 mutex_enter(&dd->dd_lock);
890 accounted_delta = parent_delta(dd, dd->dd_used_bytes, used);
891 ASSERT(used >= 0 || dd->dd_used_bytes >= -used);
939 if (needlock)
940 mutex_enter(&dd->dd_lock);
941 accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used);
942 ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used);
892 ASSERT(compressed >= 0 ||
893 dd->dd_phys->dd_compressed_bytes >= -compressed);
894 ASSERT(uncompressed >= 0 ||
895 dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
943 ASSERT(compressed >= 0 ||
944 dd->dd_phys->dd_compressed_bytes >= -compressed);
945 ASSERT(uncompressed >= 0 ||
946 dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
896 dd->dd_used_bytes += used;
947 dd->dd_phys->dd_used_bytes += used;
897 dd->dd_phys->dd_uncompressed_bytes += uncompressed;
898 dd->dd_phys->dd_compressed_bytes += compressed;
948 dd->dd_phys->dd_uncompressed_bytes += uncompressed;
949 dd->dd_phys->dd_compressed_bytes += compressed;
899 mutex_exit(&dd->dd_lock);
900
950
951 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
952 ASSERT(used > 0 ||
953 dd->dd_phys->dd_used_breakdown[type] >= -used);
954 dd->dd_phys->dd_used_breakdown[type] += used;
955#ifdef DEBUG
956 dd_used_t t;
957 uint64_t u = 0;
958 for (t = 0; t < DD_USED_NUM; t++)
959 u += dd->dd_phys->dd_used_breakdown[t];
960 ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
961#endif
962 }
963 if (needlock)
964 mutex_exit(&dd->dd_lock);
965
901 if (dd->dd_parent != NULL) {
966 if (dd->dd_parent != NULL) {
902 dsl_dir_diduse_space(dd->dd_parent,
967 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
903 accounted_delta, compressed, uncompressed, tx);
968 accounted_delta, compressed, uncompressed, tx);
969 dsl_dir_transfer_space(dd->dd_parent,
970 used - accounted_delta,
971 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
904 }
905}
906
972 }
973}
974
907/* ARGSUSED */
975void
976dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
977 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
978{
979 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
980
981 ASSERT(dmu_tx_is_syncing(tx));
982 ASSERT(oldtype < DD_USED_NUM);
983 ASSERT(newtype < DD_USED_NUM);
984
985 if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
986 return;
987
988 dsl_dir_dirty(dd, tx);
989 if (needlock)
990 mutex_enter(&dd->dd_lock);
991 ASSERT(delta > 0 ?
992 dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
993 dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
994 ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
995 dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
996 dd->dd_phys->dd_used_breakdown[newtype] += delta;
997 if (needlock)
998 mutex_exit(&dd->dd_lock);
999}
1000
908static int
909dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
910{
911 dsl_dir_t *dd = arg1;
912 uint64_t *quotap = arg2;
913 uint64_t new_quota = *quotap;
914 int err = 0;
915 uint64_t towrite;
916
917 if (new_quota == 0)
918 return (0);
919
920 mutex_enter(&dd->dd_lock);
921 /*
922 * If we are doing the preliminary check in open context, and
923 * there are pending changes, then don't fail it, since the
1001static int
1002dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
1003{
1004 dsl_dir_t *dd = arg1;
1005 uint64_t *quotap = arg2;
1006 uint64_t new_quota = *quotap;
1007 int err = 0;
1008 uint64_t towrite;
1009
1010 if (new_quota == 0)
1011 return (0);
1012
1013 mutex_enter(&dd->dd_lock);
1014 /*
1015 * If we are doing the preliminary check in open context, and
1016 * there are pending changes, then don't fail it, since the
924 * pending changes could under-estimat the amount of space to be
1017 * pending changes could under-estimate the amount of space to be
925 * freed up.
926 */
1018 * freed up.
1019 */
927 towrite = dd->dd_space_towrite[0] + dd->dd_space_towrite[1] +
928 dd->dd_space_towrite[2] + dd->dd_space_towrite[3];
1020 towrite = dsl_dir_space_towrite(dd);
929 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
930 (new_quota < dd->dd_phys->dd_reserved ||
1021 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1022 (new_quota < dd->dd_phys->dd_reserved ||
931 new_quota < dsl_dir_estimated_space(dd))) {
1023 new_quota < dd->dd_phys->dd_used_bytes + towrite)) {
932 err = ENOSPC;
933 }
934 mutex_exit(&dd->dd_lock);
935 return (err);
936}
937
1024 err = ENOSPC;
1025 }
1026 mutex_exit(&dd->dd_lock);
1027 return (err);
1028}
1029
1030/* ARGSUSED */
938static void
1031static void
939dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1032dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
940{
941 dsl_dir_t *dd = arg1;
942 uint64_t *quotap = arg2;
943 uint64_t new_quota = *quotap;
944
945 dmu_buf_will_dirty(dd->dd_dbuf, tx);
946
947 mutex_enter(&dd->dd_lock);
948 dd->dd_phys->dd_quota = new_quota;
949 mutex_exit(&dd->dd_lock);
1033{
1034 dsl_dir_t *dd = arg1;
1035 uint64_t *quotap = arg2;
1036 uint64_t new_quota = *quotap;
1037
1038 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1039
1040 mutex_enter(&dd->dd_lock);
1041 dd->dd_phys->dd_quota = new_quota;
1042 mutex_exit(&dd->dd_lock);
1043
1044 spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
1045 tx, cr, "%lld dataset = %llu ",
1046 (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj);
950}
951
952int
953dsl_dir_set_quota(const char *ddname, uint64_t quota)
954{
955 dsl_dir_t *dd;
956 int err;
957
958 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
959 if (err)
960 return (err);
1047}
1048
1049int
1050dsl_dir_set_quota(const char *ddname, uint64_t quota)
1051{
1052 dsl_dir_t *dd;
1053 int err;
1054
1055 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1056 if (err)
1057 return (err);
961 /*
962 * If someone removes a file, then tries to set the quota, we
963 * want to make sure the file freeing takes effect.
964 */
965 txg_wait_open(dd->dd_pool, 0);
966
1058
967 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
968 dsl_dir_set_quota_sync, dd, &quota, 0);
1059 if (quota != dd->dd_phys->dd_quota) {
1060 /*
1061 * If someone removes a file, then tries to set the quota, we
1062 * want to make sure the file freeing takes effect.
1063 */
1064 txg_wait_open(dd->dd_pool, 0);
1065
1066 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1067 dsl_dir_set_quota_sync, dd, &quota, 0);
1068 }
969 dsl_dir_close(dd, FTAG);
970 return (err);
971}
972
1069 dsl_dir_close(dd, FTAG);
1070 return (err);
1071}
1072
973/* ARGSUSED */
974static int
1073int
975dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
976{
977 dsl_dir_t *dd = arg1;
978 uint64_t *reservationp = arg2;
979 uint64_t new_reservation = *reservationp;
980 uint64_t used, avail;
981 int64_t delta;
982
983 if (new_reservation > INT64_MAX)
984 return (EOVERFLOW);
985
986 /*
987 * If we are doing the preliminary check in open context, the
988 * space estimates may be inaccurate.
989 */
990 if (!dmu_tx_is_syncing(tx))
991 return (0);
992
993 mutex_enter(&dd->dd_lock);
1074dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1075{
1076 dsl_dir_t *dd = arg1;
1077 uint64_t *reservationp = arg2;
1078 uint64_t new_reservation = *reservationp;
1079 uint64_t used, avail;
1080 int64_t delta;
1081
1082 if (new_reservation > INT64_MAX)
1083 return (EOVERFLOW);
1084
1085 /*
1086 * If we are doing the preliminary check in open context, the
1087 * space estimates may be inaccurate.
1088 */
1089 if (!dmu_tx_is_syncing(tx))
1090 return (0);
1091
1092 mutex_enter(&dd->dd_lock);
994 used = dd->dd_used_bytes;
1093 used = dd->dd_phys->dd_used_bytes;
995 delta = MAX(used, new_reservation) -
996 MAX(used, dd->dd_phys->dd_reserved);
997 mutex_exit(&dd->dd_lock);
998
999 if (dd->dd_parent) {
1000 avail = dsl_dir_space_available(dd->dd_parent,
1001 NULL, 0, FALSE);
1002 } else {
1003 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1004 }
1005
1006 if (delta > 0 && delta > avail)
1007 return (ENOSPC);
1008 if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1009 new_reservation > dd->dd_phys->dd_quota)
1010 return (ENOSPC);
1011 return (0);
1012}
1013
1094 delta = MAX(used, new_reservation) -
1095 MAX(used, dd->dd_phys->dd_reserved);
1096 mutex_exit(&dd->dd_lock);
1097
1098 if (dd->dd_parent) {
1099 avail = dsl_dir_space_available(dd->dd_parent,
1100 NULL, 0, FALSE);
1101 } else {
1102 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1103 }
1104
1105 if (delta > 0 && delta > avail)
1106 return (ENOSPC);
1107 if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1108 new_reservation > dd->dd_phys->dd_quota)
1109 return (ENOSPC);
1110 return (0);
1111}
1112
1113/* ARGSUSED */
1014static void
1114static void
1015dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1115dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1016{
1017 dsl_dir_t *dd = arg1;
1018 uint64_t *reservationp = arg2;
1019 uint64_t new_reservation = *reservationp;
1020 uint64_t used;
1021 int64_t delta;
1022
1116{
1117 dsl_dir_t *dd = arg1;
1118 uint64_t *reservationp = arg2;
1119 uint64_t new_reservation = *reservationp;
1120 uint64_t used;
1121 int64_t delta;
1122
1123 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1124
1023 mutex_enter(&dd->dd_lock);
1125 mutex_enter(&dd->dd_lock);
1024 used = dd->dd_used_bytes;
1126 used = dd->dd_phys->dd_used_bytes;
1025 delta = MAX(used, new_reservation) -
1026 MAX(used, dd->dd_phys->dd_reserved);
1127 delta = MAX(used, new_reservation) -
1128 MAX(used, dd->dd_phys->dd_reserved);
1027 mutex_exit(&dd->dd_lock);
1028
1029 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1030 dd->dd_phys->dd_reserved = new_reservation;
1031
1032 if (dd->dd_parent != NULL) {
1033 /* Roll up this additional usage into our ancestors */
1129 dd->dd_phys->dd_reserved = new_reservation;
1130
1131 if (dd->dd_parent != NULL) {
1132 /* Roll up this additional usage into our ancestors */
1034 dsl_dir_diduse_space(dd->dd_parent, delta, 0, 0, tx);
1133 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1134 delta, 0, 0, tx);
1035 }
1135 }
1136 mutex_exit(&dd->dd_lock);
1137
1138 spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
1139 tx, cr, "%lld dataset = %llu",
1140 (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj);
1036}
1037
1038int
1039dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1040{
1041 dsl_dir_t *dd;
1042 int err;
1043

--- 25 unchanged lines hidden (view full) ---

1069 */
1070static int64_t
1071would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1072{
1073 if (dd == ancestor)
1074 return (delta);
1075
1076 mutex_enter(&dd->dd_lock);
1141}
1142
1143int
1144dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1145{
1146 dsl_dir_t *dd;
1147 int err;
1148

--- 25 unchanged lines hidden (view full) ---

1174 */
1175static int64_t
1176would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1177{
1178 if (dd == ancestor)
1179 return (delta);
1180
1181 mutex_enter(&dd->dd_lock);
1077 delta = parent_delta(dd, dd->dd_used_bytes, delta);
1182 delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1078 mutex_exit(&dd->dd_lock);
1079 return (would_change(dd->dd_parent, delta, ancestor));
1080}
1081
1082struct renamearg {
1083 dsl_dir_t *newparent;
1084 const char *mynewname;
1085};
1086
1183 mutex_exit(&dd->dd_lock);
1184 return (would_change(dd->dd_parent, delta, ancestor));
1185}
1186
1187struct renamearg {
1188 dsl_dir_t *newparent;
1189 const char *mynewname;
1190};
1191
1087/* ARGSUSED */
1192/*ARGSUSED*/
1088static int
1089dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1090{
1091 dsl_dir_t *dd = arg1;
1092 struct renamearg *ra = arg2;
1093 dsl_pool_t *dp = dd->dd_pool;
1094 objset_t *mos = dp->dp_meta_objset;
1095 int err;

--- 9 unchanged lines hidden (view full) ---

1105 if (err == 0)
1106 return (EEXIST);
1107 if (err != ENOENT)
1108 return (err);
1109
1110 if (ra->newparent != dd->dd_parent) {
1111 /* is there enough space? */
1112 uint64_t myspace =
1193static int
1194dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1195{
1196 dsl_dir_t *dd = arg1;
1197 struct renamearg *ra = arg2;
1198 dsl_pool_t *dp = dd->dd_pool;
1199 objset_t *mos = dp->dp_meta_objset;
1200 int err;

--- 9 unchanged lines hidden (view full) ---

1210 if (err == 0)
1211 return (EEXIST);
1212 if (err != ENOENT)
1213 return (err);
1214
1215 if (ra->newparent != dd->dd_parent) {
1216 /* is there enough space? */
1217 uint64_t myspace =
1113 MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1218 MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1114
1115 /* no rename into our descendant */
1116 if (closest_common_ancestor(dd, ra->newparent) == dd)
1117 return (EINVAL);
1118
1119 if (err = dsl_dir_transfer_possible(dd->dd_parent,
1120 ra->newparent, myspace))
1121 return (err);
1122 }
1123
1124 return (0);
1125}
1126
1127static void
1219
1220 /* no rename into our descendant */
1221 if (closest_common_ancestor(dd, ra->newparent) == dd)
1222 return (EINVAL);
1223
1224 if (err = dsl_dir_transfer_possible(dd->dd_parent,
1225 ra->newparent, myspace))
1226 return (err);
1227 }
1228
1229 return (0);
1230}
1231
1232static void
1128dsl_dir_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1233dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1129{
1130 dsl_dir_t *dd = arg1;
1131 struct renamearg *ra = arg2;
1132 dsl_pool_t *dp = dd->dd_pool;
1133 objset_t *mos = dp->dp_meta_objset;
1134 int err;
1135
1136 ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1137
1138 if (ra->newparent != dd->dd_parent) {
1234{
1235 dsl_dir_t *dd = arg1;
1236 struct renamearg *ra = arg2;
1237 dsl_pool_t *dp = dd->dd_pool;
1238 objset_t *mos = dp->dp_meta_objset;
1239 int err;
1240
1241 ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1242
1243 if (ra->newparent != dd->dd_parent) {
1139 uint64_t myspace =
1140 MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1141
1142 dsl_dir_diduse_space(dd->dd_parent, -myspace,
1244 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1245 -dd->dd_phys->dd_used_bytes,
1143 -dd->dd_phys->dd_compressed_bytes,
1144 -dd->dd_phys->dd_uncompressed_bytes, tx);
1246 -dd->dd_phys->dd_compressed_bytes,
1247 -dd->dd_phys->dd_uncompressed_bytes, tx);
1145 dsl_dir_diduse_space(ra->newparent, myspace,
1248 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
1249 dd->dd_phys->dd_used_bytes,
1146 dd->dd_phys->dd_compressed_bytes,
1147 dd->dd_phys->dd_uncompressed_bytes, tx);
1250 dd->dd_phys->dd_compressed_bytes,
1251 dd->dd_phys->dd_uncompressed_bytes, tx);
1252
1253 if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
1254 uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
1255 dd->dd_phys->dd_used_bytes;
1256
1257 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1258 -unused_rsrv, 0, 0, tx);
1259 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
1260 unused_rsrv, 0, 0, tx);
1261 }
1148 }
1149
1150 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1151
1152 /* remove from old parent zapobj */
1153 err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1154 dd->dd_myname, tx);
1155 ASSERT3U(err, ==, 0);
1156
1157 (void) strcpy(dd->dd_myname, ra->mynewname);
1158 dsl_dir_close(dd->dd_parent, dd);
1159 dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1160 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1161 ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1162
1163 /* add to new parent zapobj */
1164 err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1165 dd->dd_myname, 8, 1, &dd->dd_object, tx);
1166 ASSERT3U(err, ==, 0);
1262 }
1263
1264 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1265
1266 /* remove from old parent zapobj */
1267 err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1268 dd->dd_myname, tx);
1269 ASSERT3U(err, ==, 0);
1270
1271 (void) strcpy(dd->dd_myname, ra->mynewname);
1272 dsl_dir_close(dd->dd_parent, dd);
1273 dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1274 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1275 ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1276
1277 /* add to new parent zapobj */
1278 err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1279 dd->dd_myname, 8, 1, &dd->dd_object, tx);
1280 ASSERT3U(err, ==, 0);
1281
1282 spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa,
1283 tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
1167}
1168
1169int
1170dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1171{
1172 struct renamearg ra;
1173 int err;
1174

--- 9 unchanged lines hidden (view full) ---

1184 }
1185
1186 /* new name should not already exist */
1187 if (ra.mynewname == NULL) {
1188 err = EEXIST;
1189 goto out;
1190 }
1191
1284}
1285
1286int
1287dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1288{
1289 struct renamearg ra;
1290 int err;
1291

--- 9 unchanged lines hidden (view full) ---

1301 }
1302
1303 /* new name should not already exist */
1304 if (ra.mynewname == NULL) {
1305 err = EEXIST;
1306 goto out;
1307 }
1308
1192
1193 err = dsl_sync_task_do(dd->dd_pool,
1194 dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1195
1196out:
1197 dsl_dir_close(ra.newparent, FTAG);
1198 return (err);
1199}
1200

--- 15 unchanged lines hidden ---
1309 err = dsl_sync_task_do(dd->dd_pool,
1310 dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1311
1312out:
1313 dsl_dir_close(ra.newparent, FTAG);
1314 return (err);
1315}
1316

--- 15 unchanged lines hidden ---