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