glops.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include <linux/lm_interface.h>
  15. #include <linux/bio.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "bmap.h"
  19. #include "glock.h"
  20. #include "glops.h"
  21. #include "inode.h"
  22. #include "log.h"
  23. #include "meta_io.h"
  24. #include "recovery.h"
  25. #include "rgrp.h"
  26. #include "util.h"
  27. #include "trans.h"
  28. /**
  29. * ail_empty_gl - remove all buffers for a given lock from the AIL
  30. * @gl: the glock
  31. *
  32. * None of the buffers should be dirty, locked, or pinned.
  33. */
  34. static void gfs2_ail_empty_gl(struct gfs2_glock *gl)
  35. {
  36. struct gfs2_sbd *sdp = gl->gl_sbd;
  37. unsigned int blocks;
  38. struct list_head *head = &gl->gl_ail_list;
  39. struct gfs2_bufdata *bd;
  40. struct buffer_head *bh;
  41. int error;
  42. blocks = atomic_read(&gl->gl_ail_count);
  43. if (!blocks)
  44. return;
  45. error = gfs2_trans_begin(sdp, 0, blocks);
  46. if (gfs2_assert_withdraw(sdp, !error))
  47. return;
  48. gfs2_log_lock(sdp);
  49. while (!list_empty(head)) {
  50. bd = list_entry(head->next, struct gfs2_bufdata,
  51. bd_ail_gl_list);
  52. bh = bd->bd_bh;
  53. gfs2_remove_from_ail(bd);
  54. bd->bd_bh = NULL;
  55. bh->b_private = NULL;
  56. bd->bd_blkno = bh->b_blocknr;
  57. gfs2_assert_withdraw(sdp, !buffer_busy(bh));
  58. gfs2_trans_add_revoke(sdp, bd);
  59. }
  60. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  61. gfs2_log_unlock(sdp);
  62. gfs2_trans_end(sdp);
  63. gfs2_log_flush(sdp, NULL);
  64. }
  65. /**
  66. * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
  67. * @gl: the glock
  68. *
  69. */
  70. static void gfs2_pte_inval(struct gfs2_glock *gl)
  71. {
  72. struct gfs2_inode *ip;
  73. struct inode *inode;
  74. ip = gl->gl_object;
  75. inode = &ip->i_inode;
  76. if (!ip || !S_ISREG(inode->i_mode))
  77. return;
  78. unmap_shared_mapping_range(inode->i_mapping, 0, 0);
  79. if (test_bit(GIF_SW_PAGED, &ip->i_flags))
  80. set_bit(GLF_DIRTY, &gl->gl_flags);
  81. }
  82. /**
  83. * meta_go_sync - sync out the metadata for this glock
  84. * @gl: the glock
  85. *
  86. * Called when demoting or unlocking an EX glock. We must flush
  87. * to disk all dirty buffers/pages relating to this glock, and must not
  88. * not return to caller to demote/unlock the glock until I/O is complete.
  89. */
  90. static void meta_go_sync(struct gfs2_glock *gl)
  91. {
  92. if (gl->gl_state != LM_ST_EXCLUSIVE)
  93. return;
  94. if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
  95. gfs2_log_flush(gl->gl_sbd, gl);
  96. gfs2_meta_sync(gl);
  97. gfs2_ail_empty_gl(gl);
  98. }
  99. }
  100. /**
  101. * meta_go_inval - invalidate the metadata for this glock
  102. * @gl: the glock
  103. * @flags:
  104. *
  105. */
  106. static void meta_go_inval(struct gfs2_glock *gl, int flags)
  107. {
  108. if (!(flags & DIO_METADATA))
  109. return;
  110. gfs2_meta_inval(gl);
  111. if (gl->gl_object == GFS2_I(gl->gl_sbd->sd_rindex))
  112. gl->gl_sbd->sd_rindex_uptodate = 0;
  113. else if (gl->gl_ops == &gfs2_rgrp_glops && gl->gl_object) {
  114. struct gfs2_rgrpd *rgd = (struct gfs2_rgrpd *)gl->gl_object;
  115. rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
  116. }
  117. }
  118. /**
  119. * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
  120. * @gl: the glock protecting the inode
  121. *
  122. */
  123. static void inode_go_sync(struct gfs2_glock *gl)
  124. {
  125. struct gfs2_inode *ip = gl->gl_object;
  126. struct address_space *metamapping = gl->gl_aspace->i_mapping;
  127. int error;
  128. if (gl->gl_state != LM_ST_UNLOCKED)
  129. gfs2_pte_inval(gl);
  130. if (gl->gl_state != LM_ST_EXCLUSIVE)
  131. return;
  132. if (ip && !S_ISREG(ip->i_inode.i_mode))
  133. ip = NULL;
  134. if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
  135. gfs2_log_flush(gl->gl_sbd, gl);
  136. filemap_fdatawrite(metamapping);
  137. if (ip) {
  138. struct address_space *mapping = ip->i_inode.i_mapping;
  139. filemap_fdatawrite(mapping);
  140. error = filemap_fdatawait(mapping);
  141. mapping_set_error(mapping, error);
  142. }
  143. error = filemap_fdatawait(metamapping);
  144. mapping_set_error(metamapping, error);
  145. clear_bit(GLF_DIRTY, &gl->gl_flags);
  146. gfs2_ail_empty_gl(gl);
  147. }
  148. }
  149. /**
  150. * inode_go_inval - prepare a inode glock to be released
  151. * @gl: the glock
  152. * @flags:
  153. *
  154. */
  155. static void inode_go_inval(struct gfs2_glock *gl, int flags)
  156. {
  157. struct gfs2_inode *ip = gl->gl_object;
  158. int meta = (flags & DIO_METADATA);
  159. if (meta) {
  160. gfs2_meta_inval(gl);
  161. if (ip)
  162. set_bit(GIF_INVALID, &ip->i_flags);
  163. }
  164. if (ip && S_ISREG(ip->i_inode.i_mode))
  165. truncate_inode_pages(ip->i_inode.i_mapping, 0);
  166. }
  167. /**
  168. * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
  169. * @gl: the glock
  170. *
  171. * Returns: 1 if it's ok
  172. */
  173. static int inode_go_demote_ok(const struct gfs2_glock *gl)
  174. {
  175. struct gfs2_sbd *sdp = gl->gl_sbd;
  176. if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
  177. return 0;
  178. return 1;
  179. }
  180. /**
  181. * inode_go_lock - operation done after an inode lock is locked by a process
  182. * @gl: the glock
  183. * @flags:
  184. *
  185. * Returns: errno
  186. */
  187. static int inode_go_lock(struct gfs2_holder *gh)
  188. {
  189. struct gfs2_glock *gl = gh->gh_gl;
  190. struct gfs2_sbd *sdp = gl->gl_sbd;
  191. struct gfs2_inode *ip = gl->gl_object;
  192. int error = 0;
  193. if (!ip || (gh->gh_flags & GL_SKIP))
  194. return 0;
  195. if (test_bit(GIF_INVALID, &ip->i_flags)) {
  196. error = gfs2_inode_refresh(ip);
  197. if (error)
  198. return error;
  199. }
  200. if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
  201. (gl->gl_state == LM_ST_EXCLUSIVE) &&
  202. (gh->gh_state == LM_ST_EXCLUSIVE)) {
  203. spin_lock(&sdp->sd_trunc_lock);
  204. if (list_empty(&ip->i_trunc_list))
  205. list_add(&sdp->sd_trunc_list, &ip->i_trunc_list);
  206. spin_unlock(&sdp->sd_trunc_lock);
  207. wake_up(&sdp->sd_quota_wait);
  208. return 1;
  209. }
  210. return error;
  211. }
  212. /**
  213. * inode_go_dump - print information about an inode
  214. * @seq: The iterator
  215. * @ip: the inode
  216. *
  217. * Returns: 0 on success, -ENOBUFS when we run out of space
  218. */
  219. static int inode_go_dump(struct seq_file *seq, const struct gfs2_glock *gl)
  220. {
  221. const struct gfs2_inode *ip = gl->gl_object;
  222. if (ip == NULL)
  223. return 0;
  224. gfs2_print_dbg(seq, " I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu/%llu\n",
  225. (unsigned long long)ip->i_no_formal_ino,
  226. (unsigned long long)ip->i_no_addr,
  227. IF2DT(ip->i_inode.i_mode), ip->i_flags,
  228. (unsigned int)ip->i_diskflags,
  229. (unsigned long long)ip->i_inode.i_size,
  230. (unsigned long long)ip->i_disksize);
  231. return 0;
  232. }
  233. /**
  234. * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
  235. * @gl: the glock
  236. *
  237. * Returns: 1 if it's ok
  238. */
  239. static int rgrp_go_demote_ok(const struct gfs2_glock *gl)
  240. {
  241. return !gl->gl_aspace->i_mapping->nrpages;
  242. }
  243. /**
  244. * rgrp_go_lock - operation done after an rgrp lock is locked by
  245. * a first holder on this node.
  246. * @gl: the glock
  247. * @flags:
  248. *
  249. * Returns: errno
  250. */
  251. static int rgrp_go_lock(struct gfs2_holder *gh)
  252. {
  253. return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
  254. }
  255. /**
  256. * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
  257. * a last holder on this node.
  258. * @gl: the glock
  259. * @flags:
  260. *
  261. */
  262. static void rgrp_go_unlock(struct gfs2_holder *gh)
  263. {
  264. gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
  265. }
  266. /**
  267. * rgrp_go_dump - print out an rgrp
  268. * @seq: The iterator
  269. * @gl: The glock in question
  270. *
  271. */
  272. static int rgrp_go_dump(struct seq_file *seq, const struct gfs2_glock *gl)
  273. {
  274. const struct gfs2_rgrpd *rgd = gl->gl_object;
  275. if (rgd == NULL)
  276. return 0;
  277. gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
  278. (unsigned long long)rgd->rd_addr, rgd->rd_flags,
  279. rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
  280. return 0;
  281. }
  282. /**
  283. * trans_go_sync - promote/demote the transaction glock
  284. * @gl: the glock
  285. * @state: the requested state
  286. * @flags:
  287. *
  288. */
  289. static void trans_go_sync(struct gfs2_glock *gl)
  290. {
  291. struct gfs2_sbd *sdp = gl->gl_sbd;
  292. if (gl->gl_state != LM_ST_UNLOCKED &&
  293. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  294. gfs2_meta_syncfs(sdp);
  295. gfs2_log_shutdown(sdp);
  296. }
  297. }
  298. /**
  299. * trans_go_xmote_bh - After promoting/demoting the transaction glock
  300. * @gl: the glock
  301. *
  302. */
  303. static int trans_go_xmote_bh(struct gfs2_glock *gl, struct gfs2_holder *gh)
  304. {
  305. struct gfs2_sbd *sdp = gl->gl_sbd;
  306. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  307. struct gfs2_glock *j_gl = ip->i_gl;
  308. struct gfs2_log_header_host head;
  309. int error;
  310. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  311. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  312. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  313. if (error)
  314. gfs2_consist(sdp);
  315. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
  316. gfs2_consist(sdp);
  317. /* Initialize some head of the log stuff */
  318. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
  319. sdp->sd_log_sequence = head.lh_sequence + 1;
  320. gfs2_log_pointers_init(sdp, head.lh_blkno);
  321. }
  322. }
  323. return 0;
  324. }
  325. /**
  326. * trans_go_demote_ok
  327. * @gl: the glock
  328. *
  329. * Always returns 0
  330. */
  331. static int trans_go_demote_ok(const struct gfs2_glock *gl)
  332. {
  333. return 0;
  334. }
  335. /**
  336. * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
  337. * @gl: the glock
  338. *
  339. * Returns: 1 if it's ok
  340. */
  341. static int quota_go_demote_ok(const struct gfs2_glock *gl)
  342. {
  343. return !atomic_read(&gl->gl_lvb_count);
  344. }
  345. const struct gfs2_glock_operations gfs2_meta_glops = {
  346. .go_xmote_th = meta_go_sync,
  347. .go_type = LM_TYPE_META,
  348. };
  349. const struct gfs2_glock_operations gfs2_inode_glops = {
  350. .go_xmote_th = inode_go_sync,
  351. .go_inval = inode_go_inval,
  352. .go_demote_ok = inode_go_demote_ok,
  353. .go_lock = inode_go_lock,
  354. .go_dump = inode_go_dump,
  355. .go_type = LM_TYPE_INODE,
  356. .go_min_hold_time = HZ / 5,
  357. };
  358. const struct gfs2_glock_operations gfs2_rgrp_glops = {
  359. .go_xmote_th = meta_go_sync,
  360. .go_inval = meta_go_inval,
  361. .go_demote_ok = rgrp_go_demote_ok,
  362. .go_lock = rgrp_go_lock,
  363. .go_unlock = rgrp_go_unlock,
  364. .go_dump = rgrp_go_dump,
  365. .go_type = LM_TYPE_RGRP,
  366. .go_min_hold_time = HZ / 5,
  367. };
  368. const struct gfs2_glock_operations gfs2_trans_glops = {
  369. .go_xmote_th = trans_go_sync,
  370. .go_xmote_bh = trans_go_xmote_bh,
  371. .go_demote_ok = trans_go_demote_ok,
  372. .go_type = LM_TYPE_NONDISK,
  373. };
  374. const struct gfs2_glock_operations gfs2_iopen_glops = {
  375. .go_type = LM_TYPE_IOPEN,
  376. };
  377. const struct gfs2_glock_operations gfs2_flock_glops = {
  378. .go_type = LM_TYPE_FLOCK,
  379. };
  380. const struct gfs2_glock_operations gfs2_nondisk_glops = {
  381. .go_type = LM_TYPE_NONDISK,
  382. };
  383. const struct gfs2_glock_operations gfs2_quota_glops = {
  384. .go_demote_ok = quota_go_demote_ok,
  385. .go_type = LM_TYPE_QUOTA,
  386. };
  387. const struct gfs2_glock_operations gfs2_journal_glops = {
  388. .go_type = LM_TYPE_JOURNAL,
  389. };