meta_io.c 10 KB

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