xfs_buf_item_recover.c 30 KB

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