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