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