glops.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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 "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. unsigned int blocks;
  37. struct list_head *head = &gl->gl_ail_list;
  38. struct gfs2_bufdata *bd;
  39. struct buffer_head *bh;
  40. int error;
  41. blocks = atomic_read(&gl->gl_ail_count);
  42. if (!blocks)
  43. return;
  44. error = gfs2_trans_begin(sdp, 0, blocks);
  45. if (gfs2_assert_withdraw(sdp, !error))
  46. return;
  47. gfs2_log_lock(sdp);
  48. while (!list_empty(head)) {
  49. bd = list_entry(head->next, struct gfs2_bufdata,
  50. bd_ail_gl_list);
  51. bh = bd->bd_bh;
  52. gfs2_remove_from_ail(NULL, bd);
  53. bd->bd_bh = NULL;
  54. bh->b_private = NULL;
  55. bd->bd_blkno = bh->b_blocknr;
  56. gfs2_assert_withdraw(sdp, !buffer_busy(bh));
  57. gfs2_trans_add_revoke(sdp, bd);
  58. }
  59. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  60. gfs2_log_unlock(sdp);
  61. gfs2_trans_end(sdp);
  62. gfs2_log_flush(sdp, NULL);
  63. }
  64. /**
  65. * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
  66. * @gl: the glock
  67. *
  68. */
  69. static void gfs2_pte_inval(struct gfs2_glock *gl)
  70. {
  71. struct gfs2_inode *ip;
  72. struct inode *inode;
  73. ip = gl->gl_object;
  74. inode = &ip->i_inode;
  75. if (!ip || !S_ISREG(inode->i_mode))
  76. return;
  77. unmap_shared_mapping_range(inode->i_mapping, 0, 0);
  78. if (test_bit(GIF_SW_PAGED, &ip->i_flags))
  79. set_bit(GLF_DIRTY, &gl->gl_flags);
  80. }
  81. /**
  82. * meta_go_sync - sync out the metadata for this glock
  83. * @gl: the glock
  84. *
  85. * Called when demoting or unlocking an EX glock. We must flush
  86. * to disk all dirty buffers/pages relating to this glock, and must not
  87. * not return to caller to demote/unlock the glock until I/O is complete.
  88. */
  89. static void meta_go_sync(struct gfs2_glock *gl)
  90. {
  91. if (gl->gl_state != LM_ST_EXCLUSIVE)
  92. return;
  93. if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
  94. gfs2_log_flush(gl->gl_sbd, gl);
  95. gfs2_meta_sync(gl);
  96. gfs2_ail_empty_gl(gl);
  97. }
  98. }
  99. /**
  100. * meta_go_inval - invalidate the metadata for this glock
  101. * @gl: the glock
  102. * @flags:
  103. *
  104. */
  105. static void meta_go_inval(struct gfs2_glock *gl, int flags)
  106. {
  107. if (!(flags & DIO_METADATA))
  108. return;
  109. gfs2_meta_inval(gl);
  110. gl->gl_vn++;
  111. }
  112. /**
  113. * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
  114. * @gl: the glock protecting the inode
  115. *
  116. */
  117. static void inode_go_sync(struct gfs2_glock *gl)
  118. {
  119. struct gfs2_inode *ip = gl->gl_object;
  120. if (ip && !S_ISREG(ip->i_inode.i_mode))
  121. ip = NULL;
  122. if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
  123. if (ip && !gfs2_is_jdata(ip))
  124. filemap_fdatawrite(ip->i_inode.i_mapping);
  125. gfs2_log_flush(gl->gl_sbd, gl);
  126. if (ip && gfs2_is_jdata(ip))
  127. filemap_fdatawrite(ip->i_inode.i_mapping);
  128. gfs2_meta_sync(gl);
  129. if (ip) {
  130. struct address_space *mapping = ip->i_inode.i_mapping;
  131. int error = filemap_fdatawait(mapping);
  132. mapping_set_error(mapping, error);
  133. }
  134. clear_bit(GLF_DIRTY, &gl->gl_flags);
  135. gfs2_ail_empty_gl(gl);
  136. }
  137. }
  138. /**
  139. * inode_go_xmote_th - promote/demote a glock
  140. * @gl: the glock
  141. * @state: the requested state
  142. * @flags:
  143. *
  144. */
  145. static void inode_go_xmote_th(struct gfs2_glock *gl)
  146. {
  147. if (gl->gl_state != LM_ST_UNLOCKED)
  148. gfs2_pte_inval(gl);
  149. if (gl->gl_state == LM_ST_EXCLUSIVE)
  150. inode_go_sync(gl);
  151. }
  152. /**
  153. * inode_go_xmote_bh - After promoting/demoting a glock
  154. * @gl: the glock
  155. *
  156. */
  157. static void inode_go_xmote_bh(struct gfs2_glock *gl)
  158. {
  159. struct gfs2_holder *gh = gl->gl_req_gh;
  160. struct buffer_head *bh;
  161. int error;
  162. if (gl->gl_state != LM_ST_UNLOCKED &&
  163. (!gh || !(gh->gh_flags & GL_SKIP))) {
  164. error = gfs2_meta_read(gl, gl->gl_name.ln_number, 0, &bh);
  165. if (!error)
  166. brelse(bh);
  167. }
  168. }
  169. /**
  170. * inode_go_drop_th - unlock a glock
  171. * @gl: the glock
  172. *
  173. * Invoked from rq_demote().
  174. * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
  175. * is being purged from our node's glock cache; we're dropping lock.
  176. */
  177. static void inode_go_drop_th(struct gfs2_glock *gl)
  178. {
  179. gfs2_pte_inval(gl);
  180. if (gl->gl_state == LM_ST_EXCLUSIVE)
  181. inode_go_sync(gl);
  182. }
  183. /**
  184. * inode_go_inval - prepare a inode glock to be released
  185. * @gl: the glock
  186. * @flags:
  187. *
  188. */
  189. static void inode_go_inval(struct gfs2_glock *gl, int flags)
  190. {
  191. struct gfs2_inode *ip = gl->gl_object;
  192. int meta = (flags & DIO_METADATA);
  193. if (meta) {
  194. gfs2_meta_inval(gl);
  195. if (ip)
  196. set_bit(GIF_INVALID, &ip->i_flags);
  197. }
  198. if (ip && S_ISREG(ip->i_inode.i_mode))
  199. truncate_inode_pages(ip->i_inode.i_mapping, 0);
  200. }
  201. /**
  202. * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
  203. * @gl: the glock
  204. *
  205. * Returns: 1 if it's ok
  206. */
  207. static int inode_go_demote_ok(struct gfs2_glock *gl)
  208. {
  209. struct gfs2_sbd *sdp = gl->gl_sbd;
  210. int demote = 0;
  211. if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
  212. demote = 1;
  213. else if (!sdp->sd_args.ar_localcaching &&
  214. time_after_eq(jiffies, gl->gl_stamp +
  215. gfs2_tune_get(sdp, gt_demote_secs) * HZ))
  216. demote = 1;
  217. return demote;
  218. }
  219. /**
  220. * inode_go_lock - operation done after an inode lock is locked by a process
  221. * @gl: the glock
  222. * @flags:
  223. *
  224. * Returns: errno
  225. */
  226. static int inode_go_lock(struct gfs2_holder *gh)
  227. {
  228. struct gfs2_glock *gl = gh->gh_gl;
  229. struct gfs2_inode *ip = gl->gl_object;
  230. int error = 0;
  231. if (!ip)
  232. return 0;
  233. if (test_bit(GIF_INVALID, &ip->i_flags)) {
  234. error = gfs2_inode_refresh(ip);
  235. if (error)
  236. return error;
  237. }
  238. if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
  239. (gl->gl_state == LM_ST_EXCLUSIVE) &&
  240. (gh->gh_state == LM_ST_EXCLUSIVE))
  241. error = gfs2_truncatei_resume(ip);
  242. return error;
  243. }
  244. /**
  245. * inode_go_unlock - operation done before an inode lock is unlocked by a
  246. * process
  247. * @gl: the glock
  248. * @flags:
  249. *
  250. */
  251. static void inode_go_unlock(struct gfs2_holder *gh)
  252. {
  253. struct gfs2_glock *gl = gh->gh_gl;
  254. struct gfs2_inode *ip = gl->gl_object;
  255. if (ip)
  256. gfs2_meta_cache_flush(ip);
  257. }
  258. /**
  259. * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
  260. * @gl: the glock
  261. *
  262. * Returns: 1 if it's ok
  263. */
  264. static int rgrp_go_demote_ok(struct gfs2_glock *gl)
  265. {
  266. return !gl->gl_aspace->i_mapping->nrpages;
  267. }
  268. /**
  269. * rgrp_go_lock - operation done after an rgrp lock is locked by
  270. * a first holder on this node.
  271. * @gl: the glock
  272. * @flags:
  273. *
  274. * Returns: errno
  275. */
  276. static int rgrp_go_lock(struct gfs2_holder *gh)
  277. {
  278. return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
  279. }
  280. /**
  281. * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
  282. * a last holder on this node.
  283. * @gl: the glock
  284. * @flags:
  285. *
  286. */
  287. static void rgrp_go_unlock(struct gfs2_holder *gh)
  288. {
  289. gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
  290. }
  291. /**
  292. * trans_go_xmote_th - promote/demote the transaction glock
  293. * @gl: the glock
  294. * @state: the requested state
  295. * @flags:
  296. *
  297. */
  298. static void trans_go_xmote_th(struct gfs2_glock *gl)
  299. {
  300. struct gfs2_sbd *sdp = gl->gl_sbd;
  301. if (gl->gl_state != LM_ST_UNLOCKED &&
  302. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  303. gfs2_meta_syncfs(sdp);
  304. gfs2_log_shutdown(sdp);
  305. }
  306. }
  307. /**
  308. * trans_go_xmote_bh - After promoting/demoting the transaction glock
  309. * @gl: the glock
  310. *
  311. */
  312. static void trans_go_xmote_bh(struct gfs2_glock *gl)
  313. {
  314. struct gfs2_sbd *sdp = gl->gl_sbd;
  315. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  316. struct gfs2_glock *j_gl = ip->i_gl;
  317. struct gfs2_log_header_host head;
  318. int error;
  319. if (gl->gl_state != LM_ST_UNLOCKED &&
  320. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  321. gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
  322. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  323. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  324. if (error)
  325. gfs2_consist(sdp);
  326. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
  327. gfs2_consist(sdp);
  328. /* Initialize some head of the log stuff */
  329. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
  330. sdp->sd_log_sequence = head.lh_sequence + 1;
  331. gfs2_log_pointers_init(sdp, head.lh_blkno);
  332. }
  333. }
  334. }
  335. /**
  336. * trans_go_drop_th - unlock the transaction glock
  337. * @gl: the glock
  338. *
  339. * We want to sync the device even with localcaching. Remember
  340. * that localcaching journal replay only marks buffers dirty.
  341. */
  342. static void trans_go_drop_th(struct gfs2_glock *gl)
  343. {
  344. struct gfs2_sbd *sdp = gl->gl_sbd;
  345. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  346. gfs2_meta_syncfs(sdp);
  347. gfs2_log_shutdown(sdp);
  348. }
  349. }
  350. /**
  351. * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
  352. * @gl: the glock
  353. *
  354. * Returns: 1 if it's ok
  355. */
  356. static int quota_go_demote_ok(struct gfs2_glock *gl)
  357. {
  358. return !atomic_read(&gl->gl_lvb_count);
  359. }
  360. const struct gfs2_glock_operations gfs2_meta_glops = {
  361. .go_xmote_th = meta_go_sync,
  362. .go_drop_th = meta_go_sync,
  363. .go_type = LM_TYPE_META,
  364. };
  365. const struct gfs2_glock_operations gfs2_inode_glops = {
  366. .go_xmote_th = inode_go_xmote_th,
  367. .go_xmote_bh = inode_go_xmote_bh,
  368. .go_drop_th = inode_go_drop_th,
  369. .go_inval = inode_go_inval,
  370. .go_demote_ok = inode_go_demote_ok,
  371. .go_lock = inode_go_lock,
  372. .go_unlock = inode_go_unlock,
  373. .go_type = LM_TYPE_INODE,
  374. .go_min_hold_time = HZ / 10,
  375. };
  376. const struct gfs2_glock_operations gfs2_rgrp_glops = {
  377. .go_xmote_th = meta_go_sync,
  378. .go_drop_th = meta_go_sync,
  379. .go_inval = meta_go_inval,
  380. .go_demote_ok = rgrp_go_demote_ok,
  381. .go_lock = rgrp_go_lock,
  382. .go_unlock = rgrp_go_unlock,
  383. .go_type = LM_TYPE_RGRP,
  384. .go_min_hold_time = HZ / 10,
  385. };
  386. const struct gfs2_glock_operations gfs2_trans_glops = {
  387. .go_xmote_th = trans_go_xmote_th,
  388. .go_xmote_bh = trans_go_xmote_bh,
  389. .go_drop_th = trans_go_drop_th,
  390. .go_type = LM_TYPE_NONDISK,
  391. };
  392. const struct gfs2_glock_operations gfs2_iopen_glops = {
  393. .go_type = LM_TYPE_IOPEN,
  394. };
  395. const struct gfs2_glock_operations gfs2_flock_glops = {
  396. .go_type = LM_TYPE_FLOCK,
  397. };
  398. const struct gfs2_glock_operations gfs2_nondisk_glops = {
  399. .go_type = LM_TYPE_NONDISK,
  400. };
  401. const struct gfs2_glock_operations gfs2_quota_glops = {
  402. .go_demote_ok = quota_go_demote_ok,
  403. .go_type = LM_TYPE_QUOTA,
  404. };
  405. const struct gfs2_glock_operations gfs2_journal_glops = {
  406. .go_type = LM_TYPE_JOURNAL,
  407. };