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 13120207 : if (blk_no < 0 || blk_no >= log->l_logBBsize)
63 : return false;
64 13372753 : 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 252546 : 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 505092 : 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 252546 : if (nbblks > 1 && log->l_sectBBsize > 1)
103 118798 : nbblks += log->l_sectBBsize;
104 252546 : nbblks = round_up(nbblks, log->l_sectBBsize);
105 252546 : 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 13118789 : return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
118 : }
119 :
120 : static int
121 13120207 : 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 13120207 : int error;
129 :
130 26240414 : 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 13120207 : blk_no = round_down(blk_no, log->l_sectBBsize);
138 13120207 : nbblks = round_up(nbblks, log->l_sectBBsize);
139 13120207 : ASSERT(nbblks > 0);
140 :
141 13120207 : error = xfs_rw_bdev(xfs_buftarg_bdev(log->l_targ),
142 13120207 : log->l_logBBstart + blk_no,
143 : BBTOB(nbblks), data, op);
144 13120207 : 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 1418 : xlog_bread_noalign(
155 : struct xlog *log,
156 : xfs_daddr_t blk_no,
157 : int nbblks,
158 : char *data)
159 : {
160 1418 : return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
161 : }
162 :
163 : STATIC int
164 13002983 : xlog_bread(
165 : struct xlog *log,
166 : xfs_daddr_t blk_no,
167 : int nbblks,
168 : char *data,
169 : char **offset)
170 : {
171 13002983 : int error;
172 :
173 13002983 : error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
174 13002983 : if (!error)
175 13002983 : *offset = data + xlog_align(log, blk_no);
176 13002983 : return error;
177 : }
178 :
179 : STATIC int
180 115806 : xlog_bwrite(
181 : struct xlog *log,
182 : xfs_daddr_t blk_no,
183 : int nbblks,
184 : char *data)
185 : {
186 115806 : 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 2654324 : xlog_header_check_recover(
212 : xfs_mount_t *mp,
213 : xlog_rec_header_t *head)
214 : {
215 2654324 : 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 2654324 : 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 2654324 : 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 24319 : xlog_header_check_mount(
243 : xfs_mount_t *mp,
244 : xlog_rec_header_t *head)
245 : {
246 24319 : ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
247 :
248 24319 : 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 24319 : } 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 24319 : 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 24319 : char *offset;
279 24319 : xfs_daddr_t mid_blk;
280 24319 : xfs_daddr_t end_blk;
281 24319 : uint mid_cycle;
282 24319 : int error;
283 :
284 24319 : end_blk = *last_blk;
285 24319 : mid_blk = BLK_AVG(first_blk, end_blk);
286 435771 : while (mid_blk != first_blk && mid_blk != end_blk) {
287 411452 : error = xlog_bread(log, mid_blk, 1, buffer, &offset);
288 411452 : if (error)
289 0 : return error;
290 411452 : mid_cycle = xlog_get_cycle(offset);
291 411452 : if (mid_cycle == cycle)
292 : end_blk = mid_blk; /* last_half_cycle == mid_cycle */
293 : else
294 133587 : first_blk = mid_blk; /* first_half_cycle == mid_cycle */
295 411452 : mid_blk = BLK_AVG(first_blk, end_blk);
296 : }
297 24319 : ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
298 : (mid_blk == end_blk && mid_blk-1 == first_blk));
299 :
300 24319 : *last_blk = end_blk;
301 :
302 24319 : 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 24917 : 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 24917 : xfs_daddr_t i, j;
322 24917 : uint cycle;
323 24917 : char *buffer;
324 24917 : xfs_daddr_t bufblks;
325 24917 : char *buf = NULL;
326 24917 : 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 24917 : bufblks = 1 << ffs(nbblks);
335 24917 : while (bufblks > log->l_logBBsize)
336 0 : bufblks >>= 1;
337 24917 : while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
338 0 : bufblks >>= 1;
339 0 : if (bufblks < log->l_sectBBsize)
340 : return -ENOMEM;
341 : }
342 :
343 203719 : for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
344 178828 : int bcount;
345 :
346 178828 : bcount = min(bufblks, (start_blk + nbblks - i));
347 :
348 178828 : error = xlog_bread(log, i, bcount, buffer, &buf);
349 178828 : if (error)
350 0 : goto out;
351 :
352 72042397 : for (j = 0; j < bcount; j++) {
353 71863595 : cycle = xlog_get_cycle(buf);
354 71863595 : if (cycle == stop_on_cycle_no) {
355 26 : *new_blk = i+j;
356 26 : goto out;
357 : }
358 :
359 71863569 : buf += BBSIZE;
360 : }
361 : }
362 :
363 24891 : *new_blk = -1;
364 :
365 24917 : out:
366 24917 : kmem_free(buffer);
367 24917 : return error;
368 : }
369 :
370 : static inline int
371 94129 : xlog_logrec_hblks(struct xlog *log, struct xlog_rec_header *rh)
372 : {
373 94129 : if (xfs_has_logv2(log->l_mp)) {
374 94117 : int h_size = be32_to_cpu(rh->h_size);
375 :
376 94117 : if ((be32_to_cpu(rh->h_version) & XLOG_VERSION_2) &&
377 : h_size > XLOG_HEADER_CYCLE_SIZE)
378 444 : 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 24326 : 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 24326 : xfs_daddr_t i;
403 24326 : char *buffer;
404 24326 : char *offset = NULL;
405 24326 : xlog_rec_header_t *head = NULL;
406 24326 : int error = 0;
407 24326 : int smallmem = 0;
408 24326 : int num_blks = *last_blk - start_blk;
409 24326 : int xhdrs;
410 :
411 24326 : ASSERT(start_blk != 0 || *last_blk != start_blk);
412 :
413 24326 : buffer = xlog_alloc_buffer(log, num_blks);
414 24326 : if (!buffer) {
415 0 : buffer = xlog_alloc_buffer(log, 1);
416 0 : if (!buffer)
417 : return -ENOMEM;
418 : smallmem = 1;
419 : } else {
420 24326 : error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
421 24326 : if (error)
422 0 : goto out;
423 24326 : offset += ((num_blks - 1) << BBSHIFT);
424 : }
425 :
426 547333 : for (i = (*last_blk) - 1; i >= 0; i--) {
427 547330 : if (i < start_blk) {
428 : /* valid log record not found */
429 4 : xfs_warn(log->l_mp,
430 : "Log inconsistent (didn't find previous header)");
431 4 : ASSERT(0);
432 4 : error = -EFSCORRUPTED;
433 4 : goto out;
434 : }
435 :
436 547326 : if (smallmem) {
437 0 : error = xlog_bread(log, i, 1, buffer, &offset);
438 0 : if (error)
439 0 : goto out;
440 : }
441 :
442 547326 : head = (xlog_rec_header_t *)offset;
443 :
444 547326 : if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
445 : break;
446 :
447 523007 : if (!smallmem)
448 523007 : 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 24322 : 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 24319 : 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 24319 : xhdrs = xlog_logrec_hblks(log, head);
476 :
477 48638 : if (*last_blk - i + extra_bblks !=
478 24319 : BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
479 36 : *last_blk = i;
480 :
481 24283 : out:
482 24326 : kmem_free(buffer);
483 24326 : 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 24323 : xlog_find_head(
501 : struct xlog *log,
502 : xfs_daddr_t *return_head_blk)
503 : {
504 24323 : char *buffer;
505 24323 : char *offset;
506 24323 : xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
507 24323 : int num_scan_bblks;
508 24323 : uint first_half_cycle, last_half_cycle;
509 24323 : uint stop_on_cycle;
510 24323 : int error, log_bbnum = log->l_logBBsize;
511 :
512 : /* Is the end of the log device zeroed? */
513 24323 : error = xlog_find_zeroed(log, &first_blk);
514 24323 : if (error < 0) {
515 0 : xfs_warn(log->l_mp, "empty log check failed");
516 0 : return error;
517 : }
518 24323 : if (error == 1) {
519 8377 : *return_head_blk = first_blk;
520 :
521 : /* Is the whole lot zeroed? */
522 8377 : 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 8377 : return 0;
531 : }
532 :
533 15946 : first_blk = 0; /* get cycle # of 1st block */
534 15946 : buffer = xlog_alloc_buffer(log, 1);
535 15946 : if (!buffer)
536 : return -ENOMEM;
537 :
538 15946 : error = xlog_bread(log, 0, 1, buffer, &offset);
539 15946 : if (error)
540 0 : goto out_free_buffer;
541 :
542 15946 : first_half_cycle = xlog_get_cycle(offset);
543 :
544 15946 : last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
545 15946 : error = xlog_bread(log, last_blk, 1, buffer, &offset);
546 15946 : if (error)
547 0 : goto out_free_buffer;
548 :
549 15946 : last_half_cycle = xlog_get_cycle(offset);
550 15946 : 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 15946 : 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 4 : head_blk = log_bbnum;
590 4 : 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 15942 : stop_on_cycle = last_half_cycle;
615 15942 : error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
616 : last_half_cycle);
617 15942 : 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 15946 : num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
629 15946 : if (head_blk >= num_scan_bblks) {
630 : /*
631 : * We are guaranteed that the entire check can be performed
632 : * in one buffer.
633 : */
634 15352 : start_blk = head_blk - num_scan_bblks;
635 15352 : 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 15352 : if (new_blk != -1)
640 22 : 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 594 : ASSERT(head_blk <= INT_MAX &&
670 : (xfs_daddr_t) num_scan_bblks >= head_blk);
671 594 : start_blk = log_bbnum - (num_scan_bblks - head_blk);
672 594 : if ((error = xlog_find_verify_cycle(log, start_blk,
673 594 : num_scan_bblks - (int)head_blk,
674 : (stop_on_cycle - 1), &new_blk)))
675 0 : goto out_free_buffer;
676 594 : 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 594 : start_blk = 0;
687 594 : ASSERT(head_blk <= INT_MAX);
688 594 : 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 594 : if (new_blk != -1)
693 2 : head_blk = new_blk;
694 : }
695 :
696 592 : 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 15946 : num_scan_bblks = XLOG_REC_SHIFT(log);
702 15946 : if (head_blk >= num_scan_bblks) {
703 15864 : start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
704 :
705 : /* start ptr at last block ptr before head_blk */
706 15864 : error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
707 15864 : if (error == 1)
708 : error = -EIO;
709 15864 : if (error)
710 4 : goto out_free_buffer;
711 : } else {
712 82 : start_blk = 0;
713 82 : ASSERT(head_blk <= INT_MAX);
714 82 : error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
715 82 : if (error < 0)
716 0 : goto out_free_buffer;
717 82 : 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 79 : } else if (error)
733 0 : goto out_free_buffer;
734 : }
735 :
736 15942 : kmem_free(buffer);
737 15942 : if (head_blk == log_bbnum)
738 0 : *return_head_blk = 0;
739 : else
740 15942 : *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 4 : out_free_buffer:
750 4 : kmem_free(buffer);
751 4 : if (error)
752 4 : xfs_warn(log->l_mp, "failed to find log head");
753 4 : 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 35701 : 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 35701 : int i;
776 35701 : int error;
777 35701 : int found = 0;
778 35701 : char *offset = NULL;
779 35701 : xfs_daddr_t end_blk;
780 :
781 35701 : *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 35701 : end_blk = head_blk > tail_blk ? tail_blk : 0;
788 4114481 : for (i = (int) head_blk - 1; i >= end_blk; i--) {
789 4111088 : error = xlog_bread(log, i, 1, buffer, &offset);
790 4111088 : if (error)
791 0 : goto out_error;
792 :
793 4111088 : if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
794 95949 : *rblk = i;
795 95949 : *rhead = (struct xlog_rec_header *) offset;
796 95949 : 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 35701 : if (tail_blk >= head_blk && found != count) {
807 20480 : for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
808 20480 : error = xlog_bread(log, i, 1, buffer, &offset);
809 20480 : if (error)
810 0 : goto out_error;
811 :
812 20480 : if (*(__be32 *)offset ==
813 : cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
814 110 : *wrapped = true;
815 110 : *rblk = i;
816 110 : *rhead = (struct xlog_rec_header *) offset;
817 110 : 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 11371 : 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 11371 : int i;
850 11371 : int error;
851 11371 : int found = 0;
852 11371 : char *offset = NULL;
853 11371 : xfs_daddr_t end_blk;
854 :
855 11371 : *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 11371 : end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
862 11371 : for (i = (int) tail_blk; i <= end_blk; i++) {
863 11371 : error = xlog_bread(log, i, 1, buffer, &offset);
864 11371 : if (error)
865 0 : goto out_error;
866 :
867 11371 : if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
868 11371 : *rblk = i;
869 11371 : *rhead = (struct xlog_rec_header *) offset;
870 11371 : 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 11371 : 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 11371 : xlog_verify_tail(
938 : struct xlog *log,
939 : xfs_daddr_t head_blk,
940 : xfs_daddr_t *tail_blk,
941 : int hsize)
942 : {
943 11371 : struct xlog_rec_header *thead;
944 11371 : char *buffer;
945 11371 : xfs_daddr_t first_bad;
946 11371 : int error = 0;
947 11371 : bool wrapped;
948 11371 : xfs_daddr_t tmp_tail;
949 11371 : xfs_daddr_t orig_tail = *tail_blk;
950 :
951 11371 : buffer = xlog_alloc_buffer(log, 1);
952 11371 : if (!buffer)
953 : return -ENOMEM;
954 :
955 : /*
956 : * Make sure the tail points to a record (returns positive count on
957 : * success).
958 : */
959 11371 : error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
960 : &tmp_tail, &thead, &wrapped);
961 11371 : if (error < 0)
962 0 : goto out;
963 11371 : 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 11371 : first_bad = 0;
974 11371 : error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
975 : XLOG_RECOVER_CRCPASS, &first_bad);
976 11371 : 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 11371 : 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 11371 : out:
1004 11371 : kmem_free(buffer);
1005 11371 : 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 11371 : 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 11371 : struct xlog_rec_header *tmp_rhead;
1032 11371 : char *tmp_buffer;
1033 11371 : xfs_daddr_t first_bad;
1034 11371 : xfs_daddr_t tmp_rhead_blk;
1035 11371 : int found;
1036 11371 : int error;
1037 11371 : 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 11371 : tmp_buffer = xlog_alloc_buffer(log, 1);
1046 11371 : if (!tmp_buffer)
1047 : return -ENOMEM;
1048 11371 : 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 11371 : kmem_free(tmp_buffer);
1052 11371 : 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 11371 : error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
1061 : XLOG_RECOVER_CRCPASS, &first_bad);
1062 11371 : 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 11 : error = 0;
1068 11 : 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 11 : found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
1081 : buffer, rhead_blk, rhead, wrapped);
1082 11 : if (found < 0)
1083 : return found;
1084 11 : 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 11 : *head_blk = first_bad;
1097 11 : *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
1098 11 : if (*head_blk == *tail_blk) {
1099 0 : ASSERT(0);
1100 0 : return 0;
1101 : }
1102 : }
1103 11360 : if (error)
1104 : return error;
1105 :
1106 11371 : return xlog_verify_tail(log, *head_blk, tail_blk,
1107 11371 : 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 298257 : int mod;
1124 :
1125 298257 : div_s64_rem(bno, log->l_logBBsize, &mod);
1126 298257 : 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 24330 : 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 24330 : struct xlog_op_header *op_head;
1145 24330 : xfs_daddr_t umount_data_blk;
1146 24330 : xfs_daddr_t after_umount_blk;
1147 24330 : int hblks;
1148 24330 : int error;
1149 24330 : char *offset;
1150 :
1151 24330 : *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 24330 : hblks = xlog_logrec_hblks(log, rhead);
1163 48660 : after_umount_blk = xlog_wrap_logbno(log,
1164 24330 : rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
1165 :
1166 48660 : if (*head_blk == after_umount_blk &&
1167 24330 : be32_to_cpu(rhead->h_num_logops) == 1) {
1168 12954 : umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
1169 12954 : error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
1170 12954 : if (error)
1171 : return error;
1172 :
1173 12954 : op_head = (struct xlog_op_header *)offset;
1174 12954 : 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 12948 : xlog_assign_atomic_lsn(&log->l_tail_lsn,
1181 12948 : log->l_curr_cycle, after_umount_blk);
1182 12948 : xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
1183 : log->l_curr_cycle, after_umount_blk);
1184 12948 : *tail_blk = after_umount_blk;
1185 :
1186 12948 : *clean = true;
1187 : }
1188 : }
1189 :
1190 : return 0;
1191 : }
1192 :
1193 : static void
1194 24330 : 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 24330 : log->l_prev_block = rhead_blk;
1212 24330 : log->l_curr_block = (int)head_blk;
1213 24330 : log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
1214 24330 : if (bump_cycle)
1215 39 : log->l_curr_cycle++;
1216 24330 : atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
1217 24330 : atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
1218 24330 : xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
1219 : BBTOB(log->l_curr_block));
1220 24330 : xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
1221 : BBTOB(log->l_curr_block));
1222 24330 : }
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 24323 : xlog_find_tail(
1242 : struct xlog *log,
1243 : xfs_daddr_t *head_blk,
1244 : xfs_daddr_t *tail_blk)
1245 : {
1246 24323 : xlog_rec_header_t *rhead;
1247 24323 : char *offset = NULL;
1248 24323 : char *buffer;
1249 24323 : int error;
1250 24323 : xfs_daddr_t rhead_blk;
1251 24323 : xfs_lsn_t tail_lsn;
1252 24323 : bool wrapped = false;
1253 24323 : bool clean = false;
1254 :
1255 : /*
1256 : * Find previous log record
1257 : */
1258 24323 : if ((error = xlog_find_head(log, head_blk)))
1259 : return error;
1260 24319 : ASSERT(*head_blk < INT_MAX);
1261 :
1262 24319 : buffer = xlog_alloc_buffer(log, 1);
1263 24319 : if (!buffer)
1264 : return -ENOMEM;
1265 24319 : if (*head_blk == 0) { /* special case */
1266 36 : error = xlog_bread(log, 0, 1, buffer, &offset);
1267 36 : if (error)
1268 0 : goto done;
1269 :
1270 36 : 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 24319 : error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
1283 : &rhead_blk, &rhead, &wrapped);
1284 24319 : if (error < 0)
1285 0 : goto done;
1286 24319 : 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 24319 : *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 24319 : xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
1297 24319 : 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 24319 : error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
1304 : rhead_blk, buffer, &clean);
1305 24319 : 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 24319 : if (!clean) {
1319 11371 : xfs_daddr_t orig_head = *head_blk;
1320 :
1321 11371 : error = xlog_verify_head(log, head_blk, tail_blk, buffer,
1322 : &rhead_blk, &rhead, &wrapped);
1323 11371 : if (error)
1324 0 : goto done;
1325 :
1326 : /* update in-core state again if the head changed */
1327 11371 : if (*head_blk != orig_head) {
1328 11 : xlog_set_state(log, *head_blk, rhead, rhead_blk,
1329 : wrapped);
1330 11 : tail_lsn = atomic64_read(&log->l_tail_lsn);
1331 11 : error = xlog_check_unmount_rec(log, head_blk, tail_blk,
1332 : rhead, rhead_blk, buffer,
1333 : &clean);
1334 11 : 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 24319 : if (clean)
1345 12948 : 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 24319 : if (!xfs_readonly_buftarg(log->l_targ))
1367 24315 : error = xlog_clear_stale_blocks(log, tail_lsn);
1368 :
1369 4 : done:
1370 24319 : kmem_free(buffer);
1371 :
1372 24319 : 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 24323 : xlog_find_zeroed(
1395 : struct xlog *log,
1396 : xfs_daddr_t *blk_no)
1397 : {
1398 24323 : char *buffer;
1399 24323 : char *offset;
1400 24323 : uint first_cycle, last_cycle;
1401 24323 : xfs_daddr_t new_blk, last_blk, start_blk;
1402 24323 : xfs_daddr_t num_scan_bblks;
1403 24323 : int error, log_bbnum = log->l_logBBsize;
1404 :
1405 24323 : *blk_no = 0;
1406 :
1407 : /* check totally zeroed log */
1408 24323 : buffer = xlog_alloc_buffer(log, 1);
1409 24323 : if (!buffer)
1410 : return -ENOMEM;
1411 24323 : error = xlog_bread(log, 0, 1, buffer, &offset);
1412 24323 : if (error)
1413 0 : goto out_free_buffer;
1414 :
1415 24323 : first_cycle = xlog_get_cycle(offset);
1416 24323 : 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 24323 : error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
1424 24323 : if (error)
1425 0 : goto out_free_buffer;
1426 :
1427 24323 : last_cycle = xlog_get_cycle(offset);
1428 24323 : if (last_cycle != 0) { /* log completely written to */
1429 15946 : kmem_free(buffer);
1430 15946 : return 0;
1431 : }
1432 :
1433 : /* we have a partially zeroed log */
1434 8377 : last_blk = log_bbnum-1;
1435 8377 : error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
1436 8377 : 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 8377 : num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1446 8377 : ASSERT(num_scan_bblks <= INT_MAX);
1447 :
1448 8377 : if (last_blk < num_scan_bblks)
1449 : num_scan_bblks = last_blk;
1450 8377 : 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 8377 : if ((error = xlog_find_verify_cycle(log, start_blk,
1459 : (int)num_scan_bblks, 0, &new_blk)))
1460 0 : goto out_free_buffer;
1461 8377 : if (new_blk != -1)
1462 2 : 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 8377 : error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
1469 8377 : if (error == 1)
1470 : error = -EIO;
1471 8377 : if (error)
1472 0 : goto out_free_buffer;
1473 :
1474 8377 : *blk_no = last_blk;
1475 8377 : out_free_buffer:
1476 8377 : kmem_free(buffer);
1477 8377 : 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 99571040 : 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 99571040 : xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1497 :
1498 99571040 : memset(buf, 0, BBSIZE);
1499 99571040 : recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1500 99571040 : recp->h_cycle = cpu_to_be32(cycle);
1501 99571040 : recp->h_version = cpu_to_be32(
1502 : xfs_has_logv2(log->l_mp) ? 2 : 1);
1503 99571040 : recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1504 99571040 : recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1505 99571040 : recp->h_fmt = cpu_to_be32(XLOG_FMT);
1506 199142080 : memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1507 99571040 : }
1508 :
1509 : STATIC int
1510 24845 : 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 24845 : char *offset;
1519 24845 : char *buffer;
1520 24845 : int balign, ealign;
1521 24845 : int sectbb = log->l_sectBBsize;
1522 24845 : int end_block = start_block + blocks;
1523 24845 : int bufblks;
1524 24845 : int error = 0;
1525 24845 : 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 24845 : bufblks = 1 << ffs(blocks);
1534 24851 : while (bufblks > log->l_logBBsize)
1535 6 : bufblks >>= 1;
1536 24845 : 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 24845 : balign = round_down(start_block, sectbb);
1547 24845 : 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 140651 : for (i = start_block; i < end_block; i += bufblks) {
1556 115806 : int bcount, endcount;
1557 :
1558 115806 : bcount = min(bufblks, end_block - start_block);
1559 115806 : 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 115806 : ealign = round_down(end_block, sectbb);
1566 115806 : 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 115806 : offset = buffer + xlog_align(log, start_block);
1575 99686846 : for (; j < endcount; j++) {
1576 99571040 : xlog_add_record(log, offset, cycle, i+j,
1577 : tail_cycle, tail_block);
1578 99571040 : offset += BBSIZE;
1579 : }
1580 115806 : error = xlog_bwrite(log, start_block, endcount, buffer);
1581 115806 : if (error)
1582 : break;
1583 115806 : start_block += endcount;
1584 115806 : j = 0;
1585 : }
1586 :
1587 24845 : out_free_buffer:
1588 24845 : kmem_free(buffer);
1589 24845 : 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 24315 : xlog_clear_stale_blocks(
1610 : struct xlog *log,
1611 : xfs_lsn_t tail_lsn)
1612 : {
1613 24315 : int tail_cycle, head_cycle;
1614 24315 : int tail_block, head_block;
1615 24315 : int tail_distance, max_distance;
1616 24315 : int distance;
1617 24315 : int error;
1618 :
1619 24315 : tail_cycle = CYCLE_LSN(tail_lsn);
1620 24315 : tail_block = BLOCK_LSN(tail_lsn);
1621 24315 : head_cycle = log->l_curr_cycle;
1622 24315 : 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 24315 : 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 23721 : if (XFS_IS_CORRUPT(log->l_mp,
1639 : head_block < tail_block ||
1640 : head_block >= log->l_logBBsize))
1641 0 : return -EFSCORRUPTED;
1642 23721 : 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 594 : if (XFS_IS_CORRUPT(log->l_mp,
1650 : head_block >= tail_block ||
1651 : head_cycle != tail_cycle + 1))
1652 0 : return -EFSCORRUPTED;
1653 594 : 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 24315 : if (tail_distance <= 0) {
1661 0 : ASSERT(tail_distance == 0);
1662 0 : return 0;
1663 : }
1664 :
1665 24315 : 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 24315 : max_distance = min(max_distance, tail_distance);
1674 :
1675 24315 : 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 23785 : error = xlog_write_log_records(log, (head_cycle - 1),
1684 : head_block, max_distance, tail_cycle,
1685 : tail_block);
1686 23785 : 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 530 : distance = log->l_logBBsize - head_block;
1697 530 : error = xlog_write_log_records(log, (head_cycle - 1),
1698 : head_block, distance, tail_cycle,
1699 : tail_block);
1700 :
1701 530 : 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 530 : distance = max_distance - (log->l_logBBsize - head_block);
1713 530 : error = xlog_write_log_records(log, head_cycle, 0, distance,
1714 : tail_cycle, tail_block);
1715 530 : 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 141458 : xlog_recover_release_intent(
1728 : struct xlog *log,
1729 : unsigned short intent_type,
1730 : uint64_t intent_id)
1731 : {
1732 141458 : struct xfs_ail_cursor cur;
1733 141458 : struct xfs_log_item *lip;
1734 141458 : struct xfs_ail *ailp = log->l_ailp;
1735 :
1736 141458 : spin_lock(&ailp->ail_lock);
1737 165362 : for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0); lip != NULL;
1738 23904 : lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
1739 165099 : if (lip->li_type != intent_type)
1740 18636 : continue;
1741 146463 : if (!lip->li_ops->iop_match(lip, intent_id))
1742 5268 : continue;
1743 :
1744 141195 : spin_unlock(&ailp->ail_lock);
1745 141195 : lip->li_ops->iop_release(lip);
1746 141195 : spin_lock(&ailp->ail_lock);
1747 : break;
1748 : }
1749 :
1750 141458 : xfs_trans_ail_cursor_done(&cur);
1751 141458 : spin_unlock(&ailp->ail_lock);
1752 141458 : }
1753 :
1754 : int
1755 392 : xlog_recover_iget(
1756 : struct xfs_mount *mp,
1757 : xfs_ino_t ino,
1758 : struct xfs_inode **ipp)
1759 : {
1760 392 : int error;
1761 :
1762 392 : error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
1763 392 : if (error)
1764 : return error;
1765 :
1766 392 : error = xfs_qm_dqattach(*ipp);
1767 392 : if (error) {
1768 0 : xfs_irele(*ipp);
1769 0 : return error;
1770 : }
1771 :
1772 392 : if (VFS_I(*ipp)->i_nlink == 0)
1773 27 : 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 51255370 : xlog_find_item_ops(
1806 : struct xlog_recover_item *item)
1807 : {
1808 51255370 : unsigned int i;
1809 :
1810 90278674 : for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
1811 90278674 : if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
1812 51255370 : 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 769812 : xlog_recover_reorder_trans(
1868 : struct xlog *log,
1869 : struct xlog_recover *trans,
1870 : int pass)
1871 : {
1872 769812 : struct xlog_recover_item *item, *n;
1873 769812 : int error = 0;
1874 769812 : LIST_HEAD(sort_list);
1875 769812 : LIST_HEAD(cancel_list);
1876 769812 : LIST_HEAD(buffer_list);
1877 769812 : LIST_HEAD(inode_buffer_list);
1878 769812 : LIST_HEAD(item_list);
1879 :
1880 769812 : list_splice_init(&trans->r_itemq, &sort_list);
1881 52025182 : list_for_each_entry_safe(item, n, &sort_list, ri_list) {
1882 51255370 : enum xlog_recover_reorder fate = XLOG_REORDER_ITEM_LIST;
1883 :
1884 51255370 : item->ri_ops = xlog_find_item_ops(item);
1885 51255370 : 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 51255370 : if (item->ri_ops->reorder)
1901 27286966 : fate = item->ri_ops->reorder(item);
1902 :
1903 27286966 : switch (fate) {
1904 26489326 : case XLOG_REORDER_BUFFER_LIST:
1905 26489326 : list_move_tail(&item->ri_list, &buffer_list);
1906 26489326 : break;
1907 779884 : case XLOG_REORDER_CANCEL_LIST:
1908 779884 : trace_xfs_log_recover_item_reorder_head(log,
1909 : trans, item, pass);
1910 779884 : list_move(&item->ri_list, &cancel_list);
1911 779884 : break;
1912 17756 : case XLOG_REORDER_INODE_BUFFER_LIST:
1913 17756 : list_move(&item->ri_list, &inode_buffer_list);
1914 17756 : break;
1915 23968404 : case XLOG_REORDER_ITEM_LIST:
1916 23968404 : trace_xfs_log_recover_item_reorder_tail(log,
1917 : trans, item, pass);
1918 23968404 : list_move_tail(&item->ri_list, &item_list);
1919 23968404 : break;
1920 : }
1921 : }
1922 :
1923 769812 : ASSERT(list_empty(&sort_list));
1924 769812 : if (!list_empty(&buffer_list))
1925 730950 : list_splice(&buffer_list, &trans->r_itemq);
1926 769812 : if (!list_empty(&item_list))
1927 766142 : list_splice_tail(&item_list, &trans->r_itemq);
1928 769812 : if (!list_empty(&inode_buffer_list))
1929 5484 : list_splice_tail(&inode_buffer_list, &trans->r_itemq);
1930 769812 : if (!list_empty(&cancel_list))
1931 140868 : list_splice_tail(&cancel_list, &trans->r_itemq);
1932 769812 : return error;
1933 : }
1934 :
1935 : void
1936 25333842 : xlog_buf_readahead(
1937 : struct xlog *log,
1938 : xfs_daddr_t blkno,
1939 : uint len,
1940 : const struct xfs_buf_ops *ops)
1941 : {
1942 25333842 : if (!xlog_is_buffer_cancelled(log, blkno, len))
1943 24259331 : xfs_buf_readahead(log->l_mp->m_ddev_targp, blkno, len, ops);
1944 25333842 : }
1945 :
1946 : STATIC int
1947 490676 : 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 490676 : struct xlog_recover_item *item;
1954 490676 : int error = 0;
1955 :
1956 26118361 : list_for_each_entry(item, item_list, ri_list) {
1957 25627685 : trace_xfs_log_recover_item_recover(log, trans, item,
1958 : XLOG_RECOVER_PASS2);
1959 :
1960 25627685 : if (item->ri_ops->commit_pass2)
1961 25627685 : error = item->ri_ops->commit_pass2(log, buffer_list,
1962 : item, trans->r_lsn);
1963 25627685 : 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 769812 : xlog_recover_commit_trans(
1978 : struct xlog *log,
1979 : struct xlog_recover *trans,
1980 : int pass,
1981 : struct list_head *buffer_list)
1982 : {
1983 769812 : int error = 0;
1984 769812 : int items_queued = 0;
1985 769812 : struct xlog_recover_item *item;
1986 769812 : struct xlog_recover_item *next;
1987 769812 : LIST_HEAD (ra_list);
1988 769812 : LIST_HEAD (done_list);
1989 :
1990 : #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
1991 :
1992 769812 : hlist_del_init(&trans->r_list);
1993 :
1994 769812 : error = xlog_recover_reorder_trans(log, trans, pass);
1995 769812 : if (error)
1996 : return error;
1997 :
1998 52025182 : list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
1999 51255370 : trace_xfs_log_recover_item_recover(log, trans, item, pass);
2000 :
2001 51255370 : switch (pass) {
2002 25627685 : case XLOG_RECOVER_PASS1:
2003 25627685 : if (item->ri_ops->commit_pass1)
2004 13635892 : error = item->ri_ops->commit_pass1(log, item);
2005 : break;
2006 25627685 : case XLOG_RECOVER_PASS2:
2007 25627685 : if (item->ri_ops->ra_pass2)
2008 25333842 : item->ri_ops->ra_pass2(log, item);
2009 25627685 : list_move_tail(&item->ri_list, &ra_list);
2010 25627685 : items_queued++;
2011 25627685 : if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
2012 107329 : error = xlog_recover_items_pass2(log, trans,
2013 : buffer_list, &ra_list);
2014 107329 : 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 51255370 : if (error)
2024 0 : goto out;
2025 : }
2026 :
2027 769812 : out:
2028 769812 : if (!list_empty(&ra_list)) {
2029 383347 : if (!error)
2030 383347 : error = xlog_recover_items_pass2(log, trans,
2031 : buffer_list, &ra_list);
2032 383347 : list_splice_tail_init(&ra_list, &done_list);
2033 : }
2034 :
2035 769812 : if (!list_empty(&done_list))
2036 384906 : list_splice_init(&done_list, &trans->r_itemq);
2037 :
2038 : return error;
2039 : }
2040 :
2041 : STATIC void
2042 51377984 : xlog_recover_add_item(
2043 : struct list_head *head)
2044 : {
2045 51377984 : struct xlog_recover_item *item;
2046 :
2047 51377984 : item = kmem_zalloc(sizeof(struct xlog_recover_item), 0);
2048 51377984 : INIT_LIST_HEAD(&item->ri_list);
2049 51377984 : list_add_tail(&item->ri_list, head);
2050 51377984 : }
2051 :
2052 : STATIC int
2053 1839056 : xlog_recover_add_to_cont_trans(
2054 : struct xlog *log,
2055 : struct xlog_recover *trans,
2056 : char *dp,
2057 : int len)
2058 : {
2059 1839056 : struct xlog_recover_item *item;
2060 1839056 : char *ptr, *old_ptr;
2061 1839056 : 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 1839056 : if (list_empty(&trans->r_itemq)) {
2068 2 : ASSERT(len <= sizeof(struct xfs_trans_header));
2069 2 : 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 2 : xlog_recover_add_item(&trans->r_itemq);
2075 2 : ptr = (char *)&trans->r_theader +
2076 2 : sizeof(struct xfs_trans_header) - len;
2077 4 : memcpy(ptr, dp, len);
2078 2 : return 0;
2079 : }
2080 :
2081 : /* take the tail entry */
2082 1839054 : item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
2083 : ri_list);
2084 :
2085 1839054 : old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
2086 1839054 : old_len = item->ri_buf[item->ri_cnt-1].i_len;
2087 :
2088 1839054 : ptr = kvrealloc(old_ptr, old_len, len + old_len, GFP_KERNEL);
2089 1839054 : if (!ptr)
2090 : return -ENOMEM;
2091 3678108 : memcpy(&ptr[old_len], dp, len);
2092 1839054 : item->ri_buf[item->ri_cnt-1].i_len += len;
2093 1839054 : item->ri_buf[item->ri_cnt-1].i_addr = ptr;
2094 1839054 : trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
2095 1839054 : 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 129500750 : xlog_recover_add_to_trans(
2113 : struct xlog *log,
2114 : struct xlog_recover *trans,
2115 : char *dp,
2116 : int len)
2117 : {
2118 129500750 : struct xfs_inode_log_format *in_f; /* any will do */
2119 129500750 : struct xlog_recover_item *item;
2120 129500750 : char *ptr;
2121 :
2122 129500750 : if (!len)
2123 : return 0;
2124 129500750 : if (list_empty(&trans->r_itemq)) {
2125 : /* we need to catch log corruptions here */
2126 771060 : 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 771060 : 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 771060 : if (len == sizeof(struct xfs_trans_header))
2145 771058 : xlog_recover_add_item(&trans->r_itemq);
2146 1542120 : memcpy(&trans->r_theader, dp, len);
2147 771060 : return 0;
2148 : }
2149 :
2150 128729690 : ptr = kmem_alloc(len, 0);
2151 257459380 : memcpy(ptr, dp, len);
2152 128729690 : in_f = (struct xfs_inode_log_format *)ptr;
2153 :
2154 : /* take the tail entry */
2155 128729690 : item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
2156 : ri_list);
2157 128729690 : if (item->ri_total != 0 &&
2158 127958630 : item->ri_total == item->ri_cnt) {
2159 : /* tail item is in use, get a new one */
2160 50606924 : xlog_recover_add_item(&trans->r_itemq);
2161 50606924 : item = list_entry(trans->r_itemq.prev,
2162 : struct xlog_recover_item, ri_list);
2163 : }
2164 :
2165 128729690 : if (item->ri_total == 0) { /* first region to be added */
2166 51377984 : 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 51377984 : item->ri_total = in_f->ilf_size;
2177 51377984 : item->ri_buf =
2178 51377984 : kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
2179 : 0);
2180 : }
2181 :
2182 128729690 : 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 128729690 : item->ri_buf[item->ri_cnt].i_addr = ptr;
2193 128729690 : item->ri_buf[item->ri_cnt].i_len = len;
2194 128729690 : item->ri_cnt++;
2195 128729690 : trace_xfs_log_recover_item_add(log, trans, item, 0);
2196 128729690 : 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 771060 : xlog_recover_free_trans(
2206 : struct xlog_recover *trans)
2207 : {
2208 771060 : struct xlog_recover_item *item, *n;
2209 771060 : int i;
2210 :
2211 771060 : hlist_del_init(&trans->r_list);
2212 :
2213 52149044 : list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
2214 : /* Free the regions in the item. */
2215 51377984 : list_del(&item->ri_list);
2216 180107674 : for (i = 0; i < item->ri_cnt; i++)
2217 128729690 : kmem_free(item->ri_buf[i].i_addr);
2218 : /* Free the item itself */
2219 51377984 : kmem_free(item->ri_buf);
2220 51377984 : kmem_free(item);
2221 : }
2222 : /* Free the transaction recover structure */
2223 771060 : kmem_free(trans);
2224 771060 : }
2225 :
2226 : /*
2227 : * On error or completion, trans is freed.
2228 : */
2229 : STATIC int
2230 132109618 : 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 132109618 : int error = 0;
2240 132109618 : bool freeit = false;
2241 :
2242 : /* mask off ophdr transaction container flags */
2243 132109618 : flags &= ~XLOG_END_TRANS;
2244 132109618 : if (flags & XLOG_WAS_CONT_TRANS)
2245 1839056 : 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 132109618 : switch (flags) {
2252 : /* expected flag values */
2253 129500750 : case 0:
2254 : case XLOG_CONTINUE_TRANS:
2255 129500750 : error = xlog_recover_add_to_trans(log, trans, dp, len);
2256 129500750 : break;
2257 1839056 : case XLOG_WAS_CONT_TRANS:
2258 1839056 : error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
2259 1839056 : break;
2260 769812 : case XLOG_COMMIT_TRANS:
2261 769812 : error = xlog_recover_commit_trans(log, trans, pass,
2262 : buffer_list);
2263 : /* success or fail, we are now done with this transaction. */
2264 769812 : freeit = true;
2265 769812 : 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 132109618 : if (error || freeit)
2281 769812 : xlog_recover_free_trans(trans);
2282 132109618 : 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 132988156 : xlog_recover_ophdr_to_trans(
2294 : struct hlist_head rhash[],
2295 : struct xlog_rec_header *rhead,
2296 : struct xlog_op_header *ohead)
2297 : {
2298 132988156 : struct xlog_recover *trans;
2299 132988156 : xlog_tid_t tid;
2300 132988156 : struct hlist_head *rhp;
2301 :
2302 132988156 : tid = be32_to_cpu(ohead->oh_tid);
2303 132988156 : rhp = &rhash[XLOG_RHASH(tid)];
2304 265976314 : hlist_for_each_entry(trans, rhp, r_list) {
2305 132109620 : if (trans->r_log_tid == tid)
2306 132109618 : 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 878538 : if (!(ohead->oh_flags & XLOG_START_TRANS))
2314 : return NULL;
2315 :
2316 771060 : 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 771060 : trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
2323 771060 : trans->r_log_tid = tid;
2324 771060 : trans->r_lsn = be64_to_cpu(rhead->h_lsn);
2325 771060 : INIT_LIST_HEAD(&trans->r_itemq);
2326 771060 : INIT_HLIST_NODE(&trans->r_list);
2327 771060 : 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 771060 : return NULL;
2334 : }
2335 :
2336 : STATIC int
2337 132988156 : 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 132988156 : struct xlog_recover *trans;
2348 132988156 : unsigned int len;
2349 132988156 : int error;
2350 :
2351 : /* Do we understand who wrote this op? */
2352 132988156 : 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 132988156 : len = be32_to_cpu(ohead->oh_len);
2364 132988156 : 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 132988156 : trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
2371 132988156 : 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 132109618 : if (log->l_recovery_lsn != trans->r_lsn &&
2400 132018364 : ohead->oh_flags & XLOG_COMMIT_TRANS) {
2401 766980 : error = xfs_buf_delwri_submit(buffer_list);
2402 766980 : if (error)
2403 : return error;
2404 766980 : log->l_recovery_lsn = trans->r_lsn;
2405 : }
2406 :
2407 132109618 : return xlog_recovery_process_trans(log, trans, dp, len,
2408 132109618 : 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 2654324 : 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 2654324 : struct xlog_op_header *ohead;
2430 2654324 : char *end;
2431 2654324 : int num_logops;
2432 2654324 : int error;
2433 :
2434 2654324 : end = dp + be32_to_cpu(rhead->h_len);
2435 2654324 : num_logops = be32_to_cpu(rhead->h_num_logops);
2436 :
2437 : /* check the log format matches our own - else we can't recover */
2438 2654324 : if (xlog_header_check_recover(log->l_mp, rhead))
2439 : return -EIO;
2440 :
2441 2654324 : trace_xfs_log_recover_record(log, rhead, pass);
2442 135642480 : while ((dp < end) && num_logops) {
2443 :
2444 132988156 : ohead = (struct xlog_op_header *)dp;
2445 132988156 : dp += sizeof(*ohead);
2446 132988156 : ASSERT(dp <= end);
2447 :
2448 : /* errors will abort recovery */
2449 132988156 : error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
2450 : dp, end, pass, buffer_list);
2451 132988156 : if (error)
2452 0 : return error;
2453 :
2454 132988156 : dp += be32_to_cpu(ohead->oh_len);
2455 132988156 : num_logops--;
2456 : }
2457 : return 0;
2458 : }
2459 :
2460 : /* Take all the collected deferred ops and finish them in order. */
2461 : static int
2462 11367 : xlog_finish_defer_ops(
2463 : struct xfs_mount *mp,
2464 : struct list_head *capture_list)
2465 : {
2466 11367 : struct xfs_defer_capture *dfc, *next;
2467 11367 : struct xfs_trans *tp;
2468 11367 : int error = 0;
2469 :
2470 12006 : list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
2471 639 : struct xfs_trans_res resv;
2472 639 : 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 639 : resv.tr_logres = dfc->dfc_logres;
2481 639 : resv.tr_logcount = 1;
2482 639 : resv.tr_logflags = XFS_TRANS_PERM_LOG_RES;
2483 :
2484 639 : error = xfs_trans_alloc(mp, &resv, dfc->dfc_blkres,
2485 : dfc->dfc_rtxres, XFS_TRANS_RESERVE, &tp);
2486 639 : 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 639 : list_del_init(&dfc->dfc_list);
2496 639 : xfs_defer_ops_continue(dfc, tp, &dres);
2497 639 : error = xfs_trans_commit(tp);
2498 639 : xfs_defer_resources_rele(&dres);
2499 639 : if (error)
2500 0 : return error;
2501 : }
2502 :
2503 11367 : 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 2 : xlog_abort_defer_ops(
2510 : struct xfs_mount *mp,
2511 : struct list_head *capture_list)
2512 : {
2513 2 : struct xfs_defer_capture *dfc;
2514 2 : struct xfs_defer_capture *next;
2515 :
2516 2 : 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 2 : }
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 11369 : xlog_recover_process_intents(
2540 : struct xlog *log)
2541 : {
2542 11369 : LIST_HEAD(capture_list);
2543 11369 : struct xfs_ail_cursor cur;
2544 11369 : struct xfs_log_item *lip;
2545 11369 : struct xfs_ail *ailp;
2546 11369 : int error = 0;
2547 : #if defined(DEBUG) || defined(XFS_WARN)
2548 11369 : xfs_lsn_t last_lsn;
2549 : #endif
2550 :
2551 11369 : ailp = log->l_ailp;
2552 11369 : spin_lock(&ailp->ail_lock);
2553 : #if defined(DEBUG) || defined(XFS_WARN)
2554 11369 : last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
2555 : #endif
2556 11369 : for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
2557 14966 : lip != NULL;
2558 3597 : lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
2559 3599 : const struct xfs_item_ops *ops;
2560 :
2561 3599 : 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 7198 : 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 3599 : spin_unlock(&ailp->ail_lock);
2581 3599 : ops = lip->li_ops;
2582 3599 : error = ops->iop_recover(lip, &capture_list);
2583 3599 : spin_lock(&ailp->ail_lock);
2584 3599 : if (error) {
2585 2 : trace_xlog_intent_recovery_failed(log->l_mp, error,
2586 2 : ops->iop_recover);
2587 2 : break;
2588 : }
2589 : }
2590 :
2591 11369 : xfs_trans_ail_cursor_done(&cur);
2592 11369 : spin_unlock(&ailp->ail_lock);
2593 11369 : if (error)
2594 2 : goto err;
2595 :
2596 11367 : error = xlog_finish_defer_ops(log->l_mp, &capture_list);
2597 11367 : if (error)
2598 0 : goto err;
2599 :
2600 : return 0;
2601 2 : err:
2602 2 : xlog_abort_defer_ops(log->l_mp, &capture_list);
2603 2 : 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 2 : xlog_recover_cancel_intents(
2613 : struct xlog *log)
2614 : {
2615 2 : struct xfs_log_item *lip;
2616 2 : struct xfs_ail_cursor cur;
2617 2 : struct xfs_ail *ailp;
2618 :
2619 2 : ailp = log->l_ailp;
2620 2 : spin_lock(&ailp->ail_lock);
2621 2 : lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
2622 2 : 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 2 : xfs_trans_ail_cursor_done(&cur);
2633 2 : spin_unlock(&ailp->ail_lock);
2634 2 : }
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 4 : xlog_recover_clear_agi_bucket(
2642 : struct xfs_perag *pag,
2643 : int bucket)
2644 : {
2645 4 : struct xfs_mount *mp = pag->pag_mount;
2646 4 : struct xfs_trans *tp;
2647 4 : struct xfs_agi *agi;
2648 4 : struct xfs_buf *agibp;
2649 4 : int offset;
2650 4 : int error;
2651 :
2652 4 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
2653 4 : if (error)
2654 4 : 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 4 : out_error:
2675 4 : xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__,
2676 : pag->pag_agno);
2677 4 : return;
2678 : }
2679 :
2680 : static int
2681 2908032 : xlog_recover_iunlink_bucket(
2682 : struct xfs_perag *pag,
2683 : struct xfs_agi *agi,
2684 : int bucket)
2685 : {
2686 2908032 : struct xfs_mount *mp = pag->pag_mount;
2687 2908032 : struct xfs_inode *prev_ip = NULL;
2688 2908032 : struct xfs_inode *ip;
2689 2908032 : xfs_agino_t prev_agino, agino;
2690 2908032 : int error = 0;
2691 :
2692 2908032 : agino = be32_to_cpu(agi->agi_unlinked[bucket]);
2693 3074776 : while (agino != NULLAGINO) {
2694 166744 : error = xfs_iget(mp, NULL,
2695 166744 : XFS_AGINO_TO_INO(mp, pag->pag_agno, agino),
2696 : 0, 0, &ip);
2697 166744 : if (error)
2698 : break;
2699 :
2700 166744 : ASSERT(VFS_I(ip)->i_nlink == 0);
2701 166744 : ASSERT(VFS_I(ip)->i_mode != 0);
2702 166744 : xfs_iflags_clear(ip, XFS_IRECOVERY);
2703 166744 : agino = ip->i_next_unlinked;
2704 :
2705 166744 : if (prev_ip) {
2706 161114 : ip->i_prev_unlinked = prev_agino;
2707 161114 : 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 161114 : error = xfs_inodegc_flush(mp);
2719 161114 : if (error)
2720 : break;
2721 : }
2722 :
2723 166744 : prev_agino = agino;
2724 166744 : prev_ip = ip;
2725 : }
2726 :
2727 2908032 : if (prev_ip) {
2728 5630 : int error2;
2729 :
2730 5630 : ip->i_prev_unlinked = prev_agino;
2731 5630 : xfs_irele(prev_ip);
2732 :
2733 5630 : error2 = xfs_inodegc_flush(mp);
2734 5630 : if (error2 && !error)
2735 4 : 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 45450 : xlog_recover_iunlink_ag(
2765 : struct xfs_perag *pag)
2766 : {
2767 45450 : struct xfs_agi *agi;
2768 45450 : struct xfs_buf *agibp;
2769 45450 : int bucket;
2770 45450 : int error;
2771 :
2772 45450 : error = xfs_read_agi(pag, NULL, &agibp);
2773 45450 : 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 12 : 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 45438 : agi = agibp->b_addr;
2792 45438 : xfs_buf_unlock(agibp);
2793 :
2794 2998908 : for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
2795 2908032 : error = xlog_recover_iunlink_bucket(pag, agi, bucket);
2796 2908032 : 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 4 : xlog_recover_clear_agi_bucket(pag, bucket);
2804 : }
2805 : }
2806 :
2807 45438 : xfs_buf_rele(agibp);
2808 : }
2809 :
2810 : static void
2811 11367 : xlog_recover_process_iunlinks(
2812 : struct xlog *log)
2813 : {
2814 11367 : struct xfs_perag *pag;
2815 11367 : xfs_agnumber_t agno;
2816 :
2817 56817 : for_each_perag(log->l_mp, agno, pag)
2818 45450 : xlog_recover_iunlink_ag(pag);
2819 11367 : }
2820 :
2821 : STATIC void
2822 2654324 : xlog_unpack_data(
2823 : struct xlog_rec_header *rhead,
2824 : char *dp,
2825 : struct xlog *log)
2826 : {
2827 2654324 : int i, j, k;
2828 :
2829 146655420 : for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
2830 144001096 : i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
2831 144001096 : *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
2832 144001096 : dp += BBSIZE;
2833 : }
2834 :
2835 2654324 : if (xfs_has_logv2(log->l_mp)) {
2836 : xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
2837 2959452 : for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
2838 305128 : j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
2839 305128 : k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
2840 305128 : *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
2841 305128 : dp += BBSIZE;
2842 : }
2843 : }
2844 2654324 : }
2845 :
2846 : /*
2847 : * CRC check, unpack and process a log record.
2848 : */
2849 : STATIC int
2850 4053215 : 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 4053215 : __le32 old_crc = rhead->h_crc;
2859 4053215 : __le32 crc;
2860 :
2861 4053215 : 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 4053215 : if (pass == XLOG_RECOVER_CRCPASS) {
2871 1398891 : if (old_crc && crc != old_crc)
2872 : return -EFSBADCRC;
2873 1398880 : 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 2654324 : 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 2654324 : xlog_unpack_data(rhead, dp, log);
2902 :
2903 2654324 : return xlog_recover_process_data(log, rhash, rhead, dp, pass,
2904 : buffer_list);
2905 : }
2906 :
2907 : STATIC int
2908 4098695 : xlog_valid_rec_header(
2909 : struct xlog *log,
2910 : struct xlog_rec_header *rhead,
2911 : xfs_daddr_t blkno,
2912 : int bufsize)
2913 : {
2914 4098695 : int hlen;
2915 :
2916 4098695 : if (XFS_IS_CORRUPT(log->l_mp,
2917 : rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)))
2918 0 : return -EFSCORRUPTED;
2919 4098695 : 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 4098695 : hlen = be32_to_cpu(rhead->h_len);
2933 4098695 : if (XFS_IS_CORRUPT(log->l_mp, hlen <= 0 || hlen > bufsize))
2934 0 : return -EFSCORRUPTED;
2935 :
2936 4098695 : 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 45480 : 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 45480 : xlog_rec_header_t *rhead;
2959 45480 : xfs_daddr_t blk_no, rblk_no;
2960 45480 : xfs_daddr_t rhead_blk;
2961 45480 : char *offset;
2962 45480 : char *hbp, *dbp;
2963 45480 : int error = 0, h_size, h_len;
2964 45480 : int error2 = 0;
2965 45480 : int bblks, split_bblks;
2966 45480 : int hblks, split_hblks, wrapped_hblks;
2967 45480 : int i;
2968 45480 : struct hlist_head rhash[XLOG_RHASH_SIZE];
2969 45480 : LIST_HEAD (buffer_list);
2970 :
2971 45480 : ASSERT(head_blk != tail_blk);
2972 : blk_no = rhead_blk = tail_blk;
2973 :
2974 773160 : for (i = 0; i < XLOG_RHASH_SIZE; i++)
2975 727680 : 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 45480 : 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 45480 : hbp = xlog_alloc_buffer(log, 1);
2988 45480 : if (!hbp)
2989 : return -ENOMEM;
2990 :
2991 45480 : error = xlog_bread(log, tail_blk, 1, hbp, &offset);
2992 45480 : if (error)
2993 0 : goto bread_err1;
2994 :
2995 45480 : 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 45480 : h_size = be32_to_cpu(rhead->h_size);
3009 45480 : h_len = be32_to_cpu(rhead->h_len);
3010 45480 : 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 45480 : error = xlog_valid_rec_header(log, rhead, tail_blk, h_size);
3019 45480 : if (error)
3020 0 : goto bread_err1;
3021 :
3022 45480 : hblks = xlog_logrec_hblks(log, rhead);
3023 45480 : if (hblks != 1) {
3024 168 : kmem_free(hbp);
3025 168 : 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 45480 : if (!hbp)
3035 : return -ENOMEM;
3036 45480 : dbp = xlog_alloc_buffer(log, BTOBB(h_size));
3037 45480 : if (!dbp) {
3038 0 : kmem_free(hbp);
3039 0 : return -ENOMEM;
3040 : }
3041 :
3042 45480 : memset(rhash, 0, sizeof(rhash));
3043 45480 : 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 264071 : while (blk_no < log->l_logBBsize) {
3050 : /*
3051 : * Check for header wrapping around physical end-of-log
3052 : */
3053 262391 : offset = hbp;
3054 262391 : split_hblks = 0;
3055 262391 : wrapped_hblks = 0;
3056 262391 : if (blk_no + hblks <= log->l_logBBsize) {
3057 : /* Read header in one read */
3058 262391 : error = xlog_bread(log, blk_no, hblks, hbp,
3059 : &offset);
3060 262391 : 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 262391 : rhead = (xlog_rec_header_t *)offset;
3096 262391 : error = xlog_valid_rec_header(log, rhead,
3097 : split_hblks ? blk_no : 0, h_size);
3098 262391 : if (error)
3099 0 : goto bread_err2;
3100 :
3101 262391 : bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
3102 262391 : 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 262391 : if (blk_no + bblks <= log->l_logBBsize ||
3112 : blk_no >= log->l_logBBsize) {
3113 260973 : rblk_no = xlog_wrap_logbno(log, blk_no);
3114 260973 : error = xlog_bread(log, rblk_no, bblks, dbp,
3115 : &offset);
3116 260973 : if (error)
3117 0 : goto bread_err2;
3118 : } else {
3119 : /* This log record is split across the
3120 : * physical end of log */
3121 1418 : offset = dbp;
3122 1418 : split_bblks = 0;
3123 1418 : if (blk_no != log->l_logBBsize) {
3124 : /* some data is before the physical
3125 : * end of log */
3126 1418 : ASSERT(!wrapped_hblks);
3127 1418 : ASSERT(blk_no <= INT_MAX);
3128 1418 : split_bblks =
3129 1418 : log->l_logBBsize - (int)blk_no;
3130 1418 : ASSERT(split_bblks > 0);
3131 1418 : error = xlog_bread(log, blk_no,
3132 : split_bblks, dbp,
3133 : &offset);
3134 1418 : 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 1418 : error = xlog_bread_noalign(log, 0,
3151 : bblks - split_bblks,
3152 1418 : offset + BBTOB(split_bblks));
3153 1418 : if (error)
3154 0 : goto bread_err2;
3155 : }
3156 :
3157 262391 : error = xlog_recover_process(log, rhash, rhead, offset,
3158 : pass, &buffer_list);
3159 262391 : if (error)
3160 0 : goto bread_err2;
3161 :
3162 : blk_no += bblks;
3163 : rhead_blk = blk_no;
3164 : }
3165 :
3166 1680 : ASSERT(blk_no >= log->l_logBBsize);
3167 1680 : blk_no -= log->l_logBBsize;
3168 1680 : rhead_blk = blk_no;
3169 : }
3170 :
3171 : /* read first part of physical log */
3172 3836293 : while (blk_no < head_blk) {
3173 3790824 : error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3174 3790824 : if (error)
3175 0 : goto bread_err2;
3176 :
3177 3790824 : rhead = (xlog_rec_header_t *)offset;
3178 3790824 : error = xlog_valid_rec_header(log, rhead, blk_no, h_size);
3179 3790824 : if (error)
3180 0 : goto bread_err2;
3181 :
3182 : /* blocks in data section */
3183 3790824 : bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
3184 3790824 : error = xlog_bread(log, blk_no+hblks, bblks, dbp,
3185 : &offset);
3186 3790824 : if (error)
3187 0 : goto bread_err2;
3188 :
3189 3790824 : error = xlog_recover_process(log, rhash, rhead, offset, pass,
3190 : &buffer_list);
3191 3790824 : if (error)
3192 11 : goto bread_err2;
3193 :
3194 3790813 : blk_no += bblks + hblks;
3195 3790813 : rhead_blk = blk_no;
3196 : }
3197 :
3198 45469 : bread_err2:
3199 45480 : kmem_free(dbp);
3200 45480 : bread_err1:
3201 45480 : kmem_free(hbp);
3202 :
3203 : /*
3204 : * Submit buffers that have been added from the last record processed,
3205 : * regardless of error status.
3206 : */
3207 45480 : if (!list_empty(&buffer_list))
3208 10681 : error2 = xfs_buf_delwri_submit(&buffer_list);
3209 :
3210 45480 : if (error && first_bad)
3211 11 : *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 773160 : for (i = 0; i < XLOG_RHASH_SIZE; i++) {
3219 727680 : struct hlist_node *tmp;
3220 727680 : struct xlog_recover *trans;
3221 :
3222 1456608 : hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
3223 1248 : xlog_recover_free_trans(trans);
3224 : }
3225 :
3226 45480 : 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 11369 : xlog_do_log_recovery(
3244 : struct xlog *log,
3245 : xfs_daddr_t head_blk,
3246 : xfs_daddr_t tail_blk)
3247 : {
3248 11369 : int error;
3249 :
3250 11369 : 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 11369 : error = xlog_alloc_buf_cancel_table(log);
3257 11369 : if (error)
3258 : return error;
3259 :
3260 11369 : error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3261 : XLOG_RECOVER_PASS1, NULL);
3262 11369 : 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 11369 : error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3270 : XLOG_RECOVER_PASS2, NULL);
3271 11369 : if (!error)
3272 11369 : xlog_check_buf_cancel_table(log);
3273 0 : out_cancel:
3274 11369 : xlog_free_buf_cancel_table(log);
3275 11369 : return error;
3276 : }
3277 :
3278 : /*
3279 : * Do the actual recovery
3280 : */
3281 : STATIC int
3282 11369 : xlog_do_recover(
3283 : struct xlog *log,
3284 : xfs_daddr_t head_blk,
3285 : xfs_daddr_t tail_blk)
3286 : {
3287 11369 : struct xfs_mount *mp = log->l_mp;
3288 11369 : struct xfs_buf *bp = mp->m_sb_bp;
3289 11369 : struct xfs_sb *sbp = &mp->m_sb;
3290 11369 : int error;
3291 :
3292 11369 : trace_xfs_log_recover(log, head_blk, tail_blk);
3293 :
3294 : /*
3295 : * First replay the images in the log.
3296 : */
3297 11369 : error = xlog_do_log_recovery(log, head_blk, tail_blk);
3298 11369 : if (error)
3299 : return error;
3300 :
3301 22738 : 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 11369 : 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 11369 : xfs_buf_lock(bp);
3320 11369 : xfs_buf_hold(bp);
3321 11369 : error = _xfs_buf_read(bp, XBF_READ);
3322 11369 : 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 11369 : xfs_sb_from_disk(sbp, bp->b_addr);
3333 11369 : xfs_buf_relse(bp);
3334 :
3335 : /* re-initialise in-core superblock and geometry structures */
3336 11369 : mp->m_features |= xfs_sb_version_to_features(sbp);
3337 11369 : xfs_reinit_percpu_counters(mp);
3338 11369 : error = xfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
3339 : &mp->m_maxagi);
3340 11369 : if (error) {
3341 0 : xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
3342 0 : return error;
3343 : }
3344 11369 : error = xfs_initialize_rtgroups(mp, sbp->sb_rgcount);
3345 11369 : if (error) {
3346 0 : xfs_warn(mp, "Failed post-recovery rtgroup init: %d", error);
3347 0 : return error;
3348 : }
3349 11369 : mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
3350 :
3351 : /* Normal transactions can now occur */
3352 11369 : clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
3353 : 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 24323 : xlog_recover(
3363 : struct xlog *log)
3364 : {
3365 24323 : xfs_daddr_t head_blk, tail_blk;
3366 24323 : int error;
3367 :
3368 : /* find the tail of the log */
3369 24323 : error = xlog_find_tail(log, &head_blk, &tail_blk);
3370 24323 : 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 48596 : if (xfs_has_crc(log->l_mp) &&
3379 24277 : !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
3380 : return -EINVAL;
3381 :
3382 24317 : 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 11371 : 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 11369 : 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 11369 : if (xfs_globals.log_recovery_delay) {
3425 4 : xfs_notice(log->l_mp,
3426 : "Delaying log recovery for %d seconds.",
3427 : xfs_globals.log_recovery_delay);
3428 4 : msleep(xfs_globals.log_recovery_delay * 1000);
3429 : }
3430 :
3431 11369 : xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
3432 : log->l_mp->m_logname ? log->l_mp->m_logname
3433 : : "internal");
3434 :
3435 11369 : error = xlog_do_recover(log, head_blk, tail_blk);
3436 11369 : 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 11369 : xlog_recover_finish(
3451 : struct xlog *log)
3452 : {
3453 11369 : int error;
3454 :
3455 11369 : error = xlog_recover_process_intents(log);
3456 11369 : 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 2 : xlog_recover_cancel_intents(log);
3465 2 : xfs_alert(log->l_mp, "Failed to recover intents");
3466 2 : xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
3467 2 : 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 11367 : 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 11367 : if (xfs_clear_incompat_log_features(log->l_mp,
3484 : XFS_SB_FEAT_INCOMPAT_LOG_ALL)) {
3485 9414 : error = xfs_sync_sb(log->l_mp, false);
3486 9414 : 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 11367 : 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 11367 : error = xfs_reflink_recover_cow(log->l_mp);
3503 11367 : if (error) {
3504 8 : 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 8 : xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
3514 : }
3515 :
3516 : return 0;
3517 : }
3518 :
3519 : void
3520 42 : xlog_recover_cancel(
3521 : struct xlog *log)
3522 : {
3523 84 : if (xlog_recovery_needed(log))
3524 0 : xlog_recover_cancel_intents(log);
3525 42 : }
3526 :
|