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