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 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 "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. clear_bit(GLF_SYNC, &gl->gl_flags);
  115. }
  116. /**
  117. * meta_go_inval - invalidate the metadata for this glock
  118. * @gl: the glock
  119. * @flags:
  120. *
  121. */
  122. static void meta_go_inval(struct gfs2_glock *gl, int flags)
  123. {
  124. if (!(flags & DIO_METADATA))
  125. return;
  126. gfs2_meta_inval(gl);
  127. gl->gl_vn++;
  128. }
  129. /**
  130. * inode_go_xmote_th - promote/demote a glock
  131. * @gl: the glock
  132. * @state: the requested state
  133. * @flags:
  134. *
  135. */
  136. static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
  137. int flags)
  138. {
  139. if (gl->gl_state != LM_ST_UNLOCKED)
  140. gfs2_pte_inval(gl);
  141. gfs2_glock_xmote_th(gl, state, flags);
  142. }
  143. /**
  144. * inode_go_xmote_bh - After promoting/demoting a glock
  145. * @gl: the glock
  146. *
  147. */
  148. static void inode_go_xmote_bh(struct gfs2_glock *gl)
  149. {
  150. struct gfs2_holder *gh = gl->gl_req_gh;
  151. struct buffer_head *bh;
  152. int error;
  153. if (gl->gl_state != LM_ST_UNLOCKED &&
  154. (!gh || !(gh->gh_flags & GL_SKIP))) {
  155. error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
  156. &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_sync(gl, flags | DIO_START);
  187. gfs2_log_flush(gl->gl_sbd, gl);
  188. gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
  189. gfs2_page_sync(gl, flags | DIO_WAIT);
  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, flags | DIO_START | DIO_WAIT);
  194. } else if (data)
  195. gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
  196. if (flags & DIO_RELEASE)
  197. gfs2_ail_empty_gl(gl);
  198. }
  199. clear_bit(GLF_SYNC, &gl->gl_flags);
  200. }
  201. /**
  202. * inode_go_inval - prepare a inode glock to be released
  203. * @gl: the glock
  204. * @flags:
  205. *
  206. */
  207. static void inode_go_inval(struct gfs2_glock *gl, int flags)
  208. {
  209. int meta = (flags & DIO_METADATA);
  210. int data = (flags & DIO_DATA);
  211. if (meta) {
  212. gfs2_meta_inval(gl);
  213. gl->gl_vn++;
  214. }
  215. if (data)
  216. gfs2_page_inval(gl);
  217. }
  218. /**
  219. * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
  220. * @gl: the glock
  221. *
  222. * Returns: 1 if it's ok
  223. */
  224. static int inode_go_demote_ok(struct gfs2_glock *gl)
  225. {
  226. struct gfs2_sbd *sdp = gl->gl_sbd;
  227. int demote = 0;
  228. if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
  229. demote = 1;
  230. else if (!sdp->sd_args.ar_localcaching &&
  231. time_after_eq(jiffies, gl->gl_stamp +
  232. gfs2_tune_get(sdp, gt_demote_secs) * HZ))
  233. demote = 1;
  234. return demote;
  235. }
  236. /**
  237. * inode_go_lock - operation done after an inode lock is locked by a process
  238. * @gl: the glock
  239. * @flags:
  240. *
  241. * Returns: errno
  242. */
  243. static int inode_go_lock(struct gfs2_holder *gh)
  244. {
  245. struct gfs2_glock *gl = gh->gh_gl;
  246. struct gfs2_inode *ip = gl->gl_object;
  247. int error = 0;
  248. if (!ip)
  249. return 0;
  250. if (ip->i_vn != gl->gl_vn) {
  251. error = gfs2_inode_refresh(ip);
  252. if (error)
  253. return error;
  254. gfs2_inode_attr_in(ip);
  255. }
  256. if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
  257. (gl->gl_state == LM_ST_EXCLUSIVE) &&
  258. (gh->gh_flags & GL_LOCAL_EXCL))
  259. error = gfs2_truncatei_resume(ip);
  260. return error;
  261. }
  262. /**
  263. * inode_go_unlock - operation done before an inode lock is unlocked by a
  264. * process
  265. * @gl: the glock
  266. * @flags:
  267. *
  268. */
  269. static void inode_go_unlock(struct gfs2_holder *gh)
  270. {
  271. struct gfs2_glock *gl = gh->gh_gl;
  272. struct gfs2_inode *ip = gl->gl_object;
  273. if (ip) {
  274. if (test_bit(GLF_DIRTY, &gl->gl_flags))
  275. gfs2_inode_attr_in(ip);
  276. gfs2_meta_cache_flush(ip);
  277. }
  278. }
  279. /**
  280. * inode_greedy -
  281. * @gl: the glock
  282. *
  283. */
  284. static void inode_greedy(struct gfs2_glock *gl)
  285. {
  286. struct gfs2_sbd *sdp = gl->gl_sbd;
  287. struct gfs2_inode *ip = gl->gl_object;
  288. unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
  289. unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
  290. unsigned int new_time;
  291. spin_lock(&ip->i_spin);
  292. if (time_after(ip->i_last_pfault + quantum, jiffies)) {
  293. new_time = ip->i_greedy + quantum;
  294. if (new_time > max)
  295. new_time = max;
  296. } else {
  297. new_time = ip->i_greedy - quantum;
  298. if (!new_time || new_time > max)
  299. new_time = 1;
  300. }
  301. ip->i_greedy = new_time;
  302. spin_unlock(&ip->i_spin);
  303. iput(&ip->i_inode);
  304. }
  305. /**
  306. * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
  307. * @gl: the glock
  308. *
  309. * Returns: 1 if it's ok
  310. */
  311. static int rgrp_go_demote_ok(struct gfs2_glock *gl)
  312. {
  313. return !gl->gl_aspace->i_mapping->nrpages;
  314. }
  315. /**
  316. * rgrp_go_lock - operation done after an rgrp lock is locked by
  317. * a first holder on this node.
  318. * @gl: the glock
  319. * @flags:
  320. *
  321. * Returns: errno
  322. */
  323. static int rgrp_go_lock(struct gfs2_holder *gh)
  324. {
  325. return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
  326. }
  327. /**
  328. * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
  329. * a last holder on this node.
  330. * @gl: the glock
  331. * @flags:
  332. *
  333. */
  334. static void rgrp_go_unlock(struct gfs2_holder *gh)
  335. {
  336. gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
  337. }
  338. /**
  339. * trans_go_xmote_th - promote/demote the transaction glock
  340. * @gl: the glock
  341. * @state: the requested state
  342. * @flags:
  343. *
  344. */
  345. static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
  346. int flags)
  347. {
  348. struct gfs2_sbd *sdp = gl->gl_sbd;
  349. if (gl->gl_state != LM_ST_UNLOCKED &&
  350. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  351. gfs2_meta_syncfs(sdp);
  352. gfs2_log_shutdown(sdp);
  353. }
  354. gfs2_glock_xmote_th(gl, state, flags);
  355. }
  356. /**
  357. * trans_go_xmote_bh - After promoting/demoting the transaction glock
  358. * @gl: the glock
  359. *
  360. */
  361. static void trans_go_xmote_bh(struct gfs2_glock *gl)
  362. {
  363. struct gfs2_sbd *sdp = gl->gl_sbd;
  364. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  365. struct gfs2_glock *j_gl = ip->i_gl;
  366. struct gfs2_log_header head;
  367. int error;
  368. if (gl->gl_state != LM_ST_UNLOCKED &&
  369. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  370. gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
  371. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
  372. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  373. if (error)
  374. gfs2_consist(sdp);
  375. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
  376. gfs2_consist(sdp);
  377. /* Initialize some head of the log stuff */
  378. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
  379. sdp->sd_log_sequence = head.lh_sequence + 1;
  380. gfs2_log_pointers_init(sdp, head.lh_blkno);
  381. }
  382. }
  383. }
  384. /**
  385. * trans_go_drop_th - unlock the transaction glock
  386. * @gl: the glock
  387. *
  388. * We want to sync the device even with localcaching. Remember
  389. * that localcaching journal replay only marks buffers dirty.
  390. */
  391. static void trans_go_drop_th(struct gfs2_glock *gl)
  392. {
  393. struct gfs2_sbd *sdp = gl->gl_sbd;
  394. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  395. gfs2_meta_syncfs(sdp);
  396. gfs2_log_shutdown(sdp);
  397. }
  398. gfs2_glock_drop_th(gl);
  399. }
  400. /**
  401. * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
  402. * @gl: the glock
  403. *
  404. * Returns: 1 if it's ok
  405. */
  406. static int quota_go_demote_ok(struct gfs2_glock *gl)
  407. {
  408. return !atomic_read(&gl->gl_lvb_count);
  409. }
  410. struct gfs2_glock_operations gfs2_meta_glops = {
  411. .go_xmote_th = gfs2_glock_xmote_th,
  412. .go_drop_th = gfs2_glock_drop_th,
  413. .go_type = LM_TYPE_META
  414. };
  415. struct gfs2_glock_operations gfs2_inode_glops = {
  416. .go_xmote_th = inode_go_xmote_th,
  417. .go_xmote_bh = inode_go_xmote_bh,
  418. .go_drop_th = inode_go_drop_th,
  419. .go_sync = inode_go_sync,
  420. .go_inval = inode_go_inval,
  421. .go_demote_ok = inode_go_demote_ok,
  422. .go_lock = inode_go_lock,
  423. .go_unlock = inode_go_unlock,
  424. .go_greedy = inode_greedy,
  425. .go_type = LM_TYPE_INODE
  426. };
  427. struct gfs2_glock_operations gfs2_rgrp_glops = {
  428. .go_xmote_th = gfs2_glock_xmote_th,
  429. .go_drop_th = gfs2_glock_drop_th,
  430. .go_sync = meta_go_sync,
  431. .go_inval = meta_go_inval,
  432. .go_demote_ok = rgrp_go_demote_ok,
  433. .go_lock = rgrp_go_lock,
  434. .go_unlock = rgrp_go_unlock,
  435. .go_type = LM_TYPE_RGRP
  436. };
  437. struct gfs2_glock_operations gfs2_trans_glops = {
  438. .go_xmote_th = trans_go_xmote_th,
  439. .go_xmote_bh = trans_go_xmote_bh,
  440. .go_drop_th = trans_go_drop_th,
  441. .go_type = LM_TYPE_NONDISK
  442. };
  443. struct gfs2_glock_operations gfs2_iopen_glops = {
  444. .go_xmote_th = gfs2_glock_xmote_th,
  445. .go_drop_th = gfs2_glock_drop_th,
  446. .go_callback = gfs2_iopen_go_callback,
  447. .go_type = LM_TYPE_IOPEN
  448. };
  449. 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. 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. 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. 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. };