]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_dataset.c
Illumos #4061
[mirror_zfs.git] / module / zfs / dsl_dataset.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
13fe0198 23 * Copyright (c) 2013 by Delphix. All rights reserved.
37abac6d 24 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <sys/dmu_objset.h>
28#include <sys/dsl_dataset.h>
29#include <sys/dsl_dir.h>
30#include <sys/dsl_prop.h>
31#include <sys/dsl_synctask.h>
32#include <sys/dmu_traverse.h>
37abac6d 33#include <sys/dmu_impl.h>
34dc7c2f
BB
34#include <sys/dmu_tx.h>
35#include <sys/arc.h>
36#include <sys/zio.h>
37#include <sys/zap.h>
9ae529ec 38#include <sys/zfeature.h>
34dc7c2f
BB
39#include <sys/unique.h>
40#include <sys/zfs_context.h>
41#include <sys/zfs_ioctl.h>
42#include <sys/spa.h>
b128c09f 43#include <sys/zfs_znode.h>
572e2857 44#include <sys/zfs_onexit.h>
45d1cae3 45#include <sys/zvol.h>
428870ff
BB
46#include <sys/dsl_scan.h>
47#include <sys/dsl_deadlist.h>
13fe0198
MA
48#include <sys/dsl_destroy.h>
49#include <sys/dsl_userhold.h>
34dc7c2f 50
428870ff
BB
51#define SWITCH64(x, y) \
52 { \
53 uint64_t __tmp = (x); \
54 (x) = (y); \
55 (y) = __tmp; \
56 }
57
34dc7c2f
BB
58#define DS_REF_MAX (1ULL << 62)
59
60#define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE
61
34dc7c2f
BB
62/*
63 * Figure out how much of this delta should be propogated to the dsl_dir
64 * layer. If there's a refreservation, that space has already been
65 * partially accounted for in our ancestors.
66 */
67static int64_t
68parent_delta(dsl_dataset_t *ds, int64_t delta)
69{
70 uint64_t old_bytes, new_bytes;
71
72 if (ds->ds_reserved == 0)
73 return (delta);
74
75 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
76 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
77
78 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
79 return (new_bytes - old_bytes);
80}
81
82void
428870ff 83dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
34dc7c2f 84{
d6320ddb 85 int used, compressed, uncompressed;
34dc7c2f
BB
86 int64_t delta;
87
d6320ddb
BB
88 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
89 compressed = BP_GET_PSIZE(bp);
90 uncompressed = BP_GET_UCSIZE(bp);
91
428870ff 92 dprintf_bp(bp, "ds=%p", ds);
34dc7c2f
BB
93
94 ASSERT(dmu_tx_is_syncing(tx));
95 /* It could have been compressed away to nothing */
96 if (BP_IS_HOLE(bp))
97 return;
98 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
9ae529ec 99 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
34dc7c2f 100 if (ds == NULL) {
29809a6c
MA
101 dsl_pool_mos_diduse_space(tx->tx_pool,
102 used, compressed, uncompressed);
34dc7c2f
BB
103 return;
104 }
105 dmu_buf_will_dirty(ds->ds_dbuf, tx);
428870ff 106
b128c09f 107 mutex_enter(&ds->ds_dir->dd_lock);
34dc7c2f
BB
108 mutex_enter(&ds->ds_lock);
109 delta = parent_delta(ds, used);
9ae529ec 110 ds->ds_phys->ds_referenced_bytes += used;
34dc7c2f
BB
111 ds->ds_phys->ds_compressed_bytes += compressed;
112 ds->ds_phys->ds_uncompressed_bytes += uncompressed;
113 ds->ds_phys->ds_unique_bytes += used;
114 mutex_exit(&ds->ds_lock);
b128c09f
BB
115 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
116 compressed, uncompressed, tx);
117 dsl_dir_transfer_space(ds->ds_dir, used - delta,
118 DD_USED_REFRSRV, DD_USED_HEAD, tx);
119 mutex_exit(&ds->ds_dir->dd_lock);
34dc7c2f
BB
120}
121
b128c09f 122int
428870ff
BB
123dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
124 boolean_t async)
34dc7c2f 125{
d6320ddb
BB
126 int used, compressed, uncompressed;
127
34dc7c2f 128 if (BP_IS_HOLE(bp))
b128c09f 129 return (0);
34dc7c2f 130
428870ff
BB
131 ASSERT(dmu_tx_is_syncing(tx));
132 ASSERT(bp->blk_birth <= tx->tx_txg);
133
d6320ddb
BB
134 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
135 compressed = BP_GET_PSIZE(bp);
136 uncompressed = BP_GET_UCSIZE(bp);
428870ff 137
34dc7c2f
BB
138 ASSERT(used > 0);
139 if (ds == NULL) {
428870ff 140 dsl_free(tx->tx_pool, tx->tx_txg, bp);
29809a6c
MA
141 dsl_pool_mos_diduse_space(tx->tx_pool,
142 -used, -compressed, -uncompressed);
b128c09f 143 return (used);
34dc7c2f
BB
144 }
145 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
146
b128c09f 147 ASSERT(!dsl_dataset_is_snapshot(ds));
34dc7c2f
BB
148 dmu_buf_will_dirty(ds->ds_dbuf, tx);
149
150 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
34dc7c2f
BB
151 int64_t delta;
152
428870ff
BB
153 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
154 dsl_free(tx->tx_pool, tx->tx_txg, bp);
34dc7c2f 155
b128c09f 156 mutex_enter(&ds->ds_dir->dd_lock);
34dc7c2f
BB
157 mutex_enter(&ds->ds_lock);
158 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
159 !DS_UNIQUE_IS_ACCURATE(ds));
160 delta = parent_delta(ds, -used);
161 ds->ds_phys->ds_unique_bytes -= used;
162 mutex_exit(&ds->ds_lock);
b128c09f 163 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
34dc7c2f 164 delta, -compressed, -uncompressed, tx);
b128c09f
BB
165 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
166 DD_USED_REFRSRV, DD_USED_HEAD, tx);
167 mutex_exit(&ds->ds_dir->dd_lock);
34dc7c2f
BB
168 } else {
169 dprintf_bp(bp, "putting on dead list: %s", "");
428870ff
BB
170 if (async) {
171 /*
172 * We are here as part of zio's write done callback,
173 * which means we're a zio interrupt thread. We can't
174 * call dsl_deadlist_insert() now because it may block
175 * waiting for I/O. Instead, put bp on the deferred
176 * queue and let dsl_pool_sync() finish the job.
177 */
178 bplist_append(&ds->ds_pending_deadlist, bp);
179 } else {
180 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
181 }
34dc7c2f
BB
182 ASSERT3U(ds->ds_prev->ds_object, ==,
183 ds->ds_phys->ds_prev_snap_obj);
184 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
185 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
186 if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
187 ds->ds_object && bp->blk_birth >
188 ds->ds_prev->ds_phys->ds_prev_snap_txg) {
189 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
190 mutex_enter(&ds->ds_prev->ds_lock);
191 ds->ds_prev->ds_phys->ds_unique_bytes += used;
192 mutex_exit(&ds->ds_prev->ds_lock);
193 }
428870ff 194 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
b128c09f
BB
195 dsl_dir_transfer_space(ds->ds_dir, used,
196 DD_USED_HEAD, DD_USED_SNAP, tx);
197 }
34dc7c2f
BB
198 }
199 mutex_enter(&ds->ds_lock);
9ae529ec
CS
200 ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
201 ds->ds_phys->ds_referenced_bytes -= used;
34dc7c2f
BB
202 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
203 ds->ds_phys->ds_compressed_bytes -= compressed;
204 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
205 ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
206 mutex_exit(&ds->ds_lock);
b128c09f
BB
207
208 return (used);
34dc7c2f
BB
209}
210
211uint64_t
212dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
213{
214 uint64_t trysnap = 0;
215
216 if (ds == NULL)
217 return (0);
218 /*
219 * The snapshot creation could fail, but that would cause an
220 * incorrect FALSE return, which would only result in an
221 * overestimation of the amount of space that an operation would
222 * consume, which is OK.
223 *
224 * There's also a small window where we could miss a pending
225 * snapshot, because we could set the sync task in the quiescing
226 * phase. So this should only be used as a guess.
227 */
228 if (ds->ds_trysnap_txg >
229 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
230 trysnap = ds->ds_trysnap_txg;
231 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
232}
233
9babb374 234boolean_t
428870ff
BB
235dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
236 uint64_t blk_birth)
34dc7c2f 237{
428870ff
BB
238 if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
239 return (B_FALSE);
240
572e2857 241 ddt_prefetch(dsl_dataset_get_spa(ds), bp);
428870ff
BB
242
243 return (B_TRUE);
34dc7c2f
BB
244}
245
246/* ARGSUSED */
247static void
248dsl_dataset_evict(dmu_buf_t *db, void *dsv)
249{
250 dsl_dataset_t *ds = dsv;
251
13fe0198 252 ASSERT(ds->ds_owner == NULL);
34dc7c2f 253
34dc7c2f
BB
254 unique_remove(ds->ds_fsid_guid);
255
428870ff
BB
256 if (ds->ds_objset != NULL)
257 dmu_objset_evict(ds->ds_objset);
34dc7c2f
BB
258
259 if (ds->ds_prev) {
13fe0198 260 dsl_dataset_rele(ds->ds_prev, ds);
34dc7c2f
BB
261 ds->ds_prev = NULL;
262 }
263
428870ff 264 bplist_destroy(&ds->ds_pending_deadlist);
13fe0198 265 if (ds->ds_phys->ds_deadlist_obj != 0)
428870ff 266 dsl_deadlist_close(&ds->ds_deadlist);
b128c09f 267 if (ds->ds_dir)
13fe0198 268 dsl_dir_rele(ds->ds_dir, ds);
34dc7c2f
BB
269
270 ASSERT(!list_link_active(&ds->ds_synced_link));
271
272 mutex_destroy(&ds->ds_lock);
273 mutex_destroy(&ds->ds_opening_lock);
13fe0198 274 refcount_destroy(&ds->ds_longholds);
34dc7c2f
BB
275
276 kmem_free(ds, sizeof (dsl_dataset_t));
277}
278
13fe0198 279int
34dc7c2f
BB
280dsl_dataset_get_snapname(dsl_dataset_t *ds)
281{
282 dsl_dataset_phys_t *headphys;
283 int err;
284 dmu_buf_t *headdbuf;
285 dsl_pool_t *dp = ds->ds_dir->dd_pool;
286 objset_t *mos = dp->dp_meta_objset;
287
288 if (ds->ds_snapname[0])
289 return (0);
290 if (ds->ds_phys->ds_next_snap_obj == 0)
291 return (0);
292
293 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
294 FTAG, &headdbuf);
13fe0198 295 if (err != 0)
34dc7c2f
BB
296 return (err);
297 headphys = headdbuf->db_data;
298 err = zap_value_search(dp->dp_meta_objset,
299 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
300 dmu_buf_rele(headdbuf, FTAG);
301 return (err);
302}
303
6772fb67 304int
b128c09f 305dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
34dc7c2f 306{
b128c09f
BB
307 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
308 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
34dc7c2f
BB
309 matchtype_t mt;
310 int err;
311
b128c09f 312 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f
BB
313 mt = MT_FIRST;
314 else
315 mt = MT_EXACT;
316
b128c09f 317 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
34dc7c2f
BB
318 value, mt, NULL, 0, NULL);
319 if (err == ENOTSUP && mt == MT_FIRST)
b128c09f 320 err = zap_lookup(mos, snapobj, name, 8, 1, value);
34dc7c2f
BB
321 return (err);
322}
323
13fe0198
MA
324int
325dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
34dc7c2f 326{
b128c09f
BB
327 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
328 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
34dc7c2f
BB
329 matchtype_t mt;
330 int err;
331
428870ff
BB
332 dsl_dir_snap_cmtime_update(ds->ds_dir);
333
b128c09f 334 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f
BB
335 mt = MT_FIRST;
336 else
337 mt = MT_EXACT;
338
b128c09f 339 err = zap_remove_norm(mos, snapobj, name, mt, tx);
34dc7c2f 340 if (err == ENOTSUP && mt == MT_FIRST)
b128c09f 341 err = zap_remove(mos, snapobj, name, tx);
34dc7c2f
BB
342 return (err);
343}
344
13fe0198
MA
345int
346dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
b128c09f 347 dsl_dataset_t **dsp)
34dc7c2f 348{
34dc7c2f
BB
349 objset_t *mos = dp->dp_meta_objset;
350 dmu_buf_t *dbuf;
351 dsl_dataset_t *ds;
352 int err;
572e2857 353 dmu_object_info_t doi;
34dc7c2f 354
13fe0198 355 ASSERT(dsl_pool_config_held(dp));
34dc7c2f
BB
356
357 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
13fe0198 358 if (err != 0)
34dc7c2f 359 return (err);
572e2857
BB
360
361 /* Make sure dsobj has the correct object type. */
362 dmu_object_info_from_db(dbuf, &doi);
3a84951d
WA
363 if (doi.doi_type != DMU_OT_DSL_DATASET) {
364 dmu_buf_rele(dbuf, tag);
2e528b49 365 return (SET_ERROR(EINVAL));
3a84951d 366 }
572e2857 367
34dc7c2f
BB
368 ds = dmu_buf_get_user(dbuf);
369 if (ds == NULL) {
d4ed6673 370 dsl_dataset_t *winner = NULL;
34dc7c2f 371
b8d06fca 372 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_PUSHPAGE);
34dc7c2f
BB
373 ds->ds_dbuf = dbuf;
374 ds->ds_object = dsobj;
375 ds->ds_phys = dbuf->db_data;
98f72a53 376 list_link_init(&ds->ds_synced_link);
34dc7c2f
BB
377
378 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
379 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
37abac6d 380 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
13fe0198 381 refcount_create(&ds->ds_longholds);
34dc7c2f 382
428870ff
BB
383 bplist_create(&ds->ds_pending_deadlist);
384 dsl_deadlist_open(&ds->ds_deadlist,
34dc7c2f 385 mos, ds->ds_phys->ds_deadlist_obj);
428870ff 386
37abac6d
BP
387 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
388 offsetof(dmu_sendarg_t, dsa_link));
389
34dc7c2f 390 if (err == 0) {
13fe0198 391 err = dsl_dir_hold_obj(dp,
34dc7c2f
BB
392 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
393 }
13fe0198 394 if (err != 0) {
34dc7c2f
BB
395 mutex_destroy(&ds->ds_lock);
396 mutex_destroy(&ds->ds_opening_lock);
13fe0198 397 refcount_destroy(&ds->ds_longholds);
428870ff
BB
398 bplist_destroy(&ds->ds_pending_deadlist);
399 dsl_deadlist_close(&ds->ds_deadlist);
34dc7c2f
BB
400 kmem_free(ds, sizeof (dsl_dataset_t));
401 dmu_buf_rele(dbuf, tag);
402 return (err);
403 }
404
b128c09f 405 if (!dsl_dataset_is_snapshot(ds)) {
34dc7c2f 406 ds->ds_snapname[0] = '\0';
13fe0198
MA
407 if (ds->ds_phys->ds_prev_snap_obj != 0) {
408 err = dsl_dataset_hold_obj(dp,
b128c09f
BB
409 ds->ds_phys->ds_prev_snap_obj,
410 ds, &ds->ds_prev);
34dc7c2f 411 }
45d1cae3
BB
412 } else {
413 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
414 err = dsl_dataset_get_snapname(ds);
415 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
416 err = zap_count(
417 ds->ds_dir->dd_pool->dp_meta_objset,
418 ds->ds_phys->ds_userrefs_obj,
419 &ds->ds_userrefs);
420 }
34dc7c2f
BB
421 }
422
b128c09f 423 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
13fe0198
MA
424 err = dsl_prop_get_int_ds(ds,
425 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
426 &ds->ds_reserved);
34dc7c2f 427 if (err == 0) {
13fe0198
MA
428 err = dsl_prop_get_int_ds(ds,
429 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
430 &ds->ds_quota);
34dc7c2f 431 }
34dc7c2f
BB
432 } else {
433 ds->ds_reserved = ds->ds_quota = 0;
434 }
435
a117a6d6
GW
436 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
437 &ds->ds_phys, dsl_dataset_evict)) != NULL) {
428870ff
BB
438 bplist_destroy(&ds->ds_pending_deadlist);
439 dsl_deadlist_close(&ds->ds_deadlist);
b128c09f 440 if (ds->ds_prev)
13fe0198
MA
441 dsl_dataset_rele(ds->ds_prev, ds);
442 dsl_dir_rele(ds->ds_dir, ds);
34dc7c2f
BB
443 mutex_destroy(&ds->ds_lock);
444 mutex_destroy(&ds->ds_opening_lock);
13fe0198 445 refcount_destroy(&ds->ds_longholds);
34dc7c2f 446 kmem_free(ds, sizeof (dsl_dataset_t));
13fe0198 447 if (err != 0) {
34dc7c2f
BB
448 dmu_buf_rele(dbuf, tag);
449 return (err);
450 }
451 ds = winner;
452 } else {
453 ds->ds_fsid_guid =
454 unique_insert(ds->ds_phys->ds_fsid_guid);
455 }
456 }
457 ASSERT3P(ds->ds_dbuf, ==, dbuf);
458 ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
b128c09f
BB
459 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
460 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
461 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
34dc7c2f
BB
462 *dsp = ds;
463 return (0);
464}
465
b128c09f 466int
13fe0198 467dsl_dataset_hold(dsl_pool_t *dp, const char *name,
428870ff 468 void *tag, dsl_dataset_t **dsp)
34dc7c2f
BB
469{
470 dsl_dir_t *dd;
b128c09f 471 const char *snapname;
34dc7c2f 472 uint64_t obj;
34dc7c2f
BB
473 int err = 0;
474
13fe0198
MA
475 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
476 if (err != 0)
34dc7c2f
BB
477 return (err);
478
13fe0198 479 ASSERT(dsl_pool_config_held(dp));
34dc7c2f 480 obj = dd->dd_phys->dd_head_dataset_obj;
13fe0198
MA
481 if (obj != 0)
482 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
b128c09f 483 else
2e528b49 484 err = SET_ERROR(ENOENT);
34dc7c2f 485
b128c09f
BB
486 /* we may be looking for a snapshot */
487 if (err == 0 && snapname != NULL) {
13fe0198 488 dsl_dataset_t *ds;
34dc7c2f 489
b128c09f
BB
490 if (*snapname++ != '@') {
491 dsl_dataset_rele(*dsp, tag);
13fe0198 492 dsl_dir_rele(dd, FTAG);
2e528b49 493 return (SET_ERROR(ENOENT));
34dc7c2f 494 }
34dc7c2f 495
b128c09f
BB
496 dprintf("looking for snapshot '%s'\n", snapname);
497 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
498 if (err == 0)
13fe0198 499 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
b128c09f
BB
500 dsl_dataset_rele(*dsp, tag);
501
13fe0198 502 if (err == 0) {
b128c09f
BB
503 mutex_enter(&ds->ds_lock);
504 if (ds->ds_snapname[0] == 0)
505 (void) strlcpy(ds->ds_snapname, snapname,
506 sizeof (ds->ds_snapname));
507 mutex_exit(&ds->ds_lock);
13fe0198 508 *dsp = ds;
34dc7c2f 509 }
34dc7c2f 510 }
13fe0198
MA
511
512 dsl_dir_rele(dd, FTAG);
34dc7c2f
BB
513 return (err);
514}
515
516int
13fe0198 517dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
428870ff 518 void *tag, dsl_dataset_t **dsp)
34dc7c2f 519{
13fe0198
MA
520 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
521 if (err != 0)
522 return (err);
523 if (!dsl_dataset_tryown(*dsp, tag)) {
524 dsl_dataset_rele(*dsp, tag);
525 *dsp = NULL;
2e528b49 526 return (SET_ERROR(EBUSY));
13fe0198
MA
527 }
528 return (0);
529}
530
531int
532dsl_dataset_own(dsl_pool_t *dp, const char *name,
533 void *tag, dsl_dataset_t **dsp)
534{
535 int err = dsl_dataset_hold(dp, name, tag, dsp);
536 if (err != 0)
b128c09f 537 return (err);
13fe0198 538 if (!dsl_dataset_tryown(*dsp, tag)) {
428870ff 539 dsl_dataset_rele(*dsp, tag);
2e528b49 540 return (SET_ERROR(EBUSY));
b128c09f
BB
541 }
542 return (0);
34dc7c2f
BB
543}
544
13fe0198
MA
545/*
546 * See the comment above dsl_pool_hold() for details. In summary, a long
547 * hold is used to prevent destruction of a dataset while the pool hold
548 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
549 *
550 * The dataset and pool must be held when this function is called. After it
551 * is called, the pool hold may be released while the dataset is still held
552 * and accessed.
553 */
554void
555dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
556{
557 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
558 (void) refcount_add(&ds->ds_longholds, tag);
559}
560
561void
562dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
563{
564 (void) refcount_remove(&ds->ds_longholds, tag);
565}
566
567/* Return B_TRUE if there are any long holds on this dataset. */
568boolean_t
569dsl_dataset_long_held(dsl_dataset_t *ds)
570{
571 return (!refcount_is_zero(&ds->ds_longholds));
572}
573
34dc7c2f
BB
574void
575dsl_dataset_name(dsl_dataset_t *ds, char *name)
576{
577 if (ds == NULL) {
578 (void) strcpy(name, "mos");
579 } else {
580 dsl_dir_name(ds->ds_dir, name);
13fe0198 581 VERIFY0(dsl_dataset_get_snapname(ds));
34dc7c2f
BB
582 if (ds->ds_snapname[0]) {
583 (void) strcat(name, "@");
b128c09f
BB
584 /*
585 * We use a "recursive" mutex so that we
586 * can call dprintf_ds() with ds_lock held.
587 */
34dc7c2f 588 if (!MUTEX_HELD(&ds->ds_lock)) {
34dc7c2f
BB
589 mutex_enter(&ds->ds_lock);
590 (void) strcat(name, ds->ds_snapname);
591 mutex_exit(&ds->ds_lock);
592 } else {
593 (void) strcat(name, ds->ds_snapname);
594 }
595 }
596 }
597}
598
34dc7c2f 599void
b128c09f
BB
600dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
601{
13fe0198 602 dmu_buf_rele(ds->ds_dbuf, tag);
b128c09f
BB
603}
604
605void
428870ff 606dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
34dc7c2f 607{
13fe0198 608 ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
b128c09f 609
34dc7c2f 610 mutex_enter(&ds->ds_lock);
b128c09f 611 ds->ds_owner = NULL;
34dc7c2f 612 mutex_exit(&ds->ds_lock);
13fe0198
MA
613 dsl_dataset_long_rele(ds, tag);
614 if (ds->ds_dbuf != NULL)
615 dsl_dataset_rele(ds, tag);
b128c09f 616 else
428870ff 617 dsl_dataset_evict(NULL, ds);
34dc7c2f
BB
618}
619
620boolean_t
13fe0198 621dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
34dc7c2f 622{
b128c09f
BB
623 boolean_t gotit = FALSE;
624
34dc7c2f 625 mutex_enter(&ds->ds_lock);
13fe0198 626 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
428870ff 627 ds->ds_owner = tag;
13fe0198 628 dsl_dataset_long_hold(ds, tag);
b128c09f 629 gotit = TRUE;
34dc7c2f
BB
630 }
631 mutex_exit(&ds->ds_lock);
b128c09f 632 return (gotit);
34dc7c2f
BB
633}
634
34dc7c2f 635uint64_t
b128c09f 636dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
34dc7c2f
BB
637 uint64_t flags, dmu_tx_t *tx)
638{
639 dsl_pool_t *dp = dd->dd_pool;
640 dmu_buf_t *dbuf;
641 dsl_dataset_phys_t *dsphys;
642 uint64_t dsobj;
643 objset_t *mos = dp->dp_meta_objset;
644
b128c09f
BB
645 if (origin == NULL)
646 origin = dp->dp_origin_snap;
647
34dc7c2f
BB
648 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
649 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
650 ASSERT(dmu_tx_is_syncing(tx));
651 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
652
653 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
654 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13fe0198 655 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
34dc7c2f
BB
656 dmu_buf_will_dirty(dbuf, tx);
657 dsphys = dbuf->db_data;
b128c09f 658 bzero(dsphys, sizeof (dsl_dataset_phys_t));
34dc7c2f
BB
659 dsphys->ds_dir_obj = dd->dd_object;
660 dsphys->ds_flags = flags;
661 dsphys->ds_fsid_guid = unique_create();
662 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
663 sizeof (dsphys->ds_guid));
664 dsphys->ds_snapnames_zapobj =
665 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
666 DMU_OT_NONE, 0, tx);
667 dsphys->ds_creation_time = gethrestime_sec();
b128c09f 668 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
34dc7c2f 669
428870ff
BB
670 if (origin == NULL) {
671 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
672 } else {
13fe0198 673 dsl_dataset_t *ohds; /* head of the origin snapshot */
428870ff 674
34dc7c2f
BB
675 dsphys->ds_prev_snap_obj = origin->ds_object;
676 dsphys->ds_prev_snap_txg =
677 origin->ds_phys->ds_creation_txg;
9ae529ec
CS
678 dsphys->ds_referenced_bytes =
679 origin->ds_phys->ds_referenced_bytes;
34dc7c2f
BB
680 dsphys->ds_compressed_bytes =
681 origin->ds_phys->ds_compressed_bytes;
682 dsphys->ds_uncompressed_bytes =
683 origin->ds_phys->ds_uncompressed_bytes;
684 dsphys->ds_bp = origin->ds_phys->ds_bp;
685 dsphys->ds_flags |= origin->ds_phys->ds_flags;
686
687 dmu_buf_will_dirty(origin->ds_dbuf, tx);
688 origin->ds_phys->ds_num_children++;
689
13fe0198 690 VERIFY0(dsl_dataset_hold_obj(dp,
428870ff
BB
691 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
692 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
693 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
694 dsl_dataset_rele(ohds, FTAG);
695
b128c09f
BB
696 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
697 if (origin->ds_phys->ds_next_clones_obj == 0) {
698 origin->ds_phys->ds_next_clones_obj =
699 zap_create(mos,
700 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
701 }
13fe0198
MA
702 VERIFY0(zap_add_int(mos,
703 origin->ds_phys->ds_next_clones_obj, dsobj, tx));
b128c09f
BB
704 }
705
34dc7c2f
BB
706 dmu_buf_will_dirty(dd->dd_dbuf, tx);
707 dd->dd_phys->dd_origin_obj = origin->ds_object;
428870ff
BB
708 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
709 if (origin->ds_dir->dd_phys->dd_clones == 0) {
710 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
711 origin->ds_dir->dd_phys->dd_clones =
712 zap_create(mos,
713 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
714 }
13fe0198 715 VERIFY0(zap_add_int(mos,
428870ff
BB
716 origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
717 }
34dc7c2f
BB
718 }
719
720 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
721 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
722
723 dmu_buf_rele(dbuf, FTAG);
724
725 dmu_buf_will_dirty(dd->dd_dbuf, tx);
726 dd->dd_phys->dd_head_dataset_obj = dsobj;
727
728 return (dsobj);
729}
730
13fe0198
MA
731static void
732dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
733{
734 objset_t *os;
735
736 VERIFY0(dmu_objset_from_ds(ds, &os));
737 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
738 dsl_dataset_dirty(ds, tx);
739}
740
34dc7c2f
BB
741uint64_t
742dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
743 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
744{
745 dsl_pool_t *dp = pdd->dd_pool;
746 uint64_t dsobj, ddobj;
747 dsl_dir_t *dd;
748
13fe0198 749 ASSERT(dmu_tx_is_syncing(tx));
34dc7c2f
BB
750 ASSERT(lastname[0] != '@');
751
b128c09f 752 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
13fe0198 753 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
34dc7c2f 754
13fe0198
MA
755 dsobj = dsl_dataset_create_sync_dd(dd, origin,
756 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
34dc7c2f
BB
757
758 dsl_deleg_set_create_perms(dd, tx, cr);
759
13fe0198 760 dsl_dir_rele(dd, FTAG);
34dc7c2f 761
572e2857
BB
762 /*
763 * If we are creating a clone, make sure we zero out any stale
764 * data from the origin snapshots zil header.
765 */
13fe0198 766 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
572e2857 767 dsl_dataset_t *ds;
572e2857 768
13fe0198
MA
769 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
770 dsl_dataset_zero_zil(ds, tx);
572e2857
BB
771 dsl_dataset_rele(ds, FTAG);
772 }
773
34dc7c2f
BB
774 return (dsobj);
775}
776
34dc7c2f 777/*
13fe0198
MA
778 * The unique space in the head dataset can be calculated by subtracting
779 * the space used in the most recent snapshot, that is still being used
780 * in this file system, from the space currently in use. To figure out
781 * the space in the most recent snapshot still in use, we need to take
782 * the total space used in the snapshot and subtract out the space that
783 * has been freed up since the snapshot was taken.
34dc7c2f 784 */
13fe0198
MA
785void
786dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
34dc7c2f 787{
13fe0198
MA
788 uint64_t mrs_used;
789 uint64_t dlused, dlcomp, dluncomp;
34dc7c2f 790
13fe0198 791 ASSERT(!dsl_dataset_is_snapshot(ds));
34dc7c2f 792
13fe0198
MA
793 if (ds->ds_phys->ds_prev_snap_obj != 0)
794 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
795 else
796 mrs_used = 0;
45d1cae3 797
13fe0198 798 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
34dc7c2f 799
13fe0198
MA
800 ASSERT3U(dlused, <=, mrs_used);
801 ds->ds_phys->ds_unique_bytes =
802 ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
330d06f9 803
13fe0198
MA
804 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
805 SPA_VERSION_UNIQUE_ACCURATE)
806 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
34dc7c2f
BB
807}
808
13fe0198
MA
809void
810dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
811 dmu_tx_t *tx)
45d1cae3 812{
13fe0198
MA
813 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
814 int err;
815 ASSERTV(uint64_t count);
816
817 ASSERT(ds->ds_phys->ds_num_children >= 2);
818 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
819 /*
820 * The err should not be ENOENT, but a bug in a previous version
821 * of the code could cause upgrade_clones_cb() to not set
822 * ds_next_snap_obj when it should, leading to a missing entry.
823 * If we knew that the pool was created after
824 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
825 * ENOENT. However, at least we can check that we don't have
826 * too many entries in the next_clones_obj even after failing to
827 * remove this one.
828 */
829 if (err != ENOENT)
830 VERIFY0(err);
831 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
832 &count));
833 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
834}
45d1cae3 835
45d1cae3 836
13fe0198
MA
837blkptr_t *
838dsl_dataset_get_blkptr(dsl_dataset_t *ds)
839{
840 return (&ds->ds_phys->ds_bp);
45d1cae3
BB
841}
842
13fe0198
MA
843void
844dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
45d1cae3 845{
13fe0198
MA
846 ASSERT(dmu_tx_is_syncing(tx));
847 /* If it's the meta-objset, set dp_meta_rootbp */
848 if (ds == NULL) {
849 tx->tx_pool->dp_meta_rootbp = *bp;
850 } else {
851 dmu_buf_will_dirty(ds->ds_dbuf, tx);
852 ds->ds_phys->ds_bp = *bp;
45d1cae3 853 }
13fe0198 854}
45d1cae3 855
13fe0198
MA
856spa_t *
857dsl_dataset_get_spa(dsl_dataset_t *ds)
858{
859 return (ds->ds_dir->dd_pool->dp_spa);
45d1cae3
BB
860}
861
13fe0198
MA
862void
863dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
34dc7c2f 864{
13fe0198 865 dsl_pool_t *dp;
45d1cae3 866
13fe0198
MA
867 if (ds == NULL) /* this is the meta-objset */
868 return;
34dc7c2f 869
428870ff 870 ASSERT(ds->ds_objset != NULL);
34dc7c2f
BB
871
872 if (ds->ds_phys->ds_next_snap_obj != 0)
873 panic("dirtying snapshot!");
874
875 dp = ds->ds_dir->dd_pool;
876
13fe0198 877 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
34dc7c2f
BB
878 /* up the hold count until we can be written out */
879 dmu_buf_add_ref(ds->ds_dbuf, ds);
880 }
881}
882
04434775
MA
883boolean_t
884dsl_dataset_is_dirty(dsl_dataset_t *ds)
885{
886 int t;
887
888 for (t = 0; t < TXG_SIZE; t++) {
889 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
890 ds, t))
891 return (B_TRUE);
892 }
893 return (B_FALSE);
894}
895
34dc7c2f 896static int
13fe0198 897dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
34dc7c2f 898{
13fe0198 899 uint64_t asize;
34dc7c2f 900
13fe0198 901 if (!dmu_tx_is_syncing(tx))
b128c09f
BB
902 return (0);
903
34dc7c2f 904 /*
13fe0198
MA
905 * If there's an fs-only reservation, any blocks that might become
906 * owned by the snapshot dataset must be accommodated by space
907 * outside of the reservation.
34dc7c2f 908 */
13fe0198
MA
909 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
910 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
911 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
2e528b49 912 return (SET_ERROR(ENOSPC));
34dc7c2f
BB
913
914 /*
13fe0198
MA
915 * Propagate any reserved space for this snapshot to other
916 * snapshot checks in this sync group.
34dc7c2f 917 */
13fe0198
MA
918 if (asize > 0)
919 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
34dc7c2f
BB
920
921 return (0);
922}
923
13fe0198
MA
924typedef struct dsl_dataset_snapshot_arg {
925 nvlist_t *ddsa_snaps;
926 nvlist_t *ddsa_props;
927 nvlist_t *ddsa_errors;
928} dsl_dataset_snapshot_arg_t;
45d1cae3 929
34dc7c2f 930int
13fe0198 931dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
96c2e961 932 dmu_tx_t *tx, boolean_t recv)
34dc7c2f 933{
13fe0198
MA
934 int error;
935 uint64_t value;
34dc7c2f 936
13fe0198 937 ds->ds_trysnap_txg = tx->tx_txg;
b128c09f 938
13fe0198 939 if (!dmu_tx_is_syncing(tx))
45d1cae3 940 return (0);
34dc7c2f
BB
941
942 /*
13fe0198
MA
943 * We don't allow multiple snapshots of the same txg. If there
944 * is already one, try again.
34dc7c2f 945 */
13fe0198 946 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
2e528b49 947 return (SET_ERROR(EAGAIN));
34dc7c2f
BB
948
949 /*
13fe0198 950 * Check for conflicting snapshot name.
34dc7c2f 951 */
13fe0198
MA
952 error = dsl_dataset_snap_lookup(ds, snapname, &value);
953 if (error == 0)
2e528b49 954 return (SET_ERROR(EEXIST));
13fe0198
MA
955 if (error != ENOENT)
956 return (error);
45d1cae3 957
96c2e961
KW
958 /*
959 * We don't allow taking snapshots of inconsistent datasets, such as
960 * those into which we are currently receiving. However, if we are
961 * creating this snapshot as part of a receive, this check will be
962 * executed atomically with respect to the completion of the receive
963 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
964 * case we ignore this, knowing it will be fixed up for us shortly in
965 * dmu_recv_end_sync().
966 */
967 if (!recv && DS_IS_INCONSISTENT(ds))
968 return (SET_ERROR(EBUSY));
969
13fe0198
MA
970 error = dsl_dataset_snapshot_reserve_space(ds, tx);
971 if (error != 0)
972 return (error);
45d1cae3 973
34dc7c2f
BB
974 return (0);
975}
976
13fe0198
MA
977static int
978dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
b128c09f 979{
13fe0198
MA
980 dsl_dataset_snapshot_arg_t *ddsa = arg;
981 dsl_pool_t *dp = dmu_tx_pool(tx);
982 nvpair_t *pair;
983 int rv = 0;
b128c09f 984
13fe0198
MA
985 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
986 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
987 int error = 0;
988 dsl_dataset_t *ds;
989 char *name, *atp;
990 char dsname[MAXNAMELEN];
991
992 name = nvpair_name(pair);
993 if (strlen(name) >= MAXNAMELEN)
2e528b49 994 error = SET_ERROR(ENAMETOOLONG);
13fe0198
MA
995 if (error == 0) {
996 atp = strchr(name, '@');
997 if (atp == NULL)
2e528b49 998 error = SET_ERROR(EINVAL);
13fe0198
MA
999 if (error == 0)
1000 (void) strlcpy(dsname, name, atp - name + 1);
1001 }
1002 if (error == 0)
1003 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1004 if (error == 0) {
1005 error = dsl_dataset_snapshot_check_impl(ds,
96c2e961 1006 atp + 1, tx, B_FALSE);
13fe0198
MA
1007 dsl_dataset_rele(ds, FTAG);
1008 }
b128c09f 1009
13fe0198
MA
1010 if (error != 0) {
1011 if (ddsa->ddsa_errors != NULL) {
1012 fnvlist_add_int32(ddsa->ddsa_errors,
1013 name, error);
1014 }
1015 rv = error;
1016 }
1017 }
1018 return (rv);
b128c09f
BB
1019}
1020
13fe0198
MA
1021void
1022dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1023 dmu_tx_t *tx)
428870ff 1024{
13fe0198
MA
1025 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1026 dmu_buf_t *dbuf;
1027 dsl_dataset_phys_t *dsphys;
1028 uint64_t dsobj, crtxg;
1029 objset_t *mos = dp->dp_meta_objset;
1030 ASSERTV(static zil_header_t zero_zil);
1031 ASSERTV(objset_t *os);
1032
1033 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
428870ff 1034
428870ff 1035 /*
13fe0198
MA
1036 * If we are on an old pool, the zil must not be active, in which
1037 * case it will be zeroed. Usually zil_suspend() accomplishes this.
428870ff 1038 */
13fe0198
MA
1039 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1040 dmu_objset_from_ds(ds, &os) != 0 ||
1041 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1042 sizeof (zero_zil)) == 0);
428870ff 1043
428870ff
BB
1044
1045 /*
13fe0198 1046 * The origin's ds_creation_txg has to be < TXG_INITIAL
b128c09f
BB
1047 */
1048 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1049 crtxg = 1;
1050 else
1051 crtxg = tx->tx_txg;
1052
34dc7c2f
BB
1053 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1054 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13fe0198 1055 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
34dc7c2f
BB
1056 dmu_buf_will_dirty(dbuf, tx);
1057 dsphys = dbuf->db_data;
b128c09f 1058 bzero(dsphys, sizeof (dsl_dataset_phys_t));
34dc7c2f
BB
1059 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1060 dsphys->ds_fsid_guid = unique_create();
1061 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1062 sizeof (dsphys->ds_guid));
1063 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1064 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1065 dsphys->ds_next_snap_obj = ds->ds_object;
1066 dsphys->ds_num_children = 1;
1067 dsphys->ds_creation_time = gethrestime_sec();
b128c09f 1068 dsphys->ds_creation_txg = crtxg;
34dc7c2f 1069 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
9ae529ec 1070 dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
34dc7c2f
BB
1071 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1072 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1073 dsphys->ds_flags = ds->ds_phys->ds_flags;
1074 dsphys->ds_bp = ds->ds_phys->ds_bp;
1075 dmu_buf_rele(dbuf, FTAG);
1076
1077 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1078 if (ds->ds_prev) {
b128c09f
BB
1079 uint64_t next_clones_obj =
1080 ds->ds_prev->ds_phys->ds_next_clones_obj;
34dc7c2f
BB
1081 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1082 ds->ds_object ||
1083 ds->ds_prev->ds_phys->ds_num_children > 1);
1084 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1085 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1086 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1087 ds->ds_prev->ds_phys->ds_creation_txg);
1088 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
b128c09f 1089 } else if (next_clones_obj != 0) {
13fe0198 1090 dsl_dataset_remove_from_next_clones(ds->ds_prev,
428870ff 1091 dsphys->ds_next_snap_obj, tx);
13fe0198 1092 VERIFY0(zap_add_int(mos,
b128c09f 1093 next_clones_obj, dsobj, tx));
34dc7c2f
BB
1094 }
1095 }
1096
1097 /*
1098 * If we have a reference-reservation on this dataset, we will
1099 * need to increase the amount of refreservation being charged
1100 * since our unique space is going to zero.
1101 */
1102 if (ds->ds_reserved) {
428870ff
BB
1103 int64_t delta;
1104 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1105 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
b128c09f 1106 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
428870ff 1107 delta, 0, 0, tx);
34dc7c2f
BB
1108 }
1109
34dc7c2f 1110 dmu_buf_will_dirty(ds->ds_dbuf, tx);
428870ff
BB
1111 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1112 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1113 dsl_deadlist_close(&ds->ds_deadlist);
1114 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1115 dsl_deadlist_add_key(&ds->ds_deadlist,
1116 ds->ds_phys->ds_prev_snap_txg, tx);
1117
34dc7c2f
BB
1118 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1119 ds->ds_phys->ds_prev_snap_obj = dsobj;
b128c09f 1120 ds->ds_phys->ds_prev_snap_txg = crtxg;
34dc7c2f
BB
1121 ds->ds_phys->ds_unique_bytes = 0;
1122 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1123 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
34dc7c2f 1124
13fe0198
MA
1125 VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1126 snapname, 8, 1, &dsobj, tx));
34dc7c2f
BB
1127
1128 if (ds->ds_prev)
13fe0198
MA
1129 dsl_dataset_rele(ds->ds_prev, ds);
1130 VERIFY0(dsl_dataset_hold_obj(dp,
b128c09f
BB
1131 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1132
428870ff
BB
1133 dsl_scan_ds_snapshotted(ds, tx);
1134
1135 dsl_dir_snap_cmtime_update(ds->ds_dir);
34dc7c2f 1136
6f1ffb06 1137 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
34dc7c2f
BB
1138}
1139
13fe0198
MA
1140static void
1141dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1142{
13fe0198
MA
1143 dsl_dataset_snapshot_arg_t *ddsa = arg;
1144 dsl_pool_t *dp = dmu_tx_pool(tx);
1145 nvpair_t *pair;
34dc7c2f 1146
13fe0198
MA
1147 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1148 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1149 dsl_dataset_t *ds;
1150 char *name, *atp;
1151 char dsname[MAXNAMELEN];
1152
1153 name = nvpair_name(pair);
1154 atp = strchr(name, '@');
1155 (void) strlcpy(dsname, name, atp - name + 1);
1156 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1157
1158 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1159 if (ddsa->ddsa_props != NULL) {
1160 dsl_props_set_sync_impl(ds->ds_prev,
1161 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1162 }
1163 dsl_dataset_rele(ds, FTAG);
1164 }
34dc7c2f
BB
1165}
1166
13fe0198
MA
1167/*
1168 * The snapshots must all be in the same pool.
1169 * All-or-nothing: if there are any failures, nothing will be modified.
1170 */
1171int
1172dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
330d06f9 1173{
13fe0198
MA
1174 dsl_dataset_snapshot_arg_t ddsa;
1175 nvpair_t *pair;
1176 boolean_t needsuspend;
1177 int error;
1178 spa_t *spa;
1179 char *firstname;
1180 nvlist_t *suspended = NULL;
330d06f9 1181
13fe0198
MA
1182 pair = nvlist_next_nvpair(snaps, NULL);
1183 if (pair == NULL)
1184 return (0);
1185 firstname = nvpair_name(pair);
1186
1187 error = spa_open(firstname, &spa, FTAG);
1188 if (error != 0)
1189 return (error);
1190 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1191 spa_close(spa, FTAG);
1192
1193 if (needsuspend) {
1194 suspended = fnvlist_alloc();
1195 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1196 pair = nvlist_next_nvpair(snaps, pair)) {
1197 char fsname[MAXNAMELEN];
1198 char *snapname = nvpair_name(pair);
1199 char *atp;
1200 void *cookie;
1201
1202 atp = strchr(snapname, '@');
1203 if (atp == NULL) {
2e528b49 1204 error = SET_ERROR(EINVAL);
13fe0198
MA
1205 break;
1206 }
1207 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1208
1209 error = zil_suspend(fsname, &cookie);
1210 if (error != 0)
1211 break;
1212 fnvlist_add_uint64(suspended, fsname,
1213 (uintptr_t)cookie);
1214 }
1215 }
1216
1217 ddsa.ddsa_snaps = snaps;
1218 ddsa.ddsa_props = props;
1219 ddsa.ddsa_errors = errors;
1220
1221 if (error == 0) {
1222 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1223 dsl_dataset_snapshot_sync, &ddsa,
1224 fnvlist_num_pairs(snaps) * 3);
1225 }
1226
1227 if (suspended != NULL) {
1228 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1229 pair = nvlist_next_nvpair(suspended, pair)) {
1230 zil_resume((void *)(uintptr_t)
1231 fnvpair_value_uint64(pair));
1232 }
1233 fnvlist_free(suspended);
1234 }
1235
1236 return (error);
1237}
1238
1239typedef struct dsl_dataset_snapshot_tmp_arg {
1240 const char *ddsta_fsname;
1241 const char *ddsta_snapname;
1242 minor_t ddsta_cleanup_minor;
1243 const char *ddsta_htag;
1244} dsl_dataset_snapshot_tmp_arg_t;
1245
1246static int
1247dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1248{
1249 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1250 dsl_pool_t *dp = dmu_tx_pool(tx);
1251 dsl_dataset_t *ds;
1252 int error;
1253
1254 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1255 if (error != 0)
1256 return (error);
1257
96c2e961
KW
1258 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1259 tx, B_FALSE);
13fe0198
MA
1260 if (error != 0) {
1261 dsl_dataset_rele(ds, FTAG);
1262 return (error);
1263 }
1264
1265 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1266 dsl_dataset_rele(ds, FTAG);
2e528b49 1267 return (SET_ERROR(ENOTSUP));
13fe0198
MA
1268 }
1269 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1270 B_TRUE, tx);
1271 if (error != 0) {
1272 dsl_dataset_rele(ds, FTAG);
1273 return (error);
1274 }
1275
1276 dsl_dataset_rele(ds, FTAG);
1277 return (0);
1278}
1279
1280static void
1281dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1282{
1283 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1284 dsl_pool_t *dp = dmu_tx_pool(tx);
1285 dsl_dataset_t *ds;
1286
1287 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1288
1289 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1290 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1291 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1292 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1293
1294 dsl_dataset_rele(ds, FTAG);
1295}
1296
1297int
1298dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1299 minor_t cleanup_minor, const char *htag)
1300{
1301 dsl_dataset_snapshot_tmp_arg_t ddsta;
1302 int error;
1303 spa_t *spa;
1304 boolean_t needsuspend;
1305 void *cookie;
1306
1307 ddsta.ddsta_fsname = fsname;
1308 ddsta.ddsta_snapname = snapname;
1309 ddsta.ddsta_cleanup_minor = cleanup_minor;
1310 ddsta.ddsta_htag = htag;
1311
1312 error = spa_open(fsname, &spa, FTAG);
1313 if (error != 0)
1314 return (error);
1315 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1316 spa_close(spa, FTAG);
1317
1318 if (needsuspend) {
1319 error = zil_suspend(fsname, &cookie);
1320 if (error != 0)
1321 return (error);
1322 }
1323
1324 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1325 dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1326
1327 if (needsuspend)
1328 zil_resume(cookie);
1329 return (error);
1330}
1331
1332
1333void
1334dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1335{
1336 ASSERT(dmu_tx_is_syncing(tx));
1337 ASSERT(ds->ds_objset != NULL);
1338 ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1339
1340 /*
1341 * in case we had to change ds_fsid_guid when we opened it,
1342 * sync it out now.
1343 */
1344 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1345 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1346
1347 dmu_objset_sync(ds->ds_objset, zio, tx);
1348}
1349
1350static void
1351get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1352{
1353 uint64_t count = 0;
1354 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1355 zap_cursor_t zc;
1356 zap_attribute_t za;
1357 nvlist_t *propval = fnvlist_alloc();
1358 nvlist_t *val = fnvlist_alloc();
1359
1360 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
330d06f9
MA
1361
1362 /*
13fe0198 1363 * There may be missing entries in ds_next_clones_obj
330d06f9
MA
1364 * due to a bug in a previous version of the code.
1365 * Only trust it if it has the right number of entries.
1366 */
1367 if (ds->ds_phys->ds_next_clones_obj != 0) {
13fe0198 1368 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
330d06f9
MA
1369 &count));
1370 }
13fe0198 1371 if (count != ds->ds_phys->ds_num_children - 1)
330d06f9 1372 goto fail;
330d06f9
MA
1373 for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1374 zap_cursor_retrieve(&zc, &za) == 0;
1375 zap_cursor_advance(&zc)) {
1376 dsl_dataset_t *clone;
1377 char buf[ZFS_MAXNAMELEN];
13fe0198
MA
1378 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1379 za.za_first_integer, FTAG, &clone));
330d06f9 1380 dsl_dir_name(clone->ds_dir, buf);
13fe0198 1381 fnvlist_add_boolean(val, buf);
330d06f9
MA
1382 dsl_dataset_rele(clone, FTAG);
1383 }
1384 zap_cursor_fini(&zc);
13fe0198
MA
1385 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1386 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
330d06f9
MA
1387fail:
1388 nvlist_free(val);
1389 nvlist_free(propval);
330d06f9
MA
1390}
1391
34dc7c2f
BB
1392void
1393dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1394{
f5fc4aca 1395 uint64_t refd, avail, uobjs, aobjs, ratio;
13fe0198
MA
1396 ASSERTV(dsl_pool_t *dp = ds->ds_dir->dd_pool);
1397
1398 ASSERT(dsl_pool_config_held(dp));
34dc7c2f 1399
6f1ffb06
MA
1400 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1401 (ds->ds_phys->ds_uncompressed_bytes * 100 /
1402 ds->ds_phys->ds_compressed_bytes);
1403
1404 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
24a64651
MA
1405 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1406 ds->ds_phys->ds_uncompressed_bytes);
6f1ffb06
MA
1407
1408 if (dsl_dataset_is_snapshot(ds)) {
1409 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1410 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1411 ds->ds_phys->ds_unique_bytes);
1412 get_clones_stat(ds, nv);
1413 } else {
1414 dsl_dir_stats(ds->ds_dir, nv);
1415 }
34dc7c2f
BB
1416
1417 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1418 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1419 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1420
1421 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1422 ds->ds_phys->ds_creation_time);
1423 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1424 ds->ds_phys->ds_creation_txg);
1425 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1426 ds->ds_quota);
1427 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1428 ds->ds_reserved);
b128c09f
BB
1429 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1430 ds->ds_phys->ds_guid);
428870ff
BB
1431 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1432 ds->ds_phys->ds_unique_bytes);
1433 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1434 ds->ds_object);
1435 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1436 ds->ds_userrefs);
45d1cae3
BB
1437 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1438 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
34dc7c2f 1439
330d06f9
MA
1440 if (ds->ds_phys->ds_prev_snap_obj != 0) {
1441 uint64_t written, comp, uncomp;
1442 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1443 dsl_dataset_t *prev;
1444 int err;
1445
330d06f9
MA
1446 err = dsl_dataset_hold_obj(dp,
1447 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
330d06f9
MA
1448 if (err == 0) {
1449 err = dsl_dataset_space_written(prev, ds, &written,
1450 &comp, &uncomp);
1451 dsl_dataset_rele(prev, FTAG);
1452 if (err == 0) {
1453 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1454 written);
1455 }
1456 }
1457 }
1458
34dc7c2f
BB
1459}
1460
1461void
1462dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1463{
13fe0198
MA
1464 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1465 ASSERT(dsl_pool_config_held(dp));
1466
34dc7c2f
BB
1467 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1468 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1469 stat->dds_guid = ds->ds_phys->ds_guid;
6f1ffb06
MA
1470 stat->dds_origin[0] = '\0';
1471 if (dsl_dataset_is_snapshot(ds)) {
34dc7c2f
BB
1472 stat->dds_is_snapshot = B_TRUE;
1473 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
fb5f0bc8
BB
1474 } else {
1475 stat->dds_is_snapshot = B_FALSE;
1476 stat->dds_num_clones = 0;
34dc7c2f 1477
6f1ffb06
MA
1478 if (dsl_dir_is_clone(ds->ds_dir)) {
1479 dsl_dataset_t *ods;
34dc7c2f 1480
13fe0198 1481 VERIFY0(dsl_dataset_hold_obj(dp,
6f1ffb06
MA
1482 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1483 dsl_dataset_name(ods, stat->dds_origin);
13fe0198 1484 dsl_dataset_rele(ods, FTAG);
6f1ffb06 1485 }
34dc7c2f 1486 }
34dc7c2f
BB
1487}
1488
1489uint64_t
1490dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1491{
1492 return (ds->ds_fsid_guid);
1493}
1494
1495void
1496dsl_dataset_space(dsl_dataset_t *ds,
1497 uint64_t *refdbytesp, uint64_t *availbytesp,
1498 uint64_t *usedobjsp, uint64_t *availobjsp)
1499{
9ae529ec 1500 *refdbytesp = ds->ds_phys->ds_referenced_bytes;
34dc7c2f
BB
1501 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1502 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1503 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1504 if (ds->ds_quota != 0) {
1505 /*
1506 * Adjust available bytes according to refquota
1507 */
1508 if (*refdbytesp < ds->ds_quota)
1509 *availbytesp = MIN(*availbytesp,
1510 ds->ds_quota - *refdbytesp);
1511 else
1512 *availbytesp = 0;
1513 }
1514 *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1515 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1516}
1517
1518boolean_t
19580676 1519dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
34dc7c2f 1520{
19580676
MA
1521 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1522 if (snap == NULL)
34dc7c2f
BB
1523 return (B_FALSE);
1524 if (ds->ds_phys->ds_bp.blk_birth >
19580676
MA
1525 snap->ds_phys->ds_creation_txg) {
1526 objset_t *os, *os_snap;
572e2857
BB
1527 /*
1528 * It may be that only the ZIL differs, because it was
1529 * reset in the head. Don't count that as being
1530 * modified.
1531 */
1532 if (dmu_objset_from_ds(ds, &os) != 0)
1533 return (B_TRUE);
19580676 1534 if (dmu_objset_from_ds(snap, &os_snap) != 0)
572e2857
BB
1535 return (B_TRUE);
1536 return (bcmp(&os->os_phys->os_meta_dnode,
19580676 1537 &os_snap->os_phys->os_meta_dnode,
572e2857
BB
1538 sizeof (os->os_phys->os_meta_dnode)) != 0);
1539 }
34dc7c2f
BB
1540 return (B_FALSE);
1541}
1542
13fe0198
MA
1543typedef struct dsl_dataset_rename_snapshot_arg {
1544 const char *ddrsa_fsname;
1545 const char *ddrsa_oldsnapname;
1546 const char *ddrsa_newsnapname;
1547 boolean_t ddrsa_recursive;
1548 dmu_tx_t *ddrsa_tx;
1549} dsl_dataset_rename_snapshot_arg_t;
1550
34dc7c2f
BB
1551/* ARGSUSED */
1552static int
13fe0198
MA
1553dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1554 dsl_dataset_t *hds, void *arg)
34dc7c2f 1555{
13fe0198
MA
1556 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1557 int error;
34dc7c2f 1558 uint64_t val;
34dc7c2f 1559
13fe0198
MA
1560 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1561 if (error != 0) {
1562 /* ignore nonexistent snapshots */
1563 return (error == ENOENT ? 0 : error);
1564 }
34dc7c2f 1565
13fe0198
MA
1566 /* new name should not exist */
1567 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1568 if (error == 0)
2e528b49 1569 error = SET_ERROR(EEXIST);
13fe0198
MA
1570 else if (error == ENOENT)
1571 error = 0;
34dc7c2f
BB
1572
1573 /* dataset name + 1 for the "@" + the new snapshot name must fit */
13fe0198
MA
1574 if (dsl_dir_namelen(hds->ds_dir) + 1 +
1575 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
2e528b49 1576 error = SET_ERROR(ENAMETOOLONG);
34dc7c2f 1577
13fe0198 1578 return (error);
34dc7c2f
BB
1579}
1580
13fe0198
MA
1581static int
1582dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1583{
13fe0198
MA
1584 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1585 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 1586 dsl_dataset_t *hds;
13fe0198 1587 int error;
34dc7c2f 1588
13fe0198
MA
1589 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1590 if (error != 0)
1591 return (error);
34dc7c2f 1592
13fe0198
MA
1593 if (ddrsa->ddrsa_recursive) {
1594 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1595 dsl_dataset_rename_snapshot_check_impl, ddrsa,
1596 DS_FIND_CHILDREN);
1597 } else {
1598 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1599 }
b128c09f 1600 dsl_dataset_rele(hds, FTAG);
13fe0198 1601 return (error);
34dc7c2f
BB
1602}
1603
34dc7c2f 1604static int
13fe0198
MA
1605dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1606 dsl_dataset_t *hds, void *arg)
34dc7c2f 1607{
13fe0198
MA
1608 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1609 dsl_dataset_t *ds;
1610 uint64_t val;
1611 dmu_tx_t *tx = ddrsa->ddrsa_tx;
1612 int error;
34dc7c2f 1613
13fe0198
MA
1614 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1615 ASSERT(error == 0 || error == ENOENT);
1616 if (error == ENOENT) {
1617 /* ignore nonexistent snapshots */
1618 return (0);
34dc7c2f
BB
1619 }
1620
13fe0198
MA
1621 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1622
1623 /* log before we change the name */
1624 spa_history_log_internal_ds(ds, "rename", tx,
1625 "-> @%s", ddrsa->ddrsa_newsnapname);
34dc7c2f 1626
13fe0198
MA
1627 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1628 mutex_enter(&ds->ds_lock);
1629 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1630 mutex_exit(&ds->ds_lock);
1631 VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1632 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
34dc7c2f 1633
13fe0198 1634 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
1635 return (0);
1636}
1637
13fe0198
MA
1638static void
1639dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1640{
13fe0198
MA
1641 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1642 dsl_pool_t *dp = dmu_tx_pool(tx);
1643 dsl_dataset_t *hds;
34dc7c2f 1644
13fe0198
MA
1645 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1646 ddrsa->ddrsa_tx = tx;
1647 if (ddrsa->ddrsa_recursive) {
1648 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1649 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1650 DS_FIND_CHILDREN));
1651 } else {
1652 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
34dc7c2f 1653 }
13fe0198 1654 dsl_dataset_rele(hds, FTAG);
34dc7c2f
BB
1655}
1656
13fe0198
MA
1657int
1658dsl_dataset_rename_snapshot(const char *fsname,
1659 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
34dc7c2f 1660{
13fe0198 1661 dsl_dataset_rename_snapshot_arg_t ddrsa;
34dc7c2f 1662
13fe0198
MA
1663 ddrsa.ddrsa_fsname = fsname;
1664 ddrsa.ddrsa_oldsnapname = oldsnapname;
1665 ddrsa.ddrsa_newsnapname = newsnapname;
1666 ddrsa.ddrsa_recursive = recursive;
34dc7c2f 1667
13fe0198
MA
1668 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1669 dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
34dc7c2f
BB
1670}
1671
831baf06
KW
1672/*
1673 * If we're doing an ownership handoff, we need to make sure that there is
1674 * only one long hold on the dataset. We're not allowed to change anything here
1675 * so we don't permanently release the long hold or regular hold here. We want
1676 * to do this only when syncing to avoid the dataset unexpectedly going away
1677 * when we release the long hold.
1678 */
1679static int
1680dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
1681{
1682 boolean_t held;
1683
1684 if (!dmu_tx_is_syncing(tx))
1685 return (0);
1686
1687 if (owner != NULL) {
1688 VERIFY3P(ds->ds_owner, ==, owner);
1689 dsl_dataset_long_rele(ds, owner);
1690 }
1691
1692 held = dsl_dataset_long_held(ds);
1693
1694 if (owner != NULL)
1695 dsl_dataset_long_hold(ds, owner);
1696
1697 if (held)
1698 return (SET_ERROR(EBUSY));
1699
1700 return (0);
1701}
1702
1703typedef struct dsl_dataset_rollback_arg {
1704 const char *ddra_fsname;
1705 void *ddra_owner;
46ba1e59 1706 nvlist_t *ddra_result;
831baf06
KW
1707} dsl_dataset_rollback_arg_t;
1708
13fe0198
MA
1709static int
1710dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1711{
831baf06 1712 dsl_dataset_rollback_arg_t *ddra = arg;
13fe0198 1713 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 1714 dsl_dataset_t *ds;
13fe0198
MA
1715 int64_t unused_refres_delta;
1716 int error;
34dc7c2f 1717
831baf06 1718 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
13fe0198
MA
1719 if (error != 0)
1720 return (error);
428870ff 1721
13fe0198
MA
1722 /* must not be a snapshot */
1723 if (dsl_dataset_is_snapshot(ds)) {
1724 dsl_dataset_rele(ds, FTAG);
2e528b49 1725 return (SET_ERROR(EINVAL));
13fe0198 1726 }
34dc7c2f 1727
13fe0198
MA
1728 /* must have a most recent snapshot */
1729 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1730 dsl_dataset_rele(ds, FTAG);
2e528b49 1731 return (SET_ERROR(EINVAL));
13fe0198 1732 }
34dc7c2f 1733
831baf06
KW
1734 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
1735 if (error != 0) {
13fe0198 1736 dsl_dataset_rele(ds, FTAG);
831baf06 1737 return (error);
34dc7c2f 1738 }
428870ff 1739
13fe0198
MA
1740 /*
1741 * Check if the snap we are rolling back to uses more than
1742 * the refquota.
1743 */
1744 if (ds->ds_quota != 0 &&
1745 ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1746 dsl_dataset_rele(ds, FTAG);
2e528b49 1747 return (SET_ERROR(EDQUOT));
34dc7c2f
BB
1748 }
1749
13fe0198
MA
1750 /*
1751 * When we do the clone swap, we will temporarily use more space
1752 * due to the refreservation (the head will no longer have any
1753 * unique space, so the entire amount of the refreservation will need
1754 * to be free). We will immediately destroy the clone, freeing
1755 * this space, but the freeing happens over many txg's.
1756 */
1757 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1758 ds->ds_phys->ds_unique_bytes);
34dc7c2f 1759
13fe0198
MA
1760 if (unused_refres_delta > 0 &&
1761 unused_refres_delta >
1762 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1763 dsl_dataset_rele(ds, FTAG);
2e528b49 1764 return (SET_ERROR(ENOSPC));
13fe0198 1765 }
34dc7c2f 1766
13fe0198
MA
1767 dsl_dataset_rele(ds, FTAG);
1768 return (0);
1769}
34dc7c2f 1770
13fe0198
MA
1771static void
1772dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1773{
831baf06 1774 dsl_dataset_rollback_arg_t *ddra = arg;
13fe0198
MA
1775 dsl_pool_t *dp = dmu_tx_pool(tx);
1776 dsl_dataset_t *ds, *clone;
1777 uint64_t cloneobj;
46ba1e59 1778 char namebuf[ZFS_MAXNAMELEN];
34dc7c2f 1779
831baf06 1780 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
34dc7c2f 1781
46ba1e59
MA
1782 dsl_dataset_name(ds->ds_prev, namebuf);
1783 fnvlist_add_string(ddra->ddra_result, "target", namebuf);
1784
13fe0198
MA
1785 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1786 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1787
1788 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1789
1790 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1791 dsl_dataset_zero_zil(ds, tx);
1792
1793 dsl_destroy_head_sync_impl(clone, tx);
1794
1795 dsl_dataset_rele(clone, FTAG);
1796 dsl_dataset_rele(ds, FTAG);
1797}
1798
831baf06 1799/*
46ba1e59
MA
1800 * Rolls back the given filesystem or volume to the most recent snapshot.
1801 * The name of the most recent snapshot will be returned under key "target"
1802 * in the result nvlist.
831baf06 1803 *
46ba1e59 1804 * If owner != NULL:
831baf06
KW
1805 * - The existing dataset MUST be owned by the specified owner at entry
1806 * - Upon return, dataset will still be held by the same owner, whether we
1807 * succeed or not.
1808 *
1809 * This mode is required any time the existing filesystem is mounted. See
1810 * notes above zfs_suspend_fs() for further details.
1811 */
13fe0198 1812int
46ba1e59 1813dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
13fe0198 1814{
831baf06
KW
1815 dsl_dataset_rollback_arg_t ddra;
1816
1817 ddra.ddra_fsname = fsname;
1818 ddra.ddra_owner = owner;
46ba1e59 1819 ddra.ddra_result = result;
831baf06 1820
13fe0198 1821 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
46ba1e59 1822 dsl_dataset_rollback_sync, &ddra, 1));
34dc7c2f
BB
1823}
1824
b128c09f
BB
1825struct promotenode {
1826 list_node_t link;
1827 dsl_dataset_t *ds;
1828};
1829
13fe0198
MA
1830typedef struct dsl_dataset_promote_arg {
1831 const char *ddpa_clonename;
1832 dsl_dataset_t *ddpa_clone;
b128c09f 1833 list_t shared_snaps, origin_snaps, clone_snaps;
13fe0198 1834 dsl_dataset_t *origin_origin; /* origin of the origin */
b128c09f 1835 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
428870ff 1836 char *err_ds;
13fe0198 1837} dsl_dataset_promote_arg_t;
34dc7c2f 1838
b128c09f 1839static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
13fe0198
MA
1840static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1841 void *tag);
1842static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
b128c09f 1843
34dc7c2f 1844static int
13fe0198 1845dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1846{
13fe0198
MA
1847 dsl_dataset_promote_arg_t *ddpa = arg;
1848 dsl_pool_t *dp = dmu_tx_pool(tx);
1849 dsl_dataset_t *hds;
1850 struct promotenode *snap;
1851 dsl_dataset_t *origin_ds;
34dc7c2f 1852 int err;
428870ff 1853 uint64_t unused;
34dc7c2f 1854
13fe0198
MA
1855 err = promote_hold(ddpa, dp, FTAG);
1856 if (err != 0)
1857 return (err);
34dc7c2f 1858
13fe0198 1859 hds = ddpa->ddpa_clone;
34dc7c2f 1860
13fe0198
MA
1861 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1862 promote_rele(ddpa, FTAG);
2e528b49 1863 return (SET_ERROR(EXDEV));
13fe0198
MA
1864 }
1865
1866 /*
1867 * Compute and check the amount of space to transfer. Since this is
1868 * so expensive, don't do the preliminary check.
1869 */
1870 if (!dmu_tx_is_syncing(tx)) {
1871 promote_rele(ddpa, FTAG);
1872 return (0);
1873 }
1874
1875 snap = list_head(&ddpa->shared_snaps);
1876 origin_ds = snap->ds;
34dc7c2f
BB
1877
1878 /* compute origin's new unique space */
13fe0198 1879 snap = list_tail(&ddpa->clone_snaps);
b128c09f 1880 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
428870ff
BB
1881 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1882 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
13fe0198 1883 &ddpa->unique, &unused, &unused);
34dc7c2f 1884
b128c09f
BB
1885 /*
1886 * Walk the snapshots that we are moving
1887 *
1888 * Compute space to transfer. Consider the incremental changes
13fe0198 1889 * to used by each snapshot:
b128c09f
BB
1890 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1891 * So each snapshot gave birth to:
1892 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1893 * So a sequence would look like:
1894 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1895 * Which simplifies to:
1896 * uN + kN + kN-1 + ... + k1 + k0
1897 * Note however, if we stop before we reach the ORIGIN we get:
1898 * uN + kN + kN-1 + ... + kM - uM-1
1899 */
13fe0198
MA
1900 ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1901 ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1902 ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1903 for (snap = list_head(&ddpa->shared_snaps); snap;
1904 snap = list_next(&ddpa->shared_snaps, snap)) {
34dc7c2f 1905 uint64_t val, dlused, dlcomp, dluncomp;
b128c09f 1906 dsl_dataset_t *ds = snap->ds;
34dc7c2f 1907
13fe0198
MA
1908 /*
1909 * If there are long holds, we won't be able to evict
1910 * the objset.
1911 */
1912 if (dsl_dataset_long_held(ds)) {
2e528b49 1913 err = SET_ERROR(EBUSY);
13fe0198
MA
1914 goto out;
1915 }
1916
34dc7c2f 1917 /* Check that the snapshot name does not conflict */
13fe0198 1918 VERIFY0(dsl_dataset_get_snapname(ds));
b128c09f 1919 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
428870ff 1920 if (err == 0) {
13fe0198 1921 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2e528b49 1922 err = SET_ERROR(EEXIST);
428870ff
BB
1923 goto out;
1924 }
b128c09f 1925 if (err != ENOENT)
428870ff 1926 goto out;
34dc7c2f 1927
b128c09f 1928 /* The very first snapshot does not have a deadlist */
34dc7c2f 1929 if (ds->ds_phys->ds_prev_snap_obj == 0)
b128c09f 1930 continue;
34dc7c2f 1931
428870ff
BB
1932 dsl_deadlist_space(&ds->ds_deadlist,
1933 &dlused, &dlcomp, &dluncomp);
13fe0198
MA
1934 ddpa->used += dlused;
1935 ddpa->comp += dlcomp;
1936 ddpa->uncomp += dluncomp;
b128c09f 1937 }
34dc7c2f 1938
b128c09f
BB
1939 /*
1940 * If we are a clone of a clone then we never reached ORIGIN,
1941 * so we need to subtract out the clone origin's used space.
1942 */
13fe0198
MA
1943 if (ddpa->origin_origin) {
1944 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
1945 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
1946 ddpa->uncomp -=
1947 ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
34dc7c2f
BB
1948 }
1949
1950 /* Check that there is enough space here */
b128c09f 1951 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
13fe0198
MA
1952 ddpa->used);
1953 if (err != 0)
1954 goto out;
34dc7c2f 1955
b128c09f
BB
1956 /*
1957 * Compute the amounts of space that will be used by snapshots
1958 * after the promotion (for both origin and clone). For each,
1959 * it is the amount of space that will be on all of their
1960 * deadlists (that was not born before their new origin).
1961 */
1962 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1963 uint64_t space;
1964
1965 /*
1966 * Note, typically this will not be a clone of a clone,
428870ff
BB
1967 * so dd_origin_txg will be < TXG_INITIAL, so
1968 * these snaplist_space() -> dsl_deadlist_space_range()
b128c09f
BB
1969 * calls will be fast because they do not have to
1970 * iterate over all bps.
1971 */
13fe0198
MA
1972 snap = list_head(&ddpa->origin_snaps);
1973 err = snaplist_space(&ddpa->shared_snaps,
1974 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
1975 if (err != 0)
1976 goto out;
b128c09f 1977
13fe0198 1978 err = snaplist_space(&ddpa->clone_snaps,
428870ff 1979 snap->ds->ds_dir->dd_origin_txg, &space);
13fe0198
MA
1980 if (err != 0)
1981 goto out;
1982 ddpa->cloneusedsnap += space;
b128c09f
BB
1983 }
1984 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
13fe0198
MA
1985 err = snaplist_space(&ddpa->origin_snaps,
1986 origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
1987 if (err != 0)
1988 goto out;
b128c09f
BB
1989 }
1990
428870ff 1991out:
13fe0198 1992 promote_rele(ddpa, FTAG);
428870ff 1993 return (err);
34dc7c2f
BB
1994}
1995
1996static void
13fe0198 1997dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1998{
13fe0198
MA
1999 dsl_dataset_promote_arg_t *ddpa = arg;
2000 dsl_pool_t *dp = dmu_tx_pool(tx);
2001 dsl_dataset_t *hds;
2002 struct promotenode *snap;
2003 dsl_dataset_t *origin_ds;
b128c09f 2004 dsl_dataset_t *origin_head;
13fe0198 2005 dsl_dir_t *dd;
34dc7c2f 2006 dsl_dir_t *odd = NULL;
b128c09f
BB
2007 uint64_t oldnext_obj;
2008 int64_t delta;
34dc7c2f 2009
13fe0198
MA
2010 VERIFY0(promote_hold(ddpa, dp, FTAG));
2011 hds = ddpa->ddpa_clone;
2012
2013 ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
2014
2015 snap = list_head(&ddpa->shared_snaps);
2016 origin_ds = snap->ds;
2017 dd = hds->ds_dir;
34dc7c2f 2018
13fe0198 2019 snap = list_head(&ddpa->origin_snaps);
b128c09f
BB
2020 origin_head = snap->ds;
2021
34dc7c2f
BB
2022 /*
2023 * We need to explicitly open odd, since origin_ds's dd will be
2024 * changing.
2025 */
13fe0198 2026 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
34dc7c2f
BB
2027 NULL, FTAG, &odd));
2028
b128c09f
BB
2029 /* change origin's next snap */
2030 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2031 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
13fe0198 2032 snap = list_tail(&ddpa->clone_snaps);
b128c09f
BB
2033 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2034 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2035
2036 /* change the origin's next clone */
2037 if (origin_ds->ds_phys->ds_next_clones_obj) {
13fe0198
MA
2038 dsl_dataset_remove_from_next_clones(origin_ds,
2039 snap->ds->ds_object, tx);
2040 VERIFY0(zap_add_int(dp->dp_meta_objset,
b128c09f
BB
2041 origin_ds->ds_phys->ds_next_clones_obj,
2042 oldnext_obj, tx));
2043 }
2044
2045 /* change origin */
2046 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2047 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2048 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
428870ff 2049 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
b128c09f
BB
2050 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2051 odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
428870ff
BB
2052 origin_head->ds_dir->dd_origin_txg =
2053 origin_ds->ds_phys->ds_creation_txg;
2054
2055 /* change dd_clone entries */
2056 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
13fe0198 2057 VERIFY0(zap_remove_int(dp->dp_meta_objset,
428870ff 2058 odd->dd_phys->dd_clones, hds->ds_object, tx));
13fe0198
MA
2059 VERIFY0(zap_add_int(dp->dp_meta_objset,
2060 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
428870ff
BB
2061 hds->ds_object, tx));
2062
13fe0198
MA
2063 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2064 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
428870ff
BB
2065 origin_head->ds_object, tx));
2066 if (dd->dd_phys->dd_clones == 0) {
2067 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2068 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2069 }
13fe0198 2070 VERIFY0(zap_add_int(dp->dp_meta_objset,
428870ff 2071 dd->dd_phys->dd_clones, origin_head->ds_object, tx));
428870ff 2072 }
34dc7c2f 2073
b128c09f 2074 /* move snapshots to this dir */
13fe0198
MA
2075 for (snap = list_head(&ddpa->shared_snaps); snap;
2076 snap = list_next(&ddpa->shared_snaps, snap)) {
b128c09f
BB
2077 dsl_dataset_t *ds = snap->ds;
2078
13fe0198
MA
2079 /*
2080 * Property callbacks are registered to a particular
2081 * dsl_dir. Since ours is changing, evict the objset
2082 * so that they will be unregistered from the old dsl_dir.
2083 */
428870ff
BB
2084 if (ds->ds_objset) {
2085 dmu_objset_evict(ds->ds_objset);
2086 ds->ds_objset = NULL;
b128c09f 2087 }
13fe0198 2088
34dc7c2f 2089 /* move snap name entry */
13fe0198
MA
2090 VERIFY0(dsl_dataset_get_snapname(ds));
2091 VERIFY0(dsl_dataset_snap_remove(origin_head,
b128c09f 2092 ds->ds_snapname, tx));
13fe0198 2093 VERIFY0(zap_add(dp->dp_meta_objset,
34dc7c2f
BB
2094 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2095 8, 1, &ds->ds_object, tx));
428870ff 2096
34dc7c2f
BB
2097 /* change containing dsl_dir */
2098 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2099 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2100 ds->ds_phys->ds_dir_obj = dd->dd_object;
2101 ASSERT3P(ds->ds_dir, ==, odd);
13fe0198
MA
2102 dsl_dir_rele(ds->ds_dir, ds);
2103 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
34dc7c2f
BB
2104 NULL, ds, &ds->ds_dir));
2105
428870ff
BB
2106 /* move any clone references */
2107 if (ds->ds_phys->ds_next_clones_obj &&
2108 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2109 zap_cursor_t zc;
2110 zap_attribute_t za;
2111
2112 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2113 ds->ds_phys->ds_next_clones_obj);
2114 zap_cursor_retrieve(&zc, &za) == 0;
13fe0198
MA
2115 zap_cursor_advance(&zc)) {
2116 dsl_dataset_t *cnds;
2117 uint64_t o;
34dc7c2f 2118
13fe0198
MA
2119 if (za.za_first_integer == oldnext_obj) {
2120 /*
2121 * We've already moved the
2122 * origin's reference.
2123 */
2124 continue;
2125 }
34dc7c2f 2126
13fe0198
MA
2127 VERIFY0(dsl_dataset_hold_obj(dp,
2128 za.za_first_integer, FTAG, &cnds));
2129 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
34dc7c2f 2130
13fe0198
MA
2131 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2132 odd->dd_phys->dd_clones, o, tx));
2133 VERIFY0(zap_add_int(dp->dp_meta_objset,
2134 dd->dd_phys->dd_clones, o, tx));
2135 dsl_dataset_rele(cnds, FTAG);
2136 }
2137 zap_cursor_fini(&zc);
2138 }
34dc7c2f 2139
13fe0198 2140 ASSERT(!dsl_prop_hascb(ds));
34dc7c2f
BB
2141 }
2142
34dc7c2f 2143 /*
13fe0198
MA
2144 * Change space accounting.
2145 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2146 * both be valid, or both be 0 (resulting in delta == 0). This
2147 * is true for each of {clone,origin} independently.
34dc7c2f 2148 */
570827e1 2149
13fe0198
MA
2150 delta = ddpa->cloneusedsnap -
2151 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2152 ASSERT3S(delta, >=, 0);
2153 ASSERT3U(ddpa->used, >=, delta);
2154 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2155 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2156 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
34dc7c2f 2157
13fe0198
MA
2158 delta = ddpa->originusedsnap -
2159 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2160 ASSERT3S(delta, <=, 0);
2161 ASSERT3U(ddpa->used, >=, -delta);
2162 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2163 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2164 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2165
2166 origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2167
2168 /* log history record */
2169 spa_history_log_internal_ds(hds, "promote", tx, "");
2170
2171 dsl_dir_rele(odd, FTAG);
2172 promote_rele(ddpa, FTAG);
34dc7c2f
BB
2173}
2174
13fe0198
MA
2175/*
2176 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2177 * (exclusive) and last_obj (inclusive). The list will be in reverse
2178 * order (last_obj will be the list_head()). If first_obj == 0, do all
2179 * snapshots back to this dataset's origin.
2180 */
34dc7c2f 2181static int
13fe0198
MA
2182snaplist_make(dsl_pool_t *dp,
2183 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
34dc7c2f 2184{
13fe0198 2185 uint64_t obj = last_obj;
34dc7c2f 2186
13fe0198
MA
2187 list_create(l, sizeof (struct promotenode),
2188 offsetof(struct promotenode, link));
34dc7c2f 2189
13fe0198
MA
2190 while (obj != first_obj) {
2191 dsl_dataset_t *ds;
2192 struct promotenode *snap;
2193 int err;
428870ff 2194
13fe0198
MA
2195 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2196 ASSERT(err != ENOENT);
2197 if (err != 0)
2198 return (err);
34dc7c2f 2199
13fe0198
MA
2200 if (first_obj == 0)
2201 first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2202
2203 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2204 snap->ds = ds;
2205 list_insert_tail(l, snap);
2206 obj = ds->ds_phys->ds_prev_snap_obj;
2207 }
34dc7c2f
BB
2208
2209 return (0);
2210}
2211
13fe0198
MA
2212static int
2213snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
34dc7c2f 2214{
13fe0198 2215 struct promotenode *snap;
6f1ffb06 2216
13fe0198
MA
2217 *spacep = 0;
2218 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2219 uint64_t used, comp, uncomp;
2220 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2221 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2222 *spacep += used;
428870ff 2223 }
13fe0198 2224 return (0);
34dc7c2f
BB
2225}
2226
13fe0198
MA
2227static void
2228snaplist_destroy(list_t *l, void *tag)
34dc7c2f 2229{
13fe0198 2230 struct promotenode *snap;
428870ff 2231
13fe0198
MA
2232 if (l == NULL || !list_link_active(&l->list_head))
2233 return;
34dc7c2f 2234
13fe0198
MA
2235 while ((snap = list_tail(l)) != NULL) {
2236 list_remove(l, snap);
2237 dsl_dataset_rele(snap->ds, tag);
2238 kmem_free(snap, sizeof (*snap));
2239 }
2240 list_destroy(l);
34dc7c2f
BB
2241}
2242
2243static int
13fe0198 2244promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
34dc7c2f 2245{
13fe0198
MA
2246 int error;
2247 dsl_dir_t *dd;
2248 struct promotenode *snap;
34dc7c2f 2249
13fe0198
MA
2250 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2251 &ddpa->ddpa_clone);
2252 if (error != 0)
2253 return (error);
2254 dd = ddpa->ddpa_clone->ds_dir;
34dc7c2f 2255
13fe0198
MA
2256 if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2257 !dsl_dir_is_clone(dd)) {
2258 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2e528b49 2259 return (SET_ERROR(EINVAL));
13fe0198 2260 }
34dc7c2f 2261
13fe0198
MA
2262 error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2263 &ddpa->shared_snaps, tag);
2264 if (error != 0)
2265 goto out;
34dc7c2f 2266
13fe0198
MA
2267 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2268 &ddpa->clone_snaps, tag);
2269 if (error != 0)
2270 goto out;
34dc7c2f 2271
13fe0198
MA
2272 snap = list_head(&ddpa->shared_snaps);
2273 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2274 error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2275 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2276 &ddpa->origin_snaps, tag);
2277 if (error != 0)
2278 goto out;
d164b209 2279
13fe0198
MA
2280 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2281 error = dsl_dataset_hold_obj(dp,
2282 snap->ds->ds_dir->dd_phys->dd_origin_obj,
2283 tag, &ddpa->origin_origin);
2284 if (error != 0)
2285 goto out;
d164b209 2286 }
13fe0198
MA
2287out:
2288 if (error != 0)
2289 promote_rele(ddpa, tag);
2290 return (error);
34dc7c2f
BB
2291}
2292
34dc7c2f 2293static void
13fe0198 2294promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
34dc7c2f 2295{
13fe0198
MA
2296 snaplist_destroy(&ddpa->shared_snaps, tag);
2297 snaplist_destroy(&ddpa->clone_snaps, tag);
2298 snaplist_destroy(&ddpa->origin_snaps, tag);
2299 if (ddpa->origin_origin != NULL)
2300 dsl_dataset_rele(ddpa->origin_origin, tag);
2301 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2302}
428870ff 2303
13fe0198
MA
2304/*
2305 * Promote a clone.
2306 *
2307 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2308 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2309 */
2310int
2311dsl_dataset_promote(const char *name, char *conflsnap)
2312{
2313 dsl_dataset_promote_arg_t ddpa = { 0 };
2314 uint64_t numsnaps;
2315 int error;
2316 objset_t *os;
34dc7c2f 2317
13fe0198
MA
2318 /*
2319 * We will modify space proportional to the number of
2320 * snapshots. Compute numsnaps.
2321 */
2322 error = dmu_objset_hold(name, FTAG, &os);
2323 if (error != 0)
2324 return (error);
2325 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2326 dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2327 dmu_objset_rele(os, FTAG);
2328 if (error != 0)
2329 return (error);
34dc7c2f 2330
13fe0198
MA
2331 ddpa.ddpa_clonename = name;
2332 ddpa.err_ds = conflsnap;
6f1ffb06 2333
13fe0198
MA
2334 return (dsl_sync_task(name, dsl_dataset_promote_check,
2335 dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
34dc7c2f
BB
2336}
2337
2338int
13fe0198 2339dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
831baf06 2340 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
34dc7c2f 2341{
13fe0198 2342 int64_t unused_refres_delta;
34dc7c2f 2343
13fe0198
MA
2344 /* they should both be heads */
2345 if (dsl_dataset_is_snapshot(clone) ||
2346 dsl_dataset_is_snapshot(origin_head))
2e528b49 2347 return (SET_ERROR(EINVAL));
428870ff 2348
19580676
MA
2349 /* if we are not forcing, the branch point should be just before them */
2350 if (!force && clone->ds_prev != origin_head->ds_prev)
2e528b49 2351 return (SET_ERROR(EINVAL));
34dc7c2f 2352
13fe0198
MA
2353 /* clone should be the clone (unless they are unrelated) */
2354 if (clone->ds_prev != NULL &&
2355 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
19580676 2356 origin_head->ds_dir != clone->ds_prev->ds_dir)
2e528b49 2357 return (SET_ERROR(EINVAL));
428870ff 2358
13fe0198
MA
2359 /* the clone should be a child of the origin */
2360 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2e528b49 2361 return (SET_ERROR(EINVAL));
45d1cae3 2362
13fe0198 2363 /* origin_head shouldn't be modified unless 'force' */
19580676
MA
2364 if (!force &&
2365 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2e528b49 2366 return (SET_ERROR(ETXTBSY));
572e2857 2367
13fe0198 2368 /* origin_head should have no long holds (e.g. is not mounted) */
831baf06 2369 if (dsl_dataset_handoff_check(origin_head, owner, tx))
2e528b49 2370 return (SET_ERROR(EBUSY));
13fe0198
MA
2371
2372 /* check amount of any unconsumed refreservation */
2373 unused_refres_delta =
2374 (int64_t)MIN(origin_head->ds_reserved,
2375 origin_head->ds_phys->ds_unique_bytes) -
2376 (int64_t)MIN(origin_head->ds_reserved,
2377 clone->ds_phys->ds_unique_bytes);
2378
2379 if (unused_refres_delta > 0 &&
2380 unused_refres_delta >
2381 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2e528b49 2382 return (SET_ERROR(ENOSPC));
13fe0198
MA
2383
2384 /* clone can't be over the head's refquota */
2385 if (origin_head->ds_quota != 0 &&
2386 clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2e528b49 2387 return (SET_ERROR(EDQUOT));
572e2857 2388
13fe0198 2389 return (0);
572e2857
BB
2390}
2391
2392void
13fe0198
MA
2393dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2394 dsl_dataset_t *origin_head, dmu_tx_t *tx)
572e2857 2395{
13fe0198
MA
2396 dsl_pool_t *dp = dmu_tx_pool(tx);
2397 int64_t unused_refres_delta;
428870ff 2398
13fe0198
MA
2399 ASSERT(clone->ds_reserved == 0);
2400 ASSERT(origin_head->ds_quota == 0 ||
2401 clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
19580676 2402 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
45d1cae3 2403
13fe0198
MA
2404 dmu_buf_will_dirty(clone->ds_dbuf, tx);
2405 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
45d1cae3 2406
13fe0198
MA
2407 if (clone->ds_objset != NULL) {
2408 dmu_objset_evict(clone->ds_objset);
2409 clone->ds_objset = NULL;
2410 }
45d1cae3 2411
13fe0198
MA
2412 if (origin_head->ds_objset != NULL) {
2413 dmu_objset_evict(origin_head->ds_objset);
2414 origin_head->ds_objset = NULL;
45d1cae3 2415 }
45d1cae3 2416
13fe0198
MA
2417 unused_refres_delta =
2418 (int64_t)MIN(origin_head->ds_reserved,
2419 origin_head->ds_phys->ds_unique_bytes) -
2420 (int64_t)MIN(origin_head->ds_reserved,
2421 clone->ds_phys->ds_unique_bytes);
2422
2423 /*
2424 * Reset origin's unique bytes, if it exists.
2425 */
2426 if (clone->ds_prev) {
2427 dsl_dataset_t *origin = clone->ds_prev;
2428 uint64_t comp, uncomp;
2429
2430 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2431 dsl_deadlist_space_range(&clone->ds_deadlist,
2432 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2433 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2434 }
2435
2436 /* swap blkptrs */
2437 {
2438 blkptr_t tmp;
2439 tmp = origin_head->ds_phys->ds_bp;
2440 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2441 clone->ds_phys->ds_bp = tmp;
2442 }
2443
2444 /* set dd_*_bytes */
2445 {
2446 int64_t dused, dcomp, duncomp;
2447 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2448 uint64_t odl_used, odl_comp, odl_uncomp;
2449
2450 ASSERT3U(clone->ds_dir->dd_phys->
2451 dd_used_breakdown[DD_USED_SNAP], ==, 0);
2452
2453 dsl_deadlist_space(&clone->ds_deadlist,
2454 &cdl_used, &cdl_comp, &cdl_uncomp);
2455 dsl_deadlist_space(&origin_head->ds_deadlist,
2456 &odl_used, &odl_comp, &odl_uncomp);
428870ff 2457
13fe0198
MA
2458 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2459 (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2460 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2461 (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2462 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2463 cdl_uncomp -
2464 (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
45d1cae3 2465
13fe0198
MA
2466 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2467 dused, dcomp, duncomp, tx);
2468 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2469 -dused, -dcomp, -duncomp, tx);
45d1cae3 2470
45d1cae3 2471 /*
13fe0198
MA
2472 * The difference in the space used by snapshots is the
2473 * difference in snapshot space due to the head's
2474 * deadlist (since that's the only thing that's
2475 * changing that affects the snapused).
45d1cae3 2476 */
13fe0198
MA
2477 dsl_deadlist_space_range(&clone->ds_deadlist,
2478 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2479 &cdl_used, &cdl_comp, &cdl_uncomp);
2480 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2481 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2482 &odl_used, &odl_comp, &odl_uncomp);
2483 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2484 DD_USED_HEAD, DD_USED_SNAP, tx);
45d1cae3 2485 }
45d1cae3 2486
13fe0198
MA
2487 /* swap ds_*_bytes */
2488 SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2489 clone->ds_phys->ds_referenced_bytes);
2490 SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2491 clone->ds_phys->ds_compressed_bytes);
2492 SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2493 clone->ds_phys->ds_uncompressed_bytes);
2494 SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2495 clone->ds_phys->ds_unique_bytes);
45d1cae3 2496
13fe0198
MA
2497 /* apply any parent delta for change in unconsumed refreservation */
2498 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2499 unused_refres_delta, 0, 0, tx);
45d1cae3 2500
13fe0198
MA
2501 /*
2502 * Swap deadlists.
2503 */
2504 dsl_deadlist_close(&clone->ds_deadlist);
2505 dsl_deadlist_close(&origin_head->ds_deadlist);
2506 SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2507 clone->ds_phys->ds_deadlist_obj);
2508 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2509 clone->ds_phys->ds_deadlist_obj);
2510 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2511 origin_head->ds_phys->ds_deadlist_obj);
45d1cae3 2512
13fe0198 2513 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
45d1cae3 2514
13fe0198
MA
2515 spa_history_log_internal_ds(clone, "clone swap", tx,
2516 "parent=%s", origin_head->ds_dir->dd_myname);
45d1cae3
BB
2517}
2518
13fe0198
MA
2519/*
2520 * Given a pool name and a dataset object number in that pool,
2521 * return the name of that dataset.
2522 */
572e2857 2523int
13fe0198 2524dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
572e2857 2525{
13fe0198
MA
2526 dsl_pool_t *dp;
2527 dsl_dataset_t *ds;
572e2857
BB
2528 int error;
2529
13fe0198
MA
2530 error = dsl_pool_hold(pname, FTAG, &dp);
2531 if (error != 0)
2532 return (error);
2533
2534 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2535 if (error == 0) {
2536 dsl_dataset_name(ds, buf);
2537 dsl_dataset_rele(ds, FTAG);
2538 }
2539 dsl_pool_rele(dp, FTAG);
572e2857
BB
2540
2541 return (error);
2542}
2543
45d1cae3 2544int
13fe0198
MA
2545dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2546 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
45d1cae3 2547{
13fe0198 2548 int error = 0;
45d1cae3 2549
13fe0198 2550 ASSERT3S(asize, >, 0);
45d1cae3 2551
13fe0198
MA
2552 /*
2553 * *ref_rsrv is the portion of asize that will come from any
2554 * unconsumed refreservation space.
2555 */
2556 *ref_rsrv = 0;
45d1cae3 2557
13fe0198
MA
2558 mutex_enter(&ds->ds_lock);
2559 /*
2560 * Make a space adjustment for reserved bytes.
2561 */
2562 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2563 ASSERT3U(*used, >=,
2564 ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2565 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2566 *ref_rsrv =
2567 asize - MIN(asize, parent_delta(ds, asize + inflight));
45d1cae3
BB
2568 }
2569
13fe0198
MA
2570 if (!check_quota || ds->ds_quota == 0) {
2571 mutex_exit(&ds->ds_lock);
2572 return (0);
45d1cae3 2573 }
13fe0198
MA
2574 /*
2575 * If they are requesting more space, and our current estimate
2576 * is over quota, they get to try again unless the actual
2577 * on-disk is over quota and there are no pending changes (which
2578 * may free up space for us).
2579 */
2580 if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2581 if (inflight > 0 ||
2582 ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2e528b49 2583 error = SET_ERROR(ERESTART);
13fe0198 2584 else
2e528b49 2585 error = SET_ERROR(EDQUOT);
45d1cae3 2586 }
13fe0198 2587 mutex_exit(&ds->ds_lock);
45d1cae3 2588
45d1cae3
BB
2589 return (error);
2590}
2591
13fe0198
MA
2592typedef struct dsl_dataset_set_qr_arg {
2593 const char *ddsqra_name;
2594 zprop_source_t ddsqra_source;
2595 uint64_t ddsqra_value;
2596} dsl_dataset_set_qr_arg_t;
45d1cae3 2597
13fe0198
MA
2598
2599/* ARGSUSED */
45d1cae3 2600static int
13fe0198 2601dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
45d1cae3 2602{
13fe0198
MA
2603 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2604 dsl_pool_t *dp = dmu_tx_pool(tx);
2605 dsl_dataset_t *ds;
45d1cae3 2606 int error;
13fe0198 2607 uint64_t newval;
45d1cae3 2608
13fe0198 2609 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2e528b49 2610 return (SET_ERROR(ENOTSUP));
45d1cae3 2611
13fe0198
MA
2612 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2613 if (error != 0)
2614 return (error);
2615
2616 if (dsl_dataset_is_snapshot(ds)) {
2617 dsl_dataset_rele(ds, FTAG);
2e528b49 2618 return (SET_ERROR(EINVAL));
45d1cae3
BB
2619 }
2620
13fe0198
MA
2621 error = dsl_prop_predict(ds->ds_dir,
2622 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2623 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2624 if (error != 0) {
2625 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
2626 return (error);
2627 }
2628
13fe0198
MA
2629 if (newval == 0) {
2630 dsl_dataset_rele(ds, FTAG);
2631 return (0);
2632 }
2633
2634 if (newval < ds->ds_phys->ds_referenced_bytes ||
2635 newval < ds->ds_reserved) {
2636 dsl_dataset_rele(ds, FTAG);
2e528b49 2637 return (SET_ERROR(ENOSPC));
13fe0198 2638 }
45d1cae3 2639
13fe0198 2640 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
2641 return (0);
2642}
2643
13fe0198
MA
2644static void
2645dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
45d1cae3 2646{
13fe0198
MA
2647 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2648 dsl_pool_t *dp = dmu_tx_pool(tx);
2649 dsl_dataset_t *ds;
2650 uint64_t newval;
45d1cae3 2651
13fe0198 2652 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
45d1cae3 2653
13fe0198
MA
2654 dsl_prop_set_sync_impl(ds,
2655 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2656 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2657 &ddsqra->ddsqra_value, tx);
45d1cae3 2658
13fe0198
MA
2659 VERIFY0(dsl_prop_get_int_ds(ds,
2660 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
45d1cae3 2661
13fe0198
MA
2662 if (ds->ds_quota != newval) {
2663 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2664 ds->ds_quota = newval;
45d1cae3 2665 }
13fe0198 2666 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
2667}
2668
13fe0198
MA
2669int
2670dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2671 uint64_t refquota)
45d1cae3 2672{
13fe0198 2673 dsl_dataset_set_qr_arg_t ddsqra;
428870ff 2674
13fe0198
MA
2675 ddsqra.ddsqra_name = dsname;
2676 ddsqra.ddsqra_source = source;
2677 ddsqra.ddsqra_value = refquota;
2678
2679 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2680 dsl_dataset_set_refquota_sync, &ddsqra, 0));
45d1cae3
BB
2681}
2682
2683static int
13fe0198 2684dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
45d1cae3 2685{
13fe0198
MA
2686 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2687 dsl_pool_t *dp = dmu_tx_pool(tx);
45d1cae3
BB
2688 dsl_dataset_t *ds;
2689 int error;
13fe0198 2690 uint64_t newval, unique;
428870ff 2691
13fe0198 2692 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2e528b49 2693 return (SET_ERROR(ENOTSUP));
45d1cae3 2694
13fe0198
MA
2695 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2696 if (error != 0)
45d1cae3 2697 return (error);
45d1cae3 2698
13fe0198
MA
2699 if (dsl_dataset_is_snapshot(ds)) {
2700 dsl_dataset_rele(ds, FTAG);
2e528b49 2701 return (SET_ERROR(EINVAL));
45d1cae3
BB
2702 }
2703
13fe0198
MA
2704 error = dsl_prop_predict(ds->ds_dir,
2705 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2706 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2707 if (error != 0) {
2708 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
2709 return (error);
2710 }
2711
13fe0198
MA
2712 /*
2713 * If we are doing the preliminary check in open context, the
2714 * space estimates may be inaccurate.
2715 */
2716 if (!dmu_tx_is_syncing(tx)) {
2717 dsl_dataset_rele(ds, FTAG);
2718 return (0);
45d1cae3 2719 }
45d1cae3 2720
13fe0198
MA
2721 mutex_enter(&ds->ds_lock);
2722 if (!DS_UNIQUE_IS_ACCURATE(ds))
2723 dsl_dataset_recalc_head_uniq(ds);
2724 unique = ds->ds_phys->ds_unique_bytes;
2725 mutex_exit(&ds->ds_lock);
45d1cae3 2726
13fe0198
MA
2727 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2728 uint64_t delta = MAX(unique, newval) -
2729 MAX(unique, ds->ds_reserved);
45d1cae3 2730
13fe0198
MA
2731 if (delta >
2732 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2733 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2734 dsl_dataset_rele(ds, FTAG);
2e528b49 2735 return (SET_ERROR(ENOSPC));
13fe0198 2736 }
45d1cae3
BB
2737 }
2738
13fe0198
MA
2739 dsl_dataset_rele(ds, FTAG);
2740 return (0);
45d1cae3
BB
2741}
2742
13fe0198
MA
2743void
2744dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2745 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
428870ff 2746{
13fe0198
MA
2747 uint64_t newval;
2748 uint64_t unique;
2749 int64_t delta;
428870ff 2750
13fe0198
MA
2751 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2752 source, sizeof (value), 1, &value, tx);
428870ff 2753
13fe0198
MA
2754 VERIFY0(dsl_prop_get_int_ds(ds,
2755 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
572e2857 2756
13fe0198
MA
2757 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2758 mutex_enter(&ds->ds_dir->dd_lock);
2759 mutex_enter(&ds->ds_lock);
2760 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2761 unique = ds->ds_phys->ds_unique_bytes;
2762 delta = MAX(0, (int64_t)(newval - unique)) -
2763 MAX(0, (int64_t)(ds->ds_reserved - unique));
2764 ds->ds_reserved = newval;
2765 mutex_exit(&ds->ds_lock);
572e2857 2766
13fe0198
MA
2767 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2768 mutex_exit(&ds->ds_dir->dd_lock);
428870ff
BB
2769}
2770
13fe0198
MA
2771static void
2772dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
45d1cae3 2773{
13fe0198
MA
2774 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2775 dsl_pool_t *dp = dmu_tx_pool(tx);
45d1cae3 2776 dsl_dataset_t *ds;
45d1cae3 2777
13fe0198
MA
2778 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2779 dsl_dataset_set_refreservation_sync_impl(ds,
2780 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
45d1cae3 2781 dsl_dataset_rele(ds, FTAG);
45d1cae3 2782}
428870ff 2783
428870ff 2784int
13fe0198
MA
2785dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2786 uint64_t refreservation)
428870ff 2787{
13fe0198 2788 dsl_dataset_set_qr_arg_t ddsqra;
428870ff 2789
13fe0198
MA
2790 ddsqra.ddsqra_name = dsname;
2791 ddsqra.ddsqra_source = source;
2792 ddsqra.ddsqra_value = refreservation;
c28b2279 2793
13fe0198
MA
2794 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2795 dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2796}
330d06f9
MA
2797
2798/*
2799 * Return (in *usedp) the amount of space written in new that is not
2800 * present in oldsnap. New may be a snapshot or the head. Old must be
2801 * a snapshot before new, in new's filesystem (or its origin). If not then
2802 * fail and return EINVAL.
2803 *
2804 * The written space is calculated by considering two components: First, we
2805 * ignore any freed space, and calculate the written as new's used space
2806 * minus old's used space. Next, we add in the amount of space that was freed
2807 * between the two snapshots, thus reducing new's used space relative to old's.
2808 * Specifically, this is the space that was born before old->ds_creation_txg,
2809 * and freed before new (ie. on new's deadlist or a previous deadlist).
2810 *
2811 * space freed [---------------------]
2812 * snapshots ---O-------O--------O-------O------
2813 * oldsnap new
2814 */
2815int
2816dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2817 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2818{
2819 int err = 0;
2820 uint64_t snapobj;
2821 dsl_pool_t *dp = new->ds_dir->dd_pool;
2822
13fe0198
MA
2823 ASSERT(dsl_pool_config_held(dp));
2824
330d06f9 2825 *usedp = 0;
9ae529ec
CS
2826 *usedp += new->ds_phys->ds_referenced_bytes;
2827 *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
330d06f9
MA
2828
2829 *compp = 0;
2830 *compp += new->ds_phys->ds_compressed_bytes;
2831 *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2832
2833 *uncompp = 0;
2834 *uncompp += new->ds_phys->ds_uncompressed_bytes;
2835 *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2836
330d06f9
MA
2837 snapobj = new->ds_object;
2838 while (snapobj != oldsnap->ds_object) {
2839 dsl_dataset_t *snap;
2840 uint64_t used, comp, uncomp;
2841
9ae529ec
CS
2842 if (snapobj == new->ds_object) {
2843 snap = new;
2844 } else {
2845 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2846 if (err != 0)
2847 break;
2848 }
330d06f9
MA
2849
2850 if (snap->ds_phys->ds_prev_snap_txg ==
2851 oldsnap->ds_phys->ds_creation_txg) {
2852 /*
2853 * The blocks in the deadlist can not be born after
2854 * ds_prev_snap_txg, so get the whole deadlist space,
2855 * which is more efficient (especially for old-format
2856 * deadlists). Unfortunately the deadlist code
2857 * doesn't have enough information to make this
2858 * optimization itself.
2859 */
2860 dsl_deadlist_space(&snap->ds_deadlist,
2861 &used, &comp, &uncomp);
2862 } else {
2863 dsl_deadlist_space_range(&snap->ds_deadlist,
2864 0, oldsnap->ds_phys->ds_creation_txg,
2865 &used, &comp, &uncomp);
2866 }
2867 *usedp += used;
2868 *compp += comp;
2869 *uncompp += uncomp;
2870
2871 /*
2872 * If we get to the beginning of the chain of snapshots
2873 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2874 * was not a snapshot of/before new.
2875 */
2876 snapobj = snap->ds_phys->ds_prev_snap_obj;
9ae529ec
CS
2877 if (snap != new)
2878 dsl_dataset_rele(snap, FTAG);
330d06f9 2879 if (snapobj == 0) {
2e528b49 2880 err = SET_ERROR(EINVAL);
330d06f9
MA
2881 break;
2882 }
2883
2884 }
330d06f9
MA
2885 return (err);
2886}
2887
2888/*
2889 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2890 * lastsnap, and all snapshots in between are deleted.
2891 *
2892 * blocks that would be freed [---------------------------]
2893 * snapshots ---O-------O--------O-------O--------O
2894 * firstsnap lastsnap
2895 *
2896 * This is the set of blocks that were born after the snap before firstsnap,
2897 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2898 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2899 * We calculate this by iterating over the relevant deadlists (from the snap
2900 * after lastsnap, backward to the snap after firstsnap), summing up the
2901 * space on the deadlist that was born after the snap before firstsnap.
2902 */
2903int
2904dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2905 dsl_dataset_t *lastsnap,
2906 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2907{
2908 int err = 0;
2909 uint64_t snapobj;
2910 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
2911
2912 ASSERT(dsl_dataset_is_snapshot(firstsnap));
2913 ASSERT(dsl_dataset_is_snapshot(lastsnap));
2914
2915 /*
2916 * Check that the snapshots are in the same dsl_dir, and firstsnap
2917 * is before lastsnap.
2918 */
2919 if (firstsnap->ds_dir != lastsnap->ds_dir ||
2920 firstsnap->ds_phys->ds_creation_txg >
2921 lastsnap->ds_phys->ds_creation_txg)
2e528b49 2922 return (SET_ERROR(EINVAL));
330d06f9
MA
2923
2924 *usedp = *compp = *uncompp = 0;
2925
330d06f9
MA
2926 snapobj = lastsnap->ds_phys->ds_next_snap_obj;
2927 while (snapobj != firstsnap->ds_object) {
2928 dsl_dataset_t *ds;
2929 uint64_t used, comp, uncomp;
2930
2931 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
2932 if (err != 0)
2933 break;
2934
2935 dsl_deadlist_space_range(&ds->ds_deadlist,
2936 firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2937 &used, &comp, &uncomp);
2938 *usedp += used;
2939 *compp += comp;
2940 *uncompp += uncomp;
2941
2942 snapobj = ds->ds_phys->ds_prev_snap_obj;
2943 ASSERT3U(snapobj, !=, 0);
2944 dsl_dataset_rele(ds, FTAG);
2945 }
330d06f9
MA
2946 return (err);
2947}
2948
13fe0198
MA
2949/*
2950 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
2951 * For example, they could both be snapshots of the same filesystem, and
2952 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
2953 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
2954 * filesystem. Or 'earlier' could be the origin's origin.
2955 */
2956boolean_t
2957dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
2958{
2959 dsl_pool_t *dp = later->ds_dir->dd_pool;
2960 int error;
2961 boolean_t ret;
2962 dsl_dataset_t *origin;
2963
2964 ASSERT(dsl_pool_config_held(dp));
2965
2966 if (earlier->ds_phys->ds_creation_txg >=
2967 later->ds_phys->ds_creation_txg)
2968 return (B_FALSE);
2969
2970 if (later->ds_dir == earlier->ds_dir)
2971 return (B_TRUE);
2972 if (!dsl_dir_is_clone(later->ds_dir))
2973 return (B_FALSE);
2974
2975 if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
2976 return (B_TRUE);
2977 error = dsl_dataset_hold_obj(dp,
2978 later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
2979 if (error != 0)
2980 return (B_FALSE);
2981 ret = dsl_dataset_is_before(origin, earlier);
2982 dsl_dataset_rele(origin, FTAG);
2983 return (ret);
2984}
2985
c28b2279
BB
2986#if defined(_KERNEL) && defined(HAVE_SPL)
2987EXPORT_SYMBOL(dsl_dataset_hold);
2988EXPORT_SYMBOL(dsl_dataset_hold_obj);
2989EXPORT_SYMBOL(dsl_dataset_own);
2990EXPORT_SYMBOL(dsl_dataset_own_obj);
2991EXPORT_SYMBOL(dsl_dataset_name);
2992EXPORT_SYMBOL(dsl_dataset_rele);
2993EXPORT_SYMBOL(dsl_dataset_disown);
c28b2279 2994EXPORT_SYMBOL(dsl_dataset_tryown);
c28b2279
BB
2995EXPORT_SYMBOL(dsl_dataset_create_sync);
2996EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
c28b2279
BB
2997EXPORT_SYMBOL(dsl_dataset_snapshot_check);
2998EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
c28b2279 2999EXPORT_SYMBOL(dsl_dataset_promote);
c28b2279
BB
3000EXPORT_SYMBOL(dsl_dataset_user_hold);
3001EXPORT_SYMBOL(dsl_dataset_user_release);
c28b2279
BB
3002EXPORT_SYMBOL(dsl_dataset_get_holds);
3003EXPORT_SYMBOL(dsl_dataset_get_blkptr);
3004EXPORT_SYMBOL(dsl_dataset_set_blkptr);
3005EXPORT_SYMBOL(dsl_dataset_get_spa);
19580676 3006EXPORT_SYMBOL(dsl_dataset_modified_since_snap);
330d06f9
MA
3007EXPORT_SYMBOL(dsl_dataset_space_written);
3008EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
c28b2279
BB
3009EXPORT_SYMBOL(dsl_dataset_sync);
3010EXPORT_SYMBOL(dsl_dataset_block_born);
3011EXPORT_SYMBOL(dsl_dataset_block_kill);
3012EXPORT_SYMBOL(dsl_dataset_block_freeable);
3013EXPORT_SYMBOL(dsl_dataset_prev_snap_txg);
3014EXPORT_SYMBOL(dsl_dataset_dirty);
3015EXPORT_SYMBOL(dsl_dataset_stats);
3016EXPORT_SYMBOL(dsl_dataset_fast_stat);
3017EXPORT_SYMBOL(dsl_dataset_space);
3018EXPORT_SYMBOL(dsl_dataset_fsid_guid);
3019EXPORT_SYMBOL(dsl_dsobj_to_dsname);
3020EXPORT_SYMBOL(dsl_dataset_check_quota);
13fe0198
MA
3021EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
3022EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);
c28b2279 3023#endif