glops.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 "gfs2.h"
  14. #include "incore.h"
  15. #include "bmap.h"
  16. #include "glock.h"
  17. #include "glops.h"
  18. #include "inode.h"
  19. #include "log.h"
  20. #include "meta_io.h"
  21. #include "recovery.h"
  22. #include "rgrp.h"
  23. #include "util.h"
  24. #include "trans.h"
  25. #include "dir.h"
  26. #include "lops.h"
  27. struct workqueue_struct *gfs2_freeze_wq;
  28. extern struct workqueue_struct *gfs2_control_wq;
  29. static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)
  30. {
  31. fs_err(gl->gl_name.ln_sbd,
  32. "AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page "
  33. "state 0x%lx\n",
  34. bh, (unsigned long long)bh->b_blocknr, bh->b_state,
  35. bh->b_page->mapping, bh->b_page->flags);
  36. fs_err(gl->gl_name.ln_sbd, "AIL glock %u:%llu mapping %p\n",
  37. gl->gl_name.ln_type, gl->gl_name.ln_number,
  38. gfs2_glock2aspace(gl));
  39. gfs2_lm(gl->gl_name.ln_sbd, "AIL error\n");
  40. gfs2_withdraw(gl->gl_name.ln_sbd);
  41. }
  42. /**
  43. * __gfs2_ail_flush - remove all buffers for a given lock from the AIL
  44. * @gl: the glock
  45. * @fsync: set when called from fsync (not all buffers will be clean)
  46. *
  47. * None of the buffers should be dirty, locked, or pinned.
  48. */
  49. static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
  50. unsigned int nr_revokes)
  51. {
  52. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  53. struct list_head *head = &gl->gl_ail_list;
  54. struct gfs2_bufdata *bd, *tmp;
  55. struct buffer_head *bh;
  56. const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
  57. gfs2_log_lock(sdp);
  58. spin_lock(&sdp->sd_ail_lock);
  59. list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
  60. if (nr_revokes == 0)
  61. break;
  62. bh = bd->bd_bh;
  63. if (bh->b_state & b_state) {
  64. if (fsync)
  65. continue;
  66. gfs2_ail_error(gl, bh);
  67. }
  68. gfs2_trans_add_revoke(sdp, bd);
  69. nr_revokes--;
  70. }
  71. GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
  72. spin_unlock(&sdp->sd_ail_lock);
  73. gfs2_log_unlock(sdp);
  74. }
  75. static int gfs2_ail_empty_gl(struct gfs2_glock *gl)
  76. {
  77. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  78. struct gfs2_trans tr;
  79. int ret;
  80. memset(&tr, 0, sizeof(tr));
  81. INIT_LIST_HEAD(&tr.tr_buf);
  82. INIT_LIST_HEAD(&tr.tr_databuf);
  83. INIT_LIST_HEAD(&tr.tr_ail1_list);
  84. INIT_LIST_HEAD(&tr.tr_ail2_list);
  85. tr.tr_revokes = atomic_read(&gl->gl_ail_count);
  86. if (!tr.tr_revokes) {
  87. bool have_revokes;
  88. bool log_in_flight;
  89. /*
  90. * We have nothing on the ail, but there could be revokes on
  91. * the sdp revoke queue, in which case, we still want to flush
  92. * the log and wait for it to finish.
  93. *
  94. * If the sdp revoke list is empty too, we might still have an
  95. * io outstanding for writing revokes, so we should wait for
  96. * it before returning.
  97. *
  98. * If none of these conditions are true, our revokes are all
  99. * flushed and we can return.
  100. */
  101. gfs2_log_lock(sdp);
  102. have_revokes = !list_empty(&sdp->sd_log_revokes);
  103. log_in_flight = atomic_read(&sdp->sd_log_in_flight);
  104. gfs2_log_unlock(sdp);
  105. if (have_revokes)
  106. goto flush;
  107. if (log_in_flight)
  108. log_flush_wait(sdp);
  109. return 0;
  110. }
  111. /* A shortened, inline version of gfs2_trans_begin()
  112. * tr->alloced is not set since the transaction structure is
  113. * on the stack */
  114. tr.tr_reserved = 1 + gfs2_struct2blk(sdp, tr.tr_revokes);
  115. tr.tr_ip = _RET_IP_;
  116. ret = gfs2_log_reserve(sdp, tr.tr_reserved);
  117. if (ret < 0)
  118. return ret;
  119. WARN_ON_ONCE(current->journal_info);
  120. current->journal_info = &tr;
  121. __gfs2_ail_flush(gl, 0, tr.tr_revokes);
  122. gfs2_trans_end(sdp);
  123. flush:
  124. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  125. GFS2_LFC_AIL_EMPTY_GL);
  126. return 0;
  127. }
  128. void gfs2_ail_flush(struct gfs2_glock *gl, bool fsync)
  129. {
  130. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  131. unsigned int revokes = atomic_read(&gl->gl_ail_count);
  132. unsigned int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
  133. int ret;
  134. if (!revokes)
  135. return;
  136. while (revokes > max_revokes)
  137. max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
  138. ret = gfs2_trans_begin(sdp, 0, max_revokes);
  139. if (ret)
  140. return;
  141. __gfs2_ail_flush(gl, fsync, max_revokes);
  142. gfs2_trans_end(sdp);
  143. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  144. GFS2_LFC_AIL_FLUSH);
  145. }
  146. /**
  147. * gfs2_rgrp_metasync - sync out the metadata of a resource group
  148. * @gl: the glock protecting the resource group
  149. *
  150. */
  151. static int gfs2_rgrp_metasync(struct gfs2_glock *gl)
  152. {
  153. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  154. struct address_space *metamapping = &sdp->sd_aspace;
  155. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  156. const unsigned bsize = sdp->sd_sb.sb_bsize;
  157. loff_t start = (rgd->rd_addr * bsize) & PAGE_MASK;
  158. loff_t end = PAGE_ALIGN((rgd->rd_addr + rgd->rd_length) * bsize) - 1;
  159. int error;
  160. filemap_fdatawrite_range(metamapping, start, end);
  161. error = filemap_fdatawait_range(metamapping, start, end);
  162. WARN_ON_ONCE(error && !gfs2_withdrawn(sdp));
  163. mapping_set_error(metamapping, error);
  164. if (error)
  165. gfs2_io_error(sdp);
  166. return error;
  167. }
  168. /**
  169. * rgrp_go_sync - sync out the metadata for this glock
  170. * @gl: the glock
  171. *
  172. * Called when demoting or unlocking an EX glock. We must flush
  173. * to disk all dirty buffers/pages relating to this glock, and must not
  174. * return to caller to demote/unlock the glock until I/O is complete.
  175. */
  176. static int rgrp_go_sync(struct gfs2_glock *gl)
  177. {
  178. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  179. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  180. int error;
  181. if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
  182. return 0;
  183. GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
  184. gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  185. GFS2_LFC_RGRP_GO_SYNC);
  186. error = gfs2_rgrp_metasync(gl);
  187. if (!error)
  188. error = gfs2_ail_empty_gl(gl);
  189. gfs2_free_clones(rgd);
  190. return error;
  191. }
  192. /**
  193. * rgrp_go_inval - invalidate the metadata for this glock
  194. * @gl: the glock
  195. * @flags:
  196. *
  197. * We never used LM_ST_DEFERRED with resource groups, so that we
  198. * should always see the metadata flag set here.
  199. *
  200. */
  201. static void rgrp_go_inval(struct gfs2_glock *gl, int flags)
  202. {
  203. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  204. struct address_space *mapping = &sdp->sd_aspace;
  205. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  206. const unsigned bsize = sdp->sd_sb.sb_bsize;
  207. loff_t start = (rgd->rd_addr * bsize) & PAGE_MASK;
  208. loff_t end = PAGE_ALIGN((rgd->rd_addr + rgd->rd_length) * bsize) - 1;
  209. gfs2_rgrp_brelse(rgd);
  210. WARN_ON_ONCE(!(flags & DIO_METADATA));
  211. truncate_inode_pages_range(mapping, start, end);
  212. rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
  213. }
  214. static void gfs2_rgrp_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
  215. const char *fs_id_buf)
  216. {
  217. struct gfs2_rgrpd *rgd = gl->gl_object;
  218. if (rgd)
  219. gfs2_rgrp_dump(seq, rgd, fs_id_buf);
  220. }
  221. static struct gfs2_inode *gfs2_glock2inode(struct gfs2_glock *gl)
  222. {
  223. struct gfs2_inode *ip;
  224. spin_lock(&gl->gl_lockref.lock);
  225. ip = gl->gl_object;
  226. if (ip)
  227. set_bit(GIF_GLOP_PENDING, &ip->i_flags);
  228. spin_unlock(&gl->gl_lockref.lock);
  229. return ip;
  230. }
  231. struct gfs2_rgrpd *gfs2_glock2rgrp(struct gfs2_glock *gl)
  232. {
  233. struct gfs2_rgrpd *rgd;
  234. spin_lock(&gl->gl_lockref.lock);
  235. rgd = gl->gl_object;
  236. spin_unlock(&gl->gl_lockref.lock);
  237. return rgd;
  238. }
  239. static void gfs2_clear_glop_pending(struct gfs2_inode *ip)
  240. {
  241. if (!ip)
  242. return;
  243. clear_bit_unlock(GIF_GLOP_PENDING, &ip->i_flags);
  244. wake_up_bit(&ip->i_flags, GIF_GLOP_PENDING);
  245. }
  246. /**
  247. * gfs2_inode_metasync - sync out the metadata of an inode
  248. * @gl: the glock protecting the inode
  249. *
  250. */
  251. int gfs2_inode_metasync(struct gfs2_glock *gl)
  252. {
  253. struct address_space *metamapping = gfs2_glock2aspace(gl);
  254. int error;
  255. filemap_fdatawrite(metamapping);
  256. error = filemap_fdatawait(metamapping);
  257. if (error)
  258. gfs2_io_error(gl->gl_name.ln_sbd);
  259. return error;
  260. }
  261. /**
  262. * inode_go_sync - Sync the dirty metadata of an inode
  263. * @gl: the glock protecting the inode
  264. *
  265. */
  266. static int inode_go_sync(struct gfs2_glock *gl)
  267. {
  268. struct gfs2_inode *ip = gfs2_glock2inode(gl);
  269. int isreg = ip && S_ISREG(ip->i_inode.i_mode);
  270. struct address_space *metamapping = gfs2_glock2aspace(gl);
  271. int error = 0, ret;
  272. if (isreg) {
  273. if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
  274. unmap_shared_mapping_range(ip->i_inode.i_mapping, 0, 0);
  275. inode_dio_wait(&ip->i_inode);
  276. }
  277. if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
  278. goto out;
  279. GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
  280. gfs2_log_flush(gl->gl_name.ln_sbd, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  281. GFS2_LFC_INODE_GO_SYNC);
  282. filemap_fdatawrite(metamapping);
  283. if (isreg) {
  284. struct address_space *mapping = ip->i_inode.i_mapping;
  285. filemap_fdatawrite(mapping);
  286. error = filemap_fdatawait(mapping);
  287. mapping_set_error(mapping, error);
  288. }
  289. ret = gfs2_inode_metasync(gl);
  290. if (!error)
  291. error = ret;
  292. gfs2_ail_empty_gl(gl);
  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(GIF_INVALID, &ip->i_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. /**
  337. * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
  338. * @gl: the glock
  339. *
  340. * Returns: 1 if it's ok
  341. */
  342. static int inode_go_demote_ok(const struct gfs2_glock *gl)
  343. {
  344. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  345. if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
  346. return 0;
  347. return 1;
  348. }
  349. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  350. {
  351. const struct gfs2_dinode *str = buf;
  352. struct timespec64 atime;
  353. u16 height, depth;
  354. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
  355. goto corrupt;
  356. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  357. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  358. ip->i_inode.i_rdev = 0;
  359. switch (ip->i_inode.i_mode & S_IFMT) {
  360. case S_IFBLK:
  361. case S_IFCHR:
  362. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  363. be32_to_cpu(str->di_minor));
  364. break;
  365. }
  366. i_uid_write(&ip->i_inode, be32_to_cpu(str->di_uid));
  367. i_gid_write(&ip->i_inode, be32_to_cpu(str->di_gid));
  368. set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
  369. i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
  370. gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
  371. atime.tv_sec = be64_to_cpu(str->di_atime);
  372. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  373. if (timespec64_compare(&ip->i_inode.i_atime, &atime) < 0)
  374. ip->i_inode.i_atime = atime;
  375. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  376. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  377. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  378. ip->i_inode.i_ctime.tv_nsec = 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(&ip->i_inode);
  385. height = be16_to_cpu(str->di_height);
  386. if (unlikely(height > GFS2_MAX_META_HEIGHT))
  387. goto corrupt;
  388. ip->i_height = (u8)height;
  389. depth = be16_to_cpu(str->di_depth);
  390. if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
  391. goto corrupt;
  392. ip->i_depth = (u8)depth;
  393. ip->i_entries = be32_to_cpu(str->di_entries);
  394. if (S_ISREG(ip->i_inode.i_mode))
  395. gfs2_set_aops(&ip->i_inode);
  396. return 0;
  397. corrupt:
  398. gfs2_consist_inode(ip);
  399. return -EIO;
  400. }
  401. /**
  402. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  403. * @ip: The GFS2 inode
  404. *
  405. * Returns: errno
  406. */
  407. int gfs2_inode_refresh(struct gfs2_inode *ip)
  408. {
  409. struct buffer_head *dibh;
  410. int error;
  411. error = gfs2_meta_inode_buffer(ip, &dibh);
  412. if (error)
  413. return error;
  414. error = gfs2_dinode_in(ip, dibh->b_data);
  415. brelse(dibh);
  416. clear_bit(GIF_INVALID, &ip->i_flags);
  417. return error;
  418. }
  419. /**
  420. * inode_go_lock - operation done after an inode lock is locked by a process
  421. * @gl: the glock
  422. * @flags:
  423. *
  424. * Returns: errno
  425. */
  426. static int inode_go_lock(struct gfs2_holder *gh)
  427. {
  428. struct gfs2_glock *gl = gh->gh_gl;
  429. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  430. struct gfs2_inode *ip = gl->gl_object;
  431. int error = 0;
  432. if (!ip || (gh->gh_flags & GL_SKIP))
  433. return 0;
  434. if (test_bit(GIF_INVALID, &ip->i_flags)) {
  435. error = gfs2_inode_refresh(ip);
  436. if (error)
  437. return error;
  438. }
  439. if (gh->gh_state != LM_ST_DEFERRED)
  440. inode_dio_wait(&ip->i_inode);
  441. if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
  442. (gl->gl_state == LM_ST_EXCLUSIVE) &&
  443. (gh->gh_state == LM_ST_EXCLUSIVE)) {
  444. spin_lock(&sdp->sd_trunc_lock);
  445. if (list_empty(&ip->i_trunc_list))
  446. list_add(&ip->i_trunc_list, &sdp->sd_trunc_list);
  447. spin_unlock(&sdp->sd_trunc_lock);
  448. wake_up(&sdp->sd_quota_wait);
  449. return 1;
  450. }
  451. return error;
  452. }
  453. /**
  454. * inode_go_dump - print information about an inode
  455. * @seq: The iterator
  456. * @ip: the inode
  457. * @fs_id_buf: file system id (may be empty)
  458. *
  459. */
  460. static void inode_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
  461. const char *fs_id_buf)
  462. {
  463. struct gfs2_inode *ip = gl->gl_object;
  464. struct inode *inode = &ip->i_inode;
  465. unsigned long nrpages;
  466. if (ip == NULL)
  467. return;
  468. xa_lock_irq(&inode->i_data.i_pages);
  469. nrpages = inode->i_data.nrpages;
  470. xa_unlock_irq(&inode->i_data.i_pages);
  471. gfs2_print_dbg(seq, "%s I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu "
  472. "p:%lu\n", fs_id_buf,
  473. (unsigned long long)ip->i_no_formal_ino,
  474. (unsigned long long)ip->i_no_addr,
  475. IF2DT(ip->i_inode.i_mode), ip->i_flags,
  476. (unsigned int)ip->i_diskflags,
  477. (unsigned long long)i_size_read(inode), nrpages);
  478. }
  479. /**
  480. * freeze_go_sync - promote/demote the freeze glock
  481. * @gl: the glock
  482. * @state: the requested state
  483. * @flags:
  484. *
  485. */
  486. static int freeze_go_sync(struct gfs2_glock *gl)
  487. {
  488. int error = 0;
  489. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  490. /*
  491. * We need to check gl_state == LM_ST_SHARED here and not gl_req ==
  492. * LM_ST_EXCLUSIVE. That's because when any node does a freeze,
  493. * all the nodes should have the freeze glock in SH mode and they all
  494. * call do_xmote: One for EX and the others for UN. They ALL must
  495. * freeze locally, and they ALL must queue freeze work. The freeze_work
  496. * calls freeze_func, which tries to reacquire the freeze glock in SH,
  497. * effectively waiting for the thaw on the node who holds it in EX.
  498. * Once thawed, the work func acquires the freeze glock in
  499. * SH and everybody goes back to thawed.
  500. */
  501. if (gl->gl_state == LM_ST_SHARED && !gfs2_withdrawn(sdp) &&
  502. !test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
  503. atomic_set(&sdp->sd_freeze_state, SFS_STARTING_FREEZE);
  504. error = freeze_super(sdp->sd_vfs);
  505. if (error) {
  506. fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
  507. error);
  508. if (gfs2_withdrawn(sdp)) {
  509. atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
  510. return 0;
  511. }
  512. gfs2_assert_withdraw(sdp, 0);
  513. }
  514. queue_work(gfs2_freeze_wq, &sdp->sd_freeze_work);
  515. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  516. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
  517. GFS2_LFC_FREEZE_GO_SYNC);
  518. else /* read-only mounts */
  519. atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
  520. }
  521. return 0;
  522. }
  523. /**
  524. * freeze_go_xmote_bh - After promoting/demoting the freeze glock
  525. * @gl: the glock
  526. *
  527. */
  528. static int freeze_go_xmote_bh(struct gfs2_glock *gl, struct gfs2_holder *gh)
  529. {
  530. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  531. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  532. struct gfs2_glock *j_gl = ip->i_gl;
  533. struct gfs2_log_header_host head;
  534. int error;
  535. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  536. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  537. error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
  538. if (gfs2_assert_withdraw_delayed(sdp, !error))
  539. return error;
  540. if (gfs2_assert_withdraw_delayed(sdp, head.lh_flags &
  541. GFS2_LOG_HEAD_UNMOUNT))
  542. return -EIO;
  543. sdp->sd_log_sequence = head.lh_sequence + 1;
  544. gfs2_log_pointers_init(sdp, head.lh_blkno);
  545. }
  546. return 0;
  547. }
  548. /**
  549. * trans_go_demote_ok
  550. * @gl: the glock
  551. *
  552. * Always returns 0
  553. */
  554. static int freeze_go_demote_ok(const struct gfs2_glock *gl)
  555. {
  556. return 0;
  557. }
  558. /**
  559. * iopen_go_callback - schedule the dcache entry for the inode to be deleted
  560. * @gl: the glock
  561. *
  562. * gl_lockref.lock lock is held while calling this
  563. */
  564. static void iopen_go_callback(struct gfs2_glock *gl, bool remote)
  565. {
  566. struct gfs2_inode *ip = gl->gl_object;
  567. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  568. if (!remote || sb_rdonly(sdp->sd_vfs))
  569. return;
  570. if (gl->gl_demote_state == LM_ST_UNLOCKED &&
  571. gl->gl_state == LM_ST_SHARED && ip) {
  572. gl->gl_lockref.count++;
  573. if (!queue_delayed_work(gfs2_delete_workqueue,
  574. &gl->gl_delete, 0))
  575. gl->gl_lockref.count--;
  576. }
  577. }
  578. static int iopen_go_demote_ok(const struct gfs2_glock *gl)
  579. {
  580. return !gfs2_delete_work_queued(gl);
  581. }
  582. /**
  583. * inode_go_free - wake up anyone waiting for dlm's unlock ast to free it
  584. * @gl: glock being freed
  585. *
  586. * For now, this is only used for the journal inode glock. In withdraw
  587. * situations, we need to wait for the glock to be freed so that we know
  588. * other nodes may proceed with recovery / journal replay.
  589. */
  590. static void inode_go_free(struct gfs2_glock *gl)
  591. {
  592. /* Note that we cannot reference gl_object because it's already set
  593. * to NULL by this point in its lifecycle. */
  594. if (!test_bit(GLF_FREEING, &gl->gl_flags))
  595. return;
  596. clear_bit_unlock(GLF_FREEING, &gl->gl_flags);
  597. wake_up_bit(&gl->gl_flags, GLF_FREEING);
  598. }
  599. /**
  600. * nondisk_go_callback - used to signal when a node did a withdraw
  601. * @gl: the nondisk glock
  602. * @remote: true if this came from a different cluster node
  603. *
  604. */
  605. static void nondisk_go_callback(struct gfs2_glock *gl, bool remote)
  606. {
  607. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  608. /* Ignore the callback unless it's from another node, and it's the
  609. live lock. */
  610. if (!remote || gl->gl_name.ln_number != GFS2_LIVE_LOCK)
  611. return;
  612. /* First order of business is to cancel the demote request. We don't
  613. * really want to demote a nondisk glock. At best it's just to inform
  614. * us of another node's withdraw. We'll keep it in SH mode. */
  615. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  616. clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
  617. /* Ignore the unlock if we're withdrawn, unmounting, or in recovery. */
  618. if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) ||
  619. test_bit(SDF_WITHDRAWN, &sdp->sd_flags) ||
  620. test_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags))
  621. return;
  622. /* We only care when a node wants us to unlock, because that means
  623. * they want a journal recovered. */
  624. if (gl->gl_demote_state != LM_ST_UNLOCKED)
  625. return;
  626. if (sdp->sd_args.ar_spectator) {
  627. fs_warn(sdp, "Spectator node cannot recover journals.\n");
  628. return;
  629. }
  630. fs_warn(sdp, "Some node has withdrawn; checking for recovery.\n");
  631. set_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags);
  632. /*
  633. * We can't call remote_withdraw directly here or gfs2_recover_journal
  634. * because this is called from the glock unlock function and the
  635. * remote_withdraw needs to enqueue and dequeue the same "live" glock
  636. * we were called from. So we queue it to the control work queue in
  637. * lock_dlm.
  638. */
  639. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
  640. }
  641. const struct gfs2_glock_operations gfs2_meta_glops = {
  642. .go_type = LM_TYPE_META,
  643. .go_flags = GLOF_NONDISK,
  644. };
  645. const struct gfs2_glock_operations gfs2_inode_glops = {
  646. .go_sync = inode_go_sync,
  647. .go_inval = inode_go_inval,
  648. .go_demote_ok = inode_go_demote_ok,
  649. .go_lock = inode_go_lock,
  650. .go_dump = inode_go_dump,
  651. .go_type = LM_TYPE_INODE,
  652. .go_flags = GLOF_ASPACE | GLOF_LRU | GLOF_LVB,
  653. .go_free = inode_go_free,
  654. };
  655. const struct gfs2_glock_operations gfs2_rgrp_glops = {
  656. .go_sync = rgrp_go_sync,
  657. .go_inval = rgrp_go_inval,
  658. .go_lock = gfs2_rgrp_go_lock,
  659. .go_dump = gfs2_rgrp_go_dump,
  660. .go_type = LM_TYPE_RGRP,
  661. .go_flags = GLOF_LVB,
  662. };
  663. const struct gfs2_glock_operations gfs2_freeze_glops = {
  664. .go_sync = freeze_go_sync,
  665. .go_xmote_bh = freeze_go_xmote_bh,
  666. .go_demote_ok = freeze_go_demote_ok,
  667. .go_type = LM_TYPE_NONDISK,
  668. .go_flags = GLOF_NONDISK,
  669. };
  670. const struct gfs2_glock_operations gfs2_iopen_glops = {
  671. .go_type = LM_TYPE_IOPEN,
  672. .go_callback = iopen_go_callback,
  673. .go_demote_ok = iopen_go_demote_ok,
  674. .go_flags = GLOF_LRU | GLOF_NONDISK,
  675. .go_subclass = 1,
  676. };
  677. const struct gfs2_glock_operations gfs2_flock_glops = {
  678. .go_type = LM_TYPE_FLOCK,
  679. .go_flags = GLOF_LRU | GLOF_NONDISK,
  680. };
  681. const struct gfs2_glock_operations gfs2_nondisk_glops = {
  682. .go_type = LM_TYPE_NONDISK,
  683. .go_flags = GLOF_NONDISK,
  684. .go_callback = nondisk_go_callback,
  685. };
  686. const struct gfs2_glock_operations gfs2_quota_glops = {
  687. .go_type = LM_TYPE_QUOTA,
  688. .go_flags = GLOF_LVB | GLOF_LRU | GLOF_NONDISK,
  689. };
  690. const struct gfs2_glock_operations gfs2_journal_glops = {
  691. .go_type = LM_TYPE_JOURNAL,
  692. .go_flags = GLOF_NONDISK,
  693. };
  694. const struct gfs2_glock_operations *gfs2_glops_list[] = {
  695. [LM_TYPE_META] = &gfs2_meta_glops,
  696. [LM_TYPE_INODE] = &gfs2_inode_glops,
  697. [LM_TYPE_RGRP] = &gfs2_rgrp_glops,
  698. [LM_TYPE_IOPEN] = &gfs2_iopen_glops,
  699. [LM_TYPE_FLOCK] = &gfs2_flock_glops,
  700. [LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
  701. [LM_TYPE_QUOTA] = &gfs2_quota_glops,
  702. [LM_TYPE_JOURNAL] = &gfs2_journal_glops,
  703. };