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