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