Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 : * All Rights Reserved.
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
8 : #include "xfs_shared.h"
9 : #include "xfs_format.h"
10 : #include "xfs_log_format.h"
11 : #include "xfs_trans_resv.h"
12 : #include "xfs_bit.h"
13 : #include "xfs_sb.h"
14 : #include "xfs_mount.h"
15 : #include "xfs_defer.h"
16 : #include "xfs_inode.h"
17 : #include "xfs_trans.h"
18 : #include "xfs_log.h"
19 : #include "xfs_log_priv.h"
20 : #include "xfs_log_recover.h"
21 : #include "xfs_trans_priv.h"
22 : #include "xfs_alloc.h"
23 : #include "xfs_ialloc.h"
24 : #include "xfs_trace.h"
25 : #include "xfs_icache.h"
26 : #include "xfs_error.h"
27 : #include "xfs_buf_item.h"
28 : #include "xfs_ag.h"
29 : #include "xfs_quota.h"
30 : #include "xfs_reflink.h"
31 : #include "xfs_rtgroup.h"
32 :
33 : #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
34 :
35 : STATIC int
36 : xlog_find_zeroed(
37 : struct xlog *,
38 : xfs_daddr_t *);
39 : STATIC int
40 : xlog_clear_stale_blocks(
41 : struct xlog *,
42 : xfs_lsn_t);
43 : STATIC int
44 : xlog_do_recovery_pass(
45 : struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
46 :
47 : /*
48 : * Sector aligned buffer routines for buffer create/read/write/access
49 : */
50 :
51 : /*
52 : * Verify the log-relative block number and length in basic blocks are valid for
53 : * an operation involving the given XFS log buffer. Returns true if the fields
54 : * are valid, false otherwise.
55 : */
56 : static inline bool
57 : xlog_verify_bno(
58 : struct xlog *log,
59 : xfs_daddr_t blk_no,
60 : int bbcount)
61 : {
62 17737418 : if (blk_no < 0 || blk_no >= log->l_logBBsize)
63 : return false;
64 18233949 : if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
65 0 : return false;
66 : return true;
67 : }
68 :
69 : /*
70 : * Allocate a buffer to hold log data. The buffer needs to be able to map to
71 : * a range of nbblks basic blocks at any valid offset within the log.
72 : */
73 : static char *
74 496531 : xlog_alloc_buffer(
75 : struct xlog *log,
76 : int nbblks)
77 : {
78 : /*
79 : * Pass log block 0 since we don't have an addr yet, buffer will be
80 : * verified on read.
81 : */
82 993062 : if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, 0, nbblks))) {
83 0 : xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
84 : nbblks);
85 0 : return NULL;
86 : }
87 :
88 : /*
89 : * We do log I/O in units of log sectors (a power-of-2 multiple of the
90 : * basic block size), so we round up the requested size to accommodate
91 : * the basic blocks required for complete log sectors.
92 : *
93 : * In addition, the buffer may be used for a non-sector-aligned block
94 : * offset, in which case an I/O of the requested size could extend
95 : * beyond the end of the buffer. If the requested size is only 1 basic
96 : * block it will never straddle a sector boundary, so this won't be an
97 : * issue. Nor will this be a problem if the log I/O is done in basic
98 : * blocks (sector size 1). But otherwise we extend the buffer by one
99 : * extra log sector to ensure there's space to accommodate this
100 : * possibility.
101 : */
102 496531 : if (nbblks > 1 && log->l_sectBBsize > 1)
103 169550 : nbblks += log->l_sectBBsize;
104 496531 : nbblks = round_up(nbblks, log->l_sectBBsize);
105 496531 : return kvzalloc(BBTOB(nbblks), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
106 : }
107 :
108 : /*
109 : * Return the address of the start of the given block number's data
110 : * in a log buffer. The buffer covers a log sector-aligned region.
111 : */
112 : static inline unsigned int
113 : xlog_align(
114 : struct xlog *log,
115 : xfs_daddr_t blk_no)
116 : {
117 17735784 : return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
118 : }
119 :
120 : static int
121 17737418 : xlog_do_io(
122 : struct xlog *log,
123 : xfs_daddr_t blk_no,
124 : unsigned int nbblks,
125 : char *data,
126 : enum req_op op)
127 : {
128 17737418 : int error;
129 :
130 35474836 : if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, blk_no, nbblks))) {
131 0 : xfs_warn(log->l_mp,
132 : "Invalid log block/length (0x%llx, 0x%x) for buffer",
133 : blk_no, nbblks);
134 0 : return -EFSCORRUPTED;
135 : }
136 :
137 17737418 : blk_no = round_down(blk_no, log->l_sectBBsize);
138 17737418 : nbblks = round_up(nbblks, log->l_sectBBsize);
139 17737418 : ASSERT(nbblks > 0);
140 :
141 17737418 : error = xfs_rw_bdev(xfs_buftarg_bdev(log->l_targ),
142 17737418 : log->l_logBBstart + blk_no,
143 : BBTOB(nbblks), data, op);
144 17737418 : if (error && !xlog_is_shutdown(log)) {
145 0 : xfs_alert(log->l_mp,
146 : "log recovery %s I/O error at daddr 0x%llx len %d error %d",
147 : op == REQ_OP_WRITE ? "write" : "read",
148 : blk_no, nbblks, error);
149 : }
150 : return error;
151 : }
152 :
153 : STATIC int
154 1634 : xlog_bread_noalign(
155 : struct xlog *log,
156 : xfs_daddr_t blk_no,
157 : int nbblks,
158 : char *data)
159 : {
160 1634 : return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
161 : }
162 :
163 : STATIC int
164 17437396 : xlog_bread(
165 : struct xlog *log,
166 : xfs_daddr_t blk_no,
167 : int nbblks,
168 : char *data,
169 : char **offset)
170 : {
171 17437396 : int error;
172 :
173 17437396 : error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
174 17437396 : if (!error)
175 17437396 : *offset = data + xlog_align(log, blk_no);
176 17437396 : return error;
177 : }
178 :
179 : STATIC int
180 298388 : xlog_bwrite(
181 : struct xlog *log,
182 : xfs_daddr_t blk_no,
183 : int nbblks,
184 : char *data)
185 : {
186 298388 : return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_WRITE);
187 : }
188 :
189 : #ifdef DEBUG
190 : /*
191 : * dump debug superblock and log record information
192 : */
193 : STATIC void
194 0 : xlog_header_check_dump(
195 : xfs_mount_t *mp,
196 : xlog_rec_header_t *head)
197 : {
198 0 : xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
199 : __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
200 0 : xfs_debug(mp, " log : uuid = %pU, fmt = %d",
201 : &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
202 0 : }
203 : #else
204 : #define xlog_header_check_dump(mp, head)
205 : #endif
206 :
207 : /*
208 : * check log record header for recovery
209 : */
210 : STATIC int
211 3150644 : xlog_header_check_recover(
212 : xfs_mount_t *mp,
213 : xlog_rec_header_t *head)
214 : {
215 3150644 : ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
216 :
217 : /*
218 : * IRIX doesn't write the h_fmt field and leaves it zeroed
219 : * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
220 : * a dirty log created in IRIX.
221 : */
222 3150644 : if (XFS_IS_CORRUPT(mp, head->h_fmt != cpu_to_be32(XLOG_FMT))) {
223 0 : xfs_warn(mp,
224 : "dirty log written in incompatible format - can't recover");
225 0 : xlog_header_check_dump(mp, head);
226 0 : return -EFSCORRUPTED;
227 : }
228 3150644 : if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
229 : &head->h_fs_uuid))) {
230 0 : xfs_warn(mp,
231 : "dirty log entry has mismatched uuid - can't recover");
232 0 : xlog_header_check_dump(mp, head);
233 0 : return -EFSCORRUPTED;
234 : }
235 : return 0;
236 : }
237 :
238 : /*
239 : * read the head block of the log and check the header
240 : */
241 : STATIC int
242 66782 : xlog_header_check_mount(
243 : xfs_mount_t *mp,
244 : xlog_rec_header_t *head)
245 : {
246 66782 : ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
247 :
248 66782 : if (uuid_is_null(&head->h_fs_uuid)) {
249 : /*
250 : * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
251 : * h_fs_uuid is null, we assume this log was last mounted
252 : * by IRIX and continue.
253 : */
254 0 : xfs_warn(mp, "null uuid in log - IRIX style log");
255 66782 : } else if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
256 : &head->h_fs_uuid))) {
257 0 : xfs_warn(mp, "log has mismatched uuid - can't recover");
258 0 : xlog_header_check_dump(mp, head);
259 0 : return -EFSCORRUPTED;
260 : }
261 : return 0;
262 : }
263 :
264 : /*
265 : * This routine finds (to an approximation) the first block in the physical
266 : * log which contains the given cycle. It uses a binary search algorithm.
267 : * Note that the algorithm can not be perfect because the disk will not
268 : * necessarily be perfect.
269 : */
270 : STATIC int
271 66782 : xlog_find_cycle_start(
272 : struct xlog *log,
273 : char *buffer,
274 : xfs_daddr_t first_blk,
275 : xfs_daddr_t *last_blk,
276 : uint cycle)
277 : {
278 66782 : char *offset;
279 66782 : xfs_daddr_t mid_blk;
280 66782 : xfs_daddr_t end_blk;
281 66782 : uint mid_cycle;
282 66782 : int error;
283 :
284 66782 : end_blk = *last_blk;
285 66782 : mid_blk = BLK_AVG(first_blk, end_blk);
286 1220119 : while (mid_blk != first_blk && mid_blk != end_blk) {
287 1153337 : error = xlog_bread(log, mid_blk, 1, buffer, &offset);
288 1153337 : if (error)
289 0 : return error;
290 1153337 : mid_cycle = xlog_get_cycle(offset);
291 1153337 : if (mid_cycle == cycle)
292 : end_blk = mid_blk; /* last_half_cycle == mid_cycle */
293 : else
294 307550 : first_blk = mid_blk; /* first_half_cycle == mid_cycle */
295 1153337 : mid_blk = BLK_AVG(first_blk, end_blk);
296 : }
297 66782 : ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
298 : (mid_blk == end_blk && mid_blk-1 == first_blk));
299 :
300 66782 : *last_blk = end_blk;
301 :
302 66782 : return 0;
303 : }
304 :
305 : /*
306 : * Check that a range of blocks does not contain stop_on_cycle_no.
307 : * Fill in *new_blk with the block offset where such a block is
308 : * found, or with -1 (an invalid block number) if there is no such
309 : * block in the range. The scan needs to occur from front to back
310 : * and the pointer into the region must be updated since a later
311 : * routine will need to perform another test.
312 : */
313 : STATIC int
314 67653 : xlog_find_verify_cycle(
315 : struct xlog *log,
316 : xfs_daddr_t start_blk,
317 : int nbblks,
318 : uint stop_on_cycle_no,
319 : xfs_daddr_t *new_blk)
320 : {
321 67653 : xfs_daddr_t i, j;
322 67653 : uint cycle;
323 67653 : char *buffer;
324 67653 : xfs_daddr_t bufblks;
325 67653 : char *buf = NULL;
326 67653 : int error = 0;
327 :
328 : /*
329 : * Greedily allocate a buffer big enough to handle the full
330 : * range of basic blocks we'll be examining. If that fails,
331 : * try a smaller size. We need to be able to read at least
332 : * a log sector, or we're out of luck.
333 : */
334 135306 : bufblks = 1 << ffs(nbblks);
335 67653 : while (bufblks > log->l_logBBsize)
336 0 : bufblks >>= 1;
337 67653 : while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
338 0 : bufblks >>= 1;
339 0 : if (bufblks < log->l_sectBBsize)
340 : return -ENOMEM;
341 : }
342 :
343 1786901 : for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
344 1719259 : int bcount;
345 :
346 1719259 : bcount = min(bufblks, (start_blk + nbblks - i));
347 :
348 1719259 : error = xlog_bread(log, i, bcount, buffer, &buf);
349 1719259 : if (error)
350 0 : goto out;
351 :
352 132296135 : for (j = 0; j < bcount; j++) {
353 130576887 : cycle = xlog_get_cycle(buf);
354 130576887 : if (cycle == stop_on_cycle_no) {
355 11 : *new_blk = i+j;
356 11 : goto out;
357 : }
358 :
359 130576876 : buf += BBSIZE;
360 : }
361 : }
362 :
363 67642 : *new_blk = -1;
364 :
365 67653 : out:
366 67653 : kmem_free(buffer);
367 67653 : return error;
368 : }
369 :
370 : static inline int
371 187966 : xlog_logrec_hblks(struct xlog *log, struct xlog_rec_header *rh)
372 : {
373 187966 : if (xfs_has_logv2(log->l_mp)) {
374 187906 : int h_size = be32_to_cpu(rh->h_size);
375 :
376 187906 : if ((be32_to_cpu(rh->h_version) & XLOG_VERSION_2) &&
377 : h_size > XLOG_HEADER_CYCLE_SIZE)
378 2457 : return DIV_ROUND_UP(h_size, XLOG_HEADER_CYCLE_SIZE);
379 : }
380 : return 1;
381 : }
382 :
383 : /*
384 : * Potentially backup over partial log record write.
385 : *
386 : * In the typical case, last_blk is the number of the block directly after
387 : * a good log record. Therefore, we subtract one to get the block number
388 : * of the last block in the given buffer. extra_bblks contains the number
389 : * of blocks we would have read on a previous read. This happens when the
390 : * last log record is split over the end of the physical log.
391 : *
392 : * extra_bblks is the number of blocks potentially verified on a previous
393 : * call to this routine.
394 : */
395 : STATIC int
396 66805 : xlog_find_verify_log_record(
397 : struct xlog *log,
398 : xfs_daddr_t start_blk,
399 : xfs_daddr_t *last_blk,
400 : int extra_bblks)
401 : {
402 66805 : xfs_daddr_t i;
403 66805 : char *buffer;
404 66805 : char *offset = NULL;
405 66805 : xlog_rec_header_t *head = NULL;
406 66805 : int error = 0;
407 66805 : int smallmem = 0;
408 66805 : int num_blks = *last_blk - start_blk;
409 66805 : int xhdrs;
410 :
411 66805 : ASSERT(start_blk != 0 || *last_blk != start_blk);
412 :
413 66805 : buffer = xlog_alloc_buffer(log, num_blks);
414 66805 : if (!buffer) {
415 0 : buffer = xlog_alloc_buffer(log, 1);
416 0 : if (!buffer)
417 : return -ENOMEM;
418 : smallmem = 1;
419 : } else {
420 66805 : error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
421 66805 : if (error)
422 0 : goto out;
423 66805 : offset += ((num_blks - 1) << BBSHIFT);
424 : }
425 :
426 900452 : for (i = (*last_blk) - 1; i >= 0; i--) {
427 900449 : if (i < start_blk) {
428 : /* valid log record not found */
429 20 : xfs_warn(log->l_mp,
430 : "Log inconsistent (didn't find previous header)");
431 20 : ASSERT(0);
432 20 : error = -EFSCORRUPTED;
433 20 : goto out;
434 : }
435 :
436 900429 : if (smallmem) {
437 0 : error = xlog_bread(log, i, 1, buffer, &offset);
438 0 : if (error)
439 0 : goto out;
440 : }
441 :
442 900429 : head = (xlog_rec_header_t *)offset;
443 :
444 900429 : if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
445 : break;
446 :
447 833647 : if (!smallmem)
448 833647 : offset -= BBSIZE;
449 : }
450 :
451 : /*
452 : * We hit the beginning of the physical log & still no header. Return
453 : * to caller. If caller can handle a return of -1, then this routine
454 : * will be called again for the end of the physical log.
455 : */
456 66785 : if (i == -1) {
457 3 : error = 1;
458 3 : goto out;
459 : }
460 :
461 : /*
462 : * We have the final block of the good log (the first block
463 : * of the log record _before_ the head. So we check the uuid.
464 : */
465 66782 : if ((error = xlog_header_check_mount(log->l_mp, head)))
466 0 : goto out;
467 :
468 : /*
469 : * We may have found a log record header before we expected one.
470 : * last_blk will be the 1st block # with a given cycle #. We may end
471 : * up reading an entire log record. In this case, we don't want to
472 : * reset last_blk. Only when last_blk points in the middle of a log
473 : * record do we update last_blk.
474 : */
475 66782 : xhdrs = xlog_logrec_hblks(log, head);
476 :
477 66782 : if (*last_blk - i + extra_bblks !=
478 66782 : BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
479 119 : *last_blk = i;
480 :
481 66663 : out:
482 66805 : kmem_free(buffer);
483 66805 : return error;
484 : }
485 :
486 : /*
487 : * Head is defined to be the point of the log where the next log write
488 : * could go. This means that incomplete LR writes at the end are
489 : * eliminated when calculating the head. We aren't guaranteed that previous
490 : * LR have complete transactions. We only know that a cycle number of
491 : * current cycle number -1 won't be present in the log if we start writing
492 : * from our current block number.
493 : *
494 : * last_blk contains the block number of the first block with a given
495 : * cycle number.
496 : *
497 : * Return: zero if normal, non-zero if error.
498 : */
499 : STATIC int
500 66802 : xlog_find_head(
501 : struct xlog *log,
502 : xfs_daddr_t *return_head_blk)
503 : {
504 66802 : char *buffer;
505 66802 : char *offset;
506 66802 : xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
507 66802 : int num_scan_bblks;
508 66802 : uint first_half_cycle, last_half_cycle;
509 66802 : uint stop_on_cycle;
510 66802 : int error, log_bbnum = log->l_logBBsize;
511 :
512 : /* Is the end of the log device zeroed? */
513 66802 : error = xlog_find_zeroed(log, &first_blk);
514 66802 : if (error < 0) {
515 0 : xfs_warn(log->l_mp, "empty log check failed");
516 0 : return error;
517 : }
518 66802 : if (error == 1) {
519 42576 : *return_head_blk = first_blk;
520 :
521 : /* Is the whole lot zeroed? */
522 42576 : if (!first_blk) {
523 : /* Linux XFS shouldn't generate totally zeroed logs -
524 : * mkfs etc write a dummy unmount record to a fresh
525 : * log so we can store the uuid in there
526 : */
527 0 : xfs_warn(log->l_mp, "totally zeroed log");
528 : }
529 :
530 42576 : return 0;
531 : }
532 :
533 24226 : first_blk = 0; /* get cycle # of 1st block */
534 24226 : buffer = xlog_alloc_buffer(log, 1);
535 24226 : if (!buffer)
536 : return -ENOMEM;
537 :
538 24226 : error = xlog_bread(log, 0, 1, buffer, &offset);
539 24226 : if (error)
540 0 : goto out_free_buffer;
541 :
542 24226 : first_half_cycle = xlog_get_cycle(offset);
543 :
544 24226 : last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
545 24226 : error = xlog_bread(log, last_blk, 1, buffer, &offset);
546 24226 : if (error)
547 0 : goto out_free_buffer;
548 :
549 24226 : last_half_cycle = xlog_get_cycle(offset);
550 24226 : ASSERT(last_half_cycle != 0);
551 :
552 : /*
553 : * If the 1st half cycle number is equal to the last half cycle number,
554 : * then the entire log is stamped with the same cycle number. In this
555 : * case, head_blk can't be set to zero (which makes sense). The below
556 : * math doesn't work out properly with head_blk equal to zero. Instead,
557 : * we set it to log_bbnum which is an invalid block number, but this
558 : * value makes the math correct. If head_blk doesn't changed through
559 : * all the tests below, *head_blk is set to zero at the very end rather
560 : * than log_bbnum. In a sense, log_bbnum and zero are the same block
561 : * in a circular file.
562 : */
563 24226 : if (first_half_cycle == last_half_cycle) {
564 : /*
565 : * In this case we believe that the entire log should have
566 : * cycle number last_half_cycle. We need to scan backwards
567 : * from the end verifying that there are no holes still
568 : * containing last_half_cycle - 1. If we find such a hole,
569 : * then the start of that hole will be the new head. The
570 : * simple case looks like
571 : * x | x ... | x - 1 | x
572 : * Another case that fits this picture would be
573 : * x | x + 1 | x ... | x
574 : * In this case the head really is somewhere at the end of the
575 : * log, as one of the latest writes at the beginning was
576 : * incomplete.
577 : * One more case is
578 : * x | x + 1 | x ... | x - 1 | x
579 : * This is really the combination of the above two cases, and
580 : * the head has to end up at the start of the x-1 hole at the
581 : * end of the log.
582 : *
583 : * In the 256k log case, we will read from the beginning to the
584 : * end of the log and search for cycle numbers equal to x-1.
585 : * We don't worry about the x+1 blocks that we encounter,
586 : * because we know that they cannot be the head since the log
587 : * started with x.
588 : */
589 20 : head_blk = log_bbnum;
590 20 : stop_on_cycle = last_half_cycle - 1;
591 : } else {
592 : /*
593 : * In this case we want to find the first block with cycle
594 : * number matching last_half_cycle. We expect the log to be
595 : * some variation on
596 : * x + 1 ... | x ... | x
597 : * The first block with cycle number x (last_half_cycle) will
598 : * be where the new head belongs. First we do a binary search
599 : * for the first occurrence of last_half_cycle. The binary
600 : * search may not be totally accurate, so then we scan back
601 : * from there looking for occurrences of last_half_cycle before
602 : * us. If that backwards scan wraps around the beginning of
603 : * the log, then we look for occurrences of last_half_cycle - 1
604 : * at the end of the log. The cases we're looking for look
605 : * like
606 : * v binary search stopped here
607 : * x + 1 ... | x | x + 1 | x ... | x
608 : * ^ but we want to locate this spot
609 : * or
610 : * <---------> less than scan distance
611 : * x + 1 ... | x ... | x - 1 | x
612 : * ^ we want to locate this spot
613 : */
614 24206 : stop_on_cycle = last_half_cycle;
615 24206 : error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
616 : last_half_cycle);
617 24206 : if (error)
618 0 : goto out_free_buffer;
619 : }
620 :
621 : /*
622 : * Now validate the answer. Scan back some number of maximum possible
623 : * blocks and make sure each one has the expected cycle number. The
624 : * maximum is determined by the total possible amount of buffering
625 : * in the in-core log. The following number can be made tighter if
626 : * we actually look at the block size of the filesystem.
627 : */
628 24226 : num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
629 24226 : if (head_blk >= num_scan_bblks) {
630 : /*
631 : * We are guaranteed that the entire check can be performed
632 : * in one buffer.
633 : */
634 23375 : start_blk = head_blk - num_scan_bblks;
635 23375 : if ((error = xlog_find_verify_cycle(log,
636 : start_blk, num_scan_bblks,
637 : stop_on_cycle, &new_blk)))
638 0 : goto out_free_buffer;
639 23375 : if (new_blk != -1)
640 10 : head_blk = new_blk;
641 : } else { /* need to read 2 parts of log */
642 : /*
643 : * We are going to scan backwards in the log in two parts.
644 : * First we scan the physical end of the log. In this part
645 : * of the log, we are looking for blocks with cycle number
646 : * last_half_cycle - 1.
647 : * If we find one, then we know that the log starts there, as
648 : * we've found a hole that didn't get written in going around
649 : * the end of the physical log. The simple case for this is
650 : * x + 1 ... | x ... | x - 1 | x
651 : * <---------> less than scan distance
652 : * If all of the blocks at the end of the log have cycle number
653 : * last_half_cycle, then we check the blocks at the start of
654 : * the log looking for occurrences of last_half_cycle. If we
655 : * find one, then our current estimate for the location of the
656 : * first occurrence of last_half_cycle is wrong and we move
657 : * back to the hole we've found. This case looks like
658 : * x + 1 ... | x | x + 1 | x ...
659 : * ^ binary search stopped here
660 : * Another case we need to handle that only occurs in 256k
661 : * logs is
662 : * x + 1 ... | x ... | x+1 | x ...
663 : * ^ binary search stops here
664 : * In a 256k log, the scan at the end of the log will see the
665 : * x + 1 blocks. We need to skip past those since that is
666 : * certainly not the head of the log. By searching for
667 : * last_half_cycle-1 we accomplish that.
668 : */
669 851 : ASSERT(head_blk <= INT_MAX &&
670 : (xfs_daddr_t) num_scan_bblks >= head_blk);
671 851 : start_blk = log_bbnum - (num_scan_bblks - head_blk);
672 851 : if ((error = xlog_find_verify_cycle(log, start_blk,
673 851 : num_scan_bblks - (int)head_blk,
674 : (stop_on_cycle - 1), &new_blk)))
675 0 : goto out_free_buffer;
676 851 : if (new_blk != -1) {
677 0 : head_blk = new_blk;
678 0 : goto validate_head;
679 : }
680 :
681 : /*
682 : * Scan beginning of log now. The last part of the physical
683 : * log is good. This scan needs to verify that it doesn't find
684 : * the last_half_cycle.
685 : */
686 851 : start_blk = 0;
687 851 : ASSERT(head_blk <= INT_MAX);
688 851 : if ((error = xlog_find_verify_cycle(log,
689 : start_blk, (int)head_blk,
690 : stop_on_cycle, &new_blk)))
691 0 : goto out_free_buffer;
692 851 : if (new_blk != -1)
693 0 : head_blk = new_blk;
694 : }
695 :
696 851 : validate_head:
697 : /*
698 : * Now we need to make sure head_blk is not pointing to a block in
699 : * the middle of a log record.
700 : */
701 24226 : num_scan_bblks = XLOG_REC_SHIFT(log);
702 24226 : if (head_blk >= num_scan_bblks) {
703 24065 : start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
704 :
705 : /* start ptr at last block ptr before head_blk */
706 24065 : error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
707 24065 : if (error == 1)
708 : error = -EIO;
709 24065 : if (error)
710 20 : goto out_free_buffer;
711 : } else {
712 161 : start_blk = 0;
713 161 : ASSERT(head_blk <= INT_MAX);
714 161 : error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
715 161 : if (error < 0)
716 0 : goto out_free_buffer;
717 161 : if (error == 1) {
718 : /* We hit the beginning of the log during our search */
719 3 : start_blk = log_bbnum - (num_scan_bblks - head_blk);
720 3 : new_blk = log_bbnum;
721 3 : ASSERT(start_blk <= INT_MAX &&
722 : (xfs_daddr_t) log_bbnum-start_blk >= 0);
723 3 : ASSERT(head_blk <= INT_MAX);
724 3 : error = xlog_find_verify_log_record(log, start_blk,
725 : &new_blk, (int)head_blk);
726 3 : if (error == 1)
727 : error = -EIO;
728 3 : if (error)
729 0 : goto out_free_buffer;
730 3 : if (new_blk != log_bbnum)
731 0 : head_blk = new_blk;
732 158 : } else if (error)
733 0 : goto out_free_buffer;
734 : }
735 :
736 24206 : kmem_free(buffer);
737 24206 : if (head_blk == log_bbnum)
738 0 : *return_head_blk = 0;
739 : else
740 24206 : *return_head_blk = head_blk;
741 : /*
742 : * When returning here, we have a good block number. Bad block
743 : * means that during a previous crash, we didn't have a clean break
744 : * from cycle number N to cycle number N-1. In this case, we need
745 : * to find the first block with cycle number N-1.
746 : */
747 : return 0;
748 :
749 20 : out_free_buffer:
750 20 : kmem_free(buffer);
751 20 : if (error)
752 20 : xfs_warn(log->l_mp, "failed to find log head");
753 20 : return error;
754 : }
755 :
756 : /*
757 : * Seek backwards in the log for log record headers.
758 : *
759 : * Given a starting log block, walk backwards until we find the provided number
760 : * of records or hit the provided tail block. The return value is the number of
761 : * records encountered or a negative error code. The log block and buffer
762 : * pointer of the last record seen are returned in rblk and rhead respectively.
763 : */
764 : STATIC int
765 80430 : xlog_rseek_logrec_hdr(
766 : struct xlog *log,
767 : xfs_daddr_t head_blk,
768 : xfs_daddr_t tail_blk,
769 : int count,
770 : char *buffer,
771 : xfs_daddr_t *rblk,
772 : struct xlog_rec_header **rhead,
773 : bool *wrapped)
774 : {
775 80430 : int i;
776 80430 : int error;
777 80430 : int found = 0;
778 80430 : char *offset = NULL;
779 80430 : xfs_daddr_t end_blk;
780 :
781 80430 : *wrapped = false;
782 :
783 : /*
784 : * Walk backwards from the head block until we hit the tail or the first
785 : * block in the log.
786 : */
787 80430 : end_blk = head_blk > tail_blk ? tail_blk : 0;
788 4534852 : for (i = (int) head_blk - 1; i >= end_blk; i--) {
789 4528737 : error = xlog_bread(log, i, 1, buffer, &offset);
790 4528737 : if (error)
791 0 : goto out_error;
792 :
793 4528737 : if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
794 140828 : *rblk = i;
795 140828 : *rhead = (struct xlog_rec_header *) offset;
796 140828 : if (++found == count)
797 : break;
798 : }
799 : }
800 :
801 : /*
802 : * If we haven't hit the tail block or the log record header count,
803 : * start looking again from the end of the physical log. Note that
804 : * callers can pass head == tail if the tail is not yet known.
805 : */
806 80430 : if (tail_blk >= head_blk && found != count) {
807 65529 : for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
808 65528 : error = xlog_bread(log, i, 1, buffer, &offset);
809 65528 : if (error)
810 0 : goto out_error;
811 :
812 65528 : if (*(__be32 *)offset ==
813 : cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
814 264 : *wrapped = true;
815 264 : *rblk = i;
816 264 : *rhead = (struct xlog_rec_header *) offset;
817 264 : if (++found == count)
818 : break;
819 : }
820 : }
821 : }
822 :
823 : return found;
824 :
825 : out_error:
826 : return error;
827 : }
828 :
829 : /*
830 : * Seek forward in the log for log record headers.
831 : *
832 : * Given head and tail blocks, walk forward from the tail block until we find
833 : * the provided number of records or hit the head block. The return value is the
834 : * number of records encountered or a negative error code. The log block and
835 : * buffer pointer of the last record seen are returned in rblk and rhead
836 : * respectively.
837 : */
838 : STATIC int
839 13592 : xlog_seek_logrec_hdr(
840 : struct xlog *log,
841 : xfs_daddr_t head_blk,
842 : xfs_daddr_t tail_blk,
843 : int count,
844 : char *buffer,
845 : xfs_daddr_t *rblk,
846 : struct xlog_rec_header **rhead,
847 : bool *wrapped)
848 : {
849 13592 : int i;
850 13592 : int error;
851 13592 : int found = 0;
852 13592 : char *offset = NULL;
853 13592 : xfs_daddr_t end_blk;
854 :
855 13592 : *wrapped = false;
856 :
857 : /*
858 : * Walk forward from the tail block until we hit the head or the last
859 : * block in the log.
860 : */
861 13592 : end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
862 13592 : for (i = (int) tail_blk; i <= end_blk; i++) {
863 13592 : error = xlog_bread(log, i, 1, buffer, &offset);
864 13592 : if (error)
865 0 : goto out_error;
866 :
867 13592 : if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
868 13592 : *rblk = i;
869 13592 : *rhead = (struct xlog_rec_header *) offset;
870 13592 : if (++found == count)
871 : break;
872 : }
873 : }
874 :
875 : /*
876 : * If we haven't hit the head block or the log record header count,
877 : * start looking again from the start of the physical log.
878 : */
879 13592 : if (tail_blk > head_blk && found != count) {
880 0 : for (i = 0; i < (int) head_blk; i++) {
881 0 : error = xlog_bread(log, i, 1, buffer, &offset);
882 0 : if (error)
883 0 : goto out_error;
884 :
885 0 : if (*(__be32 *)offset ==
886 : cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
887 0 : *wrapped = true;
888 0 : *rblk = i;
889 0 : *rhead = (struct xlog_rec_header *) offset;
890 0 : if (++found == count)
891 : break;
892 : }
893 : }
894 : }
895 :
896 : return found;
897 :
898 : out_error:
899 : return error;
900 : }
901 :
902 : /*
903 : * Calculate distance from head to tail (i.e., unused space in the log).
904 : */
905 : static inline int
906 : xlog_tail_distance(
907 : struct xlog *log,
908 : xfs_daddr_t head_blk,
909 : xfs_daddr_t tail_blk)
910 : {
911 0 : if (head_blk < tail_blk)
912 0 : return tail_blk - head_blk;
913 :
914 0 : return tail_blk + (log->l_logBBsize - head_blk);
915 : }
916 :
917 : /*
918 : * Verify the log tail. This is particularly important when torn or incomplete
919 : * writes have been detected near the front of the log and the head has been
920 : * walked back accordingly.
921 : *
922 : * We also have to handle the case where the tail was pinned and the head
923 : * blocked behind the tail right before a crash. If the tail had been pushed
924 : * immediately prior to the crash and the subsequent checkpoint was only
925 : * partially written, it's possible it overwrote the last referenced tail in the
926 : * log with garbage. This is not a coherency problem because the tail must have
927 : * been pushed before it can be overwritten, but appears as log corruption to
928 : * recovery because we have no way to know the tail was updated if the
929 : * subsequent checkpoint didn't write successfully.
930 : *
931 : * Therefore, CRC check the log from tail to head. If a failure occurs and the
932 : * offending record is within max iclog bufs from the head, walk the tail
933 : * forward and retry until a valid tail is found or corruption is detected out
934 : * of the range of a possible overwrite.
935 : */
936 : STATIC int
937 13592 : xlog_verify_tail(
938 : struct xlog *log,
939 : xfs_daddr_t head_blk,
940 : xfs_daddr_t *tail_blk,
941 : int hsize)
942 : {
943 13592 : struct xlog_rec_header *thead;
944 13592 : char *buffer;
945 13592 : xfs_daddr_t first_bad;
946 13592 : int error = 0;
947 13592 : bool wrapped;
948 13592 : xfs_daddr_t tmp_tail;
949 13592 : xfs_daddr_t orig_tail = *tail_blk;
950 :
951 13592 : buffer = xlog_alloc_buffer(log, 1);
952 13592 : if (!buffer)
953 : return -ENOMEM;
954 :
955 : /*
956 : * Make sure the tail points to a record (returns positive count on
957 : * success).
958 : */
959 13592 : error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
960 : &tmp_tail, &thead, &wrapped);
961 13592 : if (error < 0)
962 0 : goto out;
963 13592 : if (*tail_blk != tmp_tail)
964 0 : *tail_blk = tmp_tail;
965 :
966 : /*
967 : * Run a CRC check from the tail to the head. We can't just check
968 : * MAX_ICLOGS records past the tail because the tail may point to stale
969 : * blocks cleared during the search for the head/tail. These blocks are
970 : * overwritten with zero-length records and thus record count is not a
971 : * reliable indicator of the iclog state before a crash.
972 : */
973 13592 : first_bad = 0;
974 13592 : error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
975 : XLOG_RECOVER_CRCPASS, &first_bad);
976 13592 : while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
977 0 : int tail_distance;
978 :
979 : /*
980 : * Is corruption within range of the head? If so, retry from
981 : * the next record. Otherwise return an error.
982 : */
983 0 : tail_distance = xlog_tail_distance(log, head_blk, first_bad);
984 0 : if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
985 : break;
986 :
987 : /* skip to the next record; returns positive count on success */
988 0 : error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2,
989 : buffer, &tmp_tail, &thead, &wrapped);
990 0 : if (error < 0)
991 0 : goto out;
992 :
993 0 : *tail_blk = tmp_tail;
994 0 : first_bad = 0;
995 0 : error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
996 : XLOG_RECOVER_CRCPASS, &first_bad);
997 : }
998 :
999 13592 : if (!error && *tail_blk != orig_tail)
1000 0 : xfs_warn(log->l_mp,
1001 : "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
1002 : orig_tail, *tail_blk);
1003 13592 : out:
1004 13592 : kmem_free(buffer);
1005 13592 : return error;
1006 : }
1007 :
1008 : /*
1009 : * Detect and trim torn writes from the head of the log.
1010 : *
1011 : * Storage without sector atomicity guarantees can result in torn writes in the
1012 : * log in the event of a crash. Our only means to detect this scenario is via
1013 : * CRC verification. While we can't always be certain that CRC verification
1014 : * failure is due to a torn write vs. an unrelated corruption, we do know that
1015 : * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
1016 : * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
1017 : * the log and treat failures in this range as torn writes as a matter of
1018 : * policy. In the event of CRC failure, the head is walked back to the last good
1019 : * record in the log and the tail is updated from that record and verified.
1020 : */
1021 : STATIC int
1022 13592 : xlog_verify_head(
1023 : struct xlog *log,
1024 : xfs_daddr_t *head_blk, /* in/out: unverified head */
1025 : xfs_daddr_t *tail_blk, /* out: tail block */
1026 : char *buffer,
1027 : xfs_daddr_t *rhead_blk, /* start blk of last record */
1028 : struct xlog_rec_header **rhead, /* ptr to last record */
1029 : bool *wrapped) /* last rec. wraps phys. log */
1030 : {
1031 13592 : struct xlog_rec_header *tmp_rhead;
1032 13592 : char *tmp_buffer;
1033 13592 : xfs_daddr_t first_bad;
1034 13592 : xfs_daddr_t tmp_rhead_blk;
1035 13592 : int found;
1036 13592 : int error;
1037 13592 : bool tmp_wrapped;
1038 :
1039 : /*
1040 : * Check the head of the log for torn writes. Search backwards from the
1041 : * head until we hit the tail or the maximum number of log record I/Os
1042 : * that could have been in flight at one time. Use a temporary buffer so
1043 : * we don't trash the rhead/buffer pointers from the caller.
1044 : */
1045 13592 : tmp_buffer = xlog_alloc_buffer(log, 1);
1046 13592 : if (!tmp_buffer)
1047 : return -ENOMEM;
1048 13592 : error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
1049 : XLOG_MAX_ICLOGS, tmp_buffer,
1050 : &tmp_rhead_blk, &tmp_rhead, &tmp_wrapped);
1051 13592 : kmem_free(tmp_buffer);
1052 13592 : if (error < 0)
1053 : return error;
1054 :
1055 : /*
1056 : * Now run a CRC verification pass over the records starting at the
1057 : * block found above to the current head. If a CRC failure occurs, the
1058 : * log block of the first bad record is saved in first_bad.
1059 : */
1060 13592 : error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
1061 : XLOG_RECOVER_CRCPASS, &first_bad);
1062 13592 : if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
1063 : /*
1064 : * We've hit a potential torn write. Reset the error and warn
1065 : * about it.
1066 : */
1067 56 : error = 0;
1068 56 : xfs_warn(log->l_mp,
1069 : "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
1070 : first_bad, *head_blk);
1071 :
1072 : /*
1073 : * Get the header block and buffer pointer for the last good
1074 : * record before the bad record.
1075 : *
1076 : * Note that xlog_find_tail() clears the blocks at the new head
1077 : * (i.e., the records with invalid CRC) if the cycle number
1078 : * matches the current cycle.
1079 : */
1080 56 : found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
1081 : buffer, rhead_blk, rhead, wrapped);
1082 56 : if (found < 0)
1083 : return found;
1084 56 : if (found == 0) /* XXX: right thing to do here? */
1085 : return -EIO;
1086 :
1087 : /*
1088 : * Reset the head block to the starting block of the first bad
1089 : * log record and set the tail block based on the last good
1090 : * record.
1091 : *
1092 : * Bail out if the updated head/tail match as this indicates
1093 : * possible corruption outside of the acceptable
1094 : * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
1095 : */
1096 56 : *head_blk = first_bad;
1097 56 : *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
1098 56 : if (*head_blk == *tail_blk) {
1099 0 : ASSERT(0);
1100 0 : return 0;
1101 : }
1102 : }
1103 13536 : if (error)
1104 : return error;
1105 :
1106 13592 : return xlog_verify_tail(log, *head_blk, tail_blk,
1107 13592 : be32_to_cpu((*rhead)->h_size));
1108 : }
1109 :
1110 : /*
1111 : * We need to make sure we handle log wrapping properly, so we can't use the
1112 : * calculated logbno directly. Make sure it wraps to the correct bno inside the
1113 : * log.
1114 : *
1115 : * The log is limited to 32 bit sizes, so we use the appropriate modulus
1116 : * operation here and cast it back to a 64 bit daddr on return.
1117 : */
1118 : static inline xfs_daddr_t
1119 : xlog_wrap_logbno(
1120 : struct xlog *log,
1121 : xfs_daddr_t bno)
1122 : {
1123 460815 : int mod;
1124 :
1125 460815 : div_s64_rem(bno, log->l_logBBsize, &mod);
1126 460815 : return mod;
1127 : }
1128 :
1129 : /*
1130 : * Check whether the head of the log points to an unmount record. In other
1131 : * words, determine whether the log is clean. If so, update the in-core state
1132 : * appropriately.
1133 : */
1134 : static int
1135 66838 : xlog_check_unmount_rec(
1136 : struct xlog *log,
1137 : xfs_daddr_t *head_blk,
1138 : xfs_daddr_t *tail_blk,
1139 : struct xlog_rec_header *rhead,
1140 : xfs_daddr_t rhead_blk,
1141 : char *buffer,
1142 : bool *clean)
1143 : {
1144 66838 : struct xlog_op_header *op_head;
1145 66838 : xfs_daddr_t umount_data_blk;
1146 66838 : xfs_daddr_t after_umount_blk;
1147 66838 : int hblks;
1148 66838 : int error;
1149 66838 : char *offset;
1150 :
1151 66838 : *clean = false;
1152 :
1153 : /*
1154 : * Look for unmount record. If we find it, then we know there was a
1155 : * clean unmount. Since 'i' could be the last block in the physical
1156 : * log, we convert to a log block before comparing to the head_blk.
1157 : *
1158 : * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
1159 : * below. We won't want to clear the unmount record if there is one, so
1160 : * we pass the lsn of the unmount record rather than the block after it.
1161 : */
1162 66838 : hblks = xlog_logrec_hblks(log, rhead);
1163 66838 : after_umount_blk = xlog_wrap_logbno(log,
1164 66838 : rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
1165 :
1166 66838 : if (*head_blk == after_umount_blk &&
1167 66838 : be32_to_cpu(rhead->h_num_logops) == 1) {
1168 53193 : umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
1169 53193 : error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
1170 53193 : if (error)
1171 : return error;
1172 :
1173 53193 : op_head = (struct xlog_op_header *)offset;
1174 53193 : if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
1175 : /*
1176 : * Set tail and last sync so that newly written log
1177 : * records will point recovery to after the current
1178 : * unmount record.
1179 : */
1180 53190 : xlog_assign_atomic_lsn(&log->l_tail_lsn,
1181 53190 : log->l_curr_cycle, after_umount_blk);
1182 53190 : xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
1183 : log->l_curr_cycle, after_umount_blk);
1184 53190 : *tail_blk = after_umount_blk;
1185 :
1186 53190 : *clean = true;
1187 : }
1188 : }
1189 :
1190 : return 0;
1191 : }
1192 :
1193 : static void
1194 66838 : xlog_set_state(
1195 : struct xlog *log,
1196 : xfs_daddr_t head_blk,
1197 : struct xlog_rec_header *rhead,
1198 : xfs_daddr_t rhead_blk,
1199 : bool bump_cycle)
1200 : {
1201 : /*
1202 : * Reset log values according to the state of the log when we
1203 : * crashed. In the case where head_blk == 0, we bump curr_cycle
1204 : * one because the next write starts a new cycle rather than
1205 : * continuing the cycle of the last good log record. At this
1206 : * point we have guaranteed that all partial log records have been
1207 : * accounted for. Therefore, we know that the last good log record
1208 : * written was complete and ended exactly on the end boundary
1209 : * of the physical log.
1210 : */
1211 66838 : log->l_prev_block = rhead_blk;
1212 66838 : log->l_curr_block = (int)head_blk;
1213 66838 : log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
1214 66838 : if (bump_cycle)
1215 122 : log->l_curr_cycle++;
1216 66838 : atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
1217 66838 : atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
1218 66838 : xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
1219 : BBTOB(log->l_curr_block));
1220 66838 : xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
1221 : BBTOB(log->l_curr_block));
1222 66838 : }
1223 :
1224 : /*
1225 : * Find the sync block number or the tail of the log.
1226 : *
1227 : * This will be the block number of the last record to have its
1228 : * associated buffers synced to disk. Every log record header has
1229 : * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
1230 : * to get a sync block number. The only concern is to figure out which
1231 : * log record header to believe.
1232 : *
1233 : * The following algorithm uses the log record header with the largest
1234 : * lsn. The entire log record does not need to be valid. We only care
1235 : * that the header is valid.
1236 : *
1237 : * We could speed up search by using current head_blk buffer, but it is not
1238 : * available.
1239 : */
1240 : STATIC int
1241 66802 : xlog_find_tail(
1242 : struct xlog *log,
1243 : xfs_daddr_t *head_blk,
1244 : xfs_daddr_t *tail_blk)
1245 : {
1246 66802 : xlog_rec_header_t *rhead;
1247 66802 : char *offset = NULL;
1248 66802 : char *buffer;
1249 66802 : int error;
1250 66802 : xfs_daddr_t rhead_blk;
1251 66802 : xfs_lsn_t tail_lsn;
1252 66802 : bool wrapped = false;
1253 66802 : bool clean = false;
1254 :
1255 : /*
1256 : * Find previous log record
1257 : */
1258 66802 : if ((error = xlog_find_head(log, head_blk)))
1259 : return error;
1260 66782 : ASSERT(*head_blk < INT_MAX);
1261 :
1262 66782 : buffer = xlog_alloc_buffer(log, 1);
1263 66782 : if (!buffer)
1264 : return -ENOMEM;
1265 66782 : if (*head_blk == 0) { /* special case */
1266 119 : error = xlog_bread(log, 0, 1, buffer, &offset);
1267 119 : if (error)
1268 0 : goto done;
1269 :
1270 238 : if (xlog_get_cycle(offset) == 0) {
1271 0 : *tail_blk = 0;
1272 : /* leave all other log inited values alone */
1273 0 : goto done;
1274 : }
1275 : }
1276 :
1277 : /*
1278 : * Search backwards through the log looking for the log record header
1279 : * block. This wraps all the way back around to the head so something is
1280 : * seriously wrong if we can't find it.
1281 : */
1282 66782 : error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
1283 : &rhead_blk, &rhead, &wrapped);
1284 66782 : if (error < 0)
1285 0 : goto done;
1286 66782 : if (!error) {
1287 0 : xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
1288 0 : error = -EFSCORRUPTED;
1289 0 : goto done;
1290 : }
1291 66782 : *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
1292 :
1293 : /*
1294 : * Set the log state based on the current head record.
1295 : */
1296 66782 : xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
1297 66782 : tail_lsn = atomic64_read(&log->l_tail_lsn);
1298 :
1299 : /*
1300 : * Look for an unmount record at the head of the log. This sets the log
1301 : * state to determine whether recovery is necessary.
1302 : */
1303 66782 : error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
1304 : rhead_blk, buffer, &clean);
1305 66782 : if (error)
1306 0 : goto done;
1307 :
1308 : /*
1309 : * Verify the log head if the log is not clean (e.g., we have anything
1310 : * but an unmount record at the head). This uses CRC verification to
1311 : * detect and trim torn writes. If discovered, CRC failures are
1312 : * considered torn writes and the log head is trimmed accordingly.
1313 : *
1314 : * Note that we can only run CRC verification when the log is dirty
1315 : * because there's no guarantee that the log data behind an unmount
1316 : * record is compatible with the current architecture.
1317 : */
1318 66782 : if (!clean) {
1319 13592 : xfs_daddr_t orig_head = *head_blk;
1320 :
1321 13592 : error = xlog_verify_head(log, head_blk, tail_blk, buffer,
1322 : &rhead_blk, &rhead, &wrapped);
1323 13592 : if (error)
1324 0 : goto done;
1325 :
1326 : /* update in-core state again if the head changed */
1327 13592 : if (*head_blk != orig_head) {
1328 56 : xlog_set_state(log, *head_blk, rhead, rhead_blk,
1329 : wrapped);
1330 56 : tail_lsn = atomic64_read(&log->l_tail_lsn);
1331 56 : error = xlog_check_unmount_rec(log, head_blk, tail_blk,
1332 : rhead, rhead_blk, buffer,
1333 : &clean);
1334 56 : if (error)
1335 0 : goto done;
1336 : }
1337 : }
1338 :
1339 : /*
1340 : * Note that the unmount was clean. If the unmount was not clean, we
1341 : * need to know this to rebuild the superblock counters from the perag
1342 : * headers if we have a filesystem using non-persistent counters.
1343 : */
1344 66782 : if (clean)
1345 53190 : set_bit(XFS_OPSTATE_CLEAN, &log->l_mp->m_opstate);
1346 :
1347 : /*
1348 : * Make sure that there are no blocks in front of the head
1349 : * with the same cycle number as the head. This can happen
1350 : * because we allow multiple outstanding log writes concurrently,
1351 : * and the later writes might make it out before earlier ones.
1352 : *
1353 : * We use the lsn from before modifying it so that we'll never
1354 : * overwrite the unmount record after a clean unmount.
1355 : *
1356 : * Do this only if we are going to recover the filesystem
1357 : *
1358 : * NOTE: This used to say "if (!readonly)"
1359 : * However on Linux, we can & do recover a read-only filesystem.
1360 : * We only skip recovery if NORECOVERY is specified on mount,
1361 : * in which case we would not be here.
1362 : *
1363 : * But... if the -device- itself is readonly, just skip this.
1364 : * We can't recover this device anyway, so it won't matter.
1365 : */
1366 66782 : if (!xfs_readonly_buftarg(log->l_targ))
1367 66770 : error = xlog_clear_stale_blocks(log, tail_lsn);
1368 :
1369 12 : done:
1370 66782 : kmem_free(buffer);
1371 :
1372 66782 : if (error)
1373 0 : xfs_warn(log->l_mp, "failed to locate log tail");
1374 : return error;
1375 : }
1376 :
1377 : /*
1378 : * Is the log zeroed at all?
1379 : *
1380 : * The last binary search should be changed to perform an X block read
1381 : * once X becomes small enough. You can then search linearly through
1382 : * the X blocks. This will cut down on the number of reads we need to do.
1383 : *
1384 : * If the log is partially zeroed, this routine will pass back the blkno
1385 : * of the first block with cycle number 0. It won't have a complete LR
1386 : * preceding it.
1387 : *
1388 : * Return:
1389 : * 0 => the log is completely written to
1390 : * 1 => use *blk_no as the first block of the log
1391 : * <0 => error has occurred
1392 : */
1393 : STATIC int
1394 66802 : xlog_find_zeroed(
1395 : struct xlog *log,
1396 : xfs_daddr_t *blk_no)
1397 : {
1398 66802 : char *buffer;
1399 66802 : char *offset;
1400 66802 : uint first_cycle, last_cycle;
1401 66802 : xfs_daddr_t new_blk, last_blk, start_blk;
1402 66802 : xfs_daddr_t num_scan_bblks;
1403 66802 : int error, log_bbnum = log->l_logBBsize;
1404 :
1405 66802 : *blk_no = 0;
1406 :
1407 : /* check totally zeroed log */
1408 66802 : buffer = xlog_alloc_buffer(log, 1);
1409 66802 : if (!buffer)
1410 : return -ENOMEM;
1411 66802 : error = xlog_bread(log, 0, 1, buffer, &offset);
1412 66802 : if (error)
1413 0 : goto out_free_buffer;
1414 :
1415 66802 : first_cycle = xlog_get_cycle(offset);
1416 66802 : if (first_cycle == 0) { /* completely zeroed log */
1417 0 : *blk_no = 0;
1418 0 : kmem_free(buffer);
1419 0 : return 1;
1420 : }
1421 :
1422 : /* check partially zeroed log */
1423 66802 : error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
1424 66802 : if (error)
1425 0 : goto out_free_buffer;
1426 :
1427 66802 : last_cycle = xlog_get_cycle(offset);
1428 66802 : if (last_cycle != 0) { /* log completely written to */
1429 24226 : kmem_free(buffer);
1430 24226 : return 0;
1431 : }
1432 :
1433 : /* we have a partially zeroed log */
1434 42576 : last_blk = log_bbnum-1;
1435 42576 : error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
1436 42576 : if (error)
1437 0 : goto out_free_buffer;
1438 :
1439 : /*
1440 : * Validate the answer. Because there is no way to guarantee that
1441 : * the entire log is made up of log records which are the same size,
1442 : * we scan over the defined maximum blocks. At this point, the maximum
1443 : * is not chosen to mean anything special. XXXmiken
1444 : */
1445 42576 : num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1446 42576 : ASSERT(num_scan_bblks <= INT_MAX);
1447 :
1448 42576 : if (last_blk < num_scan_bblks)
1449 : num_scan_bblks = last_blk;
1450 42576 : start_blk = last_blk - num_scan_bblks;
1451 :
1452 : /*
1453 : * We search for any instances of cycle number 0 that occur before
1454 : * our current estimate of the head. What we're trying to detect is
1455 : * 1 ... | 0 | 1 | 0...
1456 : * ^ binary search ends here
1457 : */
1458 42576 : if ((error = xlog_find_verify_cycle(log, start_blk,
1459 : (int)num_scan_bblks, 0, &new_blk)))
1460 0 : goto out_free_buffer;
1461 42576 : if (new_blk != -1)
1462 1 : last_blk = new_blk;
1463 :
1464 : /*
1465 : * Potentially backup over partial log record write. We don't need
1466 : * to search the end of the log because we know it is zero.
1467 : */
1468 42576 : error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
1469 42576 : if (error == 1)
1470 : error = -EIO;
1471 42576 : if (error)
1472 0 : goto out_free_buffer;
1473 :
1474 42576 : *blk_no = last_blk;
1475 42576 : out_free_buffer:
1476 42576 : kmem_free(buffer);
1477 42576 : if (error)
1478 0 : return error;
1479 : return 1;
1480 : }
1481 :
1482 : /*
1483 : * These are simple subroutines used by xlog_clear_stale_blocks() below
1484 : * to initialize a buffer full of empty log record headers and write
1485 : * them into the log.
1486 : */
1487 : STATIC void
1488 273380696 : xlog_add_record(
1489 : struct xlog *log,
1490 : char *buf,
1491 : int cycle,
1492 : int block,
1493 : int tail_cycle,
1494 : int tail_block)
1495 : {
1496 273380696 : xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1497 :
1498 273380696 : memset(buf, 0, BBSIZE);
1499 273380696 : recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1500 273380696 : recp->h_cycle = cpu_to_be32(cycle);
1501 273380696 : recp->h_version = cpu_to_be32(
1502 : xfs_has_logv2(log->l_mp) ? 2 : 1);
1503 273380696 : recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1504 273380696 : recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1505 273380696 : recp->h_fmt = cpu_to_be32(XLOG_FMT);
1506 546761392 : memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1507 273380696 : }
1508 :
1509 : STATIC int
1510 67451 : xlog_write_log_records(
1511 : struct xlog *log,
1512 : int cycle,
1513 : int start_block,
1514 : int blocks,
1515 : int tail_cycle,
1516 : int tail_block)
1517 : {
1518 67451 : char *offset;
1519 67451 : char *buffer;
1520 67451 : int balign, ealign;
1521 67451 : int sectbb = log->l_sectBBsize;
1522 67451 : int end_block = start_block + blocks;
1523 67451 : int bufblks;
1524 67451 : int error = 0;
1525 67451 : int i, j = 0;
1526 :
1527 : /*
1528 : * Greedily allocate a buffer big enough to handle the full
1529 : * range of basic blocks to be written. If that fails, try
1530 : * a smaller size. We need to be able to write at least a
1531 : * log sector, or we're out of luck.
1532 : */
1533 134902 : bufblks = 1 << ffs(blocks);
1534 67486 : while (bufblks > log->l_logBBsize)
1535 35 : bufblks >>= 1;
1536 67451 : while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
1537 0 : bufblks >>= 1;
1538 0 : if (bufblks < sectbb)
1539 : return -ENOMEM;
1540 : }
1541 :
1542 : /* We may need to do a read at the start to fill in part of
1543 : * the buffer in the starting sector not covered by the first
1544 : * write below.
1545 : */
1546 67451 : balign = round_down(start_block, sectbb);
1547 67451 : if (balign != start_block) {
1548 0 : error = xlog_bread_noalign(log, start_block, 1, buffer);
1549 0 : if (error)
1550 0 : goto out_free_buffer;
1551 :
1552 0 : j = start_block - balign;
1553 : }
1554 :
1555 365839 : for (i = start_block; i < end_block; i += bufblks) {
1556 298388 : int bcount, endcount;
1557 :
1558 298388 : bcount = min(bufblks, end_block - start_block);
1559 298388 : endcount = bcount - j;
1560 :
1561 : /* We may need to do a read at the end to fill in part of
1562 : * the buffer in the final sector not covered by the write.
1563 : * If this is the same sector as the above read, skip it.
1564 : */
1565 298388 : ealign = round_down(end_block, sectbb);
1566 298388 : if (j == 0 && (start_block + endcount > ealign)) {
1567 0 : error = xlog_bread_noalign(log, ealign, sectbb,
1568 0 : buffer + BBTOB(ealign - start_block));
1569 0 : if (error)
1570 : break;
1571 :
1572 : }
1573 :
1574 298388 : offset = buffer + xlog_align(log, start_block);
1575 273679084 : for (; j < endcount; j++) {
1576 273380696 : xlog_add_record(log, offset, cycle, i+j,
1577 : tail_cycle, tail_block);
1578 273380696 : offset += BBSIZE;
1579 : }
1580 298388 : error = xlog_bwrite(log, start_block, endcount, buffer);
1581 298388 : if (error)
1582 : break;
1583 298388 : start_block += endcount;
1584 298388 : j = 0;
1585 : }
1586 :
1587 67451 : out_free_buffer:
1588 67451 : kmem_free(buffer);
1589 67451 : return error;
1590 : }
1591 :
1592 : /*
1593 : * This routine is called to blow away any incomplete log writes out
1594 : * in front of the log head. We do this so that we won't become confused
1595 : * if we come up, write only a little bit more, and then crash again.
1596 : * If we leave the partial log records out there, this situation could
1597 : * cause us to think those partial writes are valid blocks since they
1598 : * have the current cycle number. We get rid of them by overwriting them
1599 : * with empty log records with the old cycle number rather than the
1600 : * current one.
1601 : *
1602 : * The tail lsn is passed in rather than taken from
1603 : * the log so that we will not write over the unmount record after a
1604 : * clean unmount in a 512 block log. Doing so would leave the log without
1605 : * any valid log records in it until a new one was written. If we crashed
1606 : * during that time we would not be able to recover.
1607 : */
1608 : STATIC int
1609 66770 : xlog_clear_stale_blocks(
1610 : struct xlog *log,
1611 : xfs_lsn_t tail_lsn)
1612 : {
1613 66770 : int tail_cycle, head_cycle;
1614 66770 : int tail_block, head_block;
1615 66770 : int tail_distance, max_distance;
1616 66770 : int distance;
1617 66770 : int error;
1618 :
1619 66770 : tail_cycle = CYCLE_LSN(tail_lsn);
1620 66770 : tail_block = BLOCK_LSN(tail_lsn);
1621 66770 : head_cycle = log->l_curr_cycle;
1622 66770 : head_block = log->l_curr_block;
1623 :
1624 : /*
1625 : * Figure out the distance between the new head of the log
1626 : * and the tail. We want to write over any blocks beyond the
1627 : * head that we may have written just before the crash, but
1628 : * we don't want to overwrite the tail of the log.
1629 : */
1630 66770 : if (head_cycle == tail_cycle) {
1631 : /*
1632 : * The tail is behind the head in the physical log,
1633 : * so the distance from the head to the tail is the
1634 : * distance from the head to the end of the log plus
1635 : * the distance from the beginning of the log to the
1636 : * tail.
1637 : */
1638 66003 : if (XFS_IS_CORRUPT(log->l_mp,
1639 : head_block < tail_block ||
1640 : head_block >= log->l_logBBsize))
1641 0 : return -EFSCORRUPTED;
1642 66003 : tail_distance = tail_block + (log->l_logBBsize - head_block);
1643 : } else {
1644 : /*
1645 : * The head is behind the tail in the physical log,
1646 : * so the distance from the head to the tail is just
1647 : * the tail block minus the head block.
1648 : */
1649 767 : if (XFS_IS_CORRUPT(log->l_mp,
1650 : head_block >= tail_block ||
1651 : head_cycle != tail_cycle + 1))
1652 0 : return -EFSCORRUPTED;
1653 767 : tail_distance = tail_block - head_block;
1654 : }
1655 :
1656 : /*
1657 : * If the head is right up against the tail, we can't clear
1658 : * anything.
1659 : */
1660 66770 : if (tail_distance <= 0) {
1661 0 : ASSERT(tail_distance == 0);
1662 0 : return 0;
1663 : }
1664 :
1665 66770 : max_distance = XLOG_TOTAL_REC_SHIFT(log);
1666 : /*
1667 : * Take the smaller of the maximum amount of outstanding I/O
1668 : * we could have and the distance to the tail to clear out.
1669 : * We take the smaller so that we don't overwrite the tail and
1670 : * we don't waste all day writing from the head to the tail
1671 : * for no reason.
1672 : */
1673 66770 : max_distance = min(max_distance, tail_distance);
1674 :
1675 66770 : if ((head_block + max_distance) <= log->l_logBBsize) {
1676 : /*
1677 : * We can stomp all the blocks we need to without
1678 : * wrapping around the end of the log. Just do it
1679 : * in a single write. Use the cycle number of the
1680 : * current cycle minus one so that the log will look like:
1681 : * n ... | n - 1 ...
1682 : */
1683 66089 : error = xlog_write_log_records(log, (head_cycle - 1),
1684 : head_block, max_distance, tail_cycle,
1685 : tail_block);
1686 66089 : if (error)
1687 0 : return error;
1688 : } else {
1689 : /*
1690 : * We need to wrap around the end of the physical log in
1691 : * order to clear all the blocks. Do it in two separate
1692 : * I/Os. The first write should be from the head to the
1693 : * end of the physical log, and it should use the current
1694 : * cycle number minus one just like above.
1695 : */
1696 681 : distance = log->l_logBBsize - head_block;
1697 681 : error = xlog_write_log_records(log, (head_cycle - 1),
1698 : head_block, distance, tail_cycle,
1699 : tail_block);
1700 :
1701 681 : if (error)
1702 : return error;
1703 :
1704 : /*
1705 : * Now write the blocks at the start of the physical log.
1706 : * This writes the remainder of the blocks we want to clear.
1707 : * It uses the current cycle number since we're now on the
1708 : * same cycle as the head so that we get:
1709 : * n ... n ... | n - 1 ...
1710 : * ^^^^^ blocks we're writing
1711 : */
1712 681 : distance = max_distance - (log->l_logBBsize - head_block);
1713 681 : error = xlog_write_log_records(log, head_cycle, 0, distance,
1714 : tail_cycle, tail_block);
1715 681 : if (error)
1716 0 : return error;
1717 : }
1718 :
1719 : return 0;
1720 : }
1721 :
1722 : /*
1723 : * Release the recovered intent item in the AIL that matches the given intent
1724 : * type and intent id.
1725 : */
1726 : void
1727 166500 : xlog_recover_release_intent(
1728 : struct xlog *log,
1729 : unsigned short intent_type,
1730 : uint64_t intent_id)
1731 : {
1732 166500 : struct xfs_ail_cursor cur;
1733 166500 : struct xfs_log_item *lip;
1734 166500 : struct xfs_ail *ailp = log->l_ailp;
1735 :
1736 166500 : spin_lock(&ailp->ail_lock);
1737 198140 : for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0); lip != NULL;
1738 31640 : lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
1739 197688 : if (lip->li_type != intent_type)
1740 24189 : continue;
1741 173499 : if (!lip->li_ops->iop_match(lip, intent_id))
1742 7451 : continue;
1743 :
1744 166048 : spin_unlock(&ailp->ail_lock);
1745 166048 : lip->li_ops->iop_release(lip);
1746 166048 : spin_lock(&ailp->ail_lock);
1747 : break;
1748 : }
1749 :
1750 166500 : xfs_trans_ail_cursor_done(&cur);
1751 166500 : spin_unlock(&ailp->ail_lock);
1752 166500 : }
1753 :
1754 : int
1755 977 : xlog_recover_iget(
1756 : struct xfs_mount *mp,
1757 : xfs_ino_t ino,
1758 : struct xfs_inode **ipp)
1759 : {
1760 977 : int error;
1761 :
1762 977 : error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
1763 977 : if (error)
1764 : return error;
1765 :
1766 977 : error = xfs_qm_dqattach(*ipp);
1767 977 : if (error) {
1768 0 : xfs_irele(*ipp);
1769 0 : return error;
1770 : }
1771 :
1772 977 : if (VFS_I(*ipp)->i_nlink == 0)
1773 88 : xfs_iflags_set(*ipp, XFS_IRECOVERY);
1774 :
1775 : return 0;
1776 : }
1777 :
1778 : /******************************************************************************
1779 : *
1780 : * Log recover routines
1781 : *
1782 : ******************************************************************************
1783 : */
1784 : static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
1785 : &xlog_buf_item_ops,
1786 : &xlog_inode_item_ops,
1787 : &xlog_dquot_item_ops,
1788 : &xlog_quotaoff_item_ops,
1789 : &xlog_icreate_item_ops,
1790 : &xlog_efi_item_ops,
1791 : &xlog_efd_item_ops,
1792 : &xlog_rui_item_ops,
1793 : &xlog_rud_item_ops,
1794 : &xlog_cui_item_ops,
1795 : &xlog_cud_item_ops,
1796 : &xlog_bui_item_ops,
1797 : &xlog_bud_item_ops,
1798 : &xlog_attri_item_ops,
1799 : &xlog_attrd_item_ops,
1800 : &xlog_sxi_item_ops,
1801 : &xlog_sxd_item_ops,
1802 : };
1803 :
1804 : static const struct xlog_recover_item_ops *
1805 65070912 : xlog_find_item_ops(
1806 : struct xlog_recover_item *item)
1807 : {
1808 65070912 : unsigned int i;
1809 :
1810 116135080 : for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
1811 116135080 : if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
1812 65070912 : return xlog_recover_item_ops[i];
1813 :
1814 : return NULL;
1815 : }
1816 :
1817 : /*
1818 : * Sort the log items in the transaction.
1819 : *
1820 : * The ordering constraints are defined by the inode allocation and unlink
1821 : * behaviour. The rules are:
1822 : *
1823 : * 1. Every item is only logged once in a given transaction. Hence it
1824 : * represents the last logged state of the item. Hence ordering is
1825 : * dependent on the order in which operations need to be performed so
1826 : * required initial conditions are always met.
1827 : *
1828 : * 2. Cancelled buffers are recorded in pass 1 in a separate table and
1829 : * there's nothing to replay from them so we can simply cull them
1830 : * from the transaction. However, we can't do that until after we've
1831 : * replayed all the other items because they may be dependent on the
1832 : * cancelled buffer and replaying the cancelled buffer can remove it
1833 : * form the cancelled buffer table. Hence they have tobe done last.
1834 : *
1835 : * 3. Inode allocation buffers must be replayed before inode items that
1836 : * read the buffer and replay changes into it. For filesystems using the
1837 : * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
1838 : * treated the same as inode allocation buffers as they create and
1839 : * initialise the buffers directly.
1840 : *
1841 : * 4. Inode unlink buffers must be replayed after inode items are replayed.
1842 : * This ensures that inodes are completely flushed to the inode buffer
1843 : * in a "free" state before we remove the unlinked inode list pointer.
1844 : *
1845 : * Hence the ordering needs to be inode allocation buffers first, inode items
1846 : * second, inode unlink buffers third and cancelled buffers last.
1847 : *
1848 : * But there's a problem with that - we can't tell an inode allocation buffer
1849 : * apart from a regular buffer, so we can't separate them. We can, however,
1850 : * tell an inode unlink buffer from the others, and so we can separate them out
1851 : * from all the other buffers and move them to last.
1852 : *
1853 : * Hence, 4 lists, in order from head to tail:
1854 : * - buffer_list for all buffers except cancelled/inode unlink buffers
1855 : * - item_list for all non-buffer items
1856 : * - inode_buffer_list for inode unlink buffers
1857 : * - cancel_list for the cancelled buffers
1858 : *
1859 : * Note that we add objects to the tail of the lists so that first-to-last
1860 : * ordering is preserved within the lists. Adding objects to the head of the
1861 : * list means when we traverse from the head we walk them in last-to-first
1862 : * order. For cancelled buffers and inode unlink buffers this doesn't matter,
1863 : * but for all other items there may be specific ordering that we need to
1864 : * preserve.
1865 : */
1866 : STATIC int
1867 926630 : xlog_recover_reorder_trans(
1868 : struct xlog *log,
1869 : struct xlog_recover *trans,
1870 : int pass)
1871 : {
1872 926630 : struct xlog_recover_item *item, *n;
1873 926630 : int error = 0;
1874 926630 : LIST_HEAD(sort_list);
1875 926630 : LIST_HEAD(cancel_list);
1876 926630 : LIST_HEAD(buffer_list);
1877 926630 : LIST_HEAD(inode_buffer_list);
1878 926630 : LIST_HEAD(item_list);
1879 :
1880 926630 : list_splice_init(&trans->r_itemq, &sort_list);
1881 65997542 : list_for_each_entry_safe(item, n, &sort_list, ri_list) {
1882 65070912 : enum xlog_recover_reorder fate = XLOG_REORDER_ITEM_LIST;
1883 :
1884 65070912 : item->ri_ops = xlog_find_item_ops(item);
1885 65070912 : if (!item->ri_ops) {
1886 0 : xfs_warn(log->l_mp,
1887 : "%s: unrecognized type of log operation (%d)",
1888 : __func__, ITEM_TYPE(item));
1889 0 : ASSERT(0);
1890 : /*
1891 : * return the remaining items back to the transaction
1892 : * item list so they can be freed in caller.
1893 : */
1894 0 : if (!list_empty(&sort_list))
1895 0 : list_splice_init(&sort_list, &trans->r_itemq);
1896 : error = -EFSCORRUPTED;
1897 : break;
1898 : }
1899 :
1900 65070912 : if (item->ri_ops->reorder)
1901 32822620 : fate = item->ri_ops->reorder(item);
1902 :
1903 32822620 : switch (fate) {
1904 31942094 : case XLOG_REORDER_BUFFER_LIST:
1905 31942094 : list_move_tail(&item->ri_list, &buffer_list);
1906 31942094 : break;
1907 808516 : case XLOG_REORDER_CANCEL_LIST:
1908 808516 : trace_xfs_log_recover_item_reorder_head(log,
1909 : trans, item, pass);
1910 808516 : list_move(&item->ri_list, &cancel_list);
1911 808516 : break;
1912 72010 : case XLOG_REORDER_INODE_BUFFER_LIST:
1913 72010 : list_move(&item->ri_list, &inode_buffer_list);
1914 72010 : break;
1915 32248292 : case XLOG_REORDER_ITEM_LIST:
1916 32248292 : trace_xfs_log_recover_item_reorder_tail(log,
1917 : trans, item, pass);
1918 32248292 : list_move_tail(&item->ri_list, &item_list);
1919 32248292 : break;
1920 : }
1921 : }
1922 :
1923 926630 : ASSERT(list_empty(&sort_list));
1924 926630 : if (!list_empty(&buffer_list))
1925 880732 : list_splice(&buffer_list, &trans->r_itemq);
1926 926630 : if (!list_empty(&item_list))
1927 921742 : list_splice_tail(&item_list, &trans->r_itemq);
1928 926630 : if (!list_empty(&inode_buffer_list))
1929 4774 : list_splice_tail(&inode_buffer_list, &trans->r_itemq);
1930 926630 : if (!list_empty(&cancel_list))
1931 183156 : list_splice_tail(&cancel_list, &trans->r_itemq);
1932 926630 : return error;
1933 : }
1934 :
1935 : void
1936 32167548 : xlog_buf_readahead(
1937 : struct xlog *log,
1938 : xfs_daddr_t blkno,
1939 : uint len,
1940 : const struct xfs_buf_ops *ops)
1941 : {
1942 32167548 : if (!xlog_is_buffer_cancelled(log, blkno, len))
1943 31027371 : xfs_buf_readahead(log->l_mp->m_ddev_targp, blkno, len, ops);
1944 32167548 : }
1945 :
1946 : STATIC int
1947 624496 : xlog_recover_items_pass2(
1948 : struct xlog *log,
1949 : struct xlog_recover *trans,
1950 : struct list_head *buffer_list,
1951 : struct list_head *item_list)
1952 : {
1953 624496 : struct xlog_recover_item *item;
1954 624496 : int error = 0;
1955 :
1956 33159952 : list_for_each_entry(item, item_list, ri_list) {
1957 32535456 : trace_xfs_log_recover_item_recover(log, trans, item,
1958 : XLOG_RECOVER_PASS2);
1959 :
1960 32535456 : if (item->ri_ops->commit_pass2)
1961 32535456 : error = item->ri_ops->commit_pass2(log, buffer_list,
1962 : item, trans->r_lsn);
1963 32535456 : if (error)
1964 0 : return error;
1965 : }
1966 :
1967 : return error;
1968 : }
1969 :
1970 : /*
1971 : * Perform the transaction.
1972 : *
1973 : * If the transaction modifies a buffer or inode, do it now. Otherwise,
1974 : * EFIs and EFDs get queued up by adding entries into the AIL for them.
1975 : */
1976 : STATIC int
1977 926630 : xlog_recover_commit_trans(
1978 : struct xlog *log,
1979 : struct xlog_recover *trans,
1980 : int pass,
1981 : struct list_head *buffer_list)
1982 : {
1983 926630 : int error = 0;
1984 926630 : int items_queued = 0;
1985 926630 : struct xlog_recover_item *item;
1986 926630 : struct xlog_recover_item *next;
1987 926630 : LIST_HEAD (ra_list);
1988 926630 : LIST_HEAD (done_list);
1989 :
1990 : #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
1991 :
1992 926630 : hlist_del_init(&trans->r_list);
1993 :
1994 926630 : error = xlog_recover_reorder_trans(log, trans, pass);
1995 926630 : if (error)
1996 : return error;
1997 :
1998 65997542 : list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
1999 65070912 : trace_xfs_log_recover_item_recover(log, trans, item, pass);
2000 :
2001 65070912 : switch (pass) {
2002 32535456 : case XLOG_RECOVER_PASS1:
2003 32535456 : if (item->ri_ops->commit_pass1)
2004 16381035 : error = item->ri_ops->commit_pass1(log, item);
2005 : break;
2006 32535456 : case XLOG_RECOVER_PASS2:
2007 32535456 : if (item->ri_ops->ra_pass2)
2008 32167548 : item->ri_ops->ra_pass2(log, item);
2009 32535456 : list_move_tail(&item->ri_list, &ra_list);
2010 32535456 : items_queued++;
2011 32535456 : if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
2012 163176 : error = xlog_recover_items_pass2(log, trans,
2013 : buffer_list, &ra_list);
2014 163176 : list_splice_tail_init(&ra_list, &done_list);
2015 : items_queued = 0;
2016 : }
2017 :
2018 : break;
2019 0 : default:
2020 0 : ASSERT(0);
2021 : }
2022 :
2023 65070912 : if (error)
2024 0 : goto out;
2025 : }
2026 :
2027 926630 : out:
2028 926630 : if (!list_empty(&ra_list)) {
2029 461320 : if (!error)
2030 461320 : error = xlog_recover_items_pass2(log, trans,
2031 : buffer_list, &ra_list);
2032 461320 : list_splice_tail_init(&ra_list, &done_list);
2033 : }
2034 :
2035 926630 : if (!list_empty(&done_list))
2036 463315 : list_splice_init(&done_list, &trans->r_itemq);
2037 :
2038 : return error;
2039 : }
2040 :
2041 : STATIC void
2042 65242928 : xlog_recover_add_item(
2043 : struct list_head *head)
2044 : {
2045 65242928 : struct xlog_recover_item *item;
2046 :
2047 65242928 : item = kmem_zalloc(sizeof(struct xlog_recover_item), 0);
2048 65242928 : INIT_LIST_HEAD(&item->ri_list);
2049 65242928 : list_add_tail(&item->ri_list, head);
2050 65242928 : }
2051 :
2052 : STATIC int
2053 2164806 : xlog_recover_add_to_cont_trans(
2054 : struct xlog *log,
2055 : struct xlog_recover *trans,
2056 : char *dp,
2057 : int len)
2058 : {
2059 2164806 : struct xlog_recover_item *item;
2060 2164806 : char *ptr, *old_ptr;
2061 2164806 : int old_len;
2062 :
2063 : /*
2064 : * If the transaction is empty, the header was split across this and the
2065 : * previous record. Copy the rest of the header.
2066 : */
2067 2164806 : if (list_empty(&trans->r_itemq)) {
2068 8 : ASSERT(len <= sizeof(struct xfs_trans_header));
2069 8 : if (len > sizeof(struct xfs_trans_header)) {
2070 0 : xfs_warn(log->l_mp, "%s: bad header length", __func__);
2071 0 : return -EFSCORRUPTED;
2072 : }
2073 :
2074 8 : xlog_recover_add_item(&trans->r_itemq);
2075 8 : ptr = (char *)&trans->r_theader +
2076 8 : sizeof(struct xfs_trans_header) - len;
2077 16 : memcpy(ptr, dp, len);
2078 8 : return 0;
2079 : }
2080 :
2081 : /* take the tail entry */
2082 2164798 : item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
2083 : ri_list);
2084 :
2085 2164798 : old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
2086 2164798 : old_len = item->ri_buf[item->ri_cnt-1].i_len;
2087 :
2088 2164798 : ptr = kvrealloc(old_ptr, old_len, len + old_len, GFP_KERNEL);
2089 2164798 : if (!ptr)
2090 : return -ENOMEM;
2091 4329596 : memcpy(&ptr[old_len], dp, len);
2092 2164798 : item->ri_buf[item->ri_cnt-1].i_len += len;
2093 2164798 : item->ri_buf[item->ri_cnt-1].i_addr = ptr;
2094 2164798 : trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
2095 2164798 : return 0;
2096 : }
2097 :
2098 : /*
2099 : * The next region to add is the start of a new region. It could be
2100 : * a whole region or it could be the first part of a new region. Because
2101 : * of this, the assumption here is that the type and size fields of all
2102 : * format structures fit into the first 32 bits of the structure.
2103 : *
2104 : * This works because all regions must be 32 bit aligned. Therefore, we
2105 : * either have both fields or we have neither field. In the case we have
2106 : * neither field, the data part of the region is zero length. We only have
2107 : * a log_op_header and can throw away the header since a new one will appear
2108 : * later. If we have at least 4 bytes, then we can determine how many regions
2109 : * will appear in the current log item.
2110 : */
2111 : STATIC int
2112 167340324 : xlog_recover_add_to_trans(
2113 : struct xlog *log,
2114 : struct xlog_recover *trans,
2115 : char *dp,
2116 : int len)
2117 : {
2118 167340324 : struct xfs_inode_log_format *in_f; /* any will do */
2119 167340324 : struct xlog_recover_item *item;
2120 167340324 : char *ptr;
2121 :
2122 167340324 : if (!len)
2123 : return 0;
2124 167340324 : if (list_empty(&trans->r_itemq)) {
2125 : /* we need to catch log corruptions here */
2126 928384 : if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
2127 0 : xfs_warn(log->l_mp, "%s: bad header magic number",
2128 : __func__);
2129 0 : ASSERT(0);
2130 0 : return -EFSCORRUPTED;
2131 : }
2132 :
2133 928384 : if (len > sizeof(struct xfs_trans_header)) {
2134 0 : xfs_warn(log->l_mp, "%s: bad header length", __func__);
2135 0 : ASSERT(0);
2136 0 : return -EFSCORRUPTED;
2137 : }
2138 :
2139 : /*
2140 : * The transaction header can be arbitrarily split across op
2141 : * records. If we don't have the whole thing here, copy what we
2142 : * do have and handle the rest in the next record.
2143 : */
2144 928384 : if (len == sizeof(struct xfs_trans_header))
2145 928376 : xlog_recover_add_item(&trans->r_itemq);
2146 1856768 : memcpy(&trans->r_theader, dp, len);
2147 928384 : return 0;
2148 : }
2149 :
2150 166411940 : ptr = kmem_alloc(len, 0);
2151 332823880 : memcpy(ptr, dp, len);
2152 166411940 : in_f = (struct xfs_inode_log_format *)ptr;
2153 :
2154 : /* take the tail entry */
2155 166411940 : item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
2156 : ri_list);
2157 166411940 : if (item->ri_total != 0 &&
2158 165483556 : item->ri_total == item->ri_cnt) {
2159 : /* tail item is in use, get a new one */
2160 64314544 : xlog_recover_add_item(&trans->r_itemq);
2161 64314544 : item = list_entry(trans->r_itemq.prev,
2162 : struct xlog_recover_item, ri_list);
2163 : }
2164 :
2165 166411940 : if (item->ri_total == 0) { /* first region to be added */
2166 65242928 : if (in_f->ilf_size == 0 ||
2167 : in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
2168 0 : xfs_warn(log->l_mp,
2169 : "bad number of regions (%d) in inode log format",
2170 : in_f->ilf_size);
2171 0 : ASSERT(0);
2172 0 : kmem_free(ptr);
2173 0 : return -EFSCORRUPTED;
2174 : }
2175 :
2176 65242928 : item->ri_total = in_f->ilf_size;
2177 65242928 : item->ri_buf =
2178 65242928 : kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
2179 : 0);
2180 : }
2181 :
2182 166411940 : if (item->ri_total <= item->ri_cnt) {
2183 0 : xfs_warn(log->l_mp,
2184 : "log item region count (%d) overflowed size (%d)",
2185 : item->ri_cnt, item->ri_total);
2186 0 : ASSERT(0);
2187 0 : kmem_free(ptr);
2188 0 : return -EFSCORRUPTED;
2189 : }
2190 :
2191 : /* Description region is ri_buf[0] */
2192 166411940 : item->ri_buf[item->ri_cnt].i_addr = ptr;
2193 166411940 : item->ri_buf[item->ri_cnt].i_len = len;
2194 166411940 : item->ri_cnt++;
2195 166411940 : trace_xfs_log_recover_item_add(log, trans, item, 0);
2196 166411940 : return 0;
2197 : }
2198 :
2199 : /*
2200 : * Free up any resources allocated by the transaction
2201 : *
2202 : * Remember that EFIs, EFDs, and IUNLINKs are handled later.
2203 : */
2204 : STATIC void
2205 928384 : xlog_recover_free_trans(
2206 : struct xlog_recover *trans)
2207 : {
2208 928384 : struct xlog_recover_item *item, *n;
2209 928384 : int i;
2210 :
2211 928384 : hlist_del_init(&trans->r_list);
2212 :
2213 66171312 : list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
2214 : /* Free the regions in the item. */
2215 65242928 : list_del(&item->ri_list);
2216 231654868 : for (i = 0; i < item->ri_cnt; i++)
2217 166411940 : kmem_free(item->ri_buf[i].i_addr);
2218 : /* Free the item itself */
2219 65242928 : kmem_free(item->ri_buf);
2220 65242928 : kmem_free(item);
2221 : }
2222 : /* Free the transaction recover structure */
2223 928384 : kmem_free(trans);
2224 928384 : }
2225 :
2226 : /*
2227 : * On error or completion, trans is freed.
2228 : */
2229 : STATIC int
2230 170431760 : xlog_recovery_process_trans(
2231 : struct xlog *log,
2232 : struct xlog_recover *trans,
2233 : char *dp,
2234 : unsigned int len,
2235 : unsigned int flags,
2236 : int pass,
2237 : struct list_head *buffer_list)
2238 : {
2239 170431760 : int error = 0;
2240 170431760 : bool freeit = false;
2241 :
2242 : /* mask off ophdr transaction container flags */
2243 170431760 : flags &= ~XLOG_END_TRANS;
2244 170431760 : if (flags & XLOG_WAS_CONT_TRANS)
2245 2164806 : flags &= ~XLOG_CONTINUE_TRANS;
2246 :
2247 : /*
2248 : * Callees must not free the trans structure. We'll decide if we need to
2249 : * free it or not based on the operation being done and it's result.
2250 : */
2251 170431760 : switch (flags) {
2252 : /* expected flag values */
2253 167340324 : case 0:
2254 : case XLOG_CONTINUE_TRANS:
2255 167340324 : error = xlog_recover_add_to_trans(log, trans, dp, len);
2256 167340324 : break;
2257 2164806 : case XLOG_WAS_CONT_TRANS:
2258 2164806 : error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
2259 2164806 : break;
2260 926630 : case XLOG_COMMIT_TRANS:
2261 926630 : error = xlog_recover_commit_trans(log, trans, pass,
2262 : buffer_list);
2263 : /* success or fail, we are now done with this transaction. */
2264 926630 : freeit = true;
2265 926630 : break;
2266 :
2267 : /* unexpected flag values */
2268 0 : case XLOG_UNMOUNT_TRANS:
2269 : /* just skip trans */
2270 0 : xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
2271 0 : freeit = true;
2272 0 : break;
2273 0 : case XLOG_START_TRANS:
2274 : default:
2275 0 : xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
2276 0 : ASSERT(0);
2277 0 : error = -EFSCORRUPTED;
2278 0 : break;
2279 : }
2280 170431760 : if (error || freeit)
2281 926630 : xlog_recover_free_trans(trans);
2282 170431760 : return error;
2283 : }
2284 :
2285 : /*
2286 : * Lookup the transaction recovery structure associated with the ID in the
2287 : * current ophdr. If the transaction doesn't exist and the start flag is set in
2288 : * the ophdr, then allocate a new transaction for future ID matches to find.
2289 : * Either way, return what we found during the lookup - an existing transaction
2290 : * or nothing.
2291 : */
2292 : STATIC struct xlog_recover *
2293 171439488 : xlog_recover_ophdr_to_trans(
2294 : struct hlist_head rhash[],
2295 : struct xlog_rec_header *rhead,
2296 : struct xlog_op_header *ohead)
2297 : {
2298 171439488 : struct xlog_recover *trans;
2299 171439488 : xlog_tid_t tid;
2300 171439488 : struct hlist_head *rhp;
2301 :
2302 171439488 : tid = be32_to_cpu(ohead->oh_tid);
2303 171439488 : rhp = &rhash[XLOG_RHASH(tid)];
2304 342881490 : hlist_for_each_entry(trans, rhp, r_list) {
2305 170434274 : if (trans->r_log_tid == tid)
2306 170431760 : return trans;
2307 : }
2308 :
2309 : /*
2310 : * skip over non-start transaction headers - we could be
2311 : * processing slack space before the next transaction starts
2312 : */
2313 1007728 : if (!(ohead->oh_flags & XLOG_START_TRANS))
2314 : return NULL;
2315 :
2316 928384 : ASSERT(be32_to_cpu(ohead->oh_len) == 0);
2317 :
2318 : /*
2319 : * This is a new transaction so allocate a new recovery container to
2320 : * hold the recovery ops that will follow.
2321 : */
2322 928384 : trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
2323 928384 : trans->r_log_tid = tid;
2324 928384 : trans->r_lsn = be64_to_cpu(rhead->h_lsn);
2325 928384 : INIT_LIST_HEAD(&trans->r_itemq);
2326 928384 : INIT_HLIST_NODE(&trans->r_list);
2327 928384 : hlist_add_head(&trans->r_list, rhp);
2328 :
2329 : /*
2330 : * Nothing more to do for this ophdr. Items to be added to this new
2331 : * transaction will be in subsequent ophdr containers.
2332 : */
2333 928384 : return NULL;
2334 : }
2335 :
2336 : STATIC int
2337 171439488 : xlog_recover_process_ophdr(
2338 : struct xlog *log,
2339 : struct hlist_head rhash[],
2340 : struct xlog_rec_header *rhead,
2341 : struct xlog_op_header *ohead,
2342 : char *dp,
2343 : char *end,
2344 : int pass,
2345 : struct list_head *buffer_list)
2346 : {
2347 171439488 : struct xlog_recover *trans;
2348 171439488 : unsigned int len;
2349 171439488 : int error;
2350 :
2351 : /* Do we understand who wrote this op? */
2352 171439488 : if (ohead->oh_clientid != XFS_TRANSACTION &&
2353 : ohead->oh_clientid != XFS_LOG) {
2354 0 : xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
2355 : __func__, ohead->oh_clientid);
2356 0 : ASSERT(0);
2357 0 : return -EFSCORRUPTED;
2358 : }
2359 :
2360 : /*
2361 : * Check the ophdr contains all the data it is supposed to contain.
2362 : */
2363 171439488 : len = be32_to_cpu(ohead->oh_len);
2364 171439488 : if (dp + len > end) {
2365 0 : xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
2366 0 : WARN_ON(1);
2367 0 : return -EFSCORRUPTED;
2368 : }
2369 :
2370 171439488 : trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
2371 171439488 : if (!trans) {
2372 : /* nothing to do, so skip over this ophdr */
2373 : return 0;
2374 : }
2375 :
2376 : /*
2377 : * The recovered buffer queue is drained only once we know that all
2378 : * recovery items for the current LSN have been processed. This is
2379 : * required because:
2380 : *
2381 : * - Buffer write submission updates the metadata LSN of the buffer.
2382 : * - Log recovery skips items with a metadata LSN >= the current LSN of
2383 : * the recovery item.
2384 : * - Separate recovery items against the same metadata buffer can share
2385 : * a current LSN. I.e., consider that the LSN of a recovery item is
2386 : * defined as the starting LSN of the first record in which its
2387 : * transaction appears, that a record can hold multiple transactions,
2388 : * and/or that a transaction can span multiple records.
2389 : *
2390 : * In other words, we are allowed to submit a buffer from log recovery
2391 : * once per current LSN. Otherwise, we may incorrectly skip recovery
2392 : * items and cause corruption.
2393 : *
2394 : * We don't know up front whether buffers are updated multiple times per
2395 : * LSN. Therefore, track the current LSN of each commit log record as it
2396 : * is processed and drain the queue when it changes. Use commit records
2397 : * because they are ordered correctly by the logging code.
2398 : */
2399 170431760 : if (log->l_recovery_lsn != trans->r_lsn &&
2400 170226923 : ohead->oh_flags & XLOG_COMMIT_TRANS) {
2401 923019 : error = xfs_buf_delwri_submit(buffer_list);
2402 923019 : if (error)
2403 : return error;
2404 923019 : log->l_recovery_lsn = trans->r_lsn;
2405 : }
2406 :
2407 170431760 : return xlog_recovery_process_trans(log, trans, dp, len,
2408 170431760 : ohead->oh_flags, pass, buffer_list);
2409 : }
2410 :
2411 : /*
2412 : * There are two valid states of the r_state field. 0 indicates that the
2413 : * transaction structure is in a normal state. We have either seen the
2414 : * start of the transaction or the last operation we added was not a partial
2415 : * operation. If the last operation we added to the transaction was a
2416 : * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
2417 : *
2418 : * NOTE: skip LRs with 0 data length.
2419 : */
2420 : STATIC int
2421 3150644 : xlog_recover_process_data(
2422 : struct xlog *log,
2423 : struct hlist_head rhash[],
2424 : struct xlog_rec_header *rhead,
2425 : char *dp,
2426 : int pass,
2427 : struct list_head *buffer_list)
2428 : {
2429 3150644 : struct xlog_op_header *ohead;
2430 3150644 : char *end;
2431 3150644 : int num_logops;
2432 3150644 : int error;
2433 :
2434 3150644 : end = dp + be32_to_cpu(rhead->h_len);
2435 3150644 : num_logops = be32_to_cpu(rhead->h_num_logops);
2436 :
2437 : /* check the log format matches our own - else we can't recover */
2438 3150644 : if (xlog_header_check_recover(log->l_mp, rhead))
2439 : return -EIO;
2440 :
2441 3150644 : trace_xfs_log_recover_record(log, rhead, pass);
2442 174590132 : while ((dp < end) && num_logops) {
2443 :
2444 171439488 : ohead = (struct xlog_op_header *)dp;
2445 171439488 : dp += sizeof(*ohead);
2446 171439488 : ASSERT(dp <= end);
2447 :
2448 : /* errors will abort recovery */
2449 171439488 : error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
2450 : dp, end, pass, buffer_list);
2451 171439488 : if (error)
2452 0 : return error;
2453 :
2454 171439488 : dp += be32_to_cpu(ohead->oh_len);
2455 171439488 : num_logops--;
2456 : }
2457 : return 0;
2458 : }
2459 :
2460 : /* Take all the collected deferred ops and finish them in order. */
2461 : static int
2462 13570 : xlog_finish_defer_ops(
2463 : struct xfs_mount *mp,
2464 : struct list_head *capture_list)
2465 : {
2466 13570 : struct xfs_defer_capture *dfc, *next;
2467 13570 : struct xfs_trans *tp;
2468 13570 : int error = 0;
2469 :
2470 14969 : list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
2471 1399 : struct xfs_trans_res resv;
2472 1399 : struct xfs_defer_resources dres;
2473 :
2474 : /*
2475 : * Create a new transaction reservation from the captured
2476 : * information. Set logcount to 1 to force the new transaction
2477 : * to regrant every roll so that we can make forward progress
2478 : * in recovery no matter how full the log might be.
2479 : */
2480 1399 : resv.tr_logres = dfc->dfc_logres;
2481 1399 : resv.tr_logcount = 1;
2482 1399 : resv.tr_logflags = XFS_TRANS_PERM_LOG_RES;
2483 :
2484 1399 : error = xfs_trans_alloc(mp, &resv, dfc->dfc_blkres,
2485 : dfc->dfc_rtxres, XFS_TRANS_RESERVE, &tp);
2486 1399 : if (error) {
2487 0 : xlog_force_shutdown(mp->m_log, SHUTDOWN_LOG_IO_ERROR);
2488 0 : return error;
2489 : }
2490 :
2491 : /*
2492 : * Transfer to this new transaction all the dfops we captured
2493 : * from recovering a single intent item.
2494 : */
2495 1399 : list_del_init(&dfc->dfc_list);
2496 1399 : xfs_defer_ops_continue(dfc, tp, &dres);
2497 1399 : error = xfs_trans_commit(tp);
2498 1399 : xfs_defer_resources_rele(&dres);
2499 1399 : if (error)
2500 0 : return error;
2501 : }
2502 :
2503 13570 : ASSERT(list_empty(capture_list));
2504 : return 0;
2505 : }
2506 :
2507 : /* Release all the captured defer ops and capture structures in this list. */
2508 : static void
2509 11 : xlog_abort_defer_ops(
2510 : struct xfs_mount *mp,
2511 : struct list_head *capture_list)
2512 : {
2513 11 : struct xfs_defer_capture *dfc;
2514 11 : struct xfs_defer_capture *next;
2515 :
2516 11 : list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
2517 0 : list_del_init(&dfc->dfc_list);
2518 0 : xfs_defer_ops_capture_free(mp, dfc);
2519 : }
2520 11 : }
2521 :
2522 : /*
2523 : * When this is called, all of the log intent items which did not have
2524 : * corresponding log done items should be in the AIL. What we do now is update
2525 : * the data structures associated with each one.
2526 : *
2527 : * Since we process the log intent items in normal transactions, they will be
2528 : * removed at some point after the commit. This prevents us from just walking
2529 : * down the list processing each one. We'll use a flag in the intent item to
2530 : * skip those that we've already processed and use the AIL iteration mechanism's
2531 : * generation count to try to speed this up at least a bit.
2532 : *
2533 : * When we start, we know that the intents are the only things in the AIL. As we
2534 : * process them, however, other items are added to the AIL. Hence we know we
2535 : * have started recovery on all the pending intents when we find an non-intent
2536 : * item in the AIL.
2537 : */
2538 : STATIC int
2539 13581 : xlog_recover_process_intents(
2540 : struct xlog *log)
2541 : {
2542 13581 : LIST_HEAD(capture_list);
2543 13581 : struct xfs_ail_cursor cur;
2544 13581 : struct xfs_log_item *lip;
2545 13581 : struct xfs_ail *ailp;
2546 13581 : int error = 0;
2547 : #if defined(DEBUG) || defined(XFS_WARN)
2548 13581 : xfs_lsn_t last_lsn;
2549 : #endif
2550 :
2551 13581 : ailp = log->l_ailp;
2552 13581 : spin_lock(&ailp->ail_lock);
2553 : #if defined(DEBUG) || defined(XFS_WARN)
2554 13581 : last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
2555 : #endif
2556 13581 : for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
2557 18655 : lip != NULL;
2558 5074 : lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
2559 5085 : const struct xfs_item_ops *ops;
2560 :
2561 5085 : if (!xlog_item_is_intent(lip))
2562 : break;
2563 :
2564 : /*
2565 : * We should never see a redo item with a LSN higher than
2566 : * the last transaction we found in the log at the start
2567 : * of recovery.
2568 : */
2569 10170 : ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
2570 :
2571 : /*
2572 : * NOTE: If your intent processing routine can create more
2573 : * deferred ops, you /must/ attach them to the capture list in
2574 : * the recover routine or else those subsequent intents will be
2575 : * replayed in the wrong order!
2576 : *
2577 : * The recovery function can free the log item, so we must not
2578 : * access lip after it returns.
2579 : */
2580 5085 : spin_unlock(&ailp->ail_lock);
2581 5085 : ops = lip->li_ops;
2582 5085 : error = ops->iop_recover(lip, &capture_list);
2583 5085 : spin_lock(&ailp->ail_lock);
2584 5085 : if (error) {
2585 11 : trace_xlog_intent_recovery_failed(log->l_mp, error,
2586 11 : ops->iop_recover);
2587 11 : break;
2588 : }
2589 : }
2590 :
2591 13581 : xfs_trans_ail_cursor_done(&cur);
2592 13581 : spin_unlock(&ailp->ail_lock);
2593 13581 : if (error)
2594 11 : goto err;
2595 :
2596 13570 : error = xlog_finish_defer_ops(log->l_mp, &capture_list);
2597 13570 : if (error)
2598 0 : goto err;
2599 :
2600 : return 0;
2601 11 : err:
2602 11 : xlog_abort_defer_ops(log->l_mp, &capture_list);
2603 11 : return error;
2604 : }
2605 :
2606 : /*
2607 : * A cancel occurs when the mount has failed and we're bailing out. Release all
2608 : * pending log intent items that we haven't started recovery on so they don't
2609 : * pin the AIL.
2610 : */
2611 : STATIC void
2612 11 : xlog_recover_cancel_intents(
2613 : struct xlog *log)
2614 : {
2615 11 : struct xfs_log_item *lip;
2616 11 : struct xfs_ail_cursor cur;
2617 11 : struct xfs_ail *ailp;
2618 :
2619 11 : ailp = log->l_ailp;
2620 11 : spin_lock(&ailp->ail_lock);
2621 11 : lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
2622 11 : while (lip != NULL) {
2623 0 : if (!xlog_item_is_intent(lip))
2624 : break;
2625 :
2626 0 : spin_unlock(&ailp->ail_lock);
2627 0 : lip->li_ops->iop_release(lip);
2628 0 : spin_lock(&ailp->ail_lock);
2629 0 : lip = xfs_trans_ail_cursor_next(ailp, &cur);
2630 : }
2631 :
2632 11 : xfs_trans_ail_cursor_done(&cur);
2633 11 : spin_unlock(&ailp->ail_lock);
2634 11 : }
2635 :
2636 : /*
2637 : * This routine performs a transaction to null out a bad inode pointer
2638 : * in an agi unlinked inode hash bucket.
2639 : */
2640 : STATIC void
2641 10 : xlog_recover_clear_agi_bucket(
2642 : struct xfs_perag *pag,
2643 : int bucket)
2644 : {
2645 10 : struct xfs_mount *mp = pag->pag_mount;
2646 10 : struct xfs_trans *tp;
2647 10 : struct xfs_agi *agi;
2648 10 : struct xfs_buf *agibp;
2649 10 : int offset;
2650 10 : int error;
2651 :
2652 10 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
2653 10 : if (error)
2654 10 : goto out_error;
2655 :
2656 0 : error = xfs_read_agi(pag, tp, &agibp);
2657 0 : if (error)
2658 0 : goto out_abort;
2659 :
2660 0 : agi = agibp->b_addr;
2661 0 : agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
2662 0 : offset = offsetof(xfs_agi_t, agi_unlinked) +
2663 : (sizeof(xfs_agino_t) * bucket);
2664 0 : xfs_trans_log_buf(tp, agibp, offset,
2665 : (offset + sizeof(xfs_agino_t) - 1));
2666 :
2667 0 : error = xfs_trans_commit(tp);
2668 0 : if (error)
2669 0 : goto out_error;
2670 : return;
2671 :
2672 : out_abort:
2673 0 : xfs_trans_cancel(tp);
2674 10 : out_error:
2675 10 : xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__,
2676 : pag->pag_agno);
2677 10 : return;
2678 : }
2679 :
2680 : static int
2681 3707904 : xlog_recover_iunlink_bucket(
2682 : struct xfs_perag *pag,
2683 : struct xfs_agi *agi,
2684 : int bucket)
2685 : {
2686 3707904 : struct xfs_mount *mp = pag->pag_mount;
2687 3707904 : struct xfs_inode *prev_ip = NULL;
2688 3707904 : struct xfs_inode *ip;
2689 3707904 : xfs_agino_t prev_agino, agino;
2690 3707904 : int error = 0;
2691 :
2692 3707904 : agino = be32_to_cpu(agi->agi_unlinked[bucket]);
2693 4602841 : while (agino != NULLAGINO) {
2694 1789874 : error = xfs_iget(mp, NULL,
2695 894937 : XFS_AGINO_TO_INO(mp, pag->pag_agno, agino),
2696 : 0, 0, &ip);
2697 894937 : if (error)
2698 : break;
2699 :
2700 894937 : ASSERT(VFS_I(ip)->i_nlink == 0);
2701 894937 : ASSERT(VFS_I(ip)->i_mode != 0);
2702 894937 : xfs_iflags_clear(ip, XFS_IRECOVERY);
2703 894937 : agino = ip->i_next_unlinked;
2704 :
2705 894937 : if (prev_ip) {
2706 885982 : ip->i_prev_unlinked = prev_agino;
2707 885982 : xfs_irele(prev_ip);
2708 :
2709 : /*
2710 : * Ensure the inode is removed from the unlinked list
2711 : * before we continue so that it won't race with
2712 : * building the in-memory list here. This could be
2713 : * serialised with the agibp lock, but that just
2714 : * serialises via lockstepping and it's much simpler
2715 : * just to flush the inodegc queue and wait for it to
2716 : * complete.
2717 : */
2718 885982 : error = xfs_inodegc_flush(mp);
2719 885982 : if (error)
2720 : break;
2721 : }
2722 :
2723 894937 : prev_agino = agino;
2724 894937 : prev_ip = ip;
2725 : }
2726 :
2727 3707904 : if (prev_ip) {
2728 8955 : int error2;
2729 :
2730 8955 : ip->i_prev_unlinked = prev_agino;
2731 8955 : xfs_irele(prev_ip);
2732 :
2733 8955 : error2 = xfs_inodegc_flush(mp);
2734 8955 : if (error2 && !error)
2735 10 : return error2;
2736 : }
2737 : return error;
2738 : }
2739 :
2740 : /*
2741 : * Recover AGI unlinked lists
2742 : *
2743 : * This is called during recovery to process any inodes which we unlinked but
2744 : * not freed when the system crashed. These inodes will be on the lists in the
2745 : * AGI blocks. What we do here is scan all the AGIs and fully truncate and free
2746 : * any inodes found on the lists. Each inode is removed from the lists when it
2747 : * has been fully truncated and is freed. The freeing of the inode and its
2748 : * removal from the list must be atomic.
2749 : *
2750 : * If everything we touch in the agi processing loop is already in memory, this
2751 : * loop can hold the cpu for a long time. It runs without lock contention,
2752 : * memory allocation contention, the need wait for IO, etc, and so will run
2753 : * until we either run out of inodes to process, run low on memory or we run out
2754 : * of log space.
2755 : *
2756 : * This behaviour is bad for latency on single CPU and non-preemptible kernels,
2757 : * and can prevent other filesystem work (such as CIL pushes) from running. This
2758 : * can lead to deadlocks if the recovery process runs out of log reservation
2759 : * space. Hence we need to yield the CPU when there is other kernel work
2760 : * scheduled on this CPU to ensure other scheduled work can run without undue
2761 : * latency.
2762 : */
2763 : static void
2764 57990 : xlog_recover_iunlink_ag(
2765 : struct xfs_perag *pag)
2766 : {
2767 57990 : struct xfs_agi *agi;
2768 57990 : struct xfs_buf *agibp;
2769 57990 : int bucket;
2770 57990 : int error;
2771 :
2772 57990 : error = xfs_read_agi(pag, NULL, &agibp);
2773 57990 : if (error) {
2774 : /*
2775 : * AGI is b0rked. Don't process it.
2776 : *
2777 : * We should probably mark the filesystem as corrupt after we've
2778 : * recovered all the ag's we can....
2779 : */
2780 54 : return;
2781 : }
2782 :
2783 : /*
2784 : * Unlock the buffer so that it can be acquired in the normal course of
2785 : * the transaction to truncate and free each inode. Because we are not
2786 : * racing with anyone else here for the AGI buffer, we don't even need
2787 : * to hold it locked to read the initial unlinked bucket entries out of
2788 : * the buffer. We keep buffer reference though, so that it stays pinned
2789 : * in memory while we need the buffer.
2790 : */
2791 57936 : agi = agibp->b_addr;
2792 57936 : xfs_buf_unlock(agibp);
2793 :
2794 3823776 : for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
2795 3707904 : error = xlog_recover_iunlink_bucket(pag, agi, bucket);
2796 3707904 : if (error) {
2797 : /*
2798 : * Bucket is unrecoverable, so only a repair scan can
2799 : * free the remaining unlinked inodes. Just empty the
2800 : * bucket and remaining inodes on it unreferenced and
2801 : * unfreeable.
2802 : */
2803 10 : xlog_recover_clear_agi_bucket(pag, bucket);
2804 : }
2805 : }
2806 :
2807 57936 : xfs_buf_rele(agibp);
2808 : }
2809 :
2810 : static void
2811 13570 : xlog_recover_process_iunlinks(
2812 : struct xlog *log)
2813 : {
2814 13570 : struct xfs_perag *pag;
2815 13570 : xfs_agnumber_t agno;
2816 :
2817 71560 : for_each_perag(log->l_mp, agno, pag)
2818 57990 : xlog_recover_iunlink_ag(pag);
2819 13570 : }
2820 :
2821 : STATIC void
2822 3150644 : xlog_unpack_data(
2823 : struct xlog_rec_header *rhead,
2824 : char *dp,
2825 : struct xlog *log)
2826 : {
2827 3150644 : int i, j, k;
2828 :
2829 171023318 : for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
2830 167872674 : i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
2831 167872674 : *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
2832 167872674 : dp += BBSIZE;
2833 : }
2834 :
2835 3150644 : if (xfs_has_logv2(log->l_mp)) {
2836 : xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
2837 4837526 : for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
2838 1686882 : j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
2839 1686882 : k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
2840 1686882 : *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
2841 1686882 : dp += BBSIZE;
2842 : }
2843 : }
2844 3150644 : }
2845 :
2846 : /*
2847 : * CRC check, unpack and process a log record.
2848 : */
2849 : STATIC int
2850 4800212 : xlog_recover_process(
2851 : struct xlog *log,
2852 : struct hlist_head rhash[],
2853 : struct xlog_rec_header *rhead,
2854 : char *dp,
2855 : int pass,
2856 : struct list_head *buffer_list)
2857 : {
2858 4800212 : __le32 old_crc = rhead->h_crc;
2859 4800212 : __le32 crc;
2860 :
2861 4800212 : crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
2862 :
2863 : /*
2864 : * Nothing else to do if this is a CRC verification pass. Just return
2865 : * if this a record with a non-zero crc. Unfortunately, mkfs always
2866 : * sets old_crc to 0 so we must consider this valid even on v5 supers.
2867 : * Otherwise, return EFSBADCRC on failure so the callers up the stack
2868 : * know precisely what failed.
2869 : */
2870 4800212 : if (pass == XLOG_RECOVER_CRCPASS) {
2871 1649568 : if (old_crc && crc != old_crc)
2872 : return -EFSBADCRC;
2873 1649512 : return 0;
2874 : }
2875 :
2876 : /*
2877 : * We're in the normal recovery path. Issue a warning if and only if the
2878 : * CRC in the header is non-zero. This is an advisory warning and the
2879 : * zero CRC check prevents warnings from being emitted when upgrading
2880 : * the kernel from one that does not add CRCs by default.
2881 : */
2882 3150644 : if (crc != old_crc) {
2883 0 : if (old_crc || xfs_has_crc(log->l_mp)) {
2884 0 : xfs_alert(log->l_mp,
2885 : "log record CRC mismatch: found 0x%x, expected 0x%x.",
2886 : le32_to_cpu(old_crc),
2887 : le32_to_cpu(crc));
2888 0 : xfs_hex_dump(dp, 32);
2889 : }
2890 :
2891 : /*
2892 : * If the filesystem is CRC enabled, this mismatch becomes a
2893 : * fatal log corruption failure.
2894 : */
2895 0 : if (xfs_has_crc(log->l_mp)) {
2896 0 : XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
2897 0 : return -EFSCORRUPTED;
2898 : }
2899 : }
2900 :
2901 3150644 : xlog_unpack_data(rhead, dp, log);
2902 :
2903 3150644 : return xlog_recover_process_data(log, rhash, rhead, dp, pass,
2904 : buffer_list);
2905 : }
2906 :
2907 : STATIC int
2908 4854558 : xlog_valid_rec_header(
2909 : struct xlog *log,
2910 : struct xlog_rec_header *rhead,
2911 : xfs_daddr_t blkno,
2912 : int bufsize)
2913 : {
2914 4854558 : int hlen;
2915 :
2916 4854558 : if (XFS_IS_CORRUPT(log->l_mp,
2917 : rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)))
2918 0 : return -EFSCORRUPTED;
2919 4854558 : if (XFS_IS_CORRUPT(log->l_mp,
2920 : (!rhead->h_version ||
2921 : (be32_to_cpu(rhead->h_version) &
2922 : (~XLOG_VERSION_OKBITS))))) {
2923 0 : xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
2924 : __func__, be32_to_cpu(rhead->h_version));
2925 0 : return -EFSCORRUPTED;
2926 : }
2927 :
2928 : /*
2929 : * LR body must have data (or it wouldn't have been written)
2930 : * and h_len must not be greater than LR buffer size.
2931 : */
2932 4854558 : hlen = be32_to_cpu(rhead->h_len);
2933 4854558 : if (XFS_IS_CORRUPT(log->l_mp, hlen <= 0 || hlen > bufsize))
2934 0 : return -EFSCORRUPTED;
2935 :
2936 4854558 : if (XFS_IS_CORRUPT(log->l_mp,
2937 : blkno > log->l_logBBsize || blkno > INT_MAX))
2938 0 : return -EFSCORRUPTED;
2939 : return 0;
2940 : }
2941 :
2942 : /*
2943 : * Read the log from tail to head and process the log records found.
2944 : * Handle the two cases where the tail and head are in the same cycle
2945 : * and where the active portion of the log wraps around the end of
2946 : * the physical log separately. The pass parameter is passed through
2947 : * to the routines called to process the data and is not looked at
2948 : * here.
2949 : */
2950 : STATIC int
2951 54346 : xlog_do_recovery_pass(
2952 : struct xlog *log,
2953 : xfs_daddr_t head_blk,
2954 : xfs_daddr_t tail_blk,
2955 : int pass,
2956 : xfs_daddr_t *first_bad) /* out: first bad log rec */
2957 : {
2958 54346 : xlog_rec_header_t *rhead;
2959 54346 : xfs_daddr_t blk_no, rblk_no;
2960 54346 : xfs_daddr_t rhead_blk;
2961 54346 : char *offset;
2962 54346 : char *hbp, *dbp;
2963 54346 : int error = 0, h_size, h_len;
2964 54346 : int error2 = 0;
2965 54346 : int bblks, split_bblks;
2966 54346 : int hblks, split_hblks, wrapped_hblks;
2967 54346 : int i;
2968 54346 : struct hlist_head rhash[XLOG_RHASH_SIZE];
2969 54346 : LIST_HEAD (buffer_list);
2970 :
2971 54346 : ASSERT(head_blk != tail_blk);
2972 : blk_no = rhead_blk = tail_blk;
2973 :
2974 923882 : for (i = 0; i < XLOG_RHASH_SIZE; i++)
2975 869536 : INIT_HLIST_HEAD(&rhash[i]);
2976 :
2977 : /*
2978 : * Read the header of the tail block and get the iclog buffer size from
2979 : * h_size. Use this to tell how many sectors make up the log header.
2980 : */
2981 54346 : if (xfs_has_logv2(log->l_mp)) {
2982 : /*
2983 : * When using variable length iclogs, read first sector of
2984 : * iclog header and extract the header size from it. Get a
2985 : * new hbp that is the correct size.
2986 : */
2987 54346 : hbp = xlog_alloc_buffer(log, 1);
2988 54346 : if (!hbp)
2989 : return -ENOMEM;
2990 :
2991 54346 : error = xlog_bread(log, tail_blk, 1, hbp, &offset);
2992 54346 : if (error)
2993 0 : goto bread_err1;
2994 :
2995 54346 : rhead = (xlog_rec_header_t *)offset;
2996 :
2997 : /*
2998 : * xfsprogs has a bug where record length is based on lsunit but
2999 : * h_size (iclog size) is hardcoded to 32k. Now that we
3000 : * unconditionally CRC verify the unmount record, this means the
3001 : * log buffer can be too small for the record and cause an
3002 : * overrun.
3003 : *
3004 : * Detect this condition here. Use lsunit for the buffer size as
3005 : * long as this looks like the mkfs case. Otherwise, return an
3006 : * error to avoid a buffer overrun.
3007 : */
3008 54346 : h_size = be32_to_cpu(rhead->h_size);
3009 54346 : h_len = be32_to_cpu(rhead->h_len);
3010 54346 : if (h_len > h_size && h_len <= log->l_mp->m_logbsize &&
3011 0 : rhead->h_num_logops == cpu_to_be32(1)) {
3012 0 : xfs_warn(log->l_mp,
3013 : "invalid iclog size (%d bytes), using lsunit (%d bytes)",
3014 : h_size, log->l_mp->m_logbsize);
3015 0 : h_size = log->l_mp->m_logbsize;
3016 : }
3017 :
3018 54346 : error = xlog_valid_rec_header(log, rhead, tail_blk, h_size);
3019 54346 : if (error)
3020 0 : goto bread_err1;
3021 :
3022 54346 : hblks = xlog_logrec_hblks(log, rhead);
3023 54346 : if (hblks != 1) {
3024 936 : kmem_free(hbp);
3025 936 : hbp = xlog_alloc_buffer(log, hblks);
3026 : }
3027 : } else {
3028 0 : ASSERT(log->l_sectBBsize == 1);
3029 0 : hblks = 1;
3030 0 : hbp = xlog_alloc_buffer(log, 1);
3031 0 : h_size = XLOG_BIG_RECORD_BSIZE;
3032 : }
3033 :
3034 54346 : if (!hbp)
3035 : return -ENOMEM;
3036 54346 : dbp = xlog_alloc_buffer(log, BTOBB(h_size));
3037 54346 : if (!dbp) {
3038 0 : kmem_free(hbp);
3039 0 : return -ENOMEM;
3040 : }
3041 :
3042 54346 : memset(rhash, 0, sizeof(rhash));
3043 54346 : if (tail_blk > head_blk) {
3044 : /*
3045 : * Perform recovery around the end of the physical log.
3046 : * When the head is not on the same cycle number as the tail,
3047 : * we can't do a sequential recovery.
3048 : */
3049 344327 : while (blk_no < log->l_logBBsize) {
3050 : /*
3051 : * Check for header wrapping around physical end-of-log
3052 : */
3053 342418 : offset = hbp;
3054 342418 : split_hblks = 0;
3055 342418 : wrapped_hblks = 0;
3056 342418 : if (blk_no + hblks <= log->l_logBBsize) {
3057 : /* Read header in one read */
3058 342418 : error = xlog_bread(log, blk_no, hblks, hbp,
3059 : &offset);
3060 342418 : if (error)
3061 0 : goto bread_err2;
3062 : } else {
3063 : /* This LR is split across physical log end */
3064 0 : if (blk_no != log->l_logBBsize) {
3065 : /* some data before physical log end */
3066 0 : ASSERT(blk_no <= INT_MAX);
3067 0 : split_hblks = log->l_logBBsize - (int)blk_no;
3068 0 : ASSERT(split_hblks > 0);
3069 0 : error = xlog_bread(log, blk_no,
3070 : split_hblks, hbp,
3071 : &offset);
3072 0 : if (error)
3073 0 : goto bread_err2;
3074 : }
3075 :
3076 : /*
3077 : * Note: this black magic still works with
3078 : * large sector sizes (non-512) only because:
3079 : * - we increased the buffer size originally
3080 : * by 1 sector giving us enough extra space
3081 : * for the second read;
3082 : * - the log start is guaranteed to be sector
3083 : * aligned;
3084 : * - we read the log end (LR header start)
3085 : * _first_, then the log start (LR header end)
3086 : * - order is important.
3087 : */
3088 0 : wrapped_hblks = hblks - split_hblks;
3089 0 : error = xlog_bread_noalign(log, 0,
3090 : wrapped_hblks,
3091 0 : offset + BBTOB(split_hblks));
3092 0 : if (error)
3093 0 : goto bread_err2;
3094 : }
3095 342418 : rhead = (xlog_rec_header_t *)offset;
3096 342418 : error = xlog_valid_rec_header(log, rhead,
3097 : split_hblks ? blk_no : 0, h_size);
3098 342418 : if (error)
3099 0 : goto bread_err2;
3100 :
3101 342418 : bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
3102 342418 : blk_no += hblks;
3103 :
3104 : /*
3105 : * Read the log record data in multiple reads if it
3106 : * wraps around the end of the log. Note that if the
3107 : * header already wrapped, blk_no could point past the
3108 : * end of the log. The record data is contiguous in
3109 : * that case.
3110 : */
3111 342418 : if (blk_no + bblks <= log->l_logBBsize ||
3112 : blk_no >= log->l_logBBsize) {
3113 340784 : rblk_no = xlog_wrap_logbno(log, blk_no);
3114 340784 : error = xlog_bread(log, rblk_no, bblks, dbp,
3115 : &offset);
3116 340784 : if (error)
3117 0 : goto bread_err2;
3118 : } else {
3119 : /* This log record is split across the
3120 : * physical end of log */
3121 1634 : offset = dbp;
3122 1634 : split_bblks = 0;
3123 1634 : if (blk_no != log->l_logBBsize) {
3124 : /* some data is before the physical
3125 : * end of log */
3126 1634 : ASSERT(!wrapped_hblks);
3127 1634 : ASSERT(blk_no <= INT_MAX);
3128 1634 : split_bblks =
3129 1634 : log->l_logBBsize - (int)blk_no;
3130 1634 : ASSERT(split_bblks > 0);
3131 1634 : error = xlog_bread(log, blk_no,
3132 : split_bblks, dbp,
3133 : &offset);
3134 1634 : if (error)
3135 0 : goto bread_err2;
3136 : }
3137 :
3138 : /*
3139 : * Note: this black magic still works with
3140 : * large sector sizes (non-512) only because:
3141 : * - we increased the buffer size originally
3142 : * by 1 sector giving us enough extra space
3143 : * for the second read;
3144 : * - the log start is guaranteed to be sector
3145 : * aligned;
3146 : * - we read the log end (LR header start)
3147 : * _first_, then the log start (LR header end)
3148 : * - order is important.
3149 : */
3150 1634 : error = xlog_bread_noalign(log, 0,
3151 : bblks - split_bblks,
3152 1634 : offset + BBTOB(split_bblks));
3153 1634 : if (error)
3154 0 : goto bread_err2;
3155 : }
3156 :
3157 342418 : error = xlog_recover_process(log, rhash, rhead, offset,
3158 : pass, &buffer_list);
3159 342418 : if (error)
3160 0 : goto bread_err2;
3161 :
3162 : blk_no += bblks;
3163 : rhead_blk = blk_no;
3164 : }
3165 :
3166 1909 : ASSERT(blk_no >= log->l_logBBsize);
3167 1909 : blk_no -= log->l_logBBsize;
3168 1909 : rhead_blk = blk_no;
3169 : }
3170 :
3171 : /* read first part of physical log */
3172 4512084 : while (blk_no < head_blk) {
3173 4457794 : error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3174 4457794 : if (error)
3175 0 : goto bread_err2;
3176 :
3177 4457794 : rhead = (xlog_rec_header_t *)offset;
3178 4457794 : error = xlog_valid_rec_header(log, rhead, blk_no, h_size);
3179 4457794 : if (error)
3180 0 : goto bread_err2;
3181 :
3182 : /* blocks in data section */
3183 4457794 : bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
3184 4457794 : error = xlog_bread(log, blk_no+hblks, bblks, dbp,
3185 : &offset);
3186 4457794 : if (error)
3187 0 : goto bread_err2;
3188 :
3189 4457794 : error = xlog_recover_process(log, rhash, rhead, offset, pass,
3190 : &buffer_list);
3191 4457794 : if (error)
3192 56 : goto bread_err2;
3193 :
3194 4457738 : blk_no += bblks + hblks;
3195 4457738 : rhead_blk = blk_no;
3196 : }
3197 :
3198 54290 : bread_err2:
3199 54346 : kmem_free(dbp);
3200 54346 : bread_err1:
3201 54346 : kmem_free(hbp);
3202 :
3203 : /*
3204 : * Submit buffers that have been added from the last record processed,
3205 : * regardless of error status.
3206 : */
3207 54346 : if (!list_empty(&buffer_list))
3208 12663 : error2 = xfs_buf_delwri_submit(&buffer_list);
3209 :
3210 54346 : if (error && first_bad)
3211 56 : *first_bad = rhead_blk;
3212 :
3213 : /*
3214 : * Transactions are freed at commit time but transactions without commit
3215 : * records on disk are never committed. Free any that may be left in the
3216 : * hash table.
3217 : */
3218 923882 : for (i = 0; i < XLOG_RHASH_SIZE; i++) {
3219 869536 : struct hlist_node *tmp;
3220 869536 : struct xlog_recover *trans;
3221 :
3222 1740826 : hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
3223 1754 : xlog_recover_free_trans(trans);
3224 : }
3225 :
3226 54346 : return error ? error : error2;
3227 : }
3228 :
3229 : /*
3230 : * Do the recovery of the log. We actually do this in two phases.
3231 : * The two passes are necessary in order to implement the function
3232 : * of cancelling a record written into the log. The first pass
3233 : * determines those things which have been cancelled, and the
3234 : * second pass replays log items normally except for those which
3235 : * have been cancelled. The handling of the replay and cancellations
3236 : * takes place in the log item type specific routines.
3237 : *
3238 : * The table of items which have cancel records in the log is allocated
3239 : * and freed at this level, since only here do we know when all of
3240 : * the log recovery has been completed.
3241 : */
3242 : STATIC int
3243 13581 : xlog_do_log_recovery(
3244 : struct xlog *log,
3245 : xfs_daddr_t head_blk,
3246 : xfs_daddr_t tail_blk)
3247 : {
3248 13581 : int error;
3249 :
3250 13581 : ASSERT(head_blk != tail_blk);
3251 :
3252 : /*
3253 : * First do a pass to find all of the cancelled buf log items.
3254 : * Store them in the buf_cancel_table for use in the second pass.
3255 : */
3256 13581 : error = xlog_alloc_buf_cancel_table(log);
3257 13581 : if (error)
3258 : return error;
3259 :
3260 13581 : error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3261 : XLOG_RECOVER_PASS1, NULL);
3262 13581 : if (error != 0)
3263 0 : goto out_cancel;
3264 :
3265 : /*
3266 : * Then do a second pass to actually recover the items in the log.
3267 : * When it is complete free the table of buf cancel items.
3268 : */
3269 13581 : error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3270 : XLOG_RECOVER_PASS2, NULL);
3271 13581 : if (!error)
3272 13581 : xlog_check_buf_cancel_table(log);
3273 0 : out_cancel:
3274 13581 : xlog_free_buf_cancel_table(log);
3275 13581 : return error;
3276 : }
3277 :
3278 : /*
3279 : * Do the actual recovery
3280 : */
3281 : STATIC int
3282 13581 : xlog_do_recover(
3283 : struct xlog *log,
3284 : xfs_daddr_t head_blk,
3285 : xfs_daddr_t tail_blk)
3286 : {
3287 13581 : struct xfs_mount *mp = log->l_mp;
3288 13581 : struct xfs_buf *bp = mp->m_sb_bp;
3289 13581 : struct xfs_sb *sbp = &mp->m_sb;
3290 13581 : int error;
3291 :
3292 13581 : trace_xfs_log_recover(log, head_blk, tail_blk);
3293 :
3294 : /*
3295 : * First replay the images in the log.
3296 : */
3297 13581 : error = xlog_do_log_recovery(log, head_blk, tail_blk);
3298 13581 : if (error)
3299 : return error;
3300 :
3301 27162 : if (xlog_is_shutdown(log))
3302 : return -EIO;
3303 :
3304 : /*
3305 : * We now update the tail_lsn since much of the recovery has completed
3306 : * and there may be space available to use. If there were no extent
3307 : * or iunlinks, we can free up the entire log and set the tail_lsn to
3308 : * be the last_sync_lsn. This was set in xlog_find_tail to be the
3309 : * lsn of the last known good LR on disk. If there are extent frees
3310 : * or iunlinks they will have some entries in the AIL; so we look at
3311 : * the AIL to determine how to set the tail_lsn.
3312 : */
3313 13581 : xlog_assign_tail_lsn(mp);
3314 :
3315 : /*
3316 : * Now that we've finished replaying all buffer and inode updates,
3317 : * re-read the superblock and reverify it.
3318 : */
3319 13581 : xfs_buf_lock(bp);
3320 13581 : xfs_buf_hold(bp);
3321 13581 : error = _xfs_buf_read(bp, XBF_READ);
3322 13581 : if (error) {
3323 0 : if (!xlog_is_shutdown(log)) {
3324 0 : xfs_buf_ioerror_alert(bp, __this_address);
3325 0 : ASSERT(0);
3326 : }
3327 0 : xfs_buf_relse(bp);
3328 0 : return error;
3329 : }
3330 :
3331 : /* Convert superblock from on-disk format */
3332 13581 : xfs_sb_from_disk(sbp, bp->b_addr);
3333 13581 : xfs_buf_relse(bp);
3334 :
3335 : /* re-initialise in-core superblock and geometry structures */
3336 13581 : mp->m_features |= xfs_sb_version_to_features(sbp);
3337 13581 : xfs_reinit_percpu_counters(mp);
3338 13581 : error = xfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
3339 : &mp->m_maxagi);
3340 13581 : if (error) {
3341 0 : xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
3342 0 : return error;
3343 : }
3344 13581 : error = xfs_initialize_rtgroups(mp, sbp->sb_rgcount);
3345 13581 : if (error) {
3346 0 : xfs_warn(mp, "Failed post-recovery rtgroup init: %d", error);
3347 0 : return error;
3348 : }
3349 13581 : mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
3350 :
3351 : /* Normal transactions can now occur */
3352 13581 : clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
3353 13581 : return 0;
3354 : }
3355 :
3356 : /*
3357 : * Perform recovery and re-initialize some log variables in xlog_find_tail.
3358 : *
3359 : * Return error or zero.
3360 : */
3361 : int
3362 66802 : xlog_recover(
3363 : struct xlog *log)
3364 : {
3365 66802 : xfs_daddr_t head_blk, tail_blk;
3366 66802 : int error;
3367 :
3368 : /* find the tail of the log */
3369 66802 : error = xlog_find_tail(log, &head_blk, &tail_blk);
3370 66802 : if (error)
3371 : return error;
3372 :
3373 : /*
3374 : * The superblock was read before the log was available and thus the LSN
3375 : * could not be verified. Check the superblock LSN against the current
3376 : * LSN now that it's known.
3377 : */
3378 133339 : if (xfs_has_crc(log->l_mp) &&
3379 66557 : !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
3380 : return -EINVAL;
3381 :
3382 66771 : if (tail_blk != head_blk) {
3383 : /* There used to be a comment here:
3384 : *
3385 : * disallow recovery on read-only mounts. note -- mount
3386 : * checks for ENOSPC and turns it into an intelligent
3387 : * error message.
3388 : * ...but this is no longer true. Now, unless you specify
3389 : * NORECOVERY (in which case this function would never be
3390 : * called), we just go ahead and recover. We do this all
3391 : * under the vfs layer, so we can get away with it unless
3392 : * the device itself is read-only, in which case we fail.
3393 : */
3394 13592 : if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
3395 : return error;
3396 : }
3397 :
3398 : /*
3399 : * Version 5 superblock log feature mask validation. We know the
3400 : * log is dirty so check if there are any unknown log features
3401 : * in what we need to recover. If there are unknown features
3402 : * (e.g. unsupported transactions, then simply reject the
3403 : * attempt at recovery before touching anything.
3404 : */
3405 13581 : if (xfs_sb_is_v5(&log->l_mp->m_sb) &&
3406 : xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
3407 : XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
3408 0 : xfs_warn(log->l_mp,
3409 : "Superblock has unknown incompatible log features (0x%x) enabled.",
3410 : (log->l_mp->m_sb.sb_features_log_incompat &
3411 : XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
3412 0 : xfs_warn(log->l_mp,
3413 : "The log can not be fully and/or safely recovered by this kernel.");
3414 0 : xfs_warn(log->l_mp,
3415 : "Please recover the log on a kernel that supports the unknown features.");
3416 0 : return -EINVAL;
3417 : }
3418 :
3419 : /*
3420 : * Delay log recovery if the debug hook is set. This is debug
3421 : * instrumentation to coordinate simulation of I/O failures with
3422 : * log recovery.
3423 : */
3424 13581 : if (xfs_globals.log_recovery_delay) {
3425 22 : xfs_notice(log->l_mp,
3426 : "Delaying log recovery for %d seconds.",
3427 : xfs_globals.log_recovery_delay);
3428 22 : msleep(xfs_globals.log_recovery_delay * 1000);
3429 : }
3430 :
3431 15456 : xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
3432 : log->l_mp->m_logname ? log->l_mp->m_logname
3433 : : "internal");
3434 :
3435 13581 : error = xlog_do_recover(log, head_blk, tail_blk);
3436 13581 : set_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate);
3437 : }
3438 : return error;
3439 : }
3440 :
3441 : /*
3442 : * In the first part of recovery we replay inodes and buffers and build up the
3443 : * list of intents which need to be processed. Here we process the intents and
3444 : * clean up the on disk unlinked inode lists. This is separated from the first
3445 : * part of recovery so that the root and real-time bitmap inodes can be read in
3446 : * from disk in between the two stages. This is necessary so that we can free
3447 : * space in the real-time portion of the file system.
3448 : */
3449 : int
3450 13581 : xlog_recover_finish(
3451 : struct xlog *log)
3452 : {
3453 13581 : int error;
3454 :
3455 13581 : error = xlog_recover_process_intents(log);
3456 13581 : if (error) {
3457 : /*
3458 : * Cancel all the unprocessed intent items now so that we don't
3459 : * leave them pinned in the AIL. This can cause the AIL to
3460 : * livelock on the pinned item if anyone tries to push the AIL
3461 : * (inode reclaim does this) before we get around to
3462 : * xfs_log_mount_cancel.
3463 : */
3464 11 : xlog_recover_cancel_intents(log);
3465 11 : xfs_alert(log->l_mp, "Failed to recover intents");
3466 11 : xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
3467 11 : return error;
3468 : }
3469 :
3470 : /*
3471 : * Sync the log to get all the intents out of the AIL. This isn't
3472 : * absolutely necessary, but it helps in case the unlink transactions
3473 : * would have problems pushing the intents out of the way.
3474 : */
3475 13570 : xfs_log_force(log->l_mp, XFS_LOG_SYNC);
3476 :
3477 : /*
3478 : * Now that we've recovered the log and all the intents, we can clear
3479 : * the log incompat feature bits in the superblock because there's no
3480 : * longer anything to protect. We rely on the AIL push to write out the
3481 : * updated superblock after everything else.
3482 : */
3483 13570 : if (xfs_clear_incompat_log_features(log->l_mp,
3484 : XFS_SB_FEAT_INCOMPAT_LOG_ALL)) {
3485 10550 : error = xfs_sync_sb(log->l_mp, false);
3486 10550 : if (error < 0) {
3487 0 : xfs_alert(log->l_mp,
3488 : "Failed to clear log incompat features on recovery");
3489 0 : return error;
3490 : }
3491 : }
3492 :
3493 13570 : xlog_recover_process_iunlinks(log);
3494 :
3495 : /*
3496 : * Recover any CoW staging blocks that are still referenced by the
3497 : * ondisk refcount metadata. During mount there cannot be any live
3498 : * staging extents as we have not permitted any user modifications.
3499 : * Therefore, it is safe to free them all right now, even on a
3500 : * read-only mount.
3501 : */
3502 13570 : error = xfs_reflink_recover_cow(log->l_mp);
3503 13570 : if (error) {
3504 30 : xfs_alert(log->l_mp,
3505 : "Failed to recover leftover CoW staging extents, err %d.",
3506 : error);
3507 : /*
3508 : * If we get an error here, make sure the log is shut down
3509 : * but return zero so that any log items committed since the
3510 : * end of intents processing can be pushed through the CIL
3511 : * and AIL.
3512 : */
3513 30 : xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
3514 : }
3515 :
3516 : return 0;
3517 : }
3518 :
3519 : void
3520 220 : xlog_recover_cancel(
3521 : struct xlog *log)
3522 : {
3523 440 : if (xlog_recovery_needed(log))
3524 0 : xlog_recover_cancel_intents(log);
3525 220 : }
3526 :
|