recovery.c 13 KB

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