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