meta_io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
  33. {
  34. struct buffer_head *bh, *head;
  35. int nr_underway = 0;
  36. int write_op = REQ_META |
  37. (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC_PLUG : WRITE);
  38. BUG_ON(!PageLocked(page));
  39. BUG_ON(!page_has_buffers(page));
  40. head = page_buffers(page);
  41. bh = head;
  42. do {
  43. if (!buffer_mapped(bh))
  44. continue;
  45. /*
  46. * If it's a fully non-blocking write attempt and we cannot
  47. * lock the buffer then redirty the page. Note that this can
  48. * potentially cause a busy-wait loop from pdflush and kswapd
  49. * activity, but those code paths have their own higher-level
  50. * throttling.
  51. */
  52. if (wbc->sync_mode != WB_SYNC_NONE) {
  53. lock_buffer(bh);
  54. } else if (!trylock_buffer(bh)) {
  55. redirty_page_for_writepage(wbc, page);
  56. continue;
  57. }
  58. if (test_clear_buffer_dirty(bh)) {
  59. mark_buffer_async_write(bh);
  60. } else {
  61. unlock_buffer(bh);
  62. }
  63. } while ((bh = bh->b_this_page) != head);
  64. /*
  65. * The page and its buffers are protected by PageWriteback(), so we can
  66. * drop the bh refcounts early.
  67. */
  68. BUG_ON(PageWriteback(page));
  69. set_page_writeback(page);
  70. do {
  71. struct buffer_head *next = bh->b_this_page;
  72. if (buffer_async_write(bh)) {
  73. submit_bh(write_op, bh);
  74. nr_underway++;
  75. }
  76. bh = next;
  77. } while (bh != head);
  78. unlock_page(page);
  79. if (nr_underway == 0)
  80. end_page_writeback(page);
  81. return 0;
  82. }
  83. const struct address_space_operations gfs2_meta_aops = {
  84. .writepage = gfs2_aspace_writepage,
  85. .releasepage = gfs2_releasepage,
  86. .sync_page = block_sync_page,
  87. };
  88. /**
  89. * gfs2_meta_sync - Sync all buffers associated with a glock
  90. * @gl: The glock
  91. *
  92. */
  93. void gfs2_meta_sync(struct gfs2_glock *gl)
  94. {
  95. struct address_space *mapping = gfs2_glock2aspace(gl);
  96. int error;
  97. filemap_fdatawrite(mapping);
  98. error = filemap_fdatawait(mapping);
  99. if (error)
  100. gfs2_io_error(gl->gl_sbd);
  101. }
  102. /**
  103. * gfs2_getbuf - Get a buffer with a given address space
  104. * @gl: the glock
  105. * @blkno: the block number (filesystem scope)
  106. * @create: 1 if the buffer should be created
  107. *
  108. * Returns: the buffer
  109. */
  110. struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
  111. {
  112. struct address_space *mapping = gfs2_glock2aspace(gl);
  113. struct gfs2_sbd *sdp = gl->gl_sbd;
  114. struct page *page;
  115. struct buffer_head *bh;
  116. unsigned int shift;
  117. unsigned long index;
  118. unsigned int bufnum;
  119. shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  120. index = blkno >> shift; /* convert block to page */
  121. bufnum = blkno - (index << shift); /* block buf index within page */
  122. if (create) {
  123. for (;;) {
  124. page = grab_cache_page(mapping, index);
  125. if (page)
  126. break;
  127. yield();
  128. }
  129. } else {
  130. page = find_lock_page(mapping, index);
  131. if (!page)
  132. return NULL;
  133. }
  134. if (!page_has_buffers(page))
  135. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  136. /* Locate header for our buffer within our page */
  137. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  138. /* Do nothing */;
  139. get_bh(bh);
  140. if (!buffer_mapped(bh))
  141. map_bh(bh, sdp->sd_vfs, blkno);
  142. unlock_page(page);
  143. mark_page_accessed(page);
  144. page_cache_release(page);
  145. return bh;
  146. }
  147. static void meta_prep_new(struct buffer_head *bh)
  148. {
  149. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  150. lock_buffer(bh);
  151. clear_buffer_dirty(bh);
  152. set_buffer_uptodate(bh);
  153. unlock_buffer(bh);
  154. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  155. }
  156. /**
  157. * gfs2_meta_new - Get a block
  158. * @gl: The glock associated with this block
  159. * @blkno: The block number
  160. *
  161. * Returns: The buffer
  162. */
  163. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
  164. {
  165. struct buffer_head *bh;
  166. bh = gfs2_getbuf(gl, blkno, CREATE);
  167. meta_prep_new(bh);
  168. return bh;
  169. }
  170. /**
  171. * gfs2_meta_read - Read a block from disk
  172. * @gl: The glock covering the block
  173. * @blkno: The block number
  174. * @flags: flags
  175. * @bhp: the place where the buffer is returned (NULL on failure)
  176. *
  177. * Returns: errno
  178. */
  179. int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
  180. struct buffer_head **bhp)
  181. {
  182. struct gfs2_sbd *sdp = gl->gl_sbd;
  183. struct buffer_head *bh;
  184. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  185. return -EIO;
  186. *bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
  187. lock_buffer(bh);
  188. if (buffer_uptodate(bh)) {
  189. unlock_buffer(bh);
  190. return 0;
  191. }
  192. bh->b_end_io = end_buffer_read_sync;
  193. get_bh(bh);
  194. submit_bh(READ_SYNC | REQ_META, bh);
  195. if (!(flags & DIO_WAIT))
  196. return 0;
  197. wait_on_buffer(bh);
  198. if (unlikely(!buffer_uptodate(bh))) {
  199. struct gfs2_trans *tr = current->journal_info;
  200. if (tr && tr->tr_touched)
  201. gfs2_io_error_bh(sdp, bh);
  202. brelse(bh);
  203. return -EIO;
  204. }
  205. return 0;
  206. }
  207. /**
  208. * gfs2_meta_wait - Reread a block from disk
  209. * @sdp: the filesystem
  210. * @bh: The block to wait for
  211. *
  212. * Returns: errno
  213. */
  214. int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
  215. {
  216. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  217. return -EIO;
  218. wait_on_buffer(bh);
  219. if (!buffer_uptodate(bh)) {
  220. struct gfs2_trans *tr = current->journal_info;
  221. if (tr && tr->tr_touched)
  222. gfs2_io_error_bh(sdp, bh);
  223. return -EIO;
  224. }
  225. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  226. return -EIO;
  227. return 0;
  228. }
  229. /**
  230. * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
  231. * @gl: the glock the buffer belongs to
  232. * @bh: The buffer to be attached to
  233. * @meta: Flag to indicate whether its metadata or not
  234. */
  235. void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
  236. int meta)
  237. {
  238. struct gfs2_bufdata *bd;
  239. if (meta)
  240. lock_page(bh->b_page);
  241. if (bh->b_private) {
  242. if (meta)
  243. unlock_page(bh->b_page);
  244. return;
  245. }
  246. bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
  247. bd->bd_bh = bh;
  248. bd->bd_gl = gl;
  249. INIT_LIST_HEAD(&bd->bd_list_tr);
  250. if (meta)
  251. lops_init_le(&bd->bd_le, &gfs2_buf_lops);
  252. else
  253. lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
  254. bh->b_private = bd;
  255. if (meta)
  256. unlock_page(bh->b_page);
  257. }
  258. void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int meta)
  259. {
  260. struct address_space *mapping = bh->b_page->mapping;
  261. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  262. struct gfs2_bufdata *bd = bh->b_private;
  263. if (test_clear_buffer_pinned(bh)) {
  264. atomic_dec(&sdp->sd_log_pinned);
  265. list_del_init(&bd->bd_le.le_list);
  266. if (meta) {
  267. gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
  268. sdp->sd_log_num_buf--;
  269. tr->tr_num_buf_rm++;
  270. } else {
  271. gfs2_assert_warn(sdp, sdp->sd_log_num_databuf);
  272. sdp->sd_log_num_databuf--;
  273. tr->tr_num_databuf_rm++;
  274. }
  275. tr->tr_touched = 1;
  276. brelse(bh);
  277. }
  278. if (bd) {
  279. spin_lock(&sdp->sd_ail_lock);
  280. if (bd->bd_ail) {
  281. gfs2_remove_from_ail(bd);
  282. bh->b_private = NULL;
  283. bd->bd_bh = NULL;
  284. bd->bd_blkno = bh->b_blocknr;
  285. gfs2_trans_add_revoke(sdp, bd);
  286. }
  287. spin_unlock(&sdp->sd_ail_lock);
  288. }
  289. clear_buffer_dirty(bh);
  290. clear_buffer_uptodate(bh);
  291. }
  292. /**
  293. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  294. * @ip: the inode who owns the buffers
  295. * @bstart: the first buffer in the run
  296. * @blen: the number of buffers in the run
  297. *
  298. */
  299. void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
  300. {
  301. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  302. struct buffer_head *bh;
  303. while (blen) {
  304. bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
  305. if (bh) {
  306. lock_buffer(bh);
  307. gfs2_log_lock(sdp);
  308. gfs2_remove_from_journal(bh, current->journal_info, 1);
  309. gfs2_log_unlock(sdp);
  310. unlock_buffer(bh);
  311. brelse(bh);
  312. }
  313. bstart++;
  314. blen--;
  315. }
  316. }
  317. /**
  318. * gfs2_meta_indirect_buffer - Get a metadata buffer
  319. * @ip: The GFS2 inode
  320. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  321. * @num: The block number (device relative) of the buffer
  322. * @new: Non-zero if we may create a new buffer
  323. * @bhp: the buffer is returned here
  324. *
  325. * Returns: errno
  326. */
  327. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
  328. int new, struct buffer_head **bhp)
  329. {
  330. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  331. struct gfs2_glock *gl = ip->i_gl;
  332. struct buffer_head *bh;
  333. int ret = 0;
  334. if (new) {
  335. BUG_ON(height == 0);
  336. bh = gfs2_meta_new(gl, num);
  337. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  338. gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  339. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  340. } else {
  341. u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
  342. ret = gfs2_meta_read(gl, num, DIO_WAIT, &bh);
  343. if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
  344. brelse(bh);
  345. ret = -EIO;
  346. }
  347. }
  348. *bhp = bh;
  349. return ret;
  350. }
  351. /**
  352. * gfs2_meta_ra - start readahead on an extent of a file
  353. * @gl: the glock the blocks belong to
  354. * @dblock: the starting disk block
  355. * @extlen: the number of blocks in the extent
  356. *
  357. * returns: the first buffer in the extent
  358. */
  359. struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
  360. {
  361. struct gfs2_sbd *sdp = gl->gl_sbd;
  362. struct buffer_head *first_bh, *bh;
  363. u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
  364. sdp->sd_sb.sb_bsize_shift;
  365. BUG_ON(!extlen);
  366. if (max_ra < 1)
  367. max_ra = 1;
  368. if (extlen > max_ra)
  369. extlen = max_ra;
  370. first_bh = gfs2_getbuf(gl, dblock, CREATE);
  371. if (buffer_uptodate(first_bh))
  372. goto out;
  373. if (!buffer_locked(first_bh))
  374. ll_rw_block(READ_SYNC | REQ_META, 1, &first_bh);
  375. dblock++;
  376. extlen--;
  377. while (extlen) {
  378. bh = gfs2_getbuf(gl, dblock, CREATE);
  379. if (!buffer_uptodate(bh) && !buffer_locked(bh))
  380. ll_rw_block(READA, 1, &bh);
  381. brelse(bh);
  382. dblock++;
  383. extlen--;
  384. if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
  385. goto out;
  386. }
  387. wait_on_buffer(first_bh);
  388. out:
  389. return first_bh;
  390. }