]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dsl_dataset.c
14e7ced4007c842dc6b4dd02516d0875f4c104ca
[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 https://opensource.org/licenses/CDDL-1.0.
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 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 RackTop Systems.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
29 * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright (c) 2019, Klara Inc.
32 * Copyright (c) 2019, Allan Jude
33 * Copyright (c) 2020 The FreeBSD Foundation [1]
34 *
35 * [1] Portions of this software were developed by Allan Jude
36 * under sponsorship from the FreeBSD Foundation.
37 */
38
39 #include <sys/dmu_objset.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_prop.h>
43 #include <sys/dsl_synctask.h>
44 #include <sys/dmu_traverse.h>
45 #include <sys/dmu_impl.h>
46 #include <sys/dmu_tx.h>
47 #include <sys/arc.h>
48 #include <sys/zio.h>
49 #include <sys/zap.h>
50 #include <sys/zfeature.h>
51 #include <sys/unique.h>
52 #include <sys/zfs_context.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/spa.h>
55 #include <sys/spa_impl.h>
56 #include <sys/vdev.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/zfs_onexit.h>
59 #include <sys/zvol.h>
60 #include <sys/dsl_scan.h>
61 #include <sys/dsl_deadlist.h>
62 #include <sys/dsl_destroy.h>
63 #include <sys/dsl_userhold.h>
64 #include <sys/dsl_bookmark.h>
65 #include <sys/policy.h>
66 #include <sys/dmu_send.h>
67 #include <sys/dmu_recv.h>
68 #include <sys/zio_compress.h>
69 #include <zfs_fletcher.h>
70 #include <sys/zio_checksum.h>
71
72 /*
73 * The SPA supports block sizes up to 16MB. However, very large blocks
74 * can have an impact on i/o latency (e.g. tying up a spinning disk for
75 * ~300ms), and also potentially on the memory allocator. Therefore,
76 * we did not allow the recordsize to be set larger than zfs_max_recordsize
77 * (former default: 1MB). Larger blocks could be created by changing this
78 * tunable, and pools with larger blocks could always be imported and used,
79 * regardless of this setting.
80 *
81 * We do, however, still limit it by default to 1M on x86_32, because Linux's
82 * 3/1 memory split doesn't leave much room for 16M chunks.
83 */
84 #ifdef _ILP32
85 uint_t zfs_max_recordsize = 1 * 1024 * 1024;
86 #else
87 uint_t zfs_max_recordsize = 16 * 1024 * 1024;
88 #endif
89 static int zfs_allow_redacted_dataset_mount = 0;
90
91 int zfs_snapshot_history_enabled = 1;
92
93 #define SWITCH64(x, y) \
94 { \
95 uint64_t __tmp = (x); \
96 (x) = (y); \
97 (y) = __tmp; \
98 }
99
100 #define DS_REF_MAX (1ULL << 62)
101
102 static void dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds,
103 uint64_t obj, dmu_tx_t *tx);
104 static void dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds,
105 dmu_tx_t *tx);
106
107 static void unload_zfeature(dsl_dataset_t *ds, spa_feature_t f);
108
109 extern uint_t spa_asize_inflation;
110
111 static zil_header_t zero_zil;
112
113 /*
114 * Figure out how much of this delta should be propagated to the dsl_dir
115 * layer. If there's a refreservation, that space has already been
116 * partially accounted for in our ancestors.
117 */
118 static int64_t
119 parent_delta(dsl_dataset_t *ds, int64_t delta)
120 {
121 dsl_dataset_phys_t *ds_phys;
122 uint64_t old_bytes, new_bytes;
123
124 if (ds->ds_reserved == 0)
125 return (delta);
126
127 ds_phys = dsl_dataset_phys(ds);
128 old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
129 new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
130
131 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
132 return (new_bytes - old_bytes);
133 }
134
135 void
136 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
137 {
138 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
139 int used = bp_get_dsize_sync(spa, bp);
140 int compressed = BP_GET_PSIZE(bp);
141 int uncompressed = BP_GET_UCSIZE(bp);
142 int64_t delta;
143 spa_feature_t f;
144
145 dprintf_bp(bp, "ds=%p", ds);
146
147 ASSERT(dmu_tx_is_syncing(tx));
148 /* It could have been compressed away to nothing */
149 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
150 return;
151 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
152 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
153 if (ds == NULL) {
154 dsl_pool_mos_diduse_space(tx->tx_pool,
155 used, compressed, uncompressed);
156 return;
157 }
158
159 ASSERT3U(bp->blk_birth, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
160 dmu_buf_will_dirty(ds->ds_dbuf, tx);
161 mutex_enter(&ds->ds_lock);
162 delta = parent_delta(ds, used);
163 dsl_dataset_phys(ds)->ds_referenced_bytes += used;
164 dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
165 dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
166 dsl_dataset_phys(ds)->ds_unique_bytes += used;
167
168 if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
169 ds->ds_feature_activation[SPA_FEATURE_LARGE_BLOCKS] =
170 (void *)B_TRUE;
171 }
172
173
174 f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
175 if (f != SPA_FEATURE_NONE) {
176 ASSERT3S(spa_feature_table[f].fi_type, ==,
177 ZFEATURE_TYPE_BOOLEAN);
178 ds->ds_feature_activation[f] = (void *)B_TRUE;
179 }
180
181 f = zio_compress_to_feature(BP_GET_COMPRESS(bp));
182 if (f != SPA_FEATURE_NONE) {
183 ASSERT3S(spa_feature_table[f].fi_type, ==,
184 ZFEATURE_TYPE_BOOLEAN);
185 ds->ds_feature_activation[f] = (void *)B_TRUE;
186 }
187
188 /*
189 * Track block for livelist, but ignore embedded blocks because
190 * they do not need to be freed.
191 */
192 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
193 bp->blk_birth > ds->ds_dir->dd_origin_txg &&
194 !(BP_IS_EMBEDDED(bp))) {
195 ASSERT(dsl_dir_is_clone(ds->ds_dir));
196 ASSERT(spa_feature_is_enabled(spa,
197 SPA_FEATURE_LIVELIST));
198 bplist_append(&ds->ds_dir->dd_pending_allocs, bp);
199 }
200
201 mutex_exit(&ds->ds_lock);
202 dsl_dir_diduse_transfer_space(ds->ds_dir, delta,
203 compressed, uncompressed, used,
204 DD_USED_REFRSRV, DD_USED_HEAD, tx);
205 }
206
207 /*
208 * Called when the specified segment has been remapped, and is thus no
209 * longer referenced in the head dataset. The vdev must be indirect.
210 *
211 * If the segment is referenced by a snapshot, put it on the remap deadlist.
212 * Otherwise, add this segment to the obsolete spacemap.
213 */
214 void
215 dsl_dataset_block_remapped(dsl_dataset_t *ds, uint64_t vdev, uint64_t offset,
216 uint64_t size, uint64_t birth, dmu_tx_t *tx)
217 {
218 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
219
220 ASSERT(dmu_tx_is_syncing(tx));
221 ASSERT(birth <= tx->tx_txg);
222 ASSERT(!ds->ds_is_snapshot);
223
224 if (birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
225 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
226 } else {
227 blkptr_t fakebp;
228 dva_t *dva = &fakebp.blk_dva[0];
229
230 ASSERT(ds != NULL);
231
232 mutex_enter(&ds->ds_remap_deadlist_lock);
233 if (!dsl_dataset_remap_deadlist_exists(ds)) {
234 dsl_dataset_create_remap_deadlist(ds, tx);
235 }
236 mutex_exit(&ds->ds_remap_deadlist_lock);
237
238 BP_ZERO(&fakebp);
239 fakebp.blk_birth = birth;
240 DVA_SET_VDEV(dva, vdev);
241 DVA_SET_OFFSET(dva, offset);
242 DVA_SET_ASIZE(dva, size);
243 dsl_deadlist_insert(&ds->ds_remap_deadlist, &fakebp, B_FALSE,
244 tx);
245 }
246 }
247
248 int
249 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
250 boolean_t async)
251 {
252 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
253
254 int used = bp_get_dsize_sync(spa, bp);
255 int compressed = BP_GET_PSIZE(bp);
256 int uncompressed = BP_GET_UCSIZE(bp);
257
258 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
259 return (0);
260
261 ASSERT(dmu_tx_is_syncing(tx));
262 ASSERT(bp->blk_birth <= tx->tx_txg);
263
264 if (ds == NULL) {
265 dsl_free(tx->tx_pool, tx->tx_txg, bp);
266 dsl_pool_mos_diduse_space(tx->tx_pool,
267 -used, -compressed, -uncompressed);
268 return (used);
269 }
270 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
271
272 ASSERT(!ds->ds_is_snapshot);
273 dmu_buf_will_dirty(ds->ds_dbuf, tx);
274
275 /*
276 * Track block for livelist, but ignore embedded blocks because
277 * they do not need to be freed.
278 */
279 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
280 bp->blk_birth > ds->ds_dir->dd_origin_txg &&
281 !(BP_IS_EMBEDDED(bp))) {
282 ASSERT(dsl_dir_is_clone(ds->ds_dir));
283 ASSERT(spa_feature_is_enabled(spa,
284 SPA_FEATURE_LIVELIST));
285 bplist_append(&ds->ds_dir->dd_pending_frees, bp);
286 }
287
288 if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
289 int64_t delta;
290
291 dprintf_bp(bp, "freeing ds=%llu", (u_longlong_t)ds->ds_object);
292 dsl_free(tx->tx_pool, tx->tx_txg, bp);
293
294 mutex_enter(&ds->ds_lock);
295 ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
296 !DS_UNIQUE_IS_ACCURATE(ds));
297 delta = parent_delta(ds, -used);
298 dsl_dataset_phys(ds)->ds_unique_bytes -= used;
299 mutex_exit(&ds->ds_lock);
300 dsl_dir_diduse_transfer_space(ds->ds_dir,
301 delta, -compressed, -uncompressed, -used,
302 DD_USED_REFRSRV, DD_USED_HEAD, tx);
303 } else {
304 dprintf_bp(bp, "putting on dead list: %s", "");
305 if (async) {
306 /*
307 * We are here as part of zio's write done callback,
308 * which means we're a zio interrupt thread. We can't
309 * call dsl_deadlist_insert() now because it may block
310 * waiting for I/O. Instead, put bp on the deferred
311 * queue and let dsl_pool_sync() finish the job.
312 */
313 bplist_append(&ds->ds_pending_deadlist, bp);
314 } else {
315 dsl_deadlist_insert(&ds->ds_deadlist, bp, B_FALSE, tx);
316 }
317 ASSERT3U(ds->ds_prev->ds_object, ==,
318 dsl_dataset_phys(ds)->ds_prev_snap_obj);
319 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
320 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
321 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
322 ds->ds_object && bp->blk_birth >
323 dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
324 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
325 mutex_enter(&ds->ds_prev->ds_lock);
326 dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
327 mutex_exit(&ds->ds_prev->ds_lock);
328 }
329 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
330 dsl_dir_transfer_space(ds->ds_dir, used,
331 DD_USED_HEAD, DD_USED_SNAP, tx);
332 }
333 }
334
335 dsl_bookmark_block_killed(ds, bp, tx);
336
337 mutex_enter(&ds->ds_lock);
338 ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
339 dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
340 ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
341 dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
342 ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
343 dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
344 mutex_exit(&ds->ds_lock);
345
346 return (used);
347 }
348
349 struct feature_type_uint64_array_arg {
350 uint64_t length;
351 uint64_t *array;
352 };
353
354 static void
355 unload_zfeature(dsl_dataset_t *ds, spa_feature_t f)
356 {
357 switch (spa_feature_table[f].fi_type) {
358 case ZFEATURE_TYPE_BOOLEAN:
359 break;
360 case ZFEATURE_TYPE_UINT64_ARRAY:
361 {
362 struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f];
363 kmem_free(ftuaa->array, ftuaa->length * sizeof (uint64_t));
364 kmem_free(ftuaa, sizeof (*ftuaa));
365 break;
366 }
367 default:
368 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
369 }
370 }
371
372 static int
373 load_zfeature(objset_t *mos, dsl_dataset_t *ds, spa_feature_t f)
374 {
375 int err = 0;
376 switch (spa_feature_table[f].fi_type) {
377 case ZFEATURE_TYPE_BOOLEAN:
378 err = zap_contains(mos, ds->ds_object,
379 spa_feature_table[f].fi_guid);
380 if (err == 0) {
381 ds->ds_feature[f] = (void *)B_TRUE;
382 } else {
383 ASSERT3U(err, ==, ENOENT);
384 err = 0;
385 }
386 break;
387 case ZFEATURE_TYPE_UINT64_ARRAY:
388 {
389 uint64_t int_size, num_int;
390 uint64_t *data;
391 err = zap_length(mos, ds->ds_object,
392 spa_feature_table[f].fi_guid, &int_size, &num_int);
393 if (err != 0) {
394 ASSERT3U(err, ==, ENOENT);
395 err = 0;
396 break;
397 }
398 ASSERT3U(int_size, ==, sizeof (uint64_t));
399 data = kmem_alloc(int_size * num_int, KM_SLEEP);
400 VERIFY0(zap_lookup(mos, ds->ds_object,
401 spa_feature_table[f].fi_guid, int_size, num_int, data));
402 struct feature_type_uint64_array_arg *ftuaa =
403 kmem_alloc(sizeof (*ftuaa), KM_SLEEP);
404 ftuaa->length = num_int;
405 ftuaa->array = data;
406 ds->ds_feature[f] = ftuaa;
407 break;
408 }
409 default:
410 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
411 }
412 return (err);
413 }
414
415 /*
416 * We have to release the fsid synchronously or we risk that a subsequent
417 * mount of the same dataset will fail to unique_insert the fsid. This
418 * failure would manifest itself as the fsid of this dataset changing
419 * between mounts which makes NFS clients quite unhappy.
420 */
421 static void
422 dsl_dataset_evict_sync(void *dbu)
423 {
424 dsl_dataset_t *ds = dbu;
425
426 ASSERT(ds->ds_owner == NULL);
427
428 unique_remove(ds->ds_fsid_guid);
429 }
430
431 static void
432 dsl_dataset_evict_async(void *dbu)
433 {
434 dsl_dataset_t *ds = dbu;
435
436 ASSERT(ds->ds_owner == NULL);
437
438 ds->ds_dbuf = NULL;
439
440 if (ds->ds_objset != NULL)
441 dmu_objset_evict(ds->ds_objset);
442
443 if (ds->ds_prev) {
444 dsl_dataset_rele(ds->ds_prev, ds);
445 ds->ds_prev = NULL;
446 }
447
448 dsl_bookmark_fini_ds(ds);
449
450 bplist_destroy(&ds->ds_pending_deadlist);
451 if (dsl_deadlist_is_open(&ds->ds_deadlist))
452 dsl_deadlist_close(&ds->ds_deadlist);
453 if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
454 dsl_deadlist_close(&ds->ds_remap_deadlist);
455 if (ds->ds_dir)
456 dsl_dir_async_rele(ds->ds_dir, ds);
457
458 ASSERT(!list_link_active(&ds->ds_synced_link));
459
460 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
461 if (dsl_dataset_feature_is_active(ds, f))
462 unload_zfeature(ds, f);
463 }
464
465 list_destroy(&ds->ds_prop_cbs);
466 mutex_destroy(&ds->ds_lock);
467 mutex_destroy(&ds->ds_opening_lock);
468 mutex_destroy(&ds->ds_sendstream_lock);
469 mutex_destroy(&ds->ds_remap_deadlist_lock);
470 zfs_refcount_destroy(&ds->ds_longholds);
471 rrw_destroy(&ds->ds_bp_rwlock);
472
473 kmem_free(ds, sizeof (dsl_dataset_t));
474 }
475
476 int
477 dsl_dataset_get_snapname(dsl_dataset_t *ds)
478 {
479 dsl_dataset_phys_t *headphys;
480 int err;
481 dmu_buf_t *headdbuf;
482 dsl_pool_t *dp = ds->ds_dir->dd_pool;
483 objset_t *mos = dp->dp_meta_objset;
484
485 if (ds->ds_snapname[0])
486 return (0);
487 if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
488 return (0);
489
490 err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
491 FTAG, &headdbuf);
492 if (err != 0)
493 return (err);
494 headphys = headdbuf->db_data;
495 err = zap_value_search(dp->dp_meta_objset,
496 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
497 if (err != 0 && zfs_recover == B_TRUE) {
498 err = 0;
499 (void) snprintf(ds->ds_snapname, sizeof (ds->ds_snapname),
500 "SNAPOBJ=%llu-ERR=%d",
501 (unsigned long long)ds->ds_object, err);
502 }
503 dmu_buf_rele(headdbuf, FTAG);
504 return (err);
505 }
506
507 int
508 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
509 {
510 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
511 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
512 matchtype_t mt = 0;
513 int err;
514
515 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
516 mt = MT_NORMALIZE;
517
518 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
519 value, mt, NULL, 0, NULL);
520 if (err == ENOTSUP && (mt & MT_NORMALIZE))
521 err = zap_lookup(mos, snapobj, name, 8, 1, value);
522 return (err);
523 }
524
525 int
526 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
527 boolean_t adj_cnt)
528 {
529 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
530 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
531 matchtype_t mt = 0;
532 int err;
533
534 dsl_dir_snap_cmtime_update(ds->ds_dir, tx);
535
536 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
537 mt = MT_NORMALIZE;
538
539 err = zap_remove_norm(mos, snapobj, name, mt, tx);
540 if (err == ENOTSUP && (mt & MT_NORMALIZE))
541 err = zap_remove(mos, snapobj, name, tx);
542
543 if (err == 0 && adj_cnt)
544 dsl_fs_ss_count_adjust(ds->ds_dir, -1,
545 DD_FIELD_SNAPSHOT_COUNT, tx);
546
547 return (err);
548 }
549
550 boolean_t
551 dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, const void *tag)
552 {
553 dmu_buf_t *dbuf = ds->ds_dbuf;
554 boolean_t result = B_FALSE;
555
556 if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
557 ds->ds_object, DMU_BONUS_BLKID, tag)) {
558
559 if (ds == dmu_buf_get_user(dbuf))
560 result = B_TRUE;
561 else
562 dmu_buf_rele(dbuf, tag);
563 }
564
565 return (result);
566 }
567
568 int
569 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, const void *tag,
570 dsl_dataset_t **dsp)
571 {
572 objset_t *mos = dp->dp_meta_objset;
573 dmu_buf_t *dbuf;
574 dsl_dataset_t *ds;
575 int err;
576 dmu_object_info_t doi;
577
578 ASSERT(dsl_pool_config_held(dp));
579
580 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
581 if (err != 0)
582 return (err);
583
584 /* Make sure dsobj has the correct object type. */
585 dmu_object_info_from_db(dbuf, &doi);
586 if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
587 dmu_buf_rele(dbuf, tag);
588 return (SET_ERROR(EINVAL));
589 }
590
591 ds = dmu_buf_get_user(dbuf);
592 if (ds == NULL) {
593 dsl_dataset_t *winner = NULL;
594
595 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
596 ds->ds_dbuf = dbuf;
597 ds->ds_object = dsobj;
598 ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
599 list_link_init(&ds->ds_synced_link);
600
601 err = dsl_dir_hold_obj(dp, dsl_dataset_phys(ds)->ds_dir_obj,
602 NULL, ds, &ds->ds_dir);
603 if (err != 0) {
604 kmem_free(ds, sizeof (dsl_dataset_t));
605 dmu_buf_rele(dbuf, tag);
606 return (err);
607 }
608
609 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
610 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
611 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
612 mutex_init(&ds->ds_remap_deadlist_lock,
613 NULL, MUTEX_DEFAULT, NULL);
614 rrw_init(&ds->ds_bp_rwlock, B_FALSE);
615 zfs_refcount_create(&ds->ds_longholds);
616
617 bplist_create(&ds->ds_pending_deadlist);
618
619 list_create(&ds->ds_sendstreams, sizeof (dmu_sendstatus_t),
620 offsetof(dmu_sendstatus_t, dss_link));
621
622 list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
623 offsetof(dsl_prop_cb_record_t, cbr_ds_node));
624
625 if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
626 spa_feature_t f;
627
628 for (f = 0; f < SPA_FEATURES; f++) {
629 if (!(spa_feature_table[f].fi_flags &
630 ZFEATURE_FLAG_PER_DATASET))
631 continue;
632 err = load_zfeature(mos, ds, f);
633 }
634 }
635
636 if (!ds->ds_is_snapshot) {
637 ds->ds_snapname[0] = '\0';
638 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
639 err = dsl_dataset_hold_obj(dp,
640 dsl_dataset_phys(ds)->ds_prev_snap_obj,
641 ds, &ds->ds_prev);
642 }
643 if (err != 0)
644 goto after_dsl_bookmark_fini;
645 err = dsl_bookmark_init_ds(ds);
646 } else {
647 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
648 err = dsl_dataset_get_snapname(ds);
649 if (err == 0 &&
650 dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
651 err = zap_count(
652 ds->ds_dir->dd_pool->dp_meta_objset,
653 dsl_dataset_phys(ds)->ds_userrefs_obj,
654 &ds->ds_userrefs);
655 }
656 }
657
658 if (err == 0 && !ds->ds_is_snapshot) {
659 err = dsl_prop_get_int_ds(ds,
660 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
661 &ds->ds_reserved);
662 if (err == 0) {
663 err = dsl_prop_get_int_ds(ds,
664 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
665 &ds->ds_quota);
666 }
667 } else {
668 ds->ds_reserved = ds->ds_quota = 0;
669 }
670
671 if (err == 0 && ds->ds_dir->dd_crypto_obj != 0 &&
672 ds->ds_is_snapshot &&
673 zap_contains(mos, dsobj, DS_FIELD_IVSET_GUID) != 0) {
674 dp->dp_spa->spa_errata =
675 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION;
676 }
677
678 dsl_deadlist_open(&ds->ds_deadlist,
679 mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
680 uint64_t remap_deadlist_obj =
681 dsl_dataset_get_remap_deadlist_object(ds);
682 if (remap_deadlist_obj != 0) {
683 dsl_deadlist_open(&ds->ds_remap_deadlist, mos,
684 remap_deadlist_obj);
685 }
686
687 dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
688 dsl_dataset_evict_async, &ds->ds_dbuf);
689 if (err == 0)
690 winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
691
692 if (err != 0 || winner != NULL) {
693 dsl_deadlist_close(&ds->ds_deadlist);
694 if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
695 dsl_deadlist_close(&ds->ds_remap_deadlist);
696 dsl_bookmark_fini_ds(ds);
697 after_dsl_bookmark_fini:
698 if (ds->ds_prev)
699 dsl_dataset_rele(ds->ds_prev, ds);
700 dsl_dir_rele(ds->ds_dir, ds);
701 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
702 if (dsl_dataset_feature_is_active(ds, f))
703 unload_zfeature(ds, f);
704 }
705
706 list_destroy(&ds->ds_prop_cbs);
707 list_destroy(&ds->ds_sendstreams);
708 bplist_destroy(&ds->ds_pending_deadlist);
709 mutex_destroy(&ds->ds_lock);
710 mutex_destroy(&ds->ds_opening_lock);
711 mutex_destroy(&ds->ds_sendstream_lock);
712 mutex_destroy(&ds->ds_remap_deadlist_lock);
713 zfs_refcount_destroy(&ds->ds_longholds);
714 rrw_destroy(&ds->ds_bp_rwlock);
715 kmem_free(ds, sizeof (dsl_dataset_t));
716 if (err != 0) {
717 dmu_buf_rele(dbuf, tag);
718 return (err);
719 }
720 ds = winner;
721 } else {
722 ds->ds_fsid_guid =
723 unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
724 if (ds->ds_fsid_guid !=
725 dsl_dataset_phys(ds)->ds_fsid_guid) {
726 zfs_dbgmsg("ds_fsid_guid changed from "
727 "%llx to %llx for pool %s dataset id %llu",
728 (long long)
729 dsl_dataset_phys(ds)->ds_fsid_guid,
730 (long long)ds->ds_fsid_guid,
731 spa_name(dp->dp_spa),
732 (u_longlong_t)dsobj);
733 }
734 }
735 }
736
737 ASSERT3P(ds->ds_dbuf, ==, dbuf);
738 ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
739 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
740 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
741 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
742 *dsp = ds;
743
744 return (0);
745 }
746
747 int
748 dsl_dataset_create_key_mapping(dsl_dataset_t *ds)
749 {
750 dsl_dir_t *dd = ds->ds_dir;
751
752 if (dd->dd_crypto_obj == 0)
753 return (0);
754
755 return (spa_keystore_create_mapping(dd->dd_pool->dp_spa,
756 ds, ds, &ds->ds_key_mapping));
757 }
758
759 int
760 dsl_dataset_hold_obj_flags(dsl_pool_t *dp, uint64_t dsobj,
761 ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp)
762 {
763 int err;
764
765 err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
766 if (err != 0)
767 return (err);
768
769 ASSERT3P(*dsp, !=, NULL);
770
771 if (flags & DS_HOLD_FLAG_DECRYPT) {
772 err = dsl_dataset_create_key_mapping(*dsp);
773 if (err != 0)
774 dsl_dataset_rele(*dsp, tag);
775 }
776
777 return (err);
778 }
779
780 int
781 dsl_dataset_hold_flags(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
782 const void *tag, dsl_dataset_t **dsp)
783 {
784 dsl_dir_t *dd;
785 const char *snapname;
786 uint64_t obj;
787 int err = 0;
788 dsl_dataset_t *ds;
789
790 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
791 if (err != 0)
792 return (err);
793
794 ASSERT(dsl_pool_config_held(dp));
795 obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
796 if (obj != 0)
797 err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag, &ds);
798 else
799 err = SET_ERROR(ENOENT);
800
801 /* we may be looking for a snapshot */
802 if (err == 0 && snapname != NULL) {
803 dsl_dataset_t *snap_ds;
804
805 if (*snapname++ != '@') {
806 dsl_dataset_rele_flags(ds, flags, tag);
807 dsl_dir_rele(dd, FTAG);
808 return (SET_ERROR(ENOENT));
809 }
810
811 dprintf("looking for snapshot '%s'\n", snapname);
812 err = dsl_dataset_snap_lookup(ds, snapname, &obj);
813 if (err == 0) {
814 err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag,
815 &snap_ds);
816 }
817 dsl_dataset_rele_flags(ds, flags, tag);
818
819 if (err == 0) {
820 mutex_enter(&snap_ds->ds_lock);
821 if (snap_ds->ds_snapname[0] == 0)
822 (void) strlcpy(snap_ds->ds_snapname, snapname,
823 sizeof (snap_ds->ds_snapname));
824 mutex_exit(&snap_ds->ds_lock);
825 ds = snap_ds;
826 }
827 }
828 if (err == 0)
829 *dsp = ds;
830 dsl_dir_rele(dd, FTAG);
831 return (err);
832 }
833
834 int
835 dsl_dataset_hold(dsl_pool_t *dp, const char *name, const void *tag,
836 dsl_dataset_t **dsp)
837 {
838 return (dsl_dataset_hold_flags(dp, name, 0, tag, dsp));
839 }
840
841 static int
842 dsl_dataset_own_obj_impl(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
843 const void *tag, boolean_t override, dsl_dataset_t **dsp)
844 {
845 int err = dsl_dataset_hold_obj_flags(dp, dsobj, flags, tag, dsp);
846 if (err != 0)
847 return (err);
848 if (!dsl_dataset_tryown(*dsp, tag, override)) {
849 dsl_dataset_rele_flags(*dsp, flags, tag);
850 *dsp = NULL;
851 return (SET_ERROR(EBUSY));
852 }
853 return (0);
854 }
855
856
857 int
858 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
859 const void *tag, dsl_dataset_t **dsp)
860 {
861 return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_FALSE, dsp));
862 }
863
864 int
865 dsl_dataset_own_obj_force(dsl_pool_t *dp, uint64_t dsobj,
866 ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp)
867 {
868 return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_TRUE, dsp));
869 }
870
871 static int
872 dsl_dataset_own_impl(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
873 const void *tag, boolean_t override, dsl_dataset_t **dsp)
874 {
875 int err = dsl_dataset_hold_flags(dp, name, flags, tag, dsp);
876 if (err != 0)
877 return (err);
878 if (!dsl_dataset_tryown(*dsp, tag, override)) {
879 dsl_dataset_rele_flags(*dsp, flags, tag);
880 return (SET_ERROR(EBUSY));
881 }
882 return (0);
883 }
884
885 int
886 dsl_dataset_own_force(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
887 const void *tag, dsl_dataset_t **dsp)
888 {
889 return (dsl_dataset_own_impl(dp, name, flags, tag, B_TRUE, dsp));
890 }
891
892 int
893 dsl_dataset_own(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
894 const void *tag, dsl_dataset_t **dsp)
895 {
896 return (dsl_dataset_own_impl(dp, name, flags, tag, B_FALSE, dsp));
897 }
898
899 /*
900 * See the comment above dsl_pool_hold() for details. In summary, a long
901 * hold is used to prevent destruction of a dataset while the pool hold
902 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
903 *
904 * The dataset and pool must be held when this function is called. After it
905 * is called, the pool hold may be released while the dataset is still held
906 * and accessed.
907 */
908 void
909 dsl_dataset_long_hold(dsl_dataset_t *ds, const void *tag)
910 {
911 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
912 (void) zfs_refcount_add(&ds->ds_longholds, tag);
913 }
914
915 void
916 dsl_dataset_long_rele(dsl_dataset_t *ds, const void *tag)
917 {
918 (void) zfs_refcount_remove(&ds->ds_longholds, tag);
919 }
920
921 /* Return B_TRUE if there are any long holds on this dataset. */
922 boolean_t
923 dsl_dataset_long_held(dsl_dataset_t *ds)
924 {
925 return (!zfs_refcount_is_zero(&ds->ds_longholds));
926 }
927
928 void
929 dsl_dataset_name(dsl_dataset_t *ds, char *name)
930 {
931 if (ds == NULL) {
932 (void) strlcpy(name, "mos", ZFS_MAX_DATASET_NAME_LEN);
933 } else {
934 dsl_dir_name(ds->ds_dir, name);
935 VERIFY0(dsl_dataset_get_snapname(ds));
936 if (ds->ds_snapname[0]) {
937 VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
938 <, ZFS_MAX_DATASET_NAME_LEN);
939 /*
940 * We use a "recursive" mutex so that we
941 * can call dprintf_ds() with ds_lock held.
942 */
943 if (!MUTEX_HELD(&ds->ds_lock)) {
944 mutex_enter(&ds->ds_lock);
945 VERIFY3U(strlcat(name, ds->ds_snapname,
946 ZFS_MAX_DATASET_NAME_LEN), <,
947 ZFS_MAX_DATASET_NAME_LEN);
948 mutex_exit(&ds->ds_lock);
949 } else {
950 VERIFY3U(strlcat(name, ds->ds_snapname,
951 ZFS_MAX_DATASET_NAME_LEN), <,
952 ZFS_MAX_DATASET_NAME_LEN);
953 }
954 }
955 }
956 }
957
958 int
959 dsl_dataset_namelen(dsl_dataset_t *ds)
960 {
961 VERIFY0(dsl_dataset_get_snapname(ds));
962 mutex_enter(&ds->ds_lock);
963 int len = strlen(ds->ds_snapname);
964 mutex_exit(&ds->ds_lock);
965 /* add '@' if ds is a snap */
966 if (len > 0)
967 len++;
968 len += dsl_dir_namelen(ds->ds_dir);
969 return (len);
970 }
971
972 void
973 dsl_dataset_rele(dsl_dataset_t *ds, const void *tag)
974 {
975 dmu_buf_rele(ds->ds_dbuf, tag);
976 }
977
978 void
979 dsl_dataset_remove_key_mapping(dsl_dataset_t *ds)
980 {
981 dsl_dir_t *dd = ds->ds_dir;
982
983 if (dd == NULL || dd->dd_crypto_obj == 0)
984 return;
985
986 (void) spa_keystore_remove_mapping(dd->dd_pool->dp_spa,
987 ds->ds_object, ds);
988 }
989
990 void
991 dsl_dataset_rele_flags(dsl_dataset_t *ds, ds_hold_flags_t flags,
992 const void *tag)
993 {
994 if (flags & DS_HOLD_FLAG_DECRYPT)
995 dsl_dataset_remove_key_mapping(ds);
996
997 dsl_dataset_rele(ds, tag);
998 }
999
1000 void
1001 dsl_dataset_disown(dsl_dataset_t *ds, ds_hold_flags_t flags, const void *tag)
1002 {
1003 ASSERT3P(ds->ds_owner, ==, tag);
1004 ASSERT(ds->ds_dbuf != NULL);
1005
1006 mutex_enter(&ds->ds_lock);
1007 ds->ds_owner = NULL;
1008 mutex_exit(&ds->ds_lock);
1009 dsl_dataset_long_rele(ds, tag);
1010 dsl_dataset_rele_flags(ds, flags, tag);
1011 }
1012
1013 boolean_t
1014 dsl_dataset_tryown(dsl_dataset_t *ds, const void *tag, boolean_t override)
1015 {
1016 boolean_t gotit = FALSE;
1017
1018 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1019 mutex_enter(&ds->ds_lock);
1020 if (ds->ds_owner == NULL && (override || !(DS_IS_INCONSISTENT(ds) ||
1021 (dsl_dataset_feature_is_active(ds,
1022 SPA_FEATURE_REDACTED_DATASETS) &&
1023 !zfs_allow_redacted_dataset_mount)))) {
1024 ds->ds_owner = tag;
1025 dsl_dataset_long_hold(ds, tag);
1026 gotit = TRUE;
1027 }
1028 mutex_exit(&ds->ds_lock);
1029 return (gotit);
1030 }
1031
1032 boolean_t
1033 dsl_dataset_has_owner(dsl_dataset_t *ds)
1034 {
1035 boolean_t rv;
1036 mutex_enter(&ds->ds_lock);
1037 rv = (ds->ds_owner != NULL);
1038 mutex_exit(&ds->ds_lock);
1039 return (rv);
1040 }
1041
1042 static boolean_t
1043 zfeature_active(spa_feature_t f, void *arg)
1044 {
1045 switch (spa_feature_table[f].fi_type) {
1046 case ZFEATURE_TYPE_BOOLEAN: {
1047 boolean_t val = (boolean_t)(uintptr_t)arg;
1048 ASSERT(val == B_FALSE || val == B_TRUE);
1049 return (val);
1050 }
1051 case ZFEATURE_TYPE_UINT64_ARRAY:
1052 /*
1053 * In this case, arg is a uint64_t array. The feature is active
1054 * if the array is non-null.
1055 */
1056 return (arg != NULL);
1057 default:
1058 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
1059 return (B_FALSE);
1060 }
1061 }
1062
1063 boolean_t
1064 dsl_dataset_feature_is_active(dsl_dataset_t *ds, spa_feature_t f)
1065 {
1066 return (zfeature_active(f, ds->ds_feature[f]));
1067 }
1068
1069 /*
1070 * The buffers passed out by this function are references to internal buffers;
1071 * they should not be freed by callers of this function, and they should not be
1072 * used after the dataset has been released.
1073 */
1074 boolean_t
1075 dsl_dataset_get_uint64_array_feature(dsl_dataset_t *ds, spa_feature_t f,
1076 uint64_t *outlength, uint64_t **outp)
1077 {
1078 VERIFY(spa_feature_table[f].fi_type & ZFEATURE_TYPE_UINT64_ARRAY);
1079 if (!dsl_dataset_feature_is_active(ds, f)) {
1080 return (B_FALSE);
1081 }
1082 struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f];
1083 *outp = ftuaa->array;
1084 *outlength = ftuaa->length;
1085 return (B_TRUE);
1086 }
1087
1088 void
1089 dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, void *arg,
1090 dmu_tx_t *tx)
1091 {
1092 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1093 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
1094 uint64_t zero = 0;
1095
1096 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
1097
1098 spa_feature_incr(spa, f, tx);
1099 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1100
1101 switch (spa_feature_table[f].fi_type) {
1102 case ZFEATURE_TYPE_BOOLEAN:
1103 ASSERT3S((boolean_t)(uintptr_t)arg, ==, B_TRUE);
1104 VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
1105 sizeof (zero), 1, &zero, tx));
1106 break;
1107 case ZFEATURE_TYPE_UINT64_ARRAY:
1108 {
1109 struct feature_type_uint64_array_arg *ftuaa = arg;
1110 VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
1111 sizeof (uint64_t), ftuaa->length, ftuaa->array, tx));
1112 break;
1113 }
1114 default:
1115 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
1116 }
1117 }
1118
1119 static void
1120 dsl_dataset_deactivate_feature_impl(dsl_dataset_t *ds, spa_feature_t f,
1121 dmu_tx_t *tx)
1122 {
1123 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1124 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
1125 uint64_t dsobj = ds->ds_object;
1126
1127 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
1128
1129 VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
1130 spa_feature_decr(spa, f, tx);
1131 ds->ds_feature[f] = NULL;
1132 }
1133
1134 void
1135 dsl_dataset_deactivate_feature(dsl_dataset_t *ds, spa_feature_t f, dmu_tx_t *tx)
1136 {
1137 unload_zfeature(ds, f);
1138 dsl_dataset_deactivate_feature_impl(ds, f, tx);
1139 }
1140
1141 uint64_t
1142 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
1143 dsl_crypto_params_t *dcp, uint64_t flags, dmu_tx_t *tx)
1144 {
1145 dsl_pool_t *dp = dd->dd_pool;
1146 dmu_buf_t *dbuf;
1147 dsl_dataset_phys_t *dsphys;
1148 uint64_t dsobj;
1149 objset_t *mos = dp->dp_meta_objset;
1150
1151 if (origin == NULL)
1152 origin = dp->dp_origin_snap;
1153
1154 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
1155 ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
1156 ASSERT(dmu_tx_is_syncing(tx));
1157 ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1158
1159 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1160 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1161 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1162 dmu_buf_will_dirty(dbuf, tx);
1163 dsphys = dbuf->db_data;
1164 memset(dsphys, 0, sizeof (dsl_dataset_phys_t));
1165 dsphys->ds_dir_obj = dd->dd_object;
1166 dsphys->ds_flags = flags;
1167 dsphys->ds_fsid_guid = unique_create();
1168 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1169 sizeof (dsphys->ds_guid));
1170 dsphys->ds_snapnames_zapobj =
1171 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
1172 DMU_OT_NONE, 0, tx);
1173 dsphys->ds_creation_time = gethrestime_sec();
1174 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
1175
1176 if (origin == NULL) {
1177 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
1178 } else {
1179 dsl_dataset_t *ohds; /* head of the origin snapshot */
1180
1181 dsphys->ds_prev_snap_obj = origin->ds_object;
1182 dsphys->ds_prev_snap_txg =
1183 dsl_dataset_phys(origin)->ds_creation_txg;
1184 dsphys->ds_referenced_bytes =
1185 dsl_dataset_phys(origin)->ds_referenced_bytes;
1186 dsphys->ds_compressed_bytes =
1187 dsl_dataset_phys(origin)->ds_compressed_bytes;
1188 dsphys->ds_uncompressed_bytes =
1189 dsl_dataset_phys(origin)->ds_uncompressed_bytes;
1190 rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
1191 dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
1192 rrw_exit(&origin->ds_bp_rwlock, FTAG);
1193
1194 /*
1195 * Inherit flags that describe the dataset's contents
1196 * (INCONSISTENT) or properties (Case Insensitive).
1197 */
1198 dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
1199 (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
1200
1201 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1202 if (zfeature_active(f, origin->ds_feature[f])) {
1203 dsl_dataset_activate_feature(dsobj, f,
1204 origin->ds_feature[f], tx);
1205 }
1206 }
1207
1208 dmu_buf_will_dirty(origin->ds_dbuf, tx);
1209 dsl_dataset_phys(origin)->ds_num_children++;
1210
1211 VERIFY0(dsl_dataset_hold_obj(dp,
1212 dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
1213 FTAG, &ohds));
1214 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
1215 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
1216 dsl_dataset_rele(ohds, FTAG);
1217
1218 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
1219 if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
1220 dsl_dataset_phys(origin)->ds_next_clones_obj =
1221 zap_create(mos,
1222 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
1223 }
1224 VERIFY0(zap_add_int(mos,
1225 dsl_dataset_phys(origin)->ds_next_clones_obj,
1226 dsobj, tx));
1227 }
1228
1229 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1230 dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
1231 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1232 if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
1233 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
1234 dsl_dir_phys(origin->ds_dir)->dd_clones =
1235 zap_create(mos,
1236 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
1237 }
1238 VERIFY0(zap_add_int(mos,
1239 dsl_dir_phys(origin->ds_dir)->dd_clones,
1240 dsobj, tx));
1241 }
1242 }
1243
1244 /* handle encryption */
1245 dsl_dataset_create_crypt_sync(dsobj, dd, origin, dcp, tx);
1246
1247 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1248 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1249
1250 dmu_buf_rele(dbuf, FTAG);
1251
1252 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1253 dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
1254
1255 return (dsobj);
1256 }
1257
1258 static void
1259 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
1260 {
1261 objset_t *os;
1262
1263 VERIFY0(dmu_objset_from_ds(ds, &os));
1264 if (memcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
1265 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1266 zio_t *zio;
1267
1268 memset(&os->os_zil_header, 0, sizeof (os->os_zil_header));
1269 if (os->os_encrypted)
1270 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
1271
1272 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1273 dsl_dataset_sync(ds, zio, tx);
1274 VERIFY0(zio_wait(zio));
1275 dsl_dataset_sync_done(ds, tx);
1276 }
1277 }
1278
1279 uint64_t
1280 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
1281 dsl_dataset_t *origin, uint64_t flags, cred_t *cr,
1282 dsl_crypto_params_t *dcp, dmu_tx_t *tx)
1283 {
1284 dsl_pool_t *dp = pdd->dd_pool;
1285 uint64_t dsobj, ddobj;
1286 dsl_dir_t *dd;
1287
1288 ASSERT(dmu_tx_is_syncing(tx));
1289 ASSERT(lastname[0] != '@');
1290 /*
1291 * Filesystems will eventually have their origin set to dp_origin_snap,
1292 * but that's taken care of in dsl_dataset_create_sync_dd. When
1293 * creating a filesystem, this function is called with origin equal to
1294 * NULL.
1295 */
1296 if (origin != NULL)
1297 ASSERT3P(origin, !=, dp->dp_origin_snap);
1298
1299 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
1300 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
1301
1302 dsobj = dsl_dataset_create_sync_dd(dd, origin, dcp,
1303 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
1304
1305 dsl_deleg_set_create_perms(dd, tx, cr);
1306
1307 /*
1308 * If we are creating a clone and the livelist feature is enabled,
1309 * add the entry DD_FIELD_LIVELIST to ZAP.
1310 */
1311 if (origin != NULL &&
1312 spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LIVELIST)) {
1313 objset_t *mos = dd->dd_pool->dp_meta_objset;
1314 dsl_dir_zapify(dd, tx);
1315 uint64_t obj = dsl_deadlist_alloc(mos, tx);
1316 VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_LIVELIST,
1317 sizeof (uint64_t), 1, &obj, tx));
1318 spa_feature_incr(dp->dp_spa, SPA_FEATURE_LIVELIST, tx);
1319 }
1320
1321 /*
1322 * Since we're creating a new node we know it's a leaf, so we can
1323 * initialize the counts if the limit feature is active.
1324 */
1325 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
1326 uint64_t cnt = 0;
1327 objset_t *os = dd->dd_pool->dp_meta_objset;
1328
1329 dsl_dir_zapify(dd, tx);
1330 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1331 sizeof (cnt), 1, &cnt, tx));
1332 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1333 sizeof (cnt), 1, &cnt, tx));
1334 }
1335
1336 dsl_dir_rele(dd, FTAG);
1337
1338 /*
1339 * If we are creating a clone, make sure we zero out any stale
1340 * data from the origin snapshots zil header.
1341 */
1342 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
1343 dsl_dataset_t *ds;
1344
1345 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1346 dsl_dataset_zero_zil(ds, tx);
1347 dsl_dataset_rele(ds, FTAG);
1348 }
1349
1350 return (dsobj);
1351 }
1352
1353 /*
1354 * The unique space in the head dataset can be calculated by subtracting
1355 * the space used in the most recent snapshot, that is still being used
1356 * in this file system, from the space currently in use. To figure out
1357 * the space in the most recent snapshot still in use, we need to take
1358 * the total space used in the snapshot and subtract out the space that
1359 * has been freed up since the snapshot was taken.
1360 */
1361 void
1362 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1363 {
1364 uint64_t mrs_used;
1365 uint64_t dlused, dlcomp, dluncomp;
1366
1367 ASSERT(!ds->ds_is_snapshot);
1368
1369 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
1370 mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
1371 else
1372 mrs_used = 0;
1373
1374 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1375
1376 ASSERT3U(dlused, <=, mrs_used);
1377 dsl_dataset_phys(ds)->ds_unique_bytes =
1378 dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
1379
1380 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1381 SPA_VERSION_UNIQUE_ACCURATE)
1382 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1383 }
1384
1385 void
1386 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
1387 dmu_tx_t *tx)
1388 {
1389 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1390 uint64_t count __maybe_unused;
1391 int err;
1392
1393 ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1394 err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1395 obj, tx);
1396 /*
1397 * The err should not be ENOENT, but a bug in a previous version
1398 * of the code could cause upgrade_clones_cb() to not set
1399 * ds_next_snap_obj when it should, leading to a missing entry.
1400 * If we knew that the pool was created after
1401 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1402 * ENOENT. However, at least we can check that we don't have
1403 * too many entries in the next_clones_obj even after failing to
1404 * remove this one.
1405 */
1406 if (err != ENOENT)
1407 VERIFY0(err);
1408 ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1409 &count));
1410 ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1411 }
1412
1413
1414 blkptr_t *
1415 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1416 {
1417 return (&dsl_dataset_phys(ds)->ds_bp);
1418 }
1419
1420 spa_t *
1421 dsl_dataset_get_spa(dsl_dataset_t *ds)
1422 {
1423 return (ds->ds_dir->dd_pool->dp_spa);
1424 }
1425
1426 void
1427 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1428 {
1429 dsl_pool_t *dp;
1430
1431 if (ds == NULL) /* this is the meta-objset */
1432 return;
1433
1434 ASSERT(ds->ds_objset != NULL);
1435
1436 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1437 panic("dirtying snapshot!");
1438
1439 /* Must not dirty a dataset in the same txg where it got snapshotted. */
1440 ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
1441
1442 dp = ds->ds_dir->dd_pool;
1443 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1444 objset_t *os = ds->ds_objset;
1445
1446 /* up the hold count until we can be written out */
1447 dmu_buf_add_ref(ds->ds_dbuf, ds);
1448
1449 /* if this dataset is encrypted, grab a reference to the DCK */
1450 if (ds->ds_dir->dd_crypto_obj != 0 &&
1451 !os->os_raw_receive &&
1452 !os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1453 ASSERT3P(ds->ds_key_mapping, !=, NULL);
1454 key_mapping_add_ref(ds->ds_key_mapping, ds);
1455 }
1456 }
1457 }
1458
1459 static int
1460 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1461 {
1462 uint64_t asize;
1463
1464 if (!dmu_tx_is_syncing(tx))
1465 return (0);
1466
1467 /*
1468 * If there's an fs-only reservation, any blocks that might become
1469 * owned by the snapshot dataset must be accommodated by space
1470 * outside of the reservation.
1471 */
1472 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1473 asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
1474 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1475 return (SET_ERROR(ENOSPC));
1476
1477 /*
1478 * Propagate any reserved space for this snapshot to other
1479 * snapshot checks in this sync group.
1480 */
1481 if (asize > 0)
1482 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1483
1484 return (0);
1485 }
1486
1487 int
1488 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1489 dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr, proc_t *proc)
1490 {
1491 int error;
1492 uint64_t value;
1493
1494 ds->ds_trysnap_txg = tx->tx_txg;
1495
1496 if (!dmu_tx_is_syncing(tx))
1497 return (0);
1498
1499 /*
1500 * We don't allow multiple snapshots of the same txg. If there
1501 * is already one, try again.
1502 */
1503 if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1504 return (SET_ERROR(EAGAIN));
1505
1506 /*
1507 * Check for conflicting snapshot name.
1508 */
1509 error = dsl_dataset_snap_lookup(ds, snapname, &value);
1510 if (error == 0)
1511 return (SET_ERROR(EEXIST));
1512 if (error != ENOENT)
1513 return (error);
1514
1515 /*
1516 * We don't allow taking snapshots of inconsistent datasets, such as
1517 * those into which we are currently receiving. However, if we are
1518 * creating this snapshot as part of a receive, this check will be
1519 * executed atomically with respect to the completion of the receive
1520 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1521 * case we ignore this, knowing it will be fixed up for us shortly in
1522 * dmu_recv_end_sync().
1523 */
1524 if (!recv && DS_IS_INCONSISTENT(ds))
1525 return (SET_ERROR(EBUSY));
1526
1527 /*
1528 * Skip the check for temporary snapshots or if we have already checked
1529 * the counts in dsl_dataset_snapshot_check. This means we really only
1530 * check the count here when we're receiving a stream.
1531 */
1532 if (cnt != 0 && cr != NULL) {
1533 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1534 ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr, proc);
1535 if (error != 0)
1536 return (error);
1537 }
1538
1539 error = dsl_dataset_snapshot_reserve_space(ds, tx);
1540 if (error != 0)
1541 return (error);
1542
1543 return (0);
1544 }
1545
1546 int
1547 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1548 {
1549 dsl_dataset_snapshot_arg_t *ddsa = arg;
1550 dsl_pool_t *dp = dmu_tx_pool(tx);
1551 nvpair_t *pair;
1552 int rv = 0;
1553
1554 /*
1555 * Pre-compute how many total new snapshots will be created for each
1556 * level in the tree and below. This is needed for validating the
1557 * snapshot limit when either taking a recursive snapshot or when
1558 * taking multiple snapshots.
1559 *
1560 * The problem is that the counts are not actually adjusted when
1561 * we are checking, only when we finally sync. For a single snapshot,
1562 * this is easy, the count will increase by 1 at each node up the tree,
1563 * but its more complicated for the recursive/multiple snapshot case.
1564 *
1565 * The dsl_fs_ss_limit_check function does recursively check the count
1566 * at each level up the tree but since it is validating each snapshot
1567 * independently we need to be sure that we are validating the complete
1568 * count for the entire set of snapshots. We do this by rolling up the
1569 * counts for each component of the name into an nvlist and then
1570 * checking each of those cases with the aggregated count.
1571 *
1572 * This approach properly handles not only the recursive snapshot
1573 * case (where we get all of those on the ddsa_snaps list) but also
1574 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1575 * validate the limit on 'a' using a count of 2).
1576 *
1577 * We validate the snapshot names in the third loop and only report
1578 * name errors once.
1579 */
1580 if (dmu_tx_is_syncing(tx)) {
1581 char *nm;
1582 nvlist_t *cnt_track = NULL;
1583 cnt_track = fnvlist_alloc();
1584
1585 nm = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1586
1587 /* Rollup aggregated counts into the cnt_track list */
1588 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1589 pair != NULL;
1590 pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1591 char *pdelim;
1592 uint64_t val;
1593
1594 (void) strlcpy(nm, nvpair_name(pair), MAXPATHLEN);
1595 pdelim = strchr(nm, '@');
1596 if (pdelim == NULL)
1597 continue;
1598 *pdelim = '\0';
1599
1600 do {
1601 if (nvlist_lookup_uint64(cnt_track, nm,
1602 &val) == 0) {
1603 /* update existing entry */
1604 fnvlist_add_uint64(cnt_track, nm,
1605 val + 1);
1606 } else {
1607 /* add to list */
1608 fnvlist_add_uint64(cnt_track, nm, 1);
1609 }
1610
1611 pdelim = strrchr(nm, '/');
1612 if (pdelim != NULL)
1613 *pdelim = '\0';
1614 } while (pdelim != NULL);
1615 }
1616
1617 kmem_free(nm, MAXPATHLEN);
1618
1619 /* Check aggregated counts at each level */
1620 for (pair = nvlist_next_nvpair(cnt_track, NULL);
1621 pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1622 int error = 0;
1623 const char *name;
1624 uint64_t cnt = 0;
1625 dsl_dataset_t *ds;
1626
1627 name = nvpair_name(pair);
1628 cnt = fnvpair_value_uint64(pair);
1629 ASSERT(cnt > 0);
1630
1631 error = dsl_dataset_hold(dp, name, FTAG, &ds);
1632 if (error == 0) {
1633 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1634 ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1635 ddsa->ddsa_cr, ddsa->ddsa_proc);
1636 dsl_dataset_rele(ds, FTAG);
1637 }
1638
1639 if (error != 0) {
1640 if (ddsa->ddsa_errors != NULL)
1641 fnvlist_add_int32(ddsa->ddsa_errors,
1642 name, error);
1643 rv = error;
1644 /* only report one error for this check */
1645 break;
1646 }
1647 }
1648 nvlist_free(cnt_track);
1649 }
1650
1651 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1652 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1653 int error = 0;
1654 dsl_dataset_t *ds;
1655 const char *name, *atp = NULL;
1656 char dsname[ZFS_MAX_DATASET_NAME_LEN];
1657
1658 name = nvpair_name(pair);
1659 if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1660 error = SET_ERROR(ENAMETOOLONG);
1661 if (error == 0) {
1662 atp = strchr(name, '@');
1663 if (atp == NULL)
1664 error = SET_ERROR(EINVAL);
1665 if (error == 0)
1666 (void) strlcpy(dsname, name, atp - name + 1);
1667 }
1668 if (error == 0)
1669 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1670 if (error == 0) {
1671 /* passing 0/NULL skips dsl_fs_ss_limit_check */
1672 error = dsl_dataset_snapshot_check_impl(ds,
1673 atp + 1, tx, B_FALSE, 0, NULL, NULL);
1674 dsl_dataset_rele(ds, FTAG);
1675 }
1676
1677 if (error != 0) {
1678 if (ddsa->ddsa_errors != NULL) {
1679 fnvlist_add_int32(ddsa->ddsa_errors,
1680 name, error);
1681 }
1682 rv = error;
1683 }
1684 }
1685
1686 return (rv);
1687 }
1688
1689 void
1690 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1691 dmu_tx_t *tx)
1692 {
1693 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1694 dmu_buf_t *dbuf;
1695 dsl_dataset_phys_t *dsphys;
1696 uint64_t dsobj, crtxg;
1697 objset_t *mos = dp->dp_meta_objset;
1698 objset_t *os __maybe_unused;
1699
1700 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1701
1702 /*
1703 * If we are on an old pool, the zil must not be active, in which
1704 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1705 */
1706 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1707 dmu_objset_from_ds(ds, &os) != 0 ||
1708 memcmp(&os->os_phys->os_zil_header, &zero_zil,
1709 sizeof (zero_zil)) == 0);
1710
1711 /* Should not snapshot a dirty dataset. */
1712 ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1713 ds, tx->tx_txg));
1714
1715 dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1716
1717 /*
1718 * The origin's ds_creation_txg has to be < TXG_INITIAL
1719 */
1720 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1721 crtxg = 1;
1722 else
1723 crtxg = tx->tx_txg;
1724
1725 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1726 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1727 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1728 dmu_buf_will_dirty(dbuf, tx);
1729 dsphys = dbuf->db_data;
1730 memset(dsphys, 0, sizeof (dsl_dataset_phys_t));
1731 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1732 dsphys->ds_fsid_guid = unique_create();
1733 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1734 sizeof (dsphys->ds_guid));
1735 dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1736 dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1737 dsphys->ds_next_snap_obj = ds->ds_object;
1738 dsphys->ds_num_children = 1;
1739 dsphys->ds_creation_time = gethrestime_sec();
1740 dsphys->ds_creation_txg = crtxg;
1741 dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1742 dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1743 dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1744 dsphys->ds_uncompressed_bytes =
1745 dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1746 dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1747 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1748 dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1749 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1750 dmu_buf_rele(dbuf, FTAG);
1751
1752 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1753 if (zfeature_active(f, ds->ds_feature[f])) {
1754 dsl_dataset_activate_feature(dsobj, f,
1755 ds->ds_feature[f], tx);
1756 }
1757 }
1758
1759 ASSERT3U(ds->ds_prev != 0, ==,
1760 dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1761 if (ds->ds_prev) {
1762 uint64_t next_clones_obj =
1763 dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1764 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1765 ds->ds_object ||
1766 dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1767 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1768 ds->ds_object) {
1769 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1770 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1771 dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1772 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1773 } else if (next_clones_obj != 0) {
1774 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1775 dsphys->ds_next_snap_obj, tx);
1776 VERIFY0(zap_add_int(mos,
1777 next_clones_obj, dsobj, tx));
1778 }
1779 }
1780
1781 /*
1782 * If we have a reference-reservation on this dataset, we will
1783 * need to increase the amount of refreservation being charged
1784 * since our unique space is going to zero.
1785 */
1786 if (ds->ds_reserved) {
1787 int64_t delta;
1788 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1789 delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1790 ds->ds_reserved);
1791 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1792 delta, 0, 0, tx);
1793 }
1794
1795 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1796 dsl_dataset_phys(ds)->ds_deadlist_obj =
1797 dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1798 dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1799 dsl_deadlist_close(&ds->ds_deadlist);
1800 dsl_deadlist_open(&ds->ds_deadlist, mos,
1801 dsl_dataset_phys(ds)->ds_deadlist_obj);
1802 dsl_deadlist_add_key(&ds->ds_deadlist,
1803 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1804 dsl_bookmark_snapshotted(ds, tx);
1805
1806 if (dsl_dataset_remap_deadlist_exists(ds)) {
1807 uint64_t remap_deadlist_obj =
1808 dsl_dataset_get_remap_deadlist_object(ds);
1809 /*
1810 * Move the remap_deadlist to the snapshot. The head
1811 * will create a new remap deadlist on demand, from
1812 * dsl_dataset_block_remapped().
1813 */
1814 dsl_dataset_unset_remap_deadlist_object(ds, tx);
1815 dsl_deadlist_close(&ds->ds_remap_deadlist);
1816
1817 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1818 VERIFY0(zap_add(mos, dsobj, DS_FIELD_REMAP_DEADLIST,
1819 sizeof (remap_deadlist_obj), 1, &remap_deadlist_obj, tx));
1820 }
1821
1822 /*
1823 * Create a ivset guid for this snapshot if the dataset is
1824 * encrypted. This may be overridden by a raw receive. A
1825 * previous implementation of this code did not have this
1826 * field as part of the on-disk format for ZFS encryption
1827 * (see errata #4). As part of the remediation for this
1828 * issue, we ask the user to enable the bookmark_v2 feature
1829 * which is now a dependency of the encryption feature. We
1830 * use this as a heuristic to determine when the user has
1831 * elected to correct any datasets created with the old code.
1832 * As a result, we only do this step if the bookmark_v2
1833 * feature is enabled, which limits the number of states a
1834 * given pool / dataset can be in with regards to terms of
1835 * correcting the issue.
1836 */
1837 if (ds->ds_dir->dd_crypto_obj != 0 &&
1838 spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2)) {
1839 uint64_t ivset_guid = unique_create();
1840
1841 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1842 VERIFY0(zap_add(mos, dsobj, DS_FIELD_IVSET_GUID,
1843 sizeof (ivset_guid), 1, &ivset_guid, tx));
1844 }
1845
1846 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1847 dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1848 dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1849 dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1850
1851 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1852 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1853
1854 VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1855 snapname, 8, 1, &dsobj, tx));
1856
1857 if (ds->ds_prev)
1858 dsl_dataset_rele(ds->ds_prev, ds);
1859 VERIFY0(dsl_dataset_hold_obj(dp,
1860 dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1861
1862 dsl_scan_ds_snapshotted(ds, tx);
1863
1864 dsl_dir_snap_cmtime_update(ds->ds_dir, tx);
1865
1866 if (zfs_snapshot_history_enabled)
1867 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, " ");
1868 }
1869
1870 void
1871 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1872 {
1873 dsl_dataset_snapshot_arg_t *ddsa = arg;
1874 dsl_pool_t *dp = dmu_tx_pool(tx);
1875 nvpair_t *pair;
1876
1877 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1878 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1879 dsl_dataset_t *ds;
1880 const char *name, *atp;
1881 char dsname[ZFS_MAX_DATASET_NAME_LEN];
1882
1883 name = nvpair_name(pair);
1884 atp = strchr(name, '@');
1885 (void) strlcpy(dsname, name, atp - name + 1);
1886 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1887
1888 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1889 if (ddsa->ddsa_props != NULL) {
1890 dsl_props_set_sync_impl(ds->ds_prev,
1891 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1892 }
1893 dsl_dataset_rele(ds, FTAG);
1894 }
1895 }
1896
1897 /*
1898 * The snapshots must all be in the same pool.
1899 * All-or-nothing: if there are any failures, nothing will be modified.
1900 */
1901 int
1902 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1903 {
1904 dsl_dataset_snapshot_arg_t ddsa;
1905 nvpair_t *pair;
1906 boolean_t needsuspend;
1907 int error;
1908 spa_t *spa;
1909 const char *firstname;
1910 nvlist_t *suspended = NULL;
1911
1912 pair = nvlist_next_nvpair(snaps, NULL);
1913 if (pair == NULL)
1914 return (0);
1915 firstname = nvpair_name(pair);
1916
1917 error = spa_open(firstname, &spa, FTAG);
1918 if (error != 0)
1919 return (error);
1920 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1921 spa_close(spa, FTAG);
1922
1923 if (needsuspend) {
1924 suspended = fnvlist_alloc();
1925 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1926 pair = nvlist_next_nvpair(snaps, pair)) {
1927 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1928 const char *snapname = nvpair_name(pair);
1929 const char *atp;
1930 void *cookie;
1931
1932 atp = strchr(snapname, '@');
1933 if (atp == NULL) {
1934 error = SET_ERROR(EINVAL);
1935 break;
1936 }
1937 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1938
1939 error = zil_suspend(fsname, &cookie);
1940 if (error != 0)
1941 break;
1942 fnvlist_add_uint64(suspended, fsname,
1943 (uintptr_t)cookie);
1944 }
1945 }
1946
1947 ddsa.ddsa_snaps = snaps;
1948 ddsa.ddsa_props = props;
1949 ddsa.ddsa_errors = errors;
1950 ddsa.ddsa_cr = CRED();
1951 ddsa.ddsa_proc = curproc;
1952
1953 if (error == 0) {
1954 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1955 dsl_dataset_snapshot_sync, &ddsa,
1956 fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
1957 }
1958
1959 if (suspended != NULL) {
1960 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1961 pair = nvlist_next_nvpair(suspended, pair)) {
1962 zil_resume((void *)(uintptr_t)
1963 fnvpair_value_uint64(pair));
1964 }
1965 fnvlist_free(suspended);
1966 }
1967
1968 if (error == 0) {
1969 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1970 pair = nvlist_next_nvpair(snaps, pair)) {
1971 zvol_create_minor(nvpair_name(pair));
1972 }
1973 }
1974
1975 return (error);
1976 }
1977
1978 typedef struct dsl_dataset_snapshot_tmp_arg {
1979 const char *ddsta_fsname;
1980 const char *ddsta_snapname;
1981 minor_t ddsta_cleanup_minor;
1982 const char *ddsta_htag;
1983 } dsl_dataset_snapshot_tmp_arg_t;
1984
1985 static int
1986 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1987 {
1988 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1989 dsl_pool_t *dp = dmu_tx_pool(tx);
1990 dsl_dataset_t *ds;
1991 int error;
1992
1993 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1994 if (error != 0)
1995 return (error);
1996
1997 /* NULL cred means no limit check for tmp snapshot */
1998 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1999 tx, B_FALSE, 0, NULL, NULL);
2000 if (error != 0) {
2001 dsl_dataset_rele(ds, FTAG);
2002 return (error);
2003 }
2004
2005 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
2006 dsl_dataset_rele(ds, FTAG);
2007 return (SET_ERROR(ENOTSUP));
2008 }
2009 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
2010 B_TRUE, tx);
2011 if (error != 0) {
2012 dsl_dataset_rele(ds, FTAG);
2013 return (error);
2014 }
2015
2016 dsl_dataset_rele(ds, FTAG);
2017 return (0);
2018 }
2019
2020 static void
2021 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
2022 {
2023 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
2024 dsl_pool_t *dp = dmu_tx_pool(tx);
2025 dsl_dataset_t *ds = NULL;
2026
2027 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
2028
2029 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
2030 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
2031 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
2032 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
2033
2034 dsl_dataset_rele(ds, FTAG);
2035 }
2036
2037 int
2038 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
2039 minor_t cleanup_minor, const char *htag)
2040 {
2041 dsl_dataset_snapshot_tmp_arg_t ddsta;
2042 int error;
2043 spa_t *spa;
2044 boolean_t needsuspend;
2045 void *cookie;
2046
2047 ddsta.ddsta_fsname = fsname;
2048 ddsta.ddsta_snapname = snapname;
2049 ddsta.ddsta_cleanup_minor = cleanup_minor;
2050 ddsta.ddsta_htag = htag;
2051
2052 error = spa_open(fsname, &spa, FTAG);
2053 if (error != 0)
2054 return (error);
2055 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
2056 spa_close(spa, FTAG);
2057
2058 if (needsuspend) {
2059 error = zil_suspend(fsname, &cookie);
2060 if (error != 0)
2061 return (error);
2062 }
2063
2064 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
2065 dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
2066
2067 if (needsuspend)
2068 zil_resume(cookie);
2069 return (error);
2070 }
2071
2072 void
2073 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2074 {
2075 ASSERT(dmu_tx_is_syncing(tx));
2076 ASSERT(ds->ds_objset != NULL);
2077 ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
2078
2079 /*
2080 * in case we had to change ds_fsid_guid when we opened it,
2081 * sync it out now.
2082 */
2083 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2084 dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
2085
2086 if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
2087 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2088 ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
2089 &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
2090 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2091 ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
2092 &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
2093 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2094 ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
2095 &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
2096 ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
2097 ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
2098 ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
2099 }
2100
2101 dmu_objset_sync(ds->ds_objset, zio, tx);
2102 }
2103
2104 /*
2105 * Check if the percentage of blocks shared between the clone and the
2106 * snapshot (as opposed to those that are clone only) is below a certain
2107 * threshold
2108 */
2109 static boolean_t
2110 dsl_livelist_should_disable(dsl_dataset_t *ds)
2111 {
2112 uint64_t used, referenced;
2113 int percent_shared;
2114
2115 used = dsl_dir_get_usedds(ds->ds_dir);
2116 referenced = dsl_get_referenced(ds);
2117 if (referenced == 0)
2118 return (B_FALSE);
2119 percent_shared = (100 * (referenced - used)) / referenced;
2120 if (percent_shared <= zfs_livelist_min_percent_shared)
2121 return (B_TRUE);
2122 return (B_FALSE);
2123 }
2124
2125 /*
2126 * Check if it is possible to combine two livelist entries into one.
2127 * This is the case if the combined number of 'live' blkptrs (ALLOCs that
2128 * don't have a matching FREE) is under the maximum sublist size.
2129 * We check this by subtracting twice the total number of frees from the total
2130 * number of blkptrs. FREEs are counted twice because each FREE blkptr
2131 * will cancel out an ALLOC blkptr when the livelist is processed.
2132 */
2133 static boolean_t
2134 dsl_livelist_should_condense(dsl_deadlist_entry_t *first,
2135 dsl_deadlist_entry_t *next)
2136 {
2137 uint64_t total_free = first->dle_bpobj.bpo_phys->bpo_num_freed +
2138 next->dle_bpobj.bpo_phys->bpo_num_freed;
2139 uint64_t total_entries = first->dle_bpobj.bpo_phys->bpo_num_blkptrs +
2140 next->dle_bpobj.bpo_phys->bpo_num_blkptrs;
2141 if ((total_entries - (2 * total_free)) < zfs_livelist_max_entries)
2142 return (B_TRUE);
2143 return (B_FALSE);
2144 }
2145
2146 typedef struct try_condense_arg {
2147 spa_t *spa;
2148 dsl_dataset_t *ds;
2149 } try_condense_arg_t;
2150
2151 /*
2152 * Iterate over the livelist entries, searching for a pair to condense.
2153 * A nonzero return value means stop, 0 means keep looking.
2154 */
2155 static int
2156 dsl_livelist_try_condense(void *arg, dsl_deadlist_entry_t *first)
2157 {
2158 try_condense_arg_t *tca = arg;
2159 spa_t *spa = tca->spa;
2160 dsl_dataset_t *ds = tca->ds;
2161 dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist;
2162 dsl_deadlist_entry_t *next;
2163
2164 /* The condense thread has not yet been created at import */
2165 if (spa->spa_livelist_condense_zthr == NULL)
2166 return (1);
2167
2168 /* A condense is already in progress */
2169 if (spa->spa_to_condense.ds != NULL)
2170 return (1);
2171
2172 next = AVL_NEXT(&ll->dl_tree, &first->dle_node);
2173 /* The livelist has only one entry - don't condense it */
2174 if (next == NULL)
2175 return (1);
2176
2177 /* Next is the newest entry - don't condense it */
2178 if (AVL_NEXT(&ll->dl_tree, &next->dle_node) == NULL)
2179 return (1);
2180
2181 /* This pair is not ready to condense but keep looking */
2182 if (!dsl_livelist_should_condense(first, next))
2183 return (0);
2184
2185 /*
2186 * Add a ref to prevent the dataset from being evicted while
2187 * the condense zthr or synctask are running. Ref will be
2188 * released at the end of the condense synctask
2189 */
2190 dmu_buf_add_ref(ds->ds_dbuf, spa);
2191
2192 spa->spa_to_condense.ds = ds;
2193 spa->spa_to_condense.first = first;
2194 spa->spa_to_condense.next = next;
2195 spa->spa_to_condense.syncing = B_FALSE;
2196 spa->spa_to_condense.cancelled = B_FALSE;
2197
2198 zthr_wakeup(spa->spa_livelist_condense_zthr);
2199 return (1);
2200 }
2201
2202 static void
2203 dsl_flush_pending_livelist(dsl_dataset_t *ds, dmu_tx_t *tx)
2204 {
2205 dsl_dir_t *dd = ds->ds_dir;
2206 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
2207 dsl_deadlist_entry_t *last = dsl_deadlist_last(&dd->dd_livelist);
2208
2209 /* Check if we need to add a new sub-livelist */
2210 if (last == NULL) {
2211 /* The livelist is empty */
2212 dsl_deadlist_add_key(&dd->dd_livelist,
2213 tx->tx_txg - 1, tx);
2214 } else if (spa_sync_pass(spa) == 1) {
2215 /*
2216 * Check if the newest entry is full. If it is, make a new one.
2217 * We only do this once per sync because we could overfill a
2218 * sublist in one sync pass and don't want to add another entry
2219 * for a txg that is already represented. This ensures that
2220 * blkptrs born in the same txg are stored in the same sublist.
2221 */
2222 bpobj_t bpobj = last->dle_bpobj;
2223 uint64_t all = bpobj.bpo_phys->bpo_num_blkptrs;
2224 uint64_t free = bpobj.bpo_phys->bpo_num_freed;
2225 uint64_t alloc = all - free;
2226 if (alloc > zfs_livelist_max_entries) {
2227 dsl_deadlist_add_key(&dd->dd_livelist,
2228 tx->tx_txg - 1, tx);
2229 }
2230 }
2231
2232 /* Insert each entry into the on-disk livelist */
2233 bplist_iterate(&dd->dd_pending_allocs,
2234 dsl_deadlist_insert_alloc_cb, &dd->dd_livelist, tx);
2235 bplist_iterate(&dd->dd_pending_frees,
2236 dsl_deadlist_insert_free_cb, &dd->dd_livelist, tx);
2237
2238 /* Attempt to condense every pair of adjacent entries */
2239 try_condense_arg_t arg = {
2240 .spa = spa,
2241 .ds = ds
2242 };
2243 dsl_deadlist_iterate(&dd->dd_livelist, dsl_livelist_try_condense,
2244 &arg);
2245 }
2246
2247 void
2248 dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
2249 {
2250 objset_t *os = ds->ds_objset;
2251
2252 bplist_iterate(&ds->ds_pending_deadlist,
2253 dsl_deadlist_insert_alloc_cb, &ds->ds_deadlist, tx);
2254
2255 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist)) {
2256 dsl_flush_pending_livelist(ds, tx);
2257 if (dsl_livelist_should_disable(ds)) {
2258 dsl_dir_remove_livelist(ds->ds_dir, tx, B_TRUE);
2259 }
2260 }
2261
2262 dsl_bookmark_sync_done(ds, tx);
2263
2264 multilist_destroy(&os->os_synced_dnodes);
2265
2266 if (os->os_encrypted)
2267 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE;
2268 else
2269 ASSERT0(os->os_next_write_raw[tx->tx_txg & TXG_MASK]);
2270
2271 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2272 if (zfeature_active(f,
2273 ds->ds_feature_activation[f])) {
2274 if (zfeature_active(f, ds->ds_feature[f]))
2275 continue;
2276 dsl_dataset_activate_feature(ds->ds_object, f,
2277 ds->ds_feature_activation[f], tx);
2278 ds->ds_feature[f] = ds->ds_feature_activation[f];
2279 }
2280 }
2281
2282 ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
2283 }
2284
2285 int
2286 get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val)
2287 {
2288 uint64_t count = 0;
2289 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2290 zap_cursor_t zc;
2291 zap_attribute_t za;
2292
2293 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
2294
2295 /*
2296 * There may be missing entries in ds_next_clones_obj
2297 * due to a bug in a previous version of the code.
2298 * Only trust it if it has the right number of entries.
2299 */
2300 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
2301 VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
2302 &count));
2303 }
2304 if (count != dsl_dataset_phys(ds)->ds_num_children - 1) {
2305 return (SET_ERROR(ENOENT));
2306 }
2307 for (zap_cursor_init(&zc, mos,
2308 dsl_dataset_phys(ds)->ds_next_clones_obj);
2309 zap_cursor_retrieve(&zc, &za) == 0;
2310 zap_cursor_advance(&zc)) {
2311 dsl_dataset_t *clone;
2312 char buf[ZFS_MAX_DATASET_NAME_LEN];
2313 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
2314 za.za_first_integer, FTAG, &clone));
2315 dsl_dir_name(clone->ds_dir, buf);
2316 fnvlist_add_boolean(val, buf);
2317 dsl_dataset_rele(clone, FTAG);
2318 }
2319 zap_cursor_fini(&zc);
2320 return (0);
2321 }
2322
2323 void
2324 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
2325 {
2326 nvlist_t *propval = fnvlist_alloc();
2327 nvlist_t *val = fnvlist_alloc();
2328
2329 if (get_clones_stat_impl(ds, val) == 0) {
2330 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
2331 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
2332 propval);
2333 }
2334
2335 nvlist_free(val);
2336 nvlist_free(propval);
2337 }
2338
2339 static char *
2340 get_receive_resume_token_impl(dsl_dataset_t *ds)
2341 {
2342 if (!dsl_dataset_has_resume_receive_state(ds))
2343 return (NULL);
2344
2345 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2346 char *str;
2347 void *packed;
2348 uint8_t *compressed;
2349 uint64_t val;
2350 nvlist_t *token_nv = fnvlist_alloc();
2351 size_t packed_size, compressed_size;
2352
2353 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2354 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
2355 fnvlist_add_uint64(token_nv, "fromguid", val);
2356 }
2357 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2358 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
2359 fnvlist_add_uint64(token_nv, "object", val);
2360 }
2361 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2362 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
2363 fnvlist_add_uint64(token_nv, "offset", val);
2364 }
2365 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2366 DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
2367 fnvlist_add_uint64(token_nv, "bytes", val);
2368 }
2369 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2370 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
2371 fnvlist_add_uint64(token_nv, "toguid", val);
2372 }
2373 char buf[MAXNAMELEN];
2374 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2375 DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
2376 fnvlist_add_string(token_nv, "toname", buf);
2377 }
2378 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2379 DS_FIELD_RESUME_LARGEBLOCK) == 0) {
2380 fnvlist_add_boolean(token_nv, "largeblockok");
2381 }
2382 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2383 DS_FIELD_RESUME_EMBEDOK) == 0) {
2384 fnvlist_add_boolean(token_nv, "embedok");
2385 }
2386 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2387 DS_FIELD_RESUME_COMPRESSOK) == 0) {
2388 fnvlist_add_boolean(token_nv, "compressok");
2389 }
2390 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2391 DS_FIELD_RESUME_RAWOK) == 0) {
2392 fnvlist_add_boolean(token_nv, "rawok");
2393 }
2394 if (dsl_dataset_feature_is_active(ds,
2395 SPA_FEATURE_REDACTED_DATASETS)) {
2396 uint64_t num_redact_snaps = 0;
2397 uint64_t *redact_snaps = NULL;
2398 VERIFY3B(dsl_dataset_get_uint64_array_feature(ds,
2399 SPA_FEATURE_REDACTED_DATASETS, &num_redact_snaps,
2400 &redact_snaps), ==, B_TRUE);
2401 fnvlist_add_uint64_array(token_nv, "redact_snaps",
2402 redact_snaps, num_redact_snaps);
2403 }
2404 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2405 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS) == 0) {
2406 uint64_t num_redact_snaps = 0, int_size = 0;
2407 uint64_t *redact_snaps = NULL;
2408 VERIFY0(zap_length(dp->dp_meta_objset, ds->ds_object,
2409 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, &int_size,
2410 &num_redact_snaps));
2411 ASSERT3U(int_size, ==, sizeof (uint64_t));
2412
2413 redact_snaps = kmem_alloc(int_size * num_redact_snaps,
2414 KM_SLEEP);
2415 VERIFY0(zap_lookup(dp->dp_meta_objset, ds->ds_object,
2416 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, int_size,
2417 num_redact_snaps, redact_snaps));
2418 fnvlist_add_uint64_array(token_nv, "book_redact_snaps",
2419 redact_snaps, num_redact_snaps);
2420 kmem_free(redact_snaps, int_size * num_redact_snaps);
2421 }
2422 packed = fnvlist_pack(token_nv, &packed_size);
2423 fnvlist_free(token_nv);
2424 compressed = kmem_alloc(packed_size, KM_SLEEP);
2425
2426 compressed_size = gzip_compress(packed, compressed,
2427 packed_size, packed_size, 6);
2428
2429 zio_cksum_t cksum;
2430 fletcher_4_native_varsize(compressed, compressed_size, &cksum);
2431
2432 size_t alloc_size = compressed_size * 2 + 1;
2433 str = kmem_alloc(alloc_size, KM_SLEEP);
2434 for (int i = 0; i < compressed_size; i++) {
2435 size_t offset = i * 2;
2436 (void) snprintf(str + offset, alloc_size - offset,
2437 "%02x", compressed[i]);
2438 }
2439 str[compressed_size * 2] = '\0';
2440 char *propval = kmem_asprintf("%u-%llx-%llx-%s",
2441 ZFS_SEND_RESUME_TOKEN_VERSION,
2442 (longlong_t)cksum.zc_word[0],
2443 (longlong_t)packed_size, str);
2444 kmem_free(packed, packed_size);
2445 kmem_free(str, alloc_size);
2446 kmem_free(compressed, packed_size);
2447 return (propval);
2448 }
2449
2450 /*
2451 * Returns a string that represents the receive resume state token. It should
2452 * be freed with strfree(). NULL is returned if no resume state is present.
2453 */
2454 char *
2455 get_receive_resume_token(dsl_dataset_t *ds)
2456 {
2457 /*
2458 * A failed "newfs" (e.g. full) resumable receive leaves
2459 * the stats set on this dataset. Check here for the prop.
2460 */
2461 char *token = get_receive_resume_token_impl(ds);
2462 if (token != NULL)
2463 return (token);
2464 /*
2465 * A failed incremental resumable receive leaves the
2466 * stats set on our child named "%recv". Check the child
2467 * for the prop.
2468 */
2469 /* 6 extra bytes for /%recv */
2470 char name[ZFS_MAX_DATASET_NAME_LEN + 6];
2471 dsl_dataset_t *recv_ds;
2472 dsl_dataset_name(ds, name);
2473 if (strlcat(name, "/", sizeof (name)) < sizeof (name) &&
2474 strlcat(name, recv_clone_name, sizeof (name)) < sizeof (name) &&
2475 dsl_dataset_hold(ds->ds_dir->dd_pool, name, FTAG, &recv_ds) == 0) {
2476 token = get_receive_resume_token_impl(recv_ds);
2477 dsl_dataset_rele(recv_ds, FTAG);
2478 }
2479 return (token);
2480 }
2481
2482 uint64_t
2483 dsl_get_refratio(dsl_dataset_t *ds)
2484 {
2485 uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
2486 (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
2487 dsl_dataset_phys(ds)->ds_compressed_bytes);
2488 return (ratio);
2489 }
2490
2491 uint64_t
2492 dsl_get_logicalreferenced(dsl_dataset_t *ds)
2493 {
2494 return (dsl_dataset_phys(ds)->ds_uncompressed_bytes);
2495 }
2496
2497 uint64_t
2498 dsl_get_compressratio(dsl_dataset_t *ds)
2499 {
2500 if (ds->ds_is_snapshot) {
2501 return (dsl_get_refratio(ds));
2502 } else {
2503 dsl_dir_t *dd = ds->ds_dir;
2504 mutex_enter(&dd->dd_lock);
2505 uint64_t val = dsl_dir_get_compressratio(dd);
2506 mutex_exit(&dd->dd_lock);
2507 return (val);
2508 }
2509 }
2510
2511 uint64_t
2512 dsl_get_used(dsl_dataset_t *ds)
2513 {
2514 if (ds->ds_is_snapshot) {
2515 return (dsl_dataset_phys(ds)->ds_unique_bytes);
2516 } else {
2517 dsl_dir_t *dd = ds->ds_dir;
2518 mutex_enter(&dd->dd_lock);
2519 uint64_t val = dsl_dir_get_used(dd);
2520 mutex_exit(&dd->dd_lock);
2521 return (val);
2522 }
2523 }
2524
2525 uint64_t
2526 dsl_get_creation(dsl_dataset_t *ds)
2527 {
2528 return (dsl_dataset_phys(ds)->ds_creation_time);
2529 }
2530
2531 uint64_t
2532 dsl_get_creationtxg(dsl_dataset_t *ds)
2533 {
2534 return (dsl_dataset_phys(ds)->ds_creation_txg);
2535 }
2536
2537 uint64_t
2538 dsl_get_refquota(dsl_dataset_t *ds)
2539 {
2540 return (ds->ds_quota);
2541 }
2542
2543 uint64_t
2544 dsl_get_refreservation(dsl_dataset_t *ds)
2545 {
2546 return (ds->ds_reserved);
2547 }
2548
2549 uint64_t
2550 dsl_get_guid(dsl_dataset_t *ds)
2551 {
2552 return (dsl_dataset_phys(ds)->ds_guid);
2553 }
2554
2555 uint64_t
2556 dsl_get_unique(dsl_dataset_t *ds)
2557 {
2558 return (dsl_dataset_phys(ds)->ds_unique_bytes);
2559 }
2560
2561 uint64_t
2562 dsl_get_objsetid(dsl_dataset_t *ds)
2563 {
2564 return (ds->ds_object);
2565 }
2566
2567 uint64_t
2568 dsl_get_userrefs(dsl_dataset_t *ds)
2569 {
2570 return (ds->ds_userrefs);
2571 }
2572
2573 uint64_t
2574 dsl_get_defer_destroy(dsl_dataset_t *ds)
2575 {
2576 return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2577 }
2578
2579 uint64_t
2580 dsl_get_referenced(dsl_dataset_t *ds)
2581 {
2582 return (dsl_dataset_phys(ds)->ds_referenced_bytes);
2583 }
2584
2585 uint64_t
2586 dsl_get_numclones(dsl_dataset_t *ds)
2587 {
2588 ASSERT(ds->ds_is_snapshot);
2589 return (dsl_dataset_phys(ds)->ds_num_children - 1);
2590 }
2591
2592 uint64_t
2593 dsl_get_inconsistent(dsl_dataset_t *ds)
2594 {
2595 return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ?
2596 1 : 0);
2597 }
2598
2599 uint64_t
2600 dsl_get_redacted(dsl_dataset_t *ds)
2601 {
2602 return (dsl_dataset_feature_is_active(ds,
2603 SPA_FEATURE_REDACTED_DATASETS));
2604 }
2605
2606 uint64_t
2607 dsl_get_available(dsl_dataset_t *ds)
2608 {
2609 uint64_t refdbytes = dsl_get_referenced(ds);
2610 uint64_t availbytes = dsl_dir_space_available(ds->ds_dir,
2611 NULL, 0, TRUE);
2612 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
2613 availbytes +=
2614 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2615 }
2616 if (ds->ds_quota != 0) {
2617 /*
2618 * Adjust available bytes according to refquota
2619 */
2620 if (refdbytes < ds->ds_quota) {
2621 availbytes = MIN(availbytes,
2622 ds->ds_quota - refdbytes);
2623 } else {
2624 availbytes = 0;
2625 }
2626 }
2627 return (availbytes);
2628 }
2629
2630 int
2631 dsl_get_written(dsl_dataset_t *ds, uint64_t *written)
2632 {
2633 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2634 dsl_dataset_t *prev;
2635 int err = dsl_dataset_hold_obj(dp,
2636 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
2637 if (err == 0) {
2638 uint64_t comp, uncomp;
2639 err = dsl_dataset_space_written(prev, ds, written,
2640 &comp, &uncomp);
2641 dsl_dataset_rele(prev, FTAG);
2642 }
2643 return (err);
2644 }
2645
2646 /*
2647 * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN.
2648 */
2649 int
2650 dsl_get_prev_snap(dsl_dataset_t *ds, char *snap)
2651 {
2652 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2653 if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
2654 dsl_dataset_name(ds->ds_prev, snap);
2655 return (0);
2656 } else {
2657 return (SET_ERROR(ENOENT));
2658 }
2659 }
2660
2661 void
2662 dsl_get_redact_snaps(dsl_dataset_t *ds, nvlist_t *propval)
2663 {
2664 uint64_t nsnaps;
2665 uint64_t *snaps;
2666 if (dsl_dataset_get_uint64_array_feature(ds,
2667 SPA_FEATURE_REDACTED_DATASETS, &nsnaps, &snaps)) {
2668 fnvlist_add_uint64_array(propval, ZPROP_VALUE, snaps,
2669 nsnaps);
2670 }
2671 }
2672
2673 /*
2674 * Returns the mountpoint property and source for the given dataset in the value
2675 * and source buffers. The value buffer must be at least as large as MAXPATHLEN
2676 * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN.
2677 * Returns 0 on success and an error on failure.
2678 */
2679 int
2680 dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value,
2681 char *source)
2682 {
2683 int error;
2684 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2685
2686 /* Retrieve the mountpoint value stored in the zap object */
2687 error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1,
2688 ZAP_MAXVALUELEN, value, source);
2689 if (error != 0) {
2690 return (error);
2691 }
2692
2693 /*
2694 * Process the dsname and source to find the full mountpoint string.
2695 * Can be skipped for 'legacy' or 'none'.
2696 */
2697 if (value[0] == '/') {
2698 char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
2699 char *root = buf;
2700 const char *relpath;
2701
2702 /*
2703 * If we inherit the mountpoint, even from a dataset
2704 * with a received value, the source will be the path of
2705 * the dataset we inherit from. If source is
2706 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2707 * inherited.
2708 */
2709 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2710 relpath = "";
2711 } else {
2712 ASSERT0(strncmp(dsname, source, strlen(source)));
2713 relpath = dsname + strlen(source);
2714 if (relpath[0] == '/')
2715 relpath++;
2716 }
2717
2718 spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN);
2719
2720 /*
2721 * Special case an alternate root of '/'. This will
2722 * avoid having multiple leading slashes in the
2723 * mountpoint path.
2724 */
2725 if (strcmp(root, "/") == 0)
2726 root++;
2727
2728 /*
2729 * If the mountpoint is '/' then skip over this
2730 * if we are obtaining either an alternate root or
2731 * an inherited mountpoint.
2732 */
2733 char *mnt = value;
2734 if (value[1] == '\0' && (root[0] != '\0' ||
2735 relpath[0] != '\0'))
2736 mnt = value + 1;
2737
2738 mnt = kmem_strdup(mnt);
2739
2740 if (relpath[0] == '\0') {
2741 (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s",
2742 root, mnt);
2743 } else {
2744 (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s",
2745 root, mnt, relpath[0] == '@' ? "" : "/",
2746 relpath);
2747 }
2748 kmem_free(buf, ZAP_MAXVALUELEN);
2749 kmem_strfree(mnt);
2750 }
2751
2752 return (0);
2753 }
2754
2755 void
2756 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2757 {
2758 dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2759
2760 ASSERT(dsl_pool_config_held(dp));
2761
2762 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO,
2763 dsl_get_refratio(ds));
2764 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
2765 dsl_get_logicalreferenced(ds));
2766 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
2767 dsl_get_compressratio(ds));
2768 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2769 dsl_get_used(ds));
2770
2771 if (ds->ds_is_snapshot) {
2772 get_clones_stat(ds, nv);
2773 } else {
2774 char buf[ZFS_MAX_DATASET_NAME_LEN];
2775 if (dsl_get_prev_snap(ds, buf) == 0)
2776 dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP,
2777 buf);
2778 dsl_dir_stats(ds->ds_dir, nv);
2779 }
2780
2781 nvlist_t *propval = fnvlist_alloc();
2782 dsl_get_redact_snaps(ds, propval);
2783 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2784 propval);
2785 nvlist_free(propval);
2786
2787 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
2788 dsl_get_available(ds));
2789 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
2790 dsl_get_referenced(ds));
2791 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2792 dsl_get_creation(ds));
2793 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2794 dsl_get_creationtxg(ds));
2795 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2796 dsl_get_refquota(ds));
2797 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2798 dsl_get_refreservation(ds));
2799 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2800 dsl_get_guid(ds));
2801 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2802 dsl_get_unique(ds));
2803 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2804 dsl_get_objsetid(ds));
2805 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2806 dsl_get_userrefs(ds));
2807 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2808 dsl_get_defer_destroy(ds));
2809 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOTS_CHANGED,
2810 dsl_dir_snap_cmtime(ds->ds_dir).tv_sec);
2811 dsl_dataset_crypt_stats(ds, nv);
2812
2813 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2814 uint64_t written;
2815 if (dsl_get_written(ds, &written) == 0) {
2816 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2817 written);
2818 }
2819 }
2820
2821 if (!dsl_dataset_is_snapshot(ds)) {
2822 char *token = get_receive_resume_token(ds);
2823 if (token != NULL) {
2824 dsl_prop_nvlist_add_string(nv,
2825 ZFS_PROP_RECEIVE_RESUME_TOKEN, token);
2826 kmem_strfree(token);
2827 }
2828 }
2829 }
2830
2831 void
2832 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2833 {
2834 dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2835 ASSERT(dsl_pool_config_held(dp));
2836
2837 stat->dds_creation_txg = dsl_get_creationtxg(ds);
2838 stat->dds_inconsistent = dsl_get_inconsistent(ds);
2839 stat->dds_guid = dsl_get_guid(ds);
2840 stat->dds_redacted = dsl_get_redacted(ds);
2841 stat->dds_origin[0] = '\0';
2842 if (ds->ds_is_snapshot) {
2843 stat->dds_is_snapshot = B_TRUE;
2844 stat->dds_num_clones = dsl_get_numclones(ds);
2845 } else {
2846 stat->dds_is_snapshot = B_FALSE;
2847 stat->dds_num_clones = 0;
2848
2849 if (dsl_dir_is_clone(ds->ds_dir)) {
2850 dsl_dir_get_origin(ds->ds_dir, stat->dds_origin);
2851 }
2852 }
2853 }
2854
2855 uint64_t
2856 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2857 {
2858 return (ds->ds_fsid_guid);
2859 }
2860
2861 void
2862 dsl_dataset_space(dsl_dataset_t *ds,
2863 uint64_t *refdbytesp, uint64_t *availbytesp,
2864 uint64_t *usedobjsp, uint64_t *availobjsp)
2865 {
2866 *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
2867 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2868 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
2869 *availbytesp +=
2870 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2871 if (ds->ds_quota != 0) {
2872 /*
2873 * Adjust available bytes according to refquota
2874 */
2875 if (*refdbytesp < ds->ds_quota)
2876 *availbytesp = MIN(*availbytesp,
2877 ds->ds_quota - *refdbytesp);
2878 else
2879 *availbytesp = 0;
2880 }
2881 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2882 *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
2883 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2884 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
2885 }
2886
2887 boolean_t
2888 dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
2889 {
2890 dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2891 uint64_t birth;
2892
2893 ASSERT(dsl_pool_config_held(dp));
2894 if (snap == NULL)
2895 return (B_FALSE);
2896 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2897 birth = dsl_dataset_get_blkptr(ds)->blk_birth;
2898 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2899 if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
2900 objset_t *os, *os_snap;
2901 /*
2902 * It may be that only the ZIL differs, because it was
2903 * reset in the head. Don't count that as being
2904 * modified.
2905 */
2906 if (dmu_objset_from_ds(ds, &os) != 0)
2907 return (B_TRUE);
2908 if (dmu_objset_from_ds(snap, &os_snap) != 0)
2909 return (B_TRUE);
2910 return (memcmp(&os->os_phys->os_meta_dnode,
2911 &os_snap->os_phys->os_meta_dnode,
2912 sizeof (os->os_phys->os_meta_dnode)) != 0);
2913 }
2914 return (B_FALSE);
2915 }
2916
2917 static int
2918 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
2919 dsl_dataset_t *hds, void *arg)
2920 {
2921 (void) dp;
2922 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2923 int error;
2924 uint64_t val;
2925
2926 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2927 if (error != 0) {
2928 /* ignore nonexistent snapshots */
2929 return (error == ENOENT ? 0 : error);
2930 }
2931
2932 /* new name should not exist */
2933 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
2934 if (error == 0)
2935 error = SET_ERROR(EEXIST);
2936 else if (error == ENOENT)
2937 error = 0;
2938
2939 /* dataset name + 1 for the "@" + the new snapshot name must fit */
2940 if (dsl_dir_namelen(hds->ds_dir) + 1 +
2941 strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2942 error = SET_ERROR(ENAMETOOLONG);
2943
2944 return (error);
2945 }
2946
2947 int
2948 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
2949 {
2950 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2951 dsl_pool_t *dp = dmu_tx_pool(tx);
2952 dsl_dataset_t *hds;
2953 int error;
2954
2955 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
2956 if (error != 0)
2957 return (error);
2958
2959 if (ddrsa->ddrsa_recursive) {
2960 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2961 dsl_dataset_rename_snapshot_check_impl, ddrsa,
2962 DS_FIND_CHILDREN);
2963 } else {
2964 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
2965 }
2966 dsl_dataset_rele(hds, FTAG);
2967 return (error);
2968 }
2969
2970 static int
2971 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
2972 dsl_dataset_t *hds, void *arg)
2973 {
2974 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2975 dsl_dataset_t *ds;
2976 uint64_t val;
2977 dmu_tx_t *tx = ddrsa->ddrsa_tx;
2978 int error;
2979
2980 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2981 ASSERT(error == 0 || error == ENOENT);
2982 if (error == ENOENT) {
2983 /* ignore nonexistent snapshots */
2984 return (0);
2985 }
2986
2987 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
2988
2989 /* log before we change the name */
2990 spa_history_log_internal_ds(ds, "rename", tx,
2991 "-> @%s", ddrsa->ddrsa_newsnapname);
2992
2993 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2994 B_FALSE));
2995 mutex_enter(&ds->ds_lock);
2996 (void) strlcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname,
2997 sizeof (ds->ds_snapname));
2998 mutex_exit(&ds->ds_lock);
2999 VERIFY0(zap_add(dp->dp_meta_objset,
3000 dsl_dataset_phys(hds)->ds_snapnames_zapobj,
3001 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
3002 zvol_rename_minors(dp->dp_spa, ddrsa->ddrsa_oldsnapname,
3003 ddrsa->ddrsa_newsnapname, B_TRUE);
3004
3005 dsl_dataset_rele(ds, FTAG);
3006 return (0);
3007 }
3008
3009 void
3010 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
3011 {
3012 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3013 dsl_pool_t *dp = dmu_tx_pool(tx);
3014 dsl_dataset_t *hds = NULL;
3015
3016 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
3017 ddrsa->ddrsa_tx = tx;
3018 if (ddrsa->ddrsa_recursive) {
3019 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
3020 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
3021 DS_FIND_CHILDREN));
3022 } else {
3023 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
3024 }
3025 dsl_dataset_rele(hds, FTAG);
3026 }
3027
3028 int
3029 dsl_dataset_rename_snapshot(const char *fsname,
3030 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
3031 {
3032 dsl_dataset_rename_snapshot_arg_t ddrsa;
3033
3034 ddrsa.ddrsa_fsname = fsname;
3035 ddrsa.ddrsa_oldsnapname = oldsnapname;
3036 ddrsa.ddrsa_newsnapname = newsnapname;
3037 ddrsa.ddrsa_recursive = recursive;
3038
3039 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
3040 dsl_dataset_rename_snapshot_sync, &ddrsa,
3041 1, ZFS_SPACE_CHECK_RESERVED));
3042 }
3043
3044 /*
3045 * If we're doing an ownership handoff, we need to make sure that there is
3046 * only one long hold on the dataset. We're not allowed to change anything here
3047 * so we don't permanently release the long hold or regular hold here. We want
3048 * to do this only when syncing to avoid the dataset unexpectedly going away
3049 * when we release the long hold.
3050 */
3051 static int
3052 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
3053 {
3054 boolean_t held = B_FALSE;
3055
3056 if (!dmu_tx_is_syncing(tx))
3057 return (0);
3058
3059 dsl_dir_t *dd = ds->ds_dir;
3060 mutex_enter(&dd->dd_activity_lock);
3061 uint64_t holds = zfs_refcount_count(&ds->ds_longholds) -
3062 (owner != NULL ? 1 : 0);
3063 /*
3064 * The value of dd_activity_waiters can chance as soon as we drop the
3065 * lock, but we're fine with that; new waiters coming in or old
3066 * waiters leaving doesn't cause problems, since we're going to cancel
3067 * waiters later anyway. The goal of this check is to verify that no
3068 * non-waiters have long-holds, and all new long-holds will be
3069 * prevented because we're holding the pool config as writer.
3070 */
3071 if (holds != dd->dd_activity_waiters)
3072 held = B_TRUE;
3073 mutex_exit(&dd->dd_activity_lock);
3074
3075 if (held)
3076 return (SET_ERROR(EBUSY));
3077
3078 return (0);
3079 }
3080
3081 int
3082 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
3083 {
3084 dsl_dataset_rollback_arg_t *ddra = arg;
3085 dsl_pool_t *dp = dmu_tx_pool(tx);
3086 dsl_dataset_t *ds;
3087 int64_t unused_refres_delta;
3088 int error;
3089
3090 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
3091 if (error != 0)
3092 return (error);
3093
3094 /* must not be a snapshot */
3095 if (ds->ds_is_snapshot) {
3096 dsl_dataset_rele(ds, FTAG);
3097 return (SET_ERROR(EINVAL));
3098 }
3099
3100 /* must have a most recent snapshot */
3101 if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
3102 dsl_dataset_rele(ds, FTAG);
3103 return (SET_ERROR(ESRCH));
3104 }
3105
3106 /*
3107 * No rollback to a snapshot created in the current txg, because
3108 * the rollback may dirty the dataset and create blocks that are
3109 * not reachable from the rootbp while having a birth txg that
3110 * falls into the snapshot's range.
3111 */
3112 if (dmu_tx_is_syncing(tx) &&
3113 dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
3114 dsl_dataset_rele(ds, FTAG);
3115 return (SET_ERROR(EAGAIN));
3116 }
3117
3118 /*
3119 * If the expected target snapshot is specified, then check that
3120 * the latest snapshot is it.
3121 */
3122 if (ddra->ddra_tosnap != NULL) {
3123 dsl_dataset_t *snapds;
3124
3125 /* Check if the target snapshot exists at all. */
3126 error = dsl_dataset_hold(dp, ddra->ddra_tosnap, FTAG, &snapds);
3127 if (error != 0) {
3128 /*
3129 * ESRCH is used to signal that the target snapshot does
3130 * not exist, while ENOENT is used to report that
3131 * the rolled back dataset does not exist.
3132 * ESRCH is also used to cover other cases where the
3133 * target snapshot is not related to the dataset being
3134 * rolled back such as being in a different pool.
3135 */
3136 if (error == ENOENT || error == EXDEV)
3137 error = SET_ERROR(ESRCH);
3138 dsl_dataset_rele(ds, FTAG);
3139 return (error);
3140 }
3141 ASSERT(snapds->ds_is_snapshot);
3142
3143 /* Check if the snapshot is the latest snapshot indeed. */
3144 if (snapds != ds->ds_prev) {
3145 /*
3146 * Distinguish between the case where the only problem
3147 * is intervening snapshots (EEXIST) vs the snapshot
3148 * not being a valid target for rollback (ESRCH).
3149 */
3150 if (snapds->ds_dir == ds->ds_dir ||
3151 (dsl_dir_is_clone(ds->ds_dir) &&
3152 dsl_dir_phys(ds->ds_dir)->dd_origin_obj ==
3153 snapds->ds_object)) {
3154 error = SET_ERROR(EEXIST);
3155 } else {
3156 error = SET_ERROR(ESRCH);
3157 }
3158 dsl_dataset_rele(snapds, FTAG);
3159 dsl_dataset_rele(ds, FTAG);
3160 return (error);
3161 }
3162 dsl_dataset_rele(snapds, FTAG);
3163 }
3164
3165 /* must not have any bookmarks after the most recent snapshot */
3166 if (dsl_bookmark_latest_txg(ds) >
3167 dsl_dataset_phys(ds)->ds_prev_snap_txg) {
3168 dsl_dataset_rele(ds, FTAG);
3169 return (SET_ERROR(EEXIST));
3170 }
3171
3172 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
3173 if (error != 0) {
3174 dsl_dataset_rele(ds, FTAG);
3175 return (error);
3176 }
3177
3178 /*
3179 * Check if the snap we are rolling back to uses more than
3180 * the refquota.
3181 */
3182 if (ds->ds_quota != 0 &&
3183 dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
3184 dsl_dataset_rele(ds, FTAG);
3185 return (SET_ERROR(EDQUOT));
3186 }
3187
3188 /*
3189 * When we do the clone swap, we will temporarily use more space
3190 * due to the refreservation (the head will no longer have any
3191 * unique space, so the entire amount of the refreservation will need
3192 * to be free). We will immediately destroy the clone, freeing
3193 * this space, but the freeing happens over many txg's.
3194 */
3195 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
3196 dsl_dataset_phys(ds)->ds_unique_bytes);
3197
3198 if (unused_refres_delta > 0 &&
3199 unused_refres_delta >
3200 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
3201 dsl_dataset_rele(ds, FTAG);
3202 return (SET_ERROR(ENOSPC));
3203 }
3204
3205 dsl_dataset_rele(ds, FTAG);
3206 return (0);
3207 }
3208
3209 void
3210 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
3211 {
3212 dsl_dataset_rollback_arg_t *ddra = arg;
3213 dsl_pool_t *dp = dmu_tx_pool(tx);
3214 dsl_dataset_t *ds, *clone;
3215 uint64_t cloneobj;
3216 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3217
3218 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
3219
3220 dsl_dataset_name(ds->ds_prev, namebuf);
3221 fnvlist_add_string(ddra->ddra_result, "target", namebuf);
3222
3223 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
3224 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, NULL, tx);
3225
3226 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
3227
3228 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
3229 dsl_dataset_zero_zil(ds, tx);
3230
3231 dsl_destroy_head_sync_impl(clone, tx);
3232
3233 dsl_dataset_rele(clone, FTAG);
3234 dsl_dataset_rele(ds, FTAG);
3235 }
3236
3237 /*
3238 * Rolls back the given filesystem or volume to the most recent snapshot.
3239 * The name of the most recent snapshot will be returned under key "target"
3240 * in the result nvlist.
3241 *
3242 * If owner != NULL:
3243 * - The existing dataset MUST be owned by the specified owner at entry
3244 * - Upon return, dataset will still be held by the same owner, whether we
3245 * succeed or not.
3246 *
3247 * This mode is required any time the existing filesystem is mounted. See
3248 * notes above zfs_suspend_fs() for further details.
3249 */
3250 int
3251 dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner,
3252 nvlist_t *result)
3253 {
3254 dsl_dataset_rollback_arg_t ddra;
3255
3256 ddra.ddra_fsname = fsname;
3257 ddra.ddra_tosnap = tosnap;
3258 ddra.ddra_owner = owner;
3259 ddra.ddra_result = result;
3260
3261 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
3262 dsl_dataset_rollback_sync, &ddra,
3263 1, ZFS_SPACE_CHECK_RESERVED));
3264 }
3265
3266 struct promotenode {
3267 list_node_t link;
3268 dsl_dataset_t *ds;
3269 };
3270
3271 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
3272 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
3273 const void *tag);
3274 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag);
3275
3276 int
3277 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
3278 {
3279 dsl_dataset_promote_arg_t *ddpa = arg;
3280 dsl_pool_t *dp = dmu_tx_pool(tx);
3281 dsl_dataset_t *hds;
3282 struct promotenode *snap;
3283 int err;
3284 uint64_t unused;
3285 uint64_t ss_mv_cnt;
3286 size_t max_snap_len;
3287 boolean_t conflicting_snaps;
3288
3289 err = promote_hold(ddpa, dp, FTAG);
3290 if (err != 0)
3291 return (err);
3292
3293 hds = ddpa->ddpa_clone;
3294 max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
3295
3296 if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
3297 promote_rele(ddpa, FTAG);
3298 return (SET_ERROR(EXDEV));
3299 }
3300
3301 snap = list_head(&ddpa->shared_snaps);
3302 if (snap == NULL) {
3303 err = SET_ERROR(ENOENT);
3304 goto out;
3305 }
3306 dsl_dataset_t *const origin_ds = snap->ds;
3307
3308 /*
3309 * Encrypted clones share a DSL Crypto Key with their origin's dsl dir.
3310 * When doing a promote we must make sure the encryption root for
3311 * both the target and the target's origin does not change to avoid
3312 * needing to rewrap encryption keys
3313 */
3314 err = dsl_dataset_promote_crypt_check(hds->ds_dir, origin_ds->ds_dir);
3315 if (err != 0)
3316 goto out;
3317
3318 /*
3319 * Compute and check the amount of space to transfer. Since this is
3320 * so expensive, don't do the preliminary check.
3321 */
3322 if (!dmu_tx_is_syncing(tx)) {
3323 promote_rele(ddpa, FTAG);
3324 return (0);
3325 }
3326
3327 /* compute origin's new unique space */
3328 snap = list_tail(&ddpa->clone_snaps);
3329 ASSERT(snap != NULL);
3330 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3331 origin_ds->ds_object);
3332 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3333 dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
3334 &ddpa->unique, &unused, &unused);
3335
3336 /*
3337 * Walk the snapshots that we are moving
3338 *
3339 * Compute space to transfer. Consider the incremental changes
3340 * to used by each snapshot:
3341 * (my used) = (prev's used) + (blocks born) - (blocks killed)
3342 * So each snapshot gave birth to:
3343 * (blocks born) = (my used) - (prev's used) + (blocks killed)
3344 * So a sequence would look like:
3345 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
3346 * Which simplifies to:
3347 * uN + kN + kN-1 + ... + k1 + k0
3348 * Note however, if we stop before we reach the ORIGIN we get:
3349 * uN + kN + kN-1 + ... + kM - uM-1
3350 */
3351 conflicting_snaps = B_FALSE;
3352 ss_mv_cnt = 0;
3353 ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
3354 ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
3355 ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
3356 for (snap = list_head(&ddpa->shared_snaps); snap;
3357 snap = list_next(&ddpa->shared_snaps, snap)) {
3358 uint64_t val, dlused, dlcomp, dluncomp;
3359 dsl_dataset_t *ds = snap->ds;
3360
3361 ss_mv_cnt++;
3362
3363 /*
3364 * If there are long holds, we won't be able to evict
3365 * the objset.
3366 */
3367 if (dsl_dataset_long_held(ds)) {
3368 err = SET_ERROR(EBUSY);
3369 goto out;
3370 }
3371
3372 /* Check that the snapshot name does not conflict */
3373 VERIFY0(dsl_dataset_get_snapname(ds));
3374 if (strlen(ds->ds_snapname) >= max_snap_len) {
3375 err = SET_ERROR(ENAMETOOLONG);
3376 goto out;
3377 }
3378 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
3379 if (err == 0) {
3380 fnvlist_add_boolean(ddpa->err_ds,
3381 snap->ds->ds_snapname);
3382 conflicting_snaps = B_TRUE;
3383 } else if (err != ENOENT) {
3384 goto out;
3385 }
3386
3387 /* The very first snapshot does not have a deadlist */
3388 if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
3389 continue;
3390
3391 dsl_deadlist_space(&ds->ds_deadlist,
3392 &dlused, &dlcomp, &dluncomp);
3393 ddpa->used += dlused;
3394 ddpa->comp += dlcomp;
3395 ddpa->uncomp += dluncomp;
3396 }
3397
3398 /*
3399 * Check that bookmarks that are being transferred don't have
3400 * name conflicts.
3401 */
3402 for (dsl_bookmark_node_t *dbn = avl_first(&origin_ds->ds_bookmarks);
3403 dbn != NULL && dbn->dbn_phys.zbm_creation_txg <=
3404 dsl_dataset_phys(origin_ds)->ds_creation_txg;
3405 dbn = AVL_NEXT(&origin_ds->ds_bookmarks, dbn)) {
3406 if (strlen(dbn->dbn_name) >= max_snap_len) {
3407 err = SET_ERROR(ENAMETOOLONG);
3408 goto out;
3409 }
3410 zfs_bookmark_phys_t bm;
3411 err = dsl_bookmark_lookup_impl(ddpa->ddpa_clone,
3412 dbn->dbn_name, &bm);
3413
3414 if (err == 0) {
3415 fnvlist_add_boolean(ddpa->err_ds, dbn->dbn_name);
3416 conflicting_snaps = B_TRUE;
3417 } else if (err == ESRCH) {
3418 err = 0;
3419 }
3420 if (err != 0) {
3421 goto out;
3422 }
3423 }
3424
3425 /*
3426 * In order to return the full list of conflicting snapshots, we check
3427 * whether there was a conflict after traversing all of them.
3428 */
3429 if (conflicting_snaps) {
3430 err = SET_ERROR(EEXIST);
3431 goto out;
3432 }
3433
3434 /*
3435 * If we are a clone of a clone then we never reached ORIGIN,
3436 * so we need to subtract out the clone origin's used space.
3437 */
3438 if (ddpa->origin_origin) {
3439 ddpa->used -=
3440 dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
3441 ddpa->comp -=
3442 dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
3443 ddpa->uncomp -=
3444 dsl_dataset_phys(ddpa->origin_origin)->
3445 ds_uncompressed_bytes;
3446 }
3447
3448 /* Check that there is enough space and limit headroom here */
3449 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
3450 0, ss_mv_cnt, ddpa->used, ddpa->cr, ddpa->proc);
3451 if (err != 0)
3452 goto out;
3453
3454 /*
3455 * Compute the amounts of space that will be used by snapshots
3456 * after the promotion (for both origin and clone). For each,
3457 * it is the amount of space that will be on all of their
3458 * deadlists (that was not born before their new origin).
3459 */
3460 if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
3461 uint64_t space;
3462
3463 /*
3464 * Note, typically this will not be a clone of a clone,
3465 * so dd_origin_txg will be < TXG_INITIAL, so
3466 * these snaplist_space() -> dsl_deadlist_space_range()
3467 * calls will be fast because they do not have to
3468 * iterate over all bps.
3469 */
3470 snap = list_head(&ddpa->origin_snaps);
3471 if (snap == NULL) {
3472 err = SET_ERROR(ENOENT);
3473 goto out;
3474 }
3475 err = snaplist_space(&ddpa->shared_snaps,
3476 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
3477 if (err != 0)
3478 goto out;
3479
3480 err = snaplist_space(&ddpa->clone_snaps,
3481 snap->ds->ds_dir->dd_origin_txg, &space);
3482 if (err != 0)
3483 goto out;
3484 ddpa->cloneusedsnap += space;
3485 }
3486 if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
3487 DD_FLAG_USED_BREAKDOWN) {
3488 err = snaplist_space(&ddpa->origin_snaps,
3489 dsl_dataset_phys(origin_ds)->ds_creation_txg,
3490 &ddpa->originusedsnap);
3491 if (err != 0)
3492 goto out;
3493 }
3494
3495 out:
3496 promote_rele(ddpa, FTAG);
3497 return (err);
3498 }
3499
3500 void
3501 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
3502 {
3503 dsl_dataset_promote_arg_t *ddpa = arg;
3504 dsl_pool_t *dp = dmu_tx_pool(tx);
3505 dsl_dataset_t *hds;
3506 struct promotenode *snap;
3507 dsl_dataset_t *origin_ds;
3508 dsl_dataset_t *origin_head;
3509 dsl_dir_t *dd;
3510 dsl_dir_t *odd = NULL;
3511 uint64_t oldnext_obj;
3512 int64_t delta;
3513
3514 ASSERT(nvlist_empty(ddpa->err_ds));
3515
3516 VERIFY0(promote_hold(ddpa, dp, FTAG));
3517 hds = ddpa->ddpa_clone;
3518
3519 ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
3520
3521 snap = list_head(&ddpa->shared_snaps);
3522 origin_ds = snap->ds;
3523 dd = hds->ds_dir;
3524
3525 snap = list_head(&ddpa->origin_snaps);
3526 origin_head = snap->ds;
3527
3528 /*
3529 * We need to explicitly open odd, since origin_ds's dd will be
3530 * changing.
3531 */
3532 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
3533 NULL, FTAG, &odd));
3534
3535 dsl_dataset_promote_crypt_sync(hds->ds_dir, odd, tx);
3536
3537 /* change origin's next snap */
3538 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
3539 oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
3540 snap = list_tail(&ddpa->clone_snaps);
3541 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3542 origin_ds->ds_object);
3543 dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
3544
3545 /* change the origin's next clone */
3546 if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
3547 dsl_dataset_remove_from_next_clones(origin_ds,
3548 snap->ds->ds_object, tx);
3549 VERIFY0(zap_add_int(dp->dp_meta_objset,
3550 dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
3551 oldnext_obj, tx));
3552 }
3553
3554 /* change origin */
3555 dmu_buf_will_dirty(dd->dd_dbuf, tx);
3556 ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
3557 dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
3558 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
3559 dmu_buf_will_dirty(odd->dd_dbuf, tx);
3560 dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
3561 origin_head->ds_dir->dd_origin_txg =
3562 dsl_dataset_phys(origin_ds)->ds_creation_txg;
3563
3564 /* change dd_clone entries */
3565 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3566 VERIFY0(zap_remove_int(dp->dp_meta_objset,
3567 dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
3568 VERIFY0(zap_add_int(dp->dp_meta_objset,
3569 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3570 hds->ds_object, tx));
3571
3572 VERIFY0(zap_remove_int(dp->dp_meta_objset,
3573 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3574 origin_head->ds_object, tx));
3575 if (dsl_dir_phys(dd)->dd_clones == 0) {
3576 dsl_dir_phys(dd)->dd_clones =
3577 zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
3578 DMU_OT_NONE, 0, tx);
3579 }
3580 VERIFY0(zap_add_int(dp->dp_meta_objset,
3581 dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
3582 }
3583
3584 /*
3585 * Move bookmarks to this dir.
3586 */
3587 dsl_bookmark_node_t *dbn_next;
3588 for (dsl_bookmark_node_t *dbn = avl_first(&origin_head->ds_bookmarks);
3589 dbn != NULL && dbn->dbn_phys.zbm_creation_txg <=
3590 dsl_dataset_phys(origin_ds)->ds_creation_txg;
3591 dbn = dbn_next) {
3592 dbn_next = AVL_NEXT(&origin_head->ds_bookmarks, dbn);
3593
3594 avl_remove(&origin_head->ds_bookmarks, dbn);
3595 VERIFY0(zap_remove(dp->dp_meta_objset,
3596 origin_head->ds_bookmarks_obj, dbn->dbn_name, tx));
3597
3598 dsl_bookmark_node_add(hds, dbn, tx);
3599 }
3600
3601 dsl_bookmark_next_changed(hds, origin_ds, tx);
3602
3603 /* move snapshots to this dir */
3604 for (snap = list_head(&ddpa->shared_snaps); snap;
3605 snap = list_next(&ddpa->shared_snaps, snap)) {
3606 dsl_dataset_t *ds = snap->ds;
3607
3608 /*
3609 * Property callbacks are registered to a particular
3610 * dsl_dir. Since ours is changing, evict the objset
3611 * so that they will be unregistered from the old dsl_dir.
3612 */
3613 if (ds->ds_objset) {
3614 dmu_objset_evict(ds->ds_objset);
3615 ds->ds_objset = NULL;
3616 }
3617
3618 /* move snap name entry */
3619 VERIFY0(dsl_dataset_get_snapname(ds));
3620 VERIFY0(dsl_dataset_snap_remove(origin_head,
3621 ds->ds_snapname, tx, B_TRUE));
3622 VERIFY0(zap_add(dp->dp_meta_objset,
3623 dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
3624 8, 1, &ds->ds_object, tx));
3625 dsl_fs_ss_count_adjust(hds->ds_dir, 1,
3626 DD_FIELD_SNAPSHOT_COUNT, tx);
3627
3628 /* change containing dsl_dir */
3629 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3630 ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
3631 dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
3632 ASSERT3P(ds->ds_dir, ==, odd);
3633 dsl_dir_rele(ds->ds_dir, ds);
3634 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
3635 NULL, ds, &ds->ds_dir));
3636
3637 /* move any clone references */
3638 if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
3639 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3640 zap_cursor_t zc;
3641 zap_attribute_t za;
3642
3643 for (zap_cursor_init(&zc, dp->dp_meta_objset,
3644 dsl_dataset_phys(ds)->ds_next_clones_obj);
3645 zap_cursor_retrieve(&zc, &za) == 0;
3646 zap_cursor_advance(&zc)) {
3647 dsl_dataset_t *cnds;
3648 uint64_t o;
3649
3650 if (za.za_first_integer == oldnext_obj) {
3651 /*
3652 * We've already moved the
3653 * origin's reference.
3654 */
3655 continue;
3656 }
3657
3658 VERIFY0(dsl_dataset_hold_obj(dp,
3659 za.za_first_integer, FTAG, &cnds));
3660 o = dsl_dir_phys(cnds->ds_dir)->
3661 dd_head_dataset_obj;
3662
3663 VERIFY0(zap_remove_int(dp->dp_meta_objset,
3664 dsl_dir_phys(odd)->dd_clones, o, tx));
3665 VERIFY0(zap_add_int(dp->dp_meta_objset,
3666 dsl_dir_phys(dd)->dd_clones, o, tx));
3667 dsl_dataset_rele(cnds, FTAG);
3668 }
3669 zap_cursor_fini(&zc);
3670 }
3671
3672 ASSERT(!dsl_prop_hascb(ds));
3673 }
3674
3675 /*
3676 * Change space accounting.
3677 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
3678 * both be valid, or both be 0 (resulting in delta == 0). This
3679 * is true for each of {clone,origin} independently.
3680 */
3681
3682 delta = ddpa->cloneusedsnap -
3683 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
3684 ASSERT3S(delta, >=, 0);
3685 ASSERT3U(ddpa->used, >=, delta);
3686 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
3687 dsl_dir_diduse_space(dd, DD_USED_HEAD,
3688 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
3689
3690 delta = ddpa->originusedsnap -
3691 dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
3692 ASSERT3S(delta, <=, 0);
3693 ASSERT3U(ddpa->used, >=, -delta);
3694 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
3695 dsl_dir_diduse_space(odd, DD_USED_HEAD,
3696 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
3697
3698 dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
3699
3700 /*
3701 * Since livelists are specific to a clone's origin txg, they
3702 * are no longer accurate. Destroy the livelist from the clone being
3703 * promoted. If the origin dataset is a clone, destroy its livelist
3704 * as well.
3705 */
3706 dsl_dir_remove_livelist(dd, tx, B_TRUE);
3707 dsl_dir_remove_livelist(odd, tx, B_TRUE);
3708
3709 /* log history record */
3710 spa_history_log_internal_ds(hds, "promote", tx, " ");
3711
3712 dsl_dir_rele(odd, FTAG);
3713 promote_rele(ddpa, FTAG);
3714
3715 /*
3716 * Transfer common error blocks from old head to new head.
3717 */
3718 if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_HEAD_ERRLOG)) {
3719 uint64_t old_head = origin_head->ds_object;
3720 uint64_t new_head = hds->ds_object;
3721 spa_swap_errlog(dp->dp_spa, new_head, old_head, tx);
3722 }
3723 }
3724
3725 /*
3726 * Make a list of dsl_dataset_t's for the snapshots between first_obj
3727 * (exclusive) and last_obj (inclusive). The list will be in reverse
3728 * order (last_obj will be the list_head()). If first_obj == 0, do all
3729 * snapshots back to this dataset's origin.
3730 */
3731 static int
3732 snaplist_make(dsl_pool_t *dp,
3733 uint64_t first_obj, uint64_t last_obj, list_t *l, const void *tag)
3734 {
3735 uint64_t obj = last_obj;
3736
3737 list_create(l, sizeof (struct promotenode),
3738 offsetof(struct promotenode, link));
3739
3740 while (obj != first_obj) {
3741 dsl_dataset_t *ds;
3742 struct promotenode *snap;
3743 int err;
3744
3745 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
3746 ASSERT(err != ENOENT);
3747 if (err != 0)
3748 return (err);
3749
3750 if (first_obj == 0)
3751 first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
3752
3753 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
3754 snap->ds = ds;
3755 list_insert_tail(l, snap);
3756 obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3757 }
3758
3759 return (0);
3760 }
3761
3762 static int
3763 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
3764 {
3765 struct promotenode *snap;
3766
3767 *spacep = 0;
3768 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
3769 uint64_t used, comp, uncomp;
3770 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3771 mintxg, UINT64_MAX, &used, &comp, &uncomp);
3772 *spacep += used;
3773 }
3774 return (0);
3775 }
3776
3777 static void
3778 snaplist_destroy(list_t *l, const void *tag)
3779 {
3780 struct promotenode *snap;
3781
3782 if (l == NULL || !list_link_active(&l->list_head))
3783 return;
3784
3785 while ((snap = list_tail(l)) != NULL) {
3786 list_remove(l, snap);
3787 dsl_dataset_rele(snap->ds, tag);
3788 kmem_free(snap, sizeof (*snap));
3789 }
3790 list_destroy(l);
3791 }
3792
3793 static int
3794 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, const void *tag)
3795 {
3796 int error;
3797 dsl_dir_t *dd;
3798 struct promotenode *snap;
3799
3800 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
3801 &ddpa->ddpa_clone);
3802 if (error != 0)
3803 return (error);
3804 dd = ddpa->ddpa_clone->ds_dir;
3805
3806 if (ddpa->ddpa_clone->ds_is_snapshot ||
3807 !dsl_dir_is_clone(dd)) {
3808 dsl_dataset_rele(ddpa->ddpa_clone, tag);
3809 return (SET_ERROR(EINVAL));
3810 }
3811
3812 error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
3813 &ddpa->shared_snaps, tag);
3814 if (error != 0)
3815 goto out;
3816
3817 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
3818 &ddpa->clone_snaps, tag);
3819 if (error != 0)
3820 goto out;
3821
3822 snap = list_head(&ddpa->shared_snaps);
3823 ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
3824 error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
3825 dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
3826 &ddpa->origin_snaps, tag);
3827 if (error != 0)
3828 goto out;
3829
3830 if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
3831 error = dsl_dataset_hold_obj(dp,
3832 dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
3833 tag, &ddpa->origin_origin);
3834 if (error != 0)
3835 goto out;
3836 }
3837 out:
3838 if (error != 0)
3839 promote_rele(ddpa, tag);
3840 return (error);
3841 }
3842
3843 static void
3844 promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag)
3845 {
3846 snaplist_destroy(&ddpa->shared_snaps, tag);
3847 snaplist_destroy(&ddpa->clone_snaps, tag);
3848 snaplist_destroy(&ddpa->origin_snaps, tag);
3849 if (ddpa->origin_origin != NULL)
3850 dsl_dataset_rele(ddpa->origin_origin, tag);
3851 dsl_dataset_rele(ddpa->ddpa_clone, tag);
3852 }
3853
3854 /*
3855 * Promote a clone.
3856 *
3857 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
3858 * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
3859 */
3860 int
3861 dsl_dataset_promote(const char *name, char *conflsnap)
3862 {
3863 dsl_dataset_promote_arg_t ddpa = { 0 };
3864 uint64_t numsnaps;
3865 int error;
3866 nvpair_t *snap_pair;
3867 objset_t *os;
3868
3869 /*
3870 * We will modify space proportional to the number of
3871 * snapshots. Compute numsnaps.
3872 */
3873 error = dmu_objset_hold(name, FTAG, &os);
3874 if (error != 0)
3875 return (error);
3876 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
3877 dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
3878 &numsnaps);
3879 dmu_objset_rele(os, FTAG);
3880 if (error != 0)
3881 return (error);
3882
3883 ddpa.ddpa_clonename = name;
3884 ddpa.err_ds = fnvlist_alloc();
3885 ddpa.cr = CRED();
3886 ddpa.proc = curproc;
3887
3888 error = dsl_sync_task(name, dsl_dataset_promote_check,
3889 dsl_dataset_promote_sync, &ddpa,
3890 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED);
3891
3892 /*
3893 * Return the first conflicting snapshot found.
3894 */
3895 snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL);
3896 if (snap_pair != NULL && conflsnap != NULL)
3897 (void) strlcpy(conflsnap, nvpair_name(snap_pair),
3898 ZFS_MAX_DATASET_NAME_LEN);
3899
3900 fnvlist_free(ddpa.err_ds);
3901 return (error);
3902 }
3903
3904 int
3905 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
3906 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
3907 {
3908 /*
3909 * "slack" factor for received datasets with refquota set on them.
3910 * See the bottom of this function for details on its use.
3911 */
3912 uint64_t refquota_slack = (uint64_t)DMU_MAX_ACCESS *
3913 spa_asize_inflation;
3914 int64_t unused_refres_delta;
3915
3916 /* they should both be heads */
3917 if (clone->ds_is_snapshot ||
3918 origin_head->ds_is_snapshot)
3919 return (SET_ERROR(EINVAL));
3920
3921 /* if we are not forcing, the branch point should be just before them */
3922 if (!force && clone->ds_prev != origin_head->ds_prev)
3923 return (SET_ERROR(EINVAL));
3924
3925 /* clone should be the clone (unless they are unrelated) */
3926 if (clone->ds_prev != NULL &&
3927 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
3928 origin_head->ds_dir != clone->ds_prev->ds_dir)
3929 return (SET_ERROR(EINVAL));
3930
3931 /* the clone should be a child of the origin */
3932 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
3933 return (SET_ERROR(EINVAL));
3934
3935 /* origin_head shouldn't be modified unless 'force' */
3936 if (!force &&
3937 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
3938 return (SET_ERROR(ETXTBSY));
3939
3940 /* origin_head should have no long holds (e.g. is not mounted) */
3941 if (dsl_dataset_handoff_check(origin_head, owner, tx))
3942 return (SET_ERROR(EBUSY));
3943
3944 /* check amount of any unconsumed refreservation */
3945 unused_refres_delta =
3946 (int64_t)MIN(origin_head->ds_reserved,
3947 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3948 (int64_t)MIN(origin_head->ds_reserved,
3949 dsl_dataset_phys(clone)->ds_unique_bytes);
3950
3951 if (unused_refres_delta > 0 &&
3952 unused_refres_delta >
3953 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
3954 return (SET_ERROR(ENOSPC));
3955
3956 /*
3957 * The clone can't be too much over the head's refquota.
3958 *
3959 * To ensure that the entire refquota can be used, we allow one
3960 * transaction to exceed the refquota. Therefore, this check
3961 * needs to also allow for the space referenced to be more than the
3962 * refquota. The maximum amount of space that one transaction can use
3963 * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this
3964 * overage ensures that we are able to receive a filesystem that
3965 * exceeds the refquota on the source system.
3966 *
3967 * So that overage is the refquota_slack we use below.
3968 */
3969 if (origin_head->ds_quota != 0 &&
3970 dsl_dataset_phys(clone)->ds_referenced_bytes >
3971 origin_head->ds_quota + refquota_slack)
3972 return (SET_ERROR(EDQUOT));
3973
3974 return (0);
3975 }
3976
3977 static void
3978 dsl_dataset_swap_remap_deadlists(dsl_dataset_t *clone,
3979 dsl_dataset_t *origin, dmu_tx_t *tx)
3980 {
3981 uint64_t clone_remap_dl_obj, origin_remap_dl_obj;
3982 dsl_pool_t *dp = dmu_tx_pool(tx);
3983
3984 ASSERT(dsl_pool_sync_context(dp));
3985
3986 clone_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(clone);
3987 origin_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(origin);
3988
3989 if (clone_remap_dl_obj != 0) {
3990 dsl_deadlist_close(&clone->ds_remap_deadlist);
3991 dsl_dataset_unset_remap_deadlist_object(clone, tx);
3992 }
3993 if (origin_remap_dl_obj != 0) {
3994 dsl_deadlist_close(&origin->ds_remap_deadlist);
3995 dsl_dataset_unset_remap_deadlist_object(origin, tx);
3996 }
3997
3998 if (clone_remap_dl_obj != 0) {
3999 dsl_dataset_set_remap_deadlist_object(origin,
4000 clone_remap_dl_obj, tx);
4001 dsl_deadlist_open(&origin->ds_remap_deadlist,
4002 dp->dp_meta_objset, clone_remap_dl_obj);
4003 }
4004 if (origin_remap_dl_obj != 0) {
4005 dsl_dataset_set_remap_deadlist_object(clone,
4006 origin_remap_dl_obj, tx);
4007 dsl_deadlist_open(&clone->ds_remap_deadlist,
4008 dp->dp_meta_objset, origin_remap_dl_obj);
4009 }
4010 }
4011
4012 void
4013 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
4014 dsl_dataset_t *origin_head, dmu_tx_t *tx)
4015 {
4016 dsl_pool_t *dp = dmu_tx_pool(tx);
4017 int64_t unused_refres_delta;
4018
4019 ASSERT(clone->ds_reserved == 0);
4020 /*
4021 * NOTE: On DEBUG kernels there could be a race between this and
4022 * the check function if spa_asize_inflation is adjusted...
4023 */
4024 ASSERT(origin_head->ds_quota == 0 ||
4025 dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
4026 DMU_MAX_ACCESS * spa_asize_inflation);
4027 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
4028
4029 dsl_dir_cancel_waiters(origin_head->ds_dir);
4030
4031 /*
4032 * Swap per-dataset feature flags.
4033 */
4034 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4035 if (!(spa_feature_table[f].fi_flags &
4036 ZFEATURE_FLAG_PER_DATASET)) {
4037 ASSERT(!dsl_dataset_feature_is_active(clone, f));
4038 ASSERT(!dsl_dataset_feature_is_active(origin_head, f));
4039 continue;
4040 }
4041
4042 boolean_t clone_inuse = dsl_dataset_feature_is_active(clone, f);
4043 void *clone_feature = clone->ds_feature[f];
4044 boolean_t origin_head_inuse =
4045 dsl_dataset_feature_is_active(origin_head, f);
4046 void *origin_head_feature = origin_head->ds_feature[f];
4047
4048 if (clone_inuse)
4049 dsl_dataset_deactivate_feature_impl(clone, f, tx);
4050 if (origin_head_inuse)
4051 dsl_dataset_deactivate_feature_impl(origin_head, f, tx);
4052
4053 if (clone_inuse) {
4054 dsl_dataset_activate_feature(origin_head->ds_object, f,
4055 clone_feature, tx);
4056 origin_head->ds_feature[f] = clone_feature;
4057 }
4058 if (origin_head_inuse) {
4059 dsl_dataset_activate_feature(clone->ds_object, f,
4060 origin_head_feature, tx);
4061 clone->ds_feature[f] = origin_head_feature;
4062 }
4063 }
4064
4065 dmu_buf_will_dirty(clone->ds_dbuf, tx);
4066 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
4067
4068 if (clone->ds_objset != NULL) {
4069 dmu_objset_evict(clone->ds_objset);
4070 clone->ds_objset = NULL;
4071 }
4072
4073 if (origin_head->ds_objset != NULL) {
4074 dmu_objset_evict(origin_head->ds_objset);
4075 origin_head->ds_objset = NULL;
4076 }
4077
4078 unused_refres_delta =
4079 (int64_t)MIN(origin_head->ds_reserved,
4080 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
4081 (int64_t)MIN(origin_head->ds_reserved,
4082 dsl_dataset_phys(clone)->ds_unique_bytes);
4083
4084 /*
4085 * Reset origin's unique bytes.
4086 */
4087 {
4088 dsl_dataset_t *origin = clone->ds_prev;
4089 uint64_t comp, uncomp;
4090
4091 dmu_buf_will_dirty(origin->ds_dbuf, tx);
4092 dsl_deadlist_space_range(&clone->ds_deadlist,
4093 dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
4094 &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
4095 }
4096
4097 /* swap blkptrs */
4098 {
4099 rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
4100 rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
4101 blkptr_t tmp;
4102 tmp = dsl_dataset_phys(origin_head)->ds_bp;
4103 dsl_dataset_phys(origin_head)->ds_bp =
4104 dsl_dataset_phys(clone)->ds_bp;
4105 dsl_dataset_phys(clone)->ds_bp = tmp;
4106 rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
4107 rrw_exit(&clone->ds_bp_rwlock, FTAG);
4108 }
4109
4110 /* set dd_*_bytes */
4111 {
4112 int64_t dused, dcomp, duncomp;
4113 uint64_t cdl_used, cdl_comp, cdl_uncomp;
4114 uint64_t odl_used, odl_comp, odl_uncomp;
4115
4116 ASSERT3U(dsl_dir_phys(clone->ds_dir)->
4117 dd_used_breakdown[DD_USED_SNAP], ==, 0);
4118
4119 dsl_deadlist_space(&clone->ds_deadlist,
4120 &cdl_used, &cdl_comp, &cdl_uncomp);
4121 dsl_deadlist_space(&origin_head->ds_deadlist,
4122 &odl_used, &odl_comp, &odl_uncomp);
4123
4124 dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
4125 cdl_used -
4126 (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
4127 odl_used);
4128 dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
4129 cdl_comp -
4130 (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
4131 odl_comp);
4132 duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
4133 cdl_uncomp -
4134 (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
4135 odl_uncomp);
4136
4137 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
4138 dused, dcomp, duncomp, tx);
4139 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
4140 -dused, -dcomp, -duncomp, tx);
4141
4142 /*
4143 * The difference in the space used by snapshots is the
4144 * difference in snapshot space due to the head's
4145 * deadlist (since that's the only thing that's
4146 * changing that affects the snapused).
4147 */
4148 dsl_deadlist_space_range(&clone->ds_deadlist,
4149 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
4150 &cdl_used, &cdl_comp, &cdl_uncomp);
4151 dsl_deadlist_space_range(&origin_head->ds_deadlist,
4152 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
4153 &odl_used, &odl_comp, &odl_uncomp);
4154 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
4155 DD_USED_HEAD, DD_USED_SNAP, tx);
4156 }
4157
4158 /* swap ds_*_bytes */
4159 SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
4160 dsl_dataset_phys(clone)->ds_referenced_bytes);
4161 SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
4162 dsl_dataset_phys(clone)->ds_compressed_bytes);
4163 SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
4164 dsl_dataset_phys(clone)->ds_uncompressed_bytes);
4165 SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
4166 dsl_dataset_phys(clone)->ds_unique_bytes);
4167
4168 /* apply any parent delta for change in unconsumed refreservation */
4169 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
4170 unused_refres_delta, 0, 0, tx);
4171
4172 /*
4173 * Swap deadlists.
4174 */
4175 dsl_deadlist_close(&clone->ds_deadlist);
4176 dsl_deadlist_close(&origin_head->ds_deadlist);
4177 SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
4178 dsl_dataset_phys(clone)->ds_deadlist_obj);
4179 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
4180 dsl_dataset_phys(clone)->ds_deadlist_obj);
4181 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
4182 dsl_dataset_phys(origin_head)->ds_deadlist_obj);
4183 dsl_dataset_swap_remap_deadlists(clone, origin_head, tx);
4184
4185 /*
4186 * If there is a bookmark at the origin, its "next dataset" is
4187 * changing, so we need to reset its FBN.
4188 */
4189 dsl_bookmark_next_changed(origin_head, origin_head->ds_prev, tx);
4190
4191 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
4192
4193 /*
4194 * Destroy any livelists associated with the clone or the origin,
4195 * since after the swap the corresponding livelists are no longer
4196 * valid.
4197 */
4198 dsl_dir_remove_livelist(clone->ds_dir, tx, B_TRUE);
4199 dsl_dir_remove_livelist(origin_head->ds_dir, tx, B_TRUE);
4200
4201 spa_history_log_internal_ds(clone, "clone swap", tx,
4202 "parent=%s", origin_head->ds_dir->dd_myname);
4203 }
4204
4205 /*
4206 * Given a pool name and a dataset object number in that pool,
4207 * return the name of that dataset.
4208 */
4209 int
4210 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
4211 {
4212 dsl_pool_t *dp;
4213 dsl_dataset_t *ds;
4214 int error;
4215
4216 error = dsl_pool_hold(pname, FTAG, &dp);
4217 if (error != 0)
4218 return (error);
4219
4220 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
4221 if (error == 0) {
4222 dsl_dataset_name(ds, buf);
4223 dsl_dataset_rele(ds, FTAG);
4224 }
4225 dsl_pool_rele(dp, FTAG);
4226
4227 return (error);
4228 }
4229
4230 int
4231 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
4232 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
4233 {
4234 int error = 0;
4235
4236 ASSERT3S(asize, >, 0);
4237
4238 /*
4239 * *ref_rsrv is the portion of asize that will come from any
4240 * unconsumed refreservation space.
4241 */
4242 *ref_rsrv = 0;
4243
4244 mutex_enter(&ds->ds_lock);
4245 /*
4246 * Make a space adjustment for reserved bytes.
4247 */
4248 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
4249 ASSERT3U(*used, >=,
4250 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
4251 *used -=
4252 (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
4253 *ref_rsrv =
4254 asize - MIN(asize, parent_delta(ds, asize + inflight));
4255 }
4256
4257 if (!check_quota || ds->ds_quota == 0) {
4258 mutex_exit(&ds->ds_lock);
4259 return (0);
4260 }
4261 /*
4262 * If they are requesting more space, and our current estimate
4263 * is over quota, they get to try again unless the actual
4264 * on-disk is over quota and there are no pending changes (which
4265 * may free up space for us).
4266 */
4267 if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
4268 ds->ds_quota) {
4269 if (inflight > 0 ||
4270 dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
4271 error = SET_ERROR(ERESTART);
4272 else
4273 error = SET_ERROR(EDQUOT);
4274 }
4275 mutex_exit(&ds->ds_lock);
4276
4277 return (error);
4278 }
4279
4280 typedef struct dsl_dataset_set_qr_arg {
4281 const char *ddsqra_name;
4282 zprop_source_t ddsqra_source;
4283 uint64_t ddsqra_value;
4284 } dsl_dataset_set_qr_arg_t;
4285
4286
4287 static int
4288 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
4289 {
4290 dsl_dataset_set_qr_arg_t *ddsqra = arg;
4291 dsl_pool_t *dp = dmu_tx_pool(tx);
4292 dsl_dataset_t *ds;
4293 int error;
4294 uint64_t newval;
4295
4296 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
4297 return (SET_ERROR(ENOTSUP));
4298
4299 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4300 if (error != 0)
4301 return (error);
4302
4303 if (ds->ds_is_snapshot) {
4304 dsl_dataset_rele(ds, FTAG);
4305 return (SET_ERROR(EINVAL));
4306 }
4307
4308 error = dsl_prop_predict(ds->ds_dir,
4309 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4310 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4311 if (error != 0) {
4312 dsl_dataset_rele(ds, FTAG);
4313 return (error);
4314 }
4315
4316 if (newval == 0) {
4317 dsl_dataset_rele(ds, FTAG);
4318 return (0);
4319 }
4320
4321 if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
4322 newval < ds->ds_reserved) {
4323 dsl_dataset_rele(ds, FTAG);
4324 return (SET_ERROR(ENOSPC));
4325 }
4326
4327 dsl_dataset_rele(ds, FTAG);
4328 return (0);
4329 }
4330
4331 static void
4332 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
4333 {
4334 dsl_dataset_set_qr_arg_t *ddsqra = arg;
4335 dsl_pool_t *dp = dmu_tx_pool(tx);
4336 dsl_dataset_t *ds = NULL;
4337 uint64_t newval;
4338
4339 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4340
4341 dsl_prop_set_sync_impl(ds,
4342 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4343 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
4344 &ddsqra->ddsqra_value, tx);
4345
4346 VERIFY0(dsl_prop_get_int_ds(ds,
4347 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
4348
4349 if (ds->ds_quota != newval) {
4350 dmu_buf_will_dirty(ds->ds_dbuf, tx);
4351 ds->ds_quota = newval;
4352 }
4353 dsl_dataset_rele(ds, FTAG);
4354 }
4355
4356 int
4357 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
4358 uint64_t refquota)
4359 {
4360 dsl_dataset_set_qr_arg_t ddsqra;
4361
4362 ddsqra.ddsqra_name = dsname;
4363 ddsqra.ddsqra_source = source;
4364 ddsqra.ddsqra_value = refquota;
4365
4366 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
4367 dsl_dataset_set_refquota_sync, &ddsqra, 0,
4368 ZFS_SPACE_CHECK_EXTRA_RESERVED));
4369 }
4370
4371 static int
4372 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
4373 {
4374 dsl_dataset_set_qr_arg_t *ddsqra = arg;
4375 dsl_pool_t *dp = dmu_tx_pool(tx);
4376 dsl_dataset_t *ds;
4377 int error;
4378 uint64_t newval, unique;
4379
4380 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
4381 return (SET_ERROR(ENOTSUP));
4382
4383 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4384 if (error != 0)
4385 return (error);
4386
4387 if (ds->ds_is_snapshot) {
4388 dsl_dataset_rele(ds, FTAG);
4389 return (SET_ERROR(EINVAL));
4390 }
4391
4392 error = dsl_prop_predict(ds->ds_dir,
4393 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4394 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4395 if (error != 0) {
4396 dsl_dataset_rele(ds, FTAG);
4397 return (error);
4398 }
4399
4400 /*
4401 * If we are doing the preliminary check in open context, the
4402 * space estimates may be inaccurate.
4403 */
4404 if (!dmu_tx_is_syncing(tx)) {
4405 dsl_dataset_rele(ds, FTAG);
4406 return (0);
4407 }
4408
4409 mutex_enter(&ds->ds_lock);
4410 if (!DS_UNIQUE_IS_ACCURATE(ds))
4411 dsl_dataset_recalc_head_uniq(ds);
4412 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
4413 mutex_exit(&ds->ds_lock);
4414
4415 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
4416 uint64_t delta = MAX(unique, newval) -
4417 MAX(unique, ds->ds_reserved);
4418
4419 if (delta >
4420 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
4421 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
4422 dsl_dataset_rele(ds, FTAG);
4423 return (SET_ERROR(ENOSPC));
4424 }
4425 }
4426
4427 dsl_dataset_rele(ds, FTAG);
4428 return (0);
4429 }
4430
4431 void
4432 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
4433 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
4434 {
4435 uint64_t newval;
4436 uint64_t unique;
4437 int64_t delta;
4438
4439 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4440 source, sizeof (value), 1, &value, tx);
4441
4442 VERIFY0(dsl_prop_get_int_ds(ds,
4443 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
4444
4445 dmu_buf_will_dirty(ds->ds_dbuf, tx);
4446 mutex_enter(&ds->ds_dir->dd_lock);
4447 mutex_enter(&ds->ds_lock);
4448 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
4449 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
4450 delta = MAX(0, (int64_t)(newval - unique)) -
4451 MAX(0, (int64_t)(ds->ds_reserved - unique));
4452 ds->ds_reserved = newval;
4453 mutex_exit(&ds->ds_lock);
4454
4455 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
4456 mutex_exit(&ds->ds_dir->dd_lock);
4457 }
4458
4459 static void
4460 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
4461 {
4462 dsl_dataset_set_qr_arg_t *ddsqra = arg;
4463 dsl_pool_t *dp = dmu_tx_pool(tx);
4464 dsl_dataset_t *ds = NULL;
4465
4466 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4467 dsl_dataset_set_refreservation_sync_impl(ds,
4468 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
4469 dsl_dataset_rele(ds, FTAG);
4470 }
4471
4472 int
4473 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
4474 uint64_t refreservation)
4475 {
4476 dsl_dataset_set_qr_arg_t ddsqra;
4477
4478 ddsqra.ddsqra_name = dsname;
4479 ddsqra.ddsqra_source = source;
4480 ddsqra.ddsqra_value = refreservation;
4481
4482 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
4483 dsl_dataset_set_refreservation_sync, &ddsqra, 0,
4484 ZFS_SPACE_CHECK_EXTRA_RESERVED));
4485 }
4486
4487 typedef struct dsl_dataset_set_compression_arg {
4488 const char *ddsca_name;
4489 zprop_source_t ddsca_source;
4490 uint64_t ddsca_value;
4491 } dsl_dataset_set_compression_arg_t;
4492
4493 static int
4494 dsl_dataset_set_compression_check(void *arg, dmu_tx_t *tx)
4495 {
4496 dsl_dataset_set_compression_arg_t *ddsca = arg;
4497 dsl_pool_t *dp = dmu_tx_pool(tx);
4498
4499 uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value);
4500 spa_feature_t f = zio_compress_to_feature(compval);
4501
4502 if (f == SPA_FEATURE_NONE)
4503 return (SET_ERROR(EINVAL));
4504
4505 if (!spa_feature_is_enabled(dp->dp_spa, f))
4506 return (SET_ERROR(ENOTSUP));
4507
4508 return (0);
4509 }
4510
4511 static void
4512 dsl_dataset_set_compression_sync(void *arg, dmu_tx_t *tx)
4513 {
4514 dsl_dataset_set_compression_arg_t *ddsca = arg;
4515 dsl_pool_t *dp = dmu_tx_pool(tx);
4516 dsl_dataset_t *ds = NULL;
4517
4518 uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value);
4519 spa_feature_t f = zio_compress_to_feature(compval);
4520 ASSERT3S(f, !=, SPA_FEATURE_NONE);
4521 ASSERT3S(spa_feature_table[f].fi_type, ==, ZFEATURE_TYPE_BOOLEAN);
4522
4523 VERIFY0(dsl_dataset_hold(dp, ddsca->ddsca_name, FTAG, &ds));
4524 if (zfeature_active(f, ds->ds_feature[f]) != B_TRUE) {
4525 ds->ds_feature_activation[f] = (void *)B_TRUE;
4526 dsl_dataset_activate_feature(ds->ds_object, f,
4527 ds->ds_feature_activation[f], tx);
4528 ds->ds_feature[f] = ds->ds_feature_activation[f];
4529 }
4530 dsl_dataset_rele(ds, FTAG);
4531 }
4532
4533 int
4534 dsl_dataset_set_compression(const char *dsname, zprop_source_t source,
4535 uint64_t compression)
4536 {
4537 dsl_dataset_set_compression_arg_t ddsca;
4538
4539 /*
4540 * The sync task is only required for zstd in order to activate
4541 * the feature flag when the property is first set.
4542 */
4543 if (ZIO_COMPRESS_ALGO(compression) != ZIO_COMPRESS_ZSTD)
4544 return (0);
4545
4546 ddsca.ddsca_name = dsname;
4547 ddsca.ddsca_source = source;
4548 ddsca.ddsca_value = compression;
4549
4550 return (dsl_sync_task(dsname, dsl_dataset_set_compression_check,
4551 dsl_dataset_set_compression_sync, &ddsca, 0,
4552 ZFS_SPACE_CHECK_EXTRA_RESERVED));
4553 }
4554
4555 /*
4556 * Return (in *usedp) the amount of space referenced by "new" that was not
4557 * referenced at the time the bookmark corresponds to. "New" may be a
4558 * snapshot or a head. The bookmark must be before new, in
4559 * new's filesystem (or its origin) -- caller verifies this.
4560 *
4561 * The written space is calculated by considering two components: First, we
4562 * ignore any freed space, and calculate the written as new's used space
4563 * minus old's used space. Next, we add in the amount of space that was freed
4564 * between the two time points, thus reducing new's used space relative to
4565 * old's. Specifically, this is the space that was born before
4566 * zbm_creation_txg, and freed before new (ie. on new's deadlist or a
4567 * previous deadlist).
4568 *
4569 * space freed [---------------------]
4570 * snapshots ---O-------O--------O-------O------
4571 * bookmark new
4572 *
4573 * Note, the bookmark's zbm_*_bytes_refd must be valid, but if the HAS_FBN
4574 * flag is not set, we will calculate the freed_before_next based on the
4575 * next snapshot's deadlist, rather than using zbm_*_freed_before_next_snap.
4576 */
4577 static int
4578 dsl_dataset_space_written_impl(zfs_bookmark_phys_t *bmp,
4579 dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4580 {
4581 int err = 0;
4582 dsl_pool_t *dp = new->ds_dir->dd_pool;
4583
4584 ASSERT(dsl_pool_config_held(dp));
4585 if (dsl_dataset_is_snapshot(new)) {
4586 ASSERT3U(bmp->zbm_creation_txg, <,
4587 dsl_dataset_phys(new)->ds_creation_txg);
4588 }
4589
4590 *usedp = 0;
4591 *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
4592 *usedp -= bmp->zbm_referenced_bytes_refd;
4593
4594 *compp = 0;
4595 *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
4596 *compp -= bmp->zbm_compressed_bytes_refd;
4597
4598 *uncompp = 0;
4599 *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
4600 *uncompp -= bmp->zbm_uncompressed_bytes_refd;
4601
4602 dsl_dataset_t *snap = new;
4603
4604 while (dsl_dataset_phys(snap)->ds_prev_snap_txg >
4605 bmp->zbm_creation_txg) {
4606 uint64_t used, comp, uncomp;
4607
4608 dsl_deadlist_space_range(&snap->ds_deadlist,
4609 0, bmp->zbm_creation_txg,
4610 &used, &comp, &uncomp);
4611 *usedp += used;
4612 *compp += comp;
4613 *uncompp += uncomp;
4614
4615 uint64_t snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
4616 if (snap != new)
4617 dsl_dataset_rele(snap, FTAG);
4618 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
4619 if (err != 0)
4620 break;
4621 }
4622
4623 /*
4624 * We might not have the FBN if we are calculating written from
4625 * a snapshot (because we didn't know the correct "next" snapshot
4626 * until now).
4627 */
4628 if (bmp->zbm_flags & ZBM_FLAG_HAS_FBN) {
4629 *usedp += bmp->zbm_referenced_freed_before_next_snap;
4630 *compp += bmp->zbm_compressed_freed_before_next_snap;
4631 *uncompp += bmp->zbm_uncompressed_freed_before_next_snap;
4632 } else {
4633 ASSERT3U(dsl_dataset_phys(snap)->ds_prev_snap_txg, ==,
4634 bmp->zbm_creation_txg);
4635 uint64_t used, comp, uncomp;
4636 dsl_deadlist_space(&snap->ds_deadlist, &used, &comp, &uncomp);
4637 *usedp += used;
4638 *compp += comp;
4639 *uncompp += uncomp;
4640 }
4641 if (snap != new)
4642 dsl_dataset_rele(snap, FTAG);
4643 return (err);
4644 }
4645
4646 /*
4647 * Return (in *usedp) the amount of space written in new that was not
4648 * present at the time the bookmark corresponds to. New may be a
4649 * snapshot or the head. Old must be a bookmark before new, in
4650 * new's filesystem (or its origin) -- caller verifies this.
4651 */
4652 int
4653 dsl_dataset_space_written_bookmark(zfs_bookmark_phys_t *bmp,
4654 dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4655 {
4656 if (!(bmp->zbm_flags & ZBM_FLAG_HAS_FBN))
4657 return (SET_ERROR(ENOTSUP));
4658 return (dsl_dataset_space_written_impl(bmp, new,
4659 usedp, compp, uncompp));
4660 }
4661
4662 /*
4663 * Return (in *usedp) the amount of space written in new that is not
4664 * present in oldsnap. New may be a snapshot or the head. Old must be
4665 * a snapshot before new, in new's filesystem (or its origin). If not then
4666 * fail and return EINVAL.
4667 */
4668 int
4669 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
4670 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4671 {
4672 if (!dsl_dataset_is_before(new, oldsnap, 0))
4673 return (SET_ERROR(EINVAL));
4674
4675 zfs_bookmark_phys_t zbm = { 0 };
4676 dsl_dataset_phys_t *dsp = dsl_dataset_phys(oldsnap);
4677 zbm.zbm_guid = dsp->ds_guid;
4678 zbm.zbm_creation_txg = dsp->ds_creation_txg;
4679 zbm.zbm_creation_time = dsp->ds_creation_time;
4680 zbm.zbm_referenced_bytes_refd = dsp->ds_referenced_bytes;
4681 zbm.zbm_compressed_bytes_refd = dsp->ds_compressed_bytes;
4682 zbm.zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes;
4683
4684 /*
4685 * If oldsnap is the origin (or origin's origin, ...) of new,
4686 * we can't easily calculate the effective FBN. Therefore,
4687 * we do not set ZBM_FLAG_HAS_FBN, so that the _impl will calculate
4688 * it relative to the correct "next": the next snapshot towards "new",
4689 * rather than the next snapshot in oldsnap's dsl_dir.
4690 */
4691 return (dsl_dataset_space_written_impl(&zbm, new,
4692 usedp, compp, uncompp));
4693 }
4694
4695 /*
4696 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
4697 * lastsnap, and all snapshots in between are deleted.
4698 *
4699 * blocks that would be freed [---------------------------]
4700 * snapshots ---O-------O--------O-------O--------O
4701 * firstsnap lastsnap
4702 *
4703 * This is the set of blocks that were born after the snap before firstsnap,
4704 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
4705 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
4706 * We calculate this by iterating over the relevant deadlists (from the snap
4707 * after lastsnap, backward to the snap after firstsnap), summing up the
4708 * space on the deadlist that was born after the snap before firstsnap.
4709 */
4710 int
4711 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
4712 dsl_dataset_t *lastsnap,
4713 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4714 {
4715 int err = 0;
4716 uint64_t snapobj;
4717 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
4718
4719 ASSERT(firstsnap->ds_is_snapshot);
4720 ASSERT(lastsnap->ds_is_snapshot);
4721
4722 /*
4723 * Check that the snapshots are in the same dsl_dir, and firstsnap
4724 * is before lastsnap.
4725 */
4726 if (firstsnap->ds_dir != lastsnap->ds_dir ||
4727 dsl_dataset_phys(firstsnap)->ds_creation_txg >
4728 dsl_dataset_phys(lastsnap)->ds_creation_txg)
4729 return (SET_ERROR(EINVAL));
4730
4731 *usedp = *compp = *uncompp = 0;
4732
4733 snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
4734 while (snapobj != firstsnap->ds_object) {
4735 dsl_dataset_t *ds;
4736 uint64_t used, comp, uncomp;
4737
4738 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
4739 if (err != 0)
4740 break;
4741
4742 dsl_deadlist_space_range(&ds->ds_deadlist,
4743 dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
4744 &used, &comp, &uncomp);
4745 *usedp += used;
4746 *compp += comp;
4747 *uncompp += uncomp;
4748
4749 snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4750 ASSERT3U(snapobj, !=, 0);
4751 dsl_dataset_rele(ds, FTAG);
4752 }
4753 return (err);
4754 }
4755
4756 /*
4757 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
4758 * For example, they could both be snapshots of the same filesystem, and
4759 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
4760 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
4761 * filesystem. Or 'earlier' could be the origin's origin.
4762 *
4763 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
4764 */
4765 boolean_t
4766 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
4767 uint64_t earlier_txg)
4768 {
4769 dsl_pool_t *dp = later->ds_dir->dd_pool;
4770 int error;
4771 boolean_t ret;
4772
4773 ASSERT(dsl_pool_config_held(dp));
4774 ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
4775
4776 if (earlier_txg == 0)
4777 earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
4778
4779 if (later->ds_is_snapshot &&
4780 earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
4781 return (B_FALSE);
4782
4783 if (later->ds_dir == earlier->ds_dir)
4784 return (B_TRUE);
4785
4786 /*
4787 * We check dd_origin_obj explicitly here rather than using
4788 * dsl_dir_is_clone() so that we will return TRUE if "earlier"
4789 * is $ORIGIN@$ORIGIN. dsl_dataset_space_written() depends on
4790 * this behavior.
4791 */
4792 if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == 0)
4793 return (B_FALSE);
4794
4795 dsl_dataset_t *origin;
4796 error = dsl_dataset_hold_obj(dp,
4797 dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
4798 if (error != 0)
4799 return (B_FALSE);
4800 if (dsl_dataset_phys(origin)->ds_creation_txg == earlier_txg &&
4801 origin->ds_dir == earlier->ds_dir) {
4802 dsl_dataset_rele(origin, FTAG);
4803 return (B_TRUE);
4804 }
4805 ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
4806 dsl_dataset_rele(origin, FTAG);
4807 return (ret);
4808 }
4809
4810 void
4811 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
4812 {
4813 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
4814 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
4815 }
4816
4817 boolean_t
4818 dsl_dataset_is_zapified(dsl_dataset_t *ds)
4819 {
4820 dmu_object_info_t doi;
4821
4822 dmu_object_info_from_db(ds->ds_dbuf, &doi);
4823 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
4824 }
4825
4826 boolean_t
4827 dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
4828 {
4829 return (dsl_dataset_is_zapified(ds) &&
4830 zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
4831 ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
4832 }
4833
4834 uint64_t
4835 dsl_dataset_get_remap_deadlist_object(dsl_dataset_t *ds)
4836 {
4837 uint64_t remap_deadlist_obj;
4838 int err;
4839
4840 if (!dsl_dataset_is_zapified(ds))
4841 return (0);
4842
4843 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
4844 DS_FIELD_REMAP_DEADLIST, sizeof (remap_deadlist_obj), 1,
4845 &remap_deadlist_obj);
4846
4847 if (err != 0) {
4848 VERIFY3S(err, ==, ENOENT);
4849 return (0);
4850 }
4851
4852 ASSERT(remap_deadlist_obj != 0);
4853 return (remap_deadlist_obj);
4854 }
4855
4856 boolean_t
4857 dsl_dataset_remap_deadlist_exists(dsl_dataset_t *ds)
4858 {
4859 EQUIV(dsl_deadlist_is_open(&ds->ds_remap_deadlist),
4860 dsl_dataset_get_remap_deadlist_object(ds) != 0);
4861 return (dsl_deadlist_is_open(&ds->ds_remap_deadlist));
4862 }
4863
4864 static void
4865 dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds, uint64_t obj,
4866 dmu_tx_t *tx)
4867 {
4868 ASSERT(obj != 0);
4869 dsl_dataset_zapify(ds, tx);
4870 VERIFY0(zap_add(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
4871 DS_FIELD_REMAP_DEADLIST, sizeof (obj), 1, &obj, tx));
4872 }
4873
4874 static void
4875 dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds, dmu_tx_t *tx)
4876 {
4877 VERIFY0(zap_remove(ds->ds_dir->dd_pool->dp_meta_objset,
4878 ds->ds_object, DS_FIELD_REMAP_DEADLIST, tx));
4879 }
4880
4881 void
4882 dsl_dataset_destroy_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
4883 {
4884 uint64_t remap_deadlist_object;
4885 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
4886
4887 ASSERT(dmu_tx_is_syncing(tx));
4888 ASSERT(dsl_dataset_remap_deadlist_exists(ds));
4889
4890 remap_deadlist_object = ds->ds_remap_deadlist.dl_object;
4891 dsl_deadlist_close(&ds->ds_remap_deadlist);
4892 dsl_deadlist_free(spa_meta_objset(spa), remap_deadlist_object, tx);
4893 dsl_dataset_unset_remap_deadlist_object(ds, tx);
4894 spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4895 }
4896
4897 void
4898 dsl_dataset_create_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
4899 {
4900 uint64_t remap_deadlist_obj;
4901 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
4902
4903 ASSERT(dmu_tx_is_syncing(tx));
4904 ASSERT(MUTEX_HELD(&ds->ds_remap_deadlist_lock));
4905 /*
4906 * Currently we only create remap deadlists when there are indirect
4907 * vdevs with referenced mappings.
4908 */
4909 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
4910
4911 remap_deadlist_obj = dsl_deadlist_clone(
4912 &ds->ds_deadlist, UINT64_MAX,
4913 dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
4914 dsl_dataset_set_remap_deadlist_object(ds,
4915 remap_deadlist_obj, tx);
4916 dsl_deadlist_open(&ds->ds_remap_deadlist, spa_meta_objset(spa),
4917 remap_deadlist_obj);
4918 spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4919 }
4920
4921 void
4922 dsl_dataset_activate_redaction(dsl_dataset_t *ds, uint64_t *redact_snaps,
4923 uint64_t num_redact_snaps, dmu_tx_t *tx)
4924 {
4925 uint64_t dsobj = ds->ds_object;
4926 struct feature_type_uint64_array_arg *ftuaa =
4927 kmem_zalloc(sizeof (*ftuaa), KM_SLEEP);
4928 ftuaa->length = (int64_t)num_redact_snaps;
4929 if (num_redact_snaps > 0) {
4930 ftuaa->array = kmem_alloc(num_redact_snaps * sizeof (uint64_t),
4931 KM_SLEEP);
4932 memcpy(ftuaa->array, redact_snaps, num_redact_snaps *
4933 sizeof (uint64_t));
4934 }
4935 dsl_dataset_activate_feature(dsobj, SPA_FEATURE_REDACTED_DATASETS,
4936 ftuaa, tx);
4937 ds->ds_feature[SPA_FEATURE_REDACTED_DATASETS] = ftuaa;
4938 }
4939
4940 /*
4941 * Find and return (in *oldest_dsobj) the oldest snapshot of the dsobj
4942 * dataset whose birth time is >= min_txg.
4943 */
4944 int
4945 dsl_dataset_oldest_snapshot(spa_t *spa, uint64_t head_ds, uint64_t min_txg,
4946 uint64_t *oldest_dsobj)
4947 {
4948 dsl_dataset_t *ds;
4949 dsl_pool_t *dp = spa->spa_dsl_pool;
4950
4951 int error = dsl_dataset_hold_obj(dp, head_ds, FTAG, &ds);
4952 if (error != 0)
4953 return (error);
4954
4955 uint64_t prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4956 uint64_t prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
4957
4958 while (prev_obj != 0 && min_txg < prev_obj_txg) {
4959 dsl_dataset_rele(ds, FTAG);
4960 if ((error = dsl_dataset_hold_obj(dp, prev_obj,
4961 FTAG, &ds)) != 0)
4962 return (error);
4963 prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
4964 prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4965 }
4966 *oldest_dsobj = ds->ds_object;
4967 dsl_dataset_rele(ds, FTAG);
4968 return (0);
4969 }
4970
4971 ZFS_MODULE_PARAM(zfs, zfs_, max_recordsize, UINT, ZMOD_RW,
4972 "Max allowed record size");
4973
4974 ZFS_MODULE_PARAM(zfs, zfs_, allow_redacted_dataset_mount, INT, ZMOD_RW,
4975 "Allow mounting of redacted datasets");
4976
4977 ZFS_MODULE_PARAM(zfs, zfs_, snapshot_history_enabled, INT, ZMOD_RW,
4978 "Include snapshot events in pool history/events");
4979
4980 EXPORT_SYMBOL(dsl_dataset_hold);
4981 EXPORT_SYMBOL(dsl_dataset_hold_flags);
4982 EXPORT_SYMBOL(dsl_dataset_hold_obj);
4983 EXPORT_SYMBOL(dsl_dataset_hold_obj_flags);
4984 EXPORT_SYMBOL(dsl_dataset_own);
4985 EXPORT_SYMBOL(dsl_dataset_own_obj);
4986 EXPORT_SYMBOL(dsl_dataset_name);
4987 EXPORT_SYMBOL(dsl_dataset_rele);
4988 EXPORT_SYMBOL(dsl_dataset_rele_flags);
4989 EXPORT_SYMBOL(dsl_dataset_disown);
4990 EXPORT_SYMBOL(dsl_dataset_tryown);
4991 EXPORT_SYMBOL(dsl_dataset_create_sync);
4992 EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
4993 EXPORT_SYMBOL(dsl_dataset_snapshot_check);
4994 EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
4995 EXPORT_SYMBOL(dsl_dataset_promote);
4996 EXPORT_SYMBOL(dsl_dataset_user_hold);
4997 EXPORT_SYMBOL(dsl_dataset_user_release);
4998 EXPORT_SYMBOL(dsl_dataset_get_holds);
4999 EXPORT_SYMBOL(dsl_dataset_get_blkptr);
5000 EXPORT_SYMBOL(dsl_dataset_get_spa);
5001 EXPORT_SYMBOL(dsl_dataset_modified_since_snap);
5002 EXPORT_SYMBOL(dsl_dataset_space_written);
5003 EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
5004 EXPORT_SYMBOL(dsl_dataset_sync);
5005 EXPORT_SYMBOL(dsl_dataset_block_born);
5006 EXPORT_SYMBOL(dsl_dataset_block_kill);
5007 EXPORT_SYMBOL(dsl_dataset_dirty);
5008 EXPORT_SYMBOL(dsl_dataset_stats);
5009 EXPORT_SYMBOL(dsl_dataset_fast_stat);
5010 EXPORT_SYMBOL(dsl_dataset_space);
5011 EXPORT_SYMBOL(dsl_dataset_fsid_guid);
5012 EXPORT_SYMBOL(dsl_dsobj_to_dsname);
5013 EXPORT_SYMBOL(dsl_dataset_check_quota);
5014 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
5015 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);