]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dsl_dataset.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dsl_dataset.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) 2013 by Delphix. All rights reserved.
24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25  * Portions Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org>
26  */
27
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dmu_impl.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/arc.h>
37 #include <sys/zio.h>
38 #include <sys/zap.h>
39 #include <sys/zfeature.h>
40 #include <sys/unique.h>
41 #include <sys/zfs_context.h>
42 #include <sys/zfs_ioctl.h>
43 #include <sys/spa.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_onexit.h>
46 #include <sys/zvol.h>
47 #include <sys/dsl_scan.h>
48 #include <sys/dsl_deadlist.h>
49 #include <sys/dsl_destroy.h>
50 #include <sys/dsl_userhold.h>
51
52 #define SWITCH64(x, y) \
53         { \
54                 uint64_t __tmp = (x); \
55                 (x) = (y); \
56                 (y) = __tmp; \
57         }
58
59 #define DS_REF_MAX      (1ULL << 62)
60
61 #define DSL_DEADLIST_BLOCKSIZE  SPA_MAXBLOCKSIZE
62
63 /*
64  * Figure out how much of this delta should be propogated to the dsl_dir
65  * layer.  If there's a refreservation, that space has already been
66  * partially accounted for in our ancestors.
67  */
68 static int64_t
69 parent_delta(dsl_dataset_t *ds, int64_t delta)
70 {
71         uint64_t old_bytes, new_bytes;
72
73         if (ds->ds_reserved == 0)
74                 return (delta);
75
76         old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
77         new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
78
79         ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
80         return (new_bytes - old_bytes);
81 }
82
83 void
84 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
85 {
86         int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
87         int compressed = BP_GET_PSIZE(bp);
88         int uncompressed = BP_GET_UCSIZE(bp);
89         int64_t delta;
90
91         dprintf_bp(bp, "ds=%p", ds);
92
93         ASSERT(dmu_tx_is_syncing(tx));
94         /* It could have been compressed away to nothing */
95         if (BP_IS_HOLE(bp))
96                 return;
97         ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
98         ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
99         if (ds == NULL) {
100                 dsl_pool_mos_diduse_space(tx->tx_pool,
101                     used, compressed, uncompressed);
102                 return;
103         }
104         dmu_buf_will_dirty(ds->ds_dbuf, tx);
105
106         mutex_enter(&ds->ds_dir->dd_lock);
107         mutex_enter(&ds->ds_lock);
108         delta = parent_delta(ds, used);
109         ds->ds_phys->ds_referenced_bytes += used;
110         ds->ds_phys->ds_compressed_bytes += compressed;
111         ds->ds_phys->ds_uncompressed_bytes += uncompressed;
112         ds->ds_phys->ds_unique_bytes += used;
113         mutex_exit(&ds->ds_lock);
114         dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
115             compressed, uncompressed, tx);
116         dsl_dir_transfer_space(ds->ds_dir, used - delta,
117             DD_USED_REFRSRV, DD_USED_HEAD, tx);
118         mutex_exit(&ds->ds_dir->dd_lock);
119 }
120
121 int
122 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
123     boolean_t async)
124 {
125         if (BP_IS_HOLE(bp))
126                 return (0);
127
128         ASSERT(dmu_tx_is_syncing(tx));
129         ASSERT(bp->blk_birth <= tx->tx_txg);
130
131         int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
132         int compressed = BP_GET_PSIZE(bp);
133         int uncompressed = BP_GET_UCSIZE(bp);
134
135         ASSERT(used > 0);
136         if (ds == NULL) {
137                 dsl_free(tx->tx_pool, tx->tx_txg, bp);
138                 dsl_pool_mos_diduse_space(tx->tx_pool,
139                     -used, -compressed, -uncompressed);
140                 return (used);
141         }
142         ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
143
144         ASSERT(!dsl_dataset_is_snapshot(ds));
145         dmu_buf_will_dirty(ds->ds_dbuf, tx);
146
147         if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
148                 int64_t delta;
149
150                 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
151                 dsl_free(tx->tx_pool, tx->tx_txg, bp);
152
153                 mutex_enter(&ds->ds_dir->dd_lock);
154                 mutex_enter(&ds->ds_lock);
155                 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
156                     !DS_UNIQUE_IS_ACCURATE(ds));
157                 delta = parent_delta(ds, -used);
158                 ds->ds_phys->ds_unique_bytes -= used;
159                 mutex_exit(&ds->ds_lock);
160                 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
161                     delta, -compressed, -uncompressed, tx);
162                 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
163                     DD_USED_REFRSRV, DD_USED_HEAD, tx);
164                 mutex_exit(&ds->ds_dir->dd_lock);
165         } else {
166                 dprintf_bp(bp, "putting on dead list: %s", "");
167                 if (async) {
168                         /*
169                          * We are here as part of zio's write done callback,
170                          * which means we're a zio interrupt thread.  We can't
171                          * call dsl_deadlist_insert() now because it may block
172                          * waiting for I/O.  Instead, put bp on the deferred
173                          * queue and let dsl_pool_sync() finish the job.
174                          */
175                         bplist_append(&ds->ds_pending_deadlist, bp);
176                 } else {
177                         dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
178                 }
179                 ASSERT3U(ds->ds_prev->ds_object, ==,
180                     ds->ds_phys->ds_prev_snap_obj);
181                 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
182                 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
183                 if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
184                     ds->ds_object && bp->blk_birth >
185                     ds->ds_prev->ds_phys->ds_prev_snap_txg) {
186                         dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
187                         mutex_enter(&ds->ds_prev->ds_lock);
188                         ds->ds_prev->ds_phys->ds_unique_bytes += used;
189                         mutex_exit(&ds->ds_prev->ds_lock);
190                 }
191                 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
192                         dsl_dir_transfer_space(ds->ds_dir, used,
193                             DD_USED_HEAD, DD_USED_SNAP, tx);
194                 }
195         }
196         mutex_enter(&ds->ds_lock);
197         ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
198         ds->ds_phys->ds_referenced_bytes -= used;
199         ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
200         ds->ds_phys->ds_compressed_bytes -= compressed;
201         ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
202         ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
203         mutex_exit(&ds->ds_lock);
204
205         return (used);
206 }
207
208 uint64_t
209 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
210 {
211         uint64_t trysnap = 0;
212
213         if (ds == NULL)
214                 return (0);
215         /*
216          * The snapshot creation could fail, but that would cause an
217          * incorrect FALSE return, which would only result in an
218          * overestimation of the amount of space that an operation would
219          * consume, which is OK.
220          *
221          * There's also a small window where we could miss a pending
222          * snapshot, because we could set the sync task in the quiescing
223          * phase.  So this should only be used as a guess.
224          */
225         if (ds->ds_trysnap_txg >
226             spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
227                 trysnap = ds->ds_trysnap_txg;
228         return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
229 }
230
231 boolean_t
232 dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
233     uint64_t blk_birth)
234 {
235         if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
236                 return (B_FALSE);
237
238         ddt_prefetch(dsl_dataset_get_spa(ds), bp);
239
240         return (B_TRUE);
241 }
242
243 /* ARGSUSED */
244 static void
245 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
246 {
247         dsl_dataset_t *ds = dsv;
248
249         ASSERT(ds->ds_owner == NULL);
250
251         unique_remove(ds->ds_fsid_guid);
252
253         if (ds->ds_objset != NULL)
254                 dmu_objset_evict(ds->ds_objset);
255
256         if (ds->ds_prev) {
257                 dsl_dataset_rele(ds->ds_prev, ds);
258                 ds->ds_prev = NULL;
259         }
260
261         bplist_destroy(&ds->ds_pending_deadlist);
262         if (ds->ds_phys->ds_deadlist_obj != 0)
263                 dsl_deadlist_close(&ds->ds_deadlist);
264         if (ds->ds_dir)
265                 dsl_dir_rele(ds->ds_dir, ds);
266
267         ASSERT(!list_link_active(&ds->ds_synced_link));
268
269         if (mutex_owned(&ds->ds_lock))
270                 mutex_exit(&ds->ds_lock);
271         mutex_destroy(&ds->ds_lock);
272         if (mutex_owned(&ds->ds_opening_lock))
273                 mutex_exit(&ds->ds_opening_lock);
274         mutex_destroy(&ds->ds_opening_lock);
275         refcount_destroy(&ds->ds_longholds);
276
277         kmem_free(ds, sizeof (dsl_dataset_t));
278 }
279
280 int
281 dsl_dataset_get_snapname(dsl_dataset_t *ds)
282 {
283         dsl_dataset_phys_t *headphys;
284         int err;
285         dmu_buf_t *headdbuf;
286         dsl_pool_t *dp = ds->ds_dir->dd_pool;
287         objset_t *mos = dp->dp_meta_objset;
288
289         if (ds->ds_snapname[0])
290                 return (0);
291         if (ds->ds_phys->ds_next_snap_obj == 0)
292                 return (0);
293
294         err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
295             FTAG, &headdbuf);
296         if (err != 0)
297                 return (err);
298         headphys = headdbuf->db_data;
299         err = zap_value_search(dp->dp_meta_objset,
300             headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
301         dmu_buf_rele(headdbuf, FTAG);
302         return (err);
303 }
304
305 int
306 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
307 {
308         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
309         uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
310         matchtype_t mt;
311         int err;
312
313         if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
314                 mt = MT_FIRST;
315         else
316                 mt = MT_EXACT;
317
318         err = zap_lookup_norm(mos, snapobj, name, 8, 1,
319             value, mt, NULL, 0, NULL);
320         if (err == ENOTSUP && mt == MT_FIRST)
321                 err = zap_lookup(mos, snapobj, name, 8, 1, value);
322         return (err);
323 }
324
325 int
326 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
327 {
328         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
329         uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
330         matchtype_t mt;
331         int err;
332
333         dsl_dir_snap_cmtime_update(ds->ds_dir);
334
335         if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
336                 mt = MT_FIRST;
337         else
338                 mt = MT_EXACT;
339
340         err = zap_remove_norm(mos, snapobj, name, mt, tx);
341         if (err == ENOTSUP && mt == MT_FIRST)
342                 err = zap_remove(mos, snapobj, name, tx);
343         return (err);
344 }
345
346 int
347 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
348     dsl_dataset_t **dsp)
349 {
350         objset_t *mos = dp->dp_meta_objset;
351         dmu_buf_t *dbuf;
352         dsl_dataset_t *ds;
353         int err;
354         dmu_object_info_t doi;
355
356         ASSERT(dsl_pool_config_held(dp));
357
358         err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
359         if (err != 0)
360                 return (err);
361
362         /* Make sure dsobj has the correct object type. */
363         dmu_object_info_from_db(dbuf, &doi);
364         if (doi.doi_type != DMU_OT_DSL_DATASET) {
365                 dmu_buf_rele(dbuf, tag);
366                 return (SET_ERROR(EINVAL));
367         }
368
369         ds = dmu_buf_get_user(dbuf);
370         if (ds == NULL) {
371                 dsl_dataset_t *winner = NULL;
372
373                 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
374                 ds->ds_dbuf = dbuf;
375                 ds->ds_object = dsobj;
376                 ds->ds_phys = dbuf->db_data;
377
378                 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
379                 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
380                 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
381                 refcount_create(&ds->ds_longholds);
382
383                 bplist_create(&ds->ds_pending_deadlist);
384                 dsl_deadlist_open(&ds->ds_deadlist,
385                     mos, ds->ds_phys->ds_deadlist_obj);
386
387                 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
388                     offsetof(dmu_sendarg_t, dsa_link));
389
390                 if (err == 0) {
391                         err = dsl_dir_hold_obj(dp,
392                             ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
393                 }
394                 if (err != 0) {
395                         mutex_destroy(&ds->ds_lock);
396                         mutex_destroy(&ds->ds_opening_lock);
397                         refcount_destroy(&ds->ds_longholds);
398                         bplist_destroy(&ds->ds_pending_deadlist);
399                         dsl_deadlist_close(&ds->ds_deadlist);
400                         kmem_free(ds, sizeof (dsl_dataset_t));
401                         dmu_buf_rele(dbuf, tag);
402                         return (err);
403                 }
404
405                 if (!dsl_dataset_is_snapshot(ds)) {
406                         ds->ds_snapname[0] = '\0';
407                         if (ds->ds_phys->ds_prev_snap_obj != 0) {
408                                 err = dsl_dataset_hold_obj(dp,
409                                     ds->ds_phys->ds_prev_snap_obj,
410                                     ds, &ds->ds_prev);
411                         }
412                 } else {
413                         if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
414                                 err = dsl_dataset_get_snapname(ds);
415                         if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
416                                 err = zap_count(
417                                     ds->ds_dir->dd_pool->dp_meta_objset,
418                                     ds->ds_phys->ds_userrefs_obj,
419                                     &ds->ds_userrefs);
420                         }
421                 }
422
423                 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
424                         err = dsl_prop_get_int_ds(ds,
425                             zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
426                             &ds->ds_reserved);
427                         if (err == 0) {
428                                 err = dsl_prop_get_int_ds(ds,
429                                     zfs_prop_to_name(ZFS_PROP_REFQUOTA),
430                                     &ds->ds_quota);
431                         }
432                 } else {
433                         ds->ds_reserved = ds->ds_quota = 0;
434                 }
435
436                 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
437                     &ds->ds_phys, dsl_dataset_evict)) != NULL) {
438                         bplist_destroy(&ds->ds_pending_deadlist);
439                         dsl_deadlist_close(&ds->ds_deadlist);
440                         if (ds->ds_prev)
441                                 dsl_dataset_rele(ds->ds_prev, ds);
442                         dsl_dir_rele(ds->ds_dir, ds);
443                         mutex_destroy(&ds->ds_lock);
444                         mutex_destroy(&ds->ds_opening_lock);
445                         refcount_destroy(&ds->ds_longholds);
446                         kmem_free(ds, sizeof (dsl_dataset_t));
447                         if (err != 0) {
448                                 dmu_buf_rele(dbuf, tag);
449                                 return (err);
450                         }
451                         ds = winner;
452                 } else {
453                         ds->ds_fsid_guid =
454                             unique_insert(ds->ds_phys->ds_fsid_guid);
455                 }
456         }
457         ASSERT3P(ds->ds_dbuf, ==, dbuf);
458         ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
459         ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
460             spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
461             dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
462         *dsp = ds;
463         return (0);
464 }
465
466 int
467 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
468     void *tag, dsl_dataset_t **dsp)
469 {
470         dsl_dir_t *dd;
471         const char *snapname;
472         uint64_t obj;
473         int err = 0;
474
475         err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
476         if (err != 0)
477                 return (err);
478
479         ASSERT(dsl_pool_config_held(dp));
480         obj = dd->dd_phys->dd_head_dataset_obj;
481         if (obj != 0)
482                 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
483         else
484                 err = SET_ERROR(ENOENT);
485
486         /* we may be looking for a snapshot */
487         if (err == 0 && snapname != NULL) {
488                 dsl_dataset_t *ds;
489
490                 if (*snapname++ != '@') {
491                         dsl_dataset_rele(*dsp, tag);
492                         dsl_dir_rele(dd, FTAG);
493                         return (SET_ERROR(ENOENT));
494                 }
495
496                 dprintf("looking for snapshot '%s'\n", snapname);
497                 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
498                 if (err == 0)
499                         err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
500                 dsl_dataset_rele(*dsp, tag);
501
502                 if (err == 0) {
503                         mutex_enter(&ds->ds_lock);
504                         if (ds->ds_snapname[0] == 0)
505                                 (void) strlcpy(ds->ds_snapname, snapname,
506                                     sizeof (ds->ds_snapname));
507                         mutex_exit(&ds->ds_lock);
508                         *dsp = ds;
509                 }
510         }
511
512         dsl_dir_rele(dd, FTAG);
513         return (err);
514 }
515
516 int
517 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
518     void *tag, dsl_dataset_t **dsp)
519 {
520         int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
521         if (err != 0)
522                 return (err);
523         if (!dsl_dataset_tryown(*dsp, tag)) {
524                 dsl_dataset_rele(*dsp, tag);
525                 *dsp = NULL;
526                 return (SET_ERROR(EBUSY));
527         }
528         return (0);
529 }
530
531 int
532 dsl_dataset_own(dsl_pool_t *dp, const char *name,
533     void *tag, dsl_dataset_t **dsp)
534 {
535         int err = dsl_dataset_hold(dp, name, tag, dsp);
536         if (err != 0)
537                 return (err);
538         if (!dsl_dataset_tryown(*dsp, tag)) {
539                 dsl_dataset_rele(*dsp, tag);
540                 return (SET_ERROR(EBUSY));
541         }
542         return (0);
543 }
544
545 /*
546  * See the comment above dsl_pool_hold() for details.  In summary, a long
547  * hold is used to prevent destruction of a dataset while the pool hold
548  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
549  *
550  * The dataset and pool must be held when this function is called.  After it
551  * is called, the pool hold may be released while the dataset is still held
552  * and accessed.
553  */
554 void
555 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
556 {
557         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
558         (void) refcount_add(&ds->ds_longholds, tag);
559 }
560
561 void
562 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
563 {
564         (void) refcount_remove(&ds->ds_longholds, tag);
565 }
566
567 /* Return B_TRUE if there are any long holds on this dataset. */
568 boolean_t
569 dsl_dataset_long_held(dsl_dataset_t *ds)
570 {
571         return (!refcount_is_zero(&ds->ds_longholds));
572 }
573
574 void
575 dsl_dataset_name(dsl_dataset_t *ds, char *name)
576 {
577         if (ds == NULL) {
578                 (void) strcpy(name, "mos");
579         } else {
580                 dsl_dir_name(ds->ds_dir, name);
581                 VERIFY0(dsl_dataset_get_snapname(ds));
582                 if (ds->ds_snapname[0]) {
583                         (void) strcat(name, "@");
584                         /*
585                          * We use a "recursive" mutex so that we
586                          * can call dprintf_ds() with ds_lock held.
587                          */
588                         if (!MUTEX_HELD(&ds->ds_lock)) {
589                                 mutex_enter(&ds->ds_lock);
590                                 (void) strcat(name, ds->ds_snapname);
591                                 mutex_exit(&ds->ds_lock);
592                         } else {
593                                 (void) strcat(name, ds->ds_snapname);
594                         }
595                 }
596         }
597 }
598
599 static int
600 dsl_dataset_namelen(dsl_dataset_t *ds)
601 {
602         int result;
603
604         if (ds == NULL) {
605                 result = 3;     /* "mos" */
606         } else {
607                 result = dsl_dir_namelen(ds->ds_dir);
608                 VERIFY0(dsl_dataset_get_snapname(ds));
609                 if (ds->ds_snapname[0]) {
610                         ++result;       /* adding one for the @-sign */
611                         if (!MUTEX_HELD(&ds->ds_lock)) {
612                                 mutex_enter(&ds->ds_lock);
613                                 result += strlen(ds->ds_snapname);
614                                 mutex_exit(&ds->ds_lock);
615                         } else {
616                                 result += strlen(ds->ds_snapname);
617                         }
618                 }
619         }
620
621         return (result);
622 }
623
624 void
625 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
626 {
627         dmu_buf_rele(ds->ds_dbuf, tag);
628 }
629
630 void
631 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
632 {
633         ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
634
635         mutex_enter(&ds->ds_lock);
636         ds->ds_owner = NULL;
637         mutex_exit(&ds->ds_lock);
638         dsl_dataset_long_rele(ds, tag);
639         if (ds->ds_dbuf != NULL)
640                 dsl_dataset_rele(ds, tag);
641         else
642                 dsl_dataset_evict(NULL, ds);
643 }
644
645 boolean_t
646 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
647 {
648         boolean_t gotit = FALSE;
649
650         mutex_enter(&ds->ds_lock);
651         if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
652                 ds->ds_owner = tag;
653                 dsl_dataset_long_hold(ds, tag);
654                 gotit = TRUE;
655         }
656         mutex_exit(&ds->ds_lock);
657         return (gotit);
658 }
659
660 uint64_t
661 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
662     uint64_t flags, dmu_tx_t *tx)
663 {
664         dsl_pool_t *dp = dd->dd_pool;
665         dmu_buf_t *dbuf;
666         dsl_dataset_phys_t *dsphys;
667         uint64_t dsobj;
668         objset_t *mos = dp->dp_meta_objset;
669
670         if (origin == NULL)
671                 origin = dp->dp_origin_snap;
672
673         ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
674         ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
675         ASSERT(dmu_tx_is_syncing(tx));
676         ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
677
678         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
679             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
680         VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
681         dmu_buf_will_dirty(dbuf, tx);
682         dsphys = dbuf->db_data;
683         bzero(dsphys, sizeof (dsl_dataset_phys_t));
684         dsphys->ds_dir_obj = dd->dd_object;
685         dsphys->ds_flags = flags;
686         dsphys->ds_fsid_guid = unique_create();
687         do {
688                 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
689                     sizeof (dsphys->ds_guid));
690         } while (dsphys->ds_guid == 0);
691         dsphys->ds_snapnames_zapobj =
692             zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
693             DMU_OT_NONE, 0, tx);
694         dsphys->ds_creation_time = gethrestime_sec();
695         dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
696
697         if (origin == NULL) {
698                 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
699         } else {
700                 dsl_dataset_t *ohds; /* head of the origin snapshot */
701
702                 dsphys->ds_prev_snap_obj = origin->ds_object;
703                 dsphys->ds_prev_snap_txg =
704                     origin->ds_phys->ds_creation_txg;
705                 dsphys->ds_referenced_bytes =
706                     origin->ds_phys->ds_referenced_bytes;
707                 dsphys->ds_compressed_bytes =
708                     origin->ds_phys->ds_compressed_bytes;
709                 dsphys->ds_uncompressed_bytes =
710                     origin->ds_phys->ds_uncompressed_bytes;
711                 dsphys->ds_bp = origin->ds_phys->ds_bp;
712                 dsphys->ds_flags |= origin->ds_phys->ds_flags;
713
714                 dmu_buf_will_dirty(origin->ds_dbuf, tx);
715                 origin->ds_phys->ds_num_children++;
716
717                 VERIFY0(dsl_dataset_hold_obj(dp,
718                     origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
719                 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
720                     dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
721                 dsl_dataset_rele(ohds, FTAG);
722
723                 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
724                         if (origin->ds_phys->ds_next_clones_obj == 0) {
725                                 origin->ds_phys->ds_next_clones_obj =
726                                     zap_create(mos,
727                                     DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
728                         }
729                         VERIFY0(zap_add_int(mos,
730                             origin->ds_phys->ds_next_clones_obj, dsobj, tx));
731                 }
732
733                 dmu_buf_will_dirty(dd->dd_dbuf, tx);
734                 dd->dd_phys->dd_origin_obj = origin->ds_object;
735                 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
736                         if (origin->ds_dir->dd_phys->dd_clones == 0) {
737                                 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
738                                 origin->ds_dir->dd_phys->dd_clones =
739                                     zap_create(mos,
740                                     DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
741                         }
742                         VERIFY0(zap_add_int(mos,
743                             origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
744                 }
745         }
746
747         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
748                 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
749
750         dmu_buf_rele(dbuf, FTAG);
751
752         dmu_buf_will_dirty(dd->dd_dbuf, tx);
753         dd->dd_phys->dd_head_dataset_obj = dsobj;
754
755         return (dsobj);
756 }
757
758 static void
759 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
760 {
761         objset_t *os;
762
763         VERIFY0(dmu_objset_from_ds(ds, &os));
764         bzero(&os->os_zil_header, sizeof (os->os_zil_header));
765         dsl_dataset_dirty(ds, tx);
766 }
767
768 uint64_t
769 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
770     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
771 {
772         dsl_pool_t *dp = pdd->dd_pool;
773         uint64_t dsobj, ddobj;
774         dsl_dir_t *dd;
775
776         ASSERT(dmu_tx_is_syncing(tx));
777         ASSERT(lastname[0] != '@');
778
779         ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
780         VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
781
782         dsobj = dsl_dataset_create_sync_dd(dd, origin,
783             flags & ~DS_CREATE_FLAG_NODIRTY, tx);
784
785         dsl_deleg_set_create_perms(dd, tx, cr);
786
787         dsl_dir_rele(dd, FTAG);
788
789         /*
790          * If we are creating a clone, make sure we zero out any stale
791          * data from the origin snapshots zil header.
792          */
793         if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
794                 dsl_dataset_t *ds;
795
796                 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
797                 dsl_dataset_zero_zil(ds, tx);
798                 dsl_dataset_rele(ds, FTAG);
799         }
800
801         return (dsobj);
802 }
803
804 #ifdef __FreeBSD__
805 /* FreeBSD ioctl compat begin */
806 struct destroyarg {
807         nvlist_t *nvl;
808         const char *snapname;
809 };
810
811 static int
812 dsl_check_snap_cb(const char *name, void *arg)
813 {
814         struct destroyarg *da = arg;
815         dsl_dataset_t *ds;
816         char *dsname;
817
818         dsname = kmem_asprintf("%s@%s", name, da->snapname);
819         fnvlist_add_boolean(da->nvl, dsname);
820         kmem_free(dsname, strlen(dsname) + 1);
821
822         return (0);
823 }
824
825 int
826 dmu_get_recursive_snaps_nvl(char *fsname, const char *snapname,
827     nvlist_t *snaps)
828 {
829         struct destroyarg *da;
830         int err;
831
832         da = kmem_zalloc(sizeof (struct destroyarg), KM_SLEEP);
833         da->nvl = snaps;
834         da->snapname = snapname;
835         err = dmu_objset_find(fsname, dsl_check_snap_cb, da,
836             DS_FIND_CHILDREN);
837         kmem_free(da, sizeof (struct destroyarg));
838
839         return (err);
840 }
841 /* FreeBSD ioctl compat end */
842 #endif /* __FreeBSD__ */
843
844 /*
845  * The unique space in the head dataset can be calculated by subtracting
846  * the space used in the most recent snapshot, that is still being used
847  * in this file system, from the space currently in use.  To figure out
848  * the space in the most recent snapshot still in use, we need to take
849  * the total space used in the snapshot and subtract out the space that
850  * has been freed up since the snapshot was taken.
851  */
852 void
853 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
854 {
855         uint64_t mrs_used;
856         uint64_t dlused, dlcomp, dluncomp;
857
858         ASSERT(!dsl_dataset_is_snapshot(ds));
859
860         if (ds->ds_phys->ds_prev_snap_obj != 0)
861                 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
862         else
863                 mrs_used = 0;
864
865         dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
866
867         ASSERT3U(dlused, <=, mrs_used);
868         ds->ds_phys->ds_unique_bytes =
869             ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
870
871         if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
872             SPA_VERSION_UNIQUE_ACCURATE)
873                 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
874 }
875
876 void
877 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
878     dmu_tx_t *tx)
879 {
880         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
881         uint64_t count;
882         int err;
883
884         ASSERT(ds->ds_phys->ds_num_children >= 2);
885         err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
886         /*
887          * The err should not be ENOENT, but a bug in a previous version
888          * of the code could cause upgrade_clones_cb() to not set
889          * ds_next_snap_obj when it should, leading to a missing entry.
890          * If we knew that the pool was created after
891          * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
892          * ENOENT.  However, at least we can check that we don't have
893          * too many entries in the next_clones_obj even after failing to
894          * remove this one.
895          */
896         if (err != ENOENT)
897                 VERIFY0(err);
898         ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
899             &count));
900         ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
901 }
902
903
904 blkptr_t *
905 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
906 {
907         return (&ds->ds_phys->ds_bp);
908 }
909
910 void
911 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
912 {
913         ASSERT(dmu_tx_is_syncing(tx));
914         /* If it's the meta-objset, set dp_meta_rootbp */
915         if (ds == NULL) {
916                 tx->tx_pool->dp_meta_rootbp = *bp;
917         } else {
918                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
919                 ds->ds_phys->ds_bp = *bp;
920         }
921 }
922
923 spa_t *
924 dsl_dataset_get_spa(dsl_dataset_t *ds)
925 {
926         return (ds->ds_dir->dd_pool->dp_spa);
927 }
928
929 void
930 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
931 {
932         dsl_pool_t *dp;
933
934         if (ds == NULL) /* this is the meta-objset */
935                 return;
936
937         ASSERT(ds->ds_objset != NULL);
938
939         if (ds->ds_phys->ds_next_snap_obj != 0)
940                 panic("dirtying snapshot!");
941
942         dp = ds->ds_dir->dd_pool;
943
944         if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
945                 /* up the hold count until we can be written out */
946                 dmu_buf_add_ref(ds->ds_dbuf, ds);
947         }
948 }
949
950 boolean_t
951 dsl_dataset_is_dirty(dsl_dataset_t *ds)
952 {
953         for (int t = 0; t < TXG_SIZE; t++) {
954                 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
955                     ds, t))
956                         return (B_TRUE);
957         }
958         return (B_FALSE);
959 }
960
961 static int
962 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
963 {
964         uint64_t asize;
965
966         if (!dmu_tx_is_syncing(tx))
967                 return (0);
968
969         /*
970          * If there's an fs-only reservation, any blocks that might become
971          * owned by the snapshot dataset must be accommodated by space
972          * outside of the reservation.
973          */
974         ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
975         asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
976         if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
977                 return (SET_ERROR(ENOSPC));
978
979         /*
980          * Propagate any reserved space for this snapshot to other
981          * snapshot checks in this sync group.
982          */
983         if (asize > 0)
984                 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
985
986         return (0);
987 }
988
989 typedef struct dsl_dataset_snapshot_arg {
990         nvlist_t *ddsa_snaps;
991         nvlist_t *ddsa_props;
992         nvlist_t *ddsa_errors;
993 } dsl_dataset_snapshot_arg_t;
994
995 int
996 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
997     dmu_tx_t *tx)
998 {
999         int error;
1000         uint64_t value;
1001
1002         ds->ds_trysnap_txg = tx->tx_txg;
1003
1004         if (!dmu_tx_is_syncing(tx))
1005                 return (0);
1006
1007         /*
1008          * We don't allow multiple snapshots of the same txg.  If there
1009          * is already one, try again.
1010          */
1011         if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
1012                 return (SET_ERROR(EAGAIN));
1013
1014         /*
1015          * Check for conflicting snapshot name.
1016          */
1017         error = dsl_dataset_snap_lookup(ds, snapname, &value);
1018         if (error == 0)
1019                 return (SET_ERROR(EEXIST));
1020         if (error != ENOENT)
1021                 return (error);
1022
1023         error = dsl_dataset_snapshot_reserve_space(ds, tx);
1024         if (error != 0)
1025                 return (error);
1026
1027         return (0);
1028 }
1029
1030 static int
1031 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1032 {
1033         dsl_dataset_snapshot_arg_t *ddsa = arg;
1034         dsl_pool_t *dp = dmu_tx_pool(tx);
1035         nvpair_t *pair;
1036         int rv = 0;
1037
1038         for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1039             pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1040                 int error = 0;
1041                 dsl_dataset_t *ds;
1042                 char *name, *atp;
1043                 char dsname[MAXNAMELEN];
1044
1045                 name = nvpair_name(pair);
1046                 if (strlen(name) >= MAXNAMELEN)
1047                         error = SET_ERROR(ENAMETOOLONG);
1048                 if (error == 0) {
1049                         atp = strchr(name, '@');
1050                         if (atp == NULL)
1051                                 error = SET_ERROR(EINVAL);
1052                         if (error == 0)
1053                                 (void) strlcpy(dsname, name, atp - name + 1);
1054                 }
1055                 if (error == 0)
1056                         error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1057                 if (error == 0) {
1058                         error = dsl_dataset_snapshot_check_impl(ds,
1059                             atp + 1, tx);
1060                         dsl_dataset_rele(ds, FTAG);
1061                 }
1062
1063                 if (error != 0) {
1064                         if (ddsa->ddsa_errors != NULL) {
1065                                 fnvlist_add_int32(ddsa->ddsa_errors,
1066                                     name, error);
1067                         }
1068                         rv = error;
1069                 }
1070         }
1071         return (rv);
1072 }
1073
1074 void
1075 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1076     dmu_tx_t *tx)
1077 {
1078         static zil_header_t zero_zil;
1079
1080         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1081         dmu_buf_t *dbuf;
1082         dsl_dataset_phys_t *dsphys;
1083         uint64_t dsobj, crtxg;
1084         objset_t *mos = dp->dp_meta_objset;
1085         objset_t *os;
1086
1087         ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1088
1089         /*
1090          * If we are on an old pool, the zil must not be active, in which
1091          * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1092          */
1093         ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1094             dmu_objset_from_ds(ds, &os) != 0 ||
1095             bcmp(&os->os_phys->os_zil_header, &zero_zil,
1096             sizeof (zero_zil)) == 0);
1097
1098
1099         /*
1100          * The origin's ds_creation_txg has to be < TXG_INITIAL
1101          */
1102         if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1103                 crtxg = 1;
1104         else
1105                 crtxg = tx->tx_txg;
1106
1107         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1108             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1109         VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1110         dmu_buf_will_dirty(dbuf, tx);
1111         dsphys = dbuf->db_data;
1112         bzero(dsphys, sizeof (dsl_dataset_phys_t));
1113         dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1114         dsphys->ds_fsid_guid = unique_create();
1115         do {
1116                 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1117                     sizeof (dsphys->ds_guid));
1118         } while (dsphys->ds_guid == 0);
1119         dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1120         dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1121         dsphys->ds_next_snap_obj = ds->ds_object;
1122         dsphys->ds_num_children = 1;
1123         dsphys->ds_creation_time = gethrestime_sec();
1124         dsphys->ds_creation_txg = crtxg;
1125         dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1126         dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1127         dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1128         dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1129         dsphys->ds_flags = ds->ds_phys->ds_flags;
1130         dsphys->ds_bp = ds->ds_phys->ds_bp;
1131         dmu_buf_rele(dbuf, FTAG);
1132
1133         ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1134         if (ds->ds_prev) {
1135                 uint64_t next_clones_obj =
1136                     ds->ds_prev->ds_phys->ds_next_clones_obj;
1137                 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1138                     ds->ds_object ||
1139                     ds->ds_prev->ds_phys->ds_num_children > 1);
1140                 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1141                         dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1142                         ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1143                             ds->ds_prev->ds_phys->ds_creation_txg);
1144                         ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1145                 } else if (next_clones_obj != 0) {
1146                         dsl_dataset_remove_from_next_clones(ds->ds_prev,
1147                             dsphys->ds_next_snap_obj, tx);
1148                         VERIFY0(zap_add_int(mos,
1149                             next_clones_obj, dsobj, tx));
1150                 }
1151         }
1152
1153         /*
1154          * If we have a reference-reservation on this dataset, we will
1155          * need to increase the amount of refreservation being charged
1156          * since our unique space is going to zero.
1157          */
1158         if (ds->ds_reserved) {
1159                 int64_t delta;
1160                 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1161                 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1162                 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1163                     delta, 0, 0, tx);
1164         }
1165
1166         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1167         ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1168             UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1169         dsl_deadlist_close(&ds->ds_deadlist);
1170         dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1171         dsl_deadlist_add_key(&ds->ds_deadlist,
1172             ds->ds_phys->ds_prev_snap_txg, tx);
1173
1174         ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1175         ds->ds_phys->ds_prev_snap_obj = dsobj;
1176         ds->ds_phys->ds_prev_snap_txg = crtxg;
1177         ds->ds_phys->ds_unique_bytes = 0;
1178         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1179                 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1180
1181         VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1182             snapname, 8, 1, &dsobj, tx));
1183
1184         if (ds->ds_prev)
1185                 dsl_dataset_rele(ds->ds_prev, ds);
1186         VERIFY0(dsl_dataset_hold_obj(dp,
1187             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1188
1189         dsl_scan_ds_snapshotted(ds, tx);
1190
1191         dsl_dir_snap_cmtime_update(ds->ds_dir);
1192
1193         spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1194 }
1195
1196 static void
1197 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1198 {
1199         dsl_dataset_snapshot_arg_t *ddsa = arg;
1200         dsl_pool_t *dp = dmu_tx_pool(tx);
1201         nvpair_t *pair;
1202
1203         for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1204             pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1205                 dsl_dataset_t *ds;
1206                 char *name, *atp;
1207                 char dsname[MAXNAMELEN];
1208
1209                 name = nvpair_name(pair);
1210                 atp = strchr(name, '@');
1211                 (void) strlcpy(dsname, name, atp - name + 1);
1212                 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1213
1214                 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1215                 if (ddsa->ddsa_props != NULL) {
1216                         dsl_props_set_sync_impl(ds->ds_prev,
1217                             ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1218                 }
1219                 dsl_dataset_rele(ds, FTAG);
1220         }
1221 }
1222
1223 /*
1224  * The snapshots must all be in the same pool.
1225  * All-or-nothing: if there are any failures, nothing will be modified.
1226  */
1227 int
1228 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1229 {
1230         dsl_dataset_snapshot_arg_t ddsa;
1231         nvpair_t *pair;
1232         boolean_t needsuspend;
1233         int error;
1234         spa_t *spa;
1235         char *firstname;
1236         nvlist_t *suspended = NULL;
1237
1238         pair = nvlist_next_nvpair(snaps, NULL);
1239         if (pair == NULL)
1240                 return (0);
1241         firstname = nvpair_name(pair);
1242
1243         error = spa_open(firstname, &spa, FTAG);
1244         if (error != 0)
1245                 return (error);
1246         needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1247         spa_close(spa, FTAG);
1248
1249         if (needsuspend) {
1250                 suspended = fnvlist_alloc();
1251                 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1252                     pair = nvlist_next_nvpair(snaps, pair)) {
1253                         char fsname[MAXNAMELEN];
1254                         char *snapname = nvpair_name(pair);
1255                         char *atp;
1256                         void *cookie;
1257
1258                         atp = strchr(snapname, '@');
1259                         if (atp == NULL) {
1260                                 error = SET_ERROR(EINVAL);
1261                                 break;
1262                         }
1263                         (void) strlcpy(fsname, snapname, atp - snapname + 1);
1264
1265                         error = zil_suspend(fsname, &cookie);
1266                         if (error != 0)
1267                                 break;
1268                         fnvlist_add_uint64(suspended, fsname,
1269                             (uintptr_t)cookie);
1270                 }
1271         }
1272
1273         ddsa.ddsa_snaps = snaps;
1274         ddsa.ddsa_props = props;
1275         ddsa.ddsa_errors = errors;
1276
1277         if (error == 0) {
1278                 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1279                     dsl_dataset_snapshot_sync, &ddsa,
1280                     fnvlist_num_pairs(snaps) * 3);
1281         }
1282
1283         if (suspended != NULL) {
1284                 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1285                     pair = nvlist_next_nvpair(suspended, pair)) {
1286                         zil_resume((void *)(uintptr_t)
1287                             fnvpair_value_uint64(pair));
1288                 }
1289                 fnvlist_free(suspended);
1290         }
1291
1292 #ifdef __FreeBSD__
1293 #ifdef _KERNEL
1294         if (error == 0) {
1295                 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1296                     pair = nvlist_next_nvpair(snaps, pair)) {
1297                         char *snapname = nvpair_name(pair);
1298                         zvol_create_minors(snapname);
1299                 }
1300         }
1301 #endif
1302 #endif
1303         return (error);
1304 }
1305
1306 typedef struct dsl_dataset_snapshot_tmp_arg {
1307         const char *ddsta_fsname;
1308         const char *ddsta_snapname;
1309         minor_t ddsta_cleanup_minor;
1310         const char *ddsta_htag;
1311 } dsl_dataset_snapshot_tmp_arg_t;
1312
1313 static int
1314 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1315 {
1316         dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1317         dsl_pool_t *dp = dmu_tx_pool(tx);
1318         dsl_dataset_t *ds;
1319         int error;
1320
1321         error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1322         if (error != 0)
1323                 return (error);
1324
1325         error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, tx);
1326         if (error != 0) {
1327                 dsl_dataset_rele(ds, FTAG);
1328                 return (error);
1329         }
1330
1331         if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1332                 dsl_dataset_rele(ds, FTAG);
1333                 return (SET_ERROR(ENOTSUP));
1334         }
1335         error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1336             B_TRUE, tx);
1337         if (error != 0) {
1338                 dsl_dataset_rele(ds, FTAG);
1339                 return (error);
1340         }
1341
1342         dsl_dataset_rele(ds, FTAG);
1343         return (0);
1344 }
1345
1346 static void
1347 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1348 {
1349         dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1350         dsl_pool_t *dp = dmu_tx_pool(tx);
1351         dsl_dataset_t *ds;
1352
1353         VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1354
1355         dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1356         dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1357             ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1358         dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1359
1360         dsl_dataset_rele(ds, FTAG);
1361 }
1362
1363 int
1364 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1365     minor_t cleanup_minor, const char *htag)
1366 {
1367         dsl_dataset_snapshot_tmp_arg_t ddsta;
1368         int error;
1369         spa_t *spa;
1370         boolean_t needsuspend;
1371         void *cookie;
1372
1373         ddsta.ddsta_fsname = fsname;
1374         ddsta.ddsta_snapname = snapname;
1375         ddsta.ddsta_cleanup_minor = cleanup_minor;
1376         ddsta.ddsta_htag = htag;
1377
1378         error = spa_open(fsname, &spa, FTAG);
1379         if (error != 0)
1380                 return (error);
1381         needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1382         spa_close(spa, FTAG);
1383
1384         if (needsuspend) {
1385                 error = zil_suspend(fsname, &cookie);
1386                 if (error != 0)
1387                         return (error);
1388         }
1389
1390         error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1391             dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1392
1393         if (needsuspend)
1394                 zil_resume(cookie);
1395         return (error);
1396 }
1397
1398
1399 void
1400 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1401 {
1402         ASSERT(dmu_tx_is_syncing(tx));
1403         ASSERT(ds->ds_objset != NULL);
1404         ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1405
1406         /*
1407          * in case we had to change ds_fsid_guid when we opened it,
1408          * sync it out now.
1409          */
1410         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1411         ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1412
1413         dmu_objset_sync(ds->ds_objset, zio, tx);
1414 }
1415
1416 static void
1417 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1418 {
1419         uint64_t count = 0;
1420         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1421         zap_cursor_t zc;
1422         zap_attribute_t za;
1423         nvlist_t *propval = fnvlist_alloc();
1424         nvlist_t *val = fnvlist_alloc();
1425
1426         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1427
1428         /*
1429          * There may be missing entries in ds_next_clones_obj
1430          * due to a bug in a previous version of the code.
1431          * Only trust it if it has the right number of entries.
1432          */
1433         if (ds->ds_phys->ds_next_clones_obj != 0) {
1434                 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1435                     &count));
1436         }
1437         if (count != ds->ds_phys->ds_num_children - 1)
1438                 goto fail;
1439         for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1440             zap_cursor_retrieve(&zc, &za) == 0;
1441             zap_cursor_advance(&zc)) {
1442                 dsl_dataset_t *clone;
1443                 char buf[ZFS_MAXNAMELEN];
1444                 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1445                     za.za_first_integer, FTAG, &clone));
1446                 dsl_dir_name(clone->ds_dir, buf);
1447                 fnvlist_add_boolean(val, buf);
1448                 dsl_dataset_rele(clone, FTAG);
1449         }
1450         zap_cursor_fini(&zc);
1451         fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1452         fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1453 fail:
1454         nvlist_free(val);
1455         nvlist_free(propval);
1456 }
1457
1458 void
1459 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1460 {
1461         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1462         uint64_t refd, avail, uobjs, aobjs, ratio;
1463
1464         ASSERT(dsl_pool_config_held(dp));
1465
1466         ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1467             (ds->ds_phys->ds_uncompressed_bytes * 100 /
1468             ds->ds_phys->ds_compressed_bytes);
1469
1470         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1471         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1472             ds->ds_phys->ds_uncompressed_bytes);
1473
1474         if (dsl_dataset_is_snapshot(ds)) {
1475                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1476                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1477                     ds->ds_phys->ds_unique_bytes);
1478                 get_clones_stat(ds, nv);
1479         } else {
1480                 dsl_dir_stats(ds->ds_dir, nv);
1481         }
1482
1483         dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1484         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1485         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1486
1487         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1488             ds->ds_phys->ds_creation_time);
1489         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1490             ds->ds_phys->ds_creation_txg);
1491         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1492             ds->ds_quota);
1493         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1494             ds->ds_reserved);
1495         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1496             ds->ds_phys->ds_guid);
1497         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1498             ds->ds_phys->ds_unique_bytes);
1499         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1500             ds->ds_object);
1501         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1502             ds->ds_userrefs);
1503         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1504             DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1505
1506         if (ds->ds_phys->ds_prev_snap_obj != 0) {
1507                 uint64_t written, comp, uncomp;
1508                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1509                 dsl_dataset_t *prev;
1510
1511                 int err = dsl_dataset_hold_obj(dp,
1512                     ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1513                 if (err == 0) {
1514                         err = dsl_dataset_space_written(prev, ds, &written,
1515                             &comp, &uncomp);
1516                         dsl_dataset_rele(prev, FTAG);
1517                         if (err == 0) {
1518                                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1519                                     written);
1520                         }
1521                 }
1522         }
1523 }
1524
1525 void
1526 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1527 {
1528         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1529         ASSERT(dsl_pool_config_held(dp));
1530
1531         stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1532         stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1533         stat->dds_guid = ds->ds_phys->ds_guid;
1534         stat->dds_origin[0] = '\0';
1535         if (dsl_dataset_is_snapshot(ds)) {
1536                 stat->dds_is_snapshot = B_TRUE;
1537                 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1538         } else {
1539                 stat->dds_is_snapshot = B_FALSE;
1540                 stat->dds_num_clones = 0;
1541
1542                 if (dsl_dir_is_clone(ds->ds_dir)) {
1543                         dsl_dataset_t *ods;
1544
1545                         VERIFY0(dsl_dataset_hold_obj(dp,
1546                             ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1547                         dsl_dataset_name(ods, stat->dds_origin);
1548                         dsl_dataset_rele(ods, FTAG);
1549                 }
1550         }
1551 }
1552
1553 uint64_t
1554 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1555 {
1556         return (ds->ds_fsid_guid);
1557 }
1558
1559 void
1560 dsl_dataset_space(dsl_dataset_t *ds,
1561     uint64_t *refdbytesp, uint64_t *availbytesp,
1562     uint64_t *usedobjsp, uint64_t *availobjsp)
1563 {
1564         *refdbytesp = ds->ds_phys->ds_referenced_bytes;
1565         *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1566         if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1567                 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1568         if (ds->ds_quota != 0) {
1569                 /*
1570                  * Adjust available bytes according to refquota
1571                  */
1572                 if (*refdbytesp < ds->ds_quota)
1573                         *availbytesp = MIN(*availbytesp,
1574                             ds->ds_quota - *refdbytesp);
1575                 else
1576                         *availbytesp = 0;
1577         }
1578         *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1579         *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1580 }
1581
1582 boolean_t
1583 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
1584 {
1585         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1586
1587         ASSERT(dsl_pool_config_held(dp));
1588         if (ds->ds_prev == NULL)
1589                 return (B_FALSE);
1590         if (ds->ds_phys->ds_bp.blk_birth >
1591             ds->ds_prev->ds_phys->ds_creation_txg) {
1592                 objset_t *os, *os_prev;
1593                 /*
1594                  * It may be that only the ZIL differs, because it was
1595                  * reset in the head.  Don't count that as being
1596                  * modified.
1597                  */
1598                 if (dmu_objset_from_ds(ds, &os) != 0)
1599                         return (B_TRUE);
1600                 if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
1601                         return (B_TRUE);
1602                 return (bcmp(&os->os_phys->os_meta_dnode,
1603                     &os_prev->os_phys->os_meta_dnode,
1604                     sizeof (os->os_phys->os_meta_dnode)) != 0);
1605         }
1606         return (B_FALSE);
1607 }
1608
1609 typedef struct dsl_dataset_rename_snapshot_arg {
1610         const char *ddrsa_fsname;
1611         const char *ddrsa_oldsnapname;
1612         const char *ddrsa_newsnapname;
1613         boolean_t ddrsa_recursive;
1614         dmu_tx_t *ddrsa_tx;
1615 } dsl_dataset_rename_snapshot_arg_t;
1616
1617 /* ARGSUSED */
1618 static int
1619 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1620     dsl_dataset_t *hds, void *arg)
1621 {
1622         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1623         int error;
1624         uint64_t val;
1625
1626         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1627         if (error != 0) {
1628                 /* ignore nonexistent snapshots */
1629                 return (error == ENOENT ? 0 : error);
1630         }
1631
1632         /* new name should not exist */
1633         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1634         if (error == 0)
1635                 error = SET_ERROR(EEXIST);
1636         else if (error == ENOENT)
1637                 error = 0;
1638
1639         /* dataset name + 1 for the "@" + the new snapshot name must fit */
1640         if (dsl_dir_namelen(hds->ds_dir) + 1 +
1641             strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1642                 error = SET_ERROR(ENAMETOOLONG);
1643
1644         return (error);
1645 }
1646
1647 static int
1648 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1649 {
1650         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1651         dsl_pool_t *dp = dmu_tx_pool(tx);
1652         dsl_dataset_t *hds;
1653         int error;
1654
1655         error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1656         if (error != 0)
1657                 return (error);
1658
1659         if (ddrsa->ddrsa_recursive) {
1660                 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1661                     dsl_dataset_rename_snapshot_check_impl, ddrsa,
1662                     DS_FIND_CHILDREN);
1663         } else {
1664                 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1665         }
1666         dsl_dataset_rele(hds, FTAG);
1667         return (error);
1668 }
1669
1670 static int
1671 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1672     dsl_dataset_t *hds, void *arg)
1673 {
1674 #ifdef __FreeBSD__
1675 #ifdef _KERNEL
1676         char *oldname, *newname;
1677 #endif
1678 #endif
1679         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1680         dsl_dataset_t *ds;
1681         uint64_t val;
1682         dmu_tx_t *tx = ddrsa->ddrsa_tx;
1683         int error;
1684
1685         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1686         ASSERT(error == 0 || error == ENOENT);
1687         if (error == ENOENT) {
1688                 /* ignore nonexistent snapshots */
1689                 return (0);
1690         }
1691
1692         VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1693
1694         /* log before we change the name */
1695         spa_history_log_internal_ds(ds, "rename", tx,
1696             "-> @%s", ddrsa->ddrsa_newsnapname);
1697
1698         VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1699         mutex_enter(&ds->ds_lock);
1700         (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1701         mutex_exit(&ds->ds_lock);
1702         VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1703             ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1704
1705 #ifdef __FreeBSD__
1706 #ifdef _KERNEL
1707         oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1708         newname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1709         snprintf(oldname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1710             ddrsa->ddrsa_oldsnapname);
1711         snprintf(newname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1712             ddrsa->ddrsa_newsnapname);
1713         zfsvfs_update_fromname(oldname, newname);
1714         zvol_rename_minors(oldname, newname);
1715         kmem_free(newname, MAXPATHLEN);
1716         kmem_free(oldname, MAXPATHLEN);
1717 #endif
1718 #endif
1719         dsl_dataset_rele(ds, FTAG);
1720
1721         return (0);
1722 }
1723
1724 static void
1725 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1726 {
1727         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1728         dsl_pool_t *dp = dmu_tx_pool(tx);
1729         dsl_dataset_t *hds;
1730
1731         VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1732         ddrsa->ddrsa_tx = tx;
1733         if (ddrsa->ddrsa_recursive) {
1734                 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1735                     dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1736                     DS_FIND_CHILDREN));
1737         } else {
1738                 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1739         }
1740         dsl_dataset_rele(hds, FTAG);
1741 }
1742
1743 int
1744 dsl_dataset_rename_snapshot(const char *fsname,
1745     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1746 {
1747         dsl_dataset_rename_snapshot_arg_t ddrsa;
1748
1749         ddrsa.ddrsa_fsname = fsname;
1750         ddrsa.ddrsa_oldsnapname = oldsnapname;
1751         ddrsa.ddrsa_newsnapname = newsnapname;
1752         ddrsa.ddrsa_recursive = recursive;
1753
1754         return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1755             dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
1756 }
1757
1758 static int
1759 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1760 {
1761         const char *fsname = arg;
1762         dsl_pool_t *dp = dmu_tx_pool(tx);
1763         dsl_dataset_t *ds;
1764         int64_t unused_refres_delta;
1765         int error;
1766
1767         error = dsl_dataset_hold(dp, fsname, FTAG, &ds);
1768         if (error != 0)
1769                 return (error);
1770
1771         /* must not be a snapshot */
1772         if (dsl_dataset_is_snapshot(ds)) {
1773                 dsl_dataset_rele(ds, FTAG);
1774                 return (SET_ERROR(EINVAL));
1775         }
1776
1777         /* must have a most recent snapshot */
1778         if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1779                 dsl_dataset_rele(ds, FTAG);
1780                 return (SET_ERROR(EINVAL));
1781         }
1782
1783         if (dsl_dataset_long_held(ds)) {
1784                 dsl_dataset_rele(ds, FTAG);
1785                 return (SET_ERROR(EBUSY));
1786         }
1787
1788         /*
1789          * Check if the snap we are rolling back to uses more than
1790          * the refquota.
1791          */
1792         if (ds->ds_quota != 0 &&
1793             ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1794                 dsl_dataset_rele(ds, FTAG);
1795                 return (SET_ERROR(EDQUOT));
1796         }
1797
1798         /*
1799          * When we do the clone swap, we will temporarily use more space
1800          * due to the refreservation (the head will no longer have any
1801          * unique space, so the entire amount of the refreservation will need
1802          * to be free).  We will immediately destroy the clone, freeing
1803          * this space, but the freeing happens over many txg's.
1804          */
1805         unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1806             ds->ds_phys->ds_unique_bytes);
1807
1808         if (unused_refres_delta > 0 &&
1809             unused_refres_delta >
1810             dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1811                 dsl_dataset_rele(ds, FTAG);
1812                 return (SET_ERROR(ENOSPC));
1813         }
1814
1815         dsl_dataset_rele(ds, FTAG);
1816         return (0);
1817 }
1818
1819 static void
1820 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1821 {
1822         const char *fsname = arg;
1823         dsl_pool_t *dp = dmu_tx_pool(tx);
1824         dsl_dataset_t *ds, *clone;
1825         uint64_t cloneobj;
1826
1827         VERIFY0(dsl_dataset_hold(dp, fsname, FTAG, &ds));
1828
1829         cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1830             ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1831
1832         VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1833
1834         dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1835         dsl_dataset_zero_zil(ds, tx);
1836
1837         dsl_destroy_head_sync_impl(clone, tx);
1838
1839         dsl_dataset_rele(clone, FTAG);
1840         dsl_dataset_rele(ds, FTAG);
1841 }
1842
1843 int
1844 dsl_dataset_rollback(const char *fsname)
1845 {
1846         return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1847             dsl_dataset_rollback_sync, (void *)fsname, 1));
1848 }
1849
1850 struct promotenode {
1851         list_node_t link;
1852         dsl_dataset_t *ds;
1853 };
1854
1855 typedef struct dsl_dataset_promote_arg {
1856         const char *ddpa_clonename;
1857         dsl_dataset_t *ddpa_clone;
1858         list_t shared_snaps, origin_snaps, clone_snaps;
1859         dsl_dataset_t *origin_origin; /* origin of the origin */
1860         uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1861         char *err_ds;
1862 } dsl_dataset_promote_arg_t;
1863
1864 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
1865 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1866     void *tag);
1867 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
1868
1869 static int
1870 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
1871 {
1872         dsl_dataset_promote_arg_t *ddpa = arg;
1873         dsl_pool_t *dp = dmu_tx_pool(tx);
1874         dsl_dataset_t *hds;
1875         struct promotenode *snap;
1876         dsl_dataset_t *origin_ds;
1877         int err;
1878         uint64_t unused;
1879
1880         err = promote_hold(ddpa, dp, FTAG);
1881         if (err != 0)
1882                 return (err);
1883
1884         hds = ddpa->ddpa_clone;
1885
1886         if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1887                 promote_rele(ddpa, FTAG);
1888                 return (SET_ERROR(EXDEV));
1889         }
1890
1891         /*
1892          * Compute and check the amount of space to transfer.  Since this is
1893          * so expensive, don't do the preliminary check.
1894          */
1895         if (!dmu_tx_is_syncing(tx)) {
1896                 promote_rele(ddpa, FTAG);
1897                 return (0);
1898         }
1899
1900         snap = list_head(&ddpa->shared_snaps);
1901         origin_ds = snap->ds;
1902
1903         /* compute origin's new unique space */
1904         snap = list_tail(&ddpa->clone_snaps);
1905         ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1906         dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1907             origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1908             &ddpa->unique, &unused, &unused);
1909
1910         /*
1911          * Walk the snapshots that we are moving
1912          *
1913          * Compute space to transfer.  Consider the incremental changes
1914          * to used by each snapshot:
1915          * (my used) = (prev's used) + (blocks born) - (blocks killed)
1916          * So each snapshot gave birth to:
1917          * (blocks born) = (my used) - (prev's used) + (blocks killed)
1918          * So a sequence would look like:
1919          * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1920          * Which simplifies to:
1921          * uN + kN + kN-1 + ... + k1 + k0
1922          * Note however, if we stop before we reach the ORIGIN we get:
1923          * uN + kN + kN-1 + ... + kM - uM-1
1924          */
1925         ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1926         ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1927         ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1928         for (snap = list_head(&ddpa->shared_snaps); snap;
1929             snap = list_next(&ddpa->shared_snaps, snap)) {
1930                 uint64_t val, dlused, dlcomp, dluncomp;
1931                 dsl_dataset_t *ds = snap->ds;
1932
1933                 /*
1934                  * If there are long holds, we won't be able to evict
1935                  * the objset.
1936                  */
1937                 if (dsl_dataset_long_held(ds)) {
1938                         err = SET_ERROR(EBUSY);
1939                         goto out;
1940                 }
1941
1942                 /* Check that the snapshot name does not conflict */
1943                 VERIFY0(dsl_dataset_get_snapname(ds));
1944                 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
1945                 if (err == 0) {
1946                         (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
1947                         err = SET_ERROR(EEXIST);
1948                         goto out;
1949                 }
1950                 if (err != ENOENT)
1951                         goto out;
1952
1953                 /* The very first snapshot does not have a deadlist */
1954                 if (ds->ds_phys->ds_prev_snap_obj == 0)
1955                         continue;
1956
1957                 dsl_deadlist_space(&ds->ds_deadlist,
1958                     &dlused, &dlcomp, &dluncomp);
1959                 ddpa->used += dlused;
1960                 ddpa->comp += dlcomp;
1961                 ddpa->uncomp += dluncomp;
1962         }
1963
1964         /*
1965          * If we are a clone of a clone then we never reached ORIGIN,
1966          * so we need to subtract out the clone origin's used space.
1967          */
1968         if (ddpa->origin_origin) {
1969                 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
1970                 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
1971                 ddpa->uncomp -=
1972                     ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
1973         }
1974
1975         /* Check that there is enough space here */
1976         err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
1977             ddpa->used);
1978         if (err != 0)
1979                 goto out;
1980
1981         /*
1982          * Compute the amounts of space that will be used by snapshots
1983          * after the promotion (for both origin and clone).  For each,
1984          * it is the amount of space that will be on all of their
1985          * deadlists (that was not born before their new origin).
1986          */
1987         if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1988                 uint64_t space;
1989
1990                 /*
1991                  * Note, typically this will not be a clone of a clone,
1992                  * so dd_origin_txg will be < TXG_INITIAL, so
1993                  * these snaplist_space() -> dsl_deadlist_space_range()
1994                  * calls will be fast because they do not have to
1995                  * iterate over all bps.
1996                  */
1997                 snap = list_head(&ddpa->origin_snaps);
1998                 err = snaplist_space(&ddpa->shared_snaps,
1999                     snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2000                 if (err != 0)
2001                         goto out;
2002
2003                 err = snaplist_space(&ddpa->clone_snaps,
2004                     snap->ds->ds_dir->dd_origin_txg, &space);
2005                 if (err != 0)
2006                         goto out;
2007                 ddpa->cloneusedsnap += space;
2008         }
2009         if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2010                 err = snaplist_space(&ddpa->origin_snaps,
2011                     origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
2012                 if (err != 0)
2013                         goto out;
2014         }
2015
2016 out:
2017         promote_rele(ddpa, FTAG);
2018         return (err);
2019 }
2020
2021 static void
2022 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2023 {
2024         dsl_dataset_promote_arg_t *ddpa = arg;
2025         dsl_pool_t *dp = dmu_tx_pool(tx);
2026         dsl_dataset_t *hds;
2027         struct promotenode *snap;
2028         dsl_dataset_t *origin_ds;
2029         dsl_dataset_t *origin_head;
2030         dsl_dir_t *dd;
2031         dsl_dir_t *odd = NULL;
2032         uint64_t oldnext_obj;
2033         int64_t delta;
2034
2035         VERIFY0(promote_hold(ddpa, dp, FTAG));
2036         hds = ddpa->ddpa_clone;
2037
2038         ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
2039
2040         snap = list_head(&ddpa->shared_snaps);
2041         origin_ds = snap->ds;
2042         dd = hds->ds_dir;
2043
2044         snap = list_head(&ddpa->origin_snaps);
2045         origin_head = snap->ds;
2046
2047         /*
2048          * We need to explicitly open odd, since origin_ds's dd will be
2049          * changing.
2050          */
2051         VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2052             NULL, FTAG, &odd));
2053
2054         /* change origin's next snap */
2055         dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2056         oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2057         snap = list_tail(&ddpa->clone_snaps);
2058         ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2059         origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2060
2061         /* change the origin's next clone */
2062         if (origin_ds->ds_phys->ds_next_clones_obj) {
2063                 dsl_dataset_remove_from_next_clones(origin_ds,
2064                     snap->ds->ds_object, tx);
2065                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2066                     origin_ds->ds_phys->ds_next_clones_obj,
2067                     oldnext_obj, tx));
2068         }
2069
2070         /* change origin */
2071         dmu_buf_will_dirty(dd->dd_dbuf, tx);
2072         ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2073         dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2074         dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2075         dmu_buf_will_dirty(odd->dd_dbuf, tx);
2076         odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2077         origin_head->ds_dir->dd_origin_txg =
2078             origin_ds->ds_phys->ds_creation_txg;
2079
2080         /* change dd_clone entries */
2081         if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2082                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2083                     odd->dd_phys->dd_clones, hds->ds_object, tx));
2084                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2085                     ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2086                     hds->ds_object, tx));
2087
2088                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2089                     ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2090                     origin_head->ds_object, tx));
2091                 if (dd->dd_phys->dd_clones == 0) {
2092                         dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2093                             DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2094                 }
2095                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2096                     dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2097         }
2098
2099         /* move snapshots to this dir */
2100         for (snap = list_head(&ddpa->shared_snaps); snap;
2101             snap = list_next(&ddpa->shared_snaps, snap)) {
2102                 dsl_dataset_t *ds = snap->ds;
2103
2104                 /*
2105                  * Property callbacks are registered to a particular
2106                  * dsl_dir.  Since ours is changing, evict the objset
2107                  * so that they will be unregistered from the old dsl_dir.
2108                  */
2109                 if (ds->ds_objset) {
2110                         dmu_objset_evict(ds->ds_objset);
2111                         ds->ds_objset = NULL;
2112                 }
2113
2114                 /* move snap name entry */
2115                 VERIFY0(dsl_dataset_get_snapname(ds));
2116                 VERIFY0(dsl_dataset_snap_remove(origin_head,
2117                     ds->ds_snapname, tx));
2118                 VERIFY0(zap_add(dp->dp_meta_objset,
2119                     hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2120                     8, 1, &ds->ds_object, tx));
2121
2122                 /* change containing dsl_dir */
2123                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2124                 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2125                 ds->ds_phys->ds_dir_obj = dd->dd_object;
2126                 ASSERT3P(ds->ds_dir, ==, odd);
2127                 dsl_dir_rele(ds->ds_dir, ds);
2128                 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2129                     NULL, ds, &ds->ds_dir));
2130
2131                 /* move any clone references */
2132                 if (ds->ds_phys->ds_next_clones_obj &&
2133                     spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2134                         zap_cursor_t zc;
2135                         zap_attribute_t za;
2136
2137                         for (zap_cursor_init(&zc, dp->dp_meta_objset,
2138                             ds->ds_phys->ds_next_clones_obj);
2139                             zap_cursor_retrieve(&zc, &za) == 0;
2140                             zap_cursor_advance(&zc)) {
2141                                 dsl_dataset_t *cnds;
2142                                 uint64_t o;
2143
2144                                 if (za.za_first_integer == oldnext_obj) {
2145                                         /*
2146                                          * We've already moved the
2147                                          * origin's reference.
2148                                          */
2149                                         continue;
2150                                 }
2151
2152                                 VERIFY0(dsl_dataset_hold_obj(dp,
2153                                     za.za_first_integer, FTAG, &cnds));
2154                                 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2155
2156                                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2157                                     odd->dd_phys->dd_clones, o, tx));
2158                                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2159                                     dd->dd_phys->dd_clones, o, tx));
2160                                 dsl_dataset_rele(cnds, FTAG);
2161                         }
2162                         zap_cursor_fini(&zc);
2163                 }
2164
2165                 ASSERT(!dsl_prop_hascb(ds));
2166         }
2167
2168         /*
2169          * Change space accounting.
2170          * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2171          * both be valid, or both be 0 (resulting in delta == 0).  This
2172          * is true for each of {clone,origin} independently.
2173          */
2174
2175         delta = ddpa->cloneusedsnap -
2176             dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2177         ASSERT3S(delta, >=, 0);
2178         ASSERT3U(ddpa->used, >=, delta);
2179         dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2180         dsl_dir_diduse_space(dd, DD_USED_HEAD,
2181             ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2182
2183         delta = ddpa->originusedsnap -
2184             odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2185         ASSERT3S(delta, <=, 0);
2186         ASSERT3U(ddpa->used, >=, -delta);
2187         dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2188         dsl_dir_diduse_space(odd, DD_USED_HEAD,
2189             -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2190
2191         origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2192
2193         /* log history record */
2194         spa_history_log_internal_ds(hds, "promote", tx, "");
2195
2196         dsl_dir_rele(odd, FTAG);
2197         promote_rele(ddpa, FTAG);
2198 }
2199
2200 /*
2201  * Make a list of dsl_dataset_t's for the snapshots between first_obj
2202  * (exclusive) and last_obj (inclusive).  The list will be in reverse
2203  * order (last_obj will be the list_head()).  If first_obj == 0, do all
2204  * snapshots back to this dataset's origin.
2205  */
2206 static int
2207 snaplist_make(dsl_pool_t *dp,
2208     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2209 {
2210         uint64_t obj = last_obj;
2211
2212         list_create(l, sizeof (struct promotenode),
2213             offsetof(struct promotenode, link));
2214
2215         while (obj != first_obj) {
2216                 dsl_dataset_t *ds;
2217                 struct promotenode *snap;
2218                 int err;
2219
2220                 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2221                 ASSERT(err != ENOENT);
2222                 if (err != 0)
2223                         return (err);
2224
2225                 if (first_obj == 0)
2226                         first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2227
2228                 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2229                 snap->ds = ds;
2230                 list_insert_tail(l, snap);
2231                 obj = ds->ds_phys->ds_prev_snap_obj;
2232         }
2233
2234         return (0);
2235 }
2236
2237 static int
2238 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2239 {
2240         struct promotenode *snap;
2241
2242         *spacep = 0;
2243         for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2244                 uint64_t used, comp, uncomp;
2245                 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2246                     mintxg, UINT64_MAX, &used, &comp, &uncomp);
2247                 *spacep += used;
2248         }
2249         return (0);
2250 }
2251
2252 static void
2253 snaplist_destroy(list_t *l, void *tag)
2254 {
2255         struct promotenode *snap;
2256
2257         if (l == NULL || !list_link_active(&l->list_head))
2258                 return;
2259
2260         while ((snap = list_tail(l)) != NULL) {
2261                 list_remove(l, snap);
2262                 dsl_dataset_rele(snap->ds, tag);
2263                 kmem_free(snap, sizeof (*snap));
2264         }
2265         list_destroy(l);
2266 }
2267
2268 static int
2269 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2270 {
2271         int error;
2272         dsl_dir_t *dd;
2273         struct promotenode *snap;
2274
2275         error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2276             &ddpa->ddpa_clone);
2277         if (error != 0)
2278                 return (error);
2279         dd = ddpa->ddpa_clone->ds_dir;
2280
2281         if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2282             !dsl_dir_is_clone(dd)) {
2283                 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2284                 return (SET_ERROR(EINVAL));
2285         }
2286
2287         error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2288             &ddpa->shared_snaps, tag);
2289         if (error != 0)
2290                 goto out;
2291
2292         error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2293             &ddpa->clone_snaps, tag);
2294         if (error != 0)
2295                 goto out;
2296
2297         snap = list_head(&ddpa->shared_snaps);
2298         ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2299         error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2300             snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2301             &ddpa->origin_snaps, tag);
2302         if (error != 0)
2303                 goto out;
2304
2305         if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2306                 error = dsl_dataset_hold_obj(dp,
2307                     snap->ds->ds_dir->dd_phys->dd_origin_obj,
2308                     tag, &ddpa->origin_origin);
2309                 if (error != 0)
2310                         goto out;
2311         }
2312 out:
2313         if (error != 0)
2314                 promote_rele(ddpa, tag);
2315         return (error);
2316 }
2317
2318 static void
2319 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2320 {
2321         snaplist_destroy(&ddpa->shared_snaps, tag);
2322         snaplist_destroy(&ddpa->clone_snaps, tag);
2323         snaplist_destroy(&ddpa->origin_snaps, tag);
2324         if (ddpa->origin_origin != NULL)
2325                 dsl_dataset_rele(ddpa->origin_origin, tag);
2326         dsl_dataset_rele(ddpa->ddpa_clone, tag);
2327 }
2328
2329 /*
2330  * Promote a clone.
2331  *
2332  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2333  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
2334  */
2335 int
2336 dsl_dataset_promote(const char *name, char *conflsnap)
2337 {
2338         dsl_dataset_promote_arg_t ddpa = { 0 };
2339         uint64_t numsnaps;
2340         int error;
2341         objset_t *os;
2342
2343         /*
2344          * We will modify space proportional to the number of
2345          * snapshots.  Compute numsnaps.
2346          */
2347         error = dmu_objset_hold(name, FTAG, &os);
2348         if (error != 0)
2349                 return (error);
2350         error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2351             dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2352         dmu_objset_rele(os, FTAG);
2353         if (error != 0)
2354                 return (error);
2355
2356         ddpa.ddpa_clonename = name;
2357         ddpa.err_ds = conflsnap;
2358
2359         return (dsl_sync_task(name, dsl_dataset_promote_check,
2360             dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2361 }
2362
2363 int
2364 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2365     dsl_dataset_t *origin_head, boolean_t force)
2366 {
2367         int64_t unused_refres_delta;
2368
2369         /* they should both be heads */
2370         if (dsl_dataset_is_snapshot(clone) ||
2371             dsl_dataset_is_snapshot(origin_head))
2372                 return (SET_ERROR(EINVAL));
2373
2374         /* the branch point should be just before them */
2375         if (clone->ds_prev != origin_head->ds_prev)
2376                 return (SET_ERROR(EINVAL));
2377
2378         /* clone should be the clone (unless they are unrelated) */
2379         if (clone->ds_prev != NULL &&
2380             clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2381             origin_head->ds_object !=
2382             clone->ds_prev->ds_phys->ds_next_snap_obj)
2383                 return (SET_ERROR(EINVAL));
2384
2385         /* the clone should be a child of the origin */
2386         if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2387                 return (SET_ERROR(EINVAL));
2388
2389         /* origin_head shouldn't be modified unless 'force' */
2390         if (!force && dsl_dataset_modified_since_lastsnap(origin_head))
2391                 return (SET_ERROR(ETXTBSY));
2392
2393         /* origin_head should have no long holds (e.g. is not mounted) */
2394         if (dsl_dataset_long_held(origin_head))
2395                 return (SET_ERROR(EBUSY));
2396
2397         /* check amount of any unconsumed refreservation */
2398         unused_refres_delta =
2399             (int64_t)MIN(origin_head->ds_reserved,
2400             origin_head->ds_phys->ds_unique_bytes) -
2401             (int64_t)MIN(origin_head->ds_reserved,
2402             clone->ds_phys->ds_unique_bytes);
2403
2404         if (unused_refres_delta > 0 &&
2405             unused_refres_delta >
2406             dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2407                 return (SET_ERROR(ENOSPC));
2408
2409         /* clone can't be over the head's refquota */
2410         if (origin_head->ds_quota != 0 &&
2411             clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2412                 return (SET_ERROR(EDQUOT));
2413
2414         return (0);
2415 }
2416
2417 void
2418 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2419     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2420 {
2421         dsl_pool_t *dp = dmu_tx_pool(tx);
2422         int64_t unused_refres_delta;
2423
2424         ASSERT(clone->ds_reserved == 0);
2425         ASSERT(origin_head->ds_quota == 0 ||
2426             clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2427
2428         dmu_buf_will_dirty(clone->ds_dbuf, tx);
2429         dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2430
2431         if (clone->ds_objset != NULL) {
2432                 dmu_objset_evict(clone->ds_objset);
2433                 clone->ds_objset = NULL;
2434         }
2435
2436         if (origin_head->ds_objset != NULL) {
2437                 dmu_objset_evict(origin_head->ds_objset);
2438                 origin_head->ds_objset = NULL;
2439         }
2440
2441         unused_refres_delta =
2442             (int64_t)MIN(origin_head->ds_reserved,
2443             origin_head->ds_phys->ds_unique_bytes) -
2444             (int64_t)MIN(origin_head->ds_reserved,
2445             clone->ds_phys->ds_unique_bytes);
2446
2447         /*
2448          * Reset origin's unique bytes, if it exists.
2449          */
2450         if (clone->ds_prev) {
2451                 dsl_dataset_t *origin = clone->ds_prev;
2452                 uint64_t comp, uncomp;
2453
2454                 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2455                 dsl_deadlist_space_range(&clone->ds_deadlist,
2456                     origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2457                     &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2458         }
2459
2460         /* swap blkptrs */
2461         {
2462                 blkptr_t tmp;
2463                 tmp = origin_head->ds_phys->ds_bp;
2464                 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2465                 clone->ds_phys->ds_bp = tmp;
2466         }
2467
2468         /* set dd_*_bytes */
2469         {
2470                 int64_t dused, dcomp, duncomp;
2471                 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2472                 uint64_t odl_used, odl_comp, odl_uncomp;
2473
2474                 ASSERT3U(clone->ds_dir->dd_phys->
2475                     dd_used_breakdown[DD_USED_SNAP], ==, 0);
2476
2477                 dsl_deadlist_space(&clone->ds_deadlist,
2478                     &cdl_used, &cdl_comp, &cdl_uncomp);
2479                 dsl_deadlist_space(&origin_head->ds_deadlist,
2480                     &odl_used, &odl_comp, &odl_uncomp);
2481
2482                 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2483                     (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2484                 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2485                     (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2486                 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2487                     cdl_uncomp -
2488                     (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2489
2490                 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2491                     dused, dcomp, duncomp, tx);
2492                 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2493                     -dused, -dcomp, -duncomp, tx);
2494
2495                 /*
2496                  * The difference in the space used by snapshots is the
2497                  * difference in snapshot space due to the head's
2498                  * deadlist (since that's the only thing that's
2499                  * changing that affects the snapused).
2500                  */
2501                 dsl_deadlist_space_range(&clone->ds_deadlist,
2502                     origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2503                     &cdl_used, &cdl_comp, &cdl_uncomp);
2504                 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2505                     origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2506                     &odl_used, &odl_comp, &odl_uncomp);
2507                 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2508                     DD_USED_HEAD, DD_USED_SNAP, tx);
2509         }
2510
2511         /* swap ds_*_bytes */
2512         SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2513             clone->ds_phys->ds_referenced_bytes);
2514         SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2515             clone->ds_phys->ds_compressed_bytes);
2516         SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2517             clone->ds_phys->ds_uncompressed_bytes);
2518         SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2519             clone->ds_phys->ds_unique_bytes);
2520
2521         /* apply any parent delta for change in unconsumed refreservation */
2522         dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2523             unused_refres_delta, 0, 0, tx);
2524
2525         /*
2526          * Swap deadlists.
2527          */
2528         dsl_deadlist_close(&clone->ds_deadlist);
2529         dsl_deadlist_close(&origin_head->ds_deadlist);
2530         SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2531             clone->ds_phys->ds_deadlist_obj);
2532         dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2533             clone->ds_phys->ds_deadlist_obj);
2534         dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2535             origin_head->ds_phys->ds_deadlist_obj);
2536
2537         dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2538
2539         spa_history_log_internal_ds(clone, "clone swap", tx,
2540             "parent=%s", origin_head->ds_dir->dd_myname);
2541 }
2542
2543 /*
2544  * Given a pool name and a dataset object number in that pool,
2545  * return the name of that dataset.
2546  */
2547 int
2548 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2549 {
2550         dsl_pool_t *dp;
2551         dsl_dataset_t *ds;
2552         int error;
2553
2554         error = dsl_pool_hold(pname, FTAG, &dp);
2555         if (error != 0)
2556                 return (error);
2557
2558         error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2559         if (error == 0) {
2560                 dsl_dataset_name(ds, buf);
2561                 dsl_dataset_rele(ds, FTAG);
2562         }
2563         dsl_pool_rele(dp, FTAG);
2564
2565         return (error);
2566 }
2567
2568 int
2569 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2570     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2571 {
2572         int error = 0;
2573
2574         ASSERT3S(asize, >, 0);
2575
2576         /*
2577          * *ref_rsrv is the portion of asize that will come from any
2578          * unconsumed refreservation space.
2579          */
2580         *ref_rsrv = 0;
2581
2582         mutex_enter(&ds->ds_lock);
2583         /*
2584          * Make a space adjustment for reserved bytes.
2585          */
2586         if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2587                 ASSERT3U(*used, >=,
2588                     ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2589                 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2590                 *ref_rsrv =
2591                     asize - MIN(asize, parent_delta(ds, asize + inflight));
2592         }
2593
2594         if (!check_quota || ds->ds_quota == 0) {
2595                 mutex_exit(&ds->ds_lock);
2596                 return (0);
2597         }
2598         /*
2599          * If they are requesting more space, and our current estimate
2600          * is over quota, they get to try again unless the actual
2601          * on-disk is over quota and there are no pending changes (which
2602          * may free up space for us).
2603          */
2604         if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2605                 if (inflight > 0 ||
2606                     ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2607                         error = SET_ERROR(ERESTART);
2608                 else
2609                         error = SET_ERROR(EDQUOT);
2610         }
2611         mutex_exit(&ds->ds_lock);
2612
2613         return (error);
2614 }
2615
2616 typedef struct dsl_dataset_set_qr_arg {
2617         const char *ddsqra_name;
2618         zprop_source_t ddsqra_source;
2619         uint64_t ddsqra_value;
2620 } dsl_dataset_set_qr_arg_t;
2621
2622
2623 /* ARGSUSED */
2624 static int
2625 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2626 {
2627         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2628         dsl_pool_t *dp = dmu_tx_pool(tx);
2629         dsl_dataset_t *ds;
2630         int error;
2631         uint64_t newval;
2632
2633         if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2634                 return (SET_ERROR(ENOTSUP));
2635
2636         error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2637         if (error != 0)
2638                 return (error);
2639
2640         if (dsl_dataset_is_snapshot(ds)) {
2641                 dsl_dataset_rele(ds, FTAG);
2642                 return (SET_ERROR(EINVAL));
2643         }
2644
2645         error = dsl_prop_predict(ds->ds_dir,
2646             zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2647             ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2648         if (error != 0) {
2649                 dsl_dataset_rele(ds, FTAG);
2650                 return (error);
2651         }
2652
2653         if (newval == 0) {
2654                 dsl_dataset_rele(ds, FTAG);
2655                 return (0);
2656         }
2657
2658         if (newval < ds->ds_phys->ds_referenced_bytes ||
2659             newval < ds->ds_reserved) {
2660                 dsl_dataset_rele(ds, FTAG);
2661                 return (SET_ERROR(ENOSPC));
2662         }
2663
2664         dsl_dataset_rele(ds, FTAG);
2665         return (0);
2666 }
2667
2668 static void
2669 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2670 {
2671         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2672         dsl_pool_t *dp = dmu_tx_pool(tx);
2673         dsl_dataset_t *ds;
2674         uint64_t newval;
2675
2676         VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2677
2678         dsl_prop_set_sync_impl(ds,
2679             zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2680             ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2681             &ddsqra->ddsqra_value, tx);
2682
2683         VERIFY0(dsl_prop_get_int_ds(ds,
2684             zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2685
2686         if (ds->ds_quota != newval) {
2687                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2688                 ds->ds_quota = newval;
2689         }
2690         dsl_dataset_rele(ds, FTAG);
2691 }
2692
2693 int
2694 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2695     uint64_t refquota)
2696 {
2697         dsl_dataset_set_qr_arg_t ddsqra;
2698
2699         ddsqra.ddsqra_name = dsname;
2700         ddsqra.ddsqra_source = source;
2701         ddsqra.ddsqra_value = refquota;
2702
2703         return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2704             dsl_dataset_set_refquota_sync, &ddsqra, 0));
2705 }
2706
2707 static int
2708 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2709 {
2710         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2711         dsl_pool_t *dp = dmu_tx_pool(tx);
2712         dsl_dataset_t *ds;
2713         int error;
2714         uint64_t newval, unique;
2715
2716         if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2717                 return (SET_ERROR(ENOTSUP));
2718
2719         error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2720         if (error != 0)
2721                 return (error);
2722
2723         if (dsl_dataset_is_snapshot(ds)) {
2724                 dsl_dataset_rele(ds, FTAG);
2725                 return (SET_ERROR(EINVAL));
2726         }
2727
2728         error = dsl_prop_predict(ds->ds_dir,
2729             zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2730             ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2731         if (error != 0) {
2732                 dsl_dataset_rele(ds, FTAG);
2733                 return (error);
2734         }
2735
2736         /*
2737          * If we are doing the preliminary check in open context, the
2738          * space estimates may be inaccurate.
2739          */
2740         if (!dmu_tx_is_syncing(tx)) {
2741                 dsl_dataset_rele(ds, FTAG);
2742                 return (0);
2743         }
2744
2745         mutex_enter(&ds->ds_lock);
2746         if (!DS_UNIQUE_IS_ACCURATE(ds))
2747                 dsl_dataset_recalc_head_uniq(ds);
2748         unique = ds->ds_phys->ds_unique_bytes;
2749         mutex_exit(&ds->ds_lock);
2750
2751         if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2752                 uint64_t delta = MAX(unique, newval) -
2753                     MAX(unique, ds->ds_reserved);
2754
2755                 if (delta >
2756                     dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2757                     (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2758                         dsl_dataset_rele(ds, FTAG);
2759                         return (SET_ERROR(ENOSPC));
2760                 }
2761         }
2762
2763         dsl_dataset_rele(ds, FTAG);
2764         return (0);
2765 }
2766
2767 void
2768 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2769     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2770 {
2771         uint64_t newval;
2772         uint64_t unique;
2773         int64_t delta;
2774
2775         dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2776             source, sizeof (value), 1, &value, tx);
2777
2778         VERIFY0(dsl_prop_get_int_ds(ds,
2779             zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2780
2781         dmu_buf_will_dirty(ds->ds_dbuf, tx);
2782         mutex_enter(&ds->ds_dir->dd_lock);
2783         mutex_enter(&ds->ds_lock);
2784         ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2785         unique = ds->ds_phys->ds_unique_bytes;
2786         delta = MAX(0, (int64_t)(newval - unique)) -
2787             MAX(0, (int64_t)(ds->ds_reserved - unique));
2788         ds->ds_reserved = newval;
2789         mutex_exit(&ds->ds_lock);
2790
2791         dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2792         mutex_exit(&ds->ds_dir->dd_lock);
2793 }
2794
2795 static void
2796 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2797 {
2798         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2799         dsl_pool_t *dp = dmu_tx_pool(tx);
2800         dsl_dataset_t *ds;
2801
2802         VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2803         dsl_dataset_set_refreservation_sync_impl(ds,
2804             ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2805         dsl_dataset_rele(ds, FTAG);
2806 }
2807
2808 int
2809 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2810     uint64_t refreservation)
2811 {
2812         dsl_dataset_set_qr_arg_t ddsqra;
2813
2814         ddsqra.ddsqra_name = dsname;
2815         ddsqra.ddsqra_source = source;
2816         ddsqra.ddsqra_value = refreservation;
2817
2818         return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2819             dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2820 }
2821
2822 /*
2823  * Return (in *usedp) the amount of space written in new that is not
2824  * present in oldsnap.  New may be a snapshot or the head.  Old must be
2825  * a snapshot before new, in new's filesystem (or its origin).  If not then
2826  * fail and return EINVAL.
2827  *
2828  * The written space is calculated by considering two components:  First, we
2829  * ignore any freed space, and calculate the written as new's used space
2830  * minus old's used space.  Next, we add in the amount of space that was freed
2831  * between the two snapshots, thus reducing new's used space relative to old's.
2832  * Specifically, this is the space that was born before old->ds_creation_txg,
2833  * and freed before new (ie. on new's deadlist or a previous deadlist).
2834  *
2835  * space freed                         [---------------------]
2836  * snapshots                       ---O-------O--------O-------O------
2837  *                                         oldsnap            new
2838  */
2839 int
2840 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2841     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2842 {
2843         int err = 0;
2844         uint64_t snapobj;
2845         dsl_pool_t *dp = new->ds_dir->dd_pool;
2846
2847         ASSERT(dsl_pool_config_held(dp));
2848
2849         *usedp = 0;
2850         *usedp += new->ds_phys->ds_referenced_bytes;
2851         *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
2852
2853         *compp = 0;
2854         *compp += new->ds_phys->ds_compressed_bytes;
2855         *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2856
2857         *uncompp = 0;
2858         *uncompp += new->ds_phys->ds_uncompressed_bytes;
2859         *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2860
2861         snapobj = new->ds_object;
2862         while (snapobj != oldsnap->ds_object) {
2863                 dsl_dataset_t *snap;
2864                 uint64_t used, comp, uncomp;
2865
2866                 if (snapobj == new->ds_object) {
2867                         snap = new;
2868                 } else {
2869                         err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2870                         if (err != 0)
2871                                 break;
2872                 }
2873
2874                 if (snap->ds_phys->ds_prev_snap_txg ==
2875                     oldsnap->ds_phys->ds_creation_txg) {
2876                         /*
2877                          * The blocks in the deadlist can not be born after
2878                          * ds_prev_snap_txg, so get the whole deadlist space,
2879                          * which is more efficient (especially for old-format
2880                          * deadlists).  Unfortunately the deadlist code
2881                          * doesn't have enough information to make this
2882                          * optimization itself.
2883                          */
2884                         dsl_deadlist_space(&snap->ds_deadlist,
2885                             &used, &comp, &uncomp);
2886                 } else {
2887                         dsl_deadlist_space_range(&snap->ds_deadlist,
2888                             0, oldsnap->ds_phys->ds_creation_txg,
2889                             &used, &comp, &uncomp);
2890                 }
2891                 *usedp += used;
2892                 *compp += comp;
2893                 *uncompp += uncomp;
2894
2895                 /*
2896                  * If we get to the beginning of the chain of snapshots
2897                  * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2898                  * was not a snapshot of/before new.
2899                  */
2900                 snapobj = snap->ds_phys->ds_prev_snap_obj;
2901                 if (snap != new)
2902                         dsl_dataset_rele(snap, FTAG);
2903                 if (snapobj == 0) {
2904                         err = SET_ERROR(EINVAL);
2905                         break;
2906                 }
2907
2908         }
2909         return (err);
2910 }
2911
2912 /*
2913  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2914  * lastsnap, and all snapshots in between are deleted.
2915  *
2916  * blocks that would be freed            [---------------------------]
2917  * snapshots                       ---O-------O--------O-------O--------O
2918  *                                        firstsnap        lastsnap
2919  *
2920  * This is the set of blocks that were born after the snap before firstsnap,
2921  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2922  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2923  * We calculate this by iterating over the relevant deadlists (from the snap
2924  * after lastsnap, backward to the snap after firstsnap), summing up the
2925  * space on the deadlist that was born after the snap before firstsnap.
2926  */
2927 int
2928 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2929     dsl_dataset_t *lastsnap,
2930     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2931 {
2932         int err = 0;
2933         uint64_t snapobj;
2934         dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
2935
2936         ASSERT(dsl_dataset_is_snapshot(firstsnap));
2937         ASSERT(dsl_dataset_is_snapshot(lastsnap));
2938
2939         /*
2940          * Check that the snapshots are in the same dsl_dir, and firstsnap
2941          * is before lastsnap.
2942          */
2943         if (firstsnap->ds_dir != lastsnap->ds_dir ||
2944             firstsnap->ds_phys->ds_creation_txg >
2945             lastsnap->ds_phys->ds_creation_txg)
2946                 return (SET_ERROR(EINVAL));
2947
2948         *usedp = *compp = *uncompp = 0;
2949
2950         snapobj = lastsnap->ds_phys->ds_next_snap_obj;
2951         while (snapobj != firstsnap->ds_object) {
2952                 dsl_dataset_t *ds;
2953                 uint64_t used, comp, uncomp;
2954
2955                 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
2956                 if (err != 0)
2957                         break;
2958
2959                 dsl_deadlist_space_range(&ds->ds_deadlist,
2960                     firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2961                     &used, &comp, &uncomp);
2962                 *usedp += used;
2963                 *compp += comp;
2964                 *uncompp += uncomp;
2965
2966                 snapobj = ds->ds_phys->ds_prev_snap_obj;
2967                 ASSERT3U(snapobj, !=, 0);
2968                 dsl_dataset_rele(ds, FTAG);
2969         }
2970         return (err);
2971 }
2972
2973 /*
2974  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
2975  * For example, they could both be snapshots of the same filesystem, and
2976  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
2977  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
2978  * filesystem.  Or 'earlier' could be the origin's origin.
2979  */
2980 boolean_t
2981 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
2982 {
2983         dsl_pool_t *dp = later->ds_dir->dd_pool;
2984         int error;
2985         boolean_t ret;
2986
2987         ASSERT(dsl_pool_config_held(dp));
2988
2989         if (earlier->ds_phys->ds_creation_txg >=
2990             later->ds_phys->ds_creation_txg)
2991                 return (B_FALSE);
2992
2993         if (later->ds_dir == earlier->ds_dir)
2994                 return (B_TRUE);
2995         if (!dsl_dir_is_clone(later->ds_dir))
2996                 return (B_FALSE);
2997
2998         if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
2999                 return (B_TRUE);
3000         dsl_dataset_t *origin;
3001         error = dsl_dataset_hold_obj(dp,
3002             later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
3003         if (error != 0)
3004                 return (B_FALSE);
3005         ret = dsl_dataset_is_before(origin, earlier);
3006         dsl_dataset_rele(origin, FTAG);
3007         return (ret);
3008 }