recovery.c 14 KB

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