blob: 7997adce5a29a2a515107c06d1ecadefd2151472 [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
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700114static int page_cache_tree_insert(struct address_space *mapping,
115 struct page *page, void **shadowp)
116{
117 struct radix_tree_node *node;
118 void **slot;
119 int error;
120
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700121 error = __radix_tree_create(&mapping->i_pages, page->index, 0,
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700122 &node, &slot);
123 if (error)
124 return error;
125 if (*slot) {
126 void *p;
127
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700128 p = radix_tree_deref_slot_protected(slot,
129 &mapping->i_pages.xa_lock);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700130 if (!radix_tree_exceptional_entry(p))
131 return -EEXIST;
132
133 mapping->nrexceptional--;
Ross Zwislerd01ad192017-09-06 16:18:47 -0700134 if (shadowp)
135 *shadowp = p;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700136 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700137 __radix_tree_replace(&mapping->i_pages, node, slot, page,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800138 workingset_lookup_update(mapping));
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700139 mapping->nrpages++;
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700140 return 0;
141}
142
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700143static void page_cache_tree_delete(struct address_space *mapping,
144 struct page *page, void *shadow)
145{
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800146 int i, nr;
147
148 /* hugetlb pages are represented by one entry in the radix tree */
149 nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700150
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700151 VM_BUG_ON_PAGE(!PageLocked(page), page);
152 VM_BUG_ON_PAGE(PageTail(page), page);
153 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700154
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700155 for (i = 0; i < nr; i++) {
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200156 struct radix_tree_node *node;
157 void **slot;
158
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700159 __radix_tree_lookup(&mapping->i_pages, page->index + i,
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200160 &node, &slot);
161
Johannes Weinerdbc446b2016-12-12 16:43:55 -0800162 VM_BUG_ON_PAGE(!node && nr != 1, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700163
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700164 radix_tree_clear_tags(&mapping->i_pages, node, slot);
165 __radix_tree_replace(&mapping->i_pages, node, slot, shadow,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800166 workingset_lookup_update(mapping));
Johannes Weiner449dd692014-04-03 14:47:56 -0700167 }
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200168
Jan Kara23006382017-11-15 17:37:26 -0800169 page->mapping = NULL;
170 /* Leave page->index set: truncation lookup relies upon it */
171
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200172 if (shadow) {
173 mapping->nrexceptional += nr;
174 /*
175 * Make sure the nrexceptional update is committed before
176 * the nrpages update so that final truncate racing
177 * with reclaim does not see both counters 0 at the
178 * same time and miss a shadow entry.
179 */
180 smp_wmb();
181 }
182 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700183}
184
Jan Kara5ecc4d82017-11-15 17:37:29 -0800185static void unaccount_page_cache_page(struct address_space *mapping,
186 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800188 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600190 /*
191 * if we're uptodate, flush out into the cleancache, otherwise
192 * invalidate any existing cleancache entries. We can't leave
193 * stale data around in the cleancache once our page is gone
194 */
195 if (PageUptodate(page) && PageMappedToDisk(page))
196 cleancache_put_page(page);
197 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400198 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600199
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700200 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800201 VM_BUG_ON_PAGE(page_mapped(page), page);
202 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
203 int mapcount;
204
205 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
206 current->comm, page_to_pfn(page));
207 dump_page(page, "still mapped when deleted");
208 dump_stack();
209 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
210
211 mapcount = page_mapcount(page);
212 if (mapping_exiting(mapping) &&
213 page_count(page) >= mapcount + 2) {
214 /*
215 * All vmas have already been torn down, so it's
216 * a good bet that actually the page is unmapped,
217 * and we'd prefer not to leak it: if we're wrong,
218 * some other bad page check should catch it later.
219 */
220 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700221 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800222 }
223 }
224
Jan Kara76253fb2017-11-15 17:37:22 -0800225 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800226 if (PageHuge(page))
227 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800228
Jan Kara5ecc4d82017-11-15 17:37:29 -0800229 nr = hpage_nr_pages(page);
230
231 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
232 if (PageSwapBacked(page)) {
233 __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
234 if (PageTransHuge(page))
235 __dec_node_page_state(page, NR_SHMEM_THPS);
236 } else {
237 VM_BUG_ON_PAGE(PageTransHuge(page), page);
Jan Kara76253fb2017-11-15 17:37:22 -0800238 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800239
240 /*
241 * At this point page must be either written or cleaned by
242 * truncate. Dirty page here signals a bug and loss of
243 * unwritten data.
244 *
245 * This fixes dirty accounting after removing the page entirely
246 * but leaves PageDirty set: it has no effect for truncated
247 * page and anyway will be cleared before returning page into
248 * buddy allocator.
249 */
250 if (WARN_ON_ONCE(PageDirty(page)))
251 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
252}
253
254/*
255 * Delete a page from the page cache and free it. Caller has to make
256 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700257 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800258 */
259void __delete_from_page_cache(struct page *page, void *shadow)
260{
261 struct address_space *mapping = page->mapping;
262
263 trace_mm_filemap_delete_from_page_cache(page);
264
265 unaccount_page_cache_page(mapping, page);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700266 page_cache_tree_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Jan Kara59c66c52017-11-15 17:37:18 -0800269static void page_cache_free_page(struct address_space *mapping,
270 struct page *page)
271{
272 void (*freepage)(struct page *);
273
274 freepage = mapping->a_ops->freepage;
275 if (freepage)
276 freepage(page);
277
278 if (PageTransHuge(page) && !PageHuge(page)) {
279 page_ref_sub(page, HPAGE_PMD_NR);
280 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
281 } else {
282 put_page(page);
283 }
284}
285
Minchan Kim702cfbf2011-03-22 16:32:43 -0700286/**
287 * delete_from_page_cache - delete page from page cache
288 * @page: the page which the kernel is trying to remove from page cache
289 *
290 * This must be called only on pages that have been verified to be in the page
291 * cache and locked. It will never put the page into the free list, the caller
292 * has a reference on the page.
293 */
294void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700296 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400297 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Matt Mackallcd7619d2005-05-01 08:59:01 -0700299 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700300 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700301 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700302 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500303
Jan Kara59c66c52017-11-15 17:37:18 -0800304 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700305}
306EXPORT_SYMBOL(delete_from_page_cache);
307
Jan Karaaa65c292017-11-15 17:37:33 -0800308/*
309 * page_cache_tree_delete_batch - delete several pages from page cache
310 * @mapping: the mapping to which pages belong
311 * @pvec: pagevec with pages to delete
312 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700313 * The function walks over mapping->i_pages and removes pages passed in @pvec
314 * from the mapping. The function expects @pvec to be sorted by page index.
315 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800316 * modified). The function expects only THP head pages to be present in the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700317 * @pvec and takes care to delete all corresponding tail pages from the
318 * mapping as well.
Jan Karaaa65c292017-11-15 17:37:33 -0800319 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700320 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800321 */
322static void
323page_cache_tree_delete_batch(struct address_space *mapping,
324 struct pagevec *pvec)
325{
326 struct radix_tree_iter iter;
327 void **slot;
328 int total_pages = 0;
329 int i = 0, tail_pages = 0;
330 struct page *page;
331 pgoff_t start;
332
333 start = pvec->pages[0]->index;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700334 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Jan Karaaa65c292017-11-15 17:37:33 -0800335 if (i >= pagevec_count(pvec) && !tail_pages)
336 break;
337 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700338 &mapping->i_pages.xa_lock);
Jan Karaaa65c292017-11-15 17:37:33 -0800339 if (radix_tree_exceptional_entry(page))
340 continue;
341 if (!tail_pages) {
342 /*
343 * Some page got inserted in our range? Skip it. We
344 * have our pages locked so they are protected from
345 * being removed.
346 */
347 if (page != pvec->pages[i])
348 continue;
349 WARN_ON_ONCE(!PageLocked(page));
350 if (PageTransHuge(page) && !PageHuge(page))
351 tail_pages = HPAGE_PMD_NR - 1;
352 page->mapping = NULL;
353 /*
354 * Leave page->index set: truncation lookup relies
355 * upon it
356 */
357 i++;
358 } else {
359 tail_pages--;
360 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700361 radix_tree_clear_tags(&mapping->i_pages, iter.node, slot);
362 __radix_tree_replace(&mapping->i_pages, iter.node, slot, NULL,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800363 workingset_lookup_update(mapping));
Jan Karaaa65c292017-11-15 17:37:33 -0800364 total_pages++;
365 }
366 mapping->nrpages -= total_pages;
367}
368
369void delete_from_page_cache_batch(struct address_space *mapping,
370 struct pagevec *pvec)
371{
372 int i;
373 unsigned long flags;
374
375 if (!pagevec_count(pvec))
376 return;
377
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700378 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800379 for (i = 0; i < pagevec_count(pvec); i++) {
380 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
381
382 unaccount_page_cache_page(mapping, pvec->pages[i]);
383 }
384 page_cache_tree_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700385 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800386
387 for (i = 0; i < pagevec_count(pvec); i++)
388 page_cache_free_page(mapping, pvec->pages[i]);
389}
390
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200391int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700392{
393 int ret = 0;
394 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700395 if (test_bit(AS_ENOSPC, &mapping->flags) &&
396 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700397 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700398 if (test_bit(AS_EIO, &mapping->flags) &&
399 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700400 ret = -EIO;
401 return ret;
402}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200403EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700404
Jeff Layton76341ca2017-07-06 07:02:22 -0400405static int filemap_check_and_keep_errors(struct address_space *mapping)
406{
407 /* Check for outstanding write errors */
408 if (test_bit(AS_EIO, &mapping->flags))
409 return -EIO;
410 if (test_bit(AS_ENOSPC, &mapping->flags))
411 return -ENOSPC;
412 return 0;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700416 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700417 * @mapping: address space structure to write
418 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800419 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700420 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700422 * Start writeback against all of a mapping's dirty pages that lie
423 * within the byte offsets <start, end> inclusive.
424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700426 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * these two operations is that if a dirty page/buffer is encountered, it must
428 * be waited upon, and not just skipped over.
429 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800430int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
431 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int ret;
434 struct writeback_control wbc = {
435 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800436 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700437 .range_start = start,
438 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 };
440
441 if (!mapping_cap_writeback_dirty(mapping))
442 return 0;
443
Tejun Heob16b1de2015-06-02 08:39:48 -0600444 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600446 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ret;
448}
449
450static inline int __filemap_fdatawrite(struct address_space *mapping,
451 int sync_mode)
452{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700453 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456int filemap_fdatawrite(struct address_space *mapping)
457{
458 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
459}
460EXPORT_SYMBOL(filemap_fdatawrite);
461
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400462int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800463 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
466}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400467EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Randy Dunlap485bb992006-06-23 02:03:49 -0700469/**
470 * filemap_flush - mostly a non-blocking flush
471 * @mapping: target address_space
472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * This is a mostly non-blocking flush. Not suitable for data-integrity
474 * purposes - I/O may not be started against all dirty pages.
475 */
476int filemap_flush(struct address_space *mapping)
477{
478 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
479}
480EXPORT_SYMBOL(filemap_flush);
481
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500482/**
483 * filemap_range_has_page - check if a page exists in range.
484 * @mapping: address space within which to check
485 * @start_byte: offset in bytes where the range starts
486 * @end_byte: offset in bytes where the range ends (inclusive)
487 *
488 * Find at least one page in the range supplied, usually used to check if
489 * direct writing in this range will trigger a writeback.
490 */
491bool filemap_range_has_page(struct address_space *mapping,
492 loff_t start_byte, loff_t end_byte)
493{
494 pgoff_t index = start_byte >> PAGE_SHIFT;
495 pgoff_t end = end_byte >> PAGE_SHIFT;
Jan Karaf7b68042017-09-06 16:21:40 -0700496 struct page *page;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500497
498 if (end_byte < start_byte)
499 return false;
500
501 if (mapping->nrpages == 0)
502 return false;
503
Jan Karaf7b68042017-09-06 16:21:40 -0700504 if (!find_get_pages_range(mapping, &index, end, 1, &page))
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500505 return false;
Jan Karaf7b68042017-09-06 16:21:40 -0700506 put_page(page);
507 return true;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500508}
509EXPORT_SYMBOL(filemap_range_has_page);
510
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400511static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800512 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300514 pgoff_t index = start_byte >> PAGE_SHIFT;
515 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct pagevec pvec;
517 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200519 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400520 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Mel Gorman86679822017-11-15 17:37:52 -0800522 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800523 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned i;
525
Jan Kara312e9d22017-11-15 17:35:05 -0800526 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800527 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800528 if (!nr_pages)
529 break;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 for (i = 0; i < nr_pages; i++) {
532 struct page *page = pvec.pages[i];
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400535 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 pagevec_release(&pvec);
538 cond_resched();
539 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800540}
541
542/**
543 * filemap_fdatawait_range - wait for writeback to complete
544 * @mapping: address space structure to wait for
545 * @start_byte: offset in bytes where the range starts
546 * @end_byte: offset in bytes where the range ends (inclusive)
547 *
548 * Walk the list of under-writeback pages of the given address space
549 * in the given range and wait for all of them. Check error status of
550 * the address space and return it.
551 *
552 * Since the error status of the address space is cleared by this function,
553 * callers are responsible for checking the return value and handling and/or
554 * reporting the error.
555 */
556int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
557 loff_t end_byte)
558{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400559 __filemap_fdatawait_range(mapping, start_byte, end_byte);
560 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200562EXPORT_SYMBOL(filemap_fdatawait_range);
563
564/**
Jeff Laytona823e452017-07-28 07:24:43 -0400565 * file_fdatawait_range - wait for writeback to complete
566 * @file: file pointing to address space structure to wait for
567 * @start_byte: offset in bytes where the range starts
568 * @end_byte: offset in bytes where the range ends (inclusive)
569 *
570 * Walk the list of under-writeback pages of the address space that file
571 * refers to, in the given range and wait for all of them. Check error
572 * status of the address space vs. the file->f_wb_err cursor and return it.
573 *
574 * Since the error status of the file is advanced by this function,
575 * callers are responsible for checking the return value and handling and/or
576 * reporting the error.
577 */
578int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
579{
580 struct address_space *mapping = file->f_mapping;
581
582 __filemap_fdatawait_range(mapping, start_byte, end_byte);
583 return file_check_and_advance_wb_err(file);
584}
585EXPORT_SYMBOL(file_fdatawait_range);
586
587/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800588 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
589 * @mapping: address space structure to wait for
590 *
591 * Walk the list of under-writeback pages of the given address space
592 * and wait for all of them. Unlike filemap_fdatawait(), this function
593 * does not clear error status of the address space.
594 *
595 * Use this function if callers don't handle errors themselves. Expected
596 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
597 * fsfreeze(8)
598 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400599int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800600{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400601 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400602 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800603}
Jeff Layton76341ca2017-07-06 07:02:22 -0400604EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800605
Jeff Layton9326c9b2017-07-26 10:21:11 -0400606static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jeff Layton9326c9b2017-07-26 10:21:11 -0400608 return (!dax_mapping(mapping) && mapping->nrpages) ||
609 (dax_mapping(mapping) && mapping->nrexceptional);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612int filemap_write_and_wait(struct address_space *mapping)
613{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800614 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Jeff Layton9326c9b2017-07-26 10:21:11 -0400616 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800617 err = filemap_fdatawrite(mapping);
618 /*
619 * Even if the above returned error, the pages may be
620 * written partially (e.g. -ENOSPC), so we wait for it.
621 * But the -EIO is special case, it may indicate the worst
622 * thing (e.g. bug) happened, so we avoid waiting for it.
623 */
624 if (err != -EIO) {
625 int err2 = filemap_fdatawait(mapping);
626 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}
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800637EXPORT_SYMBOL(filemap_write_and_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Randy Dunlap485bb992006-06-23 02:03:49 -0700639/**
640 * filemap_write_and_wait_range - write out & wait on a file range
641 * @mapping: the address_space for the pages
642 * @lstart: offset in bytes where the range starts
643 * @lend: offset in bytes where the range ends (inclusive)
644 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800645 * Write out and wait upon file offsets lstart->lend, inclusive.
646 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300647 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800648 * that this function can be used to write to the very end-of-file (end = -1).
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650int filemap_write_and_wait_range(struct address_space *mapping,
651 loff_t lstart, loff_t lend)
652{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800653 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Jeff Layton9326c9b2017-07-26 10:21:11 -0400655 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800656 err = __filemap_fdatawrite_range(mapping, lstart, lend,
657 WB_SYNC_ALL);
658 /* See comment of filemap_write_and_wait() */
659 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200660 int err2 = filemap_fdatawait_range(mapping,
661 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800662 if (!err)
663 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400664 } else {
665 /* Clear any previously stored errors */
666 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800667 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700668 } else {
669 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
Chris Masonf6995582009-04-15 13:22:37 -0400673EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jeff Layton5660e132017-07-06 07:02:25 -0400675void __filemap_set_wb_err(struct address_space *mapping, int err)
676{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400677 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400678
679 trace_filemap_set_wb_err(mapping, eseq);
680}
681EXPORT_SYMBOL(__filemap_set_wb_err);
682
683/**
684 * file_check_and_advance_wb_err - report wb error (if any) that was previously
685 * and advance wb_err to current one
686 * @file: struct file on which the error is being reported
687 *
688 * When userland calls fsync (or something like nfsd does the equivalent), we
689 * want to report any writeback errors that occurred since the last fsync (or
690 * since the file was opened if there haven't been any).
691 *
692 * Grab the wb_err from the mapping. If it matches what we have in the file,
693 * then just quickly return 0. The file is all caught up.
694 *
695 * If it doesn't match, then take the mapping value, set the "seen" flag in
696 * it and try to swap it into place. If it works, or another task beat us
697 * to it with the new value, then update the f_wb_err and return the error
698 * portion. The error at this point must be reported via proper channels
699 * (a'la fsync, or NFS COMMIT operation, etc.).
700 *
701 * While we handle mapping->wb_err with atomic operations, the f_wb_err
702 * value is protected by the f_lock since we must ensure that it reflects
703 * the latest value swapped in for this file descriptor.
704 */
705int file_check_and_advance_wb_err(struct file *file)
706{
707 int err = 0;
708 errseq_t old = READ_ONCE(file->f_wb_err);
709 struct address_space *mapping = file->f_mapping;
710
711 /* Locklessly handle the common case where nothing has changed */
712 if (errseq_check(&mapping->wb_err, old)) {
713 /* Something changed, must use slow path */
714 spin_lock(&file->f_lock);
715 old = file->f_wb_err;
716 err = errseq_check_and_advance(&mapping->wb_err,
717 &file->f_wb_err);
718 trace_file_check_and_advance_wb_err(file, old);
719 spin_unlock(&file->f_lock);
720 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700721
722 /*
723 * We're mostly using this function as a drop in replacement for
724 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
725 * that the legacy code would have had on these flags.
726 */
727 clear_bit(AS_EIO, &mapping->flags);
728 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400729 return err;
730}
731EXPORT_SYMBOL(file_check_and_advance_wb_err);
732
733/**
734 * file_write_and_wait_range - write out & wait on a file range
735 * @file: file pointing to address_space with pages
736 * @lstart: offset in bytes where the range starts
737 * @lend: offset in bytes where the range ends (inclusive)
738 *
739 * Write out and wait upon file offsets lstart->lend, inclusive.
740 *
741 * Note that @lend is inclusive (describes the last byte to be written) so
742 * that this function can be used to write to the very end-of-file (end = -1).
743 *
744 * After writing out and waiting on the data, we check and advance the
745 * f_wb_err cursor to the latest value, and return any errors detected there.
746 */
747int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
748{
749 int err = 0, err2;
750 struct address_space *mapping = file->f_mapping;
751
Jeff Layton9326c9b2017-07-26 10:21:11 -0400752 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400753 err = __filemap_fdatawrite_range(mapping, lstart, lend,
754 WB_SYNC_ALL);
755 /* See comment of filemap_write_and_wait() */
756 if (err != -EIO)
757 __filemap_fdatawait_range(mapping, lstart, lend);
758 }
759 err2 = file_check_and_advance_wb_err(file);
760 if (!err)
761 err = err2;
762 return err;
763}
764EXPORT_SYMBOL(file_write_and_wait_range);
765
Randy Dunlap485bb992006-06-23 02:03:49 -0700766/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700767 * replace_page_cache_page - replace a pagecache page with a new one
768 * @old: page to be replaced
769 * @new: page to replace with
770 * @gfp_mask: allocation mode
771 *
772 * This function replaces a page in the pagecache with a new one. On
773 * success it acquires the pagecache reference for the new page and
774 * drops it for the old page. Both the old and new pages must be
775 * locked. This function does not add the new page to the LRU, the
776 * caller must do that.
777 *
778 * The remove + add is atomic. The only way this function can fail is
779 * memory allocation failure.
780 */
781int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
782{
783 int error;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700784
Sasha Levin309381fea2014-01-23 15:52:54 -0800785 VM_BUG_ON_PAGE(!PageLocked(old), old);
786 VM_BUG_ON_PAGE(!PageLocked(new), new);
787 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700788
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700789 error = radix_tree_preload(gfp_mask & GFP_RECLAIM_MASK);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700790 if (!error) {
791 struct address_space *mapping = old->mapping;
792 void (*freepage)(struct page *);
Greg Thelenc4843a72015-05-22 17:13:16 -0400793 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700794
795 pgoff_t offset = old->index;
796 freepage = mapping->a_ops->freepage;
797
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300798 get_page(new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700799 new->mapping = mapping;
800 new->index = offset;
801
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700802 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700803 __delete_from_page_cache(old, NULL);
Johannes Weiner22f2ac52016-09-30 15:11:29 -0700804 error = page_cache_tree_insert(mapping, new, NULL);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700805 BUG_ON(error);
Michal Hocko4165b9b2015-06-24 16:57:24 -0700806
807 /*
808 * hugetlb pages do not participate in page cache accounting.
809 */
810 if (!PageHuge(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700811 __inc_node_page_state(new, NR_FILE_PAGES);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700812 if (PageSwapBacked(new))
Mel Gorman11fb9982016-07-28 15:46:20 -0700813 __inc_node_page_state(new, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700814 xa_unlock_irqrestore(&mapping->i_pages, flags);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700815 mem_cgroup_migrate(old, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700816 radix_tree_preload_end();
817 if (freepage)
818 freepage(old);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300819 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700820 }
821
822 return error;
823}
824EXPORT_SYMBOL_GPL(replace_page_cache_page);
825
Johannes Weinera5289102014-04-03 14:47:51 -0700826static int __add_to_page_cache_locked(struct page *page,
827 struct address_space *mapping,
828 pgoff_t offset, gfp_t gfp_mask,
829 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Johannes Weiner00501b52014-08-08 14:19:20 -0700831 int huge = PageHuge(page);
832 struct mem_cgroup *memcg;
Nick Piggine2867812008-07-25 19:45:30 -0700833 int error;
834
Sasha Levin309381fea2014-01-23 15:52:54 -0800835 VM_BUG_ON_PAGE(!PageLocked(page), page);
836 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Nick Piggine2867812008-07-25 19:45:30 -0700837
Johannes Weiner00501b52014-08-08 14:19:20 -0700838 if (!huge) {
839 error = mem_cgroup_try_charge(page, current->mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800840 gfp_mask, &memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700841 if (error)
842 return error;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Matthew Wilcoxabc1be12018-04-20 14:56:20 -0700845 error = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700846 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -0700847 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800848 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700849 return error;
850 }
851
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300852 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700853 page->mapping = mapping;
854 page->index = offset;
855
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700856 xa_lock_irq(&mapping->i_pages);
Johannes Weinera5289102014-04-03 14:47:51 -0700857 error = page_cache_tree_insert(mapping, page, shadowp);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700858 radix_tree_preload_end();
859 if (unlikely(error))
860 goto err_insert;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700861
862 /* hugetlb pages do not participate in page cache accounting. */
863 if (!huge)
Mel Gorman11fb9982016-07-28 15:46:20 -0700864 __inc_node_page_state(page, NR_FILE_PAGES);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700865 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700866 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800867 mem_cgroup_commit_charge(page, memcg, false, false);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700868 trace_mm_filemap_add_to_page_cache(page);
869 return 0;
870err_insert:
871 page->mapping = NULL;
872 /* Leave page->index set: truncation relies upon it */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700873 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700874 if (!huge)
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800875 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300876 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return error;
878}
Johannes Weinera5289102014-04-03 14:47:51 -0700879
880/**
881 * add_to_page_cache_locked - add a locked page to the pagecache
882 * @page: page to add
883 * @mapping: the page's address_space
884 * @offset: page index
885 * @gfp_mask: page allocation mode
886 *
887 * This function is used to add a page to the pagecache. It must be locked.
888 * This function does not add the page to the LRU. The caller must do that.
889 */
890int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
891 pgoff_t offset, gfp_t gfp_mask)
892{
893 return __add_to_page_cache_locked(page, mapping, offset,
894 gfp_mask, NULL);
895}
Nick Piggine2867812008-07-25 19:45:30 -0700896EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400899 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Johannes Weinera5289102014-04-03 14:47:51 -0700901 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700902 int ret;
903
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800904 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700905 ret = __add_to_page_cache_locked(page, mapping, offset,
906 gfp_mask, &shadow);
907 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800908 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700909 else {
910 /*
911 * The page might have been evicted from cache only
912 * recently, in which case it should be activated like
913 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700914 * The exception is pages getting rewritten; evicting other
915 * data from the working set, only to cache data that will
916 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700917 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700918 WARN_ON_ONCE(PageActive(page));
919 if (!(gfp_mask & __GFP_WRITE) && shadow)
920 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700921 lru_cache_add(page);
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return ret;
924}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300925EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Paul Jackson44110fe2006-03-24 03:16:04 -0800927#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700928struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800929{
Miao Xiec0ff7452010-05-24 14:32:08 -0700930 int n;
931 struct page *page;
932
Paul Jackson44110fe2006-03-24 03:16:04 -0800933 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700934 unsigned int cpuset_mems_cookie;
935 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700936 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700937 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -0700938 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -0700939 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700940
Miao Xiec0ff7452010-05-24 14:32:08 -0700941 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -0800942 }
Nick Piggin2ae88142006-10-28 10:38:23 -0700943 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -0800944}
Nick Piggin2ae88142006-10-28 10:38:23 -0700945EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -0800946#endif
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
949 * In order to wait for pages to become available there must be
950 * waitqueues associated with pages. By using a hash table of
951 * waitqueues where the bucket discipline is to maintain all
952 * waiters on the same queue and wake all when any of the pages
953 * become available, and for the woken contexts to check to be
954 * sure the appropriate page became available, this saves space
955 * at a cost of "thundering herd" phenomena during rare hash
956 * collisions.
957 */
Nicholas Piggin62906022016-12-25 13:00:30 +1000958#define PAGE_WAIT_TABLE_BITS 8
959#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
960static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
961
962static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Nicholas Piggin62906022016-12-25 13:00:30 +1000964 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
Nicholas Piggin62906022016-12-25 13:00:30 +1000966
967void __init pagecache_init(void)
968{
969 int i;
970
971 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
972 init_waitqueue_head(&page_wait_table[i]);
973
974 page_writeback_init();
975}
976
Linus Torvalds3510ca22017-08-27 13:55:12 -0700977/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
Nicholas Piggin62906022016-12-25 13:00:30 +1000978struct wait_page_key {
979 struct page *page;
980 int bit_nr;
981 int page_match;
982};
983
984struct wait_page_queue {
985 struct page *page;
986 int bit_nr;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200987 wait_queue_entry_t wait;
Nicholas Piggin62906022016-12-25 13:00:30 +1000988};
989
Ingo Molnarac6424b2017-06-20 12:06:13 +0200990static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +1000991{
992 struct wait_page_key *key = arg;
993 struct wait_page_queue *wait_page
994 = container_of(wait, struct wait_page_queue, wait);
995
996 if (wait_page->page != key->page)
997 return 0;
998 key->page_match = 1;
999
1000 if (wait_page->bit_nr != key->bit_nr)
1001 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001002
1003 /* Stop walking if it's locked */
Nicholas Piggin62906022016-12-25 13:00:30 +10001004 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds3510ca22017-08-27 13:55:12 -07001005 return -1;
Nicholas Piggin62906022016-12-25 13:00:30 +10001006
1007 return autoremove_wake_function(wait, mode, sync, key);
1008}
1009
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001010static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001011{
1012 wait_queue_head_t *q = page_waitqueue(page);
1013 struct wait_page_key key;
1014 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001015 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001016
1017 key.page = page;
1018 key.bit_nr = bit_nr;
1019 key.page_match = 0;
1020
Tim Chen11a19c72017-08-25 09:13:55 -07001021 bookmark.flags = 0;
1022 bookmark.private = NULL;
1023 bookmark.func = NULL;
1024 INIT_LIST_HEAD(&bookmark.entry);
1025
Nicholas Piggin62906022016-12-25 13:00:30 +10001026 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001027 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1028
1029 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1030 /*
1031 * Take a breather from holding the lock,
1032 * allow pages that finish wake up asynchronously
1033 * to acquire the lock and remove themselves
1034 * from wait queue
1035 */
1036 spin_unlock_irqrestore(&q->lock, flags);
1037 cpu_relax();
1038 spin_lock_irqsave(&q->lock, flags);
1039 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1040 }
1041
Nicholas Piggin62906022016-12-25 13:00:30 +10001042 /*
1043 * It is possible for other pages to have collided on the waitqueue
1044 * hash, so in that case check for a page match. That prevents a long-
1045 * term waiter
1046 *
1047 * It is still possible to miss a case here, when we woke page waiters
1048 * and removed them from the waitqueue, but there are still other
1049 * page waiters.
1050 */
1051 if (!waitqueue_active(q) || !key.page_match) {
1052 ClearPageWaiters(page);
1053 /*
1054 * It's possible to miss clearing Waiters here, when we woke
1055 * our page waiters, but the hashed waitqueue has waiters for
1056 * other pages on it.
1057 *
1058 * That's okay, it's a rare case. The next waker will clear it.
1059 */
1060 }
1061 spin_unlock_irqrestore(&q->lock, flags);
1062}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001063
1064static void wake_up_page(struct page *page, int bit)
1065{
1066 if (!PageWaiters(page))
1067 return;
1068 wake_up_page_bit(page, bit);
1069}
Nicholas Piggin62906022016-12-25 13:00:30 +10001070
1071static inline int wait_on_page_bit_common(wait_queue_head_t *q,
1072 struct page *page, int bit_nr, int state, bool lock)
1073{
1074 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001075 wait_queue_entry_t *wait = &wait_page.wait;
Nicholas Piggin62906022016-12-25 13:00:30 +10001076 int ret = 0;
1077
1078 init_wait(wait);
Linus Torvalds3510ca22017-08-27 13:55:12 -07001079 wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001080 wait->func = wake_page_function;
1081 wait_page.page = page;
1082 wait_page.bit_nr = bit_nr;
1083
1084 for (;;) {
1085 spin_lock_irq(&q->lock);
1086
Ingo Molnar2055da92017-06-20 12:06:46 +02001087 if (likely(list_empty(&wait->entry))) {
Linus Torvalds3510ca22017-08-27 13:55:12 -07001088 __add_wait_queue_entry_tail(q, wait);
Nicholas Piggin62906022016-12-25 13:00:30 +10001089 SetPageWaiters(page);
1090 }
1091
1092 set_current_state(state);
1093
1094 spin_unlock_irq(&q->lock);
1095
1096 if (likely(test_bit(bit_nr, &page->flags))) {
1097 io_schedule();
Nicholas Piggin62906022016-12-25 13:00:30 +10001098 }
1099
1100 if (lock) {
1101 if (!test_and_set_bit_lock(bit_nr, &page->flags))
1102 break;
1103 } else {
1104 if (!test_bit(bit_nr, &page->flags))
1105 break;
1106 }
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001107
1108 if (unlikely(signal_pending_state(state, current))) {
1109 ret = -EINTR;
1110 break;
1111 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001112 }
1113
1114 finish_wait(q, wait);
1115
1116 /*
1117 * A signal could leave PageWaiters set. Clearing it here if
1118 * !waitqueue_active would be possible (by open-coding finish_wait),
1119 * but still fail to catch it in the case of wait hash collision. We
1120 * already can fail to clear wait hash collision cases, so don't
1121 * bother with signals either.
1122 */
1123
1124 return ret;
1125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Harvey Harrison920c7a52008-02-04 22:29:26 -08001127void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Nicholas Piggin62906022016-12-25 13:00:30 +10001129 wait_queue_head_t *q = page_waitqueue(page);
1130 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132EXPORT_SYMBOL(wait_on_page_bit);
1133
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001134int wait_on_page_bit_killable(struct page *page, int bit_nr)
1135{
Nicholas Piggin62906022016-12-25 13:00:30 +10001136 wait_queue_head_t *q = page_waitqueue(page);
1137 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001138}
David Howells4343d002017-11-02 15:27:52 +00001139EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001142 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001143 * @page: Page defining the wait queue of interest
1144 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001145 *
1146 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1147 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001148void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001149{
1150 wait_queue_head_t *q = page_waitqueue(page);
1151 unsigned long flags;
1152
1153 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001154 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001155 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001156 spin_unlock_irqrestore(&q->lock, flags);
1157}
1158EXPORT_SYMBOL_GPL(add_page_wait_queue);
1159
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001160#ifndef clear_bit_unlock_is_negative_byte
1161
1162/*
1163 * PG_waiters is the high bit in the same byte as PG_lock.
1164 *
1165 * On x86 (and on many other architectures), we can clear PG_lock and
1166 * test the sign bit at the same time. But if the architecture does
1167 * not support that special operation, we just do this all by hand
1168 * instead.
1169 *
1170 * The read of PG_waiters has to be after (or concurrently with) PG_locked
1171 * being cleared, but a memory barrier should be unneccssary since it is
1172 * in the same byte as PG_locked.
1173 */
1174static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1175{
1176 clear_bit_unlock(nr, mem);
1177 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001178 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001179}
1180
1181#endif
1182
David Howells385e1ca5f2009-04-03 16:42:39 +01001183/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001184 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * @page: the page
1186 *
1187 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
1188 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001189 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1191 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001192 * Note that this depends on PG_waiters being the sign bit in the byte
1193 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1194 * clear the PG_locked bit and test PG_waiters at the same time fairly
1195 * portably (architectures that do LL/SC can test any bit, while x86 can
1196 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001198void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001200 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001201 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001202 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001203 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1204 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206EXPORT_SYMBOL(unlock_page);
1207
Randy Dunlap485bb992006-06-23 02:03:49 -07001208/**
1209 * end_page_writeback - end writeback against a page
1210 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
1212void end_page_writeback(struct page *page)
1213{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001214 /*
1215 * TestClearPageReclaim could be used here but it is an atomic
1216 * operation and overkill in this particular case. Failing to
1217 * shuffle a page marked for immediate reclaim is too mild to
1218 * justify taking an atomic operation penalty at the end of
1219 * ever page writeback.
1220 */
1221 if (PageReclaim(page)) {
1222 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001223 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001224 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001225
1226 if (!test_clear_page_writeback(page))
1227 BUG();
1228
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001229 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 wake_up_page(page, PG_writeback);
1231}
1232EXPORT_SYMBOL(end_page_writeback);
1233
Matthew Wilcox57d99842014-06-04 16:07:45 -07001234/*
1235 * After completing I/O on a page, call this routine to update the page
1236 * flags appropriately
1237 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001238void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001239{
Jens Axboec11f0c02016-08-05 08:11:04 -06001240 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001241 if (!err) {
1242 SetPageUptodate(page);
1243 } else {
1244 ClearPageUptodate(page);
1245 SetPageError(page);
1246 }
1247 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001248 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001249 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001250 struct address_space *mapping;
1251
Matthew Wilcox57d99842014-06-04 16:07:45 -07001252 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001253 mapping = page_mapping(page);
1254 if (mapping)
1255 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001256 }
1257 end_page_writeback(page);
1258 }
1259}
1260EXPORT_SYMBOL_GPL(page_endio);
1261
Randy Dunlap485bb992006-06-23 02:03:49 -07001262/**
1263 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001264 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001266void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Nicholas Piggin62906022016-12-25 13:00:30 +10001268 struct page *page = compound_head(__page);
1269 wait_queue_head_t *q = page_waitqueue(page);
1270 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272EXPORT_SYMBOL(__lock_page);
1273
Nicholas Piggin62906022016-12-25 13:00:30 +10001274int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001275{
Nicholas Piggin62906022016-12-25 13:00:30 +10001276 struct page *page = compound_head(__page);
1277 wait_queue_head_t *q = page_waitqueue(page);
1278 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001279}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001280EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001281
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001282/*
1283 * Return values:
1284 * 1 - page is locked; mmap_sem is still held.
1285 * 0 - page is not locked.
1286 * mmap_sem has been released (up_read()), unless flags had both
1287 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
1288 * which case mmap_sem is still held.
1289 *
1290 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
1291 * with the page locked and the mmap_sem unperturbed.
1292 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001293int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1294 unsigned int flags)
1295{
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001296 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1297 /*
1298 * CAUTION! In this case, mmap_sem is not released
1299 * even though return 0.
1300 */
1301 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1302 return 0;
1303
1304 up_read(&mm->mmap_sem);
1305 if (flags & FAULT_FLAG_KILLABLE)
1306 wait_on_page_locked_killable(page);
1307 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001308 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001309 return 0;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001310 } else {
1311 if (flags & FAULT_FLAG_KILLABLE) {
1312 int ret;
1313
1314 ret = __lock_page_killable(page);
1315 if (ret) {
1316 up_read(&mm->mmap_sem);
1317 return 0;
1318 }
1319 } else
1320 __lock_page(page);
1321 return 1;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001322 }
1323}
1324
Randy Dunlap485bb992006-06-23 02:03:49 -07001325/**
Johannes Weinere7b563b2014-04-03 14:47:44 -07001326 * page_cache_next_hole - find the next hole (not-present entry)
1327 * @mapping: mapping
1328 * @index: index
1329 * @max_scan: maximum range to search
1330 *
1331 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
1332 * lowest indexed hole.
1333 *
1334 * Returns: the index of the hole if found, otherwise returns an index
1335 * outside of the set specified (in which case 'return - index >=
1336 * max_scan' will be true). In rare cases of index wrap-around, 0 will
1337 * be returned.
1338 *
1339 * page_cache_next_hole may be called under rcu_read_lock. However,
1340 * like radix_tree_gang_lookup, this will not atomically search a
1341 * snapshot of the tree at a single point in time. For example, if a
1342 * hole is created at index 5, then subsequently a hole is created at
1343 * index 10, page_cache_next_hole covering both indexes may return 10
1344 * if called under rcu_read_lock.
1345 */
1346pgoff_t page_cache_next_hole(struct address_space *mapping,
1347 pgoff_t index, unsigned long max_scan)
1348{
1349 unsigned long i;
1350
1351 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001352 struct page *page;
1353
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001354 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001355 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001356 break;
1357 index++;
1358 if (index == 0)
1359 break;
1360 }
1361
1362 return index;
1363}
1364EXPORT_SYMBOL(page_cache_next_hole);
1365
1366/**
1367 * page_cache_prev_hole - find the prev hole (not-present entry)
1368 * @mapping: mapping
1369 * @index: index
1370 * @max_scan: maximum range to search
1371 *
1372 * Search backwards in the range [max(index-max_scan+1, 0), index] for
1373 * the first hole.
1374 *
1375 * Returns: the index of the hole if found, otherwise returns an index
1376 * outside of the set specified (in which case 'index - return >=
1377 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
1378 * will be returned.
1379 *
1380 * page_cache_prev_hole may be called under rcu_read_lock. However,
1381 * like radix_tree_gang_lookup, this will not atomically search a
1382 * snapshot of the tree at a single point in time. For example, if a
1383 * hole is created at index 10, then subsequently a hole is created at
1384 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1385 * called under rcu_read_lock.
1386 */
1387pgoff_t page_cache_prev_hole(struct address_space *mapping,
1388 pgoff_t index, unsigned long max_scan)
1389{
1390 unsigned long i;
1391
1392 for (i = 0; i < max_scan; i++) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001393 struct page *page;
1394
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001395 page = radix_tree_lookup(&mapping->i_pages, index);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001396 if (!page || radix_tree_exceptional_entry(page))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001397 break;
1398 index--;
1399 if (index == ULONG_MAX)
1400 break;
1401 }
1402
1403 return index;
1404}
1405EXPORT_SYMBOL(page_cache_prev_hole);
1406
1407/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001408 * find_get_entry - find and get a page cache entry
Randy Dunlap485bb992006-06-23 02:03:49 -07001409 * @mapping: the address_space to search
Johannes Weiner0cd61442014-04-03 14:47:46 -07001410 * @offset: the page cache index
Randy Dunlap485bb992006-06-23 02:03:49 -07001411 *
Johannes Weiner0cd61442014-04-03 14:47:46 -07001412 * Looks up the page cache slot at @mapping & @offset. If there is a
1413 * page cache page, it is returned with an increased refcount.
1414 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001415 * If the slot holds a shadow entry of a previously evicted page, or a
1416 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001417 *
1418 * Otherwise, %NULL is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
Johannes Weiner0cd61442014-04-03 14:47:46 -07001420struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Nick Piggina60637c2008-07-25 19:45:31 -07001422 void **pagep;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001423 struct page *head, *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Nick Piggina60637c2008-07-25 19:45:31 -07001425 rcu_read_lock();
1426repeat:
1427 page = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001428 pagep = radix_tree_lookup_slot(&mapping->i_pages, offset);
Nick Piggina60637c2008-07-25 19:45:31 -07001429 if (pagep) {
1430 page = radix_tree_deref_slot(pagep);
Nick Piggin27d20fd2010-11-11 14:05:19 -08001431 if (unlikely(!page))
1432 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001433 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001434 if (radix_tree_deref_retry(page))
1435 goto repeat;
1436 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001437 * A shadow entry of a recently evicted page,
1438 * or a swap entry from shmem/tmpfs. Return
1439 * it without attempting to raise page count.
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001440 */
1441 goto out;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001442 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001443
1444 head = compound_head(page);
1445 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001446 goto repeat;
1447
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001448 /* The page was split under us? */
1449 if (compound_head(page) != head) {
1450 put_page(head);
1451 goto repeat;
1452 }
1453
Nick Piggina60637c2008-07-25 19:45:31 -07001454 /*
1455 * Has the page moved?
1456 * This is part of the lockless pagecache protocol. See
1457 * include/linux/pagemap.h for details.
1458 */
1459 if (unlikely(page != *pagep)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001460 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001461 goto repeat;
1462 }
1463 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001464out:
Nick Piggina60637c2008-07-25 19:45:31 -07001465 rcu_read_unlock();
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return page;
1468}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001469EXPORT_SYMBOL(find_get_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Randy Dunlap485bb992006-06-23 02:03:49 -07001471/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001472 * find_lock_entry - locate, pin and lock a page cache entry
1473 * @mapping: the address_space to search
1474 * @offset: the page cache index
1475 *
1476 * Looks up the page cache slot at @mapping & @offset. If there is a
1477 * page cache page, it is returned locked and with an increased
1478 * refcount.
1479 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001480 * If the slot holds a shadow entry of a previously evicted page, or a
1481 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001482 *
1483 * Otherwise, %NULL is returned.
1484 *
1485 * find_lock_entry() may sleep.
1486 */
1487struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct page *page;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491repeat:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001492 page = find_get_entry(mapping, offset);
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001493 if (page && !radix_tree_exception(page)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001494 lock_page(page);
1495 /* Has the page been truncated? */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001496 if (unlikely(page_mapping(page) != mapping)) {
Nick Piggina60637c2008-07-25 19:45:31 -07001497 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001498 put_page(page);
Nick Piggina60637c2008-07-25 19:45:31 -07001499 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001501 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return page;
1504}
Johannes Weiner0cd61442014-04-03 14:47:46 -07001505EXPORT_SYMBOL(find_lock_entry);
1506
1507/**
Mel Gorman2457aec2014-06-04 16:10:31 -07001508 * pagecache_get_page - find and get a page reference
Johannes Weiner0cd61442014-04-03 14:47:46 -07001509 * @mapping: the address_space to search
1510 * @offset: the page index
Mel Gorman2457aec2014-06-04 16:10:31 -07001511 * @fgp_flags: PCG flags
Michal Hocko45f87de2014-12-29 20:30:35 +01001512 * @gfp_mask: gfp mask to use for the page cache data page allocation
Johannes Weiner0cd61442014-04-03 14:47:46 -07001513 *
Mel Gorman2457aec2014-06-04 16:10:31 -07001514 * Looks up the page cache slot at @mapping & @offset.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001515 *
Randy Dunlap75325182014-07-30 16:08:37 -07001516 * PCG flags modify how the page is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001517 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001518 * @fgp_flags can be:
1519 *
1520 * - FGP_ACCESSED: the page will be marked accessed
1521 * - FGP_LOCK: Page is return locked
1522 * - FGP_CREAT: If page is not present then a new page is allocated using
1523 * @gfp_mask and added to the page cache and the VM's LRU
1524 * list. The page is returned locked and with an increased
1525 * refcount. Otherwise, NULL is returned.
Mel Gorman2457aec2014-06-04 16:10:31 -07001526 *
1527 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1528 * if the GFP flags specified for FGP_CREAT are atomic.
1529 *
1530 * If there is a page cache page, it is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001531 */
Mel Gorman2457aec2014-06-04 16:10:31 -07001532struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
Michal Hocko45f87de2014-12-29 20:30:35 +01001533 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Nick Piggineb2be182007-10-16 01:24:57 -07001535 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537repeat:
Mel Gorman2457aec2014-06-04 16:10:31 -07001538 page = find_get_entry(mapping, offset);
1539 if (radix_tree_exceptional_entry(page))
1540 page = NULL;
1541 if (!page)
1542 goto no_page;
1543
1544 if (fgp_flags & FGP_LOCK) {
1545 if (fgp_flags & FGP_NOWAIT) {
1546 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001547 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001548 return NULL;
1549 }
1550 } else {
1551 lock_page(page);
1552 }
1553
1554 /* Has the page been truncated? */
1555 if (unlikely(page->mapping != mapping)) {
1556 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001557 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001558 goto repeat;
1559 }
1560 VM_BUG_ON_PAGE(page->index != offset, page);
1561 }
1562
1563 if (page && (fgp_flags & FGP_ACCESSED))
1564 mark_page_accessed(page);
1565
1566no_page:
1567 if (!page && (fgp_flags & FGP_CREAT)) {
1568 int err;
1569 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001570 gfp_mask |= __GFP_WRITE;
1571 if (fgp_flags & FGP_NOFS)
1572 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001573
Michal Hocko45f87de2014-12-29 20:30:35 +01001574 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001575 if (!page)
1576 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001577
1578 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1579 fgp_flags |= FGP_LOCK;
1580
Hugh Dickinseb39d612014-08-06 16:06:43 -07001581 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001582 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001583 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001584
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07001585 err = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001586 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001587 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001588 page = NULL;
1589 if (err == -EEXIST)
1590 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return page;
1595}
Mel Gorman2457aec2014-06-04 16:10:31 -07001596EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001599 * find_get_entries - gang pagecache lookup
1600 * @mapping: The address_space to search
1601 * @start: The starting page cache index
1602 * @nr_entries: The maximum number of entries
1603 * @entries: Where the resulting entries are placed
1604 * @indices: The cache indices corresponding to the entries in @entries
1605 *
1606 * find_get_entries() will search for and return a group of up to
1607 * @nr_entries entries in the mapping. The entries are placed at
1608 * @entries. find_get_entries() takes a reference against any actual
1609 * pages it returns.
1610 *
1611 * The search returns a group of mapping-contiguous page cache entries
1612 * with ascending indexes. There may be holes in the indices due to
1613 * not-present pages.
1614 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001615 * Any shadow entries of evicted pages, or swap entries from
1616 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001617 *
1618 * find_get_entries() returns the number of pages and shadow entries
1619 * which were found.
1620 */
1621unsigned find_get_entries(struct address_space *mapping,
1622 pgoff_t start, unsigned int nr_entries,
1623 struct page **entries, pgoff_t *indices)
1624{
1625 void **slot;
1626 unsigned int ret = 0;
1627 struct radix_tree_iter iter;
1628
1629 if (!nr_entries)
1630 return 0;
1631
1632 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001633 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001634 struct page *head, *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001635repeat:
1636 page = radix_tree_deref_slot(slot);
1637 if (unlikely(!page))
1638 continue;
1639 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001640 if (radix_tree_deref_retry(page)) {
1641 slot = radix_tree_iter_retry(&iter);
1642 continue;
1643 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001644 /*
Ross Zwislerf9fe48b2016-01-22 15:10:40 -08001645 * A shadow entry of a recently evicted page, a swap
1646 * entry from shmem/tmpfs or a DAX entry. Return it
1647 * without attempting to raise page count.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001648 */
1649 goto export;
1650 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001651
1652 head = compound_head(page);
1653 if (!page_cache_get_speculative(head))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001654 goto repeat;
1655
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001656 /* The page was split under us? */
1657 if (compound_head(page) != head) {
1658 put_page(head);
1659 goto repeat;
1660 }
1661
Johannes Weiner0cd61442014-04-03 14:47:46 -07001662 /* Has the page moved? */
1663 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001664 put_page(head);
Johannes Weiner0cd61442014-04-03 14:47:46 -07001665 goto repeat;
1666 }
1667export:
1668 indices[ret] = iter.index;
1669 entries[ret] = page;
1670 if (++ret == nr_entries)
1671 break;
1672 }
1673 rcu_read_unlock();
1674 return ret;
1675}
1676
1677/**
Jan Karab947cee2017-09-06 16:21:21 -07001678 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * @mapping: The address_space to search
1680 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001681 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * @nr_pages: The maximum number of pages
1683 * @pages: Where the resulting pages are placed
1684 *
Jan Karab947cee2017-09-06 16:21:21 -07001685 * find_get_pages_range() will search for and return a group of up to @nr_pages
1686 * pages in the mapping starting at index @start and up to index @end
1687 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
1688 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 *
1690 * The search returns a group of mapping-contiguous pages with ascending
1691 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07001692 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 *
Jan Karab947cee2017-09-06 16:21:21 -07001694 * find_get_pages_range() returns the number of pages which were found. If this
1695 * number is smaller than @nr_pages, the end of specified range has been
1696 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
Jan Karab947cee2017-09-06 16:21:21 -07001698unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
1699 pgoff_t end, unsigned int nr_pages,
1700 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001702 struct radix_tree_iter iter;
1703 void **slot;
1704 unsigned ret = 0;
1705
1706 if (unlikely(!nr_pages))
1707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Nick Piggina60637c2008-07-25 19:45:31 -07001709 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001710 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, *start) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001711 struct page *head, *page;
Jan Karab947cee2017-09-06 16:21:21 -07001712
1713 if (iter.index > end)
1714 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001715repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001716 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001717 if (unlikely(!page))
1718 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001719
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001720 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001721 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001722 slot = radix_tree_iter_retry(&iter);
1723 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001724 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001725 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001726 * A shadow entry of a recently evicted page,
1727 * or a swap entry from shmem/tmpfs. Skip
1728 * over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001729 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001730 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001731 }
Nick Piggina60637c2008-07-25 19:45:31 -07001732
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001733 head = compound_head(page);
1734 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001735 goto repeat;
1736
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001737 /* The page was split under us? */
1738 if (compound_head(page) != head) {
1739 put_page(head);
1740 goto repeat;
1741 }
1742
Nick Piggina60637c2008-07-25 19:45:31 -07001743 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001744 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001745 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001746 goto repeat;
1747 }
1748
1749 pages[ret] = page;
Jan Karab947cee2017-09-06 16:21:21 -07001750 if (++ret == nr_pages) {
1751 *start = pages[ret - 1]->index + 1;
1752 goto out;
1753 }
Nick Piggina60637c2008-07-25 19:45:31 -07001754 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001755
Jan Karab947cee2017-09-06 16:21:21 -07001756 /*
1757 * We come here when there is no page beyond @end. We take care to not
1758 * overflow the index @start as it confuses some of the callers. This
1759 * breaks the iteration when there is page at index -1 but that is
1760 * already broken anyway.
1761 */
1762 if (end == (pgoff_t)-1)
1763 *start = (pgoff_t)-1;
1764 else
1765 *start = end + 1;
1766out:
Nick Piggina60637c2008-07-25 19:45:31 -07001767 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return ret;
1770}
1771
Jens Axboeebf43502006-04-27 08:46:01 +02001772/**
1773 * find_get_pages_contig - gang contiguous pagecache lookup
1774 * @mapping: The address_space to search
1775 * @index: The starting page index
1776 * @nr_pages: The maximum number of pages
1777 * @pages: Where the resulting pages are placed
1778 *
1779 * find_get_pages_contig() works exactly like find_get_pages(), except
1780 * that the returned number of pages are guaranteed to be contiguous.
1781 *
1782 * find_get_pages_contig() returns the number of pages which were found.
1783 */
1784unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1785 unsigned int nr_pages, struct page **pages)
1786{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001787 struct radix_tree_iter iter;
1788 void **slot;
1789 unsigned int ret = 0;
1790
1791 if (unlikely(!nr_pages))
1792 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02001793
Nick Piggina60637c2008-07-25 19:45:31 -07001794 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001795 radix_tree_for_each_contig(slot, &mapping->i_pages, &iter, index) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001796 struct page *head, *page;
Nick Piggina60637c2008-07-25 19:45:31 -07001797repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001798 page = radix_tree_deref_slot(slot);
1799 /* The hole, there no reason to continue */
Nick Piggina60637c2008-07-25 19:45:31 -07001800 if (unlikely(!page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001801 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001802
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001803 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001804 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001805 slot = radix_tree_iter_retry(&iter);
1806 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001807 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001808 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001809 * A shadow entry of a recently evicted page,
1810 * or a swap entry from shmem/tmpfs. Stop
1811 * looking for contiguous pages.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001812 */
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001813 break;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001814 }
Nick Piggina60637c2008-07-25 19:45:31 -07001815
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001816 head = compound_head(page);
1817 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001818 goto repeat;
1819
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001820 /* The page was split under us? */
1821 if (compound_head(page) != head) {
1822 put_page(head);
1823 goto repeat;
1824 }
1825
Nick Piggina60637c2008-07-25 19:45:31 -07001826 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001827 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001828 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001829 goto repeat;
1830 }
1831
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001832 /*
1833 * must check mapping and index after taking the ref.
1834 * otherwise we can get both false positives and false
1835 * negatives, which is just confusing to the caller.
1836 */
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001837 if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001838 put_page(page);
Nick Piggin9cbb4cb2011-01-13 15:45:51 -08001839 break;
1840 }
1841
Nick Piggina60637c2008-07-25 19:45:31 -07001842 pages[ret] = page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001843 if (++ret == nr_pages)
1844 break;
Jens Axboeebf43502006-04-27 08:46:01 +02001845 }
Nick Piggina60637c2008-07-25 19:45:31 -07001846 rcu_read_unlock();
1847 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02001848}
David Howellsef71c152007-05-09 02:33:44 -07001849EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02001850
Randy Dunlap485bb992006-06-23 02:03:49 -07001851/**
Jan Kara72b045a2017-11-15 17:34:33 -08001852 * find_get_pages_range_tag - find and return pages in given range matching @tag
Randy Dunlap485bb992006-06-23 02:03:49 -07001853 * @mapping: the address_space to search
1854 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08001855 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07001856 * @tag: the tag index
1857 * @nr_pages: the maximum number of pages
1858 * @pages: where the resulting pages are placed
1859 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 * Like find_get_pages, except we only return pages which are tagged with
Randy Dunlap485bb992006-06-23 02:03:49 -07001861 * @tag. We update @index to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 */
Jan Kara72b045a2017-11-15 17:34:33 -08001863unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
1864 pgoff_t end, int tag, unsigned int nr_pages,
1865 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001867 struct radix_tree_iter iter;
1868 void **slot;
1869 unsigned ret = 0;
1870
1871 if (unlikely(!nr_pages))
1872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Nick Piggina60637c2008-07-25 19:45:31 -07001874 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001875 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, *index, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001876 struct page *head, *page;
Jan Kara72b045a2017-11-15 17:34:33 -08001877
1878 if (iter.index > end)
1879 break;
Nick Piggina60637c2008-07-25 19:45:31 -07001880repeat:
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001881 page = radix_tree_deref_slot(slot);
Nick Piggina60637c2008-07-25 19:45:31 -07001882 if (unlikely(!page))
1883 continue;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07001884
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001885 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001886 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001887 slot = radix_tree_iter_retry(&iter);
1888 continue;
Hugh Dickins8079b1c2011-08-03 16:21:28 -07001889 }
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001890 /*
Johannes Weiner139b6a62014-05-06 12:50:05 -07001891 * A shadow entry of a recently evicted page.
1892 *
1893 * Those entries should never be tagged, but
1894 * this tree walk is lockless and the tags are
1895 * looked up in bulk, one radix tree node at a
1896 * time, so there is a sizable window for page
1897 * reclaim to evict a page we saw tagged.
1898 *
1899 * Skip over it.
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001900 */
Johannes Weiner139b6a62014-05-06 12:50:05 -07001901 continue;
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001902 }
Nick Piggina60637c2008-07-25 19:45:31 -07001903
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001904 head = compound_head(page);
1905 if (!page_cache_get_speculative(head))
Nick Piggina60637c2008-07-25 19:45:31 -07001906 goto repeat;
1907
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001908 /* The page was split under us? */
1909 if (compound_head(page) != head) {
1910 put_page(head);
1911 goto repeat;
1912 }
1913
Nick Piggina60637c2008-07-25 19:45:31 -07001914 /* Has the page moved? */
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07001915 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001916 put_page(head);
Nick Piggina60637c2008-07-25 19:45:31 -07001917 goto repeat;
1918 }
1919
1920 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08001921 if (++ret == nr_pages) {
1922 *index = pages[ret - 1]->index + 1;
1923 goto out;
1924 }
Nick Piggina60637c2008-07-25 19:45:31 -07001925 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07001926
Jan Kara72b045a2017-11-15 17:34:33 -08001927 /*
1928 * We come here when we got at @end. We take care to not overflow the
1929 * index @index as it confuses some of the callers. This breaks the
1930 * iteration when there is page at index -1 but that is already broken
1931 * anyway.
1932 */
1933 if (end == (pgoff_t)-1)
1934 *index = (pgoff_t)-1;
1935 else
1936 *index = end + 1;
1937out:
Nick Piggina60637c2008-07-25 19:45:31 -07001938 rcu_read_unlock();
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return ret;
1941}
Jan Kara72b045a2017-11-15 17:34:33 -08001942EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001944/**
1945 * find_get_entries_tag - find and return entries that match @tag
1946 * @mapping: the address_space to search
1947 * @start: the starting page cache index
1948 * @tag: the tag index
1949 * @nr_entries: the maximum number of entries
1950 * @entries: where the resulting entries are placed
1951 * @indices: the cache indices corresponding to the entries in @entries
1952 *
1953 * Like find_get_entries, except we only return entries which are tagged with
1954 * @tag.
1955 */
1956unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1957 int tag, unsigned int nr_entries,
1958 struct page **entries, pgoff_t *indices)
1959{
1960 void **slot;
1961 unsigned int ret = 0;
1962 struct radix_tree_iter iter;
1963
1964 if (!nr_entries)
1965 return 0;
1966
1967 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001968 radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, start, tag) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001969 struct page *head, *page;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001970repeat:
1971 page = radix_tree_deref_slot(slot);
1972 if (unlikely(!page))
1973 continue;
1974 if (radix_tree_exception(page)) {
1975 if (radix_tree_deref_retry(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07001976 slot = radix_tree_iter_retry(&iter);
1977 continue;
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001978 }
1979
1980 /*
1981 * A shadow entry of a recently evicted page, a swap
1982 * entry from shmem/tmpfs or a DAX entry. Return it
1983 * without attempting to raise page count.
1984 */
1985 goto export;
1986 }
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001987
1988 head = compound_head(page);
1989 if (!page_cache_get_speculative(head))
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001990 goto repeat;
1991
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001992 /* The page was split under us? */
1993 if (compound_head(page) != head) {
1994 put_page(head);
1995 goto repeat;
1996 }
1997
Ross Zwisler7e7f7742016-01-22 15:10:44 -08001998 /* Has the page moved? */
1999 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002000 put_page(head);
Ross Zwisler7e7f7742016-01-22 15:10:44 -08002001 goto repeat;
2002 }
2003export:
2004 indices[ret] = iter.index;
2005 entries[ret] = page;
2006 if (++ret == nr_entries)
2007 break;
2008 }
2009 rcu_read_unlock();
2010 return ret;
2011}
2012EXPORT_SYMBOL(find_get_entries_tag);
2013
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002014/*
2015 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2016 * a _large_ part of the i/o request. Imagine the worst scenario:
2017 *
2018 * ---R__________________________________________B__________
2019 * ^ reading here ^ bad block(assume 4k)
2020 *
2021 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2022 * => failing the whole request => read(R) => read(R+1) =>
2023 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2024 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2025 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2026 *
2027 * It is going insane. Fix it by quickly scaling down the readahead size.
2028 */
2029static void shrink_readahead_size_eio(struct file *filp,
2030 struct file_ra_state *ra)
2031{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002032 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002033}
2034
Randy Dunlap485bb992006-06-23 02:03:49 -07002035/**
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002036 * generic_file_buffered_read - generic file read routine
2037 * @iocb: the iocb to read
Al Viro6e58e792014-02-03 17:07:03 -05002038 * @iter: data destination
2039 * @written: already copied
Randy Dunlap485bb992006-06-23 02:03:49 -07002040 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 * This is a generic file read routine, and uses the
Randy Dunlap485bb992006-06-23 02:03:49 -07002042 * mapping->a_ops->readpage() function for the actual low-level stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 *
2044 * This is really ugly. But the goto's actually try to clarify some
2045 * of the logic when it comes to error handling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 */
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002047static ssize_t generic_file_buffered_read(struct kiocb *iocb,
Al Viro6e58e792014-02-03 17:07:03 -05002048 struct iov_iter *iter, ssize_t written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002050 struct file *filp = iocb->ki_filp;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002051 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 struct inode *inode = mapping->host;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002053 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002054 loff_t *ppos = &iocb->ki_pos;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002055 pgoff_t index;
2056 pgoff_t last_index;
2057 pgoff_t prev_index;
2058 unsigned long offset; /* offset into pagecache page */
Jan Karaec0f1632007-05-06 14:49:25 -07002059 unsigned int prev_offset;
Al Viro6e58e792014-02-03 17:07:03 -05002060 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Wei Fangc2a97372016-10-07 17:01:52 -07002062 if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002063 return 0;
Wei Fangc2a97372016-10-07 17:01:52 -07002064 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2065
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 index = *ppos >> PAGE_SHIFT;
2067 prev_index = ra->prev_pos >> PAGE_SHIFT;
2068 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
2069 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
2070 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 for (;;) {
2073 struct page *page;
Fengguang Wu57f6b962007-10-16 01:24:37 -07002074 pgoff_t end_index;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002075 loff_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 unsigned long nr, ret;
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079find_page:
Michal Hocko5abf1862017-02-03 13:13:29 -08002080 if (fatal_signal_pending(current)) {
2081 error = -EINTR;
2082 goto out;
2083 }
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 page = find_get_page(mapping, index);
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002086 if (!page) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002087 if (iocb->ki_flags & IOCB_NOWAIT)
2088 goto would_block;
Rusty Russellcf914a72007-07-19 01:48:08 -07002089 page_cache_sync_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002090 ra, filp,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002091 index, last_index - index);
2092 page = find_get_page(mapping, index);
2093 if (unlikely(page == NULL))
2094 goto no_cached_page;
2095 }
2096 if (PageReadahead(page)) {
Rusty Russellcf914a72007-07-19 01:48:08 -07002097 page_cache_async_readahead(mapping,
Fengguang Wu7ff81072007-10-16 01:24:35 -07002098 ra, filp, page,
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002099 index, last_index - index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002101 if (!PageUptodate(page)) {
Milosz Tanski3239d832017-08-29 16:13:19 +02002102 if (iocb->ki_flags & IOCB_NOWAIT) {
2103 put_page(page);
2104 goto would_block;
2105 }
2106
Mel Gormanebded022016-03-15 14:55:39 -07002107 /*
2108 * See comment in do_read_cache_page on why
2109 * wait_on_page_locked is used to avoid unnecessarily
2110 * serialisations and why it's safe.
2111 */
Bart Van Asschec4b209a2016-10-07 16:58:33 -07002112 error = wait_on_page_locked_killable(page);
2113 if (unlikely(error))
2114 goto readpage_error;
Mel Gormanebded022016-03-15 14:55:39 -07002115 if (PageUptodate(page))
2116 goto page_ok;
2117
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 if (inode->i_blkbits == PAGE_SHIFT ||
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002119 !mapping->a_ops->is_partially_uptodate)
2120 goto page_not_up_to_date;
Eryu Guan6d6d36b2016-11-01 15:43:07 +08002121 /* pipes can't handle partially uptodate pages */
2122 if (unlikely(iter->type & ITER_PIPE))
2123 goto page_not_up_to_date;
Nick Piggin529ae9a2008-08-02 12:01:03 +02002124 if (!trylock_page(page))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002125 goto page_not_up_to_date;
Dave Hansen8d056cb2010-11-11 14:05:15 -08002126 /* Did it get truncated before we got the lock? */
2127 if (!page->mapping)
2128 goto page_not_up_to_date_locked;
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002129 if (!mapping->a_ops->is_partially_uptodate(page,
Al Viro6e58e792014-02-03 17:07:03 -05002130 offset, iter->count))
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002131 goto page_not_up_to_date_locked;
2132 unlock_page(page);
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134page_ok:
NeilBrowna32ea1e2007-07-17 04:03:04 -07002135 /*
2136 * i_size must be checked after we know the page is Uptodate.
2137 *
2138 * Checking i_size after the check allows us to calculate
2139 * the correct value for "nr", which means the zero-filled
2140 * part of the page is not copied back to userspace (unless
2141 * another truncate extends the file - this is desired though).
2142 */
2143
2144 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 end_index = (isize - 1) >> PAGE_SHIFT;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002146 if (unlikely(!isize || index > end_index)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002148 goto out;
2149 }
2150
2151 /* nr is the maximum number of bytes to copy from this page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002152 nr = PAGE_SIZE;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002153 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002154 nr = ((isize - 1) & ~PAGE_MASK) + 1;
NeilBrowna32ea1e2007-07-17 04:03:04 -07002155 if (nr <= offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002156 put_page(page);
NeilBrowna32ea1e2007-07-17 04:03:04 -07002157 goto out;
2158 }
2159 }
2160 nr = nr - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* If users can be writing to this page using arbitrary
2163 * virtual addresses, take care about potential aliasing
2164 * before reading the page on the kernel side.
2165 */
2166 if (mapping_writably_mapped(mapping))
2167 flush_dcache_page(page);
2168
2169 /*
Jan Karaec0f1632007-05-06 14:49:25 -07002170 * When a sequential read accesses a page several times,
2171 * only mark it as accessed the first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 */
Jan Karaec0f1632007-05-06 14:49:25 -07002173 if (prev_index != index || offset != prev_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 mark_page_accessed(page);
2175 prev_index = index;
2176
2177 /*
2178 * Ok, we have the page, and it's up-to-date, so
2179 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 */
Al Viro6e58e792014-02-03 17:07:03 -05002181
2182 ret = copy_page_to_iter(page, offset, nr, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002184 index += offset >> PAGE_SHIFT;
2185 offset &= ~PAGE_MASK;
Jan Kara6ce745e2007-05-06 14:49:26 -07002186 prev_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002188 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002189 written += ret;
2190 if (!iov_iter_count(iter))
2191 goto out;
2192 if (ret < nr) {
2193 error = -EFAULT;
2194 goto out;
2195 }
2196 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198page_not_up_to_date:
2199 /* Get exclusive access to the page ... */
Oleg Nesterov85462322008-06-08 21:20:43 +04002200 error = lock_page_killable(page);
2201 if (unlikely(error))
2202 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002204page_not_up_to_date_locked:
Nick Pigginda6052f2006-09-25 23:31:35 -07002205 /* Did it get truncated before we got the lock? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (!page->mapping) {
2207 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002208 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 continue;
2210 }
2211
2212 /* Did somebody else fill it already? */
2213 if (PageUptodate(page)) {
2214 unlock_page(page);
2215 goto page_ok;
2216 }
2217
2218readpage:
Jeff Moyer91803b42010-05-26 11:49:40 -04002219 /*
2220 * A previous I/O error may have been due to temporary
2221 * failures, eg. multipath errors.
2222 * PG_error will be set again if readpage fails.
2223 */
2224 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* Start the actual read. The read will unlock the page. */
2226 error = mapping->a_ops->readpage(filp, page);
2227
Zach Brown994fc28c2005-12-15 14:28:17 -08002228 if (unlikely(error)) {
2229 if (error == AOP_TRUNCATED_PAGE) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002230 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002231 error = 0;
Zach Brown994fc28c2005-12-15 14:28:17 -08002232 goto find_page;
2233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto readpage_error;
Zach Brown994fc28c2005-12-15 14:28:17 -08002235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 if (!PageUptodate(page)) {
Oleg Nesterov85462322008-06-08 21:20:43 +04002238 error = lock_page_killable(page);
2239 if (unlikely(error))
2240 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (!PageUptodate(page)) {
2242 if (page->mapping == NULL) {
2243 /*
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01002244 * invalidate_mapping_pages got it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 */
2246 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002247 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto find_page;
2249 }
2250 unlock_page(page);
Fengguang Wu7ff81072007-10-16 01:24:35 -07002251 shrink_readahead_size_eio(filp, ra);
Oleg Nesterov85462322008-06-08 21:20:43 +04002252 error = -EIO;
2253 goto readpage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 unlock_page(page);
2256 }
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto page_ok;
2259
2260readpage_error:
2261 /* UHHUH! A synchronous read error occurred. Report it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002262 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto out;
2264
2265no_cached_page:
2266 /*
2267 * Ok, it wasn't cached, so we need to create a new
2268 * page..
2269 */
Mel Gorman453f85d2017-11-15 17:38:03 -08002270 page = page_cache_alloc(mapping);
Nick Piggineb2be182007-10-16 01:24:57 -07002271 if (!page) {
Al Viro6e58e792014-02-03 17:07:03 -05002272 error = -ENOMEM;
Nick Piggineb2be182007-10-16 01:24:57 -07002273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Michal Hocko6afdb852015-06-24 16:58:06 -07002275 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -08002276 mapping_gfp_constraint(mapping, GFP_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (error) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002278 put_page(page);
Al Viro6e58e792014-02-03 17:07:03 -05002279 if (error == -EEXIST) {
2280 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 goto find_page;
Al Viro6e58e792014-02-03 17:07:03 -05002282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 goto out;
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto readpage;
2286 }
2287
Milosz Tanski3239d832017-08-29 16:13:19 +02002288would_block:
2289 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290out:
Fengguang Wu7ff81072007-10-16 01:24:35 -07002291 ra->prev_pos = prev_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002292 ra->prev_pos <<= PAGE_SHIFT;
Fengguang Wu7ff81072007-10-16 01:24:35 -07002293 ra->prev_pos |= prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002295 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002296 file_accessed(filp);
Al Viro6e58e792014-02-03 17:07:03 -05002297 return written ? written : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Randy Dunlap485bb992006-06-23 02:03:49 -07002300/**
Al Viro6abd2322014-04-04 14:20:57 -04002301 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002302 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002303 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002304 *
Al Viro6abd2322014-04-04 14:20:57 -04002305 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 * that can use the page cache directly.
2307 */
2308ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002309generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002311 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002312 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002313
2314 if (!count)
2315 goto out; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Al Viro2ba48ce2015-04-09 13:52:01 -04002317 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002318 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002319 struct address_space *mapping = file->f_mapping;
2320 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002321 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002324 if (iocb->ki_flags & IOCB_NOWAIT) {
2325 if (filemap_range_has_page(mapping, iocb->ki_pos,
2326 iocb->ki_pos + count - 1))
2327 return -EAGAIN;
2328 } else {
2329 retval = filemap_write_and_wait_range(mapping,
2330 iocb->ki_pos,
2331 iocb->ki_pos + count - 1);
2332 if (retval < 0)
2333 goto out;
2334 }
Al Viroed978a82014-03-05 22:53:04 -05002335
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002336 file_accessed(file);
2337
Al Viro5ecda132017-04-13 14:13:36 -04002338 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002339 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002340 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002341 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002342 }
Al Viro5b47d592017-05-08 13:54:47 -04002343 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002344
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002345 /*
2346 * Btrfs can have a short DIO read if we encounter
2347 * compressed extents, so if there was an error, or if
2348 * we've already read everything we wanted to, or if
2349 * there was a short read because we hit EOF, go ahead
2350 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002351 * the rest of the read. Buffered reads will not work for
2352 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002353 */
Al Viro5ecda132017-04-13 14:13:36 -04002354 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002355 IS_DAX(inode))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002359 retval = generic_file_buffered_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360out:
2361 return retval;
2362}
Al Viroed978a82014-03-05 22:53:04 -05002363EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365#ifdef CONFIG_MMU
Randy Dunlap485bb992006-06-23 02:03:49 -07002366/**
2367 * page_cache_read - adds requested page to the page cache if not already there
2368 * @file: file to read
2369 * @offset: page index
Randy Dunlap62eb3202016-02-11 16:12:58 -08002370 * @gfp_mask: memory allocation flags
Randy Dunlap485bb992006-06-23 02:03:49 -07002371 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 * This adds the requested page to the page cache if it isn't already there,
2373 * and schedules an I/O to read in its contents from disk.
2374 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002375static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 struct address_space *mapping = file->f_mapping;
Paul McQuade99dadfd2014-10-09 15:29:03 -07002378 struct page *page;
Zach Brown994fc28c2005-12-15 14:28:17 -08002379 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Zach Brown994fc28c2005-12-15 14:28:17 -08002381 do {
Mel Gorman453f85d2017-11-15 17:38:03 -08002382 page = __page_cache_alloc(gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002383 if (!page)
2384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Matthew Wilcoxabc1be12018-04-20 14:56:20 -07002386 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask);
Zach Brown994fc28c2005-12-15 14:28:17 -08002387 if (ret == 0)
2388 ret = mapping->a_ops->readpage(file, page);
2389 else if (ret == -EEXIST)
2390 ret = 0; /* losing race to add is OK */
2391
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002392 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Zach Brown994fc28c2005-12-15 14:28:17 -08002394 } while (ret == AOP_TRUNCATED_PAGE);
Paul McQuade99dadfd2014-10-09 15:29:03 -07002395
Zach Brown994fc28c2005-12-15 14:28:17 -08002396 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399#define MMAP_LOTSAMISS (100)
2400
Linus Torvaldsef00e082009-06-16 15:31:25 -07002401/*
2402 * Synchronous readahead happens when we don't even find
2403 * a page in the page cache at all.
2404 */
2405static void do_sync_mmap_readahead(struct vm_area_struct *vma,
2406 struct file_ra_state *ra,
2407 struct file *file,
2408 pgoff_t offset)
2409{
Linus Torvaldsef00e082009-06-16 15:31:25 -07002410 struct address_space *mapping = file->f_mapping;
2411
2412 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002413 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002414 return;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002415 if (!ra->ra_pages)
2416 return;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002417
Joe Perches64363aa2013-07-08 16:00:18 -07002418 if (vma->vm_flags & VM_SEQ_READ) {
Wu Fengguang7ffc59b2009-06-16 15:31:38 -07002419 page_cache_sync_readahead(mapping, ra, file, offset,
2420 ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002421 return;
2422 }
2423
Andi Kleen207d04b2011-05-24 17:12:29 -07002424 /* Avoid banging the cache line if not needed */
2425 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002426 ra->mmap_miss++;
2427
2428 /*
2429 * Do we miss much more than hit in this file? If so,
2430 * stop bothering with read-ahead. It will only hurt.
2431 */
2432 if (ra->mmap_miss > MMAP_LOTSAMISS)
2433 return;
2434
Wu Fengguangd30a1102009-06-16 15:31:30 -07002435 /*
2436 * mmap read-around
2437 */
Roman Gushchin600e19a2015-11-05 18:47:08 -08002438 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
2439 ra->size = ra->ra_pages;
2440 ra->async_size = ra->ra_pages / 4;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002441 ra_submit(ra, mapping, file);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002442}
2443
2444/*
2445 * Asynchronous readahead happens when we find the page and PG_readahead,
2446 * so we want to possibly extend the readahead further..
2447 */
2448static void do_async_mmap_readahead(struct vm_area_struct *vma,
2449 struct file_ra_state *ra,
2450 struct file *file,
2451 struct page *page,
2452 pgoff_t offset)
2453{
2454 struct address_space *mapping = file->f_mapping;
2455
2456 /* If we don't want any read-ahead, don't bother */
Joe Perches64363aa2013-07-08 16:00:18 -07002457 if (vma->vm_flags & VM_RAND_READ)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002458 return;
2459 if (ra->mmap_miss > 0)
2460 ra->mmap_miss--;
2461 if (PageReadahead(page))
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002462 page_cache_async_readahead(mapping, ra, file,
2463 page, offset, ra->ra_pages);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002464}
2465
Randy Dunlap485bb992006-06-23 02:03:49 -07002466/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002467 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002468 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002469 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002470 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 * mapped memory region to read in file data during a page fault.
2472 *
2473 * The goto's are kind of ugly, but this streamlines the normal case of having
2474 * it in the page cache, and handles the special cases reasonably without
2475 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002476 *
2477 * vma->vm_mm->mmap_sem must be held on entry.
2478 *
2479 * If our return value has VM_FAULT_RETRY set, it's because
2480 * lock_page_or_retry() returned 0.
2481 * The mmap_sem has usually been released in this case.
2482 * See __lock_page_or_retry() for the exception.
2483 *
2484 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
2485 * has not been released.
2486 *
2487 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002489vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08002492 struct file *file = vmf->vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 struct address_space *mapping = file->f_mapping;
2494 struct file_ra_state *ra = &file->f_ra;
2495 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002496 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002497 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002499 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002501 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2502 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07002503 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002506 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07002508 page = find_get_page(mapping, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002509 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07002511 * We found the page, so try async readahead before
2512 * waiting for the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 */
Dave Jiang11bac802017-02-24 14:56:41 -08002514 do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
Shaohua Li45cac652012-10-08 16:32:19 -07002515 } else if (!page) {
Linus Torvaldsef00e082009-06-16 15:31:25 -07002516 /* No page in the page cache at all */
Dave Jiang11bac802017-02-24 14:56:41 -08002517 do_sync_mmap_readahead(vmf->vma, ra, file, offset);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002518 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002519 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002520 ret = VM_FAULT_MAJOR;
2521retry_find:
Michel Lespinasseb522c942010-10-26 14:21:56 -07002522 page = find_get_page(mapping, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!page)
2524 goto no_cached_page;
2525 }
2526
Dave Jiang11bac802017-02-24 14:56:41 -08002527 if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002528 put_page(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002529 return ret | VM_FAULT_RETRY;
Michel Lespinassed88c0922010-11-02 13:05:18 -07002530 }
Michel Lespinasseb522c942010-10-26 14:21:56 -07002531
2532 /* Did it get truncated? */
2533 if (unlikely(page->mapping != mapping)) {
2534 unlock_page(page);
2535 put_page(page);
2536 goto retry_find;
2537 }
Sasha Levin309381fea2014-01-23 15:52:54 -08002538 VM_BUG_ON_PAGE(page->index != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
Nick Piggind00806b2007-07-19 01:46:57 -07002541 * We have a locked page in the page cache, now we need to check
2542 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 */
Nick Piggind00806b2007-07-19 01:46:57 -07002544 if (unlikely(!PageUptodate(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 goto page_not_uptodate;
2546
Linus Torvaldsef00e082009-06-16 15:31:25 -07002547 /*
2548 * Found the page and have a reference on it.
2549 * We must recheck i_size under page lock.
2550 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002551 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2552 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07002553 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002554 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07002555 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07002556 }
2557
Nick Piggind0217ac2007-07-19 01:47:03 -07002558 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07002559 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561no_cached_page:
2562 /*
2563 * We're only likely to ever get here if MADV_RANDOM is in
2564 * effect.
2565 */
Michal Hockoc20cd452016-01-14 15:20:12 -08002566 error = page_cache_read(file, offset, vmf->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 /*
2569 * The page we want has now been added to the page cache.
2570 * In the unlikely event that someone removed it in the
2571 * meantime, we'll just come back here and read it again.
2572 */
2573 if (error >= 0)
2574 goto retry_find;
2575
2576 /*
2577 * An error return from page_cache_read can result if the
2578 * system is low on memory, or a problem occurs while trying
2579 * to schedule I/O.
2580 */
2581 if (error == -ENOMEM)
Nick Piggind0217ac2007-07-19 01:47:03 -07002582 return VM_FAULT_OOM;
2583 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /*
2587 * Umm, take care of errors if the page isn't up-to-date.
2588 * Try to re-read it _once_. We do this synchronously,
2589 * because there really aren't any performance issues here
2590 * and we need to check for errors.
2591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 ClearPageError(page);
Zach Brown994fc28c2005-12-15 14:28:17 -08002593 error = mapping->a_ops->readpage(file, page);
Miklos Szeredi3ef0f722008-05-14 16:05:37 -07002594 if (!error) {
2595 wait_on_page_locked(page);
2596 if (!PageUptodate(page))
2597 error = -EIO;
2598 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002599 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07002600
2601 if (!error || error == AOP_TRUNCATED_PAGE)
2602 goto retry_find;
2603
2604 /* Things didn't work out. Return zero to tell the mm layer so. */
2605 shrink_readahead_size_eio(file, ra);
Nick Piggind0217ac2007-07-19 01:47:03 -07002606 return VM_FAULT_SIGBUS;
Nick Piggin54cb8822007-07-19 01:46:59 -07002607}
2608EXPORT_SYMBOL(filemap_fault);
2609
Jan Kara82b0f8c2016-12-14 15:06:58 -08002610void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002611 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002612{
2613 struct radix_tree_iter iter;
2614 void **slot;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002615 struct file *file = vmf->vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002616 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002617 pgoff_t last_pgoff = start_pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002618 unsigned long max_idx;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002619 struct page *head, *page;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002620
2621 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002622 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start_pgoff) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002623 if (iter.index > end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002624 break;
2625repeat:
2626 page = radix_tree_deref_slot(slot);
2627 if (unlikely(!page))
2628 goto next;
2629 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002630 if (radix_tree_deref_retry(page)) {
2631 slot = radix_tree_iter_retry(&iter);
2632 continue;
2633 }
2634 goto next;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002635 }
2636
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002637 head = compound_head(page);
2638 if (!page_cache_get_speculative(head))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002639 goto repeat;
2640
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002641 /* The page was split under us? */
2642 if (compound_head(page) != head) {
2643 put_page(head);
2644 goto repeat;
2645 }
2646
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002647 /* Has the page moved? */
2648 if (unlikely(page != *slot)) {
Kirill A. Shutemov83929372016-07-26 15:26:04 -07002649 put_page(head);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002650 goto repeat;
2651 }
2652
2653 if (!PageUptodate(page) ||
2654 PageReadahead(page) ||
2655 PageHWPoison(page))
2656 goto skip;
2657 if (!trylock_page(page))
2658 goto skip;
2659
2660 if (page->mapping != mapping || !PageUptodate(page))
2661 goto unlock;
2662
Matthew Wilcox9ab25942017-05-03 14:53:29 -07002663 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2664 if (page->index >= max_idx)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002665 goto unlock;
2666
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002667 if (file->f_ra.mmap_miss > 0)
2668 file->f_ra.mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002669
Jan Kara82b0f8c2016-12-14 15:06:58 -08002670 vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2671 if (vmf->pte)
2672 vmf->pte += iter.index - last_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002673 last_pgoff = iter.index;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002674 if (alloc_set_pte(vmf, NULL, page))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002675 goto unlock;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002676 unlock_page(page);
2677 goto next;
2678unlock:
2679 unlock_page(page);
2680skip:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002681 put_page(page);
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002682next:
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002683 /* Huge page is mapped? No need to proceed. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002684 if (pmd_trans_huge(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07002685 break;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002686 if (iter.index == end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002687 break;
2688 }
2689 rcu_read_unlock();
2690}
2691EXPORT_SYMBOL(filemap_map_pages);
2692
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002693vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02002694{
2695 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002696 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07002697 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02002698
Jan Kara14da9202012-06-12 16:20:37 +02002699 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08002700 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002701 lock_page(page);
2702 if (page->mapping != inode->i_mapping) {
2703 unlock_page(page);
2704 ret = VM_FAULT_NOPAGE;
2705 goto out;
2706 }
Jan Kara14da9202012-06-12 16:20:37 +02002707 /*
2708 * We mark the page dirty already here so that when freeze is in
2709 * progress, we are guaranteed that writeback during freezing will
2710 * see the dirty page and writeprotect it again.
2711 */
2712 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08002713 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002714out:
Jan Kara14da9202012-06-12 16:20:37 +02002715 sb_end_pagefault(inode->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02002716 return ret;
2717}
Jan Kara4fcf1c62012-06-12 16:20:29 +02002718
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002719const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002720 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002721 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02002722 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723};
2724
2725/* This is used for a general mmap of a disk file */
2726
2727int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2728{
2729 struct address_space *mapping = file->f_mapping;
2730
2731 if (!mapping->a_ops->readpage)
2732 return -ENOEXEC;
2733 file_accessed(file);
2734 vma->vm_ops = &generic_file_vm_ops;
2735 return 0;
2736}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738/*
2739 * This is for filesystems which do not implement ->writepage.
2740 */
2741int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2742{
2743 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2744 return -EINVAL;
2745 return generic_file_mmap(file, vma);
2746}
2747#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07002748vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07002749{
Souptick Joarder4b96a372018-10-26 15:04:03 -07002750 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07002751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2753{
2754 return -ENOSYS;
2755}
2756int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2757{
2758 return -ENOSYS;
2759}
2760#endif /* CONFIG_MMU */
2761
Arnd Bergmann453972282018-04-13 15:35:27 -07002762EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763EXPORT_SYMBOL(generic_file_mmap);
2764EXPORT_SYMBOL(generic_file_readonly_mmap);
2765
Sasha Levin67f9fd92014-04-03 14:48:18 -07002766static struct page *wait_on_page_read(struct page *page)
2767{
2768 if (!IS_ERR(page)) {
2769 wait_on_page_locked(page);
2770 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002771 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07002772 page = ERR_PTR(-EIO);
2773 }
2774 }
2775 return page;
2776}
2777
Mel Gorman32b63522016-03-15 14:55:36 -07002778static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07002779 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002780 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002781 void *data,
2782 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
Nick Piggineb2be182007-10-16 01:24:57 -07002784 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 int err;
2786repeat:
2787 page = find_get_page(mapping, index);
2788 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08002789 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002790 if (!page)
2791 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002792 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07002793 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002794 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07002795 if (err == -EEXIST)
2796 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 /* Presumably ENOMEM for radix tree node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return ERR_PTR(err);
2799 }
Mel Gorman32b63522016-03-15 14:55:36 -07002800
2801filler:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 err = filler(data, page);
2803 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002804 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002805 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
Mel Gorman32b63522016-03-15 14:55:36 -07002807
2808 page = wait_on_page_read(page);
2809 if (IS_ERR(page))
2810 return page;
2811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (PageUptodate(page))
2814 goto out;
2815
Mel Gormanebded022016-03-15 14:55:39 -07002816 /*
2817 * Page is not up to date and may be locked due one of the following
2818 * case a: Page is being filled and the page lock is held
2819 * case b: Read/write error clearing the page uptodate status
2820 * case c: Truncation in progress (page locked)
2821 * case d: Reclaim in progress
2822 *
2823 * Case a, the page will be up to date when the page is unlocked.
2824 * There is no need to serialise on the page lock here as the page
2825 * is pinned so the lock gives no additional protection. Even if the
2826 * the page is truncated, the data is still valid if PageUptodate as
2827 * it's a race vs truncate race.
2828 * Case b, the page will not be up to date
2829 * Case c, the page may be truncated but in itself, the data may still
2830 * be valid after IO completes as it's a read vs truncate race. The
2831 * operation must restart if the page is not uptodate on unlock but
2832 * otherwise serialising on page lock to stabilise the mapping gives
2833 * no additional guarantees to the caller as the page lock is
2834 * released before return.
2835 * Case d, similar to truncation. If reclaim holds the page lock, it
2836 * will be a race with remove_mapping that determines if the mapping
2837 * is valid on unlock but otherwise the data is valid and there is
2838 * no need to serialise with page lock.
2839 *
2840 * As the page lock gives no additional guarantee, we optimistically
2841 * wait on the page to be unlocked and check if it's up to date and
2842 * use the page if it is. Otherwise, the page lock is required to
2843 * distinguish between the different cases. The motivation is that we
2844 * avoid spurious serialisations and wakeups when multiple processes
2845 * wait on the same page for IO to complete.
2846 */
2847 wait_on_page_locked(page);
2848 if (PageUptodate(page))
2849 goto out;
2850
2851 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07002853
2854 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (!page->mapping) {
2856 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002857 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07002858 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
Mel Gormanebded022016-03-15 14:55:39 -07002860
2861 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (PageUptodate(page)) {
2863 unlock_page(page);
2864 goto out;
2865 }
Mel Gorman32b63522016-03-15 14:55:36 -07002866 goto filler;
2867
David Howellsc855ff32007-05-09 13:42:20 +01002868out:
Nick Piggin6fe69002007-05-06 14:49:04 -07002869 mark_page_accessed(page);
2870 return page;
2871}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002872
2873/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07002874 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002875 * @mapping: the page's address_space
2876 * @index: the page index
2877 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002878 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002879 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002880 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07002881 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002882 *
2883 * If the page does not get brought uptodate, return -EIO.
2884 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07002885struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002886 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07002887 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002888 void *data)
2889{
2890 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2891}
Sasha Levin67f9fd92014-04-03 14:48:18 -07002892EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002893
2894/**
2895 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2896 * @mapping: the page's address_space
2897 * @index: the page index
2898 * @gfp: the page allocator flags to use if allocating
2899 *
2900 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06002901 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002902 *
2903 * If the page does not get brought uptodate, return -EIO.
2904 */
2905struct page *read_cache_page_gfp(struct address_space *mapping,
2906 pgoff_t index,
2907 gfp_t gfp)
2908{
2909 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2910
Sasha Levin67f9fd92014-04-03 14:48:18 -07002911 return do_read_cache_page(mapping, index, filler, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08002912}
2913EXPORT_SYMBOL(read_cache_page_gfp);
2914
Nick Piggin2f718ff2007-10-16 01:24:59 -07002915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 * Performs necessary checks before doing a write
2917 *
Randy Dunlap485bb992006-06-23 02:03:49 -07002918 * Can adjust writing position or amount of bytes to write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 * Returns appropriate error code that caller should return or
2920 * zero in case that write should be allowed.
2921 */
Al Viro3309dd02015-04-09 12:55:47 -04002922inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Al Viro3309dd02015-04-09 12:55:47 -04002924 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 struct inode *inode = file->f_mapping->host;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002926 unsigned long limit = rlimit(RLIMIT_FSIZE);
Al Viro3309dd02015-04-09 12:55:47 -04002927 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Al Viro3309dd02015-04-09 12:55:47 -04002929 if (!iov_iter_count(from))
2930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Al Viro0fa6b002015-04-04 04:05:48 -04002932 /* FIXME: this is for backwards compatibility with 2.4 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002933 if (iocb->ki_flags & IOCB_APPEND)
Al Viro3309dd02015-04-09 12:55:47 -04002934 iocb->ki_pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Al Viro3309dd02015-04-09 12:55:47 -04002936 pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002938 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2939 return -EINVAL;
2940
Al Viro0fa6b002015-04-04 04:05:48 -04002941 if (limit != RLIM_INFINITY) {
Al Viro3309dd02015-04-09 12:55:47 -04002942 if (iocb->ki_pos >= limit) {
Al Viro0fa6b002015-04-04 04:05:48 -04002943 send_sig(SIGXFSZ, current, 0);
2944 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
Al Viro3309dd02015-04-09 12:55:47 -04002946 iov_iter_truncate(from, limit - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948
2949 /*
2950 * LFS rule
2951 */
Al Viro3309dd02015-04-09 12:55:47 -04002952 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 !(file->f_flags & O_LARGEFILE))) {
Al Viro3309dd02015-04-09 12:55:47 -04002954 if (pos >= MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return -EFBIG;
Al Viro3309dd02015-04-09 12:55:47 -04002956 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958
2959 /*
2960 * Are we about to exceed the fs block limit ?
2961 *
2962 * If we have written data it becomes a short write. If we have
2963 * exceeded without writing data we send a signal and return EFBIG.
2964 * Linus frestrict idea will clean these up nicely..
2965 */
Al Viro3309dd02015-04-09 12:55:47 -04002966 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2967 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Al Viro3309dd02015-04-09 12:55:47 -04002969 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2970 return iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972EXPORT_SYMBOL(generic_write_checks);
2973
Nick Pigginafddba42007-10-16 01:25:01 -07002974int pagecache_write_begin(struct file *file, struct address_space *mapping,
2975 loff_t pos, unsigned len, unsigned flags,
2976 struct page **pagep, void **fsdata)
2977{
2978 const struct address_space_operations *aops = mapping->a_ops;
2979
Nick Piggin4e02ed42008-10-29 14:00:55 -07002980 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07002981 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002982}
2983EXPORT_SYMBOL(pagecache_write_begin);
2984
2985int pagecache_write_end(struct file *file, struct address_space *mapping,
2986 loff_t pos, unsigned len, unsigned copied,
2987 struct page *page, void *fsdata)
2988{
2989 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07002990
Nick Piggin4e02ed42008-10-29 14:00:55 -07002991 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07002992}
2993EXPORT_SYMBOL(pagecache_write_end);
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07002996generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
2998 struct file *file = iocb->ki_filp;
2999 struct address_space *mapping = file->f_mapping;
3000 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003001 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003003 size_t write_len;
3004 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Al Viro0c949332014-03-22 06:51:37 -04003006 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003007 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003008
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003009 if (iocb->ki_flags & IOCB_NOWAIT) {
3010 /* If there are pages to writeback, return */
3011 if (filemap_range_has_page(inode->i_mapping, pos,
3012 pos + iov_iter_count(from)))
3013 return -EAGAIN;
3014 } else {
3015 written = filemap_write_and_wait_range(mapping, pos,
3016 pos + write_len - 1);
3017 if (written)
3018 goto out;
3019 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003020
3021 /*
3022 * After a write we want buffered reads to be sure to go to disk to get
3023 * the new data. We invalidate clean cached page from the region we're
3024 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003025 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003026 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003027 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003028 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003029 /*
3030 * If a page can not be invalidated, return 0 to fall back
3031 * to buffered write.
3032 */
3033 if (written) {
3034 if (written == -EBUSY)
3035 return 0;
3036 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003037 }
3038
Al Viro639a93a52017-04-13 14:10:15 -04003039 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003040
3041 /*
3042 * Finally, try again to invalidate clean pages which might have been
3043 * cached by non-direct readahead, or faulted in by get_user_pages()
3044 * if the source of the write was an mmap'ed region of the file
3045 * we're writing. Either one is a pretty crazy thing to do,
3046 * so we don't support it 100%. If this invalidation
3047 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003048 *
3049 * Most of the time we do not need this since dio_complete() will do
3050 * the invalidation for us. However there are some file systems that
3051 * do not end up with dio_complete() being called, so let's not break
3052 * them by removing it completely
Christoph Hellwiga969e902008-07-23 21:27:04 -07003053 */
Lukas Czerner332391a2017-09-21 08:16:29 -06003054 if (mapping->nrpages)
3055 invalidate_inode_pages2_range(mapping,
3056 pos >> PAGE_SHIFT, end);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003059 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003060 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003061 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3062 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 mark_inode_dirty(inode);
3064 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003065 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
Al Viro639a93a52017-04-13 14:10:15 -04003067 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return written;
3070}
3071EXPORT_SYMBOL(generic_file_direct_write);
3072
Nick Piggineb2be182007-10-16 01:24:57 -07003073/*
3074 * Find or create a page at the given pagecache position. Return the locked
3075 * page. This function is specifically for buffered writes.
3076 */
Nick Piggin54566b22009-01-04 12:00:53 -08003077struct page *grab_cache_page_write_begin(struct address_space *mapping,
3078 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003079{
Nick Piggineb2be182007-10-16 01:24:57 -07003080 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003081 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003082
Nick Piggin54566b22009-01-04 12:00:53 -08003083 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003084 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003085
Mel Gorman2457aec2014-06-04 16:10:31 -07003086 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003087 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003088 if (page)
3089 wait_for_stable_page(page);
3090
Nick Piggineb2be182007-10-16 01:24:57 -07003091 return page;
3092}
Nick Piggin54566b22009-01-04 12:00:53 -08003093EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003094
Al Viro3b93f912014-02-11 21:34:08 -05003095ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003096 struct iov_iter *i, loff_t pos)
3097{
3098 struct address_space *mapping = file->f_mapping;
3099 const struct address_space_operations *a_ops = mapping->a_ops;
3100 long status = 0;
3101 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003102 unsigned int flags = 0;
3103
Nick Pigginafddba42007-10-16 01:25:01 -07003104 do {
3105 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003106 unsigned long offset; /* Offset into pagecache page */
3107 unsigned long bytes; /* Bytes to write to page */
3108 size_t copied; /* Bytes copied from user */
3109 void *fsdata;
3110
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003111 offset = (pos & (PAGE_SIZE - 1));
3112 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003113 iov_iter_count(i));
3114
3115again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003116 /*
3117 * Bring in the user page that we will copy from _first_.
3118 * Otherwise there's a nasty deadlock on copying from the
3119 * same page as we're writing to, without it being marked
3120 * up-to-date.
3121 *
3122 * Not only is this an optimisation, but it is also required
3123 * to check that the address is actually valid, when atomic
3124 * usercopies are used, below.
3125 */
3126 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3127 status = -EFAULT;
3128 break;
3129 }
3130
Jan Kara296291c2015-10-22 13:32:21 -07003131 if (fatal_signal_pending(current)) {
3132 status = -EINTR;
3133 break;
3134 }
3135
Nick Piggin674b8922007-10-16 01:25:03 -07003136 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003137 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003138 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003139 break;
3140
anfei zhou931e80e2010-02-02 13:44:02 -08003141 if (mapping_writably_mapped(mapping))
3142 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003143
Nick Pigginafddba42007-10-16 01:25:01 -07003144 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
Nick Pigginafddba42007-10-16 01:25:01 -07003145 flush_dcache_page(page);
3146
3147 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3148 page, fsdata);
3149 if (unlikely(status < 0))
3150 break;
3151 copied = status;
3152
3153 cond_resched();
3154
Nick Piggin124d3b72008-02-02 15:01:17 +01003155 iov_iter_advance(i, copied);
Nick Pigginafddba42007-10-16 01:25:01 -07003156 if (unlikely(copied == 0)) {
3157 /*
3158 * If we were unable to copy any data at all, we must
3159 * fall back to a single segment length write.
3160 *
3161 * If we didn't fallback here, we could livelock
3162 * because not all segments in the iov can be copied at
3163 * once without a pagefault.
3164 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003165 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003166 iov_iter_single_seg_count(i));
3167 goto again;
3168 }
Nick Pigginafddba42007-10-16 01:25:01 -07003169 pos += copied;
3170 written += copied;
3171
3172 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003173 } while (iov_iter_count(i));
3174
3175 return written ? written : status;
3176}
Al Viro3b93f912014-02-11 21:34:08 -05003177EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Jan Karae4dd9de2009-08-17 18:10:06 +02003179/**
Al Viro81742022014-04-03 03:17:43 -04003180 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003181 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003182 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003183 *
3184 * This function does all the work needed for actually writing data to a
3185 * file. It does all basic checks, removes SUID from the file, updates
3186 * modification times and calls proper subroutines depending on whether we
3187 * do direct IO or a standard buffered write.
3188 *
3189 * It expects i_mutex to be grabbed unless we work on a block device or similar
3190 * object which does not need locking at all.
3191 *
3192 * This function does *not* take care of syncing data in case of O_SYNC write.
3193 * A caller has to handle it. This is mainly due to the fact that we want to
3194 * avoid syncing under i_mutex.
3195 */
Al Viro81742022014-04-03 03:17:43 -04003196ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct file *file = iocb->ki_filp;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003199 struct address_space * mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003201 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003203 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003206 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003207 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (err)
3209 goto out;
3210
Josef Bacikc3b2da32012-03-26 09:59:21 -04003211 err = file_update_time(file);
3212 if (err)
3213 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Al Viro2ba48ce2015-04-09 13:52:01 -04003215 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003216 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003217
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003218 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003219 /*
3220 * If the write stopped short of completing, fall back to
3221 * buffered writes. Some filesystems do this for writes to
3222 * holes, for example. For DAX files, a buffered write will
3223 * not succeed (even if it did, DAX does not handle dirty
3224 * page-cache pages correctly).
3225 */
Al Viro0b8def92015-04-07 10:22:53 -04003226 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003228
Al Viro0b8def92015-04-07 10:22:53 -04003229 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003230 /*
Al Viro3b93f912014-02-11 21:34:08 -05003231 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003232 * then we want to return the number of bytes which were
3233 * direct-written, or the error code if that was zero. Note
3234 * that this differs from normal direct-io semantics, which
3235 * will return -EFOO even if some bytes were written.
3236 */
Al Viro60bb4522014-08-08 12:39:16 -04003237 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003238 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003239 goto out;
3240 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003241 /*
3242 * We need to ensure that the page cache pages are written to
3243 * disk and invalidated to preserve the expected O_DIRECT
3244 * semantics.
3245 */
Al Viro3b93f912014-02-11 21:34:08 -05003246 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003247 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003248 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003249 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003250 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003251 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003252 pos >> PAGE_SHIFT,
3253 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003254 } else {
3255 /*
3256 * We don't know how much we wrote, so just return
3257 * the number of bytes which were direct-written
3258 */
3259 }
3260 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003261 written = generic_perform_write(file, from, iocb->ki_pos);
3262 if (likely(written > 0))
3263 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265out:
3266 current->backing_dev_info = NULL;
3267 return written ? written : err;
3268}
Al Viro81742022014-04-03 03:17:43 -04003269EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Jan Karae4dd9de2009-08-17 18:10:06 +02003271/**
Al Viro81742022014-04-03 03:17:43 -04003272 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003273 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003274 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003275 *
Al Viro81742022014-04-03 03:17:43 -04003276 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003277 * filesystems. It takes care of syncing the file in case of O_SYNC file
3278 * and acquires i_mutex as needed.
3279 */
Al Viro81742022014-04-03 03:17:43 -04003280ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
3282 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003283 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Al Viro59551022016-01-22 15:40:57 -05003286 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003287 ret = generic_write_checks(iocb, from);
3288 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003289 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003290 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Christoph Hellwige2592212016-04-07 08:52:01 -07003292 if (ret > 0)
3293 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return ret;
3295}
Al Viro81742022014-04-03 03:17:43 -04003296EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
David Howellscf9a2ae2006-08-29 19:05:54 +01003298/**
3299 * try_to_release_page() - release old fs-specific metadata on a page
3300 *
3301 * @page: the page which the kernel is trying to free
3302 * @gfp_mask: memory allocation flags (and I/O mode)
3303 *
3304 * The address_space is to try to release any data against the page
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03003305 * (presumably at page->private). If the release was successful, return '1'.
David Howellscf9a2ae2006-08-29 19:05:54 +01003306 * Otherwise return zero.
3307 *
David Howells266cf652009-04-03 16:42:36 +01003308 * This may also be called if PG_fscache is set on a page, indicating that the
3309 * page is known to the local caching routines.
3310 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003311 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003312 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003313 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003314 */
3315int try_to_release_page(struct page *page, gfp_t gfp_mask)
3316{
3317 struct address_space * const mapping = page->mapping;
3318
3319 BUG_ON(!PageLocked(page));
3320 if (PageWriteback(page))
3321 return 0;
3322
3323 if (mapping && mapping->a_ops->releasepage)
3324 return mapping->a_ops->releasepage(page, gfp_mask);
3325 return try_to_free_buffers(page);
3326}
3327
3328EXPORT_SYMBOL(try_to_release_page);