recovery.c 12 KB

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