recovery.c 12 KB

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