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