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