glops.c 12 KB

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