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