]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_dir.c
Fix traverse_impl() kmem leak
[mirror_zfs.git] / module / zfs / dsl_dir.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
d2734cce 23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
b1118acb 24 * Copyright (c) 2013 Martin Matuska. All rights reserved.
788eb90c 25 * Copyright (c) 2014 Joyent, Inc. All rights reserved.
0c66c32d 26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
a0bd735a 27 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
34dc7c2f
BB
28 */
29
34dc7c2f
BB
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>
fa86b5db 38#include <sys/dmu_impl.h>
34dc7c2f 39#include <sys/spa.h>
ae76f45c 40#include <sys/spa_impl.h>
428870ff 41#include <sys/metaslab.h>
34dc7c2f
BB
42#include <sys/zap.h>
43#include <sys/zio.h>
44#include <sys/arc.h>
45#include <sys/sunddi.h>
788eb90c
JJ
46#include <sys/zfeature.h>
47#include <sys/policy.h>
48#include <sys/zfs_znode.h>
ba6a2402 49#include <sys/zvol.h>
34dc7c2f 50#include "zfs_namecheck.h"
788eb90c
JJ
51#include "zfs_prop.h"
52
53/*
54 * Filesystem and Snapshot Limits
55 * ------------------------------
56 *
57 * These limits are used to restrict the number of filesystems and/or snapshots
58 * that can be created at a given level in the tree or below. A typical
59 * use-case is with a delegated dataset where the administrator wants to ensure
60 * that a user within the zone is not creating too many additional filesystems
61 * or snapshots, even though they're not exceeding their space quota.
62 *
63 * The filesystem and snapshot counts are stored as extensible properties. This
64 * capability is controlled by a feature flag and must be enabled to be used.
65 * Once enabled, the feature is not active until the first limit is set. At
66 * that point, future operations to create/destroy filesystems or snapshots
67 * will validate and update the counts.
68 *
69 * Because the count properties will not exist before the feature is active,
70 * the counts are updated when a limit is first set on an uninitialized
71 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
72 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
73 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
74 * snapshot count properties on a node indicate uninitialized counts on that
75 * node.) When first setting a limit on an uninitialized node, the code starts
76 * at the filesystem with the new limit and descends into all sub-filesystems
77 * to add the count properties.
78 *
79 * In practice this is lightweight since a limit is typically set when the
80 * filesystem is created and thus has no children. Once valid, changing the
81 * limit value won't require a re-traversal since the counts are already valid.
82 * When recursively fixing the counts, if a node with a limit is encountered
83 * during the descent, the counts are known to be valid and there is no need to
84 * descend into that filesystem's children. The counts on filesystems above the
85 * one with the new limit will still be uninitialized, unless a limit is
86 * eventually set on one of those filesystems. The counts are always recursively
87 * updated when a limit is set on a dataset, unless there is already a limit.
88 * When a new limit value is set on a filesystem with an existing limit, it is
89 * possible for the new limit to be less than the current count at that level
90 * since a user who can change the limit is also allowed to exceed the limit.
91 *
92 * Once the feature is active, then whenever a filesystem or snapshot is
93 * created, the code recurses up the tree, validating the new count against the
94 * limit at each initialized level. In practice, most levels will not have a
95 * limit set. If there is a limit at any initialized level up the tree, the
96 * check must pass or the creation will fail. Likewise, when a filesystem or
97 * snapshot is destroyed, the counts are recursively adjusted all the way up
98 * the initizized nodes in the tree. Renaming a filesystem into different point
99 * in the tree will first validate, then update the counts on each branch up to
100 * the common ancestor. A receive will also validate the counts and then update
101 * them.
102 *
103 * An exception to the above behavior is that the limit is not enforced if the
104 * user has permission to modify the limit. This is primarily so that
105 * recursive snapshots in the global zone always work. We want to prevent a
106 * denial-of-service in which a lower level delegated dataset could max out its
107 * limit and thus block recursive snapshots from being taken in the global zone.
108 * Because of this, it is possible for the snapshot count to be over the limit
109 * and snapshots taken in the global zone could cause a lower level dataset to
110 * hit or exceed its limit. The administrator taking the global zone recursive
111 * snapshot should be aware of this side-effect and behave accordingly.
112 * For consistency, the filesystem limit is also not enforced if the user can
113 * modify the limit.
114 *
115 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
116 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
117 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
118 * dsl_dir_init_fs_ss_count().
119 *
120 * There is a special case when we receive a filesystem that already exists. In
121 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
122 * never update the filesystem counts for temporary clones.
123 *
124 * Likewise, we do not update the snapshot counts for temporary snapshots,
125 * such as those created by zfs diff.
126 */
34dc7c2f 127
d683ddbb
JG
128extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
129
34dc7c2f 130static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
34dc7c2f 131
a1d477c2
MA
132typedef struct ddulrt_arg {
133 dsl_dir_t *ddulrta_dd;
134 uint64_t ddlrta_txg;
135} ddulrt_arg_t;
136
34dc7c2f 137static void
39efbde7 138dsl_dir_evict_async(void *dbu)
34dc7c2f 139{
0c66c32d 140 dsl_dir_t *dd = dbu;
34dc7c2f 141 int t;
d1d7e268 142 ASSERTV(dsl_pool_t *dp = dd->dd_pool);
34dc7c2f 143
0c66c32d
JG
144 dd->dd_dbuf = NULL;
145
34dc7c2f
BB
146 for (t = 0; t < TXG_SIZE; t++) {
147 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
148 ASSERT(dd->dd_tempreserved[t] == 0);
149 ASSERT(dd->dd_space_towrite[t] == 0);
150 }
151
34dc7c2f 152 if (dd->dd_parent)
0c66c32d 153 dsl_dir_async_rele(dd->dd_parent, dd);
34dc7c2f 154
0c66c32d 155 spa_async_close(dd->dd_pool->dp_spa, dd);
34dc7c2f 156
0eb21616 157 dsl_prop_fini(dd);
34dc7c2f
BB
158 mutex_destroy(&dd->dd_lock);
159 kmem_free(dd, sizeof (dsl_dir_t));
160}
161
162int
13fe0198 163dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
34dc7c2f
BB
164 const char *tail, void *tag, dsl_dir_t **ddp)
165{
166 dmu_buf_t *dbuf;
167 dsl_dir_t *dd;
b5256303 168 dmu_object_info_t doi;
34dc7c2f
BB
169 int err;
170
13fe0198 171 ASSERT(dsl_pool_config_held(dp));
34dc7c2f
BB
172
173 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
13fe0198 174 if (err != 0)
34dc7c2f
BB
175 return (err);
176 dd = dmu_buf_get_user(dbuf);
b5256303
TC
177
178 dmu_object_info_from_db(dbuf, &doi);
179 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
180 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
181
34dc7c2f
BB
182 if (dd == NULL) {
183 dsl_dir_t *winner;
34dc7c2f 184
79c76d5b 185 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
34dc7c2f
BB
186 dd->dd_object = ddobj;
187 dd->dd_dbuf = dbuf;
188 dd->dd_pool = dp;
b5256303
TC
189
190 if (dsl_dir_is_zapified(dd) &&
191 zap_contains(dp->dp_meta_objset, ddobj,
192 DD_FIELD_CRYPTO_KEY_OBJ) == 0) {
193 VERIFY0(zap_lookup(dp->dp_meta_objset,
194 ddobj, DD_FIELD_CRYPTO_KEY_OBJ,
195 sizeof (uint64_t), 1, &dd->dd_crypto_obj));
ae76f45c
TC
196
197 /* check for on-disk format errata */
198 if (dsl_dir_incompatible_encryption_version(dd)) {
199 dp->dp_spa->spa_errata =
200 ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
201 }
b5256303
TC
202 }
203
34dc7c2f 204 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
0eb21616 205 dsl_prop_init(dd);
34dc7c2f 206
428870ff
BB
207 dsl_dir_snap_cmtime_update(dd);
208
d683ddbb
JG
209 if (dsl_dir_phys(dd)->dd_parent_obj) {
210 err = dsl_dir_hold_obj(dp,
211 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
212 &dd->dd_parent);
13fe0198 213 if (err != 0)
b128c09f 214 goto errout;
34dc7c2f
BB
215 if (tail) {
216#ifdef ZFS_DEBUG
217 uint64_t foundobj;
218
219 err = zap_lookup(dp->dp_meta_objset,
d683ddbb
JG
220 dsl_dir_phys(dd->dd_parent)->
221 dd_child_dir_zapobj, tail,
222 sizeof (foundobj), 1, &foundobj);
34dc7c2f
BB
223 ASSERT(err || foundobj == ddobj);
224#endif
680eada9 225 (void) strlcpy(dd->dd_myname, tail,
226 sizeof (dd->dd_myname));
34dc7c2f
BB
227 } else {
228 err = zap_value_search(dp->dp_meta_objset,
d683ddbb
JG
229 dsl_dir_phys(dd->dd_parent)->
230 dd_child_dir_zapobj,
34dc7c2f
BB
231 ddobj, 0, dd->dd_myname);
232 }
13fe0198 233 if (err != 0)
b128c09f 234 goto errout;
34dc7c2f
BB
235 } else {
236 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
237 }
238
428870ff
BB
239 if (dsl_dir_is_clone(dd)) {
240 dmu_buf_t *origin_bonus;
241 dsl_dataset_phys_t *origin_phys;
242
243 /*
244 * We can't open the origin dataset, because
245 * that would require opening this dsl_dir.
246 * Just look at its phys directly instead.
247 */
248 err = dmu_bonus_hold(dp->dp_meta_objset,
d683ddbb
JG
249 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
250 &origin_bonus);
13fe0198 251 if (err != 0)
428870ff
BB
252 goto errout;
253 origin_phys = origin_bonus->db_data;
254 dd->dd_origin_txg =
255 origin_phys->ds_creation_txg;
256 dmu_buf_rele(origin_bonus, FTAG);
257 }
258
39efbde7
GM
259 dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
260 &dd->dd_dbuf);
0c66c32d
JG
261 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
262 if (winner != NULL) {
34dc7c2f 263 if (dd->dd_parent)
13fe0198 264 dsl_dir_rele(dd->dd_parent, dd);
0eb21616 265 dsl_prop_fini(dd);
34dc7c2f
BB
266 mutex_destroy(&dd->dd_lock);
267 kmem_free(dd, sizeof (dsl_dir_t));
268 dd = winner;
269 } else {
270 spa_open_ref(dp->dp_spa, dd);
271 }
272 }
273
274 /*
275 * The dsl_dir_t has both open-to-close and instantiate-to-evict
276 * holds on the spa. We need the open-to-close holds because
277 * otherwise the spa_refcnt wouldn't change when we open a
278 * dir which the spa also has open, so we could incorrectly
279 * think it was OK to unload/export/destroy the pool. We need
280 * the instantiate-to-evict hold because the dsl_dir_t has a
281 * pointer to the dd_pool, which has a pointer to the spa_t.
282 */
283 spa_open_ref(dp->dp_spa, tag);
284 ASSERT3P(dd->dd_pool, ==, dp);
285 ASSERT3U(dd->dd_object, ==, ddobj);
286 ASSERT3P(dd->dd_dbuf, ==, dbuf);
287 *ddp = dd;
288 return (0);
b128c09f
BB
289
290errout:
291 if (dd->dd_parent)
13fe0198 292 dsl_dir_rele(dd->dd_parent, dd);
0eb21616 293 dsl_prop_fini(dd);
b128c09f
BB
294 mutex_destroy(&dd->dd_lock);
295 kmem_free(dd, sizeof (dsl_dir_t));
296 dmu_buf_rele(dbuf, tag);
297 return (err);
34dc7c2f
BB
298}
299
300void
13fe0198 301dsl_dir_rele(dsl_dir_t *dd, void *tag)
34dc7c2f
BB
302{
303 dprintf_dd(dd, "%s\n", "");
304 spa_close(dd->dd_pool->dp_spa, tag);
305 dmu_buf_rele(dd->dd_dbuf, tag);
306}
307
0c66c32d
JG
308/*
309 * Remove a reference to the given dsl dir that is being asynchronously
310 * released. Async releases occur from a taskq performing eviction of
311 * dsl datasets and dirs. This process is identical to a normal release
312 * with the exception of using the async API for releasing the reference on
313 * the spa.
314 */
315void
316dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
317{
318 dprintf_dd(dd, "%s\n", "");
319 spa_async_close(dd->dd_pool->dp_spa, tag);
320 dmu_buf_rele(dd->dd_dbuf, tag);
321}
322
eca7b760 323/* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
34dc7c2f
BB
324void
325dsl_dir_name(dsl_dir_t *dd, char *buf)
326{
327 if (dd->dd_parent) {
328 dsl_dir_name(dd->dd_parent, buf);
eca7b760
IK
329 VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
330 ZFS_MAX_DATASET_NAME_LEN);
34dc7c2f
BB
331 } else {
332 buf[0] = '\0';
333 }
334 if (!MUTEX_HELD(&dd->dd_lock)) {
335 /*
336 * recursive mutex so that we can use
337 * dprintf_dd() with dd_lock held
338 */
339 mutex_enter(&dd->dd_lock);
eca7b760
IK
340 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
341 <, ZFS_MAX_DATASET_NAME_LEN);
34dc7c2f
BB
342 mutex_exit(&dd->dd_lock);
343 } else {
eca7b760
IK
344 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
345 <, ZFS_MAX_DATASET_NAME_LEN);
34dc7c2f
BB
346 }
347}
348
29809a6c 349/* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
34dc7c2f
BB
350int
351dsl_dir_namelen(dsl_dir_t *dd)
352{
353 int result = 0;
354
355 if (dd->dd_parent) {
356 /* parent's name + 1 for the "/" */
357 result = dsl_dir_namelen(dd->dd_parent) + 1;
358 }
359
360 if (!MUTEX_HELD(&dd->dd_lock)) {
361 /* see dsl_dir_name */
362 mutex_enter(&dd->dd_lock);
363 result += strlen(dd->dd_myname);
364 mutex_exit(&dd->dd_lock);
365 } else {
366 result += strlen(dd->dd_myname);
367 }
368
369 return (result);
370}
371
34dc7c2f
BB
372static int
373getcomponent(const char *path, char *component, const char **nextp)
374{
375 char *p;
13fe0198 376
9babb374 377 if ((path == NULL) || (path[0] == '\0'))
2e528b49 378 return (SET_ERROR(ENOENT));
34dc7c2f
BB
379 /* This would be a good place to reserve some namespace... */
380 p = strpbrk(path, "/@");
381 if (p && (p[1] == '/' || p[1] == '@')) {
382 /* two separators in a row */
2e528b49 383 return (SET_ERROR(EINVAL));
34dc7c2f
BB
384 }
385 if (p == NULL || p == path) {
386 /*
387 * if the first thing is an @ or /, it had better be an
388 * @ and it had better not have any more ats or slashes,
389 * and it had better have something after the @.
390 */
391 if (p != NULL &&
392 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
2e528b49 393 return (SET_ERROR(EINVAL));
eca7b760 394 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
2e528b49 395 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
396 (void) strcpy(component, path);
397 p = NULL;
398 } else if (p[0] == '/') {
eca7b760 399 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
2e528b49 400 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f 401 (void) strncpy(component, path, p - path);
13fe0198 402 component[p - path] = '\0';
34dc7c2f
BB
403 p++;
404 } else if (p[0] == '@') {
405 /*
406 * if the next separator is an @, there better not be
407 * any more slashes.
408 */
409 if (strchr(path, '/'))
2e528b49 410 return (SET_ERROR(EINVAL));
eca7b760 411 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
2e528b49 412 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f 413 (void) strncpy(component, path, p - path);
13fe0198 414 component[p - path] = '\0';
34dc7c2f 415 } else {
13fe0198 416 panic("invalid p=%p", (void *)p);
34dc7c2f
BB
417 }
418 *nextp = p;
419 return (0);
420}
421
422/*
13fe0198
MA
423 * Return the dsl_dir_t, and possibly the last component which couldn't
424 * be found in *tail. The name must be in the specified dsl_pool_t. This
425 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
426 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
427 * (*tail)[0] == '@' means that the last component is a snapshot.
34dc7c2f
BB
428 */
429int
13fe0198 430dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
34dc7c2f
BB
431 dsl_dir_t **ddp, const char **tailp)
432{
fcf37ec6 433 char *buf;
13fe0198 434 const char *spaname, *next, *nextnext = NULL;
34dc7c2f
BB
435 int err;
436 dsl_dir_t *dd;
34dc7c2f 437 uint64_t ddobj;
34dc7c2f 438
eca7b760 439 buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
34dc7c2f 440 err = getcomponent(name, buf, &next);
13fe0198 441 if (err != 0)
fcf37ec6 442 goto error;
34dc7c2f 443
13fe0198
MA
444 /* Make sure the name is in the specified pool. */
445 spaname = spa_name(dp->dp_spa);
446 if (strcmp(buf, spaname) != 0) {
9063f654 447 err = SET_ERROR(EXDEV);
13fe0198 448 goto error;
34dc7c2f
BB
449 }
450
13fe0198 451 ASSERT(dsl_pool_config_held(dp));
34dc7c2f 452
13fe0198
MA
453 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
454 if (err != 0) {
fcf37ec6 455 goto error;
34dc7c2f
BB
456 }
457
458 while (next != NULL) {
0c66c32d 459 dsl_dir_t *child_dd;
34dc7c2f 460 err = getcomponent(next, buf, &nextnext);
13fe0198 461 if (err != 0)
34dc7c2f
BB
462 break;
463 ASSERT(next[0] != '\0');
464 if (next[0] == '@')
465 break;
466 dprintf("looking up %s in obj%lld\n",
d683ddbb 467 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
34dc7c2f
BB
468
469 err = zap_lookup(dp->dp_meta_objset,
d683ddbb 470 dsl_dir_phys(dd)->dd_child_dir_zapobj,
34dc7c2f 471 buf, sizeof (ddobj), 1, &ddobj);
13fe0198 472 if (err != 0) {
34dc7c2f
BB
473 if (err == ENOENT)
474 err = 0;
475 break;
476 }
477
0c66c32d 478 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
13fe0198 479 if (err != 0)
34dc7c2f 480 break;
13fe0198 481 dsl_dir_rele(dd, tag);
0c66c32d 482 dd = child_dd;
34dc7c2f
BB
483 next = nextnext;
484 }
34dc7c2f 485
13fe0198
MA
486 if (err != 0) {
487 dsl_dir_rele(dd, tag);
fcf37ec6 488 goto error;
34dc7c2f
BB
489 }
490
491 /*
492 * It's an error if there's more than one component left, or
493 * tailp==NULL and there's any component left.
494 */
495 if (next != NULL &&
496 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
497 /* bad path name */
13fe0198 498 dsl_dir_rele(dd, tag);
34dc7c2f 499 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
2e528b49 500 err = SET_ERROR(ENOENT);
34dc7c2f 501 }
13fe0198 502 if (tailp != NULL)
34dc7c2f 503 *tailp = next;
fc1ecd16
DB
504 if (err == 0)
505 *ddp = dd;
fcf37ec6 506error:
eca7b760 507 kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);
34dc7c2f
BB
508 return (err);
509}
510
788eb90c
JJ
511/*
512 * If the counts are already initialized for this filesystem and its
513 * descendants then do nothing, otherwise initialize the counts.
514 *
515 * The counts on this filesystem, and those below, may be uninitialized due to
516 * either the use of a pre-existing pool which did not support the
517 * filesystem/snapshot limit feature, or one in which the feature had not yet
518 * been enabled.
519 *
520 * Recursively descend the filesystem tree and update the filesystem/snapshot
521 * counts on each filesystem below, then update the cumulative count on the
522 * current filesystem. If the filesystem already has a count set on it,
523 * then we know that its counts, and the counts on the filesystems below it,
524 * are already correct, so we don't have to update this filesystem.
525 */
526static void
527dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
528{
529 uint64_t my_fs_cnt = 0;
530 uint64_t my_ss_cnt = 0;
531 dsl_pool_t *dp = dd->dd_pool;
532 objset_t *os = dp->dp_meta_objset;
533 zap_cursor_t *zc;
534 zap_attribute_t *za;
535 dsl_dataset_t *ds;
536
a0c9a17a 537 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
788eb90c
JJ
538 ASSERT(dsl_pool_config_held(dp));
539 ASSERT(dmu_tx_is_syncing(tx));
540
541 dsl_dir_zapify(dd, tx);
542
543 /*
544 * If the filesystem count has already been initialized then we
545 * don't need to recurse down any further.
546 */
547 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
548 return;
549
550 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
551 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
552
553 /* Iterate my child dirs */
d683ddbb 554 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
788eb90c
JJ
555 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
556 dsl_dir_t *chld_dd;
557 uint64_t count;
558
559 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
560 &chld_dd));
561
562 /*
563 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
564 * temporary datasets.
565 */
566 if (chld_dd->dd_myname[0] == '$' ||
567 chld_dd->dd_myname[0] == '%') {
568 dsl_dir_rele(chld_dd, FTAG);
569 continue;
570 }
571
572 my_fs_cnt++; /* count this child */
573
574 dsl_dir_init_fs_ss_count(chld_dd, tx);
575
576 VERIFY0(zap_lookup(os, chld_dd->dd_object,
577 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
578 my_fs_cnt += count;
579 VERIFY0(zap_lookup(os, chld_dd->dd_object,
580 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
581 my_ss_cnt += count;
582
583 dsl_dir_rele(chld_dd, FTAG);
584 }
585 zap_cursor_fini(zc);
586 /* Count my snapshots (we counted children's snapshots above) */
587 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
d683ddbb 588 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
788eb90c 589
d683ddbb 590 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
788eb90c
JJ
591 zap_cursor_retrieve(zc, za) == 0;
592 zap_cursor_advance(zc)) {
593 /* Don't count temporary snapshots */
594 if (za->za_name[0] != '%')
595 my_ss_cnt++;
596 }
ca227e54 597 zap_cursor_fini(zc);
788eb90c
JJ
598
599 dsl_dataset_rele(ds, FTAG);
600
601 kmem_free(zc, sizeof (zap_cursor_t));
602 kmem_free(za, sizeof (zap_attribute_t));
603
604 /* we're in a sync task, update counts */
605 dmu_buf_will_dirty(dd->dd_dbuf, tx);
606 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
607 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
608 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
609 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
610}
611
612static int
613dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
614{
615 char *ddname = (char *)arg;
616 dsl_pool_t *dp = dmu_tx_pool(tx);
617 dsl_dataset_t *ds;
618 dsl_dir_t *dd;
619 int error;
620
621 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
622 if (error != 0)
623 return (error);
624
625 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
626 dsl_dataset_rele(ds, FTAG);
627 return (SET_ERROR(ENOTSUP));
628 }
629
630 dd = ds->ds_dir;
631 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
632 dsl_dir_is_zapified(dd) &&
633 zap_contains(dp->dp_meta_objset, dd->dd_object,
634 DD_FIELD_FILESYSTEM_COUNT) == 0) {
635 dsl_dataset_rele(ds, FTAG);
636 return (SET_ERROR(EALREADY));
637 }
638
639 dsl_dataset_rele(ds, FTAG);
640 return (0);
641}
642
643static void
644dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
645{
646 char *ddname = (char *)arg;
647 dsl_pool_t *dp = dmu_tx_pool(tx);
648 dsl_dataset_t *ds;
649 spa_t *spa;
650
651 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
652
653 spa = dsl_dataset_get_spa(ds);
654
655 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
656 /*
657 * Since the feature was not active and we're now setting a
658 * limit, increment the feature-active counter so that the
659 * feature becomes active for the first time.
660 *
661 * We are already in a sync task so we can update the MOS.
662 */
663 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
664 }
665
666 /*
667 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
668 * we need to ensure the counts are correct. Descend down the tree from
669 * this point and update all of the counts to be accurate.
670 */
671 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
672
673 dsl_dataset_rele(ds, FTAG);
674}
675
676/*
677 * Make sure the feature is enabled and activate it if necessary.
678 * Since we're setting a limit, ensure the on-disk counts are valid.
679 * This is only called by the ioctl path when setting a limit value.
680 *
681 * We do not need to validate the new limit, since users who can change the
682 * limit are also allowed to exceed the limit.
683 */
684int
685dsl_dir_activate_fs_ss_limit(const char *ddname)
686{
687 int error;
688
689 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
3d45fdd6
MA
690 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
691 ZFS_SPACE_CHECK_RESERVED);
788eb90c
JJ
692
693 if (error == EALREADY)
694 error = 0;
695
696 return (error);
697}
698
699/*
700 * Used to determine if the filesystem_limit or snapshot_limit should be
701 * enforced. We allow the limit to be exceeded if the user has permission to
702 * write the property value. We pass in the creds that we got in the open
703 * context since we will always be the GZ root in syncing context. We also have
704 * to handle the case where we are allowed to change the limit on the current
705 * dataset, but there may be another limit in the tree above.
706 *
707 * We can never modify these two properties within a non-global zone. In
708 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
709 * can't use that function since we are already holding the dp_config_rwlock.
710 * In addition, we already have the dd and dealing with snapshots is simplified
711 * in this code.
712 */
713
714typedef enum {
715 ENFORCE_ALWAYS,
716 ENFORCE_NEVER,
717 ENFORCE_ABOVE
718} enforce_res_t;
719
720static enforce_res_t
721dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
722{
723 enforce_res_t enforce = ENFORCE_ALWAYS;
724 uint64_t obj;
725 dsl_dataset_t *ds;
726 uint64_t zoned;
727
728 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
729 prop == ZFS_PROP_SNAPSHOT_LIMIT);
730
731#ifdef _KERNEL
732 if (crgetzoneid(cr) != GLOBAL_ZONEID)
733 return (ENFORCE_ALWAYS);
734
735 if (secpolicy_zfs(cr) == 0)
736 return (ENFORCE_NEVER);
737#endif
738
d683ddbb 739 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
788eb90c
JJ
740 return (ENFORCE_ALWAYS);
741
742 ASSERT(dsl_pool_config_held(dd->dd_pool));
743
744 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
745 return (ENFORCE_ALWAYS);
746
747 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
748 /* Only root can access zoned fs's from the GZ */
749 enforce = ENFORCE_ALWAYS;
750 } else {
751 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
752 enforce = ENFORCE_ABOVE;
753 }
754
755 dsl_dataset_rele(ds, FTAG);
756 return (enforce);
757}
758
a1d477c2
MA
759static void
760dsl_dir_update_last_remap_txg_sync(void *varg, dmu_tx_t *tx)
761{
762 ddulrt_arg_t *arg = varg;
763 uint64_t last_remap_txg;
764 dsl_dir_t *dd = arg->ddulrta_dd;
765 objset_t *mos = dd->dd_pool->dp_meta_objset;
766
767 dsl_dir_zapify(dd, tx);
768 if (zap_lookup(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
769 sizeof (last_remap_txg), 1, &last_remap_txg) != 0 ||
770 last_remap_txg < arg->ddlrta_txg) {
771 VERIFY0(zap_update(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
772 sizeof (arg->ddlrta_txg), 1, &arg->ddlrta_txg, tx));
773 }
774}
775
776int
777dsl_dir_update_last_remap_txg(dsl_dir_t *dd, uint64_t txg)
778{
779 ddulrt_arg_t arg;
780 arg.ddulrta_dd = dd;
781 arg.ddlrta_txg = txg;
782
783 return (dsl_sync_task(spa_name(dd->dd_pool->dp_spa),
784 NULL, dsl_dir_update_last_remap_txg_sync, &arg,
785 1, ZFS_SPACE_CHECK_RESERVED));
786}
787
788eb90c
JJ
788/*
789 * Check if adding additional child filesystem(s) would exceed any filesystem
790 * limits or adding additional snapshot(s) would exceed any snapshot limits.
791 * The prop argument indicates which limit to check.
792 *
793 * Note that all filesystem limits up to the root (or the highest
794 * initialized) filesystem or the given ancestor must be satisfied.
795 */
796int
797dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
798 dsl_dir_t *ancestor, cred_t *cr)
799{
800 objset_t *os = dd->dd_pool->dp_meta_objset;
801 uint64_t limit, count;
802 char *count_prop;
803 enforce_res_t enforce;
804 int err = 0;
805
806 ASSERT(dsl_pool_config_held(dd->dd_pool));
807 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
808 prop == ZFS_PROP_SNAPSHOT_LIMIT);
809
810 /*
811 * If we're allowed to change the limit, don't enforce the limit
812 * e.g. this can happen if a snapshot is taken by an administrative
813 * user in the global zone (i.e. a recursive snapshot by root).
814 * However, we must handle the case of delegated permissions where we
815 * are allowed to change the limit on the current dataset, but there
816 * is another limit in the tree above.
817 */
818 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
819 if (enforce == ENFORCE_NEVER)
820 return (0);
821
822 /*
823 * e.g. if renaming a dataset with no snapshots, count adjustment
824 * is 0.
825 */
826 if (delta == 0)
827 return (0);
828
829 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
830 /*
831 * We don't enforce the limit for temporary snapshots. This is
832 * indicated by a NULL cred_t argument.
833 */
834 if (cr == NULL)
835 return (0);
836
837 count_prop = DD_FIELD_SNAPSHOT_COUNT;
838 } else {
839 count_prop = DD_FIELD_FILESYSTEM_COUNT;
840 }
841
842 /*
843 * If an ancestor has been provided, stop checking the limit once we
844 * hit that dir. We need this during rename so that we don't overcount
845 * the check once we recurse up to the common ancestor.
846 */
847 if (ancestor == dd)
848 return (0);
849
850 /*
851 * If we hit an uninitialized node while recursing up the tree, we can
852 * stop since we know there is no limit here (or above). The counts are
853 * not valid on this node and we know we won't touch this node's counts.
854 */
855 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
856 count_prop, sizeof (count), 1, &count) == ENOENT)
857 return (0);
858
859 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
860 B_FALSE);
861 if (err != 0)
862 return (err);
863
864 /* Is there a limit which we've hit? */
865 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
866 return (SET_ERROR(EDQUOT));
867
868 if (dd->dd_parent != NULL)
869 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
870 ancestor, cr);
871
872 return (err);
873}
874
875/*
876 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
877 * parents. When a new filesystem/snapshot is created, increment the count on
878 * all parents, and when a filesystem/snapshot is destroyed, decrement the
879 * count.
880 */
881void
882dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
883 dmu_tx_t *tx)
884{
885 int err;
886 objset_t *os = dd->dd_pool->dp_meta_objset;
887 uint64_t count;
888
889 ASSERT(dsl_pool_config_held(dd->dd_pool));
890 ASSERT(dmu_tx_is_syncing(tx));
891 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
892 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
893
894 /*
895 * When we receive an incremental stream into a filesystem that already
896 * exists, a temporary clone is created. We don't count this temporary
897 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
898 * $MOS & $ORIGIN) objsets.
899 */
900 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
901 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
902 return;
903
904 /*
905 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
906 */
907 if (delta == 0)
908 return;
909
910 /*
911 * If we hit an uninitialized node while recursing up the tree, we can
912 * stop since we know the counts are not valid on this node and we
913 * know we shouldn't touch this node's counts. An uninitialized count
914 * on the node indicates that either the feature has not yet been
915 * activated or there are no limits on this part of the tree.
916 */
917 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
918 prop, sizeof (count), 1, &count)) == ENOENT)
919 return;
920 VERIFY0(err);
921
922 count += delta;
923 /* Use a signed verify to make sure we're not neg. */
924 VERIFY3S(count, >=, 0);
925
926 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
927 tx));
928
929 /* Roll up this additional count into our ancestors */
930 if (dd->dd_parent != NULL)
931 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
932}
933
34dc7c2f 934uint64_t
b128c09f
BB
935dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
936 dmu_tx_t *tx)
34dc7c2f 937{
b128c09f 938 objset_t *mos = dp->dp_meta_objset;
34dc7c2f 939 uint64_t ddobj;
428870ff 940 dsl_dir_phys_t *ddphys;
34dc7c2f
BB
941 dmu_buf_t *dbuf;
942
943 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
944 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
b128c09f 945 if (pds) {
d2734cce 946 VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
b128c09f
BB
947 name, sizeof (uint64_t), 1, &ddobj, tx));
948 } else {
949 /* it's the root dir */
d2734cce 950 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
b128c09f
BB
951 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
952 }
d2734cce 953 VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
34dc7c2f 954 dmu_buf_will_dirty(dbuf, tx);
428870ff 955 ddphys = dbuf->db_data;
34dc7c2f 956
428870ff 957 ddphys->dd_creation_time = gethrestime_sec();
788eb90c 958 if (pds) {
428870ff 959 ddphys->dd_parent_obj = pds->dd_object;
788eb90c
JJ
960
961 /* update the filesystem counts */
962 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
963 }
428870ff 964 ddphys->dd_props_zapobj = zap_create(mos,
34dc7c2f 965 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
428870ff 966 ddphys->dd_child_dir_zapobj = zap_create(mos,
34dc7c2f 967 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
b128c09f 968 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
428870ff 969 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
b5256303 970
34dc7c2f
BB
971 dmu_buf_rele(dbuf, FTAG);
972
973 return (ddobj);
974}
975
b128c09f
BB
976boolean_t
977dsl_dir_is_clone(dsl_dir_t *dd)
34dc7c2f 978{
d683ddbb 979 return (dsl_dir_phys(dd)->dd_origin_obj &&
b128c09f 980 (dd->dd_pool->dp_origin_snap == NULL ||
d683ddbb 981 dsl_dir_phys(dd)->dd_origin_obj !=
b128c09f 982 dd->dd_pool->dp_origin_snap->ds_object));
34dc7c2f
BB
983}
984
d99a0153
CW
985uint64_t
986dsl_dir_get_used(dsl_dir_t *dd)
987{
988 return (dsl_dir_phys(dd)->dd_used_bytes);
989}
990
d2734cce
SD
991uint64_t
992dsl_dir_get_compressed(dsl_dir_t *dd)
993{
994 return (dsl_dir_phys(dd)->dd_compressed_bytes);
995}
996
d99a0153
CW
997uint64_t
998dsl_dir_get_quota(dsl_dir_t *dd)
999{
1000 return (dsl_dir_phys(dd)->dd_quota);
1001}
1002
1003uint64_t
1004dsl_dir_get_reservation(dsl_dir_t *dd)
1005{
1006 return (dsl_dir_phys(dd)->dd_reserved);
1007}
1008
1009uint64_t
1010dsl_dir_get_compressratio(dsl_dir_t *dd)
1011{
1012 /* a fixed point number, 100x the ratio */
1013 return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
1014 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
1015 dsl_dir_phys(dd)->dd_compressed_bytes));
1016}
1017
1018uint64_t
1019dsl_dir_get_logicalused(dsl_dir_t *dd)
1020{
1021 return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
1022}
1023
1024uint64_t
1025dsl_dir_get_usedsnap(dsl_dir_t *dd)
1026{
1027 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1028}
1029
1030uint64_t
1031dsl_dir_get_usedds(dsl_dir_t *dd)
1032{
1033 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1034}
1035
1036uint64_t
1037dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1038{
1039 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1040}
1041
1042uint64_t
1043dsl_dir_get_usedchild(dsl_dir_t *dd)
1044{
1045 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1046 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1047}
1048
34dc7c2f 1049void
d99a0153
CW
1050dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1051{
1052 dsl_dataset_t *ds;
1053 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1054 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1055
1056 dsl_dataset_name(ds, buf);
1057
1058 dsl_dataset_rele(ds, FTAG);
1059}
1060
1061int
1062dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
34dc7c2f 1063{
d99a0153
CW
1064 if (dsl_dir_is_zapified(dd)) {
1065 objset_t *os = dd->dd_pool->dp_meta_objset;
1066 return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1067 sizeof (*count), 1, count));
1068 } else {
1069 return (ENOENT);
1070 }
1071}
1072
1073int
1074dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
1075{
1076 if (dsl_dir_is_zapified(dd)) {
1077 objset_t *os = dd->dd_pool->dp_meta_objset;
1078 return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1079 sizeof (*count), 1, count));
1080 } else {
1081 return (ENOENT);
1082 }
1083}
b5256303 1084
a1d477c2
MA
1085int
1086dsl_dir_get_remaptxg(dsl_dir_t *dd, uint64_t *count)
1087{
1088 if (dsl_dir_is_zapified(dd)) {
1089 objset_t *os = dd->dd_pool->dp_meta_objset;
1090 return (zap_lookup(os, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1091 sizeof (*count), 1, count));
1092 } else {
1093 return (ENOENT);
1094 }
1095
1096}
1097
d99a0153
CW
1098void
1099dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1100{
34dc7c2f 1101 mutex_enter(&dd->dd_lock);
d683ddbb 1102 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
d99a0153 1103 dsl_dir_get_quota(dd));
34dc7c2f 1104 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
d99a0153 1105 dsl_dir_get_reservation(dd));
24a64651 1106 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
d99a0153 1107 dsl_dir_get_logicalused(dd));
d683ddbb 1108 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
b128c09f 1109 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
d99a0153 1110 dsl_dir_get_usedsnap(dd));
b128c09f 1111 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
d99a0153 1112 dsl_dir_get_usedds(dd));
b128c09f 1113 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
d99a0153 1114 dsl_dir_get_usedrefreserv(dd));
b128c09f 1115 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
d99a0153 1116 dsl_dir_get_usedchild(dd));
b128c09f 1117 }
34dc7c2f
BB
1118 mutex_exit(&dd->dd_lock);
1119
d99a0153
CW
1120 uint64_t count;
1121 if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1122 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1123 count);
1124 }
1125 if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1126 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1127 count);
788eb90c 1128 }
a1d477c2
MA
1129 if (dsl_dir_get_remaptxg(dd, &count) == 0) {
1130 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REMAPTXG,
1131 count);
1132 }
788eb90c 1133
b128c09f 1134 if (dsl_dir_is_clone(dd)) {
eca7b760 1135 char buf[ZFS_MAX_DATASET_NAME_LEN];
d99a0153 1136 dsl_dir_get_origin(dd, buf);
34dc7c2f
BB
1137 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1138 }
d99a0153 1139
34dc7c2f
BB
1140}
1141
1142void
1143dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1144{
1145 dsl_pool_t *dp = dd->dd_pool;
1146
d683ddbb 1147 ASSERT(dsl_dir_phys(dd));
34dc7c2f 1148
13fe0198 1149 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
34dc7c2f
BB
1150 /* up the hold count until we can be written out */
1151 dmu_buf_add_ref(dd->dd_dbuf, dd);
1152 }
1153}
1154
1155static int64_t
1156parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1157{
d683ddbb
JG
1158 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1159 uint64_t new_accounted =
1160 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
34dc7c2f
BB
1161 return (new_accounted - old_accounted);
1162}
1163
1164void
1165dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1166{
1167 ASSERT(dmu_tx_is_syncing(tx));
1168
34dc7c2f 1169 mutex_enter(&dd->dd_lock);
c99c9001 1170 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
34dc7c2f
BB
1171 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1172 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1173 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
34dc7c2f
BB
1174 mutex_exit(&dd->dd_lock);
1175
1176 /* release the hold from dsl_dir_dirty */
1177 dmu_buf_rele(dd->dd_dbuf, dd);
1178}
1179
1180static uint64_t
1181dsl_dir_space_towrite(dsl_dir_t *dd)
1182{
1183 uint64_t space = 0;
34dc7c2f
BB
1184
1185 ASSERT(MUTEX_HELD(&dd->dd_lock));
1186
3ec3bc21
BB
1187 for (int i = 0; i < TXG_SIZE; i++) {
1188 space += dd->dd_space_towrite[i & TXG_MASK];
1189 ASSERT3U(dd->dd_space_towrite[i & TXG_MASK], >=, 0);
34dc7c2f
BB
1190 }
1191 return (space);
1192}
1193
1194/*
1195 * How much space would dd have available if ancestor had delta applied
1196 * to it? If ondiskonly is set, we're only interested in what's
1197 * on-disk, not estimated pending changes.
1198 */
1199uint64_t
1200dsl_dir_space_available(dsl_dir_t *dd,
1201 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1202{
1203 uint64_t parentspace, myspace, quota, used;
1204
1205 /*
1206 * If there are no restrictions otherwise, assume we have
1207 * unlimited space available.
1208 */
1209 quota = UINT64_MAX;
1210 parentspace = UINT64_MAX;
1211
1212 if (dd->dd_parent != NULL) {
1213 parentspace = dsl_dir_space_available(dd->dd_parent,
1214 ancestor, delta, ondiskonly);
1215 }
1216
1217 mutex_enter(&dd->dd_lock);
d683ddbb
JG
1218 if (dsl_dir_phys(dd)->dd_quota != 0)
1219 quota = dsl_dir_phys(dd)->dd_quota;
1220 used = dsl_dir_phys(dd)->dd_used_bytes;
34dc7c2f
BB
1221 if (!ondiskonly)
1222 used += dsl_dir_space_towrite(dd);
34dc7c2f
BB
1223
1224 if (dd->dd_parent == NULL) {
d2734cce
SD
1225 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,
1226 ZFS_SPACE_CHECK_NORMAL);
34dc7c2f
BB
1227 quota = MIN(quota, poolsize);
1228 }
1229
d683ddbb 1230 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
34dc7c2f
BB
1231 /*
1232 * We have some space reserved, in addition to what our
1233 * parent gave us.
1234 */
d683ddbb 1235 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
34dc7c2f
BB
1236 }
1237
b128c09f
BB
1238 if (dd == ancestor) {
1239 ASSERT(delta <= 0);
1240 ASSERT(used >= -delta);
1241 used += delta;
1242 if (parentspace != UINT64_MAX)
1243 parentspace -= delta;
1244 }
1245
34dc7c2f
BB
1246 if (used > quota) {
1247 /* over quota */
1248 myspace = 0;
34dc7c2f
BB
1249 } else {
1250 /*
1251 * the lesser of the space provided by our parent and
1252 * the space left in our quota
1253 */
1254 myspace = MIN(parentspace, quota - used);
1255 }
1256
1257 mutex_exit(&dd->dd_lock);
1258
1259 return (myspace);
1260}
1261
1262struct tempreserve {
1263 list_node_t tr_node;
34dc7c2f
BB
1264 dsl_dir_t *tr_ds;
1265 uint64_t tr_size;
1266};
1267
1268static int
1269dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
3ec3bc21 1270 boolean_t ignorequota, list_t *tr_list,
34dc7c2f
BB
1271 dmu_tx_t *tx, boolean_t first)
1272{
419c80e6 1273 uint64_t txg;
3ec3bc21 1274 uint64_t quota;
34dc7c2f 1275 struct tempreserve *tr;
419c80e6
D
1276 int retval;
1277 uint64_t ref_rsrv;
1278
1279top_of_function:
1280 txg = tx->tx_txg;
1281 retval = EDQUOT;
1282 ref_rsrv = 0;
34dc7c2f
BB
1283
1284 ASSERT3U(txg, !=, 0);
1285 ASSERT3S(asize, >, 0);
1286
1287 mutex_enter(&dd->dd_lock);
1288
1289 /*
1290 * Check against the dsl_dir's quota. We don't add in the delta
1291 * when checking for over-quota because they get one free hit.
1292 */
3ec3bc21
BB
1293 uint64_t est_inflight = dsl_dir_space_towrite(dd);
1294 for (int i = 0; i < TXG_SIZE; i++)
34dc7c2f 1295 est_inflight += dd->dd_tempreserved[i];
3ec3bc21 1296 uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
34dc7c2f
BB
1297
1298 /*
1299 * On the first iteration, fetch the dataset's used-on-disk and
1300 * refreservation values. Also, if checkrefquota is set, test if
1301 * allocating this space would exceed the dataset's refquota.
1302 */
1303 if (first && tx->tx_objset) {
1304 int error;
428870ff 1305 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
34dc7c2f 1306
3ec3bc21 1307 error = dsl_dataset_check_quota(ds, !netfree,
34dc7c2f 1308 asize, est_inflight, &used_on_disk, &ref_rsrv);
3ec3bc21 1309 if (error != 0) {
34dc7c2f 1310 mutex_exit(&dd->dd_lock);
3d920a15 1311 DMU_TX_STAT_BUMP(dmu_tx_quota);
34dc7c2f
BB
1312 return (error);
1313 }
1314 }
1315
1316 /*
1317 * If this transaction will result in a net free of space,
1318 * we want to let it through.
1319 */
d683ddbb 1320 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
34dc7c2f
BB
1321 quota = UINT64_MAX;
1322 else
d683ddbb 1323 quota = dsl_dir_phys(dd)->dd_quota;
34dc7c2f
BB
1324
1325 /*
428870ff
BB
1326 * Adjust the quota against the actual pool size at the root
1327 * minus any outstanding deferred frees.
34dc7c2f
BB
1328 * To ensure that it's possible to remove files from a full
1329 * pool without inducing transient overcommits, we throttle
1330 * netfree transactions against a quota that is slightly larger,
1331 * but still within the pool's allocation slop. In cases where
1332 * we're very close to full, this will allow a steady trickle of
1333 * removes to get through.
1334 */
3ec3bc21 1335 uint64_t deferred = 0;
34dc7c2f 1336 if (dd->dd_parent == NULL) {
d2734cce
SD
1337 uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,
1338 (netfree) ?
1339 ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);
1340
1341 if (avail < quota) {
1342 quota = avail;
428870ff 1343 retval = ENOSPC;
34dc7c2f
BB
1344 }
1345 }
1346
1347 /*
1348 * If they are requesting more space, and our current estimate
1349 * is over quota, they get to try again unless the actual
1350 * on-disk is over quota and there are no pending changes (which
1351 * may free up space for us).
1352 */
428870ff
BB
1353 if (used_on_disk + est_inflight >= quota) {
1354 if (est_inflight > 0 || used_on_disk < quota ||
1355 (retval == ENOSPC && used_on_disk < quota + deferred))
1356 retval = ERESTART;
34dc7c2f
BB
1357 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1358 "quota=%lluK tr=%lluK err=%d\n",
1359 used_on_disk>>10, est_inflight>>10,
428870ff 1360 quota>>10, asize>>10, retval);
34dc7c2f 1361 mutex_exit(&dd->dd_lock);
3d920a15 1362 DMU_TX_STAT_BUMP(dmu_tx_quota);
2e528b49 1363 return (SET_ERROR(retval));
34dc7c2f
BB
1364 }
1365
1366 /* We need to up our estimated delta before dropping dd_lock */
3ec3bc21 1367 dd->dd_tempreserved[txg & TXG_MASK] += asize;
34dc7c2f 1368
3ec3bc21 1369 uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
34dc7c2f
BB
1370 asize - ref_rsrv);
1371 mutex_exit(&dd->dd_lock);
1372
79c76d5b 1373 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
34dc7c2f
BB
1374 tr->tr_ds = dd;
1375 tr->tr_size = asize;
1376 list_insert_tail(tr_list, tr);
1377
1378 /* see if it's OK with our parent */
3ec3bc21 1379 if (dd->dd_parent != NULL && parent_rsrv != 0) {
419c80e6
D
1380 /*
1381 * Recurse on our parent without recursion. This has been
1382 * observed to be potentially large stack usage even within
1383 * the test suite. Largest seen stack was 7632 bytes on linux.
1384 */
1385
1386 dd = dd->dd_parent;
1387 asize = parent_rsrv;
1388 ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1389 first = B_FALSE;
1390 goto top_of_function;
34dc7c2f 1391
34dc7c2f
BB
1392 } else {
1393 return (0);
1394 }
1395}
1396
1397/*
1398 * Reserve space in this dsl_dir, to be used in this tx's txg.
1399 * After the space has been dirtied (and dsl_dir_willuse_space()
1400 * has been called), the reservation should be canceled, using
1401 * dsl_dir_tempreserve_clear().
1402 */
1403int
1404dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
3ec3bc21 1405 boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
34dc7c2f
BB
1406{
1407 int err;
1408 list_t *tr_list;
1409
1410 if (asize == 0) {
1411 *tr_cookiep = NULL;
1412 return (0);
1413 }
1414
79c76d5b 1415 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
34dc7c2f
BB
1416 list_create(tr_list, sizeof (struct tempreserve),
1417 offsetof(struct tempreserve, tr_node));
1418 ASSERT3S(asize, >, 0);
34dc7c2f 1419
dae3e9ea 1420 err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg);
34dc7c2f
BB
1421 if (err == 0) {
1422 struct tempreserve *tr;
1423
79c76d5b 1424 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
34dc7c2f
BB
1425 tr->tr_size = lsize;
1426 list_insert_tail(tr_list, tr);
34dc7c2f
BB
1427 } else {
1428 if (err == EAGAIN) {
e8b96c60
MA
1429 /*
1430 * If arc_memory_throttle() detected that pageout
1431 * is running and we are low on memory, we delay new
1432 * non-pageout transactions to give pageout an
1433 * advantage.
1434 *
1435 * It is unfortunate to be delaying while the caller's
1436 * locks are held.
1437 */
63fd3c6c
AL
1438 txg_delay(dd->dd_pool, tx->tx_txg,
1439 MSEC2NSEC(10), MSEC2NSEC(10));
2e528b49 1440 err = SET_ERROR(ERESTART);
34dc7c2f 1441 }
34dc7c2f
BB
1442 }
1443
1444 if (err == 0) {
3ec3bc21
BB
1445 err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1446 B_FALSE, tr_list, tx, B_TRUE);
34dc7c2f
BB
1447 }
1448
13fe0198 1449 if (err != 0)
34dc7c2f
BB
1450 dsl_dir_tempreserve_clear(tr_list, tx);
1451 else
1452 *tr_cookiep = tr_list;
1453
1454 return (err);
1455}
1456
1457/*
1458 * Clear a temporary reservation that we previously made with
1459 * dsl_dir_tempreserve_space().
1460 */
1461void
1462dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1463{
1464 int txgidx = tx->tx_txg & TXG_MASK;
1465 list_t *tr_list = tr_cookie;
1466 struct tempreserve *tr;
1467
1468 ASSERT3U(tx->tx_txg, !=, 0);
1469
1470 if (tr_cookie == NULL)
1471 return;
1472
e8b96c60
MA
1473 while ((tr = list_head(tr_list)) != NULL) {
1474 if (tr->tr_ds) {
34dc7c2f
BB
1475 mutex_enter(&tr->tr_ds->dd_lock);
1476 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1477 tr->tr_size);
1478 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1479 mutex_exit(&tr->tr_ds->dd_lock);
1480 } else {
1481 arc_tempreserve_clear(tr->tr_size);
1482 }
1483 list_remove(tr_list, tr);
1484 kmem_free(tr, sizeof (struct tempreserve));
1485 }
1486
1487 kmem_free(tr_list, sizeof (list_t));
1488}
1489
e8b96c60
MA
1490/*
1491 * This should be called from open context when we think we're going to write
1492 * or free space, for example when dirtying data. Be conservative; it's okay
1493 * to write less space or free more, but we don't want to write more or free
1494 * less than the amount specified.
1ba16159
AB
1495 *
1496 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1497 * version however it has been adjusted to use an iterative rather then
1498 * recursive algorithm to minimize stack usage.
e8b96c60
MA
1499 */
1500void
1501dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
34dc7c2f
BB
1502{
1503 int64_t parent_space;
1504 uint64_t est_used;
1505
1ba16159
AB
1506 do {
1507 mutex_enter(&dd->dd_lock);
1508 if (space > 0)
1509 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
34dc7c2f 1510
1ba16159 1511 est_used = dsl_dir_space_towrite(dd) +
d683ddbb 1512 dsl_dir_phys(dd)->dd_used_bytes;
1ba16159
AB
1513 parent_space = parent_delta(dd, est_used, space);
1514 mutex_exit(&dd->dd_lock);
34dc7c2f 1515
1ba16159
AB
1516 /* Make sure that we clean up dd_space_to* */
1517 dsl_dir_dirty(dd, tx);
34dc7c2f 1518
1ba16159
AB
1519 dd = dd->dd_parent;
1520 space = parent_space;
1521 } while (space && dd);
34dc7c2f
BB
1522}
1523
1524/* call from syncing context when we actually write/free space for this dd */
1525void
b128c09f 1526dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
34dc7c2f
BB
1527 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1528{
1529 int64_t accounted_delta;
a169a625
MA
1530
1531 /*
1532 * dsl_dataset_set_refreservation_sync_impl() calls this with
1533 * dd_lock held, so that it can atomically update
1534 * ds->ds_reserved and the dsl_dir accounting, so that
1535 * dsl_dataset_check_quota() can see dataset and dir accounting
1536 * consistently.
1537 */
b128c09f 1538 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
34dc7c2f
BB
1539
1540 ASSERT(dmu_tx_is_syncing(tx));
b128c09f 1541 ASSERT(type < DD_USED_NUM);
34dc7c2f 1542
a169a625
MA
1543 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1544
b128c09f
BB
1545 if (needlock)
1546 mutex_enter(&dd->dd_lock);
d683ddbb
JG
1547 accounted_delta =
1548 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1549 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
34dc7c2f 1550 ASSERT(compressed >= 0 ||
d683ddbb 1551 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
34dc7c2f 1552 ASSERT(uncompressed >= 0 ||
d683ddbb
JG
1553 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1554 dsl_dir_phys(dd)->dd_used_bytes += used;
1555 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1556 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
b128c09f 1557
d683ddbb 1558 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
b128c09f 1559 ASSERT(used > 0 ||
d683ddbb
JG
1560 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1561 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
b128c09f 1562#ifdef DEBUG
d6320ddb
BB
1563 {
1564 dd_used_t t;
1565 uint64_t u = 0;
1566 for (t = 0; t < DD_USED_NUM; t++)
d683ddbb
JG
1567 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1568 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
d6320ddb 1569 }
b128c09f
BB
1570#endif
1571 }
1572 if (needlock)
1573 mutex_exit(&dd->dd_lock);
34dc7c2f
BB
1574
1575 if (dd->dd_parent != NULL) {
b128c09f 1576 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
34dc7c2f 1577 accounted_delta, compressed, uncompressed, tx);
b128c09f
BB
1578 dsl_dir_transfer_space(dd->dd_parent,
1579 used - accounted_delta,
1580 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
34dc7c2f
BB
1581 }
1582}
1583
b128c09f
BB
1584void
1585dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1586 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1587{
b128c09f
BB
1588 ASSERT(dmu_tx_is_syncing(tx));
1589 ASSERT(oldtype < DD_USED_NUM);
1590 ASSERT(newtype < DD_USED_NUM);
1591
d683ddbb
JG
1592 if (delta == 0 ||
1593 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
b128c09f
BB
1594 return;
1595
a169a625
MA
1596 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1597 mutex_enter(&dd->dd_lock);
b128c09f 1598 ASSERT(delta > 0 ?
d683ddbb
JG
1599 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1600 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1601 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1602 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1603 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
a169a625 1604 mutex_exit(&dd->dd_lock);
b128c09f
BB
1605}
1606
13fe0198
MA
1607typedef struct dsl_dir_set_qr_arg {
1608 const char *ddsqra_name;
1609 zprop_source_t ddsqra_source;
1610 uint64_t ddsqra_value;
1611} dsl_dir_set_qr_arg_t;
1612
34dc7c2f 1613static int
13fe0198 1614dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1615{
13fe0198
MA
1616 dsl_dir_set_qr_arg_t *ddsqra = arg;
1617 dsl_pool_t *dp = dmu_tx_pool(tx);
1618 dsl_dataset_t *ds;
1619 int error;
1620 uint64_t towrite, newval;
34dc7c2f 1621
13fe0198
MA
1622 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1623 if (error != 0)
1624 return (error);
1625
1626 error = dsl_prop_predict(ds->ds_dir, "quota",
1627 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1628 if (error != 0) {
1629 dsl_dataset_rele(ds, FTAG);
1630 return (error);
1631 }
428870ff 1632
13fe0198
MA
1633 if (newval == 0) {
1634 dsl_dataset_rele(ds, FTAG);
34dc7c2f 1635 return (0);
13fe0198 1636 }
34dc7c2f 1637
13fe0198 1638 mutex_enter(&ds->ds_dir->dd_lock);
34dc7c2f
BB
1639 /*
1640 * If we are doing the preliminary check in open context, and
1641 * there are pending changes, then don't fail it, since the
1642 * pending changes could under-estimate the amount of space to be
1643 * freed up.
1644 */
13fe0198 1645 towrite = dsl_dir_space_towrite(ds->ds_dir);
34dc7c2f 1646 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
d683ddbb
JG
1647 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1648 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
2e528b49 1649 error = SET_ERROR(ENOSPC);
34dc7c2f 1650 }
13fe0198
MA
1651 mutex_exit(&ds->ds_dir->dd_lock);
1652 dsl_dataset_rele(ds, FTAG);
1653 return (error);
34dc7c2f
BB
1654}
1655
34dc7c2f 1656static void
13fe0198 1657dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1658{
13fe0198
MA
1659 dsl_dir_set_qr_arg_t *ddsqra = arg;
1660 dsl_pool_t *dp = dmu_tx_pool(tx);
1661 dsl_dataset_t *ds;
1662 uint64_t newval;
428870ff 1663
13fe0198 1664 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
34dc7c2f 1665
b1118acb
MM
1666 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1667 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1668 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1669 &ddsqra->ddsqra_value, tx);
34dc7c2f 1670
b1118acb
MM
1671 VERIFY0(dsl_prop_get_int_ds(ds,
1672 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1673 } else {
1674 newval = ddsqra->ddsqra_value;
1675 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1676 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1677 }
6f1ffb06 1678
13fe0198
MA
1679 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1680 mutex_enter(&ds->ds_dir->dd_lock);
d683ddbb 1681 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
13fe0198
MA
1682 mutex_exit(&ds->ds_dir->dd_lock);
1683 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
1684}
1685
1686int
428870ff 1687dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
34dc7c2f 1688{
13fe0198 1689 dsl_dir_set_qr_arg_t ddsqra;
428870ff 1690
13fe0198
MA
1691 ddsqra.ddsqra_name = ddname;
1692 ddsqra.ddsqra_source = source;
1693 ddsqra.ddsqra_value = quota;
428870ff 1694
13fe0198 1695 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
d2734cce
SD
1696 dsl_dir_set_quota_sync, &ddsqra, 0,
1697 ZFS_SPACE_CHECK_EXTRA_RESERVED));
34dc7c2f
BB
1698}
1699
1700int
13fe0198 1701dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1702{
13fe0198
MA
1703 dsl_dir_set_qr_arg_t *ddsqra = arg;
1704 dsl_pool_t *dp = dmu_tx_pool(tx);
1705 dsl_dataset_t *ds;
1706 dsl_dir_t *dd;
1707 uint64_t newval, used, avail;
1708 int error;
428870ff 1709
13fe0198
MA
1710 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1711 if (error != 0)
1712 return (error);
1713 dd = ds->ds_dir;
34dc7c2f
BB
1714
1715 /*
1716 * If we are doing the preliminary check in open context, the
1717 * space estimates may be inaccurate.
1718 */
13fe0198
MA
1719 if (!dmu_tx_is_syncing(tx)) {
1720 dsl_dataset_rele(ds, FTAG);
34dc7c2f 1721 return (0);
13fe0198
MA
1722 }
1723
1724 error = dsl_prop_predict(ds->ds_dir,
1725 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1726 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1727 if (error != 0) {
1728 dsl_dataset_rele(ds, FTAG);
1729 return (error);
1730 }
34dc7c2f
BB
1731
1732 mutex_enter(&dd->dd_lock);
d683ddbb 1733 used = dsl_dir_phys(dd)->dd_used_bytes;
34dc7c2f
BB
1734 mutex_exit(&dd->dd_lock);
1735
1736 if (dd->dd_parent) {
1737 avail = dsl_dir_space_available(dd->dd_parent,
1738 NULL, 0, FALSE);
1739 } else {
d2734cce
SD
1740 avail = dsl_pool_adjustedsize(dd->dd_pool,
1741 ZFS_SPACE_CHECK_NORMAL) - used;
34dc7c2f
BB
1742 }
1743
d683ddbb 1744 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
13fe0198 1745 uint64_t delta = MAX(used, newval) -
d683ddbb 1746 MAX(used, dsl_dir_phys(dd)->dd_reserved);
d164b209 1747
13fe0198 1748 if (delta > avail ||
d683ddbb
JG
1749 (dsl_dir_phys(dd)->dd_quota > 0 &&
1750 newval > dsl_dir_phys(dd)->dd_quota))
2e528b49 1751 error = SET_ERROR(ENOSPC);
d164b209
BB
1752 }
1753
13fe0198
MA
1754 dsl_dataset_rele(ds, FTAG);
1755 return (error);
34dc7c2f
BB
1756}
1757
13fe0198 1758void
6f1ffb06 1759dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
34dc7c2f 1760{
34dc7c2f
BB
1761 uint64_t used;
1762 int64_t delta;
1763
1764 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1765
1766 mutex_enter(&dd->dd_lock);
d683ddbb
JG
1767 used = dsl_dir_phys(dd)->dd_used_bytes;
1768 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1769 dsl_dir_phys(dd)->dd_reserved = value;
34dc7c2f
BB
1770
1771 if (dd->dd_parent != NULL) {
1772 /* Roll up this additional usage into our ancestors */
b128c09f
BB
1773 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1774 delta, 0, 0, tx);
34dc7c2f 1775 }
b128c09f 1776 mutex_exit(&dd->dd_lock);
34dc7c2f
BB
1777}
1778
6f1ffb06 1779static void
13fe0198 1780dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
6f1ffb06 1781{
13fe0198
MA
1782 dsl_dir_set_qr_arg_t *ddsqra = arg;
1783 dsl_pool_t *dp = dmu_tx_pool(tx);
1784 dsl_dataset_t *ds;
1785 uint64_t newval;
6f1ffb06 1786
13fe0198
MA
1787 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1788
b1118acb
MM
1789 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1790 dsl_prop_set_sync_impl(ds,
1791 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1792 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1793 &ddsqra->ddsqra_value, tx);
d1d7e268 1794
b1118acb
MM
1795 VERIFY0(dsl_prop_get_int_ds(ds,
1796 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1797 } else {
1798 newval = ddsqra->ddsqra_value;
1799 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1800 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1801 (longlong_t)newval);
1802 }
1803
13fe0198
MA
1804 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1805 dsl_dataset_rele(ds, FTAG);
d1d7e268 1806}
6f1ffb06 1807
34dc7c2f 1808int
428870ff
BB
1809dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1810 uint64_t reservation)
34dc7c2f 1811{
13fe0198 1812 dsl_dir_set_qr_arg_t ddsqra;
428870ff 1813
13fe0198
MA
1814 ddsqra.ddsqra_name = ddname;
1815 ddsqra.ddsqra_source = source;
1816 ddsqra.ddsqra_value = reservation;
428870ff 1817
13fe0198 1818 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
d2734cce
SD
1819 dsl_dir_set_reservation_sync, &ddsqra, 0,
1820 ZFS_SPACE_CHECK_EXTRA_RESERVED));
34dc7c2f
BB
1821}
1822
1823static dsl_dir_t *
1824closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1825{
1826 for (; ds1; ds1 = ds1->dd_parent) {
1827 dsl_dir_t *dd;
1828 for (dd = ds2; dd; dd = dd->dd_parent) {
1829 if (ds1 == dd)
1830 return (dd);
1831 }
1832 }
1833 return (NULL);
1834}
1835
1836/*
1837 * If delta is applied to dd, how much of that delta would be applied to
1838 * ancestor? Syncing context only.
1839 */
1840static int64_t
1841would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1842{
1843 if (dd == ancestor)
1844 return (delta);
1845
1846 mutex_enter(&dd->dd_lock);
d683ddbb 1847 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
34dc7c2f
BB
1848 mutex_exit(&dd->dd_lock);
1849 return (would_change(dd->dd_parent, delta, ancestor));
1850}
1851
13fe0198
MA
1852typedef struct dsl_dir_rename_arg {
1853 const char *ddra_oldname;
1854 const char *ddra_newname;
788eb90c 1855 cred_t *ddra_cred;
13fe0198 1856} dsl_dir_rename_arg_t;
34dc7c2f 1857
a7ed98d8
SD
1858typedef struct dsl_valid_rename_arg {
1859 int char_delta;
1860 int nest_delta;
1861} dsl_valid_rename_arg_t;
1862
13fe0198 1863/* ARGSUSED */
34dc7c2f 1864static int
13fe0198 1865dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
34dc7c2f 1866{
a7ed98d8 1867 dsl_valid_rename_arg_t *dvra = arg;
eca7b760 1868 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 1869
13fe0198
MA
1870 dsl_dataset_name(ds, namebuf);
1871
a7ed98d8
SD
1872 ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN),
1873 <, ZFS_MAX_DATASET_NAME_LEN);
1874 int namelen = strlen(namebuf) + dvra->char_delta;
1875 int depth = get_dataset_depth(namebuf) + dvra->nest_delta;
1876
1877 if (namelen >= ZFS_MAX_DATASET_NAME_LEN)
1878 return (SET_ERROR(ENAMETOOLONG));
1879 if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting)
2e528b49 1880 return (SET_ERROR(ENAMETOOLONG));
13fe0198
MA
1881 return (0);
1882}
1883
1884static int
1885dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1886{
1887 dsl_dir_rename_arg_t *ddra = arg;
1888 dsl_pool_t *dp = dmu_tx_pool(tx);
1889 dsl_dir_t *dd, *newparent;
a7ed98d8 1890 dsl_valid_rename_arg_t dvra;
13fe0198
MA
1891 const char *mynewname;
1892 int error;
34dc7c2f 1893
13fe0198
MA
1894 /* target dir should exist */
1895 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1896 if (error != 0)
1897 return (error);
1898
1899 /* new parent should exist */
1900 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1901 &newparent, &mynewname);
1902 if (error != 0) {
1903 dsl_dir_rele(dd, FTAG);
1904 return (error);
1905 }
1906
1907 /* can't rename to different pool */
1908 if (dd->dd_pool != newparent->dd_pool) {
1909 dsl_dir_rele(newparent, FTAG);
1910 dsl_dir_rele(dd, FTAG);
9063f654 1911 return (SET_ERROR(EXDEV));
13fe0198
MA
1912 }
1913
1914 /* new name should not already exist */
1915 if (mynewname == NULL) {
1916 dsl_dir_rele(newparent, FTAG);
1917 dsl_dir_rele(dd, FTAG);
2e528b49 1918 return (SET_ERROR(EEXIST));
13fe0198
MA
1919 }
1920
a7ed98d8
SD
1921 ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN),
1922 <, ZFS_MAX_DATASET_NAME_LEN);
1923 ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN),
1924 <, ZFS_MAX_DATASET_NAME_LEN);
1925 dvra.char_delta = strlen(ddra->ddra_newname)
1926 - strlen(ddra->ddra_oldname);
1927 dvra.nest_delta = get_dataset_depth(ddra->ddra_newname)
1928 - get_dataset_depth(ddra->ddra_oldname);
1929
13fe0198 1930 /* if the name length is growing, validate child name lengths */
a7ed98d8 1931 if (dvra.char_delta > 0 || dvra.nest_delta > 0) {
13fe0198 1932 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
a7ed98d8 1933 &dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
13fe0198
MA
1934 if (error != 0) {
1935 dsl_dir_rele(newparent, FTAG);
1936 dsl_dir_rele(dd, FTAG);
1937 return (error);
1938 }
1939 }
34dc7c2f 1940
788eb90c 1941 if (dmu_tx_is_syncing(tx)) {
a0c9a17a 1942 if (spa_feature_is_active(dp->dp_spa,
788eb90c
JJ
1943 SPA_FEATURE_FS_SS_LIMIT)) {
1944 /*
1945 * Although this is the check function and we don't
1946 * normally make on-disk changes in check functions,
1947 * we need to do that here.
1948 *
1949 * Ensure this portion of the tree's counts have been
1950 * initialized in case the new parent has limits set.
1951 */
1952 dsl_dir_init_fs_ss_count(dd, tx);
1953 }
1954 }
1955
13fe0198 1956 if (newparent != dd->dd_parent) {
34dc7c2f
BB
1957 /* is there enough space? */
1958 uint64_t myspace =
d683ddbb
JG
1959 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1960 dsl_dir_phys(dd)->dd_reserved);
788eb90c
JJ
1961 objset_t *os = dd->dd_pool->dp_meta_objset;
1962 uint64_t fs_cnt = 0;
1963 uint64_t ss_cnt = 0;
1964
1965 if (dsl_dir_is_zapified(dd)) {
1966 int err;
1967
1968 err = zap_lookup(os, dd->dd_object,
1969 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1970 &fs_cnt);
a0c9a17a
JJ
1971 if (err != ENOENT && err != 0) {
1972 dsl_dir_rele(newparent, FTAG);
1973 dsl_dir_rele(dd, FTAG);
788eb90c 1974 return (err);
a0c9a17a 1975 }
788eb90c
JJ
1976
1977 /*
1978 * have to add 1 for the filesystem itself that we're
1979 * moving
1980 */
1981 fs_cnt++;
1982
1983 err = zap_lookup(os, dd->dd_object,
1984 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1985 &ss_cnt);
a0c9a17a
JJ
1986 if (err != ENOENT && err != 0) {
1987 dsl_dir_rele(newparent, FTAG);
1988 dsl_dir_rele(dd, FTAG);
788eb90c 1989 return (err);
a0c9a17a 1990 }
788eb90c 1991 }
34dc7c2f 1992
b5256303
TC
1993 /* check for encryption errors */
1994 error = dsl_dir_rename_crypt_check(dd, newparent);
1995 if (error != 0) {
1996 dsl_dir_rele(newparent, FTAG);
1997 dsl_dir_rele(dd, FTAG);
1998 return (SET_ERROR(EACCES));
1999 }
2000
34dc7c2f 2001 /* no rename into our descendant */
13fe0198
MA
2002 if (closest_common_ancestor(dd, newparent) == dd) {
2003 dsl_dir_rele(newparent, FTAG);
2004 dsl_dir_rele(dd, FTAG);
2e528b49 2005 return (SET_ERROR(EINVAL));
13fe0198 2006 }
34dc7c2f 2007
13fe0198 2008 error = dsl_dir_transfer_possible(dd->dd_parent,
788eb90c 2009 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
13fe0198
MA
2010 if (error != 0) {
2011 dsl_dir_rele(newparent, FTAG);
2012 dsl_dir_rele(dd, FTAG);
2013 return (error);
2014 }
34dc7c2f
BB
2015 }
2016
13fe0198
MA
2017 dsl_dir_rele(newparent, FTAG);
2018 dsl_dir_rele(dd, FTAG);
34dc7c2f
BB
2019 return (0);
2020}
2021
2022static void
13fe0198 2023dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 2024{
13fe0198
MA
2025 dsl_dir_rename_arg_t *ddra = arg;
2026 dsl_pool_t *dp = dmu_tx_pool(tx);
2027 dsl_dir_t *dd, *newparent;
2028 const char *mynewname;
2029 int error;
34dc7c2f 2030 objset_t *mos = dp->dp_meta_objset;
34dc7c2f 2031
13fe0198
MA
2032 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
2033 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
2034 &mynewname));
34dc7c2f 2035
6f1ffb06 2036 /* Log this before we change the name. */
6f1ffb06 2037 spa_history_log_internal_dd(dd, "rename", tx,
13fe0198 2038 "-> %s", ddra->ddra_newname);
6f1ffb06 2039
13fe0198 2040 if (newparent != dd->dd_parent) {
788eb90c
JJ
2041 objset_t *os = dd->dd_pool->dp_meta_objset;
2042 uint64_t fs_cnt = 0;
2043 uint64_t ss_cnt = 0;
2044
2045 /*
2046 * We already made sure the dd counts were initialized in the
2047 * check function.
2048 */
a0c9a17a 2049 if (spa_feature_is_active(dp->dp_spa,
788eb90c
JJ
2050 SPA_FEATURE_FS_SS_LIMIT)) {
2051 VERIFY0(zap_lookup(os, dd->dd_object,
2052 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2053 &fs_cnt));
2054 /* add 1 for the filesystem itself that we're moving */
2055 fs_cnt++;
2056
2057 VERIFY0(zap_lookup(os, dd->dd_object,
2058 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2059 &ss_cnt));
2060 }
2061
2062 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
2063 DD_FIELD_FILESYSTEM_COUNT, tx);
2064 dsl_fs_ss_count_adjust(newparent, fs_cnt,
2065 DD_FIELD_FILESYSTEM_COUNT, tx);
2066
2067 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
2068 DD_FIELD_SNAPSHOT_COUNT, tx);
2069 dsl_fs_ss_count_adjust(newparent, ss_cnt,
2070 DD_FIELD_SNAPSHOT_COUNT, tx);
2071
b128c09f 2072 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
d683ddbb
JG
2073 -dsl_dir_phys(dd)->dd_used_bytes,
2074 -dsl_dir_phys(dd)->dd_compressed_bytes,
2075 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
13fe0198 2076 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
d683ddbb
JG
2077 dsl_dir_phys(dd)->dd_used_bytes,
2078 dsl_dir_phys(dd)->dd_compressed_bytes,
2079 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
b128c09f 2080
d683ddbb
JG
2081 if (dsl_dir_phys(dd)->dd_reserved >
2082 dsl_dir_phys(dd)->dd_used_bytes) {
2083 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
2084 dsl_dir_phys(dd)->dd_used_bytes;
b128c09f
BB
2085
2086 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2087 -unused_rsrv, 0, 0, tx);
13fe0198 2088 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
b128c09f
BB
2089 unused_rsrv, 0, 0, tx);
2090 }
34dc7c2f
BB
2091 }
2092
2093 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2094
2095 /* remove from old parent zapobj */
d683ddbb
JG
2096 error = zap_remove(mos,
2097 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
34dc7c2f 2098 dd->dd_myname, tx);
13fe0198 2099 ASSERT0(error);
34dc7c2f 2100
c9d61adb 2101 (void) strlcpy(dd->dd_myname, mynewname,
2102 sizeof (dd->dd_myname));
13fe0198 2103 dsl_dir_rele(dd->dd_parent, dd);
d683ddbb 2104 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
13fe0198
MA
2105 VERIFY0(dsl_dir_hold_obj(dp,
2106 newparent->dd_object, NULL, dd, &dd->dd_parent));
34dc7c2f
BB
2107
2108 /* add to new parent zapobj */
d683ddbb 2109 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
13fe0198
MA
2110 dd->dd_myname, 8, 1, &dd->dd_object, tx));
2111
a0bd735a
BP
2112 zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,
2113 ddra->ddra_newname, B_TRUE);
ba6a2402 2114
13fe0198 2115 dsl_prop_notify_all(dd);
34dc7c2f 2116
13fe0198
MA
2117 dsl_dir_rele(newparent, FTAG);
2118 dsl_dir_rele(dd, FTAG);
34dc7c2f
BB
2119}
2120
2121int
13fe0198 2122dsl_dir_rename(const char *oldname, const char *newname)
34dc7c2f 2123{
13fe0198 2124 dsl_dir_rename_arg_t ddra;
34dc7c2f 2125
13fe0198
MA
2126 ddra.ddra_oldname = oldname;
2127 ddra.ddra_newname = newname;
788eb90c 2128 ddra.ddra_cred = CRED();
34dc7c2f 2129
13fe0198 2130 return (dsl_sync_task(oldname,
3d45fdd6
MA
2131 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2132 3, ZFS_SPACE_CHECK_RESERVED));
34dc7c2f
BB
2133}
2134
2135int
788eb90c
JJ
2136dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2137 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
34dc7c2f
BB
2138{
2139 dsl_dir_t *ancestor;
2140 int64_t adelta;
2141 uint64_t avail;
788eb90c 2142 int err;
34dc7c2f
BB
2143
2144 ancestor = closest_common_ancestor(sdd, tdd);
2145 adelta = would_change(sdd, -space, ancestor);
2146 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2147 if (avail < space)
2e528b49 2148 return (SET_ERROR(ENOSPC));
34dc7c2f 2149
788eb90c
JJ
2150 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2151 ancestor, cr);
2152 if (err != 0)
2153 return (err);
2154 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2155 ancestor, cr);
2156 if (err != 0)
2157 return (err);
2158
34dc7c2f
BB
2159 return (0);
2160}
428870ff 2161
6413c95f 2162inode_timespec_t
428870ff
BB
2163dsl_dir_snap_cmtime(dsl_dir_t *dd)
2164{
6413c95f 2165 inode_timespec_t t;
428870ff
BB
2166
2167 mutex_enter(&dd->dd_lock);
2168 t = dd->dd_snap_cmtime;
2169 mutex_exit(&dd->dd_lock);
2170
2171 return (t);
2172}
2173
2174void
2175dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
2176{
6413c95f 2177 inode_timespec_t t;
428870ff
BB
2178
2179 gethrestime(&t);
2180 mutex_enter(&dd->dd_lock);
2181 dd->dd_snap_cmtime = t;
2182 mutex_exit(&dd->dd_lock);
2183}
c28b2279 2184
fa86b5db
MA
2185void
2186dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2187{
2188 objset_t *mos = dd->dd_pool->dp_meta_objset;
2189 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2190}
2191
788eb90c
JJ
2192boolean_t
2193dsl_dir_is_zapified(dsl_dir_t *dd)
2194{
2195 dmu_object_info_t doi;
2196
2197 dmu_object_info_from_db(dd->dd_dbuf, &doi);
2198 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2199}
2200
93ce2b4c 2201#if defined(_KERNEL)
c28b2279
BB
2202EXPORT_SYMBOL(dsl_dir_set_quota);
2203EXPORT_SYMBOL(dsl_dir_set_reservation);
c28b2279 2204#endif