blob: 62420e3c3cd21df62addd1755dd1f65f5a7f83e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/buffer.c
3 *
4 * Copyright (C) 1991, 1992, 2002 Linus Torvalds
5 */
6
7/*
8 * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
9 *
10 * Removed a lot of unnecessary code and simplified things now that
11 * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
12 *
13 * Speed up hash, lru, and free list operations. Use gfp() for allocating
14 * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
15 *
16 * Added 32k buffer block sizes - these are required older ARM systems. - RMK
17 *
18 * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/syscalls.h>
23#include <linux/fs.h>
24#include <linux/mm.h>
25#include <linux/percpu.h>
26#include <linux/slab.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/blkdev.h>
29#include <linux/file.h>
30#include <linux/quotaops.h>
31#include <linux/highmem.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050032#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/writeback.h>
34#include <linux/hash.h>
35#include <linux/suspend.h>
36#include <linux/buffer_head.h>
Andrew Morton55e829a2006-12-10 02:19:27 -080037#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/bio.h>
39#include <linux/notifier.h>
40#include <linux/cpu.h>
41#include <linux/bitops.h>
42#include <linux/mpage.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070043#include <linux/bit_spinlock.h>
Tejun Heo5305cb82013-01-11 13:06:36 -080044#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
49
Yan Honga3f3c292012-12-12 13:52:15 -080050void init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 bh->b_end_io = handler;
53 bh->b_private = private;
54}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070055EXPORT_SYMBOL(init_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Tejun Heof0059af2013-01-11 13:06:35 -080057inline void touch_buffer(struct buffer_head *bh)
58{
Tejun Heo5305cb82013-01-11 13:06:36 -080059 trace_block_touch_buffer(bh);
Tejun Heof0059af2013-01-11 13:06:35 -080060 mark_page_accessed(bh->b_page);
61}
62EXPORT_SYMBOL(touch_buffer);
63
Jens Axboe7eaceac2011-03-10 08:52:07 +010064static int sleep_on_buffer(void *word)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 io_schedule();
67 return 0;
68}
69
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080070void __lock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Jens Axboe7eaceac2011-03-10 08:52:07 +010072 wait_on_bit_lock(&bh->b_state, BH_Lock, sleep_on_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 TASK_UNINTERRUPTIBLE);
74}
75EXPORT_SYMBOL(__lock_buffer);
76
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080077void unlock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Nick Piggin51b07fc2008-10-18 20:27:00 -070079 clear_bit_unlock(BH_Lock, &bh->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 smp_mb__after_clear_bit();
81 wake_up_bit(&bh->b_state, BH_Lock);
82}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070083EXPORT_SYMBOL(unlock_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Block until a buffer comes unlocked. This doesn't stop it
87 * from becoming locked again - you have to lock it yourself
88 * if you want to preserve its state.
89 */
90void __wait_on_buffer(struct buffer_head * bh)
91{
Jens Axboe7eaceac2011-03-10 08:52:07 +010092 wait_on_bit(&bh->b_state, BH_Lock, sleep_on_buffer, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070094EXPORT_SYMBOL(__wait_on_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static void
97__clear_page_buffers(struct page *page)
98{
99 ClearPagePrivate(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700100 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 page_cache_release(page);
102}
103
Keith Mannthey08bafc02008-11-25 10:24:35 +0100104
105static int quiet_error(struct buffer_head *bh)
106{
107 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
108 return 0;
109 return 1;
110}
111
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void buffer_io_error(struct buffer_head *bh)
114{
115 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
117 bdevname(bh->b_bdev, b),
118 (unsigned long long)bh->b_blocknr);
119}
120
121/*
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700122 * End-of-IO handler helper function which does not touch the bh after
123 * unlocking it.
124 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
125 * a race there is benign: unlock_buffer() only use the bh's address for
126 * hashing after unlocking the buffer, so it doesn't actually touch the bh
127 * itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700129static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 if (uptodate) {
132 set_buffer_uptodate(bh);
133 } else {
134 /* This happens, due to failed READA attempts. */
135 clear_buffer_uptodate(bh);
136 }
137 unlock_buffer(bh);
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700138}
139
140/*
141 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
142 * unlock the buffer. This is what ll_rw_block uses too.
143 */
144void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
145{
146 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 put_bh(bh);
148}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700149EXPORT_SYMBOL(end_buffer_read_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
152{
153 char b[BDEVNAME_SIZE];
154
155 if (uptodate) {
156 set_buffer_uptodate(bh);
157 } else {
Christoph Hellwig0edd55f2010-08-18 05:29:23 -0400158 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 buffer_io_error(bh);
160 printk(KERN_WARNING "lost page write due to "
161 "I/O error on %s\n",
162 bdevname(bh->b_bdev, b));
163 }
164 set_buffer_write_io_error(bh);
165 clear_buffer_uptodate(bh);
166 }
167 unlock_buffer(bh);
168 put_bh(bh);
169}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700170EXPORT_SYMBOL(end_buffer_write_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Various filesystems appear to want __find_get_block to be non-blocking.
174 * But it's the page lock which protects the buffers. To get around this,
175 * we get exclusion from try_to_free_buffers with the blockdev mapping's
176 * private_lock.
177 *
178 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
179 * may be quite high. This code could TryLock the page, and if that
180 * succeeds, there is no need to take private_lock. (But if
181 * private_lock is contended then so is mapping->tree_lock).
182 */
183static struct buffer_head *
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -0800184__find_get_block_slow(struct block_device *bdev, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct inode *bd_inode = bdev->bd_inode;
187 struct address_space *bd_mapping = bd_inode->i_mapping;
188 struct buffer_head *ret = NULL;
189 pgoff_t index;
190 struct buffer_head *bh;
191 struct buffer_head *head;
192 struct page *page;
193 int all_mapped = 1;
194
195 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
196 page = find_get_page(bd_mapping, index);
197 if (!page)
198 goto out;
199
200 spin_lock(&bd_mapping->private_lock);
201 if (!page_has_buffers(page))
202 goto out_unlock;
203 head = page_buffers(page);
204 bh = head;
205 do {
Nikanth Karthikesan97f76d32009-04-02 16:56:46 -0700206 if (!buffer_mapped(bh))
207 all_mapped = 0;
208 else if (bh->b_blocknr == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ret = bh;
210 get_bh(bh);
211 goto out_unlock;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 bh = bh->b_this_page;
214 } while (bh != head);
215
216 /* we might be here because some of the buffers on this page are
217 * not mapped. This is due to various races between
218 * file io on the block device and getblk. It gets dealt with
219 * elsewhere, don't buffer_error if we had some unmapped buffers
220 */
221 if (all_mapped) {
Tao Ma72a2ebd2011-10-31 17:09:00 -0700222 char b[BDEVNAME_SIZE];
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 printk("__find_get_block_slow() failed. "
225 "block=%llu, b_blocknr=%llu\n",
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800226 (unsigned long long)block,
227 (unsigned long long)bh->b_blocknr);
228 printk("b_state=0x%08lx, b_size=%zu\n",
229 bh->b_state, bh->b_size);
Tao Ma72a2ebd2011-10-31 17:09:00 -0700230 printk("device %s blocksize: %d\n", bdevname(bdev, b),
231 1 << bd_inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233out_unlock:
234 spin_unlock(&bd_mapping->private_lock);
235 page_cache_release(page);
236out:
237 return ret;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
Jens Axboe5b0830c2009-09-23 19:37:09 +0200241 * Kick the writeback threads then try to free up some ZONE_NORMAL memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243static void free_more_memory(void)
244{
Mel Gorman19770b32008-04-28 02:12:18 -0700245 struct zone *zone;
Mel Gorman0e884602008-04-28 02:12:14 -0700246 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -0600248 wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 yield();
250
Mel Gorman0e884602008-04-28 02:12:14 -0700251 for_each_online_node(nid) {
Mel Gorman19770b32008-04-28 02:12:18 -0700252 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
253 gfp_zone(GFP_NOFS), NULL,
254 &zone);
255 if (zone)
Mel Gorman54a6eb52008-04-28 02:12:16 -0700256 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -0700257 GFP_NOFS, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259}
260
261/*
262 * I/O completion handler for block_read_full_page() - pages
263 * which come unlocked at the end of I/O.
264 */
265static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700268 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct buffer_head *tmp;
270 struct page *page;
271 int page_uptodate = 1;
272
273 BUG_ON(!buffer_async_read(bh));
274
275 page = bh->b_page;
276 if (uptodate) {
277 set_buffer_uptodate(bh);
278 } else {
279 clear_buffer_uptodate(bh);
Keith Mannthey08bafc02008-11-25 10:24:35 +0100280 if (!quiet_error(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 buffer_io_error(bh);
282 SetPageError(page);
283 }
284
285 /*
286 * Be _very_ careful from here on. Bad things can happen if
287 * two buffer heads end IO at almost the same time and both
288 * decide that the page is now completely done.
289 */
Nick Piggina3972202005-07-07 17:56:56 -0700290 first = page_buffers(page);
Thomas Gleixnerc32961f2011-03-18 09:18:52 +0100291 flags = bh_uptodate_lock_irqsave(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 clear_buffer_async_read(bh);
293 unlock_buffer(bh);
294 tmp = bh;
295 do {
296 if (!buffer_uptodate(tmp))
297 page_uptodate = 0;
298 if (buffer_async_read(tmp)) {
299 BUG_ON(!buffer_locked(tmp));
300 goto still_busy;
301 }
302 tmp = tmp->b_this_page;
303 } while (tmp != bh);
Thomas Gleixnerc32961f2011-03-18 09:18:52 +0100304 bh_uptodate_unlock_irqrestore(first, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * If none of the buffers had errors and they are all
308 * uptodate then we can set the page uptodate.
309 */
310 if (page_uptodate && !PageError(page))
311 SetPageUptodate(page);
312 unlock_page(page);
313 return;
314
315still_busy:
Thomas Gleixnerc32961f2011-03-18 09:18:52 +0100316 bh_uptodate_unlock_irqrestore(first, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319/*
320 * Completion handler for block_write_full_page() - pages which are unlocked
321 * during I/O, and which have PageWriteback cleared upon I/O completion.
322 */
Chris Mason35c80d52009-04-15 13:22:38 -0400323void end_buffer_async_write(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700327 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct buffer_head *tmp;
329 struct page *page;
330
331 BUG_ON(!buffer_async_write(bh));
332
333 page = bh->b_page;
334 if (uptodate) {
335 set_buffer_uptodate(bh);
336 } else {
Keith Mannthey08bafc02008-11-25 10:24:35 +0100337 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 buffer_io_error(bh);
339 printk(KERN_WARNING "lost page write due to "
340 "I/O error on %s\n",
341 bdevname(bh->b_bdev, b));
342 }
343 set_bit(AS_EIO, &page->mapping->flags);
Jan Kara58ff4072006-10-17 00:10:19 -0700344 set_buffer_write_io_error(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 clear_buffer_uptodate(bh);
346 SetPageError(page);
347 }
348
Nick Piggina3972202005-07-07 17:56:56 -0700349 first = page_buffers(page);
Thomas Gleixnerc32961f2011-03-18 09:18:52 +0100350 flags = bh_uptodate_lock_irqsave(first);
Nick Piggina3972202005-07-07 17:56:56 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 clear_buffer_async_write(bh);
353 unlock_buffer(bh);
354 tmp = bh->b_this_page;
355 while (tmp != bh) {
356 if (buffer_async_write(tmp)) {
357 BUG_ON(!buffer_locked(tmp));
358 goto still_busy;
359 }
360 tmp = tmp->b_this_page;
361 }
Thomas Gleixnerc32961f2011-03-18 09:18:52 +0100362 bh_uptodate_unlock_irqrestore(first, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 end_page_writeback(page);
364 return;
365
366still_busy:
Thomas Gleixnerc32961f2011-03-18 09:18:52 +0100367 bh_uptodate_unlock_irqrestore(first, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700369EXPORT_SYMBOL(end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/*
372 * If a page's buffers are under async readin (end_buffer_async_read
373 * completion) then there is a possibility that another thread of
374 * control could lock one of the buffers after it has completed
375 * but while some of the other buffers have not completed. This
376 * locked buffer would confuse end_buffer_async_read() into not unlocking
377 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
378 * that this buffer is not under async I/O.
379 *
380 * The page comes unlocked when it has no locked buffer_async buffers
381 * left.
382 *
383 * PageLocked prevents anyone starting new async I/O reads any of
384 * the buffers.
385 *
386 * PageWriteback is used to prevent simultaneous writeout of the same
387 * page.
388 *
389 * PageLocked prevents anyone from starting writeback of a page which is
390 * under read I/O (PageWriteback is only ever set against a locked page).
391 */
392static void mark_buffer_async_read(struct buffer_head *bh)
393{
394 bh->b_end_io = end_buffer_async_read;
395 set_buffer_async_read(bh);
396}
397
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700398static void mark_buffer_async_write_endio(struct buffer_head *bh,
399 bh_end_io_t *handler)
Chris Mason35c80d52009-04-15 13:22:38 -0400400{
401 bh->b_end_io = handler;
402 set_buffer_async_write(bh);
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405void mark_buffer_async_write(struct buffer_head *bh)
406{
Chris Mason35c80d52009-04-15 13:22:38 -0400407 mark_buffer_async_write_endio(bh, end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409EXPORT_SYMBOL(mark_buffer_async_write);
410
411
412/*
413 * fs/buffer.c contains helper functions for buffer-backed address space's
414 * fsync functions. A common requirement for buffer-based filesystems is
415 * that certain data from the backing blockdev needs to be written out for
416 * a successful fsync(). For example, ext2 indirect blocks need to be
417 * written back and waited upon before fsync() returns.
418 *
419 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
420 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
421 * management of a list of dependent buffers at ->i_mapping->private_list.
422 *
423 * Locking is a little subtle: try_to_free_buffers() will remove buffers
424 * from their controlling inode's queue when they are being freed. But
425 * try_to_free_buffers() will be operating against the *blockdev* mapping
426 * at the time, not against the S_ISREG file which depends on those buffers.
427 * So the locking for private_list is via the private_lock in the address_space
428 * which backs the buffers. Which is different from the address_space
429 * against which the buffers are listed. So for a particular address_space,
430 * mapping->private_lock does *not* protect mapping->private_list! In fact,
431 * mapping->private_list will always be protected by the backing blockdev's
432 * ->private_lock.
433 *
434 * Which introduces a requirement: all buffers on an address_space's
435 * ->private_list must be from the same address_space: the blockdev's.
436 *
437 * address_spaces which do not place buffers at ->private_list via these
438 * utility functions are free to use private_lock and private_list for
439 * whatever they want. The only requirement is that list_empty(private_list)
440 * be true at clear_inode() time.
441 *
442 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
443 * filesystems should do that. invalidate_inode_buffers() should just go
444 * BUG_ON(!list_empty).
445 *
446 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
447 * take an address_space, not an inode. And it should be called
448 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
449 * queued up.
450 *
451 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
452 * list if it is already on a list. Because if the buffer is on a list,
453 * it *must* already be on the right one. If not, the filesystem is being
454 * silly. This will save a ton of locking. But first we have to ensure
455 * that buffers are taken *off* the old inode's list when they are freed
456 * (presumably in truncate). That requires careful auditing of all
457 * filesystems (do it inside bforget()). It could also be done by bringing
458 * b_inode back.
459 */
460
461/*
462 * The buffer's backing address_space's private_lock must be held
463 */
Thomas Petazzonidbacefc2008-07-29 22:33:47 -0700464static void __remove_assoc_queue(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -0700467 WARN_ON(!bh->b_assoc_map);
468 if (buffer_write_io_error(bh))
469 set_bit(AS_EIO, &bh->b_assoc_map->flags);
470 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473int inode_has_buffers(struct inode *inode)
474{
475 return !list_empty(&inode->i_data.private_list);
476}
477
478/*
479 * osync is designed to support O_SYNC io. It waits synchronously for
480 * all already-submitted IO to complete, but does not queue any new
481 * writes to the disk.
482 *
483 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
484 * you dirty the buffers, and then use osync_inode_buffers to wait for
485 * completion. Any other dirty buffers which are not yet queued for
486 * write will not be flushed to disk by the osync.
487 */
488static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
489{
490 struct buffer_head *bh;
491 struct list_head *p;
492 int err = 0;
493
494 spin_lock(lock);
495repeat:
496 list_for_each_prev(p, list) {
497 bh = BH_ENTRY(p);
498 if (buffer_locked(bh)) {
499 get_bh(bh);
500 spin_unlock(lock);
501 wait_on_buffer(bh);
502 if (!buffer_uptodate(bh))
503 err = -EIO;
504 brelse(bh);
505 spin_lock(lock);
506 goto repeat;
507 }
508 }
509 spin_unlock(lock);
510 return err;
511}
512
Al Viro01a05b32010-03-23 06:06:58 -0400513static void do_thaw_one(struct super_block *sb, void *unused)
514{
515 char b[BDEVNAME_SIZE];
516 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
517 printk(KERN_WARNING "Emergency Thaw on %s\n",
518 bdevname(sb->s_bdev, b));
519}
520
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700521static void do_thaw_all(struct work_struct *work)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700522{
Al Viro01a05b32010-03-23 06:06:58 -0400523 iterate_supers(do_thaw_one, NULL);
Jens Axboe053c5252009-04-08 13:44:08 +0200524 kfree(work);
Eric Sandeenc2d75432009-03-31 15:23:46 -0700525 printk(KERN_WARNING "Emergency Thaw complete\n");
526}
527
528/**
529 * emergency_thaw_all -- forcibly thaw every frozen filesystem
530 *
531 * Used for emergency unfreeze of all filesystems via SysRq
532 */
533void emergency_thaw_all(void)
534{
Jens Axboe053c5252009-04-08 13:44:08 +0200535 struct work_struct *work;
536
537 work = kmalloc(sizeof(*work), GFP_ATOMIC);
538 if (work) {
539 INIT_WORK(work, do_thaw_all);
540 schedule_work(work);
541 }
Eric Sandeenc2d75432009-03-31 15:23:46 -0700542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800545 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
Martin Waitz67be2dd2005-05-01 08:59:26 -0700546 * @mapping: the mapping which wants those buffers written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
548 * Starts I/O against the buffers at mapping->private_list, and waits upon
549 * that I/O.
550 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700551 * Basically, this is a convenience function for fsync().
552 * @mapping is a file or directory which needs those buffers to be written for
553 * a successful fsync().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555int sync_mapping_buffers(struct address_space *mapping)
556{
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800557 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
560 return 0;
561
562 return fsync_buffers_list(&buffer_mapping->private_lock,
563 &mapping->private_list);
564}
565EXPORT_SYMBOL(sync_mapping_buffers);
566
567/*
568 * Called when we've recently written block `bblock', and it is known that
569 * `bblock' was for a buffer_boundary() buffer. This means that the block at
570 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
571 * dirty, schedule it for IO. So that indirects merge nicely with their data.
572 */
573void write_boundary_block(struct block_device *bdev,
574 sector_t bblock, unsigned blocksize)
575{
576 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
577 if (bh) {
578 if (buffer_dirty(bh))
579 ll_rw_block(WRITE, 1, &bh);
580 put_bh(bh);
581 }
582}
583
584void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
585{
586 struct address_space *mapping = inode->i_mapping;
587 struct address_space *buffer_mapping = bh->b_page->mapping;
588
589 mark_buffer_dirty(bh);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800590 if (!mapping->private_data) {
591 mapping->private_data = buffer_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 } else {
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800593 BUG_ON(mapping->private_data != buffer_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Jan Kara535ee2f2008-02-08 04:21:59 -0800595 if (!bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_lock(&buffer_mapping->private_lock);
597 list_move_tail(&bh->b_assoc_buffers,
598 &mapping->private_list);
Jan Kara58ff4072006-10-17 00:10:19 -0700599 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_unlock(&buffer_mapping->private_lock);
601 }
602}
603EXPORT_SYMBOL(mark_buffer_dirty_inode);
604
605/*
Nick Piggin787d2212007-07-17 04:03:34 -0700606 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
607 * dirty.
608 *
609 * If warn is true, then emit a warning if the page is not uptodate and has
610 * not been truncated.
611 */
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700612static void __set_page_dirty(struct page *page,
Nick Piggin787d2212007-07-17 04:03:34 -0700613 struct address_space *mapping, int warn)
614{
KOSAKI Motohiro7e405af2014-02-06 12:04:28 -0800615 unsigned long flags;
616
617 spin_lock_irqsave(&mapping->tree_lock, flags);
Nick Piggin787d2212007-07-17 04:03:34 -0700618 if (page->mapping) { /* Race with truncate? */
619 WARN_ON_ONCE(warn && !PageUptodate(page));
Edward Shishkine3a7cca2009-03-31 15:19:39 -0700620 account_page_dirtied(page, mapping);
Nick Piggin787d2212007-07-17 04:03:34 -0700621 radix_tree_tag_set(&mapping->page_tree,
622 page_index(page), PAGECACHE_TAG_DIRTY);
623 }
KOSAKI Motohiro7e405af2014-02-06 12:04:28 -0800624 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Nick Piggin787d2212007-07-17 04:03:34 -0700625 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Nick Piggin787d2212007-07-17 04:03:34 -0700626}
627
628/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * Add a page to the dirty page list.
630 *
631 * It is a sad fact of life that this function is called from several places
632 * deeply under spinlocking. It may not sleep.
633 *
634 * If the page has buffers, the uptodate buffers are set dirty, to preserve
635 * dirty-state coherency between the page and the buffers. It the page does
636 * not have buffers then when they are later attached they will all be set
637 * dirty.
638 *
639 * The buffers are dirtied before the page is dirtied. There's a small race
640 * window in which a writepage caller may see the page cleanness but not the
641 * buffer dirtiness. That's fine. If this code were to set the page dirty
642 * before the buffers, a concurrent writepage caller could clear the page dirty
643 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
644 * page on the dirty page list.
645 *
646 * We use private_lock to lock against try_to_free_buffers while using the
647 * page's buffer list. Also use this to protect against clean buffers being
648 * added to the page after it was set dirty.
649 *
650 * FIXME: may need to call ->reservepage here as well. That's rather up to the
651 * address_space though.
652 */
653int __set_page_dirty_buffers(struct page *page)
654{
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700655 int newly_dirty;
Nick Piggin787d2212007-07-17 04:03:34 -0700656 struct address_space *mapping = page_mapping(page);
Nick Pigginebf7a222006-10-10 04:36:54 +0200657
658 if (unlikely(!mapping))
659 return !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 spin_lock(&mapping->private_lock);
662 if (page_has_buffers(page)) {
663 struct buffer_head *head = page_buffers(page);
664 struct buffer_head *bh = head;
665
666 do {
667 set_buffer_dirty(bh);
668 bh = bh->b_this_page;
669 } while (bh != head);
670 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700671 newly_dirty = !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_unlock(&mapping->private_lock);
673
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700674 if (newly_dirty)
675 __set_page_dirty(page, mapping, 1);
676 return newly_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678EXPORT_SYMBOL(__set_page_dirty_buffers);
679
680/*
681 * Write out and wait upon a list of buffers.
682 *
683 * We have conflicting pressures: we want to make sure that all
684 * initially dirty buffers get waited on, but that any subsequently
685 * dirtied buffers don't. After all, we don't want fsync to last
686 * forever if somebody is actively writing to the file.
687 *
688 * Do this in two main stages: first we copy dirty buffers to a
689 * temporary inode list, queueing the writes as we go. Then we clean
690 * up, waiting for those writes to complete.
691 *
692 * During this second stage, any subsequent updates to the file may end
693 * up refiling the buffer on the original inode's dirty list again, so
694 * there is a chance we will end up with a buffer queued for write but
695 * not yet completed on that list. So, as a final cleanup we go through
696 * the osync code to catch these locked, dirty buffers without requeuing
697 * any newly dirty buffers for write.
698 */
699static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
700{
701 struct buffer_head *bh;
702 struct list_head tmp;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100703 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int err = 0, err2;
Jens Axboe4ee24912011-03-17 10:51:40 +0100705 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 INIT_LIST_HEAD(&tmp);
Jens Axboe4ee24912011-03-17 10:51:40 +0100708 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 spin_lock(lock);
711 while (!list_empty(list)) {
712 bh = BH_ENTRY(list->next);
Jan Kara535ee2f2008-02-08 04:21:59 -0800713 mapping = bh->b_assoc_map;
Jan Kara58ff4072006-10-17 00:10:19 -0700714 __remove_assoc_queue(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800715 /* Avoid race with mark_buffer_dirty_inode() which does
716 * a lockless check and we rely on seeing the dirty bit */
717 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (buffer_dirty(bh) || buffer_locked(bh)) {
719 list_add(&bh->b_assoc_buffers, &tmp);
Jan Kara535ee2f2008-02-08 04:21:59 -0800720 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (buffer_dirty(bh)) {
722 get_bh(bh);
723 spin_unlock(lock);
724 /*
725 * Ensure any pending I/O completes so that
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200726 * write_dirty_buffer() actually writes the
727 * current contents - it is a noop if I/O is
728 * still in flight on potentially older
729 * contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Jens Axboe721a9602011-03-09 11:56:30 +0100731 write_dirty_buffer(bh, WRITE_SYNC);
Jens Axboe9cf6b722009-04-06 14:48:03 +0200732
733 /*
734 * Kick off IO for the previous mapping. Note
735 * that we will not run the very last mapping,
736 * wait_on_buffer() will do that for us
737 * through sync_buffer().
738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 brelse(bh);
740 spin_lock(lock);
741 }
742 }
743 }
744
Jens Axboe4ee24912011-03-17 10:51:40 +0100745 spin_unlock(lock);
746 blk_finish_plug(&plug);
747 spin_lock(lock);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 while (!list_empty(&tmp)) {
750 bh = BH_ENTRY(tmp.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 get_bh(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800752 mapping = bh->b_assoc_map;
753 __remove_assoc_queue(bh);
754 /* Avoid race with mark_buffer_dirty_inode() which does
755 * a lockless check and we rely on seeing the dirty bit */
756 smp_mb();
757 if (buffer_dirty(bh)) {
758 list_add(&bh->b_assoc_buffers,
Jan Karae3892292008-03-04 14:28:33 -0800759 &mapping->private_list);
Jan Kara535ee2f2008-02-08 04:21:59 -0800760 bh->b_assoc_map = mapping;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 spin_unlock(lock);
763 wait_on_buffer(bh);
764 if (!buffer_uptodate(bh))
765 err = -EIO;
766 brelse(bh);
767 spin_lock(lock);
768 }
769
770 spin_unlock(lock);
771 err2 = osync_buffers_list(lock, list);
772 if (err)
773 return err;
774 else
775 return err2;
776}
777
778/*
779 * Invalidate any and all dirty buffers on a given inode. We are
780 * probably unmounting the fs, but that doesn't mean we have already
781 * done a sync(). Just drop the buffers from the inode list.
782 *
783 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
784 * assumes that all the buffers are against the blockdev. Not true
785 * for reiserfs.
786 */
787void invalidate_inode_buffers(struct inode *inode)
788{
789 if (inode_has_buffers(inode)) {
790 struct address_space *mapping = &inode->i_data;
791 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800792 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 spin_lock(&buffer_mapping->private_lock);
795 while (!list_empty(list))
796 __remove_assoc_queue(BH_ENTRY(list->next));
797 spin_unlock(&buffer_mapping->private_lock);
798 }
799}
Jan Kara52b19ac2008-09-23 18:24:08 +0200800EXPORT_SYMBOL(invalidate_inode_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802/*
803 * Remove any clean buffers from the inode's buffer list. This is called
804 * when we're trying to free the inode itself. Those buffers can pin it.
805 *
806 * Returns true if all buffers were removed.
807 */
808int remove_inode_buffers(struct inode *inode)
809{
810 int ret = 1;
811
812 if (inode_has_buffers(inode)) {
813 struct address_space *mapping = &inode->i_data;
814 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800815 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 spin_lock(&buffer_mapping->private_lock);
818 while (!list_empty(list)) {
819 struct buffer_head *bh = BH_ENTRY(list->next);
820 if (buffer_dirty(bh)) {
821 ret = 0;
822 break;
823 }
824 __remove_assoc_queue(bh);
825 }
826 spin_unlock(&buffer_mapping->private_lock);
827 }
828 return ret;
829}
830
831/*
832 * Create the appropriate buffers when given a page for data area and
833 * the size of each buffer.. Use the bh->b_this_page linked list to
834 * follow the buffers created. Return NULL if unable to create more
835 * buffers.
836 *
837 * The retry flag is used to differentiate async IO (paging, swapping)
838 * which may not fail from ordinary buffer allocations.
839 */
840struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
841 int retry)
842{
843 struct buffer_head *bh, *head;
844 long offset;
845
846try_again:
847 head = NULL;
848 offset = PAGE_SIZE;
849 while ((offset -= size) >= 0) {
850 bh = alloc_buffer_head(GFP_NOFS);
851 if (!bh)
852 goto no_grow;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 bh->b_this_page = head;
855 bh->b_blocknr = -1;
856 head = bh;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 bh->b_size = size;
859
860 /* Link the buffer to its page */
861 set_bh_page(bh, page, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 return head;
864/*
865 * In case anything failed, we just free everything we got.
866 */
867no_grow:
868 if (head) {
869 do {
870 bh = head;
871 head = head->b_this_page;
872 free_buffer_head(bh);
873 } while (head);
874 }
875
876 /*
877 * Return failure for non-async IO requests. Async IO requests
878 * are not allowed to fail, so we have to wait until buffer heads
879 * become available. But we don't want tasks sleeping with
880 * partially complete buffers, so all were released above.
881 */
882 if (!retry)
883 return NULL;
884
885 /* We're _really_ low on memory. Now we just
886 * wait for old buffer heads to become free due to
887 * finishing IO. Since this is an async request and
888 * the reserve list is empty, we're sure there are
889 * async buffer heads in use.
890 */
891 free_more_memory();
892 goto try_again;
893}
894EXPORT_SYMBOL_GPL(alloc_page_buffers);
895
896static inline void
897link_dev_buffers(struct page *page, struct buffer_head *head)
898{
899 struct buffer_head *bh, *tail;
900
901 bh = head;
902 do {
903 tail = bh;
904 bh = bh->b_this_page;
905 } while (bh);
906 tail->b_this_page = head;
907 attach_page_buffers(page, head);
908}
909
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800910static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
911{
912 sector_t retval = ~((sector_t)0);
913 loff_t sz = i_size_read(bdev->bd_inode);
914
915 if (sz) {
916 unsigned int sizebits = blksize_bits(size);
917 retval = (sz >> sizebits);
918 }
919 return retval;
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/*
923 * Initialise the state of a blockdev page's buffers.
924 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200925static sector_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926init_page_buffers(struct page *page, struct block_device *bdev,
927 sector_t block, int size)
928{
929 struct buffer_head *head = page_buffers(page);
930 struct buffer_head *bh = head;
931 int uptodate = PageUptodate(page);
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800932 sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 do {
935 if (!buffer_mapped(bh)) {
936 init_buffer(bh, NULL, NULL);
937 bh->b_bdev = bdev;
938 bh->b_blocknr = block;
939 if (uptodate)
940 set_buffer_uptodate(bh);
Jeff Moyer080399a2012-05-11 16:34:10 +0200941 if (block < end_block)
942 set_buffer_mapped(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 block++;
945 bh = bh->b_this_page;
946 } while (bh != head);
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200947
948 /*
949 * Caller needs to validate requested block against end of device.
950 */
951 return end_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954/*
955 * Create the page-cache page that contains the requested block.
956 *
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200957 * This is used purely for blockdev mappings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200959static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960grow_dev_page(struct block_device *bdev, sector_t block,
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200961 pgoff_t index, int size, int sizebits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 struct inode *inode = bdev->bd_inode;
964 struct page *page;
965 struct buffer_head *bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200966 sector_t end_block;
967 int ret = 0; /* Will call free_more_memory() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Christoph Lameterea125892007-05-16 22:11:21 -0700969 page = find_or_create_page(inode->i_mapping, index,
Mel Gorman769848c2007-07-17 04:03:05 -0700970 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!page)
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Eric Sesterhenne827f922006-03-26 18:24:46 +0200974 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (page_has_buffers(page)) {
977 bh = page_buffers(page);
978 if (bh->b_size == size) {
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200979 end_block = init_page_buffers(page, bdev,
Anton Altaparmakovc2d33172014-09-22 01:53:03 +0100980 (sector_t)index << sizebits,
981 size);
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200982 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 if (!try_to_free_buffers(page))
985 goto failed;
986 }
987
988 /*
989 * Allocate some buffers for this page
990 */
991 bh = alloc_page_buffers(page, size, 0);
992 if (!bh)
993 goto failed;
994
995 /*
996 * Link the page to the buffers and initialise them. Take the
997 * lock to be atomic wrt __find_get_block(), which does not
998 * run under the page lock.
999 */
1000 spin_lock(&inode->i_mapping->private_lock);
1001 link_dev_buffers(page, bh);
Anton Altaparmakovc2d33172014-09-22 01:53:03 +01001002 end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
1003 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 spin_unlock(&inode->i_mapping->private_lock);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001005done:
1006 ret = (block < end_block) ? 1 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 unlock_page(page);
1009 page_cache_release(page);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013/*
1014 * Create buffers for the specified block device block's page. If
1015 * that page was dirty, the buffers are set dirty also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001017static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018grow_buffers(struct block_device *bdev, sector_t block, int size)
1019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 pgoff_t index;
1021 int sizebits;
1022
1023 sizebits = -1;
1024 do {
1025 sizebits++;
1026 } while ((size << sizebits) < PAGE_SIZE);
1027
1028 index = block >> sizebits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Andrew Mortone5657932006-10-11 01:21:46 -07001030 /*
1031 * Check for a block which wants to lie outside our maximum possible
1032 * pagecache index. (this comparison is done using sector_t types).
1033 */
1034 if (unlikely(index != block >> sizebits)) {
1035 char b[BDEVNAME_SIZE];
1036
1037 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1038 "device %s\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001039 __func__, (unsigned long long)block,
Andrew Mortone5657932006-10-11 01:21:46 -07001040 bdevname(bdev, b));
1041 return -EIO;
1042 }
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Create a page with the proper size buffers.. */
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001045 return grow_dev_page(bdev, block, index, size, sizebits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Adrian Bunk75c96f82005-05-05 16:16:09 -07001048static struct buffer_head *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049__getblk_slow(struct block_device *bdev, sector_t block, int size)
1050{
1051 /* Size must be multiple of hard sectorsize */
Martin K. Petersene1defc42009-05-22 17:17:49 -04001052 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 (size < 512 || size > PAGE_SIZE))) {
1054 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1055 size);
Martin K. Petersene1defc42009-05-22 17:17:49 -04001056 printk(KERN_ERR "logical block size: %d\n",
1057 bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 dump_stack();
1060 return NULL;
1061 }
1062
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001063 for (;;) {
1064 struct buffer_head *bh;
1065 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 bh = __find_get_block(bdev, block, size);
1068 if (bh)
1069 return bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001070
1071 ret = grow_buffers(bdev, block, size);
1072 if (ret < 0)
1073 return NULL;
1074 if (ret == 0)
1075 free_more_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077}
1078
1079/*
1080 * The relationship between dirty buffers and dirty pages:
1081 *
1082 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1083 * the page is tagged dirty in its radix tree.
1084 *
1085 * At all times, the dirtiness of the buffers represents the dirtiness of
1086 * subsections of the page. If the page has buffers, the page dirty bit is
1087 * merely a hint about the true dirty state.
1088 *
1089 * When a page is set dirty in its entirety, all its buffers are marked dirty
1090 * (if the page has buffers).
1091 *
1092 * When a buffer is marked dirty, its page is dirtied, but the page's other
1093 * buffers are not.
1094 *
1095 * Also. When blockdev buffers are explicitly read with bread(), they
1096 * individually become uptodate. But their backing page remains not
1097 * uptodate - even if all of its buffers are uptodate. A subsequent
1098 * block_read_full_page() against that page will discover all the uptodate
1099 * buffers, will set the page uptodate and will perform no I/O.
1100 */
1101
1102/**
1103 * mark_buffer_dirty - mark a buffer_head as needing writeout
Martin Waitz67be2dd2005-05-01 08:59:26 -07001104 * @bh: the buffer_head to mark dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *
1106 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1107 * backing page dirty, then tag the page as dirty in its address_space's radix
1108 * tree and then attach the address_space's inode to its superblock's dirty
1109 * inode list.
1110 *
1111 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
Dave Chinner250df6e2011-03-22 22:23:36 +11001112 * mapping->tree_lock and mapping->host->i_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001114void mark_buffer_dirty(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Nick Piggin787d2212007-07-17 04:03:34 -07001116 WARN_ON_ONCE(!buffer_uptodate(bh));
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001117
Tejun Heo5305cb82013-01-11 13:06:36 -08001118 trace_block_dirty_buffer(bh);
1119
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001120 /*
1121 * Very *carefully* optimize the it-is-already-dirty case.
1122 *
1123 * Don't let the final "is it dirty" escape to before we
1124 * perhaps modified the buffer.
1125 */
1126 if (buffer_dirty(bh)) {
1127 smp_mb();
1128 if (buffer_dirty(bh))
1129 return;
1130 }
1131
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001132 if (!test_set_buffer_dirty(bh)) {
1133 struct page *page = bh->b_page;
Linus Torvalds8e9d78e2009-08-21 17:40:08 -07001134 if (!TestSetPageDirty(page)) {
1135 struct address_space *mapping = page_mapping(page);
1136 if (mapping)
1137 __set_page_dirty(page, mapping, 0);
1138 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001141EXPORT_SYMBOL(mark_buffer_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143/*
1144 * Decrement a buffer_head's reference count. If all buffers against a page
1145 * have zero reference count, are clean and unlocked, and if the page is clean
1146 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1147 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1148 * a page but it ends up not being freed, and buffers may later be reattached).
1149 */
1150void __brelse(struct buffer_head * buf)
1151{
1152 if (atomic_read(&buf->b_count)) {
1153 put_bh(buf);
1154 return;
1155 }
Arjan van de Ven5c752ad2008-07-25 19:45:40 -07001156 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001158EXPORT_SYMBOL(__brelse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160/*
1161 * bforget() is like brelse(), except it discards any
1162 * potentially dirty data.
1163 */
1164void __bforget(struct buffer_head *bh)
1165{
1166 clear_buffer_dirty(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -08001167 if (bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct address_space *buffer_mapping = bh->b_page->mapping;
1169
1170 spin_lock(&buffer_mapping->private_lock);
1171 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -07001172 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_unlock(&buffer_mapping->private_lock);
1174 }
1175 __brelse(bh);
1176}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001177EXPORT_SYMBOL(__bforget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179static struct buffer_head *__bread_slow(struct buffer_head *bh)
1180{
1181 lock_buffer(bh);
1182 if (buffer_uptodate(bh)) {
1183 unlock_buffer(bh);
1184 return bh;
1185 } else {
1186 get_bh(bh);
1187 bh->b_end_io = end_buffer_read_sync;
1188 submit_bh(READ, bh);
1189 wait_on_buffer(bh);
1190 if (buffer_uptodate(bh))
1191 return bh;
1192 }
1193 brelse(bh);
1194 return NULL;
1195}
1196
1197/*
1198 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1199 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1200 * refcount elevated by one when they're in an LRU. A buffer can only appear
1201 * once in a particular CPU's LRU. A single buffer can be present in multiple
1202 * CPU's LRUs at the same time.
1203 *
1204 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1205 * sb_find_get_block().
1206 *
1207 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1208 * a local interrupt disable for that.
1209 */
1210
1211#define BH_LRU_SIZE 8
1212
1213struct bh_lru {
1214 struct buffer_head *bhs[BH_LRU_SIZE];
1215};
1216
1217static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1218
1219#ifdef CONFIG_SMP
1220#define bh_lru_lock() local_irq_disable()
1221#define bh_lru_unlock() local_irq_enable()
1222#else
1223#define bh_lru_lock() preempt_disable()
1224#define bh_lru_unlock() preempt_enable()
1225#endif
1226
1227static inline void check_irqs_on(void)
1228{
1229#ifdef irqs_disabled
1230 BUG_ON(irqs_disabled());
1231#endif
1232}
1233
1234/*
1235 * The LRU management algorithm is dopey-but-simple. Sorry.
1236 */
1237static void bh_lru_install(struct buffer_head *bh)
1238{
1239 struct buffer_head *evictee = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 check_irqs_on();
1242 bh_lru_lock();
Christoph Lameterc7b92512010-12-06 11:16:28 -06001243 if (__this_cpu_read(bh_lrus.bhs[0]) != bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct buffer_head *bhs[BH_LRU_SIZE];
1245 int in;
1246 int out = 0;
1247
1248 get_bh(bh);
1249 bhs[out++] = bh;
1250 for (in = 0; in < BH_LRU_SIZE; in++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001251 struct buffer_head *bh2 =
1252 __this_cpu_read(bh_lrus.bhs[in]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (bh2 == bh) {
1255 __brelse(bh2);
1256 } else {
1257 if (out >= BH_LRU_SIZE) {
1258 BUG_ON(evictee != NULL);
1259 evictee = bh2;
1260 } else {
1261 bhs[out++] = bh2;
1262 }
1263 }
1264 }
1265 while (out < BH_LRU_SIZE)
1266 bhs[out++] = NULL;
Christoph Lameterc7b92512010-12-06 11:16:28 -06001267 memcpy(__this_cpu_ptr(&bh_lrus.bhs), bhs, sizeof(bhs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 bh_lru_unlock();
1270
1271 if (evictee)
1272 __brelse(evictee);
1273}
1274
1275/*
1276 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1277 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001278static struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001279lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct buffer_head *ret = NULL;
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001282 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 check_irqs_on();
1285 bh_lru_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 for (i = 0; i < BH_LRU_SIZE; i++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001287 struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (bh && bh->b_bdev == bdev &&
1290 bh->b_blocknr == block && bh->b_size == size) {
1291 if (i) {
1292 while (i) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001293 __this_cpu_write(bh_lrus.bhs[i],
1294 __this_cpu_read(bh_lrus.bhs[i - 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 i--;
1296 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06001297 __this_cpu_write(bh_lrus.bhs[0], bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 get_bh(bh);
1300 ret = bh;
1301 break;
1302 }
1303 }
1304 bh_lru_unlock();
1305 return ret;
1306}
1307
1308/*
1309 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1310 * it in the LRU and mark it as accessed. If it is not present then return
1311 * NULL
1312 */
1313struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001314__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1317
1318 if (bh == NULL) {
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001319 bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (bh)
1321 bh_lru_install(bh);
1322 }
1323 if (bh)
1324 touch_buffer(bh);
1325 return bh;
1326}
1327EXPORT_SYMBOL(__find_get_block);
1328
1329/*
1330 * __getblk will locate (and, if necessary, create) the buffer_head
1331 * which corresponds to the passed block_device, block and size. The
1332 * returned buffer has its reference count incremented.
1333 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1335 * attempt is failing. FIXME, perhaps?
1336 */
1337struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001338__getblk(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct buffer_head *bh = __find_get_block(bdev, block, size);
1341
1342 might_sleep();
1343 if (bh == NULL)
1344 bh = __getblk_slow(bdev, block, size);
1345 return bh;
1346}
1347EXPORT_SYMBOL(__getblk);
1348
1349/*
1350 * Do async read-ahead on a buffer..
1351 */
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001352void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct buffer_head *bh = __getblk(bdev, block, size);
Andrew Mortona3e713b2005-10-30 15:03:15 -08001355 if (likely(bh)) {
1356 ll_rw_block(READA, 1, &bh);
1357 brelse(bh);
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360EXPORT_SYMBOL(__breadahead);
1361
1362/**
1363 * __bread() - reads a specified block and returns the bh
Martin Waitz67be2dd2005-05-01 08:59:26 -07001364 * @bdev: the block_device to read from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * @block: number of block
1366 * @size: size (in bytes) to read
1367 *
1368 * Reads a specified block, and returns buffer head that contains it.
1369 * It returns NULL if the block was unreadable.
1370 */
1371struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001372__bread(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct buffer_head *bh = __getblk(bdev, block, size);
1375
Andrew Mortona3e713b2005-10-30 15:03:15 -08001376 if (likely(bh) && !buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 bh = __bread_slow(bh);
1378 return bh;
1379}
1380EXPORT_SYMBOL(__bread);
1381
1382/*
1383 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1384 * This doesn't race because it runs in each cpu either in irq
1385 * or with preempt disabled.
1386 */
1387static void invalidate_bh_lru(void *arg)
1388{
1389 struct bh_lru *b = &get_cpu_var(bh_lrus);
1390 int i;
1391
1392 for (i = 0; i < BH_LRU_SIZE; i++) {
1393 brelse(b->bhs[i]);
1394 b->bhs[i] = NULL;
1395 }
1396 put_cpu_var(bh_lrus);
1397}
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001398
1399static bool has_bh_in_lru(int cpu, void *dummy)
1400{
1401 struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
1402 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001404 for (i = 0; i < BH_LRU_SIZE; i++) {
1405 if (b->bhs[i])
1406 return 1;
1407 }
1408
1409 return 0;
1410}
1411
Peter Zijlstraf9a14392007-05-06 14:49:55 -07001412void invalidate_bh_lrus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001414 on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
Nick Piggin9db55792008-02-08 04:19:49 -08001416EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418void set_bh_page(struct buffer_head *bh,
1419 struct page *page, unsigned long offset)
1420{
1421 bh->b_page = page;
Eric Sesterhenne827f922006-03-26 18:24:46 +02001422 BUG_ON(offset >= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (PageHighMem(page))
1424 /*
1425 * This catches illegal uses and preserves the offset:
1426 */
1427 bh->b_data = (char *)(0 + offset);
1428 else
1429 bh->b_data = page_address(page) + offset;
1430}
1431EXPORT_SYMBOL(set_bh_page);
1432
1433/*
1434 * Called when truncating a buffer on a page completely.
1435 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001436static void discard_buffer(struct buffer_head * bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 lock_buffer(bh);
1439 clear_buffer_dirty(bh);
1440 bh->b_bdev = NULL;
1441 clear_buffer_mapped(bh);
1442 clear_buffer_req(bh);
1443 clear_buffer_new(bh);
1444 clear_buffer_delay(bh);
David Chinner33a266d2007-02-12 00:51:41 -08001445 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 unlock_buffer(bh);
1447}
1448
1449/**
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001450 * block_invalidatepage - invalidate part or all of a buffer-backed page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *
1452 * @page: the page which is affected
1453 * @offset: the index of the truncation point
1454 *
1455 * block_invalidatepage() is called when all or part of the page has become
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001456 * invalidated by a truncate operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * block_invalidatepage() does not have to release all buffers, but it must
1459 * ensure that no dirty buffer is left outside @offset and that no I/O
1460 * is underway against any of the blocks which are outside the truncation
1461 * point. Because the caller is about to free (and possibly reuse) those
1462 * blocks on-disk.
1463 */
NeilBrown2ff28e22006-03-26 01:37:18 -08001464void block_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct buffer_head *head, *bh, *next;
1467 unsigned int curr_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 BUG_ON(!PageLocked(page));
1470 if (!page_has_buffers(page))
1471 goto out;
1472
1473 head = page_buffers(page);
1474 bh = head;
1475 do {
1476 unsigned int next_off = curr_off + bh->b_size;
1477 next = bh->b_this_page;
1478
1479 /*
1480 * is this block fully invalidated?
1481 */
1482 if (offset <= curr_off)
1483 discard_buffer(bh);
1484 curr_off = next_off;
1485 bh = next;
1486 } while (bh != head);
1487
1488 /*
1489 * We release buffers only if the entire page is being invalidated.
1490 * The get_block cached value has been unconditionally invalidated,
1491 * so real IO is not possible anymore.
1492 */
1493 if (offset == 0)
NeilBrown2ff28e22006-03-26 01:37:18 -08001494 try_to_release_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495out:
NeilBrown2ff28e22006-03-26 01:37:18 -08001496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498EXPORT_SYMBOL(block_invalidatepage);
1499
1500/*
1501 * We attach and possibly dirty the buffers atomically wrt
1502 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1503 * is already excluded via the page lock.
1504 */
1505void create_empty_buffers(struct page *page,
1506 unsigned long blocksize, unsigned long b_state)
1507{
1508 struct buffer_head *bh, *head, *tail;
1509
1510 head = alloc_page_buffers(page, blocksize, 1);
1511 bh = head;
1512 do {
1513 bh->b_state |= b_state;
1514 tail = bh;
1515 bh = bh->b_this_page;
1516 } while (bh);
1517 tail->b_this_page = head;
1518
1519 spin_lock(&page->mapping->private_lock);
1520 if (PageUptodate(page) || PageDirty(page)) {
1521 bh = head;
1522 do {
1523 if (PageDirty(page))
1524 set_buffer_dirty(bh);
1525 if (PageUptodate(page))
1526 set_buffer_uptodate(bh);
1527 bh = bh->b_this_page;
1528 } while (bh != head);
1529 }
1530 attach_page_buffers(page, head);
1531 spin_unlock(&page->mapping->private_lock);
1532}
1533EXPORT_SYMBOL(create_empty_buffers);
1534
1535/*
1536 * We are taking a block for data and we don't want any output from any
1537 * buffer-cache aliases starting from return from that function and
1538 * until the moment when something will explicitly mark the buffer
1539 * dirty (hopefully that will not happen until we will free that block ;-)
1540 * We don't even need to mark it not-uptodate - nobody can expect
1541 * anything from a newly allocated buffer anyway. We used to used
1542 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1543 * don't want to mark the alias unmapped, for example - it would confuse
1544 * anyone who might pick it with bread() afterwards...
1545 *
1546 * Also.. Note that bforget() doesn't lock the buffer. So there can
1547 * be writeout I/O going on against recently-freed buffers. We don't
1548 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1549 * only if we really need to. That happens here.
1550 */
1551void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1552{
1553 struct buffer_head *old_bh;
1554
1555 might_sleep();
1556
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001557 old_bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (old_bh) {
1559 clear_buffer_dirty(old_bh);
1560 wait_on_buffer(old_bh);
1561 clear_buffer_req(old_bh);
1562 __brelse(old_bh);
1563 }
1564}
1565EXPORT_SYMBOL(unmap_underlying_metadata);
1566
1567/*
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001568 * Size is a power-of-two in the range 512..PAGE_SIZE,
1569 * and the case we care about most is PAGE_SIZE.
1570 *
1571 * So this *could* possibly be written with those
1572 * constraints in mind (relevant mostly if some
1573 * architecture has a slow bit-scan instruction)
1574 */
1575static inline int block_size_bits(unsigned int blocksize)
1576{
1577 return ilog2(blocksize);
1578}
1579
1580static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
1581{
1582 BUG_ON(!PageLocked(page));
1583
1584 if (!page_has_buffers(page))
1585 create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
1586 return page_buffers(page);
1587}
1588
1589/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * NOTE! All mapped/uptodate combinations are valid:
1591 *
1592 * Mapped Uptodate Meaning
1593 *
1594 * No No "unknown" - must do get_block()
1595 * No Yes "hole" - zero-filled
1596 * Yes No "allocated" - allocated on disk, not read in
1597 * Yes Yes "valid" - allocated and up-to-date in memory.
1598 *
1599 * "Dirty" is valid only with the last case (mapped+uptodate).
1600 */
1601
1602/*
1603 * While block_write_full_page is writing back the dirty buffers under
1604 * the page lock, whoever dirtied the buffers may decide to clean them
1605 * again at any time. We handle that by only looking at the buffer
1606 * state inside lock_buffer().
1607 *
1608 * If block_write_full_page() is called for regular writeback
1609 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1610 * locked buffer. This only can happen if someone has written the buffer
1611 * directly, with submit_bh(). At the address_space level PageWriteback
1612 * prevents this contention from occurring.
Theodore Ts'o6e34eed2009-04-07 18:12:43 -04001613 *
1614 * If block_write_full_page() is called with wbc->sync_mode ==
Jens Axboe721a9602011-03-09 11:56:30 +01001615 * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
1616 * causes the writes to be flagged as synchronous writes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
1618static int __block_write_full_page(struct inode *inode, struct page *page,
Chris Mason35c80d52009-04-15 13:22:38 -04001619 get_block_t *get_block, struct writeback_control *wbc,
1620 bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 int err;
1623 sector_t block;
1624 sector_t last_block;
Andrew Mortonf0fbd5f2005-05-05 16:15:48 -07001625 struct buffer_head *bh, *head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001626 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int nr_underway = 0;
Theodore Ts'o6e34eed2009-04-07 18:12:43 -04001628 int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
Jens Axboe721a9602011-03-09 11:56:30 +01001629 WRITE_SYNC : WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001631 head = create_page_buffers(page, inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 (1 << BH_Dirty)|(1 << BH_Uptodate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /*
1635 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1636 * here, and the (potentially unmapped) buffers may become dirty at
1637 * any time. If a buffer becomes dirty here after we've inspected it
1638 * then we just miss that fact, and the page stays dirty.
1639 *
1640 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1641 * handle that here by just cleaning them.
1642 */
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 bh = head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001645 blocksize = bh->b_size;
1646 bbits = block_size_bits(blocksize);
1647
1648 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1649 last_block = (i_size_read(inode) - 1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /*
1652 * Get all the dirty buffers mapped to disk addresses and
1653 * handle any aliases from the underlying blockdev's mapping.
1654 */
1655 do {
1656 if (block > last_block) {
1657 /*
1658 * mapped buffers outside i_size will occur, because
1659 * this page can be outside i_size when there is a
1660 * truncate in progress.
1661 */
1662 /*
1663 * The buffer was zeroed by block_write_full_page()
1664 */
1665 clear_buffer_dirty(bh);
1666 set_buffer_uptodate(bh);
Alex Tomas29a814d2008-07-11 19:27:31 -04001667 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1668 buffer_dirty(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001669 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 err = get_block(inode, block, bh, 1);
1671 if (err)
1672 goto recover;
Alex Tomas29a814d2008-07-11 19:27:31 -04001673 clear_buffer_delay(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (buffer_new(bh)) {
1675 /* blockdev mappings never come here */
1676 clear_buffer_new(bh);
1677 unmap_underlying_metadata(bh->b_bdev,
1678 bh->b_blocknr);
1679 }
1680 }
1681 bh = bh->b_this_page;
1682 block++;
1683 } while (bh != head);
1684
1685 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!buffer_mapped(bh))
1687 continue;
1688 /*
1689 * If it's a fully non-blocking write attempt and we cannot
1690 * lock the buffer then redirty the page. Note that this can
Jens Axboe5b0830c2009-09-23 19:37:09 +02001691 * potentially cause a busy-wait loop from writeback threads
1692 * and kswapd activity, but those code paths have their own
1693 * higher-level throttling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
Wu Fengguang1b430be2010-10-26 14:21:26 -07001695 if (wbc->sync_mode != WB_SYNC_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 lock_buffer(bh);
Nick Pigginca5de402008-08-02 12:02:13 +02001697 } else if (!trylock_buffer(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 redirty_page_for_writepage(wbc, page);
1699 continue;
1700 }
1701 if (test_clear_buffer_dirty(bh)) {
Chris Mason35c80d52009-04-15 13:22:38 -04001702 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 } else {
1704 unlock_buffer(bh);
1705 }
1706 } while ((bh = bh->b_this_page) != head);
1707
1708 /*
1709 * The page and its buffers are protected by PageWriteback(), so we can
1710 * drop the bh refcounts early.
1711 */
1712 BUG_ON(PageWriteback(page));
1713 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 do {
1716 struct buffer_head *next = bh->b_this_page;
1717 if (buffer_async_write(bh)) {
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001718 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 nr_underway++;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 bh = next;
1722 } while (bh != head);
Andrew Morton05937ba2005-05-05 16:15:47 -07001723 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 err = 0;
1726done:
1727 if (nr_underway == 0) {
1728 /*
1729 * The page was marked dirty, but the buffers were
1730 * clean. Someone wrote them back by hand with
1731 * ll_rw_block/submit_bh. A rare case.
1732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 end_page_writeback(page);
Nick Piggin3d67f2d2007-05-06 14:49:05 -07001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
1736 * The page and buffer_heads can be released at any time from
1737 * here on.
1738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740 return err;
1741
1742recover:
1743 /*
1744 * ENOSPC, or some other error. We may already have added some
1745 * blocks to the file, so we need to write these out to avoid
1746 * exposing stale data.
1747 * The page is currently locked and not marked for writeback
1748 */
1749 bh = head;
1750 /* Recovery: lock and submit the mapped buffers */
1751 do {
Alex Tomas29a814d2008-07-11 19:27:31 -04001752 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1753 !buffer_delay(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 lock_buffer(bh);
Chris Mason35c80d52009-04-15 13:22:38 -04001755 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 } else {
1757 /*
1758 * The buffer may have been set dirty during
1759 * attachment to a dirty page.
1760 */
1761 clear_buffer_dirty(bh);
1762 }
1763 } while ((bh = bh->b_this_page) != head);
1764 SetPageError(page);
1765 BUG_ON(PageWriteback(page));
Andrew Morton7e4c3692007-05-08 00:23:27 -07001766 mapping_set_error(page->mapping, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 do {
1769 struct buffer_head *next = bh->b_this_page;
1770 if (buffer_async_write(bh)) {
1771 clear_buffer_dirty(bh);
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001772 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 nr_underway++;
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 bh = next;
1776 } while (bh != head);
Nick Pigginffda9d32007-02-20 13:57:54 -08001777 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
1779}
1780
Nick Pigginafddba42007-10-16 01:25:01 -07001781/*
1782 * If a page has any new buffers, zero them out here, and mark them uptodate
1783 * and dirty so they'll be written out (in order to prevent uninitialised
1784 * block data from leaking). And clear the new bit.
1785 */
1786void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1787{
1788 unsigned int block_start, block_end;
1789 struct buffer_head *head, *bh;
1790
1791 BUG_ON(!PageLocked(page));
1792 if (!page_has_buffers(page))
1793 return;
1794
1795 bh = head = page_buffers(page);
1796 block_start = 0;
1797 do {
1798 block_end = block_start + bh->b_size;
1799
1800 if (buffer_new(bh)) {
1801 if (block_end > from && block_start < to) {
1802 if (!PageUptodate(page)) {
1803 unsigned start, size;
1804
1805 start = max(from, block_start);
1806 size = min(to, block_end) - start;
1807
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001808 zero_user(page, start, size);
Nick Pigginafddba42007-10-16 01:25:01 -07001809 set_buffer_uptodate(bh);
1810 }
1811
1812 clear_buffer_new(bh);
1813 mark_buffer_dirty(bh);
1814 }
1815 }
1816
1817 block_start = block_end;
1818 bh = bh->b_this_page;
1819 } while (bh != head);
1820}
1821EXPORT_SYMBOL(page_zero_new_buffers);
1822
Christoph Hellwigebdec242010-10-06 10:47:23 +02001823int __block_write_begin(struct page *page, loff_t pos, unsigned len,
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001824 get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Christoph Hellwigebdec242010-10-06 10:47:23 +02001826 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1827 unsigned to = from + len;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001828 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 unsigned block_start, block_end;
1830 sector_t block;
1831 int err = 0;
1832 unsigned blocksize, bbits;
1833 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1834
1835 BUG_ON(!PageLocked(page));
1836 BUG_ON(from > PAGE_CACHE_SIZE);
1837 BUG_ON(to > PAGE_CACHE_SIZE);
1838 BUG_ON(from > to);
1839
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001840 head = create_page_buffers(page, inode, 0);
1841 blocksize = head->b_size;
1842 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1845
1846 for(bh = head, block_start = 0; bh != head || !block_start;
1847 block++, block_start=block_end, bh = bh->b_this_page) {
1848 block_end = block_start + blocksize;
1849 if (block_end <= from || block_start >= to) {
1850 if (PageUptodate(page)) {
1851 if (!buffer_uptodate(bh))
1852 set_buffer_uptodate(bh);
1853 }
1854 continue;
1855 }
1856 if (buffer_new(bh))
1857 clear_buffer_new(bh);
1858 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001859 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 err = get_block(inode, block, bh, 1);
1861 if (err)
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (buffer_new(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 unmap_underlying_metadata(bh->b_bdev,
1865 bh->b_blocknr);
1866 if (PageUptodate(page)) {
Nick Piggin637aff42007-10-16 01:25:00 -07001867 clear_buffer_new(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 set_buffer_uptodate(bh);
Nick Piggin637aff42007-10-16 01:25:00 -07001869 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 continue;
1871 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001872 if (block_end > to || block_start < from)
1873 zero_user_segments(page,
1874 to, block_end,
1875 block_start, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 continue;
1877 }
1878 }
1879 if (PageUptodate(page)) {
1880 if (!buffer_uptodate(bh))
1881 set_buffer_uptodate(bh);
1882 continue;
1883 }
1884 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
David Chinner33a266d2007-02-12 00:51:41 -08001885 !buffer_unwritten(bh) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 (block_start < from || block_end > to)) {
1887 ll_rw_block(READ, 1, &bh);
1888 *wait_bh++=bh;
1889 }
1890 }
1891 /*
1892 * If we issued read requests - let them complete.
1893 */
1894 while(wait_bh > wait) {
1895 wait_on_buffer(*--wait_bh);
1896 if (!buffer_uptodate(*wait_bh))
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001897 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Jan Karaf9f07b62011-06-14 00:58:27 +02001899 if (unlikely(err))
Nick Pigginafddba42007-10-16 01:25:01 -07001900 page_zero_new_buffers(page, from, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return err;
1902}
Christoph Hellwigebdec242010-10-06 10:47:23 +02001903EXPORT_SYMBOL(__block_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905static int __block_commit_write(struct inode *inode, struct page *page,
1906 unsigned from, unsigned to)
1907{
1908 unsigned block_start, block_end;
1909 int partial = 0;
1910 unsigned blocksize;
1911 struct buffer_head *bh, *head;
1912
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001913 bh = head = page_buffers(page);
1914 blocksize = bh->b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001916 block_start = 0;
1917 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 block_end = block_start + blocksize;
1919 if (block_end <= from || block_start >= to) {
1920 if (!buffer_uptodate(bh))
1921 partial = 1;
1922 } else {
1923 set_buffer_uptodate(bh);
1924 mark_buffer_dirty(bh);
1925 }
Nick Pigginafddba42007-10-16 01:25:01 -07001926 clear_buffer_new(bh);
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001927
1928 block_start = block_end;
1929 bh = bh->b_this_page;
1930 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
1933 * If this is a partial write which happened to make all buffers
1934 * uptodate then we can optimize away a bogus readpage() for
1935 * the next read(). Here we 'discover' whether the page went
1936 * uptodate as a result of this (potentially partial) write.
1937 */
1938 if (!partial)
1939 SetPageUptodate(page);
1940 return 0;
1941}
1942
1943/*
Christoph Hellwig155130a2010-06-04 11:29:58 +02001944 * block_write_begin takes care of the basic task of block allocation and
1945 * bringing partial write blocks uptodate first.
1946 *
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10001947 * The filesystem needs to handle block truncation upon failure.
Nick Pigginafddba42007-10-16 01:25:01 -07001948 */
Christoph Hellwig155130a2010-06-04 11:29:58 +02001949int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
1950 unsigned flags, struct page **pagep, get_block_t *get_block)
Nick Pigginafddba42007-10-16 01:25:01 -07001951{
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001952 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -07001953 struct page *page;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001954 int status;
Nick Pigginafddba42007-10-16 01:25:01 -07001955
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001956 page = grab_cache_page_write_begin(mapping, index, flags);
1957 if (!page)
1958 return -ENOMEM;
Nick Pigginafddba42007-10-16 01:25:01 -07001959
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001960 status = __block_write_begin(page, pos, len, get_block);
Nick Pigginafddba42007-10-16 01:25:01 -07001961 if (unlikely(status)) {
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001962 unlock_page(page);
1963 page_cache_release(page);
1964 page = NULL;
Nick Pigginafddba42007-10-16 01:25:01 -07001965 }
1966
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001967 *pagep = page;
Nick Pigginafddba42007-10-16 01:25:01 -07001968 return status;
1969}
1970EXPORT_SYMBOL(block_write_begin);
1971
1972int block_write_end(struct file *file, struct address_space *mapping,
1973 loff_t pos, unsigned len, unsigned copied,
1974 struct page *page, void *fsdata)
1975{
1976 struct inode *inode = mapping->host;
1977 unsigned start;
1978
1979 start = pos & (PAGE_CACHE_SIZE - 1);
1980
1981 if (unlikely(copied < len)) {
1982 /*
1983 * The buffers that were written will now be uptodate, so we
1984 * don't have to worry about a readpage reading them and
1985 * overwriting a partial write. However if we have encountered
1986 * a short write and only partially written into a buffer, it
1987 * will not be marked uptodate, so a readpage might come in and
1988 * destroy our partial write.
1989 *
1990 * Do the simplest thing, and just treat any short write to a
1991 * non uptodate page as a zero-length write, and force the
1992 * caller to redo the whole thing.
1993 */
1994 if (!PageUptodate(page))
1995 copied = 0;
1996
1997 page_zero_new_buffers(page, start+copied, start+len);
1998 }
1999 flush_dcache_page(page);
2000
2001 /* This could be a short (even 0-length) commit */
2002 __block_commit_write(inode, page, start, start+copied);
2003
2004 return copied;
2005}
2006EXPORT_SYMBOL(block_write_end);
2007
2008int generic_write_end(struct file *file, struct address_space *mapping,
2009 loff_t pos, unsigned len, unsigned copied,
2010 struct page *page, void *fsdata)
2011{
2012 struct inode *inode = mapping->host;
Jan Kara6cbdf112014-10-01 21:49:18 -04002013 loff_t old_size = inode->i_size;
Jan Karac7d206b2008-07-11 19:27:31 -04002014 int i_size_changed = 0;
Nick Pigginafddba42007-10-16 01:25:01 -07002015
2016 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2017
2018 /*
2019 * No need to use i_size_read() here, the i_size
2020 * cannot change under us because we hold i_mutex.
2021 *
2022 * But it's important to update i_size while still holding page lock:
2023 * page writeout could otherwise come in and zero beyond i_size.
2024 */
2025 if (pos+copied > inode->i_size) {
2026 i_size_write(inode, pos+copied);
Jan Karac7d206b2008-07-11 19:27:31 -04002027 i_size_changed = 1;
Nick Pigginafddba42007-10-16 01:25:01 -07002028 }
2029
2030 unlock_page(page);
2031 page_cache_release(page);
2032
Jan Kara6cbdf112014-10-01 21:49:18 -04002033 if (old_size < pos)
2034 pagecache_isize_extended(inode, old_size, pos);
Jan Karac7d206b2008-07-11 19:27:31 -04002035 /*
2036 * Don't mark the inode dirty under page lock. First, it unnecessarily
2037 * makes the holding time of page lock longer. Second, it forces lock
2038 * ordering of page lock and transaction start for journaling
2039 * filesystems.
2040 */
2041 if (i_size_changed)
2042 mark_inode_dirty(inode);
2043
Nick Pigginafddba42007-10-16 01:25:01 -07002044 return copied;
2045}
2046EXPORT_SYMBOL(generic_write_end);
2047
2048/*
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002049 * block_is_partially_uptodate checks whether buffers within a page are
2050 * uptodate or not.
2051 *
2052 * Returns true if all buffers which correspond to a file portion
2053 * we want to read are uptodate.
2054 */
2055int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2056 unsigned long from)
2057{
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002058 unsigned block_start, block_end, blocksize;
2059 unsigned to;
2060 struct buffer_head *bh, *head;
2061 int ret = 1;
2062
2063 if (!page_has_buffers(page))
2064 return 0;
2065
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002066 head = page_buffers(page);
2067 blocksize = head->b_size;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002068 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2069 to = from + to;
2070 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2071 return 0;
2072
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002073 bh = head;
2074 block_start = 0;
2075 do {
2076 block_end = block_start + blocksize;
2077 if (block_end > from && block_start < to) {
2078 if (!buffer_uptodate(bh)) {
2079 ret = 0;
2080 break;
2081 }
2082 if (block_end >= to)
2083 break;
2084 }
2085 block_start = block_end;
2086 bh = bh->b_this_page;
2087 } while (bh != head);
2088
2089 return ret;
2090}
2091EXPORT_SYMBOL(block_is_partially_uptodate);
2092
2093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * Generic "read page" function for block devices that have the normal
2095 * get_block functionality. This is most of the block device filesystems.
2096 * Reads the page asynchronously --- the unlock_buffer() and
2097 * set/clear_buffer_uptodate() functions propagate buffer state into the
2098 * page struct once IO has completed.
2099 */
2100int block_read_full_page(struct page *page, get_block_t *get_block)
2101{
2102 struct inode *inode = page->mapping->host;
2103 sector_t iblock, lblock;
2104 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002105 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int nr, i;
2107 int fully_mapped = 1;
2108
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002109 head = create_page_buffers(page, inode, 0);
2110 blocksize = head->b_size;
2111 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002113 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
2114 lblock = (i_size_read(inode)+blocksize-1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 bh = head;
2116 nr = 0;
2117 i = 0;
2118
2119 do {
2120 if (buffer_uptodate(bh))
2121 continue;
2122
2123 if (!buffer_mapped(bh)) {
Andrew Mortonc64610b2005-05-16 21:53:49 -07002124 int err = 0;
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 fully_mapped = 0;
2127 if (iblock < lblock) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002128 WARN_ON(bh->b_size != blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002129 err = get_block(inode, iblock, bh, 0);
2130 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 SetPageError(page);
2132 }
2133 if (!buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002134 zero_user(page, i * blocksize, blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002135 if (!err)
2136 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 continue;
2138 }
2139 /*
2140 * get_block() might have updated the buffer
2141 * synchronously
2142 */
2143 if (buffer_uptodate(bh))
2144 continue;
2145 }
2146 arr[nr++] = bh;
2147 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2148
2149 if (fully_mapped)
2150 SetPageMappedToDisk(page);
2151
2152 if (!nr) {
2153 /*
2154 * All buffers are uptodate - we can set the page uptodate
2155 * as well. But not if get_block() returned an error.
2156 */
2157 if (!PageError(page))
2158 SetPageUptodate(page);
2159 unlock_page(page);
2160 return 0;
2161 }
2162
2163 /* Stage two: lock the buffers */
2164 for (i = 0; i < nr; i++) {
2165 bh = arr[i];
2166 lock_buffer(bh);
2167 mark_buffer_async_read(bh);
2168 }
2169
2170 /*
2171 * Stage 3: start the IO. Check for uptodateness
2172 * inside the buffer lock in case another process reading
2173 * the underlying blockdev brought it uptodate (the sct fix).
2174 */
2175 for (i = 0; i < nr; i++) {
2176 bh = arr[i];
2177 if (buffer_uptodate(bh))
2178 end_buffer_async_read(bh, 1);
2179 else
2180 submit_bh(READ, bh);
2181 }
2182 return 0;
2183}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002184EXPORT_SYMBOL(block_read_full_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186/* utility function for filesystems that need to do work on expanding
Nick Piggin89e10782007-10-16 01:25:07 -07002187 * truncates. Uses filesystem pagecache writes to allow the filesystem to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 * deal with the hole.
2189 */
Nick Piggin89e10782007-10-16 01:25:07 -07002190int generic_cont_expand_simple(struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct address_space *mapping = inode->i_mapping;
2193 struct page *page;
Nick Piggin89e10782007-10-16 01:25:07 -07002194 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int err;
2196
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10002197 err = inode_newsize_ok(inode, size);
2198 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 goto out;
2200
Nick Piggin89e10782007-10-16 01:25:07 -07002201 err = pagecache_write_begin(NULL, mapping, size, 0,
2202 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2203 &page, &fsdata);
2204 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto out;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002206
Nick Piggin89e10782007-10-16 01:25:07 -07002207 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2208 BUG_ON(err > 0);
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210out:
2211 return err;
2212}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002213EXPORT_SYMBOL(generic_cont_expand_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Adrian Bunkf1e3af72008-04-29 00:59:01 -07002215static int cont_expand_zero(struct file *file, struct address_space *mapping,
2216 loff_t pos, loff_t *bytes)
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002217{
Nick Piggin89e10782007-10-16 01:25:07 -07002218 struct inode *inode = mapping->host;
2219 unsigned blocksize = 1 << inode->i_blkbits;
2220 struct page *page;
2221 void *fsdata;
2222 pgoff_t index, curidx;
2223 loff_t curpos;
2224 unsigned zerofrom, offset, len;
2225 int err = 0;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002226
Nick Piggin89e10782007-10-16 01:25:07 -07002227 index = pos >> PAGE_CACHE_SHIFT;
2228 offset = pos & ~PAGE_CACHE_MASK;
2229
2230 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2231 zerofrom = curpos & ~PAGE_CACHE_MASK;
2232 if (zerofrom & (blocksize-1)) {
2233 *bytes |= (blocksize-1);
2234 (*bytes)++;
2235 }
2236 len = PAGE_CACHE_SIZE - zerofrom;
2237
2238 err = pagecache_write_begin(file, mapping, curpos, len,
2239 AOP_FLAG_UNINTERRUPTIBLE,
2240 &page, &fsdata);
2241 if (err)
2242 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002243 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002244 err = pagecache_write_end(file, mapping, curpos, len, len,
2245 page, fsdata);
2246 if (err < 0)
2247 goto out;
2248 BUG_ON(err != len);
2249 err = 0;
OGAWA Hirofumi061e9742008-04-28 02:16:28 -07002250
2251 balance_dirty_pages_ratelimited(mapping);
Mikulas Patockaf83813a2014-07-27 13:00:41 -04002252
2253 if (unlikely(fatal_signal_pending(current))) {
2254 err = -EINTR;
2255 goto out;
2256 }
Nick Piggin89e10782007-10-16 01:25:07 -07002257 }
2258
2259 /* page covers the boundary, find the boundary offset */
2260 if (index == curidx) {
2261 zerofrom = curpos & ~PAGE_CACHE_MASK;
2262 /* if we will expand the thing last block will be filled */
2263 if (offset <= zerofrom) {
2264 goto out;
2265 }
2266 if (zerofrom & (blocksize-1)) {
2267 *bytes |= (blocksize-1);
2268 (*bytes)++;
2269 }
2270 len = offset - zerofrom;
2271
2272 err = pagecache_write_begin(file, mapping, curpos, len,
2273 AOP_FLAG_UNINTERRUPTIBLE,
2274 &page, &fsdata);
2275 if (err)
2276 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002277 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002278 err = pagecache_write_end(file, mapping, curpos, len, len,
2279 page, fsdata);
2280 if (err < 0)
2281 goto out;
2282 BUG_ON(err != len);
2283 err = 0;
2284 }
2285out:
2286 return err;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/*
2290 * For moronic filesystems that do not allow holes in file.
2291 * We may have to extend the file.
2292 */
Christoph Hellwig282dc172010-06-04 11:29:55 +02002293int cont_write_begin(struct file *file, struct address_space *mapping,
Nick Piggin89e10782007-10-16 01:25:07 -07002294 loff_t pos, unsigned len, unsigned flags,
2295 struct page **pagep, void **fsdata,
2296 get_block_t *get_block, loff_t *bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 unsigned blocksize = 1 << inode->i_blkbits;
Nick Piggin89e10782007-10-16 01:25:07 -07002300 unsigned zerofrom;
2301 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Nick Piggin89e10782007-10-16 01:25:07 -07002303 err = cont_expand_zero(file, mapping, pos, bytes);
2304 if (err)
Christoph Hellwig155130a2010-06-04 11:29:58 +02002305 return err;
Nick Piggin89e10782007-10-16 01:25:07 -07002306
2307 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2308 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2309 *bytes |= (blocksize-1);
2310 (*bytes)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Christoph Hellwig155130a2010-06-04 11:29:58 +02002313 return block_write_begin(mapping, pos, len, flags, pagep, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002315EXPORT_SYMBOL(cont_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317int block_commit_write(struct page *page, unsigned from, unsigned to)
2318{
2319 struct inode *inode = page->mapping->host;
2320 __block_commit_write(inode,page,from,to);
2321 return 0;
2322}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002323EXPORT_SYMBOL(block_commit_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
David Chinner54171692007-07-19 17:39:55 +10002325/*
2326 * block_page_mkwrite() is not allowed to change the file size as it gets
2327 * called from a page fault handler when a page is first dirtied. Hence we must
2328 * be careful to check for EOF conditions here. We set the page up correctly
2329 * for a written page which means we get ENOSPC checking when writing into
2330 * holes and correct delalloc and unwritten extent mapping on filesystems that
2331 * support these features.
2332 *
2333 * We are not allowed to take the i_mutex here so we have to play games to
2334 * protect against truncate races as the page could now be beyond EOF. Because
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002335 * truncate writes the inode size before removing pages, once we have the
David Chinner54171692007-07-19 17:39:55 +10002336 * page lock we can determine safely if the page is beyond EOF. If it is not
2337 * beyond EOF, then the page is guaranteed safe against truncation until we
2338 * unlock the page.
Jan Karaea13a862011-05-24 00:23:35 +02002339 *
Jan Kara14da9202012-06-12 16:20:37 +02002340 * Direct callers of this function should protect against filesystem freezing
2341 * using sb_start_write() - sb_end_write() functions.
David Chinner54171692007-07-19 17:39:55 +10002342 */
Jan Kara24da4fa2011-05-24 00:23:34 +02002343int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2344 get_block_t get_block)
David Chinner54171692007-07-19 17:39:55 +10002345{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002346 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05002347 struct inode *inode = file_inode(vma->vm_file);
David Chinner54171692007-07-19 17:39:55 +10002348 unsigned long end;
2349 loff_t size;
Jan Kara24da4fa2011-05-24 00:23:34 +02002350 int ret;
David Chinner54171692007-07-19 17:39:55 +10002351
2352 lock_page(page);
2353 size = i_size_read(inode);
2354 if ((page->mapping != inode->i_mapping) ||
Nick Piggin18336332007-07-20 00:31:45 -07002355 (page_offset(page) > size)) {
Jan Kara24da4fa2011-05-24 00:23:34 +02002356 /* We overload EFAULT to mean page got truncated */
2357 ret = -EFAULT;
2358 goto out_unlock;
David Chinner54171692007-07-19 17:39:55 +10002359 }
2360
2361 /* page is wholly or partially inside EOF */
2362 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2363 end = size & ~PAGE_CACHE_MASK;
2364 else
2365 end = PAGE_CACHE_SIZE;
2366
Christoph Hellwigebdec242010-10-06 10:47:23 +02002367 ret = __block_write_begin(page, 0, end, get_block);
David Chinner54171692007-07-19 17:39:55 +10002368 if (!ret)
2369 ret = block_commit_write(page, 0, end);
2370
Jan Kara24da4fa2011-05-24 00:23:34 +02002371 if (unlikely(ret < 0))
2372 goto out_unlock;
Jan Karaea13a862011-05-24 00:23:35 +02002373 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002374 wait_for_stable_page(page);
Jan Kara24da4fa2011-05-24 00:23:34 +02002375 return 0;
2376out_unlock:
2377 unlock_page(page);
David Chinner54171692007-07-19 17:39:55 +10002378 return ret;
2379}
Jan Kara24da4fa2011-05-24 00:23:34 +02002380EXPORT_SYMBOL(__block_page_mkwrite);
2381
2382int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2383 get_block_t get_block)
2384{
Jan Karaea13a862011-05-24 00:23:35 +02002385 int ret;
Al Viro496ad9a2013-01-23 17:07:38 -05002386 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
Jan Kara24da4fa2011-05-24 00:23:34 +02002387
Jan Kara14da9202012-06-12 16:20:37 +02002388 sb_start_pagefault(sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04002389
2390 /*
2391 * Update file times before taking page lock. We may end up failing the
2392 * fault so this update may be superfluous but who really cares...
2393 */
2394 file_update_time(vma->vm_file);
2395
Jan Karaea13a862011-05-24 00:23:35 +02002396 ret = __block_page_mkwrite(vma, vmf, get_block);
Jan Kara14da9202012-06-12 16:20:37 +02002397 sb_end_pagefault(sb);
Jan Kara24da4fa2011-05-24 00:23:34 +02002398 return block_page_mkwrite_return(ret);
2399}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002400EXPORT_SYMBOL(block_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002403 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * immediately, while under the page lock. So it needs a special end_io
2405 * handler which does not touch the bh after unlocking it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 */
2407static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2408{
Dmitry Monakhov68671f32007-10-16 01:24:47 -07002409 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
2412/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002413 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2414 * the page (converting it to circular linked list and taking care of page
2415 * dirty races).
2416 */
2417static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2418{
2419 struct buffer_head *bh;
2420
2421 BUG_ON(!PageLocked(page));
2422
2423 spin_lock(&page->mapping->private_lock);
2424 bh = head;
2425 do {
2426 if (PageDirty(page))
2427 set_buffer_dirty(bh);
2428 if (!bh->b_this_page)
2429 bh->b_this_page = head;
2430 bh = bh->b_this_page;
2431 } while (bh != head);
2432 attach_page_buffers(page, head);
2433 spin_unlock(&page->mapping->private_lock);
2434}
2435
2436/*
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002437 * On entry, the page is fully not uptodate.
2438 * On exit the page is fully uptodate in the areas outside (from,to)
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002439 * The filesystem needs to handle block truncation upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002441int nobh_write_begin(struct address_space *mapping,
Nick Piggin03158cd2007-10-16 01:25:25 -07002442 loff_t pos, unsigned len, unsigned flags,
2443 struct page **pagep, void **fsdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 get_block_t *get_block)
2445{
Nick Piggin03158cd2007-10-16 01:25:25 -07002446 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 const unsigned blkbits = inode->i_blkbits;
2448 const unsigned blocksize = 1 << blkbits;
Nick Piggina4b06722007-10-16 01:24:48 -07002449 struct buffer_head *head, *bh;
Nick Piggin03158cd2007-10-16 01:25:25 -07002450 struct page *page;
2451 pgoff_t index;
2452 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 unsigned block_in_page;
Nick Piggina4b06722007-10-16 01:24:48 -07002454 unsigned block_start, block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 sector_t block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 int nr_reads = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 int ret = 0;
2458 int is_mapped_to_disk = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Nick Piggin03158cd2007-10-16 01:25:25 -07002460 index = pos >> PAGE_CACHE_SHIFT;
2461 from = pos & (PAGE_CACHE_SIZE - 1);
2462 to = from + len;
2463
Nick Piggin54566b22009-01-04 12:00:53 -08002464 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin03158cd2007-10-16 01:25:25 -07002465 if (!page)
2466 return -ENOMEM;
2467 *pagep = page;
2468 *fsdata = NULL;
2469
2470 if (page_has_buffers(page)) {
Namhyung Kim309f77a2010-10-25 15:01:12 +09002471 ret = __block_write_begin(page, pos, len, get_block);
2472 if (unlikely(ret))
2473 goto out_release;
2474 return ret;
Nick Piggin03158cd2007-10-16 01:25:25 -07002475 }
Nick Piggina4b06722007-10-16 01:24:48 -07002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (PageMappedToDisk(page))
2478 return 0;
2479
Nick Piggina4b06722007-10-16 01:24:48 -07002480 /*
2481 * Allocate buffers so that we can keep track of state, and potentially
2482 * attach them to the page if an error occurs. In the common case of
2483 * no error, they will just be freed again without ever being attached
2484 * to the page (which is all OK, because we're under the page lock).
2485 *
2486 * Be careful: the buffer linked list is a NULL terminated one, rather
2487 * than the circular one we're used to.
2488 */
2489 head = alloc_page_buffers(page, blocksize, 0);
Nick Piggin03158cd2007-10-16 01:25:25 -07002490 if (!head) {
2491 ret = -ENOMEM;
2492 goto out_release;
2493 }
Nick Piggina4b06722007-10-16 01:24:48 -07002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 /*
2498 * We loop across all blocks in the page, whether or not they are
2499 * part of the affected region. This is so we can discover if the
2500 * page is fully mapped-to-disk.
2501 */
Nick Piggina4b06722007-10-16 01:24:48 -07002502 for (block_start = 0, block_in_page = 0, bh = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 block_start < PAGE_CACHE_SIZE;
Nick Piggina4b06722007-10-16 01:24:48 -07002504 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int create;
2506
Nick Piggina4b06722007-10-16 01:24:48 -07002507 block_end = block_start + blocksize;
2508 bh->b_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 create = 1;
2510 if (block_start >= to)
2511 create = 0;
2512 ret = get_block(inode, block_in_file + block_in_page,
Nick Piggina4b06722007-10-16 01:24:48 -07002513 bh, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (ret)
2515 goto failed;
Nick Piggina4b06722007-10-16 01:24:48 -07002516 if (!buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 is_mapped_to_disk = 0;
Nick Piggina4b06722007-10-16 01:24:48 -07002518 if (buffer_new(bh))
2519 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2520 if (PageUptodate(page)) {
2521 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 continue;
Nick Piggina4b06722007-10-16 01:24:48 -07002523 }
2524 if (buffer_new(bh) || !buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002525 zero_user_segments(page, block_start, from,
2526 to, block_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 continue;
2528 }
Nick Piggina4b06722007-10-16 01:24:48 -07002529 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 continue; /* reiserfs does this */
2531 if (block_start < from || block_end > to) {
Nick Piggina4b06722007-10-16 01:24:48 -07002532 lock_buffer(bh);
2533 bh->b_end_io = end_buffer_read_nobh;
2534 submit_bh(READ, bh);
2535 nr_reads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537 }
2538
2539 if (nr_reads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * The page is locked, so these buffers are protected from
2542 * any VM or truncate activity. Hence we don't need to care
2543 * for the buffer_head refcounts.
2544 */
Nick Piggina4b06722007-10-16 01:24:48 -07002545 for (bh = head; bh; bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 wait_on_buffer(bh);
2547 if (!buffer_uptodate(bh))
2548 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550 if (ret)
2551 goto failed;
2552 }
2553
2554 if (is_mapped_to_disk)
2555 SetPageMappedToDisk(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Nick Piggin03158cd2007-10-16 01:25:25 -07002557 *fsdata = head; /* to be released by nobh_write_end */
Nick Piggina4b06722007-10-16 01:24:48 -07002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return 0;
2560
2561failed:
Nick Piggin03158cd2007-10-16 01:25:25 -07002562 BUG_ON(!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
Nick Piggina4b06722007-10-16 01:24:48 -07002564 * Error recovery is a bit difficult. We need to zero out blocks that
2565 * were newly allocated, and dirty them to ensure they get written out.
2566 * Buffers need to be attached to the page at this point, otherwise
2567 * the handling of potential IO errors during writeout would be hard
2568 * (could try doing synchronous writeout, but what if that fails too?)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 */
Nick Piggin03158cd2007-10-16 01:25:25 -07002570 attach_nobh_buffers(page, head);
2571 page_zero_new_buffers(page, from, to);
Nick Piggina4b06722007-10-16 01:24:48 -07002572
Nick Piggin03158cd2007-10-16 01:25:25 -07002573out_release:
2574 unlock_page(page);
2575 page_cache_release(page);
2576 *pagep = NULL;
Nick Piggina4b06722007-10-16 01:24:48 -07002577
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002578 return ret;
2579}
Nick Piggin03158cd2007-10-16 01:25:25 -07002580EXPORT_SYMBOL(nobh_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Nick Piggin03158cd2007-10-16 01:25:25 -07002582int nobh_write_end(struct file *file, struct address_space *mapping,
2583 loff_t pos, unsigned len, unsigned copied,
2584 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 struct inode *inode = page->mapping->host;
Nick Pigginefdc3132007-10-21 06:57:41 +02002587 struct buffer_head *head = fsdata;
Nick Piggin03158cd2007-10-16 01:25:25 -07002588 struct buffer_head *bh;
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002589 BUG_ON(fsdata != NULL && page_has_buffers(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Dave Kleikampd4cf1092009-02-06 14:59:26 -06002591 if (unlikely(copied < len) && head)
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002592 attach_nobh_buffers(page, head);
2593 if (page_has_buffers(page))
2594 return generic_write_end(file, mapping, pos, len,
2595 copied, page, fsdata);
Nick Piggina4b06722007-10-16 01:24:48 -07002596
Nick Piggin22c8ca72007-02-20 13:58:09 -08002597 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 set_page_dirty(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002599 if (pos+copied > inode->i_size) {
2600 i_size_write(inode, pos+copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 mark_inode_dirty(inode);
2602 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002603
2604 unlock_page(page);
2605 page_cache_release(page);
2606
Nick Piggin03158cd2007-10-16 01:25:25 -07002607 while (head) {
2608 bh = head;
2609 head = head->b_this_page;
2610 free_buffer_head(bh);
2611 }
2612
2613 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
Nick Piggin03158cd2007-10-16 01:25:25 -07002615EXPORT_SYMBOL(nobh_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617/*
2618 * nobh_writepage() - based on block_full_write_page() except
2619 * that it tries to operate without attaching bufferheads to
2620 * the page.
2621 */
2622int nobh_writepage(struct page *page, get_block_t *get_block,
2623 struct writeback_control *wbc)
2624{
2625 struct inode * const inode = page->mapping->host;
2626 loff_t i_size = i_size_read(inode);
2627 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2628 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 int ret;
2630
2631 /* Is the page fully inside i_size? */
2632 if (page->index < end_index)
2633 goto out;
2634
2635 /* Is the page fully outside i_size? (truncate in progress) */
2636 offset = i_size & (PAGE_CACHE_SIZE-1);
2637 if (page->index >= end_index+1 || !offset) {
2638 /*
2639 * The page may have dirty, unmapped buffers. For example,
2640 * they may have been added in ext3_writepage(). Make them
2641 * freeable here, so the page does not leak.
2642 */
2643#if 0
2644 /* Not really sure about this - do we need this ? */
2645 if (page->mapping->a_ops->invalidatepage)
2646 page->mapping->a_ops->invalidatepage(page, offset);
2647#endif
2648 unlock_page(page);
2649 return 0; /* don't care */
2650 }
2651
2652 /*
2653 * The page straddles i_size. It must be zeroed out on each and every
2654 * writepage invocation because it may be mmapped. "A file is mapped
2655 * in multiples of the page size. For a file that is not a multiple of
2656 * the page size, the remaining memory is zeroed when mapped, and
2657 * writes to that region are not written out to the file."
2658 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002659 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660out:
2661 ret = mpage_writepage(page, get_block, wbc);
2662 if (ret == -EAGAIN)
Chris Mason35c80d52009-04-15 13:22:38 -04002663 ret = __block_write_full_page(inode, page, get_block, wbc,
2664 end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return ret;
2666}
2667EXPORT_SYMBOL(nobh_writepage);
2668
Nick Piggin03158cd2007-10-16 01:25:25 -07002669int nobh_truncate_page(struct address_space *mapping,
2670 loff_t from, get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2673 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Nick Piggin03158cd2007-10-16 01:25:25 -07002674 unsigned blocksize;
2675 sector_t iblock;
2676 unsigned length, pos;
2677 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct page *page;
Nick Piggin03158cd2007-10-16 01:25:25 -07002679 struct buffer_head map_bh;
2680 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Nick Piggin03158cd2007-10-16 01:25:25 -07002682 blocksize = 1 << inode->i_blkbits;
2683 length = offset & (blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Nick Piggin03158cd2007-10-16 01:25:25 -07002685 /* Block boundary? Nothing to do */
2686 if (!length)
2687 return 0;
2688
2689 length = blocksize - length;
2690 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 page = grab_cache_page(mapping, index);
Nick Piggin03158cd2007-10-16 01:25:25 -07002693 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (!page)
2695 goto out;
2696
Nick Piggin03158cd2007-10-16 01:25:25 -07002697 if (page_has_buffers(page)) {
2698has_buffers:
2699 unlock_page(page);
2700 page_cache_release(page);
2701 return block_truncate_page(mapping, from, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002703
2704 /* Find the buffer that contains "offset" */
2705 pos = blocksize;
2706 while (offset >= pos) {
2707 iblock++;
2708 pos += blocksize;
2709 }
2710
Theodore Ts'o460bcf52009-05-12 07:37:56 -04002711 map_bh.b_size = blocksize;
2712 map_bh.b_state = 0;
Nick Piggin03158cd2007-10-16 01:25:25 -07002713 err = get_block(inode, iblock, &map_bh, 0);
2714 if (err)
2715 goto unlock;
2716 /* unmapped? It's a hole - nothing to do */
2717 if (!buffer_mapped(&map_bh))
2718 goto unlock;
2719
2720 /* Ok, it's mapped. Make sure it's up-to-date */
2721 if (!PageUptodate(page)) {
2722 err = mapping->a_ops->readpage(NULL, page);
2723 if (err) {
2724 page_cache_release(page);
2725 goto out;
2726 }
2727 lock_page(page);
2728 if (!PageUptodate(page)) {
2729 err = -EIO;
2730 goto unlock;
2731 }
2732 if (page_has_buffers(page))
2733 goto has_buffers;
2734 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002735 zero_user(page, offset, length);
Nick Piggin03158cd2007-10-16 01:25:25 -07002736 set_page_dirty(page);
2737 err = 0;
2738
2739unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 unlock_page(page);
2741 page_cache_release(page);
2742out:
Nick Piggin03158cd2007-10-16 01:25:25 -07002743 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745EXPORT_SYMBOL(nobh_truncate_page);
2746
2747int block_truncate_page(struct address_space *mapping,
2748 loff_t from, get_block_t *get_block)
2749{
2750 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2751 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2752 unsigned blocksize;
Andrew Morton54b21a72006-01-08 01:03:05 -08002753 sector_t iblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 unsigned length, pos;
2755 struct inode *inode = mapping->host;
2756 struct page *page;
2757 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 int err;
2759
2760 blocksize = 1 << inode->i_blkbits;
2761 length = offset & (blocksize - 1);
2762
2763 /* Block boundary? Nothing to do */
2764 if (!length)
2765 return 0;
2766
2767 length = blocksize - length;
Andrew Morton54b21a72006-01-08 01:03:05 -08002768 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 page = grab_cache_page(mapping, index);
2771 err = -ENOMEM;
2772 if (!page)
2773 goto out;
2774
2775 if (!page_has_buffers(page))
2776 create_empty_buffers(page, blocksize, 0);
2777
2778 /* Find the buffer that contains "offset" */
2779 bh = page_buffers(page);
2780 pos = blocksize;
2781 while (offset >= pos) {
2782 bh = bh->b_this_page;
2783 iblock++;
2784 pos += blocksize;
2785 }
2786
2787 err = 0;
2788 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002789 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 err = get_block(inode, iblock, bh, 0);
2791 if (err)
2792 goto unlock;
2793 /* unmapped? It's a hole - nothing to do */
2794 if (!buffer_mapped(bh))
2795 goto unlock;
2796 }
2797
2798 /* Ok, it's mapped. Make sure it's up-to-date */
2799 if (PageUptodate(page))
2800 set_buffer_uptodate(bh);
2801
David Chinner33a266d2007-02-12 00:51:41 -08002802 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 err = -EIO;
2804 ll_rw_block(READ, 1, &bh);
2805 wait_on_buffer(bh);
2806 /* Uhhuh. Read error. Complain and punt. */
2807 if (!buffer_uptodate(bh))
2808 goto unlock;
2809 }
2810
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002811 zero_user(page, offset, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 mark_buffer_dirty(bh);
2813 err = 0;
2814
2815unlock:
2816 unlock_page(page);
2817 page_cache_release(page);
2818out:
2819 return err;
2820}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002821EXPORT_SYMBOL(block_truncate_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823/*
2824 * The generic ->writepage function for buffer-backed address_spaces
Chris Mason35c80d52009-04-15 13:22:38 -04002825 * this form passes in the end_io handler used to finish the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
Chris Mason35c80d52009-04-15 13:22:38 -04002827int block_write_full_page_endio(struct page *page, get_block_t *get_block,
2828 struct writeback_control *wbc, bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct inode * const inode = page->mapping->host;
2831 loff_t i_size = i_size_read(inode);
2832 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2833 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 /* Is the page fully inside i_size? */
2836 if (page->index < end_index)
Chris Mason35c80d52009-04-15 13:22:38 -04002837 return __block_write_full_page(inode, page, get_block, wbc,
2838 handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 /* Is the page fully outside i_size? (truncate in progress) */
2841 offset = i_size & (PAGE_CACHE_SIZE-1);
2842 if (page->index >= end_index+1 || !offset) {
2843 /*
2844 * The page may have dirty, unmapped buffers. For example,
2845 * they may have been added in ext3_writepage(). Make them
2846 * freeable here, so the page does not leak.
2847 */
Jan Karaaaa40592005-10-30 15:00:16 -08002848 do_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 unlock_page(page);
2850 return 0; /* don't care */
2851 }
2852
2853 /*
2854 * The page straddles i_size. It must be zeroed out on each and every
Adam Buchbinder2a61aa42009-12-11 16:35:40 -05002855 * writepage invocation because it may be mmapped. "A file is mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 * in multiples of the page size. For a file that is not a multiple of
2857 * the page size, the remaining memory is zeroed when mapped, and
2858 * writes to that region are not written out to the file."
2859 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002860 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Chris Mason35c80d52009-04-15 13:22:38 -04002861 return __block_write_full_page(inode, page, get_block, wbc, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002863EXPORT_SYMBOL(block_write_full_page_endio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Chris Mason35c80d52009-04-15 13:22:38 -04002865/*
2866 * The generic ->writepage function for buffer-backed address_spaces
2867 */
2868int block_write_full_page(struct page *page, get_block_t *get_block,
2869 struct writeback_control *wbc)
2870{
2871 return block_write_full_page_endio(page, get_block, wbc,
2872 end_buffer_async_write);
2873}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002874EXPORT_SYMBOL(block_write_full_page);
Chris Mason35c80d52009-04-15 13:22:38 -04002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2877 get_block_t *get_block)
2878{
2879 struct buffer_head tmp;
2880 struct inode *inode = mapping->host;
2881 tmp.b_state = 0;
2882 tmp.b_blocknr = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002883 tmp.b_size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 get_block(inode, block, &tmp, 0);
2885 return tmp.b_blocknr;
2886}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002887EXPORT_SYMBOL(generic_block_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
NeilBrown6712ecf2007-09-27 12:47:43 +02002889static void end_bio_bh_io_sync(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 struct buffer_head *bh = bio->bi_private;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (err == -EOPNOTSUPP) {
2894 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896
Keith Mannthey08bafc02008-11-25 10:24:35 +01002897 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2898 set_bit(BH_Quiet, &bh->b_state);
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2901 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Linus Torvalds57302e02012-12-04 08:25:11 -08002904/*
2905 * This allows us to do IO even on the odd last sectors
2906 * of a device, even if the bh block size is some multiple
2907 * of the physical sector size.
2908 *
2909 * We'll just truncate the bio to the size of the device,
2910 * and clear the end of the buffer head manually.
2911 *
2912 * Truly out-of-range accesses will turn into actual IO
2913 * errors, this only handles the "we need to be able to
2914 * do IO at the final sector" case.
2915 */
2916static void guard_bh_eod(int rw, struct bio *bio, struct buffer_head *bh)
2917{
2918 sector_t maxsector;
2919 unsigned bytes;
2920
2921 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
2922 if (!maxsector)
2923 return;
2924
2925 /*
2926 * If the *whole* IO is past the end of the device,
2927 * let it through, and the IO layer will turn it into
2928 * an EIO.
2929 */
2930 if (unlikely(bio->bi_sector >= maxsector))
2931 return;
2932
2933 maxsector -= bio->bi_sector;
2934 bytes = bio->bi_size;
2935 if (likely((bytes >> 9) <= maxsector))
2936 return;
2937
2938 /* Uhhuh. We've got a bh that straddles the device size! */
2939 bytes = maxsector << 9;
2940
2941 /* Truncate the bio.. */
2942 bio->bi_size = bytes;
2943 bio->bi_io_vec[0].bv_len = bytes;
2944
2945 /* ..and clear the end of the buffer for reads */
Dan Carpenter27d7c2a2012-12-05 20:01:24 +03002946 if ((rw & RW_MASK) == READ) {
Linus Torvalds57302e02012-12-04 08:25:11 -08002947 void *kaddr = kmap_atomic(bh->b_page);
2948 memset(kaddr + bh_offset(bh) + bytes, 0, bh->b_size - bytes);
2949 kunmap_atomic(kaddr);
Linus Torvalds6d283db2013-01-14 13:17:50 -08002950 flush_dcache_page(bh->b_page);
Linus Torvalds57302e02012-12-04 08:25:11 -08002951 }
2952}
2953
Darrick J. Wong7136851112013-04-29 15:07:25 -07002954int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 struct bio *bio;
2957 int ret = 0;
2958
2959 BUG_ON(!buffer_locked(bh));
2960 BUG_ON(!buffer_mapped(bh));
2961 BUG_ON(!bh->b_end_io);
Aneesh Kumar K.V8fb0e342009-05-12 16:22:37 -04002962 BUG_ON(buffer_delay(bh));
2963 BUG_ON(buffer_unwritten(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Jens Axboe48fd4f92008-08-22 10:00:36 +02002965 /*
Jens Axboe48fd4f92008-08-22 10:00:36 +02002966 * Only clear out a write error when rewriting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 */
Jens Axboe48fd4f92008-08-22 10:00:36 +02002968 if (test_set_buffer_req(bh) && (rw & WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 clear_buffer_write_io_error(bh);
2970
2971 /*
2972 * from here on down, it's all bio -- do the initial mapping,
2973 * submit_bio -> generic_make_request may further map this bio around
2974 */
2975 bio = bio_alloc(GFP_NOIO, 1);
2976
2977 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2978 bio->bi_bdev = bh->b_bdev;
2979 bio->bi_io_vec[0].bv_page = bh->b_page;
2980 bio->bi_io_vec[0].bv_len = bh->b_size;
2981 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2982
2983 bio->bi_vcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 bio->bi_size = bh->b_size;
2985
2986 bio->bi_end_io = end_bio_bh_io_sync;
2987 bio->bi_private = bh;
Darrick J. Wong7136851112013-04-29 15:07:25 -07002988 bio->bi_flags |= bio_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Linus Torvalds57302e02012-12-04 08:25:11 -08002990 /* Take care of bh's that straddle the end of the device */
2991 guard_bh_eod(rw, bio, bh);
2992
Theodore Ts'o877f9622013-04-20 19:58:37 -04002993 if (buffer_meta(bh))
2994 rw |= REQ_META;
2995 if (buffer_prio(bh))
2996 rw |= REQ_PRIO;
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 bio_get(bio);
2999 submit_bio(rw, bio);
3000
3001 if (bio_flagged(bio, BIO_EOPNOTSUPP))
3002 ret = -EOPNOTSUPP;
3003
3004 bio_put(bio);
3005 return ret;
3006}
Darrick J. Wong7136851112013-04-29 15:07:25 -07003007EXPORT_SYMBOL_GPL(_submit_bh);
3008
3009int submit_bh(int rw, struct buffer_head *bh)
3010{
3011 return _submit_bh(rw, bh, 0);
3012}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003013EXPORT_SYMBOL(submit_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015/**
3016 * ll_rw_block: low-level access to block devices (DEPRECATED)
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003017 * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 * @nr: number of &struct buffer_heads in the array
3019 * @bhs: array of pointers to &struct buffer_head
3020 *
Jan Karaa7662232005-09-06 15:19:10 -07003021 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
3022 * requests an I/O operation on them, either a %READ or a %WRITE. The third
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003023 * %READA option is described in the documentation for generic_make_request()
3024 * which ll_rw_block() calls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 *
3026 * This function drops any buffer that it cannot get a lock on (with the
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003027 * BH_Lock state bit), any buffer that appears to be clean when doing a write
3028 * request, and any buffer that appears to be up-to-date when doing read
3029 * request. Further it marks as clean buffers that are processed for
3030 * writing (the buffer cache won't assume that they are actually clean
3031 * until the buffer gets unlocked).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 *
3033 * ll_rw_block sets b_end_io to simple completion handler that marks
3034 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
3035 * any waiters.
3036 *
3037 * All of the buffers must be for the same device, and must also be a
3038 * multiple of the current approved size for the device.
3039 */
3040void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
3041{
3042 int i;
3043
3044 for (i = 0; i < nr; i++) {
3045 struct buffer_head *bh = bhs[i];
3046
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003047 if (!trylock_buffer(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 continue;
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003049 if (rw == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (test_clear_buffer_dirty(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003051 bh->b_end_io = end_buffer_write_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003052 get_bh(bh);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003053 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 continue;
3055 }
3056 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (!buffer_uptodate(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003058 bh->b_end_io = end_buffer_read_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003059 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 submit_bh(rw, bh);
3061 continue;
3062 }
3063 }
3064 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
3066}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003067EXPORT_SYMBOL(ll_rw_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003069void write_dirty_buffer(struct buffer_head *bh, int rw)
3070{
3071 lock_buffer(bh);
3072 if (!test_clear_buffer_dirty(bh)) {
3073 unlock_buffer(bh);
3074 return;
3075 }
3076 bh->b_end_io = end_buffer_write_sync;
3077 get_bh(bh);
3078 submit_bh(rw, bh);
3079}
3080EXPORT_SYMBOL(write_dirty_buffer);
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082/*
3083 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3084 * and then start new I/O and then wait upon it. The caller must have a ref on
3085 * the buffer_head.
3086 */
Christoph Hellwig87e99512010-08-11 17:05:45 +02003087int __sync_dirty_buffer(struct buffer_head *bh, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
3089 int ret = 0;
3090
3091 WARN_ON(atomic_read(&bh->b_count) < 1);
3092 lock_buffer(bh);
3093 if (test_clear_buffer_dirty(bh)) {
3094 get_bh(bh);
3095 bh->b_end_io = end_buffer_write_sync;
Christoph Hellwig87e99512010-08-11 17:05:45 +02003096 ret = submit_bh(rw, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 wait_on_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (!ret && !buffer_uptodate(bh))
3099 ret = -EIO;
3100 } else {
3101 unlock_buffer(bh);
3102 }
3103 return ret;
3104}
Christoph Hellwig87e99512010-08-11 17:05:45 +02003105EXPORT_SYMBOL(__sync_dirty_buffer);
3106
3107int sync_dirty_buffer(struct buffer_head *bh)
3108{
3109 return __sync_dirty_buffer(bh, WRITE_SYNC);
3110}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003111EXPORT_SYMBOL(sync_dirty_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113/*
3114 * try_to_free_buffers() checks if all the buffers on this particular page
3115 * are unused, and releases them if so.
3116 *
3117 * Exclusion against try_to_free_buffers may be obtained by either
3118 * locking the page or by holding its mapping's private_lock.
3119 *
3120 * If the page is dirty but all the buffers are clean then we need to
3121 * be sure to mark the page clean as well. This is because the page
3122 * may be against a block device, and a later reattachment of buffers
3123 * to a dirty page will set *all* buffers dirty. Which would corrupt
3124 * filesystem data on the same device.
3125 *
3126 * The same applies to regular filesystem pages: if all the buffers are
3127 * clean then we set the page clean and proceed. To do that, we require
3128 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3129 * private_lock.
3130 *
3131 * try_to_free_buffers() is non-blocking.
3132 */
3133static inline int buffer_busy(struct buffer_head *bh)
3134{
3135 return atomic_read(&bh->b_count) |
3136 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3137}
3138
3139static int
3140drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3141{
3142 struct buffer_head *head = page_buffers(page);
3143 struct buffer_head *bh;
3144
3145 bh = head;
3146 do {
akpm@osdl.orgde7d5a32005-05-01 08:58:39 -07003147 if (buffer_write_io_error(bh) && page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 set_bit(AS_EIO, &page->mapping->flags);
3149 if (buffer_busy(bh))
3150 goto failed;
3151 bh = bh->b_this_page;
3152 } while (bh != head);
3153
3154 do {
3155 struct buffer_head *next = bh->b_this_page;
3156
Jan Kara535ee2f2008-02-08 04:21:59 -08003157 if (bh->b_assoc_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 __remove_assoc_queue(bh);
3159 bh = next;
3160 } while (bh != head);
3161 *buffers_to_free = head;
3162 __clear_page_buffers(page);
3163 return 1;
3164failed:
3165 return 0;
3166}
3167
3168int try_to_free_buffers(struct page *page)
3169{
3170 struct address_space * const mapping = page->mapping;
3171 struct buffer_head *buffers_to_free = NULL;
3172 int ret = 0;
3173
3174 BUG_ON(!PageLocked(page));
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003175 if (PageWriteback(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return 0;
3177
3178 if (mapping == NULL) { /* can this still happen? */
3179 ret = drop_buffers(page, &buffers_to_free);
3180 goto out;
3181 }
3182
3183 spin_lock(&mapping->private_lock);
3184 ret = drop_buffers(page, &buffers_to_free);
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003185
3186 /*
3187 * If the filesystem writes its buffers by hand (eg ext3)
3188 * then we can have clean buffers against a dirty page. We
3189 * clean the page here; otherwise the VM will never notice
3190 * that the filesystem did any IO at all.
3191 *
3192 * Also, during truncate, discard_buffer will have marked all
3193 * the page's buffers clean. We discover that here and clean
3194 * the page also.
Nick Piggin87df7242007-01-30 14:36:27 +11003195 *
3196 * private_lock must be held over this entire operation in order
3197 * to synchronise against __set_page_dirty_buffers and prevent the
3198 * dirty bit from being lost.
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003199 */
3200 if (ret)
3201 cancel_dirty_page(page, PAGE_CACHE_SIZE);
Nick Piggin87df7242007-01-30 14:36:27 +11003202 spin_unlock(&mapping->private_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203out:
3204 if (buffers_to_free) {
3205 struct buffer_head *bh = buffers_to_free;
3206
3207 do {
3208 struct buffer_head *next = bh->b_this_page;
3209 free_buffer_head(bh);
3210 bh = next;
3211 } while (bh != buffers_to_free);
3212 }
3213 return ret;
3214}
3215EXPORT_SYMBOL(try_to_free_buffers);
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217/*
3218 * There are no bdflush tunables left. But distributions are
3219 * still running obsolete flush daemons, so we terminate them here.
3220 *
3221 * Use of bdflush() is deprecated and will be removed in a future kernel.
Jens Axboe5b0830c2009-09-23 19:37:09 +02003222 * The `flush-X' kernel threads fully replace bdflush daemons and this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01003224SYSCALL_DEFINE2(bdflush, int, func, long, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 static int msg_count;
3227
3228 if (!capable(CAP_SYS_ADMIN))
3229 return -EPERM;
3230
3231 if (msg_count < 5) {
3232 msg_count++;
3233 printk(KERN_INFO
3234 "warning: process `%s' used the obsolete bdflush"
3235 " system call\n", current->comm);
3236 printk(KERN_INFO "Fix your initscripts?\n");
3237 }
3238
3239 if (func == 1)
3240 do_exit(0);
3241 return 0;
3242}
3243
3244/*
3245 * Buffer-head allocation
3246 */
Shai Fultheima0a9b042012-05-15 12:29:52 +03003247static struct kmem_cache *bh_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249/*
3250 * Once the number of bh's in the machine exceeds this level, we start
3251 * stripping them in writeback.
3252 */
Zhang Yanfei43be5942013-02-22 16:35:46 -08003253static unsigned long max_buffer_heads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255int buffer_heads_over_limit;
3256
3257struct bh_accounting {
3258 int nr; /* Number of live bh's */
3259 int ratelimit; /* Limit cacheline bouncing */
3260};
3261
3262static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3263
3264static void recalc_bh_state(void)
3265{
3266 int i;
3267 int tot = 0;
3268
Christoph Lameteree1be862010-12-06 11:40:05 -06003269 if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return;
Christoph Lameterc7b92512010-12-06 11:16:28 -06003271 __this_cpu_write(bh_accounting.ratelimit, 0);
Eric Dumazet8a143422006-03-24 03:18:10 -08003272 for_each_online_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 tot += per_cpu(bh_accounting, i).nr;
3274 buffer_heads_over_limit = (tot > max_buffer_heads);
3275}
Christoph Lameterc7b92512010-12-06 11:16:28 -06003276
Al Virodd0fc662005-10-07 07:46:04 +01003277struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
Richard Kennedy019b4d12010-03-10 15:20:33 -08003279 struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (ret) {
Christoph Lametera35afb82007-05-16 22:10:57 -07003281 INIT_LIST_HEAD(&ret->b_assoc_buffers);
Thomas Gleixnerc32961f2011-03-18 09:18:52 +01003282 buffer_head_init_locks(ret);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003283 preempt_disable();
3284 __this_cpu_inc(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003286 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288 return ret;
3289}
3290EXPORT_SYMBOL(alloc_buffer_head);
3291
3292void free_buffer_head(struct buffer_head *bh)
3293{
3294 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3295 kmem_cache_free(bh_cachep, bh);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003296 preempt_disable();
3297 __this_cpu_dec(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003299 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301EXPORT_SYMBOL(free_buffer_head);
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303static void buffer_exit_cpu(int cpu)
3304{
3305 int i;
3306 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3307
3308 for (i = 0; i < BH_LRU_SIZE; i++) {
3309 brelse(b->bhs[i]);
3310 b->bhs[i] = NULL;
3311 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06003312 this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
Eric Dumazet8a143422006-03-24 03:18:10 -08003313 per_cpu(bh_accounting, cpu).nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
3316static int buffer_cpu_notify(struct notifier_block *self,
3317 unsigned long action, void *hcpu)
3318{
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003319 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 buffer_exit_cpu((unsigned long)hcpu);
3321 return NOTIFY_OK;
3322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003324/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003325 * bh_uptodate_or_lock - Test whether the buffer is uptodate
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003326 * @bh: struct buffer_head
3327 *
3328 * Return true if the buffer is up-to-date and false,
3329 * with the buffer locked, if not.
3330 */
3331int bh_uptodate_or_lock(struct buffer_head *bh)
3332{
3333 if (!buffer_uptodate(bh)) {
3334 lock_buffer(bh);
3335 if (!buffer_uptodate(bh))
3336 return 0;
3337 unlock_buffer(bh);
3338 }
3339 return 1;
3340}
3341EXPORT_SYMBOL(bh_uptodate_or_lock);
3342
3343/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003344 * bh_submit_read - Submit a locked buffer for reading
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003345 * @bh: struct buffer_head
3346 *
3347 * Returns zero on success and -EIO on error.
3348 */
3349int bh_submit_read(struct buffer_head *bh)
3350{
3351 BUG_ON(!buffer_locked(bh));
3352
3353 if (buffer_uptodate(bh)) {
3354 unlock_buffer(bh);
3355 return 0;
3356 }
3357
3358 get_bh(bh);
3359 bh->b_end_io = end_buffer_read_sync;
3360 submit_bh(READ, bh);
3361 wait_on_buffer(bh);
3362 if (buffer_uptodate(bh))
3363 return 0;
3364 return -EIO;
3365}
3366EXPORT_SYMBOL(bh_submit_read);
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368void __init buffer_init(void)
3369{
Zhang Yanfei43be5942013-02-22 16:35:46 -08003370 unsigned long nrpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Christoph Lameterb98938c2008-02-04 22:28:36 -08003372 bh_cachep = kmem_cache_create("buffer_head",
3373 sizeof(struct buffer_head), 0,
3374 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3375 SLAB_MEM_SPREAD),
Richard Kennedy019b4d12010-03-10 15:20:33 -08003376 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
3378 /*
3379 * Limit the bh occupancy to 10% of ZONE_NORMAL
3380 */
3381 nrpages = (nr_free_buffer_pages() * 10) / 100;
3382 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3383 hotcpu_notifier(buffer_cpu_notify, 0);
3384}