457c899653991 (Thomas Gleixner 2019-05-19 13:08:55 +0100 1) // SPDX-License-Identifier: GPL-2.0-only
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * linux/fs/buffer.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * Copyright (C) 1991, 1992, 2002 Linus Torvalds
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) * Removed a lot of unnecessary code and simplified things now that
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) * Speed up hash, lru, and free list operations. Use gfp() for allocating
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 15) * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 17) * Added 32k buffer block sizes - these are required older ARM systems. - RMK
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 22) #include <linux/kernel.h>
f361bf4a66c9b (Ingo Molnar 2017-02-03 23:47:37 +0100 23) #include <linux/sched/signal.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24) #include <linux/syscalls.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25) #include <linux/fs.h>
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 26) #include <linux/iomap.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27) #include <linux/mm.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28) #include <linux/percpu.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 29) #include <linux/slab.h>
16f7e0fe2ecc3 (Randy Dunlap 2006-01-11 12:17:46 -0800 30) #include <linux/capability.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 31) #include <linux/blkdev.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 32) #include <linux/file.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33) #include <linux/quotaops.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 34) #include <linux/highmem.h>
630d9c47274aa (Paul Gortmaker 2011-11-16 23:57:37 -0500 35) #include <linux/export.h>
bafc0dba1e20d (Tejun Heo 2015-06-02 08:37:23 -0600 36) #include <linux/backing-dev.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37) #include <linux/writeback.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 38) #include <linux/hash.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 39) #include <linux/suspend.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 40) #include <linux/buffer_head.h>
55e829af06681 (Andrew Morton 2006-12-10 02:19:27 -0800 41) #include <linux/task_io_accounting_ops.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 42) #include <linux/bio.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 43) #include <linux/cpu.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 44) #include <linux/bitops.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 45) #include <linux/mpage.h>
fb1c8f93d869b (Ingo Molnar 2005-09-10 00:25:56 -0700 46) #include <linux/bit_spinlock.h>
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 47) #include <linux/pagevec.h>
f745c6f5fe757 (Shakeel Butt 2018-08-17 15:46:44 -0700 48) #include <linux/sched/mm.h>
5305cb8308345 (Tejun Heo 2013-01-11 13:06:36 -0800 49) #include <trace/events/block.h>
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 50) #include <linux/fscrypt.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 51)
2b211dc04cb7f (Ben Dooks 2019-11-30 17:49:18 -0800 52) #include "internal.h"
2b211dc04cb7f (Ben Dooks 2019-11-30 17:49:18 -0800 53)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54) static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 55) static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 56) enum rw_hint hint, struct writeback_control *wbc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58) #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 59)
f0059afd3e6e7 (Tejun Heo 2013-01-11 13:06:35 -0800 60) inline void touch_buffer(struct buffer_head *bh)
f0059afd3e6e7 (Tejun Heo 2013-01-11 13:06:35 -0800 61) {
5305cb8308345 (Tejun Heo 2013-01-11 13:06:36 -0800 62) trace_block_touch_buffer(bh);
f0059afd3e6e7 (Tejun Heo 2013-01-11 13:06:35 -0800 63) mark_page_accessed(bh->b_page);
f0059afd3e6e7 (Tejun Heo 2013-01-11 13:06:35 -0800 64) }
f0059afd3e6e7 (Tejun Heo 2013-01-11 13:06:35 -0800 65) EXPORT_SYMBOL(touch_buffer);
f0059afd3e6e7 (Tejun Heo 2013-01-11 13:06:35 -0800 66)
fc9b52cd8f5f4 (Harvey Harrison 2008-02-08 04:19:52 -0800 67) void __lock_buffer(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) {
743162013d40c (NeilBrown 2014-07-07 15:16:04 +1000 69) wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71) EXPORT_SYMBOL(__lock_buffer);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72)
fc9b52cd8f5f4 (Harvey Harrison 2008-02-08 04:19:52 -0800 73) void unlock_buffer(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 74) {
51b07fc3c5c83 (Nicholas Piggin 2008-10-18 20:27:00 -0700 75) clear_bit_unlock(BH_Lock, &bh->b_state);
4e857c58efeb9 (Peter Zijlstra 2014-03-17 18:06:10 +0100 76) smp_mb__after_atomic();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 77) wake_up_bit(&bh->b_state, BH_Lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 78) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 79) EXPORT_SYMBOL(unlock_buffer);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 80)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 81) /*
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 82) * Returns if the page has dirty or writeback buffers. If all the buffers
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 83) * are unlocked and clean then the PageDirty information is stale. If
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 84) * any of the pages are locked, it is assumed they are locked for IO.
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 85) */
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 86) void buffer_check_dirty_writeback(struct page *page,
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 87) bool *dirty, bool *writeback)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 88) {
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 89) struct buffer_head *head, *bh;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 90) *dirty = false;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 91) *writeback = false;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 92)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 93) BUG_ON(!PageLocked(page));
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 94)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 95) if (!page_has_buffers(page))
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 96) return;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 97)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 98) if (PageWriteback(page))
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 99) *writeback = true;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 100)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 101) head = page_buffers(page);
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 102) bh = head;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 103) do {
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 104) if (buffer_locked(bh))
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 105) *writeback = true;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 106)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 107) if (buffer_dirty(bh))
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 108) *dirty = true;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 109)
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 110) bh = bh->b_this_page;
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 111) } while (bh != head);
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 112) }
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 113) EXPORT_SYMBOL(buffer_check_dirty_writeback);
b45972265f823 (Mel Gorman 2013-07-03 15:02:05 -0700 114)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 115) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 116) * Block until a buffer comes unlocked. This doesn't stop it
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 117) * from becoming locked again - you have to lock it yourself
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 118) * if you want to preserve its state.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 119) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 120) void __wait_on_buffer(struct buffer_head * bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 121) {
743162013d40c (NeilBrown 2014-07-07 15:16:04 +1000 122) wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 123) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 124) EXPORT_SYMBOL(__wait_on_buffer);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 125)
b744c2ac4bbc0 (Robert Elliott 2014-10-21 13:55:09 -0600 126) static void buffer_io_error(struct buffer_head *bh, char *msg)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 127) {
432f16e64f50f (Robert Elliott 2014-10-21 13:55:11 -0600 128) if (!test_bit(BH_Quiet, &bh->b_state))
432f16e64f50f (Robert Elliott 2014-10-21 13:55:11 -0600 129) printk_ratelimited(KERN_ERR
a1c6f05733c27 (Dmitry Monakhov 2015-04-13 16:31:37 +0400 130) "Buffer I/O error on dev %pg, logical block %llu%s\n",
a1c6f05733c27 (Dmitry Monakhov 2015-04-13 16:31:37 +0400 131) bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 132) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 133)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 134) /*
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 135) * End-of-IO handler helper function which does not touch the bh after
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 136) * unlocking it.
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 137) * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 138) * a race there is benign: unlock_buffer() only use the bh's address for
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 139) * hashing after unlocking the buffer, so it doesn't actually touch the bh
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 140) * itself.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 141) */
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 142) static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 143) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 144) if (uptodate) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 145) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 146) } else {
70246286e94c3 (Christoph Hellwig 2016-07-19 11:28:41 +0200 147) /* This happens, due to failed read-ahead attempts. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 148) clear_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 149) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 150) unlock_buffer(bh);
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 151) }
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 152)
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 153) /*
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 154) * Default synchronous end-of-IO handler.. Just mark it up-to-date and
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 155) * unlock the buffer. This is what ll_rw_block uses too.
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 156) */
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 157) void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 158) {
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 159) __end_buffer_read_notouch(bh, uptodate);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 160) put_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 161) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 162) EXPORT_SYMBOL(end_buffer_read_sync);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 163)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 164) void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 165) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 166) if (uptodate) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 167) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 168) } else {
432f16e64f50f (Robert Elliott 2014-10-21 13:55:11 -0600 169) buffer_io_error(bh, ", lost sync page write");
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 170) mark_buffer_write_io_error(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 171) clear_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 172) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 173) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 174) put_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 175) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 176) EXPORT_SYMBOL(end_buffer_write_sync);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 177)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 178) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 179) * Various filesystems appear to want __find_get_block to be non-blocking.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 180) * But it's the page lock which protects the buffers. To get around this,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 181) * we get exclusion from try_to_free_buffers with the blockdev mapping's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 182) * private_lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 183) *
b93b016313b3b (Matthew Wilcox 2018-04-10 16:36:56 -0700 184) * Hack idea: for the blockdev mapping, private_lock contention
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 185) * may be quite high. This code could TryLock the page, and if that
b93b016313b3b (Matthew Wilcox 2018-04-10 16:36:56 -0700 186) * succeeds, there is no need to take private_lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 187) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 188) static struct buffer_head *
385fd4c59d8bf (Coywolf Qi Hunt 2005-11-07 00:59:39 -0800 189) __find_get_block_slow(struct block_device *bdev, sector_t block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 190) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 191) struct inode *bd_inode = bdev->bd_inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 192) struct address_space *bd_mapping = bd_inode->i_mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 193) struct buffer_head *ret = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 194) pgoff_t index;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 195) struct buffer_head *bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 196) struct buffer_head *head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 197) struct page *page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 198) int all_mapped = 1;
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 199) static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 200)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 201) index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
2457aec63745e (Mel Gorman 2014-06-04 16:10:31 -0700 202) page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 203) if (!page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 204) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 205)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 206) spin_lock(&bd_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 207) if (!page_has_buffers(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 208) goto out_unlock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 209) head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 210) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 211) do {
97f76d3d197f2 (Nikanth Karthikesan 2009-04-02 16:56:46 -0700 212) if (!buffer_mapped(bh))
97f76d3d197f2 (Nikanth Karthikesan 2009-04-02 16:56:46 -0700 213) all_mapped = 0;
97f76d3d197f2 (Nikanth Karthikesan 2009-04-02 16:56:46 -0700 214) else if (bh->b_blocknr == block) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 215) ret = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 216) get_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 217) goto out_unlock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 218) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 219) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 220) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 221)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 222) /* we might be here because some of the buffers on this page are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 223) * not mapped. This is due to various races between
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 224) * file io on the block device and getblk. It gets dealt with
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 225) * elsewhere, don't buffer_error if we had some unmapped buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 226) */
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 227) ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 228) if (all_mapped && __ratelimit(&last_warned)) {
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 229) printk("__find_get_block_slow() failed. block=%llu, "
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 230) "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 231) "device %pg blocksize: %d\n",
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 232) (unsigned long long)block,
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 233) (unsigned long long)bh->b_blocknr,
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 234) bh->b_state, bh->b_size, bdev,
43636c804df01 (Tetsuo Handa 2019-01-21 22:49:37 +0900 235) 1 << bd_inode->i_blkbits);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 236) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 237) out_unlock:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 238) spin_unlock(&bd_mapping->private_lock);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 239) put_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 240) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 241) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 242) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 243)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 244) static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 245) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 246) unsigned long flags;
a39722034ae37 (Nicholas Piggin 2005-07-07 17:56:56 -0700 247) struct buffer_head *first;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 248) struct buffer_head *tmp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 249) struct page *page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 250) int page_uptodate = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 251)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 252) BUG_ON(!buffer_async_read(bh));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 253)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 254) page = bh->b_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 255) if (uptodate) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 256) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 257) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 258) clear_buffer_uptodate(bh);
432f16e64f50f (Robert Elliott 2014-10-21 13:55:11 -0600 259) buffer_io_error(bh, ", async page read");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 260) SetPageError(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 261) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 262)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 263) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 264) * Be _very_ careful from here on. Bad things can happen if
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 265) * two buffer heads end IO at almost the same time and both
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 266) * decide that the page is now completely done.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 267) */
a39722034ae37 (Nicholas Piggin 2005-07-07 17:56:56 -0700 268) first = page_buffers(page);
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 269) spin_lock_irqsave(&first->b_uptodate_lock, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 270) clear_buffer_async_read(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 271) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 272) tmp = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 273) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 274) if (!buffer_uptodate(tmp))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 275) page_uptodate = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 276) if (buffer_async_read(tmp)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 277) BUG_ON(!buffer_locked(tmp));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 278) goto still_busy;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 279) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 280) tmp = tmp->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 281) } while (tmp != bh);
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 282) spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 283)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 284) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 285) * If none of the buffers had errors and they are all
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 286) * uptodate then we can set the page uptodate.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 287) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 288) if (page_uptodate && !PageError(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 289) SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 290) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 291) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 292)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 293) still_busy:
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 294) spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 295) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 296) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 297)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 298) struct decrypt_bh_ctx {
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 299) struct work_struct work;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 300) struct buffer_head *bh;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 301) };
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 302)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 303) static void decrypt_bh(struct work_struct *work)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 304) {
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 305) struct decrypt_bh_ctx *ctx =
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 306) container_of(work, struct decrypt_bh_ctx, work);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 307) struct buffer_head *bh = ctx->bh;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 308) int err;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 309)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 310) err = fscrypt_decrypt_pagecache_blocks(bh->b_page, bh->b_size,
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 311) bh_offset(bh));
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 312) end_buffer_async_read(bh, err == 0);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 313) kfree(ctx);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 314) }
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 315)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 316) /*
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 317) * I/O completion handler for block_read_full_page() - pages
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 318) * which come unlocked at the end of I/O.
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 319) */
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 320) static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 321) {
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 322) /* Decrypt if needed */
4f74d15fe4083 (Eric Biggers 2020-07-02 01:56:07 +0000 323) if (uptodate &&
4f74d15fe4083 (Eric Biggers 2020-07-02 01:56:07 +0000 324) fscrypt_inode_uses_fs_layer_crypto(bh->b_page->mapping->host)) {
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 325) struct decrypt_bh_ctx *ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 326)
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 327) if (ctx) {
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 328) INIT_WORK(&ctx->work, decrypt_bh);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 329) ctx->bh = bh;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 330) fscrypt_enqueue_decrypt_work(&ctx->work);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 331) return;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 332) }
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 333) uptodate = 0;
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 334) }
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 335) end_buffer_async_read(bh, uptodate);
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 336) }
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 337)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 338) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 339) * Completion handler for block_write_full_page() - pages which are unlocked
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 340) * during I/O, and which have PageWriteback cleared upon I/O completion.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 341) */
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 342) void end_buffer_async_write(struct buffer_head *bh, int uptodate)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 343) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 344) unsigned long flags;
a39722034ae37 (Nicholas Piggin 2005-07-07 17:56:56 -0700 345) struct buffer_head *first;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 346) struct buffer_head *tmp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 347) struct page *page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 348)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 349) BUG_ON(!buffer_async_write(bh));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 350)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 351) page = bh->b_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 352) if (uptodate) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 353) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 354) } else {
432f16e64f50f (Robert Elliott 2014-10-21 13:55:11 -0600 355) buffer_io_error(bh, ", lost async page write");
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 356) mark_buffer_write_io_error(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 357) clear_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 358) SetPageError(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 359) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 360)
a39722034ae37 (Nicholas Piggin 2005-07-07 17:56:56 -0700 361) first = page_buffers(page);
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 362) spin_lock_irqsave(&first->b_uptodate_lock, flags);
a39722034ae37 (Nicholas Piggin 2005-07-07 17:56:56 -0700 363)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 364) clear_buffer_async_write(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 365) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 366) tmp = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 367) while (tmp != bh) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 368) if (buffer_async_write(tmp)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 369) BUG_ON(!buffer_locked(tmp));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 370) goto still_busy;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 371) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 372) tmp = tmp->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 373) }
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 374) spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 375) end_page_writeback(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 376) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 377)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 378) still_busy:
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 379) spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 380) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 381) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 382) EXPORT_SYMBOL(end_buffer_async_write);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 383)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 384) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 385) * If a page's buffers are under async readin (end_buffer_async_read
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 386) * completion) then there is a possibility that another thread of
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 387) * control could lock one of the buffers after it has completed
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 388) * but while some of the other buffers have not completed. This
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 389) * locked buffer would confuse end_buffer_async_read() into not unlocking
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 390) * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 391) * that this buffer is not under async I/O.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 392) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 393) * The page comes unlocked when it has no locked buffer_async buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 394) * left.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 395) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 396) * PageLocked prevents anyone starting new async I/O reads any of
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 397) * the buffers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 398) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 399) * PageWriteback is used to prevent simultaneous writeout of the same
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 400) * page.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 401) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 402) * PageLocked prevents anyone from starting writeback of a page which is
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 403) * under read I/O (PageWriteback is only ever set against a locked page).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 404) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 405) static void mark_buffer_async_read(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 406) {
31fb992ce675e (Eric Biggers 2019-10-22 20:33:11 -0700 407) bh->b_end_io = end_buffer_async_read_io;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 408) set_buffer_async_read(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 409) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 410)
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 411) static void mark_buffer_async_write_endio(struct buffer_head *bh,
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 412) bh_end_io_t *handler)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 413) {
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 414) bh->b_end_io = handler;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 415) set_buffer_async_write(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 416) }
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 417)
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 418) void mark_buffer_async_write(struct buffer_head *bh)
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 419) {
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 420) mark_buffer_async_write_endio(bh, end_buffer_async_write);
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 421) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 422) EXPORT_SYMBOL(mark_buffer_async_write);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 423)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 424)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 425) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 426) * fs/buffer.c contains helper functions for buffer-backed address space's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 427) * fsync functions. A common requirement for buffer-based filesystems is
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 428) * that certain data from the backing blockdev needs to be written out for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 429) * a successful fsync(). For example, ext2 indirect blocks need to be
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 430) * written back and waited upon before fsync() returns.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 431) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 432) * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 433) * inode_has_buffers() and invalidate_inode_buffers() are provided for the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 434) * management of a list of dependent buffers at ->i_mapping->private_list.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 435) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 436) * Locking is a little subtle: try_to_free_buffers() will remove buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 437) * from their controlling inode's queue when they are being freed. But
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 438) * try_to_free_buffers() will be operating against the *blockdev* mapping
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 439) * at the time, not against the S_ISREG file which depends on those buffers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 440) * So the locking for private_list is via the private_lock in the address_space
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 441) * which backs the buffers. Which is different from the address_space
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 442) * against which the buffers are listed. So for a particular address_space,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 443) * mapping->private_lock does *not* protect mapping->private_list! In fact,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 444) * mapping->private_list will always be protected by the backing blockdev's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 445) * ->private_lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 446) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 447) * Which introduces a requirement: all buffers on an address_space's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 448) * ->private_list must be from the same address_space: the blockdev's.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 449) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 450) * address_spaces which do not place buffers at ->private_list via these
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 451) * utility functions are free to use private_lock and private_list for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 452) * whatever they want. The only requirement is that list_empty(private_list)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 453) * be true at clear_inode() time.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 454) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 455) * FIXME: clear_inode should not call invalidate_inode_buffers(). The
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 456) * filesystems should do that. invalidate_inode_buffers() should just go
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 457) * BUG_ON(!list_empty).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 458) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 459) * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 460) * take an address_space, not an inode. And it should be called
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 461) * mark_buffer_dirty_fsync() to clearly define why those buffers are being
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 462) * queued up.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 463) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 464) * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 465) * list if it is already on a list. Because if the buffer is on a list,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 466) * it *must* already be on the right one. If not, the filesystem is being
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 467) * silly. This will save a ton of locking. But first we have to ensure
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 468) * that buffers are taken *off* the old inode's list when they are freed
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 469) * (presumably in truncate). That requires careful auditing of all
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 470) * filesystems (do it inside bforget()). It could also be done by bringing
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 471) * b_inode back.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 472) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 473)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 474) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 475) * The buffer's backing address_space's private_lock must be held
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 476) */
dbacefc9c4f6b (Thomas Petazzoni 2008-07-29 22:33:47 -0700 477) static void __remove_assoc_queue(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 478) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 479) list_del_init(&bh->b_assoc_buffers);
58ff407bee5a5 (Jan Kara 2006-10-17 00:10:19 -0700 480) WARN_ON(!bh->b_assoc_map);
58ff407bee5a5 (Jan Kara 2006-10-17 00:10:19 -0700 481) bh->b_assoc_map = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 482) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 483)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 484) int inode_has_buffers(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 485) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 486) return !list_empty(&inode->i_data.private_list);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 487) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 488)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 489) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 490) * osync is designed to support O_SYNC io. It waits synchronously for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 491) * all already-submitted IO to complete, but does not queue any new
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 492) * writes to the disk.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 493) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 494) * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 495) * you dirty the buffers, and then use osync_inode_buffers to wait for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 496) * completion. Any other dirty buffers which are not yet queued for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 497) * write will not be flushed to disk by the osync.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 498) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 499) static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 500) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 501) struct buffer_head *bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 502) struct list_head *p;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 503) int err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 504)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 505) spin_lock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 506) repeat:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 507) list_for_each_prev(p, list) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 508) bh = BH_ENTRY(p);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 509) if (buffer_locked(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 510) get_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 511) spin_unlock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 512) wait_on_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 513) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 514) err = -EIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 515) brelse(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 516) spin_lock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 517) goto repeat;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 518) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 519) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 520) spin_unlock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 521) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 522) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 523)
08fdc8a0138af (Mateusz Guzik 2017-10-03 18:17:41 +0200 524) void emergency_thaw_bdev(struct super_block *sb)
c2d7543851849 (Eric Sandeen 2009-03-31 15:23:46 -0700 525) {
040f04bd2e825 (Christoph Hellwig 2020-11-24 11:54:06 +0100 526) while (sb->s_bdev && !thaw_bdev(sb->s_bdev))
a1c6f05733c27 (Dmitry Monakhov 2015-04-13 16:31:37 +0400 527) printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
01a05b337a5b6 (Al Viro 2010-03-23 06:06:58 -0400 528) }
c2d7543851849 (Eric Sandeen 2009-03-31 15:23:46 -0700 529)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 530) /**
78a4a50a86b0a (Randy Dunlap 2008-02-29 22:02:31 -0800 531) * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
67be2dd1bace0 (Martin Waitz 2005-05-01 08:59:26 -0700 532) * @mapping: the mapping which wants those buffers written
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 533) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 534) * Starts I/O against the buffers at mapping->private_list, and waits upon
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 535) * that I/O.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 536) *
67be2dd1bace0 (Martin Waitz 2005-05-01 08:59:26 -0700 537) * Basically, this is a convenience function for fsync().
67be2dd1bace0 (Martin Waitz 2005-05-01 08:59:26 -0700 538) * @mapping is a file or directory which needs those buffers to be written for
67be2dd1bace0 (Martin Waitz 2005-05-01 08:59:26 -0700 539) * a successful fsync().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 540) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 541) int sync_mapping_buffers(struct address_space *mapping)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 542) {
252aa6f5be64c (Rafael Aquini 2012-12-11 16:02:35 -0800 543) struct address_space *buffer_mapping = mapping->private_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 544)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 545) if (buffer_mapping == NULL || list_empty(&mapping->private_list))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 546) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 547)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 548) return fsync_buffers_list(&buffer_mapping->private_lock,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 549) &mapping->private_list);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 550) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 551) EXPORT_SYMBOL(sync_mapping_buffers);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 552)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 553) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 554) * Called when we've recently written block `bblock', and it is known that
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 555) * `bblock' was for a buffer_boundary() buffer. This means that the block at
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 556) * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 557) * dirty, schedule it for IO. So that indirects merge nicely with their data.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 558) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 559) void write_boundary_block(struct block_device *bdev,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 560) sector_t bblock, unsigned blocksize)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 561) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 562) struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 563) if (bh) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 564) if (buffer_dirty(bh))
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 565) ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 566) put_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 567) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 568) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 569)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 570) void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 571) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 572) struct address_space *mapping = inode->i_mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 573) struct address_space *buffer_mapping = bh->b_page->mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 574)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 575) mark_buffer_dirty(bh);
252aa6f5be64c (Rafael Aquini 2012-12-11 16:02:35 -0800 576) if (!mapping->private_data) {
252aa6f5be64c (Rafael Aquini 2012-12-11 16:02:35 -0800 577) mapping->private_data = buffer_mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 578) } else {
252aa6f5be64c (Rafael Aquini 2012-12-11 16:02:35 -0800 579) BUG_ON(mapping->private_data != buffer_mapping);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 580) }
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 581) if (!bh->b_assoc_map) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 582) spin_lock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 583) list_move_tail(&bh->b_assoc_buffers,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 584) &mapping->private_list);
58ff407bee5a5 (Jan Kara 2006-10-17 00:10:19 -0700 585) bh->b_assoc_map = mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 586) spin_unlock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 587) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 588) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 589) EXPORT_SYMBOL(mark_buffer_dirty_inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 590)
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 591) /*
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 592) * Mark the page dirty, and set it dirty in the page cache, and mark the inode
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 593) * dirty.
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 594) *
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 595) * If warn is true, then emit a warning if the page is not uptodate and has
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 596) * not been truncated.
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 597) *
81f8c3a461d16 (Johannes Weiner 2016-03-15 14:57:04 -0700 598) * The caller must hold lock_page_memcg().
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 599) */
f82b376413298 (Matthew Wilcox 2018-04-10 16:36:44 -0700 600) void __set_page_dirty(struct page *page, struct address_space *mapping,
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 601) int warn)
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 602) {
227d53b397a32 (KOSAKI Motohiro 2014-02-06 12:04:28 -0800 603) unsigned long flags;
227d53b397a32 (KOSAKI Motohiro 2014-02-06 12:04:28 -0800 604)
b93b016313b3b (Matthew Wilcox 2018-04-10 16:36:56 -0700 605) xa_lock_irqsave(&mapping->i_pages, flags);
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 606) if (page->mapping) { /* Race with truncate? */
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 607) WARN_ON_ONCE(warn && !PageUptodate(page));
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 608) account_page_dirtied(page, mapping);
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 609) __xa_set_mark(&mapping->i_pages, page_index(page),
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 610) PAGECACHE_TAG_DIRTY);
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 611) }
b93b016313b3b (Matthew Wilcox 2018-04-10 16:36:56 -0700 612) xa_unlock_irqrestore(&mapping->i_pages, flags);
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 613) }
f82b376413298 (Matthew Wilcox 2018-04-10 16:36:44 -0700 614) EXPORT_SYMBOL_GPL(__set_page_dirty);
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 615)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 616) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 617) * Add a page to the dirty page list.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 618) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 619) * It is a sad fact of life that this function is called from several places
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 620) * deeply under spinlocking. It may not sleep.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 621) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 622) * If the page has buffers, the uptodate buffers are set dirty, to preserve
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 623) * dirty-state coherency between the page and the buffers. It the page does
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 624) * not have buffers then when they are later attached they will all be set
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 625) * dirty.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 626) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 627) * The buffers are dirtied before the page is dirtied. There's a small race
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 628) * window in which a writepage caller may see the page cleanness but not the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 629) * buffer dirtiness. That's fine. If this code were to set the page dirty
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 630) * before the buffers, a concurrent writepage caller could clear the page dirty
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 631) * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 632) * page on the dirty page list.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 633) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 634) * We use private_lock to lock against try_to_free_buffers while using the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 635) * page's buffer list. Also use this to protect against clean buffers being
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 636) * added to the page after it was set dirty.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 637) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 638) * FIXME: may need to call ->reservepage here as well. That's rather up to the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 639) * address_space though.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 640) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 641) int __set_page_dirty_buffers(struct page *page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 642) {
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 643) int newly_dirty;
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 644) struct address_space *mapping = page_mapping(page);
ebf7a227dd1d8 (Nicholas Piggin 2006-10-10 04:36:54 +0200 645)
ebf7a227dd1d8 (Nicholas Piggin 2006-10-10 04:36:54 +0200 646) if (unlikely(!mapping))
ebf7a227dd1d8 (Nicholas Piggin 2006-10-10 04:36:54 +0200 647) return !TestSetPageDirty(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 648)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 649) spin_lock(&mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 650) if (page_has_buffers(page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 651) struct buffer_head *head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 652) struct buffer_head *bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 653)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 654) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 655) set_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 656) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 657) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 658) }
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 659) /*
bcfe06bf2622f (Roman Gushchin 2020-12-01 13:58:27 -0800 660) * Lock out page's memcg migration to keep PageDirty
81f8c3a461d16 (Johannes Weiner 2016-03-15 14:57:04 -0700 661) * synchronized with per-memcg dirty page counters.
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 662) */
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 663) lock_page_memcg(page);
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 664) newly_dirty = !TestSetPageDirty(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 665) spin_unlock(&mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 666)
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 667) if (newly_dirty)
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 668) __set_page_dirty(page, mapping, 1);
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 669)
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 670) unlock_page_memcg(page);
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 671)
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 672) if (newly_dirty)
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 673) __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 674)
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 675) return newly_dirty;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 676) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 677) EXPORT_SYMBOL(__set_page_dirty_buffers);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 678)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 679) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 680) * Write out and wait upon a list of buffers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 681) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 682) * We have conflicting pressures: we want to make sure that all
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 683) * initially dirty buffers get waited on, but that any subsequently
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 684) * dirtied buffers don't. After all, we don't want fsync to last
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 685) * forever if somebody is actively writing to the file.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 686) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 687) * Do this in two main stages: first we copy dirty buffers to a
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 688) * temporary inode list, queueing the writes as we go. Then we clean
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 689) * up, waiting for those writes to complete.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 690) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 691) * During this second stage, any subsequent updates to the file may end
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 692) * up refiling the buffer on the original inode's dirty list again, so
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 693) * there is a chance we will end up with a buffer queued for write but
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 694) * not yet completed on that list. So, as a final cleanup we go through
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 695) * the osync code to catch these locked, dirty buffers without requeuing
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 696) * any newly dirty buffers for write.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 697) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 698) static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 699) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 700) struct buffer_head *bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 701) struct list_head tmp;
7eaceaccab5f4 (Jens Axboe 2011-03-10 08:52:07 +0100 702) struct address_space *mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 703) int err = 0, err2;
4ee2491ed8569 (Jens Axboe 2011-03-17 10:51:40 +0100 704) struct blk_plug plug;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 705)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 706) INIT_LIST_HEAD(&tmp);
4ee2491ed8569 (Jens Axboe 2011-03-17 10:51:40 +0100 707) blk_start_plug(&plug);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 708)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 709) spin_lock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 710) while (!list_empty(list)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 711) bh = BH_ENTRY(list->next);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 712) mapping = bh->b_assoc_map;
58ff407bee5a5 (Jan Kara 2006-10-17 00:10:19 -0700 713) __remove_assoc_queue(bh);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 714) /* Avoid race with mark_buffer_dirty_inode() which does
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 715) * a lockless check and we rely on seeing the dirty bit */
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 716) smp_mb();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 717) if (buffer_dirty(bh) || buffer_locked(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 718) list_add(&bh->b_assoc_buffers, &tmp);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 719) bh->b_assoc_map = mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 720) if (buffer_dirty(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 721) get_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 722) spin_unlock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 723) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 724) * Ensure any pending I/O completes so that
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 725) * write_dirty_buffer() actually writes the
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 726) * current contents - it is a noop if I/O is
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 727) * still in flight on potentially older
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 728) * contents.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 729) */
70fd76140a6cb (Christoph Hellwig 2016-11-01 07:40:10 -0600 730) write_dirty_buffer(bh, REQ_SYNC);
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 731)
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 732) /*
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 733) * Kick off IO for the previous mapping. Note
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 734) * that we will not run the very last mapping,
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 735) * wait_on_buffer() will do that for us
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 736) * through sync_buffer().
9cf6b720f84d6 (Jens Axboe 2009-04-06 14:48:03 +0200 737) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 738) brelse(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 739) spin_lock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 740) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 741) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 742) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 743)
4ee2491ed8569 (Jens Axboe 2011-03-17 10:51:40 +0100 744) spin_unlock(lock);
4ee2491ed8569 (Jens Axboe 2011-03-17 10:51:40 +0100 745) blk_finish_plug(&plug);
4ee2491ed8569 (Jens Axboe 2011-03-17 10:51:40 +0100 746) spin_lock(lock);
4ee2491ed8569 (Jens Axboe 2011-03-17 10:51:40 +0100 747)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 748) while (!list_empty(&tmp)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 749) bh = BH_ENTRY(tmp.prev);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 750) get_bh(bh);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 751) mapping = bh->b_assoc_map;
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 752) __remove_assoc_queue(bh);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 753) /* Avoid race with mark_buffer_dirty_inode() which does
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 754) * a lockless check and we rely on seeing the dirty bit */
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 755) smp_mb();
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 756) if (buffer_dirty(bh)) {
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 757) list_add(&bh->b_assoc_buffers,
e3892296de632 (Jan Kara 2008-03-04 14:28:33 -0800 758) &mapping->private_list);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 759) bh->b_assoc_map = mapping;
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 760) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 761) spin_unlock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 762) wait_on_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 763) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 764) err = -EIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 765) brelse(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 766) spin_lock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 767) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 768)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 769) spin_unlock(lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 770) err2 = osync_buffers_list(lock, list);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 771) if (err)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 772) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 773) else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 774) return err2;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 775) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 776)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 777) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 778) * Invalidate any and all dirty buffers on a given inode. We are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 779) * probably unmounting the fs, but that doesn't mean we have already
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 780) * done a sync(). Just drop the buffers from the inode list.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 781) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 782) * NOTE: we take the inode's blockdev's mapping's private_lock. Which
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 783) * assumes that all the buffers are against the blockdev. Not true
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 784) * for reiserfs.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 785) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 786) void invalidate_inode_buffers(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 787) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 788) if (inode_has_buffers(inode)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 789) struct address_space *mapping = &inode->i_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 790) struct list_head *list = &mapping->private_list;
252aa6f5be64c (Rafael Aquini 2012-12-11 16:02:35 -0800 791) struct address_space *buffer_mapping = mapping->private_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 792)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 793) spin_lock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 794) while (!list_empty(list))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 795) __remove_assoc_queue(BH_ENTRY(list->next));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 796) spin_unlock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 797) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 798) }
52b19ac993f1a (Jan Kara 2008-09-23 18:24:08 +0200 799) EXPORT_SYMBOL(invalidate_inode_buffers);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 800)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 801) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 802) * Remove any clean buffers from the inode's buffer list. This is called
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 803) * when we're trying to free the inode itself. Those buffers can pin it.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 804) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 805) * Returns true if all buffers were removed.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 806) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 807) int remove_inode_buffers(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 808) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 809) int ret = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 810)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 811) if (inode_has_buffers(inode)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 812) struct address_space *mapping = &inode->i_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 813) struct list_head *list = &mapping->private_list;
252aa6f5be64c (Rafael Aquini 2012-12-11 16:02:35 -0800 814) struct address_space *buffer_mapping = mapping->private_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 815)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 816) spin_lock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 817) while (!list_empty(list)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 818) struct buffer_head *bh = BH_ENTRY(list->next);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 819) if (buffer_dirty(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 820) ret = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 821) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 822) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 823) __remove_assoc_queue(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 824) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 825) spin_unlock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 826) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 827) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 828) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 829)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 830) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 831) * Create the appropriate buffers when given a page for data area and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 832) * the size of each buffer.. Use the bh->b_this_page linked list to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 833) * follow the buffers created. Return NULL if unable to create more
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 834) * buffers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 835) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 836) * The retry flag is used to differentiate async IO (paging, swapping)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 837) * which may not fail from ordinary buffer allocations.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 838) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 839) struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 840) bool retry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 841) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 842) struct buffer_head *bh, *head;
f745c6f5fe757 (Shakeel Butt 2018-08-17 15:46:44 -0700 843) gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 844) long offset;
b87d8cefe43c7 (Roman Gushchin 2020-10-17 16:13:40 -0700 845) struct mem_cgroup *memcg, *old_memcg;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 846)
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 847) if (retry)
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 848) gfp |= __GFP_NOFAIL;
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 849)
6eeb104e114cb (Johannes Weiner 2021-02-24 12:04:15 -0800 850) /* The page lock pins the memcg */
6eeb104e114cb (Johannes Weiner 2021-02-24 12:04:15 -0800 851) memcg = page_memcg(page);
b87d8cefe43c7 (Roman Gushchin 2020-10-17 16:13:40 -0700 852) old_memcg = set_active_memcg(memcg);
f745c6f5fe757 (Shakeel Butt 2018-08-17 15:46:44 -0700 853)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 854) head = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 855) offset = PAGE_SIZE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 856) while ((offset -= size) >= 0) {
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 857) bh = alloc_buffer_head(gfp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 858) if (!bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 859) goto no_grow;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 860)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 861) bh->b_this_page = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 862) bh->b_blocknr = -1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 863) head = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 864)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 865) bh->b_size = size;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 866)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 867) /* Link the buffer to its page */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 868) set_bh_page(bh, page, offset);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 869) }
f745c6f5fe757 (Shakeel Butt 2018-08-17 15:46:44 -0700 870) out:
b87d8cefe43c7 (Roman Gushchin 2020-10-17 16:13:40 -0700 871) set_active_memcg(old_memcg);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 872) return head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 873) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 874) * In case anything failed, we just free everything we got.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 875) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 876) no_grow:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 877) if (head) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 878) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 879) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 880) head = head->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 881) free_buffer_head(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 882) } while (head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 883) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 884)
f745c6f5fe757 (Shakeel Butt 2018-08-17 15:46:44 -0700 885) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 886) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 887) EXPORT_SYMBOL_GPL(alloc_page_buffers);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 888)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 889) static inline void
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 890) link_dev_buffers(struct page *page, struct buffer_head *head)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 891) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 892) struct buffer_head *bh, *tail;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 893)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 894) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 895) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 896) tail = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 897) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 898) } while (bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 899) tail->b_this_page = head;
45dcfc27329f6 (Guoqing Jiang 2020-06-01 21:47:48 -0700 900) attach_page_private(page, head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 901) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 902)
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 903) static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 904) {
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 905) sector_t retval = ~((sector_t)0);
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 906) loff_t sz = i_size_read(bdev->bd_inode);
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 907)
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 908) if (sz) {
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 909) unsigned int sizebits = blksize_bits(size);
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 910) retval = (sz >> sizebits);
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 911) }
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 912) return retval;
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 913) }
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 914)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 915) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 916) * Initialise the state of a blockdev page's buffers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 917) */
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 918) static sector_t
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 919) init_page_buffers(struct page *page, struct block_device *bdev,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 920) sector_t block, int size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 921) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 922) struct buffer_head *head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 923) struct buffer_head *bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 924) int uptodate = PageUptodate(page);
bbec0270bdd88 (Linus Torvalds 2012-11-29 12:31:52 -0800 925) sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 926)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 927) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 928) if (!buffer_mapped(bh)) {
01950a349ec25 (Eric Biggers 2018-01-16 22:25:12 -0800 929) bh->b_end_io = NULL;
01950a349ec25 (Eric Biggers 2018-01-16 22:25:12 -0800 930) bh->b_private = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 931) bh->b_bdev = bdev;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 932) bh->b_blocknr = block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 933) if (uptodate)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 934) set_buffer_uptodate(bh);
080399aaaf353 (Jeff Moyer 2012-05-11 16:34:10 +0200 935) if (block < end_block)
080399aaaf353 (Jeff Moyer 2012-05-11 16:34:10 +0200 936) set_buffer_mapped(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 937) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 938) block++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 939) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 940) } while (bh != head);
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 941)
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 942) /*
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 943) * Caller needs to validate requested block against end of device.
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 944) */
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 945) return end_block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 946) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 947)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 948) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 949) * Create the page-cache page that contains the requested block.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 950) *
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 951) * This is used purely for blockdev mappings.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 952) */
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 953) static int
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 954) grow_dev_page(struct block_device *bdev, sector_t block,
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 955) pgoff_t index, int size, int sizebits, gfp_t gfp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 956) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 957) struct inode *inode = bdev->bd_inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 958) struct page *page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 959) struct buffer_head *bh;
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 960) sector_t end_block;
c4b4c2a78a9fc (Zhiqiang Liu 2020-04-13 13:12:10 +0800 961) int ret = 0;
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 962) gfp_t gfp_mask;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 963)
c62d25556be6c (Michal Hocko 2015-11-06 16:28:49 -0800 964) gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 965)
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 966) /*
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 967) * XXX: __getblk_slow() can not really deal with failure and
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 968) * will endlessly loop on improvised global reclaim. Prefer
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 969) * looping in the allocator rather than here, at least that
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 970) * code knows what it's doing.
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 971) */
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 972) gfp_mask |= __GFP_NOFAIL;
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 973)
84235de394d97 (Johannes Weiner 2013-10-16 13:47:00 -0700 974) page = find_or_create_page(inode->i_mapping, index, gfp_mask);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 975)
e827f92355e1e (Eric Sesterhenn 2006-03-26 18:24:46 +0200 976) BUG_ON(!PageLocked(page));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 977)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 978) if (page_has_buffers(page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 979) bh = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 980) if (bh->b_size == size) {
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 981) end_block = init_page_buffers(page, bdev,
f2d5a94436cc7 (Anton Altaparmakov 2014-09-22 01:53:03 +0100 982) (sector_t)index << sizebits,
f2d5a94436cc7 (Anton Altaparmakov 2014-09-22 01:53:03 +0100 983) size);
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 984) goto done;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 985) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 986) if (!try_to_free_buffers(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 987) goto failed;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 988) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 989)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 990) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 991) * Allocate some buffers for this page
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 992) */
94dc24c0c59a2 (Jens Axboe 2017-09-27 05:45:36 -0600 993) bh = alloc_page_buffers(page, size, true);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 994)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 995) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 996) * Link the page to the buffers and initialise them. Take the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 997) * lock to be atomic wrt __find_get_block(), which does not
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 998) * run under the page lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 999) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1000) spin_lock(&inode->i_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1001) link_dev_buffers(page, bh);
f2d5a94436cc7 (Anton Altaparmakov 2014-09-22 01:53:03 +0100 1002) end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
f2d5a94436cc7 (Anton Altaparmakov 2014-09-22 01:53:03 +0100 1003) size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1004) spin_unlock(&inode->i_mapping->private_lock);
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1005) done:
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1006) ret = (block < end_block) ? 1 : -ENXIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1007) failed:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1008) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1009) put_page(page);
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1010) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1011) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1012)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1013) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1014) * Create buffers for the specified block device block's page. If
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1015) * that page was dirty, the buffers are set dirty also.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1016) */
858119e159384 (Arjan van de Ven 2006-01-14 13:20:43 -0800 1017) static int
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1018) grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1019) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1020) pgoff_t index;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1021) int sizebits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1022)
90432e600619c (Mikulas Patocka 2021-03-22 10:05:05 -0400 1023) sizebits = PAGE_SHIFT - __ffs(size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1024) index = block >> sizebits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1025)
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1026) /*
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1027) * Check for a block which wants to lie outside our maximum possible
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1028) * pagecache index. (this comparison is done using sector_t types).
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1029) */
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1030) if (unlikely(index != block >> sizebits)) {
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1031) printk(KERN_ERR "%s: requested out-of-range block %llu for "
a1c6f05733c27 (Dmitry Monakhov 2015-04-13 16:31:37 +0400 1032) "device %pg\n",
8e24eea728068 (Harvey Harrison 2008-04-30 00:55:09 -0700 1033) __func__, (unsigned long long)block,
a1c6f05733c27 (Dmitry Monakhov 2015-04-13 16:31:37 +0400 1034) bdev);
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1035) return -EIO;
e5657933863f4 (Andrew Morton 2006-10-11 01:21:46 -0700 1036) }
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1037)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1038) /* Create a page with the proper size buffers.. */
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1039) return grow_dev_page(bdev, block, index, size, sizebits, gfp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1040) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1041)
0026ba4008e08 (Eric Biggers 2016-09-12 13:30:41 -0700 1042) static struct buffer_head *
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1043) __getblk_slow(struct block_device *bdev, sector_t block,
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1044) unsigned size, gfp_t gfp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1045) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1046) /* Size must be multiple of hard sectorsize */
e1defc4ff0cf5 (Martin K. Petersen 2009-05-22 17:17:49 -0400 1047) if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1048) (size < 512 || size > PAGE_SIZE))) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1049) printk(KERN_ERR "getblk(): invalid block size %d requested\n",
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1050) size);
e1defc4ff0cf5 (Martin K. Petersen 2009-05-22 17:17:49 -0400 1051) printk(KERN_ERR "logical block size: %d\n",
e1defc4ff0cf5 (Martin K. Petersen 2009-05-22 17:17:49 -0400 1052) bdev_logical_block_size(bdev));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1053)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1054) dump_stack();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1055) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1056) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1057)
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1058) for (;;) {
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1059) struct buffer_head *bh;
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1060) int ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1061)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1062) bh = __find_get_block(bdev, block, size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1063) if (bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1064) return bh;
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1065)
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1066) ret = grow_buffers(bdev, block, size, gfp);
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1067) if (ret < 0)
676ce6d5ca309 (Hugh Dickins 2012-08-23 12:17:36 +0200 1068) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1069) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1070) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1071)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1072) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1073) * The relationship between dirty buffers and dirty pages:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1074) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1075) * Whenever a page has any dirty buffers, the page's dirty bit is set, and
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 1076) * the page is tagged dirty in the page cache.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1077) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1078) * At all times, the dirtiness of the buffers represents the dirtiness of
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1079) * subsections of the page. If the page has buffers, the page dirty bit is
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1080) * merely a hint about the true dirty state.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1081) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1082) * When a page is set dirty in its entirety, all its buffers are marked dirty
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1083) * (if the page has buffers).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1084) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1085) * When a buffer is marked dirty, its page is dirtied, but the page's other
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1086) * buffers are not.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1087) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1088) * Also. When blockdev buffers are explicitly read with bread(), they
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1089) * individually become uptodate. But their backing page remains not
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1090) * uptodate - even if all of its buffers are uptodate. A subsequent
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1091) * block_read_full_page() against that page will discover all the uptodate
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1092) * buffers, will set the page uptodate and will perform no I/O.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1093) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1094)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1095) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1096) * mark_buffer_dirty - mark a buffer_head as needing writeout
67be2dd1bace0 (Martin Waitz 2005-05-01 08:59:26 -0700 1097) * @bh: the buffer_head to mark dirty
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1098) *
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 1099) * mark_buffer_dirty() will set the dirty bit against the buffer, then set
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 1100) * its backing page dirty, then tag the page as dirty in the page cache
ec82e1c1c89ce (Matthew Wilcox 2017-12-04 10:40:41 -0500 1101) * and then attach the address_space's inode to its superblock's dirty
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1102) * inode list.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1103) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1104) * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
b93b016313b3b (Matthew Wilcox 2018-04-10 16:36:56 -0700 1105) * i_pages lock and mapping->host->i_lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1106) */
fc9b52cd8f5f4 (Harvey Harrison 2008-02-08 04:19:52 -0800 1107) void mark_buffer_dirty(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1108) {
787d2214c19bc (Nicholas Piggin 2007-07-17 04:03:34 -0700 1109) WARN_ON_ONCE(!buffer_uptodate(bh));
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1110)
5305cb8308345 (Tejun Heo 2013-01-11 13:06:36 -0800 1111) trace_block_dirty_buffer(bh);
5305cb8308345 (Tejun Heo 2013-01-11 13:06:36 -0800 1112)
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1113) /*
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1114) * Very *carefully* optimize the it-is-already-dirty case.
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1115) *
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1116) * Don't let the final "is it dirty" escape to before we
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1117) * perhaps modified the buffer.
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1118) */
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1119) if (buffer_dirty(bh)) {
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1120) smp_mb();
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1121) if (buffer_dirty(bh))
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1122) return;
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1123) }
1be62dc190eba (Linus Torvalds 2008-04-04 14:38:17 -0700 1124)
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 1125) if (!test_set_buffer_dirty(bh)) {
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 1126) struct page *page = bh->b_page;
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 1127) struct address_space *mapping = NULL;
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 1128)
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 1129) lock_page_memcg(page);
8e9d78edea3ce (Linus Torvalds 2009-08-21 17:40:08 -0700 1130) if (!TestSetPageDirty(page)) {
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 1131) mapping = page_mapping(page);
8e9d78edea3ce (Linus Torvalds 2009-08-21 17:40:08 -0700 1132) if (mapping)
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 1133) __set_page_dirty(page, mapping, 0);
8e9d78edea3ce (Linus Torvalds 2009-08-21 17:40:08 -0700 1134) }
62cccb8c8e7a3 (Johannes Weiner 2016-03-15 14:57:22 -0700 1135) unlock_page_memcg(page);
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 1136) if (mapping)
c4843a7593a9d (Greg Thelen 2015-05-22 17:13:16 -0400 1137) __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
a8e7d49aa7be7 (Linus Torvalds 2009-03-19 11:32:05 -0700 1138) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1139) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 1140) EXPORT_SYMBOL(mark_buffer_dirty);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1141)
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1142) void mark_buffer_write_io_error(struct buffer_head *bh)
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1143) {
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1144) struct super_block *sb;
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1145)
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1146) set_buffer_write_io_error(bh);
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1147) /* FIXME: do we need to set this in both places? */
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1148) if (bh->b_page && bh->b_page->mapping)
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1149) mapping_set_error(bh->b_page->mapping, -EIO);
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1150) if (bh->b_assoc_map)
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1151) mapping_set_error(bh->b_assoc_map, -EIO);
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1152) rcu_read_lock();
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1153) sb = READ_ONCE(bh->b_bdev->bd_super);
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1154) if (sb)
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1155) errseq_set(&sb->s_wb_err, -EIO);
485e9605c0573 (Jeff Layton 2020-06-01 21:45:40 -0700 1156) rcu_read_unlock();
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1157) }
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1158) EXPORT_SYMBOL(mark_buffer_write_io_error);
87354e5de04fe (Jeff Layton 2017-07-06 07:02:21 -0400 1159)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1160) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1161) * Decrement a buffer_head's reference count. If all buffers against a page
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1162) * have zero reference count, are clean and unlocked, and if the page is clean
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1163) * and unlocked then try_to_free_buffers() may strip the buffers from the page
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1164) * in preparation for freeing it (sometimes, rarely, buffers are removed from
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1165) * a page but it ends up not being freed, and buffers may later be reattached).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1166) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1167) void __brelse(struct buffer_head * buf)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1168) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1169) if (atomic_read(&buf->b_count)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1170) put_bh(buf);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1171) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1172) }
5c752ad9f3591 (Arjan van de Ven 2008-07-25 19:45:40 -0700 1173) WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1174) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 1175) EXPORT_SYMBOL(__brelse);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1176)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1177) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1178) * bforget() is like brelse(), except it discards any
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1179) * potentially dirty data.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1180) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1181) void __bforget(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1182) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1183) clear_buffer_dirty(bh);
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 1184) if (bh->b_assoc_map) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1185) struct address_space *buffer_mapping = bh->b_page->mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1186)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1187) spin_lock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1188) list_del_init(&bh->b_assoc_buffers);
58ff407bee5a5 (Jan Kara 2006-10-17 00:10:19 -0700 1189) bh->b_assoc_map = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1190) spin_unlock(&buffer_mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1191) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1192) __brelse(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1193) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 1194) EXPORT_SYMBOL(__bforget);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1195)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1196) static struct buffer_head *__bread_slow(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1197) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1198) lock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1199) if (buffer_uptodate(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1200) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1201) return bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1202) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1203) get_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1204) bh->b_end_io = end_buffer_read_sync;
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 1205) submit_bh(REQ_OP_READ, 0, bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1206) wait_on_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1207) if (buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1208) return bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1209) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1210) brelse(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1211) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1212) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1213)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1214) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1215) * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1216) * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1217) * refcount elevated by one when they're in an LRU. A buffer can only appear
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1218) * once in a particular CPU's LRU. A single buffer can be present in multiple
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1219) * CPU's LRUs at the same time.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1220) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1221) * This is a transparent caching front-end to sb_bread(), sb_getblk() and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1222) * sb_find_get_block().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1223) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1224) * The LRUs themselves only need locking against invalidate_bh_lrus. We use
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1225) * a local interrupt disable for that.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1226) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1227)
86cf78d73de8c (Sebastien Buisson 2014-10-09 15:29:38 -0700 1228) #define BH_LRU_SIZE 16
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1229)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1230) struct bh_lru {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1231) struct buffer_head *bhs[BH_LRU_SIZE];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1232) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1233)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1234) static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1235)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1236) #ifdef CONFIG_SMP
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1237) #define bh_lru_lock() local_irq_disable()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1238) #define bh_lru_unlock() local_irq_enable()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1239) #else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1240) #define bh_lru_lock() preempt_disable()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1241) #define bh_lru_unlock() preempt_enable()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1242) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1243)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1244) static inline void check_irqs_on(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1245) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1246) #ifdef irqs_disabled
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1247) BUG_ON(irqs_disabled());
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1248) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1249) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1250)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1251) /*
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1252) * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1253) * inserted at the front, and the buffer_head at the back if any is evicted.
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1254) * Or, if already in the LRU it is moved to the front.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1255) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1256) static void bh_lru_install(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1257) {
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1258) struct buffer_head *evictee = bh;
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1259) struct bh_lru *b;
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1260) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1261)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1262) check_irqs_on();
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1263) /*
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1264) * the refcount of buffer_head in bh_lru prevents dropping the
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1265) * attached page(i.e., try_to_free_buffers) so it could cause
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1266) * failing page migration.
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1267) * Skip putting upcoming bh into bh_lru until migration is done.
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1268) */
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1269) if (lru_cache_disabled())
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1270) return;
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1271)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1272) bh_lru_lock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1273)
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1274) b = this_cpu_ptr(&bh_lrus);
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1275) for (i = 0; i < BH_LRU_SIZE; i++) {
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1276) swap(evictee, b->bhs[i]);
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1277) if (evictee == bh) {
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1278) bh_lru_unlock();
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1279) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1280) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1281) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1282)
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1283) get_bh(bh);
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1284) bh_lru_unlock();
241f01fbeda25 (Eric Biggers 2017-07-10 15:47:29 -0700 1285) brelse(evictee);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1286) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1287)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1288) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1289) * Look up the bh in this cpu's LRU. If it's there, move it to the head.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1290) */
858119e159384 (Arjan van de Ven 2006-01-14 13:20:43 -0800 1291) static struct buffer_head *
3991d3bd15063 (Tomasz Kvarsin 2007-02-12 00:52:14 -0800 1292) lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1293) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1294) struct buffer_head *ret = NULL;
3991d3bd15063 (Tomasz Kvarsin 2007-02-12 00:52:14 -0800 1295) unsigned int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1296)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1297) check_irqs_on();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1298) bh_lru_lock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1299) for (i = 0; i < BH_LRU_SIZE; i++) {
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 1300) struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1301)
9470dd5d35298 (Zach Brown 2014-10-13 15:55:05 -0700 1302) if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
9470dd5d35298 (Zach Brown 2014-10-13 15:55:05 -0700 1303) bh->b_size == size) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1304) if (i) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1305) while (i) {
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 1306) __this_cpu_write(bh_lrus.bhs[i],
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 1307) __this_cpu_read(bh_lrus.bhs[i - 1]));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1308) i--;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1309) }
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 1310) __this_cpu_write(bh_lrus.bhs[0], bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1311) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1312) get_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1313) ret = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1314) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1315) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1316) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1317) bh_lru_unlock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1318) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1319) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1320)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1321) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1322) * Perform a pagecache lookup for the matching buffer. If it's there, refresh
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1323) * it in the LRU and mark it as accessed. If it is not present then return
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1324) * NULL
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1325) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1326) struct buffer_head *
3991d3bd15063 (Tomasz Kvarsin 2007-02-12 00:52:14 -0800 1327) __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1328) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1329) struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1330)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1331) if (bh == NULL) {
2457aec63745e (Mel Gorman 2014-06-04 16:10:31 -0700 1332) /* __find_get_block_slow will mark the page accessed */
385fd4c59d8bf (Coywolf Qi Hunt 2005-11-07 00:59:39 -0800 1333) bh = __find_get_block_slow(bdev, block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1334) if (bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1335) bh_lru_install(bh);
2457aec63745e (Mel Gorman 2014-06-04 16:10:31 -0700 1336) } else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1337) touch_buffer(bh);
2457aec63745e (Mel Gorman 2014-06-04 16:10:31 -0700 1338)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1339) return bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1340) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1341) EXPORT_SYMBOL(__find_get_block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1342)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1343) /*
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1344) * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1345) * which corresponds to the passed block_device, block and size. The
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1346) * returned buffer has its reference count incremented.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1347) *
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1348) * __getblk_gfp() will lock up the machine if grow_dev_page's
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1349) * try_to_free_buffers() attempt is failing. FIXME, perhaps?
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1350) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1351) struct buffer_head *
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1352) __getblk_gfp(struct block_device *bdev, sector_t block,
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1353) unsigned size, gfp_t gfp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1354) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1355) struct buffer_head *bh = __find_get_block(bdev, block, size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1356)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1357) might_sleep();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1358) if (bh == NULL)
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1359) bh = __getblk_slow(bdev, block, size, gfp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1360) return bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1361) }
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1362) EXPORT_SYMBOL(__getblk_gfp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1363)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1364) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1365) * Do async read-ahead on a buffer..
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1366) */
3991d3bd15063 (Tomasz Kvarsin 2007-02-12 00:52:14 -0800 1367) void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1368) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1369) struct buffer_head *bh = __getblk(bdev, block, size);
a3e713b5fdd0e (Andrew Morton 2005-10-30 15:03:15 -0800 1370) if (likely(bh)) {
70246286e94c3 (Christoph Hellwig 2016-07-19 11:28:41 +0200 1371) ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
a3e713b5fdd0e (Andrew Morton 2005-10-30 15:03:15 -0800 1372) brelse(bh);
a3e713b5fdd0e (Andrew Morton 2005-10-30 15:03:15 -0800 1373) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1374) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1375) EXPORT_SYMBOL(__breadahead);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1376)
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1377) void __breadahead_gfp(struct block_device *bdev, sector_t block, unsigned size,
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1378) gfp_t gfp)
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1379) {
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1380) struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1381) if (likely(bh)) {
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1382) ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1383) brelse(bh);
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1384) }
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1385) }
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1386) EXPORT_SYMBOL(__breadahead_gfp);
d87f639258a6a (Roman Gushchin 2020-02-28 16:14:11 -0800 1387)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1388) /**
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1389) * __bread_gfp() - reads a specified block and returns the bh
67be2dd1bace0 (Martin Waitz 2005-05-01 08:59:26 -0700 1390) * @bdev: the block_device to read from
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1391) * @block: number of block
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1392) * @size: size (in bytes) to read
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1393) * @gfp: page allocation flag
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1394) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1395) * Reads a specified block, and returns buffer head that contains it.
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1396) * The page cache can be allocated from non-movable area
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1397) * not to prevent page migration if you set gfp to zero.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1398) * It returns NULL if the block was unreadable.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1399) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1400) struct buffer_head *
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1401) __bread_gfp(struct block_device *bdev, sector_t block,
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1402) unsigned size, gfp_t gfp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1403) {
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1404) struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1405)
a3e713b5fdd0e (Andrew Morton 2005-10-30 15:03:15 -0800 1406) if (likely(bh) && !buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1407) bh = __bread_slow(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1408) return bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1409) }
3b5e6454aaf6b (Gioh Kim 2014-09-04 22:04:42 -0400 1410) EXPORT_SYMBOL(__bread_gfp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1411)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1412) static void __invalidate_bh_lrus(struct bh_lru *b)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1413) {
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1414) int i;
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1415)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1416) for (i = 0; i < BH_LRU_SIZE; i++) {
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1417) brelse(b->bhs[i]);
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1418) b->bhs[i] = NULL;
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1419) }
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1420) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1421) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1422) * invalidate_bh_lrus() is called rarely - but not only at unmount.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1423) * This doesn't race because it runs in each cpu either in irq
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1424) * or with preempt disabled.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1425) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1426) static void invalidate_bh_lru(void *arg)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1427) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1428) struct bh_lru *b = &get_cpu_var(bh_lrus);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1429)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1430) __invalidate_bh_lrus(b);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1431) put_cpu_var(bh_lrus);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1432) }
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1433)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1434) bool has_bh_in_lru(int cpu, void *dummy)
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1435) {
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1436) struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1437) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1438)
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1439) for (i = 0; i < BH_LRU_SIZE; i++) {
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1440) if (b->bhs[i])
1d70667973363 (Saurav Girepunje 2019-11-30 17:49:15 -0800 1441) return true;
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1442) }
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1443)
1d70667973363 (Saurav Girepunje 2019-11-30 17:49:15 -0800 1444) return false;
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1445) }
42be35d0390b9 (Gilad Ben-Yossef 2012-03-28 14:42:45 -0700 1446)
f9a14399aea13 (Peter Zijlstra 2007-05-06 14:49:55 -0700 1447) void invalidate_bh_lrus(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1448) {
cb923159bbb8c (Sebastian Andrzej Siewior 2020-01-17 10:01:37 +0100 1449) on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1450) }
9db5579be4bb5 (Nicholas Piggin 2008-02-08 04:19:49 -0800 1451) EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1452)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1453) void invalidate_bh_lrus_cpu(int cpu)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1454) {
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1455) struct bh_lru *b;
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1456)
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1457) bh_lru_lock();
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1458) b = per_cpu_ptr(&bh_lrus, cpu);
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1459) __invalidate_bh_lrus(b);
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1460) bh_lru_unlock();
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1461) }
8cc621d2f45dd (Minchan Kim 2021-05-04 18:37:00 -0700 1462)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1463) void set_bh_page(struct buffer_head *bh,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1464) struct page *page, unsigned long offset)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1465) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1466) bh->b_page = page;
e827f92355e1e (Eric Sesterhenn 2006-03-26 18:24:46 +0200 1467) BUG_ON(offset >= PAGE_SIZE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1468) if (PageHighMem(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1469) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1470) * This catches illegal uses and preserves the offset:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1471) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1472) bh->b_data = (char *)(0 + offset);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1473) else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1474) bh->b_data = page_address(page) + offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1475) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1476) EXPORT_SYMBOL(set_bh_page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1477)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1478) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1479) * Called when truncating a buffer on a page completely.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1480) */
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1481)
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1482) /* Bits that are cleared during an invalidate */
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1483) #define BUFFER_FLAGS_DISCARD \
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1484) (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1485) 1 << BH_Delay | 1 << BH_Unwritten)
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1486)
858119e159384 (Arjan van de Ven 2006-01-14 13:20:43 -0800 1487) static void discard_buffer(struct buffer_head * bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1488) {
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1489) unsigned long b_state, b_state_old;
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1490)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1491) lock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1492) clear_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1493) bh->b_bdev = NULL;
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1494) b_state = bh->b_state;
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1495) for (;;) {
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1496) b_state_old = cmpxchg(&bh->b_state, b_state,
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1497) (b_state & ~BUFFER_FLAGS_DISCARD));
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1498) if (b_state_old == b_state)
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1499) break;
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1500) b_state = b_state_old;
e7470ee89f003 (Mel Gorman 2014-06-04 16:10:29 -0700 1501) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1502) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1503) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1504)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1505) /**
814e1d25a5966 (Wang Sheng-Hui 2011-09-01 08:22:57 +0800 1506) * block_invalidatepage - invalidate part or all of a buffer-backed page
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1507) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1508) * @page: the page which is affected
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1509) * @offset: start of the range to invalidate
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1510) * @length: length of the range to invalidate
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1511) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1512) * block_invalidatepage() is called when all or part of the page has become
814e1d25a5966 (Wang Sheng-Hui 2011-09-01 08:22:57 +0800 1513) * invalidated by a truncate operation.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1514) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1515) * block_invalidatepage() does not have to release all buffers, but it must
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1516) * ensure that no dirty buffer is left outside @offset and that no I/O
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1517) * is underway against any of the blocks which are outside the truncation
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1518) * point. Because the caller is about to free (and possibly reuse) those
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1519) * blocks on-disk.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1520) */
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1521) void block_invalidatepage(struct page *page, unsigned int offset,
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1522) unsigned int length)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1523) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1524) struct buffer_head *head, *bh, *next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1525) unsigned int curr_off = 0;
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1526) unsigned int stop = length + offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1527)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1528) BUG_ON(!PageLocked(page));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1529) if (!page_has_buffers(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1530) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1531)
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1532) /*
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1533) * Check for overflow
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1534) */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1535) BUG_ON(stop > PAGE_SIZE || stop < length);
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1536)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1537) head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1538) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1539) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1540) unsigned int next_off = curr_off + bh->b_size;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1541) next = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1542)
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1543) /*
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1544) * Are we still fully in range ?
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1545) */
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1546) if (next_off > stop)
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1547) goto out;
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1548)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1549) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1550) * is this block fully invalidated?
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1551) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1552) if (offset <= curr_off)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1553) discard_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1554) curr_off = next_off;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1555) bh = next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1556) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1557)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1558) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1559) * We release buffers only if the entire page is being invalidated.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1560) * The get_block cached value has been unconditionally invalidated,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1561) * so real IO is not possible anymore.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1562) */
3172485f4f803 (Jeff Moyer 2018-04-05 16:25:01 -0700 1563) if (length == PAGE_SIZE)
2ff28e22bdb87 (NeilBrown 2006-03-26 01:37:18 -0800 1564) try_to_release_page(page, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1565) out:
2ff28e22bdb87 (NeilBrown 2006-03-26 01:37:18 -0800 1566) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1567) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1568) EXPORT_SYMBOL(block_invalidatepage);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1569)
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 1570)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1571) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1572) * We attach and possibly dirty the buffers atomically wrt
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1573) * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1574) * is already excluded via the page lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1575) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1576) void create_empty_buffers(struct page *page,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1577) unsigned long blocksize, unsigned long b_state)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1578) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1579) struct buffer_head *bh, *head, *tail;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1580)
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 1581) head = alloc_page_buffers(page, blocksize, true);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1582) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1583) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1584) bh->b_state |= b_state;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1585) tail = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1586) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1587) } while (bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1588) tail->b_this_page = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1589)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1590) spin_lock(&page->mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1591) if (PageUptodate(page) || PageDirty(page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1592) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1593) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1594) if (PageDirty(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1595) set_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1596) if (PageUptodate(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1597) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1598) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1599) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1600) }
45dcfc27329f6 (Guoqing Jiang 2020-06-01 21:47:48 -0700 1601) attach_page_private(page, head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1602) spin_unlock(&page->mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1603) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1604) EXPORT_SYMBOL(create_empty_buffers);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1605)
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1606) /**
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1607) * clean_bdev_aliases: clean a range of buffers in block device
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1608) * @bdev: Block device to clean buffers in
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1609) * @block: Start of a range of blocks to clean
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1610) * @len: Number of blocks to clean
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1611) *
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1612) * We are taking a range of blocks for data and we don't want writeback of any
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1613) * buffer-cache aliases starting from return from this function and until the
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1614) * moment when something will explicitly mark the buffer dirty (hopefully that
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1615) * will not happen until we will free that block ;-) We don't even need to mark
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1616) * it not-uptodate - nobody can expect anything from a newly allocated buffer
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1617) * anyway. We used to use unmap_buffer() for such invalidation, but that was
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1618) * wrong. We definitely don't want to mark the alias unmapped, for example - it
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1619) * would confuse anyone who might pick it with bread() afterwards...
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1620) *
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1621) * Also.. Note that bforget() doesn't lock the buffer. So there can be
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1622) * writeout I/O going on against recently-freed buffers. We don't wait on that
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1623) * I/O in bforget() - it's more efficient to wait on the I/O only if we really
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1624) * need to. That happens here.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1625) */
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1626) void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1627) {
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1628) struct inode *bd_inode = bdev->bd_inode;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1629) struct address_space *bd_mapping = bd_inode->i_mapping;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1630) struct pagevec pvec;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1631) pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1632) pgoff_t end;
c10f778ddfc16 (Jan Kara 2017-09-06 16:21:24 -0700 1633) int i, count;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1634) struct buffer_head *bh;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1635) struct buffer_head *head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1636)
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1637) end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
8667982014d60 (Mel Gorman 2017-11-15 17:37:52 -0800 1638) pagevec_init(&pvec);
397162ffa2ed1 (Jan Kara 2017-09-06 16:21:43 -0700 1639) while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
c10f778ddfc16 (Jan Kara 2017-09-06 16:21:24 -0700 1640) count = pagevec_count(&pvec);
c10f778ddfc16 (Jan Kara 2017-09-06 16:21:24 -0700 1641) for (i = 0; i < count; i++) {
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1642) struct page *page = pvec.pages[i];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1643)
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1644) if (!page_has_buffers(page))
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1645) continue;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1646) /*
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1647) * We use page lock instead of bd_mapping->private_lock
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1648) * to pin buffers here since we can afford to sleep and
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1649) * it scales better than a global spinlock lock.
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1650) */
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1651) lock_page(page);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1652) /* Recheck when the page is locked which pins bhs */
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1653) if (!page_has_buffers(page))
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1654) goto unlock_page;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1655) head = page_buffers(page);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1656) bh = head;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1657) do {
6c006a9d94bfb (Chandan Rajendra 2016-12-25 19:01:03 +0530 1658) if (!buffer_mapped(bh) || (bh->b_blocknr < block))
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1659) goto next;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1660) if (bh->b_blocknr >= block + len)
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1661) break;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1662) clear_buffer_dirty(bh);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1663) wait_on_buffer(bh);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1664) clear_buffer_req(bh);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1665) next:
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1666) bh = bh->b_this_page;
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1667) } while (bh != head);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1668) unlock_page:
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1669) unlock_page(page);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1670) }
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1671) pagevec_release(&pvec);
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1672) cond_resched();
c10f778ddfc16 (Jan Kara 2017-09-06 16:21:24 -0700 1673) /* End of range already reached? */
c10f778ddfc16 (Jan Kara 2017-09-06 16:21:24 -0700 1674) if (index > end || !index)
c10f778ddfc16 (Jan Kara 2017-09-06 16:21:24 -0700 1675) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1676) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1677) }
29f3ad7d83803 (Jan Kara 2016-11-04 18:08:11 +0100 1678) EXPORT_SYMBOL(clean_bdev_aliases);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1679)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1680) /*
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1681) * Size is a power-of-two in the range 512..PAGE_SIZE,
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1682) * and the case we care about most is PAGE_SIZE.
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1683) *
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1684) * So this *could* possibly be written with those
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1685) * constraints in mind (relevant mostly if some
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1686) * architecture has a slow bit-scan instruction)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1687) */
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1688) static inline int block_size_bits(unsigned int blocksize)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1689) {
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1690) return ilog2(blocksize);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1691) }
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1692)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1693) static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1694) {
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1695) BUG_ON(!PageLocked(page));
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1696)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1697) if (!page_has_buffers(page))
6aa7de059173a (Mark Rutland 2017-10-23 14:07:29 -0700 1698) create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
6aa7de059173a (Mark Rutland 2017-10-23 14:07:29 -0700 1699) b_state);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1700) return page_buffers(page);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1701) }
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1702)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1703) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1704) * NOTE! All mapped/uptodate combinations are valid:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1705) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1706) * Mapped Uptodate Meaning
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1707) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1708) * No No "unknown" - must do get_block()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1709) * No Yes "hole" - zero-filled
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1710) * Yes No "allocated" - allocated on disk, not read in
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1711) * Yes Yes "valid" - allocated and up-to-date in memory.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1712) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1713) * "Dirty" is valid only with the last case (mapped+uptodate).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1714) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1715)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1716) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1717) * While block_write_full_page is writing back the dirty buffers under
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1718) * the page lock, whoever dirtied the buffers may decide to clean them
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1719) * again at any time. We handle that by only looking at the buffer
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1720) * state inside lock_buffer().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1721) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1722) * If block_write_full_page() is called for regular writeback
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1723) * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1724) * locked buffer. This only can happen if someone has written the buffer
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1725) * directly, with submit_bh(). At the address_space level PageWriteback
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1726) * prevents this contention from occurring.
6e34eeddf7dee (Theodore Ts'o 2009-04-07 18:12:43 -0400 1727) *
6e34eeddf7dee (Theodore Ts'o 2009-04-07 18:12:43 -0400 1728) * If block_write_full_page() is called with wbc->sync_mode ==
70fd76140a6cb (Christoph Hellwig 2016-11-01 07:40:10 -0600 1729) * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
721a9602e6607 (Jens Axboe 2011-03-09 11:56:30 +0100 1730) * causes the writes to be flagged as synchronous writes.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1731) */
b4bba38909c21 (Benjamin Marzinski 2016-06-27 09:58:40 -0500 1732) int __block_write_full_page(struct inode *inode, struct page *page,
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 1733) get_block_t *get_block, struct writeback_control *wbc,
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 1734) bh_end_io_t *handler)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1735) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1736) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1737) sector_t block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1738) sector_t last_block;
f0fbd5fc09b20 (Andrew Morton 2005-05-05 16:15:48 -0700 1739) struct buffer_head *bh, *head;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1740) unsigned int blocksize, bbits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1741) int nr_underway = 0;
7637241e651ec (Jens Axboe 2016-11-01 10:00:38 -0600 1742) int write_flags = wbc_to_write_flags(wbc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1743)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1744) head = create_page_buffers(page, inode,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1745) (1 << BH_Dirty)|(1 << BH_Uptodate));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1746)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1747) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1748) * Be very careful. We have no exclusion from __set_page_dirty_buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1749) * here, and the (potentially unmapped) buffers may become dirty at
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1750) * any time. If a buffer becomes dirty here after we've inspected it
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1751) * then we just miss that fact, and the page stays dirty.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1752) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1753) * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1754) * handle that here by just cleaning them.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1755) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1756)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1757) bh = head;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1758) blocksize = bh->b_size;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1759) bbits = block_size_bits(blocksize);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1760)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1761) block = (sector_t)page->index << (PAGE_SHIFT - bbits);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 1762) last_block = (i_size_read(inode) - 1) >> bbits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1763)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1764) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1765) * Get all the dirty buffers mapped to disk addresses and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1766) * handle any aliases from the underlying blockdev's mapping.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1767) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1768) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1769) if (block > last_block) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1770) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1771) * mapped buffers outside i_size will occur, because
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1772) * this page can be outside i_size when there is a
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1773) * truncate in progress.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1774) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1775) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1776) * The buffer was zeroed by block_write_full_page()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1777) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1778) clear_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1779) set_buffer_uptodate(bh);
29a814d2ee0e4 (Alex Tomas 2008-07-11 19:27:31 -0400 1780) } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
29a814d2ee0e4 (Alex Tomas 2008-07-11 19:27:31 -0400 1781) buffer_dirty(bh)) {
b0cf2321c6599 (Badari Pulavarty 2006-03-26 01:38:00 -0800 1782) WARN_ON(bh->b_size != blocksize);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1783) err = get_block(inode, block, bh, 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1784) if (err)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1785) goto recover;
29a814d2ee0e4 (Alex Tomas 2008-07-11 19:27:31 -0400 1786) clear_buffer_delay(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1787) if (buffer_new(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1788) /* blockdev mappings never come here */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1789) clear_buffer_new(bh);
e64855c6cfaa0 (Jan Kara 2016-11-04 18:08:15 +0100 1790) clean_bdev_bh_alias(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1791) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1792) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1793) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1794) block++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1795) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1796)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1797) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1798) if (!buffer_mapped(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1799) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1800) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1801) * If it's a fully non-blocking write attempt and we cannot
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1802) * lock the buffer then redirty the page. Note that this can
5b0830cb9085f (Jens Axboe 2009-09-23 19:37:09 +0200 1803) * potentially cause a busy-wait loop from writeback threads
5b0830cb9085f (Jens Axboe 2009-09-23 19:37:09 +0200 1804) * and kswapd activity, but those code paths have their own
5b0830cb9085f (Jens Axboe 2009-09-23 19:37:09 +0200 1805) * higher-level throttling.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1806) */
1b430beee5e38 (Wu Fengguang 2010-10-26 14:21:26 -0700 1807) if (wbc->sync_mode != WB_SYNC_NONE) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1808) lock_buffer(bh);
ca5de404ff036 (Nicholas Piggin 2008-08-02 12:02:13 +0200 1809) } else if (!trylock_buffer(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1810) redirty_page_for_writepage(wbc, page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1811) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1812) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1813) if (test_clear_buffer_dirty(bh)) {
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 1814) mark_buffer_async_write_endio(bh, handler);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1815) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1816) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1817) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1818) } while ((bh = bh->b_this_page) != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1819)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1820) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1821) * The page and its buffers are protected by PageWriteback(), so we can
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1822) * drop the bh refcounts early.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1823) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1824) BUG_ON(PageWriteback(page));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1825) set_page_writeback(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1826)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1827) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1828) struct buffer_head *next = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1829) if (buffer_async_write(bh)) {
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 1830) submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 1831) inode->i_write_hint, wbc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1832) nr_underway++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1833) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1834) bh = next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1835) } while (bh != head);
05937baae9fc2 (Andrew Morton 2005-05-05 16:15:47 -0700 1836) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1837)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1838) err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1839) done:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1840) if (nr_underway == 0) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1841) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1842) * The page was marked dirty, but the buffers were
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1843) * clean. Someone wrote them back by hand with
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1844) * ll_rw_block/submit_bh. A rare case.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1845) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1846) end_page_writeback(page);
3d67f2d7c0fb2 (Nicholas Piggin 2007-05-06 14:49:05 -0700 1847)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1848) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1849) * The page and buffer_heads can be released at any time from
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1850) * here on.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1851) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1852) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1853) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1854)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1855) recover:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1856) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1857) * ENOSPC, or some other error. We may already have added some
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1858) * blocks to the file, so we need to write these out to avoid
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1859) * exposing stale data.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1860) * The page is currently locked and not marked for writeback
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1861) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1862) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1863) /* Recovery: lock and submit the mapped buffers */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1864) do {
29a814d2ee0e4 (Alex Tomas 2008-07-11 19:27:31 -0400 1865) if (buffer_mapped(bh) && buffer_dirty(bh) &&
29a814d2ee0e4 (Alex Tomas 2008-07-11 19:27:31 -0400 1866) !buffer_delay(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1867) lock_buffer(bh);
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 1868) mark_buffer_async_write_endio(bh, handler);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1869) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1870) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1871) * The buffer may have been set dirty during
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1872) * attachment to a dirty page.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1873) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1874) clear_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1875) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1876) } while ((bh = bh->b_this_page) != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1877) SetPageError(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1878) BUG_ON(PageWriteback(page));
7e4c3690b07f0 (Andrew Morton 2007-05-08 00:23:27 -0700 1879) mapping_set_error(page->mapping, err);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1880) set_page_writeback(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1881) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1882) struct buffer_head *next = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1883) if (buffer_async_write(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1884) clear_buffer_dirty(bh);
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 1885) submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 1886) inode->i_write_hint, wbc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1887) nr_underway++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1888) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1889) bh = next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1890) } while (bh != head);
ffda9d302267d (Nicholas Piggin 2007-02-20 13:57:54 -0800 1891) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1892) goto done;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1893) }
b4bba38909c21 (Benjamin Marzinski 2016-06-27 09:58:40 -0500 1894) EXPORT_SYMBOL(__block_write_full_page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1895)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1896) /*
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1897) * If a page has any new buffers, zero them out here, and mark them uptodate
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1898) * and dirty so they'll be written out (in order to prevent uninitialised
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1899) * block data from leaking). And clear the new bit.
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1900) */
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1901) void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1902) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1903) unsigned int block_start, block_end;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1904) struct buffer_head *head, *bh;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1905)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1906) BUG_ON(!PageLocked(page));
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1907) if (!page_has_buffers(page))
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1908) return;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1909)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1910) bh = head = page_buffers(page);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1911) block_start = 0;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1912) do {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1913) block_end = block_start + bh->b_size;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1914)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1915) if (buffer_new(bh)) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1916) if (block_end > from && block_start < to) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1917) if (!PageUptodate(page)) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1918) unsigned start, size;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1919)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1920) start = max(from, block_start);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1921) size = min(to, block_end) - start;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1922)
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 1923) zero_user(page, start, size);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1924) set_buffer_uptodate(bh);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1925) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1926)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1927) clear_buffer_new(bh);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1928) mark_buffer_dirty(bh);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1929) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1930) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1931)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1932) block_start = block_end;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1933) bh = bh->b_this_page;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1934) } while (bh != head);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1935) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1936) EXPORT_SYMBOL(page_zero_new_buffers);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 1937)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1938) static void
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1939) iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1940) struct iomap *iomap)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1941) {
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1942) loff_t offset = block << inode->i_blkbits;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1943)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1944) bh->b_bdev = iomap->bdev;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1945)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1946) /*
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1947) * Block points to offset in file we need to map, iomap contains
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1948) * the offset at which the map starts. If the map ends before the
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1949) * current block, then do not map the buffer and let the caller
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1950) * handle it.
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1951) */
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1952) BUG_ON(offset >= iomap->offset + iomap->length);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1953)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1954) switch (iomap->type) {
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1955) case IOMAP_HOLE:
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1956) /*
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1957) * If the buffer is not up to date or beyond the current EOF,
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1958) * we need to mark it as new to ensure sub-block zeroing is
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1959) * executed if necessary.
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1960) */
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1961) if (!buffer_uptodate(bh) ||
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1962) (offset >= i_size_read(inode)))
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1963) set_buffer_new(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1964) break;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1965) case IOMAP_DELALLOC:
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1966) if (!buffer_uptodate(bh) ||
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1967) (offset >= i_size_read(inode)))
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1968) set_buffer_new(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1969) set_buffer_uptodate(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1970) set_buffer_mapped(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1971) set_buffer_delay(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1972) break;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1973) case IOMAP_UNWRITTEN:
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1974) /*
3d7b6b21f6c59 (Andreas Gruenbacher 2018-06-19 15:10:55 -0700 1975) * For unwritten regions, we always need to ensure that regions
3d7b6b21f6c59 (Andreas Gruenbacher 2018-06-19 15:10:55 -0700 1976) * in the block we are not writing to are zeroed. Mark the
3d7b6b21f6c59 (Andreas Gruenbacher 2018-06-19 15:10:55 -0700 1977) * buffer as new to ensure this.
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1978) */
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1979) set_buffer_new(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1980) set_buffer_unwritten(bh);
df561f6688fef (Gustavo A. R. Silva 2020-08-23 17:36:59 -0500 1981) fallthrough;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1982) case IOMAP_MAPPED:
3d7b6b21f6c59 (Andreas Gruenbacher 2018-06-19 15:10:55 -0700 1983) if ((iomap->flags & IOMAP_F_NEW) ||
3d7b6b21f6c59 (Andreas Gruenbacher 2018-06-19 15:10:55 -0700 1984) offset >= i_size_read(inode))
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1985) set_buffer_new(bh);
19fe5f643f89f (Andreas Gruenbacher 2017-10-01 17:55:54 -0400 1986) bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
19fe5f643f89f (Andreas Gruenbacher 2017-10-01 17:55:54 -0400 1987) inode->i_blkbits;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1988) set_buffer_mapped(bh);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1989) break;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1990) }
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1991) }
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1992)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1993) int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 1994) get_block_t *get_block, struct iomap *iomap)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1995) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1996) unsigned from = pos & (PAGE_SIZE - 1);
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 1997) unsigned to = from + len;
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 1998) struct inode *inode = page->mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1999) unsigned block_start, block_end;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2000) sector_t block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2001) int err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2002) unsigned blocksize, bbits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2003) struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2004)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2005) BUG_ON(!PageLocked(page));
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2006) BUG_ON(from > PAGE_SIZE);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2007) BUG_ON(to > PAGE_SIZE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2008) BUG_ON(from > to);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2009)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2010) head = create_page_buffers(page, inode, 0);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2011) blocksize = head->b_size;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2012) bbits = block_size_bits(blocksize);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2013)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2014) block = (sector_t)page->index << (PAGE_SHIFT - bbits);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2015)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2016) for(bh = head, block_start = 0; bh != head || !block_start;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2017) block++, block_start=block_end, bh = bh->b_this_page) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2018) block_end = block_start + blocksize;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2019) if (block_end <= from || block_start >= to) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2020) if (PageUptodate(page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2021) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2022) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2023) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2024) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2025) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2026) if (buffer_new(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2027) clear_buffer_new(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2028) if (!buffer_mapped(bh)) {
b0cf2321c6599 (Badari Pulavarty 2006-03-26 01:38:00 -0800 2029) WARN_ON(bh->b_size != blocksize);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2030) if (get_block) {
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2031) err = get_block(inode, block, bh, 1);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2032) if (err)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2033) break;
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2034) } else {
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2035) iomap_to_bh(inode, block, bh, iomap);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2036) }
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2037)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2038) if (buffer_new(bh)) {
e64855c6cfaa0 (Jan Kara 2016-11-04 18:08:15 +0100 2039) clean_bdev_bh_alias(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2040) if (PageUptodate(page)) {
637aff46f94a7 (Nicholas Piggin 2007-10-16 01:25:00 -0700 2041) clear_buffer_new(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2042) set_buffer_uptodate(bh);
637aff46f94a7 (Nicholas Piggin 2007-10-16 01:25:00 -0700 2043) mark_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2044) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2045) }
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2046) if (block_end > to || block_start < from)
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2047) zero_user_segments(page,
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2048) to, block_end,
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2049) block_start, from);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2050) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2051) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2052) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2053) if (PageUptodate(page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2054) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2055) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2056) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2057) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2058) if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
33a266dda9fbb (David Chinner 2007-02-12 00:51:41 -0800 2059) !buffer_unwritten(bh) &&
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2060) (block_start < from || block_end > to)) {
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 2061) ll_rw_block(REQ_OP_READ, 0, 1, &bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2062) *wait_bh++=bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2063) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2064) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2065) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2066) * If we issued read requests - let them complete.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2067) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2068) while(wait_bh > wait) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2069) wait_on_buffer(*--wait_bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2070) if (!buffer_uptodate(*wait_bh))
f3ddbdc6267c3 (Nicholas Piggin 2005-05-05 16:15:45 -0700 2071) err = -EIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2072) }
f9f07b6c1372b (Jan Kara 2011-06-14 00:58:27 +0200 2073) if (unlikely(err))
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2074) page_zero_new_buffers(page, from, to);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2075) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2076) }
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2077)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2078) int __block_write_begin(struct page *page, loff_t pos, unsigned len,
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2079) get_block_t *get_block)
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2080) {
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2081) return __block_write_begin_int(page, pos, len, get_block, NULL);
ae259a9c8593f (Christoph Hellwig 2016-06-21 09:23:11 +1000 2082) }
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 2083) EXPORT_SYMBOL(__block_write_begin);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2084)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2085) static int __block_commit_write(struct inode *inode, struct page *page,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2086) unsigned from, unsigned to)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2087) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2088) unsigned block_start, block_end;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2089) int partial = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2090) unsigned blocksize;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2091) struct buffer_head *bh, *head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2092)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2093) bh = head = page_buffers(page);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2094) blocksize = bh->b_size;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2095)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2096) block_start = 0;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2097) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2098) block_end = block_start + blocksize;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2099) if (block_end <= from || block_start >= to) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2100) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2101) partial = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2102) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2103) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2104) mark_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2105) }
4ebd3aec38426 (Yang Guo 2021-02-24 12:02:48 -0800 2106) if (buffer_new(bh))
4ebd3aec38426 (Yang Guo 2021-02-24 12:02:48 -0800 2107) clear_buffer_new(bh);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2108)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2109) block_start = block_end;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2110) bh = bh->b_this_page;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2111) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2112)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2113) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2114) * If this is a partial write which happened to make all buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2115) * uptodate then we can optimize away a bogus readpage() for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2116) * the next read(). Here we 'discover' whether the page went
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2117) * uptodate as a result of this (potentially partial) write.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2118) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2119) if (!partial)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2120) SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2121) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2122) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2123)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2124) /*
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2125) * block_write_begin takes care of the basic task of block allocation and
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2126) * bringing partial write blocks uptodate first.
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2127) *
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 2128) * The filesystem needs to handle block truncation upon failure.
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2129) */
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2130) int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2131) unsigned flags, struct page **pagep, get_block_t *get_block)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2132) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2133) pgoff_t index = pos >> PAGE_SHIFT;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2134) struct page *page;
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2135) int status;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2136)
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2137) page = grab_cache_page_write_begin(mapping, index, flags);
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2138) if (!page)
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2139) return -ENOMEM;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2140)
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2141) status = __block_write_begin(page, pos, len, get_block);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2142) if (unlikely(status)) {
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2143) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2144) put_page(page);
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2145) page = NULL;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2146) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2147)
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2148) *pagep = page;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2149) return status;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2150) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2151) EXPORT_SYMBOL(block_write_begin);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2152)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2153) int block_write_end(struct file *file, struct address_space *mapping,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2154) loff_t pos, unsigned len, unsigned copied,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2155) struct page *page, void *fsdata)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2156) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2157) struct inode *inode = mapping->host;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2158) unsigned start;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2159)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2160) start = pos & (PAGE_SIZE - 1);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2161)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2162) if (unlikely(copied < len)) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2163) /*
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2164) * The buffers that were written will now be uptodate, so we
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2165) * don't have to worry about a readpage reading them and
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2166) * overwriting a partial write. However if we have encountered
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2167) * a short write and only partially written into a buffer, it
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2168) * will not be marked uptodate, so a readpage might come in and
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2169) * destroy our partial write.
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2170) *
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2171) * Do the simplest thing, and just treat any short write to a
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2172) * non uptodate page as a zero-length write, and force the
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2173) * caller to redo the whole thing.
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2174) */
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2175) if (!PageUptodate(page))
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2176) copied = 0;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2177)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2178) page_zero_new_buffers(page, start+copied, start+len);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2179) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2180) flush_dcache_page(page);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2181)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2182) /* This could be a short (even 0-length) commit */
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2183) __block_commit_write(inode, page, start, start+copied);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2184)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2185) return copied;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2186) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2187) EXPORT_SYMBOL(block_write_end);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2188)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2189) int generic_write_end(struct file *file, struct address_space *mapping,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2190) loff_t pos, unsigned len, unsigned copied,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2191) struct page *page, void *fsdata)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2192) {
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2193) struct inode *inode = mapping->host;
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2194) loff_t old_size = inode->i_size;
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2195) bool i_size_changed = false;
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2196)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2197) copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2198)
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2199) /*
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2200) * No need to use i_size_read() here, the i_size cannot change under us
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2201) * because we hold i_rwsem.
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2202) *
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2203) * But it's important to update i_size while still holding page lock:
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2204) * page writeout could otherwise come in and zero beyond i_size.
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2205) */
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2206) if (pos + copied > inode->i_size) {
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2207) i_size_write(inode, pos + copied);
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2208) i_size_changed = true;
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2209) }
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2210)
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2211) unlock_page(page);
7a77dad7e3be1 (Andreas Gruenbacher 2019-04-30 08:45:34 -0700 2212) put_page(page);
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2213)
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2214) if (old_size < pos)
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2215) pagecache_isize_extended(inode, old_size, pos);
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2216) /*
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2217) * Don't mark the inode dirty under page lock. First, it unnecessarily
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2218) * makes the holding time of page lock longer. Second, it forces lock
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2219) * ordering of page lock and transaction start for journaling
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2220) * filesystems.
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2221) */
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2222) if (i_size_changed)
8af54f291e5cf (Christoph Hellwig 2019-06-27 17:28:40 -0700 2223) mark_inode_dirty(inode);
26ddb1f4fd884 (Andreas Gruenbacher 2019-04-30 08:45:33 -0700 2224) return copied;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2225) }
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2226) EXPORT_SYMBOL(generic_write_end);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 2227)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2228) /*
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2229) * block_is_partially_uptodate checks whether buffers within a page are
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2230) * uptodate or not.
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2231) *
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2232) * Returns true if all buffers which correspond to a file portion
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2233) * we want to read are uptodate.
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2234) */
c186afb4dbd00 (Al Viro 2014-02-02 21:16:54 -0500 2235) int block_is_partially_uptodate(struct page *page, unsigned long from,
c186afb4dbd00 (Al Viro 2014-02-02 21:16:54 -0500 2236) unsigned long count)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2237) {
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2238) unsigned block_start, block_end, blocksize;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2239) unsigned to;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2240) struct buffer_head *bh, *head;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2241) int ret = 1;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2242)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2243) if (!page_has_buffers(page))
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2244) return 0;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2245)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2246) head = page_buffers(page);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2247) blocksize = head->b_size;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2248) to = min_t(unsigned, PAGE_SIZE - from, count);
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2249) to = from + to;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2250) if (from < blocksize && to > PAGE_SIZE - blocksize)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2251) return 0;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2252)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2253) bh = head;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2254) block_start = 0;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2255) do {
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2256) block_end = block_start + blocksize;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2257) if (block_end > from && block_start < to) {
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2258) if (!buffer_uptodate(bh)) {
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2259) ret = 0;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2260) break;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2261) }
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2262) if (block_end >= to)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2263) break;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2264) }
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2265) block_start = block_end;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2266) bh = bh->b_this_page;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2267) } while (bh != head);
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2268)
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2269) return ret;
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2270) }
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2271) EXPORT_SYMBOL(block_is_partially_uptodate);
8ab22b9abb5c5 (Hisashi Hifumi 2008-07-28 15:46:36 -0700 2272)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2273) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2274) * Generic "read page" function for block devices that have the normal
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2275) * get_block functionality. This is most of the block device filesystems.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2276) * Reads the page asynchronously --- the unlock_buffer() and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2277) * set/clear_buffer_uptodate() functions propagate buffer state into the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2278) * page struct once IO has completed.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2279) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2280) int block_read_full_page(struct page *page, get_block_t *get_block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2281) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2282) struct inode *inode = page->mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2283) sector_t iblock, lblock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2284) struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2285) unsigned int blocksize, bbits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2286) int nr, i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2287) int fully_mapped = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2288)
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2289) head = create_page_buffers(page, inode, 0);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2290) blocksize = head->b_size;
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2291) bbits = block_size_bits(blocksize);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2292)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2293) iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
45bce8f3e3436 (Linus Torvalds 2012-11-29 10:21:43 -0800 2294) lblock = (i_size_read(inode)+blocksize-1) >> bbits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2295) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2296) nr = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2297) i = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2298)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2299) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2300) if (buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2301) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2302)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2303) if (!buffer_mapped(bh)) {
c64610ba585fa (Andrew Morton 2005-05-16 21:53:49 -0700 2304) int err = 0;
c64610ba585fa (Andrew Morton 2005-05-16 21:53:49 -0700 2305)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2306) fully_mapped = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2307) if (iblock < lblock) {
b0cf2321c6599 (Badari Pulavarty 2006-03-26 01:38:00 -0800 2308) WARN_ON(bh->b_size != blocksize);
c64610ba585fa (Andrew Morton 2005-05-16 21:53:49 -0700 2309) err = get_block(inode, iblock, bh, 0);
c64610ba585fa (Andrew Morton 2005-05-16 21:53:49 -0700 2310) if (err)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2311) SetPageError(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2312) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2313) if (!buffer_mapped(bh)) {
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2314) zero_user(page, i * blocksize, blocksize);
c64610ba585fa (Andrew Morton 2005-05-16 21:53:49 -0700 2315) if (!err)
c64610ba585fa (Andrew Morton 2005-05-16 21:53:49 -0700 2316) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2317) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2318) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2319) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2320) * get_block() might have updated the buffer
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2321) * synchronously
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2322) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2323) if (buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2324) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2325) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2326) arr[nr++] = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2327) } while (i++, iblock++, (bh = bh->b_this_page) != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2328)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2329) if (fully_mapped)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2330) SetPageMappedToDisk(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2331)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2332) if (!nr) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2333) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2334) * All buffers are uptodate - we can set the page uptodate
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2335) * as well. But not if get_block() returned an error.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2336) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2337) if (!PageError(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2338) SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2339) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2340) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2341) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2342)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2343) /* Stage two: lock the buffers */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2344) for (i = 0; i < nr; i++) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2345) bh = arr[i];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2346) lock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2347) mark_buffer_async_read(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2348) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2349)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2350) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2351) * Stage 3: start the IO. Check for uptodateness
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2352) * inside the buffer lock in case another process reading
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2353) * the underlying blockdev brought it uptodate (the sct fix).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2354) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2355) for (i = 0; i < nr; i++) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2356) bh = arr[i];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2357) if (buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2358) end_buffer_async_read(bh, 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2359) else
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 2360) submit_bh(REQ_OP_READ, 0, bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2361) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2362) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2363) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 2364) EXPORT_SYMBOL(block_read_full_page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2365)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2366) /* utility function for filesystems that need to do work on expanding
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2367) * truncates. Uses filesystem pagecache writes to allow the filesystem to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2368) * deal with the hole.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2369) */
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2370) int generic_cont_expand_simple(struct inode *inode, loff_t size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2371) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2372) struct address_space *mapping = inode->i_mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2373) struct page *page;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2374) void *fsdata;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2375) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2376)
c08d3b0e33edc (Nicholas Piggin 2009-08-21 02:35:06 +1000 2377) err = inode_newsize_ok(inode, size);
c08d3b0e33edc (Nicholas Piggin 2009-08-21 02:35:06 +1000 2378) if (err)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2379) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2380)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2381) err = pagecache_write_begin(NULL, mapping, size, 0,
c718a97514e4d (Tetsuo Handa 2017-05-08 15:58:59 -0700 2382) AOP_FLAG_CONT_EXPAND, &page, &fsdata);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2383) if (err)
05eb0b51fb464 (OGAWA Hirofumi 2006-01-08 01:02:13 -0800 2384) goto out;
05eb0b51fb464 (OGAWA Hirofumi 2006-01-08 01:02:13 -0800 2385)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2386) err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2387) BUG_ON(err > 0);
05eb0b51fb464 (OGAWA Hirofumi 2006-01-08 01:02:13 -0800 2388)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2389) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2390) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2391) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 2392) EXPORT_SYMBOL(generic_cont_expand_simple);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2393)
f1e3af72c10ba (Adrian Bunk 2008-04-29 00:59:01 -0700 2394) static int cont_expand_zero(struct file *file, struct address_space *mapping,
f1e3af72c10ba (Adrian Bunk 2008-04-29 00:59:01 -0700 2395) loff_t pos, loff_t *bytes)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2396) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2397) struct inode *inode = mapping->host;
93407472a21b8 (Fabian Frederick 2017-02-27 14:28:32 -0800 2398) unsigned int blocksize = i_blocksize(inode);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2399) struct page *page;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2400) void *fsdata;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2401) pgoff_t index, curidx;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2402) loff_t curpos;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2403) unsigned zerofrom, offset, len;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2404) int err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2405)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2406) index = pos >> PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2407) offset = pos & ~PAGE_MASK;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2408)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2409) while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2410) zerofrom = curpos & ~PAGE_MASK;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2411) if (zerofrom & (blocksize-1)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2412) *bytes |= (blocksize-1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2413) (*bytes)++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2414) }
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2415) len = PAGE_SIZE - zerofrom;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2416)
c718a97514e4d (Tetsuo Handa 2017-05-08 15:58:59 -0700 2417) err = pagecache_write_begin(file, mapping, curpos, len, 0,
c718a97514e4d (Tetsuo Handa 2017-05-08 15:58:59 -0700 2418) &page, &fsdata);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2419) if (err)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2420) goto out;
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2421) zero_user(page, zerofrom, len);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2422) err = pagecache_write_end(file, mapping, curpos, len, len,
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2423) page, fsdata);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2424) if (err < 0)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2425) goto out;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2426) BUG_ON(err != len);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2427) err = 0;
061e97469f46f (OGAWA Hirofumi 2008-04-28 02:16:28 -0700 2428)
061e97469f46f (OGAWA Hirofumi 2008-04-28 02:16:28 -0700 2429) balance_dirty_pages_ratelimited(mapping);
c2ca0fcd20286 (Mikulas Patocka 2014-07-27 13:00:41 -0400 2430)
08d405c8b845a (Davidlohr Bueso 2019-01-03 15:28:58 -0800 2431) if (fatal_signal_pending(current)) {
c2ca0fcd20286 (Mikulas Patocka 2014-07-27 13:00:41 -0400 2432) err = -EINTR;
c2ca0fcd20286 (Mikulas Patocka 2014-07-27 13:00:41 -0400 2433) goto out;
c2ca0fcd20286 (Mikulas Patocka 2014-07-27 13:00:41 -0400 2434) }
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2435) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2436)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2437) /* page covers the boundary, find the boundary offset */
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2438) if (index == curidx) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2439) zerofrom = curpos & ~PAGE_MASK;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2440) /* if we will expand the thing last block will be filled */
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2441) if (offset <= zerofrom) {
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2442) goto out;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2443) }
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2444) if (zerofrom & (blocksize-1)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2445) *bytes |= (blocksize-1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2446) (*bytes)++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2447) }
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2448) len = offset - zerofrom;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2449)
c718a97514e4d (Tetsuo Handa 2017-05-08 15:58:59 -0700 2450) err = pagecache_write_begin(file, mapping, curpos, len, 0,
c718a97514e4d (Tetsuo Handa 2017-05-08 15:58:59 -0700 2451) &page, &fsdata);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2452) if (err)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2453) goto out;
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2454) zero_user(page, zerofrom, len);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2455) err = pagecache_write_end(file, mapping, curpos, len, len,
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2456) page, fsdata);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2457) if (err < 0)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2458) goto out;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2459) BUG_ON(err != len);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2460) err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2461) }
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2462) out:
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2463) return err;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2464) }
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2465)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2466) /*
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2467) * For moronic filesystems that do not allow holes in file.
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2468) * We may have to extend the file.
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2469) */
282dc17884988 (Christoph Hellwig 2010-06-04 11:29:55 +0200 2470) int cont_write_begin(struct file *file, struct address_space *mapping,
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2471) loff_t pos, unsigned len, unsigned flags,
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2472) struct page **pagep, void **fsdata,
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2473) get_block_t *get_block, loff_t *bytes)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2474) {
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2475) struct inode *inode = mapping->host;
93407472a21b8 (Fabian Frederick 2017-02-27 14:28:32 -0800 2476) unsigned int blocksize = i_blocksize(inode);
93407472a21b8 (Fabian Frederick 2017-02-27 14:28:32 -0800 2477) unsigned int zerofrom;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2478) int err;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2479)
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2480) err = cont_expand_zero(file, mapping, pos, bytes);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2481) if (err)
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2482) return err;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2483)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2484) zerofrom = *bytes & ~PAGE_MASK;
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2485) if (pos+len > *bytes && zerofrom & (blocksize-1)) {
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2486) *bytes |= (blocksize-1);
89e107877b65b (Nicholas Piggin 2007-10-16 01:25:07 -0700 2487) (*bytes)++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2488) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2489)
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 2490) return block_write_begin(mapping, pos, len, flags, pagep, get_block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2491) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 2492) EXPORT_SYMBOL(cont_write_begin);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2493)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2494) int block_commit_write(struct page *page, unsigned from, unsigned to)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2495) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2496) struct inode *inode = page->mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2497) __block_commit_write(inode,page,from,to);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2498) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2499) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 2500) EXPORT_SYMBOL(block_commit_write);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2501)
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2502) /*
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2503) * block_page_mkwrite() is not allowed to change the file size as it gets
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2504) * called from a page fault handler when a page is first dirtied. Hence we must
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2505) * be careful to check for EOF conditions here. We set the page up correctly
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2506) * for a written page which means we get ENOSPC checking when writing into
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2507) * holes and correct delalloc and unwritten extent mapping on filesystems that
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2508) * support these features.
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2509) *
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2510) * We are not allowed to take the i_mutex here so we have to play games to
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2511) * protect against truncate races as the page could now be beyond EOF. Because
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 2512) * truncate writes the inode size before removing pages, once we have the
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2513) * page lock we can determine safely if the page is beyond EOF. If it is not
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2514) * beyond EOF, then the page is guaranteed safe against truncation until we
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2515) * unlock the page.
ea13a86463fd0 (Jan Kara 2011-05-24 00:23:35 +0200 2516) *
14da9200140f8 (Jan Kara 2012-06-12 16:20:37 +0200 2517) * Direct callers of this function should protect against filesystem freezing
5c50002963369 (Ross Zwisler 2015-10-13 16:51:02 -0600 2518) * using sb_start_pagefault() - sb_end_pagefault() functions.
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2519) */
5c50002963369 (Ross Zwisler 2015-10-13 16:51:02 -0600 2520) int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2521) get_block_t get_block)
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2522) {
c2ec175c39f62 (Nicholas Piggin 2009-03-31 15:23:21 -0700 2523) struct page *page = vmf->page;
496ad9aa8ef44 (Al Viro 2013-01-23 17:07:38 -0500 2524) struct inode *inode = file_inode(vma->vm_file);
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2525) unsigned long end;
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2526) loff_t size;
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2527) int ret;
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2528)
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2529) lock_page(page);
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2530) size = i_size_read(inode);
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2531) if ((page->mapping != inode->i_mapping) ||
1833633803c7e (Nicholas Piggin 2007-07-20 00:31:45 -0700 2532) (page_offset(page) > size)) {
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2533) /* We overload EFAULT to mean page got truncated */
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2534) ret = -EFAULT;
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2535) goto out_unlock;
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2536) }
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2537)
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2538) /* page is wholly or partially inside EOF */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2539) if (((page->index + 1) << PAGE_SHIFT) > size)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2540) end = size & ~PAGE_MASK;
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2541) else
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2542) end = PAGE_SIZE;
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2543)
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 2544) ret = __block_write_begin(page, 0, end, get_block);
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2545) if (!ret)
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2546) ret = block_commit_write(page, 0, end);
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2547)
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2548) if (unlikely(ret < 0))
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2549) goto out_unlock;
ea13a86463fd0 (Jan Kara 2011-05-24 00:23:35 +0200 2550) set_page_dirty(page);
1d1d1a767206f (Darrick J. Wong 2013-02-21 16:42:51 -0800 2551) wait_for_stable_page(page);
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2552) return 0;
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2553) out_unlock:
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2554) unlock_page(page);
5417169026c3d (David Chinner 2007-07-19 17:39:55 +1000 2555) return ret;
24da4fab5a617 (Jan Kara 2011-05-24 00:23:34 +0200 2556) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 2557) EXPORT_SYMBOL(block_page_mkwrite);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2558)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2559) /*
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2560) * nobh_write_begin()'s prereads are special: the buffer_heads are freed
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2561) * immediately, while under the page lock. So it needs a special end_io
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2562) * handler which does not touch the bh after unlocking it.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2563) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2564) static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2565) {
68671f35fe8d7 (Dmitry Monakhov 2007-10-16 01:24:47 -0700 2566) __end_buffer_read_notouch(bh, uptodate);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2567) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2568)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2569) /*
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2570) * Attach the singly-linked list of buffers created by nobh_write_begin, to
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2571) * the page (converting it to circular linked list and taking care of page
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2572) * dirty races).
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2573) */
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2574) static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2575) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2576) struct buffer_head *bh;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2577)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2578) BUG_ON(!PageLocked(page));
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2579)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2580) spin_lock(&page->mapping->private_lock);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2581) bh = head;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2582) do {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2583) if (PageDirty(page))
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2584) set_buffer_dirty(bh);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2585) if (!bh->b_this_page)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2586) bh->b_this_page = head;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2587) bh = bh->b_this_page;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2588) } while (bh != head);
45dcfc27329f6 (Guoqing Jiang 2020-06-01 21:47:48 -0700 2589) attach_page_private(page, head);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2590) spin_unlock(&page->mapping->private_lock);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2591) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2592)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2593) /*
ea0f04e59543b (Christoph Hellwig 2010-06-04 11:29:54 +0200 2594) * On entry, the page is fully not uptodate.
ea0f04e59543b (Christoph Hellwig 2010-06-04 11:29:54 +0200 2595) * On exit the page is fully uptodate in the areas outside (from,to)
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 2596) * The filesystem needs to handle block truncation upon failure.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2597) */
ea0f04e59543b (Christoph Hellwig 2010-06-04 11:29:54 +0200 2598) int nobh_write_begin(struct address_space *mapping,
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2599) loff_t pos, unsigned len, unsigned flags,
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2600) struct page **pagep, void **fsdata,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2601) get_block_t *get_block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2602) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2603) struct inode *inode = mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2604) const unsigned blkbits = inode->i_blkbits;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2605) const unsigned blocksize = 1 << blkbits;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2606) struct buffer_head *head, *bh;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2607) struct page *page;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2608) pgoff_t index;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2609) unsigned from, to;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2610) unsigned block_in_page;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2611) unsigned block_start, block_end;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2612) sector_t block_in_file;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2613) int nr_reads = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2614) int ret = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2615) int is_mapped_to_disk = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2616)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2617) index = pos >> PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2618) from = pos & (PAGE_SIZE - 1);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2619) to = from + len;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2620)
54566b2c1594c (Nicholas Piggin 2009-01-04 12:00:53 -0800 2621) page = grab_cache_page_write_begin(mapping, index, flags);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2622) if (!page)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2623) return -ENOMEM;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2624) *pagep = page;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2625) *fsdata = NULL;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2626)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2627) if (page_has_buffers(page)) {
309f77ad9bea0 (Namhyung Kim 2010-10-25 15:01:12 +0900 2628) ret = __block_write_begin(page, pos, len, get_block);
309f77ad9bea0 (Namhyung Kim 2010-10-25 15:01:12 +0900 2629) if (unlikely(ret))
309f77ad9bea0 (Namhyung Kim 2010-10-25 15:01:12 +0900 2630) goto out_release;
309f77ad9bea0 (Namhyung Kim 2010-10-25 15:01:12 +0900 2631) return ret;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2632) }
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2633)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2634) if (PageMappedToDisk(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2635) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2636)
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2637) /*
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2638) * Allocate buffers so that we can keep track of state, and potentially
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2639) * attach them to the page if an error occurs. In the common case of
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2640) * no error, they will just be freed again without ever being attached
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2641) * to the page (which is all OK, because we're under the page lock).
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2642) *
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2643) * Be careful: the buffer linked list is a NULL terminated one, rather
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2644) * than the circular one we're used to.
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2645) */
640ab98fb3629 (Jens Axboe 2017-09-27 05:40:16 -0600 2646) head = alloc_page_buffers(page, blocksize, false);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2647) if (!head) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2648) ret = -ENOMEM;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2649) goto out_release;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2650) }
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2651)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2652) block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2653)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2654) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2655) * We loop across all blocks in the page, whether or not they are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2656) * part of the affected region. This is so we can discover if the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2657) * page is fully mapped-to-disk.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2658) */
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2659) for (block_start = 0, block_in_page = 0, bh = head;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2660) block_start < PAGE_SIZE;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2661) block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2662) int create;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2663)
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2664) block_end = block_start + blocksize;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2665) bh->b_state = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2666) create = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2667) if (block_start >= to)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2668) create = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2669) ret = get_block(inode, block_in_file + block_in_page,
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2670) bh, create);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2671) if (ret)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2672) goto failed;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2673) if (!buffer_mapped(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2674) is_mapped_to_disk = 0;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2675) if (buffer_new(bh))
e64855c6cfaa0 (Jan Kara 2016-11-04 18:08:15 +0100 2676) clean_bdev_bh_alias(bh);
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2677) if (PageUptodate(page)) {
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2678) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2679) continue;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2680) }
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2681) if (buffer_new(bh) || !buffer_mapped(bh)) {
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2682) zero_user_segments(page, block_start, from,
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2683) to, block_end);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2684) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2685) }
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2686) if (buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2687) continue; /* reiserfs does this */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2688) if (block_start < from || block_end > to) {
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2689) lock_buffer(bh);
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2690) bh->b_end_io = end_buffer_read_nobh;
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 2691) submit_bh(REQ_OP_READ, 0, bh);
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2692) nr_reads++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2693) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2694) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2695)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2696) if (nr_reads) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2697) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2698) * The page is locked, so these buffers are protected from
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2699) * any VM or truncate activity. Hence we don't need to care
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2700) * for the buffer_head refcounts.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2701) */
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2702) for (bh = head; bh; bh = bh->b_this_page) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2703) wait_on_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2704) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2705) ret = -EIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2706) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2707) if (ret)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2708) goto failed;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2709) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2710)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2711) if (is_mapped_to_disk)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2712) SetPageMappedToDisk(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2713)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2714) *fsdata = head; /* to be released by nobh_write_end */
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2715)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2716) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2717)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2718) failed:
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2719) BUG_ON(!ret);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2720) /*
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2721) * Error recovery is a bit difficult. We need to zero out blocks that
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2722) * were newly allocated, and dirty them to ensure they get written out.
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2723) * Buffers need to be attached to the page at this point, otherwise
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2724) * the handling of potential IO errors during writeout would be hard
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2725) * (could try doing synchronous writeout, but what if that fails too?)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2726) */
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2727) attach_nobh_buffers(page, head);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2728) page_zero_new_buffers(page, from, to);
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2729)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2730) out_release:
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2731) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2732) put_page(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2733) *pagep = NULL;
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2734)
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 2735) return ret;
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 2736) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2737) EXPORT_SYMBOL(nobh_write_begin);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2738)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2739) int nobh_write_end(struct file *file, struct address_space *mapping,
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2740) loff_t pos, unsigned len, unsigned copied,
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2741) struct page *page, void *fsdata)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2742) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2743) struct inode *inode = page->mapping->host;
efdc31319d430 (Nicholas Piggin 2007-10-21 06:57:41 +0200 2744) struct buffer_head *head = fsdata;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2745) struct buffer_head *bh;
5b41e74ad1b0b (Dmitri Monakhov 2008-03-28 14:15:52 -0700 2746) BUG_ON(fsdata != NULL && page_has_buffers(page));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2747)
d4cf109f05ff0 (Dave Kleikamp 2009-02-06 14:59:26 -0600 2748) if (unlikely(copied < len) && head)
5b41e74ad1b0b (Dmitri Monakhov 2008-03-28 14:15:52 -0700 2749) attach_nobh_buffers(page, head);
5b41e74ad1b0b (Dmitri Monakhov 2008-03-28 14:15:52 -0700 2750) if (page_has_buffers(page))
5b41e74ad1b0b (Dmitri Monakhov 2008-03-28 14:15:52 -0700 2751) return generic_write_end(file, mapping, pos, len,
5b41e74ad1b0b (Dmitri Monakhov 2008-03-28 14:15:52 -0700 2752) copied, page, fsdata);
a4b0672db3a69 (Nicholas Piggin 2007-10-16 01:24:48 -0700 2753)
22c8ca78f2072 (Nicholas Piggin 2007-02-20 13:58:09 -0800 2754) SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2755) set_page_dirty(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2756) if (pos+copied > inode->i_size) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2757) i_size_write(inode, pos+copied);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2758) mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2759) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2760)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2761) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2762) put_page(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2763)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2764) while (head) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2765) bh = head;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2766) head = head->b_this_page;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2767) free_buffer_head(bh);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2768) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2769)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2770) return copied;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2771) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2772) EXPORT_SYMBOL(nobh_write_end);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2773)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2774) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2775) * nobh_writepage() - based on block_full_write_page() except
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2776) * that it tries to operate without attaching bufferheads to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2777) * the page.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2778) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2779) int nobh_writepage(struct page *page, get_block_t *get_block,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2780) struct writeback_control *wbc)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2781) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2782) struct inode * const inode = page->mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2783) loff_t i_size = i_size_read(inode);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2784) const pgoff_t end_index = i_size >> PAGE_SHIFT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2785) unsigned offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2786) int ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2787)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2788) /* Is the page fully inside i_size? */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2789) if (page->index < end_index)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2790) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2791)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2792) /* Is the page fully outside i_size? (truncate in progress) */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2793) offset = i_size & (PAGE_SIZE-1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2794) if (page->index >= end_index+1 || !offset) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2795) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2796) return 0; /* don't care */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2797) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2798)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2799) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2800) * The page straddles i_size. It must be zeroed out on each and every
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2801) * writepage invocation because it may be mmapped. "A file is mapped
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2802) * in multiples of the page size. For a file that is not a multiple of
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2803) * the page size, the remaining memory is zeroed when mapped, and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2804) * writes to that region are not written out to the file."
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2805) */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2806) zero_user_segment(page, offset, PAGE_SIZE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2807) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2808) ret = mpage_writepage(page, get_block, wbc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2809) if (ret == -EAGAIN)
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 2810) ret = __block_write_full_page(inode, page, get_block, wbc,
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 2811) end_buffer_async_write);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2812) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2813) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2814) EXPORT_SYMBOL(nobh_writepage);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2815)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2816) int nobh_truncate_page(struct address_space *mapping,
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2817) loff_t from, get_block_t *get_block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2818) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2819) pgoff_t index = from >> PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2820) unsigned offset = from & (PAGE_SIZE-1);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2821) unsigned blocksize;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2822) sector_t iblock;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2823) unsigned length, pos;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2824) struct inode *inode = mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2825) struct page *page;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2826) struct buffer_head map_bh;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2827) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2828)
93407472a21b8 (Fabian Frederick 2017-02-27 14:28:32 -0800 2829) blocksize = i_blocksize(inode);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2830) length = offset & (blocksize - 1);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2831)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2832) /* Block boundary? Nothing to do */
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2833) if (!length)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2834) return 0;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2835)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2836) length = blocksize - length;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2837) iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2838)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2839) page = grab_cache_page(mapping, index);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2840) err = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2841) if (!page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2842) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2843)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2844) if (page_has_buffers(page)) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2845) has_buffers:
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2846) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2847) put_page(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2848) return block_truncate_page(mapping, from, get_block);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2849) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2850)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2851) /* Find the buffer that contains "offset" */
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2852) pos = blocksize;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2853) while (offset >= pos) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2854) iblock++;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2855) pos += blocksize;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2856) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2857)
460bcf57b128c (Theodore Ts'o 2009-05-12 07:37:56 -0400 2858) map_bh.b_size = blocksize;
460bcf57b128c (Theodore Ts'o 2009-05-12 07:37:56 -0400 2859) map_bh.b_state = 0;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2860) err = get_block(inode, iblock, &map_bh, 0);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2861) if (err)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2862) goto unlock;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2863) /* unmapped? It's a hole - nothing to do */
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2864) if (!buffer_mapped(&map_bh))
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2865) goto unlock;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2866)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2867) /* Ok, it's mapped. Make sure it's up-to-date */
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2868) if (!PageUptodate(page)) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2869) err = mapping->a_ops->readpage(NULL, page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2870) if (err) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2871) put_page(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2872) goto out;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2873) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2874) lock_page(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2875) if (!PageUptodate(page)) {
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2876) err = -EIO;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2877) goto unlock;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2878) }
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2879) if (page_has_buffers(page))
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2880) goto has_buffers;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2881) }
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2882) zero_user(page, offset, length);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2883) set_page_dirty(page);
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2884) err = 0;
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2885)
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2886) unlock:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2887) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2888) put_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2889) out:
03158cd7eb337 (Nicholas Piggin 2007-10-16 01:25:25 -0700 2890) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2891) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2892) EXPORT_SYMBOL(nobh_truncate_page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2893)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2894) int block_truncate_page(struct address_space *mapping,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2895) loff_t from, get_block_t *get_block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2896) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2897) pgoff_t index = from >> PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2898) unsigned offset = from & (PAGE_SIZE-1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2899) unsigned blocksize;
54b21a7992a31 (Andrew Morton 2006-01-08 01:03:05 -0800 2900) sector_t iblock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2901) unsigned length, pos;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2902) struct inode *inode = mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2903) struct page *page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2904) struct buffer_head *bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2905) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2906)
93407472a21b8 (Fabian Frederick 2017-02-27 14:28:32 -0800 2907) blocksize = i_blocksize(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2908) length = offset & (blocksize - 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2909)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2910) /* Block boundary? Nothing to do */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2911) if (!length)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2912) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2913)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2914) length = blocksize - length;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2915) iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2916)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2917) page = grab_cache_page(mapping, index);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2918) err = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2919) if (!page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2920) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2921)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2922) if (!page_has_buffers(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2923) create_empty_buffers(page, blocksize, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2924)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2925) /* Find the buffer that contains "offset" */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2926) bh = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2927) pos = blocksize;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2928) while (offset >= pos) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2929) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2930) iblock++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2931) pos += blocksize;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2932) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2933)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2934) err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2935) if (!buffer_mapped(bh)) {
b0cf2321c6599 (Badari Pulavarty 2006-03-26 01:38:00 -0800 2936) WARN_ON(bh->b_size != blocksize);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2937) err = get_block(inode, iblock, bh, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2938) if (err)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2939) goto unlock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2940) /* unmapped? It's a hole - nothing to do */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2941) if (!buffer_mapped(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2942) goto unlock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2943) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2944)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2945) /* Ok, it's mapped. Make sure it's up-to-date */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2946) if (PageUptodate(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2947) set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2948)
33a266dda9fbb (David Chinner 2007-02-12 00:51:41 -0800 2949) if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2950) err = -EIO;
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 2951) ll_rw_block(REQ_OP_READ, 0, 1, &bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2952) wait_on_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2953) /* Uhhuh. Read error. Complain and punt. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2954) if (!buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2955) goto unlock;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2956) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2957)
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2958) zero_user(page, offset, length);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2959) mark_buffer_dirty(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2960) err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2961)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2962) unlock:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2963) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2964) put_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2965) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2966) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2967) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 2968) EXPORT_SYMBOL(block_truncate_page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2969)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2970) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2971) * The generic ->writepage function for buffer-backed address_spaces
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2972) */
1b938c0827478 (Matthew Wilcox 2014-06-04 16:07:43 -0700 2973) int block_write_full_page(struct page *page, get_block_t *get_block,
1b938c0827478 (Matthew Wilcox 2014-06-04 16:07:43 -0700 2974) struct writeback_control *wbc)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2975) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2976) struct inode * const inode = page->mapping->host;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2977) loff_t i_size = i_size_read(inode);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2978) const pgoff_t end_index = i_size >> PAGE_SHIFT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2979) unsigned offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2980)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2981) /* Is the page fully inside i_size? */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2982) if (page->index < end_index)
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 2983) return __block_write_full_page(inode, page, get_block, wbc,
1b938c0827478 (Matthew Wilcox 2014-06-04 16:07:43 -0700 2984) end_buffer_async_write);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2985)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2986) /* Is the page fully outside i_size? (truncate in progress) */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2987) offset = i_size & (PAGE_SIZE-1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2988) if (page->index >= end_index+1 || !offset) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2989) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2990) return 0; /* don't care */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2991) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2992)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2993) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2994) * The page straddles i_size. It must be zeroed out on each and every
2a61aa4016385 (Adam Buchbinder 2009-12-11 16:35:40 -0500 2995) * writepage invocation because it may be mmapped. "A file is mapped
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2996) * in multiples of the page size. For a file that is not a multiple of
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2997) * the page size, the remaining memory is zeroed when mapped, and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2998) * writes to that region are not written out to the file."
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2999) */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 3000) zero_user_segment(page, offset, PAGE_SIZE);
1b938c0827478 (Matthew Wilcox 2014-06-04 16:07:43 -0700 3001) return __block_write_full_page(inode, page, get_block, wbc,
1b938c0827478 (Matthew Wilcox 2014-06-04 16:07:43 -0700 3002) end_buffer_async_write);
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 3003) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 3004) EXPORT_SYMBOL(block_write_full_page);
35c80d5f400f6 (Chris Mason 2009-04-15 13:22:38 -0400 3005)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3006) sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3007) get_block_t *get_block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3008) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3009) struct inode *inode = mapping->host;
2a527d6858c24 (Alexander Potapenko 2017-07-05 00:56:21 -0400 3010) struct buffer_head tmp = {
2a527d6858c24 (Alexander Potapenko 2017-07-05 00:56:21 -0400 3011) .b_size = i_blocksize(inode),
2a527d6858c24 (Alexander Potapenko 2017-07-05 00:56:21 -0400 3012) };
2a527d6858c24 (Alexander Potapenko 2017-07-05 00:56:21 -0400 3013)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3014) get_block(inode, block, &tmp, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3015) return tmp.b_blocknr;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3016) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 3017) EXPORT_SYMBOL(generic_block_bmap);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3018)
4246a0b63bd8f (Christoph Hellwig 2015-07-20 15:29:37 +0200 3019) static void end_bio_bh_io_sync(struct bio *bio)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3020) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3021) struct buffer_head *bh = bio->bi_private;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3022)
b7c44ed9d2fc6 (Jens Axboe 2015-07-24 12:37:59 -0600 3023) if (unlikely(bio_flagged(bio, BIO_QUIET)))
08bafc0341f2f (Keith Mannthey 2008-11-25 10:24:35 +0100 3024) set_bit(BH_Quiet, &bh->b_state);
08bafc0341f2f (Keith Mannthey 2008-11-25 10:24:35 +0100 3025)
4e4cbee93d561 (Christoph Hellwig 2017-06-03 09:38:06 +0200 3026) bh->b_end_io(bh, !bio->bi_status);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3027) bio_put(bio);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3028) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3029)
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3030) static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 3031) enum rw_hint write_hint, struct writeback_control *wbc)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3032) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3033) struct bio *bio;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3034)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3035) BUG_ON(!buffer_locked(bh));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3036) BUG_ON(!buffer_mapped(bh));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3037) BUG_ON(!bh->b_end_io);
8fb0e342481c4 (Aneesh Kumar K.V 2009-05-12 16:22:37 -0400 3038) BUG_ON(buffer_delay(bh));
8fb0e342481c4 (Aneesh Kumar K.V 2009-05-12 16:22:37 -0400 3039) BUG_ON(buffer_unwritten(bh));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3040)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3041) /*
48fd4f93a00ea (Jens Axboe 2008-08-22 10:00:36 +0200 3042) * Only clear out a write error when rewriting
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3043) */
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3044) if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3045) clear_buffer_write_io_error(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3046)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3047) bio = bio_alloc(GFP_NOIO, 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3048)
4f74d15fe4083 (Eric Biggers 2020-07-02 01:56:07 +0000 3049) fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
4f74d15fe4083 (Eric Biggers 2020-07-02 01:56:07 +0000 3050)
4f024f3797c43 (Kent Overstreet 2013-10-11 15:44:27 -0700 3051) bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
74d46992e0d9d (Christoph Hellwig 2017-08-23 19:10:32 +0200 3052) bio_set_dev(bio, bh->b_bdev);
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 3053) bio->bi_write_hint = write_hint;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3054)
6cf66b4caf9c7 (Kent Overstreet 2014-12-22 12:48:42 +0100 3055) bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
6cf66b4caf9c7 (Kent Overstreet 2014-12-22 12:48:42 +0100 3056) BUG_ON(bio->bi_iter.bi_size != bh->b_size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3057)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3058) bio->bi_end_io = end_bio_bh_io_sync;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3059) bio->bi_private = bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3060)
877f962c5edac (Theodore Ts'o 2013-04-20 19:58:37 -0400 3061) if (buffer_meta(bh))
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3062) op_flags |= REQ_META;
877f962c5edac (Theodore Ts'o 2013-04-20 19:58:37 -0400 3063) if (buffer_prio(bh))
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3064) op_flags |= REQ_PRIO;
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3065) bio_set_op_attrs(bio, op, op_flags);
877f962c5edac (Theodore Ts'o 2013-04-20 19:58:37 -0400 3066)
83c9c547168e8 (Ming Lei 2020-01-05 09:41:14 +0800 3067) /* Take care of bh's that straddle the end of the device */
83c9c547168e8 (Ming Lei 2020-01-05 09:41:14 +0800 3068) guard_bio_eod(bio);
83c9c547168e8 (Ming Lei 2020-01-05 09:41:14 +0800 3069)
fd42df305f804 (Dennis Zhou 2018-12-05 12:10:34 -0500 3070) if (wbc) {
fd42df305f804 (Dennis Zhou 2018-12-05 12:10:34 -0500 3071) wbc_init_bio(wbc, bio);
34e51a5e1a6e9 (Tejun Heo 2019-06-27 13:39:49 -0700 3072) wbc_account_cgroup_owner(wbc, bh->b_page, bh->b_size);
fd42df305f804 (Dennis Zhou 2018-12-05 12:10:34 -0500 3073) }
fd42df305f804 (Dennis Zhou 2018-12-05 12:10:34 -0500 3074)
4e49ea4a3d276 (Mike Christie 2016-06-05 14:31:41 -0500 3075) submit_bio(bio);
f6454b049d81b (Julia Lawall 2015-05-26 21:59:53 +0200 3076) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3077) }
bafc0dba1e20d (Tejun Heo 2015-06-02 08:37:23 -0600 3078)
020c2833dbc76 (Eric Biggers 2017-03-25 21:02:18 -0700 3079) int submit_bh(int op, int op_flags, struct buffer_head *bh)
bafc0dba1e20d (Tejun Heo 2015-06-02 08:37:23 -0600 3080) {
8e8f9298818c4 (Jens Axboe 2017-06-27 09:30:05 -0600 3081) return submit_bh_wbc(op, op_flags, bh, 0, NULL);
7136851117744 (Darrick J. Wong 2013-04-29 15:07:25 -0700 3082) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 3083) EXPORT_SYMBOL(submit_bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3084)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3085) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3086) * ll_rw_block: low-level access to block devices (DEPRECATED)
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 3087) * @op: whether to %READ or %WRITE
ef295ecf090d3 (Christoph Hellwig 2016-10-28 08:48:16 -0600 3088) * @op_flags: req_flag_bits
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3089) * @nr: number of &struct buffer_heads in the array
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3090) * @bhs: array of pointers to &struct buffer_head
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3091) *
a766223625337 (Jan Kara 2005-09-06 15:19:10 -0700 3092) * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
70246286e94c3 (Christoph Hellwig 2016-07-19 11:28:41 +0200 3093) * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
70246286e94c3 (Christoph Hellwig 2016-07-19 11:28:41 +0200 3094) * @op_flags contains flags modifying the detailed I/O behavior, most notably
70246286e94c3 (Christoph Hellwig 2016-07-19 11:28:41 +0200 3095) * %REQ_RAHEAD.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3096) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3097) * This function drops any buffer that it cannot get a lock on (with the
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3098) * BH_Lock state bit), any buffer that appears to be clean when doing a write
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3099) * request, and any buffer that appears to be up-to-date when doing read
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3100) * request. Further it marks as clean buffers that are processed for
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3101) * writing (the buffer cache won't assume that they are actually clean
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3102) * until the buffer gets unlocked).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3103) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3104) * ll_rw_block sets b_end_io to simple completion handler that marks
e227867f12302 (Masanari Iida 2014-02-18 22:54:36 +0900 3105) * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3106) * any waiters.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3107) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3108) * All of the buffers must be for the same device, and must also be a
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3109) * multiple of the current approved size for the device.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3110) */
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 3111) void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3112) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3113) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3114)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3115) for (i = 0; i < nr; i++) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3116) struct buffer_head *bh = bhs[i];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3117)
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3118) if (!trylock_buffer(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3119) continue;
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 3120) if (op == WRITE) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3121) if (test_clear_buffer_dirty(bh)) {
76c3073a888ae (akpm@osdl.org 2005-04-16 15:24:07 -0700 3122) bh->b_end_io = end_buffer_write_sync;
e60e5c50aa538 (OGAWA Hirofumi 2006-02-03 03:04:43 -0800 3123) get_bh(bh);
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 3124) submit_bh(op, op_flags, bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3125) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3126) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3127) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3128) if (!buffer_uptodate(bh)) {
76c3073a888ae (akpm@osdl.org 2005-04-16 15:24:07 -0700 3129) bh->b_end_io = end_buffer_read_sync;
e60e5c50aa538 (OGAWA Hirofumi 2006-02-03 03:04:43 -0800 3130) get_bh(bh);
dfec8a14fc904 (Mike Christie 2016-06-05 14:31:44 -0500 3131) submit_bh(op, op_flags, bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3132) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3133) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3134) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3135) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3136) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3137) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 3138) EXPORT_SYMBOL(ll_rw_block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3139)
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3140) void write_dirty_buffer(struct buffer_head *bh, int op_flags)
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3141) {
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3142) lock_buffer(bh);
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3143) if (!test_clear_buffer_dirty(bh)) {
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3144) unlock_buffer(bh);
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3145) return;
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3146) }
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3147) bh->b_end_io = end_buffer_write_sync;
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3148) get_bh(bh);
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3149) submit_bh(REQ_OP_WRITE, op_flags, bh);
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3150) }
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3151) EXPORT_SYMBOL(write_dirty_buffer);
9cb569d601e0b (Christoph Hellwig 2010-08-11 17:06:24 +0200 3152)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3153) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3154) * For a data-integrity writeout, we need to wait upon any in-progress I/O
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3155) * and then start new I/O and then wait upon it. The caller must have a ref on
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3156) * the buffer_head.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3157) */
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3158) int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3159) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3160) int ret = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3161)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3162) WARN_ON(atomic_read(&bh->b_count) < 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3163) lock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3164) if (test_clear_buffer_dirty(bh)) {
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3165) /*
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3166) * The bh should be mapped, but it might not be if the
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3167) * device was hot-removed. Not much we can do but fail the I/O.
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3168) */
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3169) if (!buffer_mapped(bh)) {
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3170) unlock_buffer(bh);
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3171) return -EIO;
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3172) }
377254b2cd225 (Xianting Tian 2020-07-31 12:10:25 -0400 3173)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3174) get_bh(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3175) bh->b_end_io = end_buffer_write_sync;
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3176) ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3177) wait_on_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3178) if (!ret && !buffer_uptodate(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3179) ret = -EIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3180) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3181) unlock_buffer(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3182) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3183) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3184) }
87e99511ea545 (Christoph Hellwig 2010-08-11 17:05:45 +0200 3185) EXPORT_SYMBOL(__sync_dirty_buffer);
87e99511ea545 (Christoph Hellwig 2010-08-11 17:05:45 +0200 3186)
87e99511ea545 (Christoph Hellwig 2010-08-11 17:05:45 +0200 3187) int sync_dirty_buffer(struct buffer_head *bh)
87e99511ea545 (Christoph Hellwig 2010-08-11 17:05:45 +0200 3188) {
70fd76140a6cb (Christoph Hellwig 2016-11-01 07:40:10 -0600 3189) return __sync_dirty_buffer(bh, REQ_SYNC);
87e99511ea545 (Christoph Hellwig 2010-08-11 17:05:45 +0200 3190) }
1fe72eaa0f46a (H Hartley Sweeten 2009-09-22 16:43:51 -0700 3191) EXPORT_SYMBOL(sync_dirty_buffer);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3192)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3193) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3194) * try_to_free_buffers() checks if all the buffers on this particular page
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3195) * are unused, and releases them if so.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3196) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3197) * Exclusion against try_to_free_buffers may be obtained by either
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3198) * locking the page or by holding its mapping's private_lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3199) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3200) * If the page is dirty but all the buffers are clean then we need to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3201) * be sure to mark the page clean as well. This is because the page
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3202) * may be against a block device, and a later reattachment of buffers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3203) * to a dirty page will set *all* buffers dirty. Which would corrupt
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3204) * filesystem data on the same device.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3205) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3206) * The same applies to regular filesystem pages: if all the buffers are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3207) * clean then we set the page clean and proceed. To do that, we require
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3208) * total exclusion from __set_page_dirty_buffers(). That is obtained with
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3209) * private_lock.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3210) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3211) * try_to_free_buffers() is non-blocking.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3212) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3213) static inline int buffer_busy(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3214) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3215) return atomic_read(&bh->b_count) |
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3216) (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3217) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3218)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3219) static int
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3220) drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3221) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3222) struct buffer_head *head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3223) struct buffer_head *bh;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3224)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3225) bh = head;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3226) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3227) if (buffer_busy(bh))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3228) goto failed;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3229) bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3230) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3231)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3232) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3233) struct buffer_head *next = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3234)
535ee2fbf79ab (Jan Kara 2008-02-08 04:21:59 -0800 3235) if (bh->b_assoc_map)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3236) __remove_assoc_queue(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3237) bh = next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3238) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3239) *buffers_to_free = head;
45dcfc27329f6 (Guoqing Jiang 2020-06-01 21:47:48 -0700 3240) detach_page_private(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3241) return 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3242) failed:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3243) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3244) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3245)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3246) int try_to_free_buffers(struct page *page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3247) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3248) struct address_space * const mapping = page->mapping;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3249) struct buffer_head *buffers_to_free = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3250) int ret = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3251)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3252) BUG_ON(!PageLocked(page));
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3253) if (PageWriteback(page))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3254) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3255)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3256) if (mapping == NULL) { /* can this still happen? */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3257) ret = drop_buffers(page, &buffers_to_free);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3258) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3259) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3260)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3261) spin_lock(&mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3262) ret = drop_buffers(page, &buffers_to_free);
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3263)
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3264) /*
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3265) * If the filesystem writes its buffers by hand (eg ext3)
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3266) * then we can have clean buffers against a dirty page. We
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3267) * clean the page here; otherwise the VM will never notice
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3268) * that the filesystem did any IO at all.
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3269) *
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3270) * Also, during truncate, discard_buffer will have marked all
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3271) * the page's buffers clean. We discover that here and clean
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3272) * the page also.
87df7241bd547 (Nicholas Piggin 2007-01-30 14:36:27 +1100 3273) *
87df7241bd547 (Nicholas Piggin 2007-01-30 14:36:27 +1100 3274) * private_lock must be held over this entire operation in order
87df7241bd547 (Nicholas Piggin 2007-01-30 14:36:27 +1100 3275) * to synchronise against __set_page_dirty_buffers and prevent the
87df7241bd547 (Nicholas Piggin 2007-01-30 14:36:27 +1100 3276) * dirty bit from being lost.
ecdfc9787fe52 (Linus Torvalds 2007-01-26 12:47:06 -0800 3277) */
11f81becca04b (Tejun Heo 2015-05-22 17:13:15 -0400 3278) if (ret)
11f81becca04b (Tejun Heo 2015-05-22 17:13:15 -0400 3279) cancel_dirty_page(page);
87df7241bd547 (Nicholas Piggin 2007-01-30 14:36:27 +1100 3280) spin_unlock(&mapping->private_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3281) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3282) if (buffers_to_free) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3283) struct buffer_head *bh = buffers_to_free;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3284)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3285) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3286) struct buffer_head *next = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3287) free_buffer_head(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3288) bh = next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3289) } while (bh != buffers_to_free);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3290) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3291) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3292) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3293) EXPORT_SYMBOL(try_to_free_buffers);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3294)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3295) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3296) * There are no bdflush tunables left. But distributions are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3297) * still running obsolete flush daemons, so we terminate them here.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3298) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3299) * Use of bdflush() is deprecated and will be removed in a future kernel.
5b0830cb9085f (Jens Axboe 2009-09-23 19:37:09 +0200 3300) * The `flush-X' kernel threads fully replace bdflush daemons and this call.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3301) */
bdc480e3bef6e (Heiko Carstens 2009-01-14 14:14:12 +0100 3302) SYSCALL_DEFINE2(bdflush, int, func, long, data)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3303) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3304) static int msg_count;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3305)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3306) if (!capable(CAP_SYS_ADMIN))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3307) return -EPERM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3308)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3309) if (msg_count < 5) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3310) msg_count++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3311) printk(KERN_INFO
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3312) "warning: process `%s' used the obsolete bdflush"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3313) " system call\n", current->comm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3314) printk(KERN_INFO "Fix your initscripts?\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3315) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3316)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3317) if (func == 1)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3318) do_exit(0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3319) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3320) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3321)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3322) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3323) * Buffer-head allocation
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3324) */
a0a9b04337677 (Shai Fultheim 2012-05-15 12:29:52 +0300 3325) static struct kmem_cache *bh_cachep __read_mostly;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3326)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3327) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3328) * Once the number of bh's in the machine exceeds this level, we start
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3329) * stripping them in writeback.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3330) */
43be594a6b0ce (Zhang Yanfei 2013-02-22 16:35:46 -0800 3331) static unsigned long max_buffer_heads;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3332)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3333) int buffer_heads_over_limit;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3334)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3335) struct bh_accounting {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3336) int nr; /* Number of live bh's */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3337) int ratelimit; /* Limit cacheline bouncing */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3338) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3339)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3340) static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3341)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3342) static void recalc_bh_state(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3343) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3344) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3345) int tot = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3346)
ee1be8626355e (Christoph Lameter 2010-12-06 11:40:05 -0600 3347) if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3348) return;
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3349) __this_cpu_write(bh_accounting.ratelimit, 0);
8a14342683b1e (Eric Dumazet 2006-03-24 03:18:10 -0800 3350) for_each_online_cpu(i)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3351) tot += per_cpu(bh_accounting, i).nr;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3352) buffer_heads_over_limit = (tot > max_buffer_heads);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3353) }
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3354)
dd0fc66fb33cd (Al Viro 2005-10-07 07:46:04 +0100 3355) struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3356) {
019b4d123aa7b (Richard Kennedy 2010-03-10 15:20:33 -0800 3357) struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3358) if (ret) {
a35afb830f8d7 (Christoph Lameter 2007-05-16 22:10:57 -0700 3359) INIT_LIST_HEAD(&ret->b_assoc_buffers);
f1e67e355c2aa (Thomas Gleixner 2019-11-18 14:28:24 +0100 3360) spin_lock_init(&ret->b_uptodate_lock);
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3361) preempt_disable();
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3362) __this_cpu_inc(bh_accounting.nr);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3363) recalc_bh_state();
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3364) preempt_enable();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3365) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3366) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3367) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3368) EXPORT_SYMBOL(alloc_buffer_head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3369)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3370) void free_buffer_head(struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3371) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3372) BUG_ON(!list_empty(&bh->b_assoc_buffers));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3373) kmem_cache_free(bh_cachep, bh);
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3374) preempt_disable();
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3375) __this_cpu_dec(bh_accounting.nr);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3376) recalc_bh_state();
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3377) preempt_enable();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3378) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3379) EXPORT_SYMBOL(free_buffer_head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3380)
fc4d24c9b4715 (Sebastian Andrzej Siewior 2016-11-03 15:49:57 +0100 3381) static int buffer_exit_cpu_dead(unsigned int cpu)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3382) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3383) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3384) struct bh_lru *b = &per_cpu(bh_lrus, cpu);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3385)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3386) for (i = 0; i < BH_LRU_SIZE; i++) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3387) brelse(b->bhs[i]);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3388) b->bhs[i] = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3389) }
c7b92516a9c68 (Christoph Lameter 2010-12-06 11:16:28 -0600 3390) this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
8a14342683b1e (Eric Dumazet 2006-03-24 03:18:10 -0800 3391) per_cpu(bh_accounting, cpu).nr = 0;
fc4d24c9b4715 (Sebastian Andrzej Siewior 2016-11-03 15:49:57 +0100 3392) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3393) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3394)
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3395) /**
a6b91919e0881 (Randy Dunlap 2008-03-19 17:01:00 -0700 3396) * bh_uptodate_or_lock - Test whether the buffer is uptodate
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3397) * @bh: struct buffer_head
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3398) *
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3399) * Return true if the buffer is up-to-date and false,
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3400) * with the buffer locked, if not.
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3401) */
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3402) int bh_uptodate_or_lock(struct buffer_head *bh)
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3403) {
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3404) if (!buffer_uptodate(bh)) {
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3405) lock_buffer(bh);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3406) if (!buffer_uptodate(bh))
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3407) return 0;
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3408) unlock_buffer(bh);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3409) }
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3410) return 1;
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3411) }
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3412) EXPORT_SYMBOL(bh_uptodate_or_lock);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3413)
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3414) /**
a6b91919e0881 (Randy Dunlap 2008-03-19 17:01:00 -0700 3415) * bh_submit_read - Submit a locked buffer for reading
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3416) * @bh: struct buffer_head
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3417) *
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3418) * Returns zero on success and -EIO on error.
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3419) */
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3420) int bh_submit_read(struct buffer_head *bh)
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3421) {
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3422) BUG_ON(!buffer_locked(bh));
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3423)
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3424) if (buffer_uptodate(bh)) {
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3425) unlock_buffer(bh);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3426) return 0;
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3427) }
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3428)
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3429) get_bh(bh);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3430) bh->b_end_io = end_buffer_read_sync;
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 3431) submit_bh(REQ_OP_READ, 0, bh);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3432) wait_on_buffer(bh);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3433) if (buffer_uptodate(bh))
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3434) return 0;
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3435) return -EIO;
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3436) }
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3437) EXPORT_SYMBOL(bh_submit_read);
389d1b083c767 (Aneesh Kumar K.V 2008-01-28 23:58:26 -0500 3438)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3439) void __init buffer_init(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3440) {
43be594a6b0ce (Zhang Yanfei 2013-02-22 16:35:46 -0800 3441) unsigned long nrpages;
fc4d24c9b4715 (Sebastian Andrzej Siewior 2016-11-03 15:49:57 +0100 3442) int ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3443)
b98938c373117 (Christoph Lameter 2008-02-04 22:28:36 -0800 3444) bh_cachep = kmem_cache_create("buffer_head",
b98938c373117 (Christoph Lameter 2008-02-04 22:28:36 -0800 3445) sizeof(struct buffer_head), 0,
b98938c373117 (Christoph Lameter 2008-02-04 22:28:36 -0800 3446) (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
b98938c373117 (Christoph Lameter 2008-02-04 22:28:36 -0800 3447) SLAB_MEM_SPREAD),
019b4d123aa7b (Richard Kennedy 2010-03-10 15:20:33 -0800 3448) NULL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3449)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3450) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3451) * Limit the bh occupancy to 10% of ZONE_NORMAL
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3452) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3453) nrpages = (nr_free_buffer_pages() * 10) / 100;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3454) max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
fc4d24c9b4715 (Sebastian Andrzej Siewior 2016-11-03 15:49:57 +0100 3455) ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
fc4d24c9b4715 (Sebastian Andrzej Siewior 2016-11-03 15:49:57 +0100 3456) NULL, buffer_exit_cpu_dead);
fc4d24c9b4715 (Sebastian Andrzej Siewior 2016-11-03 15:49:57 +0100 3457) WARN_ON(ret < 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3458) }