PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 2ms

/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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file