1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/buffer.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 2002 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Removed a lot of unnecessary code and simplified things now that 121da177e4SLinus Torvalds * the buffer cache isn't our primary cache - Andrew Tridgell 12/96 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Speed up hash, lru, and free list operations. Use gfp() for allocating 151da177e4SLinus Torvalds * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added 32k buffer block sizes - these are required older ARM systems. - RMK 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de> 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <linux/kernel.h> 23f361bf4aSIngo Molnar #include <linux/sched/signal.h> 241da177e4SLinus Torvalds #include <linux/syscalls.h> 251da177e4SLinus Torvalds #include <linux/fs.h> 26ae259a9cSChristoph Hellwig #include <linux/iomap.h> 271da177e4SLinus Torvalds #include <linux/mm.h> 281da177e4SLinus Torvalds #include <linux/percpu.h> 291da177e4SLinus Torvalds #include <linux/slab.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 311da177e4SLinus Torvalds #include <linux/blkdev.h> 321da177e4SLinus Torvalds #include <linux/file.h> 331da177e4SLinus Torvalds #include <linux/quotaops.h> 341da177e4SLinus Torvalds #include <linux/highmem.h> 35630d9c47SPaul Gortmaker #include <linux/export.h> 36bafc0dbaSTejun Heo #include <linux/backing-dev.h> 371da177e4SLinus Torvalds #include <linux/writeback.h> 381da177e4SLinus Torvalds #include <linux/hash.h> 391da177e4SLinus Torvalds #include <linux/suspend.h> 401da177e4SLinus Torvalds #include <linux/buffer_head.h> 4155e829afSAndrew Morton #include <linux/task_io_accounting_ops.h> 421da177e4SLinus Torvalds #include <linux/bio.h> 431da177e4SLinus Torvalds #include <linux/cpu.h> 441da177e4SLinus Torvalds #include <linux/bitops.h> 451da177e4SLinus Torvalds #include <linux/mpage.h> 46fb1c8f93SIngo Molnar #include <linux/bit_spinlock.h> 4729f3ad7dSJan Kara #include <linux/pagevec.h> 48f745c6f5SShakeel Butt #include <linux/sched/mm.h> 495305cb83STejun Heo #include <trace/events/block.h> 5031fb992cSEric Biggers #include <linux/fscrypt.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static int fsync_buffers_list(spinlock_t *lock, struct list_head *list); 532a222ca9SMike Christie static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh, 548e8f9298SJens Axboe enum rw_hint hint, struct writeback_control *wbc); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers) 571da177e4SLinus Torvalds 58f0059afdSTejun Heo inline void touch_buffer(struct buffer_head *bh) 59f0059afdSTejun Heo { 605305cb83STejun Heo trace_block_touch_buffer(bh); 61f0059afdSTejun Heo mark_page_accessed(bh->b_page); 62f0059afdSTejun Heo } 63f0059afdSTejun Heo EXPORT_SYMBOL(touch_buffer); 64f0059afdSTejun Heo 65fc9b52cdSHarvey Harrison void __lock_buffer(struct buffer_head *bh) 661da177e4SLinus Torvalds { 6774316201SNeilBrown wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE); 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds EXPORT_SYMBOL(__lock_buffer); 701da177e4SLinus Torvalds 71fc9b52cdSHarvey Harrison void unlock_buffer(struct buffer_head *bh) 721da177e4SLinus Torvalds { 7351b07fc3SNick Piggin clear_bit_unlock(BH_Lock, &bh->b_state); 744e857c58SPeter Zijlstra smp_mb__after_atomic(); 751da177e4SLinus Torvalds wake_up_bit(&bh->b_state, BH_Lock); 761da177e4SLinus Torvalds } 771fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(unlock_buffer); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds /* 80b4597226SMel Gorman * Returns if the page has dirty or writeback buffers. If all the buffers 81b4597226SMel Gorman * are unlocked and clean then the PageDirty information is stale. If 82b4597226SMel Gorman * any of the pages are locked, it is assumed they are locked for IO. 83b4597226SMel Gorman */ 84b4597226SMel Gorman void buffer_check_dirty_writeback(struct page *page, 85b4597226SMel Gorman bool *dirty, bool *writeback) 86b4597226SMel Gorman { 87b4597226SMel Gorman struct buffer_head *head, *bh; 88b4597226SMel Gorman *dirty = false; 89b4597226SMel Gorman *writeback = false; 90b4597226SMel Gorman 91b4597226SMel Gorman BUG_ON(!PageLocked(page)); 92b4597226SMel Gorman 93b4597226SMel Gorman if (!page_has_buffers(page)) 94b4597226SMel Gorman return; 95b4597226SMel Gorman 96b4597226SMel Gorman if (PageWriteback(page)) 97b4597226SMel Gorman *writeback = true; 98b4597226SMel Gorman 99b4597226SMel Gorman head = page_buffers(page); 100b4597226SMel Gorman bh = head; 101b4597226SMel Gorman do { 102b4597226SMel Gorman if (buffer_locked(bh)) 103b4597226SMel Gorman *writeback = true; 104b4597226SMel Gorman 105b4597226SMel Gorman if (buffer_dirty(bh)) 106b4597226SMel Gorman *dirty = true; 107b4597226SMel Gorman 108b4597226SMel Gorman bh = bh->b_this_page; 109b4597226SMel Gorman } while (bh != head); 110b4597226SMel Gorman } 111b4597226SMel Gorman EXPORT_SYMBOL(buffer_check_dirty_writeback); 112b4597226SMel Gorman 113b4597226SMel Gorman /* 1141da177e4SLinus Torvalds * Block until a buffer comes unlocked. This doesn't stop it 1151da177e4SLinus Torvalds * from becoming locked again - you have to lock it yourself 1161da177e4SLinus Torvalds * if you want to preserve its state. 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds void __wait_on_buffer(struct buffer_head * bh) 1191da177e4SLinus Torvalds { 12074316201SNeilBrown wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE); 1211da177e4SLinus Torvalds } 1221fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__wait_on_buffer); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds static void 1251da177e4SLinus Torvalds __clear_page_buffers(struct page *page) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds ClearPagePrivate(page); 1284c21e2f2SHugh Dickins set_page_private(page, 0); 12909cbfeafSKirill A. Shutemov put_page(page); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 132b744c2acSRobert Elliott static void buffer_io_error(struct buffer_head *bh, char *msg) 1331da177e4SLinus Torvalds { 134432f16e6SRobert Elliott if (!test_bit(BH_Quiet, &bh->b_state)) 135432f16e6SRobert Elliott printk_ratelimited(KERN_ERR 136a1c6f057SDmitry Monakhov "Buffer I/O error on dev %pg, logical block %llu%s\n", 137a1c6f057SDmitry Monakhov bh->b_bdev, (unsigned long long)bh->b_blocknr, msg); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* 14168671f35SDmitry Monakhov * End-of-IO handler helper function which does not touch the bh after 14268671f35SDmitry Monakhov * unlocking it. 14368671f35SDmitry Monakhov * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but 14468671f35SDmitry Monakhov * a race there is benign: unlock_buffer() only use the bh's address for 14568671f35SDmitry Monakhov * hashing after unlocking the buffer, so it doesn't actually touch the bh 14668671f35SDmitry Monakhov * itself. 1471da177e4SLinus Torvalds */ 14868671f35SDmitry Monakhov static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds if (uptodate) { 1511da177e4SLinus Torvalds set_buffer_uptodate(bh); 1521da177e4SLinus Torvalds } else { 15370246286SChristoph Hellwig /* This happens, due to failed read-ahead attempts. */ 1541da177e4SLinus Torvalds clear_buffer_uptodate(bh); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds unlock_buffer(bh); 15768671f35SDmitry Monakhov } 15868671f35SDmitry Monakhov 15968671f35SDmitry Monakhov /* 16068671f35SDmitry Monakhov * Default synchronous end-of-IO handler.. Just mark it up-to-date and 16168671f35SDmitry Monakhov * unlock the buffer. This is what ll_rw_block uses too. 16268671f35SDmitry Monakhov */ 16368671f35SDmitry Monakhov void end_buffer_read_sync(struct buffer_head *bh, int uptodate) 16468671f35SDmitry Monakhov { 16568671f35SDmitry Monakhov __end_buffer_read_notouch(bh, uptodate); 1661da177e4SLinus Torvalds put_bh(bh); 1671da177e4SLinus Torvalds } 1681fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_read_sync); 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds void end_buffer_write_sync(struct buffer_head *bh, int uptodate) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds if (uptodate) { 1731da177e4SLinus Torvalds set_buffer_uptodate(bh); 1741da177e4SLinus Torvalds } else { 175b744c2acSRobert Elliott buffer_io_error(bh, ", lost sync page write"); 17687354e5dSJeff Layton mark_buffer_write_io_error(bh); 1771da177e4SLinus Torvalds clear_buffer_uptodate(bh); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds unlock_buffer(bh); 1801da177e4SLinus Torvalds put_bh(bh); 1811da177e4SLinus Torvalds } 1821fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_write_sync); 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* 1851da177e4SLinus Torvalds * Various filesystems appear to want __find_get_block to be non-blocking. 1861da177e4SLinus Torvalds * But it's the page lock which protects the buffers. To get around this, 1871da177e4SLinus Torvalds * we get exclusion from try_to_free_buffers with the blockdev mapping's 1881da177e4SLinus Torvalds * private_lock. 1891da177e4SLinus Torvalds * 190b93b0163SMatthew Wilcox * Hack idea: for the blockdev mapping, private_lock contention 1911da177e4SLinus Torvalds * may be quite high. This code could TryLock the page, and if that 192b93b0163SMatthew Wilcox * succeeds, there is no need to take private_lock. 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds static struct buffer_head * 195385fd4c5SCoywolf Qi Hunt __find_get_block_slow(struct block_device *bdev, sector_t block) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds struct inode *bd_inode = bdev->bd_inode; 1981da177e4SLinus Torvalds struct address_space *bd_mapping = bd_inode->i_mapping; 1991da177e4SLinus Torvalds struct buffer_head *ret = NULL; 2001da177e4SLinus Torvalds pgoff_t index; 2011da177e4SLinus Torvalds struct buffer_head *bh; 2021da177e4SLinus Torvalds struct buffer_head *head; 2031da177e4SLinus Torvalds struct page *page; 2041da177e4SLinus Torvalds int all_mapped = 1; 20543636c80STetsuo Handa static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1); 2061da177e4SLinus Torvalds 20709cbfeafSKirill A. Shutemov index = block >> (PAGE_SHIFT - bd_inode->i_blkbits); 2082457aec6SMel Gorman page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED); 2091da177e4SLinus Torvalds if (!page) 2101da177e4SLinus Torvalds goto out; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds spin_lock(&bd_mapping->private_lock); 2131da177e4SLinus Torvalds if (!page_has_buffers(page)) 2141da177e4SLinus Torvalds goto out_unlock; 2151da177e4SLinus Torvalds head = page_buffers(page); 2161da177e4SLinus Torvalds bh = head; 2171da177e4SLinus Torvalds do { 21897f76d3dSNikanth Karthikesan if (!buffer_mapped(bh)) 21997f76d3dSNikanth Karthikesan all_mapped = 0; 22097f76d3dSNikanth Karthikesan else if (bh->b_blocknr == block) { 2211da177e4SLinus Torvalds ret = bh; 2221da177e4SLinus Torvalds get_bh(bh); 2231da177e4SLinus Torvalds goto out_unlock; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds bh = bh->b_this_page; 2261da177e4SLinus Torvalds } while (bh != head); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* we might be here because some of the buffers on this page are 2291da177e4SLinus Torvalds * not mapped. This is due to various races between 2301da177e4SLinus Torvalds * file io on the block device and getblk. It gets dealt with 2311da177e4SLinus Torvalds * elsewhere, don't buffer_error if we had some unmapped buffers 2321da177e4SLinus Torvalds */ 23343636c80STetsuo Handa ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE); 23443636c80STetsuo Handa if (all_mapped && __ratelimit(&last_warned)) { 23543636c80STetsuo Handa printk("__find_get_block_slow() failed. block=%llu, " 23643636c80STetsuo Handa "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, " 23743636c80STetsuo Handa "device %pg blocksize: %d\n", 238205f87f6SBadari Pulavarty (unsigned long long)block, 23943636c80STetsuo Handa (unsigned long long)bh->b_blocknr, 24043636c80STetsuo Handa bh->b_state, bh->b_size, bdev, 24172a2ebd8STao Ma 1 << bd_inode->i_blkbits); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds out_unlock: 2441da177e4SLinus Torvalds spin_unlock(&bd_mapping->private_lock); 24509cbfeafSKirill A. Shutemov put_page(page); 2461da177e4SLinus Torvalds out: 2471da177e4SLinus Torvalds return ret; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds static void end_buffer_async_read(struct buffer_head *bh, int uptodate) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds unsigned long flags; 253a3972203SNick Piggin struct buffer_head *first; 2541da177e4SLinus Torvalds struct buffer_head *tmp; 2551da177e4SLinus Torvalds struct page *page; 2561da177e4SLinus Torvalds int page_uptodate = 1; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds BUG_ON(!buffer_async_read(bh)); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds page = bh->b_page; 2611da177e4SLinus Torvalds if (uptodate) { 2621da177e4SLinus Torvalds set_buffer_uptodate(bh); 2631da177e4SLinus Torvalds } else { 2641da177e4SLinus Torvalds clear_buffer_uptodate(bh); 265b744c2acSRobert Elliott buffer_io_error(bh, ", async page read"); 2661da177e4SLinus Torvalds SetPageError(page); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * Be _very_ careful from here on. Bad things can happen if 2711da177e4SLinus Torvalds * two buffer heads end IO at almost the same time and both 2721da177e4SLinus Torvalds * decide that the page is now completely done. 2731da177e4SLinus Torvalds */ 274a3972203SNick Piggin first = page_buffers(page); 275a3972203SNick Piggin local_irq_save(flags); 276a3972203SNick Piggin bit_spin_lock(BH_Uptodate_Lock, &first->b_state); 2771da177e4SLinus Torvalds clear_buffer_async_read(bh); 2781da177e4SLinus Torvalds unlock_buffer(bh); 2791da177e4SLinus Torvalds tmp = bh; 2801da177e4SLinus Torvalds do { 2811da177e4SLinus Torvalds if (!buffer_uptodate(tmp)) 2821da177e4SLinus Torvalds page_uptodate = 0; 2831da177e4SLinus Torvalds if (buffer_async_read(tmp)) { 2841da177e4SLinus Torvalds BUG_ON(!buffer_locked(tmp)); 2851da177e4SLinus Torvalds goto still_busy; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds tmp = tmp->b_this_page; 2881da177e4SLinus Torvalds } while (tmp != bh); 289a3972203SNick Piggin bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); 290a3972203SNick Piggin local_irq_restore(flags); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* 2931da177e4SLinus Torvalds * If none of the buffers had errors and they are all 2941da177e4SLinus Torvalds * uptodate then we can set the page uptodate. 2951da177e4SLinus Torvalds */ 2961da177e4SLinus Torvalds if (page_uptodate && !PageError(page)) 2971da177e4SLinus Torvalds SetPageUptodate(page); 2981da177e4SLinus Torvalds unlock_page(page); 2991da177e4SLinus Torvalds return; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds still_busy: 302a3972203SNick Piggin bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); 303a3972203SNick Piggin local_irq_restore(flags); 3041da177e4SLinus Torvalds return; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 30731fb992cSEric Biggers struct decrypt_bh_ctx { 30831fb992cSEric Biggers struct work_struct work; 30931fb992cSEric Biggers struct buffer_head *bh; 31031fb992cSEric Biggers }; 31131fb992cSEric Biggers 31231fb992cSEric Biggers static void decrypt_bh(struct work_struct *work) 31331fb992cSEric Biggers { 31431fb992cSEric Biggers struct decrypt_bh_ctx *ctx = 31531fb992cSEric Biggers container_of(work, struct decrypt_bh_ctx, work); 31631fb992cSEric Biggers struct buffer_head *bh = ctx->bh; 31731fb992cSEric Biggers int err; 31831fb992cSEric Biggers 31931fb992cSEric Biggers err = fscrypt_decrypt_pagecache_blocks(bh->b_page, bh->b_size, 32031fb992cSEric Biggers bh_offset(bh)); 32131fb992cSEric Biggers end_buffer_async_read(bh, err == 0); 32231fb992cSEric Biggers kfree(ctx); 32331fb992cSEric Biggers } 32431fb992cSEric Biggers 32531fb992cSEric Biggers /* 32631fb992cSEric Biggers * I/O completion handler for block_read_full_page() - pages 32731fb992cSEric Biggers * which come unlocked at the end of I/O. 32831fb992cSEric Biggers */ 32931fb992cSEric Biggers static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate) 33031fb992cSEric Biggers { 33131fb992cSEric Biggers /* Decrypt if needed */ 33231fb992cSEric Biggers if (uptodate && IS_ENABLED(CONFIG_FS_ENCRYPTION) && 33331fb992cSEric Biggers IS_ENCRYPTED(bh->b_page->mapping->host) && 33431fb992cSEric Biggers S_ISREG(bh->b_page->mapping->host->i_mode)) { 33531fb992cSEric Biggers struct decrypt_bh_ctx *ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); 33631fb992cSEric Biggers 33731fb992cSEric Biggers if (ctx) { 33831fb992cSEric Biggers INIT_WORK(&ctx->work, decrypt_bh); 33931fb992cSEric Biggers ctx->bh = bh; 34031fb992cSEric Biggers fscrypt_enqueue_decrypt_work(&ctx->work); 34131fb992cSEric Biggers return; 34231fb992cSEric Biggers } 34331fb992cSEric Biggers uptodate = 0; 34431fb992cSEric Biggers } 34531fb992cSEric Biggers end_buffer_async_read(bh, uptodate); 34631fb992cSEric Biggers } 34731fb992cSEric Biggers 3481da177e4SLinus Torvalds /* 3491da177e4SLinus Torvalds * Completion handler for block_write_full_page() - pages which are unlocked 3501da177e4SLinus Torvalds * during I/O, and which have PageWriteback cleared upon I/O completion. 3511da177e4SLinus Torvalds */ 35235c80d5fSChris Mason void end_buffer_async_write(struct buffer_head *bh, int uptodate) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds unsigned long flags; 355a3972203SNick Piggin struct buffer_head *first; 3561da177e4SLinus Torvalds struct buffer_head *tmp; 3571da177e4SLinus Torvalds struct page *page; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds BUG_ON(!buffer_async_write(bh)); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds page = bh->b_page; 3621da177e4SLinus Torvalds if (uptodate) { 3631da177e4SLinus Torvalds set_buffer_uptodate(bh); 3641da177e4SLinus Torvalds } else { 365b744c2acSRobert Elliott buffer_io_error(bh, ", lost async page write"); 36687354e5dSJeff Layton mark_buffer_write_io_error(bh); 3671da177e4SLinus Torvalds clear_buffer_uptodate(bh); 3681da177e4SLinus Torvalds SetPageError(page); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 371a3972203SNick Piggin first = page_buffers(page); 372a3972203SNick Piggin local_irq_save(flags); 373a3972203SNick Piggin bit_spin_lock(BH_Uptodate_Lock, &first->b_state); 374a3972203SNick Piggin 3751da177e4SLinus Torvalds clear_buffer_async_write(bh); 3761da177e4SLinus Torvalds unlock_buffer(bh); 3771da177e4SLinus Torvalds tmp = bh->b_this_page; 3781da177e4SLinus Torvalds while (tmp != bh) { 3791da177e4SLinus Torvalds if (buffer_async_write(tmp)) { 3801da177e4SLinus Torvalds BUG_ON(!buffer_locked(tmp)); 3811da177e4SLinus Torvalds goto still_busy; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds tmp = tmp->b_this_page; 3841da177e4SLinus Torvalds } 385a3972203SNick Piggin bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); 386a3972203SNick Piggin local_irq_restore(flags); 3871da177e4SLinus Torvalds end_page_writeback(page); 3881da177e4SLinus Torvalds return; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds still_busy: 391a3972203SNick Piggin bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); 392a3972203SNick Piggin local_irq_restore(flags); 3931da177e4SLinus Torvalds return; 3941da177e4SLinus Torvalds } 3951fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(end_buffer_async_write); 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* 3981da177e4SLinus Torvalds * If a page's buffers are under async readin (end_buffer_async_read 3991da177e4SLinus Torvalds * completion) then there is a possibility that another thread of 4001da177e4SLinus Torvalds * control could lock one of the buffers after it has completed 4011da177e4SLinus Torvalds * but while some of the other buffers have not completed. This 4021da177e4SLinus Torvalds * locked buffer would confuse end_buffer_async_read() into not unlocking 4031da177e4SLinus Torvalds * the page. So the absence of BH_Async_Read tells end_buffer_async_read() 4041da177e4SLinus Torvalds * that this buffer is not under async I/O. 4051da177e4SLinus Torvalds * 4061da177e4SLinus Torvalds * The page comes unlocked when it has no locked buffer_async buffers 4071da177e4SLinus Torvalds * left. 4081da177e4SLinus Torvalds * 4091da177e4SLinus Torvalds * PageLocked prevents anyone starting new async I/O reads any of 4101da177e4SLinus Torvalds * the buffers. 4111da177e4SLinus Torvalds * 4121da177e4SLinus Torvalds * PageWriteback is used to prevent simultaneous writeout of the same 4131da177e4SLinus Torvalds * page. 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds * PageLocked prevents anyone from starting writeback of a page which is 4161da177e4SLinus Torvalds * under read I/O (PageWriteback is only ever set against a locked page). 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds static void mark_buffer_async_read(struct buffer_head *bh) 4191da177e4SLinus Torvalds { 42031fb992cSEric Biggers bh->b_end_io = end_buffer_async_read_io; 4211da177e4SLinus Torvalds set_buffer_async_read(bh); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241fe72eaaSH Hartley Sweeten static void mark_buffer_async_write_endio(struct buffer_head *bh, 42535c80d5fSChris Mason bh_end_io_t *handler) 42635c80d5fSChris Mason { 42735c80d5fSChris Mason bh->b_end_io = handler; 42835c80d5fSChris Mason set_buffer_async_write(bh); 42935c80d5fSChris Mason } 43035c80d5fSChris Mason 4311da177e4SLinus Torvalds void mark_buffer_async_write(struct buffer_head *bh) 4321da177e4SLinus Torvalds { 43335c80d5fSChris Mason mark_buffer_async_write_endio(bh, end_buffer_async_write); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds EXPORT_SYMBOL(mark_buffer_async_write); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* 4391da177e4SLinus Torvalds * fs/buffer.c contains helper functions for buffer-backed address space's 4401da177e4SLinus Torvalds * fsync functions. A common requirement for buffer-based filesystems is 4411da177e4SLinus Torvalds * that certain data from the backing blockdev needs to be written out for 4421da177e4SLinus Torvalds * a successful fsync(). For example, ext2 indirect blocks need to be 4431da177e4SLinus Torvalds * written back and waited upon before fsync() returns. 4441da177e4SLinus Torvalds * 4451da177e4SLinus Torvalds * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(), 4461da177e4SLinus Torvalds * inode_has_buffers() and invalidate_inode_buffers() are provided for the 4471da177e4SLinus Torvalds * management of a list of dependent buffers at ->i_mapping->private_list. 4481da177e4SLinus Torvalds * 4491da177e4SLinus Torvalds * Locking is a little subtle: try_to_free_buffers() will remove buffers 4501da177e4SLinus Torvalds * from their controlling inode's queue when they are being freed. But 4511da177e4SLinus Torvalds * try_to_free_buffers() will be operating against the *blockdev* mapping 4521da177e4SLinus Torvalds * at the time, not against the S_ISREG file which depends on those buffers. 4531da177e4SLinus Torvalds * So the locking for private_list is via the private_lock in the address_space 4541da177e4SLinus Torvalds * which backs the buffers. Which is different from the address_space 4551da177e4SLinus Torvalds * against which the buffers are listed. So for a particular address_space, 4561da177e4SLinus Torvalds * mapping->private_lock does *not* protect mapping->private_list! In fact, 4571da177e4SLinus Torvalds * mapping->private_list will always be protected by the backing blockdev's 4581da177e4SLinus Torvalds * ->private_lock. 4591da177e4SLinus Torvalds * 4601da177e4SLinus Torvalds * Which introduces a requirement: all buffers on an address_space's 4611da177e4SLinus Torvalds * ->private_list must be from the same address_space: the blockdev's. 4621da177e4SLinus Torvalds * 4631da177e4SLinus Torvalds * address_spaces which do not place buffers at ->private_list via these 4641da177e4SLinus Torvalds * utility functions are free to use private_lock and private_list for 4651da177e4SLinus Torvalds * whatever they want. The only requirement is that list_empty(private_list) 4661da177e4SLinus Torvalds * be true at clear_inode() time. 4671da177e4SLinus Torvalds * 4681da177e4SLinus Torvalds * FIXME: clear_inode should not call invalidate_inode_buffers(). The 4691da177e4SLinus Torvalds * filesystems should do that. invalidate_inode_buffers() should just go 4701da177e4SLinus Torvalds * BUG_ON(!list_empty). 4711da177e4SLinus Torvalds * 4721da177e4SLinus Torvalds * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should 4731da177e4SLinus Torvalds * take an address_space, not an inode. And it should be called 4741da177e4SLinus Torvalds * mark_buffer_dirty_fsync() to clearly define why those buffers are being 4751da177e4SLinus Torvalds * queued up. 4761da177e4SLinus Torvalds * 4771da177e4SLinus Torvalds * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the 4781da177e4SLinus Torvalds * list if it is already on a list. Because if the buffer is on a list, 4791da177e4SLinus Torvalds * it *must* already be on the right one. If not, the filesystem is being 4801da177e4SLinus Torvalds * silly. This will save a ton of locking. But first we have to ensure 4811da177e4SLinus Torvalds * that buffers are taken *off* the old inode's list when they are freed 4821da177e4SLinus Torvalds * (presumably in truncate). That requires careful auditing of all 4831da177e4SLinus Torvalds * filesystems (do it inside bforget()). It could also be done by bringing 4841da177e4SLinus Torvalds * b_inode back. 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * The buffer's backing address_space's private_lock must be held 4891da177e4SLinus Torvalds */ 490dbacefc9SThomas Petazzoni static void __remove_assoc_queue(struct buffer_head *bh) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds list_del_init(&bh->b_assoc_buffers); 49358ff407bSJan Kara WARN_ON(!bh->b_assoc_map); 49458ff407bSJan Kara bh->b_assoc_map = NULL; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds int inode_has_buffers(struct inode *inode) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds return !list_empty(&inode->i_data.private_list); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* 5031da177e4SLinus Torvalds * osync is designed to support O_SYNC io. It waits synchronously for 5041da177e4SLinus Torvalds * all already-submitted IO to complete, but does not queue any new 5051da177e4SLinus Torvalds * writes to the disk. 5061da177e4SLinus Torvalds * 5071da177e4SLinus Torvalds * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as 5081da177e4SLinus Torvalds * you dirty the buffers, and then use osync_inode_buffers to wait for 5091da177e4SLinus Torvalds * completion. Any other dirty buffers which are not yet queued for 5101da177e4SLinus Torvalds * write will not be flushed to disk by the osync. 5111da177e4SLinus Torvalds */ 5121da177e4SLinus Torvalds static int osync_buffers_list(spinlock_t *lock, struct list_head *list) 5131da177e4SLinus Torvalds { 5141da177e4SLinus Torvalds struct buffer_head *bh; 5151da177e4SLinus Torvalds struct list_head *p; 5161da177e4SLinus Torvalds int err = 0; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds spin_lock(lock); 5191da177e4SLinus Torvalds repeat: 5201da177e4SLinus Torvalds list_for_each_prev(p, list) { 5211da177e4SLinus Torvalds bh = BH_ENTRY(p); 5221da177e4SLinus Torvalds if (buffer_locked(bh)) { 5231da177e4SLinus Torvalds get_bh(bh); 5241da177e4SLinus Torvalds spin_unlock(lock); 5251da177e4SLinus Torvalds wait_on_buffer(bh); 5261da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 5271da177e4SLinus Torvalds err = -EIO; 5281da177e4SLinus Torvalds brelse(bh); 5291da177e4SLinus Torvalds spin_lock(lock); 5301da177e4SLinus Torvalds goto repeat; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds spin_unlock(lock); 5341da177e4SLinus Torvalds return err; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 53708fdc8a0SMateusz Guzik void emergency_thaw_bdev(struct super_block *sb) 538c2d75438SEric Sandeen { 539c2d75438SEric Sandeen while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb)) 540a1c6f057SDmitry Monakhov printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev); 541c2d75438SEric Sandeen } 54201a05b33SAl Viro 5431da177e4SLinus Torvalds /** 54478a4a50aSRandy Dunlap * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers 54567be2dd1SMartin Waitz * @mapping: the mapping which wants those buffers written 5461da177e4SLinus Torvalds * 5471da177e4SLinus Torvalds * Starts I/O against the buffers at mapping->private_list, and waits upon 5481da177e4SLinus Torvalds * that I/O. 5491da177e4SLinus Torvalds * 55067be2dd1SMartin Waitz * Basically, this is a convenience function for fsync(). 55167be2dd1SMartin Waitz * @mapping is a file or directory which needs those buffers to be written for 55267be2dd1SMartin Waitz * a successful fsync(). 5531da177e4SLinus Torvalds */ 5541da177e4SLinus Torvalds int sync_mapping_buffers(struct address_space *mapping) 5551da177e4SLinus Torvalds { 556252aa6f5SRafael Aquini struct address_space *buffer_mapping = mapping->private_data; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds if (buffer_mapping == NULL || list_empty(&mapping->private_list)) 5591da177e4SLinus Torvalds return 0; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds return fsync_buffers_list(&buffer_mapping->private_lock, 5621da177e4SLinus Torvalds &mapping->private_list); 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds EXPORT_SYMBOL(sync_mapping_buffers); 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds /* 5671da177e4SLinus Torvalds * Called when we've recently written block `bblock', and it is known that 5681da177e4SLinus Torvalds * `bblock' was for a buffer_boundary() buffer. This means that the block at 5691da177e4SLinus Torvalds * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's 5701da177e4SLinus Torvalds * dirty, schedule it for IO. So that indirects merge nicely with their data. 5711da177e4SLinus Torvalds */ 5721da177e4SLinus Torvalds void write_boundary_block(struct block_device *bdev, 5731da177e4SLinus Torvalds sector_t bblock, unsigned blocksize) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize); 5761da177e4SLinus Torvalds if (bh) { 5771da177e4SLinus Torvalds if (buffer_dirty(bh)) 578dfec8a14SMike Christie ll_rw_block(REQ_OP_WRITE, 0, 1, &bh); 5791da177e4SLinus Torvalds put_bh(bh); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode) 5841da177e4SLinus Torvalds { 5851da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 5861da177e4SLinus Torvalds struct address_space *buffer_mapping = bh->b_page->mapping; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds mark_buffer_dirty(bh); 589252aa6f5SRafael Aquini if (!mapping->private_data) { 590252aa6f5SRafael Aquini mapping->private_data = buffer_mapping; 5911da177e4SLinus Torvalds } else { 592252aa6f5SRafael Aquini BUG_ON(mapping->private_data != buffer_mapping); 5931da177e4SLinus Torvalds } 594535ee2fbSJan Kara if (!bh->b_assoc_map) { 5951da177e4SLinus Torvalds spin_lock(&buffer_mapping->private_lock); 5961da177e4SLinus Torvalds list_move_tail(&bh->b_assoc_buffers, 5971da177e4SLinus Torvalds &mapping->private_list); 59858ff407bSJan Kara bh->b_assoc_map = mapping; 5991da177e4SLinus Torvalds spin_unlock(&buffer_mapping->private_lock); 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds EXPORT_SYMBOL(mark_buffer_dirty_inode); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds /* 605ec82e1c1SMatthew Wilcox * Mark the page dirty, and set it dirty in the page cache, and mark the inode 606787d2214SNick Piggin * dirty. 607787d2214SNick Piggin * 608787d2214SNick Piggin * If warn is true, then emit a warning if the page is not uptodate and has 609787d2214SNick Piggin * not been truncated. 610c4843a75SGreg Thelen * 61181f8c3a4SJohannes Weiner * The caller must hold lock_page_memcg(). 612787d2214SNick Piggin */ 613f82b3764SMatthew Wilcox void __set_page_dirty(struct page *page, struct address_space *mapping, 61462cccb8cSJohannes Weiner int warn) 615787d2214SNick Piggin { 616227d53b3SKOSAKI Motohiro unsigned long flags; 617227d53b3SKOSAKI Motohiro 618b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 619787d2214SNick Piggin if (page->mapping) { /* Race with truncate? */ 620787d2214SNick Piggin WARN_ON_ONCE(warn && !PageUptodate(page)); 62162cccb8cSJohannes Weiner account_page_dirtied(page, mapping); 622ec82e1c1SMatthew Wilcox __xa_set_mark(&mapping->i_pages, page_index(page), 623ec82e1c1SMatthew Wilcox PAGECACHE_TAG_DIRTY); 624787d2214SNick Piggin } 625b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 626787d2214SNick Piggin } 627f82b3764SMatthew Wilcox EXPORT_SYMBOL_GPL(__set_page_dirty); 628787d2214SNick Piggin 629787d2214SNick Piggin /* 6301da177e4SLinus Torvalds * Add a page to the dirty page list. 6311da177e4SLinus Torvalds * 6321da177e4SLinus Torvalds * It is a sad fact of life that this function is called from several places 6331da177e4SLinus Torvalds * deeply under spinlocking. It may not sleep. 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * If the page has buffers, the uptodate buffers are set dirty, to preserve 6361da177e4SLinus Torvalds * dirty-state coherency between the page and the buffers. It the page does 6371da177e4SLinus Torvalds * not have buffers then when they are later attached they will all be set 6381da177e4SLinus Torvalds * dirty. 6391da177e4SLinus Torvalds * 6401da177e4SLinus Torvalds * The buffers are dirtied before the page is dirtied. There's a small race 6411da177e4SLinus Torvalds * window in which a writepage caller may see the page cleanness but not the 6421da177e4SLinus Torvalds * buffer dirtiness. That's fine. If this code were to set the page dirty 6431da177e4SLinus Torvalds * before the buffers, a concurrent writepage caller could clear the page dirty 6441da177e4SLinus Torvalds * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean 6451da177e4SLinus Torvalds * page on the dirty page list. 6461da177e4SLinus Torvalds * 6471da177e4SLinus Torvalds * We use private_lock to lock against try_to_free_buffers while using the 6481da177e4SLinus Torvalds * page's buffer list. Also use this to protect against clean buffers being 6491da177e4SLinus Torvalds * added to the page after it was set dirty. 6501da177e4SLinus Torvalds * 6511da177e4SLinus Torvalds * FIXME: may need to call ->reservepage here as well. That's rather up to the 6521da177e4SLinus Torvalds * address_space though. 6531da177e4SLinus Torvalds */ 6541da177e4SLinus Torvalds int __set_page_dirty_buffers(struct page *page) 6551da177e4SLinus Torvalds { 656a8e7d49aSLinus Torvalds int newly_dirty; 657787d2214SNick Piggin struct address_space *mapping = page_mapping(page); 658ebf7a227SNick Piggin 659ebf7a227SNick Piggin if (unlikely(!mapping)) 660ebf7a227SNick Piggin return !TestSetPageDirty(page); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds spin_lock(&mapping->private_lock); 6631da177e4SLinus Torvalds if (page_has_buffers(page)) { 6641da177e4SLinus Torvalds struct buffer_head *head = page_buffers(page); 6651da177e4SLinus Torvalds struct buffer_head *bh = head; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds do { 6681da177e4SLinus Torvalds set_buffer_dirty(bh); 6691da177e4SLinus Torvalds bh = bh->b_this_page; 6701da177e4SLinus Torvalds } while (bh != head); 6711da177e4SLinus Torvalds } 672c4843a75SGreg Thelen /* 67381f8c3a4SJohannes Weiner * Lock out page->mem_cgroup migration to keep PageDirty 67481f8c3a4SJohannes Weiner * synchronized with per-memcg dirty page counters. 675c4843a75SGreg Thelen */ 67662cccb8cSJohannes Weiner lock_page_memcg(page); 677a8e7d49aSLinus Torvalds newly_dirty = !TestSetPageDirty(page); 6781da177e4SLinus Torvalds spin_unlock(&mapping->private_lock); 6791da177e4SLinus Torvalds 680a8e7d49aSLinus Torvalds if (newly_dirty) 68162cccb8cSJohannes Weiner __set_page_dirty(page, mapping, 1); 682c4843a75SGreg Thelen 68362cccb8cSJohannes Weiner unlock_page_memcg(page); 684c4843a75SGreg Thelen 685c4843a75SGreg Thelen if (newly_dirty) 686c4843a75SGreg Thelen __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 687c4843a75SGreg Thelen 688a8e7d49aSLinus Torvalds return newly_dirty; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds EXPORT_SYMBOL(__set_page_dirty_buffers); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* 6931da177e4SLinus Torvalds * Write out and wait upon a list of buffers. 6941da177e4SLinus Torvalds * 6951da177e4SLinus Torvalds * We have conflicting pressures: we want to make sure that all 6961da177e4SLinus Torvalds * initially dirty buffers get waited on, but that any subsequently 6971da177e4SLinus Torvalds * dirtied buffers don't. After all, we don't want fsync to last 6981da177e4SLinus Torvalds * forever if somebody is actively writing to the file. 6991da177e4SLinus Torvalds * 7001da177e4SLinus Torvalds * Do this in two main stages: first we copy dirty buffers to a 7011da177e4SLinus Torvalds * temporary inode list, queueing the writes as we go. Then we clean 7021da177e4SLinus Torvalds * up, waiting for those writes to complete. 7031da177e4SLinus Torvalds * 7041da177e4SLinus Torvalds * During this second stage, any subsequent updates to the file may end 7051da177e4SLinus Torvalds * up refiling the buffer on the original inode's dirty list again, so 7061da177e4SLinus Torvalds * there is a chance we will end up with a buffer queued for write but 7071da177e4SLinus Torvalds * not yet completed on that list. So, as a final cleanup we go through 7081da177e4SLinus Torvalds * the osync code to catch these locked, dirty buffers without requeuing 7091da177e4SLinus Torvalds * any newly dirty buffers for write. 7101da177e4SLinus Torvalds */ 7111da177e4SLinus Torvalds static int fsync_buffers_list(spinlock_t *lock, struct list_head *list) 7121da177e4SLinus Torvalds { 7131da177e4SLinus Torvalds struct buffer_head *bh; 7141da177e4SLinus Torvalds struct list_head tmp; 7157eaceaccSJens Axboe struct address_space *mapping; 7161da177e4SLinus Torvalds int err = 0, err2; 7174ee2491eSJens Axboe struct blk_plug plug; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds INIT_LIST_HEAD(&tmp); 7204ee2491eSJens Axboe blk_start_plug(&plug); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds spin_lock(lock); 7231da177e4SLinus Torvalds while (!list_empty(list)) { 7241da177e4SLinus Torvalds bh = BH_ENTRY(list->next); 725535ee2fbSJan Kara mapping = bh->b_assoc_map; 72658ff407bSJan Kara __remove_assoc_queue(bh); 727535ee2fbSJan Kara /* Avoid race with mark_buffer_dirty_inode() which does 728535ee2fbSJan Kara * a lockless check and we rely on seeing the dirty bit */ 729535ee2fbSJan Kara smp_mb(); 7301da177e4SLinus Torvalds if (buffer_dirty(bh) || buffer_locked(bh)) { 7311da177e4SLinus Torvalds list_add(&bh->b_assoc_buffers, &tmp); 732535ee2fbSJan Kara bh->b_assoc_map = mapping; 7331da177e4SLinus Torvalds if (buffer_dirty(bh)) { 7341da177e4SLinus Torvalds get_bh(bh); 7351da177e4SLinus Torvalds spin_unlock(lock); 7361da177e4SLinus Torvalds /* 7371da177e4SLinus Torvalds * Ensure any pending I/O completes so that 7389cb569d6SChristoph Hellwig * write_dirty_buffer() actually writes the 7399cb569d6SChristoph Hellwig * current contents - it is a noop if I/O is 7409cb569d6SChristoph Hellwig * still in flight on potentially older 7419cb569d6SChristoph Hellwig * contents. 7421da177e4SLinus Torvalds */ 74370fd7614SChristoph Hellwig write_dirty_buffer(bh, REQ_SYNC); 7449cf6b720SJens Axboe 7459cf6b720SJens Axboe /* 7469cf6b720SJens Axboe * Kick off IO for the previous mapping. Note 7479cf6b720SJens Axboe * that we will not run the very last mapping, 7489cf6b720SJens Axboe * wait_on_buffer() will do that for us 7499cf6b720SJens Axboe * through sync_buffer(). 7509cf6b720SJens Axboe */ 7511da177e4SLinus Torvalds brelse(bh); 7521da177e4SLinus Torvalds spin_lock(lock); 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7574ee2491eSJens Axboe spin_unlock(lock); 7584ee2491eSJens Axboe blk_finish_plug(&plug); 7594ee2491eSJens Axboe spin_lock(lock); 7604ee2491eSJens Axboe 7611da177e4SLinus Torvalds while (!list_empty(&tmp)) { 7621da177e4SLinus Torvalds bh = BH_ENTRY(tmp.prev); 7631da177e4SLinus Torvalds get_bh(bh); 764535ee2fbSJan Kara mapping = bh->b_assoc_map; 765535ee2fbSJan Kara __remove_assoc_queue(bh); 766535ee2fbSJan Kara /* Avoid race with mark_buffer_dirty_inode() which does 767535ee2fbSJan Kara * a lockless check and we rely on seeing the dirty bit */ 768535ee2fbSJan Kara smp_mb(); 769535ee2fbSJan Kara if (buffer_dirty(bh)) { 770535ee2fbSJan Kara list_add(&bh->b_assoc_buffers, 771e3892296SJan Kara &mapping->private_list); 772535ee2fbSJan Kara bh->b_assoc_map = mapping; 773535ee2fbSJan Kara } 7741da177e4SLinus Torvalds spin_unlock(lock); 7751da177e4SLinus Torvalds wait_on_buffer(bh); 7761da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 7771da177e4SLinus Torvalds err = -EIO; 7781da177e4SLinus Torvalds brelse(bh); 7791da177e4SLinus Torvalds spin_lock(lock); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds spin_unlock(lock); 7831da177e4SLinus Torvalds err2 = osync_buffers_list(lock, list); 7841da177e4SLinus Torvalds if (err) 7851da177e4SLinus Torvalds return err; 7861da177e4SLinus Torvalds else 7871da177e4SLinus Torvalds return err2; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds /* 7911da177e4SLinus Torvalds * Invalidate any and all dirty buffers on a given inode. We are 7921da177e4SLinus Torvalds * probably unmounting the fs, but that doesn't mean we have already 7931da177e4SLinus Torvalds * done a sync(). Just drop the buffers from the inode list. 7941da177e4SLinus Torvalds * 7951da177e4SLinus Torvalds * NOTE: we take the inode's blockdev's mapping's private_lock. Which 7961da177e4SLinus Torvalds * assumes that all the buffers are against the blockdev. Not true 7971da177e4SLinus Torvalds * for reiserfs. 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds void invalidate_inode_buffers(struct inode *inode) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds if (inode_has_buffers(inode)) { 8021da177e4SLinus Torvalds struct address_space *mapping = &inode->i_data; 8031da177e4SLinus Torvalds struct list_head *list = &mapping->private_list; 804252aa6f5SRafael Aquini struct address_space *buffer_mapping = mapping->private_data; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds spin_lock(&buffer_mapping->private_lock); 8071da177e4SLinus Torvalds while (!list_empty(list)) 8081da177e4SLinus Torvalds __remove_assoc_queue(BH_ENTRY(list->next)); 8091da177e4SLinus Torvalds spin_unlock(&buffer_mapping->private_lock); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds } 81252b19ac9SJan Kara EXPORT_SYMBOL(invalidate_inode_buffers); 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds /* 8151da177e4SLinus Torvalds * Remove any clean buffers from the inode's buffer list. This is called 8161da177e4SLinus Torvalds * when we're trying to free the inode itself. Those buffers can pin it. 8171da177e4SLinus Torvalds * 8181da177e4SLinus Torvalds * Returns true if all buffers were removed. 8191da177e4SLinus Torvalds */ 8201da177e4SLinus Torvalds int remove_inode_buffers(struct inode *inode) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds int ret = 1; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds if (inode_has_buffers(inode)) { 8251da177e4SLinus Torvalds struct address_space *mapping = &inode->i_data; 8261da177e4SLinus Torvalds struct list_head *list = &mapping->private_list; 827252aa6f5SRafael Aquini struct address_space *buffer_mapping = mapping->private_data; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds spin_lock(&buffer_mapping->private_lock); 8301da177e4SLinus Torvalds while (!list_empty(list)) { 8311da177e4SLinus Torvalds struct buffer_head *bh = BH_ENTRY(list->next); 8321da177e4SLinus Torvalds if (buffer_dirty(bh)) { 8331da177e4SLinus Torvalds ret = 0; 8341da177e4SLinus Torvalds break; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds __remove_assoc_queue(bh); 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds spin_unlock(&buffer_mapping->private_lock); 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds return ret; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds /* 8441da177e4SLinus Torvalds * Create the appropriate buffers when given a page for data area and 8451da177e4SLinus Torvalds * the size of each buffer.. Use the bh->b_this_page linked list to 8461da177e4SLinus Torvalds * follow the buffers created. Return NULL if unable to create more 8471da177e4SLinus Torvalds * buffers. 8481da177e4SLinus Torvalds * 8491da177e4SLinus Torvalds * The retry flag is used to differentiate async IO (paging, swapping) 8501da177e4SLinus Torvalds * which may not fail from ordinary buffer allocations. 8511da177e4SLinus Torvalds */ 8521da177e4SLinus Torvalds struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, 853640ab98fSJens Axboe bool retry) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds struct buffer_head *bh, *head; 856f745c6f5SShakeel Butt gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT; 8571da177e4SLinus Torvalds long offset; 858f745c6f5SShakeel Butt struct mem_cgroup *memcg; 8591da177e4SLinus Torvalds 860640ab98fSJens Axboe if (retry) 861640ab98fSJens Axboe gfp |= __GFP_NOFAIL; 862640ab98fSJens Axboe 863f745c6f5SShakeel Butt memcg = get_mem_cgroup_from_page(page); 864f745c6f5SShakeel Butt memalloc_use_memcg(memcg); 865f745c6f5SShakeel Butt 8661da177e4SLinus Torvalds head = NULL; 8671da177e4SLinus Torvalds offset = PAGE_SIZE; 8681da177e4SLinus Torvalds while ((offset -= size) >= 0) { 869640ab98fSJens Axboe bh = alloc_buffer_head(gfp); 8701da177e4SLinus Torvalds if (!bh) 8711da177e4SLinus Torvalds goto no_grow; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds bh->b_this_page = head; 8741da177e4SLinus Torvalds bh->b_blocknr = -1; 8751da177e4SLinus Torvalds head = bh; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds bh->b_size = size; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* Link the buffer to its page */ 8801da177e4SLinus Torvalds set_bh_page(bh, page, offset); 8811da177e4SLinus Torvalds } 882f745c6f5SShakeel Butt out: 883f745c6f5SShakeel Butt memalloc_unuse_memcg(); 884f745c6f5SShakeel Butt mem_cgroup_put(memcg); 8851da177e4SLinus Torvalds return head; 8861da177e4SLinus Torvalds /* 8871da177e4SLinus Torvalds * In case anything failed, we just free everything we got. 8881da177e4SLinus Torvalds */ 8891da177e4SLinus Torvalds no_grow: 8901da177e4SLinus Torvalds if (head) { 8911da177e4SLinus Torvalds do { 8921da177e4SLinus Torvalds bh = head; 8931da177e4SLinus Torvalds head = head->b_this_page; 8941da177e4SLinus Torvalds free_buffer_head(bh); 8951da177e4SLinus Torvalds } while (head); 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 898f745c6f5SShakeel Butt goto out; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(alloc_page_buffers); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds static inline void 9031da177e4SLinus Torvalds link_dev_buffers(struct page *page, struct buffer_head *head) 9041da177e4SLinus Torvalds { 9051da177e4SLinus Torvalds struct buffer_head *bh, *tail; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds bh = head; 9081da177e4SLinus Torvalds do { 9091da177e4SLinus Torvalds tail = bh; 9101da177e4SLinus Torvalds bh = bh->b_this_page; 9111da177e4SLinus Torvalds } while (bh); 9121da177e4SLinus Torvalds tail->b_this_page = head; 9131da177e4SLinus Torvalds attach_page_buffers(page, head); 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 916bbec0270SLinus Torvalds static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size) 917bbec0270SLinus Torvalds { 918bbec0270SLinus Torvalds sector_t retval = ~((sector_t)0); 919bbec0270SLinus Torvalds loff_t sz = i_size_read(bdev->bd_inode); 920bbec0270SLinus Torvalds 921bbec0270SLinus Torvalds if (sz) { 922bbec0270SLinus Torvalds unsigned int sizebits = blksize_bits(size); 923bbec0270SLinus Torvalds retval = (sz >> sizebits); 924bbec0270SLinus Torvalds } 925bbec0270SLinus Torvalds return retval; 926bbec0270SLinus Torvalds } 927bbec0270SLinus Torvalds 9281da177e4SLinus Torvalds /* 9291da177e4SLinus Torvalds * Initialise the state of a blockdev page's buffers. 9301da177e4SLinus Torvalds */ 931676ce6d5SHugh Dickins static sector_t 9321da177e4SLinus Torvalds init_page_buffers(struct page *page, struct block_device *bdev, 9331da177e4SLinus Torvalds sector_t block, int size) 9341da177e4SLinus Torvalds { 9351da177e4SLinus Torvalds struct buffer_head *head = page_buffers(page); 9361da177e4SLinus Torvalds struct buffer_head *bh = head; 9371da177e4SLinus Torvalds int uptodate = PageUptodate(page); 938bbec0270SLinus Torvalds sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size); 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds do { 9411da177e4SLinus Torvalds if (!buffer_mapped(bh)) { 94201950a34SEric Biggers bh->b_end_io = NULL; 94301950a34SEric Biggers bh->b_private = NULL; 9441da177e4SLinus Torvalds bh->b_bdev = bdev; 9451da177e4SLinus Torvalds bh->b_blocknr = block; 9461da177e4SLinus Torvalds if (uptodate) 9471da177e4SLinus Torvalds set_buffer_uptodate(bh); 948080399aaSJeff Moyer if (block < end_block) 9491da177e4SLinus Torvalds set_buffer_mapped(bh); 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds block++; 9521da177e4SLinus Torvalds bh = bh->b_this_page; 9531da177e4SLinus Torvalds } while (bh != head); 954676ce6d5SHugh Dickins 955676ce6d5SHugh Dickins /* 956676ce6d5SHugh Dickins * Caller needs to validate requested block against end of device. 957676ce6d5SHugh Dickins */ 958676ce6d5SHugh Dickins return end_block; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * Create the page-cache page that contains the requested block. 9631da177e4SLinus Torvalds * 964676ce6d5SHugh Dickins * This is used purely for blockdev mappings. 9651da177e4SLinus Torvalds */ 966676ce6d5SHugh Dickins static int 9671da177e4SLinus Torvalds grow_dev_page(struct block_device *bdev, sector_t block, 9683b5e6454SGioh Kim pgoff_t index, int size, int sizebits, gfp_t gfp) 9691da177e4SLinus Torvalds { 9701da177e4SLinus Torvalds struct inode *inode = bdev->bd_inode; 9711da177e4SLinus Torvalds struct page *page; 9721da177e4SLinus Torvalds struct buffer_head *bh; 973676ce6d5SHugh Dickins sector_t end_block; 974676ce6d5SHugh Dickins int ret = 0; /* Will call free_more_memory() */ 97584235de3SJohannes Weiner gfp_t gfp_mask; 9761da177e4SLinus Torvalds 977c62d2555SMichal Hocko gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp; 9783b5e6454SGioh Kim 97984235de3SJohannes Weiner /* 98084235de3SJohannes Weiner * XXX: __getblk_slow() can not really deal with failure and 98184235de3SJohannes Weiner * will endlessly loop on improvised global reclaim. Prefer 98284235de3SJohannes Weiner * looping in the allocator rather than here, at least that 98384235de3SJohannes Weiner * code knows what it's doing. 98484235de3SJohannes Weiner */ 98584235de3SJohannes Weiner gfp_mask |= __GFP_NOFAIL; 98684235de3SJohannes Weiner 98784235de3SJohannes Weiner page = find_or_create_page(inode->i_mapping, index, gfp_mask); 9881da177e4SLinus Torvalds 989e827f923SEric Sesterhenn BUG_ON(!PageLocked(page)); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds if (page_has_buffers(page)) { 9921da177e4SLinus Torvalds bh = page_buffers(page); 9931da177e4SLinus Torvalds if (bh->b_size == size) { 994676ce6d5SHugh Dickins end_block = init_page_buffers(page, bdev, 995f2d5a944SAnton Altaparmakov (sector_t)index << sizebits, 996f2d5a944SAnton Altaparmakov size); 997676ce6d5SHugh Dickins goto done; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds if (!try_to_free_buffers(page)) 10001da177e4SLinus Torvalds goto failed; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* 10041da177e4SLinus Torvalds * Allocate some buffers for this page 10051da177e4SLinus Torvalds */ 100694dc24c0SJens Axboe bh = alloc_page_buffers(page, size, true); 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* 10091da177e4SLinus Torvalds * Link the page to the buffers and initialise them. Take the 10101da177e4SLinus Torvalds * lock to be atomic wrt __find_get_block(), which does not 10111da177e4SLinus Torvalds * run under the page lock. 10121da177e4SLinus Torvalds */ 10131da177e4SLinus Torvalds spin_lock(&inode->i_mapping->private_lock); 10141da177e4SLinus Torvalds link_dev_buffers(page, bh); 1015f2d5a944SAnton Altaparmakov end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits, 1016f2d5a944SAnton Altaparmakov size); 10171da177e4SLinus Torvalds spin_unlock(&inode->i_mapping->private_lock); 1018676ce6d5SHugh Dickins done: 1019676ce6d5SHugh Dickins ret = (block < end_block) ? 1 : -ENXIO; 10201da177e4SLinus Torvalds failed: 10211da177e4SLinus Torvalds unlock_page(page); 102209cbfeafSKirill A. Shutemov put_page(page); 1023676ce6d5SHugh Dickins return ret; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 10271da177e4SLinus Torvalds * Create buffers for the specified block device block's page. If 10281da177e4SLinus Torvalds * that page was dirty, the buffers are set dirty also. 10291da177e4SLinus Torvalds */ 1030858119e1SArjan van de Ven static int 10313b5e6454SGioh Kim grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp) 10321da177e4SLinus Torvalds { 10331da177e4SLinus Torvalds pgoff_t index; 10341da177e4SLinus Torvalds int sizebits; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds sizebits = -1; 10371da177e4SLinus Torvalds do { 10381da177e4SLinus Torvalds sizebits++; 10391da177e4SLinus Torvalds } while ((size << sizebits) < PAGE_SIZE); 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds index = block >> sizebits; 10421da177e4SLinus Torvalds 1043e5657933SAndrew Morton /* 1044e5657933SAndrew Morton * Check for a block which wants to lie outside our maximum possible 1045e5657933SAndrew Morton * pagecache index. (this comparison is done using sector_t types). 1046e5657933SAndrew Morton */ 1047e5657933SAndrew Morton if (unlikely(index != block >> sizebits)) { 1048e5657933SAndrew Morton printk(KERN_ERR "%s: requested out-of-range block %llu for " 1049a1c6f057SDmitry Monakhov "device %pg\n", 10508e24eea7SHarvey Harrison __func__, (unsigned long long)block, 1051a1c6f057SDmitry Monakhov bdev); 1052e5657933SAndrew Morton return -EIO; 1053e5657933SAndrew Morton } 1054676ce6d5SHugh Dickins 10551da177e4SLinus Torvalds /* Create a page with the proper size buffers.. */ 10563b5e6454SGioh Kim return grow_dev_page(bdev, block, index, size, sizebits, gfp); 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10590026ba40SEric Biggers static struct buffer_head * 10603b5e6454SGioh Kim __getblk_slow(struct block_device *bdev, sector_t block, 10613b5e6454SGioh Kim unsigned size, gfp_t gfp) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds /* Size must be multiple of hard sectorsize */ 1064e1defc4fSMartin K. Petersen if (unlikely(size & (bdev_logical_block_size(bdev)-1) || 10651da177e4SLinus Torvalds (size < 512 || size > PAGE_SIZE))) { 10661da177e4SLinus Torvalds printk(KERN_ERR "getblk(): invalid block size %d requested\n", 10671da177e4SLinus Torvalds size); 1068e1defc4fSMartin K. Petersen printk(KERN_ERR "logical block size: %d\n", 1069e1defc4fSMartin K. Petersen bdev_logical_block_size(bdev)); 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds dump_stack(); 10721da177e4SLinus Torvalds return NULL; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 1075676ce6d5SHugh Dickins for (;;) { 1076676ce6d5SHugh Dickins struct buffer_head *bh; 1077676ce6d5SHugh Dickins int ret; 1078676ce6d5SHugh Dickins 10791da177e4SLinus Torvalds bh = __find_get_block(bdev, block, size); 10801da177e4SLinus Torvalds if (bh) 10811da177e4SLinus Torvalds return bh; 10821da177e4SLinus Torvalds 10833b5e6454SGioh Kim ret = grow_buffers(bdev, block, size, gfp); 1084676ce6d5SHugh Dickins if (ret < 0) 108591f68c89SJeff Moyer return NULL; 1086676ce6d5SHugh Dickins } 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * The relationship between dirty buffers and dirty pages: 10911da177e4SLinus Torvalds * 10921da177e4SLinus Torvalds * Whenever a page has any dirty buffers, the page's dirty bit is set, and 1093ec82e1c1SMatthew Wilcox * the page is tagged dirty in the page cache. 10941da177e4SLinus Torvalds * 10951da177e4SLinus Torvalds * At all times, the dirtiness of the buffers represents the dirtiness of 10961da177e4SLinus Torvalds * subsections of the page. If the page has buffers, the page dirty bit is 10971da177e4SLinus Torvalds * merely a hint about the true dirty state. 10981da177e4SLinus Torvalds * 10991da177e4SLinus Torvalds * When a page is set dirty in its entirety, all its buffers are marked dirty 11001da177e4SLinus Torvalds * (if the page has buffers). 11011da177e4SLinus Torvalds * 11021da177e4SLinus Torvalds * When a buffer is marked dirty, its page is dirtied, but the page's other 11031da177e4SLinus Torvalds * buffers are not. 11041da177e4SLinus Torvalds * 11051da177e4SLinus Torvalds * Also. When blockdev buffers are explicitly read with bread(), they 11061da177e4SLinus Torvalds * individually become uptodate. But their backing page remains not 11071da177e4SLinus Torvalds * uptodate - even if all of its buffers are uptodate. A subsequent 11081da177e4SLinus Torvalds * block_read_full_page() against that page will discover all the uptodate 11091da177e4SLinus Torvalds * buffers, will set the page uptodate and will perform no I/O. 11101da177e4SLinus Torvalds */ 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds /** 11131da177e4SLinus Torvalds * mark_buffer_dirty - mark a buffer_head as needing writeout 111467be2dd1SMartin Waitz * @bh: the buffer_head to mark dirty 11151da177e4SLinus Torvalds * 1116ec82e1c1SMatthew Wilcox * mark_buffer_dirty() will set the dirty bit against the buffer, then set 1117ec82e1c1SMatthew Wilcox * its backing page dirty, then tag the page as dirty in the page cache 1118ec82e1c1SMatthew Wilcox * and then attach the address_space's inode to its superblock's dirty 11191da177e4SLinus Torvalds * inode list. 11201da177e4SLinus Torvalds * 11211da177e4SLinus Torvalds * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock, 1122b93b0163SMatthew Wilcox * i_pages lock and mapping->host->i_lock. 11231da177e4SLinus Torvalds */ 1124fc9b52cdSHarvey Harrison void mark_buffer_dirty(struct buffer_head *bh) 11251da177e4SLinus Torvalds { 1126787d2214SNick Piggin WARN_ON_ONCE(!buffer_uptodate(bh)); 11271be62dc1SLinus Torvalds 11285305cb83STejun Heo trace_block_dirty_buffer(bh); 11295305cb83STejun Heo 11301be62dc1SLinus Torvalds /* 11311be62dc1SLinus Torvalds * Very *carefully* optimize the it-is-already-dirty case. 11321be62dc1SLinus Torvalds * 11331be62dc1SLinus Torvalds * Don't let the final "is it dirty" escape to before we 11341be62dc1SLinus Torvalds * perhaps modified the buffer. 11351be62dc1SLinus Torvalds */ 11361be62dc1SLinus Torvalds if (buffer_dirty(bh)) { 11371be62dc1SLinus Torvalds smp_mb(); 11381be62dc1SLinus Torvalds if (buffer_dirty(bh)) 11391be62dc1SLinus Torvalds return; 11401be62dc1SLinus Torvalds } 11411be62dc1SLinus Torvalds 1142a8e7d49aSLinus Torvalds if (!test_set_buffer_dirty(bh)) { 1143a8e7d49aSLinus Torvalds struct page *page = bh->b_page; 1144c4843a75SGreg Thelen struct address_space *mapping = NULL; 1145c4843a75SGreg Thelen 114662cccb8cSJohannes Weiner lock_page_memcg(page); 11478e9d78edSLinus Torvalds if (!TestSetPageDirty(page)) { 1148c4843a75SGreg Thelen mapping = page_mapping(page); 11498e9d78edSLinus Torvalds if (mapping) 115062cccb8cSJohannes Weiner __set_page_dirty(page, mapping, 0); 11518e9d78edSLinus Torvalds } 115262cccb8cSJohannes Weiner unlock_page_memcg(page); 1153c4843a75SGreg Thelen if (mapping) 1154c4843a75SGreg Thelen __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 1155a8e7d49aSLinus Torvalds } 11561da177e4SLinus Torvalds } 11571fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(mark_buffer_dirty); 11581da177e4SLinus Torvalds 115987354e5dSJeff Layton void mark_buffer_write_io_error(struct buffer_head *bh) 116087354e5dSJeff Layton { 116187354e5dSJeff Layton set_buffer_write_io_error(bh); 116287354e5dSJeff Layton /* FIXME: do we need to set this in both places? */ 116387354e5dSJeff Layton if (bh->b_page && bh->b_page->mapping) 116487354e5dSJeff Layton mapping_set_error(bh->b_page->mapping, -EIO); 116587354e5dSJeff Layton if (bh->b_assoc_map) 116687354e5dSJeff Layton mapping_set_error(bh->b_assoc_map, -EIO); 116787354e5dSJeff Layton } 116887354e5dSJeff Layton EXPORT_SYMBOL(mark_buffer_write_io_error); 116987354e5dSJeff Layton 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * Decrement a buffer_head's reference count. If all buffers against a page 11721da177e4SLinus Torvalds * have zero reference count, are clean and unlocked, and if the page is clean 11731da177e4SLinus Torvalds * and unlocked then try_to_free_buffers() may strip the buffers from the page 11741da177e4SLinus Torvalds * in preparation for freeing it (sometimes, rarely, buffers are removed from 11751da177e4SLinus Torvalds * a page but it ends up not being freed, and buffers may later be reattached). 11761da177e4SLinus Torvalds */ 11771da177e4SLinus Torvalds void __brelse(struct buffer_head * buf) 11781da177e4SLinus Torvalds { 11791da177e4SLinus Torvalds if (atomic_read(&buf->b_count)) { 11801da177e4SLinus Torvalds put_bh(buf); 11811da177e4SLinus Torvalds return; 11821da177e4SLinus Torvalds } 11835c752ad9SArjan van de Ven WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n"); 11841da177e4SLinus Torvalds } 11851fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__brelse); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds /* 11881da177e4SLinus Torvalds * bforget() is like brelse(), except it discards any 11891da177e4SLinus Torvalds * potentially dirty data. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds void __bforget(struct buffer_head *bh) 11921da177e4SLinus Torvalds { 11931da177e4SLinus Torvalds clear_buffer_dirty(bh); 1194535ee2fbSJan Kara if (bh->b_assoc_map) { 11951da177e4SLinus Torvalds struct address_space *buffer_mapping = bh->b_page->mapping; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds spin_lock(&buffer_mapping->private_lock); 11981da177e4SLinus Torvalds list_del_init(&bh->b_assoc_buffers); 119958ff407bSJan Kara bh->b_assoc_map = NULL; 12001da177e4SLinus Torvalds spin_unlock(&buffer_mapping->private_lock); 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds __brelse(bh); 12031da177e4SLinus Torvalds } 12041fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(__bforget); 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds static struct buffer_head *__bread_slow(struct buffer_head *bh) 12071da177e4SLinus Torvalds { 12081da177e4SLinus Torvalds lock_buffer(bh); 12091da177e4SLinus Torvalds if (buffer_uptodate(bh)) { 12101da177e4SLinus Torvalds unlock_buffer(bh); 12111da177e4SLinus Torvalds return bh; 12121da177e4SLinus Torvalds } else { 12131da177e4SLinus Torvalds get_bh(bh); 12141da177e4SLinus Torvalds bh->b_end_io = end_buffer_read_sync; 12152a222ca9SMike Christie submit_bh(REQ_OP_READ, 0, bh); 12161da177e4SLinus Torvalds wait_on_buffer(bh); 12171da177e4SLinus Torvalds if (buffer_uptodate(bh)) 12181da177e4SLinus Torvalds return bh; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds brelse(bh); 12211da177e4SLinus Torvalds return NULL; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds /* 12251da177e4SLinus Torvalds * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block(). 12261da177e4SLinus Torvalds * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their 12271da177e4SLinus Torvalds * refcount elevated by one when they're in an LRU. A buffer can only appear 12281da177e4SLinus Torvalds * once in a particular CPU's LRU. A single buffer can be present in multiple 12291da177e4SLinus Torvalds * CPU's LRUs at the same time. 12301da177e4SLinus Torvalds * 12311da177e4SLinus Torvalds * This is a transparent caching front-end to sb_bread(), sb_getblk() and 12321da177e4SLinus Torvalds * sb_find_get_block(). 12331da177e4SLinus Torvalds * 12341da177e4SLinus Torvalds * The LRUs themselves only need locking against invalidate_bh_lrus. We use 12351da177e4SLinus Torvalds * a local interrupt disable for that. 12361da177e4SLinus Torvalds */ 12371da177e4SLinus Torvalds 123886cf78d7SSebastien Buisson #define BH_LRU_SIZE 16 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds struct bh_lru { 12411da177e4SLinus Torvalds struct buffer_head *bhs[BH_LRU_SIZE]; 12421da177e4SLinus Torvalds }; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }}; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds #ifdef CONFIG_SMP 12471da177e4SLinus Torvalds #define bh_lru_lock() local_irq_disable() 12481da177e4SLinus Torvalds #define bh_lru_unlock() local_irq_enable() 12491da177e4SLinus Torvalds #else 12501da177e4SLinus Torvalds #define bh_lru_lock() preempt_disable() 12511da177e4SLinus Torvalds #define bh_lru_unlock() preempt_enable() 12521da177e4SLinus Torvalds #endif 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds static inline void check_irqs_on(void) 12551da177e4SLinus Torvalds { 12561da177e4SLinus Torvalds #ifdef irqs_disabled 12571da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 12581da177e4SLinus Torvalds #endif 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds /* 1262241f01fbSEric Biggers * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is 1263241f01fbSEric Biggers * inserted at the front, and the buffer_head at the back if any is evicted. 1264241f01fbSEric Biggers * Or, if already in the LRU it is moved to the front. 12651da177e4SLinus Torvalds */ 12661da177e4SLinus Torvalds static void bh_lru_install(struct buffer_head *bh) 12671da177e4SLinus Torvalds { 1268241f01fbSEric Biggers struct buffer_head *evictee = bh; 1269241f01fbSEric Biggers struct bh_lru *b; 1270241f01fbSEric Biggers int i; 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds check_irqs_on(); 12731da177e4SLinus Torvalds bh_lru_lock(); 1274241f01fbSEric Biggers 1275241f01fbSEric Biggers b = this_cpu_ptr(&bh_lrus); 1276241f01fbSEric Biggers for (i = 0; i < BH_LRU_SIZE; i++) { 1277241f01fbSEric Biggers swap(evictee, b->bhs[i]); 1278241f01fbSEric Biggers if (evictee == bh) { 1279241f01fbSEric Biggers bh_lru_unlock(); 1280241f01fbSEric Biggers return; 1281241f01fbSEric Biggers } 1282241f01fbSEric Biggers } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds get_bh(bh); 12851da177e4SLinus Torvalds bh_lru_unlock(); 1286241f01fbSEric Biggers brelse(evictee); 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds /* 12901da177e4SLinus Torvalds * Look up the bh in this cpu's LRU. If it's there, move it to the head. 12911da177e4SLinus Torvalds */ 1292858119e1SArjan van de Ven static struct buffer_head * 12933991d3bdSTomasz Kvarsin lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds struct buffer_head *ret = NULL; 12963991d3bdSTomasz Kvarsin unsigned int i; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds check_irqs_on(); 12991da177e4SLinus Torvalds bh_lru_lock(); 13001da177e4SLinus Torvalds for (i = 0; i < BH_LRU_SIZE; i++) { 1301c7b92516SChristoph Lameter struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]); 13021da177e4SLinus Torvalds 13039470dd5dSZach Brown if (bh && bh->b_blocknr == block && bh->b_bdev == bdev && 13049470dd5dSZach Brown bh->b_size == size) { 13051da177e4SLinus Torvalds if (i) { 13061da177e4SLinus Torvalds while (i) { 1307c7b92516SChristoph Lameter __this_cpu_write(bh_lrus.bhs[i], 1308c7b92516SChristoph Lameter __this_cpu_read(bh_lrus.bhs[i - 1])); 13091da177e4SLinus Torvalds i--; 13101da177e4SLinus Torvalds } 1311c7b92516SChristoph Lameter __this_cpu_write(bh_lrus.bhs[0], bh); 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds get_bh(bh); 13141da177e4SLinus Torvalds ret = bh; 13151da177e4SLinus Torvalds break; 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds bh_lru_unlock(); 13191da177e4SLinus Torvalds return ret; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Perform a pagecache lookup for the matching buffer. If it's there, refresh 13241da177e4SLinus Torvalds * it in the LRU and mark it as accessed. If it is not present then return 13251da177e4SLinus Torvalds * NULL 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds struct buffer_head * 13283991d3bdSTomasz Kvarsin __find_get_block(struct block_device *bdev, sector_t block, unsigned size) 13291da177e4SLinus Torvalds { 13301da177e4SLinus Torvalds struct buffer_head *bh = lookup_bh_lru(bdev, block, size); 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds if (bh == NULL) { 13332457aec6SMel Gorman /* __find_get_block_slow will mark the page accessed */ 1334385fd4c5SCoywolf Qi Hunt bh = __find_get_block_slow(bdev, block); 13351da177e4SLinus Torvalds if (bh) 13361da177e4SLinus Torvalds bh_lru_install(bh); 13372457aec6SMel Gorman } else 13381da177e4SLinus Torvalds touch_buffer(bh); 13392457aec6SMel Gorman 13401da177e4SLinus Torvalds return bh; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds EXPORT_SYMBOL(__find_get_block); 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds /* 13453b5e6454SGioh Kim * __getblk_gfp() will locate (and, if necessary, create) the buffer_head 13461da177e4SLinus Torvalds * which corresponds to the passed block_device, block and size. The 13471da177e4SLinus Torvalds * returned buffer has its reference count incremented. 13481da177e4SLinus Torvalds * 13493b5e6454SGioh Kim * __getblk_gfp() will lock up the machine if grow_dev_page's 13503b5e6454SGioh Kim * try_to_free_buffers() attempt is failing. FIXME, perhaps? 13511da177e4SLinus Torvalds */ 13521da177e4SLinus Torvalds struct buffer_head * 13533b5e6454SGioh Kim __getblk_gfp(struct block_device *bdev, sector_t block, 13543b5e6454SGioh Kim unsigned size, gfp_t gfp) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds struct buffer_head *bh = __find_get_block(bdev, block, size); 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds might_sleep(); 13591da177e4SLinus Torvalds if (bh == NULL) 13603b5e6454SGioh Kim bh = __getblk_slow(bdev, block, size, gfp); 13611da177e4SLinus Torvalds return bh; 13621da177e4SLinus Torvalds } 13633b5e6454SGioh Kim EXPORT_SYMBOL(__getblk_gfp); 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds /* 13661da177e4SLinus Torvalds * Do async read-ahead on a buffer.. 13671da177e4SLinus Torvalds */ 13683991d3bdSTomasz Kvarsin void __breadahead(struct block_device *bdev, sector_t block, unsigned size) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct buffer_head *bh = __getblk(bdev, block, size); 1371a3e713b5SAndrew Morton if (likely(bh)) { 137270246286SChristoph Hellwig ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh); 13731da177e4SLinus Torvalds brelse(bh); 13741da177e4SLinus Torvalds } 1375a3e713b5SAndrew Morton } 13761da177e4SLinus Torvalds EXPORT_SYMBOL(__breadahead); 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds /** 13793b5e6454SGioh Kim * __bread_gfp() - reads a specified block and returns the bh 138067be2dd1SMartin Waitz * @bdev: the block_device to read from 13811da177e4SLinus Torvalds * @block: number of block 13821da177e4SLinus Torvalds * @size: size (in bytes) to read 13833b5e6454SGioh Kim * @gfp: page allocation flag 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * Reads a specified block, and returns buffer head that contains it. 13863b5e6454SGioh Kim * The page cache can be allocated from non-movable area 13873b5e6454SGioh Kim * not to prevent page migration if you set gfp to zero. 13881da177e4SLinus Torvalds * It returns NULL if the block was unreadable. 13891da177e4SLinus Torvalds */ 13901da177e4SLinus Torvalds struct buffer_head * 13913b5e6454SGioh Kim __bread_gfp(struct block_device *bdev, sector_t block, 13923b5e6454SGioh Kim unsigned size, gfp_t gfp) 13931da177e4SLinus Torvalds { 13943b5e6454SGioh Kim struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp); 13951da177e4SLinus Torvalds 1396a3e713b5SAndrew Morton if (likely(bh) && !buffer_uptodate(bh)) 13971da177e4SLinus Torvalds bh = __bread_slow(bh); 13981da177e4SLinus Torvalds return bh; 13991da177e4SLinus Torvalds } 14003b5e6454SGioh Kim EXPORT_SYMBOL(__bread_gfp); 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds /* 14031da177e4SLinus Torvalds * invalidate_bh_lrus() is called rarely - but not only at unmount. 14041da177e4SLinus Torvalds * This doesn't race because it runs in each cpu either in irq 14051da177e4SLinus Torvalds * or with preempt disabled. 14061da177e4SLinus Torvalds */ 14071da177e4SLinus Torvalds static void invalidate_bh_lru(void *arg) 14081da177e4SLinus Torvalds { 14091da177e4SLinus Torvalds struct bh_lru *b = &get_cpu_var(bh_lrus); 14101da177e4SLinus Torvalds int i; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds for (i = 0; i < BH_LRU_SIZE; i++) { 14131da177e4SLinus Torvalds brelse(b->bhs[i]); 14141da177e4SLinus Torvalds b->bhs[i] = NULL; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds put_cpu_var(bh_lrus); 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 141942be35d0SGilad Ben-Yossef static bool has_bh_in_lru(int cpu, void *dummy) 142042be35d0SGilad Ben-Yossef { 142142be35d0SGilad Ben-Yossef struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu); 142242be35d0SGilad Ben-Yossef int i; 142342be35d0SGilad Ben-Yossef 142442be35d0SGilad Ben-Yossef for (i = 0; i < BH_LRU_SIZE; i++) { 142542be35d0SGilad Ben-Yossef if (b->bhs[i]) 1426*1d706679SSaurav Girepunje return true; 142742be35d0SGilad Ben-Yossef } 142842be35d0SGilad Ben-Yossef 1429*1d706679SSaurav Girepunje return false; 143042be35d0SGilad Ben-Yossef } 143142be35d0SGilad Ben-Yossef 1432f9a14399SPeter Zijlstra void invalidate_bh_lrus(void) 14331da177e4SLinus Torvalds { 143442be35d0SGilad Ben-Yossef on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL); 14351da177e4SLinus Torvalds } 14369db5579bSNick Piggin EXPORT_SYMBOL_GPL(invalidate_bh_lrus); 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds void set_bh_page(struct buffer_head *bh, 14391da177e4SLinus Torvalds struct page *page, unsigned long offset) 14401da177e4SLinus Torvalds { 14411da177e4SLinus Torvalds bh->b_page = page; 1442e827f923SEric Sesterhenn BUG_ON(offset >= PAGE_SIZE); 14431da177e4SLinus Torvalds if (PageHighMem(page)) 14441da177e4SLinus Torvalds /* 14451da177e4SLinus Torvalds * This catches illegal uses and preserves the offset: 14461da177e4SLinus Torvalds */ 14471da177e4SLinus Torvalds bh->b_data = (char *)(0 + offset); 14481da177e4SLinus Torvalds else 14491da177e4SLinus Torvalds bh->b_data = page_address(page) + offset; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds EXPORT_SYMBOL(set_bh_page); 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds /* 14541da177e4SLinus Torvalds * Called when truncating a buffer on a page completely. 14551da177e4SLinus Torvalds */ 1456e7470ee8SMel Gorman 1457e7470ee8SMel Gorman /* Bits that are cleared during an invalidate */ 1458e7470ee8SMel Gorman #define BUFFER_FLAGS_DISCARD \ 1459e7470ee8SMel Gorman (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \ 1460e7470ee8SMel Gorman 1 << BH_Delay | 1 << BH_Unwritten) 1461e7470ee8SMel Gorman 1462858119e1SArjan van de Ven static void discard_buffer(struct buffer_head * bh) 14631da177e4SLinus Torvalds { 1464e7470ee8SMel Gorman unsigned long b_state, b_state_old; 1465e7470ee8SMel Gorman 14661da177e4SLinus Torvalds lock_buffer(bh); 14671da177e4SLinus Torvalds clear_buffer_dirty(bh); 14681da177e4SLinus Torvalds bh->b_bdev = NULL; 1469e7470ee8SMel Gorman b_state = bh->b_state; 1470e7470ee8SMel Gorman for (;;) { 1471e7470ee8SMel Gorman b_state_old = cmpxchg(&bh->b_state, b_state, 1472e7470ee8SMel Gorman (b_state & ~BUFFER_FLAGS_DISCARD)); 1473e7470ee8SMel Gorman if (b_state_old == b_state) 1474e7470ee8SMel Gorman break; 1475e7470ee8SMel Gorman b_state = b_state_old; 1476e7470ee8SMel Gorman } 14771da177e4SLinus Torvalds unlock_buffer(bh); 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds /** 1481814e1d25SWang Sheng-Hui * block_invalidatepage - invalidate part or all of a buffer-backed page 14821da177e4SLinus Torvalds * 14831da177e4SLinus Torvalds * @page: the page which is affected 1484d47992f8SLukas Czerner * @offset: start of the range to invalidate 1485d47992f8SLukas Czerner * @length: length of the range to invalidate 14861da177e4SLinus Torvalds * 14871da177e4SLinus Torvalds * block_invalidatepage() is called when all or part of the page has become 14881da177e4SLinus Torvalds * invalidated by a truncate operation. 14891da177e4SLinus Torvalds * 14901da177e4SLinus Torvalds * block_invalidatepage() does not have to release all buffers, but it must 14911da177e4SLinus Torvalds * ensure that no dirty buffer is left outside @offset and that no I/O 14921da177e4SLinus Torvalds * is underway against any of the blocks which are outside the truncation 14931da177e4SLinus Torvalds * point. Because the caller is about to free (and possibly reuse) those 14941da177e4SLinus Torvalds * blocks on-disk. 14951da177e4SLinus Torvalds */ 1496d47992f8SLukas Czerner void block_invalidatepage(struct page *page, unsigned int offset, 1497d47992f8SLukas Czerner unsigned int length) 14981da177e4SLinus Torvalds { 14991da177e4SLinus Torvalds struct buffer_head *head, *bh, *next; 15001da177e4SLinus Torvalds unsigned int curr_off = 0; 1501d47992f8SLukas Czerner unsigned int stop = length + offset; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 15041da177e4SLinus Torvalds if (!page_has_buffers(page)) 15051da177e4SLinus Torvalds goto out; 15061da177e4SLinus Torvalds 1507d47992f8SLukas Czerner /* 1508d47992f8SLukas Czerner * Check for overflow 1509d47992f8SLukas Czerner */ 151009cbfeafSKirill A. Shutemov BUG_ON(stop > PAGE_SIZE || stop < length); 1511d47992f8SLukas Czerner 15121da177e4SLinus Torvalds head = page_buffers(page); 15131da177e4SLinus Torvalds bh = head; 15141da177e4SLinus Torvalds do { 15151da177e4SLinus Torvalds unsigned int next_off = curr_off + bh->b_size; 15161da177e4SLinus Torvalds next = bh->b_this_page; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* 1519d47992f8SLukas Czerner * Are we still fully in range ? 1520d47992f8SLukas Czerner */ 1521d47992f8SLukas Czerner if (next_off > stop) 1522d47992f8SLukas Czerner goto out; 1523d47992f8SLukas Czerner 1524d47992f8SLukas Czerner /* 15251da177e4SLinus Torvalds * is this block fully invalidated? 15261da177e4SLinus Torvalds */ 15271da177e4SLinus Torvalds if (offset <= curr_off) 15281da177e4SLinus Torvalds discard_buffer(bh); 15291da177e4SLinus Torvalds curr_off = next_off; 15301da177e4SLinus Torvalds bh = next; 15311da177e4SLinus Torvalds } while (bh != head); 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds /* 15341da177e4SLinus Torvalds * We release buffers only if the entire page is being invalidated. 15351da177e4SLinus Torvalds * The get_block cached value has been unconditionally invalidated, 15361da177e4SLinus Torvalds * so real IO is not possible anymore. 15371da177e4SLinus Torvalds */ 15383172485fSJeff Moyer if (length == PAGE_SIZE) 15392ff28e22SNeilBrown try_to_release_page(page, 0); 15401da177e4SLinus Torvalds out: 15412ff28e22SNeilBrown return; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds EXPORT_SYMBOL(block_invalidatepage); 15441da177e4SLinus Torvalds 1545d47992f8SLukas Czerner 15461da177e4SLinus Torvalds /* 15471da177e4SLinus Torvalds * We attach and possibly dirty the buffers atomically wrt 15481da177e4SLinus Torvalds * __set_page_dirty_buffers() via private_lock. try_to_free_buffers 15491da177e4SLinus Torvalds * is already excluded via the page lock. 15501da177e4SLinus Torvalds */ 15511da177e4SLinus Torvalds void create_empty_buffers(struct page *page, 15521da177e4SLinus Torvalds unsigned long blocksize, unsigned long b_state) 15531da177e4SLinus Torvalds { 15541da177e4SLinus Torvalds struct buffer_head *bh, *head, *tail; 15551da177e4SLinus Torvalds 1556640ab98fSJens Axboe head = alloc_page_buffers(page, blocksize, true); 15571da177e4SLinus Torvalds bh = head; 15581da177e4SLinus Torvalds do { 15591da177e4SLinus Torvalds bh->b_state |= b_state; 15601da177e4SLinus Torvalds tail = bh; 15611da177e4SLinus Torvalds bh = bh->b_this_page; 15621da177e4SLinus Torvalds } while (bh); 15631da177e4SLinus Torvalds tail->b_this_page = head; 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds spin_lock(&page->mapping->private_lock); 15661da177e4SLinus Torvalds if (PageUptodate(page) || PageDirty(page)) { 15671da177e4SLinus Torvalds bh = head; 15681da177e4SLinus Torvalds do { 15691da177e4SLinus Torvalds if (PageDirty(page)) 15701da177e4SLinus Torvalds set_buffer_dirty(bh); 15711da177e4SLinus Torvalds if (PageUptodate(page)) 15721da177e4SLinus Torvalds set_buffer_uptodate(bh); 15731da177e4SLinus Torvalds bh = bh->b_this_page; 15741da177e4SLinus Torvalds } while (bh != head); 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds attach_page_buffers(page, head); 15771da177e4SLinus Torvalds spin_unlock(&page->mapping->private_lock); 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds EXPORT_SYMBOL(create_empty_buffers); 15801da177e4SLinus Torvalds 158129f3ad7dSJan Kara /** 158229f3ad7dSJan Kara * clean_bdev_aliases: clean a range of buffers in block device 158329f3ad7dSJan Kara * @bdev: Block device to clean buffers in 158429f3ad7dSJan Kara * @block: Start of a range of blocks to clean 158529f3ad7dSJan Kara * @len: Number of blocks to clean 15861da177e4SLinus Torvalds * 158729f3ad7dSJan Kara * We are taking a range of blocks for data and we don't want writeback of any 158829f3ad7dSJan Kara * buffer-cache aliases starting from return from this function and until the 158929f3ad7dSJan Kara * moment when something will explicitly mark the buffer dirty (hopefully that 159029f3ad7dSJan Kara * will not happen until we will free that block ;-) We don't even need to mark 159129f3ad7dSJan Kara * it not-uptodate - nobody can expect anything from a newly allocated buffer 159229f3ad7dSJan Kara * anyway. We used to use unmap_buffer() for such invalidation, but that was 159329f3ad7dSJan Kara * wrong. We definitely don't want to mark the alias unmapped, for example - it 159429f3ad7dSJan Kara * would confuse anyone who might pick it with bread() afterwards... 159529f3ad7dSJan Kara * 159629f3ad7dSJan Kara * Also.. Note that bforget() doesn't lock the buffer. So there can be 159729f3ad7dSJan Kara * writeout I/O going on against recently-freed buffers. We don't wait on that 159829f3ad7dSJan Kara * I/O in bforget() - it's more efficient to wait on the I/O only if we really 159929f3ad7dSJan Kara * need to. That happens here. 16001da177e4SLinus Torvalds */ 160129f3ad7dSJan Kara void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len) 16021da177e4SLinus Torvalds { 160329f3ad7dSJan Kara struct inode *bd_inode = bdev->bd_inode; 160429f3ad7dSJan Kara struct address_space *bd_mapping = bd_inode->i_mapping; 160529f3ad7dSJan Kara struct pagevec pvec; 160629f3ad7dSJan Kara pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits); 160729f3ad7dSJan Kara pgoff_t end; 1608c10f778dSJan Kara int i, count; 160929f3ad7dSJan Kara struct buffer_head *bh; 161029f3ad7dSJan Kara struct buffer_head *head; 16111da177e4SLinus Torvalds 161229f3ad7dSJan Kara end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits); 161386679820SMel Gorman pagevec_init(&pvec); 1614397162ffSJan Kara while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) { 1615c10f778dSJan Kara count = pagevec_count(&pvec); 1616c10f778dSJan Kara for (i = 0; i < count; i++) { 161729f3ad7dSJan Kara struct page *page = pvec.pages[i]; 16181da177e4SLinus Torvalds 161929f3ad7dSJan Kara if (!page_has_buffers(page)) 162029f3ad7dSJan Kara continue; 162129f3ad7dSJan Kara /* 162229f3ad7dSJan Kara * We use page lock instead of bd_mapping->private_lock 162329f3ad7dSJan Kara * to pin buffers here since we can afford to sleep and 162429f3ad7dSJan Kara * it scales better than a global spinlock lock. 162529f3ad7dSJan Kara */ 162629f3ad7dSJan Kara lock_page(page); 162729f3ad7dSJan Kara /* Recheck when the page is locked which pins bhs */ 162829f3ad7dSJan Kara if (!page_has_buffers(page)) 162929f3ad7dSJan Kara goto unlock_page; 163029f3ad7dSJan Kara head = page_buffers(page); 163129f3ad7dSJan Kara bh = head; 163229f3ad7dSJan Kara do { 16336c006a9dSChandan Rajendra if (!buffer_mapped(bh) || (bh->b_blocknr < block)) 163429f3ad7dSJan Kara goto next; 163529f3ad7dSJan Kara if (bh->b_blocknr >= block + len) 163629f3ad7dSJan Kara break; 163729f3ad7dSJan Kara clear_buffer_dirty(bh); 163829f3ad7dSJan Kara wait_on_buffer(bh); 163929f3ad7dSJan Kara clear_buffer_req(bh); 164029f3ad7dSJan Kara next: 164129f3ad7dSJan Kara bh = bh->b_this_page; 164229f3ad7dSJan Kara } while (bh != head); 164329f3ad7dSJan Kara unlock_page: 164429f3ad7dSJan Kara unlock_page(page); 164529f3ad7dSJan Kara } 164629f3ad7dSJan Kara pagevec_release(&pvec); 164729f3ad7dSJan Kara cond_resched(); 1648c10f778dSJan Kara /* End of range already reached? */ 1649c10f778dSJan Kara if (index > end || !index) 1650c10f778dSJan Kara break; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds } 165329f3ad7dSJan Kara EXPORT_SYMBOL(clean_bdev_aliases); 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds /* 165645bce8f3SLinus Torvalds * Size is a power-of-two in the range 512..PAGE_SIZE, 165745bce8f3SLinus Torvalds * and the case we care about most is PAGE_SIZE. 165845bce8f3SLinus Torvalds * 165945bce8f3SLinus Torvalds * So this *could* possibly be written with those 166045bce8f3SLinus Torvalds * constraints in mind (relevant mostly if some 166145bce8f3SLinus Torvalds * architecture has a slow bit-scan instruction) 166245bce8f3SLinus Torvalds */ 166345bce8f3SLinus Torvalds static inline int block_size_bits(unsigned int blocksize) 166445bce8f3SLinus Torvalds { 166545bce8f3SLinus Torvalds return ilog2(blocksize); 166645bce8f3SLinus Torvalds } 166745bce8f3SLinus Torvalds 166845bce8f3SLinus Torvalds static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state) 166945bce8f3SLinus Torvalds { 167045bce8f3SLinus Torvalds BUG_ON(!PageLocked(page)); 167145bce8f3SLinus Torvalds 167245bce8f3SLinus Torvalds if (!page_has_buffers(page)) 16736aa7de05SMark Rutland create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits), 16746aa7de05SMark Rutland b_state); 167545bce8f3SLinus Torvalds return page_buffers(page); 167645bce8f3SLinus Torvalds } 167745bce8f3SLinus Torvalds 167845bce8f3SLinus Torvalds /* 16791da177e4SLinus Torvalds * NOTE! All mapped/uptodate combinations are valid: 16801da177e4SLinus Torvalds * 16811da177e4SLinus Torvalds * Mapped Uptodate Meaning 16821da177e4SLinus Torvalds * 16831da177e4SLinus Torvalds * No No "unknown" - must do get_block() 16841da177e4SLinus Torvalds * No Yes "hole" - zero-filled 16851da177e4SLinus Torvalds * Yes No "allocated" - allocated on disk, not read in 16861da177e4SLinus Torvalds * Yes Yes "valid" - allocated and up-to-date in memory. 16871da177e4SLinus Torvalds * 16881da177e4SLinus Torvalds * "Dirty" is valid only with the last case (mapped+uptodate). 16891da177e4SLinus Torvalds */ 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds /* 16921da177e4SLinus Torvalds * While block_write_full_page is writing back the dirty buffers under 16931da177e4SLinus Torvalds * the page lock, whoever dirtied the buffers may decide to clean them 16941da177e4SLinus Torvalds * again at any time. We handle that by only looking at the buffer 16951da177e4SLinus Torvalds * state inside lock_buffer(). 16961da177e4SLinus Torvalds * 16971da177e4SLinus Torvalds * If block_write_full_page() is called for regular writeback 16981da177e4SLinus Torvalds * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a 16991da177e4SLinus Torvalds * locked buffer. This only can happen if someone has written the buffer 17001da177e4SLinus Torvalds * directly, with submit_bh(). At the address_space level PageWriteback 17011da177e4SLinus Torvalds * prevents this contention from occurring. 17026e34eeddSTheodore Ts'o * 17036e34eeddSTheodore Ts'o * If block_write_full_page() is called with wbc->sync_mode == 170470fd7614SChristoph Hellwig * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this 1705721a9602SJens Axboe * causes the writes to be flagged as synchronous writes. 17061da177e4SLinus Torvalds */ 1707b4bba389SBenjamin Marzinski int __block_write_full_page(struct inode *inode, struct page *page, 170835c80d5fSChris Mason get_block_t *get_block, struct writeback_control *wbc, 170935c80d5fSChris Mason bh_end_io_t *handler) 17101da177e4SLinus Torvalds { 17111da177e4SLinus Torvalds int err; 17121da177e4SLinus Torvalds sector_t block; 17131da177e4SLinus Torvalds sector_t last_block; 1714f0fbd5fcSAndrew Morton struct buffer_head *bh, *head; 171545bce8f3SLinus Torvalds unsigned int blocksize, bbits; 17161da177e4SLinus Torvalds int nr_underway = 0; 17177637241eSJens Axboe int write_flags = wbc_to_write_flags(wbc); 17181da177e4SLinus Torvalds 171945bce8f3SLinus Torvalds head = create_page_buffers(page, inode, 17201da177e4SLinus Torvalds (1 << BH_Dirty)|(1 << BH_Uptodate)); 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds /* 17231da177e4SLinus Torvalds * Be very careful. We have no exclusion from __set_page_dirty_buffers 17241da177e4SLinus Torvalds * here, and the (potentially unmapped) buffers may become dirty at 17251da177e4SLinus Torvalds * any time. If a buffer becomes dirty here after we've inspected it 17261da177e4SLinus Torvalds * then we just miss that fact, and the page stays dirty. 17271da177e4SLinus Torvalds * 17281da177e4SLinus Torvalds * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; 17291da177e4SLinus Torvalds * handle that here by just cleaning them. 17301da177e4SLinus Torvalds */ 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds bh = head; 173345bce8f3SLinus Torvalds blocksize = bh->b_size; 173445bce8f3SLinus Torvalds bbits = block_size_bits(blocksize); 173545bce8f3SLinus Torvalds 173609cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 173745bce8f3SLinus Torvalds last_block = (i_size_read(inode) - 1) >> bbits; 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds /* 17401da177e4SLinus Torvalds * Get all the dirty buffers mapped to disk addresses and 17411da177e4SLinus Torvalds * handle any aliases from the underlying blockdev's mapping. 17421da177e4SLinus Torvalds */ 17431da177e4SLinus Torvalds do { 17441da177e4SLinus Torvalds if (block > last_block) { 17451da177e4SLinus Torvalds /* 17461da177e4SLinus Torvalds * mapped buffers outside i_size will occur, because 17471da177e4SLinus Torvalds * this page can be outside i_size when there is a 17481da177e4SLinus Torvalds * truncate in progress. 17491da177e4SLinus Torvalds */ 17501da177e4SLinus Torvalds /* 17511da177e4SLinus Torvalds * The buffer was zeroed by block_write_full_page() 17521da177e4SLinus Torvalds */ 17531da177e4SLinus Torvalds clear_buffer_dirty(bh); 17541da177e4SLinus Torvalds set_buffer_uptodate(bh); 175529a814d2SAlex Tomas } else if ((!buffer_mapped(bh) || buffer_delay(bh)) && 175629a814d2SAlex Tomas buffer_dirty(bh)) { 1757b0cf2321SBadari Pulavarty WARN_ON(bh->b_size != blocksize); 17581da177e4SLinus Torvalds err = get_block(inode, block, bh, 1); 17591da177e4SLinus Torvalds if (err) 17601da177e4SLinus Torvalds goto recover; 176129a814d2SAlex Tomas clear_buffer_delay(bh); 17621da177e4SLinus Torvalds if (buffer_new(bh)) { 17631da177e4SLinus Torvalds /* blockdev mappings never come here */ 17641da177e4SLinus Torvalds clear_buffer_new(bh); 1765e64855c6SJan Kara clean_bdev_bh_alias(bh); 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds bh = bh->b_this_page; 17691da177e4SLinus Torvalds block++; 17701da177e4SLinus Torvalds } while (bh != head); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds do { 17731da177e4SLinus Torvalds if (!buffer_mapped(bh)) 17741da177e4SLinus Torvalds continue; 17751da177e4SLinus Torvalds /* 17761da177e4SLinus Torvalds * If it's a fully non-blocking write attempt and we cannot 17771da177e4SLinus Torvalds * lock the buffer then redirty the page. Note that this can 17785b0830cbSJens Axboe * potentially cause a busy-wait loop from writeback threads 17795b0830cbSJens Axboe * and kswapd activity, but those code paths have their own 17805b0830cbSJens Axboe * higher-level throttling. 17811da177e4SLinus Torvalds */ 17821b430beeSWu Fengguang if (wbc->sync_mode != WB_SYNC_NONE) { 17831da177e4SLinus Torvalds lock_buffer(bh); 1784ca5de404SNick Piggin } else if (!trylock_buffer(bh)) { 17851da177e4SLinus Torvalds redirty_page_for_writepage(wbc, page); 17861da177e4SLinus Torvalds continue; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds if (test_clear_buffer_dirty(bh)) { 178935c80d5fSChris Mason mark_buffer_async_write_endio(bh, handler); 17901da177e4SLinus Torvalds } else { 17911da177e4SLinus Torvalds unlock_buffer(bh); 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds } while ((bh = bh->b_this_page) != head); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds /* 17961da177e4SLinus Torvalds * The page and its buffers are protected by PageWriteback(), so we can 17971da177e4SLinus Torvalds * drop the bh refcounts early. 17981da177e4SLinus Torvalds */ 17991da177e4SLinus Torvalds BUG_ON(PageWriteback(page)); 18001da177e4SLinus Torvalds set_page_writeback(page); 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds do { 18031da177e4SLinus Torvalds struct buffer_head *next = bh->b_this_page; 18041da177e4SLinus Torvalds if (buffer_async_write(bh)) { 18058e8f9298SJens Axboe submit_bh_wbc(REQ_OP_WRITE, write_flags, bh, 18068e8f9298SJens Axboe inode->i_write_hint, wbc); 18071da177e4SLinus Torvalds nr_underway++; 1808ad576e63SNick Piggin } 18091da177e4SLinus Torvalds bh = next; 18101da177e4SLinus Torvalds } while (bh != head); 181105937baaSAndrew Morton unlock_page(page); 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds err = 0; 18141da177e4SLinus Torvalds done: 18151da177e4SLinus Torvalds if (nr_underway == 0) { 18161da177e4SLinus Torvalds /* 18171da177e4SLinus Torvalds * The page was marked dirty, but the buffers were 18181da177e4SLinus Torvalds * clean. Someone wrote them back by hand with 18191da177e4SLinus Torvalds * ll_rw_block/submit_bh. A rare case. 18201da177e4SLinus Torvalds */ 18211da177e4SLinus Torvalds end_page_writeback(page); 18223d67f2d7SNick Piggin 18231da177e4SLinus Torvalds /* 18241da177e4SLinus Torvalds * The page and buffer_heads can be released at any time from 18251da177e4SLinus Torvalds * here on. 18261da177e4SLinus Torvalds */ 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds return err; 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds recover: 18311da177e4SLinus Torvalds /* 18321da177e4SLinus Torvalds * ENOSPC, or some other error. We may already have added some 18331da177e4SLinus Torvalds * blocks to the file, so we need to write these out to avoid 18341da177e4SLinus Torvalds * exposing stale data. 18351da177e4SLinus Torvalds * The page is currently locked and not marked for writeback 18361da177e4SLinus Torvalds */ 18371da177e4SLinus Torvalds bh = head; 18381da177e4SLinus Torvalds /* Recovery: lock and submit the mapped buffers */ 18391da177e4SLinus Torvalds do { 184029a814d2SAlex Tomas if (buffer_mapped(bh) && buffer_dirty(bh) && 184129a814d2SAlex Tomas !buffer_delay(bh)) { 18421da177e4SLinus Torvalds lock_buffer(bh); 184335c80d5fSChris Mason mark_buffer_async_write_endio(bh, handler); 18441da177e4SLinus Torvalds } else { 18451da177e4SLinus Torvalds /* 18461da177e4SLinus Torvalds * The buffer may have been set dirty during 18471da177e4SLinus Torvalds * attachment to a dirty page. 18481da177e4SLinus Torvalds */ 18491da177e4SLinus Torvalds clear_buffer_dirty(bh); 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds } while ((bh = bh->b_this_page) != head); 18521da177e4SLinus Torvalds SetPageError(page); 18531da177e4SLinus Torvalds BUG_ON(PageWriteback(page)); 18547e4c3690SAndrew Morton mapping_set_error(page->mapping, err); 18551da177e4SLinus Torvalds set_page_writeback(page); 18561da177e4SLinus Torvalds do { 18571da177e4SLinus Torvalds struct buffer_head *next = bh->b_this_page; 18581da177e4SLinus Torvalds if (buffer_async_write(bh)) { 18591da177e4SLinus Torvalds clear_buffer_dirty(bh); 18608e8f9298SJens Axboe submit_bh_wbc(REQ_OP_WRITE, write_flags, bh, 18618e8f9298SJens Axboe inode->i_write_hint, wbc); 18621da177e4SLinus Torvalds nr_underway++; 1863ad576e63SNick Piggin } 18641da177e4SLinus Torvalds bh = next; 18651da177e4SLinus Torvalds } while (bh != head); 1866ffda9d30SNick Piggin unlock_page(page); 18671da177e4SLinus Torvalds goto done; 18681da177e4SLinus Torvalds } 1869b4bba389SBenjamin Marzinski EXPORT_SYMBOL(__block_write_full_page); 18701da177e4SLinus Torvalds 1871afddba49SNick Piggin /* 1872afddba49SNick Piggin * If a page has any new buffers, zero them out here, and mark them uptodate 1873afddba49SNick Piggin * and dirty so they'll be written out (in order to prevent uninitialised 1874afddba49SNick Piggin * block data from leaking). And clear the new bit. 1875afddba49SNick Piggin */ 1876afddba49SNick Piggin void page_zero_new_buffers(struct page *page, unsigned from, unsigned to) 1877afddba49SNick Piggin { 1878afddba49SNick Piggin unsigned int block_start, block_end; 1879afddba49SNick Piggin struct buffer_head *head, *bh; 1880afddba49SNick Piggin 1881afddba49SNick Piggin BUG_ON(!PageLocked(page)); 1882afddba49SNick Piggin if (!page_has_buffers(page)) 1883afddba49SNick Piggin return; 1884afddba49SNick Piggin 1885afddba49SNick Piggin bh = head = page_buffers(page); 1886afddba49SNick Piggin block_start = 0; 1887afddba49SNick Piggin do { 1888afddba49SNick Piggin block_end = block_start + bh->b_size; 1889afddba49SNick Piggin 1890afddba49SNick Piggin if (buffer_new(bh)) { 1891afddba49SNick Piggin if (block_end > from && block_start < to) { 1892afddba49SNick Piggin if (!PageUptodate(page)) { 1893afddba49SNick Piggin unsigned start, size; 1894afddba49SNick Piggin 1895afddba49SNick Piggin start = max(from, block_start); 1896afddba49SNick Piggin size = min(to, block_end) - start; 1897afddba49SNick Piggin 1898eebd2aa3SChristoph Lameter zero_user(page, start, size); 1899afddba49SNick Piggin set_buffer_uptodate(bh); 1900afddba49SNick Piggin } 1901afddba49SNick Piggin 1902afddba49SNick Piggin clear_buffer_new(bh); 1903afddba49SNick Piggin mark_buffer_dirty(bh); 1904afddba49SNick Piggin } 1905afddba49SNick Piggin } 1906afddba49SNick Piggin 1907afddba49SNick Piggin block_start = block_end; 1908afddba49SNick Piggin bh = bh->b_this_page; 1909afddba49SNick Piggin } while (bh != head); 1910afddba49SNick Piggin } 1911afddba49SNick Piggin EXPORT_SYMBOL(page_zero_new_buffers); 1912afddba49SNick Piggin 1913ae259a9cSChristoph Hellwig static void 1914ae259a9cSChristoph Hellwig iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh, 1915ae259a9cSChristoph Hellwig struct iomap *iomap) 1916ae259a9cSChristoph Hellwig { 1917ae259a9cSChristoph Hellwig loff_t offset = block << inode->i_blkbits; 1918ae259a9cSChristoph Hellwig 1919ae259a9cSChristoph Hellwig bh->b_bdev = iomap->bdev; 1920ae259a9cSChristoph Hellwig 1921ae259a9cSChristoph Hellwig /* 1922ae259a9cSChristoph Hellwig * Block points to offset in file we need to map, iomap contains 1923ae259a9cSChristoph Hellwig * the offset at which the map starts. If the map ends before the 1924ae259a9cSChristoph Hellwig * current block, then do not map the buffer and let the caller 1925ae259a9cSChristoph Hellwig * handle it. 1926ae259a9cSChristoph Hellwig */ 1927ae259a9cSChristoph Hellwig BUG_ON(offset >= iomap->offset + iomap->length); 1928ae259a9cSChristoph Hellwig 1929ae259a9cSChristoph Hellwig switch (iomap->type) { 1930ae259a9cSChristoph Hellwig case IOMAP_HOLE: 1931ae259a9cSChristoph Hellwig /* 1932ae259a9cSChristoph Hellwig * If the buffer is not up to date or beyond the current EOF, 1933ae259a9cSChristoph Hellwig * we need to mark it as new to ensure sub-block zeroing is 1934ae259a9cSChristoph Hellwig * executed if necessary. 1935ae259a9cSChristoph Hellwig */ 1936ae259a9cSChristoph Hellwig if (!buffer_uptodate(bh) || 1937ae259a9cSChristoph Hellwig (offset >= i_size_read(inode))) 1938ae259a9cSChristoph Hellwig set_buffer_new(bh); 1939ae259a9cSChristoph Hellwig break; 1940ae259a9cSChristoph Hellwig case IOMAP_DELALLOC: 1941ae259a9cSChristoph Hellwig if (!buffer_uptodate(bh) || 1942ae259a9cSChristoph Hellwig (offset >= i_size_read(inode))) 1943ae259a9cSChristoph Hellwig set_buffer_new(bh); 1944ae259a9cSChristoph Hellwig set_buffer_uptodate(bh); 1945ae259a9cSChristoph Hellwig set_buffer_mapped(bh); 1946ae259a9cSChristoph Hellwig set_buffer_delay(bh); 1947ae259a9cSChristoph Hellwig break; 1948ae259a9cSChristoph Hellwig case IOMAP_UNWRITTEN: 1949ae259a9cSChristoph Hellwig /* 19503d7b6b21SAndreas Gruenbacher * For unwritten regions, we always need to ensure that regions 19513d7b6b21SAndreas Gruenbacher * in the block we are not writing to are zeroed. Mark the 19523d7b6b21SAndreas Gruenbacher * buffer as new to ensure this. 1953ae259a9cSChristoph Hellwig */ 1954ae259a9cSChristoph Hellwig set_buffer_new(bh); 1955ae259a9cSChristoph Hellwig set_buffer_unwritten(bh); 1956ae259a9cSChristoph Hellwig /* FALLTHRU */ 1957ae259a9cSChristoph Hellwig case IOMAP_MAPPED: 19583d7b6b21SAndreas Gruenbacher if ((iomap->flags & IOMAP_F_NEW) || 19593d7b6b21SAndreas Gruenbacher offset >= i_size_read(inode)) 1960ae259a9cSChristoph Hellwig set_buffer_new(bh); 196119fe5f64SAndreas Gruenbacher bh->b_blocknr = (iomap->addr + offset - iomap->offset) >> 196219fe5f64SAndreas Gruenbacher inode->i_blkbits; 1963ae259a9cSChristoph Hellwig set_buffer_mapped(bh); 1964ae259a9cSChristoph Hellwig break; 1965ae259a9cSChristoph Hellwig } 1966ae259a9cSChristoph Hellwig } 1967ae259a9cSChristoph Hellwig 1968ae259a9cSChristoph Hellwig int __block_write_begin_int(struct page *page, loff_t pos, unsigned len, 1969ae259a9cSChristoph Hellwig get_block_t *get_block, struct iomap *iomap) 19701da177e4SLinus Torvalds { 197109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 1972ebdec241SChristoph Hellwig unsigned to = from + len; 19736e1db88dSChristoph Hellwig struct inode *inode = page->mapping->host; 19741da177e4SLinus Torvalds unsigned block_start, block_end; 19751da177e4SLinus Torvalds sector_t block; 19761da177e4SLinus Torvalds int err = 0; 19771da177e4SLinus Torvalds unsigned blocksize, bbits; 19781da177e4SLinus Torvalds struct buffer_head *bh, *head, *wait[2], **wait_bh=wait; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 198109cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 198209cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 19831da177e4SLinus Torvalds BUG_ON(from > to); 19841da177e4SLinus Torvalds 198545bce8f3SLinus Torvalds head = create_page_buffers(page, inode, 0); 198645bce8f3SLinus Torvalds blocksize = head->b_size; 198745bce8f3SLinus Torvalds bbits = block_size_bits(blocksize); 19881da177e4SLinus Torvalds 198909cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds for(bh = head, block_start = 0; bh != head || !block_start; 19921da177e4SLinus Torvalds block++, block_start=block_end, bh = bh->b_this_page) { 19931da177e4SLinus Torvalds block_end = block_start + blocksize; 19941da177e4SLinus Torvalds if (block_end <= from || block_start >= to) { 19951da177e4SLinus Torvalds if (PageUptodate(page)) { 19961da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 19971da177e4SLinus Torvalds set_buffer_uptodate(bh); 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds continue; 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds if (buffer_new(bh)) 20021da177e4SLinus Torvalds clear_buffer_new(bh); 20031da177e4SLinus Torvalds if (!buffer_mapped(bh)) { 2004b0cf2321SBadari Pulavarty WARN_ON(bh->b_size != blocksize); 2005ae259a9cSChristoph Hellwig if (get_block) { 20061da177e4SLinus Torvalds err = get_block(inode, block, bh, 1); 20071da177e4SLinus Torvalds if (err) 2008f3ddbdc6SNick Piggin break; 2009ae259a9cSChristoph Hellwig } else { 2010ae259a9cSChristoph Hellwig iomap_to_bh(inode, block, bh, iomap); 2011ae259a9cSChristoph Hellwig } 2012ae259a9cSChristoph Hellwig 20131da177e4SLinus Torvalds if (buffer_new(bh)) { 2014e64855c6SJan Kara clean_bdev_bh_alias(bh); 20151da177e4SLinus Torvalds if (PageUptodate(page)) { 2016637aff46SNick Piggin clear_buffer_new(bh); 20171da177e4SLinus Torvalds set_buffer_uptodate(bh); 2018637aff46SNick Piggin mark_buffer_dirty(bh); 20191da177e4SLinus Torvalds continue; 20201da177e4SLinus Torvalds } 2021eebd2aa3SChristoph Lameter if (block_end > to || block_start < from) 2022eebd2aa3SChristoph Lameter zero_user_segments(page, 2023eebd2aa3SChristoph Lameter to, block_end, 2024eebd2aa3SChristoph Lameter block_start, from); 20251da177e4SLinus Torvalds continue; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds if (PageUptodate(page)) { 20291da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 20301da177e4SLinus Torvalds set_buffer_uptodate(bh); 20311da177e4SLinus Torvalds continue; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds if (!buffer_uptodate(bh) && !buffer_delay(bh) && 203433a266ddSDavid Chinner !buffer_unwritten(bh) && 20351da177e4SLinus Torvalds (block_start < from || block_end > to)) { 2036dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 20371da177e4SLinus Torvalds *wait_bh++=bh; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds /* 20411da177e4SLinus Torvalds * If we issued read requests - let them complete. 20421da177e4SLinus Torvalds */ 20431da177e4SLinus Torvalds while(wait_bh > wait) { 20441da177e4SLinus Torvalds wait_on_buffer(*--wait_bh); 20451da177e4SLinus Torvalds if (!buffer_uptodate(*wait_bh)) 2046f3ddbdc6SNick Piggin err = -EIO; 20471da177e4SLinus Torvalds } 2048f9f07b6cSJan Kara if (unlikely(err)) 2049afddba49SNick Piggin page_zero_new_buffers(page, from, to); 20501da177e4SLinus Torvalds return err; 20511da177e4SLinus Torvalds } 2052ae259a9cSChristoph Hellwig 2053ae259a9cSChristoph Hellwig int __block_write_begin(struct page *page, loff_t pos, unsigned len, 2054ae259a9cSChristoph Hellwig get_block_t *get_block) 2055ae259a9cSChristoph Hellwig { 2056ae259a9cSChristoph Hellwig return __block_write_begin_int(page, pos, len, get_block, NULL); 2057ae259a9cSChristoph Hellwig } 2058ebdec241SChristoph Hellwig EXPORT_SYMBOL(__block_write_begin); 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds static int __block_commit_write(struct inode *inode, struct page *page, 20611da177e4SLinus Torvalds unsigned from, unsigned to) 20621da177e4SLinus Torvalds { 20631da177e4SLinus Torvalds unsigned block_start, block_end; 20641da177e4SLinus Torvalds int partial = 0; 20651da177e4SLinus Torvalds unsigned blocksize; 20661da177e4SLinus Torvalds struct buffer_head *bh, *head; 20671da177e4SLinus Torvalds 206845bce8f3SLinus Torvalds bh = head = page_buffers(page); 206945bce8f3SLinus Torvalds blocksize = bh->b_size; 20701da177e4SLinus Torvalds 207145bce8f3SLinus Torvalds block_start = 0; 207245bce8f3SLinus Torvalds do { 20731da177e4SLinus Torvalds block_end = block_start + blocksize; 20741da177e4SLinus Torvalds if (block_end <= from || block_start >= to) { 20751da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 20761da177e4SLinus Torvalds partial = 1; 20771da177e4SLinus Torvalds } else { 20781da177e4SLinus Torvalds set_buffer_uptodate(bh); 20791da177e4SLinus Torvalds mark_buffer_dirty(bh); 20801da177e4SLinus Torvalds } 2081afddba49SNick Piggin clear_buffer_new(bh); 208245bce8f3SLinus Torvalds 208345bce8f3SLinus Torvalds block_start = block_end; 208445bce8f3SLinus Torvalds bh = bh->b_this_page; 208545bce8f3SLinus Torvalds } while (bh != head); 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds /* 20881da177e4SLinus Torvalds * If this is a partial write which happened to make all buffers 20891da177e4SLinus Torvalds * uptodate then we can optimize away a bogus readpage() for 20901da177e4SLinus Torvalds * the next read(). Here we 'discover' whether the page went 20911da177e4SLinus Torvalds * uptodate as a result of this (potentially partial) write. 20921da177e4SLinus Torvalds */ 20931da177e4SLinus Torvalds if (!partial) 20941da177e4SLinus Torvalds SetPageUptodate(page); 20951da177e4SLinus Torvalds return 0; 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /* 2099155130a4SChristoph Hellwig * block_write_begin takes care of the basic task of block allocation and 2100155130a4SChristoph Hellwig * bringing partial write blocks uptodate first. 2101155130a4SChristoph Hellwig * 21027bb46a67Snpiggin@suse.de * The filesystem needs to handle block truncation upon failure. 2103afddba49SNick Piggin */ 2104155130a4SChristoph Hellwig int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len, 2105155130a4SChristoph Hellwig unsigned flags, struct page **pagep, get_block_t *get_block) 2106afddba49SNick Piggin { 210709cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 2108afddba49SNick Piggin struct page *page; 21096e1db88dSChristoph Hellwig int status; 2110afddba49SNick Piggin 211154566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 21126e1db88dSChristoph Hellwig if (!page) 21136e1db88dSChristoph Hellwig return -ENOMEM; 2114afddba49SNick Piggin 21156e1db88dSChristoph Hellwig status = __block_write_begin(page, pos, len, get_block); 2116afddba49SNick Piggin if (unlikely(status)) { 2117afddba49SNick Piggin unlock_page(page); 211809cbfeafSKirill A. Shutemov put_page(page); 21196e1db88dSChristoph Hellwig page = NULL; 2120afddba49SNick Piggin } 2121afddba49SNick Piggin 21226e1db88dSChristoph Hellwig *pagep = page; 2123afddba49SNick Piggin return status; 2124afddba49SNick Piggin } 2125afddba49SNick Piggin EXPORT_SYMBOL(block_write_begin); 2126afddba49SNick Piggin 2127afddba49SNick Piggin int block_write_end(struct file *file, struct address_space *mapping, 2128afddba49SNick Piggin loff_t pos, unsigned len, unsigned copied, 2129afddba49SNick Piggin struct page *page, void *fsdata) 2130afddba49SNick Piggin { 2131afddba49SNick Piggin struct inode *inode = mapping->host; 2132afddba49SNick Piggin unsigned start; 2133afddba49SNick Piggin 213409cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 2135afddba49SNick Piggin 2136afddba49SNick Piggin if (unlikely(copied < len)) { 2137afddba49SNick Piggin /* 2138afddba49SNick Piggin * The buffers that were written will now be uptodate, so we 2139afddba49SNick Piggin * don't have to worry about a readpage reading them and 2140afddba49SNick Piggin * overwriting a partial write. However if we have encountered 2141afddba49SNick Piggin * a short write and only partially written into a buffer, it 2142afddba49SNick Piggin * will not be marked uptodate, so a readpage might come in and 2143afddba49SNick Piggin * destroy our partial write. 2144afddba49SNick Piggin * 2145afddba49SNick Piggin * Do the simplest thing, and just treat any short write to a 2146afddba49SNick Piggin * non uptodate page as a zero-length write, and force the 2147afddba49SNick Piggin * caller to redo the whole thing. 2148afddba49SNick Piggin */ 2149afddba49SNick Piggin if (!PageUptodate(page)) 2150afddba49SNick Piggin copied = 0; 2151afddba49SNick Piggin 2152afddba49SNick Piggin page_zero_new_buffers(page, start+copied, start+len); 2153afddba49SNick Piggin } 2154afddba49SNick Piggin flush_dcache_page(page); 2155afddba49SNick Piggin 2156afddba49SNick Piggin /* This could be a short (even 0-length) commit */ 2157afddba49SNick Piggin __block_commit_write(inode, page, start, start+copied); 2158afddba49SNick Piggin 2159afddba49SNick Piggin return copied; 2160afddba49SNick Piggin } 2161afddba49SNick Piggin EXPORT_SYMBOL(block_write_end); 2162afddba49SNick Piggin 2163afddba49SNick Piggin int generic_write_end(struct file *file, struct address_space *mapping, 2164afddba49SNick Piggin loff_t pos, unsigned len, unsigned copied, 2165afddba49SNick Piggin struct page *page, void *fsdata) 2166afddba49SNick Piggin { 21678af54f29SChristoph Hellwig struct inode *inode = mapping->host; 21688af54f29SChristoph Hellwig loff_t old_size = inode->i_size; 21698af54f29SChristoph Hellwig bool i_size_changed = false; 21708af54f29SChristoph Hellwig 2171afddba49SNick Piggin copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 21728af54f29SChristoph Hellwig 21738af54f29SChristoph Hellwig /* 21748af54f29SChristoph Hellwig * No need to use i_size_read() here, the i_size cannot change under us 21758af54f29SChristoph Hellwig * because we hold i_rwsem. 21768af54f29SChristoph Hellwig * 21778af54f29SChristoph Hellwig * But it's important to update i_size while still holding page lock: 21788af54f29SChristoph Hellwig * page writeout could otherwise come in and zero beyond i_size. 21798af54f29SChristoph Hellwig */ 21808af54f29SChristoph Hellwig if (pos + copied > inode->i_size) { 21818af54f29SChristoph Hellwig i_size_write(inode, pos + copied); 21828af54f29SChristoph Hellwig i_size_changed = true; 21838af54f29SChristoph Hellwig } 21848af54f29SChristoph Hellwig 21858af54f29SChristoph Hellwig unlock_page(page); 21867a77dad7SAndreas Gruenbacher put_page(page); 21878af54f29SChristoph Hellwig 21888af54f29SChristoph Hellwig if (old_size < pos) 21898af54f29SChristoph Hellwig pagecache_isize_extended(inode, old_size, pos); 21908af54f29SChristoph Hellwig /* 21918af54f29SChristoph Hellwig * Don't mark the inode dirty under page lock. First, it unnecessarily 21928af54f29SChristoph Hellwig * makes the holding time of page lock longer. Second, it forces lock 21938af54f29SChristoph Hellwig * ordering of page lock and transaction start for journaling 21948af54f29SChristoph Hellwig * filesystems. 21958af54f29SChristoph Hellwig */ 21968af54f29SChristoph Hellwig if (i_size_changed) 21978af54f29SChristoph Hellwig mark_inode_dirty(inode); 219826ddb1f4SAndreas Gruenbacher return copied; 2199afddba49SNick Piggin } 2200afddba49SNick Piggin EXPORT_SYMBOL(generic_write_end); 2201afddba49SNick Piggin 2202afddba49SNick Piggin /* 22038ab22b9aSHisashi Hifumi * block_is_partially_uptodate checks whether buffers within a page are 22048ab22b9aSHisashi Hifumi * uptodate or not. 22058ab22b9aSHisashi Hifumi * 22068ab22b9aSHisashi Hifumi * Returns true if all buffers which correspond to a file portion 22078ab22b9aSHisashi Hifumi * we want to read are uptodate. 22088ab22b9aSHisashi Hifumi */ 2209c186afb4SAl Viro int block_is_partially_uptodate(struct page *page, unsigned long from, 2210c186afb4SAl Viro unsigned long count) 22118ab22b9aSHisashi Hifumi { 22128ab22b9aSHisashi Hifumi unsigned block_start, block_end, blocksize; 22138ab22b9aSHisashi Hifumi unsigned to; 22148ab22b9aSHisashi Hifumi struct buffer_head *bh, *head; 22158ab22b9aSHisashi Hifumi int ret = 1; 22168ab22b9aSHisashi Hifumi 22178ab22b9aSHisashi Hifumi if (!page_has_buffers(page)) 22188ab22b9aSHisashi Hifumi return 0; 22198ab22b9aSHisashi Hifumi 222045bce8f3SLinus Torvalds head = page_buffers(page); 222145bce8f3SLinus Torvalds blocksize = head->b_size; 222209cbfeafSKirill A. Shutemov to = min_t(unsigned, PAGE_SIZE - from, count); 22238ab22b9aSHisashi Hifumi to = from + to; 222409cbfeafSKirill A. Shutemov if (from < blocksize && to > PAGE_SIZE - blocksize) 22258ab22b9aSHisashi Hifumi return 0; 22268ab22b9aSHisashi Hifumi 22278ab22b9aSHisashi Hifumi bh = head; 22288ab22b9aSHisashi Hifumi block_start = 0; 22298ab22b9aSHisashi Hifumi do { 22308ab22b9aSHisashi Hifumi block_end = block_start + blocksize; 22318ab22b9aSHisashi Hifumi if (block_end > from && block_start < to) { 22328ab22b9aSHisashi Hifumi if (!buffer_uptodate(bh)) { 22338ab22b9aSHisashi Hifumi ret = 0; 22348ab22b9aSHisashi Hifumi break; 22358ab22b9aSHisashi Hifumi } 22368ab22b9aSHisashi Hifumi if (block_end >= to) 22378ab22b9aSHisashi Hifumi break; 22388ab22b9aSHisashi Hifumi } 22398ab22b9aSHisashi Hifumi block_start = block_end; 22408ab22b9aSHisashi Hifumi bh = bh->b_this_page; 22418ab22b9aSHisashi Hifumi } while (bh != head); 22428ab22b9aSHisashi Hifumi 22438ab22b9aSHisashi Hifumi return ret; 22448ab22b9aSHisashi Hifumi } 22458ab22b9aSHisashi Hifumi EXPORT_SYMBOL(block_is_partially_uptodate); 22468ab22b9aSHisashi Hifumi 22478ab22b9aSHisashi Hifumi /* 22481da177e4SLinus Torvalds * Generic "read page" function for block devices that have the normal 22491da177e4SLinus Torvalds * get_block functionality. This is most of the block device filesystems. 22501da177e4SLinus Torvalds * Reads the page asynchronously --- the unlock_buffer() and 22511da177e4SLinus Torvalds * set/clear_buffer_uptodate() functions propagate buffer state into the 22521da177e4SLinus Torvalds * page struct once IO has completed. 22531da177e4SLinus Torvalds */ 22541da177e4SLinus Torvalds int block_read_full_page(struct page *page, get_block_t *get_block) 22551da177e4SLinus Torvalds { 22561da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 22571da177e4SLinus Torvalds sector_t iblock, lblock; 22581da177e4SLinus Torvalds struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; 225945bce8f3SLinus Torvalds unsigned int blocksize, bbits; 22601da177e4SLinus Torvalds int nr, i; 22611da177e4SLinus Torvalds int fully_mapped = 1; 22621da177e4SLinus Torvalds 226345bce8f3SLinus Torvalds head = create_page_buffers(page, inode, 0); 226445bce8f3SLinus Torvalds blocksize = head->b_size; 226545bce8f3SLinus Torvalds bbits = block_size_bits(blocksize); 22661da177e4SLinus Torvalds 226709cbfeafSKirill A. Shutemov iblock = (sector_t)page->index << (PAGE_SHIFT - bbits); 226845bce8f3SLinus Torvalds lblock = (i_size_read(inode)+blocksize-1) >> bbits; 22691da177e4SLinus Torvalds bh = head; 22701da177e4SLinus Torvalds nr = 0; 22711da177e4SLinus Torvalds i = 0; 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds do { 22741da177e4SLinus Torvalds if (buffer_uptodate(bh)) 22751da177e4SLinus Torvalds continue; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds if (!buffer_mapped(bh)) { 2278c64610baSAndrew Morton int err = 0; 2279c64610baSAndrew Morton 22801da177e4SLinus Torvalds fully_mapped = 0; 22811da177e4SLinus Torvalds if (iblock < lblock) { 2282b0cf2321SBadari Pulavarty WARN_ON(bh->b_size != blocksize); 2283c64610baSAndrew Morton err = get_block(inode, iblock, bh, 0); 2284c64610baSAndrew Morton if (err) 22851da177e4SLinus Torvalds SetPageError(page); 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds if (!buffer_mapped(bh)) { 2288eebd2aa3SChristoph Lameter zero_user(page, i * blocksize, blocksize); 2289c64610baSAndrew Morton if (!err) 22901da177e4SLinus Torvalds set_buffer_uptodate(bh); 22911da177e4SLinus Torvalds continue; 22921da177e4SLinus Torvalds } 22931da177e4SLinus Torvalds /* 22941da177e4SLinus Torvalds * get_block() might have updated the buffer 22951da177e4SLinus Torvalds * synchronously 22961da177e4SLinus Torvalds */ 22971da177e4SLinus Torvalds if (buffer_uptodate(bh)) 22981da177e4SLinus Torvalds continue; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds arr[nr++] = bh; 23011da177e4SLinus Torvalds } while (i++, iblock++, (bh = bh->b_this_page) != head); 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds if (fully_mapped) 23041da177e4SLinus Torvalds SetPageMappedToDisk(page); 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds if (!nr) { 23071da177e4SLinus Torvalds /* 23081da177e4SLinus Torvalds * All buffers are uptodate - we can set the page uptodate 23091da177e4SLinus Torvalds * as well. But not if get_block() returned an error. 23101da177e4SLinus Torvalds */ 23111da177e4SLinus Torvalds if (!PageError(page)) 23121da177e4SLinus Torvalds SetPageUptodate(page); 23131da177e4SLinus Torvalds unlock_page(page); 23141da177e4SLinus Torvalds return 0; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* Stage two: lock the buffers */ 23181da177e4SLinus Torvalds for (i = 0; i < nr; i++) { 23191da177e4SLinus Torvalds bh = arr[i]; 23201da177e4SLinus Torvalds lock_buffer(bh); 23211da177e4SLinus Torvalds mark_buffer_async_read(bh); 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds /* 23251da177e4SLinus Torvalds * Stage 3: start the IO. Check for uptodateness 23261da177e4SLinus Torvalds * inside the buffer lock in case another process reading 23271da177e4SLinus Torvalds * the underlying blockdev brought it uptodate (the sct fix). 23281da177e4SLinus Torvalds */ 23291da177e4SLinus Torvalds for (i = 0; i < nr; i++) { 23301da177e4SLinus Torvalds bh = arr[i]; 23311da177e4SLinus Torvalds if (buffer_uptodate(bh)) 23321da177e4SLinus Torvalds end_buffer_async_read(bh, 1); 23331da177e4SLinus Torvalds else 23342a222ca9SMike Christie submit_bh(REQ_OP_READ, 0, bh); 23351da177e4SLinus Torvalds } 23361da177e4SLinus Torvalds return 0; 23371da177e4SLinus Torvalds } 23381fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_read_full_page); 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds /* utility function for filesystems that need to do work on expanding 234189e10787SNick Piggin * truncates. Uses filesystem pagecache writes to allow the filesystem to 23421da177e4SLinus Torvalds * deal with the hole. 23431da177e4SLinus Torvalds */ 234489e10787SNick Piggin int generic_cont_expand_simple(struct inode *inode, loff_t size) 23451da177e4SLinus Torvalds { 23461da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 23471da177e4SLinus Torvalds struct page *page; 234889e10787SNick Piggin void *fsdata; 23491da177e4SLinus Torvalds int err; 23501da177e4SLinus Torvalds 2351c08d3b0eSnpiggin@suse.de err = inode_newsize_ok(inode, size); 2352c08d3b0eSnpiggin@suse.de if (err) 23531da177e4SLinus Torvalds goto out; 23541da177e4SLinus Torvalds 235589e10787SNick Piggin err = pagecache_write_begin(NULL, mapping, size, 0, 2356c718a975STetsuo Handa AOP_FLAG_CONT_EXPAND, &page, &fsdata); 235789e10787SNick Piggin if (err) 235805eb0b51SOGAWA Hirofumi goto out; 235905eb0b51SOGAWA Hirofumi 236089e10787SNick Piggin err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata); 236189e10787SNick Piggin BUG_ON(err > 0); 236205eb0b51SOGAWA Hirofumi 236305eb0b51SOGAWA Hirofumi out: 236405eb0b51SOGAWA Hirofumi return err; 236505eb0b51SOGAWA Hirofumi } 23661fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(generic_cont_expand_simple); 236705eb0b51SOGAWA Hirofumi 2368f1e3af72SAdrian Bunk static int cont_expand_zero(struct file *file, struct address_space *mapping, 236989e10787SNick Piggin loff_t pos, loff_t *bytes) 237005eb0b51SOGAWA Hirofumi { 237189e10787SNick Piggin struct inode *inode = mapping->host; 237293407472SFabian Frederick unsigned int blocksize = i_blocksize(inode); 237389e10787SNick Piggin struct page *page; 237489e10787SNick Piggin void *fsdata; 237589e10787SNick Piggin pgoff_t index, curidx; 237689e10787SNick Piggin loff_t curpos; 237789e10787SNick Piggin unsigned zerofrom, offset, len; 237889e10787SNick Piggin int err = 0; 237905eb0b51SOGAWA Hirofumi 238009cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 238109cbfeafSKirill A. Shutemov offset = pos & ~PAGE_MASK; 238289e10787SNick Piggin 238309cbfeafSKirill A. Shutemov while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) { 238409cbfeafSKirill A. Shutemov zerofrom = curpos & ~PAGE_MASK; 238589e10787SNick Piggin if (zerofrom & (blocksize-1)) { 238689e10787SNick Piggin *bytes |= (blocksize-1); 238789e10787SNick Piggin (*bytes)++; 238889e10787SNick Piggin } 238909cbfeafSKirill A. Shutemov len = PAGE_SIZE - zerofrom; 239089e10787SNick Piggin 2391c718a975STetsuo Handa err = pagecache_write_begin(file, mapping, curpos, len, 0, 239289e10787SNick Piggin &page, &fsdata); 239389e10787SNick Piggin if (err) 239489e10787SNick Piggin goto out; 2395eebd2aa3SChristoph Lameter zero_user(page, zerofrom, len); 239689e10787SNick Piggin err = pagecache_write_end(file, mapping, curpos, len, len, 239789e10787SNick Piggin page, fsdata); 239889e10787SNick Piggin if (err < 0) 239989e10787SNick Piggin goto out; 240089e10787SNick Piggin BUG_ON(err != len); 240189e10787SNick Piggin err = 0; 2402061e9746SOGAWA Hirofumi 2403061e9746SOGAWA Hirofumi balance_dirty_pages_ratelimited(mapping); 2404c2ca0fcdSMikulas Patocka 240508d405c8SDavidlohr Bueso if (fatal_signal_pending(current)) { 2406c2ca0fcdSMikulas Patocka err = -EINTR; 2407c2ca0fcdSMikulas Patocka goto out; 2408c2ca0fcdSMikulas Patocka } 240989e10787SNick Piggin } 241089e10787SNick Piggin 241189e10787SNick Piggin /* page covers the boundary, find the boundary offset */ 241289e10787SNick Piggin if (index == curidx) { 241309cbfeafSKirill A. Shutemov zerofrom = curpos & ~PAGE_MASK; 241489e10787SNick Piggin /* if we will expand the thing last block will be filled */ 241589e10787SNick Piggin if (offset <= zerofrom) { 241689e10787SNick Piggin goto out; 241789e10787SNick Piggin } 241889e10787SNick Piggin if (zerofrom & (blocksize-1)) { 241989e10787SNick Piggin *bytes |= (blocksize-1); 242089e10787SNick Piggin (*bytes)++; 242189e10787SNick Piggin } 242289e10787SNick Piggin len = offset - zerofrom; 242389e10787SNick Piggin 2424c718a975STetsuo Handa err = pagecache_write_begin(file, mapping, curpos, len, 0, 242589e10787SNick Piggin &page, &fsdata); 242689e10787SNick Piggin if (err) 242789e10787SNick Piggin goto out; 2428eebd2aa3SChristoph Lameter zero_user(page, zerofrom, len); 242989e10787SNick Piggin err = pagecache_write_end(file, mapping, curpos, len, len, 243089e10787SNick Piggin page, fsdata); 243189e10787SNick Piggin if (err < 0) 243289e10787SNick Piggin goto out; 243389e10787SNick Piggin BUG_ON(err != len); 243489e10787SNick Piggin err = 0; 243589e10787SNick Piggin } 243689e10787SNick Piggin out: 243789e10787SNick Piggin return err; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds /* 24411da177e4SLinus Torvalds * For moronic filesystems that do not allow holes in file. 24421da177e4SLinus Torvalds * We may have to extend the file. 24431da177e4SLinus Torvalds */ 2444282dc178SChristoph Hellwig int cont_write_begin(struct file *file, struct address_space *mapping, 244589e10787SNick Piggin loff_t pos, unsigned len, unsigned flags, 244689e10787SNick Piggin struct page **pagep, void **fsdata, 244789e10787SNick Piggin get_block_t *get_block, loff_t *bytes) 24481da177e4SLinus Torvalds { 24491da177e4SLinus Torvalds struct inode *inode = mapping->host; 245093407472SFabian Frederick unsigned int blocksize = i_blocksize(inode); 245193407472SFabian Frederick unsigned int zerofrom; 245289e10787SNick Piggin int err; 24531da177e4SLinus Torvalds 245489e10787SNick Piggin err = cont_expand_zero(file, mapping, pos, bytes); 245589e10787SNick Piggin if (err) 2456155130a4SChristoph Hellwig return err; 24571da177e4SLinus Torvalds 245809cbfeafSKirill A. Shutemov zerofrom = *bytes & ~PAGE_MASK; 245989e10787SNick Piggin if (pos+len > *bytes && zerofrom & (blocksize-1)) { 24601da177e4SLinus Torvalds *bytes |= (blocksize-1); 24611da177e4SLinus Torvalds (*bytes)++; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 2464155130a4SChristoph Hellwig return block_write_begin(mapping, pos, len, flags, pagep, get_block); 24651da177e4SLinus Torvalds } 24661fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(cont_write_begin); 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds int block_commit_write(struct page *page, unsigned from, unsigned to) 24691da177e4SLinus Torvalds { 24701da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 24711da177e4SLinus Torvalds __block_commit_write(inode,page,from,to); 24721da177e4SLinus Torvalds return 0; 24731da177e4SLinus Torvalds } 24741fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_commit_write); 24751da177e4SLinus Torvalds 247654171690SDavid Chinner /* 247754171690SDavid Chinner * block_page_mkwrite() is not allowed to change the file size as it gets 247854171690SDavid Chinner * called from a page fault handler when a page is first dirtied. Hence we must 247954171690SDavid Chinner * be careful to check for EOF conditions here. We set the page up correctly 248054171690SDavid Chinner * for a written page which means we get ENOSPC checking when writing into 248154171690SDavid Chinner * holes and correct delalloc and unwritten extent mapping on filesystems that 248254171690SDavid Chinner * support these features. 248354171690SDavid Chinner * 248454171690SDavid Chinner * We are not allowed to take the i_mutex here so we have to play games to 248554171690SDavid Chinner * protect against truncate races as the page could now be beyond EOF. Because 24867bb46a67Snpiggin@suse.de * truncate writes the inode size before removing pages, once we have the 248754171690SDavid Chinner * page lock we can determine safely if the page is beyond EOF. If it is not 248854171690SDavid Chinner * beyond EOF, then the page is guaranteed safe against truncation until we 248954171690SDavid Chinner * unlock the page. 2490ea13a864SJan Kara * 249114da9200SJan Kara * Direct callers of this function should protect against filesystem freezing 24925c500029SRoss Zwisler * using sb_start_pagefault() - sb_end_pagefault() functions. 249354171690SDavid Chinner */ 24945c500029SRoss Zwisler int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf, 249554171690SDavid Chinner get_block_t get_block) 249654171690SDavid Chinner { 2497c2ec175cSNick Piggin struct page *page = vmf->page; 2498496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 249954171690SDavid Chinner unsigned long end; 250054171690SDavid Chinner loff_t size; 250124da4fabSJan Kara int ret; 250254171690SDavid Chinner 250354171690SDavid Chinner lock_page(page); 250454171690SDavid Chinner size = i_size_read(inode); 250554171690SDavid Chinner if ((page->mapping != inode->i_mapping) || 250618336338SNick Piggin (page_offset(page) > size)) { 250724da4fabSJan Kara /* We overload EFAULT to mean page got truncated */ 250824da4fabSJan Kara ret = -EFAULT; 250924da4fabSJan Kara goto out_unlock; 251054171690SDavid Chinner } 251154171690SDavid Chinner 251254171690SDavid Chinner /* page is wholly or partially inside EOF */ 251309cbfeafSKirill A. Shutemov if (((page->index + 1) << PAGE_SHIFT) > size) 251409cbfeafSKirill A. Shutemov end = size & ~PAGE_MASK; 251554171690SDavid Chinner else 251609cbfeafSKirill A. Shutemov end = PAGE_SIZE; 251754171690SDavid Chinner 2518ebdec241SChristoph Hellwig ret = __block_write_begin(page, 0, end, get_block); 251954171690SDavid Chinner if (!ret) 252054171690SDavid Chinner ret = block_commit_write(page, 0, end); 252154171690SDavid Chinner 252224da4fabSJan Kara if (unlikely(ret < 0)) 252324da4fabSJan Kara goto out_unlock; 2524ea13a864SJan Kara set_page_dirty(page); 25251d1d1a76SDarrick J. Wong wait_for_stable_page(page); 252624da4fabSJan Kara return 0; 252724da4fabSJan Kara out_unlock: 2528b827e496SNick Piggin unlock_page(page); 252954171690SDavid Chinner return ret; 253054171690SDavid Chinner } 25311fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_page_mkwrite); 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds /* 253403158cd7SNick Piggin * nobh_write_begin()'s prereads are special: the buffer_heads are freed 25351da177e4SLinus Torvalds * immediately, while under the page lock. So it needs a special end_io 25361da177e4SLinus Torvalds * handler which does not touch the bh after unlocking it. 25371da177e4SLinus Torvalds */ 25381da177e4SLinus Torvalds static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate) 25391da177e4SLinus Torvalds { 254068671f35SDmitry Monakhov __end_buffer_read_notouch(bh, uptodate); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds /* 254403158cd7SNick Piggin * Attach the singly-linked list of buffers created by nobh_write_begin, to 254503158cd7SNick Piggin * the page (converting it to circular linked list and taking care of page 254603158cd7SNick Piggin * dirty races). 254703158cd7SNick Piggin */ 254803158cd7SNick Piggin static void attach_nobh_buffers(struct page *page, struct buffer_head *head) 254903158cd7SNick Piggin { 255003158cd7SNick Piggin struct buffer_head *bh; 255103158cd7SNick Piggin 255203158cd7SNick Piggin BUG_ON(!PageLocked(page)); 255303158cd7SNick Piggin 255403158cd7SNick Piggin spin_lock(&page->mapping->private_lock); 255503158cd7SNick Piggin bh = head; 255603158cd7SNick Piggin do { 255703158cd7SNick Piggin if (PageDirty(page)) 255803158cd7SNick Piggin set_buffer_dirty(bh); 255903158cd7SNick Piggin if (!bh->b_this_page) 256003158cd7SNick Piggin bh->b_this_page = head; 256103158cd7SNick Piggin bh = bh->b_this_page; 256203158cd7SNick Piggin } while (bh != head); 256303158cd7SNick Piggin attach_page_buffers(page, head); 256403158cd7SNick Piggin spin_unlock(&page->mapping->private_lock); 256503158cd7SNick Piggin } 256603158cd7SNick Piggin 256703158cd7SNick Piggin /* 2568ea0f04e5SChristoph Hellwig * On entry, the page is fully not uptodate. 2569ea0f04e5SChristoph Hellwig * On exit the page is fully uptodate in the areas outside (from,to) 25707bb46a67Snpiggin@suse.de * The filesystem needs to handle block truncation upon failure. 25711da177e4SLinus Torvalds */ 2572ea0f04e5SChristoph Hellwig int nobh_write_begin(struct address_space *mapping, 257303158cd7SNick Piggin loff_t pos, unsigned len, unsigned flags, 257403158cd7SNick Piggin struct page **pagep, void **fsdata, 25751da177e4SLinus Torvalds get_block_t *get_block) 25761da177e4SLinus Torvalds { 257703158cd7SNick Piggin struct inode *inode = mapping->host; 25781da177e4SLinus Torvalds const unsigned blkbits = inode->i_blkbits; 25791da177e4SLinus Torvalds const unsigned blocksize = 1 << blkbits; 2580a4b0672dSNick Piggin struct buffer_head *head, *bh; 258103158cd7SNick Piggin struct page *page; 258203158cd7SNick Piggin pgoff_t index; 258303158cd7SNick Piggin unsigned from, to; 25841da177e4SLinus Torvalds unsigned block_in_page; 2585a4b0672dSNick Piggin unsigned block_start, block_end; 25861da177e4SLinus Torvalds sector_t block_in_file; 25871da177e4SLinus Torvalds int nr_reads = 0; 25881da177e4SLinus Torvalds int ret = 0; 25891da177e4SLinus Torvalds int is_mapped_to_disk = 1; 25901da177e4SLinus Torvalds 259109cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 259209cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 259303158cd7SNick Piggin to = from + len; 259403158cd7SNick Piggin 259554566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 259603158cd7SNick Piggin if (!page) 259703158cd7SNick Piggin return -ENOMEM; 259803158cd7SNick Piggin *pagep = page; 259903158cd7SNick Piggin *fsdata = NULL; 260003158cd7SNick Piggin 260103158cd7SNick Piggin if (page_has_buffers(page)) { 2602309f77adSNamhyung Kim ret = __block_write_begin(page, pos, len, get_block); 2603309f77adSNamhyung Kim if (unlikely(ret)) 2604309f77adSNamhyung Kim goto out_release; 2605309f77adSNamhyung Kim return ret; 260603158cd7SNick Piggin } 2607a4b0672dSNick Piggin 26081da177e4SLinus Torvalds if (PageMappedToDisk(page)) 26091da177e4SLinus Torvalds return 0; 26101da177e4SLinus Torvalds 2611a4b0672dSNick Piggin /* 2612a4b0672dSNick Piggin * Allocate buffers so that we can keep track of state, and potentially 2613a4b0672dSNick Piggin * attach them to the page if an error occurs. In the common case of 2614a4b0672dSNick Piggin * no error, they will just be freed again without ever being attached 2615a4b0672dSNick Piggin * to the page (which is all OK, because we're under the page lock). 2616a4b0672dSNick Piggin * 2617a4b0672dSNick Piggin * Be careful: the buffer linked list is a NULL terminated one, rather 2618a4b0672dSNick Piggin * than the circular one we're used to. 2619a4b0672dSNick Piggin */ 2620640ab98fSJens Axboe head = alloc_page_buffers(page, blocksize, false); 262103158cd7SNick Piggin if (!head) { 262203158cd7SNick Piggin ret = -ENOMEM; 262303158cd7SNick Piggin goto out_release; 262403158cd7SNick Piggin } 2625a4b0672dSNick Piggin 262609cbfeafSKirill A. Shutemov block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds /* 26291da177e4SLinus Torvalds * We loop across all blocks in the page, whether or not they are 26301da177e4SLinus Torvalds * part of the affected region. This is so we can discover if the 26311da177e4SLinus Torvalds * page is fully mapped-to-disk. 26321da177e4SLinus Torvalds */ 2633a4b0672dSNick Piggin for (block_start = 0, block_in_page = 0, bh = head; 263409cbfeafSKirill A. Shutemov block_start < PAGE_SIZE; 2635a4b0672dSNick Piggin block_in_page++, block_start += blocksize, bh = bh->b_this_page) { 26361da177e4SLinus Torvalds int create; 26371da177e4SLinus Torvalds 2638a4b0672dSNick Piggin block_end = block_start + blocksize; 2639a4b0672dSNick Piggin bh->b_state = 0; 26401da177e4SLinus Torvalds create = 1; 26411da177e4SLinus Torvalds if (block_start >= to) 26421da177e4SLinus Torvalds create = 0; 26431da177e4SLinus Torvalds ret = get_block(inode, block_in_file + block_in_page, 2644a4b0672dSNick Piggin bh, create); 26451da177e4SLinus Torvalds if (ret) 26461da177e4SLinus Torvalds goto failed; 2647a4b0672dSNick Piggin if (!buffer_mapped(bh)) 26481da177e4SLinus Torvalds is_mapped_to_disk = 0; 2649a4b0672dSNick Piggin if (buffer_new(bh)) 2650e64855c6SJan Kara clean_bdev_bh_alias(bh); 2651a4b0672dSNick Piggin if (PageUptodate(page)) { 2652a4b0672dSNick Piggin set_buffer_uptodate(bh); 26531da177e4SLinus Torvalds continue; 2654a4b0672dSNick Piggin } 2655a4b0672dSNick Piggin if (buffer_new(bh) || !buffer_mapped(bh)) { 2656eebd2aa3SChristoph Lameter zero_user_segments(page, block_start, from, 2657eebd2aa3SChristoph Lameter to, block_end); 26581da177e4SLinus Torvalds continue; 26591da177e4SLinus Torvalds } 2660a4b0672dSNick Piggin if (buffer_uptodate(bh)) 26611da177e4SLinus Torvalds continue; /* reiserfs does this */ 26621da177e4SLinus Torvalds if (block_start < from || block_end > to) { 2663a4b0672dSNick Piggin lock_buffer(bh); 2664a4b0672dSNick Piggin bh->b_end_io = end_buffer_read_nobh; 26652a222ca9SMike Christie submit_bh(REQ_OP_READ, 0, bh); 2666a4b0672dSNick Piggin nr_reads++; 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds if (nr_reads) { 26711da177e4SLinus Torvalds /* 26721da177e4SLinus Torvalds * The page is locked, so these buffers are protected from 26731da177e4SLinus Torvalds * any VM or truncate activity. Hence we don't need to care 26741da177e4SLinus Torvalds * for the buffer_head refcounts. 26751da177e4SLinus Torvalds */ 2676a4b0672dSNick Piggin for (bh = head; bh; bh = bh->b_this_page) { 26771da177e4SLinus Torvalds wait_on_buffer(bh); 26781da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 26791da177e4SLinus Torvalds ret = -EIO; 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds if (ret) 26821da177e4SLinus Torvalds goto failed; 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds if (is_mapped_to_disk) 26861da177e4SLinus Torvalds SetPageMappedToDisk(page); 26871da177e4SLinus Torvalds 268803158cd7SNick Piggin *fsdata = head; /* to be released by nobh_write_end */ 2689a4b0672dSNick Piggin 26901da177e4SLinus Torvalds return 0; 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds failed: 269303158cd7SNick Piggin BUG_ON(!ret); 26941da177e4SLinus Torvalds /* 2695a4b0672dSNick Piggin * Error recovery is a bit difficult. We need to zero out blocks that 2696a4b0672dSNick Piggin * were newly allocated, and dirty them to ensure they get written out. 2697a4b0672dSNick Piggin * Buffers need to be attached to the page at this point, otherwise 2698a4b0672dSNick Piggin * the handling of potential IO errors during writeout would be hard 2699a4b0672dSNick Piggin * (could try doing synchronous writeout, but what if that fails too?) 27001da177e4SLinus Torvalds */ 270103158cd7SNick Piggin attach_nobh_buffers(page, head); 270203158cd7SNick Piggin page_zero_new_buffers(page, from, to); 2703a4b0672dSNick Piggin 270403158cd7SNick Piggin out_release: 270503158cd7SNick Piggin unlock_page(page); 270609cbfeafSKirill A. Shutemov put_page(page); 270703158cd7SNick Piggin *pagep = NULL; 2708a4b0672dSNick Piggin 27097bb46a67Snpiggin@suse.de return ret; 27107bb46a67Snpiggin@suse.de } 271103158cd7SNick Piggin EXPORT_SYMBOL(nobh_write_begin); 27121da177e4SLinus Torvalds 271303158cd7SNick Piggin int nobh_write_end(struct file *file, struct address_space *mapping, 271403158cd7SNick Piggin loff_t pos, unsigned len, unsigned copied, 271503158cd7SNick Piggin struct page *page, void *fsdata) 27161da177e4SLinus Torvalds { 27171da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 2718efdc3131SNick Piggin struct buffer_head *head = fsdata; 271903158cd7SNick Piggin struct buffer_head *bh; 27205b41e74aSDmitri Monakhov BUG_ON(fsdata != NULL && page_has_buffers(page)); 27211da177e4SLinus Torvalds 2722d4cf109fSDave Kleikamp if (unlikely(copied < len) && head) 272303158cd7SNick Piggin attach_nobh_buffers(page, head); 2724a4b0672dSNick Piggin if (page_has_buffers(page)) 272503158cd7SNick Piggin return generic_write_end(file, mapping, pos, len, 272603158cd7SNick Piggin copied, page, fsdata); 2727a4b0672dSNick Piggin 272822c8ca78SNick Piggin SetPageUptodate(page); 27291da177e4SLinus Torvalds set_page_dirty(page); 273003158cd7SNick Piggin if (pos+copied > inode->i_size) { 273103158cd7SNick Piggin i_size_write(inode, pos+copied); 27321da177e4SLinus Torvalds mark_inode_dirty(inode); 27331da177e4SLinus Torvalds } 273403158cd7SNick Piggin 273503158cd7SNick Piggin unlock_page(page); 273609cbfeafSKirill A. Shutemov put_page(page); 273703158cd7SNick Piggin 273803158cd7SNick Piggin while (head) { 273903158cd7SNick Piggin bh = head; 274003158cd7SNick Piggin head = head->b_this_page; 274103158cd7SNick Piggin free_buffer_head(bh); 27421da177e4SLinus Torvalds } 274303158cd7SNick Piggin 274403158cd7SNick Piggin return copied; 274503158cd7SNick Piggin } 274603158cd7SNick Piggin EXPORT_SYMBOL(nobh_write_end); 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds /* 27491da177e4SLinus Torvalds * nobh_writepage() - based on block_full_write_page() except 27501da177e4SLinus Torvalds * that it tries to operate without attaching bufferheads to 27511da177e4SLinus Torvalds * the page. 27521da177e4SLinus Torvalds */ 27531da177e4SLinus Torvalds int nobh_writepage(struct page *page, get_block_t *get_block, 27541da177e4SLinus Torvalds struct writeback_control *wbc) 27551da177e4SLinus Torvalds { 27561da177e4SLinus Torvalds struct inode * const inode = page->mapping->host; 27571da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 275809cbfeafSKirill A. Shutemov const pgoff_t end_index = i_size >> PAGE_SHIFT; 27591da177e4SLinus Torvalds unsigned offset; 27601da177e4SLinus Torvalds int ret; 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds /* Is the page fully inside i_size? */ 27631da177e4SLinus Torvalds if (page->index < end_index) 27641da177e4SLinus Torvalds goto out; 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds /* Is the page fully outside i_size? (truncate in progress) */ 276709cbfeafSKirill A. Shutemov offset = i_size & (PAGE_SIZE-1); 27681da177e4SLinus Torvalds if (page->index >= end_index+1 || !offset) { 27691da177e4SLinus Torvalds /* 27701da177e4SLinus Torvalds * The page may have dirty, unmapped buffers. For example, 27711da177e4SLinus Torvalds * they may have been added in ext3_writepage(). Make them 27721da177e4SLinus Torvalds * freeable here, so the page does not leak. 27731da177e4SLinus Torvalds */ 27741da177e4SLinus Torvalds #if 0 27751da177e4SLinus Torvalds /* Not really sure about this - do we need this ? */ 27761da177e4SLinus Torvalds if (page->mapping->a_ops->invalidatepage) 27771da177e4SLinus Torvalds page->mapping->a_ops->invalidatepage(page, offset); 27781da177e4SLinus Torvalds #endif 27791da177e4SLinus Torvalds unlock_page(page); 27801da177e4SLinus Torvalds return 0; /* don't care */ 27811da177e4SLinus Torvalds } 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds /* 27841da177e4SLinus Torvalds * The page straddles i_size. It must be zeroed out on each and every 27851da177e4SLinus Torvalds * writepage invocation because it may be mmapped. "A file is mapped 27861da177e4SLinus Torvalds * in multiples of the page size. For a file that is not a multiple of 27871da177e4SLinus Torvalds * the page size, the remaining memory is zeroed when mapped, and 27881da177e4SLinus Torvalds * writes to that region are not written out to the file." 27891da177e4SLinus Torvalds */ 279009cbfeafSKirill A. Shutemov zero_user_segment(page, offset, PAGE_SIZE); 27911da177e4SLinus Torvalds out: 27921da177e4SLinus Torvalds ret = mpage_writepage(page, get_block, wbc); 27931da177e4SLinus Torvalds if (ret == -EAGAIN) 279435c80d5fSChris Mason ret = __block_write_full_page(inode, page, get_block, wbc, 279535c80d5fSChris Mason end_buffer_async_write); 27961da177e4SLinus Torvalds return ret; 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds EXPORT_SYMBOL(nobh_writepage); 27991da177e4SLinus Torvalds 280003158cd7SNick Piggin int nobh_truncate_page(struct address_space *mapping, 280103158cd7SNick Piggin loff_t from, get_block_t *get_block) 28021da177e4SLinus Torvalds { 280309cbfeafSKirill A. Shutemov pgoff_t index = from >> PAGE_SHIFT; 280409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 280503158cd7SNick Piggin unsigned blocksize; 280603158cd7SNick Piggin sector_t iblock; 280703158cd7SNick Piggin unsigned length, pos; 280803158cd7SNick Piggin struct inode *inode = mapping->host; 28091da177e4SLinus Torvalds struct page *page; 281003158cd7SNick Piggin struct buffer_head map_bh; 281103158cd7SNick Piggin int err; 28121da177e4SLinus Torvalds 281393407472SFabian Frederick blocksize = i_blocksize(inode); 281403158cd7SNick Piggin length = offset & (blocksize - 1); 28151da177e4SLinus Torvalds 281603158cd7SNick Piggin /* Block boundary? Nothing to do */ 281703158cd7SNick Piggin if (!length) 281803158cd7SNick Piggin return 0; 281903158cd7SNick Piggin 282003158cd7SNick Piggin length = blocksize - length; 282109cbfeafSKirill A. Shutemov iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits); 282203158cd7SNick Piggin 28231da177e4SLinus Torvalds page = grab_cache_page(mapping, index); 282403158cd7SNick Piggin err = -ENOMEM; 28251da177e4SLinus Torvalds if (!page) 28261da177e4SLinus Torvalds goto out; 28271da177e4SLinus Torvalds 282803158cd7SNick Piggin if (page_has_buffers(page)) { 282903158cd7SNick Piggin has_buffers: 283003158cd7SNick Piggin unlock_page(page); 283109cbfeafSKirill A. Shutemov put_page(page); 283203158cd7SNick Piggin return block_truncate_page(mapping, from, get_block); 28331da177e4SLinus Torvalds } 283403158cd7SNick Piggin 283503158cd7SNick Piggin /* Find the buffer that contains "offset" */ 283603158cd7SNick Piggin pos = blocksize; 283703158cd7SNick Piggin while (offset >= pos) { 283803158cd7SNick Piggin iblock++; 283903158cd7SNick Piggin pos += blocksize; 284003158cd7SNick Piggin } 284103158cd7SNick Piggin 2842460bcf57STheodore Ts'o map_bh.b_size = blocksize; 2843460bcf57STheodore Ts'o map_bh.b_state = 0; 284403158cd7SNick Piggin err = get_block(inode, iblock, &map_bh, 0); 284503158cd7SNick Piggin if (err) 284603158cd7SNick Piggin goto unlock; 284703158cd7SNick Piggin /* unmapped? It's a hole - nothing to do */ 284803158cd7SNick Piggin if (!buffer_mapped(&map_bh)) 284903158cd7SNick Piggin goto unlock; 285003158cd7SNick Piggin 285103158cd7SNick Piggin /* Ok, it's mapped. Make sure it's up-to-date */ 285203158cd7SNick Piggin if (!PageUptodate(page)) { 285303158cd7SNick Piggin err = mapping->a_ops->readpage(NULL, page); 285403158cd7SNick Piggin if (err) { 285509cbfeafSKirill A. Shutemov put_page(page); 285603158cd7SNick Piggin goto out; 285703158cd7SNick Piggin } 285803158cd7SNick Piggin lock_page(page); 285903158cd7SNick Piggin if (!PageUptodate(page)) { 286003158cd7SNick Piggin err = -EIO; 286103158cd7SNick Piggin goto unlock; 286203158cd7SNick Piggin } 286303158cd7SNick Piggin if (page_has_buffers(page)) 286403158cd7SNick Piggin goto has_buffers; 286503158cd7SNick Piggin } 2866eebd2aa3SChristoph Lameter zero_user(page, offset, length); 286703158cd7SNick Piggin set_page_dirty(page); 286803158cd7SNick Piggin err = 0; 286903158cd7SNick Piggin 287003158cd7SNick Piggin unlock: 28711da177e4SLinus Torvalds unlock_page(page); 287209cbfeafSKirill A. Shutemov put_page(page); 28731da177e4SLinus Torvalds out: 287403158cd7SNick Piggin return err; 28751da177e4SLinus Torvalds } 28761da177e4SLinus Torvalds EXPORT_SYMBOL(nobh_truncate_page); 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds int block_truncate_page(struct address_space *mapping, 28791da177e4SLinus Torvalds loff_t from, get_block_t *get_block) 28801da177e4SLinus Torvalds { 288109cbfeafSKirill A. Shutemov pgoff_t index = from >> PAGE_SHIFT; 288209cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 28831da177e4SLinus Torvalds unsigned blocksize; 288454b21a79SAndrew Morton sector_t iblock; 28851da177e4SLinus Torvalds unsigned length, pos; 28861da177e4SLinus Torvalds struct inode *inode = mapping->host; 28871da177e4SLinus Torvalds struct page *page; 28881da177e4SLinus Torvalds struct buffer_head *bh; 28891da177e4SLinus Torvalds int err; 28901da177e4SLinus Torvalds 289193407472SFabian Frederick blocksize = i_blocksize(inode); 28921da177e4SLinus Torvalds length = offset & (blocksize - 1); 28931da177e4SLinus Torvalds 28941da177e4SLinus Torvalds /* Block boundary? Nothing to do */ 28951da177e4SLinus Torvalds if (!length) 28961da177e4SLinus Torvalds return 0; 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds length = blocksize - length; 289909cbfeafSKirill A. Shutemov iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits); 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds page = grab_cache_page(mapping, index); 29021da177e4SLinus Torvalds err = -ENOMEM; 29031da177e4SLinus Torvalds if (!page) 29041da177e4SLinus Torvalds goto out; 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds if (!page_has_buffers(page)) 29071da177e4SLinus Torvalds create_empty_buffers(page, blocksize, 0); 29081da177e4SLinus Torvalds 29091da177e4SLinus Torvalds /* Find the buffer that contains "offset" */ 29101da177e4SLinus Torvalds bh = page_buffers(page); 29111da177e4SLinus Torvalds pos = blocksize; 29121da177e4SLinus Torvalds while (offset >= pos) { 29131da177e4SLinus Torvalds bh = bh->b_this_page; 29141da177e4SLinus Torvalds iblock++; 29151da177e4SLinus Torvalds pos += blocksize; 29161da177e4SLinus Torvalds } 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds err = 0; 29191da177e4SLinus Torvalds if (!buffer_mapped(bh)) { 2920b0cf2321SBadari Pulavarty WARN_ON(bh->b_size != blocksize); 29211da177e4SLinus Torvalds err = get_block(inode, iblock, bh, 0); 29221da177e4SLinus Torvalds if (err) 29231da177e4SLinus Torvalds goto unlock; 29241da177e4SLinus Torvalds /* unmapped? It's a hole - nothing to do */ 29251da177e4SLinus Torvalds if (!buffer_mapped(bh)) 29261da177e4SLinus Torvalds goto unlock; 29271da177e4SLinus Torvalds } 29281da177e4SLinus Torvalds 29291da177e4SLinus Torvalds /* Ok, it's mapped. Make sure it's up-to-date */ 29301da177e4SLinus Torvalds if (PageUptodate(page)) 29311da177e4SLinus Torvalds set_buffer_uptodate(bh); 29321da177e4SLinus Torvalds 293333a266ddSDavid Chinner if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) { 29341da177e4SLinus Torvalds err = -EIO; 2935dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 29361da177e4SLinus Torvalds wait_on_buffer(bh); 29371da177e4SLinus Torvalds /* Uhhuh. Read error. Complain and punt. */ 29381da177e4SLinus Torvalds if (!buffer_uptodate(bh)) 29391da177e4SLinus Torvalds goto unlock; 29401da177e4SLinus Torvalds } 29411da177e4SLinus Torvalds 2942eebd2aa3SChristoph Lameter zero_user(page, offset, length); 29431da177e4SLinus Torvalds mark_buffer_dirty(bh); 29441da177e4SLinus Torvalds err = 0; 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds unlock: 29471da177e4SLinus Torvalds unlock_page(page); 294809cbfeafSKirill A. Shutemov put_page(page); 29491da177e4SLinus Torvalds out: 29501da177e4SLinus Torvalds return err; 29511da177e4SLinus Torvalds } 29521fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_truncate_page); 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds /* 29551da177e4SLinus Torvalds * The generic ->writepage function for buffer-backed address_spaces 29561da177e4SLinus Torvalds */ 29571b938c08SMatthew Wilcox int block_write_full_page(struct page *page, get_block_t *get_block, 29581b938c08SMatthew Wilcox struct writeback_control *wbc) 29591da177e4SLinus Torvalds { 29601da177e4SLinus Torvalds struct inode * const inode = page->mapping->host; 29611da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 296209cbfeafSKirill A. Shutemov const pgoff_t end_index = i_size >> PAGE_SHIFT; 29631da177e4SLinus Torvalds unsigned offset; 29641da177e4SLinus Torvalds 29651da177e4SLinus Torvalds /* Is the page fully inside i_size? */ 29661da177e4SLinus Torvalds if (page->index < end_index) 296735c80d5fSChris Mason return __block_write_full_page(inode, page, get_block, wbc, 29681b938c08SMatthew Wilcox end_buffer_async_write); 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds /* Is the page fully outside i_size? (truncate in progress) */ 297109cbfeafSKirill A. Shutemov offset = i_size & (PAGE_SIZE-1); 29721da177e4SLinus Torvalds if (page->index >= end_index+1 || !offset) { 29731da177e4SLinus Torvalds /* 29741da177e4SLinus Torvalds * The page may have dirty, unmapped buffers. For example, 29751da177e4SLinus Torvalds * they may have been added in ext3_writepage(). Make them 29761da177e4SLinus Torvalds * freeable here, so the page does not leak. 29771da177e4SLinus Torvalds */ 297809cbfeafSKirill A. Shutemov do_invalidatepage(page, 0, PAGE_SIZE); 29791da177e4SLinus Torvalds unlock_page(page); 29801da177e4SLinus Torvalds return 0; /* don't care */ 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds /* 29841da177e4SLinus Torvalds * The page straddles i_size. It must be zeroed out on each and every 29852a61aa40SAdam Buchbinder * writepage invocation because it may be mmapped. "A file is mapped 29861da177e4SLinus Torvalds * in multiples of the page size. For a file that is not a multiple of 29871da177e4SLinus Torvalds * the page size, the remaining memory is zeroed when mapped, and 29881da177e4SLinus Torvalds * writes to that region are not written out to the file." 29891da177e4SLinus Torvalds */ 299009cbfeafSKirill A. Shutemov zero_user_segment(page, offset, PAGE_SIZE); 29911b938c08SMatthew Wilcox return __block_write_full_page(inode, page, get_block, wbc, 299235c80d5fSChris Mason end_buffer_async_write); 299335c80d5fSChris Mason } 29941fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(block_write_full_page); 299535c80d5fSChris Mason 29961da177e4SLinus Torvalds sector_t generic_block_bmap(struct address_space *mapping, sector_t block, 29971da177e4SLinus Torvalds get_block_t *get_block) 29981da177e4SLinus Torvalds { 29991da177e4SLinus Torvalds struct inode *inode = mapping->host; 30002a527d68SAlexander Potapenko struct buffer_head tmp = { 30012a527d68SAlexander Potapenko .b_size = i_blocksize(inode), 30022a527d68SAlexander Potapenko }; 30032a527d68SAlexander Potapenko 30041da177e4SLinus Torvalds get_block(inode, block, &tmp, 0); 30051da177e4SLinus Torvalds return tmp.b_blocknr; 30061da177e4SLinus Torvalds } 30071fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(generic_block_bmap); 30081da177e4SLinus Torvalds 30094246a0b6SChristoph Hellwig static void end_bio_bh_io_sync(struct bio *bio) 30101da177e4SLinus Torvalds { 30111da177e4SLinus Torvalds struct buffer_head *bh = bio->bi_private; 30121da177e4SLinus Torvalds 3013b7c44ed9SJens Axboe if (unlikely(bio_flagged(bio, BIO_QUIET))) 301408bafc03SKeith Mannthey set_bit(BH_Quiet, &bh->b_state); 301508bafc03SKeith Mannthey 30164e4cbee9SChristoph Hellwig bh->b_end_io(bh, !bio->bi_status); 30171da177e4SLinus Torvalds bio_put(bio); 30181da177e4SLinus Torvalds } 30191da177e4SLinus Torvalds 302057302e0dSLinus Torvalds /* 302157302e0dSLinus Torvalds * This allows us to do IO even on the odd last sectors 302259d43914SAkinobu Mita * of a device, even if the block size is some multiple 302357302e0dSLinus Torvalds * of the physical sector size. 302457302e0dSLinus Torvalds * 302557302e0dSLinus Torvalds * We'll just truncate the bio to the size of the device, 302657302e0dSLinus Torvalds * and clear the end of the buffer head manually. 302757302e0dSLinus Torvalds * 302857302e0dSLinus Torvalds * Truly out-of-range accesses will turn into actual IO 302957302e0dSLinus Torvalds * errors, this only handles the "we need to be able to 303057302e0dSLinus Torvalds * do IO at the final sector" case. 303157302e0dSLinus Torvalds */ 30322a222ca9SMike Christie void guard_bio_eod(int op, struct bio *bio) 303357302e0dSLinus Torvalds { 303457302e0dSLinus Torvalds sector_t maxsector; 3035c45a8f2dSMing Lei struct bio_vec *bvec = bio_last_bvec_all(bio); 303659d43914SAkinobu Mita unsigned truncated_bytes; 303767f2519fSGreg Edwards struct hd_struct *part; 303857302e0dSLinus Torvalds 303967f2519fSGreg Edwards rcu_read_lock(); 304067f2519fSGreg Edwards part = __disk_get_part(bio->bi_disk, bio->bi_partno); 304167f2519fSGreg Edwards if (part) 304267f2519fSGreg Edwards maxsector = part_nr_sects_read(part); 304367f2519fSGreg Edwards else 304474d46992SChristoph Hellwig maxsector = get_capacity(bio->bi_disk); 304567f2519fSGreg Edwards rcu_read_unlock(); 304667f2519fSGreg Edwards 304757302e0dSLinus Torvalds if (!maxsector) 304857302e0dSLinus Torvalds return; 304957302e0dSLinus Torvalds 305057302e0dSLinus Torvalds /* 305157302e0dSLinus Torvalds * If the *whole* IO is past the end of the device, 305257302e0dSLinus Torvalds * let it through, and the IO layer will turn it into 305357302e0dSLinus Torvalds * an EIO. 305457302e0dSLinus Torvalds */ 30554f024f37SKent Overstreet if (unlikely(bio->bi_iter.bi_sector >= maxsector)) 305657302e0dSLinus Torvalds return; 305757302e0dSLinus Torvalds 30584f024f37SKent Overstreet maxsector -= bio->bi_iter.bi_sector; 305959d43914SAkinobu Mita if (likely((bio->bi_iter.bi_size >> 9) <= maxsector)) 306057302e0dSLinus Torvalds return; 306157302e0dSLinus Torvalds 306259d43914SAkinobu Mita /* Uhhuh. We've got a bio that straddles the device size! */ 306359d43914SAkinobu Mita truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9); 306457302e0dSLinus Torvalds 3065dce30ca9SCarlos Maiolino /* 3066dce30ca9SCarlos Maiolino * The bio contains more than one segment which spans EOD, just return 3067dce30ca9SCarlos Maiolino * and let IO layer turn it into an EIO 3068dce30ca9SCarlos Maiolino */ 3069dce30ca9SCarlos Maiolino if (truncated_bytes > bvec->bv_len) 3070dce30ca9SCarlos Maiolino return; 3071dce30ca9SCarlos Maiolino 307257302e0dSLinus Torvalds /* Truncate the bio.. */ 307359d43914SAkinobu Mita bio->bi_iter.bi_size -= truncated_bytes; 307459d43914SAkinobu Mita bvec->bv_len -= truncated_bytes; 307557302e0dSLinus Torvalds 307657302e0dSLinus Torvalds /* ..and clear the end of the buffer for reads */ 30772a222ca9SMike Christie if (op == REQ_OP_READ) { 3078f70f4464SMing Lei struct bio_vec bv; 3079f70f4464SMing Lei 3080f70f4464SMing Lei mp_bvec_last_segment(bvec, &bv); 3081f70f4464SMing Lei zero_user(bv.bv_page, bv.bv_offset + bv.bv_len, 308259d43914SAkinobu Mita truncated_bytes); 308357302e0dSLinus Torvalds } 308457302e0dSLinus Torvalds } 308557302e0dSLinus Torvalds 30862a222ca9SMike Christie static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh, 30878e8f9298SJens Axboe enum rw_hint write_hint, struct writeback_control *wbc) 30881da177e4SLinus Torvalds { 30891da177e4SLinus Torvalds struct bio *bio; 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds BUG_ON(!buffer_locked(bh)); 30921da177e4SLinus Torvalds BUG_ON(!buffer_mapped(bh)); 30931da177e4SLinus Torvalds BUG_ON(!bh->b_end_io); 30948fb0e342SAneesh Kumar K.V BUG_ON(buffer_delay(bh)); 30958fb0e342SAneesh Kumar K.V BUG_ON(buffer_unwritten(bh)); 30961da177e4SLinus Torvalds 309748fd4f93SJens Axboe /* 309848fd4f93SJens Axboe * Only clear out a write error when rewriting 30991da177e4SLinus Torvalds */ 31002a222ca9SMike Christie if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE)) 31011da177e4SLinus Torvalds clear_buffer_write_io_error(bh); 31021da177e4SLinus Torvalds 31031da177e4SLinus Torvalds /* 31041da177e4SLinus Torvalds * from here on down, it's all bio -- do the initial mapping, 31051da177e4SLinus Torvalds * submit_bio -> generic_make_request may further map this bio around 31061da177e4SLinus Torvalds */ 31071da177e4SLinus Torvalds bio = bio_alloc(GFP_NOIO, 1); 31081da177e4SLinus Torvalds 31094f024f37SKent Overstreet bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); 311074d46992SChristoph Hellwig bio_set_dev(bio, bh->b_bdev); 31118e8f9298SJens Axboe bio->bi_write_hint = write_hint; 31121da177e4SLinus Torvalds 31136cf66b4cSKent Overstreet bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); 31146cf66b4cSKent Overstreet BUG_ON(bio->bi_iter.bi_size != bh->b_size); 31151da177e4SLinus Torvalds 31161da177e4SLinus Torvalds bio->bi_end_io = end_bio_bh_io_sync; 31171da177e4SLinus Torvalds bio->bi_private = bh; 31181da177e4SLinus Torvalds 311957302e0dSLinus Torvalds /* Take care of bh's that straddle the end of the device */ 31202a222ca9SMike Christie guard_bio_eod(op, bio); 312157302e0dSLinus Torvalds 3122877f962cSTheodore Ts'o if (buffer_meta(bh)) 31232a222ca9SMike Christie op_flags |= REQ_META; 3124877f962cSTheodore Ts'o if (buffer_prio(bh)) 31252a222ca9SMike Christie op_flags |= REQ_PRIO; 31262a222ca9SMike Christie bio_set_op_attrs(bio, op, op_flags); 3127877f962cSTheodore Ts'o 3128fd42df30SDennis Zhou if (wbc) { 3129fd42df30SDennis Zhou wbc_init_bio(wbc, bio); 313034e51a5eSTejun Heo wbc_account_cgroup_owner(wbc, bh->b_page, bh->b_size); 3131fd42df30SDennis Zhou } 3132fd42df30SDennis Zhou 31334e49ea4aSMike Christie submit_bio(bio); 3134f6454b04SJulia Lawall return 0; 31351da177e4SLinus Torvalds } 3136bafc0dbaSTejun Heo 31372a222ca9SMike Christie int submit_bh(int op, int op_flags, struct buffer_head *bh) 313871368511SDarrick J. Wong { 31398e8f9298SJens Axboe return submit_bh_wbc(op, op_flags, bh, 0, NULL); 314071368511SDarrick J. Wong } 31411fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(submit_bh); 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds /** 31441da177e4SLinus Torvalds * ll_rw_block: low-level access to block devices (DEPRECATED) 3145dfec8a14SMike Christie * @op: whether to %READ or %WRITE 3146ef295ecfSChristoph Hellwig * @op_flags: req_flag_bits 31471da177e4SLinus Torvalds * @nr: number of &struct buffer_heads in the array 31481da177e4SLinus Torvalds * @bhs: array of pointers to &struct buffer_head 31491da177e4SLinus Torvalds * 3150a7662236SJan Kara * ll_rw_block() takes an array of pointers to &struct buffer_heads, and 315170246286SChristoph Hellwig * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE. 315270246286SChristoph Hellwig * @op_flags contains flags modifying the detailed I/O behavior, most notably 315370246286SChristoph Hellwig * %REQ_RAHEAD. 31541da177e4SLinus Torvalds * 31551da177e4SLinus Torvalds * This function drops any buffer that it cannot get a lock on (with the 31569cb569d6SChristoph Hellwig * BH_Lock state bit), any buffer that appears to be clean when doing a write 31579cb569d6SChristoph Hellwig * request, and any buffer that appears to be up-to-date when doing read 31589cb569d6SChristoph Hellwig * request. Further it marks as clean buffers that are processed for 31599cb569d6SChristoph Hellwig * writing (the buffer cache won't assume that they are actually clean 31609cb569d6SChristoph Hellwig * until the buffer gets unlocked). 31611da177e4SLinus Torvalds * 31621da177e4SLinus Torvalds * ll_rw_block sets b_end_io to simple completion handler that marks 3163e227867fSMasanari Iida * the buffer up-to-date (if appropriate), unlocks the buffer and wakes 31641da177e4SLinus Torvalds * any waiters. 31651da177e4SLinus Torvalds * 31661da177e4SLinus Torvalds * All of the buffers must be for the same device, and must also be a 31671da177e4SLinus Torvalds * multiple of the current approved size for the device. 31681da177e4SLinus Torvalds */ 3169dfec8a14SMike Christie void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[]) 31701da177e4SLinus Torvalds { 31711da177e4SLinus Torvalds int i; 31721da177e4SLinus Torvalds 31731da177e4SLinus Torvalds for (i = 0; i < nr; i++) { 31741da177e4SLinus Torvalds struct buffer_head *bh = bhs[i]; 31751da177e4SLinus Torvalds 31769cb569d6SChristoph Hellwig if (!trylock_buffer(bh)) 31771da177e4SLinus Torvalds continue; 3178dfec8a14SMike Christie if (op == WRITE) { 31791da177e4SLinus Torvalds if (test_clear_buffer_dirty(bh)) { 318076c3073aSakpm@osdl.org bh->b_end_io = end_buffer_write_sync; 3181e60e5c50SOGAWA Hirofumi get_bh(bh); 3182dfec8a14SMike Christie submit_bh(op, op_flags, bh); 31831da177e4SLinus Torvalds continue; 31841da177e4SLinus Torvalds } 31851da177e4SLinus Torvalds } else { 31861da177e4SLinus Torvalds if (!buffer_uptodate(bh)) { 318776c3073aSakpm@osdl.org bh->b_end_io = end_buffer_read_sync; 3188e60e5c50SOGAWA Hirofumi get_bh(bh); 3189dfec8a14SMike Christie submit_bh(op, op_flags, bh); 31901da177e4SLinus Torvalds continue; 31911da177e4SLinus Torvalds } 31921da177e4SLinus Torvalds } 31931da177e4SLinus Torvalds unlock_buffer(bh); 31941da177e4SLinus Torvalds } 31951da177e4SLinus Torvalds } 31961fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(ll_rw_block); 31971da177e4SLinus Torvalds 31982a222ca9SMike Christie void write_dirty_buffer(struct buffer_head *bh, int op_flags) 31999cb569d6SChristoph Hellwig { 32009cb569d6SChristoph Hellwig lock_buffer(bh); 32019cb569d6SChristoph Hellwig if (!test_clear_buffer_dirty(bh)) { 32029cb569d6SChristoph Hellwig unlock_buffer(bh); 32039cb569d6SChristoph Hellwig return; 32049cb569d6SChristoph Hellwig } 32059cb569d6SChristoph Hellwig bh->b_end_io = end_buffer_write_sync; 32069cb569d6SChristoph Hellwig get_bh(bh); 32072a222ca9SMike Christie submit_bh(REQ_OP_WRITE, op_flags, bh); 32089cb569d6SChristoph Hellwig } 32099cb569d6SChristoph Hellwig EXPORT_SYMBOL(write_dirty_buffer); 32109cb569d6SChristoph Hellwig 32111da177e4SLinus Torvalds /* 32121da177e4SLinus Torvalds * For a data-integrity writeout, we need to wait upon any in-progress I/O 32131da177e4SLinus Torvalds * and then start new I/O and then wait upon it. The caller must have a ref on 32141da177e4SLinus Torvalds * the buffer_head. 32151da177e4SLinus Torvalds */ 32162a222ca9SMike Christie int __sync_dirty_buffer(struct buffer_head *bh, int op_flags) 32171da177e4SLinus Torvalds { 32181da177e4SLinus Torvalds int ret = 0; 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds WARN_ON(atomic_read(&bh->b_count) < 1); 32211da177e4SLinus Torvalds lock_buffer(bh); 32221da177e4SLinus Torvalds if (test_clear_buffer_dirty(bh)) { 32231da177e4SLinus Torvalds get_bh(bh); 32241da177e4SLinus Torvalds bh->b_end_io = end_buffer_write_sync; 32252a222ca9SMike Christie ret = submit_bh(REQ_OP_WRITE, op_flags, bh); 32261da177e4SLinus Torvalds wait_on_buffer(bh); 32271da177e4SLinus Torvalds if (!ret && !buffer_uptodate(bh)) 32281da177e4SLinus Torvalds ret = -EIO; 32291da177e4SLinus Torvalds } else { 32301da177e4SLinus Torvalds unlock_buffer(bh); 32311da177e4SLinus Torvalds } 32321da177e4SLinus Torvalds return ret; 32331da177e4SLinus Torvalds } 323487e99511SChristoph Hellwig EXPORT_SYMBOL(__sync_dirty_buffer); 323587e99511SChristoph Hellwig 323687e99511SChristoph Hellwig int sync_dirty_buffer(struct buffer_head *bh) 323787e99511SChristoph Hellwig { 323870fd7614SChristoph Hellwig return __sync_dirty_buffer(bh, REQ_SYNC); 323987e99511SChristoph Hellwig } 32401fe72eaaSH Hartley Sweeten EXPORT_SYMBOL(sync_dirty_buffer); 32411da177e4SLinus Torvalds 32421da177e4SLinus Torvalds /* 32431da177e4SLinus Torvalds * try_to_free_buffers() checks if all the buffers on this particular page 32441da177e4SLinus Torvalds * are unused, and releases them if so. 32451da177e4SLinus Torvalds * 32461da177e4SLinus Torvalds * Exclusion against try_to_free_buffers may be obtained by either 32471da177e4SLinus Torvalds * locking the page or by holding its mapping's private_lock. 32481da177e4SLinus Torvalds * 32491da177e4SLinus Torvalds * If the page is dirty but all the buffers are clean then we need to 32501da177e4SLinus Torvalds * be sure to mark the page clean as well. This is because the page 32511da177e4SLinus Torvalds * may be against a block device, and a later reattachment of buffers 32521da177e4SLinus Torvalds * to a dirty page will set *all* buffers dirty. Which would corrupt 32531da177e4SLinus Torvalds * filesystem data on the same device. 32541da177e4SLinus Torvalds * 32551da177e4SLinus Torvalds * The same applies to regular filesystem pages: if all the buffers are 32561da177e4SLinus Torvalds * clean then we set the page clean and proceed. To do that, we require 32571da177e4SLinus Torvalds * total exclusion from __set_page_dirty_buffers(). That is obtained with 32581da177e4SLinus Torvalds * private_lock. 32591da177e4SLinus Torvalds * 32601da177e4SLinus Torvalds * try_to_free_buffers() is non-blocking. 32611da177e4SLinus Torvalds */ 32621da177e4SLinus Torvalds static inline int buffer_busy(struct buffer_head *bh) 32631da177e4SLinus Torvalds { 32641da177e4SLinus Torvalds return atomic_read(&bh->b_count) | 32651da177e4SLinus Torvalds (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock))); 32661da177e4SLinus Torvalds } 32671da177e4SLinus Torvalds 32681da177e4SLinus Torvalds static int 32691da177e4SLinus Torvalds drop_buffers(struct page *page, struct buffer_head **buffers_to_free) 32701da177e4SLinus Torvalds { 32711da177e4SLinus Torvalds struct buffer_head *head = page_buffers(page); 32721da177e4SLinus Torvalds struct buffer_head *bh; 32731da177e4SLinus Torvalds 32741da177e4SLinus Torvalds bh = head; 32751da177e4SLinus Torvalds do { 32761da177e4SLinus Torvalds if (buffer_busy(bh)) 32771da177e4SLinus Torvalds goto failed; 32781da177e4SLinus Torvalds bh = bh->b_this_page; 32791da177e4SLinus Torvalds } while (bh != head); 32801da177e4SLinus Torvalds 32811da177e4SLinus Torvalds do { 32821da177e4SLinus Torvalds struct buffer_head *next = bh->b_this_page; 32831da177e4SLinus Torvalds 3284535ee2fbSJan Kara if (bh->b_assoc_map) 32851da177e4SLinus Torvalds __remove_assoc_queue(bh); 32861da177e4SLinus Torvalds bh = next; 32871da177e4SLinus Torvalds } while (bh != head); 32881da177e4SLinus Torvalds *buffers_to_free = head; 32891da177e4SLinus Torvalds __clear_page_buffers(page); 32901da177e4SLinus Torvalds return 1; 32911da177e4SLinus Torvalds failed: 32921da177e4SLinus Torvalds return 0; 32931da177e4SLinus Torvalds } 32941da177e4SLinus Torvalds 32951da177e4SLinus Torvalds int try_to_free_buffers(struct page *page) 32961da177e4SLinus Torvalds { 32971da177e4SLinus Torvalds struct address_space * const mapping = page->mapping; 32981da177e4SLinus Torvalds struct buffer_head *buffers_to_free = NULL; 32991da177e4SLinus Torvalds int ret = 0; 33001da177e4SLinus Torvalds 33011da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 3302ecdfc978SLinus Torvalds if (PageWriteback(page)) 33031da177e4SLinus Torvalds return 0; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds if (mapping == NULL) { /* can this still happen? */ 33061da177e4SLinus Torvalds ret = drop_buffers(page, &buffers_to_free); 33071da177e4SLinus Torvalds goto out; 33081da177e4SLinus Torvalds } 33091da177e4SLinus Torvalds 33101da177e4SLinus Torvalds spin_lock(&mapping->private_lock); 33111da177e4SLinus Torvalds ret = drop_buffers(page, &buffers_to_free); 3312ecdfc978SLinus Torvalds 3313ecdfc978SLinus Torvalds /* 3314ecdfc978SLinus Torvalds * If the filesystem writes its buffers by hand (eg ext3) 3315ecdfc978SLinus Torvalds * then we can have clean buffers against a dirty page. We 3316ecdfc978SLinus Torvalds * clean the page here; otherwise the VM will never notice 3317ecdfc978SLinus Torvalds * that the filesystem did any IO at all. 3318ecdfc978SLinus Torvalds * 3319ecdfc978SLinus Torvalds * Also, during truncate, discard_buffer will have marked all 3320ecdfc978SLinus Torvalds * the page's buffers clean. We discover that here and clean 3321ecdfc978SLinus Torvalds * the page also. 332287df7241SNick Piggin * 332387df7241SNick Piggin * private_lock must be held over this entire operation in order 332487df7241SNick Piggin * to synchronise against __set_page_dirty_buffers and prevent the 332587df7241SNick Piggin * dirty bit from being lost. 3326ecdfc978SLinus Torvalds */ 332711f81becSTejun Heo if (ret) 332811f81becSTejun Heo cancel_dirty_page(page); 332987df7241SNick Piggin spin_unlock(&mapping->private_lock); 33301da177e4SLinus Torvalds out: 33311da177e4SLinus Torvalds if (buffers_to_free) { 33321da177e4SLinus Torvalds struct buffer_head *bh = buffers_to_free; 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds do { 33351da177e4SLinus Torvalds struct buffer_head *next = bh->b_this_page; 33361da177e4SLinus Torvalds free_buffer_head(bh); 33371da177e4SLinus Torvalds bh = next; 33381da177e4SLinus Torvalds } while (bh != buffers_to_free); 33391da177e4SLinus Torvalds } 33401da177e4SLinus Torvalds return ret; 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds EXPORT_SYMBOL(try_to_free_buffers); 33431da177e4SLinus Torvalds 33441da177e4SLinus Torvalds /* 33451da177e4SLinus Torvalds * There are no bdflush tunables left. But distributions are 33461da177e4SLinus Torvalds * still running obsolete flush daemons, so we terminate them here. 33471da177e4SLinus Torvalds * 33481da177e4SLinus Torvalds * Use of bdflush() is deprecated and will be removed in a future kernel. 33495b0830cbSJens Axboe * The `flush-X' kernel threads fully replace bdflush daemons and this call. 33501da177e4SLinus Torvalds */ 3351bdc480e3SHeiko Carstens SYSCALL_DEFINE2(bdflush, int, func, long, data) 33521da177e4SLinus Torvalds { 33531da177e4SLinus Torvalds static int msg_count; 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 33561da177e4SLinus Torvalds return -EPERM; 33571da177e4SLinus Torvalds 33581da177e4SLinus Torvalds if (msg_count < 5) { 33591da177e4SLinus Torvalds msg_count++; 33601da177e4SLinus Torvalds printk(KERN_INFO 33611da177e4SLinus Torvalds "warning: process `%s' used the obsolete bdflush" 33621da177e4SLinus Torvalds " system call\n", current->comm); 33631da177e4SLinus Torvalds printk(KERN_INFO "Fix your initscripts?\n"); 33641da177e4SLinus Torvalds } 33651da177e4SLinus Torvalds 33661da177e4SLinus Torvalds if (func == 1) 33671da177e4SLinus Torvalds do_exit(0); 33681da177e4SLinus Torvalds return 0; 33691da177e4SLinus Torvalds } 33701da177e4SLinus Torvalds 33711da177e4SLinus Torvalds /* 33721da177e4SLinus Torvalds * Buffer-head allocation 33731da177e4SLinus Torvalds */ 3374a0a9b043SShai Fultheim static struct kmem_cache *bh_cachep __read_mostly; 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds /* 33771da177e4SLinus Torvalds * Once the number of bh's in the machine exceeds this level, we start 33781da177e4SLinus Torvalds * stripping them in writeback. 33791da177e4SLinus Torvalds */ 338043be594aSZhang Yanfei static unsigned long max_buffer_heads; 33811da177e4SLinus Torvalds 33821da177e4SLinus Torvalds int buffer_heads_over_limit; 33831da177e4SLinus Torvalds 33841da177e4SLinus Torvalds struct bh_accounting { 33851da177e4SLinus Torvalds int nr; /* Number of live bh's */ 33861da177e4SLinus Torvalds int ratelimit; /* Limit cacheline bouncing */ 33871da177e4SLinus Torvalds }; 33881da177e4SLinus Torvalds 33891da177e4SLinus Torvalds static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0}; 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds static void recalc_bh_state(void) 33921da177e4SLinus Torvalds { 33931da177e4SLinus Torvalds int i; 33941da177e4SLinus Torvalds int tot = 0; 33951da177e4SLinus Torvalds 3396ee1be862SChristoph Lameter if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096) 33971da177e4SLinus Torvalds return; 3398c7b92516SChristoph Lameter __this_cpu_write(bh_accounting.ratelimit, 0); 33998a143426SEric Dumazet for_each_online_cpu(i) 34001da177e4SLinus Torvalds tot += per_cpu(bh_accounting, i).nr; 34011da177e4SLinus Torvalds buffer_heads_over_limit = (tot > max_buffer_heads); 34021da177e4SLinus Torvalds } 34031da177e4SLinus Torvalds 3404dd0fc66fSAl Viro struct buffer_head *alloc_buffer_head(gfp_t gfp_flags) 34051da177e4SLinus Torvalds { 3406019b4d12SRichard Kennedy struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags); 34071da177e4SLinus Torvalds if (ret) { 3408a35afb83SChristoph Lameter INIT_LIST_HEAD(&ret->b_assoc_buffers); 3409c7b92516SChristoph Lameter preempt_disable(); 3410c7b92516SChristoph Lameter __this_cpu_inc(bh_accounting.nr); 34111da177e4SLinus Torvalds recalc_bh_state(); 3412c7b92516SChristoph Lameter preempt_enable(); 34131da177e4SLinus Torvalds } 34141da177e4SLinus Torvalds return ret; 34151da177e4SLinus Torvalds } 34161da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_buffer_head); 34171da177e4SLinus Torvalds 34181da177e4SLinus Torvalds void free_buffer_head(struct buffer_head *bh) 34191da177e4SLinus Torvalds { 34201da177e4SLinus Torvalds BUG_ON(!list_empty(&bh->b_assoc_buffers)); 34211da177e4SLinus Torvalds kmem_cache_free(bh_cachep, bh); 3422c7b92516SChristoph Lameter preempt_disable(); 3423c7b92516SChristoph Lameter __this_cpu_dec(bh_accounting.nr); 34241da177e4SLinus Torvalds recalc_bh_state(); 3425c7b92516SChristoph Lameter preempt_enable(); 34261da177e4SLinus Torvalds } 34271da177e4SLinus Torvalds EXPORT_SYMBOL(free_buffer_head); 34281da177e4SLinus Torvalds 3429fc4d24c9SSebastian Andrzej Siewior static int buffer_exit_cpu_dead(unsigned int cpu) 34301da177e4SLinus Torvalds { 34311da177e4SLinus Torvalds int i; 34321da177e4SLinus Torvalds struct bh_lru *b = &per_cpu(bh_lrus, cpu); 34331da177e4SLinus Torvalds 34341da177e4SLinus Torvalds for (i = 0; i < BH_LRU_SIZE; i++) { 34351da177e4SLinus Torvalds brelse(b->bhs[i]); 34361da177e4SLinus Torvalds b->bhs[i] = NULL; 34371da177e4SLinus Torvalds } 3438c7b92516SChristoph Lameter this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr); 34398a143426SEric Dumazet per_cpu(bh_accounting, cpu).nr = 0; 3440fc4d24c9SSebastian Andrzej Siewior return 0; 34411da177e4SLinus Torvalds } 34421da177e4SLinus Torvalds 3443389d1b08SAneesh Kumar K.V /** 3444a6b91919SRandy Dunlap * bh_uptodate_or_lock - Test whether the buffer is uptodate 3445389d1b08SAneesh Kumar K.V * @bh: struct buffer_head 3446389d1b08SAneesh Kumar K.V * 3447389d1b08SAneesh Kumar K.V * Return true if the buffer is up-to-date and false, 3448389d1b08SAneesh Kumar K.V * with the buffer locked, if not. 3449389d1b08SAneesh Kumar K.V */ 3450389d1b08SAneesh Kumar K.V int bh_uptodate_or_lock(struct buffer_head *bh) 3451389d1b08SAneesh Kumar K.V { 3452389d1b08SAneesh Kumar K.V if (!buffer_uptodate(bh)) { 3453389d1b08SAneesh Kumar K.V lock_buffer(bh); 3454389d1b08SAneesh Kumar K.V if (!buffer_uptodate(bh)) 3455389d1b08SAneesh Kumar K.V return 0; 3456389d1b08SAneesh Kumar K.V unlock_buffer(bh); 3457389d1b08SAneesh Kumar K.V } 3458389d1b08SAneesh Kumar K.V return 1; 3459389d1b08SAneesh Kumar K.V } 3460389d1b08SAneesh Kumar K.V EXPORT_SYMBOL(bh_uptodate_or_lock); 3461389d1b08SAneesh Kumar K.V 3462389d1b08SAneesh Kumar K.V /** 3463a6b91919SRandy Dunlap * bh_submit_read - Submit a locked buffer for reading 3464389d1b08SAneesh Kumar K.V * @bh: struct buffer_head 3465389d1b08SAneesh Kumar K.V * 3466389d1b08SAneesh Kumar K.V * Returns zero on success and -EIO on error. 3467389d1b08SAneesh Kumar K.V */ 3468389d1b08SAneesh Kumar K.V int bh_submit_read(struct buffer_head *bh) 3469389d1b08SAneesh Kumar K.V { 3470389d1b08SAneesh Kumar K.V BUG_ON(!buffer_locked(bh)); 3471389d1b08SAneesh Kumar K.V 3472389d1b08SAneesh Kumar K.V if (buffer_uptodate(bh)) { 3473389d1b08SAneesh Kumar K.V unlock_buffer(bh); 3474389d1b08SAneesh Kumar K.V return 0; 3475389d1b08SAneesh Kumar K.V } 3476389d1b08SAneesh Kumar K.V 3477389d1b08SAneesh Kumar K.V get_bh(bh); 3478389d1b08SAneesh Kumar K.V bh->b_end_io = end_buffer_read_sync; 34792a222ca9SMike Christie submit_bh(REQ_OP_READ, 0, bh); 3480389d1b08SAneesh Kumar K.V wait_on_buffer(bh); 3481389d1b08SAneesh Kumar K.V if (buffer_uptodate(bh)) 3482389d1b08SAneesh Kumar K.V return 0; 3483389d1b08SAneesh Kumar K.V return -EIO; 3484389d1b08SAneesh Kumar K.V } 3485389d1b08SAneesh Kumar K.V EXPORT_SYMBOL(bh_submit_read); 3486389d1b08SAneesh Kumar K.V 34871da177e4SLinus Torvalds void __init buffer_init(void) 34881da177e4SLinus Torvalds { 348943be594aSZhang Yanfei unsigned long nrpages; 3490fc4d24c9SSebastian Andrzej Siewior int ret; 34911da177e4SLinus Torvalds 3492b98938c3SChristoph Lameter bh_cachep = kmem_cache_create("buffer_head", 3493b98938c3SChristoph Lameter sizeof(struct buffer_head), 0, 3494b98938c3SChristoph Lameter (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| 3495b98938c3SChristoph Lameter SLAB_MEM_SPREAD), 3496019b4d12SRichard Kennedy NULL); 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds /* 34991da177e4SLinus Torvalds * Limit the bh occupancy to 10% of ZONE_NORMAL 35001da177e4SLinus Torvalds */ 35011da177e4SLinus Torvalds nrpages = (nr_free_buffer_pages() * 10) / 100; 35021da177e4SLinus Torvalds max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head)); 3503fc4d24c9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead", 3504fc4d24c9SSebastian Andrzej Siewior NULL, buffer_exit_cpu_dead); 3505fc4d24c9SSebastian Andrzej Siewior WARN_ON(ret < 0); 35061da177e4SLinus Torvalds } 3507