glops.c 11 KB

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