blob: 43afaa5d69014da26307910cc8cde01262a5aef6 [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>
32#include <linux/module.h>
33#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
49inline void
50init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
51{
52 bh->b_end_io = handler;
53 bh->b_private = private;
54}
55
56static int sync_buffer(void *word)
57{
58 struct block_device *bd;
59 struct buffer_head *bh
60 = container_of(word, struct buffer_head, b_state);
61
62 smp_mb();
63 bd = bh->b_bdev;
64 if (bd)
65 blk_run_address_space(bd->bd_inode->i_mapping);
66 io_schedule();
67 return 0;
68}
69
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080070void __lock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 wait_on_bit_lock(&bh->b_state, BH_Lock, sync_buffer,
73 TASK_UNINTERRUPTIBLE);
74}
75EXPORT_SYMBOL(__lock_buffer);
76
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080077void unlock_buffer(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Nick Piggin51b07fc2008-10-18 20:27:00 -070079 clear_bit_unlock(BH_Lock, &bh->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 smp_mb__after_clear_bit();
81 wake_up_bit(&bh->b_state, BH_Lock);
82}
83
84/*
85 * Block until a buffer comes unlocked. This doesn't stop it
86 * from becoming locked again - you have to lock it yourself
87 * if you want to preserve its state.
88 */
89void __wait_on_buffer(struct buffer_head * bh)
90{
91 wait_on_bit(&bh->b_state, BH_Lock, sync_buffer, TASK_UNINTERRUPTIBLE);
92}
93
94static void
95__clear_page_buffers(struct page *page)
96{
97 ClearPagePrivate(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070098 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 page_cache_release(page);
100}
101
Keith Mannthey08bafc02008-11-25 10:24:35 +0100102
103static int quiet_error(struct buffer_head *bh)
104{
105 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
106 return 0;
107 return 1;
108}
109
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static void buffer_io_error(struct buffer_head *bh)
112{
113 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
115 bdevname(bh->b_bdev, b),
116 (unsigned long long)bh->b_blocknr);
117}
118
119/*
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700120 * End-of-IO handler helper function which does not touch the bh after
121 * unlocking it.
122 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
123 * a race there is benign: unlock_buffer() only use the bh's address for
124 * hashing after unlocking the buffer, so it doesn't actually touch the bh
125 * itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700127static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 if (uptodate) {
130 set_buffer_uptodate(bh);
131 } else {
132 /* This happens, due to failed READA attempts. */
133 clear_buffer_uptodate(bh);
134 }
135 unlock_buffer(bh);
Dmitry Monakhov68671f32007-10-16 01:24:47 -0700136}
137
138/*
139 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
140 * unlock the buffer. This is what ll_rw_block uses too.
141 */
142void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
143{
144 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 put_bh(bh);
146}
147
148void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
149{
150 char b[BDEVNAME_SIZE];
151
152 if (uptodate) {
153 set_buffer_uptodate(bh);
154 } else {
Keith Mannthey08bafc02008-11-25 10:24:35 +0100155 if (!buffer_eopnotsupp(bh) && !quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 buffer_io_error(bh);
157 printk(KERN_WARNING "lost page write due to "
158 "I/O error on %s\n",
159 bdevname(bh->b_bdev, b));
160 }
161 set_buffer_write_io_error(bh);
162 clear_buffer_uptodate(bh);
163 }
164 unlock_buffer(bh);
165 put_bh(bh);
166}
167
168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Various filesystems appear to want __find_get_block to be non-blocking.
170 * But it's the page lock which protects the buffers. To get around this,
171 * we get exclusion from try_to_free_buffers with the blockdev mapping's
172 * private_lock.
173 *
174 * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
175 * may be quite high. This code could TryLock the page, and if that
176 * succeeds, there is no need to take private_lock. (But if
177 * private_lock is contended then so is mapping->tree_lock).
178 */
179static struct buffer_head *
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -0800180__find_get_block_slow(struct block_device *bdev, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct inode *bd_inode = bdev->bd_inode;
183 struct address_space *bd_mapping = bd_inode->i_mapping;
184 struct buffer_head *ret = NULL;
185 pgoff_t index;
186 struct buffer_head *bh;
187 struct buffer_head *head;
188 struct page *page;
189 int all_mapped = 1;
190
191 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
192 page = find_get_page(bd_mapping, index);
193 if (!page)
194 goto out;
195
196 spin_lock(&bd_mapping->private_lock);
197 if (!page_has_buffers(page))
198 goto out_unlock;
199 head = page_buffers(page);
200 bh = head;
201 do {
Nikanth Karthikesan97f76d32009-04-02 16:56:46 -0700202 if (!buffer_mapped(bh))
203 all_mapped = 0;
204 else if (bh->b_blocknr == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ret = bh;
206 get_bh(bh);
207 goto out_unlock;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 bh = bh->b_this_page;
210 } while (bh != head);
211
212 /* we might be here because some of the buffers on this page are
213 * not mapped. This is due to various races between
214 * file io on the block device and getblk. It gets dealt with
215 * elsewhere, don't buffer_error if we had some unmapped buffers
216 */
217 if (all_mapped) {
218 printk("__find_get_block_slow() failed. "
219 "block=%llu, b_blocknr=%llu\n",
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800220 (unsigned long long)block,
221 (unsigned long long)bh->b_blocknr);
222 printk("b_state=0x%08lx, b_size=%zu\n",
223 bh->b_state, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
225 }
226out_unlock:
227 spin_unlock(&bd_mapping->private_lock);
228 page_cache_release(page);
229out:
230 return ret;
231}
232
233/* If invalidate_buffers() will trash dirty buffers, it means some kind
234 of fs corruption is going on. Trashing dirty data always imply losing
235 information that was supposed to be just stored on the physical layer
236 by the user.
237
238 Thus invalidate_buffers in general usage is not allwowed to trash
239 dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to
240 be preserved. These buffers are simply skipped.
241
242 We also skip buffers which are still in use. For example this can
243 happen if a userspace program is reading the block device.
244
245 NOTE: In the case where the user removed a removable-media-disk even if
246 there's still dirty data not synced on disk (due a bug in the device driver
247 or due an error of the user), by not destroying the dirty buffers we could
248 generate corruption also on the next media inserted, thus a parameter is
249 necessary to handle this case in the most safe way possible (trying
250 to not corrupt also the new disk inserted with the data belonging to
251 the old now corrupted disk). Also for the ramdisk the natural thing
252 to do in order to release the ramdisk memory is to destroy dirty buffers.
253
254 These are two special cases. Normal usage imply the device driver
255 to issue a sync on the device (without waiting I/O completion) and
256 then an invalidate_buffers call that doesn't trash dirty buffers.
257
258 For handling cache coherency with the blkdev pagecache the 'update' case
259 is been introduced. It is needed to re-read from disk any pinned
260 buffer. NOTE: re-reading from disk is destructive so we can do it only
261 when we assume nobody is changing the buffercache under our I/O and when
262 we think the disk contains more recent information than the buffercache.
263 The update == 1 pass marks the buffers we need to update, the update == 2
264 pass does the actual I/O. */
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700265void invalidate_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Andrew Morton0e1dfc62006-07-30 03:03:28 -0700267 struct address_space *mapping = bdev->bd_inode->i_mapping;
268
269 if (mapping->nrpages == 0)
270 return;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 invalidate_bh_lrus();
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800273 invalidate_mapping_pages(mapping, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * Kick pdflush then try to free up some ZONE_NORMAL memory.
278 */
279static void free_more_memory(void)
280{
Mel Gorman19770b32008-04-28 02:12:18 -0700281 struct zone *zone;
Mel Gorman0e884602008-04-28 02:12:14 -0700282 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Pekka J Enberg687a21c2005-06-28 20:44:55 -0700284 wakeup_pdflush(1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 yield();
286
Mel Gorman0e884602008-04-28 02:12:14 -0700287 for_each_online_node(nid) {
Mel Gorman19770b32008-04-28 02:12:18 -0700288 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
289 gfp_zone(GFP_NOFS), NULL,
290 &zone);
291 if (zone)
Mel Gorman54a6eb52008-04-28 02:12:16 -0700292 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -0700293 GFP_NOFS, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295}
296
297/*
298 * I/O completion handler for block_read_full_page() - pages
299 * which come unlocked at the end of I/O.
300 */
301static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700304 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct buffer_head *tmp;
306 struct page *page;
307 int page_uptodate = 1;
308
309 BUG_ON(!buffer_async_read(bh));
310
311 page = bh->b_page;
312 if (uptodate) {
313 set_buffer_uptodate(bh);
314 } else {
315 clear_buffer_uptodate(bh);
Keith Mannthey08bafc02008-11-25 10:24:35 +0100316 if (!quiet_error(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 buffer_io_error(bh);
318 SetPageError(page);
319 }
320
321 /*
322 * Be _very_ careful from here on. Bad things can happen if
323 * two buffer heads end IO at almost the same time and both
324 * decide that the page is now completely done.
325 */
Nick Piggina3972202005-07-07 17:56:56 -0700326 first = page_buffers(page);
327 local_irq_save(flags);
328 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 clear_buffer_async_read(bh);
330 unlock_buffer(bh);
331 tmp = bh;
332 do {
333 if (!buffer_uptodate(tmp))
334 page_uptodate = 0;
335 if (buffer_async_read(tmp)) {
336 BUG_ON(!buffer_locked(tmp));
337 goto still_busy;
338 }
339 tmp = tmp->b_this_page;
340 } while (tmp != bh);
Nick Piggina3972202005-07-07 17:56:56 -0700341 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
342 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * If none of the buffers had errors and they are all
346 * uptodate then we can set the page uptodate.
347 */
348 if (page_uptodate && !PageError(page))
349 SetPageUptodate(page);
350 unlock_page(page);
351 return;
352
353still_busy:
Nick Piggina3972202005-07-07 17:56:56 -0700354 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
355 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357}
358
359/*
360 * Completion handler for block_write_full_page() - pages which are unlocked
361 * during I/O, and which have PageWriteback cleared upon I/O completion.
362 */
Adrian Bunkb6cd0b72006-06-27 02:53:54 -0700363static void end_buffer_async_write(struct buffer_head *bh, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long flags;
Nick Piggina3972202005-07-07 17:56:56 -0700367 struct buffer_head *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct buffer_head *tmp;
369 struct page *page;
370
371 BUG_ON(!buffer_async_write(bh));
372
373 page = bh->b_page;
374 if (uptodate) {
375 set_buffer_uptodate(bh);
376 } else {
Keith Mannthey08bafc02008-11-25 10:24:35 +0100377 if (!quiet_error(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 buffer_io_error(bh);
379 printk(KERN_WARNING "lost page write due to "
380 "I/O error on %s\n",
381 bdevname(bh->b_bdev, b));
382 }
383 set_bit(AS_EIO, &page->mapping->flags);
Jan Kara58ff4072006-10-17 00:10:19 -0700384 set_buffer_write_io_error(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 clear_buffer_uptodate(bh);
386 SetPageError(page);
387 }
388
Nick Piggina3972202005-07-07 17:56:56 -0700389 first = page_buffers(page);
390 local_irq_save(flags);
391 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 clear_buffer_async_write(bh);
394 unlock_buffer(bh);
395 tmp = bh->b_this_page;
396 while (tmp != bh) {
397 if (buffer_async_write(tmp)) {
398 BUG_ON(!buffer_locked(tmp));
399 goto still_busy;
400 }
401 tmp = tmp->b_this_page;
402 }
Nick Piggina3972202005-07-07 17:56:56 -0700403 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
404 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 end_page_writeback(page);
406 return;
407
408still_busy:
Nick Piggina3972202005-07-07 17:56:56 -0700409 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
410 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return;
412}
413
414/*
415 * If a page's buffers are under async readin (end_buffer_async_read
416 * completion) then there is a possibility that another thread of
417 * control could lock one of the buffers after it has completed
418 * but while some of the other buffers have not completed. This
419 * locked buffer would confuse end_buffer_async_read() into not unlocking
420 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
421 * that this buffer is not under async I/O.
422 *
423 * The page comes unlocked when it has no locked buffer_async buffers
424 * left.
425 *
426 * PageLocked prevents anyone starting new async I/O reads any of
427 * the buffers.
428 *
429 * PageWriteback is used to prevent simultaneous writeout of the same
430 * page.
431 *
432 * PageLocked prevents anyone from starting writeback of a page which is
433 * under read I/O (PageWriteback is only ever set against a locked page).
434 */
435static void mark_buffer_async_read(struct buffer_head *bh)
436{
437 bh->b_end_io = end_buffer_async_read;
438 set_buffer_async_read(bh);
439}
440
441void mark_buffer_async_write(struct buffer_head *bh)
442{
443 bh->b_end_io = end_buffer_async_write;
444 set_buffer_async_write(bh);
445}
446EXPORT_SYMBOL(mark_buffer_async_write);
447
448
449/*
450 * fs/buffer.c contains helper functions for buffer-backed address space's
451 * fsync functions. A common requirement for buffer-based filesystems is
452 * that certain data from the backing blockdev needs to be written out for
453 * a successful fsync(). For example, ext2 indirect blocks need to be
454 * written back and waited upon before fsync() returns.
455 *
456 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
457 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
458 * management of a list of dependent buffers at ->i_mapping->private_list.
459 *
460 * Locking is a little subtle: try_to_free_buffers() will remove buffers
461 * from their controlling inode's queue when they are being freed. But
462 * try_to_free_buffers() will be operating against the *blockdev* mapping
463 * at the time, not against the S_ISREG file which depends on those buffers.
464 * So the locking for private_list is via the private_lock in the address_space
465 * which backs the buffers. Which is different from the address_space
466 * against which the buffers are listed. So for a particular address_space,
467 * mapping->private_lock does *not* protect mapping->private_list! In fact,
468 * mapping->private_list will always be protected by the backing blockdev's
469 * ->private_lock.
470 *
471 * Which introduces a requirement: all buffers on an address_space's
472 * ->private_list must be from the same address_space: the blockdev's.
473 *
474 * address_spaces which do not place buffers at ->private_list via these
475 * utility functions are free to use private_lock and private_list for
476 * whatever they want. The only requirement is that list_empty(private_list)
477 * be true at clear_inode() time.
478 *
479 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
480 * filesystems should do that. invalidate_inode_buffers() should just go
481 * BUG_ON(!list_empty).
482 *
483 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
484 * take an address_space, not an inode. And it should be called
485 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
486 * queued up.
487 *
488 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
489 * list if it is already on a list. Because if the buffer is on a list,
490 * it *must* already be on the right one. If not, the filesystem is being
491 * silly. This will save a ton of locking. But first we have to ensure
492 * that buffers are taken *off* the old inode's list when they are freed
493 * (presumably in truncate). That requires careful auditing of all
494 * filesystems (do it inside bforget()). It could also be done by bringing
495 * b_inode back.
496 */
497
498/*
499 * The buffer's backing address_space's private_lock must be held
500 */
Thomas Petazzonidbacefc2008-07-29 22:33:47 -0700501static void __remove_assoc_queue(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -0700504 WARN_ON(!bh->b_assoc_map);
505 if (buffer_write_io_error(bh))
506 set_bit(AS_EIO, &bh->b_assoc_map->flags);
507 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510int inode_has_buffers(struct inode *inode)
511{
512 return !list_empty(&inode->i_data.private_list);
513}
514
515/*
516 * osync is designed to support O_SYNC io. It waits synchronously for
517 * all already-submitted IO to complete, but does not queue any new
518 * writes to the disk.
519 *
520 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
521 * you dirty the buffers, and then use osync_inode_buffers to wait for
522 * completion. Any other dirty buffers which are not yet queued for
523 * write will not be flushed to disk by the osync.
524 */
525static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
526{
527 struct buffer_head *bh;
528 struct list_head *p;
529 int err = 0;
530
531 spin_lock(lock);
532repeat:
533 list_for_each_prev(p, list) {
534 bh = BH_ENTRY(p);
535 if (buffer_locked(bh)) {
536 get_bh(bh);
537 spin_unlock(lock);
538 wait_on_buffer(bh);
539 if (!buffer_uptodate(bh))
540 err = -EIO;
541 brelse(bh);
542 spin_lock(lock);
543 goto repeat;
544 }
545 }
546 spin_unlock(lock);
547 return err;
548}
549
Eric Sandeenc2d75432009-03-31 15:23:46 -0700550void do_thaw_all(unsigned long unused)
551{
552 struct super_block *sb;
553 char b[BDEVNAME_SIZE];
554
555 spin_lock(&sb_lock);
556restart:
557 list_for_each_entry(sb, &super_blocks, s_list) {
558 sb->s_count++;
559 spin_unlock(&sb_lock);
560 down_read(&sb->s_umount);
561 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
562 printk(KERN_WARNING "Emergency Thaw on %s\n",
563 bdevname(sb->s_bdev, b));
564 up_read(&sb->s_umount);
565 spin_lock(&sb_lock);
566 if (__put_super_and_need_restart(sb))
567 goto restart;
568 }
569 spin_unlock(&sb_lock);
570 printk(KERN_WARNING "Emergency Thaw complete\n");
571}
572
573/**
574 * emergency_thaw_all -- forcibly thaw every frozen filesystem
575 *
576 * Used for emergency unfreeze of all filesystems via SysRq
577 */
578void emergency_thaw_all(void)
579{
580 pdflush_operation(do_thaw_all, 0);
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800584 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
Martin Waitz67be2dd2005-05-01 08:59:26 -0700585 * @mapping: the mapping which wants those buffers written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
587 * Starts I/O against the buffers at mapping->private_list, and waits upon
588 * that I/O.
589 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700590 * Basically, this is a convenience function for fsync().
591 * @mapping is a file or directory which needs those buffers to be written for
592 * a successful fsync().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594int sync_mapping_buffers(struct address_space *mapping)
595{
596 struct address_space *buffer_mapping = mapping->assoc_mapping;
597
598 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
599 return 0;
600
601 return fsync_buffers_list(&buffer_mapping->private_lock,
602 &mapping->private_list);
603}
604EXPORT_SYMBOL(sync_mapping_buffers);
605
606/*
607 * Called when we've recently written block `bblock', and it is known that
608 * `bblock' was for a buffer_boundary() buffer. This means that the block at
609 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
610 * dirty, schedule it for IO. So that indirects merge nicely with their data.
611 */
612void write_boundary_block(struct block_device *bdev,
613 sector_t bblock, unsigned blocksize)
614{
615 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
616 if (bh) {
617 if (buffer_dirty(bh))
618 ll_rw_block(WRITE, 1, &bh);
619 put_bh(bh);
620 }
621}
622
623void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
624{
625 struct address_space *mapping = inode->i_mapping;
626 struct address_space *buffer_mapping = bh->b_page->mapping;
627
628 mark_buffer_dirty(bh);
629 if (!mapping->assoc_mapping) {
630 mapping->assoc_mapping = buffer_mapping;
631 } else {
Eric Sesterhenne827f922006-03-26 18:24:46 +0200632 BUG_ON(mapping->assoc_mapping != buffer_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Jan Kara535ee2f2008-02-08 04:21:59 -0800634 if (!bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 spin_lock(&buffer_mapping->private_lock);
636 list_move_tail(&bh->b_assoc_buffers,
637 &mapping->private_list);
Jan Kara58ff4072006-10-17 00:10:19 -0700638 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 spin_unlock(&buffer_mapping->private_lock);
640 }
641}
642EXPORT_SYMBOL(mark_buffer_dirty_inode);
643
644/*
Nick Piggin787d2212007-07-17 04:03:34 -0700645 * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
646 * dirty.
647 *
648 * If warn is true, then emit a warning if the page is not uptodate and has
649 * not been truncated.
650 */
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700651static void __set_page_dirty(struct page *page,
Nick Piggin787d2212007-07-17 04:03:34 -0700652 struct address_space *mapping, int warn)
653{
Nick Piggin19fd6232008-07-25 19:45:32 -0700654 spin_lock_irq(&mapping->tree_lock);
Nick Piggin787d2212007-07-17 04:03:34 -0700655 if (page->mapping) { /* Race with truncate? */
656 WARN_ON_ONCE(warn && !PageUptodate(page));
Edward Shishkine3a7cca2009-03-31 15:19:39 -0700657 account_page_dirtied(page, mapping);
Nick Piggin787d2212007-07-17 04:03:34 -0700658 radix_tree_tag_set(&mapping->page_tree,
659 page_index(page), PAGECACHE_TAG_DIRTY);
660 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700661 spin_unlock_irq(&mapping->tree_lock);
Nick Piggin787d2212007-07-17 04:03:34 -0700662 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Nick Piggin787d2212007-07-17 04:03:34 -0700663}
664
665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * Add a page to the dirty page list.
667 *
668 * It is a sad fact of life that this function is called from several places
669 * deeply under spinlocking. It may not sleep.
670 *
671 * If the page has buffers, the uptodate buffers are set dirty, to preserve
672 * dirty-state coherency between the page and the buffers. It the page does
673 * not have buffers then when they are later attached they will all be set
674 * dirty.
675 *
676 * The buffers are dirtied before the page is dirtied. There's a small race
677 * window in which a writepage caller may see the page cleanness but not the
678 * buffer dirtiness. That's fine. If this code were to set the page dirty
679 * before the buffers, a concurrent writepage caller could clear the page dirty
680 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
681 * page on the dirty page list.
682 *
683 * We use private_lock to lock against try_to_free_buffers while using the
684 * page's buffer list. Also use this to protect against clean buffers being
685 * added to the page after it was set dirty.
686 *
687 * FIXME: may need to call ->reservepage here as well. That's rather up to the
688 * address_space though.
689 */
690int __set_page_dirty_buffers(struct page *page)
691{
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700692 int newly_dirty;
Nick Piggin787d2212007-07-17 04:03:34 -0700693 struct address_space *mapping = page_mapping(page);
Nick Pigginebf7a222006-10-10 04:36:54 +0200694
695 if (unlikely(!mapping))
696 return !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 spin_lock(&mapping->private_lock);
699 if (page_has_buffers(page)) {
700 struct buffer_head *head = page_buffers(page);
701 struct buffer_head *bh = head;
702
703 do {
704 set_buffer_dirty(bh);
705 bh = bh->b_this_page;
706 } while (bh != head);
707 }
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700708 newly_dirty = !TestSetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_unlock(&mapping->private_lock);
710
Linus Torvaldsa8e7d492009-03-19 11:32:05 -0700711 if (newly_dirty)
712 __set_page_dirty(page, mapping, 1);
713 return newly_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715EXPORT_SYMBOL(__set_page_dirty_buffers);
716
717/*
718 * Write out and wait upon a list of buffers.
719 *
720 * We have conflicting pressures: we want to make sure that all
721 * initially dirty buffers get waited on, but that any subsequently
722 * dirtied buffers don't. After all, we don't want fsync to last
723 * forever if somebody is actively writing to the file.
724 *
725 * Do this in two main stages: first we copy dirty buffers to a
726 * temporary inode list, queueing the writes as we go. Then we clean
727 * up, waiting for those writes to complete.
728 *
729 * During this second stage, any subsequent updates to the file may end
730 * up refiling the buffer on the original inode's dirty list again, so
731 * there is a chance we will end up with a buffer queued for write but
732 * not yet completed on that list. So, as a final cleanup we go through
733 * the osync code to catch these locked, dirty buffers without requeuing
734 * any newly dirty buffers for write.
735 */
736static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
737{
738 struct buffer_head *bh;
739 struct list_head tmp;
Jens Axboe9cf6b722009-04-06 14:48:03 +0200740 struct address_space *mapping, *prev_mapping = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int err = 0, err2;
742
743 INIT_LIST_HEAD(&tmp);
744
745 spin_lock(lock);
746 while (!list_empty(list)) {
747 bh = BH_ENTRY(list->next);
Jan Kara535ee2f2008-02-08 04:21:59 -0800748 mapping = bh->b_assoc_map;
Jan Kara58ff4072006-10-17 00:10:19 -0700749 __remove_assoc_queue(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800750 /* Avoid race with mark_buffer_dirty_inode() which does
751 * a lockless check and we rely on seeing the dirty bit */
752 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (buffer_dirty(bh) || buffer_locked(bh)) {
754 list_add(&bh->b_assoc_buffers, &tmp);
Jan Kara535ee2f2008-02-08 04:21:59 -0800755 bh->b_assoc_map = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (buffer_dirty(bh)) {
757 get_bh(bh);
758 spin_unlock(lock);
759 /*
760 * Ensure any pending I/O completes so that
761 * ll_rw_block() actually writes the current
762 * contents - it is a noop if I/O is still in
763 * flight on potentially older contents.
764 */
Jens Axboe9cf6b722009-04-06 14:48:03 +0200765 ll_rw_block(SWRITE_SYNC_PLUG, 1, &bh);
766
767 /*
768 * Kick off IO for the previous mapping. Note
769 * that we will not run the very last mapping,
770 * wait_on_buffer() will do that for us
771 * through sync_buffer().
772 */
773 if (prev_mapping && prev_mapping != mapping)
774 blk_run_address_space(prev_mapping);
775 prev_mapping = mapping;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 brelse(bh);
778 spin_lock(lock);
779 }
780 }
781 }
782
783 while (!list_empty(&tmp)) {
784 bh = BH_ENTRY(tmp.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 get_bh(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -0800786 mapping = bh->b_assoc_map;
787 __remove_assoc_queue(bh);
788 /* Avoid race with mark_buffer_dirty_inode() which does
789 * a lockless check and we rely on seeing the dirty bit */
790 smp_mb();
791 if (buffer_dirty(bh)) {
792 list_add(&bh->b_assoc_buffers,
Jan Karae3892292008-03-04 14:28:33 -0800793 &mapping->private_list);
Jan Kara535ee2f2008-02-08 04:21:59 -0800794 bh->b_assoc_map = mapping;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 spin_unlock(lock);
797 wait_on_buffer(bh);
798 if (!buffer_uptodate(bh))
799 err = -EIO;
800 brelse(bh);
801 spin_lock(lock);
802 }
803
804 spin_unlock(lock);
805 err2 = osync_buffers_list(lock, list);
806 if (err)
807 return err;
808 else
809 return err2;
810}
811
812/*
813 * Invalidate any and all dirty buffers on a given inode. We are
814 * probably unmounting the fs, but that doesn't mean we have already
815 * done a sync(). Just drop the buffers from the inode list.
816 *
817 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
818 * assumes that all the buffers are against the blockdev. Not true
819 * for reiserfs.
820 */
821void invalidate_inode_buffers(struct inode *inode)
822{
823 if (inode_has_buffers(inode)) {
824 struct address_space *mapping = &inode->i_data;
825 struct list_head *list = &mapping->private_list;
826 struct address_space *buffer_mapping = mapping->assoc_mapping;
827
828 spin_lock(&buffer_mapping->private_lock);
829 while (!list_empty(list))
830 __remove_assoc_queue(BH_ENTRY(list->next));
831 spin_unlock(&buffer_mapping->private_lock);
832 }
833}
Jan Kara52b19ac2008-09-23 18:24:08 +0200834EXPORT_SYMBOL(invalidate_inode_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836/*
837 * Remove any clean buffers from the inode's buffer list. This is called
838 * when we're trying to free the inode itself. Those buffers can pin it.
839 *
840 * Returns true if all buffers were removed.
841 */
842int remove_inode_buffers(struct inode *inode)
843{
844 int ret = 1;
845
846 if (inode_has_buffers(inode)) {
847 struct address_space *mapping = &inode->i_data;
848 struct list_head *list = &mapping->private_list;
849 struct address_space *buffer_mapping = mapping->assoc_mapping;
850
851 spin_lock(&buffer_mapping->private_lock);
852 while (!list_empty(list)) {
853 struct buffer_head *bh = BH_ENTRY(list->next);
854 if (buffer_dirty(bh)) {
855 ret = 0;
856 break;
857 }
858 __remove_assoc_queue(bh);
859 }
860 spin_unlock(&buffer_mapping->private_lock);
861 }
862 return ret;
863}
864
865/*
866 * Create the appropriate buffers when given a page for data area and
867 * the size of each buffer.. Use the bh->b_this_page linked list to
868 * follow the buffers created. Return NULL if unable to create more
869 * buffers.
870 *
871 * The retry flag is used to differentiate async IO (paging, swapping)
872 * which may not fail from ordinary buffer allocations.
873 */
874struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
875 int retry)
876{
877 struct buffer_head *bh, *head;
878 long offset;
879
880try_again:
881 head = NULL;
882 offset = PAGE_SIZE;
883 while ((offset -= size) >= 0) {
884 bh = alloc_buffer_head(GFP_NOFS);
885 if (!bh)
886 goto no_grow;
887
888 bh->b_bdev = NULL;
889 bh->b_this_page = head;
890 bh->b_blocknr = -1;
891 head = bh;
892
893 bh->b_state = 0;
894 atomic_set(&bh->b_count, 0);
Chris Masonfc5cd582006-02-01 03:06:48 -0800895 bh->b_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 bh->b_size = size;
897
898 /* Link the buffer to its page */
899 set_bh_page(bh, page, offset);
900
Nathan Scott01ffe332006-01-17 09:02:07 +1100901 init_buffer(bh, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 return head;
904/*
905 * In case anything failed, we just free everything we got.
906 */
907no_grow:
908 if (head) {
909 do {
910 bh = head;
911 head = head->b_this_page;
912 free_buffer_head(bh);
913 } while (head);
914 }
915
916 /*
917 * Return failure for non-async IO requests. Async IO requests
918 * are not allowed to fail, so we have to wait until buffer heads
919 * become available. But we don't want tasks sleeping with
920 * partially complete buffers, so all were released above.
921 */
922 if (!retry)
923 return NULL;
924
925 /* We're _really_ low on memory. Now we just
926 * wait for old buffer heads to become free due to
927 * finishing IO. Since this is an async request and
928 * the reserve list is empty, we're sure there are
929 * async buffer heads in use.
930 */
931 free_more_memory();
932 goto try_again;
933}
934EXPORT_SYMBOL_GPL(alloc_page_buffers);
935
936static inline void
937link_dev_buffers(struct page *page, struct buffer_head *head)
938{
939 struct buffer_head *bh, *tail;
940
941 bh = head;
942 do {
943 tail = bh;
944 bh = bh->b_this_page;
945 } while (bh);
946 tail->b_this_page = head;
947 attach_page_buffers(page, head);
948}
949
950/*
951 * Initialise the state of a blockdev page's buffers.
952 */
953static void
954init_page_buffers(struct page *page, struct block_device *bdev,
955 sector_t block, int size)
956{
957 struct buffer_head *head = page_buffers(page);
958 struct buffer_head *bh = head;
959 int uptodate = PageUptodate(page);
960
961 do {
962 if (!buffer_mapped(bh)) {
963 init_buffer(bh, NULL, NULL);
964 bh->b_bdev = bdev;
965 bh->b_blocknr = block;
966 if (uptodate)
967 set_buffer_uptodate(bh);
968 set_buffer_mapped(bh);
969 }
970 block++;
971 bh = bh->b_this_page;
972 } while (bh != head);
973}
974
975/*
976 * Create the page-cache page that contains the requested block.
977 *
978 * This is user purely for blockdev mappings.
979 */
980static struct page *
981grow_dev_page(struct block_device *bdev, sector_t block,
982 pgoff_t index, int size)
983{
984 struct inode *inode = bdev->bd_inode;
985 struct page *page;
986 struct buffer_head *bh;
987
Christoph Lameterea125892007-05-16 22:11:21 -0700988 page = find_or_create_page(inode->i_mapping, index,
Mel Gorman769848c2007-07-17 04:03:05 -0700989 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (!page)
991 return NULL;
992
Eric Sesterhenne827f922006-03-26 18:24:46 +0200993 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (page_has_buffers(page)) {
996 bh = page_buffers(page);
997 if (bh->b_size == size) {
998 init_page_buffers(page, bdev, block, size);
999 return page;
1000 }
1001 if (!try_to_free_buffers(page))
1002 goto failed;
1003 }
1004
1005 /*
1006 * Allocate some buffers for this page
1007 */
1008 bh = alloc_page_buffers(page, size, 0);
1009 if (!bh)
1010 goto failed;
1011
1012 /*
1013 * Link the page to the buffers and initialise them. Take the
1014 * lock to be atomic wrt __find_get_block(), which does not
1015 * run under the page lock.
1016 */
1017 spin_lock(&inode->i_mapping->private_lock);
1018 link_dev_buffers(page, bh);
1019 init_page_buffers(page, bdev, block, size);
1020 spin_unlock(&inode->i_mapping->private_lock);
1021 return page;
1022
1023failed:
1024 BUG();
1025 unlock_page(page);
1026 page_cache_release(page);
1027 return NULL;
1028}
1029
1030/*
1031 * Create buffers for the specified block device block's page. If
1032 * that page was dirty, the buffers are set dirty also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001034static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035grow_buffers(struct block_device *bdev, sector_t block, int size)
1036{
1037 struct page *page;
1038 pgoff_t index;
1039 int sizebits;
1040
1041 sizebits = -1;
1042 do {
1043 sizebits++;
1044 } while ((size << sizebits) < PAGE_SIZE);
1045
1046 index = block >> sizebits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Andrew Mortone5657932006-10-11 01:21:46 -07001048 /*
1049 * Check for a block which wants to lie outside our maximum possible
1050 * pagecache index. (this comparison is done using sector_t types).
1051 */
1052 if (unlikely(index != block >> sizebits)) {
1053 char b[BDEVNAME_SIZE];
1054
1055 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1056 "device %s\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001057 __func__, (unsigned long long)block,
Andrew Mortone5657932006-10-11 01:21:46 -07001058 bdevname(bdev, b));
1059 return -EIO;
1060 }
1061 block = index << sizebits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Create a page with the proper size buffers.. */
1063 page = grow_dev_page(bdev, block, index, size);
1064 if (!page)
1065 return 0;
1066 unlock_page(page);
1067 page_cache_release(page);
1068 return 1;
1069}
1070
Adrian Bunk75c96f82005-05-05 16:16:09 -07001071static struct buffer_head *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072__getblk_slow(struct block_device *bdev, sector_t block, int size)
1073{
1074 /* Size must be multiple of hard sectorsize */
1075 if (unlikely(size & (bdev_hardsect_size(bdev)-1) ||
1076 (size < 512 || size > PAGE_SIZE))) {
1077 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1078 size);
1079 printk(KERN_ERR "hardsect size: %d\n",
1080 bdev_hardsect_size(bdev));
1081
1082 dump_stack();
1083 return NULL;
1084 }
1085
1086 for (;;) {
1087 struct buffer_head * bh;
Andrew Mortone5657932006-10-11 01:21:46 -07001088 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 bh = __find_get_block(bdev, block, size);
1091 if (bh)
1092 return bh;
1093
Andrew Mortone5657932006-10-11 01:21:46 -07001094 ret = grow_buffers(bdev, block, size);
1095 if (ret < 0)
1096 return NULL;
1097 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 free_more_memory();
1099 }
1100}
1101
1102/*
1103 * The relationship between dirty buffers and dirty pages:
1104 *
1105 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1106 * the page is tagged dirty in its radix tree.
1107 *
1108 * At all times, the dirtiness of the buffers represents the dirtiness of
1109 * subsections of the page. If the page has buffers, the page dirty bit is
1110 * merely a hint about the true dirty state.
1111 *
1112 * When a page is set dirty in its entirety, all its buffers are marked dirty
1113 * (if the page has buffers).
1114 *
1115 * When a buffer is marked dirty, its page is dirtied, but the page's other
1116 * buffers are not.
1117 *
1118 * Also. When blockdev buffers are explicitly read with bread(), they
1119 * individually become uptodate. But their backing page remains not
1120 * uptodate - even if all of its buffers are uptodate. A subsequent
1121 * block_read_full_page() against that page will discover all the uptodate
1122 * buffers, will set the page uptodate and will perform no I/O.
1123 */
1124
1125/**
1126 * mark_buffer_dirty - mark a buffer_head as needing writeout
Martin Waitz67be2dd2005-05-01 08:59:26 -07001127 * @bh: the buffer_head to mark dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
1129 * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
1130 * backing page dirty, then tag the page as dirty in its address_space's radix
1131 * tree and then attach the address_space's inode to its superblock's dirty
1132 * inode list.
1133 *
1134 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
1135 * mapping->tree_lock and the global inode_lock.
1136 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001137void mark_buffer_dirty(struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Nick Piggin787d2212007-07-17 04:03:34 -07001139 WARN_ON_ONCE(!buffer_uptodate(bh));
Linus Torvalds1be62dc2008-04-04 14:38:17 -07001140
1141 /*
1142 * Very *carefully* optimize the it-is-already-dirty case.
1143 *
1144 * Don't let the final "is it dirty" escape to before we
1145 * perhaps modified the buffer.
1146 */
1147 if (buffer_dirty(bh)) {
1148 smp_mb();
1149 if (buffer_dirty(bh))
1150 return;
1151 }
1152
Linus Torvaldsa8e7d492009-03-19 11:32:05 -07001153 if (!test_set_buffer_dirty(bh)) {
1154 struct page *page = bh->b_page;
1155 if (!TestSetPageDirty(page))
1156 __set_page_dirty(page, page_mapping(page), 0);
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160/*
1161 * Decrement a buffer_head's reference count. If all buffers against a page
1162 * have zero reference count, are clean and unlocked, and if the page is clean
1163 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1164 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1165 * a page but it ends up not being freed, and buffers may later be reattached).
1166 */
1167void __brelse(struct buffer_head * buf)
1168{
1169 if (atomic_read(&buf->b_count)) {
1170 put_bh(buf);
1171 return;
1172 }
Arjan van de Ven5c752ad2008-07-25 19:45:40 -07001173 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176/*
1177 * bforget() is like brelse(), except it discards any
1178 * potentially dirty data.
1179 */
1180void __bforget(struct buffer_head *bh)
1181{
1182 clear_buffer_dirty(bh);
Jan Kara535ee2f2008-02-08 04:21:59 -08001183 if (bh->b_assoc_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct address_space *buffer_mapping = bh->b_page->mapping;
1185
1186 spin_lock(&buffer_mapping->private_lock);
1187 list_del_init(&bh->b_assoc_buffers);
Jan Kara58ff4072006-10-17 00:10:19 -07001188 bh->b_assoc_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 spin_unlock(&buffer_mapping->private_lock);
1190 }
1191 __brelse(bh);
1192}
1193
1194static struct buffer_head *__bread_slow(struct buffer_head *bh)
1195{
1196 lock_buffer(bh);
1197 if (buffer_uptodate(bh)) {
1198 unlock_buffer(bh);
1199 return bh;
1200 } else {
1201 get_bh(bh);
1202 bh->b_end_io = end_buffer_read_sync;
1203 submit_bh(READ, bh);
1204 wait_on_buffer(bh);
1205 if (buffer_uptodate(bh))
1206 return bh;
1207 }
1208 brelse(bh);
1209 return NULL;
1210}
1211
1212/*
1213 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1214 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1215 * refcount elevated by one when they're in an LRU. A buffer can only appear
1216 * once in a particular CPU's LRU. A single buffer can be present in multiple
1217 * CPU's LRUs at the same time.
1218 *
1219 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1220 * sb_find_get_block().
1221 *
1222 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1223 * a local interrupt disable for that.
1224 */
1225
1226#define BH_LRU_SIZE 8
1227
1228struct bh_lru {
1229 struct buffer_head *bhs[BH_LRU_SIZE];
1230};
1231
1232static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1233
1234#ifdef CONFIG_SMP
1235#define bh_lru_lock() local_irq_disable()
1236#define bh_lru_unlock() local_irq_enable()
1237#else
1238#define bh_lru_lock() preempt_disable()
1239#define bh_lru_unlock() preempt_enable()
1240#endif
1241
1242static inline void check_irqs_on(void)
1243{
1244#ifdef irqs_disabled
1245 BUG_ON(irqs_disabled());
1246#endif
1247}
1248
1249/*
1250 * The LRU management algorithm is dopey-but-simple. Sorry.
1251 */
1252static void bh_lru_install(struct buffer_head *bh)
1253{
1254 struct buffer_head *evictee = NULL;
1255 struct bh_lru *lru;
1256
1257 check_irqs_on();
1258 bh_lru_lock();
1259 lru = &__get_cpu_var(bh_lrus);
1260 if (lru->bhs[0] != bh) {
1261 struct buffer_head *bhs[BH_LRU_SIZE];
1262 int in;
1263 int out = 0;
1264
1265 get_bh(bh);
1266 bhs[out++] = bh;
1267 for (in = 0; in < BH_LRU_SIZE; in++) {
1268 struct buffer_head *bh2 = lru->bhs[in];
1269
1270 if (bh2 == bh) {
1271 __brelse(bh2);
1272 } else {
1273 if (out >= BH_LRU_SIZE) {
1274 BUG_ON(evictee != NULL);
1275 evictee = bh2;
1276 } else {
1277 bhs[out++] = bh2;
1278 }
1279 }
1280 }
1281 while (out < BH_LRU_SIZE)
1282 bhs[out++] = NULL;
1283 memcpy(lru->bhs, bhs, sizeof(bhs));
1284 }
1285 bh_lru_unlock();
1286
1287 if (evictee)
1288 __brelse(evictee);
1289}
1290
1291/*
1292 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1293 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001294static struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001295lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct buffer_head *ret = NULL;
1298 struct bh_lru *lru;
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001299 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 check_irqs_on();
1302 bh_lru_lock();
1303 lru = &__get_cpu_var(bh_lrus);
1304 for (i = 0; i < BH_LRU_SIZE; i++) {
1305 struct buffer_head *bh = lru->bhs[i];
1306
1307 if (bh && bh->b_bdev == bdev &&
1308 bh->b_blocknr == block && bh->b_size == size) {
1309 if (i) {
1310 while (i) {
1311 lru->bhs[i] = lru->bhs[i - 1];
1312 i--;
1313 }
1314 lru->bhs[0] = bh;
1315 }
1316 get_bh(bh);
1317 ret = bh;
1318 break;
1319 }
1320 }
1321 bh_lru_unlock();
1322 return ret;
1323}
1324
1325/*
1326 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1327 * it in the LRU and mark it as accessed. If it is not present then return
1328 * NULL
1329 */
1330struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001331__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1334
1335 if (bh == NULL) {
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001336 bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (bh)
1338 bh_lru_install(bh);
1339 }
1340 if (bh)
1341 touch_buffer(bh);
1342 return bh;
1343}
1344EXPORT_SYMBOL(__find_get_block);
1345
1346/*
1347 * __getblk will locate (and, if necessary, create) the buffer_head
1348 * which corresponds to the passed block_device, block and size. The
1349 * returned buffer has its reference count incremented.
1350 *
1351 * __getblk() cannot fail - it just keeps trying. If you pass it an
1352 * illegal block number, __getblk() will happily return a buffer_head
1353 * which represents the non-existent block. Very weird.
1354 *
1355 * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
1356 * attempt is failing. FIXME, perhaps?
1357 */
1358struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001359__getblk(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct buffer_head *bh = __find_get_block(bdev, block, size);
1362
1363 might_sleep();
1364 if (bh == NULL)
1365 bh = __getblk_slow(bdev, block, size);
1366 return bh;
1367}
1368EXPORT_SYMBOL(__getblk);
1369
1370/*
1371 * Do async read-ahead on a buffer..
1372 */
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001373void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct buffer_head *bh = __getblk(bdev, block, size);
Andrew Mortona3e713b2005-10-30 15:03:15 -08001376 if (likely(bh)) {
1377 ll_rw_block(READA, 1, &bh);
1378 brelse(bh);
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381EXPORT_SYMBOL(__breadahead);
1382
1383/**
1384 * __bread() - reads a specified block and returns the bh
Martin Waitz67be2dd2005-05-01 08:59:26 -07001385 * @bdev: the block_device to read from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * @block: number of block
1387 * @size: size (in bytes) to read
1388 *
1389 * Reads a specified block, and returns buffer head that contains it.
1390 * It returns NULL if the block was unreadable.
1391 */
1392struct buffer_head *
Tomasz Kvarsin3991d3b2007-02-12 00:52:14 -08001393__bread(struct block_device *bdev, sector_t block, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct buffer_head *bh = __getblk(bdev, block, size);
1396
Andrew Mortona3e713b2005-10-30 15:03:15 -08001397 if (likely(bh) && !buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 bh = __bread_slow(bh);
1399 return bh;
1400}
1401EXPORT_SYMBOL(__bread);
1402
1403/*
1404 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1405 * This doesn't race because it runs in each cpu either in irq
1406 * or with preempt disabled.
1407 */
1408static void invalidate_bh_lru(void *arg)
1409{
1410 struct bh_lru *b = &get_cpu_var(bh_lrus);
1411 int i;
1412
1413 for (i = 0; i < BH_LRU_SIZE; i++) {
1414 brelse(b->bhs[i]);
1415 b->bhs[i] = NULL;
1416 }
1417 put_cpu_var(bh_lrus);
1418}
1419
Peter Zijlstraf9a14392007-05-06 14:49:55 -07001420void invalidate_bh_lrus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001422 on_each_cpu(invalidate_bh_lru, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
Nick Piggin9db55792008-02-08 04:19:49 -08001424EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426void set_bh_page(struct buffer_head *bh,
1427 struct page *page, unsigned long offset)
1428{
1429 bh->b_page = page;
Eric Sesterhenne827f922006-03-26 18:24:46 +02001430 BUG_ON(offset >= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (PageHighMem(page))
1432 /*
1433 * This catches illegal uses and preserves the offset:
1434 */
1435 bh->b_data = (char *)(0 + offset);
1436 else
1437 bh->b_data = page_address(page) + offset;
1438}
1439EXPORT_SYMBOL(set_bh_page);
1440
1441/*
1442 * Called when truncating a buffer on a page completely.
1443 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001444static void discard_buffer(struct buffer_head * bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 lock_buffer(bh);
1447 clear_buffer_dirty(bh);
1448 bh->b_bdev = NULL;
1449 clear_buffer_mapped(bh);
1450 clear_buffer_req(bh);
1451 clear_buffer_new(bh);
1452 clear_buffer_delay(bh);
David Chinner33a266d2007-02-12 00:51:41 -08001453 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 unlock_buffer(bh);
1455}
1456
1457/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * block_invalidatepage - invalidate part of all of a buffer-backed page
1459 *
1460 * @page: the page which is affected
1461 * @offset: the index of the truncation point
1462 *
1463 * block_invalidatepage() is called when all or part of the page has become
1464 * invalidatedby a truncate operation.
1465 *
1466 * block_invalidatepage() does not have to release all buffers, but it must
1467 * ensure that no dirty buffer is left outside @offset and that no I/O
1468 * is underway against any of the blocks which are outside the truncation
1469 * point. Because the caller is about to free (and possibly reuse) those
1470 * blocks on-disk.
1471 */
NeilBrown2ff28e22006-03-26 01:37:18 -08001472void block_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct buffer_head *head, *bh, *next;
1475 unsigned int curr_off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 BUG_ON(!PageLocked(page));
1478 if (!page_has_buffers(page))
1479 goto out;
1480
1481 head = page_buffers(page);
1482 bh = head;
1483 do {
1484 unsigned int next_off = curr_off + bh->b_size;
1485 next = bh->b_this_page;
1486
1487 /*
1488 * is this block fully invalidated?
1489 */
1490 if (offset <= curr_off)
1491 discard_buffer(bh);
1492 curr_off = next_off;
1493 bh = next;
1494 } while (bh != head);
1495
1496 /*
1497 * We release buffers only if the entire page is being invalidated.
1498 * The get_block cached value has been unconditionally invalidated,
1499 * so real IO is not possible anymore.
1500 */
1501 if (offset == 0)
NeilBrown2ff28e22006-03-26 01:37:18 -08001502 try_to_release_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503out:
NeilBrown2ff28e22006-03-26 01:37:18 -08001504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506EXPORT_SYMBOL(block_invalidatepage);
1507
1508/*
1509 * We attach and possibly dirty the buffers atomically wrt
1510 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1511 * is already excluded via the page lock.
1512 */
1513void create_empty_buffers(struct page *page,
1514 unsigned long blocksize, unsigned long b_state)
1515{
1516 struct buffer_head *bh, *head, *tail;
1517
1518 head = alloc_page_buffers(page, blocksize, 1);
1519 bh = head;
1520 do {
1521 bh->b_state |= b_state;
1522 tail = bh;
1523 bh = bh->b_this_page;
1524 } while (bh);
1525 tail->b_this_page = head;
1526
1527 spin_lock(&page->mapping->private_lock);
1528 if (PageUptodate(page) || PageDirty(page)) {
1529 bh = head;
1530 do {
1531 if (PageDirty(page))
1532 set_buffer_dirty(bh);
1533 if (PageUptodate(page))
1534 set_buffer_uptodate(bh);
1535 bh = bh->b_this_page;
1536 } while (bh != head);
1537 }
1538 attach_page_buffers(page, head);
1539 spin_unlock(&page->mapping->private_lock);
1540}
1541EXPORT_SYMBOL(create_empty_buffers);
1542
1543/*
1544 * We are taking a block for data and we don't want any output from any
1545 * buffer-cache aliases starting from return from that function and
1546 * until the moment when something will explicitly mark the buffer
1547 * dirty (hopefully that will not happen until we will free that block ;-)
1548 * We don't even need to mark it not-uptodate - nobody can expect
1549 * anything from a newly allocated buffer anyway. We used to used
1550 * unmap_buffer() for such invalidation, but that was wrong. We definitely
1551 * don't want to mark the alias unmapped, for example - it would confuse
1552 * anyone who might pick it with bread() afterwards...
1553 *
1554 * Also.. Note that bforget() doesn't lock the buffer. So there can
1555 * be writeout I/O going on against recently-freed buffers. We don't
1556 * wait on that I/O in bforget() - it's more efficient to wait on the I/O
1557 * only if we really need to. That happens here.
1558 */
1559void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1560{
1561 struct buffer_head *old_bh;
1562
1563 might_sleep();
1564
Coywolf Qi Hunt385fd4c2005-11-07 00:59:39 -08001565 old_bh = __find_get_block_slow(bdev, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (old_bh) {
1567 clear_buffer_dirty(old_bh);
1568 wait_on_buffer(old_bh);
1569 clear_buffer_req(old_bh);
1570 __brelse(old_bh);
1571 }
1572}
1573EXPORT_SYMBOL(unmap_underlying_metadata);
1574
1575/*
1576 * NOTE! All mapped/uptodate combinations are valid:
1577 *
1578 * Mapped Uptodate Meaning
1579 *
1580 * No No "unknown" - must do get_block()
1581 * No Yes "hole" - zero-filled
1582 * Yes No "allocated" - allocated on disk, not read in
1583 * Yes Yes "valid" - allocated and up-to-date in memory.
1584 *
1585 * "Dirty" is valid only with the last case (mapped+uptodate).
1586 */
1587
1588/*
1589 * While block_write_full_page is writing back the dirty buffers under
1590 * the page lock, whoever dirtied the buffers may decide to clean them
1591 * again at any time. We handle that by only looking at the buffer
1592 * state inside lock_buffer().
1593 *
1594 * If block_write_full_page() is called for regular writeback
1595 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1596 * locked buffer. This only can happen if someone has written the buffer
1597 * directly, with submit_bh(). At the address_space level PageWriteback
1598 * prevents this contention from occurring.
1599 */
1600static int __block_write_full_page(struct inode *inode, struct page *page,
1601 get_block_t *get_block, struct writeback_control *wbc)
1602{
1603 int err;
1604 sector_t block;
1605 sector_t last_block;
Andrew Mortonf0fbd5f2005-05-05 16:15:48 -07001606 struct buffer_head *bh, *head;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001607 const unsigned blocksize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int nr_underway = 0;
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001609 int write_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 BUG_ON(!PageLocked(page));
1612
1613 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1614
1615 if (!page_has_buffers(page)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001616 create_empty_buffers(page, blocksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 (1 << BH_Dirty)|(1 << BH_Uptodate));
1618 }
1619
1620 /*
1621 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1622 * here, and the (potentially unmapped) buffers may become dirty at
1623 * any time. If a buffer becomes dirty here after we've inspected it
1624 * then we just miss that fact, and the page stays dirty.
1625 *
1626 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1627 * handle that here by just cleaning them.
1628 */
1629
Andrew Morton54b21a72006-01-08 01:03:05 -08001630 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 head = page_buffers(page);
1632 bh = head;
1633
1634 /*
1635 * Get all the dirty buffers mapped to disk addresses and
1636 * handle any aliases from the underlying blockdev's mapping.
1637 */
1638 do {
1639 if (block > last_block) {
1640 /*
1641 * mapped buffers outside i_size will occur, because
1642 * this page can be outside i_size when there is a
1643 * truncate in progress.
1644 */
1645 /*
1646 * The buffer was zeroed by block_write_full_page()
1647 */
1648 clear_buffer_dirty(bh);
1649 set_buffer_uptodate(bh);
Alex Tomas29a814d2008-07-11 19:27:31 -04001650 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1651 buffer_dirty(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001652 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 err = get_block(inode, block, bh, 1);
1654 if (err)
1655 goto recover;
Alex Tomas29a814d2008-07-11 19:27:31 -04001656 clear_buffer_delay(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (buffer_new(bh)) {
1658 /* blockdev mappings never come here */
1659 clear_buffer_new(bh);
1660 unmap_underlying_metadata(bh->b_bdev,
1661 bh->b_blocknr);
1662 }
1663 }
1664 bh = bh->b_this_page;
1665 block++;
1666 } while (bh != head);
1667
1668 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (!buffer_mapped(bh))
1670 continue;
1671 /*
1672 * If it's a fully non-blocking write attempt and we cannot
1673 * lock the buffer then redirty the page. Note that this can
1674 * potentially cause a busy-wait loop from pdflush and kswapd
1675 * activity, but those code paths have their own higher-level
1676 * throttling.
1677 */
1678 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1679 lock_buffer(bh);
Nick Pigginca5de402008-08-02 12:02:13 +02001680 } else if (!trylock_buffer(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 redirty_page_for_writepage(wbc, page);
1682 continue;
1683 }
1684 if (test_clear_buffer_dirty(bh)) {
1685 mark_buffer_async_write(bh);
1686 } else {
1687 unlock_buffer(bh);
1688 }
1689 } while ((bh = bh->b_this_page) != head);
1690
1691 /*
1692 * The page and its buffers are protected by PageWriteback(), so we can
1693 * drop the bh refcounts early.
1694 */
1695 BUG_ON(PageWriteback(page));
1696 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 do {
1699 struct buffer_head *next = bh->b_this_page;
1700 if (buffer_async_write(bh)) {
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001701 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 nr_underway++;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 bh = next;
1705 } while (bh != head);
Andrew Morton05937ba2005-05-05 16:15:47 -07001706 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 err = 0;
1709done:
1710 if (nr_underway == 0) {
1711 /*
1712 * The page was marked dirty, but the buffers were
1713 * clean. Someone wrote them back by hand with
1714 * ll_rw_block/submit_bh. A rare case.
1715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 end_page_writeback(page);
Nick Piggin3d67f2d2007-05-06 14:49:05 -07001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /*
1719 * The page and buffer_heads can be released at any time from
1720 * here on.
1721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 return err;
1724
1725recover:
1726 /*
1727 * ENOSPC, or some other error. We may already have added some
1728 * blocks to the file, so we need to write these out to avoid
1729 * exposing stale data.
1730 * The page is currently locked and not marked for writeback
1731 */
1732 bh = head;
1733 /* Recovery: lock and submit the mapped buffers */
1734 do {
Alex Tomas29a814d2008-07-11 19:27:31 -04001735 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1736 !buffer_delay(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 lock_buffer(bh);
1738 mark_buffer_async_write(bh);
1739 } else {
1740 /*
1741 * The buffer may have been set dirty during
1742 * attachment to a dirty page.
1743 */
1744 clear_buffer_dirty(bh);
1745 }
1746 } while ((bh = bh->b_this_page) != head);
1747 SetPageError(page);
1748 BUG_ON(PageWriteback(page));
Andrew Morton7e4c3692007-05-08 00:23:27 -07001749 mapping_set_error(page->mapping, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 do {
1752 struct buffer_head *next = bh->b_this_page;
1753 if (buffer_async_write(bh)) {
1754 clear_buffer_dirty(bh);
Theodore Ts'oa64c8612009-03-27 22:14:10 -04001755 submit_bh(write_op, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 nr_underway++;
1757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 bh = next;
1759 } while (bh != head);
Nick Pigginffda9d32007-02-20 13:57:54 -08001760 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto done;
1762}
1763
Nick Pigginafddba42007-10-16 01:25:01 -07001764/*
1765 * If a page has any new buffers, zero them out here, and mark them uptodate
1766 * and dirty so they'll be written out (in order to prevent uninitialised
1767 * block data from leaking). And clear the new bit.
1768 */
1769void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1770{
1771 unsigned int block_start, block_end;
1772 struct buffer_head *head, *bh;
1773
1774 BUG_ON(!PageLocked(page));
1775 if (!page_has_buffers(page))
1776 return;
1777
1778 bh = head = page_buffers(page);
1779 block_start = 0;
1780 do {
1781 block_end = block_start + bh->b_size;
1782
1783 if (buffer_new(bh)) {
1784 if (block_end > from && block_start < to) {
1785 if (!PageUptodate(page)) {
1786 unsigned start, size;
1787
1788 start = max(from, block_start);
1789 size = min(to, block_end) - start;
1790
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001791 zero_user(page, start, size);
Nick Pigginafddba42007-10-16 01:25:01 -07001792 set_buffer_uptodate(bh);
1793 }
1794
1795 clear_buffer_new(bh);
1796 mark_buffer_dirty(bh);
1797 }
1798 }
1799
1800 block_start = block_end;
1801 bh = bh->b_this_page;
1802 } while (bh != head);
1803}
1804EXPORT_SYMBOL(page_zero_new_buffers);
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806static int __block_prepare_write(struct inode *inode, struct page *page,
1807 unsigned from, unsigned to, get_block_t *get_block)
1808{
1809 unsigned block_start, block_end;
1810 sector_t block;
1811 int err = 0;
1812 unsigned blocksize, bbits;
1813 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1814
1815 BUG_ON(!PageLocked(page));
1816 BUG_ON(from > PAGE_CACHE_SIZE);
1817 BUG_ON(to > PAGE_CACHE_SIZE);
1818 BUG_ON(from > to);
1819
1820 blocksize = 1 << inode->i_blkbits;
1821 if (!page_has_buffers(page))
1822 create_empty_buffers(page, blocksize, 0);
1823 head = page_buffers(page);
1824
1825 bbits = inode->i_blkbits;
1826 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1827
1828 for(bh = head, block_start = 0; bh != head || !block_start;
1829 block++, block_start=block_end, bh = bh->b_this_page) {
1830 block_end = block_start + blocksize;
1831 if (block_end <= from || block_start >= to) {
1832 if (PageUptodate(page)) {
1833 if (!buffer_uptodate(bh))
1834 set_buffer_uptodate(bh);
1835 }
1836 continue;
1837 }
1838 if (buffer_new(bh))
1839 clear_buffer_new(bh);
1840 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08001841 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 err = get_block(inode, block, bh, 1);
1843 if (err)
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (buffer_new(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 unmap_underlying_metadata(bh->b_bdev,
1847 bh->b_blocknr);
1848 if (PageUptodate(page)) {
Nick Piggin637aff42007-10-16 01:25:00 -07001849 clear_buffer_new(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 set_buffer_uptodate(bh);
Nick Piggin637aff42007-10-16 01:25:00 -07001851 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 continue;
1853 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001854 if (block_end > to || block_start < from)
1855 zero_user_segments(page,
1856 to, block_end,
1857 block_start, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 continue;
1859 }
1860 }
1861 if (PageUptodate(page)) {
1862 if (!buffer_uptodate(bh))
1863 set_buffer_uptodate(bh);
1864 continue;
1865 }
1866 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
David Chinner33a266d2007-02-12 00:51:41 -08001867 !buffer_unwritten(bh) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 (block_start < from || block_end > to)) {
1869 ll_rw_block(READ, 1, &bh);
1870 *wait_bh++=bh;
1871 }
1872 }
1873 /*
1874 * If we issued read requests - let them complete.
1875 */
1876 while(wait_bh > wait) {
1877 wait_on_buffer(*--wait_bh);
1878 if (!buffer_uptodate(*wait_bh))
Nick Pigginf3ddbdc2005-05-05 16:15:45 -07001879 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Nick Pigginafddba42007-10-16 01:25:01 -07001881 if (unlikely(err))
1882 page_zero_new_buffers(page, from, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return err;
1884}
1885
1886static int __block_commit_write(struct inode *inode, struct page *page,
1887 unsigned from, unsigned to)
1888{
1889 unsigned block_start, block_end;
1890 int partial = 0;
1891 unsigned blocksize;
1892 struct buffer_head *bh, *head;
1893
1894 blocksize = 1 << inode->i_blkbits;
1895
1896 for(bh = head = page_buffers(page), block_start = 0;
1897 bh != head || !block_start;
1898 block_start=block_end, bh = bh->b_this_page) {
1899 block_end = block_start + blocksize;
1900 if (block_end <= from || block_start >= to) {
1901 if (!buffer_uptodate(bh))
1902 partial = 1;
1903 } else {
1904 set_buffer_uptodate(bh);
1905 mark_buffer_dirty(bh);
1906 }
Nick Pigginafddba42007-10-16 01:25:01 -07001907 clear_buffer_new(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 /*
1911 * If this is a partial write which happened to make all buffers
1912 * uptodate then we can optimize away a bogus readpage() for
1913 * the next read(). Here we 'discover' whether the page went
1914 * uptodate as a result of this (potentially partial) write.
1915 */
1916 if (!partial)
1917 SetPageUptodate(page);
1918 return 0;
1919}
1920
1921/*
Nick Pigginafddba42007-10-16 01:25:01 -07001922 * block_write_begin takes care of the basic task of block allocation and
1923 * bringing partial write blocks uptodate first.
1924 *
1925 * If *pagep is not NULL, then block_write_begin uses the locked page
1926 * at *pagep rather than allocating its own. In this case, the page will
1927 * not be unlocked or deallocated on failure.
1928 */
1929int block_write_begin(struct file *file, struct address_space *mapping,
1930 loff_t pos, unsigned len, unsigned flags,
1931 struct page **pagep, void **fsdata,
1932 get_block_t *get_block)
1933{
1934 struct inode *inode = mapping->host;
1935 int status = 0;
1936 struct page *page;
1937 pgoff_t index;
1938 unsigned start, end;
1939 int ownpage = 0;
1940
1941 index = pos >> PAGE_CACHE_SHIFT;
1942 start = pos & (PAGE_CACHE_SIZE - 1);
1943 end = start + len;
1944
1945 page = *pagep;
1946 if (page == NULL) {
1947 ownpage = 1;
Nick Piggin54566b22009-01-04 12:00:53 -08001948 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -07001949 if (!page) {
1950 status = -ENOMEM;
1951 goto out;
1952 }
1953 *pagep = page;
1954 } else
1955 BUG_ON(!PageLocked(page));
1956
1957 status = __block_prepare_write(inode, page, start, end, get_block);
1958 if (unlikely(status)) {
1959 ClearPageUptodate(page);
1960
1961 if (ownpage) {
1962 unlock_page(page);
1963 page_cache_release(page);
1964 *pagep = NULL;
1965
1966 /*
1967 * prepare_write() may have instantiated a few blocks
1968 * outside i_size. Trim these off again. Don't need
1969 * i_size_read because we hold i_mutex.
1970 */
1971 if (pos + len > inode->i_size)
1972 vmtruncate(inode, inode->i_size);
1973 }
Nick Pigginafddba42007-10-16 01:25:01 -07001974 }
1975
1976out:
1977 return status;
1978}
1979EXPORT_SYMBOL(block_write_begin);
1980
1981int block_write_end(struct file *file, struct address_space *mapping,
1982 loff_t pos, unsigned len, unsigned copied,
1983 struct page *page, void *fsdata)
1984{
1985 struct inode *inode = mapping->host;
1986 unsigned start;
1987
1988 start = pos & (PAGE_CACHE_SIZE - 1);
1989
1990 if (unlikely(copied < len)) {
1991 /*
1992 * The buffers that were written will now be uptodate, so we
1993 * don't have to worry about a readpage reading them and
1994 * overwriting a partial write. However if we have encountered
1995 * a short write and only partially written into a buffer, it
1996 * will not be marked uptodate, so a readpage might come in and
1997 * destroy our partial write.
1998 *
1999 * Do the simplest thing, and just treat any short write to a
2000 * non uptodate page as a zero-length write, and force the
2001 * caller to redo the whole thing.
2002 */
2003 if (!PageUptodate(page))
2004 copied = 0;
2005
2006 page_zero_new_buffers(page, start+copied, start+len);
2007 }
2008 flush_dcache_page(page);
2009
2010 /* This could be a short (even 0-length) commit */
2011 __block_commit_write(inode, page, start, start+copied);
2012
2013 return copied;
2014}
2015EXPORT_SYMBOL(block_write_end);
2016
2017int generic_write_end(struct file *file, struct address_space *mapping,
2018 loff_t pos, unsigned len, unsigned copied,
2019 struct page *page, void *fsdata)
2020{
2021 struct inode *inode = mapping->host;
Jan Karac7d206b2008-07-11 19:27:31 -04002022 int i_size_changed = 0;
Nick Pigginafddba42007-10-16 01:25:01 -07002023
2024 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2025
2026 /*
2027 * No need to use i_size_read() here, the i_size
2028 * cannot change under us because we hold i_mutex.
2029 *
2030 * But it's important to update i_size while still holding page lock:
2031 * page writeout could otherwise come in and zero beyond i_size.
2032 */
2033 if (pos+copied > inode->i_size) {
2034 i_size_write(inode, pos+copied);
Jan Karac7d206b2008-07-11 19:27:31 -04002035 i_size_changed = 1;
Nick Pigginafddba42007-10-16 01:25:01 -07002036 }
2037
2038 unlock_page(page);
2039 page_cache_release(page);
2040
Jan Karac7d206b2008-07-11 19:27:31 -04002041 /*
2042 * Don't mark the inode dirty under page lock. First, it unnecessarily
2043 * makes the holding time of page lock longer. Second, it forces lock
2044 * ordering of page lock and transaction start for journaling
2045 * filesystems.
2046 */
2047 if (i_size_changed)
2048 mark_inode_dirty(inode);
2049
Nick Pigginafddba42007-10-16 01:25:01 -07002050 return copied;
2051}
2052EXPORT_SYMBOL(generic_write_end);
2053
2054/*
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002055 * block_is_partially_uptodate checks whether buffers within a page are
2056 * uptodate or not.
2057 *
2058 * Returns true if all buffers which correspond to a file portion
2059 * we want to read are uptodate.
2060 */
2061int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2062 unsigned long from)
2063{
2064 struct inode *inode = page->mapping->host;
2065 unsigned block_start, block_end, blocksize;
2066 unsigned to;
2067 struct buffer_head *bh, *head;
2068 int ret = 1;
2069
2070 if (!page_has_buffers(page))
2071 return 0;
2072
2073 blocksize = 1 << inode->i_blkbits;
2074 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2075 to = from + to;
2076 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2077 return 0;
2078
2079 head = page_buffers(page);
2080 bh = head;
2081 block_start = 0;
2082 do {
2083 block_end = block_start + blocksize;
2084 if (block_end > from && block_start < to) {
2085 if (!buffer_uptodate(bh)) {
2086 ret = 0;
2087 break;
2088 }
2089 if (block_end >= to)
2090 break;
2091 }
2092 block_start = block_end;
2093 bh = bh->b_this_page;
2094 } while (bh != head);
2095
2096 return ret;
2097}
2098EXPORT_SYMBOL(block_is_partially_uptodate);
2099
2100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * Generic "read page" function for block devices that have the normal
2102 * get_block functionality. This is most of the block device filesystems.
2103 * Reads the page asynchronously --- the unlock_buffer() and
2104 * set/clear_buffer_uptodate() functions propagate buffer state into the
2105 * page struct once IO has completed.
2106 */
2107int block_read_full_page(struct page *page, get_block_t *get_block)
2108{
2109 struct inode *inode = page->mapping->host;
2110 sector_t iblock, lblock;
2111 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2112 unsigned int blocksize;
2113 int nr, i;
2114 int fully_mapped = 1;
2115
Matt Mackallcd7619d2005-05-01 08:59:01 -07002116 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 blocksize = 1 << inode->i_blkbits;
2118 if (!page_has_buffers(page))
2119 create_empty_buffers(page, blocksize, 0);
2120 head = page_buffers(page);
2121
2122 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2123 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2124 bh = head;
2125 nr = 0;
2126 i = 0;
2127
2128 do {
2129 if (buffer_uptodate(bh))
2130 continue;
2131
2132 if (!buffer_mapped(bh)) {
Andrew Mortonc64610b2005-05-16 21:53:49 -07002133 int err = 0;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 fully_mapped = 0;
2136 if (iblock < lblock) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002137 WARN_ON(bh->b_size != blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002138 err = get_block(inode, iblock, bh, 0);
2139 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 SetPageError(page);
2141 }
2142 if (!buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002143 zero_user(page, i * blocksize, blocksize);
Andrew Mortonc64610b2005-05-16 21:53:49 -07002144 if (!err)
2145 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 continue;
2147 }
2148 /*
2149 * get_block() might have updated the buffer
2150 * synchronously
2151 */
2152 if (buffer_uptodate(bh))
2153 continue;
2154 }
2155 arr[nr++] = bh;
2156 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2157
2158 if (fully_mapped)
2159 SetPageMappedToDisk(page);
2160
2161 if (!nr) {
2162 /*
2163 * All buffers are uptodate - we can set the page uptodate
2164 * as well. But not if get_block() returned an error.
2165 */
2166 if (!PageError(page))
2167 SetPageUptodate(page);
2168 unlock_page(page);
2169 return 0;
2170 }
2171
2172 /* Stage two: lock the buffers */
2173 for (i = 0; i < nr; i++) {
2174 bh = arr[i];
2175 lock_buffer(bh);
2176 mark_buffer_async_read(bh);
2177 }
2178
2179 /*
2180 * Stage 3: start the IO. Check for uptodateness
2181 * inside the buffer lock in case another process reading
2182 * the underlying blockdev brought it uptodate (the sct fix).
2183 */
2184 for (i = 0; i < nr; i++) {
2185 bh = arr[i];
2186 if (buffer_uptodate(bh))
2187 end_buffer_async_read(bh, 1);
2188 else
2189 submit_bh(READ, bh);
2190 }
2191 return 0;
2192}
2193
2194/* utility function for filesystems that need to do work on expanding
Nick Piggin89e10782007-10-16 01:25:07 -07002195 * truncates. Uses filesystem pagecache writes to allow the filesystem to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 * deal with the hole.
2197 */
Nick Piggin89e10782007-10-16 01:25:07 -07002198int generic_cont_expand_simple(struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct address_space *mapping = inode->i_mapping;
2201 struct page *page;
Nick Piggin89e10782007-10-16 01:25:07 -07002202 void *fsdata;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002203 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int err;
2205
2206 err = -EFBIG;
2207 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
2208 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
2209 send_sig(SIGXFSZ, current, 0);
2210 goto out;
2211 }
2212 if (size > inode->i_sb->s_maxbytes)
2213 goto out;
2214
Nick Piggin89e10782007-10-16 01:25:07 -07002215 err = pagecache_write_begin(NULL, mapping, size, 0,
2216 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2217 &page, &fsdata);
2218 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto out;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002220
Nick Piggin89e10782007-10-16 01:25:07 -07002221 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2222 BUG_ON(err > 0);
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224out:
2225 return err;
2226}
2227
Adrian Bunkf1e3af72008-04-29 00:59:01 -07002228static int cont_expand_zero(struct file *file, struct address_space *mapping,
2229 loff_t pos, loff_t *bytes)
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002230{
Nick Piggin89e10782007-10-16 01:25:07 -07002231 struct inode *inode = mapping->host;
2232 unsigned blocksize = 1 << inode->i_blkbits;
2233 struct page *page;
2234 void *fsdata;
2235 pgoff_t index, curidx;
2236 loff_t curpos;
2237 unsigned zerofrom, offset, len;
2238 int err = 0;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002239
Nick Piggin89e10782007-10-16 01:25:07 -07002240 index = pos >> PAGE_CACHE_SHIFT;
2241 offset = pos & ~PAGE_CACHE_MASK;
2242
2243 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2244 zerofrom = curpos & ~PAGE_CACHE_MASK;
2245 if (zerofrom & (blocksize-1)) {
2246 *bytes |= (blocksize-1);
2247 (*bytes)++;
2248 }
2249 len = PAGE_CACHE_SIZE - zerofrom;
2250
2251 err = pagecache_write_begin(file, mapping, curpos, len,
2252 AOP_FLAG_UNINTERRUPTIBLE,
2253 &page, &fsdata);
2254 if (err)
2255 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002256 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002257 err = pagecache_write_end(file, mapping, curpos, len, len,
2258 page, fsdata);
2259 if (err < 0)
2260 goto out;
2261 BUG_ON(err != len);
2262 err = 0;
OGAWA Hirofumi061e9742008-04-28 02:16:28 -07002263
2264 balance_dirty_pages_ratelimited(mapping);
Nick Piggin89e10782007-10-16 01:25:07 -07002265 }
2266
2267 /* page covers the boundary, find the boundary offset */
2268 if (index == curidx) {
2269 zerofrom = curpos & ~PAGE_CACHE_MASK;
2270 /* if we will expand the thing last block will be filled */
2271 if (offset <= zerofrom) {
2272 goto out;
2273 }
2274 if (zerofrom & (blocksize-1)) {
2275 *bytes |= (blocksize-1);
2276 (*bytes)++;
2277 }
2278 len = offset - zerofrom;
2279
2280 err = pagecache_write_begin(file, mapping, curpos, len,
2281 AOP_FLAG_UNINTERRUPTIBLE,
2282 &page, &fsdata);
2283 if (err)
2284 goto out;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002285 zero_user(page, zerofrom, len);
Nick Piggin89e10782007-10-16 01:25:07 -07002286 err = pagecache_write_end(file, mapping, curpos, len, len,
2287 page, fsdata);
2288 if (err < 0)
2289 goto out;
2290 BUG_ON(err != len);
2291 err = 0;
2292 }
2293out:
2294 return err;
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08002295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/*
2298 * For moronic filesystems that do not allow holes in file.
2299 * We may have to extend the file.
2300 */
Nick Piggin89e10782007-10-16 01:25:07 -07002301int cont_write_begin(struct file *file, struct address_space *mapping,
2302 loff_t pos, unsigned len, unsigned flags,
2303 struct page **pagep, void **fsdata,
2304 get_block_t *get_block, loff_t *bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 unsigned blocksize = 1 << inode->i_blkbits;
Nick Piggin89e10782007-10-16 01:25:07 -07002308 unsigned zerofrom;
2309 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Nick Piggin89e10782007-10-16 01:25:07 -07002311 err = cont_expand_zero(file, mapping, pos, bytes);
2312 if (err)
2313 goto out;
2314
2315 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2316 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2317 *bytes |= (blocksize-1);
2318 (*bytes)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320
Nick Piggin89e10782007-10-16 01:25:07 -07002321 *pagep = NULL;
2322 err = block_write_begin(file, mapping, pos, len,
2323 flags, pagep, fsdata, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324out:
Nick Piggin89e10782007-10-16 01:25:07 -07002325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
2328int block_prepare_write(struct page *page, unsigned from, unsigned to,
2329 get_block_t *get_block)
2330{
2331 struct inode *inode = page->mapping->host;
2332 int err = __block_prepare_write(inode, page, from, to, get_block);
2333 if (err)
2334 ClearPageUptodate(page);
2335 return err;
2336}
2337
2338int block_commit_write(struct page *page, unsigned from, unsigned to)
2339{
2340 struct inode *inode = page->mapping->host;
2341 __block_commit_write(inode,page,from,to);
2342 return 0;
2343}
2344
David Chinner54171692007-07-19 17:39:55 +10002345/*
2346 * block_page_mkwrite() is not allowed to change the file size as it gets
2347 * called from a page fault handler when a page is first dirtied. Hence we must
2348 * be careful to check for EOF conditions here. We set the page up correctly
2349 * for a written page which means we get ENOSPC checking when writing into
2350 * holes and correct delalloc and unwritten extent mapping on filesystems that
2351 * support these features.
2352 *
2353 * We are not allowed to take the i_mutex here so we have to play games to
2354 * protect against truncate races as the page could now be beyond EOF. Because
2355 * vmtruncate() writes the inode size before removing pages, once we have the
2356 * page lock we can determine safely if the page is beyond EOF. If it is not
2357 * beyond EOF, then the page is guaranteed safe against truncation until we
2358 * unlock the page.
2359 */
2360int
Nick Pigginc2ec1752009-03-31 15:23:21 -07002361block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
David Chinner54171692007-07-19 17:39:55 +10002362 get_block_t get_block)
2363{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002364 struct page *page = vmf->page;
David Chinner54171692007-07-19 17:39:55 +10002365 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2366 unsigned long end;
2367 loff_t size;
Nick Piggin56a76f82009-03-31 15:23:23 -07002368 int ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
David Chinner54171692007-07-19 17:39:55 +10002369
2370 lock_page(page);
2371 size = i_size_read(inode);
2372 if ((page->mapping != inode->i_mapping) ||
Nick Piggin18336332007-07-20 00:31:45 -07002373 (page_offset(page) > size)) {
David Chinner54171692007-07-19 17:39:55 +10002374 /* page got truncated out from underneath us */
2375 goto out_unlock;
2376 }
2377
2378 /* page is wholly or partially inside EOF */
2379 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2380 end = size & ~PAGE_CACHE_MASK;
2381 else
2382 end = PAGE_CACHE_SIZE;
2383
2384 ret = block_prepare_write(page, 0, end, get_block);
2385 if (!ret)
2386 ret = block_commit_write(page, 0, end);
2387
Nick Piggin56a76f82009-03-31 15:23:23 -07002388 if (unlikely(ret)) {
2389 if (ret == -ENOMEM)
2390 ret = VM_FAULT_OOM;
2391 else /* -ENOSPC, -EIO, etc */
2392 ret = VM_FAULT_SIGBUS;
2393 }
Nick Pigginc2ec1752009-03-31 15:23:21 -07002394
Nick Piggin56a76f82009-03-31 15:23:23 -07002395out_unlock:
David Chinner54171692007-07-19 17:39:55 +10002396 unlock_page(page);
2397 return ret;
2398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002401 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 * immediately, while under the page lock. So it needs a special end_io
2403 * handler which does not touch the bh after unlocking it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 */
2405static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2406{
Dmitry Monakhov68671f32007-10-16 01:24:47 -07002407 __end_buffer_read_notouch(bh, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410/*
Nick Piggin03158cd2007-10-16 01:25:25 -07002411 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2412 * the page (converting it to circular linked list and taking care of page
2413 * dirty races).
2414 */
2415static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2416{
2417 struct buffer_head *bh;
2418
2419 BUG_ON(!PageLocked(page));
2420
2421 spin_lock(&page->mapping->private_lock);
2422 bh = head;
2423 do {
2424 if (PageDirty(page))
2425 set_buffer_dirty(bh);
2426 if (!bh->b_this_page)
2427 bh->b_this_page = head;
2428 bh = bh->b_this_page;
2429 } while (bh != head);
2430 attach_page_buffers(page, head);
2431 spin_unlock(&page->mapping->private_lock);
2432}
2433
2434/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 * On entry, the page is fully not uptodate.
2436 * On exit the page is fully uptodate in the areas outside (from,to)
2437 */
Nick Piggin03158cd2007-10-16 01:25:25 -07002438int nobh_write_begin(struct file *file, struct address_space *mapping,
2439 loff_t pos, unsigned len, unsigned flags,
2440 struct page **pagep, void **fsdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 get_block_t *get_block)
2442{
Nick Piggin03158cd2007-10-16 01:25:25 -07002443 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 const unsigned blkbits = inode->i_blkbits;
2445 const unsigned blocksize = 1 << blkbits;
Nick Piggina4b06722007-10-16 01:24:48 -07002446 struct buffer_head *head, *bh;
Nick Piggin03158cd2007-10-16 01:25:25 -07002447 struct page *page;
2448 pgoff_t index;
2449 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned block_in_page;
Nick Piggina4b06722007-10-16 01:24:48 -07002451 unsigned block_start, block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 sector_t block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 int nr_reads = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 int ret = 0;
2455 int is_mapped_to_disk = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Nick Piggin03158cd2007-10-16 01:25:25 -07002457 index = pos >> PAGE_CACHE_SHIFT;
2458 from = pos & (PAGE_CACHE_SIZE - 1);
2459 to = from + len;
2460
Nick Piggin54566b22009-01-04 12:00:53 -08002461 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin03158cd2007-10-16 01:25:25 -07002462 if (!page)
2463 return -ENOMEM;
2464 *pagep = page;
2465 *fsdata = NULL;
2466
2467 if (page_has_buffers(page)) {
2468 unlock_page(page);
2469 page_cache_release(page);
2470 *pagep = NULL;
2471 return block_write_begin(file, mapping, pos, len, flags, pagep,
2472 fsdata, get_block);
2473 }
Nick Piggina4b06722007-10-16 01:24:48 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (PageMappedToDisk(page))
2476 return 0;
2477
Nick Piggina4b06722007-10-16 01:24:48 -07002478 /*
2479 * Allocate buffers so that we can keep track of state, and potentially
2480 * attach them to the page if an error occurs. In the common case of
2481 * no error, they will just be freed again without ever being attached
2482 * to the page (which is all OK, because we're under the page lock).
2483 *
2484 * Be careful: the buffer linked list is a NULL terminated one, rather
2485 * than the circular one we're used to.
2486 */
2487 head = alloc_page_buffers(page, blocksize, 0);
Nick Piggin03158cd2007-10-16 01:25:25 -07002488 if (!head) {
2489 ret = -ENOMEM;
2490 goto out_release;
2491 }
Nick Piggina4b06722007-10-16 01:24:48 -07002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /*
2496 * We loop across all blocks in the page, whether or not they are
2497 * part of the affected region. This is so we can discover if the
2498 * page is fully mapped-to-disk.
2499 */
Nick Piggina4b06722007-10-16 01:24:48 -07002500 for (block_start = 0, block_in_page = 0, bh = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 block_start < PAGE_CACHE_SIZE;
Nick Piggina4b06722007-10-16 01:24:48 -07002502 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 int create;
2504
Nick Piggina4b06722007-10-16 01:24:48 -07002505 block_end = block_start + blocksize;
2506 bh->b_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 create = 1;
2508 if (block_start >= to)
2509 create = 0;
2510 ret = get_block(inode, block_in_file + block_in_page,
Nick Piggina4b06722007-10-16 01:24:48 -07002511 bh, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (ret)
2513 goto failed;
Nick Piggina4b06722007-10-16 01:24:48 -07002514 if (!buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 is_mapped_to_disk = 0;
Nick Piggina4b06722007-10-16 01:24:48 -07002516 if (buffer_new(bh))
2517 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2518 if (PageUptodate(page)) {
2519 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 continue;
Nick Piggina4b06722007-10-16 01:24:48 -07002521 }
2522 if (buffer_new(bh) || !buffer_mapped(bh)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002523 zero_user_segments(page, block_start, from,
2524 to, block_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 continue;
2526 }
Nick Piggina4b06722007-10-16 01:24:48 -07002527 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 continue; /* reiserfs does this */
2529 if (block_start < from || block_end > to) {
Nick Piggina4b06722007-10-16 01:24:48 -07002530 lock_buffer(bh);
2531 bh->b_end_io = end_buffer_read_nobh;
2532 submit_bh(READ, bh);
2533 nr_reads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535 }
2536
2537 if (nr_reads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /*
2539 * The page is locked, so these buffers are protected from
2540 * any VM or truncate activity. Hence we don't need to care
2541 * for the buffer_head refcounts.
2542 */
Nick Piggina4b06722007-10-16 01:24:48 -07002543 for (bh = head; bh; bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 wait_on_buffer(bh);
2545 if (!buffer_uptodate(bh))
2546 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548 if (ret)
2549 goto failed;
2550 }
2551
2552 if (is_mapped_to_disk)
2553 SetPageMappedToDisk(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Nick Piggin03158cd2007-10-16 01:25:25 -07002555 *fsdata = head; /* to be released by nobh_write_end */
Nick Piggina4b06722007-10-16 01:24:48 -07002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return 0;
2558
2559failed:
Nick Piggin03158cd2007-10-16 01:25:25 -07002560 BUG_ON(!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
Nick Piggina4b06722007-10-16 01:24:48 -07002562 * Error recovery is a bit difficult. We need to zero out blocks that
2563 * were newly allocated, and dirty them to ensure they get written out.
2564 * Buffers need to be attached to the page at this point, otherwise
2565 * the handling of potential IO errors during writeout would be hard
2566 * (could try doing synchronous writeout, but what if that fails too?)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 */
Nick Piggin03158cd2007-10-16 01:25:25 -07002568 attach_nobh_buffers(page, head);
2569 page_zero_new_buffers(page, from, to);
Nick Piggina4b06722007-10-16 01:24:48 -07002570
Nick Piggin03158cd2007-10-16 01:25:25 -07002571out_release:
2572 unlock_page(page);
2573 page_cache_release(page);
2574 *pagep = NULL;
Nick Piggina4b06722007-10-16 01:24:48 -07002575
Nick Piggin03158cd2007-10-16 01:25:25 -07002576 if (pos + len > inode->i_size)
2577 vmtruncate(inode, inode->i_size);
Nick Piggina4b06722007-10-16 01:24:48 -07002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return ret;
2580}
Nick Piggin03158cd2007-10-16 01:25:25 -07002581EXPORT_SYMBOL(nobh_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Nick Piggin03158cd2007-10-16 01:25:25 -07002583int nobh_write_end(struct file *file, struct address_space *mapping,
2584 loff_t pos, unsigned len, unsigned copied,
2585 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 struct inode *inode = page->mapping->host;
Nick Pigginefdc3132007-10-21 06:57:41 +02002588 struct buffer_head *head = fsdata;
Nick Piggin03158cd2007-10-16 01:25:25 -07002589 struct buffer_head *bh;
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002590 BUG_ON(fsdata != NULL && page_has_buffers(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Dave Kleikampd4cf1092009-02-06 14:59:26 -06002592 if (unlikely(copied < len) && head)
Dmitri Monakhov5b41e742008-03-28 14:15:52 -07002593 attach_nobh_buffers(page, head);
2594 if (page_has_buffers(page))
2595 return generic_write_end(file, mapping, pos, len,
2596 copied, page, fsdata);
Nick Piggina4b06722007-10-16 01:24:48 -07002597
Nick Piggin22c8ca72007-02-20 13:58:09 -08002598 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 set_page_dirty(page);
Nick Piggin03158cd2007-10-16 01:25:25 -07002600 if (pos+copied > inode->i_size) {
2601 i_size_write(inode, pos+copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 mark_inode_dirty(inode);
2603 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002604
2605 unlock_page(page);
2606 page_cache_release(page);
2607
Nick Piggin03158cd2007-10-16 01:25:25 -07002608 while (head) {
2609 bh = head;
2610 head = head->b_this_page;
2611 free_buffer_head(bh);
2612 }
2613
2614 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
Nick Piggin03158cd2007-10-16 01:25:25 -07002616EXPORT_SYMBOL(nobh_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618/*
2619 * nobh_writepage() - based on block_full_write_page() except
2620 * that it tries to operate without attaching bufferheads to
2621 * the page.
2622 */
2623int nobh_writepage(struct page *page, get_block_t *get_block,
2624 struct writeback_control *wbc)
2625{
2626 struct inode * const inode = page->mapping->host;
2627 loff_t i_size = i_size_read(inode);
2628 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2629 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 int ret;
2631
2632 /* Is the page fully inside i_size? */
2633 if (page->index < end_index)
2634 goto out;
2635
2636 /* Is the page fully outside i_size? (truncate in progress) */
2637 offset = i_size & (PAGE_CACHE_SIZE-1);
2638 if (page->index >= end_index+1 || !offset) {
2639 /*
2640 * The page may have dirty, unmapped buffers. For example,
2641 * they may have been added in ext3_writepage(). Make them
2642 * freeable here, so the page does not leak.
2643 */
2644#if 0
2645 /* Not really sure about this - do we need this ? */
2646 if (page->mapping->a_ops->invalidatepage)
2647 page->mapping->a_ops->invalidatepage(page, offset);
2648#endif
2649 unlock_page(page);
2650 return 0; /* don't care */
2651 }
2652
2653 /*
2654 * The page straddles i_size. It must be zeroed out on each and every
2655 * writepage invocation because it may be mmapped. "A file is mapped
2656 * in multiples of the page size. For a file that is not a multiple of
2657 * the page size, the remaining memory is zeroed when mapped, and
2658 * writes to that region are not written out to the file."
2659 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002660 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661out:
2662 ret = mpage_writepage(page, get_block, wbc);
2663 if (ret == -EAGAIN)
2664 ret = __block_write_full_page(inode, page, get_block, wbc);
2665 return ret;
2666}
2667EXPORT_SYMBOL(nobh_writepage);
2668
Nick Piggin03158cd2007-10-16 01:25:25 -07002669int nobh_truncate_page(struct address_space *mapping,
2670 loff_t from, get_block_t *get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2673 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Nick Piggin03158cd2007-10-16 01:25:25 -07002674 unsigned blocksize;
2675 sector_t iblock;
2676 unsigned length, pos;
2677 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct page *page;
Nick Piggin03158cd2007-10-16 01:25:25 -07002679 struct buffer_head map_bh;
2680 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Nick Piggin03158cd2007-10-16 01:25:25 -07002682 blocksize = 1 << inode->i_blkbits;
2683 length = offset & (blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Nick Piggin03158cd2007-10-16 01:25:25 -07002685 /* Block boundary? Nothing to do */
2686 if (!length)
2687 return 0;
2688
2689 length = blocksize - length;
2690 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 page = grab_cache_page(mapping, index);
Nick Piggin03158cd2007-10-16 01:25:25 -07002693 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (!page)
2695 goto out;
2696
Nick Piggin03158cd2007-10-16 01:25:25 -07002697 if (page_has_buffers(page)) {
2698has_buffers:
2699 unlock_page(page);
2700 page_cache_release(page);
2701 return block_truncate_page(mapping, from, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Nick Piggin03158cd2007-10-16 01:25:25 -07002703
2704 /* Find the buffer that contains "offset" */
2705 pos = blocksize;
2706 while (offset >= pos) {
2707 iblock++;
2708 pos += blocksize;
2709 }
2710
2711 err = get_block(inode, iblock, &map_bh, 0);
2712 if (err)
2713 goto unlock;
2714 /* unmapped? It's a hole - nothing to do */
2715 if (!buffer_mapped(&map_bh))
2716 goto unlock;
2717
2718 /* Ok, it's mapped. Make sure it's up-to-date */
2719 if (!PageUptodate(page)) {
2720 err = mapping->a_ops->readpage(NULL, page);
2721 if (err) {
2722 page_cache_release(page);
2723 goto out;
2724 }
2725 lock_page(page);
2726 if (!PageUptodate(page)) {
2727 err = -EIO;
2728 goto unlock;
2729 }
2730 if (page_has_buffers(page))
2731 goto has_buffers;
2732 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002733 zero_user(page, offset, length);
Nick Piggin03158cd2007-10-16 01:25:25 -07002734 set_page_dirty(page);
2735 err = 0;
2736
2737unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 unlock_page(page);
2739 page_cache_release(page);
2740out:
Nick Piggin03158cd2007-10-16 01:25:25 -07002741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743EXPORT_SYMBOL(nobh_truncate_page);
2744
2745int block_truncate_page(struct address_space *mapping,
2746 loff_t from, get_block_t *get_block)
2747{
2748 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2749 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2750 unsigned blocksize;
Andrew Morton54b21a72006-01-08 01:03:05 -08002751 sector_t iblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 unsigned length, pos;
2753 struct inode *inode = mapping->host;
2754 struct page *page;
2755 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 int err;
2757
2758 blocksize = 1 << inode->i_blkbits;
2759 length = offset & (blocksize - 1);
2760
2761 /* Block boundary? Nothing to do */
2762 if (!length)
2763 return 0;
2764
2765 length = blocksize - length;
Andrew Morton54b21a72006-01-08 01:03:05 -08002766 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 page = grab_cache_page(mapping, index);
2769 err = -ENOMEM;
2770 if (!page)
2771 goto out;
2772
2773 if (!page_has_buffers(page))
2774 create_empty_buffers(page, blocksize, 0);
2775
2776 /* Find the buffer that contains "offset" */
2777 bh = page_buffers(page);
2778 pos = blocksize;
2779 while (offset >= pos) {
2780 bh = bh->b_this_page;
2781 iblock++;
2782 pos += blocksize;
2783 }
2784
2785 err = 0;
2786 if (!buffer_mapped(bh)) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002787 WARN_ON(bh->b_size != blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 err = get_block(inode, iblock, bh, 0);
2789 if (err)
2790 goto unlock;
2791 /* unmapped? It's a hole - nothing to do */
2792 if (!buffer_mapped(bh))
2793 goto unlock;
2794 }
2795
2796 /* Ok, it's mapped. Make sure it's up-to-date */
2797 if (PageUptodate(page))
2798 set_buffer_uptodate(bh);
2799
David Chinner33a266d2007-02-12 00:51:41 -08002800 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 err = -EIO;
2802 ll_rw_block(READ, 1, &bh);
2803 wait_on_buffer(bh);
2804 /* Uhhuh. Read error. Complain and punt. */
2805 if (!buffer_uptodate(bh))
2806 goto unlock;
2807 }
2808
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002809 zero_user(page, offset, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 mark_buffer_dirty(bh);
2811 err = 0;
2812
2813unlock:
2814 unlock_page(page);
2815 page_cache_release(page);
2816out:
2817 return err;
2818}
2819
2820/*
2821 * The generic ->writepage function for buffer-backed address_spaces
2822 */
2823int block_write_full_page(struct page *page, get_block_t *get_block,
2824 struct writeback_control *wbc)
2825{
2826 struct inode * const inode = page->mapping->host;
2827 loff_t i_size = i_size_read(inode);
2828 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2829 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 /* Is the page fully inside i_size? */
2832 if (page->index < end_index)
2833 return __block_write_full_page(inode, page, get_block, wbc);
2834
2835 /* Is the page fully outside i_size? (truncate in progress) */
2836 offset = i_size & (PAGE_CACHE_SIZE-1);
2837 if (page->index >= end_index+1 || !offset) {
2838 /*
2839 * The page may have dirty, unmapped buffers. For example,
2840 * they may have been added in ext3_writepage(). Make them
2841 * freeable here, so the page does not leak.
2842 */
Jan Karaaaa40592005-10-30 15:00:16 -08002843 do_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 unlock_page(page);
2845 return 0; /* don't care */
2846 }
2847
2848 /*
2849 * The page straddles i_size. It must be zeroed out on each and every
2850 * writepage invokation because it may be mmapped. "A file is mapped
2851 * in multiples of the page size. For a file that is not a multiple of
2852 * the page size, the remaining memory is zeroed when mapped, and
2853 * writes to that region are not written out to the file."
2854 */
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002855 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return __block_write_full_page(inode, page, get_block, wbc);
2857}
2858
2859sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2860 get_block_t *get_block)
2861{
2862 struct buffer_head tmp;
2863 struct inode *inode = mapping->host;
2864 tmp.b_state = 0;
2865 tmp.b_blocknr = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -08002866 tmp.b_size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 get_block(inode, block, &tmp, 0);
2868 return tmp.b_blocknr;
2869}
2870
NeilBrown6712ecf2007-09-27 12:47:43 +02002871static void end_bio_bh_io_sync(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 struct buffer_head *bh = bio->bi_private;
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (err == -EOPNOTSUPP) {
2876 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
2877 set_bit(BH_Eopnotsupp, &bh->b_state);
2878 }
2879
Keith Mannthey08bafc02008-11-25 10:24:35 +01002880 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2881 set_bit(BH_Quiet, &bh->b_state);
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2884 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
2887int submit_bh(int rw, struct buffer_head * bh)
2888{
2889 struct bio *bio;
2890 int ret = 0;
2891
2892 BUG_ON(!buffer_locked(bh));
2893 BUG_ON(!buffer_mapped(bh));
2894 BUG_ON(!bh->b_end_io);
2895
Jens Axboe48fd4f92008-08-22 10:00:36 +02002896 /*
2897 * Mask in barrier bit for a write (could be either a WRITE or a
2898 * WRITE_SYNC
2899 */
2900 if (buffer_ordered(bh) && (rw & WRITE))
2901 rw |= WRITE_BARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 /*
Jens Axboe48fd4f92008-08-22 10:00:36 +02002904 * Only clear out a write error when rewriting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 */
Jens Axboe48fd4f92008-08-22 10:00:36 +02002906 if (test_set_buffer_req(bh) && (rw & WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 clear_buffer_write_io_error(bh);
2908
2909 /*
2910 * from here on down, it's all bio -- do the initial mapping,
2911 * submit_bio -> generic_make_request may further map this bio around
2912 */
2913 bio = bio_alloc(GFP_NOIO, 1);
2914
2915 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2916 bio->bi_bdev = bh->b_bdev;
2917 bio->bi_io_vec[0].bv_page = bh->b_page;
2918 bio->bi_io_vec[0].bv_len = bh->b_size;
2919 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2920
2921 bio->bi_vcnt = 1;
2922 bio->bi_idx = 0;
2923 bio->bi_size = bh->b_size;
2924
2925 bio->bi_end_io = end_bio_bh_io_sync;
2926 bio->bi_private = bh;
2927
2928 bio_get(bio);
2929 submit_bio(rw, bio);
2930
2931 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2932 ret = -EOPNOTSUPP;
2933
2934 bio_put(bio);
2935 return ret;
2936}
2937
2938/**
2939 * ll_rw_block: low-level access to block devices (DEPRECATED)
Jan Karaa7662232005-09-06 15:19:10 -07002940 * @rw: whether to %READ or %WRITE or %SWRITE or maybe %READA (readahead)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 * @nr: number of &struct buffer_heads in the array
2942 * @bhs: array of pointers to &struct buffer_head
2943 *
Jan Karaa7662232005-09-06 15:19:10 -07002944 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
2945 * requests an I/O operation on them, either a %READ or a %WRITE. The third
2946 * %SWRITE is like %WRITE only we make sure that the *current* data in buffers
2947 * are sent to disk. The fourth %READA option is described in the documentation
2948 * for generic_make_request() which ll_rw_block() calls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 *
2950 * This function drops any buffer that it cannot get a lock on (with the
Jan Karaa7662232005-09-06 15:19:10 -07002951 * BH_Lock state bit) unless SWRITE is required, any buffer that appears to be
2952 * clean when doing a write request, and any buffer that appears to be
2953 * up-to-date when doing read request. Further it marks as clean buffers that
2954 * are processed for writing (the buffer cache won't assume that they are
2955 * actually clean until the buffer gets unlocked).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 *
2957 * ll_rw_block sets b_end_io to simple completion handler that marks
2958 * the buffer up-to-date (if approriate), unlocks the buffer and wakes
2959 * any waiters.
2960 *
2961 * All of the buffers must be for the same device, and must also be a
2962 * multiple of the current approved size for the device.
2963 */
2964void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
2965{
2966 int i;
2967
2968 for (i = 0; i < nr; i++) {
2969 struct buffer_head *bh = bhs[i];
2970
Jens Axboe9cf6b722009-04-06 14:48:03 +02002971 if (rw == SWRITE || rw == SWRITE_SYNC || rw == SWRITE_SYNC_PLUG)
Jan Karaa7662232005-09-06 15:19:10 -07002972 lock_buffer(bh);
Nick Pigginca5de402008-08-02 12:02:13 +02002973 else if (!trylock_buffer(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 continue;
2975
Jens Axboe9cf6b722009-04-06 14:48:03 +02002976 if (rw == WRITE || rw == SWRITE || rw == SWRITE_SYNC ||
2977 rw == SWRITE_SYNC_PLUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (test_clear_buffer_dirty(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07002979 bh->b_end_io = end_buffer_write_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08002980 get_bh(bh);
Jens Axboe18ce3752008-07-01 09:07:34 +02002981 if (rw == SWRITE_SYNC)
2982 submit_bh(WRITE_SYNC, bh);
2983 else
2984 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 continue;
2986 }
2987 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (!buffer_uptodate(bh)) {
akpm@osdl.org76c30732005-04-16 15:24:07 -07002989 bh->b_end_io = end_buffer_read_sync;
OGAWA Hirofumie60e5c52006-02-03 03:04:43 -08002990 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 submit_bh(rw, bh);
2992 continue;
2993 }
2994 }
2995 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997}
2998
2999/*
3000 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3001 * and then start new I/O and then wait upon it. The caller must have a ref on
3002 * the buffer_head.
3003 */
3004int sync_dirty_buffer(struct buffer_head *bh)
3005{
3006 int ret = 0;
3007
3008 WARN_ON(atomic_read(&bh->b_count) < 1);
3009 lock_buffer(bh);
3010 if (test_clear_buffer_dirty(bh)) {
3011 get_bh(bh);
3012 bh->b_end_io = end_buffer_write_sync;
Jens Axboe78f707b2009-02-17 13:59:08 +01003013 ret = submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 wait_on_buffer(bh);
3015 if (buffer_eopnotsupp(bh)) {
3016 clear_buffer_eopnotsupp(bh);
3017 ret = -EOPNOTSUPP;
3018 }
3019 if (!ret && !buffer_uptodate(bh))
3020 ret = -EIO;
3021 } else {
3022 unlock_buffer(bh);
3023 }
3024 return ret;
3025}
3026
3027/*
3028 * try_to_free_buffers() checks if all the buffers on this particular page
3029 * are unused, and releases them if so.
3030 *
3031 * Exclusion against try_to_free_buffers may be obtained by either
3032 * locking the page or by holding its mapping's private_lock.
3033 *
3034 * If the page is dirty but all the buffers are clean then we need to
3035 * be sure to mark the page clean as well. This is because the page
3036 * may be against a block device, and a later reattachment of buffers
3037 * to a dirty page will set *all* buffers dirty. Which would corrupt
3038 * filesystem data on the same device.
3039 *
3040 * The same applies to regular filesystem pages: if all the buffers are
3041 * clean then we set the page clean and proceed. To do that, we require
3042 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3043 * private_lock.
3044 *
3045 * try_to_free_buffers() is non-blocking.
3046 */
3047static inline int buffer_busy(struct buffer_head *bh)
3048{
3049 return atomic_read(&bh->b_count) |
3050 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3051}
3052
3053static int
3054drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3055{
3056 struct buffer_head *head = page_buffers(page);
3057 struct buffer_head *bh;
3058
3059 bh = head;
3060 do {
akpm@osdl.orgde7d5a32005-05-01 08:58:39 -07003061 if (buffer_write_io_error(bh) && page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 set_bit(AS_EIO, &page->mapping->flags);
3063 if (buffer_busy(bh))
3064 goto failed;
3065 bh = bh->b_this_page;
3066 } while (bh != head);
3067
3068 do {
3069 struct buffer_head *next = bh->b_this_page;
3070
Jan Kara535ee2f2008-02-08 04:21:59 -08003071 if (bh->b_assoc_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 __remove_assoc_queue(bh);
3073 bh = next;
3074 } while (bh != head);
3075 *buffers_to_free = head;
3076 __clear_page_buffers(page);
3077 return 1;
3078failed:
3079 return 0;
3080}
3081
3082int try_to_free_buffers(struct page *page)
3083{
3084 struct address_space * const mapping = page->mapping;
3085 struct buffer_head *buffers_to_free = NULL;
3086 int ret = 0;
3087
3088 BUG_ON(!PageLocked(page));
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003089 if (PageWriteback(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return 0;
3091
3092 if (mapping == NULL) { /* can this still happen? */
3093 ret = drop_buffers(page, &buffers_to_free);
3094 goto out;
3095 }
3096
3097 spin_lock(&mapping->private_lock);
3098 ret = drop_buffers(page, &buffers_to_free);
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003099
3100 /*
3101 * If the filesystem writes its buffers by hand (eg ext3)
3102 * then we can have clean buffers against a dirty page. We
3103 * clean the page here; otherwise the VM will never notice
3104 * that the filesystem did any IO at all.
3105 *
3106 * Also, during truncate, discard_buffer will have marked all
3107 * the page's buffers clean. We discover that here and clean
3108 * the page also.
Nick Piggin87df7242007-01-30 14:36:27 +11003109 *
3110 * private_lock must be held over this entire operation in order
3111 * to synchronise against __set_page_dirty_buffers and prevent the
3112 * dirty bit from being lost.
Linus Torvaldsecdfc972007-01-26 12:47:06 -08003113 */
3114 if (ret)
3115 cancel_dirty_page(page, PAGE_CACHE_SIZE);
Nick Piggin87df7242007-01-30 14:36:27 +11003116 spin_unlock(&mapping->private_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117out:
3118 if (buffers_to_free) {
3119 struct buffer_head *bh = buffers_to_free;
3120
3121 do {
3122 struct buffer_head *next = bh->b_this_page;
3123 free_buffer_head(bh);
3124 bh = next;
3125 } while (bh != buffers_to_free);
3126 }
3127 return ret;
3128}
3129EXPORT_SYMBOL(try_to_free_buffers);
3130
NeilBrown3978d712006-03-26 01:37:17 -08003131void block_sync_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
3133 struct address_space *mapping;
3134
3135 smp_mb();
3136 mapping = page_mapping(page);
3137 if (mapping)
3138 blk_run_backing_dev(mapping->backing_dev_info, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
3141/*
3142 * There are no bdflush tunables left. But distributions are
3143 * still running obsolete flush daemons, so we terminate them here.
3144 *
3145 * Use of bdflush() is deprecated and will be removed in a future kernel.
3146 * The `pdflush' kernel threads fully replace bdflush daemons and this call.
3147 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01003148SYSCALL_DEFINE2(bdflush, int, func, long, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 static int msg_count;
3151
3152 if (!capable(CAP_SYS_ADMIN))
3153 return -EPERM;
3154
3155 if (msg_count < 5) {
3156 msg_count++;
3157 printk(KERN_INFO
3158 "warning: process `%s' used the obsolete bdflush"
3159 " system call\n", current->comm);
3160 printk(KERN_INFO "Fix your initscripts?\n");
3161 }
3162
3163 if (func == 1)
3164 do_exit(0);
3165 return 0;
3166}
3167
3168/*
3169 * Buffer-head allocation
3170 */
Christoph Lametere18b8902006-12-06 20:33:20 -08003171static struct kmem_cache *bh_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173/*
3174 * Once the number of bh's in the machine exceeds this level, we start
3175 * stripping them in writeback.
3176 */
3177static int max_buffer_heads;
3178
3179int buffer_heads_over_limit;
3180
3181struct bh_accounting {
3182 int nr; /* Number of live bh's */
3183 int ratelimit; /* Limit cacheline bouncing */
3184};
3185
3186static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3187
3188static void recalc_bh_state(void)
3189{
3190 int i;
3191 int tot = 0;
3192
3193 if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
3194 return;
3195 __get_cpu_var(bh_accounting).ratelimit = 0;
Eric Dumazet8a143422006-03-24 03:18:10 -08003196 for_each_online_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 tot += per_cpu(bh_accounting, i).nr;
3198 buffer_heads_over_limit = (tot > max_buffer_heads);
3199}
3200
Al Virodd0fc662005-10-07 07:46:04 +01003201struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
Christoph Lameter488514d2008-04-28 02:12:05 -07003203 struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (ret) {
Christoph Lametera35afb82007-05-16 22:10:57 -07003205 INIT_LIST_HEAD(&ret->b_assoc_buffers);
Coywolf Qi Hunt736c7b82005-09-06 15:18:17 -07003206 get_cpu_var(bh_accounting).nr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 recalc_bh_state();
Coywolf Qi Hunt736c7b82005-09-06 15:18:17 -07003208 put_cpu_var(bh_accounting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 return ret;
3211}
3212EXPORT_SYMBOL(alloc_buffer_head);
3213
3214void free_buffer_head(struct buffer_head *bh)
3215{
3216 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3217 kmem_cache_free(bh_cachep, bh);
Coywolf Qi Hunt736c7b82005-09-06 15:18:17 -07003218 get_cpu_var(bh_accounting).nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 recalc_bh_state();
Coywolf Qi Hunt736c7b82005-09-06 15:18:17 -07003220 put_cpu_var(bh_accounting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222EXPORT_SYMBOL(free_buffer_head);
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224static void buffer_exit_cpu(int cpu)
3225{
3226 int i;
3227 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3228
3229 for (i = 0; i < BH_LRU_SIZE; i++) {
3230 brelse(b->bhs[i]);
3231 b->bhs[i] = NULL;
3232 }
Eric Dumazet8a143422006-03-24 03:18:10 -08003233 get_cpu_var(bh_accounting).nr += per_cpu(bh_accounting, cpu).nr;
3234 per_cpu(bh_accounting, cpu).nr = 0;
3235 put_cpu_var(bh_accounting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238static int buffer_cpu_notify(struct notifier_block *self,
3239 unsigned long action, void *hcpu)
3240{
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003241 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 buffer_exit_cpu((unsigned long)hcpu);
3243 return NOTIFY_OK;
3244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003246/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003247 * bh_uptodate_or_lock - Test whether the buffer is uptodate
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003248 * @bh: struct buffer_head
3249 *
3250 * Return true if the buffer is up-to-date and false,
3251 * with the buffer locked, if not.
3252 */
3253int bh_uptodate_or_lock(struct buffer_head *bh)
3254{
3255 if (!buffer_uptodate(bh)) {
3256 lock_buffer(bh);
3257 if (!buffer_uptodate(bh))
3258 return 0;
3259 unlock_buffer(bh);
3260 }
3261 return 1;
3262}
3263EXPORT_SYMBOL(bh_uptodate_or_lock);
3264
3265/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07003266 * bh_submit_read - Submit a locked buffer for reading
Aneesh Kumar K.V389d1b02008-01-28 23:58:26 -05003267 * @bh: struct buffer_head
3268 *
3269 * Returns zero on success and -EIO on error.
3270 */
3271int bh_submit_read(struct buffer_head *bh)
3272{
3273 BUG_ON(!buffer_locked(bh));
3274
3275 if (buffer_uptodate(bh)) {
3276 unlock_buffer(bh);
3277 return 0;
3278 }
3279
3280 get_bh(bh);
3281 bh->b_end_io = end_buffer_read_sync;
3282 submit_bh(READ, bh);
3283 wait_on_buffer(bh);
3284 if (buffer_uptodate(bh))
3285 return 0;
3286 return -EIO;
3287}
3288EXPORT_SYMBOL(bh_submit_read);
3289
Christoph Lameterb98938c2008-02-04 22:28:36 -08003290static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003291init_buffer_head(void *data)
Christoph Lameterb98938c2008-02-04 22:28:36 -08003292{
3293 struct buffer_head *bh = data;
3294
3295 memset(bh, 0, sizeof(*bh));
3296 INIT_LIST_HEAD(&bh->b_assoc_buffers);
3297}
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299void __init buffer_init(void)
3300{
3301 int nrpages;
3302
Christoph Lameterb98938c2008-02-04 22:28:36 -08003303 bh_cachep = kmem_cache_create("buffer_head",
3304 sizeof(struct buffer_head), 0,
3305 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3306 SLAB_MEM_SPREAD),
3307 init_buffer_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 /*
3310 * Limit the bh occupancy to 10% of ZONE_NORMAL
3311 */
3312 nrpages = (nr_free_buffer_pages() * 10) / 100;
3313 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3314 hotcpu_notifier(buffer_cpu_notify, 0);
3315}
3316
3317EXPORT_SYMBOL(__bforget);
3318EXPORT_SYMBOL(__brelse);
3319EXPORT_SYMBOL(__wait_on_buffer);
3320EXPORT_SYMBOL(block_commit_write);
3321EXPORT_SYMBOL(block_prepare_write);
David Chinner54171692007-07-19 17:39:55 +10003322EXPORT_SYMBOL(block_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323EXPORT_SYMBOL(block_read_full_page);
3324EXPORT_SYMBOL(block_sync_page);
3325EXPORT_SYMBOL(block_truncate_page);
3326EXPORT_SYMBOL(block_write_full_page);
Nick Piggin89e10782007-10-16 01:25:07 -07003327EXPORT_SYMBOL(cont_write_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328EXPORT_SYMBOL(end_buffer_read_sync);
3329EXPORT_SYMBOL(end_buffer_write_sync);
3330EXPORT_SYMBOL(file_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331EXPORT_SYMBOL(generic_block_bmap);
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -08003332EXPORT_SYMBOL(generic_cont_expand_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333EXPORT_SYMBOL(init_buffer);
3334EXPORT_SYMBOL(invalidate_bdev);
3335EXPORT_SYMBOL(ll_rw_block);
3336EXPORT_SYMBOL(mark_buffer_dirty);
3337EXPORT_SYMBOL(submit_bh);
3338EXPORT_SYMBOL(sync_dirty_buffer);
3339EXPORT_SYMBOL(unlock_buffer);