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