PageRenderTime 81ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xfs/xfs_log_recover.c

https://bitbucket.org/emiliolopez/linux
C | 5872 lines | 3674 code | 564 blank | 1634 comment | 694 complexity | 38acf4b57544ad66eb9765d057c186f1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_log.h"
  32. #include "xfs_log_priv.h"
  33. #include "xfs_log_recover.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_extfree_item.h"
  36. #include "xfs_trans_priv.h"
  37. #include "xfs_alloc.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_cksum.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_icache.h"
  43. #include "xfs_bmap_btree.h"
  44. #include "xfs_error.h"
  45. #include "xfs_dir2.h"
  46. #include "xfs_rmap_item.h"
  47. #include "xfs_buf_item.h"
  48. #include "xfs_refcount_item.h"
  49. #include "xfs_bmap_item.h"
  50. #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
  51. STATIC int
  52. xlog_find_zeroed(
  53. struct xlog *,
  54. xfs_daddr_t *);
  55. STATIC int
  56. xlog_clear_stale_blocks(
  57. struct xlog *,
  58. xfs_lsn_t);
  59. #if defined(DEBUG)
  60. STATIC void
  61. xlog_recover_check_summary(
  62. struct xlog *);
  63. #else
  64. #define xlog_recover_check_summary(log)
  65. #endif
  66. STATIC int
  67. xlog_do_recovery_pass(
  68. struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
  69. /*
  70. * This structure is used during recovery to record the buf log items which
  71. * have been canceled and should not be replayed.
  72. */
  73. struct xfs_buf_cancel {
  74. xfs_daddr_t bc_blkno;
  75. uint bc_len;
  76. int bc_refcount;
  77. struct list_head bc_list;
  78. };
  79. /*
  80. * Sector aligned buffer routines for buffer create/read/write/access
  81. */
  82. /*
  83. * Verify the log-relative block number and length in basic blocks are valid for
  84. * an operation involving the given XFS log buffer. Returns true if the fields
  85. * are valid, false otherwise.
  86. */
  87. static inline bool
  88. xlog_verify_bp(
  89. struct xlog *log,
  90. xfs_daddr_t blk_no,
  91. int bbcount)
  92. {
  93. if (blk_no < 0 || blk_no >= log->l_logBBsize)
  94. return false;
  95. if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
  96. return false;
  97. return true;
  98. }
  99. /*
  100. * Allocate a buffer to hold log data. The buffer needs to be able
  101. * to map to a range of nbblks basic blocks at any valid (basic
  102. * block) offset within the log.
  103. */
  104. STATIC xfs_buf_t *
  105. xlog_get_bp(
  106. struct xlog *log,
  107. int nbblks)
  108. {
  109. struct xfs_buf *bp;
  110. /*
  111. * Pass log block 0 since we don't have an addr yet, buffer will be
  112. * verified on read.
  113. */
  114. if (!xlog_verify_bp(log, 0, nbblks)) {
  115. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  116. nbblks);
  117. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  118. return NULL;
  119. }
  120. /*
  121. * We do log I/O in units of log sectors (a power-of-2
  122. * multiple of the basic block size), so we round up the
  123. * requested size to accommodate the basic blocks required
  124. * for complete log sectors.
  125. *
  126. * In addition, the buffer may be used for a non-sector-
  127. * aligned block offset, in which case an I/O of the
  128. * requested size could extend beyond the end of the
  129. * buffer. If the requested size is only 1 basic block it
  130. * will never straddle a sector boundary, so this won't be
  131. * an issue. Nor will this be a problem if the log I/O is
  132. * done in basic blocks (sector size 1). But otherwise we
  133. * extend the buffer by one extra log sector to ensure
  134. * there's space to accommodate this possibility.
  135. */
  136. if (nbblks > 1 && log->l_sectBBsize > 1)
  137. nbblks += log->l_sectBBsize;
  138. nbblks = round_up(nbblks, log->l_sectBBsize);
  139. bp = xfs_buf_get_uncached(log->l_mp->m_logdev_targp, nbblks, 0);
  140. if (bp)
  141. xfs_buf_unlock(bp);
  142. return bp;
  143. }
  144. STATIC void
  145. xlog_put_bp(
  146. xfs_buf_t *bp)
  147. {
  148. xfs_buf_free(bp);
  149. }
  150. /*
  151. * Return the address of the start of the given block number's data
  152. * in a log buffer. The buffer covers a log sector-aligned region.
  153. */
  154. STATIC char *
  155. xlog_align(
  156. struct xlog *log,
  157. xfs_daddr_t blk_no,
  158. int nbblks,
  159. struct xfs_buf *bp)
  160. {
  161. xfs_daddr_t offset = blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1);
  162. ASSERT(offset + nbblks <= bp->b_length);
  163. return bp->b_addr + BBTOB(offset);
  164. }
  165. /*
  166. * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
  167. */
  168. STATIC int
  169. xlog_bread_noalign(
  170. struct xlog *log,
  171. xfs_daddr_t blk_no,
  172. int nbblks,
  173. struct xfs_buf *bp)
  174. {
  175. int error;
  176. if (!xlog_verify_bp(log, blk_no, nbblks)) {
  177. xfs_warn(log->l_mp,
  178. "Invalid log block/length (0x%llx, 0x%x) for buffer",
  179. blk_no, nbblks);
  180. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  181. return -EFSCORRUPTED;
  182. }
  183. blk_no = round_down(blk_no, log->l_sectBBsize);
  184. nbblks = round_up(nbblks, log->l_sectBBsize);
  185. ASSERT(nbblks > 0);
  186. ASSERT(nbblks <= bp->b_length);
  187. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  188. bp->b_flags |= XBF_READ;
  189. bp->b_io_length = nbblks;
  190. bp->b_error = 0;
  191. error = xfs_buf_submit_wait(bp);
  192. if (error && !XFS_FORCED_SHUTDOWN(log->l_mp))
  193. xfs_buf_ioerror_alert(bp, __func__);
  194. return error;
  195. }
  196. STATIC int
  197. xlog_bread(
  198. struct xlog *log,
  199. xfs_daddr_t blk_no,
  200. int nbblks,
  201. struct xfs_buf *bp,
  202. char **offset)
  203. {
  204. int error;
  205. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  206. if (error)
  207. return error;
  208. *offset = xlog_align(log, blk_no, nbblks, bp);
  209. return 0;
  210. }
  211. /*
  212. * Read at an offset into the buffer. Returns with the buffer in it's original
  213. * state regardless of the result of the read.
  214. */
  215. STATIC int
  216. xlog_bread_offset(
  217. struct xlog *log,
  218. xfs_daddr_t blk_no, /* block to read from */
  219. int nbblks, /* blocks to read */
  220. struct xfs_buf *bp,
  221. char *offset)
  222. {
  223. char *orig_offset = bp->b_addr;
  224. int orig_len = BBTOB(bp->b_length);
  225. int error, error2;
  226. error = xfs_buf_associate_memory(bp, offset, BBTOB(nbblks));
  227. if (error)
  228. return error;
  229. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  230. /* must reset buffer pointer even on error */
  231. error2 = xfs_buf_associate_memory(bp, orig_offset, orig_len);
  232. if (error)
  233. return error;
  234. return error2;
  235. }
  236. /*
  237. * Write out the buffer at the given block for the given number of blocks.
  238. * The buffer is kept locked across the write and is returned locked.
  239. * This can only be used for synchronous log writes.
  240. */
  241. STATIC int
  242. xlog_bwrite(
  243. struct xlog *log,
  244. xfs_daddr_t blk_no,
  245. int nbblks,
  246. struct xfs_buf *bp)
  247. {
  248. int error;
  249. if (!xlog_verify_bp(log, blk_no, nbblks)) {
  250. xfs_warn(log->l_mp,
  251. "Invalid log block/length (0x%llx, 0x%x) for buffer",
  252. blk_no, nbblks);
  253. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  254. return -EFSCORRUPTED;
  255. }
  256. blk_no = round_down(blk_no, log->l_sectBBsize);
  257. nbblks = round_up(nbblks, log->l_sectBBsize);
  258. ASSERT(nbblks > 0);
  259. ASSERT(nbblks <= bp->b_length);
  260. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  261. xfs_buf_hold(bp);
  262. xfs_buf_lock(bp);
  263. bp->b_io_length = nbblks;
  264. bp->b_error = 0;
  265. error = xfs_bwrite(bp);
  266. if (error)
  267. xfs_buf_ioerror_alert(bp, __func__);
  268. xfs_buf_relse(bp);
  269. return error;
  270. }
  271. #ifdef DEBUG
  272. /*
  273. * dump debug superblock and log record information
  274. */
  275. STATIC void
  276. xlog_header_check_dump(
  277. xfs_mount_t *mp,
  278. xlog_rec_header_t *head)
  279. {
  280. xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
  281. __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
  282. xfs_debug(mp, " log : uuid = %pU, fmt = %d",
  283. &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
  284. }
  285. #else
  286. #define xlog_header_check_dump(mp, head)
  287. #endif
  288. /*
  289. * check log record header for recovery
  290. */
  291. STATIC int
  292. xlog_header_check_recover(
  293. xfs_mount_t *mp,
  294. xlog_rec_header_t *head)
  295. {
  296. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  297. /*
  298. * IRIX doesn't write the h_fmt field and leaves it zeroed
  299. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  300. * a dirty log created in IRIX.
  301. */
  302. if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
  303. xfs_warn(mp,
  304. "dirty log written in incompatible format - can't recover");
  305. xlog_header_check_dump(mp, head);
  306. XFS_ERROR_REPORT("xlog_header_check_recover(1)",
  307. XFS_ERRLEVEL_HIGH, mp);
  308. return -EFSCORRUPTED;
  309. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  310. xfs_warn(mp,
  311. "dirty log entry has mismatched uuid - can't recover");
  312. xlog_header_check_dump(mp, head);
  313. XFS_ERROR_REPORT("xlog_header_check_recover(2)",
  314. XFS_ERRLEVEL_HIGH, mp);
  315. return -EFSCORRUPTED;
  316. }
  317. return 0;
  318. }
  319. /*
  320. * read the head block of the log and check the header
  321. */
  322. STATIC int
  323. xlog_header_check_mount(
  324. xfs_mount_t *mp,
  325. xlog_rec_header_t *head)
  326. {
  327. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  328. if (uuid_is_null(&head->h_fs_uuid)) {
  329. /*
  330. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  331. * h_fs_uuid is null, we assume this log was last mounted
  332. * by IRIX and continue.
  333. */
  334. xfs_warn(mp, "null uuid in log - IRIX style log");
  335. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  336. xfs_warn(mp, "log has mismatched uuid - can't recover");
  337. xlog_header_check_dump(mp, head);
  338. XFS_ERROR_REPORT("xlog_header_check_mount",
  339. XFS_ERRLEVEL_HIGH, mp);
  340. return -EFSCORRUPTED;
  341. }
  342. return 0;
  343. }
  344. STATIC void
  345. xlog_recover_iodone(
  346. struct xfs_buf *bp)
  347. {
  348. if (bp->b_error) {
  349. /*
  350. * We're not going to bother about retrying
  351. * this during recovery. One strike!
  352. */
  353. if (!XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  354. xfs_buf_ioerror_alert(bp, __func__);
  355. xfs_force_shutdown(bp->b_target->bt_mount,
  356. SHUTDOWN_META_IO_ERROR);
  357. }
  358. }
  359. /*
  360. * On v5 supers, a bli could be attached to update the metadata LSN.
  361. * Clean it up.
  362. */
  363. if (bp->b_fspriv)
  364. xfs_buf_item_relse(bp);
  365. ASSERT(bp->b_fspriv == NULL);
  366. bp->b_iodone = NULL;
  367. xfs_buf_ioend(bp);
  368. }
  369. /*
  370. * This routine finds (to an approximation) the first block in the physical
  371. * log which contains the given cycle. It uses a binary search algorithm.
  372. * Note that the algorithm can not be perfect because the disk will not
  373. * necessarily be perfect.
  374. */
  375. STATIC int
  376. xlog_find_cycle_start(
  377. struct xlog *log,
  378. struct xfs_buf *bp,
  379. xfs_daddr_t first_blk,
  380. xfs_daddr_t *last_blk,
  381. uint cycle)
  382. {
  383. char *offset;
  384. xfs_daddr_t mid_blk;
  385. xfs_daddr_t end_blk;
  386. uint mid_cycle;
  387. int error;
  388. end_blk = *last_blk;
  389. mid_blk = BLK_AVG(first_blk, end_blk);
  390. while (mid_blk != first_blk && mid_blk != end_blk) {
  391. error = xlog_bread(log, mid_blk, 1, bp, &offset);
  392. if (error)
  393. return error;
  394. mid_cycle = xlog_get_cycle(offset);
  395. if (mid_cycle == cycle)
  396. end_blk = mid_blk; /* last_half_cycle == mid_cycle */
  397. else
  398. first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  399. mid_blk = BLK_AVG(first_blk, end_blk);
  400. }
  401. ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  402. (mid_blk == end_blk && mid_blk-1 == first_blk));
  403. *last_blk = end_blk;
  404. return 0;
  405. }
  406. /*
  407. * Check that a range of blocks does not contain stop_on_cycle_no.
  408. * Fill in *new_blk with the block offset where such a block is
  409. * found, or with -1 (an invalid block number) if there is no such
  410. * block in the range. The scan needs to occur from front to back
  411. * and the pointer into the region must be updated since a later
  412. * routine will need to perform another test.
  413. */
  414. STATIC int
  415. xlog_find_verify_cycle(
  416. struct xlog *log,
  417. xfs_daddr_t start_blk,
  418. int nbblks,
  419. uint stop_on_cycle_no,
  420. xfs_daddr_t *new_blk)
  421. {
  422. xfs_daddr_t i, j;
  423. uint cycle;
  424. xfs_buf_t *bp;
  425. xfs_daddr_t bufblks;
  426. char *buf = NULL;
  427. int error = 0;
  428. /*
  429. * Greedily allocate a buffer big enough to handle the full
  430. * range of basic blocks we'll be examining. If that fails,
  431. * try a smaller size. We need to be able to read at least
  432. * a log sector, or we're out of luck.
  433. */
  434. bufblks = 1 << ffs(nbblks);
  435. while (bufblks > log->l_logBBsize)
  436. bufblks >>= 1;
  437. while (!(bp = xlog_get_bp(log, bufblks))) {
  438. bufblks >>= 1;
  439. if (bufblks < log->l_sectBBsize)
  440. return -ENOMEM;
  441. }
  442. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  443. int bcount;
  444. bcount = min(bufblks, (start_blk + nbblks - i));
  445. error = xlog_bread(log, i, bcount, bp, &buf);
  446. if (error)
  447. goto out;
  448. for (j = 0; j < bcount; j++) {
  449. cycle = xlog_get_cycle(buf);
  450. if (cycle == stop_on_cycle_no) {
  451. *new_blk = i+j;
  452. goto out;
  453. }
  454. buf += BBSIZE;
  455. }
  456. }
  457. *new_blk = -1;
  458. out:
  459. xlog_put_bp(bp);
  460. return error;
  461. }
  462. /*
  463. * Potentially backup over partial log record write.
  464. *
  465. * In the typical case, last_blk is the number of the block directly after
  466. * a good log record. Therefore, we subtract one to get the block number
  467. * of the last block in the given buffer. extra_bblks contains the number
  468. * of blocks we would have read on a previous read. This happens when the
  469. * last log record is split over the end of the physical log.
  470. *
  471. * extra_bblks is the number of blocks potentially verified on a previous
  472. * call to this routine.
  473. */
  474. STATIC int
  475. xlog_find_verify_log_record(
  476. struct xlog *log,
  477. xfs_daddr_t start_blk,
  478. xfs_daddr_t *last_blk,
  479. int extra_bblks)
  480. {
  481. xfs_daddr_t i;
  482. xfs_buf_t *bp;
  483. char *offset = NULL;
  484. xlog_rec_header_t *head = NULL;
  485. int error = 0;
  486. int smallmem = 0;
  487. int num_blks = *last_blk - start_blk;
  488. int xhdrs;
  489. ASSERT(start_blk != 0 || *last_blk != start_blk);
  490. if (!(bp = xlog_get_bp(log, num_blks))) {
  491. if (!(bp = xlog_get_bp(log, 1)))
  492. return -ENOMEM;
  493. smallmem = 1;
  494. } else {
  495. error = xlog_bread(log, start_blk, num_blks, bp, &offset);
  496. if (error)
  497. goto out;
  498. offset += ((num_blks - 1) << BBSHIFT);
  499. }
  500. for (i = (*last_blk) - 1; i >= 0; i--) {
  501. if (i < start_blk) {
  502. /* valid log record not found */
  503. xfs_warn(log->l_mp,
  504. "Log inconsistent (didn't find previous header)");
  505. ASSERT(0);
  506. error = -EIO;
  507. goto out;
  508. }
  509. if (smallmem) {
  510. error = xlog_bread(log, i, 1, bp, &offset);
  511. if (error)
  512. goto out;
  513. }
  514. head = (xlog_rec_header_t *)offset;
  515. if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  516. break;
  517. if (!smallmem)
  518. offset -= BBSIZE;
  519. }
  520. /*
  521. * We hit the beginning of the physical log & still no header. Return
  522. * to caller. If caller can handle a return of -1, then this routine
  523. * will be called again for the end of the physical log.
  524. */
  525. if (i == -1) {
  526. error = 1;
  527. goto out;
  528. }
  529. /*
  530. * We have the final block of the good log (the first block
  531. * of the log record _before_ the head. So we check the uuid.
  532. */
  533. if ((error = xlog_header_check_mount(log->l_mp, head)))
  534. goto out;
  535. /*
  536. * We may have found a log record header before we expected one.
  537. * last_blk will be the 1st block # with a given cycle #. We may end
  538. * up reading an entire log record. In this case, we don't want to
  539. * reset last_blk. Only when last_blk points in the middle of a log
  540. * record do we update last_blk.
  541. */
  542. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  543. uint h_size = be32_to_cpu(head->h_size);
  544. xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
  545. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  546. xhdrs++;
  547. } else {
  548. xhdrs = 1;
  549. }
  550. if (*last_blk - i + extra_bblks !=
  551. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  552. *last_blk = i;
  553. out:
  554. xlog_put_bp(bp);
  555. return error;
  556. }
  557. /*
  558. * Head is defined to be the point of the log where the next log write
  559. * could go. This means that incomplete LR writes at the end are
  560. * eliminated when calculating the head. We aren't guaranteed that previous
  561. * LR have complete transactions. We only know that a cycle number of
  562. * current cycle number -1 won't be present in the log if we start writing
  563. * from our current block number.
  564. *
  565. * last_blk contains the block number of the first block with a given
  566. * cycle number.
  567. *
  568. * Return: zero if normal, non-zero if error.
  569. */
  570. STATIC int
  571. xlog_find_head(
  572. struct xlog *log,
  573. xfs_daddr_t *return_head_blk)
  574. {
  575. xfs_buf_t *bp;
  576. char *offset;
  577. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  578. int num_scan_bblks;
  579. uint first_half_cycle, last_half_cycle;
  580. uint stop_on_cycle;
  581. int error, log_bbnum = log->l_logBBsize;
  582. /* Is the end of the log device zeroed? */
  583. error = xlog_find_zeroed(log, &first_blk);
  584. if (error < 0) {
  585. xfs_warn(log->l_mp, "empty log check failed");
  586. return error;
  587. }
  588. if (error == 1) {
  589. *return_head_blk = first_blk;
  590. /* Is the whole lot zeroed? */
  591. if (!first_blk) {
  592. /* Linux XFS shouldn't generate totally zeroed logs -
  593. * mkfs etc write a dummy unmount record to a fresh
  594. * log so we can store the uuid in there
  595. */
  596. xfs_warn(log->l_mp, "totally zeroed log");
  597. }
  598. return 0;
  599. }
  600. first_blk = 0; /* get cycle # of 1st block */
  601. bp = xlog_get_bp(log, 1);
  602. if (!bp)
  603. return -ENOMEM;
  604. error = xlog_bread(log, 0, 1, bp, &offset);
  605. if (error)
  606. goto bp_err;
  607. first_half_cycle = xlog_get_cycle(offset);
  608. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  609. error = xlog_bread(log, last_blk, 1, bp, &offset);
  610. if (error)
  611. goto bp_err;
  612. last_half_cycle = xlog_get_cycle(offset);
  613. ASSERT(last_half_cycle != 0);
  614. /*
  615. * If the 1st half cycle number is equal to the last half cycle number,
  616. * then the entire log is stamped with the same cycle number. In this
  617. * case, head_blk can't be set to zero (which makes sense). The below
  618. * math doesn't work out properly with head_blk equal to zero. Instead,
  619. * we set it to log_bbnum which is an invalid block number, but this
  620. * value makes the math correct. If head_blk doesn't changed through
  621. * all the tests below, *head_blk is set to zero at the very end rather
  622. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  623. * in a circular file.
  624. */
  625. if (first_half_cycle == last_half_cycle) {
  626. /*
  627. * In this case we believe that the entire log should have
  628. * cycle number last_half_cycle. We need to scan backwards
  629. * from the end verifying that there are no holes still
  630. * containing last_half_cycle - 1. If we find such a hole,
  631. * then the start of that hole will be the new head. The
  632. * simple case looks like
  633. * x | x ... | x - 1 | x
  634. * Another case that fits this picture would be
  635. * x | x + 1 | x ... | x
  636. * In this case the head really is somewhere at the end of the
  637. * log, as one of the latest writes at the beginning was
  638. * incomplete.
  639. * One more case is
  640. * x | x + 1 | x ... | x - 1 | x
  641. * This is really the combination of the above two cases, and
  642. * the head has to end up at the start of the x-1 hole at the
  643. * end of the log.
  644. *
  645. * In the 256k log case, we will read from the beginning to the
  646. * end of the log and search for cycle numbers equal to x-1.
  647. * We don't worry about the x+1 blocks that we encounter,
  648. * because we know that they cannot be the head since the log
  649. * started with x.
  650. */
  651. head_blk = log_bbnum;
  652. stop_on_cycle = last_half_cycle - 1;
  653. } else {
  654. /*
  655. * In this case we want to find the first block with cycle
  656. * number matching last_half_cycle. We expect the log to be
  657. * some variation on
  658. * x + 1 ... | x ... | x
  659. * The first block with cycle number x (last_half_cycle) will
  660. * be where the new head belongs. First we do a binary search
  661. * for the first occurrence of last_half_cycle. The binary
  662. * search may not be totally accurate, so then we scan back
  663. * from there looking for occurrences of last_half_cycle before
  664. * us. If that backwards scan wraps around the beginning of
  665. * the log, then we look for occurrences of last_half_cycle - 1
  666. * at the end of the log. The cases we're looking for look
  667. * like
  668. * v binary search stopped here
  669. * x + 1 ... | x | x + 1 | x ... | x
  670. * ^ but we want to locate this spot
  671. * or
  672. * <---------> less than scan distance
  673. * x + 1 ... | x ... | x - 1 | x
  674. * ^ we want to locate this spot
  675. */
  676. stop_on_cycle = last_half_cycle;
  677. if ((error = xlog_find_cycle_start(log, bp, first_blk,
  678. &head_blk, last_half_cycle)))
  679. goto bp_err;
  680. }
  681. /*
  682. * Now validate the answer. Scan back some number of maximum possible
  683. * blocks and make sure each one has the expected cycle number. The
  684. * maximum is determined by the total possible amount of buffering
  685. * in the in-core log. The following number can be made tighter if
  686. * we actually look at the block size of the filesystem.
  687. */
  688. num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
  689. if (head_blk >= num_scan_bblks) {
  690. /*
  691. * We are guaranteed that the entire check can be performed
  692. * in one buffer.
  693. */
  694. start_blk = head_blk - num_scan_bblks;
  695. if ((error = xlog_find_verify_cycle(log,
  696. start_blk, num_scan_bblks,
  697. stop_on_cycle, &new_blk)))
  698. goto bp_err;
  699. if (new_blk != -1)
  700. head_blk = new_blk;
  701. } else { /* need to read 2 parts of log */
  702. /*
  703. * We are going to scan backwards in the log in two parts.
  704. * First we scan the physical end of the log. In this part
  705. * of the log, we are looking for blocks with cycle number
  706. * last_half_cycle - 1.
  707. * If we find one, then we know that the log starts there, as
  708. * we've found a hole that didn't get written in going around
  709. * the end of the physical log. The simple case for this is
  710. * x + 1 ... | x ... | x - 1 | x
  711. * <---------> less than scan distance
  712. * If all of the blocks at the end of the log have cycle number
  713. * last_half_cycle, then we check the blocks at the start of
  714. * the log looking for occurrences of last_half_cycle. If we
  715. * find one, then our current estimate for the location of the
  716. * first occurrence of last_half_cycle is wrong and we move
  717. * back to the hole we've found. This case looks like
  718. * x + 1 ... | x | x + 1 | x ...
  719. * ^ binary search stopped here
  720. * Another case we need to handle that only occurs in 256k
  721. * logs is
  722. * x + 1 ... | x ... | x+1 | x ...
  723. * ^ binary search stops here
  724. * In a 256k log, the scan at the end of the log will see the
  725. * x + 1 blocks. We need to skip past those since that is
  726. * certainly not the head of the log. By searching for
  727. * last_half_cycle-1 we accomplish that.
  728. */
  729. ASSERT(head_blk <= INT_MAX &&
  730. (xfs_daddr_t) num_scan_bblks >= head_blk);
  731. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  732. if ((error = xlog_find_verify_cycle(log, start_blk,
  733. num_scan_bblks - (int)head_blk,
  734. (stop_on_cycle - 1), &new_blk)))
  735. goto bp_err;
  736. if (new_blk != -1) {
  737. head_blk = new_blk;
  738. goto validate_head;
  739. }
  740. /*
  741. * Scan beginning of log now. The last part of the physical
  742. * log is good. This scan needs to verify that it doesn't find
  743. * the last_half_cycle.
  744. */
  745. start_blk = 0;
  746. ASSERT(head_blk <= INT_MAX);
  747. if ((error = xlog_find_verify_cycle(log,
  748. start_blk, (int)head_blk,
  749. stop_on_cycle, &new_blk)))
  750. goto bp_err;
  751. if (new_blk != -1)
  752. head_blk = new_blk;
  753. }
  754. validate_head:
  755. /*
  756. * Now we need to make sure head_blk is not pointing to a block in
  757. * the middle of a log record.
  758. */
  759. num_scan_bblks = XLOG_REC_SHIFT(log);
  760. if (head_blk >= num_scan_bblks) {
  761. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  762. /* start ptr at last block ptr before head_blk */
  763. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  764. if (error == 1)
  765. error = -EIO;
  766. if (error)
  767. goto bp_err;
  768. } else {
  769. start_blk = 0;
  770. ASSERT(head_blk <= INT_MAX);
  771. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  772. if (error < 0)
  773. goto bp_err;
  774. if (error == 1) {
  775. /* We hit the beginning of the log during our search */
  776. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  777. new_blk = log_bbnum;
  778. ASSERT(start_blk <= INT_MAX &&
  779. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  780. ASSERT(head_blk <= INT_MAX);
  781. error = xlog_find_verify_log_record(log, start_blk,
  782. &new_blk, (int)head_blk);
  783. if (error == 1)
  784. error = -EIO;
  785. if (error)
  786. goto bp_err;
  787. if (new_blk != log_bbnum)
  788. head_blk = new_blk;
  789. } else if (error)
  790. goto bp_err;
  791. }
  792. xlog_put_bp(bp);
  793. if (head_blk == log_bbnum)
  794. *return_head_blk = 0;
  795. else
  796. *return_head_blk = head_blk;
  797. /*
  798. * When returning here, we have a good block number. Bad block
  799. * means that during a previous crash, we didn't have a clean break
  800. * from cycle number N to cycle number N-1. In this case, we need
  801. * to find the first block with cycle number N-1.
  802. */
  803. return 0;
  804. bp_err:
  805. xlog_put_bp(bp);
  806. if (error)
  807. xfs_warn(log->l_mp, "failed to find log head");
  808. return error;
  809. }
  810. /*
  811. * Seek backwards in the log for log record headers.
  812. *
  813. * Given a starting log block, walk backwards until we find the provided number
  814. * of records or hit the provided tail block. The return value is the number of
  815. * records encountered or a negative error code. The log block and buffer
  816. * pointer of the last record seen are returned in rblk and rhead respectively.
  817. */
  818. STATIC int
  819. xlog_rseek_logrec_hdr(
  820. struct xlog *log,
  821. xfs_daddr_t head_blk,
  822. xfs_daddr_t tail_blk,
  823. int count,
  824. struct xfs_buf *bp,
  825. xfs_daddr_t *rblk,
  826. struct xlog_rec_header **rhead,
  827. bool *wrapped)
  828. {
  829. int i;
  830. int error;
  831. int found = 0;
  832. char *offset = NULL;
  833. xfs_daddr_t end_blk;
  834. *wrapped = false;
  835. /*
  836. * Walk backwards from the head block until we hit the tail or the first
  837. * block in the log.
  838. */
  839. end_blk = head_blk > tail_blk ? tail_blk : 0;
  840. for (i = (int) head_blk - 1; i >= end_blk; i--) {
  841. error = xlog_bread(log, i, 1, bp, &offset);
  842. if (error)
  843. goto out_error;
  844. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  845. *rblk = i;
  846. *rhead = (struct xlog_rec_header *) offset;
  847. if (++found == count)
  848. break;
  849. }
  850. }
  851. /*
  852. * If we haven't hit the tail block or the log record header count,
  853. * start looking again from the end of the physical log. Note that
  854. * callers can pass head == tail if the tail is not yet known.
  855. */
  856. if (tail_blk >= head_blk && found != count) {
  857. for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
  858. error = xlog_bread(log, i, 1, bp, &offset);
  859. if (error)
  860. goto out_error;
  861. if (*(__be32 *)offset ==
  862. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  863. *wrapped = true;
  864. *rblk = i;
  865. *rhead = (struct xlog_rec_header *) offset;
  866. if (++found == count)
  867. break;
  868. }
  869. }
  870. }
  871. return found;
  872. out_error:
  873. return error;
  874. }
  875. /*
  876. * Seek forward in the log for log record headers.
  877. *
  878. * Given head and tail blocks, walk forward from the tail block until we find
  879. * the provided number of records or hit the head block. The return value is the
  880. * number of records encountered or a negative error code. The log block and
  881. * buffer pointer of the last record seen are returned in rblk and rhead
  882. * respectively.
  883. */
  884. STATIC int
  885. xlog_seek_logrec_hdr(
  886. struct xlog *log,
  887. xfs_daddr_t head_blk,
  888. xfs_daddr_t tail_blk,
  889. int count,
  890. struct xfs_buf *bp,
  891. xfs_daddr_t *rblk,
  892. struct xlog_rec_header **rhead,
  893. bool *wrapped)
  894. {
  895. int i;
  896. int error;
  897. int found = 0;
  898. char *offset = NULL;
  899. xfs_daddr_t end_blk;
  900. *wrapped = false;
  901. /*
  902. * Walk forward from the tail block until we hit the head or the last
  903. * block in the log.
  904. */
  905. end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
  906. for (i = (int) tail_blk; i <= end_blk; i++) {
  907. error = xlog_bread(log, i, 1, bp, &offset);
  908. if (error)
  909. goto out_error;
  910. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  911. *rblk = i;
  912. *rhead = (struct xlog_rec_header *) offset;
  913. if (++found == count)
  914. break;
  915. }
  916. }
  917. /*
  918. * If we haven't hit the head block or the log record header count,
  919. * start looking again from the start of the physical log.
  920. */
  921. if (tail_blk > head_blk && found != count) {
  922. for (i = 0; i < (int) head_blk; i++) {
  923. error = xlog_bread(log, i, 1, bp, &offset);
  924. if (error)
  925. goto out_error;
  926. if (*(__be32 *)offset ==
  927. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  928. *wrapped = true;
  929. *rblk = i;
  930. *rhead = (struct xlog_rec_header *) offset;
  931. if (++found == count)
  932. break;
  933. }
  934. }
  935. }
  936. return found;
  937. out_error:
  938. return error;
  939. }
  940. /*
  941. * Calculate distance from head to tail (i.e., unused space in the log).
  942. */
  943. static inline int
  944. xlog_tail_distance(
  945. struct xlog *log,
  946. xfs_daddr_t head_blk,
  947. xfs_daddr_t tail_blk)
  948. {
  949. if (head_blk < tail_blk)
  950. return tail_blk - head_blk;
  951. return tail_blk + (log->l_logBBsize - head_blk);
  952. }
  953. /*
  954. * Verify the log tail. This is particularly important when torn or incomplete
  955. * writes have been detected near the front of the log and the head has been
  956. * walked back accordingly.
  957. *
  958. * We also have to handle the case where the tail was pinned and the head
  959. * blocked behind the tail right before a crash. If the tail had been pushed
  960. * immediately prior to the crash and the subsequent checkpoint was only
  961. * partially written, it's possible it overwrote the last referenced tail in the
  962. * log with garbage. This is not a coherency problem because the tail must have
  963. * been pushed before it can be overwritten, but appears as log corruption to
  964. * recovery because we have no way to know the tail was updated if the
  965. * subsequent checkpoint didn't write successfully.
  966. *
  967. * Therefore, CRC check the log from tail to head. If a failure occurs and the
  968. * offending record is within max iclog bufs from the head, walk the tail
  969. * forward and retry until a valid tail is found or corruption is detected out
  970. * of the range of a possible overwrite.
  971. */
  972. STATIC int
  973. xlog_verify_tail(
  974. struct xlog *log,
  975. xfs_daddr_t head_blk,
  976. xfs_daddr_t *tail_blk,
  977. int hsize)
  978. {
  979. struct xlog_rec_header *thead;
  980. struct xfs_buf *bp;
  981. xfs_daddr_t first_bad;
  982. int error = 0;
  983. bool wrapped;
  984. xfs_daddr_t tmp_tail;
  985. xfs_daddr_t orig_tail = *tail_blk;
  986. bp = xlog_get_bp(log, 1);
  987. if (!bp)
  988. return -ENOMEM;
  989. /*
  990. * Make sure the tail points to a record (returns positive count on
  991. * success).
  992. */
  993. error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, bp,
  994. &tmp_tail, &thead, &wrapped);
  995. if (error < 0)
  996. goto out;
  997. if (*tail_blk != tmp_tail)
  998. *tail_blk = tmp_tail;
  999. /*
  1000. * Run a CRC check from the tail to the head. We can't just check
  1001. * MAX_ICLOGS records past the tail because the tail may point to stale
  1002. * blocks cleared during the search for the head/tail. These blocks are
  1003. * overwritten with zero-length records and thus record count is not a
  1004. * reliable indicator of the iclog state before a crash.
  1005. */
  1006. first_bad = 0;
  1007. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  1008. XLOG_RECOVER_CRCPASS, &first_bad);
  1009. while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  1010. int tail_distance;
  1011. /*
  1012. * Is corruption within range of the head? If so, retry from
  1013. * the next record. Otherwise return an error.
  1014. */
  1015. tail_distance = xlog_tail_distance(log, head_blk, first_bad);
  1016. if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
  1017. break;
  1018. /* skip to the next record; returns positive count on success */
  1019. error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2, bp,
  1020. &tmp_tail, &thead, &wrapped);
  1021. if (error < 0)
  1022. goto out;
  1023. *tail_blk = tmp_tail;
  1024. first_bad = 0;
  1025. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  1026. XLOG_RECOVER_CRCPASS, &first_bad);
  1027. }
  1028. if (!error && *tail_blk != orig_tail)
  1029. xfs_warn(log->l_mp,
  1030. "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
  1031. orig_tail, *tail_blk);
  1032. out:
  1033. xlog_put_bp(bp);
  1034. return error;
  1035. }
  1036. /*
  1037. * Detect and trim torn writes from the head of the log.
  1038. *
  1039. * Storage without sector atomicity guarantees can result in torn writes in the
  1040. * log in the event of a crash. Our only means to detect this scenario is via
  1041. * CRC verification. While we can't always be certain that CRC verification
  1042. * failure is due to a torn write vs. an unrelated corruption, we do know that
  1043. * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
  1044. * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
  1045. * the log and treat failures in this range as torn writes as a matter of
  1046. * policy. In the event of CRC failure, the head is walked back to the last good
  1047. * record in the log and the tail is updated from that record and verified.
  1048. */
  1049. STATIC int
  1050. xlog_verify_head(
  1051. struct xlog *log,
  1052. xfs_daddr_t *head_blk, /* in/out: unverified head */
  1053. xfs_daddr_t *tail_blk, /* out: tail block */
  1054. struct xfs_buf *bp,
  1055. xfs_daddr_t *rhead_blk, /* start blk of last record */
  1056. struct xlog_rec_header **rhead, /* ptr to last record */
  1057. bool *wrapped) /* last rec. wraps phys. log */
  1058. {
  1059. struct xlog_rec_header *tmp_rhead;
  1060. struct xfs_buf *tmp_bp;
  1061. xfs_daddr_t first_bad;
  1062. xfs_daddr_t tmp_rhead_blk;
  1063. int found;
  1064. int error;
  1065. bool tmp_wrapped;
  1066. /*
  1067. * Check the head of the log for torn writes. Search backwards from the
  1068. * head until we hit the tail or the maximum number of log record I/Os
  1069. * that could have been in flight at one time. Use a temporary buffer so
  1070. * we don't trash the rhead/bp pointers from the caller.
  1071. */
  1072. tmp_bp = xlog_get_bp(log, 1);
  1073. if (!tmp_bp)
  1074. return -ENOMEM;
  1075. error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
  1076. XLOG_MAX_ICLOGS, tmp_bp, &tmp_rhead_blk,
  1077. &tmp_rhead, &tmp_wrapped);
  1078. xlog_put_bp(tmp_bp);
  1079. if (error < 0)
  1080. return error;
  1081. /*
  1082. * Now run a CRC verification pass over the records starting at the
  1083. * block found above to the current head. If a CRC failure occurs, the
  1084. * log block of the first bad record is saved in first_bad.
  1085. */
  1086. error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
  1087. XLOG_RECOVER_CRCPASS, &first_bad);
  1088. if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  1089. /*
  1090. * We've hit a potential torn write. Reset the error and warn
  1091. * about it.
  1092. */
  1093. error = 0;
  1094. xfs_warn(log->l_mp,
  1095. "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
  1096. first_bad, *head_blk);
  1097. /*
  1098. * Get the header block and buffer pointer for the last good
  1099. * record before the bad record.
  1100. *
  1101. * Note that xlog_find_tail() clears the blocks at the new head
  1102. * (i.e., the records with invalid CRC) if the cycle number
  1103. * matches the the current cycle.
  1104. */
  1105. found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1, bp,
  1106. rhead_blk, rhead, wrapped);
  1107. if (found < 0)
  1108. return found;
  1109. if (found == 0) /* XXX: right thing to do here? */
  1110. return -EIO;
  1111. /*
  1112. * Reset the head block to the starting block of the first bad
  1113. * log record and set the tail block based on the last good
  1114. * record.
  1115. *
  1116. * Bail out if the updated head/tail match as this indicates
  1117. * possible corruption outside of the acceptable
  1118. * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
  1119. */
  1120. *head_blk = first_bad;
  1121. *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
  1122. if (*head_blk == *tail_blk) {
  1123. ASSERT(0);
  1124. return 0;
  1125. }
  1126. }
  1127. if (error)
  1128. return error;
  1129. return xlog_verify_tail(log, *head_blk, tail_blk,
  1130. be32_to_cpu((*rhead)->h_size));
  1131. }
  1132. /*
  1133. * Check whether the head of the log points to an unmount record. In other
  1134. * words, determine whether the log is clean. If so, update the in-core state
  1135. * appropriately.
  1136. */
  1137. static int
  1138. xlog_check_unmount_rec(
  1139. struct xlog *log,
  1140. xfs_daddr_t *head_blk,
  1141. xfs_daddr_t *tail_blk,
  1142. struct xlog_rec_header *rhead,
  1143. xfs_daddr_t rhead_blk,
  1144. struct xfs_buf *bp,
  1145. bool *clean)
  1146. {
  1147. struct xlog_op_header *op_head;
  1148. xfs_daddr_t umount_data_blk;
  1149. xfs_daddr_t after_umount_blk;
  1150. int hblks;
  1151. int error;
  1152. char *offset;
  1153. *clean = false;
  1154. /*
  1155. * Look for unmount record. If we find it, then we know there was a
  1156. * clean unmount. Since 'i' could be the last block in the physical
  1157. * log, we convert to a log block before comparing to the head_blk.
  1158. *
  1159. * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
  1160. * below. We won't want to clear the unmount record if there is one, so
  1161. * we pass the lsn of the unmount record rather than the block after it.
  1162. */
  1163. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  1164. int h_size = be32_to_cpu(rhead->h_size);
  1165. int h_version = be32_to_cpu(rhead->h_version);
  1166. if ((h_version & XLOG_VERSION_2) &&
  1167. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  1168. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  1169. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  1170. hblks++;
  1171. } else {
  1172. hblks = 1;
  1173. }
  1174. } else {
  1175. hblks = 1;
  1176. }
  1177. after_umount_blk = rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len));
  1178. after_umount_blk = do_mod(after_umount_blk, log->l_logBBsize);
  1179. if (*head_blk == after_umount_blk &&
  1180. be32_to_cpu(rhead->h_num_logops) == 1) {
  1181. umount_data_blk = rhead_blk + hblks;
  1182. umount_data_blk = do_mod(umount_data_blk, log->l_logBBsize);
  1183. error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
  1184. if (error)
  1185. return error;
  1186. op_head = (struct xlog_op_header *)offset;
  1187. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  1188. /*
  1189. * Set tail and last sync so that newly written log
  1190. * records will point recovery to after the current
  1191. * unmount record.
  1192. */
  1193. xlog_assign_atomic_lsn(&log->l_tail_lsn,
  1194. log->l_curr_cycle, after_umount_blk);
  1195. xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  1196. log->l_curr_cycle, after_umount_blk);
  1197. *tail_blk = after_umount_blk;
  1198. *clean = true;
  1199. }
  1200. }
  1201. return 0;
  1202. }
  1203. static void
  1204. xlog_set_state(
  1205. struct xlog *log,
  1206. xfs_daddr_t head_blk,
  1207. struct xlog_rec_header *rhead,
  1208. xfs_daddr_t rhead_blk,
  1209. bool bump_cycle)
  1210. {
  1211. /*
  1212. * Reset log values according to the state of the log when we
  1213. * crashed. In the case where head_blk == 0, we bump curr_cycle
  1214. * one because the next write starts a new cycle rather than
  1215. * continuing the cycle of the last good log record. At this
  1216. * point we have guaranteed that all partial log records have been
  1217. * accounted for. Therefore, we know that the last good log record
  1218. * written was complete and ended exactly on the end boundary
  1219. * of the physical log.
  1220. */
  1221. log->l_prev_block = rhead_blk;
  1222. log->l_curr_block = (int)head_blk;
  1223. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  1224. if (bump_cycle)
  1225. log->l_curr_cycle++;
  1226. atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  1227. atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  1228. xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  1229. BBTOB(log->l_curr_block));
  1230. xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  1231. BBTOB(log->l_curr_block));
  1232. }
  1233. /*
  1234. * Find the sync block number or the tail of the log.
  1235. *
  1236. * This will be the block number of the last record to have its
  1237. * associated buffers synced to disk. Every log record header has
  1238. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  1239. * to get a sync block number. The only concern is to figure out which
  1240. * log record header to believe.
  1241. *
  1242. * The following algorithm uses the log record header with the largest
  1243. * lsn. The entire log record does not need to be valid. We only care
  1244. * that the header is valid.
  1245. *
  1246. * We could speed up search by using current head_blk buffer, but it is not
  1247. * available.
  1248. */
  1249. STATIC int
  1250. xlog_find_tail(
  1251. struct xlog *log,
  1252. xfs_daddr_t *head_blk,
  1253. xfs_daddr_t *tail_blk)
  1254. {
  1255. xlog_rec_header_t *rhead;
  1256. char *offset = NULL;
  1257. xfs_buf_t *bp;
  1258. int error;
  1259. xfs_daddr_t rhead_blk;
  1260. xfs_lsn_t tail_lsn;
  1261. bool wrapped = false;
  1262. bool clean = false;
  1263. /*
  1264. * Find previous log record
  1265. */
  1266. if ((error = xlog_find_head(log, head_blk)))
  1267. return error;
  1268. ASSERT(*head_blk < INT_MAX);
  1269. bp = xlog_get_bp(log, 1);
  1270. if (!bp)
  1271. return -ENOMEM;
  1272. if (*head_blk == 0) { /* special case */
  1273. error = xlog_bread(log, 0, 1, bp, &offset);
  1274. if (error)
  1275. goto done;
  1276. if (xlog_get_cycle(offset) == 0) {
  1277. *tail_blk = 0;
  1278. /* leave all other log inited values alone */
  1279. goto done;
  1280. }
  1281. }
  1282. /*
  1283. * Search backwards through the log looking for the log record header
  1284. * block. This wraps all the way back around to the head so something is
  1285. * seriously wrong if we can't find it.
  1286. */
  1287. error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, bp,
  1288. &rhead_blk, &rhead, &wrapped);
  1289. if (error < 0)
  1290. return error;
  1291. if (!error) {
  1292. xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
  1293. return -EIO;
  1294. }
  1295. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  1296. /*
  1297. * Set the log state based on the current head record.
  1298. */
  1299. xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
  1300. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1301. /*
  1302. * Look for an unmount record at the head of the log. This sets the log
  1303. * state to determine whether recovery is necessary.
  1304. */
  1305. error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
  1306. rhead_blk, bp, &clean);
  1307. if (error)
  1308. goto done;
  1309. /*
  1310. * Verify the log head if the log is not clean (e.g., we have anything
  1311. * but an unmount record at the head). This uses CRC verification to
  1312. * detect and trim torn writes. If discovered, CRC failures are
  1313. * considered torn writes and the log head is trimmed accordingly.
  1314. *
  1315. * Note that we can only run CRC verification when the log is dirty
  1316. * because there's no guarantee that the log data behind an unmount
  1317. * record is compatible with the current architecture.
  1318. */
  1319. if (!clean) {
  1320. xfs_daddr_t orig_head = *head_blk;
  1321. error = xlog_verify_head(log, head_blk, tail_blk, bp,
  1322. &rhead_blk, &rhead, &wrapped);
  1323. if (error)
  1324. goto done;
  1325. /* update in-core state again if the head changed */
  1326. if (*head_blk != orig_head) {
  1327. xlog_set_state(log, *head_blk, rhead, rhead_blk,
  1328. wrapped);
  1329. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1330. error = xlog_check_unmount_rec(log, head_blk, tail_blk,
  1331. rhead, rhead_blk, bp,
  1332. &clean);
  1333. if (error)
  1334. goto done;
  1335. }
  1336. }
  1337. /*
  1338. * Note that the unmount was clean. If the unmount was not clean, we
  1339. * need to know this to rebuild the superblock counters from the perag
  1340. * headers if we have a filesystem using non-persistent counters.
  1341. */
  1342. if (clean)
  1343. log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
  1344. /*
  1345. * Make sure that there are no blocks in front of the head
  1346. * with the same cycle number as the head. This can happen
  1347. * because we allow multiple outstanding log writes concurrently,
  1348. * and the later writes might make it out before earlier ones.
  1349. *
  1350. * We use the lsn from before modifying it so that we'll never
  1351. * overwrite the unmount record after a clean unmount.
  1352. *
  1353. * Do this only if we are going to recover the filesystem
  1354. *
  1355. * NOTE: This used to say "if (!readonly)"
  1356. * However on Linux, we can & do recover a read-only filesystem.
  1357. * We only skip recovery if NORECOVERY is specified on mount,
  1358. * in which case we would not be here.
  1359. *
  1360. * But... if the -device- itself is readonly, just skip this.
  1361. * We can't recover this device anyway, so it won't matter.
  1362. */
  1363. if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp))
  1364. error = xlog_clear_stale_blocks(log, tail_lsn);
  1365. done:
  1366. xlog_put_bp(bp);
  1367. if (error)
  1368. xfs_warn(log->l_mp, "failed to locate log tail");
  1369. return error;
  1370. }
  1371. /*
  1372. * Is the log zeroed at all?
  1373. *
  1374. * The last binary search should be changed to perform an X block read
  1375. * once X becomes small enough. You can then search linearly through
  1376. * the X blocks. This will cut down on the number of reads we need to do.
  1377. *
  1378. * If the log is partially zeroed, this routine will pass back the blkno
  1379. * of the first block with cycle number 0. It won't have a complete LR
  1380. * preceding it.
  1381. *
  1382. * Return:
  1383. * 0 => the log is completely written to
  1384. * 1 => use *blk_no as the first block of the log
  1385. * <0 => error has occurred
  1386. */
  1387. STATIC int
  1388. xlog_find_zeroed(
  1389. struct xlog *log,
  1390. xfs_daddr_t *blk_no)
  1391. {
  1392. xfs_buf_t *bp;
  1393. char *offset;
  1394. uint first_cycle, last_cycle;
  1395. xfs_daddr_t new_blk, last_blk, start_blk;
  1396. xfs_daddr_t num_scan_bblks;
  1397. int error, log_bbnum = log->l_logBBsize;
  1398. *blk_no = 0;
  1399. /* check totally zeroed log */
  1400. bp = xlog_get_bp(log, 1);
  1401. if (!bp)
  1402. return -ENOMEM;
  1403. error = xlog_bread(log, 0, 1, bp, &offset);
  1404. if (error)
  1405. goto bp_err;
  1406. first_cycle = xlog_get_cycle(offset);
  1407. if (first_cycle == 0) { /* completely zeroed log */
  1408. *blk_no = 0;
  1409. xlog_put_bp(bp);
  1410. return 1;
  1411. }
  1412. /* check partially zeroed log */
  1413. error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
  1414. if (error)
  1415. goto bp_err;
  1416. last_cycle = xlog_get_cycle(offset);
  1417. if (last_cycle != 0) { /* log completely written to */
  1418. xlog_put_bp(bp);
  1419. return 0;
  1420. } else if (first_cycle != 1) {
  1421. /*
  1422. * If the cycle of the last block is zero, the cycle of
  1423. * the first block must be 1. If it's not, maybe we're
  1424. * not looking at a log... Bail out.
  1425. */
  1426. xfs_warn(log->l_mp,
  1427. "Log inconsistent or not a log (last==0, first!=1)");
  1428. error = -EINVAL;
  1429. goto bp_err;
  1430. }
  1431. /* we have a partially zeroed log */
  1432. last_blk = log_bbnum-1;
  1433. if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
  1434. goto bp_err;
  1435. /*
  1436. * Validate the answer. Because there is no way to guarantee that
  1437. * the entire log is made up of log records which are the same size,
  1438. * we scan over the defined maximum blocks. At this point, the maximum
  1439. * is not chosen to mean anything special. XXXmiken
  1440. */
  1441. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  1442. ASSERT(num_scan_bblks <= INT_MAX);
  1443. if (last_blk < num_scan_bblks)
  1444. num_scan_bblks = last_blk;
  1445. start_blk = last_blk - num_scan_bblks;
  1446. /*
  1447. * We search for any instances of cycle number 0 that occur before
  1448. * our current estimate of the head. What we're trying to detect is
  1449. * 1 ... | 0 | 1 | 0...
  1450. * ^ binary search ends here
  1451. */
  1452. if ((error = xlog_find_verify_cycle(log, start_blk,
  1453. (int)num_scan_bblks, 0, &new_blk)))
  1454. goto bp_err;
  1455. if (new_blk != -1)
  1456. last_blk = new_blk;
  1457. /*
  1458. * Potentially backup over partial log record write. We don't need
  1459. * to search the end of the log because we know it is zero.
  1460. */
  1461. error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  1462. if (error == 1)
  1463. error = -EIO;
  1464. if (error)
  1465. goto bp_err;
  1466. *blk_no = last_blk;
  1467. bp_err:
  1468. xlog_put_bp(bp);
  1469. if (error)
  1470. return error;
  1471. return 1;
  1472. }
  1473. /*
  1474. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1475. * to initialize a buffer full of empty log record headers and write
  1476. * them into the log.
  1477. */
  1478. STATIC void
  1479. xlog_add_record(
  1480. struct xlog *log,
  1481. char *buf,
  1482. int cycle,
  1483. int block,
  1484. int tail_cycle,
  1485. int tail_block)
  1486. {
  1487. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1488. memset(buf, 0, BBSIZE);
  1489. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1490. recp->h_cycle = cpu_to_be32(cycle);
  1491. recp->h_version = cpu_to_be32(
  1492. xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
  1493. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1494. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1495. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1496. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1497. }
  1498. STATIC int
  1499. xlog_write_log_records(
  1500. struct xlog *log,
  1501. int cycle,
  1502. int start_block,
  1503. int blocks,
  1504. int tail_cycle,
  1505. int tail_block)
  1506. {
  1507. char *offset;
  1508. xfs_buf_t *bp;
  1509. int balign, ealign;
  1510. int sectbb = log->l_sectBBsize;
  1511. int end_block = start_block + blocks;
  1512. int bufblks;
  1513. int error = 0;
  1514. int i, j = 0;
  1515. /*
  1516. * Greedily allocate a buffer big enough to handle the full
  1517. * range of basic blocks to be written. If that fails, try
  1518. * a smaller size. We need to be able to write at least a
  1519. * log sector, or we're out of luck.
  1520. */
  1521. bufblks = 1 << ffs(blocks);
  1522. while (bufblks > log->l_logBBsize)
  1523. bufblks >>= 1;
  1524. while (!(bp = xlog_get_bp(log, bufblks))) {
  1525. bufblks >>= 1;
  1526. if (bufblks < sectbb)
  1527. return -ENOMEM;
  1528. }
  1529. /* We may need to do a read at the start to fill in part of
  1530. * the buffer in the starting sector not covered by the first
  1531. * write below.
  1532. */
  1533. balign = round_down(start_block, sectbb);
  1534. if (balign != start_block) {
  1535. error = xlog_bread_noalign(log, start_block, 1, bp);
  1536. if (error)
  1537. goto out_put_bp;
  1538. j = start_block - balign;
  1539. }
  1540. for (i = start_block; i < end_block; i += bufblks) {
  1541. int bcount, endcount;
  1542. bcount = min(bufblks, end_block - start_block);
  1543. endcount = bcount - j;
  1544. /* We may need to do a read at the end to fill in part of
  1545. * the buffer in the final sector not covered by the write.
  1546. * If this is the same sector as the above read, skip it.
  1547. */
  1548. ealign = round_down(end_block, sectbb);
  1549. if (j == 0 && (start_block + endcount > ealign)) {
  1550. offset = bp->b_addr + BBTOB(ealign - start_block);
  1551. error = xlog_bread_offset(log, ealign, sectbb,
  1552. bp, offset);
  1553. if (error)
  1554. break;
  1555. }
  1556. offset = xlog_align(log, start_block, endcount, bp);
  1557. for (; j < endcount; j++) {
  1558. xlog_add_record(log, offset, cycle, i+j,
  1559. tail_cycle, tail_block);
  1560. offset += BBSIZE;
  1561. }
  1562. error = xlog_bwrite(log, start_block, endcount, bp);
  1563. if (error)
  1564. break;
  1565. start_block += endcount;
  1566. j = 0;
  1567. }
  1568. out_put_bp:
  1569. xlog_put_bp(bp);
  1570. return error;
  1571. }
  1572. /*
  1573. * This routine is called to blow away any incomplete log writes out
  1574. * in front of the log head. We do this so that we won't become confused
  1575. * if we come up, write only a little bit more, and then crash again.
  1576. * If we leave the partial log records out there, this situation could
  1577. * cause us to think those partial writes are valid blocks since they
  1578. * have the current cycle number. We get rid of them by overwriting them
  1579. * with empty log records with the old cycle number rather than the
  1580. * current one.
  1581. *
  1582. * The tail lsn is passed in rather than taken from
  1583. * the log so that we will not write over the unmount record after a
  1584. * clean unmount in a 512 block log. Doing so would leave the log without
  1585. * any valid log records in it until a new one was written. If we crashed
  1586. * during that time we would not be able to recover.
  1587. */
  1588. STATIC int
  1589. xlog_clear_stale_blocks(
  1590. struct xlog *log,
  1591. xfs_lsn_t tail_lsn)
  1592. {
  1593. int tail_cycle, head_cycle;
  1594. int tail_block, head_block;
  1595. int tail_distance, max_distance;
  1596. int distance;
  1597. int error;
  1598. tail_cycle = CYCLE_LSN(tail_lsn);
  1599. tail_block = BLOCK_LSN(tail_lsn);
  1600. head_cycle = log->l_curr_cycle;
  1601. head_block = log->l_curr_block;
  1602. /*
  1603. * Figure out the distance between the new head of the log
  1604. * and the tail. We want to write over any blocks beyond the
  1605. * head that we may have written just before the crash, but
  1606. * we don't want to overwrite the tail of the log.
  1607. */
  1608. if (head_cycle == tail_cycle) {
  1609. /*
  1610. * The tail is behind the head in the physical log,
  1611. * so the distance from the head to the tail is the
  1612. * distance from the head to the end of the log plus
  1613. * the distance from the beginning of the log to the
  1614. * tail.
  1615. */
  1616. if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
  1617. XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
  1618. XFS_ERRLEVEL_LOW, log->l_mp);
  1619. return -EFSCORRUPTED;
  1620. }
  1621. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1622. } else {
  1623. /*
  1624. * The head is behind the tail in the physical log,
  1625. * so the distance from the head to the tail is just
  1626. * the tail block minus the head block.
  1627. */
  1628. if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
  1629. XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
  1630. XFS_ERRLEVEL_LOW, log->l_mp);
  1631. return -EFSCORRUPTED;
  1632. }
  1633. tail_distance = tail_block - head_block;
  1634. }
  1635. /*
  1636. * If the head is right up against the tail, we can't clear
  1637. * anything.
  1638. */
  1639. if (tail_distance <= 0) {
  1640. ASSERT(tail_distance == 0);
  1641. return 0;
  1642. }
  1643. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1644. /*
  1645. * Take the smaller of the maximum amount of outstanding I/O
  1646. * we could have and the distance to the tail to clear out.
  1647. * We take the smaller so that we don't overwrite the tail and
  1648. * we don't waste all day writing from the head to the tail
  1649. * for no reason.
  1650. */
  1651. max_distance = MIN(max_distance, tail_distance);
  1652. if ((head_block + max_distance) <= log->l_logBBsize) {
  1653. /*
  1654. * We can stomp all the blocks we need to without
  1655. * wrapping around the end of the log. Just do it
  1656. * in a single write. Use the cycle number of the
  1657. * current cycle minus one so that the log will look like:
  1658. * n ... | n - 1 ...
  1659. */
  1660. error = xlog_write_log_records(log, (head_cycle - 1),
  1661. head_block, max_distance, tail_cycle,
  1662. tail_block);
  1663. if (error)
  1664. return error;
  1665. } else {
  1666. /*
  1667. * We need to wrap around the end of the physical log in
  1668. * order to clear all the blocks. Do it in two separate
  1669. * I/Os. The first write should be from the head to the
  1670. * end of the physical log, and it should use the current
  1671. * cycle number minus one just like above.
  1672. */
  1673. distance = log->l_logBBsize - head_block;
  1674. error = xlog_write_log_records(log, (head_cycle - 1),
  1675. head_block, distance, tail_cycle,
  1676. tail_block);
  1677. if (error)
  1678. return error;
  1679. /*
  1680. * Now write the blocks at the start of the physical log.
  1681. * This writes the remainder of the blocks we want to clear.
  1682. * It uses the current cycle number since we're now on the
  1683. * same cycle as the head so that we get:
  1684. * n ... n ... | n - 1 ...
  1685. * ^^^^^ blocks we're writing
  1686. */
  1687. distance = max_distance - (log->l_logBBsize - head_block);
  1688. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1689. tail_cycle, tail_block);
  1690. if (error)
  1691. return error;
  1692. }
  1693. return 0;
  1694. }
  1695. /******************************************************************************
  1696. *
  1697. * Log recover routines
  1698. *
  1699. ******************************************************************************
  1700. */
  1701. /*
  1702. * Sort the log items in the transaction.
  1703. *
  1704. * The ordering constraints are defined by the inode allocation and unlink
  1705. * behaviour. The rules are:
  1706. *
  1707. * 1. Every item is only logged once in a given transaction. Hence it
  1708. * represents the last logged state of the item. Hence ordering is
  1709. * dependent on the order in which operations need to be performed so
  1710. * required initial conditions are always met.
  1711. *
  1712. * 2. Cancelled buffers are recorded in pass 1 in a separate table and
  1713. * there's nothing to replay from them so we can simply cull them
  1714. * from the transaction. However, we can't do that until after we've
  1715. * replayed all the other items because they may be dependent on the
  1716. * cancelled buffer and replaying the cancelled buffer can remove it
  1717. * form the cancelled buffer table. Hence they have tobe done last.
  1718. *
  1719. * 3. Inode allocation buffers must be replayed before inode items that
  1720. * read the buffer and replay changes into it. For filesystems using the
  1721. * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
  1722. * treated the same as inode allocation buffers as they create and
  1723. * initialise the buffers directly.
  1724. *
  1725. * 4. Inode unlink buffers must be replayed after inode items are replayed.
  1726. * This ensures that inodes are completely flushed to the inode buffer
  1727. * in a "free" state before we remove the unlinked inode list pointer.
  1728. *
  1729. * Hence the ordering needs to be inode allocation buffers first, inode items
  1730. * second, inode unlink buffers third and cancelled buffers last.
  1731. *
  1732. * But there's a problem with that - we can't tell an inode allocation buffer
  1733. * apart from a regular buffer, so we can't separate them. We can, however,
  1734. * tell an inode unlink buffer from the others, and so we can separate them out
  1735. * from all the other buffers and move them to last.
  1736. *
  1737. * Hence, 4 lists, in order from head to tail:
  1738. * - buffer_list for all buffers except cancelled/inode unlink buffers
  1739. * - item_list for all non-buffer items
  1740. * - inode_buffer_list for inode unlink buffers
  1741. * - cancel_list for the cancelled buffers
  1742. *
  1743. * Note that we add objects to the tail of the lists so that first-to-last
  1744. * ordering is preserved within the lists. Adding objects to the head of the
  1745. * list means when we traverse from the head we walk them in last-to-first
  1746. * order. For cancelled buffers and inode unlink buffers this doesn't matter,
  1747. * but for all other items there may be specific ordering that we need to
  1748. * preserve.
  1749. */
  1750. STATIC int
  1751. xlog_recover_reorder_trans(
  1752. struct xlog *log,
  1753. struct xlog_recover *trans,
  1754. int pass)
  1755. {
  1756. xlog_recover_item_t *item, *n;
  1757. int error = 0;
  1758. LIST_HEAD(sort_list);
  1759. LIST_HEAD(cancel_list);
  1760. LIST_HEAD(buffer_list);
  1761. LIST_HEAD(inode_buffer_list);
  1762. LIST_HEAD(inode_list);
  1763. list_splice_init(&trans->r_itemq, &sort_list);
  1764. list_for_each_entry_safe(item, n, &sort_list, ri_list) {
  1765. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1766. switch (ITEM_TYPE(item)) {
  1767. case XFS_LI_ICREATE:
  1768. list_move_tail(&item->ri_list, &buffer_list);
  1769. break;
  1770. case XFS_LI_BUF:
  1771. if (buf_f->blf_flags & XFS_BLF_CANCEL) {
  1772. trace_xfs_log_recover_item_reorder_head(log,
  1773. trans, item, pass);
  1774. list_move(&item->ri_list, &cancel_list);
  1775. break;
  1776. }
  1777. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  1778. list_move(&item->ri_list, &inode_buffer_list);
  1779. break;
  1780. }
  1781. list_move_tail(&item->ri_list, &buffer_list);
  1782. break;
  1783. case XFS_LI_INODE:
  1784. case XFS_LI_DQUOT:
  1785. case XFS_LI_QUOTAOFF:
  1786. case XFS_LI_EFD:
  1787. case XFS_LI_EFI:
  1788. case XFS_LI_RUI:
  1789. case XFS_LI_RUD:
  1790. case XFS_LI_CUI:
  1791. case XFS_LI_CUD:
  1792. case XFS_LI_BUI:
  1793. case XFS_LI_BUD:
  1794. trace_xfs_log_recover_item_reorder_tail(log,
  1795. trans, item, pass);
  1796. list_move_tail(&item->ri_list, &inode_list);
  1797. break;
  1798. default:
  1799. xfs_warn(log->l_mp,
  1800. "%s: unrecognized type of log operation",
  1801. __func__);
  1802. ASSERT(0);
  1803. /*
  1804. * return the remaining items back to the transaction
  1805. * item list so they can be freed in caller.
  1806. */
  1807. if (!list_empty(&sort_list))
  1808. list_splice_init(&sort_list, &trans->r_itemq);
  1809. error = -EIO;
  1810. goto out;
  1811. }
  1812. }
  1813. out:
  1814. ASSERT(list_empty(&sort_list));
  1815. if (!list_empty(&buffer_list))
  1816. list_splice(&buffer_list, &trans->r_itemq);
  1817. if (!list_empty(&inode_list))
  1818. list_splice_tail(&inode_list, &trans->r_itemq);
  1819. if (!list_empty(&inode_buffer_list))
  1820. list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  1821. if (!list_empty(&cancel_list))
  1822. list_splice_tail(&cancel_list, &trans->r_itemq);
  1823. return error;
  1824. }
  1825. /*
  1826. * Build up the table of buf cancel records so that we don't replay
  1827. * cancelled data in the second pass. For buffer records that are
  1828. * not cancel records, there is nothing to do here so we just return.
  1829. *
  1830. * If we get a cancel record which is already in the table, this indicates
  1831. * that the buffer was cancelled multiple times. In order to ensure
  1832. * that during pass 2 we keep the record in the table until we reach its
  1833. * last occurrence in the log, we keep a reference count in the cancel
  1834. * record in the table to tell us how many times we expect to see this
  1835. * record during the second pass.
  1836. */
  1837. STATIC int
  1838. xlog_recover_buffer_pass1(
  1839. struct xlog *log,
  1840. struct xlog_recover_item *item)
  1841. {
  1842. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1843. struct list_head *bucket;
  1844. struct xfs_buf_cancel *bcp;
  1845. /*
  1846. * If this isn't a cancel buffer item, then just return.
  1847. */
  1848. if (!(buf_f->blf_flags & XFS_BLF_CANCEL)) {
  1849. trace_xfs_log_recover_buf_not_cancel(log, buf_f);
  1850. return 0;
  1851. }
  1852. /*
  1853. * Insert an xfs_buf_cancel record into the hash table of them.
  1854. * If there is already an identical record, bump its reference count.
  1855. */
  1856. bucket = XLOG_BUF_CANCEL_BUCKET(log, buf_f->blf_blkno);
  1857. list_for_each_entry(bcp, bucket, bc_list) {
  1858. if (bcp->bc_blkno == buf_f->blf_blkno &&
  1859. bcp->bc_len == buf_f->blf_len) {
  1860. bcp->bc_refcount++;
  1861. trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
  1862. return 0;
  1863. }
  1864. }
  1865. bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), KM_SLEEP);
  1866. bcp->bc_blkno = buf_f->blf_blkno;
  1867. bcp->bc_len = buf_f->blf_len;
  1868. bcp->bc_refcount = 1;
  1869. list_add_tail(&bcp->bc_list, bucket);
  1870. trace_xfs_log_recover_buf_cancel_add(log, buf_f);
  1871. return 0;
  1872. }
  1873. /*
  1874. * Check to see whether the buffer being recovered has a corresponding
  1875. * entry in the buffer cancel record table. If it is, return the cancel
  1876. * buffer structure to the caller.
  1877. */
  1878. STATIC struct xfs_buf_cancel *
  1879. xlog_peek_buffer_cancelled(
  1880. struct xlog *log,
  1881. xfs_daddr_t blkno,
  1882. uint len,
  1883. unsigned short flags)
  1884. {
  1885. struct list_head *bucket;
  1886. struct xfs_buf_cancel *bcp;
  1887. if (!log->l_buf_cancel_table) {
  1888. /* empty table means no cancelled buffers in the log */
  1889. ASSERT(!(flags & XFS_BLF_CANCEL));
  1890. return NULL;
  1891. }
  1892. bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
  1893. list_for_each_entry(bcp, bucket, bc_list) {
  1894. if (bcp->bc_blkno == blkno && bcp->bc_len == len)
  1895. return bcp;
  1896. }
  1897. /*
  1898. * We didn't find a corresponding entry in the table, so return 0 so
  1899. * that the buffer is NOT cancelled.
  1900. */
  1901. ASSERT(!(flags & XFS_BLF_CANCEL));
  1902. return NULL;
  1903. }
  1904. /*
  1905. * If the buffer is being cancelled then return 1 so that it will be cancelled,
  1906. * otherwise return 0. If the buffer is actually a buffer cancel item
  1907. * (XFS_BLF_CANCEL is set), then decrement the refcount on the entry in the
  1908. * table and remove it from the table if this is the last reference.
  1909. *
  1910. * We remove the cancel record from the table when we encounter its last
  1911. * occurrence in the log so that if the same buffer is re-used again after its
  1912. * last cancellation we actually replay the changes made at that point.
  1913. */
  1914. STATIC int
  1915. xlog_check_buffer_cancelled(
  1916. struct xlog *log,
  1917. xfs_daddr_t blkno,
  1918. uint len,
  1919. unsigned short flags)
  1920. {
  1921. struct xfs_buf_cancel *bcp;
  1922. bcp = xlog_peek_buffer_cancelled(log, blkno, len, flags);
  1923. if (!bcp)
  1924. return 0;
  1925. /*
  1926. * We've go a match, so return 1 so that the recovery of this buffer
  1927. * is cancelled. If this buffer is actually a buffer cancel log
  1928. * item, then decrement the refcount on the one in the table and
  1929. * remove it if this is the last reference.
  1930. */
  1931. if (flags & XFS_BLF_CANCEL) {
  1932. if (--bcp->bc_refcount == 0) {
  1933. list_del(&bcp->bc_list);
  1934. kmem_free(bcp);
  1935. }
  1936. }
  1937. return 1;
  1938. }
  1939. /*
  1940. * Perform recovery for a buffer full of inodes. In these buffers, the only
  1941. * data which should be recovered is that which corresponds to the
  1942. * di_next_unlinked pointers in the on disk inode structures. The rest of the
  1943. * data for the inodes is always logged through the inodes themselves rather
  1944. * than the inode buffer and is recovered in xlog_recover_inode_pass2().
  1945. *
  1946. * The only time when buffers full of inodes are fully recovered is when the
  1947. * buffer is full of newly allocated inodes. In this case the buffer will
  1948. * not be marked as an inode buffer and so will be sent to
  1949. * xlog_recover_do_reg_buffer() below during recovery.
  1950. */
  1951. STATIC int
  1952. xlog_recover_do_inode_buffer(
  1953. struct xfs_mount *mp,
  1954. xlog_recover_item_t *item,
  1955. struct xfs_buf *bp,
  1956. xfs_buf_log_format_t *buf_f)
  1957. {
  1958. int i;
  1959. int item_index = 0;
  1960. int bit = 0;
  1961. int nbits = 0;
  1962. int reg_buf_offset = 0;
  1963. int reg_buf_bytes = 0;
  1964. int next_unlinked_offset;
  1965. int inodes_per_buf;
  1966. xfs_agino_t *logged_nextp;
  1967. xfs_agino_t *buffer_nextp;
  1968. trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
  1969. /*
  1970. * Post recovery validation only works properly on CRC enabled
  1971. * filesystems.
  1972. */
  1973. if (xfs_sb_version_hascrc(&mp->m_sb))
  1974. bp->b_ops = &xfs_inode_buf_ops;
  1975. inodes_per_buf = BBTOB(bp->b_io_length) >> mp->m_sb.sb_inodelog;
  1976. for (i = 0; i < inodes_per_buf; i++) {
  1977. next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
  1978. offsetof(xfs_dinode_t, di_next_unlinked);
  1979. while (next_unlinked_offset >=
  1980. (reg_buf_offset + reg_buf_bytes)) {
  1981. /*
  1982. * The next di_next_unlinked field is beyond
  1983. * the current logged region. Find the next
  1984. * logged region that contains or is beyond
  1985. * the current di_next_unlinked field.
  1986. */
  1987. bit += nbits;
  1988. bit = xfs_next_bit(buf_f->blf_data_map,
  1989. buf_f->blf_map_size, bit);
  1990. /*
  1991. * If there are no more logged regions in the
  1992. * buffer, then we're done.
  1993. */
  1994. if (bit == -1)
  1995. return 0;
  1996. nbits = xfs_contig_bits(buf_f->blf_data_map,
  1997. buf_f->blf_map_size, bit);
  1998. ASSERT(nbits > 0);
  1999. reg_buf_offset = bit << XFS_BLF_SHIFT;
  2000. reg_buf_bytes = nbits << XFS_BLF_SHIFT;
  2001. item_index++;
  2002. }
  2003. /*
  2004. * If the current logged region starts after the current
  2005. * di_next_unlinked field, then move on to the next
  2006. * di_next_unlinked field.
  2007. */
  2008. if (next_unlinked_offset < reg_buf_offset)
  2009. continue;
  2010. ASSERT(item->ri_buf[item_index].i_addr != NULL);
  2011. ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
  2012. ASSERT((reg_buf_offset + reg_buf_bytes) <=
  2013. BBTOB(bp->b_io_length));
  2014. /*
  2015. * The current logged region contains a copy of the
  2016. * current di_next_unlinked field. Extract its value
  2017. * and copy it to the buffer copy.
  2018. */
  2019. logged_nextp = item->ri_buf[item_index].i_addr +
  2020. next_unlinked_offset - reg_buf_offset;
  2021. if (unlikely(*logged_nextp == 0)) {
  2022. xfs_alert(mp,
  2023. "Bad inode buffer log record (ptr = 0x%p, bp = 0x%p). "
  2024. "Trying to replay bad (0) inode di_next_unlinked field.",
  2025. item, bp);
  2026. XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
  2027. XFS_ERRLEVEL_LOW, mp);
  2028. return -EFSCORRUPTED;
  2029. }
  2030. buffer_nextp = xfs_buf_offset(bp, next_unlinked_offset);
  2031. *buffer_nextp = *logged_nextp;
  2032. /*
  2033. * If necessary, recalculate the CRC in the on-disk inode. We
  2034. * have to leave the inode in a consistent state for whoever
  2035. * reads it next....
  2036. */
  2037. xfs_dinode_calc_crc(mp,
  2038. xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
  2039. }
  2040. return 0;
  2041. }
  2042. /*
  2043. * V5 filesystems know the age of the buffer on disk being recovered. We can
  2044. * have newer objects on disk than we are replaying, and so for these cases we
  2045. * don't want to replay the current change as that will make the buffer contents
  2046. * temporarily invalid on disk.
  2047. *
  2048. * The magic number might not match the buffer type we are going to recover
  2049. * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
  2050. * extract the LSN of the existing object in the buffer based on it's current
  2051. * magic number. If we don't recognise the magic number in the buffer, then
  2052. * return a LSN of -1 so that the caller knows it was an unrecognised block and
  2053. * so can recover the buffer.
  2054. *
  2055. * Note: we cannot rely solely on magic number matches to determine that the
  2056. * buffer has a valid LSN - we also need to verify that it belongs to this
  2057. * filesystem, so we need to extract the object's LSN and compare it to that
  2058. * which we read from the superblock. If the UUIDs don't match, then we've got a
  2059. * stale metadata block from an old filesystem instance that we need to recover
  2060. * over the top of.
  2061. */
  2062. static xfs_lsn_t
  2063. xlog_recover_get_buf_lsn(
  2064. struct xfs_mount *mp,
  2065. struct xfs_buf *bp)
  2066. {
  2067. uint32_t magic32;
  2068. uint16_t magic16;
  2069. uint16_t magicda;
  2070. void *blk = bp->b_addr;
  2071. uuid_t *uuid;
  2072. xfs_lsn_t lsn = -1;
  2073. /* v4 filesystems always recover immediately */
  2074. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2075. goto recover_immediately;
  2076. magic32 = be32_to_cpu(*(__be32 *)blk);
  2077. switch (magic32) {
  2078. case XFS_ABTB_CRC_MAGIC:
  2079. case XFS_ABTC_CRC_MAGIC:
  2080. case XFS_ABTB_MAGIC:
  2081. case XFS_ABTC_MAGIC:
  2082. case XFS_RMAP_CRC_MAGIC:
  2083. case XFS_REFC_CRC_MAGIC:
  2084. case XFS_IBT_CRC_MAGIC:
  2085. case XFS_IBT_MAGIC: {
  2086. struct xfs_btree_block *btb = blk;
  2087. lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
  2088. uuid = &btb->bb_u.s.bb_uuid;
  2089. break;
  2090. }
  2091. case XFS_BMAP_CRC_MAGIC:
  2092. case XFS_BMAP_MAGIC: {
  2093. struct xfs_btree_block *btb = blk;
  2094. lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
  2095. uuid = &btb->bb_u.l.bb_uuid;
  2096. break;
  2097. }
  2098. case XFS_AGF_MAGIC:
  2099. lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
  2100. uuid = &((struct xfs_agf *)blk)->agf_uuid;
  2101. break;
  2102. case XFS_AGFL_MAGIC:
  2103. lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
  2104. uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
  2105. break;
  2106. case XFS_AGI_MAGIC:
  2107. lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
  2108. uuid = &((struct xfs_agi *)blk)->agi_uuid;
  2109. break;
  2110. case XFS_SYMLINK_MAGIC:
  2111. lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
  2112. uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
  2113. break;
  2114. case XFS_DIR3_BLOCK_MAGIC:
  2115. case XFS_DIR3_DATA_MAGIC:
  2116. case XFS_DIR3_FREE_MAGIC:
  2117. lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
  2118. uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
  2119. break;
  2120. case XFS_ATTR3_RMT_MAGIC:
  2121. /*
  2122. * Remote attr blocks are written synchronously, rather than
  2123. * being logged. That means they do not contain a valid LSN
  2124. * (i.e. transactionally ordered) in them, and hence any time we
  2125. * see a buffer to replay over the top of a remote attribute
  2126. * block we should simply do so.
  2127. */
  2128. goto recover_immediately;
  2129. case XFS_SB_MAGIC:
  2130. /*
  2131. * superblock uuids are magic. We may or may not have a
  2132. * sb_meta_uuid on disk, but it will be set in the in-core
  2133. * superblock. We set the uuid pointer for verification
  2134. * according to the superblock feature mask to ensure we check
  2135. * the relevant UUID in the superblock.
  2136. */
  2137. lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
  2138. if (xfs_sb_version_hasmetauuid(&mp->m_sb))
  2139. uuid = &((struct xfs_dsb *)blk)->sb_meta_uuid;
  2140. else
  2141. uuid = &((struct xfs_dsb *)blk)->sb_uuid;
  2142. break;
  2143. default:
  2144. break;
  2145. }
  2146. if (lsn != (xfs_lsn_t)-1) {
  2147. if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
  2148. goto recover_immediately;
  2149. return lsn;
  2150. }
  2151. magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
  2152. switch (magicda) {
  2153. case XFS_DIR3_LEAF1_MAGIC:
  2154. case XFS_DIR3_LEAFN_MAGIC:
  2155. case XFS_DA3_NODE_MAGIC:
  2156. lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
  2157. uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
  2158. break;
  2159. default:
  2160. break;
  2161. }
  2162. if (lsn != (xfs_lsn_t)-1) {
  2163. if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
  2164. goto recover_immediately;
  2165. return lsn;
  2166. }
  2167. /*
  2168. * We do individual object checks on dquot and inode buffers as they
  2169. * have their own individual LSN records. Also, we could have a stale
  2170. * buffer here, so we have to at least recognise these buffer types.
  2171. *
  2172. * A notd complexity here is inode unlinked list processing - it logs
  2173. * the inode directly in the buffer, but we don't know which inodes have
  2174. * been modified, and there is no global buffer LSN. Hence we need to
  2175. * recover all inode buffer types immediately. This problem will be
  2176. * fixed by logical logging of the unlinked list modifications.
  2177. */
  2178. magic16 = be16_to_cpu(*(__be16 *)blk);
  2179. switch (magic16) {
  2180. case XFS_DQUOT_MAGIC:
  2181. case XFS_DINODE_MAGIC:
  2182. goto recover_immediately;
  2183. default:
  2184. break;
  2185. }
  2186. /* unknown buffer contents, recover immediately */
  2187. recover_immediately:
  2188. return (xfs_lsn_t)-1;
  2189. }
  2190. /*
  2191. * Validate the recovered buffer is of the correct type and attach the
  2192. * appropriate buffer operations to them for writeback. Magic numbers are in a
  2193. * few places:
  2194. * the first 16 bits of the buffer (inode buffer, dquot buffer),
  2195. * the first 32 bits of the buffer (most blocks),
  2196. * inside a struct xfs_da_blkinfo at the start of the buffer.
  2197. */
  2198. static void
  2199. xlog_recover_validate_buf_type(
  2200. struct xfs_mount *mp,
  2201. struct xfs_buf *bp,
  2202. xfs_buf_log_format_t *buf_f,
  2203. xfs_lsn_t current_lsn)
  2204. {
  2205. struct xfs_da_blkinfo *info = bp->b_addr;
  2206. uint32_t magic32;
  2207. uint16_t magic16;
  2208. uint16_t magicda;
  2209. char *warnmsg = NULL;
  2210. /*
  2211. * We can only do post recovery validation on items on CRC enabled
  2212. * fielsystems as we need to know when the buffer was written to be able
  2213. * to determine if we should have replayed the item. If we replay old
  2214. * metadata over a newer buffer, then it will enter a temporarily
  2215. * inconsistent state resulting in verification failures. Hence for now
  2216. * just avoid the verification stage for non-crc filesystems
  2217. */
  2218. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2219. return;
  2220. magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
  2221. magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
  2222. magicda = be16_to_cpu(info->magic);
  2223. switch (xfs_blft_from_flags(buf_f)) {
  2224. case XFS_BLFT_BTREE_BUF:
  2225. switch (magic32) {
  2226. case XFS_ABTB_CRC_MAGIC:
  2227. case XFS_ABTC_CRC_MAGIC:
  2228. case XFS_ABTB_MAGIC:
  2229. case XFS_ABTC_MAGIC:
  2230. bp->b_ops = &xfs_allocbt_buf_ops;
  2231. break;
  2232. case XFS_IBT_CRC_MAGIC:
  2233. case XFS_FIBT_CRC_MAGIC:
  2234. case XFS_IBT_MAGIC:
  2235. case XFS_FIBT_MAGIC:
  2236. bp->b_ops = &xfs_inobt_buf_ops;
  2237. break;
  2238. case XFS_BMAP_CRC_MAGIC:
  2239. case XFS_BMAP_MAGIC:
  2240. bp->b_ops = &xfs_bmbt_buf_ops;
  2241. break;
  2242. case XFS_RMAP_CRC_MAGIC:
  2243. bp->b_ops = &xfs_rmapbt_buf_ops;
  2244. break;
  2245. case XFS_REFC_CRC_MAGIC:
  2246. bp->b_ops = &xfs_refcountbt_buf_ops;
  2247. break;
  2248. default:
  2249. warnmsg = "Bad btree block magic!";
  2250. break;
  2251. }
  2252. break;
  2253. case XFS_BLFT_AGF_BUF:
  2254. if (magic32 != XFS_AGF_MAGIC) {
  2255. warnmsg = "Bad AGF block magic!";
  2256. break;
  2257. }
  2258. bp->b_ops = &xfs_agf_buf_ops;
  2259. break;
  2260. case XFS_BLFT_AGFL_BUF:
  2261. if (magic32 != XFS_AGFL_MAGIC) {
  2262. warnmsg = "Bad AGFL block magic!";
  2263. break;
  2264. }
  2265. bp->b_ops = &xfs_agfl_buf_ops;
  2266. break;
  2267. case XFS_BLFT_AGI_BUF:
  2268. if (magic32 != XFS_AGI_MAGIC) {
  2269. warnmsg = "Bad AGI block magic!";
  2270. break;
  2271. }
  2272. bp->b_ops = &xfs_agi_buf_ops;
  2273. break;
  2274. case XFS_BLFT_UDQUOT_BUF:
  2275. case XFS_BLFT_PDQUOT_BUF:
  2276. case XFS_BLFT_GDQUOT_BUF:
  2277. #ifdef CONFIG_XFS_QUOTA
  2278. if (magic16 != XFS_DQUOT_MAGIC) {
  2279. warnmsg = "Bad DQUOT block magic!";
  2280. break;
  2281. }
  2282. bp->b_ops = &xfs_dquot_buf_ops;
  2283. #else
  2284. xfs_alert(mp,
  2285. "Trying to recover dquots without QUOTA support built in!");
  2286. ASSERT(0);
  2287. #endif
  2288. break;
  2289. case XFS_BLFT_DINO_BUF:
  2290. if (magic16 != XFS_DINODE_MAGIC) {
  2291. warnmsg = "Bad INODE block magic!";
  2292. break;
  2293. }
  2294. bp->b_ops = &xfs_inode_buf_ops;
  2295. break;
  2296. case XFS_BLFT_SYMLINK_BUF:
  2297. if (magic32 != XFS_SYMLINK_MAGIC) {
  2298. warnmsg = "Bad symlink block magic!";
  2299. break;
  2300. }
  2301. bp->b_ops = &xfs_symlink_buf_ops;
  2302. break;
  2303. case XFS_BLFT_DIR_BLOCK_BUF:
  2304. if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
  2305. magic32 != XFS_DIR3_BLOCK_MAGIC) {
  2306. warnmsg = "Bad dir block magic!";
  2307. break;
  2308. }
  2309. bp->b_ops = &xfs_dir3_block_buf_ops;
  2310. break;
  2311. case XFS_BLFT_DIR_DATA_BUF:
  2312. if (magic32 != XFS_DIR2_DATA_MAGIC &&
  2313. magic32 != XFS_DIR3_DATA_MAGIC) {
  2314. warnmsg = "Bad dir data magic!";
  2315. break;
  2316. }
  2317. bp->b_ops = &xfs_dir3_data_buf_ops;
  2318. break;
  2319. case XFS_BLFT_DIR_FREE_BUF:
  2320. if (magic32 != XFS_DIR2_FREE_MAGIC &&
  2321. magic32 != XFS_DIR3_FREE_MAGIC) {
  2322. warnmsg = "Bad dir3 free magic!";
  2323. break;
  2324. }
  2325. bp->b_ops = &xfs_dir3_free_buf_ops;
  2326. break;
  2327. case XFS_BLFT_DIR_LEAF1_BUF:
  2328. if (magicda != XFS_DIR2_LEAF1_MAGIC &&
  2329. magicda != XFS_DIR3_LEAF1_MAGIC) {
  2330. warnmsg = "Bad dir leaf1 magic!";
  2331. break;
  2332. }
  2333. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  2334. break;
  2335. case XFS_BLFT_DIR_LEAFN_BUF:
  2336. if (magicda != XFS_DIR2_LEAFN_MAGIC &&
  2337. magicda != XFS_DIR3_LEAFN_MAGIC) {
  2338. warnmsg = "Bad dir leafn magic!";
  2339. break;
  2340. }
  2341. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  2342. break;
  2343. case XFS_BLFT_DA_NODE_BUF:
  2344. if (magicda != XFS_DA_NODE_MAGIC &&
  2345. magicda != XFS_DA3_NODE_MAGIC) {
  2346. warnmsg = "Bad da node magic!";
  2347. break;
  2348. }
  2349. bp->b_ops = &xfs_da3_node_buf_ops;
  2350. break;
  2351. case XFS_BLFT_ATTR_LEAF_BUF:
  2352. if (magicda != XFS_ATTR_LEAF_MAGIC &&
  2353. magicda != XFS_ATTR3_LEAF_MAGIC) {
  2354. warnmsg = "Bad attr leaf magic!";
  2355. break;
  2356. }
  2357. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  2358. break;
  2359. case XFS_BLFT_ATTR_RMT_BUF:
  2360. if (magic32 != XFS_ATTR3_RMT_MAGIC) {
  2361. warnmsg = "Bad attr remote magic!";
  2362. break;
  2363. }
  2364. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  2365. break;
  2366. case XFS_BLFT_SB_BUF:
  2367. if (magic32 != XFS_SB_MAGIC) {
  2368. warnmsg = "Bad SB block magic!";
  2369. break;
  2370. }
  2371. bp->b_ops = &xfs_sb_buf_ops;
  2372. break;
  2373. #ifdef CONFIG_XFS_RT
  2374. case XFS_BLFT_RTBITMAP_BUF:
  2375. case XFS_BLFT_RTSUMMARY_BUF:
  2376. /* no magic numbers for verification of RT buffers */
  2377. bp->b_ops = &xfs_rtbuf_ops;
  2378. break;
  2379. #endif /* CONFIG_XFS_RT */
  2380. default:
  2381. xfs_warn(mp, "Unknown buffer type %d!",
  2382. xfs_blft_from_flags(buf_f));
  2383. break;
  2384. }
  2385. /*
  2386. * Nothing else to do in the case of a NULL current LSN as this means
  2387. * the buffer is more recent than the change in the log and will be
  2388. * skipped.
  2389. */
  2390. if (current_lsn == NULLCOMMITLSN)
  2391. return;
  2392. if (warnmsg) {
  2393. xfs_warn(mp, warnmsg);
  2394. ASSERT(0);
  2395. }
  2396. /*
  2397. * We must update the metadata LSN of the buffer as it is written out to
  2398. * ensure that older transactions never replay over this one and corrupt
  2399. * the buffer. This can occur if log recovery is interrupted at some
  2400. * point after the current transaction completes, at which point a
  2401. * subsequent mount starts recovery from the beginning.
  2402. *
  2403. * Write verifiers update the metadata LSN from log items attached to
  2404. * the buffer. Therefore, initialize a bli purely to carry the LSN to
  2405. * the verifier. We'll clean it up in our ->iodone() callback.
  2406. */
  2407. if (bp->b_ops) {
  2408. struct xfs_buf_log_item *bip;
  2409. ASSERT(!bp->b_iodone || bp->b_iodone == xlog_recover_iodone);
  2410. bp->b_iodone = xlog_recover_iodone;
  2411. xfs_buf_item_init(bp, mp);
  2412. bip = bp->b_fspriv;
  2413. bip->bli_item.li_lsn = current_lsn;
  2414. }
  2415. }
  2416. /*
  2417. * Perform a 'normal' buffer recovery. Each logged region of the
  2418. * buffer should be copied over the corresponding region in the
  2419. * given buffer. The bitmap in the buf log format structure indicates
  2420. * where to place the logged data.
  2421. */
  2422. STATIC void
  2423. xlog_recover_do_reg_buffer(
  2424. struct xfs_mount *mp,
  2425. xlog_recover_item_t *item,
  2426. struct xfs_buf *bp,
  2427. xfs_buf_log_format_t *buf_f,
  2428. xfs_lsn_t current_lsn)
  2429. {
  2430. int i;
  2431. int bit;
  2432. int nbits;
  2433. int error;
  2434. trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
  2435. bit = 0;
  2436. i = 1; /* 0 is the buf format structure */
  2437. while (1) {
  2438. bit = xfs_next_bit(buf_f->blf_data_map,
  2439. buf_f->blf_map_size, bit);
  2440. if (bit == -1)
  2441. break;
  2442. nbits = xfs_contig_bits(buf_f->blf_data_map,
  2443. buf_f->blf_map_size, bit);
  2444. ASSERT(nbits > 0);
  2445. ASSERT(item->ri_buf[i].i_addr != NULL);
  2446. ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
  2447. ASSERT(BBTOB(bp->b_io_length) >=
  2448. ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
  2449. /*
  2450. * The dirty regions logged in the buffer, even though
  2451. * contiguous, may span multiple chunks. This is because the
  2452. * dirty region may span a physical page boundary in a buffer
  2453. * and hence be split into two separate vectors for writing into
  2454. * the log. Hence we need to trim nbits back to the length of
  2455. * the current region being copied out of the log.
  2456. */
  2457. if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
  2458. nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
  2459. /*
  2460. * Do a sanity check if this is a dquot buffer. Just checking
  2461. * the first dquot in the buffer should do. XXXThis is
  2462. * probably a good thing to do for other buf types also.
  2463. */
  2464. error = 0;
  2465. if (buf_f->blf_flags &
  2466. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2467. if (item->ri_buf[i].i_addr == NULL) {
  2468. xfs_alert(mp,
  2469. "XFS: NULL dquot in %s.", __func__);
  2470. goto next;
  2471. }
  2472. if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
  2473. xfs_alert(mp,
  2474. "XFS: dquot too small (%d) in %s.",
  2475. item->ri_buf[i].i_len, __func__);
  2476. goto next;
  2477. }
  2478. error = xfs_dqcheck(mp, item->ri_buf[i].i_addr,
  2479. -1, 0, XFS_QMOPT_DOWARN,
  2480. "dquot_buf_recover");
  2481. if (error)
  2482. goto next;
  2483. }
  2484. memcpy(xfs_buf_offset(bp,
  2485. (uint)bit << XFS_BLF_SHIFT), /* dest */
  2486. item->ri_buf[i].i_addr, /* source */
  2487. nbits<<XFS_BLF_SHIFT); /* length */
  2488. next:
  2489. i++;
  2490. bit += nbits;
  2491. }
  2492. /* Shouldn't be any more regions */
  2493. ASSERT(i == item->ri_total);
  2494. xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
  2495. }
  2496. /*
  2497. * Perform a dquot buffer recovery.
  2498. * Simple algorithm: if we have found a QUOTAOFF log item of the same type
  2499. * (ie. USR or GRP), then just toss this buffer away; don't recover it.
  2500. * Else, treat it as a regular buffer and do recovery.
  2501. *
  2502. * Return false if the buffer was tossed and true if we recovered the buffer to
  2503. * indicate to the caller if the buffer needs writing.
  2504. */
  2505. STATIC bool
  2506. xlog_recover_do_dquot_buffer(
  2507. struct xfs_mount *mp,
  2508. struct xlog *log,
  2509. struct xlog_recover_item *item,
  2510. struct xfs_buf *bp,
  2511. struct xfs_buf_log_format *buf_f)
  2512. {
  2513. uint type;
  2514. trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
  2515. /*
  2516. * Filesystems are required to send in quota flags at mount time.
  2517. */
  2518. if (!mp->m_qflags)
  2519. return false;
  2520. type = 0;
  2521. if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
  2522. type |= XFS_DQ_USER;
  2523. if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
  2524. type |= XFS_DQ_PROJ;
  2525. if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
  2526. type |= XFS_DQ_GROUP;
  2527. /*
  2528. * This type of quotas was turned off, so ignore this buffer
  2529. */
  2530. if (log->l_quotaoffs_flag & type)
  2531. return false;
  2532. xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
  2533. return true;
  2534. }
  2535. /*
  2536. * This routine replays a modification made to a buffer at runtime.
  2537. * There are actually two types of buffer, regular and inode, which
  2538. * are handled differently. Inode buffers are handled differently
  2539. * in that we only recover a specific set of data from them, namely
  2540. * the inode di_next_unlinked fields. This is because all other inode
  2541. * data is actually logged via inode records and any data we replay
  2542. * here which overlaps that may be stale.
  2543. *
  2544. * When meta-data buffers are freed at run time we log a buffer item
  2545. * with the XFS_BLF_CANCEL bit set to indicate that previous copies
  2546. * of the buffer in the log should not be replayed at recovery time.
  2547. * This is so that if the blocks covered by the buffer are reused for
  2548. * file data before we crash we don't end up replaying old, freed
  2549. * meta-data into a user's file.
  2550. *
  2551. * To handle the cancellation of buffer log items, we make two passes
  2552. * over the log during recovery. During the first we build a table of
  2553. * those buffers which have been cancelled, and during the second we
  2554. * only replay those buffers which do not have corresponding cancel
  2555. * records in the table. See xlog_recover_buffer_pass[1,2] above
  2556. * for more details on the implementation of the table of cancel records.
  2557. */
  2558. STATIC int
  2559. xlog_recover_buffer_pass2(
  2560. struct xlog *log,
  2561. struct list_head *buffer_list,
  2562. struct xlog_recover_item *item,
  2563. xfs_lsn_t current_lsn)
  2564. {
  2565. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  2566. xfs_mount_t *mp = log->l_mp;
  2567. xfs_buf_t *bp;
  2568. int error;
  2569. uint buf_flags;
  2570. xfs_lsn_t lsn;
  2571. /*
  2572. * In this pass we only want to recover all the buffers which have
  2573. * not been cancelled and are not cancellation buffers themselves.
  2574. */
  2575. if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
  2576. buf_f->blf_len, buf_f->blf_flags)) {
  2577. trace_xfs_log_recover_buf_cancel(log, buf_f);
  2578. return 0;
  2579. }
  2580. trace_xfs_log_recover_buf_recover(log, buf_f);
  2581. buf_flags = 0;
  2582. if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
  2583. buf_flags |= XBF_UNMAPPED;
  2584. bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
  2585. buf_flags, NULL);
  2586. if (!bp)
  2587. return -ENOMEM;
  2588. error = bp->b_error;
  2589. if (error) {
  2590. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
  2591. goto out_release;
  2592. }
  2593. /*
  2594. * Recover the buffer only if we get an LSN from it and it's less than
  2595. * the lsn of the transaction we are replaying.
  2596. *
  2597. * Note that we have to be extremely careful of readahead here.
  2598. * Readahead does not attach verfiers to the buffers so if we don't
  2599. * actually do any replay after readahead because of the LSN we found
  2600. * in the buffer if more recent than that current transaction then we
  2601. * need to attach the verifier directly. Failure to do so can lead to
  2602. * future recovery actions (e.g. EFI and unlinked list recovery) can
  2603. * operate on the buffers and they won't get the verifier attached. This
  2604. * can lead to blocks on disk having the correct content but a stale
  2605. * CRC.
  2606. *
  2607. * It is safe to assume these clean buffers are currently up to date.
  2608. * If the buffer is dirtied by a later transaction being replayed, then
  2609. * the verifier will be reset to match whatever recover turns that
  2610. * buffer into.
  2611. */
  2612. lsn = xlog_recover_get_buf_lsn(mp, bp);
  2613. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2614. trace_xfs_log_recover_buf_skip(log, buf_f);
  2615. xlog_recover_validate_buf_type(mp, bp, buf_f, NULLCOMMITLSN);
  2616. goto out_release;
  2617. }
  2618. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  2619. error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
  2620. if (error)
  2621. goto out_release;
  2622. } else if (buf_f->blf_flags &
  2623. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2624. bool dirty;
  2625. dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
  2626. if (!dirty)
  2627. goto out_release;
  2628. } else {
  2629. xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
  2630. }
  2631. /*
  2632. * Perform delayed write on the buffer. Asynchronous writes will be
  2633. * slower when taking into account all the buffers to be flushed.
  2634. *
  2635. * Also make sure that only inode buffers with good sizes stay in
  2636. * the buffer cache. The kernel moves inodes in buffers of 1 block
  2637. * or mp->m_inode_cluster_size bytes, whichever is bigger. The inode
  2638. * buffers in the log can be a different size if the log was generated
  2639. * by an older kernel using unclustered inode buffers or a newer kernel
  2640. * running with a different inode cluster size. Regardless, if the
  2641. * the inode buffer size isn't MAX(blocksize, mp->m_inode_cluster_size)
  2642. * for *our* value of mp->m_inode_cluster_size, then we need to keep
  2643. * the buffer out of the buffer cache so that the buffer won't
  2644. * overlap with future reads of those inodes.
  2645. */
  2646. if (XFS_DINODE_MAGIC ==
  2647. be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
  2648. (BBTOB(bp->b_io_length) != MAX(log->l_mp->m_sb.sb_blocksize,
  2649. (uint32_t)log->l_mp->m_inode_cluster_size))) {
  2650. xfs_buf_stale(bp);
  2651. error = xfs_bwrite(bp);
  2652. } else {
  2653. ASSERT(bp->b_target->bt_mount == mp);
  2654. bp->b_iodone = xlog_recover_iodone;
  2655. xfs_buf_delwri_queue(bp, buffer_list);
  2656. }
  2657. out_release:
  2658. xfs_buf_relse(bp);
  2659. return error;
  2660. }
  2661. /*
  2662. * Inode fork owner changes
  2663. *
  2664. * If we have been told that we have to reparent the inode fork, it's because an
  2665. * extent swap operation on a CRC enabled filesystem has been done and we are
  2666. * replaying it. We need to walk the BMBT of the appropriate fork and change the
  2667. * owners of it.
  2668. *
  2669. * The complexity here is that we don't have an inode context to work with, so
  2670. * after we've replayed the inode we need to instantiate one. This is where the
  2671. * fun begins.
  2672. *
  2673. * We are in the middle of log recovery, so we can't run transactions. That
  2674. * means we cannot use cache coherent inode instantiation via xfs_iget(), as
  2675. * that will result in the corresponding iput() running the inode through
  2676. * xfs_inactive(). If we've just replayed an inode core that changes the link
  2677. * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
  2678. * transactions (bad!).
  2679. *
  2680. * So, to avoid this, we instantiate an inode directly from the inode core we've
  2681. * just recovered. We have the buffer still locked, and all we really need to
  2682. * instantiate is the inode core and the forks being modified. We can do this
  2683. * manually, then run the inode btree owner change, and then tear down the
  2684. * xfs_inode without having to run any transactions at all.
  2685. *
  2686. * Also, because we don't have a transaction context available here but need to
  2687. * gather all the buffers we modify for writeback so we pass the buffer_list
  2688. * instead for the operation to use.
  2689. */
  2690. STATIC int
  2691. xfs_recover_inode_owner_change(
  2692. struct xfs_mount *mp,
  2693. struct xfs_dinode *dip,
  2694. struct xfs_inode_log_format *in_f,
  2695. struct list_head *buffer_list)
  2696. {
  2697. struct xfs_inode *ip;
  2698. int error;
  2699. ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
  2700. ip = xfs_inode_alloc(mp, in_f->ilf_ino);
  2701. if (!ip)
  2702. return -ENOMEM;
  2703. /* instantiate the inode */
  2704. xfs_inode_from_disk(ip, dip);
  2705. ASSERT(ip->i_d.di_version >= 3);
  2706. error = xfs_iformat_fork(ip, dip);
  2707. if (error)
  2708. goto out_free_ip;
  2709. if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
  2710. ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
  2711. error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
  2712. ip->i_ino, buffer_list);
  2713. if (error)
  2714. goto out_free_ip;
  2715. }
  2716. if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
  2717. ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
  2718. error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
  2719. ip->i_ino, buffer_list);
  2720. if (error)
  2721. goto out_free_ip;
  2722. }
  2723. out_free_ip:
  2724. xfs_inode_free(ip);
  2725. return error;
  2726. }
  2727. STATIC int
  2728. xlog_recover_inode_pass2(
  2729. struct xlog *log,
  2730. struct list_head *buffer_list,
  2731. struct xlog_recover_item *item,
  2732. xfs_lsn_t current_lsn)
  2733. {
  2734. struct xfs_inode_log_format *in_f;
  2735. xfs_mount_t *mp = log->l_mp;
  2736. xfs_buf_t *bp;
  2737. xfs_dinode_t *dip;
  2738. int len;
  2739. char *src;
  2740. char *dest;
  2741. int error;
  2742. int attr_index;
  2743. uint fields;
  2744. struct xfs_log_dinode *ldip;
  2745. uint isize;
  2746. int need_free = 0;
  2747. if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
  2748. in_f = item->ri_buf[0].i_addr;
  2749. } else {
  2750. in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), KM_SLEEP);
  2751. need_free = 1;
  2752. error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
  2753. if (error)
  2754. goto error;
  2755. }
  2756. /*
  2757. * Inode buffers can be freed, look out for it,
  2758. * and do not replay the inode.
  2759. */
  2760. if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
  2761. in_f->ilf_len, 0)) {
  2762. error = 0;
  2763. trace_xfs_log_recover_inode_cancel(log, in_f);
  2764. goto error;
  2765. }
  2766. trace_xfs_log_recover_inode_recover(log, in_f);
  2767. bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
  2768. &xfs_inode_buf_ops);
  2769. if (!bp) {
  2770. error = -ENOMEM;
  2771. goto error;
  2772. }
  2773. error = bp->b_error;
  2774. if (error) {
  2775. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
  2776. goto out_release;
  2777. }
  2778. ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
  2779. dip = xfs_buf_offset(bp, in_f->ilf_boffset);
  2780. /*
  2781. * Make sure the place we're flushing out to really looks
  2782. * like an inode!
  2783. */
  2784. if (unlikely(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))) {
  2785. xfs_alert(mp,
  2786. "%s: Bad inode magic number, dip = 0x%p, dino bp = 0x%p, ino = %Ld",
  2787. __func__, dip, bp, in_f->ilf_ino);
  2788. XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
  2789. XFS_ERRLEVEL_LOW, mp);
  2790. error = -EFSCORRUPTED;
  2791. goto out_release;
  2792. }
  2793. ldip = item->ri_buf[1].i_addr;
  2794. if (unlikely(ldip->di_magic != XFS_DINODE_MAGIC)) {
  2795. xfs_alert(mp,
  2796. "%s: Bad inode log record, rec ptr 0x%p, ino %Ld",
  2797. __func__, item, in_f->ilf_ino);
  2798. XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
  2799. XFS_ERRLEVEL_LOW, mp);
  2800. error = -EFSCORRUPTED;
  2801. goto out_release;
  2802. }
  2803. /*
  2804. * If the inode has an LSN in it, recover the inode only if it's less
  2805. * than the lsn of the transaction we are replaying. Note: we still
  2806. * need to replay an owner change even though the inode is more recent
  2807. * than the transaction as there is no guarantee that all the btree
  2808. * blocks are more recent than this transaction, too.
  2809. */
  2810. if (dip->di_version >= 3) {
  2811. xfs_lsn_t lsn = be64_to_cpu(dip->di_lsn);
  2812. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2813. trace_xfs_log_recover_inode_skip(log, in_f);
  2814. error = 0;
  2815. goto out_owner_change;
  2816. }
  2817. }
  2818. /*
  2819. * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
  2820. * are transactional and if ordering is necessary we can determine that
  2821. * more accurately by the LSN field in the V3 inode core. Don't trust
  2822. * the inode versions we might be changing them here - use the
  2823. * superblock flag to determine whether we need to look at di_flushiter
  2824. * to skip replay when the on disk inode is newer than the log one
  2825. */
  2826. if (!xfs_sb_version_hascrc(&mp->m_sb) &&
  2827. ldip->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
  2828. /*
  2829. * Deal with the wrap case, DI_MAX_FLUSH is less
  2830. * than smaller numbers
  2831. */
  2832. if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
  2833. ldip->di_flushiter < (DI_MAX_FLUSH >> 1)) {
  2834. /* do nothing */
  2835. } else {
  2836. trace_xfs_log_recover_inode_skip(log, in_f);
  2837. error = 0;
  2838. goto out_release;
  2839. }
  2840. }
  2841. /* Take the opportunity to reset the flush iteration count */
  2842. ldip->di_flushiter = 0;
  2843. if (unlikely(S_ISREG(ldip->di_mode))) {
  2844. if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2845. (ldip->di_format != XFS_DINODE_FMT_BTREE)) {
  2846. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
  2847. XFS_ERRLEVEL_LOW, mp, ldip);
  2848. xfs_alert(mp,
  2849. "%s: Bad regular inode log record, rec ptr 0x%p, "
  2850. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2851. __func__, item, dip, bp, in_f->ilf_ino);
  2852. error = -EFSCORRUPTED;
  2853. goto out_release;
  2854. }
  2855. } else if (unlikely(S_ISDIR(ldip->di_mode))) {
  2856. if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2857. (ldip->di_format != XFS_DINODE_FMT_BTREE) &&
  2858. (ldip->di_format != XFS_DINODE_FMT_LOCAL)) {
  2859. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
  2860. XFS_ERRLEVEL_LOW, mp, ldip);
  2861. xfs_alert(mp,
  2862. "%s: Bad dir inode log record, rec ptr 0x%p, "
  2863. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2864. __func__, item, dip, bp, in_f->ilf_ino);
  2865. error = -EFSCORRUPTED;
  2866. goto out_release;
  2867. }
  2868. }
  2869. if (unlikely(ldip->di_nextents + ldip->di_anextents > ldip->di_nblocks)){
  2870. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
  2871. XFS_ERRLEVEL_LOW, mp, ldip);
  2872. xfs_alert(mp,
  2873. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2874. "dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
  2875. __func__, item, dip, bp, in_f->ilf_ino,
  2876. ldip->di_nextents + ldip->di_anextents,
  2877. ldip->di_nblocks);
  2878. error = -EFSCORRUPTED;
  2879. goto out_release;
  2880. }
  2881. if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {
  2882. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
  2883. XFS_ERRLEVEL_LOW, mp, ldip);
  2884. xfs_alert(mp,
  2885. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2886. "dino bp 0x%p, ino %Ld, forkoff 0x%x", __func__,
  2887. item, dip, bp, in_f->ilf_ino, ldip->di_forkoff);
  2888. error = -EFSCORRUPTED;
  2889. goto out_release;
  2890. }
  2891. isize = xfs_log_dinode_size(ldip->di_version);
  2892. if (unlikely(item->ri_buf[1].i_len > isize)) {
  2893. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
  2894. XFS_ERRLEVEL_LOW, mp, ldip);
  2895. xfs_alert(mp,
  2896. "%s: Bad inode log record length %d, rec ptr 0x%p",
  2897. __func__, item->ri_buf[1].i_len, item);
  2898. error = -EFSCORRUPTED;
  2899. goto out_release;
  2900. }
  2901. /* recover the log dinode inode into the on disk inode */
  2902. xfs_log_dinode_to_disk(ldip, dip);
  2903. /* the rest is in on-disk format */
  2904. if (item->ri_buf[1].i_len > isize) {
  2905. memcpy((char *)dip + isize,
  2906. item->ri_buf[1].i_addr + isize,
  2907. item->ri_buf[1].i_len - isize);
  2908. }
  2909. fields = in_f->ilf_fields;
  2910. if (fields & XFS_ILOG_DEV)
  2911. xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
  2912. if (in_f->ilf_size == 2)
  2913. goto out_owner_change;
  2914. len = item->ri_buf[2].i_len;
  2915. src = item->ri_buf[2].i_addr;
  2916. ASSERT(in_f->ilf_size <= 4);
  2917. ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
  2918. ASSERT(!(fields & XFS_ILOG_DFORK) ||
  2919. (len == in_f->ilf_dsize));
  2920. switch (fields & XFS_ILOG_DFORK) {
  2921. case XFS_ILOG_DDATA:
  2922. case XFS_ILOG_DEXT:
  2923. memcpy(XFS_DFORK_DPTR(dip), src, len);
  2924. break;
  2925. case XFS_ILOG_DBROOT:
  2926. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
  2927. (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
  2928. XFS_DFORK_DSIZE(dip, mp));
  2929. break;
  2930. default:
  2931. /*
  2932. * There are no data fork flags set.
  2933. */
  2934. ASSERT((fields & XFS_ILOG_DFORK) == 0);
  2935. break;
  2936. }
  2937. /*
  2938. * If we logged any attribute data, recover it. There may or
  2939. * may not have been any other non-core data logged in this
  2940. * transaction.
  2941. */
  2942. if (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2943. if (in_f->ilf_fields & XFS_ILOG_DFORK) {
  2944. attr_index = 3;
  2945. } else {
  2946. attr_index = 2;
  2947. }
  2948. len = item->ri_buf[attr_index].i_len;
  2949. src = item->ri_buf[attr_index].i_addr;
  2950. ASSERT(len == in_f->ilf_asize);
  2951. switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2952. case XFS_ILOG_ADATA:
  2953. case XFS_ILOG_AEXT:
  2954. dest = XFS_DFORK_APTR(dip);
  2955. ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
  2956. memcpy(dest, src, len);
  2957. break;
  2958. case XFS_ILOG_ABROOT:
  2959. dest = XFS_DFORK_APTR(dip);
  2960. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
  2961. len, (xfs_bmdr_block_t*)dest,
  2962. XFS_DFORK_ASIZE(dip, mp));
  2963. break;
  2964. default:
  2965. xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
  2966. ASSERT(0);
  2967. error = -EIO;
  2968. goto out_release;
  2969. }
  2970. }
  2971. out_owner_change:
  2972. if (in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER))
  2973. error = xfs_recover_inode_owner_change(mp, dip, in_f,
  2974. buffer_list);
  2975. /* re-generate the checksum. */
  2976. xfs_dinode_calc_crc(log->l_mp, dip);
  2977. ASSERT(bp->b_target->bt_mount == mp);
  2978. bp->b_iodone = xlog_recover_iodone;
  2979. xfs_buf_delwri_queue(bp, buffer_list);
  2980. out_release:
  2981. xfs_buf_relse(bp);
  2982. error:
  2983. if (need_free)
  2984. kmem_free(in_f);
  2985. return error;
  2986. }
  2987. /*
  2988. * Recover QUOTAOFF records. We simply make a note of it in the xlog
  2989. * structure, so that we know not to do any dquot item or dquot buffer recovery,
  2990. * of that type.
  2991. */
  2992. STATIC int
  2993. xlog_recover_quotaoff_pass1(
  2994. struct xlog *log,
  2995. struct xlog_recover_item *item)
  2996. {
  2997. xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
  2998. ASSERT(qoff_f);
  2999. /*
  3000. * The logitem format's flag tells us if this was user quotaoff,
  3001. * group/project quotaoff or both.
  3002. */
  3003. if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
  3004. log->l_quotaoffs_flag |= XFS_DQ_USER;
  3005. if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
  3006. log->l_quotaoffs_flag |= XFS_DQ_PROJ;
  3007. if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
  3008. log->l_quotaoffs_flag |= XFS_DQ_GROUP;
  3009. return 0;
  3010. }
  3011. /*
  3012. * Recover a dquot record
  3013. */
  3014. STATIC int
  3015. xlog_recover_dquot_pass2(
  3016. struct xlog *log,
  3017. struct list_head *buffer_list,
  3018. struct xlog_recover_item *item,
  3019. xfs_lsn_t current_lsn)
  3020. {
  3021. xfs_mount_t *mp = log->l_mp;
  3022. xfs_buf_t *bp;
  3023. struct xfs_disk_dquot *ddq, *recddq;
  3024. int error;
  3025. xfs_dq_logformat_t *dq_f;
  3026. uint type;
  3027. /*
  3028. * Filesystems are required to send in quota flags at mount time.
  3029. */
  3030. if (mp->m_qflags == 0)
  3031. return 0;
  3032. recddq = item->ri_buf[1].i_addr;
  3033. if (recddq == NULL) {
  3034. xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
  3035. return -EIO;
  3036. }
  3037. if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
  3038. xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
  3039. item->ri_buf[1].i_len, __func__);
  3040. return -EIO;
  3041. }
  3042. /*
  3043. * This type of quotas was turned off, so ignore this record.
  3044. */
  3045. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  3046. ASSERT(type);
  3047. if (log->l_quotaoffs_flag & type)
  3048. return 0;
  3049. /*
  3050. * At this point we know that quota was _not_ turned off.
  3051. * Since the mount flags are not indicating to us otherwise, this
  3052. * must mean that quota is on, and the dquot needs to be replayed.
  3053. * Remember that we may not have fully recovered the superblock yet,
  3054. * so we can't do the usual trick of looking at the SB quota bits.
  3055. *
  3056. * The other possibility, of course, is that the quota subsystem was
  3057. * removed since the last mount - ENOSYS.
  3058. */
  3059. dq_f = item->ri_buf[0].i_addr;
  3060. ASSERT(dq_f);
  3061. error = xfs_dqcheck(mp, recddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  3062. "xlog_recover_dquot_pass2 (log copy)");
  3063. if (error)
  3064. return -EIO;
  3065. ASSERT(dq_f->qlf_len == 1);
  3066. /*
  3067. * At this point we are assuming that the dquots have been allocated
  3068. * and hence the buffer has valid dquots stamped in it. It should,
  3069. * therefore, pass verifier validation. If the dquot is bad, then the
  3070. * we'll return an error here, so we don't need to specifically check
  3071. * the dquot in the buffer after the verifier has run.
  3072. */
  3073. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
  3074. XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
  3075. &xfs_dquot_buf_ops);
  3076. if (error)
  3077. return error;
  3078. ASSERT(bp);
  3079. ddq = xfs_buf_offset(bp, dq_f->qlf_boffset);
  3080. /*
  3081. * If the dquot has an LSN in it, recover the dquot only if it's less
  3082. * than the lsn of the transaction we are replaying.
  3083. */
  3084. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  3085. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
  3086. xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
  3087. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  3088. goto out_release;
  3089. }
  3090. }
  3091. memcpy(ddq, recddq, item->ri_buf[1].i_len);
  3092. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  3093. xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
  3094. XFS_DQUOT_CRC_OFF);
  3095. }
  3096. ASSERT(dq_f->qlf_size == 2);
  3097. ASSERT(bp->b_target->bt_mount == mp);
  3098. bp->b_iodone = xlog_recover_iodone;
  3099. xfs_buf_delwri_queue(bp, buffer_list);
  3100. out_release:
  3101. xfs_buf_relse(bp);
  3102. return 0;
  3103. }
  3104. /*
  3105. * This routine is called to create an in-core extent free intent
  3106. * item from the efi format structure which was logged on disk.
  3107. * It allocates an in-core efi, copies the extents from the format
  3108. * structure into it, and adds the efi to the AIL with the given
  3109. * LSN.
  3110. */
  3111. STATIC int
  3112. xlog_recover_efi_pass2(
  3113. struct xlog *log,
  3114. struct xlog_recover_item *item,
  3115. xfs_lsn_t lsn)
  3116. {
  3117. int error;
  3118. struct xfs_mount *mp = log->l_mp;
  3119. struct xfs_efi_log_item *efip;
  3120. struct xfs_efi_log_format *efi_formatp;
  3121. efi_formatp = item->ri_buf[0].i_addr;
  3122. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  3123. error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
  3124. if (error) {
  3125. xfs_efi_item_free(efip);
  3126. return error;
  3127. }
  3128. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  3129. spin_lock(&log->l_ailp->xa_lock);
  3130. /*
  3131. * The EFI has two references. One for the EFD and one for EFI to ensure
  3132. * it makes it into the AIL. Insert the EFI into the AIL directly and
  3133. * drop the EFI reference. Note that xfs_trans_ail_update() drops the
  3134. * AIL lock.
  3135. */
  3136. xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
  3137. xfs_efi_release(efip);
  3138. return 0;
  3139. }
  3140. /*
  3141. * This routine is called when an EFD format structure is found in a committed
  3142. * transaction in the log. Its purpose is to cancel the corresponding EFI if it
  3143. * was still in the log. To do this it searches the AIL for the EFI with an id
  3144. * equal to that in the EFD format structure. If we find it we drop the EFD
  3145. * reference, which removes the EFI from the AIL and frees it.
  3146. */
  3147. STATIC int
  3148. xlog_recover_efd_pass2(
  3149. struct xlog *log,
  3150. struct xlog_recover_item *item)
  3151. {
  3152. xfs_efd_log_format_t *efd_formatp;
  3153. xfs_efi_log_item_t *efip = NULL;
  3154. xfs_log_item_t *lip;
  3155. uint64_t efi_id;
  3156. struct xfs_ail_cursor cur;
  3157. struct xfs_ail *ailp = log->l_ailp;
  3158. efd_formatp = item->ri_buf[0].i_addr;
  3159. ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
  3160. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
  3161. (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
  3162. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
  3163. efi_id = efd_formatp->efd_efi_id;
  3164. /*
  3165. * Search for the EFI with the id in the EFD format structure in the
  3166. * AIL.
  3167. */
  3168. spin_lock(&ailp->xa_lock);
  3169. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3170. while (lip != NULL) {
  3171. if (lip->li_type == XFS_LI_EFI) {
  3172. efip = (xfs_efi_log_item_t *)lip;
  3173. if (efip->efi_format.efi_id == efi_id) {
  3174. /*
  3175. * Drop the EFD reference to the EFI. This
  3176. * removes the EFI from the AIL and frees it.
  3177. */
  3178. spin_unlock(&ailp->xa_lock);
  3179. xfs_efi_release(efip);
  3180. spin_lock(&ailp->xa_lock);
  3181. break;
  3182. }
  3183. }
  3184. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3185. }
  3186. xfs_trans_ail_cursor_done(&cur);
  3187. spin_unlock(&ailp->xa_lock);
  3188. return 0;
  3189. }
  3190. /*
  3191. * This routine is called to create an in-core extent rmap update
  3192. * item from the rui format structure which was logged on disk.
  3193. * It allocates an in-core rui, copies the extents from the format
  3194. * structure into it, and adds the rui to the AIL with the given
  3195. * LSN.
  3196. */
  3197. STATIC int
  3198. xlog_recover_rui_pass2(
  3199. struct xlog *log,
  3200. struct xlog_recover_item *item,
  3201. xfs_lsn_t lsn)
  3202. {
  3203. int error;
  3204. struct xfs_mount *mp = log->l_mp;
  3205. struct xfs_rui_log_item *ruip;
  3206. struct xfs_rui_log_format *rui_formatp;
  3207. rui_formatp = item->ri_buf[0].i_addr;
  3208. ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
  3209. error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
  3210. if (error) {
  3211. xfs_rui_item_free(ruip);
  3212. return error;
  3213. }
  3214. atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
  3215. spin_lock(&log->l_ailp->xa_lock);
  3216. /*
  3217. * The RUI has two references. One for the RUD and one for RUI to ensure
  3218. * it makes it into the AIL. Insert the RUI into the AIL directly and
  3219. * drop the RUI reference. Note that xfs_trans_ail_update() drops the
  3220. * AIL lock.
  3221. */
  3222. xfs_trans_ail_update(log->l_ailp, &ruip->rui_item, lsn);
  3223. xfs_rui_release(ruip);
  3224. return 0;
  3225. }
  3226. /*
  3227. * This routine is called when an RUD format structure is found in a committed
  3228. * transaction in the log. Its purpose is to cancel the corresponding RUI if it
  3229. * was still in the log. To do this it searches the AIL for the RUI with an id
  3230. * equal to that in the RUD format structure. If we find it we drop the RUD
  3231. * reference, which removes the RUI from the AIL and frees it.
  3232. */
  3233. STATIC int
  3234. xlog_recover_rud_pass2(
  3235. struct xlog *log,
  3236. struct xlog_recover_item *item)
  3237. {
  3238. struct xfs_rud_log_format *rud_formatp;
  3239. struct xfs_rui_log_item *ruip = NULL;
  3240. struct xfs_log_item *lip;
  3241. uint64_t rui_id;
  3242. struct xfs_ail_cursor cur;
  3243. struct xfs_ail *ailp = log->l_ailp;
  3244. rud_formatp = item->ri_buf[0].i_addr;
  3245. ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
  3246. rui_id = rud_formatp->rud_rui_id;
  3247. /*
  3248. * Search for the RUI with the id in the RUD format structure in the
  3249. * AIL.
  3250. */
  3251. spin_lock(&ailp->xa_lock);
  3252. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3253. while (lip != NULL) {
  3254. if (lip->li_type == XFS_LI_RUI) {
  3255. ruip = (struct xfs_rui_log_item *)lip;
  3256. if (ruip->rui_format.rui_id == rui_id) {
  3257. /*
  3258. * Drop the RUD reference to the RUI. This
  3259. * removes the RUI from the AIL and frees it.
  3260. */
  3261. spin_unlock(&ailp->xa_lock);
  3262. xfs_rui_release(ruip);
  3263. spin_lock(&ailp->xa_lock);
  3264. break;
  3265. }
  3266. }
  3267. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3268. }
  3269. xfs_trans_ail_cursor_done(&cur);
  3270. spin_unlock(&ailp->xa_lock);
  3271. return 0;
  3272. }
  3273. /*
  3274. * Copy an CUI format buffer from the given buf, and into the destination
  3275. * CUI format structure. The CUI/CUD items were designed not to need any
  3276. * special alignment handling.
  3277. */
  3278. static int
  3279. xfs_cui_copy_format(
  3280. struct xfs_log_iovec *buf,
  3281. struct xfs_cui_log_format *dst_cui_fmt)
  3282. {
  3283. struct xfs_cui_log_format *src_cui_fmt;
  3284. uint len;
  3285. src_cui_fmt = buf->i_addr;
  3286. len = xfs_cui_log_format_sizeof(src_cui_fmt->cui_nextents);
  3287. if (buf->i_len == len) {
  3288. memcpy(dst_cui_fmt, src_cui_fmt, len);
  3289. return 0;
  3290. }
  3291. return -EFSCORRUPTED;
  3292. }
  3293. /*
  3294. * This routine is called to create an in-core extent refcount update
  3295. * item from the cui format structure which was logged on disk.
  3296. * It allocates an in-core cui, copies the extents from the format
  3297. * structure into it, and adds the cui to the AIL with the given
  3298. * LSN.
  3299. */
  3300. STATIC int
  3301. xlog_recover_cui_pass2(
  3302. struct xlog *log,
  3303. struct xlog_recover_item *item,
  3304. xfs_lsn_t lsn)
  3305. {
  3306. int error;
  3307. struct xfs_mount *mp = log->l_mp;
  3308. struct xfs_cui_log_item *cuip;
  3309. struct xfs_cui_log_format *cui_formatp;
  3310. cui_formatp = item->ri_buf[0].i_addr;
  3311. cuip = xfs_cui_init(mp, cui_formatp->cui_nextents);
  3312. error = xfs_cui_copy_format(&item->ri_buf[0], &cuip->cui_format);
  3313. if (error) {
  3314. xfs_cui_item_free(cuip);
  3315. return error;
  3316. }
  3317. atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
  3318. spin_lock(&log->l_ailp->xa_lock);
  3319. /*
  3320. * The CUI has two references. One for the CUD and one for CUI to ensure
  3321. * it makes it into the AIL. Insert the CUI into the AIL directly and
  3322. * drop the CUI reference. Note that xfs_trans_ail_update() drops the
  3323. * AIL lock.
  3324. */
  3325. xfs_trans_ail_update(log->l_ailp, &cuip->cui_item, lsn);
  3326. xfs_cui_release(cuip);
  3327. return 0;
  3328. }
  3329. /*
  3330. * This routine is called when an CUD format structure is found in a committed
  3331. * transaction in the log. Its purpose is to cancel the corresponding CUI if it
  3332. * was still in the log. To do this it searches the AIL for the CUI with an id
  3333. * equal to that in the CUD format structure. If we find it we drop the CUD
  3334. * reference, which removes the CUI from the AIL and frees it.
  3335. */
  3336. STATIC int
  3337. xlog_recover_cud_pass2(
  3338. struct xlog *log,
  3339. struct xlog_recover_item *item)
  3340. {
  3341. struct xfs_cud_log_format *cud_formatp;
  3342. struct xfs_cui_log_item *cuip = NULL;
  3343. struct xfs_log_item *lip;
  3344. uint64_t cui_id;
  3345. struct xfs_ail_cursor cur;
  3346. struct xfs_ail *ailp = log->l_ailp;
  3347. cud_formatp = item->ri_buf[0].i_addr;
  3348. if (item->ri_buf[0].i_len != sizeof(struct xfs_cud_log_format))
  3349. return -EFSCORRUPTED;
  3350. cui_id = cud_formatp->cud_cui_id;
  3351. /*
  3352. * Search for the CUI with the id in the CUD format structure in the
  3353. * AIL.
  3354. */
  3355. spin_lock(&ailp->xa_lock);
  3356. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3357. while (lip != NULL) {
  3358. if (lip->li_type == XFS_LI_CUI) {
  3359. cuip = (struct xfs_cui_log_item *)lip;
  3360. if (cuip->cui_format.cui_id == cui_id) {
  3361. /*
  3362. * Drop the CUD reference to the CUI. This
  3363. * removes the CUI from the AIL and frees it.
  3364. */
  3365. spin_unlock(&ailp->xa_lock);
  3366. xfs_cui_release(cuip);
  3367. spin_lock(&ailp->xa_lock);
  3368. break;
  3369. }
  3370. }
  3371. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3372. }
  3373. xfs_trans_ail_cursor_done(&cur);
  3374. spin_unlock(&ailp->xa_lock);
  3375. return 0;
  3376. }
  3377. /*
  3378. * Copy an BUI format buffer from the given buf, and into the destination
  3379. * BUI format structure. The BUI/BUD items were designed not to need any
  3380. * special alignment handling.
  3381. */
  3382. static int
  3383. xfs_bui_copy_format(
  3384. struct xfs_log_iovec *buf,
  3385. struct xfs_bui_log_format *dst_bui_fmt)
  3386. {
  3387. struct xfs_bui_log_format *src_bui_fmt;
  3388. uint len;
  3389. src_bui_fmt = buf->i_addr;
  3390. len = xfs_bui_log_format_sizeof(src_bui_fmt->bui_nextents);
  3391. if (buf->i_len == len) {
  3392. memcpy(dst_bui_fmt, src_bui_fmt, len);
  3393. return 0;
  3394. }
  3395. return -EFSCORRUPTED;
  3396. }
  3397. /*
  3398. * This routine is called to create an in-core extent bmap update
  3399. * item from the bui format structure which was logged on disk.
  3400. * It allocates an in-core bui, copies the extents from the format
  3401. * structure into it, and adds the bui to the AIL with the given
  3402. * LSN.
  3403. */
  3404. STATIC int
  3405. xlog_recover_bui_pass2(
  3406. struct xlog *log,
  3407. struct xlog_recover_item *item,
  3408. xfs_lsn_t lsn)
  3409. {
  3410. int error;
  3411. struct xfs_mount *mp = log->l_mp;
  3412. struct xfs_bui_log_item *buip;
  3413. struct xfs_bui_log_format *bui_formatp;
  3414. bui_formatp = item->ri_buf[0].i_addr;
  3415. if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
  3416. return -EFSCORRUPTED;
  3417. buip = xfs_bui_init(mp);
  3418. error = xfs_bui_copy_format(&item->ri_buf[0], &buip->bui_format);
  3419. if (error) {
  3420. xfs_bui_item_free(buip);
  3421. return error;
  3422. }
  3423. atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
  3424. spin_lock(&log->l_ailp->xa_lock);
  3425. /*
  3426. * The RUI has two references. One for the RUD and one for RUI to ensure
  3427. * it makes it into the AIL. Insert the RUI into the AIL directly and
  3428. * drop the RUI reference. Note that xfs_trans_ail_update() drops the
  3429. * AIL lock.
  3430. */
  3431. xfs_trans_ail_update(log->l_ailp, &buip->bui_item, lsn);
  3432. xfs_bui_release(buip);
  3433. return 0;
  3434. }
  3435. /*
  3436. * This routine is called when an BUD format structure is found in a committed
  3437. * transaction in the log. Its purpose is to cancel the corresponding BUI if it
  3438. * was still in the log. To do this it searches the AIL for the BUI with an id
  3439. * equal to that in the BUD format structure. If we find it we drop the BUD
  3440. * reference, which removes the BUI from the AIL and frees it.
  3441. */
  3442. STATIC int
  3443. xlog_recover_bud_pass2(
  3444. struct xlog *log,
  3445. struct xlog_recover_item *item)
  3446. {
  3447. struct xfs_bud_log_format *bud_formatp;
  3448. struct xfs_bui_log_item *buip = NULL;
  3449. struct xfs_log_item *lip;
  3450. uint64_t bui_id;
  3451. struct xfs_ail_cursor cur;
  3452. struct xfs_ail *ailp = log->l_ailp;
  3453. bud_formatp = item->ri_buf[0].i_addr;
  3454. if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format))
  3455. return -EFSCORRUPTED;
  3456. bui_id = bud_formatp->bud_bui_id;
  3457. /*
  3458. * Search for the BUI with the id in the BUD format structure in the
  3459. * AIL.
  3460. */
  3461. spin_lock(&ailp->xa_lock);
  3462. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3463. while (lip != NULL) {
  3464. if (lip->li_type == XFS_LI_BUI) {
  3465. buip = (struct xfs_bui_log_item *)lip;
  3466. if (buip->bui_format.bui_id == bui_id) {
  3467. /*
  3468. * Drop the BUD reference to the BUI. This
  3469. * removes the BUI from the AIL and frees it.
  3470. */
  3471. spin_unlock(&ailp->xa_lock);
  3472. xfs_bui_release(buip);
  3473. spin_lock(&ailp->xa_lock);
  3474. break;
  3475. }
  3476. }
  3477. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3478. }
  3479. xfs_trans_ail_cursor_done(&cur);
  3480. spin_unlock(&ailp->xa_lock);
  3481. return 0;
  3482. }
  3483. /*
  3484. * This routine is called when an inode create format structure is found in a
  3485. * committed transaction in the log. It's purpose is to initialise the inodes
  3486. * being allocated on disk. This requires us to get inode cluster buffers that
  3487. * match the range to be initialised, stamped with inode templates and written
  3488. * by delayed write so that subsequent modifications will hit the cached buffer
  3489. * and only need writing out at the end of recovery.
  3490. */
  3491. STATIC int
  3492. xlog_recover_do_icreate_pass2(
  3493. struct xlog *log,
  3494. struct list_head *buffer_list,
  3495. xlog_recover_item_t *item)
  3496. {
  3497. struct xfs_mount *mp = log->l_mp;
  3498. struct xfs_icreate_log *icl;
  3499. xfs_agnumber_t agno;
  3500. xfs_agblock_t agbno;
  3501. unsigned int count;
  3502. unsigned int isize;
  3503. xfs_agblock_t length;
  3504. int blks_per_cluster;
  3505. int bb_per_cluster;
  3506. int cancel_count;
  3507. int nbufs;
  3508. int i;
  3509. icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
  3510. if (icl->icl_type != XFS_LI_ICREATE) {
  3511. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
  3512. return -EINVAL;
  3513. }
  3514. if (icl->icl_size != 1) {
  3515. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
  3516. return -EINVAL;
  3517. }
  3518. agno = be32_to_cpu(icl->icl_ag);
  3519. if (agno >= mp->m_sb.sb_agcount) {
  3520. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
  3521. return -EINVAL;
  3522. }
  3523. agbno = be32_to_cpu(icl->icl_agbno);
  3524. if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
  3525. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
  3526. return -EINVAL;
  3527. }
  3528. isize = be32_to_cpu(icl->icl_isize);
  3529. if (isize != mp->m_sb.sb_inodesize) {
  3530. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
  3531. return -EINVAL;
  3532. }
  3533. count = be32_to_cpu(icl->icl_count);
  3534. if (!count) {
  3535. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
  3536. return -EINVAL;
  3537. }
  3538. length = be32_to_cpu(icl->icl_length);
  3539. if (!length || length >= mp->m_sb.sb_agblocks) {
  3540. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
  3541. return -EINVAL;
  3542. }
  3543. /*
  3544. * The inode chunk is either full or sparse and we only support
  3545. * m_ialloc_min_blks sized sparse allocations at this time.
  3546. */
  3547. if (length != mp->m_ialloc_blks &&
  3548. length != mp->m_ialloc_min_blks) {
  3549. xfs_warn(log->l_mp,
  3550. "%s: unsupported chunk length", __FUNCTION__);
  3551. return -EINVAL;
  3552. }
  3553. /* verify inode count is consistent with extent length */
  3554. if ((count >> mp->m_sb.sb_inopblog) != length) {
  3555. xfs_warn(log->l_mp,
  3556. "%s: inconsistent inode count and chunk length",
  3557. __FUNCTION__);
  3558. return -EINVAL;
  3559. }
  3560. /*
  3561. * The icreate transaction can cover multiple cluster buffers and these
  3562. * buffers could have been freed and reused. Check the individual
  3563. * buffers for cancellation so we don't overwrite anything written after
  3564. * a cancellation.
  3565. */
  3566. blks_per_cluster = xfs_icluster_size_fsb(mp);
  3567. bb_per_cluster = XFS_FSB_TO_BB(mp, blks_per_cluster);
  3568. nbufs = length / blks_per_cluster;
  3569. for (i = 0, cancel_count = 0; i < nbufs; i++) {
  3570. xfs_daddr_t daddr;
  3571. daddr = XFS_AGB_TO_DADDR(mp, agno,
  3572. agbno + i * blks_per_cluster);
  3573. if (xlog_check_buffer_cancelled(log, daddr, bb_per_cluster, 0))
  3574. cancel_count++;
  3575. }
  3576. /*
  3577. * We currently only use icreate for a single allocation at a time. This
  3578. * means we should expect either all or none of the buffers to be
  3579. * cancelled. Be conservative and skip replay if at least one buffer is
  3580. * cancelled, but warn the user that something is awry if the buffers
  3581. * are not consistent.
  3582. *
  3583. * XXX: This must be refined to only skip cancelled clusters once we use
  3584. * icreate for multiple chunk allocations.
  3585. */
  3586. ASSERT(!cancel_count || cancel_count == nbufs);
  3587. if (cancel_count) {
  3588. if (cancel_count != nbufs)
  3589. xfs_warn(mp,
  3590. "WARNING: partial inode chunk cancellation, skipped icreate.");
  3591. trace_xfs_log_recover_icreate_cancel(log, icl);
  3592. return 0;
  3593. }
  3594. trace_xfs_log_recover_icreate_recover(log, icl);
  3595. return xfs_ialloc_inode_init(mp, NULL, buffer_list, count, agno, agbno,
  3596. length, be32_to_cpu(icl->icl_gen));
  3597. }
  3598. STATIC void
  3599. xlog_recover_buffer_ra_pass2(
  3600. struct xlog *log,
  3601. struct xlog_recover_item *item)
  3602. {
  3603. struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
  3604. struct xfs_mount *mp = log->l_mp;
  3605. if (xlog_peek_buffer_cancelled(log, buf_f->blf_blkno,
  3606. buf_f->blf_len, buf_f->blf_flags)) {
  3607. return;
  3608. }
  3609. xfs_buf_readahead(mp->m_ddev_targp, buf_f->blf_blkno,
  3610. buf_f->blf_len, NULL);
  3611. }
  3612. STATIC void
  3613. xlog_recover_inode_ra_pass2(
  3614. struct xlog *log,
  3615. struct xlog_recover_item *item)
  3616. {
  3617. struct xfs_inode_log_format ilf_buf;
  3618. struct xfs_inode_log_format *ilfp;
  3619. struct xfs_mount *mp = log->l_mp;
  3620. int error;
  3621. if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
  3622. ilfp = item->ri_buf[0].i_addr;
  3623. } else {
  3624. ilfp = &ilf_buf;
  3625. memset(ilfp, 0, sizeof(*ilfp));
  3626. error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
  3627. if (error)
  3628. return;
  3629. }
  3630. if (xlog_peek_buffer_cancelled(log, ilfp->ilf_blkno, ilfp->ilf_len, 0))
  3631. return;
  3632. xfs_buf_readahead(mp->m_ddev_targp, ilfp->ilf_blkno,
  3633. ilfp->ilf_len, &xfs_inode_buf_ra_ops);
  3634. }
  3635. STATIC void
  3636. xlog_recover_dquot_ra_pass2(
  3637. struct xlog *log,
  3638. struct xlog_recover_item *item)
  3639. {
  3640. struct xfs_mount *mp = log->l_mp;
  3641. struct xfs_disk_dquot *recddq;
  3642. struct xfs_dq_logformat *dq_f;
  3643. uint type;
  3644. int len;
  3645. if (mp->m_qflags == 0)
  3646. return;
  3647. recddq = item->ri_buf[1].i_addr;
  3648. if (recddq == NULL)
  3649. return;
  3650. if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
  3651. return;
  3652. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  3653. ASSERT(type);
  3654. if (log->l_quotaoffs_flag & type)
  3655. return;
  3656. dq_f = item->ri_buf[0].i_addr;
  3657. ASSERT(dq_f);
  3658. ASSERT(dq_f->qlf_len == 1);
  3659. len = XFS_FSB_TO_BB(mp, dq_f->qlf_len);
  3660. if (xlog_peek_buffer_cancelled(log, dq_f->qlf_blkno, len, 0))
  3661. return;
  3662. xfs_buf_readahead(mp->m_ddev_targp, dq_f->qlf_blkno, len,
  3663. &xfs_dquot_buf_ra_ops);
  3664. }
  3665. STATIC void
  3666. xlog_recover_ra_pass2(
  3667. struct xlog *log,
  3668. struct xlog_recover_item *item)
  3669. {
  3670. switch (ITEM_TYPE(item)) {
  3671. case XFS_LI_BUF:
  3672. xlog_recover_buffer_ra_pass2(log, item);
  3673. break;
  3674. case XFS_LI_INODE:
  3675. xlog_recover_inode_ra_pass2(log, item);
  3676. break;
  3677. case XFS_LI_DQUOT:
  3678. xlog_recover_dquot_ra_pass2(log, item);
  3679. break;
  3680. case XFS_LI_EFI:
  3681. case XFS_LI_EFD:
  3682. case XFS_LI_QUOTAOFF:
  3683. case XFS_LI_RUI:
  3684. case XFS_LI_RUD:
  3685. case XFS_LI_CUI:
  3686. case XFS_LI_CUD:
  3687. case XFS_LI_BUI:
  3688. case XFS_LI_BUD:
  3689. default:
  3690. break;
  3691. }
  3692. }
  3693. STATIC int
  3694. xlog_recover_commit_pass1(
  3695. struct xlog *log,
  3696. struct xlog_recover *trans,
  3697. struct xlog_recover_item *item)
  3698. {
  3699. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
  3700. switch (ITEM_TYPE(item)) {
  3701. case XFS_LI_BUF:
  3702. return xlog_recover_buffer_pass1(log, item);
  3703. case XFS_LI_QUOTAOFF:
  3704. return xlog_recover_quotaoff_pass1(log, item);
  3705. case XFS_LI_INODE:
  3706. case XFS_LI_EFI:
  3707. case XFS_LI_EFD:
  3708. case XFS_LI_DQUOT:
  3709. case XFS_LI_ICREATE:
  3710. case XFS_LI_RUI:
  3711. case XFS_LI_RUD:
  3712. case XFS_LI_CUI:
  3713. case XFS_LI_CUD:
  3714. case XFS_LI_BUI:
  3715. case XFS_LI_BUD:
  3716. /* nothing to do in pass 1 */
  3717. return 0;
  3718. default:
  3719. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  3720. __func__, ITEM_TYPE(item));
  3721. ASSERT(0);
  3722. return -EIO;
  3723. }
  3724. }
  3725. STATIC int
  3726. xlog_recover_commit_pass2(
  3727. struct xlog *log,
  3728. struct xlog_recover *trans,
  3729. struct list_head *buffer_list,
  3730. struct xlog_recover_item *item)
  3731. {
  3732. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
  3733. switch (ITEM_TYPE(item)) {
  3734. case XFS_LI_BUF:
  3735. return xlog_recover_buffer_pass2(log, buffer_list, item,
  3736. trans->r_lsn);
  3737. case XFS_LI_INODE:
  3738. return xlog_recover_inode_pass2(log, buffer_list, item,
  3739. trans->r_lsn);
  3740. case XFS_LI_EFI:
  3741. return xlog_recover_efi_pass2(log, item, trans->r_lsn);
  3742. case XFS_LI_EFD:
  3743. return xlog_recover_efd_pass2(log, item);
  3744. case XFS_LI_RUI:
  3745. return xlog_recover_rui_pass2(log, item, trans->r_lsn);
  3746. case XFS_LI_RUD:
  3747. return xlog_recover_rud_pass2(log, item);
  3748. case XFS_LI_CUI:
  3749. return xlog_recover_cui_pass2(log, item, trans->r_lsn);
  3750. case XFS_LI_CUD:
  3751. return xlog_recover_cud_pass2(log, item);
  3752. case XFS_LI_BUI:
  3753. return xlog_recover_bui_pass2(log, item, trans->r_lsn);
  3754. case XFS_LI_BUD:
  3755. return xlog_recover_bud_pass2(log, item);
  3756. case XFS_LI_DQUOT:
  3757. return xlog_recover_dquot_pass2(log, buffer_list, item,
  3758. trans->r_lsn);
  3759. case XFS_LI_ICREATE:
  3760. return xlog_recover_do_icreate_pass2(log, buffer_list, item);
  3761. case XFS_LI_QUOTAOFF:
  3762. /* nothing to do in pass2 */
  3763. return 0;
  3764. default:
  3765. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  3766. __func__, ITEM_TYPE(item));
  3767. ASSERT(0);
  3768. return -EIO;
  3769. }
  3770. }
  3771. STATIC int
  3772. xlog_recover_items_pass2(
  3773. struct xlog *log,
  3774. struct xlog_recover *trans,
  3775. struct list_head *buffer_list,
  3776. struct list_head *item_list)
  3777. {
  3778. struct xlog_recover_item *item;
  3779. int error = 0;
  3780. list_for_each_entry(item, item_list, ri_list) {
  3781. error = xlog_recover_commit_pass2(log, trans,
  3782. buffer_list, item);
  3783. if (error)
  3784. return error;
  3785. }
  3786. return error;
  3787. }
  3788. /*
  3789. * Perform the transaction.
  3790. *
  3791. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  3792. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  3793. */
  3794. STATIC int
  3795. xlog_recover_commit_trans(
  3796. struct xlog *log,
  3797. struct xlog_recover *trans,
  3798. int pass,
  3799. struct list_head *buffer_list)
  3800. {
  3801. int error = 0;
  3802. int items_queued = 0;
  3803. struct xlog_recover_item *item;
  3804. struct xlog_recover_item *next;
  3805. LIST_HEAD (ra_list);
  3806. LIST_HEAD (done_list);
  3807. #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
  3808. hlist_del_init(&trans->r_list);
  3809. error = xlog_recover_reorder_trans(log, trans, pass);
  3810. if (error)
  3811. return error;
  3812. list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
  3813. switch (pass) {
  3814. case XLOG_RECOVER_PASS1:
  3815. error = xlog_recover_commit_pass1(log, trans, item);
  3816. break;
  3817. case XLOG_RECOVER_PASS2:
  3818. xlog_recover_ra_pass2(log, item);
  3819. list_move_tail(&item->ri_list, &ra_list);
  3820. items_queued++;
  3821. if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  3822. error = xlog_recover_items_pass2(log, trans,
  3823. buffer_list, &ra_list);
  3824. list_splice_tail_init(&ra_list, &done_list);
  3825. items_queued = 0;
  3826. }
  3827. break;
  3828. default:
  3829. ASSERT(0);
  3830. }
  3831. if (error)
  3832. goto out;
  3833. }
  3834. out:
  3835. if (!list_empty(&ra_list)) {
  3836. if (!error)
  3837. error = xlog_recover_items_pass2(log, trans,
  3838. buffer_list, &ra_list);
  3839. list_splice_tail_init(&ra_list, &done_list);
  3840. }
  3841. if (!list_empty(&done_list))
  3842. list_splice_init(&done_list, &trans->r_itemq);
  3843. return error;
  3844. }
  3845. STATIC void
  3846. xlog_recover_add_item(
  3847. struct list_head *head)
  3848. {
  3849. xlog_recover_item_t *item;
  3850. item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
  3851. INIT_LIST_HEAD(&item->ri_list);
  3852. list_add_tail(&item->ri_list, head);
  3853. }
  3854. STATIC int
  3855. xlog_recover_add_to_cont_trans(
  3856. struct xlog *log,
  3857. struct xlog_recover *trans,
  3858. char *dp,
  3859. int len)
  3860. {
  3861. xlog_recover_item_t *item;
  3862. char *ptr, *old_ptr;
  3863. int old_len;
  3864. /*
  3865. * If the transaction is empty, the header was split across this and the
  3866. * previous record. Copy the rest of the header.
  3867. */
  3868. if (list_empty(&trans->r_itemq)) {
  3869. ASSERT(len <= sizeof(struct xfs_trans_header));
  3870. if (len > sizeof(struct xfs_trans_header)) {
  3871. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  3872. return -EIO;
  3873. }
  3874. xlog_recover_add_item(&trans->r_itemq);
  3875. ptr = (char *)&trans->r_theader +
  3876. sizeof(struct xfs_trans_header) - len;
  3877. memcpy(ptr, dp, len);
  3878. return 0;
  3879. }
  3880. /* take the tail entry */
  3881. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3882. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  3883. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  3884. ptr = kmem_realloc(old_ptr, len + old_len, KM_SLEEP);
  3885. memcpy(&ptr[old_len], dp, len);
  3886. item->ri_buf[item->ri_cnt-1].i_len += len;
  3887. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  3888. trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
  3889. return 0;
  3890. }
  3891. /*
  3892. * The next region to add is the start of a new region. It could be
  3893. * a whole region or it could be the first part of a new region. Because
  3894. * of this, the assumption here is that the type and size fields of all
  3895. * format structures fit into the first 32 bits of the structure.
  3896. *
  3897. * This works because all regions must be 32 bit aligned. Therefore, we
  3898. * either have both fields or we have neither field. In the case we have
  3899. * neither field, the data part of the region is zero length. We only have
  3900. * a log_op_header and can throw away the header since a new one will appear
  3901. * later. If we have at least 4 bytes, then we can determine how many regions
  3902. * will appear in the current log item.
  3903. */
  3904. STATIC int
  3905. xlog_recover_add_to_trans(
  3906. struct xlog *log,
  3907. struct xlog_recover *trans,
  3908. char *dp,
  3909. int len)
  3910. {
  3911. struct xfs_inode_log_format *in_f; /* any will do */
  3912. xlog_recover_item_t *item;
  3913. char *ptr;
  3914. if (!len)
  3915. return 0;
  3916. if (list_empty(&trans->r_itemq)) {
  3917. /* we need to catch log corruptions here */
  3918. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  3919. xfs_warn(log->l_mp, "%s: bad header magic number",
  3920. __func__);
  3921. ASSERT(0);
  3922. return -EIO;
  3923. }
  3924. if (len > sizeof(struct xfs_trans_header)) {
  3925. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  3926. ASSERT(0);
  3927. return -EIO;
  3928. }
  3929. /*
  3930. * The transaction header can be arbitrarily split across op
  3931. * records. If we don't have the whole thing here, copy what we
  3932. * do have and handle the rest in the next record.
  3933. */
  3934. if (len == sizeof(struct xfs_trans_header))
  3935. xlog_recover_add_item(&trans->r_itemq);
  3936. memcpy(&trans->r_theader, dp, len);
  3937. return 0;
  3938. }
  3939. ptr = kmem_alloc(len, KM_SLEEP);
  3940. memcpy(ptr, dp, len);
  3941. in_f = (struct xfs_inode_log_format *)ptr;
  3942. /* take the tail entry */
  3943. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3944. if (item->ri_total != 0 &&
  3945. item->ri_total == item->ri_cnt) {
  3946. /* tail item is in use, get a new one */
  3947. xlog_recover_add_item(&trans->r_itemq);
  3948. item = list_entry(trans->r_itemq.prev,
  3949. xlog_recover_item_t, ri_list);
  3950. }
  3951. if (item->ri_total == 0) { /* first region to be added */
  3952. if (in_f->ilf_size == 0 ||
  3953. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  3954. xfs_warn(log->l_mp,
  3955. "bad number of regions (%d) in inode log format",
  3956. in_f->ilf_size);
  3957. ASSERT(0);
  3958. kmem_free(ptr);
  3959. return -EIO;
  3960. }
  3961. item->ri_total = in_f->ilf_size;
  3962. item->ri_buf =
  3963. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  3964. KM_SLEEP);
  3965. }
  3966. ASSERT(item->ri_total > item->ri_cnt);
  3967. /* Description region is ri_buf[0] */
  3968. item->ri_buf[item->ri_cnt].i_addr = ptr;
  3969. item->ri_buf[item->ri_cnt].i_len = len;
  3970. item->ri_cnt++;
  3971. trace_xfs_log_recover_item_add(log, trans, item, 0);
  3972. return 0;
  3973. }
  3974. /*
  3975. * Free up any resources allocated by the transaction
  3976. *
  3977. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  3978. */
  3979. STATIC void
  3980. xlog_recover_free_trans(
  3981. struct xlog_recover *trans)
  3982. {
  3983. xlog_recover_item_t *item, *n;
  3984. int i;
  3985. hlist_del_init(&trans->r_list);
  3986. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  3987. /* Free the regions in the item. */
  3988. list_del(&item->ri_list);
  3989. for (i = 0; i < item->ri_cnt; i++)
  3990. kmem_free(item->ri_buf[i].i_addr);
  3991. /* Free the item itself */
  3992. kmem_free(item->ri_buf);
  3993. kmem_free(item);
  3994. }
  3995. /* Free the transaction recover structure */
  3996. kmem_free(trans);
  3997. }
  3998. /*
  3999. * On error or completion, trans is freed.
  4000. */
  4001. STATIC int
  4002. xlog_recovery_process_trans(
  4003. struct xlog *log,
  4004. struct xlog_recover *trans,
  4005. char *dp,
  4006. unsigned int len,
  4007. unsigned int flags,
  4008. int pass,
  4009. struct list_head *buffer_list)
  4010. {
  4011. int error = 0;
  4012. bool freeit = false;
  4013. /* mask off ophdr transaction container flags */
  4014. flags &= ~XLOG_END_TRANS;
  4015. if (flags & XLOG_WAS_CONT_TRANS)
  4016. flags &= ~XLOG_CONTINUE_TRANS;
  4017. /*
  4018. * Callees must not free the trans structure. We'll decide if we need to
  4019. * free it or not based on the operation being done and it's result.
  4020. */
  4021. switch (flags) {
  4022. /* expected flag values */
  4023. case 0:
  4024. case XLOG_CONTINUE_TRANS:
  4025. error = xlog_recover_add_to_trans(log, trans, dp, len);
  4026. break;
  4027. case XLOG_WAS_CONT_TRANS:
  4028. error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  4029. break;
  4030. case XLOG_COMMIT_TRANS:
  4031. error = xlog_recover_commit_trans(log, trans, pass,
  4032. buffer_list);
  4033. /* success or fail, we are now done with this transaction. */
  4034. freeit = true;
  4035. break;
  4036. /* unexpected flag values */
  4037. case XLOG_UNMOUNT_TRANS:
  4038. /* just skip trans */
  4039. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  4040. freeit = true;
  4041. break;
  4042. case XLOG_START_TRANS:
  4043. default:
  4044. xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  4045. ASSERT(0);
  4046. error = -EIO;
  4047. break;
  4048. }
  4049. if (error || freeit)
  4050. xlog_recover_free_trans(trans);
  4051. return error;
  4052. }
  4053. /*
  4054. * Lookup the transaction recovery structure associated with the ID in the
  4055. * current ophdr. If the transaction doesn't exist and the start flag is set in
  4056. * the ophdr, then allocate a new transaction for future ID matches to find.
  4057. * Either way, return what we found during the lookup - an existing transaction
  4058. * or nothing.
  4059. */
  4060. STATIC struct xlog_recover *
  4061. xlog_recover_ophdr_to_trans(
  4062. struct hlist_head rhash[],
  4063. struct xlog_rec_header *rhead,
  4064. struct xlog_op_header *ohead)
  4065. {
  4066. struct xlog_recover *trans;
  4067. xlog_tid_t tid;
  4068. struct hlist_head *rhp;
  4069. tid = be32_to_cpu(ohead->oh_tid);
  4070. rhp = &rhash[XLOG_RHASH(tid)];
  4071. hlist_for_each_entry(trans, rhp, r_list) {
  4072. if (trans->r_log_tid == tid)
  4073. return trans;
  4074. }
  4075. /*
  4076. * skip over non-start transaction headers - we could be
  4077. * processing slack space before the next transaction starts
  4078. */
  4079. if (!(ohead->oh_flags & XLOG_START_TRANS))
  4080. return NULL;
  4081. ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  4082. /*
  4083. * This is a new transaction so allocate a new recovery container to
  4084. * hold the recovery ops that will follow.
  4085. */
  4086. trans = kmem_zalloc(sizeof(struct xlog_recover), KM_SLEEP);
  4087. trans->r_log_tid = tid;
  4088. trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  4089. INIT_LIST_HEAD(&trans->r_itemq);
  4090. INIT_HLIST_NODE(&trans->r_list);
  4091. hlist_add_head(&trans->r_list, rhp);
  4092. /*
  4093. * Nothing more to do for this ophdr. Items to be added to this new
  4094. * transaction will be in subsequent ophdr containers.
  4095. */
  4096. return NULL;
  4097. }
  4098. STATIC int
  4099. xlog_recover_process_ophdr(
  4100. struct xlog *log,
  4101. struct hlist_head rhash[],
  4102. struct xlog_rec_header *rhead,
  4103. struct xlog_op_header *ohead,
  4104. char *dp,
  4105. char *end,
  4106. int pass,
  4107. struct list_head *buffer_list)
  4108. {
  4109. struct xlog_recover *trans;
  4110. unsigned int len;
  4111. int error;
  4112. /* Do we understand who wrote this op? */
  4113. if (ohead->oh_clientid != XFS_TRANSACTION &&
  4114. ohead->oh_clientid != XFS_LOG) {
  4115. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  4116. __func__, ohead->oh_clientid);
  4117. ASSERT(0);
  4118. return -EIO;
  4119. }
  4120. /*
  4121. * Check the ophdr contains all the data it is supposed to contain.
  4122. */
  4123. len = be32_to_cpu(ohead->oh_len);
  4124. if (dp + len > end) {
  4125. xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  4126. WARN_ON(1);
  4127. return -EIO;
  4128. }
  4129. trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  4130. if (!trans) {
  4131. /* nothing to do, so skip over this ophdr */
  4132. return 0;
  4133. }
  4134. /*
  4135. * The recovered buffer queue is drained only once we know that all
  4136. * recovery items for the current LSN have been processed. This is
  4137. * required because:
  4138. *
  4139. * - Buffer write submission updates the metadata LSN of the buffer.
  4140. * - Log recovery skips items with a metadata LSN >= the current LSN of
  4141. * the recovery item.
  4142. * - Separate recovery items against the same metadata buffer can share
  4143. * a current LSN. I.e., consider that the LSN of a recovery item is
  4144. * defined as the starting LSN of the first record in which its
  4145. * transaction appears, that a record can hold multiple transactions,
  4146. * and/or that a transaction can span multiple records.
  4147. *
  4148. * In other words, we are allowed to submit a buffer from log recovery
  4149. * once per current LSN. Otherwise, we may incorrectly skip recovery
  4150. * items and cause corruption.
  4151. *
  4152. * We don't know up front whether buffers are updated multiple times per
  4153. * LSN. Therefore, track the current LSN of each commit log record as it
  4154. * is processed and drain the queue when it changes. Use commit records
  4155. * because they are ordered correctly by the logging code.
  4156. */
  4157. if (log->l_recovery_lsn != trans->r_lsn &&
  4158. ohead->oh_flags & XLOG_COMMIT_TRANS) {
  4159. error = xfs_buf_delwri_submit(buffer_list);
  4160. if (error)
  4161. return error;
  4162. log->l_recovery_lsn = trans->r_lsn;
  4163. }
  4164. return xlog_recovery_process_trans(log, trans, dp, len,
  4165. ohead->oh_flags, pass, buffer_list);
  4166. }
  4167. /*
  4168. * There are two valid states of the r_state field. 0 indicates that the
  4169. * transaction structure is in a normal state. We have either seen the
  4170. * start of the transaction or the last operation we added was not a partial
  4171. * operation. If the last operation we added to the transaction was a
  4172. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  4173. *
  4174. * NOTE: skip LRs with 0 data length.
  4175. */
  4176. STATIC int
  4177. xlog_recover_process_data(
  4178. struct xlog *log,
  4179. struct hlist_head rhash[],
  4180. struct xlog_rec_header *rhead,
  4181. char *dp,
  4182. int pass,
  4183. struct list_head *buffer_list)
  4184. {
  4185. struct xlog_op_header *ohead;
  4186. char *end;
  4187. int num_logops;
  4188. int error;
  4189. end = dp + be32_to_cpu(rhead->h_len);
  4190. num_logops = be32_to_cpu(rhead->h_num_logops);
  4191. /* check the log format matches our own - else we can't recover */
  4192. if (xlog_header_check_recover(log->l_mp, rhead))
  4193. return -EIO;
  4194. trace_xfs_log_recover_record(log, rhead, pass);
  4195. while ((dp < end) && num_logops) {
  4196. ohead = (struct xlog_op_header *)dp;
  4197. dp += sizeof(*ohead);
  4198. ASSERT(dp <= end);
  4199. /* errors will abort recovery */
  4200. error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
  4201. dp, end, pass, buffer_list);
  4202. if (error)
  4203. return error;
  4204. dp += be32_to_cpu(ohead->oh_len);
  4205. num_logops--;
  4206. }
  4207. return 0;
  4208. }
  4209. /* Recover the EFI if necessary. */
  4210. STATIC int
  4211. xlog_recover_process_efi(
  4212. struct xfs_mount *mp,
  4213. struct xfs_ail *ailp,
  4214. struct xfs_log_item *lip)
  4215. {
  4216. struct xfs_efi_log_item *efip;
  4217. int error;
  4218. /*
  4219. * Skip EFIs that we've already processed.
  4220. */
  4221. efip = container_of(lip, struct xfs_efi_log_item, efi_item);
  4222. if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags))
  4223. return 0;
  4224. spin_unlock(&ailp->xa_lock);
  4225. error = xfs_efi_recover(mp, efip);
  4226. spin_lock(&ailp->xa_lock);
  4227. return error;
  4228. }
  4229. /* Release the EFI since we're cancelling everything. */
  4230. STATIC void
  4231. xlog_recover_cancel_efi(
  4232. struct xfs_mount *mp,
  4233. struct xfs_ail *ailp,
  4234. struct xfs_log_item *lip)
  4235. {
  4236. struct xfs_efi_log_item *efip;
  4237. efip = container_of(lip, struct xfs_efi_log_item, efi_item);
  4238. spin_unlock(&ailp->xa_lock);
  4239. xfs_efi_release(efip);
  4240. spin_lock(&ailp->xa_lock);
  4241. }
  4242. /* Recover the RUI if necessary. */
  4243. STATIC int
  4244. xlog_recover_process_rui(
  4245. struct xfs_mount *mp,
  4246. struct xfs_ail *ailp,
  4247. struct xfs_log_item *lip)
  4248. {
  4249. struct xfs_rui_log_item *ruip;
  4250. int error;
  4251. /*
  4252. * Skip RUIs that we've already processed.
  4253. */
  4254. ruip = container_of(lip, struct xfs_rui_log_item, rui_item);
  4255. if (test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags))
  4256. return 0;
  4257. spin_unlock(&ailp->xa_lock);
  4258. error = xfs_rui_recover(mp, ruip);
  4259. spin_lock(&ailp->xa_lock);
  4260. return error;
  4261. }
  4262. /* Release the RUI since we're cancelling everything. */
  4263. STATIC void
  4264. xlog_recover_cancel_rui(
  4265. struct xfs_mount *mp,
  4266. struct xfs_ail *ailp,
  4267. struct xfs_log_item *lip)
  4268. {
  4269. struct xfs_rui_log_item *ruip;
  4270. ruip = container_of(lip, struct xfs_rui_log_item, rui_item);
  4271. spin_unlock(&ailp->xa_lock);
  4272. xfs_rui_release(ruip);
  4273. spin_lock(&ailp->xa_lock);
  4274. }
  4275. /* Recover the CUI if necessary. */
  4276. STATIC int
  4277. xlog_recover_process_cui(
  4278. struct xfs_mount *mp,
  4279. struct xfs_ail *ailp,
  4280. struct xfs_log_item *lip)
  4281. {
  4282. struct xfs_cui_log_item *cuip;
  4283. int error;
  4284. /*
  4285. * Skip CUIs that we've already processed.
  4286. */
  4287. cuip = container_of(lip, struct xfs_cui_log_item, cui_item);
  4288. if (test_bit(XFS_CUI_RECOVERED, &cuip->cui_flags))
  4289. return 0;
  4290. spin_unlock(&ailp->xa_lock);
  4291. error = xfs_cui_recover(mp, cuip);
  4292. spin_lock(&ailp->xa_lock);
  4293. return error;
  4294. }
  4295. /* Release the CUI since we're cancelling everything. */
  4296. STATIC void
  4297. xlog_recover_cancel_cui(
  4298. struct xfs_mount *mp,
  4299. struct xfs_ail *ailp,
  4300. struct xfs_log_item *lip)
  4301. {
  4302. struct xfs_cui_log_item *cuip;
  4303. cuip = container_of(lip, struct xfs_cui_log_item, cui_item);
  4304. spin_unlock(&ailp->xa_lock);
  4305. xfs_cui_release(cuip);
  4306. spin_lock(&ailp->xa_lock);
  4307. }
  4308. /* Recover the BUI if necessary. */
  4309. STATIC int
  4310. xlog_recover_process_bui(
  4311. struct xfs_mount *mp,
  4312. struct xfs_ail *ailp,
  4313. struct xfs_log_item *lip)
  4314. {
  4315. struct xfs_bui_log_item *buip;
  4316. int error;
  4317. /*
  4318. * Skip BUIs that we've already processed.
  4319. */
  4320. buip = container_of(lip, struct xfs_bui_log_item, bui_item);
  4321. if (test_bit(XFS_BUI_RECOVERED, &buip->bui_flags))
  4322. return 0;
  4323. spin_unlock(&ailp->xa_lock);
  4324. error = xfs_bui_recover(mp, buip);
  4325. spin_lock(&ailp->xa_lock);
  4326. return error;
  4327. }
  4328. /* Release the BUI since we're cancelling everything. */
  4329. STATIC void
  4330. xlog_recover_cancel_bui(
  4331. struct xfs_mount *mp,
  4332. struct xfs_ail *ailp,
  4333. struct xfs_log_item *lip)
  4334. {
  4335. struct xfs_bui_log_item *buip;
  4336. buip = container_of(lip, struct xfs_bui_log_item, bui_item);
  4337. spin_unlock(&ailp->xa_lock);
  4338. xfs_bui_release(buip);
  4339. spin_lock(&ailp->xa_lock);
  4340. }
  4341. /* Is this log item a deferred action intent? */
  4342. static inline bool xlog_item_is_intent(struct xfs_log_item *lip)
  4343. {
  4344. switch (lip->li_type) {
  4345. case XFS_LI_EFI:
  4346. case XFS_LI_RUI:
  4347. case XFS_LI_CUI:
  4348. case XFS_LI_BUI:
  4349. return true;
  4350. default:
  4351. return false;
  4352. }
  4353. }
  4354. /*
  4355. * When this is called, all of the log intent items which did not have
  4356. * corresponding log done items should be in the AIL. What we do now
  4357. * is update the data structures associated with each one.
  4358. *
  4359. * Since we process the log intent items in normal transactions, they
  4360. * will be removed at some point after the commit. This prevents us
  4361. * from just walking down the list processing each one. We'll use a
  4362. * flag in the intent item to skip those that we've already processed
  4363. * and use the AIL iteration mechanism's generation count to try to
  4364. * speed this up at least a bit.
  4365. *
  4366. * When we start, we know that the intents are the only things in the
  4367. * AIL. As we process them, however, other items are added to the
  4368. * AIL.
  4369. */
  4370. STATIC int
  4371. xlog_recover_process_intents(
  4372. struct xlog *log)
  4373. {
  4374. struct xfs_log_item *lip;
  4375. int error = 0;
  4376. struct xfs_ail_cursor cur;
  4377. struct xfs_ail *ailp;
  4378. #if defined(DEBUG) || defined(XFS_WARN)
  4379. xfs_lsn_t last_lsn;
  4380. #endif
  4381. ailp = log->l_ailp;
  4382. spin_lock(&ailp->xa_lock);
  4383. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  4384. #if defined(DEBUG) || defined(XFS_WARN)
  4385. last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
  4386. #endif
  4387. while (lip != NULL) {
  4388. /*
  4389. * We're done when we see something other than an intent.
  4390. * There should be no intents left in the AIL now.
  4391. */
  4392. if (!xlog_item_is_intent(lip)) {
  4393. #ifdef DEBUG
  4394. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  4395. ASSERT(!xlog_item_is_intent(lip));
  4396. #endif
  4397. break;
  4398. }
  4399. /*
  4400. * We should never see a redo item with a LSN higher than
  4401. * the last transaction we found in the log at the start
  4402. * of recovery.
  4403. */
  4404. ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
  4405. switch (lip->li_type) {
  4406. case XFS_LI_EFI:
  4407. error = xlog_recover_process_efi(log->l_mp, ailp, lip);
  4408. break;
  4409. case XFS_LI_RUI:
  4410. error = xlog_recover_process_rui(log->l_mp, ailp, lip);
  4411. break;
  4412. case XFS_LI_CUI:
  4413. error = xlog_recover_process_cui(log->l_mp, ailp, lip);
  4414. break;
  4415. case XFS_LI_BUI:
  4416. error = xlog_recover_process_bui(log->l_mp, ailp, lip);
  4417. break;
  4418. }
  4419. if (error)
  4420. goto out;
  4421. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  4422. }
  4423. out:
  4424. xfs_trans_ail_cursor_done(&cur);
  4425. spin_unlock(&ailp->xa_lock);
  4426. return error;
  4427. }
  4428. /*
  4429. * A cancel occurs when the mount has failed and we're bailing out.
  4430. * Release all pending log intent items so they don't pin the AIL.
  4431. */
  4432. STATIC int
  4433. xlog_recover_cancel_intents(
  4434. struct xlog *log)
  4435. {
  4436. struct xfs_log_item *lip;
  4437. int error = 0;
  4438. struct xfs_ail_cursor cur;
  4439. struct xfs_ail *ailp;
  4440. ailp = log->l_ailp;
  4441. spin_lock(&ailp->xa_lock);
  4442. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  4443. while (lip != NULL) {
  4444. /*
  4445. * We're done when we see something other than an intent.
  4446. * There should be no intents left in the AIL now.
  4447. */
  4448. if (!xlog_item_is_intent(lip)) {
  4449. #ifdef DEBUG
  4450. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  4451. ASSERT(!xlog_item_is_intent(lip));
  4452. #endif
  4453. break;
  4454. }
  4455. switch (lip->li_type) {
  4456. case XFS_LI_EFI:
  4457. xlog_recover_cancel_efi(log->l_mp, ailp, lip);
  4458. break;
  4459. case XFS_LI_RUI:
  4460. xlog_recover_cancel_rui(log->l_mp, ailp, lip);
  4461. break;
  4462. case XFS_LI_CUI:
  4463. xlog_recover_cancel_cui(log->l_mp, ailp, lip);
  4464. break;
  4465. case XFS_LI_BUI:
  4466. xlog_recover_cancel_bui(log->l_mp, ailp, lip);
  4467. break;
  4468. }
  4469. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  4470. }
  4471. xfs_trans_ail_cursor_done(&cur);
  4472. spin_unlock(&ailp->xa_lock);
  4473. return error;
  4474. }
  4475. /*
  4476. * This routine performs a transaction to null out a bad inode pointer
  4477. * in an agi unlinked inode hash bucket.
  4478. */
  4479. STATIC void
  4480. xlog_recover_clear_agi_bucket(
  4481. xfs_mount_t *mp,
  4482. xfs_agnumber_t agno,
  4483. int bucket)
  4484. {
  4485. xfs_trans_t *tp;
  4486. xfs_agi_t *agi;
  4487. xfs_buf_t *agibp;
  4488. int offset;
  4489. int error;
  4490. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
  4491. if (error)
  4492. goto out_error;
  4493. error = xfs_read_agi(mp, tp, agno, &agibp);
  4494. if (error)
  4495. goto out_abort;
  4496. agi = XFS_BUF_TO_AGI(agibp);
  4497. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  4498. offset = offsetof(xfs_agi_t, agi_unlinked) +
  4499. (sizeof(xfs_agino_t) * bucket);
  4500. xfs_trans_log_buf(tp, agibp, offset,
  4501. (offset + sizeof(xfs_agino_t) - 1));
  4502. error = xfs_trans_commit(tp);
  4503. if (error)
  4504. goto out_error;
  4505. return;
  4506. out_abort:
  4507. xfs_trans_cancel(tp);
  4508. out_error:
  4509. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
  4510. return;
  4511. }
  4512. STATIC xfs_agino_t
  4513. xlog_recover_process_one_iunlink(
  4514. struct xfs_mount *mp,
  4515. xfs_agnumber_t agno,
  4516. xfs_agino_t agino,
  4517. int bucket)
  4518. {
  4519. struct xfs_buf *ibp;
  4520. struct xfs_dinode *dip;
  4521. struct xfs_inode *ip;
  4522. xfs_ino_t ino;
  4523. int error;
  4524. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  4525. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  4526. if (error)
  4527. goto fail;
  4528. /*
  4529. * Get the on disk inode to find the next inode in the bucket.
  4530. */
  4531. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
  4532. if (error)
  4533. goto fail_iput;
  4534. xfs_iflags_clear(ip, XFS_IRECOVERY);
  4535. ASSERT(VFS_I(ip)->i_nlink == 0);
  4536. ASSERT(VFS_I(ip)->i_mode != 0);
  4537. /* setup for the next pass */
  4538. agino = be32_to_cpu(dip->di_next_unlinked);
  4539. xfs_buf_relse(ibp);
  4540. /*
  4541. * Prevent any DMAPI event from being sent when the reference on
  4542. * the inode is dropped.
  4543. */
  4544. ip->i_d.di_dmevmask = 0;
  4545. IRELE(ip);
  4546. return agino;
  4547. fail_iput:
  4548. IRELE(ip);
  4549. fail:
  4550. /*
  4551. * We can't read in the inode this bucket points to, or this inode
  4552. * is messed up. Just ditch this bucket of inodes. We will lose
  4553. * some inodes and space, but at least we won't hang.
  4554. *
  4555. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  4556. * clear the inode pointer in the bucket.
  4557. */
  4558. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  4559. return NULLAGINO;
  4560. }
  4561. /*
  4562. * xlog_iunlink_recover
  4563. *
  4564. * This is called during recovery to process any inodes which
  4565. * we unlinked but not freed when the system crashed. These
  4566. * inodes will be on the lists in the AGI blocks. What we do
  4567. * here is scan all the AGIs and fully truncate and free any
  4568. * inodes found on the lists. Each inode is removed from the
  4569. * lists when it has been fully truncated and is freed. The
  4570. * freeing of the inode and its removal from the list must be
  4571. * atomic.
  4572. */
  4573. STATIC void
  4574. xlog_recover_process_iunlinks(
  4575. struct xlog *log)
  4576. {
  4577. xfs_mount_t *mp;
  4578. xfs_agnumber_t agno;
  4579. xfs_agi_t *agi;
  4580. xfs_buf_t *agibp;
  4581. xfs_agino_t agino;
  4582. int bucket;
  4583. int error;
  4584. uint mp_dmevmask;
  4585. mp = log->l_mp;
  4586. /*
  4587. * Prevent any DMAPI event from being sent while in this function.
  4588. */
  4589. mp_dmevmask = mp->m_dmevmask;
  4590. mp->m_dmevmask = 0;
  4591. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  4592. /*
  4593. * Find the agi for this ag.
  4594. */
  4595. error = xfs_read_agi(mp, NULL, agno, &agibp);
  4596. if (error) {
  4597. /*
  4598. * AGI is b0rked. Don't process it.
  4599. *
  4600. * We should probably mark the filesystem as corrupt
  4601. * after we've recovered all the ag's we can....
  4602. */
  4603. continue;
  4604. }
  4605. /*
  4606. * Unlock the buffer so that it can be acquired in the normal
  4607. * course of the transaction to truncate and free each inode.
  4608. * Because we are not racing with anyone else here for the AGI
  4609. * buffer, we don't even need to hold it locked to read the
  4610. * initial unlinked bucket entries out of the buffer. We keep
  4611. * buffer reference though, so that it stays pinned in memory
  4612. * while we need the buffer.
  4613. */
  4614. agi = XFS_BUF_TO_AGI(agibp);
  4615. xfs_buf_unlock(agibp);
  4616. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  4617. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  4618. while (agino != NULLAGINO) {
  4619. agino = xlog_recover_process_one_iunlink(mp,
  4620. agno, agino, bucket);
  4621. }
  4622. }
  4623. xfs_buf_rele(agibp);
  4624. }
  4625. mp->m_dmevmask = mp_dmevmask;
  4626. }
  4627. STATIC int
  4628. xlog_unpack_data(
  4629. struct xlog_rec_header *rhead,
  4630. char *dp,
  4631. struct xlog *log)
  4632. {
  4633. int i, j, k;
  4634. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  4635. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  4636. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  4637. dp += BBSIZE;
  4638. }
  4639. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  4640. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  4641. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  4642. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  4643. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  4644. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  4645. dp += BBSIZE;
  4646. }
  4647. }
  4648. return 0;
  4649. }
  4650. /*
  4651. * CRC check, unpack and process a log record.
  4652. */
  4653. STATIC int
  4654. xlog_recover_process(
  4655. struct xlog *log,
  4656. struct hlist_head rhash[],
  4657. struct xlog_rec_header *rhead,
  4658. char *dp,
  4659. int pass,
  4660. struct list_head *buffer_list)
  4661. {
  4662. int error;
  4663. __le32 old_crc = rhead->h_crc;
  4664. __le32 crc;
  4665. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  4666. /*
  4667. * Nothing else to do if this is a CRC verification pass. Just return
  4668. * if this a record with a non-zero crc. Unfortunately, mkfs always
  4669. * sets old_crc to 0 so we must consider this valid even on v5 supers.
  4670. * Otherwise, return EFSBADCRC on failure so the callers up the stack
  4671. * know precisely what failed.
  4672. */
  4673. if (pass == XLOG_RECOVER_CRCPASS) {
  4674. if (old_crc && crc != old_crc)
  4675. return -EFSBADCRC;
  4676. return 0;
  4677. }
  4678. /*
  4679. * We're in the normal recovery path. Issue a warning if and only if the
  4680. * CRC in the header is non-zero. This is an advisory warning and the
  4681. * zero CRC check prevents warnings from being emitted when upgrading
  4682. * the kernel from one that does not add CRCs by default.
  4683. */
  4684. if (crc != old_crc) {
  4685. if (old_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  4686. xfs_alert(log->l_mp,
  4687. "log record CRC mismatch: found 0x%x, expected 0x%x.",
  4688. le32_to_cpu(old_crc),
  4689. le32_to_cpu(crc));
  4690. xfs_hex_dump(dp, 32);
  4691. }
  4692. /*
  4693. * If the filesystem is CRC enabled, this mismatch becomes a
  4694. * fatal log corruption failure.
  4695. */
  4696. if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
  4697. return -EFSCORRUPTED;
  4698. }
  4699. error = xlog_unpack_data(rhead, dp, log);
  4700. if (error)
  4701. return error;
  4702. return xlog_recover_process_data(log, rhash, rhead, dp, pass,
  4703. buffer_list);
  4704. }
  4705. STATIC int
  4706. xlog_valid_rec_header(
  4707. struct xlog *log,
  4708. struct xlog_rec_header *rhead,
  4709. xfs_daddr_t blkno)
  4710. {
  4711. int hlen;
  4712. if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
  4713. XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
  4714. XFS_ERRLEVEL_LOW, log->l_mp);
  4715. return -EFSCORRUPTED;
  4716. }
  4717. if (unlikely(
  4718. (!rhead->h_version ||
  4719. (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
  4720. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  4721. __func__, be32_to_cpu(rhead->h_version));
  4722. return -EIO;
  4723. }
  4724. /* LR body must have data or it wouldn't have been written */
  4725. hlen = be32_to_cpu(rhead->h_len);
  4726. if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
  4727. XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
  4728. XFS_ERRLEVEL_LOW, log->l_mp);
  4729. return -EFSCORRUPTED;
  4730. }
  4731. if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
  4732. XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
  4733. XFS_ERRLEVEL_LOW, log->l_mp);
  4734. return -EFSCORRUPTED;
  4735. }
  4736. return 0;
  4737. }
  4738. /*
  4739. * Read the log from tail to head and process the log records found.
  4740. * Handle the two cases where the tail and head are in the same cycle
  4741. * and where the active portion of the log wraps around the end of
  4742. * the physical log separately. The pass parameter is passed through
  4743. * to the routines called to process the data and is not looked at
  4744. * here.
  4745. */
  4746. STATIC int
  4747. xlog_do_recovery_pass(
  4748. struct xlog *log,
  4749. xfs_daddr_t head_blk,
  4750. xfs_daddr_t tail_blk,
  4751. int pass,
  4752. xfs_daddr_t *first_bad) /* out: first bad log rec */
  4753. {
  4754. xlog_rec_header_t *rhead;
  4755. xfs_daddr_t blk_no, rblk_no;
  4756. xfs_daddr_t rhead_blk;
  4757. char *offset;
  4758. xfs_buf_t *hbp, *dbp;
  4759. int error = 0, h_size, h_len;
  4760. int error2 = 0;
  4761. int bblks, split_bblks;
  4762. int hblks, split_hblks, wrapped_hblks;
  4763. int i;
  4764. struct hlist_head rhash[XLOG_RHASH_SIZE];
  4765. LIST_HEAD (buffer_list);
  4766. ASSERT(head_blk != tail_blk);
  4767. blk_no = rhead_blk = tail_blk;
  4768. for (i = 0; i < XLOG_RHASH_SIZE; i++)
  4769. INIT_HLIST_HEAD(&rhash[i]);
  4770. /*
  4771. * Read the header of the tail block and get the iclog buffer size from
  4772. * h_size. Use this to tell how many sectors make up the log header.
  4773. */
  4774. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  4775. /*
  4776. * When using variable length iclogs, read first sector of
  4777. * iclog header and extract the header size from it. Get a
  4778. * new hbp that is the correct size.
  4779. */
  4780. hbp = xlog_get_bp(log, 1);
  4781. if (!hbp)
  4782. return -ENOMEM;
  4783. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  4784. if (error)
  4785. goto bread_err1;
  4786. rhead = (xlog_rec_header_t *)offset;
  4787. error = xlog_valid_rec_header(log, rhead, tail_blk);
  4788. if (error)
  4789. goto bread_err1;
  4790. /*
  4791. * xfsprogs has a bug where record length is based on lsunit but
  4792. * h_size (iclog size) is hardcoded to 32k. Now that we
  4793. * unconditionally CRC verify the unmount record, this means the
  4794. * log buffer can be too small for the record and cause an
  4795. * overrun.
  4796. *
  4797. * Detect this condition here. Use lsunit for the buffer size as
  4798. * long as this looks like the mkfs case. Otherwise, return an
  4799. * error to avoid a buffer overrun.
  4800. */
  4801. h_size = be32_to_cpu(rhead->h_size);
  4802. h_len = be32_to_cpu(rhead->h_len);
  4803. if (h_len > h_size) {
  4804. if (h_len <= log->l_mp->m_logbsize &&
  4805. be32_to_cpu(rhead->h_num_logops) == 1) {
  4806. xfs_warn(log->l_mp,
  4807. "invalid iclog size (%d bytes), using lsunit (%d bytes)",
  4808. h_size, log->l_mp->m_logbsize);
  4809. h_size = log->l_mp->m_logbsize;
  4810. } else
  4811. return -EFSCORRUPTED;
  4812. }
  4813. if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
  4814. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  4815. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  4816. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  4817. hblks++;
  4818. xlog_put_bp(hbp);
  4819. hbp = xlog_get_bp(log, hblks);
  4820. } else {
  4821. hblks = 1;
  4822. }
  4823. } else {
  4824. ASSERT(log->l_sectBBsize == 1);
  4825. hblks = 1;
  4826. hbp = xlog_get_bp(log, 1);
  4827. h_size = XLOG_BIG_RECORD_BSIZE;
  4828. }
  4829. if (!hbp)
  4830. return -ENOMEM;
  4831. dbp = xlog_get_bp(log, BTOBB(h_size));
  4832. if (!dbp) {
  4833. xlog_put_bp(hbp);
  4834. return -ENOMEM;
  4835. }
  4836. memset(rhash, 0, sizeof(rhash));
  4837. if (tail_blk > head_blk) {
  4838. /*
  4839. * Perform recovery around the end of the physical log.
  4840. * When the head is not on the same cycle number as the tail,
  4841. * we can't do a sequential recovery.
  4842. */
  4843. while (blk_no < log->l_logBBsize) {
  4844. /*
  4845. * Check for header wrapping around physical end-of-log
  4846. */
  4847. offset = hbp->b_addr;
  4848. split_hblks = 0;
  4849. wrapped_hblks = 0;
  4850. if (blk_no + hblks <= log->l_logBBsize) {
  4851. /* Read header in one read */
  4852. error = xlog_bread(log, blk_no, hblks, hbp,
  4853. &offset);
  4854. if (error)
  4855. goto bread_err2;
  4856. } else {
  4857. /* This LR is split across physical log end */
  4858. if (blk_no != log->l_logBBsize) {
  4859. /* some data before physical log end */
  4860. ASSERT(blk_no <= INT_MAX);
  4861. split_hblks = log->l_logBBsize - (int)blk_no;
  4862. ASSERT(split_hblks > 0);
  4863. error = xlog_bread(log, blk_no,
  4864. split_hblks, hbp,
  4865. &offset);
  4866. if (error)
  4867. goto bread_err2;
  4868. }
  4869. /*
  4870. * Note: this black magic still works with
  4871. * large sector sizes (non-512) only because:
  4872. * - we increased the buffer size originally
  4873. * by 1 sector giving us enough extra space
  4874. * for the second read;
  4875. * - the log start is guaranteed to be sector
  4876. * aligned;
  4877. * - we read the log end (LR header start)
  4878. * _first_, then the log start (LR header end)
  4879. * - order is important.
  4880. */
  4881. wrapped_hblks = hblks - split_hblks;
  4882. error = xlog_bread_offset(log, 0,
  4883. wrapped_hblks, hbp,
  4884. offset + BBTOB(split_hblks));
  4885. if (error)
  4886. goto bread_err2;
  4887. }
  4888. rhead = (xlog_rec_header_t *)offset;
  4889. error = xlog_valid_rec_header(log, rhead,
  4890. split_hblks ? blk_no : 0);
  4891. if (error)
  4892. goto bread_err2;
  4893. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  4894. blk_no += hblks;
  4895. /*
  4896. * Read the log record data in multiple reads if it
  4897. * wraps around the end of the log. Note that if the
  4898. * header already wrapped, blk_no could point past the
  4899. * end of the log. The record data is contiguous in
  4900. * that case.
  4901. */
  4902. if (blk_no + bblks <= log->l_logBBsize ||
  4903. blk_no >= log->l_logBBsize) {
  4904. /* mod blk_no in case the header wrapped and
  4905. * pushed it beyond the end of the log */
  4906. rblk_no = do_mod(blk_no, log->l_logBBsize);
  4907. error = xlog_bread(log, rblk_no, bblks, dbp,
  4908. &offset);
  4909. if (error)
  4910. goto bread_err2;
  4911. } else {
  4912. /* This log record is split across the
  4913. * physical end of log */
  4914. offset = dbp->b_addr;
  4915. split_bblks = 0;
  4916. if (blk_no != log->l_logBBsize) {
  4917. /* some data is before the physical
  4918. * end of log */
  4919. ASSERT(!wrapped_hblks);
  4920. ASSERT(blk_no <= INT_MAX);
  4921. split_bblks =
  4922. log->l_logBBsize - (int)blk_no;
  4923. ASSERT(split_bblks > 0);
  4924. error = xlog_bread(log, blk_no,
  4925. split_bblks, dbp,
  4926. &offset);
  4927. if (error)
  4928. goto bread_err2;
  4929. }
  4930. /*
  4931. * Note: this black magic still works with
  4932. * large sector sizes (non-512) only because:
  4933. * - we increased the buffer size originally
  4934. * by 1 sector giving us enough extra space
  4935. * for the second read;
  4936. * - the log start is guaranteed to be sector
  4937. * aligned;
  4938. * - we read the log end (LR header start)
  4939. * _first_, then the log start (LR header end)
  4940. * - order is important.
  4941. */
  4942. error = xlog_bread_offset(log, 0,
  4943. bblks - split_bblks, dbp,
  4944. offset + BBTOB(split_bblks));
  4945. if (error)
  4946. goto bread_err2;
  4947. }
  4948. error = xlog_recover_process(log, rhash, rhead, offset,
  4949. pass, &buffer_list);
  4950. if (error)
  4951. goto bread_err2;
  4952. blk_no += bblks;
  4953. rhead_blk = blk_no;
  4954. }
  4955. ASSERT(blk_no >= log->l_logBBsize);
  4956. blk_no -= log->l_logBBsize;
  4957. rhead_blk = blk_no;
  4958. }
  4959. /* read first part of physical log */
  4960. while (blk_no < head_blk) {
  4961. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  4962. if (error)
  4963. goto bread_err2;
  4964. rhead = (xlog_rec_header_t *)offset;
  4965. error = xlog_valid_rec_header(log, rhead, blk_no);
  4966. if (error)
  4967. goto bread_err2;
  4968. /* blocks in data section */
  4969. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  4970. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  4971. &offset);
  4972. if (error)
  4973. goto bread_err2;
  4974. error = xlog_recover_process(log, rhash, rhead, offset, pass,
  4975. &buffer_list);
  4976. if (error)
  4977. goto bread_err2;
  4978. blk_no += bblks + hblks;
  4979. rhead_blk = blk_no;
  4980. }
  4981. bread_err2:
  4982. xlog_put_bp(dbp);
  4983. bread_err1:
  4984. xlog_put_bp(hbp);
  4985. /*
  4986. * Submit buffers that have been added from the last record processed,
  4987. * regardless of error status.
  4988. */
  4989. if (!list_empty(&buffer_list))
  4990. error2 = xfs_buf_delwri_submit(&buffer_list);
  4991. if (error && first_bad)
  4992. *first_bad = rhead_blk;
  4993. /*
  4994. * Transactions are freed at commit time but transactions without commit
  4995. * records on disk are never committed. Free any that may be left in the
  4996. * hash table.
  4997. */
  4998. for (i = 0; i < XLOG_RHASH_SIZE; i++) {
  4999. struct hlist_node *tmp;
  5000. struct xlog_recover *trans;
  5001. hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
  5002. xlog_recover_free_trans(trans);
  5003. }
  5004. return error ? error : error2;
  5005. }
  5006. /*
  5007. * Do the recovery of the log. We actually do this in two phases.
  5008. * The two passes are necessary in order to implement the function
  5009. * of cancelling a record written into the log. The first pass
  5010. * determines those things which have been cancelled, and the
  5011. * second pass replays log items normally except for those which
  5012. * have been cancelled. The handling of the replay and cancellations
  5013. * takes place in the log item type specific routines.
  5014. *
  5015. * The table of items which have cancel records in the log is allocated
  5016. * and freed at this level, since only here do we know when all of
  5017. * the log recovery has been completed.
  5018. */
  5019. STATIC int
  5020. xlog_do_log_recovery(
  5021. struct xlog *log,
  5022. xfs_daddr_t head_blk,
  5023. xfs_daddr_t tail_blk)
  5024. {
  5025. int error, i;
  5026. ASSERT(head_blk != tail_blk);
  5027. /*
  5028. * First do a pass to find all of the cancelled buf log items.
  5029. * Store them in the buf_cancel_table for use in the second pass.
  5030. */
  5031. log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  5032. sizeof(struct list_head),
  5033. KM_SLEEP);
  5034. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  5035. INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
  5036. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  5037. XLOG_RECOVER_PASS1, NULL);
  5038. if (error != 0) {
  5039. kmem_free(log->l_buf_cancel_table);
  5040. log->l_buf_cancel_table = NULL;
  5041. return error;
  5042. }
  5043. /*
  5044. * Then do a second pass to actually recover the items in the log.
  5045. * When it is complete free the table of buf cancel items.
  5046. */
  5047. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  5048. XLOG_RECOVER_PASS2, NULL);
  5049. #ifdef DEBUG
  5050. if (!error) {
  5051. int i;
  5052. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  5053. ASSERT(list_empty(&log->l_buf_cancel_table[i]));
  5054. }
  5055. #endif /* DEBUG */
  5056. kmem_free(log->l_buf_cancel_table);
  5057. log->l_buf_cancel_table = NULL;
  5058. return error;
  5059. }
  5060. /*
  5061. * Do the actual recovery
  5062. */
  5063. STATIC int
  5064. xlog_do_recover(
  5065. struct xlog *log,
  5066. xfs_daddr_t head_blk,
  5067. xfs_daddr_t tail_blk)
  5068. {
  5069. struct xfs_mount *mp = log->l_mp;
  5070. int error;
  5071. xfs_buf_t *bp;
  5072. xfs_sb_t *sbp;
  5073. trace_xfs_log_recover(log, head_blk, tail_blk);
  5074. /*
  5075. * First replay the images in the log.
  5076. */
  5077. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  5078. if (error)
  5079. return error;
  5080. /*
  5081. * If IO errors happened during recovery, bail out.
  5082. */
  5083. if (XFS_FORCED_SHUTDOWN(mp)) {
  5084. return -EIO;
  5085. }
  5086. /*
  5087. * We now update the tail_lsn since much of the recovery has completed
  5088. * and there may be space available to use. If there were no extent
  5089. * or iunlinks, we can free up the entire log and set the tail_lsn to
  5090. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  5091. * lsn of the last known good LR on disk. If there are extent frees
  5092. * or iunlinks they will have some entries in the AIL; so we look at
  5093. * the AIL to determine how to set the tail_lsn.
  5094. */
  5095. xlog_assign_tail_lsn(mp);
  5096. /*
  5097. * Now that we've finished replaying all buffer and inode
  5098. * updates, re-read in the superblock and reverify it.
  5099. */
  5100. bp = xfs_getsb(mp, 0);
  5101. bp->b_flags &= ~(XBF_DONE | XBF_ASYNC);
  5102. ASSERT(!(bp->b_flags & XBF_WRITE));
  5103. bp->b_flags |= XBF_READ;
  5104. bp->b_ops = &xfs_sb_buf_ops;
  5105. error = xfs_buf_submit_wait(bp);
  5106. if (error) {
  5107. if (!XFS_FORCED_SHUTDOWN(mp)) {
  5108. xfs_buf_ioerror_alert(bp, __func__);
  5109. ASSERT(0);
  5110. }
  5111. xfs_buf_relse(bp);
  5112. return error;
  5113. }
  5114. /* Convert superblock from on-disk format */
  5115. sbp = &mp->m_sb;
  5116. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  5117. xfs_buf_relse(bp);
  5118. /* re-initialise in-core superblock and geometry structures */
  5119. xfs_reinit_percpu_counters(mp);
  5120. error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  5121. if (error) {
  5122. xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
  5123. return error;
  5124. }
  5125. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  5126. xlog_recover_check_summary(log);
  5127. /* Normal transactions can now occur */
  5128. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  5129. return 0;
  5130. }
  5131. /*
  5132. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  5133. *
  5134. * Return error or zero.
  5135. */
  5136. int
  5137. xlog_recover(
  5138. struct xlog *log)
  5139. {
  5140. xfs_daddr_t head_blk, tail_blk;
  5141. int error;
  5142. /* find the tail of the log */
  5143. error = xlog_find_tail(log, &head_blk, &tail_blk);
  5144. if (error)
  5145. return error;
  5146. /*
  5147. * The superblock was read before the log was available and thus the LSN
  5148. * could not be verified. Check the superblock LSN against the current
  5149. * LSN now that it's known.
  5150. */
  5151. if (xfs_sb_version_hascrc(&log->l_mp->m_sb) &&
  5152. !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
  5153. return -EINVAL;
  5154. if (tail_blk != head_blk) {
  5155. /* There used to be a comment here:
  5156. *
  5157. * disallow recovery on read-only mounts. note -- mount
  5158. * checks for ENOSPC and turns it into an intelligent
  5159. * error message.
  5160. * ...but this is no longer true. Now, unless you specify
  5161. * NORECOVERY (in which case this function would never be
  5162. * called), we just go ahead and recover. We do this all
  5163. * under the vfs layer, so we can get away with it unless
  5164. * the device itself is read-only, in which case we fail.
  5165. */
  5166. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  5167. return error;
  5168. }
  5169. /*
  5170. * Version 5 superblock log feature mask validation. We know the
  5171. * log is dirty so check if there are any unknown log features
  5172. * in what we need to recover. If there are unknown features
  5173. * (e.g. unsupported transactions, then simply reject the
  5174. * attempt at recovery before touching anything.
  5175. */
  5176. if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  5177. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  5178. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  5179. xfs_warn(log->l_mp,
  5180. "Superblock has unknown incompatible log features (0x%x) enabled.",
  5181. (log->l_mp->m_sb.sb_features_log_incompat &
  5182. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  5183. xfs_warn(log->l_mp,
  5184. "The log can not be fully and/or safely recovered by this kernel.");
  5185. xfs_warn(log->l_mp,
  5186. "Please recover the log on a kernel that supports the unknown features.");
  5187. return -EINVAL;
  5188. }
  5189. /*
  5190. * Delay log recovery if the debug hook is set. This is debug
  5191. * instrumention to coordinate simulation of I/O failures with
  5192. * log recovery.
  5193. */
  5194. if (xfs_globals.log_recovery_delay) {
  5195. xfs_notice(log->l_mp,
  5196. "Delaying log recovery for %d seconds.",
  5197. xfs_globals.log_recovery_delay);
  5198. msleep(xfs_globals.log_recovery_delay * 1000);
  5199. }
  5200. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  5201. log->l_mp->m_logname ? log->l_mp->m_logname
  5202. : "internal");
  5203. error = xlog_do_recover(log, head_blk, tail_blk);
  5204. log->l_flags |= XLOG_RECOVERY_NEEDED;
  5205. }
  5206. return error;
  5207. }
  5208. /*
  5209. * In the first part of recovery we replay inodes and buffers and build
  5210. * up the list of extent free items which need to be processed. Here
  5211. * we process the extent free items and clean up the on disk unlinked
  5212. * inode lists. This is separated from the first part of recovery so
  5213. * that the root and real-time bitmap inodes can be read in from disk in
  5214. * between the two stages. This is necessary so that we can free space
  5215. * in the real-time portion of the file system.
  5216. */
  5217. int
  5218. xlog_recover_finish(
  5219. struct xlog *log)
  5220. {
  5221. /*
  5222. * Now we're ready to do the transactions needed for the
  5223. * rest of recovery. Start with completing all the extent
  5224. * free intent records and then process the unlinked inode
  5225. * lists. At this point, we essentially run in normal mode
  5226. * except that we're still performing recovery actions
  5227. * rather than accepting new requests.
  5228. */
  5229. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  5230. int error;
  5231. error = xlog_recover_process_intents(log);
  5232. if (error) {
  5233. xfs_alert(log->l_mp, "Failed to recover intents");
  5234. return error;
  5235. }
  5236. /*
  5237. * Sync the log to get all the intents out of the AIL.
  5238. * This isn't absolutely necessary, but it helps in
  5239. * case the unlink transactions would have problems
  5240. * pushing the intents out of the way.
  5241. */
  5242. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  5243. xlog_recover_process_iunlinks(log);
  5244. xlog_recover_check_summary(log);
  5245. xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  5246. log->l_mp->m_logname ? log->l_mp->m_logname
  5247. : "internal");
  5248. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  5249. } else {
  5250. xfs_info(log->l_mp, "Ending clean mount");
  5251. }
  5252. return 0;
  5253. }
  5254. int
  5255. xlog_recover_cancel(
  5256. struct xlog *log)
  5257. {
  5258. int error = 0;
  5259. if (log->l_flags & XLOG_RECOVERY_NEEDED)
  5260. error = xlog_recover_cancel_intents(log);
  5261. return error;
  5262. }
  5263. #if defined(DEBUG)
  5264. /*
  5265. * Read all of the agf and agi counters and check that they
  5266. * are consistent with the superblock counters.
  5267. */
  5268. STATIC void
  5269. xlog_recover_check_summary(
  5270. struct xlog *log)
  5271. {
  5272. xfs_mount_t *mp;
  5273. xfs_agf_t *agfp;
  5274. xfs_buf_t *agfbp;
  5275. xfs_buf_t *agibp;
  5276. xfs_agnumber_t agno;
  5277. uint64_t freeblks;
  5278. uint64_t itotal;
  5279. uint64_t ifree;
  5280. int error;
  5281. mp = log->l_mp;
  5282. freeblks = 0LL;
  5283. itotal = 0LL;
  5284. ifree = 0LL;
  5285. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  5286. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  5287. if (error) {
  5288. xfs_alert(mp, "%s agf read failed agno %d error %d",
  5289. __func__, agno, error);
  5290. } else {
  5291. agfp = XFS_BUF_TO_AGF(agfbp);
  5292. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  5293. be32_to_cpu(agfp->agf_flcount);
  5294. xfs_buf_relse(agfbp);
  5295. }
  5296. error = xfs_read_agi(mp, NULL, agno, &agibp);
  5297. if (error) {
  5298. xfs_alert(mp, "%s agi read failed agno %d error %d",
  5299. __func__, agno, error);
  5300. } else {
  5301. struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
  5302. itotal += be32_to_cpu(agi->agi_count);
  5303. ifree += be32_to_cpu(agi->agi_freecount);
  5304. xfs_buf_relse(agibp);
  5305. }
  5306. }
  5307. }
  5308. #endif /* DEBUG */