Merge tag 'probes-fixes-v6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / buffer.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/fs/buffer.c
4 *
5 * Copyright (C) 1991, 1992, 2002 Linus Torvalds
6 */
7
8/*
9 * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
10 *
11 * Removed a lot of unnecessary code and simplified things now that
12 * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
13 *
14 * Speed up hash, lru, and free list operations. Use gfp() for allocating
15 * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
16 *
17 * Added 32k buffer block sizes - these are required older ARM systems. - RMK
18 *
19 * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
20 */
21
1da177e4 22#include <linux/kernel.h>
f361bf4a 23#include <linux/sched/signal.h>
1da177e4
LT
24#include <linux/syscalls.h>
25#include <linux/fs.h>
ae259a9c 26#include <linux/iomap.h>
1da177e4
LT
27#include <linux/mm.h>
28#include <linux/percpu.h>
29#include <linux/slab.h>
16f7e0fe 30#include <linux/capability.h>
1da177e4
LT
31#include <linux/blkdev.h>
32#include <linux/file.h>
33#include <linux/quotaops.h>
34#include <linux/highmem.h>
630d9c47 35#include <linux/export.h>
bafc0dba 36#include <linux/backing-dev.h>
1da177e4
LT
37#include <linux/writeback.h>
38#include <linux/hash.h>
39#include <linux/suspend.h>
40#include <linux/buffer_head.h>
55e829af 41#include <linux/task_io_accounting_ops.h>
1da177e4 42#include <linux/bio.h>
1da177e4
LT
43#include <linux/cpu.h>
44#include <linux/bitops.h>
45#include <linux/mpage.h>
fb1c8f93 46#include <linux/bit_spinlock.h>
29f3ad7d 47#include <linux/pagevec.h>
f745c6f5 48#include <linux/sched/mm.h>
5305cb83 49#include <trace/events/block.h>
31fb992c 50#include <linux/fscrypt.h>
4fa512ce 51#include <linux/fsverity.h>
8a237adf 52#include <linux/sched/isolation.h>
1da177e4 53
2b211dc0
BD
54#include "internal.h"
55
1da177e4 56static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
5bdf402a 57static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
44981351 58 enum rw_hint hint, struct writeback_control *wbc);
1da177e4
LT
59
60#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
61
f0059afd
TH
62inline void touch_buffer(struct buffer_head *bh)
63{
5305cb83 64 trace_block_touch_buffer(bh);
03c5f331 65 folio_mark_accessed(bh->b_folio);
f0059afd
TH
66}
67EXPORT_SYMBOL(touch_buffer);
68
fc9b52cd 69void __lock_buffer(struct buffer_head *bh)
1da177e4 70{
74316201 71 wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
1da177e4
LT
72}
73EXPORT_SYMBOL(__lock_buffer);
74
fc9b52cd 75void unlock_buffer(struct buffer_head *bh)
1da177e4 76{
51b07fc3 77 clear_bit_unlock(BH_Lock, &bh->b_state);
4e857c58 78 smp_mb__after_atomic();
1da177e4
LT
79 wake_up_bit(&bh->b_state, BH_Lock);
80}
1fe72eaa 81EXPORT_SYMBOL(unlock_buffer);
1da177e4 82
b4597226 83/*
520f301c
MWO
84 * Returns if the folio has dirty or writeback buffers. If all the buffers
85 * are unlocked and clean then the folio_test_dirty information is stale. If
86 * any of the buffers are locked, it is assumed they are locked for IO.
b4597226 87 */
520f301c 88void buffer_check_dirty_writeback(struct folio *folio,
b4597226
MG
89 bool *dirty, bool *writeback)
90{
91 struct buffer_head *head, *bh;
92 *dirty = false;
93 *writeback = false;
94
520f301c 95 BUG_ON(!folio_test_locked(folio));
b4597226 96
520f301c
MWO
97 head = folio_buffers(folio);
98 if (!head)
b4597226
MG
99 return;
100
520f301c 101 if (folio_test_writeback(folio))
b4597226
MG
102 *writeback = true;
103
b4597226
MG
104 bh = head;
105 do {
106 if (buffer_locked(bh))
107 *writeback = true;
108
109 if (buffer_dirty(bh))
110 *dirty = true;
111
112 bh = bh->b_this_page;
113 } while (bh != head);
114}
b4597226 115
1da177e4
LT
116/*
117 * Block until a buffer comes unlocked. This doesn't stop it
118 * from becoming locked again - you have to lock it yourself
119 * if you want to preserve its state.
120 */
121void __wait_on_buffer(struct buffer_head * bh)
122{
74316201 123 wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
1da177e4 124}
1fe72eaa 125EXPORT_SYMBOL(__wait_on_buffer);
1da177e4 126
b744c2ac 127static void buffer_io_error(struct buffer_head *bh, char *msg)
1da177e4 128{
432f16e6
RE
129 if (!test_bit(BH_Quiet, &bh->b_state))
130 printk_ratelimited(KERN_ERR
a1c6f057
DM
131 "Buffer I/O error on dev %pg, logical block %llu%s\n",
132 bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
1da177e4
LT
133}
134
135/*
68671f35
DM
136 * End-of-IO handler helper function which does not touch the bh after
137 * unlocking it.
138 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
139 * a race there is benign: unlock_buffer() only use the bh's address for
140 * hashing after unlocking the buffer, so it doesn't actually touch the bh
141 * itself.
1da177e4 142 */
68671f35 143static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
1da177e4
LT
144{
145 if (uptodate) {
146 set_buffer_uptodate(bh);
147 } else {
70246286 148 /* This happens, due to failed read-ahead attempts. */
1da177e4
LT
149 clear_buffer_uptodate(bh);
150 }
151 unlock_buffer(bh);
68671f35
DM
152}
153
154/*
155 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
79f59784 156 * unlock the buffer.
68671f35
DM
157 */
158void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
159{
160 __end_buffer_read_notouch(bh, uptodate);
1da177e4
LT
161 put_bh(bh);
162}
1fe72eaa 163EXPORT_SYMBOL(end_buffer_read_sync);
1da177e4
LT
164
165void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
166{
1da177e4
LT
167 if (uptodate) {
168 set_buffer_uptodate(bh);
169 } else {
432f16e6 170 buffer_io_error(bh, ", lost sync page write");
87354e5d 171 mark_buffer_write_io_error(bh);
1da177e4
LT
172 clear_buffer_uptodate(bh);
173 }
174 unlock_buffer(bh);
175 put_bh(bh);
176}
1fe72eaa 177EXPORT_SYMBOL(end_buffer_write_sync);
1da177e4 178
1da177e4 179static struct buffer_head *
7ffe3de5 180__find_get_block_slow(struct block_device *bdev, sector_t block, bool atomic)
1da177e4 181{
53cd4cd3
AV
182 struct address_space *bd_mapping = bdev->bd_mapping;
183 const int blkbits = bd_mapping->host->i_blkbits;
1da177e4
LT
184 struct buffer_head *ret = NULL;
185 pgoff_t index;
186 struct buffer_head *bh;
187 struct buffer_head *head;
eee25182 188 struct folio *folio;
1da177e4 189 int all_mapped = 1;
43636c80 190 static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
1da177e4 191
53cd4cd3 192 index = ((loff_t)block << blkbits) / PAGE_SIZE;
eee25182
MWO
193 folio = __filemap_get_folio(bd_mapping, index, FGP_ACCESSED, 0);
194 if (IS_ERR(folio))
1da177e4
LT
195 goto out;
196
7ffe3de5
DB
197 /*
198 * Folio lock protects the buffers. Callers that cannot block
199 * will fallback to serializing vs try_to_free_buffers() via
200 * the i_private_lock.
201 */
202 if (atomic)
203 spin_lock(&bd_mapping->i_private_lock);
204 else
205 folio_lock(folio);
206
eee25182
MWO
207 head = folio_buffers(folio);
208 if (!head)
1da177e4 209 goto out_unlock;
2d900eff
DB
210 /*
211 * Upon a noref migration, the folio lock serializes here;
212 * otherwise bail.
213 */
214 if (test_bit_acquire(BH_Migrate, &head->b_state)) {
215 WARN_ON(!atomic);
216 goto out_unlock;
217 }
218
1da177e4
LT
219 bh = head;
220 do {
97f76d3d
NK
221 if (!buffer_mapped(bh))
222 all_mapped = 0;
223 else if (bh->b_blocknr == block) {
1da177e4
LT
224 ret = bh;
225 get_bh(bh);
226 goto out_unlock;
227 }
1da177e4
LT
228 bh = bh->b_this_page;
229 } while (bh != head);
230
231 /* we might be here because some of the buffers on this page are
232 * not mapped. This is due to various races between
233 * file io on the block device and getblk. It gets dealt with
234 * elsewhere, don't buffer_error if we had some unmapped buffers
235 */
43636c80
TH
236 ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
237 if (all_mapped && __ratelimit(&last_warned)) {
238 printk("__find_get_block_slow() failed. block=%llu, "
239 "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
240 "device %pg blocksize: %d\n",
241 (unsigned long long)block,
242 (unsigned long long)bh->b_blocknr,
243 bh->b_state, bh->b_size, bdev,
53cd4cd3 244 1 << blkbits);
1da177e4
LT
245 }
246out_unlock:
7ffe3de5
DB
247 if (atomic)
248 spin_unlock(&bd_mapping->i_private_lock);
249 else
250 folio_unlock(folio);
eee25182 251 folio_put(folio);
1da177e4
LT
252out:
253 return ret;
254}
255
1da177e4
LT
256static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
257{
1da177e4 258 unsigned long flags;
a3972203 259 struct buffer_head *first;
1da177e4 260 struct buffer_head *tmp;
2e2dba15
MWO
261 struct folio *folio;
262 int folio_uptodate = 1;
1da177e4
LT
263
264 BUG_ON(!buffer_async_read(bh));
265
2e2dba15 266 folio = bh->b_folio;
1da177e4
LT
267 if (uptodate) {
268 set_buffer_uptodate(bh);
269 } else {
270 clear_buffer_uptodate(bh);
432f16e6 271 buffer_io_error(bh, ", async page read");
1da177e4
LT
272 }
273
274 /*
275 * Be _very_ careful from here on. Bad things can happen if
276 * two buffer heads end IO at almost the same time and both
277 * decide that the page is now completely done.
278 */
2e2dba15 279 first = folio_buffers(folio);
f1e67e35 280 spin_lock_irqsave(&first->b_uptodate_lock, flags);
1da177e4
LT
281 clear_buffer_async_read(bh);
282 unlock_buffer(bh);
283 tmp = bh;
284 do {
285 if (!buffer_uptodate(tmp))
2e2dba15 286 folio_uptodate = 0;
1da177e4
LT
287 if (buffer_async_read(tmp)) {
288 BUG_ON(!buffer_locked(tmp));
289 goto still_busy;
290 }
291 tmp = tmp->b_this_page;
292 } while (tmp != bh);
f1e67e35 293 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
1da177e4 294
6ba924d3 295 folio_end_read(folio, folio_uptodate);
1da177e4
LT
296 return;
297
298still_busy:
f1e67e35 299 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
1da177e4
LT
300}
301
4fa512ce 302struct postprocess_bh_ctx {
31fb992c
EB
303 struct work_struct work;
304 struct buffer_head *bh;
305};
306
4fa512ce
EB
307static void verify_bh(struct work_struct *work)
308{
309 struct postprocess_bh_ctx *ctx =
310 container_of(work, struct postprocess_bh_ctx, work);
311 struct buffer_head *bh = ctx->bh;
312 bool valid;
313
8b7d3fe9 314 valid = fsverity_verify_blocks(bh->b_folio, bh->b_size, bh_offset(bh));
4fa512ce
EB
315 end_buffer_async_read(bh, valid);
316 kfree(ctx);
317}
318
319static bool need_fsverity(struct buffer_head *bh)
320{
8b7d3fe9
EB
321 struct folio *folio = bh->b_folio;
322 struct inode *inode = folio->mapping->host;
4fa512ce
EB
323
324 return fsverity_active(inode) &&
325 /* needed by ext4 */
8b7d3fe9 326 folio->index < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
4fa512ce
EB
327}
328
31fb992c
EB
329static void decrypt_bh(struct work_struct *work)
330{
4fa512ce
EB
331 struct postprocess_bh_ctx *ctx =
332 container_of(work, struct postprocess_bh_ctx, work);
31fb992c
EB
333 struct buffer_head *bh = ctx->bh;
334 int err;
335
9c7fb7f7
EB
336 err = fscrypt_decrypt_pagecache_blocks(bh->b_folio, bh->b_size,
337 bh_offset(bh));
4fa512ce
EB
338 if (err == 0 && need_fsverity(bh)) {
339 /*
340 * We use different work queues for decryption and for verity
341 * because verity may require reading metadata pages that need
342 * decryption, and we shouldn't recurse to the same workqueue.
343 */
344 INIT_WORK(&ctx->work, verify_bh);
345 fsverity_enqueue_verify_work(&ctx->work);
346 return;
347 }
31fb992c
EB
348 end_buffer_async_read(bh, err == 0);
349 kfree(ctx);
350}
351
352/*
2c69e205 353 * I/O completion handler for block_read_full_folio() - pages
31fb992c
EB
354 * which come unlocked at the end of I/O.
355 */
356static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate)
357{
3822a7c4 358 struct inode *inode = bh->b_folio->mapping->host;
4fa512ce
EB
359 bool decrypt = fscrypt_inode_uses_fs_layer_crypto(inode);
360 bool verify = need_fsverity(bh);
361
362 /* Decrypt (with fscrypt) and/or verify (with fsverity) if needed. */
363 if (uptodate && (decrypt || verify)) {
364 struct postprocess_bh_ctx *ctx =
365 kmalloc(sizeof(*ctx), GFP_ATOMIC);
31fb992c
EB
366
367 if (ctx) {
31fb992c 368 ctx->bh = bh;
4fa512ce
EB
369 if (decrypt) {
370 INIT_WORK(&ctx->work, decrypt_bh);
371 fscrypt_enqueue_decrypt_work(&ctx->work);
372 } else {
373 INIT_WORK(&ctx->work, verify_bh);
374 fsverity_enqueue_verify_work(&ctx->work);
375 }
31fb992c
EB
376 return;
377 }
378 uptodate = 0;
379 }
380 end_buffer_async_read(bh, uptodate);
381}
382
1da177e4 383/*
14059f66
MWO
384 * Completion handler for block_write_full_folio() - folios which are unlocked
385 * during I/O, and which have the writeback flag cleared upon I/O completion.
1da177e4 386 */
14059f66 387static void end_buffer_async_write(struct buffer_head *bh, int uptodate)
1da177e4 388{
1da177e4 389 unsigned long flags;
a3972203 390 struct buffer_head *first;
1da177e4 391 struct buffer_head *tmp;
743ed81e 392 struct folio *folio;
1da177e4
LT
393
394 BUG_ON(!buffer_async_write(bh));
395
743ed81e 396 folio = bh->b_folio;
1da177e4
LT
397 if (uptodate) {
398 set_buffer_uptodate(bh);
399 } else {
432f16e6 400 buffer_io_error(bh, ", lost async page write");
87354e5d 401 mark_buffer_write_io_error(bh);
1da177e4 402 clear_buffer_uptodate(bh);
1da177e4
LT
403 }
404
743ed81e 405 first = folio_buffers(folio);
f1e67e35 406 spin_lock_irqsave(&first->b_uptodate_lock, flags);
a3972203 407
1da177e4
LT
408 clear_buffer_async_write(bh);
409 unlock_buffer(bh);
410 tmp = bh->b_this_page;
411 while (tmp != bh) {
412 if (buffer_async_write(tmp)) {
413 BUG_ON(!buffer_locked(tmp));
414 goto still_busy;
415 }
416 tmp = tmp->b_this_page;
417 }
f1e67e35 418 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
743ed81e 419 folio_end_writeback(folio);
1da177e4
LT
420 return;
421
422still_busy:
f1e67e35 423 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
1da177e4
LT
424}
425
426/*
427 * If a page's buffers are under async readin (end_buffer_async_read
428 * completion) then there is a possibility that another thread of
429 * control could lock one of the buffers after it has completed
430 * but while some of the other buffers have not completed. This
431 * locked buffer would confuse end_buffer_async_read() into not unlocking
432 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
433 * that this buffer is not under async I/O.
434 *
435 * The page comes unlocked when it has no locked buffer_async buffers
436 * left.
437 *
438 * PageLocked prevents anyone starting new async I/O reads any of
439 * the buffers.
440 *
441 * PageWriteback is used to prevent simultaneous writeout of the same
442 * page.
443 *
444 * PageLocked prevents anyone from starting writeback of a page which is
445 * under read I/O (PageWriteback is only ever set against a locked page).
446 */
447static void mark_buffer_async_read(struct buffer_head *bh)
448{
31fb992c 449 bh->b_end_io = end_buffer_async_read_io;
1da177e4
LT
450 set_buffer_async_read(bh);
451}
452
1fe72eaa
HS
453static void mark_buffer_async_write_endio(struct buffer_head *bh,
454 bh_end_io_t *handler)
1da177e4 455{
35c80d5f 456 bh->b_end_io = handler;
1da177e4
LT
457 set_buffer_async_write(bh);
458}
35c80d5f
CM
459
460void mark_buffer_async_write(struct buffer_head *bh)
461{
462 mark_buffer_async_write_endio(bh, end_buffer_async_write);
463}
1da177e4
LT
464EXPORT_SYMBOL(mark_buffer_async_write);
465
466
467/*
468 * fs/buffer.c contains helper functions for buffer-backed address space's
469 * fsync functions. A common requirement for buffer-based filesystems is
470 * that certain data from the backing blockdev needs to be written out for
471 * a successful fsync(). For example, ext2 indirect blocks need to be
472 * written back and waited upon before fsync() returns.
473 *
73f65b8b 474 * The functions mark_buffer_dirty_inode(), fsync_inode_buffers(),
1da177e4 475 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
600f111e 476 * management of a list of dependent buffers at ->i_mapping->i_private_list.
1da177e4
LT
477 *
478 * Locking is a little subtle: try_to_free_buffers() will remove buffers
479 * from their controlling inode's queue when they are being freed. But
480 * try_to_free_buffers() will be operating against the *blockdev* mapping
481 * at the time, not against the S_ISREG file which depends on those buffers.
600f111e 482 * So the locking for i_private_list is via the i_private_lock in the address_space
1da177e4
LT
483 * which backs the buffers. Which is different from the address_space
484 * against which the buffers are listed. So for a particular address_space,
600f111e
MWO
485 * mapping->i_private_lock does *not* protect mapping->i_private_list! In fact,
486 * mapping->i_private_list will always be protected by the backing blockdev's
487 * ->i_private_lock.
1da177e4
LT
488 *
489 * Which introduces a requirement: all buffers on an address_space's
600f111e 490 * ->i_private_list must be from the same address_space: the blockdev's.
1da177e4 491 *
600f111e
MWO
492 * address_spaces which do not place buffers at ->i_private_list via these
493 * utility functions are free to use i_private_lock and i_private_list for
494 * whatever they want. The only requirement is that list_empty(i_private_list)
1da177e4
LT
495 * be true at clear_inode() time.
496 *
497 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
498 * filesystems should do that. invalidate_inode_buffers() should just go
499 * BUG_ON(!list_empty).
500 *
501 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
502 * take an address_space, not an inode. And it should be called
503 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
504 * queued up.
505 *
506 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
507 * list if it is already on a list. Because if the buffer is on a list,
508 * it *must* already be on the right one. If not, the filesystem is being
509 * silly. This will save a ton of locking. But first we have to ensure
510 * that buffers are taken *off* the old inode's list when they are freed
511 * (presumably in truncate). That requires careful auditing of all
512 * filesystems (do it inside bforget()). It could also be done by bringing
513 * b_inode back.
514 */
515
516/*
600f111e 517 * The buffer's backing address_space's i_private_lock must be held
1da177e4 518 */
dbacefc9 519static void __remove_assoc_queue(struct buffer_head *bh)
1da177e4
LT
520{
521 list_del_init(&bh->b_assoc_buffers);
58ff407b 522 WARN_ON(!bh->b_assoc_map);
58ff407b 523 bh->b_assoc_map = NULL;
1da177e4
LT
524}
525
526int inode_has_buffers(struct inode *inode)
527{
600f111e 528 return !list_empty(&inode->i_data.i_private_list);
1da177e4
LT
529}
530
531/*
532 * osync is designed to support O_SYNC io. It waits synchronously for
533 * all already-submitted IO to complete, but does not queue any new
534 * writes to the disk.
535 *
79f59784
ZY
536 * To do O_SYNC writes, just queue the buffer writes with write_dirty_buffer
537 * as you dirty the buffers, and then use osync_inode_buffers to wait for
1da177e4
LT
538 * completion. Any other dirty buffers which are not yet queued for
539 * write will not be flushed to disk by the osync.
540 */
541static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
542{
543 struct buffer_head *bh;
544 struct list_head *p;
545 int err = 0;
546
547 spin_lock(lock);
548repeat:
549 list_for_each_prev(p, list) {
550 bh = BH_ENTRY(p);
551 if (buffer_locked(bh)) {
552 get_bh(bh);
553 spin_unlock(lock);
554 wait_on_buffer(bh);
555 if (!buffer_uptodate(bh))
556 err = -EIO;
557 brelse(bh);
558 spin_lock(lock);
559 goto repeat;
560 }
561 }
562 spin_unlock(lock);
563 return err;
564}
565
566/**
78a4a50a 567 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
67be2dd1 568 * @mapping: the mapping which wants those buffers written
1da177e4 569 *
600f111e 570 * Starts I/O against the buffers at mapping->i_private_list, and waits upon
1da177e4
LT
571 * that I/O.
572 *
67be2dd1
MW
573 * Basically, this is a convenience function for fsync().
574 * @mapping is a file or directory which needs those buffers to be written for
575 * a successful fsync().
1da177e4
LT
576 */
577int sync_mapping_buffers(struct address_space *mapping)
578{
600f111e 579 struct address_space *buffer_mapping = mapping->i_private_data;
1da177e4 580
600f111e 581 if (buffer_mapping == NULL || list_empty(&mapping->i_private_list))
1da177e4
LT
582 return 0;
583
600f111e
MWO
584 return fsync_buffers_list(&buffer_mapping->i_private_lock,
585 &mapping->i_private_list);
1da177e4
LT
586}
587EXPORT_SYMBOL(sync_mapping_buffers);
588
31b2ebc0
RHI
589/**
590 * generic_buffers_fsync_noflush - generic buffer fsync implementation
591 * for simple filesystems with no inode lock
592 *
593 * @file: file to synchronize
594 * @start: start offset in bytes
595 * @end: end offset in bytes (inclusive)
596 * @datasync: only synchronize essential metadata if true
597 *
598 * This is a generic implementation of the fsync method for simple
599 * filesystems which track all non-inode metadata in the buffers list
600 * hanging off the address_space structure.
601 */
602int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
603 bool datasync)
604{
605 struct inode *inode = file->f_mapping->host;
606 int err;
607 int ret;
608
609 err = file_write_and_wait_range(file, start, end);
610 if (err)
611 return err;
612
613 ret = sync_mapping_buffers(inode->i_mapping);
614 if (!(inode->i_state & I_DIRTY_ALL))
615 goto out;
616 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
617 goto out;
618
619 err = sync_inode_metadata(inode, 1);
620 if (ret == 0)
621 ret = err;
622
623out:
624 /* check and advance again to catch errors after syncing out buffers */
625 err = file_check_and_advance_wb_err(file);
626 if (ret == 0)
627 ret = err;
628 return ret;
629}
630EXPORT_SYMBOL(generic_buffers_fsync_noflush);
631
632/**
633 * generic_buffers_fsync - generic buffer fsync implementation
634 * for simple filesystems with no inode lock
635 *
636 * @file: file to synchronize
637 * @start: start offset in bytes
638 * @end: end offset in bytes (inclusive)
639 * @datasync: only synchronize essential metadata if true
640 *
641 * This is a generic implementation of the fsync method for simple
642 * filesystems which track all non-inode metadata in the buffers list
643 * hanging off the address_space structure. This also makes sure that
644 * a device cache flush operation is called at the end.
645 */
646int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
647 bool datasync)
648{
649 struct inode *inode = file->f_mapping->host;
650 int ret;
651
652 ret = generic_buffers_fsync_noflush(file, start, end, datasync);
653 if (!ret)
654 ret = blkdev_issue_flush(inode->i_sb->s_bdev);
655 return ret;
656}
657EXPORT_SYMBOL(generic_buffers_fsync);
658
1da177e4
LT
659/*
660 * Called when we've recently written block `bblock', and it is known that
661 * `bblock' was for a buffer_boundary() buffer. This means that the block at
662 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
663 * dirty, schedule it for IO. So that indirects merge nicely with their data.
664 */
665void write_boundary_block(struct block_device *bdev,
666 sector_t bblock, unsigned blocksize)
667{
5b67d439
DB
668 struct buffer_head *bh;
669
670 bh = __find_get_block_nonatomic(bdev, bblock + 1, blocksize);
1da177e4
LT
671 if (bh) {
672 if (buffer_dirty(bh))
e7ea1129 673 write_dirty_buffer(bh, 0);
1da177e4
LT
674 put_bh(bh);
675 }
676}
677
678void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
679{
680 struct address_space *mapping = inode->i_mapping;
abc8a8a2 681 struct address_space *buffer_mapping = bh->b_folio->mapping;
1da177e4
LT
682
683 mark_buffer_dirty(bh);
600f111e
MWO
684 if (!mapping->i_private_data) {
685 mapping->i_private_data = buffer_mapping;
1da177e4 686 } else {
600f111e 687 BUG_ON(mapping->i_private_data != buffer_mapping);
1da177e4 688 }
535ee2fb 689 if (!bh->b_assoc_map) {
600f111e 690 spin_lock(&buffer_mapping->i_private_lock);
1da177e4 691 list_move_tail(&bh->b_assoc_buffers,
600f111e 692 &mapping->i_private_list);
58ff407b 693 bh->b_assoc_map = mapping;
600f111e 694 spin_unlock(&buffer_mapping->i_private_lock);
1da177e4
LT
695 }
696}
697EXPORT_SYMBOL(mark_buffer_dirty_inode);
698
3814ec89
MWO
699/**
700 * block_dirty_folio - Mark a folio as dirty.
701 * @mapping: The address space containing this folio.
702 * @folio: The folio to mark dirty.
703 *
704 * Filesystems which use buffer_heads can use this function as their
705 * ->dirty_folio implementation. Some filesystems need to do a little
706 * work before calling this function. Filesystems which do not use
707 * buffer_heads should call filemap_dirty_folio() instead.
1da177e4 708 *
3814ec89
MWO
709 * If the folio has buffers, the uptodate buffers are set dirty, to
710 * preserve dirty-state coherency between the folio and the buffers.
711 * Buffers added to a dirty folio are created dirty.
1da177e4 712 *
3814ec89
MWO
713 * The buffers are dirtied before the folio is dirtied. There's a small
714 * race window in which writeback may see the folio cleanness but not the
715 * buffer dirtiness. That's fine. If this code were to set the folio
716 * dirty before the buffers, writeback could clear the folio dirty flag,
717 * see a bunch of clean buffers and we'd end up with dirty buffers/clean
718 * folio on the dirty folio list.
1da177e4 719 *
3814ec89
MWO
720 * We use i_private_lock to lock against try_to_free_buffers() while
721 * using the folio's buffer list. This also prevents clean buffers
722 * being added to the folio after it was set dirty.
1da177e4 723 *
3814ec89
MWO
724 * Context: May only be called from process context. Does not sleep.
725 * Caller must ensure that @folio cannot be truncated during this call,
726 * typically by holding the folio lock or having a page in the folio
727 * mapped and holding the page table lock.
1da177e4 728 *
3814ec89 729 * Return: True if the folio was dirtied; false if it was already dirtied.
1da177e4 730 */
e621900a 731bool block_dirty_folio(struct address_space *mapping, struct folio *folio)
1da177e4 732{
e621900a
MWO
733 struct buffer_head *head;
734 bool newly_dirty;
1da177e4 735
600f111e 736 spin_lock(&mapping->i_private_lock);
e621900a
MWO
737 head = folio_buffers(folio);
738 if (head) {
1da177e4
LT
739 struct buffer_head *bh = head;
740
741 do {
742 set_buffer_dirty(bh);
743 bh = bh->b_this_page;
744 } while (bh != head);
745 }
c4843a75 746 /*
bcfe06bf 747 * Lock out page's memcg migration to keep PageDirty
81f8c3a4 748 * synchronized with per-memcg dirty page counters.
c4843a75 749 */
e621900a 750 newly_dirty = !folio_test_set_dirty(folio);
600f111e 751 spin_unlock(&mapping->i_private_lock);
1da177e4 752
a8e7d49a 753 if (newly_dirty)
e621900a 754 __folio_mark_dirty(folio, mapping, 1);
c4843a75 755
c4843a75
GT
756 if (newly_dirty)
757 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
758
a8e7d49a 759 return newly_dirty;
1da177e4 760}
e621900a 761EXPORT_SYMBOL(block_dirty_folio);
1da177e4
LT
762
763/*
764 * Write out and wait upon a list of buffers.
765 *
766 * We have conflicting pressures: we want to make sure that all
767 * initially dirty buffers get waited on, but that any subsequently
768 * dirtied buffers don't. After all, we don't want fsync to last
769 * forever if somebody is actively writing to the file.
770 *
771 * Do this in two main stages: first we copy dirty buffers to a
772 * temporary inode list, queueing the writes as we go. Then we clean
773 * up, waiting for those writes to complete.
774 *
775 * During this second stage, any subsequent updates to the file may end
776 * up refiling the buffer on the original inode's dirty list again, so
777 * there is a chance we will end up with a buffer queued for write but
778 * not yet completed on that list. So, as a final cleanup we go through
779 * the osync code to catch these locked, dirty buffers without requeuing
780 * any newly dirty buffers for write.
781 */
782static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
783{
784 struct buffer_head *bh;
7eaceacc 785 struct address_space *mapping;
1da177e4 786 int err = 0, err2;
4ee2491e 787 struct blk_plug plug;
73ce1c9f 788 LIST_HEAD(tmp);
1da177e4 789
4ee2491e 790 blk_start_plug(&plug);
1da177e4
LT
791
792 spin_lock(lock);
793 while (!list_empty(list)) {
794 bh = BH_ENTRY(list->next);
535ee2fb 795 mapping = bh->b_assoc_map;
58ff407b 796 __remove_assoc_queue(bh);
535ee2fb
JK
797 /* Avoid race with mark_buffer_dirty_inode() which does
798 * a lockless check and we rely on seeing the dirty bit */
799 smp_mb();
1da177e4
LT
800 if (buffer_dirty(bh) || buffer_locked(bh)) {
801 list_add(&bh->b_assoc_buffers, &tmp);
535ee2fb 802 bh->b_assoc_map = mapping;
1da177e4
LT
803 if (buffer_dirty(bh)) {
804 get_bh(bh);
805 spin_unlock(lock);
806 /*
807 * Ensure any pending I/O completes so that
9cb569d6
CH
808 * write_dirty_buffer() actually writes the
809 * current contents - it is a noop if I/O is
810 * still in flight on potentially older
811 * contents.
1da177e4 812 */
70fd7614 813 write_dirty_buffer(bh, REQ_SYNC);
9cf6b720
JA
814
815 /*
816 * Kick off IO for the previous mapping. Note
817 * that we will not run the very last mapping,
818 * wait_on_buffer() will do that for us
819 * through sync_buffer().
820 */
1da177e4
LT
821 brelse(bh);
822 spin_lock(lock);
823 }
824 }
825 }
826
4ee2491e
JA
827 spin_unlock(lock);
828 blk_finish_plug(&plug);
829 spin_lock(lock);
830
1da177e4
LT
831 while (!list_empty(&tmp)) {
832 bh = BH_ENTRY(tmp.prev);
1da177e4 833 get_bh(bh);
535ee2fb
JK
834 mapping = bh->b_assoc_map;
835 __remove_assoc_queue(bh);
836 /* Avoid race with mark_buffer_dirty_inode() which does
837 * a lockless check and we rely on seeing the dirty bit */
838 smp_mb();
839 if (buffer_dirty(bh)) {
840 list_add(&bh->b_assoc_buffers,
600f111e 841 &mapping->i_private_list);
535ee2fb
JK
842 bh->b_assoc_map = mapping;
843 }
1da177e4
LT
844 spin_unlock(lock);
845 wait_on_buffer(bh);
846 if (!buffer_uptodate(bh))
847 err = -EIO;
848 brelse(bh);
849 spin_lock(lock);
850 }
851
852 spin_unlock(lock);
853 err2 = osync_buffers_list(lock, list);
854 if (err)
855 return err;
856 else
857 return err2;
858}
859
860/*
861 * Invalidate any and all dirty buffers on a given inode. We are
862 * probably unmounting the fs, but that doesn't mean we have already
863 * done a sync(). Just drop the buffers from the inode list.
864 *
600f111e 865 * NOTE: we take the inode's blockdev's mapping's i_private_lock. Which
fb6f20ec 866 * assumes that all the buffers are against the blockdev.
1da177e4
LT
867 */
868void invalidate_inode_buffers(struct inode *inode)
869{
870 if (inode_has_buffers(inode)) {
871 struct address_space *mapping = &inode->i_data;
600f111e
MWO
872 struct list_head *list = &mapping->i_private_list;
873 struct address_space *buffer_mapping = mapping->i_private_data;
1da177e4 874
600f111e 875 spin_lock(&buffer_mapping->i_private_lock);
1da177e4
LT
876 while (!list_empty(list))
877 __remove_assoc_queue(BH_ENTRY(list->next));
600f111e 878 spin_unlock(&buffer_mapping->i_private_lock);
1da177e4
LT
879 }
880}
52b19ac9 881EXPORT_SYMBOL(invalidate_inode_buffers);
1da177e4
LT
882
883/*
884 * Remove any clean buffers from the inode's buffer list. This is called
885 * when we're trying to free the inode itself. Those buffers can pin it.
886 *
887 * Returns true if all buffers were removed.
888 */
889int remove_inode_buffers(struct inode *inode)
890{
891 int ret = 1;
892
893 if (inode_has_buffers(inode)) {
894 struct address_space *mapping = &inode->i_data;
600f111e
MWO
895 struct list_head *list = &mapping->i_private_list;
896 struct address_space *buffer_mapping = mapping->i_private_data;
1da177e4 897
600f111e 898 spin_lock(&buffer_mapping->i_private_lock);
1da177e4
LT
899 while (!list_empty(list)) {
900 struct buffer_head *bh = BH_ENTRY(list->next);
901 if (buffer_dirty(bh)) {
902 ret = 0;
903 break;
904 }
905 __remove_assoc_queue(bh);
906 }
600f111e 907 spin_unlock(&buffer_mapping->i_private_lock);
1da177e4
LT
908 }
909 return ret;
910}
911
912/*
c71124a8 913 * Create the appropriate buffers when given a folio for data area and
1da177e4
LT
914 * the size of each buffer.. Use the bh->b_this_page linked list to
915 * follow the buffers created. Return NULL if unable to create more
916 * buffers.
917 *
918 * The retry flag is used to differentiate async IO (paging, swapping)
919 * which may not fail from ordinary buffer allocations.
920 */
c71124a8 921struct buffer_head *folio_alloc_buffers(struct folio *folio, unsigned long size,
2a418157 922 gfp_t gfp)
1da177e4
LT
923{
924 struct buffer_head *bh, *head;
925 long offset;
b87d8cef 926 struct mem_cgroup *memcg, *old_memcg;
1da177e4 927
c71124a8
PR
928 /* The folio lock pins the memcg */
929 memcg = folio_memcg(folio);
b87d8cef 930 old_memcg = set_active_memcg(memcg);
f745c6f5 931
1da177e4 932 head = NULL;
c71124a8 933 offset = folio_size(folio);
1da177e4 934 while ((offset -= size) >= 0) {
640ab98f 935 bh = alloc_buffer_head(gfp);
1da177e4
LT
936 if (!bh)
937 goto no_grow;
938
1da177e4
LT
939 bh->b_this_page = head;
940 bh->b_blocknr = -1;
941 head = bh;
942
1da177e4
LT
943 bh->b_size = size;
944
c71124a8
PR
945 /* Link the buffer to its folio */
946 folio_set_bh(bh, folio, offset);
1da177e4 947 }
f745c6f5 948out:
b87d8cef 949 set_active_memcg(old_memcg);
1da177e4
LT
950 return head;
951/*
952 * In case anything failed, we just free everything we got.
953 */
954no_grow:
955 if (head) {
956 do {
957 bh = head;
958 head = head->b_this_page;
959 free_buffer_head(bh);
960 } while (head);
961 }
962
f745c6f5 963 goto out;
1da177e4 964}
c71124a8
PR
965EXPORT_SYMBOL_GPL(folio_alloc_buffers);
966
5c40e050 967struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size)
c71124a8 968{
2a418157 969 gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
2a418157
MWO
970
971 return folio_alloc_buffers(page_folio(page), size, gfp);
c71124a8 972}
1da177e4
LT
973EXPORT_SYMBOL_GPL(alloc_page_buffers);
974
08d84add
MWO
975static inline void link_dev_buffers(struct folio *folio,
976 struct buffer_head *head)
1da177e4
LT
977{
978 struct buffer_head *bh, *tail;
979
980 bh = head;
981 do {
982 tail = bh;
983 bh = bh->b_this_page;
984 } while (bh);
985 tail->b_this_page = head;
08d84add 986 folio_attach_private(folio, head);
1da177e4
LT
987}
988
bbec0270
LT
989static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
990{
991 sector_t retval = ~((sector_t)0);
b86058f9 992 loff_t sz = bdev_nr_bytes(bdev);
bbec0270
LT
993
994 if (sz) {
995 unsigned int sizebits = blksize_bits(size);
996 retval = (sz >> sizebits);
997 }
998 return retval;
999}
1000
1da177e4 1001/*
6f24ce6b 1002 * Initialise the state of a blockdev folio's buffers.
1da177e4 1003 */
6f24ce6b 1004static sector_t folio_init_buffers(struct folio *folio,
382497ad 1005 struct block_device *bdev, unsigned size)
1da177e4 1006{
6f24ce6b 1007 struct buffer_head *head = folio_buffers(folio);
1da177e4 1008 struct buffer_head *bh = head;
6f24ce6b 1009 bool uptodate = folio_test_uptodate(folio);
382497ad 1010 sector_t block = div_u64(folio_pos(folio), size);
bcd1d063 1011 sector_t end_block = blkdev_max_block(bdev, size);
1da177e4
LT
1012
1013 do {
1014 if (!buffer_mapped(bh)) {
01950a34
EB
1015 bh->b_end_io = NULL;
1016 bh->b_private = NULL;
1da177e4
LT
1017 bh->b_bdev = bdev;
1018 bh->b_blocknr = block;
1019 if (uptodate)
1020 set_buffer_uptodate(bh);
080399aa
JM
1021 if (block < end_block)
1022 set_buffer_mapped(bh);
1da177e4
LT
1023 }
1024 block++;
1025 bh = bh->b_this_page;
1026 } while (bh != head);
676ce6d5
HD
1027
1028 /*
1029 * Caller needs to validate requested block against end of device.
1030 */
1031 return end_block;
1da177e4
LT
1032}
1033
1034/*
6d840a18 1035 * Create the page-cache folio that contains the requested block.
1da177e4 1036 *
676ce6d5 1037 * This is used purely for blockdev mappings.
6d840a18 1038 *
bcd30d4c
MWO
1039 * Returns false if we have a failure which cannot be cured by retrying
1040 * without sleeping. Returns true if we succeeded, or the caller should retry.
1da177e4 1041 */
6d840a18 1042static bool grow_dev_folio(struct block_device *bdev, sector_t block,
382497ad 1043 pgoff_t index, unsigned size, gfp_t gfp)
1da177e4 1044{
22f89a4f 1045 struct address_space *mapping = bdev->bd_mapping;
3c98a41c 1046 struct folio *folio;
1da177e4 1047 struct buffer_head *bh;
6d840a18 1048 sector_t end_block = 0;
84235de3 1049
22f89a4f 1050 folio = __filemap_get_folio(mapping, index,
3ed65f04
MWO
1051 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
1052 if (IS_ERR(folio))
6d840a18 1053 return false;
1da177e4 1054
3c98a41c
MWO
1055 bh = folio_buffers(folio);
1056 if (bh) {
1da177e4 1057 if (bh->b_size == size) {
382497ad 1058 end_block = folio_init_buffers(folio, bdev, size);
6d840a18 1059 goto unlock;
1da177e4 1060 }
6d840a18 1061
bcd30d4c
MWO
1062 /*
1063 * Retrying may succeed; for example the folio may finish
1064 * writeback, or buffers may be cleaned. This should not
1065 * happen very often; maybe we have old buffers attached to
1066 * this blockdev's page cache and we're trying to change
1067 * the block size?
1068 */
1069 if (!try_to_free_buffers(folio)) {
1070 end_block = ~0ULL;
6d840a18 1071 goto unlock;
1da177e4 1072 }
1da177e4
LT
1073 }
1074
3ed65f04
MWO
1075 bh = folio_alloc_buffers(folio, size, gfp | __GFP_ACCOUNT);
1076 if (!bh)
6d840a18 1077 goto unlock;
1da177e4
LT
1078
1079 /*
3c98a41c 1080 * Link the folio to the buffers and initialise them. Take the
1da177e4 1081 * lock to be atomic wrt __find_get_block(), which does not
3c98a41c 1082 * run under the folio lock.
1da177e4 1083 */
22f89a4f 1084 spin_lock(&mapping->i_private_lock);
08d84add 1085 link_dev_buffers(folio, bh);
382497ad 1086 end_block = folio_init_buffers(folio, bdev, size);
22f89a4f 1087 spin_unlock(&mapping->i_private_lock);
6d840a18 1088unlock:
3c98a41c
MWO
1089 folio_unlock(folio);
1090 folio_put(folio);
6d840a18 1091 return block < end_block;
1da177e4
LT
1092}
1093
1094/*
6d840a18
MWO
1095 * Create buffers for the specified block device block's folio. If
1096 * that folio was dirty, the buffers are set dirty also. Returns false
1097 * if we've hit a permanent error.
1da177e4 1098 */
6d840a18
MWO
1099static bool grow_buffers(struct block_device *bdev, sector_t block,
1100 unsigned size, gfp_t gfp)
1da177e4 1101{
5f3bd90d 1102 loff_t pos;
1da177e4 1103
e5657933 1104 /*
5f3bd90d
MWO
1105 * Check for a block which lies outside our maximum possible
1106 * pagecache index.
e5657933 1107 */
5f3bd90d
MWO
1108 if (check_mul_overflow(block, (sector_t)size, &pos) || pos > MAX_LFS_FILESIZE) {
1109 printk(KERN_ERR "%s: requested out-of-range block %llu for device %pg\n",
8e24eea7 1110 __func__, (unsigned long long)block,
a1c6f057 1111 bdev);
6d840a18 1112 return false;
e5657933 1113 }
676ce6d5 1114
6d840a18 1115 /* Create a folio with the proper size buffers */
5f3bd90d 1116 return grow_dev_folio(bdev, block, pos / PAGE_SIZE, size, gfp);
1da177e4
LT
1117}
1118
0026ba40 1119static struct buffer_head *
3b5e6454
GK
1120__getblk_slow(struct block_device *bdev, sector_t block,
1121 unsigned size, gfp_t gfp)
1da177e4 1122{
fb27226c
DB
1123 bool blocking = gfpflags_allow_blocking(gfp);
1124
1da177e4 1125 /* Size must be multiple of hard sectorsize */
e1defc4f 1126 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
1da177e4
LT
1127 (size < 512 || size > PAGE_SIZE))) {
1128 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1129 size);
e1defc4f
MP
1130 printk(KERN_ERR "logical block size: %d\n",
1131 bdev_logical_block_size(bdev));
1da177e4
LT
1132
1133 dump_stack();
1134 return NULL;
1135 }
1136
676ce6d5
HD
1137 for (;;) {
1138 struct buffer_head *bh;
1da177e4 1139
98a6ca16
DB
1140 if (!grow_buffers(bdev, block, size, gfp))
1141 return NULL;
1142
fb27226c
DB
1143 if (blocking)
1144 bh = __find_get_block_nonatomic(bdev, block, size);
1145 else
1146 bh = __find_get_block(bdev, block, size);
1da177e4
LT
1147 if (bh)
1148 return bh;
1da177e4
LT
1149 }
1150}
1151
1152/*
1153 * The relationship between dirty buffers and dirty pages:
1154 *
1155 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
ec82e1c1 1156 * the page is tagged dirty in the page cache.
1da177e4
LT
1157 *
1158 * At all times, the dirtiness of the buffers represents the dirtiness of
1159 * subsections of the page. If the page has buffers, the page dirty bit is
1160 * merely a hint about the true dirty state.
1161 *
1162 * When a page is set dirty in its entirety, all its buffers are marked dirty
1163 * (if the page has buffers).
1164 *
1165 * When a buffer is marked dirty, its page is dirtied, but the page's other
1166 * buffers are not.
1167 *
1168 * Also. When blockdev buffers are explicitly read with bread(), they
1169 * individually become uptodate. But their backing page remains not
1170 * uptodate - even if all of its buffers are uptodate. A subsequent
2c69e205
MWO
1171 * block_read_full_folio() against that folio will discover all the uptodate
1172 * buffers, will set the folio uptodate and will perform no I/O.
1da177e4
LT
1173 */
1174
1175/**
1176 * mark_buffer_dirty - mark a buffer_head as needing writeout
67be2dd1 1177 * @bh: the buffer_head to mark dirty
1da177e4 1178 *
ec82e1c1
MW
1179 * mark_buffer_dirty() will set the dirty bit against the buffer, then set
1180 * its backing page dirty, then tag the page as dirty in the page cache
1181 * and then attach the address_space's inode to its superblock's dirty
1da177e4
LT
1182 * inode list.
1183 *
600f111e 1184 * mark_buffer_dirty() is atomic. It takes bh->b_folio->mapping->i_private_lock,
b93b0163 1185 * i_pages lock and mapping->host->i_lock.
1da177e4 1186 */
fc9b52cd 1187void mark_buffer_dirty(struct buffer_head *bh)
1da177e4 1188{
787d2214 1189 WARN_ON_ONCE(!buffer_uptodate(bh));
1be62dc1 1190
5305cb83
TH
1191 trace_block_dirty_buffer(bh);
1192
1be62dc1
LT
1193 /*
1194 * Very *carefully* optimize the it-is-already-dirty case.
1195 *
1196 * Don't let the final "is it dirty" escape to before we
1197 * perhaps modified the buffer.
1198 */
1199 if (buffer_dirty(bh)) {
1200 smp_mb();
1201 if (buffer_dirty(bh))
1202 return;
1203 }
1204
a8e7d49a 1205 if (!test_set_buffer_dirty(bh)) {
cf1d3417 1206 struct folio *folio = bh->b_folio;
c4843a75 1207 struct address_space *mapping = NULL;
c4843a75 1208
cf1d3417
MWO
1209 if (!folio_test_set_dirty(folio)) {
1210 mapping = folio->mapping;
8e9d78ed 1211 if (mapping)
cf1d3417 1212 __folio_mark_dirty(folio, mapping, 0);
8e9d78ed 1213 }
c4843a75
GT
1214 if (mapping)
1215 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
a8e7d49a 1216 }
1da177e4 1217}
1fe72eaa 1218EXPORT_SYMBOL(mark_buffer_dirty);
1da177e4 1219
87354e5d
JL
1220void mark_buffer_write_io_error(struct buffer_head *bh)
1221{
1222 set_buffer_write_io_error(bh);
1223 /* FIXME: do we need to set this in both places? */
abc8a8a2
MWO
1224 if (bh->b_folio && bh->b_folio->mapping)
1225 mapping_set_error(bh->b_folio->mapping, -EIO);
04679f3c 1226 if (bh->b_assoc_map)
87354e5d
JL
1227 mapping_set_error(bh->b_assoc_map, -EIO);
1228}
1229EXPORT_SYMBOL(mark_buffer_write_io_error);
1230
66924fda
MWO
1231/**
1232 * __brelse - Release a buffer.
1233 * @bh: The buffer to release.
1234 *
1235 * This variant of brelse() can be called if @bh is guaranteed to not be NULL.
1da177e4 1236 */
66924fda 1237void __brelse(struct buffer_head *bh)
1da177e4 1238{
66924fda
MWO
1239 if (atomic_read(&bh->b_count)) {
1240 put_bh(bh);
1da177e4
LT
1241 return;
1242 }
5c752ad9 1243 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1da177e4 1244}
1fe72eaa 1245EXPORT_SYMBOL(__brelse);
1da177e4 1246
b73a936f
MWO
1247/**
1248 * __bforget - Discard any dirty data in a buffer.
1249 * @bh: The buffer to forget.
1250 *
1251 * This variant of bforget() can be called if @bh is guaranteed to not
1252 * be NULL.
1da177e4
LT
1253 */
1254void __bforget(struct buffer_head *bh)
1255{
1256 clear_buffer_dirty(bh);
535ee2fb 1257 if (bh->b_assoc_map) {
abc8a8a2 1258 struct address_space *buffer_mapping = bh->b_folio->mapping;
1da177e4 1259
600f111e 1260 spin_lock(&buffer_mapping->i_private_lock);
1da177e4 1261 list_del_init(&bh->b_assoc_buffers);
58ff407b 1262 bh->b_assoc_map = NULL;
600f111e 1263 spin_unlock(&buffer_mapping->i_private_lock);
1da177e4
LT
1264 }
1265 __brelse(bh);
1266}
1fe72eaa 1267EXPORT_SYMBOL(__bforget);
1da177e4
LT
1268
1269static struct buffer_head *__bread_slow(struct buffer_head *bh)
1270{
1271 lock_buffer(bh);
1272 if (buffer_uptodate(bh)) {
1273 unlock_buffer(bh);
1274 return bh;
1275 } else {
1276 get_bh(bh);
1277 bh->b_end_io = end_buffer_read_sync;
1420c4a5 1278 submit_bh(REQ_OP_READ, bh);
1da177e4
LT
1279 wait_on_buffer(bh);
1280 if (buffer_uptodate(bh))
1281 return bh;
1282 }
1283 brelse(bh);
1284 return NULL;
1285}
1286
1287/*
1288 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1289 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1290 * refcount elevated by one when they're in an LRU. A buffer can only appear
1291 * once in a particular CPU's LRU. A single buffer can be present in multiple
1292 * CPU's LRUs at the same time.
1293 *
1294 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1295 * sb_find_get_block().
1296 *
1297 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1298 * a local interrupt disable for that.
1299 */
1300
86cf78d7 1301#define BH_LRU_SIZE 16
1da177e4
LT
1302
1303struct bh_lru {
1304 struct buffer_head *bhs[BH_LRU_SIZE];
1305};
1306
1307static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1308
1309#ifdef CONFIG_SMP
1310#define bh_lru_lock() local_irq_disable()
1311#define bh_lru_unlock() local_irq_enable()
1312#else
1313#define bh_lru_lock() preempt_disable()
1314#define bh_lru_unlock() preempt_enable()
1315#endif
1316
1317static inline void check_irqs_on(void)
1318{
1319#ifdef irqs_disabled
1320 BUG_ON(irqs_disabled());
1321#endif
1322}
1323
1324/*
241f01fb
EB
1325 * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
1326 * inserted at the front, and the buffer_head at the back if any is evicted.
1327 * Or, if already in the LRU it is moved to the front.
1da177e4
LT
1328 */
1329static void bh_lru_install(struct buffer_head *bh)
1330{
241f01fb
EB
1331 struct buffer_head *evictee = bh;
1332 struct bh_lru *b;
1333 int i;
1da177e4
LT
1334
1335 check_irqs_on();
c0226eb8
MK
1336 bh_lru_lock();
1337
8cc621d2
MK
1338 /*
1339 * the refcount of buffer_head in bh_lru prevents dropping the
1340 * attached page(i.e., try_to_free_buffers) so it could cause
1341 * failing page migration.
1342 * Skip putting upcoming bh into bh_lru until migration is done.
1343 */
8a237adf 1344 if (lru_cache_disabled() || cpu_is_isolated(smp_processor_id())) {
c0226eb8 1345 bh_lru_unlock();
8cc621d2 1346 return;
c0226eb8 1347 }
1da177e4 1348
241f01fb
EB
1349 b = this_cpu_ptr(&bh_lrus);
1350 for (i = 0; i < BH_LRU_SIZE; i++) {
1351 swap(evictee, b->bhs[i]);
1352 if (evictee == bh) {
1353 bh_lru_unlock();
1354 return;
1da177e4 1355 }
1da177e4 1356 }
1da177e4 1357
241f01fb
EB
1358 get_bh(bh);
1359 bh_lru_unlock();
1360 brelse(evictee);
1da177e4
LT
1361}
1362
1363/*
1364 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1365 */
858119e1 1366static struct buffer_head *
3991d3bd 1367lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
1da177e4
LT
1368{
1369 struct buffer_head *ret = NULL;
3991d3bd 1370 unsigned int i;
1da177e4
LT
1371
1372 check_irqs_on();
1373 bh_lru_lock();
8a237adf
MT
1374 if (cpu_is_isolated(smp_processor_id())) {
1375 bh_lru_unlock();
1376 return NULL;
1377 }
1da177e4 1378 for (i = 0; i < BH_LRU_SIZE; i++) {
c7b92516 1379 struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
1da177e4 1380
9470dd5d
ZB
1381 if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
1382 bh->b_size == size) {
1da177e4
LT
1383 if (i) {
1384 while (i) {
c7b92516
CL
1385 __this_cpu_write(bh_lrus.bhs[i],
1386 __this_cpu_read(bh_lrus.bhs[i - 1]));
1da177e4
LT
1387 i--;
1388 }
c7b92516 1389 __this_cpu_write(bh_lrus.bhs[0], bh);
1da177e4
LT
1390 }
1391 get_bh(bh);
1392 ret = bh;
1393 break;
1394 }
1395 }
1396 bh_lru_unlock();
1397 return ret;
1398}
1399
1400/*
1401 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1402 * it in the LRU and mark it as accessed. If it is not present then return
2d900eff
DB
1403 * NULL. Atomic context callers may also return NULL if the buffer is being
1404 * migrated; similarly the page is not marked accessed either.
1da177e4 1405 */
7ffe3de5
DB
1406static struct buffer_head *
1407find_get_block_common(struct block_device *bdev, sector_t block,
1408 unsigned size, bool atomic)
1da177e4
LT
1409{
1410 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1411
1412 if (bh == NULL) {
2457aec6 1413 /* __find_get_block_slow will mark the page accessed */
7ffe3de5 1414 bh = __find_get_block_slow(bdev, block, atomic);
1da177e4
LT
1415 if (bh)
1416 bh_lru_install(bh);
2457aec6 1417 } else
1da177e4 1418 touch_buffer(bh);
2457aec6 1419
1da177e4
LT
1420 return bh;
1421}
7ffe3de5
DB
1422
1423struct buffer_head *
1424__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
1425{
1426 return find_get_block_common(bdev, block, size, true);
1427}
1da177e4
LT
1428EXPORT_SYMBOL(__find_get_block);
1429
2814a7d3
DB
1430/* same as __find_get_block() but allows sleeping contexts */
1431struct buffer_head *
1432__find_get_block_nonatomic(struct block_device *bdev, sector_t block,
1433 unsigned size)
1434{
1435 return find_get_block_common(bdev, block, size, false);
1436}
1437EXPORT_SYMBOL(__find_get_block_nonatomic);
1438
3ed65f04
MWO
1439/**
1440 * bdev_getblk - Get a buffer_head in a block device's buffer cache.
1441 * @bdev: The block device.
1442 * @block: The block number.
1443 * @size: The size of buffer_heads for this @bdev.
1444 * @gfp: The memory allocation flags to use.
1445 *
0b116ff4
MWO
1446 * The returned buffer head has its reference count incremented, but is
1447 * not locked. The caller should call brelse() when it has finished
1448 * with the buffer. The buffer may not be uptodate. If needed, the
1449 * caller can bring it uptodate either by reading it or overwriting it.
1450 *
3ed65f04
MWO
1451 * Return: The buffer head, or NULL if memory could not be allocated.
1452 */
1453struct buffer_head *bdev_getblk(struct block_device *bdev, sector_t block,
1454 unsigned size, gfp_t gfp)
1455{
5b67d439
DB
1456 struct buffer_head *bh;
1457
1458 if (gfpflags_allow_blocking(gfp))
1459 bh = __find_get_block_nonatomic(bdev, block, size);
1460 else
1461 bh = __find_get_block(bdev, block, size);
3ed65f04
MWO
1462
1463 might_alloc(gfp);
1464 if (bh)
1465 return bh;
1466
1467 return __getblk_slow(bdev, block, size, gfp);
1468}
1469EXPORT_SYMBOL(bdev_getblk);
1470
1da177e4
LT
1471/*
1472 * Do async read-ahead on a buffer..
1473 */
3991d3bd 1474void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
1da177e4 1475{
775d9b10
MWO
1476 struct buffer_head *bh = bdev_getblk(bdev, block, size,
1477 GFP_NOWAIT | __GFP_MOVABLE);
1478
a3e713b5 1479 if (likely(bh)) {
e7ea1129 1480 bh_readahead(bh, REQ_RAHEAD);
a3e713b5
AM
1481 brelse(bh);
1482 }
1da177e4
LT
1483}
1484EXPORT_SYMBOL(__breadahead);
1485
1486/**
324ecaee
MWO
1487 * __bread_gfp() - Read a block.
1488 * @bdev: The block device to read from.
1489 * @block: Block number in units of block size.
1490 * @size: The block size of this device in bytes.
1491 * @gfp: Not page allocation flags; see below.
3b5e6454 1492 *
324ecaee
MWO
1493 * You are not expected to call this function. You should use one of
1494 * sb_bread(), sb_bread_unmovable() or __bread().
1495 *
1496 * Read a specified block, and return the buffer head that refers to it.
1497 * If @gfp is 0, the memory will be allocated using the block device's
1498 * default GFP flags. If @gfp is __GFP_MOVABLE, the memory may be
1499 * allocated from a movable area. Do not pass in a complete set of
1500 * GFP flags.
1501 *
1502 * The returned buffer head has its refcount increased. The caller should
1503 * call brelse() when it has finished with the buffer.
1504 *
1505 * Context: May sleep waiting for I/O.
1506 * Return: NULL if the block was unreadable.
1da177e4 1507 */
324ecaee
MWO
1508struct buffer_head *__bread_gfp(struct block_device *bdev, sector_t block,
1509 unsigned size, gfp_t gfp)
1da177e4 1510{
93b13eca
MWO
1511 struct buffer_head *bh;
1512
224941e8 1513 gfp |= mapping_gfp_constraint(bdev->bd_mapping, ~__GFP_FS);
93b13eca
MWO
1514
1515 /*
1516 * Prefer looping in the allocator rather than here, at least that
1517 * code knows what it's doing.
1518 */
1519 gfp |= __GFP_NOFAIL;
1520
1521 bh = bdev_getblk(bdev, block, size, gfp);
1da177e4 1522
a3e713b5 1523 if (likely(bh) && !buffer_uptodate(bh))
1da177e4
LT
1524 bh = __bread_slow(bh);
1525 return bh;
1526}
3b5e6454 1527EXPORT_SYMBOL(__bread_gfp);
1da177e4 1528
8cc621d2
MK
1529static void __invalidate_bh_lrus(struct bh_lru *b)
1530{
1531 int i;
1532
1533 for (i = 0; i < BH_LRU_SIZE; i++) {
1534 brelse(b->bhs[i]);
1535 b->bhs[i] = NULL;
1536 }
1537}
1da177e4
LT
1538/*
1539 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1540 * This doesn't race because it runs in each cpu either in irq
1541 * or with preempt disabled.
1542 */
1543static void invalidate_bh_lru(void *arg)
1544{
1545 struct bh_lru *b = &get_cpu_var(bh_lrus);
1da177e4 1546
8cc621d2 1547 __invalidate_bh_lrus(b);
1da177e4
LT
1548 put_cpu_var(bh_lrus);
1549}
42be35d0 1550
8cc621d2 1551bool has_bh_in_lru(int cpu, void *dummy)
42be35d0
GBY
1552{
1553 struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
1554 int i;
1da177e4 1555
42be35d0
GBY
1556 for (i = 0; i < BH_LRU_SIZE; i++) {
1557 if (b->bhs[i])
1d706679 1558 return true;
42be35d0
GBY
1559 }
1560
1d706679 1561 return false;
42be35d0
GBY
1562}
1563
f9a14399 1564void invalidate_bh_lrus(void)
1da177e4 1565{
cb923159 1566 on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1);
1da177e4 1567}
9db5579b 1568EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
1da177e4 1569
243418e3
MK
1570/*
1571 * It's called from workqueue context so we need a bh_lru_lock to close
1572 * the race with preemption/irq.
1573 */
1574void invalidate_bh_lrus_cpu(void)
8cc621d2
MK
1575{
1576 struct bh_lru *b;
1577
1578 bh_lru_lock();
243418e3 1579 b = this_cpu_ptr(&bh_lrus);
8cc621d2
MK
1580 __invalidate_bh_lrus(b);
1581 bh_lru_unlock();
1582}
1583
465e5e6a
PR
1584void folio_set_bh(struct buffer_head *bh, struct folio *folio,
1585 unsigned long offset)
1586{
1587 bh->b_folio = folio;
1588 BUG_ON(offset >= folio_size(folio));
1589 if (folio_test_highmem(folio))
1590 /*
1591 * This catches illegal uses and preserves the offset:
1592 */
1593 bh->b_data = (char *)(0 + offset);
1594 else
1595 bh->b_data = folio_address(folio) + offset;
1596}
1597EXPORT_SYMBOL(folio_set_bh);
1598
1da177e4
LT
1599/*
1600 * Called when truncating a buffer on a page completely.
1601 */
e7470ee8
MG
1602
1603/* Bits that are cleared during an invalidate */
1604#define BUFFER_FLAGS_DISCARD \
1605 (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
1606 1 << BH_Delay | 1 << BH_Unwritten)
1607
858119e1 1608static void discard_buffer(struct buffer_head * bh)
1da177e4 1609{
b0192296 1610 unsigned long b_state;
e7470ee8 1611
1da177e4
LT
1612 lock_buffer(bh);
1613 clear_buffer_dirty(bh);
1614 bh->b_bdev = NULL;
b0192296
UB
1615 b_state = READ_ONCE(bh->b_state);
1616 do {
8e184bf1
DB
1617 } while (!try_cmpxchg_relaxed(&bh->b_state, &b_state,
1618 b_state & ~BUFFER_FLAGS_DISCARD));
1da177e4
LT
1619 unlock_buffer(bh);
1620}
1621
1da177e4 1622/**
7ba13abb
MWO
1623 * block_invalidate_folio - Invalidate part or all of a buffer-backed folio.
1624 * @folio: The folio which is affected.
d47992f8
LC
1625 * @offset: start of the range to invalidate
1626 * @length: length of the range to invalidate
1da177e4 1627 *
7ba13abb 1628 * block_invalidate_folio() is called when all or part of the folio has been
814e1d25 1629 * invalidated by a truncate operation.
1da177e4 1630 *
7ba13abb 1631 * block_invalidate_folio() does not have to release all buffers, but it must
1da177e4
LT
1632 * ensure that no dirty buffer is left outside @offset and that no I/O
1633 * is underway against any of the blocks which are outside the truncation
1634 * point. Because the caller is about to free (and possibly reuse) those
1635 * blocks on-disk.
1636 */
7ba13abb 1637void block_invalidate_folio(struct folio *folio, size_t offset, size_t length)
1da177e4
LT
1638{
1639 struct buffer_head *head, *bh, *next;
7ba13abb
MWO
1640 size_t curr_off = 0;
1641 size_t stop = length + offset;
1da177e4 1642
7ba13abb 1643 BUG_ON(!folio_test_locked(folio));
1da177e4 1644
d47992f8
LC
1645 /*
1646 * Check for overflow
1647 */
7ba13abb
MWO
1648 BUG_ON(stop > folio_size(folio) || stop < length);
1649
1650 head = folio_buffers(folio);
1651 if (!head)
1652 return;
d47992f8 1653
1da177e4
LT
1654 bh = head;
1655 do {
7ba13abb 1656 size_t next_off = curr_off + bh->b_size;
1da177e4
LT
1657 next = bh->b_this_page;
1658
d47992f8
LC
1659 /*
1660 * Are we still fully in range ?
1661 */
1662 if (next_off > stop)
1663 goto out;
1664
1da177e4
LT
1665 /*
1666 * is this block fully invalidated?
1667 */
1668 if (offset <= curr_off)
1669 discard_buffer(bh);
1670 curr_off = next_off;
1671 bh = next;
1672 } while (bh != head);
1673
1674 /*
7ba13abb 1675 * We release buffers only if the entire folio is being invalidated.
1da177e4
LT
1676 * The get_block cached value has been unconditionally invalidated,
1677 * so real IO is not possible anymore.
1678 */
7ba13abb
MWO
1679 if (length == folio_size(folio))
1680 filemap_release_folio(folio, 0);
1da177e4 1681out:
9c33d85e 1682 folio_clear_mappedtodisk(folio);
1da177e4 1683}
7ba13abb 1684EXPORT_SYMBOL(block_invalidate_folio);
1da177e4
LT
1685
1686/*
1687 * We attach and possibly dirty the buffers atomically wrt
600f111e 1688 * block_dirty_folio() via i_private_lock. try_to_free_buffers
8e2e1756 1689 * is already excluded via the folio lock.
1da177e4 1690 */
0a88810d 1691struct buffer_head *create_empty_buffers(struct folio *folio,
3decb856 1692 unsigned long blocksize, unsigned long b_state)
1da177e4
LT
1693{
1694 struct buffer_head *bh, *head, *tail;
2a418157 1695 gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT | __GFP_NOFAIL;
1da177e4 1696
2a418157 1697 head = folio_alloc_buffers(folio, blocksize, gfp);
1da177e4
LT
1698 bh = head;
1699 do {
1700 bh->b_state |= b_state;
1701 tail = bh;
1702 bh = bh->b_this_page;
1703 } while (bh);
1704 tail->b_this_page = head;
1705
600f111e 1706 spin_lock(&folio->mapping->i_private_lock);
8e2e1756 1707 if (folio_test_uptodate(folio) || folio_test_dirty(folio)) {
1da177e4
LT
1708 bh = head;
1709 do {
8e2e1756 1710 if (folio_test_dirty(folio))
1da177e4 1711 set_buffer_dirty(bh);
8e2e1756 1712 if (folio_test_uptodate(folio))
1da177e4
LT
1713 set_buffer_uptodate(bh);
1714 bh = bh->b_this_page;
1715 } while (bh != head);
1716 }
8e2e1756 1717 folio_attach_private(folio, head);
600f111e 1718 spin_unlock(&folio->mapping->i_private_lock);
3decb856
MWO
1719
1720 return head;
8e2e1756 1721}
1da177e4
LT
1722EXPORT_SYMBOL(create_empty_buffers);
1723
29f3ad7d
JK
1724/**
1725 * clean_bdev_aliases: clean a range of buffers in block device
1726 * @bdev: Block device to clean buffers in
1727 * @block: Start of a range of blocks to clean
1728 * @len: Number of blocks to clean
1da177e4 1729 *
29f3ad7d
JK
1730 * We are taking a range of blocks for data and we don't want writeback of any
1731 * buffer-cache aliases starting from return from this function and until the
1732 * moment when something will explicitly mark the buffer dirty (hopefully that
1733 * will not happen until we will free that block ;-) We don't even need to mark
1734 * it not-uptodate - nobody can expect anything from a newly allocated buffer
1735 * anyway. We used to use unmap_buffer() for such invalidation, but that was
1736 * wrong. We definitely don't want to mark the alias unmapped, for example - it
1737 * would confuse anyone who might pick it with bread() afterwards...
1738 *
1739 * Also.. Note that bforget() doesn't lock the buffer. So there can be
1740 * writeout I/O going on against recently-freed buffers. We don't wait on that
1741 * I/O in bforget() - it's more efficient to wait on the I/O only if we really
1742 * need to. That happens here.
1da177e4 1743 */
29f3ad7d 1744void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
1da177e4 1745{
53cd4cd3
AV
1746 struct address_space *bd_mapping = bdev->bd_mapping;
1747 const int blkbits = bd_mapping->host->i_blkbits;
9e0b6f31 1748 struct folio_batch fbatch;
53cd4cd3 1749 pgoff_t index = ((loff_t)block << blkbits) / PAGE_SIZE;
29f3ad7d 1750 pgoff_t end;
c10f778d 1751 int i, count;
29f3ad7d
JK
1752 struct buffer_head *bh;
1753 struct buffer_head *head;
1da177e4 1754
53cd4cd3 1755 end = ((loff_t)(block + len - 1) << blkbits) / PAGE_SIZE;
9e0b6f31
MWO
1756 folio_batch_init(&fbatch);
1757 while (filemap_get_folios(bd_mapping, &index, end, &fbatch)) {
1758 count = folio_batch_count(&fbatch);
c10f778d 1759 for (i = 0; i < count; i++) {
9e0b6f31 1760 struct folio *folio = fbatch.folios[i];
1da177e4 1761
9e0b6f31 1762 if (!folio_buffers(folio))
29f3ad7d
JK
1763 continue;
1764 /*
600f111e 1765 * We use folio lock instead of bd_mapping->i_private_lock
29f3ad7d
JK
1766 * to pin buffers here since we can afford to sleep and
1767 * it scales better than a global spinlock lock.
1768 */
9e0b6f31
MWO
1769 folio_lock(folio);
1770 /* Recheck when the folio is locked which pins bhs */
1771 head = folio_buffers(folio);
1772 if (!head)
29f3ad7d 1773 goto unlock_page;
29f3ad7d
JK
1774 bh = head;
1775 do {
6c006a9d 1776 if (!buffer_mapped(bh) || (bh->b_blocknr < block))
29f3ad7d
JK
1777 goto next;
1778 if (bh->b_blocknr >= block + len)
1779 break;
1780 clear_buffer_dirty(bh);
1781 wait_on_buffer(bh);
1782 clear_buffer_req(bh);
1783next:
1784 bh = bh->b_this_page;
1785 } while (bh != head);
1786unlock_page:
9e0b6f31 1787 folio_unlock(folio);
29f3ad7d 1788 }
9e0b6f31 1789 folio_batch_release(&fbatch);
29f3ad7d 1790 cond_resched();
c10f778d
JK
1791 /* End of range already reached? */
1792 if (index > end || !index)
1793 break;
1da177e4
LT
1794 }
1795}
29f3ad7d 1796EXPORT_SYMBOL(clean_bdev_aliases);
1da177e4 1797
c6c8c3e7
PR
1798static struct buffer_head *folio_create_buffers(struct folio *folio,
1799 struct inode *inode,
1800 unsigned int b_state)
45bce8f3 1801{
3decb856
MWO
1802 struct buffer_head *bh;
1803
c6c8c3e7 1804 BUG_ON(!folio_test_locked(folio));
45bce8f3 1805
3decb856
MWO
1806 bh = folio_buffers(folio);
1807 if (!bh)
0a88810d 1808 bh = create_empty_buffers(folio,
3decb856
MWO
1809 1 << READ_ONCE(inode->i_blkbits), b_state);
1810 return bh;
45bce8f3
LT
1811}
1812
1da177e4
LT
1813/*
1814 * NOTE! All mapped/uptodate combinations are valid:
1815 *
1816 * Mapped Uptodate Meaning
1817 *
1818 * No No "unknown" - must do get_block()
1819 * No Yes "hole" - zero-filled
1820 * Yes No "allocated" - allocated on disk, not read in
1821 * Yes Yes "valid" - allocated and up-to-date in memory.
1822 *
1823 * "Dirty" is valid only with the last case (mapped+uptodate).
1824 */
1825
1826/*
17bf23a9 1827 * While block_write_full_folio is writing back the dirty buffers under
1da177e4
LT
1828 * the page lock, whoever dirtied the buffers may decide to clean them
1829 * again at any time. We handle that by only looking at the buffer
1830 * state inside lock_buffer().
1831 *
17bf23a9 1832 * If block_write_full_folio() is called for regular writeback
1da177e4
LT
1833 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1834 * locked buffer. This only can happen if someone has written the buffer
1835 * directly, with submit_bh(). At the address_space level PageWriteback
1836 * prevents this contention from occurring.
6e34eedd 1837 *
17bf23a9 1838 * If block_write_full_folio() is called with wbc->sync_mode ==
70fd7614 1839 * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
721a9602 1840 * causes the writes to be flagged as synchronous writes.
1da177e4 1841 */
53418a18 1842int __block_write_full_folio(struct inode *inode, struct folio *folio,
14059f66 1843 get_block_t *get_block, struct writeback_control *wbc)
1da177e4
LT
1844{
1845 int err;
1846 sector_t block;
1847 sector_t last_block;
f0fbd5fc 1848 struct buffer_head *bh, *head;
fa399c31 1849 size_t blocksize;
1da177e4 1850 int nr_underway = 0;
3ae72869 1851 blk_opf_t write_flags = wbc_to_write_flags(wbc);
1da177e4 1852
53418a18 1853 head = folio_create_buffers(folio, inode,
c6c8c3e7 1854 (1 << BH_Dirty) | (1 << BH_Uptodate));
1da177e4
LT
1855
1856 /*
e621900a 1857 * Be very careful. We have no exclusion from block_dirty_folio
1da177e4
LT
1858 * here, and the (potentially unmapped) buffers may become dirty at
1859 * any time. If a buffer becomes dirty here after we've inspected it
53418a18 1860 * then we just miss that fact, and the folio stays dirty.
1da177e4 1861 *
e621900a 1862 * Buffers outside i_size may be dirtied by block_dirty_folio;
1da177e4
LT
1863 * handle that here by just cleaning them.
1864 */
1865
1da177e4 1866 bh = head;
45bce8f3 1867 blocksize = bh->b_size;
45bce8f3 1868
fa399c31
MWO
1869 block = div_u64(folio_pos(folio), blocksize);
1870 last_block = div_u64(i_size_read(inode) - 1, blocksize);
1da177e4
LT
1871
1872 /*
1873 * Get all the dirty buffers mapped to disk addresses and
1874 * handle any aliases from the underlying blockdev's mapping.
1875 */
1876 do {
1877 if (block > last_block) {
1878 /*
1879 * mapped buffers outside i_size will occur, because
53418a18 1880 * this folio can be outside i_size when there is a
1da177e4
LT
1881 * truncate in progress.
1882 */
1883 /*
17bf23a9 1884 * The buffer was zeroed by block_write_full_folio()
1da177e4
LT
1885 */
1886 clear_buffer_dirty(bh);
1887 set_buffer_uptodate(bh);
29a814d2
AT
1888 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1889 buffer_dirty(bh)) {
b0cf2321 1890 WARN_ON(bh->b_size != blocksize);
1da177e4
LT
1891 err = get_block(inode, block, bh, 1);
1892 if (err)
1893 goto recover;
29a814d2 1894 clear_buffer_delay(bh);
1da177e4
LT
1895 if (buffer_new(bh)) {
1896 /* blockdev mappings never come here */
1897 clear_buffer_new(bh);
e64855c6 1898 clean_bdev_bh_alias(bh);
1da177e4
LT
1899 }
1900 }
1901 bh = bh->b_this_page;
1902 block++;
1903 } while (bh != head);
1904
1905 do {
1da177e4
LT
1906 if (!buffer_mapped(bh))
1907 continue;
1908 /*
1909 * If it's a fully non-blocking write attempt and we cannot
53418a18 1910 * lock the buffer then redirty the folio. Note that this can
5b0830cb
JA
1911 * potentially cause a busy-wait loop from writeback threads
1912 * and kswapd activity, but those code paths have their own
1913 * higher-level throttling.
1da177e4 1914 */
1b430bee 1915 if (wbc->sync_mode != WB_SYNC_NONE) {
1da177e4 1916 lock_buffer(bh);
ca5de404 1917 } else if (!trylock_buffer(bh)) {
53418a18 1918 folio_redirty_for_writepage(wbc, folio);
1da177e4
LT
1919 continue;
1920 }
1921 if (test_clear_buffer_dirty(bh)) {
14059f66
MWO
1922 mark_buffer_async_write_endio(bh,
1923 end_buffer_async_write);
1da177e4
LT
1924 } else {
1925 unlock_buffer(bh);
1926 }
1927 } while ((bh = bh->b_this_page) != head);
1928
1929 /*
53418a18
MWO
1930 * The folio and its buffers are protected by the writeback flag,
1931 * so we can drop the bh refcounts early.
1da177e4 1932 */
53418a18
MWO
1933 BUG_ON(folio_test_writeback(folio));
1934 folio_start_writeback(folio);
1da177e4
LT
1935
1936 do {
1937 struct buffer_head *next = bh->b_this_page;
1938 if (buffer_async_write(bh)) {
44981351
BVA
1939 submit_bh_wbc(REQ_OP_WRITE | write_flags, bh,
1940 inode->i_write_hint, wbc);
1da177e4
LT
1941 nr_underway++;
1942 }
1da177e4
LT
1943 bh = next;
1944 } while (bh != head);
53418a18 1945 folio_unlock(folio);
1da177e4
LT
1946
1947 err = 0;
1948done:
1949 if (nr_underway == 0) {
1950 /*
53418a18 1951 * The folio was marked dirty, but the buffers were
1da177e4 1952 * clean. Someone wrote them back by hand with
79f59784 1953 * write_dirty_buffer/submit_bh. A rare case.
1da177e4 1954 */
53418a18 1955 folio_end_writeback(folio);
3d67f2d7 1956
1da177e4 1957 /*
53418a18 1958 * The folio and buffer_heads can be released at any time from
1da177e4
LT
1959 * here on.
1960 */
1da177e4
LT
1961 }
1962 return err;
1963
1964recover:
1965 /*
1966 * ENOSPC, or some other error. We may already have added some
1967 * blocks to the file, so we need to write these out to avoid
1968 * exposing stale data.
53418a18 1969 * The folio is currently locked and not marked for writeback
1da177e4
LT
1970 */
1971 bh = head;
1972 /* Recovery: lock and submit the mapped buffers */
1973 do {
29a814d2
AT
1974 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1975 !buffer_delay(bh)) {
1da177e4 1976 lock_buffer(bh);
14059f66
MWO
1977 mark_buffer_async_write_endio(bh,
1978 end_buffer_async_write);
1da177e4
LT
1979 } else {
1980 /*
1981 * The buffer may have been set dirty during
53418a18 1982 * attachment to a dirty folio.
1da177e4
LT
1983 */
1984 clear_buffer_dirty(bh);
1985 }
1986 } while ((bh = bh->b_this_page) != head);
53418a18
MWO
1987 BUG_ON(folio_test_writeback(folio));
1988 mapping_set_error(folio->mapping, err);
1989 folio_start_writeback(folio);
1da177e4
LT
1990 do {
1991 struct buffer_head *next = bh->b_this_page;
1992 if (buffer_async_write(bh)) {
1993 clear_buffer_dirty(bh);
44981351
BVA
1994 submit_bh_wbc(REQ_OP_WRITE | write_flags, bh,
1995 inode->i_write_hint, wbc);
1da177e4
LT
1996 nr_underway++;
1997 }
1da177e4
LT
1998 bh = next;
1999 } while (bh != head);
53418a18 2000 folio_unlock(folio);
1da177e4
LT
2001 goto done;
2002}
53418a18 2003EXPORT_SYMBOL(__block_write_full_folio);
1da177e4 2004
afddba49 2005/*
4a9622f2 2006 * If a folio has any new buffers, zero them out here, and mark them uptodate
afddba49
NP
2007 * and dirty so they'll be written out (in order to prevent uninitialised
2008 * block data from leaking). And clear the new bit.
2009 */
4a9622f2 2010void folio_zero_new_buffers(struct folio *folio, size_t from, size_t to)
afddba49 2011{
4a9622f2 2012 size_t block_start, block_end;
afddba49
NP
2013 struct buffer_head *head, *bh;
2014
4a9622f2
MWO
2015 BUG_ON(!folio_test_locked(folio));
2016 head = folio_buffers(folio);
2017 if (!head)
afddba49
NP
2018 return;
2019
4a9622f2 2020 bh = head;
afddba49
NP
2021 block_start = 0;
2022 do {
2023 block_end = block_start + bh->b_size;
2024
2025 if (buffer_new(bh)) {
2026 if (block_end > from && block_start < to) {
4a9622f2
MWO
2027 if (!folio_test_uptodate(folio)) {
2028 size_t start, xend;
afddba49
NP
2029
2030 start = max(from, block_start);
4a9622f2 2031 xend = min(to, block_end);
afddba49 2032
4a9622f2 2033 folio_zero_segment(folio, start, xend);
afddba49
NP
2034 set_buffer_uptodate(bh);
2035 }
2036
2037 clear_buffer_new(bh);
2038 mark_buffer_dirty(bh);
2039 }
2040 }
2041
2042 block_start = block_end;
2043 bh = bh->b_this_page;
2044 } while (bh != head);
2045}
4a9622f2 2046EXPORT_SYMBOL(folio_zero_new_buffers);
afddba49 2047
4aa8cdd5 2048static int
ae259a9c 2049iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
6d49cc85 2050 const struct iomap *iomap)
ae259a9c 2051{
80844194 2052 loff_t offset = (loff_t)block << inode->i_blkbits;
ae259a9c
CH
2053
2054 bh->b_bdev = iomap->bdev;
2055
2056 /*
2057 * Block points to offset in file we need to map, iomap contains
2058 * the offset at which the map starts. If the map ends before the
2059 * current block, then do not map the buffer and let the caller
2060 * handle it.
2061 */
4aa8cdd5
CH
2062 if (offset >= iomap->offset + iomap->length)
2063 return -EIO;
ae259a9c
CH
2064
2065 switch (iomap->type) {
2066 case IOMAP_HOLE:
2067 /*
2068 * If the buffer is not up to date or beyond the current EOF,
2069 * we need to mark it as new to ensure sub-block zeroing is
2070 * executed if necessary.
2071 */
2072 if (!buffer_uptodate(bh) ||
2073 (offset >= i_size_read(inode)))
2074 set_buffer_new(bh);
4aa8cdd5 2075 return 0;
ae259a9c
CH
2076 case IOMAP_DELALLOC:
2077 if (!buffer_uptodate(bh) ||
2078 (offset >= i_size_read(inode)))
2079 set_buffer_new(bh);
2080 set_buffer_uptodate(bh);
2081 set_buffer_mapped(bh);
2082 set_buffer_delay(bh);
4aa8cdd5 2083 return 0;
ae259a9c
CH
2084 case IOMAP_UNWRITTEN:
2085 /*
3d7b6b21
AG
2086 * For unwritten regions, we always need to ensure that regions
2087 * in the block we are not writing to are zeroed. Mark the
2088 * buffer as new to ensure this.
ae259a9c
CH
2089 */
2090 set_buffer_new(bh);
2091 set_buffer_unwritten(bh);
df561f66 2092 fallthrough;
ae259a9c 2093 case IOMAP_MAPPED:
3d7b6b21 2094 if ((iomap->flags & IOMAP_F_NEW) ||
381c0432
CH
2095 offset >= i_size_read(inode)) {
2096 /*
2097 * This can happen if truncating the block device races
2098 * with the check in the caller as i_size updates on
2099 * block devices aren't synchronized by i_rwsem for
2100 * block devices.
2101 */
2102 if (S_ISBLK(inode->i_mode))
2103 return -EIO;
ae259a9c 2104 set_buffer_new(bh);
381c0432 2105 }
19fe5f64
AG
2106 bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
2107 inode->i_blkbits;
ae259a9c 2108 set_buffer_mapped(bh);
4aa8cdd5
CH
2109 return 0;
2110 default:
2111 WARN_ON_ONCE(1);
2112 return -EIO;
ae259a9c
CH
2113 }
2114}
2115
d1bd0b4e 2116int __block_write_begin_int(struct folio *folio, loff_t pos, unsigned len,
6d49cc85 2117 get_block_t *get_block, const struct iomap *iomap)
1da177e4 2118{
b0619401
MWO
2119 size_t from = offset_in_folio(folio, pos);
2120 size_t to = from + len;
d1bd0b4e 2121 struct inode *inode = folio->mapping->host;
b0619401 2122 size_t block_start, block_end;
1da177e4
LT
2123 sector_t block;
2124 int err = 0;
b0619401 2125 size_t blocksize;
1da177e4
LT
2126 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
2127
d1bd0b4e 2128 BUG_ON(!folio_test_locked(folio));
b0619401 2129 BUG_ON(to > folio_size(folio));
1da177e4
LT
2130 BUG_ON(from > to);
2131
c6c8c3e7 2132 head = folio_create_buffers(folio, inode, 0);
45bce8f3 2133 blocksize = head->b_size;
b0619401 2134 block = div_u64(folio_pos(folio), blocksize);
1da177e4 2135
b0619401 2136 for (bh = head, block_start = 0; bh != head || !block_start;
1da177e4
LT
2137 block++, block_start=block_end, bh = bh->b_this_page) {
2138 block_end = block_start + blocksize;
2139 if (block_end <= from || block_start >= to) {
d1bd0b4e 2140 if (folio_test_uptodate(folio)) {
1da177e4
LT
2141 if (!buffer_uptodate(bh))
2142 set_buffer_uptodate(bh);
2143 }
2144 continue;
2145 }
2146 if (buffer_new(bh))
2147 clear_buffer_new(bh);
2148 if (!buffer_mapped(bh)) {
b0cf2321 2149 WARN_ON(bh->b_size != blocksize);
4aa8cdd5 2150 if (get_block)
ae259a9c 2151 err = get_block(inode, block, bh, 1);
4aa8cdd5
CH
2152 else
2153 err = iomap_to_bh(inode, block, bh, iomap);
2154 if (err)
2155 break;
ae259a9c 2156
1da177e4 2157 if (buffer_new(bh)) {
e64855c6 2158 clean_bdev_bh_alias(bh);
d1bd0b4e 2159 if (folio_test_uptodate(folio)) {
637aff46 2160 clear_buffer_new(bh);
1da177e4 2161 set_buffer_uptodate(bh);
637aff46 2162 mark_buffer_dirty(bh);
1da177e4
LT
2163 continue;
2164 }
eebd2aa3 2165 if (block_end > to || block_start < from)
d1bd0b4e 2166 folio_zero_segments(folio,
eebd2aa3
CL
2167 to, block_end,
2168 block_start, from);
1da177e4
LT
2169 continue;
2170 }
2171 }
d1bd0b4e 2172 if (folio_test_uptodate(folio)) {
1da177e4
LT
2173 if (!buffer_uptodate(bh))
2174 set_buffer_uptodate(bh);
2175 continue;
2176 }
2177 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
33a266dd 2178 !buffer_unwritten(bh) &&
1da177e4 2179 (block_start < from || block_end > to)) {
e7ea1129 2180 bh_read_nowait(bh, 0);
1da177e4
LT
2181 *wait_bh++=bh;
2182 }
2183 }
2184 /*
2185 * If we issued read requests - let them complete.
2186 */
2187 while(wait_bh > wait) {
2188 wait_on_buffer(*--wait_bh);
2189 if (!buffer_uptodate(*wait_bh))
f3ddbdc6 2190 err = -EIO;
1da177e4 2191 }
f9f07b6c 2192 if (unlikely(err))
4a9622f2 2193 folio_zero_new_buffers(folio, from, to);
1da177e4
LT
2194 return err;
2195}
ae259a9c 2196
9f04609f 2197int __block_write_begin(struct folio *folio, loff_t pos, unsigned len,
ae259a9c
CH
2198 get_block_t *get_block)
2199{
9f04609f 2200 return __block_write_begin_int(folio, pos, len, get_block, NULL);
ae259a9c 2201}
ebdec241 2202EXPORT_SYMBOL(__block_write_begin);
1da177e4 2203
63a23847 2204void block_commit_write(struct folio *folio, size_t from, size_t to)
1da177e4 2205{
8c6cb3e3
MWO
2206 size_t block_start, block_end;
2207 bool partial = false;
1da177e4
LT
2208 unsigned blocksize;
2209 struct buffer_head *bh, *head;
2210
8c6cb3e3 2211 bh = head = folio_buffers(folio);
83f4414b
WG
2212 if (!bh)
2213 return;
45bce8f3 2214 blocksize = bh->b_size;
1da177e4 2215
45bce8f3
LT
2216 block_start = 0;
2217 do {
1da177e4
LT
2218 block_end = block_start + blocksize;
2219 if (block_end <= from || block_start >= to) {
2220 if (!buffer_uptodate(bh))
8c6cb3e3 2221 partial = true;
1da177e4
LT
2222 } else {
2223 set_buffer_uptodate(bh);
2224 mark_buffer_dirty(bh);
2225 }
4ebd3aec
YG
2226 if (buffer_new(bh))
2227 clear_buffer_new(bh);
45bce8f3
LT
2228
2229 block_start = block_end;
2230 bh = bh->b_this_page;
2231 } while (bh != head);
1da177e4
LT
2232
2233 /*
2234 * If this is a partial write which happened to make all buffers
2c69e205 2235 * uptodate then we can optimize away a bogus read_folio() for
8c6cb3e3 2236 * the next read(). Here we 'discover' whether the folio went
1da177e4
LT
2237 * uptodate as a result of this (potentially partial) write.
2238 */
2239 if (!partial)
8c6cb3e3 2240 folio_mark_uptodate(folio);
1da177e4 2241}
63a23847 2242EXPORT_SYMBOL(block_commit_write);
1da177e4 2243
afddba49 2244/*
155130a4
CH
2245 * block_write_begin takes care of the basic task of block allocation and
2246 * bringing partial write blocks uptodate first.
2247 *
7bb46a67 2248 * The filesystem needs to handle block truncation upon failure.
afddba49 2249 */
155130a4 2250int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
1da86618 2251 struct folio **foliop, get_block_t *get_block)
afddba49 2252{
09cbfeaf 2253 pgoff_t index = pos >> PAGE_SHIFT;
8eb835a1 2254 struct folio *folio;
6e1db88d 2255 int status;
afddba49 2256
8eb835a1
MWO
2257 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
2258 mapping_gfp_mask(mapping));
2259 if (IS_ERR(folio))
2260 return PTR_ERR(folio);
afddba49 2261
8eb835a1 2262 status = __block_write_begin_int(folio, pos, len, get_block, NULL);
afddba49 2263 if (unlikely(status)) {
8eb835a1
MWO
2264 folio_unlock(folio);
2265 folio_put(folio);
2266 folio = NULL;
afddba49
NP
2267 }
2268
1da86618 2269 *foliop = folio;
afddba49
NP
2270 return status;
2271}
2272EXPORT_SYMBOL(block_write_begin);
2273
2274int block_write_end(struct file *file, struct address_space *mapping,
2275 loff_t pos, unsigned len, unsigned copied,
97edbc02 2276 struct folio *folio, void *fsdata)
afddba49 2277{
8c6cb3e3 2278 size_t start = pos - folio_pos(folio);
afddba49
NP
2279
2280 if (unlikely(copied < len)) {
2281 /*
2c69e205
MWO
2282 * The buffers that were written will now be uptodate, so
2283 * we don't have to worry about a read_folio reading them
2284 * and overwriting a partial write. However if we have
2285 * encountered a short write and only partially written
2286 * into a buffer, it will not be marked uptodate, so a
2287 * read_folio might come in and destroy our partial write.
afddba49
NP
2288 *
2289 * Do the simplest thing, and just treat any short write to a
8c6cb3e3 2290 * non uptodate folio as a zero-length write, and force the
afddba49
NP
2291 * caller to redo the whole thing.
2292 */
8c6cb3e3 2293 if (!folio_test_uptodate(folio))
afddba49
NP
2294 copied = 0;
2295
4a9622f2 2296 folio_zero_new_buffers(folio, start+copied, start+len);
afddba49 2297 }
8c6cb3e3 2298 flush_dcache_folio(folio);
afddba49
NP
2299
2300 /* This could be a short (even 0-length) commit */
63a23847 2301 block_commit_write(folio, start, start + copied);
afddba49
NP
2302
2303 return copied;
2304}
2305EXPORT_SYMBOL(block_write_end);
2306
2307int generic_write_end(struct file *file, struct address_space *mapping,
2308 loff_t pos, unsigned len, unsigned copied,
a225800f 2309 struct folio *folio, void *fsdata)
afddba49 2310{
8af54f29
CH
2311 struct inode *inode = mapping->host;
2312 loff_t old_size = inode->i_size;
2313 bool i_size_changed = false;
2314
97edbc02 2315 copied = block_write_end(file, mapping, pos, len, copied, folio, fsdata);
8af54f29
CH
2316
2317 /*
2318 * No need to use i_size_read() here, the i_size cannot change under us
2319 * because we hold i_rwsem.
2320 *
696876d0 2321 * But it's important to update i_size while still holding folio lock:
8af54f29
CH
2322 * page writeout could otherwise come in and zero beyond i_size.
2323 */
2324 if (pos + copied > inode->i_size) {
2325 i_size_write(inode, pos + copied);
2326 i_size_changed = true;
2327 }
2328
696876d0
MWO
2329 folio_unlock(folio);
2330 folio_put(folio);
8af54f29
CH
2331
2332 if (old_size < pos)
2333 pagecache_isize_extended(inode, old_size, pos);
2334 /*
2335 * Don't mark the inode dirty under page lock. First, it unnecessarily
2336 * makes the holding time of page lock longer. Second, it forces lock
2337 * ordering of page lock and transaction start for journaling
2338 * filesystems.
2339 */
2340 if (i_size_changed)
2341 mark_inode_dirty(inode);
26ddb1f4 2342 return copied;
afddba49
NP
2343}
2344EXPORT_SYMBOL(generic_write_end);
2345
8ab22b9a 2346/*
2e7e80f7 2347 * block_is_partially_uptodate checks whether buffers within a folio are
8ab22b9a
HH
2348 * uptodate or not.
2349 *
2e7e80f7
MWO
2350 * Returns true if all buffers which correspond to the specified part
2351 * of the folio are uptodate.
8ab22b9a 2352 */
2e7e80f7 2353bool block_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
8ab22b9a 2354{
8ab22b9a
HH
2355 unsigned block_start, block_end, blocksize;
2356 unsigned to;
2357 struct buffer_head *bh, *head;
2e7e80f7 2358 bool ret = true;
8ab22b9a 2359
2e7e80f7
MWO
2360 head = folio_buffers(folio);
2361 if (!head)
2362 return false;
45bce8f3 2363 blocksize = head->b_size;
2e7e80f7 2364 to = min_t(unsigned, folio_size(folio) - from, count);
8ab22b9a 2365 to = from + to;
2e7e80f7
MWO
2366 if (from < blocksize && to > folio_size(folio) - blocksize)
2367 return false;
8ab22b9a 2368
8ab22b9a
HH
2369 bh = head;
2370 block_start = 0;
2371 do {
2372 block_end = block_start + blocksize;
2373 if (block_end > from && block_start < to) {
2374 if (!buffer_uptodate(bh)) {
2e7e80f7 2375 ret = false;
8ab22b9a
HH
2376 break;
2377 }
2378 if (block_end >= to)
2379 break;
2380 }
2381 block_start = block_end;
2382 bh = bh->b_this_page;
2383 } while (bh != head);
2384
2385 return ret;
2386}
2387EXPORT_SYMBOL(block_is_partially_uptodate);
2388
1da177e4 2389/*
2c69e205 2390 * Generic "read_folio" function for block devices that have the normal
1da177e4 2391 * get_block functionality. This is most of the block device filesystems.
2c69e205 2392 * Reads the folio asynchronously --- the unlock_buffer() and
1da177e4 2393 * set/clear_buffer_uptodate() functions propagate buffer state into the
2c69e205 2394 * folio once IO has completed.
1da177e4 2395 */
2c69e205 2396int block_read_full_folio(struct folio *folio, get_block_t *get_block)
1da177e4 2397{
2c69e205 2398 struct inode *inode = folio->mapping->host;
1da177e4 2399 sector_t iblock, lblock;
b72e591f 2400 struct buffer_head *bh, *head, *prev = NULL;
fa399c31 2401 size_t blocksize;
1da177e4 2402 int fully_mapped = 1;
b7a6eb22 2403 bool page_error = false;
4fa512ce
EB
2404 loff_t limit = i_size_read(inode);
2405
2406 /* This is needed for ext4. */
2407 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
2408 limit = inode->i_sb->s_maxbytes;
1da177e4 2409
c6c8c3e7 2410 head = folio_create_buffers(folio, inode, 0);
45bce8f3 2411 blocksize = head->b_size;
1da177e4 2412
fa399c31
MWO
2413 iblock = div_u64(folio_pos(folio), blocksize);
2414 lblock = div_u64(limit + blocksize - 1, blocksize);
1da177e4 2415 bh = head;
1da177e4
LT
2416
2417 do {
2418 if (buffer_uptodate(bh))
2419 continue;
2420
2421 if (!buffer_mapped(bh)) {
c64610ba
AM
2422 int err = 0;
2423
1da177e4
LT
2424 fully_mapped = 0;
2425 if (iblock < lblock) {
b0cf2321 2426 WARN_ON(bh->b_size != blocksize);
c64610ba 2427 err = get_block(inode, iblock, bh, 0);
7ad635ea 2428 if (err)
b7a6eb22 2429 page_error = true;
1da177e4
LT
2430 }
2431 if (!buffer_mapped(bh)) {
753aadeb 2432 folio_zero_range(folio, bh_offset(bh),
2c69e205 2433 blocksize);
c64610ba
AM
2434 if (!err)
2435 set_buffer_uptodate(bh);
1da177e4
LT
2436 continue;
2437 }
2438 /*
2439 * get_block() might have updated the buffer
2440 * synchronously
2441 */
2442 if (buffer_uptodate(bh))
2443 continue;
2444 }
b72e591f
MW
2445
2446 lock_buffer(bh);
2447 if (buffer_uptodate(bh)) {
2448 unlock_buffer(bh);
2449 continue;
2450 }
2451
2452 mark_buffer_async_read(bh);
2453 if (prev)
2454 submit_bh(REQ_OP_READ, prev);
2455 prev = bh;
753aadeb 2456 } while (iblock++, (bh = bh->b_this_page) != head);
1da177e4
LT
2457
2458 if (fully_mapped)
2c69e205 2459 folio_set_mappedtodisk(folio);
1da177e4 2460
1da177e4 2461 /*
b72e591f
MW
2462 * All buffers are uptodate or get_block() returned an error
2463 * when trying to map them - we must finish the read because
2464 * end_buffer_async_read() will never be called on any buffer
2465 * in this folio.
1da177e4 2466 */
b72e591f
MW
2467 if (prev)
2468 submit_bh(REQ_OP_READ, prev);
2469 else
2470 folio_end_read(folio, !page_error);
2471
1da177e4
LT
2472 return 0;
2473}
2c69e205 2474EXPORT_SYMBOL(block_read_full_folio);
1da177e4
LT
2475
2476/* utility function for filesystems that need to do work on expanding
89e10787 2477 * truncates. Uses filesystem pagecache writes to allow the filesystem to
1da177e4
LT
2478 * deal with the hole.
2479 */
89e10787 2480int generic_cont_expand_simple(struct inode *inode, loff_t size)
1da177e4
LT
2481{
2482 struct address_space *mapping = inode->i_mapping;
53b524b8 2483 const struct address_space_operations *aops = mapping->a_ops;
1da86618 2484 struct folio *folio;
1468c6f4 2485 void *fsdata = NULL;
1da177e4
LT
2486 int err;
2487
c08d3b0e 2488 err = inode_newsize_ok(inode, size);
2489 if (err)
1da177e4
LT
2490 goto out;
2491
1da86618 2492 err = aops->write_begin(NULL, mapping, size, 0, &folio, &fsdata);
89e10787 2493 if (err)
05eb0b51 2494 goto out;
05eb0b51 2495
1da86618 2496 err = aops->write_end(NULL, mapping, size, 0, 0, folio, fsdata);
89e10787 2497 BUG_ON(err > 0);
05eb0b51 2498
1da177e4
LT
2499out:
2500 return err;
2501}
1fe72eaa 2502EXPORT_SYMBOL(generic_cont_expand_simple);
1da177e4 2503
f1e3af72
AB
2504static int cont_expand_zero(struct file *file, struct address_space *mapping,
2505 loff_t pos, loff_t *bytes)
1da177e4 2506{
1da177e4 2507 struct inode *inode = mapping->host;
53b524b8 2508 const struct address_space_operations *aops = mapping->a_ops;
93407472 2509 unsigned int blocksize = i_blocksize(inode);
1da86618 2510 struct folio *folio;
1468c6f4 2511 void *fsdata = NULL;
89e10787
NP
2512 pgoff_t index, curidx;
2513 loff_t curpos;
2514 unsigned zerofrom, offset, len;
2515 int err = 0;
1da177e4 2516
09cbfeaf
KS
2517 index = pos >> PAGE_SHIFT;
2518 offset = pos & ~PAGE_MASK;
89e10787 2519
09cbfeaf
KS
2520 while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
2521 zerofrom = curpos & ~PAGE_MASK;
1da177e4
LT
2522 if (zerofrom & (blocksize-1)) {
2523 *bytes |= (blocksize-1);
2524 (*bytes)++;
2525 }
09cbfeaf 2526 len = PAGE_SIZE - zerofrom;
1da177e4 2527
53b524b8 2528 err = aops->write_begin(file, mapping, curpos, len,
1da86618 2529 &folio, &fsdata);
89e10787
NP
2530 if (err)
2531 goto out;
1da86618 2532 folio_zero_range(folio, offset_in_folio(folio, curpos), len);
53b524b8 2533 err = aops->write_end(file, mapping, curpos, len, len,
1da86618 2534 folio, fsdata);
89e10787
NP
2535 if (err < 0)
2536 goto out;
2537 BUG_ON(err != len);
2538 err = 0;
061e9746
OH
2539
2540 balance_dirty_pages_ratelimited(mapping);
c2ca0fcd 2541
08d405c8 2542 if (fatal_signal_pending(current)) {
c2ca0fcd
MP
2543 err = -EINTR;
2544 goto out;
2545 }
89e10787 2546 }
1da177e4 2547
89e10787
NP
2548 /* page covers the boundary, find the boundary offset */
2549 if (index == curidx) {
09cbfeaf 2550 zerofrom = curpos & ~PAGE_MASK;
1da177e4 2551 /* if we will expand the thing last block will be filled */
89e10787
NP
2552 if (offset <= zerofrom) {
2553 goto out;
2554 }
2555 if (zerofrom & (blocksize-1)) {
1da177e4
LT
2556 *bytes |= (blocksize-1);
2557 (*bytes)++;
2558 }
89e10787 2559 len = offset - zerofrom;
1da177e4 2560
53b524b8 2561 err = aops->write_begin(file, mapping, curpos, len,
1da86618 2562 &folio, &fsdata);
89e10787
NP
2563 if (err)
2564 goto out;
1da86618 2565 folio_zero_range(folio, offset_in_folio(folio, curpos), len);
53b524b8 2566 err = aops->write_end(file, mapping, curpos, len, len,
1da86618 2567 folio, fsdata);
89e10787
NP
2568 if (err < 0)
2569 goto out;
2570 BUG_ON(err != len);
2571 err = 0;
1da177e4 2572 }
89e10787
NP
2573out:
2574 return err;
2575}
2576
2577/*
2578 * For moronic filesystems that do not allow holes in file.
2579 * We may have to extend the file.
2580 */
282dc178 2581int cont_write_begin(struct file *file, struct address_space *mapping,
be3bbbc5 2582 loff_t pos, unsigned len,
1da86618 2583 struct folio **foliop, void **fsdata,
89e10787
NP
2584 get_block_t *get_block, loff_t *bytes)
2585{
2586 struct inode *inode = mapping->host;
93407472
FF
2587 unsigned int blocksize = i_blocksize(inode);
2588 unsigned int zerofrom;
89e10787
NP
2589 int err;
2590
2591 err = cont_expand_zero(file, mapping, pos, bytes);
2592 if (err)
155130a4 2593 return err;
89e10787 2594
09cbfeaf 2595 zerofrom = *bytes & ~PAGE_MASK;
89e10787
NP
2596 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2597 *bytes |= (blocksize-1);
2598 (*bytes)++;
1da177e4 2599 }
1da177e4 2600
1da86618 2601 return block_write_begin(mapping, pos, len, foliop, get_block);
1da177e4 2602}
1fe72eaa 2603EXPORT_SYMBOL(cont_write_begin);
1da177e4 2604
54171690
DC
2605/*
2606 * block_page_mkwrite() is not allowed to change the file size as it gets
2607 * called from a page fault handler when a page is first dirtied. Hence we must
2608 * be careful to check for EOF conditions here. We set the page up correctly
2609 * for a written page which means we get ENOSPC checking when writing into
2610 * holes and correct delalloc and unwritten extent mapping on filesystems that
2611 * support these features.
2612 *
2613 * We are not allowed to take the i_mutex here so we have to play games to
2614 * protect against truncate races as the page could now be beyond EOF. Because
7bb46a67 2615 * truncate writes the inode size before removing pages, once we have the
54171690
DC
2616 * page lock we can determine safely if the page is beyond EOF. If it is not
2617 * beyond EOF, then the page is guaranteed safe against truncation until we
2618 * unlock the page.
ea13a864 2619 *
14da9200 2620 * Direct callers of this function should protect against filesystem freezing
5c500029 2621 * using sb_start_pagefault() - sb_end_pagefault() functions.
54171690 2622 */
5c500029 2623int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
24da4fab 2624 get_block_t get_block)
54171690 2625{
fe181377 2626 struct folio *folio = page_folio(vmf->page);
496ad9aa 2627 struct inode *inode = file_inode(vma->vm_file);
54171690
DC
2628 unsigned long end;
2629 loff_t size;
24da4fab 2630 int ret;
54171690 2631
fe181377 2632 folio_lock(folio);
54171690 2633 size = i_size_read(inode);
fe181377
MWO
2634 if ((folio->mapping != inode->i_mapping) ||
2635 (folio_pos(folio) >= size)) {
24da4fab
JK
2636 /* We overload EFAULT to mean page got truncated */
2637 ret = -EFAULT;
2638 goto out_unlock;
54171690
DC
2639 }
2640
fe181377
MWO
2641 end = folio_size(folio);
2642 /* folio is wholly or partially inside EOF */
2643 if (folio_pos(folio) + end > size)
2644 end = size - folio_pos(folio);
54171690 2645
fe181377 2646 ret = __block_write_begin_int(folio, 0, end, get_block, NULL);
a524fcfe 2647 if (unlikely(ret))
24da4fab 2648 goto out_unlock;
a524fcfe 2649
63a23847 2650 block_commit_write(folio, 0, end);
a524fcfe 2651
fe181377
MWO
2652 folio_mark_dirty(folio);
2653 folio_wait_stable(folio);
24da4fab
JK
2654 return 0;
2655out_unlock:
fe181377 2656 folio_unlock(folio);
54171690 2657 return ret;
24da4fab 2658}
1fe72eaa 2659EXPORT_SYMBOL(block_page_mkwrite);
1da177e4 2660
1da177e4
LT
2661int block_truncate_page(struct address_space *mapping,
2662 loff_t from, get_block_t *get_block)
2663{
09cbfeaf 2664 pgoff_t index = from >> PAGE_SHIFT;
1da177e4 2665 unsigned blocksize;
54b21a79 2666 sector_t iblock;
6d68f644 2667 size_t offset, length, pos;
1da177e4 2668 struct inode *inode = mapping->host;
6d68f644 2669 struct folio *folio;
1da177e4 2670 struct buffer_head *bh;
dc7cb2d2 2671 int err = 0;
1da177e4 2672
93407472 2673 blocksize = i_blocksize(inode);
6d68f644 2674 length = from & (blocksize - 1);
1da177e4
LT
2675
2676 /* Block boundary? Nothing to do */
2677 if (!length)
2678 return 0;
2679
2680 length = blocksize - length;
4b04646c
MWO
2681 iblock = ((loff_t)index * PAGE_SIZE) >> inode->i_blkbits;
2682
6d68f644
MWO
2683 folio = filemap_grab_folio(mapping, index);
2684 if (IS_ERR(folio))
2685 return PTR_ERR(folio);
1da177e4 2686
6d68f644 2687 bh = folio_buffers(folio);
3decb856 2688 if (!bh)
0a88810d 2689 bh = create_empty_buffers(folio, blocksize, 0);
1da177e4
LT
2690
2691 /* Find the buffer that contains "offset" */
6d68f644 2692 offset = offset_in_folio(folio, from);
1da177e4
LT
2693 pos = blocksize;
2694 while (offset >= pos) {
2695 bh = bh->b_this_page;
2696 iblock++;
2697 pos += blocksize;
2698 }
2699
1da177e4 2700 if (!buffer_mapped(bh)) {
b0cf2321 2701 WARN_ON(bh->b_size != blocksize);
1da177e4
LT
2702 err = get_block(inode, iblock, bh, 0);
2703 if (err)
2704 goto unlock;
2705 /* unmapped? It's a hole - nothing to do */
2706 if (!buffer_mapped(bh))
2707 goto unlock;
2708 }
2709
2710 /* Ok, it's mapped. Make sure it's up-to-date */
6d68f644 2711 if (folio_test_uptodate(folio))
1da177e4
LT
2712 set_buffer_uptodate(bh);
2713
33a266dd 2714 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
e7ea1129 2715 err = bh_read(bh, 0);
1da177e4 2716 /* Uhhuh. Read error. Complain and punt. */
e7ea1129 2717 if (err < 0)
1da177e4
LT
2718 goto unlock;
2719 }
2720
6d68f644 2721 folio_zero_range(folio, offset, length);
1da177e4 2722 mark_buffer_dirty(bh);
1da177e4
LT
2723
2724unlock:
6d68f644
MWO
2725 folio_unlock(folio);
2726 folio_put(folio);
dc7cb2d2 2727
1da177e4
LT
2728 return err;
2729}
1fe72eaa 2730EXPORT_SYMBOL(block_truncate_page);
1da177e4
LT
2731
2732/*
6b0dfabb 2733 * The generic write folio function for buffer-backed address_spaces
1da177e4 2734 */
17bf23a9
MWO
2735int block_write_full_folio(struct folio *folio, struct writeback_control *wbc,
2736 void *get_block)
1da177e4 2737{
bb0ea598 2738 struct inode * const inode = folio->mapping->host;
1da177e4 2739 loff_t i_size = i_size_read(inode);
1da177e4 2740
bb0ea598
MWO
2741 /* Is the folio fully inside i_size? */
2742 if (folio_pos(folio) + folio_size(folio) <= i_size)
14059f66 2743 return __block_write_full_folio(inode, folio, get_block, wbc);
1da177e4 2744
bb0ea598
MWO
2745 /* Is the folio fully outside i_size? (truncate in progress) */
2746 if (folio_pos(folio) >= i_size) {
53418a18 2747 folio_unlock(folio);
1da177e4
LT
2748 return 0; /* don't care */
2749 }
2750
2751 /*
bb0ea598 2752 * The folio straddles i_size. It must be zeroed out on each and every
6b0dfabb 2753 * writeback invocation because it may be mmapped. "A file is mapped
1da177e4 2754 * in multiples of the page size. For a file that is not a multiple of
bb0ea598 2755 * the page size, the remaining memory is zeroed when mapped, and
1da177e4
LT
2756 * writes to that region are not written out to the file."
2757 */
bb0ea598
MWO
2758 folio_zero_segment(folio, offset_in_folio(folio, i_size),
2759 folio_size(folio));
14059f66 2760 return __block_write_full_folio(inode, folio, get_block, wbc);
35c80d5f 2761}
35c80d5f 2762
1da177e4
LT
2763sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2764 get_block_t *get_block)
2765{
1da177e4 2766 struct inode *inode = mapping->host;
2a527d68
AP
2767 struct buffer_head tmp = {
2768 .b_size = i_blocksize(inode),
2769 };
2770
1da177e4
LT
2771 get_block(inode, block, &tmp, 0);
2772 return tmp.b_blocknr;
2773}
1fe72eaa 2774EXPORT_SYMBOL(generic_block_bmap);
1da177e4 2775
4246a0b6 2776static void end_bio_bh_io_sync(struct bio *bio)
1da177e4
LT
2777{
2778 struct buffer_head *bh = bio->bi_private;
2779
b7c44ed9 2780 if (unlikely(bio_flagged(bio, BIO_QUIET)))
08bafc03
KM
2781 set_bit(BH_Quiet, &bh->b_state);
2782
4e4cbee9 2783 bh->b_end_io(bh, !bio->bi_status);
1da177e4 2784 bio_put(bio);
1da177e4
LT
2785}
2786
5bdf402a 2787static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
44981351 2788 enum rw_hint write_hint,
5bdf402a 2789 struct writeback_control *wbc)
1da177e4 2790{
1420c4a5 2791 const enum req_op op = opf & REQ_OP_MASK;
1da177e4 2792 struct bio *bio;
1da177e4
LT
2793
2794 BUG_ON(!buffer_locked(bh));
2795 BUG_ON(!buffer_mapped(bh));
2796 BUG_ON(!bh->b_end_io);
8fb0e342
AK
2797 BUG_ON(buffer_delay(bh));
2798 BUG_ON(buffer_unwritten(bh));
1da177e4 2799
1da177e4 2800 /*
48fd4f93 2801 * Only clear out a write error when rewriting
1da177e4 2802 */
2a222ca9 2803 if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
1da177e4
LT
2804 clear_buffer_write_io_error(bh);
2805
07888c66 2806 if (buffer_meta(bh))
1420c4a5 2807 opf |= REQ_META;
07888c66 2808 if (buffer_prio(bh))
1420c4a5 2809 opf |= REQ_PRIO;
07888c66 2810
1420c4a5 2811 bio = bio_alloc(bh->b_bdev, 1, opf, GFP_NOIO);
1da177e4 2812
4f74d15f
EB
2813 fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
2814
4f024f37 2815 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
44981351 2816 bio->bi_write_hint = write_hint;
1da177e4 2817
30dac24e 2818 bio_add_folio_nofail(bio, bh->b_folio, bh->b_size, bh_offset(bh));
1da177e4
LT
2819
2820 bio->bi_end_io = end_bio_bh_io_sync;
2821 bio->bi_private = bh;
2822
83c9c547
ML
2823 /* Take care of bh's that straddle the end of the device */
2824 guard_bio_eod(bio);
2825
fd42df30
DZ
2826 if (wbc) {
2827 wbc_init_bio(wbc, bio);
30dac24e 2828 wbc_account_cgroup_owner(wbc, bh->b_folio, bh->b_size);
fd42df30
DZ
2829 }
2830
4e49ea4a 2831 submit_bio(bio);
1da177e4 2832}
bafc0dba 2833
5bdf402a 2834void submit_bh(blk_opf_t opf, struct buffer_head *bh)
bafc0dba 2835{
44981351 2836 submit_bh_wbc(opf, bh, WRITE_LIFE_NOT_SET, NULL);
71368511 2837}
1fe72eaa 2838EXPORT_SYMBOL(submit_bh);
1da177e4 2839
3ae72869 2840void write_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags)
9cb569d6
CH
2841{
2842 lock_buffer(bh);
2843 if (!test_clear_buffer_dirty(bh)) {
2844 unlock_buffer(bh);
2845 return;
2846 }
2847 bh->b_end_io = end_buffer_write_sync;
2848 get_bh(bh);
1420c4a5 2849 submit_bh(REQ_OP_WRITE | op_flags, bh);
9cb569d6
CH
2850}
2851EXPORT_SYMBOL(write_dirty_buffer);
2852
1da177e4
LT
2853/*
2854 * For a data-integrity writeout, we need to wait upon any in-progress I/O
2855 * and then start new I/O and then wait upon it. The caller must have a ref on
2856 * the buffer_head.
2857 */
3ae72869 2858int __sync_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags)
1da177e4 2859{
1da177e4
LT
2860 WARN_ON(atomic_read(&bh->b_count) < 1);
2861 lock_buffer(bh);
2862 if (test_clear_buffer_dirty(bh)) {
377254b2
XT
2863 /*
2864 * The bh should be mapped, but it might not be if the
2865 * device was hot-removed. Not much we can do but fail the I/O.
2866 */
2867 if (!buffer_mapped(bh)) {
2868 unlock_buffer(bh);
2869 return -EIO;
2870 }
2871
1da177e4
LT
2872 get_bh(bh);
2873 bh->b_end_io = end_buffer_write_sync;
ab620620 2874 submit_bh(REQ_OP_WRITE | op_flags, bh);
1da177e4 2875 wait_on_buffer(bh);
ab620620
RHI
2876 if (!buffer_uptodate(bh))
2877 return -EIO;
1da177e4
LT
2878 } else {
2879 unlock_buffer(bh);
2880 }
ab620620 2881 return 0;
1da177e4 2882}
87e99511
CH
2883EXPORT_SYMBOL(__sync_dirty_buffer);
2884
2885int sync_dirty_buffer(struct buffer_head *bh)
2886{
70fd7614 2887 return __sync_dirty_buffer(bh, REQ_SYNC);
87e99511 2888}
1fe72eaa 2889EXPORT_SYMBOL(sync_dirty_buffer);
1da177e4 2890
1da177e4
LT
2891static inline int buffer_busy(struct buffer_head *bh)
2892{
2893 return atomic_read(&bh->b_count) |
2894 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
2895}
2896
64394763
MWO
2897static bool
2898drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free)
1da177e4 2899{
64394763 2900 struct buffer_head *head = folio_buffers(folio);
1da177e4
LT
2901 struct buffer_head *bh;
2902
2903 bh = head;
2904 do {
1da177e4
LT
2905 if (buffer_busy(bh))
2906 goto failed;
2907 bh = bh->b_this_page;
2908 } while (bh != head);
2909
2910 do {
2911 struct buffer_head *next = bh->b_this_page;
2912
535ee2fb 2913 if (bh->b_assoc_map)
1da177e4
LT
2914 __remove_assoc_queue(bh);
2915 bh = next;
2916 } while (bh != head);
2917 *buffers_to_free = head;
64394763
MWO
2918 folio_detach_private(folio);
2919 return true;
1da177e4 2920failed:
64394763 2921 return false;
1da177e4
LT
2922}
2923
b1888d14
MWO
2924/**
2925 * try_to_free_buffers - Release buffers attached to this folio.
2926 * @folio: The folio.
2927 *
2928 * If any buffers are in use (dirty, under writeback, elevated refcount),
2929 * no buffers will be freed.
2930 *
2931 * If the folio is dirty but all the buffers are clean then we need to
2932 * be sure to mark the folio clean as well. This is because the folio
2933 * may be against a block device, and a later reattachment of buffers
2934 * to a dirty folio will set *all* buffers dirty. Which would corrupt
2935 * filesystem data on the same device.
2936 *
2937 * The same applies to regular filesystem folios: if all the buffers are
2938 * clean then we set the folio clean and proceed. To do that, we require
2939 * total exclusion from block_dirty_folio(). That is obtained with
2940 * i_private_lock.
2941 *
2942 * Exclusion against try_to_free_buffers may be obtained by either
2943 * locking the folio or by holding its mapping's i_private_lock.
2944 *
2945 * Context: Process context. @folio must be locked. Will not sleep.
2946 * Return: true if all buffers attached to this folio were freed.
2947 */
68189fef 2948bool try_to_free_buffers(struct folio *folio)
1da177e4 2949{
68189fef 2950 struct address_space * const mapping = folio->mapping;
1da177e4 2951 struct buffer_head *buffers_to_free = NULL;
68189fef 2952 bool ret = 0;
1da177e4 2953
68189fef
MWO
2954 BUG_ON(!folio_test_locked(folio));
2955 if (folio_test_writeback(folio))
2956 return false;
1da177e4
LT
2957
2958 if (mapping == NULL) { /* can this still happen? */
64394763 2959 ret = drop_buffers(folio, &buffers_to_free);
1da177e4
LT
2960 goto out;
2961 }
2962
600f111e 2963 spin_lock(&mapping->i_private_lock);
64394763 2964 ret = drop_buffers(folio, &buffers_to_free);
ecdfc978
LT
2965
2966 /*
2967 * If the filesystem writes its buffers by hand (eg ext3)
68189fef
MWO
2968 * then we can have clean buffers against a dirty folio. We
2969 * clean the folio here; otherwise the VM will never notice
ecdfc978
LT
2970 * that the filesystem did any IO at all.
2971 *
2972 * Also, during truncate, discard_buffer will have marked all
68189fef
MWO
2973 * the folio's buffers clean. We discover that here and clean
2974 * the folio also.
87df7241 2975 *
600f111e 2976 * i_private_lock must be held over this entire operation in order
e621900a 2977 * to synchronise against block_dirty_folio and prevent the
87df7241 2978 * dirty bit from being lost.
ecdfc978 2979 */
11f81bec 2980 if (ret)
68189fef 2981 folio_cancel_dirty(folio);
600f111e 2982 spin_unlock(&mapping->i_private_lock);
1da177e4
LT
2983out:
2984 if (buffers_to_free) {
2985 struct buffer_head *bh = buffers_to_free;
2986
2987 do {
2988 struct buffer_head *next = bh->b_this_page;
2989 free_buffer_head(bh);
2990 bh = next;
2991 } while (bh != buffers_to_free);
2992 }
2993 return ret;
2994}
2995EXPORT_SYMBOL(try_to_free_buffers);
2996
1da177e4
LT
2997/*
2998 * Buffer-head allocation
2999 */
68279f9c 3000static struct kmem_cache *bh_cachep __ro_after_init;
1da177e4
LT
3001
3002/*
3003 * Once the number of bh's in the machine exceeds this level, we start
3004 * stripping them in writeback.
3005 */
68279f9c 3006static unsigned long max_buffer_heads __ro_after_init;
1da177e4
LT
3007
3008int buffer_heads_over_limit;
3009
3010struct bh_accounting {
3011 int nr; /* Number of live bh's */
3012 int ratelimit; /* Limit cacheline bouncing */
3013};
3014
3015static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3016
3017static void recalc_bh_state(void)
3018{
3019 int i;
3020 int tot = 0;
3021
ee1be862 3022 if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
1da177e4 3023 return;
c7b92516 3024 __this_cpu_write(bh_accounting.ratelimit, 0);
8a143426 3025 for_each_online_cpu(i)
1da177e4
LT
3026 tot += per_cpu(bh_accounting, i).nr;
3027 buffer_heads_over_limit = (tot > max_buffer_heads);
3028}
c7b92516 3029
dd0fc66f 3030struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
1da177e4 3031{
019b4d12 3032 struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
1da177e4 3033 if (ret) {
a35afb83 3034 INIT_LIST_HEAD(&ret->b_assoc_buffers);
f1e67e35 3035 spin_lock_init(&ret->b_uptodate_lock);
c7b92516
CL
3036 preempt_disable();
3037 __this_cpu_inc(bh_accounting.nr);
1da177e4 3038 recalc_bh_state();
c7b92516 3039 preempt_enable();
1da177e4
LT
3040 }
3041 return ret;
3042}
3043EXPORT_SYMBOL(alloc_buffer_head);
3044
3045void free_buffer_head(struct buffer_head *bh)
3046{
3047 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3048 kmem_cache_free(bh_cachep, bh);
c7b92516
CL
3049 preempt_disable();
3050 __this_cpu_dec(bh_accounting.nr);
1da177e4 3051 recalc_bh_state();
c7b92516 3052 preempt_enable();
1da177e4
LT
3053}
3054EXPORT_SYMBOL(free_buffer_head);
3055
fc4d24c9 3056static int buffer_exit_cpu_dead(unsigned int cpu)
1da177e4
LT
3057{
3058 int i;
3059 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3060
3061 for (i = 0; i < BH_LRU_SIZE; i++) {
3062 brelse(b->bhs[i]);
3063 b->bhs[i] = NULL;
3064 }
c7b92516 3065 this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
8a143426 3066 per_cpu(bh_accounting, cpu).nr = 0;
fc4d24c9 3067 return 0;
1da177e4 3068}
1da177e4 3069
389d1b08 3070/**
a6b91919 3071 * bh_uptodate_or_lock - Test whether the buffer is uptodate
389d1b08
AK
3072 * @bh: struct buffer_head
3073 *
3074 * Return true if the buffer is up-to-date and false,
3075 * with the buffer locked, if not.
3076 */
3077int bh_uptodate_or_lock(struct buffer_head *bh)
3078{
3079 if (!buffer_uptodate(bh)) {
3080 lock_buffer(bh);
3081 if (!buffer_uptodate(bh))
3082 return 0;
3083 unlock_buffer(bh);
3084 }
3085 return 1;
3086}
3087EXPORT_SYMBOL(bh_uptodate_or_lock);
3088
3089/**
fdee117e 3090 * __bh_read - Submit read for a locked buffer
389d1b08 3091 * @bh: struct buffer_head
fdee117e
ZY
3092 * @op_flags: appending REQ_OP_* flags besides REQ_OP_READ
3093 * @wait: wait until reading finish
389d1b08 3094 *
fdee117e 3095 * Returns zero on success or don't wait, and -EIO on error.
389d1b08 3096 */
fdee117e 3097int __bh_read(struct buffer_head *bh, blk_opf_t op_flags, bool wait)
389d1b08 3098{
fdee117e 3099 int ret = 0;
389d1b08 3100
fdee117e 3101 BUG_ON(!buffer_locked(bh));
389d1b08
AK
3102
3103 get_bh(bh);
3104 bh->b_end_io = end_buffer_read_sync;
fdee117e
ZY
3105 submit_bh(REQ_OP_READ | op_flags, bh);
3106 if (wait) {
3107 wait_on_buffer(bh);
3108 if (!buffer_uptodate(bh))
3109 ret = -EIO;
3110 }
3111 return ret;
3112}
3113EXPORT_SYMBOL(__bh_read);
3114
3115/**
3116 * __bh_read_batch - Submit read for a batch of unlocked buffers
3117 * @nr: entry number of the buffer batch
3118 * @bhs: a batch of struct buffer_head
3119 * @op_flags: appending REQ_OP_* flags besides REQ_OP_READ
3120 * @force_lock: force to get a lock on the buffer if set, otherwise drops any
3121 * buffer that cannot lock.
3122 *
3123 * Returns zero on success or don't wait, and -EIO on error.
3124 */
3125void __bh_read_batch(int nr, struct buffer_head *bhs[],
3126 blk_opf_t op_flags, bool force_lock)
3127{
3128 int i;
3129
3130 for (i = 0; i < nr; i++) {
3131 struct buffer_head *bh = bhs[i];
3132
3133 if (buffer_uptodate(bh))
3134 continue;
3135
3136 if (force_lock)
3137 lock_buffer(bh);
3138 else
3139 if (!trylock_buffer(bh))
3140 continue;
3141
3142 if (buffer_uptodate(bh)) {
3143 unlock_buffer(bh);
3144 continue;
3145 }
3146
3147 bh->b_end_io = end_buffer_read_sync;
3148 get_bh(bh);
3149 submit_bh(REQ_OP_READ | op_flags, bh);
3150 }
389d1b08 3151}
fdee117e 3152EXPORT_SYMBOL(__bh_read_batch);
389d1b08 3153
1da177e4
LT
3154void __init buffer_init(void)
3155{
43be594a 3156 unsigned long nrpages;
fc4d24c9 3157 int ret;
1da177e4 3158
de8a3207 3159 bh_cachep = KMEM_CACHE(buffer_head,
c997d683 3160 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC);
1da177e4
LT
3161 /*
3162 * Limit the bh occupancy to 10% of ZONE_NORMAL
3163 */
3164 nrpages = (nr_free_buffer_pages() * 10) / 100;
3165 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
fc4d24c9
SAS
3166 ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
3167 NULL, buffer_exit_cpu_dead);
3168 WARN_ON(ret < 0);
1da177e4 3169}