glops.c 13 KB

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