]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_dir.c
Remove unused machelf.h header
[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.
34dc7c2f
BB
23 */
24
34dc7c2f
BB
25#include <sys/dmu.h>
26#include <sys/dmu_objset.h>
27#include <sys/dmu_tx.h>
28#include <sys/dsl_dataset.h>
29#include <sys/dsl_dir.h>
30#include <sys/dsl_prop.h>
31#include <sys/dsl_synctask.h>
32#include <sys/dsl_deleg.h>
33#include <sys/spa.h>
428870ff 34#include <sys/metaslab.h>
34dc7c2f
BB
35#include <sys/zap.h>
36#include <sys/zio.h>
37#include <sys/arc.h>
38#include <sys/sunddi.h>
39#include "zfs_namecheck.h"
40
41static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
428870ff 42static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx);
34dc7c2f
BB
43
44
45/* ARGSUSED */
46static void
47dsl_dir_evict(dmu_buf_t *db, void *arg)
48{
49 dsl_dir_t *dd = arg;
1fde1e37 50 ASSERTV(dsl_pool_t *dp = dd->dd_pool;)
34dc7c2f
BB
51 int t;
52
53 for (t = 0; t < TXG_SIZE; t++) {
54 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
55 ASSERT(dd->dd_tempreserved[t] == 0);
56 ASSERT(dd->dd_space_towrite[t] == 0);
57 }
58
34dc7c2f
BB
59 if (dd->dd_parent)
60 dsl_dir_close(dd->dd_parent, dd);
61
62 spa_close(dd->dd_pool->dp_spa, dd);
63
64 /*
428870ff
BB
65 * The props callback list should have been cleaned up by
66 * objset_evict().
34dc7c2f
BB
67 */
68 list_destroy(&dd->dd_prop_cbs);
69 mutex_destroy(&dd->dd_lock);
70 kmem_free(dd, sizeof (dsl_dir_t));
71}
72
73int
74dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
75 const char *tail, void *tag, dsl_dir_t **ddp)
76{
77 dmu_buf_t *dbuf;
78 dsl_dir_t *dd;
79 int err;
80
81 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
82 dsl_pool_sync_context(dp));
83
84 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
85 if (err)
86 return (err);
87 dd = dmu_buf_get_user(dbuf);
88#ifdef ZFS_DEBUG
89 {
90 dmu_object_info_t doi;
91 dmu_object_info_from_db(dbuf, &doi);
92 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
b128c09f 93 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
34dc7c2f
BB
94 }
95#endif
34dc7c2f
BB
96 if (dd == NULL) {
97 dsl_dir_t *winner;
34dc7c2f 98
b8d06fca 99 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_PUSHPAGE);
34dc7c2f
BB
100 dd->dd_object = ddobj;
101 dd->dd_dbuf = dbuf;
102 dd->dd_pool = dp;
103 dd->dd_phys = dbuf->db_data;
34dc7c2f
BB
104 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
105
106 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
107 offsetof(dsl_prop_cb_record_t, cbr_node));
108
428870ff
BB
109 dsl_dir_snap_cmtime_update(dd);
110
34dc7c2f
BB
111 if (dd->dd_phys->dd_parent_obj) {
112 err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
113 NULL, dd, &dd->dd_parent);
b128c09f
BB
114 if (err)
115 goto errout;
34dc7c2f
BB
116 if (tail) {
117#ifdef ZFS_DEBUG
118 uint64_t foundobj;
119
120 err = zap_lookup(dp->dp_meta_objset,
121 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
122 tail, sizeof (foundobj), 1, &foundobj);
123 ASSERT(err || foundobj == ddobj);
124#endif
125 (void) strcpy(dd->dd_myname, tail);
126 } else {
127 err = zap_value_search(dp->dp_meta_objset,
128 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
129 ddobj, 0, dd->dd_myname);
130 }
b128c09f
BB
131 if (err)
132 goto errout;
34dc7c2f
BB
133 } else {
134 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
135 }
136
428870ff
BB
137 if (dsl_dir_is_clone(dd)) {
138 dmu_buf_t *origin_bonus;
139 dsl_dataset_phys_t *origin_phys;
140
141 /*
142 * We can't open the origin dataset, because
143 * that would require opening this dsl_dir.
144 * Just look at its phys directly instead.
145 */
146 err = dmu_bonus_hold(dp->dp_meta_objset,
147 dd->dd_phys->dd_origin_obj, FTAG, &origin_bonus);
148 if (err)
149 goto errout;
150 origin_phys = origin_bonus->db_data;
151 dd->dd_origin_txg =
152 origin_phys->ds_creation_txg;
153 dmu_buf_rele(origin_bonus, FTAG);
154 }
155
34dc7c2f
BB
156 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
157 dsl_dir_evict);
158 if (winner) {
159 if (dd->dd_parent)
160 dsl_dir_close(dd->dd_parent, dd);
161 mutex_destroy(&dd->dd_lock);
162 kmem_free(dd, sizeof (dsl_dir_t));
163 dd = winner;
164 } else {
165 spa_open_ref(dp->dp_spa, dd);
166 }
167 }
168
169 /*
170 * The dsl_dir_t has both open-to-close and instantiate-to-evict
171 * holds on the spa. We need the open-to-close holds because
172 * otherwise the spa_refcnt wouldn't change when we open a
173 * dir which the spa also has open, so we could incorrectly
174 * think it was OK to unload/export/destroy the pool. We need
175 * the instantiate-to-evict hold because the dsl_dir_t has a
176 * pointer to the dd_pool, which has a pointer to the spa_t.
177 */
178 spa_open_ref(dp->dp_spa, tag);
179 ASSERT3P(dd->dd_pool, ==, dp);
180 ASSERT3U(dd->dd_object, ==, ddobj);
181 ASSERT3P(dd->dd_dbuf, ==, dbuf);
182 *ddp = dd;
183 return (0);
b128c09f
BB
184
185errout:
186 if (dd->dd_parent)
187 dsl_dir_close(dd->dd_parent, dd);
188 mutex_destroy(&dd->dd_lock);
189 kmem_free(dd, sizeof (dsl_dir_t));
190 dmu_buf_rele(dbuf, tag);
191 return (err);
34dc7c2f
BB
192}
193
194void
195dsl_dir_close(dsl_dir_t *dd, void *tag)
196{
197 dprintf_dd(dd, "%s\n", "");
198 spa_close(dd->dd_pool->dp_spa, tag);
199 dmu_buf_rele(dd->dd_dbuf, tag);
200}
201
202/* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
203void
204dsl_dir_name(dsl_dir_t *dd, char *buf)
205{
206 if (dd->dd_parent) {
207 dsl_dir_name(dd->dd_parent, buf);
208 (void) strcat(buf, "/");
209 } else {
210 buf[0] = '\0';
211 }
212 if (!MUTEX_HELD(&dd->dd_lock)) {
213 /*
214 * recursive mutex so that we can use
215 * dprintf_dd() with dd_lock held
216 */
217 mutex_enter(&dd->dd_lock);
218 (void) strcat(buf, dd->dd_myname);
219 mutex_exit(&dd->dd_lock);
220 } else {
221 (void) strcat(buf, dd->dd_myname);
222 }
223}
224
29809a6c 225/* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
34dc7c2f
BB
226int
227dsl_dir_namelen(dsl_dir_t *dd)
228{
229 int result = 0;
230
231 if (dd->dd_parent) {
232 /* parent's name + 1 for the "/" */
233 result = dsl_dir_namelen(dd->dd_parent) + 1;
234 }
235
236 if (!MUTEX_HELD(&dd->dd_lock)) {
237 /* see dsl_dir_name */
238 mutex_enter(&dd->dd_lock);
239 result += strlen(dd->dd_myname);
240 mutex_exit(&dd->dd_lock);
241 } else {
242 result += strlen(dd->dd_myname);
243 }
244
245 return (result);
246}
247
34dc7c2f
BB
248static int
249getcomponent(const char *path, char *component, const char **nextp)
250{
251 char *p;
9babb374 252 if ((path == NULL) || (path[0] == '\0'))
34dc7c2f
BB
253 return (ENOENT);
254 /* This would be a good place to reserve some namespace... */
255 p = strpbrk(path, "/@");
256 if (p && (p[1] == '/' || p[1] == '@')) {
257 /* two separators in a row */
258 return (EINVAL);
259 }
260 if (p == NULL || p == path) {
261 /*
262 * if the first thing is an @ or /, it had better be an
263 * @ and it had better not have any more ats or slashes,
264 * and it had better have something after the @.
265 */
266 if (p != NULL &&
267 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
268 return (EINVAL);
269 if (strlen(path) >= MAXNAMELEN)
270 return (ENAMETOOLONG);
271 (void) strcpy(component, path);
272 p = NULL;
273 } else if (p[0] == '/') {
274 if (p-path >= MAXNAMELEN)
275 return (ENAMETOOLONG);
276 (void) strncpy(component, path, p - path);
277 component[p-path] = '\0';
278 p++;
279 } else if (p[0] == '@') {
280 /*
281 * if the next separator is an @, there better not be
282 * any more slashes.
283 */
284 if (strchr(path, '/'))
285 return (EINVAL);
286 if (p-path >= MAXNAMELEN)
287 return (ENAMETOOLONG);
288 (void) strncpy(component, path, p - path);
289 component[p-path] = '\0';
290 } else {
291 ASSERT(!"invalid p");
292 }
293 *nextp = p;
294 return (0);
295}
296
297/*
298 * same as dsl_open_dir, ignore the first component of name and use the
299 * spa instead
300 */
301int
302dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
303 dsl_dir_t **ddp, const char **tailp)
304{
fcf37ec6 305 char *buf;
34dc7c2f
BB
306 const char *next, *nextnext = NULL;
307 int err;
308 dsl_dir_t *dd;
309 dsl_pool_t *dp;
310 uint64_t ddobj;
311 int openedspa = FALSE;
312
313 dprintf("%s\n", name);
314
fcf37ec6 315 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
34dc7c2f
BB
316 err = getcomponent(name, buf, &next);
317 if (err)
fcf37ec6 318 goto error;
34dc7c2f
BB
319 if (spa == NULL) {
320 err = spa_open(buf, &spa, FTAG);
321 if (err) {
322 dprintf("spa_open(%s) failed\n", buf);
fcf37ec6 323 goto error;
34dc7c2f
BB
324 }
325 openedspa = TRUE;
326
327 /* XXX this assertion belongs in spa_open */
328 ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
329 }
330
331 dp = spa_get_dsl(spa);
332
333 rw_enter(&dp->dp_config_rwlock, RW_READER);
334 err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
335 if (err) {
336 rw_exit(&dp->dp_config_rwlock);
337 if (openedspa)
338 spa_close(spa, FTAG);
fcf37ec6 339 goto error;
34dc7c2f
BB
340 }
341
342 while (next != NULL) {
343 dsl_dir_t *child_ds;
344 err = getcomponent(next, buf, &nextnext);
345 if (err)
346 break;
347 ASSERT(next[0] != '\0');
348 if (next[0] == '@')
349 break;
350 dprintf("looking up %s in obj%lld\n",
351 buf, dd->dd_phys->dd_child_dir_zapobj);
352
353 err = zap_lookup(dp->dp_meta_objset,
354 dd->dd_phys->dd_child_dir_zapobj,
355 buf, sizeof (ddobj), 1, &ddobj);
356 if (err) {
357 if (err == ENOENT)
358 err = 0;
359 break;
360 }
361
362 err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
363 if (err)
364 break;
365 dsl_dir_close(dd, tag);
366 dd = child_ds;
367 next = nextnext;
368 }
369 rw_exit(&dp->dp_config_rwlock);
370
371 if (err) {
372 dsl_dir_close(dd, tag);
373 if (openedspa)
374 spa_close(spa, FTAG);
fcf37ec6 375 goto error;
34dc7c2f
BB
376 }
377
378 /*
379 * It's an error if there's more than one component left, or
380 * tailp==NULL and there's any component left.
381 */
382 if (next != NULL &&
383 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
384 /* bad path name */
385 dsl_dir_close(dd, tag);
386 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
387 err = ENOENT;
388 }
389 if (tailp)
390 *tailp = next;
391 if (openedspa)
392 spa_close(spa, FTAG);
393 *ddp = dd;
fcf37ec6
BB
394error:
395 kmem_free(buf, MAXNAMELEN);
34dc7c2f
BB
396 return (err);
397}
398
399/*
400 * Return the dsl_dir_t, and possibly the last component which couldn't
401 * be found in *tail. Return NULL if the path is bogus, or if
402 * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@'
403 * means that the last component is a snapshot.
404 */
405int
406dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
407{
408 return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
409}
410
411uint64_t
b128c09f
BB
412dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
413 dmu_tx_t *tx)
34dc7c2f 414{
b128c09f 415 objset_t *mos = dp->dp_meta_objset;
34dc7c2f 416 uint64_t ddobj;
428870ff 417 dsl_dir_phys_t *ddphys;
34dc7c2f
BB
418 dmu_buf_t *dbuf;
419
420 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
421 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
b128c09f
BB
422 if (pds) {
423 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
424 name, sizeof (uint64_t), 1, &ddobj, tx));
425 } else {
426 /* it's the root dir */
427 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
428 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
429 }
34dc7c2f
BB
430 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
431 dmu_buf_will_dirty(dbuf, tx);
428870ff 432 ddphys = dbuf->db_data;
34dc7c2f 433
428870ff 434 ddphys->dd_creation_time = gethrestime_sec();
b128c09f 435 if (pds)
428870ff
BB
436 ddphys->dd_parent_obj = pds->dd_object;
437 ddphys->dd_props_zapobj = zap_create(mos,
34dc7c2f 438 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
428870ff 439 ddphys->dd_child_dir_zapobj = zap_create(mos,
34dc7c2f 440 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
b128c09f 441 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
428870ff 442 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
34dc7c2f
BB
443 dmu_buf_rele(dbuf, FTAG);
444
445 return (ddobj);
446}
447
448/* ARGSUSED */
449int
450dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
451{
428870ff
BB
452 dsl_dataset_t *ds = arg1;
453 dsl_dir_t *dd = ds->ds_dir;
34dc7c2f
BB
454 dsl_pool_t *dp = dd->dd_pool;
455 objset_t *mos = dp->dp_meta_objset;
456 int err;
457 uint64_t count;
458
459 /*
460 * There should be exactly two holds, both from
461 * dsl_dataset_destroy: one on the dd directory, and one on its
65947351
GW
462 * head ds. If there are more holds, then a concurrent thread is
463 * performing a lookup inside this dir while we're trying to destroy
464 * it. To minimize this possibility, we perform this check only
465 * in syncing context and fail the operation if we encounter
466 * additional holds. The dp_config_rwlock ensures that nobody else
467 * opens it after we check.
34dc7c2f 468 */
65947351 469 if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 2)
34dc7c2f
BB
470 return (EBUSY);
471
472 err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
473 if (err)
474 return (err);
475 if (count != 0)
476 return (EEXIST);
477
478 return (0);
479}
480
481void
428870ff 482dsl_dir_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
34dc7c2f 483{
428870ff
BB
484 dsl_dataset_t *ds = arg1;
485 dsl_dir_t *dd = ds->ds_dir;
34dc7c2f 486 objset_t *mos = dd->dd_pool->dp_meta_objset;
428870ff
BB
487 dsl_prop_setarg_t psa;
488 uint64_t value = 0;
489 uint64_t obj;
b128c09f 490 dd_used_t t;
34dc7c2f
BB
491
492 ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
493 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
494
495 /* Remove our reservation. */
428870ff
BB
496 dsl_prop_setarg_init_uint64(&psa, "reservation",
497 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
498 &value);
499 psa.psa_effective_value = 0; /* predict default value */
500
501 dsl_dir_set_reservation_sync(ds, &psa, tx);
502
b128c09f 503 ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0);
34dc7c2f 504 ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
b128c09f
BB
505 for (t = 0; t < DD_USED_NUM; t++)
506 ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0);
34dc7c2f
BB
507
508 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
509 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
510 VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
511 VERIFY(0 == zap_remove(mos,
512 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
513
514 obj = dd->dd_object;
515 dsl_dir_close(dd, tag);
516 VERIFY(0 == dmu_object_free(mos, obj, tx));
517}
518
b128c09f
BB
519boolean_t
520dsl_dir_is_clone(dsl_dir_t *dd)
34dc7c2f 521{
b128c09f
BB
522 return (dd->dd_phys->dd_origin_obj &&
523 (dd->dd_pool->dp_origin_snap == NULL ||
524 dd->dd_phys->dd_origin_obj !=
525 dd->dd_pool->dp_origin_snap->ds_object));
34dc7c2f
BB
526}
527
528void
529dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
530{
531 mutex_enter(&dd->dd_lock);
b128c09f
BB
532 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
533 dd->dd_phys->dd_used_bytes);
34dc7c2f
BB
534 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
535 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
536 dd->dd_phys->dd_reserved);
537 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
538 dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
539 (dd->dd_phys->dd_uncompressed_bytes * 100 /
540 dd->dd_phys->dd_compressed_bytes));
b128c09f
BB
541 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
542 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
543 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
544 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
545 dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
546 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
547 dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
548 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
549 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
550 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
551 }
34dc7c2f
BB
552 mutex_exit(&dd->dd_lock);
553
554 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
b128c09f 555 if (dsl_dir_is_clone(dd)) {
34dc7c2f
BB
556 dsl_dataset_t *ds;
557 char buf[MAXNAMELEN];
558
b128c09f
BB
559 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
560 dd->dd_phys->dd_origin_obj, FTAG, &ds));
34dc7c2f 561 dsl_dataset_name(ds, buf);
b128c09f 562 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
563 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
564 }
565 rw_exit(&dd->dd_pool->dp_config_rwlock);
566}
567
568void
569dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
570{
571 dsl_pool_t *dp = dd->dd_pool;
572
573 ASSERT(dd->dd_phys);
574
575 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
576 /* up the hold count until we can be written out */
577 dmu_buf_add_ref(dd->dd_dbuf, dd);
578 }
579}
580
581static int64_t
582parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
583{
584 uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
585 uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
586 return (new_accounted - old_accounted);
587}
588
589void
590dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
591{
592 ASSERT(dmu_tx_is_syncing(tx));
593
34dc7c2f
BB
594 mutex_enter(&dd->dd_lock);
595 ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
596 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
597 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
598 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
34dc7c2f
BB
599 mutex_exit(&dd->dd_lock);
600
601 /* release the hold from dsl_dir_dirty */
602 dmu_buf_rele(dd->dd_dbuf, dd);
603}
604
605static uint64_t
606dsl_dir_space_towrite(dsl_dir_t *dd)
607{
608 uint64_t space = 0;
609 int i;
610
611 ASSERT(MUTEX_HELD(&dd->dd_lock));
612
613 for (i = 0; i < TXG_SIZE; i++) {
614 space += dd->dd_space_towrite[i&TXG_MASK];
615 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
616 }
617 return (space);
618}
619
620/*
621 * How much space would dd have available if ancestor had delta applied
622 * to it? If ondiskonly is set, we're only interested in what's
623 * on-disk, not estimated pending changes.
624 */
625uint64_t
626dsl_dir_space_available(dsl_dir_t *dd,
627 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
628{
629 uint64_t parentspace, myspace, quota, used;
630
631 /*
632 * If there are no restrictions otherwise, assume we have
633 * unlimited space available.
634 */
635 quota = UINT64_MAX;
636 parentspace = UINT64_MAX;
637
638 if (dd->dd_parent != NULL) {
639 parentspace = dsl_dir_space_available(dd->dd_parent,
640 ancestor, delta, ondiskonly);
641 }
642
643 mutex_enter(&dd->dd_lock);
644 if (dd->dd_phys->dd_quota != 0)
645 quota = dd->dd_phys->dd_quota;
b128c09f 646 used = dd->dd_phys->dd_used_bytes;
34dc7c2f
BB
647 if (!ondiskonly)
648 used += dsl_dir_space_towrite(dd);
34dc7c2f
BB
649
650 if (dd->dd_parent == NULL) {
651 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
652 quota = MIN(quota, poolsize);
653 }
654
655 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
656 /*
657 * We have some space reserved, in addition to what our
658 * parent gave us.
659 */
660 parentspace += dd->dd_phys->dd_reserved - used;
661 }
662
b128c09f
BB
663 if (dd == ancestor) {
664 ASSERT(delta <= 0);
665 ASSERT(used >= -delta);
666 used += delta;
667 if (parentspace != UINT64_MAX)
668 parentspace -= delta;
669 }
670
34dc7c2f
BB
671 if (used > quota) {
672 /* over quota */
673 myspace = 0;
34dc7c2f
BB
674 } else {
675 /*
676 * the lesser of the space provided by our parent and
677 * the space left in our quota
678 */
679 myspace = MIN(parentspace, quota - used);
680 }
681
682 mutex_exit(&dd->dd_lock);
683
684 return (myspace);
685}
686
687struct tempreserve {
688 list_node_t tr_node;
689 dsl_pool_t *tr_dp;
690 dsl_dir_t *tr_ds;
691 uint64_t tr_size;
692};
693
694static int
695dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
696 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
697 dmu_tx_t *tx, boolean_t first)
698{
699 uint64_t txg = tx->tx_txg;
700 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
428870ff 701 uint64_t deferred = 0;
34dc7c2f 702 struct tempreserve *tr;
428870ff 703 int retval = EDQUOT;
34dc7c2f
BB
704 int txgidx = txg & TXG_MASK;
705 int i;
706 uint64_t ref_rsrv = 0;
707
708 ASSERT3U(txg, !=, 0);
709 ASSERT3S(asize, >, 0);
710
711 mutex_enter(&dd->dd_lock);
712
713 /*
714 * Check against the dsl_dir's quota. We don't add in the delta
715 * when checking for over-quota because they get one free hit.
716 */
717 est_inflight = dsl_dir_space_towrite(dd);
718 for (i = 0; i < TXG_SIZE; i++)
719 est_inflight += dd->dd_tempreserved[i];
b128c09f 720 used_on_disk = dd->dd_phys->dd_used_bytes;
34dc7c2f
BB
721
722 /*
723 * On the first iteration, fetch the dataset's used-on-disk and
724 * refreservation values. Also, if checkrefquota is set, test if
725 * allocating this space would exceed the dataset's refquota.
726 */
727 if (first && tx->tx_objset) {
728 int error;
428870ff 729 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
34dc7c2f
BB
730
731 error = dsl_dataset_check_quota(ds, checkrefquota,
732 asize, est_inflight, &used_on_disk, &ref_rsrv);
733 if (error) {
734 mutex_exit(&dd->dd_lock);
735 return (error);
736 }
737 }
738
739 /*
740 * If this transaction will result in a net free of space,
741 * we want to let it through.
742 */
743 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
744 quota = UINT64_MAX;
745 else
746 quota = dd->dd_phys->dd_quota;
747
748 /*
428870ff
BB
749 * Adjust the quota against the actual pool size at the root
750 * minus any outstanding deferred frees.
34dc7c2f
BB
751 * To ensure that it's possible to remove files from a full
752 * pool without inducing transient overcommits, we throttle
753 * netfree transactions against a quota that is slightly larger,
754 * but still within the pool's allocation slop. In cases where
755 * we're very close to full, this will allow a steady trickle of
756 * removes to get through.
757 */
758 if (dd->dd_parent == NULL) {
428870ff 759 spa_t *spa = dd->dd_pool->dp_spa;
34dc7c2f 760 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
428870ff
BB
761 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
762 if (poolsize - deferred < quota) {
763 quota = poolsize - deferred;
764 retval = ENOSPC;
34dc7c2f
BB
765 }
766 }
767
768 /*
769 * If they are requesting more space, and our current estimate
770 * is over quota, they get to try again unless the actual
771 * on-disk is over quota and there are no pending changes (which
772 * may free up space for us).
773 */
428870ff
BB
774 if (used_on_disk + est_inflight >= quota) {
775 if (est_inflight > 0 || used_on_disk < quota ||
776 (retval == ENOSPC && used_on_disk < quota + deferred))
777 retval = ERESTART;
34dc7c2f
BB
778 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
779 "quota=%lluK tr=%lluK err=%d\n",
780 used_on_disk>>10, est_inflight>>10,
428870ff 781 quota>>10, asize>>10, retval);
34dc7c2f 782 mutex_exit(&dd->dd_lock);
428870ff 783 return (retval);
34dc7c2f
BB
784 }
785
786 /* We need to up our estimated delta before dropping dd_lock */
787 dd->dd_tempreserved[txgidx] += asize;
788
789 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
790 asize - ref_rsrv);
791 mutex_exit(&dd->dd_lock);
792
b8d06fca 793 tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE);
34dc7c2f
BB
794 tr->tr_ds = dd;
795 tr->tr_size = asize;
796 list_insert_tail(tr_list, tr);
797
798 /* see if it's OK with our parent */
799 if (dd->dd_parent && parent_rsrv) {
800 boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
801
802 return (dsl_dir_tempreserve_impl(dd->dd_parent,
803 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
804 } else {
805 return (0);
806 }
807}
808
809/*
810 * Reserve space in this dsl_dir, to be used in this tx's txg.
811 * After the space has been dirtied (and dsl_dir_willuse_space()
812 * has been called), the reservation should be canceled, using
813 * dsl_dir_tempreserve_clear().
814 */
815int
816dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
817 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
818{
819 int err;
820 list_t *tr_list;
821
822 if (asize == 0) {
823 *tr_cookiep = NULL;
824 return (0);
825 }
826
b8d06fca 827 tr_list = kmem_alloc(sizeof (list_t), KM_PUSHPAGE);
34dc7c2f
BB
828 list_create(tr_list, sizeof (struct tempreserve),
829 offsetof(struct tempreserve, tr_node));
830 ASSERT3S(asize, >, 0);
831 ASSERT3S(fsize, >=, 0);
832
833 err = arc_tempreserve_space(lsize, tx->tx_txg);
834 if (err == 0) {
835 struct tempreserve *tr;
836
b8d06fca 837 tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE);
34dc7c2f
BB
838 tr->tr_size = lsize;
839 list_insert_tail(tr_list, tr);
840
841 err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
842 } else {
843 if (err == EAGAIN) {
844 txg_delay(dd->dd_pool, tx->tx_txg, 1);
845 err = ERESTART;
846 }
847 dsl_pool_memory_pressure(dd->dd_pool);
848 }
849
850 if (err == 0) {
851 struct tempreserve *tr;
852
b8d06fca 853 tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE);
34dc7c2f
BB
854 tr->tr_dp = dd->dd_pool;
855 tr->tr_size = asize;
856 list_insert_tail(tr_list, tr);
857
858 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
859 FALSE, asize > usize, tr_list, tx, TRUE);
860 }
861
862 if (err)
863 dsl_dir_tempreserve_clear(tr_list, tx);
864 else
865 *tr_cookiep = tr_list;
866
867 return (err);
868}
869
870/*
871 * Clear a temporary reservation that we previously made with
872 * dsl_dir_tempreserve_space().
873 */
874void
875dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
876{
877 int txgidx = tx->tx_txg & TXG_MASK;
878 list_t *tr_list = tr_cookie;
879 struct tempreserve *tr;
880
881 ASSERT3U(tx->tx_txg, !=, 0);
882
883 if (tr_cookie == NULL)
884 return;
885
c65aa5b2 886 while ((tr = list_head(tr_list))) {
34dc7c2f
BB
887 if (tr->tr_dp) {
888 dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
889 } else if (tr->tr_ds) {
890 mutex_enter(&tr->tr_ds->dd_lock);
891 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
892 tr->tr_size);
893 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
894 mutex_exit(&tr->tr_ds->dd_lock);
895 } else {
896 arc_tempreserve_clear(tr->tr_size);
897 }
898 list_remove(tr_list, tr);
899 kmem_free(tr, sizeof (struct tempreserve));
900 }
901
902 kmem_free(tr_list, sizeof (list_t));
903}
904
905static void
906dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
907{
908 int64_t parent_space;
909 uint64_t est_used;
910
911 mutex_enter(&dd->dd_lock);
912 if (space > 0)
913 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
914
b128c09f 915 est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
34dc7c2f
BB
916 parent_space = parent_delta(dd, est_used, space);
917 mutex_exit(&dd->dd_lock);
918
919 /* Make sure that we clean up dd_space_to* */
920 dsl_dir_dirty(dd, tx);
921
922 /* XXX this is potentially expensive and unnecessary... */
923 if (parent_space && dd->dd_parent)
924 dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
925}
926
927/*
928 * Call in open context when we think we're going to write/free space,
929 * eg. when dirtying data. Be conservative (ie. OK to write less than
930 * this or free more than this, but don't write more or free less).
931 */
932void
933dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
934{
935 dsl_pool_willuse_space(dd->dd_pool, space, tx);
936 dsl_dir_willuse_space_impl(dd, space, tx);
937}
938
939/* call from syncing context when we actually write/free space for this dd */
940void
b128c09f 941dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
34dc7c2f
BB
942 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
943{
944 int64_t accounted_delta;
b128c09f 945 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
34dc7c2f
BB
946
947 ASSERT(dmu_tx_is_syncing(tx));
b128c09f 948 ASSERT(type < DD_USED_NUM);
34dc7c2f 949
b128c09f
BB
950 if (needlock)
951 mutex_enter(&dd->dd_lock);
952 accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used);
953 ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used);
34dc7c2f
BB
954 ASSERT(compressed >= 0 ||
955 dd->dd_phys->dd_compressed_bytes >= -compressed);
956 ASSERT(uncompressed >= 0 ||
957 dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
29809a6c 958 dmu_buf_will_dirty(dd->dd_dbuf, tx);
b128c09f 959 dd->dd_phys->dd_used_bytes += used;
34dc7c2f
BB
960 dd->dd_phys->dd_uncompressed_bytes += uncompressed;
961 dd->dd_phys->dd_compressed_bytes += compressed;
b128c09f
BB
962
963 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
964 ASSERT(used > 0 ||
965 dd->dd_phys->dd_used_breakdown[type] >= -used);
966 dd->dd_phys->dd_used_breakdown[type] += used;
967#ifdef DEBUG
d6320ddb
BB
968 {
969 dd_used_t t;
970 uint64_t u = 0;
971 for (t = 0; t < DD_USED_NUM; t++)
972 u += dd->dd_phys->dd_used_breakdown[t];
973 ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
974 }
b128c09f
BB
975#endif
976 }
977 if (needlock)
978 mutex_exit(&dd->dd_lock);
34dc7c2f
BB
979
980 if (dd->dd_parent != NULL) {
b128c09f 981 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
34dc7c2f 982 accounted_delta, compressed, uncompressed, tx);
b128c09f
BB
983 dsl_dir_transfer_space(dd->dd_parent,
984 used - accounted_delta,
985 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
34dc7c2f
BB
986 }
987}
988
b128c09f
BB
989void
990dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
991 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
992{
993 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
994
995 ASSERT(dmu_tx_is_syncing(tx));
996 ASSERT(oldtype < DD_USED_NUM);
997 ASSERT(newtype < DD_USED_NUM);
998
999 if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
1000 return;
1001
b128c09f
BB
1002 if (needlock)
1003 mutex_enter(&dd->dd_lock);
1004 ASSERT(delta > 0 ?
1005 dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
1006 dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
1007 ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
29809a6c 1008 dmu_buf_will_dirty(dd->dd_dbuf, tx);
b128c09f
BB
1009 dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
1010 dd->dd_phys->dd_used_breakdown[newtype] += delta;
1011 if (needlock)
1012 mutex_exit(&dd->dd_lock);
1013}
1014
34dc7c2f
BB
1015static int
1016dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
1017{
428870ff
BB
1018 dsl_dataset_t *ds = arg1;
1019 dsl_dir_t *dd = ds->ds_dir;
1020 dsl_prop_setarg_t *psa = arg2;
1021 int err;
34dc7c2f
BB
1022 uint64_t towrite;
1023
428870ff
BB
1024 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1025 return (err);
1026
1027 if (psa->psa_effective_value == 0)
34dc7c2f
BB
1028 return (0);
1029
1030 mutex_enter(&dd->dd_lock);
1031 /*
1032 * If we are doing the preliminary check in open context, and
1033 * there are pending changes, then don't fail it, since the
1034 * pending changes could under-estimate the amount of space to be
1035 * freed up.
1036 */
1037 towrite = dsl_dir_space_towrite(dd);
1038 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
428870ff
BB
1039 (psa->psa_effective_value < dd->dd_phys->dd_reserved ||
1040 psa->psa_effective_value < dd->dd_phys->dd_used_bytes + towrite)) {
34dc7c2f
BB
1041 err = ENOSPC;
1042 }
1043 mutex_exit(&dd->dd_lock);
1044 return (err);
1045}
1046
428870ff
BB
1047extern dsl_syncfunc_t dsl_prop_set_sync;
1048
34dc7c2f 1049static void
428870ff 1050dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f 1051{
428870ff
BB
1052 dsl_dataset_t *ds = arg1;
1053 dsl_dir_t *dd = ds->ds_dir;
1054 dsl_prop_setarg_t *psa = arg2;
1055 uint64_t effective_value = psa->psa_effective_value;
1056
1057 dsl_prop_set_sync(ds, psa, tx);
1058 DSL_PROP_CHECK_PREDICTION(dd, psa);
34dc7c2f
BB
1059
1060 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1061
1062 mutex_enter(&dd->dd_lock);
428870ff 1063 dd->dd_phys->dd_quota = effective_value;
34dc7c2f 1064 mutex_exit(&dd->dd_lock);
34dc7c2f
BB
1065}
1066
1067int
428870ff 1068dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
34dc7c2f
BB
1069{
1070 dsl_dir_t *dd;
428870ff
BB
1071 dsl_dataset_t *ds;
1072 dsl_prop_setarg_t psa;
34dc7c2f
BB
1073 int err;
1074
428870ff
BB
1075 dsl_prop_setarg_init_uint64(&psa, "quota", source, &quota);
1076
1077 err = dsl_dataset_hold(ddname, FTAG, &ds);
34dc7c2f
BB
1078 if (err)
1079 return (err);
1080
428870ff
BB
1081 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1082 if (err) {
1083 dsl_dataset_rele(ds, FTAG);
1084 return (err);
34dc7c2f 1085 }
428870ff
BB
1086
1087 ASSERT(ds->ds_dir == dd);
1088
1089 /*
1090 * If someone removes a file, then tries to set the quota, we want to
1091 * make sure the file freeing takes effect.
1092 */
1093 txg_wait_open(dd->dd_pool, 0);
1094
1095 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1096 dsl_dir_set_quota_sync, ds, &psa, 0);
1097
34dc7c2f 1098 dsl_dir_close(dd, FTAG);
428870ff 1099 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
1100 return (err);
1101}
1102
1103int
1104dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1105{
428870ff
BB
1106 dsl_dataset_t *ds = arg1;
1107 dsl_dir_t *dd = ds->ds_dir;
1108 dsl_prop_setarg_t *psa = arg2;
1109 uint64_t effective_value;
34dc7c2f 1110 uint64_t used, avail;
428870ff
BB
1111 int err;
1112
1113 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1114 return (err);
1115
1116 effective_value = psa->psa_effective_value;
34dc7c2f
BB
1117
1118 /*
1119 * If we are doing the preliminary check in open context, the
1120 * space estimates may be inaccurate.
1121 */
1122 if (!dmu_tx_is_syncing(tx))
1123 return (0);
1124
1125 mutex_enter(&dd->dd_lock);
b128c09f 1126 used = dd->dd_phys->dd_used_bytes;
34dc7c2f
BB
1127 mutex_exit(&dd->dd_lock);
1128
1129 if (dd->dd_parent) {
1130 avail = dsl_dir_space_available(dd->dd_parent,
1131 NULL, 0, FALSE);
1132 } else {
1133 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1134 }
1135
428870ff
BB
1136 if (MAX(used, effective_value) > MAX(used, dd->dd_phys->dd_reserved)) {
1137 uint64_t delta = MAX(used, effective_value) -
d164b209
BB
1138 MAX(used, dd->dd_phys->dd_reserved);
1139
1140 if (delta > avail)
1141 return (ENOSPC);
1142 if (dd->dd_phys->dd_quota > 0 &&
428870ff 1143 effective_value > dd->dd_phys->dd_quota)
d164b209
BB
1144 return (ENOSPC);
1145 }
1146
34dc7c2f
BB
1147 return (0);
1148}
1149
34dc7c2f 1150static void
428870ff 1151dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f 1152{
428870ff
BB
1153 dsl_dataset_t *ds = arg1;
1154 dsl_dir_t *dd = ds->ds_dir;
1155 dsl_prop_setarg_t *psa = arg2;
1156 uint64_t effective_value = psa->psa_effective_value;
34dc7c2f
BB
1157 uint64_t used;
1158 int64_t delta;
1159
428870ff
BB
1160 dsl_prop_set_sync(ds, psa, tx);
1161 DSL_PROP_CHECK_PREDICTION(dd, psa);
1162
34dc7c2f
BB
1163 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1164
1165 mutex_enter(&dd->dd_lock);
b128c09f 1166 used = dd->dd_phys->dd_used_bytes;
428870ff 1167 delta = MAX(used, effective_value) -
34dc7c2f 1168 MAX(used, dd->dd_phys->dd_reserved);
428870ff 1169 dd->dd_phys->dd_reserved = effective_value;
34dc7c2f
BB
1170
1171 if (dd->dd_parent != NULL) {
1172 /* Roll up this additional usage into our ancestors */
b128c09f
BB
1173 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1174 delta, 0, 0, tx);
34dc7c2f 1175 }
b128c09f 1176 mutex_exit(&dd->dd_lock);
34dc7c2f
BB
1177}
1178
1179int
428870ff
BB
1180dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1181 uint64_t reservation)
34dc7c2f
BB
1182{
1183 dsl_dir_t *dd;
428870ff
BB
1184 dsl_dataset_t *ds;
1185 dsl_prop_setarg_t psa;
34dc7c2f
BB
1186 int err;
1187
428870ff
BB
1188 dsl_prop_setarg_init_uint64(&psa, "reservation", source, &reservation);
1189
1190 err = dsl_dataset_hold(ddname, FTAG, &ds);
34dc7c2f
BB
1191 if (err)
1192 return (err);
428870ff
BB
1193
1194 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1195 if (err) {
1196 dsl_dataset_rele(ds, FTAG);
1197 return (err);
1198 }
1199
1200 ASSERT(ds->ds_dir == dd);
1201
34dc7c2f 1202 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
428870ff
BB
1203 dsl_dir_set_reservation_sync, ds, &psa, 0);
1204
34dc7c2f 1205 dsl_dir_close(dd, FTAG);
428870ff 1206 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
1207 return (err);
1208}
1209
1210static dsl_dir_t *
1211closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1212{
1213 for (; ds1; ds1 = ds1->dd_parent) {
1214 dsl_dir_t *dd;
1215 for (dd = ds2; dd; dd = dd->dd_parent) {
1216 if (ds1 == dd)
1217 return (dd);
1218 }
1219 }
1220 return (NULL);
1221}
1222
1223/*
1224 * If delta is applied to dd, how much of that delta would be applied to
1225 * ancestor? Syncing context only.
1226 */
1227static int64_t
1228would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1229{
1230 if (dd == ancestor)
1231 return (delta);
1232
1233 mutex_enter(&dd->dd_lock);
b128c09f 1234 delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
34dc7c2f
BB
1235 mutex_exit(&dd->dd_lock);
1236 return (would_change(dd->dd_parent, delta, ancestor));
1237}
1238
1239struct renamearg {
1240 dsl_dir_t *newparent;
1241 const char *mynewname;
1242};
1243
34dc7c2f
BB
1244static int
1245dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1246{
1247 dsl_dir_t *dd = arg1;
1248 struct renamearg *ra = arg2;
1249 dsl_pool_t *dp = dd->dd_pool;
1250 objset_t *mos = dp->dp_meta_objset;
1251 int err;
1252 uint64_t val;
1253
428870ff
BB
1254 /*
1255 * There should only be one reference, from dmu_objset_rename().
1256 * Fleeting holds are also possible (eg, from "zfs list" getting
1257 * stats), but any that are present in open context will likely
1258 * be gone by syncing context, so only fail from syncing
1259 * context.
1260 */
1261 if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 1)
34dc7c2f
BB
1262 return (EBUSY);
1263
1264 /* check for existing name */
1265 err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1266 ra->mynewname, 8, 1, &val);
1267 if (err == 0)
1268 return (EEXIST);
1269 if (err != ENOENT)
1270 return (err);
1271
1272 if (ra->newparent != dd->dd_parent) {
1273 /* is there enough space? */
1274 uint64_t myspace =
b128c09f 1275 MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
34dc7c2f
BB
1276
1277 /* no rename into our descendant */
1278 if (closest_common_ancestor(dd, ra->newparent) == dd)
1279 return (EINVAL);
1280
c65aa5b2
BB
1281 if ((err = dsl_dir_transfer_possible(dd->dd_parent,
1282 ra->newparent, myspace)))
34dc7c2f
BB
1283 return (err);
1284 }
1285
1286 return (0);
1287}
1288
1289static void
428870ff 1290dsl_dir_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
1291{
1292 dsl_dir_t *dd = arg1;
1293 struct renamearg *ra = arg2;
1294 dsl_pool_t *dp = dd->dd_pool;
1295 objset_t *mos = dp->dp_meta_objset;
1296 int err;
1297
1298 ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1299
1300 if (ra->newparent != dd->dd_parent) {
b128c09f
BB
1301 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1302 -dd->dd_phys->dd_used_bytes,
34dc7c2f
BB
1303 -dd->dd_phys->dd_compressed_bytes,
1304 -dd->dd_phys->dd_uncompressed_bytes, tx);
b128c09f
BB
1305 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
1306 dd->dd_phys->dd_used_bytes,
34dc7c2f
BB
1307 dd->dd_phys->dd_compressed_bytes,
1308 dd->dd_phys->dd_uncompressed_bytes, tx);
b128c09f
BB
1309
1310 if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
1311 uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
1312 dd->dd_phys->dd_used_bytes;
1313
1314 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1315 -unused_rsrv, 0, 0, tx);
1316 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
1317 unused_rsrv, 0, 0, tx);
1318 }
34dc7c2f
BB
1319 }
1320
1321 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1322
1323 /* remove from old parent zapobj */
1324 err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1325 dd->dd_myname, tx);
1326 ASSERT3U(err, ==, 0);
1327
1328 (void) strcpy(dd->dd_myname, ra->mynewname);
1329 dsl_dir_close(dd->dd_parent, dd);
1330 dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1331 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1332 ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1333
1334 /* add to new parent zapobj */
1335 err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1336 dd->dd_myname, 8, 1, &dd->dd_object, tx);
1337 ASSERT3U(err, ==, 0);
1338
428870ff
BB
1339 spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa,
1340 tx, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
34dc7c2f
BB
1341}
1342
1343int
1344dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1345{
1346 struct renamearg ra;
1347 int err;
1348
1349 /* new parent should exist */
1350 err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
1351 if (err)
1352 return (err);
1353
1354 /* can't rename to different pool */
1355 if (dd->dd_pool != ra.newparent->dd_pool) {
1356 err = ENXIO;
1357 goto out;
1358 }
1359
1360 /* new name should not already exist */
1361 if (ra.mynewname == NULL) {
1362 err = EEXIST;
1363 goto out;
1364 }
1365
1366 err = dsl_sync_task_do(dd->dd_pool,
1367 dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1368
1369out:
1370 dsl_dir_close(ra.newparent, FTAG);
1371 return (err);
1372}
1373
1374int
1375dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1376{
1377 dsl_dir_t *ancestor;
1378 int64_t adelta;
1379 uint64_t avail;
1380
1381 ancestor = closest_common_ancestor(sdd, tdd);
1382 adelta = would_change(sdd, -space, ancestor);
1383 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1384 if (avail < space)
1385 return (ENOSPC);
1386
1387 return (0);
1388}
428870ff
BB
1389
1390timestruc_t
1391dsl_dir_snap_cmtime(dsl_dir_t *dd)
1392{
1393 timestruc_t t;
1394
1395 mutex_enter(&dd->dd_lock);
1396 t = dd->dd_snap_cmtime;
1397 mutex_exit(&dd->dd_lock);
1398
1399 return (t);
1400}
1401
1402void
1403dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1404{
1405 timestruc_t t;
1406
1407 gethrestime(&t);
1408 mutex_enter(&dd->dd_lock);
1409 dd->dd_snap_cmtime = t;
1410 mutex_exit(&dd->dd_lock);
1411}
c28b2279
BB
1412
1413#if defined(_KERNEL) && defined(HAVE_SPL)
1414EXPORT_SYMBOL(dsl_dir_set_quota);
1415EXPORT_SYMBOL(dsl_dir_set_reservation);
1416EXPORT_SYMBOL(dsl_dir_open);
1417EXPORT_SYMBOL(dsl_dir_close);
1418#endif