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