meta_io.c 11 KB

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