recovery.c 12 KB

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