recovery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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/slow-work.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_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
  361. unsigned int message)
  362. {
  363. char env_jid[20];
  364. char env_status[20];
  365. char *envp[] = { env_jid, env_status, NULL };
  366. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  367. ls->ls_recover_jid_done = jid;
  368. ls->ls_recover_jid_status = message;
  369. sprintf(env_jid, "JID=%d", jid);
  370. sprintf(env_status, "RECOVERY=%s",
  371. message == LM_RD_SUCCESS ? "Done" : "Failed");
  372. kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
  373. }
  374. static int gfs2_recover_get_ref(struct slow_work *work)
  375. {
  376. struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
  377. if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
  378. return -EBUSY;
  379. return 0;
  380. }
  381. static void gfs2_recover_put_ref(struct slow_work *work)
  382. {
  383. struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
  384. clear_bit(JDF_RECOVERY, &jd->jd_flags);
  385. smp_mb__after_clear_bit();
  386. wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
  387. }
  388. static void gfs2_recover_work(struct slow_work *work)
  389. {
  390. struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
  391. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  392. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  393. struct gfs2_log_header_host head;
  394. struct gfs2_holder j_gh, ji_gh, t_gh;
  395. unsigned long t;
  396. int ro = 0;
  397. unsigned int pass;
  398. int error;
  399. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
  400. fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
  401. jd->jd_jid);
  402. /* Acquire the journal lock so we can do recovery */
  403. error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
  404. LM_ST_EXCLUSIVE,
  405. LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
  406. &j_gh);
  407. switch (error) {
  408. case 0:
  409. break;
  410. case GLR_TRYFAILED:
  411. fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
  412. error = 0;
  413. default:
  414. goto fail;
  415. };
  416. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
  417. LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
  418. if (error)
  419. goto fail_gunlock_j;
  420. } else {
  421. fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
  422. }
  423. fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
  424. error = gfs2_jdesc_check(jd);
  425. if (error)
  426. goto fail_gunlock_ji;
  427. error = gfs2_find_jhead(jd, &head);
  428. if (error)
  429. goto fail_gunlock_ji;
  430. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  431. fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
  432. jd->jd_jid);
  433. t = jiffies;
  434. /* Acquire a shared hold on the transaction lock */
  435. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
  436. LM_FLAG_NOEXP | LM_FLAG_PRIORITY |
  437. GL_NOCACHE, &t_gh);
  438. if (error)
  439. goto fail_gunlock_ji;
  440. if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
  441. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  442. ro = 1;
  443. } else {
  444. if (sdp->sd_vfs->s_flags & MS_RDONLY) {
  445. /* check if device itself is read-only */
  446. ro = bdev_read_only(sdp->sd_vfs->s_bdev);
  447. if (!ro) {
  448. fs_info(sdp, "recovery required on "
  449. "read-only filesystem.\n");
  450. fs_info(sdp, "write access will be "
  451. "enabled during recovery.\n");
  452. }
  453. }
  454. }
  455. if (ro) {
  456. fs_warn(sdp, "jid=%u: Can't replay: read-only block "
  457. "device\n", jd->jd_jid);
  458. error = -EROFS;
  459. goto fail_gunlock_tr;
  460. }
  461. fs_info(sdp, "jid=%u: Replaying journal...\n", jd->jd_jid);
  462. for (pass = 0; pass < 2; pass++) {
  463. lops_before_scan(jd, &head, pass);
  464. error = foreach_descriptor(jd, head.lh_tail,
  465. head.lh_blkno, pass);
  466. lops_after_scan(jd, error, pass);
  467. if (error)
  468. goto fail_gunlock_tr;
  469. }
  470. error = clean_journal(jd, &head);
  471. if (error)
  472. goto fail_gunlock_tr;
  473. gfs2_glock_dq_uninit(&t_gh);
  474. t = DIV_ROUND_UP(jiffies - t, HZ);
  475. fs_info(sdp, "jid=%u: Journal replayed in %lus\n",
  476. jd->jd_jid, t);
  477. }
  478. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
  479. gfs2_glock_dq_uninit(&ji_gh);
  480. gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
  481. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
  482. gfs2_glock_dq_uninit(&j_gh);
  483. fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
  484. return;
  485. fail_gunlock_tr:
  486. gfs2_glock_dq_uninit(&t_gh);
  487. fail_gunlock_ji:
  488. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
  489. gfs2_glock_dq_uninit(&ji_gh);
  490. fail_gunlock_j:
  491. gfs2_glock_dq_uninit(&j_gh);
  492. }
  493. fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
  494. fail:
  495. gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
  496. }
  497. struct slow_work_ops gfs2_recover_ops = {
  498. .owner = THIS_MODULE,
  499. .get_ref = gfs2_recover_get_ref,
  500. .put_ref = gfs2_recover_put_ref,
  501. .execute = gfs2_recover_work,
  502. };
  503. static int gfs2_recovery_wait(void *word)
  504. {
  505. schedule();
  506. return 0;
  507. }
  508. int gfs2_recover_journal(struct gfs2_jdesc *jd)
  509. {
  510. int rv;
  511. rv = slow_work_enqueue(&jd->jd_work);
  512. if (rv)
  513. return rv;
  514. wait_on_bit(&jd->jd_flags, JDF_RECOVERY, gfs2_recovery_wait, TASK_UNINTERRUPTIBLE);
  515. return 0;
  516. }