meta_io.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 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/mm.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/writeback.h>
  17. #include <linux/swap.h>
  18. #include <linux/delay.h>
  19. #include <linux/bio.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include "gfs2.h"
  22. #include "incore.h"
  23. #include "glock.h"
  24. #include "glops.h"
  25. #include "inode.h"
  26. #include "log.h"
  27. #include "lops.h"
  28. #include "meta_io.h"
  29. #include "rgrp.h"
  30. #include "trans.h"
  31. #include "util.h"
  32. #include "trace_gfs2.h"
  33. static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
  34. {
  35. struct buffer_head *bh, *head;
  36. int nr_underway = 0;
  37. int write_op = REQ_META | REQ_PRIO |
  38. (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  39. BUG_ON(!PageLocked(page));
  40. BUG_ON(!page_has_buffers(page));
  41. head = page_buffers(page);
  42. bh = head;
  43. do {
  44. if (!buffer_mapped(bh))
  45. continue;
  46. /*
  47. * If it's a fully non-blocking write attempt and we cannot
  48. * lock the buffer then redirty the page. Note that this can
  49. * potentially cause a busy-wait loop from flusher thread and kswapd
  50. * activity, but those code paths have their own higher-level
  51. * throttling.
  52. */
  53. if (wbc->sync_mode != WB_SYNC_NONE) {
  54. lock_buffer(bh);
  55. } else if (!trylock_buffer(bh)) {
  56. redirty_page_for_writepage(wbc, page);
  57. continue;
  58. }
  59. if (test_clear_buffer_dirty(bh)) {
  60. mark_buffer_async_write(bh);
  61. } else {
  62. unlock_buffer(bh);
  63. }
  64. } while ((bh = bh->b_this_page) != head);
  65. /*
  66. * The page and its buffers are protected by PageWriteback(), so we can
  67. * drop the bh refcounts early.
  68. */
  69. BUG_ON(PageWriteback(page));
  70. set_page_writeback(page);
  71. do {
  72. struct buffer_head *next = bh->b_this_page;
  73. if (buffer_async_write(bh)) {
  74. submit_bh(write_op, bh);
  75. nr_underway++;
  76. }
  77. bh = next;
  78. } while (bh != head);
  79. unlock_page(page);
  80. if (nr_underway == 0)
  81. end_page_writeback(page);
  82. return 0;
  83. }
  84. const struct address_space_operations gfs2_meta_aops = {
  85. .writepage = gfs2_aspace_writepage,
  86. .releasepage = gfs2_releasepage,
  87. };
  88. /**
  89. * gfs2_getbuf - Get a buffer with a given address space
  90. * @gl: the glock
  91. * @blkno: the block number (filesystem scope)
  92. * @create: 1 if the buffer should be created
  93. *
  94. * Returns: the buffer
  95. */
  96. struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
  97. {
  98. struct address_space *mapping = gfs2_glock2aspace(gl);
  99. struct gfs2_sbd *sdp = gl->gl_sbd;
  100. struct page *page;
  101. struct buffer_head *bh;
  102. unsigned int shift;
  103. unsigned long index;
  104. unsigned int bufnum;
  105. shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  106. index = blkno >> shift; /* convert block to page */
  107. bufnum = blkno - (index << shift); /* block buf index within page */
  108. if (create) {
  109. for (;;) {
  110. page = grab_cache_page(mapping, index);
  111. if (page)
  112. break;
  113. yield();
  114. }
  115. } else {
  116. page = find_lock_page(mapping, index);
  117. if (!page)
  118. return NULL;
  119. }
  120. if (!page_has_buffers(page))
  121. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  122. /* Locate header for our buffer within our page */
  123. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  124. /* Do nothing */;
  125. get_bh(bh);
  126. if (!buffer_mapped(bh))
  127. map_bh(bh, sdp->sd_vfs, blkno);
  128. unlock_page(page);
  129. mark_page_accessed(page);
  130. page_cache_release(page);
  131. return bh;
  132. }
  133. static void meta_prep_new(struct buffer_head *bh)
  134. {
  135. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  136. lock_buffer(bh);
  137. clear_buffer_dirty(bh);
  138. set_buffer_uptodate(bh);
  139. unlock_buffer(bh);
  140. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  141. }
  142. /**
  143. * gfs2_meta_new - Get a block
  144. * @gl: The glock associated with this block
  145. * @blkno: The block number
  146. *
  147. * Returns: The buffer
  148. */
  149. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
  150. {
  151. struct buffer_head *bh;
  152. bh = gfs2_getbuf(gl, blkno, CREATE);
  153. meta_prep_new(bh);
  154. return bh;
  155. }
  156. /**
  157. * gfs2_meta_read - Read a block from disk
  158. * @gl: The glock covering the block
  159. * @blkno: The block number
  160. * @flags: flags
  161. * @bhp: the place where the buffer is returned (NULL on failure)
  162. *
  163. * Returns: errno
  164. */
  165. int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
  166. struct buffer_head **bhp)
  167. {
  168. struct gfs2_sbd *sdp = gl->gl_sbd;
  169. struct buffer_head *bh;
  170. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  171. *bhp = NULL;
  172. return -EIO;
  173. }
  174. *bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
  175. lock_buffer(bh);
  176. if (buffer_uptodate(bh)) {
  177. unlock_buffer(bh);
  178. return 0;
  179. }
  180. bh->b_end_io = end_buffer_read_sync;
  181. get_bh(bh);
  182. submit_bh(READ_SYNC | REQ_META | REQ_PRIO, bh);
  183. if (!(flags & DIO_WAIT))
  184. return 0;
  185. wait_on_buffer(bh);
  186. if (unlikely(!buffer_uptodate(bh))) {
  187. struct gfs2_trans *tr = current->journal_info;
  188. if (tr && tr->tr_touched)
  189. gfs2_io_error_bh(sdp, bh);
  190. brelse(bh);
  191. *bhp = NULL;
  192. return -EIO;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * gfs2_meta_wait - Reread a block from disk
  198. * @sdp: the filesystem
  199. * @bh: The block to wait for
  200. *
  201. * Returns: errno
  202. */
  203. int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
  204. {
  205. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  206. return -EIO;
  207. wait_on_buffer(bh);
  208. if (!buffer_uptodate(bh)) {
  209. struct gfs2_trans *tr = current->journal_info;
  210. if (tr && tr->tr_touched)
  211. gfs2_io_error_bh(sdp, bh);
  212. return -EIO;
  213. }
  214. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  215. return -EIO;
  216. return 0;
  217. }
  218. void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int meta)
  219. {
  220. struct address_space *mapping = bh->b_page->mapping;
  221. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  222. struct gfs2_bufdata *bd = bh->b_private;
  223. if (test_clear_buffer_pinned(bh)) {
  224. trace_gfs2_pin(bd, 0);
  225. atomic_dec(&sdp->sd_log_pinned);
  226. list_del_init(&bd->bd_list);
  227. if (meta) {
  228. gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
  229. sdp->sd_log_num_buf--;
  230. tr->tr_num_buf_rm++;
  231. } else {
  232. gfs2_assert_warn(sdp, sdp->sd_log_num_databuf);
  233. sdp->sd_log_num_databuf--;
  234. tr->tr_num_databuf_rm++;
  235. }
  236. tr->tr_touched = 1;
  237. brelse(bh);
  238. }
  239. if (bd) {
  240. spin_lock(&sdp->sd_ail_lock);
  241. if (bd->bd_tr) {
  242. gfs2_trans_add_revoke(sdp, bd);
  243. }
  244. spin_unlock(&sdp->sd_ail_lock);
  245. }
  246. clear_buffer_dirty(bh);
  247. clear_buffer_uptodate(bh);
  248. }
  249. /**
  250. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  251. * @ip: the inode who owns the buffers
  252. * @bstart: the first buffer in the run
  253. * @blen: the number of buffers in the run
  254. *
  255. */
  256. void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
  257. {
  258. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  259. struct buffer_head *bh;
  260. while (blen) {
  261. bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
  262. if (bh) {
  263. lock_buffer(bh);
  264. gfs2_log_lock(sdp);
  265. gfs2_remove_from_journal(bh, current->journal_info, 1);
  266. gfs2_log_unlock(sdp);
  267. unlock_buffer(bh);
  268. brelse(bh);
  269. }
  270. bstart++;
  271. blen--;
  272. }
  273. }
  274. /**
  275. * gfs2_meta_indirect_buffer - Get a metadata buffer
  276. * @ip: The GFS2 inode
  277. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  278. * @num: The block number (device relative) of the buffer
  279. * @bhp: the buffer is returned here
  280. *
  281. * Returns: errno
  282. */
  283. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
  284. struct buffer_head **bhp)
  285. {
  286. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  287. struct gfs2_glock *gl = ip->i_gl;
  288. struct buffer_head *bh;
  289. int ret = 0;
  290. u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
  291. ret = gfs2_meta_read(gl, num, DIO_WAIT, &bh);
  292. if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
  293. brelse(bh);
  294. ret = -EIO;
  295. }
  296. *bhp = bh;
  297. return ret;
  298. }
  299. /**
  300. * gfs2_meta_ra - start readahead on an extent of a file
  301. * @gl: the glock the blocks belong to
  302. * @dblock: the starting disk block
  303. * @extlen: the number of blocks in the extent
  304. *
  305. * returns: the first buffer in the extent
  306. */
  307. struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
  308. {
  309. struct gfs2_sbd *sdp = gl->gl_sbd;
  310. struct buffer_head *first_bh, *bh;
  311. u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
  312. sdp->sd_sb.sb_bsize_shift;
  313. BUG_ON(!extlen);
  314. if (max_ra < 1)
  315. max_ra = 1;
  316. if (extlen > max_ra)
  317. extlen = max_ra;
  318. first_bh = gfs2_getbuf(gl, dblock, CREATE);
  319. if (buffer_uptodate(first_bh))
  320. goto out;
  321. if (!buffer_locked(first_bh))
  322. ll_rw_block(READ_SYNC | REQ_META, 1, &first_bh);
  323. dblock++;
  324. extlen--;
  325. while (extlen) {
  326. bh = gfs2_getbuf(gl, dblock, CREATE);
  327. if (!buffer_uptodate(bh) && !buffer_locked(bh))
  328. ll_rw_block(READA | REQ_META, 1, &bh);
  329. brelse(bh);
  330. dblock++;
  331. extlen--;
  332. if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
  333. goto out;
  334. }
  335. wait_on_buffer(first_bh);
  336. out:
  337. return first_bh;
  338. }