Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/filemap.c |
| 3 | * |
| 4 | * Copyright (C) 1994-1999 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This file handles the generic file mmap semantics used by |
| 9 | * most "normal" filesystems (but you don't /have/ to use this: |
| 10 | * the NFS filesystem used to do this differently, for example) |
| 11 | */ |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/compiler.h> |
| 14 | #include <linux/fs.h> |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/aio.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel_stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/mm.h> |
| 21 | #include <linux/swap.h> |
| 22 | #include <linux/mman.h> |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <linux/file.h> |
| 25 | #include <linux/uio.h> |
| 26 | #include <linux/hash.h> |
| 27 | #include <linux/writeback.h> |
Linus Torvalds | 5325338 | 2007-10-18 14:47:32 -0700 | [diff] [blame] | 28 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/pagevec.h> |
| 30 | #include <linux/blkdev.h> |
| 31 | #include <linux/security.h> |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 32 | #include <linux/cpuset.h> |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 33 | #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 34 | #include <linux/memcontrol.h> |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 35 | #include <linux/cleancache.h> |
Nick Piggin | 0f8053a | 2006-03-22 00:08:33 -0800 | [diff] [blame] | 36 | #include "internal.h" |
| 37 | |
Robert Jarzmik | fe0bfaa | 2013-04-29 15:06:10 -0700 | [diff] [blame] | 38 | #define CREATE_TRACE_POINTS |
| 39 | #include <trace/events/filemap.h> |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * FIXME: remove all knowledge of the buffer layer from the core VM |
| 43 | */ |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 44 | #include <linux/buffer_head.h> /* for try_to_free_buffers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/mman.h> |
| 47 | |
| 48 | /* |
| 49 | * Shared mappings implemented 30.11.1994. It's not fully working yet, |
| 50 | * though. |
| 51 | * |
| 52 | * Shared mappings now work. 15.8.1995 Bruno. |
| 53 | * |
| 54 | * finished 'unifying' the page and buffer cache and SMP-threaded the |
| 55 | * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> |
| 56 | * |
| 57 | * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * Lock ordering: |
| 62 | * |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 63 | * ->i_mmap_mutex (truncate_pagecache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * ->private_lock (__free_pte->__set_page_dirty_buffers) |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 65 | * ->swap_lock (exclusive_swap_page, others) |
| 66 | * ->mapping->tree_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 68 | * ->i_mutex |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 69 | * ->i_mmap_mutex (truncate->unmap_mapping_range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * |
| 71 | * ->mmap_sem |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 72 | * ->i_mmap_mutex |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 73 | * ->page_table_lock or pte_lock (various, mainly in memory.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) |
| 75 | * |
| 76 | * ->mmap_sem |
| 77 | * ->lock_page (access_process_vm) |
| 78 | * |
Nick Piggin | 82591e6 | 2006-10-19 23:29:10 -0700 | [diff] [blame] | 79 | * ->i_mutex (generic_file_buffered_write) |
| 80 | * ->mmap_sem (fault_in_pages_readable->do_page_fault) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 82 | * bdi->wb.list_lock |
Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 83 | * sb_lock (fs/fs-writeback.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * ->mapping->tree_lock (__sync_single_inode) |
| 85 | * |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 86 | * ->i_mmap_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * ->anon_vma.lock (vma_adjust) |
| 88 | * |
| 89 | * ->anon_vma.lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 90 | * ->page_table_lock or pte_lock (anon_vma_prepare and various) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 92 | * ->page_table_lock or pte_lock |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 93 | * ->swap_lock (try_to_unmap_one) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * ->private_lock (try_to_unmap_one) |
| 95 | * ->tree_lock (try_to_unmap_one) |
| 96 | * ->zone.lru_lock (follow_page->mark_page_accessed) |
Nick Piggin | 053837f | 2006-01-18 17:42:27 -0800 | [diff] [blame] | 97 | * ->zone.lru_lock (check_pte_range->isolate_lru_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * ->private_lock (page_remove_rmap->set_page_dirty) |
| 99 | * ->tree_lock (page_remove_rmap->set_page_dirty) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 100 | * bdi.wb->list_lock (page_remove_rmap->set_page_dirty) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 101 | * ->inode->i_lock (page_remove_rmap->set_page_dirty) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 102 | * bdi.wb->list_lock (zap_pte_range->set_page_dirty) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 103 | * ->inode->i_lock (zap_pte_range->set_page_dirty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * ->private_lock (zap_pte_range->__set_page_dirty_buffers) |
| 105 | * |
Andi Kleen | 9a3c531 | 2012-03-21 16:34:09 -0700 | [diff] [blame] | 106 | * ->i_mmap_mutex |
| 107 | * ->tasklist_lock (memory_failure, collect_procs_ao) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | */ |
| 109 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 110 | static void page_cache_tree_delete(struct address_space *mapping, |
| 111 | struct page *page, void *shadow) |
| 112 | { |
| 113 | if (shadow) { |
| 114 | void **slot; |
| 115 | |
| 116 | slot = radix_tree_lookup_slot(&mapping->page_tree, page->index); |
| 117 | radix_tree_replace_slot(slot, shadow); |
| 118 | mapping->nrshadows++; |
| 119 | /* |
| 120 | * Make sure the nrshadows update is committed before |
| 121 | * the nrpages update so that final truncate racing |
| 122 | * with reclaim does not see both counters 0 at the |
| 123 | * same time and miss a shadow entry. |
| 124 | */ |
| 125 | smp_wmb(); |
| 126 | } else |
| 127 | radix_tree_delete(&mapping->page_tree, page->index); |
| 128 | mapping->nrpages--; |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* |
Minchan Kim | e64a782 | 2011-03-22 16:32:44 -0700 | [diff] [blame] | 132 | * Delete a page from the page cache and free it. Caller has to make |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * sure the page is locked and that nobody else uses it - or that usage |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 134 | * is safe. The caller must hold the mapping's tree_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | */ |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 136 | void __delete_from_page_cache(struct page *page, void *shadow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | struct address_space *mapping = page->mapping; |
| 139 | |
Robert Jarzmik | fe0bfaa | 2013-04-29 15:06:10 -0700 | [diff] [blame] | 140 | trace_mm_filemap_delete_from_page_cache(page); |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 141 | /* |
| 142 | * if we're uptodate, flush out into the cleancache, otherwise |
| 143 | * invalidate any existing cleancache entries. We can't leave |
| 144 | * stale data around in the cleancache once our page is gone |
| 145 | */ |
| 146 | if (PageUptodate(page) && PageMappedToDisk(page)) |
| 147 | cleancache_put_page(page); |
| 148 | else |
Dan Magenheimer | 3167760 | 2011-09-21 11:56:28 -0400 | [diff] [blame] | 149 | cleancache_invalidate_page(mapping, page); |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 150 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 151 | page_cache_tree_delete(mapping, page, shadow); |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | page->mapping = NULL; |
Hugh Dickins | b85e0ef | 2011-07-25 17:12:25 -0700 | [diff] [blame] | 154 | /* Leave page->index set: truncation lookup relies upon it */ |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 155 | |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 156 | __dec_zone_page_state(page, NR_FILE_PAGES); |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 157 | if (PageSwapBacked(page)) |
| 158 | __dec_zone_page_state(page, NR_SHMEM); |
Nick Piggin | 4542681 | 2007-07-15 23:38:12 -0700 | [diff] [blame] | 159 | BUG_ON(page_mapped(page)); |
Linus Torvalds | 3a69279 | 2007-12-19 14:05:13 -0800 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Some filesystems seem to re-dirty the page even after |
| 163 | * the VM has canceled the dirty bit (eg ext3 journaling). |
| 164 | * |
| 165 | * Fix it up by doing a final dirty accounting check after |
| 166 | * having removed the page entirely. |
| 167 | */ |
| 168 | if (PageDirty(page) && mapping_cap_account_dirty(mapping)) { |
| 169 | dec_zone_page_state(page, NR_FILE_DIRTY); |
| 170 | dec_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE); |
| 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Minchan Kim | 702cfbf | 2011-03-22 16:32:43 -0700 | [diff] [blame] | 174 | /** |
| 175 | * delete_from_page_cache - delete page from page cache |
| 176 | * @page: the page which the kernel is trying to remove from page cache |
| 177 | * |
| 178 | * This must be called only on pages that have been verified to be in the page |
| 179 | * cache and locked. It will never put the page into the free list, the caller |
| 180 | * has a reference on the page. |
| 181 | */ |
| 182 | void delete_from_page_cache(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct address_space *mapping = page->mapping; |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 185 | void (*freepage)(struct page *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 187 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 189 | freepage = mapping->a_ops->freepage; |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 190 | spin_lock_irq(&mapping->tree_lock); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 191 | __delete_from_page_cache(page, NULL); |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 192 | spin_unlock_irq(&mapping->tree_lock); |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 193 | mem_cgroup_uncharge_cache_page(page); |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 194 | |
| 195 | if (freepage) |
| 196 | freepage(page); |
Minchan Kim | 97cecb5 | 2011-03-22 16:30:53 -0700 | [diff] [blame] | 197 | page_cache_release(page); |
| 198 | } |
| 199 | EXPORT_SYMBOL(delete_from_page_cache); |
| 200 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 201 | static int sleep_on_page(void *word) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | io_schedule(); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 207 | static int sleep_on_page_killable(void *word) |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 208 | { |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 209 | sleep_on_page(word); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 210 | return fatal_signal_pending(current) ? -EINTR : 0; |
| 211 | } |
| 212 | |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 213 | static int filemap_check_errors(struct address_space *mapping) |
| 214 | { |
| 215 | int ret = 0; |
| 216 | /* Check for outstanding write errors */ |
| 217 | if (test_and_clear_bit(AS_ENOSPC, &mapping->flags)) |
| 218 | ret = -ENOSPC; |
| 219 | if (test_and_clear_bit(AS_EIO, &mapping->flags)) |
| 220 | ret = -EIO; |
| 221 | return ret; |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 225 | * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 226 | * @mapping: address space structure to write |
| 227 | * @start: offset in bytes where the range starts |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 228 | * @end: offset in bytes where the range ends (inclusive) |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 229 | * @sync_mode: enable synchronous operation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 231 | * Start writeback against all of a mapping's dirty pages that lie |
| 232 | * within the byte offsets <start, end> inclusive. |
| 233 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 235 | * opposed to a regular memory cleansing writeback. The difference between |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * these two operations is that if a dirty page/buffer is encountered, it must |
| 237 | * be waited upon, and not just skipped over. |
| 238 | */ |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 239 | int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
| 240 | loff_t end, int sync_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | int ret; |
| 243 | struct writeback_control wbc = { |
| 244 | .sync_mode = sync_mode, |
Nick Piggin | 05fe478 | 2009-01-06 14:39:08 -0800 | [diff] [blame] | 245 | .nr_to_write = LONG_MAX, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 246 | .range_start = start, |
| 247 | .range_end = end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | if (!mapping_cap_writeback_dirty(mapping)) |
| 251 | return 0; |
| 252 | |
| 253 | ret = do_writepages(mapping, &wbc); |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static inline int __filemap_fdatawrite(struct address_space *mapping, |
| 258 | int sync_mode) |
| 259 | { |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 260 | return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | int filemap_fdatawrite(struct address_space *mapping) |
| 264 | { |
| 265 | return __filemap_fdatawrite(mapping, WB_SYNC_ALL); |
| 266 | } |
| 267 | EXPORT_SYMBOL(filemap_fdatawrite); |
| 268 | |
Jan Kara | f4c0a0f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 269 | int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 270 | loff_t end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL); |
| 273 | } |
Jan Kara | f4c0a0f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 274 | EXPORT_SYMBOL(filemap_fdatawrite_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 276 | /** |
| 277 | * filemap_flush - mostly a non-blocking flush |
| 278 | * @mapping: target address_space |
| 279 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | * This is a mostly non-blocking flush. Not suitable for data-integrity |
| 281 | * purposes - I/O may not be started against all dirty pages. |
| 282 | */ |
| 283 | int filemap_flush(struct address_space *mapping) |
| 284 | { |
| 285 | return __filemap_fdatawrite(mapping, WB_SYNC_NONE); |
| 286 | } |
| 287 | EXPORT_SYMBOL(filemap_flush); |
| 288 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 289 | /** |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 290 | * filemap_fdatawait_range - wait for writeback to complete |
| 291 | * @mapping: address space structure to wait for |
| 292 | * @start_byte: offset in bytes where the range starts |
| 293 | * @end_byte: offset in bytes where the range ends (inclusive) |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 294 | * |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 295 | * Walk the list of under-writeback pages of the given address space |
| 296 | * in the given range and wait for all of them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | */ |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 298 | int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte, |
| 299 | loff_t end_byte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 301 | pgoff_t index = start_byte >> PAGE_CACHE_SHIFT; |
| 302 | pgoff_t end = end_byte >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | struct pagevec pvec; |
| 304 | int nr_pages; |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 305 | int ret2, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 307 | if (end_byte < start_byte) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 308 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | pagevec_init(&pvec, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | while ((index <= end) && |
| 312 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 313 | PAGECACHE_TAG_WRITEBACK, |
| 314 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) { |
| 315 | unsigned i; |
| 316 | |
| 317 | for (i = 0; i < nr_pages; i++) { |
| 318 | struct page *page = pvec.pages[i]; |
| 319 | |
| 320 | /* until radix tree lookup accepts end_index */ |
| 321 | if (page->index > end) |
| 322 | continue; |
| 323 | |
| 324 | wait_on_page_writeback(page); |
Rik van Riel | 212260a | 2011-01-13 15:46:06 -0800 | [diff] [blame] | 325 | if (TestClearPageError(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | ret = -EIO; |
| 327 | } |
| 328 | pagevec_release(&pvec); |
| 329 | cond_resched(); |
| 330 | } |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 331 | out: |
| 332 | ret2 = filemap_check_errors(mapping); |
| 333 | if (!ret) |
| 334 | ret = ret2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | return ret; |
| 337 | } |
Jan Kara | d3bccb6f | 2009-08-17 19:30:27 +0200 | [diff] [blame] | 338 | EXPORT_SYMBOL(filemap_fdatawait_range); |
| 339 | |
| 340 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 341 | * filemap_fdatawait - wait for all under-writeback pages to complete |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | * @mapping: address space structure to wait for |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 343 | * |
| 344 | * Walk the list of under-writeback pages of the given address space |
| 345 | * and wait for all of them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | */ |
| 347 | int filemap_fdatawait(struct address_space *mapping) |
| 348 | { |
| 349 | loff_t i_size = i_size_read(mapping->host); |
| 350 | |
| 351 | if (i_size == 0) |
| 352 | return 0; |
| 353 | |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 354 | return filemap_fdatawait_range(mapping, 0, i_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | EXPORT_SYMBOL(filemap_fdatawait); |
| 357 | |
| 358 | int filemap_write_and_wait(struct address_space *mapping) |
| 359 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 360 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | if (mapping->nrpages) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 363 | err = filemap_fdatawrite(mapping); |
| 364 | /* |
| 365 | * Even if the above returned error, the pages may be |
| 366 | * written partially (e.g. -ENOSPC), so we wait for it. |
| 367 | * But the -EIO is special case, it may indicate the worst |
| 368 | * thing (e.g. bug) happened, so we avoid waiting for it. |
| 369 | */ |
| 370 | if (err != -EIO) { |
| 371 | int err2 = filemap_fdatawait(mapping); |
| 372 | if (!err) |
| 373 | err = err2; |
| 374 | } |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 375 | } else { |
| 376 | err = filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 378 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 380 | EXPORT_SYMBOL(filemap_write_and_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 382 | /** |
| 383 | * filemap_write_and_wait_range - write out & wait on a file range |
| 384 | * @mapping: the address_space for the pages |
| 385 | * @lstart: offset in bytes where the range starts |
| 386 | * @lend: offset in bytes where the range ends (inclusive) |
| 387 | * |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 388 | * Write out and wait upon file offsets lstart->lend, inclusive. |
| 389 | * |
| 390 | * Note that `lend' is inclusive (describes the last byte to be written) so |
| 391 | * that this function can be used to write to the very end-of-file (end = -1). |
| 392 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | int filemap_write_and_wait_range(struct address_space *mapping, |
| 394 | loff_t lstart, loff_t lend) |
| 395 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 396 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | if (mapping->nrpages) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 399 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 400 | WB_SYNC_ALL); |
| 401 | /* See comment of filemap_write_and_wait() */ |
| 402 | if (err != -EIO) { |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 403 | int err2 = filemap_fdatawait_range(mapping, |
| 404 | lstart, lend); |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 405 | if (!err) |
| 406 | err = err2; |
| 407 | } |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 408 | } else { |
| 409 | err = filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 411 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
Chris Mason | f699558 | 2009-04-15 13:22:37 -0400 | [diff] [blame] | 413 | EXPORT_SYMBOL(filemap_write_and_wait_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 415 | /** |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 416 | * replace_page_cache_page - replace a pagecache page with a new one |
| 417 | * @old: page to be replaced |
| 418 | * @new: page to replace with |
| 419 | * @gfp_mask: allocation mode |
| 420 | * |
| 421 | * This function replaces a page in the pagecache with a new one. On |
| 422 | * success it acquires the pagecache reference for the new page and |
| 423 | * drops it for the old page. Both the old and new pages must be |
| 424 | * locked. This function does not add the new page to the LRU, the |
| 425 | * caller must do that. |
| 426 | * |
| 427 | * The remove + add is atomic. The only way this function can fail is |
| 428 | * memory allocation failure. |
| 429 | */ |
| 430 | int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask) |
| 431 | { |
| 432 | int error; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 433 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 434 | VM_BUG_ON_PAGE(!PageLocked(old), old); |
| 435 | VM_BUG_ON_PAGE(!PageLocked(new), new); |
| 436 | VM_BUG_ON_PAGE(new->mapping, new); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 437 | |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 438 | error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM); |
| 439 | if (!error) { |
| 440 | struct address_space *mapping = old->mapping; |
| 441 | void (*freepage)(struct page *); |
| 442 | |
| 443 | pgoff_t offset = old->index; |
| 444 | freepage = mapping->a_ops->freepage; |
| 445 | |
| 446 | page_cache_get(new); |
| 447 | new->mapping = mapping; |
| 448 | new->index = offset; |
| 449 | |
| 450 | spin_lock_irq(&mapping->tree_lock); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 451 | __delete_from_page_cache(old, NULL); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 452 | error = radix_tree_insert(&mapping->page_tree, offset, new); |
| 453 | BUG_ON(error); |
| 454 | mapping->nrpages++; |
| 455 | __inc_zone_page_state(new, NR_FILE_PAGES); |
| 456 | if (PageSwapBacked(new)) |
| 457 | __inc_zone_page_state(new, NR_SHMEM); |
| 458 | spin_unlock_irq(&mapping->tree_lock); |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 459 | /* mem_cgroup codes must not be called under tree_lock */ |
| 460 | mem_cgroup_replace_page_cache(old, new); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 461 | radix_tree_preload_end(); |
| 462 | if (freepage) |
| 463 | freepage(old); |
| 464 | page_cache_release(old); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return error; |
| 468 | } |
| 469 | EXPORT_SYMBOL_GPL(replace_page_cache_page); |
| 470 | |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 471 | static int page_cache_tree_insert(struct address_space *mapping, |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 472 | struct page *page, void **shadowp) |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 473 | { |
| 474 | void **slot; |
| 475 | int error; |
| 476 | |
| 477 | slot = radix_tree_lookup_slot(&mapping->page_tree, page->index); |
| 478 | if (slot) { |
| 479 | void *p; |
| 480 | |
| 481 | p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 482 | if (!radix_tree_exceptional_entry(p)) |
| 483 | return -EEXIST; |
| 484 | radix_tree_replace_slot(slot, page); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 485 | mapping->nrshadows--; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 486 | mapping->nrpages++; |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 487 | if (shadowp) |
| 488 | *shadowp = p; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | error = radix_tree_insert(&mapping->page_tree, page->index, page); |
| 492 | if (!error) |
| 493 | mapping->nrpages++; |
| 494 | return error; |
| 495 | } |
| 496 | |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 497 | static int __add_to_page_cache_locked(struct page *page, |
| 498 | struct address_space *mapping, |
| 499 | pgoff_t offset, gfp_t gfp_mask, |
| 500 | void **shadowp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 502 | int error; |
| 503 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 504 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 505 | VM_BUG_ON_PAGE(PageSwapBacked(page), page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 506 | |
| 507 | error = mem_cgroup_cache_charge(page, current->mm, |
KAMEZAWA Hiroyuki | 2c26fdd | 2009-01-07 18:08:10 -0800 | [diff] [blame] | 508 | gfp_mask & GFP_RECLAIM_MASK); |
Balbir Singh | 35c754d | 2008-02-07 00:14:05 -0800 | [diff] [blame] | 509 | if (error) |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 510 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 512 | error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM); |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 513 | if (error) { |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 514 | mem_cgroup_uncharge_cache_page(page); |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 515 | return error; |
| 516 | } |
| 517 | |
| 518 | page_cache_get(page); |
| 519 | page->mapping = mapping; |
| 520 | page->index = offset; |
| 521 | |
| 522 | spin_lock_irq(&mapping->tree_lock); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 523 | error = page_cache_tree_insert(mapping, page, shadowp); |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 524 | radix_tree_preload_end(); |
| 525 | if (unlikely(error)) |
| 526 | goto err_insert; |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 527 | __inc_zone_page_state(page, NR_FILE_PAGES); |
| 528 | spin_unlock_irq(&mapping->tree_lock); |
| 529 | trace_mm_filemap_add_to_page_cache(page); |
| 530 | return 0; |
| 531 | err_insert: |
| 532 | page->mapping = NULL; |
| 533 | /* Leave page->index set: truncation relies upon it */ |
| 534 | spin_unlock_irq(&mapping->tree_lock); |
| 535 | mem_cgroup_uncharge_cache_page(page); |
| 536 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return error; |
| 538 | } |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 539 | |
| 540 | /** |
| 541 | * add_to_page_cache_locked - add a locked page to the pagecache |
| 542 | * @page: page to add |
| 543 | * @mapping: the page's address_space |
| 544 | * @offset: page index |
| 545 | * @gfp_mask: page allocation mode |
| 546 | * |
| 547 | * This function is used to add a page to the pagecache. It must be locked. |
| 548 | * This function does not add the page to the LRU. The caller must do that. |
| 549 | */ |
| 550 | int add_to_page_cache_locked(struct page *page, struct address_space *mapping, |
| 551 | pgoff_t offset, gfp_t gfp_mask) |
| 552 | { |
| 553 | return __add_to_page_cache_locked(page, mapping, offset, |
| 554 | gfp_mask, NULL); |
| 555 | } |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 556 | EXPORT_SYMBOL(add_to_page_cache_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 559 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 561 | void *shadow = NULL; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 562 | int ret; |
| 563 | |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 564 | __set_page_locked(page); |
| 565 | ret = __add_to_page_cache_locked(page, mapping, offset, |
| 566 | gfp_mask, &shadow); |
| 567 | if (unlikely(ret)) |
| 568 | __clear_page_locked(page); |
| 569 | else { |
| 570 | /* |
| 571 | * The page might have been evicted from cache only |
| 572 | * recently, in which case it should be activated like |
| 573 | * any other repeatedly accessed page. |
| 574 | */ |
| 575 | if (shadow && workingset_refault(shadow)) { |
| 576 | SetPageActive(page); |
| 577 | workingset_activation(page); |
| 578 | } else |
| 579 | ClearPageActive(page); |
| 580 | lru_cache_add(page); |
| 581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | return ret; |
| 583 | } |
Evgeniy Polyakov | 18bc0bb | 2009-02-09 17:02:42 +0300 | [diff] [blame] | 584 | EXPORT_SYMBOL_GPL(add_to_page_cache_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 586 | #ifdef CONFIG_NUMA |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 587 | struct page *__page_cache_alloc(gfp_t gfp) |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 588 | { |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 589 | int n; |
| 590 | struct page *page; |
| 591 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 592 | if (cpuset_do_page_mem_spread()) { |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 593 | unsigned int cpuset_mems_cookie; |
| 594 | do { |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 595 | cpuset_mems_cookie = read_mems_allowed_begin(); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 596 | n = cpuset_mem_spread_node(); |
| 597 | page = alloc_pages_exact_node(n, gfp, 0); |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 598 | } while (!page && read_mems_allowed_retry(cpuset_mems_cookie)); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 599 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 600 | return page; |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 601 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 602 | return alloc_pages(gfp, 0); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 603 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 604 | EXPORT_SYMBOL(__page_cache_alloc); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 605 | #endif |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* |
| 608 | * In order to wait for pages to become available there must be |
| 609 | * waitqueues associated with pages. By using a hash table of |
| 610 | * waitqueues where the bucket discipline is to maintain all |
| 611 | * waiters on the same queue and wake all when any of the pages |
| 612 | * become available, and for the woken contexts to check to be |
| 613 | * sure the appropriate page became available, this saves space |
| 614 | * at a cost of "thundering herd" phenomena during rare hash |
| 615 | * collisions. |
| 616 | */ |
| 617 | static wait_queue_head_t *page_waitqueue(struct page *page) |
| 618 | { |
| 619 | const struct zone *zone = page_zone(page); |
| 620 | |
| 621 | return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)]; |
| 622 | } |
| 623 | |
| 624 | static inline void wake_up_page(struct page *page, int bit) |
| 625 | { |
| 626 | __wake_up_bit(page_waitqueue(page), &page->flags, bit); |
| 627 | } |
| 628 | |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 629 | void wait_on_page_bit(struct page *page, int bit_nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { |
| 631 | DEFINE_WAIT_BIT(wait, &page->flags, bit_nr); |
| 632 | |
| 633 | if (test_bit(bit_nr, &page->flags)) |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 634 | __wait_on_bit(page_waitqueue(page), &wait, sleep_on_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | TASK_UNINTERRUPTIBLE); |
| 636 | } |
| 637 | EXPORT_SYMBOL(wait_on_page_bit); |
| 638 | |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 639 | int wait_on_page_bit_killable(struct page *page, int bit_nr) |
| 640 | { |
| 641 | DEFINE_WAIT_BIT(wait, &page->flags, bit_nr); |
| 642 | |
| 643 | if (!test_bit(bit_nr, &page->flags)) |
| 644 | return 0; |
| 645 | |
| 646 | return __wait_on_bit(page_waitqueue(page), &wait, |
| 647 | sleep_on_page_killable, TASK_KILLABLE); |
| 648 | } |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | /** |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 651 | * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue |
Randy Dunlap | 697f619 | 2009-04-13 14:39:54 -0700 | [diff] [blame] | 652 | * @page: Page defining the wait queue of interest |
| 653 | * @waiter: Waiter to add to the queue |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 654 | * |
| 655 | * Add an arbitrary @waiter to the wait queue for the nominated @page. |
| 656 | */ |
| 657 | void add_page_wait_queue(struct page *page, wait_queue_t *waiter) |
| 658 | { |
| 659 | wait_queue_head_t *q = page_waitqueue(page); |
| 660 | unsigned long flags; |
| 661 | |
| 662 | spin_lock_irqsave(&q->lock, flags); |
| 663 | __add_wait_queue(q, waiter); |
| 664 | spin_unlock_irqrestore(&q->lock, flags); |
| 665 | } |
| 666 | EXPORT_SYMBOL_GPL(add_page_wait_queue); |
| 667 | |
| 668 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 669 | * unlock_page - unlock a locked page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | * @page: the page |
| 671 | * |
| 672 | * Unlocks the page and wakes up sleepers in ___wait_on_page_locked(). |
| 673 | * Also wakes sleepers in wait_on_page_writeback() because the wakeup |
| 674 | * mechananism between PageLocked pages and PageWriteback pages is shared. |
| 675 | * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. |
| 676 | * |
Nick Piggin | 8413ac9 | 2008-10-18 20:26:59 -0700 | [diff] [blame] | 677 | * The mb is necessary to enforce ordering between the clear_bit and the read |
| 678 | * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | */ |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 680 | void unlock_page(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 682 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Nick Piggin | 8413ac9 | 2008-10-18 20:26:59 -0700 | [diff] [blame] | 683 | clear_bit_unlock(PG_locked, &page->flags); |
| 684 | smp_mb__after_clear_bit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | wake_up_page(page, PG_locked); |
| 686 | } |
| 687 | EXPORT_SYMBOL(unlock_page); |
| 688 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 689 | /** |
| 690 | * end_page_writeback - end writeback against a page |
| 691 | * @page: the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | */ |
| 693 | void end_page_writeback(struct page *page) |
| 694 | { |
Miklos Szeredi | ac6aadb | 2008-04-28 02:12:38 -0700 | [diff] [blame] | 695 | if (TestClearPageReclaim(page)) |
| 696 | rotate_reclaimable_page(page); |
| 697 | |
| 698 | if (!test_clear_page_writeback(page)) |
| 699 | BUG(); |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | smp_mb__after_clear_bit(); |
| 702 | wake_up_page(page, PG_writeback); |
| 703 | } |
| 704 | EXPORT_SYMBOL(end_page_writeback); |
| 705 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 706 | /** |
| 707 | * __lock_page - get a lock on the page, assuming we need to sleep to get it |
| 708 | * @page: the page to lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | */ |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 710 | void __lock_page(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
| 712 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); |
| 713 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 714 | __wait_on_bit_lock(page_waitqueue(page), &wait, sleep_on_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | TASK_UNINTERRUPTIBLE); |
| 716 | } |
| 717 | EXPORT_SYMBOL(__lock_page); |
| 718 | |
Harvey Harrison | b5606c2 | 2008-02-13 15:03:16 -0800 | [diff] [blame] | 719 | int __lock_page_killable(struct page *page) |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 720 | { |
| 721 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); |
| 722 | |
| 723 | return __wait_on_bit_lock(page_waitqueue(page), &wait, |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 724 | sleep_on_page_killable, TASK_KILLABLE); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 725 | } |
Evgeniy Polyakov | 18bc0bb | 2009-02-09 17:02:42 +0300 | [diff] [blame] | 726 | EXPORT_SYMBOL_GPL(__lock_page_killable); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 727 | |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 728 | int __lock_page_or_retry(struct page *page, struct mm_struct *mm, |
| 729 | unsigned int flags) |
| 730 | { |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 731 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
| 732 | /* |
| 733 | * CAUTION! In this case, mmap_sem is not released |
| 734 | * even though return 0. |
| 735 | */ |
| 736 | if (flags & FAULT_FLAG_RETRY_NOWAIT) |
| 737 | return 0; |
| 738 | |
| 739 | up_read(&mm->mmap_sem); |
| 740 | if (flags & FAULT_FLAG_KILLABLE) |
| 741 | wait_on_page_locked_killable(page); |
| 742 | else |
Gleb Natapov | 318b275 | 2011-03-22 16:30:51 -0700 | [diff] [blame] | 743 | wait_on_page_locked(page); |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 744 | return 0; |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 745 | } else { |
| 746 | if (flags & FAULT_FLAG_KILLABLE) { |
| 747 | int ret; |
| 748 | |
| 749 | ret = __lock_page_killable(page); |
| 750 | if (ret) { |
| 751 | up_read(&mm->mmap_sem); |
| 752 | return 0; |
| 753 | } |
| 754 | } else |
| 755 | __lock_page(page); |
| 756 | return 1; |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 760 | /** |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 761 | * page_cache_next_hole - find the next hole (not-present entry) |
| 762 | * @mapping: mapping |
| 763 | * @index: index |
| 764 | * @max_scan: maximum range to search |
| 765 | * |
| 766 | * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the |
| 767 | * lowest indexed hole. |
| 768 | * |
| 769 | * Returns: the index of the hole if found, otherwise returns an index |
| 770 | * outside of the set specified (in which case 'return - index >= |
| 771 | * max_scan' will be true). In rare cases of index wrap-around, 0 will |
| 772 | * be returned. |
| 773 | * |
| 774 | * page_cache_next_hole may be called under rcu_read_lock. However, |
| 775 | * like radix_tree_gang_lookup, this will not atomically search a |
| 776 | * snapshot of the tree at a single point in time. For example, if a |
| 777 | * hole is created at index 5, then subsequently a hole is created at |
| 778 | * index 10, page_cache_next_hole covering both indexes may return 10 |
| 779 | * if called under rcu_read_lock. |
| 780 | */ |
| 781 | pgoff_t page_cache_next_hole(struct address_space *mapping, |
| 782 | pgoff_t index, unsigned long max_scan) |
| 783 | { |
| 784 | unsigned long i; |
| 785 | |
| 786 | for (i = 0; i < max_scan; i++) { |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 787 | struct page *page; |
| 788 | |
| 789 | page = radix_tree_lookup(&mapping->page_tree, index); |
| 790 | if (!page || radix_tree_exceptional_entry(page)) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 791 | break; |
| 792 | index++; |
| 793 | if (index == 0) |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | return index; |
| 798 | } |
| 799 | EXPORT_SYMBOL(page_cache_next_hole); |
| 800 | |
| 801 | /** |
| 802 | * page_cache_prev_hole - find the prev hole (not-present entry) |
| 803 | * @mapping: mapping |
| 804 | * @index: index |
| 805 | * @max_scan: maximum range to search |
| 806 | * |
| 807 | * Search backwards in the range [max(index-max_scan+1, 0), index] for |
| 808 | * the first hole. |
| 809 | * |
| 810 | * Returns: the index of the hole if found, otherwise returns an index |
| 811 | * outside of the set specified (in which case 'index - return >= |
| 812 | * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX |
| 813 | * will be returned. |
| 814 | * |
| 815 | * page_cache_prev_hole may be called under rcu_read_lock. However, |
| 816 | * like radix_tree_gang_lookup, this will not atomically search a |
| 817 | * snapshot of the tree at a single point in time. For example, if a |
| 818 | * hole is created at index 10, then subsequently a hole is created at |
| 819 | * index 5, page_cache_prev_hole covering both indexes may return 5 if |
| 820 | * called under rcu_read_lock. |
| 821 | */ |
| 822 | pgoff_t page_cache_prev_hole(struct address_space *mapping, |
| 823 | pgoff_t index, unsigned long max_scan) |
| 824 | { |
| 825 | unsigned long i; |
| 826 | |
| 827 | for (i = 0; i < max_scan; i++) { |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 828 | struct page *page; |
| 829 | |
| 830 | page = radix_tree_lookup(&mapping->page_tree, index); |
| 831 | if (!page || radix_tree_exceptional_entry(page)) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 832 | break; |
| 833 | index--; |
| 834 | if (index == ULONG_MAX) |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | return index; |
| 839 | } |
| 840 | EXPORT_SYMBOL(page_cache_prev_hole); |
| 841 | |
| 842 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 843 | * find_get_entry - find and get a page cache entry |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 844 | * @mapping: the address_space to search |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 845 | * @offset: the page cache index |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 846 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 847 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 848 | * page cache page, it is returned with an increased refcount. |
| 849 | * |
| 850 | * If the slot holds a shadow entry of a previously evicted page, it |
| 851 | * is returned. |
| 852 | * |
| 853 | * Otherwise, %NULL is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | */ |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 855 | struct page *find_get_entry(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 857 | void **pagep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | struct page *page; |
| 859 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 860 | rcu_read_lock(); |
| 861 | repeat: |
| 862 | page = NULL; |
| 863 | pagep = radix_tree_lookup_slot(&mapping->page_tree, offset); |
| 864 | if (pagep) { |
| 865 | page = radix_tree_deref_slot(pagep); |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 866 | if (unlikely(!page)) |
| 867 | goto out; |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 868 | if (radix_tree_exception(page)) { |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 869 | if (radix_tree_deref_retry(page)) |
| 870 | goto repeat; |
| 871 | /* |
| 872 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 873 | * here as an exceptional entry: so return it without |
| 874 | * attempting to raise page count. |
| 875 | */ |
| 876 | goto out; |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 877 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 878 | if (!page_cache_get_speculative(page)) |
| 879 | goto repeat; |
| 880 | |
| 881 | /* |
| 882 | * Has the page moved? |
| 883 | * This is part of the lockless pagecache protocol. See |
| 884 | * include/linux/pagemap.h for details. |
| 885 | */ |
| 886 | if (unlikely(page != *pagep)) { |
| 887 | page_cache_release(page); |
| 888 | goto repeat; |
| 889 | } |
| 890 | } |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 891 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 892 | rcu_read_unlock(); |
| 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return page; |
| 895 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 896 | EXPORT_SYMBOL(find_get_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 898 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 899 | * find_get_page - find and get a page reference |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 900 | * @mapping: the address_space to search |
| 901 | * @offset: the page index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 903 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 904 | * page cache page, it is returned with an increased refcount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 906 | * Otherwise, %NULL is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | */ |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 908 | struct page *find_get_page(struct address_space *mapping, pgoff_t offset) |
| 909 | { |
| 910 | struct page *page = find_get_entry(mapping, offset); |
| 911 | |
| 912 | if (radix_tree_exceptional_entry(page)) |
| 913 | page = NULL; |
| 914 | return page; |
| 915 | } |
| 916 | EXPORT_SYMBOL(find_get_page); |
| 917 | |
| 918 | /** |
| 919 | * find_lock_entry - locate, pin and lock a page cache entry |
| 920 | * @mapping: the address_space to search |
| 921 | * @offset: the page cache index |
| 922 | * |
| 923 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 924 | * page cache page, it is returned locked and with an increased |
| 925 | * refcount. |
| 926 | * |
| 927 | * If the slot holds a shadow entry of a previously evicted page, it |
| 928 | * is returned. |
| 929 | * |
| 930 | * Otherwise, %NULL is returned. |
| 931 | * |
| 932 | * find_lock_entry() may sleep. |
| 933 | */ |
| 934 | struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
| 936 | struct page *page; |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | repeat: |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 939 | page = find_get_entry(mapping, offset); |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 940 | if (page && !radix_tree_exception(page)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 941 | lock_page(page); |
| 942 | /* Has the page been truncated? */ |
| 943 | if (unlikely(page->mapping != mapping)) { |
| 944 | unlock_page(page); |
| 945 | page_cache_release(page); |
| 946 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 948 | VM_BUG_ON_PAGE(page->index != offset, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return page; |
| 951 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 952 | EXPORT_SYMBOL(find_lock_entry); |
| 953 | |
| 954 | /** |
| 955 | * find_lock_page - locate, pin and lock a pagecache page |
| 956 | * @mapping: the address_space to search |
| 957 | * @offset: the page index |
| 958 | * |
| 959 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 960 | * page cache page, it is returned locked and with an increased |
| 961 | * refcount. |
| 962 | * |
| 963 | * Otherwise, %NULL is returned. |
| 964 | * |
| 965 | * find_lock_page() may sleep. |
| 966 | */ |
| 967 | struct page *find_lock_page(struct address_space *mapping, pgoff_t offset) |
| 968 | { |
| 969 | struct page *page = find_lock_entry(mapping, offset); |
| 970 | |
| 971 | if (radix_tree_exceptional_entry(page)) |
| 972 | page = NULL; |
| 973 | return page; |
| 974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | EXPORT_SYMBOL(find_lock_page); |
| 976 | |
| 977 | /** |
| 978 | * find_or_create_page - locate or add a pagecache page |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 979 | * @mapping: the page's address_space |
| 980 | * @index: the page's index into the mapping |
| 981 | * @gfp_mask: page allocation mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 983 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 984 | * page cache page, it is returned locked and with an increased |
| 985 | * refcount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 987 | * If the page is not present, a new page is allocated using @gfp_mask |
| 988 | * and added to the page cache and the VM's LRU list. The page is |
| 989 | * returned locked and with an increased refcount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 991 | * On memory exhaustion, %NULL is returned. |
| 992 | * |
| 993 | * find_or_create_page() may sleep, even if @gfp_flags specifies an |
| 994 | * atomic allocation! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | */ |
| 996 | struct page *find_or_create_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 997 | pgoff_t index, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 999 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | int err; |
| 1001 | repeat: |
| 1002 | page = find_lock_page(mapping, index); |
| 1003 | if (!page) { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1004 | page = __page_cache_alloc(gfp_mask); |
| 1005 | if (!page) |
| 1006 | return NULL; |
Nick Piggin | 67d58ac | 2009-01-06 14:40:28 -0800 | [diff] [blame] | 1007 | /* |
| 1008 | * We want a regular kernel memory (not highmem or DMA etc) |
| 1009 | * allocation for the radix tree nodes, but we need to honour |
| 1010 | * the context-specific requirements the caller has asked for. |
| 1011 | * GFP_RECLAIM_MASK collects those requirements. |
| 1012 | */ |
| 1013 | err = add_to_page_cache_lru(page, mapping, index, |
| 1014 | (gfp_mask & GFP_RECLAIM_MASK)); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1015 | if (unlikely(err)) { |
| 1016 | page_cache_release(page); |
| 1017 | page = NULL; |
| 1018 | if (err == -EEXIST) |
| 1019 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return page; |
| 1023 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | EXPORT_SYMBOL(find_or_create_page); |
| 1025 | |
| 1026 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1027 | * find_get_entries - gang pagecache lookup |
| 1028 | * @mapping: The address_space to search |
| 1029 | * @start: The starting page cache index |
| 1030 | * @nr_entries: The maximum number of entries |
| 1031 | * @entries: Where the resulting entries are placed |
| 1032 | * @indices: The cache indices corresponding to the entries in @entries |
| 1033 | * |
| 1034 | * find_get_entries() will search for and return a group of up to |
| 1035 | * @nr_entries entries in the mapping. The entries are placed at |
| 1036 | * @entries. find_get_entries() takes a reference against any actual |
| 1037 | * pages it returns. |
| 1038 | * |
| 1039 | * The search returns a group of mapping-contiguous page cache entries |
| 1040 | * with ascending indexes. There may be holes in the indices due to |
| 1041 | * not-present pages. |
| 1042 | * |
| 1043 | * Any shadow entries of evicted pages are included in the returned |
| 1044 | * array. |
| 1045 | * |
| 1046 | * find_get_entries() returns the number of pages and shadow entries |
| 1047 | * which were found. |
| 1048 | */ |
| 1049 | unsigned find_get_entries(struct address_space *mapping, |
| 1050 | pgoff_t start, unsigned int nr_entries, |
| 1051 | struct page **entries, pgoff_t *indices) |
| 1052 | { |
| 1053 | void **slot; |
| 1054 | unsigned int ret = 0; |
| 1055 | struct radix_tree_iter iter; |
| 1056 | |
| 1057 | if (!nr_entries) |
| 1058 | return 0; |
| 1059 | |
| 1060 | rcu_read_lock(); |
| 1061 | restart: |
| 1062 | radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { |
| 1063 | struct page *page; |
| 1064 | repeat: |
| 1065 | page = radix_tree_deref_slot(slot); |
| 1066 | if (unlikely(!page)) |
| 1067 | continue; |
| 1068 | if (radix_tree_exception(page)) { |
| 1069 | if (radix_tree_deref_retry(page)) |
| 1070 | goto restart; |
| 1071 | /* |
| 1072 | * Otherwise, we must be storing a swap entry |
| 1073 | * here as an exceptional entry: so return it |
| 1074 | * without attempting to raise page count. |
| 1075 | */ |
| 1076 | goto export; |
| 1077 | } |
| 1078 | if (!page_cache_get_speculative(page)) |
| 1079 | goto repeat; |
| 1080 | |
| 1081 | /* Has the page moved? */ |
| 1082 | if (unlikely(page != *slot)) { |
| 1083 | page_cache_release(page); |
| 1084 | goto repeat; |
| 1085 | } |
| 1086 | export: |
| 1087 | indices[ret] = iter.index; |
| 1088 | entries[ret] = page; |
| 1089 | if (++ret == nr_entries) |
| 1090 | break; |
| 1091 | } |
| 1092 | rcu_read_unlock(); |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | * find_get_pages - gang pagecache lookup |
| 1098 | * @mapping: The address_space to search |
| 1099 | * @start: The starting page index |
| 1100 | * @nr_pages: The maximum number of pages |
| 1101 | * @pages: Where the resulting pages are placed |
| 1102 | * |
| 1103 | * find_get_pages() will search for and return a group of up to |
| 1104 | * @nr_pages pages in the mapping. The pages are placed at @pages. |
| 1105 | * find_get_pages() takes a reference against the returned pages. |
| 1106 | * |
| 1107 | * The search returns a group of mapping-contiguous pages with ascending |
| 1108 | * indexes. There may be holes in the indices due to not-present pages. |
| 1109 | * |
| 1110 | * find_get_pages() returns the number of pages which were found. |
| 1111 | */ |
| 1112 | unsigned find_get_pages(struct address_space *mapping, pgoff_t start, |
| 1113 | unsigned int nr_pages, struct page **pages) |
| 1114 | { |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1115 | struct radix_tree_iter iter; |
| 1116 | void **slot; |
| 1117 | unsigned ret = 0; |
| 1118 | |
| 1119 | if (unlikely(!nr_pages)) |
| 1120 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1122 | rcu_read_lock(); |
| 1123 | restart: |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1124 | radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1125 | struct page *page; |
| 1126 | repeat: |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1127 | page = radix_tree_deref_slot(slot); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1128 | if (unlikely(!page)) |
| 1129 | continue; |
Hugh Dickins | 9d8aa4e | 2011-03-22 16:33:06 -0700 | [diff] [blame] | 1130 | |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1131 | if (radix_tree_exception(page)) { |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1132 | if (radix_tree_deref_retry(page)) { |
| 1133 | /* |
| 1134 | * Transient condition which can only trigger |
| 1135 | * when entry at index 0 moves out of or back |
| 1136 | * to root: none yet gotten, safe to restart. |
| 1137 | */ |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1138 | WARN_ON(iter.index); |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1139 | goto restart; |
| 1140 | } |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1141 | /* |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1142 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 1143 | * here as an exceptional entry: so skip over it - |
| 1144 | * we only reach this from invalidate_mapping_pages(). |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1145 | */ |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1146 | continue; |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 1147 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1148 | |
| 1149 | if (!page_cache_get_speculative(page)) |
| 1150 | goto repeat; |
| 1151 | |
| 1152 | /* Has the page moved? */ |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1153 | if (unlikely(page != *slot)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1154 | page_cache_release(page); |
| 1155 | goto repeat; |
| 1156 | } |
| 1157 | |
| 1158 | pages[ret] = page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1159 | if (++ret == nr_pages) |
| 1160 | break; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1161 | } |
Hugh Dickins | 5b280c0 | 2011-03-22 16:33:07 -0700 | [diff] [blame] | 1162 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1163 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | return ret; |
| 1165 | } |
| 1166 | |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1167 | /** |
| 1168 | * find_get_pages_contig - gang contiguous pagecache lookup |
| 1169 | * @mapping: The address_space to search |
| 1170 | * @index: The starting page index |
| 1171 | * @nr_pages: The maximum number of pages |
| 1172 | * @pages: Where the resulting pages are placed |
| 1173 | * |
| 1174 | * find_get_pages_contig() works exactly like find_get_pages(), except |
| 1175 | * that the returned number of pages are guaranteed to be contiguous. |
| 1176 | * |
| 1177 | * find_get_pages_contig() returns the number of pages which were found. |
| 1178 | */ |
| 1179 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, |
| 1180 | unsigned int nr_pages, struct page **pages) |
| 1181 | { |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1182 | struct radix_tree_iter iter; |
| 1183 | void **slot; |
| 1184 | unsigned int ret = 0; |
| 1185 | |
| 1186 | if (unlikely(!nr_pages)) |
| 1187 | return 0; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1188 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1189 | rcu_read_lock(); |
| 1190 | restart: |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1191 | radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1192 | struct page *page; |
| 1193 | repeat: |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1194 | page = radix_tree_deref_slot(slot); |
| 1195 | /* The hole, there no reason to continue */ |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1196 | if (unlikely(!page)) |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1197 | break; |
Hugh Dickins | 9d8aa4e | 2011-03-22 16:33:06 -0700 | [diff] [blame] | 1198 | |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1199 | if (radix_tree_exception(page)) { |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1200 | if (radix_tree_deref_retry(page)) { |
| 1201 | /* |
| 1202 | * Transient condition which can only trigger |
| 1203 | * when entry at index 0 moves out of or back |
| 1204 | * to root: none yet gotten, safe to restart. |
| 1205 | */ |
| 1206 | goto restart; |
| 1207 | } |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1208 | /* |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1209 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 1210 | * here as an exceptional entry: so stop looking for |
| 1211 | * contiguous pages. |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1212 | */ |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1213 | break; |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1214 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1215 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1216 | if (!page_cache_get_speculative(page)) |
| 1217 | goto repeat; |
| 1218 | |
| 1219 | /* Has the page moved? */ |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1220 | if (unlikely(page != *slot)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1221 | page_cache_release(page); |
| 1222 | goto repeat; |
| 1223 | } |
| 1224 | |
Nick Piggin | 9cbb4cb | 2011-01-13 15:45:51 -0800 | [diff] [blame] | 1225 | /* |
| 1226 | * must check mapping and index after taking the ref. |
| 1227 | * otherwise we can get both false positives and false |
| 1228 | * negatives, which is just confusing to the caller. |
| 1229 | */ |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1230 | if (page->mapping == NULL || page->index != iter.index) { |
Nick Piggin | 9cbb4cb | 2011-01-13 15:45:51 -0800 | [diff] [blame] | 1231 | page_cache_release(page); |
| 1232 | break; |
| 1233 | } |
| 1234 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1235 | pages[ret] = page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1236 | if (++ret == nr_pages) |
| 1237 | break; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1238 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1239 | rcu_read_unlock(); |
| 1240 | return ret; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1241 | } |
David Howells | ef71c15 | 2007-05-09 02:33:44 -0700 | [diff] [blame] | 1242 | EXPORT_SYMBOL(find_get_pages_contig); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1243 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1244 | /** |
| 1245 | * find_get_pages_tag - find and return pages that match @tag |
| 1246 | * @mapping: the address_space to search |
| 1247 | * @index: the starting page index |
| 1248 | * @tag: the tag index |
| 1249 | * @nr_pages: the maximum number of pages |
| 1250 | * @pages: where the resulting pages are placed |
| 1251 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | * Like find_get_pages, except we only return pages which are tagged with |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1253 | * @tag. We update @index to index the next page for the traversal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | */ |
| 1255 | unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, |
| 1256 | int tag, unsigned int nr_pages, struct page **pages) |
| 1257 | { |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1258 | struct radix_tree_iter iter; |
| 1259 | void **slot; |
| 1260 | unsigned ret = 0; |
| 1261 | |
| 1262 | if (unlikely(!nr_pages)) |
| 1263 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1265 | rcu_read_lock(); |
| 1266 | restart: |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1267 | radix_tree_for_each_tagged(slot, &mapping->page_tree, |
| 1268 | &iter, *index, tag) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1269 | struct page *page; |
| 1270 | repeat: |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1271 | page = radix_tree_deref_slot(slot); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1272 | if (unlikely(!page)) |
| 1273 | continue; |
Hugh Dickins | 9d8aa4e | 2011-03-22 16:33:06 -0700 | [diff] [blame] | 1274 | |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1275 | if (radix_tree_exception(page)) { |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1276 | if (radix_tree_deref_retry(page)) { |
| 1277 | /* |
| 1278 | * Transient condition which can only trigger |
| 1279 | * when entry at index 0 moves out of or back |
| 1280 | * to root: none yet gotten, safe to restart. |
| 1281 | */ |
| 1282 | goto restart; |
| 1283 | } |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1284 | /* |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1285 | * This function is never used on a shmem/tmpfs |
| 1286 | * mapping, so a swap entry won't be found here. |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1287 | */ |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1288 | BUG(); |
Hugh Dickins | a2c16d6 | 2011-08-03 16:21:19 -0700 | [diff] [blame] | 1289 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1290 | |
| 1291 | if (!page_cache_get_speculative(page)) |
| 1292 | goto repeat; |
| 1293 | |
| 1294 | /* Has the page moved? */ |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1295 | if (unlikely(page != *slot)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1296 | page_cache_release(page); |
| 1297 | goto repeat; |
| 1298 | } |
| 1299 | |
| 1300 | pages[ret] = page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1301 | if (++ret == nr_pages) |
| 1302 | break; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1303 | } |
Hugh Dickins | 5b280c0 | 2011-03-22 16:33:07 -0700 | [diff] [blame] | 1304 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1305 | rcu_read_unlock(); |
| 1306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | if (ret) |
| 1308 | *index = pages[ret - 1]->index + 1; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | return ret; |
| 1311 | } |
David Howells | ef71c15 | 2007-05-09 02:33:44 -0700 | [diff] [blame] | 1312 | EXPORT_SYMBOL(find_get_pages_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1314 | /** |
| 1315 | * grab_cache_page_nowait - returns locked page at given index in given cache |
| 1316 | * @mapping: target address_space |
| 1317 | * @index: the page index |
| 1318 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1319 | * Same as grab_cache_page(), but do not wait if the page is unavailable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | * This is intended for speculative data generators, where the data can |
| 1321 | * be regenerated if the page couldn't be grabbed. This routine should |
| 1322 | * be safe to call while holding the lock for another page. |
| 1323 | * |
| 1324 | * Clear __GFP_FS when allocating the page to avoid recursion into the fs |
| 1325 | * and deadlock against the caller's locked page. |
| 1326 | */ |
| 1327 | struct page * |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1328 | grab_cache_page_nowait(struct address_space *mapping, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | { |
| 1330 | struct page *page = find_get_page(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
| 1332 | if (page) { |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 1333 | if (trylock_page(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | return page; |
| 1335 | page_cache_release(page); |
| 1336 | return NULL; |
| 1337 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 1338 | page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS); |
Nick Piggin | 67d58ac | 2009-01-06 14:40:28 -0800 | [diff] [blame] | 1339 | if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | page_cache_release(page); |
| 1341 | page = NULL; |
| 1342 | } |
| 1343 | return page; |
| 1344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | EXPORT_SYMBOL(grab_cache_page_nowait); |
| 1346 | |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1347 | /* |
| 1348 | * CD/DVDs are error prone. When a medium error occurs, the driver may fail |
| 1349 | * a _large_ part of the i/o request. Imagine the worst scenario: |
| 1350 | * |
| 1351 | * ---R__________________________________________B__________ |
| 1352 | * ^ reading here ^ bad block(assume 4k) |
| 1353 | * |
| 1354 | * read(R) => miss => readahead(R...B) => media error => frustrating retries |
| 1355 | * => failing the whole request => read(R) => read(R+1) => |
| 1356 | * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) => |
| 1357 | * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) => |
| 1358 | * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ...... |
| 1359 | * |
| 1360 | * It is going insane. Fix it by quickly scaling down the readahead size. |
| 1361 | */ |
| 1362 | static void shrink_readahead_size_eio(struct file *filp, |
| 1363 | struct file_ra_state *ra) |
| 1364 | { |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1365 | ra->ra_pages /= 4; |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1368 | /** |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 1369 | * do_generic_file_read - generic file read routine |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1370 | * @filp: the file to read |
| 1371 | * @ppos: current file position |
| 1372 | * @desc: read_descriptor |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1373 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | * This is a generic file read routine, and uses the |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1375 | * mapping->a_ops->readpage() function for the actual low-level stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | * |
| 1377 | * This is really ugly. But the goto's actually try to clarify some |
| 1378 | * of the logic when it comes to error handling etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | */ |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 1380 | static void do_generic_file_read(struct file *filp, loff_t *ppos, |
Kirill A. Shutemov | b77d88d | 2013-11-14 14:30:40 -0800 | [diff] [blame] | 1381 | read_descriptor_t *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | { |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 1383 | struct address_space *mapping = filp->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | struct inode *inode = mapping->host; |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 1385 | struct file_ra_state *ra = &filp->f_ra; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1386 | pgoff_t index; |
| 1387 | pgoff_t last_index; |
| 1388 | pgoff_t prev_index; |
| 1389 | unsigned long offset; /* offset into pagecache page */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 1390 | unsigned int prev_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | index = *ppos >> PAGE_CACHE_SHIFT; |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1394 | prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT; |
| 1395 | prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT; |
| 1397 | offset = *ppos & ~PAGE_CACHE_MASK; |
| 1398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | for (;;) { |
| 1400 | struct page *page; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 1401 | pgoff_t end_index; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 1402 | loff_t isize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | unsigned long nr, ret; |
| 1404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | find_page: |
| 1407 | page = find_get_page(mapping, index); |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 1408 | if (!page) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 1409 | page_cache_sync_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1410 | ra, filp, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 1411 | index, last_index - index); |
| 1412 | page = find_get_page(mapping, index); |
| 1413 | if (unlikely(page == NULL)) |
| 1414 | goto no_cached_page; |
| 1415 | } |
| 1416 | if (PageReadahead(page)) { |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 1417 | page_cache_async_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1418 | ra, filp, page, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 1419 | index, last_index - index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 1421 | if (!PageUptodate(page)) { |
| 1422 | if (inode->i_blkbits == PAGE_CACHE_SHIFT || |
| 1423 | !mapping->a_ops->is_partially_uptodate) |
| 1424 | goto page_not_up_to_date; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 1425 | if (!trylock_page(page)) |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 1426 | goto page_not_up_to_date; |
Dave Hansen | 8d056cb | 2010-11-11 14:05:15 -0800 | [diff] [blame] | 1427 | /* Did it get truncated before we got the lock? */ |
| 1428 | if (!page->mapping) |
| 1429 | goto page_not_up_to_date_locked; |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 1430 | if (!mapping->a_ops->is_partially_uptodate(page, |
| 1431 | desc, offset)) |
| 1432 | goto page_not_up_to_date_locked; |
| 1433 | unlock_page(page); |
| 1434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | page_ok: |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 1436 | /* |
| 1437 | * i_size must be checked after we know the page is Uptodate. |
| 1438 | * |
| 1439 | * Checking i_size after the check allows us to calculate |
| 1440 | * the correct value for "nr", which means the zero-filled |
| 1441 | * part of the page is not copied back to userspace (unless |
| 1442 | * another truncate extends the file - this is desired though). |
| 1443 | */ |
| 1444 | |
| 1445 | isize = i_size_read(inode); |
| 1446 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 1447 | if (unlikely(!isize || index > end_index)) { |
| 1448 | page_cache_release(page); |
| 1449 | goto out; |
| 1450 | } |
| 1451 | |
| 1452 | /* nr is the maximum number of bytes to copy from this page */ |
| 1453 | nr = PAGE_CACHE_SIZE; |
| 1454 | if (index == end_index) { |
| 1455 | nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 1456 | if (nr <= offset) { |
| 1457 | page_cache_release(page); |
| 1458 | goto out; |
| 1459 | } |
| 1460 | } |
| 1461 | nr = nr - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
| 1463 | /* If users can be writing to this page using arbitrary |
| 1464 | * virtual addresses, take care about potential aliasing |
| 1465 | * before reading the page on the kernel side. |
| 1466 | */ |
| 1467 | if (mapping_writably_mapped(mapping)) |
| 1468 | flush_dcache_page(page); |
| 1469 | |
| 1470 | /* |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 1471 | * When a sequential read accesses a page several times, |
| 1472 | * only mark it as accessed the first time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 1474 | if (prev_index != index || offset != prev_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | mark_page_accessed(page); |
| 1476 | prev_index = index; |
| 1477 | |
| 1478 | /* |
| 1479 | * Ok, we have the page, and it's up-to-date, so |
| 1480 | * now we can copy it to user space... |
| 1481 | * |
Kirill A. Shutemov | b77d88d | 2013-11-14 14:30:40 -0800 | [diff] [blame] | 1482 | * The file_read_actor routine returns how many bytes were |
| 1483 | * actually used.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | * NOTE! This may not be the same as how much of a user buffer |
| 1485 | * we filled up (we may be padding etc), so we can only update |
| 1486 | * "pos" here (the actor routine has to update the user buffer |
| 1487 | * pointers and the remaining count). |
| 1488 | */ |
Kirill A. Shutemov | b77d88d | 2013-11-14 14:30:40 -0800 | [diff] [blame] | 1489 | ret = file_read_actor(desc, page, offset, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | offset += ret; |
| 1491 | index += offset >> PAGE_CACHE_SHIFT; |
| 1492 | offset &= ~PAGE_CACHE_MASK; |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 1493 | prev_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | |
| 1495 | page_cache_release(page); |
| 1496 | if (ret == nr && desc->count) |
| 1497 | continue; |
| 1498 | goto out; |
| 1499 | |
| 1500 | page_not_up_to_date: |
| 1501 | /* Get exclusive access to the page ... */ |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 1502 | error = lock_page_killable(page); |
| 1503 | if (unlikely(error)) |
| 1504 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 1506 | page_not_up_to_date_locked: |
Nick Piggin | da6052f | 2006-09-25 23:31:35 -0700 | [diff] [blame] | 1507 | /* Did it get truncated before we got the lock? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | if (!page->mapping) { |
| 1509 | unlock_page(page); |
| 1510 | page_cache_release(page); |
| 1511 | continue; |
| 1512 | } |
| 1513 | |
| 1514 | /* Did somebody else fill it already? */ |
| 1515 | if (PageUptodate(page)) { |
| 1516 | unlock_page(page); |
| 1517 | goto page_ok; |
| 1518 | } |
| 1519 | |
| 1520 | readpage: |
Jeff Moyer | 91803b4 | 2010-05-26 11:49:40 -0400 | [diff] [blame] | 1521 | /* |
| 1522 | * A previous I/O error may have been due to temporary |
| 1523 | * failures, eg. multipath errors. |
| 1524 | * PG_error will be set again if readpage fails. |
| 1525 | */ |
| 1526 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | /* Start the actual read. The read will unlock the page. */ |
| 1528 | error = mapping->a_ops->readpage(filp, page); |
| 1529 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1530 | if (unlikely(error)) { |
| 1531 | if (error == AOP_TRUNCATED_PAGE) { |
| 1532 | page_cache_release(page); |
| 1533 | goto find_page; |
| 1534 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | goto readpage_error; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | if (!PageUptodate(page)) { |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 1539 | error = lock_page_killable(page); |
| 1540 | if (unlikely(error)) |
| 1541 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | if (!PageUptodate(page)) { |
| 1543 | if (page->mapping == NULL) { |
| 1544 | /* |
Christoph Hellwig | 2ecdc82 | 2010-01-26 17:27:20 +0100 | [diff] [blame] | 1545 | * invalidate_mapping_pages got it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | */ |
| 1547 | unlock_page(page); |
| 1548 | page_cache_release(page); |
| 1549 | goto find_page; |
| 1550 | } |
| 1551 | unlock_page(page); |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1552 | shrink_readahead_size_eio(filp, ra); |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 1553 | error = -EIO; |
| 1554 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | unlock_page(page); |
| 1557 | } |
| 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | goto page_ok; |
| 1560 | |
| 1561 | readpage_error: |
| 1562 | /* UHHUH! A synchronous read error occurred. Report it */ |
| 1563 | desc->error = error; |
| 1564 | page_cache_release(page); |
| 1565 | goto out; |
| 1566 | |
| 1567 | no_cached_page: |
| 1568 | /* |
| 1569 | * Ok, it wasn't cached, so we need to create a new |
| 1570 | * page.. |
| 1571 | */ |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1572 | page = page_cache_alloc_cold(mapping); |
| 1573 | if (!page) { |
| 1574 | desc->error = -ENOMEM; |
| 1575 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | } |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1577 | error = add_to_page_cache_lru(page, mapping, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | index, GFP_KERNEL); |
| 1579 | if (error) { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1580 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if (error == -EEXIST) |
| 1582 | goto find_page; |
| 1583 | desc->error = error; |
| 1584 | goto out; |
| 1585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | goto readpage; |
| 1587 | } |
| 1588 | |
| 1589 | out: |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 1590 | ra->prev_pos = prev_index; |
| 1591 | ra->prev_pos <<= PAGE_CACHE_SHIFT; |
| 1592 | ra->prev_pos |= prev_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
Fengguang Wu | f4e6b49 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 1594 | *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset; |
Krishna Kumar | 0c6aa26 | 2008-10-15 22:01:13 -0700 | [diff] [blame] | 1595 | file_accessed(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | |
| 1598 | int file_read_actor(read_descriptor_t *desc, struct page *page, |
| 1599 | unsigned long offset, unsigned long size) |
| 1600 | { |
| 1601 | char *kaddr; |
| 1602 | unsigned long left, count = desc->count; |
| 1603 | |
| 1604 | if (size > count) |
| 1605 | size = count; |
| 1606 | |
| 1607 | /* |
| 1608 | * Faults on the destination of a read are common, so do it before |
| 1609 | * taking the kmap. |
| 1610 | */ |
| 1611 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 1612 | kaddr = kmap_atomic(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 1614 | kaddr + offset, size); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 1615 | kunmap_atomic(kaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | if (left == 0) |
| 1617 | goto success; |
| 1618 | } |
| 1619 | |
| 1620 | /* Do it the slow way */ |
| 1621 | kaddr = kmap(page); |
| 1622 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 1623 | kunmap(page); |
| 1624 | |
| 1625 | if (left) { |
| 1626 | size -= left; |
| 1627 | desc->error = -EFAULT; |
| 1628 | } |
| 1629 | success: |
| 1630 | desc->count = count - size; |
| 1631 | desc->written += size; |
| 1632 | desc->arg.buf += size; |
| 1633 | return size; |
| 1634 | } |
| 1635 | |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 1636 | /* |
| 1637 | * Performs necessary checks before doing a write |
| 1638 | * @iov: io vector request |
| 1639 | * @nr_segs: number of segments in the iovec |
| 1640 | * @count: number of bytes to write |
| 1641 | * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE |
| 1642 | * |
| 1643 | * Adjust number of segments and amount of bytes to write (nr_segs should be |
| 1644 | * properly initialized first). Returns appropriate error code that caller |
| 1645 | * should return or zero in case that write should be allowed. |
| 1646 | */ |
| 1647 | int generic_segment_checks(const struct iovec *iov, |
| 1648 | unsigned long *nr_segs, size_t *count, int access_flags) |
| 1649 | { |
| 1650 | unsigned long seg; |
| 1651 | size_t cnt = 0; |
| 1652 | for (seg = 0; seg < *nr_segs; seg++) { |
| 1653 | const struct iovec *iv = &iov[seg]; |
| 1654 | |
| 1655 | /* |
| 1656 | * If any segment has a negative length, or the cumulative |
| 1657 | * length ever wraps negative then return -EINVAL. |
| 1658 | */ |
| 1659 | cnt += iv->iov_len; |
| 1660 | if (unlikely((ssize_t)(cnt|iv->iov_len) < 0)) |
| 1661 | return -EINVAL; |
| 1662 | if (access_ok(access_flags, iv->iov_base, iv->iov_len)) |
| 1663 | continue; |
| 1664 | if (seg == 0) |
| 1665 | return -EFAULT; |
| 1666 | *nr_segs = seg; |
| 1667 | cnt -= iv->iov_len; /* This segment is no good */ |
| 1668 | break; |
| 1669 | } |
| 1670 | *count = cnt; |
| 1671 | return 0; |
| 1672 | } |
| 1673 | EXPORT_SYMBOL(generic_segment_checks); |
| 1674 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1675 | /** |
Henrik Kretzschmar | b2abacf | 2006-10-04 02:15:22 -0700 | [diff] [blame] | 1676 | * generic_file_aio_read - generic filesystem read routine |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1677 | * @iocb: kernel I/O control block |
| 1678 | * @iov: io vector request |
| 1679 | * @nr_segs: number of segments in the iovec |
Henrik Kretzschmar | b2abacf | 2006-10-04 02:15:22 -0700 | [diff] [blame] | 1680 | * @pos: current file position |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1681 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | * This is the "read()" routine for all filesystems |
| 1683 | * that can use the page cache directly. |
| 1684 | */ |
| 1685 | ssize_t |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1686 | generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 1687 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | { |
| 1689 | struct file *filp = iocb->ki_filp; |
| 1690 | ssize_t retval; |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 1691 | unsigned long seg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | size_t count; |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1693 | loff_t *ppos = &iocb->ki_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | |
| 1695 | count = 0; |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 1696 | retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); |
| 1697 | if (retval) |
| 1698 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | |
| 1700 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 1701 | if (filp->f_flags & O_DIRECT) { |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1702 | loff_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | struct address_space *mapping; |
| 1704 | struct inode *inode; |
| 1705 | |
| 1706 | mapping = filp->f_mapping; |
| 1707 | inode = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | if (!count) |
| 1709 | goto out; /* skip atime */ |
| 1710 | size = i_size_read(inode); |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 1711 | retval = filemap_write_and_wait_range(mapping, pos, |
Nick Piggin | 48b47c5 | 2009-01-06 14:40:22 -0800 | [diff] [blame] | 1712 | pos + iov_length(iov, nr_segs) - 1); |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 1713 | if (!retval) { |
| 1714 | retval = mapping->a_ops->direct_IO(READ, iocb, |
| 1715 | iov, pos, nr_segs); |
| 1716 | } |
| 1717 | if (retval > 0) { |
| 1718 | *ppos = pos + retval; |
| 1719 | count -= retval; |
| 1720 | } |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 1721 | |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 1722 | /* |
| 1723 | * Btrfs can have a short DIO read if we encounter |
| 1724 | * compressed extents, so if there was an error, or if |
| 1725 | * we've already read everything we wanted to, or if |
| 1726 | * there was a short read because we hit EOF, go ahead |
| 1727 | * and return. Otherwise fallthrough to buffered io for |
| 1728 | * the rest of the read. |
| 1729 | */ |
| 1730 | if (retval < 0 || !count || *ppos >= size) { |
| 1731 | file_accessed(filp); |
| 1732 | goto out; |
Steven Whitehouse | 0e0bcae | 2006-09-27 14:45:07 -0400 | [diff] [blame] | 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 1736 | count = retval; |
Hugh Dickins | 11fa977e | 2008-07-23 21:27:34 -0700 | [diff] [blame] | 1737 | for (seg = 0; seg < nr_segs; seg++) { |
| 1738 | read_descriptor_t desc; |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 1739 | loff_t offset = 0; |
| 1740 | |
| 1741 | /* |
| 1742 | * If we did a short DIO read we need to skip the section of the |
| 1743 | * iov that we've already read data into. |
| 1744 | */ |
| 1745 | if (count) { |
| 1746 | if (count > iov[seg].iov_len) { |
| 1747 | count -= iov[seg].iov_len; |
| 1748 | continue; |
| 1749 | } |
| 1750 | offset = count; |
| 1751 | count = 0; |
| 1752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
Hugh Dickins | 11fa977e | 2008-07-23 21:27:34 -0700 | [diff] [blame] | 1754 | desc.written = 0; |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 1755 | desc.arg.buf = iov[seg].iov_base + offset; |
| 1756 | desc.count = iov[seg].iov_len - offset; |
Hugh Dickins | 11fa977e | 2008-07-23 21:27:34 -0700 | [diff] [blame] | 1757 | if (desc.count == 0) |
| 1758 | continue; |
| 1759 | desc.error = 0; |
Kirill A. Shutemov | b77d88d | 2013-11-14 14:30:40 -0800 | [diff] [blame] | 1760 | do_generic_file_read(filp, ppos, &desc); |
Hugh Dickins | 11fa977e | 2008-07-23 21:27:34 -0700 | [diff] [blame] | 1761 | retval += desc.written; |
| 1762 | if (desc.error) { |
| 1763 | retval = retval ?: desc.error; |
| 1764 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | } |
Hugh Dickins | 11fa977e | 2008-07-23 21:27:34 -0700 | [diff] [blame] | 1766 | if (desc.count > 0) |
| 1767 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | } |
| 1769 | out: |
| 1770 | return retval; |
| 1771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | EXPORT_SYMBOL(generic_file_aio_read); |
| 1773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | #ifdef CONFIG_MMU |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1775 | /** |
| 1776 | * page_cache_read - adds requested page to the page cache if not already there |
| 1777 | * @file: file to read |
| 1778 | * @offset: page index |
| 1779 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | * This adds the requested page to the page cache if it isn't already there, |
| 1781 | * and schedules an I/O to read in its contents from disk. |
| 1782 | */ |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 1783 | static int page_cache_read(struct file *file, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | { |
| 1785 | struct address_space *mapping = file->f_mapping; |
| 1786 | struct page *page; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1787 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1789 | do { |
| 1790 | page = page_cache_alloc_cold(mapping); |
| 1791 | if (!page) |
| 1792 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1794 | ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL); |
| 1795 | if (ret == 0) |
| 1796 | ret = mapping->a_ops->readpage(file, page); |
| 1797 | else if (ret == -EEXIST) |
| 1798 | ret = 0; /* losing race to add is OK */ |
| 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1802 | } while (ret == AOP_TRUNCATED_PAGE); |
| 1803 | |
| 1804 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | #define MMAP_LOTSAMISS (100) |
| 1808 | |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1809 | /* |
| 1810 | * Synchronous readahead happens when we don't even find |
| 1811 | * a page in the page cache at all. |
| 1812 | */ |
| 1813 | static void do_sync_mmap_readahead(struct vm_area_struct *vma, |
| 1814 | struct file_ra_state *ra, |
| 1815 | struct file *file, |
| 1816 | pgoff_t offset) |
| 1817 | { |
| 1818 | unsigned long ra_pages; |
| 1819 | struct address_space *mapping = file->f_mapping; |
| 1820 | |
| 1821 | /* If we don't want any read-ahead, don't bother */ |
Joe Perches | 64363aa | 2013-07-08 16:00:18 -0700 | [diff] [blame] | 1822 | if (vma->vm_flags & VM_RAND_READ) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1823 | return; |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 1824 | if (!ra->ra_pages) |
| 1825 | return; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1826 | |
Joe Perches | 64363aa | 2013-07-08 16:00:18 -0700 | [diff] [blame] | 1827 | if (vma->vm_flags & VM_SEQ_READ) { |
Wu Fengguang | 7ffc59b | 2009-06-16 15:31:38 -0700 | [diff] [blame] | 1828 | page_cache_sync_readahead(mapping, ra, file, offset, |
| 1829 | ra->ra_pages); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1830 | return; |
| 1831 | } |
| 1832 | |
Andi Kleen | 207d04b | 2011-05-24 17:12:29 -0700 | [diff] [blame] | 1833 | /* Avoid banging the cache line if not needed */ |
| 1834 | if (ra->mmap_miss < MMAP_LOTSAMISS * 10) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1835 | ra->mmap_miss++; |
| 1836 | |
| 1837 | /* |
| 1838 | * Do we miss much more than hit in this file? If so, |
| 1839 | * stop bothering with read-ahead. It will only hurt. |
| 1840 | */ |
| 1841 | if (ra->mmap_miss > MMAP_LOTSAMISS) |
| 1842 | return; |
| 1843 | |
Wu Fengguang | d30a110 | 2009-06-16 15:31:30 -0700 | [diff] [blame] | 1844 | /* |
| 1845 | * mmap read-around |
| 1846 | */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1847 | ra_pages = max_sane_readahead(ra->ra_pages); |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 1848 | ra->start = max_t(long, 0, offset - ra_pages / 2); |
| 1849 | ra->size = ra_pages; |
Wu Fengguang | 2cbea1d | 2011-05-24 17:12:30 -0700 | [diff] [blame] | 1850 | ra->async_size = ra_pages / 4; |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 1851 | ra_submit(ra, mapping, file); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Asynchronous readahead happens when we find the page and PG_readahead, |
| 1856 | * so we want to possibly extend the readahead further.. |
| 1857 | */ |
| 1858 | static void do_async_mmap_readahead(struct vm_area_struct *vma, |
| 1859 | struct file_ra_state *ra, |
| 1860 | struct file *file, |
| 1861 | struct page *page, |
| 1862 | pgoff_t offset) |
| 1863 | { |
| 1864 | struct address_space *mapping = file->f_mapping; |
| 1865 | |
| 1866 | /* If we don't want any read-ahead, don't bother */ |
Joe Perches | 64363aa | 2013-07-08 16:00:18 -0700 | [diff] [blame] | 1867 | if (vma->vm_flags & VM_RAND_READ) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1868 | return; |
| 1869 | if (ra->mmap_miss > 0) |
| 1870 | ra->mmap_miss--; |
| 1871 | if (PageReadahead(page)) |
Wu Fengguang | 2fad6f5 | 2009-06-16 15:31:29 -0700 | [diff] [blame] | 1872 | page_cache_async_readahead(mapping, ra, file, |
| 1873 | page, offset, ra->ra_pages); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1876 | /** |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1877 | * filemap_fault - read in file data for page fault handling |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1878 | * @vma: vma in which the fault was taken |
| 1879 | * @vmf: struct vm_fault containing details of the fault |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1880 | * |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1881 | * filemap_fault() is invoked via the vma operations vector for a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | * mapped memory region to read in file data during a page fault. |
| 1883 | * |
| 1884 | * The goto's are kind of ugly, but this streamlines the normal case of having |
| 1885 | * it in the page cache, and handles the special cases reasonably without |
| 1886 | * having a lot of duplicated code. |
| 1887 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1888 | int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | { |
| 1890 | int error; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1891 | struct file *file = vma->vm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | struct address_space *mapping = file->f_mapping; |
| 1893 | struct file_ra_state *ra = &file->f_ra; |
| 1894 | struct inode *inode = mapping->host; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1895 | pgoff_t offset = vmf->pgoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | struct page *page; |
Jan Kara | 2004dc8 | 2008-02-08 04:20:11 -0800 | [diff] [blame] | 1897 | pgoff_t size; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1898 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1901 | if (offset >= size) |
Linus Torvalds | 5307cc1 | 2007-10-31 09:19:46 -0700 | [diff] [blame] | 1902 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | /* |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 1905 | * Do we have something in the page cache already? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1907 | page = find_get_page(mapping, offset); |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 1908 | if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | /* |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1910 | * We found the page, so try async readahead before |
| 1911 | * waiting for the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1913 | do_async_mmap_readahead(vma, ra, file, page, offset); |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 1914 | } else if (!page) { |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1915 | /* No page in the page cache at all */ |
| 1916 | do_sync_mmap_readahead(vma, ra, file, offset); |
| 1917 | count_vm_event(PGMAJFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1918 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1919 | ret = VM_FAULT_MAJOR; |
| 1920 | retry_find: |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 1921 | page = find_get_page(mapping, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | if (!page) |
| 1923 | goto no_cached_page; |
| 1924 | } |
| 1925 | |
Michel Lespinasse | d88c092 | 2010-11-02 13:05:18 -0700 | [diff] [blame] | 1926 | if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) { |
| 1927 | page_cache_release(page); |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1928 | return ret | VM_FAULT_RETRY; |
Michel Lespinasse | d88c092 | 2010-11-02 13:05:18 -0700 | [diff] [blame] | 1929 | } |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 1930 | |
| 1931 | /* Did it get truncated? */ |
| 1932 | if (unlikely(page->mapping != mapping)) { |
| 1933 | unlock_page(page); |
| 1934 | put_page(page); |
| 1935 | goto retry_find; |
| 1936 | } |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1937 | VM_BUG_ON_PAGE(page->index != offset, page); |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | /* |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1940 | * We have a locked page in the page cache, now we need to check |
| 1941 | * that it's up-to-date. If not, it is going to be due to an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | */ |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1943 | if (unlikely(!PageUptodate(page))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | goto page_not_uptodate; |
| 1945 | |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1946 | /* |
| 1947 | * Found the page and have a reference on it. |
| 1948 | * We must recheck i_size under page lock. |
| 1949 | */ |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1950 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1951 | if (unlikely(offset >= size)) { |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1952 | unlock_page(page); |
Yan Zheng | 745ad48 | 2007-10-08 10:08:37 -0700 | [diff] [blame] | 1953 | page_cache_release(page); |
Linus Torvalds | 5307cc1 | 2007-10-31 09:19:46 -0700 | [diff] [blame] | 1954 | return VM_FAULT_SIGBUS; |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1957 | vmf->page = page; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1958 | return ret | VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | no_cached_page: |
| 1961 | /* |
| 1962 | * We're only likely to ever get here if MADV_RANDOM is in |
| 1963 | * effect. |
| 1964 | */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 1965 | error = page_cache_read(file, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* |
| 1968 | * The page we want has now been added to the page cache. |
| 1969 | * In the unlikely event that someone removed it in the |
| 1970 | * meantime, we'll just come back here and read it again. |
| 1971 | */ |
| 1972 | if (error >= 0) |
| 1973 | goto retry_find; |
| 1974 | |
| 1975 | /* |
| 1976 | * An error return from page_cache_read can result if the |
| 1977 | * system is low on memory, or a problem occurs while trying |
| 1978 | * to schedule I/O. |
| 1979 | */ |
| 1980 | if (error == -ENOMEM) |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1981 | return VM_FAULT_OOM; |
| 1982 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
| 1984 | page_not_uptodate: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | /* |
| 1986 | * Umm, take care of errors if the page isn't up-to-date. |
| 1987 | * Try to re-read it _once_. We do this synchronously, |
| 1988 | * because there really aren't any performance issues here |
| 1989 | * and we need to check for errors. |
| 1990 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | ClearPageError(page); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1992 | error = mapping->a_ops->readpage(file, page); |
Miklos Szeredi | 3ef0f72 | 2008-05-14 16:05:37 -0700 | [diff] [blame] | 1993 | if (!error) { |
| 1994 | wait_on_page_locked(page); |
| 1995 | if (!PageUptodate(page)) |
| 1996 | error = -EIO; |
| 1997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | page_cache_release(page); |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1999 | |
| 2000 | if (!error || error == AOP_TRUNCATED_PAGE) |
| 2001 | goto retry_find; |
| 2002 | |
| 2003 | /* Things didn't work out. Return zero to tell the mm layer so. */ |
| 2004 | shrink_readahead_size_eio(file, ra); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2005 | return VM_FAULT_SIGBUS; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2006 | } |
| 2007 | EXPORT_SYMBOL(filemap_fault); |
| 2008 | |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2009 | int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 2010 | { |
| 2011 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2012 | struct inode *inode = file_inode(vma->vm_file); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2013 | int ret = VM_FAULT_LOCKED; |
| 2014 | |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2015 | sb_start_pagefault(inode->i_sb); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2016 | file_update_time(vma->vm_file); |
| 2017 | lock_page(page); |
| 2018 | if (page->mapping != inode->i_mapping) { |
| 2019 | unlock_page(page); |
| 2020 | ret = VM_FAULT_NOPAGE; |
| 2021 | goto out; |
| 2022 | } |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2023 | /* |
| 2024 | * We mark the page dirty already here so that when freeze is in |
| 2025 | * progress, we are guaranteed that writeback during freezing will |
| 2026 | * see the dirty page and writeprotect it again. |
| 2027 | */ |
| 2028 | set_page_dirty(page); |
Darrick J. Wong | 1d1d1a7 | 2013-02-21 16:42:51 -0800 | [diff] [blame] | 2029 | wait_for_stable_page(page); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2030 | out: |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2031 | sb_end_pagefault(inode->i_sb); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2032 | return ret; |
| 2033 | } |
| 2034 | EXPORT_SYMBOL(filemap_page_mkwrite); |
| 2035 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2036 | const struct vm_operations_struct generic_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2037 | .fault = filemap_fault, |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2038 | .page_mkwrite = filemap_page_mkwrite, |
Konstantin Khlebnikov | 0b173bc | 2012-10-08 16:28:46 -0700 | [diff] [blame] | 2039 | .remap_pages = generic_file_remap_pages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | }; |
| 2041 | |
| 2042 | /* This is used for a general mmap of a disk file */ |
| 2043 | |
| 2044 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 2045 | { |
| 2046 | struct address_space *mapping = file->f_mapping; |
| 2047 | |
| 2048 | if (!mapping->a_ops->readpage) |
| 2049 | return -ENOEXEC; |
| 2050 | file_accessed(file); |
| 2051 | vma->vm_ops = &generic_file_vm_ops; |
| 2052 | return 0; |
| 2053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
| 2055 | /* |
| 2056 | * This is for filesystems which do not implement ->writepage. |
| 2057 | */ |
| 2058 | int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) |
| 2059 | { |
| 2060 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 2061 | return -EINVAL; |
| 2062 | return generic_file_mmap(file, vma); |
| 2063 | } |
| 2064 | #else |
| 2065 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 2066 | { |
| 2067 | return -ENOSYS; |
| 2068 | } |
| 2069 | int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma) |
| 2070 | { |
| 2071 | return -ENOSYS; |
| 2072 | } |
| 2073 | #endif /* CONFIG_MMU */ |
| 2074 | |
| 2075 | EXPORT_SYMBOL(generic_file_mmap); |
| 2076 | EXPORT_SYMBOL(generic_file_readonly_mmap); |
| 2077 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2078 | static struct page *__read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2079 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2080 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2081 | void *data, |
| 2082 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2084 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | int err; |
| 2086 | repeat: |
| 2087 | page = find_get_page(mapping, index); |
| 2088 | if (!page) { |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2089 | page = __page_cache_alloc(gfp | __GFP_COLD); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2090 | if (!page) |
| 2091 | return ERR_PTR(-ENOMEM); |
Dave Kleikamp | e6f67b8 | 2011-12-21 11:05:48 -0600 | [diff] [blame] | 2092 | err = add_to_page_cache_lru(page, mapping, index, gfp); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2093 | if (unlikely(err)) { |
| 2094 | page_cache_release(page); |
| 2095 | if (err == -EEXIST) |
| 2096 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | /* Presumably ENOMEM for radix tree node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | return ERR_PTR(err); |
| 2099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | err = filler(data, page); |
| 2101 | if (err < 0) { |
| 2102 | page_cache_release(page); |
| 2103 | page = ERR_PTR(err); |
| 2104 | } |
| 2105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | return page; |
| 2107 | } |
| 2108 | |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2109 | static struct page *do_read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2110 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2111 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2112 | void *data, |
| 2113 | gfp_t gfp) |
| 2114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | { |
| 2116 | struct page *page; |
| 2117 | int err; |
| 2118 | |
| 2119 | retry: |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2120 | page = __read_cache_page(mapping, index, filler, data, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | if (IS_ERR(page)) |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 2122 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | if (PageUptodate(page)) |
| 2124 | goto out; |
| 2125 | |
| 2126 | lock_page(page); |
| 2127 | if (!page->mapping) { |
| 2128 | unlock_page(page); |
| 2129 | page_cache_release(page); |
| 2130 | goto retry; |
| 2131 | } |
| 2132 | if (PageUptodate(page)) { |
| 2133 | unlock_page(page); |
| 2134 | goto out; |
| 2135 | } |
| 2136 | err = filler(data, page); |
| 2137 | if (err < 0) { |
| 2138 | page_cache_release(page); |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 2139 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 2141 | out: |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2142 | mark_page_accessed(page); |
| 2143 | return page; |
| 2144 | } |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2145 | |
| 2146 | /** |
| 2147 | * read_cache_page_async - read into page cache, fill it if needed |
| 2148 | * @mapping: the page's address_space |
| 2149 | * @index: the page index |
| 2150 | * @filler: function to perform the read |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2151 | * @data: first arg to filler(data, page) function, often left as NULL |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2152 | * |
| 2153 | * Same as read_cache_page, but don't wait for page to become unlocked |
| 2154 | * after submitting it to the filler. |
| 2155 | * |
| 2156 | * Read into the page cache. If a page already exists, and PageUptodate() is |
| 2157 | * not set, try to fill the page but don't wait for it to become unlocked. |
| 2158 | * |
| 2159 | * If the page does not get brought uptodate, return -EIO. |
| 2160 | */ |
| 2161 | struct page *read_cache_page_async(struct address_space *mapping, |
| 2162 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2163 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2164 | void *data) |
| 2165 | { |
| 2166 | return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping)); |
| 2167 | } |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2168 | EXPORT_SYMBOL(read_cache_page_async); |
| 2169 | |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2170 | static struct page *wait_on_page_read(struct page *page) |
| 2171 | { |
| 2172 | if (!IS_ERR(page)) { |
| 2173 | wait_on_page_locked(page); |
| 2174 | if (!PageUptodate(page)) { |
| 2175 | page_cache_release(page); |
| 2176 | page = ERR_PTR(-EIO); |
| 2177 | } |
| 2178 | } |
| 2179 | return page; |
| 2180 | } |
| 2181 | |
| 2182 | /** |
| 2183 | * read_cache_page_gfp - read into page cache, using specified page allocation flags. |
| 2184 | * @mapping: the page's address_space |
| 2185 | * @index: the page index |
| 2186 | * @gfp: the page allocator flags to use if allocating |
| 2187 | * |
| 2188 | * This is the same as "read_mapping_page(mapping, index, NULL)", but with |
Dave Kleikamp | e6f67b8 | 2011-12-21 11:05:48 -0600 | [diff] [blame] | 2189 | * any new page allocations done using the specified allocation flags. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2190 | * |
| 2191 | * If the page does not get brought uptodate, return -EIO. |
| 2192 | */ |
| 2193 | struct page *read_cache_page_gfp(struct address_space *mapping, |
| 2194 | pgoff_t index, |
| 2195 | gfp_t gfp) |
| 2196 | { |
| 2197 | filler_t *filler = (filler_t *)mapping->a_ops->readpage; |
| 2198 | |
| 2199 | return wait_on_page_read(do_read_cache_page(mapping, index, filler, NULL, gfp)); |
| 2200 | } |
| 2201 | EXPORT_SYMBOL(read_cache_page_gfp); |
| 2202 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2203 | /** |
| 2204 | * read_cache_page - read into page cache, fill it if needed |
| 2205 | * @mapping: the page's address_space |
| 2206 | * @index: the page index |
| 2207 | * @filler: function to perform the read |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2208 | * @data: first arg to filler(data, page) function, often left as NULL |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2209 | * |
| 2210 | * Read into the page cache. If a page already exists, and PageUptodate() is |
| 2211 | * not set, try to fill the page then wait for it to become unlocked. |
| 2212 | * |
| 2213 | * If the page does not get brought uptodate, return -EIO. |
| 2214 | */ |
| 2215 | struct page *read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2216 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2217 | int (*filler)(void *, struct page *), |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2218 | void *data) |
| 2219 | { |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2220 | return wait_on_page_read(read_cache_page_async(mapping, index, filler, data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | EXPORT_SYMBOL(read_cache_page); |
| 2223 | |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2224 | static size_t __iovec_copy_from_user_inatomic(char *vaddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | const struct iovec *iov, size_t base, size_t bytes) |
| 2226 | { |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 2227 | size_t copied = 0, left = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
| 2229 | while (bytes) { |
| 2230 | char __user *buf = iov->iov_base + base; |
| 2231 | int copy = min(bytes, iov->iov_len - base); |
| 2232 | |
| 2233 | base = 0; |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 2234 | left = __copy_from_user_inatomic(vaddr, buf, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | copied += copy; |
| 2236 | bytes -= copy; |
| 2237 | vaddr += copy; |
| 2238 | iov++; |
| 2239 | |
NeilBrown | 01408c4 | 2006-06-25 05:47:58 -0700 | [diff] [blame] | 2240 | if (unlikely(left)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | } |
| 2243 | return copied - left; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2247 | * Copy as much as we can into the page and return the number of bytes which |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2248 | * were successfully copied. If a fault is encountered then return the number of |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2249 | * bytes which were copied. |
| 2250 | */ |
| 2251 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
| 2252 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 2253 | { |
| 2254 | char *kaddr; |
| 2255 | size_t copied; |
| 2256 | |
| 2257 | BUG_ON(!in_atomic()); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2258 | kaddr = kmap_atomic(page); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2259 | if (likely(i->nr_segs == 1)) { |
| 2260 | int left; |
| 2261 | char __user *buf = i->iov->iov_base + i->iov_offset; |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 2262 | left = __copy_from_user_inatomic(kaddr + offset, buf, bytes); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2263 | copied = bytes - left; |
| 2264 | } else { |
| 2265 | copied = __iovec_copy_from_user_inatomic(kaddr + offset, |
| 2266 | i->iov, i->iov_offset, bytes); |
| 2267 | } |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2268 | kunmap_atomic(kaddr); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2269 | |
| 2270 | return copied; |
| 2271 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 2272 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2273 | |
| 2274 | /* |
| 2275 | * This has the same sideeffects and return value as |
| 2276 | * iov_iter_copy_from_user_atomic(). |
| 2277 | * The difference is that it attempts to resolve faults. |
| 2278 | * Page must not be locked. |
| 2279 | */ |
| 2280 | size_t iov_iter_copy_from_user(struct page *page, |
| 2281 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 2282 | { |
| 2283 | char *kaddr; |
| 2284 | size_t copied; |
| 2285 | |
| 2286 | kaddr = kmap(page); |
| 2287 | if (likely(i->nr_segs == 1)) { |
| 2288 | int left; |
| 2289 | char __user *buf = i->iov->iov_base + i->iov_offset; |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 2290 | left = __copy_from_user(kaddr + offset, buf, bytes); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2291 | copied = bytes - left; |
| 2292 | } else { |
| 2293 | copied = __iovec_copy_from_user_inatomic(kaddr + offset, |
| 2294 | i->iov, i->iov_offset, bytes); |
| 2295 | } |
| 2296 | kunmap(page); |
| 2297 | return copied; |
| 2298 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 2299 | EXPORT_SYMBOL(iov_iter_copy_from_user); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2300 | |
Nick Piggin | f700926 | 2008-03-10 11:43:59 -0700 | [diff] [blame] | 2301 | void iov_iter_advance(struct iov_iter *i, size_t bytes) |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2302 | { |
Nick Piggin | f700926 | 2008-03-10 11:43:59 -0700 | [diff] [blame] | 2303 | BUG_ON(i->count < bytes); |
| 2304 | |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2305 | if (likely(i->nr_segs == 1)) { |
| 2306 | i->iov_offset += bytes; |
Nick Piggin | f700926 | 2008-03-10 11:43:59 -0700 | [diff] [blame] | 2307 | i->count -= bytes; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2308 | } else { |
| 2309 | const struct iovec *iov = i->iov; |
| 2310 | size_t base = i->iov_offset; |
Jeff Layton | 39be79c | 2011-10-27 23:53:08 +0200 | [diff] [blame] | 2311 | unsigned long nr_segs = i->nr_segs; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2312 | |
Nick Piggin | 124d3b7 | 2008-02-02 15:01:17 +0100 | [diff] [blame] | 2313 | /* |
| 2314 | * The !iov->iov_len check ensures we skip over unlikely |
Nick Piggin | f700926 | 2008-03-10 11:43:59 -0700 | [diff] [blame] | 2315 | * zero-length segments (without overruning the iovec). |
Nick Piggin | 124d3b7 | 2008-02-02 15:01:17 +0100 | [diff] [blame] | 2316 | */ |
Linus Torvalds | 94ad374 | 2008-07-30 14:45:12 -0700 | [diff] [blame] | 2317 | while (bytes || unlikely(i->count && !iov->iov_len)) { |
Nick Piggin | f700926 | 2008-03-10 11:43:59 -0700 | [diff] [blame] | 2318 | int copy; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2319 | |
Nick Piggin | f700926 | 2008-03-10 11:43:59 -0700 | [diff] [blame] | 2320 | copy = min(bytes, iov->iov_len - base); |
| 2321 | BUG_ON(!i->count || i->count < copy); |
| 2322 | i->count -= copy; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2323 | bytes -= copy; |
| 2324 | base += copy; |
| 2325 | if (iov->iov_len == base) { |
| 2326 | iov++; |
Jeff Layton | 39be79c | 2011-10-27 23:53:08 +0200 | [diff] [blame] | 2327 | nr_segs--; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2328 | base = 0; |
| 2329 | } |
| 2330 | } |
| 2331 | i->iov = iov; |
| 2332 | i->iov_offset = base; |
Jeff Layton | 39be79c | 2011-10-27 23:53:08 +0200 | [diff] [blame] | 2333 | i->nr_segs = nr_segs; |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2334 | } |
| 2335 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 2336 | EXPORT_SYMBOL(iov_iter_advance); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2337 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2338 | /* |
| 2339 | * Fault in the first iovec of the given iov_iter, to a maximum length |
| 2340 | * of bytes. Returns 0 on success, or non-zero if the memory could not be |
| 2341 | * accessed (ie. because it is an invalid address). |
| 2342 | * |
| 2343 | * writev-intensive code may want this to prefault several iovecs -- that |
| 2344 | * would be possible (callers must not rely on the fact that _only_ the |
| 2345 | * first iovec will be faulted with the current implementation). |
| 2346 | */ |
| 2347 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2348 | { |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2349 | char __user *buf = i->iov->iov_base + i->iov_offset; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2350 | bytes = min(bytes, i->iov->iov_len - i->iov_offset); |
| 2351 | return fault_in_pages_readable(buf, bytes); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2352 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 2353 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2354 | |
| 2355 | /* |
| 2356 | * Return the count of just the current iov_iter segment. |
| 2357 | */ |
Maxim Patlasov | d28574e | 2012-10-26 19:50:04 +0400 | [diff] [blame] | 2358 | size_t iov_iter_single_seg_count(const struct iov_iter *i) |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2359 | { |
| 2360 | const struct iovec *iov = i->iov; |
| 2361 | if (i->nr_segs == 1) |
| 2362 | return i->count; |
| 2363 | else |
| 2364 | return min(i->count, iov->iov_len - i->iov_offset); |
| 2365 | } |
Nick Piggin | 89e1078 | 2007-10-16 01:25:07 -0700 | [diff] [blame] | 2366 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2367 | |
| 2368 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | * Performs necessary checks before doing a write |
| 2370 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2371 | * Can adjust writing position or amount of bytes to write. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | * Returns appropriate error code that caller should return or |
| 2373 | * zero in case that write should be allowed. |
| 2374 | */ |
| 2375 | inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk) |
| 2376 | { |
| 2377 | struct inode *inode = file->f_mapping->host; |
Jiri Slaby | 59e99e5 | 2010-03-05 13:41:44 -0800 | [diff] [blame] | 2378 | unsigned long limit = rlimit(RLIMIT_FSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | |
| 2380 | if (unlikely(*pos < 0)) |
| 2381 | return -EINVAL; |
| 2382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | if (!isblk) { |
| 2384 | /* FIXME: this is for backwards compatibility with 2.4 */ |
| 2385 | if (file->f_flags & O_APPEND) |
| 2386 | *pos = i_size_read(inode); |
| 2387 | |
| 2388 | if (limit != RLIM_INFINITY) { |
| 2389 | if (*pos >= limit) { |
| 2390 | send_sig(SIGXFSZ, current, 0); |
| 2391 | return -EFBIG; |
| 2392 | } |
| 2393 | if (*count > limit - (typeof(limit))*pos) { |
| 2394 | *count = limit - (typeof(limit))*pos; |
| 2395 | } |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | * LFS rule |
| 2401 | */ |
| 2402 | if (unlikely(*pos + *count > MAX_NON_LFS && |
| 2403 | !(file->f_flags & O_LARGEFILE))) { |
| 2404 | if (*pos >= MAX_NON_LFS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | return -EFBIG; |
| 2406 | } |
| 2407 | if (*count > MAX_NON_LFS - (unsigned long)*pos) { |
| 2408 | *count = MAX_NON_LFS - (unsigned long)*pos; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | /* |
| 2413 | * Are we about to exceed the fs block limit ? |
| 2414 | * |
| 2415 | * If we have written data it becomes a short write. If we have |
| 2416 | * exceeded without writing data we send a signal and return EFBIG. |
| 2417 | * Linus frestrict idea will clean these up nicely.. |
| 2418 | */ |
| 2419 | if (likely(!isblk)) { |
| 2420 | if (unlikely(*pos >= inode->i_sb->s_maxbytes)) { |
| 2421 | if (*count || *pos > inode->i_sb->s_maxbytes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | return -EFBIG; |
| 2423 | } |
| 2424 | /* zero-length writes at ->s_maxbytes are OK */ |
| 2425 | } |
| 2426 | |
| 2427 | if (unlikely(*pos + *count > inode->i_sb->s_maxbytes)) |
| 2428 | *count = inode->i_sb->s_maxbytes - *pos; |
| 2429 | } else { |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 2430 | #ifdef CONFIG_BLOCK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | loff_t isize; |
| 2432 | if (bdev_read_only(I_BDEV(inode))) |
| 2433 | return -EPERM; |
| 2434 | isize = i_size_read(inode); |
| 2435 | if (*pos >= isize) { |
| 2436 | if (*count || *pos > isize) |
| 2437 | return -ENOSPC; |
| 2438 | } |
| 2439 | |
| 2440 | if (*pos + *count > isize) |
| 2441 | *count = isize - *pos; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 2442 | #else |
| 2443 | return -EPERM; |
| 2444 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | } |
| 2446 | return 0; |
| 2447 | } |
| 2448 | EXPORT_SYMBOL(generic_write_checks); |
| 2449 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2450 | int pagecache_write_begin(struct file *file, struct address_space *mapping, |
| 2451 | loff_t pos, unsigned len, unsigned flags, |
| 2452 | struct page **pagep, void **fsdata) |
| 2453 | { |
| 2454 | const struct address_space_operations *aops = mapping->a_ops; |
| 2455 | |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 2456 | return aops->write_begin(file, mapping, pos, len, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2457 | pagep, fsdata); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2458 | } |
| 2459 | EXPORT_SYMBOL(pagecache_write_begin); |
| 2460 | |
| 2461 | int pagecache_write_end(struct file *file, struct address_space *mapping, |
| 2462 | loff_t pos, unsigned len, unsigned copied, |
| 2463 | struct page *page, void *fsdata) |
| 2464 | { |
| 2465 | const struct address_space_operations *aops = mapping->a_ops; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2466 | |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 2467 | mark_page_accessed(page); |
| 2468 | return aops->write_end(file, mapping, pos, len, copied, page, fsdata); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2469 | } |
| 2470 | EXPORT_SYMBOL(pagecache_write_end); |
| 2471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | ssize_t |
| 2473 | generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov, |
| 2474 | unsigned long *nr_segs, loff_t pos, loff_t *ppos, |
| 2475 | size_t count, size_t ocount) |
| 2476 | { |
| 2477 | struct file *file = iocb->ki_filp; |
| 2478 | struct address_space *mapping = file->f_mapping; |
| 2479 | struct inode *inode = mapping->host; |
| 2480 | ssize_t written; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2481 | size_t write_len; |
| 2482 | pgoff_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | |
| 2484 | if (count != ocount) |
| 2485 | *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count); |
| 2486 | |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2487 | write_len = iov_length(iov, *nr_segs); |
| 2488 | end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2489 | |
Nick Piggin | 48b47c5 | 2009-01-06 14:40:22 -0800 | [diff] [blame] | 2490 | written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2491 | if (written) |
| 2492 | goto out; |
| 2493 | |
| 2494 | /* |
| 2495 | * After a write we want buffered reads to be sure to go to disk to get |
| 2496 | * the new data. We invalidate clean cached page from the region we're |
| 2497 | * about to write. We do this *before* the write so that we can return |
Hisashi Hifumi | 6ccfa80 | 2008-09-02 14:35:40 -0700 | [diff] [blame] | 2498 | * without clobbering -EIOCBQUEUED from ->direct_IO(). |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2499 | */ |
| 2500 | if (mapping->nrpages) { |
| 2501 | written = invalidate_inode_pages2_range(mapping, |
| 2502 | pos >> PAGE_CACHE_SHIFT, end); |
Hisashi Hifumi | 6ccfa80 | 2008-09-02 14:35:40 -0700 | [diff] [blame] | 2503 | /* |
| 2504 | * If a page can not be invalidated, return 0 to fall back |
| 2505 | * to buffered write. |
| 2506 | */ |
| 2507 | if (written) { |
| 2508 | if (written == -EBUSY) |
| 2509 | return 0; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2510 | goto out; |
Hisashi Hifumi | 6ccfa80 | 2008-09-02 14:35:40 -0700 | [diff] [blame] | 2511 | } |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs); |
| 2515 | |
| 2516 | /* |
| 2517 | * Finally, try again to invalidate clean pages which might have been |
| 2518 | * cached by non-direct readahead, or faulted in by get_user_pages() |
| 2519 | * if the source of the write was an mmap'ed region of the file |
| 2520 | * we're writing. Either one is a pretty crazy thing to do, |
| 2521 | * so we don't support it 100%. If this invalidation |
| 2522 | * fails, tough, the write still worked... |
| 2523 | */ |
| 2524 | if (mapping->nrpages) { |
| 2525 | invalidate_inode_pages2_range(mapping, |
| 2526 | pos >> PAGE_CACHE_SHIFT, end); |
| 2527 | } |
| 2528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | if (written > 0) { |
Namhyung Kim | 0116651 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 2530 | pos += written; |
| 2531 | if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { |
| 2532 | i_size_write(inode, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | mark_inode_dirty(inode); |
| 2534 | } |
Namhyung Kim | 0116651 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 2535 | *ppos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | } |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 2537 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | return written; |
| 2539 | } |
| 2540 | EXPORT_SYMBOL(generic_file_direct_write); |
| 2541 | |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2542 | /* |
| 2543 | * Find or create a page at the given pagecache position. Return the locked |
| 2544 | * page. This function is specifically for buffered writes. |
| 2545 | */ |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2546 | struct page *grab_cache_page_write_begin(struct address_space *mapping, |
| 2547 | pgoff_t index, unsigned flags) |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2548 | { |
| 2549 | int status; |
Johannes Weiner | 0faa70c | 2012-01-10 15:07:53 -0800 | [diff] [blame] | 2550 | gfp_t gfp_mask; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2551 | struct page *page; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2552 | gfp_t gfp_notmask = 0; |
Johannes Weiner | 0faa70c | 2012-01-10 15:07:53 -0800 | [diff] [blame] | 2553 | |
Fengguang Wu | 1010bb1 | 2012-03-21 16:34:08 -0700 | [diff] [blame] | 2554 | gfp_mask = mapping_gfp_mask(mapping); |
| 2555 | if (mapping_cap_account_dirty(mapping)) |
| 2556 | gfp_mask |= __GFP_WRITE; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2557 | if (flags & AOP_FLAG_NOFS) |
| 2558 | gfp_notmask = __GFP_FS; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2559 | repeat: |
| 2560 | page = find_lock_page(mapping, index); |
Steven Rostedt | c585a26 | 2011-01-13 15:46:18 -0800 | [diff] [blame] | 2561 | if (page) |
Darrick J. Wong | 3d08bcc | 2011-05-27 12:23:34 -0700 | [diff] [blame] | 2562 | goto found; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2563 | |
Johannes Weiner | 0faa70c | 2012-01-10 15:07:53 -0800 | [diff] [blame] | 2564 | page = __page_cache_alloc(gfp_mask & ~gfp_notmask); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2565 | if (!page) |
| 2566 | return NULL; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2567 | status = add_to_page_cache_lru(page, mapping, index, |
| 2568 | GFP_KERNEL & ~gfp_notmask); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2569 | if (unlikely(status)) { |
| 2570 | page_cache_release(page); |
| 2571 | if (status == -EEXIST) |
| 2572 | goto repeat; |
| 2573 | return NULL; |
| 2574 | } |
Darrick J. Wong | 3d08bcc | 2011-05-27 12:23:34 -0700 | [diff] [blame] | 2575 | found: |
Darrick J. Wong | 1d1d1a7 | 2013-02-21 16:42:51 -0800 | [diff] [blame] | 2576 | wait_for_stable_page(page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2577 | return page; |
| 2578 | } |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2579 | EXPORT_SYMBOL(grab_cache_page_write_begin); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2580 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2581 | static ssize_t generic_perform_write(struct file *file, |
| 2582 | struct iov_iter *i, loff_t pos) |
| 2583 | { |
| 2584 | struct address_space *mapping = file->f_mapping; |
| 2585 | const struct address_space_operations *a_ops = mapping->a_ops; |
| 2586 | long status = 0; |
| 2587 | ssize_t written = 0; |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2588 | unsigned int flags = 0; |
| 2589 | |
| 2590 | /* |
| 2591 | * Copies from kernel address space cannot fail (NFSD is a big user). |
| 2592 | */ |
| 2593 | if (segment_eq(get_fs(), KERNEL_DS)) |
| 2594 | flags |= AOP_FLAG_UNINTERRUPTIBLE; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2595 | |
| 2596 | do { |
| 2597 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2598 | unsigned long offset; /* Offset into pagecache page */ |
| 2599 | unsigned long bytes; /* Bytes to write to page */ |
| 2600 | size_t copied; /* Bytes copied from user */ |
| 2601 | void *fsdata; |
| 2602 | |
| 2603 | offset = (pos & (PAGE_CACHE_SIZE - 1)); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2604 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, |
| 2605 | iov_iter_count(i)); |
| 2606 | |
| 2607 | again: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2608 | /* |
| 2609 | * Bring in the user page that we will copy from _first_. |
| 2610 | * Otherwise there's a nasty deadlock on copying from the |
| 2611 | * same page as we're writing to, without it being marked |
| 2612 | * up-to-date. |
| 2613 | * |
| 2614 | * Not only is this an optimisation, but it is also required |
| 2615 | * to check that the address is actually valid, when atomic |
| 2616 | * usercopies are used, below. |
| 2617 | */ |
| 2618 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
| 2619 | status = -EFAULT; |
| 2620 | break; |
| 2621 | } |
| 2622 | |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2623 | status = a_ops->write_begin(file, mapping, pos, bytes, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2624 | &page, &fsdata); |
| 2625 | if (unlikely(status)) |
| 2626 | break; |
| 2627 | |
anfei zhou | 931e80e | 2010-02-02 13:44:02 -0800 | [diff] [blame] | 2628 | if (mapping_writably_mapped(mapping)) |
| 2629 | flush_dcache_page(page); |
| 2630 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2631 | pagefault_disable(); |
| 2632 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
| 2633 | pagefault_enable(); |
| 2634 | flush_dcache_page(page); |
| 2635 | |
Josef Bacik | c8236db | 2009-07-05 12:08:18 -0700 | [diff] [blame] | 2636 | mark_page_accessed(page); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2637 | status = a_ops->write_end(file, mapping, pos, bytes, copied, |
| 2638 | page, fsdata); |
| 2639 | if (unlikely(status < 0)) |
| 2640 | break; |
| 2641 | copied = status; |
| 2642 | |
| 2643 | cond_resched(); |
| 2644 | |
Nick Piggin | 124d3b7 | 2008-02-02 15:01:17 +0100 | [diff] [blame] | 2645 | iov_iter_advance(i, copied); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2646 | if (unlikely(copied == 0)) { |
| 2647 | /* |
| 2648 | * If we were unable to copy any data at all, we must |
| 2649 | * fall back to a single segment length write. |
| 2650 | * |
| 2651 | * If we didn't fallback here, we could livelock |
| 2652 | * because not all segments in the iov can be copied at |
| 2653 | * once without a pagefault. |
| 2654 | */ |
| 2655 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, |
| 2656 | iov_iter_single_seg_count(i)); |
| 2657 | goto again; |
| 2658 | } |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2659 | pos += copied; |
| 2660 | written += copied; |
| 2661 | |
| 2662 | balance_dirty_pages_ratelimited(mapping); |
Jan Kara | a50527b | 2011-12-02 09:17:02 +0800 | [diff] [blame] | 2663 | if (fatal_signal_pending(current)) { |
| 2664 | status = -EINTR; |
| 2665 | break; |
| 2666 | } |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2667 | } while (iov_iter_count(i)); |
| 2668 | |
| 2669 | return written ? written : status; |
| 2670 | } |
| 2671 | |
| 2672 | ssize_t |
| 2673 | generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov, |
| 2674 | unsigned long nr_segs, loff_t pos, loff_t *ppos, |
| 2675 | size_t count, ssize_t written) |
| 2676 | { |
| 2677 | struct file *file = iocb->ki_filp; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2678 | ssize_t status; |
| 2679 | struct iov_iter i; |
| 2680 | |
| 2681 | iov_iter_init(&i, iov, nr_segs, count, written); |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 2682 | status = generic_perform_write(file, &i, pos); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | if (likely(status >= 0)) { |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 2685 | written += status; |
| 2686 | *ppos = pos + status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | return written ? written : status; |
| 2690 | } |
| 2691 | EXPORT_SYMBOL(generic_file_buffered_write); |
| 2692 | |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 2693 | /** |
| 2694 | * __generic_file_aio_write - write data to a file |
| 2695 | * @iocb: IO state structure (file, offset, etc.) |
| 2696 | * @iov: vector with data to write |
| 2697 | * @nr_segs: number of segments in the vector |
| 2698 | * @ppos: position where to write |
| 2699 | * |
| 2700 | * This function does all the work needed for actually writing data to a |
| 2701 | * file. It does all basic checks, removes SUID from the file, updates |
| 2702 | * modification times and calls proper subroutines depending on whether we |
| 2703 | * do direct IO or a standard buffered write. |
| 2704 | * |
| 2705 | * It expects i_mutex to be grabbed unless we work on a block device or similar |
| 2706 | * object which does not need locking at all. |
| 2707 | * |
| 2708 | * This function does *not* take care of syncing data in case of O_SYNC write. |
| 2709 | * A caller has to handle it. This is mainly due to the fact that we want to |
| 2710 | * avoid syncing under i_mutex. |
| 2711 | */ |
| 2712 | ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 2713 | unsigned long nr_segs, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | { |
| 2715 | struct file *file = iocb->ki_filp; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2716 | struct address_space * mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | size_t ocount; /* original count */ |
| 2718 | size_t count; /* after file limit checks */ |
| 2719 | struct inode *inode = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | loff_t pos; |
| 2721 | ssize_t written; |
| 2722 | ssize_t err; |
| 2723 | |
| 2724 | ocount = 0; |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 2725 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
| 2726 | if (err) |
| 2727 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | |
| 2729 | count = ocount; |
| 2730 | pos = *ppos; |
| 2731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | /* We can write back this queue in page reclaim */ |
| 2733 | current->backing_dev_info = mapping->backing_dev_info; |
| 2734 | written = 0; |
| 2735 | |
| 2736 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 2737 | if (err) |
| 2738 | goto out; |
| 2739 | |
| 2740 | if (count == 0) |
| 2741 | goto out; |
| 2742 | |
Miklos Szeredi | 2f1936b | 2008-06-24 16:50:14 +0200 | [diff] [blame] | 2743 | err = file_remove_suid(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | if (err) |
| 2745 | goto out; |
| 2746 | |
Josef Bacik | c3b2da3 | 2012-03-26 09:59:21 -0400 | [diff] [blame] | 2747 | err = file_update_time(file); |
| 2748 | if (err) |
| 2749 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | |
| 2751 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 2752 | if (unlikely(file->f_flags & O_DIRECT)) { |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2753 | loff_t endbyte; |
| 2754 | ssize_t written_buffered; |
| 2755 | |
| 2756 | written = generic_file_direct_write(iocb, iov, &nr_segs, pos, |
| 2757 | ppos, count, ocount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | if (written < 0 || written == count) |
| 2759 | goto out; |
| 2760 | /* |
| 2761 | * direct-io write to a hole: fall through to buffered I/O |
| 2762 | * for completing the rest of the request. |
| 2763 | */ |
| 2764 | pos += written; |
| 2765 | count -= written; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2766 | written_buffered = generic_file_buffered_write(iocb, iov, |
| 2767 | nr_segs, pos, ppos, count, |
| 2768 | written); |
| 2769 | /* |
| 2770 | * If generic_file_buffered_write() retuned a synchronous error |
| 2771 | * then we want to return the number of bytes which were |
| 2772 | * direct-written, or the error code if that was zero. Note |
| 2773 | * that this differs from normal direct-io semantics, which |
| 2774 | * will return -EFOO even if some bytes were written. |
| 2775 | */ |
| 2776 | if (written_buffered < 0) { |
| 2777 | err = written_buffered; |
| 2778 | goto out; |
| 2779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2781 | /* |
| 2782 | * We need to ensure that the page cache pages are written to |
| 2783 | * disk and invalidated to preserve the expected O_DIRECT |
| 2784 | * semantics. |
| 2785 | */ |
| 2786 | endbyte = pos + written_buffered - written - 1; |
Christoph Hellwig | c05c4ed | 2009-09-23 15:07:30 +0200 | [diff] [blame] | 2787 | err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 2788 | if (err == 0) { |
| 2789 | written = written_buffered; |
| 2790 | invalidate_mapping_pages(mapping, |
| 2791 | pos >> PAGE_CACHE_SHIFT, |
| 2792 | endbyte >> PAGE_CACHE_SHIFT); |
| 2793 | } else { |
| 2794 | /* |
| 2795 | * We don't know how much we wrote, so just return |
| 2796 | * the number of bytes which were direct-written |
| 2797 | */ |
| 2798 | } |
| 2799 | } else { |
| 2800 | written = generic_file_buffered_write(iocb, iov, nr_segs, |
| 2801 | pos, ppos, count, written); |
| 2802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | out: |
| 2804 | current->backing_dev_info = NULL; |
| 2805 | return written ? written : err; |
| 2806 | } |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 2807 | EXPORT_SYMBOL(__generic_file_aio_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 2809 | /** |
| 2810 | * generic_file_aio_write - write data to a file |
| 2811 | * @iocb: IO state structure |
| 2812 | * @iov: vector with data to write |
| 2813 | * @nr_segs: number of segments in the vector |
| 2814 | * @pos: position in file where to write |
| 2815 | * |
| 2816 | * This is a wrapper around __generic_file_aio_write() to be used by most |
| 2817 | * filesystems. It takes care of syncing the file in case of O_SYNC file |
| 2818 | * and acquires i_mutex as needed. |
| 2819 | */ |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2820 | ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 2821 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | { |
| 2823 | struct file *file = iocb->ki_filp; |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 2824 | struct inode *inode = file->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
| 2827 | BUG_ON(iocb->ki_pos != pos); |
| 2828 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2829 | mutex_lock(&inode->i_mutex); |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 2830 | ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2831 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
Christoph Hellwig | 02afc27 | 2013-09-04 15:04:40 +0200 | [diff] [blame] | 2833 | if (ret > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | ssize_t err; |
| 2835 | |
Al Viro | d311d79 | 2014-02-09 15:18:09 -0500 | [diff] [blame] | 2836 | err = generic_write_sync(file, iocb->ki_pos - ret, ret); |
| 2837 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | ret = err; |
| 2839 | } |
| 2840 | return ret; |
| 2841 | } |
| 2842 | EXPORT_SYMBOL(generic_file_aio_write); |
| 2843 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 2844 | /** |
| 2845 | * try_to_release_page() - release old fs-specific metadata on a page |
| 2846 | * |
| 2847 | * @page: the page which the kernel is trying to free |
| 2848 | * @gfp_mask: memory allocation flags (and I/O mode) |
| 2849 | * |
| 2850 | * The address_space is to try to release any data against the page |
| 2851 | * (presumably at page->private). If the release was successful, return `1'. |
| 2852 | * Otherwise return zero. |
| 2853 | * |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 2854 | * This may also be called if PG_fscache is set on a page, indicating that the |
| 2855 | * page is known to the local caching routines. |
| 2856 | * |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 2857 | * The @gfp_mask argument specifies whether I/O may be performed to release |
Mingming Cao | 3f31fdd | 2008-07-25 01:46:22 -0700 | [diff] [blame] | 2858 | * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS). |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 2859 | * |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 2860 | */ |
| 2861 | int try_to_release_page(struct page *page, gfp_t gfp_mask) |
| 2862 | { |
| 2863 | struct address_space * const mapping = page->mapping; |
| 2864 | |
| 2865 | BUG_ON(!PageLocked(page)); |
| 2866 | if (PageWriteback(page)) |
| 2867 | return 0; |
| 2868 | |
| 2869 | if (mapping && mapping->a_ops->releasepage) |
| 2870 | return mapping->a_ops->releasepage(page, gfp_mask); |
| 2871 | return try_to_free_buffers(page); |
| 2872 | } |
| 2873 | |
| 2874 | EXPORT_SYMBOL(try_to_release_page); |