blob: 5cffa98a5c3f4f100b255e9e5aff75eab8b5fa29 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010023#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/syscalls.h>
25#include <linux/fs.h>
Christoph Hellwigae259a92016-06-21 09:23:11 +100026#include <linux/iomap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mm.h>
28#include <linux/percpu.h>
29#include <linux/slab.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/blkdev.h>
32#include <linux/file.h>
33#include <linux/quotaops.h>
34#include <linux/highmem.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050035#include <linux/export.h>
Tejun Heobafc0db2015-06-02 08:37:23 -060036#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/writeback.h>
38#include <linux/hash.h>
39#include <linux/suspend.h>
40#include <linux/buffer_head.h>
Andrew Morton55e829a2006-12-10 02:19:27 -080041#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/cpu.h>
44#include <linux/bitops.h>
45#include <linux/mpage.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070046#include <linux/bit_spinlock.h>
Jan Kara29f3ad72016-11-04 18:08:11 +010047#include <linux/pagevec.h>
Shakeel Buttf745c6f2018-08-17 15:46:44 -070048#include <linux/sched/mm.h>
Tejun Heo5305cb82013-01-11 13:06:36 -080049#include <trace/events/block.h>
Eric Biggers31fb9922019-10-22 20:33:11 -070050#include <linux/fscrypt.h>
Eric Biggers0957cd62022-12-23 12:36:37 -080051#include <linux/fsverity.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Ben Dooks2b211dc2019-11-30 17:49:18 -080053#include "internal.h"
54
Minchan Kimba005d62022-10-20 16:03:35 -070055#include <trace/hooks/buffer.h>
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
Mike Christie2a222ca2016-06-05 14:31:43 -050058static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
Jens Axboe8e8f9292017-06-27 09:30:05 -060059 enum rw_hint hint, struct writeback_control *wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
62
Tejun Heof0059af2013-01-11 13:06:35 -080063inline void touch_buffer(struct buffer_head *bh)
64{
Tejun Heo5305cb82013-01-11 13:06:36 -080065 trace_block_touch_buffer(bh);
Tejun Heof0059af2013-01-11 13:06:35 -080066 mark_page_accessed(bh->b_page);
67}
68EXPORT_SYMBOL(touch_buffer);
69
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080070void __lock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
NeilBrown74316202014-07-07 15:16:04 +100072 wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74EXPORT_SYMBOL(__lock_buffer);
75
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080076void unlock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Nick Piggin51b07fc2008-10-18 20:27:00 -070078 clear_bit_unlock(BH_Lock, &bh->b_state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010079 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 wake_up_bit(&bh->b_state, BH_Lock);
81}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070082EXPORT_SYMBOL(unlock_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
Mel Gormanb4597222013-07-03 15:02:05 -070085 * Returns if the page has dirty or writeback buffers. If all the buffers
86 * are unlocked and clean then the PageDirty information is stale. If
87 * any of the pages are locked, it is assumed they are locked for IO.
88 */
89void buffer_check_dirty_writeback(struct page *page,
90 bool *dirty, bool *writeback)
91{
92 struct buffer_head *head, *bh;
93 *dirty = false;
94 *writeback = false;
95
96 BUG_ON(!PageLocked(page));
97
98 if (!page_has_buffers(page))
99 return;
100
101 if (PageWriteback(page))
102 *writeback = true;
103
104 head = page_buffers(page);
105 bh = head;
106 do {
107 if (buffer_locked(bh))
108 *writeback = true;
109
110 if (buffer_dirty(bh))
111 *dirty = true;
112
113 bh = bh->b_this_page;
114 } while (bh != head);
115}
116EXPORT_SYMBOL(buffer_check_dirty_writeback);
117
118/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * Block until a buffer comes unlocked. This doesn't stop it
120 * from becoming locked again - you have to lock it yourself
121 * if you want to preserve its state.
122 */
123void __wait_on_buffer(struct buffer_head * bh)
124{
NeilBrown74316202014-07-07 15:16:04 +1000125 wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700127EXPORT_SYMBOL(__wait_on_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Robert Elliottb744c2a2014-10-21 13:55:09 -0600129static void buffer_io_error(struct buffer_head *bh, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Robert Elliott432f16e2014-10-21 13:55:11 -0600131 if (!test_bit(BH_Quiet, &bh->b_state))
132 printk_ratelimited(KERN_ERR
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +0400133 "Buffer I/O error on dev %pg, logical block %llu%s\n",
134 bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137/*
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700138 * End-of-IO handler helper function which does not touch the bh after
139 * unlocking it.
140 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
141 * a race there is benign: unlock_buffer() only use the bh's address for
142 * hashing after unlocking the buffer, so it doesn't actually touch the bh
143 * itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700145static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 if (uptodate) {
148 set_buffer_uptodate(bh);
149 } else {
Christoph Hellwig70246282016-07-19 11:28:41 +0200150 /* This happens, due to failed read-ahead attempts. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 clear_buffer_uptodate(bh);
152 }
153 unlock_buffer(bh);
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700154}
155
156/*
157 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
158 * unlock the buffer. This is what ll_rw_block uses too.
159 */
160void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
161{
162 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 put_bh(bh);
164}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700165EXPORT_SYMBOL(end_buffer_read_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (uptodate) {
170 set_buffer_uptodate(bh);
171 } else {
Robert Elliott432f16e2014-10-21 13:55:11 -0600172 buffer_io_error(bh, ", lost sync page write");
Jeff Layton87354e52017-07-06 07:02:21 -0400173 mark_buffer_write_io_error(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 clear_buffer_uptodate(bh);
175 }
176 unlock_buffer(bh);
177 put_bh(bh);
178}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +0200179EXPORT_SYMBOL_NS(end_buffer_write_sync, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Various filesystems appear to want __find_get_block to be non-blocking.
183 * But it's the page lock which protects the buffers. To get around this,
184 * we get exclusion from try_to_free_buffers with the blockdev mapping's
185 * private_lock.
186 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700187 * Hack idea: for the blockdev mapping, private_lock contention
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * may be quite high. This code could TryLock the page, and if that
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700189 * succeeds, there is no need to take private_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191static struct buffer_head *
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -0800192__find_get_block_slow(struct block_device *bdev, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct inode *bd_inode = bdev->bd_inode;
195 struct address_space *bd_mapping = bd_inode->i_mapping;
196 struct buffer_head *ret = NULL;
197 pgoff_t index;
198 struct buffer_head *bh;
199 struct buffer_head *head;
200 struct page *page;
201 int all_mapped = 1;
Tetsuo Handa43636c82019-01-21 22:49:37 +0900202 static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
Mel Gorman2457aec2014-06-04 16:10:31 -0700205 page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!page)
207 goto out;
208
209 spin_lock(&bd_mapping->private_lock);
210 if (!page_has_buffers(page))
211 goto out_unlock;
212 head = page_buffers(page);
213 bh = head;
214 do {
Nikanth Karthikesan97f76d32009-04-02 16:56:46 -0700215 if (!buffer_mapped(bh))
216 all_mapped = 0;
217 else if (bh->b_blocknr == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ret = bh;
219 get_bh(bh);
220 goto out_unlock;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 bh = bh->b_this_page;
223 } while (bh != head);
224
225 /* we might be here because some of the buffers on this page are
226 * not mapped. This is due to various races between
227 * file io on the block device and getblk. It gets dealt with
228 * elsewhere, don't buffer_error if we had some unmapped buffers
229 */
Tetsuo Handa43636c82019-01-21 22:49:37 +0900230 ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
231 if (all_mapped && __ratelimit(&last_warned)) {
232 printk("__find_get_block_slow() failed. block=%llu, "
233 "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
234 "device %pg blocksize: %d\n",
235 (unsigned long long)block,
236 (unsigned long long)bh->b_blocknr,
237 bh->b_state, bh->b_size, bdev,
238 1 << bd_inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240out_unlock:
241 spin_unlock(&bd_mapping->private_lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300242 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243out:
244 return ret;
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
248{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700250 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct buffer_head *tmp;
252 struct page *page;
253 int page_uptodate = 1;
254
255 BUG_ON(!buffer_async_read(bh));
256
257 page = bh->b_page;
258 if (uptodate) {
259 set_buffer_uptodate(bh);
260 } else {
261 clear_buffer_uptodate(bh);
Robert Elliott432f16e2014-10-21 13:55:11 -0600262 buffer_io_error(bh, ", async page read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 SetPageError(page);
264 }
265
266 /*
267 * Be _very_ careful from here on. Bad things can happen if
268 * two buffer heads end IO at almost the same time and both
269 * decide that the page is now completely done.
270 */
Nick Piggina3972202005-07-07 17:56:56 -0700271 first = page_buffers(page);
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +0100272 spin_lock_irqsave(&first->b_uptodate_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 clear_buffer_async_read(bh);
274 unlock_buffer(bh);
275 tmp = bh;
276 do {
277 if (!buffer_uptodate(tmp))
278 page_uptodate = 0;
279 if (buffer_async_read(tmp)) {
280 BUG_ON(!buffer_locked(tmp));
281 goto still_busy;
282 }
283 tmp = tmp->b_this_page;
284 } while (tmp != bh);
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +0100285 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /*
288 * If none of the buffers had errors and they are all
289 * uptodate then we can set the page uptodate.
290 */
291 if (page_uptodate && !PageError(page))
292 SetPageUptodate(page);
293 unlock_page(page);
294 return;
295
296still_busy:
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +0100297 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return;
299}
300
Eric Biggers0957cd62022-12-23 12:36:37 -0800301struct postprocess_bh_ctx {
Eric Biggers31fb9922019-10-22 20:33:11 -0700302 struct work_struct work;
303 struct buffer_head *bh;
304};
305
Eric Biggers0957cd62022-12-23 12:36:37 -0800306static void verify_bh(struct work_struct *work)
307{
308 struct postprocess_bh_ctx *ctx =
309 container_of(work, struct postprocess_bh_ctx, work);
310 struct buffer_head *bh = ctx->bh;
311 bool valid;
312
313 valid = fsverity_verify_blocks(bh->b_page, bh->b_size, bh_offset(bh));
314 end_buffer_async_read(bh, valid);
315 kfree(ctx);
316}
317
318static bool need_fsverity(struct buffer_head *bh)
319{
320 struct page *page = bh->b_page;
321 struct inode *inode = page->mapping->host;
322
323 return fsverity_active(inode) &&
324 /* needed by ext4 */
325 page->index < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
326}
327
Eric Biggers31fb9922019-10-22 20:33:11 -0700328static void decrypt_bh(struct work_struct *work)
329{
Eric Biggers0957cd62022-12-23 12:36:37 -0800330 struct postprocess_bh_ctx *ctx =
331 container_of(work, struct postprocess_bh_ctx, work);
Eric Biggers31fb9922019-10-22 20:33:11 -0700332 struct buffer_head *bh = ctx->bh;
333 int err;
334
335 err = fscrypt_decrypt_pagecache_blocks(bh->b_page, bh->b_size,
336 bh_offset(bh));
Eric Biggers0957cd62022-12-23 12:36:37 -0800337 if (err == 0 && need_fsverity(bh)) {
338 /*
339 * We use different work queues for decryption and for verity
340 * because verity may require reading metadata pages that need
341 * decryption, and we shouldn't recurse to the same workqueue.
342 */
343 INIT_WORK(&ctx->work, verify_bh);
344 fsverity_enqueue_verify_work(&ctx->work);
345 return;
346 }
Eric Biggers31fb9922019-10-22 20:33:11 -0700347 end_buffer_async_read(bh, err == 0);
348 kfree(ctx);
349}
350
351/*
352 * I/O completion handler for block_read_full_page() - pages
353 * which come unlocked at the end of I/O.
354 */
355static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate)
356{
Eric Biggers0957cd62022-12-23 12:36:37 -0800357 struct inode *inode = bh->b_page->mapping->host;
358 bool decrypt = fscrypt_inode_uses_fs_layer_crypto(inode);
359 bool verify = need_fsverity(bh);
360
361 /* Decrypt (with fscrypt) and/or verify (with fsverity) if needed. */
362 if (uptodate && (decrypt || verify)) {
363 struct postprocess_bh_ctx *ctx =
364 kmalloc(sizeof(*ctx), GFP_ATOMIC);
Eric Biggers31fb9922019-10-22 20:33:11 -0700365
366 if (ctx) {
Eric Biggers31fb9922019-10-22 20:33:11 -0700367 ctx->bh = bh;
Eric Biggers0957cd62022-12-23 12:36:37 -0800368 if (decrypt) {
369 INIT_WORK(&ctx->work, decrypt_bh);
370 fscrypt_enqueue_decrypt_work(&ctx->work);
371 } else {
372 INIT_WORK(&ctx->work, verify_bh);
373 fsverity_enqueue_verify_work(&ctx->work);
374 }
Eric Biggers31fb9922019-10-22 20:33:11 -0700375 return;
376 }
377 uptodate = 0;
378 }
379 end_buffer_async_read(bh, uptodate);
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * Completion handler for block_write_full_page() - pages which are unlocked
384 * during I/O, and which have PageWriteback cleared upon I/O completion.
385 */
Chris Mason35c80d52009-04-15 13:22:38 -0400386void end_buffer_async_write(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700389 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct buffer_head *tmp;
391 struct page *page;
392
393 BUG_ON(!buffer_async_write(bh));
394
395 page = bh->b_page;
396 if (uptodate) {
397 set_buffer_uptodate(bh);
398 } else {
Robert Elliott432f16e2014-10-21 13:55:11 -0600399 buffer_io_error(bh, ", lost async page write");
Jeff Layton87354e52017-07-06 07:02:21 -0400400 mark_buffer_write_io_error(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 clear_buffer_uptodate(bh);
402 SetPageError(page);
403 }
404
Nick Piggina3972202005-07-07 17:56:56 -0700405 first = page_buffers(page);
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +0100406 spin_lock_irqsave(&first->b_uptodate_lock, flags);
Nick Piggina3972202005-07-07 17:56:56 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 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 }
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +0100418 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 end_page_writeback(page);
420 return;
421
422still_busy:
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +0100423 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return;
425}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700426EXPORT_SYMBOL(end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428/*
429 * If a page's buffers are under async readin (end_buffer_async_read
430 * completion) then there is a possibility that another thread of
431 * control could lock one of the buffers after it has completed
432 * but while some of the other buffers have not completed. This
433 * locked buffer would confuse end_buffer_async_read() into not unlocking
434 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
435 * that this buffer is not under async I/O.
436 *
437 * The page comes unlocked when it has no locked buffer_async buffers
438 * left.
439 *
440 * PageLocked prevents anyone starting new async I/O reads any of
441 * the buffers.
442 *
443 * PageWriteback is used to prevent simultaneous writeout of the same
444 * page.
445 *
446 * PageLocked prevents anyone from starting writeback of a page which is
447 * under read I/O (PageWriteback is only ever set against a locked page).
448 */
449static void mark_buffer_async_read(struct buffer_head *bh)
450{
Eric Biggers31fb9922019-10-22 20:33:11 -0700451 bh->b_end_io = end_buffer_async_read_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 set_buffer_async_read(bh);
453}
454
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700455static void mark_buffer_async_write_endio(struct buffer_head *bh,
456 bh_end_io_t *handler)
Chris Mason35c80d52009-04-15 13:22:38 -0400457{
458 bh->b_end_io = handler;
459 set_buffer_async_write(bh);
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462void mark_buffer_async_write(struct buffer_head *bh)
463{
Chris Mason35c80d52009-04-15 13:22:38 -0400464 mark_buffer_async_write_endio(bh, end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +0200466EXPORT_SYMBOL_NS(mark_buffer_async_write, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468
469/*
470 * fs/buffer.c contains helper functions for buffer-backed address space's
471 * fsync functions. A common requirement for buffer-based filesystems is
472 * that certain data from the backing blockdev needs to be written out for
473 * a successful fsync(). For example, ext2 indirect blocks need to be
474 * written back and waited upon before fsync() returns.
475 *
476 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
477 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
478 * management of a list of dependent buffers at ->i_mapping->private_list.
479 *
480 * Locking is a little subtle: try_to_free_buffers() will remove buffers
481 * from their controlling inode's queue when they are being freed. But
482 * try_to_free_buffers() will be operating against the *blockdev* mapping
483 * at the time, not against the S_ISREG file which depends on those buffers.
484 * So the locking for private_list is via the private_lock in the address_space
485 * which backs the buffers. Which is different from the address_space
486 * against which the buffers are listed. So for a particular address_space,
487 * mapping->private_lock does *not* protect mapping->private_list! In fact,
488 * mapping->private_list will always be protected by the backing blockdev's
489 * ->private_lock.
490 *
491 * Which introduces a requirement: all buffers on an address_space's
492 * ->private_list must be from the same address_space: the blockdev's.
493 *
494 * address_spaces which do not place buffers at ->private_list via these
495 * utility functions are free to use private_lock and private_list for
496 * whatever they want. The only requirement is that list_empty(private_list)
497 * be true at clear_inode() time.
498 *
499 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
500 * filesystems should do that. invalidate_inode_buffers() should just go
501 * BUG_ON(!list_empty).
502 *
503 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
504 * take an address_space, not an inode. And it should be called
505 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
506 * queued up.
507 *
508 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
509 * list if it is already on a list. Because if the buffer is on a list,
510 * it *must* already be on the right one. If not, the filesystem is being
511 * silly. This will save a ton of locking. But first we have to ensure
512 * that buffers are taken *off* the old inode's list when they are freed
513 * (presumably in truncate). That requires careful auditing of all
514 * filesystems (do it inside bforget()). It could also be done by bringing
515 * b_inode back.
516 */
517
518/*
519 * The buffer's backing address_space's private_lock must be held
520 */
Thomas Petazzonidbacefc2008-07-29 22:33:47 -0700521static void __remove_assoc_queue(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -0700524 WARN_ON(!bh->b_assoc_map);
Jan Kara58ff4072006-10-17 00:10:19 -0700525 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528int inode_has_buffers(struct inode *inode)
529{
530 return !list_empty(&inode->i_data.private_list);
531}
532
533/*
534 * osync is designed to support O_SYNC io. It waits synchronously for
535 * all already-submitted IO to complete, but does not queue any new
536 * writes to the disk.
537 *
538 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
539 * you dirty the buffers, and then use osync_inode_buffers to wait for
540 * completion. Any other dirty buffers which are not yet queued for
541 * write will not be flushed to disk by the osync.
542 */
543static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
544{
545 struct buffer_head *bh;
546 struct list_head *p;
547 int err = 0;
548
549 spin_lock(lock);
550repeat:
551 list_for_each_prev(p, list) {
552 bh = BH_ENTRY(p);
553 if (buffer_locked(bh)) {
554 get_bh(bh);
555 spin_unlock(lock);
556 wait_on_buffer(bh);
557 if (!buffer_uptodate(bh))
558 err = -EIO;
559 brelse(bh);
560 spin_lock(lock);
561 goto repeat;
562 }
563 }
564 spin_unlock(lock);
565 return err;
566}
567
Mateusz Guzik08fdc8a2017-10-03 18:17:41 +0200568void emergency_thaw_bdev(struct super_block *sb)
Al Viro01a05b32010-03-23 06:06:58 -0400569{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100570 while (sb->s_bdev && !thaw_bdev(sb->s_bdev))
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +0400571 printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
Al Viro01a05b32010-03-23 06:06:58 -0400572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800575 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
Martin Waitz67be2dd2005-05-01 08:59:26 -0700576 * @mapping: the mapping which wants those buffers written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
578 * Starts I/O against the buffers at mapping->private_list, and waits upon
579 * that I/O.
580 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700581 * Basically, this is a convenience function for fsync().
582 * @mapping is a file or directory which needs those buffers to be written for
583 * a successful fsync().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
585int sync_mapping_buffers(struct address_space *mapping)
586{
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800587 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
590 return 0;
591
592 return fsync_buffers_list(&buffer_mapping->private_lock,
593 &mapping->private_list);
594}
595EXPORT_SYMBOL(sync_mapping_buffers);
596
597/*
598 * Called when we've recently written block `bblock', and it is known that
599 * `bblock' was for a buffer_boundary() buffer. This means that the block at
600 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
601 * dirty, schedule it for IO. So that indirects merge nicely with their data.
602 */
603void write_boundary_block(struct block_device *bdev,
604 sector_t bblock, unsigned blocksize)
605{
606 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
607 if (bh) {
608 if (buffer_dirty(bh))
Mike Christiedfec8a12016-06-05 14:31:44 -0500609 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 put_bh(bh);
611 }
612}
613
614void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
615{
616 struct address_space *mapping = inode->i_mapping;
617 struct address_space *buffer_mapping = bh->b_page->mapping;
618
619 mark_buffer_dirty(bh);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800620 if (!mapping->private_data) {
621 mapping->private_data = buffer_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else {
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800623 BUG_ON(mapping->private_data != buffer_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Jan Kara535ee2f2008-02-08 04:21:59 -0800625 if (!bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 spin_lock(&buffer_mapping->private_lock);
627 list_move_tail(&bh->b_assoc_buffers,
628 &mapping->private_list);
Jan Kara58ff4072006-10-17 00:10:19 -0700629 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 spin_unlock(&buffer_mapping->private_lock);
631 }
632}
633EXPORT_SYMBOL(mark_buffer_dirty_inode);
634
635/*
636 * Add a page to the dirty page list.
637 *
638 * It is a sad fact of life that this function is called from several places
639 * deeply under spinlocking. It may not sleep.
640 *
641 * If the page has buffers, the uptodate buffers are set dirty, to preserve
642 * dirty-state coherency between the page and the buffers. It the page does
643 * not have buffers then when they are later attached they will all be set
644 * dirty.
645 *
646 * The buffers are dirtied before the page is dirtied. There's a small race
647 * window in which a writepage caller may see the page cleanness but not the
648 * buffer dirtiness. That's fine. If this code were to set the page dirty
649 * before the buffers, a concurrent writepage caller could clear the page dirty
650 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
651 * page on the dirty page list.
652 *
653 * We use private_lock to lock against try_to_free_buffers while using the
654 * page's buffer list. Also use this to protect against clean buffers being
655 * added to the page after it was set dirty.
656 *
657 * FIXME: may need to call ->reservepage here as well. That's rather up to the
658 * address_space though.
659 */
660int __set_page_dirty_buffers(struct page *page)
661{
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700662 int newly_dirty;
Nick Piggin787d2212007-07-17 04:03:34 -0700663 struct address_space *mapping = page_mapping(page);
Nick Pigginebf7a222006-10-10 04:36:54 +0200664
665 if (unlikely(!mapping))
666 return !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 spin_lock(&mapping->private_lock);
669 if (page_has_buffers(page)) {
670 struct buffer_head *head = page_buffers(page);
671 struct buffer_head *bh = head;
672
673 do {
674 set_buffer_dirty(bh);
675 bh = bh->b_this_page;
676 } while (bh != head);
677 }
Greg Thelenc4843a72015-05-22 17:13:16 -0400678 /*
Roman Gushchinbcfe06b2020-12-01 13:58:27 -0800679 * Lock out page's memcg migration to keep PageDirty
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700680 * synchronized with per-memcg dirty page counters.
Greg Thelenc4843a72015-05-22 17:13:16 -0400681 */
Johannes Weiner62cccb82016-03-15 14:57:22 -0700682 lock_page_memcg(page);
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700683 newly_dirty = !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 spin_unlock(&mapping->private_lock);
685
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700686 if (newly_dirty)
Johannes Weiner62cccb82016-03-15 14:57:22 -0700687 __set_page_dirty(page, mapping, 1);
Greg Thelenc4843a72015-05-22 17:13:16 -0400688
Johannes Weiner62cccb82016-03-15 14:57:22 -0700689 unlock_page_memcg(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400690
691 if (newly_dirty)
692 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
693
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700694 return newly_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +0200696EXPORT_SYMBOL_NS(__set_page_dirty_buffers, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/*
699 * Write out and wait upon a list of buffers.
700 *
701 * We have conflicting pressures: we want to make sure that all
702 * initially dirty buffers get waited on, but that any subsequently
703 * dirtied buffers don't. After all, we don't want fsync to last
704 * forever if somebody is actively writing to the file.
705 *
706 * Do this in two main stages: first we copy dirty buffers to a
707 * temporary inode list, queueing the writes as we go. Then we clean
708 * up, waiting for those writes to complete.
709 *
710 * During this second stage, any subsequent updates to the file may end
711 * up refiling the buffer on the original inode's dirty list again, so
712 * there is a chance we will end up with a buffer queued for write but
713 * not yet completed on that list. So, as a final cleanup we go through
714 * the osync code to catch these locked, dirty buffers without requeuing
715 * any newly dirty buffers for write.
716 */
717static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
718{
719 struct buffer_head *bh;
720 struct list_head tmp;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100721 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int err = 0, err2;
Jens Axboe4ee24912011-03-17 10:51:40 +0100723 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 INIT_LIST_HEAD(&tmp);
Jens Axboe4ee24912011-03-17 10:51:40 +0100726 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 spin_lock(lock);
729 while (!list_empty(list)) {
730 bh = BH_ENTRY(list->next);
Jan Kara535ee2f2008-02-08 04:21:59 -0800731 mapping = bh->b_assoc_map;
Jan Kara58ff4072006-10-17 00:10:19 -0700732 __remove_assoc_queue(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800733 /* Avoid race with mark_buffer_dirty_inode() which does
734 * a lockless check and we rely on seeing the dirty bit */
735 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (buffer_dirty(bh) || buffer_locked(bh)) {
737 list_add(&bh->b_assoc_buffers, &tmp);
Jan Kara535ee2f2008-02-08 04:21:59 -0800738 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (buffer_dirty(bh)) {
740 get_bh(bh);
741 spin_unlock(lock);
742 /*
743 * Ensure any pending I/O completes so that
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200744 * write_dirty_buffer() actually writes the
745 * current contents - it is a noop if I/O is
746 * still in flight on potentially older
747 * contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600749 write_dirty_buffer(bh, REQ_SYNC);
Jens Axboe9cf6b722009-04-06 14:48:03 +0200750
751 /*
752 * Kick off IO for the previous mapping. Note
753 * that we will not run the very last mapping,
754 * wait_on_buffer() will do that for us
755 * through sync_buffer().
756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 brelse(bh);
758 spin_lock(lock);
759 }
760 }
761 }
762
Jens Axboe4ee24912011-03-17 10:51:40 +0100763 spin_unlock(lock);
764 blk_finish_plug(&plug);
765 spin_lock(lock);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 while (!list_empty(&tmp)) {
768 bh = BH_ENTRY(tmp.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 get_bh(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800770 mapping = bh->b_assoc_map;
771 __remove_assoc_queue(bh);
772 /* Avoid race with mark_buffer_dirty_inode() which does
773 * a lockless check and we rely on seeing the dirty bit */
774 smp_mb();
775 if (buffer_dirty(bh)) {
776 list_add(&bh->b_assoc_buffers,
Jan Karae3892292008-03-04 14:28:33 -0800777 &mapping->private_list);
Jan Kara535ee2f2008-02-08 04:21:59 -0800778 bh->b_assoc_map = mapping;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_unlock(lock);
781 wait_on_buffer(bh);
782 if (!buffer_uptodate(bh))
783 err = -EIO;
784 brelse(bh);
785 spin_lock(lock);
786 }
787
788 spin_unlock(lock);
789 err2 = osync_buffers_list(lock, list);
790 if (err)
791 return err;
792 else
793 return err2;
794}
795
796/*
797 * Invalidate any and all dirty buffers on a given inode. We are
798 * probably unmounting the fs, but that doesn't mean we have already
799 * done a sync(). Just drop the buffers from the inode list.
800 *
801 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
802 * assumes that all the buffers are against the blockdev. Not true
803 * for reiserfs.
804 */
805void invalidate_inode_buffers(struct inode *inode)
806{
807 if (inode_has_buffers(inode)) {
808 struct address_space *mapping = &inode->i_data;
809 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800810 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 spin_lock(&buffer_mapping->private_lock);
813 while (!list_empty(list))
814 __remove_assoc_queue(BH_ENTRY(list->next));
815 spin_unlock(&buffer_mapping->private_lock);
816 }
817}
Jan Kara52b19ac2008-09-23 18:24:08 +0200818EXPORT_SYMBOL(invalidate_inode_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820/*
821 * Remove any clean buffers from the inode's buffer list. This is called
822 * when we're trying to free the inode itself. Those buffers can pin it.
823 *
824 * Returns true if all buffers were removed.
825 */
826int remove_inode_buffers(struct inode *inode)
827{
828 int ret = 1;
829
830 if (inode_has_buffers(inode)) {
831 struct address_space *mapping = &inode->i_data;
832 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800833 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 spin_lock(&buffer_mapping->private_lock);
836 while (!list_empty(list)) {
837 struct buffer_head *bh = BH_ENTRY(list->next);
838 if (buffer_dirty(bh)) {
839 ret = 0;
840 break;
841 }
842 __remove_assoc_queue(bh);
843 }
844 spin_unlock(&buffer_mapping->private_lock);
845 }
846 return ret;
847}
848
849/*
850 * Create the appropriate buffers when given a page for data area and
851 * the size of each buffer.. Use the bh->b_this_page linked list to
852 * follow the buffers created. Return NULL if unable to create more
853 * buffers.
854 *
855 * The retry flag is used to differentiate async IO (paging, swapping)
856 * which may not fail from ordinary buffer allocations.
857 */
858struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
Jens Axboe640ab982017-09-27 05:40:16 -0600859 bool retry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct buffer_head *bh, *head;
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700862 gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 long offset;
Roman Gushchinb87d8ce2020-10-17 16:13:40 -0700864 struct mem_cgroup *memcg, *old_memcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Jens Axboe640ab982017-09-27 05:40:16 -0600866 if (retry)
867 gfp |= __GFP_NOFAIL;
868
Johannes Weiner6eeb1042021-02-24 12:04:15 -0800869 /* The page lock pins the memcg */
870 memcg = page_memcg(page);
Roman Gushchinb87d8ce2020-10-17 16:13:40 -0700871 old_memcg = set_active_memcg(memcg);
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 head = NULL;
874 offset = PAGE_SIZE;
875 while ((offset -= size) >= 0) {
Jens Axboe640ab982017-09-27 05:40:16 -0600876 bh = alloc_buffer_head(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!bh)
878 goto no_grow;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 bh->b_this_page = head;
881 bh->b_blocknr = -1;
882 head = bh;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 bh->b_size = size;
885
886 /* Link the buffer to its page */
887 set_bh_page(bh, page, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700889out:
Roman Gushchinb87d8ce2020-10-17 16:13:40 -0700890 set_active_memcg(old_memcg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return head;
892/*
893 * In case anything failed, we just free everything we got.
894 */
895no_grow:
896 if (head) {
897 do {
898 bh = head;
899 head = head->b_this_page;
900 free_buffer_head(bh);
901 } while (head);
902 }
903
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906EXPORT_SYMBOL_GPL(alloc_page_buffers);
907
908static inline void
909link_dev_buffers(struct page *page, struct buffer_head *head)
910{
911 struct buffer_head *bh, *tail;
912
913 bh = head;
914 do {
915 tail = bh;
916 bh = bh->b_this_page;
917 } while (bh);
918 tail->b_this_page = head;
Guoqing Jiang45dcfc22020-06-01 21:47:48 -0700919 attach_page_private(page, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800922static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
923{
924 sector_t retval = ~((sector_t)0);
925 loff_t sz = i_size_read(bdev->bd_inode);
926
927 if (sz) {
928 unsigned int sizebits = blksize_bits(size);
929 retval = (sz >> sizebits);
930 }
931 return retval;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/*
935 * Initialise the state of a blockdev page's buffers.
936 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200937static sector_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938init_page_buffers(struct page *page, struct block_device *bdev,
939 sector_t block, int size)
940{
941 struct buffer_head *head = page_buffers(page);
942 struct buffer_head *bh = head;
943 int uptodate = PageUptodate(page);
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800944 sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 do {
947 if (!buffer_mapped(bh)) {
Eric Biggers01950a32018-01-16 22:25:12 -0800948 bh->b_end_io = NULL;
949 bh->b_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 bh->b_bdev = bdev;
951 bh->b_blocknr = block;
952 if (uptodate)
953 set_buffer_uptodate(bh);
Jeff Moyer080399a2012-05-11 16:34:10 +0200954 if (block < end_block)
955 set_buffer_mapped(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 block++;
958 bh = bh->b_this_page;
959 } while (bh != head);
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200960
961 /*
962 * Caller needs to validate requested block against end of device.
963 */
964 return end_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967/*
968 * Create the page-cache page that contains the requested block.
969 *
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200970 * This is used purely for blockdev mappings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200972static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973grow_dev_page(struct block_device *bdev, sector_t block,
Gioh Kim3b5e6452014-09-04 22:04:42 -0400974 pgoff_t index, int size, int sizebits, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct inode *inode = bdev->bd_inode;
977 struct page *page;
978 struct buffer_head *bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200979 sector_t end_block;
Zhiqiang Liuc4b4c2a2020-04-13 13:12:10 +0800980 int ret = 0;
Johannes Weiner84235de2013-10-16 13:47:00 -0700981 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Michal Hockoc62d2552015-11-06 16:28:49 -0800983 gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
Gioh Kim3b5e6452014-09-04 22:04:42 -0400984
Johannes Weiner84235de2013-10-16 13:47:00 -0700985 /*
986 * XXX: __getblk_slow() can not really deal with failure and
987 * will endlessly loop on improvised global reclaim. Prefer
988 * looping in the allocator rather than here, at least that
989 * code knows what it's doing.
990 */
991 gfp_mask |= __GFP_NOFAIL;
992
993 page = find_or_create_page(inode->i_mapping, index, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Eric Sesterhenne827f922006-03-26 18:24:46 +0200995 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (page_has_buffers(page)) {
998 bh = page_buffers(page);
999 if (bh->b_size == size) {
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001000 end_block = init_page_buffers(page, bdev,
Anton Altaparmakovf2d5a942014-09-22 01:53:03 +01001001 (sector_t)index << sizebits,
1002 size);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001003 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 if (!try_to_free_buffers(page))
1006 goto failed;
1007 }
1008
1009 /*
1010 * Allocate some buffers for this page
1011 */
Jens Axboe94dc24c2017-09-27 05:45:36 -06001012 bh = alloc_page_buffers(page, size, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
1015 * Link the page to the buffers and initialise them. Take the
1016 * lock to be atomic wrt __find_get_block(), which does not
1017 * run under the page lock.
1018 */
1019 spin_lock(&inode->i_mapping->private_lock);
1020 link_dev_buffers(page, bh);
Anton Altaparmakovf2d5a942014-09-22 01:53:03 +01001021 end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
1022 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 spin_unlock(&inode->i_mapping->private_lock);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001024done:
1025 ret = (block < end_block) ? 1 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 put_page(page);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001029 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * Create buffers for the specified block device block's page. If
1034 * that page was dirty, the buffers are set dirty also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001036static int
Gioh Kim3b5e6452014-09-04 22:04:42 -04001037grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 pgoff_t index;
1040 int sizebits;
1041
Mikulas Patocka90432e62021-03-22 10:05:05 -04001042 sizebits = PAGE_SHIFT - __ffs(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 index = block >> sizebits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Andrew Mortone5657932006-10-11 01:21:46 -07001045 /*
1046 * Check for a block which wants to lie outside our maximum possible
1047 * pagecache index. (this comparison is done using sector_t types).
1048 */
1049 if (unlikely(index != block >> sizebits)) {
Andrew Mortone5657932006-10-11 01:21:46 -07001050 printk(KERN_ERR "%s: requested out-of-range block %llu for "
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001051 "device %pg\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001052 __func__, (unsigned long long)block,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001053 bdev);
Andrew Mortone5657932006-10-11 01:21:46 -07001054 return -EIO;
1055 }
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* Create a page with the proper size buffers.. */
Gioh Kim3b5e6452014-09-04 22:04:42 -04001058 return grow_dev_page(bdev, block, index, size, sizebits, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Eric Biggers0026ba42016-09-12 13:30:41 -07001061static struct buffer_head *
Gioh Kim3b5e6452014-09-04 22:04:42 -04001062__getblk_slow(struct block_device *bdev, sector_t block,
1063 unsigned size, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 /* Size must be multiple of hard sectorsize */
Martin K. Petersene1defc42009-05-22 17:17:49 -04001066 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 (size < 512 || size > PAGE_SIZE))) {
1068 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1069 size);
Martin K. Petersene1defc42009-05-22 17:17:49 -04001070 printk(KERN_ERR "logical block size: %d\n",
1071 bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 dump_stack();
1074 return NULL;
1075 }
1076
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001077 for (;;) {
1078 struct buffer_head *bh;
1079 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 bh = __find_get_block(bdev, block, size);
1082 if (bh)
1083 return bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001084
Gioh Kim3b5e6452014-09-04 22:04:42 -04001085 ret = grow_buffers(bdev, block, size, gfp);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001086 if (ret < 0)
1087 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089}
1090
1091/*
1092 * The relationship between dirty buffers and dirty pages:
1093 *
1094 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
Matthew Wilcoxec82e1c2017-12-04 10:40:41 -05001095 * the page is tagged dirty in the page cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 *
1097 * At all times, the dirtiness of the buffers represents the dirtiness of
1098 * subsections of the page. If the page has buffers, the page dirty bit is
1099 * merely a hint about the true dirty state.
1100 *
1101 * When a page is set dirty in its entirety, all its buffers are marked dirty
1102 * (if the page has buffers).
1103 *
1104 * When a buffer is marked dirty, its page is dirtied, but the page's other
1105 * buffers are not.
1106 *
1107 * Also. When blockdev buffers are explicitly read with bread(), they
1108 * individually become uptodate. But their backing page remains not
1109 * uptodate - even if all of its buffers are uptodate. A subsequent
1110 * block_read_full_page() against that page will discover all the uptodate
1111 * buffers, will set the page uptodate and will perform no I/O.
1112 */
1113
1114/**
1115 * mark_buffer_dirty - mark a buffer_head as needing writeout
Martin Waitz67be2dd2005-05-01 08:59:26 -07001116 * @bh: the buffer_head to mark dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *
Matthew Wilcoxec82e1c2017-12-04 10:40:41 -05001118 * mark_buffer_dirty() will set the dirty bit against the buffer, then set
1119 * its backing page dirty, then tag the page as dirty in the page cache
1120 * and then attach the address_space's inode to its superblock's dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 * inode list.
1122 *
1123 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001124 * i_pages lock and mapping->host->i_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001126void mark_buffer_dirty(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Nick Piggin787d2212007-07-17 04:03:34 -07001128 WARN_ON_ONCE(!buffer_uptodate(bh));
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001129
Tejun Heo5305cb82013-01-11 13:06:36 -08001130 trace_block_dirty_buffer(bh);
1131
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001132 /*
1133 * Very *carefully* optimize the it-is-already-dirty case.
1134 *
1135 * Don't let the final "is it dirty" escape to before we
1136 * perhaps modified the buffer.
1137 */
1138 if (buffer_dirty(bh)) {
1139 smp_mb();
1140 if (buffer_dirty(bh))
1141 return;
1142 }
1143
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001144 if (!test_set_buffer_dirty(bh)) {
1145 struct page *page = bh->b_page;
Greg Thelenc4843a72015-05-22 17:13:16 -04001146 struct address_space *mapping = NULL;
Greg Thelenc4843a72015-05-22 17:13:16 -04001147
Johannes Weiner62cccb82016-03-15 14:57:22 -07001148 lock_page_memcg(page);
Linus Torvalds8e9d78e2009-08-21 17:40:08 -07001149 if (!TestSetPageDirty(page)) {
Greg Thelenc4843a72015-05-22 17:13:16 -04001150 mapping = page_mapping(page);
Linus Torvalds8e9d78e2009-08-21 17:40:08 -07001151 if (mapping)
Johannes Weiner62cccb82016-03-15 14:57:22 -07001152 __set_page_dirty(page, mapping, 0);
Linus Torvalds8e9d78e2009-08-21 17:40:08 -07001153 }
Johannes Weiner62cccb82016-03-15 14:57:22 -07001154 unlock_page_memcg(page);
Greg Thelenc4843a72015-05-22 17:13:16 -04001155 if (mapping)
1156 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001159EXPORT_SYMBOL_NS(mark_buffer_dirty, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jeff Layton87354e52017-07-06 07:02:21 -04001161void mark_buffer_write_io_error(struct buffer_head *bh)
1162{
Jeff Layton485e9602020-06-01 21:45:40 -07001163 struct super_block *sb;
1164
Jeff Layton87354e52017-07-06 07:02:21 -04001165 set_buffer_write_io_error(bh);
1166 /* FIXME: do we need to set this in both places? */
1167 if (bh->b_page && bh->b_page->mapping)
1168 mapping_set_error(bh->b_page->mapping, -EIO);
1169 if (bh->b_assoc_map)
1170 mapping_set_error(bh->b_assoc_map, -EIO);
Jeff Layton485e9602020-06-01 21:45:40 -07001171 rcu_read_lock();
1172 sb = READ_ONCE(bh->b_bdev->bd_super);
1173 if (sb)
1174 errseq_set(&sb->s_wb_err, -EIO);
1175 rcu_read_unlock();
Jeff Layton87354e52017-07-06 07:02:21 -04001176}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001177EXPORT_SYMBOL_NS(mark_buffer_write_io_error, ANDROID_GKI_VFS_EXPORT_ONLY);
Jeff Layton87354e52017-07-06 07:02:21 -04001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179/*
1180 * Decrement a buffer_head's reference count. If all buffers against a page
1181 * have zero reference count, are clean and unlocked, and if the page is clean
1182 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1183 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1184 * a page but it ends up not being freed, and buffers may later be reattached).
1185 */
1186void __brelse(struct buffer_head * buf)
1187{
1188 if (atomic_read(&buf->b_count)) {
1189 put_bh(buf);
1190 return;
1191 }
Arjan van de Ven5c752ad2008-07-25 19:45:40 -07001192 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001194EXPORT_SYMBOL_NS(__brelse, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196/*
1197 * bforget() is like brelse(), except it discards any
1198 * potentially dirty data.
1199 */
1200void __bforget(struct buffer_head *bh)
1201{
1202 clear_buffer_dirty(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -08001203 if (bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct address_space *buffer_mapping = bh->b_page->mapping;
1205
1206 spin_lock(&buffer_mapping->private_lock);
1207 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -07001208 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_unlock(&buffer_mapping->private_lock);
1210 }
1211 __brelse(bh);
1212}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001213EXPORT_SYMBOL_NS(__bforget, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215static struct buffer_head *__bread_slow(struct buffer_head *bh)
1216{
1217 lock_buffer(bh);
1218 if (buffer_uptodate(bh)) {
1219 unlock_buffer(bh);
1220 return bh;
1221 } else {
1222 get_bh(bh);
1223 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05001224 submit_bh(REQ_OP_READ, 0, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 wait_on_buffer(bh);
1226 if (buffer_uptodate(bh))
1227 return bh;
1228 }
1229 brelse(bh);
1230 return NULL;
1231}
1232
1233/*
1234 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1235 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1236 * refcount elevated by one when they're in an LRU. A buffer can only appear
1237 * once in a particular CPU's LRU. A single buffer can be present in multiple
1238 * CPU's LRUs at the same time.
1239 *
1240 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1241 * sb_find_get_block().
1242 *
1243 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1244 * a local interrupt disable for that.
1245 */
1246
Sebastien Buisson86cf78d2014-10-09 15:29:38 -07001247#define BH_LRU_SIZE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249struct bh_lru {
1250 struct buffer_head *bhs[BH_LRU_SIZE];
1251};
1252
1253static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1254
1255#ifdef CONFIG_SMP
1256#define bh_lru_lock() local_irq_disable()
1257#define bh_lru_unlock() local_irq_enable()
1258#else
1259#define bh_lru_lock() preempt_disable()
1260#define bh_lru_unlock() preempt_enable()
1261#endif
1262
1263static inline void check_irqs_on(void)
1264{
1265#ifdef irqs_disabled
1266 BUG_ON(irqs_disabled());
1267#endif
1268}
1269
1270/*
Eric Biggers241f01f2017-07-10 15:47:29 -07001271 * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
1272 * inserted at the front, and the buffer_head at the back if any is evicted.
1273 * Or, if already in the LRU it is moved to the front.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
1275static void bh_lru_install(struct buffer_head *bh)
1276{
Eric Biggers241f01f2017-07-10 15:47:29 -07001277 struct buffer_head *evictee = bh;
1278 struct bh_lru *b;
1279 int i;
Minchan Kimba005d62022-10-20 16:03:35 -07001280 bool skip = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 check_irqs_on();
Minchan Kimab657a22022-03-22 14:39:34 -07001283 bh_lru_lock();
1284
Minchan Kim8cc621d2021-05-04 18:37:00 -07001285 /*
1286 * the refcount of buffer_head in bh_lru prevents dropping the
1287 * attached page(i.e., try_to_free_buffers) so it could cause
1288 * failing page migration.
1289 * Skip putting upcoming bh into bh_lru until migration is done.
1290 */
Minchan Kimab657a22022-03-22 14:39:34 -07001291 if (lru_cache_disabled()) {
1292 bh_lru_unlock();
Minchan Kim8cc621d2021-05-04 18:37:00 -07001293 return;
Minchan Kimab657a22022-03-22 14:39:34 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Minchan Kimba005d62022-10-20 16:03:35 -07001296 trace_android_vh_bh_lru_install(bh->b_page, &skip);
1297 if (skip) {
1298 bh_lru_unlock();
1299 return;
1300 }
1301
Eric Biggers241f01f2017-07-10 15:47:29 -07001302 b = this_cpu_ptr(&bh_lrus);
1303 for (i = 0; i < BH_LRU_SIZE; i++) {
1304 swap(evictee, b->bhs[i]);
1305 if (evictee == bh) {
1306 bh_lru_unlock();
1307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Eric Biggers241f01f2017-07-10 15:47:29 -07001311 get_bh(bh);
1312 bh_lru_unlock();
1313 brelse(evictee);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
1316/*
1317 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1318 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001319static struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001320lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct buffer_head *ret = NULL;
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001323 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 check_irqs_on();
1326 bh_lru_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 for (i = 0; i < BH_LRU_SIZE; i++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001328 struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Zach Brown9470dd52014-10-13 15:55:05 -07001330 if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
1331 bh->b_size == size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (i) {
1333 while (i) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001334 __this_cpu_write(bh_lrus.bhs[i],
1335 __this_cpu_read(bh_lrus.bhs[i - 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 i--;
1337 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06001338 __this_cpu_write(bh_lrus.bhs[0], bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 get_bh(bh);
1341 ret = bh;
1342 break;
1343 }
1344 }
1345 bh_lru_unlock();
1346 return ret;
1347}
1348
1349/*
1350 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1351 * it in the LRU and mark it as accessed. If it is not present then return
1352 * NULL
1353 */
1354struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001355__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1358
1359 if (bh == NULL) {
Mel Gorman2457aec2014-06-04 16:10:31 -07001360 /* __find_get_block_slow will mark the page accessed */
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001361 bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (bh)
1363 bh_lru_install(bh);
Mel Gorman2457aec2014-06-04 16:10:31 -07001364 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 touch_buffer(bh);
Mel Gorman2457aec2014-06-04 16:10:31 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return bh;
1368}
1369EXPORT_SYMBOL(__find_get_block);
1370
1371/*
Gioh Kim3b5e6452014-09-04 22:04:42 -04001372 * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * which corresponds to the passed block_device, block and size. The
1374 * returned buffer has its reference count incremented.
1375 *
Gioh Kim3b5e6452014-09-04 22:04:42 -04001376 * __getblk_gfp() will lock up the machine if grow_dev_page's
1377 * try_to_free_buffers() attempt is failing. FIXME, perhaps?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
1379struct buffer_head *
Gioh Kim3b5e6452014-09-04 22:04:42 -04001380__getblk_gfp(struct block_device *bdev, sector_t block,
1381 unsigned size, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct buffer_head *bh = __find_get_block(bdev, block, size);
1384
1385 might_sleep();
1386 if (bh == NULL)
Gioh Kim3b5e6452014-09-04 22:04:42 -04001387 bh = __getblk_slow(bdev, block, size, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return bh;
1389}
Gioh Kim3b5e6452014-09-04 22:04:42 -04001390EXPORT_SYMBOL(__getblk_gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392/*
1393 * Do async read-ahead on a buffer..
1394 */
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001395void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct buffer_head *bh = __getblk(bdev, block, size);
Andrew Mortona3e713b2005-10-30 15:03:15 -08001398 if (likely(bh)) {
Christoph Hellwig70246282016-07-19 11:28:41 +02001399 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
Andrew Mortona3e713b2005-10-30 15:03:15 -08001400 brelse(bh);
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001403EXPORT_SYMBOL_NS(__breadahead, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Roman Gushchind87f6392020-02-28 16:14:11 -08001405void __breadahead_gfp(struct block_device *bdev, sector_t block, unsigned size,
1406 gfp_t gfp)
1407{
1408 struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
1409 if (likely(bh)) {
1410 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
1411 brelse(bh);
1412 }
1413}
1414EXPORT_SYMBOL(__breadahead_gfp);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/**
Gioh Kim3b5e6452014-09-04 22:04:42 -04001417 * __bread_gfp() - reads a specified block and returns the bh
Martin Waitz67be2dd2005-05-01 08:59:26 -07001418 * @bdev: the block_device to read from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * @block: number of block
1420 * @size: size (in bytes) to read
Gioh Kim3b5e6452014-09-04 22:04:42 -04001421 * @gfp: page allocation flag
1422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * Reads a specified block, and returns buffer head that contains it.
Gioh Kim3b5e6452014-09-04 22:04:42 -04001424 * The page cache can be allocated from non-movable area
1425 * not to prevent page migration if you set gfp to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * It returns NULL if the block was unreadable.
1427 */
1428struct buffer_head *
Gioh Kim3b5e6452014-09-04 22:04:42 -04001429__bread_gfp(struct block_device *bdev, sector_t block,
1430 unsigned size, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Gioh Kim3b5e6452014-09-04 22:04:42 -04001432 struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Andrew Mortona3e713b2005-10-30 15:03:15 -08001434 if (likely(bh) && !buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 bh = __bread_slow(bh);
1436 return bh;
1437}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001438EXPORT_SYMBOL_NS(__bread_gfp, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Minchan Kim8cc621d2021-05-04 18:37:00 -07001440static void __invalidate_bh_lrus(struct bh_lru *b)
1441{
1442 int i;
1443
1444 for (i = 0; i < BH_LRU_SIZE; i++) {
1445 brelse(b->bhs[i]);
1446 b->bhs[i] = NULL;
1447 }
1448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/*
1450 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1451 * This doesn't race because it runs in each cpu either in irq
1452 * or with preempt disabled.
1453 */
1454static void invalidate_bh_lru(void *arg)
1455{
1456 struct bh_lru *b = &get_cpu_var(bh_lrus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Minchan Kim8cc621d2021-05-04 18:37:00 -07001458 __invalidate_bh_lrus(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 put_cpu_var(bh_lrus);
1460}
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001461
Minchan Kim8cc621d2021-05-04 18:37:00 -07001462bool has_bh_in_lru(int cpu, void *dummy)
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001463{
1464 struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
1465 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001467 for (i = 0; i < BH_LRU_SIZE; i++) {
1468 if (b->bhs[i])
Saurav Girepunje1d706672019-11-30 17:49:15 -08001469 return true;
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001470 }
1471
Saurav Girepunje1d706672019-11-30 17:49:15 -08001472 return false;
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001473}
1474
Peter Zijlstraf9a14392007-05-06 14:49:55 -07001475void invalidate_bh_lrus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +01001477 on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
Nick Piggin9db55792008-02-08 04:19:49 -08001479EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Minchan Kim243418e2021-09-24 15:43:47 -07001481/*
1482 * It's called from workqueue context so we need a bh_lru_lock to close
1483 * the race with preemption/irq.
1484 */
1485void invalidate_bh_lrus_cpu(void)
Minchan Kim8cc621d2021-05-04 18:37:00 -07001486{
1487 struct bh_lru *b;
1488
1489 bh_lru_lock();
Minchan Kim243418e2021-09-24 15:43:47 -07001490 b = this_cpu_ptr(&bh_lrus);
Minchan Kim8cc621d2021-05-04 18:37:00 -07001491 __invalidate_bh_lrus(b);
1492 bh_lru_unlock();
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495void set_bh_page(struct buffer_head *bh,
1496 struct page *page, unsigned long offset)
1497{
1498 bh->b_page = page;
Eric Sesterhenne827f922006-03-26 18:24:46 +02001499 BUG_ON(offset >= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (PageHighMem(page))
1501 /*
1502 * This catches illegal uses and preserves the offset:
1503 */
1504 bh->b_data = (char *)(0 + offset);
1505 else
1506 bh->b_data = page_address(page) + offset;
1507}
1508EXPORT_SYMBOL(set_bh_page);
1509
1510/*
1511 * Called when truncating a buffer on a page completely.
1512 */
Mel Gormane7470ee2014-06-04 16:10:29 -07001513
1514/* Bits that are cleared during an invalidate */
1515#define BUFFER_FLAGS_DISCARD \
1516 (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
1517 1 << BH_Delay | 1 << BH_Unwritten)
1518
Arjan van de Ven858119e2006-01-14 13:20:43 -08001519static void discard_buffer(struct buffer_head * bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Mel Gormane7470ee2014-06-04 16:10:29 -07001521 unsigned long b_state, b_state_old;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 lock_buffer(bh);
1524 clear_buffer_dirty(bh);
1525 bh->b_bdev = NULL;
Mel Gormane7470ee2014-06-04 16:10:29 -07001526 b_state = bh->b_state;
1527 for (;;) {
1528 b_state_old = cmpxchg(&bh->b_state, b_state,
1529 (b_state & ~BUFFER_FLAGS_DISCARD));
1530 if (b_state_old == b_state)
1531 break;
1532 b_state = b_state_old;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 unlock_buffer(bh);
1535}
1536
1537/**
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001538 * block_invalidatepage - invalidate part or all of a buffer-backed page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 *
1540 * @page: the page which is affected
Lukas Czernerd47992f2013-05-21 23:17:23 -04001541 * @offset: start of the range to invalidate
1542 * @length: length of the range to invalidate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 *
1544 * block_invalidatepage() is called when all or part of the page has become
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001545 * invalidated by a truncate operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 *
1547 * block_invalidatepage() does not have to release all buffers, but it must
1548 * ensure that no dirty buffer is left outside @offset and that no I/O
1549 * is underway against any of the blocks which are outside the truncation
1550 * point. Because the caller is about to free (and possibly reuse) those
1551 * blocks on-disk.
1552 */
Lukas Czernerd47992f2013-05-21 23:17:23 -04001553void block_invalidatepage(struct page *page, unsigned int offset,
1554 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 struct buffer_head *head, *bh, *next;
1557 unsigned int curr_off = 0;
Lukas Czernerd47992f2013-05-21 23:17:23 -04001558 unsigned int stop = length + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 BUG_ON(!PageLocked(page));
1561 if (!page_has_buffers(page))
1562 goto out;
1563
Lukas Czernerd47992f2013-05-21 23:17:23 -04001564 /*
1565 * Check for overflow
1566 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001567 BUG_ON(stop > PAGE_SIZE || stop < length);
Lukas Czernerd47992f2013-05-21 23:17:23 -04001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 head = page_buffers(page);
1570 bh = head;
1571 do {
1572 unsigned int next_off = curr_off + bh->b_size;
1573 next = bh->b_this_page;
1574
1575 /*
Lukas Czernerd47992f2013-05-21 23:17:23 -04001576 * Are we still fully in range ?
1577 */
1578 if (next_off > stop)
1579 goto out;
1580
1581 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * is this block fully invalidated?
1583 */
1584 if (offset <= curr_off)
1585 discard_buffer(bh);
1586 curr_off = next_off;
1587 bh = next;
1588 } while (bh != head);
1589
1590 /*
1591 * We release buffers only if the entire page is being invalidated.
1592 * The get_block cached value has been unconditionally invalidated,
1593 * so real IO is not possible anymore.
1594 */
Jeff Moyer31724852018-04-05 16:25:01 -07001595 if (length == PAGE_SIZE)
NeilBrown2ff28e22006-03-26 01:37:18 -08001596 try_to_release_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597out:
NeilBrown2ff28e22006-03-26 01:37:18 -08001598 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001600EXPORT_SYMBOL_NS(block_invalidatepage, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Lukas Czernerd47992f2013-05-21 23:17:23 -04001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603/*
1604 * We attach and possibly dirty the buffers atomically wrt
1605 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1606 * is already excluded via the page lock.
1607 */
1608void create_empty_buffers(struct page *page,
1609 unsigned long blocksize, unsigned long b_state)
1610{
1611 struct buffer_head *bh, *head, *tail;
1612
Jens Axboe640ab982017-09-27 05:40:16 -06001613 head = alloc_page_buffers(page, blocksize, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 bh = head;
1615 do {
1616 bh->b_state |= b_state;
1617 tail = bh;
1618 bh = bh->b_this_page;
1619 } while (bh);
1620 tail->b_this_page = head;
1621
1622 spin_lock(&page->mapping->private_lock);
1623 if (PageUptodate(page) || PageDirty(page)) {
1624 bh = head;
1625 do {
1626 if (PageDirty(page))
1627 set_buffer_dirty(bh);
1628 if (PageUptodate(page))
1629 set_buffer_uptodate(bh);
1630 bh = bh->b_this_page;
1631 } while (bh != head);
1632 }
Guoqing Jiang45dcfc22020-06-01 21:47:48 -07001633 attach_page_private(page, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 spin_unlock(&page->mapping->private_lock);
1635}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001636EXPORT_SYMBOL_NS(create_empty_buffers, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Jan Kara29f3ad72016-11-04 18:08:11 +01001638/**
1639 * clean_bdev_aliases: clean a range of buffers in block device
1640 * @bdev: Block device to clean buffers in
1641 * @block: Start of a range of blocks to clean
1642 * @len: Number of blocks to clean
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 *
Jan Kara29f3ad72016-11-04 18:08:11 +01001644 * We are taking a range of blocks for data and we don't want writeback of any
1645 * buffer-cache aliases starting from return from this function and until the
1646 * moment when something will explicitly mark the buffer dirty (hopefully that
1647 * will not happen until we will free that block ;-) We don't even need to mark
1648 * it not-uptodate - nobody can expect anything from a newly allocated buffer
1649 * anyway. We used to use unmap_buffer() for such invalidation, but that was
1650 * wrong. We definitely don't want to mark the alias unmapped, for example - it
1651 * would confuse anyone who might pick it with bread() afterwards...
1652 *
1653 * Also.. Note that bforget() doesn't lock the buffer. So there can be
1654 * writeout I/O going on against recently-freed buffers. We don't wait on that
1655 * I/O in bforget() - it's more efficient to wait on the I/O only if we really
1656 * need to. That happens here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 */
Jan Kara29f3ad72016-11-04 18:08:11 +01001658void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Jan Kara29f3ad72016-11-04 18:08:11 +01001660 struct inode *bd_inode = bdev->bd_inode;
1661 struct address_space *bd_mapping = bd_inode->i_mapping;
1662 struct pagevec pvec;
1663 pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
1664 pgoff_t end;
Jan Karac10f7782017-09-06 16:21:24 -07001665 int i, count;
Jan Kara29f3ad72016-11-04 18:08:11 +01001666 struct buffer_head *bh;
1667 struct buffer_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jan Kara29f3ad72016-11-04 18:08:11 +01001669 end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
Mel Gorman86679822017-11-15 17:37:52 -08001670 pagevec_init(&pvec);
Jan Kara397162f2017-09-06 16:21:43 -07001671 while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
Jan Karac10f7782017-09-06 16:21:24 -07001672 count = pagevec_count(&pvec);
1673 for (i = 0; i < count; i++) {
Jan Kara29f3ad72016-11-04 18:08:11 +01001674 struct page *page = pvec.pages[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Jan Kara29f3ad72016-11-04 18:08:11 +01001676 if (!page_has_buffers(page))
1677 continue;
1678 /*
1679 * We use page lock instead of bd_mapping->private_lock
1680 * to pin buffers here since we can afford to sleep and
1681 * it scales better than a global spinlock lock.
1682 */
1683 lock_page(page);
1684 /* Recheck when the page is locked which pins bhs */
1685 if (!page_has_buffers(page))
1686 goto unlock_page;
1687 head = page_buffers(page);
1688 bh = head;
1689 do {
Chandan Rajendra6c006a92016-12-25 19:01:03 +05301690 if (!buffer_mapped(bh) || (bh->b_blocknr < block))
Jan Kara29f3ad72016-11-04 18:08:11 +01001691 goto next;
1692 if (bh->b_blocknr >= block + len)
1693 break;
1694 clear_buffer_dirty(bh);
1695 wait_on_buffer(bh);
1696 clear_buffer_req(bh);
1697next:
1698 bh = bh->b_this_page;
1699 } while (bh != head);
1700unlock_page:
1701 unlock_page(page);
1702 }
1703 pagevec_release(&pvec);
1704 cond_resched();
Jan Karac10f7782017-09-06 16:21:24 -07001705 /* End of range already reached? */
1706 if (index > end || !index)
1707 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001710EXPORT_SYMBOL_NS(clean_bdev_aliases, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712/*
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001713 * Size is a power-of-two in the range 512..PAGE_SIZE,
1714 * and the case we care about most is PAGE_SIZE.
1715 *
1716 * So this *could* possibly be written with those
1717 * constraints in mind (relevant mostly if some
1718 * architecture has a slow bit-scan instruction)
1719 */
1720static inline int block_size_bits(unsigned int blocksize)
1721{
1722 return ilog2(blocksize);
1723}
1724
1725static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
1726{
1727 BUG_ON(!PageLocked(page));
1728
1729 if (!page_has_buffers(page))
Mark Rutland6aa7de02017-10-23 14:07:29 -07001730 create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
1731 b_state);
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001732 return page_buffers(page);
1733}
1734
1735/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * NOTE! All mapped/uptodate combinations are valid:
1737 *
1738 * Mapped Uptodate Meaning
1739 *
1740 * No No "unknown" - must do get_block()
1741 * No Yes "hole" - zero-filled
1742 * Yes No "allocated" - allocated on disk, not read in
1743 * Yes Yes "valid" - allocated and up-to-date in memory.
1744 *
1745 * "Dirty" is valid only with the last case (mapped+uptodate).
1746 */
1747
1748/*
1749 * While block_write_full_page is writing back the dirty buffers under
1750 * the page lock, whoever dirtied the buffers may decide to clean them
1751 * again at any time. We handle that by only looking at the buffer
1752 * state inside lock_buffer().
1753 *
1754 * If block_write_full_page() is called for regular writeback
1755 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1756 * locked buffer. This only can happen if someone has written the buffer
1757 * directly, with submit_bh(). At the address_space level PageWriteback
1758 * prevents this contention from occurring.
Theodore Ts'o6e34eedd2009-04-07 18:12:43 -04001759 *
1760 * If block_write_full_page() is called with wbc->sync_mode ==
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001761 * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
Jens Axboe721a9602011-03-09 11:56:30 +01001762 * causes the writes to be flagged as synchronous writes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
Benjamin Marzinskib4bba382016-06-27 09:58:40 -05001764int __block_write_full_page(struct inode *inode, struct page *page,
Chris Mason35c80d52009-04-15 13:22:38 -04001765 get_block_t *get_block, struct writeback_control *wbc,
1766 bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 int err;
1769 sector_t block;
1770 sector_t last_block;
Andrew Mortonf0fbd5f2005-05-05 16:15:48 -07001771 struct buffer_head *bh, *head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001772 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 int nr_underway = 0;
Jens Axboe76372412016-11-01 10:00:38 -06001774 int write_flags = wbc_to_write_flags(wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001776 head = create_page_buffers(page, inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 (1 << BH_Dirty)|(1 << BH_Uptodate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 /*
1780 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1781 * here, and the (potentially unmapped) buffers may become dirty at
1782 * any time. If a buffer becomes dirty here after we've inspected it
1783 * then we just miss that fact, and the page stays dirty.
1784 *
1785 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1786 * handle that here by just cleaning them.
1787 */
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 bh = head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001790 blocksize = bh->b_size;
1791 bbits = block_size_bits(blocksize);
1792
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001793 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001794 last_block = (i_size_read(inode) - 1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /*
1797 * Get all the dirty buffers mapped to disk addresses and
1798 * handle any aliases from the underlying blockdev's mapping.
1799 */
1800 do {
1801 if (block > last_block) {
1802 /*
1803 * mapped buffers outside i_size will occur, because
1804 * this page can be outside i_size when there is a
1805 * truncate in progress.
1806 */
1807 /*
1808 * The buffer was zeroed by block_write_full_page()
1809 */
1810 clear_buffer_dirty(bh);
1811 set_buffer_uptodate(bh);
Alex Tomas29a814d2008-07-11 19:27:31 -04001812 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1813 buffer_dirty(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001814 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 err = get_block(inode, block, bh, 1);
1816 if (err)
1817 goto recover;
Alex Tomas29a814d2008-07-11 19:27:31 -04001818 clear_buffer_delay(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (buffer_new(bh)) {
1820 /* blockdev mappings never come here */
1821 clear_buffer_new(bh);
Jan Karae64855c2016-11-04 18:08:15 +01001822 clean_bdev_bh_alias(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 }
1825 bh = bh->b_this_page;
1826 block++;
1827 } while (bh != head);
1828
1829 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (!buffer_mapped(bh))
1831 continue;
1832 /*
1833 * If it's a fully non-blocking write attempt and we cannot
1834 * lock the buffer then redirty the page. Note that this can
Jens Axboe5b0830c2009-09-23 19:37:09 +02001835 * potentially cause a busy-wait loop from writeback threads
1836 * and kswapd activity, but those code paths have their own
1837 * higher-level throttling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Wu Fengguang1b430be2010-10-26 14:21:26 -07001839 if (wbc->sync_mode != WB_SYNC_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 lock_buffer(bh);
Nick Pigginca5de402008-08-02 12:02:13 +02001841 } else if (!trylock_buffer(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 redirty_page_for_writepage(wbc, page);
1843 continue;
1844 }
1845 if (test_clear_buffer_dirty(bh)) {
Chris Mason35c80d52009-04-15 13:22:38 -04001846 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 } else {
1848 unlock_buffer(bh);
1849 }
1850 } while ((bh = bh->b_this_page) != head);
1851
1852 /*
1853 * The page and its buffers are protected by PageWriteback(), so we can
1854 * drop the bh refcounts early.
1855 */
1856 BUG_ON(PageWriteback(page));
1857 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 do {
1860 struct buffer_head *next = bh->b_this_page;
1861 if (buffer_async_write(bh)) {
Jens Axboe8e8f9292017-06-27 09:30:05 -06001862 submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
1863 inode->i_write_hint, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 nr_underway++;
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 bh = next;
1867 } while (bh != head);
Andrew Morton05937ba2005-05-05 16:15:47 -07001868 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 err = 0;
1871done:
1872 if (nr_underway == 0) {
1873 /*
1874 * The page was marked dirty, but the buffers were
1875 * clean. Someone wrote them back by hand with
1876 * ll_rw_block/submit_bh. A rare case.
1877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 end_page_writeback(page);
Nick Piggin3d67f2d2007-05-06 14:49:05 -07001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * The page and buffer_heads can be released at any time from
1882 * here on.
1883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 return err;
1886
1887recover:
1888 /*
1889 * ENOSPC, or some other error. We may already have added some
1890 * blocks to the file, so we need to write these out to avoid
1891 * exposing stale data.
1892 * The page is currently locked and not marked for writeback
1893 */
1894 bh = head;
1895 /* Recovery: lock and submit the mapped buffers */
1896 do {
Alex Tomas29a814d2008-07-11 19:27:31 -04001897 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1898 !buffer_delay(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 lock_buffer(bh);
Chris Mason35c80d52009-04-15 13:22:38 -04001900 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 } else {
1902 /*
1903 * The buffer may have been set dirty during
1904 * attachment to a dirty page.
1905 */
1906 clear_buffer_dirty(bh);
1907 }
1908 } while ((bh = bh->b_this_page) != head);
1909 SetPageError(page);
1910 BUG_ON(PageWriteback(page));
Andrew Morton7e4c3692007-05-08 00:23:27 -07001911 mapping_set_error(page->mapping, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 do {
1914 struct buffer_head *next = bh->b_this_page;
1915 if (buffer_async_write(bh)) {
1916 clear_buffer_dirty(bh);
Jens Axboe8e8f9292017-06-27 09:30:05 -06001917 submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
1918 inode->i_write_hint, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 nr_underway++;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 bh = next;
1922 } while (bh != head);
Nick Pigginffda9d32007-02-20 13:57:54 -08001923 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto done;
1925}
Benjamin Marzinskib4bba382016-06-27 09:58:40 -05001926EXPORT_SYMBOL(__block_write_full_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Nick Pigginafddba42007-10-16 01:25:01 -07001928/*
1929 * If a page has any new buffers, zero them out here, and mark them uptodate
1930 * and dirty so they'll be written out (in order to prevent uninitialised
1931 * block data from leaking). And clear the new bit.
1932 */
1933void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1934{
1935 unsigned int block_start, block_end;
1936 struct buffer_head *head, *bh;
1937
1938 BUG_ON(!PageLocked(page));
1939 if (!page_has_buffers(page))
1940 return;
1941
1942 bh = head = page_buffers(page);
1943 block_start = 0;
1944 do {
1945 block_end = block_start + bh->b_size;
1946
1947 if (buffer_new(bh)) {
1948 if (block_end > from && block_start < to) {
1949 if (!PageUptodate(page)) {
1950 unsigned start, size;
1951
1952 start = max(from, block_start);
1953 size = min(to, block_end) - start;
1954
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001955 zero_user(page, start, size);
Nick Pigginafddba42007-10-16 01:25:01 -07001956 set_buffer_uptodate(bh);
1957 }
1958
1959 clear_buffer_new(bh);
1960 mark_buffer_dirty(bh);
1961 }
1962 }
1963
1964 block_start = block_end;
1965 bh = bh->b_this_page;
1966 } while (bh != head);
1967}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02001968EXPORT_SYMBOL_NS(page_zero_new_buffers, ANDROID_GKI_VFS_EXPORT_ONLY);
Nick Pigginafddba42007-10-16 01:25:01 -07001969
Christoph Hellwigae259a92016-06-21 09:23:11 +10001970static void
1971iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
Christoph Hellwig6d49cc82021-08-10 18:33:05 -07001972 const struct iomap *iomap)
Christoph Hellwigae259a92016-06-21 09:23:11 +10001973{
1974 loff_t offset = block << inode->i_blkbits;
1975
1976 bh->b_bdev = iomap->bdev;
1977
1978 /*
1979 * Block points to offset in file we need to map, iomap contains
1980 * the offset at which the map starts. If the map ends before the
1981 * current block, then do not map the buffer and let the caller
1982 * handle it.
1983 */
1984 BUG_ON(offset >= iomap->offset + iomap->length);
1985
1986 switch (iomap->type) {
1987 case IOMAP_HOLE:
1988 /*
1989 * If the buffer is not up to date or beyond the current EOF,
1990 * we need to mark it as new to ensure sub-block zeroing is
1991 * executed if necessary.
1992 */
1993 if (!buffer_uptodate(bh) ||
1994 (offset >= i_size_read(inode)))
1995 set_buffer_new(bh);
1996 break;
1997 case IOMAP_DELALLOC:
1998 if (!buffer_uptodate(bh) ||
1999 (offset >= i_size_read(inode)))
2000 set_buffer_new(bh);
2001 set_buffer_uptodate(bh);
2002 set_buffer_mapped(bh);
2003 set_buffer_delay(bh);
2004 break;
2005 case IOMAP_UNWRITTEN:
2006 /*
Andreas Gruenbacher3d7b6b212018-06-19 15:10:55 -07002007 * For unwritten regions, we always need to ensure that regions
2008 * in the block we are not writing to are zeroed. Mark the
2009 * buffer as new to ensure this.
Christoph Hellwigae259a92016-06-21 09:23:11 +10002010 */
2011 set_buffer_new(bh);
2012 set_buffer_unwritten(bh);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002013 fallthrough;
Christoph Hellwigae259a92016-06-21 09:23:11 +10002014 case IOMAP_MAPPED:
Andreas Gruenbacher3d7b6b212018-06-19 15:10:55 -07002015 if ((iomap->flags & IOMAP_F_NEW) ||
2016 offset >= i_size_read(inode))
Christoph Hellwigae259a92016-06-21 09:23:11 +10002017 set_buffer_new(bh);
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -04002018 bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
2019 inode->i_blkbits;
Christoph Hellwigae259a92016-06-21 09:23:11 +10002020 set_buffer_mapped(bh);
2021 break;
2022 }
2023}
2024
2025int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
Christoph Hellwig6d49cc82021-08-10 18:33:05 -07002026 get_block_t *get_block, const struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 unsigned from = pos & (PAGE_SIZE - 1);
Christoph Hellwigebdec242010-10-06 10:47:23 +02002029 unsigned to = from + len;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002030 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 unsigned block_start, block_end;
2032 sector_t block;
2033 int err = 0;
2034 unsigned blocksize, bbits;
2035 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
2036
2037 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002038 BUG_ON(from > PAGE_SIZE);
2039 BUG_ON(to > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 BUG_ON(from > to);
2041
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002042 head = create_page_buffers(page, inode, 0);
2043 blocksize = head->b_size;
2044 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002046 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 for(bh = head, block_start = 0; bh != head || !block_start;
2049 block++, block_start=block_end, bh = bh->b_this_page) {
2050 block_end = block_start + blocksize;
2051 if (block_end <= from || block_start >= to) {
2052 if (PageUptodate(page)) {
2053 if (!buffer_uptodate(bh))
2054 set_buffer_uptodate(bh);
2055 }
2056 continue;
2057 }
2058 if (buffer_new(bh))
2059 clear_buffer_new(bh);
2060 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002061 WARN_ON(bh->b_size != blocksize);
Christoph Hellwigae259a92016-06-21 09:23:11 +10002062 if (get_block) {
2063 err = get_block(inode, block, bh, 1);
2064 if (err)
2065 break;
2066 } else {
2067 iomap_to_bh(inode, block, bh, iomap);
2068 }
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (buffer_new(bh)) {
Jan Karae64855c2016-11-04 18:08:15 +01002071 clean_bdev_bh_alias(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (PageUptodate(page)) {
Nick Piggin637aff42007-10-16 01:25:00 -07002073 clear_buffer_new(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 set_buffer_uptodate(bh);
Nick Piggin637aff42007-10-16 01:25:00 -07002075 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 continue;
2077 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002078 if (block_end > to || block_start < from)
2079 zero_user_segments(page,
2080 to, block_end,
2081 block_start, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 continue;
2083 }
2084 }
2085 if (PageUptodate(page)) {
2086 if (!buffer_uptodate(bh))
2087 set_buffer_uptodate(bh);
2088 continue;
2089 }
2090 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
David Chinner33a266d2007-02-12 00:51:41 -08002091 !buffer_unwritten(bh) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 (block_start < from || block_end > to)) {
Mike Christiedfec8a12016-06-05 14:31:44 -05002093 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 *wait_bh++=bh;
2095 }
2096 }
2097 /*
2098 * If we issued read requests - let them complete.
2099 */
2100 while(wait_bh > wait) {
2101 wait_on_buffer(*--wait_bh);
2102 if (!buffer_uptodate(*wait_bh))
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07002103 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Jan Karaf9f07b62011-06-14 00:58:27 +02002105 if (unlikely(err))
Nick Pigginafddba42007-10-16 01:25:01 -07002106 page_zero_new_buffers(page, from, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return err;
2108}
Christoph Hellwigae259a92016-06-21 09:23:11 +10002109
2110int __block_write_begin(struct page *page, loff_t pos, unsigned len,
2111 get_block_t *get_block)
2112{
2113 return __block_write_begin_int(page, pos, len, get_block, NULL);
2114}
Christoph Hellwigebdec242010-10-06 10:47:23 +02002115EXPORT_SYMBOL(__block_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117static int __block_commit_write(struct inode *inode, struct page *page,
2118 unsigned from, unsigned to)
2119{
2120 unsigned block_start, block_end;
2121 int partial = 0;
2122 unsigned blocksize;
2123 struct buffer_head *bh, *head;
2124
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002125 bh = head = page_buffers(page);
2126 blocksize = bh->b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002128 block_start = 0;
2129 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 block_end = block_start + blocksize;
2131 if (block_end <= from || block_start >= to) {
2132 if (!buffer_uptodate(bh))
2133 partial = 1;
2134 } else {
2135 set_buffer_uptodate(bh);
2136 mark_buffer_dirty(bh);
2137 }
Yang Guo4ebd3ae2021-02-24 12:02:48 -08002138 if (buffer_new(bh))
2139 clear_buffer_new(bh);
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002140
2141 block_start = block_end;
2142 bh = bh->b_this_page;
2143 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /*
2146 * If this is a partial write which happened to make all buffers
2147 * uptodate then we can optimize away a bogus readpage() for
2148 * the next read(). Here we 'discover' whether the page went
2149 * uptodate as a result of this (potentially partial) write.
2150 */
2151 if (!partial)
2152 SetPageUptodate(page);
2153 return 0;
2154}
2155
2156/*
Christoph Hellwig155130a2010-06-04 11:29:58 +02002157 * block_write_begin takes care of the basic task of block allocation and
2158 * bringing partial write blocks uptodate first.
2159 *
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002160 * The filesystem needs to handle block truncation upon failure.
Nick Pigginafddba42007-10-16 01:25:01 -07002161 */
Christoph Hellwig155130a2010-06-04 11:29:58 +02002162int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
2163 unsigned flags, struct page **pagep, get_block_t *get_block)
Nick Pigginafddba42007-10-16 01:25:01 -07002164{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002165 pgoff_t index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -07002166 struct page *page;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002167 int status;
Nick Pigginafddba42007-10-16 01:25:01 -07002168
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002169 page = grab_cache_page_write_begin(mapping, index, flags);
2170 if (!page)
2171 return -ENOMEM;
Nick Pigginafddba42007-10-16 01:25:01 -07002172
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002173 status = __block_write_begin(page, pos, len, get_block);
Nick Pigginafddba42007-10-16 01:25:01 -07002174 if (unlikely(status)) {
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002175 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002176 put_page(page);
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002177 page = NULL;
Nick Pigginafddba42007-10-16 01:25:01 -07002178 }
2179
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002180 *pagep = page;
Nick Pigginafddba42007-10-16 01:25:01 -07002181 return status;
2182}
2183EXPORT_SYMBOL(block_write_begin);
2184
2185int block_write_end(struct file *file, struct address_space *mapping,
2186 loff_t pos, unsigned len, unsigned copied,
2187 struct page *page, void *fsdata)
2188{
2189 struct inode *inode = mapping->host;
2190 unsigned start;
2191
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002192 start = pos & (PAGE_SIZE - 1);
Nick Pigginafddba42007-10-16 01:25:01 -07002193
2194 if (unlikely(copied < len)) {
2195 /*
2196 * The buffers that were written will now be uptodate, so we
2197 * don't have to worry about a readpage reading them and
2198 * overwriting a partial write. However if we have encountered
2199 * a short write and only partially written into a buffer, it
2200 * will not be marked uptodate, so a readpage might come in and
2201 * destroy our partial write.
2202 *
2203 * Do the simplest thing, and just treat any short write to a
2204 * non uptodate page as a zero-length write, and force the
2205 * caller to redo the whole thing.
2206 */
2207 if (!PageUptodate(page))
2208 copied = 0;
2209
2210 page_zero_new_buffers(page, start+copied, start+len);
2211 }
2212 flush_dcache_page(page);
2213
2214 /* This could be a short (even 0-length) commit */
2215 __block_commit_write(inode, page, start, start+copied);
2216
2217 return copied;
2218}
2219EXPORT_SYMBOL(block_write_end);
2220
2221int generic_write_end(struct file *file, struct address_space *mapping,
2222 loff_t pos, unsigned len, unsigned copied,
2223 struct page *page, void *fsdata)
2224{
Christoph Hellwig8af54f22019-06-27 17:28:40 -07002225 struct inode *inode = mapping->host;
2226 loff_t old_size = inode->i_size;
2227 bool i_size_changed = false;
2228
Nick Pigginafddba42007-10-16 01:25:01 -07002229 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
Christoph Hellwig8af54f22019-06-27 17:28:40 -07002230
2231 /*
2232 * No need to use i_size_read() here, the i_size cannot change under us
2233 * because we hold i_rwsem.
2234 *
2235 * But it's important to update i_size while still holding page lock:
2236 * page writeout could otherwise come in and zero beyond i_size.
2237 */
2238 if (pos + copied > inode->i_size) {
2239 i_size_write(inode, pos + copied);
2240 i_size_changed = true;
2241 }
2242
2243 unlock_page(page);
Andreas Gruenbacher7a77dad2019-04-30 08:45:34 -07002244 put_page(page);
Christoph Hellwig8af54f22019-06-27 17:28:40 -07002245
2246 if (old_size < pos)
2247 pagecache_isize_extended(inode, old_size, pos);
2248 /*
2249 * Don't mark the inode dirty under page lock. First, it unnecessarily
2250 * makes the holding time of page lock longer. Second, it forces lock
2251 * ordering of page lock and transaction start for journaling
2252 * filesystems.
2253 */
2254 if (i_size_changed)
2255 mark_inode_dirty(inode);
Andreas Gruenbacher26ddb1f2019-04-30 08:45:33 -07002256 return copied;
Nick Pigginafddba42007-10-16 01:25:01 -07002257}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02002258EXPORT_SYMBOL_NS(generic_write_end, ANDROID_GKI_VFS_EXPORT_ONLY);
Nick Pigginafddba42007-10-16 01:25:01 -07002259
2260/*
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002261 * block_is_partially_uptodate checks whether buffers within a page are
2262 * uptodate or not.
2263 *
2264 * Returns true if all buffers which correspond to a file portion
2265 * we want to read are uptodate.
2266 */
Al Viroc186afb42014-02-02 21:16:54 -05002267int block_is_partially_uptodate(struct page *page, unsigned long from,
2268 unsigned long count)
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002269{
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002270 unsigned block_start, block_end, blocksize;
2271 unsigned to;
2272 struct buffer_head *bh, *head;
2273 int ret = 1;
2274
2275 if (!page_has_buffers(page))
2276 return 0;
2277
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002278 head = page_buffers(page);
2279 blocksize = head->b_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002280 to = min_t(unsigned, PAGE_SIZE - from, count);
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002281 to = from + to;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002282 if (from < blocksize && to > PAGE_SIZE - blocksize)
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002283 return 0;
2284
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002285 bh = head;
2286 block_start = 0;
2287 do {
2288 block_end = block_start + blocksize;
2289 if (block_end > from && block_start < to) {
2290 if (!buffer_uptodate(bh)) {
2291 ret = 0;
2292 break;
2293 }
2294 if (block_end >= to)
2295 break;
2296 }
2297 block_start = block_end;
2298 bh = bh->b_this_page;
2299 } while (bh != head);
2300
2301 return ret;
2302}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02002303EXPORT_SYMBOL_NS(block_is_partially_uptodate, ANDROID_GKI_VFS_EXPORT_ONLY);
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002304
2305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 * Generic "read page" function for block devices that have the normal
2307 * get_block functionality. This is most of the block device filesystems.
2308 * Reads the page asynchronously --- the unlock_buffer() and
2309 * set/clear_buffer_uptodate() functions propagate buffer state into the
2310 * page struct once IO has completed.
2311 */
2312int block_read_full_page(struct page *page, get_block_t *get_block)
2313{
2314 struct inode *inode = page->mapping->host;
2315 sector_t iblock, lblock;
2316 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002317 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 int nr, i;
2319 int fully_mapped = 1;
Eric Biggers0957cd62022-12-23 12:36:37 -08002320 loff_t limit = i_size_read(inode);
2321
2322 /* This is needed for ext4. */
2323 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
2324 limit = inode->i_sb->s_maxbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002326 head = create_page_buffers(page, inode, 0);
2327 blocksize = head->b_size;
2328 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002330 iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
Eric Biggers0957cd62022-12-23 12:36:37 -08002331 lblock = (limit+blocksize-1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 bh = head;
2333 nr = 0;
2334 i = 0;
2335
2336 do {
2337 if (buffer_uptodate(bh))
2338 continue;
2339
2340 if (!buffer_mapped(bh)) {
Andrew Mortonc64610b2005-05-16 21:53:49 -07002341 int err = 0;
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 fully_mapped = 0;
2344 if (iblock < lblock) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002345 WARN_ON(bh->b_size != blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002346 err = get_block(inode, iblock, bh, 0);
2347 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 SetPageError(page);
2349 }
2350 if (!buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002351 zero_user(page, i * blocksize, blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002352 if (!err)
2353 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 continue;
2355 }
2356 /*
2357 * get_block() might have updated the buffer
2358 * synchronously
2359 */
2360 if (buffer_uptodate(bh))
2361 continue;
2362 }
2363 arr[nr++] = bh;
2364 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2365
2366 if (fully_mapped)
2367 SetPageMappedToDisk(page);
2368
2369 if (!nr) {
2370 /*
2371 * All buffers are uptodate - we can set the page uptodate
2372 * as well. But not if get_block() returned an error.
2373 */
2374 if (!PageError(page))
2375 SetPageUptodate(page);
2376 unlock_page(page);
2377 return 0;
2378 }
2379
2380 /* Stage two: lock the buffers */
2381 for (i = 0; i < nr; i++) {
2382 bh = arr[i];
2383 lock_buffer(bh);
2384 mark_buffer_async_read(bh);
2385 }
2386
2387 /*
2388 * Stage 3: start the IO. Check for uptodateness
2389 * inside the buffer lock in case another process reading
2390 * the underlying blockdev brought it uptodate (the sct fix).
2391 */
2392 for (i = 0; i < nr; i++) {
2393 bh = arr[i];
2394 if (buffer_uptodate(bh))
2395 end_buffer_async_read(bh, 1);
2396 else
Mike Christie2a222ca2016-06-05 14:31:43 -05002397 submit_bh(REQ_OP_READ, 0, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 return 0;
2400}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002401EXPORT_SYMBOL(block_read_full_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403/* utility function for filesystems that need to do work on expanding
Nick Piggin89e10782007-10-16 01:25:07 -07002404 * truncates. Uses filesystem pagecache writes to allow the filesystem to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 * deal with the hole.
2406 */
Nick Piggin89e10782007-10-16 01:25:07 -07002407int generic_cont_expand_simple(struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 struct address_space *mapping = inode->i_mapping;
2410 struct page *page;
Alexander Potapenko9357fca2022-09-15 17:04:16 +02002411 void *fsdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int err;
2413
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10002414 err = inode_newsize_ok(inode, size);
2415 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 goto out;
2417
Nick Piggin89e10782007-10-16 01:25:07 -07002418 err = pagecache_write_begin(NULL, mapping, size, 0,
Tetsuo Handac718a972017-05-08 15:58:59 -07002419 AOP_FLAG_CONT_EXPAND, &page, &fsdata);
Nick Piggin89e10782007-10-16 01:25:07 -07002420 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 goto out;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002422
Nick Piggin89e10782007-10-16 01:25:07 -07002423 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2424 BUG_ON(err > 0);
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426out:
2427 return err;
2428}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02002429EXPORT_SYMBOL_NS(generic_cont_expand_simple, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Adrian Bunkf1e3af72008-04-29 00:59:01 -07002431static int cont_expand_zero(struct file *file, struct address_space *mapping,
2432 loff_t pos, loff_t *bytes)
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002433{
Nick Piggin89e10782007-10-16 01:25:07 -07002434 struct inode *inode = mapping->host;
Fabian Frederick93407472017-02-27 14:28:32 -08002435 unsigned int blocksize = i_blocksize(inode);
Nick Piggin89e10782007-10-16 01:25:07 -07002436 struct page *page;
Alexander Potapenko9357fca2022-09-15 17:04:16 +02002437 void *fsdata = NULL;
Nick Piggin89e10782007-10-16 01:25:07 -07002438 pgoff_t index, curidx;
2439 loff_t curpos;
2440 unsigned zerofrom, offset, len;
2441 int err = 0;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002442
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002443 index = pos >> PAGE_SHIFT;
2444 offset = pos & ~PAGE_MASK;
Nick Piggin89e10782007-10-16 01:25:07 -07002445
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002446 while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
2447 zerofrom = curpos & ~PAGE_MASK;
Nick Piggin89e10782007-10-16 01:25:07 -07002448 if (zerofrom & (blocksize-1)) {
2449 *bytes |= (blocksize-1);
2450 (*bytes)++;
2451 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002452 len = PAGE_SIZE - zerofrom;
Nick Piggin89e10782007-10-16 01:25:07 -07002453
Tetsuo Handac718a972017-05-08 15:58:59 -07002454 err = pagecache_write_begin(file, mapping, curpos, len, 0,
2455 &page, &fsdata);
Nick Piggin89e10782007-10-16 01:25:07 -07002456 if (err)
2457 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002458 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002459 err = pagecache_write_end(file, mapping, curpos, len, len,
2460 page, fsdata);
2461 if (err < 0)
2462 goto out;
2463 BUG_ON(err != len);
2464 err = 0;
OGAWA Hirofumi061e9742008-04-28 02:16:28 -07002465
2466 balance_dirty_pages_ratelimited(mapping);
Mikulas Patockac2ca0fc2014-07-27 13:00:41 -04002467
Davidlohr Bueso08d405c2019-01-03 15:28:58 -08002468 if (fatal_signal_pending(current)) {
Mikulas Patockac2ca0fc2014-07-27 13:00:41 -04002469 err = -EINTR;
2470 goto out;
2471 }
Nick Piggin89e10782007-10-16 01:25:07 -07002472 }
2473
2474 /* page covers the boundary, find the boundary offset */
2475 if (index == curidx) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002476 zerofrom = curpos & ~PAGE_MASK;
Nick Piggin89e10782007-10-16 01:25:07 -07002477 /* if we will expand the thing last block will be filled */
2478 if (offset <= zerofrom) {
2479 goto out;
2480 }
2481 if (zerofrom & (blocksize-1)) {
2482 *bytes |= (blocksize-1);
2483 (*bytes)++;
2484 }
2485 len = offset - zerofrom;
2486
Tetsuo Handac718a972017-05-08 15:58:59 -07002487 err = pagecache_write_begin(file, mapping, curpos, len, 0,
2488 &page, &fsdata);
Nick Piggin89e10782007-10-16 01:25:07 -07002489 if (err)
2490 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002491 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002492 err = pagecache_write_end(file, mapping, curpos, len, len,
2493 page, fsdata);
2494 if (err < 0)
2495 goto out;
2496 BUG_ON(err != len);
2497 err = 0;
2498 }
2499out:
2500 return err;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002501}
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503/*
2504 * For moronic filesystems that do not allow holes in file.
2505 * We may have to extend the file.
2506 */
Christoph Hellwig282dc172010-06-04 11:29:55 +02002507int cont_write_begin(struct file *file, struct address_space *mapping,
Nick Piggin89e10782007-10-16 01:25:07 -07002508 loff_t pos, unsigned len, unsigned flags,
2509 struct page **pagep, void **fsdata,
2510 get_block_t *get_block, loff_t *bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct inode *inode = mapping->host;
Fabian Frederick93407472017-02-27 14:28:32 -08002513 unsigned int blocksize = i_blocksize(inode);
2514 unsigned int zerofrom;
Nick Piggin89e10782007-10-16 01:25:07 -07002515 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Nick Piggin89e10782007-10-16 01:25:07 -07002517 err = cont_expand_zero(file, mapping, pos, bytes);
2518 if (err)
Christoph Hellwig155130a2010-06-04 11:29:58 +02002519 return err;
Nick Piggin89e10782007-10-16 01:25:07 -07002520
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002521 zerofrom = *bytes & ~PAGE_MASK;
Nick Piggin89e10782007-10-16 01:25:07 -07002522 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2523 *bytes |= (blocksize-1);
2524 (*bytes)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526
Christoph Hellwig155130a2010-06-04 11:29:58 +02002527 return block_write_begin(mapping, pos, len, flags, pagep, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002529EXPORT_SYMBOL(cont_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531int block_commit_write(struct page *page, unsigned from, unsigned to)
2532{
2533 struct inode *inode = page->mapping->host;
2534 __block_commit_write(inode,page,from,to);
2535 return 0;
2536}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002537EXPORT_SYMBOL(block_commit_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
David Chinner54171692007-07-19 17:39:55 +10002539/*
2540 * block_page_mkwrite() is not allowed to change the file size as it gets
2541 * called from a page fault handler when a page is first dirtied. Hence we must
2542 * be careful to check for EOF conditions here. We set the page up correctly
2543 * for a written page which means we get ENOSPC checking when writing into
2544 * holes and correct delalloc and unwritten extent mapping on filesystems that
2545 * support these features.
2546 *
2547 * We are not allowed to take the i_mutex here so we have to play games to
2548 * protect against truncate races as the page could now be beyond EOF. Because
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002549 * truncate writes the inode size before removing pages, once we have the
David Chinner54171692007-07-19 17:39:55 +10002550 * page lock we can determine safely if the page is beyond EOF. If it is not
2551 * beyond EOF, then the page is guaranteed safe against truncation until we
2552 * unlock the page.
Jan Karaea13a862011-05-24 00:23:35 +02002553 *
Jan Kara14da9202012-06-12 16:20:37 +02002554 * Direct callers of this function should protect against filesystem freezing
Ross Zwisler5c500022015-10-13 16:51:02 -06002555 * using sb_start_pagefault() - sb_end_pagefault() functions.
David Chinner54171692007-07-19 17:39:55 +10002556 */
Ross Zwisler5c500022015-10-13 16:51:02 -06002557int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara24da4fa2011-05-24 00:23:34 +02002558 get_block_t get_block)
David Chinner54171692007-07-19 17:39:55 +10002559{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002560 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002561 struct inode *inode = file_inode(vma->vm_file);
David Chinner54171692007-07-19 17:39:55 +10002562 unsigned long end;
2563 loff_t size;
Jan Kara24da4fa2011-05-24 00:23:34 +02002564 int ret;
David Chinner54171692007-07-19 17:39:55 +10002565
2566 lock_page(page);
2567 size = i_size_read(inode);
2568 if ((page->mapping != inode->i_mapping) ||
Nick Piggin18336332007-07-20 00:31:45 -07002569 (page_offset(page) > size)) {
Jan Kara24da4fa2011-05-24 00:23:34 +02002570 /* We overload EFAULT to mean page got truncated */
2571 ret = -EFAULT;
2572 goto out_unlock;
David Chinner54171692007-07-19 17:39:55 +10002573 }
2574
2575 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002576 if (((page->index + 1) << PAGE_SHIFT) > size)
2577 end = size & ~PAGE_MASK;
David Chinner54171692007-07-19 17:39:55 +10002578 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002579 end = PAGE_SIZE;
David Chinner54171692007-07-19 17:39:55 +10002580
Christoph Hellwigebdec242010-10-06 10:47:23 +02002581 ret = __block_write_begin(page, 0, end, get_block);
David Chinner54171692007-07-19 17:39:55 +10002582 if (!ret)
2583 ret = block_commit_write(page, 0, end);
2584
Jan Kara24da4fa2011-05-24 00:23:34 +02002585 if (unlikely(ret < 0))
2586 goto out_unlock;
Jan Karaea13a862011-05-24 00:23:35 +02002587 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002588 wait_for_stable_page(page);
Jan Kara24da4fa2011-05-24 00:23:34 +02002589 return 0;
2590out_unlock:
2591 unlock_page(page);
David Chinner54171692007-07-19 17:39:55 +10002592 return ret;
2593}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002594EXPORT_SYMBOL(block_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002597 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 * immediately, while under the page lock. So it needs a special end_io
2599 * handler which does not touch the bh after unlocking it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 */
2601static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2602{
Dmitry Monakhov68671f32007-10-16 01:24:47 -07002603 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002607 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2608 * the page (converting it to circular linked list and taking care of page
2609 * dirty races).
2610 */
2611static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2612{
2613 struct buffer_head *bh;
2614
2615 BUG_ON(!PageLocked(page));
2616
2617 spin_lock(&page->mapping->private_lock);
2618 bh = head;
2619 do {
2620 if (PageDirty(page))
2621 set_buffer_dirty(bh);
2622 if (!bh->b_this_page)
2623 bh->b_this_page = head;
2624 bh = bh->b_this_page;
2625 } while (bh != head);
Guoqing Jiang45dcfc22020-06-01 21:47:48 -07002626 attach_page_private(page, head);
Nick Piggin03158cd2007-10-16 01:25:25 -07002627 spin_unlock(&page->mapping->private_lock);
2628}
2629
2630/*
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002631 * On entry, the page is fully not uptodate.
2632 * On exit the page is fully uptodate in the areas outside (from,to)
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002633 * The filesystem needs to handle block truncation upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 */
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002635int nobh_write_begin(struct address_space *mapping,
Nick Piggin03158cd2007-10-16 01:25:25 -07002636 loff_t pos, unsigned len, unsigned flags,
2637 struct page **pagep, void **fsdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 get_block_t *get_block)
2639{
Nick Piggin03158cd2007-10-16 01:25:25 -07002640 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 const unsigned blkbits = inode->i_blkbits;
2642 const unsigned blocksize = 1 << blkbits;
Nick Piggina4b06722007-10-16 01:24:48 -07002643 struct buffer_head *head, *bh;
Nick Piggin03158cd2007-10-16 01:25:25 -07002644 struct page *page;
2645 pgoff_t index;
2646 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 unsigned block_in_page;
Nick Piggina4b06722007-10-16 01:24:48 -07002648 unsigned block_start, block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 sector_t block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 int nr_reads = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 int ret = 0;
2652 int is_mapped_to_disk = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002654 index = pos >> PAGE_SHIFT;
2655 from = pos & (PAGE_SIZE - 1);
Nick Piggin03158cd2007-10-16 01:25:25 -07002656 to = from + len;
2657
Nick Piggin54566b22009-01-04 12:00:53 -08002658 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin03158cd2007-10-16 01:25:25 -07002659 if (!page)
2660 return -ENOMEM;
2661 *pagep = page;
2662 *fsdata = NULL;
2663
2664 if (page_has_buffers(page)) {
Namhyung Kim309f77a2010-10-25 15:01:12 +09002665 ret = __block_write_begin(page, pos, len, get_block);
2666 if (unlikely(ret))
2667 goto out_release;
2668 return ret;
Nick Piggin03158cd2007-10-16 01:25:25 -07002669 }
Nick Piggina4b06722007-10-16 01:24:48 -07002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (PageMappedToDisk(page))
2672 return 0;
2673
Nick Piggina4b06722007-10-16 01:24:48 -07002674 /*
2675 * Allocate buffers so that we can keep track of state, and potentially
2676 * attach them to the page if an error occurs. In the common case of
2677 * no error, they will just be freed again without ever being attached
2678 * to the page (which is all OK, because we're under the page lock).
2679 *
2680 * Be careful: the buffer linked list is a NULL terminated one, rather
2681 * than the circular one we're used to.
2682 */
Jens Axboe640ab982017-09-27 05:40:16 -06002683 head = alloc_page_buffers(page, blocksize, false);
Nick Piggin03158cd2007-10-16 01:25:25 -07002684 if (!head) {
2685 ret = -ENOMEM;
2686 goto out_release;
2687 }
Nick Piggina4b06722007-10-16 01:24:48 -07002688
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002689 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 /*
2692 * We loop across all blocks in the page, whether or not they are
2693 * part of the affected region. This is so we can discover if the
2694 * page is fully mapped-to-disk.
2695 */
Nick Piggina4b06722007-10-16 01:24:48 -07002696 for (block_start = 0, block_in_page = 0, bh = head;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002697 block_start < PAGE_SIZE;
Nick Piggina4b06722007-10-16 01:24:48 -07002698 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 int create;
2700
Nick Piggina4b06722007-10-16 01:24:48 -07002701 block_end = block_start + blocksize;
2702 bh->b_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 create = 1;
2704 if (block_start >= to)
2705 create = 0;
2706 ret = get_block(inode, block_in_file + block_in_page,
Nick Piggina4b06722007-10-16 01:24:48 -07002707 bh, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (ret)
2709 goto failed;
Nick Piggina4b06722007-10-16 01:24:48 -07002710 if (!buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 is_mapped_to_disk = 0;
Nick Piggina4b06722007-10-16 01:24:48 -07002712 if (buffer_new(bh))
Jan Karae64855c2016-11-04 18:08:15 +01002713 clean_bdev_bh_alias(bh);
Nick Piggina4b06722007-10-16 01:24:48 -07002714 if (PageUptodate(page)) {
2715 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 continue;
Nick Piggina4b06722007-10-16 01:24:48 -07002717 }
2718 if (buffer_new(bh) || !buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002719 zero_user_segments(page, block_start, from,
2720 to, block_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 continue;
2722 }
Nick Piggina4b06722007-10-16 01:24:48 -07002723 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 continue; /* reiserfs does this */
2725 if (block_start < from || block_end > to) {
Nick Piggina4b06722007-10-16 01:24:48 -07002726 lock_buffer(bh);
2727 bh->b_end_io = end_buffer_read_nobh;
Mike Christie2a222ca2016-06-05 14:31:43 -05002728 submit_bh(REQ_OP_READ, 0, bh);
Nick Piggina4b06722007-10-16 01:24:48 -07002729 nr_reads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731 }
2732
2733 if (nr_reads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * The page is locked, so these buffers are protected from
2736 * any VM or truncate activity. Hence we don't need to care
2737 * for the buffer_head refcounts.
2738 */
Nick Piggina4b06722007-10-16 01:24:48 -07002739 for (bh = head; bh; bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 wait_on_buffer(bh);
2741 if (!buffer_uptodate(bh))
2742 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 if (ret)
2745 goto failed;
2746 }
2747
2748 if (is_mapped_to_disk)
2749 SetPageMappedToDisk(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Nick Piggin03158cd2007-10-16 01:25:25 -07002751 *fsdata = head; /* to be released by nobh_write_end */
Nick Piggina4b06722007-10-16 01:24:48 -07002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
2754
2755failed:
Nick Piggin03158cd2007-10-16 01:25:25 -07002756 BUG_ON(!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /*
Nick Piggina4b06722007-10-16 01:24:48 -07002758 * Error recovery is a bit difficult. We need to zero out blocks that
2759 * were newly allocated, and dirty them to ensure they get written out.
2760 * Buffers need to be attached to the page at this point, otherwise
2761 * the handling of potential IO errors during writeout would be hard
2762 * (could try doing synchronous writeout, but what if that fails too?)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
Nick Piggin03158cd2007-10-16 01:25:25 -07002764 attach_nobh_buffers(page, head);
2765 page_zero_new_buffers(page, from, to);
Nick Piggina4b06722007-10-16 01:24:48 -07002766
Nick Piggin03158cd2007-10-16 01:25:25 -07002767out_release:
2768 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002769 put_page(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002770 *pagep = NULL;
Nick Piggina4b06722007-10-16 01:24:48 -07002771
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002772 return ret;
2773}
Nick Piggin03158cd2007-10-16 01:25:25 -07002774EXPORT_SYMBOL(nobh_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Nick Piggin03158cd2007-10-16 01:25:25 -07002776int nobh_write_end(struct file *file, struct address_space *mapping,
2777 loff_t pos, unsigned len, unsigned copied,
2778 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 struct inode *inode = page->mapping->host;
Nick Pigginefdc3132007-10-21 06:57:41 +02002781 struct buffer_head *head = fsdata;
Nick Piggin03158cd2007-10-16 01:25:25 -07002782 struct buffer_head *bh;
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002783 BUG_ON(fsdata != NULL && page_has_buffers(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Dave Kleikampd4cf1092009-02-06 14:59:26 -06002785 if (unlikely(copied < len) && head)
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002786 attach_nobh_buffers(page, head);
2787 if (page_has_buffers(page))
2788 return generic_write_end(file, mapping, pos, len,
2789 copied, page, fsdata);
Nick Piggina4b06722007-10-16 01:24:48 -07002790
Nick Piggin22c8ca72007-02-20 13:58:09 -08002791 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 set_page_dirty(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002793 if (pos+copied > inode->i_size) {
2794 i_size_write(inode, pos+copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 mark_inode_dirty(inode);
2796 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002797
2798 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002799 put_page(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002800
Nick Piggin03158cd2007-10-16 01:25:25 -07002801 while (head) {
2802 bh = head;
2803 head = head->b_this_page;
2804 free_buffer_head(bh);
2805 }
2806
2807 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
Nick Piggin03158cd2007-10-16 01:25:25 -07002809EXPORT_SYMBOL(nobh_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811/*
2812 * nobh_writepage() - based on block_full_write_page() except
2813 * that it tries to operate without attaching bufferheads to
2814 * the page.
2815 */
2816int nobh_writepage(struct page *page, get_block_t *get_block,
2817 struct writeback_control *wbc)
2818{
2819 struct inode * const inode = page->mapping->host;
2820 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002821 const pgoff_t end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 int ret;
2824
2825 /* Is the page fully inside i_size? */
2826 if (page->index < end_index)
2827 goto out;
2828
2829 /* Is the page fully outside i_size? (truncate in progress) */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002830 offset = i_size & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (page->index >= end_index+1 || !offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 unlock_page(page);
2833 return 0; /* don't care */
2834 }
2835
2836 /*
2837 * The page straddles i_size. It must be zeroed out on each and every
2838 * writepage invocation because it may be mmapped. "A file is mapped
2839 * in multiples of the page size. For a file that is not a multiple of
2840 * the page size, the remaining memory is zeroed when mapped, and
2841 * writes to that region are not written out to the file."
2842 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002843 zero_user_segment(page, offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844out:
2845 ret = mpage_writepage(page, get_block, wbc);
2846 if (ret == -EAGAIN)
Chris Mason35c80d52009-04-15 13:22:38 -04002847 ret = __block_write_full_page(inode, page, get_block, wbc,
2848 end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return ret;
2850}
2851EXPORT_SYMBOL(nobh_writepage);
2852
Nick Piggin03158cd2007-10-16 01:25:25 -07002853int nobh_truncate_page(struct address_space *mapping,
2854 loff_t from, get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002856 pgoff_t index = from >> PAGE_SHIFT;
2857 unsigned offset = from & (PAGE_SIZE-1);
Nick Piggin03158cd2007-10-16 01:25:25 -07002858 unsigned blocksize;
2859 sector_t iblock;
2860 unsigned length, pos;
2861 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct page *page;
Nick Piggin03158cd2007-10-16 01:25:25 -07002863 struct buffer_head map_bh;
2864 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Fabian Frederick93407472017-02-27 14:28:32 -08002866 blocksize = i_blocksize(inode);
Nick Piggin03158cd2007-10-16 01:25:25 -07002867 length = offset & (blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Nick Piggin03158cd2007-10-16 01:25:25 -07002869 /* Block boundary? Nothing to do */
2870 if (!length)
2871 return 0;
2872
2873 length = blocksize - length;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002874 iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
Nick Piggin03158cd2007-10-16 01:25:25 -07002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 page = grab_cache_page(mapping, index);
Nick Piggin03158cd2007-10-16 01:25:25 -07002877 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (!page)
2879 goto out;
2880
Nick Piggin03158cd2007-10-16 01:25:25 -07002881 if (page_has_buffers(page)) {
2882has_buffers:
2883 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002884 put_page(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002885 return block_truncate_page(mapping, from, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002887
2888 /* Find the buffer that contains "offset" */
2889 pos = blocksize;
2890 while (offset >= pos) {
2891 iblock++;
2892 pos += blocksize;
2893 }
2894
Theodore Ts'o460bcf52009-05-12 07:37:56 -04002895 map_bh.b_size = blocksize;
2896 map_bh.b_state = 0;
Nick Piggin03158cd2007-10-16 01:25:25 -07002897 err = get_block(inode, iblock, &map_bh, 0);
2898 if (err)
2899 goto unlock;
2900 /* unmapped? It's a hole - nothing to do */
2901 if (!buffer_mapped(&map_bh))
2902 goto unlock;
2903
2904 /* Ok, it's mapped. Make sure it's up-to-date */
2905 if (!PageUptodate(page)) {
2906 err = mapping->a_ops->readpage(NULL, page);
2907 if (err) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002908 put_page(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002909 goto out;
2910 }
2911 lock_page(page);
2912 if (!PageUptodate(page)) {
2913 err = -EIO;
2914 goto unlock;
2915 }
2916 if (page_has_buffers(page))
2917 goto has_buffers;
2918 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002919 zero_user(page, offset, length);
Nick Piggin03158cd2007-10-16 01:25:25 -07002920 set_page_dirty(page);
2921 err = 0;
2922
2923unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002925 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926out:
Nick Piggin03158cd2007-10-16 01:25:25 -07002927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929EXPORT_SYMBOL(nobh_truncate_page);
2930
2931int block_truncate_page(struct address_space *mapping,
2932 loff_t from, get_block_t *get_block)
2933{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002934 pgoff_t index = from >> PAGE_SHIFT;
2935 unsigned offset = from & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 unsigned blocksize;
Andrew Morton54b21a72006-01-08 01:03:05 -08002937 sector_t iblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 unsigned length, pos;
2939 struct inode *inode = mapping->host;
2940 struct page *page;
2941 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 int err;
2943
Fabian Frederick93407472017-02-27 14:28:32 -08002944 blocksize = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 length = offset & (blocksize - 1);
2946
2947 /* Block boundary? Nothing to do */
2948 if (!length)
2949 return 0;
2950
2951 length = blocksize - length;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002952 iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 page = grab_cache_page(mapping, index);
2955 err = -ENOMEM;
2956 if (!page)
2957 goto out;
2958
2959 if (!page_has_buffers(page))
2960 create_empty_buffers(page, blocksize, 0);
2961
2962 /* Find the buffer that contains "offset" */
2963 bh = page_buffers(page);
2964 pos = blocksize;
2965 while (offset >= pos) {
2966 bh = bh->b_this_page;
2967 iblock++;
2968 pos += blocksize;
2969 }
2970
2971 err = 0;
2972 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002973 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 err = get_block(inode, iblock, bh, 0);
2975 if (err)
2976 goto unlock;
2977 /* unmapped? It's a hole - nothing to do */
2978 if (!buffer_mapped(bh))
2979 goto unlock;
2980 }
2981
2982 /* Ok, it's mapped. Make sure it's up-to-date */
2983 if (PageUptodate(page))
2984 set_buffer_uptodate(bh);
2985
David Chinner33a266d2007-02-12 00:51:41 -08002986 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -05002988 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 wait_on_buffer(bh);
2990 /* Uhhuh. Read error. Complain and punt. */
2991 if (!buffer_uptodate(bh))
2992 goto unlock;
2993 }
2994
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002995 zero_user(page, offset, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 mark_buffer_dirty(bh);
2997 err = 0;
2998
2999unlock:
3000 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003001 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002out:
3003 return err;
3004}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003005EXPORT_SYMBOL(block_truncate_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007/*
3008 * The generic ->writepage function for buffer-backed address_spaces
3009 */
Matthew Wilcox1b938c02014-06-04 16:07:43 -07003010int block_write_full_page(struct page *page, get_block_t *get_block,
3011 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 struct inode * const inode = page->mapping->host;
3014 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003015 const pgoff_t end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 /* Is the page fully inside i_size? */
3019 if (page->index < end_index)
Chris Mason35c80d52009-04-15 13:22:38 -04003020 return __block_write_full_page(inode, page, get_block, wbc,
Matthew Wilcox1b938c02014-06-04 16:07:43 -07003021 end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /* Is the page fully outside i_size? (truncate in progress) */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003024 offset = i_size & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (page->index >= end_index+1 || !offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 unlock_page(page);
3027 return 0; /* don't care */
3028 }
3029
3030 /*
3031 * The page straddles i_size. It must be zeroed out on each and every
Adam Buchbinder2a61aa42009-12-11 16:35:40 -05003032 * writepage invocation because it may be mmapped. "A file is mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 * in multiples of the page size. For a file that is not a multiple of
3034 * the page size, the remaining memory is zeroed when mapped, and
3035 * writes to that region are not written out to the file."
3036 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003037 zero_user_segment(page, offset, PAGE_SIZE);
Matthew Wilcox1b938c02014-06-04 16:07:43 -07003038 return __block_write_full_page(inode, page, get_block, wbc,
3039 end_buffer_async_write);
Chris Mason35c80d52009-04-15 13:22:38 -04003040}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003041EXPORT_SYMBOL(block_write_full_page);
Chris Mason35c80d52009-04-15 13:22:38 -04003042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
3044 get_block_t *get_block)
3045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 struct inode *inode = mapping->host;
Alexander Potapenko2a527d62017-07-05 00:56:21 -04003047 struct buffer_head tmp = {
3048 .b_size = i_blocksize(inode),
3049 };
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 get_block(inode, block, &tmp, 0);
3052 return tmp.b_blocknr;
3053}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02003054EXPORT_SYMBOL_NS(generic_block_bmap, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003056static void end_bio_bh_io_sync(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct buffer_head *bh = bio->bi_private;
3059
Jens Axboeb7c44ed2015-07-24 12:37:59 -06003060 if (unlikely(bio_flagged(bio, BIO_QUIET)))
Keith Mannthey08bafc02008-11-25 10:24:35 +01003061 set_bit(BH_Quiet, &bh->b_state);
3062
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003063 bh->b_end_io(bh, !bio->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
Mike Christie2a222ca2016-06-05 14:31:43 -05003067static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
Jens Axboe8e8f9292017-06-27 09:30:05 -06003068 enum rw_hint write_hint, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 BUG_ON(!buffer_locked(bh));
3073 BUG_ON(!buffer_mapped(bh));
3074 BUG_ON(!bh->b_end_io);
Aneesh Kumar K.V8fb0e342009-05-12 16:22:37 -04003075 BUG_ON(buffer_delay(bh));
3076 BUG_ON(buffer_unwritten(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Jens Axboe48fd4f92008-08-22 10:00:36 +02003078 /*
Jens Axboe48fd4f92008-08-22 10:00:36 +02003079 * Only clear out a write error when rewriting
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 */
Mike Christie2a222ca2016-06-05 14:31:43 -05003081 if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 clear_buffer_write_io_error(bh);
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 bio = bio_alloc(GFP_NOIO, 1);
3085
Eric Biggers4f74d152020-07-02 01:56:07 +00003086 fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
3087
Kent Overstreet4f024f32013-10-11 15:44:27 -07003088 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
Christoph Hellwig74d46992017-08-23 19:10:32 +02003089 bio_set_dev(bio, bh->b_bdev);
Jens Axboe8e8f9292017-06-27 09:30:05 -06003090 bio->bi_write_hint = write_hint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Kent Overstreet6cf66b42014-12-22 12:48:42 +01003092 bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
3093 BUG_ON(bio->bi_iter.bi_size != bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 bio->bi_end_io = end_bio_bh_io_sync;
3096 bio->bi_private = bh;
3097
Theodore Ts'o877f9622013-04-20 19:58:37 -04003098 if (buffer_meta(bh))
Mike Christie2a222ca2016-06-05 14:31:43 -05003099 op_flags |= REQ_META;
Theodore Ts'o877f9622013-04-20 19:58:37 -04003100 if (buffer_prio(bh))
Mike Christie2a222ca2016-06-05 14:31:43 -05003101 op_flags |= REQ_PRIO;
3102 bio_set_op_attrs(bio, op, op_flags);
Theodore Ts'o877f9622013-04-20 19:58:37 -04003103
Ming Lei83c9c542020-01-05 09:41:14 +08003104 /* Take care of bh's that straddle the end of the device */
3105 guard_bio_eod(bio);
3106
Dennis Zhoufd42df32018-12-05 12:10:34 -05003107 if (wbc) {
3108 wbc_init_bio(wbc, bio);
Tejun Heo34e51a52019-06-27 13:39:49 -07003109 wbc_account_cgroup_owner(wbc, bh->b_page, bh->b_size);
Dennis Zhoufd42df32018-12-05 12:10:34 -05003110 }
3111
Mike Christie4e49ea42016-06-05 14:31:41 -05003112 submit_bio(bio);
Julia Lawallf6454b02015-05-26 21:59:53 +02003113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
Tejun Heobafc0db2015-06-02 08:37:23 -06003115
Eric Biggers020c28332017-03-25 21:02:18 -07003116int submit_bh(int op, int op_flags, struct buffer_head *bh)
Tejun Heobafc0db2015-06-02 08:37:23 -06003117{
Jens Axboe8e8f9292017-06-27 09:30:05 -06003118 return submit_bh_wbc(op, op_flags, bh, 0, NULL);
Darrick J. Wong7136851112013-04-29 15:07:25 -07003119}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003120EXPORT_SYMBOL(submit_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122/**
3123 * ll_rw_block: low-level access to block devices (DEPRECATED)
Mike Christiedfec8a12016-06-05 14:31:44 -05003124 * @op: whether to %READ or %WRITE
Christoph Hellwigef295ec2016-10-28 08:48:16 -06003125 * @op_flags: req_flag_bits
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 * @nr: number of &struct buffer_heads in the array
3127 * @bhs: array of pointers to &struct buffer_head
3128 *
Jan Karaa7662232005-09-06 15:19:10 -07003129 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
Christoph Hellwig70246282016-07-19 11:28:41 +02003130 * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
3131 * @op_flags contains flags modifying the detailed I/O behavior, most notably
3132 * %REQ_RAHEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 *
3134 * This function drops any buffer that it cannot get a lock on (with the
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003135 * BH_Lock state bit), any buffer that appears to be clean when doing a write
3136 * request, and any buffer that appears to be up-to-date when doing read
3137 * request. Further it marks as clean buffers that are processed for
3138 * writing (the buffer cache won't assume that they are actually clean
3139 * until the buffer gets unlocked).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 *
3141 * ll_rw_block sets b_end_io to simple completion handler that marks
Masanari Iidae2278672014-02-18 22:54:36 +09003142 * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 * any waiters.
3144 *
3145 * All of the buffers must be for the same device, and must also be a
3146 * multiple of the current approved size for the device.
3147 */
Mike Christiedfec8a12016-06-05 14:31:44 -05003148void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 int i;
3151
3152 for (i = 0; i < nr; i++) {
3153 struct buffer_head *bh = bhs[i];
3154
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003155 if (!trylock_buffer(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 continue;
Mike Christiedfec8a12016-06-05 14:31:44 -05003157 if (op == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (test_clear_buffer_dirty(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003159 bh->b_end_io = end_buffer_write_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003160 get_bh(bh);
Mike Christiedfec8a12016-06-05 14:31:44 -05003161 submit_bh(op, op_flags, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 continue;
3163 }
3164 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (!buffer_uptodate(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003166 bh->b_end_io = end_buffer_read_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003167 get_bh(bh);
Mike Christiedfec8a12016-06-05 14:31:44 -05003168 submit_bh(op, op_flags, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 continue;
3170 }
3171 }
3172 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 }
3174}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02003175EXPORT_SYMBOL_NS(ll_rw_block, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Mike Christie2a222ca2016-06-05 14:31:43 -05003177void write_dirty_buffer(struct buffer_head *bh, int op_flags)
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003178{
3179 lock_buffer(bh);
3180 if (!test_clear_buffer_dirty(bh)) {
3181 unlock_buffer(bh);
3182 return;
3183 }
3184 bh->b_end_io = end_buffer_write_sync;
3185 get_bh(bh);
Mike Christie2a222ca2016-06-05 14:31:43 -05003186 submit_bh(REQ_OP_WRITE, op_flags, bh);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003187}
3188EXPORT_SYMBOL(write_dirty_buffer);
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/*
3191 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3192 * and then start new I/O and then wait upon it. The caller must have a ref on
3193 * the buffer_head.
3194 */
Mike Christie2a222ca2016-06-05 14:31:43 -05003195int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 int ret = 0;
3198
3199 WARN_ON(atomic_read(&bh->b_count) < 1);
3200 lock_buffer(bh);
3201 if (test_clear_buffer_dirty(bh)) {
Xianting Tian377254b2020-07-31 12:10:25 -04003202 /*
3203 * The bh should be mapped, but it might not be if the
3204 * device was hot-removed. Not much we can do but fail the I/O.
3205 */
3206 if (!buffer_mapped(bh)) {
3207 unlock_buffer(bh);
3208 return -EIO;
3209 }
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 get_bh(bh);
3212 bh->b_end_io = end_buffer_write_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05003213 ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 wait_on_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (!ret && !buffer_uptodate(bh))
3216 ret = -EIO;
3217 } else {
3218 unlock_buffer(bh);
3219 }
3220 return ret;
3221}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02003222EXPORT_SYMBOL_NS(__sync_dirty_buffer, ANDROID_GKI_VFS_EXPORT_ONLY);
Christoph Hellwig87e99512010-08-11 17:05:45 +02003223
3224int sync_dirty_buffer(struct buffer_head *bh)
3225{
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003226 return __sync_dirty_buffer(bh, REQ_SYNC);
Christoph Hellwig87e99512010-08-11 17:05:45 +02003227}
Greg Kroah-Hartman0a77fca2020-07-02 12:51:03 +02003228EXPORT_SYMBOL_NS(sync_dirty_buffer, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230/*
3231 * try_to_free_buffers() checks if all the buffers on this particular page
3232 * are unused, and releases them if so.
3233 *
3234 * Exclusion against try_to_free_buffers may be obtained by either
3235 * locking the page or by holding its mapping's private_lock.
3236 *
3237 * If the page is dirty but all the buffers are clean then we need to
3238 * be sure to mark the page clean as well. This is because the page
3239 * may be against a block device, and a later reattachment of buffers
3240 * to a dirty page will set *all* buffers dirty. Which would corrupt
3241 * filesystem data on the same device.
3242 *
3243 * The same applies to regular filesystem pages: if all the buffers are
3244 * clean then we set the page clean and proceed. To do that, we require
3245 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3246 * private_lock.
3247 *
3248 * try_to_free_buffers() is non-blocking.
3249 */
3250static inline int buffer_busy(struct buffer_head *bh)
3251{
3252 return atomic_read(&bh->b_count) |
3253 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3254}
3255
3256static int
3257drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3258{
3259 struct buffer_head *head = page_buffers(page);
3260 struct buffer_head *bh;
3261
3262 bh = head;
3263 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (buffer_busy(bh))
3265 goto failed;
3266 bh = bh->b_this_page;
3267 } while (bh != head);
3268
3269 do {
3270 struct buffer_head *next = bh->b_this_page;
3271
Jan Kara535ee2f2008-02-08 04:21:59 -08003272 if (bh->b_assoc_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 __remove_assoc_queue(bh);
3274 bh = next;
3275 } while (bh != head);
3276 *buffers_to_free = head;
Guoqing Jiang45dcfc22020-06-01 21:47:48 -07003277 detach_page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 1;
3279failed:
3280 return 0;
3281}
3282
3283int try_to_free_buffers(struct page *page)
3284{
3285 struct address_space * const mapping = page->mapping;
3286 struct buffer_head *buffers_to_free = NULL;
3287 int ret = 0;
3288
3289 BUG_ON(!PageLocked(page));
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003290 if (PageWriteback(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return 0;
3292
3293 if (mapping == NULL) { /* can this still happen? */
3294 ret = drop_buffers(page, &buffers_to_free);
3295 goto out;
3296 }
3297
3298 spin_lock(&mapping->private_lock);
3299 ret = drop_buffers(page, &buffers_to_free);
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003300
3301 /*
3302 * If the filesystem writes its buffers by hand (eg ext3)
3303 * then we can have clean buffers against a dirty page. We
3304 * clean the page here; otherwise the VM will never notice
3305 * that the filesystem did any IO at all.
3306 *
3307 * Also, during truncate, discard_buffer will have marked all
3308 * the page's buffers clean. We discover that here and clean
3309 * the page also.
Nick Piggin87df7242007-01-30 14:36:27 +11003310 *
3311 * private_lock must be held over this entire operation in order
3312 * to synchronise against __set_page_dirty_buffers and prevent the
3313 * dirty bit from being lost.
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003314 */
Tejun Heo11f81be2015-05-22 17:13:15 -04003315 if (ret)
3316 cancel_dirty_page(page);
Nick Piggin87df7242007-01-30 14:36:27 +11003317 spin_unlock(&mapping->private_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318out:
3319 if (buffers_to_free) {
3320 struct buffer_head *bh = buffers_to_free;
3321
3322 do {
3323 struct buffer_head *next = bh->b_this_page;
3324 free_buffer_head(bh);
3325 bh = next;
3326 } while (bh != buffers_to_free);
3327 }
3328 return ret;
3329}
3330EXPORT_SYMBOL(try_to_free_buffers);
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 * Buffer-head allocation
3334 */
Shai Fultheima0a9b042012-05-15 12:29:52 +03003335static struct kmem_cache *bh_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337/*
3338 * Once the number of bh's in the machine exceeds this level, we start
3339 * stripping them in writeback.
3340 */
Zhang Yanfei43be5942013-02-22 16:35:46 -08003341static unsigned long max_buffer_heads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343int buffer_heads_over_limit;
3344
3345struct bh_accounting {
3346 int nr; /* Number of live bh's */
3347 int ratelimit; /* Limit cacheline bouncing */
3348};
3349
3350static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3351
3352static void recalc_bh_state(void)
3353{
3354 int i;
3355 int tot = 0;
3356
Christoph Lameteree1be862010-12-06 11:40:05 -06003357 if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return;
Christoph Lameterc7b92512010-12-06 11:16:28 -06003359 __this_cpu_write(bh_accounting.ratelimit, 0);
Eric Dumazet8a143422006-03-24 03:18:10 -08003360 for_each_online_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 tot += per_cpu(bh_accounting, i).nr;
3362 buffer_heads_over_limit = (tot > max_buffer_heads);
3363}
Christoph Lameterc7b92512010-12-06 11:16:28 -06003364
Al Virodd0fc662005-10-07 07:46:04 +01003365struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Richard Kennedy019b4d12010-03-10 15:20:33 -08003367 struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (ret) {
Christoph Lametera35afb82007-05-16 22:10:57 -07003369 INIT_LIST_HEAD(&ret->b_assoc_buffers);
Thomas Gleixnerf1e67e32019-11-18 14:28:24 +01003370 spin_lock_init(&ret->b_uptodate_lock);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003371 preempt_disable();
3372 __this_cpu_inc(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003374 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 return ret;
3377}
3378EXPORT_SYMBOL(alloc_buffer_head);
3379
3380void free_buffer_head(struct buffer_head *bh)
3381{
3382 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3383 kmem_cache_free(bh_cachep, bh);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003384 preempt_disable();
3385 __this_cpu_dec(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003387 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389EXPORT_SYMBOL(free_buffer_head);
3390
Sebastian Andrzej Siewiorfc4d24c2016-11-03 15:49:57 +01003391static int buffer_exit_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 int i;
3394 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3395
3396 for (i = 0; i < BH_LRU_SIZE; i++) {
3397 brelse(b->bhs[i]);
3398 b->bhs[i] = NULL;
3399 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06003400 this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
Eric Dumazet8a143422006-03-24 03:18:10 -08003401 per_cpu(bh_accounting, cpu).nr = 0;
Sebastian Andrzej Siewiorfc4d24c2016-11-03 15:49:57 +01003402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003405/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003406 * bh_uptodate_or_lock - Test whether the buffer is uptodate
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003407 * @bh: struct buffer_head
3408 *
3409 * Return true if the buffer is up-to-date and false,
3410 * with the buffer locked, if not.
3411 */
3412int bh_uptodate_or_lock(struct buffer_head *bh)
3413{
3414 if (!buffer_uptodate(bh)) {
3415 lock_buffer(bh);
3416 if (!buffer_uptodate(bh))
3417 return 0;
3418 unlock_buffer(bh);
3419 }
3420 return 1;
3421}
3422EXPORT_SYMBOL(bh_uptodate_or_lock);
3423
3424/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003425 * bh_submit_read - Submit a locked buffer for reading
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003426 * @bh: struct buffer_head
3427 *
3428 * Returns zero on success and -EIO on error.
3429 */
3430int bh_submit_read(struct buffer_head *bh)
3431{
3432 BUG_ON(!buffer_locked(bh));
3433
3434 if (buffer_uptodate(bh)) {
3435 unlock_buffer(bh);
3436 return 0;
3437 }
3438
3439 get_bh(bh);
3440 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05003441 submit_bh(REQ_OP_READ, 0, bh);
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003442 wait_on_buffer(bh);
3443 if (buffer_uptodate(bh))
3444 return 0;
3445 return -EIO;
3446}
3447EXPORT_SYMBOL(bh_submit_read);
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449void __init buffer_init(void)
3450{
Zhang Yanfei43be5942013-02-22 16:35:46 -08003451 unsigned long nrpages;
Sebastian Andrzej Siewiorfc4d24c2016-11-03 15:49:57 +01003452 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Christoph Lameterb98938c2008-02-04 22:28:36 -08003454 bh_cachep = kmem_cache_create("buffer_head",
3455 sizeof(struct buffer_head), 0,
3456 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3457 SLAB_MEM_SPREAD),
Richard Kennedy019b4d12010-03-10 15:20:33 -08003458 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 /*
3461 * Limit the bh occupancy to 10% of ZONE_NORMAL
3462 */
3463 nrpages = (nr_free_buffer_pages() * 10) / 100;
3464 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
Sebastian Andrzej Siewiorfc4d24c2016-11-03 15:49:57 +01003465 ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
3466 NULL, buffer_exit_cpu_dead);
3467 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}