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_mount.h"
14 : #include "xfs_trans.h"
15 : #include "xfs_buf_item.h"
16 : #include "xfs_trans_priv.h"
17 : #include "xfs_trace.h"
18 : #include "xfs_log.h"
19 : #include "xfs_log_priv.h"
20 : #include "xfs_log_recover.h"
21 : #include "xfs_error.h"
22 : #include "xfs_inode.h"
23 : #include "xfs_dir2.h"
24 : #include "xfs_quota.h"
25 :
26 : /*
27 : * This is the number of entries in the l_buf_cancel_table used during
28 : * recovery.
29 : */
30 : #define XLOG_BC_TABLE_SIZE 64
31 :
32 : #define XLOG_BUF_CANCEL_BUCKET(log, blkno) \
33 : ((log)->l_buf_cancel_table + ((uint64_t)blkno % XLOG_BC_TABLE_SIZE))
34 :
35 : /*
36 : * This structure is used during recovery to record the buf log items which
37 : * have been canceled and should not be replayed.
38 : */
39 : struct xfs_buf_cancel {
40 : xfs_daddr_t bc_blkno;
41 : uint bc_len;
42 : int bc_refcount;
43 : struct list_head bc_list;
44 : };
45 :
46 : static struct xfs_buf_cancel *
47 57740163 : xlog_find_buffer_cancelled(
48 : struct xlog *log,
49 : xfs_daddr_t blkno,
50 : uint len)
51 : {
52 57740163 : struct list_head *bucket;
53 57740163 : struct xfs_buf_cancel *bcp;
54 :
55 57740163 : if (!log->l_buf_cancel_table)
56 : return NULL;
57 :
58 57740163 : bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
59 327227622 : list_for_each_entry(bcp, bucket, bc_list) {
60 271229275 : if (bcp->bc_blkno == blkno && bcp->bc_len == len)
61 1741816 : return bcp;
62 : }
63 :
64 : return NULL;
65 : }
66 :
67 : static bool
68 299031 : xlog_add_buffer_cancelled(
69 : struct xlog *log,
70 : xfs_daddr_t blkno,
71 : uint len)
72 : {
73 299031 : struct xfs_buf_cancel *bcp;
74 :
75 : /*
76 : * If we find an existing cancel record, this indicates that the buffer
77 : * was cancelled multiple times. To ensure that during pass 2 we keep
78 : * the record in the table until we reach its last occurrence in the
79 : * log, a reference count is kept to tell how many times we expect to
80 : * see this record during the second pass.
81 : */
82 299031 : bcp = xlog_find_buffer_cancelled(log, blkno, len);
83 299031 : if (bcp) {
84 44869 : bcp->bc_refcount++;
85 44869 : return false;
86 : }
87 :
88 254162 : bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
89 254162 : bcp->bc_blkno = blkno;
90 254162 : bcp->bc_len = len;
91 254162 : bcp->bc_refcount = 1;
92 254162 : list_add_tail(&bcp->bc_list, XLOG_BUF_CANCEL_BUCKET(log, blkno));
93 254162 : return true;
94 : }
95 :
96 : /*
97 : * Check if there is and entry for blkno, len in the buffer cancel record table.
98 : */
99 : bool
100 40892380 : xlog_is_buffer_cancelled(
101 : struct xlog *log,
102 : xfs_daddr_t blkno,
103 : uint len)
104 : {
105 40892380 : return xlog_find_buffer_cancelled(log, blkno, len) != NULL;
106 : }
107 :
108 : /*
109 : * Check if there is and entry for blkno, len in the buffer cancel record table,
110 : * and decremented the reference count on it if there is one.
111 : *
112 : * Remove the cancel record once the refcount hits zero, so that if the same
113 : * buffer is re-used again after its last cancellation we actually replay the
114 : * changes made at that point.
115 : */
116 : static bool
117 299031 : xlog_put_buffer_cancelled(
118 : struct xlog *log,
119 : xfs_daddr_t blkno,
120 : uint len)
121 : {
122 299031 : struct xfs_buf_cancel *bcp;
123 :
124 299031 : bcp = xlog_find_buffer_cancelled(log, blkno, len);
125 299031 : if (!bcp) {
126 0 : ASSERT(0);
127 0 : return false;
128 : }
129 :
130 299031 : if (--bcp->bc_refcount == 0) {
131 254162 : list_del(&bcp->bc_list);
132 254162 : kmem_free(bcp);
133 : }
134 : return true;
135 : }
136 :
137 : /* log buffer item recovery */
138 :
139 : /*
140 : * Sort buffer items for log recovery. Most buffer items should end up on the
141 : * buffer list and are recovered first, with the following exceptions:
142 : *
143 : * 1. XFS_BLF_CANCEL buffers must be processed last because some log items
144 : * might depend on the incor ecancellation record, and replaying a cancelled
145 : * buffer item can remove the incore record.
146 : *
147 : * 2. XFS_BLF_INODE_BUF buffers are handled after most regular items so that
148 : * we replay di_next_unlinked only after flushing the inode 'free' state
149 : * to the inode buffer.
150 : *
151 : * See xlog_recover_reorder_trans for more details.
152 : */
153 : STATIC enum xlog_recover_reorder
154 33097504 : xlog_recover_buf_reorder(
155 : struct xlog_recover_item *item)
156 : {
157 33097504 : struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
158 :
159 33097504 : if (buf_f->blf_flags & XFS_BLF_CANCEL)
160 : return XLOG_REORDER_CANCEL_LIST;
161 32499442 : if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
162 66694 : return XLOG_REORDER_INODE_BUFFER_LIST;
163 : return XLOG_REORDER_BUFFER_LIST;
164 : }
165 :
166 : STATIC void
167 16548752 : xlog_recover_buf_ra_pass2(
168 : struct xlog *log,
169 : struct xlog_recover_item *item)
170 : {
171 16548752 : struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
172 :
173 16548752 : xlog_buf_readahead(log, buf_f->blf_blkno, buf_f->blf_len, NULL);
174 16548752 : }
175 :
176 : /*
177 : * Build up the table of buf cancel records so that we don't replay cancelled
178 : * data in the second pass.
179 : */
180 : static int
181 16548752 : xlog_recover_buf_commit_pass1(
182 : struct xlog *log,
183 : struct xlog_recover_item *item)
184 : {
185 16548752 : struct xfs_buf_log_format *bf = item->ri_buf[0].i_addr;
186 :
187 16548752 : if (!xfs_buf_log_check_iovec(&item->ri_buf[0])) {
188 0 : xfs_err(log->l_mp, "bad buffer log item size (%d)",
189 : item->ri_buf[0].i_len);
190 0 : return -EFSCORRUPTED;
191 : }
192 :
193 16548752 : if (!(bf->blf_flags & XFS_BLF_CANCEL))
194 16249721 : trace_xfs_log_recover_buf_not_cancel(log, bf);
195 299031 : else if (xlog_add_buffer_cancelled(log, bf->blf_blkno, bf->blf_len))
196 254162 : trace_xfs_log_recover_buf_cancel_add(log, bf);
197 : else
198 44869 : trace_xfs_log_recover_buf_cancel_ref_inc(log, bf);
199 : return 0;
200 : }
201 :
202 : /*
203 : * Validate the recovered buffer is of the correct type and attach the
204 : * appropriate buffer operations to them for writeback. Magic numbers are in a
205 : * few places:
206 : * the first 16 bits of the buffer (inode buffer, dquot buffer),
207 : * the first 32 bits of the buffer (most blocks),
208 : * inside a struct xfs_da_blkinfo at the start of the buffer.
209 : */
210 : static void
211 15914350 : xlog_recover_validate_buf_type(
212 : struct xfs_mount *mp,
213 : struct xfs_buf *bp,
214 : struct xfs_buf_log_format *buf_f,
215 : xfs_lsn_t current_lsn)
216 : {
217 15914350 : struct xfs_da_blkinfo *info = bp->b_addr;
218 15914350 : uint32_t magic32;
219 15914350 : uint16_t magic16;
220 15914350 : uint16_t magicda;
221 15914350 : char *warnmsg = NULL;
222 :
223 : /*
224 : * We can only do post recovery validation on items on CRC enabled
225 : * fielsystems as we need to know when the buffer was written to be able
226 : * to determine if we should have replayed the item. If we replay old
227 : * metadata over a newer buffer, then it will enter a temporarily
228 : * inconsistent state resulting in verification failures. Hence for now
229 : * just avoid the verification stage for non-crc filesystems
230 : */
231 15914350 : if (!xfs_has_crc(mp))
232 : return;
233 :
234 15914350 : magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
235 15914350 : magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
236 15914350 : magicda = be16_to_cpu(info->magic);
237 15914350 : switch (xfs_blft_from_flags(buf_f)) {
238 13067278 : case XFS_BLFT_BTREE_BUF:
239 13067278 : switch (magic32) {
240 1639969 : case XFS_ABTB_CRC_MAGIC:
241 : case XFS_ABTB_MAGIC:
242 1639969 : bp->b_ops = &xfs_bnobt_buf_ops;
243 1639969 : break;
244 2581890 : case XFS_ABTC_CRC_MAGIC:
245 : case XFS_ABTC_MAGIC:
246 2581890 : bp->b_ops = &xfs_cntbt_buf_ops;
247 2581890 : break;
248 400015 : case XFS_IBT_CRC_MAGIC:
249 : case XFS_IBT_MAGIC:
250 400015 : bp->b_ops = &xfs_inobt_buf_ops;
251 400015 : break;
252 396403 : case XFS_FIBT_CRC_MAGIC:
253 : case XFS_FIBT_MAGIC:
254 396403 : bp->b_ops = &xfs_finobt_buf_ops;
255 396403 : break;
256 1030623 : case XFS_BMAP_CRC_MAGIC:
257 : case XFS_BMAP_MAGIC:
258 1030623 : bp->b_ops = &xfs_bmbt_buf_ops;
259 1030623 : break;
260 6428351 : case XFS_RMAP_CRC_MAGIC:
261 6428351 : bp->b_ops = &xfs_rmapbt_buf_ops;
262 6428351 : break;
263 590027 : case XFS_REFC_CRC_MAGIC:
264 590027 : bp->b_ops = &xfs_refcountbt_buf_ops;
265 590027 : break;
266 : default:
267 : warnmsg = "Bad btree block magic!";
268 : break;
269 : }
270 : break;
271 853806 : case XFS_BLFT_AGF_BUF:
272 853806 : if (magic32 != XFS_AGF_MAGIC) {
273 : warnmsg = "Bad AGF block magic!";
274 : break;
275 : }
276 853806 : bp->b_ops = &xfs_agf_buf_ops;
277 853806 : break;
278 18206 : case XFS_BLFT_AGFL_BUF:
279 18206 : if (magic32 != XFS_AGFL_MAGIC) {
280 : warnmsg = "Bad AGFL block magic!";
281 : break;
282 : }
283 18206 : bp->b_ops = &xfs_agfl_buf_ops;
284 18206 : break;
285 438325 : case XFS_BLFT_AGI_BUF:
286 438325 : if (magic32 != XFS_AGI_MAGIC) {
287 : warnmsg = "Bad AGI block magic!";
288 : break;
289 : }
290 438325 : bp->b_ops = &xfs_agi_buf_ops;
291 438325 : break;
292 336079 : case XFS_BLFT_UDQUOT_BUF:
293 : case XFS_BLFT_PDQUOT_BUF:
294 : case XFS_BLFT_GDQUOT_BUF:
295 : #ifdef CONFIG_XFS_QUOTA
296 336079 : if (magic16 != XFS_DQUOT_MAGIC) {
297 : warnmsg = "Bad DQUOT block magic!";
298 : break;
299 : }
300 336079 : bp->b_ops = &xfs_dquot_buf_ops;
301 : #else
302 : xfs_alert(mp,
303 : "Trying to recover dquots without QUOTA support built in!");
304 : ASSERT(0);
305 : #endif
306 336079 : break;
307 0 : case XFS_BLFT_DINO_BUF:
308 0 : if (magic16 != XFS_DINODE_MAGIC) {
309 : warnmsg = "Bad INODE block magic!";
310 : break;
311 : }
312 0 : bp->b_ops = &xfs_inode_buf_ops;
313 0 : break;
314 8937 : case XFS_BLFT_SYMLINK_BUF:
315 8937 : if (magic32 != XFS_SYMLINK_MAGIC) {
316 : warnmsg = "Bad symlink block magic!";
317 : break;
318 : }
319 8937 : bp->b_ops = &xfs_symlink_buf_ops;
320 8937 : break;
321 14671 : case XFS_BLFT_DIR_BLOCK_BUF:
322 14671 : if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
323 14671 : magic32 != XFS_DIR3_BLOCK_MAGIC) {
324 : warnmsg = "Bad dir block magic!";
325 : break;
326 : }
327 14671 : bp->b_ops = &xfs_dir3_block_buf_ops;
328 14671 : break;
329 531708 : case XFS_BLFT_DIR_DATA_BUF:
330 531708 : if (magic32 != XFS_DIR2_DATA_MAGIC &&
331 531708 : magic32 != XFS_DIR3_DATA_MAGIC) {
332 : warnmsg = "Bad dir data magic!";
333 : break;
334 : }
335 531708 : bp->b_ops = &xfs_dir3_data_buf_ops;
336 531708 : break;
337 127282 : case XFS_BLFT_DIR_FREE_BUF:
338 127282 : if (magic32 != XFS_DIR2_FREE_MAGIC &&
339 127282 : magic32 != XFS_DIR3_FREE_MAGIC) {
340 : warnmsg = "Bad dir3 free magic!";
341 : break;
342 : }
343 127282 : bp->b_ops = &xfs_dir3_free_buf_ops;
344 127282 : break;
345 61250 : case XFS_BLFT_DIR_LEAF1_BUF:
346 61250 : if (magicda != XFS_DIR2_LEAF1_MAGIC &&
347 61250 : magicda != XFS_DIR3_LEAF1_MAGIC) {
348 : warnmsg = "Bad dir leaf1 magic!";
349 : break;
350 : }
351 61250 : bp->b_ops = &xfs_dir3_leaf1_buf_ops;
352 61250 : break;
353 391270 : case XFS_BLFT_DIR_LEAFN_BUF:
354 391270 : if (magicda != XFS_DIR2_LEAFN_MAGIC &&
355 391270 : magicda != XFS_DIR3_LEAFN_MAGIC) {
356 : warnmsg = "Bad dir leafn magic!";
357 : break;
358 : }
359 391270 : bp->b_ops = &xfs_dir3_leafn_buf_ops;
360 391270 : break;
361 5418 : case XFS_BLFT_DA_NODE_BUF:
362 5418 : if (magicda != XFS_DA_NODE_MAGIC &&
363 5418 : magicda != XFS_DA3_NODE_MAGIC) {
364 : warnmsg = "Bad da node magic!";
365 : break;
366 : }
367 5418 : bp->b_ops = &xfs_da3_node_buf_ops;
368 5418 : break;
369 20247 : case XFS_BLFT_ATTR_LEAF_BUF:
370 20247 : if (magicda != XFS_ATTR_LEAF_MAGIC &&
371 20247 : magicda != XFS_ATTR3_LEAF_MAGIC) {
372 : warnmsg = "Bad attr leaf magic!";
373 : break;
374 : }
375 20247 : bp->b_ops = &xfs_attr3_leaf_buf_ops;
376 20247 : break;
377 0 : case XFS_BLFT_ATTR_RMT_BUF:
378 0 : if (magic32 != XFS_ATTR3_RMT_MAGIC) {
379 : warnmsg = "Bad attr remote magic!";
380 : break;
381 : }
382 0 : bp->b_ops = &xfs_attr3_rmt_buf_ops;
383 0 : break;
384 11165 : case XFS_BLFT_SB_BUF:
385 11165 : if (magic32 != XFS_SB_MAGIC) {
386 : warnmsg = "Bad SB block magic!";
387 : break;
388 : }
389 11165 : bp->b_ops = &xfs_sb_buf_ops;
390 11165 : break;
391 : #ifdef CONFIG_XFS_RT
392 28708 : case XFS_BLFT_RTBITMAP_BUF:
393 : case XFS_BLFT_RTSUMMARY_BUF:
394 : /* no magic numbers for verification of RT buffers */
395 28708 : bp->b_ops = &xfs_rtbuf_ops;
396 28708 : break;
397 : #endif /* CONFIG_XFS_RT */
398 : default:
399 0 : xfs_warn(mp, "Unknown buffer type %d!",
400 : xfs_blft_from_flags(buf_f));
401 0 : break;
402 : }
403 :
404 : /*
405 : * Nothing else to do in the case of a NULL current LSN as this means
406 : * the buffer is more recent than the change in the log and will be
407 : * skipped.
408 : */
409 15914350 : if (current_lsn == NULLCOMMITLSN)
410 : return;
411 :
412 15366104 : if (warnmsg) {
413 0 : xfs_warn(mp, warnmsg);
414 0 : ASSERT(0);
415 : }
416 :
417 : /*
418 : * We must update the metadata LSN of the buffer as it is written out to
419 : * ensure that older transactions never replay over this one and corrupt
420 : * the buffer. This can occur if log recovery is interrupted at some
421 : * point after the current transaction completes, at which point a
422 : * subsequent mount starts recovery from the beginning.
423 : *
424 : * Write verifiers update the metadata LSN from log items attached to
425 : * the buffer. Therefore, initialize a bli purely to carry the LSN to
426 : * the verifier.
427 : */
428 15366104 : if (bp->b_ops) {
429 15366104 : struct xfs_buf_log_item *bip;
430 :
431 15366104 : bp->b_flags |= _XBF_LOGRECOVERY;
432 15366104 : xfs_buf_item_init(bp, mp);
433 15366104 : bip = bp->b_log_item;
434 15366104 : bip->bli_item.li_lsn = current_lsn;
435 : }
436 : }
437 :
438 : /*
439 : * Perform a 'normal' buffer recovery. Each logged region of the
440 : * buffer should be copied over the corresponding region in the
441 : * given buffer. The bitmap in the buf log format structure indicates
442 : * where to place the logged data.
443 : */
444 : STATIC void
445 15702183 : xlog_recover_do_reg_buffer(
446 : struct xfs_mount *mp,
447 : struct xlog_recover_item *item,
448 : struct xfs_buf *bp,
449 : struct xfs_buf_log_format *buf_f,
450 : xfs_lsn_t current_lsn)
451 : {
452 15702183 : int i;
453 15702183 : int bit;
454 15702183 : int nbits;
455 15702183 : xfs_failaddr_t fa;
456 15702183 : const size_t size_disk_dquot = sizeof(struct xfs_disk_dquot);
457 :
458 15702183 : trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
459 :
460 15702183 : bit = 0;
461 15702183 : i = 1; /* 0 is the buf format structure */
462 63916493 : while (1) {
463 39809338 : bit = xfs_next_bit(buf_f->blf_data_map,
464 : buf_f->blf_map_size, bit);
465 39809338 : if (bit == -1)
466 : break;
467 24107155 : nbits = xfs_contig_bits(buf_f->blf_data_map,
468 : buf_f->blf_map_size, bit);
469 24107155 : ASSERT(nbits > 0);
470 24107155 : ASSERT(item->ri_buf[i].i_addr != NULL);
471 24107155 : ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
472 24107155 : ASSERT(BBTOB(bp->b_length) >=
473 : ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
474 :
475 : /*
476 : * The dirty regions logged in the buffer, even though
477 : * contiguous, may span multiple chunks. This is because the
478 : * dirty region may span a physical page boundary in a buffer
479 : * and hence be split into two separate vectors for writing into
480 : * the log. Hence we need to trim nbits back to the length of
481 : * the current region being copied out of the log.
482 : */
483 24107155 : if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
484 0 : nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
485 :
486 : /*
487 : * Do a sanity check if this is a dquot buffer. Just checking
488 : * the first dquot in the buffer should do. XXXThis is
489 : * probably a good thing to do for other buf types also.
490 : */
491 24107155 : fa = NULL;
492 24107155 : if (buf_f->blf_flags &
493 : (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
494 336079 : if (item->ri_buf[i].i_addr == NULL) {
495 0 : xfs_alert(mp,
496 : "XFS: NULL dquot in %s.", __func__);
497 0 : goto next;
498 : }
499 336079 : if (item->ri_buf[i].i_len < size_disk_dquot) {
500 0 : xfs_alert(mp,
501 : "XFS: dquot too small (%d) in %s.",
502 : item->ri_buf[i].i_len, __func__);
503 0 : goto next;
504 : }
505 336079 : fa = xfs_dquot_verify(mp, item->ri_buf[i].i_addr, -1);
506 336079 : if (fa) {
507 0 : xfs_alert(mp,
508 : "dquot corrupt at %pS trying to replay into block 0x%llx",
509 : fa, xfs_buf_daddr(bp));
510 0 : goto next;
511 : }
512 : }
513 :
514 48214310 : memcpy(xfs_buf_offset(bp,
515 : (uint)bit << XFS_BLF_SHIFT), /* dest */
516 : item->ri_buf[i].i_addr, /* source */
517 : nbits<<XFS_BLF_SHIFT); /* length */
518 24107155 : next:
519 24107155 : i++;
520 24107155 : bit += nbits;
521 : }
522 :
523 : /* Shouldn't be any more regions */
524 15702183 : ASSERT(i == item->ri_total);
525 :
526 15702183 : xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
527 15702183 : }
528 :
529 : /*
530 : * Perform a dquot buffer recovery.
531 : * Simple algorithm: if we have found a QUOTAOFF log item of the same type
532 : * (ie. USR or GRP), then just toss this buffer away; don't recover it.
533 : * Else, treat it as a regular buffer and do recovery.
534 : *
535 : * Return false if the buffer was tossed and true if we recovered the buffer to
536 : * indicate to the caller if the buffer needs writing.
537 : */
538 : STATIC bool
539 336079 : xlog_recover_do_dquot_buffer(
540 : struct xfs_mount *mp,
541 : struct xlog *log,
542 : struct xlog_recover_item *item,
543 : struct xfs_buf *bp,
544 : struct xfs_buf_log_format *buf_f)
545 : {
546 336079 : uint type;
547 :
548 336079 : trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
549 :
550 : /*
551 : * Filesystems are required to send in quota flags at mount time.
552 : */
553 336079 : if (!mp->m_qflags)
554 : return false;
555 :
556 336079 : type = 0;
557 336079 : if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
558 166547 : type |= XFS_DQTYPE_USER;
559 336079 : if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
560 2856 : type |= XFS_DQTYPE_PROJ;
561 336079 : if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
562 166676 : type |= XFS_DQTYPE_GROUP;
563 : /*
564 : * This type of quotas was turned off, so ignore this buffer
565 : */
566 336079 : if (log->l_quotaoffs_flag & type)
567 : return false;
568 :
569 336079 : xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
570 336079 : return true;
571 : }
572 :
573 : /*
574 : * Perform recovery for a buffer full of inodes. In these buffers, the only
575 : * data which should be recovered is that which corresponds to the
576 : * di_next_unlinked pointers in the on disk inode structures. The rest of the
577 : * data for the inodes is always logged through the inodes themselves rather
578 : * than the inode buffer and is recovered in xlog_recover_inode_pass2().
579 : *
580 : * The only time when buffers full of inodes are fully recovered is when the
581 : * buffer is full of newly allocated inodes. In this case the buffer will
582 : * not be marked as an inode buffer and so will be sent to
583 : * xlog_recover_do_reg_buffer() below during recovery.
584 : */
585 : STATIC int
586 33317 : xlog_recover_do_inode_buffer(
587 : struct xfs_mount *mp,
588 : struct xlog_recover_item *item,
589 : struct xfs_buf *bp,
590 : struct xfs_buf_log_format *buf_f)
591 : {
592 33317 : int i;
593 33317 : int item_index = 0;
594 33317 : int bit = 0;
595 33317 : int nbits = 0;
596 33317 : int reg_buf_offset = 0;
597 33317 : int reg_buf_bytes = 0;
598 33317 : int next_unlinked_offset;
599 33317 : int inodes_per_buf;
600 33317 : xfs_agino_t *logged_nextp;
601 33317 : xfs_agino_t *buffer_nextp;
602 :
603 33317 : trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
604 :
605 : /*
606 : * Post recovery validation only works properly on CRC enabled
607 : * filesystems.
608 : */
609 33317 : if (xfs_has_crc(mp))
610 33317 : bp->b_ops = &xfs_inode_buf_ops;
611 :
612 33317 : inodes_per_buf = BBTOB(bp->b_length) >> mp->m_sb.sb_inodelog;
613 1044966 : for (i = 0; i < inodes_per_buf; i++) {
614 1016665 : next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
615 : offsetof(struct xfs_dinode, di_next_unlinked);
616 :
617 1016665 : while (next_unlinked_offset >=
618 1924894 : (reg_buf_offset + reg_buf_bytes)) {
619 : /*
620 : * The next di_next_unlinked field is beyond
621 : * the current logged region. Find the next
622 : * logged region that contains or is beyond
623 : * the current di_next_unlinked field.
624 : */
625 913245 : bit += nbits;
626 913245 : bit = xfs_next_bit(buf_f->blf_data_map,
627 : buf_f->blf_map_size, bit);
628 :
629 : /*
630 : * If there are no more logged regions in the
631 : * buffer, then we're done.
632 : */
633 913245 : if (bit == -1)
634 : return 0;
635 :
636 908229 : nbits = xfs_contig_bits(buf_f->blf_data_map,
637 : buf_f->blf_map_size, bit);
638 908229 : ASSERT(nbits > 0);
639 908229 : reg_buf_offset = bit << XFS_BLF_SHIFT;
640 908229 : reg_buf_bytes = nbits << XFS_BLF_SHIFT;
641 908229 : item_index++;
642 : }
643 :
644 : /*
645 : * If the current logged region starts after the current
646 : * di_next_unlinked field, then move on to the next
647 : * di_next_unlinked field.
648 : */
649 1011649 : if (next_unlinked_offset < reg_buf_offset)
650 103420 : continue;
651 :
652 908229 : ASSERT(item->ri_buf[item_index].i_addr != NULL);
653 908229 : ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
654 908229 : ASSERT((reg_buf_offset + reg_buf_bytes) <= BBTOB(bp->b_length));
655 :
656 : /*
657 : * The current logged region contains a copy of the
658 : * current di_next_unlinked field. Extract its value
659 : * and copy it to the buffer copy.
660 : */
661 908229 : logged_nextp = item->ri_buf[item_index].i_addr +
662 908229 : next_unlinked_offset - reg_buf_offset;
663 908229 : if (XFS_IS_CORRUPT(mp, *logged_nextp == 0)) {
664 0 : xfs_alert(mp,
665 : "Bad inode buffer log record (ptr = "PTR_FMT", bp = "PTR_FMT"). "
666 : "Trying to replay bad (0) inode di_next_unlinked field.",
667 : item, bp);
668 0 : return -EFSCORRUPTED;
669 : }
670 :
671 908229 : buffer_nextp = xfs_buf_offset(bp, next_unlinked_offset);
672 908229 : *buffer_nextp = *logged_nextp;
673 :
674 : /*
675 : * If necessary, recalculate the CRC in the on-disk inode. We
676 : * have to leave the inode in a consistent state for whoever
677 : * reads it next....
678 : */
679 908229 : xfs_dinode_calc_crc(mp,
680 908229 : xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
681 :
682 : }
683 :
684 : return 0;
685 : }
686 :
687 : /*
688 : * V5 filesystems know the age of the buffer on disk being recovered. We can
689 : * have newer objects on disk than we are replaying, and so for these cases we
690 : * don't want to replay the current change as that will make the buffer contents
691 : * temporarily invalid on disk.
692 : *
693 : * The magic number might not match the buffer type we are going to recover
694 : * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
695 : * extract the LSN of the existing object in the buffer based on it's current
696 : * magic number. If we don't recognise the magic number in the buffer, then
697 : * return a LSN of -1 so that the caller knows it was an unrecognised block and
698 : * so can recover the buffer.
699 : *
700 : * Note: we cannot rely solely on magic number matches to determine that the
701 : * buffer has a valid LSN - we also need to verify that it belongs to this
702 : * filesystem, so we need to extract the object's LSN and compare it to that
703 : * which we read from the superblock. If the UUIDs don't match, then we've got a
704 : * stale metadata block from an old filesystem instance that we need to recover
705 : * over the top of.
706 : */
707 : static xfs_lsn_t
708 15947667 : xlog_recover_get_buf_lsn(
709 : struct xfs_mount *mp,
710 : struct xfs_buf *bp,
711 : struct xfs_buf_log_format *buf_f)
712 : {
713 15947667 : uint32_t magic32;
714 15947667 : uint16_t magic16;
715 15947667 : uint16_t magicda;
716 15947667 : void *blk = bp->b_addr;
717 15947667 : uuid_t *uuid;
718 15947667 : xfs_lsn_t lsn = -1;
719 15947667 : uint16_t blft;
720 :
721 : /* v4 filesystems always recover immediately */
722 15947667 : if (!xfs_has_crc(mp))
723 0 : goto recover_immediately;
724 :
725 : /*
726 : * realtime bitmap and summary file blocks do not have magic numbers or
727 : * UUIDs, so we must recover them immediately.
728 : */
729 15947667 : blft = xfs_blft_from_flags(buf_f);
730 15947667 : if (blft == XFS_BLFT_RTBITMAP_BUF || blft == XFS_BLFT_RTSUMMARY_BUF)
731 28708 : goto recover_immediately;
732 :
733 15918959 : magic32 = be32_to_cpu(*(__be32 *)blk);
734 15918959 : switch (magic32) {
735 12029349 : case XFS_ABTB_CRC_MAGIC:
736 : case XFS_ABTC_CRC_MAGIC:
737 : case XFS_ABTB_MAGIC:
738 : case XFS_ABTC_MAGIC:
739 : case XFS_RMAP_CRC_MAGIC:
740 : case XFS_REFC_CRC_MAGIC:
741 : case XFS_FIBT_CRC_MAGIC:
742 : case XFS_FIBT_MAGIC:
743 : case XFS_IBT_CRC_MAGIC:
744 : case XFS_IBT_MAGIC: {
745 12029349 : struct xfs_btree_block *btb = blk;
746 :
747 12029349 : lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
748 12029349 : uuid = &btb->bb_u.s.bb_uuid;
749 12029349 : break;
750 : }
751 1013260 : case XFS_BMAP_CRC_MAGIC:
752 : case XFS_BMAP_MAGIC: {
753 1013260 : struct xfs_btree_block *btb = blk;
754 :
755 1013260 : lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
756 1013260 : uuid = &btb->bb_u.l.bb_uuid;
757 1013260 : break;
758 : }
759 853824 : case XFS_AGF_MAGIC:
760 853824 : lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
761 853824 : uuid = &((struct xfs_agf *)blk)->agf_uuid;
762 853824 : break;
763 18208 : case XFS_AGFL_MAGIC:
764 18208 : lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
765 18208 : uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
766 18208 : break;
767 438346 : case XFS_AGI_MAGIC:
768 438346 : lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
769 438346 : uuid = &((struct xfs_agi *)blk)->agi_uuid;
770 438346 : break;
771 971 : case XFS_SYMLINK_MAGIC:
772 971 : lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
773 971 : uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
774 971 : break;
775 672268 : case XFS_DIR3_BLOCK_MAGIC:
776 : case XFS_DIR3_DATA_MAGIC:
777 : case XFS_DIR3_FREE_MAGIC:
778 672268 : lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
779 672268 : uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
780 672268 : break;
781 92 : case XFS_ATTR3_RMT_MAGIC:
782 : /*
783 : * Remote attr blocks are written synchronously, rather than
784 : * being logged. That means they do not contain a valid LSN
785 : * (i.e. transactionally ordered) in them, and hence any time we
786 : * see a buffer to replay over the top of a remote attribute
787 : * block we should simply do so.
788 : */
789 92 : goto recover_immediately;
790 11167 : case XFS_SB_MAGIC:
791 : /*
792 : * superblock uuids are magic. We may or may not have a
793 : * sb_meta_uuid on disk, but it will be set in the in-core
794 : * superblock. We set the uuid pointer for verification
795 : * according to the superblock feature mask to ensure we check
796 : * the relevant UUID in the superblock.
797 : */
798 11167 : lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
799 11167 : if (xfs_has_metauuid(mp))
800 0 : uuid = &((struct xfs_dsb *)blk)->sb_meta_uuid;
801 : else
802 11167 : uuid = &((struct xfs_dsb *)blk)->sb_uuid;
803 : break;
804 : default:
805 : break;
806 : }
807 :
808 15037393 : if (lsn != (xfs_lsn_t)-1) {
809 15037267 : if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
810 2090 : goto recover_immediately;
811 : return lsn;
812 : }
813 :
814 881600 : magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
815 881600 : switch (magicda) {
816 469909 : case XFS_DIR3_LEAF1_MAGIC:
817 : case XFS_DIR3_LEAFN_MAGIC:
818 : case XFS_ATTR3_LEAF_MAGIC:
819 : case XFS_DA3_NODE_MAGIC:
820 469909 : lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
821 469909 : uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
822 469909 : break;
823 : default:
824 : break;
825 : }
826 :
827 881600 : if (lsn != (xfs_lsn_t)-1) {
828 469909 : if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
829 471 : goto recover_immediately;
830 : return lsn;
831 : }
832 :
833 : /*
834 : * We do individual object checks on dquot and inode buffers as they
835 : * have their own individual LSN records. Also, we could have a stale
836 : * buffer here, so we have to at least recognise these buffer types.
837 : *
838 : * A notd complexity here is inode unlinked list processing - it logs
839 : * the inode directly in the buffer, but we don't know which inodes have
840 : * been modified, and there is no global buffer LSN. Hence we need to
841 : * recover all inode buffer types immediately. This problem will be
842 : * fixed by logical logging of the unlinked list modifications.
843 : */
844 411691 : magic16 = be16_to_cpu(*(__be16 *)blk);
845 411691 : switch (magic16) {
846 44932 : case XFS_DQUOT_MAGIC:
847 : case XFS_DINODE_MAGIC:
848 44932 : goto recover_immediately;
849 : default:
850 : break;
851 : }
852 :
853 : /* unknown buffer contents, recover immediately */
854 :
855 : recover_immediately:
856 : return (xfs_lsn_t)-1;
857 :
858 : }
859 :
860 : /*
861 : * This routine replays a modification made to a buffer at runtime.
862 : * There are actually two types of buffer, regular and inode, which
863 : * are handled differently. Inode buffers are handled differently
864 : * in that we only recover a specific set of data from them, namely
865 : * the inode di_next_unlinked fields. This is because all other inode
866 : * data is actually logged via inode records and any data we replay
867 : * here which overlaps that may be stale.
868 : *
869 : * When meta-data buffers are freed at run time we log a buffer item
870 : * with the XFS_BLF_CANCEL bit set to indicate that previous copies
871 : * of the buffer in the log should not be replayed at recovery time.
872 : * This is so that if the blocks covered by the buffer are reused for
873 : * file data before we crash we don't end up replaying old, freed
874 : * meta-data into a user's file.
875 : *
876 : * To handle the cancellation of buffer log items, we make two passes
877 : * over the log during recovery. During the first we build a table of
878 : * those buffers which have been cancelled, and during the second we
879 : * only replay those buffers which do not have corresponding cancel
880 : * records in the table. See xlog_recover_buf_pass[1,2] above
881 : * for more details on the implementation of the table of cancel records.
882 : */
883 : STATIC int
884 16548752 : xlog_recover_buf_commit_pass2(
885 : struct xlog *log,
886 : struct list_head *buffer_list,
887 : struct xlog_recover_item *item,
888 : xfs_lsn_t current_lsn)
889 : {
890 16548752 : struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
891 16548752 : struct xfs_mount *mp = log->l_mp;
892 16548752 : struct xfs_buf *bp;
893 16548752 : int error;
894 16548752 : uint buf_flags;
895 16548752 : xfs_lsn_t lsn;
896 :
897 : /*
898 : * In this pass we only want to recover all the buffers which have
899 : * not been cancelled and are not cancellation buffers themselves.
900 : */
901 16548752 : if (buf_f->blf_flags & XFS_BLF_CANCEL) {
902 299031 : if (xlog_put_buffer_cancelled(log, buf_f->blf_blkno,
903 299031 : buf_f->blf_len))
904 299031 : goto cancelled;
905 : } else {
906 :
907 16249721 : if (xlog_is_buffer_cancelled(log, buf_f->blf_blkno,
908 16249721 : buf_f->blf_len))
909 302054 : goto cancelled;
910 : }
911 :
912 15947667 : trace_xfs_log_recover_buf_recover(log, buf_f);
913 :
914 15947667 : buf_flags = 0;
915 15947667 : if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
916 33317 : buf_flags |= XBF_UNMAPPED;
917 :
918 15947667 : error = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
919 : buf_flags, &bp, NULL);
920 15947667 : if (error)
921 : return error;
922 :
923 : /*
924 : * Recover the buffer only if we get an LSN from it and it's less than
925 : * the lsn of the transaction we are replaying.
926 : *
927 : * Note that we have to be extremely careful of readahead here.
928 : * Readahead does not attach verfiers to the buffers so if we don't
929 : * actually do any replay after readahead because of the LSN we found
930 : * in the buffer if more recent than that current transaction then we
931 : * need to attach the verifier directly. Failure to do so can lead to
932 : * future recovery actions (e.g. EFI and unlinked list recovery) can
933 : * operate on the buffers and they won't get the verifier attached. This
934 : * can lead to blocks on disk having the correct content but a stale
935 : * CRC.
936 : *
937 : * It is safe to assume these clean buffers are currently up to date.
938 : * If the buffer is dirtied by a later transaction being replayed, then
939 : * the verifier will be reset to match whatever recover turns that
940 : * buffer into.
941 : */
942 15947667 : lsn = xlog_recover_get_buf_lsn(mp, bp, buf_f);
943 15947667 : if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
944 212167 : trace_xfs_log_recover_buf_skip(log, buf_f);
945 212167 : xlog_recover_validate_buf_type(mp, bp, buf_f, NULLCOMMITLSN);
946 :
947 : /*
948 : * We're skipping replay of this buffer log item due to the log
949 : * item LSN being behind the ondisk buffer. Verify the buffer
950 : * contents since we aren't going to run the write verifier.
951 : */
952 212167 : if (bp->b_ops) {
953 212167 : bp->b_ops->verify_read(bp);
954 212167 : error = bp->b_error;
955 : }
956 212167 : goto out_release;
957 : }
958 :
959 15735500 : if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
960 33317 : error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
961 33317 : if (error)
962 0 : goto out_release;
963 15702183 : } else if (buf_f->blf_flags &
964 : (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
965 336079 : bool dirty;
966 :
967 336079 : dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
968 336079 : if (!dirty)
969 0 : goto out_release;
970 : } else {
971 15366104 : xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
972 : }
973 :
974 : /*
975 : * Perform delayed write on the buffer. Asynchronous writes will be
976 : * slower when taking into account all the buffers to be flushed.
977 : *
978 : * Also make sure that only inode buffers with good sizes stay in
979 : * the buffer cache. The kernel moves inodes in buffers of 1 block
980 : * or inode_cluster_size bytes, whichever is bigger. The inode
981 : * buffers in the log can be a different size if the log was generated
982 : * by an older kernel using unclustered inode buffers or a newer kernel
983 : * running with a different inode cluster size. Regardless, if
984 : * the inode buffer size isn't max(blocksize, inode_cluster_size)
985 : * for *our* value of inode_cluster_size, then we need to keep
986 : * the buffer out of the buffer cache so that the buffer won't
987 : * overlap with future reads of those inodes.
988 : */
989 31471000 : if (XFS_DINODE_MAGIC ==
990 15735500 : be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
991 33317 : (BBTOB(bp->b_length) != M_IGEO(log->l_mp)->inode_cluster_size)) {
992 0 : xfs_buf_stale(bp);
993 0 : error = xfs_bwrite(bp);
994 : } else {
995 15735500 : ASSERT(bp->b_mount == mp);
996 15735500 : bp->b_flags |= _XBF_LOGRECOVERY;
997 15735500 : xfs_buf_delwri_queue(bp, buffer_list);
998 : }
999 :
1000 15947667 : out_release:
1001 15947667 : xfs_buf_relse(bp);
1002 15947667 : return error;
1003 601085 : cancelled:
1004 601085 : trace_xfs_log_recover_buf_cancel(log, buf_f);
1005 601085 : return 0;
1006 : }
1007 :
1008 : const struct xlog_recover_item_ops xlog_buf_item_ops = {
1009 : .item_type = XFS_LI_BUF,
1010 : .reorder = xlog_recover_buf_reorder,
1011 : .ra_pass2 = xlog_recover_buf_ra_pass2,
1012 : .commit_pass1 = xlog_recover_buf_commit_pass1,
1013 : .commit_pass2 = xlog_recover_buf_commit_pass2,
1014 : };
1015 :
1016 : #ifdef DEBUG
1017 : void
1018 13723 : xlog_check_buf_cancel_table(
1019 : struct xlog *log)
1020 : {
1021 13723 : int i;
1022 :
1023 891995 : for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
1024 878272 : ASSERT(list_empty(&log->l_buf_cancel_table[i]));
1025 13723 : }
1026 : #endif
1027 :
1028 : int
1029 13723 : xlog_alloc_buf_cancel_table(
1030 : struct xlog *log)
1031 : {
1032 13723 : void *p;
1033 13723 : int i;
1034 :
1035 13723 : ASSERT(log->l_buf_cancel_table == NULL);
1036 :
1037 13723 : p = kmalloc_array(XLOG_BC_TABLE_SIZE, sizeof(struct list_head),
1038 : GFP_KERNEL);
1039 13723 : if (!p)
1040 : return -ENOMEM;
1041 :
1042 13723 : log->l_buf_cancel_table = p;
1043 891995 : for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
1044 878272 : INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
1045 :
1046 : return 0;
1047 : }
1048 :
1049 : void
1050 13723 : xlog_free_buf_cancel_table(
1051 : struct xlog *log)
1052 : {
1053 13723 : int i;
1054 :
1055 13723 : if (!log->l_buf_cancel_table)
1056 : return;
1057 :
1058 891995 : for (i = 0; i < XLOG_BC_TABLE_SIZE; i++) {
1059 : struct xfs_buf_cancel *bc;
1060 :
1061 878272 : while ((bc = list_first_entry_or_null(
1062 : &log->l_buf_cancel_table[i],
1063 : struct xfs_buf_cancel, bc_list))) {
1064 0 : list_del(&bc->bc_list);
1065 0 : kmem_free(bc);
1066 : }
1067 : }
1068 :
1069 13723 : kmem_free(log->l_buf_cancel_table);
1070 13723 : log->l_buf_cancel_table = NULL;
1071 : }
|