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