xref: /openbmc/linux/fs/gfs2/aops.c (revision c1401fd1)
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 
40c1b0c3cfSAndreas Gruenbacher void gfs2_trans_add_databufs(struct gfs2_inode *ip, struct folio *folio,
4188b65ce5SAndreas Gruenbacher 			     unsigned int from, unsigned int 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;
4688b65ce5SAndreas Gruenbacher 	unsigned int to = from + len;
47b1e71b06SSteven Whitehouse 	unsigned int 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 
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 /**
85*c1401fd1SMatthew Wilcox (Oracle)  * gfs2_write_jdata_folio - gfs2 jdata-specific version of block_write_full_page
86*c1401fd1SMatthew 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  */
92*c1401fd1SMatthew Wilcox (Oracle) static int gfs2_write_jdata_folio(struct folio *folio,
93fd4c5748SBenjamin Marzinski 				 struct writeback_control *wbc)
94fd4c5748SBenjamin Marzinski {
95*c1401fd1SMatthew Wilcox (Oracle) 	struct inode * const inode = folio->mapping->host;
96fd4c5748SBenjamin Marzinski 	loff_t i_size = i_size_read(inode);
97fd4c5748SBenjamin Marzinski 
98fd4c5748SBenjamin Marzinski 	/*
99*c1401fd1SMatthew 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 	 */
105*c1401fd1SMatthew Wilcox (Oracle) 	if (folio_pos(folio) < i_size &&
106*c1401fd1SMatthew Wilcox (Oracle) 	    i_size < folio_pos(folio) + folio_size(folio))
107*c1401fd1SMatthew Wilcox (Oracle) 		folio_zero_segment(folio, offset_in_folio(folio, i_size),
108*c1401fd1SMatthew Wilcox (Oracle) 				folio_size(folio));
109fd4c5748SBenjamin Marzinski 
110*c1401fd1SMatthew Wilcox (Oracle) 	return __block_write_full_page(inode, &folio->page,
111*c1401fd1SMatthew Wilcox (Oracle) 				       gfs2_get_block_noalloc, wbc,
112fd4c5748SBenjamin Marzinski 				       end_buffer_async_write);
113fd4c5748SBenjamin Marzinski }
114fd4c5748SBenjamin Marzinski 
115b1e71b06SSteven Whitehouse /**
116d0cfcaeeSMatthew Wilcox (Oracle)  * __gfs2_jdata_write_folio - The core of jdata writepage
117d0cfcaeeSMatthew Wilcox (Oracle)  * @folio: The folio to write
118b1e71b06SSteven Whitehouse  * @wbc: The writeback control
119b1e71b06SSteven Whitehouse  *
120b1e71b06SSteven Whitehouse  * This is shared between writepage and writepages and implements the
121b1e71b06SSteven Whitehouse  * core of the writepage operation. If a transaction is required then
122d0cfcaeeSMatthew Wilcox (Oracle)  * the checked flag will have been set and the transaction will have
123b1e71b06SSteven Whitehouse  * already been started before this is called.
124b1e71b06SSteven Whitehouse  */
125d0cfcaeeSMatthew Wilcox (Oracle) static int __gfs2_jdata_write_folio(struct folio *folio,
126d0cfcaeeSMatthew Wilcox (Oracle) 		struct writeback_control *wbc)
127b1e71b06SSteven Whitehouse {
128d0cfcaeeSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
129b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(inode);
130b1e71b06SSteven Whitehouse 
131d0cfcaeeSMatthew Wilcox (Oracle) 	if (folio_test_checked(folio)) {
132d0cfcaeeSMatthew Wilcox (Oracle) 		folio_clear_checked(folio);
133d0cfcaeeSMatthew Wilcox (Oracle) 		if (!folio_buffers(folio)) {
134d0cfcaeeSMatthew Wilcox (Oracle) 			folio_create_empty_buffers(folio,
135d0cfcaeeSMatthew Wilcox (Oracle) 					inode->i_sb->s_blocksize,
13647a9a527SFabian Frederick 					BIT(BH_Dirty)|BIT(BH_Uptodate));
137b1e71b06SSteven Whitehouse 		}
138d0cfcaeeSMatthew Wilcox (Oracle) 		gfs2_trans_add_databufs(ip, folio, 0, folio_size(folio));
139b1e71b06SSteven Whitehouse 	}
140*c1401fd1SMatthew Wilcox (Oracle) 	return gfs2_write_jdata_folio(folio, wbc);
141b1e71b06SSteven Whitehouse }
142b1e71b06SSteven Whitehouse 
143b1e71b06SSteven Whitehouse /**
144b1e71b06SSteven Whitehouse  * gfs2_jdata_writepage - Write complete page
145b1e71b06SSteven Whitehouse  * @page: Page to write
1461272574bSFabian Frederick  * @wbc: The writeback control
147b1e71b06SSteven Whitehouse  *
148b1e71b06SSteven Whitehouse  * Returns: errno
149b1e71b06SSteven Whitehouse  *
150b1e71b06SSteven Whitehouse  */
151b1e71b06SSteven Whitehouse 
152b1e71b06SSteven Whitehouse static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
153b1e71b06SSteven Whitehouse {
154c0ba597dSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
155b1e71b06SSteven Whitehouse 	struct inode *inode = page->mapping->host;
156fd4c5748SBenjamin Marzinski 	struct gfs2_inode *ip = GFS2_I(inode);
157b1e71b06SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(inode);
158b1e71b06SSteven Whitehouse 
159fd4c5748SBenjamin Marzinski 	if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
160fd4c5748SBenjamin Marzinski 		goto out;
161c0ba597dSMatthew Wilcox (Oracle) 	if (folio_test_checked(folio) || current->journal_info)
162b1e71b06SSteven Whitehouse 		goto out_ignore;
163d0cfcaeeSMatthew Wilcox (Oracle) 	return __gfs2_jdata_write_folio(folio, wbc);
164b1e71b06SSteven Whitehouse 
165b1e71b06SSteven Whitehouse out_ignore:
166c0ba597dSMatthew Wilcox (Oracle) 	folio_redirty_for_writepage(wbc, folio);
167fd4c5748SBenjamin Marzinski out:
168c0ba597dSMatthew Wilcox (Oracle) 	folio_unlock(folio);
169b1e71b06SSteven Whitehouse 	return 0;
170b1e71b06SSteven Whitehouse }
171b1e71b06SSteven Whitehouse 
172b1e71b06SSteven Whitehouse /**
17345138990SSteven Whitehouse  * gfs2_writepages - Write a bunch of dirty pages back to disk
174b1e71b06SSteven Whitehouse  * @mapping: The mapping to write
175b1e71b06SSteven Whitehouse  * @wbc: Write-back control
176b1e71b06SSteven Whitehouse  *
17745138990SSteven Whitehouse  * Used for both ordered and writeback modes.
178b1e71b06SSteven Whitehouse  */
17945138990SSteven Whitehouse static int gfs2_writepages(struct address_space *mapping,
180b1e71b06SSteven Whitehouse 			   struct writeback_control *wbc)
181b1e71b06SSteven Whitehouse {
182b066a4eeSAbhi Das 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
1832164f9b9SChristoph Hellwig 	struct iomap_writepage_ctx wpc = { };
1842164f9b9SChristoph Hellwig 	int ret;
185b066a4eeSAbhi Das 
186b066a4eeSAbhi Das 	/*
187b066a4eeSAbhi Das 	 * Even if we didn't write any pages here, we might still be holding
188b066a4eeSAbhi Das 	 * dirty pages in the ail. We forcibly flush the ail because we don't
189b066a4eeSAbhi Das 	 * want balance_dirty_pages() to loop indefinitely trying to write out
190b066a4eeSAbhi Das 	 * pages held in the ail that it can't find.
191b066a4eeSAbhi Das 	 */
1922164f9b9SChristoph Hellwig 	ret = iomap_writepages(mapping, wbc, &wpc, &gfs2_writeback_ops);
193b066a4eeSAbhi Das 	if (ret == 0)
194b066a4eeSAbhi Das 		set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
195b066a4eeSAbhi Das 	return ret;
196b1e71b06SSteven Whitehouse }
197b1e71b06SSteven Whitehouse 
198b1e71b06SSteven Whitehouse /**
19987ed37e6SVishal Moola (Oracle)  * gfs2_write_jdata_batch - Write back a folio batch's worth of folios
200b1e71b06SSteven Whitehouse  * @mapping: The mapping
201b1e71b06SSteven Whitehouse  * @wbc: The writeback control
20287ed37e6SVishal Moola (Oracle)  * @fbatch: The batch of folios
2031272574bSFabian Frederick  * @done_index: Page index
204b1e71b06SSteven Whitehouse  *
205b1e71b06SSteven Whitehouse  * Returns: non-zero if loop should terminate, zero otherwise
206b1e71b06SSteven Whitehouse  */
207b1e71b06SSteven Whitehouse 
20887ed37e6SVishal Moola (Oracle) static int gfs2_write_jdata_batch(struct address_space *mapping,
209b1e71b06SSteven Whitehouse 				    struct writeback_control *wbc,
21087ed37e6SVishal Moola (Oracle) 				    struct folio_batch *fbatch,
211774016b2SSteven Whitehouse 				    pgoff_t *done_index)
212b1e71b06SSteven Whitehouse {
213b1e71b06SSteven Whitehouse 	struct inode *inode = mapping->host;
214b1e71b06SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(inode);
21587ed37e6SVishal Moola (Oracle) 	unsigned nrblocks;
216b1e71b06SSteven Whitehouse 	int i;
217b1e71b06SSteven Whitehouse 	int ret;
21887ed37e6SVishal Moola (Oracle) 	int nr_pages = 0;
21987ed37e6SVishal Moola (Oracle) 	int nr_folios = folio_batch_count(fbatch);
22087ed37e6SVishal Moola (Oracle) 
22187ed37e6SVishal Moola (Oracle) 	for (i = 0; i < nr_folios; i++)
22287ed37e6SVishal Moola (Oracle) 		nr_pages += folio_nr_pages(fbatch->folios[i]);
22387ed37e6SVishal Moola (Oracle) 	nrblocks = nr_pages * (PAGE_SIZE >> inode->i_blkbits);
224b1e71b06SSteven Whitehouse 
225b1e71b06SSteven Whitehouse 	ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
226b1e71b06SSteven Whitehouse 	if (ret < 0)
227b1e71b06SSteven Whitehouse 		return ret;
228b1e71b06SSteven Whitehouse 
22987ed37e6SVishal Moola (Oracle) 	for (i = 0; i < nr_folios; i++) {
23087ed37e6SVishal Moola (Oracle) 		struct folio *folio = fbatch->folios[i];
231b1e71b06SSteven Whitehouse 
23287ed37e6SVishal Moola (Oracle) 		*done_index = folio->index;
233774016b2SSteven Whitehouse 
23487ed37e6SVishal Moola (Oracle) 		folio_lock(folio);
235b1e71b06SSteven Whitehouse 
23687ed37e6SVishal Moola (Oracle) 		if (unlikely(folio->mapping != mapping)) {
237774016b2SSteven Whitehouse continue_unlock:
23887ed37e6SVishal Moola (Oracle) 			folio_unlock(folio);
239b1e71b06SSteven Whitehouse 			continue;
240b1e71b06SSteven Whitehouse 		}
241b1e71b06SSteven Whitehouse 
24287ed37e6SVishal Moola (Oracle) 		if (!folio_test_dirty(folio)) {
243774016b2SSteven Whitehouse 			/* someone wrote it for us */
244774016b2SSteven Whitehouse 			goto continue_unlock;
245b1e71b06SSteven Whitehouse 		}
246b1e71b06SSteven Whitehouse 
24787ed37e6SVishal Moola (Oracle) 		if (folio_test_writeback(folio)) {
248b1e71b06SSteven Whitehouse 			if (wbc->sync_mode != WB_SYNC_NONE)
24987ed37e6SVishal Moola (Oracle) 				folio_wait_writeback(folio);
250774016b2SSteven Whitehouse 			else
251774016b2SSteven Whitehouse 				goto continue_unlock;
252b1e71b06SSteven Whitehouse 		}
253b1e71b06SSteven Whitehouse 
25487ed37e6SVishal Moola (Oracle) 		BUG_ON(folio_test_writeback(folio));
25587ed37e6SVishal Moola (Oracle) 		if (!folio_clear_dirty_for_io(folio))
256774016b2SSteven Whitehouse 			goto continue_unlock;
257774016b2SSteven Whitehouse 
258de1414a6SChristoph Hellwig 		trace_wbc_writepage(wbc, inode_to_bdi(inode));
259b1e71b06SSteven Whitehouse 
260d0cfcaeeSMatthew Wilcox (Oracle) 		ret = __gfs2_jdata_write_folio(folio, wbc);
261774016b2SSteven Whitehouse 		if (unlikely(ret)) {
262774016b2SSteven Whitehouse 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
26387ed37e6SVishal Moola (Oracle) 				folio_unlock(folio);
264774016b2SSteven Whitehouse 				ret = 0;
265774016b2SSteven Whitehouse 			} else {
266b1e71b06SSteven Whitehouse 
267774016b2SSteven Whitehouse 				/*
268774016b2SSteven Whitehouse 				 * done_index is set past this page,
269774016b2SSteven Whitehouse 				 * so media errors will not choke
270774016b2SSteven Whitehouse 				 * background writeout for the entire
271774016b2SSteven Whitehouse 				 * file. This has consequences for
272774016b2SSteven Whitehouse 				 * range_cyclic semantics (ie. it may
273774016b2SSteven Whitehouse 				 * not be suitable for data integrity
274774016b2SSteven Whitehouse 				 * writeout).
275774016b2SSteven Whitehouse 				 */
27687ed37e6SVishal Moola (Oracle) 				*done_index = folio->index +
27787ed37e6SVishal Moola (Oracle) 					folio_nr_pages(folio);
278b1e71b06SSteven Whitehouse 				ret = 1;
279774016b2SSteven Whitehouse 				break;
280774016b2SSteven Whitehouse 			}
281774016b2SSteven Whitehouse 		}
282774016b2SSteven Whitehouse 
283774016b2SSteven Whitehouse 		/*
284774016b2SSteven Whitehouse 		 * We stop writing back only if we are not doing
285774016b2SSteven Whitehouse 		 * integrity sync. In case of integrity sync we have to
286774016b2SSteven Whitehouse 		 * keep going until we have written all the pages
287774016b2SSteven Whitehouse 		 * we tagged for writeback prior to entering this loop.
288774016b2SSteven Whitehouse 		 */
289774016b2SSteven Whitehouse 		if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
290774016b2SSteven Whitehouse 			ret = 1;
291774016b2SSteven Whitehouse 			break;
292774016b2SSteven Whitehouse 		}
293774016b2SSteven Whitehouse 
294b1e71b06SSteven Whitehouse 	}
295b1e71b06SSteven Whitehouse 	gfs2_trans_end(sdp);
296b1e71b06SSteven Whitehouse 	return ret;
297b1e71b06SSteven Whitehouse }
298b1e71b06SSteven Whitehouse 
299b1e71b06SSteven Whitehouse /**
300b1e71b06SSteven Whitehouse  * gfs2_write_cache_jdata - Like write_cache_pages but different
301b1e71b06SSteven Whitehouse  * @mapping: The mapping to write
302b1e71b06SSteven Whitehouse  * @wbc: The writeback control
303b1e71b06SSteven Whitehouse  *
304b1e71b06SSteven Whitehouse  * The reason that we use our own function here is that we need to
305b1e71b06SSteven Whitehouse  * start transactions before we grab page locks. This allows us
306b1e71b06SSteven Whitehouse  * to get the ordering right.
307b1e71b06SSteven Whitehouse  */
308b1e71b06SSteven Whitehouse 
309b1e71b06SSteven Whitehouse static int gfs2_write_cache_jdata(struct address_space *mapping,
310b1e71b06SSteven Whitehouse 				  struct writeback_control *wbc)
311b1e71b06SSteven Whitehouse {
312b1e71b06SSteven Whitehouse 	int ret = 0;
313b1e71b06SSteven Whitehouse 	int done = 0;
31487ed37e6SVishal Moola (Oracle) 	struct folio_batch fbatch;
31587ed37e6SVishal Moola (Oracle) 	int nr_folios;
3163f649ab7SKees Cook 	pgoff_t writeback_index;
317b1e71b06SSteven Whitehouse 	pgoff_t index;
318b1e71b06SSteven Whitehouse 	pgoff_t end;
319774016b2SSteven Whitehouse 	pgoff_t done_index;
320774016b2SSteven Whitehouse 	int cycled;
321b1e71b06SSteven Whitehouse 	int range_whole = 0;
32210bbd235SMatthew Wilcox 	xa_mark_t tag;
323b1e71b06SSteven Whitehouse 
32487ed37e6SVishal Moola (Oracle) 	folio_batch_init(&fbatch);
325b1e71b06SSteven Whitehouse 	if (wbc->range_cyclic) {
326774016b2SSteven Whitehouse 		writeback_index = mapping->writeback_index; /* prev offset */
327774016b2SSteven Whitehouse 		index = writeback_index;
328774016b2SSteven Whitehouse 		if (index == 0)
329774016b2SSteven Whitehouse 			cycled = 1;
330774016b2SSteven Whitehouse 		else
331774016b2SSteven Whitehouse 			cycled = 0;
332b1e71b06SSteven Whitehouse 		end = -1;
333b1e71b06SSteven Whitehouse 	} else {
33409cbfeafSKirill A. Shutemov 		index = wbc->range_start >> PAGE_SHIFT;
33509cbfeafSKirill A. Shutemov 		end = wbc->range_end >> PAGE_SHIFT;
336b1e71b06SSteven Whitehouse 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
337b1e71b06SSteven Whitehouse 			range_whole = 1;
338774016b2SSteven Whitehouse 		cycled = 1; /* ignore range_cyclic tests */
339b1e71b06SSteven Whitehouse 	}
340774016b2SSteven Whitehouse 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
341774016b2SSteven Whitehouse 		tag = PAGECACHE_TAG_TOWRITE;
342774016b2SSteven Whitehouse 	else
343774016b2SSteven Whitehouse 		tag = PAGECACHE_TAG_DIRTY;
344b1e71b06SSteven Whitehouse 
345b1e71b06SSteven Whitehouse retry:
346774016b2SSteven Whitehouse 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
347774016b2SSteven Whitehouse 		tag_pages_for_writeback(mapping, index, end);
348774016b2SSteven Whitehouse 	done_index = index;
349774016b2SSteven Whitehouse 	while (!done && (index <= end)) {
35087ed37e6SVishal Moola (Oracle) 		nr_folios = filemap_get_folios_tag(mapping, &index, end,
35187ed37e6SVishal Moola (Oracle) 				tag, &fbatch);
35287ed37e6SVishal Moola (Oracle) 		if (nr_folios == 0)
353774016b2SSteven Whitehouse 			break;
354774016b2SSteven Whitehouse 
35587ed37e6SVishal Moola (Oracle) 		ret = gfs2_write_jdata_batch(mapping, wbc, &fbatch,
35687ed37e6SVishal Moola (Oracle) 				&done_index);
357b1e71b06SSteven Whitehouse 		if (ret)
358b1e71b06SSteven Whitehouse 			done = 1;
359b1e71b06SSteven Whitehouse 		if (ret > 0)
360b1e71b06SSteven Whitehouse 			ret = 0;
36187ed37e6SVishal Moola (Oracle) 		folio_batch_release(&fbatch);
362b1e71b06SSteven Whitehouse 		cond_resched();
363b1e71b06SSteven Whitehouse 	}
364b1e71b06SSteven Whitehouse 
365774016b2SSteven Whitehouse 	if (!cycled && !done) {
366b1e71b06SSteven Whitehouse 		/*
367774016b2SSteven Whitehouse 		 * range_cyclic:
368b1e71b06SSteven Whitehouse 		 * We hit the last page and there is more work to be done: wrap
369b1e71b06SSteven Whitehouse 		 * back to the start of the file
370b1e71b06SSteven Whitehouse 		 */
371774016b2SSteven Whitehouse 		cycled = 1;
372b1e71b06SSteven Whitehouse 		index = 0;
373774016b2SSteven Whitehouse 		end = writeback_index - 1;
374b1e71b06SSteven Whitehouse 		goto retry;
375b1e71b06SSteven Whitehouse 	}
376b1e71b06SSteven Whitehouse 
377b1e71b06SSteven Whitehouse 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
378774016b2SSteven Whitehouse 		mapping->writeback_index = done_index;
379774016b2SSteven Whitehouse 
380b1e71b06SSteven Whitehouse 	return ret;
381b1e71b06SSteven Whitehouse }
382b1e71b06SSteven Whitehouse 
383b1e71b06SSteven Whitehouse 
384b1e71b06SSteven Whitehouse /**
385b1e71b06SSteven Whitehouse  * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
386b1e71b06SSteven Whitehouse  * @mapping: The mapping to write
387b1e71b06SSteven Whitehouse  * @wbc: The writeback control
388b1e71b06SSteven Whitehouse  *
389b1e71b06SSteven Whitehouse  */
390b1e71b06SSteven Whitehouse 
391b1e71b06SSteven Whitehouse static int gfs2_jdata_writepages(struct address_space *mapping,
392b1e71b06SSteven Whitehouse 				 struct writeback_control *wbc)
393b1e71b06SSteven Whitehouse {
394b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(mapping->host);
395b1e71b06SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
396b1e71b06SSteven Whitehouse 	int ret;
397b1e71b06SSteven Whitehouse 
398b1e71b06SSteven Whitehouse 	ret = gfs2_write_cache_jdata(mapping, wbc);
399b1e71b06SSteven Whitehouse 	if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
400805c0907SBob Peterson 		gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
401805c0907SBob Peterson 			       GFS2_LFC_JDATA_WPAGES);
402b1e71b06SSteven Whitehouse 		ret = gfs2_write_cache_jdata(mapping, wbc);
403b1e71b06SSteven Whitehouse 	}
404b1e71b06SSteven Whitehouse 	return ret;
405b1e71b06SSteven Whitehouse }
406b1e71b06SSteven Whitehouse 
407b1e71b06SSteven Whitehouse /**
408b1e71b06SSteven Whitehouse  * stuffed_readpage - Fill in a Linux page with stuffed file data
409b1e71b06SSteven Whitehouse  * @ip: the inode
410b1e71b06SSteven Whitehouse  * @page: the page
411b1e71b06SSteven Whitehouse  *
412b1e71b06SSteven Whitehouse  * Returns: errno
413b1e71b06SSteven Whitehouse  */
414378b6cbfSChristoph Hellwig static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
415b1e71b06SSteven Whitehouse {
416b1e71b06SSteven Whitehouse 	struct buffer_head *dibh;
417602c89d2SSteven Whitehouse 	u64 dsize = i_size_read(&ip->i_inode);
418b1e71b06SSteven Whitehouse 	void *kaddr;
419b1e71b06SSteven Whitehouse 	int error;
420b1e71b06SSteven Whitehouse 
421b1e71b06SSteven Whitehouse 	/*
422b1e71b06SSteven Whitehouse 	 * Due to the order of unstuffing files and ->fault(), we can be
423b1e71b06SSteven Whitehouse 	 * asked for a zero page in the case of a stuffed file being extended,
424b1e71b06SSteven Whitehouse 	 * so we need to supply one here. It doesn't happen often.
425b1e71b06SSteven Whitehouse 	 */
426b1e71b06SSteven Whitehouse 	if (unlikely(page->index)) {
42709cbfeafSKirill A. Shutemov 		zero_user(page, 0, PAGE_SIZE);
428b1e71b06SSteven Whitehouse 		SetPageUptodate(page);
429b1e71b06SSteven Whitehouse 		return 0;
430b1e71b06SSteven Whitehouse 	}
431b1e71b06SSteven Whitehouse 
432b1e71b06SSteven Whitehouse 	error = gfs2_meta_inode_buffer(ip, &dibh);
433b1e71b06SSteven Whitehouse 	if (error)
434b1e71b06SSteven Whitehouse 		return error;
435b1e71b06SSteven Whitehouse 
436d9349285SCong Wang 	kaddr = kmap_atomic(page);
437602c89d2SSteven Whitehouse 	memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
43809cbfeafSKirill A. Shutemov 	memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
439d9349285SCong Wang 	kunmap_atomic(kaddr);
440b1e71b06SSteven Whitehouse 	flush_dcache_page(page);
441b1e71b06SSteven Whitehouse 	brelse(dibh);
442b1e71b06SSteven Whitehouse 	SetPageUptodate(page);
443b1e71b06SSteven Whitehouse 
444b1e71b06SSteven Whitehouse 	return 0;
445b1e71b06SSteven Whitehouse }
446b1e71b06SSteven Whitehouse 
447e9b5b23eSMatthew Wilcox (Oracle) /**
448e9b5b23eSMatthew Wilcox (Oracle)  * gfs2_read_folio - read a folio from a file
449e9b5b23eSMatthew Wilcox (Oracle)  * @file: The file to read
450e9b5b23eSMatthew Wilcox (Oracle)  * @folio: The folio in the file
451e9b5b23eSMatthew Wilcox (Oracle)  */
452e9b5b23eSMatthew Wilcox (Oracle) static int gfs2_read_folio(struct file *file, struct folio *folio)
453b1e71b06SSteven Whitehouse {
454e9b5b23eSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
4552164f9b9SChristoph Hellwig 	struct gfs2_inode *ip = GFS2_I(inode);
4562164f9b9SChristoph Hellwig 	struct gfs2_sbd *sdp = GFS2_SB(inode);
457b1e71b06SSteven Whitehouse 	int error;
458b1e71b06SSteven Whitehouse 
4592164f9b9SChristoph Hellwig 	if (!gfs2_is_jdata(ip) ||
460e9b5b23eSMatthew Wilcox (Oracle) 	    (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
4617479c505SMatthew Wilcox (Oracle) 		error = iomap_read_folio(folio, &gfs2_iomap_ops);
462f95cbb44SAndreas Gruenbacher 	} else if (gfs2_is_stuffed(ip)) {
463e9b5b23eSMatthew Wilcox (Oracle) 		error = stuffed_readpage(ip, &folio->page);
464e9b5b23eSMatthew Wilcox (Oracle) 		folio_unlock(folio);
465b1e71b06SSteven Whitehouse 	} else {
466f132ab7dSMatthew Wilcox (Oracle) 		error = mpage_read_folio(folio, gfs2_block_map);
467b1e71b06SSteven Whitehouse 	}
468b1e71b06SSteven Whitehouse 
469eb43e660SBob Peterson 	if (unlikely(gfs2_withdrawn(sdp)))
470b1e71b06SSteven Whitehouse 		return -EIO;
471b1e71b06SSteven Whitehouse 
472b1e71b06SSteven Whitehouse 	return error;
473b1e71b06SSteven Whitehouse }
474b1e71b06SSteven Whitehouse 
475b1e71b06SSteven Whitehouse /**
476b1e71b06SSteven Whitehouse  * gfs2_internal_read - read an internal file
477b1e71b06SSteven Whitehouse  * @ip: The gfs2 inode
478b1e71b06SSteven Whitehouse  * @buf: The buffer to fill
479b1e71b06SSteven Whitehouse  * @pos: The file position
480b1e71b06SSteven Whitehouse  * @size: The amount to read
481b1e71b06SSteven Whitehouse  *
482b1e71b06SSteven Whitehouse  */
483b1e71b06SSteven Whitehouse 
4844306629eSAndrew Price int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
4854306629eSAndrew Price                        unsigned size)
486b1e71b06SSteven Whitehouse {
487b1e71b06SSteven Whitehouse 	struct address_space *mapping = ip->i_inode.i_mapping;
48845eb0504SAndreas Gruenbacher 	unsigned long index = *pos >> PAGE_SHIFT;
48909cbfeafSKirill A. Shutemov 	unsigned offset = *pos & (PAGE_SIZE - 1);
490b1e71b06SSteven Whitehouse 	unsigned copied = 0;
491b1e71b06SSteven Whitehouse 	unsigned amt;
492b1e71b06SSteven Whitehouse 	struct page *page;
493b1e71b06SSteven Whitehouse 	void *p;
494b1e71b06SSteven Whitehouse 
495b1e71b06SSteven Whitehouse 	do {
496b1e71b06SSteven Whitehouse 		amt = size - copied;
49709cbfeafSKirill A. Shutemov 		if (offset + size > PAGE_SIZE)
49809cbfeafSKirill A. Shutemov 			amt = PAGE_SIZE - offset;
499e9b5b23eSMatthew Wilcox (Oracle) 		page = read_cache_page(mapping, index, gfs2_read_folio, NULL);
500b1e71b06SSteven Whitehouse 		if (IS_ERR(page))
501b1e71b06SSteven Whitehouse 			return PTR_ERR(page);
502d9349285SCong Wang 		p = kmap_atomic(page);
503b1e71b06SSteven Whitehouse 		memcpy(buf + copied, p + offset, amt);
504d9349285SCong Wang 		kunmap_atomic(p);
50509cbfeafSKirill A. Shutemov 		put_page(page);
506b1e71b06SSteven Whitehouse 		copied += amt;
507b1e71b06SSteven Whitehouse 		index++;
508b1e71b06SSteven Whitehouse 		offset = 0;
509b1e71b06SSteven Whitehouse 	} while(copied < size);
510b1e71b06SSteven Whitehouse 	(*pos) += size;
511b1e71b06SSteven Whitehouse 	return size;
512b1e71b06SSteven Whitehouse }
513b1e71b06SSteven Whitehouse 
514b1e71b06SSteven Whitehouse /**
515d4388340SMatthew Wilcox (Oracle)  * gfs2_readahead - Read a bunch of pages at once
516c551f66cSLee Jones  * @rac: Read-ahead control structure
517b1e71b06SSteven Whitehouse  *
518b1e71b06SSteven Whitehouse  * Some notes:
519b1e71b06SSteven Whitehouse  * 1. This is only for readahead, so we can simply ignore any things
520b1e71b06SSteven Whitehouse  *    which are slightly inconvenient (such as locking conflicts between
521b1e71b06SSteven Whitehouse  *    the page lock and the glock) and return having done no I/O. Its
522b1e71b06SSteven Whitehouse  *    obviously not something we'd want to do on too regular a basis.
523b1e71b06SSteven Whitehouse  *    Any I/O we ignore at this time will be done via readpage later.
524b1e71b06SSteven Whitehouse  * 2. We don't handle stuffed files here we let readpage do the honours.
525d4388340SMatthew Wilcox (Oracle)  * 3. mpage_readahead() does most of the heavy lifting in the common case.
526b1e71b06SSteven Whitehouse  * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
527b1e71b06SSteven Whitehouse  */
528b1e71b06SSteven Whitehouse 
529d4388340SMatthew Wilcox (Oracle) static void gfs2_readahead(struct readahead_control *rac)
530b1e71b06SSteven Whitehouse {
531d4388340SMatthew Wilcox (Oracle) 	struct inode *inode = rac->mapping->host;
532b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(inode);
533b1e71b06SSteven Whitehouse 
5342164f9b9SChristoph Hellwig 	if (gfs2_is_stuffed(ip))
5352164f9b9SChristoph Hellwig 		;
5362164f9b9SChristoph Hellwig 	else if (gfs2_is_jdata(ip))
537d4388340SMatthew Wilcox (Oracle) 		mpage_readahead(rac, gfs2_block_map);
5382164f9b9SChristoph Hellwig 	else
5392164f9b9SChristoph Hellwig 		iomap_readahead(rac, &gfs2_iomap_ops);
540b1e71b06SSteven Whitehouse }
541b1e71b06SSteven Whitehouse 
542b1e71b06SSteven Whitehouse /**
543b1e71b06SSteven Whitehouse  * adjust_fs_space - Adjusts the free space available due to gfs2_grow
544b1e71b06SSteven Whitehouse  * @inode: the rindex inode
545b1e71b06SSteven Whitehouse  */
54664bc06bbSAndreas Gruenbacher void adjust_fs_space(struct inode *inode)
547b1e71b06SSteven Whitehouse {
548d0a22a4bSAndreas Gruenbacher 	struct gfs2_sbd *sdp = GFS2_SB(inode);
5491946f70aSBenjamin Marzinski 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
550b1e71b06SSteven Whitehouse 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
551b1e71b06SSteven Whitehouse 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
55270c11ba8SBob Peterson 	struct buffer_head *m_bh;
553b1e71b06SSteven Whitehouse 	u64 fs_total, new_free;
554b1e71b06SSteven Whitehouse 
555d0a22a4bSAndreas Gruenbacher 	if (gfs2_trans_begin(sdp, 2 * RES_STATFS, 0) != 0)
556d0a22a4bSAndreas Gruenbacher 		return;
557d0a22a4bSAndreas Gruenbacher 
558b1e71b06SSteven Whitehouse 	/* Total up the file system space, according to the latest rindex. */
559b1e71b06SSteven Whitehouse 	fs_total = gfs2_ri_total(sdp);
5601946f70aSBenjamin Marzinski 	if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
561d0a22a4bSAndreas Gruenbacher 		goto out;
562b1e71b06SSteven Whitehouse 
563b1e71b06SSteven Whitehouse 	spin_lock(&sdp->sd_statfs_spin);
5641946f70aSBenjamin Marzinski 	gfs2_statfs_change_in(m_sc, m_bh->b_data +
5651946f70aSBenjamin Marzinski 			      sizeof(struct gfs2_dinode));
566b1e71b06SSteven Whitehouse 	if (fs_total > (m_sc->sc_total + l_sc->sc_total))
567b1e71b06SSteven Whitehouse 		new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
568b1e71b06SSteven Whitehouse 	else
569b1e71b06SSteven Whitehouse 		new_free = 0;
570b1e71b06SSteven Whitehouse 	spin_unlock(&sdp->sd_statfs_spin);
571b1e71b06SSteven Whitehouse 	fs_warn(sdp, "File system extended by %llu blocks.\n",
572b1e71b06SSteven Whitehouse 		(unsigned long long)new_free);
573b1e71b06SSteven Whitehouse 	gfs2_statfs_change(sdp, new_free, new_free, 0);
5741946f70aSBenjamin Marzinski 
57570c11ba8SBob Peterson 	update_statfs(sdp, m_bh);
5761946f70aSBenjamin Marzinski 	brelse(m_bh);
577d0a22a4bSAndreas Gruenbacher out:
578d0a22a4bSAndreas Gruenbacher 	sdp->sd_rindex_uptodate = 0;
579d0a22a4bSAndreas Gruenbacher 	gfs2_trans_end(sdp);
580b1e71b06SSteven Whitehouse }
581b1e71b06SSteven Whitehouse 
582e621900aSMatthew Wilcox (Oracle) static bool jdata_dirty_folio(struct address_space *mapping,
583e621900aSMatthew Wilcox (Oracle) 		struct folio *folio)
584b1e71b06SSteven Whitehouse {
5856302d6f4SBob Peterson 	if (current->journal_info)
586e621900aSMatthew Wilcox (Oracle) 		folio_set_checked(folio);
587e621900aSMatthew Wilcox (Oracle) 	return block_dirty_folio(mapping, folio);
588b1e71b06SSteven Whitehouse }
589b1e71b06SSteven Whitehouse 
590b1e71b06SSteven Whitehouse /**
591b1e71b06SSteven Whitehouse  * gfs2_bmap - Block map function
592b1e71b06SSteven Whitehouse  * @mapping: Address space info
593b1e71b06SSteven Whitehouse  * @lblock: The block to map
594b1e71b06SSteven Whitehouse  *
595b1e71b06SSteven Whitehouse  * Returns: The disk address for the block or 0 on hole or error
596b1e71b06SSteven Whitehouse  */
597b1e71b06SSteven Whitehouse 
598b1e71b06SSteven Whitehouse static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
599b1e71b06SSteven Whitehouse {
600b1e71b06SSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(mapping->host);
601b1e71b06SSteven Whitehouse 	struct gfs2_holder i_gh;
602b1e71b06SSteven Whitehouse 	sector_t dblock = 0;
603b1e71b06SSteven Whitehouse 	int error;
604b1e71b06SSteven Whitehouse 
605b1e71b06SSteven Whitehouse 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
606b1e71b06SSteven Whitehouse 	if (error)
607b1e71b06SSteven Whitehouse 		return 0;
608b1e71b06SSteven Whitehouse 
609b1e71b06SSteven Whitehouse 	if (!gfs2_is_stuffed(ip))
6107770c93aSChristoph Hellwig 		dblock = iomap_bmap(mapping, lblock, &gfs2_iomap_ops);
611b1e71b06SSteven Whitehouse 
612b1e71b06SSteven Whitehouse 	gfs2_glock_dq_uninit(&i_gh);
613b1e71b06SSteven Whitehouse 
614b1e71b06SSteven Whitehouse 	return dblock;
615b1e71b06SSteven Whitehouse }
616b1e71b06SSteven Whitehouse 
617b1e71b06SSteven Whitehouse static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
618b1e71b06SSteven Whitehouse {
619b1e71b06SSteven Whitehouse 	struct gfs2_bufdata *bd;
620b1e71b06SSteven Whitehouse 
621b1e71b06SSteven Whitehouse 	lock_buffer(bh);
622b1e71b06SSteven Whitehouse 	gfs2_log_lock(sdp);
623b1e71b06SSteven Whitehouse 	clear_buffer_dirty(bh);
624b1e71b06SSteven Whitehouse 	bd = bh->b_private;
625b1e71b06SSteven Whitehouse 	if (bd) {
626c0752aa7SBob Peterson 		if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
627c0752aa7SBob Peterson 			list_del_init(&bd->bd_list);
62868942870SBob Peterson 		else {
62968942870SBob Peterson 			spin_lock(&sdp->sd_ail_lock);
63068cd4ce2SBob Peterson 			gfs2_remove_from_journal(bh, REMOVE_JDATA);
63168942870SBob Peterson 			spin_unlock(&sdp->sd_ail_lock);
63268942870SBob Peterson 		}
633b1e71b06SSteven Whitehouse 	}
634b1e71b06SSteven Whitehouse 	bh->b_bdev = NULL;
635b1e71b06SSteven Whitehouse 	clear_buffer_mapped(bh);
636b1e71b06SSteven Whitehouse 	clear_buffer_req(bh);
637b1e71b06SSteven Whitehouse 	clear_buffer_new(bh);
638b1e71b06SSteven Whitehouse 	gfs2_log_unlock(sdp);
639b1e71b06SSteven Whitehouse 	unlock_buffer(bh);
640b1e71b06SSteven Whitehouse }
641b1e71b06SSteven Whitehouse 
6425f4b2976SMatthew Wilcox (Oracle) static void gfs2_invalidate_folio(struct folio *folio, size_t offset,
6435f4b2976SMatthew Wilcox (Oracle) 				size_t length)
644b1e71b06SSteven Whitehouse {
6455f4b2976SMatthew Wilcox (Oracle) 	struct gfs2_sbd *sdp = GFS2_SB(folio->mapping->host);
6465f4b2976SMatthew Wilcox (Oracle) 	size_t stop = offset + length;
6475f4b2976SMatthew Wilcox (Oracle) 	int partial_page = (offset || length < folio_size(folio));
648b1e71b06SSteven Whitehouse 	struct buffer_head *bh, *head;
649b1e71b06SSteven Whitehouse 	unsigned long pos = 0;
650b1e71b06SSteven Whitehouse 
6515f4b2976SMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
6525c0bb97cSLukas Czerner 	if (!partial_page)
6535f4b2976SMatthew Wilcox (Oracle) 		folio_clear_checked(folio);
6545f4b2976SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
6555f4b2976SMatthew Wilcox (Oracle) 	if (!head)
656b1e71b06SSteven Whitehouse 		goto out;
657b1e71b06SSteven Whitehouse 
6585f4b2976SMatthew Wilcox (Oracle) 	bh = head;
659b1e71b06SSteven Whitehouse 	do {
6605c0bb97cSLukas Czerner 		if (pos + bh->b_size > stop)
6615c0bb97cSLukas Czerner 			return;
6625c0bb97cSLukas Czerner 
663b1e71b06SSteven Whitehouse 		if (offset <= pos)
664b1e71b06SSteven Whitehouse 			gfs2_discard(sdp, bh);
665b1e71b06SSteven Whitehouse 		pos += bh->b_size;
666b1e71b06SSteven Whitehouse 		bh = bh->b_this_page;
667b1e71b06SSteven Whitehouse 	} while (bh != head);
668b1e71b06SSteven Whitehouse out:
6695c0bb97cSLukas Czerner 	if (!partial_page)
6705f4b2976SMatthew Wilcox (Oracle) 		filemap_release_folio(folio, 0);
671b1e71b06SSteven Whitehouse }
672b1e71b06SSteven Whitehouse 
673b1e71b06SSteven Whitehouse /**
674e45c20d1SMatthew Wilcox (Oracle)  * gfs2_release_folio - free the metadata associated with a folio
675e45c20d1SMatthew Wilcox (Oracle)  * @folio: the folio that's being released
676b1e71b06SSteven Whitehouse  * @gfp_mask: passed from Linux VFS, ignored by us
677b1e71b06SSteven Whitehouse  *
678e45c20d1SMatthew Wilcox (Oracle)  * Calls try_to_free_buffers() to free the buffers and put the folio if the
6790ebbe4f9SAndreas Gruenbacher  * buffers can be released.
680b1e71b06SSteven Whitehouse  *
681e45c20d1SMatthew Wilcox (Oracle)  * Returns: true if the folio was put or else false
682b1e71b06SSteven Whitehouse  */
683b1e71b06SSteven Whitehouse 
684e45c20d1SMatthew Wilcox (Oracle) bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
685b1e71b06SSteven Whitehouse {
686e45c20d1SMatthew Wilcox (Oracle) 	struct address_space *mapping = folio->mapping;
687009d8518SSteven Whitehouse 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
688b1e71b06SSteven Whitehouse 	struct buffer_head *bh, *head;
689b1e71b06SSteven Whitehouse 	struct gfs2_bufdata *bd;
690b1e71b06SSteven Whitehouse 
691e45c20d1SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
692e45c20d1SMatthew Wilcox (Oracle) 	if (!head)
693e45c20d1SMatthew Wilcox (Oracle) 		return false;
694b1e71b06SSteven Whitehouse 
6951c185c02SAndreas Gruenbacher 	/*
696e45c20d1SMatthew Wilcox (Oracle) 	 * mm accommodates an old ext3 case where clean folios might
697e45c20d1SMatthew Wilcox (Oracle) 	 * not have had the dirty bit cleared.	Thus, it can send actual
698e45c20d1SMatthew Wilcox (Oracle) 	 * dirty folios to ->release_folio() via shrink_active_list().
6991c185c02SAndreas Gruenbacher 	 *
700e45c20d1SMatthew Wilcox (Oracle) 	 * As a workaround, we skip folios that contain dirty buffers
701e45c20d1SMatthew Wilcox (Oracle) 	 * below.  Once ->release_folio isn't called on dirty folios
702e45c20d1SMatthew Wilcox (Oracle) 	 * anymore, we can warn on dirty buffers like we used to here
703e45c20d1SMatthew Wilcox (Oracle) 	 * again.
7041c185c02SAndreas Gruenbacher 	 */
7051c185c02SAndreas Gruenbacher 
706b1e71b06SSteven Whitehouse 	gfs2_log_lock(sdp);
707e45c20d1SMatthew Wilcox (Oracle) 	bh = head;
708b1e71b06SSteven Whitehouse 	do {
709b1e71b06SSteven Whitehouse 		if (atomic_read(&bh->b_count))
710b1e71b06SSteven Whitehouse 			goto cannot_release;
711b1e71b06SSteven Whitehouse 		bd = bh->b_private;
71216ca9412SBenjamin Marzinski 		if (bd && bd->bd_tr)
713b1e71b06SSteven Whitehouse 			goto cannot_release;
7141c185c02SAndreas Gruenbacher 		if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
7151c185c02SAndreas Gruenbacher 			goto cannot_release;
716b1e71b06SSteven Whitehouse 		bh = bh->b_this_page;
717b1e71b06SSteven Whitehouse 	} while (bh != head);
718b1e71b06SSteven Whitehouse 
719e45c20d1SMatthew Wilcox (Oracle) 	bh = head;
720b1e71b06SSteven Whitehouse 	do {
721b1e71b06SSteven Whitehouse 		bd = bh->b_private;
722b1e71b06SSteven Whitehouse 		if (bd) {
723b1e71b06SSteven Whitehouse 			gfs2_assert_warn(sdp, bd->bd_bh == bh);
724b1e71b06SSteven Whitehouse 			bd->bd_bh = NULL;
725b1e71b06SSteven Whitehouse 			bh->b_private = NULL;
726019dd669SBob Peterson 			/*
727019dd669SBob Peterson 			 * The bd may still be queued as a revoke, in which
728019dd669SBob Peterson 			 * case we must not dequeue nor free it.
729019dd669SBob Peterson 			 */
730019dd669SBob Peterson 			if (!bd->bd_blkno && !list_empty(&bd->bd_list))
731019dd669SBob Peterson 				list_del_init(&bd->bd_list);
732019dd669SBob Peterson 			if (list_empty(&bd->bd_list))
733b1e71b06SSteven Whitehouse 				kmem_cache_free(gfs2_bufdata_cachep, bd);
734e4f29206SSteven Whitehouse 		}
735b1e71b06SSteven Whitehouse 
736b1e71b06SSteven Whitehouse 		bh = bh->b_this_page;
737b1e71b06SSteven Whitehouse 	} while (bh != head);
738e4f29206SSteven Whitehouse 	gfs2_log_unlock(sdp);
739b1e71b06SSteven Whitehouse 
74068189fefSMatthew Wilcox (Oracle) 	return try_to_free_buffers(folio);
7418f065d36SSteven Whitehouse 
742b1e71b06SSteven Whitehouse cannot_release:
743b1e71b06SSteven Whitehouse 	gfs2_log_unlock(sdp);
744e45c20d1SMatthew Wilcox (Oracle) 	return false;
745b1e71b06SSteven Whitehouse }
746b1e71b06SSteven Whitehouse 
747eadd7535SChristoph Hellwig static const struct address_space_operations gfs2_aops = {
74845138990SSteven Whitehouse 	.writepages = gfs2_writepages,
749f132ab7dSMatthew Wilcox (Oracle) 	.read_folio = gfs2_read_folio,
750d4388340SMatthew Wilcox (Oracle) 	.readahead = gfs2_readahead,
751187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio = filemap_dirty_folio,
7528597447dSMatthew Wilcox (Oracle) 	.release_folio = iomap_release_folio,
753d82354f6SMatthew Wilcox (Oracle) 	.invalidate_folio = iomap_invalidate_folio,
754b1e71b06SSteven Whitehouse 	.bmap = gfs2_bmap,
755967bcc91SAndreas Gruenbacher 	.direct_IO = noop_direct_IO,
7562ec810d5SMatthew Wilcox (Oracle) 	.migrate_folio = filemap_migrate_folio,
7572164f9b9SChristoph Hellwig 	.is_partially_uptodate = iomap_is_partially_uptodate,
758aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
759b1e71b06SSteven Whitehouse };
760b1e71b06SSteven Whitehouse 
761b1e71b06SSteven Whitehouse static const struct address_space_operations gfs2_jdata_aops = {
762b1e71b06SSteven Whitehouse 	.writepage = gfs2_jdata_writepage,
763b1e71b06SSteven Whitehouse 	.writepages = gfs2_jdata_writepages,
764f132ab7dSMatthew Wilcox (Oracle) 	.read_folio = gfs2_read_folio,
765d4388340SMatthew Wilcox (Oracle) 	.readahead = gfs2_readahead,
766e621900aSMatthew Wilcox (Oracle) 	.dirty_folio = jdata_dirty_folio,
767b1e71b06SSteven Whitehouse 	.bmap = gfs2_bmap,
7685f4b2976SMatthew Wilcox (Oracle) 	.invalidate_folio = gfs2_invalidate_folio,
769e45c20d1SMatthew Wilcox (Oracle) 	.release_folio = gfs2_release_folio,
770b1e71b06SSteven Whitehouse 	.is_partially_uptodate = block_is_partially_uptodate,
771aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
772b1e71b06SSteven Whitehouse };
773b1e71b06SSteven Whitehouse 
774b1e71b06SSteven Whitehouse void gfs2_set_aops(struct inode *inode)
775b1e71b06SSteven Whitehouse {
776eadd7535SChristoph Hellwig 	if (gfs2_is_jdata(GFS2_I(inode)))
777b1e71b06SSteven Whitehouse 		inode->i_mapping->a_ops = &gfs2_jdata_aops;
778b1e71b06SSteven Whitehouse 	else
779eadd7535SChristoph Hellwig 		inode->i_mapping->a_ops = &gfs2_aops;
780b1e71b06SSteven Whitehouse }
781