xref: /openbmc/linux/fs/gfs2/aops.c (revision d6b412c5)
17336d0e6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b1e71b06SSteven Whitehouse /*
3b1e71b06SSteven Whitehouse  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4b1e71b06SSteven Whitehouse  * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
5b1e71b06SSteven Whitehouse  */
6b1e71b06SSteven Whitehouse 
7b1e71b06SSteven Whitehouse #include <linux/sched.h>
8b1e71b06SSteven Whitehouse #include <linux/slab.h>
9b1e71b06SSteven Whitehouse #include <linux/spinlock.h>
10b1e71b06SSteven Whitehouse #include <linux/completion.h>
11b1e71b06SSteven Whitehouse #include <linux/buffer_head.h>
12b1e71b06SSteven Whitehouse #include <linux/pagemap.h>
13b1e71b06SSteven Whitehouse #include <linux/pagevec.h>
14b1e71b06SSteven Whitehouse #include <linux/mpage.h>
15b1e71b06SSteven Whitehouse #include <linux/fs.h>
16b1e71b06SSteven Whitehouse #include <linux/writeback.h>
17b1e71b06SSteven Whitehouse #include <linux/swap.h>
18b1e71b06SSteven Whitehouse #include <linux/gfs2_ondisk.h>
19b1e71b06SSteven Whitehouse #include <linux/backing-dev.h>
20e2e40f2cSChristoph Hellwig #include <linux/uio.h>
21774016b2SSteven Whitehouse #include <trace/events/writeback.h>
2264bc06bbSAndreas Gruenbacher #include <linux/sched/signal.h>
23b1e71b06SSteven Whitehouse 
24b1e71b06SSteven Whitehouse #include "gfs2.h"
25b1e71b06SSteven Whitehouse #include "incore.h"
26b1e71b06SSteven Whitehouse #include "bmap.h"
27b1e71b06SSteven Whitehouse #include "glock.h"
28b1e71b06SSteven Whitehouse #include "inode.h"
29b1e71b06SSteven Whitehouse #include "log.h"
30b1e71b06SSteven Whitehouse #include "meta_io.h"
31b1e71b06SSteven Whitehouse #include "quota.h"
32b1e71b06SSteven Whitehouse #include "trans.h"
33b1e71b06SSteven Whitehouse #include "rgrp.h"
34b1e71b06SSteven Whitehouse #include "super.h"
35b1e71b06SSteven Whitehouse #include "util.h"
36b1e71b06SSteven Whitehouse #include "glops.h"
3764bc06bbSAndreas Gruenbacher #include "aops.h"
38b1e71b06SSteven Whitehouse 
39b1e71b06SSteven Whitehouse 
gfs2_trans_add_databufs(struct gfs2_inode * ip,struct folio * folio,size_t from,size_t len)40c1b0c3cfSAndreas Gruenbacher void gfs2_trans_add_databufs(struct gfs2_inode *ip, struct folio *folio,
41285e0fc9SMatthew Wilcox (Oracle) 			     size_t from, size_t len)
42b1e71b06SSteven Whitehouse {
43c1b0c3cfSAndreas Gruenbacher 	struct buffer_head *head = folio_buffers(folio);
44b1e71b06SSteven Whitehouse 	unsigned int bsize = head->b_size;
45b1e71b06SSteven Whitehouse 	struct buffer_head *bh;
46285e0fc9SMatthew Wilcox (Oracle) 	size_t to = from + len;
47285e0fc9SMatthew Wilcox (Oracle) 	size_t start, end;
48b1e71b06SSteven Whitehouse 
49b1e71b06SSteven Whitehouse 	for (bh = head, start = 0; bh != head || !start;
50b1e71b06SSteven Whitehouse 	     bh = bh->b_this_page, start = end) {
51b1e71b06SSteven Whitehouse 		end = start + bsize;
5288b65ce5SAndreas Gruenbacher 		if (end <= from)
53b1e71b06SSteven Whitehouse 			continue;
5488b65ce5SAndreas Gruenbacher 		if (start >= to)
5588b65ce5SAndreas Gruenbacher 			break;
56b1e71b06SSteven Whitehouse 		set_buffer_uptodate(bh);
57350a9b0aSSteven Whitehouse 		gfs2_trans_add_data(ip->i_gl, bh);
58b1e71b06SSteven Whitehouse 	}
59b1e71b06SSteven Whitehouse }
60b1e71b06SSteven Whitehouse 
61b1e71b06SSteven Whitehouse /**
62b1e71b06SSteven Whitehouse  * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
63b1e71b06SSteven Whitehouse  * @inode: The inode
64b1e71b06SSteven Whitehouse  * @lblock: The block number to look up
65b1e71b06SSteven Whitehouse  * @bh_result: The buffer head to return the result in
66b1e71b06SSteven Whitehouse  * @create: Non-zero if we may add block to the file
67b1e71b06SSteven Whitehouse  *
68b1e71b06SSteven Whitehouse  * Returns: errno
69b1e71b06SSteven Whitehouse  */
70b1e71b06SSteven Whitehouse 
gfs2_get_block_noalloc(struct inode * inode,sector_t lblock,struct buffer_head * bh_result,int create)71b1e71b06SSteven Whitehouse static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
72b1e71b06SSteven Whitehouse 				  struct buffer_head *bh_result, int create)
73b1e71b06SSteven Whitehouse {
74b1e71b06SSteven Whitehouse 	int error;
75b1e71b06SSteven Whitehouse 
76b1e71b06SSteven Whitehouse 	error = gfs2_block_map(inode, lblock, bh_result, 0);
77b1e71b06SSteven Whitehouse 	if (error)
78b1e71b06SSteven Whitehouse 		return error;
79b1e71b06SSteven Whitehouse 	if (!buffer_mapped(bh_result))
804e79e3f0SBob Peterson 		return -ENODATA;
81b1e71b06SSteven Whitehouse 	return 0;
82b1e71b06SSteven Whitehouse }
83b1e71b06SSteven Whitehouse 
84b1e71b06SSteven Whitehouse /**
85c1401fd1SMatthew Wilcox (Oracle)  * gfs2_write_jdata_folio - gfs2 jdata-specific version of block_write_full_page
86c1401fd1SMatthew Wilcox (Oracle)  * @folio: The folio to write
8721b6924bSBob Peterson  * @wbc: The writeback control
8821b6924bSBob Peterson  *
8921b6924bSBob Peterson  * This is the same as calling block_write_full_page, but it also
90fd4c5748SBenjamin Marzinski  * writes pages outside of i_size
91fd4c5748SBenjamin Marzinski  */
gfs2_write_jdata_folio(struct folio * folio,struct writeback_control * wbc)92c1401fd1SMatthew Wilcox (Oracle) static int gfs2_write_jdata_folio(struct folio *folio,
93fd4c5748SBenjamin Marzinski 				 struct writeback_control *wbc)
94fd4c5748SBenjamin Marzinski {
95c1401fd1SMatthew Wilcox (Oracle) 	struct inode * const inode = folio->mapping->host;
96fd4c5748SBenjamin Marzinski 	loff_t i_size = i_size_read(inode);
97fd4c5748SBenjamin Marzinski 
98fd4c5748SBenjamin Marzinski 	/*
99c1401fd1SMatthew Wilcox (Oracle) 	 * The folio straddles i_size.  It must be zeroed out on each and every
100fd4c5748SBenjamin Marzinski 	 * writepage invocation because it may be mmapped.  "A file is mapped
101fd4c5748SBenjamin Marzinski 	 * in multiples of the page size.  For a file that is not a multiple of
102fd4c5748SBenjamin Marzinski 	 * the page size, the remaining memory is zeroed when mapped, and
103fd4c5748SBenjamin Marzinski 	 * writes to that region are not written out to the file."
104fd4c5748SBenjamin Marzinski 	 */
105c1401fd1SMatthew Wilcox (Oracle) 	if (folio_pos(folio) < i_size &&
106c1401fd1SMatthew Wilcox (Oracle) 	    i_size < folio_pos(folio) + folio_size(folio))
107c1401fd1SMatthew Wilcox (Oracle) 		folio_zero_segment(folio, offset_in_folio(folio, i_size),
108c1401fd1SMatthew Wilcox (Oracle) 				folio_size(folio));
109fd4c5748SBenjamin Marzinski 
11053418a18SMatthew Wilcox (Oracle) 	return __block_write_full_folio(inode, folio, gfs2_get_block_noalloc,
11153418a18SMatthew Wilcox (Oracle) 			wbc, end_buffer_async_write);
112fd4c5748SBenjamin Marzinski }
113fd4c5748SBenjamin Marzinski 
114b1e71b06SSteven Whitehouse /**
115d0cfcaeeSMatthew Wilcox (Oracle)  * __gfs2_jdata_write_folio - The core of jdata writepage
116d0cfcaeeSMatthew Wilcox (Oracle)  * @folio: The folio to write
117b1e71b06SSteven Whitehouse  * @wbc: The writeback control
118b1e71b06SSteven Whitehouse  *
119b1e71b06SSteven Whitehouse  * This is shared between writepage and writepages and implements the
120b1e71b06SSteven Whitehouse  * core of the writepage operation. If a transaction is required then
121d0cfcaeeSMatthew Wilcox (Oracle)  * the checked flag will have been set and the transaction will have
122b1e71b06SSteven Whitehouse  * already been started before this is called.
123b1e71b06SSteven Whitehouse  */
__gfs2_jdata_write_folio(struct folio * folio,struct writeback_control * wbc)124d0cfcaeeSMatthew Wilcox (Oracle) static int __gfs2_jdata_write_folio(struct folio *folio,
125d0cfcaeeSMatthew Wilcox (Oracle) 		struct writeback_control *wbc)
126b1e71b06SSteven Whitehouse {
127d0cfcaeeSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
128b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(inode);
129b1e71b06SSteven Whitehouse 
130d0cfcaeeSMatthew Wilcox (Oracle) 	if (folio_test_checked(folio)) {
131d0cfcaeeSMatthew Wilcox (Oracle) 		folio_clear_checked(folio);
132d0cfcaeeSMatthew Wilcox (Oracle) 		if (!folio_buffers(folio)) {
133d0cfcaeeSMatthew Wilcox (Oracle) 			folio_create_empty_buffers(folio,
134d0cfcaeeSMatthew Wilcox (Oracle) 					inode->i_sb->s_blocksize,
13547a9a527SFabian Frederick 					BIT(BH_Dirty)|BIT(BH_Uptodate));
136b1e71b06SSteven Whitehouse 		}
137d0cfcaeeSMatthew Wilcox (Oracle) 		gfs2_trans_add_databufs(ip, folio, 0, folio_size(folio));
138b1e71b06SSteven Whitehouse 	}
139c1401fd1SMatthew Wilcox (Oracle) 	return gfs2_write_jdata_folio(folio, wbc);
140b1e71b06SSteven Whitehouse }
141b1e71b06SSteven Whitehouse 
142b1e71b06SSteven Whitehouse /**
143b1e71b06SSteven Whitehouse  * gfs2_jdata_writepage - Write complete page
144b1e71b06SSteven Whitehouse  * @page: Page to write
1451272574bSFabian Frederick  * @wbc: The writeback control
146b1e71b06SSteven Whitehouse  *
147b1e71b06SSteven Whitehouse  * Returns: errno
148b1e71b06SSteven Whitehouse  *
149b1e71b06SSteven Whitehouse  */
150b1e71b06SSteven Whitehouse 
gfs2_jdata_writepage(struct page * page,struct writeback_control * wbc)151b1e71b06SSteven Whitehouse static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
152b1e71b06SSteven Whitehouse {
153c0ba597dSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
154b1e71b06SSteven Whitehouse 	struct inode *inode = page->mapping->host;
155fd4c5748SBenjamin Marzinski 	struct gfs2_inode *ip = GFS2_I(inode);
156b1e71b06SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(inode);
157b1e71b06SSteven Whitehouse 
158fd4c5748SBenjamin Marzinski 	if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
159fd4c5748SBenjamin Marzinski 		goto out;
160c0ba597dSMatthew Wilcox (Oracle) 	if (folio_test_checked(folio) || current->journal_info)
161b1e71b06SSteven Whitehouse 		goto out_ignore;
162d0cfcaeeSMatthew Wilcox (Oracle) 	return __gfs2_jdata_write_folio(folio, wbc);
163b1e71b06SSteven Whitehouse 
164b1e71b06SSteven Whitehouse out_ignore:
165c0ba597dSMatthew Wilcox (Oracle) 	folio_redirty_for_writepage(wbc, folio);
166fd4c5748SBenjamin Marzinski out:
167c0ba597dSMatthew Wilcox (Oracle) 	folio_unlock(folio);
168b1e71b06SSteven Whitehouse 	return 0;
169b1e71b06SSteven Whitehouse }
170b1e71b06SSteven Whitehouse 
171b1e71b06SSteven Whitehouse /**
17245138990SSteven Whitehouse  * gfs2_writepages - Write a bunch of dirty pages back to disk
173b1e71b06SSteven Whitehouse  * @mapping: The mapping to write
174b1e71b06SSteven Whitehouse  * @wbc: Write-back control
175b1e71b06SSteven Whitehouse  *
17645138990SSteven Whitehouse  * Used for both ordered and writeback modes.
177b1e71b06SSteven Whitehouse  */
gfs2_writepages(struct address_space * mapping,struct writeback_control * wbc)17845138990SSteven Whitehouse static int gfs2_writepages(struct address_space *mapping,
179b1e71b06SSteven Whitehouse 			   struct writeback_control *wbc)
180b1e71b06SSteven Whitehouse {
181b066a4eeSAbhi Das 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
1822164f9b9SChristoph Hellwig 	struct iomap_writepage_ctx wpc = { };
1832164f9b9SChristoph Hellwig 	int ret;
184b066a4eeSAbhi Das 
185b066a4eeSAbhi Das 	/*
186b74cd55aSAndreas Gruenbacher 	 * Even if we didn't write enough pages here, we might still be holding
187b066a4eeSAbhi Das 	 * dirty pages in the ail. We forcibly flush the ail because we don't
188b066a4eeSAbhi Das 	 * want balance_dirty_pages() to loop indefinitely trying to write out
189b066a4eeSAbhi Das 	 * pages held in the ail that it can't find.
190b066a4eeSAbhi Das 	 */
1912164f9b9SChristoph Hellwig 	ret = iomap_writepages(mapping, wbc, &wpc, &gfs2_writeback_ops);
192b74cd55aSAndreas Gruenbacher 	if (ret == 0 && wbc->nr_to_write > 0)
193b066a4eeSAbhi Das 		set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
194b066a4eeSAbhi Das 	return ret;
195b1e71b06SSteven Whitehouse }
196b1e71b06SSteven Whitehouse 
197b1e71b06SSteven Whitehouse /**
19887ed37e6SVishal Moola (Oracle)  * gfs2_write_jdata_batch - Write back a folio batch's worth of folios
199b1e71b06SSteven Whitehouse  * @mapping: The mapping
200b1e71b06SSteven Whitehouse  * @wbc: The writeback control
20187ed37e6SVishal Moola (Oracle)  * @fbatch: The batch of folios
2021272574bSFabian Frederick  * @done_index: Page index
203b1e71b06SSteven Whitehouse  *
204b1e71b06SSteven Whitehouse  * Returns: non-zero if loop should terminate, zero otherwise
205b1e71b06SSteven Whitehouse  */
206b1e71b06SSteven Whitehouse 
gfs2_write_jdata_batch(struct address_space * mapping,struct writeback_control * wbc,struct folio_batch * fbatch,pgoff_t * done_index)20787ed37e6SVishal Moola (Oracle) static int gfs2_write_jdata_batch(struct address_space *mapping,
208b1e71b06SSteven Whitehouse 				    struct writeback_control *wbc,
20987ed37e6SVishal Moola (Oracle) 				    struct folio_batch *fbatch,
210774016b2SSteven Whitehouse 				    pgoff_t *done_index)
211b1e71b06SSteven Whitehouse {
212b1e71b06SSteven Whitehouse 	struct inode *inode = mapping->host;
213b1e71b06SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(inode);
21487ed37e6SVishal Moola (Oracle) 	unsigned nrblocks;
215b1e71b06SSteven Whitehouse 	int i;
216b1e71b06SSteven Whitehouse 	int ret;
21787ed37e6SVishal Moola (Oracle) 	int nr_pages = 0;
21887ed37e6SVishal Moola (Oracle) 	int nr_folios = folio_batch_count(fbatch);
21987ed37e6SVishal Moola (Oracle) 
22087ed37e6SVishal Moola (Oracle) 	for (i = 0; i < nr_folios; i++)
22187ed37e6SVishal Moola (Oracle) 		nr_pages += folio_nr_pages(fbatch->folios[i]);
22287ed37e6SVishal Moola (Oracle) 	nrblocks = nr_pages * (PAGE_SIZE >> inode->i_blkbits);
223b1e71b06SSteven Whitehouse 
224b1e71b06SSteven Whitehouse 	ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
225b1e71b06SSteven Whitehouse 	if (ret < 0)
226b1e71b06SSteven Whitehouse 		return ret;
227b1e71b06SSteven Whitehouse 
22887ed37e6SVishal Moola (Oracle) 	for (i = 0; i < nr_folios; i++) {
22987ed37e6SVishal Moola (Oracle) 		struct folio *folio = fbatch->folios[i];
230b1e71b06SSteven Whitehouse 
23187ed37e6SVishal Moola (Oracle) 		*done_index = folio->index;
232774016b2SSteven Whitehouse 
23387ed37e6SVishal Moola (Oracle) 		folio_lock(folio);
234b1e71b06SSteven Whitehouse 
23587ed37e6SVishal Moola (Oracle) 		if (unlikely(folio->mapping != mapping)) {
236774016b2SSteven Whitehouse continue_unlock:
23787ed37e6SVishal Moola (Oracle) 			folio_unlock(folio);
238b1e71b06SSteven Whitehouse 			continue;
239b1e71b06SSteven Whitehouse 		}
240b1e71b06SSteven Whitehouse 
24187ed37e6SVishal Moola (Oracle) 		if (!folio_test_dirty(folio)) {
242774016b2SSteven Whitehouse 			/* someone wrote it for us */
243774016b2SSteven Whitehouse 			goto continue_unlock;
244b1e71b06SSteven Whitehouse 		}
245b1e71b06SSteven Whitehouse 
24687ed37e6SVishal Moola (Oracle) 		if (folio_test_writeback(folio)) {
247b1e71b06SSteven Whitehouse 			if (wbc->sync_mode != WB_SYNC_NONE)
24887ed37e6SVishal Moola (Oracle) 				folio_wait_writeback(folio);
249774016b2SSteven Whitehouse 			else
250774016b2SSteven Whitehouse 				goto continue_unlock;
251b1e71b06SSteven Whitehouse 		}
252b1e71b06SSteven Whitehouse 
25387ed37e6SVishal Moola (Oracle) 		BUG_ON(folio_test_writeback(folio));
25487ed37e6SVishal Moola (Oracle) 		if (!folio_clear_dirty_for_io(folio))
255774016b2SSteven Whitehouse 			goto continue_unlock;
256774016b2SSteven Whitehouse 
257de1414a6SChristoph Hellwig 		trace_wbc_writepage(wbc, inode_to_bdi(inode));
258b1e71b06SSteven Whitehouse 
259d0cfcaeeSMatthew Wilcox (Oracle) 		ret = __gfs2_jdata_write_folio(folio, wbc);
260774016b2SSteven Whitehouse 		if (unlikely(ret)) {
261774016b2SSteven Whitehouse 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
26287ed37e6SVishal Moola (Oracle) 				folio_unlock(folio);
263774016b2SSteven Whitehouse 				ret = 0;
264774016b2SSteven Whitehouse 			} else {
265b1e71b06SSteven Whitehouse 
266774016b2SSteven Whitehouse 				/*
267774016b2SSteven Whitehouse 				 * done_index is set past this page,
268774016b2SSteven Whitehouse 				 * so media errors will not choke
269774016b2SSteven Whitehouse 				 * background writeout for the entire
270774016b2SSteven Whitehouse 				 * file. This has consequences for
271774016b2SSteven Whitehouse 				 * range_cyclic semantics (ie. it may
272774016b2SSteven Whitehouse 				 * not be suitable for data integrity
273774016b2SSteven Whitehouse 				 * writeout).
274774016b2SSteven Whitehouse 				 */
2755f02d168SMinjie Du 				*done_index = folio_next_index(folio);
276b1e71b06SSteven Whitehouse 				ret = 1;
277774016b2SSteven Whitehouse 				break;
278774016b2SSteven Whitehouse 			}
279774016b2SSteven Whitehouse 		}
280774016b2SSteven Whitehouse 
281774016b2SSteven Whitehouse 		/*
282774016b2SSteven Whitehouse 		 * We stop writing back only if we are not doing
283774016b2SSteven Whitehouse 		 * integrity sync. In case of integrity sync we have to
284774016b2SSteven Whitehouse 		 * keep going until we have written all the pages
285774016b2SSteven Whitehouse 		 * we tagged for writeback prior to entering this loop.
286774016b2SSteven Whitehouse 		 */
287774016b2SSteven Whitehouse 		if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
288774016b2SSteven Whitehouse 			ret = 1;
289774016b2SSteven Whitehouse 			break;
290774016b2SSteven Whitehouse 		}
291774016b2SSteven Whitehouse 
292b1e71b06SSteven Whitehouse 	}
293b1e71b06SSteven Whitehouse 	gfs2_trans_end(sdp);
294b1e71b06SSteven Whitehouse 	return ret;
295b1e71b06SSteven Whitehouse }
296b1e71b06SSteven Whitehouse 
297b1e71b06SSteven Whitehouse /**
298b1e71b06SSteven Whitehouse  * gfs2_write_cache_jdata - Like write_cache_pages but different
299b1e71b06SSteven Whitehouse  * @mapping: The mapping to write
300b1e71b06SSteven Whitehouse  * @wbc: The writeback control
301b1e71b06SSteven Whitehouse  *
302b1e71b06SSteven Whitehouse  * The reason that we use our own function here is that we need to
303b1e71b06SSteven Whitehouse  * start transactions before we grab page locks. This allows us
304b1e71b06SSteven Whitehouse  * to get the ordering right.
305b1e71b06SSteven Whitehouse  */
306b1e71b06SSteven Whitehouse 
gfs2_write_cache_jdata(struct address_space * mapping,struct writeback_control * wbc)307b1e71b06SSteven Whitehouse static int gfs2_write_cache_jdata(struct address_space *mapping,
308b1e71b06SSteven Whitehouse 				  struct writeback_control *wbc)
309b1e71b06SSteven Whitehouse {
310b1e71b06SSteven Whitehouse 	int ret = 0;
311b1e71b06SSteven Whitehouse 	int done = 0;
31287ed37e6SVishal Moola (Oracle) 	struct folio_batch fbatch;
31387ed37e6SVishal Moola (Oracle) 	int nr_folios;
3143f649ab7SKees Cook 	pgoff_t writeback_index;
315b1e71b06SSteven Whitehouse 	pgoff_t index;
316b1e71b06SSteven Whitehouse 	pgoff_t end;
317774016b2SSteven Whitehouse 	pgoff_t done_index;
318774016b2SSteven Whitehouse 	int cycled;
319b1e71b06SSteven Whitehouse 	int range_whole = 0;
32010bbd235SMatthew Wilcox 	xa_mark_t tag;
321b1e71b06SSteven Whitehouse 
32287ed37e6SVishal Moola (Oracle) 	folio_batch_init(&fbatch);
323b1e71b06SSteven Whitehouse 	if (wbc->range_cyclic) {
324774016b2SSteven Whitehouse 		writeback_index = mapping->writeback_index; /* prev offset */
325774016b2SSteven Whitehouse 		index = writeback_index;
326774016b2SSteven Whitehouse 		if (index == 0)
327774016b2SSteven Whitehouse 			cycled = 1;
328774016b2SSteven Whitehouse 		else
329774016b2SSteven Whitehouse 			cycled = 0;
330b1e71b06SSteven Whitehouse 		end = -1;
331b1e71b06SSteven Whitehouse 	} else {
33209cbfeafSKirill A. Shutemov 		index = wbc->range_start >> PAGE_SHIFT;
33309cbfeafSKirill A. Shutemov 		end = wbc->range_end >> PAGE_SHIFT;
334b1e71b06SSteven Whitehouse 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
335b1e71b06SSteven Whitehouse 			range_whole = 1;
336774016b2SSteven Whitehouse 		cycled = 1; /* ignore range_cyclic tests */
337b1e71b06SSteven Whitehouse 	}
338774016b2SSteven Whitehouse 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
339774016b2SSteven Whitehouse 		tag = PAGECACHE_TAG_TOWRITE;
340774016b2SSteven Whitehouse 	else
341774016b2SSteven Whitehouse 		tag = PAGECACHE_TAG_DIRTY;
342b1e71b06SSteven Whitehouse 
343b1e71b06SSteven Whitehouse retry:
344774016b2SSteven Whitehouse 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
345774016b2SSteven Whitehouse 		tag_pages_for_writeback(mapping, index, end);
346774016b2SSteven Whitehouse 	done_index = index;
347774016b2SSteven Whitehouse 	while (!done && (index <= end)) {
34887ed37e6SVishal Moola (Oracle) 		nr_folios = filemap_get_folios_tag(mapping, &index, end,
34987ed37e6SVishal Moola (Oracle) 				tag, &fbatch);
35087ed37e6SVishal Moola (Oracle) 		if (nr_folios == 0)
351774016b2SSteven Whitehouse 			break;
352774016b2SSteven Whitehouse 
35387ed37e6SVishal Moola (Oracle) 		ret = gfs2_write_jdata_batch(mapping, wbc, &fbatch,
35487ed37e6SVishal Moola (Oracle) 				&done_index);
355b1e71b06SSteven Whitehouse 		if (ret)
356b1e71b06SSteven Whitehouse 			done = 1;
357b1e71b06SSteven Whitehouse 		if (ret > 0)
358b1e71b06SSteven Whitehouse 			ret = 0;
35987ed37e6SVishal Moola (Oracle) 		folio_batch_release(&fbatch);
360b1e71b06SSteven Whitehouse 		cond_resched();
361b1e71b06SSteven Whitehouse 	}
362b1e71b06SSteven Whitehouse 
363774016b2SSteven Whitehouse 	if (!cycled && !done) {
364b1e71b06SSteven Whitehouse 		/*
365774016b2SSteven Whitehouse 		 * range_cyclic:
366b1e71b06SSteven Whitehouse 		 * We hit the last page and there is more work to be done: wrap
367b1e71b06SSteven Whitehouse 		 * back to the start of the file
368b1e71b06SSteven Whitehouse 		 */
369774016b2SSteven Whitehouse 		cycled = 1;
370b1e71b06SSteven Whitehouse 		index = 0;
371774016b2SSteven Whitehouse 		end = writeback_index - 1;
372b1e71b06SSteven Whitehouse 		goto retry;
373b1e71b06SSteven Whitehouse 	}
374b1e71b06SSteven Whitehouse 
375b1e71b06SSteven Whitehouse 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
376774016b2SSteven Whitehouse 		mapping->writeback_index = done_index;
377774016b2SSteven Whitehouse 
378b1e71b06SSteven Whitehouse 	return ret;
379b1e71b06SSteven Whitehouse }
380b1e71b06SSteven Whitehouse 
381b1e71b06SSteven Whitehouse 
382b1e71b06SSteven Whitehouse /**
383b1e71b06SSteven Whitehouse  * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
384b1e71b06SSteven Whitehouse  * @mapping: The mapping to write
385b1e71b06SSteven Whitehouse  * @wbc: The writeback control
386b1e71b06SSteven Whitehouse  *
387b1e71b06SSteven Whitehouse  */
388b1e71b06SSteven Whitehouse 
gfs2_jdata_writepages(struct address_space * mapping,struct writeback_control * wbc)389b1e71b06SSteven Whitehouse static int gfs2_jdata_writepages(struct address_space *mapping,
390b1e71b06SSteven Whitehouse 				 struct writeback_control *wbc)
391b1e71b06SSteven Whitehouse {
392b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(mapping->host);
393b1e71b06SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
394b1e71b06SSteven Whitehouse 	int ret;
395b1e71b06SSteven Whitehouse 
396b1e71b06SSteven Whitehouse 	ret = gfs2_write_cache_jdata(mapping, wbc);
397b1e71b06SSteven Whitehouse 	if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
398805c0907SBob Peterson 		gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
399805c0907SBob Peterson 			       GFS2_LFC_JDATA_WPAGES);
400b1e71b06SSteven Whitehouse 		ret = gfs2_write_cache_jdata(mapping, wbc);
401b1e71b06SSteven Whitehouse 	}
402b1e71b06SSteven Whitehouse 	return ret;
403b1e71b06SSteven Whitehouse }
404b1e71b06SSteven Whitehouse 
405b1e71b06SSteven Whitehouse /**
406b1e71b06SSteven Whitehouse  * stuffed_readpage - Fill in a Linux page with stuffed file data
407b1e71b06SSteven Whitehouse  * @ip: the inode
408b1e71b06SSteven Whitehouse  * @page: the page
409b1e71b06SSteven Whitehouse  *
410b1e71b06SSteven Whitehouse  * Returns: errno
411b1e71b06SSteven Whitehouse  */
stuffed_readpage(struct gfs2_inode * ip,struct page * page)412378b6cbfSChristoph Hellwig static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
413b1e71b06SSteven Whitehouse {
414b1e71b06SSteven Whitehouse 	struct buffer_head *dibh;
415602c89d2SSteven Whitehouse 	u64 dsize = i_size_read(&ip->i_inode);
416b1e71b06SSteven Whitehouse 	void *kaddr;
417b1e71b06SSteven Whitehouse 	int error;
418b1e71b06SSteven Whitehouse 
419b1e71b06SSteven Whitehouse 	/*
420b1e71b06SSteven Whitehouse 	 * Due to the order of unstuffing files and ->fault(), we can be
421b1e71b06SSteven Whitehouse 	 * asked for a zero page in the case of a stuffed file being extended,
422b1e71b06SSteven Whitehouse 	 * so we need to supply one here. It doesn't happen often.
423b1e71b06SSteven Whitehouse 	 */
424b1e71b06SSteven Whitehouse 	if (unlikely(page->index)) {
42509cbfeafSKirill A. Shutemov 		zero_user(page, 0, PAGE_SIZE);
426b1e71b06SSteven Whitehouse 		SetPageUptodate(page);
427b1e71b06SSteven Whitehouse 		return 0;
428b1e71b06SSteven Whitehouse 	}
429b1e71b06SSteven Whitehouse 
430b1e71b06SSteven Whitehouse 	error = gfs2_meta_inode_buffer(ip, &dibh);
431b1e71b06SSteven Whitehouse 	if (error)
432b1e71b06SSteven Whitehouse 		return error;
433b1e71b06SSteven Whitehouse 
43458721bd4SDeepak R Varma 	kaddr = kmap_local_page(page);
435602c89d2SSteven Whitehouse 	memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
43609cbfeafSKirill A. Shutemov 	memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
43758721bd4SDeepak R Varma 	kunmap_local(kaddr);
438b1e71b06SSteven Whitehouse 	flush_dcache_page(page);
439b1e71b06SSteven Whitehouse 	brelse(dibh);
440b1e71b06SSteven Whitehouse 	SetPageUptodate(page);
441b1e71b06SSteven Whitehouse 
442b1e71b06SSteven Whitehouse 	return 0;
443b1e71b06SSteven Whitehouse }
444b1e71b06SSteven Whitehouse 
445e9b5b23eSMatthew Wilcox (Oracle) /**
446e9b5b23eSMatthew Wilcox (Oracle)  * gfs2_read_folio - read a folio from a file
447e9b5b23eSMatthew Wilcox (Oracle)  * @file: The file to read
448e9b5b23eSMatthew Wilcox (Oracle)  * @folio: The folio in the file
449e9b5b23eSMatthew Wilcox (Oracle)  */
gfs2_read_folio(struct file * file,struct folio * folio)450e9b5b23eSMatthew Wilcox (Oracle) static int gfs2_read_folio(struct file *file, struct folio *folio)
451b1e71b06SSteven Whitehouse {
452e9b5b23eSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
4532164f9b9SChristoph Hellwig 	struct gfs2_inode *ip = GFS2_I(inode);
4542164f9b9SChristoph Hellwig 	struct gfs2_sbd *sdp = GFS2_SB(inode);
455b1e71b06SSteven Whitehouse 	int error;
456b1e71b06SSteven Whitehouse 
4572164f9b9SChristoph Hellwig 	if (!gfs2_is_jdata(ip) ||
458e9b5b23eSMatthew Wilcox (Oracle) 	    (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
4597479c505SMatthew Wilcox (Oracle) 		error = iomap_read_folio(folio, &gfs2_iomap_ops);
460f95cbb44SAndreas Gruenbacher 	} else if (gfs2_is_stuffed(ip)) {
461e9b5b23eSMatthew Wilcox (Oracle) 		error = stuffed_readpage(ip, &folio->page);
462e9b5b23eSMatthew Wilcox (Oracle) 		folio_unlock(folio);
463b1e71b06SSteven Whitehouse 	} else {
464f132ab7dSMatthew Wilcox (Oracle) 		error = mpage_read_folio(folio, gfs2_block_map);
465b1e71b06SSteven Whitehouse 	}
466b1e71b06SSteven Whitehouse 
467d6b412c5SAndreas Gruenbacher 	if (gfs2_withdrawing_or_withdrawn(sdp))
468b1e71b06SSteven Whitehouse 		return -EIO;
469b1e71b06SSteven Whitehouse 
470b1e71b06SSteven Whitehouse 	return error;
471b1e71b06SSteven Whitehouse }
472b1e71b06SSteven Whitehouse 
473b1e71b06SSteven Whitehouse /**
474b1e71b06SSteven Whitehouse  * gfs2_internal_read - read an internal file
475b1e71b06SSteven Whitehouse  * @ip: The gfs2 inode
476b1e71b06SSteven Whitehouse  * @buf: The buffer to fill
477b1e71b06SSteven Whitehouse  * @pos: The file position
478b1e71b06SSteven Whitehouse  * @size: The amount to read
479b1e71b06SSteven Whitehouse  *
480b1e71b06SSteven Whitehouse  */
481b1e71b06SSteven Whitehouse 
gfs2_internal_read(struct gfs2_inode * ip,char * buf,loff_t * pos,size_t size)4829db1bdd7SAndreas Gruenbacher ssize_t gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
4839db1bdd7SAndreas Gruenbacher 			   size_t size)
484b1e71b06SSteven Whitehouse {
485b1e71b06SSteven Whitehouse 	struct address_space *mapping = ip->i_inode.i_mapping;
48645eb0504SAndreas Gruenbacher 	unsigned long index = *pos >> PAGE_SHIFT;
4879db1bdd7SAndreas Gruenbacher 	size_t copied = 0;
488b1e71b06SSteven Whitehouse 
489b1e71b06SSteven Whitehouse 	do {
4909db1bdd7SAndreas Gruenbacher 		size_t offset, chunk;
4919db1bdd7SAndreas Gruenbacher 		struct folio *folio;
4929db1bdd7SAndreas Gruenbacher 
4939db1bdd7SAndreas Gruenbacher 		folio = read_cache_folio(mapping, index, gfs2_read_folio, NULL);
4949db1bdd7SAndreas Gruenbacher 		if (IS_ERR(folio)) {
4959db1bdd7SAndreas Gruenbacher 			if (PTR_ERR(folio) == -EINTR)
496cea44032SAndreas Gruenbacher 				continue;
4979db1bdd7SAndreas Gruenbacher 			return PTR_ERR(folio);
498cea44032SAndreas Gruenbacher 		}
4999db1bdd7SAndreas Gruenbacher 		offset = *pos + copied - folio_pos(folio);
5009db1bdd7SAndreas Gruenbacher 		chunk = min(size - copied, folio_size(folio) - offset);
5019db1bdd7SAndreas Gruenbacher 		memcpy_from_folio(buf + copied, folio, offset, chunk);
5029db1bdd7SAndreas Gruenbacher 		index = folio_next_index(folio);
5039db1bdd7SAndreas Gruenbacher 		folio_put(folio);
5049db1bdd7SAndreas Gruenbacher 		copied += chunk;
505b1e71b06SSteven Whitehouse 	} while(copied < size);
506b1e71b06SSteven Whitehouse 	(*pos) += size;
507b1e71b06SSteven Whitehouse 	return size;
508b1e71b06SSteven Whitehouse }
509b1e71b06SSteven Whitehouse 
510b1e71b06SSteven Whitehouse /**
511d4388340SMatthew Wilcox (Oracle)  * gfs2_readahead - Read a bunch of pages at once
512c551f66cSLee Jones  * @rac: Read-ahead control structure
513b1e71b06SSteven Whitehouse  *
514b1e71b06SSteven Whitehouse  * Some notes:
515b1e71b06SSteven Whitehouse  * 1. This is only for readahead, so we can simply ignore any things
516b1e71b06SSteven Whitehouse  *    which are slightly inconvenient (such as locking conflicts between
517b1e71b06SSteven Whitehouse  *    the page lock and the glock) and return having done no I/O. Its
518b1e71b06SSteven Whitehouse  *    obviously not something we'd want to do on too regular a basis.
519b1e71b06SSteven Whitehouse  *    Any I/O we ignore at this time will be done via readpage later.
520b1e71b06SSteven Whitehouse  * 2. We don't handle stuffed files here we let readpage do the honours.
521d4388340SMatthew Wilcox (Oracle)  * 3. mpage_readahead() does most of the heavy lifting in the common case.
522b1e71b06SSteven Whitehouse  * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
523b1e71b06SSteven Whitehouse  */
524b1e71b06SSteven Whitehouse 
gfs2_readahead(struct readahead_control * rac)525d4388340SMatthew Wilcox (Oracle) static void gfs2_readahead(struct readahead_control *rac)
526b1e71b06SSteven Whitehouse {
527d4388340SMatthew Wilcox (Oracle) 	struct inode *inode = rac->mapping->host;
528b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(inode);
529b1e71b06SSteven Whitehouse 
5302164f9b9SChristoph Hellwig 	if (gfs2_is_stuffed(ip))
5312164f9b9SChristoph Hellwig 		;
5322164f9b9SChristoph Hellwig 	else if (gfs2_is_jdata(ip))
533d4388340SMatthew Wilcox (Oracle) 		mpage_readahead(rac, gfs2_block_map);
5342164f9b9SChristoph Hellwig 	else
5352164f9b9SChristoph Hellwig 		iomap_readahead(rac, &gfs2_iomap_ops);
536b1e71b06SSteven Whitehouse }
537b1e71b06SSteven Whitehouse 
538b1e71b06SSteven Whitehouse /**
539b1e71b06SSteven Whitehouse  * adjust_fs_space - Adjusts the free space available due to gfs2_grow
540b1e71b06SSteven Whitehouse  * @inode: the rindex inode
541b1e71b06SSteven Whitehouse  */
adjust_fs_space(struct inode * inode)54264bc06bbSAndreas Gruenbacher void adjust_fs_space(struct inode *inode)
543b1e71b06SSteven Whitehouse {
544d0a22a4bSAndreas Gruenbacher 	struct gfs2_sbd *sdp = GFS2_SB(inode);
5451946f70aSBenjamin Marzinski 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
546b1e71b06SSteven Whitehouse 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
547b1e71b06SSteven Whitehouse 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
54870c11ba8SBob Peterson 	struct buffer_head *m_bh;
549b1e71b06SSteven Whitehouse 	u64 fs_total, new_free;
550b1e71b06SSteven Whitehouse 
551d0a22a4bSAndreas Gruenbacher 	if (gfs2_trans_begin(sdp, 2 * RES_STATFS, 0) != 0)
552d0a22a4bSAndreas Gruenbacher 		return;
553d0a22a4bSAndreas Gruenbacher 
554b1e71b06SSteven Whitehouse 	/* Total up the file system space, according to the latest rindex. */
555b1e71b06SSteven Whitehouse 	fs_total = gfs2_ri_total(sdp);
5561946f70aSBenjamin Marzinski 	if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
557d0a22a4bSAndreas Gruenbacher 		goto out;
558b1e71b06SSteven Whitehouse 
559b1e71b06SSteven Whitehouse 	spin_lock(&sdp->sd_statfs_spin);
5601946f70aSBenjamin Marzinski 	gfs2_statfs_change_in(m_sc, m_bh->b_data +
5611946f70aSBenjamin Marzinski 			      sizeof(struct gfs2_dinode));
562b1e71b06SSteven Whitehouse 	if (fs_total > (m_sc->sc_total + l_sc->sc_total))
563b1e71b06SSteven Whitehouse 		new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
564b1e71b06SSteven Whitehouse 	else
565b1e71b06SSteven Whitehouse 		new_free = 0;
566b1e71b06SSteven Whitehouse 	spin_unlock(&sdp->sd_statfs_spin);
567b1e71b06SSteven Whitehouse 	fs_warn(sdp, "File system extended by %llu blocks.\n",
568b1e71b06SSteven Whitehouse 		(unsigned long long)new_free);
569b1e71b06SSteven Whitehouse 	gfs2_statfs_change(sdp, new_free, new_free, 0);
5701946f70aSBenjamin Marzinski 
57170c11ba8SBob Peterson 	update_statfs(sdp, m_bh);
5721946f70aSBenjamin Marzinski 	brelse(m_bh);
573d0a22a4bSAndreas Gruenbacher out:
574d0a22a4bSAndreas Gruenbacher 	sdp->sd_rindex_uptodate = 0;
575d0a22a4bSAndreas Gruenbacher 	gfs2_trans_end(sdp);
576b1e71b06SSteven Whitehouse }
577b1e71b06SSteven Whitehouse 
jdata_dirty_folio(struct address_space * mapping,struct folio * folio)578e621900aSMatthew Wilcox (Oracle) static bool jdata_dirty_folio(struct address_space *mapping,
579e621900aSMatthew Wilcox (Oracle) 		struct folio *folio)
580b1e71b06SSteven Whitehouse {
5816302d6f4SBob Peterson 	if (current->journal_info)
582e621900aSMatthew Wilcox (Oracle) 		folio_set_checked(folio);
583e621900aSMatthew Wilcox (Oracle) 	return block_dirty_folio(mapping, folio);
584b1e71b06SSteven Whitehouse }
585b1e71b06SSteven Whitehouse 
586b1e71b06SSteven Whitehouse /**
587b1e71b06SSteven Whitehouse  * gfs2_bmap - Block map function
588b1e71b06SSteven Whitehouse  * @mapping: Address space info
589b1e71b06SSteven Whitehouse  * @lblock: The block to map
590b1e71b06SSteven Whitehouse  *
591b1e71b06SSteven Whitehouse  * Returns: The disk address for the block or 0 on hole or error
592b1e71b06SSteven Whitehouse  */
593b1e71b06SSteven Whitehouse 
gfs2_bmap(struct address_space * mapping,sector_t lblock)594b1e71b06SSteven Whitehouse static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
595b1e71b06SSteven Whitehouse {
596b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(mapping->host);
597b1e71b06SSteven Whitehouse 	struct gfs2_holder i_gh;
598b1e71b06SSteven Whitehouse 	sector_t dblock = 0;
599b1e71b06SSteven Whitehouse 	int error;
600b1e71b06SSteven Whitehouse 
601b1e71b06SSteven Whitehouse 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
602b1e71b06SSteven Whitehouse 	if (error)
603b1e71b06SSteven Whitehouse 		return 0;
604b1e71b06SSteven Whitehouse 
605b1e71b06SSteven Whitehouse 	if (!gfs2_is_stuffed(ip))
6067770c93aSChristoph Hellwig 		dblock = iomap_bmap(mapping, lblock, &gfs2_iomap_ops);
607b1e71b06SSteven Whitehouse 
608b1e71b06SSteven Whitehouse 	gfs2_glock_dq_uninit(&i_gh);
609b1e71b06SSteven Whitehouse 
610b1e71b06SSteven Whitehouse 	return dblock;
611b1e71b06SSteven Whitehouse }
612b1e71b06SSteven Whitehouse 
gfs2_discard(struct gfs2_sbd * sdp,struct buffer_head * bh)613b1e71b06SSteven Whitehouse static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
614b1e71b06SSteven Whitehouse {
615b1e71b06SSteven Whitehouse 	struct gfs2_bufdata *bd;
616b1e71b06SSteven Whitehouse 
617b1e71b06SSteven Whitehouse 	lock_buffer(bh);
618b1e71b06SSteven Whitehouse 	gfs2_log_lock(sdp);
619b1e71b06SSteven Whitehouse 	clear_buffer_dirty(bh);
620b1e71b06SSteven Whitehouse 	bd = bh->b_private;
621b1e71b06SSteven Whitehouse 	if (bd) {
622c0752aa7SBob Peterson 		if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
623c0752aa7SBob Peterson 			list_del_init(&bd->bd_list);
62468942870SBob Peterson 		else {
62568942870SBob Peterson 			spin_lock(&sdp->sd_ail_lock);
62668cd4ce2SBob Peterson 			gfs2_remove_from_journal(bh, REMOVE_JDATA);
62768942870SBob Peterson 			spin_unlock(&sdp->sd_ail_lock);
62868942870SBob Peterson 		}
629b1e71b06SSteven Whitehouse 	}
630b1e71b06SSteven Whitehouse 	bh->b_bdev = NULL;
631b1e71b06SSteven Whitehouse 	clear_buffer_mapped(bh);
632b1e71b06SSteven Whitehouse 	clear_buffer_req(bh);
633b1e71b06SSteven Whitehouse 	clear_buffer_new(bh);
634b1e71b06SSteven Whitehouse 	gfs2_log_unlock(sdp);
635b1e71b06SSteven Whitehouse 	unlock_buffer(bh);
636b1e71b06SSteven Whitehouse }
637b1e71b06SSteven Whitehouse 
gfs2_invalidate_folio(struct folio * folio,size_t offset,size_t length)6385f4b2976SMatthew Wilcox (Oracle) static void gfs2_invalidate_folio(struct folio *folio, size_t offset,
6395f4b2976SMatthew Wilcox (Oracle) 				size_t length)
640b1e71b06SSteven Whitehouse {
6415f4b2976SMatthew Wilcox (Oracle) 	struct gfs2_sbd *sdp = GFS2_SB(folio->mapping->host);
6425f4b2976SMatthew Wilcox (Oracle) 	size_t stop = offset + length;
6435f4b2976SMatthew Wilcox (Oracle) 	int partial_page = (offset || length < folio_size(folio));
644b1e71b06SSteven Whitehouse 	struct buffer_head *bh, *head;
645b1e71b06SSteven Whitehouse 	unsigned long pos = 0;
646b1e71b06SSteven Whitehouse 
6475f4b2976SMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
6485c0bb97cSLukas Czerner 	if (!partial_page)
6495f4b2976SMatthew Wilcox (Oracle) 		folio_clear_checked(folio);
6505f4b2976SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
6515f4b2976SMatthew Wilcox (Oracle) 	if (!head)
652b1e71b06SSteven Whitehouse 		goto out;
653b1e71b06SSteven Whitehouse 
6545f4b2976SMatthew Wilcox (Oracle) 	bh = head;
655b1e71b06SSteven Whitehouse 	do {
6565c0bb97cSLukas Czerner 		if (pos + bh->b_size > stop)
6575c0bb97cSLukas Czerner 			return;
6585c0bb97cSLukas Czerner 
659b1e71b06SSteven Whitehouse 		if (offset <= pos)
660b1e71b06SSteven Whitehouse 			gfs2_discard(sdp, bh);
661b1e71b06SSteven Whitehouse 		pos += bh->b_size;
662b1e71b06SSteven Whitehouse 		bh = bh->b_this_page;
663b1e71b06SSteven Whitehouse 	} while (bh != head);
664b1e71b06SSteven Whitehouse out:
6655c0bb97cSLukas Czerner 	if (!partial_page)
6665f4b2976SMatthew Wilcox (Oracle) 		filemap_release_folio(folio, 0);
667b1e71b06SSteven Whitehouse }
668b1e71b06SSteven Whitehouse 
669b1e71b06SSteven Whitehouse /**
670e45c20d1SMatthew Wilcox (Oracle)  * gfs2_release_folio - free the metadata associated with a folio
671e45c20d1SMatthew Wilcox (Oracle)  * @folio: the folio that's being released
672b1e71b06SSteven Whitehouse  * @gfp_mask: passed from Linux VFS, ignored by us
673b1e71b06SSteven Whitehouse  *
674e45c20d1SMatthew Wilcox (Oracle)  * Calls try_to_free_buffers() to free the buffers and put the folio if the
6750ebbe4f9SAndreas Gruenbacher  * buffers can be released.
676b1e71b06SSteven Whitehouse  *
677e45c20d1SMatthew Wilcox (Oracle)  * Returns: true if the folio was put or else false
678b1e71b06SSteven Whitehouse  */
679b1e71b06SSteven Whitehouse 
gfs2_release_folio(struct folio * folio,gfp_t gfp_mask)680e45c20d1SMatthew Wilcox (Oracle) bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
681b1e71b06SSteven Whitehouse {
682e45c20d1SMatthew Wilcox (Oracle) 	struct address_space *mapping = folio->mapping;
683009d8518SSteven Whitehouse 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
684b1e71b06SSteven Whitehouse 	struct buffer_head *bh, *head;
685b1e71b06SSteven Whitehouse 	struct gfs2_bufdata *bd;
686b1e71b06SSteven Whitehouse 
687e45c20d1SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
688e45c20d1SMatthew Wilcox (Oracle) 	if (!head)
689e45c20d1SMatthew Wilcox (Oracle) 		return false;
690b1e71b06SSteven Whitehouse 
6911c185c02SAndreas Gruenbacher 	/*
692e45c20d1SMatthew Wilcox (Oracle) 	 * mm accommodates an old ext3 case where clean folios might
693e45c20d1SMatthew Wilcox (Oracle) 	 * not have had the dirty bit cleared.	Thus, it can send actual
694e45c20d1SMatthew Wilcox (Oracle) 	 * dirty folios to ->release_folio() via shrink_active_list().
6951c185c02SAndreas Gruenbacher 	 *
696e45c20d1SMatthew Wilcox (Oracle) 	 * As a workaround, we skip folios that contain dirty buffers
697e45c20d1SMatthew Wilcox (Oracle) 	 * below.  Once ->release_folio isn't called on dirty folios
698e45c20d1SMatthew Wilcox (Oracle) 	 * anymore, we can warn on dirty buffers like we used to here
699e45c20d1SMatthew Wilcox (Oracle) 	 * again.
7001c185c02SAndreas Gruenbacher 	 */
7011c185c02SAndreas Gruenbacher 
702b1e71b06SSteven Whitehouse 	gfs2_log_lock(sdp);
703e45c20d1SMatthew Wilcox (Oracle) 	bh = head;
704b1e71b06SSteven Whitehouse 	do {
705b1e71b06SSteven Whitehouse 		if (atomic_read(&bh->b_count))
706b1e71b06SSteven Whitehouse 			goto cannot_release;
707b1e71b06SSteven Whitehouse 		bd = bh->b_private;
70816ca9412SBenjamin Marzinski 		if (bd && bd->bd_tr)
709b1e71b06SSteven Whitehouse 			goto cannot_release;
7101c185c02SAndreas Gruenbacher 		if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
7111c185c02SAndreas Gruenbacher 			goto cannot_release;
712b1e71b06SSteven Whitehouse 		bh = bh->b_this_page;
713b1e71b06SSteven Whitehouse 	} while (bh != head);
714b1e71b06SSteven Whitehouse 
715e45c20d1SMatthew Wilcox (Oracle) 	bh = head;
716b1e71b06SSteven Whitehouse 	do {
717b1e71b06SSteven Whitehouse 		bd = bh->b_private;
718b1e71b06SSteven Whitehouse 		if (bd) {
719b1e71b06SSteven Whitehouse 			gfs2_assert_warn(sdp, bd->bd_bh == bh);
720b1e71b06SSteven Whitehouse 			bd->bd_bh = NULL;
721b1e71b06SSteven Whitehouse 			bh->b_private = NULL;
722019dd669SBob Peterson 			/*
723019dd669SBob Peterson 			 * The bd may still be queued as a revoke, in which
724019dd669SBob Peterson 			 * case we must not dequeue nor free it.
725019dd669SBob Peterson 			 */
726019dd669SBob Peterson 			if (!bd->bd_blkno && !list_empty(&bd->bd_list))
727019dd669SBob Peterson 				list_del_init(&bd->bd_list);
728019dd669SBob Peterson 			if (list_empty(&bd->bd_list))
729b1e71b06SSteven Whitehouse 				kmem_cache_free(gfs2_bufdata_cachep, bd);
730e4f29206SSteven Whitehouse 		}
731b1e71b06SSteven Whitehouse 
732b1e71b06SSteven Whitehouse 		bh = bh->b_this_page;
733b1e71b06SSteven Whitehouse 	} while (bh != head);
734e4f29206SSteven Whitehouse 	gfs2_log_unlock(sdp);
735b1e71b06SSteven Whitehouse 
73668189fefSMatthew Wilcox (Oracle) 	return try_to_free_buffers(folio);
7378f065d36SSteven Whitehouse 
738b1e71b06SSteven Whitehouse cannot_release:
739b1e71b06SSteven Whitehouse 	gfs2_log_unlock(sdp);
740e45c20d1SMatthew Wilcox (Oracle) 	return false;
741b1e71b06SSteven Whitehouse }
742b1e71b06SSteven Whitehouse 
743eadd7535SChristoph Hellwig static const struct address_space_operations gfs2_aops = {
74445138990SSteven Whitehouse 	.writepages = gfs2_writepages,
745f132ab7dSMatthew Wilcox (Oracle) 	.read_folio = gfs2_read_folio,
746d4388340SMatthew Wilcox (Oracle) 	.readahead = gfs2_readahead,
7474ce02c67SRitesh Harjani (IBM) 	.dirty_folio = iomap_dirty_folio,
7488597447dSMatthew Wilcox (Oracle) 	.release_folio = iomap_release_folio,
749d82354f6SMatthew Wilcox (Oracle) 	.invalidate_folio = iomap_invalidate_folio,
750b1e71b06SSteven Whitehouse 	.bmap = gfs2_bmap,
7512ec810d5SMatthew Wilcox (Oracle) 	.migrate_folio = filemap_migrate_folio,
7522164f9b9SChristoph Hellwig 	.is_partially_uptodate = iomap_is_partially_uptodate,
753aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
754b1e71b06SSteven Whitehouse };
755b1e71b06SSteven Whitehouse 
756b1e71b06SSteven Whitehouse static const struct address_space_operations gfs2_jdata_aops = {
757b1e71b06SSteven Whitehouse 	.writepage = gfs2_jdata_writepage,
758b1e71b06SSteven Whitehouse 	.writepages = gfs2_jdata_writepages,
759f132ab7dSMatthew Wilcox (Oracle) 	.read_folio = gfs2_read_folio,
760d4388340SMatthew Wilcox (Oracle) 	.readahead = gfs2_readahead,
761e621900aSMatthew Wilcox (Oracle) 	.dirty_folio = jdata_dirty_folio,
762b1e71b06SSteven Whitehouse 	.bmap = gfs2_bmap,
7635f4b2976SMatthew Wilcox (Oracle) 	.invalidate_folio = gfs2_invalidate_folio,
764e45c20d1SMatthew Wilcox (Oracle) 	.release_folio = gfs2_release_folio,
765b1e71b06SSteven Whitehouse 	.is_partially_uptodate = block_is_partially_uptodate,
766aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
767b1e71b06SSteven Whitehouse };
768b1e71b06SSteven Whitehouse 
gfs2_set_aops(struct inode * inode)769b1e71b06SSteven Whitehouse void gfs2_set_aops(struct inode *inode)
770b1e71b06SSteven Whitehouse {
771eadd7535SChristoph Hellwig 	if (gfs2_is_jdata(GFS2_I(inode)))
772b1e71b06SSteven Whitehouse 		inode->i_mapping->a_ops = &gfs2_jdata_aops;
773b1e71b06SSteven Whitehouse 	else
774eadd7535SChristoph Hellwig 		inode->i_mapping->a_ops = &gfs2_aops;
775b1e71b06SSteven Whitehouse }
776