glops.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/spinlock.h>
  7. #include <linux/completion.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/gfs2_ondisk.h>
  10. #include <linux/bio.h>
  11. #include <linux/posix_acl.h>
  12. #include <linux/security.h>
  13. #include <linux/log2.h>
  14. #include "gfs2.h"
  15. #include "incore.h"
  16. #include "bmap.h"
  17. #include "glock.h"
  18. #include "glops.h"
  19. #include "inode.h"
  20. #include "log.h"
  21. #include "meta_io.h"
  22. #include "recovery.h"
  23. #include "rgrp.h"
  24. #include "util.h"
  25. #include "trans.h"
  26. #include "dir.h"
  27. #include "lops.h"
  28. struct workqueue_struct *gfs2_freeze_wq;
  29. extern struct workqueue_struct *gfs2_control_wq;
  30. static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)
  31. {
  32. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  33. fs_err(sdp,
  34. "AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page "
  35. "state 0x%lx\n",
  36. bh, (unsigned long long)bh->b_blocknr, bh->b_state,
  37. bh->b_folio->mapping, bh->b_folio->flags);
  38. fs_err(sdp, "AIL glock %u:%llu mapping %p\n",
  39. gl->gl_name.ln_type, gl->gl_name.ln_number,
  40. gfs2_glock2aspace(gl));
  41. gfs2_lm(sdp, "AIL error\n");
  42. gfs2_withdraw_delayed(sdp);
  43. }
  44. /**
  45. * __gfs2_ail_flush - remove all buffers for a given lock from the AIL
  46. * @gl: the glock
  47. * @fsync: set when called from fsync (not all buffers will be clean)
  48. * @nr_revokes: Number of buffers to revoke
  49. *
  50. * None of the buffers should be dirty, locked, or pinned.
  51. */
  52. static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
  53. unsigned int nr_revokes)
  54. {
  55. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  56. struct list_head *head = &gl->gl_ail_list;
  57. struct gfs2_bufdata *bd, *tmp;
  58. struct buffer_head *bh;
  59. const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
  60. gfs2_log_lock(sdp);
  61. spin_lock(&sdp->sd_ail_lock);
  62. list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
  63. if (nr_revokes == 0)
  64. break;
  65. bh = bd->bd_bh;
  66. if (bh->b_state & b_state) {
  67. if (fsync)
  68. continue;
  69. gfs2_ail_error(gl, bh);
  70. }
  71. gfs2_trans_add_revoke(sdp, bd);
  72. nr_revokes--;
  73. }
  74. GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
  75. spin_unlock(&sdp->sd_ail_lock);
  76. gfs2_log_unlock(sdp);
  77. if (gfs2_withdrawing(sdp))
  78. gfs2_withdraw(sdp);
  79. }
  80. static int gfs2_ail_empty_gl(struct gfs2_glock *gl)
  81. {
  82. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  83. struct gfs2_trans tr;
  84. unsigned int revokes;
  85. int ret = 0;
  86. revokes = atomic_read(&gl->gl_ail_count);
  87. if (!revokes) {
  88. bool have_revokes;
  89. bool log_in_flight;
  90. /*
  91. * We have nothing on the ail, but there could be revokes on
  92. * the sdp revoke queue, in which case, we still want to flush
  93. * the log and wait for it to finish.
  94. *
  95. * If the sdp revoke list is empty too, we might still have an
  96. * io outstanding for writing revokes, so we should wait for
  97. * it before returning.
  98. *
  99. * If none of these conditions are true, our revokes are all
  100. * flushed and we can return.
  101. */
  102. gfs2_log_lock(sdp);
  103. have_revokes = !list_empty(&sdp->sd_log_revokes);
  104. log_in_flight = atomic_read(&sdp->sd_log_in_flight);
  105. gfs2_log_unlock(sdp);
  106. if (have_revokes)
  107. goto flush;
  108. if (log_in_flight)
  109. log_flush_wait(sdp);
  110. return 0;
  111. }
  112. memset(&tr, 0, sizeof(tr));
  113. set_bit(TR_ONSTACK, &tr.tr_flags);
  114. ret = __gfs2_trans_begin(&tr, sdp, 0, revokes, _RET_IP_);
  115. if (ret) {
  116. fs_err(sdp, "Transaction error %d: Unable to write revokes.", ret);
  117. goto flush;
  118. }
  119. __gfs2_ail_flush(gl, 0, revokes);
  120. gfs2_trans_end(sdp);
  121. flush:
  122. if (!ret)
  123. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  124. GFS2_LFC_AIL_EMPTY_GL);
  125. return ret;
  126. }
  127. void gfs2_ail_flush(struct gfs2_glock *gl, bool fsync)
  128. {
  129. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  130. unsigned int revokes = atomic_read(&gl->gl_ail_count);
  131. int ret;
  132. if (!revokes)
  133. return;
  134. ret = gfs2_trans_begin(sdp, 0, revokes);
  135. if (ret)
  136. return;
  137. __gfs2_ail_flush(gl, fsync, revokes);
  138. gfs2_trans_end(sdp);
  139. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  140. GFS2_LFC_AIL_FLUSH);
  141. }
  142. /**
  143. * gfs2_rgrp_metasync - sync out the metadata of a resource group
  144. * @gl: the glock protecting the resource group
  145. *
  146. */
  147. static int gfs2_rgrp_metasync(struct gfs2_glock *gl)
  148. {
  149. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  150. struct address_space *metamapping = gfs2_aspace(sdp);
  151. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  152. const unsigned bsize = sdp->sd_sb.sb_bsize;
  153. loff_t start = (rgd->rd_addr * bsize) & PAGE_MASK;
  154. loff_t end = PAGE_ALIGN((rgd->rd_addr + rgd->rd_length) * bsize) - 1;
  155. int error;
  156. filemap_fdatawrite_range(metamapping, start, end);
  157. error = filemap_fdatawait_range(metamapping, start, end);
  158. WARN_ON_ONCE(error && !gfs2_withdrawing_or_withdrawn(sdp));
  159. mapping_set_error(metamapping, error);
  160. if (error)
  161. gfs2_io_error(sdp);
  162. return error;
  163. }
  164. /**
  165. * rgrp_go_sync - sync out the metadata for this glock
  166. * @gl: the glock
  167. *
  168. * Called when demoting or unlocking an EX glock. We must flush
  169. * to disk all dirty buffers/pages relating to this glock, and must not
  170. * return to caller to demote/unlock the glock until I/O is complete.
  171. */
  172. static int rgrp_go_sync(struct gfs2_glock *gl)
  173. {
  174. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  175. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  176. int error;
  177. if (!rgd || !test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
  178. return 0;
  179. GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
  180. gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  181. GFS2_LFC_RGRP_GO_SYNC);
  182. error = gfs2_rgrp_metasync(gl);
  183. if (!error)
  184. error = gfs2_ail_empty_gl(gl);
  185. gfs2_free_clones(rgd);
  186. return error;
  187. }
  188. /**
  189. * rgrp_go_inval - invalidate the metadata for this glock
  190. * @gl: the glock
  191. * @flags:
  192. *
  193. * We never used LM_ST_DEFERRED with resource groups, so that we
  194. * should always see the metadata flag set here.
  195. *
  196. */
  197. static void rgrp_go_inval(struct gfs2_glock *gl, int flags)
  198. {
  199. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  200. struct address_space *mapping = gfs2_aspace(sdp);
  201. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  202. const unsigned bsize = sdp->sd_sb.sb_bsize;
  203. loff_t start, end;
  204. if (!rgd)
  205. return;
  206. start = (rgd->rd_addr * bsize) & PAGE_MASK;
  207. end = PAGE_ALIGN((rgd->rd_addr + rgd->rd_length) * bsize) - 1;
  208. gfs2_rgrp_brelse(rgd);
  209. WARN_ON_ONCE(!(flags & DIO_METADATA));
  210. truncate_inode_pages_range(mapping, start, end);
  211. }
  212. static void gfs2_rgrp_go_dump(struct seq_file *seq, const struct gfs2_glock *gl,
  213. const char *fs_id_buf)
  214. {
  215. struct gfs2_rgrpd *rgd = gl->gl_object;
  216. if (rgd)
  217. gfs2_rgrp_dump(seq, rgd, fs_id_buf);
  218. }
  219. static struct gfs2_inode *gfs2_glock2inode(struct gfs2_glock *gl)
  220. {
  221. struct gfs2_inode *ip;
  222. spin_lock(&gl->gl_lockref.lock);
  223. ip = gl->gl_object;
  224. if (ip)
  225. set_bit(GIF_GLOP_PENDING, &ip->i_flags);
  226. spin_unlock(&gl->gl_lockref.lock);
  227. return ip;
  228. }
  229. struct gfs2_rgrpd *gfs2_glock2rgrp(struct gfs2_glock *gl)
  230. {
  231. struct gfs2_rgrpd *rgd;
  232. spin_lock(&gl->gl_lockref.lock);
  233. rgd = gl->gl_object;
  234. spin_unlock(&gl->gl_lockref.lock);
  235. return rgd;
  236. }
  237. static void gfs2_clear_glop_pending(struct gfs2_inode *ip)
  238. {
  239. if (!ip)
  240. return;
  241. clear_bit_unlock(GIF_GLOP_PENDING, &ip->i_flags);
  242. wake_up_bit(&ip->i_flags, GIF_GLOP_PENDING);
  243. }
  244. /**
  245. * gfs2_inode_metasync - sync out the metadata of an inode
  246. * @gl: the glock protecting the inode
  247. *
  248. */
  249. int gfs2_inode_metasync(struct gfs2_glock *gl)
  250. {
  251. struct address_space *metamapping = gfs2_glock2aspace(gl);
  252. int error;
  253. filemap_fdatawrite(metamapping);
  254. error = filemap_fdatawait(metamapping);
  255. if (error)
  256. gfs2_io_error(gl->gl_name.ln_sbd);
  257. return error;
  258. }
  259. /**
  260. * inode_go_sync - Sync the dirty metadata of an inode
  261. * @gl: the glock protecting the inode
  262. *
  263. */
  264. static int inode_go_sync(struct gfs2_glock *gl)
  265. {
  266. struct gfs2_inode *ip = gfs2_glock2inode(gl);
  267. int isreg = ip && S_ISREG(ip->i_inode.i_mode);
  268. struct address_space *metamapping = gfs2_glock2aspace(gl);
  269. int error = 0, ret;
  270. if (isreg) {
  271. if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
  272. unmap_shared_mapping_range(ip->i_inode.i_mapping, 0, 0);
  273. inode_dio_wait(&ip->i_inode);
  274. }
  275. if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
  276. goto out;
  277. GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
  278. gfs2_log_flush(gl->gl_name.ln_sbd, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  279. GFS2_LFC_INODE_GO_SYNC);
  280. filemap_fdatawrite(metamapping);
  281. if (isreg) {
  282. struct address_space *mapping = ip->i_inode.i_mapping;
  283. filemap_fdatawrite(mapping);
  284. error = filemap_fdatawait(mapping);
  285. mapping_set_error(mapping, error);
  286. }
  287. ret = gfs2_inode_metasync(gl);
  288. if (!error)
  289. error = ret;
  290. ret = gfs2_ail_empty_gl(gl);
  291. if (!error)
  292. error = ret;
  293. /*
  294. * Writeback of the data mapping may cause the dirty flag to be set
  295. * so we have to clear it again here.
  296. */
  297. smp_mb__before_atomic();
  298. clear_bit(GLF_DIRTY, &gl->gl_flags);
  299. out:
  300. gfs2_clear_glop_pending(ip);
  301. return error;
  302. }
  303. /**
  304. * inode_go_inval - prepare a inode glock to be released
  305. * @gl: the glock
  306. * @flags:
  307. *
  308. * Normally we invalidate everything, but if we are moving into
  309. * LM_ST_DEFERRED from LM_ST_SHARED or LM_ST_EXCLUSIVE then we
  310. * can keep hold of the metadata, since it won't have changed.
  311. *
  312. */
  313. static void inode_go_inval(struct gfs2_glock *gl, int flags)
  314. {
  315. struct gfs2_inode *ip = gfs2_glock2inode(gl);
  316. if (flags & DIO_METADATA) {
  317. struct address_space *mapping = gfs2_glock2aspace(gl);
  318. truncate_inode_pages(mapping, 0);
  319. if (ip) {
  320. set_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags);
  321. forget_all_cached_acls(&ip->i_inode);
  322. security_inode_invalidate_secctx(&ip->i_inode);
  323. gfs2_dir_hash_inval(ip);
  324. }
  325. }
  326. if (ip == GFS2_I(gl->gl_name.ln_sbd->sd_rindex)) {
  327. gfs2_log_flush(gl->gl_name.ln_sbd, NULL,
  328. GFS2_LOG_HEAD_FLUSH_NORMAL |
  329. GFS2_LFC_INODE_GO_INVAL);
  330. gl->gl_name.ln_sbd->sd_rindex_uptodate = 0;
  331. }
  332. if (ip && S_ISREG(ip->i_inode.i_mode))
  333. truncate_inode_pages(ip->i_inode.i_mapping, 0);
  334. gfs2_clear_glop_pending(ip);
  335. }
  336. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  337. {
  338. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  339. const struct gfs2_dinode *str = buf;
  340. struct timespec64 atime, iatime;
  341. u16 height, depth;
  342. umode_t mode = be32_to_cpu(str->di_mode);
  343. struct inode *inode = &ip->i_inode;
  344. bool is_new = inode->i_state & I_NEW;
  345. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr))) {
  346. gfs2_consist_inode(ip);
  347. return -EIO;
  348. }
  349. if (unlikely(!is_new && inode_wrong_type(inode, mode))) {
  350. gfs2_consist_inode(ip);
  351. return -EIO;
  352. }
  353. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  354. inode->i_mode = mode;
  355. if (is_new) {
  356. inode->i_rdev = 0;
  357. switch (mode & S_IFMT) {
  358. case S_IFBLK:
  359. case S_IFCHR:
  360. inode->i_rdev = MKDEV(be32_to_cpu(str->di_major),
  361. be32_to_cpu(str->di_minor));
  362. break;
  363. }
  364. }
  365. i_uid_write(inode, be32_to_cpu(str->di_uid));
  366. i_gid_write(inode, be32_to_cpu(str->di_gid));
  367. set_nlink(inode, be32_to_cpu(str->di_nlink));
  368. i_size_write(inode, be64_to_cpu(str->di_size));
  369. gfs2_set_inode_blocks(inode, be64_to_cpu(str->di_blocks));
  370. atime.tv_sec = be64_to_cpu(str->di_atime);
  371. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  372. iatime = inode_get_atime(inode);
  373. if (timespec64_compare(&iatime, &atime) < 0)
  374. inode_set_atime_to_ts(inode, atime);
  375. inode_set_mtime(inode, be64_to_cpu(str->di_mtime),
  376. be32_to_cpu(str->di_mtime_nsec));
  377. inode_set_ctime(inode, be64_to_cpu(str->di_ctime),
  378. be32_to_cpu(str->di_ctime_nsec));
  379. ip->i_goal = be64_to_cpu(str->di_goal_meta);
  380. ip->i_generation = be64_to_cpu(str->di_generation);
  381. ip->i_diskflags = be32_to_cpu(str->di_flags);
  382. ip->i_eattr = be64_to_cpu(str->di_eattr);
  383. /* i_diskflags and i_eattr must be set before gfs2_set_inode_flags() */
  384. gfs2_set_inode_flags(inode);
  385. height = be16_to_cpu(str->di_height);
  386. if (unlikely(height > sdp->sd_max_height)) {
  387. gfs2_consist_inode(ip);
  388. return -EIO;
  389. }
  390. ip->i_height = (u8)height;
  391. depth = be16_to_cpu(str->di_depth);
  392. if (unlikely(depth > GFS2_DIR_MAX_DEPTH)) {
  393. gfs2_consist_inode(ip);
  394. return -EIO;
  395. }
  396. if ((ip->i_diskflags & GFS2_DIF_EXHASH) &&
  397. depth < ilog2(sdp->sd_hash_ptrs)) {
  398. gfs2_consist_inode(ip);
  399. return -EIO;
  400. }
  401. ip->i_depth = (u8)depth;
  402. ip->i_entries = be32_to_cpu(str->di_entries);
  403. if (gfs2_is_stuffed(ip) && inode->i_size > gfs2_max_stuffed_size(ip)) {
  404. gfs2_consist_inode(ip);
  405. return -EIO;
  406. }
  407. if (S_ISREG(inode->i_mode))
  408. gfs2_set_aops(inode);
  409. return 0;
  410. }
  411. /**
  412. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  413. * @ip: The GFS2 inode
  414. *
  415. * Returns: errno
  416. */
  417. int gfs2_inode_refresh(struct gfs2_inode *ip)
  418. {
  419. struct buffer_head *dibh;
  420. int error;
  421. error = gfs2_meta_inode_buffer(ip, &dibh);
  422. if (error)
  423. return error;
  424. error = gfs2_dinode_in(ip, dibh->b_data);
  425. brelse(dibh);
  426. return error;
  427. }
  428. /**
  429. * inode_go_instantiate - read in an inode if necessary
  430. * @gl: The glock
  431. *
  432. * Returns: errno
  433. */
  434. static int inode_go_instantiate(struct gfs2_glock *gl)
  435. {
  436. struct gfs2_inode *ip = gl->gl_object;
  437. struct gfs2_glock *io_gl;
  438. int error;
  439. if (!ip) /* no inode to populate - read it in later */
  440. return 0;
  441. error = gfs2_inode_refresh(ip);
  442. if (error)
  443. return error;
  444. io_gl = ip->i_iopen_gh.gh_gl;
  445. io_gl->gl_no_formal_ino = ip->i_no_formal_ino;
  446. return 0;
  447. }
  448. static int inode_go_held(struct gfs2_holder *gh)
  449. {
  450. struct gfs2_glock *gl = gh->gh_gl;
  451. struct gfs2_inode *ip = gl->gl_object;
  452. int error = 0;
  453. if (!ip) /* no inode to populate - read it in later */
  454. return 0;
  455. if (gh->gh_state != LM_ST_DEFERRED)
  456. inode_dio_wait(&ip->i_inode);
  457. if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
  458. (gl->gl_state == LM_ST_EXCLUSIVE) &&
  459. (gh->gh_state == LM_ST_EXCLUSIVE))
  460. error = gfs2_truncatei_resume(ip);
  461. return error;
  462. }
  463. /**
  464. * inode_go_dump - print information about an inode
  465. * @seq: The iterator
  466. * @gl: The glock
  467. * @fs_id_buf: file system id (may be empty)
  468. *
  469. */
  470. static void inode_go_dump(struct seq_file *seq, const struct gfs2_glock *gl,
  471. const char *fs_id_buf)
  472. {
  473. struct gfs2_inode *ip = gl->gl_object;
  474. const struct inode *inode = &ip->i_inode;
  475. if (ip == NULL)
  476. return;
  477. gfs2_print_dbg(seq, "%s I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu "
  478. "p:%lu\n", fs_id_buf,
  479. (unsigned long long)ip->i_no_formal_ino,
  480. (unsigned long long)ip->i_no_addr,
  481. IF2DT(inode->i_mode), ip->i_flags,
  482. (unsigned int)ip->i_diskflags,
  483. (unsigned long long)i_size_read(inode),
  484. inode->i_data.nrpages);
  485. }
  486. /**
  487. * freeze_go_callback - A cluster node is requesting a freeze
  488. * @gl: the glock
  489. * @remote: true if this came from a different cluster node
  490. */
  491. static void freeze_go_callback(struct gfs2_glock *gl, bool remote)
  492. {
  493. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  494. struct super_block *sb = sdp->sd_vfs;
  495. if (!remote ||
  496. (gl->gl_state != LM_ST_SHARED &&
  497. gl->gl_state != LM_ST_UNLOCKED) ||
  498. gl->gl_demote_state != LM_ST_UNLOCKED)
  499. return;
  500. /*
  501. * Try to get an active super block reference to prevent racing with
  502. * unmount (see super_trylock_shared()). But note that unmount isn't
  503. * the only place where a write lock on s_umount is taken, and we can
  504. * fail here because of things like remount as well.
  505. */
  506. if (down_read_trylock(&sb->s_umount)) {
  507. atomic_inc(&sb->s_active);
  508. up_read(&sb->s_umount);
  509. if (!queue_work(gfs2_freeze_wq, &sdp->sd_freeze_work))
  510. deactivate_super(sb);
  511. }
  512. }
  513. /**
  514. * freeze_go_xmote_bh - After promoting/demoting the freeze glock
  515. * @gl: the glock
  516. */
  517. static int freeze_go_xmote_bh(struct gfs2_glock *gl)
  518. {
  519. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  520. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  521. struct gfs2_glock *j_gl = ip->i_gl;
  522. struct gfs2_log_header_host head;
  523. int error;
  524. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  525. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  526. error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
  527. if (gfs2_assert_withdraw_delayed(sdp, !error))
  528. return error;
  529. if (gfs2_assert_withdraw_delayed(sdp, head.lh_flags &
  530. GFS2_LOG_HEAD_UNMOUNT))
  531. return -EIO;
  532. sdp->sd_log_sequence = head.lh_sequence + 1;
  533. gfs2_log_pointers_init(sdp, head.lh_blkno);
  534. }
  535. return 0;
  536. }
  537. /**
  538. * iopen_go_callback - schedule the dcache entry for the inode to be deleted
  539. * @gl: the glock
  540. * @remote: true if this came from a different cluster node
  541. *
  542. * gl_lockref.lock lock is held while calling this
  543. */
  544. static void iopen_go_callback(struct gfs2_glock *gl, bool remote)
  545. {
  546. struct gfs2_inode *ip = gl->gl_object;
  547. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  548. if (!remote || sb_rdonly(sdp->sd_vfs) ||
  549. test_bit(SDF_KILL, &sdp->sd_flags))
  550. return;
  551. if (gl->gl_demote_state == LM_ST_UNLOCKED &&
  552. gl->gl_state == LM_ST_SHARED && ip) {
  553. gl->gl_lockref.count++;
  554. if (!gfs2_queue_try_to_evict(gl))
  555. gl->gl_lockref.count--;
  556. }
  557. }
  558. /**
  559. * inode_go_unlocked - wake up anyone waiting for dlm's unlock ast
  560. * @gl: glock being unlocked
  561. *
  562. * For now, this is only used for the journal inode glock. In withdraw
  563. * situations, we need to wait for the glock to be unlocked so that we know
  564. * other nodes may proceed with recovery / journal replay.
  565. */
  566. static void inode_go_unlocked(struct gfs2_glock *gl)
  567. {
  568. /* Note that we cannot reference gl_object because it's already set
  569. * to NULL by this point in its lifecycle. */
  570. if (!test_bit(GLF_UNLOCKED, &gl->gl_flags))
  571. return;
  572. clear_bit_unlock(GLF_UNLOCKED, &gl->gl_flags);
  573. wake_up_bit(&gl->gl_flags, GLF_UNLOCKED);
  574. }
  575. /**
  576. * nondisk_go_callback - used to signal when a node did a withdraw
  577. * @gl: the nondisk glock
  578. * @remote: true if this came from a different cluster node
  579. *
  580. */
  581. static void nondisk_go_callback(struct gfs2_glock *gl, bool remote)
  582. {
  583. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  584. /* Ignore the callback unless it's from another node, and it's the
  585. live lock. */
  586. if (!remote || gl->gl_name.ln_number != GFS2_LIVE_LOCK)
  587. return;
  588. /* First order of business is to cancel the demote request. We don't
  589. * really want to demote a nondisk glock. At best it's just to inform
  590. * us of another node's withdraw. We'll keep it in SH mode. */
  591. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  592. clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
  593. /* Ignore the unlock if we're withdrawn, unmounting, or in recovery. */
  594. if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) ||
  595. test_bit(SDF_WITHDRAWN, &sdp->sd_flags) ||
  596. test_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags))
  597. return;
  598. /* We only care when a node wants us to unlock, because that means
  599. * they want a journal recovered. */
  600. if (gl->gl_demote_state != LM_ST_UNLOCKED)
  601. return;
  602. if (sdp->sd_args.ar_spectator) {
  603. fs_warn(sdp, "Spectator node cannot recover journals.\n");
  604. return;
  605. }
  606. fs_warn(sdp, "Some node has withdrawn; checking for recovery.\n");
  607. set_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags);
  608. /*
  609. * We can't call remote_withdraw directly here or gfs2_recover_journal
  610. * because this is called from the glock unlock function and the
  611. * remote_withdraw needs to enqueue and dequeue the same "live" glock
  612. * we were called from. So we queue it to the control work queue in
  613. * lock_dlm.
  614. */
  615. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
  616. }
  617. const struct gfs2_glock_operations gfs2_meta_glops = {
  618. .go_type = LM_TYPE_META,
  619. .go_flags = GLOF_NONDISK,
  620. };
  621. const struct gfs2_glock_operations gfs2_inode_glops = {
  622. .go_sync = inode_go_sync,
  623. .go_inval = inode_go_inval,
  624. .go_instantiate = inode_go_instantiate,
  625. .go_held = inode_go_held,
  626. .go_dump = inode_go_dump,
  627. .go_type = LM_TYPE_INODE,
  628. .go_flags = GLOF_ASPACE | GLOF_LVB,
  629. .go_unlocked = inode_go_unlocked,
  630. };
  631. const struct gfs2_glock_operations gfs2_rgrp_glops = {
  632. .go_sync = rgrp_go_sync,
  633. .go_inval = rgrp_go_inval,
  634. .go_instantiate = gfs2_rgrp_go_instantiate,
  635. .go_dump = gfs2_rgrp_go_dump,
  636. .go_type = LM_TYPE_RGRP,
  637. .go_flags = GLOF_LVB,
  638. };
  639. const struct gfs2_glock_operations gfs2_freeze_glops = {
  640. .go_xmote_bh = freeze_go_xmote_bh,
  641. .go_callback = freeze_go_callback,
  642. .go_type = LM_TYPE_NONDISK,
  643. .go_flags = GLOF_NONDISK,
  644. };
  645. const struct gfs2_glock_operations gfs2_iopen_glops = {
  646. .go_type = LM_TYPE_IOPEN,
  647. .go_callback = iopen_go_callback,
  648. .go_dump = inode_go_dump,
  649. .go_flags = GLOF_NONDISK,
  650. .go_subclass = 1,
  651. };
  652. const struct gfs2_glock_operations gfs2_flock_glops = {
  653. .go_type = LM_TYPE_FLOCK,
  654. .go_flags = GLOF_NONDISK,
  655. };
  656. const struct gfs2_glock_operations gfs2_nondisk_glops = {
  657. .go_type = LM_TYPE_NONDISK,
  658. .go_flags = GLOF_NONDISK,
  659. .go_callback = nondisk_go_callback,
  660. };
  661. const struct gfs2_glock_operations gfs2_quota_glops = {
  662. .go_type = LM_TYPE_QUOTA,
  663. .go_flags = GLOF_LVB | GLOF_NONDISK,
  664. };
  665. const struct gfs2_glock_operations gfs2_journal_glops = {
  666. .go_type = LM_TYPE_JOURNAL,
  667. .go_flags = GLOF_NONDISK,
  668. };
  669. const struct gfs2_glock_operations *gfs2_glops_list[] = {
  670. [LM_TYPE_META] = &gfs2_meta_glops,
  671. [LM_TYPE_INODE] = &gfs2_inode_glops,
  672. [LM_TYPE_RGRP] = &gfs2_rgrp_glops,
  673. [LM_TYPE_IOPEN] = &gfs2_iopen_glops,
  674. [LM_TYPE_FLOCK] = &gfs2_flock_glops,
  675. [LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
  676. [LM_TYPE_QUOTA] = &gfs2_quota_glops,
  677. [LM_TYPE_JOURNAL] = &gfs2_journal_glops,
  678. };