recovery.c 14 KB

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