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