recovery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. journal_superblock_t * sb;
  235. struct recovery_info info;
  236. memset (&info, 0, sizeof(info));
  237. sb = journal->j_superblock;
  238. err = do_one_pass(journal, &info, PASS_SCAN);
  239. if (err) {
  240. printk(KERN_ERR "JBD: error %d scanning journal\n", err);
  241. ++journal->j_transaction_sequence;
  242. } else {
  243. #ifdef CONFIG_JBD_DEBUG
  244. int dropped = info.end_transaction - be32_to_cpu(sb->s_sequence);
  245. #endif
  246. jbd_debug(1,
  247. "JBD: ignoring %d transaction%s from the journal.\n",
  248. dropped, (dropped == 1) ? "" : "s");
  249. journal->j_transaction_sequence = ++info.end_transaction;
  250. }
  251. journal->j_tail = 0;
  252. return err;
  253. }
  254. static int do_one_pass(journal_t *journal,
  255. struct recovery_info *info, enum passtype pass)
  256. {
  257. unsigned int first_commit_ID, next_commit_ID;
  258. unsigned int next_log_block;
  259. int err, success = 0;
  260. journal_superblock_t * sb;
  261. journal_header_t * tmp;
  262. struct buffer_head * bh;
  263. unsigned int sequence;
  264. int blocktype;
  265. /* Precompute the maximum metadata descriptors in a descriptor block */
  266. int MAX_BLOCKS_PER_DESC;
  267. MAX_BLOCKS_PER_DESC = ((journal->j_blocksize-sizeof(journal_header_t))
  268. / sizeof(journal_block_tag_t));
  269. /*
  270. * First thing is to establish what we expect to find in the log
  271. * (in terms of transaction IDs), and where (in terms of log
  272. * block offsets): query the superblock.
  273. */
  274. sb = journal->j_superblock;
  275. next_commit_ID = be32_to_cpu(sb->s_sequence);
  276. next_log_block = be32_to_cpu(sb->s_start);
  277. first_commit_ID = next_commit_ID;
  278. if (pass == PASS_SCAN)
  279. info->start_transaction = first_commit_ID;
  280. jbd_debug(1, "Starting recovery pass %d\n", pass);
  281. /*
  282. * Now we walk through the log, transaction by transaction,
  283. * making sure that each transaction has a commit block in the
  284. * expected place. Each complete transaction gets replayed back
  285. * into the main filesystem.
  286. */
  287. while (1) {
  288. int flags;
  289. char * tagp;
  290. journal_block_tag_t * tag;
  291. struct buffer_head * obh;
  292. struct buffer_head * nbh;
  293. cond_resched();
  294. /* If we already know where to stop the log traversal,
  295. * check right now that we haven't gone past the end of
  296. * the log. */
  297. if (pass != PASS_SCAN)
  298. if (tid_geq(next_commit_ID, info->end_transaction))
  299. break;
  300. jbd_debug(2, "Scanning for sequence ID %u at %u/%u\n",
  301. next_commit_ID, next_log_block, journal->j_last);
  302. /* Skip over each chunk of the transaction looking
  303. * either the next descriptor block or the final commit
  304. * record. */
  305. jbd_debug(3, "JBD: checking block %u\n", next_log_block);
  306. err = jread(&bh, journal, next_log_block);
  307. if (err)
  308. goto failed;
  309. next_log_block++;
  310. wrap(journal, next_log_block);
  311. /* What kind of buffer is it?
  312. *
  313. * If it is a descriptor block, check that it has the
  314. * expected sequence number. Otherwise, we're all done
  315. * here. */
  316. tmp = (journal_header_t *)bh->b_data;
  317. if (tmp->h_magic != cpu_to_be32(JFS_MAGIC_NUMBER)) {
  318. brelse(bh);
  319. break;
  320. }
  321. blocktype = be32_to_cpu(tmp->h_blocktype);
  322. sequence = be32_to_cpu(tmp->h_sequence);
  323. jbd_debug(3, "Found magic %d, sequence %d\n",
  324. blocktype, sequence);
  325. if (sequence != next_commit_ID) {
  326. brelse(bh);
  327. break;
  328. }
  329. /* OK, we have a valid descriptor block which matches
  330. * all of the sequence number checks. What are we going
  331. * to do with it? That depends on the pass... */
  332. switch(blocktype) {
  333. case JFS_DESCRIPTOR_BLOCK:
  334. /* If it is a valid descriptor block, replay it
  335. * in pass REPLAY; otherwise, just skip over the
  336. * blocks it describes. */
  337. if (pass != PASS_REPLAY) {
  338. next_log_block +=
  339. count_tags(bh, journal->j_blocksize);
  340. wrap(journal, next_log_block);
  341. brelse(bh);
  342. continue;
  343. }
  344. /* A descriptor block: we can now write all of
  345. * the data blocks. Yay, useful work is finally
  346. * getting done here! */
  347. tagp = &bh->b_data[sizeof(journal_header_t)];
  348. while ((tagp - bh->b_data +sizeof(journal_block_tag_t))
  349. <= journal->j_blocksize) {
  350. unsigned int io_block;
  351. tag = (journal_block_tag_t *) tagp;
  352. flags = be32_to_cpu(tag->t_flags);
  353. io_block = next_log_block++;
  354. wrap(journal, next_log_block);
  355. err = jread(&obh, journal, io_block);
  356. if (err) {
  357. /* Recover what we can, but
  358. * report failure at the end. */
  359. success = err;
  360. printk (KERN_ERR
  361. "JBD: IO error %d recovering "
  362. "block %u in log\n",
  363. err, io_block);
  364. } else {
  365. unsigned int blocknr;
  366. J_ASSERT(obh != NULL);
  367. blocknr = be32_to_cpu(tag->t_blocknr);
  368. /* If the block has been
  369. * revoked, then we're all done
  370. * here. */
  371. if (journal_test_revoke
  372. (journal, blocknr,
  373. next_commit_ID)) {
  374. brelse(obh);
  375. ++info->nr_revoke_hits;
  376. goto skip_write;
  377. }
  378. /* Find a buffer for the new
  379. * data being restored */
  380. nbh = __getblk(journal->j_fs_dev,
  381. blocknr,
  382. journal->j_blocksize);
  383. if (nbh == NULL) {
  384. printk(KERN_ERR
  385. "JBD: Out of memory "
  386. "during recovery.\n");
  387. err = -ENOMEM;
  388. brelse(bh);
  389. brelse(obh);
  390. goto failed;
  391. }
  392. lock_buffer(nbh);
  393. memcpy(nbh->b_data, obh->b_data,
  394. journal->j_blocksize);
  395. if (flags & JFS_FLAG_ESCAPE) {
  396. *((__be32 *)nbh->b_data) =
  397. cpu_to_be32(JFS_MAGIC_NUMBER);
  398. }
  399. BUFFER_TRACE(nbh, "marking dirty");
  400. set_buffer_uptodate(nbh);
  401. mark_buffer_dirty(nbh);
  402. BUFFER_TRACE(nbh, "marking uptodate");
  403. ++info->nr_replays;
  404. /* ll_rw_block(WRITE, 1, &nbh); */
  405. unlock_buffer(nbh);
  406. brelse(obh);
  407. brelse(nbh);
  408. }
  409. skip_write:
  410. tagp += sizeof(journal_block_tag_t);
  411. if (!(flags & JFS_FLAG_SAME_UUID))
  412. tagp += 16;
  413. if (flags & JFS_FLAG_LAST_TAG)
  414. break;
  415. }
  416. brelse(bh);
  417. continue;
  418. case JFS_COMMIT_BLOCK:
  419. /* Found an expected commit block: not much to
  420. * do other than move on to the next sequence
  421. * number. */
  422. brelse(bh);
  423. next_commit_ID++;
  424. continue;
  425. case JFS_REVOKE_BLOCK:
  426. /* If we aren't in the REVOKE pass, then we can
  427. * just skip over this block. */
  428. if (pass != PASS_REVOKE) {
  429. brelse(bh);
  430. continue;
  431. }
  432. err = scan_revoke_records(journal, bh,
  433. next_commit_ID, info);
  434. brelse(bh);
  435. if (err)
  436. goto failed;
  437. continue;
  438. default:
  439. jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
  440. blocktype);
  441. brelse(bh);
  442. goto done;
  443. }
  444. }
  445. done:
  446. /*
  447. * We broke out of the log scan loop: either we came to the
  448. * known end of the log or we found an unexpected block in the
  449. * log. If the latter happened, then we know that the "current"
  450. * transaction marks the end of the valid log.
  451. */
  452. if (pass == PASS_SCAN)
  453. info->end_transaction = next_commit_ID;
  454. else {
  455. /* It's really bad news if different passes end up at
  456. * different places (but possible due to IO errors). */
  457. if (info->end_transaction != next_commit_ID) {
  458. printk (KERN_ERR "JBD: recovery pass %d ended at "
  459. "transaction %u, expected %u\n",
  460. pass, next_commit_ID, info->end_transaction);
  461. if (!success)
  462. success = -EIO;
  463. }
  464. }
  465. return success;
  466. failed:
  467. return err;
  468. }
  469. /* Scan a revoke record, marking all blocks mentioned as revoked. */
  470. static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
  471. tid_t sequence, struct recovery_info *info)
  472. {
  473. journal_revoke_header_t *header;
  474. int offset, max;
  475. header = (journal_revoke_header_t *) bh->b_data;
  476. offset = sizeof(journal_revoke_header_t);
  477. max = be32_to_cpu(header->r_count);
  478. while (offset < max) {
  479. unsigned int blocknr;
  480. int err;
  481. blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
  482. offset += 4;
  483. err = journal_set_revoke(journal, blocknr, sequence);
  484. if (err)
  485. return err;
  486. ++info->nr_revokes;
  487. }
  488. return 0;
  489. }