meta_io.c 11 KB

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