util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/spinlock.h>
  8. #include <linux/completion.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/kthread.h>
  11. #include <linux/crc32.h>
  12. #include <linux/gfs2_ondisk.h>
  13. #include <linux/delay.h>
  14. #include <linux/uaccess.h>
  15. #include "gfs2.h"
  16. #include "incore.h"
  17. #include "glock.h"
  18. #include "glops.h"
  19. #include "log.h"
  20. #include "lops.h"
  21. #include "recovery.h"
  22. #include "rgrp.h"
  23. #include "super.h"
  24. #include "util.h"
  25. struct kmem_cache *gfs2_glock_cachep __read_mostly;
  26. struct kmem_cache *gfs2_glock_aspace_cachep __read_mostly;
  27. struct kmem_cache *gfs2_inode_cachep __read_mostly;
  28. struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
  29. struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
  30. struct kmem_cache *gfs2_quotad_cachep __read_mostly;
  31. struct kmem_cache *gfs2_qadata_cachep __read_mostly;
  32. struct kmem_cache *gfs2_trans_cachep __read_mostly;
  33. mempool_t *gfs2_page_pool __read_mostly;
  34. void gfs2_assert_i(struct gfs2_sbd *sdp)
  35. {
  36. fs_emerg(sdp, "fatal assertion failed\n");
  37. }
  38. /**
  39. * check_journal_clean - Make sure a journal is clean for a spectator mount
  40. * @sdp: The GFS2 superblock
  41. * @jd: The journal descriptor
  42. * @verbose: Show more prints in the log
  43. *
  44. * Returns: 0 if the journal is clean or locked, else an error
  45. */
  46. int check_journal_clean(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
  47. bool verbose)
  48. {
  49. int error;
  50. struct gfs2_holder j_gh;
  51. struct gfs2_log_header_host head;
  52. struct gfs2_inode *ip;
  53. ip = GFS2_I(jd->jd_inode);
  54. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_NOEXP |
  55. GL_EXACT | GL_NOCACHE, &j_gh);
  56. if (error) {
  57. if (verbose)
  58. fs_err(sdp, "Error %d locking journal for spectator "
  59. "mount.\n", error);
  60. return -EPERM;
  61. }
  62. error = gfs2_jdesc_check(jd);
  63. if (error) {
  64. if (verbose)
  65. fs_err(sdp, "Error checking journal for spectator "
  66. "mount.\n");
  67. goto out_unlock;
  68. }
  69. error = gfs2_find_jhead(jd, &head, false);
  70. if (error) {
  71. if (verbose)
  72. fs_err(sdp, "Error parsing journal for spectator "
  73. "mount.\n");
  74. goto out_unlock;
  75. }
  76. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  77. error = -EPERM;
  78. if (verbose)
  79. fs_err(sdp, "jid=%u: Journal is dirty, so the first "
  80. "mounter must not be a spectator.\n",
  81. jd->jd_jid);
  82. }
  83. out_unlock:
  84. gfs2_glock_dq_uninit(&j_gh);
  85. return error;
  86. }
  87. /**
  88. * gfs2_freeze_lock_shared - hold the freeze glock
  89. * @sdp: the superblock
  90. */
  91. int gfs2_freeze_lock_shared(struct gfs2_sbd *sdp)
  92. {
  93. int flags = LM_FLAG_NOEXP | GL_EXACT;
  94. int error;
  95. error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, flags,
  96. &sdp->sd_freeze_gh);
  97. if (error && error != GLR_TRYFAILED)
  98. fs_err(sdp, "can't lock the freeze glock: %d\n", error);
  99. return error;
  100. }
  101. void gfs2_freeze_unlock(struct gfs2_sbd *sdp)
  102. {
  103. if (gfs2_holder_initialized(&sdp->sd_freeze_gh))
  104. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  105. }
  106. static void signal_our_withdraw(struct gfs2_sbd *sdp)
  107. {
  108. struct gfs2_glock *live_gl = sdp->sd_live_gh.gh_gl;
  109. struct inode *inode;
  110. struct gfs2_inode *ip;
  111. struct gfs2_glock *i_gl;
  112. u64 no_formal_ino;
  113. int ret = 0;
  114. int tries;
  115. if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) || !sdp->sd_jdesc)
  116. return;
  117. gfs2_ail_drain(sdp); /* frees all transactions */
  118. inode = sdp->sd_jdesc->jd_inode;
  119. ip = GFS2_I(inode);
  120. i_gl = ip->i_gl;
  121. no_formal_ino = ip->i_no_formal_ino;
  122. /* Prevent any glock dq until withdraw recovery is complete */
  123. set_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
  124. /*
  125. * Don't tell dlm we're bailing until we have no more buffers in the
  126. * wind. If journal had an IO error, the log code should just purge
  127. * the outstanding buffers rather than submitting new IO. Making the
  128. * file system read-only will flush the journal, etc.
  129. *
  130. * During a normal unmount, gfs2_make_fs_ro calls gfs2_log_shutdown
  131. * which clears SDF_JOURNAL_LIVE. In a withdraw, we must not write
  132. * any UNMOUNT log header, so we can't call gfs2_log_shutdown, and
  133. * therefore we need to clear SDF_JOURNAL_LIVE manually.
  134. */
  135. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  136. if (!sb_rdonly(sdp->sd_vfs)) {
  137. bool locked = mutex_trylock(&sdp->sd_freeze_mutex);
  138. wake_up(&sdp->sd_logd_waitq);
  139. wake_up(&sdp->sd_quota_wait);
  140. wait_event_timeout(sdp->sd_log_waitq,
  141. gfs2_log_is_empty(sdp),
  142. HZ * 5);
  143. sdp->sd_vfs->s_flags |= SB_RDONLY;
  144. if (locked)
  145. mutex_unlock(&sdp->sd_freeze_mutex);
  146. /*
  147. * Dequeue any pending non-system glock holders that can no
  148. * longer be granted because the file system is withdrawn.
  149. */
  150. gfs2_gl_dq_holders(sdp);
  151. }
  152. if (sdp->sd_lockstruct.ls_ops->lm_lock == NULL) { /* lock_nolock */
  153. if (!ret)
  154. ret = -EIO;
  155. clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
  156. goto skip_recovery;
  157. }
  158. /*
  159. * Drop the glock for our journal so another node can recover it.
  160. */
  161. if (gfs2_holder_initialized(&sdp->sd_journal_gh)) {
  162. gfs2_glock_dq_wait(&sdp->sd_journal_gh);
  163. gfs2_holder_uninit(&sdp->sd_journal_gh);
  164. }
  165. sdp->sd_jinode_gh.gh_flags |= GL_NOCACHE;
  166. gfs2_glock_dq(&sdp->sd_jinode_gh);
  167. gfs2_thaw_freeze_initiator(sdp->sd_vfs);
  168. wait_on_bit(&i_gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
  169. /*
  170. * holder_uninit to force glock_put, to force dlm to let go
  171. */
  172. gfs2_holder_uninit(&sdp->sd_jinode_gh);
  173. /*
  174. * Note: We need to be careful here:
  175. * Our iput of jd_inode will evict it. The evict will dequeue its
  176. * glock, but the glock dq will wait for the withdraw unless we have
  177. * exception code in glock_dq.
  178. */
  179. iput(inode);
  180. sdp->sd_jdesc->jd_inode = NULL;
  181. /*
  182. * Wait until the journal inode's glock is freed. This allows try locks
  183. * on other nodes to be successful, otherwise we remain the owner of
  184. * the glock as far as dlm is concerned.
  185. */
  186. if (i_gl->gl_ops->go_unlocked) {
  187. set_bit(GLF_UNLOCKED, &i_gl->gl_flags);
  188. wait_on_bit(&i_gl->gl_flags, GLF_UNLOCKED, TASK_UNINTERRUPTIBLE);
  189. }
  190. /*
  191. * Dequeue the "live" glock, but keep a reference so it's never freed.
  192. */
  193. gfs2_glock_hold(live_gl);
  194. gfs2_glock_dq_wait(&sdp->sd_live_gh);
  195. /*
  196. * We enqueue the "live" glock in EX so that all other nodes
  197. * get a demote request and act on it. We don't really want the
  198. * lock in EX, so we send a "try" lock with 1CB to produce a callback.
  199. */
  200. fs_warn(sdp, "Requesting recovery of jid %d.\n",
  201. sdp->sd_lockstruct.ls_jid);
  202. gfs2_holder_reinit(LM_ST_EXCLUSIVE,
  203. LM_FLAG_TRY_1CB | LM_FLAG_NOEXP | GL_NOPID,
  204. &sdp->sd_live_gh);
  205. msleep(GL_GLOCK_MAX_HOLD);
  206. /*
  207. * This will likely fail in a cluster, but succeed standalone:
  208. */
  209. ret = gfs2_glock_nq(&sdp->sd_live_gh);
  210. gfs2_glock_put(live_gl); /* drop extra reference we acquired */
  211. clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
  212. /*
  213. * If we actually got the "live" lock in EX mode, there are no other
  214. * nodes available to replay our journal.
  215. */
  216. if (ret == 0) {
  217. fs_warn(sdp, "No other mounters found.\n");
  218. /*
  219. * We are about to release the lockspace. By keeping live_gl
  220. * locked here, we ensure that the next mounter coming along
  221. * will be a "first" mounter which will perform recovery.
  222. */
  223. goto skip_recovery;
  224. }
  225. /*
  226. * At this point our journal is evicted, so we need to get a new inode
  227. * for it. Once done, we need to call gfs2_find_jhead which
  228. * calls gfs2_map_journal_extents to map it for us again.
  229. *
  230. * Note that we don't really want it to look up a FREE block. The
  231. * GFS2_BLKST_FREE simply overrides a block check in gfs2_inode_lookup
  232. * which would otherwise fail because it requires grabbing an rgrp
  233. * glock, which would fail with -EIO because we're withdrawing.
  234. */
  235. inode = gfs2_inode_lookup(sdp->sd_vfs, DT_UNKNOWN,
  236. sdp->sd_jdesc->jd_no_addr, no_formal_ino,
  237. GFS2_BLKST_FREE);
  238. if (IS_ERR(inode)) {
  239. fs_warn(sdp, "Reprocessing of jid %d failed with %ld.\n",
  240. sdp->sd_lockstruct.ls_jid, PTR_ERR(inode));
  241. goto skip_recovery;
  242. }
  243. sdp->sd_jdesc->jd_inode = inode;
  244. d_mark_dontcache(inode);
  245. /*
  246. * Now wait until recovery is complete.
  247. */
  248. for (tries = 0; tries < 10; tries++) {
  249. ret = check_journal_clean(sdp, sdp->sd_jdesc, false);
  250. if (!ret)
  251. break;
  252. msleep(HZ);
  253. fs_warn(sdp, "Waiting for journal recovery jid %d.\n",
  254. sdp->sd_lockstruct.ls_jid);
  255. }
  256. skip_recovery:
  257. if (!ret)
  258. fs_warn(sdp, "Journal recovery complete for jid %d.\n",
  259. sdp->sd_lockstruct.ls_jid);
  260. else
  261. fs_warn(sdp, "Journal recovery skipped for jid %d until next "
  262. "mount.\n", sdp->sd_lockstruct.ls_jid);
  263. fs_warn(sdp, "Glock dequeues delayed: %lu\n", sdp->sd_glock_dqs_held);
  264. sdp->sd_glock_dqs_held = 0;
  265. wake_up_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY);
  266. }
  267. void gfs2_lm(struct gfs2_sbd *sdp, const char *fmt, ...)
  268. {
  269. struct va_format vaf;
  270. va_list args;
  271. if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
  272. test_bit(SDF_WITHDRAWN, &sdp->sd_flags))
  273. return;
  274. va_start(args, fmt);
  275. vaf.fmt = fmt;
  276. vaf.va = &args;
  277. fs_err(sdp, "%pV", &vaf);
  278. va_end(args);
  279. }
  280. int gfs2_withdraw(struct gfs2_sbd *sdp)
  281. {
  282. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  283. const struct lm_lockops *lm = ls->ls_ops;
  284. if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) {
  285. unsigned long old = READ_ONCE(sdp->sd_flags), new;
  286. do {
  287. if (old & BIT(SDF_WITHDRAWN)) {
  288. wait_on_bit(&sdp->sd_flags,
  289. SDF_WITHDRAW_IN_PROG,
  290. TASK_UNINTERRUPTIBLE);
  291. return -1;
  292. }
  293. new = old | BIT(SDF_WITHDRAWN) | BIT(SDF_WITHDRAW_IN_PROG);
  294. } while (unlikely(!try_cmpxchg(&sdp->sd_flags, &old, new)));
  295. fs_err(sdp, "about to withdraw this file system\n");
  296. BUG_ON(sdp->sd_args.ar_debug);
  297. signal_our_withdraw(sdp);
  298. kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE);
  299. if (!strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
  300. wait_for_completion(&sdp->sd_wdack);
  301. if (lm->lm_unmount) {
  302. fs_err(sdp, "telling LM to unmount\n");
  303. lm->lm_unmount(sdp);
  304. }
  305. fs_err(sdp, "File system withdrawn\n");
  306. dump_stack();
  307. clear_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags);
  308. smp_mb__after_atomic();
  309. wake_up_bit(&sdp->sd_flags, SDF_WITHDRAW_IN_PROG);
  310. }
  311. if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
  312. panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname);
  313. return -1;
  314. }
  315. /*
  316. * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
  317. */
  318. void gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
  319. const char *function, char *file, unsigned int line,
  320. bool delayed)
  321. {
  322. if (gfs2_withdrawing_or_withdrawn(sdp))
  323. return;
  324. fs_err(sdp,
  325. "fatal: assertion \"%s\" failed - "
  326. "function = %s, file = %s, line = %u\n",
  327. assertion, function, file, line);
  328. /*
  329. * If errors=panic was specified on mount, it won't help to delay the
  330. * withdraw.
  331. */
  332. if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
  333. delayed = false;
  334. if (delayed)
  335. gfs2_withdraw_delayed(sdp);
  336. else
  337. gfs2_withdraw(sdp);
  338. dump_stack();
  339. }
  340. /*
  341. * gfs2_assert_warn_i - Print a message to the console if @assertion is false
  342. */
  343. void gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
  344. const char *function, char *file, unsigned int line)
  345. {
  346. if (time_before(jiffies,
  347. sdp->sd_last_warning +
  348. gfs2_tune_get(sdp, gt_complain_secs) * HZ))
  349. return;
  350. if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW)
  351. fs_warn(sdp, "warning: assertion \"%s\" failed - "
  352. "function = %s, file = %s, line = %u\n",
  353. assertion, function, file, line);
  354. if (sdp->sd_args.ar_debug)
  355. BUG();
  356. else
  357. dump_stack();
  358. if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
  359. panic("GFS2: fsid=%s: warning: assertion \"%s\" failed - "
  360. "function = %s, file = %s, line = %u\n",
  361. sdp->sd_fsname, assertion,
  362. function, file, line);
  363. sdp->sd_last_warning = jiffies;
  364. }
  365. /*
  366. * gfs2_consist_i - Flag a filesystem consistency error and withdraw
  367. */
  368. void gfs2_consist_i(struct gfs2_sbd *sdp, const char *function,
  369. char *file, unsigned int line)
  370. {
  371. gfs2_lm(sdp,
  372. "fatal: filesystem consistency error - "
  373. "function = %s, file = %s, line = %u\n",
  374. function, file, line);
  375. gfs2_withdraw(sdp);
  376. }
  377. /*
  378. * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
  379. */
  380. void gfs2_consist_inode_i(struct gfs2_inode *ip,
  381. const char *function, char *file, unsigned int line)
  382. {
  383. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  384. gfs2_lm(sdp,
  385. "fatal: filesystem consistency error - "
  386. "inode = %llu %llu, "
  387. "function = %s, file = %s, line = %u\n",
  388. (unsigned long long)ip->i_no_formal_ino,
  389. (unsigned long long)ip->i_no_addr,
  390. function, file, line);
  391. gfs2_dump_glock(NULL, ip->i_gl, 1);
  392. gfs2_withdraw(sdp);
  393. }
  394. /*
  395. * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
  396. */
  397. void gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd,
  398. const char *function, char *file, unsigned int line)
  399. {
  400. struct gfs2_sbd *sdp = rgd->rd_sbd;
  401. char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
  402. sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
  403. gfs2_rgrp_dump(NULL, rgd, fs_id_buf);
  404. gfs2_lm(sdp,
  405. "fatal: filesystem consistency error - "
  406. "RG = %llu, "
  407. "function = %s, file = %s, line = %u\n",
  408. (unsigned long long)rgd->rd_addr,
  409. function, file, line);
  410. gfs2_dump_glock(NULL, rgd->rd_gl, 1);
  411. gfs2_withdraw(sdp);
  412. }
  413. /*
  414. * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
  415. * Returns: -1 if this call withdrew the machine,
  416. * -2 if it was already withdrawn
  417. */
  418. int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
  419. const char *function, char *file,
  420. unsigned int line)
  421. {
  422. int me;
  423. gfs2_lm(sdp,
  424. "fatal: invalid metadata block - "
  425. "bh = %llu (bad magic number), "
  426. "function = %s, file = %s, line = %u\n",
  427. (unsigned long long)bh->b_blocknr,
  428. function, file, line);
  429. me = gfs2_withdraw(sdp);
  430. return (me) ? -1 : -2;
  431. }
  432. /*
  433. * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
  434. * Returns: -1 if this call withdrew the machine,
  435. * -2 if it was already withdrawn
  436. */
  437. int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
  438. u16 type, u16 t, const char *function,
  439. char *file, unsigned int line)
  440. {
  441. int me;
  442. gfs2_lm(sdp,
  443. "fatal: invalid metadata block - "
  444. "bh = %llu (type: exp=%u, found=%u), "
  445. "function = %s, file = %s, line = %u\n",
  446. (unsigned long long)bh->b_blocknr, type, t,
  447. function, file, line);
  448. me = gfs2_withdraw(sdp);
  449. return (me) ? -1 : -2;
  450. }
  451. /*
  452. * gfs2_io_error_i - Flag an I/O error and withdraw
  453. * Returns: -1 if this call withdrew the machine,
  454. * 0 if it was already withdrawn
  455. */
  456. int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
  457. unsigned int line)
  458. {
  459. gfs2_lm(sdp,
  460. "fatal: I/O error - "
  461. "function = %s, file = %s, line = %u\n",
  462. function, file, line);
  463. return gfs2_withdraw(sdp);
  464. }
  465. /*
  466. * gfs2_io_error_bh_i - Flag a buffer I/O error
  467. * @withdraw: withdraw the filesystem
  468. */
  469. void gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
  470. const char *function, char *file, unsigned int line,
  471. bool withdraw)
  472. {
  473. if (gfs2_withdrawing_or_withdrawn(sdp))
  474. return;
  475. fs_err(sdp, "fatal: I/O error - "
  476. "block = %llu, "
  477. "function = %s, file = %s, line = %u\n",
  478. (unsigned long long)bh->b_blocknr, function, file, line);
  479. if (withdraw)
  480. gfs2_withdraw(sdp);
  481. }