blob: ecf7565ff435778788a7f4b89dea8865f7323700 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080014#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070017#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/mman.h>
24#include <linux/pagemap.h>
25#include <linux/file.h>
26#include <linux/uio.h>
27#include <linux/hash.h>
28#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070029#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/pagevec.h>
31#include <linux/blkdev.h>
32#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080033#include <linux/cpuset.h>
Nick Piggin2f718ff2007-10-16 01:24:59 -070034#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
Johannes Weiner00501b52014-08-08 14:19:20 -070035#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080036#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060037#include <linux/cleancache.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070038#include <linux/rmap.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080039#include "internal.h"
40
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070041#define CREATE_TRACE_POINTS
42#include <trace/events/filemap.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * FIXME: remove all knowledge of the buffer layer from the core VM
46 */
Jan Kara148f9482009-08-17 19:52:36 +020047#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/mman.h>
50
51/*
52 * Shared mappings implemented 30.11.1994. It's not fully working yet,
53 * though.
54 *
55 * Shared mappings now work. 15.8.1995 Bruno.
56 *
57 * finished 'unifying' the page and buffer cache and SMP-threaded the
58 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
59 *
60 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
61 */
62
63/*
64 * Lock ordering:
65 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080066 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070068 * ->swap_lock (exclusive_swap_page, others)
69 * ->mapping->tree_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080071 * ->i_mutex
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080072 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * ->mmap_sem
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080075 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070076 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
78 *
79 * ->mmap_sem
80 * ->lock_page (access_process_vm)
81 *
Al Viroccad2362014-02-11 22:36:48 -050082 * ->i_mutex (generic_perform_write)
Nick Piggin82591e62006-10-19 23:29:10 -070083 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060085 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110086 * sb_lock (fs/fs-writeback.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * ->mapping->tree_lock (__sync_single_inode)
88 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080089 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * ->anon_vma.lock (vma_adjust)
91 *
92 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -070093 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -070095 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -070096 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * ->private_lock (try_to_unmap_one)
98 * ->tree_lock (try_to_unmap_one)
Mel Gormana52633d2016-07-28 15:45:28 -070099 * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
100 * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * ->private_lock (page_remove_rmap->set_page_dirty)
102 * ->tree_lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600103 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100104 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700105 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600106 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100107 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
109 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800110 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700111 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700114static int page_cache_tree_insert(struct address_space *mapping,
115 struct page *page, void **shadowp)
116{
117 struct radix_tree_node *node;
118 void **slot;
119 int error;
120
121 error = __radix_tree_create(&mapping->page_tree, page->index, 0,
122 &node, &slot);
123 if (error)
124 return error;
125 if (*slot) {
126 void *p;
127
128 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
129 if (!radix_tree_exceptional_entry(p))
130 return -EEXIST;
131
132 mapping->nrexceptional--;
Ross Zwislerd01ad192017-09-06 16:18:47 -0700133 if (shadowp)
134 *shadowp = p;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700135 }
Johannes Weiner14b46872016-12-12 16:43:52 -0800136 __radix_tree_replace(&mapping->page_tree, node, slot, page,
137 workingset_update_node, mapping);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700138 mapping->nrpages++;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700139 return 0;
140}
141
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700142static void page_cache_tree_delete(struct address_space *mapping,
143 struct page *page, void *shadow)
144{
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800145 int i, nr;
146
147 /* hugetlb pages are represented by one entry in the radix tree */
148 nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700149
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700150 VM_BUG_ON_PAGE(!PageLocked(page), page);
151 VM_BUG_ON_PAGE(PageTail(page), page);
152 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700153
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700154 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200155 struct radix_tree_node *node;
156 void **slot;
157
158 __radix_tree_lookup(&mapping->page_tree, page->index + i,
159 &node, &slot);
160
Johannes Weinerdbc446b2016-12-12 16:43:55 -0800161 VM_BUG_ON_PAGE(!node && nr != 1, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700162
Johannes Weiner14b46872016-12-12 16:43:52 -0800163 radix_tree_clear_tags(&mapping->page_tree, node, slot);
164 __radix_tree_replace(&mapping->page_tree, node, slot, shadow,
165 workingset_update_node, mapping);
Johannes Weiner449dd692014-04-03 14:47:56 -0700166 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200167
168 if (shadow) {
169 mapping->nrexceptional += nr;
170 /*
171 * Make sure the nrexceptional update is committed before
172 * the nrpages update so that final truncate racing
173 * with reclaim does not see both counters 0 at the
174 * same time and miss a shadow entry.
175 */
176 smp_wmb();
177 }
178 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
Minchan Kime64a7822011-03-22 16:32:44 -0700182 * Delete a page from the page cache and free it. Caller has to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * sure the page is locked and that nobody else uses it - or that usage
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700184 * is safe. The caller must hold the mapping's tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Johannes Weiner62cccb82016-03-15 14:57:22 -0700186void __delete_from_page_cache(struct page *page, void *shadow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct address_space *mapping = page->mapping;
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700189 int nr = hpage_nr_pages(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -0700191 trace_mm_filemap_delete_from_page_cache(page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600192 /*
193 * if we're uptodate, flush out into the cleancache, otherwise
194 * invalidate any existing cleancache entries. We can't leave
195 * stale data around in the cleancache once our page is gone
196 */
197 if (PageUptodate(page) && PageMappedToDisk(page))
198 cleancache_put_page(page);
199 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400200 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600201
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700202 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800203 VM_BUG_ON_PAGE(page_mapped(page), page);
204 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
205 int mapcount;
206
207 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
208 current->comm, page_to_pfn(page));
209 dump_page(page, "still mapped when deleted");
210 dump_stack();
211 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
212
213 mapcount = page_mapcount(page);
214 if (mapping_exiting(mapping) &&
215 page_count(page) >= mapcount + 2) {
216 /*
217 * All vmas have already been torn down, so it's
218 * a good bet that actually the page is unmapped,
219 * and we'd prefer not to leak it: if we're wrong,
220 * some other bad page check should catch it later.
221 */
222 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700223 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800224 }
225 }
226
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700227 page_cache_tree_delete(mapping, page, shadow);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 page->mapping = NULL;
Hugh Dickinsb85e0ef2011-07-25 17:12:25 -0700230 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700231
Michal Hocko4165b9b2015-06-24 16:57:24 -0700232 /* hugetlb pages do not participate in page cache accounting. */
Naoya Horiguchi09612fa2017-07-10 15:47:35 -0700233 if (PageHuge(page))
234 return;
235
236 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700237 if (PageSwapBacked(page)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700238 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700239 if (PageTransHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700240 __dec_node_page_state(page, NR_SHMEM_THPS);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700241 } else {
Naoya Horiguchi09612fa2017-07-10 15:47:35 -0700242 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700243 }
Linus Torvalds3a692792007-12-19 14:05:13 -0800244
245 /*
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700246 * At this point page must be either written or cleaned by truncate.
247 * Dirty page here signals a bug and loss of unwritten data.
Linus Torvalds3a692792007-12-19 14:05:13 -0800248 *
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700249 * This fixes dirty accounting after removing the page entirely but
250 * leaves PageDirty set: it has no effect for truncated page and
251 * anyway will be cleared before returning page into buddy allocator.
Linus Torvalds3a692792007-12-19 14:05:13 -0800252 */
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700253 if (WARN_ON_ONCE(PageDirty(page)))
Johannes Weiner62cccb82016-03-15 14:57:22 -0700254 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Jan Kara59c66c52017-11-15 17:37:18 -0800257static void page_cache_free_page(struct address_space *mapping,
258 struct page *page)
259{
260 void (*freepage)(struct page *);
261
262 freepage = mapping->a_ops->freepage;
263 if (freepage)
264 freepage(page);
265
266 if (PageTransHuge(page) && !PageHuge(page)) {
267 page_ref_sub(page, HPAGE_PMD_NR);
268 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
269 } else {
270 put_page(page);
271 }
272}
273
Minchan Kim702cfbf2011-03-22 16:32:43 -0700274/**
275 * delete_from_page_cache - delete page from page cache
276 * @page: the page which the kernel is trying to remove from page cache
277 *
278 * This must be called only on pages that have been verified to be in the page
279 * cache and locked. It will never put the page into the free list, the caller
280 * has a reference on the page.
281 */
282void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700284 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400285 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Matt Mackallcd7619d2005-05-01 08:59:01 -0700287 BUG_ON(!PageLocked(page));
Greg Thelenc4843a72015-05-22 17:13:16 -0400288 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700289 __delete_from_page_cache(page, NULL);
Greg Thelenc4843a72015-05-22 17:13:16 -0400290 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500291
Jan Kara59c66c52017-11-15 17:37:18 -0800292 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700293}
294EXPORT_SYMBOL(delete_from_page_cache);
295
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200296int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700297{
298 int ret = 0;
299 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700300 if (test_bit(AS_ENOSPC, &mapping->flags) &&
301 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700302 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700303 if (test_bit(AS_EIO, &mapping->flags) &&
304 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700305 ret = -EIO;
306 return ret;
307}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200308EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700309
Jeff Layton76341ca2017-07-06 07:02:22 -0400310static int filemap_check_and_keep_errors(struct address_space *mapping)
311{
312 /* Check for outstanding write errors */
313 if (test_bit(AS_EIO, &mapping->flags))
314 return -EIO;
315 if (test_bit(AS_ENOSPC, &mapping->flags))
316 return -ENOSPC;
317 return 0;
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700321 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700322 * @mapping: address space structure to write
323 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800324 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700325 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700327 * Start writeback against all of a mapping's dirty pages that lie
328 * within the byte offsets <start, end> inclusive.
329 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700331 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * these two operations is that if a dirty page/buffer is encountered, it must
333 * be waited upon, and not just skipped over.
334 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800335int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
336 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 int ret;
339 struct writeback_control wbc = {
340 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800341 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700342 .range_start = start,
343 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 };
345
346 if (!mapping_cap_writeback_dirty(mapping))
347 return 0;
348
Tejun Heob16b1de2015-06-02 08:39:48 -0600349 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600351 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
353}
354
355static inline int __filemap_fdatawrite(struct address_space *mapping,
356 int sync_mode)
357{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700358 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361int filemap_fdatawrite(struct address_space *mapping)
362{
363 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
364}
365EXPORT_SYMBOL(filemap_fdatawrite);
366
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400367int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800368 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
371}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400372EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Randy Dunlap485bb992006-06-23 02:03:49 -0700374/**
375 * filemap_flush - mostly a non-blocking flush
376 * @mapping: target address_space
377 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * This is a mostly non-blocking flush. Not suitable for data-integrity
379 * purposes - I/O may not be started against all dirty pages.
380 */
381int filemap_flush(struct address_space *mapping)
382{
383 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
384}
385EXPORT_SYMBOL(filemap_flush);
386
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500387/**
388 * filemap_range_has_page - check if a page exists in range.
389 * @mapping: address space within which to check
390 * @start_byte: offset in bytes where the range starts
391 * @end_byte: offset in bytes where the range ends (inclusive)
392 *
393 * Find at least one page in the range supplied, usually used to check if
394 * direct writing in this range will trigger a writeback.
395 */
396bool filemap_range_has_page(struct address_space *mapping,
397 loff_t start_byte, loff_t end_byte)
398{
399 pgoff_t index = start_byte >> PAGE_SHIFT;
400 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700401 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500402
403 if (end_byte < start_byte)
404 return false;
405
406 if (mapping->nrpages == 0)
407 return false;
408
Jan Karaf7b68042017-09-06 16:21:40 -0700409 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500410 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700411 put_page(page);
412 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500413}
414EXPORT_SYMBOL(filemap_range_has_page);
415
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400416static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800417 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300419 pgoff_t index = start_byte >> PAGE_SHIFT;
420 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct pagevec pvec;
422 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200424 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400425 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 pagevec_init(&pvec, 0);
Jan Kara312e9d22017-11-15 17:35:05 -0800428 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned i;
430
Jan Kara312e9d22017-11-15 17:35:05 -0800431 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800432 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800433 if (!nr_pages)
434 break;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 for (i = 0; i < nr_pages; i++) {
437 struct page *page = pvec.pages[i];
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400440 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 pagevec_release(&pvec);
443 cond_resched();
444 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800445}
446
447/**
448 * filemap_fdatawait_range - wait for writeback to complete
449 * @mapping: address space structure to wait for
450 * @start_byte: offset in bytes where the range starts
451 * @end_byte: offset in bytes where the range ends (inclusive)
452 *
453 * Walk the list of under-writeback pages of the given address space
454 * in the given range and wait for all of them. Check error status of
455 * the address space and return it.
456 *
457 * Since the error status of the address space is cleared by this function,
458 * callers are responsible for checking the return value and handling and/or
459 * reporting the error.
460 */
461int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
462 loff_t end_byte)
463{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400464 __filemap_fdatawait_range(mapping, start_byte, end_byte);
465 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200467EXPORT_SYMBOL(filemap_fdatawait_range);
468
469/**
Jeff Laytona823e452017-07-28 07:24:43 -0400470 * file_fdatawait_range - wait for writeback to complete
471 * @file: file pointing to address space structure to wait for
472 * @start_byte: offset in bytes where the range starts
473 * @end_byte: offset in bytes where the range ends (inclusive)
474 *
475 * Walk the list of under-writeback pages of the address space that file
476 * refers to, in the given range and wait for all of them. Check error
477 * status of the address space vs. the file->f_wb_err cursor and return it.
478 *
479 * Since the error status of the file is advanced by this function,
480 * callers are responsible for checking the return value and handling and/or
481 * reporting the error.
482 */
483int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
484{
485 struct address_space *mapping = file->f_mapping;
486
487 __filemap_fdatawait_range(mapping, start_byte, end_byte);
488 return file_check_and_advance_wb_err(file);
489}
490EXPORT_SYMBOL(file_fdatawait_range);
491
492/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800493 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
494 * @mapping: address space structure to wait for
495 *
496 * Walk the list of under-writeback pages of the given address space
497 * and wait for all of them. Unlike filemap_fdatawait(), this function
498 * does not clear error status of the address space.
499 *
500 * Use this function if callers don't handle errors themselves. Expected
501 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
502 * fsfreeze(8)
503 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400504int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800505{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400506 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400507 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800508}
Jeff Layton76341ca2017-07-06 07:02:22 -0400509EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800510
Jeff Layton9326c9b2017-07-26 10:21:11 -0400511static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400513 return (!dax_mapping(mapping) && mapping->nrpages) ||
514 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517int filemap_write_and_wait(struct address_space *mapping)
518{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800519 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jeff Layton9326c9b2017-07-26 10:21:11 -0400521 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800522 err = filemap_fdatawrite(mapping);
523 /*
524 * Even if the above returned error, the pages may be
525 * written partially (e.g. -ENOSPC), so we wait for it.
526 * But the -EIO is special case, it may indicate the worst
527 * thing (e.g. bug) happened, so we avoid waiting for it.
528 */
529 if (err != -EIO) {
530 int err2 = filemap_fdatawait(mapping);
531 if (!err)
532 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400533 } else {
534 /* Clear any previously stored errors */
535 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800536 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700537 } else {
538 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800542EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Randy Dunlap485bb992006-06-23 02:03:49 -0700544/**
545 * filemap_write_and_wait_range - write out & wait on a file range
546 * @mapping: the address_space for the pages
547 * @lstart: offset in bytes where the range starts
548 * @lend: offset in bytes where the range ends (inclusive)
549 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800550 * Write out and wait upon file offsets lstart->lend, inclusive.
551 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300552 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800553 * that this function can be used to write to the very end-of-file (end = -1).
554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555int filemap_write_and_wait_range(struct address_space *mapping,
556 loff_t lstart, loff_t lend)
557{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800558 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jeff Layton9326c9b2017-07-26 10:21:11 -0400560 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800561 err = __filemap_fdatawrite_range(mapping, lstart, lend,
562 WB_SYNC_ALL);
563 /* See comment of filemap_write_and_wait() */
564 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200565 int err2 = filemap_fdatawait_range(mapping,
566 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800567 if (!err)
568 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400569 } else {
570 /* Clear any previously stored errors */
571 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800572 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700573 } else {
574 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Chris Masonf6995582009-04-15 13:22:37 -0400578EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jeff Layton5660e132017-07-06 07:02:25 -0400580void __filemap_set_wb_err(struct address_space *mapping, int err)
581{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400582 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400583
584 trace_filemap_set_wb_err(mapping, eseq);
585}
586EXPORT_SYMBOL(__filemap_set_wb_err);
587
588/**
589 * file_check_and_advance_wb_err - report wb error (if any) that was previously
590 * and advance wb_err to current one
591 * @file: struct file on which the error is being reported
592 *
593 * When userland calls fsync (or something like nfsd does the equivalent), we
594 * want to report any writeback errors that occurred since the last fsync (or
595 * since the file was opened if there haven't been any).
596 *
597 * Grab the wb_err from the mapping. If it matches what we have in the file,
598 * then just quickly return 0. The file is all caught up.
599 *
600 * If it doesn't match, then take the mapping value, set the "seen" flag in
601 * it and try to swap it into place. If it works, or another task beat us
602 * to it with the new value, then update the f_wb_err and return the error
603 * portion. The error at this point must be reported via proper channels
604 * (a'la fsync, or NFS COMMIT operation, etc.).
605 *
606 * While we handle mapping->wb_err with atomic operations, the f_wb_err
607 * value is protected by the f_lock since we must ensure that it reflects
608 * the latest value swapped in for this file descriptor.
609 */
610int file_check_and_advance_wb_err(struct file *file)
611{
612 int err = 0;
613 errseq_t old = READ_ONCE(file->f_wb_err);
614 struct address_space *mapping = file->f_mapping;
615
616 /* Locklessly handle the common case where nothing has changed */
617 if (errseq_check(&mapping->wb_err, old)) {
618 /* Something changed, must use slow path */
619 spin_lock(&file->f_lock);
620 old = file->f_wb_err;
621 err = errseq_check_and_advance(&mapping->wb_err,
622 &file->f_wb_err);
623 trace_file_check_and_advance_wb_err(file, old);
624 spin_unlock(&file->f_lock);
625 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700626
627 /*
628 * We're mostly using this function as a drop in replacement for
629 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
630 * that the legacy code would have had on these flags.
631 */
632 clear_bit(AS_EIO, &mapping->flags);
633 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400634 return err;
635}
636EXPORT_SYMBOL(file_check_and_advance_wb_err);
637
638/**
639 * file_write_and_wait_range - write out & wait on a file range
640 * @file: file pointing to address_space with pages
641 * @lstart: offset in bytes where the range starts
642 * @lend: offset in bytes where the range ends (inclusive)
643 *
644 * Write out and wait upon file offsets lstart->lend, inclusive.
645 *
646 * Note that @lend is inclusive (describes the last byte to be written) so
647 * that this function can be used to write to the very end-of-file (end = -1).
648 *
649 * After writing out and waiting on the data, we check and advance the
650 * f_wb_err cursor to the latest value, and return any errors detected there.
651 */
652int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
653{
654 int err = 0, err2;
655 struct address_space *mapping = file->f_mapping;
656
Jeff Layton9326c9b2017-07-26 10:21:11 -0400657 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400658 err = __filemap_fdatawrite_range(mapping, lstart, lend,
659 WB_SYNC_ALL);
660 /* See comment of filemap_write_and_wait() */
661 if (err != -EIO)
662 __filemap_fdatawait_range(mapping, lstart, lend);
663 }
664 err2 = file_check_and_advance_wb_err(file);
665 if (!err)
666 err = err2;
667 return err;
668}
669EXPORT_SYMBOL(file_write_and_wait_range);
670
Randy Dunlap485bb992006-06-23 02:03:49 -0700671/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700672 * replace_page_cache_page - replace a pagecache page with a new one
673 * @old: page to be replaced
674 * @new: page to replace with
675 * @gfp_mask: allocation mode
676 *
677 * This function replaces a page in the pagecache with a new one. On
678 * success it acquires the pagecache reference for the new page and
679 * drops it for the old page. Both the old and new pages must be
680 * locked. This function does not add the new page to the LRU, the
681 * caller must do that.
682 *
683 * The remove + add is atomic. The only way this function can fail is
684 * memory allocation failure.
685 */
686int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
687{
688 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700689
Sasha Levin309381fea2014-01-23 15:52:54 -0800690 VM_BUG_ON_PAGE(!PageLocked(old), old);
691 VM_BUG_ON_PAGE(!PageLocked(new), new);
692 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700693
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700694 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
695 if (!error) {
696 struct address_space *mapping = old->mapping;
697 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400698 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700699
700 pgoff_t offset = old->index;
701 freepage = mapping->a_ops->freepage;
702
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300703 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700704 new->mapping = mapping;
705 new->index = offset;
706
Greg Thelenc4843a72015-05-22 17:13:16 -0400707 spin_lock_irqsave(&mapping->tree_lock, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700708 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700709 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700710 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700711
712 /*
713 * hugetlb pages do not participate in page cache accounting.
714 */
715 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700716 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700717 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700718 __inc_node_page_state(new, NR_SHMEM);
Greg Thelenc4843a72015-05-22 17:13:16 -0400719 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700720 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700721 radix_tree_preload_end();
722 if (freepage)
723 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300724 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700725 }
726
727 return error;
728}
729EXPORT_SYMBOL_GPL(replace_page_cache_page);
730
Johannes Weinera5289102014-04-03 14:47:51 -0700731static int __add_to_page_cache_locked(struct page *page,
732 struct address_space *mapping,
733 pgoff_t offset, gfp_t gfp_mask,
734 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Johannes Weiner00501b52014-08-08 14:19:20 -0700736 int huge = PageHuge(page);
737 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700738 int error;
739
Sasha Levin309381fea2014-01-23 15:52:54 -0800740 VM_BUG_ON_PAGE(!PageLocked(page), page);
741 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700742
Johannes Weiner00501b52014-08-08 14:19:20 -0700743 if (!huge) {
744 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800745 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700746 if (error)
747 return error;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Jan Kara5e4c0d972013-09-11 14:26:05 -0700750 error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700751 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700752 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800753 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700754 return error;
755 }
756
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300757 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700758 page->mapping = mapping;
759 page->index = offset;
760
761 spin_lock_irq(&mapping->tree_lock);
Johannes Weinera5289102014-04-03 14:47:51 -0700762 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700763 radix_tree_preload_end();
764 if (unlikely(error))
765 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700766
767 /* hugetlb pages do not participate in page cache accounting. */
768 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700769 __inc_node_page_state(page, NR_FILE_PAGES);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700770 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700771 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800772 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700773 trace_mm_filemap_add_to_page_cache(page);
774 return 0;
775err_insert:
776 page->mapping = NULL;
777 /* Leave page->index set: truncation relies upon it */
778 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner00501b52014-08-08 14:19:20 -0700779 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800780 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300781 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return error;
783}
Johannes Weinera5289102014-04-03 14:47:51 -0700784
785/**
786 * add_to_page_cache_locked - add a locked page to the pagecache
787 * @page: page to add
788 * @mapping: the page's address_space
789 * @offset: page index
790 * @gfp_mask: page allocation mode
791 *
792 * This function is used to add a page to the pagecache. It must be locked.
793 * This function does not add the page to the LRU. The caller must do that.
794 */
795int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
796 pgoff_t offset, gfp_t gfp_mask)
797{
798 return __add_to_page_cache_locked(page, mapping, offset,
799 gfp_mask, NULL);
800}
Nick Piggine2867812008-07-25 19:45:30 -0700801EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400804 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Johannes Weinera5289102014-04-03 14:47:51 -0700806 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700807 int ret;
808
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800809 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700810 ret = __add_to_page_cache_locked(page, mapping, offset,
811 gfp_mask, &shadow);
812 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800813 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700814 else {
815 /*
816 * The page might have been evicted from cache only
817 * recently, in which case it should be activated like
818 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700819 * The exception is pages getting rewritten; evicting other
820 * data from the working set, only to cache data that will
821 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700822 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700823 if (!(gfp_mask & __GFP_WRITE) &&
824 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700825 SetPageActive(page);
826 workingset_activation(page);
827 } else
828 ClearPageActive(page);
829 lru_cache_add(page);
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return ret;
832}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300833EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Paul Jackson44110fe2006-03-24 03:16:04 -0800835#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700836struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800837{
Miao Xiec0ff7452010-05-24 14:32:08 -0700838 int n;
839 struct page *page;
840
Paul Jackson44110fe2006-03-24 03:16:04 -0800841 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700842 unsigned int cpuset_mems_cookie;
843 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700844 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700845 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700846 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700847 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700848
Miao Xiec0ff7452010-05-24 14:32:08 -0700849 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800850 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700851 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800852}
Nick Piggin2ae88142006-10-28 10:38:23 -0700853EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800854#endif
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * In order to wait for pages to become available there must be
858 * waitqueues associated with pages. By using a hash table of
859 * waitqueues where the bucket discipline is to maintain all
860 * waiters on the same queue and wake all when any of the pages
861 * become available, and for the woken contexts to check to be
862 * sure the appropriate page became available, this saves space
863 * at a cost of "thundering herd" phenomena during rare hash
864 * collisions.
865 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000866#define PAGE_WAIT_TABLE_BITS 8
867#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
868static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
869
870static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Nicholas Piggin62906022016-12-25 13:00:30 +1000872 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Nicholas Piggin62906022016-12-25 13:00:30 +1000874
875void __init pagecache_init(void)
876{
877 int i;
878
879 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
880 init_waitqueue_head(&page_wait_table[i]);
881
882 page_writeback_init();
883}
884
Linus Torvalds3510ca22017-08-27 13:55:12 -0700885/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000886struct wait_page_key {
887 struct page *page;
888 int bit_nr;
889 int page_match;
890};
891
892struct wait_page_queue {
893 struct page *page;
894 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200895 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000896};
897
Ingo Molnarac6424b2017-06-20 12:06:13 +0200898static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000899{
900 struct wait_page_key *key = arg;
901 struct wait_page_queue *wait_page
902 = container_of(wait, struct wait_page_queue, wait);
903
904 if (wait_page->page != key->page)
905 return 0;
906 key->page_match = 1;
907
908 if (wait_page->bit_nr != key->bit_nr)
909 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -0700910
911 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +1000912 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -0700913 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +1000914
915 return autoremove_wake_function(wait, mode, sync, key);
916}
917
Nicholas Piggin74d81bf2017-02-22 15:44:41 -0800918static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +1000919{
920 wait_queue_head_t *q = page_waitqueue(page);
921 struct wait_page_key key;
922 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -0700923 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +1000924
925 key.page = page;
926 key.bit_nr = bit_nr;
927 key.page_match = 0;
928
Tim Chen11a19c72017-08-25 09:13:55 -0700929 bookmark.flags = 0;
930 bookmark.private = NULL;
931 bookmark.func = NULL;
932 INIT_LIST_HEAD(&bookmark.entry);
933
Nicholas Piggin62906022016-12-25 13:00:30 +1000934 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -0700935 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
936
937 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
938 /*
939 * Take a breather from holding the lock,
940 * allow pages that finish wake up asynchronously
941 * to acquire the lock and remove themselves
942 * from wait queue
943 */
944 spin_unlock_irqrestore(&q->lock, flags);
945 cpu_relax();
946 spin_lock_irqsave(&q->lock, flags);
947 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
948 }
949
Nicholas Piggin62906022016-12-25 13:00:30 +1000950 /*
951 * It is possible for other pages to have collided on the waitqueue
952 * hash, so in that case check for a page match. That prevents a long-
953 * term waiter
954 *
955 * It is still possible to miss a case here, when we woke page waiters
956 * and removed them from the waitqueue, but there are still other
957 * page waiters.
958 */
959 if (!waitqueue_active(q) || !key.page_match) {
960 ClearPageWaiters(page);
961 /*
962 * It's possible to miss clearing Waiters here, when we woke
963 * our page waiters, but the hashed waitqueue has waiters for
964 * other pages on it.
965 *
966 * That's okay, it's a rare case. The next waker will clear it.
967 */
968 }
969 spin_unlock_irqrestore(&q->lock, flags);
970}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -0800971
972static void wake_up_page(struct page *page, int bit)
973{
974 if (!PageWaiters(page))
975 return;
976 wake_up_page_bit(page, bit);
977}
Nicholas Piggin62906022016-12-25 13:00:30 +1000978
979static inline int wait_on_page_bit_common(wait_queue_head_t *q,
980 struct page *page, int bit_nr, int state, bool lock)
981{
982 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200983 wait_queue_entry_t *wait = &wait_page.wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000984 int ret = 0;
985
986 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -0700987 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +1000988 wait->func = wake_page_function;
989 wait_page.page = page;
990 wait_page.bit_nr = bit_nr;
991
992 for (;;) {
993 spin_lock_irq(&q->lock);
994
Ingo Molnar2055da92017-06-20 12:06:46 +0200995 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -0700996 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +1000997 SetPageWaiters(page);
998 }
999
1000 set_current_state(state);
1001
1002 spin_unlock_irq(&q->lock);
1003
1004 if (likely(test_bit(bit_nr, &page->flags))) {
1005 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001006 }
1007
1008 if (lock) {
1009 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1010 break;
1011 } else {
1012 if (!test_bit(bit_nr, &page->flags))
1013 break;
1014 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001015
1016 if (unlikely(signal_pending_state(state, current))) {
1017 ret = -EINTR;
1018 break;
1019 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001020 }
1021
1022 finish_wait(q, wait);
1023
1024 /*
1025 * A signal could leave PageWaiters set. Clearing it here if
1026 * !waitqueue_active would be possible (by open-coding finish_wait),
1027 * but still fail to catch it in the case of wait hash collision. We
1028 * already can fail to clear wait hash collision cases, so don't
1029 * bother with signals either.
1030 */
1031
1032 return ret;
1033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Harvey Harrison920c7a52008-02-04 22:29:26 -08001035void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Nicholas Piggin62906022016-12-25 13:00:30 +10001037 wait_queue_head_t *q = page_waitqueue(page);
1038 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040EXPORT_SYMBOL(wait_on_page_bit);
1041
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001042int wait_on_page_bit_killable(struct page *page, int bit_nr)
1043{
Nicholas Piggin62906022016-12-25 13:00:30 +10001044 wait_queue_head_t *q = page_waitqueue(page);
1045 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001049 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001050 * @page: Page defining the wait queue of interest
1051 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001052 *
1053 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1054 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001055void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001056{
1057 wait_queue_head_t *q = page_waitqueue(page);
1058 unsigned long flags;
1059
1060 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001061 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001062 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001063 spin_unlock_irqrestore(&q->lock, flags);
1064}
1065EXPORT_SYMBOL_GPL(add_page_wait_queue);
1066
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001067#ifndef clear_bit_unlock_is_negative_byte
1068
1069/*
1070 * PG_waiters is the high bit in the same byte as PG_lock.
1071 *
1072 * On x86 (and on many other architectures), we can clear PG_lock and
1073 * test the sign bit at the same time. But if the architecture does
1074 * not support that special operation, we just do this all by hand
1075 * instead.
1076 *
1077 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1078 * being cleared, but a memory barrier should be unneccssary since it is
1079 * in the same byte as PG_locked.
1080 */
1081static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1082{
1083 clear_bit_unlock(nr, mem);
1084 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001085 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001086}
1087
1088#endif
1089
David Howells385e1ca5f2009-04-03 16:42:39 +01001090/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001091 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * @page: the page
1093 *
1094 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1095 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001096 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1098 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001099 * Note that this depends on PG_waiters being the sign bit in the byte
1100 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1101 * clear the PG_locked bit and test PG_waiters at the same time fairly
1102 * portably (architectures that do LL/SC can test any bit, while x86 can
1103 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001105void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001107 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001108 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001109 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001110 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1111 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113EXPORT_SYMBOL(unlock_page);
1114
Randy Dunlap485bb992006-06-23 02:03:49 -07001115/**
1116 * end_page_writeback - end writeback against a page
1117 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
1119void end_page_writeback(struct page *page)
1120{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001121 /*
1122 * TestClearPageReclaim could be used here but it is an atomic
1123 * operation and overkill in this particular case. Failing to
1124 * shuffle a page marked for immediate reclaim is too mild to
1125 * justify taking an atomic operation penalty at the end of
1126 * ever page writeback.
1127 */
1128 if (PageReclaim(page)) {
1129 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001130 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001131 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001132
1133 if (!test_clear_page_writeback(page))
1134 BUG();
1135
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001136 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 wake_up_page(page, PG_writeback);
1138}
1139EXPORT_SYMBOL(end_page_writeback);
1140
Matthew Wilcox57d99842014-06-04 16:07:45 -07001141/*
1142 * After completing I/O on a page, call this routine to update the page
1143 * flags appropriately
1144 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001145void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001146{
Jens Axboec11f0c02016-08-05 08:11:04 -06001147 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001148 if (!err) {
1149 SetPageUptodate(page);
1150 } else {
1151 ClearPageUptodate(page);
1152 SetPageError(page);
1153 }
1154 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001155 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001156 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001157 struct address_space *mapping;
1158
Matthew Wilcox57d99842014-06-04 16:07:45 -07001159 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001160 mapping = page_mapping(page);
1161 if (mapping)
1162 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001163 }
1164 end_page_writeback(page);
1165 }
1166}
1167EXPORT_SYMBOL_GPL(page_endio);
1168
Randy Dunlap485bb992006-06-23 02:03:49 -07001169/**
1170 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001171 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001173void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Nicholas Piggin62906022016-12-25 13:00:30 +10001175 struct page *page = compound_head(__page);
1176 wait_queue_head_t *q = page_waitqueue(page);
1177 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179EXPORT_SYMBOL(__lock_page);
1180
Nicholas Piggin62906022016-12-25 13:00:30 +10001181int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001182{
Nicholas Piggin62906022016-12-25 13:00:30 +10001183 struct page *page = compound_head(__page);
1184 wait_queue_head_t *q = page_waitqueue(page);
1185 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001186}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001187EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001188
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001189/*
1190 * Return values:
1191 * 1 - page is locked; mmap_sem is still held.
1192 * 0 - page is not locked.
1193 * mmap_sem has been released (up_read()), unless flags had both
1194 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1195 * which case mmap_sem is still held.
1196 *
1197 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1198 * with the page locked and the mmap_sem unperturbed.
1199 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001200int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1201 unsigned int flags)
1202{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001203 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1204 /*
1205 * CAUTION! In this case, mmap_sem is not released
1206 * even though return 0.
1207 */
1208 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1209 return 0;
1210
1211 up_read(&mm->mmap_sem);
1212 if (flags & FAULT_FLAG_KILLABLE)
1213 wait_on_page_locked_killable(page);
1214 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001215 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001216 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001217 } else {
1218 if (flags & FAULT_FLAG_KILLABLE) {
1219 int ret;
1220
1221 ret = __lock_page_killable(page);
1222 if (ret) {
1223 up_read(&mm->mmap_sem);
1224 return 0;
1225 }
1226 } else
1227 __lock_page(page);
1228 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001229 }
1230}
1231
Randy Dunlap485bb992006-06-23 02:03:49 -07001232/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001233 * page_cache_next_hole - find the next hole (not-present entry)
1234 * @mapping: mapping
1235 * @index: index
1236 * @max_scan: maximum range to search
1237 *
1238 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1239 * lowest indexed hole.
1240 *
1241 * Returns: the index of the hole if found, otherwise returns an index
1242 * outside of the set specified (in which case 'return - index >=
1243 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1244 * be returned.
1245 *
1246 * page_cache_next_hole may be called under rcu_read_lock. However,
1247 * like radix_tree_gang_lookup, this will not atomically search a
1248 * snapshot of the tree at a single point in time. For example, if a
1249 * hole is created at index 5, then subsequently a hole is created at
1250 * index 10, page_cache_next_hole covering both indexes may return 10
1251 * if called under rcu_read_lock.
1252 */
1253pgoff_t page_cache_next_hole(struct address_space *mapping,
1254 pgoff_t index, unsigned long max_scan)
1255{
1256 unsigned long i;
1257
1258 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001259 struct page *page;
1260
1261 page = radix_tree_lookup(&mapping->page_tree, index);
1262 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001263 break;
1264 index++;
1265 if (index == 0)
1266 break;
1267 }
1268
1269 return index;
1270}
1271EXPORT_SYMBOL(page_cache_next_hole);
1272
1273/**
1274 * page_cache_prev_hole - find the prev hole (not-present entry)
1275 * @mapping: mapping
1276 * @index: index
1277 * @max_scan: maximum range to search
1278 *
1279 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1280 * the first hole.
1281 *
1282 * Returns: the index of the hole if found, otherwise returns an index
1283 * outside of the set specified (in which case 'index - return >=
1284 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1285 * will be returned.
1286 *
1287 * page_cache_prev_hole may be called under rcu_read_lock. However,
1288 * like radix_tree_gang_lookup, this will not atomically search a
1289 * snapshot of the tree at a single point in time. For example, if a
1290 * hole is created at index 10, then subsequently a hole is created at
1291 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1292 * called under rcu_read_lock.
1293 */
1294pgoff_t page_cache_prev_hole(struct address_space *mapping,
1295 pgoff_t index, unsigned long max_scan)
1296{
1297 unsigned long i;
1298
1299 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001300 struct page *page;
1301
1302 page = radix_tree_lookup(&mapping->page_tree, index);
1303 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001304 break;
1305 index--;
1306 if (index == ULONG_MAX)
1307 break;
1308 }
1309
1310 return index;
1311}
1312EXPORT_SYMBOL(page_cache_prev_hole);
1313
1314/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001315 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001316 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001317 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001318 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001319 * Looks up the page cache slot at @mapping & @offset. If there is a
1320 * page cache page, it is returned with an increased refcount.
1321 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001322 * If the slot holds a shadow entry of a previously evicted page, or a
1323 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001324 *
1325 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001327struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Nick Piggina60637c2008-07-25 19:45:31 -07001329 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001330 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Nick Piggina60637c2008-07-25 19:45:31 -07001332 rcu_read_lock();
1333repeat:
1334 page = NULL;
1335 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1336 if (pagep) {
1337 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001338 if (unlikely(!page))
1339 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001340 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001341 if (radix_tree_deref_retry(page))
1342 goto repeat;
1343 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001344 * A shadow entry of a recently evicted page,
1345 * or a swap entry from shmem/tmpfs. Return
1346 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001347 */
1348 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001349 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001350
1351 head = compound_head(page);
1352 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001353 goto repeat;
1354
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001355 /* The page was split under us? */
1356 if (compound_head(page) != head) {
1357 put_page(head);
1358 goto repeat;
1359 }
1360
Nick Piggina60637c2008-07-25 19:45:31 -07001361 /*
1362 * Has the page moved?
1363 * This is part of the lockless pagecache protocol. See
1364 * include/linux/pagemap.h for details.
1365 */
1366 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001367 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001368 goto repeat;
1369 }
1370 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001371out:
Nick Piggina60637c2008-07-25 19:45:31 -07001372 rcu_read_unlock();
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return page;
1375}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001376EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Randy Dunlap485bb992006-06-23 02:03:49 -07001378/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001379 * find_lock_entry - locate, pin and lock a page cache entry
1380 * @mapping: the address_space to search
1381 * @offset: the page cache index
1382 *
1383 * Looks up the page cache slot at @mapping & @offset. If there is a
1384 * page cache page, it is returned locked and with an increased
1385 * refcount.
1386 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001387 * If the slot holds a shadow entry of a previously evicted page, or a
1388 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001389 *
1390 * Otherwise, %NULL is returned.
1391 *
1392 * find_lock_entry() may sleep.
1393 */
1394struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct page *page;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001399 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001400 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001401 lock_page(page);
1402 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001403 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001404 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001405 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001406 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001408 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return page;
1411}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001412EXPORT_SYMBOL(find_lock_entry);
1413
1414/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001415 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001416 * @mapping: the address_space to search
1417 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001418 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001419 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001420 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001421 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001422 *
Randy Dunlap75325182014-07-30 16:08:37 -07001423 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001424 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001425 * @fgp_flags can be:
1426 *
1427 * - FGP_ACCESSED: the page will be marked accessed
1428 * - FGP_LOCK: Page is return locked
1429 * - FGP_CREAT: If page is not present then a new page is allocated using
1430 * @gfp_mask and added to the page cache and the VM's LRU
1431 * list. The page is returned locked and with an increased
1432 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001433 *
1434 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1435 * if the GFP flags specified for FGP_CREAT are atomic.
1436 *
1437 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001438 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001439struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001440 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Nick Piggineb2be182007-10-16 01:24:57 -07001442 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001445 page = find_get_entry(mapping, offset);
1446 if (radix_tree_exceptional_entry(page))
1447 page = NULL;
1448 if (!page)
1449 goto no_page;
1450
1451 if (fgp_flags & FGP_LOCK) {
1452 if (fgp_flags & FGP_NOWAIT) {
1453 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001454 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001455 return NULL;
1456 }
1457 } else {
1458 lock_page(page);
1459 }
1460
1461 /* Has the page been truncated? */
1462 if (unlikely(page->mapping != mapping)) {
1463 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001464 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001465 goto repeat;
1466 }
1467 VM_BUG_ON_PAGE(page->index != offset, page);
1468 }
1469
1470 if (page && (fgp_flags & FGP_ACCESSED))
1471 mark_page_accessed(page);
1472
1473no_page:
1474 if (!page && (fgp_flags & FGP_CREAT)) {
1475 int err;
1476 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001477 gfp_mask |= __GFP_WRITE;
1478 if (fgp_flags & FGP_NOFS)
1479 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001480
Michal Hocko45f87de2014-12-29 20:30:35 +01001481 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001482 if (!page)
1483 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001484
1485 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1486 fgp_flags |= FGP_LOCK;
1487
Hugh Dickinseb39d612014-08-06 16:06:43 -07001488 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001489 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001490 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001491
Michal Hocko45f87de2014-12-29 20:30:35 +01001492 err = add_to_page_cache_lru(page, mapping, offset,
1493 gfp_mask & GFP_RECLAIM_MASK);
Nick Piggineb2be182007-10-16 01:24:57 -07001494 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001495 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001496 page = NULL;
1497 if (err == -EEXIST)
1498 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return page;
1503}
Mel Gorman2457aec2014-06-04 16:10:31 -07001504EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001507 * find_get_entries - gang pagecache lookup
1508 * @mapping: The address_space to search
1509 * @start: The starting page cache index
1510 * @nr_entries: The maximum number of entries
1511 * @entries: Where the resulting entries are placed
1512 * @indices: The cache indices corresponding to the entries in @entries
1513 *
1514 * find_get_entries() will search for and return a group of up to
1515 * @nr_entries entries in the mapping. The entries are placed at
1516 * @entries. find_get_entries() takes a reference against any actual
1517 * pages it returns.
1518 *
1519 * The search returns a group of mapping-contiguous page cache entries
1520 * with ascending indexes. There may be holes in the indices due to
1521 * not-present pages.
1522 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001523 * Any shadow entries of evicted pages, or swap entries from
1524 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001525 *
1526 * find_get_entries() returns the number of pages and shadow entries
1527 * which were found.
1528 */
1529unsigned find_get_entries(struct address_space *mapping,
1530 pgoff_t start, unsigned int nr_entries,
1531 struct page **entries, pgoff_t *indices)
1532{
1533 void **slot;
1534 unsigned int ret = 0;
1535 struct radix_tree_iter iter;
1536
1537 if (!nr_entries)
1538 return 0;
1539
1540 rcu_read_lock();
Johannes Weiner0cd61442014-04-03 14:47:46 -07001541 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001542 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001543repeat:
1544 page = radix_tree_deref_slot(slot);
1545 if (unlikely(!page))
1546 continue;
1547 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001548 if (radix_tree_deref_retry(page)) {
1549 slot = radix_tree_iter_retry(&iter);
1550 continue;
1551 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001552 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001553 * A shadow entry of a recently evicted page, a swap
1554 * entry from shmem/tmpfs or a DAX entry. Return it
1555 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001556 */
1557 goto export;
1558 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001559
1560 head = compound_head(page);
1561 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001562 goto repeat;
1563
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001564 /* The page was split under us? */
1565 if (compound_head(page) != head) {
1566 put_page(head);
1567 goto repeat;
1568 }
1569
Johannes Weiner0cd61442014-04-03 14:47:46 -07001570 /* Has the page moved? */
1571 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001572 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001573 goto repeat;
1574 }
1575export:
1576 indices[ret] = iter.index;
1577 entries[ret] = page;
1578 if (++ret == nr_entries)
1579 break;
1580 }
1581 rcu_read_unlock();
1582 return ret;
1583}
1584
1585/**
Jan Karab947cee2017-09-06 16:21:21 -07001586 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * @mapping: The address_space to search
1588 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001589 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * @nr_pages: The maximum number of pages
1591 * @pages: Where the resulting pages are placed
1592 *
Jan Karab947cee2017-09-06 16:21:21 -07001593 * find_get_pages_range() will search for and return a group of up to @nr_pages
1594 * pages in the mapping starting at index @start and up to index @end
1595 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1596 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 *
1598 * The search returns a group of mapping-contiguous pages with ascending
1599 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001600 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 *
Jan Karab947cee2017-09-06 16:21:21 -07001602 * find_get_pages_range() returns the number of pages which were found. If this
1603 * number is smaller than @nr_pages, the end of specified range has been
1604 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Jan Karab947cee2017-09-06 16:21:21 -07001606unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1607 pgoff_t end, unsigned int nr_pages,
1608 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001610 struct radix_tree_iter iter;
1611 void **slot;
1612 unsigned ret = 0;
1613
1614 if (unlikely(!nr_pages))
1615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Nick Piggina60637c2008-07-25 19:45:31 -07001617 rcu_read_lock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001618 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001619 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001620
1621 if (iter.index > end)
1622 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001623repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001624 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001625 if (unlikely(!page))
1626 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001627
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001628 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001629 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001630 slot = radix_tree_iter_retry(&iter);
1631 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001632 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001633 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001634 * A shadow entry of a recently evicted page,
1635 * or a swap entry from shmem/tmpfs. Skip
1636 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001637 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001638 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001639 }
Nick Piggina60637c2008-07-25 19:45:31 -07001640
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001641 head = compound_head(page);
1642 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001643 goto repeat;
1644
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001645 /* The page was split under us? */
1646 if (compound_head(page) != head) {
1647 put_page(head);
1648 goto repeat;
1649 }
1650
Nick Piggina60637c2008-07-25 19:45:31 -07001651 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001652 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001653 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001654 goto repeat;
1655 }
1656
1657 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001658 if (++ret == nr_pages) {
1659 *start = pages[ret - 1]->index + 1;
1660 goto out;
1661 }
Nick Piggina60637c2008-07-25 19:45:31 -07001662 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001663
Jan Karab947cee2017-09-06 16:21:21 -07001664 /*
1665 * We come here when there is no page beyond @end. We take care to not
1666 * overflow the index @start as it confuses some of the callers. This
1667 * breaks the iteration when there is page at index -1 but that is
1668 * already broken anyway.
1669 */
1670 if (end == (pgoff_t)-1)
1671 *start = (pgoff_t)-1;
1672 else
1673 *start = end + 1;
1674out:
Nick Piggina60637c2008-07-25 19:45:31 -07001675 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return ret;
1678}
1679
Jens Axboeebf43502006-04-27 08:46:01 +02001680/**
1681 * find_get_pages_contig - gang contiguous pagecache lookup
1682 * @mapping: The address_space to search
1683 * @index: The starting page index
1684 * @nr_pages: The maximum number of pages
1685 * @pages: Where the resulting pages are placed
1686 *
1687 * find_get_pages_contig() works exactly like find_get_pages(), except
1688 * that the returned number of pages are guaranteed to be contiguous.
1689 *
1690 * find_get_pages_contig() returns the number of pages which were found.
1691 */
1692unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1693 unsigned int nr_pages, struct page **pages)
1694{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001695 struct radix_tree_iter iter;
1696 void **slot;
1697 unsigned int ret = 0;
1698
1699 if (unlikely(!nr_pages))
1700 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001701
Nick Piggina60637c2008-07-25 19:45:31 -07001702 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001703 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001704 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001705repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001706 page = radix_tree_deref_slot(slot);
1707 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001708 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001709 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001710
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001711 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001712 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001713 slot = radix_tree_iter_retry(&iter);
1714 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001715 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001716 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001717 * A shadow entry of a recently evicted page,
1718 * or a swap entry from shmem/tmpfs. Stop
1719 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001720 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001721 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001722 }
Nick Piggina60637c2008-07-25 19:45:31 -07001723
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001724 head = compound_head(page);
1725 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001726 goto repeat;
1727
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001728 /* The page was split under us? */
1729 if (compound_head(page) != head) {
1730 put_page(head);
1731 goto repeat;
1732 }
1733
Nick Piggina60637c2008-07-25 19:45:31 -07001734 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001735 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001736 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001737 goto repeat;
1738 }
1739
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001740 /*
1741 * must check mapping and index after taking the ref.
1742 * otherwise we can get both false positives and false
1743 * negatives, which is just confusing to the caller.
1744 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001745 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001746 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001747 break;
1748 }
1749
Nick Piggina60637c2008-07-25 19:45:31 -07001750 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001751 if (++ret == nr_pages)
1752 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001753 }
Nick Piggina60637c2008-07-25 19:45:31 -07001754 rcu_read_unlock();
1755 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001756}
David Howellsef71c152007-05-09 02:33:44 -07001757EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001758
Randy Dunlap485bb992006-06-23 02:03:49 -07001759/**
Jan Kara72b045a2017-11-15 17:34:33 -08001760 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001761 * @mapping: the address_space to search
1762 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001763 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001764 * @tag: the tag index
1765 * @nr_pages: the maximum number of pages
1766 * @pages: where the resulting pages are placed
1767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001769 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 */
Jan Kara72b045a2017-11-15 17:34:33 -08001771unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1772 pgoff_t end, int tag, unsigned int nr_pages,
1773 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001775 struct radix_tree_iter iter;
1776 void **slot;
1777 unsigned ret = 0;
1778
1779 if (unlikely(!nr_pages))
1780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Nick Piggina60637c2008-07-25 19:45:31 -07001782 rcu_read_lock();
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001783 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1784 &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001785 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001786
1787 if (iter.index > end)
1788 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001789repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001790 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001791 if (unlikely(!page))
1792 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001793
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001794 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001795 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001796 slot = radix_tree_iter_retry(&iter);
1797 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001798 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001799 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001800 * A shadow entry of a recently evicted page.
1801 *
1802 * Those entries should never be tagged, but
1803 * this tree walk is lockless and the tags are
1804 * looked up in bulk, one radix tree node at a
1805 * time, so there is a sizable window for page
1806 * reclaim to evict a page we saw tagged.
1807 *
1808 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001809 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001810 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001811 }
Nick Piggina60637c2008-07-25 19:45:31 -07001812
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001813 head = compound_head(page);
1814 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001815 goto repeat;
1816
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001817 /* The page was split under us? */
1818 if (compound_head(page) != head) {
1819 put_page(head);
1820 goto repeat;
1821 }
1822
Nick Piggina60637c2008-07-25 19:45:31 -07001823 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001824 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001825 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001826 goto repeat;
1827 }
1828
1829 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001830 if (++ret == nr_pages) {
1831 *index = pages[ret - 1]->index + 1;
1832 goto out;
1833 }
Nick Piggina60637c2008-07-25 19:45:31 -07001834 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001835
Jan Kara72b045a2017-11-15 17:34:33 -08001836 /*
1837 * We come here when we got at @end. We take care to not overflow the
1838 * index @index as it confuses some of the callers. This breaks the
1839 * iteration when there is page at index -1 but that is already broken
1840 * anyway.
1841 */
1842 if (end == (pgoff_t)-1)
1843 *index = (pgoff_t)-1;
1844 else
1845 *index = end + 1;
1846out:
Nick Piggina60637c2008-07-25 19:45:31 -07001847 rcu_read_unlock();
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return ret;
1850}
Jan Kara72b045a2017-11-15 17:34:33 -08001851EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001853/**
1854 * find_get_entries_tag - find and return entries that match @tag
1855 * @mapping: the address_space to search
1856 * @start: the starting page cache index
1857 * @tag: the tag index
1858 * @nr_entries: the maximum number of entries
1859 * @entries: where the resulting entries are placed
1860 * @indices: the cache indices corresponding to the entries in @entries
1861 *
1862 * Like find_get_entries, except we only return entries which are tagged with
1863 * @tag.
1864 */
1865unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1866 int tag, unsigned int nr_entries,
1867 struct page **entries, pgoff_t *indices)
1868{
1869 void **slot;
1870 unsigned int ret = 0;
1871 struct radix_tree_iter iter;
1872
1873 if (!nr_entries)
1874 return 0;
1875
1876 rcu_read_lock();
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001877 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1878 &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001879 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001880repeat:
1881 page = radix_tree_deref_slot(slot);
1882 if (unlikely(!page))
1883 continue;
1884 if (radix_tree_exception(page)) {
1885 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001886 slot = radix_tree_iter_retry(&iter);
1887 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001888 }
1889
1890 /*
1891 * A shadow entry of a recently evicted page, a swap
1892 * entry from shmem/tmpfs or a DAX entry. Return it
1893 * without attempting to raise page count.
1894 */
1895 goto export;
1896 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001897
1898 head = compound_head(page);
1899 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001900 goto repeat;
1901
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001902 /* The page was split under us? */
1903 if (compound_head(page) != head) {
1904 put_page(head);
1905 goto repeat;
1906 }
1907
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001908 /* Has the page moved? */
1909 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001910 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001911 goto repeat;
1912 }
1913export:
1914 indices[ret] = iter.index;
1915 entries[ret] = page;
1916 if (++ret == nr_entries)
1917 break;
1918 }
1919 rcu_read_unlock();
1920 return ret;
1921}
1922EXPORT_SYMBOL(find_get_entries_tag);
1923
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001924/*
1925 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1926 * a _large_ part of the i/o request. Imagine the worst scenario:
1927 *
1928 * ---R__________________________________________B__________
1929 * ^ reading here ^ bad block(assume 4k)
1930 *
1931 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1932 * => failing the whole request => read(R) => read(R+1) =>
1933 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1934 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1935 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1936 *
1937 * It is going insane. Fix it by quickly scaling down the readahead size.
1938 */
1939static void shrink_readahead_size_eio(struct file *filp,
1940 struct file_ra_state *ra)
1941{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001942 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001943}
1944
Randy Dunlap485bb992006-06-23 02:03:49 -07001945/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001946 * generic_file_buffered_read - generic file read routine
1947 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05001948 * @iter: data destination
1949 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001950 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001952 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 *
1954 * This is really ugly. But the goto's actually try to clarify some
1955 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001957static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05001958 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001960 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001961 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001963 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001964 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001965 pgoff_t index;
1966 pgoff_t last_index;
1967 pgoff_t prev_index;
1968 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001969 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001970 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Wei Fangc2a97372016-10-07 17:01:52 -07001972 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08001973 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07001974 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
1975
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001976 index = *ppos >> PAGE_SHIFT;
1977 prev_index = ra->prev_pos >> PAGE_SHIFT;
1978 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1979 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1980 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 for (;;) {
1983 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001984 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001985 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned long nr, ret;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08001990 if (fatal_signal_pending(current)) {
1991 error = -EINTR;
1992 goto out;
1993 }
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001996 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02001997 if (iocb->ki_flags & IOCB_NOWAIT)
1998 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07001999 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002000 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002001 index, last_index - index);
2002 page = find_get_page(mapping, index);
2003 if (unlikely(page == NULL))
2004 goto no_cached_page;
2005 }
2006 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002007 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002008 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002009 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002011 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002012 if (iocb->ki_flags & IOCB_NOWAIT) {
2013 put_page(page);
2014 goto would_block;
2015 }
2016
Mel Gormanebded022016-03-15 14:55:39 -07002017 /*
2018 * See comment in do_read_cache_page on why
2019 * wait_on_page_locked is used to avoid unnecessarily
2020 * serialisations and why it's safe.
2021 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002022 error = wait_on_page_locked_killable(page);
2023 if (unlikely(error))
2024 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002025 if (PageUptodate(page))
2026 goto page_ok;
2027
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002029 !mapping->a_ops->is_partially_uptodate)
2030 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002031 /* pipes can't handle partially uptodate pages */
2032 if (unlikely(iter->type & ITER_PIPE))
2033 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002034 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002035 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002036 /* Did it get truncated before we got the lock? */
2037 if (!page->mapping)
2038 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002039 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002040 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002041 goto page_not_up_to_date_locked;
2042 unlock_page(page);
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002045 /*
2046 * i_size must be checked after we know the page is Uptodate.
2047 *
2048 * Checking i_size after the check allows us to calculate
2049 * the correct value for "nr", which means the zero-filled
2050 * part of the page is not copied back to userspace (unless
2051 * another truncate extends the file - this is desired though).
2052 */
2053
2054 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002055 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002056 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002057 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002058 goto out;
2059 }
2060
2061 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002063 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002064 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002065 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002067 goto out;
2068 }
2069 }
2070 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 /* If users can be writing to this page using arbitrary
2073 * virtual addresses, take care about potential aliasing
2074 * before reading the page on the kernel side.
2075 */
2076 if (mapping_writably_mapped(mapping))
2077 flush_dcache_page(page);
2078
2079 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002080 * When a sequential read accesses a page several times,
2081 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 */
Jan Karaec0f1632007-05-06 14:49:25 -07002083 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 mark_page_accessed(page);
2085 prev_index = index;
2086
2087 /*
2088 * Ok, we have the page, and it's up-to-date, so
2089 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
Al Viro6e58e792014-02-03 17:07:03 -05002091
2092 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002094 index += offset >> PAGE_SHIFT;
2095 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002096 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002098 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002099 written += ret;
2100 if (!iov_iter_count(iter))
2101 goto out;
2102 if (ret < nr) {
2103 error = -EFAULT;
2104 goto out;
2105 }
2106 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108page_not_up_to_date:
2109 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002110 error = lock_page_killable(page);
2111 if (unlikely(error))
2112 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002114page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002115 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (!page->mapping) {
2117 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 continue;
2120 }
2121
2122 /* Did somebody else fill it already? */
2123 if (PageUptodate(page)) {
2124 unlock_page(page);
2125 goto page_ok;
2126 }
2127
2128readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002129 /*
2130 * A previous I/O error may have been due to temporary
2131 * failures, eg. multipath errors.
2132 * PG_error will be set again if readpage fails.
2133 */
2134 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Start the actual read. The read will unlock the page. */
2136 error = mapping->a_ops->readpage(filp, page);
2137
Zach Brown994fc28c2005-12-15 14:28:17 -08002138 if (unlikely(error)) {
2139 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002141 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002142 goto find_page;
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002148 error = lock_page_killable(page);
2149 if (unlikely(error))
2150 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (!PageUptodate(page)) {
2152 if (page->mapping == NULL) {
2153 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002154 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
2156 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002157 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 goto find_page;
2159 }
2160 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002161 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002162 error = -EIO;
2163 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165 unlock_page(page);
2166 }
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto page_ok;
2169
2170readpage_error:
2171 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002172 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto out;
2174
2175no_cached_page:
2176 /*
2177 * Ok, it wasn't cached, so we need to create a new
2178 * page..
2179 */
Nick Piggineb2be182007-10-16 01:24:57 -07002180 page = page_cache_alloc_cold(mapping);
2181 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002182 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002185 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002186 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002188 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002189 if (error == -EEXIST) {
2190 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 goto out;
2194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto readpage;
2196 }
2197
Milosz Tanski3239d832017-08-29 16:13:19 +02002198would_block:
2199 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002201 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002202 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002203 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002205 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002206 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002207 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Randy Dunlap485bb992006-06-23 02:03:49 -07002210/**
Al Viro6abd2322014-04-04 14:20:57 -04002211 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002212 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002213 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002214 *
Al Viro6abd2322014-04-04 14:20:57 -04002215 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * that can use the page cache directly.
2217 */
2218ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002219generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002221 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002222 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002223
2224 if (!count)
2225 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Al Viro2ba48ce2015-04-09 13:52:01 -04002227 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002228 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002229 struct address_space *mapping = file->f_mapping;
2230 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002231 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002234 if (iocb->ki_flags & IOCB_NOWAIT) {
2235 if (filemap_range_has_page(mapping, iocb->ki_pos,
2236 iocb->ki_pos + count - 1))
2237 return -EAGAIN;
2238 } else {
2239 retval = filemap_write_and_wait_range(mapping,
2240 iocb->ki_pos,
2241 iocb->ki_pos + count - 1);
2242 if (retval < 0)
2243 goto out;
2244 }
Al Viroed978a82014-03-05 22:53:04 -05002245
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002246 file_accessed(file);
2247
Al Viro5ecda132017-04-13 14:13:36 -04002248 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002249 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002250 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002251 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002252 }
Al Viro5b47d592017-05-08 13:54:47 -04002253 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002254
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002255 /*
2256 * Btrfs can have a short DIO read if we encounter
2257 * compressed extents, so if there was an error, or if
2258 * we've already read everything we wanted to, or if
2259 * there was a short read because we hit EOF, go ahead
2260 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002261 * the rest of the read. Buffered reads will not work for
2262 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002263 */
Al Viro5ecda132017-04-13 14:13:36 -04002264 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002265 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002269 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270out:
2271 return retval;
2272}
Al Viroed978a82014-03-05 22:53:04 -05002273EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002276/**
2277 * page_cache_read - adds requested page to the page cache if not already there
2278 * @file: file to read
2279 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002280 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 * This adds the requested page to the page cache if it isn't already there,
2283 * and schedules an I/O to read in its contents from disk.
2284 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002285static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002288 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002289 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Zach Brown994fc28c2005-12-15 14:28:17 -08002291 do {
Michal Hockoc20cd452016-01-14 15:20:12 -08002292 page = __page_cache_alloc(gfp_mask|__GFP_COLD);
Zach Brown994fc28c2005-12-15 14:28:17 -08002293 if (!page)
2294 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Michal Hockoc20cd452016-01-14 15:20:12 -08002296 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask & GFP_KERNEL);
Zach Brown994fc28c2005-12-15 14:28:17 -08002297 if (ret == 0)
2298 ret = mapping->a_ops->readpage(file, page);
2299 else if (ret == -EEXIST)
2300 ret = 0; /* losing race to add is OK */
2301
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002302 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Zach Brown994fc28c2005-12-15 14:28:17 -08002304 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002305
Zach Brown994fc28c2005-12-15 14:28:17 -08002306 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
2309#define MMAP_LOTSAMISS (100)
2310
Linus Torvaldsef00e082009-06-16 15:31:25 -07002311/*
2312 * Synchronous readahead happens when we don't even find
2313 * a page in the page cache at all.
2314 */
2315static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2316 struct file_ra_state *ra,
2317 struct file *file,
2318 pgoff_t offset)
2319{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002320 struct address_space *mapping = file->f_mapping;
2321
2322 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002323 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002324 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002325 if (!ra->ra_pages)
2326 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002327
Joe Perches64363aa2013-07-08 16:00:18 -07002328 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002329 page_cache_sync_readahead(mapping, ra, file, offset,
2330 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002331 return;
2332 }
2333
Andi Kleen207d04b2011-05-24 17:12:29 -07002334 /* Avoid banging the cache line if not needed */
2335 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002336 ra->mmap_miss++;
2337
2338 /*
2339 * Do we miss much more than hit in this file? If so,
2340 * stop bothering with read-ahead. It will only hurt.
2341 */
2342 if (ra->mmap_miss > MMAP_LOTSAMISS)
2343 return;
2344
Wu Fengguangd30a1102009-06-16 15:31:30 -07002345 /*
2346 * mmap read-around
2347 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002348 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2349 ra->size = ra->ra_pages;
2350 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002351 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002352}
2353
2354/*
2355 * Asynchronous readahead happens when we find the page and PG_readahead,
2356 * so we want to possibly extend the readahead further..
2357 */
2358static void do_async_mmap_readahead(struct vm_area_struct *vma,
2359 struct file_ra_state *ra,
2360 struct file *file,
2361 struct page *page,
2362 pgoff_t offset)
2363{
2364 struct address_space *mapping = file->f_mapping;
2365
2366 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002367 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002368 return;
2369 if (ra->mmap_miss > 0)
2370 ra->mmap_miss--;
2371 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002372 page_cache_async_readahead(mapping, ra, file,
2373 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002374}
2375
Randy Dunlap485bb992006-06-23 02:03:49 -07002376/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002377 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002378 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002379 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002380 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 * mapped memory region to read in file data during a page fault.
2382 *
2383 * The goto's are kind of ugly, but this streamlines the normal case of having
2384 * it in the page cache, and handles the special cases reasonably without
2385 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002386 *
2387 * vma->vm_mm->mmap_sem must be held on entry.
2388 *
2389 * If our return value has VM_FAULT_RETRY set, it's because
2390 * lock_page_or_retry() returned 0.
2391 * The mmap_sem has usually been released in this case.
2392 * See __lock_page_or_retry() for the exception.
2393 *
2394 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2395 * has not been released.
2396 *
2397 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 */
Dave Jiang11bac802017-02-24 14:56:41 -08002399int filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002402 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct address_space *mapping = file->f_mapping;
2404 struct file_ra_state *ra = &file->f_ra;
2405 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002406 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002407 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct page *page;
Nick Piggin83c54072007-07-19 01:47:05 -07002409 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002411 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2412 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002413 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002416 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002418 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002419 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002421 * We found the page, so try async readahead before
2422 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 */
Dave Jiang11bac802017-02-24 14:56:41 -08002424 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002425 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002426 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002427 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002428 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002429 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002430 ret = VM_FAULT_MAJOR;
2431retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002432 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (!page)
2434 goto no_cached_page;
2435 }
2436
Dave Jiang11bac802017-02-24 14:56:41 -08002437 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002438 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002439 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002440 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002441
2442 /* Did it get truncated? */
2443 if (unlikely(page->mapping != mapping)) {
2444 unlock_page(page);
2445 put_page(page);
2446 goto retry_find;
2447 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002448 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002451 * We have a locked page in the page cache, now we need to check
2452 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 */
Nick Piggind00806b2007-07-19 01:46:57 -07002454 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto page_not_uptodate;
2456
Linus Torvaldsef00e082009-06-16 15:31:25 -07002457 /*
2458 * Found the page and have a reference on it.
2459 * We must recheck i_size under page lock.
2460 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002461 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2462 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002463 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002464 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002465 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002466 }
2467
Nick Piggind0217ac2007-07-19 01:47:03 -07002468 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002469 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471no_cached_page:
2472 /*
2473 * We're only likely to ever get here if MADV_RANDOM is in
2474 * effect.
2475 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002476 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /*
2479 * The page we want has now been added to the page cache.
2480 * In the unlikely event that someone removed it in the
2481 * meantime, we'll just come back here and read it again.
2482 */
2483 if (error >= 0)
2484 goto retry_find;
2485
2486 /*
2487 * An error return from page_cache_read can result if the
2488 * system is low on memory, or a problem occurs while trying
2489 * to schedule I/O.
2490 */
2491 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002492 return VM_FAULT_OOM;
2493 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 /*
2497 * Umm, take care of errors if the page isn't up-to-date.
2498 * Try to re-read it _once_. We do this synchronously,
2499 * because there really aren't any performance issues here
2500 * and we need to check for errors.
2501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002503 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002504 if (!error) {
2505 wait_on_page_locked(page);
2506 if (!PageUptodate(page))
2507 error = -EIO;
2508 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002509 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002510
2511 if (!error || error == AOP_TRUNCATED_PAGE)
2512 goto retry_find;
2513
2514 /* Things didn't work out. Return zero to tell the mm layer so. */
2515 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002516 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002517}
2518EXPORT_SYMBOL(filemap_fault);
2519
Jan Kara82b0f8c2016-12-14 15:06:58 -08002520void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002521 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002522{
2523 struct radix_tree_iter iter;
2524 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002525 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002526 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002527 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002528 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002529 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002530
2531 rcu_read_lock();
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002532 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
2533 start_pgoff) {
2534 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002535 break;
2536repeat:
2537 page = radix_tree_deref_slot(slot);
2538 if (unlikely(!page))
2539 goto next;
2540 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002541 if (radix_tree_deref_retry(page)) {
2542 slot = radix_tree_iter_retry(&iter);
2543 continue;
2544 }
2545 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002546 }
2547
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002548 head = compound_head(page);
2549 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002550 goto repeat;
2551
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002552 /* The page was split under us? */
2553 if (compound_head(page) != head) {
2554 put_page(head);
2555 goto repeat;
2556 }
2557
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002558 /* Has the page moved? */
2559 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002560 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002561 goto repeat;
2562 }
2563
2564 if (!PageUptodate(page) ||
2565 PageReadahead(page) ||
2566 PageHWPoison(page))
2567 goto skip;
2568 if (!trylock_page(page))
2569 goto skip;
2570
2571 if (page->mapping != mapping || !PageUptodate(page))
2572 goto unlock;
2573
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002574 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2575 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002576 goto unlock;
2577
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002578 if (file->f_ra.mmap_miss > 0)
2579 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002580
Jan Kara82b0f8c2016-12-14 15:06:58 -08002581 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2582 if (vmf->pte)
2583 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002584 last_pgoff = iter.index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002585 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002586 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002587 unlock_page(page);
2588 goto next;
2589unlock:
2590 unlock_page(page);
2591skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002592 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002593next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002594 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002595 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002596 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002597 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002598 break;
2599 }
2600 rcu_read_unlock();
2601}
2602EXPORT_SYMBOL(filemap_map_pages);
2603
Dave Jiang11bac802017-02-24 14:56:41 -08002604int filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002605{
2606 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002607 struct inode *inode = file_inode(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002608 int ret = VM_FAULT_LOCKED;
2609
Jan Kara14da9202012-06-12 16:20:37 +02002610 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002611 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002612 lock_page(page);
2613 if (page->mapping != inode->i_mapping) {
2614 unlock_page(page);
2615 ret = VM_FAULT_NOPAGE;
2616 goto out;
2617 }
Jan Kara14da9202012-06-12 16:20:37 +02002618 /*
2619 * We mark the page dirty already here so that when freeze is in
2620 * progress, we are guaranteed that writeback during freezing will
2621 * see the dirty page and writeprotect it again.
2622 */
2623 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002624 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002625out:
Jan Kara14da9202012-06-12 16:20:37 +02002626 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002627 return ret;
2628}
2629EXPORT_SYMBOL(filemap_page_mkwrite);
2630
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002631const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002632 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002633 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002634 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635};
2636
2637/* This is used for a general mmap of a disk file */
2638
2639int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2640{
2641 struct address_space *mapping = file->f_mapping;
2642
2643 if (!mapping->a_ops->readpage)
2644 return -ENOEXEC;
2645 file_accessed(file);
2646 vma->vm_ops = &generic_file_vm_ops;
2647 return 0;
2648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650/*
2651 * This is for filesystems which do not implement ->writepage.
2652 */
2653int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2654{
2655 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2656 return -EINVAL;
2657 return generic_file_mmap(file, vma);
2658}
2659#else
2660int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2661{
2662 return -ENOSYS;
2663}
2664int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2665{
2666 return -ENOSYS;
2667}
2668#endif /* CONFIG_MMU */
2669
2670EXPORT_SYMBOL(generic_file_mmap);
2671EXPORT_SYMBOL(generic_file_readonly_mmap);
2672
Sasha Levin67f9fd92014-04-03 14:48:18 -07002673static struct page *wait_on_page_read(struct page *page)
2674{
2675 if (!IS_ERR(page)) {
2676 wait_on_page_locked(page);
2677 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002678 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002679 page = ERR_PTR(-EIO);
2680 }
2681 }
2682 return page;
2683}
2684
Mel Gorman32b63522016-03-15 14:55:36 -07002685static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002686 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002687 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002688 void *data,
2689 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Nick Piggineb2be182007-10-16 01:24:57 -07002691 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 int err;
2693repeat:
2694 page = find_get_page(mapping, index);
2695 if (!page) {
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002696 page = __page_cache_alloc(gfp | __GFP_COLD);
Nick Piggineb2be182007-10-16 01:24:57 -07002697 if (!page)
2698 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002699 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002700 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002701 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002702 if (err == -EEXIST)
2703 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return ERR_PTR(err);
2706 }
Mel Gorman32b63522016-03-15 14:55:36 -07002707
2708filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 err = filler(data, page);
2710 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002711 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002712 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Mel Gorman32b63522016-03-15 14:55:36 -07002714
2715 page = wait_on_page_read(page);
2716 if (IS_ERR(page))
2717 return page;
2718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (PageUptodate(page))
2721 goto out;
2722
Mel Gormanebded022016-03-15 14:55:39 -07002723 /*
2724 * Page is not up to date and may be locked due one of the following
2725 * case a: Page is being filled and the page lock is held
2726 * case b: Read/write error clearing the page uptodate status
2727 * case c: Truncation in progress (page locked)
2728 * case d: Reclaim in progress
2729 *
2730 * Case a, the page will be up to date when the page is unlocked.
2731 * There is no need to serialise on the page lock here as the page
2732 * is pinned so the lock gives no additional protection. Even if the
2733 * the page is truncated, the data is still valid if PageUptodate as
2734 * it's a race vs truncate race.
2735 * Case b, the page will not be up to date
2736 * Case c, the page may be truncated but in itself, the data may still
2737 * be valid after IO completes as it's a read vs truncate race. The
2738 * operation must restart if the page is not uptodate on unlock but
2739 * otherwise serialising on page lock to stabilise the mapping gives
2740 * no additional guarantees to the caller as the page lock is
2741 * released before return.
2742 * Case d, similar to truncation. If reclaim holds the page lock, it
2743 * will be a race with remove_mapping that determines if the mapping
2744 * is valid on unlock but otherwise the data is valid and there is
2745 * no need to serialise with page lock.
2746 *
2747 * As the page lock gives no additional guarantee, we optimistically
2748 * wait on the page to be unlocked and check if it's up to date and
2749 * use the page if it is. Otherwise, the page lock is required to
2750 * distinguish between the different cases. The motivation is that we
2751 * avoid spurious serialisations and wakeups when multiple processes
2752 * wait on the same page for IO to complete.
2753 */
2754 wait_on_page_locked(page);
2755 if (PageUptodate(page))
2756 goto out;
2757
2758 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002760
2761 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (!page->mapping) {
2763 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002764 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002765 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Mel Gormanebded022016-03-15 14:55:39 -07002767
2768 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (PageUptodate(page)) {
2770 unlock_page(page);
2771 goto out;
2772 }
Mel Gorman32b63522016-03-15 14:55:36 -07002773 goto filler;
2774
David Howellsc855ff32007-05-09 13:42:20 +01002775out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002776 mark_page_accessed(page);
2777 return page;
2778}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002779
2780/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002781 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002782 * @mapping: the page's address_space
2783 * @index: the page index
2784 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002785 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002786 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002787 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002788 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002789 *
2790 * If the page does not get brought uptodate, return -EIO.
2791 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002792struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002793 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002794 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002795 void *data)
2796{
2797 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2798}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002799EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002800
2801/**
2802 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2803 * @mapping: the page's address_space
2804 * @index: the page index
2805 * @gfp: the page allocator flags to use if allocating
2806 *
2807 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002808 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002809 *
2810 * If the page does not get brought uptodate, return -EIO.
2811 */
2812struct page *read_cache_page_gfp(struct address_space *mapping,
2813 pgoff_t index,
2814 gfp_t gfp)
2815{
2816 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2817
Sasha Levin67f9fd92014-04-03 14:48:18 -07002818 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002819}
2820EXPORT_SYMBOL(read_cache_page_gfp);
2821
Nick Piggin2f718ff2007-10-16 01:24:59 -07002822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 * Performs necessary checks before doing a write
2824 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002825 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 * Returns appropriate error code that caller should return or
2827 * zero in case that write should be allowed.
2828 */
Al Viro3309dd02015-04-09 12:55:47 -04002829inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Al Viro3309dd02015-04-09 12:55:47 -04002831 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002833 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002834 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Al Viro3309dd02015-04-09 12:55:47 -04002836 if (!iov_iter_count(from))
2837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Al Viro0fa6b002015-04-04 04:05:48 -04002839 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002840 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002841 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Al Viro3309dd02015-04-09 12:55:47 -04002843 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002845 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2846 return -EINVAL;
2847
Al Viro0fa6b002015-04-04 04:05:48 -04002848 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002849 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002850 send_sig(SIGXFSZ, current, 0);
2851 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Al Viro3309dd02015-04-09 12:55:47 -04002853 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855
2856 /*
2857 * LFS rule
2858 */
Al Viro3309dd02015-04-09 12:55:47 -04002859 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002861 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002863 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
2866 /*
2867 * Are we about to exceed the fs block limit ?
2868 *
2869 * If we have written data it becomes a short write. If we have
2870 * exceeded without writing data we send a signal and return EFBIG.
2871 * Linus frestrict idea will clean these up nicely..
2872 */
Al Viro3309dd02015-04-09 12:55:47 -04002873 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2874 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Al Viro3309dd02015-04-09 12:55:47 -04002876 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2877 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879EXPORT_SYMBOL(generic_write_checks);
2880
Nick Pigginafddba42007-10-16 01:25:01 -07002881int pagecache_write_begin(struct file *file, struct address_space *mapping,
2882 loff_t pos, unsigned len, unsigned flags,
2883 struct page **pagep, void **fsdata)
2884{
2885 const struct address_space_operations *aops = mapping->a_ops;
2886
Nick Piggin4e02ed42008-10-29 14:00:55 -07002887 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002888 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002889}
2890EXPORT_SYMBOL(pagecache_write_begin);
2891
2892int pagecache_write_end(struct file *file, struct address_space *mapping,
2893 loff_t pos, unsigned len, unsigned copied,
2894 struct page *page, void *fsdata)
2895{
2896 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002897
Nick Piggin4e02ed42008-10-29 14:00:55 -07002898 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002899}
2900EXPORT_SYMBOL(pagecache_write_end);
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002903generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 struct file *file = iocb->ki_filp;
2906 struct address_space *mapping = file->f_mapping;
2907 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002908 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002910 size_t write_len;
2911 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Al Viro0c949332014-03-22 06:51:37 -04002913 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002915
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002916 if (iocb->ki_flags & IOCB_NOWAIT) {
2917 /* If there are pages to writeback, return */
2918 if (filemap_range_has_page(inode->i_mapping, pos,
2919 pos + iov_iter_count(from)))
2920 return -EAGAIN;
2921 } else {
2922 written = filemap_write_and_wait_range(mapping, pos,
2923 pos + write_len - 1);
2924 if (written)
2925 goto out;
2926 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002927
2928 /*
2929 * After a write we want buffered reads to be sure to go to disk to get
2930 * the new data. We invalidate clean cached page from the region we're
2931 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002932 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002933 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07002934 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002935 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07002936 /*
2937 * If a page can not be invalidated, return 0 to fall back
2938 * to buffered write.
2939 */
2940 if (written) {
2941 if (written == -EBUSY)
2942 return 0;
2943 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002944 }
2945
Al Viro639a93a52017-04-13 14:10:15 -04002946 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002947
2948 /*
2949 * Finally, try again to invalidate clean pages which might have been
2950 * cached by non-direct readahead, or faulted in by get_user_pages()
2951 * if the source of the write was an mmap'ed region of the file
2952 * we're writing. Either one is a pretty crazy thing to do,
2953 * so we don't support it 100%. If this invalidation
2954 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06002955 *
2956 * Most of the time we do not need this since dio_complete() will do
2957 * the invalidation for us. However there are some file systems that
2958 * do not end up with dio_complete() being called, so let's not break
2959 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07002960 */
Lukas Czerner332391a2017-09-21 08:16:29 -06002961 if (mapping->nrpages)
2962 invalidate_inode_pages2_range(mapping,
2963 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002966 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04002967 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07002968 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2969 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 mark_inode_dirty(inode);
2971 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002972 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
Al Viro639a93a52017-04-13 14:10:15 -04002974 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07002975out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return written;
2977}
2978EXPORT_SYMBOL(generic_file_direct_write);
2979
Nick Piggineb2be182007-10-16 01:24:57 -07002980/*
2981 * Find or create a page at the given pagecache position. Return the locked
2982 * page. This function is specifically for buffered writes.
2983 */
Nick Piggin54566b22009-01-04 12:00:53 -08002984struct page *grab_cache_page_write_begin(struct address_space *mapping,
2985 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002986{
Nick Piggineb2be182007-10-16 01:24:57 -07002987 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07002988 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002989
Nick Piggin54566b22009-01-04 12:00:53 -08002990 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002991 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002992
Mel Gorman2457aec2014-06-04 16:10:31 -07002993 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002994 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002995 if (page)
2996 wait_for_stable_page(page);
2997
Nick Piggineb2be182007-10-16 01:24:57 -07002998 return page;
2999}
Nick Piggin54566b22009-01-04 12:00:53 -08003000EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003001
Al Viro3b93f912014-02-11 21:34:08 -05003002ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003003 struct iov_iter *i, loff_t pos)
3004{
3005 struct address_space *mapping = file->f_mapping;
3006 const struct address_space_operations *a_ops = mapping->a_ops;
3007 long status = 0;
3008 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003009 unsigned int flags = 0;
3010
Nick Pigginafddba42007-10-16 01:25:01 -07003011 do {
3012 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003013 unsigned long offset; /* Offset into pagecache page */
3014 unsigned long bytes; /* Bytes to write to page */
3015 size_t copied; /* Bytes copied from user */
3016 void *fsdata;
3017
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003018 offset = (pos & (PAGE_SIZE - 1));
3019 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003020 iov_iter_count(i));
3021
3022again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003023 /*
3024 * Bring in the user page that we will copy from _first_.
3025 * Otherwise there's a nasty deadlock on copying from the
3026 * same page as we're writing to, without it being marked
3027 * up-to-date.
3028 *
3029 * Not only is this an optimisation, but it is also required
3030 * to check that the address is actually valid, when atomic
3031 * usercopies are used, below.
3032 */
3033 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3034 status = -EFAULT;
3035 break;
3036 }
3037
Jan Kara296291c2015-10-22 13:32:21 -07003038 if (fatal_signal_pending(current)) {
3039 status = -EINTR;
3040 break;
3041 }
3042
Nick Piggin674b8922007-10-16 01:25:03 -07003043 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003044 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003045 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003046 break;
3047
anfei zhou931e80e2010-02-02 13:44:02 -08003048 if (mapping_writably_mapped(mapping))
3049 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003050
Nick Pigginafddba42007-10-16 01:25:01 -07003051 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003052 flush_dcache_page(page);
3053
3054 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3055 page, fsdata);
3056 if (unlikely(status < 0))
3057 break;
3058 copied = status;
3059
3060 cond_resched();
3061
Nick Piggin124d3b72008-02-02 15:01:17 +01003062 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003063 if (unlikely(copied == 0)) {
3064 /*
3065 * If we were unable to copy any data at all, we must
3066 * fall back to a single segment length write.
3067 *
3068 * If we didn't fallback here, we could livelock
3069 * because not all segments in the iov can be copied at
3070 * once without a pagefault.
3071 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003072 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003073 iov_iter_single_seg_count(i));
3074 goto again;
3075 }
Nick Pigginafddba42007-10-16 01:25:01 -07003076 pos += copied;
3077 written += copied;
3078
3079 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003080 } while (iov_iter_count(i));
3081
3082 return written ? written : status;
3083}
Al Viro3b93f912014-02-11 21:34:08 -05003084EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Jan Karae4dd9de2009-08-17 18:10:06 +02003086/**
Al Viro81742022014-04-03 03:17:43 -04003087 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003088 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003089 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003090 *
3091 * This function does all the work needed for actually writing data to a
3092 * file. It does all basic checks, removes SUID from the file, updates
3093 * modification times and calls proper subroutines depending on whether we
3094 * do direct IO or a standard buffered write.
3095 *
3096 * It expects i_mutex to be grabbed unless we work on a block device or similar
3097 * object which does not need locking at all.
3098 *
3099 * This function does *not* take care of syncing data in case of O_SYNC write.
3100 * A caller has to handle it. This is mainly due to the fact that we want to
3101 * avoid syncing under i_mutex.
3102 */
Al Viro81742022014-04-03 03:17:43 -04003103ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
3105 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003106 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003108 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003110 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003113 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003114 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (err)
3116 goto out;
3117
Josef Bacikc3b2da32012-03-26 09:59:21 -04003118 err = file_update_time(file);
3119 if (err)
3120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Al Viro2ba48ce2015-04-09 13:52:01 -04003122 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003123 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003124
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003125 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003126 /*
3127 * If the write stopped short of completing, fall back to
3128 * buffered writes. Some filesystems do this for writes to
3129 * holes, for example. For DAX files, a buffered write will
3130 * not succeed (even if it did, DAX does not handle dirty
3131 * page-cache pages correctly).
3132 */
Al Viro0b8def92015-04-07 10:22:53 -04003133 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003135
Al Viro0b8def92015-04-07 10:22:53 -04003136 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003137 /*
Al Viro3b93f912014-02-11 21:34:08 -05003138 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003139 * then we want to return the number of bytes which were
3140 * direct-written, or the error code if that was zero. Note
3141 * that this differs from normal direct-io semantics, which
3142 * will return -EFOO even if some bytes were written.
3143 */
Al Viro60bb4522014-08-08 12:39:16 -04003144 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003145 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003146 goto out;
3147 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003148 /*
3149 * We need to ensure that the page cache pages are written to
3150 * disk and invalidated to preserve the expected O_DIRECT
3151 * semantics.
3152 */
Al Viro3b93f912014-02-11 21:34:08 -05003153 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003154 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003155 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003156 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003157 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003158 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003159 pos >> PAGE_SHIFT,
3160 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003161 } else {
3162 /*
3163 * We don't know how much we wrote, so just return
3164 * the number of bytes which were direct-written
3165 */
3166 }
3167 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003168 written = generic_perform_write(file, from, iocb->ki_pos);
3169 if (likely(written > 0))
3170 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172out:
3173 current->backing_dev_info = NULL;
3174 return written ? written : err;
3175}
Al Viro81742022014-04-03 03:17:43 -04003176EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Jan Karae4dd9de2009-08-17 18:10:06 +02003178/**
Al Viro81742022014-04-03 03:17:43 -04003179 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003180 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003181 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003182 *
Al Viro81742022014-04-03 03:17:43 -04003183 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003184 * filesystems. It takes care of syncing the file in case of O_SYNC file
3185 * and acquires i_mutex as needed.
3186 */
Al Viro81742022014-04-03 03:17:43 -04003187ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
3189 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003190 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Al Viro59551022016-01-22 15:40:57 -05003193 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003194 ret = generic_write_checks(iocb, from);
3195 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003196 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003197 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Christoph Hellwige2592212016-04-07 08:52:01 -07003199 if (ret > 0)
3200 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return ret;
3202}
Al Viro81742022014-04-03 03:17:43 -04003203EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
David Howellscf9a2ae2006-08-29 19:05:54 +01003205/**
3206 * try_to_release_page() - release old fs-specific metadata on a page
3207 *
3208 * @page: the page which the kernel is trying to free
3209 * @gfp_mask: memory allocation flags (and I/O mode)
3210 *
3211 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003212 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003213 * Otherwise return zero.
3214 *
David Howells266cf652009-04-03 16:42:36 +01003215 * This may also be called if PG_fscache is set on a page, indicating that the
3216 * page is known to the local caching routines.
3217 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003218 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003219 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003220 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003221 */
3222int try_to_release_page(struct page *page, gfp_t gfp_mask)
3223{
3224 struct address_space * const mapping = page->mapping;
3225
3226 BUG_ON(!PageLocked(page));
3227 if (PageWriteback(page))
3228 return 0;
3229
3230 if (mapping && mapping->a_ops->releasepage)
3231 return mapping->a_ops->releasepage(page, gfp_mask);
3232 return try_to_free_buffers(page);
3233}
3234
3235EXPORT_SYMBOL(try_to_release_page);