recovery.c 14 KB

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