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