]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dsl_dir.c
WiP merge of libzfs_core (MFV r238590, r238592)
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dsl_dir.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24  * All rights reserved.
25  * Copyright (c) 2012 by Delphix. All rights reserved.
26  */
27
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>
36 #include <sys/spa.h>
37 #include <sys/metaslab.h>
38 #include <sys/zap.h>
39 #include <sys/zio.h>
40 #include <sys/arc.h>
41 #include <sys/sunddi.h>
42 #include <sys/zvol.h>
43 #ifdef _KERNEL
44 #include <sys/zfs_vfsops.h>
45 #endif
46 #include "zfs_namecheck.h"
47
48 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
49 static void dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd,
50     uint64_t value, dmu_tx_t *tx);
51
52 /* ARGSUSED */
53 static void
54 dsl_dir_evict(dmu_buf_t *db, void *arg)
55 {
56         dsl_dir_t *dd = arg;
57         dsl_pool_t *dp = dd->dd_pool;
58         int t;
59
60         for (t = 0; t < TXG_SIZE; t++) {
61                 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
62                 ASSERT(dd->dd_tempreserved[t] == 0);
63                 ASSERT(dd->dd_space_towrite[t] == 0);
64         }
65
66         if (dd->dd_parent)
67                 dsl_dir_close(dd->dd_parent, dd);
68
69         spa_close(dd->dd_pool->dp_spa, dd);
70
71         /*
72          * The props callback list should have been cleaned up by
73          * objset_evict().
74          */
75         list_destroy(&dd->dd_prop_cbs);
76         mutex_destroy(&dd->dd_lock);
77         kmem_free(dd, sizeof (dsl_dir_t));
78 }
79
80 int
81 dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
82     const char *tail, void *tag, dsl_dir_t **ddp)
83 {
84         dmu_buf_t *dbuf;
85         dsl_dir_t *dd;
86         int err;
87
88         ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
89             dsl_pool_sync_context(dp));
90
91         err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
92         if (err)
93                 return (err);
94         dd = dmu_buf_get_user(dbuf);
95 #ifdef ZFS_DEBUG
96         {
97                 dmu_object_info_t doi;
98                 dmu_object_info_from_db(dbuf, &doi);
99                 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
100                 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
101         }
102 #endif
103         if (dd == NULL) {
104                 dsl_dir_t *winner;
105
106                 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
107                 dd->dd_object = ddobj;
108                 dd->dd_dbuf = dbuf;
109                 dd->dd_pool = dp;
110                 dd->dd_phys = dbuf->db_data;
111                 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
112
113                 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
114                     offsetof(dsl_prop_cb_record_t, cbr_node));
115
116                 dsl_dir_snap_cmtime_update(dd);
117
118                 if (dd->dd_phys->dd_parent_obj) {
119                         err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
120                             NULL, dd, &dd->dd_parent);
121                         if (err)
122                                 goto errout;
123                         if (tail) {
124 #ifdef ZFS_DEBUG
125                                 uint64_t foundobj;
126
127                                 err = zap_lookup(dp->dp_meta_objset,
128                                     dd->dd_parent->dd_phys->dd_child_dir_zapobj,
129                                     tail, sizeof (foundobj), 1, &foundobj);
130                                 ASSERT(err || foundobj == ddobj);
131 #endif
132                                 (void) strcpy(dd->dd_myname, tail);
133                         } else {
134                                 err = zap_value_search(dp->dp_meta_objset,
135                                     dd->dd_parent->dd_phys->dd_child_dir_zapobj,
136                                     ddobj, 0, dd->dd_myname);
137                         }
138                         if (err)
139                                 goto errout;
140                 } else {
141                         (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
142                 }
143
144                 if (dsl_dir_is_clone(dd)) {
145                         dmu_buf_t *origin_bonus;
146                         dsl_dataset_phys_t *origin_phys;
147
148                         /*
149                          * We can't open the origin dataset, because
150                          * that would require opening this dsl_dir.
151                          * Just look at its phys directly instead.
152                          */
153                         err = dmu_bonus_hold(dp->dp_meta_objset,
154                             dd->dd_phys->dd_origin_obj, FTAG, &origin_bonus);
155                         if (err)
156                                 goto errout;
157                         origin_phys = origin_bonus->db_data;
158                         dd->dd_origin_txg =
159                             origin_phys->ds_creation_txg;
160                         dmu_buf_rele(origin_bonus, FTAG);
161                 }
162
163                 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
164                     dsl_dir_evict);
165                 if (winner) {
166                         if (dd->dd_parent)
167                                 dsl_dir_close(dd->dd_parent, dd);
168                         mutex_destroy(&dd->dd_lock);
169                         kmem_free(dd, sizeof (dsl_dir_t));
170                         dd = winner;
171                 } else {
172                         spa_open_ref(dp->dp_spa, dd);
173                 }
174         }
175
176         /*
177          * The dsl_dir_t has both open-to-close and instantiate-to-evict
178          * holds on the spa.  We need the open-to-close holds because
179          * otherwise the spa_refcnt wouldn't change when we open a
180          * dir which the spa also has open, so we could incorrectly
181          * think it was OK to unload/export/destroy the pool.  We need
182          * the instantiate-to-evict hold because the dsl_dir_t has a
183          * pointer to the dd_pool, which has a pointer to the spa_t.
184          */
185         spa_open_ref(dp->dp_spa, tag);
186         ASSERT3P(dd->dd_pool, ==, dp);
187         ASSERT3U(dd->dd_object, ==, ddobj);
188         ASSERT3P(dd->dd_dbuf, ==, dbuf);
189         *ddp = dd;
190         return (0);
191
192 errout:
193         if (dd->dd_parent)
194                 dsl_dir_close(dd->dd_parent, dd);
195         mutex_destroy(&dd->dd_lock);
196         kmem_free(dd, sizeof (dsl_dir_t));
197         dmu_buf_rele(dbuf, tag);
198         return (err);
199 }
200
201 void
202 dsl_dir_close(dsl_dir_t *dd, void *tag)
203 {
204         dprintf_dd(dd, "%s\n", "");
205         spa_close(dd->dd_pool->dp_spa, tag);
206         dmu_buf_rele(dd->dd_dbuf, tag);
207 }
208
209 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
210 void
211 dsl_dir_name(dsl_dir_t *dd, char *buf)
212 {
213         if (dd->dd_parent) {
214                 dsl_dir_name(dd->dd_parent, buf);
215                 (void) strcat(buf, "/");
216         } else {
217                 buf[0] = '\0';
218         }
219         if (!MUTEX_HELD(&dd->dd_lock)) {
220                 /*
221                  * recursive mutex so that we can use
222                  * dprintf_dd() with dd_lock held
223                  */
224                 mutex_enter(&dd->dd_lock);
225                 (void) strcat(buf, dd->dd_myname);
226                 mutex_exit(&dd->dd_lock);
227         } else {
228                 (void) strcat(buf, dd->dd_myname);
229         }
230 }
231
232 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
233 int
234 dsl_dir_namelen(dsl_dir_t *dd)
235 {
236         int result = 0;
237
238         if (dd->dd_parent) {
239                 /* parent's name + 1 for the "/" */
240                 result = dsl_dir_namelen(dd->dd_parent) + 1;
241         }
242
243         if (!MUTEX_HELD(&dd->dd_lock)) {
244                 /* see dsl_dir_name */
245                 mutex_enter(&dd->dd_lock);
246                 result += strlen(dd->dd_myname);
247                 mutex_exit(&dd->dd_lock);
248         } else {
249                 result += strlen(dd->dd_myname);
250         }
251
252         return (result);
253 }
254
255 static int
256 getcomponent(const char *path, char *component, const char **nextp)
257 {
258         char *p;
259         if ((path == NULL) || (path[0] == '\0'))
260                 return (ENOENT);
261         /* This would be a good place to reserve some namespace... */
262         p = strpbrk(path, "/@");
263         if (p && (p[1] == '/' || p[1] == '@')) {
264                 /* two separators in a row */
265                 return (EINVAL);
266         }
267         if (p == NULL || p == path) {
268                 /*
269                  * if the first thing is an @ or /, it had better be an
270                  * @ and it had better not have any more ats or slashes,
271                  * and it had better have something after the @.
272                  */
273                 if (p != NULL &&
274                     (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
275                         return (EINVAL);
276                 if (strlen(path) >= MAXNAMELEN)
277                         return (ENAMETOOLONG);
278                 (void) strcpy(component, path);
279                 p = NULL;
280         } else if (p[0] == '/') {
281                 if (p-path >= MAXNAMELEN)
282                         return (ENAMETOOLONG);
283                 (void) strncpy(component, path, p - path);
284                 component[p-path] = '\0';
285                 p++;
286         } else if (p[0] == '@') {
287                 /*
288                  * if the next separator is an @, there better not be
289                  * any more slashes.
290                  */
291                 if (strchr(path, '/'))
292                         return (EINVAL);
293                 if (p-path >= MAXNAMELEN)
294                         return (ENAMETOOLONG);
295                 (void) strncpy(component, path, p - path);
296                 component[p-path] = '\0';
297         } else {
298                 ASSERT(!"invalid p");
299         }
300         *nextp = p;
301         return (0);
302 }
303
304 /*
305  * same as dsl_open_dir, ignore the first component of name and use the
306  * spa instead
307  */
308 int
309 dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
310     dsl_dir_t **ddp, const char **tailp)
311 {
312         char buf[MAXNAMELEN];
313         const char *next, *nextnext = NULL;
314         int err;
315         dsl_dir_t *dd;
316         dsl_pool_t *dp;
317         uint64_t ddobj;
318         int openedspa = FALSE;
319
320         dprintf("%s\n", name);
321
322         err = getcomponent(name, buf, &next);
323         if (err)
324                 return (err);
325         if (spa == NULL) {
326                 err = spa_open(buf, &spa, FTAG);
327                 if (err) {
328                         dprintf("spa_open(%s) failed\n", buf);
329                         return (err);
330                 }
331                 openedspa = TRUE;
332
333                 /* XXX this assertion belongs in spa_open */
334                 ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
335         }
336
337         dp = spa_get_dsl(spa);
338
339         rw_enter(&dp->dp_config_rwlock, RW_READER);
340         err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
341         if (err) {
342                 rw_exit(&dp->dp_config_rwlock);
343                 if (openedspa)
344                         spa_close(spa, FTAG);
345                 return (err);
346         }
347
348         while (next != NULL) {
349                 dsl_dir_t *child_ds;
350                 err = getcomponent(next, buf, &nextnext);
351                 if (err)
352                         break;
353                 ASSERT(next[0] != '\0');
354                 if (next[0] == '@')
355                         break;
356                 dprintf("looking up %s in obj%lld\n",
357                     buf, dd->dd_phys->dd_child_dir_zapobj);
358
359                 err = zap_lookup(dp->dp_meta_objset,
360                     dd->dd_phys->dd_child_dir_zapobj,
361                     buf, sizeof (ddobj), 1, &ddobj);
362                 if (err) {
363                         if (err == ENOENT)
364                                 err = 0;
365                         break;
366                 }
367
368                 err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
369                 if (err)
370                         break;
371                 dsl_dir_close(dd, tag);
372                 dd = child_ds;
373                 next = nextnext;
374         }
375         rw_exit(&dp->dp_config_rwlock);
376
377         if (err) {
378                 dsl_dir_close(dd, tag);
379                 if (openedspa)
380                         spa_close(spa, FTAG);
381                 return (err);
382         }
383
384         /*
385          * It's an error if there's more than one component left, or
386          * tailp==NULL and there's any component left.
387          */
388         if (next != NULL &&
389             (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
390                 /* bad path name */
391                 dsl_dir_close(dd, tag);
392                 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
393                 err = ENOENT;
394         }
395         if (tailp)
396                 *tailp = next;
397         if (openedspa)
398                 spa_close(spa, FTAG);
399         *ddp = dd;
400         return (err);
401 }
402
403 /*
404  * Return the dsl_dir_t, and possibly the last component which couldn't
405  * be found in *tail.  Return NULL if the path is bogus, or if
406  * tail==NULL and we couldn't parse the whole name.  (*tail)[0] == '@'
407  * means that the last component is a snapshot.
408  */
409 int
410 dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
411 {
412         return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
413 }
414
415 uint64_t
416 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
417     dmu_tx_t *tx)
418 {
419         objset_t *mos = dp->dp_meta_objset;
420         uint64_t ddobj;
421         dsl_dir_phys_t *ddphys;
422         dmu_buf_t *dbuf;
423
424         ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
425             DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
426         if (pds) {
427                 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
428                     name, sizeof (uint64_t), 1, &ddobj, tx));
429         } else {
430                 /* it's the root dir */
431                 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
432                     DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
433         }
434         VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
435         dmu_buf_will_dirty(dbuf, tx);
436         ddphys = dbuf->db_data;
437
438         ddphys->dd_creation_time = gethrestime_sec();
439         if (pds)
440                 ddphys->dd_parent_obj = pds->dd_object;
441         ddphys->dd_props_zapobj = zap_create(mos,
442             DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
443         ddphys->dd_child_dir_zapobj = zap_create(mos,
444             DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
445         if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
446                 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
447         dmu_buf_rele(dbuf, FTAG);
448
449         return (ddobj);
450 }
451
452 /* ARGSUSED */
453 int
454 dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
455 {
456         dsl_dir_t *dd = arg1;
457         dsl_pool_t *dp = dd->dd_pool;
458         objset_t *mos = dp->dp_meta_objset;
459         int err;
460         uint64_t count;
461
462         /*
463          * There should be exactly two holds, both from
464          * dsl_dataset_destroy: one on the dd directory, and one on its
465          * head ds.  If there are more holds, then a concurrent thread is
466          * performing a lookup inside this dir while we're trying to destroy
467          * it.  To minimize this possibility, we perform this check only
468          * in syncing context and fail the operation if we encounter
469          * additional holds.  The dp_config_rwlock ensures that nobody else
470          * opens it after we check.
471          */
472         if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 2)
473                 return (EBUSY);
474
475         err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
476         if (err)
477                 return (err);
478         if (count != 0)
479                 return (EEXIST);
480
481         return (0);
482 }
483
484 void
485 dsl_dir_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
486 {
487         dsl_dir_t *dd = arg1;
488         objset_t *mos = dd->dd_pool->dp_meta_objset;
489         uint64_t obj;
490         dd_used_t t;
491
492         ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
493         ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
494
495         /*
496          * Remove our reservation. The impl() routine avoids setting the
497          * actual property, which would require the (already destroyed) ds.
498          */
499         dsl_dir_set_reservation_sync_impl(dd, 0, tx);
500
501         ASSERT0(dd->dd_phys->dd_used_bytes);
502         ASSERT0(dd->dd_phys->dd_reserved);
503         for (t = 0; t < DD_USED_NUM; t++)
504                 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
505
506         VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
507         VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
508         VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
509         VERIFY(0 == zap_remove(mos,
510             dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
511
512         obj = dd->dd_object;
513         dsl_dir_close(dd, tag);
514         VERIFY(0 == dmu_object_free(mos, obj, tx));
515 }
516
517 boolean_t
518 dsl_dir_is_clone(dsl_dir_t *dd)
519 {
520         return (dd->dd_phys->dd_origin_obj &&
521             (dd->dd_pool->dp_origin_snap == NULL ||
522             dd->dd_phys->dd_origin_obj !=
523             dd->dd_pool->dp_origin_snap->ds_object));
524 }
525
526 void
527 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
528 {
529         mutex_enter(&dd->dd_lock);
530         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
531             dd->dd_phys->dd_used_bytes);
532         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
533         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
534             dd->dd_phys->dd_reserved);
535         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
536             dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
537             (dd->dd_phys->dd_uncompressed_bytes * 100 /
538             dd->dd_phys->dd_compressed_bytes));
539         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
540             dd->dd_phys->dd_uncompressed_bytes);
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         }
552         mutex_exit(&dd->dd_lock);
553
554         rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
555         if (dsl_dir_is_clone(dd)) {
556                 dsl_dataset_t *ds;
557                 char buf[MAXNAMELEN];
558
559                 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
560                     dd->dd_phys->dd_origin_obj, FTAG, &ds));
561                 dsl_dataset_name(ds, buf);
562                 dsl_dataset_rele(ds, FTAG);
563                 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
564         }
565         rw_exit(&dd->dd_pool->dp_config_rwlock);
566 }
567
568 void
569 dsl_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
581 static int64_t
582 parent_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
589 void
590 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
591 {
592         ASSERT(dmu_tx_is_syncing(tx));
593
594         mutex_enter(&dd->dd_lock);
595         ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
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;
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
605 static uint64_t
606 dsl_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  */
625 uint64_t
626 dsl_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;
646         used = dd->dd_phys->dd_used_bytes;
647         if (!ondiskonly)
648                 used += dsl_dir_space_towrite(dd);
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
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
671         if (used > quota) {
672                 /* over quota */
673                 myspace = 0;
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
687 struct 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
694 static int
695 dsl_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;
701         uint64_t deferred = 0;
702         struct tempreserve *tr;
703         int retval = EDQUOT;
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];
720         used_on_disk = dd->dd_phys->dd_used_bytes;
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;
729                 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
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         /*
749          * Adjust the quota against the actual pool size at the root
750          * minus any outstanding deferred frees.
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) {
759                 spa_t *spa = dd->dd_pool->dp_spa;
760                 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
761                 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
762                 if (poolsize - deferred < quota) {
763                         quota = poolsize - deferred;
764                         retval = ENOSPC;
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          */
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;
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,
781                     quota>>10, asize>>10, retval);
782                 mutex_exit(&dd->dd_lock);
783                 return (retval);
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
793         tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
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  */
815 int
816 dsl_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
827         tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
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
837                 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
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
853                 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
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  */
874 void
875 dsl_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
886         while (tr = list_head(tr_list)) {
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
905 static void
906 dsl_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
915         est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
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  */
932 void
933 dsl_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 */
940 void
941 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
942     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
943 {
944         int64_t accounted_delta;
945         boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
946
947         ASSERT(dmu_tx_is_syncing(tx));
948         ASSERT(type < DD_USED_NUM);
949
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);
954         ASSERT(compressed >= 0 ||
955             dd->dd_phys->dd_compressed_bytes >= -compressed);
956         ASSERT(uncompressed >= 0 ||
957             dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
958         dmu_buf_will_dirty(dd->dd_dbuf, tx);
959         dd->dd_phys->dd_used_bytes += used;
960         dd->dd_phys->dd_uncompressed_bytes += uncompressed;
961         dd->dd_phys->dd_compressed_bytes += compressed;
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
968                 dd_used_t t;
969                 uint64_t u = 0;
970                 for (t = 0; t < DD_USED_NUM; t++)
971                         u += dd->dd_phys->dd_used_breakdown[t];
972                 ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
973 #endif
974         }
975         if (needlock)
976                 mutex_exit(&dd->dd_lock);
977
978         if (dd->dd_parent != NULL) {
979                 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
980                     accounted_delta, compressed, uncompressed, tx);
981                 dsl_dir_transfer_space(dd->dd_parent,
982                     used - accounted_delta,
983                     DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
984         }
985 }
986
987 void
988 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
989     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
990 {
991         boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
992
993         ASSERT(dmu_tx_is_syncing(tx));
994         ASSERT(oldtype < DD_USED_NUM);
995         ASSERT(newtype < DD_USED_NUM);
996
997         if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
998                 return;
999
1000         if (needlock)
1001                 mutex_enter(&dd->dd_lock);
1002         ASSERT(delta > 0 ?
1003             dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
1004             dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
1005         ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
1006         dmu_buf_will_dirty(dd->dd_dbuf, tx);
1007         dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
1008         dd->dd_phys->dd_used_breakdown[newtype] += delta;
1009         if (needlock)
1010                 mutex_exit(&dd->dd_lock);
1011 }
1012
1013 static int
1014 dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
1015 {
1016         dsl_dataset_t *ds = arg1;
1017         dsl_dir_t *dd = ds->ds_dir;
1018         dsl_prop_setarg_t *psa = arg2;
1019         int err;
1020         uint64_t towrite;
1021
1022         if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1023                 return (err);
1024
1025         if (psa->psa_effective_value == 0)
1026                 return (0);
1027
1028         mutex_enter(&dd->dd_lock);
1029         /*
1030          * If we are doing the preliminary check in open context, and
1031          * there are pending changes, then don't fail it, since the
1032          * pending changes could under-estimate the amount of space to be
1033          * freed up.
1034          */
1035         towrite = dsl_dir_space_towrite(dd);
1036         if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1037             (psa->psa_effective_value < dd->dd_phys->dd_reserved ||
1038             psa->psa_effective_value < dd->dd_phys->dd_used_bytes + towrite)) {
1039                 err = ENOSPC;
1040         }
1041         mutex_exit(&dd->dd_lock);
1042         return (err);
1043 }
1044
1045 extern dsl_syncfunc_t dsl_prop_set_sync;
1046
1047 static void
1048 dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1049 {
1050         dsl_dataset_t *ds = arg1;
1051         dsl_dir_t *dd = ds->ds_dir;
1052         dsl_prop_setarg_t *psa = arg2;
1053         uint64_t effective_value = psa->psa_effective_value;
1054
1055         dsl_prop_set_sync(ds, psa, tx);
1056         DSL_PROP_CHECK_PREDICTION(dd, psa);
1057
1058         dmu_buf_will_dirty(dd->dd_dbuf, tx);
1059
1060         mutex_enter(&dd->dd_lock);
1061         dd->dd_phys->dd_quota = effective_value;
1062         mutex_exit(&dd->dd_lock);
1063 }
1064
1065 int
1066 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1067 {
1068         dsl_dir_t *dd;
1069         dsl_dataset_t *ds;
1070         dsl_prop_setarg_t psa;
1071         int err;
1072
1073         dsl_prop_setarg_init_uint64(&psa, "quota", source, &quota);
1074
1075         err = dsl_dataset_hold(ddname, FTAG, &ds);
1076         if (err)
1077                 return (err);
1078
1079         err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1080         if (err) {
1081                 dsl_dataset_rele(ds, FTAG);
1082                 return (err);
1083         }
1084
1085         ASSERT(ds->ds_dir == dd);
1086
1087         /*
1088          * If someone removes a file, then tries to set the quota, we want to
1089          * make sure the file freeing takes effect.
1090          */
1091         txg_wait_open(dd->dd_pool, 0);
1092
1093         err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1094             dsl_dir_set_quota_sync, ds, &psa, 0);
1095
1096         dsl_dir_close(dd, FTAG);
1097         dsl_dataset_rele(ds, FTAG);
1098         return (err);
1099 }
1100
1101 int
1102 dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1103 {
1104         dsl_dataset_t *ds = arg1;
1105         dsl_dir_t *dd = ds->ds_dir;
1106         dsl_prop_setarg_t *psa = arg2;
1107         uint64_t effective_value;
1108         uint64_t used, avail;
1109         int err;
1110
1111         if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1112                 return (err);
1113
1114         effective_value = psa->psa_effective_value;
1115
1116         /*
1117          * If we are doing the preliminary check in open context, the
1118          * space estimates may be inaccurate.
1119          */
1120         if (!dmu_tx_is_syncing(tx))
1121                 return (0);
1122
1123         mutex_enter(&dd->dd_lock);
1124         used = dd->dd_phys->dd_used_bytes;
1125         mutex_exit(&dd->dd_lock);
1126
1127         if (dd->dd_parent) {
1128                 avail = dsl_dir_space_available(dd->dd_parent,
1129                     NULL, 0, FALSE);
1130         } else {
1131                 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1132         }
1133
1134         if (MAX(used, effective_value) > MAX(used, dd->dd_phys->dd_reserved)) {
1135                 uint64_t delta = MAX(used, effective_value) -
1136                     MAX(used, dd->dd_phys->dd_reserved);
1137
1138                 if (delta > avail)
1139                         return (ENOSPC);
1140                 if (dd->dd_phys->dd_quota > 0 &&
1141                     effective_value > dd->dd_phys->dd_quota)
1142                         return (ENOSPC);
1143         }
1144
1145         return (0);
1146 }
1147
1148 static void
1149 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1150 {
1151         uint64_t used;
1152         int64_t delta;
1153
1154         dmu_buf_will_dirty(dd->dd_dbuf, tx);
1155
1156         mutex_enter(&dd->dd_lock);
1157         used = dd->dd_phys->dd_used_bytes;
1158         delta = MAX(used, value) - MAX(used, dd->dd_phys->dd_reserved);
1159         dd->dd_phys->dd_reserved = value;
1160
1161         if (dd->dd_parent != NULL) {
1162                 /* Roll up this additional usage into our ancestors */
1163                 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1164                     delta, 0, 0, tx);
1165         }
1166         mutex_exit(&dd->dd_lock);
1167 }
1168
1169 static void
1170 dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1171 {
1172         dsl_dataset_t *ds = arg1;
1173         dsl_dir_t *dd = ds->ds_dir;
1174         dsl_prop_setarg_t *psa = arg2;
1175         uint64_t value = psa->psa_effective_value;
1176
1177         dsl_prop_set_sync(ds, psa, tx);
1178         DSL_PROP_CHECK_PREDICTION(dd, psa);
1179
1180         dsl_dir_set_reservation_sync_impl(dd, value, tx);
1181
1182         spa_history_log_internal_dd(dd, "set reservation", tx,
1183             "reservation=%lld", (longlong_t)value);
1184 }
1185
1186 int
1187 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1188     uint64_t reservation)
1189 {
1190         dsl_dir_t *dd;
1191         dsl_dataset_t *ds;
1192         dsl_prop_setarg_t psa;
1193         int err;
1194
1195         dsl_prop_setarg_init_uint64(&psa, "reservation", source, &reservation);
1196
1197         err = dsl_dataset_hold(ddname, FTAG, &ds);
1198         if (err)
1199                 return (err);
1200
1201         err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1202         if (err) {
1203                 dsl_dataset_rele(ds, FTAG);
1204                 return (err);
1205         }
1206
1207         ASSERT(ds->ds_dir == dd);
1208
1209         err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
1210             dsl_dir_set_reservation_sync, ds, &psa, 0);
1211
1212         dsl_dir_close(dd, FTAG);
1213         dsl_dataset_rele(ds, FTAG);
1214         return (err);
1215 }
1216
1217 static dsl_dir_t *
1218 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1219 {
1220         for (; ds1; ds1 = ds1->dd_parent) {
1221                 dsl_dir_t *dd;
1222                 for (dd = ds2; dd; dd = dd->dd_parent) {
1223                         if (ds1 == dd)
1224                                 return (dd);
1225                 }
1226         }
1227         return (NULL);
1228 }
1229
1230 /*
1231  * If delta is applied to dd, how much of that delta would be applied to
1232  * ancestor?  Syncing context only.
1233  */
1234 static int64_t
1235 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1236 {
1237         if (dd == ancestor)
1238                 return (delta);
1239
1240         mutex_enter(&dd->dd_lock);
1241         delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1242         mutex_exit(&dd->dd_lock);
1243         return (would_change(dd->dd_parent, delta, ancestor));
1244 }
1245
1246 struct renamearg {
1247         dsl_dir_t *newparent;
1248         const char *mynewname;
1249         boolean_t allowmounted;
1250 };
1251
1252 static int
1253 dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1254 {
1255         dsl_dir_t *dd = arg1;
1256         struct renamearg *ra = arg2;
1257         dsl_pool_t *dp = dd->dd_pool;
1258         objset_t *mos = dp->dp_meta_objset;
1259         int err;
1260         uint64_t val;
1261
1262         /*
1263          * There should only be one reference, from dmu_objset_rename().
1264          * Fleeting holds are also possible (eg, from "zfs list" getting
1265          * stats), but any that are present in open context will likely
1266          * be gone by syncing context, so only fail from syncing
1267          * context.
1268          * Don't check if we allow renaming of busy (mounted) dataset.
1269          */
1270         if (!ra->allowmounted && dmu_tx_is_syncing(tx) &&
1271             dmu_buf_refcount(dd->dd_dbuf) > 1) {
1272                 return (EBUSY);
1273         }
1274
1275         /* check for existing name */
1276         err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1277             ra->mynewname, 8, 1, &val);
1278         if (err == 0)
1279                 return (EEXIST);
1280         if (err != ENOENT)
1281                 return (err);
1282
1283         if (ra->newparent != dd->dd_parent) {
1284                 /* is there enough space? */
1285                 uint64_t myspace =
1286                     MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1287
1288                 /* no rename into our descendant */
1289                 if (closest_common_ancestor(dd, ra->newparent) == dd)
1290                         return (EINVAL);
1291
1292                 if (err = dsl_dir_transfer_possible(dd->dd_parent,
1293                     ra->newparent, myspace))
1294                         return (err);
1295         }
1296
1297         return (0);
1298 }
1299
1300 static void
1301 dsl_dir_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1302 {
1303         char oldname[MAXPATHLEN], newname[MAXPATHLEN];
1304         dsl_dir_t *dd = arg1;
1305         struct renamearg *ra = arg2;
1306         dsl_pool_t *dp = dd->dd_pool;
1307         objset_t *mos = dp->dp_meta_objset;
1308         int err;
1309         char namebuf[MAXNAMELEN];
1310
1311         ASSERT(ra->allowmounted || dmu_buf_refcount(dd->dd_dbuf) <= 2);
1312
1313         /* Log this before we change the name. */
1314         dsl_dir_name(ra->newparent, namebuf);
1315         spa_history_log_internal_dd(dd, "rename", tx,
1316             "-> %s/%s", namebuf, ra->mynewname);
1317
1318         if (ra->newparent != dd->dd_parent) {
1319                 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1320                     -dd->dd_phys->dd_used_bytes,
1321                     -dd->dd_phys->dd_compressed_bytes,
1322                     -dd->dd_phys->dd_uncompressed_bytes, tx);
1323                 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
1324                     dd->dd_phys->dd_used_bytes,
1325                     dd->dd_phys->dd_compressed_bytes,
1326                     dd->dd_phys->dd_uncompressed_bytes, tx);
1327
1328                 if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
1329                         uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
1330                             dd->dd_phys->dd_used_bytes;
1331
1332                         dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1333                             -unused_rsrv, 0, 0, tx);
1334                         dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
1335                             unused_rsrv, 0, 0, tx);
1336                 }
1337         }
1338
1339         dmu_buf_will_dirty(dd->dd_dbuf, tx);
1340
1341         /* remove from old parent zapobj */
1342         dsl_dir_name(dd, oldname);
1343         err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1344             dd->dd_myname, tx);
1345         ASSERT0(err);
1346
1347         (void) strcpy(dd->dd_myname, ra->mynewname);
1348         dsl_dir_close(dd->dd_parent, dd);
1349         dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1350         VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1351             ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1352
1353         /* add to new parent zapobj */
1354         err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1355             dd->dd_myname, 8, 1, &dd->dd_object, tx);
1356         ASSERT0(err);
1357         dsl_dir_name(dd, newname);
1358 #ifdef _KERNEL
1359         zfsvfs_update_fromname(oldname, newname);
1360         zvol_rename_minors(oldname, newname);
1361 #endif
1362
1363 }
1364
1365 int
1366 dsl_dir_rename(dsl_dir_t *dd, const char *newname, int flags)
1367 {
1368         struct renamearg ra;
1369         int err;
1370
1371         /* new parent should exist */
1372         err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
1373         if (err)
1374                 return (err);
1375
1376         /* can't rename to different pool */
1377         if (dd->dd_pool != ra.newparent->dd_pool) {
1378                 err = ENXIO;
1379                 goto out;
1380         }
1381
1382         /* new name should not already exist */
1383         if (ra.mynewname == NULL) {
1384                 err = EEXIST;
1385                 goto out;
1386         }
1387
1388         ra.allowmounted = !!(flags & ZFS_RENAME_ALLOW_MOUNTED);
1389
1390         err = dsl_sync_task_do(dd->dd_pool,
1391             dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1392
1393 out:
1394         dsl_dir_close(ra.newparent, FTAG);
1395         return (err);
1396 }
1397
1398 int
1399 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1400 {
1401         dsl_dir_t *ancestor;
1402         int64_t adelta;
1403         uint64_t avail;
1404
1405         ancestor = closest_common_ancestor(sdd, tdd);
1406         adelta = would_change(sdd, -space, ancestor);
1407         avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1408         if (avail < space)
1409                 return (ENOSPC);
1410
1411         return (0);
1412 }
1413
1414 timestruc_t
1415 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1416 {
1417         timestruc_t t;
1418
1419         mutex_enter(&dd->dd_lock);
1420         t = dd->dd_snap_cmtime;
1421         mutex_exit(&dd->dd_lock);
1422
1423         return (t);
1424 }
1425
1426 void
1427 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1428 {
1429         timestruc_t t;
1430
1431         gethrestime(&t);
1432         mutex_enter(&dd->dd_lock);
1433         dd->dd_snap_cmtime = t;
1434         mutex_exit(&dd->dd_lock);
1435 }