recovery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/gfs2_ondisk.h>
  12. #include <linux/crc32.h>
  13. #include <linux/crc32c.h>
  14. #include <linux/ktime.h>
  15. #include "gfs2.h"
  16. #include "incore.h"
  17. #include "bmap.h"
  18. #include "glock.h"
  19. #include "glops.h"
  20. #include "log.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_first_entry(head, struct gfs2_revoke_replay, rr_list);
  95. list_del(&rr->rr_list);
  96. kfree(rr);
  97. }
  98. }
  99. int __get_log_header(struct gfs2_sbd *sdp, const struct gfs2_log_header *lh,
  100. unsigned int blkno, struct gfs2_log_header_host *head)
  101. {
  102. u32 hash, crc;
  103. if (lh->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
  104. lh->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH) ||
  105. (blkno && be32_to_cpu(lh->lh_blkno) != blkno))
  106. return 1;
  107. hash = crc32(~0, lh, LH_V1_SIZE - 4);
  108. hash = ~crc32_le_shift(hash, 4); /* assume lh_hash is zero */
  109. if (be32_to_cpu(lh->lh_hash) != hash)
  110. return 1;
  111. crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
  112. sdp->sd_sb.sb_bsize - LH_V1_SIZE - 4);
  113. if ((lh->lh_crc != 0 && be32_to_cpu(lh->lh_crc) != crc))
  114. return 1;
  115. head->lh_sequence = be64_to_cpu(lh->lh_sequence);
  116. head->lh_flags = be32_to_cpu(lh->lh_flags);
  117. head->lh_tail = be32_to_cpu(lh->lh_tail);
  118. head->lh_blkno = be32_to_cpu(lh->lh_blkno);
  119. head->lh_local_total = be64_to_cpu(lh->lh_local_total);
  120. head->lh_local_free = be64_to_cpu(lh->lh_local_free);
  121. head->lh_local_dinodes = be64_to_cpu(lh->lh_local_dinodes);
  122. return 0;
  123. }
  124. /**
  125. * get_log_header - read the log header for a given segment
  126. * @jd: the journal
  127. * @blk: the block to look at
  128. * @lh: the log header to return
  129. *
  130. * Read the log header for a given segement in a given journal. Do a few
  131. * sanity checks on it.
  132. *
  133. * Returns: 0 on success,
  134. * 1 if the header was invalid or incomplete,
  135. * errno on error
  136. */
  137. static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
  138. struct gfs2_log_header_host *head)
  139. {
  140. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  141. struct buffer_head *bh;
  142. int error;
  143. error = gfs2_replay_read_block(jd, blk, &bh);
  144. if (error)
  145. return error;
  146. error = __get_log_header(sdp, (const struct gfs2_log_header *)bh->b_data,
  147. blk, head);
  148. brelse(bh);
  149. return error;
  150. }
  151. /**
  152. * foreach_descriptor - go through the active part of the log
  153. * @jd: the journal
  154. * @start: the first log header in the active region
  155. * @end: the last log header (don't process the contents of this entry))
  156. *
  157. * Call a given function once for every log descriptor in the active
  158. * portion of the log.
  159. *
  160. * Returns: errno
  161. */
  162. static int foreach_descriptor(struct gfs2_jdesc *jd, u32 start,
  163. unsigned int end, int pass)
  164. {
  165. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  166. struct buffer_head *bh;
  167. struct gfs2_log_descriptor *ld;
  168. int error = 0;
  169. u32 length;
  170. __be64 *ptr;
  171. unsigned int offset = sizeof(struct gfs2_log_descriptor);
  172. offset += sizeof(__be64) - 1;
  173. offset &= ~(sizeof(__be64) - 1);
  174. while (start != end) {
  175. error = gfs2_replay_read_block(jd, start, &bh);
  176. if (error)
  177. return error;
  178. if (gfs2_meta_check(sdp, bh)) {
  179. brelse(bh);
  180. return -EIO;
  181. }
  182. ld = (struct gfs2_log_descriptor *)bh->b_data;
  183. length = be32_to_cpu(ld->ld_length);
  184. if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
  185. struct gfs2_log_header_host lh;
  186. error = get_log_header(jd, start, &lh);
  187. if (!error) {
  188. gfs2_replay_incr_blk(jd, &start);
  189. brelse(bh);
  190. continue;
  191. }
  192. if (error == 1) {
  193. gfs2_consist_inode(GFS2_I(jd->jd_inode));
  194. error = -EIO;
  195. }
  196. brelse(bh);
  197. return error;
  198. } else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
  199. brelse(bh);
  200. return -EIO;
  201. }
  202. ptr = (__be64 *)(bh->b_data + offset);
  203. error = lops_scan_elements(jd, start, ld, ptr, pass);
  204. if (error) {
  205. brelse(bh);
  206. return error;
  207. }
  208. while (length--)
  209. gfs2_replay_incr_blk(jd, &start);
  210. brelse(bh);
  211. }
  212. return 0;
  213. }
  214. /**
  215. * clean_journal - mark a dirty journal as being clean
  216. * @jd: the journal
  217. * @head: the head journal to start from
  218. *
  219. * Returns: errno
  220. */
  221. static void clean_journal(struct gfs2_jdesc *jd,
  222. struct gfs2_log_header_host *head)
  223. {
  224. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  225. u32 lblock = head->lh_blkno;
  226. gfs2_replay_incr_blk(jd, &lblock);
  227. gfs2_write_log_header(sdp, jd, head->lh_sequence + 1, 0, lblock,
  228. GFS2_LOG_HEAD_UNMOUNT | GFS2_LOG_HEAD_RECOVERY,
  229. REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC);
  230. if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
  231. sdp->sd_log_flush_head = lblock;
  232. gfs2_log_incr_head(sdp);
  233. }
  234. }
  235. static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
  236. unsigned int message)
  237. {
  238. char env_jid[20];
  239. char env_status[20];
  240. char *envp[] = { env_jid, env_status, NULL };
  241. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  242. ls->ls_recover_jid_done = jid;
  243. ls->ls_recover_jid_status = message;
  244. sprintf(env_jid, "JID=%u", jid);
  245. sprintf(env_status, "RECOVERY=%s",
  246. message == LM_RD_SUCCESS ? "Done" : "Failed");
  247. kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
  248. if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
  249. sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
  250. }
  251. /**
  252. * update_statfs_inode - Update the master statfs inode or zero out the local
  253. * statfs inode for a given journal.
  254. * @jd: The journal
  255. * @head: If NULL, @inode is the local statfs inode and we need to zero it out.
  256. * Otherwise, it @head contains the statfs change info that needs to be
  257. * synced to the master statfs inode (pointed to by @inode).
  258. * @inode: statfs inode to update.
  259. */
  260. static int update_statfs_inode(struct gfs2_jdesc *jd,
  261. struct gfs2_log_header_host *head,
  262. struct inode *inode)
  263. {
  264. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  265. struct gfs2_inode *ip;
  266. struct buffer_head *bh;
  267. struct gfs2_statfs_change_host sc;
  268. int error = 0;
  269. BUG_ON(!inode);
  270. ip = GFS2_I(inode);
  271. error = gfs2_meta_inode_buffer(ip, &bh);
  272. if (error)
  273. goto out;
  274. spin_lock(&sdp->sd_statfs_spin);
  275. if (head) { /* Update the master statfs inode */
  276. gfs2_statfs_change_in(&sc, bh->b_data + sizeof(struct gfs2_dinode));
  277. sc.sc_total += head->lh_local_total;
  278. sc.sc_free += head->lh_local_free;
  279. sc.sc_dinodes += head->lh_local_dinodes;
  280. gfs2_statfs_change_out(&sc, bh->b_data + sizeof(struct gfs2_dinode));
  281. fs_info(sdp, "jid=%u: Updated master statfs Total:%lld, "
  282. "Free:%lld, Dinodes:%lld after change "
  283. "[%+lld,%+lld,%+lld]\n", jd->jd_jid, sc.sc_total,
  284. sc.sc_free, sc.sc_dinodes, head->lh_local_total,
  285. head->lh_local_free, head->lh_local_dinodes);
  286. } else { /* Zero out the local statfs inode */
  287. memset(bh->b_data + sizeof(struct gfs2_dinode), 0,
  288. sizeof(struct gfs2_statfs_change));
  289. /* If it's our own journal, reset any in-memory changes too */
  290. if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
  291. memset(&sdp->sd_statfs_local, 0,
  292. sizeof(struct gfs2_statfs_change_host));
  293. }
  294. }
  295. spin_unlock(&sdp->sd_statfs_spin);
  296. mark_buffer_dirty(bh);
  297. brelse(bh);
  298. gfs2_inode_metasync(ip->i_gl);
  299. out:
  300. return error;
  301. }
  302. /**
  303. * recover_local_statfs - Update the master and local statfs changes for this
  304. * journal.
  305. *
  306. * Previously, statfs updates would be read in from the local statfs inode and
  307. * synced to the master statfs inode during recovery.
  308. *
  309. * We now use the statfs updates in the journal head to update the master statfs
  310. * inode instead of reading in from the local statfs inode. To preserve backward
  311. * compatibility with kernels that can't do this, we still need to keep the
  312. * local statfs inode up to date by writing changes to it. At some point in the
  313. * future, we can do away with the local statfs inodes altogether and keep the
  314. * statfs changes solely in the journal.
  315. *
  316. * @jd: the journal
  317. * @head: the journal head
  318. *
  319. * Returns: errno
  320. */
  321. static void recover_local_statfs(struct gfs2_jdesc *jd,
  322. struct gfs2_log_header_host *head)
  323. {
  324. int error;
  325. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  326. if (!head->lh_local_total && !head->lh_local_free
  327. && !head->lh_local_dinodes) /* No change */
  328. goto zero_local;
  329. /* First update the master statfs inode with the changes we
  330. * found in the journal. */
  331. error = update_statfs_inode(jd, head, sdp->sd_statfs_inode);
  332. if (error)
  333. goto out;
  334. zero_local:
  335. /* Zero out the local statfs inode so any changes in there
  336. * are not re-recovered. */
  337. error = update_statfs_inode(jd, NULL,
  338. find_local_statfs_inode(sdp, jd->jd_jid));
  339. out:
  340. return;
  341. }
  342. void gfs2_recover_func(struct work_struct *work)
  343. {
  344. struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
  345. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  346. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  347. struct gfs2_log_header_host head;
  348. struct gfs2_holder j_gh, ji_gh, thaw_gh;
  349. ktime_t t_start, t_jlck, t_jhd, t_tlck, t_rep;
  350. int ro = 0;
  351. unsigned int pass;
  352. int error = 0;
  353. int jlocked = 0;
  354. if (gfs2_withdrawn(sdp)) {
  355. fs_err(sdp, "jid=%u: Recovery not attempted due to withdraw.\n",
  356. jd->jd_jid);
  357. goto fail;
  358. }
  359. t_start = ktime_get();
  360. if (sdp->sd_args.ar_spectator)
  361. goto fail;
  362. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
  363. fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
  364. jd->jd_jid);
  365. jlocked = 1;
  366. /* Acquire the journal lock so we can do recovery */
  367. error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
  368. LM_ST_EXCLUSIVE,
  369. LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
  370. &j_gh);
  371. switch (error) {
  372. case 0:
  373. break;
  374. case GLR_TRYFAILED:
  375. fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
  376. error = 0;
  377. default:
  378. goto fail;
  379. }
  380. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
  381. LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
  382. if (error)
  383. goto fail_gunlock_j;
  384. } else {
  385. fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
  386. }
  387. t_jlck = ktime_get();
  388. fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
  389. error = gfs2_jdesc_check(jd);
  390. if (error)
  391. goto fail_gunlock_ji;
  392. error = gfs2_find_jhead(jd, &head, true);
  393. if (error)
  394. goto fail_gunlock_ji;
  395. t_jhd = ktime_get();
  396. fs_info(sdp, "jid=%u: Journal head lookup took %lldms\n", jd->jd_jid,
  397. ktime_ms_delta(t_jhd, t_jlck));
  398. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  399. fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
  400. jd->jd_jid);
  401. /* Acquire a shared hold on the freeze lock */
  402. error = gfs2_freeze_lock(sdp, &thaw_gh, LM_FLAG_PRIORITY);
  403. if (error)
  404. goto fail_gunlock_ji;
  405. if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) {
  406. ro = 1;
  407. } else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
  408. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  409. ro = 1;
  410. } else {
  411. if (sb_rdonly(sdp->sd_vfs)) {
  412. /* check if device itself is read-only */
  413. ro = bdev_read_only(sdp->sd_vfs->s_bdev);
  414. if (!ro) {
  415. fs_info(sdp, "recovery required on "
  416. "read-only filesystem.\n");
  417. fs_info(sdp, "write access will be "
  418. "enabled during recovery.\n");
  419. }
  420. }
  421. }
  422. if (ro) {
  423. fs_warn(sdp, "jid=%u: Can't replay: read-only block "
  424. "device\n", jd->jd_jid);
  425. error = -EROFS;
  426. goto fail_gunlock_thaw;
  427. }
  428. t_tlck = ktime_get();
  429. fs_info(sdp, "jid=%u: Replaying journal...0x%x to 0x%x\n",
  430. jd->jd_jid, head.lh_tail, head.lh_blkno);
  431. /* We take the sd_log_flush_lock here primarily to prevent log
  432. * flushes and simultaneous journal replays from stomping on
  433. * each other wrt sd_log_bio. */
  434. down_read(&sdp->sd_log_flush_lock);
  435. for (pass = 0; pass < 2; pass++) {
  436. lops_before_scan(jd, &head, pass);
  437. error = foreach_descriptor(jd, head.lh_tail,
  438. head.lh_blkno, pass);
  439. lops_after_scan(jd, error, pass);
  440. if (error) {
  441. up_read(&sdp->sd_log_flush_lock);
  442. goto fail_gunlock_thaw;
  443. }
  444. }
  445. recover_local_statfs(jd, &head);
  446. clean_journal(jd, &head);
  447. up_read(&sdp->sd_log_flush_lock);
  448. gfs2_freeze_unlock(&thaw_gh);
  449. t_rep = ktime_get();
  450. fs_info(sdp, "jid=%u: Journal replayed in %lldms [jlck:%lldms, "
  451. "jhead:%lldms, tlck:%lldms, replay:%lldms]\n",
  452. jd->jd_jid, ktime_ms_delta(t_rep, t_start),
  453. ktime_ms_delta(t_jlck, t_start),
  454. ktime_ms_delta(t_jhd, t_jlck),
  455. ktime_ms_delta(t_tlck, t_jhd),
  456. ktime_ms_delta(t_rep, t_tlck));
  457. }
  458. gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
  459. if (jlocked) {
  460. gfs2_glock_dq_uninit(&ji_gh);
  461. gfs2_glock_dq_uninit(&j_gh);
  462. }
  463. fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
  464. goto done;
  465. fail_gunlock_thaw:
  466. gfs2_freeze_unlock(&thaw_gh);
  467. fail_gunlock_ji:
  468. if (jlocked) {
  469. gfs2_glock_dq_uninit(&ji_gh);
  470. fail_gunlock_j:
  471. gfs2_glock_dq_uninit(&j_gh);
  472. }
  473. fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
  474. fail:
  475. jd->jd_recover_error = error;
  476. gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
  477. done:
  478. clear_bit(JDF_RECOVERY, &jd->jd_flags);
  479. smp_mb__after_atomic();
  480. wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
  481. }
  482. int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait)
  483. {
  484. int rv;
  485. if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
  486. return -EBUSY;
  487. /* we have JDF_RECOVERY, queue should always succeed */
  488. rv = queue_work(gfs_recovery_wq, &jd->jd_work);
  489. BUG_ON(!rv);
  490. if (wait)
  491. wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
  492. TASK_UNINTERRUPTIBLE);
  493. return wait ? jd->jd_recover_error : 0;
  494. }