meta_io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 "trace_gfs2.h"
  33. static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
  34. {
  35. struct buffer_head *bh, *head;
  36. int nr_underway = 0;
  37. int write_flags = REQ_META | REQ_PRIO |
  38. (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : 0);
  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 flusher thread and kswapd
  50. * activity, but those code paths have their own higher-level
  51. * throttling.
  52. */
  53. if (wbc->sync_mode != WB_SYNC_NONE) {
  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(REQ_OP_WRITE, write_flags, bh);
  75. nr_underway++;
  76. }
  77. bh = next;
  78. } while (bh != head);
  79. unlock_page(page);
  80. if (nr_underway == 0)
  81. end_page_writeback(page);
  82. return 0;
  83. }
  84. const struct address_space_operations gfs2_meta_aops = {
  85. .writepage = gfs2_aspace_writepage,
  86. .releasepage = gfs2_releasepage,
  87. };
  88. const struct address_space_operations gfs2_rgrp_aops = {
  89. .writepage = gfs2_aspace_writepage,
  90. .releasepage = gfs2_releasepage,
  91. };
  92. /**
  93. * gfs2_getbuf - Get a buffer with a given address space
  94. * @gl: the glock
  95. * @blkno: the block number (filesystem scope)
  96. * @create: 1 if the buffer should be created
  97. *
  98. * Returns: the buffer
  99. */
  100. struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
  101. {
  102. struct address_space *mapping = gfs2_glock2aspace(gl);
  103. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  104. struct page *page;
  105. struct buffer_head *bh;
  106. unsigned int shift;
  107. unsigned long index;
  108. unsigned int bufnum;
  109. if (mapping == NULL)
  110. mapping = &sdp->sd_aspace;
  111. shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  112. index = blkno >> shift; /* convert block to page */
  113. bufnum = blkno - (index << shift); /* block buf index within page */
  114. if (create) {
  115. for (;;) {
  116. page = grab_cache_page(mapping, index);
  117. if (page)
  118. break;
  119. yield();
  120. }
  121. } else {
  122. page = find_get_page_flags(mapping, index,
  123. FGP_LOCK|FGP_ACCESSED);
  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. put_page(page);
  137. return bh;
  138. }
  139. static void meta_prep_new(struct buffer_head *bh)
  140. {
  141. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  142. lock_buffer(bh);
  143. clear_buffer_dirty(bh);
  144. set_buffer_uptodate(bh);
  145. unlock_buffer(bh);
  146. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  147. }
  148. /**
  149. * gfs2_meta_new - Get a block
  150. * @gl: The glock associated with this block
  151. * @blkno: The block number
  152. *
  153. * Returns: The buffer
  154. */
  155. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
  156. {
  157. struct buffer_head *bh;
  158. bh = gfs2_getbuf(gl, blkno, CREATE);
  159. meta_prep_new(bh);
  160. return bh;
  161. }
  162. static void gfs2_meta_read_endio(struct bio *bio)
  163. {
  164. struct bio_vec *bvec;
  165. int i;
  166. bio_for_each_segment_all(bvec, bio, i) {
  167. struct page *page = bvec->bv_page;
  168. struct buffer_head *bh = page_buffers(page);
  169. unsigned int len = bvec->bv_len;
  170. while (bh_offset(bh) < bvec->bv_offset)
  171. bh = bh->b_this_page;
  172. do {
  173. struct buffer_head *next = bh->b_this_page;
  174. len -= bh->b_size;
  175. bh->b_end_io(bh, !bio->bi_error);
  176. bh = next;
  177. } while (bh && len);
  178. }
  179. bio_put(bio);
  180. }
  181. /*
  182. * Submit several consecutive buffer head I/O requests as a single bio I/O
  183. * request. (See submit_bh_wbc.)
  184. */
  185. static void gfs2_submit_bhs(int op, int op_flags, struct buffer_head *bhs[],
  186. int num)
  187. {
  188. struct buffer_head *bh = bhs[0];
  189. struct bio *bio;
  190. int i;
  191. if (!num)
  192. return;
  193. bio = bio_alloc(GFP_NOIO, num);
  194. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  195. bio->bi_bdev = bh->b_bdev;
  196. for (i = 0; i < num; i++) {
  197. bh = bhs[i];
  198. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  199. }
  200. bio->bi_end_io = gfs2_meta_read_endio;
  201. bio_set_op_attrs(bio, op, op_flags);
  202. submit_bio(bio);
  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. int rahead, struct buffer_head **bhp)
  215. {
  216. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  217. struct buffer_head *bh, *bhs[2];
  218. int num = 0;
  219. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  220. *bhp = NULL;
  221. return -EIO;
  222. }
  223. *bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
  224. lock_buffer(bh);
  225. if (buffer_uptodate(bh)) {
  226. unlock_buffer(bh);
  227. flags &= ~DIO_WAIT;
  228. } else {
  229. bh->b_end_io = end_buffer_read_sync;
  230. get_bh(bh);
  231. bhs[num++] = bh;
  232. }
  233. if (rahead) {
  234. bh = gfs2_getbuf(gl, blkno + 1, CREATE);
  235. lock_buffer(bh);
  236. if (buffer_uptodate(bh)) {
  237. unlock_buffer(bh);
  238. brelse(bh);
  239. } else {
  240. bh->b_end_io = end_buffer_read_sync;
  241. bhs[num++] = bh;
  242. }
  243. }
  244. gfs2_submit_bhs(REQ_OP_READ, READ_SYNC | REQ_META | REQ_PRIO, bhs, num);
  245. if (!(flags & DIO_WAIT))
  246. return 0;
  247. bh = *bhp;
  248. wait_on_buffer(bh);
  249. if (unlikely(!buffer_uptodate(bh))) {
  250. struct gfs2_trans *tr = current->journal_info;
  251. if (tr && tr->tr_touched)
  252. gfs2_io_error_bh(sdp, bh);
  253. brelse(bh);
  254. *bhp = NULL;
  255. return -EIO;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * gfs2_meta_wait - Reread a block from disk
  261. * @sdp: the filesystem
  262. * @bh: The block to wait for
  263. *
  264. * Returns: errno
  265. */
  266. int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
  267. {
  268. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  269. return -EIO;
  270. wait_on_buffer(bh);
  271. if (!buffer_uptodate(bh)) {
  272. struct gfs2_trans *tr = current->journal_info;
  273. if (tr && tr->tr_touched)
  274. gfs2_io_error_bh(sdp, bh);
  275. return -EIO;
  276. }
  277. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  278. return -EIO;
  279. return 0;
  280. }
  281. void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
  282. {
  283. struct address_space *mapping = bh->b_page->mapping;
  284. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  285. struct gfs2_bufdata *bd = bh->b_private;
  286. struct gfs2_trans *tr = current->journal_info;
  287. int was_pinned = 0;
  288. if (test_clear_buffer_pinned(bh)) {
  289. trace_gfs2_pin(bd, 0);
  290. atomic_dec(&sdp->sd_log_pinned);
  291. list_del_init(&bd->bd_list);
  292. if (meta == REMOVE_META)
  293. tr->tr_num_buf_rm++;
  294. else
  295. tr->tr_num_databuf_rm++;
  296. tr->tr_touched = 1;
  297. was_pinned = 1;
  298. brelse(bh);
  299. }
  300. if (bd) {
  301. spin_lock(&sdp->sd_ail_lock);
  302. if (bd->bd_tr) {
  303. gfs2_trans_add_revoke(sdp, bd);
  304. } else if (was_pinned) {
  305. bh->b_private = NULL;
  306. kmem_cache_free(gfs2_bufdata_cachep, bd);
  307. }
  308. spin_unlock(&sdp->sd_ail_lock);
  309. }
  310. clear_buffer_dirty(bh);
  311. clear_buffer_uptodate(bh);
  312. }
  313. /**
  314. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  315. * @ip: the inode who owns the buffers
  316. * @bstart: the first buffer in the run
  317. * @blen: the number of buffers in the run
  318. *
  319. */
  320. void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
  321. {
  322. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  323. struct buffer_head *bh;
  324. while (blen) {
  325. bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
  326. if (bh) {
  327. lock_buffer(bh);
  328. gfs2_log_lock(sdp);
  329. gfs2_remove_from_journal(bh, REMOVE_META);
  330. gfs2_log_unlock(sdp);
  331. unlock_buffer(bh);
  332. brelse(bh);
  333. }
  334. bstart++;
  335. blen--;
  336. }
  337. }
  338. /**
  339. * gfs2_meta_indirect_buffer - Get a metadata buffer
  340. * @ip: The GFS2 inode
  341. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  342. * @num: The block number (device relative) of the buffer
  343. * @bhp: the buffer is returned here
  344. *
  345. * Returns: errno
  346. */
  347. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
  348. struct buffer_head **bhp)
  349. {
  350. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  351. struct gfs2_glock *gl = ip->i_gl;
  352. struct buffer_head *bh;
  353. int ret = 0;
  354. u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
  355. int rahead = 0;
  356. if (num == ip->i_no_addr)
  357. rahead = ip->i_rahead;
  358. ret = gfs2_meta_read(gl, num, DIO_WAIT, rahead, &bh);
  359. if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
  360. brelse(bh);
  361. ret = -EIO;
  362. }
  363. *bhp = bh;
  364. return ret;
  365. }
  366. /**
  367. * gfs2_meta_ra - start readahead on an extent of a file
  368. * @gl: the glock the blocks belong to
  369. * @dblock: the starting disk block
  370. * @extlen: the number of blocks in the extent
  371. *
  372. * returns: the first buffer in the extent
  373. */
  374. struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
  375. {
  376. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  377. struct buffer_head *first_bh, *bh;
  378. u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
  379. sdp->sd_sb.sb_bsize_shift;
  380. BUG_ON(!extlen);
  381. if (max_ra < 1)
  382. max_ra = 1;
  383. if (extlen > max_ra)
  384. extlen = max_ra;
  385. first_bh = gfs2_getbuf(gl, dblock, CREATE);
  386. if (buffer_uptodate(first_bh))
  387. goto out;
  388. if (!buffer_locked(first_bh))
  389. ll_rw_block(REQ_OP_READ, READ_SYNC | REQ_META, 1, &first_bh);
  390. dblock++;
  391. extlen--;
  392. while (extlen) {
  393. bh = gfs2_getbuf(gl, dblock, CREATE);
  394. if (!buffer_uptodate(bh) && !buffer_locked(bh))
  395. ll_rw_block(REQ_OP_READ, REQ_RAHEAD | REQ_META, 1, &bh);
  396. brelse(bh);
  397. dblock++;
  398. extlen--;
  399. if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
  400. goto out;
  401. }
  402. wait_on_buffer(first_bh);
  403. out:
  404. return first_bh;
  405. }