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