glops.c 13 KB

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