recovery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * linux/fs/jbd/recovery.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
  5. *
  6. * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * Journal recovery routines for the generic filesystem journaling code;
  13. * part of the ext2fs journaling system.
  14. */
  15. #ifndef __KERNEL__
  16. #include "jfs_user.h"
  17. #else
  18. #include <linux/time.h>
  19. #include <linux/fs.h>
  20. #include <linux/jbd.h>
  21. #include <linux/errno.h>
  22. #include <linux/blkdev.h>
  23. #endif
  24. /*
  25. * Maintain information about the progress of the recovery job, so that
  26. * the different passes can carry information between them.
  27. */
  28. struct recovery_info
  29. {
  30. tid_t start_transaction;
  31. tid_t end_transaction;
  32. int nr_replays;
  33. int nr_revokes;
  34. int nr_revoke_hits;
  35. };
  36. enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
  37. static int do_one_pass(journal_t *journal,
  38. struct recovery_info *info, enum passtype pass);
  39. static int scan_revoke_records(journal_t *, struct buffer_head *,
  40. tid_t, struct recovery_info *);
  41. #ifdef __KERNEL__
  42. /* Release readahead buffers after use */
  43. static void journal_brelse_array(struct buffer_head *b[], int n)
  44. {
  45. while (--n >= 0)
  46. brelse (b[n]);
  47. }
  48. /*
  49. * When reading from the journal, we are going through the block device
  50. * layer directly and so there is no readahead being done for us. We
  51. * need to implement any readahead ourselves if we want it to happen at
  52. * all. Recovery is basically one long sequential read, so make sure we
  53. * do the IO in reasonably large chunks.
  54. *
  55. * This is not so critical that we need to be enormously clever about
  56. * the readahead size, though. 128K is a purely arbitrary, good-enough
  57. * fixed value.
  58. */
  59. #define MAXBUF 8
  60. static int do_readahead(journal_t *journal, unsigned int start)
  61. {
  62. int err;
  63. unsigned int max, nbufs, next;
  64. unsigned int blocknr;
  65. struct buffer_head *bh;
  66. struct buffer_head * bufs[MAXBUF];
  67. /* Do up to 128K of readahead */
  68. max = start + (128 * 1024 / journal->j_blocksize);
  69. if (max > journal->j_maxlen)
  70. max = journal->j_maxlen;
  71. /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
  72. * a time to the block device IO layer. */
  73. nbufs = 0;
  74. for (next = start; next < max; next++) {
  75. err = journal_bmap(journal, next, &blocknr);
  76. if (err) {
  77. printk (KERN_ERR "JBD: bad block at offset %u\n",
  78. next);
  79. goto failed;
  80. }
  81. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  82. if (!bh) {
  83. err = -ENOMEM;
  84. goto failed;
  85. }
  86. if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
  87. bufs[nbufs++] = bh;
  88. if (nbufs == MAXBUF) {
  89. ll_rw_block(READ, nbufs, bufs);
  90. journal_brelse_array(bufs, nbufs);
  91. nbufs = 0;
  92. }
  93. } else
  94. brelse(bh);
  95. }
  96. if (nbufs)
  97. ll_rw_block(READ, nbufs, bufs);
  98. err = 0;
  99. failed:
  100. if (nbufs)
  101. journal_brelse_array(bufs, nbufs);
  102. return err;
  103. }
  104. #endif /* __KERNEL__ */
  105. /*
  106. * Read a block from the journal
  107. */
  108. static int jread(struct buffer_head **bhp, journal_t *journal,
  109. unsigned int offset)
  110. {
  111. int err;
  112. unsigned int blocknr;
  113. struct buffer_head *bh;
  114. *bhp = NULL;
  115. if (offset >= journal->j_maxlen) {
  116. printk(KERN_ERR "JBD: corrupted journal superblock\n");
  117. return -EIO;
  118. }
  119. err = journal_bmap(journal, offset, &blocknr);
  120. if (err) {
  121. printk (KERN_ERR "JBD: bad block at offset %u\n",
  122. offset);
  123. return err;
  124. }
  125. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  126. if (!bh)
  127. return -ENOMEM;
  128. if (!buffer_uptodate(bh)) {
  129. /* If this is a brand new buffer, start readahead.
  130. Otherwise, we assume we are already reading it. */
  131. if (!buffer_req(bh))
  132. do_readahead(journal, offset);
  133. wait_on_buffer(bh);
  134. }
  135. if (!buffer_uptodate(bh)) {
  136. printk (KERN_ERR "JBD: Failed to read block at offset %u\n",
  137. offset);
  138. brelse(bh);
  139. return -EIO;
  140. }
  141. *bhp = bh;
  142. return 0;
  143. }
  144. /*
  145. * Count the number of in-use tags in a journal descriptor block.
  146. */
  147. static int count_tags(struct buffer_head *bh, int size)
  148. {
  149. char * tagp;
  150. journal_block_tag_t * tag;
  151. int nr = 0;
  152. tagp = &bh->b_data[sizeof(journal_header_t)];
  153. while ((tagp - bh->b_data + sizeof(journal_block_tag_t)) <= size) {
  154. tag = (journal_block_tag_t *) tagp;
  155. nr++;
  156. tagp += sizeof(journal_block_tag_t);
  157. if (!(tag->t_flags & cpu_to_be32(JFS_FLAG_SAME_UUID)))
  158. tagp += 16;
  159. if (tag->t_flags & cpu_to_be32(JFS_FLAG_LAST_TAG))
  160. break;
  161. }
  162. return nr;
  163. }
  164. /* Make sure we wrap around the log correctly! */
  165. #define wrap(journal, var) \
  166. do { \
  167. if (var >= (journal)->j_last) \
  168. var -= ((journal)->j_last - (journal)->j_first); \
  169. } while (0)
  170. /**
  171. * journal_recover - recovers a on-disk journal
  172. * @journal: the journal to recover
  173. *
  174. * The primary function for recovering the log contents when mounting a
  175. * journaled device.
  176. *
  177. * Recovery is done in three passes. In the first pass, we look for the
  178. * end of the log. In the second, we assemble the list of revoke
  179. * blocks. In the third and final pass, we replay any un-revoked blocks
  180. * in the log.
  181. */
  182. int journal_recover(journal_t *journal)
  183. {
  184. int err, err2;
  185. journal_superblock_t * sb;
  186. struct recovery_info info;
  187. memset(&info, 0, sizeof(info));
  188. sb = journal->j_superblock;
  189. /*
  190. * The journal superblock's s_start field (the current log head)
  191. * is always zero if, and only if, the journal was cleanly
  192. * unmounted.
  193. */
  194. if (!sb->s_start) {
  195. jbd_debug(1, "No recovery required, last transaction %d\n",
  196. be32_to_cpu(sb->s_sequence));
  197. journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
  198. return 0;
  199. }
  200. err = do_one_pass(journal, &info, PASS_SCAN);
  201. if (!err)
  202. err = do_one_pass(journal, &info, PASS_REVOKE);
  203. if (!err)
  204. err = do_one_pass(journal, &info, PASS_REPLAY);
  205. jbd_debug(1, "JBD: recovery, exit status %d, "
  206. "recovered transactions %u to %u\n",
  207. err, info.start_transaction, info.end_transaction);
  208. jbd_debug(1, "JBD: Replayed %d and revoked %d/%d blocks\n",
  209. info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
  210. /* Restart the log at the next transaction ID, thus invalidating
  211. * any existing commit records in the log. */
  212. journal->j_transaction_sequence = ++info.end_transaction;
  213. journal_clear_revoke(journal);
  214. err2 = sync_blockdev(journal->j_fs_dev);
  215. if (!err)
  216. err = err2;
  217. /* Flush disk caches to get replayed data on the permanent storage */
  218. if (journal->j_flags & JFS_BARRIER) {
  219. err2 = blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
  220. if (!err)
  221. err = err2;
  222. }
  223. return err;
  224. }
  225. /**
  226. * journal_skip_recovery - Start journal and wipe exiting records
  227. * @journal: journal to startup
  228. *
  229. * Locate any valid recovery information from the journal and set up the
  230. * journal structures in memory to ignore it (presumably because the
  231. * caller has evidence that it is out of date).
  232. * This function does'nt appear to be exorted..
  233. *
  234. * We perform one pass over the journal to allow us to tell the user how
  235. * much recovery information is being erased, and to let us initialise
  236. * the journal transaction sequence numbers to the next unused ID.
  237. */
  238. int journal_skip_recovery(journal_t *journal)
  239. {
  240. int err;
  241. struct recovery_info info;
  242. memset (&info, 0, sizeof(info));
  243. err = do_one_pass(journal, &info, PASS_SCAN);
  244. if (err) {
  245. printk(KERN_ERR "JBD: error %d scanning journal\n", err);
  246. ++journal->j_transaction_sequence;
  247. } else {
  248. #ifdef CONFIG_JBD_DEBUG
  249. int dropped = info.end_transaction -
  250. be32_to_cpu(journal->j_superblock->s_sequence);
  251. jbd_debug(1,
  252. "JBD: ignoring %d transaction%s from the journal.\n",
  253. dropped, (dropped == 1) ? "" : "s");
  254. #endif
  255. journal->j_transaction_sequence = ++info.end_transaction;
  256. }
  257. journal->j_tail = 0;
  258. return err;
  259. }
  260. static int do_one_pass(journal_t *journal,
  261. struct recovery_info *info, enum passtype pass)
  262. {
  263. unsigned int first_commit_ID, next_commit_ID;
  264. unsigned int next_log_block;
  265. int err, success = 0;
  266. journal_superblock_t * sb;
  267. journal_header_t * tmp;
  268. struct buffer_head * bh;
  269. unsigned int sequence;
  270. int blocktype;
  271. /*
  272. * First thing is to establish what we expect to find in the log
  273. * (in terms of transaction IDs), and where (in terms of log
  274. * block offsets): query the superblock.
  275. */
  276. sb = journal->j_superblock;
  277. next_commit_ID = be32_to_cpu(sb->s_sequence);
  278. next_log_block = be32_to_cpu(sb->s_start);
  279. first_commit_ID = next_commit_ID;
  280. if (pass == PASS_SCAN)
  281. info->start_transaction = first_commit_ID;
  282. jbd_debug(1, "Starting recovery pass %d\n", pass);
  283. /*
  284. * Now we walk through the log, transaction by transaction,
  285. * making sure that each transaction has a commit block in the
  286. * expected place. Each complete transaction gets replayed back
  287. * into the main filesystem.
  288. */
  289. while (1) {
  290. int flags;
  291. char * tagp;
  292. journal_block_tag_t * tag;
  293. struct buffer_head * obh;
  294. struct buffer_head * nbh;
  295. cond_resched();
  296. /* If we already know where to stop the log traversal,
  297. * check right now that we haven't gone past the end of
  298. * the log. */
  299. if (pass != PASS_SCAN)
  300. if (tid_geq(next_commit_ID, info->end_transaction))
  301. break;
  302. jbd_debug(2, "Scanning for sequence ID %u at %u/%u\n",
  303. next_commit_ID, next_log_block, journal->j_last);
  304. /* Skip over each chunk of the transaction looking
  305. * either the next descriptor block or the final commit
  306. * record. */
  307. jbd_debug(3, "JBD: checking block %u\n", next_log_block);
  308. err = jread(&bh, journal, next_log_block);
  309. if (err)
  310. goto failed;
  311. next_log_block++;
  312. wrap(journal, next_log_block);
  313. /* What kind of buffer is it?
  314. *
  315. * If it is a descriptor block, check that it has the
  316. * expected sequence number. Otherwise, we're all done
  317. * here. */
  318. tmp = (journal_header_t *)bh->b_data;
  319. if (tmp->h_magic != cpu_to_be32(JFS_MAGIC_NUMBER)) {
  320. brelse(bh);
  321. break;
  322. }
  323. blocktype = be32_to_cpu(tmp->h_blocktype);
  324. sequence = be32_to_cpu(tmp->h_sequence);
  325. jbd_debug(3, "Found magic %d, sequence %d\n",
  326. blocktype, sequence);
  327. if (sequence != next_commit_ID) {
  328. brelse(bh);
  329. break;
  330. }
  331. /* OK, we have a valid descriptor block which matches
  332. * all of the sequence number checks. What are we going
  333. * to do with it? That depends on the pass... */
  334. switch(blocktype) {
  335. case JFS_DESCRIPTOR_BLOCK:
  336. /* If it is a valid descriptor block, replay it
  337. * in pass REPLAY; otherwise, just skip over the
  338. * blocks it describes. */
  339. if (pass != PASS_REPLAY) {
  340. next_log_block +=
  341. count_tags(bh, journal->j_blocksize);
  342. wrap(journal, next_log_block);
  343. brelse(bh);
  344. continue;
  345. }
  346. /* A descriptor block: we can now write all of
  347. * the data blocks. Yay, useful work is finally
  348. * getting done here! */
  349. tagp = &bh->b_data[sizeof(journal_header_t)];
  350. while ((tagp - bh->b_data +sizeof(journal_block_tag_t))
  351. <= journal->j_blocksize) {
  352. unsigned int io_block;
  353. tag = (journal_block_tag_t *) tagp;
  354. flags = be32_to_cpu(tag->t_flags);
  355. io_block = next_log_block++;
  356. wrap(journal, next_log_block);
  357. err = jread(&obh, journal, io_block);
  358. if (err) {
  359. /* Recover what we can, but
  360. * report failure at the end. */
  361. success = err;
  362. printk (KERN_ERR
  363. "JBD: IO error %d recovering "
  364. "block %u in log\n",
  365. err, io_block);
  366. } else {
  367. unsigned int blocknr;
  368. J_ASSERT(obh != NULL);
  369. blocknr = be32_to_cpu(tag->t_blocknr);
  370. /* If the block has been
  371. * revoked, then we're all done
  372. * here. */
  373. if (journal_test_revoke
  374. (journal, blocknr,
  375. next_commit_ID)) {
  376. brelse(obh);
  377. ++info->nr_revoke_hits;
  378. goto skip_write;
  379. }
  380. /* Find a buffer for the new
  381. * data being restored */
  382. nbh = __getblk(journal->j_fs_dev,
  383. blocknr,
  384. journal->j_blocksize);
  385. if (nbh == NULL) {
  386. printk(KERN_ERR
  387. "JBD: Out of memory "
  388. "during recovery.\n");
  389. err = -ENOMEM;
  390. brelse(bh);
  391. brelse(obh);
  392. goto failed;
  393. }
  394. lock_buffer(nbh);
  395. memcpy(nbh->b_data, obh->b_data,
  396. journal->j_blocksize);
  397. if (flags & JFS_FLAG_ESCAPE) {
  398. *((__be32 *)nbh->b_data) =
  399. cpu_to_be32(JFS_MAGIC_NUMBER);
  400. }
  401. BUFFER_TRACE(nbh, "marking dirty");
  402. set_buffer_uptodate(nbh);
  403. mark_buffer_dirty(nbh);
  404. BUFFER_TRACE(nbh, "marking uptodate");
  405. ++info->nr_replays;
  406. /* ll_rw_block(WRITE, 1, &nbh); */
  407. unlock_buffer(nbh);
  408. brelse(obh);
  409. brelse(nbh);
  410. }
  411. skip_write:
  412. tagp += sizeof(journal_block_tag_t);
  413. if (!(flags & JFS_FLAG_SAME_UUID))
  414. tagp += 16;
  415. if (flags & JFS_FLAG_LAST_TAG)
  416. break;
  417. }
  418. brelse(bh);
  419. continue;
  420. case JFS_COMMIT_BLOCK:
  421. /* Found an expected commit block: not much to
  422. * do other than move on to the next sequence
  423. * number. */
  424. brelse(bh);
  425. next_commit_ID++;
  426. continue;
  427. case JFS_REVOKE_BLOCK:
  428. /* If we aren't in the REVOKE pass, then we can
  429. * just skip over this block. */
  430. if (pass != PASS_REVOKE) {
  431. brelse(bh);
  432. continue;
  433. }
  434. err = scan_revoke_records(journal, bh,
  435. next_commit_ID, info);
  436. brelse(bh);
  437. if (err)
  438. goto failed;
  439. continue;
  440. default:
  441. jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
  442. blocktype);
  443. brelse(bh);
  444. goto done;
  445. }
  446. }
  447. done:
  448. /*
  449. * We broke out of the log scan loop: either we came to the
  450. * known end of the log or we found an unexpected block in the
  451. * log. If the latter happened, then we know that the "current"
  452. * transaction marks the end of the valid log.
  453. */
  454. if (pass == PASS_SCAN)
  455. info->end_transaction = next_commit_ID;
  456. else {
  457. /* It's really bad news if different passes end up at
  458. * different places (but possible due to IO errors). */
  459. if (info->end_transaction != next_commit_ID) {
  460. printk (KERN_ERR "JBD: recovery pass %d ended at "
  461. "transaction %u, expected %u\n",
  462. pass, next_commit_ID, info->end_transaction);
  463. if (!success)
  464. success = -EIO;
  465. }
  466. }
  467. return success;
  468. failed:
  469. return err;
  470. }
  471. /* Scan a revoke record, marking all blocks mentioned as revoked. */
  472. static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
  473. tid_t sequence, struct recovery_info *info)
  474. {
  475. journal_revoke_header_t *header;
  476. int offset, max;
  477. header = (journal_revoke_header_t *) bh->b_data;
  478. offset = sizeof(journal_revoke_header_t);
  479. max = be32_to_cpu(header->r_count);
  480. while (offset < max) {
  481. unsigned int blocknr;
  482. int err;
  483. blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
  484. offset += 4;
  485. err = journal_set_revoke(journal, blocknr, sequence);
  486. if (err)
  487. return err;
  488. ++info->nr_revokes;
  489. }
  490. return 0;
  491. }