xref: /linux/fs/buffer.c (revision b4bba38909c21689de21355e84259cb7b38f25ac)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/buffer.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 2002  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Removed a lot of unnecessary code and simplified things now that
111da177e4SLinus Torvalds  * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Speed up hash, lru, and free list operations.  Use gfp() for allocating
141da177e4SLinus Torvalds  * hash table, use SLAB cache for buffer heads. SMP threading.  -DaveM
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * Added 32k buffer block sizes - these are required older ARM systems. - RMK
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/syscalls.h>
231da177e4SLinus Torvalds #include <linux/fs.h>
241da177e4SLinus Torvalds #include <linux/mm.h>
251da177e4SLinus Torvalds #include <linux/percpu.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
2716f7e0feSRandy Dunlap #include <linux/capability.h>
281da177e4SLinus Torvalds #include <linux/blkdev.h>
291da177e4SLinus Torvalds #include <linux/file.h>
301da177e4SLinus Torvalds #include <linux/quotaops.h>
311da177e4SLinus Torvalds #include <linux/highmem.h>
32630d9c47SPaul Gortmaker #include <linux/export.h>
33bafc0dbaSTejun Heo #include <linux/backing-dev.h>
341da177e4SLinus Torvalds #include <linux/writeback.h>
351da177e4SLinus Torvalds #include <linux/hash.h>
361da177e4SLinus Torvalds #include <linux/suspend.h>
371da177e4SLinus Torvalds #include <linux/buffer_head.h>
3855e829afSAndrew Morton #include <linux/task_io_accounting_ops.h>
391da177e4SLinus Torvalds #include <linux/bio.h>
401da177e4SLinus Torvalds #include <linux/notifier.h>
411da177e4SLinus Torvalds #include <linux/cpu.h>
421da177e4SLinus Torvalds #include <linux/bitops.h>
431da177e4SLinus Torvalds #include <linux/mpage.h>
44fb1c8f93SIngo Molnar #include <linux/bit_spinlock.h>
455305cb83STejun Heo #include <trace/events/block.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
48b16b1debSTejun Heo static int submit_bh_wbc(int rw, struct buffer_head *bh,
49bafc0dbaSTejun Heo 			 unsigned long bio_flags,
50b16b1debSTejun Heo 			 struct writeback_control *wbc);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
531da177e4SLinus Torvalds 
54a3f3c29cSYan Hong void init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	bh->b_end_io = handler;
571da177e4SLinus Torvalds 	bh->b_private = private;
581da177e4SLinus Torvalds }
591fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(init_buffer);
601da177e4SLinus Torvalds 
61f0059afdSTejun Heo inline void touch_buffer(struct buffer_head *bh)
62f0059afdSTejun Heo {
635305cb83STejun Heo 	trace_block_touch_buffer(bh);
64f0059afdSTejun Heo 	mark_page_accessed(bh->b_page);
65f0059afdSTejun Heo }
66f0059afdSTejun Heo EXPORT_SYMBOL(touch_buffer);
67f0059afdSTejun Heo 
68fc9b52cdSHarvey Harrison void __lock_buffer(struct buffer_head *bh)
691da177e4SLinus Torvalds {
7074316201SNeilBrown 	wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds EXPORT_SYMBOL(__lock_buffer);
731da177e4SLinus Torvalds 
74fc9b52cdSHarvey Harrison void unlock_buffer(struct buffer_head *bh)
751da177e4SLinus Torvalds {
7651b07fc3SNick Piggin 	clear_bit_unlock(BH_Lock, &bh->b_state);
774e857c58SPeter Zijlstra 	smp_mb__after_atomic();
781da177e4SLinus Torvalds 	wake_up_bit(&bh->b_state, BH_Lock);
791da177e4SLinus Torvalds }
801fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(unlock_buffer);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /*
83b4597226SMel Gorman  * Returns if the page has dirty or writeback buffers. If all the buffers
84b4597226SMel Gorman  * are unlocked and clean then the PageDirty information is stale. If
85b4597226SMel Gorman  * any of the pages are locked, it is assumed they are locked for IO.
86b4597226SMel Gorman  */
87b4597226SMel Gorman void buffer_check_dirty_writeback(struct page *page,
88b4597226SMel Gorman 				     bool *dirty, bool *writeback)
89b4597226SMel Gorman {
90b4597226SMel Gorman 	struct buffer_head *head, *bh;
91b4597226SMel Gorman 	*dirty = false;
92b4597226SMel Gorman 	*writeback = false;
93b4597226SMel Gorman 
94b4597226SMel Gorman 	BUG_ON(!PageLocked(page));
95b4597226SMel Gorman 
96b4597226SMel Gorman 	if (!page_has_buffers(page))
97b4597226SMel Gorman 		return;
98b4597226SMel Gorman 
99b4597226SMel Gorman 	if (PageWriteback(page))
100b4597226SMel Gorman 		*writeback = true;
101b4597226SMel Gorman 
102b4597226SMel Gorman 	head = page_buffers(page);
103b4597226SMel Gorman 	bh = head;
104b4597226SMel Gorman 	do {
105b4597226SMel Gorman 		if (buffer_locked(bh))
106b4597226SMel Gorman 			*writeback = true;
107b4597226SMel Gorman 
108b4597226SMel Gorman 		if (buffer_dirty(bh))
109b4597226SMel Gorman 			*dirty = true;
110b4597226SMel Gorman 
111b4597226SMel Gorman 		bh = bh->b_this_page;
112b4597226SMel Gorman 	} while (bh != head);
113b4597226SMel Gorman }
114b4597226SMel Gorman EXPORT_SYMBOL(buffer_check_dirty_writeback);
115b4597226SMel Gorman 
116b4597226SMel Gorman /*
1171da177e4SLinus Torvalds  * Block until a buffer comes unlocked.  This doesn't stop it
1181da177e4SLinus Torvalds  * from becoming locked again - you have to lock it yourself
1191da177e4SLinus Torvalds  * if you want to preserve its state.
1201da177e4SLinus Torvalds  */
1211da177e4SLinus Torvalds void __wait_on_buffer(struct buffer_head * bh)
1221da177e4SLinus Torvalds {
12374316201SNeilBrown 	wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
1241da177e4SLinus Torvalds }
1251fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__wait_on_buffer);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static void
1281da177e4SLinus Torvalds __clear_page_buffers(struct page *page)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	ClearPagePrivate(page);
1314c21e2f2SHugh Dickins 	set_page_private(page, 0);
13209cbfeafSKirill A. Shutemov 	put_page(page);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
135b744c2acSRobert Elliott static void buffer_io_error(struct buffer_head *bh, char *msg)
1361da177e4SLinus Torvalds {
137432f16e6SRobert Elliott 	if (!test_bit(BH_Quiet, &bh->b_state))
138432f16e6SRobert Elliott 		printk_ratelimited(KERN_ERR
139a1c6f057SDmitry Monakhov 			"Buffer I/O error on dev %pg, logical block %llu%s\n",
140a1c6f057SDmitry Monakhov 			bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds /*
14468671f35SDmitry Monakhov  * End-of-IO handler helper function which does not touch the bh after
14568671f35SDmitry Monakhov  * unlocking it.
14668671f35SDmitry Monakhov  * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
14768671f35SDmitry Monakhov  * a race there is benign: unlock_buffer() only use the bh's address for
14868671f35SDmitry Monakhov  * hashing after unlocking the buffer, so it doesn't actually touch the bh
14968671f35SDmitry Monakhov  * itself.
1501da177e4SLinus Torvalds  */
15168671f35SDmitry Monakhov static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	if (uptodate) {
1541da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
1551da177e4SLinus Torvalds 	} else {
1561da177e4SLinus Torvalds 		/* This happens, due to failed READA attempts. */
1571da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 	unlock_buffer(bh);
16068671f35SDmitry Monakhov }
16168671f35SDmitry Monakhov 
16268671f35SDmitry Monakhov /*
16368671f35SDmitry Monakhov  * Default synchronous end-of-IO handler..  Just mark it up-to-date and
16468671f35SDmitry Monakhov  * unlock the buffer. This is what ll_rw_block uses too.
16568671f35SDmitry Monakhov  */
16668671f35SDmitry Monakhov void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
16768671f35SDmitry Monakhov {
16868671f35SDmitry Monakhov 	__end_buffer_read_notouch(bh, uptodate);
1691da177e4SLinus Torvalds 	put_bh(bh);
1701da177e4SLinus Torvalds }
1711fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_read_sync);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	if (uptodate) {
1761da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
1771da177e4SLinus Torvalds 	} else {
178b744c2acSRobert Elliott 		buffer_io_error(bh, ", lost sync page write");
1791da177e4SLinus Torvalds 		set_buffer_write_io_error(bh);
1801da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 	unlock_buffer(bh);
1831da177e4SLinus Torvalds 	put_bh(bh);
1841da177e4SLinus Torvalds }
1851fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_write_sync);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * Various filesystems appear to want __find_get_block to be non-blocking.
1891da177e4SLinus Torvalds  * But it's the page lock which protects the buffers.  To get around this,
1901da177e4SLinus Torvalds  * we get exclusion from try_to_free_buffers with the blockdev mapping's
1911da177e4SLinus Torvalds  * private_lock.
1921da177e4SLinus Torvalds  *
1931da177e4SLinus Torvalds  * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
1941da177e4SLinus Torvalds  * may be quite high.  This code could TryLock the page, and if that
1951da177e4SLinus Torvalds  * succeeds, there is no need to take private_lock. (But if
1961da177e4SLinus Torvalds  * private_lock is contended then so is mapping->tree_lock).
1971da177e4SLinus Torvalds  */
1981da177e4SLinus Torvalds static struct buffer_head *
199385fd4c5SCoywolf Qi Hunt __find_get_block_slow(struct block_device *bdev, sector_t block)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct inode *bd_inode = bdev->bd_inode;
2021da177e4SLinus Torvalds 	struct address_space *bd_mapping = bd_inode->i_mapping;
2031da177e4SLinus Torvalds 	struct buffer_head *ret = NULL;
2041da177e4SLinus Torvalds 	pgoff_t index;
2051da177e4SLinus Torvalds 	struct buffer_head *bh;
2061da177e4SLinus Torvalds 	struct buffer_head *head;
2071da177e4SLinus Torvalds 	struct page *page;
2081da177e4SLinus Torvalds 	int all_mapped = 1;
2091da177e4SLinus Torvalds 
21009cbfeafSKirill A. Shutemov 	index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
2112457aec6SMel Gorman 	page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
2121da177e4SLinus Torvalds 	if (!page)
2131da177e4SLinus Torvalds 		goto out;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	spin_lock(&bd_mapping->private_lock);
2161da177e4SLinus Torvalds 	if (!page_has_buffers(page))
2171da177e4SLinus Torvalds 		goto out_unlock;
2181da177e4SLinus Torvalds 	head = page_buffers(page);
2191da177e4SLinus Torvalds 	bh = head;
2201da177e4SLinus Torvalds 	do {
22197f76d3dSNikanth Karthikesan 		if (!buffer_mapped(bh))
22297f76d3dSNikanth Karthikesan 			all_mapped = 0;
22397f76d3dSNikanth Karthikesan 		else if (bh->b_blocknr == block) {
2241da177e4SLinus Torvalds 			ret = bh;
2251da177e4SLinus Torvalds 			get_bh(bh);
2261da177e4SLinus Torvalds 			goto out_unlock;
2271da177e4SLinus Torvalds 		}
2281da177e4SLinus Torvalds 		bh = bh->b_this_page;
2291da177e4SLinus Torvalds 	} while (bh != head);
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/* we might be here because some of the buffers on this page are
2321da177e4SLinus Torvalds 	 * not mapped.  This is due to various races between
2331da177e4SLinus Torvalds 	 * file io on the block device and getblk.  It gets dealt with
2341da177e4SLinus Torvalds 	 * elsewhere, don't buffer_error if we had some unmapped buffers
2351da177e4SLinus Torvalds 	 */
2361da177e4SLinus Torvalds 	if (all_mapped) {
2371da177e4SLinus Torvalds 		printk("__find_get_block_slow() failed. "
2381da177e4SLinus Torvalds 			"block=%llu, b_blocknr=%llu\n",
239205f87f6SBadari Pulavarty 			(unsigned long long)block,
240205f87f6SBadari Pulavarty 			(unsigned long long)bh->b_blocknr);
241205f87f6SBadari Pulavarty 		printk("b_state=0x%08lx, b_size=%zu\n",
242205f87f6SBadari Pulavarty 			bh->b_state, bh->b_size);
243a1c6f057SDmitry Monakhov 		printk("device %pg blocksize: %d\n", bdev,
24472a2ebd8STao Ma 			1 << bd_inode->i_blkbits);
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds out_unlock:
2471da177e4SLinus Torvalds 	spin_unlock(&bd_mapping->private_lock);
24809cbfeafSKirill A. Shutemov 	put_page(page);
2491da177e4SLinus Torvalds out:
2501da177e4SLinus Torvalds 	return ret;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /*
2545b0830cbSJens Axboe  * Kick the writeback threads then try to free up some ZONE_NORMAL memory.
2551da177e4SLinus Torvalds  */
2561da177e4SLinus Torvalds static void free_more_memory(void)
2571da177e4SLinus Torvalds {
258c33d6c06SMel Gorman 	struct zoneref *z;
2590e88460dSMel Gorman 	int nid;
2601da177e4SLinus Torvalds 
2610e175a18SCurt Wohlgemuth 	wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);
2621da177e4SLinus Torvalds 	yield();
2631da177e4SLinus Torvalds 
2640e88460dSMel Gorman 	for_each_online_node(nid) {
265c33d6c06SMel Gorman 
266c33d6c06SMel Gorman 		z = first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
267c33d6c06SMel Gorman 						gfp_zone(GFP_NOFS), NULL);
268c33d6c06SMel Gorman 		if (z->zone)
26954a6eb5cSMel Gorman 			try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
270327c0e96SKAMEZAWA Hiroyuki 						GFP_NOFS, NULL);
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds /*
2751da177e4SLinus Torvalds  * I/O completion handler for block_read_full_page() - pages
2761da177e4SLinus Torvalds  * which come unlocked at the end of I/O.
2771da177e4SLinus Torvalds  */
2781da177e4SLinus Torvalds static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	unsigned long flags;
281a3972203SNick Piggin 	struct buffer_head *first;
2821da177e4SLinus Torvalds 	struct buffer_head *tmp;
2831da177e4SLinus Torvalds 	struct page *page;
2841da177e4SLinus Torvalds 	int page_uptodate = 1;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	BUG_ON(!buffer_async_read(bh));
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	page = bh->b_page;
2891da177e4SLinus Torvalds 	if (uptodate) {
2901da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
2911da177e4SLinus Torvalds 	} else {
2921da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
293b744c2acSRobert Elliott 		buffer_io_error(bh, ", async page read");
2941da177e4SLinus Torvalds 		SetPageError(page);
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/*
2981da177e4SLinus Torvalds 	 * Be _very_ careful from here on. Bad things can happen if
2991da177e4SLinus Torvalds 	 * two buffer heads end IO at almost the same time and both
3001da177e4SLinus Torvalds 	 * decide that the page is now completely done.
3011da177e4SLinus Torvalds 	 */
302a3972203SNick Piggin 	first = page_buffers(page);
303a3972203SNick Piggin 	local_irq_save(flags);
304a3972203SNick Piggin 	bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
3051da177e4SLinus Torvalds 	clear_buffer_async_read(bh);
3061da177e4SLinus Torvalds 	unlock_buffer(bh);
3071da177e4SLinus Torvalds 	tmp = bh;
3081da177e4SLinus Torvalds 	do {
3091da177e4SLinus Torvalds 		if (!buffer_uptodate(tmp))
3101da177e4SLinus Torvalds 			page_uptodate = 0;
3111da177e4SLinus Torvalds 		if (buffer_async_read(tmp)) {
3121da177e4SLinus Torvalds 			BUG_ON(!buffer_locked(tmp));
3131da177e4SLinus Torvalds 			goto still_busy;
3141da177e4SLinus Torvalds 		}
3151da177e4SLinus Torvalds 		tmp = tmp->b_this_page;
3161da177e4SLinus Torvalds 	} while (tmp != bh);
317a3972203SNick Piggin 	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
318a3972203SNick Piggin 	local_irq_restore(flags);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/*
3211da177e4SLinus Torvalds 	 * If none of the buffers had errors and they are all
3221da177e4SLinus Torvalds 	 * uptodate then we can set the page uptodate.
3231da177e4SLinus Torvalds 	 */
3241da177e4SLinus Torvalds 	if (page_uptodate && !PageError(page))
3251da177e4SLinus Torvalds 		SetPageUptodate(page);
3261da177e4SLinus Torvalds 	unlock_page(page);
3271da177e4SLinus Torvalds 	return;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds still_busy:
330a3972203SNick Piggin 	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
331a3972203SNick Piggin 	local_irq_restore(flags);
3321da177e4SLinus Torvalds 	return;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds /*
3361da177e4SLinus Torvalds  * Completion handler for block_write_full_page() - pages which are unlocked
3371da177e4SLinus Torvalds  * during I/O, and which have PageWriteback cleared upon I/O completion.
3381da177e4SLinus Torvalds  */
33935c80d5fSChris Mason void end_buffer_async_write(struct buffer_head *bh, int uptodate)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	unsigned long flags;
342a3972203SNick Piggin 	struct buffer_head *first;
3431da177e4SLinus Torvalds 	struct buffer_head *tmp;
3441da177e4SLinus Torvalds 	struct page *page;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	BUG_ON(!buffer_async_write(bh));
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	page = bh->b_page;
3491da177e4SLinus Torvalds 	if (uptodate) {
3501da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
3511da177e4SLinus Torvalds 	} else {
352b744c2acSRobert Elliott 		buffer_io_error(bh, ", lost async page write");
3531da177e4SLinus Torvalds 		set_bit(AS_EIO, &page->mapping->flags);
35458ff407bSJan Kara 		set_buffer_write_io_error(bh);
3551da177e4SLinus Torvalds 		clear_buffer_uptodate(bh);
3561da177e4SLinus Torvalds 		SetPageError(page);
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
359a3972203SNick Piggin 	first = page_buffers(page);
360a3972203SNick Piggin 	local_irq_save(flags);
361a3972203SNick Piggin 	bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
362a3972203SNick Piggin 
3631da177e4SLinus Torvalds 	clear_buffer_async_write(bh);
3641da177e4SLinus Torvalds 	unlock_buffer(bh);
3651da177e4SLinus Torvalds 	tmp = bh->b_this_page;
3661da177e4SLinus Torvalds 	while (tmp != bh) {
3671da177e4SLinus Torvalds 		if (buffer_async_write(tmp)) {
3681da177e4SLinus Torvalds 			BUG_ON(!buffer_locked(tmp));
3691da177e4SLinus Torvalds 			goto still_busy;
3701da177e4SLinus Torvalds 		}
3711da177e4SLinus Torvalds 		tmp = tmp->b_this_page;
3721da177e4SLinus Torvalds 	}
373a3972203SNick Piggin 	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
374a3972203SNick Piggin 	local_irq_restore(flags);
3751da177e4SLinus Torvalds 	end_page_writeback(page);
3761da177e4SLinus Torvalds 	return;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds still_busy:
379a3972203SNick Piggin 	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
380a3972203SNick Piggin 	local_irq_restore(flags);
3811da177e4SLinus Torvalds 	return;
3821da177e4SLinus Torvalds }
3831fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_async_write);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /*
3861da177e4SLinus Torvalds  * If a page's buffers are under async readin (end_buffer_async_read
3871da177e4SLinus Torvalds  * completion) then there is a possibility that another thread of
3881da177e4SLinus Torvalds  * control could lock one of the buffers after it has completed
3891da177e4SLinus Torvalds  * but while some of the other buffers have not completed.  This
3901da177e4SLinus Torvalds  * locked buffer would confuse end_buffer_async_read() into not unlocking
3911da177e4SLinus Torvalds  * the page.  So the absence of BH_Async_Read tells end_buffer_async_read()
3921da177e4SLinus Torvalds  * that this buffer is not under async I/O.
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * The page comes unlocked when it has no locked buffer_async buffers
3951da177e4SLinus Torvalds  * left.
3961da177e4SLinus Torvalds  *
3971da177e4SLinus Torvalds  * PageLocked prevents anyone starting new async I/O reads any of
3981da177e4SLinus Torvalds  * the buffers.
3991da177e4SLinus Torvalds  *
4001da177e4SLinus Torvalds  * PageWriteback is used to prevent simultaneous writeout of the same
4011da177e4SLinus Torvalds  * page.
4021da177e4SLinus Torvalds  *
4031da177e4SLinus Torvalds  * PageLocked prevents anyone from starting writeback of a page which is
4041da177e4SLinus Torvalds  * under read I/O (PageWriteback is only ever set against a locked page).
4051da177e4SLinus Torvalds  */
4061da177e4SLinus Torvalds static void mark_buffer_async_read(struct buffer_head *bh)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	bh->b_end_io = end_buffer_async_read;
4091da177e4SLinus Torvalds 	set_buffer_async_read(bh);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121fe72eaaSH Hartley Sweeten static void mark_buffer_async_write_endio(struct buffer_head *bh,
41335c80d5fSChris Mason 					  bh_end_io_t *handler)
41435c80d5fSChris Mason {
41535c80d5fSChris Mason 	bh->b_end_io = handler;
41635c80d5fSChris Mason 	set_buffer_async_write(bh);
41735c80d5fSChris Mason }
41835c80d5fSChris Mason 
4191da177e4SLinus Torvalds void mark_buffer_async_write(struct buffer_head *bh)
4201da177e4SLinus Torvalds {
42135c80d5fSChris Mason 	mark_buffer_async_write_endio(bh, end_buffer_async_write);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds EXPORT_SYMBOL(mark_buffer_async_write);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * fs/buffer.c contains helper functions for buffer-backed address space's
4281da177e4SLinus Torvalds  * fsync functions.  A common requirement for buffer-based filesystems is
4291da177e4SLinus Torvalds  * that certain data from the backing blockdev needs to be written out for
4301da177e4SLinus Torvalds  * a successful fsync().  For example, ext2 indirect blocks need to be
4311da177e4SLinus Torvalds  * written back and waited upon before fsync() returns.
4321da177e4SLinus Torvalds  *
4331da177e4SLinus Torvalds  * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
4341da177e4SLinus Torvalds  * inode_has_buffers() and invalidate_inode_buffers() are provided for the
4351da177e4SLinus Torvalds  * management of a list of dependent buffers at ->i_mapping->private_list.
4361da177e4SLinus Torvalds  *
4371da177e4SLinus Torvalds  * Locking is a little subtle: try_to_free_buffers() will remove buffers
4381da177e4SLinus Torvalds  * from their controlling inode's queue when they are being freed.  But
4391da177e4SLinus Torvalds  * try_to_free_buffers() will be operating against the *blockdev* mapping
4401da177e4SLinus Torvalds  * at the time, not against the S_ISREG file which depends on those buffers.
4411da177e4SLinus Torvalds  * So the locking for private_list is via the private_lock in the address_space
4421da177e4SLinus Torvalds  * which backs the buffers.  Which is different from the address_space
4431da177e4SLinus Torvalds  * against which the buffers are listed.  So for a particular address_space,
4441da177e4SLinus Torvalds  * mapping->private_lock does *not* protect mapping->private_list!  In fact,
4451da177e4SLinus Torvalds  * mapping->private_list will always be protected by the backing blockdev's
4461da177e4SLinus Torvalds  * ->private_lock.
4471da177e4SLinus Torvalds  *
4481da177e4SLinus Torvalds  * Which introduces a requirement: all buffers on an address_space's
4491da177e4SLinus Torvalds  * ->private_list must be from the same address_space: the blockdev's.
4501da177e4SLinus Torvalds  *
4511da177e4SLinus Torvalds  * address_spaces which do not place buffers at ->private_list via these
4521da177e4SLinus Torvalds  * utility functions are free to use private_lock and private_list for
4531da177e4SLinus Torvalds  * whatever they want.  The only requirement is that list_empty(private_list)
4541da177e4SLinus Torvalds  * be true at clear_inode() time.
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  * FIXME: clear_inode should not call invalidate_inode_buffers().  The
4571da177e4SLinus Torvalds  * filesystems should do that.  invalidate_inode_buffers() should just go
4581da177e4SLinus Torvalds  * BUG_ON(!list_empty).
4591da177e4SLinus Torvalds  *
4601da177e4SLinus Torvalds  * FIXME: mark_buffer_dirty_inode() is a data-plane operation.  It should
4611da177e4SLinus Torvalds  * take an address_space, not an inode.  And it should be called
4621da177e4SLinus Torvalds  * mark_buffer_dirty_fsync() to clearly define why those buffers are being
4631da177e4SLinus Torvalds  * queued up.
4641da177e4SLinus Torvalds  *
4651da177e4SLinus Torvalds  * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
4661da177e4SLinus Torvalds  * list if it is already on a list.  Because if the buffer is on a list,
4671da177e4SLinus Torvalds  * it *must* already be on the right one.  If not, the filesystem is being
4681da177e4SLinus Torvalds  * silly.  This will save a ton of locking.  But first we have to ensure
4691da177e4SLinus Torvalds  * that buffers are taken *off* the old inode's list when they are freed
4701da177e4SLinus Torvalds  * (presumably in truncate).  That requires careful auditing of all
4711da177e4SLinus Torvalds  * filesystems (do it inside bforget()).  It could also be done by bringing
4721da177e4SLinus Torvalds  * b_inode back.
4731da177e4SLinus Torvalds  */
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4761da177e4SLinus Torvalds  * The buffer's backing address_space's private_lock must be held
4771da177e4SLinus Torvalds  */
478dbacefc9SThomas Petazzoni static void __remove_assoc_queue(struct buffer_head *bh)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	list_del_init(&bh->b_assoc_buffers);
48158ff407bSJan Kara 	WARN_ON(!bh->b_assoc_map);
48258ff407bSJan Kara 	if (buffer_write_io_error(bh))
48358ff407bSJan Kara 		set_bit(AS_EIO, &bh->b_assoc_map->flags);
48458ff407bSJan Kara 	bh->b_assoc_map = NULL;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds int inode_has_buffers(struct inode *inode)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	return !list_empty(&inode->i_data.private_list);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /*
4931da177e4SLinus Torvalds  * osync is designed to support O_SYNC io.  It waits synchronously for
4941da177e4SLinus Torvalds  * all already-submitted IO to complete, but does not queue any new
4951da177e4SLinus Torvalds  * writes to the disk.
4961da177e4SLinus Torvalds  *
4971da177e4SLinus Torvalds  * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
4981da177e4SLinus Torvalds  * you dirty the buffers, and then use osync_inode_buffers to wait for
4991da177e4SLinus Torvalds  * completion.  Any other dirty buffers which are not yet queued for
5001da177e4SLinus Torvalds  * write will not be flushed to disk by the osync.
5011da177e4SLinus Torvalds  */
5021da177e4SLinus Torvalds static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct buffer_head *bh;
5051da177e4SLinus Torvalds 	struct list_head *p;
5061da177e4SLinus Torvalds 	int err = 0;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	spin_lock(lock);
5091da177e4SLinus Torvalds repeat:
5101da177e4SLinus Torvalds 	list_for_each_prev(p, list) {
5111da177e4SLinus Torvalds 		bh = BH_ENTRY(p);
5121da177e4SLinus Torvalds 		if (buffer_locked(bh)) {
5131da177e4SLinus Torvalds 			get_bh(bh);
5141da177e4SLinus Torvalds 			spin_unlock(lock);
5151da177e4SLinus Torvalds 			wait_on_buffer(bh);
5161da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
5171da177e4SLinus Torvalds 				err = -EIO;
5181da177e4SLinus Torvalds 			brelse(bh);
5191da177e4SLinus Torvalds 			spin_lock(lock);
5201da177e4SLinus Torvalds 			goto repeat;
5211da177e4SLinus Torvalds 		}
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 	spin_unlock(lock);
5241da177e4SLinus Torvalds 	return err;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
52701a05b33SAl Viro static void do_thaw_one(struct super_block *sb, void *unused)
528c2d75438SEric Sandeen {
529c2d75438SEric Sandeen 	while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
530a1c6f057SDmitry Monakhov 		printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
531c2d75438SEric Sandeen }
53201a05b33SAl Viro 
53301a05b33SAl Viro static void do_thaw_all(struct work_struct *work)
53401a05b33SAl Viro {
53501a05b33SAl Viro 	iterate_supers(do_thaw_one, NULL);
536053c525fSJens Axboe 	kfree(work);
537c2d75438SEric Sandeen 	printk(KERN_WARNING "Emergency Thaw complete\n");
538c2d75438SEric Sandeen }
539c2d75438SEric Sandeen 
540c2d75438SEric Sandeen /**
541c2d75438SEric Sandeen  * emergency_thaw_all -- forcibly thaw every frozen filesystem
542c2d75438SEric Sandeen  *
543c2d75438SEric Sandeen  * Used for emergency unfreeze of all filesystems via SysRq
544c2d75438SEric Sandeen  */
545c2d75438SEric Sandeen void emergency_thaw_all(void)
546c2d75438SEric Sandeen {
547053c525fSJens Axboe 	struct work_struct *work;
548053c525fSJens Axboe 
549053c525fSJens Axboe 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
550053c525fSJens Axboe 	if (work) {
551053c525fSJens Axboe 		INIT_WORK(work, do_thaw_all);
552053c525fSJens Axboe 		schedule_work(work);
553053c525fSJens Axboe 	}
554c2d75438SEric Sandeen }
555c2d75438SEric Sandeen 
5561da177e4SLinus Torvalds /**
55778a4a50aSRandy Dunlap  * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
55867be2dd1SMartin Waitz  * @mapping: the mapping which wants those buffers written
5591da177e4SLinus Torvalds  *
5601da177e4SLinus Torvalds  * Starts I/O against the buffers at mapping->private_list, and waits upon
5611da177e4SLinus Torvalds  * that I/O.
5621da177e4SLinus Torvalds  *
56367be2dd1SMartin Waitz  * Basically, this is a convenience function for fsync().
56467be2dd1SMartin Waitz  * @mapping is a file or directory which needs those buffers to be written for
56567be2dd1SMartin Waitz  * a successful fsync().
5661da177e4SLinus Torvalds  */
5671da177e4SLinus Torvalds int sync_mapping_buffers(struct address_space *mapping)
5681da177e4SLinus Torvalds {
569252aa6f5SRafael Aquini 	struct address_space *buffer_mapping = mapping->private_data;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	if (buffer_mapping == NULL || list_empty(&mapping->private_list))
5721da177e4SLinus Torvalds 		return 0;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	return fsync_buffers_list(&buffer_mapping->private_lock,
5751da177e4SLinus Torvalds 					&mapping->private_list);
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds EXPORT_SYMBOL(sync_mapping_buffers);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /*
5801da177e4SLinus Torvalds  * Called when we've recently written block `bblock', and it is known that
5811da177e4SLinus Torvalds  * `bblock' was for a buffer_boundary() buffer.  This means that the block at
5821da177e4SLinus Torvalds  * `bblock + 1' is probably a dirty indirect block.  Hunt it down and, if it's
5831da177e4SLinus Torvalds  * dirty, schedule it for IO.  So that indirects merge nicely with their data.
5841da177e4SLinus Torvalds  */
5851da177e4SLinus Torvalds void write_boundary_block(struct block_device *bdev,
5861da177e4SLinus Torvalds 			sector_t bblock, unsigned blocksize)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
5891da177e4SLinus Torvalds 	if (bh) {
5901da177e4SLinus Torvalds 		if (buffer_dirty(bh))
5911da177e4SLinus Torvalds 			ll_rw_block(WRITE, 1, &bh);
5921da177e4SLinus Torvalds 		put_bh(bh);
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
5991da177e4SLinus Torvalds 	struct address_space *buffer_mapping = bh->b_page->mapping;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
602252aa6f5SRafael Aquini 	if (!mapping->private_data) {
603252aa6f5SRafael Aquini 		mapping->private_data = buffer_mapping;
6041da177e4SLinus Torvalds 	} else {
605252aa6f5SRafael Aquini 		BUG_ON(mapping->private_data != buffer_mapping);
6061da177e4SLinus Torvalds 	}
607535ee2fbSJan Kara 	if (!bh->b_assoc_map) {
6081da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
6091da177e4SLinus Torvalds 		list_move_tail(&bh->b_assoc_buffers,
6101da177e4SLinus Torvalds 				&mapping->private_list);
61158ff407bSJan Kara 		bh->b_assoc_map = mapping;
6121da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds EXPORT_SYMBOL(mark_buffer_dirty_inode);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /*
618787d2214SNick Piggin  * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
619787d2214SNick Piggin  * dirty.
620787d2214SNick Piggin  *
621787d2214SNick Piggin  * If warn is true, then emit a warning if the page is not uptodate and has
622787d2214SNick Piggin  * not been truncated.
623c4843a75SGreg Thelen  *
62481f8c3a4SJohannes Weiner  * The caller must hold lock_page_memcg().
625787d2214SNick Piggin  */
626c4843a75SGreg Thelen static void __set_page_dirty(struct page *page, struct address_space *mapping,
62762cccb8cSJohannes Weiner 			     int warn)
628787d2214SNick Piggin {
629227d53b3SKOSAKI Motohiro 	unsigned long flags;
630227d53b3SKOSAKI Motohiro 
631227d53b3SKOSAKI Motohiro 	spin_lock_irqsave(&mapping->tree_lock, flags);
632787d2214SNick Piggin 	if (page->mapping) {	/* Race with truncate? */
633787d2214SNick Piggin 		WARN_ON_ONCE(warn && !PageUptodate(page));
63462cccb8cSJohannes Weiner 		account_page_dirtied(page, mapping);
635787d2214SNick Piggin 		radix_tree_tag_set(&mapping->page_tree,
636787d2214SNick Piggin 				page_index(page), PAGECACHE_TAG_DIRTY);
637787d2214SNick Piggin 	}
638227d53b3SKOSAKI Motohiro 	spin_unlock_irqrestore(&mapping->tree_lock, flags);
639787d2214SNick Piggin }
640787d2214SNick Piggin 
641787d2214SNick Piggin /*
6421da177e4SLinus Torvalds  * Add a page to the dirty page list.
6431da177e4SLinus Torvalds  *
6441da177e4SLinus Torvalds  * It is a sad fact of life that this function is called from several places
6451da177e4SLinus Torvalds  * deeply under spinlocking.  It may not sleep.
6461da177e4SLinus Torvalds  *
6471da177e4SLinus Torvalds  * If the page has buffers, the uptodate buffers are set dirty, to preserve
6481da177e4SLinus Torvalds  * dirty-state coherency between the page and the buffers.  It the page does
6491da177e4SLinus Torvalds  * not have buffers then when they are later attached they will all be set
6501da177e4SLinus Torvalds  * dirty.
6511da177e4SLinus Torvalds  *
6521da177e4SLinus Torvalds  * The buffers are dirtied before the page is dirtied.  There's a small race
6531da177e4SLinus Torvalds  * window in which a writepage caller may see the page cleanness but not the
6541da177e4SLinus Torvalds  * buffer dirtiness.  That's fine.  If this code were to set the page dirty
6551da177e4SLinus Torvalds  * before the buffers, a concurrent writepage caller could clear the page dirty
6561da177e4SLinus Torvalds  * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
6571da177e4SLinus Torvalds  * page on the dirty page list.
6581da177e4SLinus Torvalds  *
6591da177e4SLinus Torvalds  * We use private_lock to lock against try_to_free_buffers while using the
6601da177e4SLinus Torvalds  * page's buffer list.  Also use this to protect against clean buffers being
6611da177e4SLinus Torvalds  * added to the page after it was set dirty.
6621da177e4SLinus Torvalds  *
6631da177e4SLinus Torvalds  * FIXME: may need to call ->reservepage here as well.  That's rather up to the
6641da177e4SLinus Torvalds  * address_space though.
6651da177e4SLinus Torvalds  */
6661da177e4SLinus Torvalds int __set_page_dirty_buffers(struct page *page)
6671da177e4SLinus Torvalds {
668a8e7d49aSLinus Torvalds 	int newly_dirty;
669787d2214SNick Piggin 	struct address_space *mapping = page_mapping(page);
670ebf7a227SNick Piggin 
671ebf7a227SNick Piggin 	if (unlikely(!mapping))
672ebf7a227SNick Piggin 		return !TestSetPageDirty(page);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	spin_lock(&mapping->private_lock);
6751da177e4SLinus Torvalds 	if (page_has_buffers(page)) {
6761da177e4SLinus Torvalds 		struct buffer_head *head = page_buffers(page);
6771da177e4SLinus Torvalds 		struct buffer_head *bh = head;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 		do {
6801da177e4SLinus Torvalds 			set_buffer_dirty(bh);
6811da177e4SLinus Torvalds 			bh = bh->b_this_page;
6821da177e4SLinus Torvalds 		} while (bh != head);
6831da177e4SLinus Torvalds 	}
684c4843a75SGreg Thelen 	/*
68581f8c3a4SJohannes Weiner 	 * Lock out page->mem_cgroup migration to keep PageDirty
68681f8c3a4SJohannes Weiner 	 * synchronized with per-memcg dirty page counters.
687c4843a75SGreg Thelen 	 */
68862cccb8cSJohannes Weiner 	lock_page_memcg(page);
689a8e7d49aSLinus Torvalds 	newly_dirty = !TestSetPageDirty(page);
6901da177e4SLinus Torvalds 	spin_unlock(&mapping->private_lock);
6911da177e4SLinus Torvalds 
692a8e7d49aSLinus Torvalds 	if (newly_dirty)
69362cccb8cSJohannes Weiner 		__set_page_dirty(page, mapping, 1);
694c4843a75SGreg Thelen 
69562cccb8cSJohannes Weiner 	unlock_page_memcg(page);
696c4843a75SGreg Thelen 
697c4843a75SGreg Thelen 	if (newly_dirty)
698c4843a75SGreg Thelen 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
699c4843a75SGreg Thelen 
700a8e7d49aSLinus Torvalds 	return newly_dirty;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds EXPORT_SYMBOL(__set_page_dirty_buffers);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * Write out and wait upon a list of buffers.
7061da177e4SLinus Torvalds  *
7071da177e4SLinus Torvalds  * We have conflicting pressures: we want to make sure that all
7081da177e4SLinus Torvalds  * initially dirty buffers get waited on, but that any subsequently
7091da177e4SLinus Torvalds  * dirtied buffers don't.  After all, we don't want fsync to last
7101da177e4SLinus Torvalds  * forever if somebody is actively writing to the file.
7111da177e4SLinus Torvalds  *
7121da177e4SLinus Torvalds  * Do this in two main stages: first we copy dirty buffers to a
7131da177e4SLinus Torvalds  * temporary inode list, queueing the writes as we go.  Then we clean
7141da177e4SLinus Torvalds  * up, waiting for those writes to complete.
7151da177e4SLinus Torvalds  *
7161da177e4SLinus Torvalds  * During this second stage, any subsequent updates to the file may end
7171da177e4SLinus Torvalds  * up refiling the buffer on the original inode's dirty list again, so
7181da177e4SLinus Torvalds  * there is a chance we will end up with a buffer queued for write but
7191da177e4SLinus Torvalds  * not yet completed on that list.  So, as a final cleanup we go through
7201da177e4SLinus Torvalds  * the osync code to catch these locked, dirty buffers without requeuing
7211da177e4SLinus Torvalds  * any newly dirty buffers for write.
7221da177e4SLinus Torvalds  */
7231da177e4SLinus Torvalds static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	struct buffer_head *bh;
7261da177e4SLinus Torvalds 	struct list_head tmp;
7277eaceaccSJens Axboe 	struct address_space *mapping;
7281da177e4SLinus Torvalds 	int err = 0, err2;
7294ee2491eSJens Axboe 	struct blk_plug plug;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp);
7324ee2491eSJens Axboe 	blk_start_plug(&plug);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	spin_lock(lock);
7351da177e4SLinus Torvalds 	while (!list_empty(list)) {
7361da177e4SLinus Torvalds 		bh = BH_ENTRY(list->next);
737535ee2fbSJan Kara 		mapping = bh->b_assoc_map;
73858ff407bSJan Kara 		__remove_assoc_queue(bh);
739535ee2fbSJan Kara 		/* Avoid race with mark_buffer_dirty_inode() which does
740535ee2fbSJan Kara 		 * a lockless check and we rely on seeing the dirty bit */
741535ee2fbSJan Kara 		smp_mb();
7421da177e4SLinus Torvalds 		if (buffer_dirty(bh) || buffer_locked(bh)) {
7431da177e4SLinus Torvalds 			list_add(&bh->b_assoc_buffers, &tmp);
744535ee2fbSJan Kara 			bh->b_assoc_map = mapping;
7451da177e4SLinus Torvalds 			if (buffer_dirty(bh)) {
7461da177e4SLinus Torvalds 				get_bh(bh);
7471da177e4SLinus Torvalds 				spin_unlock(lock);
7481da177e4SLinus Torvalds 				/*
7491da177e4SLinus Torvalds 				 * Ensure any pending I/O completes so that
7509cb569d6SChristoph Hellwig 				 * write_dirty_buffer() actually writes the
7519cb569d6SChristoph Hellwig 				 * current contents - it is a noop if I/O is
7529cb569d6SChristoph Hellwig 				 * still in flight on potentially older
7539cb569d6SChristoph Hellwig 				 * contents.
7541da177e4SLinus Torvalds 				 */
755721a9602SJens Axboe 				write_dirty_buffer(bh, WRITE_SYNC);
7569cf6b720SJens Axboe 
7579cf6b720SJens Axboe 				/*
7589cf6b720SJens Axboe 				 * Kick off IO for the previous mapping. Note
7599cf6b720SJens Axboe 				 * that we will not run the very last mapping,
7609cf6b720SJens Axboe 				 * wait_on_buffer() will do that for us
7619cf6b720SJens Axboe 				 * through sync_buffer().
7629cf6b720SJens Axboe 				 */
7631da177e4SLinus Torvalds 				brelse(bh);
7641da177e4SLinus Torvalds 				spin_lock(lock);
7651da177e4SLinus Torvalds 			}
7661da177e4SLinus Torvalds 		}
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7694ee2491eSJens Axboe 	spin_unlock(lock);
7704ee2491eSJens Axboe 	blk_finish_plug(&plug);
7714ee2491eSJens Axboe 	spin_lock(lock);
7724ee2491eSJens Axboe 
7731da177e4SLinus Torvalds 	while (!list_empty(&tmp)) {
7741da177e4SLinus Torvalds 		bh = BH_ENTRY(tmp.prev);
7751da177e4SLinus Torvalds 		get_bh(bh);
776535ee2fbSJan Kara 		mapping = bh->b_assoc_map;
777535ee2fbSJan Kara 		__remove_assoc_queue(bh);
778535ee2fbSJan Kara 		/* Avoid race with mark_buffer_dirty_inode() which does
779535ee2fbSJan Kara 		 * a lockless check and we rely on seeing the dirty bit */
780535ee2fbSJan Kara 		smp_mb();
781535ee2fbSJan Kara 		if (buffer_dirty(bh)) {
782535ee2fbSJan Kara 			list_add(&bh->b_assoc_buffers,
783e3892296SJan Kara 				 &mapping->private_list);
784535ee2fbSJan Kara 			bh->b_assoc_map = mapping;
785535ee2fbSJan Kara 		}
7861da177e4SLinus Torvalds 		spin_unlock(lock);
7871da177e4SLinus Torvalds 		wait_on_buffer(bh);
7881da177e4SLinus Torvalds 		if (!buffer_uptodate(bh))
7891da177e4SLinus Torvalds 			err = -EIO;
7901da177e4SLinus Torvalds 		brelse(bh);
7911da177e4SLinus Torvalds 		spin_lock(lock);
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	spin_unlock(lock);
7951da177e4SLinus Torvalds 	err2 = osync_buffers_list(lock, list);
7961da177e4SLinus Torvalds 	if (err)
7971da177e4SLinus Torvalds 		return err;
7981da177e4SLinus Torvalds 	else
7991da177e4SLinus Torvalds 		return err2;
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds /*
8031da177e4SLinus Torvalds  * Invalidate any and all dirty buffers on a given inode.  We are
8041da177e4SLinus Torvalds  * probably unmounting the fs, but that doesn't mean we have already
8051da177e4SLinus Torvalds  * done a sync().  Just drop the buffers from the inode list.
8061da177e4SLinus Torvalds  *
8071da177e4SLinus Torvalds  * NOTE: we take the inode's blockdev's mapping's private_lock.  Which
8081da177e4SLinus Torvalds  * assumes that all the buffers are against the blockdev.  Not true
8091da177e4SLinus Torvalds  * for reiserfs.
8101da177e4SLinus Torvalds  */
8111da177e4SLinus Torvalds void invalidate_inode_buffers(struct inode *inode)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	if (inode_has_buffers(inode)) {
8141da177e4SLinus Torvalds 		struct address_space *mapping = &inode->i_data;
8151da177e4SLinus Torvalds 		struct list_head *list = &mapping->private_list;
816252aa6f5SRafael Aquini 		struct address_space *buffer_mapping = mapping->private_data;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
8191da177e4SLinus Torvalds 		while (!list_empty(list))
8201da177e4SLinus Torvalds 			__remove_assoc_queue(BH_ENTRY(list->next));
8211da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds }
82452b19ac9SJan Kara EXPORT_SYMBOL(invalidate_inode_buffers);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /*
8271da177e4SLinus Torvalds  * Remove any clean buffers from the inode's buffer list.  This is called
8281da177e4SLinus Torvalds  * when we're trying to free the inode itself.  Those buffers can pin it.
8291da177e4SLinus Torvalds  *
8301da177e4SLinus Torvalds  * Returns true if all buffers were removed.
8311da177e4SLinus Torvalds  */
8321da177e4SLinus Torvalds int remove_inode_buffers(struct inode *inode)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	int ret = 1;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if (inode_has_buffers(inode)) {
8371da177e4SLinus Torvalds 		struct address_space *mapping = &inode->i_data;
8381da177e4SLinus Torvalds 		struct list_head *list = &mapping->private_list;
839252aa6f5SRafael Aquini 		struct address_space *buffer_mapping = mapping->private_data;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
8421da177e4SLinus Torvalds 		while (!list_empty(list)) {
8431da177e4SLinus Torvalds 			struct buffer_head *bh = BH_ENTRY(list->next);
8441da177e4SLinus Torvalds 			if (buffer_dirty(bh)) {
8451da177e4SLinus Torvalds 				ret = 0;
8461da177e4SLinus Torvalds 				break;
8471da177e4SLinus Torvalds 			}
8481da177e4SLinus Torvalds 			__remove_assoc_queue(bh);
8491da177e4SLinus Torvalds 		}
8501da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	return ret;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds /*
8561da177e4SLinus Torvalds  * Create the appropriate buffers when given a page for data area and
8571da177e4SLinus Torvalds  * the size of each buffer.. Use the bh->b_this_page linked list to
8581da177e4SLinus Torvalds  * follow the buffers created.  Return NULL if unable to create more
8591da177e4SLinus Torvalds  * buffers.
8601da177e4SLinus Torvalds  *
8611da177e4SLinus Torvalds  * The retry flag is used to differentiate async IO (paging, swapping)
8621da177e4SLinus Torvalds  * which may not fail from ordinary buffer allocations.
8631da177e4SLinus Torvalds  */
8641da177e4SLinus Torvalds struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
8651da177e4SLinus Torvalds 		int retry)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	struct buffer_head *bh, *head;
8681da177e4SLinus Torvalds 	long offset;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds try_again:
8711da177e4SLinus Torvalds 	head = NULL;
8721da177e4SLinus Torvalds 	offset = PAGE_SIZE;
8731da177e4SLinus Torvalds 	while ((offset -= size) >= 0) {
8741da177e4SLinus Torvalds 		bh = alloc_buffer_head(GFP_NOFS);
8751da177e4SLinus Torvalds 		if (!bh)
8761da177e4SLinus Torvalds 			goto no_grow;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 		bh->b_this_page = head;
8791da177e4SLinus Torvalds 		bh->b_blocknr = -1;
8801da177e4SLinus Torvalds 		head = bh;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 		bh->b_size = size;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 		/* Link the buffer to its page */
8851da177e4SLinus Torvalds 		set_bh_page(bh, page, offset);
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 	return head;
8881da177e4SLinus Torvalds /*
8891da177e4SLinus Torvalds  * In case anything failed, we just free everything we got.
8901da177e4SLinus Torvalds  */
8911da177e4SLinus Torvalds no_grow:
8921da177e4SLinus Torvalds 	if (head) {
8931da177e4SLinus Torvalds 		do {
8941da177e4SLinus Torvalds 			bh = head;
8951da177e4SLinus Torvalds 			head = head->b_this_page;
8961da177e4SLinus Torvalds 			free_buffer_head(bh);
8971da177e4SLinus Torvalds 		} while (head);
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	/*
9011da177e4SLinus Torvalds 	 * Return failure for non-async IO requests.  Async IO requests
9021da177e4SLinus Torvalds 	 * are not allowed to fail, so we have to wait until buffer heads
9031da177e4SLinus Torvalds 	 * become available.  But we don't want tasks sleeping with
9041da177e4SLinus Torvalds 	 * partially complete buffers, so all were released above.
9051da177e4SLinus Torvalds 	 */
9061da177e4SLinus Torvalds 	if (!retry)
9071da177e4SLinus Torvalds 		return NULL;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	/* We're _really_ low on memory. Now we just
9101da177e4SLinus Torvalds 	 * wait for old buffer heads to become free due to
9111da177e4SLinus Torvalds 	 * finishing IO.  Since this is an async request and
9121da177e4SLinus Torvalds 	 * the reserve list is empty, we're sure there are
9131da177e4SLinus Torvalds 	 * async buffer heads in use.
9141da177e4SLinus Torvalds 	 */
9151da177e4SLinus Torvalds 	free_more_memory();
9161da177e4SLinus Torvalds 	goto try_again;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(alloc_page_buffers);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds static inline void
9211da177e4SLinus Torvalds link_dev_buffers(struct page *page, struct buffer_head *head)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	struct buffer_head *bh, *tail;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	bh = head;
9261da177e4SLinus Torvalds 	do {
9271da177e4SLinus Torvalds 		tail = bh;
9281da177e4SLinus Torvalds 		bh = bh->b_this_page;
9291da177e4SLinus Torvalds 	} while (bh);
9301da177e4SLinus Torvalds 	tail->b_this_page = head;
9311da177e4SLinus Torvalds 	attach_page_buffers(page, head);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
934bbec0270SLinus Torvalds static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
935bbec0270SLinus Torvalds {
936bbec0270SLinus Torvalds 	sector_t retval = ~((sector_t)0);
937bbec0270SLinus Torvalds 	loff_t sz = i_size_read(bdev->bd_inode);
938bbec0270SLinus Torvalds 
939bbec0270SLinus Torvalds 	if (sz) {
940bbec0270SLinus Torvalds 		unsigned int sizebits = blksize_bits(size);
941bbec0270SLinus Torvalds 		retval = (sz >> sizebits);
942bbec0270SLinus Torvalds 	}
943bbec0270SLinus Torvalds 	return retval;
944bbec0270SLinus Torvalds }
945bbec0270SLinus Torvalds 
9461da177e4SLinus Torvalds /*
9471da177e4SLinus Torvalds  * Initialise the state of a blockdev page's buffers.
9481da177e4SLinus Torvalds  */
949676ce6d5SHugh Dickins static sector_t
9501da177e4SLinus Torvalds init_page_buffers(struct page *page, struct block_device *bdev,
9511da177e4SLinus Torvalds 			sector_t block, int size)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct buffer_head *head = page_buffers(page);
9541da177e4SLinus Torvalds 	struct buffer_head *bh = head;
9551da177e4SLinus Torvalds 	int uptodate = PageUptodate(page);
956bbec0270SLinus Torvalds 	sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	do {
9591da177e4SLinus Torvalds 		if (!buffer_mapped(bh)) {
9601da177e4SLinus Torvalds 			init_buffer(bh, NULL, NULL);
9611da177e4SLinus Torvalds 			bh->b_bdev = bdev;
9621da177e4SLinus Torvalds 			bh->b_blocknr = block;
9631da177e4SLinus Torvalds 			if (uptodate)
9641da177e4SLinus Torvalds 				set_buffer_uptodate(bh);
965080399aaSJeff Moyer 			if (block < end_block)
9661da177e4SLinus Torvalds 				set_buffer_mapped(bh);
9671da177e4SLinus Torvalds 		}
9681da177e4SLinus Torvalds 		block++;
9691da177e4SLinus Torvalds 		bh = bh->b_this_page;
9701da177e4SLinus Torvalds 	} while (bh != head);
971676ce6d5SHugh Dickins 
972676ce6d5SHugh Dickins 	/*
973676ce6d5SHugh Dickins 	 * Caller needs to validate requested block against end of device.
974676ce6d5SHugh Dickins 	 */
975676ce6d5SHugh Dickins 	return end_block;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds /*
9791da177e4SLinus Torvalds  * Create the page-cache page that contains the requested block.
9801da177e4SLinus Torvalds  *
981676ce6d5SHugh Dickins  * This is used purely for blockdev mappings.
9821da177e4SLinus Torvalds  */
983676ce6d5SHugh Dickins static int
9841da177e4SLinus Torvalds grow_dev_page(struct block_device *bdev, sector_t block,
9853b5e6454SGioh Kim 	      pgoff_t index, int size, int sizebits, gfp_t gfp)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	struct inode *inode = bdev->bd_inode;
9881da177e4SLinus Torvalds 	struct page *page;
9891da177e4SLinus Torvalds 	struct buffer_head *bh;
990676ce6d5SHugh Dickins 	sector_t end_block;
991676ce6d5SHugh Dickins 	int ret = 0;		/* Will call free_more_memory() */
99284235de3SJohannes Weiner 	gfp_t gfp_mask;
9931da177e4SLinus Torvalds 
994c62d2555SMichal Hocko 	gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
9953b5e6454SGioh Kim 
99684235de3SJohannes Weiner 	/*
99784235de3SJohannes Weiner 	 * XXX: __getblk_slow() can not really deal with failure and
99884235de3SJohannes Weiner 	 * will endlessly loop on improvised global reclaim.  Prefer
99984235de3SJohannes Weiner 	 * looping in the allocator rather than here, at least that
100084235de3SJohannes Weiner 	 * code knows what it's doing.
100184235de3SJohannes Weiner 	 */
100284235de3SJohannes Weiner 	gfp_mask |= __GFP_NOFAIL;
100384235de3SJohannes Weiner 
100484235de3SJohannes Weiner 	page = find_or_create_page(inode->i_mapping, index, gfp_mask);
10051da177e4SLinus Torvalds 	if (!page)
1006676ce6d5SHugh Dickins 		return ret;
10071da177e4SLinus Torvalds 
1008e827f923SEric Sesterhenn 	BUG_ON(!PageLocked(page));
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	if (page_has_buffers(page)) {
10111da177e4SLinus Torvalds 		bh = page_buffers(page);
10121da177e4SLinus Torvalds 		if (bh->b_size == size) {
1013676ce6d5SHugh Dickins 			end_block = init_page_buffers(page, bdev,
1014f2d5a944SAnton Altaparmakov 						(sector_t)index << sizebits,
1015f2d5a944SAnton Altaparmakov 						size);
1016676ce6d5SHugh Dickins 			goto done;
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 		if (!try_to_free_buffers(page))
10191da177e4SLinus Torvalds 			goto failed;
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	/*
10231da177e4SLinus Torvalds 	 * Allocate some buffers for this page
10241da177e4SLinus Torvalds 	 */
10251da177e4SLinus Torvalds 	bh = alloc_page_buffers(page, size, 0);
10261da177e4SLinus Torvalds 	if (!bh)
10271da177e4SLinus Torvalds 		goto failed;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	/*
10301da177e4SLinus Torvalds 	 * Link the page to the buffers and initialise them.  Take the
10311da177e4SLinus Torvalds 	 * lock to be atomic wrt __find_get_block(), which does not
10321da177e4SLinus Torvalds 	 * run under the page lock.
10331da177e4SLinus Torvalds 	 */
10341da177e4SLinus Torvalds 	spin_lock(&inode->i_mapping->private_lock);
10351da177e4SLinus Torvalds 	link_dev_buffers(page, bh);
1036f2d5a944SAnton Altaparmakov 	end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
1037f2d5a944SAnton Altaparmakov 			size);
10381da177e4SLinus Torvalds 	spin_unlock(&inode->i_mapping->private_lock);
1039676ce6d5SHugh Dickins done:
1040676ce6d5SHugh Dickins 	ret = (block < end_block) ? 1 : -ENXIO;
10411da177e4SLinus Torvalds failed:
10421da177e4SLinus Torvalds 	unlock_page(page);
104309cbfeafSKirill A. Shutemov 	put_page(page);
1044676ce6d5SHugh Dickins 	return ret;
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds /*
10481da177e4SLinus Torvalds  * Create buffers for the specified block device block's page.  If
10491da177e4SLinus Torvalds  * that page was dirty, the buffers are set dirty also.
10501da177e4SLinus Torvalds  */
1051858119e1SArjan van de Ven static int
10523b5e6454SGioh Kim grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	pgoff_t index;
10551da177e4SLinus Torvalds 	int sizebits;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	sizebits = -1;
10581da177e4SLinus Torvalds 	do {
10591da177e4SLinus Torvalds 		sizebits++;
10601da177e4SLinus Torvalds 	} while ((size << sizebits) < PAGE_SIZE);
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	index = block >> sizebits;
10631da177e4SLinus Torvalds 
1064e5657933SAndrew Morton 	/*
1065e5657933SAndrew Morton 	 * Check for a block which wants to lie outside our maximum possible
1066e5657933SAndrew Morton 	 * pagecache index.  (this comparison is done using sector_t types).
1067e5657933SAndrew Morton 	 */
1068e5657933SAndrew Morton 	if (unlikely(index != block >> sizebits)) {
1069e5657933SAndrew Morton 		printk(KERN_ERR "%s: requested out-of-range block %llu for "
1070a1c6f057SDmitry Monakhov 			"device %pg\n",
10718e24eea7SHarvey Harrison 			__func__, (unsigned long long)block,
1072a1c6f057SDmitry Monakhov 			bdev);
1073e5657933SAndrew Morton 		return -EIO;
1074e5657933SAndrew Morton 	}
1075676ce6d5SHugh Dickins 
10761da177e4SLinus Torvalds 	/* Create a page with the proper size buffers.. */
10773b5e6454SGioh Kim 	return grow_dev_page(bdev, block, index, size, sizebits, gfp);
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10803b5e6454SGioh Kim struct buffer_head *
10813b5e6454SGioh Kim __getblk_slow(struct block_device *bdev, sector_t block,
10823b5e6454SGioh Kim 	     unsigned size, gfp_t gfp)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	/* Size must be multiple of hard sectorsize */
1085e1defc4fSMartin K. Petersen 	if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
10861da177e4SLinus Torvalds 			(size < 512 || size > PAGE_SIZE))) {
10871da177e4SLinus Torvalds 		printk(KERN_ERR "getblk(): invalid block size %d requested\n",
10881da177e4SLinus Torvalds 					size);
1089e1defc4fSMartin K. Petersen 		printk(KERN_ERR "logical block size: %d\n",
1090e1defc4fSMartin K. Petersen 					bdev_logical_block_size(bdev));
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		dump_stack();
10931da177e4SLinus Torvalds 		return NULL;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 
1096676ce6d5SHugh Dickins 	for (;;) {
1097676ce6d5SHugh Dickins 		struct buffer_head *bh;
1098676ce6d5SHugh Dickins 		int ret;
1099676ce6d5SHugh Dickins 
11001da177e4SLinus Torvalds 		bh = __find_get_block(bdev, block, size);
11011da177e4SLinus Torvalds 		if (bh)
11021da177e4SLinus Torvalds 			return bh;
11031da177e4SLinus Torvalds 
11043b5e6454SGioh Kim 		ret = grow_buffers(bdev, block, size, gfp);
1105676ce6d5SHugh Dickins 		if (ret < 0)
110691f68c89SJeff Moyer 			return NULL;
1107676ce6d5SHugh Dickins 		if (ret == 0)
1108676ce6d5SHugh Dickins 			free_more_memory();
1109676ce6d5SHugh Dickins 	}
11101da177e4SLinus Torvalds }
11113b5e6454SGioh Kim EXPORT_SYMBOL(__getblk_slow);
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds /*
11141da177e4SLinus Torvalds  * The relationship between dirty buffers and dirty pages:
11151da177e4SLinus Torvalds  *
11161da177e4SLinus Torvalds  * Whenever a page has any dirty buffers, the page's dirty bit is set, and
11171da177e4SLinus Torvalds  * the page is tagged dirty in its radix tree.
11181da177e4SLinus Torvalds  *
11191da177e4SLinus Torvalds  * At all times, the dirtiness of the buffers represents the dirtiness of
11201da177e4SLinus Torvalds  * subsections of the page.  If the page has buffers, the page dirty bit is
11211da177e4SLinus Torvalds  * merely a hint about the true dirty state.
11221da177e4SLinus Torvalds  *
11231da177e4SLinus Torvalds  * When a page is set dirty in its entirety, all its buffers are marked dirty
11241da177e4SLinus Torvalds  * (if the page has buffers).
11251da177e4SLinus Torvalds  *
11261da177e4SLinus Torvalds  * When a buffer is marked dirty, its page is dirtied, but the page's other
11271da177e4SLinus Torvalds  * buffers are not.
11281da177e4SLinus Torvalds  *
11291da177e4SLinus Torvalds  * Also.  When blockdev buffers are explicitly read with bread(), they
11301da177e4SLinus Torvalds  * individually become uptodate.  But their backing page remains not
11311da177e4SLinus Torvalds  * uptodate - even if all of its buffers are uptodate.  A subsequent
11321da177e4SLinus Torvalds  * block_read_full_page() against that page will discover all the uptodate
11331da177e4SLinus Torvalds  * buffers, will set the page uptodate and will perform no I/O.
11341da177e4SLinus Torvalds  */
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds /**
11371da177e4SLinus Torvalds  * mark_buffer_dirty - mark a buffer_head as needing writeout
113867be2dd1SMartin Waitz  * @bh: the buffer_head to mark dirty
11391da177e4SLinus Torvalds  *
11401da177e4SLinus Torvalds  * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
11411da177e4SLinus Torvalds  * backing page dirty, then tag the page as dirty in its address_space's radix
11421da177e4SLinus Torvalds  * tree and then attach the address_space's inode to its superblock's dirty
11431da177e4SLinus Torvalds  * inode list.
11441da177e4SLinus Torvalds  *
11451da177e4SLinus Torvalds  * mark_buffer_dirty() is atomic.  It takes bh->b_page->mapping->private_lock,
1146250df6edSDave Chinner  * mapping->tree_lock and mapping->host->i_lock.
11471da177e4SLinus Torvalds  */
1148fc9b52cdSHarvey Harrison void mark_buffer_dirty(struct buffer_head *bh)
11491da177e4SLinus Torvalds {
1150787d2214SNick Piggin 	WARN_ON_ONCE(!buffer_uptodate(bh));
11511be62dc1SLinus Torvalds 
11525305cb83STejun Heo 	trace_block_dirty_buffer(bh);
11535305cb83STejun Heo 
11541be62dc1SLinus Torvalds 	/*
11551be62dc1SLinus Torvalds 	 * Very *carefully* optimize the it-is-already-dirty case.
11561be62dc1SLinus Torvalds 	 *
11571be62dc1SLinus Torvalds 	 * Don't let the final "is it dirty" escape to before we
11581be62dc1SLinus Torvalds 	 * perhaps modified the buffer.
11591be62dc1SLinus Torvalds 	 */
11601be62dc1SLinus Torvalds 	if (buffer_dirty(bh)) {
11611be62dc1SLinus Torvalds 		smp_mb();
11621be62dc1SLinus Torvalds 		if (buffer_dirty(bh))
11631be62dc1SLinus Torvalds 			return;
11641be62dc1SLinus Torvalds 	}
11651be62dc1SLinus Torvalds 
1166a8e7d49aSLinus Torvalds 	if (!test_set_buffer_dirty(bh)) {
1167a8e7d49aSLinus Torvalds 		struct page *page = bh->b_page;
1168c4843a75SGreg Thelen 		struct address_space *mapping = NULL;
1169c4843a75SGreg Thelen 
117062cccb8cSJohannes Weiner 		lock_page_memcg(page);
11718e9d78edSLinus Torvalds 		if (!TestSetPageDirty(page)) {
1172c4843a75SGreg Thelen 			mapping = page_mapping(page);
11738e9d78edSLinus Torvalds 			if (mapping)
117462cccb8cSJohannes Weiner 				__set_page_dirty(page, mapping, 0);
11758e9d78edSLinus Torvalds 		}
117662cccb8cSJohannes Weiner 		unlock_page_memcg(page);
1177c4843a75SGreg Thelen 		if (mapping)
1178c4843a75SGreg Thelen 			__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1179a8e7d49aSLinus Torvalds 	}
11801da177e4SLinus Torvalds }
11811fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(mark_buffer_dirty);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds /*
11841da177e4SLinus Torvalds  * Decrement a buffer_head's reference count.  If all buffers against a page
11851da177e4SLinus Torvalds  * have zero reference count, are clean and unlocked, and if the page is clean
11861da177e4SLinus Torvalds  * and unlocked then try_to_free_buffers() may strip the buffers from the page
11871da177e4SLinus Torvalds  * in preparation for freeing it (sometimes, rarely, buffers are removed from
11881da177e4SLinus Torvalds  * a page but it ends up not being freed, and buffers may later be reattached).
11891da177e4SLinus Torvalds  */
11901da177e4SLinus Torvalds void __brelse(struct buffer_head * buf)
11911da177e4SLinus Torvalds {
11921da177e4SLinus Torvalds 	if (atomic_read(&buf->b_count)) {
11931da177e4SLinus Torvalds 		put_bh(buf);
11941da177e4SLinus Torvalds 		return;
11951da177e4SLinus Torvalds 	}
11965c752ad9SArjan van de Ven 	WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
11971da177e4SLinus Torvalds }
11981fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__brelse);
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds /*
12011da177e4SLinus Torvalds  * bforget() is like brelse(), except it discards any
12021da177e4SLinus Torvalds  * potentially dirty data.
12031da177e4SLinus Torvalds  */
12041da177e4SLinus Torvalds void __bforget(struct buffer_head *bh)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	clear_buffer_dirty(bh);
1207535ee2fbSJan Kara 	if (bh->b_assoc_map) {
12081da177e4SLinus Torvalds 		struct address_space *buffer_mapping = bh->b_page->mapping;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 		spin_lock(&buffer_mapping->private_lock);
12111da177e4SLinus Torvalds 		list_del_init(&bh->b_assoc_buffers);
121258ff407bSJan Kara 		bh->b_assoc_map = NULL;
12131da177e4SLinus Torvalds 		spin_unlock(&buffer_mapping->private_lock);
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 	__brelse(bh);
12161da177e4SLinus Torvalds }
12171fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__bforget);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds static struct buffer_head *__bread_slow(struct buffer_head *bh)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	lock_buffer(bh);
12221da177e4SLinus Torvalds 	if (buffer_uptodate(bh)) {
12231da177e4SLinus Torvalds 		unlock_buffer(bh);
12241da177e4SLinus Torvalds 		return bh;
12251da177e4SLinus Torvalds 	} else {
12261da177e4SLinus Torvalds 		get_bh(bh);
12271da177e4SLinus Torvalds 		bh->b_end_io = end_buffer_read_sync;
12281da177e4SLinus Torvalds 		submit_bh(READ, bh);
12291da177e4SLinus Torvalds 		wait_on_buffer(bh);
12301da177e4SLinus Torvalds 		if (buffer_uptodate(bh))
12311da177e4SLinus Torvalds 			return bh;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	brelse(bh);
12341da177e4SLinus Torvalds 	return NULL;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds /*
12381da177e4SLinus Torvalds  * Per-cpu buffer LRU implementation.  To reduce the cost of __find_get_block().
12391da177e4SLinus Torvalds  * The bhs[] array is sorted - newest buffer is at bhs[0].  Buffers have their
12401da177e4SLinus Torvalds  * refcount elevated by one when they're in an LRU.  A buffer can only appear
12411da177e4SLinus Torvalds  * once in a particular CPU's LRU.  A single buffer can be present in multiple
12421da177e4SLinus Torvalds  * CPU's LRUs at the same time.
12431da177e4SLinus Torvalds  *
12441da177e4SLinus Torvalds  * This is a transparent caching front-end to sb_bread(), sb_getblk() and
12451da177e4SLinus Torvalds  * sb_find_get_block().
12461da177e4SLinus Torvalds  *
12471da177e4SLinus Torvalds  * The LRUs themselves only need locking against invalidate_bh_lrus.  We use
12481da177e4SLinus Torvalds  * a local interrupt disable for that.
12491da177e4SLinus Torvalds  */
12501da177e4SLinus Torvalds 
125186cf78d7SSebastien Buisson #define BH_LRU_SIZE	16
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds struct bh_lru {
12541da177e4SLinus Torvalds 	struct buffer_head *bhs[BH_LRU_SIZE];
12551da177e4SLinus Torvalds };
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds #ifdef CONFIG_SMP
12601da177e4SLinus Torvalds #define bh_lru_lock()	local_irq_disable()
12611da177e4SLinus Torvalds #define bh_lru_unlock()	local_irq_enable()
12621da177e4SLinus Torvalds #else
12631da177e4SLinus Torvalds #define bh_lru_lock()	preempt_disable()
12641da177e4SLinus Torvalds #define bh_lru_unlock()	preempt_enable()
12651da177e4SLinus Torvalds #endif
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds static inline void check_irqs_on(void)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds #ifdef irqs_disabled
12701da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
12711da177e4SLinus Torvalds #endif
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds /*
12751da177e4SLinus Torvalds  * The LRU management algorithm is dopey-but-simple.  Sorry.
12761da177e4SLinus Torvalds  */
12771da177e4SLinus Torvalds static void bh_lru_install(struct buffer_head *bh)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	struct buffer_head *evictee = NULL;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	check_irqs_on();
12821da177e4SLinus Torvalds 	bh_lru_lock();
1283c7b92516SChristoph Lameter 	if (__this_cpu_read(bh_lrus.bhs[0]) != bh) {
12841da177e4SLinus Torvalds 		struct buffer_head *bhs[BH_LRU_SIZE];
12851da177e4SLinus Torvalds 		int in;
12861da177e4SLinus Torvalds 		int out = 0;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 		get_bh(bh);
12891da177e4SLinus Torvalds 		bhs[out++] = bh;
12901da177e4SLinus Torvalds 		for (in = 0; in < BH_LRU_SIZE; in++) {
1291c7b92516SChristoph Lameter 			struct buffer_head *bh2 =
1292c7b92516SChristoph Lameter 				__this_cpu_read(bh_lrus.bhs[in]);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 			if (bh2 == bh) {
12951da177e4SLinus Torvalds 				__brelse(bh2);
12961da177e4SLinus Torvalds 			} else {
12971da177e4SLinus Torvalds 				if (out >= BH_LRU_SIZE) {
12981da177e4SLinus Torvalds 					BUG_ON(evictee != NULL);
12991da177e4SLinus Torvalds 					evictee = bh2;
13001da177e4SLinus Torvalds 				} else {
13011da177e4SLinus Torvalds 					bhs[out++] = bh2;
13021da177e4SLinus Torvalds 				}
13031da177e4SLinus Torvalds 			}
13041da177e4SLinus Torvalds 		}
13051da177e4SLinus Torvalds 		while (out < BH_LRU_SIZE)
13061da177e4SLinus Torvalds 			bhs[out++] = NULL;
1307ca6673b0SChristoph Lameter 		memcpy(this_cpu_ptr(&bh_lrus.bhs), bhs, sizeof(bhs));
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds 	bh_lru_unlock();
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	if (evictee)
13121da177e4SLinus Torvalds 		__brelse(evictee);
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds /*
13161da177e4SLinus Torvalds  * Look up the bh in this cpu's LRU.  If it's there, move it to the head.
13171da177e4SLinus Torvalds  */
1318858119e1SArjan van de Ven static struct buffer_head *
13193991d3bdSTomasz Kvarsin lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
13201da177e4SLinus Torvalds {
13211da177e4SLinus Torvalds 	struct buffer_head *ret = NULL;
13223991d3bdSTomasz Kvarsin 	unsigned int i;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	check_irqs_on();
13251da177e4SLinus Torvalds 	bh_lru_lock();
13261da177e4SLinus Torvalds 	for (i = 0; i < BH_LRU_SIZE; i++) {
1327c7b92516SChristoph Lameter 		struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
13281da177e4SLinus Torvalds 
13299470dd5dSZach Brown 		if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
13309470dd5dSZach Brown 		    bh->b_size == size) {
13311da177e4SLinus Torvalds 			if (i) {
13321da177e4SLinus Torvalds 				while (i) {
1333c7b92516SChristoph Lameter 					__this_cpu_write(bh_lrus.bhs[i],
1334c7b92516SChristoph Lameter 						__this_cpu_read(bh_lrus.bhs[i - 1]));
13351da177e4SLinus Torvalds 					i--;
13361da177e4SLinus Torvalds 				}
1337c7b92516SChristoph Lameter 				__this_cpu_write(bh_lrus.bhs[0], bh);
13381da177e4SLinus Torvalds 			}
13391da177e4SLinus Torvalds 			get_bh(bh);
13401da177e4SLinus Torvalds 			ret = bh;
13411da177e4SLinus Torvalds 			break;
13421da177e4SLinus Torvalds 		}
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 	bh_lru_unlock();
13451da177e4SLinus Torvalds 	return ret;
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
13491da177e4SLinus Torvalds  * Perform a pagecache lookup for the matching buffer.  If it's there, refresh
13501da177e4SLinus Torvalds  * it in the LRU and mark it as accessed.  If it is not present then return
13511da177e4SLinus Torvalds  * NULL
13521da177e4SLinus Torvalds  */
13531da177e4SLinus Torvalds struct buffer_head *
13543991d3bdSTomasz Kvarsin __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	if (bh == NULL) {
13592457aec6SMel Gorman 		/* __find_get_block_slow will mark the page accessed */
1360385fd4c5SCoywolf Qi Hunt 		bh = __find_get_block_slow(bdev, block);
13611da177e4SLinus Torvalds 		if (bh)
13621da177e4SLinus Torvalds 			bh_lru_install(bh);
13632457aec6SMel Gorman 	} else
13641da177e4SLinus Torvalds 		touch_buffer(bh);
13652457aec6SMel Gorman 
13661da177e4SLinus Torvalds 	return bh;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds EXPORT_SYMBOL(__find_get_block);
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds /*
13713b5e6454SGioh Kim  * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
13721da177e4SLinus Torvalds  * which corresponds to the passed block_device, block and size. The
13731da177e4SLinus Torvalds  * returned buffer has its reference count incremented.
13741da177e4SLinus Torvalds  *
13753b5e6454SGioh Kim  * __getblk_gfp() will lock up the machine if grow_dev_page's
13763b5e6454SGioh Kim  * try_to_free_buffers() attempt is failing.  FIXME, perhaps?
13771da177e4SLinus Torvalds  */
13781da177e4SLinus Torvalds struct buffer_head *
13793b5e6454SGioh Kim __getblk_gfp(struct block_device *bdev, sector_t block,
13803b5e6454SGioh Kim 	     unsigned size, gfp_t gfp)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	struct buffer_head *bh = __find_get_block(bdev, block, size);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	might_sleep();
13851da177e4SLinus Torvalds 	if (bh == NULL)
13863b5e6454SGioh Kim 		bh = __getblk_slow(bdev, block, size, gfp);
13871da177e4SLinus Torvalds 	return bh;
13881da177e4SLinus Torvalds }
13893b5e6454SGioh Kim EXPORT_SYMBOL(__getblk_gfp);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds /*
13921da177e4SLinus Torvalds  * Do async read-ahead on a buffer..
13931da177e4SLinus Torvalds  */
13943991d3bdSTomasz Kvarsin void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	struct buffer_head *bh = __getblk(bdev, block, size);
1397a3e713b5SAndrew Morton 	if (likely(bh)) {
13981da177e4SLinus Torvalds 		ll_rw_block(READA, 1, &bh);
13991da177e4SLinus Torvalds 		brelse(bh);
14001da177e4SLinus Torvalds 	}
1401a3e713b5SAndrew Morton }
14021da177e4SLinus Torvalds EXPORT_SYMBOL(__breadahead);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds /**
14053b5e6454SGioh Kim  *  __bread_gfp() - reads a specified block and returns the bh
140667be2dd1SMartin Waitz  *  @bdev: the block_device to read from
14071da177e4SLinus Torvalds  *  @block: number of block
14081da177e4SLinus Torvalds  *  @size: size (in bytes) to read
14093b5e6454SGioh Kim  *  @gfp: page allocation flag
14101da177e4SLinus Torvalds  *
14111da177e4SLinus Torvalds  *  Reads a specified block, and returns buffer head that contains it.
14123b5e6454SGioh Kim  *  The page cache can be allocated from non-movable area
14133b5e6454SGioh Kim  *  not to prevent page migration if you set gfp to zero.
14141da177e4SLinus Torvalds  *  It returns NULL if the block was unreadable.
14151da177e4SLinus Torvalds  */
14161da177e4SLinus Torvalds struct buffer_head *
14173b5e6454SGioh Kim __bread_gfp(struct block_device *bdev, sector_t block,
14183b5e6454SGioh Kim 		   unsigned size, gfp_t gfp)
14191da177e4SLinus Torvalds {
14203b5e6454SGioh Kim 	struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
14211da177e4SLinus Torvalds 
1422a3e713b5SAndrew Morton 	if (likely(bh) && !buffer_uptodate(bh))
14231da177e4SLinus Torvalds 		bh = __bread_slow(bh);
14241da177e4SLinus Torvalds 	return bh;
14251da177e4SLinus Torvalds }
14263b5e6454SGioh Kim EXPORT_SYMBOL(__bread_gfp);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds /*
14291da177e4SLinus Torvalds  * invalidate_bh_lrus() is called rarely - but not only at unmount.
14301da177e4SLinus Torvalds  * This doesn't race because it runs in each cpu either in irq
14311da177e4SLinus Torvalds  * or with preempt disabled.
14321da177e4SLinus Torvalds  */
14331da177e4SLinus Torvalds static void invalidate_bh_lru(void *arg)
14341da177e4SLinus Torvalds {
14351da177e4SLinus Torvalds 	struct bh_lru *b = &get_cpu_var(bh_lrus);
14361da177e4SLinus Torvalds 	int i;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	for (i = 0; i < BH_LRU_SIZE; i++) {
14391da177e4SLinus Torvalds 		brelse(b->bhs[i]);
14401da177e4SLinus Torvalds 		b->bhs[i] = NULL;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 	put_cpu_var(bh_lrus);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
144542be35d0SGilad Ben-Yossef static bool has_bh_in_lru(int cpu, void *dummy)
144642be35d0SGilad Ben-Yossef {
144742be35d0SGilad Ben-Yossef 	struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
144842be35d0SGilad Ben-Yossef 	int i;
144942be35d0SGilad Ben-Yossef 
145042be35d0SGilad Ben-Yossef 	for (i = 0; i < BH_LRU_SIZE; i++) {
145142be35d0SGilad Ben-Yossef 		if (b->bhs[i])
145242be35d0SGilad Ben-Yossef 			return 1;
145342be35d0SGilad Ben-Yossef 	}
145442be35d0SGilad Ben-Yossef 
145542be35d0SGilad Ben-Yossef 	return 0;
145642be35d0SGilad Ben-Yossef }
145742be35d0SGilad Ben-Yossef 
1458f9a14399SPeter Zijlstra void invalidate_bh_lrus(void)
14591da177e4SLinus Torvalds {
146042be35d0SGilad Ben-Yossef 	on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
14611da177e4SLinus Torvalds }
14629db5579bSNick Piggin EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds void set_bh_page(struct buffer_head *bh,
14651da177e4SLinus Torvalds 		struct page *page, unsigned long offset)
14661da177e4SLinus Torvalds {
14671da177e4SLinus Torvalds 	bh->b_page = page;
1468e827f923SEric Sesterhenn 	BUG_ON(offset >= PAGE_SIZE);
14691da177e4SLinus Torvalds 	if (PageHighMem(page))
14701da177e4SLinus Torvalds 		/*
14711da177e4SLinus Torvalds 		 * This catches illegal uses and preserves the offset:
14721da177e4SLinus Torvalds 		 */
14731da177e4SLinus Torvalds 		bh->b_data = (char *)(0 + offset);
14741da177e4SLinus Torvalds 	else
14751da177e4SLinus Torvalds 		bh->b_data = page_address(page) + offset;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds EXPORT_SYMBOL(set_bh_page);
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds /*
14801da177e4SLinus Torvalds  * Called when truncating a buffer on a page completely.
14811da177e4SLinus Torvalds  */
1482e7470ee8SMel Gorman 
1483e7470ee8SMel Gorman /* Bits that are cleared during an invalidate */
1484e7470ee8SMel Gorman #define BUFFER_FLAGS_DISCARD \
1485e7470ee8SMel Gorman 	(1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
1486e7470ee8SMel Gorman 	 1 << BH_Delay | 1 << BH_Unwritten)
1487e7470ee8SMel Gorman 
1488858119e1SArjan van de Ven static void discard_buffer(struct buffer_head * bh)
14891da177e4SLinus Torvalds {
1490e7470ee8SMel Gorman 	unsigned long b_state, b_state_old;
1491e7470ee8SMel Gorman 
14921da177e4SLinus Torvalds 	lock_buffer(bh);
14931da177e4SLinus Torvalds 	clear_buffer_dirty(bh);
14941da177e4SLinus Torvalds 	bh->b_bdev = NULL;
1495e7470ee8SMel Gorman 	b_state = bh->b_state;
1496e7470ee8SMel Gorman 	for (;;) {
1497e7470ee8SMel Gorman 		b_state_old = cmpxchg(&bh->b_state, b_state,
1498e7470ee8SMel Gorman 				      (b_state & ~BUFFER_FLAGS_DISCARD));
1499e7470ee8SMel Gorman 		if (b_state_old == b_state)
1500e7470ee8SMel Gorman 			break;
1501e7470ee8SMel Gorman 		b_state = b_state_old;
1502e7470ee8SMel Gorman 	}
15031da177e4SLinus Torvalds 	unlock_buffer(bh);
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds /**
1507814e1d25SWang Sheng-Hui  * block_invalidatepage - invalidate part or all of a buffer-backed page
15081da177e4SLinus Torvalds  *
15091da177e4SLinus Torvalds  * @page: the page which is affected
1510d47992f8SLukas Czerner  * @offset: start of the range to invalidate
1511d47992f8SLukas Czerner  * @length: length of the range to invalidate
15121da177e4SLinus Torvalds  *
15131da177e4SLinus Torvalds  * block_invalidatepage() is called when all or part of the page has become
15141da177e4SLinus Torvalds  * invalidated by a truncate operation.
15151da177e4SLinus Torvalds  *
15161da177e4SLinus Torvalds  * block_invalidatepage() does not have to release all buffers, but it must
15171da177e4SLinus Torvalds  * ensure that no dirty buffer is left outside @offset and that no I/O
15181da177e4SLinus Torvalds  * is underway against any of the blocks which are outside the truncation
15191da177e4SLinus Torvalds  * point.  Because the caller is about to free (and possibly reuse) those
15201da177e4SLinus Torvalds  * blocks on-disk.
15211da177e4SLinus Torvalds  */
1522d47992f8SLukas Czerner void block_invalidatepage(struct page *page, unsigned int offset,
1523d47992f8SLukas Czerner 			  unsigned int length)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	struct buffer_head *head, *bh, *next;
15261da177e4SLinus Torvalds 	unsigned int curr_off = 0;
1527d47992f8SLukas Czerner 	unsigned int stop = length + offset;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
15301da177e4SLinus Torvalds 	if (!page_has_buffers(page))
15311da177e4SLinus Torvalds 		goto out;
15321da177e4SLinus Torvalds 
1533d47992f8SLukas Czerner 	/*
1534d47992f8SLukas Czerner 	 * Check for overflow
1535d47992f8SLukas Czerner 	 */
153609cbfeafSKirill A. Shutemov 	BUG_ON(stop > PAGE_SIZE || stop < length);
1537d47992f8SLukas Czerner 
15381da177e4SLinus Torvalds 	head = page_buffers(page);
15391da177e4SLinus Torvalds 	bh = head;
15401da177e4SLinus Torvalds 	do {
15411da177e4SLinus Torvalds 		unsigned int next_off = curr_off + bh->b_size;
15421da177e4SLinus Torvalds 		next = bh->b_this_page;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 		/*
1545d47992f8SLukas Czerner 		 * Are we still fully in range ?
1546d47992f8SLukas Czerner 		 */
1547d47992f8SLukas Czerner 		if (next_off > stop)
1548d47992f8SLukas Czerner 			goto out;
1549d47992f8SLukas Czerner 
1550d47992f8SLukas Czerner 		/*
15511da177e4SLinus Torvalds 		 * is this block fully invalidated?
15521da177e4SLinus Torvalds 		 */
15531da177e4SLinus Torvalds 		if (offset <= curr_off)
15541da177e4SLinus Torvalds 			discard_buffer(bh);
15551da177e4SLinus Torvalds 		curr_off = next_off;
15561da177e4SLinus Torvalds 		bh = next;
15571da177e4SLinus Torvalds 	} while (bh != head);
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	/*
15601da177e4SLinus Torvalds 	 * We release buffers only if the entire page is being invalidated.
15611da177e4SLinus Torvalds 	 * The get_block cached value has been unconditionally invalidated,
15621da177e4SLinus Torvalds 	 * so real IO is not possible anymore.
15631da177e4SLinus Torvalds 	 */
15641da177e4SLinus Torvalds 	if (offset == 0)
15652ff28e22SNeilBrown 		try_to_release_page(page, 0);
15661da177e4SLinus Torvalds out:
15672ff28e22SNeilBrown 	return;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds EXPORT_SYMBOL(block_invalidatepage);
15701da177e4SLinus Torvalds 
1571d47992f8SLukas Czerner 
15721da177e4SLinus Torvalds /*
15731da177e4SLinus Torvalds  * We attach and possibly dirty the buffers atomically wrt
15741da177e4SLinus Torvalds  * __set_page_dirty_buffers() via private_lock.  try_to_free_buffers
15751da177e4SLinus Torvalds  * is already excluded via the page lock.
15761da177e4SLinus Torvalds  */
15771da177e4SLinus Torvalds void create_empty_buffers(struct page *page,
15781da177e4SLinus Torvalds 			unsigned long blocksize, unsigned long b_state)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	struct buffer_head *bh, *head, *tail;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	head = alloc_page_buffers(page, blocksize, 1);
15831da177e4SLinus Torvalds 	bh = head;
15841da177e4SLinus Torvalds 	do {
15851da177e4SLinus Torvalds 		bh->b_state |= b_state;
15861da177e4SLinus Torvalds 		tail = bh;
15871da177e4SLinus Torvalds 		bh = bh->b_this_page;
15881da177e4SLinus Torvalds 	} while (bh);
15891da177e4SLinus Torvalds 	tail->b_this_page = head;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	spin_lock(&page->mapping->private_lock);
15921da177e4SLinus Torvalds 	if (PageUptodate(page) || PageDirty(page)) {
15931da177e4SLinus Torvalds 		bh = head;
15941da177e4SLinus Torvalds 		do {
15951da177e4SLinus Torvalds 			if (PageDirty(page))
15961da177e4SLinus Torvalds 				set_buffer_dirty(bh);
15971da177e4SLinus Torvalds 			if (PageUptodate(page))
15981da177e4SLinus Torvalds 				set_buffer_uptodate(bh);
15991da177e4SLinus Torvalds 			bh = bh->b_this_page;
16001da177e4SLinus Torvalds 		} while (bh != head);
16011da177e4SLinus Torvalds 	}
16021da177e4SLinus Torvalds 	attach_page_buffers(page, head);
16031da177e4SLinus Torvalds 	spin_unlock(&page->mapping->private_lock);
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds EXPORT_SYMBOL(create_empty_buffers);
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds /*
16081da177e4SLinus Torvalds  * We are taking a block for data and we don't want any output from any
16091da177e4SLinus Torvalds  * buffer-cache aliases starting from return from that function and
16101da177e4SLinus Torvalds  * until the moment when something will explicitly mark the buffer
16111da177e4SLinus Torvalds  * dirty (hopefully that will not happen until we will free that block ;-)
16121da177e4SLinus Torvalds  * We don't even need to mark it not-uptodate - nobody can expect
16131da177e4SLinus Torvalds  * anything from a newly allocated buffer anyway. We used to used
16141da177e4SLinus Torvalds  * unmap_buffer() for such invalidation, but that was wrong. We definitely
16151da177e4SLinus Torvalds  * don't want to mark the alias unmapped, for example - it would confuse
16161da177e4SLinus Torvalds  * anyone who might pick it with bread() afterwards...
16171da177e4SLinus Torvalds  *
16181da177e4SLinus Torvalds  * Also..  Note that bforget() doesn't lock the buffer.  So there can
16191da177e4SLinus Torvalds  * be writeout I/O going on against recently-freed buffers.  We don't
16201da177e4SLinus Torvalds  * wait on that I/O in bforget() - it's more efficient to wait on the I/O
16211da177e4SLinus Torvalds  * only if we really need to.  That happens here.
16221da177e4SLinus Torvalds  */
16231da177e4SLinus Torvalds void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	struct buffer_head *old_bh;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	might_sleep();
16281da177e4SLinus Torvalds 
1629385fd4c5SCoywolf Qi Hunt 	old_bh = __find_get_block_slow(bdev, block);
16301da177e4SLinus Torvalds 	if (old_bh) {
16311da177e4SLinus Torvalds 		clear_buffer_dirty(old_bh);
16321da177e4SLinus Torvalds 		wait_on_buffer(old_bh);
16331da177e4SLinus Torvalds 		clear_buffer_req(old_bh);
16341da177e4SLinus Torvalds 		__brelse(old_bh);
16351da177e4SLinus Torvalds 	}
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_underlying_metadata);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds /*
164045bce8f3SLinus Torvalds  * Size is a power-of-two in the range 512..PAGE_SIZE,
164145bce8f3SLinus Torvalds  * and the case we care about most is PAGE_SIZE.
164245bce8f3SLinus Torvalds  *
164345bce8f3SLinus Torvalds  * So this *could* possibly be written with those
164445bce8f3SLinus Torvalds  * constraints in mind (relevant mostly if some
164545bce8f3SLinus Torvalds  * architecture has a slow bit-scan instruction)
164645bce8f3SLinus Torvalds  */
164745bce8f3SLinus Torvalds static inline int block_size_bits(unsigned int blocksize)
164845bce8f3SLinus Torvalds {
164945bce8f3SLinus Torvalds 	return ilog2(blocksize);
165045bce8f3SLinus Torvalds }
165145bce8f3SLinus Torvalds 
165245bce8f3SLinus Torvalds static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
165345bce8f3SLinus Torvalds {
165445bce8f3SLinus Torvalds 	BUG_ON(!PageLocked(page));
165545bce8f3SLinus Torvalds 
165645bce8f3SLinus Torvalds 	if (!page_has_buffers(page))
165745bce8f3SLinus Torvalds 		create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
165845bce8f3SLinus Torvalds 	return page_buffers(page);
165945bce8f3SLinus Torvalds }
166045bce8f3SLinus Torvalds 
166145bce8f3SLinus Torvalds /*
16621da177e4SLinus Torvalds  * NOTE! All mapped/uptodate combinations are valid:
16631da177e4SLinus Torvalds  *
16641da177e4SLinus Torvalds  *	Mapped	Uptodate	Meaning
16651da177e4SLinus Torvalds  *
16661da177e4SLinus Torvalds  *	No	No		"unknown" - must do get_block()
16671da177e4SLinus Torvalds  *	No	Yes		"hole" - zero-filled
16681da177e4SLinus Torvalds  *	Yes	No		"allocated" - allocated on disk, not read in
16691da177e4SLinus Torvalds  *	Yes	Yes		"valid" - allocated and up-to-date in memory.
16701da177e4SLinus Torvalds  *
16711da177e4SLinus Torvalds  * "Dirty" is valid only with the last case (mapped+uptodate).
16721da177e4SLinus Torvalds  */
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds /*
16751da177e4SLinus Torvalds  * While block_write_full_page is writing back the dirty buffers under
16761da177e4SLinus Torvalds  * the page lock, whoever dirtied the buffers may decide to clean them
16771da177e4SLinus Torvalds  * again at any time.  We handle that by only looking at the buffer
16781da177e4SLinus Torvalds  * state inside lock_buffer().
16791da177e4SLinus Torvalds  *
16801da177e4SLinus Torvalds  * If block_write_full_page() is called for regular writeback
16811da177e4SLinus Torvalds  * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
16821da177e4SLinus Torvalds  * locked buffer.   This only can happen if someone has written the buffer
16831da177e4SLinus Torvalds  * directly, with submit_bh().  At the address_space level PageWriteback
16841da177e4SLinus Torvalds  * prevents this contention from occurring.
16856e34eeddSTheodore Ts'o  *
16866e34eeddSTheodore Ts'o  * If block_write_full_page() is called with wbc->sync_mode ==
1687721a9602SJens Axboe  * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
1688721a9602SJens Axboe  * causes the writes to be flagged as synchronous writes.
16891da177e4SLinus Torvalds  */
1690*b4bba389SBenjamin Marzinski int __block_write_full_page(struct inode *inode, struct page *page,
169135c80d5fSChris Mason 			get_block_t *get_block, struct writeback_control *wbc,
169235c80d5fSChris Mason 			bh_end_io_t *handler)
16931da177e4SLinus Torvalds {
16941da177e4SLinus Torvalds 	int err;
16951da177e4SLinus Torvalds 	sector_t block;
16961da177e4SLinus Torvalds 	sector_t last_block;
1697f0fbd5fcSAndrew Morton 	struct buffer_head *bh, *head;
169845bce8f3SLinus Torvalds 	unsigned int blocksize, bbits;
16991da177e4SLinus Torvalds 	int nr_underway = 0;
1700bafc0dbaSTejun Heo 	int write_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
17011da177e4SLinus Torvalds 
170245bce8f3SLinus Torvalds 	head = create_page_buffers(page, inode,
17031da177e4SLinus Torvalds 					(1 << BH_Dirty)|(1 << BH_Uptodate));
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	/*
17061da177e4SLinus Torvalds 	 * Be very careful.  We have no exclusion from __set_page_dirty_buffers
17071da177e4SLinus Torvalds 	 * here, and the (potentially unmapped) buffers may become dirty at
17081da177e4SLinus Torvalds 	 * any time.  If a buffer becomes dirty here after we've inspected it
17091da177e4SLinus Torvalds 	 * then we just miss that fact, and the page stays dirty.
17101da177e4SLinus Torvalds 	 *
17111da177e4SLinus Torvalds 	 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
17121da177e4SLinus Torvalds 	 * handle that here by just cleaning them.
17131da177e4SLinus Torvalds 	 */
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	bh = head;
171645bce8f3SLinus Torvalds 	blocksize = bh->b_size;
171745bce8f3SLinus Torvalds 	bbits = block_size_bits(blocksize);
171845bce8f3SLinus Torvalds 
171909cbfeafSKirill A. Shutemov 	block = (sector_t)page->index << (PAGE_SHIFT - bbits);
172045bce8f3SLinus Torvalds 	last_block = (i_size_read(inode) - 1) >> bbits;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	/*
17231da177e4SLinus Torvalds 	 * Get all the dirty buffers mapped to disk addresses and
17241da177e4SLinus Torvalds 	 * handle any aliases from the underlying blockdev's mapping.
17251da177e4SLinus Torvalds 	 */
17261da177e4SLinus Torvalds 	do {
17271da177e4SLinus Torvalds 		if (block > last_block) {
17281da177e4SLinus Torvalds 			/*
17291da177e4SLinus Torvalds 			 * mapped buffers outside i_size will occur, because
17301da177e4SLinus Torvalds 			 * this page can be outside i_size when there is a
17311da177e4SLinus Torvalds 			 * truncate in progress.
17321da177e4SLinus Torvalds 			 */
17331da177e4SLinus Torvalds 			/*
17341da177e4SLinus Torvalds 			 * The buffer was zeroed by block_write_full_page()
17351da177e4SLinus Torvalds 			 */
17361da177e4SLinus Torvalds 			clear_buffer_dirty(bh);
17371da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
173829a814d2SAlex Tomas 		} else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
173929a814d2SAlex Tomas 			   buffer_dirty(bh)) {
1740b0cf2321SBadari Pulavarty 			WARN_ON(bh->b_size != blocksize);
17411da177e4SLinus Torvalds 			err = get_block(inode, block, bh, 1);
17421da177e4SLinus Torvalds 			if (err)
17431da177e4SLinus Torvalds 				goto recover;
174429a814d2SAlex Tomas 			clear_buffer_delay(bh);
17451da177e4SLinus Torvalds 			if (buffer_new(bh)) {
17461da177e4SLinus Torvalds 				/* blockdev mappings never come here */
17471da177e4SLinus Torvalds 				clear_buffer_new(bh);
17481da177e4SLinus Torvalds 				unmap_underlying_metadata(bh->b_bdev,
17491da177e4SLinus Torvalds 							bh->b_blocknr);
17501da177e4SLinus Torvalds 			}
17511da177e4SLinus Torvalds 		}
17521da177e4SLinus Torvalds 		bh = bh->b_this_page;
17531da177e4SLinus Torvalds 		block++;
17541da177e4SLinus Torvalds 	} while (bh != head);
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	do {
17571da177e4SLinus Torvalds 		if (!buffer_mapped(bh))
17581da177e4SLinus Torvalds 			continue;
17591da177e4SLinus Torvalds 		/*
17601da177e4SLinus Torvalds 		 * If it's a fully non-blocking write attempt and we cannot
17611da177e4SLinus Torvalds 		 * lock the buffer then redirty the page.  Note that this can
17625b0830cbSJens Axboe 		 * potentially cause a busy-wait loop from writeback threads
17635b0830cbSJens Axboe 		 * and kswapd activity, but those code paths have their own
17645b0830cbSJens Axboe 		 * higher-level throttling.
17651da177e4SLinus Torvalds 		 */
17661b430beeSWu Fengguang 		if (wbc->sync_mode != WB_SYNC_NONE) {
17671da177e4SLinus Torvalds 			lock_buffer(bh);
1768ca5de404SNick Piggin 		} else if (!trylock_buffer(bh)) {
17691da177e4SLinus Torvalds 			redirty_page_for_writepage(wbc, page);
17701da177e4SLinus Torvalds 			continue;
17711da177e4SLinus Torvalds 		}
17721da177e4SLinus Torvalds 		if (test_clear_buffer_dirty(bh)) {
177335c80d5fSChris Mason 			mark_buffer_async_write_endio(bh, handler);
17741da177e4SLinus Torvalds 		} else {
17751da177e4SLinus Torvalds 			unlock_buffer(bh);
17761da177e4SLinus Torvalds 		}
17771da177e4SLinus Torvalds 	} while ((bh = bh->b_this_page) != head);
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	/*
17801da177e4SLinus Torvalds 	 * The page and its buffers are protected by PageWriteback(), so we can
17811da177e4SLinus Torvalds 	 * drop the bh refcounts early.
17821da177e4SLinus Torvalds 	 */
17831da177e4SLinus Torvalds 	BUG_ON(PageWriteback(page));
17841da177e4SLinus Torvalds 	set_page_writeback(page);
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	do {
17871da177e4SLinus Torvalds 		struct buffer_head *next = bh->b_this_page;
17881da177e4SLinus Torvalds 		if (buffer_async_write(bh)) {
1789b16b1debSTejun Heo 			submit_bh_wbc(write_op, bh, 0, wbc);
17901da177e4SLinus Torvalds 			nr_underway++;
1791ad576e63SNick Piggin 		}
17921da177e4SLinus Torvalds 		bh = next;
17931da177e4SLinus Torvalds 	} while (bh != head);
179405937baaSAndrew Morton 	unlock_page(page);
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds 	err = 0;
17971da177e4SLinus Torvalds done:
17981da177e4SLinus Torvalds 	if (nr_underway == 0) {
17991da177e4SLinus Torvalds 		/*
18001da177e4SLinus Torvalds 		 * The page was marked dirty, but the buffers were
18011da177e4SLinus Torvalds 		 * clean.  Someone wrote them back by hand with
18021da177e4SLinus Torvalds 		 * ll_rw_block/submit_bh.  A rare case.
18031da177e4SLinus Torvalds 		 */
18041da177e4SLinus Torvalds 		end_page_writeback(page);
18053d67f2d7SNick Piggin 
18061da177e4SLinus Torvalds 		/*
18071da177e4SLinus Torvalds 		 * The page and buffer_heads can be released at any time from
18081da177e4SLinus Torvalds 		 * here on.
18091da177e4SLinus Torvalds 		 */
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 	return err;
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds recover:
18141da177e4SLinus Torvalds 	/*
18151da177e4SLinus Torvalds 	 * ENOSPC, or some other error.  We may already have added some
18161da177e4SLinus Torvalds 	 * blocks to the file, so we need to write these out to avoid
18171da177e4SLinus Torvalds 	 * exposing stale data.
18181da177e4SLinus Torvalds 	 * The page is currently locked and not marked for writeback
18191da177e4SLinus Torvalds 	 */
18201da177e4SLinus Torvalds 	bh = head;
18211da177e4SLinus Torvalds 	/* Recovery: lock and submit the mapped buffers */
18221da177e4SLinus Torvalds 	do {
182329a814d2SAlex Tomas 		if (buffer_mapped(bh) && buffer_dirty(bh) &&
182429a814d2SAlex Tomas 		    !buffer_delay(bh)) {
18251da177e4SLinus Torvalds 			lock_buffer(bh);
182635c80d5fSChris Mason 			mark_buffer_async_write_endio(bh, handler);
18271da177e4SLinus Torvalds 		} else {
18281da177e4SLinus Torvalds 			/*
18291da177e4SLinus Torvalds 			 * The buffer may have been set dirty during
18301da177e4SLinus Torvalds 			 * attachment to a dirty page.
18311da177e4SLinus Torvalds 			 */
18321da177e4SLinus Torvalds 			clear_buffer_dirty(bh);
18331da177e4SLinus Torvalds 		}
18341da177e4SLinus Torvalds 	} while ((bh = bh->b_this_page) != head);
18351da177e4SLinus Torvalds 	SetPageError(page);
18361da177e4SLinus Torvalds 	BUG_ON(PageWriteback(page));
18377e4c3690SAndrew Morton 	mapping_set_error(page->mapping, err);
18381da177e4SLinus Torvalds 	set_page_writeback(page);
18391da177e4SLinus Torvalds 	do {
18401da177e4SLinus Torvalds 		struct buffer_head *next = bh->b_this_page;
18411da177e4SLinus Torvalds 		if (buffer_async_write(bh)) {
18421da177e4SLinus Torvalds 			clear_buffer_dirty(bh);
1843b16b1debSTejun Heo 			submit_bh_wbc(write_op, bh, 0, wbc);
18441da177e4SLinus Torvalds 			nr_underway++;
1845ad576e63SNick Piggin 		}
18461da177e4SLinus Torvalds 		bh = next;
18471da177e4SLinus Torvalds 	} while (bh != head);
1848ffda9d30SNick Piggin 	unlock_page(page);
18491da177e4SLinus Torvalds 	goto done;
18501da177e4SLinus Torvalds }
1851*b4bba389SBenjamin Marzinski EXPORT_SYMBOL(__block_write_full_page);
18521da177e4SLinus Torvalds 
1853afddba49SNick Piggin /*
1854afddba49SNick Piggin  * If a page has any new buffers, zero them out here, and mark them uptodate
1855afddba49SNick Piggin  * and dirty so they'll be written out (in order to prevent uninitialised
1856afddba49SNick Piggin  * block data from leaking). And clear the new bit.
1857afddba49SNick Piggin  */
1858afddba49SNick Piggin void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1859afddba49SNick Piggin {
1860afddba49SNick Piggin 	unsigned int block_start, block_end;
1861afddba49SNick Piggin 	struct buffer_head *head, *bh;
1862afddba49SNick Piggin 
1863afddba49SNick Piggin 	BUG_ON(!PageLocked(page));
1864afddba49SNick Piggin 	if (!page_has_buffers(page))
1865afddba49SNick Piggin 		return;
1866afddba49SNick Piggin 
1867afddba49SNick Piggin 	bh = head = page_buffers(page);
1868afddba49SNick Piggin 	block_start = 0;
1869afddba49SNick Piggin 	do {
1870afddba49SNick Piggin 		block_end = block_start + bh->b_size;
1871afddba49SNick Piggin 
1872afddba49SNick Piggin 		if (buffer_new(bh)) {
1873afddba49SNick Piggin 			if (block_end > from && block_start < to) {
1874afddba49SNick Piggin 				if (!PageUptodate(page)) {
1875afddba49SNick Piggin 					unsigned start, size;
1876afddba49SNick Piggin 
1877afddba49SNick Piggin 					start = max(from, block_start);
1878afddba49SNick Piggin 					size = min(to, block_end) - start;
1879afddba49SNick Piggin 
1880eebd2aa3SChristoph Lameter 					zero_user(page, start, size);
1881afddba49SNick Piggin 					set_buffer_uptodate(bh);
1882afddba49SNick Piggin 				}
1883afddba49SNick Piggin 
1884afddba49SNick Piggin 				clear_buffer_new(bh);
1885afddba49SNick Piggin 				mark_buffer_dirty(bh);
1886afddba49SNick Piggin 			}
1887afddba49SNick Piggin 		}
1888afddba49SNick Piggin 
1889afddba49SNick Piggin 		block_start = block_end;
1890afddba49SNick Piggin 		bh = bh->b_this_page;
1891afddba49SNick Piggin 	} while (bh != head);
1892afddba49SNick Piggin }
1893afddba49SNick Piggin EXPORT_SYMBOL(page_zero_new_buffers);
1894afddba49SNick Piggin 
1895ebdec241SChristoph Hellwig int __block_write_begin(struct page *page, loff_t pos, unsigned len,
18966e1db88dSChristoph Hellwig 		get_block_t *get_block)
18971da177e4SLinus Torvalds {
189809cbfeafSKirill A. Shutemov 	unsigned from = pos & (PAGE_SIZE - 1);
1899ebdec241SChristoph Hellwig 	unsigned to = from + len;
19006e1db88dSChristoph Hellwig 	struct inode *inode = page->mapping->host;
19011da177e4SLinus Torvalds 	unsigned block_start, block_end;
19021da177e4SLinus Torvalds 	sector_t block;
19031da177e4SLinus Torvalds 	int err = 0;
19041da177e4SLinus Torvalds 	unsigned blocksize, bbits;
19051da177e4SLinus Torvalds 	struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
190809cbfeafSKirill A. Shutemov 	BUG_ON(from > PAGE_SIZE);
190909cbfeafSKirill A. Shutemov 	BUG_ON(to > PAGE_SIZE);
19101da177e4SLinus Torvalds 	BUG_ON(from > to);
19111da177e4SLinus Torvalds 
191245bce8f3SLinus Torvalds 	head = create_page_buffers(page, inode, 0);
191345bce8f3SLinus Torvalds 	blocksize = head->b_size;
191445bce8f3SLinus Torvalds 	bbits = block_size_bits(blocksize);
19151da177e4SLinus Torvalds 
191609cbfeafSKirill A. Shutemov 	block = (sector_t)page->index << (PAGE_SHIFT - bbits);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	for(bh = head, block_start = 0; bh != head || !block_start;
19191da177e4SLinus Torvalds 	    block++, block_start=block_end, bh = bh->b_this_page) {
19201da177e4SLinus Torvalds 		block_end = block_start + blocksize;
19211da177e4SLinus Torvalds 		if (block_end <= from || block_start >= to) {
19221da177e4SLinus Torvalds 			if (PageUptodate(page)) {
19231da177e4SLinus Torvalds 				if (!buffer_uptodate(bh))
19241da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
19251da177e4SLinus Torvalds 			}
19261da177e4SLinus Torvalds 			continue;
19271da177e4SLinus Torvalds 		}
19281da177e4SLinus Torvalds 		if (buffer_new(bh))
19291da177e4SLinus Torvalds 			clear_buffer_new(bh);
19301da177e4SLinus Torvalds 		if (!buffer_mapped(bh)) {
1931b0cf2321SBadari Pulavarty 			WARN_ON(bh->b_size != blocksize);
19321da177e4SLinus Torvalds 			err = get_block(inode, block, bh, 1);
19331da177e4SLinus Torvalds 			if (err)
1934f3ddbdc6SNick Piggin 				break;
19351da177e4SLinus Torvalds 			if (buffer_new(bh)) {
19361da177e4SLinus Torvalds 				unmap_underlying_metadata(bh->b_bdev,
19371da177e4SLinus Torvalds 							bh->b_blocknr);
19381da177e4SLinus Torvalds 				if (PageUptodate(page)) {
1939637aff46SNick Piggin 					clear_buffer_new(bh);
19401da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
1941637aff46SNick Piggin 					mark_buffer_dirty(bh);
19421da177e4SLinus Torvalds 					continue;
19431da177e4SLinus Torvalds 				}
1944eebd2aa3SChristoph Lameter 				if (block_end > to || block_start < from)
1945eebd2aa3SChristoph Lameter 					zero_user_segments(page,
1946eebd2aa3SChristoph Lameter 						to, block_end,
1947eebd2aa3SChristoph Lameter 						block_start, from);
19481da177e4SLinus Torvalds 				continue;
19491da177e4SLinus Torvalds 			}
19501da177e4SLinus Torvalds 		}
19511da177e4SLinus Torvalds 		if (PageUptodate(page)) {
19521da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
19531da177e4SLinus Torvalds 				set_buffer_uptodate(bh);
19541da177e4SLinus Torvalds 			continue;
19551da177e4SLinus Torvalds 		}
19561da177e4SLinus Torvalds 		if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
195733a266ddSDavid Chinner 		    !buffer_unwritten(bh) &&
19581da177e4SLinus Torvalds 		     (block_start < from || block_end > to)) {
19591da177e4SLinus Torvalds 			ll_rw_block(READ, 1, &bh);
19601da177e4SLinus Torvalds 			*wait_bh++=bh;
19611da177e4SLinus Torvalds 		}
19621da177e4SLinus Torvalds 	}
19631da177e4SLinus Torvalds 	/*
19641da177e4SLinus Torvalds 	 * If we issued read requests - let them complete.
19651da177e4SLinus Torvalds 	 */
19661da177e4SLinus Torvalds 	while(wait_bh > wait) {
19671da177e4SLinus Torvalds 		wait_on_buffer(*--wait_bh);
19681da177e4SLinus Torvalds 		if (!buffer_uptodate(*wait_bh))
1969f3ddbdc6SNick Piggin 			err = -EIO;
19701da177e4SLinus Torvalds 	}
1971f9f07b6cSJan Kara 	if (unlikely(err))
1972afddba49SNick Piggin 		page_zero_new_buffers(page, from, to);
19731da177e4SLinus Torvalds 	return err;
19741da177e4SLinus Torvalds }
1975ebdec241SChristoph Hellwig EXPORT_SYMBOL(__block_write_begin);
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds static int __block_commit_write(struct inode *inode, struct page *page,
19781da177e4SLinus Torvalds 		unsigned from, unsigned to)
19791da177e4SLinus Torvalds {
19801da177e4SLinus Torvalds 	unsigned block_start, block_end;
19811da177e4SLinus Torvalds 	int partial = 0;
19821da177e4SLinus Torvalds 	unsigned blocksize;
19831da177e4SLinus Torvalds 	struct buffer_head *bh, *head;
19841da177e4SLinus Torvalds 
198545bce8f3SLinus Torvalds 	bh = head = page_buffers(page);
198645bce8f3SLinus Torvalds 	blocksize = bh->b_size;
19871da177e4SLinus Torvalds 
198845bce8f3SLinus Torvalds 	block_start = 0;
198945bce8f3SLinus Torvalds 	do {
19901da177e4SLinus Torvalds 		block_end = block_start + blocksize;
19911da177e4SLinus Torvalds 		if (block_end <= from || block_start >= to) {
19921da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
19931da177e4SLinus Torvalds 				partial = 1;
19941da177e4SLinus Torvalds 		} else {
19951da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
19961da177e4SLinus Torvalds 			mark_buffer_dirty(bh);
19971da177e4SLinus Torvalds 		}
1998afddba49SNick Piggin 		clear_buffer_new(bh);
199945bce8f3SLinus Torvalds 
200045bce8f3SLinus Torvalds 		block_start = block_end;
200145bce8f3SLinus Torvalds 		bh = bh->b_this_page;
200245bce8f3SLinus Torvalds 	} while (bh != head);
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	/*
20051da177e4SLinus Torvalds 	 * If this is a partial write which happened to make all buffers
20061da177e4SLinus Torvalds 	 * uptodate then we can optimize away a bogus readpage() for
20071da177e4SLinus Torvalds 	 * the next read(). Here we 'discover' whether the page went
20081da177e4SLinus Torvalds 	 * uptodate as a result of this (potentially partial) write.
20091da177e4SLinus Torvalds 	 */
20101da177e4SLinus Torvalds 	if (!partial)
20111da177e4SLinus Torvalds 		SetPageUptodate(page);
20121da177e4SLinus Torvalds 	return 0;
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds /*
2016155130a4SChristoph Hellwig  * block_write_begin takes care of the basic task of block allocation and
2017155130a4SChristoph Hellwig  * bringing partial write blocks uptodate first.
2018155130a4SChristoph Hellwig  *
20197bb46a67Snpiggin@suse.de  * The filesystem needs to handle block truncation upon failure.
2020afddba49SNick Piggin  */
2021155130a4SChristoph Hellwig int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
2022155130a4SChristoph Hellwig 		unsigned flags, struct page **pagep, get_block_t *get_block)
2023afddba49SNick Piggin {
202409cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
2025afddba49SNick Piggin 	struct page *page;
20266e1db88dSChristoph Hellwig 	int status;
2027afddba49SNick Piggin 
202854566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
20296e1db88dSChristoph Hellwig 	if (!page)
20306e1db88dSChristoph Hellwig 		return -ENOMEM;
2031afddba49SNick Piggin 
20326e1db88dSChristoph Hellwig 	status = __block_write_begin(page, pos, len, get_block);
2033afddba49SNick Piggin 	if (unlikely(status)) {
2034afddba49SNick Piggin 		unlock_page(page);
203509cbfeafSKirill A. Shutemov 		put_page(page);
20366e1db88dSChristoph Hellwig 		page = NULL;
2037afddba49SNick Piggin 	}
2038afddba49SNick Piggin 
20396e1db88dSChristoph Hellwig 	*pagep = page;
2040afddba49SNick Piggin 	return status;
2041afddba49SNick Piggin }
2042afddba49SNick Piggin EXPORT_SYMBOL(block_write_begin);
2043afddba49SNick Piggin 
2044afddba49SNick Piggin int block_write_end(struct file *file, struct address_space *mapping,
2045afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
2046afddba49SNick Piggin 			struct page *page, void *fsdata)
2047afddba49SNick Piggin {
2048afddba49SNick Piggin 	struct inode *inode = mapping->host;
2049afddba49SNick Piggin 	unsigned start;
2050afddba49SNick Piggin 
205109cbfeafSKirill A. Shutemov 	start = pos & (PAGE_SIZE - 1);
2052afddba49SNick Piggin 
2053afddba49SNick Piggin 	if (unlikely(copied < len)) {
2054afddba49SNick Piggin 		/*
2055afddba49SNick Piggin 		 * The buffers that were written will now be uptodate, so we
2056afddba49SNick Piggin 		 * don't have to worry about a readpage reading them and
2057afddba49SNick Piggin 		 * overwriting a partial write. However if we have encountered
2058afddba49SNick Piggin 		 * a short write and only partially written into a buffer, it
2059afddba49SNick Piggin 		 * will not be marked uptodate, so a readpage might come in and
2060afddba49SNick Piggin 		 * destroy our partial write.
2061afddba49SNick Piggin 		 *
2062afddba49SNick Piggin 		 * Do the simplest thing, and just treat any short write to a
2063afddba49SNick Piggin 		 * non uptodate page as a zero-length write, and force the
2064afddba49SNick Piggin 		 * caller to redo the whole thing.
2065afddba49SNick Piggin 		 */
2066afddba49SNick Piggin 		if (!PageUptodate(page))
2067afddba49SNick Piggin 			copied = 0;
2068afddba49SNick Piggin 
2069afddba49SNick Piggin 		page_zero_new_buffers(page, start+copied, start+len);
2070afddba49SNick Piggin 	}
2071afddba49SNick Piggin 	flush_dcache_page(page);
2072afddba49SNick Piggin 
2073afddba49SNick Piggin 	/* This could be a short (even 0-length) commit */
2074afddba49SNick Piggin 	__block_commit_write(inode, page, start, start+copied);
2075afddba49SNick Piggin 
2076afddba49SNick Piggin 	return copied;
2077afddba49SNick Piggin }
2078afddba49SNick Piggin EXPORT_SYMBOL(block_write_end);
2079afddba49SNick Piggin 
2080afddba49SNick Piggin int generic_write_end(struct file *file, struct address_space *mapping,
2081afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
2082afddba49SNick Piggin 			struct page *page, void *fsdata)
2083afddba49SNick Piggin {
2084afddba49SNick Piggin 	struct inode *inode = mapping->host;
208590a80202SJan Kara 	loff_t old_size = inode->i_size;
2086c7d206b3SJan Kara 	int i_size_changed = 0;
2087afddba49SNick Piggin 
2088afddba49SNick Piggin 	copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2089afddba49SNick Piggin 
2090afddba49SNick Piggin 	/*
2091afddba49SNick Piggin 	 * No need to use i_size_read() here, the i_size
2092afddba49SNick Piggin 	 * cannot change under us because we hold i_mutex.
2093afddba49SNick Piggin 	 *
2094afddba49SNick Piggin 	 * But it's important to update i_size while still holding page lock:
2095afddba49SNick Piggin 	 * page writeout could otherwise come in and zero beyond i_size.
2096afddba49SNick Piggin 	 */
2097afddba49SNick Piggin 	if (pos+copied > inode->i_size) {
2098afddba49SNick Piggin 		i_size_write(inode, pos+copied);
2099c7d206b3SJan Kara 		i_size_changed = 1;
2100afddba49SNick Piggin 	}
2101afddba49SNick Piggin 
2102afddba49SNick Piggin 	unlock_page(page);
210309cbfeafSKirill A. Shutemov 	put_page(page);
2104afddba49SNick Piggin 
210590a80202SJan Kara 	if (old_size < pos)
210690a80202SJan Kara 		pagecache_isize_extended(inode, old_size, pos);
2107c7d206b3SJan Kara 	/*
2108c7d206b3SJan Kara 	 * Don't mark the inode dirty under page lock. First, it unnecessarily
2109c7d206b3SJan Kara 	 * makes the holding time of page lock longer. Second, it forces lock
2110c7d206b3SJan Kara 	 * ordering of page lock and transaction start for journaling
2111c7d206b3SJan Kara 	 * filesystems.
2112c7d206b3SJan Kara 	 */
2113c7d206b3SJan Kara 	if (i_size_changed)
2114c7d206b3SJan Kara 		mark_inode_dirty(inode);
2115c7d206b3SJan Kara 
2116afddba49SNick Piggin 	return copied;
2117afddba49SNick Piggin }
2118afddba49SNick Piggin EXPORT_SYMBOL(generic_write_end);
2119afddba49SNick Piggin 
2120afddba49SNick Piggin /*
21218ab22b9aSHisashi Hifumi  * block_is_partially_uptodate checks whether buffers within a page are
21228ab22b9aSHisashi Hifumi  * uptodate or not.
21238ab22b9aSHisashi Hifumi  *
21248ab22b9aSHisashi Hifumi  * Returns true if all buffers which correspond to a file portion
21258ab22b9aSHisashi Hifumi  * we want to read are uptodate.
21268ab22b9aSHisashi Hifumi  */
2127c186afb4SAl Viro int block_is_partially_uptodate(struct page *page, unsigned long from,
2128c186afb4SAl Viro 					unsigned long count)
21298ab22b9aSHisashi Hifumi {
21308ab22b9aSHisashi Hifumi 	unsigned block_start, block_end, blocksize;
21318ab22b9aSHisashi Hifumi 	unsigned to;
21328ab22b9aSHisashi Hifumi 	struct buffer_head *bh, *head;
21338ab22b9aSHisashi Hifumi 	int ret = 1;
21348ab22b9aSHisashi Hifumi 
21358ab22b9aSHisashi Hifumi 	if (!page_has_buffers(page))
21368ab22b9aSHisashi Hifumi 		return 0;
21378ab22b9aSHisashi Hifumi 
213845bce8f3SLinus Torvalds 	head = page_buffers(page);
213945bce8f3SLinus Torvalds 	blocksize = head->b_size;
214009cbfeafSKirill A. Shutemov 	to = min_t(unsigned, PAGE_SIZE - from, count);
21418ab22b9aSHisashi Hifumi 	to = from + to;
214209cbfeafSKirill A. Shutemov 	if (from < blocksize && to > PAGE_SIZE - blocksize)
21438ab22b9aSHisashi Hifumi 		return 0;
21448ab22b9aSHisashi Hifumi 
21458ab22b9aSHisashi Hifumi 	bh = head;
21468ab22b9aSHisashi Hifumi 	block_start = 0;
21478ab22b9aSHisashi Hifumi 	do {
21488ab22b9aSHisashi Hifumi 		block_end = block_start + blocksize;
21498ab22b9aSHisashi Hifumi 		if (block_end > from && block_start < to) {
21508ab22b9aSHisashi Hifumi 			if (!buffer_uptodate(bh)) {
21518ab22b9aSHisashi Hifumi 				ret = 0;
21528ab22b9aSHisashi Hifumi 				break;
21538ab22b9aSHisashi Hifumi 			}
21548ab22b9aSHisashi Hifumi 			if (block_end >= to)
21558ab22b9aSHisashi Hifumi 				break;
21568ab22b9aSHisashi Hifumi 		}
21578ab22b9aSHisashi Hifumi 		block_start = block_end;
21588ab22b9aSHisashi Hifumi 		bh = bh->b_this_page;
21598ab22b9aSHisashi Hifumi 	} while (bh != head);
21608ab22b9aSHisashi Hifumi 
21618ab22b9aSHisashi Hifumi 	return ret;
21628ab22b9aSHisashi Hifumi }
21638ab22b9aSHisashi Hifumi EXPORT_SYMBOL(block_is_partially_uptodate);
21648ab22b9aSHisashi Hifumi 
21658ab22b9aSHisashi Hifumi /*
21661da177e4SLinus Torvalds  * Generic "read page" function for block devices that have the normal
21671da177e4SLinus Torvalds  * get_block functionality. This is most of the block device filesystems.
21681da177e4SLinus Torvalds  * Reads the page asynchronously --- the unlock_buffer() and
21691da177e4SLinus Torvalds  * set/clear_buffer_uptodate() functions propagate buffer state into the
21701da177e4SLinus Torvalds  * page struct once IO has completed.
21711da177e4SLinus Torvalds  */
21721da177e4SLinus Torvalds int block_read_full_page(struct page *page, get_block_t *get_block)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
21751da177e4SLinus Torvalds 	sector_t iblock, lblock;
21761da177e4SLinus Torvalds 	struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
217745bce8f3SLinus Torvalds 	unsigned int blocksize, bbits;
21781da177e4SLinus Torvalds 	int nr, i;
21791da177e4SLinus Torvalds 	int fully_mapped = 1;
21801da177e4SLinus Torvalds 
218145bce8f3SLinus Torvalds 	head = create_page_buffers(page, inode, 0);
218245bce8f3SLinus Torvalds 	blocksize = head->b_size;
218345bce8f3SLinus Torvalds 	bbits = block_size_bits(blocksize);
21841da177e4SLinus Torvalds 
218509cbfeafSKirill A. Shutemov 	iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
218645bce8f3SLinus Torvalds 	lblock = (i_size_read(inode)+blocksize-1) >> bbits;
21871da177e4SLinus Torvalds 	bh = head;
21881da177e4SLinus Torvalds 	nr = 0;
21891da177e4SLinus Torvalds 	i = 0;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	do {
21921da177e4SLinus Torvalds 		if (buffer_uptodate(bh))
21931da177e4SLinus Torvalds 			continue;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 		if (!buffer_mapped(bh)) {
2196c64610baSAndrew Morton 			int err = 0;
2197c64610baSAndrew Morton 
21981da177e4SLinus Torvalds 			fully_mapped = 0;
21991da177e4SLinus Torvalds 			if (iblock < lblock) {
2200b0cf2321SBadari Pulavarty 				WARN_ON(bh->b_size != blocksize);
2201c64610baSAndrew Morton 				err = get_block(inode, iblock, bh, 0);
2202c64610baSAndrew Morton 				if (err)
22031da177e4SLinus Torvalds 					SetPageError(page);
22041da177e4SLinus Torvalds 			}
22051da177e4SLinus Torvalds 			if (!buffer_mapped(bh)) {
2206eebd2aa3SChristoph Lameter 				zero_user(page, i * blocksize, blocksize);
2207c64610baSAndrew Morton 				if (!err)
22081da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
22091da177e4SLinus Torvalds 				continue;
22101da177e4SLinus Torvalds 			}
22111da177e4SLinus Torvalds 			/*
22121da177e4SLinus Torvalds 			 * get_block() might have updated the buffer
22131da177e4SLinus Torvalds 			 * synchronously
22141da177e4SLinus Torvalds 			 */
22151da177e4SLinus Torvalds 			if (buffer_uptodate(bh))
22161da177e4SLinus Torvalds 				continue;
22171da177e4SLinus Torvalds 		}
22181da177e4SLinus Torvalds 		arr[nr++] = bh;
22191da177e4SLinus Torvalds 	} while (i++, iblock++, (bh = bh->b_this_page) != head);
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	if (fully_mapped)
22221da177e4SLinus Torvalds 		SetPageMappedToDisk(page);
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	if (!nr) {
22251da177e4SLinus Torvalds 		/*
22261da177e4SLinus Torvalds 		 * All buffers are uptodate - we can set the page uptodate
22271da177e4SLinus Torvalds 		 * as well. But not if get_block() returned an error.
22281da177e4SLinus Torvalds 		 */
22291da177e4SLinus Torvalds 		if (!PageError(page))
22301da177e4SLinus Torvalds 			SetPageUptodate(page);
22311da177e4SLinus Torvalds 		unlock_page(page);
22321da177e4SLinus Torvalds 		return 0;
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds 	/* Stage two: lock the buffers */
22361da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
22371da177e4SLinus Torvalds 		bh = arr[i];
22381da177e4SLinus Torvalds 		lock_buffer(bh);
22391da177e4SLinus Torvalds 		mark_buffer_async_read(bh);
22401da177e4SLinus Torvalds 	}
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	/*
22431da177e4SLinus Torvalds 	 * Stage 3: start the IO.  Check for uptodateness
22441da177e4SLinus Torvalds 	 * inside the buffer lock in case another process reading
22451da177e4SLinus Torvalds 	 * the underlying blockdev brought it uptodate (the sct fix).
22461da177e4SLinus Torvalds 	 */
22471da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
22481da177e4SLinus Torvalds 		bh = arr[i];
22491da177e4SLinus Torvalds 		if (buffer_uptodate(bh))
22501da177e4SLinus Torvalds 			end_buffer_async_read(bh, 1);
22511da177e4SLinus Torvalds 		else
22521da177e4SLinus Torvalds 			submit_bh(READ, bh);
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 	return 0;
22551da177e4SLinus Torvalds }
22561fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_read_full_page);
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds /* utility function for filesystems that need to do work on expanding
225989e10787SNick Piggin  * truncates.  Uses filesystem pagecache writes to allow the filesystem to
22601da177e4SLinus Torvalds  * deal with the hole.
22611da177e4SLinus Torvalds  */
226289e10787SNick Piggin int generic_cont_expand_simple(struct inode *inode, loff_t size)
22631da177e4SLinus Torvalds {
22641da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
22651da177e4SLinus Torvalds 	struct page *page;
226689e10787SNick Piggin 	void *fsdata;
22671da177e4SLinus Torvalds 	int err;
22681da177e4SLinus Torvalds 
2269c08d3b0eSnpiggin@suse.de 	err = inode_newsize_ok(inode, size);
2270c08d3b0eSnpiggin@suse.de 	if (err)
22711da177e4SLinus Torvalds 		goto out;
22721da177e4SLinus Torvalds 
227389e10787SNick Piggin 	err = pagecache_write_begin(NULL, mapping, size, 0,
227489e10787SNick Piggin 				AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
227589e10787SNick Piggin 				&page, &fsdata);
227689e10787SNick Piggin 	if (err)
227705eb0b51SOGAWA Hirofumi 		goto out;
227805eb0b51SOGAWA Hirofumi 
227989e10787SNick Piggin 	err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
228089e10787SNick Piggin 	BUG_ON(err > 0);
228105eb0b51SOGAWA Hirofumi 
228205eb0b51SOGAWA Hirofumi out:
228305eb0b51SOGAWA Hirofumi 	return err;
228405eb0b51SOGAWA Hirofumi }
22851fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(generic_cont_expand_simple);
228605eb0b51SOGAWA Hirofumi 
2287f1e3af72SAdrian Bunk static int cont_expand_zero(struct file *file, struct address_space *mapping,
228889e10787SNick Piggin 			    loff_t pos, loff_t *bytes)
228905eb0b51SOGAWA Hirofumi {
229089e10787SNick Piggin 	struct inode *inode = mapping->host;
229189e10787SNick Piggin 	unsigned blocksize = 1 << inode->i_blkbits;
229289e10787SNick Piggin 	struct page *page;
229389e10787SNick Piggin 	void *fsdata;
229489e10787SNick Piggin 	pgoff_t index, curidx;
229589e10787SNick Piggin 	loff_t curpos;
229689e10787SNick Piggin 	unsigned zerofrom, offset, len;
229789e10787SNick Piggin 	int err = 0;
229805eb0b51SOGAWA Hirofumi 
229909cbfeafSKirill A. Shutemov 	index = pos >> PAGE_SHIFT;
230009cbfeafSKirill A. Shutemov 	offset = pos & ~PAGE_MASK;
230189e10787SNick Piggin 
230209cbfeafSKirill A. Shutemov 	while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
230309cbfeafSKirill A. Shutemov 		zerofrom = curpos & ~PAGE_MASK;
230489e10787SNick Piggin 		if (zerofrom & (blocksize-1)) {
230589e10787SNick Piggin 			*bytes |= (blocksize-1);
230689e10787SNick Piggin 			(*bytes)++;
230789e10787SNick Piggin 		}
230809cbfeafSKirill A. Shutemov 		len = PAGE_SIZE - zerofrom;
230989e10787SNick Piggin 
231089e10787SNick Piggin 		err = pagecache_write_begin(file, mapping, curpos, len,
231189e10787SNick Piggin 						AOP_FLAG_UNINTERRUPTIBLE,
231289e10787SNick Piggin 						&page, &fsdata);
231389e10787SNick Piggin 		if (err)
231489e10787SNick Piggin 			goto out;
2315eebd2aa3SChristoph Lameter 		zero_user(page, zerofrom, len);
231689e10787SNick Piggin 		err = pagecache_write_end(file, mapping, curpos, len, len,
231789e10787SNick Piggin 						page, fsdata);
231889e10787SNick Piggin 		if (err < 0)
231989e10787SNick Piggin 			goto out;
232089e10787SNick Piggin 		BUG_ON(err != len);
232189e10787SNick Piggin 		err = 0;
2322061e9746SOGAWA Hirofumi 
2323061e9746SOGAWA Hirofumi 		balance_dirty_pages_ratelimited(mapping);
2324c2ca0fcdSMikulas Patocka 
2325c2ca0fcdSMikulas Patocka 		if (unlikely(fatal_signal_pending(current))) {
2326c2ca0fcdSMikulas Patocka 			err = -EINTR;
2327c2ca0fcdSMikulas Patocka 			goto out;
2328c2ca0fcdSMikulas Patocka 		}
232989e10787SNick Piggin 	}
233089e10787SNick Piggin 
233189e10787SNick Piggin 	/* page covers the boundary, find the boundary offset */
233289e10787SNick Piggin 	if (index == curidx) {
233309cbfeafSKirill A. Shutemov 		zerofrom = curpos & ~PAGE_MASK;
233489e10787SNick Piggin 		/* if we will expand the thing last block will be filled */
233589e10787SNick Piggin 		if (offset <= zerofrom) {
233689e10787SNick Piggin 			goto out;
233789e10787SNick Piggin 		}
233889e10787SNick Piggin 		if (zerofrom & (blocksize-1)) {
233989e10787SNick Piggin 			*bytes |= (blocksize-1);
234089e10787SNick Piggin 			(*bytes)++;
234189e10787SNick Piggin 		}
234289e10787SNick Piggin 		len = offset - zerofrom;
234389e10787SNick Piggin 
234489e10787SNick Piggin 		err = pagecache_write_begin(file, mapping, curpos, len,
234589e10787SNick Piggin 						AOP_FLAG_UNINTERRUPTIBLE,
234689e10787SNick Piggin 						&page, &fsdata);
234789e10787SNick Piggin 		if (err)
234889e10787SNick Piggin 			goto out;
2349eebd2aa3SChristoph Lameter 		zero_user(page, zerofrom, len);
235089e10787SNick Piggin 		err = pagecache_write_end(file, mapping, curpos, len, len,
235189e10787SNick Piggin 						page, fsdata);
235289e10787SNick Piggin 		if (err < 0)
235389e10787SNick Piggin 			goto out;
235489e10787SNick Piggin 		BUG_ON(err != len);
235589e10787SNick Piggin 		err = 0;
235689e10787SNick Piggin 	}
235789e10787SNick Piggin out:
235889e10787SNick Piggin 	return err;
23591da177e4SLinus Torvalds }
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds /*
23621da177e4SLinus Torvalds  * For moronic filesystems that do not allow holes in file.
23631da177e4SLinus Torvalds  * We may have to extend the file.
23641da177e4SLinus Torvalds  */
2365282dc178SChristoph Hellwig int cont_write_begin(struct file *file, struct address_space *mapping,
236689e10787SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
236789e10787SNick Piggin 			struct page **pagep, void **fsdata,
236889e10787SNick Piggin 			get_block_t *get_block, loff_t *bytes)
23691da177e4SLinus Torvalds {
23701da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
23711da177e4SLinus Torvalds 	unsigned blocksize = 1 << inode->i_blkbits;
237289e10787SNick Piggin 	unsigned zerofrom;
237389e10787SNick Piggin 	int err;
23741da177e4SLinus Torvalds 
237589e10787SNick Piggin 	err = cont_expand_zero(file, mapping, pos, bytes);
237689e10787SNick Piggin 	if (err)
2377155130a4SChristoph Hellwig 		return err;
23781da177e4SLinus Torvalds 
237909cbfeafSKirill A. Shutemov 	zerofrom = *bytes & ~PAGE_MASK;
238089e10787SNick Piggin 	if (pos+len > *bytes && zerofrom & (blocksize-1)) {
23811da177e4SLinus Torvalds 		*bytes |= (blocksize-1);
23821da177e4SLinus Torvalds 		(*bytes)++;
23831da177e4SLinus Torvalds 	}
23841da177e4SLinus Torvalds 
2385155130a4SChristoph Hellwig 	return block_write_begin(mapping, pos, len, flags, pagep, get_block);
23861da177e4SLinus Torvalds }
23871fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(cont_write_begin);
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds int block_commit_write(struct page *page, unsigned from, unsigned to)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
23921da177e4SLinus Torvalds 	__block_commit_write(inode,page,from,to);
23931da177e4SLinus Torvalds 	return 0;
23941da177e4SLinus Torvalds }
23951fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_commit_write);
23961da177e4SLinus Torvalds 
239754171690SDavid Chinner /*
239854171690SDavid Chinner  * block_page_mkwrite() is not allowed to change the file size as it gets
239954171690SDavid Chinner  * called from a page fault handler when a page is first dirtied. Hence we must
240054171690SDavid Chinner  * be careful to check for EOF conditions here. We set the page up correctly
240154171690SDavid Chinner  * for a written page which means we get ENOSPC checking when writing into
240254171690SDavid Chinner  * holes and correct delalloc and unwritten extent mapping on filesystems that
240354171690SDavid Chinner  * support these features.
240454171690SDavid Chinner  *
240554171690SDavid Chinner  * We are not allowed to take the i_mutex here so we have to play games to
240654171690SDavid Chinner  * protect against truncate races as the page could now be beyond EOF.  Because
24077bb46a67Snpiggin@suse.de  * truncate writes the inode size before removing pages, once we have the
240854171690SDavid Chinner  * page lock we can determine safely if the page is beyond EOF. If it is not
240954171690SDavid Chinner  * beyond EOF, then the page is guaranteed safe against truncation until we
241054171690SDavid Chinner  * unlock the page.
2411ea13a864SJan Kara  *
241214da9200SJan Kara  * Direct callers of this function should protect against filesystem freezing
24135c500029SRoss Zwisler  * using sb_start_pagefault() - sb_end_pagefault() functions.
241454171690SDavid Chinner  */
24155c500029SRoss Zwisler int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
241654171690SDavid Chinner 			 get_block_t get_block)
241754171690SDavid Chinner {
2418c2ec175cSNick Piggin 	struct page *page = vmf->page;
2419496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
242054171690SDavid Chinner 	unsigned long end;
242154171690SDavid Chinner 	loff_t size;
242224da4fabSJan Kara 	int ret;
242354171690SDavid Chinner 
242454171690SDavid Chinner 	lock_page(page);
242554171690SDavid Chinner 	size = i_size_read(inode);
242654171690SDavid Chinner 	if ((page->mapping != inode->i_mapping) ||
242718336338SNick Piggin 	    (page_offset(page) > size)) {
242824da4fabSJan Kara 		/* We overload EFAULT to mean page got truncated */
242924da4fabSJan Kara 		ret = -EFAULT;
243024da4fabSJan Kara 		goto out_unlock;
243154171690SDavid Chinner 	}
243254171690SDavid Chinner 
243354171690SDavid Chinner 	/* page is wholly or partially inside EOF */
243409cbfeafSKirill A. Shutemov 	if (((page->index + 1) << PAGE_SHIFT) > size)
243509cbfeafSKirill A. Shutemov 		end = size & ~PAGE_MASK;
243654171690SDavid Chinner 	else
243709cbfeafSKirill A. Shutemov 		end = PAGE_SIZE;
243854171690SDavid Chinner 
2439ebdec241SChristoph Hellwig 	ret = __block_write_begin(page, 0, end, get_block);
244054171690SDavid Chinner 	if (!ret)
244154171690SDavid Chinner 		ret = block_commit_write(page, 0, end);
244254171690SDavid Chinner 
244324da4fabSJan Kara 	if (unlikely(ret < 0))
244424da4fabSJan Kara 		goto out_unlock;
2445ea13a864SJan Kara 	set_page_dirty(page);
24461d1d1a76SDarrick J. Wong 	wait_for_stable_page(page);
244724da4fabSJan Kara 	return 0;
244824da4fabSJan Kara out_unlock:
2449b827e496SNick Piggin 	unlock_page(page);
245054171690SDavid Chinner 	return ret;
245154171690SDavid Chinner }
24521fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_page_mkwrite);
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds /*
245503158cd7SNick Piggin  * nobh_write_begin()'s prereads are special: the buffer_heads are freed
24561da177e4SLinus Torvalds  * immediately, while under the page lock.  So it needs a special end_io
24571da177e4SLinus Torvalds  * handler which does not touch the bh after unlocking it.
24581da177e4SLinus Torvalds  */
24591da177e4SLinus Torvalds static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
24601da177e4SLinus Torvalds {
246168671f35SDmitry Monakhov 	__end_buffer_read_notouch(bh, uptodate);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds /*
246503158cd7SNick Piggin  * Attach the singly-linked list of buffers created by nobh_write_begin, to
246603158cd7SNick Piggin  * the page (converting it to circular linked list and taking care of page
246703158cd7SNick Piggin  * dirty races).
246803158cd7SNick Piggin  */
246903158cd7SNick Piggin static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
247003158cd7SNick Piggin {
247103158cd7SNick Piggin 	struct buffer_head *bh;
247203158cd7SNick Piggin 
247303158cd7SNick Piggin 	BUG_ON(!PageLocked(page));
247403158cd7SNick Piggin 
247503158cd7SNick Piggin 	spin_lock(&page->mapping->private_lock);
247603158cd7SNick Piggin 	bh = head;
247703158cd7SNick Piggin 	do {
247803158cd7SNick Piggin 		if (PageDirty(page))
247903158cd7SNick Piggin 			set_buffer_dirty(bh);
248003158cd7SNick Piggin 		if (!bh->b_this_page)
248103158cd7SNick Piggin 			bh->b_this_page = head;
248203158cd7SNick Piggin 		bh = bh->b_this_page;
248303158cd7SNick Piggin 	} while (bh != head);
248403158cd7SNick Piggin 	attach_page_buffers(page, head);
248503158cd7SNick Piggin 	spin_unlock(&page->mapping->private_lock);
248603158cd7SNick Piggin }
248703158cd7SNick Piggin 
248803158cd7SNick Piggin /*
2489ea0f04e5SChristoph Hellwig  * On entry, the page is fully not uptodate.
2490ea0f04e5SChristoph Hellwig  * On exit the page is fully uptodate in the areas outside (from,to)
24917bb46a67Snpiggin@suse.de  * The filesystem needs to handle block truncation upon failure.
24921da177e4SLinus Torvalds  */
2493ea0f04e5SChristoph Hellwig int nobh_write_begin(struct address_space *mapping,
249403158cd7SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
249503158cd7SNick Piggin 			struct page **pagep, void **fsdata,
24961da177e4SLinus Torvalds 			get_block_t *get_block)
24971da177e4SLinus Torvalds {
249803158cd7SNick Piggin 	struct inode *inode = mapping->host;
24991da177e4SLinus Torvalds 	const unsigned blkbits = inode->i_blkbits;
25001da177e4SLinus Torvalds 	const unsigned blocksize = 1 << blkbits;
2501a4b0672dSNick Piggin 	struct buffer_head *head, *bh;
250203158cd7SNick Piggin 	struct page *page;
250303158cd7SNick Piggin 	pgoff_t index;
250403158cd7SNick Piggin 	unsigned from, to;
25051da177e4SLinus Torvalds 	unsigned block_in_page;
2506a4b0672dSNick Piggin 	unsigned block_start, block_end;
25071da177e4SLinus Torvalds 	sector_t block_in_file;
25081da177e4SLinus Torvalds 	int nr_reads = 0;
25091da177e4SLinus Torvalds 	int ret = 0;
25101da177e4SLinus Torvalds 	int is_mapped_to_disk = 1;
25111da177e4SLinus Torvalds 
251209cbfeafSKirill A. Shutemov 	index = pos >> PAGE_SHIFT;
251309cbfeafSKirill A. Shutemov 	from = pos & (PAGE_SIZE - 1);
251403158cd7SNick Piggin 	to = from + len;
251503158cd7SNick Piggin 
251654566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
251703158cd7SNick Piggin 	if (!page)
251803158cd7SNick Piggin 		return -ENOMEM;
251903158cd7SNick Piggin 	*pagep = page;
252003158cd7SNick Piggin 	*fsdata = NULL;
252103158cd7SNick Piggin 
252203158cd7SNick Piggin 	if (page_has_buffers(page)) {
2523309f77adSNamhyung Kim 		ret = __block_write_begin(page, pos, len, get_block);
2524309f77adSNamhyung Kim 		if (unlikely(ret))
2525309f77adSNamhyung Kim 			goto out_release;
2526309f77adSNamhyung Kim 		return ret;
252703158cd7SNick Piggin 	}
2528a4b0672dSNick Piggin 
25291da177e4SLinus Torvalds 	if (PageMappedToDisk(page))
25301da177e4SLinus Torvalds 		return 0;
25311da177e4SLinus Torvalds 
2532a4b0672dSNick Piggin 	/*
2533a4b0672dSNick Piggin 	 * Allocate buffers so that we can keep track of state, and potentially
2534a4b0672dSNick Piggin 	 * attach them to the page if an error occurs. In the common case of
2535a4b0672dSNick Piggin 	 * no error, they will just be freed again without ever being attached
2536a4b0672dSNick Piggin 	 * to the page (which is all OK, because we're under the page lock).
2537a4b0672dSNick Piggin 	 *
2538a4b0672dSNick Piggin 	 * Be careful: the buffer linked list is a NULL terminated one, rather
2539a4b0672dSNick Piggin 	 * than the circular one we're used to.
2540a4b0672dSNick Piggin 	 */
2541a4b0672dSNick Piggin 	head = alloc_page_buffers(page, blocksize, 0);
254203158cd7SNick Piggin 	if (!head) {
254303158cd7SNick Piggin 		ret = -ENOMEM;
254403158cd7SNick Piggin 		goto out_release;
254503158cd7SNick Piggin 	}
2546a4b0672dSNick Piggin 
254709cbfeafSKirill A. Shutemov 	block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	/*
25501da177e4SLinus Torvalds 	 * We loop across all blocks in the page, whether or not they are
25511da177e4SLinus Torvalds 	 * part of the affected region.  This is so we can discover if the
25521da177e4SLinus Torvalds 	 * page is fully mapped-to-disk.
25531da177e4SLinus Torvalds 	 */
2554a4b0672dSNick Piggin 	for (block_start = 0, block_in_page = 0, bh = head;
255509cbfeafSKirill A. Shutemov 		  block_start < PAGE_SIZE;
2556a4b0672dSNick Piggin 		  block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
25571da177e4SLinus Torvalds 		int create;
25581da177e4SLinus Torvalds 
2559a4b0672dSNick Piggin 		block_end = block_start + blocksize;
2560a4b0672dSNick Piggin 		bh->b_state = 0;
25611da177e4SLinus Torvalds 		create = 1;
25621da177e4SLinus Torvalds 		if (block_start >= to)
25631da177e4SLinus Torvalds 			create = 0;
25641da177e4SLinus Torvalds 		ret = get_block(inode, block_in_file + block_in_page,
2565a4b0672dSNick Piggin 					bh, create);
25661da177e4SLinus Torvalds 		if (ret)
25671da177e4SLinus Torvalds 			goto failed;
2568a4b0672dSNick Piggin 		if (!buffer_mapped(bh))
25691da177e4SLinus Torvalds 			is_mapped_to_disk = 0;
2570a4b0672dSNick Piggin 		if (buffer_new(bh))
2571a4b0672dSNick Piggin 			unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2572a4b0672dSNick Piggin 		if (PageUptodate(page)) {
2573a4b0672dSNick Piggin 			set_buffer_uptodate(bh);
25741da177e4SLinus Torvalds 			continue;
2575a4b0672dSNick Piggin 		}
2576a4b0672dSNick Piggin 		if (buffer_new(bh) || !buffer_mapped(bh)) {
2577eebd2aa3SChristoph Lameter 			zero_user_segments(page, block_start, from,
2578eebd2aa3SChristoph Lameter 							to, block_end);
25791da177e4SLinus Torvalds 			continue;
25801da177e4SLinus Torvalds 		}
2581a4b0672dSNick Piggin 		if (buffer_uptodate(bh))
25821da177e4SLinus Torvalds 			continue;	/* reiserfs does this */
25831da177e4SLinus Torvalds 		if (block_start < from || block_end > to) {
2584a4b0672dSNick Piggin 			lock_buffer(bh);
2585a4b0672dSNick Piggin 			bh->b_end_io = end_buffer_read_nobh;
2586a4b0672dSNick Piggin 			submit_bh(READ, bh);
2587a4b0672dSNick Piggin 			nr_reads++;
25881da177e4SLinus Torvalds 		}
25891da177e4SLinus Torvalds 	}
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	if (nr_reads) {
25921da177e4SLinus Torvalds 		/*
25931da177e4SLinus Torvalds 		 * The page is locked, so these buffers are protected from
25941da177e4SLinus Torvalds 		 * any VM or truncate activity.  Hence we don't need to care
25951da177e4SLinus Torvalds 		 * for the buffer_head refcounts.
25961da177e4SLinus Torvalds 		 */
2597a4b0672dSNick Piggin 		for (bh = head; bh; bh = bh->b_this_page) {
25981da177e4SLinus Torvalds 			wait_on_buffer(bh);
25991da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
26001da177e4SLinus Torvalds 				ret = -EIO;
26011da177e4SLinus Torvalds 		}
26021da177e4SLinus Torvalds 		if (ret)
26031da177e4SLinus Torvalds 			goto failed;
26041da177e4SLinus Torvalds 	}
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	if (is_mapped_to_disk)
26071da177e4SLinus Torvalds 		SetPageMappedToDisk(page);
26081da177e4SLinus Torvalds 
260903158cd7SNick Piggin 	*fsdata = head; /* to be released by nobh_write_end */
2610a4b0672dSNick Piggin 
26111da177e4SLinus Torvalds 	return 0;
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds failed:
261403158cd7SNick Piggin 	BUG_ON(!ret);
26151da177e4SLinus Torvalds 	/*
2616a4b0672dSNick Piggin 	 * Error recovery is a bit difficult. We need to zero out blocks that
2617a4b0672dSNick Piggin 	 * were newly allocated, and dirty them to ensure they get written out.
2618a4b0672dSNick Piggin 	 * Buffers need to be attached to the page at this point, otherwise
2619a4b0672dSNick Piggin 	 * the handling of potential IO errors during writeout would be hard
2620a4b0672dSNick Piggin 	 * (could try doing synchronous writeout, but what if that fails too?)
26211da177e4SLinus Torvalds 	 */
262203158cd7SNick Piggin 	attach_nobh_buffers(page, head);
262303158cd7SNick Piggin 	page_zero_new_buffers(page, from, to);
2624a4b0672dSNick Piggin 
262503158cd7SNick Piggin out_release:
262603158cd7SNick Piggin 	unlock_page(page);
262709cbfeafSKirill A. Shutemov 	put_page(page);
262803158cd7SNick Piggin 	*pagep = NULL;
2629a4b0672dSNick Piggin 
26307bb46a67Snpiggin@suse.de 	return ret;
26317bb46a67Snpiggin@suse.de }
263203158cd7SNick Piggin EXPORT_SYMBOL(nobh_write_begin);
26331da177e4SLinus Torvalds 
263403158cd7SNick Piggin int nobh_write_end(struct file *file, struct address_space *mapping,
263503158cd7SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
263603158cd7SNick Piggin 			struct page *page, void *fsdata)
26371da177e4SLinus Torvalds {
26381da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
2639efdc3131SNick Piggin 	struct buffer_head *head = fsdata;
264003158cd7SNick Piggin 	struct buffer_head *bh;
26415b41e74aSDmitri Monakhov 	BUG_ON(fsdata != NULL && page_has_buffers(page));
26421da177e4SLinus Torvalds 
2643d4cf109fSDave Kleikamp 	if (unlikely(copied < len) && head)
264403158cd7SNick Piggin 		attach_nobh_buffers(page, head);
2645a4b0672dSNick Piggin 	if (page_has_buffers(page))
264603158cd7SNick Piggin 		return generic_write_end(file, mapping, pos, len,
264703158cd7SNick Piggin 					copied, page, fsdata);
2648a4b0672dSNick Piggin 
264922c8ca78SNick Piggin 	SetPageUptodate(page);
26501da177e4SLinus Torvalds 	set_page_dirty(page);
265103158cd7SNick Piggin 	if (pos+copied > inode->i_size) {
265203158cd7SNick Piggin 		i_size_write(inode, pos+copied);
26531da177e4SLinus Torvalds 		mark_inode_dirty(inode);
26541da177e4SLinus Torvalds 	}
265503158cd7SNick Piggin 
265603158cd7SNick Piggin 	unlock_page(page);
265709cbfeafSKirill A. Shutemov 	put_page(page);
265803158cd7SNick Piggin 
265903158cd7SNick Piggin 	while (head) {
266003158cd7SNick Piggin 		bh = head;
266103158cd7SNick Piggin 		head = head->b_this_page;
266203158cd7SNick Piggin 		free_buffer_head(bh);
26631da177e4SLinus Torvalds 	}
266403158cd7SNick Piggin 
266503158cd7SNick Piggin 	return copied;
266603158cd7SNick Piggin }
266703158cd7SNick Piggin EXPORT_SYMBOL(nobh_write_end);
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds /*
26701da177e4SLinus Torvalds  * nobh_writepage() - based on block_full_write_page() except
26711da177e4SLinus Torvalds  * that it tries to operate without attaching bufferheads to
26721da177e4SLinus Torvalds  * the page.
26731da177e4SLinus Torvalds  */
26741da177e4SLinus Torvalds int nobh_writepage(struct page *page, get_block_t *get_block,
26751da177e4SLinus Torvalds 			struct writeback_control *wbc)
26761da177e4SLinus Torvalds {
26771da177e4SLinus Torvalds 	struct inode * const inode = page->mapping->host;
26781da177e4SLinus Torvalds 	loff_t i_size = i_size_read(inode);
267909cbfeafSKirill A. Shutemov 	const pgoff_t end_index = i_size >> PAGE_SHIFT;
26801da177e4SLinus Torvalds 	unsigned offset;
26811da177e4SLinus Torvalds 	int ret;
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	/* Is the page fully inside i_size? */
26841da177e4SLinus Torvalds 	if (page->index < end_index)
26851da177e4SLinus Torvalds 		goto out;
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 	/* Is the page fully outside i_size? (truncate in progress) */
268809cbfeafSKirill A. Shutemov 	offset = i_size & (PAGE_SIZE-1);
26891da177e4SLinus Torvalds 	if (page->index >= end_index+1 || !offset) {
26901da177e4SLinus Torvalds 		/*
26911da177e4SLinus Torvalds 		 * The page may have dirty, unmapped buffers.  For example,
26921da177e4SLinus Torvalds 		 * they may have been added in ext3_writepage().  Make them
26931da177e4SLinus Torvalds 		 * freeable here, so the page does not leak.
26941da177e4SLinus Torvalds 		 */
26951da177e4SLinus Torvalds #if 0
26961da177e4SLinus Torvalds 		/* Not really sure about this  - do we need this ? */
26971da177e4SLinus Torvalds 		if (page->mapping->a_ops->invalidatepage)
26981da177e4SLinus Torvalds 			page->mapping->a_ops->invalidatepage(page, offset);
26991da177e4SLinus Torvalds #endif
27001da177e4SLinus Torvalds 		unlock_page(page);
27011da177e4SLinus Torvalds 		return 0; /* don't care */
27021da177e4SLinus Torvalds 	}
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	/*
27051da177e4SLinus Torvalds 	 * The page straddles i_size.  It must be zeroed out on each and every
27061da177e4SLinus Torvalds 	 * writepage invocation because it may be mmapped.  "A file is mapped
27071da177e4SLinus Torvalds 	 * in multiples of the page size.  For a file that is not a multiple of
27081da177e4SLinus Torvalds 	 * the  page size, the remaining memory is zeroed when mapped, and
27091da177e4SLinus Torvalds 	 * writes to that region are not written out to the file."
27101da177e4SLinus Torvalds 	 */
271109cbfeafSKirill A. Shutemov 	zero_user_segment(page, offset, PAGE_SIZE);
27121da177e4SLinus Torvalds out:
27131da177e4SLinus Torvalds 	ret = mpage_writepage(page, get_block, wbc);
27141da177e4SLinus Torvalds 	if (ret == -EAGAIN)
271535c80d5fSChris Mason 		ret = __block_write_full_page(inode, page, get_block, wbc,
271635c80d5fSChris Mason 					      end_buffer_async_write);
27171da177e4SLinus Torvalds 	return ret;
27181da177e4SLinus Torvalds }
27191da177e4SLinus Torvalds EXPORT_SYMBOL(nobh_writepage);
27201da177e4SLinus Torvalds 
272103158cd7SNick Piggin int nobh_truncate_page(struct address_space *mapping,
272203158cd7SNick Piggin 			loff_t from, get_block_t *get_block)
27231da177e4SLinus Torvalds {
272409cbfeafSKirill A. Shutemov 	pgoff_t index = from >> PAGE_SHIFT;
272509cbfeafSKirill A. Shutemov 	unsigned offset = from & (PAGE_SIZE-1);
272603158cd7SNick Piggin 	unsigned blocksize;
272703158cd7SNick Piggin 	sector_t iblock;
272803158cd7SNick Piggin 	unsigned length, pos;
272903158cd7SNick Piggin 	struct inode *inode = mapping->host;
27301da177e4SLinus Torvalds 	struct page *page;
273103158cd7SNick Piggin 	struct buffer_head map_bh;
273203158cd7SNick Piggin 	int err;
27331da177e4SLinus Torvalds 
273403158cd7SNick Piggin 	blocksize = 1 << inode->i_blkbits;
273503158cd7SNick Piggin 	length = offset & (blocksize - 1);
27361da177e4SLinus Torvalds 
273703158cd7SNick Piggin 	/* Block boundary? Nothing to do */
273803158cd7SNick Piggin 	if (!length)
273903158cd7SNick Piggin 		return 0;
274003158cd7SNick Piggin 
274103158cd7SNick Piggin 	length = blocksize - length;
274209cbfeafSKirill A. Shutemov 	iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
274303158cd7SNick Piggin 
27441da177e4SLinus Torvalds 	page = grab_cache_page(mapping, index);
274503158cd7SNick Piggin 	err = -ENOMEM;
27461da177e4SLinus Torvalds 	if (!page)
27471da177e4SLinus Torvalds 		goto out;
27481da177e4SLinus Torvalds 
274903158cd7SNick Piggin 	if (page_has_buffers(page)) {
275003158cd7SNick Piggin has_buffers:
275103158cd7SNick Piggin 		unlock_page(page);
275209cbfeafSKirill A. Shutemov 		put_page(page);
275303158cd7SNick Piggin 		return block_truncate_page(mapping, from, get_block);
27541da177e4SLinus Torvalds 	}
275503158cd7SNick Piggin 
275603158cd7SNick Piggin 	/* Find the buffer that contains "offset" */
275703158cd7SNick Piggin 	pos = blocksize;
275803158cd7SNick Piggin 	while (offset >= pos) {
275903158cd7SNick Piggin 		iblock++;
276003158cd7SNick Piggin 		pos += blocksize;
276103158cd7SNick Piggin 	}
276203158cd7SNick Piggin 
2763460bcf57STheodore Ts'o 	map_bh.b_size = blocksize;
2764460bcf57STheodore Ts'o 	map_bh.b_state = 0;
276503158cd7SNick Piggin 	err = get_block(inode, iblock, &map_bh, 0);
276603158cd7SNick Piggin 	if (err)
276703158cd7SNick Piggin 		goto unlock;
276803158cd7SNick Piggin 	/* unmapped? It's a hole - nothing to do */
276903158cd7SNick Piggin 	if (!buffer_mapped(&map_bh))
277003158cd7SNick Piggin 		goto unlock;
277103158cd7SNick Piggin 
277203158cd7SNick Piggin 	/* Ok, it's mapped. Make sure it's up-to-date */
277303158cd7SNick Piggin 	if (!PageUptodate(page)) {
277403158cd7SNick Piggin 		err = mapping->a_ops->readpage(NULL, page);
277503158cd7SNick Piggin 		if (err) {
277609cbfeafSKirill A. Shutemov 			put_page(page);
277703158cd7SNick Piggin 			goto out;
277803158cd7SNick Piggin 		}
277903158cd7SNick Piggin 		lock_page(page);
278003158cd7SNick Piggin 		if (!PageUptodate(page)) {
278103158cd7SNick Piggin 			err = -EIO;
278203158cd7SNick Piggin 			goto unlock;
278303158cd7SNick Piggin 		}
278403158cd7SNick Piggin 		if (page_has_buffers(page))
278503158cd7SNick Piggin 			goto has_buffers;
278603158cd7SNick Piggin 	}
2787eebd2aa3SChristoph Lameter 	zero_user(page, offset, length);
278803158cd7SNick Piggin 	set_page_dirty(page);
278903158cd7SNick Piggin 	err = 0;
279003158cd7SNick Piggin 
279103158cd7SNick Piggin unlock:
27921da177e4SLinus Torvalds 	unlock_page(page);
279309cbfeafSKirill A. Shutemov 	put_page(page);
27941da177e4SLinus Torvalds out:
279503158cd7SNick Piggin 	return err;
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds EXPORT_SYMBOL(nobh_truncate_page);
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds int block_truncate_page(struct address_space *mapping,
28001da177e4SLinus Torvalds 			loff_t from, get_block_t *get_block)
28011da177e4SLinus Torvalds {
280209cbfeafSKirill A. Shutemov 	pgoff_t index = from >> PAGE_SHIFT;
280309cbfeafSKirill A. Shutemov 	unsigned offset = from & (PAGE_SIZE-1);
28041da177e4SLinus Torvalds 	unsigned blocksize;
280554b21a79SAndrew Morton 	sector_t iblock;
28061da177e4SLinus Torvalds 	unsigned length, pos;
28071da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
28081da177e4SLinus Torvalds 	struct page *page;
28091da177e4SLinus Torvalds 	struct buffer_head *bh;
28101da177e4SLinus Torvalds 	int err;
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds 	blocksize = 1 << inode->i_blkbits;
28131da177e4SLinus Torvalds 	length = offset & (blocksize - 1);
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds 	/* Block boundary? Nothing to do */
28161da177e4SLinus Torvalds 	if (!length)
28171da177e4SLinus Torvalds 		return 0;
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 	length = blocksize - length;
282009cbfeafSKirill A. Shutemov 	iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 	page = grab_cache_page(mapping, index);
28231da177e4SLinus Torvalds 	err = -ENOMEM;
28241da177e4SLinus Torvalds 	if (!page)
28251da177e4SLinus Torvalds 		goto out;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 	if (!page_has_buffers(page))
28281da177e4SLinus Torvalds 		create_empty_buffers(page, blocksize, 0);
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds 	/* Find the buffer that contains "offset" */
28311da177e4SLinus Torvalds 	bh = page_buffers(page);
28321da177e4SLinus Torvalds 	pos = blocksize;
28331da177e4SLinus Torvalds 	while (offset >= pos) {
28341da177e4SLinus Torvalds 		bh = bh->b_this_page;
28351da177e4SLinus Torvalds 		iblock++;
28361da177e4SLinus Torvalds 		pos += blocksize;
28371da177e4SLinus Torvalds 	}
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds 	err = 0;
28401da177e4SLinus Torvalds 	if (!buffer_mapped(bh)) {
2841b0cf2321SBadari Pulavarty 		WARN_ON(bh->b_size != blocksize);
28421da177e4SLinus Torvalds 		err = get_block(inode, iblock, bh, 0);
28431da177e4SLinus Torvalds 		if (err)
28441da177e4SLinus Torvalds 			goto unlock;
28451da177e4SLinus Torvalds 		/* unmapped? It's a hole - nothing to do */
28461da177e4SLinus Torvalds 		if (!buffer_mapped(bh))
28471da177e4SLinus Torvalds 			goto unlock;
28481da177e4SLinus Torvalds 	}
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 	/* Ok, it's mapped. Make sure it's up-to-date */
28511da177e4SLinus Torvalds 	if (PageUptodate(page))
28521da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
28531da177e4SLinus Torvalds 
285433a266ddSDavid Chinner 	if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
28551da177e4SLinus Torvalds 		err = -EIO;
28561da177e4SLinus Torvalds 		ll_rw_block(READ, 1, &bh);
28571da177e4SLinus Torvalds 		wait_on_buffer(bh);
28581da177e4SLinus Torvalds 		/* Uhhuh. Read error. Complain and punt. */
28591da177e4SLinus Torvalds 		if (!buffer_uptodate(bh))
28601da177e4SLinus Torvalds 			goto unlock;
28611da177e4SLinus Torvalds 	}
28621da177e4SLinus Torvalds 
2863eebd2aa3SChristoph Lameter 	zero_user(page, offset, length);
28641da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
28651da177e4SLinus Torvalds 	err = 0;
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds unlock:
28681da177e4SLinus Torvalds 	unlock_page(page);
286909cbfeafSKirill A. Shutemov 	put_page(page);
28701da177e4SLinus Torvalds out:
28711da177e4SLinus Torvalds 	return err;
28721da177e4SLinus Torvalds }
28731fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_truncate_page);
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds /*
28761da177e4SLinus Torvalds  * The generic ->writepage function for buffer-backed address_spaces
28771da177e4SLinus Torvalds  */
28781b938c08SMatthew Wilcox int block_write_full_page(struct page *page, get_block_t *get_block,
28791b938c08SMatthew Wilcox 			struct writeback_control *wbc)
28801da177e4SLinus Torvalds {
28811da177e4SLinus Torvalds 	struct inode * const inode = page->mapping->host;
28821da177e4SLinus Torvalds 	loff_t i_size = i_size_read(inode);
288309cbfeafSKirill A. Shutemov 	const pgoff_t end_index = i_size >> PAGE_SHIFT;
28841da177e4SLinus Torvalds 	unsigned offset;
28851da177e4SLinus Torvalds 
28861da177e4SLinus Torvalds 	/* Is the page fully inside i_size? */
28871da177e4SLinus Torvalds 	if (page->index < end_index)
288835c80d5fSChris Mason 		return __block_write_full_page(inode, page, get_block, wbc,
28891b938c08SMatthew Wilcox 					       end_buffer_async_write);
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 	/* Is the page fully outside i_size? (truncate in progress) */
289209cbfeafSKirill A. Shutemov 	offset = i_size & (PAGE_SIZE-1);
28931da177e4SLinus Torvalds 	if (page->index >= end_index+1 || !offset) {
28941da177e4SLinus Torvalds 		/*
28951da177e4SLinus Torvalds 		 * The page may have dirty, unmapped buffers.  For example,
28961da177e4SLinus Torvalds 		 * they may have been added in ext3_writepage().  Make them
28971da177e4SLinus Torvalds 		 * freeable here, so the page does not leak.
28981da177e4SLinus Torvalds 		 */
289909cbfeafSKirill A. Shutemov 		do_invalidatepage(page, 0, PAGE_SIZE);
29001da177e4SLinus Torvalds 		unlock_page(page);
29011da177e4SLinus Torvalds 		return 0; /* don't care */
29021da177e4SLinus Torvalds 	}
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds 	/*
29051da177e4SLinus Torvalds 	 * The page straddles i_size.  It must be zeroed out on each and every
29062a61aa40SAdam Buchbinder 	 * writepage invocation because it may be mmapped.  "A file is mapped
29071da177e4SLinus Torvalds 	 * in multiples of the page size.  For a file that is not a multiple of
29081da177e4SLinus Torvalds 	 * the  page size, the remaining memory is zeroed when mapped, and
29091da177e4SLinus Torvalds 	 * writes to that region are not written out to the file."
29101da177e4SLinus Torvalds 	 */
291109cbfeafSKirill A. Shutemov 	zero_user_segment(page, offset, PAGE_SIZE);
29121b938c08SMatthew Wilcox 	return __block_write_full_page(inode, page, get_block, wbc,
291335c80d5fSChris Mason 							end_buffer_async_write);
291435c80d5fSChris Mason }
29151fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_write_full_page);
291635c80d5fSChris Mason 
29171da177e4SLinus Torvalds sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
29181da177e4SLinus Torvalds 			    get_block_t *get_block)
29191da177e4SLinus Torvalds {
29201da177e4SLinus Torvalds 	struct buffer_head tmp;
29211da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
29221da177e4SLinus Torvalds 	tmp.b_state = 0;
29231da177e4SLinus Torvalds 	tmp.b_blocknr = 0;
2924b0cf2321SBadari Pulavarty 	tmp.b_size = 1 << inode->i_blkbits;
29251da177e4SLinus Torvalds 	get_block(inode, block, &tmp, 0);
29261da177e4SLinus Torvalds 	return tmp.b_blocknr;
29271da177e4SLinus Torvalds }
29281fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(generic_block_bmap);
29291da177e4SLinus Torvalds 
29304246a0b6SChristoph Hellwig static void end_bio_bh_io_sync(struct bio *bio)
29311da177e4SLinus Torvalds {
29321da177e4SLinus Torvalds 	struct buffer_head *bh = bio->bi_private;
29331da177e4SLinus Torvalds 
2934b7c44ed9SJens Axboe 	if (unlikely(bio_flagged(bio, BIO_QUIET)))
293508bafc03SKeith Mannthey 		set_bit(BH_Quiet, &bh->b_state);
293608bafc03SKeith Mannthey 
29374246a0b6SChristoph Hellwig 	bh->b_end_io(bh, !bio->bi_error);
29381da177e4SLinus Torvalds 	bio_put(bio);
29391da177e4SLinus Torvalds }
29401da177e4SLinus Torvalds 
294157302e0dSLinus Torvalds /*
294257302e0dSLinus Torvalds  * This allows us to do IO even on the odd last sectors
294359d43914SAkinobu Mita  * of a device, even if the block size is some multiple
294457302e0dSLinus Torvalds  * of the physical sector size.
294557302e0dSLinus Torvalds  *
294657302e0dSLinus Torvalds  * We'll just truncate the bio to the size of the device,
294757302e0dSLinus Torvalds  * and clear the end of the buffer head manually.
294857302e0dSLinus Torvalds  *
294957302e0dSLinus Torvalds  * Truly out-of-range accesses will turn into actual IO
295057302e0dSLinus Torvalds  * errors, this only handles the "we need to be able to
295157302e0dSLinus Torvalds  * do IO at the final sector" case.
295257302e0dSLinus Torvalds  */
29534db96b71SAkinobu Mita void guard_bio_eod(int rw, struct bio *bio)
295457302e0dSLinus Torvalds {
295557302e0dSLinus Torvalds 	sector_t maxsector;
295659d43914SAkinobu Mita 	struct bio_vec *bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
295759d43914SAkinobu Mita 	unsigned truncated_bytes;
295857302e0dSLinus Torvalds 
295957302e0dSLinus Torvalds 	maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
296057302e0dSLinus Torvalds 	if (!maxsector)
296157302e0dSLinus Torvalds 		return;
296257302e0dSLinus Torvalds 
296357302e0dSLinus Torvalds 	/*
296457302e0dSLinus Torvalds 	 * If the *whole* IO is past the end of the device,
296557302e0dSLinus Torvalds 	 * let it through, and the IO layer will turn it into
296657302e0dSLinus Torvalds 	 * an EIO.
296757302e0dSLinus Torvalds 	 */
29684f024f37SKent Overstreet 	if (unlikely(bio->bi_iter.bi_sector >= maxsector))
296957302e0dSLinus Torvalds 		return;
297057302e0dSLinus Torvalds 
29714f024f37SKent Overstreet 	maxsector -= bio->bi_iter.bi_sector;
297259d43914SAkinobu Mita 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
297357302e0dSLinus Torvalds 		return;
297457302e0dSLinus Torvalds 
297559d43914SAkinobu Mita 	/* Uhhuh. We've got a bio that straddles the device size! */
297659d43914SAkinobu Mita 	truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
297757302e0dSLinus Torvalds 
297857302e0dSLinus Torvalds 	/* Truncate the bio.. */
297959d43914SAkinobu Mita 	bio->bi_iter.bi_size -= truncated_bytes;
298059d43914SAkinobu Mita 	bvec->bv_len -= truncated_bytes;
298157302e0dSLinus Torvalds 
298257302e0dSLinus Torvalds 	/* ..and clear the end of the buffer for reads */
298327d7c2a0SDan Carpenter 	if ((rw & RW_MASK) == READ) {
298459d43914SAkinobu Mita 		zero_user(bvec->bv_page, bvec->bv_offset + bvec->bv_len,
298559d43914SAkinobu Mita 				truncated_bytes);
298657302e0dSLinus Torvalds 	}
298757302e0dSLinus Torvalds }
298857302e0dSLinus Torvalds 
2989b16b1debSTejun Heo static int submit_bh_wbc(int rw, struct buffer_head *bh,
2990b16b1debSTejun Heo 			 unsigned long bio_flags, struct writeback_control *wbc)
29911da177e4SLinus Torvalds {
29921da177e4SLinus Torvalds 	struct bio *bio;
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds 	BUG_ON(!buffer_locked(bh));
29951da177e4SLinus Torvalds 	BUG_ON(!buffer_mapped(bh));
29961da177e4SLinus Torvalds 	BUG_ON(!bh->b_end_io);
29978fb0e342SAneesh Kumar K.V 	BUG_ON(buffer_delay(bh));
29988fb0e342SAneesh Kumar K.V 	BUG_ON(buffer_unwritten(bh));
29991da177e4SLinus Torvalds 
300048fd4f93SJens Axboe 	/*
300148fd4f93SJens Axboe 	 * Only clear out a write error when rewriting
30021da177e4SLinus Torvalds 	 */
300348fd4f93SJens Axboe 	if (test_set_buffer_req(bh) && (rw & WRITE))
30041da177e4SLinus Torvalds 		clear_buffer_write_io_error(bh);
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	/*
30071da177e4SLinus Torvalds 	 * from here on down, it's all bio -- do the initial mapping,
30081da177e4SLinus Torvalds 	 * submit_bio -> generic_make_request may further map this bio around
30091da177e4SLinus Torvalds 	 */
30101da177e4SLinus Torvalds 	bio = bio_alloc(GFP_NOIO, 1);
30111da177e4SLinus Torvalds 
30122a814908STejun Heo 	if (wbc) {
3013b16b1debSTejun Heo 		wbc_init_bio(wbc, bio);
30142a814908STejun Heo 		wbc_account_io(wbc, bh->b_page, bh->b_size);
30152a814908STejun Heo 	}
3016bafc0dbaSTejun Heo 
30174f024f37SKent Overstreet 	bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
30181da177e4SLinus Torvalds 	bio->bi_bdev = bh->b_bdev;
30191da177e4SLinus Torvalds 
30206cf66b4cSKent Overstreet 	bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
30216cf66b4cSKent Overstreet 	BUG_ON(bio->bi_iter.bi_size != bh->b_size);
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 	bio->bi_end_io = end_bio_bh_io_sync;
30241da177e4SLinus Torvalds 	bio->bi_private = bh;
302571368511SDarrick J. Wong 	bio->bi_flags |= bio_flags;
30261da177e4SLinus Torvalds 
302757302e0dSLinus Torvalds 	/* Take care of bh's that straddle the end of the device */
302859d43914SAkinobu Mita 	guard_bio_eod(rw, bio);
302957302e0dSLinus Torvalds 
3030877f962cSTheodore Ts'o 	if (buffer_meta(bh))
3031877f962cSTheodore Ts'o 		rw |= REQ_META;
3032877f962cSTheodore Ts'o 	if (buffer_prio(bh))
3033877f962cSTheodore Ts'o 		rw |= REQ_PRIO;
3034877f962cSTheodore Ts'o 
30351da177e4SLinus Torvalds 	submit_bio(rw, bio);
3036f6454b04SJulia Lawall 	return 0;
30371da177e4SLinus Torvalds }
3038bafc0dbaSTejun Heo 
3039bafc0dbaSTejun Heo int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
3040bafc0dbaSTejun Heo {
3041b16b1debSTejun Heo 	return submit_bh_wbc(rw, bh, bio_flags, NULL);
3042bafc0dbaSTejun Heo }
304371368511SDarrick J. Wong EXPORT_SYMBOL_GPL(_submit_bh);
304471368511SDarrick J. Wong 
304571368511SDarrick J. Wong int submit_bh(int rw, struct buffer_head *bh)
304671368511SDarrick J. Wong {
3047b16b1debSTejun Heo 	return submit_bh_wbc(rw, bh, 0, NULL);
304871368511SDarrick J. Wong }
30491fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(submit_bh);
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds /**
30521da177e4SLinus Torvalds  * ll_rw_block: low-level access to block devices (DEPRECATED)
30539cb569d6SChristoph Hellwig  * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
30541da177e4SLinus Torvalds  * @nr: number of &struct buffer_heads in the array
30551da177e4SLinus Torvalds  * @bhs: array of pointers to &struct buffer_head
30561da177e4SLinus Torvalds  *
3057a7662236SJan Kara  * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
3058a7662236SJan Kara  * requests an I/O operation on them, either a %READ or a %WRITE.  The third
30599cb569d6SChristoph Hellwig  * %READA option is described in the documentation for generic_make_request()
30609cb569d6SChristoph Hellwig  * which ll_rw_block() calls.
30611da177e4SLinus Torvalds  *
30621da177e4SLinus Torvalds  * This function drops any buffer that it cannot get a lock on (with the
30639cb569d6SChristoph Hellwig  * BH_Lock state bit), any buffer that appears to be clean when doing a write
30649cb569d6SChristoph Hellwig  * request, and any buffer that appears to be up-to-date when doing read
30659cb569d6SChristoph Hellwig  * request.  Further it marks as clean buffers that are processed for
30669cb569d6SChristoph Hellwig  * writing (the buffer cache won't assume that they are actually clean
30679cb569d6SChristoph Hellwig  * until the buffer gets unlocked).
30681da177e4SLinus Torvalds  *
30691da177e4SLinus Torvalds  * ll_rw_block sets b_end_io to simple completion handler that marks
3070e227867fSMasanari Iida  * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
30711da177e4SLinus Torvalds  * any waiters.
30721da177e4SLinus Torvalds  *
30731da177e4SLinus Torvalds  * All of the buffers must be for the same device, and must also be a
30741da177e4SLinus Torvalds  * multiple of the current approved size for the device.
30751da177e4SLinus Torvalds  */
30761da177e4SLinus Torvalds void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
30771da177e4SLinus Torvalds {
30781da177e4SLinus Torvalds 	int i;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
30811da177e4SLinus Torvalds 		struct buffer_head *bh = bhs[i];
30821da177e4SLinus Torvalds 
30839cb569d6SChristoph Hellwig 		if (!trylock_buffer(bh))
30841da177e4SLinus Torvalds 			continue;
30859cb569d6SChristoph Hellwig 		if (rw == WRITE) {
30861da177e4SLinus Torvalds 			if (test_clear_buffer_dirty(bh)) {
308776c3073aSakpm@osdl.org 				bh->b_end_io = end_buffer_write_sync;
3088e60e5c50SOGAWA Hirofumi 				get_bh(bh);
30891da177e4SLinus Torvalds 				submit_bh(WRITE, bh);
30901da177e4SLinus Torvalds 				continue;
30911da177e4SLinus Torvalds 			}
30921da177e4SLinus Torvalds 		} else {
30931da177e4SLinus Torvalds 			if (!buffer_uptodate(bh)) {
309476c3073aSakpm@osdl.org 				bh->b_end_io = end_buffer_read_sync;
3095e60e5c50SOGAWA Hirofumi 				get_bh(bh);
30961da177e4SLinus Torvalds 				submit_bh(rw, bh);
30971da177e4SLinus Torvalds 				continue;
30981da177e4SLinus Torvalds 			}
30991da177e4SLinus Torvalds 		}
31001da177e4SLinus Torvalds 		unlock_buffer(bh);
31011da177e4SLinus Torvalds 	}
31021da177e4SLinus Torvalds }
31031fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(ll_rw_block);
31041da177e4SLinus Torvalds 
31059cb569d6SChristoph Hellwig void write_dirty_buffer(struct buffer_head *bh, int rw)
31069cb569d6SChristoph Hellwig {
31079cb569d6SChristoph Hellwig 	lock_buffer(bh);
31089cb569d6SChristoph Hellwig 	if (!test_clear_buffer_dirty(bh)) {
31099cb569d6SChristoph Hellwig 		unlock_buffer(bh);
31109cb569d6SChristoph Hellwig 		return;
31119cb569d6SChristoph Hellwig 	}
31129cb569d6SChristoph Hellwig 	bh->b_end_io = end_buffer_write_sync;
31139cb569d6SChristoph Hellwig 	get_bh(bh);
31149cb569d6SChristoph Hellwig 	submit_bh(rw, bh);
31159cb569d6SChristoph Hellwig }
31169cb569d6SChristoph Hellwig EXPORT_SYMBOL(write_dirty_buffer);
31179cb569d6SChristoph Hellwig 
31181da177e4SLinus Torvalds /*
31191da177e4SLinus Torvalds  * For a data-integrity writeout, we need to wait upon any in-progress I/O
31201da177e4SLinus Torvalds  * and then start new I/O and then wait upon it.  The caller must have a ref on
31211da177e4SLinus Torvalds  * the buffer_head.
31221da177e4SLinus Torvalds  */
312387e99511SChristoph Hellwig int __sync_dirty_buffer(struct buffer_head *bh, int rw)
31241da177e4SLinus Torvalds {
31251da177e4SLinus Torvalds 	int ret = 0;
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds 	WARN_ON(atomic_read(&bh->b_count) < 1);
31281da177e4SLinus Torvalds 	lock_buffer(bh);
31291da177e4SLinus Torvalds 	if (test_clear_buffer_dirty(bh)) {
31301da177e4SLinus Torvalds 		get_bh(bh);
31311da177e4SLinus Torvalds 		bh->b_end_io = end_buffer_write_sync;
313287e99511SChristoph Hellwig 		ret = submit_bh(rw, bh);
31331da177e4SLinus Torvalds 		wait_on_buffer(bh);
31341da177e4SLinus Torvalds 		if (!ret && !buffer_uptodate(bh))
31351da177e4SLinus Torvalds 			ret = -EIO;
31361da177e4SLinus Torvalds 	} else {
31371da177e4SLinus Torvalds 		unlock_buffer(bh);
31381da177e4SLinus Torvalds 	}
31391da177e4SLinus Torvalds 	return ret;
31401da177e4SLinus Torvalds }
314187e99511SChristoph Hellwig EXPORT_SYMBOL(__sync_dirty_buffer);
314287e99511SChristoph Hellwig 
314387e99511SChristoph Hellwig int sync_dirty_buffer(struct buffer_head *bh)
314487e99511SChristoph Hellwig {
314587e99511SChristoph Hellwig 	return __sync_dirty_buffer(bh, WRITE_SYNC);
314687e99511SChristoph Hellwig }
31471fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(sync_dirty_buffer);
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds /*
31501da177e4SLinus Torvalds  * try_to_free_buffers() checks if all the buffers on this particular page
31511da177e4SLinus Torvalds  * are unused, and releases them if so.
31521da177e4SLinus Torvalds  *
31531da177e4SLinus Torvalds  * Exclusion against try_to_free_buffers may be obtained by either
31541da177e4SLinus Torvalds  * locking the page or by holding its mapping's private_lock.
31551da177e4SLinus Torvalds  *
31561da177e4SLinus Torvalds  * If the page is dirty but all the buffers are clean then we need to
31571da177e4SLinus Torvalds  * be sure to mark the page clean as well.  This is because the page
31581da177e4SLinus Torvalds  * may be against a block device, and a later reattachment of buffers
31591da177e4SLinus Torvalds  * to a dirty page will set *all* buffers dirty.  Which would corrupt
31601da177e4SLinus Torvalds  * filesystem data on the same device.
31611da177e4SLinus Torvalds  *
31621da177e4SLinus Torvalds  * The same applies to regular filesystem pages: if all the buffers are
31631da177e4SLinus Torvalds  * clean then we set the page clean and proceed.  To do that, we require
31641da177e4SLinus Torvalds  * total exclusion from __set_page_dirty_buffers().  That is obtained with
31651da177e4SLinus Torvalds  * private_lock.
31661da177e4SLinus Torvalds  *
31671da177e4SLinus Torvalds  * try_to_free_buffers() is non-blocking.
31681da177e4SLinus Torvalds  */
31691da177e4SLinus Torvalds static inline int buffer_busy(struct buffer_head *bh)
31701da177e4SLinus Torvalds {
31711da177e4SLinus Torvalds 	return atomic_read(&bh->b_count) |
31721da177e4SLinus Torvalds 		(bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds static int
31761da177e4SLinus Torvalds drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
31771da177e4SLinus Torvalds {
31781da177e4SLinus Torvalds 	struct buffer_head *head = page_buffers(page);
31791da177e4SLinus Torvalds 	struct buffer_head *bh;
31801da177e4SLinus Torvalds 
31811da177e4SLinus Torvalds 	bh = head;
31821da177e4SLinus Torvalds 	do {
3183de7d5a3bSakpm@osdl.org 		if (buffer_write_io_error(bh) && page->mapping)
31841da177e4SLinus Torvalds 			set_bit(AS_EIO, &page->mapping->flags);
31851da177e4SLinus Torvalds 		if (buffer_busy(bh))
31861da177e4SLinus Torvalds 			goto failed;
31871da177e4SLinus Torvalds 		bh = bh->b_this_page;
31881da177e4SLinus Torvalds 	} while (bh != head);
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds 	do {
31911da177e4SLinus Torvalds 		struct buffer_head *next = bh->b_this_page;
31921da177e4SLinus Torvalds 
3193535ee2fbSJan Kara 		if (bh->b_assoc_map)
31941da177e4SLinus Torvalds 			__remove_assoc_queue(bh);
31951da177e4SLinus Torvalds 		bh = next;
31961da177e4SLinus Torvalds 	} while (bh != head);
31971da177e4SLinus Torvalds 	*buffers_to_free = head;
31981da177e4SLinus Torvalds 	__clear_page_buffers(page);
31991da177e4SLinus Torvalds 	return 1;
32001da177e4SLinus Torvalds failed:
32011da177e4SLinus Torvalds 	return 0;
32021da177e4SLinus Torvalds }
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds int try_to_free_buffers(struct page *page)
32051da177e4SLinus Torvalds {
32061da177e4SLinus Torvalds 	struct address_space * const mapping = page->mapping;
32071da177e4SLinus Torvalds 	struct buffer_head *buffers_to_free = NULL;
32081da177e4SLinus Torvalds 	int ret = 0;
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
3211ecdfc978SLinus Torvalds 	if (PageWriteback(page))
32121da177e4SLinus Torvalds 		return 0;
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds 	if (mapping == NULL) {		/* can this still happen? */
32151da177e4SLinus Torvalds 		ret = drop_buffers(page, &buffers_to_free);
32161da177e4SLinus Torvalds 		goto out;
32171da177e4SLinus Torvalds 	}
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds 	spin_lock(&mapping->private_lock);
32201da177e4SLinus Torvalds 	ret = drop_buffers(page, &buffers_to_free);
3221ecdfc978SLinus Torvalds 
3222ecdfc978SLinus Torvalds 	/*
3223ecdfc978SLinus Torvalds 	 * If the filesystem writes its buffers by hand (eg ext3)
3224ecdfc978SLinus Torvalds 	 * then we can have clean buffers against a dirty page.  We
3225ecdfc978SLinus Torvalds 	 * clean the page here; otherwise the VM will never notice
3226ecdfc978SLinus Torvalds 	 * that the filesystem did any IO at all.
3227ecdfc978SLinus Torvalds 	 *
3228ecdfc978SLinus Torvalds 	 * Also, during truncate, discard_buffer will have marked all
3229ecdfc978SLinus Torvalds 	 * the page's buffers clean.  We discover that here and clean
3230ecdfc978SLinus Torvalds 	 * the page also.
323187df7241SNick Piggin 	 *
323287df7241SNick Piggin 	 * private_lock must be held over this entire operation in order
323387df7241SNick Piggin 	 * to synchronise against __set_page_dirty_buffers and prevent the
323487df7241SNick Piggin 	 * dirty bit from being lost.
3235ecdfc978SLinus Torvalds 	 */
323611f81becSTejun Heo 	if (ret)
323711f81becSTejun Heo 		cancel_dirty_page(page);
323887df7241SNick Piggin 	spin_unlock(&mapping->private_lock);
32391da177e4SLinus Torvalds out:
32401da177e4SLinus Torvalds 	if (buffers_to_free) {
32411da177e4SLinus Torvalds 		struct buffer_head *bh = buffers_to_free;
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 		do {
32441da177e4SLinus Torvalds 			struct buffer_head *next = bh->b_this_page;
32451da177e4SLinus Torvalds 			free_buffer_head(bh);
32461da177e4SLinus Torvalds 			bh = next;
32471da177e4SLinus Torvalds 		} while (bh != buffers_to_free);
32481da177e4SLinus Torvalds 	}
32491da177e4SLinus Torvalds 	return ret;
32501da177e4SLinus Torvalds }
32511da177e4SLinus Torvalds EXPORT_SYMBOL(try_to_free_buffers);
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds /*
32541da177e4SLinus Torvalds  * There are no bdflush tunables left.  But distributions are
32551da177e4SLinus Torvalds  * still running obsolete flush daemons, so we terminate them here.
32561da177e4SLinus Torvalds  *
32571da177e4SLinus Torvalds  * Use of bdflush() is deprecated and will be removed in a future kernel.
32585b0830cbSJens Axboe  * The `flush-X' kernel threads fully replace bdflush daemons and this call.
32591da177e4SLinus Torvalds  */
3260bdc480e3SHeiko Carstens SYSCALL_DEFINE2(bdflush, int, func, long, data)
32611da177e4SLinus Torvalds {
32621da177e4SLinus Torvalds 	static int msg_count;
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
32651da177e4SLinus Torvalds 		return -EPERM;
32661da177e4SLinus Torvalds 
32671da177e4SLinus Torvalds 	if (msg_count < 5) {
32681da177e4SLinus Torvalds 		msg_count++;
32691da177e4SLinus Torvalds 		printk(KERN_INFO
32701da177e4SLinus Torvalds 			"warning: process `%s' used the obsolete bdflush"
32711da177e4SLinus Torvalds 			" system call\n", current->comm);
32721da177e4SLinus Torvalds 		printk(KERN_INFO "Fix your initscripts?\n");
32731da177e4SLinus Torvalds 	}
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds 	if (func == 1)
32761da177e4SLinus Torvalds 		do_exit(0);
32771da177e4SLinus Torvalds 	return 0;
32781da177e4SLinus Torvalds }
32791da177e4SLinus Torvalds 
32801da177e4SLinus Torvalds /*
32811da177e4SLinus Torvalds  * Buffer-head allocation
32821da177e4SLinus Torvalds  */
3283a0a9b043SShai Fultheim static struct kmem_cache *bh_cachep __read_mostly;
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds /*
32861da177e4SLinus Torvalds  * Once the number of bh's in the machine exceeds this level, we start
32871da177e4SLinus Torvalds  * stripping them in writeback.
32881da177e4SLinus Torvalds  */
328943be594aSZhang Yanfei static unsigned long max_buffer_heads;
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds int buffer_heads_over_limit;
32921da177e4SLinus Torvalds 
32931da177e4SLinus Torvalds struct bh_accounting {
32941da177e4SLinus Torvalds 	int nr;			/* Number of live bh's */
32951da177e4SLinus Torvalds 	int ratelimit;		/* Limit cacheline bouncing */
32961da177e4SLinus Torvalds };
32971da177e4SLinus Torvalds 
32981da177e4SLinus Torvalds static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
32991da177e4SLinus Torvalds 
33001da177e4SLinus Torvalds static void recalc_bh_state(void)
33011da177e4SLinus Torvalds {
33021da177e4SLinus Torvalds 	int i;
33031da177e4SLinus Torvalds 	int tot = 0;
33041da177e4SLinus Torvalds 
3305ee1be862SChristoph Lameter 	if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
33061da177e4SLinus Torvalds 		return;
3307c7b92516SChristoph Lameter 	__this_cpu_write(bh_accounting.ratelimit, 0);
33088a143426SEric Dumazet 	for_each_online_cpu(i)
33091da177e4SLinus Torvalds 		tot += per_cpu(bh_accounting, i).nr;
33101da177e4SLinus Torvalds 	buffer_heads_over_limit = (tot > max_buffer_heads);
33111da177e4SLinus Torvalds }
33121da177e4SLinus Torvalds 
3313dd0fc66fSAl Viro struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
33141da177e4SLinus Torvalds {
3315019b4d12SRichard Kennedy 	struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
33161da177e4SLinus Torvalds 	if (ret) {
3317a35afb83SChristoph Lameter 		INIT_LIST_HEAD(&ret->b_assoc_buffers);
3318c7b92516SChristoph Lameter 		preempt_disable();
3319c7b92516SChristoph Lameter 		__this_cpu_inc(bh_accounting.nr);
33201da177e4SLinus Torvalds 		recalc_bh_state();
3321c7b92516SChristoph Lameter 		preempt_enable();
33221da177e4SLinus Torvalds 	}
33231da177e4SLinus Torvalds 	return ret;
33241da177e4SLinus Torvalds }
33251da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_buffer_head);
33261da177e4SLinus Torvalds 
33271da177e4SLinus Torvalds void free_buffer_head(struct buffer_head *bh)
33281da177e4SLinus Torvalds {
33291da177e4SLinus Torvalds 	BUG_ON(!list_empty(&bh->b_assoc_buffers));
33301da177e4SLinus Torvalds 	kmem_cache_free(bh_cachep, bh);
3331c7b92516SChristoph Lameter 	preempt_disable();
3332c7b92516SChristoph Lameter 	__this_cpu_dec(bh_accounting.nr);
33331da177e4SLinus Torvalds 	recalc_bh_state();
3334c7b92516SChristoph Lameter 	preempt_enable();
33351da177e4SLinus Torvalds }
33361da177e4SLinus Torvalds EXPORT_SYMBOL(free_buffer_head);
33371da177e4SLinus Torvalds 
33381da177e4SLinus Torvalds static void buffer_exit_cpu(int cpu)
33391da177e4SLinus Torvalds {
33401da177e4SLinus Torvalds 	int i;
33411da177e4SLinus Torvalds 	struct bh_lru *b = &per_cpu(bh_lrus, cpu);
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds 	for (i = 0; i < BH_LRU_SIZE; i++) {
33441da177e4SLinus Torvalds 		brelse(b->bhs[i]);
33451da177e4SLinus Torvalds 		b->bhs[i] = NULL;
33461da177e4SLinus Torvalds 	}
3347c7b92516SChristoph Lameter 	this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
33488a143426SEric Dumazet 	per_cpu(bh_accounting, cpu).nr = 0;
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds 
33511da177e4SLinus Torvalds static int buffer_cpu_notify(struct notifier_block *self,
33521da177e4SLinus Torvalds 			      unsigned long action, void *hcpu)
33531da177e4SLinus Torvalds {
33548bb78442SRafael J. Wysocki 	if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
33551da177e4SLinus Torvalds 		buffer_exit_cpu((unsigned long)hcpu);
33561da177e4SLinus Torvalds 	return NOTIFY_OK;
33571da177e4SLinus Torvalds }
33581da177e4SLinus Torvalds 
3359389d1b08SAneesh Kumar K.V /**
3360a6b91919SRandy Dunlap  * bh_uptodate_or_lock - Test whether the buffer is uptodate
3361389d1b08SAneesh Kumar K.V  * @bh: struct buffer_head
3362389d1b08SAneesh Kumar K.V  *
3363389d1b08SAneesh Kumar K.V  * Return true if the buffer is up-to-date and false,
3364389d1b08SAneesh Kumar K.V  * with the buffer locked, if not.
3365389d1b08SAneesh Kumar K.V  */
3366389d1b08SAneesh Kumar K.V int bh_uptodate_or_lock(struct buffer_head *bh)
3367389d1b08SAneesh Kumar K.V {
3368389d1b08SAneesh Kumar K.V 	if (!buffer_uptodate(bh)) {
3369389d1b08SAneesh Kumar K.V 		lock_buffer(bh);
3370389d1b08SAneesh Kumar K.V 		if (!buffer_uptodate(bh))
3371389d1b08SAneesh Kumar K.V 			return 0;
3372389d1b08SAneesh Kumar K.V 		unlock_buffer(bh);
3373389d1b08SAneesh Kumar K.V 	}
3374389d1b08SAneesh Kumar K.V 	return 1;
3375389d1b08SAneesh Kumar K.V }
3376389d1b08SAneesh Kumar K.V EXPORT_SYMBOL(bh_uptodate_or_lock);
3377389d1b08SAneesh Kumar K.V 
3378389d1b08SAneesh Kumar K.V /**
3379a6b91919SRandy Dunlap  * bh_submit_read - Submit a locked buffer for reading
3380389d1b08SAneesh Kumar K.V  * @bh: struct buffer_head
3381389d1b08SAneesh Kumar K.V  *
3382389d1b08SAneesh Kumar K.V  * Returns zero on success and -EIO on error.
3383389d1b08SAneesh Kumar K.V  */
3384389d1b08SAneesh Kumar K.V int bh_submit_read(struct buffer_head *bh)
3385389d1b08SAneesh Kumar K.V {
3386389d1b08SAneesh Kumar K.V 	BUG_ON(!buffer_locked(bh));
3387389d1b08SAneesh Kumar K.V 
3388389d1b08SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
3389389d1b08SAneesh Kumar K.V 		unlock_buffer(bh);
3390389d1b08SAneesh Kumar K.V 		return 0;
3391389d1b08SAneesh Kumar K.V 	}
3392389d1b08SAneesh Kumar K.V 
3393389d1b08SAneesh Kumar K.V 	get_bh(bh);
3394389d1b08SAneesh Kumar K.V 	bh->b_end_io = end_buffer_read_sync;
3395389d1b08SAneesh Kumar K.V 	submit_bh(READ, bh);
3396389d1b08SAneesh Kumar K.V 	wait_on_buffer(bh);
3397389d1b08SAneesh Kumar K.V 	if (buffer_uptodate(bh))
3398389d1b08SAneesh Kumar K.V 		return 0;
3399389d1b08SAneesh Kumar K.V 	return -EIO;
3400389d1b08SAneesh Kumar K.V }
3401389d1b08SAneesh Kumar K.V EXPORT_SYMBOL(bh_submit_read);
3402389d1b08SAneesh Kumar K.V 
34031da177e4SLinus Torvalds void __init buffer_init(void)
34041da177e4SLinus Torvalds {
340543be594aSZhang Yanfei 	unsigned long nrpages;
34061da177e4SLinus Torvalds 
3407b98938c3SChristoph Lameter 	bh_cachep = kmem_cache_create("buffer_head",
3408b98938c3SChristoph Lameter 			sizeof(struct buffer_head), 0,
3409b98938c3SChristoph Lameter 				(SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3410b98938c3SChristoph Lameter 				SLAB_MEM_SPREAD),
3411019b4d12SRichard Kennedy 				NULL);
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds 	/*
34141da177e4SLinus Torvalds 	 * Limit the bh occupancy to 10% of ZONE_NORMAL
34151da177e4SLinus Torvalds 	 */
34161da177e4SLinus Torvalds 	nrpages = (nr_free_buffer_pages() * 10) / 100;
34171da177e4SLinus Torvalds 	max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
34181da177e4SLinus Torvalds 	hotcpu_notifier(buffer_cpu_notify, 0);
34191da177e4SLinus Torvalds }
3420