meta_io.c 10 KB

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