meta_io.c 10.0 KB

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