xref: /freebsd/sys/contrib/openzfs/module/zfs/spa_errlog.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  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2013, 2014, Delphix. All rights reserved.
15  * Copyright (c) 2019 Datto Inc.
16  * Copyright (c) 2021, 2022, George Amanakis. All rights reserved.
17  */
18 
19 /*
20  * Routines to manage the on-disk persistent error log.
21  *
22  * Each pool stores a log of all logical data errors seen during normal
23  * operation.  This is actually the union of two distinct logs: the last log,
24  * and the current log.  All errors seen are logged to the current log.  When a
25  * scrub completes, the current log becomes the last log, the last log is thrown
26  * out, and the current log is reinitialized.  This way, if an error is somehow
27  * corrected, a new scrub will show that it no longer exists, and will be
28  * deleted from the log when the scrub completes.
29  *
30  * The log is stored using a ZAP object whose key is a string form of the
31  * zbookmark_phys tuple (objset, object, level, blkid), and whose contents is an
32  * optional 'objset:object' human-readable string describing the data.  When an
33  * error is first logged, this string will be empty, indicating that no name is
34  * known.  This prevents us from having to issue a potentially large amount of
35  * I/O to discover the object name during an error path.  Instead, we do the
36  * calculation when the data is requested, storing the result so future queries
37  * will be faster.
38  *
39  * If the head_errlog feature is enabled, a different on-disk format is used.
40  * The error log of each head dataset is stored separately in the zap object
41  * and keyed by the head id. This enables listing every dataset affected in
42  * userland. In order to be able to track whether an error block has been
43  * modified or added to snapshots since it was marked as an error, a new tuple
44  * is introduced: zbookmark_err_phys_t. It allows the storage of the birth
45  * transaction group of an error block on-disk. The birth transaction group is
46  * used by check_filesystem() to assess whether this block was freed,
47  * re-written or added to a snapshot since its marking as an error.
48  *
49  * This log is then shipped into an nvlist where the key is the dataset name and
50  * the value is the object name.  Userland is then responsible for uniquifying
51  * this list and displaying it to the user.
52  */
53 
54 #include <sys/dmu_tx.h>
55 #include <sys/spa.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zap.h>
58 #include <sys/zio.h>
59 #include <sys/dsl_dir.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dbuf.h>
62 #include <sys/zfs_znode.h>
63 
64 #define	NAME_MAX_LEN 64
65 
66 typedef struct clones {
67 	uint64_t clone_ds;
68 	list_node_t node;
69 } clones_t;
70 
71 /*
72  * spa_upgrade_errlog_limit : A zfs module parameter that controls the number
73  *		of on-disk error log entries that will be converted to the new
74  *		format when enabling head_errlog. Defaults to 0 which converts
75  *		all log entries.
76  */
77 static uint_t spa_upgrade_errlog_limit = 0;
78 
79 /*
80  * Convert a bookmark to a string.
81  */
82 static void
bookmark_to_name(zbookmark_phys_t * zb,char * buf,size_t len)83 bookmark_to_name(zbookmark_phys_t *zb, char *buf, size_t len)
84 {
85 	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
86 	    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
87 	    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
88 }
89 
90 /*
91  * Convert an err_phys to a string.
92  */
93 static void
errphys_to_name(zbookmark_err_phys_t * zep,char * buf,size_t len)94 errphys_to_name(zbookmark_err_phys_t *zep, char *buf, size_t len)
95 {
96 	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
97 	    (u_longlong_t)zep->zb_object, (u_longlong_t)zep->zb_level,
98 	    (u_longlong_t)zep->zb_blkid, (u_longlong_t)zep->zb_birth);
99 }
100 
101 /*
102  * Convert a string to a err_phys.
103  */
104 void
name_to_errphys(char * buf,zbookmark_err_phys_t * zep)105 name_to_errphys(char *buf, zbookmark_err_phys_t *zep)
106 {
107 	zep->zb_object = zfs_strtonum(buf, &buf);
108 	ASSERT(*buf == ':');
109 	zep->zb_level = (int)zfs_strtonum(buf + 1, &buf);
110 	ASSERT(*buf == ':');
111 	zep->zb_blkid = zfs_strtonum(buf + 1, &buf);
112 	ASSERT(*buf == ':');
113 	zep->zb_birth = zfs_strtonum(buf + 1, &buf);
114 	ASSERT(*buf == '\0');
115 }
116 
117 /*
118  * Convert a string to a bookmark.
119  */
120 static void
name_to_bookmark(char * buf,zbookmark_phys_t * zb)121 name_to_bookmark(char *buf, zbookmark_phys_t *zb)
122 {
123 	zb->zb_objset = zfs_strtonum(buf, &buf);
124 	ASSERT(*buf == ':');
125 	zb->zb_object = zfs_strtonum(buf + 1, &buf);
126 	ASSERT(*buf == ':');
127 	zb->zb_level = (int)zfs_strtonum(buf + 1, &buf);
128 	ASSERT(*buf == ':');
129 	zb->zb_blkid = zfs_strtonum(buf + 1, &buf);
130 	ASSERT(*buf == '\0');
131 }
132 
133 void
zep_to_zb(uint64_t dataset,zbookmark_err_phys_t * zep,zbookmark_phys_t * zb)134 zep_to_zb(uint64_t dataset, zbookmark_err_phys_t *zep, zbookmark_phys_t *zb)
135 {
136 	zb->zb_objset = dataset;
137 	zb->zb_object = zep->zb_object;
138 	zb->zb_level = zep->zb_level;
139 	zb->zb_blkid = zep->zb_blkid;
140 }
141 
142 static void
name_to_object(char * buf,uint64_t * obj)143 name_to_object(char *buf, uint64_t *obj)
144 {
145 	*obj = zfs_strtonum(buf, &buf);
146 	ASSERT(*buf == '\0');
147 }
148 
149 /*
150  * Retrieve the head filesystem.
151  */
get_head_ds(spa_t * spa,uint64_t dsobj,uint64_t * head_ds)152 static int get_head_ds(spa_t *spa, uint64_t dsobj, uint64_t *head_ds)
153 {
154 	dsl_dataset_t *ds;
155 	int error = dsl_dataset_hold_obj_flags(spa->spa_dsl_pool,
156 	    dsobj, DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
157 
158 	if (error != 0)
159 		return (error);
160 
161 	ASSERT(head_ds);
162 	*head_ds = dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj;
163 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
164 
165 	return (error);
166 }
167 
168 /*
169  * Log an uncorrectable error to the persistent error log.  We add it to the
170  * spa's list of pending errors.  The changes are actually synced out to disk
171  * during spa_errlog_sync().
172  */
173 void
spa_log_error(spa_t * spa,const zbookmark_phys_t * zb,const uint64_t birth)174 spa_log_error(spa_t *spa, const zbookmark_phys_t *zb, const uint64_t birth)
175 {
176 	spa_error_entry_t search;
177 	spa_error_entry_t *new;
178 	avl_tree_t *tree;
179 	avl_index_t where;
180 
181 	/*
182 	 * If we are trying to import a pool, ignore any errors, as we won't be
183 	 * writing to the pool any time soon.
184 	 */
185 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
186 		return;
187 
188 	mutex_enter(&spa->spa_errlist_lock);
189 
190 	/*
191 	 * If we have had a request to rotate the log, log it to the next list
192 	 * instead of the current one.
193 	 */
194 	if (spa->spa_scrub_active || spa->spa_scrub_finished)
195 		tree = &spa->spa_errlist_scrub;
196 	else
197 		tree = &spa->spa_errlist_last;
198 
199 	search.se_bookmark = *zb;
200 	if (avl_find(tree, &search, &where) != NULL) {
201 		mutex_exit(&spa->spa_errlist_lock);
202 		return;
203 	}
204 
205 	new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
206 	new->se_bookmark = *zb;
207 
208 	/*
209 	 * If the head_errlog feature is enabled, store the birth txg now. In
210 	 * case the file is deleted before spa_errlog_sync() runs, we will not
211 	 * be able to retrieve the birth txg.
212 	 */
213 	if (spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
214 		new->se_zep.zb_object = zb->zb_object;
215 		new->se_zep.zb_level = zb->zb_level;
216 		new->se_zep.zb_blkid = zb->zb_blkid;
217 		new->se_zep.zb_birth = birth;
218 	}
219 
220 	avl_insert(tree, new, where);
221 	mutex_exit(&spa->spa_errlist_lock);
222 }
223 
224 int
find_birth_txg(dsl_dataset_t * ds,zbookmark_err_phys_t * zep,uint64_t * birth_txg)225 find_birth_txg(dsl_dataset_t *ds, zbookmark_err_phys_t *zep,
226     uint64_t *birth_txg)
227 {
228 	objset_t *os;
229 	int error = dmu_objset_from_ds(ds, &os);
230 	if (error != 0)
231 		return (error);
232 
233 	dnode_t *dn;
234 	blkptr_t bp;
235 
236 	error = dnode_hold(os, zep->zb_object, FTAG, &dn);
237 	if (error != 0)
238 		return (error);
239 
240 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
241 	error = dbuf_dnode_findbp(dn, zep->zb_level, zep->zb_blkid, &bp, NULL,
242 	    NULL);
243 	if (error == 0 && BP_IS_HOLE(&bp))
244 		error = SET_ERROR(ENOENT);
245 
246 	*birth_txg = BP_GET_PHYSICAL_BIRTH(&bp);
247 	rw_exit(&dn->dn_struct_rwlock);
248 	dnode_rele(dn, FTAG);
249 	return (error);
250 }
251 
252 /*
253  * This function finds the oldest affected filesystem containing an error
254  * block.
255  */
256 int
find_top_affected_fs(spa_t * spa,uint64_t head_ds,zbookmark_err_phys_t * zep,uint64_t * top_affected_fs)257 find_top_affected_fs(spa_t *spa, uint64_t head_ds, zbookmark_err_phys_t *zep,
258     uint64_t *top_affected_fs)
259 {
260 	uint64_t oldest_dsobj;
261 	int error = dsl_dataset_oldest_snapshot(spa, head_ds, zep->zb_birth,
262 	    &oldest_dsobj);
263 	if (error != 0)
264 		return (error);
265 
266 	dsl_dataset_t *ds;
267 	error = dsl_dataset_hold_obj_flags(spa->spa_dsl_pool, oldest_dsobj,
268 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
269 	if (error != 0)
270 		return (error);
271 
272 	*top_affected_fs =
273 	    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj;
274 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
275 	return (0);
276 }
277 
278 
279 #ifdef _KERNEL
280 /*
281  * Copy the bookmark to the end of the user-space buffer which starts at
282  * uaddr and has *count unused entries, and decrement *count by 1.
283  */
284 static int
copyout_entry(const zbookmark_phys_t * zb,void * uaddr,uint64_t * count)285 copyout_entry(const zbookmark_phys_t *zb, void *uaddr, uint64_t *count)
286 {
287 	if (*count == 0)
288 		return (SET_ERROR(ENOMEM));
289 
290 	*count -= 1;
291 	if (copyout(zb, (char *)uaddr + (*count) * sizeof (zbookmark_phys_t),
292 	    sizeof (zbookmark_phys_t)) != 0)
293 		return (SET_ERROR(EFAULT));
294 	return (0);
295 }
296 
297 /*
298  * Each time the error block is referenced by a snapshot or clone, add a
299  * zbookmark_phys_t entry to the userspace array at uaddr. The array is
300  * filled from the back and the in-out parameter *count is modified to be the
301  * number of unused entries at the beginning of the array. The function
302  * scrub_filesystem() is modelled after this one.
303  */
304 static int
check_filesystem(spa_t * spa,uint64_t head_ds,zbookmark_err_phys_t * zep,void * uaddr,uint64_t * count,list_t * clones_list)305 check_filesystem(spa_t *spa, uint64_t head_ds, zbookmark_err_phys_t *zep,
306     void *uaddr, uint64_t *count, list_t *clones_list)
307 {
308 	dsl_dataset_t *ds;
309 	dsl_pool_t *dp = spa->spa_dsl_pool;
310 
311 	int error = dsl_dataset_hold_obj_flags(dp, head_ds,
312 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
313 	if (error != 0)
314 		return (error);
315 
316 	uint64_t latest_txg;
317 	uint64_t txg_to_consider = spa->spa_syncing_txg;
318 	boolean_t check_snapshot = B_TRUE;
319 	error = find_birth_txg(ds, zep, &latest_txg);
320 
321 	/*
322 	 * If find_birth_txg() errors out otherwise, let txg_to_consider be
323 	 * equal to the spa's syncing txg: if check_filesystem() errors out
324 	 * then affected snapshots or clones will not be checked.
325 	 */
326 	if (error == 0 && zep->zb_birth == latest_txg) {
327 		/* Block neither free nor rewritten. */
328 		zbookmark_phys_t zb;
329 		zep_to_zb(head_ds, zep, &zb);
330 		error = copyout_entry(&zb, uaddr, count);
331 		if (error != 0) {
332 			dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
333 			return (error);
334 		}
335 		check_snapshot = B_FALSE;
336 	} else if (error == 0) {
337 		txg_to_consider = latest_txg;
338 	}
339 
340 	/*
341 	 * Retrieve the number of snapshots if the dataset is not a snapshot.
342 	 */
343 	uint64_t snap_count = 0;
344 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
345 
346 		error = zap_count(spa->spa_meta_objset,
347 		    dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
348 
349 		if (error != 0) {
350 			dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
351 			return (error);
352 		}
353 	}
354 
355 	if (snap_count == 0) {
356 		/* Filesystem without snapshots. */
357 		dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
358 		return (0);
359 	}
360 
361 	uint64_t *snap_obj_array = kmem_zalloc(snap_count * sizeof (uint64_t),
362 	    KM_SLEEP);
363 
364 	int aff_snap_count = 0;
365 	uint64_t snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
366 	uint64_t snap_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
367 	uint64_t zap_clone = dsl_dir_phys(ds->ds_dir)->dd_clones;
368 
369 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
370 
371 	/* Check only snapshots created from this file system. */
372 	while (snap_obj != 0 && zep->zb_birth < snap_obj_txg &&
373 	    snap_obj_txg <= txg_to_consider) {
374 
375 		error = dsl_dataset_hold_obj_flags(dp, snap_obj,
376 		    DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
377 		if (error != 0)
378 			goto out;
379 
380 		if (dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj != head_ds) {
381 			snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
382 			snap_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
383 			dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
384 			continue;
385 		}
386 
387 		boolean_t affected = B_TRUE;
388 		if (check_snapshot) {
389 			uint64_t blk_txg;
390 			error = find_birth_txg(ds, zep, &blk_txg);
391 			affected = (error == 0 && zep->zb_birth == blk_txg);
392 		}
393 
394 		/* Report errors in snapshots. */
395 		if (affected) {
396 			snap_obj_array[aff_snap_count] = snap_obj;
397 			aff_snap_count++;
398 
399 			zbookmark_phys_t zb;
400 			zep_to_zb(snap_obj, zep, &zb);
401 			error = copyout_entry(&zb, uaddr, count);
402 			if (error != 0) {
403 				dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT,
404 				    FTAG);
405 				goto out;
406 			}
407 		}
408 		snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
409 		snap_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
410 		dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
411 	}
412 
413 	if (zap_clone == 0 || aff_snap_count == 0) {
414 		error = 0;
415 		goto out;
416 	}
417 
418 	/* Check clones. */
419 	zap_cursor_t *zc;
420 	zap_attribute_t *za;
421 
422 	zc = kmem_zalloc(sizeof (zap_cursor_t), KM_SLEEP);
423 	za = zap_attribute_alloc();
424 
425 	for (zap_cursor_init(zc, spa->spa_meta_objset, zap_clone);
426 	    zap_cursor_retrieve(zc, za) == 0;
427 	    zap_cursor_advance(zc)) {
428 
429 		dsl_dataset_t *clone;
430 		error = dsl_dataset_hold_obj_flags(dp, za->za_first_integer,
431 		    DS_HOLD_FLAG_DECRYPT, FTAG, &clone);
432 
433 		if (error != 0)
434 			break;
435 
436 		/*
437 		 * Only clones whose origins were affected could also
438 		 * have affected snapshots.
439 		 */
440 		boolean_t found = B_FALSE;
441 		for (int i = 0; i < snap_count; i++) {
442 			if (dsl_dir_phys(clone->ds_dir)->dd_origin_obj
443 			    == snap_obj_array[i])
444 				found = B_TRUE;
445 		}
446 		dsl_dataset_rele_flags(clone, DS_HOLD_FLAG_DECRYPT, FTAG);
447 
448 		if (!found)
449 			continue;
450 
451 		clones_t *ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
452 		ct->clone_ds = za->za_first_integer;
453 		list_insert_tail(clones_list, ct);
454 	}
455 
456 	zap_cursor_fini(zc);
457 	zap_attribute_free(za);
458 	kmem_free(zc, sizeof (*zc));
459 
460 out:
461 	kmem_free(snap_obj_array, snap_count * sizeof (*snap_obj_array));
462 	return (error);
463 }
464 
465 static int
process_error_block(spa_t * spa,uint64_t head_ds,zbookmark_err_phys_t * zep,void * uaddr,uint64_t * count)466 process_error_block(spa_t *spa, uint64_t head_ds, zbookmark_err_phys_t *zep,
467     void *uaddr, uint64_t *count)
468 {
469 	/*
470 	 * If zb_birth == 0 or head_ds == 0 it means we failed to retrieve the
471 	 * birth txg or the head filesystem of the block pointer. This may
472 	 * happen e.g. when an encrypted filesystem is not mounted or when
473 	 * the key is not loaded. In this case do not proceed to
474 	 * check_filesystem(), instead do the accounting here.
475 	 */
476 	if (zep->zb_birth == 0 || head_ds == 0) {
477 		zbookmark_phys_t zb;
478 		zep_to_zb(head_ds, zep, &zb);
479 		int error = copyout_entry(&zb, uaddr, count);
480 		if (error != 0) {
481 			return (error);
482 		}
483 		return (0);
484 	}
485 
486 	uint64_t top_affected_fs;
487 	uint64_t init_count = *count;
488 	int error = find_top_affected_fs(spa, head_ds, zep, &top_affected_fs);
489 	if (error == 0) {
490 		clones_t *ct;
491 		list_t clones_list;
492 
493 		list_create(&clones_list, sizeof (clones_t),
494 		    offsetof(clones_t, node));
495 
496 		error = check_filesystem(spa, top_affected_fs, zep,
497 		    uaddr, count, &clones_list);
498 
499 		while ((ct = list_remove_head(&clones_list)) != NULL) {
500 			error = check_filesystem(spa, ct->clone_ds, zep,
501 			    uaddr, count, &clones_list);
502 			kmem_free(ct, sizeof (*ct));
503 
504 			if (error) {
505 				while (!list_is_empty(&clones_list)) {
506 					ct = list_remove_head(&clones_list);
507 					kmem_free(ct, sizeof (*ct));
508 				}
509 				break;
510 			}
511 		}
512 
513 		list_destroy(&clones_list);
514 	}
515 	if (error == 0 && init_count == *count) {
516 		/*
517 		 * If we reach this point, no errors have been detected
518 		 * in the checked filesystems/snapshots. Before returning mark
519 		 * the error block to be removed from the error lists and logs.
520 		 */
521 		zbookmark_phys_t zb;
522 		zep_to_zb(head_ds, zep, &zb);
523 		spa_remove_error(spa, &zb, zep->zb_birth);
524 	}
525 
526 	return (error);
527 }
528 #endif
529 
530 /* Return the number of errors in the error log */
531 uint64_t
spa_get_last_errlog_size(spa_t * spa)532 spa_get_last_errlog_size(spa_t *spa)
533 {
534 	uint64_t total = 0, count;
535 	mutex_enter(&spa->spa_errlog_lock);
536 
537 	if (spa->spa_errlog_last != 0 &&
538 	    zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
539 	    &count) == 0)
540 		total += count;
541 	mutex_exit(&spa->spa_errlog_lock);
542 	return (total);
543 }
544 
545 /*
546  * If a healed bookmark matches an entry in the error log we stash it in a tree
547  * so that we can later remove the related log entries in sync context.
548  */
549 static void
spa_add_healed_error(spa_t * spa,uint64_t obj,zbookmark_phys_t * healed_zb,const uint64_t birth)550 spa_add_healed_error(spa_t *spa, uint64_t obj, zbookmark_phys_t *healed_zb,
551     const uint64_t birth)
552 {
553 	char name[NAME_MAX_LEN];
554 
555 	if (obj == 0)
556 		return;
557 
558 	boolean_t held_list = B_FALSE;
559 	boolean_t held_log = B_FALSE;
560 
561 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
562 		bookmark_to_name(healed_zb, name, sizeof (name));
563 
564 		if (zap_contains(spa->spa_meta_objset, healed_zb->zb_objset,
565 		    name) == 0) {
566 			if (!MUTEX_HELD(&spa->spa_errlog_lock)) {
567 				mutex_enter(&spa->spa_errlog_lock);
568 				held_log = B_TRUE;
569 			}
570 
571 			/*
572 			 * Found an error matching healed zb, add zb to our
573 			 * tree of healed errors
574 			 */
575 			avl_tree_t *tree = &spa->spa_errlist_healed;
576 			spa_error_entry_t search;
577 			spa_error_entry_t *new;
578 			avl_index_t where;
579 			search.se_bookmark = *healed_zb;
580 			if (!MUTEX_HELD(&spa->spa_errlist_lock)) {
581 				mutex_enter(&spa->spa_errlist_lock);
582 				held_list = B_TRUE;
583 			}
584 			if (avl_find(tree, &search, &where) != NULL) {
585 				if (held_list)
586 					mutex_exit(&spa->spa_errlist_lock);
587 				if (held_log)
588 					mutex_exit(&spa->spa_errlog_lock);
589 				return;
590 			}
591 			new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
592 			new->se_bookmark = *healed_zb;
593 			avl_insert(tree, new, where);
594 			if (held_list)
595 				mutex_exit(&spa->spa_errlist_lock);
596 			if (held_log)
597 				mutex_exit(&spa->spa_errlog_lock);
598 		}
599 		return;
600 	}
601 
602 	zbookmark_err_phys_t healed_zep;
603 	healed_zep.zb_object = healed_zb->zb_object;
604 	healed_zep.zb_level = healed_zb->zb_level;
605 	healed_zep.zb_blkid = healed_zb->zb_blkid;
606 	healed_zep.zb_birth = birth;
607 
608 	errphys_to_name(&healed_zep, name, sizeof (name));
609 
610 	zap_cursor_t zc;
611 	zap_attribute_t *za = zap_attribute_alloc();
612 	for (zap_cursor_init(&zc, spa->spa_meta_objset, spa->spa_errlog_last);
613 	    zap_cursor_retrieve(&zc, za) == 0; zap_cursor_advance(&zc)) {
614 		if (zap_contains(spa->spa_meta_objset, za->za_first_integer,
615 		    name) == 0) {
616 			if (!MUTEX_HELD(&spa->spa_errlog_lock)) {
617 				mutex_enter(&spa->spa_errlog_lock);
618 				held_log = B_TRUE;
619 			}
620 
621 			avl_tree_t *tree = &spa->spa_errlist_healed;
622 			spa_error_entry_t search;
623 			spa_error_entry_t *new;
624 			avl_index_t where;
625 			search.se_bookmark = *healed_zb;
626 
627 			if (!MUTEX_HELD(&spa->spa_errlist_lock)) {
628 				mutex_enter(&spa->spa_errlist_lock);
629 				held_list = B_TRUE;
630 			}
631 
632 			if (avl_find(tree, &search, &where) != NULL) {
633 				if (held_list)
634 					mutex_exit(&spa->spa_errlist_lock);
635 				if (held_log)
636 					mutex_exit(&spa->spa_errlog_lock);
637 				continue;
638 			}
639 			new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
640 			new->se_bookmark = *healed_zb;
641 			new->se_zep = healed_zep;
642 			avl_insert(tree, new, where);
643 
644 			if (held_list)
645 				mutex_exit(&spa->spa_errlist_lock);
646 			if (held_log)
647 				mutex_exit(&spa->spa_errlog_lock);
648 		}
649 	}
650 	zap_cursor_fini(&zc);
651 	zap_attribute_free(za);
652 }
653 
654 /*
655  * If this error exists in the given tree remove it.
656  */
657 static void
remove_error_from_list(spa_t * spa,avl_tree_t * t,const zbookmark_phys_t * zb)658 remove_error_from_list(spa_t *spa, avl_tree_t *t, const zbookmark_phys_t *zb)
659 {
660 	spa_error_entry_t search, *found;
661 	avl_index_t where;
662 
663 	mutex_enter(&spa->spa_errlist_lock);
664 	search.se_bookmark = *zb;
665 	if ((found = avl_find(t, &search, &where)) != NULL) {
666 		avl_remove(t, found);
667 		kmem_free(found, sizeof (spa_error_entry_t));
668 	}
669 	mutex_exit(&spa->spa_errlist_lock);
670 }
671 
672 
673 /*
674  * Removes all of the recv healed errors from both on-disk error logs
675  */
676 static void
spa_remove_healed_errors(spa_t * spa,avl_tree_t * s,avl_tree_t * l,dmu_tx_t * tx)677 spa_remove_healed_errors(spa_t *spa, avl_tree_t *s, avl_tree_t *l, dmu_tx_t *tx)
678 {
679 	char name[NAME_MAX_LEN];
680 	spa_error_entry_t *se;
681 	void *cookie = NULL;
682 
683 	ASSERT(MUTEX_HELD(&spa->spa_errlog_lock));
684 
685 	while ((se = avl_destroy_nodes(&spa->spa_errlist_healed,
686 	    &cookie)) != NULL) {
687 		remove_error_from_list(spa, s, &se->se_bookmark);
688 		remove_error_from_list(spa, l, &se->se_bookmark);
689 
690 		if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
691 			bookmark_to_name(&se->se_bookmark, name, sizeof (name));
692 			(void) zap_remove(spa->spa_meta_objset,
693 			    spa->spa_errlog_last, name, tx);
694 			(void) zap_remove(spa->spa_meta_objset,
695 			    spa->spa_errlog_scrub, name, tx);
696 		} else {
697 			errphys_to_name(&se->se_zep, name, sizeof (name));
698 			zap_cursor_t zc;
699 			zap_attribute_t *za = zap_attribute_alloc();
700 			for (zap_cursor_init(&zc, spa->spa_meta_objset,
701 			    spa->spa_errlog_last);
702 			    zap_cursor_retrieve(&zc, za) == 0;
703 			    zap_cursor_advance(&zc)) {
704 				zap_remove(spa->spa_meta_objset,
705 				    za->za_first_integer, name, tx);
706 			}
707 			zap_cursor_fini(&zc);
708 
709 			for (zap_cursor_init(&zc, spa->spa_meta_objset,
710 			    spa->spa_errlog_scrub);
711 			    zap_cursor_retrieve(&zc, za) == 0;
712 			    zap_cursor_advance(&zc)) {
713 				zap_remove(spa->spa_meta_objset,
714 				    za->za_first_integer, name, tx);
715 			}
716 			zap_cursor_fini(&zc);
717 			zap_attribute_free(za);
718 		}
719 		kmem_free(se, sizeof (spa_error_entry_t));
720 	}
721 }
722 
723 /*
724  * Stash away healed bookmarks to remove them from the on-disk error logs
725  * later in spa_remove_healed_errors().
726  */
727 void
spa_remove_error(spa_t * spa,zbookmark_phys_t * zb,uint64_t birth)728 spa_remove_error(spa_t *spa, zbookmark_phys_t *zb, uint64_t birth)
729 {
730 	spa_add_healed_error(spa, spa->spa_errlog_last, zb, birth);
731 	spa_add_healed_error(spa, spa->spa_errlog_scrub, zb, birth);
732 }
733 
734 static uint64_t
approx_errlog_size_impl(spa_t * spa,uint64_t spa_err_obj)735 approx_errlog_size_impl(spa_t *spa, uint64_t spa_err_obj)
736 {
737 	if (spa_err_obj == 0)
738 		return (0);
739 	uint64_t total = 0;
740 
741 	zap_cursor_t zc;
742 	zap_attribute_t *za = zap_attribute_alloc();
743 	for (zap_cursor_init(&zc, spa->spa_meta_objset, spa_err_obj);
744 	    zap_cursor_retrieve(&zc, za) == 0; zap_cursor_advance(&zc)) {
745 		uint64_t count;
746 		if (zap_count(spa->spa_meta_objset, za->za_first_integer,
747 		    &count) == 0)
748 			total += count;
749 	}
750 	zap_cursor_fini(&zc);
751 	zap_attribute_free(za);
752 	return (total);
753 }
754 
755 /*
756  * Return the approximate number of errors currently in the error log.  This
757  * will be nonzero if there are some errors, but otherwise it may be more
758  * or less than the number of entries returned by spa_get_errlog().
759  */
760 uint64_t
spa_approx_errlog_size(spa_t * spa)761 spa_approx_errlog_size(spa_t *spa)
762 {
763 	uint64_t total = 0;
764 
765 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
766 		mutex_enter(&spa->spa_errlog_lock);
767 		uint64_t count;
768 		if (spa->spa_errlog_scrub != 0 &&
769 		    zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub,
770 		    &count) == 0)
771 			total += count;
772 
773 		if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished &&
774 		    zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
775 		    &count) == 0)
776 			total += count;
777 		mutex_exit(&spa->spa_errlog_lock);
778 
779 	} else {
780 		mutex_enter(&spa->spa_errlog_lock);
781 		total += approx_errlog_size_impl(spa, spa->spa_errlog_last);
782 		total += approx_errlog_size_impl(spa, spa->spa_errlog_scrub);
783 		mutex_exit(&spa->spa_errlog_lock);
784 	}
785 	mutex_enter(&spa->spa_errlist_lock);
786 	total += avl_numnodes(&spa->spa_errlist_last);
787 	total += avl_numnodes(&spa->spa_errlist_scrub);
788 	mutex_exit(&spa->spa_errlist_lock);
789 	return (total);
790 }
791 
792 /*
793  * This function sweeps through an on-disk error log and stores all bookmarks
794  * as error bookmarks in a new ZAP object. At the end we discard the old one,
795  * and spa_update_errlog() will set the spa's on-disk error log to new ZAP
796  * object.
797  */
798 static void
sync_upgrade_errlog(spa_t * spa,uint64_t spa_err_obj,uint64_t * newobj,dmu_tx_t * tx)799 sync_upgrade_errlog(spa_t *spa, uint64_t spa_err_obj, uint64_t *newobj,
800     dmu_tx_t *tx)
801 {
802 	zap_cursor_t zc;
803 	zap_attribute_t *za;
804 	zbookmark_phys_t zb;
805 	uint64_t count;
806 
807 	*newobj = zap_create(spa->spa_meta_objset, DMU_OT_ERROR_LOG,
808 	    DMU_OT_NONE, 0, tx);
809 
810 	/*
811 	 * If we cannnot perform the upgrade we should clear the old on-disk
812 	 * error logs.
813 	 */
814 	if (zap_count(spa->spa_meta_objset, spa_err_obj, &count) != 0) {
815 		VERIFY0(dmu_object_free(spa->spa_meta_objset, spa_err_obj, tx));
816 		return;
817 	}
818 
819 	za = zap_attribute_alloc();
820 	for (zap_cursor_init(&zc, spa->spa_meta_objset, spa_err_obj);
821 	    zap_cursor_retrieve(&zc, za) == 0;
822 	    zap_cursor_advance(&zc)) {
823 		if (spa_upgrade_errlog_limit != 0 &&
824 		    zc.zc_cd == spa_upgrade_errlog_limit)
825 			break;
826 
827 		name_to_bookmark(za->za_name, &zb);
828 
829 		zbookmark_err_phys_t zep;
830 		zep.zb_object = zb.zb_object;
831 		zep.zb_level = zb.zb_level;
832 		zep.zb_blkid = zb.zb_blkid;
833 		zep.zb_birth = 0;
834 
835 		/*
836 		 * In case of an error we should simply continue instead of
837 		 * returning prematurely. See the next comment.
838 		 */
839 		uint64_t head_ds;
840 		dsl_pool_t *dp = spa->spa_dsl_pool;
841 		dsl_dataset_t *ds;
842 		objset_t *os;
843 
844 		int error = dsl_dataset_hold_obj_flags(dp, zb.zb_objset,
845 		    DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
846 		if (error != 0)
847 			continue;
848 
849 		head_ds = dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj;
850 
851 		/*
852 		 * The objset and the dnode are required for getting the block
853 		 * pointer, which is used to determine if BP_IS_HOLE(). If
854 		 * getting the objset or the dnode fails, do not create a
855 		 * zap entry (presuming we know the dataset) as this may create
856 		 * spurious errors that we cannot ever resolve. If an error is
857 		 * truly persistent, it should re-appear after a scan.
858 		 */
859 		if (dmu_objset_from_ds(ds, &os) != 0) {
860 			dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
861 			continue;
862 		}
863 
864 		dnode_t *dn;
865 		blkptr_t bp;
866 
867 		if (dnode_hold(os, zep.zb_object, FTAG, &dn) != 0) {
868 			dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
869 			continue;
870 		}
871 
872 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
873 		error = dbuf_dnode_findbp(dn, zep.zb_level, zep.zb_blkid, &bp,
874 		    NULL, NULL);
875 		if (error == EACCES)
876 			error = 0;
877 		else if (!error)
878 			zep.zb_birth = BP_GET_PHYSICAL_BIRTH(&bp);
879 
880 		rw_exit(&dn->dn_struct_rwlock);
881 		dnode_rele(dn, FTAG);
882 		dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
883 
884 		if (error != 0 || BP_IS_HOLE(&bp))
885 			continue;
886 
887 		uint64_t err_obj;
888 		error = zap_lookup_int_key(spa->spa_meta_objset, *newobj,
889 		    head_ds, &err_obj);
890 
891 		if (error == ENOENT) {
892 			err_obj = zap_create(spa->spa_meta_objset,
893 			    DMU_OT_ERROR_LOG, DMU_OT_NONE, 0, tx);
894 
895 			(void) zap_update_int_key(spa->spa_meta_objset,
896 			    *newobj, head_ds, err_obj, tx);
897 		}
898 
899 		char buf[64];
900 		errphys_to_name(&zep, buf, sizeof (buf));
901 
902 		const char *name = "";
903 		(void) zap_update(spa->spa_meta_objset, err_obj,
904 		    buf, 1, strlen(name) + 1, name, tx);
905 	}
906 	zap_cursor_fini(&zc);
907 	zap_attribute_free(za);
908 
909 	VERIFY0(dmu_object_free(spa->spa_meta_objset, spa_err_obj, tx));
910 }
911 
912 void
spa_upgrade_errlog(spa_t * spa,dmu_tx_t * tx)913 spa_upgrade_errlog(spa_t *spa, dmu_tx_t *tx)
914 {
915 	uint64_t newobj = 0;
916 
917 	mutex_enter(&spa->spa_errlog_lock);
918 	if (spa->spa_errlog_last != 0) {
919 		sync_upgrade_errlog(spa, spa->spa_errlog_last, &newobj, tx);
920 		spa->spa_errlog_last = newobj;
921 
922 		(void) zap_update(spa->spa_meta_objset,
923 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
924 		    sizeof (uint64_t), 1, &spa->spa_errlog_last, tx);
925 	}
926 
927 	if (spa->spa_errlog_scrub != 0) {
928 		sync_upgrade_errlog(spa, spa->spa_errlog_scrub, &newobj, tx);
929 		spa->spa_errlog_scrub = newobj;
930 
931 		(void) zap_update(spa->spa_meta_objset,
932 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
933 		    sizeof (uint64_t), 1, &spa->spa_errlog_scrub, tx);
934 	}
935 
936 	mutex_exit(&spa->spa_errlog_lock);
937 }
938 
939 #ifdef _KERNEL
940 /*
941  * If an error block is shared by two datasets it will be counted twice.
942  */
943 static int
process_error_log(spa_t * spa,uint64_t obj,void * uaddr,uint64_t * count)944 process_error_log(spa_t *spa, uint64_t obj, void *uaddr, uint64_t *count)
945 {
946 	if (obj == 0)
947 		return (0);
948 
949 	zap_cursor_t *zc;
950 	zap_attribute_t *za;
951 
952 	zc = kmem_zalloc(sizeof (zap_cursor_t), KM_SLEEP);
953 	za = zap_attribute_alloc();
954 
955 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
956 		for (zap_cursor_init(zc, spa->spa_meta_objset, obj);
957 		    zap_cursor_retrieve(zc, za) == 0;
958 		    zap_cursor_advance(zc)) {
959 			if (*count == 0) {
960 				zap_cursor_fini(zc);
961 				kmem_free(zc, sizeof (*zc));
962 				zap_attribute_free(za);
963 				return (SET_ERROR(ENOMEM));
964 			}
965 
966 			zbookmark_phys_t zb;
967 			name_to_bookmark(za->za_name, &zb);
968 
969 			int error = copyout_entry(&zb, uaddr, count);
970 			if (error != 0) {
971 				zap_cursor_fini(zc);
972 				kmem_free(zc, sizeof (*zc));
973 				zap_attribute_free(za);
974 				return (error);
975 			}
976 		}
977 		zap_cursor_fini(zc);
978 		kmem_free(zc, sizeof (*zc));
979 		zap_attribute_free(za);
980 		return (0);
981 	}
982 
983 	for (zap_cursor_init(zc, spa->spa_meta_objset, obj);
984 	    zap_cursor_retrieve(zc, za) == 0;
985 	    zap_cursor_advance(zc)) {
986 
987 		zap_cursor_t *head_ds_cursor;
988 		zap_attribute_t *head_ds_attr;
989 
990 		head_ds_cursor = kmem_zalloc(sizeof (zap_cursor_t), KM_SLEEP);
991 		head_ds_attr = zap_attribute_alloc();
992 
993 		uint64_t head_ds_err_obj = za->za_first_integer;
994 		uint64_t head_ds;
995 		name_to_object(za->za_name, &head_ds);
996 		for (zap_cursor_init(head_ds_cursor, spa->spa_meta_objset,
997 		    head_ds_err_obj); zap_cursor_retrieve(head_ds_cursor,
998 		    head_ds_attr) == 0; zap_cursor_advance(head_ds_cursor)) {
999 
1000 			zbookmark_err_phys_t head_ds_block;
1001 			name_to_errphys(head_ds_attr->za_name, &head_ds_block);
1002 			int error = process_error_block(spa, head_ds,
1003 			    &head_ds_block, uaddr, count);
1004 
1005 			if (error != 0) {
1006 				zap_cursor_fini(head_ds_cursor);
1007 				kmem_free(head_ds_cursor,
1008 				    sizeof (*head_ds_cursor));
1009 				zap_attribute_free(head_ds_attr);
1010 
1011 				zap_cursor_fini(zc);
1012 				zap_attribute_free(za);
1013 				kmem_free(zc, sizeof (*zc));
1014 				return (error);
1015 			}
1016 		}
1017 		zap_cursor_fini(head_ds_cursor);
1018 		kmem_free(head_ds_cursor, sizeof (*head_ds_cursor));
1019 		zap_attribute_free(head_ds_attr);
1020 	}
1021 	zap_cursor_fini(zc);
1022 	zap_attribute_free(za);
1023 	kmem_free(zc, sizeof (*zc));
1024 	return (0);
1025 }
1026 
1027 static int
process_error_list(spa_t * spa,avl_tree_t * list,void * uaddr,uint64_t * count)1028 process_error_list(spa_t *spa, avl_tree_t *list, void *uaddr, uint64_t *count)
1029 {
1030 	spa_error_entry_t *se;
1031 
1032 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
1033 		for (se = avl_first(list); se != NULL;
1034 		    se = AVL_NEXT(list, se)) {
1035 			int error =
1036 			    copyout_entry(&se->se_bookmark, uaddr, count);
1037 			if (error != 0) {
1038 				return (error);
1039 			}
1040 		}
1041 		return (0);
1042 	}
1043 
1044 	for (se = avl_first(list); se != NULL; se = AVL_NEXT(list, se)) {
1045 		uint64_t head_ds = 0;
1046 		int error = get_head_ds(spa, se->se_bookmark.zb_objset,
1047 		    &head_ds);
1048 
1049 		/*
1050 		 * If get_head_ds() errors out, set the head filesystem
1051 		 * to the filesystem stored in the bookmark of the
1052 		 * error block.
1053 		 */
1054 		if (error != 0)
1055 			head_ds = se->se_bookmark.zb_objset;
1056 
1057 		error = process_error_block(spa, head_ds,
1058 		    &se->se_zep, uaddr, count);
1059 		if (error != 0)
1060 			return (error);
1061 	}
1062 	return (0);
1063 }
1064 #endif
1065 
1066 /*
1067  * Copy all known errors to userland as an array of bookmarks.  This is
1068  * actually a union of the on-disk last log and current log, as well as any
1069  * pending error requests.
1070  *
1071  * Because the act of reading the on-disk log could cause errors to be
1072  * generated, we have two separate locks: one for the error log and one for the
1073  * in-core error lists.  We only need the error list lock to log and error, so
1074  * we grab the error log lock while we read the on-disk logs, and only pick up
1075  * the error list lock when we are finished.
1076  */
1077 int
spa_get_errlog(spa_t * spa,void * uaddr,uint64_t * count)1078 spa_get_errlog(spa_t *spa, void *uaddr, uint64_t *count)
1079 {
1080 	int ret = 0;
1081 
1082 #ifdef _KERNEL
1083 	/*
1084 	 * The pool config lock is needed to hold a dataset_t via (among other
1085 	 * places) process_error_list() -> process_error_block()->
1086 	 * find_top_affected_fs(), and lock ordering requires that we get it
1087 	 * before the spa_errlog_lock.
1088 	 */
1089 	dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
1090 	mutex_enter(&spa->spa_errlog_lock);
1091 
1092 	ret = process_error_log(spa, spa->spa_errlog_scrub, uaddr, count);
1093 
1094 	if (!ret && !spa->spa_scrub_finished)
1095 		ret = process_error_log(spa, spa->spa_errlog_last, uaddr,
1096 		    count);
1097 
1098 	mutex_enter(&spa->spa_errlist_lock);
1099 	if (!ret)
1100 		ret = process_error_list(spa, &spa->spa_errlist_scrub, uaddr,
1101 		    count);
1102 	if (!ret)
1103 		ret = process_error_list(spa, &spa->spa_errlist_last, uaddr,
1104 		    count);
1105 	mutex_exit(&spa->spa_errlist_lock);
1106 
1107 	mutex_exit(&spa->spa_errlog_lock);
1108 	dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
1109 #else
1110 	(void) spa, (void) uaddr, (void) count;
1111 #endif
1112 
1113 	return (ret);
1114 }
1115 
1116 /*
1117  * Called when a scrub completes.  This simply set a bit which tells which AVL
1118  * tree to add new errors.  spa_errlog_sync() is responsible for actually
1119  * syncing the changes to the underlying objects.
1120  */
1121 void
spa_errlog_rotate(spa_t * spa)1122 spa_errlog_rotate(spa_t *spa)
1123 {
1124 	mutex_enter(&spa->spa_errlist_lock);
1125 	spa->spa_scrub_finished = B_TRUE;
1126 	mutex_exit(&spa->spa_errlist_lock);
1127 }
1128 
1129 /*
1130  * Discard any pending errors from the spa_t.  Called when unloading a faulted
1131  * pool, as the errors encountered during the open cannot be synced to disk.
1132  */
1133 void
spa_errlog_drain(spa_t * spa)1134 spa_errlog_drain(spa_t *spa)
1135 {
1136 	spa_error_entry_t *se;
1137 	void *cookie;
1138 
1139 	mutex_enter(&spa->spa_errlist_lock);
1140 
1141 	cookie = NULL;
1142 	while ((se = avl_destroy_nodes(&spa->spa_errlist_last,
1143 	    &cookie)) != NULL)
1144 		kmem_free(se, sizeof (spa_error_entry_t));
1145 	cookie = NULL;
1146 	while ((se = avl_destroy_nodes(&spa->spa_errlist_scrub,
1147 	    &cookie)) != NULL)
1148 		kmem_free(se, sizeof (spa_error_entry_t));
1149 
1150 	mutex_exit(&spa->spa_errlist_lock);
1151 }
1152 
1153 /*
1154  * Process a list of errors into the current on-disk log.
1155  */
1156 void
sync_error_list(spa_t * spa,avl_tree_t * t,uint64_t * obj,dmu_tx_t * tx)1157 sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj, dmu_tx_t *tx)
1158 {
1159 	spa_error_entry_t *se;
1160 	char buf[NAME_MAX_LEN];
1161 	void *cookie;
1162 
1163 	if (avl_numnodes(t) == 0)
1164 		return;
1165 
1166 	/* create log if necessary */
1167 	if (*obj == 0)
1168 		*obj = zap_create(spa->spa_meta_objset, DMU_OT_ERROR_LOG,
1169 		    DMU_OT_NONE, 0, tx);
1170 
1171 	/* add errors to the current log */
1172 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
1173 		for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) {
1174 			bookmark_to_name(&se->se_bookmark, buf, sizeof (buf));
1175 
1176 			const char *name = se->se_name ? se->se_name : "";
1177 			(void) zap_update(spa->spa_meta_objset, *obj, buf, 1,
1178 			    strlen(name) + 1, name, tx);
1179 		}
1180 	} else {
1181 		for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) {
1182 			zbookmark_err_phys_t zep;
1183 			zep.zb_object = se->se_zep.zb_object;
1184 			zep.zb_level = se->se_zep.zb_level;
1185 			zep.zb_blkid = se->se_zep.zb_blkid;
1186 			zep.zb_birth = se->se_zep.zb_birth;
1187 
1188 			uint64_t head_ds = 0;
1189 			int error = get_head_ds(spa, se->se_bookmark.zb_objset,
1190 			    &head_ds);
1191 
1192 			/*
1193 			 * If get_head_ds() errors out, set the head filesystem
1194 			 * to the filesystem stored in the bookmark of the
1195 			 * error block.
1196 			 */
1197 			if (error != 0)
1198 				head_ds = se->se_bookmark.zb_objset;
1199 
1200 			uint64_t err_obj;
1201 			error = zap_lookup_int_key(spa->spa_meta_objset,
1202 			    *obj, head_ds, &err_obj);
1203 
1204 			if (error == ENOENT) {
1205 				err_obj = zap_create(spa->spa_meta_objset,
1206 				    DMU_OT_ERROR_LOG, DMU_OT_NONE, 0, tx);
1207 
1208 				(void) zap_update_int_key(spa->spa_meta_objset,
1209 				    *obj, head_ds, err_obj, tx);
1210 			}
1211 			errphys_to_name(&zep, buf, sizeof (buf));
1212 
1213 			const char *name = se->se_name ? se->se_name : "";
1214 			(void) zap_update(spa->spa_meta_objset,
1215 			    err_obj, buf, 1, strlen(name) + 1, name, tx);
1216 		}
1217 	}
1218 	/* purge the error list */
1219 	cookie = NULL;
1220 	while ((se = avl_destroy_nodes(t, &cookie)) != NULL)
1221 		kmem_free(se, sizeof (spa_error_entry_t));
1222 }
1223 
1224 static void
delete_errlog(spa_t * spa,uint64_t spa_err_obj,dmu_tx_t * tx)1225 delete_errlog(spa_t *spa, uint64_t spa_err_obj, dmu_tx_t *tx)
1226 {
1227 	if (spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
1228 		zap_cursor_t zc;
1229 		zap_attribute_t *za = zap_attribute_alloc();
1230 		for (zap_cursor_init(&zc, spa->spa_meta_objset, spa_err_obj);
1231 		    zap_cursor_retrieve(&zc, za) == 0;
1232 		    zap_cursor_advance(&zc)) {
1233 			VERIFY0(dmu_object_free(spa->spa_meta_objset,
1234 			    za->za_first_integer, tx));
1235 		}
1236 		zap_cursor_fini(&zc);
1237 		zap_attribute_free(za);
1238 	}
1239 	VERIFY0(dmu_object_free(spa->spa_meta_objset, spa_err_obj, tx));
1240 }
1241 
1242 /*
1243  * Sync the error log out to disk.  This is a little tricky because the act of
1244  * writing the error log requires the spa_errlist_lock.  So, we need to lock the
1245  * error lists, take a copy of the lists, and then reinitialize them.  Then, we
1246  * drop the error list lock and take the error log lock, at which point we
1247  * do the errlog processing.  Then, if we encounter an I/O error during this
1248  * process, we can successfully add the error to the list.  Note that this will
1249  * result in the perpetual recycling of errors, but it is an unlikely situation
1250  * and not a performance critical operation.
1251  */
1252 void
spa_errlog_sync(spa_t * spa,uint64_t txg)1253 spa_errlog_sync(spa_t *spa, uint64_t txg)
1254 {
1255 	dmu_tx_t *tx;
1256 	avl_tree_t scrub, last;
1257 	int scrub_finished;
1258 
1259 	mutex_enter(&spa->spa_errlist_lock);
1260 
1261 	/*
1262 	 * Bail out early under normal circumstances.
1263 	 */
1264 	if (avl_numnodes(&spa->spa_errlist_scrub) == 0 &&
1265 	    avl_numnodes(&spa->spa_errlist_last) == 0 &&
1266 	    avl_numnodes(&spa->spa_errlist_healed) == 0 &&
1267 	    !spa->spa_scrub_finished) {
1268 		mutex_exit(&spa->spa_errlist_lock);
1269 		return;
1270 	}
1271 
1272 	spa_get_errlists(spa, &last, &scrub);
1273 	scrub_finished = spa->spa_scrub_finished;
1274 	spa->spa_scrub_finished = B_FALSE;
1275 
1276 	mutex_exit(&spa->spa_errlist_lock);
1277 
1278 	/*
1279 	 * The pool config lock is needed to hold a dataset_t via
1280 	 * sync_error_list() -> get_head_ds(), and lock ordering
1281 	 * requires that we get it before the spa_errlog_lock.
1282 	 */
1283 	dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
1284 	mutex_enter(&spa->spa_errlog_lock);
1285 
1286 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1287 
1288 	/*
1289 	 * Remove healed errors from errors.
1290 	 */
1291 	spa_remove_healed_errors(spa, &last, &scrub, tx);
1292 
1293 	/*
1294 	 * Sync out the current list of errors.
1295 	 */
1296 	sync_error_list(spa, &last, &spa->spa_errlog_last, tx);
1297 
1298 	/*
1299 	 * Rotate the log if necessary.
1300 	 */
1301 	if (scrub_finished) {
1302 		if (spa->spa_errlog_last != 0)
1303 			delete_errlog(spa, spa->spa_errlog_last, tx);
1304 		spa->spa_errlog_last = spa->spa_errlog_scrub;
1305 		spa->spa_errlog_scrub = 0;
1306 
1307 		sync_error_list(spa, &scrub, &spa->spa_errlog_last, tx);
1308 	}
1309 
1310 	/*
1311 	 * Sync out any pending scrub errors.
1312 	 */
1313 	sync_error_list(spa, &scrub, &spa->spa_errlog_scrub, tx);
1314 
1315 	/*
1316 	 * Update the MOS to reflect the new values.
1317 	 */
1318 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1319 	    DMU_POOL_ERRLOG_LAST, sizeof (uint64_t), 1,
1320 	    &spa->spa_errlog_last, tx);
1321 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1322 	    DMU_POOL_ERRLOG_SCRUB, sizeof (uint64_t), 1,
1323 	    &spa->spa_errlog_scrub, tx);
1324 
1325 	dmu_tx_commit(tx);
1326 
1327 	mutex_exit(&spa->spa_errlog_lock);
1328 	dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
1329 }
1330 
1331 static void
delete_dataset_errlog(spa_t * spa,uint64_t spa_err_obj,uint64_t ds,dmu_tx_t * tx)1332 delete_dataset_errlog(spa_t *spa, uint64_t spa_err_obj, uint64_t ds,
1333     dmu_tx_t *tx)
1334 {
1335 	if (spa_err_obj == 0)
1336 		return;
1337 
1338 	zap_cursor_t zc;
1339 	zap_attribute_t *za = zap_attribute_alloc();
1340 	for (zap_cursor_init(&zc, spa->spa_meta_objset, spa_err_obj);
1341 	    zap_cursor_retrieve(&zc, za) == 0; zap_cursor_advance(&zc)) {
1342 		uint64_t head_ds;
1343 		name_to_object(za->za_name, &head_ds);
1344 		if (head_ds == ds) {
1345 			(void) zap_remove(spa->spa_meta_objset, spa_err_obj,
1346 			    za->za_name, tx);
1347 			VERIFY0(dmu_object_free(spa->spa_meta_objset,
1348 			    za->za_first_integer, tx));
1349 			break;
1350 		}
1351 	}
1352 	zap_cursor_fini(&zc);
1353 	zap_attribute_free(za);
1354 }
1355 
1356 void
spa_delete_dataset_errlog(spa_t * spa,uint64_t ds,dmu_tx_t * tx)1357 spa_delete_dataset_errlog(spa_t *spa, uint64_t ds, dmu_tx_t *tx)
1358 {
1359 	mutex_enter(&spa->spa_errlog_lock);
1360 	delete_dataset_errlog(spa, spa->spa_errlog_scrub, ds, tx);
1361 	delete_dataset_errlog(spa, spa->spa_errlog_last, ds, tx);
1362 	mutex_exit(&spa->spa_errlog_lock);
1363 }
1364 
1365 static int
find_txg_ancestor_snapshot(spa_t * spa,uint64_t new_head,uint64_t old_head,uint64_t * txg)1366 find_txg_ancestor_snapshot(spa_t *spa, uint64_t new_head, uint64_t old_head,
1367     uint64_t *txg)
1368 {
1369 	dsl_dataset_t *ds;
1370 	dsl_pool_t *dp = spa->spa_dsl_pool;
1371 
1372 	int error = dsl_dataset_hold_obj_flags(dp, old_head,
1373 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
1374 	if (error != 0)
1375 		return (error);
1376 
1377 	uint64_t prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1378 	uint64_t prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1379 
1380 	while (prev_obj != 0) {
1381 		dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1382 		if ((error = dsl_dataset_hold_obj_flags(dp, prev_obj,
1383 		    DS_HOLD_FLAG_DECRYPT, FTAG, &ds)) == 0 &&
1384 		    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj == new_head)
1385 			break;
1386 
1387 		if (error != 0)
1388 			return (error);
1389 
1390 		prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1391 		prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1392 	}
1393 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1394 	ASSERT(prev_obj != 0);
1395 	*txg = prev_obj_txg;
1396 	return (0);
1397 }
1398 
1399 static void
swap_errlog(spa_t * spa,uint64_t spa_err_obj,uint64_t new_head,uint64_t old_head,dmu_tx_t * tx)1400 swap_errlog(spa_t *spa, uint64_t spa_err_obj, uint64_t new_head, uint64_t
1401     old_head, dmu_tx_t *tx)
1402 {
1403 	if (spa_err_obj == 0)
1404 		return;
1405 
1406 	uint64_t old_head_errlog;
1407 	int error = zap_lookup_int_key(spa->spa_meta_objset, spa_err_obj,
1408 	    old_head, &old_head_errlog);
1409 
1410 	/* If no error log, then there is nothing to do. */
1411 	if (error != 0)
1412 		return;
1413 
1414 	uint64_t txg;
1415 	error = find_txg_ancestor_snapshot(spa, new_head, old_head, &txg);
1416 	if (error != 0)
1417 		return;
1418 
1419 	/*
1420 	 * Create an error log if the file system being promoted does not
1421 	 * already have one.
1422 	 */
1423 	uint64_t new_head_errlog;
1424 	error = zap_lookup_int_key(spa->spa_meta_objset, spa_err_obj, new_head,
1425 	    &new_head_errlog);
1426 
1427 	if (error != 0) {
1428 		new_head_errlog = zap_create(spa->spa_meta_objset,
1429 		    DMU_OT_ERROR_LOG, DMU_OT_NONE, 0, tx);
1430 
1431 		(void) zap_update_int_key(spa->spa_meta_objset, spa_err_obj,
1432 		    new_head, new_head_errlog, tx);
1433 	}
1434 
1435 	zap_cursor_t zc;
1436 	zap_attribute_t *za = zap_attribute_alloc();
1437 	zbookmark_err_phys_t err_block;
1438 	for (zap_cursor_init(&zc, spa->spa_meta_objset, old_head_errlog);
1439 	    zap_cursor_retrieve(&zc, za) == 0; zap_cursor_advance(&zc)) {
1440 
1441 		const char *name = "";
1442 		name_to_errphys(za->za_name, &err_block);
1443 		if (err_block.zb_birth < txg) {
1444 			(void) zap_update(spa->spa_meta_objset, new_head_errlog,
1445 			    za->za_name, 1, strlen(name) + 1, name, tx);
1446 
1447 			(void) zap_remove(spa->spa_meta_objset, old_head_errlog,
1448 			    za->za_name, tx);
1449 		}
1450 	}
1451 	zap_cursor_fini(&zc);
1452 	zap_attribute_free(za);
1453 }
1454 
1455 void
spa_swap_errlog(spa_t * spa,uint64_t new_head_ds,uint64_t old_head_ds,dmu_tx_t * tx)1456 spa_swap_errlog(spa_t *spa, uint64_t new_head_ds, uint64_t old_head_ds,
1457     dmu_tx_t *tx)
1458 {
1459 	mutex_enter(&spa->spa_errlog_lock);
1460 	swap_errlog(spa, spa->spa_errlog_scrub, new_head_ds, old_head_ds, tx);
1461 	swap_errlog(spa, spa->spa_errlog_last, new_head_ds, old_head_ds, tx);
1462 	mutex_exit(&spa->spa_errlog_lock);
1463 }
1464 
1465 #if defined(_KERNEL)
1466 /* error handling */
1467 EXPORT_SYMBOL(spa_log_error);
1468 EXPORT_SYMBOL(spa_approx_errlog_size);
1469 EXPORT_SYMBOL(spa_get_last_errlog_size);
1470 EXPORT_SYMBOL(spa_get_errlog);
1471 EXPORT_SYMBOL(spa_errlog_rotate);
1472 EXPORT_SYMBOL(spa_errlog_drain);
1473 EXPORT_SYMBOL(spa_errlog_sync);
1474 EXPORT_SYMBOL(spa_get_errlists);
1475 EXPORT_SYMBOL(spa_delete_dataset_errlog);
1476 EXPORT_SYMBOL(spa_swap_errlog);
1477 EXPORT_SYMBOL(sync_error_list);
1478 EXPORT_SYMBOL(spa_upgrade_errlog);
1479 EXPORT_SYMBOL(find_top_affected_fs);
1480 EXPORT_SYMBOL(find_birth_txg);
1481 EXPORT_SYMBOL(zep_to_zb);
1482 EXPORT_SYMBOL(name_to_errphys);
1483 #endif
1484 
1485 ZFS_MODULE_PARAM(zfs_spa, spa_, upgrade_errlog_limit, UINT, ZMOD_RW,
1486 	"Limit the number of errors which will be upgraded to the new "
1487 	"on-disk error log when enabling head_errlog");
1488