glops.c 11 KB

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