Debug auto_replace_001_pos failures
[zfs.git] / module / zfs / dsl_dir.c
blobeac9828a204aca50a54fe756bdc7a7d265436ab4
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 https://opensource.org/licenses/CDDL-1.0.
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, 2018 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.
28 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
31 #include <sys/dmu.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_prop.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_deleg.h>
39 #include <sys/dmu_impl.h>
40 #include <sys/spa.h>
41 #include <sys/spa_impl.h>
42 #include <sys/metaslab.h>
43 #include <sys/zap.h>
44 #include <sys/zio.h>
45 #include <sys/arc.h>
46 #include <sys/sunddi.h>
47 #include <sys/zfeature.h>
48 #include <sys/policy.h>
49 #include <sys/zfs_vfsops.h>
50 #include <sys/zfs_znode.h>
51 #include <sys/zvol.h>
52 #include <sys/zthr.h>
53 #include "zfs_namecheck.h"
54 #include "zfs_prop.h"
57 * This controls if we verify the ZVOL quota or not.
58 * Currently, quotas are not implemented for ZVOLs.
59 * The quota size is the size of the ZVOL.
60 * The size of the volume already implies the ZVOL size quota.
61 * The quota mechanism can introduce a significant performance drop.
63 static int zvol_enforce_quotas = B_TRUE;
66 * Filesystem and Snapshot Limits
67 * ------------------------------
69 * These limits are used to restrict the number of filesystems and/or snapshots
70 * that can be created at a given level in the tree or below. A typical
71 * use-case is with a delegated dataset where the administrator wants to ensure
72 * that a user within the zone is not creating too many additional filesystems
73 * or snapshots, even though they're not exceeding their space quota.
75 * The filesystem and snapshot counts are stored as extensible properties. This
76 * capability is controlled by a feature flag and must be enabled to be used.
77 * Once enabled, the feature is not active until the first limit is set. At
78 * that point, future operations to create/destroy filesystems or snapshots
79 * will validate and update the counts.
81 * Because the count properties will not exist before the feature is active,
82 * the counts are updated when a limit is first set on an uninitialized
83 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
84 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
85 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
86 * snapshot count properties on a node indicate uninitialized counts on that
87 * node.) When first setting a limit on an uninitialized node, the code starts
88 * at the filesystem with the new limit and descends into all sub-filesystems
89 * to add the count properties.
91 * In practice this is lightweight since a limit is typically set when the
92 * filesystem is created and thus has no children. Once valid, changing the
93 * limit value won't require a re-traversal since the counts are already valid.
94 * When recursively fixing the counts, if a node with a limit is encountered
95 * during the descent, the counts are known to be valid and there is no need to
96 * descend into that filesystem's children. The counts on filesystems above the
97 * one with the new limit will still be uninitialized, unless a limit is
98 * eventually set on one of those filesystems. The counts are always recursively
99 * updated when a limit is set on a dataset, unless there is already a limit.
100 * When a new limit value is set on a filesystem with an existing limit, it is
101 * possible for the new limit to be less than the current count at that level
102 * since a user who can change the limit is also allowed to exceed the limit.
104 * Once the feature is active, then whenever a filesystem or snapshot is
105 * created, the code recurses up the tree, validating the new count against the
106 * limit at each initialized level. In practice, most levels will not have a
107 * limit set. If there is a limit at any initialized level up the tree, the
108 * check must pass or the creation will fail. Likewise, when a filesystem or
109 * snapshot is destroyed, the counts are recursively adjusted all the way up
110 * the initialized nodes in the tree. Renaming a filesystem into different point
111 * in the tree will first validate, then update the counts on each branch up to
112 * the common ancestor. A receive will also validate the counts and then update
113 * them.
115 * An exception to the above behavior is that the limit is not enforced if the
116 * user has permission to modify the limit. This is primarily so that
117 * recursive snapshots in the global zone always work. We want to prevent a
118 * denial-of-service in which a lower level delegated dataset could max out its
119 * limit and thus block recursive snapshots from being taken in the global zone.
120 * Because of this, it is possible for the snapshot count to be over the limit
121 * and snapshots taken in the global zone could cause a lower level dataset to
122 * hit or exceed its limit. The administrator taking the global zone recursive
123 * snapshot should be aware of this side-effect and behave accordingly.
124 * For consistency, the filesystem limit is also not enforced if the user can
125 * modify the limit.
127 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
128 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
129 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
130 * dsl_dir_init_fs_ss_count().
133 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
135 typedef struct ddulrt_arg {
136 dsl_dir_t *ddulrta_dd;
137 uint64_t ddlrta_txg;
138 } ddulrt_arg_t;
140 static void
141 dsl_dir_evict_async(void *dbu)
143 dsl_dir_t *dd = dbu;
144 int t;
145 dsl_pool_t *dp __maybe_unused = dd->dd_pool;
147 dd->dd_dbuf = NULL;
149 for (t = 0; t < TXG_SIZE; t++) {
150 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
151 ASSERT(dd->dd_tempreserved[t] == 0);
152 ASSERT(dd->dd_space_towrite[t] == 0);
155 if (dd->dd_parent)
156 dsl_dir_async_rele(dd->dd_parent, dd);
158 spa_async_close(dd->dd_pool->dp_spa, dd);
160 if (dsl_deadlist_is_open(&dd->dd_livelist))
161 dsl_dir_livelist_close(dd);
163 dsl_prop_fini(dd);
164 cv_destroy(&dd->dd_activity_cv);
165 mutex_destroy(&dd->dd_activity_lock);
166 mutex_destroy(&dd->dd_lock);
167 kmem_free(dd, sizeof (dsl_dir_t));
171 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
172 const char *tail, const void *tag, dsl_dir_t **ddp)
174 dmu_buf_t *dbuf;
175 dsl_dir_t *dd;
176 dmu_object_info_t doi;
177 int err;
179 ASSERT(dsl_pool_config_held(dp));
181 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
182 if (err != 0)
183 return (err);
184 dd = dmu_buf_get_user(dbuf);
186 dmu_object_info_from_db(dbuf, &doi);
187 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
188 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
190 if (dd == NULL) {
191 dsl_dir_t *winner;
193 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
194 dd->dd_object = ddobj;
195 dd->dd_dbuf = dbuf;
196 dd->dd_pool = dp;
198 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
199 mutex_init(&dd->dd_activity_lock, NULL, MUTEX_DEFAULT, NULL);
200 cv_init(&dd->dd_activity_cv, NULL, CV_DEFAULT, NULL);
201 dsl_prop_init(dd);
203 if (dsl_dir_is_zapified(dd)) {
204 err = zap_lookup(dp->dp_meta_objset,
205 ddobj, DD_FIELD_CRYPTO_KEY_OBJ,
206 sizeof (uint64_t), 1, &dd->dd_crypto_obj);
207 if (err == 0) {
208 /* check for on-disk format errata */
209 if (dsl_dir_incompatible_encryption_version(
210 dd)) {
211 dp->dp_spa->spa_errata =
212 ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
214 } else if (err != ENOENT) {
215 goto errout;
219 if (dsl_dir_phys(dd)->dd_parent_obj) {
220 err = dsl_dir_hold_obj(dp,
221 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
222 &dd->dd_parent);
223 if (err != 0)
224 goto errout;
225 if (tail) {
226 #ifdef ZFS_DEBUG
227 uint64_t foundobj;
229 err = zap_lookup(dp->dp_meta_objset,
230 dsl_dir_phys(dd->dd_parent)->
231 dd_child_dir_zapobj, tail,
232 sizeof (foundobj), 1, &foundobj);
233 ASSERT(err || foundobj == ddobj);
234 #endif
235 (void) strlcpy(dd->dd_myname, tail,
236 sizeof (dd->dd_myname));
237 } else {
238 err = zap_value_search(dp->dp_meta_objset,
239 dsl_dir_phys(dd->dd_parent)->
240 dd_child_dir_zapobj,
241 ddobj, 0, dd->dd_myname);
243 if (err != 0)
244 goto errout;
245 } else {
246 (void) strlcpy(dd->dd_myname, spa_name(dp->dp_spa),
247 sizeof (dd->dd_myname));
250 if (dsl_dir_is_clone(dd)) {
251 dmu_buf_t *origin_bonus;
252 dsl_dataset_phys_t *origin_phys;
255 * We can't open the origin dataset, because
256 * that would require opening this dsl_dir.
257 * Just look at its phys directly instead.
259 err = dmu_bonus_hold(dp->dp_meta_objset,
260 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
261 &origin_bonus);
262 if (err != 0)
263 goto errout;
264 origin_phys = origin_bonus->db_data;
265 dd->dd_origin_txg =
266 origin_phys->ds_creation_txg;
267 dmu_buf_rele(origin_bonus, FTAG);
268 if (dsl_dir_is_zapified(dd)) {
269 uint64_t obj;
270 err = zap_lookup(dp->dp_meta_objset,
271 dd->dd_object, DD_FIELD_LIVELIST,
272 sizeof (uint64_t), 1, &obj);
273 if (err == 0)
274 dsl_dir_livelist_open(dd, obj);
275 else if (err != ENOENT)
276 goto errout;
280 if (dsl_dir_is_zapified(dd)) {
281 inode_timespec_t t = {0};
282 (void) zap_lookup(dp->dp_meta_objset, ddobj,
283 DD_FIELD_SNAPSHOTS_CHANGED,
284 sizeof (uint64_t),
285 sizeof (inode_timespec_t) / sizeof (uint64_t),
286 &t);
287 dd->dd_snap_cmtime = t;
290 dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
291 &dd->dd_dbuf);
292 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
293 if (winner != NULL) {
294 if (dd->dd_parent)
295 dsl_dir_rele(dd->dd_parent, dd);
296 if (dsl_deadlist_is_open(&dd->dd_livelist))
297 dsl_dir_livelist_close(dd);
298 dsl_prop_fini(dd);
299 cv_destroy(&dd->dd_activity_cv);
300 mutex_destroy(&dd->dd_activity_lock);
301 mutex_destroy(&dd->dd_lock);
302 kmem_free(dd, sizeof (dsl_dir_t));
303 dd = winner;
304 } else {
305 spa_open_ref(dp->dp_spa, dd);
310 * The dsl_dir_t has both open-to-close and instantiate-to-evict
311 * holds on the spa. We need the open-to-close holds because
312 * otherwise the spa_refcnt wouldn't change when we open a
313 * dir which the spa also has open, so we could incorrectly
314 * think it was OK to unload/export/destroy the pool. We need
315 * the instantiate-to-evict hold because the dsl_dir_t has a
316 * pointer to the dd_pool, which has a pointer to the spa_t.
318 spa_open_ref(dp->dp_spa, tag);
319 ASSERT3P(dd->dd_pool, ==, dp);
320 ASSERT3U(dd->dd_object, ==, ddobj);
321 ASSERT3P(dd->dd_dbuf, ==, dbuf);
322 *ddp = dd;
323 return (0);
325 errout:
326 if (dd->dd_parent)
327 dsl_dir_rele(dd->dd_parent, dd);
328 if (dsl_deadlist_is_open(&dd->dd_livelist))
329 dsl_dir_livelist_close(dd);
330 dsl_prop_fini(dd);
331 cv_destroy(&dd->dd_activity_cv);
332 mutex_destroy(&dd->dd_activity_lock);
333 mutex_destroy(&dd->dd_lock);
334 kmem_free(dd, sizeof (dsl_dir_t));
335 dmu_buf_rele(dbuf, tag);
336 return (err);
339 void
340 dsl_dir_rele(dsl_dir_t *dd, const void *tag)
342 dprintf_dd(dd, "%s\n", "");
343 spa_close(dd->dd_pool->dp_spa, tag);
344 dmu_buf_rele(dd->dd_dbuf, tag);
348 * Remove a reference to the given dsl dir that is being asynchronously
349 * released. Async releases occur from a taskq performing eviction of
350 * dsl datasets and dirs. This process is identical to a normal release
351 * with the exception of using the async API for releasing the reference on
352 * the spa.
354 void
355 dsl_dir_async_rele(dsl_dir_t *dd, const void *tag)
357 dprintf_dd(dd, "%s\n", "");
358 spa_async_close(dd->dd_pool->dp_spa, tag);
359 dmu_buf_rele(dd->dd_dbuf, tag);
362 /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
363 void
364 dsl_dir_name(dsl_dir_t *dd, char *buf)
366 if (dd->dd_parent) {
367 dsl_dir_name(dd->dd_parent, buf);
368 VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
369 ZFS_MAX_DATASET_NAME_LEN);
370 } else {
371 buf[0] = '\0';
373 if (!MUTEX_HELD(&dd->dd_lock)) {
375 * recursive mutex so that we can use
376 * dprintf_dd() with dd_lock held
378 mutex_enter(&dd->dd_lock);
379 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
380 <, ZFS_MAX_DATASET_NAME_LEN);
381 mutex_exit(&dd->dd_lock);
382 } else {
383 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
384 <, ZFS_MAX_DATASET_NAME_LEN);
388 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
390 dsl_dir_namelen(dsl_dir_t *dd)
392 int result = 0;
394 if (dd->dd_parent) {
395 /* parent's name + 1 for the "/" */
396 result = dsl_dir_namelen(dd->dd_parent) + 1;
399 if (!MUTEX_HELD(&dd->dd_lock)) {
400 /* see dsl_dir_name */
401 mutex_enter(&dd->dd_lock);
402 result += strlen(dd->dd_myname);
403 mutex_exit(&dd->dd_lock);
404 } else {
405 result += strlen(dd->dd_myname);
408 return (result);
411 static int
412 getcomponent(const char *path, char *component, const char **nextp)
414 char *p;
416 if ((path == NULL) || (path[0] == '\0'))
417 return (SET_ERROR(ENOENT));
418 /* This would be a good place to reserve some namespace... */
419 p = strpbrk(path, "/@");
420 if (p && (p[1] == '/' || p[1] == '@')) {
421 /* two separators in a row */
422 return (SET_ERROR(EINVAL));
424 if (p == NULL || p == path) {
426 * if the first thing is an @ or /, it had better be an
427 * @ and it had better not have any more ats or slashes,
428 * and it had better have something after the @.
430 if (p != NULL &&
431 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
432 return (SET_ERROR(EINVAL));
433 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
434 return (SET_ERROR(ENAMETOOLONG));
435 (void) strlcpy(component, path, ZFS_MAX_DATASET_NAME_LEN);
436 p = NULL;
437 } else if (p[0] == '/') {
438 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
439 return (SET_ERROR(ENAMETOOLONG));
440 (void) strlcpy(component, path, p - path + 1);
441 p++;
442 } else if (p[0] == '@') {
444 * if the next separator is an @, there better not be
445 * any more slashes.
447 if (strchr(path, '/'))
448 return (SET_ERROR(EINVAL));
449 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
450 return (SET_ERROR(ENAMETOOLONG));
451 (void) strlcpy(component, path, p - path + 1);
452 } else {
453 panic("invalid p=%p", (void *)p);
455 *nextp = p;
456 return (0);
460 * Return the dsl_dir_t, and possibly the last component which couldn't
461 * be found in *tail. The name must be in the specified dsl_pool_t. This
462 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
463 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
464 * (*tail)[0] == '@' means that the last component is a snapshot.
467 dsl_dir_hold(dsl_pool_t *dp, const char *name, const void *tag,
468 dsl_dir_t **ddp, const char **tailp)
470 char *buf;
471 const char *spaname, *next, *nextnext = NULL;
472 int err;
473 dsl_dir_t *dd;
474 uint64_t ddobj;
476 buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
477 err = getcomponent(name, buf, &next);
478 if (err != 0)
479 goto error;
481 /* Make sure the name is in the specified pool. */
482 spaname = spa_name(dp->dp_spa);
483 if (strcmp(buf, spaname) != 0) {
484 err = SET_ERROR(EXDEV);
485 goto error;
488 ASSERT(dsl_pool_config_held(dp));
490 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
491 if (err != 0) {
492 goto error;
495 while (next != NULL) {
496 dsl_dir_t *child_dd;
497 err = getcomponent(next, buf, &nextnext);
498 if (err != 0)
499 break;
500 ASSERT(next[0] != '\0');
501 if (next[0] == '@')
502 break;
503 dprintf("looking up %s in obj%lld\n",
504 buf, (longlong_t)dsl_dir_phys(dd)->dd_child_dir_zapobj);
506 err = zap_lookup(dp->dp_meta_objset,
507 dsl_dir_phys(dd)->dd_child_dir_zapobj,
508 buf, sizeof (ddobj), 1, &ddobj);
509 if (err != 0) {
510 if (err == ENOENT)
511 err = 0;
512 break;
515 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
516 if (err != 0)
517 break;
518 dsl_dir_rele(dd, tag);
519 dd = child_dd;
520 next = nextnext;
523 if (err != 0) {
524 dsl_dir_rele(dd, tag);
525 goto error;
529 * It's an error if there's more than one component left, or
530 * tailp==NULL and there's any component left.
532 if (next != NULL &&
533 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
534 /* bad path name */
535 dsl_dir_rele(dd, tag);
536 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
537 err = SET_ERROR(ENOENT);
539 if (tailp != NULL)
540 *tailp = next;
541 if (err == 0)
542 *ddp = dd;
543 error:
544 kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);
545 return (err);
549 * If the counts are already initialized for this filesystem and its
550 * descendants then do nothing, otherwise initialize the counts.
552 * The counts on this filesystem, and those below, may be uninitialized due to
553 * either the use of a pre-existing pool which did not support the
554 * filesystem/snapshot limit feature, or one in which the feature had not yet
555 * been enabled.
557 * Recursively descend the filesystem tree and update the filesystem/snapshot
558 * counts on each filesystem below, then update the cumulative count on the
559 * current filesystem. If the filesystem already has a count set on it,
560 * then we know that its counts, and the counts on the filesystems below it,
561 * are already correct, so we don't have to update this filesystem.
563 static void
564 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
566 uint64_t my_fs_cnt = 0;
567 uint64_t my_ss_cnt = 0;
568 dsl_pool_t *dp = dd->dd_pool;
569 objset_t *os = dp->dp_meta_objset;
570 zap_cursor_t *zc;
571 zap_attribute_t *za;
572 dsl_dataset_t *ds;
574 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
575 ASSERT(dsl_pool_config_held(dp));
576 ASSERT(dmu_tx_is_syncing(tx));
578 dsl_dir_zapify(dd, tx);
581 * If the filesystem count has already been initialized then we
582 * don't need to recurse down any further.
584 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
585 return;
587 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
588 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
590 /* Iterate my child dirs */
591 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
592 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
593 dsl_dir_t *chld_dd;
594 uint64_t count;
596 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
597 &chld_dd));
600 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets.
602 if (chld_dd->dd_myname[0] == '$') {
603 dsl_dir_rele(chld_dd, FTAG);
604 continue;
607 my_fs_cnt++; /* count this child */
609 dsl_dir_init_fs_ss_count(chld_dd, tx);
611 VERIFY0(zap_lookup(os, chld_dd->dd_object,
612 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
613 my_fs_cnt += count;
614 VERIFY0(zap_lookup(os, chld_dd->dd_object,
615 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
616 my_ss_cnt += count;
618 dsl_dir_rele(chld_dd, FTAG);
620 zap_cursor_fini(zc);
621 /* Count my snapshots (we counted children's snapshots above) */
622 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
623 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
625 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
626 zap_cursor_retrieve(zc, za) == 0;
627 zap_cursor_advance(zc)) {
628 /* Don't count temporary snapshots */
629 if (za->za_name[0] != '%')
630 my_ss_cnt++;
632 zap_cursor_fini(zc);
634 dsl_dataset_rele(ds, FTAG);
636 kmem_free(zc, sizeof (zap_cursor_t));
637 kmem_free(za, sizeof (zap_attribute_t));
639 /* we're in a sync task, update counts */
640 dmu_buf_will_dirty(dd->dd_dbuf, tx);
641 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
642 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
643 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
644 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
647 static int
648 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
650 char *ddname = (char *)arg;
651 dsl_pool_t *dp = dmu_tx_pool(tx);
652 dsl_dataset_t *ds;
653 dsl_dir_t *dd;
654 int error;
656 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
657 if (error != 0)
658 return (error);
660 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
661 dsl_dataset_rele(ds, FTAG);
662 return (SET_ERROR(ENOTSUP));
665 dd = ds->ds_dir;
666 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
667 dsl_dir_is_zapified(dd) &&
668 zap_contains(dp->dp_meta_objset, dd->dd_object,
669 DD_FIELD_FILESYSTEM_COUNT) == 0) {
670 dsl_dataset_rele(ds, FTAG);
671 return (SET_ERROR(EALREADY));
674 dsl_dataset_rele(ds, FTAG);
675 return (0);
678 static void
679 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
681 char *ddname = (char *)arg;
682 dsl_pool_t *dp = dmu_tx_pool(tx);
683 dsl_dataset_t *ds;
684 spa_t *spa;
686 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
688 spa = dsl_dataset_get_spa(ds);
690 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
692 * Since the feature was not active and we're now setting a
693 * limit, increment the feature-active counter so that the
694 * feature becomes active for the first time.
696 * We are already in a sync task so we can update the MOS.
698 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
702 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
703 * we need to ensure the counts are correct. Descend down the tree from
704 * this point and update all of the counts to be accurate.
706 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
708 dsl_dataset_rele(ds, FTAG);
712 * Make sure the feature is enabled and activate it if necessary.
713 * Since we're setting a limit, ensure the on-disk counts are valid.
714 * This is only called by the ioctl path when setting a limit value.
716 * We do not need to validate the new limit, since users who can change the
717 * limit are also allowed to exceed the limit.
720 dsl_dir_activate_fs_ss_limit(const char *ddname)
722 int error;
724 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
725 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
726 ZFS_SPACE_CHECK_RESERVED);
728 if (error == EALREADY)
729 error = 0;
731 return (error);
735 * Used to determine if the filesystem_limit or snapshot_limit should be
736 * enforced. We allow the limit to be exceeded if the user has permission to
737 * write the property value. We pass in the creds that we got in the open
738 * context since we will always be the GZ root in syncing context. We also have
739 * to handle the case where we are allowed to change the limit on the current
740 * dataset, but there may be another limit in the tree above.
742 * We can never modify these two properties within a non-global zone. In
743 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
744 * can't use that function since we are already holding the dp_config_rwlock.
745 * In addition, we already have the dd and dealing with snapshots is simplified
746 * in this code.
749 typedef enum {
750 ENFORCE_ALWAYS,
751 ENFORCE_NEVER,
752 ENFORCE_ABOVE
753 } enforce_res_t;
755 static enforce_res_t
756 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop,
757 cred_t *cr, proc_t *proc)
759 enforce_res_t enforce = ENFORCE_ALWAYS;
760 uint64_t obj;
761 dsl_dataset_t *ds;
762 uint64_t zoned;
763 const char *zonedstr;
765 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
766 prop == ZFS_PROP_SNAPSHOT_LIMIT);
768 #ifdef _KERNEL
769 if (crgetzoneid(cr) != GLOBAL_ZONEID)
770 return (ENFORCE_ALWAYS);
773 * We are checking the saved credentials of the user process, which is
774 * not the current process. Note that we can't use secpolicy_zfs(),
775 * because it only works if the cred is that of the current process (on
776 * Linux).
778 if (secpolicy_zfs_proc(cr, proc) == 0)
779 return (ENFORCE_NEVER);
780 #else
781 (void) proc;
782 #endif
784 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
785 return (ENFORCE_ALWAYS);
787 ASSERT(dsl_pool_config_held(dd->dd_pool));
789 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
790 return (ENFORCE_ALWAYS);
792 zonedstr = zfs_prop_to_name(ZFS_PROP_ZONED);
793 if (dsl_prop_get_ds(ds, zonedstr, 8, 1, &zoned, NULL) || zoned) {
794 /* Only root can access zoned fs's from the GZ */
795 enforce = ENFORCE_ALWAYS;
796 } else {
797 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
798 enforce = ENFORCE_ABOVE;
801 dsl_dataset_rele(ds, FTAG);
802 return (enforce);
806 * Check if adding additional child filesystem(s) would exceed any filesystem
807 * limits or adding additional snapshot(s) would exceed any snapshot limits.
808 * The prop argument indicates which limit to check.
810 * Note that all filesystem limits up to the root (or the highest
811 * initialized) filesystem or the given ancestor must be satisfied.
814 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
815 dsl_dir_t *ancestor, cred_t *cr, proc_t *proc)
817 objset_t *os = dd->dd_pool->dp_meta_objset;
818 uint64_t limit, count;
819 const char *count_prop;
820 enforce_res_t enforce;
821 int err = 0;
823 ASSERT(dsl_pool_config_held(dd->dd_pool));
824 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
825 prop == ZFS_PROP_SNAPSHOT_LIMIT);
827 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
829 * We don't enforce the limit for temporary snapshots. This is
830 * indicated by a NULL cred_t argument.
832 if (cr == NULL)
833 return (0);
835 count_prop = DD_FIELD_SNAPSHOT_COUNT;
836 } else {
837 count_prop = DD_FIELD_FILESYSTEM_COUNT;
840 * If we're allowed to change the limit, don't enforce the limit
841 * e.g. this can happen if a snapshot is taken by an administrative
842 * user in the global zone (i.e. a recursive snapshot by root).
843 * However, we must handle the case of delegated permissions where we
844 * are allowed to change the limit on the current dataset, but there
845 * is another limit in the tree above.
847 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr, proc);
848 if (enforce == ENFORCE_NEVER)
849 return (0);
852 * e.g. if renaming a dataset with no snapshots, count adjustment
853 * is 0.
855 if (delta == 0)
856 return (0);
859 * If an ancestor has been provided, stop checking the limit once we
860 * hit that dir. We need this during rename so that we don't overcount
861 * the check once we recurse up to the common ancestor.
863 if (ancestor == dd)
864 return (0);
867 * If we hit an uninitialized node while recursing up the tree, we can
868 * stop since we know there is no limit here (or above). The counts are
869 * not valid on this node and we know we won't touch this node's counts.
871 if (!dsl_dir_is_zapified(dd))
872 return (0);
873 err = zap_lookup(os, dd->dd_object,
874 count_prop, sizeof (count), 1, &count);
875 if (err == ENOENT)
876 return (0);
877 if (err != 0)
878 return (err);
880 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
881 B_FALSE);
882 if (err != 0)
883 return (err);
885 /* Is there a limit which we've hit? */
886 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
887 return (SET_ERROR(EDQUOT));
889 if (dd->dd_parent != NULL)
890 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
891 ancestor, cr, proc);
893 return (err);
897 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
898 * parents. When a new filesystem/snapshot is created, increment the count on
899 * all parents, and when a filesystem/snapshot is destroyed, decrement the
900 * count.
902 void
903 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
904 dmu_tx_t *tx)
906 int err;
907 objset_t *os = dd->dd_pool->dp_meta_objset;
908 uint64_t count;
910 ASSERT(dsl_pool_config_held(dd->dd_pool));
911 ASSERT(dmu_tx_is_syncing(tx));
912 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
913 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
916 * We don't do accounting for hidden ($FREE, $MOS & $ORIGIN) objsets.
918 if (dd->dd_myname[0] == '$' && strcmp(prop,
919 DD_FIELD_FILESYSTEM_COUNT) == 0) {
920 return;
924 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
926 if (delta == 0)
927 return;
930 * If we hit an uninitialized node while recursing up the tree, we can
931 * stop since we know the counts are not valid on this node and we
932 * know we shouldn't touch this node's counts. An uninitialized count
933 * on the node indicates that either the feature has not yet been
934 * activated or there are no limits on this part of the tree.
936 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
937 prop, sizeof (count), 1, &count)) == ENOENT)
938 return;
939 VERIFY0(err);
941 count += delta;
942 /* Use a signed verify to make sure we're not neg. */
943 VERIFY3S(count, >=, 0);
945 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
946 tx));
948 /* Roll up this additional count into our ancestors */
949 if (dd->dd_parent != NULL)
950 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
953 uint64_t
954 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
955 dmu_tx_t *tx)
957 objset_t *mos = dp->dp_meta_objset;
958 uint64_t ddobj;
959 dsl_dir_phys_t *ddphys;
960 dmu_buf_t *dbuf;
962 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
963 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
964 if (pds) {
965 VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
966 name, sizeof (uint64_t), 1, &ddobj, tx));
967 } else {
968 /* it's the root dir */
969 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
970 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
972 VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
973 dmu_buf_will_dirty(dbuf, tx);
974 ddphys = dbuf->db_data;
976 ddphys->dd_creation_time = gethrestime_sec();
977 if (pds) {
978 ddphys->dd_parent_obj = pds->dd_object;
980 /* update the filesystem counts */
981 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
983 ddphys->dd_props_zapobj = zap_create(mos,
984 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
985 ddphys->dd_child_dir_zapobj = zap_create(mos,
986 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
987 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
988 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
990 dmu_buf_rele(dbuf, FTAG);
992 return (ddobj);
995 boolean_t
996 dsl_dir_is_clone(dsl_dir_t *dd)
998 return (dsl_dir_phys(dd)->dd_origin_obj &&
999 (dd->dd_pool->dp_origin_snap == NULL ||
1000 dsl_dir_phys(dd)->dd_origin_obj !=
1001 dd->dd_pool->dp_origin_snap->ds_object));
1004 uint64_t
1005 dsl_dir_get_used(dsl_dir_t *dd)
1007 return (dsl_dir_phys(dd)->dd_used_bytes);
1010 uint64_t
1011 dsl_dir_get_compressed(dsl_dir_t *dd)
1013 return (dsl_dir_phys(dd)->dd_compressed_bytes);
1016 uint64_t
1017 dsl_dir_get_quota(dsl_dir_t *dd)
1019 return (dsl_dir_phys(dd)->dd_quota);
1022 uint64_t
1023 dsl_dir_get_reservation(dsl_dir_t *dd)
1025 return (dsl_dir_phys(dd)->dd_reserved);
1028 uint64_t
1029 dsl_dir_get_compressratio(dsl_dir_t *dd)
1031 /* a fixed point number, 100x the ratio */
1032 return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
1033 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
1034 dsl_dir_phys(dd)->dd_compressed_bytes));
1037 uint64_t
1038 dsl_dir_get_logicalused(dsl_dir_t *dd)
1040 return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
1043 uint64_t
1044 dsl_dir_get_usedsnap(dsl_dir_t *dd)
1046 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1049 uint64_t
1050 dsl_dir_get_usedds(dsl_dir_t *dd)
1052 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1055 uint64_t
1056 dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1058 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1061 uint64_t
1062 dsl_dir_get_usedchild(dsl_dir_t *dd)
1064 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1065 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1068 void
1069 dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1071 dsl_dataset_t *ds;
1072 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1073 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1075 dsl_dataset_name(ds, buf);
1077 dsl_dataset_rele(ds, FTAG);
1081 dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
1083 if (dsl_dir_is_zapified(dd)) {
1084 objset_t *os = dd->dd_pool->dp_meta_objset;
1085 return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1086 sizeof (*count), 1, count));
1087 } else {
1088 return (SET_ERROR(ENOENT));
1093 dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
1095 if (dsl_dir_is_zapified(dd)) {
1096 objset_t *os = dd->dd_pool->dp_meta_objset;
1097 return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1098 sizeof (*count), 1, count));
1099 } else {
1100 return (SET_ERROR(ENOENT));
1104 void
1105 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1107 mutex_enter(&dd->dd_lock);
1108 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
1109 dsl_dir_get_quota(dd));
1110 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
1111 dsl_dir_get_reservation(dd));
1112 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
1113 dsl_dir_get_logicalused(dd));
1114 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1115 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
1116 dsl_dir_get_usedsnap(dd));
1117 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
1118 dsl_dir_get_usedds(dd));
1119 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
1120 dsl_dir_get_usedrefreserv(dd));
1121 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
1122 dsl_dir_get_usedchild(dd));
1124 mutex_exit(&dd->dd_lock);
1126 uint64_t count;
1127 if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1128 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1129 count);
1131 if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1132 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1133 count);
1136 if (dsl_dir_is_clone(dd)) {
1137 char buf[ZFS_MAX_DATASET_NAME_LEN];
1138 dsl_dir_get_origin(dd, buf);
1139 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1144 void
1145 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1147 dsl_pool_t *dp = dd->dd_pool;
1149 ASSERT(dsl_dir_phys(dd));
1151 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1152 /* up the hold count until we can be written out */
1153 dmu_buf_add_ref(dd->dd_dbuf, dd);
1157 static int64_t
1158 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1160 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1161 uint64_t new_accounted =
1162 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1163 return (new_accounted - old_accounted);
1166 void
1167 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1169 ASSERT(dmu_tx_is_syncing(tx));
1171 mutex_enter(&dd->dd_lock);
1172 ASSERT0(dd->dd_tempreserved[tx->tx_txg & TXG_MASK]);
1173 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", (u_longlong_t)tx->tx_txg,
1174 (u_longlong_t)dd->dd_space_towrite[tx->tx_txg & TXG_MASK] / 1024);
1175 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] = 0;
1176 mutex_exit(&dd->dd_lock);
1178 /* release the hold from dsl_dir_dirty */
1179 dmu_buf_rele(dd->dd_dbuf, dd);
1182 static uint64_t
1183 dsl_dir_space_towrite(dsl_dir_t *dd)
1185 uint64_t space = 0;
1187 ASSERT(MUTEX_HELD(&dd->dd_lock));
1189 for (int i = 0; i < TXG_SIZE; i++)
1190 space += dd->dd_space_towrite[i & TXG_MASK];
1192 return (space);
1196 * How much space would dd have available if ancestor had delta applied
1197 * to it? If ondiskonly is set, we're only interested in what's
1198 * on-disk, not estimated pending changes.
1200 uint64_t
1201 dsl_dir_space_available(dsl_dir_t *dd,
1202 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1204 uint64_t parentspace, myspace, quota, used;
1207 * If there are no restrictions otherwise, assume we have
1208 * unlimited space available.
1210 quota = UINT64_MAX;
1211 parentspace = UINT64_MAX;
1213 if (dd->dd_parent != NULL) {
1214 parentspace = dsl_dir_space_available(dd->dd_parent,
1215 ancestor, delta, ondiskonly);
1218 mutex_enter(&dd->dd_lock);
1219 if (dsl_dir_phys(dd)->dd_quota != 0)
1220 quota = dsl_dir_phys(dd)->dd_quota;
1221 used = dsl_dir_phys(dd)->dd_used_bytes;
1222 if (!ondiskonly)
1223 used += dsl_dir_space_towrite(dd);
1225 if (dd->dd_parent == NULL) {
1226 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,
1227 ZFS_SPACE_CHECK_NORMAL);
1228 quota = MIN(quota, poolsize);
1231 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1233 * We have some space reserved, in addition to what our
1234 * parent gave us.
1236 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1239 if (dd == ancestor) {
1240 ASSERT(delta <= 0);
1241 ASSERT(used >= -delta);
1242 used += delta;
1243 if (parentspace != UINT64_MAX)
1244 parentspace -= delta;
1247 if (used > quota) {
1248 /* over quota */
1249 myspace = 0;
1250 } else {
1252 * the lesser of the space provided by our parent and
1253 * the space left in our quota
1255 myspace = MIN(parentspace, quota - used);
1258 mutex_exit(&dd->dd_lock);
1260 return (myspace);
1263 struct tempreserve {
1264 list_node_t tr_node;
1265 dsl_dir_t *tr_ds;
1266 uint64_t tr_size;
1269 static int
1270 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1271 boolean_t ignorequota, list_t *tr_list,
1272 dmu_tx_t *tx, boolean_t first)
1274 uint64_t txg;
1275 uint64_t quota;
1276 struct tempreserve *tr;
1277 int retval;
1278 uint64_t ext_quota;
1279 uint64_t ref_rsrv;
1281 top_of_function:
1282 txg = tx->tx_txg;
1283 retval = EDQUOT;
1284 ref_rsrv = 0;
1286 ASSERT3U(txg, !=, 0);
1287 ASSERT3S(asize, >, 0);
1289 mutex_enter(&dd->dd_lock);
1292 * Check against the dsl_dir's quota. We don't add in the delta
1293 * when checking for over-quota because they get one free hit.
1295 uint64_t est_inflight = dsl_dir_space_towrite(dd);
1296 for (int i = 0; i < TXG_SIZE; i++)
1297 est_inflight += dd->dd_tempreserved[i];
1298 uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1301 * On the first iteration, fetch the dataset's used-on-disk and
1302 * refreservation values. Also, if checkrefquota is set, test if
1303 * allocating this space would exceed the dataset's refquota.
1305 if (first && tx->tx_objset) {
1306 int error;
1307 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1309 error = dsl_dataset_check_quota(ds, !netfree,
1310 asize, est_inflight, &used_on_disk, &ref_rsrv);
1311 if (error != 0) {
1312 mutex_exit(&dd->dd_lock);
1313 DMU_TX_STAT_BUMP(dmu_tx_quota);
1314 return (error);
1319 * If this transaction will result in a net free of space,
1320 * we want to let it through.
1322 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0 ||
1323 (tx->tx_objset && dmu_objset_type(tx->tx_objset) == DMU_OST_ZVOL &&
1324 zvol_enforce_quotas == B_FALSE))
1325 quota = UINT64_MAX;
1326 else
1327 quota = dsl_dir_phys(dd)->dd_quota;
1330 * Adjust the quota against the actual pool size at the root
1331 * minus any outstanding deferred frees.
1332 * To ensure that it's possible to remove files from a full
1333 * pool without inducing transient overcommits, we throttle
1334 * netfree transactions against a quota that is slightly larger,
1335 * but still within the pool's allocation slop. In cases where
1336 * we're very close to full, this will allow a steady trickle of
1337 * removes to get through.
1339 if (dd->dd_parent == NULL) {
1340 uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,
1341 (netfree) ?
1342 ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);
1344 if (avail < quota) {
1345 quota = avail;
1346 retval = SET_ERROR(ENOSPC);
1351 * If they are requesting more space, and our current estimate
1352 * is over quota, they get to try again unless the actual
1353 * on-disk is over quota and there are no pending changes
1354 * or deferred frees (which may free up space for us).
1356 ext_quota = quota >> 5;
1357 if (quota == UINT64_MAX)
1358 ext_quota = 0;
1360 if (used_on_disk >= quota) {
1361 /* Quota exceeded */
1362 mutex_exit(&dd->dd_lock);
1363 DMU_TX_STAT_BUMP(dmu_tx_quota);
1364 return (retval);
1365 } else if (used_on_disk + est_inflight >= quota + ext_quota) {
1366 if (est_inflight > 0 || used_on_disk < quota) {
1367 retval = SET_ERROR(ERESTART);
1368 } else {
1369 ASSERT3U(used_on_disk, >=, quota);
1371 if (retval == ENOSPC && (used_on_disk - quota) <
1372 dsl_pool_deferred_space(dd->dd_pool)) {
1373 retval = SET_ERROR(ERESTART);
1377 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1378 "quota=%lluK tr=%lluK err=%d\n",
1379 (u_longlong_t)used_on_disk>>10,
1380 (u_longlong_t)est_inflight>>10,
1381 (u_longlong_t)quota>>10, (u_longlong_t)asize>>10, retval);
1382 mutex_exit(&dd->dd_lock);
1383 DMU_TX_STAT_BUMP(dmu_tx_quota);
1384 return (retval);
1387 /* We need to up our estimated delta before dropping dd_lock */
1388 dd->dd_tempreserved[txg & TXG_MASK] += asize;
1390 uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1391 asize - ref_rsrv);
1392 mutex_exit(&dd->dd_lock);
1394 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1395 tr->tr_ds = dd;
1396 tr->tr_size = asize;
1397 list_insert_tail(tr_list, tr);
1399 /* see if it's OK with our parent */
1400 if (dd->dd_parent != NULL && parent_rsrv != 0) {
1402 * Recurse on our parent without recursion. This has been
1403 * observed to be potentially large stack usage even within
1404 * the test suite. Largest seen stack was 7632 bytes on linux.
1407 dd = dd->dd_parent;
1408 asize = parent_rsrv;
1409 ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1410 first = B_FALSE;
1411 goto top_of_function;
1414 return (0);
1418 * Reserve space in this dsl_dir, to be used in this tx's txg.
1419 * After the space has been dirtied (and dsl_dir_willuse_space()
1420 * has been called), the reservation should be canceled, using
1421 * dsl_dir_tempreserve_clear().
1424 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1425 boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
1427 int err;
1428 list_t *tr_list;
1430 if (asize == 0) {
1431 *tr_cookiep = NULL;
1432 return (0);
1435 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1436 list_create(tr_list, sizeof (struct tempreserve),
1437 offsetof(struct tempreserve, tr_node));
1438 ASSERT3S(asize, >, 0);
1440 err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg);
1441 if (err == 0) {
1442 struct tempreserve *tr;
1444 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1445 tr->tr_size = lsize;
1446 list_insert_tail(tr_list, tr);
1447 } else {
1448 if (err == EAGAIN) {
1450 * If arc_memory_throttle() detected that pageout
1451 * is running and we are low on memory, we delay new
1452 * non-pageout transactions to give pageout an
1453 * advantage.
1455 * It is unfortunate to be delaying while the caller's
1456 * locks are held.
1458 txg_delay(dd->dd_pool, tx->tx_txg,
1459 MSEC2NSEC(10), MSEC2NSEC(10));
1460 err = SET_ERROR(ERESTART);
1464 if (err == 0) {
1465 err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1466 B_FALSE, tr_list, tx, B_TRUE);
1469 if (err != 0)
1470 dsl_dir_tempreserve_clear(tr_list, tx);
1471 else
1472 *tr_cookiep = tr_list;
1474 return (err);
1478 * Clear a temporary reservation that we previously made with
1479 * dsl_dir_tempreserve_space().
1481 void
1482 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1484 int txgidx = tx->tx_txg & TXG_MASK;
1485 list_t *tr_list = tr_cookie;
1486 struct tempreserve *tr;
1488 ASSERT3U(tx->tx_txg, !=, 0);
1490 if (tr_cookie == NULL)
1491 return;
1493 while ((tr = list_head(tr_list)) != NULL) {
1494 if (tr->tr_ds) {
1495 mutex_enter(&tr->tr_ds->dd_lock);
1496 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1497 tr->tr_size);
1498 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1499 mutex_exit(&tr->tr_ds->dd_lock);
1500 } else {
1501 arc_tempreserve_clear(tr->tr_size);
1503 list_remove(tr_list, tr);
1504 kmem_free(tr, sizeof (struct tempreserve));
1507 kmem_free(tr_list, sizeof (list_t));
1511 * This should be called from open context when we think we're going to write
1512 * or free space, for example when dirtying data. Be conservative; it's okay
1513 * to write less space or free more, but we don't want to write more or free
1514 * less than the amount specified.
1516 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1517 * version however it has been adjusted to use an iterative rather than
1518 * recursive algorithm to minimize stack usage.
1520 void
1521 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1523 int64_t parent_space;
1524 uint64_t est_used;
1526 do {
1527 mutex_enter(&dd->dd_lock);
1528 if (space > 0)
1529 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1531 est_used = dsl_dir_space_towrite(dd) +
1532 dsl_dir_phys(dd)->dd_used_bytes;
1533 parent_space = parent_delta(dd, est_used, space);
1534 mutex_exit(&dd->dd_lock);
1536 /* Make sure that we clean up dd_space_to* */
1537 dsl_dir_dirty(dd, tx);
1539 dd = dd->dd_parent;
1540 space = parent_space;
1541 } while (space && dd);
1544 /* call from syncing context when we actually write/free space for this dd */
1545 void
1546 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1547 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1549 int64_t accounted_delta;
1551 ASSERT(dmu_tx_is_syncing(tx));
1552 ASSERT(type < DD_USED_NUM);
1554 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1557 * dsl_dataset_set_refreservation_sync_impl() calls this with
1558 * dd_lock held, so that it can atomically update
1559 * ds->ds_reserved and the dsl_dir accounting, so that
1560 * dsl_dataset_check_quota() can see dataset and dir accounting
1561 * consistently.
1563 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1564 if (needlock)
1565 mutex_enter(&dd->dd_lock);
1566 dsl_dir_phys_t *ddp = dsl_dir_phys(dd);
1567 accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used);
1568 ASSERT(used >= 0 || ddp->dd_used_bytes >= -used);
1569 ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed);
1570 ASSERT(uncompressed >= 0 ||
1571 ddp->dd_uncompressed_bytes >= -uncompressed);
1572 ddp->dd_used_bytes += used;
1573 ddp->dd_uncompressed_bytes += uncompressed;
1574 ddp->dd_compressed_bytes += compressed;
1576 if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1577 ASSERT(used >= 0 || ddp->dd_used_breakdown[type] >= -used);
1578 ddp->dd_used_breakdown[type] += used;
1579 #ifdef ZFS_DEBUG
1581 dd_used_t t;
1582 uint64_t u = 0;
1583 for (t = 0; t < DD_USED_NUM; t++)
1584 u += ddp->dd_used_breakdown[t];
1585 ASSERT3U(u, ==, ddp->dd_used_bytes);
1587 #endif
1589 if (needlock)
1590 mutex_exit(&dd->dd_lock);
1592 if (dd->dd_parent != NULL) {
1593 dsl_dir_diduse_transfer_space(dd->dd_parent,
1594 accounted_delta, compressed, uncompressed,
1595 used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1599 void
1600 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1601 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1603 ASSERT(dmu_tx_is_syncing(tx));
1604 ASSERT(oldtype < DD_USED_NUM);
1605 ASSERT(newtype < DD_USED_NUM);
1607 dsl_dir_phys_t *ddp = dsl_dir_phys(dd);
1608 if (delta == 0 ||
1609 !(ddp->dd_flags & DD_FLAG_USED_BREAKDOWN))
1610 return;
1612 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1613 mutex_enter(&dd->dd_lock);
1614 ASSERT(delta > 0 ?
1615 ddp->dd_used_breakdown[oldtype] >= delta :
1616 ddp->dd_used_breakdown[newtype] >= -delta);
1617 ASSERT(ddp->dd_used_bytes >= ABS(delta));
1618 ddp->dd_used_breakdown[oldtype] -= delta;
1619 ddp->dd_used_breakdown[newtype] += delta;
1620 mutex_exit(&dd->dd_lock);
1623 void
1624 dsl_dir_diduse_transfer_space(dsl_dir_t *dd, int64_t used,
1625 int64_t compressed, int64_t uncompressed, int64_t tonew,
1626 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1628 int64_t accounted_delta;
1630 ASSERT(dmu_tx_is_syncing(tx));
1631 ASSERT(oldtype < DD_USED_NUM);
1632 ASSERT(newtype < DD_USED_NUM);
1634 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1636 mutex_enter(&dd->dd_lock);
1637 dsl_dir_phys_t *ddp = dsl_dir_phys(dd);
1638 accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used);
1639 ASSERT(used >= 0 || ddp->dd_used_bytes >= -used);
1640 ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed);
1641 ASSERT(uncompressed >= 0 ||
1642 ddp->dd_uncompressed_bytes >= -uncompressed);
1643 ddp->dd_used_bytes += used;
1644 ddp->dd_uncompressed_bytes += uncompressed;
1645 ddp->dd_compressed_bytes += compressed;
1647 if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1648 ASSERT(tonew - used <= 0 ||
1649 ddp->dd_used_breakdown[oldtype] >= tonew - used);
1650 ASSERT(tonew >= 0 ||
1651 ddp->dd_used_breakdown[newtype] >= -tonew);
1652 ddp->dd_used_breakdown[oldtype] -= tonew - used;
1653 ddp->dd_used_breakdown[newtype] += tonew;
1654 #ifdef ZFS_DEBUG
1656 dd_used_t t;
1657 uint64_t u = 0;
1658 for (t = 0; t < DD_USED_NUM; t++)
1659 u += ddp->dd_used_breakdown[t];
1660 ASSERT3U(u, ==, ddp->dd_used_bytes);
1662 #endif
1664 mutex_exit(&dd->dd_lock);
1666 if (dd->dd_parent != NULL) {
1667 dsl_dir_diduse_transfer_space(dd->dd_parent,
1668 accounted_delta, compressed, uncompressed,
1669 used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1673 typedef struct dsl_dir_set_qr_arg {
1674 const char *ddsqra_name;
1675 zprop_source_t ddsqra_source;
1676 uint64_t ddsqra_value;
1677 } dsl_dir_set_qr_arg_t;
1679 static int
1680 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1682 dsl_dir_set_qr_arg_t *ddsqra = arg;
1683 dsl_pool_t *dp = dmu_tx_pool(tx);
1684 dsl_dataset_t *ds;
1685 int error;
1686 uint64_t towrite, newval;
1688 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1689 if (error != 0)
1690 return (error);
1692 error = dsl_prop_predict(ds->ds_dir, "quota",
1693 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1694 if (error != 0) {
1695 dsl_dataset_rele(ds, FTAG);
1696 return (error);
1699 if (newval == 0) {
1700 dsl_dataset_rele(ds, FTAG);
1701 return (0);
1704 mutex_enter(&ds->ds_dir->dd_lock);
1706 * If we are doing the preliminary check in open context, and
1707 * there are pending changes, then don't fail it, since the
1708 * pending changes could under-estimate the amount of space to be
1709 * freed up.
1711 towrite = dsl_dir_space_towrite(ds->ds_dir);
1712 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1713 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1714 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1715 error = SET_ERROR(ENOSPC);
1717 mutex_exit(&ds->ds_dir->dd_lock);
1718 dsl_dataset_rele(ds, FTAG);
1719 return (error);
1722 static void
1723 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1725 dsl_dir_set_qr_arg_t *ddsqra = arg;
1726 dsl_pool_t *dp = dmu_tx_pool(tx);
1727 dsl_dataset_t *ds;
1728 uint64_t newval;
1730 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1732 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1733 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1734 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1735 &ddsqra->ddsqra_value, tx);
1737 VERIFY0(dsl_prop_get_int_ds(ds,
1738 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1739 } else {
1740 newval = ddsqra->ddsqra_value;
1741 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1742 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1745 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1746 mutex_enter(&ds->ds_dir->dd_lock);
1747 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1748 mutex_exit(&ds->ds_dir->dd_lock);
1749 dsl_dataset_rele(ds, FTAG);
1753 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1755 dsl_dir_set_qr_arg_t ddsqra;
1757 ddsqra.ddsqra_name = ddname;
1758 ddsqra.ddsqra_source = source;
1759 ddsqra.ddsqra_value = quota;
1761 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1762 dsl_dir_set_quota_sync, &ddsqra, 0,
1763 ZFS_SPACE_CHECK_EXTRA_RESERVED));
1766 static int
1767 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1769 dsl_dir_set_qr_arg_t *ddsqra = arg;
1770 dsl_pool_t *dp = dmu_tx_pool(tx);
1771 dsl_dataset_t *ds;
1772 dsl_dir_t *dd;
1773 uint64_t newval, used, avail;
1774 int error;
1776 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1777 if (error != 0)
1778 return (error);
1779 dd = ds->ds_dir;
1782 * If we are doing the preliminary check in open context, the
1783 * space estimates may be inaccurate.
1785 if (!dmu_tx_is_syncing(tx)) {
1786 dsl_dataset_rele(ds, FTAG);
1787 return (0);
1790 error = dsl_prop_predict(ds->ds_dir,
1791 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1792 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1793 if (error != 0) {
1794 dsl_dataset_rele(ds, FTAG);
1795 return (error);
1798 mutex_enter(&dd->dd_lock);
1799 used = dsl_dir_phys(dd)->dd_used_bytes;
1800 mutex_exit(&dd->dd_lock);
1802 if (dd->dd_parent) {
1803 avail = dsl_dir_space_available(dd->dd_parent,
1804 NULL, 0, FALSE);
1805 } else {
1806 avail = dsl_pool_adjustedsize(dd->dd_pool,
1807 ZFS_SPACE_CHECK_NORMAL) - used;
1810 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1811 uint64_t delta = MAX(used, newval) -
1812 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1814 if (delta > avail ||
1815 (dsl_dir_phys(dd)->dd_quota > 0 &&
1816 newval > dsl_dir_phys(dd)->dd_quota))
1817 error = SET_ERROR(ENOSPC);
1820 dsl_dataset_rele(ds, FTAG);
1821 return (error);
1824 void
1825 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1827 uint64_t used;
1828 int64_t delta;
1830 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1832 mutex_enter(&dd->dd_lock);
1833 used = dsl_dir_phys(dd)->dd_used_bytes;
1834 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1835 dsl_dir_phys(dd)->dd_reserved = value;
1837 if (dd->dd_parent != NULL) {
1838 /* Roll up this additional usage into our ancestors */
1839 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1840 delta, 0, 0, tx);
1842 mutex_exit(&dd->dd_lock);
1845 static void
1846 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1848 dsl_dir_set_qr_arg_t *ddsqra = arg;
1849 dsl_pool_t *dp = dmu_tx_pool(tx);
1850 dsl_dataset_t *ds;
1851 uint64_t newval;
1853 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1855 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1856 dsl_prop_set_sync_impl(ds,
1857 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1858 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1859 &ddsqra->ddsqra_value, tx);
1861 VERIFY0(dsl_prop_get_int_ds(ds,
1862 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1863 } else {
1864 newval = ddsqra->ddsqra_value;
1865 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1866 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1867 (longlong_t)newval);
1870 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1871 dsl_dataset_rele(ds, FTAG);
1875 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1876 uint64_t reservation)
1878 dsl_dir_set_qr_arg_t ddsqra;
1880 ddsqra.ddsqra_name = ddname;
1881 ddsqra.ddsqra_source = source;
1882 ddsqra.ddsqra_value = reservation;
1884 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1885 dsl_dir_set_reservation_sync, &ddsqra, 0,
1886 ZFS_SPACE_CHECK_EXTRA_RESERVED));
1889 static dsl_dir_t *
1890 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1892 for (; ds1; ds1 = ds1->dd_parent) {
1893 dsl_dir_t *dd;
1894 for (dd = ds2; dd; dd = dd->dd_parent) {
1895 if (ds1 == dd)
1896 return (dd);
1899 return (NULL);
1903 * If delta is applied to dd, how much of that delta would be applied to
1904 * ancestor? Syncing context only.
1906 static int64_t
1907 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1909 if (dd == ancestor)
1910 return (delta);
1912 mutex_enter(&dd->dd_lock);
1913 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1914 mutex_exit(&dd->dd_lock);
1915 return (would_change(dd->dd_parent, delta, ancestor));
1918 typedef struct dsl_dir_rename_arg {
1919 const char *ddra_oldname;
1920 const char *ddra_newname;
1921 cred_t *ddra_cred;
1922 proc_t *ddra_proc;
1923 } dsl_dir_rename_arg_t;
1925 typedef struct dsl_valid_rename_arg {
1926 int char_delta;
1927 int nest_delta;
1928 } dsl_valid_rename_arg_t;
1930 static int
1931 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1933 (void) dp;
1934 dsl_valid_rename_arg_t *dvra = arg;
1935 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1937 dsl_dataset_name(ds, namebuf);
1939 ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN),
1940 <, ZFS_MAX_DATASET_NAME_LEN);
1941 int namelen = strlen(namebuf) + dvra->char_delta;
1942 int depth = get_dataset_depth(namebuf) + dvra->nest_delta;
1944 if (namelen >= ZFS_MAX_DATASET_NAME_LEN)
1945 return (SET_ERROR(ENAMETOOLONG));
1946 if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting)
1947 return (SET_ERROR(ENAMETOOLONG));
1948 return (0);
1951 static int
1952 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1954 dsl_dir_rename_arg_t *ddra = arg;
1955 dsl_pool_t *dp = dmu_tx_pool(tx);
1956 dsl_dir_t *dd, *newparent;
1957 dsl_valid_rename_arg_t dvra;
1958 dsl_dataset_t *parentds;
1959 objset_t *parentos;
1960 const char *mynewname;
1961 int error;
1963 /* target dir should exist */
1964 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1965 if (error != 0)
1966 return (error);
1968 /* new parent should exist */
1969 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1970 &newparent, &mynewname);
1971 if (error != 0) {
1972 dsl_dir_rele(dd, FTAG);
1973 return (error);
1976 /* can't rename to different pool */
1977 if (dd->dd_pool != newparent->dd_pool) {
1978 dsl_dir_rele(newparent, FTAG);
1979 dsl_dir_rele(dd, FTAG);
1980 return (SET_ERROR(EXDEV));
1983 /* new name should not already exist */
1984 if (mynewname == NULL) {
1985 dsl_dir_rele(newparent, FTAG);
1986 dsl_dir_rele(dd, FTAG);
1987 return (SET_ERROR(EEXIST));
1990 /* can't rename below anything but filesystems (eg. no ZVOLs) */
1991 error = dsl_dataset_hold_obj(newparent->dd_pool,
1992 dsl_dir_phys(newparent)->dd_head_dataset_obj, FTAG, &parentds);
1993 if (error != 0) {
1994 dsl_dir_rele(newparent, FTAG);
1995 dsl_dir_rele(dd, FTAG);
1996 return (error);
1998 error = dmu_objset_from_ds(parentds, &parentos);
1999 if (error != 0) {
2000 dsl_dataset_rele(parentds, FTAG);
2001 dsl_dir_rele(newparent, FTAG);
2002 dsl_dir_rele(dd, FTAG);
2003 return (error);
2005 if (dmu_objset_type(parentos) != DMU_OST_ZFS) {
2006 dsl_dataset_rele(parentds, FTAG);
2007 dsl_dir_rele(newparent, FTAG);
2008 dsl_dir_rele(dd, FTAG);
2009 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
2011 dsl_dataset_rele(parentds, FTAG);
2013 ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN),
2014 <, ZFS_MAX_DATASET_NAME_LEN);
2015 ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN),
2016 <, ZFS_MAX_DATASET_NAME_LEN);
2017 dvra.char_delta = strlen(ddra->ddra_newname)
2018 - strlen(ddra->ddra_oldname);
2019 dvra.nest_delta = get_dataset_depth(ddra->ddra_newname)
2020 - get_dataset_depth(ddra->ddra_oldname);
2022 /* if the name length is growing, validate child name lengths */
2023 if (dvra.char_delta > 0 || dvra.nest_delta > 0) {
2024 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
2025 &dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2026 if (error != 0) {
2027 dsl_dir_rele(newparent, FTAG);
2028 dsl_dir_rele(dd, FTAG);
2029 return (error);
2033 if (dmu_tx_is_syncing(tx)) {
2034 if (spa_feature_is_active(dp->dp_spa,
2035 SPA_FEATURE_FS_SS_LIMIT)) {
2037 * Although this is the check function and we don't
2038 * normally make on-disk changes in check functions,
2039 * we need to do that here.
2041 * Ensure this portion of the tree's counts have been
2042 * initialized in case the new parent has limits set.
2044 dsl_dir_init_fs_ss_count(dd, tx);
2048 if (newparent != dd->dd_parent) {
2049 /* is there enough space? */
2050 uint64_t myspace =
2051 MAX(dsl_dir_phys(dd)->dd_used_bytes,
2052 dsl_dir_phys(dd)->dd_reserved);
2053 objset_t *os = dd->dd_pool->dp_meta_objset;
2054 uint64_t fs_cnt = 0;
2055 uint64_t ss_cnt = 0;
2057 if (dsl_dir_is_zapified(dd)) {
2058 int err;
2060 err = zap_lookup(os, dd->dd_object,
2061 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2062 &fs_cnt);
2063 if (err != ENOENT && err != 0) {
2064 dsl_dir_rele(newparent, FTAG);
2065 dsl_dir_rele(dd, FTAG);
2066 return (err);
2070 * have to add 1 for the filesystem itself that we're
2071 * moving
2073 fs_cnt++;
2075 err = zap_lookup(os, dd->dd_object,
2076 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2077 &ss_cnt);
2078 if (err != ENOENT && err != 0) {
2079 dsl_dir_rele(newparent, FTAG);
2080 dsl_dir_rele(dd, FTAG);
2081 return (err);
2085 /* check for encryption errors */
2086 error = dsl_dir_rename_crypt_check(dd, newparent);
2087 if (error != 0) {
2088 dsl_dir_rele(newparent, FTAG);
2089 dsl_dir_rele(dd, FTAG);
2090 return (SET_ERROR(EACCES));
2093 /* no rename into our descendant */
2094 if (closest_common_ancestor(dd, newparent) == dd) {
2095 dsl_dir_rele(newparent, FTAG);
2096 dsl_dir_rele(dd, FTAG);
2097 return (SET_ERROR(EINVAL));
2100 error = dsl_dir_transfer_possible(dd->dd_parent,
2101 newparent, fs_cnt, ss_cnt, myspace,
2102 ddra->ddra_cred, ddra->ddra_proc);
2103 if (error != 0) {
2104 dsl_dir_rele(newparent, FTAG);
2105 dsl_dir_rele(dd, FTAG);
2106 return (error);
2110 dsl_dir_rele(newparent, FTAG);
2111 dsl_dir_rele(dd, FTAG);
2112 return (0);
2115 static void
2116 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
2118 dsl_dir_rename_arg_t *ddra = arg;
2119 dsl_pool_t *dp = dmu_tx_pool(tx);
2120 dsl_dir_t *dd, *newparent;
2121 const char *mynewname;
2122 objset_t *mos = dp->dp_meta_objset;
2124 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
2125 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
2126 &mynewname));
2128 ASSERT3P(mynewname, !=, NULL);
2130 /* Log this before we change the name. */
2131 spa_history_log_internal_dd(dd, "rename", tx,
2132 "-> %s", ddra->ddra_newname);
2134 if (newparent != dd->dd_parent) {
2135 objset_t *os = dd->dd_pool->dp_meta_objset;
2136 uint64_t fs_cnt = 0;
2137 uint64_t ss_cnt = 0;
2140 * We already made sure the dd counts were initialized in the
2141 * check function.
2143 if (spa_feature_is_active(dp->dp_spa,
2144 SPA_FEATURE_FS_SS_LIMIT)) {
2145 VERIFY0(zap_lookup(os, dd->dd_object,
2146 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2147 &fs_cnt));
2148 /* add 1 for the filesystem itself that we're moving */
2149 fs_cnt++;
2151 VERIFY0(zap_lookup(os, dd->dd_object,
2152 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2153 &ss_cnt));
2156 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
2157 DD_FIELD_FILESYSTEM_COUNT, tx);
2158 dsl_fs_ss_count_adjust(newparent, fs_cnt,
2159 DD_FIELD_FILESYSTEM_COUNT, tx);
2161 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
2162 DD_FIELD_SNAPSHOT_COUNT, tx);
2163 dsl_fs_ss_count_adjust(newparent, ss_cnt,
2164 DD_FIELD_SNAPSHOT_COUNT, tx);
2166 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
2167 -dsl_dir_phys(dd)->dd_used_bytes,
2168 -dsl_dir_phys(dd)->dd_compressed_bytes,
2169 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2170 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
2171 dsl_dir_phys(dd)->dd_used_bytes,
2172 dsl_dir_phys(dd)->dd_compressed_bytes,
2173 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2175 if (dsl_dir_phys(dd)->dd_reserved >
2176 dsl_dir_phys(dd)->dd_used_bytes) {
2177 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
2178 dsl_dir_phys(dd)->dd_used_bytes;
2180 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2181 -unused_rsrv, 0, 0, tx);
2182 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
2183 unused_rsrv, 0, 0, tx);
2187 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2189 /* remove from old parent zapobj */
2190 VERIFY0(zap_remove(mos,
2191 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
2192 dd->dd_myname, tx));
2194 (void) strlcpy(dd->dd_myname, mynewname,
2195 sizeof (dd->dd_myname));
2196 dsl_dir_rele(dd->dd_parent, dd);
2197 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
2198 VERIFY0(dsl_dir_hold_obj(dp,
2199 newparent->dd_object, NULL, dd, &dd->dd_parent));
2201 /* add to new parent zapobj */
2202 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
2203 dd->dd_myname, 8, 1, &dd->dd_object, tx));
2205 /* TODO: A rename callback to avoid these layering violations. */
2206 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
2207 zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,
2208 ddra->ddra_newname, B_TRUE);
2210 dsl_prop_notify_all(dd);
2212 dsl_dir_rele(newparent, FTAG);
2213 dsl_dir_rele(dd, FTAG);
2217 dsl_dir_rename(const char *oldname, const char *newname)
2219 dsl_dir_rename_arg_t ddra;
2221 ddra.ddra_oldname = oldname;
2222 ddra.ddra_newname = newname;
2223 ddra.ddra_cred = CRED();
2224 ddra.ddra_proc = curproc;
2226 return (dsl_sync_task(oldname,
2227 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2228 3, ZFS_SPACE_CHECK_RESERVED));
2232 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2233 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space,
2234 cred_t *cr, proc_t *proc)
2236 dsl_dir_t *ancestor;
2237 int64_t adelta;
2238 uint64_t avail;
2239 int err;
2241 ancestor = closest_common_ancestor(sdd, tdd);
2242 adelta = would_change(sdd, -space, ancestor);
2243 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2244 if (avail < space)
2245 return (SET_ERROR(ENOSPC));
2247 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2248 ancestor, cr, proc);
2249 if (err != 0)
2250 return (err);
2251 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2252 ancestor, cr, proc);
2253 if (err != 0)
2254 return (err);
2256 return (0);
2259 inode_timespec_t
2260 dsl_dir_snap_cmtime(dsl_dir_t *dd)
2262 inode_timespec_t t;
2264 mutex_enter(&dd->dd_lock);
2265 t = dd->dd_snap_cmtime;
2266 mutex_exit(&dd->dd_lock);
2268 return (t);
2271 void
2272 dsl_dir_snap_cmtime_update(dsl_dir_t *dd, dmu_tx_t *tx)
2274 dsl_pool_t *dp = dmu_tx_pool(tx);
2275 inode_timespec_t t;
2276 gethrestime(&t);
2278 mutex_enter(&dd->dd_lock);
2279 dd->dd_snap_cmtime = t;
2280 if (spa_feature_is_enabled(dp->dp_spa,
2281 SPA_FEATURE_EXTENSIBLE_DATASET)) {
2282 objset_t *mos = dd->dd_pool->dp_meta_objset;
2283 uint64_t ddobj = dd->dd_object;
2284 dsl_dir_zapify(dd, tx);
2285 VERIFY0(zap_update(mos, ddobj,
2286 DD_FIELD_SNAPSHOTS_CHANGED,
2287 sizeof (uint64_t),
2288 sizeof (inode_timespec_t) / sizeof (uint64_t),
2289 &t, tx));
2291 mutex_exit(&dd->dd_lock);
2294 void
2295 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2297 objset_t *mos = dd->dd_pool->dp_meta_objset;
2298 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2301 boolean_t
2302 dsl_dir_is_zapified(dsl_dir_t *dd)
2304 dmu_object_info_t doi;
2306 dmu_object_info_from_db(dd->dd_dbuf, &doi);
2307 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2310 void
2311 dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj)
2313 objset_t *mos = dd->dd_pool->dp_meta_objset;
2314 ASSERT(spa_feature_is_active(dd->dd_pool->dp_spa,
2315 SPA_FEATURE_LIVELIST));
2316 dsl_deadlist_open(&dd->dd_livelist, mos, obj);
2317 bplist_create(&dd->dd_pending_allocs);
2318 bplist_create(&dd->dd_pending_frees);
2321 void
2322 dsl_dir_livelist_close(dsl_dir_t *dd)
2324 dsl_deadlist_close(&dd->dd_livelist);
2325 bplist_destroy(&dd->dd_pending_allocs);
2326 bplist_destroy(&dd->dd_pending_frees);
2329 void
2330 dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total)
2332 uint64_t obj;
2333 dsl_pool_t *dp = dmu_tx_pool(tx);
2334 spa_t *spa = dp->dp_spa;
2335 livelist_condense_entry_t to_condense = spa->spa_to_condense;
2337 if (!dsl_deadlist_is_open(&dd->dd_livelist))
2338 return;
2341 * If the livelist being removed is set to be condensed, stop the
2342 * condense zthr and indicate the cancellation in the spa_to_condense
2343 * struct in case the condense no-wait synctask has already started
2345 zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
2346 if (ll_condense_thread != NULL &&
2347 (to_condense.ds != NULL) && (to_condense.ds->ds_dir == dd)) {
2349 * We use zthr_wait_cycle_done instead of zthr_cancel
2350 * because we don't want to destroy the zthr, just have
2351 * it skip its current task.
2353 spa->spa_to_condense.cancelled = B_TRUE;
2354 zthr_wait_cycle_done(ll_condense_thread);
2356 * If we've returned from zthr_wait_cycle_done without
2357 * clearing the to_condense data structure it's either
2358 * because the no-wait synctask has started (which is
2359 * indicated by 'syncing' field of to_condense) and we
2360 * can expect it to clear to_condense on its own.
2361 * Otherwise, we returned before the zthr ran. The
2362 * checkfunc will now fail as cancelled == B_TRUE so we
2363 * can safely NULL out ds, allowing a different dir's
2364 * livelist to be condensed.
2366 * We can be sure that the to_condense struct will not
2367 * be repopulated at this stage because both this
2368 * function and dsl_livelist_try_condense execute in
2369 * syncing context.
2371 if ((spa->spa_to_condense.ds != NULL) &&
2372 !spa->spa_to_condense.syncing) {
2373 dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf,
2374 spa);
2375 spa->spa_to_condense.ds = NULL;
2379 dsl_dir_livelist_close(dd);
2380 VERIFY0(zap_lookup(dp->dp_meta_objset, dd->dd_object,
2381 DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &obj));
2382 VERIFY0(zap_remove(dp->dp_meta_objset, dd->dd_object,
2383 DD_FIELD_LIVELIST, tx));
2384 if (total) {
2385 dsl_deadlist_free(dp->dp_meta_objset, obj, tx);
2386 spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx);
2390 static int
2391 dsl_dir_activity_in_progress(dsl_dir_t *dd, dsl_dataset_t *ds,
2392 zfs_wait_activity_t activity, boolean_t *in_progress)
2394 int error = 0;
2396 ASSERT(MUTEX_HELD(&dd->dd_activity_lock));
2398 switch (activity) {
2399 case ZFS_WAIT_DELETEQ: {
2400 #ifdef _KERNEL
2401 objset_t *os;
2402 error = dmu_objset_from_ds(ds, &os);
2403 if (error != 0)
2404 break;
2406 mutex_enter(&os->os_user_ptr_lock);
2407 void *user = dmu_objset_get_user(os);
2408 mutex_exit(&os->os_user_ptr_lock);
2409 if (dmu_objset_type(os) != DMU_OST_ZFS ||
2410 user == NULL || zfs_get_vfs_flag_unmounted(os)) {
2411 *in_progress = B_FALSE;
2412 return (0);
2415 uint64_t readonly = B_FALSE;
2416 error = zfs_get_temporary_prop(ds, ZFS_PROP_READONLY, &readonly,
2417 NULL);
2419 if (error != 0)
2420 break;
2422 if (readonly || !spa_writeable(dd->dd_pool->dp_spa)) {
2423 *in_progress = B_FALSE;
2424 return (0);
2427 uint64_t count, unlinked_obj;
2428 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
2429 &unlinked_obj);
2430 if (error != 0) {
2431 dsl_dataset_rele(ds, FTAG);
2432 break;
2434 error = zap_count(os, unlinked_obj, &count);
2436 if (error == 0)
2437 *in_progress = (count != 0);
2438 break;
2439 #else
2441 * The delete queue is ZPL specific, and libzpool doesn't have
2442 * it. It doesn't make sense to wait for it.
2444 (void) ds;
2445 *in_progress = B_FALSE;
2446 break;
2447 #endif
2449 default:
2450 panic("unrecognized value for activity %d", activity);
2453 return (error);
2457 dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity,
2458 boolean_t *waited)
2460 int error = 0;
2461 boolean_t in_progress;
2462 dsl_pool_t *dp = dd->dd_pool;
2463 for (;;) {
2464 dsl_pool_config_enter(dp, FTAG);
2465 error = dsl_dir_activity_in_progress(dd, ds, activity,
2466 &in_progress);
2467 dsl_pool_config_exit(dp, FTAG);
2468 if (error != 0 || !in_progress)
2469 break;
2471 *waited = B_TRUE;
2473 if (cv_wait_sig(&dd->dd_activity_cv, &dd->dd_activity_lock) ==
2474 0 || dd->dd_activity_cancelled) {
2475 error = SET_ERROR(EINTR);
2476 break;
2479 return (error);
2482 void
2483 dsl_dir_cancel_waiters(dsl_dir_t *dd)
2485 mutex_enter(&dd->dd_activity_lock);
2486 dd->dd_activity_cancelled = B_TRUE;
2487 cv_broadcast(&dd->dd_activity_cv);
2488 while (dd->dd_activity_waiters > 0)
2489 cv_wait(&dd->dd_activity_cv, &dd->dd_activity_lock);
2490 mutex_exit(&dd->dd_activity_lock);
2493 #if defined(_KERNEL)
2494 EXPORT_SYMBOL(dsl_dir_set_quota);
2495 EXPORT_SYMBOL(dsl_dir_set_reservation);
2496 #endif
2498 /* CSTYLED */
2499 ZFS_MODULE_PARAM(zfs, , zvol_enforce_quotas, INT, ZMOD_RW,
2500 "Enable strict ZVOL quota enforcment");