recovery.c 14 KB

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