meta_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 void gfs2_aspace_write_folio(struct folio *folio,
  31. struct writeback_control *wbc)
  32. {
  33. struct buffer_head *bh, *head;
  34. int nr_underway = 0;
  35. blk_opf_t write_flags = REQ_META | REQ_PRIO | wbc_to_write_flags(wbc);
  36. BUG_ON(!folio_test_locked(folio));
  37. head = folio_buffers(folio);
  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. folio_redirty_for_writepage(wbc, folio);
  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 folio and its buffers are protected from truncation by
  63. * the writeback flag, so we can drop the bh refcounts early.
  64. */
  65. BUG_ON(folio_test_writeback(folio));
  66. folio_start_writeback(folio);
  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. folio_unlock(folio);
  76. if (nr_underway == 0)
  77. folio_end_writeback(folio);
  78. }
  79. static int gfs2_aspace_writepages(struct address_space *mapping,
  80. struct writeback_control *wbc)
  81. {
  82. struct folio *folio = NULL;
  83. int error;
  84. while ((folio = writeback_iter(mapping, wbc, folio, &error)))
  85. gfs2_aspace_write_folio(folio, wbc);
  86. return error;
  87. }
  88. const struct address_space_operations gfs2_meta_aops = {
  89. .dirty_folio = block_dirty_folio,
  90. .invalidate_folio = block_invalidate_folio,
  91. .writepages = gfs2_aspace_writepages,
  92. .release_folio = gfs2_release_folio,
  93. .migrate_folio = buffer_migrate_folio_norefs,
  94. };
  95. const struct address_space_operations gfs2_rgrp_aops = {
  96. .dirty_folio = block_dirty_folio,
  97. .invalidate_folio = block_invalidate_folio,
  98. .writepages = gfs2_aspace_writepages,
  99. .release_folio = gfs2_release_folio,
  100. .migrate_folio = buffer_migrate_folio_norefs,
  101. };
  102. /**
  103. * gfs2_getbuf - Get a buffer with a given address space
  104. * @gl: the glock
  105. * @blkno: the block number (filesystem scope)
  106. * @create: 1 if the buffer should be created
  107. *
  108. * Returns: the buffer
  109. */
  110. struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
  111. {
  112. struct address_space *mapping = gfs2_glock2aspace(gl);
  113. struct gfs2_sbd *sdp = glock_sbd(gl);
  114. struct folio *folio;
  115. struct buffer_head *bh;
  116. unsigned int shift;
  117. unsigned long index;
  118. unsigned int bufnum;
  119. if (mapping == NULL)
  120. mapping = gfs2_aspace(sdp);
  121. shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  122. index = blkno >> shift; /* convert block to page */
  123. bufnum = blkno - (index << shift); /* block buf index within page */
  124. if (create) {
  125. folio = __filemap_get_folio(mapping, index,
  126. FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  127. mapping_gfp_mask(mapping) | __GFP_NOFAIL);
  128. bh = folio_buffers(folio);
  129. if (!bh)
  130. bh = create_empty_buffers(folio,
  131. sdp->sd_sb.sb_bsize, 0);
  132. } else {
  133. folio = __filemap_get_folio(mapping, index,
  134. FGP_LOCK | FGP_ACCESSED, 0);
  135. if (IS_ERR(folio))
  136. return NULL;
  137. bh = folio_buffers(folio);
  138. }
  139. if (!bh)
  140. goto out_unlock;
  141. bh = get_nth_bh(bh, bufnum);
  142. if (!buffer_mapped(bh))
  143. map_bh(bh, sdp->sd_vfs, blkno);
  144. out_unlock:
  145. folio_unlock(folio);
  146. folio_put(folio);
  147. return bh;
  148. }
  149. static void meta_prep_new(struct buffer_head *bh)
  150. {
  151. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  152. lock_buffer(bh);
  153. clear_buffer_dirty(bh);
  154. set_buffer_uptodate(bh);
  155. unlock_buffer(bh);
  156. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  157. }
  158. /**
  159. * gfs2_meta_new - Get a block
  160. * @gl: The glock associated with this block
  161. * @blkno: The block number
  162. *
  163. * Returns: The buffer
  164. */
  165. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
  166. {
  167. struct buffer_head *bh;
  168. bh = gfs2_getbuf(gl, blkno, CREATE);
  169. meta_prep_new(bh);
  170. return bh;
  171. }
  172. static void gfs2_meta_read_endio(struct bio *bio)
  173. {
  174. struct folio_iter fi;
  175. bio_for_each_folio_all(fi, bio) {
  176. struct folio *folio = fi.folio;
  177. struct buffer_head *bh = folio_buffers(folio);
  178. size_t len = fi.length;
  179. while (bh_offset(bh) < fi.offset)
  180. bh = bh->b_this_page;
  181. do {
  182. struct buffer_head *next = bh->b_this_page;
  183. len -= bh->b_size;
  184. bh->b_end_io(bh, !bio->bi_status);
  185. bh = next;
  186. } while (bh && len);
  187. }
  188. bio_put(bio);
  189. }
  190. /*
  191. * Submit several consecutive buffer head I/O requests as a single bio I/O
  192. * request. (See submit_bh_wbc.)
  193. */
  194. static void gfs2_submit_bhs(blk_opf_t opf, struct buffer_head *bhs[], int num)
  195. {
  196. while (num > 0) {
  197. struct buffer_head *bh = *bhs;
  198. struct bio *bio;
  199. bio = bio_alloc(bh->b_bdev, num, opf, GFP_NOIO);
  200. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> SECTOR_SHIFT);
  201. while (num > 0) {
  202. bh = *bhs;
  203. if (!bio_add_folio(bio, bh->b_folio, bh->b_size, bh_offset(bh))) {
  204. BUG_ON(bio->bi_iter.bi_size == 0);
  205. break;
  206. }
  207. bhs++;
  208. num--;
  209. }
  210. bio->bi_end_io = gfs2_meta_read_endio;
  211. submit_bio(bio);
  212. }
  213. }
  214. /**
  215. * gfs2_meta_read - Read a block from disk
  216. * @gl: The glock covering the block
  217. * @blkno: The block number
  218. * @flags: flags
  219. * @rahead: Do read-ahead
  220. * @bhp: the place where the buffer is returned (NULL on failure)
  221. *
  222. * Returns: errno
  223. */
  224. int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
  225. int rahead, struct buffer_head **bhp)
  226. {
  227. struct gfs2_sbd *sdp = glock_sbd(gl);
  228. struct buffer_head *bh, *bhs[2];
  229. int num = 0;
  230. if (gfs2_withdrawn(sdp)) {
  231. *bhp = NULL;
  232. return -EIO;
  233. }
  234. *bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
  235. lock_buffer(bh);
  236. if (buffer_uptodate(bh)) {
  237. unlock_buffer(bh);
  238. flags &= ~DIO_WAIT;
  239. } else {
  240. bh->b_end_io = end_buffer_read_sync;
  241. get_bh(bh);
  242. bhs[num++] = bh;
  243. }
  244. if (rahead) {
  245. bh = gfs2_getbuf(gl, blkno + 1, CREATE);
  246. lock_buffer(bh);
  247. if (buffer_uptodate(bh)) {
  248. unlock_buffer(bh);
  249. brelse(bh);
  250. } else {
  251. bh->b_end_io = end_buffer_read_sync;
  252. bhs[num++] = bh;
  253. }
  254. }
  255. gfs2_submit_bhs(REQ_OP_READ | REQ_META | REQ_PRIO, bhs, num);
  256. if (!(flags & DIO_WAIT))
  257. return 0;
  258. bh = *bhp;
  259. wait_on_buffer(bh);
  260. if (unlikely(!buffer_uptodate(bh))) {
  261. struct gfs2_trans *tr = current->journal_info;
  262. if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
  263. gfs2_io_error_bh(sdp, bh);
  264. brelse(bh);
  265. *bhp = NULL;
  266. return -EIO;
  267. }
  268. return 0;
  269. }
  270. /**
  271. * gfs2_meta_wait - Reread a block from disk
  272. * @sdp: the filesystem
  273. * @bh: The block to wait for
  274. *
  275. * Returns: errno
  276. */
  277. int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
  278. {
  279. if (gfs2_withdrawn(sdp))
  280. return -EIO;
  281. wait_on_buffer(bh);
  282. if (!buffer_uptodate(bh)) {
  283. struct gfs2_trans *tr = current->journal_info;
  284. if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
  285. gfs2_io_error_bh(sdp, bh);
  286. return -EIO;
  287. }
  288. if (gfs2_withdrawn(sdp))
  289. return -EIO;
  290. return 0;
  291. }
  292. void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
  293. {
  294. struct address_space *mapping = bh->b_folio->mapping;
  295. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  296. struct gfs2_bufdata *bd = bh->b_private;
  297. struct gfs2_trans *tr = current->journal_info;
  298. int was_pinned = 0;
  299. if (test_clear_buffer_pinned(bh)) {
  300. trace_gfs2_pin(bd, 0);
  301. atomic_dec(&sdp->sd_log_pinned);
  302. list_del_init(&bd->bd_list);
  303. if (meta == REMOVE_META)
  304. tr->tr_num_buf_rm++;
  305. else
  306. tr->tr_num_databuf_rm++;
  307. set_bit(TR_TOUCHED, &tr->tr_flags);
  308. was_pinned = 1;
  309. brelse(bh);
  310. }
  311. if (bd) {
  312. if (bd->bd_tr) {
  313. gfs2_trans_add_revoke(sdp, bd);
  314. } else if (was_pinned) {
  315. bh->b_private = NULL;
  316. kmem_cache_free(gfs2_bufdata_cachep, bd);
  317. } else if (!list_empty(&bd->bd_ail_st_list) &&
  318. !list_empty(&bd->bd_ail_gl_list)) {
  319. gfs2_remove_from_ail(bd);
  320. }
  321. }
  322. clear_buffer_dirty(bh);
  323. clear_buffer_uptodate(bh);
  324. }
  325. /**
  326. * gfs2_ail1_wipe - remove deleted/freed buffers from the ail1 list
  327. * @sdp: superblock
  328. * @bstart: starting block address of buffers to remove
  329. * @blen: length of buffers to be removed
  330. *
  331. * This function is called from gfs2_journal wipe, whose job is to remove
  332. * buffers, corresponding to deleted blocks, from the journal. If we find any
  333. * bufdata elements on the system ail1 list, they haven't been written to
  334. * the journal yet. So we remove them.
  335. */
  336. static void gfs2_ail1_wipe(struct gfs2_sbd *sdp, u64 bstart, u32 blen)
  337. {
  338. struct gfs2_trans *tr, *s;
  339. struct gfs2_bufdata *bd, *bs;
  340. struct buffer_head *bh;
  341. u64 end = bstart + blen;
  342. gfs2_log_lock(sdp);
  343. spin_lock(&sdp->sd_ail_lock);
  344. list_for_each_entry_safe(tr, s, &sdp->sd_ail1_list, tr_list) {
  345. list_for_each_entry_safe(bd, bs, &tr->tr_ail1_list,
  346. bd_ail_st_list) {
  347. bh = bd->bd_bh;
  348. if (bh->b_blocknr < bstart || bh->b_blocknr >= end)
  349. continue;
  350. gfs2_remove_from_journal(bh, REMOVE_JDATA);
  351. }
  352. }
  353. spin_unlock(&sdp->sd_ail_lock);
  354. gfs2_log_unlock(sdp);
  355. }
  356. static struct buffer_head *gfs2_getjdatabuf(struct gfs2_inode *ip, u64 blkno)
  357. {
  358. struct address_space *mapping = ip->i_inode.i_mapping;
  359. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  360. struct folio *folio;
  361. struct buffer_head *bh;
  362. unsigned int shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  363. unsigned long index = blkno >> shift; /* convert block to page */
  364. unsigned int bufnum = blkno - (index << shift);
  365. folio = __filemap_get_folio(mapping, index, FGP_LOCK | FGP_ACCESSED, 0);
  366. if (IS_ERR(folio))
  367. return NULL;
  368. bh = folio_buffers(folio);
  369. if (bh)
  370. bh = get_nth_bh(bh, bufnum);
  371. folio_unlock(folio);
  372. folio_put(folio);
  373. return bh;
  374. }
  375. /**
  376. * gfs2_journal_wipe - make inode's buffers so they aren't dirty/pinned anymore
  377. * @ip: the inode who owns the buffers
  378. * @bstart: the first buffer in the run
  379. * @blen: the number of buffers in the run
  380. *
  381. */
  382. void gfs2_journal_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
  383. {
  384. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  385. struct buffer_head *bh;
  386. int ty;
  387. /* This can only happen during incomplete inode creation. */
  388. if (!ip->i_gl)
  389. return;
  390. gfs2_ail1_wipe(sdp, bstart, blen);
  391. while (blen) {
  392. ty = REMOVE_META;
  393. bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
  394. if (!bh && gfs2_is_jdata(ip)) {
  395. bh = gfs2_getjdatabuf(ip, bstart);
  396. ty = REMOVE_JDATA;
  397. }
  398. if (bh) {
  399. lock_buffer(bh);
  400. gfs2_log_lock(sdp);
  401. spin_lock(&sdp->sd_ail_lock);
  402. gfs2_remove_from_journal(bh, ty);
  403. spin_unlock(&sdp->sd_ail_lock);
  404. gfs2_log_unlock(sdp);
  405. unlock_buffer(bh);
  406. brelse(bh);
  407. }
  408. bstart++;
  409. blen--;
  410. }
  411. }
  412. /**
  413. * gfs2_meta_buffer - Get a metadata buffer
  414. * @ip: The GFS2 inode
  415. * @mtype: The block type (GFS2_METATYPE_*)
  416. * @num: The block number (device relative) of the buffer
  417. * @bhp: the buffer is returned here
  418. *
  419. * Returns: errno
  420. */
  421. int gfs2_meta_buffer(struct gfs2_inode *ip, u32 mtype, u64 num,
  422. struct buffer_head **bhp)
  423. {
  424. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  425. struct gfs2_glock *gl = ip->i_gl;
  426. struct buffer_head *bh;
  427. int ret = 0;
  428. int rahead = 0;
  429. if (num == ip->i_no_addr)
  430. rahead = ip->i_rahead;
  431. ret = gfs2_meta_read(gl, num, DIO_WAIT, rahead, &bh);
  432. if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
  433. brelse(bh);
  434. ret = -EIO;
  435. } else {
  436. *bhp = bh;
  437. }
  438. return ret;
  439. }
  440. /**
  441. * gfs2_meta_ra - start readahead on an extent of a file
  442. * @gl: the glock the blocks belong to
  443. * @dblock: the starting disk block
  444. * @extlen: the number of blocks in the extent
  445. *
  446. * returns: the first buffer in the extent
  447. */
  448. struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
  449. {
  450. struct gfs2_sbd *sdp = glock_sbd(gl);
  451. struct buffer_head *first_bh, *bh;
  452. u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
  453. sdp->sd_sb.sb_bsize_shift;
  454. BUG_ON(!extlen);
  455. if (max_ra < 1)
  456. max_ra = 1;
  457. if (extlen > max_ra)
  458. extlen = max_ra;
  459. first_bh = gfs2_getbuf(gl, dblock, CREATE);
  460. if (buffer_uptodate(first_bh))
  461. goto out;
  462. bh_read_nowait(first_bh, REQ_META | REQ_PRIO);
  463. dblock++;
  464. extlen--;
  465. while (extlen) {
  466. bh = gfs2_getbuf(gl, dblock, CREATE);
  467. bh_readahead(bh, REQ_RAHEAD | REQ_META | REQ_PRIO);
  468. brelse(bh);
  469. dblock++;
  470. extlen--;
  471. if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
  472. goto out;
  473. }
  474. wait_on_buffer(first_bh);
  475. out:
  476. return first_bh;
  477. }