blob: f7f9af1d98b01046d9013c1abcd4e5ccf9638c03 [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>
Johannes Weiner00501b52014-08-08 14:19:20 -070034#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060036#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080037#include <linux/shmem_fs.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)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070069 * ->i_pages 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)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070077 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
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)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070087 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
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)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070098 * ->i_pages 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)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700102 * ->i_pages 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
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500114static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700115 struct page *page, void *shadow)
116{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500117 XA_STATE(xas, &mapping->i_pages, page->index);
118 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800119
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500120 mapping_set_update(&xas, mapping);
121
122 /* hugetlb pages are represented by a single entry in the xarray */
123 if (!PageHuge(page)) {
124 xas_set_order(&xas, page->index, compound_order(page));
125 nr = 1U << compound_order(page);
126 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700127
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700128 VM_BUG_ON_PAGE(!PageLocked(page), page);
129 VM_BUG_ON_PAGE(PageTail(page), page);
130 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700131
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500132 xas_store(&xas, shadow);
133 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200134
Jan Kara23006382017-11-15 17:37:26 -0800135 page->mapping = NULL;
136 /* Leave page->index set: truncation lookup relies upon it */
137
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200138 if (shadow) {
139 mapping->nrexceptional += nr;
140 /*
141 * Make sure the nrexceptional update is committed before
142 * the nrpages update so that final truncate racing
143 * with reclaim does not see both counters 0 at the
144 * same time and miss a shadow entry.
145 */
146 smp_wmb();
147 }
148 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700149}
150
Jan Kara5ecc4d82017-11-15 17:37:29 -0800151static void unaccount_page_cache_page(struct address_space *mapping,
152 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800154 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600156 /*
157 * if we're uptodate, flush out into the cleancache, otherwise
158 * invalidate any existing cleancache entries. We can't leave
159 * stale data around in the cleancache once our page is gone
160 */
161 if (PageUptodate(page) && PageMappedToDisk(page))
162 cleancache_put_page(page);
163 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400164 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600165
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700166 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800167 VM_BUG_ON_PAGE(page_mapped(page), page);
168 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
169 int mapcount;
170
171 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
172 current->comm, page_to_pfn(page));
173 dump_page(page, "still mapped when deleted");
174 dump_stack();
175 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
176
177 mapcount = page_mapcount(page);
178 if (mapping_exiting(mapping) &&
179 page_count(page) >= mapcount + 2) {
180 /*
181 * All vmas have already been torn down, so it's
182 * a good bet that actually the page is unmapped,
183 * and we'd prefer not to leak it: if we're wrong,
184 * some other bad page check should catch it later.
185 */
186 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700187 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800188 }
189 }
190
Jan Kara76253fb2017-11-15 17:37:22 -0800191 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800192 if (PageHuge(page))
193 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800194
Jan Kara5ecc4d82017-11-15 17:37:29 -0800195 nr = hpage_nr_pages(page);
196
197 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
198 if (PageSwapBacked(page)) {
199 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
200 if (PageTransHuge(page))
201 __dec_node_page_state(page, NR_SHMEM_THPS);
202 } else {
203 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800204 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800205
206 /*
207 * At this point page must be either written or cleaned by
208 * truncate. Dirty page here signals a bug and loss of
209 * unwritten data.
210 *
211 * This fixes dirty accounting after removing the page entirely
212 * but leaves PageDirty set: it has no effect for truncated
213 * page and anyway will be cleared before returning page into
214 * buddy allocator.
215 */
216 if (WARN_ON_ONCE(PageDirty(page)))
217 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
218}
219
220/*
221 * Delete a page from the page cache and free it. Caller has to make
222 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700223 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800224 */
225void __delete_from_page_cache(struct page *page, void *shadow)
226{
227 struct address_space *mapping = page->mapping;
228
229 trace_mm_filemap_delete_from_page_cache(page);
230
231 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500232 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Jan Kara59c66c52017-11-15 17:37:18 -0800235static void page_cache_free_page(struct address_space *mapping,
236 struct page *page)
237{
238 void (*freepage)(struct page *);
239
240 freepage = mapping->a_ops->freepage;
241 if (freepage)
242 freepage(page);
243
244 if (PageTransHuge(page) && !PageHuge(page)) {
245 page_ref_sub(page, HPAGE_PMD_NR);
246 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
247 } else {
248 put_page(page);
249 }
250}
251
Minchan Kim702cfbf2011-03-22 16:32:43 -0700252/**
253 * delete_from_page_cache - delete page from page cache
254 * @page: the page which the kernel is trying to remove from page cache
255 *
256 * This must be called only on pages that have been verified to be in the page
257 * cache and locked. It will never put the page into the free list, the caller
258 * has a reference on the page.
259 */
260void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700262 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400263 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Matt Mackallcd7619d2005-05-01 08:59:01 -0700265 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700266 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700267 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700268 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500269
Jan Kara59c66c52017-11-15 17:37:18 -0800270 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700271}
272EXPORT_SYMBOL(delete_from_page_cache);
273
Jan Karaaa65c292017-11-15 17:37:33 -0800274/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500275 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800276 * @mapping: the mapping to which pages belong
277 * @pvec: pagevec with pages to delete
278 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700279 * The function walks over mapping->i_pages and removes pages passed in @pvec
280 * from the mapping. The function expects @pvec to be sorted by page index.
281 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800282 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700283 * @pvec and takes care to delete all corresponding tail pages from the
284 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800285 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700286 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800287 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500288static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800289 struct pagevec *pvec)
290{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500291 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800292 int total_pages = 0;
293 int i = 0, tail_pages = 0;
294 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800295
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500296 mapping_set_update(&xas, mapping);
297 xas_for_each(&xas, page, ULONG_MAX) {
Jan Karaaa65c292017-11-15 17:37:33 -0800298 if (i >= pagevec_count(pvec) && !tail_pages)
299 break;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400300 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800301 continue;
302 if (!tail_pages) {
303 /*
304 * Some page got inserted in our range? Skip it. We
305 * have our pages locked so they are protected from
306 * being removed.
307 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500308 if (page != pvec->pages[i]) {
309 VM_BUG_ON_PAGE(page->index >
310 pvec->pages[i]->index, page);
Jan Karaaa65c292017-11-15 17:37:33 -0800311 continue;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500312 }
Jan Karaaa65c292017-11-15 17:37:33 -0800313 WARN_ON_ONCE(!PageLocked(page));
314 if (PageTransHuge(page) && !PageHuge(page))
315 tail_pages = HPAGE_PMD_NR - 1;
316 page->mapping = NULL;
317 /*
318 * Leave page->index set: truncation lookup relies
319 * upon it
320 */
321 i++;
322 } else {
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500323 VM_BUG_ON_PAGE(page->index + HPAGE_PMD_NR - tail_pages
324 != pvec->pages[i]->index, page);
Jan Karaaa65c292017-11-15 17:37:33 -0800325 tail_pages--;
326 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500327 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800328 total_pages++;
329 }
330 mapping->nrpages -= total_pages;
331}
332
333void delete_from_page_cache_batch(struct address_space *mapping,
334 struct pagevec *pvec)
335{
336 int i;
337 unsigned long flags;
338
339 if (!pagevec_count(pvec))
340 return;
341
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700342 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800343 for (i = 0; i < pagevec_count(pvec); i++) {
344 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
345
346 unaccount_page_cache_page(mapping, pvec->pages[i]);
347 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500348 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700349 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800350
351 for (i = 0; i < pagevec_count(pvec); i++)
352 page_cache_free_page(mapping, pvec->pages[i]);
353}
354
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200355int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700356{
357 int ret = 0;
358 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700359 if (test_bit(AS_ENOSPC, &mapping->flags) &&
360 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700361 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700362 if (test_bit(AS_EIO, &mapping->flags) &&
363 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700364 ret = -EIO;
365 return ret;
366}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200367EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700368
Jeff Layton76341ca2017-07-06 07:02:22 -0400369static int filemap_check_and_keep_errors(struct address_space *mapping)
370{
371 /* Check for outstanding write errors */
372 if (test_bit(AS_EIO, &mapping->flags))
373 return -EIO;
374 if (test_bit(AS_ENOSPC, &mapping->flags))
375 return -ENOSPC;
376 return 0;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700380 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700381 * @mapping: address space structure to write
382 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800383 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700384 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700386 * Start writeback against all of a mapping's dirty pages that lie
387 * within the byte offsets <start, end> inclusive.
388 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700390 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * these two operations is that if a dirty page/buffer is encountered, it must
392 * be waited upon, and not just skipped over.
393 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800394int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
395 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 int ret;
398 struct writeback_control wbc = {
399 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800400 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700401 .range_start = start,
402 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 };
404
405 if (!mapping_cap_writeback_dirty(mapping))
406 return 0;
407
Tejun Heob16b1de2015-06-02 08:39:48 -0600408 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600410 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return ret;
412}
413
414static inline int __filemap_fdatawrite(struct address_space *mapping,
415 int sync_mode)
416{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700417 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420int filemap_fdatawrite(struct address_space *mapping)
421{
422 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
423}
424EXPORT_SYMBOL(filemap_fdatawrite);
425
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400426int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800427 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
430}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400431EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Randy Dunlap485bb992006-06-23 02:03:49 -0700433/**
434 * filemap_flush - mostly a non-blocking flush
435 * @mapping: target address_space
436 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * This is a mostly non-blocking flush. Not suitable for data-integrity
438 * purposes - I/O may not be started against all dirty pages.
439 */
440int filemap_flush(struct address_space *mapping)
441{
442 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
443}
444EXPORT_SYMBOL(filemap_flush);
445
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500446/**
447 * filemap_range_has_page - check if a page exists in range.
448 * @mapping: address space within which to check
449 * @start_byte: offset in bytes where the range starts
450 * @end_byte: offset in bytes where the range ends (inclusive)
451 *
452 * Find at least one page in the range supplied, usually used to check if
453 * direct writing in this range will trigger a writeback.
454 */
455bool filemap_range_has_page(struct address_space *mapping,
456 loff_t start_byte, loff_t end_byte)
457{
458 pgoff_t index = start_byte >> PAGE_SHIFT;
459 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700460 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500461
462 if (end_byte < start_byte)
463 return false;
464
465 if (mapping->nrpages == 0)
466 return false;
467
Jan Karaf7b68042017-09-06 16:21:40 -0700468 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500469 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700470 put_page(page);
471 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500472}
473EXPORT_SYMBOL(filemap_range_has_page);
474
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400475static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800476 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300478 pgoff_t index = start_byte >> PAGE_SHIFT;
479 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct pagevec pvec;
481 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200483 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Mel Gorman86679822017-11-15 17:37:52 -0800486 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800487 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unsigned i;
489
Jan Kara312e9d22017-11-15 17:35:05 -0800490 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800491 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800492 if (!nr_pages)
493 break;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 for (i = 0; i < nr_pages; i++) {
496 struct page *page = pvec.pages[i];
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400499 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 pagevec_release(&pvec);
502 cond_resched();
503 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800504}
505
506/**
507 * filemap_fdatawait_range - wait for writeback to complete
508 * @mapping: address space structure to wait for
509 * @start_byte: offset in bytes where the range starts
510 * @end_byte: offset in bytes where the range ends (inclusive)
511 *
512 * Walk the list of under-writeback pages of the given address space
513 * in the given range and wait for all of them. Check error status of
514 * the address space and return it.
515 *
516 * Since the error status of the address space is cleared by this function,
517 * callers are responsible for checking the return value and handling and/or
518 * reporting the error.
519 */
520int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
521 loff_t end_byte)
522{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400523 __filemap_fdatawait_range(mapping, start_byte, end_byte);
524 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200526EXPORT_SYMBOL(filemap_fdatawait_range);
527
528/**
Jeff Laytona823e452017-07-28 07:24:43 -0400529 * file_fdatawait_range - wait for writeback to complete
530 * @file: file pointing to address space structure to wait for
531 * @start_byte: offset in bytes where the range starts
532 * @end_byte: offset in bytes where the range ends (inclusive)
533 *
534 * Walk the list of under-writeback pages of the address space that file
535 * refers to, in the given range and wait for all of them. Check error
536 * status of the address space vs. the file->f_wb_err cursor and return it.
537 *
538 * Since the error status of the file is advanced by this function,
539 * callers are responsible for checking the return value and handling and/or
540 * reporting the error.
541 */
542int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
543{
544 struct address_space *mapping = file->f_mapping;
545
546 __filemap_fdatawait_range(mapping, start_byte, end_byte);
547 return file_check_and_advance_wb_err(file);
548}
549EXPORT_SYMBOL(file_fdatawait_range);
550
551/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800552 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
553 * @mapping: address space structure to wait for
554 *
555 * Walk the list of under-writeback pages of the given address space
556 * and wait for all of them. Unlike filemap_fdatawait(), this function
557 * does not clear error status of the address space.
558 *
559 * Use this function if callers don't handle errors themselves. Expected
560 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
561 * fsfreeze(8)
562 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400563int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800564{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400565 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400566 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800567}
Jeff Layton76341ca2017-07-06 07:02:22 -0400568EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800569
Jeff Layton9326c9b2017-07-26 10:21:11 -0400570static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400572 return (!dax_mapping(mapping) && mapping->nrpages) ||
573 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576int filemap_write_and_wait(struct address_space *mapping)
577{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800578 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jeff Layton9326c9b2017-07-26 10:21:11 -0400580 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800581 err = filemap_fdatawrite(mapping);
582 /*
583 * Even if the above returned error, the pages may be
584 * written partially (e.g. -ENOSPC), so we wait for it.
585 * But the -EIO is special case, it may indicate the worst
586 * thing (e.g. bug) happened, so we avoid waiting for it.
587 */
588 if (err != -EIO) {
589 int err2 = filemap_fdatawait(mapping);
590 if (!err)
591 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400592 } else {
593 /* Clear any previously stored errors */
594 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800595 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700596 } else {
597 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800599 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800601EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Randy Dunlap485bb992006-06-23 02:03:49 -0700603/**
604 * filemap_write_and_wait_range - write out & wait on a file range
605 * @mapping: the address_space for the pages
606 * @lstart: offset in bytes where the range starts
607 * @lend: offset in bytes where the range ends (inclusive)
608 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800609 * Write out and wait upon file offsets lstart->lend, inclusive.
610 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300611 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800612 * that this function can be used to write to the very end-of-file (end = -1).
613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614int filemap_write_and_wait_range(struct address_space *mapping,
615 loff_t lstart, loff_t lend)
616{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800617 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jeff Layton9326c9b2017-07-26 10:21:11 -0400619 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800620 err = __filemap_fdatawrite_range(mapping, lstart, lend,
621 WB_SYNC_ALL);
622 /* See comment of filemap_write_and_wait() */
623 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200624 int err2 = filemap_fdatawait_range(mapping,
625 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800626 if (!err)
627 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400628 } else {
629 /* Clear any previously stored errors */
630 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800631 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700632 } else {
633 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Chris Masonf6995582009-04-15 13:22:37 -0400637EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jeff Layton5660e132017-07-06 07:02:25 -0400639void __filemap_set_wb_err(struct address_space *mapping, int err)
640{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400641 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400642
643 trace_filemap_set_wb_err(mapping, eseq);
644}
645EXPORT_SYMBOL(__filemap_set_wb_err);
646
647/**
648 * file_check_and_advance_wb_err - report wb error (if any) that was previously
649 * and advance wb_err to current one
650 * @file: struct file on which the error is being reported
651 *
652 * When userland calls fsync (or something like nfsd does the equivalent), we
653 * want to report any writeback errors that occurred since the last fsync (or
654 * since the file was opened if there haven't been any).
655 *
656 * Grab the wb_err from the mapping. If it matches what we have in the file,
657 * then just quickly return 0. The file is all caught up.
658 *
659 * If it doesn't match, then take the mapping value, set the "seen" flag in
660 * it and try to swap it into place. If it works, or another task beat us
661 * to it with the new value, then update the f_wb_err and return the error
662 * portion. The error at this point must be reported via proper channels
663 * (a'la fsync, or NFS COMMIT operation, etc.).
664 *
665 * While we handle mapping->wb_err with atomic operations, the f_wb_err
666 * value is protected by the f_lock since we must ensure that it reflects
667 * the latest value swapped in for this file descriptor.
668 */
669int file_check_and_advance_wb_err(struct file *file)
670{
671 int err = 0;
672 errseq_t old = READ_ONCE(file->f_wb_err);
673 struct address_space *mapping = file->f_mapping;
674
675 /* Locklessly handle the common case where nothing has changed */
676 if (errseq_check(&mapping->wb_err, old)) {
677 /* Something changed, must use slow path */
678 spin_lock(&file->f_lock);
679 old = file->f_wb_err;
680 err = errseq_check_and_advance(&mapping->wb_err,
681 &file->f_wb_err);
682 trace_file_check_and_advance_wb_err(file, old);
683 spin_unlock(&file->f_lock);
684 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700685
686 /*
687 * We're mostly using this function as a drop in replacement for
688 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
689 * that the legacy code would have had on these flags.
690 */
691 clear_bit(AS_EIO, &mapping->flags);
692 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400693 return err;
694}
695EXPORT_SYMBOL(file_check_and_advance_wb_err);
696
697/**
698 * file_write_and_wait_range - write out & wait on a file range
699 * @file: file pointing to address_space with pages
700 * @lstart: offset in bytes where the range starts
701 * @lend: offset in bytes where the range ends (inclusive)
702 *
703 * Write out and wait upon file offsets lstart->lend, inclusive.
704 *
705 * Note that @lend is inclusive (describes the last byte to be written) so
706 * that this function can be used to write to the very end-of-file (end = -1).
707 *
708 * After writing out and waiting on the data, we check and advance the
709 * f_wb_err cursor to the latest value, and return any errors detected there.
710 */
711int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
712{
713 int err = 0, err2;
714 struct address_space *mapping = file->f_mapping;
715
Jeff Layton9326c9b2017-07-26 10:21:11 -0400716 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400717 err = __filemap_fdatawrite_range(mapping, lstart, lend,
718 WB_SYNC_ALL);
719 /* See comment of filemap_write_and_wait() */
720 if (err != -EIO)
721 __filemap_fdatawait_range(mapping, lstart, lend);
722 }
723 err2 = file_check_and_advance_wb_err(file);
724 if (!err)
725 err = err2;
726 return err;
727}
728EXPORT_SYMBOL(file_write_and_wait_range);
729
Randy Dunlap485bb992006-06-23 02:03:49 -0700730/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700731 * replace_page_cache_page - replace a pagecache page with a new one
732 * @old: page to be replaced
733 * @new: page to replace with
734 * @gfp_mask: allocation mode
735 *
736 * This function replaces a page in the pagecache with a new one. On
737 * success it acquires the pagecache reference for the new page and
738 * drops it for the old page. Both the old and new pages must be
739 * locked. This function does not add the new page to the LRU, the
740 * caller must do that.
741 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500742 * The remove + add is atomic. This function cannot fail.
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700743 */
744int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
745{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500746 struct address_space *mapping = old->mapping;
747 void (*freepage)(struct page *) = mapping->a_ops->freepage;
748 pgoff_t offset = old->index;
749 XA_STATE(xas, &mapping->i_pages, offset);
750 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700751
Sasha Levin309381fea2014-01-23 15:52:54 -0800752 VM_BUG_ON_PAGE(!PageLocked(old), old);
753 VM_BUG_ON_PAGE(!PageLocked(new), new);
754 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700755
Matthew Wilcox74d60952017-11-17 10:01:45 -0500756 get_page(new);
757 new->mapping = mapping;
758 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700759
Matthew Wilcox74d60952017-11-17 10:01:45 -0500760 xas_lock_irqsave(&xas, flags);
761 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700762
Matthew Wilcox74d60952017-11-17 10:01:45 -0500763 old->mapping = NULL;
764 /* hugetlb pages do not participate in page cache accounting. */
765 if (!PageHuge(old))
766 __dec_node_page_state(new, NR_FILE_PAGES);
767 if (!PageHuge(new))
768 __inc_node_page_state(new, NR_FILE_PAGES);
769 if (PageSwapBacked(old))
770 __dec_node_page_state(new, NR_SHMEM);
771 if (PageSwapBacked(new))
772 __inc_node_page_state(new, NR_SHMEM);
773 xas_unlock_irqrestore(&xas, flags);
774 mem_cgroup_migrate(old, new);
775 if (freepage)
776 freepage(old);
777 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700778
Matthew Wilcox74d60952017-11-17 10:01:45 -0500779 return 0;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700780}
781EXPORT_SYMBOL_GPL(replace_page_cache_page);
782
Johannes Weinera5289102014-04-03 14:47:51 -0700783static int __add_to_page_cache_locked(struct page *page,
784 struct address_space *mapping,
785 pgoff_t offset, gfp_t gfp_mask,
786 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500788 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700789 int huge = PageHuge(page);
790 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700791 int error;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500792 void *old;
Nick Piggine2867812008-07-25 19:45:30 -0700793
Sasha Levin309381fea2014-01-23 15:52:54 -0800794 VM_BUG_ON_PAGE(!PageLocked(page), page);
795 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500796 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700797
Johannes Weiner00501b52014-08-08 14:19:20 -0700798 if (!huge) {
799 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800800 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700801 if (error)
802 return error;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300805 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700806 page->mapping = mapping;
807 page->index = offset;
808
Matthew Wilcox74d60952017-11-17 10:01:45 -0500809 do {
810 xas_lock_irq(&xas);
811 old = xas_load(&xas);
812 if (old && !xa_is_value(old))
813 xas_set_err(&xas, -EEXIST);
814 xas_store(&xas, page);
815 if (xas_error(&xas))
816 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700817
Matthew Wilcox74d60952017-11-17 10:01:45 -0500818 if (xa_is_value(old)) {
819 mapping->nrexceptional--;
820 if (shadowp)
821 *shadowp = old;
822 }
823 mapping->nrpages++;
824
825 /* hugetlb pages do not participate in page cache accounting */
826 if (!huge)
827 __inc_node_page_state(page, NR_FILE_PAGES);
828unlock:
829 xas_unlock_irq(&xas);
830 } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK));
831
832 if (xas_error(&xas))
833 goto error;
834
Johannes Weiner00501b52014-08-08 14:19:20 -0700835 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800836 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700837 trace_mm_filemap_add_to_page_cache(page);
838 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500839error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700840 page->mapping = NULL;
841 /* Leave page->index set: truncation relies upon it */
Johannes Weiner00501b52014-08-08 14:19:20 -0700842 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800843 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300844 put_page(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500845 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
Johannes Weinera5289102014-04-03 14:47:51 -0700847
848/**
849 * add_to_page_cache_locked - add a locked page to the pagecache
850 * @page: page to add
851 * @mapping: the page's address_space
852 * @offset: page index
853 * @gfp_mask: page allocation mode
854 *
855 * This function is used to add a page to the pagecache. It must be locked.
856 * This function does not add the page to the LRU. The caller must do that.
857 */
858int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
859 pgoff_t offset, gfp_t gfp_mask)
860{
861 return __add_to_page_cache_locked(page, mapping, offset,
862 gfp_mask, NULL);
863}
Nick Piggine2867812008-07-25 19:45:30 -0700864EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400867 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Johannes Weinera5289102014-04-03 14:47:51 -0700869 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700870 int ret;
871
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800872 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700873 ret = __add_to_page_cache_locked(page, mapping, offset,
874 gfp_mask, &shadow);
875 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800876 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700877 else {
878 /*
879 * The page might have been evicted from cache only
880 * recently, in which case it should be activated like
881 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700882 * The exception is pages getting rewritten; evicting other
883 * data from the working set, only to cache data that will
884 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700885 */
Rik van Rielf0281a02016-05-20 16:56:25 -0700886 if (!(gfp_mask & __GFP_WRITE) &&
887 shadow && workingset_refault(shadow)) {
Johannes Weinera5289102014-04-03 14:47:51 -0700888 SetPageActive(page);
889 workingset_activation(page);
890 } else
891 ClearPageActive(page);
892 lru_cache_add(page);
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return ret;
895}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300896EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Paul Jackson44110fe2006-03-24 03:16:04 -0800898#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700899struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800900{
Miao Xiec0ff7452010-05-24 14:32:08 -0700901 int n;
902 struct page *page;
903
Paul Jackson44110fe2006-03-24 03:16:04 -0800904 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700905 unsigned int cpuset_mems_cookie;
906 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700907 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700908 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700909 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700910 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700911
Miao Xiec0ff7452010-05-24 14:32:08 -0700912 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800913 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700914 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800915}
Nick Piggin2ae88142006-10-28 10:38:23 -0700916EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800917#endif
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/*
920 * In order to wait for pages to become available there must be
921 * waitqueues associated with pages. By using a hash table of
922 * waitqueues where the bucket discipline is to maintain all
923 * waiters on the same queue and wake all when any of the pages
924 * become available, and for the woken contexts to check to be
925 * sure the appropriate page became available, this saves space
926 * at a cost of "thundering herd" phenomena during rare hash
927 * collisions.
928 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000929#define PAGE_WAIT_TABLE_BITS 8
930#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
931static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
932
933static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Nicholas Piggin62906022016-12-25 13:00:30 +1000935 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
Nicholas Piggin62906022016-12-25 13:00:30 +1000937
938void __init pagecache_init(void)
939{
940 int i;
941
942 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
943 init_waitqueue_head(&page_wait_table[i]);
944
945 page_writeback_init();
946}
947
Linus Torvalds3510ca22017-08-27 13:55:12 -0700948/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000949struct wait_page_key {
950 struct page *page;
951 int bit_nr;
952 int page_match;
953};
954
955struct wait_page_queue {
956 struct page *page;
957 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200958 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000959};
960
Ingo Molnarac6424b2017-06-20 12:06:13 +0200961static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000962{
963 struct wait_page_key *key = arg;
964 struct wait_page_queue *wait_page
965 = container_of(wait, struct wait_page_queue, wait);
966
967 if (wait_page->page != key->page)
968 return 0;
969 key->page_match = 1;
970
971 if (wait_page->bit_nr != key->bit_nr)
972 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -0700973
974 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +1000975 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -0700976 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +1000977
978 return autoremove_wake_function(wait, mode, sync, key);
979}
980
Nicholas Piggin74d81bf2017-02-22 15:44:41 -0800981static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +1000982{
983 wait_queue_head_t *q = page_waitqueue(page);
984 struct wait_page_key key;
985 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -0700986 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +1000987
988 key.page = page;
989 key.bit_nr = bit_nr;
990 key.page_match = 0;
991
Tim Chen11a19c72017-08-25 09:13:55 -0700992 bookmark.flags = 0;
993 bookmark.private = NULL;
994 bookmark.func = NULL;
995 INIT_LIST_HEAD(&bookmark.entry);
996
Nicholas Piggin62906022016-12-25 13:00:30 +1000997 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -0700998 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
999
1000 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1001 /*
1002 * Take a breather from holding the lock,
1003 * allow pages that finish wake up asynchronously
1004 * to acquire the lock and remove themselves
1005 * from wait queue
1006 */
1007 spin_unlock_irqrestore(&q->lock, flags);
1008 cpu_relax();
1009 spin_lock_irqsave(&q->lock, flags);
1010 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1011 }
1012
Nicholas Piggin62906022016-12-25 13:00:30 +10001013 /*
1014 * It is possible for other pages to have collided on the waitqueue
1015 * hash, so in that case check for a page match. That prevents a long-
1016 * term waiter
1017 *
1018 * It is still possible to miss a case here, when we woke page waiters
1019 * and removed them from the waitqueue, but there are still other
1020 * page waiters.
1021 */
1022 if (!waitqueue_active(q) || !key.page_match) {
1023 ClearPageWaiters(page);
1024 /*
1025 * It's possible to miss clearing Waiters here, when we woke
1026 * our page waiters, but the hashed waitqueue has waiters for
1027 * other pages on it.
1028 *
1029 * That's okay, it's a rare case. The next waker will clear it.
1030 */
1031 }
1032 spin_unlock_irqrestore(&q->lock, flags);
1033}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001034
1035static void wake_up_page(struct page *page, int bit)
1036{
1037 if (!PageWaiters(page))
1038 return;
1039 wake_up_page_bit(page, bit);
1040}
Nicholas Piggin62906022016-12-25 13:00:30 +10001041
1042static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1043 struct page *page, int bit_nr, int state, bool lock)
1044{
1045 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001046 wait_queue_entry_t *wait = &wait_page.wait;
Nicholas Piggin62906022016-12-25 13:00:30 +10001047 int ret = 0;
1048
1049 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001050 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001051 wait->func = wake_page_function;
1052 wait_page.page = page;
1053 wait_page.bit_nr = bit_nr;
1054
1055 for (;;) {
1056 spin_lock_irq(&q->lock);
1057
Ingo Molnar2055da92017-06-20 12:06:46 +02001058 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001059 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001060 SetPageWaiters(page);
1061 }
1062
1063 set_current_state(state);
1064
1065 spin_unlock_irq(&q->lock);
1066
1067 if (likely(test_bit(bit_nr, &page->flags))) {
1068 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001069 }
1070
1071 if (lock) {
1072 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1073 break;
1074 } else {
1075 if (!test_bit(bit_nr, &page->flags))
1076 break;
1077 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001078
1079 if (unlikely(signal_pending_state(state, current))) {
1080 ret = -EINTR;
1081 break;
1082 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001083 }
1084
1085 finish_wait(q, wait);
1086
1087 /*
1088 * A signal could leave PageWaiters set. Clearing it here if
1089 * !waitqueue_active would be possible (by open-coding finish_wait),
1090 * but still fail to catch it in the case of wait hash collision. We
1091 * already can fail to clear wait hash collision cases, so don't
1092 * bother with signals either.
1093 */
1094
1095 return ret;
1096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Harvey Harrison920c7a52008-02-04 22:29:26 -08001098void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Nicholas Piggin62906022016-12-25 13:00:30 +10001100 wait_queue_head_t *q = page_waitqueue(page);
1101 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103EXPORT_SYMBOL(wait_on_page_bit);
1104
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001105int wait_on_page_bit_killable(struct page *page, int bit_nr)
1106{
Nicholas Piggin62906022016-12-25 13:00:30 +10001107 wait_queue_head_t *q = page_waitqueue(page);
1108 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001109}
David Howells4343d002017-11-02 15:27:52 +00001110EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001113 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001114 * @page: Page defining the wait queue of interest
1115 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001116 *
1117 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1118 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001119void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001120{
1121 wait_queue_head_t *q = page_waitqueue(page);
1122 unsigned long flags;
1123
1124 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001125 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001126 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001127 spin_unlock_irqrestore(&q->lock, flags);
1128}
1129EXPORT_SYMBOL_GPL(add_page_wait_queue);
1130
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001131#ifndef clear_bit_unlock_is_negative_byte
1132
1133/*
1134 * PG_waiters is the high bit in the same byte as PG_lock.
1135 *
1136 * On x86 (and on many other architectures), we can clear PG_lock and
1137 * test the sign bit at the same time. But if the architecture does
1138 * not support that special operation, we just do this all by hand
1139 * instead.
1140 *
1141 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1142 * being cleared, but a memory barrier should be unneccssary since it is
1143 * in the same byte as PG_locked.
1144 */
1145static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1146{
1147 clear_bit_unlock(nr, mem);
1148 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001149 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001150}
1151
1152#endif
1153
David Howells385e1ca5f2009-04-03 16:42:39 +01001154/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001155 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * @page: the page
1157 *
1158 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1159 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001160 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1162 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001163 * Note that this depends on PG_waiters being the sign bit in the byte
1164 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1165 * clear the PG_locked bit and test PG_waiters at the same time fairly
1166 * portably (architectures that do LL/SC can test any bit, while x86 can
1167 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001169void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001171 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001172 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001173 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001174 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1175 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177EXPORT_SYMBOL(unlock_page);
1178
Randy Dunlap485bb992006-06-23 02:03:49 -07001179/**
1180 * end_page_writeback - end writeback against a page
1181 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
1183void end_page_writeback(struct page *page)
1184{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001185 /*
1186 * TestClearPageReclaim could be used here but it is an atomic
1187 * operation and overkill in this particular case. Failing to
1188 * shuffle a page marked for immediate reclaim is too mild to
1189 * justify taking an atomic operation penalty at the end of
1190 * ever page writeback.
1191 */
1192 if (PageReclaim(page)) {
1193 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001194 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001195 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001196
1197 if (!test_clear_page_writeback(page))
1198 BUG();
1199
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001200 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 wake_up_page(page, PG_writeback);
1202}
1203EXPORT_SYMBOL(end_page_writeback);
1204
Matthew Wilcox57d99842014-06-04 16:07:45 -07001205/*
1206 * After completing I/O on a page, call this routine to update the page
1207 * flags appropriately
1208 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001209void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001210{
Jens Axboec11f0c02016-08-05 08:11:04 -06001211 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001212 if (!err) {
1213 SetPageUptodate(page);
1214 } else {
1215 ClearPageUptodate(page);
1216 SetPageError(page);
1217 }
1218 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001219 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001220 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001221 struct address_space *mapping;
1222
Matthew Wilcox57d99842014-06-04 16:07:45 -07001223 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001224 mapping = page_mapping(page);
1225 if (mapping)
1226 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001227 }
1228 end_page_writeback(page);
1229 }
1230}
1231EXPORT_SYMBOL_GPL(page_endio);
1232
Randy Dunlap485bb992006-06-23 02:03:49 -07001233/**
1234 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001235 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001237void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Nicholas Piggin62906022016-12-25 13:00:30 +10001239 struct page *page = compound_head(__page);
1240 wait_queue_head_t *q = page_waitqueue(page);
1241 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243EXPORT_SYMBOL(__lock_page);
1244
Nicholas Piggin62906022016-12-25 13:00:30 +10001245int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001246{
Nicholas Piggin62906022016-12-25 13:00:30 +10001247 struct page *page = compound_head(__page);
1248 wait_queue_head_t *q = page_waitqueue(page);
1249 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001250}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001251EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001252
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001253/*
1254 * Return values:
1255 * 1 - page is locked; mmap_sem is still held.
1256 * 0 - page is not locked.
1257 * mmap_sem has been released (up_read()), unless flags had both
1258 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1259 * which case mmap_sem is still held.
1260 *
1261 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1262 * with the page locked and the mmap_sem unperturbed.
1263 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001264int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1265 unsigned int flags)
1266{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001267 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1268 /*
1269 * CAUTION! In this case, mmap_sem is not released
1270 * even though return 0.
1271 */
1272 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1273 return 0;
1274
1275 up_read(&mm->mmap_sem);
1276 if (flags & FAULT_FLAG_KILLABLE)
1277 wait_on_page_locked_killable(page);
1278 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001279 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001280 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001281 } else {
1282 if (flags & FAULT_FLAG_KILLABLE) {
1283 int ret;
1284
1285 ret = __lock_page_killable(page);
1286 if (ret) {
1287 up_read(&mm->mmap_sem);
1288 return 0;
1289 }
1290 } else
1291 __lock_page(page);
1292 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001293 }
1294}
1295
Randy Dunlap485bb992006-06-23 02:03:49 -07001296/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001297 * page_cache_next_miss() - Find the next gap in the page cache.
1298 * @mapping: Mapping.
1299 * @index: Index.
1300 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001301 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001302 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1303 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001304 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001305 * This function may be called under the rcu_read_lock. However, this will
1306 * not atomically search a snapshot of the cache at a single point in time.
1307 * For example, if a gap is created at index 5, then subsequently a gap is
1308 * created at index 10, page_cache_next_miss covering both indices may
1309 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001310 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001311 * Return: The index of the gap if found, otherwise an index outside the
1312 * range specified (in which case 'return - index >= max_scan' will be true).
1313 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001314 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001315pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001316 pgoff_t index, unsigned long max_scan)
1317{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001318 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001319
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001320 while (max_scan--) {
1321 void *entry = xas_next(&xas);
1322 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001323 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001324 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001325 break;
1326 }
1327
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001328 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001329}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001330EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001331
1332/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001333 * page_cache_prev_miss() - Find the next gap in the page cache.
1334 * @mapping: Mapping.
1335 * @index: Index.
1336 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001337 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001338 * Search the range [max(index - max_scan + 1, 0), index] for the
1339 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001340 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001341 * This function may be called under the rcu_read_lock. However, this will
1342 * not atomically search a snapshot of the cache at a single point in time.
1343 * For example, if a gap is created at index 10, then subsequently a gap is
1344 * created at index 5, page_cache_prev_miss() covering both indices may
1345 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001346 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001347 * Return: The index of the gap if found, otherwise an index outside the
1348 * range specified (in which case 'index - return >= max_scan' will be true).
1349 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001350 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001351pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001352 pgoff_t index, unsigned long max_scan)
1353{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001354 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001355
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001356 while (max_scan--) {
1357 void *entry = xas_prev(&xas);
1358 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001359 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001360 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001361 break;
1362 }
1363
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001364 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001365}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001366EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001367
1368/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001369 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001370 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001371 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001372 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001373 * Looks up the page cache slot at @mapping & @offset. If there is a
1374 * page cache page, it is returned with an increased refcount.
1375 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001376 * If the slot holds a shadow entry of a previously evicted page, or a
1377 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001378 *
1379 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001381struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001383 XA_STATE(xas, &mapping->i_pages, offset);
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001384 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Nick Piggina60637c2008-07-25 19:45:31 -07001386 rcu_read_lock();
1387repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001388 xas_reset(&xas);
1389 page = xas_load(&xas);
1390 if (xas_retry(&xas, page))
1391 goto repeat;
1392 /*
1393 * A shadow entry of a recently evicted page, or a swap entry from
1394 * shmem/tmpfs. Return it without attempting to raise page count.
1395 */
1396 if (!page || xa_is_value(page))
1397 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001398
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001399 head = compound_head(page);
1400 if (!page_cache_get_speculative(head))
1401 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001402
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001403 /* The page was split under us? */
1404 if (compound_head(page) != head) {
1405 put_page(head);
1406 goto repeat;
1407 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001408
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001409 /*
1410 * Has the page moved?
1411 * This is part of the lockless pagecache protocol. See
1412 * include/linux/pagemap.h for details.
1413 */
1414 if (unlikely(page != xas_reload(&xas))) {
1415 put_page(head);
1416 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001417 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001418out:
Nick Piggina60637c2008-07-25 19:45:31 -07001419 rcu_read_unlock();
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return page;
1422}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001423EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Randy Dunlap485bb992006-06-23 02:03:49 -07001425/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001426 * find_lock_entry - locate, pin and lock a page cache entry
1427 * @mapping: the address_space to search
1428 * @offset: the page cache index
1429 *
1430 * Looks up the page cache slot at @mapping & @offset. If there is a
1431 * page cache page, it is returned locked and with an increased
1432 * refcount.
1433 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001434 * If the slot holds a shadow entry of a previously evicted page, or a
1435 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001436 *
1437 * Otherwise, %NULL is returned.
1438 *
1439 * find_lock_entry() may sleep.
1440 */
1441struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct page *page;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001446 page = find_get_entry(mapping, offset);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001447 if (page && !xa_is_value(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001448 lock_page(page);
1449 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001450 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001451 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001452 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001453 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001455 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return page;
1458}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001459EXPORT_SYMBOL(find_lock_entry);
1460
1461/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001462 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001463 * @mapping: the address_space to search
1464 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001465 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001466 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001467 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001468 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001469 *
Randy Dunlap75325182014-07-30 16:08:37 -07001470 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001471 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001472 * @fgp_flags can be:
1473 *
1474 * - FGP_ACCESSED: the page will be marked accessed
1475 * - FGP_LOCK: Page is return locked
1476 * - FGP_CREAT: If page is not present then a new page is allocated using
1477 * @gfp_mask and added to the page cache and the VM's LRU
1478 * list. The page is returned locked and with an increased
1479 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001480 *
1481 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1482 * if the GFP flags specified for FGP_CREAT are atomic.
1483 *
1484 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001485 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001486struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001487 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Nick Piggineb2be182007-10-16 01:24:57 -07001489 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001492 page = find_get_entry(mapping, offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001493 if (xa_is_value(page))
Mel Gorman2457aec2014-06-04 16:10:31 -07001494 page = NULL;
1495 if (!page)
1496 goto no_page;
1497
1498 if (fgp_flags & FGP_LOCK) {
1499 if (fgp_flags & FGP_NOWAIT) {
1500 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001501 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001502 return NULL;
1503 }
1504 } else {
1505 lock_page(page);
1506 }
1507
1508 /* Has the page been truncated? */
1509 if (unlikely(page->mapping != mapping)) {
1510 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001511 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001512 goto repeat;
1513 }
1514 VM_BUG_ON_PAGE(page->index != offset, page);
1515 }
1516
1517 if (page && (fgp_flags & FGP_ACCESSED))
1518 mark_page_accessed(page);
1519
1520no_page:
1521 if (!page && (fgp_flags & FGP_CREAT)) {
1522 int err;
1523 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001524 gfp_mask |= __GFP_WRITE;
1525 if (fgp_flags & FGP_NOFS)
1526 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001527
Michal Hocko45f87de2014-12-29 20:30:35 +01001528 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001529 if (!page)
1530 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001531
1532 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1533 fgp_flags |= FGP_LOCK;
1534
Hugh Dickinseb39d612014-08-06 16:06:43 -07001535 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001536 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001537 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001538
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001539 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001540 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001541 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001542 page = NULL;
1543 if (err == -EEXIST)
1544 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return page;
1549}
Mel Gorman2457aec2014-06-04 16:10:31 -07001550EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001553 * find_get_entries - gang pagecache lookup
1554 * @mapping: The address_space to search
1555 * @start: The starting page cache index
1556 * @nr_entries: The maximum number of entries
1557 * @entries: Where the resulting entries are placed
1558 * @indices: The cache indices corresponding to the entries in @entries
1559 *
1560 * find_get_entries() will search for and return a group of up to
1561 * @nr_entries entries in the mapping. The entries are placed at
1562 * @entries. find_get_entries() takes a reference against any actual
1563 * pages it returns.
1564 *
1565 * The search returns a group of mapping-contiguous page cache entries
1566 * with ascending indexes. There may be holes in the indices due to
1567 * not-present pages.
1568 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001569 * Any shadow entries of evicted pages, or swap entries from
1570 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001571 *
1572 * find_get_entries() returns the number of pages and shadow entries
1573 * which were found.
1574 */
1575unsigned find_get_entries(struct address_space *mapping,
1576 pgoff_t start, unsigned int nr_entries,
1577 struct page **entries, pgoff_t *indices)
1578{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001579 XA_STATE(xas, &mapping->i_pages, start);
1580 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001581 unsigned int ret = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001582
1583 if (!nr_entries)
1584 return 0;
1585
1586 rcu_read_lock();
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001587 xas_for_each(&xas, page, ULONG_MAX) {
1588 struct page *head;
1589 if (xas_retry(&xas, page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001590 continue;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001591 /*
1592 * A shadow entry of a recently evicted page, a swap
1593 * entry from shmem/tmpfs or a DAX entry. Return it
1594 * without attempting to raise page count.
1595 */
1596 if (xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001597 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001598
1599 head = compound_head(page);
1600 if (!page_cache_get_speculative(head))
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001601 goto retry;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001602
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001603 /* The page was split under us? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001604 if (compound_head(page) != head)
1605 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001606
Johannes Weiner0cd61442014-04-03 14:47:46 -07001607 /* Has the page moved? */
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001608 if (unlikely(page != xas_reload(&xas)))
1609 goto put_page;
1610
Johannes Weiner0cd61442014-04-03 14:47:46 -07001611export:
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001612 indices[ret] = xas.xa_index;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001613 entries[ret] = page;
1614 if (++ret == nr_entries)
1615 break;
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04001616 continue;
1617put_page:
1618 put_page(head);
1619retry:
1620 xas_reset(&xas);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001621 }
1622 rcu_read_unlock();
1623 return ret;
1624}
1625
1626/**
Jan Karab947cee2017-09-06 16:21:21 -07001627 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * @mapping: The address_space to search
1629 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001630 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 * @nr_pages: The maximum number of pages
1632 * @pages: Where the resulting pages are placed
1633 *
Jan Karab947cee2017-09-06 16:21:21 -07001634 * find_get_pages_range() will search for and return a group of up to @nr_pages
1635 * pages in the mapping starting at index @start and up to index @end
1636 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1637 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 *
1639 * The search returns a group of mapping-contiguous pages with ascending
1640 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001641 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 *
Jan Karab947cee2017-09-06 16:21:21 -07001643 * find_get_pages_range() returns the number of pages which were found. If this
1644 * number is smaller than @nr_pages, the end of specified range has been
1645 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Jan Karab947cee2017-09-06 16:21:21 -07001647unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1648 pgoff_t end, unsigned int nr_pages,
1649 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001651 XA_STATE(xas, &mapping->i_pages, *start);
1652 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001653 unsigned ret = 0;
1654
1655 if (unlikely(!nr_pages))
1656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Nick Piggina60637c2008-07-25 19:45:31 -07001658 rcu_read_lock();
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001659 xas_for_each(&xas, page, end) {
1660 struct page *head;
1661 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001662 continue;
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001663 /* Skip over shadow, swap and DAX entries */
1664 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001665 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07001666
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001667 head = compound_head(page);
1668 if (!page_cache_get_speculative(head))
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001669 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001670
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001671 /* The page was split under us? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001672 if (compound_head(page) != head)
1673 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001674
Nick Piggina60637c2008-07-25 19:45:31 -07001675 /* Has the page moved? */
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001676 if (unlikely(page != xas_reload(&xas)))
1677 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001678
1679 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001680 if (++ret == nr_pages) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001681 *start = page->index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07001682 goto out;
1683 }
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001684 continue;
1685put_page:
1686 put_page(head);
1687retry:
1688 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001689 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001690
Jan Karab947cee2017-09-06 16:21:21 -07001691 /*
1692 * We come here when there is no page beyond @end. We take care to not
1693 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04001694 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07001695 * already broken anyway.
1696 */
1697 if (end == (pgoff_t)-1)
1698 *start = (pgoff_t)-1;
1699 else
1700 *start = end + 1;
1701out:
Nick Piggina60637c2008-07-25 19:45:31 -07001702 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return ret;
1705}
1706
Jens Axboeebf43502006-04-27 08:46:01 +02001707/**
1708 * find_get_pages_contig - gang contiguous pagecache lookup
1709 * @mapping: The address_space to search
1710 * @index: The starting page index
1711 * @nr_pages: The maximum number of pages
1712 * @pages: Where the resulting pages are placed
1713 *
1714 * find_get_pages_contig() works exactly like find_get_pages(), except
1715 * that the returned number of pages are guaranteed to be contiguous.
1716 *
1717 * find_get_pages_contig() returns the number of pages which were found.
1718 */
1719unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1720 unsigned int nr_pages, struct page **pages)
1721{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001722 XA_STATE(xas, &mapping->i_pages, index);
1723 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001724 unsigned int ret = 0;
1725
1726 if (unlikely(!nr_pages))
1727 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001728
Nick Piggina60637c2008-07-25 19:45:31 -07001729 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001730 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
1731 struct page *head;
1732 if (xas_retry(&xas, page))
1733 continue;
1734 /*
1735 * If the entry has been swapped out, we can stop looking.
1736 * No current caller is looking for DAX entries.
1737 */
1738 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001739 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001740
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001741 head = compound_head(page);
1742 if (!page_cache_get_speculative(head))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001743 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001744
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001745 /* The page was split under us? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001746 if (compound_head(page) != head)
1747 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001748
Nick Piggina60637c2008-07-25 19:45:31 -07001749 /* Has the page moved? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001750 if (unlikely(page != xas_reload(&xas)))
1751 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001752
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001753 /*
1754 * must check mapping and index after taking the ref.
1755 * otherwise we can get both false positives and false
1756 * negatives, which is just confusing to the caller.
1757 */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001758 if (!page->mapping || page_to_pgoff(page) != xas.xa_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001759 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001760 break;
1761 }
1762
Nick Piggina60637c2008-07-25 19:45:31 -07001763 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001764 if (++ret == nr_pages)
1765 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04001766 continue;
1767put_page:
1768 put_page(head);
1769retry:
1770 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02001771 }
Nick Piggina60637c2008-07-25 19:45:31 -07001772 rcu_read_unlock();
1773 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001774}
David Howellsef71c152007-05-09 02:33:44 -07001775EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001776
Randy Dunlap485bb992006-06-23 02:03:49 -07001777/**
Jan Kara72b045a2017-11-15 17:34:33 -08001778 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001779 * @mapping: the address_space to search
1780 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001781 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001782 * @tag: the tag index
1783 * @nr_pages: the maximum number of pages
1784 * @pages: where the resulting pages are placed
1785 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001787 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 */
Jan Kara72b045a2017-11-15 17:34:33 -08001789unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001790 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08001791 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001793 XA_STATE(xas, &mapping->i_pages, *index);
1794 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001795 unsigned ret = 0;
1796
1797 if (unlikely(!nr_pages))
1798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Nick Piggina60637c2008-07-25 19:45:31 -07001800 rcu_read_lock();
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001801 xas_for_each_marked(&xas, page, end, tag) {
1802 struct page *head;
1803 if (xas_retry(&xas, page))
Nick Piggina60637c2008-07-25 19:45:31 -07001804 continue;
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001805 /*
1806 * Shadow entries should never be tagged, but this iteration
1807 * is lockless so there is a window for page reclaim to evict
1808 * a page we saw tagged. Skip over it.
1809 */
1810 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07001811 continue;
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))
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001815 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07001816
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001817 /* The page was split under us? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001818 if (compound_head(page) != head)
1819 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001820
Nick Piggina60637c2008-07-25 19:45:31 -07001821 /* Has the page moved? */
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001822 if (unlikely(page != xas_reload(&xas)))
1823 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07001824
1825 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001826 if (++ret == nr_pages) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001827 *index = page->index + 1;
Jan Kara72b045a2017-11-15 17:34:33 -08001828 goto out;
1829 }
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001830 continue;
1831put_page:
1832 put_page(head);
1833retry:
1834 xas_reset(&xas);
Nick Piggina60637c2008-07-25 19:45:31 -07001835 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001836
Jan Kara72b045a2017-11-15 17:34:33 -08001837 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001838 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08001839 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04001840 * iteration when there is a page at index -1 but that is already
1841 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08001842 */
1843 if (end == (pgoff_t)-1)
1844 *index = (pgoff_t)-1;
1845 else
1846 *index = end + 1;
1847out:
Nick Piggina60637c2008-07-25 19:45:31 -07001848 rcu_read_unlock();
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return ret;
1851}
Jan Kara72b045a2017-11-15 17:34:33 -08001852EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001854/**
1855 * find_get_entries_tag - find and return entries that match @tag
1856 * @mapping: the address_space to search
1857 * @start: the starting page cache index
1858 * @tag: the tag index
1859 * @nr_entries: the maximum number of entries
1860 * @entries: where the resulting entries are placed
1861 * @indices: the cache indices corresponding to the entries in @entries
1862 *
1863 * Like find_get_entries, except we only return entries which are tagged with
1864 * @tag.
1865 */
1866unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001867 xa_mark_t tag, unsigned int nr_entries,
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001868 struct page **entries, pgoff_t *indices)
1869{
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001870 XA_STATE(xas, &mapping->i_pages, start);
1871 struct page *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001872 unsigned int ret = 0;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001873
1874 if (!nr_entries)
1875 return 0;
1876
1877 rcu_read_lock();
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001878 xas_for_each_marked(&xas, page, ULONG_MAX, tag) {
1879 struct page *head;
1880 if (xas_retry(&xas, page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001881 continue;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001882 /*
1883 * A shadow entry of a recently evicted page, a swap
1884 * entry from shmem/tmpfs or a DAX entry. Return it
1885 * without attempting to raise page count.
1886 */
1887 if (xa_is_value(page))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001888 goto export;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001889
1890 head = compound_head(page);
1891 if (!page_cache_get_speculative(head))
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001892 goto retry;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001893
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001894 /* The page was split under us? */
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001895 if (compound_head(page) != head)
1896 goto put_page;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001897
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001898 /* Has the page moved? */
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001899 if (unlikely(page != xas_reload(&xas)))
1900 goto put_page;
1901
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001902export:
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001903 indices[ret] = xas.xa_index;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001904 entries[ret] = page;
1905 if (++ret == nr_entries)
1906 break;
Matthew Wilcoxc1901cd2018-05-16 23:56:04 -04001907 continue;
1908put_page:
1909 put_page(head);
1910retry:
1911 xas_reset(&xas);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001912 }
1913 rcu_read_unlock();
1914 return ret;
1915}
1916EXPORT_SYMBOL(find_get_entries_tag);
1917
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001918/*
1919 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1920 * a _large_ part of the i/o request. Imagine the worst scenario:
1921 *
1922 * ---R__________________________________________B__________
1923 * ^ reading here ^ bad block(assume 4k)
1924 *
1925 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1926 * => failing the whole request => read(R) => read(R+1) =>
1927 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1928 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1929 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1930 *
1931 * It is going insane. Fix it by quickly scaling down the readahead size.
1932 */
1933static void shrink_readahead_size_eio(struct file *filp,
1934 struct file_ra_state *ra)
1935{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001936 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07001937}
1938
Randy Dunlap485bb992006-06-23 02:03:49 -07001939/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001940 * generic_file_buffered_read - generic file read routine
1941 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05001942 * @iter: data destination
1943 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07001944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07001946 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 *
1948 * This is really ugly. But the goto's actually try to clarify some
1949 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001951static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05001952 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001954 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001955 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08001957 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02001958 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001959 pgoff_t index;
1960 pgoff_t last_index;
1961 pgoff_t prev_index;
1962 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07001963 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05001964 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Wei Fangc2a97372016-10-07 17:01:52 -07001966 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08001967 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07001968 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
1969
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001970 index = *ppos >> PAGE_SHIFT;
1971 prev_index = ra->prev_pos >> PAGE_SHIFT;
1972 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1973 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1974 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 for (;;) {
1977 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07001978 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07001979 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 unsigned long nr, ret;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08001984 if (fatal_signal_pending(current)) {
1985 error = -EINTR;
1986 goto out;
1987 }
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001990 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02001991 if (iocb->ki_flags & IOCB_NOWAIT)
1992 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07001993 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07001994 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07001995 index, last_index - index);
1996 page = find_get_page(mapping, index);
1997 if (unlikely(page == NULL))
1998 goto no_cached_page;
1999 }
2000 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002001 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002002 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002003 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002005 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002006 if (iocb->ki_flags & IOCB_NOWAIT) {
2007 put_page(page);
2008 goto would_block;
2009 }
2010
Mel Gormanebded022016-03-15 14:55:39 -07002011 /*
2012 * See comment in do_read_cache_page on why
2013 * wait_on_page_locked is used to avoid unnecessarily
2014 * serialisations and why it's safe.
2015 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002016 error = wait_on_page_locked_killable(page);
2017 if (unlikely(error))
2018 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002019 if (PageUptodate(page))
2020 goto page_ok;
2021
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002022 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002023 !mapping->a_ops->is_partially_uptodate)
2024 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002025 /* pipes can't handle partially uptodate pages */
2026 if (unlikely(iter->type & ITER_PIPE))
2027 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002028 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002029 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002030 /* Did it get truncated before we got the lock? */
2031 if (!page->mapping)
2032 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002033 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002034 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002035 goto page_not_up_to_date_locked;
2036 unlock_page(page);
2037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002039 /*
2040 * i_size must be checked after we know the page is Uptodate.
2041 *
2042 * Checking i_size after the check allows us to calculate
2043 * the correct value for "nr", which means the zero-filled
2044 * part of the page is not copied back to userspace (unless
2045 * another truncate extends the file - this is desired though).
2046 */
2047
2048 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002049 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002050 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002051 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002052 goto out;
2053 }
2054
2055 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002057 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002058 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002059 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002060 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002061 goto out;
2062 }
2063 }
2064 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 /* If users can be writing to this page using arbitrary
2067 * virtual addresses, take care about potential aliasing
2068 * before reading the page on the kernel side.
2069 */
2070 if (mapping_writably_mapped(mapping))
2071 flush_dcache_page(page);
2072
2073 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002074 * When a sequential read accesses a page several times,
2075 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Jan Karaec0f1632007-05-06 14:49:25 -07002077 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 mark_page_accessed(page);
2079 prev_index = index;
2080
2081 /*
2082 * Ok, we have the page, and it's up-to-date, so
2083 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 */
Al Viro6e58e792014-02-03 17:07:03 -05002085
2086 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002088 index += offset >> PAGE_SHIFT;
2089 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002090 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002092 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002093 written += ret;
2094 if (!iov_iter_count(iter))
2095 goto out;
2096 if (ret < nr) {
2097 error = -EFAULT;
2098 goto out;
2099 }
2100 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102page_not_up_to_date:
2103 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002104 error = lock_page_killable(page);
2105 if (unlikely(error))
2106 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002108page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002109 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (!page->mapping) {
2111 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002112 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 continue;
2114 }
2115
2116 /* Did somebody else fill it already? */
2117 if (PageUptodate(page)) {
2118 unlock_page(page);
2119 goto page_ok;
2120 }
2121
2122readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002123 /*
2124 * A previous I/O error may have been due to temporary
2125 * failures, eg. multipath errors.
2126 * PG_error will be set again if readpage fails.
2127 */
2128 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* Start the actual read. The read will unlock the page. */
2130 error = mapping->a_ops->readpage(filp, page);
2131
Zach Brown994fc28c2005-12-15 14:28:17 -08002132 if (unlikely(error)) {
2133 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002134 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002135 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002136 goto find_page;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002142 error = lock_page_killable(page);
2143 if (unlikely(error))
2144 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (!PageUptodate(page)) {
2146 if (page->mapping == NULL) {
2147 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002148 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
2150 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002151 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto find_page;
2153 }
2154 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002155 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002156 error = -EIO;
2157 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159 unlock_page(page);
2160 }
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 goto page_ok;
2163
2164readpage_error:
2165 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002166 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto out;
2168
2169no_cached_page:
2170 /*
2171 * Ok, it wasn't cached, so we need to create a new
2172 * page..
2173 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002174 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002175 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002176 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002179 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002180 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002182 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002183 if (error == -EEXIST) {
2184 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto out;
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 goto readpage;
2190 }
2191
Milosz Tanski3239d832017-08-29 16:13:19 +02002192would_block:
2193 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002195 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002196 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002197 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002199 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002200 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002201 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Randy Dunlap485bb992006-06-23 02:03:49 -07002204/**
Al Viro6abd2322014-04-04 14:20:57 -04002205 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002206 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002207 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002208 *
Al Viro6abd2322014-04-04 14:20:57 -04002209 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 * that can use the page cache directly.
2211 */
2212ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002213generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002215 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002216 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002217
2218 if (!count)
2219 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Al Viro2ba48ce2015-04-09 13:52:01 -04002221 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002222 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002223 struct address_space *mapping = file->f_mapping;
2224 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002225 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002228 if (iocb->ki_flags & IOCB_NOWAIT) {
2229 if (filemap_range_has_page(mapping, iocb->ki_pos,
2230 iocb->ki_pos + count - 1))
2231 return -EAGAIN;
2232 } else {
2233 retval = filemap_write_and_wait_range(mapping,
2234 iocb->ki_pos,
2235 iocb->ki_pos + count - 1);
2236 if (retval < 0)
2237 goto out;
2238 }
Al Viroed978a82014-03-05 22:53:04 -05002239
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002240 file_accessed(file);
2241
Al Viro5ecda132017-04-13 14:13:36 -04002242 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002243 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002244 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002245 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002246 }
Al Viro5b47d592017-05-08 13:54:47 -04002247 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002248
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002249 /*
2250 * Btrfs can have a short DIO read if we encounter
2251 * compressed extents, so if there was an error, or if
2252 * we've already read everything we wanted to, or if
2253 * there was a short read because we hit EOF, go ahead
2254 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002255 * the rest of the read. Buffered reads will not work for
2256 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002257 */
Al Viro5ecda132017-04-13 14:13:36 -04002258 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002259 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002263 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264out:
2265 return retval;
2266}
Al Viroed978a82014-03-05 22:53:04 -05002267EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002270/**
2271 * page_cache_read - adds requested page to the page cache if not already there
2272 * @file: file to read
2273 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002274 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 * This adds the requested page to the page cache if it isn't already there,
2277 * and schedules an I/O to read in its contents from disk.
2278 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002279static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002282 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002283 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Zach Brown994fc28c2005-12-15 14:28:17 -08002285 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002286 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002287 if (!page)
2288 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002290 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002291 if (ret == 0)
2292 ret = mapping->a_ops->readpage(file, page);
2293 else if (ret == -EEXIST)
2294 ret = 0; /* losing race to add is OK */
2295
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002296 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Zach Brown994fc28c2005-12-15 14:28:17 -08002298 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002299
Zach Brown994fc28c2005-12-15 14:28:17 -08002300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303#define MMAP_LOTSAMISS (100)
2304
Linus Torvaldsef00e082009-06-16 15:31:25 -07002305/*
2306 * Synchronous readahead happens when we don't even find
2307 * a page in the page cache at all.
2308 */
2309static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2310 struct file_ra_state *ra,
2311 struct file *file,
2312 pgoff_t offset)
2313{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002314 struct address_space *mapping = file->f_mapping;
2315
2316 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002317 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002318 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002319 if (!ra->ra_pages)
2320 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002321
Joe Perches64363aa2013-07-08 16:00:18 -07002322 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002323 page_cache_sync_readahead(mapping, ra, file, offset,
2324 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002325 return;
2326 }
2327
Andi Kleen207d04b2011-05-24 17:12:29 -07002328 /* Avoid banging the cache line if not needed */
2329 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002330 ra->mmap_miss++;
2331
2332 /*
2333 * Do we miss much more than hit in this file? If so,
2334 * stop bothering with read-ahead. It will only hurt.
2335 */
2336 if (ra->mmap_miss > MMAP_LOTSAMISS)
2337 return;
2338
Wu Fengguangd30a1102009-06-16 15:31:30 -07002339 /*
2340 * mmap read-around
2341 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002342 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2343 ra->size = ra->ra_pages;
2344 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002345 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002346}
2347
2348/*
2349 * Asynchronous readahead happens when we find the page and PG_readahead,
2350 * so we want to possibly extend the readahead further..
2351 */
2352static void do_async_mmap_readahead(struct vm_area_struct *vma,
2353 struct file_ra_state *ra,
2354 struct file *file,
2355 struct page *page,
2356 pgoff_t offset)
2357{
2358 struct address_space *mapping = file->f_mapping;
2359
2360 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002361 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002362 return;
2363 if (ra->mmap_miss > 0)
2364 ra->mmap_miss--;
2365 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002366 page_cache_async_readahead(mapping, ra, file,
2367 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002368}
2369
Randy Dunlap485bb992006-06-23 02:03:49 -07002370/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002371 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002372 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002373 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002374 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 * mapped memory region to read in file data during a page fault.
2376 *
2377 * The goto's are kind of ugly, but this streamlines the normal case of having
2378 * it in the page cache, and handles the special cases reasonably without
2379 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002380 *
2381 * vma->vm_mm->mmap_sem must be held on entry.
2382 *
2383 * If our return value has VM_FAULT_RETRY set, it's because
2384 * lock_page_or_retry() returned 0.
2385 * The mmap_sem has usually been released in this case.
2386 * See __lock_page_or_retry() for the exception.
2387 *
2388 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2389 * has not been released.
2390 *
2391 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002393vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002396 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct address_space *mapping = file->f_mapping;
2398 struct file_ra_state *ra = &file->f_ra;
2399 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002400 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002401 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002403 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002405 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2406 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002407 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002410 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002412 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002413 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002415 * We found the page, so try async readahead before
2416 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 */
Dave Jiang11bac802017-02-24 14:56:41 -08002418 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002419 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002420 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002421 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002422 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002423 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002424 ret = VM_FAULT_MAJOR;
2425retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002426 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (!page)
2428 goto no_cached_page;
2429 }
2430
Dave Jiang11bac802017-02-24 14:56:41 -08002431 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002432 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002433 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002434 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002435
2436 /* Did it get truncated? */
2437 if (unlikely(page->mapping != mapping)) {
2438 unlock_page(page);
2439 put_page(page);
2440 goto retry_find;
2441 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002442 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002445 * We have a locked page in the page cache, now we need to check
2446 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 */
Nick Piggind00806b2007-07-19 01:46:57 -07002448 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 goto page_not_uptodate;
2450
Linus Torvaldsef00e082009-06-16 15:31:25 -07002451 /*
2452 * Found the page and have a reference on it.
2453 * We must recheck i_size under page lock.
2454 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002455 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2456 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002457 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002458 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002459 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002460 }
2461
Nick Piggind0217ac2007-07-19 01:47:03 -07002462 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002463 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465no_cached_page:
2466 /*
2467 * We're only likely to ever get here if MADV_RANDOM is in
2468 * effect.
2469 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002470 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /*
2473 * The page we want has now been added to the page cache.
2474 * In the unlikely event that someone removed it in the
2475 * meantime, we'll just come back here and read it again.
2476 */
2477 if (error >= 0)
2478 goto retry_find;
2479
2480 /*
2481 * An error return from page_cache_read can result if the
2482 * system is low on memory, or a problem occurs while trying
2483 * to schedule I/O.
2484 */
2485 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002486 return VM_FAULT_OOM;
2487 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /*
2491 * Umm, take care of errors if the page isn't up-to-date.
2492 * Try to re-read it _once_. We do this synchronously,
2493 * because there really aren't any performance issues here
2494 * and we need to check for errors.
2495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002497 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002498 if (!error) {
2499 wait_on_page_locked(page);
2500 if (!PageUptodate(page))
2501 error = -EIO;
2502 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002503 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002504
2505 if (!error || error == AOP_TRUNCATED_PAGE)
2506 goto retry_find;
2507
2508 /* Things didn't work out. Return zero to tell the mm layer so. */
2509 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002510 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002511}
2512EXPORT_SYMBOL(filemap_fault);
2513
Jan Kara82b0f8c2016-12-14 15:06:58 -08002514void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002515 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002516{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002517 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002518 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002519 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002520 unsigned long max_idx;
Matthew Wilcox070e8072018-05-17 00:08:30 -04002521 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002522 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002523
2524 rcu_read_lock();
Matthew Wilcox070e8072018-05-17 00:08:30 -04002525 xas_for_each(&xas, page, end_pgoff) {
2526 if (xas_retry(&xas, page))
2527 continue;
2528 if (xa_is_value(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002529 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002530
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002531 head = compound_head(page);
2532 if (!page_cache_get_speculative(head))
Matthew Wilcox070e8072018-05-17 00:08:30 -04002533 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002534
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002535 /* The page was split under us? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002536 if (compound_head(page) != head)
2537 goto skip;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002538
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002539 /* Has the page moved? */
Matthew Wilcox070e8072018-05-17 00:08:30 -04002540 if (unlikely(page != xas_reload(&xas)))
2541 goto skip;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002542
2543 if (!PageUptodate(page) ||
2544 PageReadahead(page) ||
2545 PageHWPoison(page))
2546 goto skip;
2547 if (!trylock_page(page))
2548 goto skip;
2549
2550 if (page->mapping != mapping || !PageUptodate(page))
2551 goto unlock;
2552
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002553 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2554 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002555 goto unlock;
2556
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002557 if (file->f_ra.mmap_miss > 0)
2558 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002559
Matthew Wilcox070e8072018-05-17 00:08:30 -04002560 vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002561 if (vmf->pte)
Matthew Wilcox070e8072018-05-17 00:08:30 -04002562 vmf->pte += xas.xa_index - last_pgoff;
2563 last_pgoff = xas.xa_index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002564 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002565 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002566 unlock_page(page);
2567 goto next;
2568unlock:
2569 unlock_page(page);
2570skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002571 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002572next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002573 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002574 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002575 break;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002576 }
2577 rcu_read_unlock();
2578}
2579EXPORT_SYMBOL(filemap_map_pages);
2580
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002581vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002582{
2583 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002584 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002585 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002586
Jan Kara14da9202012-06-12 16:20:37 +02002587 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002588 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002589 lock_page(page);
2590 if (page->mapping != inode->i_mapping) {
2591 unlock_page(page);
2592 ret = VM_FAULT_NOPAGE;
2593 goto out;
2594 }
Jan Kara14da9202012-06-12 16:20:37 +02002595 /*
2596 * We mark the page dirty already here so that when freeze is in
2597 * progress, we are guaranteed that writeback during freezing will
2598 * see the dirty page and writeprotect it again.
2599 */
2600 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002601 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002602out:
Jan Kara14da9202012-06-12 16:20:37 +02002603 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002604 return ret;
2605}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002606
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002607const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002608 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002609 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002610 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611};
2612
2613/* This is used for a general mmap of a disk file */
2614
2615int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2616{
2617 struct address_space *mapping = file->f_mapping;
2618
2619 if (!mapping->a_ops->readpage)
2620 return -ENOEXEC;
2621 file_accessed(file);
2622 vma->vm_ops = &generic_file_vm_ops;
2623 return 0;
2624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626/*
2627 * This is for filesystems which do not implement ->writepage.
2628 */
2629int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2630{
2631 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2632 return -EINVAL;
2633 return generic_file_mmap(file, vma);
2634}
2635#else
Arnd Bergmann45397222018-04-13 15:35:27 -07002636int filemap_page_mkwrite(struct vm_fault *vmf)
2637{
2638 return -ENOSYS;
2639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2641{
2642 return -ENOSYS;
2643}
2644int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2645{
2646 return -ENOSYS;
2647}
2648#endif /* CONFIG_MMU */
2649
Arnd Bergmann45397222018-04-13 15:35:27 -07002650EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651EXPORT_SYMBOL(generic_file_mmap);
2652EXPORT_SYMBOL(generic_file_readonly_mmap);
2653
Sasha Levin67f9fd92014-04-03 14:48:18 -07002654static struct page *wait_on_page_read(struct page *page)
2655{
2656 if (!IS_ERR(page)) {
2657 wait_on_page_locked(page);
2658 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002659 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002660 page = ERR_PTR(-EIO);
2661 }
2662 }
2663 return page;
2664}
2665
Mel Gorman32b63522016-03-15 14:55:36 -07002666static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002667 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002668 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002669 void *data,
2670 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Nick Piggineb2be182007-10-16 01:24:57 -07002672 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int err;
2674repeat:
2675 page = find_get_page(mapping, index);
2676 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002677 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002678 if (!page)
2679 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002680 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002681 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002682 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002683 if (err == -EEXIST)
2684 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return ERR_PTR(err);
2687 }
Mel Gorman32b63522016-03-15 14:55:36 -07002688
2689filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 err = filler(data, page);
2691 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002692 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002693 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Mel Gorman32b63522016-03-15 14:55:36 -07002695
2696 page = wait_on_page_read(page);
2697 if (IS_ERR(page))
2698 return page;
2699 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (PageUptodate(page))
2702 goto out;
2703
Mel Gormanebded022016-03-15 14:55:39 -07002704 /*
2705 * Page is not up to date and may be locked due one of the following
2706 * case a: Page is being filled and the page lock is held
2707 * case b: Read/write error clearing the page uptodate status
2708 * case c: Truncation in progress (page locked)
2709 * case d: Reclaim in progress
2710 *
2711 * Case a, the page will be up to date when the page is unlocked.
2712 * There is no need to serialise on the page lock here as the page
2713 * is pinned so the lock gives no additional protection. Even if the
2714 * the page is truncated, the data is still valid if PageUptodate as
2715 * it's a race vs truncate race.
2716 * Case b, the page will not be up to date
2717 * Case c, the page may be truncated but in itself, the data may still
2718 * be valid after IO completes as it's a read vs truncate race. The
2719 * operation must restart if the page is not uptodate on unlock but
2720 * otherwise serialising on page lock to stabilise the mapping gives
2721 * no additional guarantees to the caller as the page lock is
2722 * released before return.
2723 * Case d, similar to truncation. If reclaim holds the page lock, it
2724 * will be a race with remove_mapping that determines if the mapping
2725 * is valid on unlock but otherwise the data is valid and there is
2726 * no need to serialise with page lock.
2727 *
2728 * As the page lock gives no additional guarantee, we optimistically
2729 * wait on the page to be unlocked and check if it's up to date and
2730 * use the page if it is. Otherwise, the page lock is required to
2731 * distinguish between the different cases. The motivation is that we
2732 * avoid spurious serialisations and wakeups when multiple processes
2733 * wait on the same page for IO to complete.
2734 */
2735 wait_on_page_locked(page);
2736 if (PageUptodate(page))
2737 goto out;
2738
2739 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002741
2742 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (!page->mapping) {
2744 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002745 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002746 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Mel Gormanebded022016-03-15 14:55:39 -07002748
2749 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (PageUptodate(page)) {
2751 unlock_page(page);
2752 goto out;
2753 }
Mel Gorman32b63522016-03-15 14:55:36 -07002754 goto filler;
2755
David Howellsc855ff32007-05-09 13:42:20 +01002756out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002757 mark_page_accessed(page);
2758 return page;
2759}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002760
2761/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002762 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002763 * @mapping: the page's address_space
2764 * @index: the page index
2765 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002766 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002767 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002768 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002769 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002770 *
2771 * If the page does not get brought uptodate, return -EIO.
2772 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002773struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002774 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002775 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002776 void *data)
2777{
2778 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2779}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002780EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002781
2782/**
2783 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2784 * @mapping: the page's address_space
2785 * @index: the page index
2786 * @gfp: the page allocator flags to use if allocating
2787 *
2788 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002789 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002790 *
2791 * If the page does not get brought uptodate, return -EIO.
2792 */
2793struct page *read_cache_page_gfp(struct address_space *mapping,
2794 pgoff_t index,
2795 gfp_t gfp)
2796{
2797 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2798
Sasha Levin67f9fd92014-04-03 14:48:18 -07002799 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002800}
2801EXPORT_SYMBOL(read_cache_page_gfp);
2802
Nick Piggin2f718ff2007-10-16 01:24:59 -07002803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 * Performs necessary checks before doing a write
2805 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002806 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 * Returns appropriate error code that caller should return or
2808 * zero in case that write should be allowed.
2809 */
Al Viro3309dd02015-04-09 12:55:47 -04002810inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Al Viro3309dd02015-04-09 12:55:47 -04002812 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002814 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002815 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Al Viro3309dd02015-04-09 12:55:47 -04002817 if (!iov_iter_count(from))
2818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Al Viro0fa6b002015-04-04 04:05:48 -04002820 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002821 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002822 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Al Viro3309dd02015-04-09 12:55:47 -04002824 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002826 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2827 return -EINVAL;
2828
Al Viro0fa6b002015-04-04 04:05:48 -04002829 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002830 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002831 send_sig(SIGXFSZ, current, 0);
2832 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Al Viro3309dd02015-04-09 12:55:47 -04002834 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
2837 /*
2838 * LFS rule
2839 */
Al Viro3309dd02015-04-09 12:55:47 -04002840 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002842 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002844 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846
2847 /*
2848 * Are we about to exceed the fs block limit ?
2849 *
2850 * If we have written data it becomes a short write. If we have
2851 * exceeded without writing data we send a signal and return EFBIG.
2852 * Linus frestrict idea will clean these up nicely..
2853 */
Al Viro3309dd02015-04-09 12:55:47 -04002854 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2855 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Al Viro3309dd02015-04-09 12:55:47 -04002857 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2858 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860EXPORT_SYMBOL(generic_write_checks);
2861
Nick Pigginafddba42007-10-16 01:25:01 -07002862int pagecache_write_begin(struct file *file, struct address_space *mapping,
2863 loff_t pos, unsigned len, unsigned flags,
2864 struct page **pagep, void **fsdata)
2865{
2866 const struct address_space_operations *aops = mapping->a_ops;
2867
Nick Piggin4e02ed42008-10-29 14:00:55 -07002868 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002869 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002870}
2871EXPORT_SYMBOL(pagecache_write_begin);
2872
2873int pagecache_write_end(struct file *file, struct address_space *mapping,
2874 loff_t pos, unsigned len, unsigned copied,
2875 struct page *page, void *fsdata)
2876{
2877 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002878
Nick Piggin4e02ed42008-10-29 14:00:55 -07002879 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002880}
2881EXPORT_SYMBOL(pagecache_write_end);
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002884generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 struct file *file = iocb->ki_filp;
2887 struct address_space *mapping = file->f_mapping;
2888 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002889 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002891 size_t write_len;
2892 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Al Viro0c949332014-03-22 06:51:37 -04002894 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002895 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002896
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002897 if (iocb->ki_flags & IOCB_NOWAIT) {
2898 /* If there are pages to writeback, return */
2899 if (filemap_range_has_page(inode->i_mapping, pos,
2900 pos + iov_iter_count(from)))
2901 return -EAGAIN;
2902 } else {
2903 written = filemap_write_and_wait_range(mapping, pos,
2904 pos + write_len - 1);
2905 if (written)
2906 goto out;
2907 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07002908
2909 /*
2910 * After a write we want buffered reads to be sure to go to disk to get
2911 * the new data. We invalidate clean cached page from the region we're
2912 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07002913 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07002914 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07002915 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002916 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07002917 /*
2918 * If a page can not be invalidated, return 0 to fall back
2919 * to buffered write.
2920 */
2921 if (written) {
2922 if (written == -EBUSY)
2923 return 0;
2924 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07002925 }
2926
Al Viro639a93a52017-04-13 14:10:15 -04002927 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002928
2929 /*
2930 * Finally, try again to invalidate clean pages which might have been
2931 * cached by non-direct readahead, or faulted in by get_user_pages()
2932 * if the source of the write was an mmap'ed region of the file
2933 * we're writing. Either one is a pretty crazy thing to do,
2934 * so we don't support it 100%. If this invalidation
2935 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06002936 *
2937 * Most of the time we do not need this since dio_complete() will do
2938 * the invalidation for us. However there are some file systems that
2939 * do not end up with dio_complete() being called, so let's not break
2940 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07002941 */
Lukas Czerner332391a2017-09-21 08:16:29 -06002942 if (mapping->nrpages)
2943 invalidate_inode_pages2_range(mapping,
2944 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07002945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07002947 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04002948 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07002949 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2950 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 mark_inode_dirty(inode);
2952 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05002953 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
Al Viro639a93a52017-04-13 14:10:15 -04002955 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07002956out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return written;
2958}
2959EXPORT_SYMBOL(generic_file_direct_write);
2960
Nick Piggineb2be182007-10-16 01:24:57 -07002961/*
2962 * Find or create a page at the given pagecache position. Return the locked
2963 * page. This function is specifically for buffered writes.
2964 */
Nick Piggin54566b22009-01-04 12:00:53 -08002965struct page *grab_cache_page_write_begin(struct address_space *mapping,
2966 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07002967{
Nick Piggineb2be182007-10-16 01:24:57 -07002968 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07002969 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08002970
Nick Piggin54566b22009-01-04 12:00:53 -08002971 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07002972 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07002973
Mel Gorman2457aec2014-06-04 16:10:31 -07002974 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01002975 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07002976 if (page)
2977 wait_for_stable_page(page);
2978
Nick Piggineb2be182007-10-16 01:24:57 -07002979 return page;
2980}
Nick Piggin54566b22009-01-04 12:00:53 -08002981EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07002982
Al Viro3b93f912014-02-11 21:34:08 -05002983ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07002984 struct iov_iter *i, loff_t pos)
2985{
2986 struct address_space *mapping = file->f_mapping;
2987 const struct address_space_operations *a_ops = mapping->a_ops;
2988 long status = 0;
2989 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07002990 unsigned int flags = 0;
2991
Nick Pigginafddba42007-10-16 01:25:01 -07002992 do {
2993 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002994 unsigned long offset; /* Offset into pagecache page */
2995 unsigned long bytes; /* Bytes to write to page */
2996 size_t copied; /* Bytes copied from user */
2997 void *fsdata;
2998
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002999 offset = (pos & (PAGE_SIZE - 1));
3000 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003001 iov_iter_count(i));
3002
3003again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003004 /*
3005 * Bring in the user page that we will copy from _first_.
3006 * Otherwise there's a nasty deadlock on copying from the
3007 * same page as we're writing to, without it being marked
3008 * up-to-date.
3009 *
3010 * Not only is this an optimisation, but it is also required
3011 * to check that the address is actually valid, when atomic
3012 * usercopies are used, below.
3013 */
3014 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3015 status = -EFAULT;
3016 break;
3017 }
3018
Jan Kara296291c2015-10-22 13:32:21 -07003019 if (fatal_signal_pending(current)) {
3020 status = -EINTR;
3021 break;
3022 }
3023
Nick Piggin674b8922007-10-16 01:25:03 -07003024 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003025 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003026 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003027 break;
3028
anfei zhou931e80e2010-02-02 13:44:02 -08003029 if (mapping_writably_mapped(mapping))
3030 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003031
Nick Pigginafddba42007-10-16 01:25:01 -07003032 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003033 flush_dcache_page(page);
3034
3035 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3036 page, fsdata);
3037 if (unlikely(status < 0))
3038 break;
3039 copied = status;
3040
3041 cond_resched();
3042
Nick Piggin124d3b72008-02-02 15:01:17 +01003043 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003044 if (unlikely(copied == 0)) {
3045 /*
3046 * If we were unable to copy any data at all, we must
3047 * fall back to a single segment length write.
3048 *
3049 * If we didn't fallback here, we could livelock
3050 * because not all segments in the iov can be copied at
3051 * once without a pagefault.
3052 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003053 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003054 iov_iter_single_seg_count(i));
3055 goto again;
3056 }
Nick Pigginafddba42007-10-16 01:25:01 -07003057 pos += copied;
3058 written += copied;
3059
3060 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003061 } while (iov_iter_count(i));
3062
3063 return written ? written : status;
3064}
Al Viro3b93f912014-02-11 21:34:08 -05003065EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Jan Karae4dd9de2009-08-17 18:10:06 +02003067/**
Al Viro81742022014-04-03 03:17:43 -04003068 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003069 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003070 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003071 *
3072 * This function does all the work needed for actually writing data to a
3073 * file. It does all basic checks, removes SUID from the file, updates
3074 * modification times and calls proper subroutines depending on whether we
3075 * do direct IO or a standard buffered write.
3076 *
3077 * It expects i_mutex to be grabbed unless we work on a block device or similar
3078 * object which does not need locking at all.
3079 *
3080 * This function does *not* take care of syncing data in case of O_SYNC write.
3081 * A caller has to handle it. This is mainly due to the fact that we want to
3082 * avoid syncing under i_mutex.
3083 */
Al Viro81742022014-04-03 03:17:43 -04003084ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003087 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003089 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003091 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003094 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003095 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (err)
3097 goto out;
3098
Josef Bacikc3b2da32012-03-26 09:59:21 -04003099 err = file_update_time(file);
3100 if (err)
3101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Al Viro2ba48ce2015-04-09 13:52:01 -04003103 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003104 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003105
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003106 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003107 /*
3108 * If the write stopped short of completing, fall back to
3109 * buffered writes. Some filesystems do this for writes to
3110 * holes, for example. For DAX files, a buffered write will
3111 * not succeed (even if it did, DAX does not handle dirty
3112 * page-cache pages correctly).
3113 */
Al Viro0b8def92015-04-07 10:22:53 -04003114 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003116
Al Viro0b8def92015-04-07 10:22:53 -04003117 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003118 /*
Al Viro3b93f912014-02-11 21:34:08 -05003119 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003120 * then we want to return the number of bytes which were
3121 * direct-written, or the error code if that was zero. Note
3122 * that this differs from normal direct-io semantics, which
3123 * will return -EFOO even if some bytes were written.
3124 */
Al Viro60bb4522014-08-08 12:39:16 -04003125 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003126 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003127 goto out;
3128 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003129 /*
3130 * We need to ensure that the page cache pages are written to
3131 * disk and invalidated to preserve the expected O_DIRECT
3132 * semantics.
3133 */
Al Viro3b93f912014-02-11 21:34:08 -05003134 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003135 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003136 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003137 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003138 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003139 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003140 pos >> PAGE_SHIFT,
3141 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003142 } else {
3143 /*
3144 * We don't know how much we wrote, so just return
3145 * the number of bytes which were direct-written
3146 */
3147 }
3148 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003149 written = generic_perform_write(file, from, iocb->ki_pos);
3150 if (likely(written > 0))
3151 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153out:
3154 current->backing_dev_info = NULL;
3155 return written ? written : err;
3156}
Al Viro81742022014-04-03 03:17:43 -04003157EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Jan Karae4dd9de2009-08-17 18:10:06 +02003159/**
Al Viro81742022014-04-03 03:17:43 -04003160 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003161 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003162 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003163 *
Al Viro81742022014-04-03 03:17:43 -04003164 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003165 * filesystems. It takes care of syncing the file in case of O_SYNC file
3166 * and acquires i_mutex as needed.
3167 */
Al Viro81742022014-04-03 03:17:43 -04003168ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003171 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Al Viro59551022016-01-22 15:40:57 -05003174 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003175 ret = generic_write_checks(iocb, from);
3176 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003177 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003178 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Christoph Hellwige2592212016-04-07 08:52:01 -07003180 if (ret > 0)
3181 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return ret;
3183}
Al Viro81742022014-04-03 03:17:43 -04003184EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
David Howellscf9a2ae2006-08-29 19:05:54 +01003186/**
3187 * try_to_release_page() - release old fs-specific metadata on a page
3188 *
3189 * @page: the page which the kernel is trying to free
3190 * @gfp_mask: memory allocation flags (and I/O mode)
3191 *
3192 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003193 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003194 * Otherwise return zero.
3195 *
David Howells266cf652009-04-03 16:42:36 +01003196 * This may also be called if PG_fscache is set on a page, indicating that the
3197 * page is known to the local caching routines.
3198 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003199 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003200 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003201 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003202 */
3203int try_to_release_page(struct page *page, gfp_t gfp_mask)
3204{
3205 struct address_space * const mapping = page->mapping;
3206
3207 BUG_ON(!PageLocked(page));
3208 if (PageWriteback(page))
3209 return 0;
3210
3211 if (mapping && mapping->a_ops->releasepage)
3212 return mapping->a_ops->releasepage(page, gfp_mask);
3213 return try_to_free_buffers(page);
3214}
3215
3216EXPORT_SYMBOL(try_to_release_page);