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