meta_io.c 9.9 KB

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