1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
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>
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>
40 void gfs2_trans_add_databufs(struct gfs2_inode *ip, struct folio *folio,
41 size_t from, size_t len)
43 struct buffer_head *head = folio_buffers(folio);
44 unsigned int bsize = head->b_size;
45 struct buffer_head *bh;
46 size_t to = from + len;
49 for (bh = head, start = 0; bh != head || !start;
50 bh = bh->b_this_page, start = end) {
56 set_buffer_uptodate(bh);
57 gfs2_trans_add_data(ip->i_gl, bh);
62 * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
64 * @lblock: The block number to look up
65 * @bh_result: The buffer head to return the result in
66 * @create: Non-zero if we may add block to the file
71 static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
72 struct buffer_head *bh_result, int create)
76 error = gfs2_block_map(inode, lblock, bh_result, 0);
79 if (!buffer_mapped(bh_result))
85 * gfs2_write_jdata_folio - gfs2 jdata-specific version of block_write_full_folio
86 * @folio: The folio to write
87 * @wbc: The writeback control
89 * This is the same as calling block_write_full_folio, but it also
90 * writes pages outside of i_size
92 static int gfs2_write_jdata_folio(struct folio *folio,
93 struct writeback_control *wbc)
95 struct inode * const inode = folio->mapping->host;
96 loff_t i_size = i_size_read(inode);
99 * The folio straddles i_size. It must be zeroed out on each and every
100 * writepage invocation because it may be mmapped. "A file is mapped
101 * in multiples of the page size. For a file that is not a multiple of
102 * the page size, the remaining memory is zeroed when mapped, and
103 * writes to that region are not written out to the file."
105 if (folio_pos(folio) < i_size &&
106 i_size < folio_pos(folio) + folio_size(folio))
107 folio_zero_segment(folio, offset_in_folio(folio, i_size),
110 return __block_write_full_folio(inode, folio, gfs2_get_block_noalloc,
115 * __gfs2_jdata_write_folio - The core of jdata writepage
116 * @folio: The folio to write
117 * @wbc: The writeback control
119 * Implements the core of write back. If a transaction is required then
120 * the checked flag will have been set and the transaction will have
121 * already been started before this is called.
123 static int __gfs2_jdata_write_folio(struct folio *folio,
124 struct writeback_control *wbc)
126 struct inode *inode = folio->mapping->host;
127 struct gfs2_inode *ip = GFS2_I(inode);
129 if (folio_test_checked(folio)) {
130 folio_clear_checked(folio);
131 if (!folio_buffers(folio)) {
132 create_empty_buffers(folio,
133 inode->i_sb->s_blocksize,
134 BIT(BH_Dirty)|BIT(BH_Uptodate));
136 gfs2_trans_add_databufs(ip, folio, 0, folio_size(folio));
138 return gfs2_write_jdata_folio(folio, wbc);
142 * gfs2_jdata_writepage - Write complete page
143 * @page: Page to write
144 * @wbc: The writeback control
150 static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
152 struct folio *folio = page_folio(page);
153 struct inode *inode = page->mapping->host;
154 struct gfs2_inode *ip = GFS2_I(inode);
155 struct gfs2_sbd *sdp = GFS2_SB(inode);
157 if (gfs2_assert_withdraw(sdp, ip->i_gl->gl_state == LM_ST_EXCLUSIVE))
159 if (folio_test_checked(folio) || current->journal_info)
161 return __gfs2_jdata_write_folio(folio, wbc);
164 folio_redirty_for_writepage(wbc, folio);
171 * gfs2_writepages - Write a bunch of dirty pages back to disk
172 * @mapping: The mapping to write
173 * @wbc: Write-back control
175 * Used for both ordered and writeback modes.
177 static int gfs2_writepages(struct address_space *mapping,
178 struct writeback_control *wbc)
180 struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
181 struct iomap_writepage_ctx wpc = { };
185 * Even if we didn't write enough pages here, we might still be holding
186 * dirty pages in the ail. We forcibly flush the ail because we don't
187 * want balance_dirty_pages() to loop indefinitely trying to write out
188 * pages held in the ail that it can't find.
190 ret = iomap_writepages(mapping, wbc, &wpc, &gfs2_writeback_ops);
191 if (ret == 0 && wbc->nr_to_write > 0)
192 set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
197 * gfs2_write_jdata_batch - Write back a folio batch's worth of folios
198 * @mapping: The mapping
199 * @wbc: The writeback control
200 * @fbatch: The batch of folios
201 * @done_index: Page index
203 * Returns: non-zero if loop should terminate, zero otherwise
206 static int gfs2_write_jdata_batch(struct address_space *mapping,
207 struct writeback_control *wbc,
208 struct folio_batch *fbatch,
211 struct inode *inode = mapping->host;
212 struct gfs2_sbd *sdp = GFS2_SB(inode);
217 int nr_folios = folio_batch_count(fbatch);
219 for (i = 0; i < nr_folios; i++)
220 size += folio_size(fbatch->folios[i]);
221 nrblocks = size >> inode->i_blkbits;
223 ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
227 for (i = 0; i < nr_folios; i++) {
228 struct folio *folio = fbatch->folios[i];
230 *done_index = folio->index;
234 if (unlikely(folio->mapping != mapping)) {
240 if (!folio_test_dirty(folio)) {
241 /* someone wrote it for us */
242 goto continue_unlock;
245 if (folio_test_writeback(folio)) {
246 if (wbc->sync_mode != WB_SYNC_NONE)
247 folio_wait_writeback(folio);
249 goto continue_unlock;
252 BUG_ON(folio_test_writeback(folio));
253 if (!folio_clear_dirty_for_io(folio))
254 goto continue_unlock;
256 trace_wbc_writepage(wbc, inode_to_bdi(inode));
258 ret = __gfs2_jdata_write_folio(folio, wbc);
260 if (ret == AOP_WRITEPAGE_ACTIVATE) {
266 * done_index is set past this page,
267 * so media errors will not choke
268 * background writeout for the entire
269 * file. This has consequences for
270 * range_cyclic semantics (ie. it may
271 * not be suitable for data integrity
274 *done_index = folio_next_index(folio);
281 * We stop writing back only if we are not doing
282 * integrity sync. In case of integrity sync we have to
283 * keep going until we have written all the pages
284 * we tagged for writeback prior to entering this loop.
286 if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
297 * gfs2_write_cache_jdata - Like write_cache_pages but different
298 * @mapping: The mapping to write
299 * @wbc: The writeback control
301 * The reason that we use our own function here is that we need to
302 * start transactions before we grab page locks. This allows us
303 * to get the ordering right.
306 static int gfs2_write_cache_jdata(struct address_space *mapping,
307 struct writeback_control *wbc)
311 struct folio_batch fbatch;
313 pgoff_t writeback_index;
321 folio_batch_init(&fbatch);
322 if (wbc->range_cyclic) {
323 writeback_index = mapping->writeback_index; /* prev offset */
324 index = writeback_index;
331 index = wbc->range_start >> PAGE_SHIFT;
332 end = wbc->range_end >> PAGE_SHIFT;
333 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
335 cycled = 1; /* ignore range_cyclic tests */
337 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
338 tag = PAGECACHE_TAG_TOWRITE;
340 tag = PAGECACHE_TAG_DIRTY;
343 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
344 tag_pages_for_writeback(mapping, index, end);
346 while (!done && (index <= end)) {
347 nr_folios = filemap_get_folios_tag(mapping, &index, end,
352 ret = gfs2_write_jdata_batch(mapping, wbc, &fbatch,
358 folio_batch_release(&fbatch);
362 if (!cycled && !done) {
365 * We hit the last page and there is more work to be done: wrap
366 * back to the start of the file
370 end = writeback_index - 1;
374 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
375 mapping->writeback_index = done_index;
382 * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
383 * @mapping: The mapping to write
384 * @wbc: The writeback control
388 static int gfs2_jdata_writepages(struct address_space *mapping,
389 struct writeback_control *wbc)
391 struct gfs2_inode *ip = GFS2_I(mapping->host);
392 struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
395 ret = gfs2_write_cache_jdata(mapping, wbc);
396 if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
397 gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
398 GFS2_LFC_JDATA_WPAGES);
399 ret = gfs2_write_cache_jdata(mapping, wbc);
405 * stuffed_read_folio - Fill in a Linux folio with stuffed file data
411 static int stuffed_read_folio(struct gfs2_inode *ip, struct folio *folio)
413 struct buffer_head *dibh = NULL;
414 size_t dsize = i_size_read(&ip->i_inode);
419 * Due to the order of unstuffing files and ->fault(), we can be
420 * asked for a zero folio in the case of a stuffed file being extended,
421 * so we need to supply one here. It doesn't happen often.
423 if (unlikely(folio->index)) {
426 error = gfs2_meta_inode_buffer(ip, &dibh);
429 from = dibh->b_data + sizeof(struct gfs2_dinode);
432 folio_fill_tail(folio, 0, from, dsize);
435 folio_end_read(folio, error == 0);
441 * gfs2_read_folio - read a folio from a file
442 * @file: The file to read
443 * @folio: The folio in the file
445 static int gfs2_read_folio(struct file *file, struct folio *folio)
447 struct inode *inode = folio->mapping->host;
448 struct gfs2_inode *ip = GFS2_I(inode);
449 struct gfs2_sbd *sdp = GFS2_SB(inode);
452 if (!gfs2_is_jdata(ip) ||
453 (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
454 error = iomap_read_folio(folio, &gfs2_iomap_ops);
455 } else if (gfs2_is_stuffed(ip)) {
456 error = stuffed_read_folio(ip, folio);
458 error = mpage_read_folio(folio, gfs2_block_map);
461 if (gfs2_withdrawing_or_withdrawn(sdp))
468 * gfs2_internal_read - read an internal file
469 * @ip: The gfs2 inode
470 * @buf: The buffer to fill
471 * @pos: The file position
472 * @size: The amount to read
476 ssize_t gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
479 struct address_space *mapping = ip->i_inode.i_mapping;
480 unsigned long index = *pos >> PAGE_SHIFT;
484 size_t offset, chunk;
487 folio = read_cache_folio(mapping, index, gfs2_read_folio, NULL);
489 if (PTR_ERR(folio) == -EINTR)
491 return PTR_ERR(folio);
493 offset = *pos + copied - folio_pos(folio);
494 chunk = min(size - copied, folio_size(folio) - offset);
495 memcpy_from_folio(buf + copied, folio, offset, chunk);
496 index = folio_next_index(folio);
499 } while(copied < size);
505 * gfs2_readahead - Read a bunch of pages at once
506 * @rac: Read-ahead control structure
509 * 1. This is only for readahead, so we can simply ignore any things
510 * which are slightly inconvenient (such as locking conflicts between
511 * the page lock and the glock) and return having done no I/O. Its
512 * obviously not something we'd want to do on too regular a basis.
513 * Any I/O we ignore at this time will be done via readpage later.
514 * 2. We don't handle stuffed files here we let readpage do the honours.
515 * 3. mpage_readahead() does most of the heavy lifting in the common case.
516 * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
519 static void gfs2_readahead(struct readahead_control *rac)
521 struct inode *inode = rac->mapping->host;
522 struct gfs2_inode *ip = GFS2_I(inode);
524 if (gfs2_is_stuffed(ip))
526 else if (gfs2_is_jdata(ip))
527 mpage_readahead(rac, gfs2_block_map);
529 iomap_readahead(rac, &gfs2_iomap_ops);
533 * adjust_fs_space - Adjusts the free space available due to gfs2_grow
534 * @inode: the rindex inode
536 void adjust_fs_space(struct inode *inode)
538 struct gfs2_sbd *sdp = GFS2_SB(inode);
539 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
540 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
541 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
542 struct buffer_head *m_bh;
543 u64 fs_total, new_free;
545 if (gfs2_trans_begin(sdp, 2 * RES_STATFS, 0) != 0)
548 /* Total up the file system space, according to the latest rindex. */
549 fs_total = gfs2_ri_total(sdp);
550 if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
553 spin_lock(&sdp->sd_statfs_spin);
554 gfs2_statfs_change_in(m_sc, m_bh->b_data +
555 sizeof(struct gfs2_dinode));
556 if (fs_total > (m_sc->sc_total + l_sc->sc_total))
557 new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
560 spin_unlock(&sdp->sd_statfs_spin);
561 fs_warn(sdp, "File system extended by %llu blocks.\n",
562 (unsigned long long)new_free);
563 gfs2_statfs_change(sdp, new_free, new_free, 0);
565 update_statfs(sdp, m_bh);
568 sdp->sd_rindex_uptodate = 0;
572 static bool jdata_dirty_folio(struct address_space *mapping,
575 if (current->journal_info)
576 folio_set_checked(folio);
577 return block_dirty_folio(mapping, folio);
581 * gfs2_bmap - Block map function
582 * @mapping: Address space info
583 * @lblock: The block to map
585 * Returns: The disk address for the block or 0 on hole or error
588 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
590 struct gfs2_inode *ip = GFS2_I(mapping->host);
591 struct gfs2_holder i_gh;
595 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
599 if (!gfs2_is_stuffed(ip))
600 dblock = iomap_bmap(mapping, lblock, &gfs2_iomap_ops);
602 gfs2_glock_dq_uninit(&i_gh);
607 static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
609 struct gfs2_bufdata *bd;
613 clear_buffer_dirty(bh);
616 if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
617 list_del_init(&bd->bd_list);
619 spin_lock(&sdp->sd_ail_lock);
620 gfs2_remove_from_journal(bh, REMOVE_JDATA);
621 spin_unlock(&sdp->sd_ail_lock);
625 clear_buffer_mapped(bh);
626 clear_buffer_req(bh);
627 clear_buffer_new(bh);
628 gfs2_log_unlock(sdp);
632 static void gfs2_invalidate_folio(struct folio *folio, size_t offset,
635 struct gfs2_sbd *sdp = GFS2_SB(folio->mapping->host);
636 size_t stop = offset + length;
637 int partial_page = (offset || length < folio_size(folio));
638 struct buffer_head *bh, *head;
639 unsigned long pos = 0;
641 BUG_ON(!folio_test_locked(folio));
643 folio_clear_checked(folio);
644 head = folio_buffers(folio);
650 if (pos + bh->b_size > stop)
654 gfs2_discard(sdp, bh);
656 bh = bh->b_this_page;
657 } while (bh != head);
660 filemap_release_folio(folio, 0);
664 * gfs2_release_folio - free the metadata associated with a folio
665 * @folio: the folio that's being released
666 * @gfp_mask: passed from Linux VFS, ignored by us
668 * Calls try_to_free_buffers() to free the buffers and put the folio if the
669 * buffers can be released.
671 * Returns: true if the folio was put or else false
674 bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
676 struct address_space *mapping = folio->mapping;
677 struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
678 struct buffer_head *bh, *head;
679 struct gfs2_bufdata *bd;
681 head = folio_buffers(folio);
686 * mm accommodates an old ext3 case where clean folios might
687 * not have had the dirty bit cleared. Thus, it can send actual
688 * dirty folios to ->release_folio() via shrink_active_list().
690 * As a workaround, we skip folios that contain dirty buffers
691 * below. Once ->release_folio isn't called on dirty folios
692 * anymore, we can warn on dirty buffers like we used to here
699 if (atomic_read(&bh->b_count))
704 if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
706 bh = bh->b_this_page;
707 } while (bh != head);
713 gfs2_assert_warn(sdp, bd->bd_bh == bh);
715 bh->b_private = NULL;
717 * The bd may still be queued as a revoke, in which
718 * case we must not dequeue nor free it.
720 if (!bd->bd_blkno && !list_empty(&bd->bd_list))
721 list_del_init(&bd->bd_list);
722 if (list_empty(&bd->bd_list))
723 kmem_cache_free(gfs2_bufdata_cachep, bd);
726 bh = bh->b_this_page;
727 } while (bh != head);
728 gfs2_log_unlock(sdp);
730 return try_to_free_buffers(folio);
733 gfs2_log_unlock(sdp);
737 static const struct address_space_operations gfs2_aops = {
738 .writepages = gfs2_writepages,
739 .read_folio = gfs2_read_folio,
740 .readahead = gfs2_readahead,
741 .dirty_folio = iomap_dirty_folio,
742 .release_folio = iomap_release_folio,
743 .invalidate_folio = iomap_invalidate_folio,
745 .migrate_folio = filemap_migrate_folio,
746 .is_partially_uptodate = iomap_is_partially_uptodate,
747 .error_remove_folio = generic_error_remove_folio,
750 static const struct address_space_operations gfs2_jdata_aops = {
751 .writepage = gfs2_jdata_writepage,
752 .writepages = gfs2_jdata_writepages,
753 .read_folio = gfs2_read_folio,
754 .readahead = gfs2_readahead,
755 .dirty_folio = jdata_dirty_folio,
757 .migrate_folio = buffer_migrate_folio,
758 .invalidate_folio = gfs2_invalidate_folio,
759 .release_folio = gfs2_release_folio,
760 .is_partially_uptodate = block_is_partially_uptodate,
761 .error_remove_folio = generic_error_remove_folio,
764 void gfs2_set_aops(struct inode *inode)
766 if (gfs2_is_jdata(GFS2_I(inode)))
767 inode->i_mapping->a_ops = &gfs2_jdata_aops;
769 inode->i_mapping->a_ops = &gfs2_aops;