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. u64 dblock;
  34. u32 extlen;
  35. int error;
  36. extlen = 32;
  37. error = gfs2_get_extent(&ip->i_inode, blk, &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 = NULL, *iter;
  51. list_for_each_entry(iter, head, rr_list) {
  52. if (iter->rr_blkno == blkno) {
  53. rr = iter;
  54. break;
  55. }
  56. }
  57. if (rr) {
  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_jdesc *jd, u64 blkno, unsigned int where)
  70. {
  71. struct gfs2_revoke_replay *rr = NULL, *iter;
  72. int wrap, a, b, revoke;
  73. list_for_each_entry(iter, &jd->jd_revoke_list, rr_list) {
  74. if (iter->rr_blkno == blkno) {
  75. rr = iter;
  76. break;
  77. }
  78. }
  79. if (!rr)
  80. return 0;
  81. wrap = (rr->rr_where < jd->jd_replay_tail);
  82. a = (jd->jd_replay_tail < where);
  83. b = (where < rr->rr_where);
  84. revoke = (wrap) ? (a || b) : (a && b);
  85. return revoke;
  86. }
  87. void gfs2_revoke_clean(struct gfs2_jdesc *jd)
  88. {
  89. struct list_head *head = &jd->jd_revoke_list;
  90. struct gfs2_revoke_replay *rr;
  91. while (!list_empty(head)) {
  92. rr = list_first_entry(head, struct gfs2_revoke_replay, rr_list);
  93. list_del(&rr->rr_list);
  94. kfree(rr);
  95. }
  96. }
  97. int __get_log_header(struct gfs2_sbd *sdp, const struct gfs2_log_header *lh,
  98. unsigned int blkno, struct gfs2_log_header_host *head)
  99. {
  100. u32 hash, crc;
  101. if (lh->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
  102. lh->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH) ||
  103. (blkno && be32_to_cpu(lh->lh_blkno) != blkno))
  104. return 1;
  105. hash = crc32(~0, lh, LH_V1_SIZE - 4);
  106. hash = ~crc32_le_shift(hash, 4); /* assume lh_hash is zero */
  107. if (be32_to_cpu(lh->lh_hash) != hash)
  108. return 1;
  109. crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
  110. sdp->sd_sb.sb_bsize - LH_V1_SIZE - 4);
  111. if ((lh->lh_crc != 0 && be32_to_cpu(lh->lh_crc) != crc))
  112. return 1;
  113. head->lh_sequence = be64_to_cpu(lh->lh_sequence);
  114. head->lh_flags = be32_to_cpu(lh->lh_flags);
  115. head->lh_tail = be32_to_cpu(lh->lh_tail);
  116. head->lh_blkno = be32_to_cpu(lh->lh_blkno);
  117. head->lh_local_total = be64_to_cpu(lh->lh_local_total);
  118. head->lh_local_free = be64_to_cpu(lh->lh_local_free);
  119. head->lh_local_dinodes = be64_to_cpu(lh->lh_local_dinodes);
  120. return 0;
  121. }
  122. /**
  123. * get_log_header - read the log header for a given segment
  124. * @jd: the journal
  125. * @blk: the block to look at
  126. * @head: the log header to return
  127. *
  128. * Read the log header for a given segement in a given journal. Do a few
  129. * sanity checks on it.
  130. *
  131. * Returns: 0 on success,
  132. * 1 if the header was invalid or incomplete,
  133. * errno on error
  134. */
  135. static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
  136. struct gfs2_log_header_host *head)
  137. {
  138. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  139. struct buffer_head *bh;
  140. int error;
  141. error = gfs2_replay_read_block(jd, blk, &bh);
  142. if (error)
  143. return error;
  144. error = __get_log_header(sdp, (const struct gfs2_log_header *)bh->b_data,
  145. blk, head);
  146. brelse(bh);
  147. return error;
  148. }
  149. /**
  150. * foreach_descriptor - go through the active part of the log
  151. * @jd: the journal
  152. * @start: the first log header in the active region
  153. * @end: the last log header (don't process the contents of this entry))
  154. * @pass: iteration number (foreach_descriptor() is called in a for() loop)
  155. *
  156. * Call a given function once for every log descriptor in the active
  157. * portion of the log.
  158. *
  159. * Returns: errno
  160. */
  161. static int foreach_descriptor(struct gfs2_jdesc *jd, u32 start,
  162. unsigned int end, int pass)
  163. {
  164. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  165. struct buffer_head *bh;
  166. struct gfs2_log_descriptor *ld;
  167. int error = 0;
  168. u32 length;
  169. __be64 *ptr;
  170. unsigned int offset = sizeof(struct gfs2_log_descriptor);
  171. offset += sizeof(__be64) - 1;
  172. offset &= ~(sizeof(__be64) - 1);
  173. while (start != end) {
  174. error = gfs2_replay_read_block(jd, start, &bh);
  175. if (error)
  176. return error;
  177. if (gfs2_meta_check(sdp, bh)) {
  178. brelse(bh);
  179. return -EIO;
  180. }
  181. ld = (struct gfs2_log_descriptor *)bh->b_data;
  182. length = be32_to_cpu(ld->ld_length);
  183. if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
  184. struct gfs2_log_header_host lh;
  185. error = get_log_header(jd, start, &lh);
  186. if (!error) {
  187. gfs2_replay_incr_blk(jd, &start);
  188. brelse(bh);
  189. continue;
  190. }
  191. if (error == 1) {
  192. gfs2_consist_inode(GFS2_I(jd->jd_inode));
  193. error = -EIO;
  194. }
  195. brelse(bh);
  196. return error;
  197. } else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
  198. brelse(bh);
  199. return -EIO;
  200. }
  201. ptr = (__be64 *)(bh->b_data + offset);
  202. error = lops_scan_elements(jd, start, ld, ptr, pass);
  203. if (error) {
  204. brelse(bh);
  205. return error;
  206. }
  207. while (length--)
  208. gfs2_replay_incr_blk(jd, &start);
  209. brelse(bh);
  210. }
  211. return 0;
  212. }
  213. /**
  214. * clean_journal - mark a dirty journal as being clean
  215. * @jd: the journal
  216. * @head: the head journal to start from
  217. *
  218. * Returns: errno
  219. */
  220. static void clean_journal(struct gfs2_jdesc *jd,
  221. struct gfs2_log_header_host *head)
  222. {
  223. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  224. u32 lblock = head->lh_blkno;
  225. gfs2_replay_incr_blk(jd, &lblock);
  226. gfs2_write_log_header(sdp, jd, head->lh_sequence + 1, 0, lblock,
  227. GFS2_LOG_HEAD_UNMOUNT | GFS2_LOG_HEAD_RECOVERY,
  228. REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC);
  229. if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
  230. sdp->sd_log_flush_head = lblock;
  231. gfs2_log_incr_head(sdp);
  232. }
  233. }
  234. static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
  235. unsigned int message)
  236. {
  237. char env_jid[20];
  238. char env_status[20];
  239. char *envp[] = { env_jid, env_status, NULL };
  240. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  241. ls->ls_recover_jid_done = jid;
  242. ls->ls_recover_jid_status = message;
  243. sprintf(env_jid, "JID=%u", jid);
  244. sprintf(env_status, "RECOVERY=%s",
  245. message == LM_RD_SUCCESS ? "Done" : "Failed");
  246. kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
  247. if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
  248. sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
  249. }
  250. /**
  251. * update_statfs_inode - Update the master statfs inode or zero out the local
  252. * statfs inode for a given journal.
  253. * @jd: The journal
  254. * @head: If NULL, @inode is the local statfs inode and we need to zero it out.
  255. * Otherwise, it @head contains the statfs change info that needs to be
  256. * synced to the master statfs inode (pointed to by @inode).
  257. * @inode: statfs inode to update.
  258. */
  259. static int update_statfs_inode(struct gfs2_jdesc *jd,
  260. struct gfs2_log_header_host *head,
  261. struct inode *inode)
  262. {
  263. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  264. struct gfs2_inode *ip;
  265. struct buffer_head *bh;
  266. struct gfs2_statfs_change_host sc;
  267. int error = 0;
  268. BUG_ON(!inode);
  269. ip = GFS2_I(inode);
  270. error = gfs2_meta_inode_buffer(ip, &bh);
  271. if (error)
  272. goto out;
  273. spin_lock(&sdp->sd_statfs_spin);
  274. if (head) { /* Update the master statfs inode */
  275. gfs2_statfs_change_in(&sc, bh->b_data + sizeof(struct gfs2_dinode));
  276. sc.sc_total += head->lh_local_total;
  277. sc.sc_free += head->lh_local_free;
  278. sc.sc_dinodes += head->lh_local_dinodes;
  279. gfs2_statfs_change_out(&sc, bh->b_data + sizeof(struct gfs2_dinode));
  280. fs_info(sdp, "jid=%u: Updated master statfs Total:%lld, "
  281. "Free:%lld, Dinodes:%lld after change "
  282. "[%+lld,%+lld,%+lld]\n", jd->jd_jid, sc.sc_total,
  283. sc.sc_free, sc.sc_dinodes, head->lh_local_total,
  284. head->lh_local_free, head->lh_local_dinodes);
  285. } else { /* Zero out the local statfs inode */
  286. memset(bh->b_data + sizeof(struct gfs2_dinode), 0,
  287. sizeof(struct gfs2_statfs_change));
  288. /* If it's our own journal, reset any in-memory changes too */
  289. if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
  290. memset(&sdp->sd_statfs_local, 0,
  291. sizeof(struct gfs2_statfs_change_host));
  292. }
  293. }
  294. spin_unlock(&sdp->sd_statfs_spin);
  295. mark_buffer_dirty(bh);
  296. brelse(bh);
  297. gfs2_inode_metasync(ip->i_gl);
  298. out:
  299. return error;
  300. }
  301. /**
  302. * recover_local_statfs - Update the master and local statfs changes for this
  303. * journal.
  304. *
  305. * Previously, statfs updates would be read in from the local statfs inode and
  306. * synced to the master statfs inode during recovery.
  307. *
  308. * We now use the statfs updates in the journal head to update the master statfs
  309. * inode instead of reading in from the local statfs inode. To preserve backward
  310. * compatibility with kernels that can't do this, we still need to keep the
  311. * local statfs inode up to date by writing changes to it. At some point in the
  312. * future, we can do away with the local statfs inodes altogether and keep the
  313. * statfs changes solely in the journal.
  314. *
  315. * @jd: the journal
  316. * @head: the journal head
  317. *
  318. * Returns: errno
  319. */
  320. static void recover_local_statfs(struct gfs2_jdesc *jd,
  321. struct gfs2_log_header_host *head)
  322. {
  323. int error;
  324. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  325. if (!head->lh_local_total && !head->lh_local_free
  326. && !head->lh_local_dinodes) /* No change */
  327. goto zero_local;
  328. /* First update the master statfs inode with the changes we
  329. * found in the journal. */
  330. error = update_statfs_inode(jd, head, sdp->sd_statfs_inode);
  331. if (error)
  332. goto out;
  333. zero_local:
  334. /* Zero out the local statfs inode so any changes in there
  335. * are not re-recovered. */
  336. error = update_statfs_inode(jd, NULL,
  337. find_local_statfs_inode(sdp, jd->jd_jid));
  338. out:
  339. return;
  340. }
  341. void gfs2_recover_func(struct work_struct *work)
  342. {
  343. struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
  344. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  345. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  346. struct gfs2_log_header_host head;
  347. struct gfs2_holder j_gh, ji_gh, thaw_gh;
  348. ktime_t t_start, t_jlck, t_jhd, t_tlck, t_rep;
  349. int ro = 0;
  350. unsigned int pass;
  351. int error = 0;
  352. int jlocked = 0;
  353. if (gfs2_withdrawn(sdp)) {
  354. fs_err(sdp, "jid=%u: Recovery not attempted due to withdraw.\n",
  355. jd->jd_jid);
  356. goto fail;
  357. }
  358. t_start = ktime_get();
  359. if (sdp->sd_args.ar_spectator)
  360. goto fail;
  361. if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
  362. fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
  363. jd->jd_jid);
  364. jlocked = 1;
  365. /* Acquire the journal lock so we can do recovery */
  366. error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
  367. LM_ST_EXCLUSIVE,
  368. LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
  369. &j_gh);
  370. switch (error) {
  371. case 0:
  372. break;
  373. case GLR_TRYFAILED:
  374. fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
  375. error = 0;
  376. goto fail;
  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 jd_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. }