]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dsl_dataset.c
OpenZFS 7968 - multi-threaded spa_sync()
[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 void *ddra_owner;
2214 nvlist_t *ddra_result;
2215 } dsl_dataset_rollback_arg_t;
2216
2217 static int
2218 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
2219 {
2220 dsl_dataset_rollback_arg_t *ddra = arg;
2221 dsl_pool_t *dp = dmu_tx_pool(tx);
2222 dsl_dataset_t *ds;
2223 int64_t unused_refres_delta;
2224 int error;
2225 nvpair_t *pair;
2226 nvlist_t *proprequest, *bookmarks;
2227
2228 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
2229 if (error != 0)
2230 return (error);
2231
2232 /* must not be a snapshot */
2233 if (ds->ds_is_snapshot) {
2234 dsl_dataset_rele(ds, FTAG);
2235 return (SET_ERROR(EINVAL));
2236 }
2237
2238 /* must have a most recent snapshot */
2239 if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
2240 dsl_dataset_rele(ds, FTAG);
2241 return (SET_ERROR(EINVAL));
2242 }
2243
2244 /*
2245 * No rollback to a snapshot created in the current txg, because
2246 * the rollback may dirty the dataset and create blocks that are
2247 * not reachable from the rootbp while having a birth txg that
2248 * falls into the snapshot's range.
2249 */
2250 if (dmu_tx_is_syncing(tx) &&
2251 dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
2252 dsl_dataset_rele(ds, FTAG);
2253 return (SET_ERROR(EAGAIN));
2254 }
2255
2256 /* must not have any bookmarks after the most recent snapshot */
2257 proprequest = fnvlist_alloc();
2258 fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
2259 bookmarks = fnvlist_alloc();
2260 error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
2261 fnvlist_free(proprequest);
2262 if (error != 0)
2263 return (error);
2264 for (pair = nvlist_next_nvpair(bookmarks, NULL);
2265 pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
2266 nvlist_t *valuenv =
2267 fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
2268 zfs_prop_to_name(ZFS_PROP_CREATETXG));
2269 uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2270 if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
2271 fnvlist_free(bookmarks);
2272 dsl_dataset_rele(ds, FTAG);
2273 return (SET_ERROR(EEXIST));
2274 }
2275 }
2276 fnvlist_free(bookmarks);
2277
2278 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
2279 if (error != 0) {
2280 dsl_dataset_rele(ds, FTAG);
2281 return (error);
2282 }
2283
2284 /*
2285 * Check if the snap we are rolling back to uses more than
2286 * the refquota.
2287 */
2288 if (ds->ds_quota != 0 &&
2289 dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
2290 dsl_dataset_rele(ds, FTAG);
2291 return (SET_ERROR(EDQUOT));
2292 }
2293
2294 /*
2295 * When we do the clone swap, we will temporarily use more space
2296 * due to the refreservation (the head will no longer have any
2297 * unique space, so the entire amount of the refreservation will need
2298 * to be free). We will immediately destroy the clone, freeing
2299 * this space, but the freeing happens over many txg's.
2300 */
2301 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2302 dsl_dataset_phys(ds)->ds_unique_bytes);
2303
2304 if (unused_refres_delta > 0 &&
2305 unused_refres_delta >
2306 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
2307 dsl_dataset_rele(ds, FTAG);
2308 return (SET_ERROR(ENOSPC));
2309 }
2310
2311 dsl_dataset_rele(ds, FTAG);
2312 return (0);
2313 }
2314
2315 static void
2316 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
2317 {
2318 dsl_dataset_rollback_arg_t *ddra = arg;
2319 dsl_pool_t *dp = dmu_tx_pool(tx);
2320 dsl_dataset_t *ds, *clone;
2321 uint64_t cloneobj;
2322 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
2323
2324 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
2325
2326 dsl_dataset_name(ds->ds_prev, namebuf);
2327 fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2328
2329 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
2330 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
2331
2332 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
2333
2334 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
2335 dsl_dataset_zero_zil(ds, tx);
2336
2337 dsl_destroy_head_sync_impl(clone, tx);
2338
2339 dsl_dataset_rele(clone, FTAG);
2340 dsl_dataset_rele(ds, FTAG);
2341 }
2342
2343 /*
2344 * Rolls back the given filesystem or volume to the most recent snapshot.
2345 * The name of the most recent snapshot will be returned under key "target"
2346 * in the result nvlist.
2347 *
2348 * If owner != NULL:
2349 * - The existing dataset MUST be owned by the specified owner at entry
2350 * - Upon return, dataset will still be held by the same owner, whether we
2351 * succeed or not.
2352 *
2353 * This mode is required any time the existing filesystem is mounted. See
2354 * notes above zfs_suspend_fs() for further details.
2355 */
2356 int
2357 dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
2358 {
2359 dsl_dataset_rollback_arg_t ddra;
2360
2361 ddra.ddra_fsname = fsname;
2362 ddra.ddra_owner = owner;
2363 ddra.ddra_result = result;
2364
2365 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
2366 dsl_dataset_rollback_sync, &ddra,
2367 1, ZFS_SPACE_CHECK_RESERVED));
2368 }
2369
2370 struct promotenode {
2371 list_node_t link;
2372 dsl_dataset_t *ds;
2373 };
2374
2375 typedef struct dsl_dataset_promote_arg {
2376 const char *ddpa_clonename;
2377 dsl_dataset_t *ddpa_clone;
2378 list_t shared_snaps, origin_snaps, clone_snaps;
2379 dsl_dataset_t *origin_origin; /* origin of the origin */
2380 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2381 char *err_ds;
2382 cred_t *cr;
2383 } dsl_dataset_promote_arg_t;
2384
2385 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2386 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
2387 void *tag);
2388 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
2389
2390 static int
2391 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
2392 {
2393 dsl_dataset_promote_arg_t *ddpa = arg;
2394 dsl_pool_t *dp = dmu_tx_pool(tx);
2395 dsl_dataset_t *hds;
2396 struct promotenode *snap;
2397 dsl_dataset_t *origin_ds;
2398 int err;
2399 uint64_t unused;
2400 uint64_t ss_mv_cnt;
2401 size_t max_snap_len;
2402
2403 err = promote_hold(ddpa, dp, FTAG);
2404 if (err != 0)
2405 return (err);
2406
2407 hds = ddpa->ddpa_clone;
2408 max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
2409
2410 if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
2411 promote_rele(ddpa, FTAG);
2412 return (SET_ERROR(EXDEV));
2413 }
2414
2415 /*
2416 * Compute and check the amount of space to transfer. Since this is
2417 * so expensive, don't do the preliminary check.
2418 */
2419 if (!dmu_tx_is_syncing(tx)) {
2420 promote_rele(ddpa, FTAG);
2421 return (0);
2422 }
2423
2424 snap = list_head(&ddpa->shared_snaps);
2425 if (snap == NULL) {
2426 err = SET_ERROR(ENOENT);
2427 goto out;
2428 }
2429 origin_ds = snap->ds;
2430
2431 /* compute origin's new unique space */
2432 snap = list_tail(&ddpa->clone_snaps);
2433 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2434 origin_ds->ds_object);
2435 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2436 dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
2437 &ddpa->unique, &unused, &unused);
2438
2439 /*
2440 * Walk the snapshots that we are moving
2441 *
2442 * Compute space to transfer. Consider the incremental changes
2443 * to used by each snapshot:
2444 * (my used) = (prev's used) + (blocks born) - (blocks killed)
2445 * So each snapshot gave birth to:
2446 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2447 * So a sequence would look like:
2448 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2449 * Which simplifies to:
2450 * uN + kN + kN-1 + ... + k1 + k0
2451 * Note however, if we stop before we reach the ORIGIN we get:
2452 * uN + kN + kN-1 + ... + kM - uM-1
2453 */
2454 ss_mv_cnt = 0;
2455 ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2456 ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2457 ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
2458 for (snap = list_head(&ddpa->shared_snaps); snap;
2459 snap = list_next(&ddpa->shared_snaps, snap)) {
2460 uint64_t val, dlused, dlcomp, dluncomp;
2461 dsl_dataset_t *ds = snap->ds;
2462
2463 ss_mv_cnt++;
2464
2465 /*
2466 * If there are long holds, we won't be able to evict
2467 * the objset.
2468 */
2469 if (dsl_dataset_long_held(ds)) {
2470 err = SET_ERROR(EBUSY);
2471 goto out;
2472 }
2473
2474 /* Check that the snapshot name does not conflict */
2475 VERIFY0(dsl_dataset_get_snapname(ds));
2476 if (strlen(ds->ds_snapname) >= max_snap_len) {
2477 err = SET_ERROR(ENAMETOOLONG);
2478 goto out;
2479 }
2480 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2481 if (err == 0) {
2482 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2483 err = SET_ERROR(EEXIST);
2484 goto out;
2485 }
2486 if (err != ENOENT)
2487 goto out;
2488
2489 /* The very first snapshot does not have a deadlist */
2490 if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
2491 continue;
2492
2493 dsl_deadlist_space(&ds->ds_deadlist,
2494 &dlused, &dlcomp, &dluncomp);
2495 ddpa->used += dlused;
2496 ddpa->comp += dlcomp;
2497 ddpa->uncomp += dluncomp;
2498 }
2499
2500 /*
2501 * If we are a clone of a clone then we never reached ORIGIN,
2502 * so we need to subtract out the clone origin's used space.
2503 */
2504 if (ddpa->origin_origin) {
2505 ddpa->used -=
2506 dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2507 ddpa->comp -=
2508 dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
2509 ddpa->uncomp -=
2510 dsl_dataset_phys(ddpa->origin_origin)->
2511 ds_uncompressed_bytes;
2512 }
2513
2514 /* Check that there is enough space and limit headroom here */
2515 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2516 0, ss_mv_cnt, ddpa->used, ddpa->cr);
2517 if (err != 0)
2518 goto out;
2519
2520 /*
2521 * Compute the amounts of space that will be used by snapshots
2522 * after the promotion (for both origin and clone). For each,
2523 * it is the amount of space that will be on all of their
2524 * deadlists (that was not born before their new origin).
2525 */
2526 if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2527 uint64_t space;
2528
2529 /*
2530 * Note, typically this will not be a clone of a clone,
2531 * so dd_origin_txg will be < TXG_INITIAL, so
2532 * these snaplist_space() -> dsl_deadlist_space_range()
2533 * calls will be fast because they do not have to
2534 * iterate over all bps.
2535 */
2536 snap = list_head(&ddpa->origin_snaps);
2537 if (snap == NULL) {
2538 err = SET_ERROR(ENOENT);
2539 goto out;
2540 }
2541 err = snaplist_space(&ddpa->shared_snaps,
2542 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2543 if (err != 0)
2544 goto out;
2545
2546 err = snaplist_space(&ddpa->clone_snaps,
2547 snap->ds->ds_dir->dd_origin_txg, &space);
2548 if (err != 0)
2549 goto out;
2550 ddpa->cloneusedsnap += space;
2551 }
2552 if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2553 DD_FLAG_USED_BREAKDOWN) {
2554 err = snaplist_space(&ddpa->origin_snaps,
2555 dsl_dataset_phys(origin_ds)->ds_creation_txg,
2556 &ddpa->originusedsnap);
2557 if (err != 0)
2558 goto out;
2559 }
2560
2561 out:
2562 promote_rele(ddpa, FTAG);
2563 return (err);
2564 }
2565
2566 static void
2567 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2568 {
2569 dsl_dataset_promote_arg_t *ddpa = arg;
2570 dsl_pool_t *dp = dmu_tx_pool(tx);
2571 dsl_dataset_t *hds;
2572 struct promotenode *snap;
2573 dsl_dataset_t *origin_ds;
2574 dsl_dataset_t *origin_head;
2575 dsl_dir_t *dd;
2576 dsl_dir_t *odd = NULL;
2577 uint64_t oldnext_obj;
2578 int64_t delta;
2579
2580 VERIFY0(promote_hold(ddpa, dp, FTAG));
2581 hds = ddpa->ddpa_clone;
2582
2583 ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
2584
2585 snap = list_head(&ddpa->shared_snaps);
2586 origin_ds = snap->ds;
2587 dd = hds->ds_dir;
2588
2589 snap = list_head(&ddpa->origin_snaps);
2590 origin_head = snap->ds;
2591
2592 /*
2593 * We need to explicitly open odd, since origin_ds's dd will be
2594 * changing.
2595 */
2596 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2597 NULL, FTAG, &odd));
2598
2599 /* change origin's next snap */
2600 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2601 oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
2602 snap = list_tail(&ddpa->clone_snaps);
2603 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2604 origin_ds->ds_object);
2605 dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2606
2607 /* change the origin's next clone */
2608 if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
2609 dsl_dataset_remove_from_next_clones(origin_ds,
2610 snap->ds->ds_object, tx);
2611 VERIFY0(zap_add_int(dp->dp_meta_objset,
2612 dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2613 oldnext_obj, tx));
2614 }
2615
2616 /* change origin */
2617 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2618 ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2619 dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
2620 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2621 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2622 dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
2623 origin_head->ds_dir->dd_origin_txg =
2624 dsl_dataset_phys(origin_ds)->ds_creation_txg;
2625
2626 /* change dd_clone entries */
2627 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2628 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2629 dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
2630 VERIFY0(zap_add_int(dp->dp_meta_objset,
2631 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2632 hds->ds_object, tx));
2633
2634 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2635 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2636 origin_head->ds_object, tx));
2637 if (dsl_dir_phys(dd)->dd_clones == 0) {
2638 dsl_dir_phys(dd)->dd_clones =
2639 zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2640 DMU_OT_NONE, 0, tx);
2641 }
2642 VERIFY0(zap_add_int(dp->dp_meta_objset,
2643 dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2644 }
2645
2646 /* move snapshots to this dir */
2647 for (snap = list_head(&ddpa->shared_snaps); snap;
2648 snap = list_next(&ddpa->shared_snaps, snap)) {
2649 dsl_dataset_t *ds = snap->ds;
2650
2651 /*
2652 * Property callbacks are registered to a particular
2653 * dsl_dir. Since ours is changing, evict the objset
2654 * so that they will be unregistered from the old dsl_dir.
2655 */
2656 if (ds->ds_objset) {
2657 dmu_objset_evict(ds->ds_objset);
2658 ds->ds_objset = NULL;
2659 }
2660
2661 /* move snap name entry */
2662 VERIFY0(dsl_dataset_get_snapname(ds));
2663 VERIFY0(dsl_dataset_snap_remove(origin_head,
2664 ds->ds_snapname, tx, B_TRUE));
2665 VERIFY0(zap_add(dp->dp_meta_objset,
2666 dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
2667 8, 1, &ds->ds_object, tx));
2668 dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2669 DD_FIELD_SNAPSHOT_COUNT, tx);
2670
2671 /* change containing dsl_dir */
2672 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2673 ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2674 dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
2675 ASSERT3P(ds->ds_dir, ==, odd);
2676 dsl_dir_rele(ds->ds_dir, ds);
2677 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2678 NULL, ds, &ds->ds_dir));
2679
2680 /* move any clone references */
2681 if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2682 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2683 zap_cursor_t zc;
2684 zap_attribute_t za;
2685
2686 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2687 dsl_dataset_phys(ds)->ds_next_clones_obj);
2688 zap_cursor_retrieve(&zc, &za) == 0;
2689 zap_cursor_advance(&zc)) {
2690 dsl_dataset_t *cnds;
2691 uint64_t o;
2692
2693 if (za.za_first_integer == oldnext_obj) {
2694 /*
2695 * We've already moved the
2696 * origin's reference.
2697 */
2698 continue;
2699 }
2700
2701 VERIFY0(dsl_dataset_hold_obj(dp,
2702 za.za_first_integer, FTAG, &cnds));
2703 o = dsl_dir_phys(cnds->ds_dir)->
2704 dd_head_dataset_obj;
2705
2706 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2707 dsl_dir_phys(odd)->dd_clones, o, tx));
2708 VERIFY0(zap_add_int(dp->dp_meta_objset,
2709 dsl_dir_phys(dd)->dd_clones, o, tx));
2710 dsl_dataset_rele(cnds, FTAG);
2711 }
2712 zap_cursor_fini(&zc);
2713 }
2714
2715 ASSERT(!dsl_prop_hascb(ds));
2716 }
2717
2718 /*
2719 * Change space accounting.
2720 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2721 * both be valid, or both be 0 (resulting in delta == 0). This
2722 * is true for each of {clone,origin} independently.
2723 */
2724
2725 delta = ddpa->cloneusedsnap -
2726 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
2727 ASSERT3S(delta, >=, 0);
2728 ASSERT3U(ddpa->used, >=, delta);
2729 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2730 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2731 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2732
2733 delta = ddpa->originusedsnap -
2734 dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
2735 ASSERT3S(delta, <=, 0);
2736 ASSERT3U(ddpa->used, >=, -delta);
2737 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2738 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2739 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2740
2741 dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
2742
2743 /* log history record */
2744 spa_history_log_internal_ds(hds, "promote", tx, "");
2745
2746 dsl_dir_rele(odd, FTAG);
2747 promote_rele(ddpa, FTAG);
2748 }
2749
2750 /*
2751 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2752 * (exclusive) and last_obj (inclusive). The list will be in reverse
2753 * order (last_obj will be the list_head()). If first_obj == 0, do all
2754 * snapshots back to this dataset's origin.
2755 */
2756 static int
2757 snaplist_make(dsl_pool_t *dp,
2758 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2759 {
2760 uint64_t obj = last_obj;
2761
2762 list_create(l, sizeof (struct promotenode),
2763 offsetof(struct promotenode, link));
2764
2765 while (obj != first_obj) {
2766 dsl_dataset_t *ds;
2767 struct promotenode *snap;
2768 int err;
2769
2770 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2771 ASSERT(err != ENOENT);
2772 if (err != 0)
2773 return (err);
2774
2775 if (first_obj == 0)
2776 first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
2777
2778 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2779 snap->ds = ds;
2780 list_insert_tail(l, snap);
2781 obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
2782 }
2783
2784 return (0);
2785 }
2786
2787 static int
2788 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2789 {
2790 struct promotenode *snap;
2791
2792 *spacep = 0;
2793 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2794 uint64_t used, comp, uncomp;
2795 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2796 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2797 *spacep += used;
2798 }
2799 return (0);
2800 }
2801
2802 static void
2803 snaplist_destroy(list_t *l, void *tag)
2804 {
2805 struct promotenode *snap;
2806
2807 if (l == NULL || !list_link_active(&l->list_head))
2808 return;
2809
2810 while ((snap = list_tail(l)) != NULL) {
2811 list_remove(l, snap);
2812 dsl_dataset_rele(snap->ds, tag);
2813 kmem_free(snap, sizeof (*snap));
2814 }
2815 list_destroy(l);
2816 }
2817
2818 static int
2819 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2820 {
2821 int error;
2822 dsl_dir_t *dd;
2823 struct promotenode *snap;
2824
2825 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2826 &ddpa->ddpa_clone);
2827 if (error != 0)
2828 return (error);
2829 dd = ddpa->ddpa_clone->ds_dir;
2830
2831 if (ddpa->ddpa_clone->ds_is_snapshot ||
2832 !dsl_dir_is_clone(dd)) {
2833 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2834 return (SET_ERROR(EINVAL));
2835 }
2836
2837 error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
2838 &ddpa->shared_snaps, tag);
2839 if (error != 0)
2840 goto out;
2841
2842 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2843 &ddpa->clone_snaps, tag);
2844 if (error != 0)
2845 goto out;
2846
2847 snap = list_head(&ddpa->shared_snaps);
2848 ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2849 error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2850 dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
2851 &ddpa->origin_snaps, tag);
2852 if (error != 0)
2853 goto out;
2854
2855 if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
2856 error = dsl_dataset_hold_obj(dp,
2857 dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
2858 tag, &ddpa->origin_origin);
2859 if (error != 0)
2860 goto out;
2861 }
2862 out:
2863 if (error != 0)
2864 promote_rele(ddpa, tag);
2865 return (error);
2866 }
2867
2868 static void
2869 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2870 {
2871 snaplist_destroy(&ddpa->shared_snaps, tag);
2872 snaplist_destroy(&ddpa->clone_snaps, tag);
2873 snaplist_destroy(&ddpa->origin_snaps, tag);
2874 if (ddpa->origin_origin != NULL)
2875 dsl_dataset_rele(ddpa->origin_origin, tag);
2876 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2877 }
2878
2879 /*
2880 * Promote a clone.
2881 *
2882 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2883 * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
2884 */
2885 int
2886 dsl_dataset_promote(const char *name, char *conflsnap)
2887 {
2888 dsl_dataset_promote_arg_t ddpa = { 0 };
2889 uint64_t numsnaps;
2890 int error;
2891 objset_t *os;
2892
2893 /*
2894 * We will modify space proportional to the number of
2895 * snapshots. Compute numsnaps.
2896 */
2897 error = dmu_objset_hold(name, FTAG, &os);
2898 if (error != 0)
2899 return (error);
2900 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2901 dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2902 &numsnaps);
2903 dmu_objset_rele(os, FTAG);
2904 if (error != 0)
2905 return (error);
2906
2907 ddpa.ddpa_clonename = name;
2908 ddpa.err_ds = conflsnap;
2909 ddpa.cr = CRED();
2910
2911 return (dsl_sync_task(name, dsl_dataset_promote_check,
2912 dsl_dataset_promote_sync, &ddpa,
2913 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2914 }
2915
2916 int
2917 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2918 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2919 {
2920 /*
2921 * "slack" factor for received datasets with refquota set on them.
2922 * See the bottom of this function for details on its use.
2923 */
2924 uint64_t refquota_slack = (uint64_t)DMU_MAX_ACCESS *
2925 spa_asize_inflation;
2926 int64_t unused_refres_delta;
2927
2928 /* they should both be heads */
2929 if (clone->ds_is_snapshot ||
2930 origin_head->ds_is_snapshot)
2931 return (SET_ERROR(EINVAL));
2932
2933 /* if we are not forcing, the branch point should be just before them */
2934 if (!force && clone->ds_prev != origin_head->ds_prev)
2935 return (SET_ERROR(EINVAL));
2936
2937 /* clone should be the clone (unless they are unrelated) */
2938 if (clone->ds_prev != NULL &&
2939 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2940 origin_head->ds_dir != clone->ds_prev->ds_dir)
2941 return (SET_ERROR(EINVAL));
2942
2943 /* the clone should be a child of the origin */
2944 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2945 return (SET_ERROR(EINVAL));
2946
2947 /* origin_head shouldn't be modified unless 'force' */
2948 if (!force &&
2949 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2950 return (SET_ERROR(ETXTBSY));
2951
2952 /* origin_head should have no long holds (e.g. is not mounted) */
2953 if (dsl_dataset_handoff_check(origin_head, owner, tx))
2954 return (SET_ERROR(EBUSY));
2955
2956 /* check amount of any unconsumed refreservation */
2957 unused_refres_delta =
2958 (int64_t)MIN(origin_head->ds_reserved,
2959 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
2960 (int64_t)MIN(origin_head->ds_reserved,
2961 dsl_dataset_phys(clone)->ds_unique_bytes);
2962
2963 if (unused_refres_delta > 0 &&
2964 unused_refres_delta >
2965 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2966 return (SET_ERROR(ENOSPC));
2967
2968 /*
2969 * The clone can't be too much over the head's refquota.
2970 *
2971 * To ensure that the entire refquota can be used, we allow one
2972 * transaction to exceed the the refquota. Therefore, this check
2973 * needs to also allow for the space referenced to be more than the
2974 * refquota. The maximum amount of space that one transaction can use
2975 * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this
2976 * overage ensures that we are able to receive a filesystem that
2977 * exceeds the refquota on the source system.
2978 *
2979 * So that overage is the refquota_slack we use below.
2980 */
2981 if (origin_head->ds_quota != 0 &&
2982 dsl_dataset_phys(clone)->ds_referenced_bytes >
2983 origin_head->ds_quota + refquota_slack)
2984 return (SET_ERROR(EDQUOT));
2985
2986 return (0);
2987 }
2988
2989 void
2990 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2991 dsl_dataset_t *origin_head, dmu_tx_t *tx)
2992 {
2993 dsl_pool_t *dp = dmu_tx_pool(tx);
2994 int64_t unused_refres_delta;
2995 blkptr_t tmp;
2996
2997 ASSERT(clone->ds_reserved == 0);
2998 /*
2999 * NOTE: On DEBUG kernels there could be a race between this and
3000 * the check function if spa_asize_inflation is adjusted...
3001 */
3002 ASSERT(origin_head->ds_quota == 0 ||
3003 dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
3004 DMU_MAX_ACCESS * spa_asize_inflation);
3005 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
3006
3007 /*
3008 * Swap per-dataset feature flags.
3009 */
3010 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3011 boolean_t clone_inuse;
3012 boolean_t origin_head_inuse;
3013
3014 if (!(spa_feature_table[f].fi_flags &
3015 ZFEATURE_FLAG_PER_DATASET)) {
3016 ASSERT(!clone->ds_feature_inuse[f]);
3017 ASSERT(!origin_head->ds_feature_inuse[f]);
3018 continue;
3019 }
3020
3021 clone_inuse = clone->ds_feature_inuse[f];
3022 origin_head_inuse = origin_head->ds_feature_inuse[f];
3023
3024 if (clone_inuse) {
3025 dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
3026 clone->ds_feature_inuse[f] = B_FALSE;
3027 }
3028 if (origin_head_inuse) {
3029 dsl_dataset_deactivate_feature(origin_head->ds_object,
3030 f, tx);
3031 origin_head->ds_feature_inuse[f] = B_FALSE;
3032 }
3033 if (clone_inuse) {
3034 dsl_dataset_activate_feature(origin_head->ds_object,
3035 f, tx);
3036 origin_head->ds_feature_inuse[f] = B_TRUE;
3037 }
3038 if (origin_head_inuse) {
3039 dsl_dataset_activate_feature(clone->ds_object, f, tx);
3040 clone->ds_feature_inuse[f] = B_TRUE;
3041 }
3042 }
3043
3044 dmu_buf_will_dirty(clone->ds_dbuf, tx);
3045 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3046
3047 if (clone->ds_objset != NULL) {
3048 dmu_objset_evict(clone->ds_objset);
3049 clone->ds_objset = NULL;
3050 }
3051
3052 if (origin_head->ds_objset != NULL) {
3053 dmu_objset_evict(origin_head->ds_objset);
3054 origin_head->ds_objset = NULL;
3055 }
3056
3057 unused_refres_delta =
3058 (int64_t)MIN(origin_head->ds_reserved,
3059 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3060 (int64_t)MIN(origin_head->ds_reserved,
3061 dsl_dataset_phys(clone)->ds_unique_bytes);
3062
3063 /*
3064 * Reset origin's unique bytes, if it exists.
3065 */
3066 if (clone->ds_prev) {
3067 dsl_dataset_t *origin = clone->ds_prev;
3068 uint64_t comp, uncomp;
3069
3070 dmu_buf_will_dirty(origin->ds_dbuf, tx);
3071 dsl_deadlist_space_range(&clone->ds_deadlist,
3072 dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
3073 &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
3074 }
3075
3076 /* swap blkptrs */
3077 {
3078 rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
3079 rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
3080 tmp = dsl_dataset_phys(origin_head)->ds_bp;
3081 dsl_dataset_phys(origin_head)->ds_bp =
3082 dsl_dataset_phys(clone)->ds_bp;
3083 dsl_dataset_phys(clone)->ds_bp = tmp;
3084 rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
3085 rrw_exit(&clone->ds_bp_rwlock, FTAG);
3086 }
3087
3088 /* set dd_*_bytes */
3089 {
3090 int64_t dused, dcomp, duncomp;
3091 uint64_t cdl_used, cdl_comp, cdl_uncomp;
3092 uint64_t odl_used, odl_comp, odl_uncomp;
3093
3094 ASSERT3U(dsl_dir_phys(clone->ds_dir)->
3095 dd_used_breakdown[DD_USED_SNAP], ==, 0);
3096
3097 dsl_deadlist_space(&clone->ds_deadlist,
3098 &cdl_used, &cdl_comp, &cdl_uncomp);
3099 dsl_deadlist_space(&origin_head->ds_deadlist,
3100 &odl_used, &odl_comp, &odl_uncomp);
3101
3102 dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
3103 cdl_used -
3104 (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
3105 odl_used);
3106 dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
3107 cdl_comp -
3108 (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
3109 odl_comp);
3110 duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
3111 cdl_uncomp -
3112 (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
3113 odl_uncomp);
3114
3115 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
3116 dused, dcomp, duncomp, tx);
3117 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
3118 -dused, -dcomp, -duncomp, tx);
3119
3120 /*
3121 * The difference in the space used by snapshots is the
3122 * difference in snapshot space due to the head's
3123 * deadlist (since that's the only thing that's
3124 * changing that affects the snapused).
3125 */
3126 dsl_deadlist_space_range(&clone->ds_deadlist,
3127 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3128 &cdl_used, &cdl_comp, &cdl_uncomp);
3129 dsl_deadlist_space_range(&origin_head->ds_deadlist,
3130 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3131 &odl_used, &odl_comp, &odl_uncomp);
3132 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
3133 DD_USED_HEAD, DD_USED_SNAP, tx);
3134 }
3135
3136 /* swap ds_*_bytes */
3137 SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
3138 dsl_dataset_phys(clone)->ds_referenced_bytes);
3139 SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
3140 dsl_dataset_phys(clone)->ds_compressed_bytes);
3141 SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
3142 dsl_dataset_phys(clone)->ds_uncompressed_bytes);
3143 SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
3144 dsl_dataset_phys(clone)->ds_unique_bytes);
3145
3146 /* apply any parent delta for change in unconsumed refreservation */
3147 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
3148 unused_refres_delta, 0, 0, tx);
3149
3150 /*
3151 * Swap deadlists.
3152 */
3153 dsl_deadlist_close(&clone->ds_deadlist);
3154 dsl_deadlist_close(&origin_head->ds_deadlist);
3155 SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
3156 dsl_dataset_phys(clone)->ds_deadlist_obj);
3157 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
3158 dsl_dataset_phys(clone)->ds_deadlist_obj);
3159 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
3160 dsl_dataset_phys(origin_head)->ds_deadlist_obj);
3161
3162 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
3163
3164 spa_history_log_internal_ds(clone, "clone swap", tx,
3165 "parent=%s", origin_head->ds_dir->dd_myname);
3166 }
3167
3168 /*
3169 * Given a pool name and a dataset object number in that pool,
3170 * return the name of that dataset.
3171 */
3172 int
3173 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3174 {
3175 dsl_pool_t *dp;
3176 dsl_dataset_t *ds;
3177 int error;
3178
3179 error = dsl_pool_hold(pname, FTAG, &dp);
3180 if (error != 0)
3181 return (error);
3182
3183 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
3184 if (error == 0) {
3185 dsl_dataset_name(ds, buf);
3186 dsl_dataset_rele(ds, FTAG);
3187 }
3188 dsl_pool_rele(dp, FTAG);
3189
3190 return (error);
3191 }
3192
3193 int
3194 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3195 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3196 {
3197 int error = 0;
3198
3199 ASSERT3S(asize, >, 0);
3200
3201 /*
3202 * *ref_rsrv is the portion of asize that will come from any
3203 * unconsumed refreservation space.
3204 */
3205 *ref_rsrv = 0;
3206
3207 mutex_enter(&ds->ds_lock);
3208 /*
3209 * Make a space adjustment for reserved bytes.
3210 */
3211 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
3212 ASSERT3U(*used, >=,
3213 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3214 *used -=
3215 (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3216 *ref_rsrv =
3217 asize - MIN(asize, parent_delta(ds, asize + inflight));
3218 }
3219
3220 if (!check_quota || ds->ds_quota == 0) {
3221 mutex_exit(&ds->ds_lock);
3222 return (0);
3223 }
3224 /*
3225 * If they are requesting more space, and our current estimate
3226 * is over quota, they get to try again unless the actual
3227 * on-disk is over quota and there are no pending changes (which
3228 * may free up space for us).
3229 */
3230 if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
3231 ds->ds_quota) {
3232 if (inflight > 0 ||
3233 dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3234 error = SET_ERROR(ERESTART);
3235 else
3236 error = SET_ERROR(EDQUOT);
3237 }
3238 mutex_exit(&ds->ds_lock);
3239
3240 return (error);
3241 }
3242
3243 typedef struct dsl_dataset_set_qr_arg {
3244 const char *ddsqra_name;
3245 zprop_source_t ddsqra_source;
3246 uint64_t ddsqra_value;
3247 } dsl_dataset_set_qr_arg_t;
3248
3249
3250 /* ARGSUSED */
3251 static int
3252 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3253 {
3254 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3255 dsl_pool_t *dp = dmu_tx_pool(tx);
3256 dsl_dataset_t *ds;
3257 int error;
3258 uint64_t newval;
3259
3260 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3261 return (SET_ERROR(ENOTSUP));
3262
3263 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3264 if (error != 0)
3265 return (error);
3266
3267 if (ds->ds_is_snapshot) {
3268 dsl_dataset_rele(ds, FTAG);
3269 return (SET_ERROR(EINVAL));
3270 }
3271
3272 error = dsl_prop_predict(ds->ds_dir,
3273 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3274 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3275 if (error != 0) {
3276 dsl_dataset_rele(ds, FTAG);
3277 return (error);
3278 }
3279
3280 if (newval == 0) {
3281 dsl_dataset_rele(ds, FTAG);
3282 return (0);
3283 }
3284
3285 if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
3286 newval < ds->ds_reserved) {
3287 dsl_dataset_rele(ds, FTAG);
3288 return (SET_ERROR(ENOSPC));
3289 }
3290
3291 dsl_dataset_rele(ds, FTAG);
3292 return (0);
3293 }
3294
3295 static void
3296 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3297 {
3298 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3299 dsl_pool_t *dp = dmu_tx_pool(tx);
3300 dsl_dataset_t *ds = NULL;
3301 uint64_t newval;
3302
3303 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3304
3305 dsl_prop_set_sync_impl(ds,
3306 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3307 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
3308 &ddsqra->ddsqra_value, tx);
3309
3310 VERIFY0(dsl_prop_get_int_ds(ds,
3311 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
3312
3313 if (ds->ds_quota != newval) {
3314 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3315 ds->ds_quota = newval;
3316 }
3317 dsl_dataset_rele(ds, FTAG);
3318 }
3319
3320 int
3321 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
3322 uint64_t refquota)
3323 {
3324 dsl_dataset_set_qr_arg_t ddsqra;
3325
3326 ddsqra.ddsqra_name = dsname;
3327 ddsqra.ddsqra_source = source;
3328 ddsqra.ddsqra_value = refquota;
3329
3330 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
3331 dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3332 }
3333
3334 static int
3335 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3336 {
3337 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3338 dsl_pool_t *dp = dmu_tx_pool(tx);
3339 dsl_dataset_t *ds;
3340 int error;
3341 uint64_t newval, unique;
3342
3343 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3344 return (SET_ERROR(ENOTSUP));
3345
3346 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3347 if (error != 0)
3348 return (error);
3349
3350 if (ds->ds_is_snapshot) {
3351 dsl_dataset_rele(ds, FTAG);
3352 return (SET_ERROR(EINVAL));
3353 }
3354
3355 error = dsl_prop_predict(ds->ds_dir,
3356 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3357 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3358 if (error != 0) {
3359 dsl_dataset_rele(ds, FTAG);
3360 return (error);
3361 }
3362
3363 /*
3364 * If we are doing the preliminary check in open context, the
3365 * space estimates may be inaccurate.
3366 */
3367 if (!dmu_tx_is_syncing(tx)) {
3368 dsl_dataset_rele(ds, FTAG);
3369 return (0);
3370 }
3371
3372 mutex_enter(&ds->ds_lock);
3373 if (!DS_UNIQUE_IS_ACCURATE(ds))
3374 dsl_dataset_recalc_head_uniq(ds);
3375 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3376 mutex_exit(&ds->ds_lock);
3377
3378 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
3379 uint64_t delta = MAX(unique, newval) -
3380 MAX(unique, ds->ds_reserved);
3381
3382 if (delta >
3383 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
3384 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
3385 dsl_dataset_rele(ds, FTAG);
3386 return (SET_ERROR(ENOSPC));
3387 }
3388 }
3389
3390 dsl_dataset_rele(ds, FTAG);
3391 return (0);
3392 }
3393
3394 void
3395 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
3396 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3397 {
3398 uint64_t newval;
3399 uint64_t unique;
3400 int64_t delta;
3401
3402 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3403 source, sizeof (value), 1, &value, tx);
3404
3405 VERIFY0(dsl_prop_get_int_ds(ds,
3406 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3407
3408 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3409 mutex_enter(&ds->ds_dir->dd_lock);
3410 mutex_enter(&ds->ds_lock);
3411 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3412 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3413 delta = MAX(0, (int64_t)(newval - unique)) -
3414 MAX(0, (int64_t)(ds->ds_reserved - unique));
3415 ds->ds_reserved = newval;
3416 mutex_exit(&ds->ds_lock);
3417
3418 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3419 mutex_exit(&ds->ds_dir->dd_lock);
3420 }
3421
3422 static void
3423 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3424 {
3425 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3426 dsl_pool_t *dp = dmu_tx_pool(tx);
3427 dsl_dataset_t *ds = NULL;
3428
3429 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3430 dsl_dataset_set_refreservation_sync_impl(ds,
3431 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3432 dsl_dataset_rele(ds, FTAG);
3433 }
3434
3435 int
3436 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
3437 uint64_t refreservation)
3438 {
3439 dsl_dataset_set_qr_arg_t ddsqra;
3440
3441 ddsqra.ddsqra_name = dsname;
3442 ddsqra.ddsqra_source = source;
3443 ddsqra.ddsqra_value = refreservation;
3444
3445 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
3446 dsl_dataset_set_refreservation_sync, &ddsqra,
3447 0, ZFS_SPACE_CHECK_NONE));
3448 }
3449
3450 /*
3451 * Return (in *usedp) the amount of space written in new that is not
3452 * present in oldsnap. New may be a snapshot or the head. Old must be
3453 * a snapshot before new, in new's filesystem (or its origin). If not then
3454 * fail and return EINVAL.
3455 *
3456 * The written space is calculated by considering two components: First, we
3457 * ignore any freed space, and calculate the written as new's used space
3458 * minus old's used space. Next, we add in the amount of space that was freed
3459 * between the two snapshots, thus reducing new's used space relative to old's.
3460 * Specifically, this is the space that was born before old->ds_creation_txg,
3461 * and freed before new (ie. on new's deadlist or a previous deadlist).
3462 *
3463 * space freed [---------------------]
3464 * snapshots ---O-------O--------O-------O------
3465 * oldsnap new
3466 */
3467 int
3468 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
3469 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3470 {
3471 int err = 0;
3472 uint64_t snapobj;
3473 dsl_pool_t *dp = new->ds_dir->dd_pool;
3474
3475 ASSERT(dsl_pool_config_held(dp));
3476
3477 *usedp = 0;
3478 *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3479 *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
3480
3481 *compp = 0;
3482 *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3483 *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
3484
3485 *uncompp = 0;
3486 *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3487 *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
3488
3489 snapobj = new->ds_object;
3490 while (snapobj != oldsnap->ds_object) {
3491 dsl_dataset_t *snap;
3492 uint64_t used, comp, uncomp;
3493
3494 if (snapobj == new->ds_object) {
3495 snap = new;
3496 } else {
3497 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3498 if (err != 0)
3499 break;
3500 }
3501
3502 if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3503 dsl_dataset_phys(oldsnap)->ds_creation_txg) {
3504 /*
3505 * The blocks in the deadlist can not be born after
3506 * ds_prev_snap_txg, so get the whole deadlist space,
3507 * which is more efficient (especially for old-format
3508 * deadlists). Unfortunately the deadlist code
3509 * doesn't have enough information to make this
3510 * optimization itself.
3511 */
3512 dsl_deadlist_space(&snap->ds_deadlist,
3513 &used, &comp, &uncomp);
3514 } else {
3515 dsl_deadlist_space_range(&snap->ds_deadlist,
3516 0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
3517 &used, &comp, &uncomp);
3518 }
3519 *usedp += used;
3520 *compp += comp;
3521 *uncompp += uncomp;
3522
3523 /*
3524 * If we get to the beginning of the chain of snapshots
3525 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
3526 * was not a snapshot of/before new.
3527 */
3528 snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3529 if (snap != new)
3530 dsl_dataset_rele(snap, FTAG);
3531 if (snapobj == 0) {
3532 err = SET_ERROR(EINVAL);
3533 break;
3534 }
3535
3536 }
3537 return (err);
3538 }
3539
3540 /*
3541 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
3542 * lastsnap, and all snapshots in between are deleted.
3543 *
3544 * blocks that would be freed [---------------------------]
3545 * snapshots ---O-------O--------O-------O--------O
3546 * firstsnap lastsnap
3547 *
3548 * This is the set of blocks that were born after the snap before firstsnap,
3549 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
3550 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
3551 * We calculate this by iterating over the relevant deadlists (from the snap
3552 * after lastsnap, backward to the snap after firstsnap), summing up the
3553 * space on the deadlist that was born after the snap before firstsnap.
3554 */
3555 int
3556 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
3557 dsl_dataset_t *lastsnap,
3558 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3559 {
3560 int err = 0;
3561 uint64_t snapobj;
3562 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
3563
3564 ASSERT(firstsnap->ds_is_snapshot);
3565 ASSERT(lastsnap->ds_is_snapshot);
3566
3567 /*
3568 * Check that the snapshots are in the same dsl_dir, and firstsnap
3569 * is before lastsnap.
3570 */
3571 if (firstsnap->ds_dir != lastsnap->ds_dir ||
3572 dsl_dataset_phys(firstsnap)->ds_creation_txg >
3573 dsl_dataset_phys(lastsnap)->ds_creation_txg)
3574 return (SET_ERROR(EINVAL));
3575
3576 *usedp = *compp = *uncompp = 0;
3577
3578 snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
3579 while (snapobj != firstsnap->ds_object) {
3580 dsl_dataset_t *ds;
3581 uint64_t used, comp, uncomp;
3582
3583 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3584 if (err != 0)
3585 break;
3586
3587 dsl_deadlist_space_range(&ds->ds_deadlist,
3588 dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
3589 &used, &comp, &uncomp);
3590 *usedp += used;
3591 *compp += comp;
3592 *uncompp += uncomp;
3593
3594 snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3595 ASSERT3U(snapobj, !=, 0);
3596 dsl_dataset_rele(ds, FTAG);
3597 }
3598 return (err);
3599 }
3600
3601 /*
3602 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3603 * For example, they could both be snapshots of the same filesystem, and
3604 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3605 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3606 * filesystem. Or 'earlier' could be the origin's origin.
3607 *
3608 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3609 */
3610 boolean_t
3611 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
3612 uint64_t earlier_txg)
3613 {
3614 dsl_pool_t *dp = later->ds_dir->dd_pool;
3615 int error;
3616 boolean_t ret;
3617 dsl_dataset_t *origin;
3618
3619 ASSERT(dsl_pool_config_held(dp));
3620 ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
3621
3622 if (earlier_txg == 0)
3623 earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
3624
3625 if (later->ds_is_snapshot &&
3626 earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
3627 return (B_FALSE);
3628
3629 if (later->ds_dir == earlier->ds_dir)
3630 return (B_TRUE);
3631 if (!dsl_dir_is_clone(later->ds_dir))
3632 return (B_FALSE);
3633
3634 if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
3635 return (B_TRUE);
3636 error = dsl_dataset_hold_obj(dp,
3637 dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
3638 if (error != 0)
3639 return (B_FALSE);
3640 ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
3641 dsl_dataset_rele(origin, FTAG);
3642 return (ret);
3643 }
3644
3645 void
3646 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
3647 {
3648 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3649 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
3650 }
3651
3652 boolean_t
3653 dsl_dataset_is_zapified(dsl_dataset_t *ds)
3654 {
3655 dmu_object_info_t doi;
3656
3657 dmu_object_info_from_db(ds->ds_dbuf, &doi);
3658 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
3659 }
3660
3661 boolean_t
3662 dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
3663 {
3664 return (dsl_dataset_is_zapified(ds) &&
3665 zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
3666 ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
3667 }
3668
3669 #if defined(_KERNEL) && defined(HAVE_SPL)
3670 #if defined(_LP64)
3671 module_param(zfs_max_recordsize, int, 0644);
3672 MODULE_PARM_DESC(zfs_max_recordsize, "Max allowed record size");
3673 #else
3674 /* Limited to 1M on 32-bit platforms due to lack of virtual address space */
3675 module_param(zfs_max_recordsize, int, 0444);
3676 MODULE_PARM_DESC(zfs_max_recordsize, "Max allowed record size");
3677 #endif
3678
3679 EXPORT_SYMBOL(dsl_dataset_hold);
3680 EXPORT_SYMBOL(dsl_dataset_hold_obj);
3681 EXPORT_SYMBOL(dsl_dataset_own);
3682 EXPORT_SYMBOL(dsl_dataset_own_obj);
3683 EXPORT_SYMBOL(dsl_dataset_name);
3684 EXPORT_SYMBOL(dsl_dataset_rele);
3685 EXPORT_SYMBOL(dsl_dataset_disown);
3686 EXPORT_SYMBOL(dsl_dataset_tryown);
3687 EXPORT_SYMBOL(dsl_dataset_create_sync);
3688 EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
3689 EXPORT_SYMBOL(dsl_dataset_snapshot_check);
3690 EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
3691 EXPORT_SYMBOL(dsl_dataset_promote);
3692 EXPORT_SYMBOL(dsl_dataset_user_hold);
3693 EXPORT_SYMBOL(dsl_dataset_user_release);
3694 EXPORT_SYMBOL(dsl_dataset_get_holds);
3695 EXPORT_SYMBOL(dsl_dataset_get_blkptr);
3696 EXPORT_SYMBOL(dsl_dataset_get_spa);
3697 EXPORT_SYMBOL(dsl_dataset_modified_since_snap);
3698 EXPORT_SYMBOL(dsl_dataset_space_written);
3699 EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
3700 EXPORT_SYMBOL(dsl_dataset_sync);
3701 EXPORT_SYMBOL(dsl_dataset_block_born);
3702 EXPORT_SYMBOL(dsl_dataset_block_kill);
3703 EXPORT_SYMBOL(dsl_dataset_dirty);
3704 EXPORT_SYMBOL(dsl_dataset_stats);
3705 EXPORT_SYMBOL(dsl_dataset_fast_stat);
3706 EXPORT_SYMBOL(dsl_dataset_space);
3707 EXPORT_SYMBOL(dsl_dataset_fsid_guid);
3708 EXPORT_SYMBOL(dsl_dsobj_to_dsname);
3709 EXPORT_SYMBOL(dsl_dataset_check_quota);
3710 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
3711 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);
3712 #endif