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