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