recovery.c 12 KB

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