xfs_buf_item_recover.c 32 KB

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