glops.c 11 KB

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