glops.c 11 KB

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