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