blob: 9083e528e3c9a8d5876f230f70bc452ff9e5c70e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/buffer.c
3 *
4 * Copyright (C) 1991, 1992, 2002 Linus Torvalds
5 */
6
7/*
8 * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
9 *
10 * Removed a lot of unnecessary code and simplified things now that
11 * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
12 *
13 * Speed up hash, lru, and free list operations. Use gfp() for allocating
14 * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
15 *
16 * Added 32k buffer block sizes - these are required older ARM systems. - RMK
17 *
18 * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/syscalls.h>
23#include <linux/fs.h>
24#include <linux/mm.h>
25#include <linux/percpu.h>
26#include <linux/slab.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/blkdev.h>
29#include <linux/file.h>
30#include <linux/quotaops.h>
31#include <linux/highmem.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050032#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/writeback.h>
34#include <linux/hash.h>
35#include <linux/suspend.h>
36#include <linux/buffer_head.h>
Andrew Morton55e829a2006-12-10 02:19:27 -080037#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/bio.h>
39#include <linux/notifier.h>
40#include <linux/cpu.h>
41#include <linux/bitops.h>
42#include <linux/mpage.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070043#include <linux/bit_spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
48
Yan Honga3f3c292012-12-12 13:52:15 -080049void init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 bh->b_end_io = handler;
52 bh->b_private = private;
53}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070054EXPORT_SYMBOL(init_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jens Axboe7eaceac2011-03-10 08:52:07 +010056static int sleep_on_buffer(void *word)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 io_schedule();
59 return 0;
60}
61
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080062void __lock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Jens Axboe7eaceac2011-03-10 08:52:07 +010064 wait_on_bit_lock(&bh->b_state, BH_Lock, sleep_on_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 TASK_UNINTERRUPTIBLE);
66}
67EXPORT_SYMBOL(__lock_buffer);
68
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080069void unlock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Nick Piggin51b07fc2008-10-18 20:27:00 -070071 clear_bit_unlock(BH_Lock, &bh->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 smp_mb__after_clear_bit();
73 wake_up_bit(&bh->b_state, BH_Lock);
74}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070075EXPORT_SYMBOL(unlock_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Block until a buffer comes unlocked. This doesn't stop it
79 * from becoming locked again - you have to lock it yourself
80 * if you want to preserve its state.
81 */
82void __wait_on_buffer(struct buffer_head * bh)
83{
Jens Axboe7eaceac2011-03-10 08:52:07 +010084 wait_on_bit(&bh->b_state, BH_Lock, sleep_on_buffer, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -070086EXPORT_SYMBOL(__wait_on_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static void
89__clear_page_buffers(struct page *page)
90{
91 ClearPagePrivate(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070092 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 page_cache_release(page);
94}
95
Keith Mannthey08bafc02008-11-25 10:24:35 +010096
97static int quiet_error(struct buffer_head *bh)
98{
99 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
100 return 0;
101 return 1;
102}
103
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void buffer_io_error(struct buffer_head *bh)
106{
107 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
109 bdevname(bh->b_bdev, b),
110 (unsigned long long)bh->b_blocknr);
111}
112
113/*
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700114 * End-of-IO handler helper function which does not touch the bh after
115 * unlocking it.
116 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
117 * a race there is benign: unlock_buffer() only use the bh's address for
118 * hashing after unlocking the buffer, so it doesn't actually touch the bh
119 * itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700121static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 if (uptodate) {
124 set_buffer_uptodate(bh);
125 } else {
126 /* This happens, due to failed READA attempts. */
127 clear_buffer_uptodate(bh);
128 }
129 unlock_buffer(bh);
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700130}
131
132/*
133 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
134 * unlock the buffer. This is what ll_rw_block uses too.
135 */
136void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
137{
138 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 put_bh(bh);
140}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700141EXPORT_SYMBOL(end_buffer_read_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
144{
145 char b[BDEVNAME_SIZE];
146
147 if (uptodate) {
148 set_buffer_uptodate(bh);
149 } else {
Christoph Hellwig0edd55f2010-08-18 05:29:23 -0400150 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 buffer_io_error(bh);
152 printk(KERN_WARNING "lost page write due to "
153 "I/O error on %s\n",
154 bdevname(bh->b_bdev, b));
155 }
156 set_buffer_write_io_error(bh);
157 clear_buffer_uptodate(bh);
158 }
159 unlock_buffer(bh);
160 put_bh(bh);
161}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700162EXPORT_SYMBOL(end_buffer_write_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Various filesystems appear to want __find_get_block to be non-blocking.
166 * But it's the page lock which protects the buffers. To get around this,
167 * we get exclusion from try_to_free_buffers with the blockdev mapping's
168 * private_lock.
169 *
170 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
171 * may be quite high. This code could TryLock the page, and if that
172 * succeeds, there is no need to take private_lock. (But if
173 * private_lock is contended then so is mapping->tree_lock).
174 */
175static struct buffer_head *
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -0800176__find_get_block_slow(struct block_device *bdev, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct inode *bd_inode = bdev->bd_inode;
179 struct address_space *bd_mapping = bd_inode->i_mapping;
180 struct buffer_head *ret = NULL;
181 pgoff_t index;
182 struct buffer_head *bh;
183 struct buffer_head *head;
184 struct page *page;
185 int all_mapped = 1;
186
187 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
188 page = find_get_page(bd_mapping, index);
189 if (!page)
190 goto out;
191
192 spin_lock(&bd_mapping->private_lock);
193 if (!page_has_buffers(page))
194 goto out_unlock;
195 head = page_buffers(page);
196 bh = head;
197 do {
Nikanth Karthikesan97f76d32009-04-02 16:56:46 -0700198 if (!buffer_mapped(bh))
199 all_mapped = 0;
200 else if (bh->b_blocknr == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ret = bh;
202 get_bh(bh);
203 goto out_unlock;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 bh = bh->b_this_page;
206 } while (bh != head);
207
208 /* we might be here because some of the buffers on this page are
209 * not mapped. This is due to various races between
210 * file io on the block device and getblk. It gets dealt with
211 * elsewhere, don't buffer_error if we had some unmapped buffers
212 */
213 if (all_mapped) {
Tao Ma72a2ebd2011-10-31 17:09:00 -0700214 char b[BDEVNAME_SIZE];
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 printk("__find_get_block_slow() failed. "
217 "block=%llu, b_blocknr=%llu\n",
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800218 (unsigned long long)block,
219 (unsigned long long)bh->b_blocknr);
220 printk("b_state=0x%08lx, b_size=%zu\n",
221 bh->b_state, bh->b_size);
Tao Ma72a2ebd2011-10-31 17:09:00 -0700222 printk("device %s blocksize: %d\n", bdevname(bdev, b),
223 1 << bd_inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225out_unlock:
226 spin_unlock(&bd_mapping->private_lock);
227 page_cache_release(page);
228out:
229 return ret;
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
Jens Axboe5b0830c2009-09-23 19:37:09 +0200233 * Kick the writeback threads then try to free up some ZONE_NORMAL memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235static void free_more_memory(void)
236{
Mel Gorman19770b32008-04-28 02:12:18 -0700237 struct zone *zone;
Mel Gorman0e884602008-04-28 02:12:14 -0700238 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -0600240 wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 yield();
242
Mel Gorman0e884602008-04-28 02:12:14 -0700243 for_each_online_node(nid) {
Mel Gorman19770b32008-04-28 02:12:18 -0700244 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
245 gfp_zone(GFP_NOFS), NULL,
246 &zone);
247 if (zone)
Mel Gorman54a6eb52008-04-28 02:12:16 -0700248 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -0700249 GFP_NOFS, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251}
252
253/*
254 * I/O completion handler for block_read_full_page() - pages
255 * which come unlocked at the end of I/O.
256 */
257static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700260 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct buffer_head *tmp;
262 struct page *page;
263 int page_uptodate = 1;
264
265 BUG_ON(!buffer_async_read(bh));
266
267 page = bh->b_page;
268 if (uptodate) {
269 set_buffer_uptodate(bh);
270 } else {
271 clear_buffer_uptodate(bh);
Keith Mannthey08bafc02008-11-25 10:24:35 +0100272 if (!quiet_error(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 buffer_io_error(bh);
274 SetPageError(page);
275 }
276
277 /*
278 * Be _very_ careful from here on. Bad things can happen if
279 * two buffer heads end IO at almost the same time and both
280 * decide that the page is now completely done.
281 */
Nick Piggina3972202005-07-07 17:56:56 -0700282 first = page_buffers(page);
283 local_irq_save(flags);
284 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 clear_buffer_async_read(bh);
286 unlock_buffer(bh);
287 tmp = bh;
288 do {
289 if (!buffer_uptodate(tmp))
290 page_uptodate = 0;
291 if (buffer_async_read(tmp)) {
292 BUG_ON(!buffer_locked(tmp));
293 goto still_busy;
294 }
295 tmp = tmp->b_this_page;
296 } while (tmp != bh);
Nick Piggina3972202005-07-07 17:56:56 -0700297 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
298 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /*
301 * If none of the buffers had errors and they are all
302 * uptodate then we can set the page uptodate.
303 */
304 if (page_uptodate && !PageError(page))
305 SetPageUptodate(page);
306 unlock_page(page);
307 return;
308
309still_busy:
Nick Piggina3972202005-07-07 17:56:56 -0700310 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
311 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return;
313}
314
315/*
316 * Completion handler for block_write_full_page() - pages which are unlocked
317 * during I/O, and which have PageWriteback cleared upon I/O completion.
318 */
Chris Mason35c80d52009-04-15 13:22:38 -0400319void end_buffer_async_write(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700323 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct buffer_head *tmp;
325 struct page *page;
326
327 BUG_ON(!buffer_async_write(bh));
328
329 page = bh->b_page;
330 if (uptodate) {
331 set_buffer_uptodate(bh);
332 } else {
Keith Mannthey08bafc02008-11-25 10:24:35 +0100333 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 buffer_io_error(bh);
335 printk(KERN_WARNING "lost page write due to "
336 "I/O error on %s\n",
337 bdevname(bh->b_bdev, b));
338 }
339 set_bit(AS_EIO, &page->mapping->flags);
Jan Kara58ff4072006-10-17 00:10:19 -0700340 set_buffer_write_io_error(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 clear_buffer_uptodate(bh);
342 SetPageError(page);
343 }
344
Nick Piggina3972202005-07-07 17:56:56 -0700345 first = page_buffers(page);
346 local_irq_save(flags);
347 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 clear_buffer_async_write(bh);
350 unlock_buffer(bh);
351 tmp = bh->b_this_page;
352 while (tmp != bh) {
353 if (buffer_async_write(tmp)) {
354 BUG_ON(!buffer_locked(tmp));
355 goto still_busy;
356 }
357 tmp = tmp->b_this_page;
358 }
Nick Piggina3972202005-07-07 17:56:56 -0700359 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
360 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 end_page_writeback(page);
362 return;
363
364still_busy:
Nick Piggina3972202005-07-07 17:56:56 -0700365 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
366 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return;
368}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700369EXPORT_SYMBOL(end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/*
372 * If a page's buffers are under async readin (end_buffer_async_read
373 * completion) then there is a possibility that another thread of
374 * control could lock one of the buffers after it has completed
375 * but while some of the other buffers have not completed. This
376 * locked buffer would confuse end_buffer_async_read() into not unlocking
377 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
378 * that this buffer is not under async I/O.
379 *
380 * The page comes unlocked when it has no locked buffer_async buffers
381 * left.
382 *
383 * PageLocked prevents anyone starting new async I/O reads any of
384 * the buffers.
385 *
386 * PageWriteback is used to prevent simultaneous writeout of the same
387 * page.
388 *
389 * PageLocked prevents anyone from starting writeback of a page which is
390 * under read I/O (PageWriteback is only ever set against a locked page).
391 */
392static void mark_buffer_async_read(struct buffer_head *bh)
393{
394 bh->b_end_io = end_buffer_async_read;
395 set_buffer_async_read(bh);
396}
397
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700398static void mark_buffer_async_write_endio(struct buffer_head *bh,
399 bh_end_io_t *handler)
Chris Mason35c80d52009-04-15 13:22:38 -0400400{
401 bh->b_end_io = handler;
402 set_buffer_async_write(bh);
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405void mark_buffer_async_write(struct buffer_head *bh)
406{
Chris Mason35c80d52009-04-15 13:22:38 -0400407 mark_buffer_async_write_endio(bh, end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409EXPORT_SYMBOL(mark_buffer_async_write);
410
411
412/*
413 * fs/buffer.c contains helper functions for buffer-backed address space's
414 * fsync functions. A common requirement for buffer-based filesystems is
415 * that certain data from the backing blockdev needs to be written out for
416 * a successful fsync(). For example, ext2 indirect blocks need to be
417 * written back and waited upon before fsync() returns.
418 *
419 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
420 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
421 * management of a list of dependent buffers at ->i_mapping->private_list.
422 *
423 * Locking is a little subtle: try_to_free_buffers() will remove buffers
424 * from their controlling inode's queue when they are being freed. But
425 * try_to_free_buffers() will be operating against the *blockdev* mapping
426 * at the time, not against the S_ISREG file which depends on those buffers.
427 * So the locking for private_list is via the private_lock in the address_space
428 * which backs the buffers. Which is different from the address_space
429 * against which the buffers are listed. So for a particular address_space,
430 * mapping->private_lock does *not* protect mapping->private_list! In fact,
431 * mapping->private_list will always be protected by the backing blockdev's
432 * ->private_lock.
433 *
434 * Which introduces a requirement: all buffers on an address_space's
435 * ->private_list must be from the same address_space: the blockdev's.
436 *
437 * address_spaces which do not place buffers at ->private_list via these
438 * utility functions are free to use private_lock and private_list for
439 * whatever they want. The only requirement is that list_empty(private_list)
440 * be true at clear_inode() time.
441 *
442 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
443 * filesystems should do that. invalidate_inode_buffers() should just go
444 * BUG_ON(!list_empty).
445 *
446 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
447 * take an address_space, not an inode. And it should be called
448 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
449 * queued up.
450 *
451 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
452 * list if it is already on a list. Because if the buffer is on a list,
453 * it *must* already be on the right one. If not, the filesystem is being
454 * silly. This will save a ton of locking. But first we have to ensure
455 * that buffers are taken *off* the old inode's list when they are freed
456 * (presumably in truncate). That requires careful auditing of all
457 * filesystems (do it inside bforget()). It could also be done by bringing
458 * b_inode back.
459 */
460
461/*
462 * The buffer's backing address_space's private_lock must be held
463 */
Thomas Petazzonidbacefc2008-07-29 22:33:47 -0700464static void __remove_assoc_queue(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -0700467 WARN_ON(!bh->b_assoc_map);
468 if (buffer_write_io_error(bh))
469 set_bit(AS_EIO, &bh->b_assoc_map->flags);
470 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473int inode_has_buffers(struct inode *inode)
474{
475 return !list_empty(&inode->i_data.private_list);
476}
477
478/*
479 * osync is designed to support O_SYNC io. It waits synchronously for
480 * all already-submitted IO to complete, but does not queue any new
481 * writes to the disk.
482 *
483 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
484 * you dirty the buffers, and then use osync_inode_buffers to wait for
485 * completion. Any other dirty buffers which are not yet queued for
486 * write will not be flushed to disk by the osync.
487 */
488static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
489{
490 struct buffer_head *bh;
491 struct list_head *p;
492 int err = 0;
493
494 spin_lock(lock);
495repeat:
496 list_for_each_prev(p, list) {
497 bh = BH_ENTRY(p);
498 if (buffer_locked(bh)) {
499 get_bh(bh);
500 spin_unlock(lock);
501 wait_on_buffer(bh);
502 if (!buffer_uptodate(bh))
503 err = -EIO;
504 brelse(bh);
505 spin_lock(lock);
506 goto repeat;
507 }
508 }
509 spin_unlock(lock);
510 return err;
511}
512
Al Viro01a05b32010-03-23 06:06:58 -0400513static void do_thaw_one(struct super_block *sb, void *unused)
514{
515 char b[BDEVNAME_SIZE];
516 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
517 printk(KERN_WARNING "Emergency Thaw on %s\n",
518 bdevname(sb->s_bdev, b));
519}
520
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -0700521static void do_thaw_all(struct work_struct *work)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700522{
Al Viro01a05b32010-03-23 06:06:58 -0400523 iterate_supers(do_thaw_one, NULL);
Jens Axboe053c5252009-04-08 13:44:08 +0200524 kfree(work);
Eric Sandeenc2d75432009-03-31 15:23:46 -0700525 printk(KERN_WARNING "Emergency Thaw complete\n");
526}
527
528/**
529 * emergency_thaw_all -- forcibly thaw every frozen filesystem
530 *
531 * Used for emergency unfreeze of all filesystems via SysRq
532 */
533void emergency_thaw_all(void)
534{
Jens Axboe053c5252009-04-08 13:44:08 +0200535 struct work_struct *work;
536
537 work = kmalloc(sizeof(*work), GFP_ATOMIC);
538 if (work) {
539 INIT_WORK(work, do_thaw_all);
540 schedule_work(work);
541 }
Eric Sandeenc2d75432009-03-31 15:23:46 -0700542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800545 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
Martin Waitz67be2dd2005-05-01 08:59:26 -0700546 * @mapping: the mapping which wants those buffers written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
548 * Starts I/O against the buffers at mapping->private_list, and waits upon
549 * that I/O.
550 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700551 * Basically, this is a convenience function for fsync().
552 * @mapping is a file or directory which needs those buffers to be written for
553 * a successful fsync().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555int sync_mapping_buffers(struct address_space *mapping)
556{
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800557 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
560 return 0;
561
562 return fsync_buffers_list(&buffer_mapping->private_lock,
563 &mapping->private_list);
564}
565EXPORT_SYMBOL(sync_mapping_buffers);
566
567/*
568 * Called when we've recently written block `bblock', and it is known that
569 * `bblock' was for a buffer_boundary() buffer. This means that the block at
570 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
571 * dirty, schedule it for IO. So that indirects merge nicely with their data.
572 */
573void write_boundary_block(struct block_device *bdev,
574 sector_t bblock, unsigned blocksize)
575{
576 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
577 if (bh) {
578 if (buffer_dirty(bh))
579 ll_rw_block(WRITE, 1, &bh);
580 put_bh(bh);
581 }
582}
583
584void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
585{
586 struct address_space *mapping = inode->i_mapping;
587 struct address_space *buffer_mapping = bh->b_page->mapping;
588
589 mark_buffer_dirty(bh);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800590 if (!mapping->private_data) {
591 mapping->private_data = buffer_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 } else {
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800593 BUG_ON(mapping->private_data != buffer_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Jan Kara535ee2f2008-02-08 04:21:59 -0800595 if (!bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_lock(&buffer_mapping->private_lock);
597 list_move_tail(&bh->b_assoc_buffers,
598 &mapping->private_list);
Jan Kara58ff4072006-10-17 00:10:19 -0700599 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_unlock(&buffer_mapping->private_lock);
601 }
602}
603EXPORT_SYMBOL(mark_buffer_dirty_inode);
604
605/*
Nick Piggin787d2212007-07-17 04:03:34 -0700606 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
607 * dirty.
608 *
609 * If warn is true, then emit a warning if the page is not uptodate and has
610 * not been truncated.
611 */
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700612static void __set_page_dirty(struct page *page,
Nick Piggin787d2212007-07-17 04:03:34 -0700613 struct address_space *mapping, int warn)
614{
Nick Piggin19fd6232008-07-25 19:45:32 -0700615 spin_lock_irq(&mapping->tree_lock);
Nick Piggin787d2212007-07-17 04:03:34 -0700616 if (page->mapping) { /* Race with truncate? */
617 WARN_ON_ONCE(warn && !PageUptodate(page));
Edward Shishkine3a7cca2009-03-31 15:19:39 -0700618 account_page_dirtied(page, mapping);
Nick Piggin787d2212007-07-17 04:03:34 -0700619 radix_tree_tag_set(&mapping->page_tree,
620 page_index(page), PAGECACHE_TAG_DIRTY);
621 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700622 spin_unlock_irq(&mapping->tree_lock);
Nick Piggin787d2212007-07-17 04:03:34 -0700623 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Nick Piggin787d2212007-07-17 04:03:34 -0700624}
625
626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * Add a page to the dirty page list.
628 *
629 * It is a sad fact of life that this function is called from several places
630 * deeply under spinlocking. It may not sleep.
631 *
632 * If the page has buffers, the uptodate buffers are set dirty, to preserve
633 * dirty-state coherency between the page and the buffers. It the page does
634 * not have buffers then when they are later attached they will all be set
635 * dirty.
636 *
637 * The buffers are dirtied before the page is dirtied. There's a small race
638 * window in which a writepage caller may see the page cleanness but not the
639 * buffer dirtiness. That's fine. If this code were to set the page dirty
640 * before the buffers, a concurrent writepage caller could clear the page dirty
641 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
642 * page on the dirty page list.
643 *
644 * We use private_lock to lock against try_to_free_buffers while using the
645 * page's buffer list. Also use this to protect against clean buffers being
646 * added to the page after it was set dirty.
647 *
648 * FIXME: may need to call ->reservepage here as well. That's rather up to the
649 * address_space though.
650 */
651int __set_page_dirty_buffers(struct page *page)
652{
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700653 int newly_dirty;
Nick Piggin787d2212007-07-17 04:03:34 -0700654 struct address_space *mapping = page_mapping(page);
Nick Pigginebf7a222006-10-10 04:36:54 +0200655
656 if (unlikely(!mapping))
657 return !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 spin_lock(&mapping->private_lock);
660 if (page_has_buffers(page)) {
661 struct buffer_head *head = page_buffers(page);
662 struct buffer_head *bh = head;
663
664 do {
665 set_buffer_dirty(bh);
666 bh = bh->b_this_page;
667 } while (bh != head);
668 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700669 newly_dirty = !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 spin_unlock(&mapping->private_lock);
671
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700672 if (newly_dirty)
673 __set_page_dirty(page, mapping, 1);
674 return newly_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676EXPORT_SYMBOL(__set_page_dirty_buffers);
677
678/*
679 * Write out and wait upon a list of buffers.
680 *
681 * We have conflicting pressures: we want to make sure that all
682 * initially dirty buffers get waited on, but that any subsequently
683 * dirtied buffers don't. After all, we don't want fsync to last
684 * forever if somebody is actively writing to the file.
685 *
686 * Do this in two main stages: first we copy dirty buffers to a
687 * temporary inode list, queueing the writes as we go. Then we clean
688 * up, waiting for those writes to complete.
689 *
690 * During this second stage, any subsequent updates to the file may end
691 * up refiling the buffer on the original inode's dirty list again, so
692 * there is a chance we will end up with a buffer queued for write but
693 * not yet completed on that list. So, as a final cleanup we go through
694 * the osync code to catch these locked, dirty buffers without requeuing
695 * any newly dirty buffers for write.
696 */
697static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
698{
699 struct buffer_head *bh;
700 struct list_head tmp;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100701 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int err = 0, err2;
Jens Axboe4ee24912011-03-17 10:51:40 +0100703 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 INIT_LIST_HEAD(&tmp);
Jens Axboe4ee24912011-03-17 10:51:40 +0100706 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 spin_lock(lock);
709 while (!list_empty(list)) {
710 bh = BH_ENTRY(list->next);
Jan Kara535ee2f2008-02-08 04:21:59 -0800711 mapping = bh->b_assoc_map;
Jan Kara58ff4072006-10-17 00:10:19 -0700712 __remove_assoc_queue(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800713 /* Avoid race with mark_buffer_dirty_inode() which does
714 * a lockless check and we rely on seeing the dirty bit */
715 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (buffer_dirty(bh) || buffer_locked(bh)) {
717 list_add(&bh->b_assoc_buffers, &tmp);
Jan Kara535ee2f2008-02-08 04:21:59 -0800718 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (buffer_dirty(bh)) {
720 get_bh(bh);
721 spin_unlock(lock);
722 /*
723 * Ensure any pending I/O completes so that
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200724 * write_dirty_buffer() actually writes the
725 * current contents - it is a noop if I/O is
726 * still in flight on potentially older
727 * contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Jens Axboe721a9602011-03-09 11:56:30 +0100729 write_dirty_buffer(bh, WRITE_SYNC);
Jens Axboe9cf6b722009-04-06 14:48:03 +0200730
731 /*
732 * Kick off IO for the previous mapping. Note
733 * that we will not run the very last mapping,
734 * wait_on_buffer() will do that for us
735 * through sync_buffer().
736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 brelse(bh);
738 spin_lock(lock);
739 }
740 }
741 }
742
Jens Axboe4ee24912011-03-17 10:51:40 +0100743 spin_unlock(lock);
744 blk_finish_plug(&plug);
745 spin_lock(lock);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 while (!list_empty(&tmp)) {
748 bh = BH_ENTRY(tmp.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 get_bh(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800750 mapping = bh->b_assoc_map;
751 __remove_assoc_queue(bh);
752 /* Avoid race with mark_buffer_dirty_inode() which does
753 * a lockless check and we rely on seeing the dirty bit */
754 smp_mb();
755 if (buffer_dirty(bh)) {
756 list_add(&bh->b_assoc_buffers,
Jan Karae3892292008-03-04 14:28:33 -0800757 &mapping->private_list);
Jan Kara535ee2f2008-02-08 04:21:59 -0800758 bh->b_assoc_map = mapping;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 spin_unlock(lock);
761 wait_on_buffer(bh);
762 if (!buffer_uptodate(bh))
763 err = -EIO;
764 brelse(bh);
765 spin_lock(lock);
766 }
767
768 spin_unlock(lock);
769 err2 = osync_buffers_list(lock, list);
770 if (err)
771 return err;
772 else
773 return err2;
774}
775
776/*
777 * Invalidate any and all dirty buffers on a given inode. We are
778 * probably unmounting the fs, but that doesn't mean we have already
779 * done a sync(). Just drop the buffers from the inode list.
780 *
781 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
782 * assumes that all the buffers are against the blockdev. Not true
783 * for reiserfs.
784 */
785void invalidate_inode_buffers(struct inode *inode)
786{
787 if (inode_has_buffers(inode)) {
788 struct address_space *mapping = &inode->i_data;
789 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800790 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 spin_lock(&buffer_mapping->private_lock);
793 while (!list_empty(list))
794 __remove_assoc_queue(BH_ENTRY(list->next));
795 spin_unlock(&buffer_mapping->private_lock);
796 }
797}
Jan Kara52b19ac2008-09-23 18:24:08 +0200798EXPORT_SYMBOL(invalidate_inode_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800/*
801 * Remove any clean buffers from the inode's buffer list. This is called
802 * when we're trying to free the inode itself. Those buffers can pin it.
803 *
804 * Returns true if all buffers were removed.
805 */
806int remove_inode_buffers(struct inode *inode)
807{
808 int ret = 1;
809
810 if (inode_has_buffers(inode)) {
811 struct address_space *mapping = &inode->i_data;
812 struct list_head *list = &mapping->private_list;
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800813 struct address_space *buffer_mapping = mapping->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 spin_lock(&buffer_mapping->private_lock);
816 while (!list_empty(list)) {
817 struct buffer_head *bh = BH_ENTRY(list->next);
818 if (buffer_dirty(bh)) {
819 ret = 0;
820 break;
821 }
822 __remove_assoc_queue(bh);
823 }
824 spin_unlock(&buffer_mapping->private_lock);
825 }
826 return ret;
827}
828
829/*
830 * Create the appropriate buffers when given a page for data area and
831 * the size of each buffer.. Use the bh->b_this_page linked list to
832 * follow the buffers created. Return NULL if unable to create more
833 * buffers.
834 *
835 * The retry flag is used to differentiate async IO (paging, swapping)
836 * which may not fail from ordinary buffer allocations.
837 */
838struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
839 int retry)
840{
841 struct buffer_head *bh, *head;
842 long offset;
843
844try_again:
845 head = NULL;
846 offset = PAGE_SIZE;
847 while ((offset -= size) >= 0) {
848 bh = alloc_buffer_head(GFP_NOFS);
849 if (!bh)
850 goto no_grow;
851
852 bh->b_bdev = NULL;
853 bh->b_this_page = head;
854 bh->b_blocknr = -1;
855 head = bh;
856
857 bh->b_state = 0;
858 atomic_set(&bh->b_count, 0);
859 bh->b_size = size;
860
861 /* Link the buffer to its page */
862 set_bh_page(bh, page, offset);
863
Nathan Scott01ffe332006-01-17 09:02:07 +1100864 init_buffer(bh, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 return head;
867/*
868 * In case anything failed, we just free everything we got.
869 */
870no_grow:
871 if (head) {
872 do {
873 bh = head;
874 head = head->b_this_page;
875 free_buffer_head(bh);
876 } while (head);
877 }
878
879 /*
880 * Return failure for non-async IO requests. Async IO requests
881 * are not allowed to fail, so we have to wait until buffer heads
882 * become available. But we don't want tasks sleeping with
883 * partially complete buffers, so all were released above.
884 */
885 if (!retry)
886 return NULL;
887
888 /* We're _really_ low on memory. Now we just
889 * wait for old buffer heads to become free due to
890 * finishing IO. Since this is an async request and
891 * the reserve list is empty, we're sure there are
892 * async buffer heads in use.
893 */
894 free_more_memory();
895 goto try_again;
896}
897EXPORT_SYMBOL_GPL(alloc_page_buffers);
898
899static inline void
900link_dev_buffers(struct page *page, struct buffer_head *head)
901{
902 struct buffer_head *bh, *tail;
903
904 bh = head;
905 do {
906 tail = bh;
907 bh = bh->b_this_page;
908 } while (bh);
909 tail->b_this_page = head;
910 attach_page_buffers(page, head);
911}
912
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800913static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
914{
915 sector_t retval = ~((sector_t)0);
916 loff_t sz = i_size_read(bdev->bd_inode);
917
918 if (sz) {
919 unsigned int sizebits = blksize_bits(size);
920 retval = (sz >> sizebits);
921 }
922 return retval;
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Initialise the state of a blockdev page's buffers.
927 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200928static sector_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929init_page_buffers(struct page *page, struct block_device *bdev,
930 sector_t block, int size)
931{
932 struct buffer_head *head = page_buffers(page);
933 struct buffer_head *bh = head;
934 int uptodate = PageUptodate(page);
Linus Torvaldsbbec02702012-11-29 12:31:52 -0800935 sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 do {
938 if (!buffer_mapped(bh)) {
939 init_buffer(bh, NULL, NULL);
940 bh->b_bdev = bdev;
941 bh->b_blocknr = block;
942 if (uptodate)
943 set_buffer_uptodate(bh);
Jeff Moyer080399a2012-05-11 16:34:10 +0200944 if (block < end_block)
945 set_buffer_mapped(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 block++;
948 bh = bh->b_this_page;
949 } while (bh != head);
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200950
951 /*
952 * Caller needs to validate requested block against end of device.
953 */
954 return end_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * Create the page-cache page that contains the requested block.
959 *
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200960 * This is used purely for blockdev mappings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200962static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963grow_dev_page(struct block_device *bdev, sector_t block,
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200964 pgoff_t index, int size, int sizebits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct inode *inode = bdev->bd_inode;
967 struct page *page;
968 struct buffer_head *bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200969 sector_t end_block;
970 int ret = 0; /* Will call free_more_memory() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Christoph Lameterea125892007-05-16 22:11:21 -0700972 page = find_or_create_page(inode->i_mapping, index,
Mel Gorman769848c2007-07-17 04:03:05 -0700973 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!page)
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200975 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Eric Sesterhenne827f922006-03-26 18:24:46 +0200977 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (page_has_buffers(page)) {
980 bh = page_buffers(page);
981 if (bh->b_size == size) {
Hugh Dickins676ce6d2012-08-23 12:17:36 +0200982 end_block = init_page_buffers(page, bdev,
983 index << sizebits, size);
984 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 if (!try_to_free_buffers(page))
987 goto failed;
988 }
989
990 /*
991 * Allocate some buffers for this page
992 */
993 bh = alloc_page_buffers(page, size, 0);
994 if (!bh)
995 goto failed;
996
997 /*
998 * Link the page to the buffers and initialise them. Take the
999 * lock to be atomic wrt __find_get_block(), which does not
1000 * run under the page lock.
1001 */
1002 spin_lock(&inode->i_mapping->private_lock);
1003 link_dev_buffers(page, bh);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001004 end_block = init_page_buffers(page, bdev, index << sizebits, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock(&inode->i_mapping->private_lock);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001006done:
1007 ret = (block < end_block) ? 1 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unlock_page(page);
1010 page_cache_release(page);
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001011 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
1015 * Create buffers for the specified block device block's page. If
1016 * that page was dirty, the buffers are set dirty also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001018static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019grow_buffers(struct block_device *bdev, sector_t block, int size)
1020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 pgoff_t index;
1022 int sizebits;
1023
1024 sizebits = -1;
1025 do {
1026 sizebits++;
1027 } while ((size << sizebits) < PAGE_SIZE);
1028
1029 index = block >> sizebits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Andrew Mortone5657932006-10-11 01:21:46 -07001031 /*
1032 * Check for a block which wants to lie outside our maximum possible
1033 * pagecache index. (this comparison is done using sector_t types).
1034 */
1035 if (unlikely(index != block >> sizebits)) {
1036 char b[BDEVNAME_SIZE];
1037
1038 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1039 "device %s\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001040 __func__, (unsigned long long)block,
Andrew Mortone5657932006-10-11 01:21:46 -07001041 bdevname(bdev, b));
1042 return -EIO;
1043 }
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* Create a page with the proper size buffers.. */
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001046 return grow_dev_page(bdev, block, index, size, sizebits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Adrian Bunk75c96f82005-05-05 16:16:09 -07001049static struct buffer_head *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050__getblk_slow(struct block_device *bdev, sector_t block, int size)
1051{
1052 /* Size must be multiple of hard sectorsize */
Martin K. Petersene1defc42009-05-22 17:17:49 -04001053 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 (size < 512 || size > PAGE_SIZE))) {
1055 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1056 size);
Martin K. Petersene1defc42009-05-22 17:17:49 -04001057 printk(KERN_ERR "logical block size: %d\n",
1058 bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 dump_stack();
1061 return NULL;
1062 }
1063
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001064 for (;;) {
1065 struct buffer_head *bh;
1066 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 bh = __find_get_block(bdev, block, size);
1069 if (bh)
1070 return bh;
Hugh Dickins676ce6d2012-08-23 12:17:36 +02001071
1072 ret = grow_buffers(bdev, block, size);
1073 if (ret < 0)
1074 return NULL;
1075 if (ret == 0)
1076 free_more_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078}
1079
1080/*
1081 * The relationship between dirty buffers and dirty pages:
1082 *
1083 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1084 * the page is tagged dirty in its radix tree.
1085 *
1086 * At all times, the dirtiness of the buffers represents the dirtiness of
1087 * subsections of the page. If the page has buffers, the page dirty bit is
1088 * merely a hint about the true dirty state.
1089 *
1090 * When a page is set dirty in its entirety, all its buffers are marked dirty
1091 * (if the page has buffers).
1092 *
1093 * When a buffer is marked dirty, its page is dirtied, but the page's other
1094 * buffers are not.
1095 *
1096 * Also. When blockdev buffers are explicitly read with bread(), they
1097 * individually become uptodate. But their backing page remains not
1098 * uptodate - even if all of its buffers are uptodate. A subsequent
1099 * block_read_full_page() against that page will discover all the uptodate
1100 * buffers, will set the page uptodate and will perform no I/O.
1101 */
1102
1103/**
1104 * mark_buffer_dirty - mark a buffer_head as needing writeout
Martin Waitz67be2dd2005-05-01 08:59:26 -07001105 * @bh: the buffer_head to mark dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 *
1107 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1108 * backing page dirty, then tag the page as dirty in its address_space's radix
1109 * tree and then attach the address_space's inode to its superblock's dirty
1110 * inode list.
1111 *
1112 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
Dave Chinner250df6e2011-03-22 22:23:36 +11001113 * mapping->tree_lock and mapping->host->i_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001115void mark_buffer_dirty(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Nick Piggin787d2212007-07-17 04:03:34 -07001117 WARN_ON_ONCE(!buffer_uptodate(bh));
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001118
1119 /*
1120 * Very *carefully* optimize the it-is-already-dirty case.
1121 *
1122 * Don't let the final "is it dirty" escape to before we
1123 * perhaps modified the buffer.
1124 */
1125 if (buffer_dirty(bh)) {
1126 smp_mb();
1127 if (buffer_dirty(bh))
1128 return;
1129 }
1130
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001131 if (!test_set_buffer_dirty(bh)) {
1132 struct page *page = bh->b_page;
Linus Torvalds8e9d78e2009-08-21 17:40:08 -07001133 if (!TestSetPageDirty(page)) {
1134 struct address_space *mapping = page_mapping(page);
1135 if (mapping)
1136 __set_page_dirty(page, mapping, 0);
1137 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001140EXPORT_SYMBOL(mark_buffer_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142/*
1143 * Decrement a buffer_head's reference count. If all buffers against a page
1144 * have zero reference count, are clean and unlocked, and if the page is clean
1145 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1146 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1147 * a page but it ends up not being freed, and buffers may later be reattached).
1148 */
1149void __brelse(struct buffer_head * buf)
1150{
1151 if (atomic_read(&buf->b_count)) {
1152 put_bh(buf);
1153 return;
1154 }
Arjan van de Ven5c752ad2008-07-25 19:45:40 -07001155 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001157EXPORT_SYMBOL(__brelse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159/*
1160 * bforget() is like brelse(), except it discards any
1161 * potentially dirty data.
1162 */
1163void __bforget(struct buffer_head *bh)
1164{
1165 clear_buffer_dirty(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -08001166 if (bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct address_space *buffer_mapping = bh->b_page->mapping;
1168
1169 spin_lock(&buffer_mapping->private_lock);
1170 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -07001171 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 spin_unlock(&buffer_mapping->private_lock);
1173 }
1174 __brelse(bh);
1175}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07001176EXPORT_SYMBOL(__bforget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178static struct buffer_head *__bread_slow(struct buffer_head *bh)
1179{
1180 lock_buffer(bh);
1181 if (buffer_uptodate(bh)) {
1182 unlock_buffer(bh);
1183 return bh;
1184 } else {
1185 get_bh(bh);
1186 bh->b_end_io = end_buffer_read_sync;
1187 submit_bh(READ, bh);
1188 wait_on_buffer(bh);
1189 if (buffer_uptodate(bh))
1190 return bh;
1191 }
1192 brelse(bh);
1193 return NULL;
1194}
1195
1196/*
1197 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1198 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1199 * refcount elevated by one when they're in an LRU. A buffer can only appear
1200 * once in a particular CPU's LRU. A single buffer can be present in multiple
1201 * CPU's LRUs at the same time.
1202 *
1203 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1204 * sb_find_get_block().
1205 *
1206 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1207 * a local interrupt disable for that.
1208 */
1209
1210#define BH_LRU_SIZE 8
1211
1212struct bh_lru {
1213 struct buffer_head *bhs[BH_LRU_SIZE];
1214};
1215
1216static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1217
1218#ifdef CONFIG_SMP
1219#define bh_lru_lock() local_irq_disable()
1220#define bh_lru_unlock() local_irq_enable()
1221#else
1222#define bh_lru_lock() preempt_disable()
1223#define bh_lru_unlock() preempt_enable()
1224#endif
1225
1226static inline void check_irqs_on(void)
1227{
1228#ifdef irqs_disabled
1229 BUG_ON(irqs_disabled());
1230#endif
1231}
1232
1233/*
1234 * The LRU management algorithm is dopey-but-simple. Sorry.
1235 */
1236static void bh_lru_install(struct buffer_head *bh)
1237{
1238 struct buffer_head *evictee = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 check_irqs_on();
1241 bh_lru_lock();
Christoph Lameterc7b92512010-12-06 11:16:28 -06001242 if (__this_cpu_read(bh_lrus.bhs[0]) != bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct buffer_head *bhs[BH_LRU_SIZE];
1244 int in;
1245 int out = 0;
1246
1247 get_bh(bh);
1248 bhs[out++] = bh;
1249 for (in = 0; in < BH_LRU_SIZE; in++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001250 struct buffer_head *bh2 =
1251 __this_cpu_read(bh_lrus.bhs[in]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (bh2 == bh) {
1254 __brelse(bh2);
1255 } else {
1256 if (out >= BH_LRU_SIZE) {
1257 BUG_ON(evictee != NULL);
1258 evictee = bh2;
1259 } else {
1260 bhs[out++] = bh2;
1261 }
1262 }
1263 }
1264 while (out < BH_LRU_SIZE)
1265 bhs[out++] = NULL;
Christoph Lameterc7b92512010-12-06 11:16:28 -06001266 memcpy(__this_cpu_ptr(&bh_lrus.bhs), bhs, sizeof(bhs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 bh_lru_unlock();
1269
1270 if (evictee)
1271 __brelse(evictee);
1272}
1273
1274/*
1275 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1276 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001277static struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001278lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct buffer_head *ret = NULL;
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001281 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 check_irqs_on();
1284 bh_lru_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 for (i = 0; i < BH_LRU_SIZE; i++) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001286 struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (bh && bh->b_bdev == bdev &&
1289 bh->b_blocknr == block && bh->b_size == size) {
1290 if (i) {
1291 while (i) {
Christoph Lameterc7b92512010-12-06 11:16:28 -06001292 __this_cpu_write(bh_lrus.bhs[i],
1293 __this_cpu_read(bh_lrus.bhs[i - 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 i--;
1295 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06001296 __this_cpu_write(bh_lrus.bhs[0], bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 get_bh(bh);
1299 ret = bh;
1300 break;
1301 }
1302 }
1303 bh_lru_unlock();
1304 return ret;
1305}
1306
1307/*
1308 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1309 * it in the LRU and mark it as accessed. If it is not present then return
1310 * NULL
1311 */
1312struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001313__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1316
1317 if (bh == NULL) {
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001318 bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (bh)
1320 bh_lru_install(bh);
1321 }
1322 if (bh)
1323 touch_buffer(bh);
1324 return bh;
1325}
1326EXPORT_SYMBOL(__find_get_block);
1327
1328/*
1329 * __getblk will locate (and, if necessary, create) the buffer_head
1330 * which corresponds to the passed block_device, block and size. The
1331 * returned buffer has its reference count incremented.
1332 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1334 * attempt is failing. FIXME, perhaps?
1335 */
1336struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001337__getblk(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct buffer_head *bh = __find_get_block(bdev, block, size);
1340
1341 might_sleep();
1342 if (bh == NULL)
1343 bh = __getblk_slow(bdev, block, size);
1344 return bh;
1345}
1346EXPORT_SYMBOL(__getblk);
1347
1348/*
1349 * Do async read-ahead on a buffer..
1350 */
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001351void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct buffer_head *bh = __getblk(bdev, block, size);
Andrew Mortona3e713b2005-10-30 15:03:15 -08001354 if (likely(bh)) {
1355 ll_rw_block(READA, 1, &bh);
1356 brelse(bh);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359EXPORT_SYMBOL(__breadahead);
1360
1361/**
1362 * __bread() - reads a specified block and returns the bh
Martin Waitz67be2dd2005-05-01 08:59:26 -07001363 * @bdev: the block_device to read from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * @block: number of block
1365 * @size: size (in bytes) to read
1366 *
1367 * Reads a specified block, and returns buffer head that contains it.
1368 * It returns NULL if the block was unreadable.
1369 */
1370struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001371__bread(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct buffer_head *bh = __getblk(bdev, block, size);
1374
Andrew Mortona3e713b2005-10-30 15:03:15 -08001375 if (likely(bh) && !buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 bh = __bread_slow(bh);
1377 return bh;
1378}
1379EXPORT_SYMBOL(__bread);
1380
1381/*
1382 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1383 * This doesn't race because it runs in each cpu either in irq
1384 * or with preempt disabled.
1385 */
1386static void invalidate_bh_lru(void *arg)
1387{
1388 struct bh_lru *b = &get_cpu_var(bh_lrus);
1389 int i;
1390
1391 for (i = 0; i < BH_LRU_SIZE; i++) {
1392 brelse(b->bhs[i]);
1393 b->bhs[i] = NULL;
1394 }
1395 put_cpu_var(bh_lrus);
1396}
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001397
1398static bool has_bh_in_lru(int cpu, void *dummy)
1399{
1400 struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
1401 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001403 for (i = 0; i < BH_LRU_SIZE; i++) {
1404 if (b->bhs[i])
1405 return 1;
1406 }
1407
1408 return 0;
1409}
1410
Peter Zijlstraf9a14392007-05-06 14:49:55 -07001411void invalidate_bh_lrus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Gilad Ben-Yossef42be35d2012-03-28 14:42:45 -07001413 on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
Nick Piggin9db55792008-02-08 04:19:49 -08001415EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417void set_bh_page(struct buffer_head *bh,
1418 struct page *page, unsigned long offset)
1419{
1420 bh->b_page = page;
Eric Sesterhenne827f922006-03-26 18:24:46 +02001421 BUG_ON(offset >= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (PageHighMem(page))
1423 /*
1424 * This catches illegal uses and preserves the offset:
1425 */
1426 bh->b_data = (char *)(0 + offset);
1427 else
1428 bh->b_data = page_address(page) + offset;
1429}
1430EXPORT_SYMBOL(set_bh_page);
1431
1432/*
1433 * Called when truncating a buffer on a page completely.
1434 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001435static void discard_buffer(struct buffer_head * bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 lock_buffer(bh);
1438 clear_buffer_dirty(bh);
1439 bh->b_bdev = NULL;
1440 clear_buffer_mapped(bh);
1441 clear_buffer_req(bh);
1442 clear_buffer_new(bh);
1443 clear_buffer_delay(bh);
David Chinner33a266d2007-02-12 00:51:41 -08001444 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 unlock_buffer(bh);
1446}
1447
1448/**
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001449 * block_invalidatepage - invalidate part or all of a buffer-backed page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 *
1451 * @page: the page which is affected
1452 * @offset: the index of the truncation point
1453 *
1454 * block_invalidatepage() is called when all or part of the page has become
Wang Sheng-Hui814e1d22011-09-01 08:22:57 +08001455 * invalidated by a truncate operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 *
1457 * block_invalidatepage() does not have to release all buffers, but it must
1458 * ensure that no dirty buffer is left outside @offset and that no I/O
1459 * is underway against any of the blocks which are outside the truncation
1460 * point. Because the caller is about to free (and possibly reuse) those
1461 * blocks on-disk.
1462 */
NeilBrown2ff28e22006-03-26 01:37:18 -08001463void block_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct buffer_head *head, *bh, *next;
1466 unsigned int curr_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 BUG_ON(!PageLocked(page));
1469 if (!page_has_buffers(page))
1470 goto out;
1471
1472 head = page_buffers(page);
1473 bh = head;
1474 do {
1475 unsigned int next_off = curr_off + bh->b_size;
1476 next = bh->b_this_page;
1477
1478 /*
1479 * is this block fully invalidated?
1480 */
1481 if (offset <= curr_off)
1482 discard_buffer(bh);
1483 curr_off = next_off;
1484 bh = next;
1485 } while (bh != head);
1486
1487 /*
1488 * We release buffers only if the entire page is being invalidated.
1489 * The get_block cached value has been unconditionally invalidated,
1490 * so real IO is not possible anymore.
1491 */
1492 if (offset == 0)
NeilBrown2ff28e22006-03-26 01:37:18 -08001493 try_to_release_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494out:
NeilBrown2ff28e22006-03-26 01:37:18 -08001495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497EXPORT_SYMBOL(block_invalidatepage);
1498
1499/*
1500 * We attach and possibly dirty the buffers atomically wrt
1501 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1502 * is already excluded via the page lock.
1503 */
1504void create_empty_buffers(struct page *page,
1505 unsigned long blocksize, unsigned long b_state)
1506{
1507 struct buffer_head *bh, *head, *tail;
1508
1509 head = alloc_page_buffers(page, blocksize, 1);
1510 bh = head;
1511 do {
1512 bh->b_state |= b_state;
1513 tail = bh;
1514 bh = bh->b_this_page;
1515 } while (bh);
1516 tail->b_this_page = head;
1517
1518 spin_lock(&page->mapping->private_lock);
1519 if (PageUptodate(page) || PageDirty(page)) {
1520 bh = head;
1521 do {
1522 if (PageDirty(page))
1523 set_buffer_dirty(bh);
1524 if (PageUptodate(page))
1525 set_buffer_uptodate(bh);
1526 bh = bh->b_this_page;
1527 } while (bh != head);
1528 }
1529 attach_page_buffers(page, head);
1530 spin_unlock(&page->mapping->private_lock);
1531}
1532EXPORT_SYMBOL(create_empty_buffers);
1533
1534/*
1535 * We are taking a block for data and we don't want any output from any
1536 * buffer-cache aliases starting from return from that function and
1537 * until the moment when something will explicitly mark the buffer
1538 * dirty (hopefully that will not happen until we will free that block ;-)
1539 * We don't even need to mark it not-uptodate - nobody can expect
1540 * anything from a newly allocated buffer anyway. We used to used
1541 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1542 * don't want to mark the alias unmapped, for example - it would confuse
1543 * anyone who might pick it with bread() afterwards...
1544 *
1545 * Also.. Note that bforget() doesn't lock the buffer. So there can
1546 * be writeout I/O going on against recently-freed buffers. We don't
1547 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1548 * only if we really need to. That happens here.
1549 */
1550void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1551{
1552 struct buffer_head *old_bh;
1553
1554 might_sleep();
1555
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001556 old_bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (old_bh) {
1558 clear_buffer_dirty(old_bh);
1559 wait_on_buffer(old_bh);
1560 clear_buffer_req(old_bh);
1561 __brelse(old_bh);
1562 }
1563}
1564EXPORT_SYMBOL(unmap_underlying_metadata);
1565
1566/*
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001567 * Size is a power-of-two in the range 512..PAGE_SIZE,
1568 * and the case we care about most is PAGE_SIZE.
1569 *
1570 * So this *could* possibly be written with those
1571 * constraints in mind (relevant mostly if some
1572 * architecture has a slow bit-scan instruction)
1573 */
1574static inline int block_size_bits(unsigned int blocksize)
1575{
1576 return ilog2(blocksize);
1577}
1578
1579static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
1580{
1581 BUG_ON(!PageLocked(page));
1582
1583 if (!page_has_buffers(page))
1584 create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
1585 return page_buffers(page);
1586}
1587
1588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 * NOTE! All mapped/uptodate combinations are valid:
1590 *
1591 * Mapped Uptodate Meaning
1592 *
1593 * No No "unknown" - must do get_block()
1594 * No Yes "hole" - zero-filled
1595 * Yes No "allocated" - allocated on disk, not read in
1596 * Yes Yes "valid" - allocated and up-to-date in memory.
1597 *
1598 * "Dirty" is valid only with the last case (mapped+uptodate).
1599 */
1600
1601/*
1602 * While block_write_full_page is writing back the dirty buffers under
1603 * the page lock, whoever dirtied the buffers may decide to clean them
1604 * again at any time. We handle that by only looking at the buffer
1605 * state inside lock_buffer().
1606 *
1607 * If block_write_full_page() is called for regular writeback
1608 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1609 * locked buffer. This only can happen if someone has written the buffer
1610 * directly, with submit_bh(). At the address_space level PageWriteback
1611 * prevents this contention from occurring.
Theodore Ts'o6e34eedd2009-04-07 18:12:43 -04001612 *
1613 * If block_write_full_page() is called with wbc->sync_mode ==
Jens Axboe721a9602011-03-09 11:56:30 +01001614 * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
1615 * causes the writes to be flagged as synchronous writes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
1617static int __block_write_full_page(struct inode *inode, struct page *page,
Chris Mason35c80d52009-04-15 13:22:38 -04001618 get_block_t *get_block, struct writeback_control *wbc,
1619 bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 int err;
1622 sector_t block;
1623 sector_t last_block;
Andrew Mortonf0fbd5f2005-05-05 16:15:48 -07001624 struct buffer_head *bh, *head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001625 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int nr_underway = 0;
Theodore Ts'o6e34eedd2009-04-07 18:12:43 -04001627 int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
Jens Axboe721a9602011-03-09 11:56:30 +01001628 WRITE_SYNC : WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001630 head = create_page_buffers(page, inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 (1 << BH_Dirty)|(1 << BH_Uptodate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1635 * here, and the (potentially unmapped) buffers may become dirty at
1636 * any time. If a buffer becomes dirty here after we've inspected it
1637 * then we just miss that fact, and the page stays dirty.
1638 *
1639 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1640 * handle that here by just cleaning them.
1641 */
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 bh = head;
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001644 blocksize = bh->b_size;
1645 bbits = block_size_bits(blocksize);
1646
1647 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1648 last_block = (i_size_read(inode) - 1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /*
1651 * Get all the dirty buffers mapped to disk addresses and
1652 * handle any aliases from the underlying blockdev's mapping.
1653 */
1654 do {
1655 if (block > last_block) {
1656 /*
1657 * mapped buffers outside i_size will occur, because
1658 * this page can be outside i_size when there is a
1659 * truncate in progress.
1660 */
1661 /*
1662 * The buffer was zeroed by block_write_full_page()
1663 */
1664 clear_buffer_dirty(bh);
1665 set_buffer_uptodate(bh);
Alex Tomas29a814d2008-07-11 19:27:31 -04001666 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1667 buffer_dirty(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001668 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 err = get_block(inode, block, bh, 1);
1670 if (err)
1671 goto recover;
Alex Tomas29a814d2008-07-11 19:27:31 -04001672 clear_buffer_delay(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (buffer_new(bh)) {
1674 /* blockdev mappings never come here */
1675 clear_buffer_new(bh);
1676 unmap_underlying_metadata(bh->b_bdev,
1677 bh->b_blocknr);
1678 }
1679 }
1680 bh = bh->b_this_page;
1681 block++;
1682 } while (bh != head);
1683
1684 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (!buffer_mapped(bh))
1686 continue;
1687 /*
1688 * If it's a fully non-blocking write attempt and we cannot
1689 * lock the buffer then redirty the page. Note that this can
Jens Axboe5b0830c2009-09-23 19:37:09 +02001690 * potentially cause a busy-wait loop from writeback threads
1691 * and kswapd activity, but those code paths have their own
1692 * higher-level throttling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
Wu Fengguang1b430be2010-10-26 14:21:26 -07001694 if (wbc->sync_mode != WB_SYNC_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 lock_buffer(bh);
Nick Pigginca5de402008-08-02 12:02:13 +02001696 } else if (!trylock_buffer(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 redirty_page_for_writepage(wbc, page);
1698 continue;
1699 }
1700 if (test_clear_buffer_dirty(bh)) {
Chris Mason35c80d52009-04-15 13:22:38 -04001701 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 } else {
1703 unlock_buffer(bh);
1704 }
1705 } while ((bh = bh->b_this_page) != head);
1706
1707 /*
1708 * The page and its buffers are protected by PageWriteback(), so we can
1709 * drop the bh refcounts early.
1710 */
1711 BUG_ON(PageWriteback(page));
1712 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 do {
1715 struct buffer_head *next = bh->b_this_page;
1716 if (buffer_async_write(bh)) {
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001717 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 nr_underway++;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 bh = next;
1721 } while (bh != head);
Andrew Morton05937ba2005-05-05 16:15:47 -07001722 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 err = 0;
1725done:
1726 if (nr_underway == 0) {
1727 /*
1728 * The page was marked dirty, but the buffers were
1729 * clean. Someone wrote them back by hand with
1730 * ll_rw_block/submit_bh. A rare case.
1731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 end_page_writeback(page);
Nick Piggin3d67f2d2007-05-06 14:49:05 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * The page and buffer_heads can be released at any time from
1736 * here on.
1737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 return err;
1740
1741recover:
1742 /*
1743 * ENOSPC, or some other error. We may already have added some
1744 * blocks to the file, so we need to write these out to avoid
1745 * exposing stale data.
1746 * The page is currently locked and not marked for writeback
1747 */
1748 bh = head;
1749 /* Recovery: lock and submit the mapped buffers */
1750 do {
Alex Tomas29a814d2008-07-11 19:27:31 -04001751 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1752 !buffer_delay(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 lock_buffer(bh);
Chris Mason35c80d52009-04-15 13:22:38 -04001754 mark_buffer_async_write_endio(bh, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 } else {
1756 /*
1757 * The buffer may have been set dirty during
1758 * attachment to a dirty page.
1759 */
1760 clear_buffer_dirty(bh);
1761 }
1762 } while ((bh = bh->b_this_page) != head);
1763 SetPageError(page);
1764 BUG_ON(PageWriteback(page));
Andrew Morton7e4c3692007-05-08 00:23:27 -07001765 mapping_set_error(page->mapping, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 do {
1768 struct buffer_head *next = bh->b_this_page;
1769 if (buffer_async_write(bh)) {
1770 clear_buffer_dirty(bh);
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001771 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 nr_underway++;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 bh = next;
1775 } while (bh != head);
Nick Pigginffda9d32007-02-20 13:57:54 -08001776 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto done;
1778}
1779
Nick Pigginafddba42007-10-16 01:25:01 -07001780/*
1781 * If a page has any new buffers, zero them out here, and mark them uptodate
1782 * and dirty so they'll be written out (in order to prevent uninitialised
1783 * block data from leaking). And clear the new bit.
1784 */
1785void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1786{
1787 unsigned int block_start, block_end;
1788 struct buffer_head *head, *bh;
1789
1790 BUG_ON(!PageLocked(page));
1791 if (!page_has_buffers(page))
1792 return;
1793
1794 bh = head = page_buffers(page);
1795 block_start = 0;
1796 do {
1797 block_end = block_start + bh->b_size;
1798
1799 if (buffer_new(bh)) {
1800 if (block_end > from && block_start < to) {
1801 if (!PageUptodate(page)) {
1802 unsigned start, size;
1803
1804 start = max(from, block_start);
1805 size = min(to, block_end) - start;
1806
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001807 zero_user(page, start, size);
Nick Pigginafddba42007-10-16 01:25:01 -07001808 set_buffer_uptodate(bh);
1809 }
1810
1811 clear_buffer_new(bh);
1812 mark_buffer_dirty(bh);
1813 }
1814 }
1815
1816 block_start = block_end;
1817 bh = bh->b_this_page;
1818 } while (bh != head);
1819}
1820EXPORT_SYMBOL(page_zero_new_buffers);
1821
Christoph Hellwigebdec242010-10-06 10:47:23 +02001822int __block_write_begin(struct page *page, loff_t pos, unsigned len,
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001823 get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Christoph Hellwigebdec242010-10-06 10:47:23 +02001825 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1826 unsigned to = from + len;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001827 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 unsigned block_start, block_end;
1829 sector_t block;
1830 int err = 0;
1831 unsigned blocksize, bbits;
1832 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1833
1834 BUG_ON(!PageLocked(page));
1835 BUG_ON(from > PAGE_CACHE_SIZE);
1836 BUG_ON(to > PAGE_CACHE_SIZE);
1837 BUG_ON(from > to);
1838
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001839 head = create_page_buffers(page, inode, 0);
1840 blocksize = head->b_size;
1841 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1844
1845 for(bh = head, block_start = 0; bh != head || !block_start;
1846 block++, block_start=block_end, bh = bh->b_this_page) {
1847 block_end = block_start + blocksize;
1848 if (block_end <= from || block_start >= to) {
1849 if (PageUptodate(page)) {
1850 if (!buffer_uptodate(bh))
1851 set_buffer_uptodate(bh);
1852 }
1853 continue;
1854 }
1855 if (buffer_new(bh))
1856 clear_buffer_new(bh);
1857 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001858 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 err = get_block(inode, block, bh, 1);
1860 if (err)
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (buffer_new(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unmap_underlying_metadata(bh->b_bdev,
1864 bh->b_blocknr);
1865 if (PageUptodate(page)) {
Nick Piggin637aff42007-10-16 01:25:00 -07001866 clear_buffer_new(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 set_buffer_uptodate(bh);
Nick Piggin637aff42007-10-16 01:25:00 -07001868 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 continue;
1870 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001871 if (block_end > to || block_start < from)
1872 zero_user_segments(page,
1873 to, block_end,
1874 block_start, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 continue;
1876 }
1877 }
1878 if (PageUptodate(page)) {
1879 if (!buffer_uptodate(bh))
1880 set_buffer_uptodate(bh);
1881 continue;
1882 }
1883 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
David Chinner33a266d2007-02-12 00:51:41 -08001884 !buffer_unwritten(bh) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 (block_start < from || block_end > to)) {
1886 ll_rw_block(READ, 1, &bh);
1887 *wait_bh++=bh;
1888 }
1889 }
1890 /*
1891 * If we issued read requests - let them complete.
1892 */
1893 while(wait_bh > wait) {
1894 wait_on_buffer(*--wait_bh);
1895 if (!buffer_uptodate(*wait_bh))
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001896 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Jan Karaf9f07b62011-06-14 00:58:27 +02001898 if (unlikely(err))
Nick Pigginafddba42007-10-16 01:25:01 -07001899 page_zero_new_buffers(page, from, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return err;
1901}
Christoph Hellwigebdec242010-10-06 10:47:23 +02001902EXPORT_SYMBOL(__block_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904static int __block_commit_write(struct inode *inode, struct page *page,
1905 unsigned from, unsigned to)
1906{
1907 unsigned block_start, block_end;
1908 int partial = 0;
1909 unsigned blocksize;
1910 struct buffer_head *bh, *head;
1911
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001912 bh = head = page_buffers(page);
1913 blocksize = bh->b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001915 block_start = 0;
1916 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 block_end = block_start + blocksize;
1918 if (block_end <= from || block_start >= to) {
1919 if (!buffer_uptodate(bh))
1920 partial = 1;
1921 } else {
1922 set_buffer_uptodate(bh);
1923 mark_buffer_dirty(bh);
1924 }
Nick Pigginafddba42007-10-16 01:25:01 -07001925 clear_buffer_new(bh);
Linus Torvalds45bce8f2012-11-29 10:21:43 -08001926
1927 block_start = block_end;
1928 bh = bh->b_this_page;
1929 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /*
1932 * If this is a partial write which happened to make all buffers
1933 * uptodate then we can optimize away a bogus readpage() for
1934 * the next read(). Here we 'discover' whether the page went
1935 * uptodate as a result of this (potentially partial) write.
1936 */
1937 if (!partial)
1938 SetPageUptodate(page);
1939 return 0;
1940}
1941
1942/*
Christoph Hellwig155130a2010-06-04 11:29:58 +02001943 * block_write_begin takes care of the basic task of block allocation and
1944 * bringing partial write blocks uptodate first.
1945 *
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10001946 * The filesystem needs to handle block truncation upon failure.
Nick Pigginafddba42007-10-16 01:25:01 -07001947 */
Christoph Hellwig155130a2010-06-04 11:29:58 +02001948int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
1949 unsigned flags, struct page **pagep, get_block_t *get_block)
Nick Pigginafddba42007-10-16 01:25:01 -07001950{
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001951 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -07001952 struct page *page;
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001953 int status;
Nick Pigginafddba42007-10-16 01:25:01 -07001954
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001955 page = grab_cache_page_write_begin(mapping, index, flags);
1956 if (!page)
1957 return -ENOMEM;
Nick Pigginafddba42007-10-16 01:25:01 -07001958
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001959 status = __block_write_begin(page, pos, len, get_block);
Nick Pigginafddba42007-10-16 01:25:01 -07001960 if (unlikely(status)) {
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001961 unlock_page(page);
1962 page_cache_release(page);
1963 page = NULL;
Nick Pigginafddba42007-10-16 01:25:01 -07001964 }
1965
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001966 *pagep = page;
Nick Pigginafddba42007-10-16 01:25:01 -07001967 return status;
1968}
1969EXPORT_SYMBOL(block_write_begin);
1970
1971int block_write_end(struct file *file, struct address_space *mapping,
1972 loff_t pos, unsigned len, unsigned copied,
1973 struct page *page, void *fsdata)
1974{
1975 struct inode *inode = mapping->host;
1976 unsigned start;
1977
1978 start = pos & (PAGE_CACHE_SIZE - 1);
1979
1980 if (unlikely(copied < len)) {
1981 /*
1982 * The buffers that were written will now be uptodate, so we
1983 * don't have to worry about a readpage reading them and
1984 * overwriting a partial write. However if we have encountered
1985 * a short write and only partially written into a buffer, it
1986 * will not be marked uptodate, so a readpage might come in and
1987 * destroy our partial write.
1988 *
1989 * Do the simplest thing, and just treat any short write to a
1990 * non uptodate page as a zero-length write, and force the
1991 * caller to redo the whole thing.
1992 */
1993 if (!PageUptodate(page))
1994 copied = 0;
1995
1996 page_zero_new_buffers(page, start+copied, start+len);
1997 }
1998 flush_dcache_page(page);
1999
2000 /* This could be a short (even 0-length) commit */
2001 __block_commit_write(inode, page, start, start+copied);
2002
2003 return copied;
2004}
2005EXPORT_SYMBOL(block_write_end);
2006
2007int generic_write_end(struct file *file, struct address_space *mapping,
2008 loff_t pos, unsigned len, unsigned copied,
2009 struct page *page, void *fsdata)
2010{
2011 struct inode *inode = mapping->host;
Jan Karac7d206b2008-07-11 19:27:31 -04002012 int i_size_changed = 0;
Nick Pigginafddba42007-10-16 01:25:01 -07002013
2014 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2015
2016 /*
2017 * No need to use i_size_read() here, the i_size
2018 * cannot change under us because we hold i_mutex.
2019 *
2020 * But it's important to update i_size while still holding page lock:
2021 * page writeout could otherwise come in and zero beyond i_size.
2022 */
2023 if (pos+copied > inode->i_size) {
2024 i_size_write(inode, pos+copied);
Jan Karac7d206b2008-07-11 19:27:31 -04002025 i_size_changed = 1;
Nick Pigginafddba42007-10-16 01:25:01 -07002026 }
2027
2028 unlock_page(page);
2029 page_cache_release(page);
2030
Jan Karac7d206b2008-07-11 19:27:31 -04002031 /*
2032 * Don't mark the inode dirty under page lock. First, it unnecessarily
2033 * makes the holding time of page lock longer. Second, it forces lock
2034 * ordering of page lock and transaction start for journaling
2035 * filesystems.
2036 */
2037 if (i_size_changed)
2038 mark_inode_dirty(inode);
2039
Nick Pigginafddba42007-10-16 01:25:01 -07002040 return copied;
2041}
2042EXPORT_SYMBOL(generic_write_end);
2043
2044/*
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002045 * block_is_partially_uptodate checks whether buffers within a page are
2046 * uptodate or not.
2047 *
2048 * Returns true if all buffers which correspond to a file portion
2049 * we want to read are uptodate.
2050 */
2051int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2052 unsigned long from)
2053{
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002054 unsigned block_start, block_end, blocksize;
2055 unsigned to;
2056 struct buffer_head *bh, *head;
2057 int ret = 1;
2058
2059 if (!page_has_buffers(page))
2060 return 0;
2061
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002062 head = page_buffers(page);
2063 blocksize = head->b_size;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002064 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2065 to = from + to;
2066 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2067 return 0;
2068
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002069 bh = head;
2070 block_start = 0;
2071 do {
2072 block_end = block_start + blocksize;
2073 if (block_end > from && block_start < to) {
2074 if (!buffer_uptodate(bh)) {
2075 ret = 0;
2076 break;
2077 }
2078 if (block_end >= to)
2079 break;
2080 }
2081 block_start = block_end;
2082 bh = bh->b_this_page;
2083 } while (bh != head);
2084
2085 return ret;
2086}
2087EXPORT_SYMBOL(block_is_partially_uptodate);
2088
2089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 * Generic "read page" function for block devices that have the normal
2091 * get_block functionality. This is most of the block device filesystems.
2092 * Reads the page asynchronously --- the unlock_buffer() and
2093 * set/clear_buffer_uptodate() functions propagate buffer state into the
2094 * page struct once IO has completed.
2095 */
2096int block_read_full_page(struct page *page, get_block_t *get_block)
2097{
2098 struct inode *inode = page->mapping->host;
2099 sector_t iblock, lblock;
2100 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002101 unsigned int blocksize, bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int nr, i;
2103 int fully_mapped = 1;
2104
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002105 head = create_page_buffers(page, inode, 0);
2106 blocksize = head->b_size;
2107 bbits = block_size_bits(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Linus Torvalds45bce8f2012-11-29 10:21:43 -08002109 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
2110 lblock = (i_size_read(inode)+blocksize-1) >> bbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 bh = head;
2112 nr = 0;
2113 i = 0;
2114
2115 do {
2116 if (buffer_uptodate(bh))
2117 continue;
2118
2119 if (!buffer_mapped(bh)) {
Andrew Mortonc64610b2005-05-16 21:53:49 -07002120 int err = 0;
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 fully_mapped = 0;
2123 if (iblock < lblock) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002124 WARN_ON(bh->b_size != blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002125 err = get_block(inode, iblock, bh, 0);
2126 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 SetPageError(page);
2128 }
2129 if (!buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002130 zero_user(page, i * blocksize, blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002131 if (!err)
2132 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 continue;
2134 }
2135 /*
2136 * get_block() might have updated the buffer
2137 * synchronously
2138 */
2139 if (buffer_uptodate(bh))
2140 continue;
2141 }
2142 arr[nr++] = bh;
2143 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2144
2145 if (fully_mapped)
2146 SetPageMappedToDisk(page);
2147
2148 if (!nr) {
2149 /*
2150 * All buffers are uptodate - we can set the page uptodate
2151 * as well. But not if get_block() returned an error.
2152 */
2153 if (!PageError(page))
2154 SetPageUptodate(page);
2155 unlock_page(page);
2156 return 0;
2157 }
2158
2159 /* Stage two: lock the buffers */
2160 for (i = 0; i < nr; i++) {
2161 bh = arr[i];
2162 lock_buffer(bh);
2163 mark_buffer_async_read(bh);
2164 }
2165
2166 /*
2167 * Stage 3: start the IO. Check for uptodateness
2168 * inside the buffer lock in case another process reading
2169 * the underlying blockdev brought it uptodate (the sct fix).
2170 */
2171 for (i = 0; i < nr; i++) {
2172 bh = arr[i];
2173 if (buffer_uptodate(bh))
2174 end_buffer_async_read(bh, 1);
2175 else
2176 submit_bh(READ, bh);
2177 }
2178 return 0;
2179}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002180EXPORT_SYMBOL(block_read_full_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182/* utility function for filesystems that need to do work on expanding
Nick Piggin89e10782007-10-16 01:25:07 -07002183 * truncates. Uses filesystem pagecache writes to allow the filesystem to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * deal with the hole.
2185 */
Nick Piggin89e10782007-10-16 01:25:07 -07002186int generic_cont_expand_simple(struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct address_space *mapping = inode->i_mapping;
2189 struct page *page;
Nick Piggin89e10782007-10-16 01:25:07 -07002190 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 int err;
2192
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10002193 err = inode_newsize_ok(inode, size);
2194 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto out;
2196
Nick Piggin89e10782007-10-16 01:25:07 -07002197 err = pagecache_write_begin(NULL, mapping, size, 0,
2198 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2199 &page, &fsdata);
2200 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto out;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002202
Nick Piggin89e10782007-10-16 01:25:07 -07002203 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2204 BUG_ON(err > 0);
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206out:
2207 return err;
2208}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002209EXPORT_SYMBOL(generic_cont_expand_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Adrian Bunkf1e3af72008-04-29 00:59:01 -07002211static int cont_expand_zero(struct file *file, struct address_space *mapping,
2212 loff_t pos, loff_t *bytes)
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002213{
Nick Piggin89e10782007-10-16 01:25:07 -07002214 struct inode *inode = mapping->host;
2215 unsigned blocksize = 1 << inode->i_blkbits;
2216 struct page *page;
2217 void *fsdata;
2218 pgoff_t index, curidx;
2219 loff_t curpos;
2220 unsigned zerofrom, offset, len;
2221 int err = 0;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002222
Nick Piggin89e10782007-10-16 01:25:07 -07002223 index = pos >> PAGE_CACHE_SHIFT;
2224 offset = pos & ~PAGE_CACHE_MASK;
2225
2226 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2227 zerofrom = curpos & ~PAGE_CACHE_MASK;
2228 if (zerofrom & (blocksize-1)) {
2229 *bytes |= (blocksize-1);
2230 (*bytes)++;
2231 }
2232 len = PAGE_CACHE_SIZE - zerofrom;
2233
2234 err = pagecache_write_begin(file, mapping, curpos, len,
2235 AOP_FLAG_UNINTERRUPTIBLE,
2236 &page, &fsdata);
2237 if (err)
2238 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002239 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002240 err = pagecache_write_end(file, mapping, curpos, len, len,
2241 page, fsdata);
2242 if (err < 0)
2243 goto out;
2244 BUG_ON(err != len);
2245 err = 0;
OGAWA Hirofumi061e9742008-04-28 02:16:28 -07002246
2247 balance_dirty_pages_ratelimited(mapping);
Nick Piggin89e10782007-10-16 01:25:07 -07002248 }
2249
2250 /* page covers the boundary, find the boundary offset */
2251 if (index == curidx) {
2252 zerofrom = curpos & ~PAGE_CACHE_MASK;
2253 /* if we will expand the thing last block will be filled */
2254 if (offset <= zerofrom) {
2255 goto out;
2256 }
2257 if (zerofrom & (blocksize-1)) {
2258 *bytes |= (blocksize-1);
2259 (*bytes)++;
2260 }
2261 len = offset - zerofrom;
2262
2263 err = pagecache_write_begin(file, mapping, curpos, len,
2264 AOP_FLAG_UNINTERRUPTIBLE,
2265 &page, &fsdata);
2266 if (err)
2267 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002268 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002269 err = pagecache_write_end(file, mapping, curpos, len, len,
2270 page, fsdata);
2271 if (err < 0)
2272 goto out;
2273 BUG_ON(err != len);
2274 err = 0;
2275 }
2276out:
2277 return err;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/*
2281 * For moronic filesystems that do not allow holes in file.
2282 * We may have to extend the file.
2283 */
Christoph Hellwig282dc172010-06-04 11:29:55 +02002284int cont_write_begin(struct file *file, struct address_space *mapping,
Nick Piggin89e10782007-10-16 01:25:07 -07002285 loff_t pos, unsigned len, unsigned flags,
2286 struct page **pagep, void **fsdata,
2287 get_block_t *get_block, loff_t *bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 unsigned blocksize = 1 << inode->i_blkbits;
Nick Piggin89e10782007-10-16 01:25:07 -07002291 unsigned zerofrom;
2292 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Nick Piggin89e10782007-10-16 01:25:07 -07002294 err = cont_expand_zero(file, mapping, pos, bytes);
2295 if (err)
Christoph Hellwig155130a2010-06-04 11:29:58 +02002296 return err;
Nick Piggin89e10782007-10-16 01:25:07 -07002297
2298 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2299 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2300 *bytes |= (blocksize-1);
2301 (*bytes)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
Christoph Hellwig155130a2010-06-04 11:29:58 +02002304 return block_write_begin(mapping, pos, len, flags, pagep, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002306EXPORT_SYMBOL(cont_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308int block_commit_write(struct page *page, unsigned from, unsigned to)
2309{
2310 struct inode *inode = page->mapping->host;
2311 __block_commit_write(inode,page,from,to);
2312 return 0;
2313}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002314EXPORT_SYMBOL(block_commit_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
David Chinner54171692007-07-19 17:39:55 +10002316/*
2317 * block_page_mkwrite() is not allowed to change the file size as it gets
2318 * called from a page fault handler when a page is first dirtied. Hence we must
2319 * be careful to check for EOF conditions here. We set the page up correctly
2320 * for a written page which means we get ENOSPC checking when writing into
2321 * holes and correct delalloc and unwritten extent mapping on filesystems that
2322 * support these features.
2323 *
2324 * We are not allowed to take the i_mutex here so we have to play games to
2325 * protect against truncate races as the page could now be beyond EOF. Because
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002326 * truncate writes the inode size before removing pages, once we have the
David Chinner54171692007-07-19 17:39:55 +10002327 * page lock we can determine safely if the page is beyond EOF. If it is not
2328 * beyond EOF, then the page is guaranteed safe against truncation until we
2329 * unlock the page.
Jan Karaea13a862011-05-24 00:23:35 +02002330 *
Jan Kara14da9202012-06-12 16:20:37 +02002331 * Direct callers of this function should protect against filesystem freezing
2332 * using sb_start_write() - sb_end_write() functions.
David Chinner54171692007-07-19 17:39:55 +10002333 */
Jan Kara24da4fa2011-05-24 00:23:34 +02002334int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2335 get_block_t get_block)
David Chinner54171692007-07-19 17:39:55 +10002336{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002337 struct page *page = vmf->page;
David Chinner54171692007-07-19 17:39:55 +10002338 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2339 unsigned long end;
2340 loff_t size;
Jan Kara24da4fa2011-05-24 00:23:34 +02002341 int ret;
David Chinner54171692007-07-19 17:39:55 +10002342
2343 lock_page(page);
2344 size = i_size_read(inode);
2345 if ((page->mapping != inode->i_mapping) ||
Nick Piggin18336332007-07-20 00:31:45 -07002346 (page_offset(page) > size)) {
Jan Kara24da4fa2011-05-24 00:23:34 +02002347 /* We overload EFAULT to mean page got truncated */
2348 ret = -EFAULT;
2349 goto out_unlock;
David Chinner54171692007-07-19 17:39:55 +10002350 }
2351
2352 /* page is wholly or partially inside EOF */
2353 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2354 end = size & ~PAGE_CACHE_MASK;
2355 else
2356 end = PAGE_CACHE_SIZE;
2357
Christoph Hellwigebdec242010-10-06 10:47:23 +02002358 ret = __block_write_begin(page, 0, end, get_block);
David Chinner54171692007-07-19 17:39:55 +10002359 if (!ret)
2360 ret = block_commit_write(page, 0, end);
2361
Jan Kara24da4fa2011-05-24 00:23:34 +02002362 if (unlikely(ret < 0))
2363 goto out_unlock;
Jan Karaea13a862011-05-24 00:23:35 +02002364 set_page_dirty(page);
Darrick J. Wongd76ee182011-05-27 12:23:41 -07002365 wait_on_page_writeback(page);
Jan Kara24da4fa2011-05-24 00:23:34 +02002366 return 0;
2367out_unlock:
2368 unlock_page(page);
David Chinner54171692007-07-19 17:39:55 +10002369 return ret;
2370}
Jan Kara24da4fa2011-05-24 00:23:34 +02002371EXPORT_SYMBOL(__block_page_mkwrite);
2372
2373int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2374 get_block_t get_block)
2375{
Jan Karaea13a862011-05-24 00:23:35 +02002376 int ret;
2377 struct super_block *sb = vma->vm_file->f_path.dentry->d_inode->i_sb;
Jan Kara24da4fa2011-05-24 00:23:34 +02002378
Jan Kara14da9202012-06-12 16:20:37 +02002379 sb_start_pagefault(sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04002380
2381 /*
2382 * Update file times before taking page lock. We may end up failing the
2383 * fault so this update may be superfluous but who really cares...
2384 */
2385 file_update_time(vma->vm_file);
2386
Jan Karaea13a862011-05-24 00:23:35 +02002387 ret = __block_page_mkwrite(vma, vmf, get_block);
Jan Kara14da9202012-06-12 16:20:37 +02002388 sb_end_pagefault(sb);
Jan Kara24da4fa2011-05-24 00:23:34 +02002389 return block_page_mkwrite_return(ret);
2390}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002391EXPORT_SYMBOL(block_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002394 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 * immediately, while under the page lock. So it needs a special end_io
2396 * handler which does not touch the bh after unlocking it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 */
2398static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2399{
Dmitry Monakhov68671f32007-10-16 01:24:47 -07002400 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002404 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2405 * the page (converting it to circular linked list and taking care of page
2406 * dirty races).
2407 */
2408static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2409{
2410 struct buffer_head *bh;
2411
2412 BUG_ON(!PageLocked(page));
2413
2414 spin_lock(&page->mapping->private_lock);
2415 bh = head;
2416 do {
2417 if (PageDirty(page))
2418 set_buffer_dirty(bh);
2419 if (!bh->b_this_page)
2420 bh->b_this_page = head;
2421 bh = bh->b_this_page;
2422 } while (bh != head);
2423 attach_page_buffers(page, head);
2424 spin_unlock(&page->mapping->private_lock);
2425}
2426
2427/*
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002428 * On entry, the page is fully not uptodate.
2429 * On exit the page is fully uptodate in the areas outside (from,to)
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002430 * The filesystem needs to handle block truncation upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 */
Christoph Hellwigea0f04e2010-06-04 11:29:54 +02002432int nobh_write_begin(struct address_space *mapping,
Nick Piggin03158cd2007-10-16 01:25:25 -07002433 loff_t pos, unsigned len, unsigned flags,
2434 struct page **pagep, void **fsdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 get_block_t *get_block)
2436{
Nick Piggin03158cd2007-10-16 01:25:25 -07002437 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 const unsigned blkbits = inode->i_blkbits;
2439 const unsigned blocksize = 1 << blkbits;
Nick Piggina4b06722007-10-16 01:24:48 -07002440 struct buffer_head *head, *bh;
Nick Piggin03158cd2007-10-16 01:25:25 -07002441 struct page *page;
2442 pgoff_t index;
2443 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 unsigned block_in_page;
Nick Piggina4b06722007-10-16 01:24:48 -07002445 unsigned block_start, block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 sector_t block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 int nr_reads = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 int ret = 0;
2449 int is_mapped_to_disk = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Nick Piggin03158cd2007-10-16 01:25:25 -07002451 index = pos >> PAGE_CACHE_SHIFT;
2452 from = pos & (PAGE_CACHE_SIZE - 1);
2453 to = from + len;
2454
Nick Piggin54566b22009-01-04 12:00:53 -08002455 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin03158cd2007-10-16 01:25:25 -07002456 if (!page)
2457 return -ENOMEM;
2458 *pagep = page;
2459 *fsdata = NULL;
2460
2461 if (page_has_buffers(page)) {
Namhyung Kim309f77a2010-10-25 15:01:12 +09002462 ret = __block_write_begin(page, pos, len, get_block);
2463 if (unlikely(ret))
2464 goto out_release;
2465 return ret;
Nick Piggin03158cd2007-10-16 01:25:25 -07002466 }
Nick Piggina4b06722007-10-16 01:24:48 -07002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (PageMappedToDisk(page))
2469 return 0;
2470
Nick Piggina4b06722007-10-16 01:24:48 -07002471 /*
2472 * Allocate buffers so that we can keep track of state, and potentially
2473 * attach them to the page if an error occurs. In the common case of
2474 * no error, they will just be freed again without ever being attached
2475 * to the page (which is all OK, because we're under the page lock).
2476 *
2477 * Be careful: the buffer linked list is a NULL terminated one, rather
2478 * than the circular one we're used to.
2479 */
2480 head = alloc_page_buffers(page, blocksize, 0);
Nick Piggin03158cd2007-10-16 01:25:25 -07002481 if (!head) {
2482 ret = -ENOMEM;
2483 goto out_release;
2484 }
Nick Piggina4b06722007-10-16 01:24:48 -07002485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 /*
2489 * We loop across all blocks in the page, whether or not they are
2490 * part of the affected region. This is so we can discover if the
2491 * page is fully mapped-to-disk.
2492 */
Nick Piggina4b06722007-10-16 01:24:48 -07002493 for (block_start = 0, block_in_page = 0, bh = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 block_start < PAGE_CACHE_SIZE;
Nick Piggina4b06722007-10-16 01:24:48 -07002495 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 int create;
2497
Nick Piggina4b06722007-10-16 01:24:48 -07002498 block_end = block_start + blocksize;
2499 bh->b_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 create = 1;
2501 if (block_start >= to)
2502 create = 0;
2503 ret = get_block(inode, block_in_file + block_in_page,
Nick Piggina4b06722007-10-16 01:24:48 -07002504 bh, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (ret)
2506 goto failed;
Nick Piggina4b06722007-10-16 01:24:48 -07002507 if (!buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 is_mapped_to_disk = 0;
Nick Piggina4b06722007-10-16 01:24:48 -07002509 if (buffer_new(bh))
2510 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2511 if (PageUptodate(page)) {
2512 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 continue;
Nick Piggina4b06722007-10-16 01:24:48 -07002514 }
2515 if (buffer_new(bh) || !buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002516 zero_user_segments(page, block_start, from,
2517 to, block_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 continue;
2519 }
Nick Piggina4b06722007-10-16 01:24:48 -07002520 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 continue; /* reiserfs does this */
2522 if (block_start < from || block_end > to) {
Nick Piggina4b06722007-10-16 01:24:48 -07002523 lock_buffer(bh);
2524 bh->b_end_io = end_buffer_read_nobh;
2525 submit_bh(READ, bh);
2526 nr_reads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528 }
2529
2530 if (nr_reads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * The page is locked, so these buffers are protected from
2533 * any VM or truncate activity. Hence we don't need to care
2534 * for the buffer_head refcounts.
2535 */
Nick Piggina4b06722007-10-16 01:24:48 -07002536 for (bh = head; bh; bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 wait_on_buffer(bh);
2538 if (!buffer_uptodate(bh))
2539 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 if (ret)
2542 goto failed;
2543 }
2544
2545 if (is_mapped_to_disk)
2546 SetPageMappedToDisk(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Nick Piggin03158cd2007-10-16 01:25:25 -07002548 *fsdata = head; /* to be released by nobh_write_end */
Nick Piggina4b06722007-10-16 01:24:48 -07002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return 0;
2551
2552failed:
Nick Piggin03158cd2007-10-16 01:25:25 -07002553 BUG_ON(!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /*
Nick Piggina4b06722007-10-16 01:24:48 -07002555 * Error recovery is a bit difficult. We need to zero out blocks that
2556 * were newly allocated, and dirty them to ensure they get written out.
2557 * Buffers need to be attached to the page at this point, otherwise
2558 * the handling of potential IO errors during writeout would be hard
2559 * (could try doing synchronous writeout, but what if that fails too?)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 */
Nick Piggin03158cd2007-10-16 01:25:25 -07002561 attach_nobh_buffers(page, head);
2562 page_zero_new_buffers(page, from, to);
Nick Piggina4b06722007-10-16 01:24:48 -07002563
Nick Piggin03158cd2007-10-16 01:25:25 -07002564out_release:
2565 unlock_page(page);
2566 page_cache_release(page);
2567 *pagep = NULL;
Nick Piggina4b06722007-10-16 01:24:48 -07002568
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10002569 return ret;
2570}
Nick Piggin03158cd2007-10-16 01:25:25 -07002571EXPORT_SYMBOL(nobh_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Nick Piggin03158cd2007-10-16 01:25:25 -07002573int nobh_write_end(struct file *file, struct address_space *mapping,
2574 loff_t pos, unsigned len, unsigned copied,
2575 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 struct inode *inode = page->mapping->host;
Nick Pigginefdc3132007-10-21 06:57:41 +02002578 struct buffer_head *head = fsdata;
Nick Piggin03158cd2007-10-16 01:25:25 -07002579 struct buffer_head *bh;
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002580 BUG_ON(fsdata != NULL && page_has_buffers(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Dave Kleikampd4cf1092009-02-06 14:59:26 -06002582 if (unlikely(copied < len) && head)
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002583 attach_nobh_buffers(page, head);
2584 if (page_has_buffers(page))
2585 return generic_write_end(file, mapping, pos, len,
2586 copied, page, fsdata);
Nick Piggina4b06722007-10-16 01:24:48 -07002587
Nick Piggin22c8ca72007-02-20 13:58:09 -08002588 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 set_page_dirty(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002590 if (pos+copied > inode->i_size) {
2591 i_size_write(inode, pos+copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 mark_inode_dirty(inode);
2593 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002594
2595 unlock_page(page);
2596 page_cache_release(page);
2597
Nick Piggin03158cd2007-10-16 01:25:25 -07002598 while (head) {
2599 bh = head;
2600 head = head->b_this_page;
2601 free_buffer_head(bh);
2602 }
2603
2604 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
Nick Piggin03158cd2007-10-16 01:25:25 -07002606EXPORT_SYMBOL(nobh_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608/*
2609 * nobh_writepage() - based on block_full_write_page() except
2610 * that it tries to operate without attaching bufferheads to
2611 * the page.
2612 */
2613int nobh_writepage(struct page *page, get_block_t *get_block,
2614 struct writeback_control *wbc)
2615{
2616 struct inode * const inode = page->mapping->host;
2617 loff_t i_size = i_size_read(inode);
2618 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2619 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 int ret;
2621
2622 /* Is the page fully inside i_size? */
2623 if (page->index < end_index)
2624 goto out;
2625
2626 /* Is the page fully outside i_size? (truncate in progress) */
2627 offset = i_size & (PAGE_CACHE_SIZE-1);
2628 if (page->index >= end_index+1 || !offset) {
2629 /*
2630 * The page may have dirty, unmapped buffers. For example,
2631 * they may have been added in ext3_writepage(). Make them
2632 * freeable here, so the page does not leak.
2633 */
2634#if 0
2635 /* Not really sure about this - do we need this ? */
2636 if (page->mapping->a_ops->invalidatepage)
2637 page->mapping->a_ops->invalidatepage(page, offset);
2638#endif
2639 unlock_page(page);
2640 return 0; /* don't care */
2641 }
2642
2643 /*
2644 * The page straddles i_size. It must be zeroed out on each and every
2645 * writepage invocation because it may be mmapped. "A file is mapped
2646 * in multiples of the page size. For a file that is not a multiple of
2647 * the page size, the remaining memory is zeroed when mapped, and
2648 * writes to that region are not written out to the file."
2649 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002650 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651out:
2652 ret = mpage_writepage(page, get_block, wbc);
2653 if (ret == -EAGAIN)
Chris Mason35c80d52009-04-15 13:22:38 -04002654 ret = __block_write_full_page(inode, page, get_block, wbc,
2655 end_buffer_async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return ret;
2657}
2658EXPORT_SYMBOL(nobh_writepage);
2659
Nick Piggin03158cd2007-10-16 01:25:25 -07002660int nobh_truncate_page(struct address_space *mapping,
2661 loff_t from, get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2664 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Nick Piggin03158cd2007-10-16 01:25:25 -07002665 unsigned blocksize;
2666 sector_t iblock;
2667 unsigned length, pos;
2668 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct page *page;
Nick Piggin03158cd2007-10-16 01:25:25 -07002670 struct buffer_head map_bh;
2671 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Nick Piggin03158cd2007-10-16 01:25:25 -07002673 blocksize = 1 << inode->i_blkbits;
2674 length = offset & (blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Nick Piggin03158cd2007-10-16 01:25:25 -07002676 /* Block boundary? Nothing to do */
2677 if (!length)
2678 return 0;
2679
2680 length = blocksize - length;
2681 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 page = grab_cache_page(mapping, index);
Nick Piggin03158cd2007-10-16 01:25:25 -07002684 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (!page)
2686 goto out;
2687
Nick Piggin03158cd2007-10-16 01:25:25 -07002688 if (page_has_buffers(page)) {
2689has_buffers:
2690 unlock_page(page);
2691 page_cache_release(page);
2692 return block_truncate_page(mapping, from, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002694
2695 /* Find the buffer that contains "offset" */
2696 pos = blocksize;
2697 while (offset >= pos) {
2698 iblock++;
2699 pos += blocksize;
2700 }
2701
Theodore Ts'o460bcf52009-05-12 07:37:56 -04002702 map_bh.b_size = blocksize;
2703 map_bh.b_state = 0;
Nick Piggin03158cd2007-10-16 01:25:25 -07002704 err = get_block(inode, iblock, &map_bh, 0);
2705 if (err)
2706 goto unlock;
2707 /* unmapped? It's a hole - nothing to do */
2708 if (!buffer_mapped(&map_bh))
2709 goto unlock;
2710
2711 /* Ok, it's mapped. Make sure it's up-to-date */
2712 if (!PageUptodate(page)) {
2713 err = mapping->a_ops->readpage(NULL, page);
2714 if (err) {
2715 page_cache_release(page);
2716 goto out;
2717 }
2718 lock_page(page);
2719 if (!PageUptodate(page)) {
2720 err = -EIO;
2721 goto unlock;
2722 }
2723 if (page_has_buffers(page))
2724 goto has_buffers;
2725 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002726 zero_user(page, offset, length);
Nick Piggin03158cd2007-10-16 01:25:25 -07002727 set_page_dirty(page);
2728 err = 0;
2729
2730unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 unlock_page(page);
2732 page_cache_release(page);
2733out:
Nick Piggin03158cd2007-10-16 01:25:25 -07002734 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736EXPORT_SYMBOL(nobh_truncate_page);
2737
2738int block_truncate_page(struct address_space *mapping,
2739 loff_t from, get_block_t *get_block)
2740{
2741 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2742 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2743 unsigned blocksize;
Andrew Morton54b21a72006-01-08 01:03:05 -08002744 sector_t iblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 unsigned length, pos;
2746 struct inode *inode = mapping->host;
2747 struct page *page;
2748 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 int err;
2750
2751 blocksize = 1 << inode->i_blkbits;
2752 length = offset & (blocksize - 1);
2753
2754 /* Block boundary? Nothing to do */
2755 if (!length)
2756 return 0;
2757
2758 length = blocksize - length;
Andrew Morton54b21a72006-01-08 01:03:05 -08002759 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 page = grab_cache_page(mapping, index);
2762 err = -ENOMEM;
2763 if (!page)
2764 goto out;
2765
2766 if (!page_has_buffers(page))
2767 create_empty_buffers(page, blocksize, 0);
2768
2769 /* Find the buffer that contains "offset" */
2770 bh = page_buffers(page);
2771 pos = blocksize;
2772 while (offset >= pos) {
2773 bh = bh->b_this_page;
2774 iblock++;
2775 pos += blocksize;
2776 }
2777
2778 err = 0;
2779 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002780 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 err = get_block(inode, iblock, bh, 0);
2782 if (err)
2783 goto unlock;
2784 /* unmapped? It's a hole - nothing to do */
2785 if (!buffer_mapped(bh))
2786 goto unlock;
2787 }
2788
2789 /* Ok, it's mapped. Make sure it's up-to-date */
2790 if (PageUptodate(page))
2791 set_buffer_uptodate(bh);
2792
David Chinner33a266d2007-02-12 00:51:41 -08002793 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 err = -EIO;
2795 ll_rw_block(READ, 1, &bh);
2796 wait_on_buffer(bh);
2797 /* Uhhuh. Read error. Complain and punt. */
2798 if (!buffer_uptodate(bh))
2799 goto unlock;
2800 }
2801
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002802 zero_user(page, offset, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 mark_buffer_dirty(bh);
2804 err = 0;
2805
2806unlock:
2807 unlock_page(page);
2808 page_cache_release(page);
2809out:
2810 return err;
2811}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002812EXPORT_SYMBOL(block_truncate_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814/*
2815 * The generic ->writepage function for buffer-backed address_spaces
Chris Mason35c80d52009-04-15 13:22:38 -04002816 * this form passes in the end_io handler used to finish the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 */
Chris Mason35c80d52009-04-15 13:22:38 -04002818int block_write_full_page_endio(struct page *page, get_block_t *get_block,
2819 struct writeback_control *wbc, bh_end_io_t *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 struct inode * const inode = page->mapping->host;
2822 loff_t i_size = i_size_read(inode);
2823 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2824 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 /* Is the page fully inside i_size? */
2827 if (page->index < end_index)
Chris Mason35c80d52009-04-15 13:22:38 -04002828 return __block_write_full_page(inode, page, get_block, wbc,
2829 handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 /* Is the page fully outside i_size? (truncate in progress) */
2832 offset = i_size & (PAGE_CACHE_SIZE-1);
2833 if (page->index >= end_index+1 || !offset) {
2834 /*
2835 * The page may have dirty, unmapped buffers. For example,
2836 * they may have been added in ext3_writepage(). Make them
2837 * freeable here, so the page does not leak.
2838 */
Jan Karaaaa40592005-10-30 15:00:16 -08002839 do_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 unlock_page(page);
2841 return 0; /* don't care */
2842 }
2843
2844 /*
2845 * The page straddles i_size. It must be zeroed out on each and every
Adam Buchbinder2a61aa42009-12-11 16:35:40 -05002846 * writepage invocation because it may be mmapped. "A file is mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * in multiples of the page size. For a file that is not a multiple of
2848 * the page size, the remaining memory is zeroed when mapped, and
2849 * writes to that region are not written out to the file."
2850 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002851 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Chris Mason35c80d52009-04-15 13:22:38 -04002852 return __block_write_full_page(inode, page, get_block, wbc, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002854EXPORT_SYMBOL(block_write_full_page_endio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Chris Mason35c80d52009-04-15 13:22:38 -04002856/*
2857 * The generic ->writepage function for buffer-backed address_spaces
2858 */
2859int block_write_full_page(struct page *page, get_block_t *get_block,
2860 struct writeback_control *wbc)
2861{
2862 return block_write_full_page_endio(page, get_block, wbc,
2863 end_buffer_async_write);
2864}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002865EXPORT_SYMBOL(block_write_full_page);
Chris Mason35c80d52009-04-15 13:22:38 -04002866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2868 get_block_t *get_block)
2869{
2870 struct buffer_head tmp;
2871 struct inode *inode = mapping->host;
2872 tmp.b_state = 0;
2873 tmp.b_blocknr = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002874 tmp.b_size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 get_block(inode, block, &tmp, 0);
2876 return tmp.b_blocknr;
2877}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002878EXPORT_SYMBOL(generic_block_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
NeilBrown6712ecf2007-09-27 12:47:43 +02002880static void end_bio_bh_io_sync(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 struct buffer_head *bh = bio->bi_private;
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (err == -EOPNOTSUPP) {
2885 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887
Keith Mannthey08bafc02008-11-25 10:24:35 +01002888 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2889 set_bit(BH_Quiet, &bh->b_state);
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2892 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
Linus Torvalds57302e02012-12-04 08:25:11 -08002895/*
2896 * This allows us to do IO even on the odd last sectors
2897 * of a device, even if the bh block size is some multiple
2898 * of the physical sector size.
2899 *
2900 * We'll just truncate the bio to the size of the device,
2901 * and clear the end of the buffer head manually.
2902 *
2903 * Truly out-of-range accesses will turn into actual IO
2904 * errors, this only handles the "we need to be able to
2905 * do IO at the final sector" case.
2906 */
2907static void guard_bh_eod(int rw, struct bio *bio, struct buffer_head *bh)
2908{
2909 sector_t maxsector;
2910 unsigned bytes;
2911
2912 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
2913 if (!maxsector)
2914 return;
2915
2916 /*
2917 * If the *whole* IO is past the end of the device,
2918 * let it through, and the IO layer will turn it into
2919 * an EIO.
2920 */
2921 if (unlikely(bio->bi_sector >= maxsector))
2922 return;
2923
2924 maxsector -= bio->bi_sector;
2925 bytes = bio->bi_size;
2926 if (likely((bytes >> 9) <= maxsector))
2927 return;
2928
2929 /* Uhhuh. We've got a bh that straddles the device size! */
2930 bytes = maxsector << 9;
2931
2932 /* Truncate the bio.. */
2933 bio->bi_size = bytes;
2934 bio->bi_io_vec[0].bv_len = bytes;
2935
2936 /* ..and clear the end of the buffer for reads */
Dan Carpenter27d7c2a2012-12-05 20:01:24 +03002937 if ((rw & RW_MASK) == READ) {
Linus Torvalds57302e02012-12-04 08:25:11 -08002938 void *kaddr = kmap_atomic(bh->b_page);
2939 memset(kaddr + bh_offset(bh) + bytes, 0, bh->b_size - bytes);
2940 kunmap_atomic(kaddr);
2941 }
2942}
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944int submit_bh(int rw, struct buffer_head * bh)
2945{
2946 struct bio *bio;
2947 int ret = 0;
2948
2949 BUG_ON(!buffer_locked(bh));
2950 BUG_ON(!buffer_mapped(bh));
2951 BUG_ON(!bh->b_end_io);
Aneesh Kumar K.V8fb0e342009-05-12 16:22:37 -04002952 BUG_ON(buffer_delay(bh));
2953 BUG_ON(buffer_unwritten(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Jens Axboe48fd4f92008-08-22 10:00:36 +02002955 /*
Jens Axboe48fd4f92008-08-22 10:00:36 +02002956 * Only clear out a write error when rewriting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 */
Jens Axboe48fd4f92008-08-22 10:00:36 +02002958 if (test_set_buffer_req(bh) && (rw & WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 clear_buffer_write_io_error(bh);
2960
2961 /*
2962 * from here on down, it's all bio -- do the initial mapping,
2963 * submit_bio -> generic_make_request may further map this bio around
2964 */
2965 bio = bio_alloc(GFP_NOIO, 1);
2966
2967 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2968 bio->bi_bdev = bh->b_bdev;
2969 bio->bi_io_vec[0].bv_page = bh->b_page;
2970 bio->bi_io_vec[0].bv_len = bh->b_size;
2971 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2972
2973 bio->bi_vcnt = 1;
2974 bio->bi_idx = 0;
2975 bio->bi_size = bh->b_size;
2976
2977 bio->bi_end_io = end_bio_bh_io_sync;
2978 bio->bi_private = bh;
2979
Linus Torvalds57302e02012-12-04 08:25:11 -08002980 /* Take care of bh's that straddle the end of the device */
2981 guard_bh_eod(rw, bio, bh);
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 bio_get(bio);
2984 submit_bio(rw, bio);
2985
2986 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2987 ret = -EOPNOTSUPP;
2988
2989 bio_put(bio);
2990 return ret;
2991}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07002992EXPORT_SYMBOL(submit_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994/**
2995 * ll_rw_block: low-level access to block devices (DEPRECATED)
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02002996 * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 * @nr: number of &struct buffer_heads in the array
2998 * @bhs: array of pointers to &struct buffer_head
2999 *
Jan Karaa7662232005-09-06 15:19:10 -07003000 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
3001 * requests an I/O operation on them, either a %READ or a %WRITE. The third
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003002 * %READA option is described in the documentation for generic_make_request()
3003 * which ll_rw_block() calls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 *
3005 * This function drops any buffer that it cannot get a lock on (with the
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003006 * BH_Lock state bit), any buffer that appears to be clean when doing a write
3007 * request, and any buffer that appears to be up-to-date when doing read
3008 * request. Further it marks as clean buffers that are processed for
3009 * writing (the buffer cache won't assume that they are actually clean
3010 * until the buffer gets unlocked).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 *
3012 * ll_rw_block sets b_end_io to simple completion handler that marks
3013 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
3014 * any waiters.
3015 *
3016 * All of the buffers must be for the same device, and must also be a
3017 * multiple of the current approved size for the device.
3018 */
3019void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
3020{
3021 int i;
3022
3023 for (i = 0; i < nr; i++) {
3024 struct buffer_head *bh = bhs[i];
3025
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003026 if (!trylock_buffer(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 continue;
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003028 if (rw == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (test_clear_buffer_dirty(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003030 bh->b_end_io = end_buffer_write_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003031 get_bh(bh);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003032 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 continue;
3034 }
3035 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 if (!buffer_uptodate(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07003037 bh->b_end_io = end_buffer_read_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08003038 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 submit_bh(rw, bh);
3040 continue;
3041 }
3042 }
3043 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
3045}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003046EXPORT_SYMBOL(ll_rw_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02003048void write_dirty_buffer(struct buffer_head *bh, int rw)
3049{
3050 lock_buffer(bh);
3051 if (!test_clear_buffer_dirty(bh)) {
3052 unlock_buffer(bh);
3053 return;
3054 }
3055 bh->b_end_io = end_buffer_write_sync;
3056 get_bh(bh);
3057 submit_bh(rw, bh);
3058}
3059EXPORT_SYMBOL(write_dirty_buffer);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061/*
3062 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3063 * and then start new I/O and then wait upon it. The caller must have a ref on
3064 * the buffer_head.
3065 */
Christoph Hellwig87e99512010-08-11 17:05:45 +02003066int __sync_dirty_buffer(struct buffer_head *bh, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 int ret = 0;
3069
3070 WARN_ON(atomic_read(&bh->b_count) < 1);
3071 lock_buffer(bh);
3072 if (test_clear_buffer_dirty(bh)) {
3073 get_bh(bh);
3074 bh->b_end_io = end_buffer_write_sync;
Christoph Hellwig87e99512010-08-11 17:05:45 +02003075 ret = submit_bh(rw, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 wait_on_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (!ret && !buffer_uptodate(bh))
3078 ret = -EIO;
3079 } else {
3080 unlock_buffer(bh);
3081 }
3082 return ret;
3083}
Christoph Hellwig87e99512010-08-11 17:05:45 +02003084EXPORT_SYMBOL(__sync_dirty_buffer);
3085
3086int sync_dirty_buffer(struct buffer_head *bh)
3087{
3088 return __sync_dirty_buffer(bh, WRITE_SYNC);
3089}
H Hartley Sweeten1fe72ea2009-09-22 16:43:51 -07003090EXPORT_SYMBOL(sync_dirty_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092/*
3093 * try_to_free_buffers() checks if all the buffers on this particular page
3094 * are unused, and releases them if so.
3095 *
3096 * Exclusion against try_to_free_buffers may be obtained by either
3097 * locking the page or by holding its mapping's private_lock.
3098 *
3099 * If the page is dirty but all the buffers are clean then we need to
3100 * be sure to mark the page clean as well. This is because the page
3101 * may be against a block device, and a later reattachment of buffers
3102 * to a dirty page will set *all* buffers dirty. Which would corrupt
3103 * filesystem data on the same device.
3104 *
3105 * The same applies to regular filesystem pages: if all the buffers are
3106 * clean then we set the page clean and proceed. To do that, we require
3107 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3108 * private_lock.
3109 *
3110 * try_to_free_buffers() is non-blocking.
3111 */
3112static inline int buffer_busy(struct buffer_head *bh)
3113{
3114 return atomic_read(&bh->b_count) |
3115 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3116}
3117
3118static int
3119drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3120{
3121 struct buffer_head *head = page_buffers(page);
3122 struct buffer_head *bh;
3123
3124 bh = head;
3125 do {
akpm@osdl.orgde7d5a32005-05-01 08:58:39 -07003126 if (buffer_write_io_error(bh) && page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 set_bit(AS_EIO, &page->mapping->flags);
3128 if (buffer_busy(bh))
3129 goto failed;
3130 bh = bh->b_this_page;
3131 } while (bh != head);
3132
3133 do {
3134 struct buffer_head *next = bh->b_this_page;
3135
Jan Kara535ee2f2008-02-08 04:21:59 -08003136 if (bh->b_assoc_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 __remove_assoc_queue(bh);
3138 bh = next;
3139 } while (bh != head);
3140 *buffers_to_free = head;
3141 __clear_page_buffers(page);
3142 return 1;
3143failed:
3144 return 0;
3145}
3146
3147int try_to_free_buffers(struct page *page)
3148{
3149 struct address_space * const mapping = page->mapping;
3150 struct buffer_head *buffers_to_free = NULL;
3151 int ret = 0;
3152
3153 BUG_ON(!PageLocked(page));
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003154 if (PageWriteback(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 0;
3156
3157 if (mapping == NULL) { /* can this still happen? */
3158 ret = drop_buffers(page, &buffers_to_free);
3159 goto out;
3160 }
3161
3162 spin_lock(&mapping->private_lock);
3163 ret = drop_buffers(page, &buffers_to_free);
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003164
3165 /*
3166 * If the filesystem writes its buffers by hand (eg ext3)
3167 * then we can have clean buffers against a dirty page. We
3168 * clean the page here; otherwise the VM will never notice
3169 * that the filesystem did any IO at all.
3170 *
3171 * Also, during truncate, discard_buffer will have marked all
3172 * the page's buffers clean. We discover that here and clean
3173 * the page also.
Nick Piggin87df7242007-01-30 14:36:27 +11003174 *
3175 * private_lock must be held over this entire operation in order
3176 * to synchronise against __set_page_dirty_buffers and prevent the
3177 * dirty bit from being lost.
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003178 */
3179 if (ret)
3180 cancel_dirty_page(page, PAGE_CACHE_SIZE);
Nick Piggin87df7242007-01-30 14:36:27 +11003181 spin_unlock(&mapping->private_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182out:
3183 if (buffers_to_free) {
3184 struct buffer_head *bh = buffers_to_free;
3185
3186 do {
3187 struct buffer_head *next = bh->b_this_page;
3188 free_buffer_head(bh);
3189 bh = next;
3190 } while (bh != buffers_to_free);
3191 }
3192 return ret;
3193}
3194EXPORT_SYMBOL(try_to_free_buffers);
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196/*
3197 * There are no bdflush tunables left. But distributions are
3198 * still running obsolete flush daemons, so we terminate them here.
3199 *
3200 * Use of bdflush() is deprecated and will be removed in a future kernel.
Jens Axboe5b0830c2009-09-23 19:37:09 +02003201 * The `flush-X' kernel threads fully replace bdflush daemons and this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01003203SYSCALL_DEFINE2(bdflush, int, func, long, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 static int msg_count;
3206
3207 if (!capable(CAP_SYS_ADMIN))
3208 return -EPERM;
3209
3210 if (msg_count < 5) {
3211 msg_count++;
3212 printk(KERN_INFO
3213 "warning: process `%s' used the obsolete bdflush"
3214 " system call\n", current->comm);
3215 printk(KERN_INFO "Fix your initscripts?\n");
3216 }
3217
3218 if (func == 1)
3219 do_exit(0);
3220 return 0;
3221}
3222
3223/*
3224 * Buffer-head allocation
3225 */
Shai Fultheima0a9b042012-05-15 12:29:52 +03003226static struct kmem_cache *bh_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228/*
3229 * Once the number of bh's in the machine exceeds this level, we start
3230 * stripping them in writeback.
3231 */
3232static int max_buffer_heads;
3233
3234int buffer_heads_over_limit;
3235
3236struct bh_accounting {
3237 int nr; /* Number of live bh's */
3238 int ratelimit; /* Limit cacheline bouncing */
3239};
3240
3241static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3242
3243static void recalc_bh_state(void)
3244{
3245 int i;
3246 int tot = 0;
3247
Christoph Lameteree1be862010-12-06 11:40:05 -06003248 if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return;
Christoph Lameterc7b92512010-12-06 11:16:28 -06003250 __this_cpu_write(bh_accounting.ratelimit, 0);
Eric Dumazet8a143422006-03-24 03:18:10 -08003251 for_each_online_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 tot += per_cpu(bh_accounting, i).nr;
3253 buffer_heads_over_limit = (tot > max_buffer_heads);
3254}
Christoph Lameterc7b92512010-12-06 11:16:28 -06003255
Al Virodd0fc662005-10-07 07:46:04 +01003256struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
Richard Kennedy019b4d12010-03-10 15:20:33 -08003258 struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (ret) {
Christoph Lametera35afb82007-05-16 22:10:57 -07003260 INIT_LIST_HEAD(&ret->b_assoc_buffers);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003261 preempt_disable();
3262 __this_cpu_inc(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003264 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 return ret;
3267}
3268EXPORT_SYMBOL(alloc_buffer_head);
3269
3270void free_buffer_head(struct buffer_head *bh)
3271{
3272 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3273 kmem_cache_free(bh_cachep, bh);
Christoph Lameterc7b92512010-12-06 11:16:28 -06003274 preempt_disable();
3275 __this_cpu_dec(bh_accounting.nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 recalc_bh_state();
Christoph Lameterc7b92512010-12-06 11:16:28 -06003277 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279EXPORT_SYMBOL(free_buffer_head);
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281static void buffer_exit_cpu(int cpu)
3282{
3283 int i;
3284 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3285
3286 for (i = 0; i < BH_LRU_SIZE; i++) {
3287 brelse(b->bhs[i]);
3288 b->bhs[i] = NULL;
3289 }
Christoph Lameterc7b92512010-12-06 11:16:28 -06003290 this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
Eric Dumazet8a143422006-03-24 03:18:10 -08003291 per_cpu(bh_accounting, cpu).nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294static int buffer_cpu_notify(struct notifier_block *self,
3295 unsigned long action, void *hcpu)
3296{
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003297 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 buffer_exit_cpu((unsigned long)hcpu);
3299 return NOTIFY_OK;
3300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003302/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003303 * bh_uptodate_or_lock - Test whether the buffer is uptodate
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003304 * @bh: struct buffer_head
3305 *
3306 * Return true if the buffer is up-to-date and false,
3307 * with the buffer locked, if not.
3308 */
3309int bh_uptodate_or_lock(struct buffer_head *bh)
3310{
3311 if (!buffer_uptodate(bh)) {
3312 lock_buffer(bh);
3313 if (!buffer_uptodate(bh))
3314 return 0;
3315 unlock_buffer(bh);
3316 }
3317 return 1;
3318}
3319EXPORT_SYMBOL(bh_uptodate_or_lock);
3320
3321/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003322 * bh_submit_read - Submit a locked buffer for reading
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003323 * @bh: struct buffer_head
3324 *
3325 * Returns zero on success and -EIO on error.
3326 */
3327int bh_submit_read(struct buffer_head *bh)
3328{
3329 BUG_ON(!buffer_locked(bh));
3330
3331 if (buffer_uptodate(bh)) {
3332 unlock_buffer(bh);
3333 return 0;
3334 }
3335
3336 get_bh(bh);
3337 bh->b_end_io = end_buffer_read_sync;
3338 submit_bh(READ, bh);
3339 wait_on_buffer(bh);
3340 if (buffer_uptodate(bh))
3341 return 0;
3342 return -EIO;
3343}
3344EXPORT_SYMBOL(bh_submit_read);
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346void __init buffer_init(void)
3347{
3348 int nrpages;
3349
Christoph Lameterb98938c2008-02-04 22:28:36 -08003350 bh_cachep = kmem_cache_create("buffer_head",
3351 sizeof(struct buffer_head), 0,
3352 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3353 SLAB_MEM_SPREAD),
Richard Kennedy019b4d12010-03-10 15:20:33 -08003354 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 /*
3357 * Limit the bh occupancy to 10% of ZONE_NORMAL
3358 */
3359 nrpages = (nr_free_buffer_pages() * 10) / 100;
3360 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3361 hotcpu_notifier(buffer_cpu_notify, 0);
3362}