glops.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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(ip->i_di.di_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(ip->i_di.di_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(ip->i_di.di_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(ip->i_di.di_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. gfs2_meta_inval(gl);
  257. gl->gl_vn++;
  258. }
  259. if (data)
  260. gfs2_page_inval(gl);
  261. }
  262. /**
  263. * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
  264. * @gl: the glock
  265. *
  266. * Returns: 1 if it's ok
  267. */
  268. static int inode_go_demote_ok(struct gfs2_glock *gl)
  269. {
  270. struct gfs2_sbd *sdp = gl->gl_sbd;
  271. int demote = 0;
  272. if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
  273. demote = 1;
  274. else if (!sdp->sd_args.ar_localcaching &&
  275. time_after_eq(jiffies, gl->gl_stamp +
  276. gfs2_tune_get(sdp, gt_demote_secs) * HZ))
  277. demote = 1;
  278. return demote;
  279. }
  280. /**
  281. * inode_go_lock - operation done after an inode lock is locked by a process
  282. * @gl: the glock
  283. * @flags:
  284. *
  285. * Returns: errno
  286. */
  287. static int inode_go_lock(struct gfs2_holder *gh)
  288. {
  289. struct gfs2_glock *gl = gh->gh_gl;
  290. struct gfs2_inode *ip = gl->gl_object;
  291. int error = 0;
  292. if (!ip)
  293. return 0;
  294. if (ip->i_vn != gl->gl_vn) {
  295. error = gfs2_inode_refresh(ip);
  296. if (error)
  297. return error;
  298. gfs2_inode_attr_in(ip);
  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 == NULL)
  318. return;
  319. if (test_bit(GLF_DIRTY, &gl->gl_flags))
  320. gfs2_inode_attr_in(ip);
  321. gfs2_meta_cache_flush(ip);
  322. }
  323. /**
  324. * inode_greedy -
  325. * @gl: the glock
  326. *
  327. */
  328. static void inode_greedy(struct gfs2_glock *gl)
  329. {
  330. struct gfs2_sbd *sdp = gl->gl_sbd;
  331. struct gfs2_inode *ip = gl->gl_object;
  332. unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
  333. unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
  334. unsigned int new_time;
  335. spin_lock(&ip->i_spin);
  336. if (time_after(ip->i_last_pfault + quantum, jiffies)) {
  337. new_time = ip->i_greedy + quantum;
  338. if (new_time > max)
  339. new_time = max;
  340. } else {
  341. new_time = ip->i_greedy - quantum;
  342. if (!new_time || new_time > max)
  343. new_time = 1;
  344. }
  345. ip->i_greedy = new_time;
  346. spin_unlock(&ip->i_spin);
  347. iput(&ip->i_inode);
  348. }
  349. /**
  350. * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
  351. * @gl: the glock
  352. *
  353. * Returns: 1 if it's ok
  354. */
  355. static int rgrp_go_demote_ok(struct gfs2_glock *gl)
  356. {
  357. return !gl->gl_aspace->i_mapping->nrpages;
  358. }
  359. /**
  360. * rgrp_go_lock - operation done after an rgrp lock is locked by
  361. * a first holder on this node.
  362. * @gl: the glock
  363. * @flags:
  364. *
  365. * Returns: errno
  366. */
  367. static int rgrp_go_lock(struct gfs2_holder *gh)
  368. {
  369. return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
  370. }
  371. /**
  372. * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
  373. * a last holder on this node.
  374. * @gl: the glock
  375. * @flags:
  376. *
  377. */
  378. static void rgrp_go_unlock(struct gfs2_holder *gh)
  379. {
  380. gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
  381. }
  382. /**
  383. * trans_go_xmote_th - promote/demote the transaction glock
  384. * @gl: the glock
  385. * @state: the requested state
  386. * @flags:
  387. *
  388. */
  389. static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
  390. int flags)
  391. {
  392. struct gfs2_sbd *sdp = gl->gl_sbd;
  393. if (gl->gl_state != LM_ST_UNLOCKED &&
  394. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  395. gfs2_meta_syncfs(sdp);
  396. gfs2_log_shutdown(sdp);
  397. }
  398. gfs2_glock_xmote_th(gl, state, flags);
  399. }
  400. /**
  401. * trans_go_xmote_bh - After promoting/demoting the transaction glock
  402. * @gl: the glock
  403. *
  404. */
  405. static void trans_go_xmote_bh(struct gfs2_glock *gl)
  406. {
  407. struct gfs2_sbd *sdp = gl->gl_sbd;
  408. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  409. struct gfs2_glock *j_gl = ip->i_gl;
  410. struct gfs2_log_header head;
  411. int error;
  412. if (gl->gl_state != LM_ST_UNLOCKED &&
  413. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  414. gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
  415. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
  416. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  417. if (error)
  418. gfs2_consist(sdp);
  419. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
  420. gfs2_consist(sdp);
  421. /* Initialize some head of the log stuff */
  422. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
  423. sdp->sd_log_sequence = head.lh_sequence + 1;
  424. gfs2_log_pointers_init(sdp, head.lh_blkno);
  425. }
  426. }
  427. }
  428. /**
  429. * trans_go_drop_th - unlock the transaction glock
  430. * @gl: the glock
  431. *
  432. * We want to sync the device even with localcaching. Remember
  433. * that localcaching journal replay only marks buffers dirty.
  434. */
  435. static void trans_go_drop_th(struct gfs2_glock *gl)
  436. {
  437. struct gfs2_sbd *sdp = gl->gl_sbd;
  438. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  439. gfs2_meta_syncfs(sdp);
  440. gfs2_log_shutdown(sdp);
  441. }
  442. gfs2_glock_drop_th(gl);
  443. }
  444. /**
  445. * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
  446. * @gl: the glock
  447. *
  448. * Returns: 1 if it's ok
  449. */
  450. static int quota_go_demote_ok(struct gfs2_glock *gl)
  451. {
  452. return !atomic_read(&gl->gl_lvb_count);
  453. }
  454. const struct gfs2_glock_operations gfs2_meta_glops = {
  455. .go_xmote_th = gfs2_glock_xmote_th,
  456. .go_drop_th = gfs2_glock_drop_th,
  457. .go_type = LM_TYPE_META,
  458. };
  459. const struct gfs2_glock_operations gfs2_inode_glops = {
  460. .go_xmote_th = inode_go_xmote_th,
  461. .go_xmote_bh = inode_go_xmote_bh,
  462. .go_drop_th = inode_go_drop_th,
  463. .go_sync = inode_go_sync,
  464. .go_inval = inode_go_inval,
  465. .go_demote_ok = inode_go_demote_ok,
  466. .go_lock = inode_go_lock,
  467. .go_unlock = inode_go_unlock,
  468. .go_greedy = inode_greedy,
  469. .go_type = LM_TYPE_INODE,
  470. };
  471. const struct gfs2_glock_operations gfs2_rgrp_glops = {
  472. .go_xmote_th = gfs2_glock_xmote_th,
  473. .go_drop_th = gfs2_glock_drop_th,
  474. .go_sync = meta_go_sync,
  475. .go_inval = meta_go_inval,
  476. .go_demote_ok = rgrp_go_demote_ok,
  477. .go_lock = rgrp_go_lock,
  478. .go_unlock = rgrp_go_unlock,
  479. .go_type = LM_TYPE_RGRP,
  480. };
  481. const struct gfs2_glock_operations gfs2_trans_glops = {
  482. .go_xmote_th = trans_go_xmote_th,
  483. .go_xmote_bh = trans_go_xmote_bh,
  484. .go_drop_th = trans_go_drop_th,
  485. .go_type = LM_TYPE_NONDISK,
  486. };
  487. const struct gfs2_glock_operations gfs2_iopen_glops = {
  488. .go_xmote_th = gfs2_glock_xmote_th,
  489. .go_drop_th = gfs2_glock_drop_th,
  490. .go_type = LM_TYPE_IOPEN,
  491. };
  492. const struct gfs2_glock_operations gfs2_flock_glops = {
  493. .go_xmote_th = gfs2_glock_xmote_th,
  494. .go_drop_th = gfs2_glock_drop_th,
  495. .go_type = LM_TYPE_FLOCK,
  496. };
  497. const struct gfs2_glock_operations gfs2_nondisk_glops = {
  498. .go_xmote_th = gfs2_glock_xmote_th,
  499. .go_drop_th = gfs2_glock_drop_th,
  500. .go_type = LM_TYPE_NONDISK,
  501. };
  502. const struct gfs2_glock_operations gfs2_quota_glops = {
  503. .go_xmote_th = gfs2_glock_xmote_th,
  504. .go_drop_th = gfs2_glock_drop_th,
  505. .go_demote_ok = quota_go_demote_ok,
  506. .go_type = LM_TYPE_QUOTA,
  507. };
  508. const struct gfs2_glock_operations gfs2_journal_glops = {
  509. .go_xmote_th = gfs2_glock_xmote_th,
  510. .go_drop_th = gfs2_glock_drop_th,
  511. .go_type = LM_TYPE_JOURNAL,
  512. };