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