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 
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
10 #include <linux/completion.h>
11 #include <linux/buffer_head.h>
12 #include <linux/pagemap.h>
13 #include <linux/pagevec.h>
14 #include <linux/mpage.h>
15 #include <linux/fs.h>
16 #include <linux/writeback.h>
17 #include <linux/swap.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/backing-dev.h>
20 #include <linux/uio.h>
21 #include <trace/events/writeback.h>
22 #include <linux/sched/signal.h>
23 
24 #include "gfs2.h"
25 #include "incore.h"
26 #include "bmap.h"
27 #include "glock.h"
28 #include "inode.h"
29 #include "log.h"
30 #include "meta_io.h"
31 #include "quota.h"
32 #include "trans.h"
33 #include "rgrp.h"
34 #include "super.h"
35 #include "util.h"
36 #include "glops.h"
37 #include "aops.h"
38 
39 
40 /**
41  * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
42  * @inode: The inode
43  * @lblock: The block number to look up
44  * @bh_result: The buffer head to return the result in
45  * @create: Non-zero if we may add block to the file
46  *
47  * Returns: errno
48  */
49 
50 static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
51 				  struct buffer_head *bh_result, int create)
52 {
53 	int error;
54 
55 	error = gfs2_block_map(inode, lblock, bh_result, 0);
56 	if (error)
57 		return error;
58 	if (!buffer_mapped(bh_result))
59 		return -ENODATA;
60 	return 0;
61 }
62 
63 /**
64  * gfs2_write_jdata_folio - gfs2 jdata-specific version of block_write_full_folio
65  * @folio: The folio to write
66  * @wbc: The writeback control
67  *
68  * This is the same as calling block_write_full_folio, but it also
69  * writes pages outside of i_size
70  */
71 static int gfs2_write_jdata_folio(struct folio *folio,
72 				 struct writeback_control *wbc)
73 {
74 	struct inode * const inode = folio->mapping->host;
75 	loff_t i_size = i_size_read(inode);
76 
77 	/*
78 	 * The folio straddles i_size.  It must be zeroed out on each and every
79 	 * writepage invocation because it may be mmapped.  "A file is mapped
80 	 * in multiples of the page size.  For a file that is not a multiple of
81 	 * the page size, the remaining memory is zeroed when mapped, and
82 	 * writes to that region are not written out to the file."
83 	 */
84 	if (folio_pos(folio) < i_size &&
85 	    i_size < folio_pos(folio) + folio_size(folio))
86 		folio_zero_segment(folio, offset_in_folio(folio, i_size),
87 				folio_size(folio));
88 
89 	return __block_write_full_folio(inode, folio, gfs2_get_block_noalloc,
90 			wbc);
91 }
92 
93 /**
94  * __gfs2_jdata_write_folio - The core of jdata writepage
95  * @folio: The folio to write
96  * @wbc: The writeback control
97  *
98  * Implements the core of write back. If a transaction is required then
99  * the checked flag will have been set and the transaction will have
100  * already been started before this is called.
101  */
102 static int __gfs2_jdata_write_folio(struct folio *folio,
103 		struct writeback_control *wbc)
104 {
105 	struct inode *inode = folio->mapping->host;
106 	struct gfs2_inode *ip = GFS2_I(inode);
107 
108 	if (folio_test_checked(folio)) {
109 		folio_clear_checked(folio);
110 		if (!folio_buffers(folio)) {
111 			create_empty_buffers(folio,
112 					inode->i_sb->s_blocksize,
113 					BIT(BH_Dirty)|BIT(BH_Uptodate));
114 		}
115 		gfs2_trans_add_databufs(ip->i_gl, folio, 0, folio_size(folio));
116 	}
117 	return gfs2_write_jdata_folio(folio, wbc);
118 }
119 
120 /**
121  * gfs2_jdata_writeback - Write jdata folios to the log
122  * @mapping: The mapping to write
123  * @wbc: The writeback control
124  *
125  * Returns: errno
126  */
127 int gfs2_jdata_writeback(struct address_space *mapping, struct writeback_control *wbc)
128 {
129 	struct inode *inode = mapping->host;
130 	struct gfs2_inode *ip = GFS2_I(inode);
131 	struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
132 	struct folio *folio = NULL;
133 	int error;
134 
135 	BUG_ON(current->journal_info);
136 	if (gfs2_assert_withdraw(sdp, ip->i_gl->gl_state == LM_ST_EXCLUSIVE))
137 		return 0;
138 
139 	while ((folio = writeback_iter(mapping, wbc, folio, &error))) {
140 		if (folio_test_checked(folio)) {
141 			folio_redirty_for_writepage(wbc, folio);
142 			folio_unlock(folio);
143 			continue;
144 		}
145 		error = __gfs2_jdata_write_folio(folio, wbc);
146 	}
147 
148 	return error;
149 }
150 
151 /**
152  * gfs2_writepages - Write a bunch of dirty pages back to disk
153  * @mapping: The mapping to write
154  * @wbc: Write-back control
155  *
156  * Used for both ordered and writeback modes.
157  */
158 static int gfs2_writepages(struct address_space *mapping,
159 			   struct writeback_control *wbc)
160 {
161 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
162 	struct iomap_writepage_ctx wpc = { };
163 	int ret;
164 
165 	/*
166 	 * Even if we didn't write enough pages here, we might still be holding
167 	 * dirty pages in the ail. We forcibly flush the ail because we don't
168 	 * want balance_dirty_pages() to loop indefinitely trying to write out
169 	 * pages held in the ail that it can't find.
170 	 */
171 	ret = iomap_writepages(mapping, wbc, &wpc, &gfs2_writeback_ops);
172 	if (ret == 0 && wbc->nr_to_write > 0)
173 		set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
174 	return ret;
175 }
176 
177 /**
178  * gfs2_write_jdata_batch - Write back a folio batch's worth of folios
179  * @mapping: The mapping
180  * @wbc: The writeback control
181  * @fbatch: The batch of folios
182  * @done_index: Page index
183  *
184  * Returns: non-zero if loop should terminate, zero otherwise
185  */
186 
187 static int gfs2_write_jdata_batch(struct address_space *mapping,
188 				    struct writeback_control *wbc,
189 				    struct folio_batch *fbatch,
190 				    pgoff_t *done_index)
191 {
192 	struct inode *inode = mapping->host;
193 	struct gfs2_sbd *sdp = GFS2_SB(inode);
194 	unsigned nrblocks;
195 	int i;
196 	int ret;
197 	size_t size = 0;
198 	int nr_folios = folio_batch_count(fbatch);
199 
200 	for (i = 0; i < nr_folios; i++)
201 		size += folio_size(fbatch->folios[i]);
202 	nrblocks = size >> inode->i_blkbits;
203 
204 	ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
205 	if (ret < 0)
206 		return ret;
207 
208 	for (i = 0; i < nr_folios; i++) {
209 		struct folio *folio = fbatch->folios[i];
210 
211 		*done_index = folio->index;
212 
213 		folio_lock(folio);
214 
215 		if (unlikely(folio->mapping != mapping)) {
216 continue_unlock:
217 			folio_unlock(folio);
218 			continue;
219 		}
220 
221 		if (!folio_test_dirty(folio)) {
222 			/* someone wrote it for us */
223 			goto continue_unlock;
224 		}
225 
226 		if (folio_test_writeback(folio)) {
227 			if (wbc->sync_mode != WB_SYNC_NONE)
228 				folio_wait_writeback(folio);
229 			else
230 				goto continue_unlock;
231 		}
232 
233 		BUG_ON(folio_test_writeback(folio));
234 		if (!folio_clear_dirty_for_io(folio))
235 			goto continue_unlock;
236 
237 		trace_wbc_writepage(wbc, inode_to_bdi(inode));
238 
239 		ret = __gfs2_jdata_write_folio(folio, wbc);
240 		if (unlikely(ret)) {
241 			/*
242 			 * done_index is set past this page, so media errors
243 			 * will not choke background writeout for the entire
244 			 * file. This has consequences for range_cyclic
245 			 * semantics (ie. it may not be suitable for data
246 			 * integrity writeout).
247 			 */
248 			*done_index = folio_next_index(folio);
249 			ret = 1;
250 			break;
251 		}
252 
253 		/*
254 		 * We stop writing back only if we are not doing
255 		 * integrity sync. In case of integrity sync we have to
256 		 * keep going until we have written all the pages
257 		 * we tagged for writeback prior to entering this loop.
258 		 */
259 		if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
260 			ret = 1;
261 			break;
262 		}
263 
264 	}
265 	gfs2_trans_end(sdp);
266 	return ret;
267 }
268 
269 /**
270  * gfs2_write_cache_jdata - Like write_cache_pages but different
271  * @mapping: The mapping to write
272  * @wbc: The writeback control
273  *
274  * The reason that we use our own function here is that we need to
275  * start transactions before we grab page locks. This allows us
276  * to get the ordering right.
277  */
278 
279 static int gfs2_write_cache_jdata(struct address_space *mapping,
280 				  struct writeback_control *wbc)
281 {
282 	int ret = 0;
283 	int done = 0;
284 	struct folio_batch fbatch;
285 	int nr_folios;
286 	pgoff_t writeback_index;
287 	pgoff_t index;
288 	pgoff_t end;
289 	pgoff_t done_index;
290 	int cycled;
291 	int range_whole = 0;
292 	xa_mark_t tag;
293 
294 	folio_batch_init(&fbatch);
295 	if (wbc->range_cyclic) {
296 		writeback_index = mapping->writeback_index; /* prev offset */
297 		index = writeback_index;
298 		if (index == 0)
299 			cycled = 1;
300 		else
301 			cycled = 0;
302 		end = -1;
303 	} else {
304 		index = wbc->range_start >> PAGE_SHIFT;
305 		end = wbc->range_end >> PAGE_SHIFT;
306 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
307 			range_whole = 1;
308 		cycled = 1; /* ignore range_cyclic tests */
309 	}
310 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
311 		tag = PAGECACHE_TAG_TOWRITE;
312 	else
313 		tag = PAGECACHE_TAG_DIRTY;
314 
315 retry:
316 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
317 		tag_pages_for_writeback(mapping, index, end);
318 	done_index = index;
319 	while (!done && (index <= end)) {
320 		nr_folios = filemap_get_folios_tag(mapping, &index, end,
321 				tag, &fbatch);
322 		if (nr_folios == 0)
323 			break;
324 
325 		ret = gfs2_write_jdata_batch(mapping, wbc, &fbatch,
326 				&done_index);
327 		if (ret)
328 			done = 1;
329 		if (ret > 0)
330 			ret = 0;
331 		folio_batch_release(&fbatch);
332 		cond_resched();
333 	}
334 
335 	if (!cycled && !done) {
336 		/*
337 		 * range_cyclic:
338 		 * We hit the last page and there is more work to be done: wrap
339 		 * back to the start of the file
340 		 */
341 		cycled = 1;
342 		index = 0;
343 		end = writeback_index - 1;
344 		goto retry;
345 	}
346 
347 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
348 		mapping->writeback_index = done_index;
349 
350 	return ret;
351 }
352 
353 
354 /**
355  * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
356  * @mapping: The mapping to write
357  * @wbc: The writeback control
358  *
359  */
360 
361 static int gfs2_jdata_writepages(struct address_space *mapping,
362 				 struct writeback_control *wbc)
363 {
364 	struct gfs2_inode *ip = GFS2_I(mapping->host);
365 	struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
366 	int ret;
367 
368 	ret = gfs2_write_cache_jdata(mapping, wbc);
369 	if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
370 		gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
371 			       GFS2_LFC_JDATA_WPAGES);
372 		ret = gfs2_write_cache_jdata(mapping, wbc);
373 	}
374 	return ret;
375 }
376 
377 /**
378  * stuffed_read_folio - Fill in a Linux folio with stuffed file data
379  * @ip: the inode
380  * @folio: the folio
381  *
382  * Returns: errno
383  */
384 static int stuffed_read_folio(struct gfs2_inode *ip, struct folio *folio)
385 {
386 	struct buffer_head *dibh = NULL;
387 	size_t dsize = i_size_read(&ip->i_inode);
388 	void *from = NULL;
389 	int error = 0;
390 
391 	/*
392 	 * Due to the order of unstuffing files and ->fault(), we can be
393 	 * asked for a zero folio in the case of a stuffed file being extended,
394 	 * so we need to supply one here. It doesn't happen often.
395 	 */
396 	if (unlikely(folio->index)) {
397 		dsize = 0;
398 	} else {
399 		error = gfs2_meta_inode_buffer(ip, &dibh);
400 		if (error)
401 			goto out;
402 		from = dibh->b_data + sizeof(struct gfs2_dinode);
403 	}
404 
405 	folio_fill_tail(folio, 0, from, dsize);
406 	brelse(dibh);
407 out:
408 	folio_end_read(folio, error == 0);
409 
410 	return error;
411 }
412 
413 /**
414  * gfs2_read_folio - read a folio from a file
415  * @file: The file to read
416  * @folio: The folio in the file
417  */
418 static int gfs2_read_folio(struct file *file, struct folio *folio)
419 {
420 	struct inode *inode = folio->mapping->host;
421 	struct gfs2_inode *ip = GFS2_I(inode);
422 	struct gfs2_sbd *sdp = GFS2_SB(inode);
423 	int error;
424 
425 	if (!gfs2_is_jdata(ip) ||
426 	    (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
427 		error = iomap_read_folio(folio, &gfs2_iomap_ops);
428 	} else if (gfs2_is_stuffed(ip)) {
429 		error = stuffed_read_folio(ip, folio);
430 	} else {
431 		error = mpage_read_folio(folio, gfs2_block_map);
432 	}
433 
434 	if (gfs2_withdrawing_or_withdrawn(sdp))
435 		return -EIO;
436 
437 	return error;
438 }
439 
440 /**
441  * gfs2_internal_read - read an internal file
442  * @ip: The gfs2 inode
443  * @buf: The buffer to fill
444  * @pos: The file position
445  * @size: The amount to read
446  *
447  */
448 
449 ssize_t gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
450 			   size_t size)
451 {
452 	struct address_space *mapping = ip->i_inode.i_mapping;
453 	unsigned long index = *pos >> PAGE_SHIFT;
454 	size_t copied = 0;
455 
456 	do {
457 		size_t offset, chunk;
458 		struct folio *folio;
459 
460 		folio = read_cache_folio(mapping, index, gfs2_read_folio, NULL);
461 		if (IS_ERR(folio)) {
462 			if (PTR_ERR(folio) == -EINTR)
463 				continue;
464 			return PTR_ERR(folio);
465 		}
466 		offset = *pos + copied - folio_pos(folio);
467 		chunk = min(size - copied, folio_size(folio) - offset);
468 		memcpy_from_folio(buf + copied, folio, offset, chunk);
469 		index = folio_next_index(folio);
470 		folio_put(folio);
471 		copied += chunk;
472 	} while(copied < size);
473 	(*pos) += size;
474 	return size;
475 }
476 
477 /**
478  * gfs2_readahead - Read a bunch of pages at once
479  * @rac: Read-ahead control structure
480  *
481  * Some notes:
482  * 1. This is only for readahead, so we can simply ignore any things
483  *    which are slightly inconvenient (such as locking conflicts between
484  *    the page lock and the glock) and return having done no I/O. Its
485  *    obviously not something we'd want to do on too regular a basis.
486  *    Any I/O we ignore at this time will be done via readpage later.
487  * 2. We don't handle stuffed files here we let readpage do the honours.
488  * 3. mpage_readahead() does most of the heavy lifting in the common case.
489  * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
490  */
491 
492 static void gfs2_readahead(struct readahead_control *rac)
493 {
494 	struct inode *inode = rac->mapping->host;
495 	struct gfs2_inode *ip = GFS2_I(inode);
496 
497 	if (gfs2_is_stuffed(ip))
498 		;
499 	else if (gfs2_is_jdata(ip))
500 		mpage_readahead(rac, gfs2_block_map);
501 	else
502 		iomap_readahead(rac, &gfs2_iomap_ops);
503 }
504 
505 /**
506  * adjust_fs_space - Adjusts the free space available due to gfs2_grow
507  * @inode: the rindex inode
508  */
509 void adjust_fs_space(struct inode *inode)
510 {
511 	struct gfs2_sbd *sdp = GFS2_SB(inode);
512 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
513 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
514 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
515 	struct buffer_head *m_bh;
516 	u64 fs_total, new_free;
517 
518 	if (gfs2_trans_begin(sdp, 2 * RES_STATFS, 0) != 0)
519 		return;
520 
521 	/* Total up the file system space, according to the latest rindex. */
522 	fs_total = gfs2_ri_total(sdp);
523 	if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
524 		goto out;
525 
526 	spin_lock(&sdp->sd_statfs_spin);
527 	gfs2_statfs_change_in(m_sc, m_bh->b_data +
528 			      sizeof(struct gfs2_dinode));
529 	if (fs_total > (m_sc->sc_total + l_sc->sc_total))
530 		new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
531 	else
532 		new_free = 0;
533 	spin_unlock(&sdp->sd_statfs_spin);
534 	fs_warn(sdp, "File system extended by %llu blocks.\n",
535 		(unsigned long long)new_free);
536 	gfs2_statfs_change(sdp, new_free, new_free, 0);
537 
538 	update_statfs(sdp, m_bh);
539 	brelse(m_bh);
540 out:
541 	sdp->sd_rindex_uptodate = 0;
542 	gfs2_trans_end(sdp);
543 }
544 
545 static bool gfs2_jdata_dirty_folio(struct address_space *mapping,
546 		struct folio *folio)
547 {
548 	if (current->journal_info)
549 		folio_set_checked(folio);
550 	return block_dirty_folio(mapping, folio);
551 }
552 
553 /**
554  * gfs2_bmap - Block map function
555  * @mapping: Address space info
556  * @lblock: The block to map
557  *
558  * Returns: The disk address for the block or 0 on hole or error
559  */
560 
561 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
562 {
563 	struct gfs2_inode *ip = GFS2_I(mapping->host);
564 	struct gfs2_holder i_gh;
565 	sector_t dblock = 0;
566 	int error;
567 
568 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
569 	if (error)
570 		return 0;
571 
572 	if (!gfs2_is_stuffed(ip))
573 		dblock = iomap_bmap(mapping, lblock, &gfs2_iomap_ops);
574 
575 	gfs2_glock_dq_uninit(&i_gh);
576 
577 	return dblock;
578 }
579 
580 static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
581 {
582 	struct gfs2_bufdata *bd;
583 
584 	lock_buffer(bh);
585 	gfs2_log_lock(sdp);
586 	clear_buffer_dirty(bh);
587 	bd = bh->b_private;
588 	if (bd) {
589 		if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
590 			list_del_init(&bd->bd_list);
591 		else {
592 			spin_lock(&sdp->sd_ail_lock);
593 			gfs2_remove_from_journal(bh, REMOVE_JDATA);
594 			spin_unlock(&sdp->sd_ail_lock);
595 		}
596 	}
597 	bh->b_bdev = NULL;
598 	clear_buffer_mapped(bh);
599 	clear_buffer_req(bh);
600 	clear_buffer_new(bh);
601 	gfs2_log_unlock(sdp);
602 	unlock_buffer(bh);
603 }
604 
605 static void gfs2_invalidate_folio(struct folio *folio, size_t offset,
606 				size_t length)
607 {
608 	struct gfs2_sbd *sdp = GFS2_SB(folio->mapping->host);
609 	size_t stop = offset + length;
610 	int partial_page = (offset || length < folio_size(folio));
611 	struct buffer_head *bh, *head;
612 	unsigned long pos = 0;
613 
614 	BUG_ON(!folio_test_locked(folio));
615 	if (!partial_page)
616 		folio_clear_checked(folio);
617 	head = folio_buffers(folio);
618 	if (!head)
619 		goto out;
620 
621 	bh = head;
622 	do {
623 		if (pos + bh->b_size > stop)
624 			return;
625 
626 		if (offset <= pos)
627 			gfs2_discard(sdp, bh);
628 		pos += bh->b_size;
629 		bh = bh->b_this_page;
630 	} while (bh != head);
631 out:
632 	if (!partial_page)
633 		filemap_release_folio(folio, 0);
634 }
635 
636 /**
637  * gfs2_release_folio - free the metadata associated with a folio
638  * @folio: the folio that's being released
639  * @gfp_mask: passed from Linux VFS, ignored by us
640  *
641  * Calls try_to_free_buffers() to free the buffers and put the folio if the
642  * buffers can be released.
643  *
644  * Returns: true if the folio was put or else false
645  */
646 
647 bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
648 {
649 	struct address_space *mapping = folio->mapping;
650 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
651 	struct buffer_head *bh, *head;
652 	struct gfs2_bufdata *bd;
653 
654 	head = folio_buffers(folio);
655 	if (!head)
656 		return false;
657 
658 	/*
659 	 * mm accommodates an old ext3 case where clean folios might
660 	 * not have had the dirty bit cleared.	Thus, it can send actual
661 	 * dirty folios to ->release_folio() via shrink_active_list().
662 	 *
663 	 * As a workaround, we skip folios that contain dirty buffers
664 	 * below.  Once ->release_folio isn't called on dirty folios
665 	 * anymore, we can warn on dirty buffers like we used to here
666 	 * again.
667 	 */
668 
669 	gfs2_log_lock(sdp);
670 	bh = head;
671 	do {
672 		if (atomic_read(&bh->b_count))
673 			goto cannot_release;
674 		bd = bh->b_private;
675 		if (bd && bd->bd_tr)
676 			goto cannot_release;
677 		if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
678 			goto cannot_release;
679 		bh = bh->b_this_page;
680 	} while (bh != head);
681 
682 	bh = head;
683 	do {
684 		bd = bh->b_private;
685 		if (bd) {
686 			gfs2_assert_warn(sdp, bd->bd_bh == bh);
687 			bd->bd_bh = NULL;
688 			bh->b_private = NULL;
689 			/*
690 			 * The bd may still be queued as a revoke, in which
691 			 * case we must not dequeue nor free it.
692 			 */
693 			if (!bd->bd_blkno && !list_empty(&bd->bd_list))
694 				list_del_init(&bd->bd_list);
695 			if (list_empty(&bd->bd_list))
696 				kmem_cache_free(gfs2_bufdata_cachep, bd);
697 		}
698 
699 		bh = bh->b_this_page;
700 	} while (bh != head);
701 	gfs2_log_unlock(sdp);
702 
703 	return try_to_free_buffers(folio);
704 
705 cannot_release:
706 	gfs2_log_unlock(sdp);
707 	return false;
708 }
709 
710 static const struct address_space_operations gfs2_aops = {
711 	.writepages = gfs2_writepages,
712 	.read_folio = gfs2_read_folio,
713 	.readahead = gfs2_readahead,
714 	.dirty_folio = iomap_dirty_folio,
715 	.release_folio = iomap_release_folio,
716 	.invalidate_folio = iomap_invalidate_folio,
717 	.bmap = gfs2_bmap,
718 	.migrate_folio = filemap_migrate_folio,
719 	.is_partially_uptodate = iomap_is_partially_uptodate,
720 	.error_remove_folio = generic_error_remove_folio,
721 };
722 
723 static const struct address_space_operations gfs2_jdata_aops = {
724 	.writepages = gfs2_jdata_writepages,
725 	.read_folio = gfs2_read_folio,
726 	.readahead = gfs2_readahead,
727 	.dirty_folio = gfs2_jdata_dirty_folio,
728 	.bmap = gfs2_bmap,
729 	.migrate_folio = buffer_migrate_folio,
730 	.invalidate_folio = gfs2_invalidate_folio,
731 	.release_folio = gfs2_release_folio,
732 	.is_partially_uptodate = block_is_partially_uptodate,
733 	.error_remove_folio = generic_error_remove_folio,
734 };
735 
736 void gfs2_set_aops(struct inode *inode)
737 {
738 	if (gfs2_is_jdata(GFS2_I(inode)))
739 		inode->i_mapping->a_ops = &gfs2_jdata_aops;
740 	else
741 		inode->i_mapping->a_ops = &gfs2_aops;
742 }
743