glops.c 11 KB

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