blob: a8c2dfb68dcdfcf5cb73b7d8a1e6bd289476c15a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
48
Yan Honga3f3c292012-12-12 13:52:15 -080049void init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 bh->b_end_io = handler;
52 bh->b_private = private;
53}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070054EXPORT_SYMBOL(init_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Tejun Heof0059af2013-01-11 13:06:35 -080056inline void touch_buffer(struct buffer_head *bh)
57{
58 mark_page_accessed(bh->b_page);
59}
60EXPORT_SYMBOL(touch_buffer);
61
Jens Axboe7eaceac2011-03-10 08:52:07 +010062static int sleep_on_buffer(void *word)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 io_schedule();
65 return 0;
66}
67
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080068void __lock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jens Axboe7eaceac2011-03-10 08:52:07 +010070 wait_on_bit_lock(&bh->b_state, BH_Lock, sleep_on_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 TASK_UNINTERRUPTIBLE);
72}
73EXPORT_SYMBOL(__lock_buffer);
74
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080075void unlock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Nick Piggin51b07fc2008-10-18 20:27:00 -070077 clear_bit_unlock(BH_Lock, &bh->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 smp_mb__after_clear_bit();
79 wake_up_bit(&bh->b_state, BH_Lock);
80}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070081EXPORT_SYMBOL(unlock_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * Block until a buffer comes unlocked. This doesn't stop it
85 * from becoming locked again - you have to lock it yourself
86 * if you want to preserve its state.
87 */
88void __wait_on_buffer(struct buffer_head * bh)
89{
Jens Axboe7eaceac2011-03-10 08:52:07 +010090 wait_on_bit(&bh->b_state, BH_Lock, sleep_on_buffer, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070092EXPORT_SYMBOL(__wait_on_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94static void
95__clear_page_buffers(struct page *page)
96{
97 ClearPagePrivate(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070098 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 page_cache_release(page);
100}
101
Keith Mannthey08bafc02008-11-25 10:24:35 +0100102
103static int quiet_error(struct buffer_head *bh)
104{
105 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
106 return 0;
107 return 1;
108}
109
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static void buffer_io_error(struct buffer_head *bh)
112{
113 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
115 bdevname(bh->b_bdev, b),
116 (unsigned long long)bh->b_blocknr);
117}
118
119/*
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700120 * End-of-IO handler helper function which does not touch the bh after
121 * unlocking it.
122 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
123 * a race there is benign: unlock_buffer() only use the bh's address for
124 * hashing after unlocking the buffer, so it doesn't actually touch the bh
125 * itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700127static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 if (uptodate) {
130 set_buffer_uptodate(bh);
131 } else {
132 /* This happens, due to failed READA attempts. */
133 clear_buffer_uptodate(bh);
134 }
135 unlock_buffer(bh);
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700136}
137
138/*
139 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
140 * unlock the buffer. This is what ll_rw_block uses too.
141 */
142void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
143{
144 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 put_bh(bh);
146}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700147EXPORT_SYMBOL(end_buffer_read_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
150{
151 char b[BDEVNAME_SIZE];
152
153 if (uptodate) {
154 set_buffer_uptodate(bh);
155 } else {
Christoph Hellwig0edd55f2010-08-18 05:29:23 -0400156 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 buffer_io_error(bh);
158 printk(KERN_WARNING "lost page write due to "
159 "I/O error on %s\n",
160 bdevname(bh->b_bdev, b));
161 }
162 set_buffer_write_io_error(bh);
163 clear_buffer_uptodate(bh);
164 }
165 unlock_buffer(bh);
166 put_bh(bh);
167}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700168EXPORT_SYMBOL(end_buffer_write_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Various filesystems appear to want __find_get_block to be non-blocking.
172 * But it's the page lock which protects the buffers. To get around this,
173 * we get exclusion from try_to_free_buffers with the blockdev mapping's
174 * private_lock.
175 *
176 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
177 * may be quite high. This code could TryLock the page, and if that
178 * succeeds, there is no need to take private_lock. (But if
179 * private_lock is contended then so is mapping->tree_lock).
180 */
181static struct buffer_head *
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -0800182__find_get_block_slow(struct block_device *bdev, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct inode *bd_inode = bdev->bd_inode;
185 struct address_space *bd_mapping = bd_inode->i_mapping;
186 struct buffer_head *ret = NULL;
187 pgoff_t index;
188 struct buffer_head *bh;
189 struct buffer_head *head;
190 struct page *page;
191 int all_mapped = 1;
192
193 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
194 page = find_get_page(bd_mapping, index);
195 if (!page)
196 goto out;
197
198 spin_lock(&bd_mapping->private_lock);
199 if (!page_has_buffers(page))
200 goto out_unlock;
201 head = page_buffers(page);
202 bh = head;
203 do {
Nikanth Karthikesan97f76d32009-04-02 16:56:46 -0700204 if (!buffer_mapped(bh))
205 all_mapped = 0;
206 else if (bh->b_blocknr == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 ret = bh;
208 get_bh(bh);
209 goto out_unlock;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 bh = bh->b_this_page;
212 } while (bh != head);
213
214 /* we might be here because some of the buffers on this page are
215 * not mapped. This is due to various races between
216 * file io on the block device and getblk. It gets dealt with
217 * elsewhere, don't buffer_error if we had some unmapped buffers
218 */
219 if (all_mapped) {
Tao Ma72a2ebd2011-10-31 17:09:00 -0700220 char b[BDEVNAME_SIZE];
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 printk("__find_get_block_slow() failed. "
223 "block=%llu, b_blocknr=%llu\n",
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800224 (unsigned long long)block,
225 (unsigned long long)bh->b_blocknr);
226 printk("b_state=0x%08lx, b_size=%zu\n",
227 bh->b_state, bh->b_size);
Tao Ma72a2ebd2011-10-31 17:09:00 -0700228 printk("device %s blocksize: %d\n", bdevname(bdev, b),
229 1 << bd_inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231out_unlock:
232 spin_unlock(&bd_mapping->private_lock);
233 page_cache_release(page);
234out:
235 return ret;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
Jens Axboe5b0830c2009-09-23 19:37:09 +0200239 * Kick the writeback threads then try to free up some ZONE_NORMAL memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241static void free_more_memory(void)
242{
Mel Gorman19770b32008-04-28 02:12:18 -0700243 struct zone *zone;
Mel Gorman0e884602008-04-28 02:12:14 -0700244 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -0600246 wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 yield();
248
Mel Gorman0e884602008-04-28 02:12:14 -0700249 for_each_online_node(nid) {
Mel Gorman19770b32008-04-28 02:12:18 -0700250 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
251 gfp_zone(GFP_NOFS), NULL,
252 &zone);
253 if (zone)
Mel Gorman54a6eb52008-04-28 02:12:16 -0700254 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -0700255 GFP_NOFS, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257}
258
259/*
260 * I/O completion handler for block_read_full_page() - pages
261 * which come unlocked at the end of I/O.
262 */
263static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700266 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct buffer_head *tmp;
268 struct page *page;
269 int page_uptodate = 1;
270
271 BUG_ON(!buffer_async_read(bh));
272
273 page = bh->b_page;
274 if (uptodate) {
275 set_buffer_uptodate(bh);
276 } else {
277 clear_buffer_uptodate(bh);
Keith Mannthey08bafc02008-11-25 10:24:35 +0100278 if (!quiet_error(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 buffer_io_error(bh);
280 SetPageError(page);
281 }
282
283 /*
284 * Be _very_ careful from here on. Bad things can happen if
285 * two buffer heads end IO at almost the same time and both
286 * decide that the page is now completely done.
287 */
Nick Piggina3972202005-07-07 17:56:56 -0700288 first = page_buffers(page);
289 local_irq_save(flags);
290 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 clear_buffer_async_read(bh);
292 unlock_buffer(bh);
293 tmp = bh;
294 do {
295 if (!buffer_uptodate(tmp))
296 page_uptodate = 0;
297 if (buffer_async_read(tmp)) {
298 BUG_ON(!buffer_locked(tmp));
299 goto still_busy;
300 }
301 tmp = tmp->b_this_page;
302 } while (tmp != bh);
Nick Piggina3972202005-07-07 17:56:56 -0700303 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
304 local_irq_restore(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:
Nick Piggina3972202005-07-07 17:56:56 -0700316 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
317 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return;
319}
320
321/*
322 * Completion handler for block_write_full_page() - pages which are unlocked
323 * during I/O, and which have PageWriteback cleared upon I/O completion.
324 */
Chris Mason35c80d52009-04-15 13:22:38 -0400325void end_buffer_async_write(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700329 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct buffer_head *tmp;
331 struct page *page;
332
333 BUG_ON(!buffer_async_write(bh));
334
335 page = bh->b_page;
336 if (uptodate) {
337 set_buffer_uptodate(bh);
338 } else {
Keith Mannthey08bafc02008-11-25 10:24:35 +0100339 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 buffer_io_error(bh);
341 printk(KERN_WARNING "lost page write due to "
342 "I/O error on %s\n",
343 bdevname(bh->b_bdev, b));
344 }
345 set_bit(AS_EIO, &page->mapping->flags);
Jan Kara58ff4072006-10-17 00:10:19 -0700346 set_buffer_write_io_error(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 clear_buffer_uptodate(bh);
348 SetPageError(page);
349 }
350
Nick Piggina3972202005-07-07 17:56:56 -0700351 first = page_buffers(page);
352 local_irq_save(flags);
353 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 clear_buffer_async_write(bh);
356 unlock_buffer(bh);
357 tmp = bh->b_this_page;
358 while (tmp != bh) {
359 if (buffer_async_write(tmp)) {
360 BUG_ON(!buffer_locked(tmp));
361 goto still_busy;
362 }
363 tmp = tmp->b_this_page;
364 }
Nick Piggina3972202005-07-07 17:56:56 -0700365 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
366 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 end_page_writeback(page);
368 return;
369
370still_busy:
Nick Piggina3972202005-07-07 17:56:56 -0700371 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
372 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return;
374}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700375EXPORT_SYMBOL(end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377/*
378 * If a page's buffers are under async readin (end_buffer_async_read
379 * completion) then there is a possibility that another thread of
380 * control could lock one of the buffers after it has completed
381 * but while some of the other buffers have not completed. This
382 * locked buffer would confuse end_buffer_async_read() into not unlocking
383 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
384 * that this buffer is not under async I/O.
385 *
386 * The page comes unlocked when it has no locked buffer_async buffers
387 * left.
388 *
389 * PageLocked prevents anyone starting new async I/O reads any of
390 * the buffers.
391 *
392 * PageWriteback is used to prevent simultaneous writeout of the same
393 * page.
394 *
395 * PageLocked prevents anyone from starting writeback of a page which is
396 * under read I/O (PageWriteback is only ever set against a locked page).
397 */
398static void mark_buffer_async_read(struct buffer_head *bh)
399{
400 bh->b_end_io = end_buffer_async_read;
401 set_buffer_async_read(bh);
402}
403
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700404static void mark_buffer_async_write_endio(struct buffer_head *bh,
405 bh_end_io_t *handler)
Chris Mason35c80d52009-04-15 13:22:38 -0400406{
407 bh->b_end_io = handler;
408 set_buffer_async_write(bh);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411void mark_buffer_async_write(struct buffer_head *bh)
412{
Chris Mason35c80d52009-04-15 13:22:38 -0400413 mark_buffer_async_write_endio(bh, end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415EXPORT_SYMBOL(mark_buffer_async_write);
416
417
418/*
419 * fs/buffer.c contains helper functions for buffer-backed address space's
420 * fsync functions. A common requirement for buffer-based filesystems is
421 * that certain data from the backing blockdev needs to be written out for
422 * a successful fsync(). For example, ext2 indirect blocks need to be
423 * written back and waited upon before fsync() returns.
424 *
425 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
426 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
427 * management of a list of dependent buffers at ->i_mapping->private_list.
428 *
429 * Locking is a little subtle: try_to_free_buffers() will remove buffers
430 * from their controlling inode's queue when they are being freed. But
431 * try_to_free_buffers() will be operating against the *blockdev* mapping
432 * at the time, not against the S_ISREG file which depends on those buffers.
433 * So the locking for private_list is via the private_lock in the address_space
434 * which backs the buffers. Which is different from the address_space
435 * against which the buffers are listed. So for a particular address_space,
436 * mapping->private_lock does *not* protect mapping->private_list! In fact,
437 * mapping->private_list will always be protected by the backing blockdev's
438 * ->private_lock.
439 *
440 * Which introduces a requirement: all buffers on an address_space's
441 * ->private_list must be from the same address_space: the blockdev's.
442 *
443 * address_spaces which do not place buffers at ->private_list via these
444 * utility functions are free to use private_lock and private_list for
445 * whatever they want. The only requirement is that list_empty(private_list)
446 * be true at clear_inode() time.
447 *
448 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
449 * filesystems should do that. invalidate_inode_buffers() should just go
450 * BUG_ON(!list_empty).
451 *
452 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
453 * take an address_space, not an inode. And it should be called
454 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
455 * queued up.
456 *
457 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
458 * list if it is already on a list. Because if the buffer is on a list,
459 * it *must* already be on the right one. If not, the filesystem is being
460 * silly. This will save a ton of locking. But first we have to ensure
461 * that buffers are taken *off* the old inode's list when they are freed
462 * (presumably in truncate). That requires careful auditing of all
463 * filesystems (do it inside bforget()). It could also be done by bringing
464 * b_inode back.
465 */
466
467/*
468 * The buffer's backing address_space's private_lock must be held
469 */
Thomas Petazzonidbacefc2008-07-29 22:33:47 -0700470static void __remove_assoc_queue(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -0700473 WARN_ON(!bh->b_assoc_map);
474 if (buffer_write_io_error(bh))
475 set_bit(AS_EIO, &bh->b_assoc_map->flags);
476 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479int inode_has_buffers(struct inode *inode)
480{
481 return !list_empty(&inode->i_data.private_list);
482}
483
484/*
485 * osync is designed to support O_SYNC io. It waits synchronously for
486 * all already-submitted IO to complete, but does not queue any new
487 * writes to the disk.
488 *
489 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
490 * you dirty the buffers, and then use osync_inode_buffers to wait for
491 * completion. Any other dirty buffers which are not yet queued for
492 * write will not be flushed to disk by the osync.
493 */
494static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
495{
496 struct buffer_head *bh;
497 struct list_head *p;
498 int err = 0;
499
500 spin_lock(lock);
501repeat:
502 list_for_each_prev(p, list) {
503 bh = BH_ENTRY(p);
504 if (buffer_locked(bh)) {
505 get_bh(bh);
506 spin_unlock(lock);
507 wait_on_buffer(bh);
508 if (!buffer_uptodate(bh))
509 err = -EIO;
510 brelse(bh);
511 spin_lock(lock);
512 goto repeat;
513 }
514 }
515 spin_unlock(lock);
516 return err;
517}
518
Al Viro01a05b32010-03-23 06:06:58 -0400519static void do_thaw_one(struct super_block *sb, void *unused)
520{
521 char b[BDEVNAME_SIZE];
522 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
523 printk(KERN_WARNING "Emergency Thaw on %s\n",
524 bdevname(sb->s_bdev, b));
525}
526
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700527static void do_thaw_all(struct work_struct *work)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700528{
Al Viro01a05b32010-03-23 06:06:58 -0400529 iterate_supers(do_thaw_one, NULL);
Jens Axboe053c5252009-04-08 13:44:08 +0200530 kfree(work);
Eric Sandeenc2d75432009-03-31 15:23:46 -0700531 printk(KERN_WARNING "Emergency Thaw complete\n");
532}
533
534/**
535 * emergency_thaw_all -- forcibly thaw every frozen filesystem
536 *
537 * Used for emergency unfreeze of all filesystems via SysRq
538 */
539void emergency_thaw_all(void)
540{
Jens Axboe053c5252009-04-08 13:44:08 +0200541 struct work_struct *work;
542
543 work = kmalloc(sizeof(*work), GFP_ATOMIC);
544 if (work) {
545 INIT_WORK(work, do_thaw_all);
546 schedule_work(work);
547 }
Eric Sandeenc2d75432009-03-31 15:23:46 -0700548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800551 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
Martin Waitz67be2dd2005-05-01 08:59:26 -0700552 * @mapping: the mapping which wants those buffers written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
554 * Starts I/O against the buffers at mapping->private_list, and waits upon
555 * that I/O.
556 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700557 * Basically, this is a convenience function for fsync().
558 * @mapping is a file or directory which needs those buffers to be written for
559 * a successful fsync().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561int sync_mapping_buffers(struct address_space *mapping)
562{
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800563 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
566 return 0;
567
568 return fsync_buffers_list(&buffer_mapping->private_lock,
569 &mapping->private_list);
570}
571EXPORT_SYMBOL(sync_mapping_buffers);
572
573/*
574 * Called when we've recently written block `bblock', and it is known that
575 * `bblock' was for a buffer_boundary() buffer. This means that the block at
576 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
577 * dirty, schedule it for IO. So that indirects merge nicely with their data.
578 */
579void write_boundary_block(struct block_device *bdev,
580 sector_t bblock, unsigned blocksize)
581{
582 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
583 if (bh) {
584 if (buffer_dirty(bh))
585 ll_rw_block(WRITE, 1, &bh);
586 put_bh(bh);
587 }
588}
589
590void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
591{
592 struct address_space *mapping = inode->i_mapping;
593 struct address_space *buffer_mapping = bh->b_page->mapping;
594
595 mark_buffer_dirty(bh);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800596 if (!mapping->private_data) {
597 mapping->private_data = buffer_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } else {
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800599 BUG_ON(mapping->private_data != buffer_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Jan Kara535ee2f2008-02-08 04:21:59 -0800601 if (!bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_lock(&buffer_mapping->private_lock);
603 list_move_tail(&bh->b_assoc_buffers,
604 &mapping->private_list);
Jan Kara58ff4072006-10-17 00:10:19 -0700605 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_unlock(&buffer_mapping->private_lock);
607 }
608}
609EXPORT_SYMBOL(mark_buffer_dirty_inode);
610
611/*
Nick Piggin787d2212007-07-17 04:03:34 -0700612 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
613 * dirty.
614 *
615 * If warn is true, then emit a warning if the page is not uptodate and has
616 * not been truncated.
617 */
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700618static void __set_page_dirty(struct page *page,
Nick Piggin787d2212007-07-17 04:03:34 -0700619 struct address_space *mapping, int warn)
620{
Nick Piggin19fd6232008-07-25 19:45:32 -0700621 spin_lock_irq(&mapping->tree_lock);
Nick Piggin787d2212007-07-17 04:03:34 -0700622 if (page->mapping) { /* Race with truncate? */
623 WARN_ON_ONCE(warn && !PageUptodate(page));
Edward Shishkine3a7cca2009-03-31 15:19:39 -0700624 account_page_dirtied(page, mapping);
Nick Piggin787d2212007-07-17 04:03:34 -0700625 radix_tree_tag_set(&mapping->page_tree,
626 page_index(page), PAGECACHE_TAG_DIRTY);
627 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700628 spin_unlock_irq(&mapping->tree_lock);
Nick Piggin787d2212007-07-17 04:03:34 -0700629 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Nick Piggin787d2212007-07-17 04:03:34 -0700630}
631
632/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * Add a page to the dirty page list.
634 *
635 * It is a sad fact of life that this function is called from several places
636 * deeply under spinlocking. It may not sleep.
637 *
638 * If the page has buffers, the uptodate buffers are set dirty, to preserve
639 * dirty-state coherency between the page and the buffers. It the page does
640 * not have buffers then when they are later attached they will all be set
641 * dirty.
642 *
643 * The buffers are dirtied before the page is dirtied. There's a small race
644 * window in which a writepage caller may see the page cleanness but not the
645 * buffer dirtiness. That's fine. If this code were to set the page dirty
646 * before the buffers, a concurrent writepage caller could clear the page dirty
647 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
648 * page on the dirty page list.
649 *
650 * We use private_lock to lock against try_to_free_buffers while using the
651 * page's buffer list. Also use this to protect against clean buffers being
652 * added to the page after it was set dirty.
653 *
654 * FIXME: may need to call ->reservepage here as well. That's rather up to the
655 * address_space though.
656 */
657int __set_page_dirty_buffers(struct page *page)
658{
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700659 int newly_dirty;
Nick Piggin787d2212007-07-17 04:03:34 -0700660 struct address_space *mapping = page_mapping(page);
Nick Pigginebf7a222006-10-10 04:36:54 +0200661
662 if (unlikely(!mapping))
663 return !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 spin_lock(&mapping->private_lock);
666 if (page_has_buffers(page)) {
667 struct buffer_head *head = page_buffers(page);
668 struct buffer_head *bh = head;
669
670 do {
671 set_buffer_dirty(bh);
672 bh = bh->b_this_page;
673 } while (bh != head);
674 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700675 newly_dirty = !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_unlock(&mapping->private_lock);
677
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700678 if (newly_dirty)
679 __set_page_dirty(page, mapping, 1);
680 return newly_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682EXPORT_SYMBOL(__set_page_dirty_buffers);
683
684/*
685 * Write out and wait upon a list of buffers.
686 *
687 * We have conflicting pressures: we want to make sure that all
688 * initially dirty buffers get waited on, but that any subsequently
689 * dirtied buffers don't. After all, we don't want fsync to last
690 * forever if somebody is actively writing to the file.
691 *
692 * Do this in two main stages: first we copy dirty buffers to a
693 * temporary inode list, queueing the writes as we go. Then we clean
694 * up, waiting for those writes to complete.
695 *
696 * During this second stage, any subsequent updates to the file may end
697 * up refiling the buffer on the original inode's dirty list again, so
698 * there is a chance we will end up with a buffer queued for write but
699 * not yet completed on that list. So, as a final cleanup we go through
700 * the osync code to catch these locked, dirty buffers without requeuing
701 * any newly dirty buffers for write.
702 */
703static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
704{
705 struct buffer_head *bh;
706 struct list_head tmp;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100707 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int err = 0, err2;
Jens Axboe4ee24912011-03-17 10:51:40 +0100709 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 INIT_LIST_HEAD(&tmp);
Jens Axboe4ee24912011-03-17 10:51:40 +0100712 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 spin_lock(lock);
715 while (!list_empty(list)) {
716 bh = BH_ENTRY(list->next);
Jan Kara535ee2f2008-02-08 04:21:59 -0800717 mapping = bh->b_assoc_map;
Jan Kara58ff4072006-10-17 00:10:19 -0700718 __remove_assoc_queue(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800719 /* Avoid race with mark_buffer_dirty_inode() which does
720 * a lockless check and we rely on seeing the dirty bit */
721 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (buffer_dirty(bh) || buffer_locked(bh)) {
723 list_add(&bh->b_assoc_buffers, &tmp);
Jan Kara535ee2f2008-02-08 04:21:59 -0800724 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (buffer_dirty(bh)) {
726 get_bh(bh);
727 spin_unlock(lock);
728 /*
729 * Ensure any pending I/O completes so that
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200730 * write_dirty_buffer() actually writes the
731 * current contents - it is a noop if I/O is
732 * still in flight on potentially older
733 * contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Jens Axboe721a9602011-03-09 11:56:30 +0100735 write_dirty_buffer(bh, WRITE_SYNC);
Jens Axboe9cf6b722009-04-06 14:48:03 +0200736
737 /*
738 * Kick off IO for the previous mapping. Note
739 * that we will not run the very last mapping,
740 * wait_on_buffer() will do that for us
741 * through sync_buffer().
742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 brelse(bh);
744 spin_lock(lock);
745 }
746 }
747 }
748
Jens Axboe4ee24912011-03-17 10:51:40 +0100749 spin_unlock(lock);
750 blk_finish_plug(&plug);
751 spin_lock(lock);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 while (!list_empty(&tmp)) {
754 bh = BH_ENTRY(tmp.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 get_bh(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800756 mapping = bh->b_assoc_map;
757 __remove_assoc_queue(bh);
758 /* Avoid race with mark_buffer_dirty_inode() which does
759 * a lockless check and we rely on seeing the dirty bit */
760 smp_mb();
761 if (buffer_dirty(bh)) {
762 list_add(&bh->b_assoc_buffers,
Jan Karae3892292008-03-04 14:28:33 -0800763 &mapping->private_list);
Jan Kara535ee2f2008-02-08 04:21:59 -0800764 bh->b_assoc_map = mapping;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 spin_unlock(lock);
767 wait_on_buffer(bh);
768 if (!buffer_uptodate(bh))
769 err = -EIO;
770 brelse(bh);
771 spin_lock(lock);
772 }
773
774 spin_unlock(lock);
775 err2 = osync_buffers_list(lock, list);
776 if (err)
777 return err;
778 else
779 return err2;
780}
781
782/*
783 * Invalidate any and all dirty buffers on a given inode. We are
784 * probably unmounting the fs, but that doesn't mean we have already
785 * done a sync(). Just drop the buffers from the inode list.
786 *
787 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
788 * assumes that all the buffers are against the blockdev. Not true
789 * for reiserfs.
790 */
791void invalidate_inode_buffers(struct inode *inode)
792{
793 if (inode_has_buffers(inode)) {
794 struct address_space *mapping = &inode->i_data;
795 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800796 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 spin_lock(&buffer_mapping->private_lock);
799 while (!list_empty(list))
800 __remove_assoc_queue(BH_ENTRY(list->next));
801 spin_unlock(&buffer_mapping->private_lock);
802 }
803}
Jan Kara52b19ac2008-09-23 18:24:08 +0200804EXPORT_SYMBOL(invalidate_inode_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806/*
807 * Remove any clean buffers from the inode's buffer list. This is called
808 * when we're trying to free the inode itself. Those buffers can pin it.
809 *
810 * Returns true if all buffers were removed.
811 */
812int remove_inode_buffers(struct inode *inode)
813{
814 int ret = 1;
815
816 if (inode_has_buffers(inode)) {
817 struct address_space *mapping = &inode->i_data;
818 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800819 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 spin_lock(&buffer_mapping->private_lock);
822 while (!list_empty(list)) {
823 struct buffer_head *bh = BH_ENTRY(list->next);
824 if (buffer_dirty(bh)) {
825 ret = 0;
826 break;
827 }
828 __remove_assoc_queue(bh);
829 }
830 spin_unlock(&buffer_mapping->private_lock);
831 }
832 return ret;
833}
834
835/*
836 * Create the appropriate buffers when given a page for data area and
837 * the size of each buffer.. Use the bh->b_this_page linked list to
838 * follow the buffers created. Return NULL if unable to create more
839 * buffers.
840 *
841 * The retry flag is used to differentiate async IO (paging, swapping)
842 * which may not fail from ordinary buffer allocations.
843 */
844struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
845 int retry)
846{
847 struct buffer_head *bh, *head;
848 long offset;
849
850try_again:
851 head = NULL;
852 offset = PAGE_SIZE;
853 while ((offset -= size) >= 0) {
854 bh = alloc_buffer_head(GFP_NOFS);
855 if (!bh)
856 goto no_grow;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 bh->b_this_page = head;
859 bh->b_blocknr = -1;
860 head = bh;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 bh->b_size = size;
863
864 /* Link the buffer to its page */
865 set_bh_page(bh, page, offset);
866
Nathan Scott01ffe332006-01-17 09:02:07 +1100867 init_buffer(bh, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 return head;
870/*
871 * In case anything failed, we just free everything we got.
872 */
873no_grow:
874 if (head) {
875 do {
876 bh = head;
877 head = head->b_this_page;
878 free_buffer_head(bh);
879 } while (head);
880 }
881
882 /*
883 * Return failure for non-async IO requests. Async IO requests
884 * are not allowed to fail, so we have to wait until buffer heads
885 * become available. But we don't want tasks sleeping with
886 * partially complete buffers, so all were released above.
887 */
888 if (!retry)
889 return NULL;
890
891 /* We're _really_ low on memory. Now we just
892 * wait for old buffer heads to become free due to
893 * finishing IO. Since this is an async request and
894 * the reserve list is empty, we're sure there are
895 * async buffer heads in use.
896 */
897 free_more_memory();
898 goto try_again;
899}
900EXPORT_SYMBOL_GPL(alloc_page_buffers);
901
902static inline void
903link_dev_buffers(struct page *page, struct buffer_head *head)
904{
905 struct buffer_head *bh, *tail;
906
907 bh = head;
908 do {
909 tail = bh;
910 bh = bh->b_this_page;
911 } while (bh);
912 tail->b_this_page = head;
913 attach_page_buffers(page, head);
914}
915
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800916static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
917{
918 sector_t retval = ~((sector_t)0);
919 loff_t sz = i_size_read(bdev->bd_inode);
920
921 if (sz) {
922 unsigned int sizebits = blksize_bits(size);
923 retval = (sz >> sizebits);
924 }
925 return retval;
926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
929 * Initialise the state of a blockdev page's buffers.
930 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200931static sector_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932init_page_buffers(struct page *page, struct block_device *bdev,
933 sector_t block, int size)
934{
935 struct buffer_head *head = page_buffers(page);
936 struct buffer_head *bh = head;
937 int uptodate = PageUptodate(page);
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800938 sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 do {
941 if (!buffer_mapped(bh)) {
942 init_buffer(bh, NULL, NULL);
943 bh->b_bdev = bdev;
944 bh->b_blocknr = block;
945 if (uptodate)
946 set_buffer_uptodate(bh);
Jeff Moyer080399a2012-05-11 16:34:10 +0200947 if (block < end_block)
948 set_buffer_mapped(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 block++;
951 bh = bh->b_this_page;
952 } while (bh != head);
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200953
954 /*
955 * Caller needs to validate requested block against end of device.
956 */
957 return end_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960/*
961 * Create the page-cache page that contains the requested block.
962 *
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200963 * This is used purely for blockdev mappings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200965static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966grow_dev_page(struct block_device *bdev, sector_t block,
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200967 pgoff_t index, int size, int sizebits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct inode *inode = bdev->bd_inode;
970 struct page *page;
971 struct buffer_head *bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200972 sector_t end_block;
973 int ret = 0; /* Will call free_more_memory() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Christoph Lameterea125892007-05-16 22:11:21 -0700975 page = find_or_create_page(inode->i_mapping, index,
Mel Gorman769848c2007-07-17 04:03:05 -0700976 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!page)
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200978 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Eric Sesterhenne827f922006-03-26 18:24:46 +0200980 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (page_has_buffers(page)) {
983 bh = page_buffers(page);
984 if (bh->b_size == size) {
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200985 end_block = init_page_buffers(page, bdev,
986 index << sizebits, size);
987 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 if (!try_to_free_buffers(page))
990 goto failed;
991 }
992
993 /*
994 * Allocate some buffers for this page
995 */
996 bh = alloc_page_buffers(page, size, 0);
997 if (!bh)
998 goto failed;
999
1000 /*
1001 * Link the page to the buffers and initialise them. Take the
1002 * lock to be atomic wrt __find_get_block(), which does not
1003 * run under the page lock.
1004 */
1005 spin_lock(&inode->i_mapping->private_lock);
1006 link_dev_buffers(page, bh);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001007 end_block = init_page_buffers(page, bdev, index << sizebits, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_unlock(&inode->i_mapping->private_lock);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001009done:
1010 ret = (block < end_block) ? 1 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unlock_page(page);
1013 page_cache_release(page);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001014 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*
1018 * Create buffers for the specified block device block's page. If
1019 * that page was dirty, the buffers are set dirty also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001021static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022grow_buffers(struct block_device *bdev, sector_t block, int size)
1023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 pgoff_t index;
1025 int sizebits;
1026
1027 sizebits = -1;
1028 do {
1029 sizebits++;
1030 } while ((size << sizebits) < PAGE_SIZE);
1031
1032 index = block >> sizebits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Andrew Mortone5657932006-10-11 01:21:46 -07001034 /*
1035 * Check for a block which wants to lie outside our maximum possible
1036 * pagecache index. (this comparison is done using sector_t types).
1037 */
1038 if (unlikely(index != block >> sizebits)) {
1039 char b[BDEVNAME_SIZE];
1040
1041 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1042 "device %s\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001043 __func__, (unsigned long long)block,
Andrew Mortone5657932006-10-11 01:21:46 -07001044 bdevname(bdev, b));
1045 return -EIO;
1046 }
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* Create a page with the proper size buffers.. */
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001049 return grow_dev_page(bdev, block, index, size, sizebits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Adrian Bunk75c96f82005-05-05 16:16:09 -07001052static struct buffer_head *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053__getblk_slow(struct block_device *bdev, sector_t block, int size)
1054{
1055 /* Size must be multiple of hard sectorsize */
Martin K. Petersene1defc42009-05-22 17:17:49 -04001056 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 (size < 512 || size > PAGE_SIZE))) {
1058 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1059 size);
Martin K. Petersene1defc42009-05-22 17:17:49 -04001060 printk(KERN_ERR "logical block size: %d\n",
1061 bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 dump_stack();
1064 return NULL;
1065 }
1066
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001067 for (;;) {
1068 struct buffer_head *bh;
1069 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 bh = __find_get_block(bdev, block, size);
1072 if (bh)
1073 return bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001074
1075 ret = grow_buffers(bdev, block, size);
1076 if (ret < 0)
1077 return NULL;
1078 if (ret == 0)
1079 free_more_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081}
1082
1083/*
1084 * The relationship between dirty buffers and dirty pages:
1085 *
1086 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1087 * the page is tagged dirty in its radix tree.
1088 *
1089 * At all times, the dirtiness of the buffers represents the dirtiness of
1090 * subsections of the page. If the page has buffers, the page dirty bit is
1091 * merely a hint about the true dirty state.
1092 *
1093 * When a page is set dirty in its entirety, all its buffers are marked dirty
1094 * (if the page has buffers).
1095 *
1096 * When a buffer is marked dirty, its page is dirtied, but the page's other
1097 * buffers are not.
1098 *
1099 * Also. When blockdev buffers are explicitly read with bread(), they
1100 * individually become uptodate. But their backing page remains not
1101 * uptodate - even if all of its buffers are uptodate. A subsequent
1102 * block_read_full_page() against that page will discover all the uptodate
1103 * buffers, will set the page uptodate and will perform no I/O.
1104 */
1105
1106/**
1107 * mark_buffer_dirty - mark a buffer_head as needing writeout
Martin Waitz67be2dd2005-05-01 08:59:26 -07001108 * @bh: the buffer_head to mark dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 *
1110 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1111 * backing page dirty, then tag the page as dirty in its address_space's radix
1112 * tree and then attach the address_space's inode to its superblock's dirty
1113 * inode list.
1114 *
1115 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
Dave Chinner250df6e2011-03-22 22:23:36 +11001116 * mapping->tree_lock and mapping->host->i_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001118void mark_buffer_dirty(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Nick Piggin787d2212007-07-17 04:03:34 -07001120 WARN_ON_ONCE(!buffer_uptodate(bh));
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001121
1122 /*
1123 * Very *carefully* optimize the it-is-already-dirty case.
1124 *
1125 * Don't let the final "is it dirty" escape to before we
1126 * perhaps modified the buffer.
1127 */
1128 if (buffer_dirty(bh)) {
1129 smp_mb();
1130 if (buffer_dirty(bh))
1131 return;
1132 }
1133
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001134 if (!test_set_buffer_dirty(bh)) {
1135 struct page *page = bh->b_page;
Linus Torvalds8e9d78e2009-08-21 17:40:08 -07001136 if (!TestSetPageDirty(page)) {
1137 struct address_space *mapping = page_mapping(page);
1138 if (mapping)
1139 __set_page_dirty(page, mapping, 0);
1140 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001143EXPORT_SYMBOL(mark_buffer_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145/*
1146 * Decrement a buffer_head's reference count. If all buffers against a page
1147 * have zero reference count, are clean and unlocked, and if the page is clean
1148 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1149 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1150 * a page but it ends up not being freed, and buffers may later be reattached).
1151 */
1152void __brelse(struct buffer_head * buf)
1153{
1154 if (atomic_read(&buf->b_count)) {
1155 put_bh(buf);
1156 return;
1157 }
Arjan van de Ven5c752ad2008-07-25 19:45:40 -07001158 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001160EXPORT_SYMBOL(__brelse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162/*
1163 * bforget() is like brelse(), except it discards any
1164 * potentially dirty data.
1165 */
1166void __bforget(struct buffer_head *bh)
1167{
1168 clear_buffer_dirty(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -08001169 if (bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct address_space *buffer_mapping = bh->b_page->mapping;
1171
1172 spin_lock(&buffer_mapping->private_lock);
1173 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -07001174 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 spin_unlock(&buffer_mapping->private_lock);
1176 }
1177 __brelse(bh);
1178}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001179EXPORT_SYMBOL(__bforget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181static struct buffer_head *__bread_slow(struct buffer_head *bh)
1182{
1183 lock_buffer(bh);
1184 if (buffer_uptodate(bh)) {
1185 unlock_buffer(bh);
1186 return bh;
1187 } else {
1188 get_bh(bh);
1189 bh->b_end_io = end_buffer_read_sync;
1190 submit_bh(READ, bh);
1191 wait_on_buffer(bh);
1192 if (buffer_uptodate(bh))
1193 return bh;
1194 }
1195 brelse(bh);
1196 return NULL;
1197}
1198
1199/*
1200 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1201 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1202 * refcount elevated by one when they're in an LRU. A buffer can only appear
1203 * once in a particular CPU's LRU. A single buffer can be present in multiple
1204 * CPU's LRUs at the same time.
1205 *
1206 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1207 * sb_find_get_block().
1208 *
1209 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1210 * a local interrupt disable for that.
1211 */
1212
1213#define BH_LRU_SIZE 8
1214
1215struct bh_lru {
1216 struct buffer_head *bhs[BH_LRU_SIZE];
1217};
1218
1219static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1220
1221#ifdef CONFIG_SMP
1222#define bh_lru_lock() local_irq_disable()
1223#define bh_lru_unlock() local_irq_enable()
1224#else
1225#define bh_lru_lock() preempt_disable()
1226#define bh_lru_unlock() preempt_enable()
1227#endif
1228
1229static inline void check_irqs_on(void)
1230{
1231#ifdef irqs_disabled
1232 BUG_ON(irqs_disabled());
1233#endif
1234}
1235
1236/*
1237 * The LRU management algorithm is dopey-but-simple. Sorry.
1238 */
1239static void bh_lru_install(struct buffer_head *bh)
1240{
1241 struct buffer_head *evictee = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 check_irqs_on();
1244 bh_lru_lock();
Christoph Lameterc7b92512010-12-06 11:16:28 -06001245 if (__this_cpu_read(bh_lrus.bhs[0]) != bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct buffer_head *bhs[BH_LRU_SIZE];
1247 int in;
1248 int out = 0;
1249
1250 get_bh(bh);
1251 bhs[out++] = bh;
1252 for (in = 0; in < BH_LRU_SIZE; in++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001253 struct buffer_head *bh2 =
1254 __this_cpu_read(bh_lrus.bhs[in]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (bh2 == bh) {
1257 __brelse(bh2);
1258 } else {
1259 if (out >= BH_LRU_SIZE) {
1260 BUG_ON(evictee != NULL);
1261 evictee = bh2;
1262 } else {
1263 bhs[out++] = bh2;
1264 }
1265 }
1266 }
1267 while (out < BH_LRU_SIZE)
1268 bhs[out++] = NULL;
Christoph Lameterc7b92512010-12-06 11:16:28 -06001269 memcpy(__this_cpu_ptr(&bh_lrus.bhs), bhs, sizeof(bhs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 bh_lru_unlock();
1272
1273 if (evictee)
1274 __brelse(evictee);
1275}
1276
1277/*
1278 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1279 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001280static struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001281lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct buffer_head *ret = NULL;
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001284 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 check_irqs_on();
1287 bh_lru_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 for (i = 0; i < BH_LRU_SIZE; i++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001289 struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 if (bh && bh->b_bdev == bdev &&
1292 bh->b_blocknr == block && bh->b_size == size) {
1293 if (i) {
1294 while (i) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001295 __this_cpu_write(bh_lrus.bhs[i],
1296 __this_cpu_read(bh_lrus.bhs[i - 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 i--;
1298 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06001299 __this_cpu_write(bh_lrus.bhs[0], bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 get_bh(bh);
1302 ret = bh;
1303 break;
1304 }
1305 }
1306 bh_lru_unlock();
1307 return ret;
1308}
1309
1310/*
1311 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1312 * it in the LRU and mark it as accessed. If it is not present then return
1313 * NULL
1314 */
1315struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001316__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1319
1320 if (bh == NULL) {
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001321 bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (bh)
1323 bh_lru_install(bh);
1324 }
1325 if (bh)
1326 touch_buffer(bh);
1327 return bh;
1328}
1329EXPORT_SYMBOL(__find_get_block);
1330
1331/*
1332 * __getblk will locate (and, if necessary, create) the buffer_head
1333 * which corresponds to the passed block_device, block and size. The
1334 * returned buffer has its reference count incremented.
1335 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1337 * attempt is failing. FIXME, perhaps?
1338 */
1339struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001340__getblk(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct buffer_head *bh = __find_get_block(bdev, block, size);
1343
1344 might_sleep();
1345 if (bh == NULL)
1346 bh = __getblk_slow(bdev, block, size);
1347 return bh;
1348}
1349EXPORT_SYMBOL(__getblk);
1350
1351/*
1352 * Do async read-ahead on a buffer..
1353 */
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001354void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct buffer_head *bh = __getblk(bdev, block, size);
Andrew Mortona3e713b2005-10-30 15:03:15 -08001357 if (likely(bh)) {
1358 ll_rw_block(READA, 1, &bh);
1359 brelse(bh);
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362EXPORT_SYMBOL(__breadahead);
1363
1364/**
1365 * __bread() - reads a specified block and returns the bh
Martin Waitz67be2dd2005-05-01 08:59:26 -07001366 * @bdev: the block_device to read from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * @block: number of block
1368 * @size: size (in bytes) to read
1369 *
1370 * Reads a specified block, and returns buffer head that contains it.
1371 * It returns NULL if the block was unreadable.
1372 */
1373struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001374__bread(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct buffer_head *bh = __getblk(bdev, block, size);
1377
Andrew Mortona3e713b2005-10-30 15:03:15 -08001378 if (likely(bh) && !buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 bh = __bread_slow(bh);
1380 return bh;
1381}
1382EXPORT_SYMBOL(__bread);
1383
1384/*
1385 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1386 * This doesn't race because it runs in each cpu either in irq
1387 * or with preempt disabled.
1388 */
1389static void invalidate_bh_lru(void *arg)
1390{
1391 struct bh_lru *b = &get_cpu_var(bh_lrus);
1392 int i;
1393
1394 for (i = 0; i < BH_LRU_SIZE; i++) {
1395 brelse(b->bhs[i]);
1396 b->bhs[i] = NULL;
1397 }
1398 put_cpu_var(bh_lrus);
1399}
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001400
1401static bool has_bh_in_lru(int cpu, void *dummy)
1402{
1403 struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
1404 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001406 for (i = 0; i < BH_LRU_SIZE; i++) {
1407 if (b->bhs[i])
1408 return 1;
1409 }
1410
1411 return 0;
1412}
1413
Peter Zijlstraf9a14392007-05-06 14:49:55 -07001414void invalidate_bh_lrus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001416 on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
Nick Piggin9db55792008-02-08 04:19:49 -08001418EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420void set_bh_page(struct buffer_head *bh,
1421 struct page *page, unsigned long offset)
1422{
1423 bh->b_page = page;
Eric Sesterhenne827f922006-03-26 18:24:46 +02001424 BUG_ON(offset >= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (PageHighMem(page))
1426 /*
1427 * This catches illegal uses and preserves the offset:
1428 */
1429 bh->b_data = (char *)(0 + offset);
1430 else
1431 bh->b_data = page_address(page) + offset;
1432}
1433EXPORT_SYMBOL(set_bh_page);
1434
1435/*
1436 * Called when truncating a buffer on a page completely.
1437 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001438static void discard_buffer(struct buffer_head * bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 lock_buffer(bh);
1441 clear_buffer_dirty(bh);
1442 bh->b_bdev = NULL;
1443 clear_buffer_mapped(bh);
1444 clear_buffer_req(bh);
1445 clear_buffer_new(bh);
1446 clear_buffer_delay(bh);
David Chinner33a266d2007-02-12 00:51:41 -08001447 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unlock_buffer(bh);
1449}
1450
1451/**
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001452 * block_invalidatepage - invalidate part or all of a buffer-backed page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 *
1454 * @page: the page which is affected
1455 * @offset: the index of the truncation point
1456 *
1457 * block_invalidatepage() is called when all or part of the page has become
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001458 * invalidated by a truncate operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 *
1460 * block_invalidatepage() does not have to release all buffers, but it must
1461 * ensure that no dirty buffer is left outside @offset and that no I/O
1462 * is underway against any of the blocks which are outside the truncation
1463 * point. Because the caller is about to free (and possibly reuse) those
1464 * blocks on-disk.
1465 */
NeilBrown2ff28e22006-03-26 01:37:18 -08001466void block_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct buffer_head *head, *bh, *next;
1469 unsigned int curr_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 BUG_ON(!PageLocked(page));
1472 if (!page_has_buffers(page))
1473 goto out;
1474
1475 head = page_buffers(page);
1476 bh = head;
1477 do {
1478 unsigned int next_off = curr_off + bh->b_size;
1479 next = bh->b_this_page;
1480
1481 /*
1482 * is this block fully invalidated?
1483 */
1484 if (offset <= curr_off)
1485 discard_buffer(bh);
1486 curr_off = next_off;
1487 bh = next;
1488 } while (bh != head);
1489
1490 /*
1491 * We release buffers only if the entire page is being invalidated.
1492 * The get_block cached value has been unconditionally invalidated,
1493 * so real IO is not possible anymore.
1494 */
1495 if (offset == 0)
NeilBrown2ff28e22006-03-26 01:37:18 -08001496 try_to_release_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497out:
NeilBrown2ff28e22006-03-26 01:37:18 -08001498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500EXPORT_SYMBOL(block_invalidatepage);
1501
1502/*
1503 * We attach and possibly dirty the buffers atomically wrt
1504 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1505 * is already excluded via the page lock.
1506 */
1507void create_empty_buffers(struct page *page,
1508 unsigned long blocksize, unsigned long b_state)
1509{
1510 struct buffer_head *bh, *head, *tail;
1511
1512 head = alloc_page_buffers(page, blocksize, 1);
1513 bh = head;
1514 do {
1515 bh->b_state |= b_state;
1516 tail = bh;
1517 bh = bh->b_this_page;
1518 } while (bh);
1519 tail->b_this_page = head;
1520
1521 spin_lock(&page->mapping->private_lock);
1522 if (PageUptodate(page) || PageDirty(page)) {
1523 bh = head;
1524 do {
1525 if (PageDirty(page))
1526 set_buffer_dirty(bh);
1527 if (PageUptodate(page))
1528 set_buffer_uptodate(bh);
1529 bh = bh->b_this_page;
1530 } while (bh != head);
1531 }
1532 attach_page_buffers(page, head);
1533 spin_unlock(&page->mapping->private_lock);
1534}
1535EXPORT_SYMBOL(create_empty_buffers);
1536
1537/*
1538 * We are taking a block for data and we don't want any output from any
1539 * buffer-cache aliases starting from return from that function and
1540 * until the moment when something will explicitly mark the buffer
1541 * dirty (hopefully that will not happen until we will free that block ;-)
1542 * We don't even need to mark it not-uptodate - nobody can expect
1543 * anything from a newly allocated buffer anyway. We used to used
1544 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1545 * don't want to mark the alias unmapped, for example - it would confuse
1546 * anyone who might pick it with bread() afterwards...
1547 *
1548 * Also.. Note that bforget() doesn't lock the buffer. So there can
1549 * be writeout I/O going on against recently-freed buffers. We don't
1550 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1551 * only if we really need to. That happens here.
1552 */
1553void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1554{
1555 struct buffer_head *old_bh;
1556
1557 might_sleep();
1558
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001559 old_bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (old_bh) {
1561 clear_buffer_dirty(old_bh);
1562 wait_on_buffer(old_bh);
1563 clear_buffer_req(old_bh);
1564 __brelse(old_bh);
1565 }
1566}
1567EXPORT_SYMBOL(unmap_underlying_metadata);
1568
1569/*
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001570 * Size is a power-of-two in the range 512..PAGE_SIZE,
1571 * and the case we care about most is PAGE_SIZE.
1572 *
1573 * So this *could* possibly be written with those
1574 * constraints in mind (relevant mostly if some
1575 * architecture has a slow bit-scan instruction)
1576 */
1577static inline int block_size_bits(unsigned int blocksize)
1578{
1579 return ilog2(blocksize);
1580}
1581
1582static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
1583{
1584 BUG_ON(!PageLocked(page));
1585
1586 if (!page_has_buffers(page))
1587 create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
1588 return page_buffers(page);
1589}
1590
1591/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * NOTE! All mapped/uptodate combinations are valid:
1593 *
1594 * Mapped Uptodate Meaning
1595 *
1596 * No No "unknown" - must do get_block()
1597 * No Yes "hole" - zero-filled
1598 * Yes No "allocated" - allocated on disk, not read in
1599 * Yes Yes "valid" - allocated and up-to-date in memory.
1600 *
1601 * "Dirty" is valid only with the last case (mapped+uptodate).
1602 */
1603
1604/*
1605 * While block_write_full_page is writing back the dirty buffers under
1606 * the page lock, whoever dirtied the buffers may decide to clean them
1607 * again at any time. We handle that by only looking at the buffer
1608 * state inside lock_buffer().
1609 *
1610 * If block_write_full_page() is called for regular writeback
1611 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1612 * locked buffer. This only can happen if someone has written the buffer
1613 * directly, with submit_bh(). At the address_space level PageWriteback
1614 * prevents this contention from occurring.
Theodore Ts'o6e34eedd2009-04-07 18:12:43 -04001615 *
1616 * If block_write_full_page() is called with wbc->sync_mode ==
Jens Axboe721a9602011-03-09 11:56:30 +01001617 * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
1618 * causes the writes to be flagged as synchronous writes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
1620static int __block_write_full_page(struct inode *inode, struct page *page,
Chris Mason35c80d52009-04-15 13:22:38 -04001621 get_block_t *get_block, struct writeback_control *wbc,
1622 bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 int err;
1625 sector_t block;
1626 sector_t last_block;
Andrew Mortonf0fbd5f2005-05-05 16:15:48 -07001627 struct buffer_head *bh, *head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001628 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int nr_underway = 0;
Theodore Ts'o6e34eedd2009-04-07 18:12:43 -04001630 int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
Jens Axboe721a9602011-03-09 11:56:30 +01001631 WRITE_SYNC : WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001633 head = create_page_buffers(page, inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 (1 << BH_Dirty)|(1 << BH_Uptodate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /*
1637 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1638 * here, and the (potentially unmapped) buffers may become dirty at
1639 * any time. If a buffer becomes dirty here after we've inspected it
1640 * then we just miss that fact, and the page stays dirty.
1641 *
1642 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1643 * handle that here by just cleaning them.
1644 */
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 bh = head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001647 blocksize = bh->b_size;
1648 bbits = block_size_bits(blocksize);
1649
1650 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1651 last_block = (i_size_read(inode) - 1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /*
1654 * Get all the dirty buffers mapped to disk addresses and
1655 * handle any aliases from the underlying blockdev's mapping.
1656 */
1657 do {
1658 if (block > last_block) {
1659 /*
1660 * mapped buffers outside i_size will occur, because
1661 * this page can be outside i_size when there is a
1662 * truncate in progress.
1663 */
1664 /*
1665 * The buffer was zeroed by block_write_full_page()
1666 */
1667 clear_buffer_dirty(bh);
1668 set_buffer_uptodate(bh);
Alex Tomas29a814d2008-07-11 19:27:31 -04001669 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1670 buffer_dirty(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001671 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 err = get_block(inode, block, bh, 1);
1673 if (err)
1674 goto recover;
Alex Tomas29a814d2008-07-11 19:27:31 -04001675 clear_buffer_delay(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (buffer_new(bh)) {
1677 /* blockdev mappings never come here */
1678 clear_buffer_new(bh);
1679 unmap_underlying_metadata(bh->b_bdev,
1680 bh->b_blocknr);
1681 }
1682 }
1683 bh = bh->b_this_page;
1684 block++;
1685 } while (bh != head);
1686
1687 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (!buffer_mapped(bh))
1689 continue;
1690 /*
1691 * If it's a fully non-blocking write attempt and we cannot
1692 * lock the buffer then redirty the page. Note that this can
Jens Axboe5b0830c2009-09-23 19:37:09 +02001693 * potentially cause a busy-wait loop from writeback threads
1694 * and kswapd activity, but those code paths have their own
1695 * higher-level throttling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Wu Fengguang1b430be2010-10-26 14:21:26 -07001697 if (wbc->sync_mode != WB_SYNC_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 lock_buffer(bh);
Nick Pigginca5de402008-08-02 12:02:13 +02001699 } else if (!trylock_buffer(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 redirty_page_for_writepage(wbc, page);
1701 continue;
1702 }
1703 if (test_clear_buffer_dirty(bh)) {
Chris Mason35c80d52009-04-15 13:22:38 -04001704 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 } else {
1706 unlock_buffer(bh);
1707 }
1708 } while ((bh = bh->b_this_page) != head);
1709
1710 /*
1711 * The page and its buffers are protected by PageWriteback(), so we can
1712 * drop the bh refcounts early.
1713 */
1714 BUG_ON(PageWriteback(page));
1715 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 do {
1718 struct buffer_head *next = bh->b_this_page;
1719 if (buffer_async_write(bh)) {
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001720 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 nr_underway++;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 bh = next;
1724 } while (bh != head);
Andrew Morton05937ba2005-05-05 16:15:47 -07001725 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 err = 0;
1728done:
1729 if (nr_underway == 0) {
1730 /*
1731 * The page was marked dirty, but the buffers were
1732 * clean. Someone wrote them back by hand with
1733 * ll_rw_block/submit_bh. A rare case.
1734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 end_page_writeback(page);
Nick Piggin3d67f2d2007-05-06 14:49:05 -07001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /*
1738 * The page and buffer_heads can be released at any time from
1739 * here on.
1740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742 return err;
1743
1744recover:
1745 /*
1746 * ENOSPC, or some other error. We may already have added some
1747 * blocks to the file, so we need to write these out to avoid
1748 * exposing stale data.
1749 * The page is currently locked and not marked for writeback
1750 */
1751 bh = head;
1752 /* Recovery: lock and submit the mapped buffers */
1753 do {
Alex Tomas29a814d2008-07-11 19:27:31 -04001754 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1755 !buffer_delay(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 lock_buffer(bh);
Chris Mason35c80d52009-04-15 13:22:38 -04001757 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 } else {
1759 /*
1760 * The buffer may have been set dirty during
1761 * attachment to a dirty page.
1762 */
1763 clear_buffer_dirty(bh);
1764 }
1765 } while ((bh = bh->b_this_page) != head);
1766 SetPageError(page);
1767 BUG_ON(PageWriteback(page));
Andrew Morton7e4c3692007-05-08 00:23:27 -07001768 mapping_set_error(page->mapping, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 do {
1771 struct buffer_head *next = bh->b_this_page;
1772 if (buffer_async_write(bh)) {
1773 clear_buffer_dirty(bh);
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001774 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 nr_underway++;
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 bh = next;
1778 } while (bh != head);
Nick Pigginffda9d32007-02-20 13:57:54 -08001779 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto done;
1781}
1782
Nick Pigginafddba42007-10-16 01:25:01 -07001783/*
1784 * If a page has any new buffers, zero them out here, and mark them uptodate
1785 * and dirty so they'll be written out (in order to prevent uninitialised
1786 * block data from leaking). And clear the new bit.
1787 */
1788void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1789{
1790 unsigned int block_start, block_end;
1791 struct buffer_head *head, *bh;
1792
1793 BUG_ON(!PageLocked(page));
1794 if (!page_has_buffers(page))
1795 return;
1796
1797 bh = head = page_buffers(page);
1798 block_start = 0;
1799 do {
1800 block_end = block_start + bh->b_size;
1801
1802 if (buffer_new(bh)) {
1803 if (block_end > from && block_start < to) {
1804 if (!PageUptodate(page)) {
1805 unsigned start, size;
1806
1807 start = max(from, block_start);
1808 size = min(to, block_end) - start;
1809
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001810 zero_user(page, start, size);
Nick Pigginafddba42007-10-16 01:25:01 -07001811 set_buffer_uptodate(bh);
1812 }
1813
1814 clear_buffer_new(bh);
1815 mark_buffer_dirty(bh);
1816 }
1817 }
1818
1819 block_start = block_end;
1820 bh = bh->b_this_page;
1821 } while (bh != head);
1822}
1823EXPORT_SYMBOL(page_zero_new_buffers);
1824
Christoph Hellwigebdec242010-10-06 10:47:23 +02001825int __block_write_begin(struct page *page, loff_t pos, unsigned len,
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001826 get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Christoph Hellwigebdec242010-10-06 10:47:23 +02001828 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1829 unsigned to = from + len;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001830 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 unsigned block_start, block_end;
1832 sector_t block;
1833 int err = 0;
1834 unsigned blocksize, bbits;
1835 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1836
1837 BUG_ON(!PageLocked(page));
1838 BUG_ON(from > PAGE_CACHE_SIZE);
1839 BUG_ON(to > PAGE_CACHE_SIZE);
1840 BUG_ON(from > to);
1841
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001842 head = create_page_buffers(page, inode, 0);
1843 blocksize = head->b_size;
1844 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1847
1848 for(bh = head, block_start = 0; bh != head || !block_start;
1849 block++, block_start=block_end, bh = bh->b_this_page) {
1850 block_end = block_start + blocksize;
1851 if (block_end <= from || block_start >= to) {
1852 if (PageUptodate(page)) {
1853 if (!buffer_uptodate(bh))
1854 set_buffer_uptodate(bh);
1855 }
1856 continue;
1857 }
1858 if (buffer_new(bh))
1859 clear_buffer_new(bh);
1860 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001861 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 err = get_block(inode, block, bh, 1);
1863 if (err)
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (buffer_new(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 unmap_underlying_metadata(bh->b_bdev,
1867 bh->b_blocknr);
1868 if (PageUptodate(page)) {
Nick Piggin637aff42007-10-16 01:25:00 -07001869 clear_buffer_new(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 set_buffer_uptodate(bh);
Nick Piggin637aff42007-10-16 01:25:00 -07001871 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 continue;
1873 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001874 if (block_end > to || block_start < from)
1875 zero_user_segments(page,
1876 to, block_end,
1877 block_start, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 continue;
1879 }
1880 }
1881 if (PageUptodate(page)) {
1882 if (!buffer_uptodate(bh))
1883 set_buffer_uptodate(bh);
1884 continue;
1885 }
1886 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
David Chinner33a266d2007-02-12 00:51:41 -08001887 !buffer_unwritten(bh) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 (block_start < from || block_end > to)) {
1889 ll_rw_block(READ, 1, &bh);
1890 *wait_bh++=bh;
1891 }
1892 }
1893 /*
1894 * If we issued read requests - let them complete.
1895 */
1896 while(wait_bh > wait) {
1897 wait_on_buffer(*--wait_bh);
1898 if (!buffer_uptodate(*wait_bh))
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001899 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Jan Karaf9f07b62011-06-14 00:58:27 +02001901 if (unlikely(err))
Nick Pigginafddba42007-10-16 01:25:01 -07001902 page_zero_new_buffers(page, from, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return err;
1904}
Christoph Hellwigebdec242010-10-06 10:47:23 +02001905EXPORT_SYMBOL(__block_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907static int __block_commit_write(struct inode *inode, struct page *page,
1908 unsigned from, unsigned to)
1909{
1910 unsigned block_start, block_end;
1911 int partial = 0;
1912 unsigned blocksize;
1913 struct buffer_head *bh, *head;
1914
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001915 bh = head = page_buffers(page);
1916 blocksize = bh->b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001918 block_start = 0;
1919 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 block_end = block_start + blocksize;
1921 if (block_end <= from || block_start >= to) {
1922 if (!buffer_uptodate(bh))
1923 partial = 1;
1924 } else {
1925 set_buffer_uptodate(bh);
1926 mark_buffer_dirty(bh);
1927 }
Nick Pigginafddba42007-10-16 01:25:01 -07001928 clear_buffer_new(bh);
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001929
1930 block_start = block_end;
1931 bh = bh->b_this_page;
1932 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /*
1935 * If this is a partial write which happened to make all buffers
1936 * uptodate then we can optimize away a bogus readpage() for
1937 * the next read(). Here we 'discover' whether the page went
1938 * uptodate as a result of this (potentially partial) write.
1939 */
1940 if (!partial)
1941 SetPageUptodate(page);
1942 return 0;
1943}
1944
1945/*
Christoph Hellwig155130a2010-06-04 11:29:58 +02001946 * block_write_begin takes care of the basic task of block allocation and
1947 * bringing partial write blocks uptodate first.
1948 *
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10001949 * The filesystem needs to handle block truncation upon failure.
Nick Pigginafddba42007-10-16 01:25:01 -07001950 */
Christoph Hellwig155130a2010-06-04 11:29:58 +02001951int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
1952 unsigned flags, struct page **pagep, get_block_t *get_block)
Nick Pigginafddba42007-10-16 01:25:01 -07001953{
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001954 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -07001955 struct page *page;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001956 int status;
Nick Pigginafddba42007-10-16 01:25:01 -07001957
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001958 page = grab_cache_page_write_begin(mapping, index, flags);
1959 if (!page)
1960 return -ENOMEM;
Nick Pigginafddba42007-10-16 01:25:01 -07001961
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001962 status = __block_write_begin(page, pos, len, get_block);
Nick Pigginafddba42007-10-16 01:25:01 -07001963 if (unlikely(status)) {
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001964 unlock_page(page);
1965 page_cache_release(page);
1966 page = NULL;
Nick Pigginafddba42007-10-16 01:25:01 -07001967 }
1968
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001969 *pagep = page;
Nick Pigginafddba42007-10-16 01:25:01 -07001970 return status;
1971}
1972EXPORT_SYMBOL(block_write_begin);
1973
1974int block_write_end(struct file *file, struct address_space *mapping,
1975 loff_t pos, unsigned len, unsigned copied,
1976 struct page *page, void *fsdata)
1977{
1978 struct inode *inode = mapping->host;
1979 unsigned start;
1980
1981 start = pos & (PAGE_CACHE_SIZE - 1);
1982
1983 if (unlikely(copied < len)) {
1984 /*
1985 * The buffers that were written will now be uptodate, so we
1986 * don't have to worry about a readpage reading them and
1987 * overwriting a partial write. However if we have encountered
1988 * a short write and only partially written into a buffer, it
1989 * will not be marked uptodate, so a readpage might come in and
1990 * destroy our partial write.
1991 *
1992 * Do the simplest thing, and just treat any short write to a
1993 * non uptodate page as a zero-length write, and force the
1994 * caller to redo the whole thing.
1995 */
1996 if (!PageUptodate(page))
1997 copied = 0;
1998
1999 page_zero_new_buffers(page, start+copied, start+len);
2000 }
2001 flush_dcache_page(page);
2002
2003 /* This could be a short (even 0-length) commit */
2004 __block_commit_write(inode, page, start, start+copied);
2005
2006 return copied;
2007}
2008EXPORT_SYMBOL(block_write_end);
2009
2010int generic_write_end(struct file *file, struct address_space *mapping,
2011 loff_t pos, unsigned len, unsigned copied,
2012 struct page *page, void *fsdata)
2013{
2014 struct inode *inode = mapping->host;
Jan Karac7d206b2008-07-11 19:27:31 -04002015 int i_size_changed = 0;
Nick Pigginafddba42007-10-16 01:25:01 -07002016
2017 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2018
2019 /*
2020 * No need to use i_size_read() here, the i_size
2021 * cannot change under us because we hold i_mutex.
2022 *
2023 * But it's important to update i_size while still holding page lock:
2024 * page writeout could otherwise come in and zero beyond i_size.
2025 */
2026 if (pos+copied > inode->i_size) {
2027 i_size_write(inode, pos+copied);
Jan Karac7d206b2008-07-11 19:27:31 -04002028 i_size_changed = 1;
Nick Pigginafddba42007-10-16 01:25:01 -07002029 }
2030
2031 unlock_page(page);
2032 page_cache_release(page);
2033
Jan Karac7d206b2008-07-11 19:27:31 -04002034 /*
2035 * Don't mark the inode dirty under page lock. First, it unnecessarily
2036 * makes the holding time of page lock longer. Second, it forces lock
2037 * ordering of page lock and transaction start for journaling
2038 * filesystems.
2039 */
2040 if (i_size_changed)
2041 mark_inode_dirty(inode);
2042
Nick Pigginafddba42007-10-16 01:25:01 -07002043 return copied;
2044}
2045EXPORT_SYMBOL(generic_write_end);
2046
2047/*
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002048 * block_is_partially_uptodate checks whether buffers within a page are
2049 * uptodate or not.
2050 *
2051 * Returns true if all buffers which correspond to a file portion
2052 * we want to read are uptodate.
2053 */
2054int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2055 unsigned long from)
2056{
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002057 unsigned block_start, block_end, blocksize;
2058 unsigned to;
2059 struct buffer_head *bh, *head;
2060 int ret = 1;
2061
2062 if (!page_has_buffers(page))
2063 return 0;
2064
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002065 head = page_buffers(page);
2066 blocksize = head->b_size;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002067 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2068 to = from + to;
2069 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2070 return 0;
2071
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002072 bh = head;
2073 block_start = 0;
2074 do {
2075 block_end = block_start + blocksize;
2076 if (block_end > from && block_start < to) {
2077 if (!buffer_uptodate(bh)) {
2078 ret = 0;
2079 break;
2080 }
2081 if (block_end >= to)
2082 break;
2083 }
2084 block_start = block_end;
2085 bh = bh->b_this_page;
2086 } while (bh != head);
2087
2088 return ret;
2089}
2090EXPORT_SYMBOL(block_is_partially_uptodate);
2091
2092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 * Generic "read page" function for block devices that have the normal
2094 * get_block functionality. This is most of the block device filesystems.
2095 * Reads the page asynchronously --- the unlock_buffer() and
2096 * set/clear_buffer_uptodate() functions propagate buffer state into the
2097 * page struct once IO has completed.
2098 */
2099int block_read_full_page(struct page *page, get_block_t *get_block)
2100{
2101 struct inode *inode = page->mapping->host;
2102 sector_t iblock, lblock;
2103 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002104 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int nr, i;
2106 int fully_mapped = 1;
2107
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002108 head = create_page_buffers(page, inode, 0);
2109 blocksize = head->b_size;
2110 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002112 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
2113 lblock = (i_size_read(inode)+blocksize-1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 bh = head;
2115 nr = 0;
2116 i = 0;
2117
2118 do {
2119 if (buffer_uptodate(bh))
2120 continue;
2121
2122 if (!buffer_mapped(bh)) {
Andrew Mortonc64610b2005-05-16 21:53:49 -07002123 int err = 0;
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 fully_mapped = 0;
2126 if (iblock < lblock) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002127 WARN_ON(bh->b_size != blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002128 err = get_block(inode, iblock, bh, 0);
2129 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 SetPageError(page);
2131 }
2132 if (!buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002133 zero_user(page, i * blocksize, blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002134 if (!err)
2135 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 continue;
2137 }
2138 /*
2139 * get_block() might have updated the buffer
2140 * synchronously
2141 */
2142 if (buffer_uptodate(bh))
2143 continue;
2144 }
2145 arr[nr++] = bh;
2146 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2147
2148 if (fully_mapped)
2149 SetPageMappedToDisk(page);
2150
2151 if (!nr) {
2152 /*
2153 * All buffers are uptodate - we can set the page uptodate
2154 * as well. But not if get_block() returned an error.
2155 */
2156 if (!PageError(page))
2157 SetPageUptodate(page);
2158 unlock_page(page);
2159 return 0;
2160 }
2161
2162 /* Stage two: lock the buffers */
2163 for (i = 0; i < nr; i++) {
2164 bh = arr[i];
2165 lock_buffer(bh);
2166 mark_buffer_async_read(bh);
2167 }
2168
2169 /*
2170 * Stage 3: start the IO. Check for uptodateness
2171 * inside the buffer lock in case another process reading
2172 * the underlying blockdev brought it uptodate (the sct fix).
2173 */
2174 for (i = 0; i < nr; i++) {
2175 bh = arr[i];
2176 if (buffer_uptodate(bh))
2177 end_buffer_async_read(bh, 1);
2178 else
2179 submit_bh(READ, bh);
2180 }
2181 return 0;
2182}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002183EXPORT_SYMBOL(block_read_full_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185/* utility function for filesystems that need to do work on expanding
Nick Piggin89e10782007-10-16 01:25:07 -07002186 * truncates. Uses filesystem pagecache writes to allow the filesystem to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 * deal with the hole.
2188 */
Nick Piggin89e10782007-10-16 01:25:07 -07002189int generic_cont_expand_simple(struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct address_space *mapping = inode->i_mapping;
2192 struct page *page;
Nick Piggin89e10782007-10-16 01:25:07 -07002193 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int err;
2195
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10002196 err = inode_newsize_ok(inode, size);
2197 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto out;
2199
Nick Piggin89e10782007-10-16 01:25:07 -07002200 err = pagecache_write_begin(NULL, mapping, size, 0,
2201 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2202 &page, &fsdata);
2203 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto out;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002205
Nick Piggin89e10782007-10-16 01:25:07 -07002206 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2207 BUG_ON(err > 0);
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209out:
2210 return err;
2211}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002212EXPORT_SYMBOL(generic_cont_expand_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Adrian Bunkf1e3af72008-04-29 00:59:01 -07002214static int cont_expand_zero(struct file *file, struct address_space *mapping,
2215 loff_t pos, loff_t *bytes)
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002216{
Nick Piggin89e10782007-10-16 01:25:07 -07002217 struct inode *inode = mapping->host;
2218 unsigned blocksize = 1 << inode->i_blkbits;
2219 struct page *page;
2220 void *fsdata;
2221 pgoff_t index, curidx;
2222 loff_t curpos;
2223 unsigned zerofrom, offset, len;
2224 int err = 0;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002225
Nick Piggin89e10782007-10-16 01:25:07 -07002226 index = pos >> PAGE_CACHE_SHIFT;
2227 offset = pos & ~PAGE_CACHE_MASK;
2228
2229 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2230 zerofrom = curpos & ~PAGE_CACHE_MASK;
2231 if (zerofrom & (blocksize-1)) {
2232 *bytes |= (blocksize-1);
2233 (*bytes)++;
2234 }
2235 len = PAGE_CACHE_SIZE - zerofrom;
2236
2237 err = pagecache_write_begin(file, mapping, curpos, len,
2238 AOP_FLAG_UNINTERRUPTIBLE,
2239 &page, &fsdata);
2240 if (err)
2241 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002242 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002243 err = pagecache_write_end(file, mapping, curpos, len, len,
2244 page, fsdata);
2245 if (err < 0)
2246 goto out;
2247 BUG_ON(err != len);
2248 err = 0;
OGAWA Hirofumi061e9742008-04-28 02:16:28 -07002249
2250 balance_dirty_pages_ratelimited(mapping);
Nick Piggin89e10782007-10-16 01:25:07 -07002251 }
2252
2253 /* page covers the boundary, find the boundary offset */
2254 if (index == curidx) {
2255 zerofrom = curpos & ~PAGE_CACHE_MASK;
2256 /* if we will expand the thing last block will be filled */
2257 if (offset <= zerofrom) {
2258 goto out;
2259 }
2260 if (zerofrom & (blocksize-1)) {
2261 *bytes |= (blocksize-1);
2262 (*bytes)++;
2263 }
2264 len = offset - zerofrom;
2265
2266 err = pagecache_write_begin(file, mapping, curpos, len,
2267 AOP_FLAG_UNINTERRUPTIBLE,
2268 &page, &fsdata);
2269 if (err)
2270 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002271 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002272 err = pagecache_write_end(file, mapping, curpos, len, len,
2273 page, fsdata);
2274 if (err < 0)
2275 goto out;
2276 BUG_ON(err != len);
2277 err = 0;
2278 }
2279out:
2280 return err;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002281}
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283/*
2284 * For moronic filesystems that do not allow holes in file.
2285 * We may have to extend the file.
2286 */
Christoph Hellwig282dc172010-06-04 11:29:55 +02002287int cont_write_begin(struct file *file, struct address_space *mapping,
Nick Piggin89e10782007-10-16 01:25:07 -07002288 loff_t pos, unsigned len, unsigned flags,
2289 struct page **pagep, void **fsdata,
2290 get_block_t *get_block, loff_t *bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 unsigned blocksize = 1 << inode->i_blkbits;
Nick Piggin89e10782007-10-16 01:25:07 -07002294 unsigned zerofrom;
2295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Nick Piggin89e10782007-10-16 01:25:07 -07002297 err = cont_expand_zero(file, mapping, pos, bytes);
2298 if (err)
Christoph Hellwig155130a2010-06-04 11:29:58 +02002299 return err;
Nick Piggin89e10782007-10-16 01:25:07 -07002300
2301 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2302 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2303 *bytes |= (blocksize-1);
2304 (*bytes)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306
Christoph Hellwig155130a2010-06-04 11:29:58 +02002307 return block_write_begin(mapping, pos, len, flags, pagep, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002309EXPORT_SYMBOL(cont_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311int block_commit_write(struct page *page, unsigned from, unsigned to)
2312{
2313 struct inode *inode = page->mapping->host;
2314 __block_commit_write(inode,page,from,to);
2315 return 0;
2316}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002317EXPORT_SYMBOL(block_commit_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
David Chinner54171692007-07-19 17:39:55 +10002319/*
2320 * block_page_mkwrite() is not allowed to change the file size as it gets
2321 * called from a page fault handler when a page is first dirtied. Hence we must
2322 * be careful to check for EOF conditions here. We set the page up correctly
2323 * for a written page which means we get ENOSPC checking when writing into
2324 * holes and correct delalloc and unwritten extent mapping on filesystems that
2325 * support these features.
2326 *
2327 * We are not allowed to take the i_mutex here so we have to play games to
2328 * protect against truncate races as the page could now be beyond EOF. Because
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002329 * truncate writes the inode size before removing pages, once we have the
David Chinner54171692007-07-19 17:39:55 +10002330 * page lock we can determine safely if the page is beyond EOF. If it is not
2331 * beyond EOF, then the page is guaranteed safe against truncation until we
2332 * unlock the page.
Jan Karaea13a862011-05-24 00:23:35 +02002333 *
Jan Kara14da9202012-06-12 16:20:37 +02002334 * Direct callers of this function should protect against filesystem freezing
2335 * using sb_start_write() - sb_end_write() functions.
David Chinner54171692007-07-19 17:39:55 +10002336 */
Jan Kara24da4fa2011-05-24 00:23:34 +02002337int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2338 get_block_t get_block)
David Chinner54171692007-07-19 17:39:55 +10002339{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002340 struct page *page = vmf->page;
David Chinner54171692007-07-19 17:39:55 +10002341 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2342 unsigned long end;
2343 loff_t size;
Jan Kara24da4fa2011-05-24 00:23:34 +02002344 int ret;
David Chinner54171692007-07-19 17:39:55 +10002345
2346 lock_page(page);
2347 size = i_size_read(inode);
2348 if ((page->mapping != inode->i_mapping) ||
Nick Piggin18336332007-07-20 00:31:45 -07002349 (page_offset(page) > size)) {
Jan Kara24da4fa2011-05-24 00:23:34 +02002350 /* We overload EFAULT to mean page got truncated */
2351 ret = -EFAULT;
2352 goto out_unlock;
David Chinner54171692007-07-19 17:39:55 +10002353 }
2354
2355 /* page is wholly or partially inside EOF */
2356 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2357 end = size & ~PAGE_CACHE_MASK;
2358 else
2359 end = PAGE_CACHE_SIZE;
2360
Christoph Hellwigebdec242010-10-06 10:47:23 +02002361 ret = __block_write_begin(page, 0, end, get_block);
David Chinner54171692007-07-19 17:39:55 +10002362 if (!ret)
2363 ret = block_commit_write(page, 0, end);
2364
Jan Kara24da4fa2011-05-24 00:23:34 +02002365 if (unlikely(ret < 0))
2366 goto out_unlock;
Jan Karaea13a862011-05-24 00:23:35 +02002367 set_page_dirty(page);
Darrick J. Wongd76ee182011-05-27 12:23:41 -07002368 wait_on_page_writeback(page);
Jan Kara24da4fa2011-05-24 00:23:34 +02002369 return 0;
2370out_unlock:
2371 unlock_page(page);
David Chinner54171692007-07-19 17:39:55 +10002372 return ret;
2373}
Jan Kara24da4fa2011-05-24 00:23:34 +02002374EXPORT_SYMBOL(__block_page_mkwrite);
2375
2376int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2377 get_block_t get_block)
2378{
Jan Karaea13a862011-05-24 00:23:35 +02002379 int ret;
2380 struct super_block *sb = vma->vm_file->f_path.dentry->d_inode->i_sb;
Jan Kara24da4fa2011-05-24 00:23:34 +02002381
Jan Kara14da9202012-06-12 16:20:37 +02002382 sb_start_pagefault(sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04002383
2384 /*
2385 * Update file times before taking page lock. We may end up failing the
2386 * fault so this update may be superfluous but who really cares...
2387 */
2388 file_update_time(vma->vm_file);
2389
Jan Karaea13a862011-05-24 00:23:35 +02002390 ret = __block_page_mkwrite(vma, vmf, get_block);
Jan Kara14da9202012-06-12 16:20:37 +02002391 sb_end_pagefault(sb);
Jan Kara24da4fa2011-05-24 00:23:34 +02002392 return block_page_mkwrite_return(ret);
2393}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002394EXPORT_SYMBOL(block_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002397 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 * immediately, while under the page lock. So it needs a special end_io
2399 * handler which does not touch the bh after unlocking it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 */
2401static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2402{
Dmitry Monakhov68671f32007-10-16 01:24:47 -07002403 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002407 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2408 * the page (converting it to circular linked list and taking care of page
2409 * dirty races).
2410 */
2411static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2412{
2413 struct buffer_head *bh;
2414
2415 BUG_ON(!PageLocked(page));
2416
2417 spin_lock(&page->mapping->private_lock);
2418 bh = head;
2419 do {
2420 if (PageDirty(page))
2421 set_buffer_dirty(bh);
2422 if (!bh->b_this_page)
2423 bh->b_this_page = head;
2424 bh = bh->b_this_page;
2425 } while (bh != head);
2426 attach_page_buffers(page, head);
2427 spin_unlock(&page->mapping->private_lock);
2428}
2429
2430/*
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002431 * On entry, the page is fully not uptodate.
2432 * On exit the page is fully uptodate in the areas outside (from,to)
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002433 * The filesystem needs to handle block truncation upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002435int nobh_write_begin(struct address_space *mapping,
Nick Piggin03158cd2007-10-16 01:25:25 -07002436 loff_t pos, unsigned len, unsigned flags,
2437 struct page **pagep, void **fsdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 get_block_t *get_block)
2439{
Nick Piggin03158cd2007-10-16 01:25:25 -07002440 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 const unsigned blkbits = inode->i_blkbits;
2442 const unsigned blocksize = 1 << blkbits;
Nick Piggina4b06722007-10-16 01:24:48 -07002443 struct buffer_head *head, *bh;
Nick Piggin03158cd2007-10-16 01:25:25 -07002444 struct page *page;
2445 pgoff_t index;
2446 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 unsigned block_in_page;
Nick Piggina4b06722007-10-16 01:24:48 -07002448 unsigned block_start, block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 sector_t block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int nr_reads = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 int ret = 0;
2452 int is_mapped_to_disk = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Nick Piggin03158cd2007-10-16 01:25:25 -07002454 index = pos >> PAGE_CACHE_SHIFT;
2455 from = pos & (PAGE_CACHE_SIZE - 1);
2456 to = from + len;
2457
Nick Piggin54566b22009-01-04 12:00:53 -08002458 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin03158cd2007-10-16 01:25:25 -07002459 if (!page)
2460 return -ENOMEM;
2461 *pagep = page;
2462 *fsdata = NULL;
2463
2464 if (page_has_buffers(page)) {
Namhyung Kim309f77a2010-10-25 15:01:12 +09002465 ret = __block_write_begin(page, pos, len, get_block);
2466 if (unlikely(ret))
2467 goto out_release;
2468 return ret;
Nick Piggin03158cd2007-10-16 01:25:25 -07002469 }
Nick Piggina4b06722007-10-16 01:24:48 -07002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (PageMappedToDisk(page))
2472 return 0;
2473
Nick Piggina4b06722007-10-16 01:24:48 -07002474 /*
2475 * Allocate buffers so that we can keep track of state, and potentially
2476 * attach them to the page if an error occurs. In the common case of
2477 * no error, they will just be freed again without ever being attached
2478 * to the page (which is all OK, because we're under the page lock).
2479 *
2480 * Be careful: the buffer linked list is a NULL terminated one, rather
2481 * than the circular one we're used to.
2482 */
2483 head = alloc_page_buffers(page, blocksize, 0);
Nick Piggin03158cd2007-10-16 01:25:25 -07002484 if (!head) {
2485 ret = -ENOMEM;
2486 goto out_release;
2487 }
Nick Piggina4b06722007-10-16 01:24:48 -07002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 /*
2492 * We loop across all blocks in the page, whether or not they are
2493 * part of the affected region. This is so we can discover if the
2494 * page is fully mapped-to-disk.
2495 */
Nick Piggina4b06722007-10-16 01:24:48 -07002496 for (block_start = 0, block_in_page = 0, bh = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 block_start < PAGE_CACHE_SIZE;
Nick Piggina4b06722007-10-16 01:24:48 -07002498 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 int create;
2500
Nick Piggina4b06722007-10-16 01:24:48 -07002501 block_end = block_start + blocksize;
2502 bh->b_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 create = 1;
2504 if (block_start >= to)
2505 create = 0;
2506 ret = get_block(inode, block_in_file + block_in_page,
Nick Piggina4b06722007-10-16 01:24:48 -07002507 bh, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (ret)
2509 goto failed;
Nick Piggina4b06722007-10-16 01:24:48 -07002510 if (!buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 is_mapped_to_disk = 0;
Nick Piggina4b06722007-10-16 01:24:48 -07002512 if (buffer_new(bh))
2513 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2514 if (PageUptodate(page)) {
2515 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 continue;
Nick Piggina4b06722007-10-16 01:24:48 -07002517 }
2518 if (buffer_new(bh) || !buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002519 zero_user_segments(page, block_start, from,
2520 to, block_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 continue;
2522 }
Nick Piggina4b06722007-10-16 01:24:48 -07002523 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 continue; /* reiserfs does this */
2525 if (block_start < from || block_end > to) {
Nick Piggina4b06722007-10-16 01:24:48 -07002526 lock_buffer(bh);
2527 bh->b_end_io = end_buffer_read_nobh;
2528 submit_bh(READ, bh);
2529 nr_reads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 }
2532
2533 if (nr_reads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * The page is locked, so these buffers are protected from
2536 * any VM or truncate activity. Hence we don't need to care
2537 * for the buffer_head refcounts.
2538 */
Nick Piggina4b06722007-10-16 01:24:48 -07002539 for (bh = head; bh; bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 wait_on_buffer(bh);
2541 if (!buffer_uptodate(bh))
2542 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 if (ret)
2545 goto failed;
2546 }
2547
2548 if (is_mapped_to_disk)
2549 SetPageMappedToDisk(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Nick Piggin03158cd2007-10-16 01:25:25 -07002551 *fsdata = head; /* to be released by nobh_write_end */
Nick Piggina4b06722007-10-16 01:24:48 -07002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return 0;
2554
2555failed:
Nick Piggin03158cd2007-10-16 01:25:25 -07002556 BUG_ON(!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /*
Nick Piggina4b06722007-10-16 01:24:48 -07002558 * Error recovery is a bit difficult. We need to zero out blocks that
2559 * were newly allocated, and dirty them to ensure they get written out.
2560 * Buffers need to be attached to the page at this point, otherwise
2561 * the handling of potential IO errors during writeout would be hard
2562 * (could try doing synchronous writeout, but what if that fails too?)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 */
Nick Piggin03158cd2007-10-16 01:25:25 -07002564 attach_nobh_buffers(page, head);
2565 page_zero_new_buffers(page, from, to);
Nick Piggina4b06722007-10-16 01:24:48 -07002566
Nick Piggin03158cd2007-10-16 01:25:25 -07002567out_release:
2568 unlock_page(page);
2569 page_cache_release(page);
2570 *pagep = NULL;
Nick Piggina4b06722007-10-16 01:24:48 -07002571
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002572 return ret;
2573}
Nick Piggin03158cd2007-10-16 01:25:25 -07002574EXPORT_SYMBOL(nobh_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Nick Piggin03158cd2007-10-16 01:25:25 -07002576int nobh_write_end(struct file *file, struct address_space *mapping,
2577 loff_t pos, unsigned len, unsigned copied,
2578 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct inode *inode = page->mapping->host;
Nick Pigginefdc3132007-10-21 06:57:41 +02002581 struct buffer_head *head = fsdata;
Nick Piggin03158cd2007-10-16 01:25:25 -07002582 struct buffer_head *bh;
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002583 BUG_ON(fsdata != NULL && page_has_buffers(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Dave Kleikampd4cf1092009-02-06 14:59:26 -06002585 if (unlikely(copied < len) && head)
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002586 attach_nobh_buffers(page, head);
2587 if (page_has_buffers(page))
2588 return generic_write_end(file, mapping, pos, len,
2589 copied, page, fsdata);
Nick Piggina4b06722007-10-16 01:24:48 -07002590
Nick Piggin22c8ca72007-02-20 13:58:09 -08002591 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 set_page_dirty(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002593 if (pos+copied > inode->i_size) {
2594 i_size_write(inode, pos+copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 mark_inode_dirty(inode);
2596 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002597
2598 unlock_page(page);
2599 page_cache_release(page);
2600
Nick Piggin03158cd2007-10-16 01:25:25 -07002601 while (head) {
2602 bh = head;
2603 head = head->b_this_page;
2604 free_buffer_head(bh);
2605 }
2606
2607 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
Nick Piggin03158cd2007-10-16 01:25:25 -07002609EXPORT_SYMBOL(nobh_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611/*
2612 * nobh_writepage() - based on block_full_write_page() except
2613 * that it tries to operate without attaching bufferheads to
2614 * the page.
2615 */
2616int nobh_writepage(struct page *page, get_block_t *get_block,
2617 struct writeback_control *wbc)
2618{
2619 struct inode * const inode = page->mapping->host;
2620 loff_t i_size = i_size_read(inode);
2621 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2622 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 int ret;
2624
2625 /* Is the page fully inside i_size? */
2626 if (page->index < end_index)
2627 goto out;
2628
2629 /* Is the page fully outside i_size? (truncate in progress) */
2630 offset = i_size & (PAGE_CACHE_SIZE-1);
2631 if (page->index >= end_index+1 || !offset) {
2632 /*
2633 * The page may have dirty, unmapped buffers. For example,
2634 * they may have been added in ext3_writepage(). Make them
2635 * freeable here, so the page does not leak.
2636 */
2637#if 0
2638 /* Not really sure about this - do we need this ? */
2639 if (page->mapping->a_ops->invalidatepage)
2640 page->mapping->a_ops->invalidatepage(page, offset);
2641#endif
2642 unlock_page(page);
2643 return 0; /* don't care */
2644 }
2645
2646 /*
2647 * The page straddles i_size. It must be zeroed out on each and every
2648 * writepage invocation because it may be mmapped. "A file is mapped
2649 * in multiples of the page size. For a file that is not a multiple of
2650 * the page size, the remaining memory is zeroed when mapped, and
2651 * writes to that region are not written out to the file."
2652 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002653 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654out:
2655 ret = mpage_writepage(page, get_block, wbc);
2656 if (ret == -EAGAIN)
Chris Mason35c80d52009-04-15 13:22:38 -04002657 ret = __block_write_full_page(inode, page, get_block, wbc,
2658 end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return ret;
2660}
2661EXPORT_SYMBOL(nobh_writepage);
2662
Nick Piggin03158cd2007-10-16 01:25:25 -07002663int nobh_truncate_page(struct address_space *mapping,
2664 loff_t from, get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2667 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Nick Piggin03158cd2007-10-16 01:25:25 -07002668 unsigned blocksize;
2669 sector_t iblock;
2670 unsigned length, pos;
2671 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct page *page;
Nick Piggin03158cd2007-10-16 01:25:25 -07002673 struct buffer_head map_bh;
2674 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Nick Piggin03158cd2007-10-16 01:25:25 -07002676 blocksize = 1 << inode->i_blkbits;
2677 length = offset & (blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Nick Piggin03158cd2007-10-16 01:25:25 -07002679 /* Block boundary? Nothing to do */
2680 if (!length)
2681 return 0;
2682
2683 length = blocksize - length;
2684 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 page = grab_cache_page(mapping, index);
Nick Piggin03158cd2007-10-16 01:25:25 -07002687 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (!page)
2689 goto out;
2690
Nick Piggin03158cd2007-10-16 01:25:25 -07002691 if (page_has_buffers(page)) {
2692has_buffers:
2693 unlock_page(page);
2694 page_cache_release(page);
2695 return block_truncate_page(mapping, from, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002697
2698 /* Find the buffer that contains "offset" */
2699 pos = blocksize;
2700 while (offset >= pos) {
2701 iblock++;
2702 pos += blocksize;
2703 }
2704
Theodore Ts'o460bcf52009-05-12 07:37:56 -04002705 map_bh.b_size = blocksize;
2706 map_bh.b_state = 0;
Nick Piggin03158cd2007-10-16 01:25:25 -07002707 err = get_block(inode, iblock, &map_bh, 0);
2708 if (err)
2709 goto unlock;
2710 /* unmapped? It's a hole - nothing to do */
2711 if (!buffer_mapped(&map_bh))
2712 goto unlock;
2713
2714 /* Ok, it's mapped. Make sure it's up-to-date */
2715 if (!PageUptodate(page)) {
2716 err = mapping->a_ops->readpage(NULL, page);
2717 if (err) {
2718 page_cache_release(page);
2719 goto out;
2720 }
2721 lock_page(page);
2722 if (!PageUptodate(page)) {
2723 err = -EIO;
2724 goto unlock;
2725 }
2726 if (page_has_buffers(page))
2727 goto has_buffers;
2728 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002729 zero_user(page, offset, length);
Nick Piggin03158cd2007-10-16 01:25:25 -07002730 set_page_dirty(page);
2731 err = 0;
2732
2733unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 unlock_page(page);
2735 page_cache_release(page);
2736out:
Nick Piggin03158cd2007-10-16 01:25:25 -07002737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739EXPORT_SYMBOL(nobh_truncate_page);
2740
2741int block_truncate_page(struct address_space *mapping,
2742 loff_t from, get_block_t *get_block)
2743{
2744 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2745 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2746 unsigned blocksize;
Andrew Morton54b21a72006-01-08 01:03:05 -08002747 sector_t iblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 unsigned length, pos;
2749 struct inode *inode = mapping->host;
2750 struct page *page;
2751 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 int err;
2753
2754 blocksize = 1 << inode->i_blkbits;
2755 length = offset & (blocksize - 1);
2756
2757 /* Block boundary? Nothing to do */
2758 if (!length)
2759 return 0;
2760
2761 length = blocksize - length;
Andrew Morton54b21a72006-01-08 01:03:05 -08002762 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 page = grab_cache_page(mapping, index);
2765 err = -ENOMEM;
2766 if (!page)
2767 goto out;
2768
2769 if (!page_has_buffers(page))
2770 create_empty_buffers(page, blocksize, 0);
2771
2772 /* Find the buffer that contains "offset" */
2773 bh = page_buffers(page);
2774 pos = blocksize;
2775 while (offset >= pos) {
2776 bh = bh->b_this_page;
2777 iblock++;
2778 pos += blocksize;
2779 }
2780
2781 err = 0;
2782 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002783 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 err = get_block(inode, iblock, bh, 0);
2785 if (err)
2786 goto unlock;
2787 /* unmapped? It's a hole - nothing to do */
2788 if (!buffer_mapped(bh))
2789 goto unlock;
2790 }
2791
2792 /* Ok, it's mapped. Make sure it's up-to-date */
2793 if (PageUptodate(page))
2794 set_buffer_uptodate(bh);
2795
David Chinner33a266d2007-02-12 00:51:41 -08002796 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 err = -EIO;
2798 ll_rw_block(READ, 1, &bh);
2799 wait_on_buffer(bh);
2800 /* Uhhuh. Read error. Complain and punt. */
2801 if (!buffer_uptodate(bh))
2802 goto unlock;
2803 }
2804
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002805 zero_user(page, offset, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 mark_buffer_dirty(bh);
2807 err = 0;
2808
2809unlock:
2810 unlock_page(page);
2811 page_cache_release(page);
2812out:
2813 return err;
2814}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002815EXPORT_SYMBOL(block_truncate_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817/*
2818 * The generic ->writepage function for buffer-backed address_spaces
Chris Mason35c80d52009-04-15 13:22:38 -04002819 * this form passes in the end_io handler used to finish the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 */
Chris Mason35c80d52009-04-15 13:22:38 -04002821int block_write_full_page_endio(struct page *page, get_block_t *get_block,
2822 struct writeback_control *wbc, bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 struct inode * const inode = page->mapping->host;
2825 loff_t i_size = i_size_read(inode);
2826 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2827 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Is the page fully inside i_size? */
2830 if (page->index < end_index)
Chris Mason35c80d52009-04-15 13:22:38 -04002831 return __block_write_full_page(inode, page, get_block, wbc,
2832 handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 /* Is the page fully outside i_size? (truncate in progress) */
2835 offset = i_size & (PAGE_CACHE_SIZE-1);
2836 if (page->index >= end_index+1 || !offset) {
2837 /*
2838 * The page may have dirty, unmapped buffers. For example,
2839 * they may have been added in ext3_writepage(). Make them
2840 * freeable here, so the page does not leak.
2841 */
Jan Karaaaa40592005-10-30 15:00:16 -08002842 do_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 unlock_page(page);
2844 return 0; /* don't care */
2845 }
2846
2847 /*
2848 * The page straddles i_size. It must be zeroed out on each and every
Adam Buchbinder2a61aa42009-12-11 16:35:40 -05002849 * writepage invocation because it may be mmapped. "A file is mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 * in multiples of the page size. For a file that is not a multiple of
2851 * the page size, the remaining memory is zeroed when mapped, and
2852 * writes to that region are not written out to the file."
2853 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002854 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Chris Mason35c80d52009-04-15 13:22:38 -04002855 return __block_write_full_page(inode, page, get_block, wbc, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002857EXPORT_SYMBOL(block_write_full_page_endio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Chris Mason35c80d52009-04-15 13:22:38 -04002859/*
2860 * The generic ->writepage function for buffer-backed address_spaces
2861 */
2862int block_write_full_page(struct page *page, get_block_t *get_block,
2863 struct writeback_control *wbc)
2864{
2865 return block_write_full_page_endio(page, get_block, wbc,
2866 end_buffer_async_write);
2867}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002868EXPORT_SYMBOL(block_write_full_page);
Chris Mason35c80d52009-04-15 13:22:38 -04002869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2871 get_block_t *get_block)
2872{
2873 struct buffer_head tmp;
2874 struct inode *inode = mapping->host;
2875 tmp.b_state = 0;
2876 tmp.b_blocknr = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002877 tmp.b_size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 get_block(inode, block, &tmp, 0);
2879 return tmp.b_blocknr;
2880}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002881EXPORT_SYMBOL(generic_block_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
NeilBrown6712ecf2007-09-27 12:47:43 +02002883static void end_bio_bh_io_sync(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 struct buffer_head *bh = bio->bi_private;
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (err == -EOPNOTSUPP) {
2888 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890
Keith Mannthey08bafc02008-11-25 10:24:35 +01002891 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2892 set_bit(BH_Quiet, &bh->b_state);
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2895 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
Linus Torvalds57302e02012-12-04 08:25:11 -08002898/*
2899 * This allows us to do IO even on the odd last sectors
2900 * of a device, even if the bh block size is some multiple
2901 * of the physical sector size.
2902 *
2903 * We'll just truncate the bio to the size of the device,
2904 * and clear the end of the buffer head manually.
2905 *
2906 * Truly out-of-range accesses will turn into actual IO
2907 * errors, this only handles the "we need to be able to
2908 * do IO at the final sector" case.
2909 */
2910static void guard_bh_eod(int rw, struct bio *bio, struct buffer_head *bh)
2911{
2912 sector_t maxsector;
2913 unsigned bytes;
2914
2915 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
2916 if (!maxsector)
2917 return;
2918
2919 /*
2920 * If the *whole* IO is past the end of the device,
2921 * let it through, and the IO layer will turn it into
2922 * an EIO.
2923 */
2924 if (unlikely(bio->bi_sector >= maxsector))
2925 return;
2926
2927 maxsector -= bio->bi_sector;
2928 bytes = bio->bi_size;
2929 if (likely((bytes >> 9) <= maxsector))
2930 return;
2931
2932 /* Uhhuh. We've got a bh that straddles the device size! */
2933 bytes = maxsector << 9;
2934
2935 /* Truncate the bio.. */
2936 bio->bi_size = bytes;
2937 bio->bi_io_vec[0].bv_len = bytes;
2938
2939 /* ..and clear the end of the buffer for reads */
Dan Carpenter27d7c2a2012-12-05 20:01:24 +03002940 if ((rw & RW_MASK) == READ) {
Linus Torvalds57302e02012-12-04 08:25:11 -08002941 void *kaddr = kmap_atomic(bh->b_page);
2942 memset(kaddr + bh_offset(bh) + bytes, 0, bh->b_size - bytes);
2943 kunmap_atomic(kaddr);
2944 }
2945}
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947int submit_bh(int rw, struct buffer_head * bh)
2948{
2949 struct bio *bio;
2950 int ret = 0;
2951
2952 BUG_ON(!buffer_locked(bh));
2953 BUG_ON(!buffer_mapped(bh));
2954 BUG_ON(!bh->b_end_io);
Aneesh Kumar K.V8fb0e342009-05-12 16:22:37 -04002955 BUG_ON(buffer_delay(bh));
2956 BUG_ON(buffer_unwritten(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Jens Axboe48fd4f92008-08-22 10:00:36 +02002958 /*
Jens Axboe48fd4f92008-08-22 10:00:36 +02002959 * Only clear out a write error when rewriting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
Jens Axboe48fd4f92008-08-22 10:00:36 +02002961 if (test_set_buffer_req(bh) && (rw & WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 clear_buffer_write_io_error(bh);
2963
2964 /*
2965 * from here on down, it's all bio -- do the initial mapping,
2966 * submit_bio -> generic_make_request may further map this bio around
2967 */
2968 bio = bio_alloc(GFP_NOIO, 1);
2969
2970 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2971 bio->bi_bdev = bh->b_bdev;
2972 bio->bi_io_vec[0].bv_page = bh->b_page;
2973 bio->bi_io_vec[0].bv_len = bh->b_size;
2974 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2975
2976 bio->bi_vcnt = 1;
2977 bio->bi_idx = 0;
2978 bio->bi_size = bh->b_size;
2979
2980 bio->bi_end_io = end_bio_bh_io_sync;
2981 bio->bi_private = bh;
2982
Linus Torvalds57302e02012-12-04 08:25:11 -08002983 /* Take care of bh's that straddle the end of the device */
2984 guard_bh_eod(rw, bio, bh);
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 bio_get(bio);
2987 submit_bio(rw, bio);
2988
2989 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2990 ret = -EOPNOTSUPP;
2991
2992 bio_put(bio);
2993 return ret;
2994}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002995EXPORT_SYMBOL(submit_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997/**
2998 * ll_rw_block: low-level access to block devices (DEPRECATED)
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02002999 * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 * @nr: number of &struct buffer_heads in the array
3001 * @bhs: array of pointers to &struct buffer_head
3002 *
Jan Karaa7662232005-09-06 15:19:10 -07003003 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
3004 * requests an I/O operation on them, either a %READ or a %WRITE. The third
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003005 * %READA option is described in the documentation for generic_make_request()
3006 * which ll_rw_block() calls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 *
3008 * This function drops any buffer that it cannot get a lock on (with the
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003009 * BH_Lock state bit), any buffer that appears to be clean when doing a write
3010 * request, and any buffer that appears to be up-to-date when doing read
3011 * request. Further it marks as clean buffers that are processed for
3012 * writing (the buffer cache won't assume that they are actually clean
3013 * until the buffer gets unlocked).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 *
3015 * ll_rw_block sets b_end_io to simple completion handler that marks
3016 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
3017 * any waiters.
3018 *
3019 * All of the buffers must be for the same device, and must also be a
3020 * multiple of the current approved size for the device.
3021 */
3022void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
3023{
3024 int i;
3025
3026 for (i = 0; i < nr; i++) {
3027 struct buffer_head *bh = bhs[i];
3028
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003029 if (!trylock_buffer(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 continue;
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003031 if (rw == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (test_clear_buffer_dirty(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003033 bh->b_end_io = end_buffer_write_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003034 get_bh(bh);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003035 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 continue;
3037 }
3038 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (!buffer_uptodate(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003040 bh->b_end_io = end_buffer_read_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003041 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 submit_bh(rw, bh);
3043 continue;
3044 }
3045 }
3046 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003049EXPORT_SYMBOL(ll_rw_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003051void write_dirty_buffer(struct buffer_head *bh, int rw)
3052{
3053 lock_buffer(bh);
3054 if (!test_clear_buffer_dirty(bh)) {
3055 unlock_buffer(bh);
3056 return;
3057 }
3058 bh->b_end_io = end_buffer_write_sync;
3059 get_bh(bh);
3060 submit_bh(rw, bh);
3061}
3062EXPORT_SYMBOL(write_dirty_buffer);
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064/*
3065 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3066 * and then start new I/O and then wait upon it. The caller must have a ref on
3067 * the buffer_head.
3068 */
Christoph Hellwig87e99512010-08-11 17:05:45 +02003069int __sync_dirty_buffer(struct buffer_head *bh, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
3071 int ret = 0;
3072
3073 WARN_ON(atomic_read(&bh->b_count) < 1);
3074 lock_buffer(bh);
3075 if (test_clear_buffer_dirty(bh)) {
3076 get_bh(bh);
3077 bh->b_end_io = end_buffer_write_sync;
Christoph Hellwig87e99512010-08-11 17:05:45 +02003078 ret = submit_bh(rw, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 wait_on_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!ret && !buffer_uptodate(bh))
3081 ret = -EIO;
3082 } else {
3083 unlock_buffer(bh);
3084 }
3085 return ret;
3086}
Christoph Hellwig87e99512010-08-11 17:05:45 +02003087EXPORT_SYMBOL(__sync_dirty_buffer);
3088
3089int sync_dirty_buffer(struct buffer_head *bh)
3090{
3091 return __sync_dirty_buffer(bh, WRITE_SYNC);
3092}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003093EXPORT_SYMBOL(sync_dirty_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095/*
3096 * try_to_free_buffers() checks if all the buffers on this particular page
3097 * are unused, and releases them if so.
3098 *
3099 * Exclusion against try_to_free_buffers may be obtained by either
3100 * locking the page or by holding its mapping's private_lock.
3101 *
3102 * If the page is dirty but all the buffers are clean then we need to
3103 * be sure to mark the page clean as well. This is because the page
3104 * may be against a block device, and a later reattachment of buffers
3105 * to a dirty page will set *all* buffers dirty. Which would corrupt
3106 * filesystem data on the same device.
3107 *
3108 * The same applies to regular filesystem pages: if all the buffers are
3109 * clean then we set the page clean and proceed. To do that, we require
3110 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3111 * private_lock.
3112 *
3113 * try_to_free_buffers() is non-blocking.
3114 */
3115static inline int buffer_busy(struct buffer_head *bh)
3116{
3117 return atomic_read(&bh->b_count) |
3118 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3119}
3120
3121static int
3122drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3123{
3124 struct buffer_head *head = page_buffers(page);
3125 struct buffer_head *bh;
3126
3127 bh = head;
3128 do {
akpm@osdl.orgde7d5a32005-05-01 08:58:39 -07003129 if (buffer_write_io_error(bh) && page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 set_bit(AS_EIO, &page->mapping->flags);
3131 if (buffer_busy(bh))
3132 goto failed;
3133 bh = bh->b_this_page;
3134 } while (bh != head);
3135
3136 do {
3137 struct buffer_head *next = bh->b_this_page;
3138
Jan Kara535ee2f2008-02-08 04:21:59 -08003139 if (bh->b_assoc_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 __remove_assoc_queue(bh);
3141 bh = next;
3142 } while (bh != head);
3143 *buffers_to_free = head;
3144 __clear_page_buffers(page);
3145 return 1;
3146failed:
3147 return 0;
3148}
3149
3150int try_to_free_buffers(struct page *page)
3151{
3152 struct address_space * const mapping = page->mapping;
3153 struct buffer_head *buffers_to_free = NULL;
3154 int ret = 0;
3155
3156 BUG_ON(!PageLocked(page));
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003157 if (PageWriteback(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 return 0;
3159
3160 if (mapping == NULL) { /* can this still happen? */
3161 ret = drop_buffers(page, &buffers_to_free);
3162 goto out;
3163 }
3164
3165 spin_lock(&mapping->private_lock);
3166 ret = drop_buffers(page, &buffers_to_free);
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003167
3168 /*
3169 * If the filesystem writes its buffers by hand (eg ext3)
3170 * then we can have clean buffers against a dirty page. We
3171 * clean the page here; otherwise the VM will never notice
3172 * that the filesystem did any IO at all.
3173 *
3174 * Also, during truncate, discard_buffer will have marked all
3175 * the page's buffers clean. We discover that here and clean
3176 * the page also.
Nick Piggin87df7242007-01-30 14:36:27 +11003177 *
3178 * private_lock must be held over this entire operation in order
3179 * to synchronise against __set_page_dirty_buffers and prevent the
3180 * dirty bit from being lost.
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003181 */
3182 if (ret)
3183 cancel_dirty_page(page, PAGE_CACHE_SIZE);
Nick Piggin87df7242007-01-30 14:36:27 +11003184 spin_unlock(&mapping->private_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185out:
3186 if (buffers_to_free) {
3187 struct buffer_head *bh = buffers_to_free;
3188
3189 do {
3190 struct buffer_head *next = bh->b_this_page;
3191 free_buffer_head(bh);
3192 bh = next;
3193 } while (bh != buffers_to_free);
3194 }
3195 return ret;
3196}
3197EXPORT_SYMBOL(try_to_free_buffers);
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/*
3200 * There are no bdflush tunables left. But distributions are
3201 * still running obsolete flush daemons, so we terminate them here.
3202 *
3203 * Use of bdflush() is deprecated and will be removed in a future kernel.
Jens Axboe5b0830c2009-09-23 19:37:09 +02003204 * The `flush-X' kernel threads fully replace bdflush daemons and this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01003206SYSCALL_DEFINE2(bdflush, int, func, long, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 static int msg_count;
3209
3210 if (!capable(CAP_SYS_ADMIN))
3211 return -EPERM;
3212
3213 if (msg_count < 5) {
3214 msg_count++;
3215 printk(KERN_INFO
3216 "warning: process `%s' used the obsolete bdflush"
3217 " system call\n", current->comm);
3218 printk(KERN_INFO "Fix your initscripts?\n");
3219 }
3220
3221 if (func == 1)
3222 do_exit(0);
3223 return 0;
3224}
3225
3226/*
3227 * Buffer-head allocation
3228 */
Shai Fultheima0a9b042012-05-15 12:29:52 +03003229static struct kmem_cache *bh_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231/*
3232 * Once the number of bh's in the machine exceeds this level, we start
3233 * stripping them in writeback.
3234 */
3235static int max_buffer_heads;
3236
3237int buffer_heads_over_limit;
3238
3239struct bh_accounting {
3240 int nr; /* Number of live bh's */
3241 int ratelimit; /* Limit cacheline bouncing */
3242};
3243
3244static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3245
3246static void recalc_bh_state(void)
3247{
3248 int i;
3249 int tot = 0;
3250
Christoph Lameteree1be862010-12-06 11:40:05 -06003251 if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return;
Christoph Lameterc7b92512010-12-06 11:16:28 -06003253 __this_cpu_write(bh_accounting.ratelimit, 0);
Eric Dumazet8a143422006-03-24 03:18:10 -08003254 for_each_online_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 tot += per_cpu(bh_accounting, i).nr;
3256 buffer_heads_over_limit = (tot > max_buffer_heads);
3257}
Christoph Lameterc7b92512010-12-06 11:16:28 -06003258
Al Virodd0fc662005-10-07 07:46:04 +01003259struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Richard Kennedy019b4d12010-03-10 15:20:33 -08003261 struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (ret) {
Christoph Lametera35afb82007-05-16 22:10:57 -07003263 INIT_LIST_HEAD(&ret->b_assoc_buffers);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003264 preempt_disable();
3265 __this_cpu_inc(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003267 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
3269 return ret;
3270}
3271EXPORT_SYMBOL(alloc_buffer_head);
3272
3273void free_buffer_head(struct buffer_head *bh)
3274{
3275 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3276 kmem_cache_free(bh_cachep, bh);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003277 preempt_disable();
3278 __this_cpu_dec(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003280 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282EXPORT_SYMBOL(free_buffer_head);
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284static void buffer_exit_cpu(int cpu)
3285{
3286 int i;
3287 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3288
3289 for (i = 0; i < BH_LRU_SIZE; i++) {
3290 brelse(b->bhs[i]);
3291 b->bhs[i] = NULL;
3292 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06003293 this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
Eric Dumazet8a143422006-03-24 03:18:10 -08003294 per_cpu(bh_accounting, cpu).nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
3297static int buffer_cpu_notify(struct notifier_block *self,
3298 unsigned long action, void *hcpu)
3299{
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003300 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 buffer_exit_cpu((unsigned long)hcpu);
3302 return NOTIFY_OK;
3303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003305/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003306 * bh_uptodate_or_lock - Test whether the buffer is uptodate
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003307 * @bh: struct buffer_head
3308 *
3309 * Return true if the buffer is up-to-date and false,
3310 * with the buffer locked, if not.
3311 */
3312int bh_uptodate_or_lock(struct buffer_head *bh)
3313{
3314 if (!buffer_uptodate(bh)) {
3315 lock_buffer(bh);
3316 if (!buffer_uptodate(bh))
3317 return 0;
3318 unlock_buffer(bh);
3319 }
3320 return 1;
3321}
3322EXPORT_SYMBOL(bh_uptodate_or_lock);
3323
3324/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003325 * bh_submit_read - Submit a locked buffer for reading
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003326 * @bh: struct buffer_head
3327 *
3328 * Returns zero on success and -EIO on error.
3329 */
3330int bh_submit_read(struct buffer_head *bh)
3331{
3332 BUG_ON(!buffer_locked(bh));
3333
3334 if (buffer_uptodate(bh)) {
3335 unlock_buffer(bh);
3336 return 0;
3337 }
3338
3339 get_bh(bh);
3340 bh->b_end_io = end_buffer_read_sync;
3341 submit_bh(READ, bh);
3342 wait_on_buffer(bh);
3343 if (buffer_uptodate(bh))
3344 return 0;
3345 return -EIO;
3346}
3347EXPORT_SYMBOL(bh_submit_read);
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349void __init buffer_init(void)
3350{
3351 int nrpages;
3352
Christoph Lameterb98938c2008-02-04 22:28:36 -08003353 bh_cachep = kmem_cache_create("buffer_head",
3354 sizeof(struct buffer_head), 0,
3355 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3356 SLAB_MEM_SPREAD),
Richard Kennedy019b4d12010-03-10 15:20:33 -08003357 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 /*
3360 * Limit the bh occupancy to 10% of ZONE_NORMAL
3361 */
3362 nrpages = (nr_free_buffer_pages() * 10) / 100;
3363 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3364 hotcpu_notifier(buffer_cpu_notify, 0);
3365}