meta_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/writeback.h>
  14. #include <linux/swap.h>
  15. #include <linux/delay.h>
  16. #include <linux/bio.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "glock.h"
  21. #include "glops.h"
  22. #include "inode.h"
  23. #include "log.h"
  24. #include "lops.h"
  25. #include "meta_io.h"
  26. #include "rgrp.h"
  27. #include "trans.h"
  28. #include "util.h"
  29. #include "trace_gfs2.h"
  30. static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
  31. {
  32. struct buffer_head *bh, *head;
  33. int nr_underway = 0;
  34. int write_flags = REQ_META | REQ_PRIO | wbc_to_write_flags(wbc);
  35. BUG_ON(!PageLocked(page));
  36. BUG_ON(!page_has_buffers(page));
  37. head = page_buffers(page);
  38. bh = head;
  39. do {
  40. if (!buffer_mapped(bh))
  41. continue;
  42. /*
  43. * If it's a fully non-blocking write attempt and we cannot
  44. * lock the buffer then redirty the page. Note that this can
  45. * potentially cause a busy-wait loop from flusher thread and kswapd
  46. * activity, but those code paths have their own higher-level
  47. * throttling.
  48. */
  49. if (wbc->sync_mode != WB_SYNC_NONE) {
  50. lock_buffer(bh);
  51. } else if (!trylock_buffer(bh)) {
  52. redirty_page_for_writepage(wbc, page);
  53. continue;
  54. }
  55. if (test_clear_buffer_dirty(bh)) {
  56. mark_buffer_async_write(bh);
  57. } else {
  58. unlock_buffer(bh);
  59. }
  60. } while ((bh = bh->b_this_page) != head);
  61. /*
  62. * The page and its buffers are protected by PageWriteback(), so we can
  63. * drop the bh refcounts early.
  64. */
  65. BUG_ON(PageWriteback(page));
  66. set_page_writeback(page);
  67. do {
  68. struct buffer_head *next = bh->b_this_page;
  69. if (buffer_async_write(bh)) {
  70. submit_bh(REQ_OP_WRITE, write_flags, bh);
  71. nr_underway++;
  72. }
  73. bh = next;
  74. } while (bh != head);
  75. unlock_page(page);
  76. if (nr_underway == 0)
  77. end_page_writeback(page);
  78. return 0;
  79. }
  80. const struct address_space_operations gfs2_meta_aops = {
  81. .writepage = gfs2_aspace_writepage,
  82. .releasepage = gfs2_releasepage,
  83. };
  84. const struct address_space_operations gfs2_rgrp_aops = {
  85. .writepage = gfs2_aspace_writepage,
  86. .releasepage = gfs2_releasepage,
  87. };
  88. /**
  89. * gfs2_getbuf - Get a buffer with a given address space
  90. * @gl: the glock
  91. * @blkno: the block number (filesystem scope)
  92. * @create: 1 if the buffer should be created
  93. *
  94. * Returns: the buffer
  95. */
  96. struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
  97. {
  98. struct address_space *mapping = gfs2_glock2aspace(gl);
  99. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  100. struct page *page;
  101. struct buffer_head *bh;
  102. unsigned int shift;
  103. unsigned long index;
  104. unsigned int bufnum;
  105. if (mapping == NULL)
  106. mapping = &sdp->sd_aspace;
  107. shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  108. index = blkno >> shift; /* convert block to page */
  109. bufnum = blkno - (index << shift); /* block buf index within page */
  110. if (create) {
  111. for (;;) {
  112. page = grab_cache_page(mapping, index);
  113. if (page)
  114. break;
  115. yield();
  116. }
  117. } else {
  118. page = find_get_page_flags(mapping, index,
  119. FGP_LOCK|FGP_ACCESSED);
  120. if (!page)
  121. return NULL;
  122. }
  123. if (!page_has_buffers(page))
  124. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  125. /* Locate header for our buffer within our page */
  126. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  127. /* Do nothing */;
  128. get_bh(bh);
  129. if (!buffer_mapped(bh))
  130. map_bh(bh, sdp->sd_vfs, blkno);
  131. unlock_page(page);
  132. put_page(page);
  133. return bh;
  134. }
  135. static void meta_prep_new(struct buffer_head *bh)
  136. {
  137. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  138. lock_buffer(bh);
  139. clear_buffer_dirty(bh);
  140. set_buffer_uptodate(bh);
  141. unlock_buffer(bh);
  142. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  143. }
  144. /**
  145. * gfs2_meta_new - Get a block
  146. * @gl: The glock associated with this block
  147. * @blkno: The block number
  148. *
  149. * Returns: The buffer
  150. */
  151. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
  152. {
  153. struct buffer_head *bh;
  154. bh = gfs2_getbuf(gl, blkno, CREATE);
  155. meta_prep_new(bh);
  156. return bh;
  157. }
  158. static void gfs2_meta_read_endio(struct bio *bio)
  159. {
  160. struct bio_vec *bvec;
  161. struct bvec_iter_all iter_all;
  162. bio_for_each_segment_all(bvec, bio, iter_all) {
  163. struct page *page = bvec->bv_page;
  164. struct buffer_head *bh = page_buffers(page);
  165. unsigned int len = bvec->bv_len;
  166. while (bh_offset(bh) < bvec->bv_offset)
  167. bh = bh->b_this_page;
  168. do {
  169. struct buffer_head *next = bh->b_this_page;
  170. len -= bh->b_size;
  171. bh->b_end_io(bh, !bio->bi_status);
  172. bh = next;
  173. } while (bh && len);
  174. }
  175. bio_put(bio);
  176. }
  177. /*
  178. * Submit several consecutive buffer head I/O requests as a single bio I/O
  179. * request. (See submit_bh_wbc.)
  180. */
  181. static void gfs2_submit_bhs(int op, int op_flags, struct buffer_head *bhs[],
  182. int num)
  183. {
  184. while (num > 0) {
  185. struct buffer_head *bh = *bhs;
  186. struct bio *bio;
  187. bio = bio_alloc(GFP_NOIO, num);
  188. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  189. bio_set_dev(bio, bh->b_bdev);
  190. while (num > 0) {
  191. bh = *bhs;
  192. if (!bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh))) {
  193. BUG_ON(bio->bi_iter.bi_size == 0);
  194. break;
  195. }
  196. bhs++;
  197. num--;
  198. }
  199. bio->bi_end_io = gfs2_meta_read_endio;
  200. bio_set_op_attrs(bio, op, op_flags);
  201. submit_bio(bio);
  202. }
  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(gfs2_withdrawn(sdp)) &&
  220. (!sdp->sd_jdesc || gl != sdp->sd_jinode_gl)) {
  221. *bhp = NULL;
  222. return -EIO;
  223. }
  224. *bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
  225. lock_buffer(bh);
  226. if (buffer_uptodate(bh)) {
  227. unlock_buffer(bh);
  228. flags &= ~DIO_WAIT;
  229. } else {
  230. bh->b_end_io = end_buffer_read_sync;
  231. get_bh(bh);
  232. bhs[num++] = bh;
  233. }
  234. if (rahead) {
  235. bh = gfs2_getbuf(gl, blkno + 1, CREATE);
  236. lock_buffer(bh);
  237. if (buffer_uptodate(bh)) {
  238. unlock_buffer(bh);
  239. brelse(bh);
  240. } else {
  241. bh->b_end_io = end_buffer_read_sync;
  242. bhs[num++] = bh;
  243. }
  244. }
  245. gfs2_submit_bhs(REQ_OP_READ, REQ_META | REQ_PRIO, bhs, num);
  246. if (!(flags & DIO_WAIT))
  247. return 0;
  248. bh = *bhp;
  249. wait_on_buffer(bh);
  250. if (unlikely(!buffer_uptodate(bh))) {
  251. struct gfs2_trans *tr = current->journal_info;
  252. if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
  253. gfs2_io_error_bh_wd(sdp, bh);
  254. brelse(bh);
  255. *bhp = NULL;
  256. return -EIO;
  257. }
  258. return 0;
  259. }
  260. /**
  261. * gfs2_meta_wait - Reread a block from disk
  262. * @sdp: the filesystem
  263. * @bh: The block to wait for
  264. *
  265. * Returns: errno
  266. */
  267. int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
  268. {
  269. if (unlikely(gfs2_withdrawn(sdp)))
  270. return -EIO;
  271. wait_on_buffer(bh);
  272. if (!buffer_uptodate(bh)) {
  273. struct gfs2_trans *tr = current->journal_info;
  274. if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
  275. gfs2_io_error_bh_wd(sdp, bh);
  276. return -EIO;
  277. }
  278. if (unlikely(gfs2_withdrawn(sdp)))
  279. return -EIO;
  280. return 0;
  281. }
  282. void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
  283. {
  284. struct address_space *mapping = bh->b_page->mapping;
  285. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  286. struct gfs2_bufdata *bd = bh->b_private;
  287. struct gfs2_trans *tr = current->journal_info;
  288. int was_pinned = 0;
  289. if (test_clear_buffer_pinned(bh)) {
  290. trace_gfs2_pin(bd, 0);
  291. atomic_dec(&sdp->sd_log_pinned);
  292. list_del_init(&bd->bd_list);
  293. if (meta == REMOVE_META)
  294. tr->tr_num_buf_rm++;
  295. else
  296. tr->tr_num_databuf_rm++;
  297. set_bit(TR_TOUCHED, &tr->tr_flags);
  298. was_pinned = 1;
  299. brelse(bh);
  300. }
  301. if (bd) {
  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. } else if (!list_empty(&bd->bd_ail_st_list) &&
  308. !list_empty(&bd->bd_ail_gl_list)) {
  309. gfs2_remove_from_ail(bd);
  310. }
  311. }
  312. clear_buffer_dirty(bh);
  313. clear_buffer_uptodate(bh);
  314. }
  315. /**
  316. * gfs2_ail1_wipe - remove deleted/freed buffers from the ail1 list
  317. * @sdp: superblock
  318. * @bstart: starting block address of buffers to remove
  319. * @blen: length of buffers to be removed
  320. *
  321. * This function is called from gfs2_journal wipe, whose job is to remove
  322. * buffers, corresponding to deleted blocks, from the journal. If we find any
  323. * bufdata elements on the system ail1 list, they haven't been written to
  324. * the journal yet. So we remove them.
  325. */
  326. static void gfs2_ail1_wipe(struct gfs2_sbd *sdp, u64 bstart, u32 blen)
  327. {
  328. struct gfs2_trans *tr, *s;
  329. struct gfs2_bufdata *bd, *bs;
  330. struct buffer_head *bh;
  331. u64 end = bstart + blen;
  332. gfs2_log_lock(sdp);
  333. spin_lock(&sdp->sd_ail_lock);
  334. list_for_each_entry_safe(tr, s, &sdp->sd_ail1_list, tr_list) {
  335. list_for_each_entry_safe(bd, bs, &tr->tr_ail1_list,
  336. bd_ail_st_list) {
  337. bh = bd->bd_bh;
  338. if (bh->b_blocknr < bstart || bh->b_blocknr >= end)
  339. continue;
  340. gfs2_remove_from_journal(bh, REMOVE_JDATA);
  341. }
  342. }
  343. spin_unlock(&sdp->sd_ail_lock);
  344. gfs2_log_unlock(sdp);
  345. }
  346. static struct buffer_head *gfs2_getjdatabuf(struct gfs2_inode *ip, u64 blkno)
  347. {
  348. struct address_space *mapping = ip->i_inode.i_mapping;
  349. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  350. struct page *page;
  351. struct buffer_head *bh;
  352. unsigned int shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  353. unsigned long index = blkno >> shift; /* convert block to page */
  354. unsigned int bufnum = blkno - (index << shift);
  355. page = find_get_page_flags(mapping, index, FGP_LOCK|FGP_ACCESSED);
  356. if (!page)
  357. return NULL;
  358. if (!page_has_buffers(page)) {
  359. unlock_page(page);
  360. put_page(page);
  361. return NULL;
  362. }
  363. /* Locate header for our buffer within our page */
  364. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  365. /* Do nothing */;
  366. get_bh(bh);
  367. unlock_page(page);
  368. put_page(page);
  369. return bh;
  370. }
  371. /**
  372. * gfs2_journal_wipe - make inode's buffers so they aren't dirty/pinned anymore
  373. * @ip: the inode who owns the buffers
  374. * @bstart: the first buffer in the run
  375. * @blen: the number of buffers in the run
  376. *
  377. */
  378. void gfs2_journal_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
  379. {
  380. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  381. struct buffer_head *bh;
  382. int ty;
  383. gfs2_ail1_wipe(sdp, bstart, blen);
  384. while (blen) {
  385. ty = REMOVE_META;
  386. bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
  387. if (!bh && gfs2_is_jdata(ip)) {
  388. bh = gfs2_getjdatabuf(ip, bstart);
  389. ty = REMOVE_JDATA;
  390. }
  391. if (bh) {
  392. lock_buffer(bh);
  393. gfs2_log_lock(sdp);
  394. spin_lock(&sdp->sd_ail_lock);
  395. gfs2_remove_from_journal(bh, ty);
  396. spin_unlock(&sdp->sd_ail_lock);
  397. gfs2_log_unlock(sdp);
  398. unlock_buffer(bh);
  399. brelse(bh);
  400. }
  401. bstart++;
  402. blen--;
  403. }
  404. }
  405. /**
  406. * gfs2_meta_indirect_buffer - Get a metadata buffer
  407. * @ip: The GFS2 inode
  408. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  409. * @num: The block number (device relative) of the buffer
  410. * @bhp: the buffer is returned here
  411. *
  412. * Returns: errno
  413. */
  414. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
  415. struct buffer_head **bhp)
  416. {
  417. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  418. struct gfs2_glock *gl = ip->i_gl;
  419. struct buffer_head *bh;
  420. int ret = 0;
  421. u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
  422. int rahead = 0;
  423. if (num == ip->i_no_addr)
  424. rahead = ip->i_rahead;
  425. ret = gfs2_meta_read(gl, num, DIO_WAIT, rahead, &bh);
  426. if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
  427. brelse(bh);
  428. ret = -EIO;
  429. } else {
  430. *bhp = bh;
  431. }
  432. return ret;
  433. }
  434. /**
  435. * gfs2_meta_ra - start readahead on an extent of a file
  436. * @gl: the glock the blocks belong to
  437. * @dblock: the starting disk block
  438. * @extlen: the number of blocks in the extent
  439. *
  440. * returns: the first buffer in the extent
  441. */
  442. struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
  443. {
  444. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  445. struct buffer_head *first_bh, *bh;
  446. u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
  447. sdp->sd_sb.sb_bsize_shift;
  448. BUG_ON(!extlen);
  449. if (max_ra < 1)
  450. max_ra = 1;
  451. if (extlen > max_ra)
  452. extlen = max_ra;
  453. first_bh = gfs2_getbuf(gl, dblock, CREATE);
  454. if (buffer_uptodate(first_bh))
  455. goto out;
  456. if (!buffer_locked(first_bh))
  457. ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &first_bh);
  458. dblock++;
  459. extlen--;
  460. while (extlen) {
  461. bh = gfs2_getbuf(gl, dblock, CREATE);
  462. if (!buffer_uptodate(bh) && !buffer_locked(bh))
  463. ll_rw_block(REQ_OP_READ,
  464. REQ_RAHEAD | REQ_META | REQ_PRIO,
  465. 1, &bh);
  466. brelse(bh);
  467. dblock++;
  468. extlen--;
  469. if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
  470. goto out;
  471. }
  472. wait_on_buffer(first_bh);
  473. out:
  474. return first_bh;
  475. }