OpenZFS 6676 - Race between unique_insert() and unique_remove() causes ZFS fsid change
[zfs.git] / module / zfs / dsl_dir.c
blob305a87ed9a31fa09498ba0976a00e45deca4327a
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Martin Matuska. All rights reserved.
25 * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
30 #include <sys/dmu.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/dsl_deleg.h>
38 #include <sys/dmu_impl.h>
39 #include <sys/spa.h>
40 #include <sys/metaslab.h>
41 #include <sys/zap.h>
42 #include <sys/zio.h>
43 #include <sys/arc.h>
44 #include <sys/sunddi.h>
45 #include <sys/zfeature.h>
46 #include <sys/policy.h>
47 #include <sys/zfs_znode.h>
48 #include <sys/zvol.h>
49 #include "zfs_namecheck.h"
50 #include "zfs_prop.h"
53 * Filesystem and Snapshot Limits
54 * ------------------------------
56 * These limits are used to restrict the number of filesystems and/or snapshots
57 * that can be created at a given level in the tree or below. A typical
58 * use-case is with a delegated dataset where the administrator wants to ensure
59 * that a user within the zone is not creating too many additional filesystems
60 * or snapshots, even though they're not exceeding their space quota.
62 * The filesystem and snapshot counts are stored as extensible properties. This
63 * capability is controlled by a feature flag and must be enabled to be used.
64 * Once enabled, the feature is not active until the first limit is set. At
65 * that point, future operations to create/destroy filesystems or snapshots
66 * will validate and update the counts.
68 * Because the count properties will not exist before the feature is active,
69 * the counts are updated when a limit is first set on an uninitialized
70 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
71 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
72 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
73 * snapshot count properties on a node indicate uninitialized counts on that
74 * node.) When first setting a limit on an uninitialized node, the code starts
75 * at the filesystem with the new limit and descends into all sub-filesystems
76 * to add the count properties.
78 * In practice this is lightweight since a limit is typically set when the
79 * filesystem is created and thus has no children. Once valid, changing the
80 * limit value won't require a re-traversal since the counts are already valid.
81 * When recursively fixing the counts, if a node with a limit is encountered
82 * during the descent, the counts are known to be valid and there is no need to
83 * descend into that filesystem's children. The counts on filesystems above the
84 * one with the new limit will still be uninitialized, unless a limit is
85 * eventually set on one of those filesystems. The counts are always recursively
86 * updated when a limit is set on a dataset, unless there is already a limit.
87 * When a new limit value is set on a filesystem with an existing limit, it is
88 * possible for the new limit to be less than the current count at that level
89 * since a user who can change the limit is also allowed to exceed the limit.
91 * Once the feature is active, then whenever a filesystem or snapshot is
92 * created, the code recurses up the tree, validating the new count against the
93 * limit at each initialized level. In practice, most levels will not have a
94 * limit set. If there is a limit at any initialized level up the tree, the
95 * check must pass or the creation will fail. Likewise, when a filesystem or
96 * snapshot is destroyed, the counts are recursively adjusted all the way up
97 * the initizized nodes in the tree. Renaming a filesystem into different point
98 * in the tree will first validate, then update the counts on each branch up to
99 * the common ancestor. A receive will also validate the counts and then update
100 * them.
102 * An exception to the above behavior is that the limit is not enforced if the
103 * user has permission to modify the limit. This is primarily so that
104 * recursive snapshots in the global zone always work. We want to prevent a
105 * denial-of-service in which a lower level delegated dataset could max out its
106 * limit and thus block recursive snapshots from being taken in the global zone.
107 * Because of this, it is possible for the snapshot count to be over the limit
108 * and snapshots taken in the global zone could cause a lower level dataset to
109 * hit or exceed its limit. The administrator taking the global zone recursive
110 * snapshot should be aware of this side-effect and behave accordingly.
111 * For consistency, the filesystem limit is also not enforced if the user can
112 * modify the limit.
114 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
115 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
116 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
117 * dsl_dir_init_fs_ss_count().
119 * There is a special case when we receive a filesystem that already exists. In
120 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
121 * never update the filesystem counts for temporary clones.
123 * Likewise, we do not update the snapshot counts for temporary snapshots,
124 * such as those created by zfs diff.
127 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
129 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
131 static void
132 dsl_dir_evict_async(void *dbu)
134 dsl_dir_t *dd = dbu;
135 int t;
136 ASSERTV(dsl_pool_t *dp = dd->dd_pool);
138 dd->dd_dbuf = NULL;
140 for (t = 0; t < TXG_SIZE; t++) {
141 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
142 ASSERT(dd->dd_tempreserved[t] == 0);
143 ASSERT(dd->dd_space_towrite[t] == 0);
146 if (dd->dd_parent)
147 dsl_dir_async_rele(dd->dd_parent, dd);
149 spa_async_close(dd->dd_pool->dp_spa, dd);
151 dsl_prop_fini(dd);
152 mutex_destroy(&dd->dd_lock);
153 kmem_free(dd, sizeof (dsl_dir_t));
157 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
158 const char *tail, void *tag, dsl_dir_t **ddp)
160 dmu_buf_t *dbuf;
161 dsl_dir_t *dd;
162 int err;
164 ASSERT(dsl_pool_config_held(dp));
166 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
167 if (err != 0)
168 return (err);
169 dd = dmu_buf_get_user(dbuf);
170 #ifdef ZFS_DEBUG
172 dmu_object_info_t doi;
173 dmu_object_info_from_db(dbuf, &doi);
174 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
175 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
177 #endif
178 if (dd == NULL) {
179 dsl_dir_t *winner;
181 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
182 dd->dd_object = ddobj;
183 dd->dd_dbuf = dbuf;
184 dd->dd_pool = dp;
185 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
186 dsl_prop_init(dd);
188 dsl_dir_snap_cmtime_update(dd);
190 if (dsl_dir_phys(dd)->dd_parent_obj) {
191 err = dsl_dir_hold_obj(dp,
192 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
193 &dd->dd_parent);
194 if (err != 0)
195 goto errout;
196 if (tail) {
197 #ifdef ZFS_DEBUG
198 uint64_t foundobj;
200 err = zap_lookup(dp->dp_meta_objset,
201 dsl_dir_phys(dd->dd_parent)->
202 dd_child_dir_zapobj, tail,
203 sizeof (foundobj), 1, &foundobj);
204 ASSERT(err || foundobj == ddobj);
205 #endif
206 (void) strlcpy(dd->dd_myname, tail,
207 sizeof (dd->dd_myname));
208 } else {
209 err = zap_value_search(dp->dp_meta_objset,
210 dsl_dir_phys(dd->dd_parent)->
211 dd_child_dir_zapobj,
212 ddobj, 0, dd->dd_myname);
214 if (err != 0)
215 goto errout;
216 } else {
217 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
220 if (dsl_dir_is_clone(dd)) {
221 dmu_buf_t *origin_bonus;
222 dsl_dataset_phys_t *origin_phys;
225 * We can't open the origin dataset, because
226 * that would require opening this dsl_dir.
227 * Just look at its phys directly instead.
229 err = dmu_bonus_hold(dp->dp_meta_objset,
230 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
231 &origin_bonus);
232 if (err != 0)
233 goto errout;
234 origin_phys = origin_bonus->db_data;
235 dd->dd_origin_txg =
236 origin_phys->ds_creation_txg;
237 dmu_buf_rele(origin_bonus, FTAG);
240 dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
241 &dd->dd_dbuf);
242 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
243 if (winner != NULL) {
244 if (dd->dd_parent)
245 dsl_dir_rele(dd->dd_parent, dd);
246 dsl_prop_fini(dd);
247 mutex_destroy(&dd->dd_lock);
248 kmem_free(dd, sizeof (dsl_dir_t));
249 dd = winner;
250 } else {
251 spa_open_ref(dp->dp_spa, dd);
256 * The dsl_dir_t has both open-to-close and instantiate-to-evict
257 * holds on the spa. We need the open-to-close holds because
258 * otherwise the spa_refcnt wouldn't change when we open a
259 * dir which the spa also has open, so we could incorrectly
260 * think it was OK to unload/export/destroy the pool. We need
261 * the instantiate-to-evict hold because the dsl_dir_t has a
262 * pointer to the dd_pool, which has a pointer to the spa_t.
264 spa_open_ref(dp->dp_spa, tag);
265 ASSERT3P(dd->dd_pool, ==, dp);
266 ASSERT3U(dd->dd_object, ==, ddobj);
267 ASSERT3P(dd->dd_dbuf, ==, dbuf);
268 *ddp = dd;
269 return (0);
271 errout:
272 if (dd->dd_parent)
273 dsl_dir_rele(dd->dd_parent, dd);
274 dsl_prop_fini(dd);
275 mutex_destroy(&dd->dd_lock);
276 kmem_free(dd, sizeof (dsl_dir_t));
277 dmu_buf_rele(dbuf, tag);
278 return (err);
281 void
282 dsl_dir_rele(dsl_dir_t *dd, void *tag)
284 dprintf_dd(dd, "%s\n", "");
285 spa_close(dd->dd_pool->dp_spa, tag);
286 dmu_buf_rele(dd->dd_dbuf, tag);
290 * Remove a reference to the given dsl dir that is being asynchronously
291 * released. Async releases occur from a taskq performing eviction of
292 * dsl datasets and dirs. This process is identical to a normal release
293 * with the exception of using the async API for releasing the reference on
294 * the spa.
296 void
297 dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
299 dprintf_dd(dd, "%s\n", "");
300 spa_async_close(dd->dd_pool->dp_spa, tag);
301 dmu_buf_rele(dd->dd_dbuf, tag);
304 /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
305 void
306 dsl_dir_name(dsl_dir_t *dd, char *buf)
308 if (dd->dd_parent) {
309 dsl_dir_name(dd->dd_parent, buf);
310 VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
311 ZFS_MAX_DATASET_NAME_LEN);
312 } else {
313 buf[0] = '\0';
315 if (!MUTEX_HELD(&dd->dd_lock)) {
317 * recursive mutex so that we can use
318 * dprintf_dd() with dd_lock held
320 mutex_enter(&dd->dd_lock);
321 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
322 <, ZFS_MAX_DATASET_NAME_LEN);
323 mutex_exit(&dd->dd_lock);
324 } else {
325 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
326 <, ZFS_MAX_DATASET_NAME_LEN);
330 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
332 dsl_dir_namelen(dsl_dir_t *dd)
334 int result = 0;
336 if (dd->dd_parent) {
337 /* parent's name + 1 for the "/" */
338 result = dsl_dir_namelen(dd->dd_parent) + 1;
341 if (!MUTEX_HELD(&dd->dd_lock)) {
342 /* see dsl_dir_name */
343 mutex_enter(&dd->dd_lock);
344 result += strlen(dd->dd_myname);
345 mutex_exit(&dd->dd_lock);
346 } else {
347 result += strlen(dd->dd_myname);
350 return (result);
353 static int
354 getcomponent(const char *path, char *component, const char **nextp)
356 char *p;
358 if ((path == NULL) || (path[0] == '\0'))
359 return (SET_ERROR(ENOENT));
360 /* This would be a good place to reserve some namespace... */
361 p = strpbrk(path, "/@");
362 if (p && (p[1] == '/' || p[1] == '@')) {
363 /* two separators in a row */
364 return (SET_ERROR(EINVAL));
366 if (p == NULL || p == path) {
368 * if the first thing is an @ or /, it had better be an
369 * @ and it had better not have any more ats or slashes,
370 * and it had better have something after the @.
372 if (p != NULL &&
373 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
374 return (SET_ERROR(EINVAL));
375 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
376 return (SET_ERROR(ENAMETOOLONG));
377 (void) strcpy(component, path);
378 p = NULL;
379 } else if (p[0] == '/') {
380 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
381 return (SET_ERROR(ENAMETOOLONG));
382 (void) strncpy(component, path, p - path);
383 component[p - path] = '\0';
384 p++;
385 } else if (p[0] == '@') {
387 * if the next separator is an @, there better not be
388 * any more slashes.
390 if (strchr(path, '/'))
391 return (SET_ERROR(EINVAL));
392 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
393 return (SET_ERROR(ENAMETOOLONG));
394 (void) strncpy(component, path, p - path);
395 component[p - path] = '\0';
396 } else {
397 panic("invalid p=%p", (void *)p);
399 *nextp = p;
400 return (0);
404 * Return the dsl_dir_t, and possibly the last component which couldn't
405 * be found in *tail. The name must be in the specified dsl_pool_t. This
406 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
407 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
408 * (*tail)[0] == '@' means that the last component is a snapshot.
411 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
412 dsl_dir_t **ddp, const char **tailp)
414 char *buf;
415 const char *spaname, *next, *nextnext = NULL;
416 int err;
417 dsl_dir_t *dd;
418 uint64_t ddobj;
420 buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
421 err = getcomponent(name, buf, &next);
422 if (err != 0)
423 goto error;
425 /* Make sure the name is in the specified pool. */
426 spaname = spa_name(dp->dp_spa);
427 if (strcmp(buf, spaname) != 0) {
428 err = SET_ERROR(EXDEV);
429 goto error;
432 ASSERT(dsl_pool_config_held(dp));
434 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
435 if (err != 0) {
436 goto error;
439 while (next != NULL) {
440 dsl_dir_t *child_dd;
441 err = getcomponent(next, buf, &nextnext);
442 if (err != 0)
443 break;
444 ASSERT(next[0] != '\0');
445 if (next[0] == '@')
446 break;
447 dprintf("looking up %s in obj%lld\n",
448 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
450 err = zap_lookup(dp->dp_meta_objset,
451 dsl_dir_phys(dd)->dd_child_dir_zapobj,
452 buf, sizeof (ddobj), 1, &ddobj);
453 if (err != 0) {
454 if (err == ENOENT)
455 err = 0;
456 break;
459 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
460 if (err != 0)
461 break;
462 dsl_dir_rele(dd, tag);
463 dd = child_dd;
464 next = nextnext;
467 if (err != 0) {
468 dsl_dir_rele(dd, tag);
469 goto error;
473 * It's an error if there's more than one component left, or
474 * tailp==NULL and there's any component left.
476 if (next != NULL &&
477 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
478 /* bad path name */
479 dsl_dir_rele(dd, tag);
480 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
481 err = SET_ERROR(ENOENT);
483 if (tailp != NULL)
484 *tailp = next;
485 *ddp = dd;
486 error:
487 kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);
488 return (err);
492 * If the counts are already initialized for this filesystem and its
493 * descendants then do nothing, otherwise initialize the counts.
495 * The counts on this filesystem, and those below, may be uninitialized due to
496 * either the use of a pre-existing pool which did not support the
497 * filesystem/snapshot limit feature, or one in which the feature had not yet
498 * been enabled.
500 * Recursively descend the filesystem tree and update the filesystem/snapshot
501 * counts on each filesystem below, then update the cumulative count on the
502 * current filesystem. If the filesystem already has a count set on it,
503 * then we know that its counts, and the counts on the filesystems below it,
504 * are already correct, so we don't have to update this filesystem.
506 static void
507 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
509 uint64_t my_fs_cnt = 0;
510 uint64_t my_ss_cnt = 0;
511 dsl_pool_t *dp = dd->dd_pool;
512 objset_t *os = dp->dp_meta_objset;
513 zap_cursor_t *zc;
514 zap_attribute_t *za;
515 dsl_dataset_t *ds;
517 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
518 ASSERT(dsl_pool_config_held(dp));
519 ASSERT(dmu_tx_is_syncing(tx));
521 dsl_dir_zapify(dd, tx);
524 * If the filesystem count has already been initialized then we
525 * don't need to recurse down any further.
527 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
528 return;
530 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
531 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
533 /* Iterate my child dirs */
534 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
535 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
536 dsl_dir_t *chld_dd;
537 uint64_t count;
539 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
540 &chld_dd));
543 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
544 * temporary datasets.
546 if (chld_dd->dd_myname[0] == '$' ||
547 chld_dd->dd_myname[0] == '%') {
548 dsl_dir_rele(chld_dd, FTAG);
549 continue;
552 my_fs_cnt++; /* count this child */
554 dsl_dir_init_fs_ss_count(chld_dd, tx);
556 VERIFY0(zap_lookup(os, chld_dd->dd_object,
557 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
558 my_fs_cnt += count;
559 VERIFY0(zap_lookup(os, chld_dd->dd_object,
560 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
561 my_ss_cnt += count;
563 dsl_dir_rele(chld_dd, FTAG);
565 zap_cursor_fini(zc);
566 /* Count my snapshots (we counted children's snapshots above) */
567 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
568 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
570 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
571 zap_cursor_retrieve(zc, za) == 0;
572 zap_cursor_advance(zc)) {
573 /* Don't count temporary snapshots */
574 if (za->za_name[0] != '%')
575 my_ss_cnt++;
577 zap_cursor_fini(zc);
579 dsl_dataset_rele(ds, FTAG);
581 kmem_free(zc, sizeof (zap_cursor_t));
582 kmem_free(za, sizeof (zap_attribute_t));
584 /* we're in a sync task, update counts */
585 dmu_buf_will_dirty(dd->dd_dbuf, tx);
586 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
587 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
588 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
589 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
592 static int
593 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
595 char *ddname = (char *)arg;
596 dsl_pool_t *dp = dmu_tx_pool(tx);
597 dsl_dataset_t *ds;
598 dsl_dir_t *dd;
599 int error;
601 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
602 if (error != 0)
603 return (error);
605 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
606 dsl_dataset_rele(ds, FTAG);
607 return (SET_ERROR(ENOTSUP));
610 dd = ds->ds_dir;
611 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
612 dsl_dir_is_zapified(dd) &&
613 zap_contains(dp->dp_meta_objset, dd->dd_object,
614 DD_FIELD_FILESYSTEM_COUNT) == 0) {
615 dsl_dataset_rele(ds, FTAG);
616 return (SET_ERROR(EALREADY));
619 dsl_dataset_rele(ds, FTAG);
620 return (0);
623 static void
624 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
626 char *ddname = (char *)arg;
627 dsl_pool_t *dp = dmu_tx_pool(tx);
628 dsl_dataset_t *ds;
629 spa_t *spa;
631 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
633 spa = dsl_dataset_get_spa(ds);
635 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
637 * Since the feature was not active and we're now setting a
638 * limit, increment the feature-active counter so that the
639 * feature becomes active for the first time.
641 * We are already in a sync task so we can update the MOS.
643 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
647 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
648 * we need to ensure the counts are correct. Descend down the tree from
649 * this point and update all of the counts to be accurate.
651 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
653 dsl_dataset_rele(ds, FTAG);
657 * Make sure the feature is enabled and activate it if necessary.
658 * Since we're setting a limit, ensure the on-disk counts are valid.
659 * This is only called by the ioctl path when setting a limit value.
661 * We do not need to validate the new limit, since users who can change the
662 * limit are also allowed to exceed the limit.
665 dsl_dir_activate_fs_ss_limit(const char *ddname)
667 int error;
669 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
670 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
671 ZFS_SPACE_CHECK_RESERVED);
673 if (error == EALREADY)
674 error = 0;
676 return (error);
680 * Used to determine if the filesystem_limit or snapshot_limit should be
681 * enforced. We allow the limit to be exceeded if the user has permission to
682 * write the property value. We pass in the creds that we got in the open
683 * context since we will always be the GZ root in syncing context. We also have
684 * to handle the case where we are allowed to change the limit on the current
685 * dataset, but there may be another limit in the tree above.
687 * We can never modify these two properties within a non-global zone. In
688 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
689 * can't use that function since we are already holding the dp_config_rwlock.
690 * In addition, we already have the dd and dealing with snapshots is simplified
691 * in this code.
694 typedef enum {
695 ENFORCE_ALWAYS,
696 ENFORCE_NEVER,
697 ENFORCE_ABOVE
698 } enforce_res_t;
700 static enforce_res_t
701 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
703 enforce_res_t enforce = ENFORCE_ALWAYS;
704 uint64_t obj;
705 dsl_dataset_t *ds;
706 uint64_t zoned;
708 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
709 prop == ZFS_PROP_SNAPSHOT_LIMIT);
711 #ifdef _KERNEL
712 if (crgetzoneid(cr) != GLOBAL_ZONEID)
713 return (ENFORCE_ALWAYS);
715 if (secpolicy_zfs(cr) == 0)
716 return (ENFORCE_NEVER);
717 #endif
719 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
720 return (ENFORCE_ALWAYS);
722 ASSERT(dsl_pool_config_held(dd->dd_pool));
724 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
725 return (ENFORCE_ALWAYS);
727 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
728 /* Only root can access zoned fs's from the GZ */
729 enforce = ENFORCE_ALWAYS;
730 } else {
731 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
732 enforce = ENFORCE_ABOVE;
735 dsl_dataset_rele(ds, FTAG);
736 return (enforce);
740 * Check if adding additional child filesystem(s) would exceed any filesystem
741 * limits or adding additional snapshot(s) would exceed any snapshot limits.
742 * The prop argument indicates which limit to check.
744 * Note that all filesystem limits up to the root (or the highest
745 * initialized) filesystem or the given ancestor must be satisfied.
748 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
749 dsl_dir_t *ancestor, cred_t *cr)
751 objset_t *os = dd->dd_pool->dp_meta_objset;
752 uint64_t limit, count;
753 char *count_prop;
754 enforce_res_t enforce;
755 int err = 0;
757 ASSERT(dsl_pool_config_held(dd->dd_pool));
758 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
759 prop == ZFS_PROP_SNAPSHOT_LIMIT);
762 * If we're allowed to change the limit, don't enforce the limit
763 * e.g. this can happen if a snapshot is taken by an administrative
764 * user in the global zone (i.e. a recursive snapshot by root).
765 * However, we must handle the case of delegated permissions where we
766 * are allowed to change the limit on the current dataset, but there
767 * is another limit in the tree above.
769 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
770 if (enforce == ENFORCE_NEVER)
771 return (0);
774 * e.g. if renaming a dataset with no snapshots, count adjustment
775 * is 0.
777 if (delta == 0)
778 return (0);
780 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
782 * We don't enforce the limit for temporary snapshots. This is
783 * indicated by a NULL cred_t argument.
785 if (cr == NULL)
786 return (0);
788 count_prop = DD_FIELD_SNAPSHOT_COUNT;
789 } else {
790 count_prop = DD_FIELD_FILESYSTEM_COUNT;
794 * If an ancestor has been provided, stop checking the limit once we
795 * hit that dir. We need this during rename so that we don't overcount
796 * the check once we recurse up to the common ancestor.
798 if (ancestor == dd)
799 return (0);
802 * If we hit an uninitialized node while recursing up the tree, we can
803 * stop since we know there is no limit here (or above). The counts are
804 * not valid on this node and we know we won't touch this node's counts.
806 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
807 count_prop, sizeof (count), 1, &count) == ENOENT)
808 return (0);
810 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
811 B_FALSE);
812 if (err != 0)
813 return (err);
815 /* Is there a limit which we've hit? */
816 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
817 return (SET_ERROR(EDQUOT));
819 if (dd->dd_parent != NULL)
820 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
821 ancestor, cr);
823 return (err);
827 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
828 * parents. When a new filesystem/snapshot is created, increment the count on
829 * all parents, and when a filesystem/snapshot is destroyed, decrement the
830 * count.
832 void
833 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
834 dmu_tx_t *tx)
836 int err;
837 objset_t *os = dd->dd_pool->dp_meta_objset;
838 uint64_t count;
840 ASSERT(dsl_pool_config_held(dd->dd_pool));
841 ASSERT(dmu_tx_is_syncing(tx));
842 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
843 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
846 * When we receive an incremental stream into a filesystem that already
847 * exists, a temporary clone is created. We don't count this temporary
848 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
849 * $MOS & $ORIGIN) objsets.
851 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
852 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
853 return;
856 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
858 if (delta == 0)
859 return;
862 * If we hit an uninitialized node while recursing up the tree, we can
863 * stop since we know the counts are not valid on this node and we
864 * know we shouldn't touch this node's counts. An uninitialized count
865 * on the node indicates that either the feature has not yet been
866 * activated or there are no limits on this part of the tree.
868 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
869 prop, sizeof (count), 1, &count)) == ENOENT)
870 return;
871 VERIFY0(err);
873 count += delta;
874 /* Use a signed verify to make sure we're not neg. */
875 VERIFY3S(count, >=, 0);
877 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
878 tx));
880 /* Roll up this additional count into our ancestors */
881 if (dd->dd_parent != NULL)
882 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
885 uint64_t
886 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
887 dmu_tx_t *tx)
889 objset_t *mos = dp->dp_meta_objset;
890 uint64_t ddobj;
891 dsl_dir_phys_t *ddphys;
892 dmu_buf_t *dbuf;
894 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
895 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
896 if (pds) {
897 VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
898 name, sizeof (uint64_t), 1, &ddobj, tx));
899 } else {
900 /* it's the root dir */
901 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
902 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
904 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
905 dmu_buf_will_dirty(dbuf, tx);
906 ddphys = dbuf->db_data;
908 ddphys->dd_creation_time = gethrestime_sec();
909 if (pds) {
910 ddphys->dd_parent_obj = pds->dd_object;
912 /* update the filesystem counts */
913 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
915 ddphys->dd_props_zapobj = zap_create(mos,
916 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
917 ddphys->dd_child_dir_zapobj = zap_create(mos,
918 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
919 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
920 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
921 dmu_buf_rele(dbuf, FTAG);
923 return (ddobj);
926 boolean_t
927 dsl_dir_is_clone(dsl_dir_t *dd)
929 return (dsl_dir_phys(dd)->dd_origin_obj &&
930 (dd->dd_pool->dp_origin_snap == NULL ||
931 dsl_dir_phys(dd)->dd_origin_obj !=
932 dd->dd_pool->dp_origin_snap->ds_object));
935 void
936 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
938 mutex_enter(&dd->dd_lock);
939 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
940 dsl_dir_phys(dd)->dd_used_bytes);
941 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
942 dsl_dir_phys(dd)->dd_quota);
943 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
944 dsl_dir_phys(dd)->dd_reserved);
945 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
946 dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
947 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
948 dsl_dir_phys(dd)->dd_compressed_bytes));
949 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
950 dsl_dir_phys(dd)->dd_uncompressed_bytes);
951 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
952 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
953 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
954 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
955 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
956 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
957 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
958 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
959 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
960 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
962 mutex_exit(&dd->dd_lock);
964 if (dsl_dir_is_zapified(dd)) {
965 uint64_t count;
966 objset_t *os = dd->dd_pool->dp_meta_objset;
968 if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
969 sizeof (count), 1, &count) == 0) {
970 dsl_prop_nvlist_add_uint64(nv,
971 ZFS_PROP_FILESYSTEM_COUNT, count);
973 if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
974 sizeof (count), 1, &count) == 0) {
975 dsl_prop_nvlist_add_uint64(nv,
976 ZFS_PROP_SNAPSHOT_COUNT, count);
980 if (dsl_dir_is_clone(dd)) {
981 dsl_dataset_t *ds;
982 char buf[ZFS_MAX_DATASET_NAME_LEN];
984 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
985 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
986 dsl_dataset_name(ds, buf);
987 dsl_dataset_rele(ds, FTAG);
988 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
992 void
993 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
995 dsl_pool_t *dp = dd->dd_pool;
997 ASSERT(dsl_dir_phys(dd));
999 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1000 /* up the hold count until we can be written out */
1001 dmu_buf_add_ref(dd->dd_dbuf, dd);
1005 static int64_t
1006 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1008 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1009 uint64_t new_accounted =
1010 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1011 return (new_accounted - old_accounted);
1014 void
1015 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1017 ASSERT(dmu_tx_is_syncing(tx));
1019 mutex_enter(&dd->dd_lock);
1020 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1021 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1022 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1023 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1024 mutex_exit(&dd->dd_lock);
1026 /* release the hold from dsl_dir_dirty */
1027 dmu_buf_rele(dd->dd_dbuf, dd);
1030 static uint64_t
1031 dsl_dir_space_towrite(dsl_dir_t *dd)
1033 uint64_t space = 0;
1034 int i;
1036 ASSERT(MUTEX_HELD(&dd->dd_lock));
1038 for (i = 0; i < TXG_SIZE; i++) {
1039 space += dd->dd_space_towrite[i&TXG_MASK];
1040 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1042 return (space);
1046 * How much space would dd have available if ancestor had delta applied
1047 * to it? If ondiskonly is set, we're only interested in what's
1048 * on-disk, not estimated pending changes.
1050 uint64_t
1051 dsl_dir_space_available(dsl_dir_t *dd,
1052 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1054 uint64_t parentspace, myspace, quota, used;
1057 * If there are no restrictions otherwise, assume we have
1058 * unlimited space available.
1060 quota = UINT64_MAX;
1061 parentspace = UINT64_MAX;
1063 if (dd->dd_parent != NULL) {
1064 parentspace = dsl_dir_space_available(dd->dd_parent,
1065 ancestor, delta, ondiskonly);
1068 mutex_enter(&dd->dd_lock);
1069 if (dsl_dir_phys(dd)->dd_quota != 0)
1070 quota = dsl_dir_phys(dd)->dd_quota;
1071 used = dsl_dir_phys(dd)->dd_used_bytes;
1072 if (!ondiskonly)
1073 used += dsl_dir_space_towrite(dd);
1075 if (dd->dd_parent == NULL) {
1076 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1077 quota = MIN(quota, poolsize);
1080 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1082 * We have some space reserved, in addition to what our
1083 * parent gave us.
1085 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1088 if (dd == ancestor) {
1089 ASSERT(delta <= 0);
1090 ASSERT(used >= -delta);
1091 used += delta;
1092 if (parentspace != UINT64_MAX)
1093 parentspace -= delta;
1096 if (used > quota) {
1097 /* over quota */
1098 myspace = 0;
1099 } else {
1101 * the lesser of the space provided by our parent and
1102 * the space left in our quota
1104 myspace = MIN(parentspace, quota - used);
1107 mutex_exit(&dd->dd_lock);
1109 return (myspace);
1112 struct tempreserve {
1113 list_node_t tr_node;
1114 dsl_dir_t *tr_ds;
1115 uint64_t tr_size;
1118 static int
1119 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1120 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
1121 dmu_tx_t *tx, boolean_t first)
1123 uint64_t txg = tx->tx_txg;
1124 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1125 uint64_t deferred = 0;
1126 struct tempreserve *tr;
1127 int retval = EDQUOT;
1128 int txgidx = txg & TXG_MASK;
1129 int i;
1130 uint64_t ref_rsrv = 0;
1132 ASSERT3U(txg, !=, 0);
1133 ASSERT3S(asize, >, 0);
1135 mutex_enter(&dd->dd_lock);
1138 * Check against the dsl_dir's quota. We don't add in the delta
1139 * when checking for over-quota because they get one free hit.
1141 est_inflight = dsl_dir_space_towrite(dd);
1142 for (i = 0; i < TXG_SIZE; i++)
1143 est_inflight += dd->dd_tempreserved[i];
1144 used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1147 * On the first iteration, fetch the dataset's used-on-disk and
1148 * refreservation values. Also, if checkrefquota is set, test if
1149 * allocating this space would exceed the dataset's refquota.
1151 if (first && tx->tx_objset) {
1152 int error;
1153 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1155 error = dsl_dataset_check_quota(ds, checkrefquota,
1156 asize, est_inflight, &used_on_disk, &ref_rsrv);
1157 if (error) {
1158 mutex_exit(&dd->dd_lock);
1159 DMU_TX_STAT_BUMP(dmu_tx_quota);
1160 return (error);
1165 * If this transaction will result in a net free of space,
1166 * we want to let it through.
1168 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1169 quota = UINT64_MAX;
1170 else
1171 quota = dsl_dir_phys(dd)->dd_quota;
1174 * Adjust the quota against the actual pool size at the root
1175 * minus any outstanding deferred frees.
1176 * To ensure that it's possible to remove files from a full
1177 * pool without inducing transient overcommits, we throttle
1178 * netfree transactions against a quota that is slightly larger,
1179 * but still within the pool's allocation slop. In cases where
1180 * we're very close to full, this will allow a steady trickle of
1181 * removes to get through.
1183 if (dd->dd_parent == NULL) {
1184 spa_t *spa = dd->dd_pool->dp_spa;
1185 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1186 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1187 if (poolsize - deferred < quota) {
1188 quota = poolsize - deferred;
1189 retval = ENOSPC;
1194 * If they are requesting more space, and our current estimate
1195 * is over quota, they get to try again unless the actual
1196 * on-disk is over quota and there are no pending changes (which
1197 * may free up space for us).
1199 if (used_on_disk + est_inflight >= quota) {
1200 if (est_inflight > 0 || used_on_disk < quota ||
1201 (retval == ENOSPC && used_on_disk < quota + deferred))
1202 retval = ERESTART;
1203 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1204 "quota=%lluK tr=%lluK err=%d\n",
1205 used_on_disk>>10, est_inflight>>10,
1206 quota>>10, asize>>10, retval);
1207 mutex_exit(&dd->dd_lock);
1208 DMU_TX_STAT_BUMP(dmu_tx_quota);
1209 return (SET_ERROR(retval));
1212 /* We need to up our estimated delta before dropping dd_lock */
1213 dd->dd_tempreserved[txgidx] += asize;
1215 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1216 asize - ref_rsrv);
1217 mutex_exit(&dd->dd_lock);
1219 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1220 tr->tr_ds = dd;
1221 tr->tr_size = asize;
1222 list_insert_tail(tr_list, tr);
1224 /* see if it's OK with our parent */
1225 if (dd->dd_parent && parent_rsrv) {
1226 boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1228 return (dsl_dir_tempreserve_impl(dd->dd_parent,
1229 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1230 } else {
1231 return (0);
1236 * Reserve space in this dsl_dir, to be used in this tx's txg.
1237 * After the space has been dirtied (and dsl_dir_willuse_space()
1238 * has been called), the reservation should be canceled, using
1239 * dsl_dir_tempreserve_clear().
1242 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1243 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1245 int err;
1246 list_t *tr_list;
1248 if (asize == 0) {
1249 *tr_cookiep = NULL;
1250 return (0);
1253 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1254 list_create(tr_list, sizeof (struct tempreserve),
1255 offsetof(struct tempreserve, tr_node));
1256 ASSERT3S(asize, >, 0);
1257 ASSERT3S(fsize, >=, 0);
1259 err = arc_tempreserve_space(lsize, tx->tx_txg);
1260 if (err == 0) {
1261 struct tempreserve *tr;
1263 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1264 tr->tr_size = lsize;
1265 list_insert_tail(tr_list, tr);
1266 } else {
1267 if (err == EAGAIN) {
1269 * If arc_memory_throttle() detected that pageout
1270 * is running and we are low on memory, we delay new
1271 * non-pageout transactions to give pageout an
1272 * advantage.
1274 * It is unfortunate to be delaying while the caller's
1275 * locks are held.
1277 txg_delay(dd->dd_pool, tx->tx_txg,
1278 MSEC2NSEC(10), MSEC2NSEC(10));
1279 err = SET_ERROR(ERESTART);
1283 if (err == 0) {
1284 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
1285 FALSE, asize > usize, tr_list, tx, TRUE);
1288 if (err != 0)
1289 dsl_dir_tempreserve_clear(tr_list, tx);
1290 else
1291 *tr_cookiep = tr_list;
1293 return (err);
1297 * Clear a temporary reservation that we previously made with
1298 * dsl_dir_tempreserve_space().
1300 void
1301 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1303 int txgidx = tx->tx_txg & TXG_MASK;
1304 list_t *tr_list = tr_cookie;
1305 struct tempreserve *tr;
1307 ASSERT3U(tx->tx_txg, !=, 0);
1309 if (tr_cookie == NULL)
1310 return;
1312 while ((tr = list_head(tr_list)) != NULL) {
1313 if (tr->tr_ds) {
1314 mutex_enter(&tr->tr_ds->dd_lock);
1315 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1316 tr->tr_size);
1317 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1318 mutex_exit(&tr->tr_ds->dd_lock);
1319 } else {
1320 arc_tempreserve_clear(tr->tr_size);
1322 list_remove(tr_list, tr);
1323 kmem_free(tr, sizeof (struct tempreserve));
1326 kmem_free(tr_list, sizeof (list_t));
1330 * This should be called from open context when we think we're going to write
1331 * or free space, for example when dirtying data. Be conservative; it's okay
1332 * to write less space or free more, but we don't want to write more or free
1333 * less than the amount specified.
1335 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1336 * version however it has been adjusted to use an iterative rather then
1337 * recursive algorithm to minimize stack usage.
1339 void
1340 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1342 int64_t parent_space;
1343 uint64_t est_used;
1345 do {
1346 mutex_enter(&dd->dd_lock);
1347 if (space > 0)
1348 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1350 est_used = dsl_dir_space_towrite(dd) +
1351 dsl_dir_phys(dd)->dd_used_bytes;
1352 parent_space = parent_delta(dd, est_used, space);
1353 mutex_exit(&dd->dd_lock);
1355 /* Make sure that we clean up dd_space_to* */
1356 dsl_dir_dirty(dd, tx);
1358 dd = dd->dd_parent;
1359 space = parent_space;
1360 } while (space && dd);
1363 /* call from syncing context when we actually write/free space for this dd */
1364 void
1365 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1366 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1368 int64_t accounted_delta;
1371 * dsl_dataset_set_refreservation_sync_impl() calls this with
1372 * dd_lock held, so that it can atomically update
1373 * ds->ds_reserved and the dsl_dir accounting, so that
1374 * dsl_dataset_check_quota() can see dataset and dir accounting
1375 * consistently.
1377 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1379 ASSERT(dmu_tx_is_syncing(tx));
1380 ASSERT(type < DD_USED_NUM);
1382 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1384 if (needlock)
1385 mutex_enter(&dd->dd_lock);
1386 accounted_delta =
1387 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1388 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1389 ASSERT(compressed >= 0 ||
1390 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1391 ASSERT(uncompressed >= 0 ||
1392 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1393 dsl_dir_phys(dd)->dd_used_bytes += used;
1394 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1395 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1397 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1398 ASSERT(used > 0 ||
1399 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1400 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1401 #ifdef DEBUG
1403 dd_used_t t;
1404 uint64_t u = 0;
1405 for (t = 0; t < DD_USED_NUM; t++)
1406 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1407 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1409 #endif
1411 if (needlock)
1412 mutex_exit(&dd->dd_lock);
1414 if (dd->dd_parent != NULL) {
1415 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1416 accounted_delta, compressed, uncompressed, tx);
1417 dsl_dir_transfer_space(dd->dd_parent,
1418 used - accounted_delta,
1419 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1423 void
1424 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1425 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1427 ASSERT(dmu_tx_is_syncing(tx));
1428 ASSERT(oldtype < DD_USED_NUM);
1429 ASSERT(newtype < DD_USED_NUM);
1431 if (delta == 0 ||
1432 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1433 return;
1435 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1436 mutex_enter(&dd->dd_lock);
1437 ASSERT(delta > 0 ?
1438 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1439 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1440 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1441 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1442 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1443 mutex_exit(&dd->dd_lock);
1446 typedef struct dsl_dir_set_qr_arg {
1447 const char *ddsqra_name;
1448 zprop_source_t ddsqra_source;
1449 uint64_t ddsqra_value;
1450 } dsl_dir_set_qr_arg_t;
1452 static int
1453 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1455 dsl_dir_set_qr_arg_t *ddsqra = arg;
1456 dsl_pool_t *dp = dmu_tx_pool(tx);
1457 dsl_dataset_t *ds;
1458 int error;
1459 uint64_t towrite, newval;
1461 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1462 if (error != 0)
1463 return (error);
1465 error = dsl_prop_predict(ds->ds_dir, "quota",
1466 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1467 if (error != 0) {
1468 dsl_dataset_rele(ds, FTAG);
1469 return (error);
1472 if (newval == 0) {
1473 dsl_dataset_rele(ds, FTAG);
1474 return (0);
1477 mutex_enter(&ds->ds_dir->dd_lock);
1479 * If we are doing the preliminary check in open context, and
1480 * there are pending changes, then don't fail it, since the
1481 * pending changes could under-estimate the amount of space to be
1482 * freed up.
1484 towrite = dsl_dir_space_towrite(ds->ds_dir);
1485 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1486 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1487 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1488 error = SET_ERROR(ENOSPC);
1490 mutex_exit(&ds->ds_dir->dd_lock);
1491 dsl_dataset_rele(ds, FTAG);
1492 return (error);
1495 static void
1496 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1498 dsl_dir_set_qr_arg_t *ddsqra = arg;
1499 dsl_pool_t *dp = dmu_tx_pool(tx);
1500 dsl_dataset_t *ds;
1501 uint64_t newval;
1503 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1505 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1506 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1507 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1508 &ddsqra->ddsqra_value, tx);
1510 VERIFY0(dsl_prop_get_int_ds(ds,
1511 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1512 } else {
1513 newval = ddsqra->ddsqra_value;
1514 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1515 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1518 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1519 mutex_enter(&ds->ds_dir->dd_lock);
1520 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1521 mutex_exit(&ds->ds_dir->dd_lock);
1522 dsl_dataset_rele(ds, FTAG);
1526 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1528 dsl_dir_set_qr_arg_t ddsqra;
1530 ddsqra.ddsqra_name = ddname;
1531 ddsqra.ddsqra_source = source;
1532 ddsqra.ddsqra_value = quota;
1534 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1535 dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1539 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1541 dsl_dir_set_qr_arg_t *ddsqra = arg;
1542 dsl_pool_t *dp = dmu_tx_pool(tx);
1543 dsl_dataset_t *ds;
1544 dsl_dir_t *dd;
1545 uint64_t newval, used, avail;
1546 int error;
1548 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1549 if (error != 0)
1550 return (error);
1551 dd = ds->ds_dir;
1554 * If we are doing the preliminary check in open context, the
1555 * space estimates may be inaccurate.
1557 if (!dmu_tx_is_syncing(tx)) {
1558 dsl_dataset_rele(ds, FTAG);
1559 return (0);
1562 error = dsl_prop_predict(ds->ds_dir,
1563 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1564 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1565 if (error != 0) {
1566 dsl_dataset_rele(ds, FTAG);
1567 return (error);
1570 mutex_enter(&dd->dd_lock);
1571 used = dsl_dir_phys(dd)->dd_used_bytes;
1572 mutex_exit(&dd->dd_lock);
1574 if (dd->dd_parent) {
1575 avail = dsl_dir_space_available(dd->dd_parent,
1576 NULL, 0, FALSE);
1577 } else {
1578 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1581 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1582 uint64_t delta = MAX(used, newval) -
1583 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1585 if (delta > avail ||
1586 (dsl_dir_phys(dd)->dd_quota > 0 &&
1587 newval > dsl_dir_phys(dd)->dd_quota))
1588 error = SET_ERROR(ENOSPC);
1591 dsl_dataset_rele(ds, FTAG);
1592 return (error);
1595 void
1596 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1598 uint64_t used;
1599 int64_t delta;
1601 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1603 mutex_enter(&dd->dd_lock);
1604 used = dsl_dir_phys(dd)->dd_used_bytes;
1605 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1606 dsl_dir_phys(dd)->dd_reserved = value;
1608 if (dd->dd_parent != NULL) {
1609 /* Roll up this additional usage into our ancestors */
1610 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1611 delta, 0, 0, tx);
1613 mutex_exit(&dd->dd_lock);
1616 static void
1617 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1619 dsl_dir_set_qr_arg_t *ddsqra = arg;
1620 dsl_pool_t *dp = dmu_tx_pool(tx);
1621 dsl_dataset_t *ds;
1622 uint64_t newval;
1624 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1626 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1627 dsl_prop_set_sync_impl(ds,
1628 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1629 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1630 &ddsqra->ddsqra_value, tx);
1632 VERIFY0(dsl_prop_get_int_ds(ds,
1633 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1634 } else {
1635 newval = ddsqra->ddsqra_value;
1636 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1637 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1638 (longlong_t)newval);
1641 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1642 dsl_dataset_rele(ds, FTAG);
1646 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1647 uint64_t reservation)
1649 dsl_dir_set_qr_arg_t ddsqra;
1651 ddsqra.ddsqra_name = ddname;
1652 ddsqra.ddsqra_source = source;
1653 ddsqra.ddsqra_value = reservation;
1655 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1656 dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1659 static dsl_dir_t *
1660 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1662 for (; ds1; ds1 = ds1->dd_parent) {
1663 dsl_dir_t *dd;
1664 for (dd = ds2; dd; dd = dd->dd_parent) {
1665 if (ds1 == dd)
1666 return (dd);
1669 return (NULL);
1673 * If delta is applied to dd, how much of that delta would be applied to
1674 * ancestor? Syncing context only.
1676 static int64_t
1677 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1679 if (dd == ancestor)
1680 return (delta);
1682 mutex_enter(&dd->dd_lock);
1683 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1684 mutex_exit(&dd->dd_lock);
1685 return (would_change(dd->dd_parent, delta, ancestor));
1688 typedef struct dsl_dir_rename_arg {
1689 const char *ddra_oldname;
1690 const char *ddra_newname;
1691 cred_t *ddra_cred;
1692 } dsl_dir_rename_arg_t;
1694 /* ARGSUSED */
1695 static int
1696 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1698 int *deltap = arg;
1699 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1701 dsl_dataset_name(ds, namebuf);
1703 if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1704 return (SET_ERROR(ENAMETOOLONG));
1705 return (0);
1708 static int
1709 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1711 dsl_dir_rename_arg_t *ddra = arg;
1712 dsl_pool_t *dp = dmu_tx_pool(tx);
1713 dsl_dir_t *dd, *newparent;
1714 const char *mynewname;
1715 int error;
1716 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1718 /* target dir should exist */
1719 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1720 if (error != 0)
1721 return (error);
1723 /* new parent should exist */
1724 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1725 &newparent, &mynewname);
1726 if (error != 0) {
1727 dsl_dir_rele(dd, FTAG);
1728 return (error);
1731 /* can't rename to different pool */
1732 if (dd->dd_pool != newparent->dd_pool) {
1733 dsl_dir_rele(newparent, FTAG);
1734 dsl_dir_rele(dd, FTAG);
1735 return (SET_ERROR(EXDEV));
1738 /* new name should not already exist */
1739 if (mynewname == NULL) {
1740 dsl_dir_rele(newparent, FTAG);
1741 dsl_dir_rele(dd, FTAG);
1742 return (SET_ERROR(EEXIST));
1745 /* if the name length is growing, validate child name lengths */
1746 if (delta > 0) {
1747 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1748 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1749 if (error != 0) {
1750 dsl_dir_rele(newparent, FTAG);
1751 dsl_dir_rele(dd, FTAG);
1752 return (error);
1756 if (dmu_tx_is_syncing(tx)) {
1757 if (spa_feature_is_active(dp->dp_spa,
1758 SPA_FEATURE_FS_SS_LIMIT)) {
1760 * Although this is the check function and we don't
1761 * normally make on-disk changes in check functions,
1762 * we need to do that here.
1764 * Ensure this portion of the tree's counts have been
1765 * initialized in case the new parent has limits set.
1767 dsl_dir_init_fs_ss_count(dd, tx);
1771 if (newparent != dd->dd_parent) {
1772 /* is there enough space? */
1773 uint64_t myspace =
1774 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1775 dsl_dir_phys(dd)->dd_reserved);
1776 objset_t *os = dd->dd_pool->dp_meta_objset;
1777 uint64_t fs_cnt = 0;
1778 uint64_t ss_cnt = 0;
1780 if (dsl_dir_is_zapified(dd)) {
1781 int err;
1783 err = zap_lookup(os, dd->dd_object,
1784 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1785 &fs_cnt);
1786 if (err != ENOENT && err != 0) {
1787 dsl_dir_rele(newparent, FTAG);
1788 dsl_dir_rele(dd, FTAG);
1789 return (err);
1793 * have to add 1 for the filesystem itself that we're
1794 * moving
1796 fs_cnt++;
1798 err = zap_lookup(os, dd->dd_object,
1799 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1800 &ss_cnt);
1801 if (err != ENOENT && err != 0) {
1802 dsl_dir_rele(newparent, FTAG);
1803 dsl_dir_rele(dd, FTAG);
1804 return (err);
1808 /* no rename into our descendant */
1809 if (closest_common_ancestor(dd, newparent) == dd) {
1810 dsl_dir_rele(newparent, FTAG);
1811 dsl_dir_rele(dd, FTAG);
1812 return (SET_ERROR(EINVAL));
1815 error = dsl_dir_transfer_possible(dd->dd_parent,
1816 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1817 if (error != 0) {
1818 dsl_dir_rele(newparent, FTAG);
1819 dsl_dir_rele(dd, FTAG);
1820 return (error);
1824 dsl_dir_rele(newparent, FTAG);
1825 dsl_dir_rele(dd, FTAG);
1826 return (0);
1829 static void
1830 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1832 dsl_dir_rename_arg_t *ddra = arg;
1833 dsl_pool_t *dp = dmu_tx_pool(tx);
1834 dsl_dir_t *dd, *newparent;
1835 const char *mynewname;
1836 int error;
1837 objset_t *mos = dp->dp_meta_objset;
1839 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
1840 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
1841 &mynewname));
1843 /* Log this before we change the name. */
1844 spa_history_log_internal_dd(dd, "rename", tx,
1845 "-> %s", ddra->ddra_newname);
1847 if (newparent != dd->dd_parent) {
1848 objset_t *os = dd->dd_pool->dp_meta_objset;
1849 uint64_t fs_cnt = 0;
1850 uint64_t ss_cnt = 0;
1853 * We already made sure the dd counts were initialized in the
1854 * check function.
1856 if (spa_feature_is_active(dp->dp_spa,
1857 SPA_FEATURE_FS_SS_LIMIT)) {
1858 VERIFY0(zap_lookup(os, dd->dd_object,
1859 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1860 &fs_cnt));
1861 /* add 1 for the filesystem itself that we're moving */
1862 fs_cnt++;
1864 VERIFY0(zap_lookup(os, dd->dd_object,
1865 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1866 &ss_cnt));
1869 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1870 DD_FIELD_FILESYSTEM_COUNT, tx);
1871 dsl_fs_ss_count_adjust(newparent, fs_cnt,
1872 DD_FIELD_FILESYSTEM_COUNT, tx);
1874 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1875 DD_FIELD_SNAPSHOT_COUNT, tx);
1876 dsl_fs_ss_count_adjust(newparent, ss_cnt,
1877 DD_FIELD_SNAPSHOT_COUNT, tx);
1879 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1880 -dsl_dir_phys(dd)->dd_used_bytes,
1881 -dsl_dir_phys(dd)->dd_compressed_bytes,
1882 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1883 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1884 dsl_dir_phys(dd)->dd_used_bytes,
1885 dsl_dir_phys(dd)->dd_compressed_bytes,
1886 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1888 if (dsl_dir_phys(dd)->dd_reserved >
1889 dsl_dir_phys(dd)->dd_used_bytes) {
1890 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1891 dsl_dir_phys(dd)->dd_used_bytes;
1893 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1894 -unused_rsrv, 0, 0, tx);
1895 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
1896 unused_rsrv, 0, 0, tx);
1900 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1902 /* remove from old parent zapobj */
1903 error = zap_remove(mos,
1904 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1905 dd->dd_myname, tx);
1906 ASSERT0(error);
1908 (void) strlcpy(dd->dd_myname, mynewname,
1909 sizeof (dd->dd_myname));
1910 dsl_dir_rele(dd->dd_parent, dd);
1911 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
1912 VERIFY0(dsl_dir_hold_obj(dp,
1913 newparent->dd_object, NULL, dd, &dd->dd_parent));
1915 /* add to new parent zapobj */
1916 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
1917 dd->dd_myname, 8, 1, &dd->dd_object, tx));
1919 zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,
1920 ddra->ddra_newname, B_TRUE);
1922 dsl_prop_notify_all(dd);
1924 dsl_dir_rele(newparent, FTAG);
1925 dsl_dir_rele(dd, FTAG);
1929 dsl_dir_rename(const char *oldname, const char *newname)
1931 dsl_dir_rename_arg_t ddra;
1933 ddra.ddra_oldname = oldname;
1934 ddra.ddra_newname = newname;
1935 ddra.ddra_cred = CRED();
1937 return (dsl_sync_task(oldname,
1938 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
1939 3, ZFS_SPACE_CHECK_RESERVED));
1943 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1944 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
1946 dsl_dir_t *ancestor;
1947 int64_t adelta;
1948 uint64_t avail;
1949 int err;
1951 ancestor = closest_common_ancestor(sdd, tdd);
1952 adelta = would_change(sdd, -space, ancestor);
1953 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1954 if (avail < space)
1955 return (SET_ERROR(ENOSPC));
1957 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1958 ancestor, cr);
1959 if (err != 0)
1960 return (err);
1961 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1962 ancestor, cr);
1963 if (err != 0)
1964 return (err);
1966 return (0);
1969 timestruc_t
1970 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1972 timestruc_t t;
1974 mutex_enter(&dd->dd_lock);
1975 t = dd->dd_snap_cmtime;
1976 mutex_exit(&dd->dd_lock);
1978 return (t);
1981 void
1982 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1984 timestruc_t t;
1986 gethrestime(&t);
1987 mutex_enter(&dd->dd_lock);
1988 dd->dd_snap_cmtime = t;
1989 mutex_exit(&dd->dd_lock);
1992 void
1993 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
1995 objset_t *mos = dd->dd_pool->dp_meta_objset;
1996 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
1999 boolean_t
2000 dsl_dir_is_zapified(dsl_dir_t *dd)
2002 dmu_object_info_t doi;
2004 dmu_object_info_from_db(dd->dd_dbuf, &doi);
2005 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2008 #if defined(_KERNEL) && defined(HAVE_SPL)
2009 EXPORT_SYMBOL(dsl_dir_set_quota);
2010 EXPORT_SYMBOL(dsl_dir_set_reservation);
2011 #endif