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