xref: /linux/fs/buffer.c (revision 3decb8564eff88a2533f83b01cec2cf9259c3eaf)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/buffer.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 2002  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Removed a lot of unnecessary code and simplified things now that
121da177e4SLinus Torvalds  * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Speed up hash, lru, and free list operations.  Use gfp() for allocating
151da177e4SLinus Torvalds  * hash table, use SLAB cache for buffer heads. SMP threading.  -DaveM
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Added 32k buffer block sizes - these are required older ARM systems. - RMK
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/kernel.h>
23f361bf4aSIngo Molnar #include <linux/sched/signal.h>
241da177e4SLinus Torvalds #include <linux/syscalls.h>
251da177e4SLinus Torvalds #include <linux/fs.h>
26ae259a9cSChristoph Hellwig #include <linux/iomap.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/percpu.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
311da177e4SLinus Torvalds #include <linux/blkdev.h>
321da177e4SLinus Torvalds #include <linux/file.h>
331da177e4SLinus Torvalds #include <linux/quotaops.h>
341da177e4SLinus Torvalds #include <linux/highmem.h>
35630d9c47SPaul Gortmaker #include <linux/export.h>
36bafc0dbaSTejun Heo #include <linux/backing-dev.h>
371da177e4SLinus Torvalds #include <linux/writeback.h>
381da177e4SLinus Torvalds #include <linux/hash.h>
391da177e4SLinus Torvalds #include <linux/suspend.h>
401da177e4SLinus Torvalds #include <linux/buffer_head.h>
4155e829afSAndrew Morton #include <linux/task_io_accounting_ops.h>
421da177e4SLinus Torvalds #include <linux/bio.h>
431da177e4SLinus Torvalds #include <linux/cpu.h>
441da177e4SLinus Torvalds #include <linux/bitops.h>
451da177e4SLinus Torvalds #include <linux/mpage.h>
46fb1c8f93SIngo Molnar #include <linux/bit_spinlock.h>
4729f3ad7dSJan Kara #include <linux/pagevec.h>
48f745c6f5SShakeel Butt #include <linux/sched/mm.h>
495305cb83STejun Heo #include <trace/events/block.h>
5031fb992cSEric Biggers #include <linux/fscrypt.h>
514fa512ceSEric Biggers #include <linux/fsverity.h>
528a237adfSMarcelo Tosatti #include <linux/sched/isolation.h>
531da177e4SLinus Torvalds 
542b211dc0SBen Dooks #include "internal.h"
552b211dc0SBen Dooks 
561da177e4SLinus Torvalds static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
575bdf402aSRitesh Harjani (IBM) static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
581420c4a5SBart Van Assche 			  struct writeback_control *wbc);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
611da177e4SLinus Torvalds 
62f0059afdSTejun Heo inline void touch_buffer(struct buffer_head *bh)
63f0059afdSTejun Heo {
645305cb83STejun Heo 	trace_block_touch_buffer(bh);
6503c5f331SMatthew Wilcox (Oracle) 	folio_mark_accessed(bh->b_folio);
66f0059afdSTejun Heo }
67f0059afdSTejun Heo EXPORT_SYMBOL(touch_buffer);
68f0059afdSTejun Heo 
69fc9b52cdSHarvey Harrison void __lock_buffer(struct buffer_head *bh)
701da177e4SLinus Torvalds {
7174316201SNeilBrown 	wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds EXPORT_SYMBOL(__lock_buffer);
741da177e4SLinus Torvalds 
75fc9b52cdSHarvey Harrison void unlock_buffer(struct buffer_head *bh)
761da177e4SLinus Torvalds {
7751b07fc3SNick Piggin 	clear_bit_unlock(BH_Lock, &bh->b_state);
784e857c58SPeter Zijlstra 	smp_mb__after_atomic();
791da177e4SLinus Torvalds 	wake_up_bit(&bh->b_state, BH_Lock);
801da177e4SLinus Torvalds }
811fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(unlock_buffer);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
84520f301cSMatthew Wilcox (Oracle)  * Returns if the folio has dirty or writeback buffers. If all the buffers
85520f301cSMatthew Wilcox (Oracle)  * are unlocked and clean then the folio_test_dirty information is stale. If
86520f301cSMatthew Wilcox (Oracle)  * any of the buffers are locked, it is assumed they are locked for IO.
87b4597226SMel Gorman  */
88520f301cSMatthew Wilcox (Oracle) void buffer_check_dirty_writeback(struct folio *folio,
89b4597226SMel Gorman 				     bool *dirty, bool *writeback)
90b4597226SMel Gorman {
91b4597226SMel Gorman 	struct buffer_head *head, *bh;
92b4597226SMel Gorman 	*dirty = false;
93b4597226SMel Gorman 	*writeback = false;
94b4597226SMel Gorman 
95520f301cSMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
96b4597226SMel Gorman 
97520f301cSMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
98520f301cSMatthew Wilcox (Oracle) 	if (!head)
99b4597226SMel Gorman 		return;
100b4597226SMel Gorman 
101520f301cSMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio))
102b4597226SMel Gorman 		*writeback = true;
103b4597226SMel Gorman 
104b4597226SMel Gorman 	bh = head;
105b4597226SMel Gorman 	do {
106b4597226SMel Gorman 		if (buffer_locked(bh))
107b4597226SMel Gorman 			*writeback = true;
108b4597226SMel Gorman 
109b4597226SMel Gorman 		if (buffer_dirty(bh))
110b4597226SMel Gorman 			*dirty = true;
111b4597226SMel Gorman 
112b4597226SMel Gorman 		bh = bh->b_this_page;
113b4597226SMel Gorman 	} while (bh != head);
114b4597226SMel Gorman }
115b4597226SMel Gorman 
116b4597226SMel Gorman /*
1171da177e4SLinus Torvalds  * Block until a buffer comes unlocked.  This doesn't stop it
1181da177e4SLinus Torvalds  * from becoming locked again - you have to lock it yourself
1191da177e4SLinus Torvalds  * if you want to preserve its state.
1201da177e4SLinus Torvalds  */
1211da177e4SLinus Torvalds void __wait_on_buffer(struct buffer_head * bh)
1221da177e4SLinus Torvalds {
12374316201SNeilBrown 	wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
1241da177e4SLinus Torvalds }
1251fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__wait_on_buffer);
1261da177e4SLinus Torvalds 
127b744c2acSRobert Elliott static void buffer_io_error(struct buffer_head *bh, char *msg)
1281da177e4SLinus Torvalds {
129432f16e6SRobert Elliott 	if (!test_bit(BH_Quiet, &bh->b_state))
130432f16e6SRobert Elliott 		printk_ratelimited(KERN_ERR
131a1c6f057SDmitry Monakhov 			"Buffer I/O error on dev %pg, logical block %llu%s\n",
132a1c6f057SDmitry Monakhov 			bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
13668671f35SDmitry Monakhov  * End-of-IO handler helper function which does not touch the bh after
13768671f35SDmitry Monakhov  * unlocking it.
13868671f35SDmitry Monakhov  * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
13968671f35SDmitry Monakhov  * a race there is benign: unlock_buffer() only use the bh's address for
14068671f35SDmitry Monakhov  * hashing after unlocking the buffer, so it doesn't actually touch the bh
14168671f35SDmitry Monakhov  * itself.
1421da177e4SLinus Torvalds  */
14368671f35SDmitry Monakhov static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	if (uptodate) {
1461da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
1471da177e4SLinus Torvalds 	} else {
14870246286SChristoph Hellwig 		/* This happens, due to failed read-ahead attempts. */
1491da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	unlock_buffer(bh);
15268671f35SDmitry Monakhov }
15368671f35SDmitry Monakhov 
15468671f35SDmitry Monakhov /*
15568671f35SDmitry Monakhov  * Default synchronous end-of-IO handler..  Just mark it up-to-date and
15679f59784SZhang Yi  * unlock the buffer.
15768671f35SDmitry Monakhov  */
15868671f35SDmitry Monakhov void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
15968671f35SDmitry Monakhov {
16068671f35SDmitry Monakhov 	__end_buffer_read_notouch(bh, uptodate);
1611da177e4SLinus Torvalds 	put_bh(bh);
1621da177e4SLinus Torvalds }
1631fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_read_sync);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	if (uptodate) {
1681da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
1691da177e4SLinus Torvalds 	} else {
170b744c2acSRobert Elliott 		buffer_io_error(bh, ", lost sync page write");
17187354e5dSJeff Layton 		mark_buffer_write_io_error(bh);
1721da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 	unlock_buffer(bh);
1751da177e4SLinus Torvalds 	put_bh(bh);
1761da177e4SLinus Torvalds }
1771fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_write_sync);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /*
1801da177e4SLinus Torvalds  * Various filesystems appear to want __find_get_block to be non-blocking.
1811da177e4SLinus Torvalds  * But it's the page lock which protects the buffers.  To get around this,
1821da177e4SLinus Torvalds  * we get exclusion from try_to_free_buffers with the blockdev mapping's
1831da177e4SLinus Torvalds  * private_lock.
1841da177e4SLinus Torvalds  *
185b93b0163SMatthew Wilcox  * Hack idea: for the blockdev mapping, private_lock contention
1861da177e4SLinus Torvalds  * may be quite high.  This code could TryLock the page, and if that
187b93b0163SMatthew Wilcox  * succeeds, there is no need to take private_lock.
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds static struct buffer_head *
190385fd4c5SCoywolf Qi Hunt __find_get_block_slow(struct block_device *bdev, sector_t block)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct inode *bd_inode = bdev->bd_inode;
1931da177e4SLinus Torvalds 	struct address_space *bd_mapping = bd_inode->i_mapping;
1941da177e4SLinus Torvalds 	struct buffer_head *ret = NULL;
1951da177e4SLinus Torvalds 	pgoff_t index;
1961da177e4SLinus Torvalds 	struct buffer_head *bh;
1971da177e4SLinus Torvalds 	struct buffer_head *head;
198eee25182SMatthew Wilcox (Oracle) 	struct folio *folio;
1991da177e4SLinus Torvalds 	int all_mapped = 1;
20043636c80STetsuo Handa 	static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
2011da177e4SLinus Torvalds 
20209cbfeafSKirill A. Shutemov 	index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
203eee25182SMatthew Wilcox (Oracle) 	folio = __filemap_get_folio(bd_mapping, index, FGP_ACCESSED, 0);
204eee25182SMatthew Wilcox (Oracle) 	if (IS_ERR(folio))
2051da177e4SLinus Torvalds 		goto out;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	spin_lock(&bd_mapping->private_lock);
208eee25182SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
209eee25182SMatthew Wilcox (Oracle) 	if (!head)
2101da177e4SLinus Torvalds 		goto out_unlock;
2111da177e4SLinus Torvalds 	bh = head;
2121da177e4SLinus Torvalds 	do {
21397f76d3dSNikanth Karthikesan 		if (!buffer_mapped(bh))
21497f76d3dSNikanth Karthikesan 			all_mapped = 0;
21597f76d3dSNikanth Karthikesan 		else if (bh->b_blocknr == block) {
2161da177e4SLinus Torvalds 			ret = bh;
2171da177e4SLinus Torvalds 			get_bh(bh);
2181da177e4SLinus Torvalds 			goto out_unlock;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 		bh = bh->b_this_page;
2211da177e4SLinus Torvalds 	} while (bh != head);
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	/* we might be here because some of the buffers on this page are
2241da177e4SLinus Torvalds 	 * not mapped.  This is due to various races between
2251da177e4SLinus Torvalds 	 * file io on the block device and getblk.  It gets dealt with
2261da177e4SLinus Torvalds 	 * elsewhere, don't buffer_error if we had some unmapped buffers
2271da177e4SLinus Torvalds 	 */
22843636c80STetsuo Handa 	ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
22943636c80STetsuo Handa 	if (all_mapped && __ratelimit(&last_warned)) {
23043636c80STetsuo Handa 		printk("__find_get_block_slow() failed. block=%llu, "
23143636c80STetsuo Handa 		       "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
23243636c80STetsuo Handa 		       "device %pg blocksize: %d\n",
233205f87f6SBadari Pulavarty 		       (unsigned long long)block,
23443636c80STetsuo Handa 		       (unsigned long long)bh->b_blocknr,
23543636c80STetsuo Handa 		       bh->b_state, bh->b_size, bdev,
23672a2ebd8STao Ma 		       1 << bd_inode->i_blkbits);
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds out_unlock:
2391da177e4SLinus Torvalds 	spin_unlock(&bd_mapping->private_lock);
240eee25182SMatthew Wilcox (Oracle) 	folio_put(folio);
2411da177e4SLinus Torvalds out:
2421da177e4SLinus Torvalds 	return ret;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	unsigned long flags;
248a3972203SNick Piggin 	struct buffer_head *first;
2491da177e4SLinus Torvalds 	struct buffer_head *tmp;
2502e2dba15SMatthew Wilcox (Oracle) 	struct folio *folio;
2512e2dba15SMatthew Wilcox (Oracle) 	int folio_uptodate = 1;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	BUG_ON(!buffer_async_read(bh));
2541da177e4SLinus Torvalds 
2552e2dba15SMatthew Wilcox (Oracle) 	folio = bh->b_folio;
2561da177e4SLinus Torvalds 	if (uptodate) {
2571da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
2581da177e4SLinus Torvalds 	} else {
2591da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
260b744c2acSRobert Elliott 		buffer_io_error(bh, ", async page read");
2612e2dba15SMatthew Wilcox (Oracle) 		folio_set_error(folio);
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/*
2651da177e4SLinus Torvalds 	 * Be _very_ careful from here on. Bad things can happen if
2661da177e4SLinus Torvalds 	 * two buffer heads end IO at almost the same time and both
2671da177e4SLinus Torvalds 	 * decide that the page is now completely done.
2681da177e4SLinus Torvalds 	 */
2692e2dba15SMatthew Wilcox (Oracle) 	first = folio_buffers(folio);
270f1e67e35SThomas Gleixner 	spin_lock_irqsave(&first->b_uptodate_lock, flags);
2711da177e4SLinus Torvalds 	clear_buffer_async_read(bh);
2721da177e4SLinus Torvalds 	unlock_buffer(bh);
2731da177e4SLinus Torvalds 	tmp = bh;
2741da177e4SLinus Torvalds 	do {
2751da177e4SLinus Torvalds 		if (!buffer_uptodate(tmp))
2762e2dba15SMatthew Wilcox (Oracle) 			folio_uptodate = 0;
2771da177e4SLinus Torvalds 		if (buffer_async_read(tmp)) {
2781da177e4SLinus Torvalds 			BUG_ON(!buffer_locked(tmp));
2791da177e4SLinus Torvalds 			goto still_busy;
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 		tmp = tmp->b_this_page;
2821da177e4SLinus Torvalds 	} while (tmp != bh);
283f1e67e35SThomas Gleixner 	spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
2841da177e4SLinus Torvalds 
2856ba924d3SMatthew Wilcox (Oracle) 	folio_end_read(folio, folio_uptodate);
2861da177e4SLinus Torvalds 	return;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds still_busy:
289f1e67e35SThomas Gleixner 	spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
2901da177e4SLinus Torvalds 	return;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2934fa512ceSEric Biggers struct postprocess_bh_ctx {
29431fb992cSEric Biggers 	struct work_struct work;
29531fb992cSEric Biggers 	struct buffer_head *bh;
29631fb992cSEric Biggers };
29731fb992cSEric Biggers 
2984fa512ceSEric Biggers static void verify_bh(struct work_struct *work)
2994fa512ceSEric Biggers {
3004fa512ceSEric Biggers 	struct postprocess_bh_ctx *ctx =
3014fa512ceSEric Biggers 		container_of(work, struct postprocess_bh_ctx, work);
3024fa512ceSEric Biggers 	struct buffer_head *bh = ctx->bh;
3034fa512ceSEric Biggers 	bool valid;
3044fa512ceSEric Biggers 
3058b7d3fe9SEric Biggers 	valid = fsverity_verify_blocks(bh->b_folio, bh->b_size, bh_offset(bh));
3064fa512ceSEric Biggers 	end_buffer_async_read(bh, valid);
3074fa512ceSEric Biggers 	kfree(ctx);
3084fa512ceSEric Biggers }
3094fa512ceSEric Biggers 
3104fa512ceSEric Biggers static bool need_fsverity(struct buffer_head *bh)
3114fa512ceSEric Biggers {
3128b7d3fe9SEric Biggers 	struct folio *folio = bh->b_folio;
3138b7d3fe9SEric Biggers 	struct inode *inode = folio->mapping->host;
3144fa512ceSEric Biggers 
3154fa512ceSEric Biggers 	return fsverity_active(inode) &&
3164fa512ceSEric Biggers 		/* needed by ext4 */
3178b7d3fe9SEric Biggers 		folio->index < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
3184fa512ceSEric Biggers }
3194fa512ceSEric Biggers 
32031fb992cSEric Biggers static void decrypt_bh(struct work_struct *work)
32131fb992cSEric Biggers {
3224fa512ceSEric Biggers 	struct postprocess_bh_ctx *ctx =
3234fa512ceSEric Biggers 		container_of(work, struct postprocess_bh_ctx, work);
32431fb992cSEric Biggers 	struct buffer_head *bh = ctx->bh;
32531fb992cSEric Biggers 	int err;
32631fb992cSEric Biggers 
3279c7fb7f7SEric Biggers 	err = fscrypt_decrypt_pagecache_blocks(bh->b_folio, bh->b_size,
3289c7fb7f7SEric Biggers 					       bh_offset(bh));
3294fa512ceSEric Biggers 	if (err == 0 && need_fsverity(bh)) {
3304fa512ceSEric Biggers 		/*
3314fa512ceSEric Biggers 		 * We use different work queues for decryption and for verity
3324fa512ceSEric Biggers 		 * because verity may require reading metadata pages that need
3334fa512ceSEric Biggers 		 * decryption, and we shouldn't recurse to the same workqueue.
3344fa512ceSEric Biggers 		 */
3354fa512ceSEric Biggers 		INIT_WORK(&ctx->work, verify_bh);
3364fa512ceSEric Biggers 		fsverity_enqueue_verify_work(&ctx->work);
3374fa512ceSEric Biggers 		return;
3384fa512ceSEric Biggers 	}
33931fb992cSEric Biggers 	end_buffer_async_read(bh, err == 0);
34031fb992cSEric Biggers 	kfree(ctx);
34131fb992cSEric Biggers }
34231fb992cSEric Biggers 
34331fb992cSEric Biggers /*
3442c69e205SMatthew Wilcox (Oracle)  * I/O completion handler for block_read_full_folio() - pages
34531fb992cSEric Biggers  * which come unlocked at the end of I/O.
34631fb992cSEric Biggers  */
34731fb992cSEric Biggers static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate)
34831fb992cSEric Biggers {
3493822a7c4SLinus Torvalds 	struct inode *inode = bh->b_folio->mapping->host;
3504fa512ceSEric Biggers 	bool decrypt = fscrypt_inode_uses_fs_layer_crypto(inode);
3514fa512ceSEric Biggers 	bool verify = need_fsverity(bh);
3524fa512ceSEric Biggers 
3534fa512ceSEric Biggers 	/* Decrypt (with fscrypt) and/or verify (with fsverity) if needed. */
3544fa512ceSEric Biggers 	if (uptodate && (decrypt || verify)) {
3554fa512ceSEric Biggers 		struct postprocess_bh_ctx *ctx =
3564fa512ceSEric Biggers 			kmalloc(sizeof(*ctx), GFP_ATOMIC);
35731fb992cSEric Biggers 
35831fb992cSEric Biggers 		if (ctx) {
35931fb992cSEric Biggers 			ctx->bh = bh;
3604fa512ceSEric Biggers 			if (decrypt) {
3614fa512ceSEric Biggers 				INIT_WORK(&ctx->work, decrypt_bh);
36231fb992cSEric Biggers 				fscrypt_enqueue_decrypt_work(&ctx->work);
3634fa512ceSEric Biggers 			} else {
3644fa512ceSEric Biggers 				INIT_WORK(&ctx->work, verify_bh);
3654fa512ceSEric Biggers 				fsverity_enqueue_verify_work(&ctx->work);
3664fa512ceSEric Biggers 			}
36731fb992cSEric Biggers 			return;
36831fb992cSEric Biggers 		}
36931fb992cSEric Biggers 		uptodate = 0;
37031fb992cSEric Biggers 	}
37131fb992cSEric Biggers 	end_buffer_async_read(bh, uptodate);
37231fb992cSEric Biggers }
37331fb992cSEric Biggers 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  * Completion handler for block_write_full_page() - pages which are unlocked
3761da177e4SLinus Torvalds  * during I/O, and which have PageWriteback cleared upon I/O completion.
3771da177e4SLinus Torvalds  */
37835c80d5fSChris Mason void end_buffer_async_write(struct buffer_head *bh, int uptodate)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	unsigned long flags;
381a3972203SNick Piggin 	struct buffer_head *first;
3821da177e4SLinus Torvalds 	struct buffer_head *tmp;
383743ed81eSMatthew Wilcox (Oracle) 	struct folio *folio;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	BUG_ON(!buffer_async_write(bh));
3861da177e4SLinus Torvalds 
387743ed81eSMatthew Wilcox (Oracle) 	folio = bh->b_folio;
3881da177e4SLinus Torvalds 	if (uptodate) {
3891da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
3901da177e4SLinus Torvalds 	} else {
391b744c2acSRobert Elliott 		buffer_io_error(bh, ", lost async page write");
39287354e5dSJeff Layton 		mark_buffer_write_io_error(bh);
3931da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
394743ed81eSMatthew Wilcox (Oracle) 		folio_set_error(folio);
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
397743ed81eSMatthew Wilcox (Oracle) 	first = folio_buffers(folio);
398f1e67e35SThomas Gleixner 	spin_lock_irqsave(&first->b_uptodate_lock, flags);
399a3972203SNick Piggin 
4001da177e4SLinus Torvalds 	clear_buffer_async_write(bh);
4011da177e4SLinus Torvalds 	unlock_buffer(bh);
4021da177e4SLinus Torvalds 	tmp = bh->b_this_page;
4031da177e4SLinus Torvalds 	while (tmp != bh) {
4041da177e4SLinus Torvalds 		if (buffer_async_write(tmp)) {
4051da177e4SLinus Torvalds 			BUG_ON(!buffer_locked(tmp));
4061da177e4SLinus Torvalds 			goto still_busy;
4071da177e4SLinus Torvalds 		}
4081da177e4SLinus Torvalds 		tmp = tmp->b_this_page;
4091da177e4SLinus Torvalds 	}
410f1e67e35SThomas Gleixner 	spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
411743ed81eSMatthew Wilcox (Oracle) 	folio_end_writeback(folio);
4121da177e4SLinus Torvalds 	return;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds still_busy:
415f1e67e35SThomas Gleixner 	spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
4161da177e4SLinus Torvalds 	return;
4171da177e4SLinus Torvalds }
4181fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_async_write);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds /*
4211da177e4SLinus Torvalds  * If a page's buffers are under async readin (end_buffer_async_read
4221da177e4SLinus Torvalds  * completion) then there is a possibility that another thread of
4231da177e4SLinus Torvalds  * control could lock one of the buffers after it has completed
4241da177e4SLinus Torvalds  * but while some of the other buffers have not completed.  This
4251da177e4SLinus Torvalds  * locked buffer would confuse end_buffer_async_read() into not unlocking
4261da177e4SLinus Torvalds  * the page.  So the absence of BH_Async_Read tells end_buffer_async_read()
4271da177e4SLinus Torvalds  * that this buffer is not under async I/O.
4281da177e4SLinus Torvalds  *
4291da177e4SLinus Torvalds  * The page comes unlocked when it has no locked buffer_async buffers
4301da177e4SLinus Torvalds  * left.
4311da177e4SLinus Torvalds  *
4321da177e4SLinus Torvalds  * PageLocked prevents anyone starting new async I/O reads any of
4331da177e4SLinus Torvalds  * the buffers.
4341da177e4SLinus Torvalds  *
4351da177e4SLinus Torvalds  * PageWriteback is used to prevent simultaneous writeout of the same
4361da177e4SLinus Torvalds  * page.
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  * PageLocked prevents anyone from starting writeback of a page which is
4391da177e4SLinus Torvalds  * under read I/O (PageWriteback is only ever set against a locked page).
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds static void mark_buffer_async_read(struct buffer_head *bh)
4421da177e4SLinus Torvalds {
44331fb992cSEric Biggers 	bh->b_end_io = end_buffer_async_read_io;
4441da177e4SLinus Torvalds 	set_buffer_async_read(bh);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471fe72eaaSH Hartley Sweeten static void mark_buffer_async_write_endio(struct buffer_head *bh,
44835c80d5fSChris Mason 					  bh_end_io_t *handler)
44935c80d5fSChris Mason {
45035c80d5fSChris Mason 	bh->b_end_io = handler;
45135c80d5fSChris Mason 	set_buffer_async_write(bh);
45235c80d5fSChris Mason }
45335c80d5fSChris Mason 
4541da177e4SLinus Torvalds void mark_buffer_async_write(struct buffer_head *bh)
4551da177e4SLinus Torvalds {
45635c80d5fSChris Mason 	mark_buffer_async_write_endio(bh, end_buffer_async_write);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds EXPORT_SYMBOL(mark_buffer_async_write);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /*
4621da177e4SLinus Torvalds  * fs/buffer.c contains helper functions for buffer-backed address space's
4631da177e4SLinus Torvalds  * fsync functions.  A common requirement for buffer-based filesystems is
4641da177e4SLinus Torvalds  * that certain data from the backing blockdev needs to be written out for
4651da177e4SLinus Torvalds  * a successful fsync().  For example, ext2 indirect blocks need to be
4661da177e4SLinus Torvalds  * written back and waited upon before fsync() returns.
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
4691da177e4SLinus Torvalds  * inode_has_buffers() and invalidate_inode_buffers() are provided for the
4701da177e4SLinus Torvalds  * management of a list of dependent buffers at ->i_mapping->private_list.
4711da177e4SLinus Torvalds  *
4721da177e4SLinus Torvalds  * Locking is a little subtle: try_to_free_buffers() will remove buffers
4731da177e4SLinus Torvalds  * from their controlling inode's queue when they are being freed.  But
4741da177e4SLinus Torvalds  * try_to_free_buffers() will be operating against the *blockdev* mapping
4751da177e4SLinus Torvalds  * at the time, not against the S_ISREG file which depends on those buffers.
4761da177e4SLinus Torvalds  * So the locking for private_list is via the private_lock in the address_space
4771da177e4SLinus Torvalds  * which backs the buffers.  Which is different from the address_space
4781da177e4SLinus Torvalds  * against which the buffers are listed.  So for a particular address_space,
4791da177e4SLinus Torvalds  * mapping->private_lock does *not* protect mapping->private_list!  In fact,
4801da177e4SLinus Torvalds  * mapping->private_list will always be protected by the backing blockdev's
4811da177e4SLinus Torvalds  * ->private_lock.
4821da177e4SLinus Torvalds  *
4831da177e4SLinus Torvalds  * Which introduces a requirement: all buffers on an address_space's
4841da177e4SLinus Torvalds  * ->private_list must be from the same address_space: the blockdev's.
4851da177e4SLinus Torvalds  *
4861da177e4SLinus Torvalds  * address_spaces which do not place buffers at ->private_list via these
4871da177e4SLinus Torvalds  * utility functions are free to use private_lock and private_list for
4881da177e4SLinus Torvalds  * whatever they want.  The only requirement is that list_empty(private_list)
4891da177e4SLinus Torvalds  * be true at clear_inode() time.
4901da177e4SLinus Torvalds  *
4911da177e4SLinus Torvalds  * FIXME: clear_inode should not call invalidate_inode_buffers().  The
4921da177e4SLinus Torvalds  * filesystems should do that.  invalidate_inode_buffers() should just go
4931da177e4SLinus Torvalds  * BUG_ON(!list_empty).
4941da177e4SLinus Torvalds  *
4951da177e4SLinus Torvalds  * FIXME: mark_buffer_dirty_inode() is a data-plane operation.  It should
4961da177e4SLinus Torvalds  * take an address_space, not an inode.  And it should be called
4971da177e4SLinus Torvalds  * mark_buffer_dirty_fsync() to clearly define why those buffers are being
4981da177e4SLinus Torvalds  * queued up.
4991da177e4SLinus Torvalds  *
5001da177e4SLinus Torvalds  * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
5011da177e4SLinus Torvalds  * list if it is already on a list.  Because if the buffer is on a list,
5021da177e4SLinus Torvalds  * it *must* already be on the right one.  If not, the filesystem is being
5031da177e4SLinus Torvalds  * silly.  This will save a ton of locking.  But first we have to ensure
5041da177e4SLinus Torvalds  * that buffers are taken *off* the old inode's list when they are freed
5051da177e4SLinus Torvalds  * (presumably in truncate).  That requires careful auditing of all
5061da177e4SLinus Torvalds  * filesystems (do it inside bforget()).  It could also be done by bringing
5071da177e4SLinus Torvalds  * b_inode back.
5081da177e4SLinus Torvalds  */
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /*
5111da177e4SLinus Torvalds  * The buffer's backing address_space's private_lock must be held
5121da177e4SLinus Torvalds  */
513dbacefc9SThomas Petazzoni static void __remove_assoc_queue(struct buffer_head *bh)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	list_del_init(&bh->b_assoc_buffers);
51658ff407bSJan Kara 	WARN_ON(!bh->b_assoc_map);
51758ff407bSJan Kara 	bh->b_assoc_map = NULL;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds int inode_has_buffers(struct inode *inode)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	return !list_empty(&inode->i_data.private_list);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /*
5261da177e4SLinus Torvalds  * osync is designed to support O_SYNC io.  It waits synchronously for
5271da177e4SLinus Torvalds  * all already-submitted IO to complete, but does not queue any new
5281da177e4SLinus Torvalds  * writes to the disk.
5291da177e4SLinus Torvalds  *
53079f59784SZhang Yi  * To do O_SYNC writes, just queue the buffer writes with write_dirty_buffer
53179f59784SZhang Yi  * as you dirty the buffers, and then use osync_inode_buffers to wait for
5321da177e4SLinus Torvalds  * completion.  Any other dirty buffers which are not yet queued for
5331da177e4SLinus Torvalds  * write will not be flushed to disk by the osync.
5341da177e4SLinus Torvalds  */
5351da177e4SLinus Torvalds static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct buffer_head *bh;
5381da177e4SLinus Torvalds 	struct list_head *p;
5391da177e4SLinus Torvalds 	int err = 0;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	spin_lock(lock);
5421da177e4SLinus Torvalds repeat:
5431da177e4SLinus Torvalds 	list_for_each_prev(p, list) {
5441da177e4SLinus Torvalds 		bh = BH_ENTRY(p);
5451da177e4SLinus Torvalds 		if (buffer_locked(bh)) {
5461da177e4SLinus Torvalds 			get_bh(bh);
5471da177e4SLinus Torvalds 			spin_unlock(lock);
5481da177e4SLinus Torvalds 			wait_on_buffer(bh);
5491da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
5501da177e4SLinus Torvalds 				err = -EIO;
5511da177e4SLinus Torvalds 			brelse(bh);
5521da177e4SLinus Torvalds 			spin_lock(lock);
5531da177e4SLinus Torvalds 			goto repeat;
5541da177e4SLinus Torvalds 		}
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 	spin_unlock(lock);
5571da177e4SLinus Torvalds 	return err;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds /**
56178a4a50aSRandy Dunlap  * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
56267be2dd1SMartin Waitz  * @mapping: the mapping which wants those buffers written
5631da177e4SLinus Torvalds  *
5641da177e4SLinus Torvalds  * Starts I/O against the buffers at mapping->private_list, and waits upon
5651da177e4SLinus Torvalds  * that I/O.
5661da177e4SLinus Torvalds  *
56767be2dd1SMartin Waitz  * Basically, this is a convenience function for fsync().
56867be2dd1SMartin Waitz  * @mapping is a file or directory which needs those buffers to be written for
56967be2dd1SMartin Waitz  * a successful fsync().
5701da177e4SLinus Torvalds  */
5711da177e4SLinus Torvalds int sync_mapping_buffers(struct address_space *mapping)
5721da177e4SLinus Torvalds {
573252aa6f5SRafael Aquini 	struct address_space *buffer_mapping = mapping->private_data;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (buffer_mapping == NULL || list_empty(&mapping->private_list))
5761da177e4SLinus Torvalds 		return 0;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	return fsync_buffers_list(&buffer_mapping->private_lock,
5791da177e4SLinus Torvalds 					&mapping->private_list);
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds EXPORT_SYMBOL(sync_mapping_buffers);
5821da177e4SLinus Torvalds 
58331b2ebc0SRitesh Harjani (IBM) /**
58431b2ebc0SRitesh Harjani (IBM)  * generic_buffers_fsync_noflush - generic buffer fsync implementation
58531b2ebc0SRitesh Harjani (IBM)  * for simple filesystems with no inode lock
58631b2ebc0SRitesh Harjani (IBM)  *
58731b2ebc0SRitesh Harjani (IBM)  * @file:	file to synchronize
58831b2ebc0SRitesh Harjani (IBM)  * @start:	start offset in bytes
58931b2ebc0SRitesh Harjani (IBM)  * @end:	end offset in bytes (inclusive)
59031b2ebc0SRitesh Harjani (IBM)  * @datasync:	only synchronize essential metadata if true
59131b2ebc0SRitesh Harjani (IBM)  *
59231b2ebc0SRitesh Harjani (IBM)  * This is a generic implementation of the fsync method for simple
59331b2ebc0SRitesh Harjani (IBM)  * filesystems which track all non-inode metadata in the buffers list
59431b2ebc0SRitesh Harjani (IBM)  * hanging off the address_space structure.
59531b2ebc0SRitesh Harjani (IBM)  */
59631b2ebc0SRitesh Harjani (IBM) int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
59731b2ebc0SRitesh Harjani (IBM) 				  bool datasync)
59831b2ebc0SRitesh Harjani (IBM) {
59931b2ebc0SRitesh Harjani (IBM) 	struct inode *inode = file->f_mapping->host;
60031b2ebc0SRitesh Harjani (IBM) 	int err;
60131b2ebc0SRitesh Harjani (IBM) 	int ret;
60231b2ebc0SRitesh Harjani (IBM) 
60331b2ebc0SRitesh Harjani (IBM) 	err = file_write_and_wait_range(file, start, end);
60431b2ebc0SRitesh Harjani (IBM) 	if (err)
60531b2ebc0SRitesh Harjani (IBM) 		return err;
60631b2ebc0SRitesh Harjani (IBM) 
60731b2ebc0SRitesh Harjani (IBM) 	ret = sync_mapping_buffers(inode->i_mapping);
60831b2ebc0SRitesh Harjani (IBM) 	if (!(inode->i_state & I_DIRTY_ALL))
60931b2ebc0SRitesh Harjani (IBM) 		goto out;
61031b2ebc0SRitesh Harjani (IBM) 	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
61131b2ebc0SRitesh Harjani (IBM) 		goto out;
61231b2ebc0SRitesh Harjani (IBM) 
61331b2ebc0SRitesh Harjani (IBM) 	err = sync_inode_metadata(inode, 1);
61431b2ebc0SRitesh Harjani (IBM) 	if (ret == 0)
61531b2ebc0SRitesh Harjani (IBM) 		ret = err;
61631b2ebc0SRitesh Harjani (IBM) 
61731b2ebc0SRitesh Harjani (IBM) out:
61831b2ebc0SRitesh Harjani (IBM) 	/* check and advance again to catch errors after syncing out buffers */
61931b2ebc0SRitesh Harjani (IBM) 	err = file_check_and_advance_wb_err(file);
62031b2ebc0SRitesh Harjani (IBM) 	if (ret == 0)
62131b2ebc0SRitesh Harjani (IBM) 		ret = err;
62231b2ebc0SRitesh Harjani (IBM) 	return ret;
62331b2ebc0SRitesh Harjani (IBM) }
62431b2ebc0SRitesh Harjani (IBM) EXPORT_SYMBOL(generic_buffers_fsync_noflush);
62531b2ebc0SRitesh Harjani (IBM) 
62631b2ebc0SRitesh Harjani (IBM) /**
62731b2ebc0SRitesh Harjani (IBM)  * generic_buffers_fsync - generic buffer fsync implementation
62831b2ebc0SRitesh Harjani (IBM)  * for simple filesystems with no inode lock
62931b2ebc0SRitesh Harjani (IBM)  *
63031b2ebc0SRitesh Harjani (IBM)  * @file:	file to synchronize
63131b2ebc0SRitesh Harjani (IBM)  * @start:	start offset in bytes
63231b2ebc0SRitesh Harjani (IBM)  * @end:	end offset in bytes (inclusive)
63331b2ebc0SRitesh Harjani (IBM)  * @datasync:	only synchronize essential metadata if true
63431b2ebc0SRitesh Harjani (IBM)  *
63531b2ebc0SRitesh Harjani (IBM)  * This is a generic implementation of the fsync method for simple
63631b2ebc0SRitesh Harjani (IBM)  * filesystems which track all non-inode metadata in the buffers list
63731b2ebc0SRitesh Harjani (IBM)  * hanging off the address_space structure. This also makes sure that
63831b2ebc0SRitesh Harjani (IBM)  * a device cache flush operation is called at the end.
63931b2ebc0SRitesh Harjani (IBM)  */
64031b2ebc0SRitesh Harjani (IBM) int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
64131b2ebc0SRitesh Harjani (IBM) 			  bool datasync)
64231b2ebc0SRitesh Harjani (IBM) {
64331b2ebc0SRitesh Harjani (IBM) 	struct inode *inode = file->f_mapping->host;
64431b2ebc0SRitesh Harjani (IBM) 	int ret;
64531b2ebc0SRitesh Harjani (IBM) 
64631b2ebc0SRitesh Harjani (IBM) 	ret = generic_buffers_fsync_noflush(file, start, end, datasync);
64731b2ebc0SRitesh Harjani (IBM) 	if (!ret)
64831b2ebc0SRitesh Harjani (IBM) 		ret = blkdev_issue_flush(inode->i_sb->s_bdev);
64931b2ebc0SRitesh Harjani (IBM) 	return ret;
65031b2ebc0SRitesh Harjani (IBM) }
65131b2ebc0SRitesh Harjani (IBM) EXPORT_SYMBOL(generic_buffers_fsync);
65231b2ebc0SRitesh Harjani (IBM) 
6531da177e4SLinus Torvalds /*
6541da177e4SLinus Torvalds  * Called when we've recently written block `bblock', and it is known that
6551da177e4SLinus Torvalds  * `bblock' was for a buffer_boundary() buffer.  This means that the block at
6561da177e4SLinus Torvalds  * `bblock + 1' is probably a dirty indirect block.  Hunt it down and, if it's
6571da177e4SLinus Torvalds  * dirty, schedule it for IO.  So that indirects merge nicely with their data.
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds void write_boundary_block(struct block_device *bdev,
6601da177e4SLinus Torvalds 			sector_t bblock, unsigned blocksize)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
6631da177e4SLinus Torvalds 	if (bh) {
6641da177e4SLinus Torvalds 		if (buffer_dirty(bh))
665e7ea1129SZhang Yi 			write_dirty_buffer(bh, 0);
6661da177e4SLinus Torvalds 		put_bh(bh);
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
673abc8a8a2SMatthew Wilcox (Oracle) 	struct address_space *buffer_mapping = bh->b_folio->mapping;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
676252aa6f5SRafael Aquini 	if (!mapping->private_data) {
677252aa6f5SRafael Aquini 		mapping->private_data = buffer_mapping;
6781da177e4SLinus Torvalds 	} else {
679252aa6f5SRafael Aquini 		BUG_ON(mapping->private_data != buffer_mapping);
6801da177e4SLinus Torvalds 	}
681535ee2fbSJan Kara 	if (!bh->b_assoc_map) {
6821da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
6831da177e4SLinus Torvalds 		list_move_tail(&bh->b_assoc_buffers,
6841da177e4SLinus Torvalds 				&mapping->private_list);
68558ff407bSJan Kara 		bh->b_assoc_map = mapping;
6861da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds EXPORT_SYMBOL(mark_buffer_dirty_inode);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds /*
6921da177e4SLinus Torvalds  * Add a page to the dirty page list.
6931da177e4SLinus Torvalds  *
6941da177e4SLinus Torvalds  * It is a sad fact of life that this function is called from several places
6951da177e4SLinus Torvalds  * deeply under spinlocking.  It may not sleep.
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  * If the page has buffers, the uptodate buffers are set dirty, to preserve
6981da177e4SLinus Torvalds  * dirty-state coherency between the page and the buffers.  It the page does
6991da177e4SLinus Torvalds  * not have buffers then when they are later attached they will all be set
7001da177e4SLinus Torvalds  * dirty.
7011da177e4SLinus Torvalds  *
7021da177e4SLinus Torvalds  * The buffers are dirtied before the page is dirtied.  There's a small race
7031da177e4SLinus Torvalds  * window in which a writepage caller may see the page cleanness but not the
7041da177e4SLinus Torvalds  * buffer dirtiness.  That's fine.  If this code were to set the page dirty
7051da177e4SLinus Torvalds  * before the buffers, a concurrent writepage caller could clear the page dirty
7061da177e4SLinus Torvalds  * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
7071da177e4SLinus Torvalds  * page on the dirty page list.
7081da177e4SLinus Torvalds  *
7091da177e4SLinus Torvalds  * We use private_lock to lock against try_to_free_buffers while using the
7101da177e4SLinus Torvalds  * page's buffer list.  Also use this to protect against clean buffers being
7111da177e4SLinus Torvalds  * added to the page after it was set dirty.
7121da177e4SLinus Torvalds  *
7131da177e4SLinus Torvalds  * FIXME: may need to call ->reservepage here as well.  That's rather up to the
7141da177e4SLinus Torvalds  * address_space though.
7151da177e4SLinus Torvalds  */
716e621900aSMatthew Wilcox (Oracle) bool block_dirty_folio(struct address_space *mapping, struct folio *folio)
7171da177e4SLinus Torvalds {
718e621900aSMatthew Wilcox (Oracle) 	struct buffer_head *head;
719e621900aSMatthew Wilcox (Oracle) 	bool newly_dirty;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	spin_lock(&mapping->private_lock);
722e621900aSMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
723e621900aSMatthew Wilcox (Oracle) 	if (head) {
7241da177e4SLinus Torvalds 		struct buffer_head *bh = head;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		do {
7271da177e4SLinus Torvalds 			set_buffer_dirty(bh);
7281da177e4SLinus Torvalds 			bh = bh->b_this_page;
7291da177e4SLinus Torvalds 		} while (bh != head);
7301da177e4SLinus Torvalds 	}
731c4843a75SGreg Thelen 	/*
732bcfe06bfSRoman Gushchin 	 * Lock out page's memcg migration to keep PageDirty
73381f8c3a4SJohannes Weiner 	 * synchronized with per-memcg dirty page counters.
734c4843a75SGreg Thelen 	 */
735e621900aSMatthew Wilcox (Oracle) 	folio_memcg_lock(folio);
736e621900aSMatthew Wilcox (Oracle) 	newly_dirty = !folio_test_set_dirty(folio);
7371da177e4SLinus Torvalds 	spin_unlock(&mapping->private_lock);
7381da177e4SLinus Torvalds 
739a8e7d49aSLinus Torvalds 	if (newly_dirty)
740e621900aSMatthew Wilcox (Oracle) 		__folio_mark_dirty(folio, mapping, 1);
741c4843a75SGreg Thelen 
742e621900aSMatthew Wilcox (Oracle) 	folio_memcg_unlock(folio);
743c4843a75SGreg Thelen 
744c4843a75SGreg Thelen 	if (newly_dirty)
745c4843a75SGreg Thelen 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
746c4843a75SGreg Thelen 
747a8e7d49aSLinus Torvalds 	return newly_dirty;
7481da177e4SLinus Torvalds }
749e621900aSMatthew Wilcox (Oracle) EXPORT_SYMBOL(block_dirty_folio);
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds /*
7521da177e4SLinus Torvalds  * Write out and wait upon a list of buffers.
7531da177e4SLinus Torvalds  *
7541da177e4SLinus Torvalds  * We have conflicting pressures: we want to make sure that all
7551da177e4SLinus Torvalds  * initially dirty buffers get waited on, but that any subsequently
7561da177e4SLinus Torvalds  * dirtied buffers don't.  After all, we don't want fsync to last
7571da177e4SLinus Torvalds  * forever if somebody is actively writing to the file.
7581da177e4SLinus Torvalds  *
7591da177e4SLinus Torvalds  * Do this in two main stages: first we copy dirty buffers to a
7601da177e4SLinus Torvalds  * temporary inode list, queueing the writes as we go.  Then we clean
7611da177e4SLinus Torvalds  * up, waiting for those writes to complete.
7621da177e4SLinus Torvalds  *
7631da177e4SLinus Torvalds  * During this second stage, any subsequent updates to the file may end
7641da177e4SLinus Torvalds  * up refiling the buffer on the original inode's dirty list again, so
7651da177e4SLinus Torvalds  * there is a chance we will end up with a buffer queued for write but
7661da177e4SLinus Torvalds  * not yet completed on that list.  So, as a final cleanup we go through
7671da177e4SLinus Torvalds  * the osync code to catch these locked, dirty buffers without requeuing
7681da177e4SLinus Torvalds  * any newly dirty buffers for write.
7691da177e4SLinus Torvalds  */
7701da177e4SLinus Torvalds static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	struct buffer_head *bh;
7731da177e4SLinus Torvalds 	struct list_head tmp;
7747eaceaccSJens Axboe 	struct address_space *mapping;
7751da177e4SLinus Torvalds 	int err = 0, err2;
7764ee2491eSJens Axboe 	struct blk_plug plug;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp);
7794ee2491eSJens Axboe 	blk_start_plug(&plug);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	spin_lock(lock);
7821da177e4SLinus Torvalds 	while (!list_empty(list)) {
7831da177e4SLinus Torvalds 		bh = BH_ENTRY(list->next);
784535ee2fbSJan Kara 		mapping = bh->b_assoc_map;
78558ff407bSJan Kara 		__remove_assoc_queue(bh);
786535ee2fbSJan Kara 		/* Avoid race with mark_buffer_dirty_inode() which does
787535ee2fbSJan Kara 		 * a lockless check and we rely on seeing the dirty bit */
788535ee2fbSJan Kara 		smp_mb();
7891da177e4SLinus Torvalds 		if (buffer_dirty(bh) || buffer_locked(bh)) {
7901da177e4SLinus Torvalds 			list_add(&bh->b_assoc_buffers, &tmp);
791535ee2fbSJan Kara 			bh->b_assoc_map = mapping;
7921da177e4SLinus Torvalds 			if (buffer_dirty(bh)) {
7931da177e4SLinus Torvalds 				get_bh(bh);
7941da177e4SLinus Torvalds 				spin_unlock(lock);
7951da177e4SLinus Torvalds 				/*
7961da177e4SLinus Torvalds 				 * Ensure any pending I/O completes so that
7979cb569d6SChristoph Hellwig 				 * write_dirty_buffer() actually writes the
7989cb569d6SChristoph Hellwig 				 * current contents - it is a noop if I/O is
7999cb569d6SChristoph Hellwig 				 * still in flight on potentially older
8009cb569d6SChristoph Hellwig 				 * contents.
8011da177e4SLinus Torvalds 				 */
80270fd7614SChristoph Hellwig 				write_dirty_buffer(bh, REQ_SYNC);
8039cf6b720SJens Axboe 
8049cf6b720SJens Axboe 				/*
8059cf6b720SJens Axboe 				 * Kick off IO for the previous mapping. Note
8069cf6b720SJens Axboe 				 * that we will not run the very last mapping,
8079cf6b720SJens Axboe 				 * wait_on_buffer() will do that for us
8089cf6b720SJens Axboe 				 * through sync_buffer().
8099cf6b720SJens Axboe 				 */
8101da177e4SLinus Torvalds 				brelse(bh);
8111da177e4SLinus Torvalds 				spin_lock(lock);
8121da177e4SLinus Torvalds 			}
8131da177e4SLinus Torvalds 		}
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 
8164ee2491eSJens Axboe 	spin_unlock(lock);
8174ee2491eSJens Axboe 	blk_finish_plug(&plug);
8184ee2491eSJens Axboe 	spin_lock(lock);
8194ee2491eSJens Axboe 
8201da177e4SLinus Torvalds 	while (!list_empty(&tmp)) {
8211da177e4SLinus Torvalds 		bh = BH_ENTRY(tmp.prev);
8221da177e4SLinus Torvalds 		get_bh(bh);
823535ee2fbSJan Kara 		mapping = bh->b_assoc_map;
824535ee2fbSJan Kara 		__remove_assoc_queue(bh);
825535ee2fbSJan Kara 		/* Avoid race with mark_buffer_dirty_inode() which does
826535ee2fbSJan Kara 		 * a lockless check and we rely on seeing the dirty bit */
827535ee2fbSJan Kara 		smp_mb();
828535ee2fbSJan Kara 		if (buffer_dirty(bh)) {
829535ee2fbSJan Kara 			list_add(&bh->b_assoc_buffers,
830e3892296SJan Kara 				 &mapping->private_list);
831535ee2fbSJan Kara 			bh->b_assoc_map = mapping;
832535ee2fbSJan Kara 		}
8331da177e4SLinus Torvalds 		spin_unlock(lock);
8341da177e4SLinus Torvalds 		wait_on_buffer(bh);
8351da177e4SLinus Torvalds 		if (!buffer_uptodate(bh))
8361da177e4SLinus Torvalds 			err = -EIO;
8371da177e4SLinus Torvalds 		brelse(bh);
8381da177e4SLinus Torvalds 		spin_lock(lock);
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	spin_unlock(lock);
8421da177e4SLinus Torvalds 	err2 = osync_buffers_list(lock, list);
8431da177e4SLinus Torvalds 	if (err)
8441da177e4SLinus Torvalds 		return err;
8451da177e4SLinus Torvalds 	else
8461da177e4SLinus Torvalds 		return err2;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
8501da177e4SLinus Torvalds  * Invalidate any and all dirty buffers on a given inode.  We are
8511da177e4SLinus Torvalds  * probably unmounting the fs, but that doesn't mean we have already
8521da177e4SLinus Torvalds  * done a sync().  Just drop the buffers from the inode list.
8531da177e4SLinus Torvalds  *
8541da177e4SLinus Torvalds  * NOTE: we take the inode's blockdev's mapping's private_lock.  Which
8551da177e4SLinus Torvalds  * assumes that all the buffers are against the blockdev.  Not true
8561da177e4SLinus Torvalds  * for reiserfs.
8571da177e4SLinus Torvalds  */
8581da177e4SLinus Torvalds void invalidate_inode_buffers(struct inode *inode)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	if (inode_has_buffers(inode)) {
8611da177e4SLinus Torvalds 		struct address_space *mapping = &inode->i_data;
8621da177e4SLinus Torvalds 		struct list_head *list = &mapping->private_list;
863252aa6f5SRafael Aquini 		struct address_space *buffer_mapping = mapping->private_data;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
8661da177e4SLinus Torvalds 		while (!list_empty(list))
8671da177e4SLinus Torvalds 			__remove_assoc_queue(BH_ENTRY(list->next));
8681da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds }
87152b19ac9SJan Kara EXPORT_SYMBOL(invalidate_inode_buffers);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds /*
8741da177e4SLinus Torvalds  * Remove any clean buffers from the inode's buffer list.  This is called
8751da177e4SLinus Torvalds  * when we're trying to free the inode itself.  Those buffers can pin it.
8761da177e4SLinus Torvalds  *
8771da177e4SLinus Torvalds  * Returns true if all buffers were removed.
8781da177e4SLinus Torvalds  */
8791da177e4SLinus Torvalds int remove_inode_buffers(struct inode *inode)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	int ret = 1;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	if (inode_has_buffers(inode)) {
8841da177e4SLinus Torvalds 		struct address_space *mapping = &inode->i_data;
8851da177e4SLinus Torvalds 		struct list_head *list = &mapping->private_list;
886252aa6f5SRafael Aquini 		struct address_space *buffer_mapping = mapping->private_data;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
8891da177e4SLinus Torvalds 		while (!list_empty(list)) {
8901da177e4SLinus Torvalds 			struct buffer_head *bh = BH_ENTRY(list->next);
8911da177e4SLinus Torvalds 			if (buffer_dirty(bh)) {
8921da177e4SLinus Torvalds 				ret = 0;
8931da177e4SLinus Torvalds 				break;
8941da177e4SLinus Torvalds 			}
8951da177e4SLinus Torvalds 			__remove_assoc_queue(bh);
8961da177e4SLinus Torvalds 		}
8971da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 	return ret;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds /*
903c71124a8SPankaj Raghav  * Create the appropriate buffers when given a folio for data area and
9041da177e4SLinus Torvalds  * the size of each buffer.. Use the bh->b_this_page linked list to
9051da177e4SLinus Torvalds  * follow the buffers created.  Return NULL if unable to create more
9061da177e4SLinus Torvalds  * buffers.
9071da177e4SLinus Torvalds  *
9081da177e4SLinus Torvalds  * The retry flag is used to differentiate async IO (paging, swapping)
9091da177e4SLinus Torvalds  * which may not fail from ordinary buffer allocations.
9101da177e4SLinus Torvalds  */
911c71124a8SPankaj Raghav struct buffer_head *folio_alloc_buffers(struct folio *folio, unsigned long size,
9122a418157SMatthew Wilcox (Oracle) 					gfp_t gfp)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	struct buffer_head *bh, *head;
9151da177e4SLinus Torvalds 	long offset;
916b87d8cefSRoman Gushchin 	struct mem_cgroup *memcg, *old_memcg;
9171da177e4SLinus Torvalds 
918c71124a8SPankaj Raghav 	/* The folio lock pins the memcg */
919c71124a8SPankaj Raghav 	memcg = folio_memcg(folio);
920b87d8cefSRoman Gushchin 	old_memcg = set_active_memcg(memcg);
921f745c6f5SShakeel Butt 
9221da177e4SLinus Torvalds 	head = NULL;
923c71124a8SPankaj Raghav 	offset = folio_size(folio);
9241da177e4SLinus Torvalds 	while ((offset -= size) >= 0) {
925640ab98fSJens Axboe 		bh = alloc_buffer_head(gfp);
9261da177e4SLinus Torvalds 		if (!bh)
9271da177e4SLinus Torvalds 			goto no_grow;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 		bh->b_this_page = head;
9301da177e4SLinus Torvalds 		bh->b_blocknr = -1;
9311da177e4SLinus Torvalds 		head = bh;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 		bh->b_size = size;
9341da177e4SLinus Torvalds 
935c71124a8SPankaj Raghav 		/* Link the buffer to its folio */
936c71124a8SPankaj Raghav 		folio_set_bh(bh, folio, offset);
9371da177e4SLinus Torvalds 	}
938f745c6f5SShakeel Butt out:
939b87d8cefSRoman Gushchin 	set_active_memcg(old_memcg);
9401da177e4SLinus Torvalds 	return head;
9411da177e4SLinus Torvalds /*
9421da177e4SLinus Torvalds  * In case anything failed, we just free everything we got.
9431da177e4SLinus Torvalds  */
9441da177e4SLinus Torvalds no_grow:
9451da177e4SLinus Torvalds 	if (head) {
9461da177e4SLinus Torvalds 		do {
9471da177e4SLinus Torvalds 			bh = head;
9481da177e4SLinus Torvalds 			head = head->b_this_page;
9491da177e4SLinus Torvalds 			free_buffer_head(bh);
9501da177e4SLinus Torvalds 		} while (head);
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
953f745c6f5SShakeel Butt 	goto out;
9541da177e4SLinus Torvalds }
955c71124a8SPankaj Raghav EXPORT_SYMBOL_GPL(folio_alloc_buffers);
956c71124a8SPankaj Raghav 
957c71124a8SPankaj Raghav struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
958c71124a8SPankaj Raghav 				       bool retry)
959c71124a8SPankaj Raghav {
9602a418157SMatthew Wilcox (Oracle) 	gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
9612a418157SMatthew Wilcox (Oracle) 	if (retry)
9622a418157SMatthew Wilcox (Oracle) 		gfp |= __GFP_NOFAIL;
9632a418157SMatthew Wilcox (Oracle) 
9642a418157SMatthew Wilcox (Oracle) 	return folio_alloc_buffers(page_folio(page), size, gfp);
965c71124a8SPankaj Raghav }
9661da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(alloc_page_buffers);
9671da177e4SLinus Torvalds 
96808d84addSMatthew Wilcox (Oracle) static inline void link_dev_buffers(struct folio *folio,
96908d84addSMatthew Wilcox (Oracle) 		struct buffer_head *head)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	struct buffer_head *bh, *tail;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	bh = head;
9741da177e4SLinus Torvalds 	do {
9751da177e4SLinus Torvalds 		tail = bh;
9761da177e4SLinus Torvalds 		bh = bh->b_this_page;
9771da177e4SLinus Torvalds 	} while (bh);
9781da177e4SLinus Torvalds 	tail->b_this_page = head;
97908d84addSMatthew Wilcox (Oracle) 	folio_attach_private(folio, head);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
982bbec0270SLinus Torvalds static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
983bbec0270SLinus Torvalds {
984bbec0270SLinus Torvalds 	sector_t retval = ~((sector_t)0);
985b86058f9SChristoph Hellwig 	loff_t sz = bdev_nr_bytes(bdev);
986bbec0270SLinus Torvalds 
987bbec0270SLinus Torvalds 	if (sz) {
988bbec0270SLinus Torvalds 		unsigned int sizebits = blksize_bits(size);
989bbec0270SLinus Torvalds 		retval = (sz >> sizebits);
990bbec0270SLinus Torvalds 	}
991bbec0270SLinus Torvalds 	return retval;
992bbec0270SLinus Torvalds }
993bbec0270SLinus Torvalds 
9941da177e4SLinus Torvalds /*
9956f24ce6bSMatthew Wilcox (Oracle)  * Initialise the state of a blockdev folio's buffers.
9961da177e4SLinus Torvalds  */
9976f24ce6bSMatthew Wilcox (Oracle) static sector_t folio_init_buffers(struct folio *folio,
9986f24ce6bSMatthew Wilcox (Oracle) 		struct block_device *bdev, sector_t block, int size)
9991da177e4SLinus Torvalds {
10006f24ce6bSMatthew Wilcox (Oracle) 	struct buffer_head *head = folio_buffers(folio);
10011da177e4SLinus Torvalds 	struct buffer_head *bh = head;
10026f24ce6bSMatthew Wilcox (Oracle) 	bool uptodate = folio_test_uptodate(folio);
1003bcd1d063SChristoph Hellwig 	sector_t end_block = blkdev_max_block(bdev, size);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	do {
10061da177e4SLinus Torvalds 		if (!buffer_mapped(bh)) {
100701950a34SEric Biggers 			bh->b_end_io = NULL;
100801950a34SEric Biggers 			bh->b_private = NULL;
10091da177e4SLinus Torvalds 			bh->b_bdev = bdev;
10101da177e4SLinus Torvalds 			bh->b_blocknr = block;
10111da177e4SLinus Torvalds 			if (uptodate)
10121da177e4SLinus Torvalds 				set_buffer_uptodate(bh);
1013080399aaSJeff Moyer 			if (block < end_block)
10141da177e4SLinus Torvalds 				set_buffer_mapped(bh);
10151da177e4SLinus Torvalds 		}
10161da177e4SLinus Torvalds 		block++;
10171da177e4SLinus Torvalds 		bh = bh->b_this_page;
10181da177e4SLinus Torvalds 	} while (bh != head);
1019676ce6d5SHugh Dickins 
1020676ce6d5SHugh Dickins 	/*
1021676ce6d5SHugh Dickins 	 * Caller needs to validate requested block against end of device.
1022676ce6d5SHugh Dickins 	 */
1023676ce6d5SHugh Dickins 	return end_block;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds /*
10271da177e4SLinus Torvalds  * Create the page-cache page that contains the requested block.
10281da177e4SLinus Torvalds  *
1029676ce6d5SHugh Dickins  * This is used purely for blockdev mappings.
10301da177e4SLinus Torvalds  */
1031676ce6d5SHugh Dickins static int
10321da177e4SLinus Torvalds grow_dev_page(struct block_device *bdev, sector_t block,
10333b5e6454SGioh Kim 	      pgoff_t index, int size, int sizebits, gfp_t gfp)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	struct inode *inode = bdev->bd_inode;
10363c98a41cSMatthew Wilcox (Oracle) 	struct folio *folio;
10371da177e4SLinus Torvalds 	struct buffer_head *bh;
1038676ce6d5SHugh Dickins 	sector_t end_block;
1039c4b4c2a7SZhiqiang Liu 	int ret = 0;
104084235de3SJohannes Weiner 
10413c98a41cSMatthew Wilcox (Oracle) 	folio = __filemap_get_folio(inode->i_mapping, index,
10423ed65f04SMatthew Wilcox (Oracle) 			FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
10433ed65f04SMatthew Wilcox (Oracle) 	if (IS_ERR(folio))
10443ed65f04SMatthew Wilcox (Oracle) 		return PTR_ERR(folio);
10451da177e4SLinus Torvalds 
10463c98a41cSMatthew Wilcox (Oracle) 	bh = folio_buffers(folio);
10473c98a41cSMatthew Wilcox (Oracle) 	if (bh) {
10481da177e4SLinus Torvalds 		if (bh->b_size == size) {
10496f24ce6bSMatthew Wilcox (Oracle) 			end_block = folio_init_buffers(folio, bdev,
10506f24ce6bSMatthew Wilcox (Oracle) 					(sector_t)index << sizebits, size);
1051676ce6d5SHugh Dickins 			goto done;
10521da177e4SLinus Torvalds 		}
10533c98a41cSMatthew Wilcox (Oracle) 		if (!try_to_free_buffers(folio))
10541da177e4SLinus Torvalds 			goto failed;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
10573ed65f04SMatthew Wilcox (Oracle) 	ret = -ENOMEM;
10583ed65f04SMatthew Wilcox (Oracle) 	bh = folio_alloc_buffers(folio, size, gfp | __GFP_ACCOUNT);
10593ed65f04SMatthew Wilcox (Oracle) 	if (!bh)
10603ed65f04SMatthew Wilcox (Oracle) 		goto failed;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	/*
10633c98a41cSMatthew Wilcox (Oracle) 	 * Link the folio to the buffers and initialise them.  Take the
10641da177e4SLinus Torvalds 	 * lock to be atomic wrt __find_get_block(), which does not
10653c98a41cSMatthew Wilcox (Oracle) 	 * run under the folio lock.
10661da177e4SLinus Torvalds 	 */
10671da177e4SLinus Torvalds 	spin_lock(&inode->i_mapping->private_lock);
106808d84addSMatthew Wilcox (Oracle) 	link_dev_buffers(folio, bh);
10696f24ce6bSMatthew Wilcox (Oracle) 	end_block = folio_init_buffers(folio, bdev,
10703c98a41cSMatthew Wilcox (Oracle) 			(sector_t)index << sizebits, size);
10711da177e4SLinus Torvalds 	spin_unlock(&inode->i_mapping->private_lock);
1072676ce6d5SHugh Dickins done:
1073676ce6d5SHugh Dickins 	ret = (block < end_block) ? 1 : -ENXIO;
10741da177e4SLinus Torvalds failed:
10753c98a41cSMatthew Wilcox (Oracle) 	folio_unlock(folio);
10763c98a41cSMatthew Wilcox (Oracle) 	folio_put(folio);
1077676ce6d5SHugh Dickins 	return ret;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds  * Create buffers for the specified block device block's page.  If
10821da177e4SLinus Torvalds  * that page was dirty, the buffers are set dirty also.
10831da177e4SLinus Torvalds  */
1084858119e1SArjan van de Ven static int
10853b5e6454SGioh Kim grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	pgoff_t index;
10881da177e4SLinus Torvalds 	int sizebits;
10891da177e4SLinus Torvalds 
109090432e60SMikulas Patocka 	sizebits = PAGE_SHIFT - __ffs(size);
10911da177e4SLinus Torvalds 	index = block >> sizebits;
10921da177e4SLinus Torvalds 
1093e5657933SAndrew Morton 	/*
1094e5657933SAndrew Morton 	 * Check for a block which wants to lie outside our maximum possible
1095e5657933SAndrew Morton 	 * pagecache index.  (this comparison is done using sector_t types).
1096e5657933SAndrew Morton 	 */
1097e5657933SAndrew Morton 	if (unlikely(index != block >> sizebits)) {
1098e5657933SAndrew Morton 		printk(KERN_ERR "%s: requested out-of-range block %llu for "
1099a1c6f057SDmitry Monakhov 			"device %pg\n",
11008e24eea7SHarvey Harrison 			__func__, (unsigned long long)block,
1101a1c6f057SDmitry Monakhov 			bdev);
1102e5657933SAndrew Morton 		return -EIO;
1103e5657933SAndrew Morton 	}
1104676ce6d5SHugh Dickins 
11051da177e4SLinus Torvalds 	/* Create a page with the proper size buffers.. */
11063b5e6454SGioh Kim 	return grow_dev_page(bdev, block, index, size, sizebits, gfp);
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
11090026ba40SEric Biggers static struct buffer_head *
11103b5e6454SGioh Kim __getblk_slow(struct block_device *bdev, sector_t block,
11113b5e6454SGioh Kim 	     unsigned size, gfp_t gfp)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	/* Size must be multiple of hard sectorsize */
1114e1defc4fSMartin K. Petersen 	if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
11151da177e4SLinus Torvalds 			(size < 512 || size > PAGE_SIZE))) {
11161da177e4SLinus Torvalds 		printk(KERN_ERR "getblk(): invalid block size %d requested\n",
11171da177e4SLinus Torvalds 					size);
1118e1defc4fSMartin K. Petersen 		printk(KERN_ERR "logical block size: %d\n",
1119e1defc4fSMartin K. Petersen 					bdev_logical_block_size(bdev));
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 		dump_stack();
11221da177e4SLinus Torvalds 		return NULL;
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 
1125676ce6d5SHugh Dickins 	for (;;) {
1126676ce6d5SHugh Dickins 		struct buffer_head *bh;
1127676ce6d5SHugh Dickins 		int ret;
1128676ce6d5SHugh Dickins 
11291da177e4SLinus Torvalds 		bh = __find_get_block(bdev, block, size);
11301da177e4SLinus Torvalds 		if (bh)
11311da177e4SLinus Torvalds 			return bh;
11321da177e4SLinus Torvalds 
11333b5e6454SGioh Kim 		ret = grow_buffers(bdev, block, size, gfp);
1134676ce6d5SHugh Dickins 		if (ret < 0)
113591f68c89SJeff Moyer 			return NULL;
1136676ce6d5SHugh Dickins 	}
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds /*
11401da177e4SLinus Torvalds  * The relationship between dirty buffers and dirty pages:
11411da177e4SLinus Torvalds  *
11421da177e4SLinus Torvalds  * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1143ec82e1c1SMatthew Wilcox  * the page is tagged dirty in the page cache.
11441da177e4SLinus Torvalds  *
11451da177e4SLinus Torvalds  * At all times, the dirtiness of the buffers represents the dirtiness of
11461da177e4SLinus Torvalds  * subsections of the page.  If the page has buffers, the page dirty bit is
11471da177e4SLinus Torvalds  * merely a hint about the true dirty state.
11481da177e4SLinus Torvalds  *
11491da177e4SLinus Torvalds  * When a page is set dirty in its entirety, all its buffers are marked dirty
11501da177e4SLinus Torvalds  * (if the page has buffers).
11511da177e4SLinus Torvalds  *
11521da177e4SLinus Torvalds  * When a buffer is marked dirty, its page is dirtied, but the page's other
11531da177e4SLinus Torvalds  * buffers are not.
11541da177e4SLinus Torvalds  *
11551da177e4SLinus Torvalds  * Also.  When blockdev buffers are explicitly read with bread(), they
11561da177e4SLinus Torvalds  * individually become uptodate.  But their backing page remains not
11571da177e4SLinus Torvalds  * uptodate - even if all of its buffers are uptodate.  A subsequent
11582c69e205SMatthew Wilcox (Oracle)  * block_read_full_folio() against that folio will discover all the uptodate
11592c69e205SMatthew Wilcox (Oracle)  * buffers, will set the folio uptodate and will perform no I/O.
11601da177e4SLinus Torvalds  */
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds /**
11631da177e4SLinus Torvalds  * mark_buffer_dirty - mark a buffer_head as needing writeout
116467be2dd1SMartin Waitz  * @bh: the buffer_head to mark dirty
11651da177e4SLinus Torvalds  *
1166ec82e1c1SMatthew Wilcox  * mark_buffer_dirty() will set the dirty bit against the buffer, then set
1167ec82e1c1SMatthew Wilcox  * its backing page dirty, then tag the page as dirty in the page cache
1168ec82e1c1SMatthew Wilcox  * and then attach the address_space's inode to its superblock's dirty
11691da177e4SLinus Torvalds  * inode list.
11701da177e4SLinus Torvalds  *
1171abc8a8a2SMatthew Wilcox (Oracle)  * mark_buffer_dirty() is atomic.  It takes bh->b_folio->mapping->private_lock,
1172b93b0163SMatthew Wilcox  * i_pages lock and mapping->host->i_lock.
11731da177e4SLinus Torvalds  */
1174fc9b52cdSHarvey Harrison void mark_buffer_dirty(struct buffer_head *bh)
11751da177e4SLinus Torvalds {
1176787d2214SNick Piggin 	WARN_ON_ONCE(!buffer_uptodate(bh));
11771be62dc1SLinus Torvalds 
11785305cb83STejun Heo 	trace_block_dirty_buffer(bh);
11795305cb83STejun Heo 
11801be62dc1SLinus Torvalds 	/*
11811be62dc1SLinus Torvalds 	 * Very *carefully* optimize the it-is-already-dirty case.
11821be62dc1SLinus Torvalds 	 *
11831be62dc1SLinus Torvalds 	 * Don't let the final "is it dirty" escape to before we
11841be62dc1SLinus Torvalds 	 * perhaps modified the buffer.
11851be62dc1SLinus Torvalds 	 */
11861be62dc1SLinus Torvalds 	if (buffer_dirty(bh)) {
11871be62dc1SLinus Torvalds 		smp_mb();
11881be62dc1SLinus Torvalds 		if (buffer_dirty(bh))
11891be62dc1SLinus Torvalds 			return;
11901be62dc1SLinus Torvalds 	}
11911be62dc1SLinus Torvalds 
1192a8e7d49aSLinus Torvalds 	if (!test_set_buffer_dirty(bh)) {
1193cf1d3417SMatthew Wilcox (Oracle) 		struct folio *folio = bh->b_folio;
1194c4843a75SGreg Thelen 		struct address_space *mapping = NULL;
1195c4843a75SGreg Thelen 
1196cf1d3417SMatthew Wilcox (Oracle) 		folio_memcg_lock(folio);
1197cf1d3417SMatthew Wilcox (Oracle) 		if (!folio_test_set_dirty(folio)) {
1198cf1d3417SMatthew Wilcox (Oracle) 			mapping = folio->mapping;
11998e9d78edSLinus Torvalds 			if (mapping)
1200cf1d3417SMatthew Wilcox (Oracle) 				__folio_mark_dirty(folio, mapping, 0);
12018e9d78edSLinus Torvalds 		}
1202cf1d3417SMatthew Wilcox (Oracle) 		folio_memcg_unlock(folio);
1203c4843a75SGreg Thelen 		if (mapping)
1204c4843a75SGreg Thelen 			__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1205a8e7d49aSLinus Torvalds 	}
12061da177e4SLinus Torvalds }
12071fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(mark_buffer_dirty);
12081da177e4SLinus Torvalds 
120987354e5dSJeff Layton void mark_buffer_write_io_error(struct buffer_head *bh)
121087354e5dSJeff Layton {
121187354e5dSJeff Layton 	set_buffer_write_io_error(bh);
121287354e5dSJeff Layton 	/* FIXME: do we need to set this in both places? */
1213abc8a8a2SMatthew Wilcox (Oracle) 	if (bh->b_folio && bh->b_folio->mapping)
1214abc8a8a2SMatthew Wilcox (Oracle) 		mapping_set_error(bh->b_folio->mapping, -EIO);
12154b2201daSChristoph Hellwig 	if (bh->b_assoc_map) {
121687354e5dSJeff Layton 		mapping_set_error(bh->b_assoc_map, -EIO);
12174b2201daSChristoph Hellwig 		errseq_set(&bh->b_assoc_map->host->i_sb->s_wb_err, -EIO);
12184b2201daSChristoph Hellwig 	}
121987354e5dSJeff Layton }
122087354e5dSJeff Layton EXPORT_SYMBOL(mark_buffer_write_io_error);
122187354e5dSJeff Layton 
12221da177e4SLinus Torvalds /*
12231da177e4SLinus Torvalds  * Decrement a buffer_head's reference count.  If all buffers against a page
12241da177e4SLinus Torvalds  * have zero reference count, are clean and unlocked, and if the page is clean
12251da177e4SLinus Torvalds  * and unlocked then try_to_free_buffers() may strip the buffers from the page
12261da177e4SLinus Torvalds  * in preparation for freeing it (sometimes, rarely, buffers are removed from
12271da177e4SLinus Torvalds  * a page but it ends up not being freed, and buffers may later be reattached).
12281da177e4SLinus Torvalds  */
12291da177e4SLinus Torvalds void __brelse(struct buffer_head * buf)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	if (atomic_read(&buf->b_count)) {
12321da177e4SLinus Torvalds 		put_bh(buf);
12331da177e4SLinus Torvalds 		return;
12341da177e4SLinus Torvalds 	}
12355c752ad9SArjan van de Ven 	WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
12361da177e4SLinus Torvalds }
12371fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__brelse);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds /*
12401da177e4SLinus Torvalds  * bforget() is like brelse(), except it discards any
12411da177e4SLinus Torvalds  * potentially dirty data.
12421da177e4SLinus Torvalds  */
12431da177e4SLinus Torvalds void __bforget(struct buffer_head *bh)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	clear_buffer_dirty(bh);
1246535ee2fbSJan Kara 	if (bh->b_assoc_map) {
1247abc8a8a2SMatthew Wilcox (Oracle) 		struct address_space *buffer_mapping = bh->b_folio->mapping;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
12501da177e4SLinus Torvalds 		list_del_init(&bh->b_assoc_buffers);
125158ff407bSJan Kara 		bh->b_assoc_map = NULL;
12521da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 	__brelse(bh);
12551da177e4SLinus Torvalds }
12561fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__bforget);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds static struct buffer_head *__bread_slow(struct buffer_head *bh)
12591da177e4SLinus Torvalds {
12601da177e4SLinus Torvalds 	lock_buffer(bh);
12611da177e4SLinus Torvalds 	if (buffer_uptodate(bh)) {
12621da177e4SLinus Torvalds 		unlock_buffer(bh);
12631da177e4SLinus Torvalds 		return bh;
12641da177e4SLinus Torvalds 	} else {
12651da177e4SLinus Torvalds 		get_bh(bh);
12661da177e4SLinus Torvalds 		bh->b_end_io = end_buffer_read_sync;
12671420c4a5SBart Van Assche 		submit_bh(REQ_OP_READ, bh);
12681da177e4SLinus Torvalds 		wait_on_buffer(bh);
12691da177e4SLinus Torvalds 		if (buffer_uptodate(bh))
12701da177e4SLinus Torvalds 			return bh;
12711da177e4SLinus Torvalds 	}
12721da177e4SLinus Torvalds 	brelse(bh);
12731da177e4SLinus Torvalds 	return NULL;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds /*
12771da177e4SLinus Torvalds  * Per-cpu buffer LRU implementation.  To reduce the cost of __find_get_block().
12781da177e4SLinus Torvalds  * The bhs[] array is sorted - newest buffer is at bhs[0].  Buffers have their
12791da177e4SLinus Torvalds  * refcount elevated by one when they're in an LRU.  A buffer can only appear
12801da177e4SLinus Torvalds  * once in a particular CPU's LRU.  A single buffer can be present in multiple
12811da177e4SLinus Torvalds  * CPU's LRUs at the same time.
12821da177e4SLinus Torvalds  *
12831da177e4SLinus Torvalds  * This is a transparent caching front-end to sb_bread(), sb_getblk() and
12841da177e4SLinus Torvalds  * sb_find_get_block().
12851da177e4SLinus Torvalds  *
12861da177e4SLinus Torvalds  * The LRUs themselves only need locking against invalidate_bh_lrus.  We use
12871da177e4SLinus Torvalds  * a local interrupt disable for that.
12881da177e4SLinus Torvalds  */
12891da177e4SLinus Torvalds 
129086cf78d7SSebastien Buisson #define BH_LRU_SIZE	16
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds struct bh_lru {
12931da177e4SLinus Torvalds 	struct buffer_head *bhs[BH_LRU_SIZE];
12941da177e4SLinus Torvalds };
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds #ifdef CONFIG_SMP
12991da177e4SLinus Torvalds #define bh_lru_lock()	local_irq_disable()
13001da177e4SLinus Torvalds #define bh_lru_unlock()	local_irq_enable()
13011da177e4SLinus Torvalds #else
13021da177e4SLinus Torvalds #define bh_lru_lock()	preempt_disable()
13031da177e4SLinus Torvalds #define bh_lru_unlock()	preempt_enable()
13041da177e4SLinus Torvalds #endif
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds static inline void check_irqs_on(void)
13071da177e4SLinus Torvalds {
13081da177e4SLinus Torvalds #ifdef irqs_disabled
13091da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
13101da177e4SLinus Torvalds #endif
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds /*
1314241f01fbSEric Biggers  * Install a buffer_head into this cpu's LRU.  If not already in the LRU, it is
1315241f01fbSEric Biggers  * inserted at the front, and the buffer_head at the back if any is evicted.
1316241f01fbSEric Biggers  * Or, if already in the LRU it is moved to the front.
13171da177e4SLinus Torvalds  */
13181da177e4SLinus Torvalds static void bh_lru_install(struct buffer_head *bh)
13191da177e4SLinus Torvalds {
1320241f01fbSEric Biggers 	struct buffer_head *evictee = bh;
1321241f01fbSEric Biggers 	struct bh_lru *b;
1322241f01fbSEric Biggers 	int i;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	check_irqs_on();
1325c0226eb8SMinchan Kim 	bh_lru_lock();
1326c0226eb8SMinchan Kim 
13278cc621d2SMinchan Kim 	/*
13288cc621d2SMinchan Kim 	 * the refcount of buffer_head in bh_lru prevents dropping the
13298cc621d2SMinchan Kim 	 * attached page(i.e., try_to_free_buffers) so it could cause
13308cc621d2SMinchan Kim 	 * failing page migration.
13318cc621d2SMinchan Kim 	 * Skip putting upcoming bh into bh_lru until migration is done.
13328cc621d2SMinchan Kim 	 */
13338a237adfSMarcelo Tosatti 	if (lru_cache_disabled() || cpu_is_isolated(smp_processor_id())) {
1334c0226eb8SMinchan Kim 		bh_lru_unlock();
13358cc621d2SMinchan Kim 		return;
1336c0226eb8SMinchan Kim 	}
1337241f01fbSEric Biggers 
1338241f01fbSEric Biggers 	b = this_cpu_ptr(&bh_lrus);
1339241f01fbSEric Biggers 	for (i = 0; i < BH_LRU_SIZE; i++) {
1340241f01fbSEric Biggers 		swap(evictee, b->bhs[i]);
1341241f01fbSEric Biggers 		if (evictee == bh) {
1342241f01fbSEric Biggers 			bh_lru_unlock();
1343241f01fbSEric Biggers 			return;
1344241f01fbSEric Biggers 		}
1345241f01fbSEric Biggers 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	get_bh(bh);
13481da177e4SLinus Torvalds 	bh_lru_unlock();
1349241f01fbSEric Biggers 	brelse(evictee);
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds /*
13531da177e4SLinus Torvalds  * Look up the bh in this cpu's LRU.  If it's there, move it to the head.
13541da177e4SLinus Torvalds  */
1355858119e1SArjan van de Ven static struct buffer_head *
13563991d3bdSTomasz Kvarsin lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	struct buffer_head *ret = NULL;
13593991d3bdSTomasz Kvarsin 	unsigned int i;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	check_irqs_on();
13621da177e4SLinus Torvalds 	bh_lru_lock();
13638a237adfSMarcelo Tosatti 	if (cpu_is_isolated(smp_processor_id())) {
13648a237adfSMarcelo Tosatti 		bh_lru_unlock();
13658a237adfSMarcelo Tosatti 		return NULL;
13668a237adfSMarcelo Tosatti 	}
13671da177e4SLinus Torvalds 	for (i = 0; i < BH_LRU_SIZE; i++) {
1368c7b92516SChristoph Lameter 		struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
13691da177e4SLinus Torvalds 
13709470dd5dSZach Brown 		if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
13719470dd5dSZach Brown 		    bh->b_size == size) {
13721da177e4SLinus Torvalds 			if (i) {
13731da177e4SLinus Torvalds 				while (i) {
1374c7b92516SChristoph Lameter 					__this_cpu_write(bh_lrus.bhs[i],
1375c7b92516SChristoph Lameter 						__this_cpu_read(bh_lrus.bhs[i - 1]));
13761da177e4SLinus Torvalds 					i--;
13771da177e4SLinus Torvalds 				}
1378c7b92516SChristoph Lameter 				__this_cpu_write(bh_lrus.bhs[0], bh);
13791da177e4SLinus Torvalds 			}
13801da177e4SLinus Torvalds 			get_bh(bh);
13811da177e4SLinus Torvalds 			ret = bh;
13821da177e4SLinus Torvalds 			break;
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 	}
13851da177e4SLinus Torvalds 	bh_lru_unlock();
13861da177e4SLinus Torvalds 	return ret;
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds /*
13901da177e4SLinus Torvalds  * Perform a pagecache lookup for the matching buffer.  If it's there, refresh
13911da177e4SLinus Torvalds  * it in the LRU and mark it as accessed.  If it is not present then return
13921da177e4SLinus Torvalds  * NULL
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds struct buffer_head *
13953991d3bdSTomasz Kvarsin __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if (bh == NULL) {
14002457aec6SMel Gorman 		/* __find_get_block_slow will mark the page accessed */
1401385fd4c5SCoywolf Qi Hunt 		bh = __find_get_block_slow(bdev, block);
14021da177e4SLinus Torvalds 		if (bh)
14031da177e4SLinus Torvalds 			bh_lru_install(bh);
14042457aec6SMel Gorman 	} else
14051da177e4SLinus Torvalds 		touch_buffer(bh);
14062457aec6SMel Gorman 
14071da177e4SLinus Torvalds 	return bh;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds EXPORT_SYMBOL(__find_get_block);
14101da177e4SLinus Torvalds 
14113ed65f04SMatthew Wilcox (Oracle) /**
14123ed65f04SMatthew Wilcox (Oracle)  * bdev_getblk - Get a buffer_head in a block device's buffer cache.
14133ed65f04SMatthew Wilcox (Oracle)  * @bdev: The block device.
14143ed65f04SMatthew Wilcox (Oracle)  * @block: The block number.
14153ed65f04SMatthew Wilcox (Oracle)  * @size: The size of buffer_heads for this @bdev.
14163ed65f04SMatthew Wilcox (Oracle)  * @gfp: The memory allocation flags to use.
14173ed65f04SMatthew Wilcox (Oracle)  *
14183ed65f04SMatthew Wilcox (Oracle)  * Return: The buffer head, or NULL if memory could not be allocated.
14193ed65f04SMatthew Wilcox (Oracle)  */
14203ed65f04SMatthew Wilcox (Oracle) struct buffer_head *bdev_getblk(struct block_device *bdev, sector_t block,
14213ed65f04SMatthew Wilcox (Oracle) 		unsigned size, gfp_t gfp)
14223ed65f04SMatthew Wilcox (Oracle) {
14233ed65f04SMatthew Wilcox (Oracle) 	struct buffer_head *bh = __find_get_block(bdev, block, size);
14243ed65f04SMatthew Wilcox (Oracle) 
14253ed65f04SMatthew Wilcox (Oracle) 	might_alloc(gfp);
14263ed65f04SMatthew Wilcox (Oracle) 	if (bh)
14273ed65f04SMatthew Wilcox (Oracle) 		return bh;
14283ed65f04SMatthew Wilcox (Oracle) 
14293ed65f04SMatthew Wilcox (Oracle) 	return __getblk_slow(bdev, block, size, gfp);
14303ed65f04SMatthew Wilcox (Oracle) }
14313ed65f04SMatthew Wilcox (Oracle) EXPORT_SYMBOL(bdev_getblk);
14323ed65f04SMatthew Wilcox (Oracle) 
14331da177e4SLinus Torvalds /*
14341da177e4SLinus Torvalds  * Do async read-ahead on a buffer..
14351da177e4SLinus Torvalds  */
14363991d3bdSTomasz Kvarsin void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
14371da177e4SLinus Torvalds {
1438775d9b10SMatthew Wilcox (Oracle) 	struct buffer_head *bh = bdev_getblk(bdev, block, size,
1439775d9b10SMatthew Wilcox (Oracle) 			GFP_NOWAIT | __GFP_MOVABLE);
1440775d9b10SMatthew Wilcox (Oracle) 
1441a3e713b5SAndrew Morton 	if (likely(bh)) {
1442e7ea1129SZhang Yi 		bh_readahead(bh, REQ_RAHEAD);
14431da177e4SLinus Torvalds 		brelse(bh);
14441da177e4SLinus Torvalds 	}
1445a3e713b5SAndrew Morton }
14461da177e4SLinus Torvalds EXPORT_SYMBOL(__breadahead);
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds /**
14493b5e6454SGioh Kim  *  __bread_gfp() - reads a specified block and returns the bh
145067be2dd1SMartin Waitz  *  @bdev: the block_device to read from
14511da177e4SLinus Torvalds  *  @block: number of block
14521da177e4SLinus Torvalds  *  @size: size (in bytes) to read
14533b5e6454SGioh Kim  *  @gfp: page allocation flag
14541da177e4SLinus Torvalds  *
14551da177e4SLinus Torvalds  *  Reads a specified block, and returns buffer head that contains it.
14563b5e6454SGioh Kim  *  The page cache can be allocated from non-movable area
14573b5e6454SGioh Kim  *  not to prevent page migration if you set gfp to zero.
14581da177e4SLinus Torvalds  *  It returns NULL if the block was unreadable.
14591da177e4SLinus Torvalds  */
14601da177e4SLinus Torvalds struct buffer_head *
14613b5e6454SGioh Kim __bread_gfp(struct block_device *bdev, sector_t block,
14623b5e6454SGioh Kim 		   unsigned size, gfp_t gfp)
14631da177e4SLinus Torvalds {
146493b13ecaSMatthew Wilcox (Oracle) 	struct buffer_head *bh;
146593b13ecaSMatthew Wilcox (Oracle) 
146693b13ecaSMatthew Wilcox (Oracle) 	gfp |= mapping_gfp_constraint(bdev->bd_inode->i_mapping, ~__GFP_FS);
146793b13ecaSMatthew Wilcox (Oracle) 
146893b13ecaSMatthew Wilcox (Oracle) 	/*
146993b13ecaSMatthew Wilcox (Oracle) 	 * Prefer looping in the allocator rather than here, at least that
147093b13ecaSMatthew Wilcox (Oracle) 	 * code knows what it's doing.
147193b13ecaSMatthew Wilcox (Oracle) 	 */
147293b13ecaSMatthew Wilcox (Oracle) 	gfp |= __GFP_NOFAIL;
147393b13ecaSMatthew Wilcox (Oracle) 
147493b13ecaSMatthew Wilcox (Oracle) 	bh = bdev_getblk(bdev, block, size, gfp);
14751da177e4SLinus Torvalds 
1476a3e713b5SAndrew Morton 	if (likely(bh) && !buffer_uptodate(bh))
14771da177e4SLinus Torvalds 		bh = __bread_slow(bh);
14781da177e4SLinus Torvalds 	return bh;
14791da177e4SLinus Torvalds }
14803b5e6454SGioh Kim EXPORT_SYMBOL(__bread_gfp);
14811da177e4SLinus Torvalds 
14828cc621d2SMinchan Kim static void __invalidate_bh_lrus(struct bh_lru *b)
14838cc621d2SMinchan Kim {
14848cc621d2SMinchan Kim 	int i;
14858cc621d2SMinchan Kim 
14868cc621d2SMinchan Kim 	for (i = 0; i < BH_LRU_SIZE; i++) {
14878cc621d2SMinchan Kim 		brelse(b->bhs[i]);
14888cc621d2SMinchan Kim 		b->bhs[i] = NULL;
14898cc621d2SMinchan Kim 	}
14908cc621d2SMinchan Kim }
14911da177e4SLinus Torvalds /*
14921da177e4SLinus Torvalds  * invalidate_bh_lrus() is called rarely - but not only at unmount.
14931da177e4SLinus Torvalds  * This doesn't race because it runs in each cpu either in irq
14941da177e4SLinus Torvalds  * or with preempt disabled.
14951da177e4SLinus Torvalds  */
14961da177e4SLinus Torvalds static void invalidate_bh_lru(void *arg)
14971da177e4SLinus Torvalds {
14981da177e4SLinus Torvalds 	struct bh_lru *b = &get_cpu_var(bh_lrus);
14991da177e4SLinus Torvalds 
15008cc621d2SMinchan Kim 	__invalidate_bh_lrus(b);
15011da177e4SLinus Torvalds 	put_cpu_var(bh_lrus);
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15048cc621d2SMinchan Kim bool has_bh_in_lru(int cpu, void *dummy)
150542be35d0SGilad Ben-Yossef {
150642be35d0SGilad Ben-Yossef 	struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
150742be35d0SGilad Ben-Yossef 	int i;
150842be35d0SGilad Ben-Yossef 
150942be35d0SGilad Ben-Yossef 	for (i = 0; i < BH_LRU_SIZE; i++) {
151042be35d0SGilad Ben-Yossef 		if (b->bhs[i])
15111d706679SSaurav Girepunje 			return true;
151242be35d0SGilad Ben-Yossef 	}
151342be35d0SGilad Ben-Yossef 
15141d706679SSaurav Girepunje 	return false;
151542be35d0SGilad Ben-Yossef }
151642be35d0SGilad Ben-Yossef 
1517f9a14399SPeter Zijlstra void invalidate_bh_lrus(void)
15181da177e4SLinus Torvalds {
1519cb923159SSebastian Andrzej Siewior 	on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1);
15201da177e4SLinus Torvalds }
15219db5579bSNick Piggin EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
15221da177e4SLinus Torvalds 
1523243418e3SMinchan Kim /*
1524243418e3SMinchan Kim  * It's called from workqueue context so we need a bh_lru_lock to close
1525243418e3SMinchan Kim  * the race with preemption/irq.
1526243418e3SMinchan Kim  */
1527243418e3SMinchan Kim void invalidate_bh_lrus_cpu(void)
15288cc621d2SMinchan Kim {
15298cc621d2SMinchan Kim 	struct bh_lru *b;
15308cc621d2SMinchan Kim 
15318cc621d2SMinchan Kim 	bh_lru_lock();
1532243418e3SMinchan Kim 	b = this_cpu_ptr(&bh_lrus);
15338cc621d2SMinchan Kim 	__invalidate_bh_lrus(b);
15348cc621d2SMinchan Kim 	bh_lru_unlock();
15358cc621d2SMinchan Kim }
15368cc621d2SMinchan Kim 
1537465e5e6aSPankaj Raghav void folio_set_bh(struct buffer_head *bh, struct folio *folio,
1538465e5e6aSPankaj Raghav 		  unsigned long offset)
1539465e5e6aSPankaj Raghav {
1540465e5e6aSPankaj Raghav 	bh->b_folio = folio;
1541465e5e6aSPankaj Raghav 	BUG_ON(offset >= folio_size(folio));
1542465e5e6aSPankaj Raghav 	if (folio_test_highmem(folio))
1543465e5e6aSPankaj Raghav 		/*
1544465e5e6aSPankaj Raghav 		 * This catches illegal uses and preserves the offset:
1545465e5e6aSPankaj Raghav 		 */
1546465e5e6aSPankaj Raghav 		bh->b_data = (char *)(0 + offset);
1547465e5e6aSPankaj Raghav 	else
1548465e5e6aSPankaj Raghav 		bh->b_data = folio_address(folio) + offset;
1549465e5e6aSPankaj Raghav }
1550465e5e6aSPankaj Raghav EXPORT_SYMBOL(folio_set_bh);
1551465e5e6aSPankaj Raghav 
15521da177e4SLinus Torvalds /*
15531da177e4SLinus Torvalds  * Called when truncating a buffer on a page completely.
15541da177e4SLinus Torvalds  */
1555e7470ee8SMel Gorman 
1556e7470ee8SMel Gorman /* Bits that are cleared during an invalidate */
1557e7470ee8SMel Gorman #define BUFFER_FLAGS_DISCARD \
1558e7470ee8SMel Gorman 	(1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
1559e7470ee8SMel Gorman 	 1 << BH_Delay | 1 << BH_Unwritten)
1560e7470ee8SMel Gorman 
1561858119e1SArjan van de Ven static void discard_buffer(struct buffer_head * bh)
15621da177e4SLinus Torvalds {
1563b0192296SUros Bizjak 	unsigned long b_state;
1564e7470ee8SMel Gorman 
15651da177e4SLinus Torvalds 	lock_buffer(bh);
15661da177e4SLinus Torvalds 	clear_buffer_dirty(bh);
15671da177e4SLinus Torvalds 	bh->b_bdev = NULL;
1568b0192296SUros Bizjak 	b_state = READ_ONCE(bh->b_state);
1569b0192296SUros Bizjak 	do {
1570b0192296SUros Bizjak 	} while (!try_cmpxchg(&bh->b_state, &b_state,
1571b0192296SUros Bizjak 			      b_state & ~BUFFER_FLAGS_DISCARD));
15721da177e4SLinus Torvalds 	unlock_buffer(bh);
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds /**
15767ba13abbSMatthew Wilcox (Oracle)  * block_invalidate_folio - Invalidate part or all of a buffer-backed folio.
15777ba13abbSMatthew Wilcox (Oracle)  * @folio: The folio which is affected.
1578d47992f8SLukas Czerner  * @offset: start of the range to invalidate
1579d47992f8SLukas Czerner  * @length: length of the range to invalidate
15801da177e4SLinus Torvalds  *
15817ba13abbSMatthew Wilcox (Oracle)  * block_invalidate_folio() is called when all or part of the folio has been
15821da177e4SLinus Torvalds  * invalidated by a truncate operation.
15831da177e4SLinus Torvalds  *
15847ba13abbSMatthew Wilcox (Oracle)  * block_invalidate_folio() does not have to release all buffers, but it must
15851da177e4SLinus Torvalds  * ensure that no dirty buffer is left outside @offset and that no I/O
15861da177e4SLinus Torvalds  * is underway against any of the blocks which are outside the truncation
15871da177e4SLinus Torvalds  * point.  Because the caller is about to free (and possibly reuse) those
15881da177e4SLinus Torvalds  * blocks on-disk.
15891da177e4SLinus Torvalds  */
15907ba13abbSMatthew Wilcox (Oracle) void block_invalidate_folio(struct folio *folio, size_t offset, size_t length)
15911da177e4SLinus Torvalds {
15921da177e4SLinus Torvalds 	struct buffer_head *head, *bh, *next;
15937ba13abbSMatthew Wilcox (Oracle) 	size_t curr_off = 0;
15947ba13abbSMatthew Wilcox (Oracle) 	size_t stop = length + offset;
15951da177e4SLinus Torvalds 
15967ba13abbSMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
15971da177e4SLinus Torvalds 
1598d47992f8SLukas Czerner 	/*
1599d47992f8SLukas Czerner 	 * Check for overflow
1600d47992f8SLukas Czerner 	 */
16017ba13abbSMatthew Wilcox (Oracle) 	BUG_ON(stop > folio_size(folio) || stop < length);
1602d47992f8SLukas Czerner 
16037ba13abbSMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
16047ba13abbSMatthew Wilcox (Oracle) 	if (!head)
16057ba13abbSMatthew Wilcox (Oracle) 		return;
16067ba13abbSMatthew Wilcox (Oracle) 
16071da177e4SLinus Torvalds 	bh = head;
16081da177e4SLinus Torvalds 	do {
16097ba13abbSMatthew Wilcox (Oracle) 		size_t next_off = curr_off + bh->b_size;
16101da177e4SLinus Torvalds 		next = bh->b_this_page;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 		/*
1613d47992f8SLukas Czerner 		 * Are we still fully in range ?
1614d47992f8SLukas Czerner 		 */
1615d47992f8SLukas Czerner 		if (next_off > stop)
1616d47992f8SLukas Czerner 			goto out;
1617d47992f8SLukas Czerner 
1618d47992f8SLukas Czerner 		/*
16191da177e4SLinus Torvalds 		 * is this block fully invalidated?
16201da177e4SLinus Torvalds 		 */
16211da177e4SLinus Torvalds 		if (offset <= curr_off)
16221da177e4SLinus Torvalds 			discard_buffer(bh);
16231da177e4SLinus Torvalds 		curr_off = next_off;
16241da177e4SLinus Torvalds 		bh = next;
16251da177e4SLinus Torvalds 	} while (bh != head);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	/*
16287ba13abbSMatthew Wilcox (Oracle) 	 * We release buffers only if the entire folio is being invalidated.
16291da177e4SLinus Torvalds 	 * The get_block cached value has been unconditionally invalidated,
16301da177e4SLinus Torvalds 	 * so real IO is not possible anymore.
16311da177e4SLinus Torvalds 	 */
16327ba13abbSMatthew Wilcox (Oracle) 	if (length == folio_size(folio))
16337ba13abbSMatthew Wilcox (Oracle) 		filemap_release_folio(folio, 0);
16341da177e4SLinus Torvalds out:
16352ff28e22SNeilBrown 	return;
16361da177e4SLinus Torvalds }
16377ba13abbSMatthew Wilcox (Oracle) EXPORT_SYMBOL(block_invalidate_folio);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds /*
16401da177e4SLinus Torvalds  * We attach and possibly dirty the buffers atomically wrt
1641e621900aSMatthew Wilcox (Oracle)  * block_dirty_folio() via private_lock.  try_to_free_buffers
16428e2e1756SPankaj Raghav  * is already excluded via the folio lock.
16431da177e4SLinus Torvalds  */
1644*3decb856SMatthew Wilcox (Oracle) struct buffer_head *folio_create_empty_buffers(struct folio *folio,
1645*3decb856SMatthew Wilcox (Oracle) 		unsigned long blocksize, unsigned long b_state)
16461da177e4SLinus Torvalds {
16471da177e4SLinus Torvalds 	struct buffer_head *bh, *head, *tail;
16482a418157SMatthew Wilcox (Oracle) 	gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT | __GFP_NOFAIL;
16491da177e4SLinus Torvalds 
16502a418157SMatthew Wilcox (Oracle) 	head = folio_alloc_buffers(folio, blocksize, gfp);
16511da177e4SLinus Torvalds 	bh = head;
16521da177e4SLinus Torvalds 	do {
16531da177e4SLinus Torvalds 		bh->b_state |= b_state;
16541da177e4SLinus Torvalds 		tail = bh;
16551da177e4SLinus Torvalds 		bh = bh->b_this_page;
16561da177e4SLinus Torvalds 	} while (bh);
16571da177e4SLinus Torvalds 	tail->b_this_page = head;
16581da177e4SLinus Torvalds 
16598e2e1756SPankaj Raghav 	spin_lock(&folio->mapping->private_lock);
16608e2e1756SPankaj Raghav 	if (folio_test_uptodate(folio) || folio_test_dirty(folio)) {
16611da177e4SLinus Torvalds 		bh = head;
16621da177e4SLinus Torvalds 		do {
16638e2e1756SPankaj Raghav 			if (folio_test_dirty(folio))
16641da177e4SLinus Torvalds 				set_buffer_dirty(bh);
16658e2e1756SPankaj Raghav 			if (folio_test_uptodate(folio))
16661da177e4SLinus Torvalds 				set_buffer_uptodate(bh);
16671da177e4SLinus Torvalds 			bh = bh->b_this_page;
16681da177e4SLinus Torvalds 		} while (bh != head);
16691da177e4SLinus Torvalds 	}
16708e2e1756SPankaj Raghav 	folio_attach_private(folio, head);
16718e2e1756SPankaj Raghav 	spin_unlock(&folio->mapping->private_lock);
1672*3decb856SMatthew Wilcox (Oracle) 
1673*3decb856SMatthew Wilcox (Oracle) 	return head;
16748e2e1756SPankaj Raghav }
16758e2e1756SPankaj Raghav EXPORT_SYMBOL(folio_create_empty_buffers);
16768e2e1756SPankaj Raghav 
16778e2e1756SPankaj Raghav void create_empty_buffers(struct page *page,
16788e2e1756SPankaj Raghav 			unsigned long blocksize, unsigned long b_state)
16798e2e1756SPankaj Raghav {
16808e2e1756SPankaj Raghav 	folio_create_empty_buffers(page_folio(page), blocksize, b_state);
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds EXPORT_SYMBOL(create_empty_buffers);
16831da177e4SLinus Torvalds 
168429f3ad7dSJan Kara /**
168529f3ad7dSJan Kara  * clean_bdev_aliases: clean a range of buffers in block device
168629f3ad7dSJan Kara  * @bdev: Block device to clean buffers in
168729f3ad7dSJan Kara  * @block: Start of a range of blocks to clean
168829f3ad7dSJan Kara  * @len: Number of blocks to clean
16891da177e4SLinus Torvalds  *
169029f3ad7dSJan Kara  * We are taking a range of blocks for data and we don't want writeback of any
169129f3ad7dSJan Kara  * buffer-cache aliases starting from return from this function and until the
169229f3ad7dSJan Kara  * moment when something will explicitly mark the buffer dirty (hopefully that
169329f3ad7dSJan Kara  * will not happen until we will free that block ;-) We don't even need to mark
169429f3ad7dSJan Kara  * it not-uptodate - nobody can expect anything from a newly allocated buffer
169529f3ad7dSJan Kara  * anyway. We used to use unmap_buffer() for such invalidation, but that was
169629f3ad7dSJan Kara  * wrong. We definitely don't want to mark the alias unmapped, for example - it
169729f3ad7dSJan Kara  * would confuse anyone who might pick it with bread() afterwards...
169829f3ad7dSJan Kara  *
169929f3ad7dSJan Kara  * Also..  Note that bforget() doesn't lock the buffer.  So there can be
170029f3ad7dSJan Kara  * writeout I/O going on against recently-freed buffers.  We don't wait on that
170129f3ad7dSJan Kara  * I/O in bforget() - it's more efficient to wait on the I/O only if we really
170229f3ad7dSJan Kara  * need to.  That happens here.
17031da177e4SLinus Torvalds  */
170429f3ad7dSJan Kara void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
17051da177e4SLinus Torvalds {
170629f3ad7dSJan Kara 	struct inode *bd_inode = bdev->bd_inode;
170729f3ad7dSJan Kara 	struct address_space *bd_mapping = bd_inode->i_mapping;
17089e0b6f31SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
170929f3ad7dSJan Kara 	pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
171029f3ad7dSJan Kara 	pgoff_t end;
1711c10f778dSJan Kara 	int i, count;
171229f3ad7dSJan Kara 	struct buffer_head *bh;
171329f3ad7dSJan Kara 	struct buffer_head *head;
17141da177e4SLinus Torvalds 
171529f3ad7dSJan Kara 	end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
17169e0b6f31SMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
17179e0b6f31SMatthew Wilcox (Oracle) 	while (filemap_get_folios(bd_mapping, &index, end, &fbatch)) {
17189e0b6f31SMatthew Wilcox (Oracle) 		count = folio_batch_count(&fbatch);
1719c10f778dSJan Kara 		for (i = 0; i < count; i++) {
17209e0b6f31SMatthew Wilcox (Oracle) 			struct folio *folio = fbatch.folios[i];
17211da177e4SLinus Torvalds 
17229e0b6f31SMatthew Wilcox (Oracle) 			if (!folio_buffers(folio))
172329f3ad7dSJan Kara 				continue;
172429f3ad7dSJan Kara 			/*
17259e0b6f31SMatthew Wilcox (Oracle) 			 * We use folio lock instead of bd_mapping->private_lock
172629f3ad7dSJan Kara 			 * to pin buffers here since we can afford to sleep and
172729f3ad7dSJan Kara 			 * it scales better than a global spinlock lock.
172829f3ad7dSJan Kara 			 */
17299e0b6f31SMatthew Wilcox (Oracle) 			folio_lock(folio);
17309e0b6f31SMatthew Wilcox (Oracle) 			/* Recheck when the folio is locked which pins bhs */
17319e0b6f31SMatthew Wilcox (Oracle) 			head = folio_buffers(folio);
17329e0b6f31SMatthew Wilcox (Oracle) 			if (!head)
173329f3ad7dSJan Kara 				goto unlock_page;
173429f3ad7dSJan Kara 			bh = head;
173529f3ad7dSJan Kara 			do {
17366c006a9dSChandan Rajendra 				if (!buffer_mapped(bh) || (bh->b_blocknr < block))
173729f3ad7dSJan Kara 					goto next;
173829f3ad7dSJan Kara 				if (bh->b_blocknr >= block + len)
173929f3ad7dSJan Kara 					break;
174029f3ad7dSJan Kara 				clear_buffer_dirty(bh);
174129f3ad7dSJan Kara 				wait_on_buffer(bh);
174229f3ad7dSJan Kara 				clear_buffer_req(bh);
174329f3ad7dSJan Kara next:
174429f3ad7dSJan Kara 				bh = bh->b_this_page;
174529f3ad7dSJan Kara 			} while (bh != head);
174629f3ad7dSJan Kara unlock_page:
17479e0b6f31SMatthew Wilcox (Oracle) 			folio_unlock(folio);
174829f3ad7dSJan Kara 		}
17499e0b6f31SMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
175029f3ad7dSJan Kara 		cond_resched();
1751c10f778dSJan Kara 		/* End of range already reached? */
1752c10f778dSJan Kara 		if (index > end || !index)
1753c10f778dSJan Kara 			break;
17541da177e4SLinus Torvalds 	}
17551da177e4SLinus Torvalds }
175629f3ad7dSJan Kara EXPORT_SYMBOL(clean_bdev_aliases);
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds /*
175945bce8f3SLinus Torvalds  * Size is a power-of-two in the range 512..PAGE_SIZE,
176045bce8f3SLinus Torvalds  * and the case we care about most is PAGE_SIZE.
176145bce8f3SLinus Torvalds  *
176245bce8f3SLinus Torvalds  * So this *could* possibly be written with those
176345bce8f3SLinus Torvalds  * constraints in mind (relevant mostly if some
176445bce8f3SLinus Torvalds  * architecture has a slow bit-scan instruction)
176545bce8f3SLinus Torvalds  */
176645bce8f3SLinus Torvalds static inline int block_size_bits(unsigned int blocksize)
176745bce8f3SLinus Torvalds {
176845bce8f3SLinus Torvalds 	return ilog2(blocksize);
176945bce8f3SLinus Torvalds }
177045bce8f3SLinus Torvalds 
1771c6c8c3e7SPankaj Raghav static struct buffer_head *folio_create_buffers(struct folio *folio,
1772c6c8c3e7SPankaj Raghav 						struct inode *inode,
1773c6c8c3e7SPankaj Raghav 						unsigned int b_state)
177445bce8f3SLinus Torvalds {
1775*3decb856SMatthew Wilcox (Oracle) 	struct buffer_head *bh;
1776*3decb856SMatthew Wilcox (Oracle) 
1777c6c8c3e7SPankaj Raghav 	BUG_ON(!folio_test_locked(folio));
177845bce8f3SLinus Torvalds 
1779*3decb856SMatthew Wilcox (Oracle) 	bh = folio_buffers(folio);
1780*3decb856SMatthew Wilcox (Oracle) 	if (!bh)
1781*3decb856SMatthew Wilcox (Oracle) 		bh = folio_create_empty_buffers(folio,
1782*3decb856SMatthew Wilcox (Oracle) 				1 << READ_ONCE(inode->i_blkbits), b_state);
1783*3decb856SMatthew Wilcox (Oracle) 	return bh;
178445bce8f3SLinus Torvalds }
178545bce8f3SLinus Torvalds 
178645bce8f3SLinus Torvalds /*
17871da177e4SLinus Torvalds  * NOTE! All mapped/uptodate combinations are valid:
17881da177e4SLinus Torvalds  *
17891da177e4SLinus Torvalds  *	Mapped	Uptodate	Meaning
17901da177e4SLinus Torvalds  *
17911da177e4SLinus Torvalds  *	No	No		"unknown" - must do get_block()
17921da177e4SLinus Torvalds  *	No	Yes		"hole" - zero-filled
17931da177e4SLinus Torvalds  *	Yes	No		"allocated" - allocated on disk, not read in
17941da177e4SLinus Torvalds  *	Yes	Yes		"valid" - allocated and up-to-date in memory.
17951da177e4SLinus Torvalds  *
17961da177e4SLinus Torvalds  * "Dirty" is valid only with the last case (mapped+uptodate).
17971da177e4SLinus Torvalds  */
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds /*
18001da177e4SLinus Torvalds  * While block_write_full_page is writing back the dirty buffers under
18011da177e4SLinus Torvalds  * the page lock, whoever dirtied the buffers may decide to clean them
18021da177e4SLinus Torvalds  * again at any time.  We handle that by only looking at the buffer
18031da177e4SLinus Torvalds  * state inside lock_buffer().
18041da177e4SLinus Torvalds  *
18051da177e4SLinus Torvalds  * If block_write_full_page() is called for regular writeback
18061da177e4SLinus Torvalds  * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
18071da177e4SLinus Torvalds  * locked buffer.   This only can happen if someone has written the buffer
18081da177e4SLinus Torvalds  * directly, with submit_bh().  At the address_space level PageWriteback
18091da177e4SLinus Torvalds  * prevents this contention from occurring.
18106e34eeddSTheodore Ts'o  *
18116e34eeddSTheodore Ts'o  * If block_write_full_page() is called with wbc->sync_mode ==
181270fd7614SChristoph Hellwig  * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
1813721a9602SJens Axboe  * causes the writes to be flagged as synchronous writes.
18141da177e4SLinus Torvalds  */
181553418a18SMatthew Wilcox (Oracle) int __block_write_full_folio(struct inode *inode, struct folio *folio,
181635c80d5fSChris Mason 			get_block_t *get_block, struct writeback_control *wbc,
181735c80d5fSChris Mason 			bh_end_io_t *handler)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	int err;
18201da177e4SLinus Torvalds 	sector_t block;
18211da177e4SLinus Torvalds 	sector_t last_block;
1822f0fbd5fcSAndrew Morton 	struct buffer_head *bh, *head;
182345bce8f3SLinus Torvalds 	unsigned int blocksize, bbits;
18241da177e4SLinus Torvalds 	int nr_underway = 0;
18253ae72869SBart Van Assche 	blk_opf_t write_flags = wbc_to_write_flags(wbc);
18261da177e4SLinus Torvalds 
182753418a18SMatthew Wilcox (Oracle) 	head = folio_create_buffers(folio, inode,
18281da177e4SLinus Torvalds 				    (1 << BH_Dirty) | (1 << BH_Uptodate));
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	/*
1831e621900aSMatthew Wilcox (Oracle) 	 * Be very careful.  We have no exclusion from block_dirty_folio
18321da177e4SLinus Torvalds 	 * here, and the (potentially unmapped) buffers may become dirty at
18331da177e4SLinus Torvalds 	 * any time.  If a buffer becomes dirty here after we've inspected it
183453418a18SMatthew Wilcox (Oracle) 	 * then we just miss that fact, and the folio stays dirty.
18351da177e4SLinus Torvalds 	 *
1836e621900aSMatthew Wilcox (Oracle) 	 * Buffers outside i_size may be dirtied by block_dirty_folio;
18371da177e4SLinus Torvalds 	 * handle that here by just cleaning them.
18381da177e4SLinus Torvalds 	 */
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	bh = head;
184145bce8f3SLinus Torvalds 	blocksize = bh->b_size;
184245bce8f3SLinus Torvalds 	bbits = block_size_bits(blocksize);
184345bce8f3SLinus Torvalds 
184453418a18SMatthew Wilcox (Oracle) 	block = (sector_t)folio->index << (PAGE_SHIFT - bbits);
184545bce8f3SLinus Torvalds 	last_block = (i_size_read(inode) - 1) >> bbits;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	/*
18481da177e4SLinus Torvalds 	 * Get all the dirty buffers mapped to disk addresses and
18491da177e4SLinus Torvalds 	 * handle any aliases from the underlying blockdev's mapping.
18501da177e4SLinus Torvalds 	 */
18511da177e4SLinus Torvalds 	do {
18521da177e4SLinus Torvalds 		if (block > last_block) {
18531da177e4SLinus Torvalds 			/*
18541da177e4SLinus Torvalds 			 * mapped buffers outside i_size will occur, because
185553418a18SMatthew Wilcox (Oracle) 			 * this folio can be outside i_size when there is a
18561da177e4SLinus Torvalds 			 * truncate in progress.
18571da177e4SLinus Torvalds 			 */
18581da177e4SLinus Torvalds 			/*
18591da177e4SLinus Torvalds 			 * The buffer was zeroed by block_write_full_page()
18601da177e4SLinus Torvalds 			 */
18611da177e4SLinus Torvalds 			clear_buffer_dirty(bh);
18621da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
186329a814d2SAlex Tomas 		} else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
186429a814d2SAlex Tomas 			   buffer_dirty(bh)) {
1865b0cf2321SBadari Pulavarty 			WARN_ON(bh->b_size != blocksize);
18661da177e4SLinus Torvalds 			err = get_block(inode, block, bh, 1);
18671da177e4SLinus Torvalds 			if (err)
18681da177e4SLinus Torvalds 				goto recover;
186929a814d2SAlex Tomas 			clear_buffer_delay(bh);
18701da177e4SLinus Torvalds 			if (buffer_new(bh)) {
18711da177e4SLinus Torvalds 				/* blockdev mappings never come here */
18721da177e4SLinus Torvalds 				clear_buffer_new(bh);
1873e64855c6SJan Kara 				clean_bdev_bh_alias(bh);
18741da177e4SLinus Torvalds 			}
18751da177e4SLinus Torvalds 		}
18761da177e4SLinus Torvalds 		bh = bh->b_this_page;
18771da177e4SLinus Torvalds 		block++;
18781da177e4SLinus Torvalds 	} while (bh != head);
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	do {
18811da177e4SLinus Torvalds 		if (!buffer_mapped(bh))
18821da177e4SLinus Torvalds 			continue;
18831da177e4SLinus Torvalds 		/*
18841da177e4SLinus Torvalds 		 * If it's a fully non-blocking write attempt and we cannot
188553418a18SMatthew Wilcox (Oracle) 		 * lock the buffer then redirty the folio.  Note that this can
18865b0830cbSJens Axboe 		 * potentially cause a busy-wait loop from writeback threads
18875b0830cbSJens Axboe 		 * and kswapd activity, but those code paths have their own
18885b0830cbSJens Axboe 		 * higher-level throttling.
18891da177e4SLinus Torvalds 		 */
18901b430beeSWu Fengguang 		if (wbc->sync_mode != WB_SYNC_NONE) {
18911da177e4SLinus Torvalds 			lock_buffer(bh);
1892ca5de404SNick Piggin 		} else if (!trylock_buffer(bh)) {
189353418a18SMatthew Wilcox (Oracle) 			folio_redirty_for_writepage(wbc, folio);
18941da177e4SLinus Torvalds 			continue;
18951da177e4SLinus Torvalds 		}
18961da177e4SLinus Torvalds 		if (test_clear_buffer_dirty(bh)) {
189735c80d5fSChris Mason 			mark_buffer_async_write_endio(bh, handler);
18981da177e4SLinus Torvalds 		} else {
18991da177e4SLinus Torvalds 			unlock_buffer(bh);
19001da177e4SLinus Torvalds 		}
19011da177e4SLinus Torvalds 	} while ((bh = bh->b_this_page) != head);
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	/*
190453418a18SMatthew Wilcox (Oracle) 	 * The folio and its buffers are protected by the writeback flag,
190553418a18SMatthew Wilcox (Oracle) 	 * so we can drop the bh refcounts early.
19061da177e4SLinus Torvalds 	 */
190753418a18SMatthew Wilcox (Oracle) 	BUG_ON(folio_test_writeback(folio));
190853418a18SMatthew Wilcox (Oracle) 	folio_start_writeback(folio);
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	do {
19111da177e4SLinus Torvalds 		struct buffer_head *next = bh->b_this_page;
19121da177e4SLinus Torvalds 		if (buffer_async_write(bh)) {
19131420c4a5SBart Van Assche 			submit_bh_wbc(REQ_OP_WRITE | write_flags, bh, wbc);
19141da177e4SLinus Torvalds 			nr_underway++;
1915ad576e63SNick Piggin 		}
19161da177e4SLinus Torvalds 		bh = next;
19171da177e4SLinus Torvalds 	} while (bh != head);
191853418a18SMatthew Wilcox (Oracle) 	folio_unlock(folio);
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	err = 0;
19211da177e4SLinus Torvalds done:
19221da177e4SLinus Torvalds 	if (nr_underway == 0) {
19231da177e4SLinus Torvalds 		/*
192453418a18SMatthew Wilcox (Oracle) 		 * The folio was marked dirty, but the buffers were
19251da177e4SLinus Torvalds 		 * clean.  Someone wrote them back by hand with
192679f59784SZhang Yi 		 * write_dirty_buffer/submit_bh.  A rare case.
19271da177e4SLinus Torvalds 		 */
192853418a18SMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
19293d67f2d7SNick Piggin 
19301da177e4SLinus Torvalds 		/*
193153418a18SMatthew Wilcox (Oracle) 		 * The folio and buffer_heads can be released at any time from
19321da177e4SLinus Torvalds 		 * here on.
19331da177e4SLinus Torvalds 		 */
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 	return err;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds recover:
19381da177e4SLinus Torvalds 	/*
19391da177e4SLinus Torvalds 	 * ENOSPC, or some other error.  We may already have added some
19401da177e4SLinus Torvalds 	 * blocks to the file, so we need to write these out to avoid
19411da177e4SLinus Torvalds 	 * exposing stale data.
194253418a18SMatthew Wilcox (Oracle) 	 * The folio is currently locked and not marked for writeback
19431da177e4SLinus Torvalds 	 */
19441da177e4SLinus Torvalds 	bh = head;
19451da177e4SLinus Torvalds 	/* Recovery: lock and submit the mapped buffers */
19461da177e4SLinus Torvalds 	do {
194729a814d2SAlex Tomas 		if (buffer_mapped(bh) && buffer_dirty(bh) &&
194829a814d2SAlex Tomas 		    !buffer_delay(bh)) {
19491da177e4SLinus Torvalds 			lock_buffer(bh);
195035c80d5fSChris Mason 			mark_buffer_async_write_endio(bh, handler);
19511da177e4SLinus Torvalds 		} else {
19521da177e4SLinus Torvalds 			/*
19531da177e4SLinus Torvalds 			 * The buffer may have been set dirty during
195453418a18SMatthew Wilcox (Oracle) 			 * attachment to a dirty folio.
19551da177e4SLinus Torvalds 			 */
19561da177e4SLinus Torvalds 			clear_buffer_dirty(bh);
19571da177e4SLinus Torvalds 		}
19581da177e4SLinus Torvalds 	} while ((bh = bh->b_this_page) != head);
195953418a18SMatthew Wilcox (Oracle) 	folio_set_error(folio);
196053418a18SMatthew Wilcox (Oracle) 	BUG_ON(folio_test_writeback(folio));
196153418a18SMatthew Wilcox (Oracle) 	mapping_set_error(folio->mapping, err);
196253418a18SMatthew Wilcox (Oracle) 	folio_start_writeback(folio);
19631da177e4SLinus Torvalds 	do {
19641da177e4SLinus Torvalds 		struct buffer_head *next = bh->b_this_page;
19651da177e4SLinus Torvalds 		if (buffer_async_write(bh)) {
19661da177e4SLinus Torvalds 			clear_buffer_dirty(bh);
19671420c4a5SBart Van Assche 			submit_bh_wbc(REQ_OP_WRITE | write_flags, bh, wbc);
19681da177e4SLinus Torvalds 			nr_underway++;
1969ad576e63SNick Piggin 		}
19701da177e4SLinus Torvalds 		bh = next;
19711da177e4SLinus Torvalds 	} while (bh != head);
197253418a18SMatthew Wilcox (Oracle) 	folio_unlock(folio);
19731da177e4SLinus Torvalds 	goto done;
19741da177e4SLinus Torvalds }
197553418a18SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__block_write_full_folio);
19761da177e4SLinus Torvalds 
1977afddba49SNick Piggin /*
19784a9622f2SMatthew Wilcox (Oracle)  * If a folio has any new buffers, zero them out here, and mark them uptodate
1979afddba49SNick Piggin  * and dirty so they'll be written out (in order to prevent uninitialised
1980afddba49SNick Piggin  * block data from leaking). And clear the new bit.
1981afddba49SNick Piggin  */
19824a9622f2SMatthew Wilcox (Oracle) void folio_zero_new_buffers(struct folio *folio, size_t from, size_t to)
1983afddba49SNick Piggin {
19844a9622f2SMatthew Wilcox (Oracle) 	size_t block_start, block_end;
1985afddba49SNick Piggin 	struct buffer_head *head, *bh;
1986afddba49SNick Piggin 
19874a9622f2SMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
19884a9622f2SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
19894a9622f2SMatthew Wilcox (Oracle) 	if (!head)
1990afddba49SNick Piggin 		return;
1991afddba49SNick Piggin 
19924a9622f2SMatthew Wilcox (Oracle) 	bh = head;
1993afddba49SNick Piggin 	block_start = 0;
1994afddba49SNick Piggin 	do {
1995afddba49SNick Piggin 		block_end = block_start + bh->b_size;
1996afddba49SNick Piggin 
1997afddba49SNick Piggin 		if (buffer_new(bh)) {
1998afddba49SNick Piggin 			if (block_end > from && block_start < to) {
19994a9622f2SMatthew Wilcox (Oracle) 				if (!folio_test_uptodate(folio)) {
20004a9622f2SMatthew Wilcox (Oracle) 					size_t start, xend;
2001afddba49SNick Piggin 
2002afddba49SNick Piggin 					start = max(from, block_start);
20034a9622f2SMatthew Wilcox (Oracle) 					xend = min(to, block_end);
2004afddba49SNick Piggin 
20054a9622f2SMatthew Wilcox (Oracle) 					folio_zero_segment(folio, start, xend);
2006afddba49SNick Piggin 					set_buffer_uptodate(bh);
2007afddba49SNick Piggin 				}
2008afddba49SNick Piggin 
2009afddba49SNick Piggin 				clear_buffer_new(bh);
2010afddba49SNick Piggin 				mark_buffer_dirty(bh);
2011afddba49SNick Piggin 			}
2012afddba49SNick Piggin 		}
2013afddba49SNick Piggin 
2014afddba49SNick Piggin 		block_start = block_end;
2015afddba49SNick Piggin 		bh = bh->b_this_page;
2016afddba49SNick Piggin 	} while (bh != head);
2017afddba49SNick Piggin }
20184a9622f2SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_zero_new_buffers);
2019afddba49SNick Piggin 
20204aa8cdd5SChristoph Hellwig static int
2021ae259a9cSChristoph Hellwig iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
20226d49cc85SChristoph Hellwig 		const struct iomap *iomap)
2023ae259a9cSChristoph Hellwig {
2024ae259a9cSChristoph Hellwig 	loff_t offset = block << inode->i_blkbits;
2025ae259a9cSChristoph Hellwig 
2026ae259a9cSChristoph Hellwig 	bh->b_bdev = iomap->bdev;
2027ae259a9cSChristoph Hellwig 
2028ae259a9cSChristoph Hellwig 	/*
2029ae259a9cSChristoph Hellwig 	 * Block points to offset in file we need to map, iomap contains
2030ae259a9cSChristoph Hellwig 	 * the offset at which the map starts. If the map ends before the
2031ae259a9cSChristoph Hellwig 	 * current block, then do not map the buffer and let the caller
2032ae259a9cSChristoph Hellwig 	 * handle it.
2033ae259a9cSChristoph Hellwig 	 */
20344aa8cdd5SChristoph Hellwig 	if (offset >= iomap->offset + iomap->length)
20354aa8cdd5SChristoph Hellwig 		return -EIO;
2036ae259a9cSChristoph Hellwig 
2037ae259a9cSChristoph Hellwig 	switch (iomap->type) {
2038ae259a9cSChristoph Hellwig 	case IOMAP_HOLE:
2039ae259a9cSChristoph Hellwig 		/*
2040ae259a9cSChristoph Hellwig 		 * If the buffer is not up to date or beyond the current EOF,
2041ae259a9cSChristoph Hellwig 		 * we need to mark it as new to ensure sub-block zeroing is
2042ae259a9cSChristoph Hellwig 		 * executed if necessary.
2043ae259a9cSChristoph Hellwig 		 */
2044ae259a9cSChristoph Hellwig 		if (!buffer_uptodate(bh) ||
2045ae259a9cSChristoph Hellwig 		    (offset >= i_size_read(inode)))
2046ae259a9cSChristoph Hellwig 			set_buffer_new(bh);
20474aa8cdd5SChristoph Hellwig 		return 0;
2048ae259a9cSChristoph Hellwig 	case IOMAP_DELALLOC:
2049ae259a9cSChristoph Hellwig 		if (!buffer_uptodate(bh) ||
2050ae259a9cSChristoph Hellwig 		    (offset >= i_size_read(inode)))
2051ae259a9cSChristoph Hellwig 			set_buffer_new(bh);
2052ae259a9cSChristoph Hellwig 		set_buffer_uptodate(bh);
2053ae259a9cSChristoph Hellwig 		set_buffer_mapped(bh);
2054ae259a9cSChristoph Hellwig 		set_buffer_delay(bh);
20554aa8cdd5SChristoph Hellwig 		return 0;
2056ae259a9cSChristoph Hellwig 	case IOMAP_UNWRITTEN:
2057ae259a9cSChristoph Hellwig 		/*
20583d7b6b21SAndreas Gruenbacher 		 * For unwritten regions, we always need to ensure that regions
20593d7b6b21SAndreas Gruenbacher 		 * in the block we are not writing to are zeroed. Mark the
20603d7b6b21SAndreas Gruenbacher 		 * buffer as new to ensure this.
2061ae259a9cSChristoph Hellwig 		 */
2062ae259a9cSChristoph Hellwig 		set_buffer_new(bh);
2063ae259a9cSChristoph Hellwig 		set_buffer_unwritten(bh);
2064df561f66SGustavo A. R. Silva 		fallthrough;
2065ae259a9cSChristoph Hellwig 	case IOMAP_MAPPED:
20663d7b6b21SAndreas Gruenbacher 		if ((iomap->flags & IOMAP_F_NEW) ||
2067381c0432SChristoph Hellwig 		    offset >= i_size_read(inode)) {
2068381c0432SChristoph Hellwig 			/*
2069381c0432SChristoph Hellwig 			 * This can happen if truncating the block device races
2070381c0432SChristoph Hellwig 			 * with the check in the caller as i_size updates on
2071381c0432SChristoph Hellwig 			 * block devices aren't synchronized by i_rwsem for
2072381c0432SChristoph Hellwig 			 * block devices.
2073381c0432SChristoph Hellwig 			 */
2074381c0432SChristoph Hellwig 			if (S_ISBLK(inode->i_mode))
2075381c0432SChristoph Hellwig 				return -EIO;
2076ae259a9cSChristoph Hellwig 			set_buffer_new(bh);
2077381c0432SChristoph Hellwig 		}
207819fe5f64SAndreas Gruenbacher 		bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
207919fe5f64SAndreas Gruenbacher 				inode->i_blkbits;
2080ae259a9cSChristoph Hellwig 		set_buffer_mapped(bh);
20814aa8cdd5SChristoph Hellwig 		return 0;
20824aa8cdd5SChristoph Hellwig 	default:
20834aa8cdd5SChristoph Hellwig 		WARN_ON_ONCE(1);
20844aa8cdd5SChristoph Hellwig 		return -EIO;
2085ae259a9cSChristoph Hellwig 	}
2086ae259a9cSChristoph Hellwig }
2087ae259a9cSChristoph Hellwig 
2088d1bd0b4eSMatthew Wilcox (Oracle) int __block_write_begin_int(struct folio *folio, loff_t pos, unsigned len,
20896d49cc85SChristoph Hellwig 		get_block_t *get_block, const struct iomap *iomap)
20901da177e4SLinus Torvalds {
209109cbfeafSKirill A. Shutemov 	unsigned from = pos & (PAGE_SIZE - 1);
2092ebdec241SChristoph Hellwig 	unsigned to = from + len;
2093d1bd0b4eSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
20941da177e4SLinus Torvalds 	unsigned block_start, block_end;
20951da177e4SLinus Torvalds 	sector_t block;
20961da177e4SLinus Torvalds 	int err = 0;
20971da177e4SLinus Torvalds 	unsigned blocksize, bbits;
20981da177e4SLinus Torvalds 	struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
20991da177e4SLinus Torvalds 
2100d1bd0b4eSMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
210109cbfeafSKirill A. Shutemov 	BUG_ON(from > PAGE_SIZE);
210209cbfeafSKirill A. Shutemov 	BUG_ON(to > PAGE_SIZE);
21031da177e4SLinus Torvalds 	BUG_ON(from > to);
21041da177e4SLinus Torvalds 
2105c6c8c3e7SPankaj Raghav 	head = folio_create_buffers(folio, inode, 0);
210645bce8f3SLinus Torvalds 	blocksize = head->b_size;
210745bce8f3SLinus Torvalds 	bbits = block_size_bits(blocksize);
21081da177e4SLinus Torvalds 
2109d1bd0b4eSMatthew Wilcox (Oracle) 	block = (sector_t)folio->index << (PAGE_SHIFT - bbits);
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	for(bh = head, block_start = 0; bh != head || !block_start;
21121da177e4SLinus Torvalds 	    block++, block_start=block_end, bh = bh->b_this_page) {
21131da177e4SLinus Torvalds 		block_end = block_start + blocksize;
21141da177e4SLinus Torvalds 		if (block_end <= from || block_start >= to) {
2115d1bd0b4eSMatthew Wilcox (Oracle) 			if (folio_test_uptodate(folio)) {
21161da177e4SLinus Torvalds 				if (!buffer_uptodate(bh))
21171da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
21181da177e4SLinus Torvalds 			}
21191da177e4SLinus Torvalds 			continue;
21201da177e4SLinus Torvalds 		}
21211da177e4SLinus Torvalds 		if (buffer_new(bh))
21221da177e4SLinus Torvalds 			clear_buffer_new(bh);
21231da177e4SLinus Torvalds 		if (!buffer_mapped(bh)) {
2124b0cf2321SBadari Pulavarty 			WARN_ON(bh->b_size != blocksize);
21254aa8cdd5SChristoph Hellwig 			if (get_block)
21261da177e4SLinus Torvalds 				err = get_block(inode, block, bh, 1);
21274aa8cdd5SChristoph Hellwig 			else
21284aa8cdd5SChristoph Hellwig 				err = iomap_to_bh(inode, block, bh, iomap);
21291da177e4SLinus Torvalds 			if (err)
2130f3ddbdc6SNick Piggin 				break;
2131ae259a9cSChristoph Hellwig 
21321da177e4SLinus Torvalds 			if (buffer_new(bh)) {
2133e64855c6SJan Kara 				clean_bdev_bh_alias(bh);
2134d1bd0b4eSMatthew Wilcox (Oracle) 				if (folio_test_uptodate(folio)) {
2135637aff46SNick Piggin 					clear_buffer_new(bh);
21361da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
2137637aff46SNick Piggin 					mark_buffer_dirty(bh);
21381da177e4SLinus Torvalds 					continue;
21391da177e4SLinus Torvalds 				}
2140eebd2aa3SChristoph Lameter 				if (block_end > to || block_start < from)
2141d1bd0b4eSMatthew Wilcox (Oracle) 					folio_zero_segments(folio,
2142eebd2aa3SChristoph Lameter 						to, block_end,
2143eebd2aa3SChristoph Lameter 						block_start, from);
21441da177e4SLinus Torvalds 				continue;
21451da177e4SLinus Torvalds 			}
21461da177e4SLinus Torvalds 		}
2147d1bd0b4eSMatthew Wilcox (Oracle) 		if (folio_test_uptodate(folio)) {
21481da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
21491da177e4SLinus Torvalds 				set_buffer_uptodate(bh);
21501da177e4SLinus Torvalds 			continue;
21511da177e4SLinus Torvalds 		}
21521da177e4SLinus Torvalds 		if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
215333a266ddSDavid Chinner 		    !buffer_unwritten(bh) &&
21541da177e4SLinus Torvalds 		     (block_start < from || block_end > to)) {
2155e7ea1129SZhang Yi 			bh_read_nowait(bh, 0);
21561da177e4SLinus Torvalds 			*wait_bh++=bh;
21571da177e4SLinus Torvalds 		}
21581da177e4SLinus Torvalds 	}
21591da177e4SLinus Torvalds 	/*
21601da177e4SLinus Torvalds 	 * If we issued read requests - let them complete.
21611da177e4SLinus Torvalds 	 */
21621da177e4SLinus Torvalds 	while(wait_bh > wait) {
21631da177e4SLinus Torvalds 		wait_on_buffer(*--wait_bh);
21641da177e4SLinus Torvalds 		if (!buffer_uptodate(*wait_bh))
2165f3ddbdc6SNick Piggin 			err = -EIO;
21661da177e4SLinus Torvalds 	}
2167f9f07b6cSJan Kara 	if (unlikely(err))
21684a9622f2SMatthew Wilcox (Oracle) 		folio_zero_new_buffers(folio, from, to);
21691da177e4SLinus Torvalds 	return err;
21701da177e4SLinus Torvalds }
2171ae259a9cSChristoph Hellwig 
2172ae259a9cSChristoph Hellwig int __block_write_begin(struct page *page, loff_t pos, unsigned len,
2173ae259a9cSChristoph Hellwig 		get_block_t *get_block)
2174ae259a9cSChristoph Hellwig {
2175d1bd0b4eSMatthew Wilcox (Oracle) 	return __block_write_begin_int(page_folio(page), pos, len, get_block,
2176d1bd0b4eSMatthew Wilcox (Oracle) 				       NULL);
2177ae259a9cSChristoph Hellwig }
2178ebdec241SChristoph Hellwig EXPORT_SYMBOL(__block_write_begin);
21791da177e4SLinus Torvalds 
2180a524fcfeSBean Huo static void __block_commit_write(struct folio *folio, size_t from, size_t to)
21811da177e4SLinus Torvalds {
21828c6cb3e3SMatthew Wilcox (Oracle) 	size_t block_start, block_end;
21838c6cb3e3SMatthew Wilcox (Oracle) 	bool partial = false;
21841da177e4SLinus Torvalds 	unsigned blocksize;
21851da177e4SLinus Torvalds 	struct buffer_head *bh, *head;
21861da177e4SLinus Torvalds 
21878c6cb3e3SMatthew Wilcox (Oracle) 	bh = head = folio_buffers(folio);
218845bce8f3SLinus Torvalds 	blocksize = bh->b_size;
21891da177e4SLinus Torvalds 
219045bce8f3SLinus Torvalds 	block_start = 0;
219145bce8f3SLinus Torvalds 	do {
21921da177e4SLinus Torvalds 		block_end = block_start + blocksize;
21931da177e4SLinus Torvalds 		if (block_end <= from || block_start >= to) {
21941da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
21958c6cb3e3SMatthew Wilcox (Oracle) 				partial = true;
21961da177e4SLinus Torvalds 		} else {
21971da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
21981da177e4SLinus Torvalds 			mark_buffer_dirty(bh);
21991da177e4SLinus Torvalds 		}
22004ebd3aecSYang Guo 		if (buffer_new(bh))
2201afddba49SNick Piggin 			clear_buffer_new(bh);
220245bce8f3SLinus Torvalds 
220345bce8f3SLinus Torvalds 		block_start = block_end;
220445bce8f3SLinus Torvalds 		bh = bh->b_this_page;
220545bce8f3SLinus Torvalds 	} while (bh != head);
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	/*
22081da177e4SLinus Torvalds 	 * If this is a partial write which happened to make all buffers
22092c69e205SMatthew Wilcox (Oracle) 	 * uptodate then we can optimize away a bogus read_folio() for
22108c6cb3e3SMatthew Wilcox (Oracle) 	 * the next read(). Here we 'discover' whether the folio went
22111da177e4SLinus Torvalds 	 * uptodate as a result of this (potentially partial) write.
22121da177e4SLinus Torvalds 	 */
22131da177e4SLinus Torvalds 	if (!partial)
22148c6cb3e3SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds /*
2218155130a4SChristoph Hellwig  * block_write_begin takes care of the basic task of block allocation and
2219155130a4SChristoph Hellwig  * bringing partial write blocks uptodate first.
2220155130a4SChristoph Hellwig  *
22217bb46a67Snpiggin@suse.de  * The filesystem needs to handle block truncation upon failure.
2222afddba49SNick Piggin  */
2223155130a4SChristoph Hellwig int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
2224b3992d1eSMatthew Wilcox (Oracle) 		struct page **pagep, get_block_t *get_block)
2225afddba49SNick Piggin {
222609cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
2227afddba49SNick Piggin 	struct page *page;
22286e1db88dSChristoph Hellwig 	int status;
2229afddba49SNick Piggin 
2230b7446e7cSMatthew Wilcox (Oracle) 	page = grab_cache_page_write_begin(mapping, index);
22316e1db88dSChristoph Hellwig 	if (!page)
22326e1db88dSChristoph Hellwig 		return -ENOMEM;
2233afddba49SNick Piggin 
22346e1db88dSChristoph Hellwig 	status = __block_write_begin(page, pos, len, get_block);
2235afddba49SNick Piggin 	if (unlikely(status)) {
2236afddba49SNick Piggin 		unlock_page(page);
223709cbfeafSKirill A. Shutemov 		put_page(page);
22386e1db88dSChristoph Hellwig 		page = NULL;
2239afddba49SNick Piggin 	}
2240afddba49SNick Piggin 
22416e1db88dSChristoph Hellwig 	*pagep = page;
2242afddba49SNick Piggin 	return status;
2243afddba49SNick Piggin }
2244afddba49SNick Piggin EXPORT_SYMBOL(block_write_begin);
2245afddba49SNick Piggin 
2246afddba49SNick Piggin int block_write_end(struct file *file, struct address_space *mapping,
2247afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
2248afddba49SNick Piggin 			struct page *page, void *fsdata)
2249afddba49SNick Piggin {
22508c6cb3e3SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
22518c6cb3e3SMatthew Wilcox (Oracle) 	size_t start = pos - folio_pos(folio);
2252afddba49SNick Piggin 
2253afddba49SNick Piggin 	if (unlikely(copied < len)) {
2254afddba49SNick Piggin 		/*
22552c69e205SMatthew Wilcox (Oracle) 		 * The buffers that were written will now be uptodate, so
22562c69e205SMatthew Wilcox (Oracle) 		 * we don't have to worry about a read_folio reading them
22572c69e205SMatthew Wilcox (Oracle) 		 * and overwriting a partial write. However if we have
22582c69e205SMatthew Wilcox (Oracle) 		 * encountered a short write and only partially written
22592c69e205SMatthew Wilcox (Oracle) 		 * into a buffer, it will not be marked uptodate, so a
22602c69e205SMatthew Wilcox (Oracle) 		 * read_folio might come in and destroy our partial write.
2261afddba49SNick Piggin 		 *
2262afddba49SNick Piggin 		 * Do the simplest thing, and just treat any short write to a
22638c6cb3e3SMatthew Wilcox (Oracle) 		 * non uptodate folio as a zero-length write, and force the
2264afddba49SNick Piggin 		 * caller to redo the whole thing.
2265afddba49SNick Piggin 		 */
22668c6cb3e3SMatthew Wilcox (Oracle) 		if (!folio_test_uptodate(folio))
2267afddba49SNick Piggin 			copied = 0;
2268afddba49SNick Piggin 
22694a9622f2SMatthew Wilcox (Oracle) 		folio_zero_new_buffers(folio, start+copied, start+len);
2270afddba49SNick Piggin 	}
22718c6cb3e3SMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
2272afddba49SNick Piggin 
2273afddba49SNick Piggin 	/* This could be a short (even 0-length) commit */
2274489b7e72SBean Huo 	__block_commit_write(folio, start, start + copied);
2275afddba49SNick Piggin 
2276afddba49SNick Piggin 	return copied;
2277afddba49SNick Piggin }
2278afddba49SNick Piggin EXPORT_SYMBOL(block_write_end);
2279afddba49SNick Piggin 
2280afddba49SNick Piggin int generic_write_end(struct file *file, struct address_space *mapping,
2281afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
2282afddba49SNick Piggin 			struct page *page, void *fsdata)
2283afddba49SNick Piggin {
22848af54f29SChristoph Hellwig 	struct inode *inode = mapping->host;
22858af54f29SChristoph Hellwig 	loff_t old_size = inode->i_size;
22868af54f29SChristoph Hellwig 	bool i_size_changed = false;
22878af54f29SChristoph Hellwig 
2288afddba49SNick Piggin 	copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
22898af54f29SChristoph Hellwig 
22908af54f29SChristoph Hellwig 	/*
22918af54f29SChristoph Hellwig 	 * No need to use i_size_read() here, the i_size cannot change under us
22928af54f29SChristoph Hellwig 	 * because we hold i_rwsem.
22938af54f29SChristoph Hellwig 	 *
22948af54f29SChristoph Hellwig 	 * But it's important to update i_size while still holding page lock:
22958af54f29SChristoph Hellwig 	 * page writeout could otherwise come in and zero beyond i_size.
22968af54f29SChristoph Hellwig 	 */
22978af54f29SChristoph Hellwig 	if (pos + copied > inode->i_size) {
22988af54f29SChristoph Hellwig 		i_size_write(inode, pos + copied);
22998af54f29SChristoph Hellwig 		i_size_changed = true;
23008af54f29SChristoph Hellwig 	}
23018af54f29SChristoph Hellwig 
23028af54f29SChristoph Hellwig 	unlock_page(page);
23037a77dad7SAndreas Gruenbacher 	put_page(page);
23048af54f29SChristoph Hellwig 
23058af54f29SChristoph Hellwig 	if (old_size < pos)
23068af54f29SChristoph Hellwig 		pagecache_isize_extended(inode, old_size, pos);
23078af54f29SChristoph Hellwig 	/*
23088af54f29SChristoph Hellwig 	 * Don't mark the inode dirty under page lock. First, it unnecessarily
23098af54f29SChristoph Hellwig 	 * makes the holding time of page lock longer. Second, it forces lock
23108af54f29SChristoph Hellwig 	 * ordering of page lock and transaction start for journaling
23118af54f29SChristoph Hellwig 	 * filesystems.
23128af54f29SChristoph Hellwig 	 */
23138af54f29SChristoph Hellwig 	if (i_size_changed)
23148af54f29SChristoph Hellwig 		mark_inode_dirty(inode);
231526ddb1f4SAndreas Gruenbacher 	return copied;
2316afddba49SNick Piggin }
2317afddba49SNick Piggin EXPORT_SYMBOL(generic_write_end);
2318afddba49SNick Piggin 
2319afddba49SNick Piggin /*
23202e7e80f7SMatthew Wilcox (Oracle)  * block_is_partially_uptodate checks whether buffers within a folio are
23218ab22b9aSHisashi Hifumi  * uptodate or not.
23228ab22b9aSHisashi Hifumi  *
23232e7e80f7SMatthew Wilcox (Oracle)  * Returns true if all buffers which correspond to the specified part
23242e7e80f7SMatthew Wilcox (Oracle)  * of the folio are uptodate.
23258ab22b9aSHisashi Hifumi  */
23262e7e80f7SMatthew Wilcox (Oracle) bool block_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
23278ab22b9aSHisashi Hifumi {
23288ab22b9aSHisashi Hifumi 	unsigned block_start, block_end, blocksize;
23298ab22b9aSHisashi Hifumi 	unsigned to;
23308ab22b9aSHisashi Hifumi 	struct buffer_head *bh, *head;
23312e7e80f7SMatthew Wilcox (Oracle) 	bool ret = true;
23328ab22b9aSHisashi Hifumi 
23332e7e80f7SMatthew Wilcox (Oracle) 	head = folio_buffers(folio);
23342e7e80f7SMatthew Wilcox (Oracle) 	if (!head)
23352e7e80f7SMatthew Wilcox (Oracle) 		return false;
233645bce8f3SLinus Torvalds 	blocksize = head->b_size;
23372e7e80f7SMatthew Wilcox (Oracle) 	to = min_t(unsigned, folio_size(folio) - from, count);
23388ab22b9aSHisashi Hifumi 	to = from + to;
23392e7e80f7SMatthew Wilcox (Oracle) 	if (from < blocksize && to > folio_size(folio) - blocksize)
23402e7e80f7SMatthew Wilcox (Oracle) 		return false;
23418ab22b9aSHisashi Hifumi 
23428ab22b9aSHisashi Hifumi 	bh = head;
23438ab22b9aSHisashi Hifumi 	block_start = 0;
23448ab22b9aSHisashi Hifumi 	do {
23458ab22b9aSHisashi Hifumi 		block_end = block_start + blocksize;
23468ab22b9aSHisashi Hifumi 		if (block_end > from && block_start < to) {
23478ab22b9aSHisashi Hifumi 			if (!buffer_uptodate(bh)) {
23482e7e80f7SMatthew Wilcox (Oracle) 				ret = false;
23498ab22b9aSHisashi Hifumi 				break;
23508ab22b9aSHisashi Hifumi 			}
23518ab22b9aSHisashi Hifumi 			if (block_end >= to)
23528ab22b9aSHisashi Hifumi 				break;
23538ab22b9aSHisashi Hifumi 		}
23548ab22b9aSHisashi Hifumi 		block_start = block_end;
23558ab22b9aSHisashi Hifumi 		bh = bh->b_this_page;
23568ab22b9aSHisashi Hifumi 	} while (bh != head);
23578ab22b9aSHisashi Hifumi 
23588ab22b9aSHisashi Hifumi 	return ret;
23598ab22b9aSHisashi Hifumi }
23608ab22b9aSHisashi Hifumi EXPORT_SYMBOL(block_is_partially_uptodate);
23618ab22b9aSHisashi Hifumi 
23628ab22b9aSHisashi Hifumi /*
23632c69e205SMatthew Wilcox (Oracle)  * Generic "read_folio" function for block devices that have the normal
23641da177e4SLinus Torvalds  * get_block functionality. This is most of the block device filesystems.
23652c69e205SMatthew Wilcox (Oracle)  * Reads the folio asynchronously --- the unlock_buffer() and
23661da177e4SLinus Torvalds  * set/clear_buffer_uptodate() functions propagate buffer state into the
23672c69e205SMatthew Wilcox (Oracle)  * folio once IO has completed.
23681da177e4SLinus Torvalds  */
23692c69e205SMatthew Wilcox (Oracle) int block_read_full_folio(struct folio *folio, get_block_t *get_block)
23701da177e4SLinus Torvalds {
23712c69e205SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
23721da177e4SLinus Torvalds 	sector_t iblock, lblock;
23731da177e4SLinus Torvalds 	struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
237445bce8f3SLinus Torvalds 	unsigned int blocksize, bbits;
23751da177e4SLinus Torvalds 	int nr, i;
23761da177e4SLinus Torvalds 	int fully_mapped = 1;
2377b7a6eb22SMatthew Wilcox (Oracle) 	bool page_error = false;
23784fa512ceSEric Biggers 	loff_t limit = i_size_read(inode);
23794fa512ceSEric Biggers 
23804fa512ceSEric Biggers 	/* This is needed for ext4. */
23814fa512ceSEric Biggers 	if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
23824fa512ceSEric Biggers 		limit = inode->i_sb->s_maxbytes;
23831da177e4SLinus Torvalds 
23842c69e205SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_large(folio), folio);
23852c69e205SMatthew Wilcox (Oracle) 
2386c6c8c3e7SPankaj Raghav 	head = folio_create_buffers(folio, inode, 0);
238745bce8f3SLinus Torvalds 	blocksize = head->b_size;
238845bce8f3SLinus Torvalds 	bbits = block_size_bits(blocksize);
23891da177e4SLinus Torvalds 
23902c69e205SMatthew Wilcox (Oracle) 	iblock = (sector_t)folio->index << (PAGE_SHIFT - bbits);
23914fa512ceSEric Biggers 	lblock = (limit+blocksize-1) >> bbits;
23921da177e4SLinus Torvalds 	bh = head;
23931da177e4SLinus Torvalds 	nr = 0;
23941da177e4SLinus Torvalds 	i = 0;
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	do {
23971da177e4SLinus Torvalds 		if (buffer_uptodate(bh))
23981da177e4SLinus Torvalds 			continue;
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 		if (!buffer_mapped(bh)) {
2401c64610baSAndrew Morton 			int err = 0;
2402c64610baSAndrew Morton 
24031da177e4SLinus Torvalds 			fully_mapped = 0;
24041da177e4SLinus Torvalds 			if (iblock < lblock) {
2405b0cf2321SBadari Pulavarty 				WARN_ON(bh->b_size != blocksize);
2406c64610baSAndrew Morton 				err = get_block(inode, iblock, bh, 0);
2407b7a6eb22SMatthew Wilcox (Oracle) 				if (err) {
24082c69e205SMatthew Wilcox (Oracle) 					folio_set_error(folio);
2409b7a6eb22SMatthew Wilcox (Oracle) 					page_error = true;
2410b7a6eb22SMatthew Wilcox (Oracle) 				}
24111da177e4SLinus Torvalds 			}
24121da177e4SLinus Torvalds 			if (!buffer_mapped(bh)) {
24132c69e205SMatthew Wilcox (Oracle) 				folio_zero_range(folio, i * blocksize,
24142c69e205SMatthew Wilcox (Oracle) 						blocksize);
2415c64610baSAndrew Morton 				if (!err)
24161da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
24171da177e4SLinus Torvalds 				continue;
24181da177e4SLinus Torvalds 			}
24191da177e4SLinus Torvalds 			/*
24201da177e4SLinus Torvalds 			 * get_block() might have updated the buffer
24211da177e4SLinus Torvalds 			 * synchronously
24221da177e4SLinus Torvalds 			 */
24231da177e4SLinus Torvalds 			if (buffer_uptodate(bh))
24241da177e4SLinus Torvalds 				continue;
24251da177e4SLinus Torvalds 		}
24261da177e4SLinus Torvalds 		arr[nr++] = bh;
24271da177e4SLinus Torvalds 	} while (i++, iblock++, (bh = bh->b_this_page) != head);
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	if (fully_mapped)
24302c69e205SMatthew Wilcox (Oracle) 		folio_set_mappedtodisk(folio);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	if (!nr) {
24331da177e4SLinus Torvalds 		/*
24346ba924d3SMatthew Wilcox (Oracle) 		 * All buffers are uptodate or get_block() returned an
24356ba924d3SMatthew Wilcox (Oracle) 		 * error when trying to map them - we can finish the read.
24361da177e4SLinus Torvalds 		 */
24376ba924d3SMatthew Wilcox (Oracle) 		folio_end_read(folio, !page_error);
24381da177e4SLinus Torvalds 		return 0;
24391da177e4SLinus Torvalds 	}
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	/* Stage two: lock the buffers */
24421da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
24431da177e4SLinus Torvalds 		bh = arr[i];
24441da177e4SLinus Torvalds 		lock_buffer(bh);
24451da177e4SLinus Torvalds 		mark_buffer_async_read(bh);
24461da177e4SLinus Torvalds 	}
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	/*
24491da177e4SLinus Torvalds 	 * Stage 3: start the IO.  Check for uptodateness
24501da177e4SLinus Torvalds 	 * inside the buffer lock in case another process reading
24511da177e4SLinus Torvalds 	 * the underlying blockdev brought it uptodate (the sct fix).
24521da177e4SLinus Torvalds 	 */
24531da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
24541da177e4SLinus Torvalds 		bh = arr[i];
24551da177e4SLinus Torvalds 		if (buffer_uptodate(bh))
24561da177e4SLinus Torvalds 			end_buffer_async_read(bh, 1);
24571da177e4SLinus Torvalds 		else
24581420c4a5SBart Van Assche 			submit_bh(REQ_OP_READ, bh);
24591da177e4SLinus Torvalds 	}
24601da177e4SLinus Torvalds 	return 0;
24611da177e4SLinus Torvalds }
24622c69e205SMatthew Wilcox (Oracle) EXPORT_SYMBOL(block_read_full_folio);
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds /* utility function for filesystems that need to do work on expanding
246589e10787SNick Piggin  * truncates.  Uses filesystem pagecache writes to allow the filesystem to
24661da177e4SLinus Torvalds  * deal with the hole.
24671da177e4SLinus Torvalds  */
246889e10787SNick Piggin int generic_cont_expand_simple(struct inode *inode, loff_t size)
24691da177e4SLinus Torvalds {
24701da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
247153b524b8SMatthew Wilcox (Oracle) 	const struct address_space_operations *aops = mapping->a_ops;
24721da177e4SLinus Torvalds 	struct page *page;
24731468c6f4SAlexander Potapenko 	void *fsdata = NULL;
24741da177e4SLinus Torvalds 	int err;
24751da177e4SLinus Torvalds 
2476c08d3b0eSnpiggin@suse.de 	err = inode_newsize_ok(inode, size);
2477c08d3b0eSnpiggin@suse.de 	if (err)
24781da177e4SLinus Torvalds 		goto out;
24791da177e4SLinus Torvalds 
248053b524b8SMatthew Wilcox (Oracle) 	err = aops->write_begin(NULL, mapping, size, 0, &page, &fsdata);
248189e10787SNick Piggin 	if (err)
248205eb0b51SOGAWA Hirofumi 		goto out;
248305eb0b51SOGAWA Hirofumi 
248453b524b8SMatthew Wilcox (Oracle) 	err = aops->write_end(NULL, mapping, size, 0, 0, page, fsdata);
248589e10787SNick Piggin 	BUG_ON(err > 0);
248605eb0b51SOGAWA Hirofumi 
248705eb0b51SOGAWA Hirofumi out:
248805eb0b51SOGAWA Hirofumi 	return err;
248905eb0b51SOGAWA Hirofumi }
24901fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(generic_cont_expand_simple);
249105eb0b51SOGAWA Hirofumi 
2492f1e3af72SAdrian Bunk static int cont_expand_zero(struct file *file, struct address_space *mapping,
249389e10787SNick Piggin 			    loff_t pos, loff_t *bytes)
249405eb0b51SOGAWA Hirofumi {
249589e10787SNick Piggin 	struct inode *inode = mapping->host;
249653b524b8SMatthew Wilcox (Oracle) 	const struct address_space_operations *aops = mapping->a_ops;
249793407472SFabian Frederick 	unsigned int blocksize = i_blocksize(inode);
249889e10787SNick Piggin 	struct page *page;
24991468c6f4SAlexander Potapenko 	void *fsdata = NULL;
250089e10787SNick Piggin 	pgoff_t index, curidx;
250189e10787SNick Piggin 	loff_t curpos;
250289e10787SNick Piggin 	unsigned zerofrom, offset, len;
250389e10787SNick Piggin 	int err = 0;
250405eb0b51SOGAWA Hirofumi 
250509cbfeafSKirill A. Shutemov 	index = pos >> PAGE_SHIFT;
250609cbfeafSKirill A. Shutemov 	offset = pos & ~PAGE_MASK;
250789e10787SNick Piggin 
250809cbfeafSKirill A. Shutemov 	while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
250909cbfeafSKirill A. Shutemov 		zerofrom = curpos & ~PAGE_MASK;
251089e10787SNick Piggin 		if (zerofrom & (blocksize-1)) {
251189e10787SNick Piggin 			*bytes |= (blocksize-1);
251289e10787SNick Piggin 			(*bytes)++;
251389e10787SNick Piggin 		}
251409cbfeafSKirill A. Shutemov 		len = PAGE_SIZE - zerofrom;
251589e10787SNick Piggin 
251653b524b8SMatthew Wilcox (Oracle) 		err = aops->write_begin(file, mapping, curpos, len,
251789e10787SNick Piggin 					    &page, &fsdata);
251889e10787SNick Piggin 		if (err)
251989e10787SNick Piggin 			goto out;
2520eebd2aa3SChristoph Lameter 		zero_user(page, zerofrom, len);
252153b524b8SMatthew Wilcox (Oracle) 		err = aops->write_end(file, mapping, curpos, len, len,
252289e10787SNick Piggin 						page, fsdata);
252389e10787SNick Piggin 		if (err < 0)
252489e10787SNick Piggin 			goto out;
252589e10787SNick Piggin 		BUG_ON(err != len);
252689e10787SNick Piggin 		err = 0;
2527061e9746SOGAWA Hirofumi 
2528061e9746SOGAWA Hirofumi 		balance_dirty_pages_ratelimited(mapping);
2529c2ca0fcdSMikulas Patocka 
253008d405c8SDavidlohr Bueso 		if (fatal_signal_pending(current)) {
2531c2ca0fcdSMikulas Patocka 			err = -EINTR;
2532c2ca0fcdSMikulas Patocka 			goto out;
2533c2ca0fcdSMikulas Patocka 		}
253489e10787SNick Piggin 	}
253589e10787SNick Piggin 
253689e10787SNick Piggin 	/* page covers the boundary, find the boundary offset */
253789e10787SNick Piggin 	if (index == curidx) {
253809cbfeafSKirill A. Shutemov 		zerofrom = curpos & ~PAGE_MASK;
253989e10787SNick Piggin 		/* if we will expand the thing last block will be filled */
254089e10787SNick Piggin 		if (offset <= zerofrom) {
254189e10787SNick Piggin 			goto out;
254289e10787SNick Piggin 		}
254389e10787SNick Piggin 		if (zerofrom & (blocksize-1)) {
254489e10787SNick Piggin 			*bytes |= (blocksize-1);
254589e10787SNick Piggin 			(*bytes)++;
254689e10787SNick Piggin 		}
254789e10787SNick Piggin 		len = offset - zerofrom;
254889e10787SNick Piggin 
254953b524b8SMatthew Wilcox (Oracle) 		err = aops->write_begin(file, mapping, curpos, len,
255089e10787SNick Piggin 					    &page, &fsdata);
255189e10787SNick Piggin 		if (err)
255289e10787SNick Piggin 			goto out;
2553eebd2aa3SChristoph Lameter 		zero_user(page, zerofrom, len);
255453b524b8SMatthew Wilcox (Oracle) 		err = aops->write_end(file, mapping, curpos, len, len,
255589e10787SNick Piggin 						page, fsdata);
255689e10787SNick Piggin 		if (err < 0)
255789e10787SNick Piggin 			goto out;
255889e10787SNick Piggin 		BUG_ON(err != len);
255989e10787SNick Piggin 		err = 0;
256089e10787SNick Piggin 	}
256189e10787SNick Piggin out:
256289e10787SNick Piggin 	return err;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds /*
25661da177e4SLinus Torvalds  * For moronic filesystems that do not allow holes in file.
25671da177e4SLinus Torvalds  * We may have to extend the file.
25681da177e4SLinus Torvalds  */
2569282dc178SChristoph Hellwig int cont_write_begin(struct file *file, struct address_space *mapping,
2570be3bbbc5SMatthew Wilcox (Oracle) 			loff_t pos, unsigned len,
257189e10787SNick Piggin 			struct page **pagep, void **fsdata,
257289e10787SNick Piggin 			get_block_t *get_block, loff_t *bytes)
25731da177e4SLinus Torvalds {
25741da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
257593407472SFabian Frederick 	unsigned int blocksize = i_blocksize(inode);
257693407472SFabian Frederick 	unsigned int zerofrom;
257789e10787SNick Piggin 	int err;
25781da177e4SLinus Torvalds 
257989e10787SNick Piggin 	err = cont_expand_zero(file, mapping, pos, bytes);
258089e10787SNick Piggin 	if (err)
2581155130a4SChristoph Hellwig 		return err;
25821da177e4SLinus Torvalds 
258309cbfeafSKirill A. Shutemov 	zerofrom = *bytes & ~PAGE_MASK;
258489e10787SNick Piggin 	if (pos+len > *bytes && zerofrom & (blocksize-1)) {
25851da177e4SLinus Torvalds 		*bytes |= (blocksize-1);
25861da177e4SLinus Torvalds 		(*bytes)++;
25871da177e4SLinus Torvalds 	}
25881da177e4SLinus Torvalds 
2589b3992d1eSMatthew Wilcox (Oracle) 	return block_write_begin(mapping, pos, len, pagep, get_block);
25901da177e4SLinus Torvalds }
25911fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(cont_write_begin);
25921da177e4SLinus Torvalds 
2593a524fcfeSBean Huo void block_commit_write(struct page *page, unsigned from, unsigned to)
25941da177e4SLinus Torvalds {
25958c6cb3e3SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
2596489b7e72SBean Huo 	__block_commit_write(folio, from, to);
25971da177e4SLinus Torvalds }
25981fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_commit_write);
25991da177e4SLinus Torvalds 
260054171690SDavid Chinner /*
260154171690SDavid Chinner  * block_page_mkwrite() is not allowed to change the file size as it gets
260254171690SDavid Chinner  * called from a page fault handler when a page is first dirtied. Hence we must
260354171690SDavid Chinner  * be careful to check for EOF conditions here. We set the page up correctly
260454171690SDavid Chinner  * for a written page which means we get ENOSPC checking when writing into
260554171690SDavid Chinner  * holes and correct delalloc and unwritten extent mapping on filesystems that
260654171690SDavid Chinner  * support these features.
260754171690SDavid Chinner  *
260854171690SDavid Chinner  * We are not allowed to take the i_mutex here so we have to play games to
260954171690SDavid Chinner  * protect against truncate races as the page could now be beyond EOF.  Because
26107bb46a67Snpiggin@suse.de  * truncate writes the inode size before removing pages, once we have the
261154171690SDavid Chinner  * page lock we can determine safely if the page is beyond EOF. If it is not
261254171690SDavid Chinner  * beyond EOF, then the page is guaranteed safe against truncation until we
261354171690SDavid Chinner  * unlock the page.
2614ea13a864SJan Kara  *
261514da9200SJan Kara  * Direct callers of this function should protect against filesystem freezing
26165c500029SRoss Zwisler  * using sb_start_pagefault() - sb_end_pagefault() functions.
261754171690SDavid Chinner  */
26185c500029SRoss Zwisler int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
261954171690SDavid Chinner 			 get_block_t get_block)
262054171690SDavid Chinner {
2621fe181377SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
2622496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
262354171690SDavid Chinner 	unsigned long end;
262454171690SDavid Chinner 	loff_t size;
262524da4fabSJan Kara 	int ret;
262654171690SDavid Chinner 
2627fe181377SMatthew Wilcox (Oracle) 	folio_lock(folio);
262854171690SDavid Chinner 	size = i_size_read(inode);
2629fe181377SMatthew Wilcox (Oracle) 	if ((folio->mapping != inode->i_mapping) ||
2630fe181377SMatthew Wilcox (Oracle) 	    (folio_pos(folio) >= size)) {
263124da4fabSJan Kara 		/* We overload EFAULT to mean page got truncated */
263224da4fabSJan Kara 		ret = -EFAULT;
263324da4fabSJan Kara 		goto out_unlock;
263454171690SDavid Chinner 	}
263554171690SDavid Chinner 
2636fe181377SMatthew Wilcox (Oracle) 	end = folio_size(folio);
2637fe181377SMatthew Wilcox (Oracle) 	/* folio is wholly or partially inside EOF */
2638fe181377SMatthew Wilcox (Oracle) 	if (folio_pos(folio) + end > size)
2639fe181377SMatthew Wilcox (Oracle) 		end = size - folio_pos(folio);
264054171690SDavid Chinner 
2641fe181377SMatthew Wilcox (Oracle) 	ret = __block_write_begin_int(folio, 0, end, get_block, NULL);
2642a524fcfeSBean Huo 	if (unlikely(ret))
264324da4fabSJan Kara 		goto out_unlock;
2644a524fcfeSBean Huo 
2645a524fcfeSBean Huo 	__block_commit_write(folio, 0, end);
2646a524fcfeSBean Huo 
2647fe181377SMatthew Wilcox (Oracle) 	folio_mark_dirty(folio);
2648fe181377SMatthew Wilcox (Oracle) 	folio_wait_stable(folio);
264924da4fabSJan Kara 	return 0;
265024da4fabSJan Kara out_unlock:
2651fe181377SMatthew Wilcox (Oracle) 	folio_unlock(folio);
265254171690SDavid Chinner 	return ret;
265354171690SDavid Chinner }
26541fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_page_mkwrite);
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds int block_truncate_page(struct address_space *mapping,
26571da177e4SLinus Torvalds 			loff_t from, get_block_t *get_block)
26581da177e4SLinus Torvalds {
265909cbfeafSKirill A. Shutemov 	pgoff_t index = from >> PAGE_SHIFT;
26601da177e4SLinus Torvalds 	unsigned blocksize;
266154b21a79SAndrew Morton 	sector_t iblock;
26626d68f644SMatthew Wilcox (Oracle) 	size_t offset, length, pos;
26631da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
26646d68f644SMatthew Wilcox (Oracle) 	struct folio *folio;
26651da177e4SLinus Torvalds 	struct buffer_head *bh;
2666dc7cb2d2SJiapeng Chong 	int err = 0;
26671da177e4SLinus Torvalds 
266893407472SFabian Frederick 	blocksize = i_blocksize(inode);
26696d68f644SMatthew Wilcox (Oracle) 	length = from & (blocksize - 1);
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	/* Block boundary? Nothing to do */
26721da177e4SLinus Torvalds 	if (!length)
26731da177e4SLinus Torvalds 		return 0;
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds 	length = blocksize - length;
267609cbfeafSKirill A. Shutemov 	iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
26771da177e4SLinus Torvalds 
26786d68f644SMatthew Wilcox (Oracle) 	folio = filemap_grab_folio(mapping, index);
26796d68f644SMatthew Wilcox (Oracle) 	if (IS_ERR(folio))
26806d68f644SMatthew Wilcox (Oracle) 		return PTR_ERR(folio);
26811da177e4SLinus Torvalds 
26826d68f644SMatthew Wilcox (Oracle) 	bh = folio_buffers(folio);
2683*3decb856SMatthew Wilcox (Oracle) 	if (!bh)
2684*3decb856SMatthew Wilcox (Oracle) 		bh = folio_create_empty_buffers(folio, blocksize, 0);
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 	/* Find the buffer that contains "offset" */
26876d68f644SMatthew Wilcox (Oracle) 	offset = offset_in_folio(folio, from);
26881da177e4SLinus Torvalds 	pos = blocksize;
26891da177e4SLinus Torvalds 	while (offset >= pos) {
26901da177e4SLinus Torvalds 		bh = bh->b_this_page;
26911da177e4SLinus Torvalds 		iblock++;
26921da177e4SLinus Torvalds 		pos += blocksize;
26931da177e4SLinus Torvalds 	}
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds 	if (!buffer_mapped(bh)) {
2696b0cf2321SBadari Pulavarty 		WARN_ON(bh->b_size != blocksize);
26971da177e4SLinus Torvalds 		err = get_block(inode, iblock, bh, 0);
26981da177e4SLinus Torvalds 		if (err)
26991da177e4SLinus Torvalds 			goto unlock;
27001da177e4SLinus Torvalds 		/* unmapped? It's a hole - nothing to do */
27011da177e4SLinus Torvalds 		if (!buffer_mapped(bh))
27021da177e4SLinus Torvalds 			goto unlock;
27031da177e4SLinus Torvalds 	}
27041da177e4SLinus Torvalds 
27051da177e4SLinus Torvalds 	/* Ok, it's mapped. Make sure it's up-to-date */
27066d68f644SMatthew Wilcox (Oracle) 	if (folio_test_uptodate(folio))
27071da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
27081da177e4SLinus Torvalds 
270933a266ddSDavid Chinner 	if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
2710e7ea1129SZhang Yi 		err = bh_read(bh, 0);
27111da177e4SLinus Torvalds 		/* Uhhuh. Read error. Complain and punt. */
2712e7ea1129SZhang Yi 		if (err < 0)
27131da177e4SLinus Torvalds 			goto unlock;
27141da177e4SLinus Torvalds 	}
27151da177e4SLinus Torvalds 
27166d68f644SMatthew Wilcox (Oracle) 	folio_zero_range(folio, offset, length);
27171da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds unlock:
27206d68f644SMatthew Wilcox (Oracle) 	folio_unlock(folio);
27216d68f644SMatthew Wilcox (Oracle) 	folio_put(folio);
2722dc7cb2d2SJiapeng Chong 
27231da177e4SLinus Torvalds 	return err;
27241da177e4SLinus Torvalds }
27251fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_truncate_page);
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds /*
27281da177e4SLinus Torvalds  * The generic ->writepage function for buffer-backed address_spaces
27291da177e4SLinus Torvalds  */
27301b938c08SMatthew Wilcox int block_write_full_page(struct page *page, get_block_t *get_block,
27311b938c08SMatthew Wilcox 			struct writeback_control *wbc)
27321da177e4SLinus Torvalds {
273353418a18SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
2734bb0ea598SMatthew Wilcox (Oracle) 	struct inode * const inode = folio->mapping->host;
27351da177e4SLinus Torvalds 	loff_t i_size = i_size_read(inode);
27361da177e4SLinus Torvalds 
2737bb0ea598SMatthew Wilcox (Oracle) 	/* Is the folio fully inside i_size? */
2738bb0ea598SMatthew Wilcox (Oracle) 	if (folio_pos(folio) + folio_size(folio) <= i_size)
273953418a18SMatthew Wilcox (Oracle) 		return __block_write_full_folio(inode, folio, get_block, wbc,
27401b938c08SMatthew Wilcox 					       end_buffer_async_write);
27411da177e4SLinus Torvalds 
2742bb0ea598SMatthew Wilcox (Oracle) 	/* Is the folio fully outside i_size? (truncate in progress) */
2743bb0ea598SMatthew Wilcox (Oracle) 	if (folio_pos(folio) >= i_size) {
274453418a18SMatthew Wilcox (Oracle) 		folio_unlock(folio);
27451da177e4SLinus Torvalds 		return 0; /* don't care */
27461da177e4SLinus Torvalds 	}
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	/*
2749bb0ea598SMatthew Wilcox (Oracle) 	 * The folio straddles i_size.  It must be zeroed out on each and every
27502a61aa40SAdam Buchbinder 	 * writepage invocation because it may be mmapped.  "A file is mapped
27511da177e4SLinus Torvalds 	 * in multiples of the page size.  For a file that is not a multiple of
27521da177e4SLinus Torvalds 	 * the page size, the remaining memory is zeroed when mapped, and
27531da177e4SLinus Torvalds 	 * writes to that region are not written out to the file."
27541da177e4SLinus Torvalds 	 */
2755bb0ea598SMatthew Wilcox (Oracle) 	folio_zero_segment(folio, offset_in_folio(folio, i_size),
2756bb0ea598SMatthew Wilcox (Oracle) 			folio_size(folio));
275753418a18SMatthew Wilcox (Oracle) 	return __block_write_full_folio(inode, folio, get_block, wbc,
275835c80d5fSChris Mason 			end_buffer_async_write);
275935c80d5fSChris Mason }
27601fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_write_full_page);
276135c80d5fSChris Mason 
27621da177e4SLinus Torvalds sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
27631da177e4SLinus Torvalds 			    get_block_t *get_block)
27641da177e4SLinus Torvalds {
27651da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
27662a527d68SAlexander Potapenko 	struct buffer_head tmp = {
27672a527d68SAlexander Potapenko 		.b_size = i_blocksize(inode),
27682a527d68SAlexander Potapenko 	};
27692a527d68SAlexander Potapenko 
27701da177e4SLinus Torvalds 	get_block(inode, block, &tmp, 0);
27711da177e4SLinus Torvalds 	return tmp.b_blocknr;
27721da177e4SLinus Torvalds }
27731fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(generic_block_bmap);
27741da177e4SLinus Torvalds 
27754246a0b6SChristoph Hellwig static void end_bio_bh_io_sync(struct bio *bio)
27761da177e4SLinus Torvalds {
27771da177e4SLinus Torvalds 	struct buffer_head *bh = bio->bi_private;
27781da177e4SLinus Torvalds 
2779b7c44ed9SJens Axboe 	if (unlikely(bio_flagged(bio, BIO_QUIET)))
278008bafc03SKeith Mannthey 		set_bit(BH_Quiet, &bh->b_state);
278108bafc03SKeith Mannthey 
27824e4cbee9SChristoph Hellwig 	bh->b_end_io(bh, !bio->bi_status);
27831da177e4SLinus Torvalds 	bio_put(bio);
27841da177e4SLinus Torvalds }
27851da177e4SLinus Torvalds 
27865bdf402aSRitesh Harjani (IBM) static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
27871420c4a5SBart Van Assche 			  struct writeback_control *wbc)
27881da177e4SLinus Torvalds {
27891420c4a5SBart Van Assche 	const enum req_op op = opf & REQ_OP_MASK;
27901da177e4SLinus Torvalds 	struct bio *bio;
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds 	BUG_ON(!buffer_locked(bh));
27931da177e4SLinus Torvalds 	BUG_ON(!buffer_mapped(bh));
27941da177e4SLinus Torvalds 	BUG_ON(!bh->b_end_io);
27958fb0e342SAneesh Kumar K.V 	BUG_ON(buffer_delay(bh));
27968fb0e342SAneesh Kumar K.V 	BUG_ON(buffer_unwritten(bh));
27971da177e4SLinus Torvalds 
279848fd4f93SJens Axboe 	/*
279948fd4f93SJens Axboe 	 * Only clear out a write error when rewriting
28001da177e4SLinus Torvalds 	 */
28012a222ca9SMike Christie 	if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
28021da177e4SLinus Torvalds 		clear_buffer_write_io_error(bh);
28031da177e4SLinus Torvalds 
280407888c66SChristoph Hellwig 	if (buffer_meta(bh))
28051420c4a5SBart Van Assche 		opf |= REQ_META;
280607888c66SChristoph Hellwig 	if (buffer_prio(bh))
28071420c4a5SBart Van Assche 		opf |= REQ_PRIO;
280807888c66SChristoph Hellwig 
28091420c4a5SBart Van Assche 	bio = bio_alloc(bh->b_bdev, 1, opf, GFP_NOIO);
28101da177e4SLinus Torvalds 
28114f74d15fSEric Biggers 	fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
28124f74d15fSEric Biggers 
28134f024f37SKent Overstreet 	bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
28141da177e4SLinus Torvalds 
2815741af75dSJohannes Thumshirn 	__bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	bio->bi_end_io = end_bio_bh_io_sync;
28181da177e4SLinus Torvalds 	bio->bi_private = bh;
28191da177e4SLinus Torvalds 
282083c9c547SMing Lei 	/* Take care of bh's that straddle the end of the device */
282183c9c547SMing Lei 	guard_bio_eod(bio);
282283c9c547SMing Lei 
2823fd42df30SDennis Zhou 	if (wbc) {
2824fd42df30SDennis Zhou 		wbc_init_bio(wbc, bio);
282534e51a5eSTejun Heo 		wbc_account_cgroup_owner(wbc, bh->b_page, bh->b_size);
2826fd42df30SDennis Zhou 	}
2827fd42df30SDennis Zhou 
28284e49ea4aSMike Christie 	submit_bio(bio);
28291da177e4SLinus Torvalds }
2830bafc0dbaSTejun Heo 
28315bdf402aSRitesh Harjani (IBM) void submit_bh(blk_opf_t opf, struct buffer_head *bh)
283271368511SDarrick J. Wong {
28335bdf402aSRitesh Harjani (IBM) 	submit_bh_wbc(opf, bh, NULL);
283471368511SDarrick J. Wong }
28351fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(submit_bh);
28361da177e4SLinus Torvalds 
28373ae72869SBart Van Assche void write_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags)
28389cb569d6SChristoph Hellwig {
28399cb569d6SChristoph Hellwig 	lock_buffer(bh);
28409cb569d6SChristoph Hellwig 	if (!test_clear_buffer_dirty(bh)) {
28419cb569d6SChristoph Hellwig 		unlock_buffer(bh);
28429cb569d6SChristoph Hellwig 		return;
28439cb569d6SChristoph Hellwig 	}
28449cb569d6SChristoph Hellwig 	bh->b_end_io = end_buffer_write_sync;
28459cb569d6SChristoph Hellwig 	get_bh(bh);
28461420c4a5SBart Van Assche 	submit_bh(REQ_OP_WRITE | op_flags, bh);
28479cb569d6SChristoph Hellwig }
28489cb569d6SChristoph Hellwig EXPORT_SYMBOL(write_dirty_buffer);
28499cb569d6SChristoph Hellwig 
28501da177e4SLinus Torvalds /*
28511da177e4SLinus Torvalds  * For a data-integrity writeout, we need to wait upon any in-progress I/O
28521da177e4SLinus Torvalds  * and then start new I/O and then wait upon it.  The caller must have a ref on
28531da177e4SLinus Torvalds  * the buffer_head.
28541da177e4SLinus Torvalds  */
28553ae72869SBart Van Assche int __sync_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags)
28561da177e4SLinus Torvalds {
28571da177e4SLinus Torvalds 	WARN_ON(atomic_read(&bh->b_count) < 1);
28581da177e4SLinus Torvalds 	lock_buffer(bh);
28591da177e4SLinus Torvalds 	if (test_clear_buffer_dirty(bh)) {
2860377254b2SXianting Tian 		/*
2861377254b2SXianting Tian 		 * The bh should be mapped, but it might not be if the
2862377254b2SXianting Tian 		 * device was hot-removed. Not much we can do but fail the I/O.
2863377254b2SXianting Tian 		 */
2864377254b2SXianting Tian 		if (!buffer_mapped(bh)) {
2865377254b2SXianting Tian 			unlock_buffer(bh);
2866377254b2SXianting Tian 			return -EIO;
2867377254b2SXianting Tian 		}
2868377254b2SXianting Tian 
28691da177e4SLinus Torvalds 		get_bh(bh);
28701da177e4SLinus Torvalds 		bh->b_end_io = end_buffer_write_sync;
2871ab620620SRitesh Harjani (IBM) 		submit_bh(REQ_OP_WRITE | op_flags, bh);
28721da177e4SLinus Torvalds 		wait_on_buffer(bh);
2873ab620620SRitesh Harjani (IBM) 		if (!buffer_uptodate(bh))
2874ab620620SRitesh Harjani (IBM) 			return -EIO;
28751da177e4SLinus Torvalds 	} else {
28761da177e4SLinus Torvalds 		unlock_buffer(bh);
28771da177e4SLinus Torvalds 	}
2878ab620620SRitesh Harjani (IBM) 	return 0;
28791da177e4SLinus Torvalds }
288087e99511SChristoph Hellwig EXPORT_SYMBOL(__sync_dirty_buffer);
288187e99511SChristoph Hellwig 
288287e99511SChristoph Hellwig int sync_dirty_buffer(struct buffer_head *bh)
288387e99511SChristoph Hellwig {
288470fd7614SChristoph Hellwig 	return __sync_dirty_buffer(bh, REQ_SYNC);
288587e99511SChristoph Hellwig }
28861fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(sync_dirty_buffer);
28871da177e4SLinus Torvalds 
28881da177e4SLinus Torvalds /*
288968189fefSMatthew Wilcox (Oracle)  * try_to_free_buffers() checks if all the buffers on this particular folio
28901da177e4SLinus Torvalds  * are unused, and releases them if so.
28911da177e4SLinus Torvalds  *
28921da177e4SLinus Torvalds  * Exclusion against try_to_free_buffers may be obtained by either
289368189fefSMatthew Wilcox (Oracle)  * locking the folio or by holding its mapping's private_lock.
28941da177e4SLinus Torvalds  *
289568189fefSMatthew Wilcox (Oracle)  * If the folio is dirty but all the buffers are clean then we need to
289668189fefSMatthew Wilcox (Oracle)  * be sure to mark the folio clean as well.  This is because the folio
28971da177e4SLinus Torvalds  * may be against a block device, and a later reattachment of buffers
289868189fefSMatthew Wilcox (Oracle)  * to a dirty folio will set *all* buffers dirty.  Which would corrupt
28991da177e4SLinus Torvalds  * filesystem data on the same device.
29001da177e4SLinus Torvalds  *
290168189fefSMatthew Wilcox (Oracle)  * The same applies to regular filesystem folios: if all the buffers are
290268189fefSMatthew Wilcox (Oracle)  * clean then we set the folio clean and proceed.  To do that, we require
2903e621900aSMatthew Wilcox (Oracle)  * total exclusion from block_dirty_folio().  That is obtained with
29041da177e4SLinus Torvalds  * private_lock.
29051da177e4SLinus Torvalds  *
29061da177e4SLinus Torvalds  * try_to_free_buffers() is non-blocking.
29071da177e4SLinus Torvalds  */
29081da177e4SLinus Torvalds static inline int buffer_busy(struct buffer_head *bh)
29091da177e4SLinus Torvalds {
29101da177e4SLinus Torvalds 	return atomic_read(&bh->b_count) |
29111da177e4SLinus Torvalds 		(bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
29121da177e4SLinus Torvalds }
29131da177e4SLinus Torvalds 
291464394763SMatthew Wilcox (Oracle) static bool
291564394763SMatthew Wilcox (Oracle) drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free)
29161da177e4SLinus Torvalds {
291764394763SMatthew Wilcox (Oracle) 	struct buffer_head *head = folio_buffers(folio);
29181da177e4SLinus Torvalds 	struct buffer_head *bh;
29191da177e4SLinus Torvalds 
29201da177e4SLinus Torvalds 	bh = head;
29211da177e4SLinus Torvalds 	do {
29221da177e4SLinus Torvalds 		if (buffer_busy(bh))
29231da177e4SLinus Torvalds 			goto failed;
29241da177e4SLinus Torvalds 		bh = bh->b_this_page;
29251da177e4SLinus Torvalds 	} while (bh != head);
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 	do {
29281da177e4SLinus Torvalds 		struct buffer_head *next = bh->b_this_page;
29291da177e4SLinus Torvalds 
2930535ee2fbSJan Kara 		if (bh->b_assoc_map)
29311da177e4SLinus Torvalds 			__remove_assoc_queue(bh);
29321da177e4SLinus Torvalds 		bh = next;
29331da177e4SLinus Torvalds 	} while (bh != head);
29341da177e4SLinus Torvalds 	*buffers_to_free = head;
293564394763SMatthew Wilcox (Oracle) 	folio_detach_private(folio);
293664394763SMatthew Wilcox (Oracle) 	return true;
29371da177e4SLinus Torvalds failed:
293864394763SMatthew Wilcox (Oracle) 	return false;
29391da177e4SLinus Torvalds }
29401da177e4SLinus Torvalds 
294168189fefSMatthew Wilcox (Oracle) bool try_to_free_buffers(struct folio *folio)
29421da177e4SLinus Torvalds {
294368189fefSMatthew Wilcox (Oracle) 	struct address_space * const mapping = folio->mapping;
29441da177e4SLinus Torvalds 	struct buffer_head *buffers_to_free = NULL;
294568189fefSMatthew Wilcox (Oracle) 	bool ret = 0;
29461da177e4SLinus Torvalds 
294768189fefSMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
294868189fefSMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio))
294968189fefSMatthew Wilcox (Oracle) 		return false;
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 	if (mapping == NULL) {		/* can this still happen? */
295264394763SMatthew Wilcox (Oracle) 		ret = drop_buffers(folio, &buffers_to_free);
29531da177e4SLinus Torvalds 		goto out;
29541da177e4SLinus Torvalds 	}
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	spin_lock(&mapping->private_lock);
295764394763SMatthew Wilcox (Oracle) 	ret = drop_buffers(folio, &buffers_to_free);
2958ecdfc978SLinus Torvalds 
2959ecdfc978SLinus Torvalds 	/*
2960ecdfc978SLinus Torvalds 	 * If the filesystem writes its buffers by hand (eg ext3)
296168189fefSMatthew Wilcox (Oracle) 	 * then we can have clean buffers against a dirty folio.  We
296268189fefSMatthew Wilcox (Oracle) 	 * clean the folio here; otherwise the VM will never notice
2963ecdfc978SLinus Torvalds 	 * that the filesystem did any IO at all.
2964ecdfc978SLinus Torvalds 	 *
2965ecdfc978SLinus Torvalds 	 * Also, during truncate, discard_buffer will have marked all
296668189fefSMatthew Wilcox (Oracle) 	 * the folio's buffers clean.  We discover that here and clean
296768189fefSMatthew Wilcox (Oracle) 	 * the folio also.
296887df7241SNick Piggin 	 *
296987df7241SNick Piggin 	 * private_lock must be held over this entire operation in order
2970e621900aSMatthew Wilcox (Oracle) 	 * to synchronise against block_dirty_folio and prevent the
297187df7241SNick Piggin 	 * dirty bit from being lost.
2972ecdfc978SLinus Torvalds 	 */
297311f81becSTejun Heo 	if (ret)
297468189fefSMatthew Wilcox (Oracle) 		folio_cancel_dirty(folio);
297587df7241SNick Piggin 	spin_unlock(&mapping->private_lock);
29761da177e4SLinus Torvalds out:
29771da177e4SLinus Torvalds 	if (buffers_to_free) {
29781da177e4SLinus Torvalds 		struct buffer_head *bh = buffers_to_free;
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 		do {
29811da177e4SLinus Torvalds 			struct buffer_head *next = bh->b_this_page;
29821da177e4SLinus Torvalds 			free_buffer_head(bh);
29831da177e4SLinus Torvalds 			bh = next;
29841da177e4SLinus Torvalds 		} while (bh != buffers_to_free);
29851da177e4SLinus Torvalds 	}
29861da177e4SLinus Torvalds 	return ret;
29871da177e4SLinus Torvalds }
29881da177e4SLinus Torvalds EXPORT_SYMBOL(try_to_free_buffers);
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds /*
29911da177e4SLinus Torvalds  * Buffer-head allocation
29921da177e4SLinus Torvalds  */
2993a0a9b043SShai Fultheim static struct kmem_cache *bh_cachep __read_mostly;
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds /*
29961da177e4SLinus Torvalds  * Once the number of bh's in the machine exceeds this level, we start
29971da177e4SLinus Torvalds  * stripping them in writeback.
29981da177e4SLinus Torvalds  */
299943be594aSZhang Yanfei static unsigned long max_buffer_heads;
30001da177e4SLinus Torvalds 
30011da177e4SLinus Torvalds int buffer_heads_over_limit;
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds struct bh_accounting {
30041da177e4SLinus Torvalds 	int nr;			/* Number of live bh's */
30051da177e4SLinus Torvalds 	int ratelimit;		/* Limit cacheline bouncing */
30061da177e4SLinus Torvalds };
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds static void recalc_bh_state(void)
30111da177e4SLinus Torvalds {
30121da177e4SLinus Torvalds 	int i;
30131da177e4SLinus Torvalds 	int tot = 0;
30141da177e4SLinus Torvalds 
3015ee1be862SChristoph Lameter 	if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
30161da177e4SLinus Torvalds 		return;
3017c7b92516SChristoph Lameter 	__this_cpu_write(bh_accounting.ratelimit, 0);
30188a143426SEric Dumazet 	for_each_online_cpu(i)
30191da177e4SLinus Torvalds 		tot += per_cpu(bh_accounting, i).nr;
30201da177e4SLinus Torvalds 	buffer_heads_over_limit = (tot > max_buffer_heads);
30211da177e4SLinus Torvalds }
30221da177e4SLinus Torvalds 
3023dd0fc66fSAl Viro struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
30241da177e4SLinus Torvalds {
3025019b4d12SRichard Kennedy 	struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
30261da177e4SLinus Torvalds 	if (ret) {
3027a35afb83SChristoph Lameter 		INIT_LIST_HEAD(&ret->b_assoc_buffers);
3028f1e67e35SThomas Gleixner 		spin_lock_init(&ret->b_uptodate_lock);
3029c7b92516SChristoph Lameter 		preempt_disable();
3030c7b92516SChristoph Lameter 		__this_cpu_inc(bh_accounting.nr);
30311da177e4SLinus Torvalds 		recalc_bh_state();
3032c7b92516SChristoph Lameter 		preempt_enable();
30331da177e4SLinus Torvalds 	}
30341da177e4SLinus Torvalds 	return ret;
30351da177e4SLinus Torvalds }
30361da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_buffer_head);
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds void free_buffer_head(struct buffer_head *bh)
30391da177e4SLinus Torvalds {
30401da177e4SLinus Torvalds 	BUG_ON(!list_empty(&bh->b_assoc_buffers));
30411da177e4SLinus Torvalds 	kmem_cache_free(bh_cachep, bh);
3042c7b92516SChristoph Lameter 	preempt_disable();
3043c7b92516SChristoph Lameter 	__this_cpu_dec(bh_accounting.nr);
30441da177e4SLinus Torvalds 	recalc_bh_state();
3045c7b92516SChristoph Lameter 	preempt_enable();
30461da177e4SLinus Torvalds }
30471da177e4SLinus Torvalds EXPORT_SYMBOL(free_buffer_head);
30481da177e4SLinus Torvalds 
3049fc4d24c9SSebastian Andrzej Siewior static int buffer_exit_cpu_dead(unsigned int cpu)
30501da177e4SLinus Torvalds {
30511da177e4SLinus Torvalds 	int i;
30521da177e4SLinus Torvalds 	struct bh_lru *b = &per_cpu(bh_lrus, cpu);
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	for (i = 0; i < BH_LRU_SIZE; i++) {
30551da177e4SLinus Torvalds 		brelse(b->bhs[i]);
30561da177e4SLinus Torvalds 		b->bhs[i] = NULL;
30571da177e4SLinus Torvalds 	}
3058c7b92516SChristoph Lameter 	this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
30598a143426SEric Dumazet 	per_cpu(bh_accounting, cpu).nr = 0;
3060fc4d24c9SSebastian Andrzej Siewior 	return 0;
30611da177e4SLinus Torvalds }
30621da177e4SLinus Torvalds 
3063389d1b08SAneesh Kumar K.V /**
3064a6b91919SRandy Dunlap  * bh_uptodate_or_lock - Test whether the buffer is uptodate
3065389d1b08SAneesh Kumar K.V  * @bh: struct buffer_head
3066389d1b08SAneesh Kumar K.V  *
3067389d1b08SAneesh Kumar K.V  * Return true if the buffer is up-to-date and false,
3068389d1b08SAneesh Kumar K.V  * with the buffer locked, if not.
3069389d1b08SAneesh Kumar K.V  */
3070389d1b08SAneesh Kumar K.V int bh_uptodate_or_lock(struct buffer_head *bh)
3071389d1b08SAneesh Kumar K.V {
3072389d1b08SAneesh Kumar K.V 	if (!buffer_uptodate(bh)) {
3073389d1b08SAneesh Kumar K.V 		lock_buffer(bh);
3074389d1b08SAneesh Kumar K.V 		if (!buffer_uptodate(bh))
3075389d1b08SAneesh Kumar K.V 			return 0;
3076389d1b08SAneesh Kumar K.V 		unlock_buffer(bh);
3077389d1b08SAneesh Kumar K.V 	}
3078389d1b08SAneesh Kumar K.V 	return 1;
3079389d1b08SAneesh Kumar K.V }
3080389d1b08SAneesh Kumar K.V EXPORT_SYMBOL(bh_uptodate_or_lock);
3081389d1b08SAneesh Kumar K.V 
3082389d1b08SAneesh Kumar K.V /**
3083fdee117eSZhang Yi  * __bh_read - Submit read for a locked buffer
3084389d1b08SAneesh Kumar K.V  * @bh: struct buffer_head
3085fdee117eSZhang Yi  * @op_flags: appending REQ_OP_* flags besides REQ_OP_READ
3086fdee117eSZhang Yi  * @wait: wait until reading finish
3087389d1b08SAneesh Kumar K.V  *
3088fdee117eSZhang Yi  * Returns zero on success or don't wait, and -EIO on error.
3089389d1b08SAneesh Kumar K.V  */
3090fdee117eSZhang Yi int __bh_read(struct buffer_head *bh, blk_opf_t op_flags, bool wait)
3091389d1b08SAneesh Kumar K.V {
3092fdee117eSZhang Yi 	int ret = 0;
3093389d1b08SAneesh Kumar K.V 
3094fdee117eSZhang Yi 	BUG_ON(!buffer_locked(bh));
3095389d1b08SAneesh Kumar K.V 
3096389d1b08SAneesh Kumar K.V 	get_bh(bh);
3097389d1b08SAneesh Kumar K.V 	bh->b_end_io = end_buffer_read_sync;
3098fdee117eSZhang Yi 	submit_bh(REQ_OP_READ | op_flags, bh);
3099fdee117eSZhang Yi 	if (wait) {
3100389d1b08SAneesh Kumar K.V 		wait_on_buffer(bh);
3101fdee117eSZhang Yi 		if (!buffer_uptodate(bh))
3102fdee117eSZhang Yi 			ret = -EIO;
3103389d1b08SAneesh Kumar K.V 	}
3104fdee117eSZhang Yi 	return ret;
3105fdee117eSZhang Yi }
3106fdee117eSZhang Yi EXPORT_SYMBOL(__bh_read);
3107fdee117eSZhang Yi 
3108fdee117eSZhang Yi /**
3109fdee117eSZhang Yi  * __bh_read_batch - Submit read for a batch of unlocked buffers
3110fdee117eSZhang Yi  * @nr: entry number of the buffer batch
3111fdee117eSZhang Yi  * @bhs: a batch of struct buffer_head
3112fdee117eSZhang Yi  * @op_flags: appending REQ_OP_* flags besides REQ_OP_READ
3113fdee117eSZhang Yi  * @force_lock: force to get a lock on the buffer if set, otherwise drops any
3114fdee117eSZhang Yi  *              buffer that cannot lock.
3115fdee117eSZhang Yi  *
3116fdee117eSZhang Yi  * Returns zero on success or don't wait, and -EIO on error.
3117fdee117eSZhang Yi  */
3118fdee117eSZhang Yi void __bh_read_batch(int nr, struct buffer_head *bhs[],
3119fdee117eSZhang Yi 		     blk_opf_t op_flags, bool force_lock)
3120fdee117eSZhang Yi {
3121fdee117eSZhang Yi 	int i;
3122fdee117eSZhang Yi 
3123fdee117eSZhang Yi 	for (i = 0; i < nr; i++) {
3124fdee117eSZhang Yi 		struct buffer_head *bh = bhs[i];
3125fdee117eSZhang Yi 
3126fdee117eSZhang Yi 		if (buffer_uptodate(bh))
3127fdee117eSZhang Yi 			continue;
3128fdee117eSZhang Yi 
3129fdee117eSZhang Yi 		if (force_lock)
3130fdee117eSZhang Yi 			lock_buffer(bh);
3131fdee117eSZhang Yi 		else
3132fdee117eSZhang Yi 			if (!trylock_buffer(bh))
3133fdee117eSZhang Yi 				continue;
3134fdee117eSZhang Yi 
3135fdee117eSZhang Yi 		if (buffer_uptodate(bh)) {
3136fdee117eSZhang Yi 			unlock_buffer(bh);
3137fdee117eSZhang Yi 			continue;
3138fdee117eSZhang Yi 		}
3139fdee117eSZhang Yi 
3140fdee117eSZhang Yi 		bh->b_end_io = end_buffer_read_sync;
3141fdee117eSZhang Yi 		get_bh(bh);
3142fdee117eSZhang Yi 		submit_bh(REQ_OP_READ | op_flags, bh);
3143fdee117eSZhang Yi 	}
3144fdee117eSZhang Yi }
3145fdee117eSZhang Yi EXPORT_SYMBOL(__bh_read_batch);
3146389d1b08SAneesh Kumar K.V 
31471da177e4SLinus Torvalds void __init buffer_init(void)
31481da177e4SLinus Torvalds {
314943be594aSZhang Yanfei 	unsigned long nrpages;
3150fc4d24c9SSebastian Andrzej Siewior 	int ret;
31511da177e4SLinus Torvalds 
3152b98938c3SChristoph Lameter 	bh_cachep = kmem_cache_create("buffer_head",
3153b98938c3SChristoph Lameter 			sizeof(struct buffer_head), 0,
3154b98938c3SChristoph Lameter 				(SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3155b98938c3SChristoph Lameter 				SLAB_MEM_SPREAD),
3156019b4d12SRichard Kennedy 				NULL);
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds 	/*
31591da177e4SLinus Torvalds 	 * Limit the bh occupancy to 10% of ZONE_NORMAL
31601da177e4SLinus Torvalds 	 */
31611da177e4SLinus Torvalds 	nrpages = (nr_free_buffer_pages() * 10) / 100;
31621da177e4SLinus Torvalds 	max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3163fc4d24c9SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
3164fc4d24c9SSebastian Andrzej Siewior 					NULL, buffer_exit_cpu_dead);
3165fc4d24c9SSebastian Andrzej Siewior 	WARN_ON(ret < 0);
31661da177e4SLinus Torvalds }
3167