xref: /freebsd/sys/contrib/openzfs/module/zfs/dsl_bookmark.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
15  * Copyright 2017 Nexenta Systems, Inc.
16  * Copyright 2019, 2020 by Christian Schwarz. All rights reserved.
17  */
18 
19 #include <sys/zfs_context.h>
20 #include <sys/dsl_dataset.h>
21 #include <sys/dsl_dir.h>
22 #include <sys/dsl_prop.h>
23 #include <sys/dsl_synctask.h>
24 #include <sys/dsl_destroy.h>
25 #include <sys/dmu_impl.h>
26 #include <sys/dmu_tx.h>
27 #include <sys/arc.h>
28 #include <sys/zap.h>
29 #include <sys/zfeature.h>
30 #include <sys/spa.h>
31 #include <sys/dsl_bookmark.h>
32 #include <zfs_namecheck.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dbuf.h>
35 
36 static int
dsl_bookmark_hold_ds(dsl_pool_t * dp,const char * fullname,dsl_dataset_t ** dsp,const void * tag,const char ** shortnamep)37 dsl_bookmark_hold_ds(dsl_pool_t *dp, const char *fullname,
38     dsl_dataset_t **dsp, const void *tag, const char **shortnamep)
39 {
40 	char buf[ZFS_MAX_DATASET_NAME_LEN];
41 	const char *hashp;
42 
43 	if (strlen(fullname) >= ZFS_MAX_DATASET_NAME_LEN)
44 		return (SET_ERROR(ENAMETOOLONG));
45 	hashp = strchr(fullname, '#');
46 	if (hashp == NULL)
47 		return (SET_ERROR(EINVAL));
48 
49 	*shortnamep = hashp + 1;
50 	if (zfs_component_namecheck(*shortnamep, NULL, NULL))
51 		return (SET_ERROR(EINVAL));
52 	(void) strlcpy(buf, fullname, hashp - fullname + 1);
53 	return (dsl_dataset_hold(dp, buf, tag, dsp));
54 }
55 
56 /*
57  * When reading BOOKMARK_V1 bookmarks, the BOOKMARK_V2 fields are guaranteed
58  * to be zeroed.
59  *
60  * Returns ESRCH if bookmark is not found.
61  * Note, we need to use the ZAP rather than the AVL to look up bookmarks
62  * by name, because only the ZAP honors the casesensitivity setting.
63  */
64 int
dsl_bookmark_lookup_impl(dsl_dataset_t * ds,const char * shortname,zfs_bookmark_phys_t * bmark_phys)65 dsl_bookmark_lookup_impl(dsl_dataset_t *ds, const char *shortname,
66     zfs_bookmark_phys_t *bmark_phys)
67 {
68 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
69 	uint64_t bmark_zapobj = ds->ds_bookmarks_obj;
70 	matchtype_t mt = 0;
71 	int err;
72 
73 	if (bmark_zapobj == 0)
74 		return (SET_ERROR(ESRCH));
75 
76 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
77 		mt = MT_NORMALIZE;
78 
79 	/*
80 	 * Zero out the bookmark in case the one stored on disk
81 	 * is in an older, shorter format.
82 	 */
83 	memset(bmark_phys, 0, sizeof (*bmark_phys));
84 
85 	err = zap_lookup_norm(mos, bmark_zapobj, shortname, sizeof (uint64_t),
86 	    sizeof (*bmark_phys) / sizeof (uint64_t), bmark_phys, mt, NULL, 0,
87 	    NULL);
88 
89 	return (err == ENOENT ? SET_ERROR(ESRCH) : err);
90 }
91 
92 /*
93  * If later_ds is non-NULL, this will return EXDEV if the specified bookmark
94  * does not represents an earlier point in later_ds's timeline.  However,
95  * bmp will still be filled in if we return EXDEV.
96  *
97  * Returns ENOENT if the dataset containing the bookmark does not exist.
98  * Returns ESRCH if the dataset exists but the bookmark was not found in it.
99  */
100 int
dsl_bookmark_lookup(dsl_pool_t * dp,const char * fullname,dsl_dataset_t * later_ds,zfs_bookmark_phys_t * bmp)101 dsl_bookmark_lookup(dsl_pool_t *dp, const char *fullname,
102     dsl_dataset_t *later_ds, zfs_bookmark_phys_t *bmp)
103 {
104 	const char *shortname;
105 	dsl_dataset_t *ds;
106 	int error;
107 
108 	error = dsl_bookmark_hold_ds(dp, fullname, &ds, FTAG, &shortname);
109 	if (error != 0)
110 		return (error);
111 
112 	error = dsl_bookmark_lookup_impl(ds, shortname, bmp);
113 	if (error == 0 && later_ds != NULL) {
114 		if (!dsl_dataset_is_before(later_ds, ds, bmp->zbm_creation_txg))
115 			error = SET_ERROR(EXDEV);
116 	}
117 	dsl_dataset_rele(ds, FTAG);
118 	return (error);
119 }
120 
121 /*
122  * Validates that
123  * - bmark is a full dataset path of a bookmark (bookmark_namecheck)
124  * - source is a full path of a snapshot or bookmark
125  *   ({bookmark,snapshot}_namecheck)
126  *
127  * Returns 0 if valid, -1 otherwise.
128  */
129 static int
dsl_bookmark_create_nvl_validate_pair(const char * bmark,const char * source)130 dsl_bookmark_create_nvl_validate_pair(const char *bmark, const char *source)
131 {
132 	if (bookmark_namecheck(bmark, NULL, NULL) != 0)
133 		return (-1);
134 
135 	int is_bmark, is_snap;
136 	is_bmark = bookmark_namecheck(source, NULL, NULL) == 0;
137 	is_snap = snapshot_namecheck(source, NULL, NULL) == 0;
138 	if (!is_bmark && !is_snap)
139 		return (-1);
140 
141 	return (0);
142 }
143 
144 /*
145  * Check that the given nvlist corresponds to the following schema:
146  *  { newbookmark -> source, ... }
147  * where
148  * - each pair passes dsl_bookmark_create_nvl_validate_pair
149  * - all newbookmarks are in the same pool
150  * - all newbookmarks have unique names
151  *
152  * Note that this function is only validates above schema. Callers must ensure
153  * that the bookmarks can be created, e.g. that sources exist.
154  *
155  * Returns 0 if the nvlist adheres to above schema.
156  * Returns -1 if it doesn't.
157  */
158 int
dsl_bookmark_create_nvl_validate(nvlist_t * bmarks)159 dsl_bookmark_create_nvl_validate(nvlist_t *bmarks)
160 {
161 	const char *first = NULL;
162 	size_t first_len = 0;
163 
164 	for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
165 	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
166 
167 		const char *bmark = nvpair_name(pair);
168 		const char *source;
169 
170 		/* list structure: values must be snapshots XOR bookmarks */
171 		if (nvpair_value_string(pair, &source) != 0)
172 			return (-1);
173 		if (dsl_bookmark_create_nvl_validate_pair(bmark, source) != 0)
174 			return (-1);
175 
176 		/* same pool check */
177 		if (first == NULL) {
178 			const char *cp = strpbrk(bmark, "/#");
179 			if (cp == NULL)
180 				return (-1);
181 			first = bmark;
182 			first_len = cp - bmark;
183 		}
184 		if (strncmp(first, bmark, first_len) != 0)
185 			return (-1);
186 		switch (*(bmark + first_len)) {
187 			case '/': /* fallthrough */
188 			case '#':
189 				break;
190 			default:
191 				return (-1);
192 		}
193 
194 		/* unique newbookmark names; todo: O(n^2) */
195 		for (nvpair_t *pair2 = nvlist_next_nvpair(bmarks, pair);
196 		    pair2 != NULL; pair2 = nvlist_next_nvpair(bmarks, pair2)) {
197 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
198 				return (-1);
199 		}
200 
201 	}
202 	return (0);
203 }
204 
205 /*
206  * expects that newbm and source have been validated using
207  * dsl_bookmark_create_nvl_validate_pair
208  */
209 static int
dsl_bookmark_create_check_impl(dsl_pool_t * dp,const char * newbm,const char * source)210 dsl_bookmark_create_check_impl(dsl_pool_t *dp,
211     const char *newbm, const char *source)
212 {
213 	ASSERT0(dsl_bookmark_create_nvl_validate_pair(newbm, source));
214 	/* defer source namecheck until we know it's a snapshot or bookmark */
215 
216 	int error;
217 	dsl_dataset_t *newbm_ds;
218 	const char *newbm_short;
219 	zfs_bookmark_phys_t bmark_phys;
220 
221 	error = dsl_bookmark_hold_ds(dp, newbm, &newbm_ds, FTAG, &newbm_short);
222 	if (error != 0)
223 		return (error);
224 
225 	/* Verify that the new bookmark does not already exist */
226 	error = dsl_bookmark_lookup_impl(newbm_ds, newbm_short, &bmark_phys);
227 	switch (error) {
228 	case ESRCH:
229 		/* happy path: new bmark doesn't exist, proceed after switch */
230 		break;
231 	case 0:
232 		error = SET_ERROR(EEXIST);
233 		goto eholdnewbmds;
234 	default:
235 		/* dsl_bookmark_lookup_impl already did SET_ERROR */
236 		goto eholdnewbmds;
237 	}
238 
239 	/* error is retval of the following if-cascade */
240 	if (strchr(source, '@') != NULL) {
241 		dsl_dataset_t *source_snap_ds;
242 		ASSERT0(snapshot_namecheck(source, NULL, NULL));
243 		error = dsl_dataset_hold(dp, source, FTAG, &source_snap_ds);
244 		if (error == 0) {
245 			VERIFY(source_snap_ds->ds_is_snapshot);
246 			/*
247 			 * Verify that source snapshot is an earlier point in
248 			 * newbm_ds's timeline (source may be newbm_ds's origin)
249 			 */
250 			if (!dsl_dataset_is_before(newbm_ds, source_snap_ds, 0))
251 				error = SET_ERROR(
252 				    ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR);
253 			dsl_dataset_rele(source_snap_ds, FTAG);
254 		}
255 	} else if (strchr(source, '#') != NULL) {
256 		zfs_bookmark_phys_t source_phys;
257 		ASSERT0(bookmark_namecheck(source, NULL, NULL));
258 		/*
259 		 * Source must exists and be an earlier point in newbm_ds's
260 		 * timeline (newbm_ds's origin may be a snap of source's ds)
261 		 */
262 		error = dsl_bookmark_lookup(dp, source, newbm_ds, &source_phys);
263 		switch (error) {
264 		case 0:
265 			break; /* happy path */
266 		case EXDEV:
267 			error = SET_ERROR(ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR);
268 			break;
269 		default:
270 			/* dsl_bookmark_lookup already did SET_ERROR */
271 			break;
272 		}
273 	} else {
274 		/*
275 		 * dsl_bookmark_create_nvl_validate validates that source is
276 		 * either snapshot or bookmark
277 		 */
278 		panic("unreachable code: %s", source);
279 	}
280 
281 eholdnewbmds:
282 	dsl_dataset_rele(newbm_ds, FTAG);
283 	return (error);
284 }
285 
286 int
dsl_bookmark_create_check(void * arg,dmu_tx_t * tx)287 dsl_bookmark_create_check(void *arg, dmu_tx_t *tx)
288 {
289 	dsl_bookmark_create_arg_t *dbca = arg;
290 	int rv = 0;
291 	int schema_err = 0;
292 	ASSERT3P(dbca, !=, NULL);
293 	ASSERT3P(dbca->dbca_bmarks, !=, NULL);
294 	/* dbca->dbca_errors is allowed to be NULL */
295 
296 	dsl_pool_t *dp = dmu_tx_pool(tx);
297 
298 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))
299 		return (SET_ERROR(ENOTSUP));
300 
301 	if (dsl_bookmark_create_nvl_validate(dbca->dbca_bmarks) != 0)
302 		rv = schema_err = SET_ERROR(EINVAL);
303 
304 	for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);
305 	    pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {
306 		const char *new = nvpair_name(pair);
307 
308 		int error = schema_err;
309 		if (error == 0) {
310 			const char *source = fnvpair_value_string(pair);
311 			error = dsl_bookmark_create_check_impl(dp, new, source);
312 			if (error != 0)
313 				error = SET_ERROR(error);
314 		}
315 
316 		if (error != 0) {
317 			rv = error;
318 			if (dbca->dbca_errors != NULL)
319 				fnvlist_add_int32(dbca->dbca_errors,
320 				    new, error);
321 		}
322 	}
323 
324 	return (rv);
325 }
326 
327 static dsl_bookmark_node_t *
dsl_bookmark_node_alloc(const char * shortname)328 dsl_bookmark_node_alloc(const char *shortname)
329 {
330 	dsl_bookmark_node_t *dbn = kmem_alloc(sizeof (*dbn), KM_SLEEP);
331 	dbn->dbn_name = spa_strdup(shortname);
332 	dbn->dbn_dirty = B_FALSE;
333 	mutex_init(&dbn->dbn_lock, NULL, MUTEX_DEFAULT, NULL);
334 	return (dbn);
335 }
336 
337 /*
338  * Set the fields in the zfs_bookmark_phys_t based on the specified snapshot.
339  */
340 static void
dsl_bookmark_set_phys(zfs_bookmark_phys_t * zbm,dsl_dataset_t * snap)341 dsl_bookmark_set_phys(zfs_bookmark_phys_t *zbm, dsl_dataset_t *snap)
342 {
343 	spa_t *spa = dsl_dataset_get_spa(snap);
344 	objset_t *mos = spa_get_dsl(spa)->dp_meta_objset;
345 	dsl_dataset_phys_t *dsp = dsl_dataset_phys(snap);
346 
347 	memset(zbm, 0, sizeof (zfs_bookmark_phys_t));
348 	zbm->zbm_guid = dsp->ds_guid;
349 	zbm->zbm_creation_txg = dsp->ds_creation_txg;
350 	zbm->zbm_creation_time = dsp->ds_creation_time;
351 	zbm->zbm_redaction_obj = 0;
352 
353 	/*
354 	 * If the dataset is encrypted create a larger bookmark to
355 	 * accommodate the IVset guid. The IVset guid was added
356 	 * after the encryption feature to prevent a problem with
357 	 * raw sends. If we encounter an encrypted dataset without
358 	 * an IVset guid we fall back to a normal bookmark.
359 	 */
360 	if (snap->ds_dir->dd_crypto_obj != 0 &&
361 	    spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) {
362 		(void) zap_lookup(mos, snap->ds_object,
363 		    DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
364 		    &zbm->zbm_ivset_guid);
365 	}
366 
367 	if (spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_WRITTEN)) {
368 		zbm->zbm_flags = ZBM_FLAG_SNAPSHOT_EXISTS | ZBM_FLAG_HAS_FBN;
369 		zbm->zbm_referenced_bytes_refd = dsp->ds_referenced_bytes;
370 		zbm->zbm_compressed_bytes_refd = dsp->ds_compressed_bytes;
371 		zbm->zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes;
372 
373 		dsl_dataset_t *nextds;
374 		VERIFY0(dsl_dataset_hold_obj(snap->ds_dir->dd_pool,
375 		    dsp->ds_next_snap_obj, FTAG, &nextds));
376 		dsl_deadlist_space(&nextds->ds_deadlist,
377 		    &zbm->zbm_referenced_freed_before_next_snap,
378 		    &zbm->zbm_compressed_freed_before_next_snap,
379 		    &zbm->zbm_uncompressed_freed_before_next_snap);
380 		dsl_dataset_rele(nextds, FTAG);
381 	}
382 }
383 
384 /*
385  * Add dsl_bookmark_node_t `dbn` to the given dataset and increment appropriate
386  * SPA feature counters.
387  */
388 void
dsl_bookmark_node_add(dsl_dataset_t * hds,dsl_bookmark_node_t * dbn,dmu_tx_t * tx)389 dsl_bookmark_node_add(dsl_dataset_t *hds, dsl_bookmark_node_t *dbn,
390     dmu_tx_t *tx)
391 {
392 	dsl_pool_t *dp = dmu_tx_pool(tx);
393 	objset_t *mos = dp->dp_meta_objset;
394 
395 	if (hds->ds_bookmarks_obj == 0) {
396 		hds->ds_bookmarks_obj = zap_create_norm(mos,
397 		    U8_TEXTPREP_TOUPPER, DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0,
398 		    tx);
399 		spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
400 
401 		dsl_dataset_zapify(hds, tx);
402 		VERIFY0(zap_add(mos, hds->ds_object,
403 		    DS_FIELD_BOOKMARK_NAMES,
404 		    sizeof (hds->ds_bookmarks_obj), 1,
405 		    &hds->ds_bookmarks_obj, tx));
406 	}
407 
408 	avl_add(&hds->ds_bookmarks, dbn);
409 
410 	/*
411 	 * To maintain backwards compatibility with software that doesn't
412 	 * understand SPA_FEATURE_BOOKMARK_V2, we need to use the smallest
413 	 * possible bookmark size.
414 	 */
415 	uint64_t bookmark_phys_size = BOOKMARK_PHYS_SIZE_V1;
416 	if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2) &&
417 	    (dbn->dbn_phys.zbm_ivset_guid != 0 || dbn->dbn_phys.zbm_flags &
418 	    ZBM_FLAG_HAS_FBN || dbn->dbn_phys.zbm_redaction_obj != 0)) {
419 		bookmark_phys_size = BOOKMARK_PHYS_SIZE_V2;
420 		spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2, tx);
421 	}
422 
423 	zfs_bookmark_phys_t zero_phys = { 0 };
424 	ASSERT0(memcmp(((char *)&dbn->dbn_phys) + bookmark_phys_size,
425 	    &zero_phys, sizeof (zfs_bookmark_phys_t) - bookmark_phys_size));
426 
427 	VERIFY0(zap_add(mos, hds->ds_bookmarks_obj, dbn->dbn_name,
428 	    sizeof (uint64_t), bookmark_phys_size / sizeof (uint64_t),
429 	    &dbn->dbn_phys, tx));
430 }
431 
432 /*
433  * If redaction_list is non-null, we create a redacted bookmark and redaction
434  * list, and store the object number of the redaction list in redact_obj.
435  */
436 static void
dsl_bookmark_create_sync_impl_snap(const char * bookmark,const char * snapshot,dmu_tx_t * tx,uint64_t num_redact_snaps,uint64_t * redact_snaps,const void * tag,redaction_list_t ** redaction_list)437 dsl_bookmark_create_sync_impl_snap(const char *bookmark, const char *snapshot,
438     dmu_tx_t *tx, uint64_t num_redact_snaps, uint64_t *redact_snaps,
439     const void *tag, redaction_list_t **redaction_list)
440 {
441 	dsl_pool_t *dp = dmu_tx_pool(tx);
442 	objset_t *mos = dp->dp_meta_objset;
443 	dsl_dataset_t *snapds, *bmark_fs;
444 	const char *shortname;
445 	boolean_t bookmark_redacted;
446 	uint64_t *dsredactsnaps;
447 	uint64_t dsnumsnaps;
448 
449 	VERIFY0(dsl_dataset_hold(dp, snapshot, FTAG, &snapds));
450 	VERIFY0(dsl_bookmark_hold_ds(dp, bookmark, &bmark_fs, FTAG,
451 	    &shortname));
452 
453 	dsl_bookmark_node_t *dbn = dsl_bookmark_node_alloc(shortname);
454 	dsl_bookmark_set_phys(&dbn->dbn_phys, snapds);
455 
456 	bookmark_redacted = dsl_dataset_get_uint64_array_feature(snapds,
457 	    SPA_FEATURE_REDACTED_DATASETS, &dsnumsnaps, &dsredactsnaps);
458 	if (redaction_list != NULL || bookmark_redacted) {
459 		redaction_list_t *local_rl;
460 		boolean_t spill = B_FALSE;
461 		if (bookmark_redacted) {
462 			redact_snaps = dsredactsnaps;
463 			num_redact_snaps = dsnumsnaps;
464 		}
465 		int bonuslen = sizeof (redaction_list_phys_t) +
466 		    num_redact_snaps * sizeof (uint64_t);
467 		if (bonuslen > dmu_bonus_max())
468 			spill = B_TRUE;
469 		dbn->dbn_phys.zbm_redaction_obj = dmu_object_alloc(mos,
470 		    DMU_OTN_UINT64_METADATA, SPA_OLD_MAXBLOCKSIZE,
471 		    DMU_OTN_UINT64_METADATA, spill ? 0 : bonuslen, tx);
472 		spa_feature_incr(dp->dp_spa,
473 		    SPA_FEATURE_REDACTION_BOOKMARKS, tx);
474 		if (spill) {
475 			spa_feature_incr(dp->dp_spa,
476 			    SPA_FEATURE_REDACTION_LIST_SPILL, tx);
477 		}
478 
479 		VERIFY0(dsl_redaction_list_hold_obj(dp,
480 		    dbn->dbn_phys.zbm_redaction_obj, tag, &local_rl));
481 		dsl_redaction_list_long_hold(dp, local_rl, tag);
482 
483 		if (!spill) {
484 			ASSERT3U(local_rl->rl_bonus->db_size, >=, bonuslen);
485 			dmu_buf_will_dirty(local_rl->rl_bonus, tx);
486 		} else {
487 			dmu_buf_t *db;
488 			VERIFY0(dmu_spill_hold_by_bonus(local_rl->rl_bonus,
489 			    DB_RF_MUST_SUCCEED, tag, &db));
490 			dmu_buf_will_fill(db, tx, B_FALSE);
491 			VERIFY0(dbuf_spill_set_blksz(db, P2ROUNDUP(bonuslen,
492 			    SPA_MINBLOCKSIZE), tx));
493 			local_rl->rl_phys = db->db_data;
494 			local_rl->rl_dbuf = db;
495 		}
496 		memcpy(local_rl->rl_phys->rlp_snaps, redact_snaps,
497 		    sizeof (uint64_t) * num_redact_snaps);
498 		local_rl->rl_phys->rlp_num_snaps = num_redact_snaps;
499 		if (bookmark_redacted) {
500 			ASSERT0P(redaction_list);
501 			local_rl->rl_phys->rlp_last_blkid = UINT64_MAX;
502 			local_rl->rl_phys->rlp_last_object = UINT64_MAX;
503 			dsl_redaction_list_long_rele(local_rl, tag);
504 			dsl_redaction_list_rele(local_rl, tag);
505 		} else {
506 			*redaction_list = local_rl;
507 		}
508 	}
509 
510 	if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
511 		spa_feature_incr(dp->dp_spa,
512 		    SPA_FEATURE_BOOKMARK_WRITTEN, tx);
513 	}
514 
515 	dsl_bookmark_node_add(bmark_fs, dbn, tx);
516 
517 	spa_history_log_internal_ds(bmark_fs, "bookmark", tx,
518 	    "name=%s creation_txg=%llu target_snap=%llu redact_obj=%llu",
519 	    shortname, (longlong_t)dbn->dbn_phys.zbm_creation_txg,
520 	    (longlong_t)snapds->ds_object,
521 	    (longlong_t)dbn->dbn_phys.zbm_redaction_obj);
522 
523 	dsl_dataset_rele(bmark_fs, FTAG);
524 	dsl_dataset_rele(snapds, FTAG);
525 }
526 
527 
528 static void
dsl_bookmark_create_sync_impl_book(const char * new_name,const char * source_name,dmu_tx_t * tx)529 dsl_bookmark_create_sync_impl_book(
530     const char *new_name, const char *source_name, dmu_tx_t *tx)
531 {
532 	dsl_pool_t *dp = dmu_tx_pool(tx);
533 	dsl_dataset_t *bmark_fs_source, *bmark_fs_new;
534 	const char *source_shortname, *new_shortname;
535 	zfs_bookmark_phys_t source_phys;
536 
537 	VERIFY0(dsl_bookmark_hold_ds(dp, source_name, &bmark_fs_source, FTAG,
538 	    &source_shortname));
539 	VERIFY0(dsl_bookmark_hold_ds(dp, new_name, &bmark_fs_new, FTAG,
540 	    &new_shortname));
541 
542 	/*
543 	 * create a copy of the source bookmark by copying most of its members
544 	 *
545 	 * Caveat: bookmarking a redaction bookmark yields a normal bookmark
546 	 * -----------------------------------------------------------------
547 	 * Reasoning:
548 	 * - The zbm_redaction_obj would be referred to by both source and new
549 	 *   bookmark, but would be destroyed once either source or new is
550 	 *   destroyed, resulting in use-after-free of the referred object.
551 	 * - User expectation when issuing the `zfs bookmark` command is that
552 	 *   a normal bookmark of the source is created
553 	 *
554 	 * Design Alternatives For Full Redaction Bookmark Copying:
555 	 * - reference-count the redaction object => would require on-disk
556 	 *   format change for existing redaction objects
557 	 * - Copy the redaction object => cannot be done in syncing context
558 	 *   because the redaction object might be too large
559 	 */
560 
561 	VERIFY0(dsl_bookmark_lookup_impl(bmark_fs_source, source_shortname,
562 	    &source_phys));
563 	dsl_bookmark_node_t *new_dbn = dsl_bookmark_node_alloc(new_shortname);
564 
565 	memcpy(&new_dbn->dbn_phys, &source_phys, sizeof (source_phys));
566 	new_dbn->dbn_phys.zbm_redaction_obj = 0;
567 
568 	/* update feature counters */
569 	if (new_dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
570 		spa_feature_incr(dp->dp_spa,
571 		    SPA_FEATURE_BOOKMARK_WRITTEN, tx);
572 	}
573 	/* no need for redaction bookmark counter; nulled zbm_redaction_obj */
574 	/* dsl_bookmark_node_add bumps bookmarks and v2-bookmarks counter */
575 
576 	/*
577 	 * write new bookmark
578 	 *
579 	 * Note that dsl_bookmark_lookup_impl guarantees that, if source is a
580 	 * v1 bookmark, the v2-only fields are zeroed.
581 	 * And dsl_bookmark_node_add writes back a v1-sized bookmark if
582 	 * v2 bookmarks are disabled and/or v2-only fields are zeroed.
583 	 * => bookmark copying works on pre-bookmark-v2 pools
584 	 */
585 	dsl_bookmark_node_add(bmark_fs_new, new_dbn, tx);
586 
587 	spa_history_log_internal_ds(bmark_fs_source, "bookmark", tx,
588 	    "name=%s creation_txg=%llu source_guid=%llu",
589 	    new_shortname, (longlong_t)new_dbn->dbn_phys.zbm_creation_txg,
590 	    (longlong_t)source_phys.zbm_guid);
591 
592 	dsl_dataset_rele(bmark_fs_source, FTAG);
593 	dsl_dataset_rele(bmark_fs_new, FTAG);
594 }
595 
596 void
dsl_bookmark_create_sync(void * arg,dmu_tx_t * tx)597 dsl_bookmark_create_sync(void *arg, dmu_tx_t *tx)
598 {
599 	dsl_bookmark_create_arg_t *dbca = arg;
600 
601 	ASSERT(spa_feature_is_enabled(dmu_tx_pool(tx)->dp_spa,
602 	    SPA_FEATURE_BOOKMARKS));
603 
604 	for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);
605 	    pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {
606 
607 		const char *new = nvpair_name(pair);
608 		const char *source = fnvpair_value_string(pair);
609 
610 		if (strchr(source, '@') != NULL) {
611 			dsl_bookmark_create_sync_impl_snap(new, source, tx,
612 			    0, NULL, NULL, NULL);
613 		} else if (strchr(source, '#') != NULL) {
614 			dsl_bookmark_create_sync_impl_book(new, source, tx);
615 		} else {
616 			panic("unreachable code");
617 		}
618 
619 	}
620 }
621 
622 /*
623  * The bookmarks must all be in the same pool.
624  */
625 int
dsl_bookmark_create(nvlist_t * bmarks,nvlist_t * errors)626 dsl_bookmark_create(nvlist_t *bmarks, nvlist_t *errors)
627 {
628 	nvpair_t *pair;
629 	dsl_bookmark_create_arg_t dbca;
630 
631 	pair = nvlist_next_nvpair(bmarks, NULL);
632 	if (pair == NULL)
633 		return (0);
634 
635 	dbca.dbca_bmarks = bmarks;
636 	dbca.dbca_errors = errors;
637 
638 	return (dsl_sync_task(nvpair_name(pair), dsl_bookmark_create_check,
639 	    dsl_bookmark_create_sync, &dbca,
640 	    fnvlist_num_pairs(bmarks), ZFS_SPACE_CHECK_NORMAL));
641 }
642 
643 static int
dsl_bookmark_create_redacted_check(void * arg,dmu_tx_t * tx)644 dsl_bookmark_create_redacted_check(void *arg, dmu_tx_t *tx)
645 {
646 	dsl_bookmark_create_redacted_arg_t *dbcra = arg;
647 	dsl_pool_t *dp = dmu_tx_pool(tx);
648 	int rv = 0;
649 
650 	if (!spa_feature_is_enabled(dp->dp_spa,
651 	    SPA_FEATURE_REDACTION_BOOKMARKS))
652 		return (SET_ERROR(ENOTSUP));
653 	/*
654 	 * If the list of redact snaps will not fit in the bonus buffer (or
655 	 * spill block, with the REDACTION_LIST_SPILL feature) with the
656 	 * furthest reached object and offset, fail.
657 	 */
658 	uint64_t snaplimit = ((spa_feature_is_enabled(dp->dp_spa,
659 	    SPA_FEATURE_REDACTION_LIST_SPILL) ? spa_maxblocksize(dp->dp_spa) :
660 	    dmu_bonus_max()) -
661 	    sizeof (redaction_list_phys_t)) / sizeof (uint64_t);
662 	if (dbcra->dbcra_numsnaps > snaplimit)
663 		return (SET_ERROR(E2BIG));
664 
665 	if (dsl_bookmark_create_nvl_validate_pair(
666 	    dbcra->dbcra_bmark, dbcra->dbcra_snap) != 0)
667 		return (SET_ERROR(EINVAL));
668 
669 	rv = dsl_bookmark_create_check_impl(dp,
670 	    dbcra->dbcra_bmark, dbcra->dbcra_snap);
671 	return (rv);
672 }
673 
674 static void
dsl_bookmark_create_redacted_sync(void * arg,dmu_tx_t * tx)675 dsl_bookmark_create_redacted_sync(void *arg, dmu_tx_t *tx)
676 {
677 	dsl_bookmark_create_redacted_arg_t *dbcra = arg;
678 	dsl_bookmark_create_sync_impl_snap(dbcra->dbcra_bmark,
679 	    dbcra->dbcra_snap, tx, dbcra->dbcra_numsnaps, dbcra->dbcra_snaps,
680 	    dbcra->dbcra_tag, dbcra->dbcra_rl);
681 }
682 
683 int
dsl_bookmark_create_redacted(const char * bookmark,const char * snapshot,uint64_t numsnaps,uint64_t * snapguids,const void * tag,redaction_list_t ** rl)684 dsl_bookmark_create_redacted(const char *bookmark, const char *snapshot,
685     uint64_t numsnaps, uint64_t *snapguids, const void *tag,
686     redaction_list_t **rl)
687 {
688 	dsl_bookmark_create_redacted_arg_t dbcra;
689 
690 	dbcra.dbcra_bmark = bookmark;
691 	dbcra.dbcra_snap = snapshot;
692 	dbcra.dbcra_rl = rl;
693 	dbcra.dbcra_numsnaps = numsnaps;
694 	dbcra.dbcra_snaps = snapguids;
695 	dbcra.dbcra_tag = tag;
696 
697 	return (dsl_sync_task(bookmark, dsl_bookmark_create_redacted_check,
698 	    dsl_bookmark_create_redacted_sync, &dbcra, 5,
699 	    ZFS_SPACE_CHECK_NORMAL));
700 }
701 
702 /*
703  * Retrieve the list of properties given in the 'props' nvlist for a bookmark.
704  * If 'props' is NULL, retrieves all properties.
705  */
706 static void
dsl_bookmark_fetch_props(dsl_pool_t * dp,zfs_bookmark_phys_t * bmark_phys,nvlist_t * props,nvlist_t * out_props)707 dsl_bookmark_fetch_props(dsl_pool_t *dp, zfs_bookmark_phys_t *bmark_phys,
708     nvlist_t *props, nvlist_t *out_props)
709 {
710 	ASSERT3P(dp, !=, NULL);
711 	ASSERT3P(bmark_phys, !=, NULL);
712 	ASSERT3P(out_props, !=, NULL);
713 	ASSERT(RRW_LOCK_HELD(&dp->dp_config_rwlock));
714 
715 	if (props == NULL || nvlist_exists(props,
716 	    zfs_prop_to_name(ZFS_PROP_GUID))) {
717 		dsl_prop_nvlist_add_uint64(out_props,
718 		    ZFS_PROP_GUID, bmark_phys->zbm_guid);
719 	}
720 	if (props == NULL || nvlist_exists(props,
721 	    zfs_prop_to_name(ZFS_PROP_CREATETXG))) {
722 		dsl_prop_nvlist_add_uint64(out_props,
723 		    ZFS_PROP_CREATETXG, bmark_phys->zbm_creation_txg);
724 	}
725 	if (props == NULL || nvlist_exists(props,
726 	    zfs_prop_to_name(ZFS_PROP_CREATION))) {
727 		dsl_prop_nvlist_add_uint64(out_props,
728 		    ZFS_PROP_CREATION, bmark_phys->zbm_creation_time);
729 	}
730 	if (props == NULL || nvlist_exists(props,
731 	    zfs_prop_to_name(ZFS_PROP_IVSET_GUID))) {
732 		dsl_prop_nvlist_add_uint64(out_props,
733 		    ZFS_PROP_IVSET_GUID, bmark_phys->zbm_ivset_guid);
734 	}
735 	if (bmark_phys->zbm_flags & ZBM_FLAG_HAS_FBN) {
736 		if (props == NULL || nvlist_exists(props,
737 		    zfs_prop_to_name(ZFS_PROP_REFERENCED))) {
738 			dsl_prop_nvlist_add_uint64(out_props,
739 			    ZFS_PROP_REFERENCED,
740 			    bmark_phys->zbm_referenced_bytes_refd);
741 		}
742 		if (props == NULL || nvlist_exists(props,
743 		    zfs_prop_to_name(ZFS_PROP_LOGICALREFERENCED))) {
744 			dsl_prop_nvlist_add_uint64(out_props,
745 			    ZFS_PROP_LOGICALREFERENCED,
746 			    bmark_phys->zbm_uncompressed_bytes_refd);
747 		}
748 		if (props == NULL || nvlist_exists(props,
749 		    zfs_prop_to_name(ZFS_PROP_REFRATIO))) {
750 			uint64_t ratio =
751 			    bmark_phys->zbm_compressed_bytes_refd == 0 ? 100 :
752 			    bmark_phys->zbm_uncompressed_bytes_refd * 100 /
753 			    bmark_phys->zbm_compressed_bytes_refd;
754 			dsl_prop_nvlist_add_uint64(out_props,
755 			    ZFS_PROP_REFRATIO, ratio);
756 		}
757 	}
758 
759 	if ((props == NULL || nvlist_exists(props, "redact_snaps") ||
760 	    nvlist_exists(props, "redact_complete")) &&
761 	    bmark_phys->zbm_redaction_obj != 0) {
762 		redaction_list_t *rl;
763 		int err = dsl_redaction_list_hold_obj(dp,
764 		    bmark_phys->zbm_redaction_obj, FTAG, &rl);
765 		if (err == 0) {
766 			if (nvlist_exists(props, "redact_snaps")) {
767 				nvlist_t *nvl;
768 				nvl = fnvlist_alloc();
769 				fnvlist_add_uint64_array(nvl, ZPROP_VALUE,
770 				    rl->rl_phys->rlp_snaps,
771 				    rl->rl_phys->rlp_num_snaps);
772 				fnvlist_add_nvlist(out_props, "redact_snaps",
773 				    nvl);
774 				nvlist_free(nvl);
775 			}
776 			if (nvlist_exists(props, "redact_complete")) {
777 				nvlist_t *nvl;
778 				nvl = fnvlist_alloc();
779 				fnvlist_add_boolean_value(nvl, ZPROP_VALUE,
780 				    rl->rl_phys->rlp_last_blkid == UINT64_MAX &&
781 				    rl->rl_phys->rlp_last_object == UINT64_MAX);
782 				fnvlist_add_nvlist(out_props, "redact_complete",
783 				    nvl);
784 				nvlist_free(nvl);
785 			}
786 			dsl_redaction_list_rele(rl, FTAG);
787 		}
788 	}
789 }
790 
791 int
dsl_get_bookmarks_impl(dsl_dataset_t * ds,nvlist_t * props,nvlist_t * outnvl)792 dsl_get_bookmarks_impl(dsl_dataset_t *ds, nvlist_t *props, nvlist_t *outnvl)
793 {
794 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
795 
796 	ASSERT(dsl_pool_config_held(dp));
797 
798 	if (dsl_dataset_is_snapshot(ds))
799 		return (SET_ERROR(EINVAL));
800 
801 	for (dsl_bookmark_node_t *dbn = avl_first(&ds->ds_bookmarks);
802 	    dbn != NULL; dbn = AVL_NEXT(&ds->ds_bookmarks, dbn)) {
803 		nvlist_t *out_props = fnvlist_alloc();
804 
805 		dsl_bookmark_fetch_props(dp, &dbn->dbn_phys, props, out_props);
806 
807 		fnvlist_add_nvlist(outnvl, dbn->dbn_name, out_props);
808 		fnvlist_free(out_props);
809 	}
810 	return (0);
811 }
812 
813 /*
814  * Comparison func for ds_bookmarks AVL tree.  We sort the bookmarks by
815  * their TXG, then by their FBN-ness.  The "FBN-ness" component ensures
816  * that all bookmarks at the same TXG that HAS_FBN are adjacent, which
817  * dsl_bookmark_destroy_sync_impl() depends on.  Note that there may be
818  * multiple bookmarks at the same TXG (with the same FBN-ness).  In this
819  * case we differentiate them by an arbitrary metric (in this case,
820  * their names).
821  */
822 static int
dsl_bookmark_compare(const void * l,const void * r)823 dsl_bookmark_compare(const void *l, const void *r)
824 {
825 	const dsl_bookmark_node_t *ldbn = l;
826 	const dsl_bookmark_node_t *rdbn = r;
827 
828 	int64_t cmp = TREE_CMP(ldbn->dbn_phys.zbm_creation_txg,
829 	    rdbn->dbn_phys.zbm_creation_txg);
830 	if (likely(cmp))
831 		return (cmp);
832 	cmp = TREE_CMP((ldbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN),
833 	    (rdbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN));
834 	if (likely(cmp))
835 		return (cmp);
836 	return (TREE_ISIGN(strcmp(ldbn->dbn_name, rdbn->dbn_name)));
837 }
838 
839 /*
840  * Cache this (head) dataset's bookmarks in the ds_bookmarks AVL tree.
841  */
842 int
dsl_bookmark_init_ds(dsl_dataset_t * ds)843 dsl_bookmark_init_ds(dsl_dataset_t *ds)
844 {
845 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
846 	objset_t *mos = dp->dp_meta_objset;
847 
848 	ASSERT(!ds->ds_is_snapshot);
849 
850 	avl_create(&ds->ds_bookmarks, dsl_bookmark_compare,
851 	    sizeof (dsl_bookmark_node_t),
852 	    offsetof(dsl_bookmark_node_t, dbn_node));
853 
854 	if (!dsl_dataset_is_zapified(ds))
855 		return (0);
856 
857 	int zaperr = zap_lookup(mos, ds->ds_object, DS_FIELD_BOOKMARK_NAMES,
858 	    sizeof (ds->ds_bookmarks_obj), 1, &ds->ds_bookmarks_obj);
859 	if (zaperr == ENOENT)
860 		return (0);
861 	if (zaperr != 0)
862 		return (zaperr);
863 
864 	if (ds->ds_bookmarks_obj == 0)
865 		return (0);
866 
867 	int err = 0;
868 	zap_cursor_t zc;
869 	zap_attribute_t *attr;
870 
871 	attr = zap_attribute_alloc();
872 	for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj);
873 	    (err = zap_cursor_retrieve(&zc, attr)) == 0;
874 	    zap_cursor_advance(&zc)) {
875 		dsl_bookmark_node_t *dbn =
876 		    dsl_bookmark_node_alloc(attr->za_name);
877 
878 		err = dsl_bookmark_lookup_impl(ds,
879 		    dbn->dbn_name, &dbn->dbn_phys);
880 		ASSERT3U(err, !=, ENOENT);
881 		if (err != 0) {
882 			kmem_free(dbn, sizeof (*dbn));
883 			break;
884 		}
885 		avl_add(&ds->ds_bookmarks, dbn);
886 	}
887 	zap_cursor_fini(&zc);
888 	zap_attribute_free(attr);
889 	if (err == ENOENT)
890 		err = 0;
891 	return (err);
892 }
893 
894 void
dsl_bookmark_fini_ds(dsl_dataset_t * ds)895 dsl_bookmark_fini_ds(dsl_dataset_t *ds)
896 {
897 	void *cookie = NULL;
898 	dsl_bookmark_node_t *dbn;
899 
900 	if (ds->ds_is_snapshot)
901 		return;
902 
903 	while ((dbn = avl_destroy_nodes(&ds->ds_bookmarks, &cookie)) != NULL) {
904 		spa_strfree(dbn->dbn_name);
905 		mutex_destroy(&dbn->dbn_lock);
906 		kmem_free(dbn, sizeof (*dbn));
907 	}
908 	avl_destroy(&ds->ds_bookmarks);
909 }
910 
911 /*
912  * Retrieve the bookmarks that exist in the specified dataset, and the
913  * requested properties of each bookmark.
914  *
915  * The "props" nvlist specifies which properties are requested.
916  * See lzc_get_bookmarks() for the list of valid properties.
917  */
918 int
dsl_get_bookmarks(const char * dsname,nvlist_t * props,nvlist_t * outnvl)919 dsl_get_bookmarks(const char *dsname, nvlist_t *props, nvlist_t *outnvl)
920 {
921 	dsl_pool_t *dp;
922 	dsl_dataset_t *ds;
923 	int err;
924 
925 	err = dsl_pool_hold(dsname, FTAG, &dp);
926 	if (err != 0)
927 		return (err);
928 	err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
929 	if (err != 0) {
930 		dsl_pool_rele(dp, FTAG);
931 		return (err);
932 	}
933 
934 	err = dsl_get_bookmarks_impl(ds, props, outnvl);
935 
936 	dsl_dataset_rele(ds, FTAG);
937 	dsl_pool_rele(dp, FTAG);
938 	return (err);
939 }
940 
941 /*
942  * Retrieve all properties for a single bookmark in the given dataset.
943  */
944 int
dsl_get_bookmark_props(const char * dsname,const char * bmname,nvlist_t * props)945 dsl_get_bookmark_props(const char *dsname, const char *bmname, nvlist_t *props)
946 {
947 	dsl_pool_t *dp;
948 	dsl_dataset_t *ds;
949 	zfs_bookmark_phys_t bmark_phys = { 0 };
950 	int err;
951 
952 	err = dsl_pool_hold(dsname, FTAG, &dp);
953 	if (err != 0)
954 		return (err);
955 	err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
956 	if (err != 0) {
957 		dsl_pool_rele(dp, FTAG);
958 		return (err);
959 	}
960 
961 	err = dsl_bookmark_lookup_impl(ds, bmname, &bmark_phys);
962 	if (err != 0)
963 		goto out;
964 
965 	dsl_bookmark_fetch_props(dp, &bmark_phys, NULL, props);
966 out:
967 	dsl_dataset_rele(ds, FTAG);
968 	dsl_pool_rele(dp, FTAG);
969 	return (err);
970 }
971 
972 typedef struct dsl_bookmark_destroy_arg {
973 	nvlist_t *dbda_bmarks;
974 	nvlist_t *dbda_success;
975 	nvlist_t *dbda_errors;
976 } dsl_bookmark_destroy_arg_t;
977 
978 static void
dsl_bookmark_destroy_sync_impl(dsl_dataset_t * ds,const char * name,dmu_tx_t * tx)979 dsl_bookmark_destroy_sync_impl(dsl_dataset_t *ds, const char *name,
980     dmu_tx_t *tx)
981 {
982 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
983 	uint64_t bmark_zapobj = ds->ds_bookmarks_obj;
984 	matchtype_t mt = 0;
985 	uint64_t int_size, num_ints;
986 	/*
987 	 * 'search' must be zeroed so that dbn_flags (which is used in
988 	 * dsl_bookmark_compare()) will be zeroed even if the on-disk
989 	 * (in ZAP) bookmark is shorter than offsetof(dbn_flags).
990 	 */
991 	dsl_bookmark_node_t search = { 0 };
992 	char realname[ZFS_MAX_DATASET_NAME_LEN];
993 
994 	/*
995 	 * Find the real name of this bookmark, which may be different
996 	 * from the given name if the dataset is case-insensitive.  Then
997 	 * use the real name to find the node in the ds_bookmarks AVL tree.
998 	 */
999 
1000 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
1001 		mt = MT_NORMALIZE;
1002 
1003 	VERIFY0(zap_length(mos, bmark_zapobj, name, &int_size, &num_ints));
1004 
1005 	ASSERT3U(int_size, ==, sizeof (uint64_t));
1006 
1007 	if (num_ints * int_size > BOOKMARK_PHYS_SIZE_V1) {
1008 		spa_feature_decr(dmu_objset_spa(mos),
1009 		    SPA_FEATURE_BOOKMARK_V2, tx);
1010 	}
1011 	VERIFY0(zap_lookup_norm(mos, bmark_zapobj, name, sizeof (uint64_t),
1012 	    num_ints, &search.dbn_phys, mt, realname, sizeof (realname), NULL));
1013 
1014 	search.dbn_name = realname;
1015 	dsl_bookmark_node_t *dbn = avl_find(&ds->ds_bookmarks, &search, NULL);
1016 	ASSERT(dbn != NULL);
1017 
1018 	if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
1019 		/*
1020 		 * If this bookmark HAS_FBN, and it is before the most
1021 		 * recent snapshot, then its TXG is a key in the head's
1022 		 * deadlist (and all clones' heads' deadlists).  If this is
1023 		 * the last thing keeping the key (i.e. there are no more
1024 		 * bookmarks with HAS_FBN at this TXG, and there is no
1025 		 * snapshot at this TXG), then remove the key.
1026 		 *
1027 		 * Note that this algorithm depends on ds_bookmarks being
1028 		 * sorted such that all bookmarks at the same TXG with
1029 		 * HAS_FBN are adjacent (with no non-HAS_FBN bookmarks
1030 		 * at the same TXG in between them).  If this were not
1031 		 * the case, we would need to examine *all* bookmarks
1032 		 * at this TXG, rather than just the adjacent ones.
1033 		 */
1034 
1035 		dsl_bookmark_node_t *dbn_prev =
1036 		    AVL_PREV(&ds->ds_bookmarks, dbn);
1037 		dsl_bookmark_node_t *dbn_next =
1038 		    AVL_NEXT(&ds->ds_bookmarks, dbn);
1039 
1040 		boolean_t more_bookmarks_at_this_txg =
1041 		    (dbn_prev != NULL && dbn_prev->dbn_phys.zbm_creation_txg ==
1042 		    dbn->dbn_phys.zbm_creation_txg &&
1043 		    (dbn_prev->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) ||
1044 		    (dbn_next != NULL && dbn_next->dbn_phys.zbm_creation_txg ==
1045 		    dbn->dbn_phys.zbm_creation_txg &&
1046 		    (dbn_next->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN));
1047 
1048 		if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS) &&
1049 		    !more_bookmarks_at_this_txg &&
1050 		    dbn->dbn_phys.zbm_creation_txg <
1051 		    dsl_dataset_phys(ds)->ds_prev_snap_txg) {
1052 			dsl_dir_remove_clones_key(ds->ds_dir,
1053 			    dbn->dbn_phys.zbm_creation_txg, tx);
1054 			dsl_deadlist_remove_key(&ds->ds_deadlist,
1055 			    dbn->dbn_phys.zbm_creation_txg, tx);
1056 		}
1057 
1058 		spa_feature_decr(dmu_objset_spa(mos),
1059 		    SPA_FEATURE_BOOKMARK_WRITTEN, tx);
1060 	}
1061 
1062 	if (dbn->dbn_phys.zbm_redaction_obj != 0) {
1063 		dnode_t *rl;
1064 		VERIFY0(dnode_hold(mos,
1065 		    dbn->dbn_phys.zbm_redaction_obj, FTAG, &rl));
1066 		if (rl->dn_have_spill) {
1067 			spa_feature_decr(dmu_objset_spa(mos),
1068 			    SPA_FEATURE_REDACTION_LIST_SPILL, tx);
1069 		}
1070 		dnode_rele(rl, FTAG);
1071 		VERIFY0(dmu_object_free(mos,
1072 		    dbn->dbn_phys.zbm_redaction_obj, tx));
1073 		spa_feature_decr(dmu_objset_spa(mos),
1074 		    SPA_FEATURE_REDACTION_BOOKMARKS, tx);
1075 	}
1076 
1077 	avl_remove(&ds->ds_bookmarks, dbn);
1078 	spa_strfree(dbn->dbn_name);
1079 	mutex_destroy(&dbn->dbn_lock);
1080 	kmem_free(dbn, sizeof (*dbn));
1081 
1082 	VERIFY0(zap_remove_norm(mos, bmark_zapobj, name, mt, tx));
1083 }
1084 
1085 static int
dsl_bookmark_destroy_check(void * arg,dmu_tx_t * tx)1086 dsl_bookmark_destroy_check(void *arg, dmu_tx_t *tx)
1087 {
1088 	dsl_bookmark_destroy_arg_t *dbda = arg;
1089 	dsl_pool_t *dp = dmu_tx_pool(tx);
1090 	int rv = 0;
1091 
1092 	ASSERT(nvlist_empty(dbda->dbda_success));
1093 	ASSERT(nvlist_empty(dbda->dbda_errors));
1094 
1095 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))
1096 		return (0);
1097 
1098 	for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_bmarks, NULL);
1099 	    pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_bmarks, pair)) {
1100 		const char *fullname = nvpair_name(pair);
1101 		dsl_dataset_t *ds;
1102 		zfs_bookmark_phys_t bm;
1103 		int error;
1104 		const char *shortname;
1105 
1106 		error = dsl_bookmark_hold_ds(dp, fullname, &ds,
1107 		    FTAG, &shortname);
1108 		if (error == ENOENT) {
1109 			/* ignore it; the bookmark is "already destroyed" */
1110 			continue;
1111 		}
1112 		if (error == 0) {
1113 			error = dsl_bookmark_lookup_impl(ds, shortname, &bm);
1114 			dsl_dataset_rele(ds, FTAG);
1115 			if (error == ESRCH) {
1116 				/*
1117 				 * ignore it; the bookmark is
1118 				 * "already destroyed"
1119 				 */
1120 				continue;
1121 			}
1122 			if (error == 0 && bm.zbm_redaction_obj != 0) {
1123 				redaction_list_t *rl = NULL;
1124 				error = dsl_redaction_list_hold_obj(tx->tx_pool,
1125 				    bm.zbm_redaction_obj, FTAG, &rl);
1126 				if (error == ENOENT) {
1127 					error = 0;
1128 				} else if (error == 0 &&
1129 				    dsl_redaction_list_long_held(rl)) {
1130 					error = SET_ERROR(EBUSY);
1131 				}
1132 				if (rl != NULL) {
1133 					dsl_redaction_list_rele(rl, FTAG);
1134 				}
1135 			}
1136 		}
1137 		if (error == 0) {
1138 			if (dmu_tx_is_syncing(tx)) {
1139 				fnvlist_add_boolean(dbda->dbda_success,
1140 				    fullname);
1141 			}
1142 		} else {
1143 			fnvlist_add_int32(dbda->dbda_errors, fullname, error);
1144 			rv = error;
1145 		}
1146 	}
1147 	return (rv);
1148 }
1149 
1150 static void
dsl_bookmark_destroy_sync(void * arg,dmu_tx_t * tx)1151 dsl_bookmark_destroy_sync(void *arg, dmu_tx_t *tx)
1152 {
1153 	dsl_bookmark_destroy_arg_t *dbda = arg;
1154 	dsl_pool_t *dp = dmu_tx_pool(tx);
1155 	objset_t *mos = dp->dp_meta_objset;
1156 
1157 	for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_success, NULL);
1158 	    pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_success, pair)) {
1159 		dsl_dataset_t *ds;
1160 		const char *shortname;
1161 		uint64_t zap_cnt;
1162 
1163 		VERIFY0(dsl_bookmark_hold_ds(dp, nvpair_name(pair),
1164 		    &ds, FTAG, &shortname));
1165 		dsl_bookmark_destroy_sync_impl(ds, shortname, tx);
1166 
1167 		/*
1168 		 * If all of this dataset's bookmarks have been destroyed,
1169 		 * free the zap object and decrement the feature's use count.
1170 		 */
1171 		VERIFY0(zap_count(mos, ds->ds_bookmarks_obj, &zap_cnt));
1172 		if (zap_cnt == 0) {
1173 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
1174 			VERIFY0(zap_destroy(mos, ds->ds_bookmarks_obj, tx));
1175 			ds->ds_bookmarks_obj = 0;
1176 			spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
1177 			VERIFY0(zap_remove(mos, ds->ds_object,
1178 			    DS_FIELD_BOOKMARK_NAMES, tx));
1179 		}
1180 
1181 		spa_history_log_internal_ds(ds, "remove bookmark", tx,
1182 		    "name=%s", shortname);
1183 
1184 		dsl_dataset_rele(ds, FTAG);
1185 	}
1186 }
1187 
1188 /*
1189  * The bookmarks must all be in the same pool.
1190  */
1191 int
dsl_bookmark_destroy(nvlist_t * bmarks,nvlist_t * errors)1192 dsl_bookmark_destroy(nvlist_t *bmarks, nvlist_t *errors)
1193 {
1194 	int rv;
1195 	dsl_bookmark_destroy_arg_t dbda;
1196 	nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
1197 	if (pair == NULL)
1198 		return (0);
1199 
1200 	dbda.dbda_bmarks = bmarks;
1201 	dbda.dbda_errors = errors;
1202 	dbda.dbda_success = fnvlist_alloc();
1203 
1204 	rv = dsl_sync_task(nvpair_name(pair), dsl_bookmark_destroy_check,
1205 	    dsl_bookmark_destroy_sync, &dbda, fnvlist_num_pairs(bmarks),
1206 	    ZFS_SPACE_CHECK_RESERVED);
1207 	fnvlist_free(dbda.dbda_success);
1208 	return (rv);
1209 }
1210 
1211 /* Return B_TRUE if there are any long holds on this dataset. */
1212 boolean_t
dsl_redaction_list_long_held(redaction_list_t * rl)1213 dsl_redaction_list_long_held(redaction_list_t *rl)
1214 {
1215 	return (!zfs_refcount_is_zero(&rl->rl_longholds));
1216 }
1217 
1218 void
dsl_redaction_list_long_hold(dsl_pool_t * dp,redaction_list_t * rl,const void * tag)1219 dsl_redaction_list_long_hold(dsl_pool_t *dp, redaction_list_t *rl,
1220     const void *tag)
1221 {
1222 	ASSERT(dsl_pool_config_held(dp));
1223 	(void) zfs_refcount_add(&rl->rl_longholds, tag);
1224 }
1225 
1226 void
dsl_redaction_list_long_rele(redaction_list_t * rl,const void * tag)1227 dsl_redaction_list_long_rele(redaction_list_t *rl, const void *tag)
1228 {
1229 	(void) zfs_refcount_remove(&rl->rl_longholds, tag);
1230 }
1231 
1232 static void
redaction_list_evict_sync(void * rlu)1233 redaction_list_evict_sync(void *rlu)
1234 {
1235 	redaction_list_t *rl = rlu;
1236 	zfs_refcount_destroy(&rl->rl_longholds);
1237 
1238 	kmem_free(rl, sizeof (redaction_list_t));
1239 }
1240 
1241 void
dsl_redaction_list_rele(redaction_list_t * rl,const void * tag)1242 dsl_redaction_list_rele(redaction_list_t *rl, const void *tag)
1243 {
1244 	if (rl->rl_bonus != rl->rl_dbuf)
1245 		dmu_buf_rele(rl->rl_dbuf, tag);
1246 	dmu_buf_rele(rl->rl_bonus, tag);
1247 }
1248 
1249 int
dsl_redaction_list_hold_obj(dsl_pool_t * dp,uint64_t rlobj,const void * tag,redaction_list_t ** rlp)1250 dsl_redaction_list_hold_obj(dsl_pool_t *dp, uint64_t rlobj, const void *tag,
1251     redaction_list_t **rlp)
1252 {
1253 	objset_t *mos = dp->dp_meta_objset;
1254 	dmu_buf_t *dbuf, *spill_dbuf;
1255 	redaction_list_t *rl;
1256 	int err;
1257 
1258 	ASSERT(dsl_pool_config_held(dp));
1259 
1260 	err = dmu_bonus_hold(mos, rlobj, tag, &dbuf);
1261 	if (err != 0)
1262 		return (err);
1263 
1264 	rl = dmu_buf_get_user(dbuf);
1265 	if (rl == NULL) {
1266 		redaction_list_t *winner = NULL;
1267 
1268 		rl = kmem_zalloc(sizeof (redaction_list_t), KM_SLEEP);
1269 		rl->rl_bonus = dbuf;
1270 		if (dmu_spill_hold_existing(dbuf, tag, &spill_dbuf) == 0) {
1271 			rl->rl_dbuf = spill_dbuf;
1272 		} else {
1273 			rl->rl_dbuf = dbuf;
1274 		}
1275 		rl->rl_object = rlobj;
1276 		rl->rl_phys = rl->rl_dbuf->db_data;
1277 		rl->rl_mos = dp->dp_meta_objset;
1278 		zfs_refcount_create(&rl->rl_longholds);
1279 		dmu_buf_init_user(&rl->rl_dbu, redaction_list_evict_sync, NULL,
1280 		    &rl->rl_bonus);
1281 		if ((winner = dmu_buf_set_user_ie(dbuf, &rl->rl_dbu)) != NULL) {
1282 			kmem_free(rl, sizeof (*rl));
1283 			rl = winner;
1284 		}
1285 	}
1286 	*rlp = rl;
1287 	return (0);
1288 }
1289 
1290 /*
1291  * Snapshot ds is being destroyed.
1292  *
1293  * Adjust the "freed_before_next" of any bookmarks between this snap
1294  * and the previous snapshot, because their "next snapshot" is changing.
1295  *
1296  * If there are any bookmarks with HAS_FBN at this snapshot, remove
1297  * their HAS_SNAP flag (note: there can be at most one snapshot of
1298  * each filesystem at a given txg), and return B_TRUE.  In this case
1299  * the caller can not remove the key in the deadlist at this TXG, because
1300  * the HAS_FBN bookmarks require the key be there.
1301  *
1302  * Returns B_FALSE if there are no bookmarks with HAS_FBN at this
1303  * snapshot's TXG.  In this case the caller can remove the key in the
1304  * deadlist at this TXG.
1305  */
1306 boolean_t
dsl_bookmark_ds_destroyed(dsl_dataset_t * ds,dmu_tx_t * tx)1307 dsl_bookmark_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
1308 {
1309 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1310 
1311 	dsl_dataset_t *head, *next;
1312 	VERIFY0(dsl_dataset_hold_obj(dp,
1313 	    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &head));
1314 	VERIFY0(dsl_dataset_hold_obj(dp,
1315 	    dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &next));
1316 
1317 	/*
1318 	 * Find the first bookmark that HAS_FBN at or after the
1319 	 * previous snapshot.
1320 	 */
1321 	dsl_bookmark_node_t search = { 0 };
1322 	avl_index_t idx;
1323 	search.dbn_phys.zbm_creation_txg =
1324 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1325 	search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;
1326 	/*
1327 	 * The empty-string name can't be in the AVL, and it compares
1328 	 * before any entries with this TXG.
1329 	 */
1330 	search.dbn_name = (char *)"";
1331 	VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);
1332 	dsl_bookmark_node_t *dbn =
1333 	    avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);
1334 
1335 	/*
1336 	 * Iterate over all bookmarks that are at or after the previous
1337 	 * snapshot, and before this (being deleted) snapshot.  Adjust
1338 	 * their FBN based on their new next snapshot.
1339 	 */
1340 	for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg <
1341 	    dsl_dataset_phys(ds)->ds_creation_txg;
1342 	    dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1343 		if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN))
1344 			continue;
1345 		/*
1346 		 * Increase our FBN by the amount of space that was live
1347 		 * (referenced) at the time of this bookmark (i.e.
1348 		 * birth <= zbm_creation_txg), and killed between this
1349 		 * (being deleted) snapshot and the next snapshot (i.e.
1350 		 * on the next snapshot's deadlist).  (Space killed before
1351 		 * this are already on our FBN.)
1352 		 */
1353 		uint64_t referenced, compressed, uncompressed;
1354 		dsl_deadlist_space_range(&next->ds_deadlist,
1355 		    0, dbn->dbn_phys.zbm_creation_txg,
1356 		    &referenced, &compressed, &uncompressed);
1357 		dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=
1358 		    referenced;
1359 		dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=
1360 		    compressed;
1361 		dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=
1362 		    uncompressed;
1363 		VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1364 		    dbn->dbn_name, sizeof (uint64_t),
1365 		    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1366 		    &dbn->dbn_phys, tx));
1367 	}
1368 	dsl_dataset_rele(next, FTAG);
1369 
1370 	/*
1371 	 * There may be several bookmarks at this txg (the TXG of the
1372 	 * snapshot being deleted).  We need to clear the SNAPSHOT_EXISTS
1373 	 * flag on all of them, and return TRUE if there is at least 1
1374 	 * bookmark here with HAS_FBN (thus preventing the deadlist
1375 	 * key from being removed).
1376 	 */
1377 	boolean_t rv = B_FALSE;
1378 	for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==
1379 	    dsl_dataset_phys(ds)->ds_creation_txg;
1380 	    dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1381 		if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {
1382 			ASSERT(!(dbn->dbn_phys.zbm_flags &
1383 			    ZBM_FLAG_SNAPSHOT_EXISTS));
1384 			continue;
1385 		}
1386 		ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS);
1387 		dbn->dbn_phys.zbm_flags &= ~ZBM_FLAG_SNAPSHOT_EXISTS;
1388 		VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1389 		    dbn->dbn_name, sizeof (uint64_t),
1390 		    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1391 		    &dbn->dbn_phys, tx));
1392 		rv = B_TRUE;
1393 	}
1394 	dsl_dataset_rele(head, FTAG);
1395 	return (rv);
1396 }
1397 
1398 /*
1399  * A snapshot is being created of this (head) dataset.
1400  *
1401  * We don't keep keys in the deadlist for the most recent snapshot, or any
1402  * bookmarks at or after it, because there can't be any blocks on the
1403  * deadlist in this range.  Now that the most recent snapshot is after
1404  * all bookmarks, we need to add these keys.  Note that the caller always
1405  * adds a key at the previous snapshot, so we only add keys for bookmarks
1406  * after that.
1407  */
1408 void
dsl_bookmark_snapshotted(dsl_dataset_t * ds,dmu_tx_t * tx)1409 dsl_bookmark_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
1410 {
1411 	uint64_t last_key_added = UINT64_MAX;
1412 	for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1413 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg >
1414 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1415 	    dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1416 		uint64_t creation_txg = dbn->dbn_phys.zbm_creation_txg;
1417 		ASSERT3U(creation_txg, <=, last_key_added);
1418 		/*
1419 		 * Note, there may be multiple bookmarks at this TXG,
1420 		 * and we only want to add the key for this TXG once.
1421 		 * The ds_bookmarks AVL is sorted by TXG, so we will visit
1422 		 * these bookmarks in sequence.
1423 		 */
1424 		if ((dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) &&
1425 		    creation_txg != last_key_added) {
1426 			dsl_deadlist_add_key(&ds->ds_deadlist,
1427 			    creation_txg, tx);
1428 			last_key_added = creation_txg;
1429 		}
1430 	}
1431 }
1432 
1433 /*
1434  * The next snapshot of the origin dataset has changed, due to
1435  * promote or clone swap.  If there are any bookmarks at this dataset,
1436  * we need to update their zbm_*_freed_before_next_snap to reflect this.
1437  * The head dataset has the relevant bookmarks in ds_bookmarks.
1438  */
1439 void
dsl_bookmark_next_changed(dsl_dataset_t * head,dsl_dataset_t * origin,dmu_tx_t * tx)1440 dsl_bookmark_next_changed(dsl_dataset_t *head, dsl_dataset_t *origin,
1441     dmu_tx_t *tx)
1442 {
1443 	dsl_pool_t *dp = dmu_tx_pool(tx);
1444 
1445 	/*
1446 	 * Find the first bookmark that HAS_FBN at the origin snapshot.
1447 	 */
1448 	dsl_bookmark_node_t search = { 0 };
1449 	avl_index_t idx;
1450 	search.dbn_phys.zbm_creation_txg =
1451 	    dsl_dataset_phys(origin)->ds_creation_txg;
1452 	search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;
1453 	/*
1454 	 * The empty-string name can't be in the AVL, and it compares
1455 	 * before any entries with this TXG.
1456 	 */
1457 	search.dbn_name = (char *)"";
1458 	VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);
1459 	dsl_bookmark_node_t *dbn =
1460 	    avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);
1461 
1462 	/*
1463 	 * Iterate over all bookmarks that are at the origin txg.
1464 	 * Adjust their FBN based on their new next snapshot.
1465 	 */
1466 	for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==
1467 	    dsl_dataset_phys(origin)->ds_creation_txg &&
1468 	    (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);
1469 	    dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1470 
1471 		/*
1472 		 * Bookmark is at the origin, therefore its
1473 		 * "next dataset" is changing, so we need
1474 		 * to reset its FBN by recomputing it in
1475 		 * dsl_bookmark_set_phys().
1476 		 */
1477 		ASSERT3U(dbn->dbn_phys.zbm_guid, ==,
1478 		    dsl_dataset_phys(origin)->ds_guid);
1479 		ASSERT3U(dbn->dbn_phys.zbm_referenced_bytes_refd, ==,
1480 		    dsl_dataset_phys(origin)->ds_referenced_bytes);
1481 		ASSERT(dbn->dbn_phys.zbm_flags &
1482 		    ZBM_FLAG_SNAPSHOT_EXISTS);
1483 		/*
1484 		 * Save and restore the zbm_redaction_obj, which
1485 		 * is zeroed by dsl_bookmark_set_phys().
1486 		 */
1487 		uint64_t redaction_obj =
1488 		    dbn->dbn_phys.zbm_redaction_obj;
1489 		dsl_bookmark_set_phys(&dbn->dbn_phys, origin);
1490 		dbn->dbn_phys.zbm_redaction_obj = redaction_obj;
1491 
1492 		VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1493 		    dbn->dbn_name, sizeof (uint64_t),
1494 		    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1495 		    &dbn->dbn_phys, tx));
1496 	}
1497 }
1498 
1499 /*
1500  * This block is no longer referenced by this (head) dataset.
1501  *
1502  * Adjust the FBN of any bookmarks that reference this block, whose "next"
1503  * is the head dataset.
1504  */
1505 void
dsl_bookmark_block_killed(dsl_dataset_t * ds,const blkptr_t * bp,dmu_tx_t * tx)1506 dsl_bookmark_block_killed(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
1507 {
1508 	(void) tx;
1509 
1510 	/*
1511 	 * Iterate over bookmarks whose "next" is the head dataset.
1512 	 */
1513 	for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1514 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg >=
1515 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1516 	    dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1517 		/*
1518 		 * If the block was live (referenced) at the time of this
1519 		 * bookmark, add its space to the bookmark's FBN.
1520 		 */
1521 		if (BP_GET_BIRTH(bp) <=
1522 		    dbn->dbn_phys.zbm_creation_txg &&
1523 		    (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {
1524 			mutex_enter(&dbn->dbn_lock);
1525 			dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=
1526 			    bp_get_dsize_sync(dsl_dataset_get_spa(ds), bp);
1527 			dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=
1528 			    BP_GET_PSIZE(bp);
1529 			dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=
1530 			    BP_GET_UCSIZE(bp);
1531 			/*
1532 			 * Changing the ZAP object here would be too
1533 			 * expensive.  Also, we may be called from the zio
1534 			 * interrupt thread, which can't block on i/o.
1535 			 * Therefore, we mark this bookmark as dirty and
1536 			 * modify the ZAP once per txg, in
1537 			 * dsl_bookmark_sync_done().
1538 			 */
1539 			dbn->dbn_dirty = B_TRUE;
1540 			mutex_exit(&dbn->dbn_lock);
1541 		}
1542 	}
1543 }
1544 
1545 void
dsl_bookmark_sync_done(dsl_dataset_t * ds,dmu_tx_t * tx)1546 dsl_bookmark_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
1547 {
1548 	dsl_pool_t *dp = dmu_tx_pool(tx);
1549 
1550 	if (dsl_dataset_is_snapshot(ds))
1551 		return;
1552 
1553 	/*
1554 	 * We only dirty bookmarks that are at or after the most recent
1555 	 * snapshot.  We can't create snapshots between
1556 	 * dsl_bookmark_block_killed() and dsl_bookmark_sync_done(), so we
1557 	 * don't need to look at any bookmarks before ds_prev_snap_txg.
1558 	 */
1559 	for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1560 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg >=
1561 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1562 	    dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1563 		if (dbn->dbn_dirty) {
1564 			/*
1565 			 * We only dirty nodes with HAS_FBN, therefore
1566 			 * we can always use the current bookmark struct size.
1567 			 */
1568 			ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);
1569 			VERIFY0(zap_update(dp->dp_meta_objset,
1570 			    ds->ds_bookmarks_obj,
1571 			    dbn->dbn_name, sizeof (uint64_t),
1572 			    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1573 			    &dbn->dbn_phys, tx));
1574 			dbn->dbn_dirty = B_FALSE;
1575 		}
1576 	}
1577 #ifdef ZFS_DEBUG
1578 	for (dsl_bookmark_node_t *dbn = avl_first(&ds->ds_bookmarks);
1579 	    dbn != NULL; dbn = AVL_NEXT(&ds->ds_bookmarks, dbn)) {
1580 		ASSERT(!dbn->dbn_dirty);
1581 	}
1582 #endif
1583 }
1584 
1585 /*
1586  * Return the TXG of the most recent bookmark (or 0 if there are no bookmarks).
1587  */
1588 uint64_t
dsl_bookmark_latest_txg(dsl_dataset_t * ds)1589 dsl_bookmark_latest_txg(dsl_dataset_t *ds)
1590 {
1591 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1592 	dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1593 	if (dbn == NULL)
1594 		return (0);
1595 	return (dbn->dbn_phys.zbm_creation_txg);
1596 }
1597 
1598 /*
1599  * Compare the redact_block_phys_t to the bookmark. If the last block in the
1600  * redact_block_phys_t is before the bookmark, return -1.  If the first block in
1601  * the redact_block_phys_t is after the bookmark, return 1.  Otherwise, the
1602  * bookmark is inside the range of the redact_block_phys_t, and we return 0.
1603  */
1604 static int
redact_block_zb_compare(redact_block_phys_t * first,zbookmark_phys_t * second)1605 redact_block_zb_compare(redact_block_phys_t *first,
1606     zbookmark_phys_t *second)
1607 {
1608 	/*
1609 	 * If the block_phys is for a previous object, or the last block in the
1610 	 * block_phys is strictly before the block in the bookmark, the
1611 	 * block_phys is earlier.
1612 	 */
1613 	if (first->rbp_object < second->zb_object ||
1614 	    (first->rbp_object == second->zb_object &&
1615 	    first->rbp_blkid + (redact_block_get_count(first) - 1) <
1616 	    second->zb_blkid)) {
1617 		return (-1);
1618 	}
1619 
1620 	/*
1621 	 * If the bookmark is for a previous object, or the block in the
1622 	 * bookmark is strictly before the first block in the block_phys, the
1623 	 * bookmark is earlier.
1624 	 */
1625 	if (first->rbp_object > second->zb_object ||
1626 	    (first->rbp_object == second->zb_object &&
1627 	    first->rbp_blkid > second->zb_blkid)) {
1628 		return (1);
1629 	}
1630 
1631 	return (0);
1632 }
1633 
1634 /*
1635  * Traverse the redaction list in the provided object, and call the callback for
1636  * each entry we find. Don't call the callback for any records before resume.
1637  */
1638 int
dsl_redaction_list_traverse(redaction_list_t * rl,zbookmark_phys_t * resume,rl_traverse_callback_t cb,void * arg)1639 dsl_redaction_list_traverse(redaction_list_t *rl, zbookmark_phys_t *resume,
1640     rl_traverse_callback_t cb, void *arg)
1641 {
1642 	objset_t *mos = rl->rl_mos;
1643 	int err = 0;
1644 
1645 	if (rl->rl_phys->rlp_last_object != UINT64_MAX ||
1646 	    rl->rl_phys->rlp_last_blkid != UINT64_MAX) {
1647 		/*
1648 		 * When we finish a send, we update the last object and offset
1649 		 * to UINT64_MAX.  If a send fails partway through, the last
1650 		 * object and offset will have some other value, indicating how
1651 		 * far the send got. The redaction list must be complete before
1652 		 * it can be traversed, so return EINVAL if the last object and
1653 		 * blkid are not set to UINT64_MAX.
1654 		 */
1655 		return (SET_ERROR(EINVAL));
1656 	}
1657 
1658 	/*
1659 	 * This allows us to skip the binary search and resume checking logic
1660 	 * below, if we're not resuming a redacted send.
1661 	 */
1662 	if (ZB_IS_ZERO(resume))
1663 		resume = NULL;
1664 
1665 	/*
1666 	 * Binary search for the point to resume from.
1667 	 */
1668 	uint64_t maxidx = rl->rl_phys->rlp_num_entries - 1;
1669 	uint64_t minidx = 0;
1670 	while (resume != NULL && maxidx > minidx) {
1671 		redact_block_phys_t rbp = { 0 };
1672 		ASSERT3U(maxidx, >, minidx);
1673 		uint64_t mididx = minidx + ((maxidx - minidx) / 2);
1674 		err = dmu_read(mos, rl->rl_object, mididx * sizeof (rbp),
1675 		    sizeof (rbp), &rbp, DMU_READ_NO_PREFETCH);
1676 		if (err != 0)
1677 			break;
1678 
1679 		int cmp = redact_block_zb_compare(&rbp, resume);
1680 
1681 		if (cmp == 0) {
1682 			minidx = mididx;
1683 			break;
1684 		} else if (cmp > 0) {
1685 			maxidx =
1686 			    (mididx == minidx ? minidx : mididx - 1);
1687 		} else {
1688 			minidx = mididx + 1;
1689 		}
1690 	}
1691 
1692 	unsigned int bufsize = SPA_OLD_MAXBLOCKSIZE;
1693 	redact_block_phys_t *buf = zio_data_buf_alloc(bufsize);
1694 
1695 	unsigned int entries_per_buf = bufsize / sizeof (redact_block_phys_t);
1696 	uint64_t start_block = minidx / entries_per_buf;
1697 	err = dmu_read(mos, rl->rl_object, start_block * bufsize, bufsize, buf,
1698 	    DMU_READ_PREFETCH);
1699 
1700 	for (uint64_t curidx = minidx;
1701 	    err == 0 && curidx < rl->rl_phys->rlp_num_entries;
1702 	    curidx++) {
1703 		/*
1704 		 * We read in the redaction list one block at a time.  Once we
1705 		 * finish with all the entries in a given block, we read in a
1706 		 * new one.  The predictive prefetcher will take care of any
1707 		 * prefetching, and this code shouldn't be the bottleneck, so we
1708 		 * don't need to do manual prefetching.
1709 		 */
1710 		if (curidx % entries_per_buf == 0) {
1711 			err = dmu_read(mos, rl->rl_object, curidx *
1712 			    sizeof (*buf), bufsize, buf,
1713 			    DMU_READ_PREFETCH);
1714 			if (err != 0)
1715 				break;
1716 		}
1717 		redact_block_phys_t *rb = &buf[curidx % entries_per_buf];
1718 		/*
1719 		 * If resume is non-null, we should either not send the data, or
1720 		 * null out resume so we don't have to keep doing these
1721 		 * comparisons.
1722 		 */
1723 		if (resume != NULL) {
1724 			/*
1725 			 * It is possible that after the binary search we got
1726 			 * a record before the resume point. There's two cases
1727 			 * where this can occur. If the record is the last
1728 			 * redaction record, and the resume point is after the
1729 			 * end of the redacted data, curidx will be the last
1730 			 * redaction record. In that case, the loop will end
1731 			 * after this iteration. The second case is if the
1732 			 * resume point is between two redaction records, the
1733 			 * binary search can return either the record before
1734 			 * or after the resume point. In that case, the next
1735 			 * iteration will be greater than the resume point.
1736 			 */
1737 			if (redact_block_zb_compare(rb, resume) < 0) {
1738 				ASSERT3U(curidx, ==, minidx);
1739 				continue;
1740 			} else {
1741 				/*
1742 				 * If the place to resume is in the middle of
1743 				 * the range described by this
1744 				 * redact_block_phys, then modify the
1745 				 * redact_block_phys in memory so we generate
1746 				 * the right records.
1747 				 */
1748 				if (resume->zb_object == rb->rbp_object &&
1749 				    resume->zb_blkid > rb->rbp_blkid) {
1750 					uint64_t diff = resume->zb_blkid -
1751 					    rb->rbp_blkid;
1752 					rb->rbp_blkid = resume->zb_blkid;
1753 					redact_block_set_count(rb,
1754 					    redact_block_get_count(rb) - diff);
1755 				}
1756 				resume = NULL;
1757 			}
1758 		}
1759 
1760 		if (cb(rb, arg) != 0) {
1761 			err = EINTR;
1762 			break;
1763 		}
1764 	}
1765 
1766 	zio_data_buf_free(buf, bufsize);
1767 	return (err);
1768 }
1769