glops.c 12 KB

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