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