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