Print this page
3743 zfs needs a refcount audit
Submitted by: Will Andrews <willa@spectralogic.com>
Submitted by: Justin Gibbs <justing@spectralogic.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/spa_errlog.c
+++ new/usr/src/uts/common/fs/zfs/spa_errlog.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * Routines to manage the on-disk persistent error log.
28 28 *
29 29 * Each pool stores a log of all logical data errors seen during normal
30 30 * operation. This is actually the union of two distinct logs: the last log,
31 31 * and the current log. All errors seen are logged to the current log. When a
32 32 * scrub completes, the current log becomes the last log, the last log is thrown
33 33 * out, and the current log is reinitialized. This way, if an error is somehow
34 34 * corrected, a new scrub will show that that it no longer exists, and will be
35 35 * deleted from the log when the scrub completes.
36 36 *
37 37 * The log is stored using a ZAP object whose key is a string form of the
38 38 * zbookmark tuple (objset, object, level, blkid), and whose contents is an
39 39 * optional 'objset:object' human-readable string describing the data. When an
40 40 * error is first logged, this string will be empty, indicating that no name is
41 41 * known. This prevents us from having to issue a potentially large amount of
42 42 * I/O to discover the object name during an error path. Instead, we do the
43 43 * calculation when the data is requested, storing the result so future queries
44 44 * will be faster.
45 45 *
46 46 * This log is then shipped into an nvlist where the key is the dataset name and
47 47 * the value is the object name. Userland is then responsible for uniquifying
48 48 * this list and displaying it to the user.
49 49 */
50 50
51 51 #include <sys/dmu_tx.h>
52 52 #include <sys/spa.h>
53 53 #include <sys/spa_impl.h>
54 54 #include <sys/zap.h>
55 55 #include <sys/zio.h>
56 56
57 57
58 58 /*
59 59 * Convert a bookmark to a string.
60 60 */
61 61 static void
62 62 bookmark_to_name(zbookmark_t *zb, char *buf, size_t len)
63 63 {
64 64 (void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
65 65 (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
66 66 (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
67 67 }
68 68
69 69 /*
70 70 * Convert a string to a bookmark
71 71 */
72 72 #ifdef _KERNEL
73 73 static void
74 74 name_to_bookmark(char *buf, zbookmark_t *zb)
75 75 {
76 76 zb->zb_objset = strtonum(buf, &buf);
77 77 ASSERT(*buf == ':');
78 78 zb->zb_object = strtonum(buf + 1, &buf);
79 79 ASSERT(*buf == ':');
80 80 zb->zb_level = (int)strtonum(buf + 1, &buf);
81 81 ASSERT(*buf == ':');
82 82 zb->zb_blkid = strtonum(buf + 1, &buf);
83 83 ASSERT(*buf == '\0');
84 84 }
85 85 #endif
86 86
87 87 /*
88 88 * Log an uncorrectable error to the persistent error log. We add it to the
89 89 * spa's list of pending errors. The changes are actually synced out to disk
90 90 * during spa_errlog_sync().
91 91 */
92 92 void
93 93 spa_log_error(spa_t *spa, zio_t *zio)
94 94 {
95 95 zbookmark_t *zb = &zio->io_logical->io_bookmark;
96 96 spa_error_entry_t search;
97 97 spa_error_entry_t *new;
98 98 avl_tree_t *tree;
99 99 avl_index_t where;
100 100
101 101 /*
102 102 * If we are trying to import a pool, ignore any errors, as we won't be
103 103 * writing to the pool any time soon.
104 104 */
105 105 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
106 106 return;
107 107
108 108 mutex_enter(&spa->spa_errlist_lock);
109 109
110 110 /*
111 111 * If we have had a request to rotate the log, log it to the next list
112 112 * instead of the current one.
113 113 */
114 114 if (spa->spa_scrub_active || spa->spa_scrub_finished)
115 115 tree = &spa->spa_errlist_scrub;
116 116 else
117 117 tree = &spa->spa_errlist_last;
118 118
119 119 search.se_bookmark = *zb;
120 120 if (avl_find(tree, &search, &where) != NULL) {
121 121 mutex_exit(&spa->spa_errlist_lock);
122 122 return;
123 123 }
124 124
125 125 new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
126 126 new->se_bookmark = *zb;
127 127 avl_insert(tree, new, where);
128 128
129 129 mutex_exit(&spa->spa_errlist_lock);
130 130 }
131 131
132 132 /*
133 133 * Return the number of errors currently in the error log. This is actually the
134 134 * sum of both the last log and the current log, since we don't know the union
135 135 * of these logs until we reach userland.
136 136 */
137 137 uint64_t
138 138 spa_get_errlog_size(spa_t *spa)
139 139 {
140 140 uint64_t total = 0, count;
141 141
142 142 mutex_enter(&spa->spa_errlog_lock);
143 143 if (spa->spa_errlog_scrub != 0 &&
144 144 zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub,
145 145 &count) == 0)
146 146 total += count;
147 147
148 148 if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished &&
149 149 zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
150 150 &count) == 0)
151 151 total += count;
152 152 mutex_exit(&spa->spa_errlog_lock);
153 153
154 154 mutex_enter(&spa->spa_errlist_lock);
155 155 total += avl_numnodes(&spa->spa_errlist_last);
156 156 total += avl_numnodes(&spa->spa_errlist_scrub);
157 157 mutex_exit(&spa->spa_errlist_lock);
158 158
159 159 return (total);
160 160 }
161 161
162 162 #ifdef _KERNEL
163 163 static int
164 164 process_error_log(spa_t *spa, uint64_t obj, void *addr, size_t *count)
165 165 {
166 166 zap_cursor_t zc;
167 167 zap_attribute_t za;
168 168 zbookmark_t zb;
169 169
170 170 if (obj == 0)
171 171 return (0);
172 172
173 173 for (zap_cursor_init(&zc, spa->spa_meta_objset, obj);
174 174 zap_cursor_retrieve(&zc, &za) == 0;
175 175 zap_cursor_advance(&zc)) {
↓ open down ↓ |
175 lines elided |
↑ open up ↑ |
176 176
177 177 if (*count == 0) {
178 178 zap_cursor_fini(&zc);
179 179 return (SET_ERROR(ENOMEM));
180 180 }
181 181
182 182 name_to_bookmark(za.za_name, &zb);
183 183
184 184 if (copyout(&zb, (char *)addr +
185 185 (*count - 1) * sizeof (zbookmark_t),
186 - sizeof (zbookmark_t)) != 0)
186 + sizeof (zbookmark_t)) != 0) {
187 + zap_cursor_fini(&zc);
187 188 return (SET_ERROR(EFAULT));
189 + }
188 190
189 191 *count -= 1;
190 192 }
191 193
192 194 zap_cursor_fini(&zc);
193 195
194 196 return (0);
195 197 }
196 198
197 199 static int
198 200 process_error_list(avl_tree_t *list, void *addr, size_t *count)
199 201 {
200 202 spa_error_entry_t *se;
201 203
202 204 for (se = avl_first(list); se != NULL; se = AVL_NEXT(list, se)) {
203 205
204 206 if (*count == 0)
205 207 return (SET_ERROR(ENOMEM));
206 208
207 209 if (copyout(&se->se_bookmark, (char *)addr +
208 210 (*count - 1) * sizeof (zbookmark_t),
209 211 sizeof (zbookmark_t)) != 0)
210 212 return (SET_ERROR(EFAULT));
211 213
212 214 *count -= 1;
213 215 }
214 216
215 217 return (0);
216 218 }
217 219 #endif
218 220
219 221 /*
220 222 * Copy all known errors to userland as an array of bookmarks. This is
221 223 * actually a union of the on-disk last log and current log, as well as any
222 224 * pending error requests.
223 225 *
224 226 * Because the act of reading the on-disk log could cause errors to be
225 227 * generated, we have two separate locks: one for the error log and one for the
226 228 * in-core error lists. We only need the error list lock to log and error, so
227 229 * we grab the error log lock while we read the on-disk logs, and only pick up
228 230 * the error list lock when we are finished.
229 231 */
230 232 int
231 233 spa_get_errlog(spa_t *spa, void *uaddr, size_t *count)
232 234 {
233 235 int ret = 0;
234 236
235 237 #ifdef _KERNEL
236 238 mutex_enter(&spa->spa_errlog_lock);
237 239
238 240 ret = process_error_log(spa, spa->spa_errlog_scrub, uaddr, count);
239 241
240 242 if (!ret && !spa->spa_scrub_finished)
241 243 ret = process_error_log(spa, spa->spa_errlog_last, uaddr,
242 244 count);
243 245
244 246 mutex_enter(&spa->spa_errlist_lock);
245 247 if (!ret)
246 248 ret = process_error_list(&spa->spa_errlist_scrub, uaddr,
247 249 count);
248 250 if (!ret)
249 251 ret = process_error_list(&spa->spa_errlist_last, uaddr,
250 252 count);
251 253 mutex_exit(&spa->spa_errlist_lock);
252 254
253 255 mutex_exit(&spa->spa_errlog_lock);
254 256 #endif
255 257
256 258 return (ret);
257 259 }
258 260
259 261 /*
260 262 * Called when a scrub completes. This simply set a bit which tells which AVL
261 263 * tree to add new errors. spa_errlog_sync() is responsible for actually
262 264 * syncing the changes to the underlying objects.
263 265 */
264 266 void
265 267 spa_errlog_rotate(spa_t *spa)
266 268 {
267 269 mutex_enter(&spa->spa_errlist_lock);
268 270 spa->spa_scrub_finished = B_TRUE;
269 271 mutex_exit(&spa->spa_errlist_lock);
270 272 }
271 273
272 274 /*
273 275 * Discard any pending errors from the spa_t. Called when unloading a faulted
274 276 * pool, as the errors encountered during the open cannot be synced to disk.
275 277 */
276 278 void
277 279 spa_errlog_drain(spa_t *spa)
278 280 {
279 281 spa_error_entry_t *se;
280 282 void *cookie;
281 283
282 284 mutex_enter(&spa->spa_errlist_lock);
283 285
284 286 cookie = NULL;
285 287 while ((se = avl_destroy_nodes(&spa->spa_errlist_last,
286 288 &cookie)) != NULL)
287 289 kmem_free(se, sizeof (spa_error_entry_t));
288 290 cookie = NULL;
289 291 while ((se = avl_destroy_nodes(&spa->spa_errlist_scrub,
290 292 &cookie)) != NULL)
291 293 kmem_free(se, sizeof (spa_error_entry_t));
292 294
293 295 mutex_exit(&spa->spa_errlist_lock);
294 296 }
295 297
296 298 /*
297 299 * Process a list of errors into the current on-disk log.
298 300 */
299 301 static void
300 302 sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj, dmu_tx_t *tx)
301 303 {
302 304 spa_error_entry_t *se;
303 305 char buf[64];
304 306 void *cookie;
305 307
306 308 if (avl_numnodes(t) != 0) {
307 309 /* create log if necessary */
308 310 if (*obj == 0)
309 311 *obj = zap_create(spa->spa_meta_objset,
310 312 DMU_OT_ERROR_LOG, DMU_OT_NONE,
311 313 0, tx);
312 314
313 315 /* add errors to the current log */
314 316 for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) {
315 317 char *name = se->se_name ? se->se_name : "";
316 318
317 319 bookmark_to_name(&se->se_bookmark, buf, sizeof (buf));
318 320
319 321 (void) zap_update(spa->spa_meta_objset,
320 322 *obj, buf, 1, strlen(name) + 1, name, tx);
321 323 }
322 324
323 325 /* purge the error list */
324 326 cookie = NULL;
325 327 while ((se = avl_destroy_nodes(t, &cookie)) != NULL)
326 328 kmem_free(se, sizeof (spa_error_entry_t));
327 329 }
328 330 }
329 331
330 332 /*
331 333 * Sync the error log out to disk. This is a little tricky because the act of
332 334 * writing the error log requires the spa_errlist_lock. So, we need to lock the
333 335 * error lists, take a copy of the lists, and then reinitialize them. Then, we
334 336 * drop the error list lock and take the error log lock, at which point we
335 337 * do the errlog processing. Then, if we encounter an I/O error during this
336 338 * process, we can successfully add the error to the list. Note that this will
337 339 * result in the perpetual recycling of errors, but it is an unlikely situation
338 340 * and not a performance critical operation.
339 341 */
340 342 void
341 343 spa_errlog_sync(spa_t *spa, uint64_t txg)
342 344 {
343 345 dmu_tx_t *tx;
344 346 avl_tree_t scrub, last;
345 347 int scrub_finished;
346 348
347 349 mutex_enter(&spa->spa_errlist_lock);
348 350
349 351 /*
350 352 * Bail out early under normal circumstances.
351 353 */
352 354 if (avl_numnodes(&spa->spa_errlist_scrub) == 0 &&
353 355 avl_numnodes(&spa->spa_errlist_last) == 0 &&
354 356 !spa->spa_scrub_finished) {
355 357 mutex_exit(&spa->spa_errlist_lock);
356 358 return;
357 359 }
358 360
359 361 spa_get_errlists(spa, &last, &scrub);
360 362 scrub_finished = spa->spa_scrub_finished;
361 363 spa->spa_scrub_finished = B_FALSE;
362 364
363 365 mutex_exit(&spa->spa_errlist_lock);
364 366 mutex_enter(&spa->spa_errlog_lock);
365 367
366 368 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
367 369
368 370 /*
369 371 * Sync out the current list of errors.
370 372 */
371 373 sync_error_list(spa, &last, &spa->spa_errlog_last, tx);
372 374
373 375 /*
374 376 * Rotate the log if necessary.
375 377 */
376 378 if (scrub_finished) {
377 379 if (spa->spa_errlog_last != 0)
378 380 VERIFY(dmu_object_free(spa->spa_meta_objset,
379 381 spa->spa_errlog_last, tx) == 0);
380 382 spa->spa_errlog_last = spa->spa_errlog_scrub;
381 383 spa->spa_errlog_scrub = 0;
382 384
383 385 sync_error_list(spa, &scrub, &spa->spa_errlog_last, tx);
384 386 }
385 387
386 388 /*
387 389 * Sync out any pending scrub errors.
388 390 */
389 391 sync_error_list(spa, &scrub, &spa->spa_errlog_scrub, tx);
390 392
391 393 /*
392 394 * Update the MOS to reflect the new values.
393 395 */
394 396 (void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
395 397 DMU_POOL_ERRLOG_LAST, sizeof (uint64_t), 1,
396 398 &spa->spa_errlog_last, tx);
397 399 (void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
398 400 DMU_POOL_ERRLOG_SCRUB, sizeof (uint64_t), 1,
399 401 &spa->spa_errlog_scrub, tx);
400 402
401 403 dmu_tx_commit(tx);
402 404
403 405 mutex_exit(&spa->spa_errlog_lock);
404 406 }
↓ open down ↓ |
207 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX