recovery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include <linux/lm_interface.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "bmap.h"
  20. #include "glock.h"
  21. #include "glops.h"
  22. #include "lm.h"
  23. #include "lops.h"
  24. #include "meta_io.h"
  25. #include "recovery.h"
  26. #include "super.h"
  27. #include "util.h"
  28. #include "dir.h"
  29. int gfs2_replay_read_block(struct gfs2_jdesc *jd, unsigned int blk,
  30. struct buffer_head **bh)
  31. {
  32. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  33. struct gfs2_glock *gl = ip->i_gl;
  34. int new = 0;
  35. u64 dblock;
  36. u32 extlen;
  37. int error;
  38. error = gfs2_extent_map(&ip->i_inode, blk, &new, &dblock, &extlen);
  39. if (error)
  40. return error;
  41. if (!dblock) {
  42. gfs2_consist_inode(ip);
  43. return -EIO;
  44. }
  45. *bh = gfs2_meta_ra(gl, dblock, extlen);
  46. return error;
  47. }
  48. int gfs2_revoke_add(struct gfs2_sbd *sdp, u64 blkno, unsigned int where)
  49. {
  50. struct list_head *head = &sdp->sd_revoke_list;
  51. struct gfs2_revoke_replay *rr;
  52. int found = 0;
  53. list_for_each_entry(rr, head, rr_list) {
  54. if (rr->rr_blkno == blkno) {
  55. found = 1;
  56. break;
  57. }
  58. }
  59. if (found) {
  60. rr->rr_where = where;
  61. return 0;
  62. }
  63. rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_KERNEL);
  64. if (!rr)
  65. return -ENOMEM;
  66. rr->rr_blkno = blkno;
  67. rr->rr_where = where;
  68. list_add(&rr->rr_list, head);
  69. return 1;
  70. }
  71. int gfs2_revoke_check(struct gfs2_sbd *sdp, u64 blkno, unsigned int where)
  72. {
  73. struct gfs2_revoke_replay *rr;
  74. int wrap, a, b, revoke;
  75. int found = 0;
  76. list_for_each_entry(rr, &sdp->sd_revoke_list, rr_list) {
  77. if (rr->rr_blkno == blkno) {
  78. found = 1;
  79. break;
  80. }
  81. }
  82. if (!found)
  83. return 0;
  84. wrap = (rr->rr_where < sdp->sd_replay_tail);
  85. a = (sdp->sd_replay_tail < where);
  86. b = (where < rr->rr_where);
  87. revoke = (wrap) ? (a || b) : (a && b);
  88. return revoke;
  89. }
  90. void gfs2_revoke_clean(struct gfs2_sbd *sdp)
  91. {
  92. struct list_head *head = &sdp->sd_revoke_list;
  93. struct gfs2_revoke_replay *rr;
  94. while (!list_empty(head)) {
  95. rr = list_entry(head->next, struct gfs2_revoke_replay, rr_list);
  96. list_del(&rr->rr_list);
  97. kfree(rr);
  98. }
  99. }
  100. /**
  101. * get_log_header - read the log header for a given segment
  102. * @jd: the journal
  103. * @blk: the block to look at
  104. * @lh: the log header to return
  105. *
  106. * Read the log header for a given segement in a given journal. Do a few
  107. * sanity checks on it.
  108. *
  109. * Returns: 0 on success,
  110. * 1 if the header was invalid or incomplete,
  111. * errno on error
  112. */
  113. static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
  114. struct gfs2_log_header *head)
  115. {
  116. struct buffer_head *bh;
  117. struct gfs2_log_header lh;
  118. u32 hash;
  119. int error;
  120. error = gfs2_replay_read_block(jd, blk, &bh);
  121. if (error)
  122. return error;
  123. memcpy(&lh, bh->b_data, sizeof(struct gfs2_log_header));
  124. lh.lh_hash = 0;
  125. hash = gfs2_disk_hash((char *)&lh, sizeof(struct gfs2_log_header));
  126. gfs2_log_header_in(&lh, bh->b_data);
  127. brelse(bh);
  128. if (lh.lh_header.mh_magic != GFS2_MAGIC ||
  129. lh.lh_header.mh_type != GFS2_METATYPE_LH ||
  130. lh.lh_blkno != blk || lh.lh_hash != hash)
  131. return 1;
  132. *head = lh;
  133. return 0;
  134. }
  135. /**
  136. * find_good_lh - find a good log header
  137. * @jd: the journal
  138. * @blk: the segment to start searching from
  139. * @lh: the log header to fill in
  140. * @forward: if true search forward in the log, else search backward
  141. *
  142. * Call get_log_header() to get a log header for a segment, but if the
  143. * segment is bad, either scan forward or backward until we find a good one.
  144. *
  145. * Returns: errno
  146. */
  147. static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk,
  148. struct gfs2_log_header *head)
  149. {
  150. unsigned int orig_blk = *blk;
  151. int error;
  152. for (;;) {
  153. error = get_log_header(jd, *blk, head);
  154. if (error <= 0)
  155. return error;
  156. if (++*blk == jd->jd_blocks)
  157. *blk = 0;
  158. if (*blk == orig_blk) {
  159. gfs2_consist_inode(GFS2_I(jd->jd_inode));
  160. return -EIO;
  161. }
  162. }
  163. }
  164. /**
  165. * jhead_scan - make sure we've found the head of the log
  166. * @jd: the journal
  167. * @head: this is filled in with the log descriptor of the head
  168. *
  169. * At this point, seg and lh should be either the head of the log or just
  170. * before. Scan forward until we find the head.
  171. *
  172. * Returns: errno
  173. */
  174. static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header *head)
  175. {
  176. unsigned int blk = head->lh_blkno;
  177. struct gfs2_log_header lh;
  178. int error;
  179. for (;;) {
  180. if (++blk == jd->jd_blocks)
  181. blk = 0;
  182. error = get_log_header(jd, blk, &lh);
  183. if (error < 0)
  184. return error;
  185. if (error == 1)
  186. continue;
  187. if (lh.lh_sequence == head->lh_sequence) {
  188. gfs2_consist_inode(GFS2_I(jd->jd_inode));
  189. return -EIO;
  190. }
  191. if (lh.lh_sequence < head->lh_sequence)
  192. break;
  193. *head = lh;
  194. }
  195. return 0;
  196. }
  197. /**
  198. * gfs2_find_jhead - find the head of a log
  199. * @jd: the journal
  200. * @head: the log descriptor for the head of the log is returned here
  201. *
  202. * Do a binary search of a journal and find the valid log entry with the
  203. * highest sequence number. (i.e. the log head)
  204. *
  205. * Returns: errno
  206. */
  207. int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header *head)
  208. {
  209. struct gfs2_log_header lh_1, lh_m;
  210. u32 blk_1, blk_2, blk_m;
  211. int error;
  212. blk_1 = 0;
  213. blk_2 = jd->jd_blocks - 1;
  214. for (;;) {
  215. blk_m = (blk_1 + blk_2) / 2;
  216. error = find_good_lh(jd, &blk_1, &lh_1);
  217. if (error)
  218. return error;
  219. error = find_good_lh(jd, &blk_m, &lh_m);
  220. if (error)
  221. return error;
  222. if (blk_1 == blk_m || blk_m == blk_2)
  223. break;
  224. if (lh_1.lh_sequence <= lh_m.lh_sequence)
  225. blk_1 = blk_m;
  226. else
  227. blk_2 = blk_m;
  228. }
  229. error = jhead_scan(jd, &lh_1);
  230. if (error)
  231. return error;
  232. *head = lh_1;
  233. return error;
  234. }
  235. /**
  236. * foreach_descriptor - go through the active part of the log
  237. * @jd: the journal
  238. * @start: the first log header in the active region
  239. * @end: the last log header (don't process the contents of this entry))
  240. *
  241. * Call a given function once for every log descriptor in the active
  242. * portion of the log.
  243. *
  244. * Returns: errno
  245. */
  246. static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start,
  247. unsigned int end, int pass)
  248. {
  249. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  250. struct buffer_head *bh;
  251. struct gfs2_log_descriptor *ld;
  252. int error = 0;
  253. u32 length;
  254. __be64 *ptr;
  255. unsigned int offset = sizeof(struct gfs2_log_descriptor);
  256. offset += sizeof(__be64) - 1;
  257. offset &= ~(sizeof(__be64) - 1);
  258. while (start != end) {
  259. error = gfs2_replay_read_block(jd, start, &bh);
  260. if (error)
  261. return error;
  262. if (gfs2_meta_check(sdp, bh)) {
  263. brelse(bh);
  264. return -EIO;
  265. }
  266. ld = (struct gfs2_log_descriptor *)bh->b_data;
  267. length = be32_to_cpu(ld->ld_length);
  268. if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
  269. struct gfs2_log_header lh;
  270. error = get_log_header(jd, start, &lh);
  271. if (!error) {
  272. gfs2_replay_incr_blk(sdp, &start);
  273. brelse(bh);
  274. continue;
  275. }
  276. if (error == 1) {
  277. gfs2_consist_inode(GFS2_I(jd->jd_inode));
  278. error = -EIO;
  279. }
  280. brelse(bh);
  281. return error;
  282. } else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
  283. brelse(bh);
  284. return -EIO;
  285. }
  286. ptr = (__be64 *)(bh->b_data + offset);
  287. error = lops_scan_elements(jd, start, ld, ptr, pass);
  288. if (error) {
  289. brelse(bh);
  290. return error;
  291. }
  292. while (length--)
  293. gfs2_replay_incr_blk(sdp, &start);
  294. brelse(bh);
  295. }
  296. return 0;
  297. }
  298. /**
  299. * clean_journal - mark a dirty journal as being clean
  300. * @sdp: the filesystem
  301. * @jd: the journal
  302. * @gl: the journal's glock
  303. * @head: the head journal to start from
  304. *
  305. * Returns: errno
  306. */
  307. static int clean_journal(struct gfs2_jdesc *jd, struct gfs2_log_header *head)
  308. {
  309. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  310. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  311. unsigned int lblock;
  312. struct gfs2_log_header *lh;
  313. u32 hash;
  314. struct buffer_head *bh;
  315. int error;
  316. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  317. lblock = head->lh_blkno;
  318. gfs2_replay_incr_blk(sdp, &lblock);
  319. bh_map.b_size = 1 << ip->i_inode.i_blkbits;
  320. error = gfs2_block_map(&ip->i_inode, lblock, 0, &bh_map);
  321. if (error)
  322. return error;
  323. if (!bh_map.b_blocknr) {
  324. gfs2_consist_inode(ip);
  325. return -EIO;
  326. }
  327. bh = sb_getblk(sdp->sd_vfs, bh_map.b_blocknr);
  328. lock_buffer(bh);
  329. memset(bh->b_data, 0, bh->b_size);
  330. set_buffer_uptodate(bh);
  331. clear_buffer_dirty(bh);
  332. unlock_buffer(bh);
  333. lh = (struct gfs2_log_header *)bh->b_data;
  334. memset(lh, 0, sizeof(struct gfs2_log_header));
  335. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  336. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  337. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  338. lh->lh_sequence = cpu_to_be64(head->lh_sequence + 1);
  339. lh->lh_flags = cpu_to_be32(GFS2_LOG_HEAD_UNMOUNT);
  340. lh->lh_blkno = cpu_to_be32(lblock);
  341. hash = gfs2_disk_hash((const char *)lh, sizeof(struct gfs2_log_header));
  342. lh->lh_hash = cpu_to_be32(hash);
  343. set_buffer_dirty(bh);
  344. if (sync_dirty_buffer(bh))
  345. gfs2_io_error_bh(sdp, bh);
  346. brelse(bh);
  347. return error;
  348. }
  349. /**
  350. * gfs2_recover_journal - recovery a given journal
  351. * @jd: the struct gfs2_jdesc describing the journal
  352. *
  353. * Acquire the journal's lock, check to see if the journal is clean, and
  354. * do recovery if necessary.
  355. *
  356. * Returns: errno
  357. */
  358. int gfs2_recover_journal(struct gfs2_jdesc *jd)
  359. {
  360. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  361. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  362. struct gfs2_log_header head;
  363. struct gfs2_holder j_gh, ji_gh, t_gh;
  364. unsigned long t;
  365. int ro = 0;
  366. unsigned int pass;
  367. int error;
  368. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
  369. fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
  370. jd->jd_jid);
  371. /* Aquire the journal lock so we can do recovery */
  372. error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
  373. LM_ST_EXCLUSIVE,
  374. LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
  375. &j_gh);
  376. switch (error) {
  377. case 0:
  378. break;
  379. case GLR_TRYFAILED:
  380. fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
  381. error = 0;
  382. default:
  383. goto fail;
  384. };
  385. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
  386. LM_FLAG_NOEXP, &ji_gh);
  387. if (error)
  388. goto fail_gunlock_j;
  389. } else {
  390. fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
  391. }
  392. fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
  393. error = gfs2_jdesc_check(jd);
  394. if (error)
  395. goto fail_gunlock_ji;
  396. error = gfs2_find_jhead(jd, &head);
  397. if (error)
  398. goto fail_gunlock_ji;
  399. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  400. fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
  401. jd->jd_jid);
  402. t = jiffies;
  403. /* Acquire a shared hold on the transaction lock */
  404. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
  405. LM_FLAG_NOEXP | LM_FLAG_PRIORITY |
  406. GL_NOCANCEL | GL_NOCACHE, &t_gh);
  407. if (error)
  408. goto fail_gunlock_ji;
  409. if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
  410. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  411. ro = 1;
  412. } else {
  413. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  414. ro = 1;
  415. }
  416. if (ro) {
  417. fs_warn(sdp, "jid=%u: Can't replay: read-only FS\n",
  418. jd->jd_jid);
  419. error = -EROFS;
  420. goto fail_gunlock_tr;
  421. }
  422. fs_info(sdp, "jid=%u: Replaying journal...\n", jd->jd_jid);
  423. for (pass = 0; pass < 2; pass++) {
  424. lops_before_scan(jd, &head, pass);
  425. error = foreach_descriptor(jd, head.lh_tail,
  426. head.lh_blkno, pass);
  427. lops_after_scan(jd, error, pass);
  428. if (error)
  429. goto fail_gunlock_tr;
  430. }
  431. error = clean_journal(jd, &head);
  432. if (error)
  433. goto fail_gunlock_tr;
  434. gfs2_glock_dq_uninit(&t_gh);
  435. t = DIV_ROUND_UP(jiffies - t, HZ);
  436. fs_info(sdp, "jid=%u: Journal replayed in %lus\n",
  437. jd->jd_jid, t);
  438. }
  439. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
  440. gfs2_glock_dq_uninit(&ji_gh);
  441. gfs2_lm_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
  442. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
  443. gfs2_glock_dq_uninit(&j_gh);
  444. fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
  445. return 0;
  446. fail_gunlock_tr:
  447. gfs2_glock_dq_uninit(&t_gh);
  448. fail_gunlock_ji:
  449. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
  450. gfs2_glock_dq_uninit(&ji_gh);
  451. fail_gunlock_j:
  452. gfs2_glock_dq_uninit(&j_gh);
  453. }
  454. fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
  455. fail:
  456. gfs2_lm_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
  457. return error;
  458. }
  459. /**
  460. * gfs2_check_journals - Recover any dirty journals
  461. * @sdp: the filesystem
  462. *
  463. */
  464. void gfs2_check_journals(struct gfs2_sbd *sdp)
  465. {
  466. struct gfs2_jdesc *jd;
  467. for (;;) {
  468. jd = gfs2_jdesc_find_dirty(sdp);
  469. if (!jd)
  470. break;
  471. if (jd != sdp->sd_jdesc)
  472. gfs2_recover_journal(jd);
  473. }
  474. }