]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/filemap.c
Merge tag 'mmc-v4.7-rc1' of git://git.linaro.org/people/ulf.hansson/mmc
[mirror_ubuntu-artful-kernel.git] / mm / filemap.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/filemap.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * This file handles the generic file mmap semantics used by
9 * most "normal" filesystems (but you don't /have/ to use this:
10 * the NFS filesystem used to do this differently, for example)
11 */
b95f1b31 12#include <linux/export.h>
1da177e4 13#include <linux/compiler.h>
f9fe48be 14#include <linux/dax.h>
1da177e4 15#include <linux/fs.h>
c22ce143 16#include <linux/uaccess.h>
c59ede7b 17#include <linux/capability.h>
1da177e4 18#include <linux/kernel_stat.h>
5a0e3ad6 19#include <linux/gfp.h>
1da177e4
LT
20#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/mman.h>
23#include <linux/pagemap.h>
24#include <linux/file.h>
25#include <linux/uio.h>
26#include <linux/hash.h>
27#include <linux/writeback.h>
53253383 28#include <linux/backing-dev.h>
1da177e4
LT
29#include <linux/pagevec.h>
30#include <linux/blkdev.h>
31#include <linux/security.h>
44110fe3 32#include <linux/cpuset.h>
2f718ffc 33#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
00501b53 34#include <linux/hugetlb.h>
8a9f3ccd 35#include <linux/memcontrol.h>
c515e1fd 36#include <linux/cleancache.h>
f1820361 37#include <linux/rmap.h>
0f8053a5
NP
38#include "internal.h"
39
fe0bfaaf
RJ
40#define CREATE_TRACE_POINTS
41#include <trace/events/filemap.h>
42
1da177e4 43/*
1da177e4
LT
44 * FIXME: remove all knowledge of the buffer layer from the core VM
45 */
148f948b 46#include <linux/buffer_head.h> /* for try_to_free_buffers */
1da177e4 47
1da177e4
LT
48#include <asm/mman.h>
49
50/*
51 * Shared mappings implemented 30.11.1994. It's not fully working yet,
52 * though.
53 *
54 * Shared mappings now work. 15.8.1995 Bruno.
55 *
56 * finished 'unifying' the page and buffer cache and SMP-threaded the
57 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
58 *
59 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
60 */
61
62/*
63 * Lock ordering:
64 *
c8c06efa 65 * ->i_mmap_rwsem (truncate_pagecache)
1da177e4 66 * ->private_lock (__free_pte->__set_page_dirty_buffers)
5d337b91
HD
67 * ->swap_lock (exclusive_swap_page, others)
68 * ->mapping->tree_lock
1da177e4 69 *
1b1dcc1b 70 * ->i_mutex
c8c06efa 71 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
1da177e4
LT
72 *
73 * ->mmap_sem
c8c06efa 74 * ->i_mmap_rwsem
b8072f09 75 * ->page_table_lock or pte_lock (various, mainly in memory.c)
1da177e4
LT
76 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
77 *
78 * ->mmap_sem
79 * ->lock_page (access_process_vm)
80 *
ccad2365 81 * ->i_mutex (generic_perform_write)
82591e6e 82 * ->mmap_sem (fault_in_pages_readable->do_page_fault)
1da177e4 83 *
f758eeab 84 * bdi->wb.list_lock
a66979ab 85 * sb_lock (fs/fs-writeback.c)
1da177e4
LT
86 * ->mapping->tree_lock (__sync_single_inode)
87 *
c8c06efa 88 * ->i_mmap_rwsem
1da177e4
LT
89 * ->anon_vma.lock (vma_adjust)
90 *
91 * ->anon_vma.lock
b8072f09 92 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
1da177e4 93 *
b8072f09 94 * ->page_table_lock or pte_lock
5d337b91 95 * ->swap_lock (try_to_unmap_one)
1da177e4
LT
96 * ->private_lock (try_to_unmap_one)
97 * ->tree_lock (try_to_unmap_one)
98 * ->zone.lru_lock (follow_page->mark_page_accessed)
053837fc 99 * ->zone.lru_lock (check_pte_range->isolate_lru_page)
1da177e4
LT
100 * ->private_lock (page_remove_rmap->set_page_dirty)
101 * ->tree_lock (page_remove_rmap->set_page_dirty)
f758eeab 102 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
250df6ed 103 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
81f8c3a4 104 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
f758eeab 105 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
250df6ed 106 * ->inode->i_lock (zap_pte_range->set_page_dirty)
1da177e4
LT
107 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
108 *
c8c06efa 109 * ->i_mmap_rwsem
9a3c531d 110 * ->tasklist_lock (memory_failure, collect_procs_ao)
1da177e4
LT
111 */
112
91b0abe3
JW
113static void page_cache_tree_delete(struct address_space *mapping,
114 struct page *page, void *shadow)
115{
449dd698 116 struct radix_tree_node *node;
91b0abe3 117
449dd698
JW
118 VM_BUG_ON(!PageLocked(page));
119
d604c324
MW
120 node = radix_tree_replace_clear_tags(&mapping->page_tree, page->index,
121 shadow);
449dd698
JW
122
123 if (shadow) {
f9fe48be 124 mapping->nrexceptional++;
91b0abe3 125 /*
f9fe48be 126 * Make sure the nrexceptional update is committed before
91b0abe3
JW
127 * the nrpages update so that final truncate racing
128 * with reclaim does not see both counters 0 at the
129 * same time and miss a shadow entry.
130 */
131 smp_wmb();
449dd698 132 }
91b0abe3 133 mapping->nrpages--;
449dd698 134
d604c324 135 if (!node)
449dd698 136 return;
449dd698 137
449dd698
JW
138 workingset_node_pages_dec(node);
139 if (shadow)
140 workingset_node_shadows_inc(node);
141 else
142 if (__radix_tree_delete_node(&mapping->page_tree, node))
143 return;
144
145 /*
146 * Track node that only contains shadow entries.
147 *
148 * Avoid acquiring the list_lru lock if already tracked. The
149 * list_empty() test is safe as node->private_list is
150 * protected by mapping->tree_lock.
151 */
152 if (!workingset_node_pages(node) &&
153 list_empty(&node->private_list)) {
154 node->private_data = mapping;
155 list_lru_add(&workingset_shadow_nodes, &node->private_list);
156 }
91b0abe3
JW
157}
158
1da177e4 159/*
e64a782f 160 * Delete a page from the page cache and free it. Caller has to make
1da177e4 161 * sure the page is locked and that nobody else uses it - or that usage
fdf1cdb9 162 * is safe. The caller must hold the mapping's tree_lock.
1da177e4 163 */
62cccb8c 164void __delete_from_page_cache(struct page *page, void *shadow)
1da177e4
LT
165{
166 struct address_space *mapping = page->mapping;
167
fe0bfaaf 168 trace_mm_filemap_delete_from_page_cache(page);
c515e1fd
DM
169 /*
170 * if we're uptodate, flush out into the cleancache, otherwise
171 * invalidate any existing cleancache entries. We can't leave
172 * stale data around in the cleancache once our page is gone
173 */
174 if (PageUptodate(page) && PageMappedToDisk(page))
175 cleancache_put_page(page);
176 else
3167760f 177 cleancache_invalidate_page(mapping, page);
c515e1fd 178
06b241f3
HD
179 VM_BUG_ON_PAGE(page_mapped(page), page);
180 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
181 int mapcount;
182
183 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
184 current->comm, page_to_pfn(page));
185 dump_page(page, "still mapped when deleted");
186 dump_stack();
187 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
188
189 mapcount = page_mapcount(page);
190 if (mapping_exiting(mapping) &&
191 page_count(page) >= mapcount + 2) {
192 /*
193 * All vmas have already been torn down, so it's
194 * a good bet that actually the page is unmapped,
195 * and we'd prefer not to leak it: if we're wrong,
196 * some other bad page check should catch it later.
197 */
198 page_mapcount_reset(page);
6d061f9f 199 page_ref_sub(page, mapcount);
06b241f3
HD
200 }
201 }
202
91b0abe3
JW
203 page_cache_tree_delete(mapping, page, shadow);
204
1da177e4 205 page->mapping = NULL;
b85e0eff 206 /* Leave page->index set: truncation lookup relies upon it */
91b0abe3 207
4165b9b4
MH
208 /* hugetlb pages do not participate in page cache accounting. */
209 if (!PageHuge(page))
210 __dec_zone_page_state(page, NR_FILE_PAGES);
4b02108a
KM
211 if (PageSwapBacked(page))
212 __dec_zone_page_state(page, NR_SHMEM);
3a692790
LT
213
214 /*
b9ea2515
KK
215 * At this point page must be either written or cleaned by truncate.
216 * Dirty page here signals a bug and loss of unwritten data.
3a692790 217 *
b9ea2515
KK
218 * This fixes dirty accounting after removing the page entirely but
219 * leaves PageDirty set: it has no effect for truncated page and
220 * anyway will be cleared before returning page into buddy allocator.
3a692790 221 */
b9ea2515 222 if (WARN_ON_ONCE(PageDirty(page)))
62cccb8c 223 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
1da177e4
LT
224}
225
702cfbf9
MK
226/**
227 * delete_from_page_cache - delete page from page cache
228 * @page: the page which the kernel is trying to remove from page cache
229 *
230 * This must be called only on pages that have been verified to be in the page
231 * cache and locked. It will never put the page into the free list, the caller
232 * has a reference on the page.
233 */
234void delete_from_page_cache(struct page *page)
1da177e4
LT
235{
236 struct address_space *mapping = page->mapping;
c4843a75
GT
237 unsigned long flags;
238
6072d13c 239 void (*freepage)(struct page *);
1da177e4 240
cd7619d6 241 BUG_ON(!PageLocked(page));
1da177e4 242
6072d13c 243 freepage = mapping->a_ops->freepage;
c4843a75 244
c4843a75 245 spin_lock_irqsave(&mapping->tree_lock, flags);
62cccb8c 246 __delete_from_page_cache(page, NULL);
c4843a75 247 spin_unlock_irqrestore(&mapping->tree_lock, flags);
6072d13c
LT
248
249 if (freepage)
250 freepage(page);
09cbfeaf 251 put_page(page);
97cecb5a
MK
252}
253EXPORT_SYMBOL(delete_from_page_cache);
254
865ffef3
DM
255static int filemap_check_errors(struct address_space *mapping)
256{
257 int ret = 0;
258 /* Check for outstanding write errors */
7fcbbaf1
JA
259 if (test_bit(AS_ENOSPC, &mapping->flags) &&
260 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
865ffef3 261 ret = -ENOSPC;
7fcbbaf1
JA
262 if (test_bit(AS_EIO, &mapping->flags) &&
263 test_and_clear_bit(AS_EIO, &mapping->flags))
865ffef3
DM
264 ret = -EIO;
265 return ret;
266}
267
1da177e4 268/**
485bb99b 269 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
67be2dd1
MW
270 * @mapping: address space structure to write
271 * @start: offset in bytes where the range starts
469eb4d0 272 * @end: offset in bytes where the range ends (inclusive)
67be2dd1 273 * @sync_mode: enable synchronous operation
1da177e4 274 *
485bb99b
RD
275 * Start writeback against all of a mapping's dirty pages that lie
276 * within the byte offsets <start, end> inclusive.
277 *
1da177e4 278 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
485bb99b 279 * opposed to a regular memory cleansing writeback. The difference between
1da177e4
LT
280 * these two operations is that if a dirty page/buffer is encountered, it must
281 * be waited upon, and not just skipped over.
282 */
ebcf28e1
AM
283int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
284 loff_t end, int sync_mode)
1da177e4
LT
285{
286 int ret;
287 struct writeback_control wbc = {
288 .sync_mode = sync_mode,
05fe478d 289 .nr_to_write = LONG_MAX,
111ebb6e
OH
290 .range_start = start,
291 .range_end = end,
1da177e4
LT
292 };
293
294 if (!mapping_cap_writeback_dirty(mapping))
295 return 0;
296
b16b1deb 297 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
1da177e4 298 ret = do_writepages(mapping, &wbc);
b16b1deb 299 wbc_detach_inode(&wbc);
1da177e4
LT
300 return ret;
301}
302
303static inline int __filemap_fdatawrite(struct address_space *mapping,
304 int sync_mode)
305{
111ebb6e 306 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
1da177e4
LT
307}
308
309int filemap_fdatawrite(struct address_space *mapping)
310{
311 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
312}
313EXPORT_SYMBOL(filemap_fdatawrite);
314
f4c0a0fd 315int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
ebcf28e1 316 loff_t end)
1da177e4
LT
317{
318 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
319}
f4c0a0fd 320EXPORT_SYMBOL(filemap_fdatawrite_range);
1da177e4 321
485bb99b
RD
322/**
323 * filemap_flush - mostly a non-blocking flush
324 * @mapping: target address_space
325 *
1da177e4
LT
326 * This is a mostly non-blocking flush. Not suitable for data-integrity
327 * purposes - I/O may not be started against all dirty pages.
328 */
329int filemap_flush(struct address_space *mapping)
330{
331 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
332}
333EXPORT_SYMBOL(filemap_flush);
334
aa750fd7
JN
335static int __filemap_fdatawait_range(struct address_space *mapping,
336 loff_t start_byte, loff_t end_byte)
1da177e4 337{
09cbfeaf
KS
338 pgoff_t index = start_byte >> PAGE_SHIFT;
339 pgoff_t end = end_byte >> PAGE_SHIFT;
1da177e4
LT
340 struct pagevec pvec;
341 int nr_pages;
aa750fd7 342 int ret = 0;
1da177e4 343
94004ed7 344 if (end_byte < start_byte)
865ffef3 345 goto out;
1da177e4
LT
346
347 pagevec_init(&pvec, 0);
1da177e4
LT
348 while ((index <= end) &&
349 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
350 PAGECACHE_TAG_WRITEBACK,
351 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
352 unsigned i;
353
354 for (i = 0; i < nr_pages; i++) {
355 struct page *page = pvec.pages[i];
356
357 /* until radix tree lookup accepts end_index */
358 if (page->index > end)
359 continue;
360
361 wait_on_page_writeback(page);
212260aa 362 if (TestClearPageError(page))
1da177e4
LT
363 ret = -EIO;
364 }
365 pagevec_release(&pvec);
366 cond_resched();
367 }
865ffef3 368out:
aa750fd7
JN
369 return ret;
370}
371
372/**
373 * filemap_fdatawait_range - wait for writeback to complete
374 * @mapping: address space structure to wait for
375 * @start_byte: offset in bytes where the range starts
376 * @end_byte: offset in bytes where the range ends (inclusive)
377 *
378 * Walk the list of under-writeback pages of the given address space
379 * in the given range and wait for all of them. Check error status of
380 * the address space and return it.
381 *
382 * Since the error status of the address space is cleared by this function,
383 * callers are responsible for checking the return value and handling and/or
384 * reporting the error.
385 */
386int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
387 loff_t end_byte)
388{
389 int ret, ret2;
390
391 ret = __filemap_fdatawait_range(mapping, start_byte, end_byte);
865ffef3
DM
392 ret2 = filemap_check_errors(mapping);
393 if (!ret)
394 ret = ret2;
1da177e4
LT
395
396 return ret;
397}
d3bccb6f
JK
398EXPORT_SYMBOL(filemap_fdatawait_range);
399
aa750fd7
JN
400/**
401 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
402 * @mapping: address space structure to wait for
403 *
404 * Walk the list of under-writeback pages of the given address space
405 * and wait for all of them. Unlike filemap_fdatawait(), this function
406 * does not clear error status of the address space.
407 *
408 * Use this function if callers don't handle errors themselves. Expected
409 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
410 * fsfreeze(8)
411 */
412void filemap_fdatawait_keep_errors(struct address_space *mapping)
413{
414 loff_t i_size = i_size_read(mapping->host);
415
416 if (i_size == 0)
417 return;
418
419 __filemap_fdatawait_range(mapping, 0, i_size - 1);
420}
421
1da177e4 422/**
485bb99b 423 * filemap_fdatawait - wait for all under-writeback pages to complete
1da177e4 424 * @mapping: address space structure to wait for
485bb99b
RD
425 *
426 * Walk the list of under-writeback pages of the given address space
aa750fd7
JN
427 * and wait for all of them. Check error status of the address space
428 * and return it.
429 *
430 * Since the error status of the address space is cleared by this function,
431 * callers are responsible for checking the return value and handling and/or
432 * reporting the error.
1da177e4
LT
433 */
434int filemap_fdatawait(struct address_space *mapping)
435{
436 loff_t i_size = i_size_read(mapping->host);
437
438 if (i_size == 0)
439 return 0;
440
94004ed7 441 return filemap_fdatawait_range(mapping, 0, i_size - 1);
1da177e4
LT
442}
443EXPORT_SYMBOL(filemap_fdatawait);
444
445int filemap_write_and_wait(struct address_space *mapping)
446{
28fd1298 447 int err = 0;
1da177e4 448
7f6d5b52
RZ
449 if ((!dax_mapping(mapping) && mapping->nrpages) ||
450 (dax_mapping(mapping) && mapping->nrexceptional)) {
28fd1298
OH
451 err = filemap_fdatawrite(mapping);
452 /*
453 * Even if the above returned error, the pages may be
454 * written partially (e.g. -ENOSPC), so we wait for it.
455 * But the -EIO is special case, it may indicate the worst
456 * thing (e.g. bug) happened, so we avoid waiting for it.
457 */
458 if (err != -EIO) {
459 int err2 = filemap_fdatawait(mapping);
460 if (!err)
461 err = err2;
462 }
865ffef3
DM
463 } else {
464 err = filemap_check_errors(mapping);
1da177e4 465 }
28fd1298 466 return err;
1da177e4 467}
28fd1298 468EXPORT_SYMBOL(filemap_write_and_wait);
1da177e4 469
485bb99b
RD
470/**
471 * filemap_write_and_wait_range - write out & wait on a file range
472 * @mapping: the address_space for the pages
473 * @lstart: offset in bytes where the range starts
474 * @lend: offset in bytes where the range ends (inclusive)
475 *
469eb4d0
AM
476 * Write out and wait upon file offsets lstart->lend, inclusive.
477 *
478 * Note that `lend' is inclusive (describes the last byte to be written) so
479 * that this function can be used to write to the very end-of-file (end = -1).
480 */
1da177e4
LT
481int filemap_write_and_wait_range(struct address_space *mapping,
482 loff_t lstart, loff_t lend)
483{
28fd1298 484 int err = 0;
1da177e4 485
7f6d5b52
RZ
486 if ((!dax_mapping(mapping) && mapping->nrpages) ||
487 (dax_mapping(mapping) && mapping->nrexceptional)) {
28fd1298
OH
488 err = __filemap_fdatawrite_range(mapping, lstart, lend,
489 WB_SYNC_ALL);
490 /* See comment of filemap_write_and_wait() */
491 if (err != -EIO) {
94004ed7
CH
492 int err2 = filemap_fdatawait_range(mapping,
493 lstart, lend);
28fd1298
OH
494 if (!err)
495 err = err2;
496 }
865ffef3
DM
497 } else {
498 err = filemap_check_errors(mapping);
1da177e4 499 }
28fd1298 500 return err;
1da177e4 501}
f6995585 502EXPORT_SYMBOL(filemap_write_and_wait_range);
1da177e4 503
ef6a3c63
MS
504/**
505 * replace_page_cache_page - replace a pagecache page with a new one
506 * @old: page to be replaced
507 * @new: page to replace with
508 * @gfp_mask: allocation mode
509 *
510 * This function replaces a page in the pagecache with a new one. On
511 * success it acquires the pagecache reference for the new page and
512 * drops it for the old page. Both the old and new pages must be
513 * locked. This function does not add the new page to the LRU, the
514 * caller must do that.
515 *
516 * The remove + add is atomic. The only way this function can fail is
517 * memory allocation failure.
518 */
519int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
520{
521 int error;
ef6a3c63 522
309381fe
SL
523 VM_BUG_ON_PAGE(!PageLocked(old), old);
524 VM_BUG_ON_PAGE(!PageLocked(new), new);
525 VM_BUG_ON_PAGE(new->mapping, new);
ef6a3c63 526
ef6a3c63
MS
527 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
528 if (!error) {
529 struct address_space *mapping = old->mapping;
530 void (*freepage)(struct page *);
c4843a75 531 unsigned long flags;
ef6a3c63
MS
532
533 pgoff_t offset = old->index;
534 freepage = mapping->a_ops->freepage;
535
09cbfeaf 536 get_page(new);
ef6a3c63
MS
537 new->mapping = mapping;
538 new->index = offset;
539
c4843a75 540 spin_lock_irqsave(&mapping->tree_lock, flags);
62cccb8c 541 __delete_from_page_cache(old, NULL);
ef6a3c63
MS
542 error = radix_tree_insert(&mapping->page_tree, offset, new);
543 BUG_ON(error);
544 mapping->nrpages++;
4165b9b4
MH
545
546 /*
547 * hugetlb pages do not participate in page cache accounting.
548 */
549 if (!PageHuge(new))
550 __inc_zone_page_state(new, NR_FILE_PAGES);
ef6a3c63
MS
551 if (PageSwapBacked(new))
552 __inc_zone_page_state(new, NR_SHMEM);
c4843a75 553 spin_unlock_irqrestore(&mapping->tree_lock, flags);
6a93ca8f 554 mem_cgroup_migrate(old, new);
ef6a3c63
MS
555 radix_tree_preload_end();
556 if (freepage)
557 freepage(old);
09cbfeaf 558 put_page(old);
ef6a3c63
MS
559 }
560
561 return error;
562}
563EXPORT_SYMBOL_GPL(replace_page_cache_page);
564
0cd6144a 565static int page_cache_tree_insert(struct address_space *mapping,
a528910e 566 struct page *page, void **shadowp)
0cd6144a 567{
449dd698 568 struct radix_tree_node *node;
0cd6144a
JW
569 void **slot;
570 int error;
571
e6145236 572 error = __radix_tree_create(&mapping->page_tree, page->index, 0,
449dd698
JW
573 &node, &slot);
574 if (error)
575 return error;
576 if (*slot) {
0cd6144a
JW
577 void *p;
578
579 p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
580 if (!radix_tree_exceptional_entry(p))
581 return -EEXIST;
f9fe48be
RZ
582
583 if (WARN_ON(dax_mapping(mapping)))
584 return -EINVAL;
585
a528910e
JW
586 if (shadowp)
587 *shadowp = p;
f9fe48be 588 mapping->nrexceptional--;
449dd698
JW
589 if (node)
590 workingset_node_shadows_dec(node);
0cd6144a 591 }
449dd698
JW
592 radix_tree_replace_slot(slot, page);
593 mapping->nrpages++;
594 if (node) {
595 workingset_node_pages_inc(node);
596 /*
597 * Don't track node that contains actual pages.
598 *
599 * Avoid acquiring the list_lru lock if already
600 * untracked. The list_empty() test is safe as
601 * node->private_list is protected by
602 * mapping->tree_lock.
603 */
604 if (!list_empty(&node->private_list))
605 list_lru_del(&workingset_shadow_nodes,
606 &node->private_list);
607 }
608 return 0;
0cd6144a
JW
609}
610
a528910e
JW
611static int __add_to_page_cache_locked(struct page *page,
612 struct address_space *mapping,
613 pgoff_t offset, gfp_t gfp_mask,
614 void **shadowp)
1da177e4 615{
00501b53
JW
616 int huge = PageHuge(page);
617 struct mem_cgroup *memcg;
e286781d
NP
618 int error;
619
309381fe
SL
620 VM_BUG_ON_PAGE(!PageLocked(page), page);
621 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
e286781d 622
00501b53
JW
623 if (!huge) {
624 error = mem_cgroup_try_charge(page, current->mm,
f627c2f5 625 gfp_mask, &memcg, false);
00501b53
JW
626 if (error)
627 return error;
628 }
1da177e4 629
5e4c0d97 630 error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
66a0c8ee 631 if (error) {
00501b53 632 if (!huge)
f627c2f5 633 mem_cgroup_cancel_charge(page, memcg, false);
66a0c8ee
KS
634 return error;
635 }
636
09cbfeaf 637 get_page(page);
66a0c8ee
KS
638 page->mapping = mapping;
639 page->index = offset;
640
641 spin_lock_irq(&mapping->tree_lock);
a528910e 642 error = page_cache_tree_insert(mapping, page, shadowp);
66a0c8ee
KS
643 radix_tree_preload_end();
644 if (unlikely(error))
645 goto err_insert;
4165b9b4
MH
646
647 /* hugetlb pages do not participate in page cache accounting. */
648 if (!huge)
649 __inc_zone_page_state(page, NR_FILE_PAGES);
66a0c8ee 650 spin_unlock_irq(&mapping->tree_lock);
00501b53 651 if (!huge)
f627c2f5 652 mem_cgroup_commit_charge(page, memcg, false, false);
66a0c8ee
KS
653 trace_mm_filemap_add_to_page_cache(page);
654 return 0;
655err_insert:
656 page->mapping = NULL;
657 /* Leave page->index set: truncation relies upon it */
658 spin_unlock_irq(&mapping->tree_lock);
00501b53 659 if (!huge)
f627c2f5 660 mem_cgroup_cancel_charge(page, memcg, false);
09cbfeaf 661 put_page(page);
1da177e4
LT
662 return error;
663}
a528910e
JW
664
665/**
666 * add_to_page_cache_locked - add a locked page to the pagecache
667 * @page: page to add
668 * @mapping: the page's address_space
669 * @offset: page index
670 * @gfp_mask: page allocation mode
671 *
672 * This function is used to add a page to the pagecache. It must be locked.
673 * This function does not add the page to the LRU. The caller must do that.
674 */
675int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
676 pgoff_t offset, gfp_t gfp_mask)
677{
678 return __add_to_page_cache_locked(page, mapping, offset,
679 gfp_mask, NULL);
680}
e286781d 681EXPORT_SYMBOL(add_to_page_cache_locked);
1da177e4
LT
682
683int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
6daa0e28 684 pgoff_t offset, gfp_t gfp_mask)
1da177e4 685{
a528910e 686 void *shadow = NULL;
4f98a2fe
RR
687 int ret;
688
48c935ad 689 __SetPageLocked(page);
a528910e
JW
690 ret = __add_to_page_cache_locked(page, mapping, offset,
691 gfp_mask, &shadow);
692 if (unlikely(ret))
48c935ad 693 __ClearPageLocked(page);
a528910e
JW
694 else {
695 /*
696 * The page might have been evicted from cache only
697 * recently, in which case it should be activated like
698 * any other repeatedly accessed page.
f0281a00
RR
699 * The exception is pages getting rewritten; evicting other
700 * data from the working set, only to cache data that will
701 * get overwritten with something else, is a waste of memory.
a528910e 702 */
f0281a00
RR
703 if (!(gfp_mask & __GFP_WRITE) &&
704 shadow && workingset_refault(shadow)) {
a528910e
JW
705 SetPageActive(page);
706 workingset_activation(page);
707 } else
708 ClearPageActive(page);
709 lru_cache_add(page);
710 }
1da177e4
LT
711 return ret;
712}
18bc0bbd 713EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
1da177e4 714
44110fe3 715#ifdef CONFIG_NUMA
2ae88149 716struct page *__page_cache_alloc(gfp_t gfp)
44110fe3 717{
c0ff7453
MX
718 int n;
719 struct page *page;
720
44110fe3 721 if (cpuset_do_page_mem_spread()) {
cc9a6c87
MG
722 unsigned int cpuset_mems_cookie;
723 do {
d26914d1 724 cpuset_mems_cookie = read_mems_allowed_begin();
cc9a6c87 725 n = cpuset_mem_spread_node();
96db800f 726 page = __alloc_pages_node(n, gfp, 0);
d26914d1 727 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
cc9a6c87 728
c0ff7453 729 return page;
44110fe3 730 }
2ae88149 731 return alloc_pages(gfp, 0);
44110fe3 732}
2ae88149 733EXPORT_SYMBOL(__page_cache_alloc);
44110fe3
PJ
734#endif
735
1da177e4
LT
736/*
737 * In order to wait for pages to become available there must be
738 * waitqueues associated with pages. By using a hash table of
739 * waitqueues where the bucket discipline is to maintain all
740 * waiters on the same queue and wake all when any of the pages
741 * become available, and for the woken contexts to check to be
742 * sure the appropriate page became available, this saves space
743 * at a cost of "thundering herd" phenomena during rare hash
744 * collisions.
745 */
a4796e37 746wait_queue_head_t *page_waitqueue(struct page *page)
1da177e4
LT
747{
748 const struct zone *zone = page_zone(page);
749
750 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
751}
a4796e37 752EXPORT_SYMBOL(page_waitqueue);
1da177e4 753
920c7a5d 754void wait_on_page_bit(struct page *page, int bit_nr)
1da177e4
LT
755{
756 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
757
758 if (test_bit(bit_nr, &page->flags))
74316201 759 __wait_on_bit(page_waitqueue(page), &wait, bit_wait_io,
1da177e4
LT
760 TASK_UNINTERRUPTIBLE);
761}
762EXPORT_SYMBOL(wait_on_page_bit);
763
f62e00cc
KM
764int wait_on_page_bit_killable(struct page *page, int bit_nr)
765{
766 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
767
768 if (!test_bit(bit_nr, &page->flags))
769 return 0;
770
771 return __wait_on_bit(page_waitqueue(page), &wait,
74316201 772 bit_wait_io, TASK_KILLABLE);
f62e00cc
KM
773}
774
cbbce822
N
775int wait_on_page_bit_killable_timeout(struct page *page,
776 int bit_nr, unsigned long timeout)
777{
778 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
779
780 wait.key.timeout = jiffies + timeout;
781 if (!test_bit(bit_nr, &page->flags))
782 return 0;
783 return __wait_on_bit(page_waitqueue(page), &wait,
784 bit_wait_io_timeout, TASK_KILLABLE);
785}
786EXPORT_SYMBOL_GPL(wait_on_page_bit_killable_timeout);
787
385e1ca5
DH
788/**
789 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
697f619f
RD
790 * @page: Page defining the wait queue of interest
791 * @waiter: Waiter to add to the queue
385e1ca5
DH
792 *
793 * Add an arbitrary @waiter to the wait queue for the nominated @page.
794 */
795void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
796{
797 wait_queue_head_t *q = page_waitqueue(page);
798 unsigned long flags;
799
800 spin_lock_irqsave(&q->lock, flags);
801 __add_wait_queue(q, waiter);
802 spin_unlock_irqrestore(&q->lock, flags);
803}
804EXPORT_SYMBOL_GPL(add_page_wait_queue);
805
1da177e4 806/**
485bb99b 807 * unlock_page - unlock a locked page
1da177e4
LT
808 * @page: the page
809 *
810 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
811 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
da3dae54 812 * mechanism between PageLocked pages and PageWriteback pages is shared.
1da177e4
LT
813 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
814 *
8413ac9d
NP
815 * The mb is necessary to enforce ordering between the clear_bit and the read
816 * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
1da177e4 817 */
920c7a5d 818void unlock_page(struct page *page)
1da177e4 819{
48c935ad 820 page = compound_head(page);
309381fe 821 VM_BUG_ON_PAGE(!PageLocked(page), page);
8413ac9d 822 clear_bit_unlock(PG_locked, &page->flags);
4e857c58 823 smp_mb__after_atomic();
1da177e4
LT
824 wake_up_page(page, PG_locked);
825}
826EXPORT_SYMBOL(unlock_page);
827
485bb99b
RD
828/**
829 * end_page_writeback - end writeback against a page
830 * @page: the page
1da177e4
LT
831 */
832void end_page_writeback(struct page *page)
833{
888cf2db
MG
834 /*
835 * TestClearPageReclaim could be used here but it is an atomic
836 * operation and overkill in this particular case. Failing to
837 * shuffle a page marked for immediate reclaim is too mild to
838 * justify taking an atomic operation penalty at the end of
839 * ever page writeback.
840 */
841 if (PageReclaim(page)) {
842 ClearPageReclaim(page);
ac6aadb2 843 rotate_reclaimable_page(page);
888cf2db 844 }
ac6aadb2
MS
845
846 if (!test_clear_page_writeback(page))
847 BUG();
848
4e857c58 849 smp_mb__after_atomic();
1da177e4
LT
850 wake_up_page(page, PG_writeback);
851}
852EXPORT_SYMBOL(end_page_writeback);
853
57d99845
MW
854/*
855 * After completing I/O on a page, call this routine to update the page
856 * flags appropriately
857 */
858void page_endio(struct page *page, int rw, int err)
859{
860 if (rw == READ) {
861 if (!err) {
862 SetPageUptodate(page);
863 } else {
864 ClearPageUptodate(page);
865 SetPageError(page);
866 }
867 unlock_page(page);
868 } else { /* rw == WRITE */
869 if (err) {
870 SetPageError(page);
871 if (page->mapping)
872 mapping_set_error(page->mapping, err);
873 }
874 end_page_writeback(page);
875 }
876}
877EXPORT_SYMBOL_GPL(page_endio);
878
485bb99b
RD
879/**
880 * __lock_page - get a lock on the page, assuming we need to sleep to get it
881 * @page: the page to lock
1da177e4 882 */
920c7a5d 883void __lock_page(struct page *page)
1da177e4 884{
48c935ad
KS
885 struct page *page_head = compound_head(page);
886 DEFINE_WAIT_BIT(wait, &page_head->flags, PG_locked);
1da177e4 887
48c935ad 888 __wait_on_bit_lock(page_waitqueue(page_head), &wait, bit_wait_io,
1da177e4
LT
889 TASK_UNINTERRUPTIBLE);
890}
891EXPORT_SYMBOL(__lock_page);
892
b5606c2d 893int __lock_page_killable(struct page *page)
2687a356 894{
48c935ad
KS
895 struct page *page_head = compound_head(page);
896 DEFINE_WAIT_BIT(wait, &page_head->flags, PG_locked);
2687a356 897
48c935ad 898 return __wait_on_bit_lock(page_waitqueue(page_head), &wait,
74316201 899 bit_wait_io, TASK_KILLABLE);
2687a356 900}
18bc0bbd 901EXPORT_SYMBOL_GPL(__lock_page_killable);
2687a356 902
9a95f3cf
PC
903/*
904 * Return values:
905 * 1 - page is locked; mmap_sem is still held.
906 * 0 - page is not locked.
907 * mmap_sem has been released (up_read()), unless flags had both
908 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
909 * which case mmap_sem is still held.
910 *
911 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
912 * with the page locked and the mmap_sem unperturbed.
913 */
d065bd81
ML
914int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
915 unsigned int flags)
916{
37b23e05
KM
917 if (flags & FAULT_FLAG_ALLOW_RETRY) {
918 /*
919 * CAUTION! In this case, mmap_sem is not released
920 * even though return 0.
921 */
922 if (flags & FAULT_FLAG_RETRY_NOWAIT)
923 return 0;
924
925 up_read(&mm->mmap_sem);
926 if (flags & FAULT_FLAG_KILLABLE)
927 wait_on_page_locked_killable(page);
928 else
318b275f 929 wait_on_page_locked(page);
d065bd81 930 return 0;
37b23e05
KM
931 } else {
932 if (flags & FAULT_FLAG_KILLABLE) {
933 int ret;
934
935 ret = __lock_page_killable(page);
936 if (ret) {
937 up_read(&mm->mmap_sem);
938 return 0;
939 }
940 } else
941 __lock_page(page);
942 return 1;
d065bd81
ML
943 }
944}
945
e7b563bb
JW
946/**
947 * page_cache_next_hole - find the next hole (not-present entry)
948 * @mapping: mapping
949 * @index: index
950 * @max_scan: maximum range to search
951 *
952 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
953 * lowest indexed hole.
954 *
955 * Returns: the index of the hole if found, otherwise returns an index
956 * outside of the set specified (in which case 'return - index >=
957 * max_scan' will be true). In rare cases of index wrap-around, 0 will
958 * be returned.
959 *
960 * page_cache_next_hole may be called under rcu_read_lock. However,
961 * like radix_tree_gang_lookup, this will not atomically search a
962 * snapshot of the tree at a single point in time. For example, if a
963 * hole is created at index 5, then subsequently a hole is created at
964 * index 10, page_cache_next_hole covering both indexes may return 10
965 * if called under rcu_read_lock.
966 */
967pgoff_t page_cache_next_hole(struct address_space *mapping,
968 pgoff_t index, unsigned long max_scan)
969{
970 unsigned long i;
971
972 for (i = 0; i < max_scan; i++) {
0cd6144a
JW
973 struct page *page;
974
975 page = radix_tree_lookup(&mapping->page_tree, index);
976 if (!page || radix_tree_exceptional_entry(page))
e7b563bb
JW
977 break;
978 index++;
979 if (index == 0)
980 break;
981 }
982
983 return index;
984}
985EXPORT_SYMBOL(page_cache_next_hole);
986
987/**
988 * page_cache_prev_hole - find the prev hole (not-present entry)
989 * @mapping: mapping
990 * @index: index
991 * @max_scan: maximum range to search
992 *
993 * Search backwards in the range [max(index-max_scan+1, 0), index] for
994 * the first hole.
995 *
996 * Returns: the index of the hole if found, otherwise returns an index
997 * outside of the set specified (in which case 'index - return >=
998 * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
999 * will be returned.
1000 *
1001 * page_cache_prev_hole may be called under rcu_read_lock. However,
1002 * like radix_tree_gang_lookup, this will not atomically search a
1003 * snapshot of the tree at a single point in time. For example, if a
1004 * hole is created at index 10, then subsequently a hole is created at
1005 * index 5, page_cache_prev_hole covering both indexes may return 5 if
1006 * called under rcu_read_lock.
1007 */
1008pgoff_t page_cache_prev_hole(struct address_space *mapping,
1009 pgoff_t index, unsigned long max_scan)
1010{
1011 unsigned long i;
1012
1013 for (i = 0; i < max_scan; i++) {
0cd6144a
JW
1014 struct page *page;
1015
1016 page = radix_tree_lookup(&mapping->page_tree, index);
1017 if (!page || radix_tree_exceptional_entry(page))
e7b563bb
JW
1018 break;
1019 index--;
1020 if (index == ULONG_MAX)
1021 break;
1022 }
1023
1024 return index;
1025}
1026EXPORT_SYMBOL(page_cache_prev_hole);
1027
485bb99b 1028/**
0cd6144a 1029 * find_get_entry - find and get a page cache entry
485bb99b 1030 * @mapping: the address_space to search
0cd6144a
JW
1031 * @offset: the page cache index
1032 *
1033 * Looks up the page cache slot at @mapping & @offset. If there is a
1034 * page cache page, it is returned with an increased refcount.
485bb99b 1035 *
139b6a6f
JW
1036 * If the slot holds a shadow entry of a previously evicted page, or a
1037 * swap entry from shmem/tmpfs, it is returned.
0cd6144a
JW
1038 *
1039 * Otherwise, %NULL is returned.
1da177e4 1040 */
0cd6144a 1041struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
1da177e4 1042{
a60637c8 1043 void **pagep;
1da177e4
LT
1044 struct page *page;
1045
a60637c8
NP
1046 rcu_read_lock();
1047repeat:
1048 page = NULL;
1049 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
1050 if (pagep) {
1051 page = radix_tree_deref_slot(pagep);
27d20fdd
NP
1052 if (unlikely(!page))
1053 goto out;
a2c16d6c 1054 if (radix_tree_exception(page)) {
8079b1c8
HD
1055 if (radix_tree_deref_retry(page))
1056 goto repeat;
1057 /*
139b6a6f
JW
1058 * A shadow entry of a recently evicted page,
1059 * or a swap entry from shmem/tmpfs. Return
1060 * it without attempting to raise page count.
8079b1c8
HD
1061 */
1062 goto out;
a2c16d6c 1063 }
a60637c8
NP
1064 if (!page_cache_get_speculative(page))
1065 goto repeat;
1066
1067 /*
1068 * Has the page moved?
1069 * This is part of the lockless pagecache protocol. See
1070 * include/linux/pagemap.h for details.
1071 */
1072 if (unlikely(page != *pagep)) {
09cbfeaf 1073 put_page(page);
a60637c8
NP
1074 goto repeat;
1075 }
1076 }
27d20fdd 1077out:
a60637c8
NP
1078 rcu_read_unlock();
1079
1da177e4
LT
1080 return page;
1081}
0cd6144a 1082EXPORT_SYMBOL(find_get_entry);
1da177e4 1083
0cd6144a
JW
1084/**
1085 * find_lock_entry - locate, pin and lock a page cache entry
1086 * @mapping: the address_space to search
1087 * @offset: the page cache index
1088 *
1089 * Looks up the page cache slot at @mapping & @offset. If there is a
1090 * page cache page, it is returned locked and with an increased
1091 * refcount.
1092 *
139b6a6f
JW
1093 * If the slot holds a shadow entry of a previously evicted page, or a
1094 * swap entry from shmem/tmpfs, it is returned.
0cd6144a
JW
1095 *
1096 * Otherwise, %NULL is returned.
1097 *
1098 * find_lock_entry() may sleep.
1099 */
1100struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
1da177e4
LT
1101{
1102 struct page *page;
1103
1da177e4 1104repeat:
0cd6144a 1105 page = find_get_entry(mapping, offset);
a2c16d6c 1106 if (page && !radix_tree_exception(page)) {
a60637c8
NP
1107 lock_page(page);
1108 /* Has the page been truncated? */
1109 if (unlikely(page->mapping != mapping)) {
1110 unlock_page(page);
09cbfeaf 1111 put_page(page);
a60637c8 1112 goto repeat;
1da177e4 1113 }
309381fe 1114 VM_BUG_ON_PAGE(page->index != offset, page);
1da177e4 1115 }
1da177e4
LT
1116 return page;
1117}
0cd6144a
JW
1118EXPORT_SYMBOL(find_lock_entry);
1119
1120/**
2457aec6 1121 * pagecache_get_page - find and get a page reference
0cd6144a
JW
1122 * @mapping: the address_space to search
1123 * @offset: the page index
2457aec6 1124 * @fgp_flags: PCG flags
45f87de5 1125 * @gfp_mask: gfp mask to use for the page cache data page allocation
0cd6144a 1126 *
2457aec6 1127 * Looks up the page cache slot at @mapping & @offset.
1da177e4 1128 *
75325189 1129 * PCG flags modify how the page is returned.
0cd6144a 1130 *
2457aec6
MG
1131 * FGP_ACCESSED: the page will be marked accessed
1132 * FGP_LOCK: Page is return locked
1133 * FGP_CREAT: If page is not present then a new page is allocated using
45f87de5
MH
1134 * @gfp_mask and added to the page cache and the VM's LRU
1135 * list. The page is returned locked and with an increased
1136 * refcount. Otherwise, %NULL is returned.
1da177e4 1137 *
2457aec6
MG
1138 * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
1139 * if the GFP flags specified for FGP_CREAT are atomic.
1da177e4 1140 *
2457aec6 1141 * If there is a page cache page, it is returned with an increased refcount.
1da177e4 1142 */
2457aec6 1143struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
45f87de5 1144 int fgp_flags, gfp_t gfp_mask)
1da177e4 1145{
eb2be189 1146 struct page *page;
2457aec6 1147
1da177e4 1148repeat:
2457aec6
MG
1149 page = find_get_entry(mapping, offset);
1150 if (radix_tree_exceptional_entry(page))
1151 page = NULL;
1152 if (!page)
1153 goto no_page;
1154
1155 if (fgp_flags & FGP_LOCK) {
1156 if (fgp_flags & FGP_NOWAIT) {
1157 if (!trylock_page(page)) {
09cbfeaf 1158 put_page(page);
2457aec6
MG
1159 return NULL;
1160 }
1161 } else {
1162 lock_page(page);
1163 }
1164
1165 /* Has the page been truncated? */
1166 if (unlikely(page->mapping != mapping)) {
1167 unlock_page(page);
09cbfeaf 1168 put_page(page);
2457aec6
MG
1169 goto repeat;
1170 }
1171 VM_BUG_ON_PAGE(page->index != offset, page);
1172 }
1173
1174 if (page && (fgp_flags & FGP_ACCESSED))
1175 mark_page_accessed(page);
1176
1177no_page:
1178 if (!page && (fgp_flags & FGP_CREAT)) {
1179 int err;
1180 if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
45f87de5
MH
1181 gfp_mask |= __GFP_WRITE;
1182 if (fgp_flags & FGP_NOFS)
1183 gfp_mask &= ~__GFP_FS;
2457aec6 1184
45f87de5 1185 page = __page_cache_alloc(gfp_mask);
eb2be189
NP
1186 if (!page)
1187 return NULL;
2457aec6
MG
1188
1189 if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
1190 fgp_flags |= FGP_LOCK;
1191
eb39d618 1192 /* Init accessed so avoid atomic mark_page_accessed later */
2457aec6 1193 if (fgp_flags & FGP_ACCESSED)
eb39d618 1194 __SetPageReferenced(page);
2457aec6 1195
45f87de5
MH
1196 err = add_to_page_cache_lru(page, mapping, offset,
1197 gfp_mask & GFP_RECLAIM_MASK);
eb2be189 1198 if (unlikely(err)) {
09cbfeaf 1199 put_page(page);
eb2be189
NP
1200 page = NULL;
1201 if (err == -EEXIST)
1202 goto repeat;
1da177e4 1203 }
1da177e4 1204 }
2457aec6 1205
1da177e4
LT
1206 return page;
1207}
2457aec6 1208EXPORT_SYMBOL(pagecache_get_page);
1da177e4 1209
0cd6144a
JW
1210/**
1211 * find_get_entries - gang pagecache lookup
1212 * @mapping: The address_space to search
1213 * @start: The starting page cache index
1214 * @nr_entries: The maximum number of entries
1215 * @entries: Where the resulting entries are placed
1216 * @indices: The cache indices corresponding to the entries in @entries
1217 *
1218 * find_get_entries() will search for and return a group of up to
1219 * @nr_entries entries in the mapping. The entries are placed at
1220 * @entries. find_get_entries() takes a reference against any actual
1221 * pages it returns.
1222 *
1223 * The search returns a group of mapping-contiguous page cache entries
1224 * with ascending indexes. There may be holes in the indices due to
1225 * not-present pages.
1226 *
139b6a6f
JW
1227 * Any shadow entries of evicted pages, or swap entries from
1228 * shmem/tmpfs, are included in the returned array.
0cd6144a
JW
1229 *
1230 * find_get_entries() returns the number of pages and shadow entries
1231 * which were found.
1232 */
1233unsigned find_get_entries(struct address_space *mapping,
1234 pgoff_t start, unsigned int nr_entries,
1235 struct page **entries, pgoff_t *indices)
1236{
1237 void **slot;
1238 unsigned int ret = 0;
1239 struct radix_tree_iter iter;
1240
1241 if (!nr_entries)
1242 return 0;
1243
1244 rcu_read_lock();
0cd6144a
JW
1245 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1246 struct page *page;
1247repeat:
1248 page = radix_tree_deref_slot(slot);
1249 if (unlikely(!page))
1250 continue;
1251 if (radix_tree_exception(page)) {
2cf938aa
MW
1252 if (radix_tree_deref_retry(page)) {
1253 slot = radix_tree_iter_retry(&iter);
1254 continue;
1255 }
0cd6144a 1256 /*
f9fe48be
RZ
1257 * A shadow entry of a recently evicted page, a swap
1258 * entry from shmem/tmpfs or a DAX entry. Return it
1259 * without attempting to raise page count.
0cd6144a
JW
1260 */
1261 goto export;
1262 }
1263 if (!page_cache_get_speculative(page))
1264 goto repeat;
1265
1266 /* Has the page moved? */
1267 if (unlikely(page != *slot)) {
09cbfeaf 1268 put_page(page);
0cd6144a
JW
1269 goto repeat;
1270 }
1271export:
1272 indices[ret] = iter.index;
1273 entries[ret] = page;
1274 if (++ret == nr_entries)
1275 break;
1276 }
1277 rcu_read_unlock();
1278 return ret;
1279}
1280
1da177e4
LT
1281/**
1282 * find_get_pages - gang pagecache lookup
1283 * @mapping: The address_space to search
1284 * @start: The starting page index
1285 * @nr_pages: The maximum number of pages
1286 * @pages: Where the resulting pages are placed
1287 *
1288 * find_get_pages() will search for and return a group of up to
1289 * @nr_pages pages in the mapping. The pages are placed at @pages.
1290 * find_get_pages() takes a reference against the returned pages.
1291 *
1292 * The search returns a group of mapping-contiguous pages with ascending
1293 * indexes. There may be holes in the indices due to not-present pages.
1294 *
1295 * find_get_pages() returns the number of pages which were found.
1296 */
1297unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
1298 unsigned int nr_pages, struct page **pages)
1299{
0fc9d104
KK
1300 struct radix_tree_iter iter;
1301 void **slot;
1302 unsigned ret = 0;
1303
1304 if (unlikely(!nr_pages))
1305 return 0;
a60637c8
NP
1306
1307 rcu_read_lock();
0fc9d104 1308 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
a60637c8
NP
1309 struct page *page;
1310repeat:
0fc9d104 1311 page = radix_tree_deref_slot(slot);
a60637c8
NP
1312 if (unlikely(!page))
1313 continue;
9d8aa4ea 1314
a2c16d6c 1315 if (radix_tree_exception(page)) {
8079b1c8 1316 if (radix_tree_deref_retry(page)) {
2cf938aa
MW
1317 slot = radix_tree_iter_retry(&iter);
1318 continue;
8079b1c8 1319 }
a2c16d6c 1320 /*
139b6a6f
JW
1321 * A shadow entry of a recently evicted page,
1322 * or a swap entry from shmem/tmpfs. Skip
1323 * over it.
a2c16d6c 1324 */
8079b1c8 1325 continue;
27d20fdd 1326 }
a60637c8
NP
1327
1328 if (!page_cache_get_speculative(page))
1329 goto repeat;
1330
1331 /* Has the page moved? */
0fc9d104 1332 if (unlikely(page != *slot)) {
09cbfeaf 1333 put_page(page);
a60637c8
NP
1334 goto repeat;
1335 }
1da177e4 1336
a60637c8 1337 pages[ret] = page;
0fc9d104
KK
1338 if (++ret == nr_pages)
1339 break;
a60637c8 1340 }
5b280c0c 1341
a60637c8 1342 rcu_read_unlock();
1da177e4
LT
1343 return ret;
1344}
1345
ebf43500
JA
1346/**
1347 * find_get_pages_contig - gang contiguous pagecache lookup
1348 * @mapping: The address_space to search
1349 * @index: The starting page index
1350 * @nr_pages: The maximum number of pages
1351 * @pages: Where the resulting pages are placed
1352 *
1353 * find_get_pages_contig() works exactly like find_get_pages(), except
1354 * that the returned number of pages are guaranteed to be contiguous.
1355 *
1356 * find_get_pages_contig() returns the number of pages which were found.
1357 */
1358unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
1359 unsigned int nr_pages, struct page **pages)
1360{
0fc9d104
KK
1361 struct radix_tree_iter iter;
1362 void **slot;
1363 unsigned int ret = 0;
1364
1365 if (unlikely(!nr_pages))
1366 return 0;
a60637c8
NP
1367
1368 rcu_read_lock();
0fc9d104 1369 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
a60637c8
NP
1370 struct page *page;
1371repeat:
0fc9d104
KK
1372 page = radix_tree_deref_slot(slot);
1373 /* The hole, there no reason to continue */
a60637c8 1374 if (unlikely(!page))
0fc9d104 1375 break;
9d8aa4ea 1376
a2c16d6c 1377 if (radix_tree_exception(page)) {
8079b1c8 1378 if (radix_tree_deref_retry(page)) {
2cf938aa
MW
1379 slot = radix_tree_iter_retry(&iter);
1380 continue;
8079b1c8 1381 }
a2c16d6c 1382 /*
139b6a6f
JW
1383 * A shadow entry of a recently evicted page,
1384 * or a swap entry from shmem/tmpfs. Stop
1385 * looking for contiguous pages.
a2c16d6c 1386 */
8079b1c8 1387 break;
a2c16d6c 1388 }
ebf43500 1389
a60637c8
NP
1390 if (!page_cache_get_speculative(page))
1391 goto repeat;
1392
1393 /* Has the page moved? */
0fc9d104 1394 if (unlikely(page != *slot)) {
09cbfeaf 1395 put_page(page);
a60637c8
NP
1396 goto repeat;
1397 }
1398
9cbb4cb2
NP
1399 /*
1400 * must check mapping and index after taking the ref.
1401 * otherwise we can get both false positives and false
1402 * negatives, which is just confusing to the caller.
1403 */
0fc9d104 1404 if (page->mapping == NULL || page->index != iter.index) {
09cbfeaf 1405 put_page(page);
9cbb4cb2
NP
1406 break;
1407 }
1408
a60637c8 1409 pages[ret] = page;
0fc9d104
KK
1410 if (++ret == nr_pages)
1411 break;
ebf43500 1412 }
a60637c8
NP
1413 rcu_read_unlock();
1414 return ret;
ebf43500 1415}
ef71c15c 1416EXPORT_SYMBOL(find_get_pages_contig);
ebf43500 1417
485bb99b
RD
1418/**
1419 * find_get_pages_tag - find and return pages that match @tag
1420 * @mapping: the address_space to search
1421 * @index: the starting page index
1422 * @tag: the tag index
1423 * @nr_pages: the maximum number of pages
1424 * @pages: where the resulting pages are placed
1425 *
1da177e4 1426 * Like find_get_pages, except we only return pages which are tagged with
485bb99b 1427 * @tag. We update @index to index the next page for the traversal.
1da177e4
LT
1428 */
1429unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
1430 int tag, unsigned int nr_pages, struct page **pages)
1431{
0fc9d104
KK
1432 struct radix_tree_iter iter;
1433 void **slot;
1434 unsigned ret = 0;
1435
1436 if (unlikely(!nr_pages))
1437 return 0;
a60637c8
NP
1438
1439 rcu_read_lock();
0fc9d104
KK
1440 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1441 &iter, *index, tag) {
a60637c8
NP
1442 struct page *page;
1443repeat:
0fc9d104 1444 page = radix_tree_deref_slot(slot);
a60637c8
NP
1445 if (unlikely(!page))
1446 continue;
9d8aa4ea 1447
a2c16d6c 1448 if (radix_tree_exception(page)) {
8079b1c8 1449 if (radix_tree_deref_retry(page)) {
2cf938aa
MW
1450 slot = radix_tree_iter_retry(&iter);
1451 continue;
8079b1c8 1452 }
a2c16d6c 1453 /*
139b6a6f
JW
1454 * A shadow entry of a recently evicted page.
1455 *
1456 * Those entries should never be tagged, but
1457 * this tree walk is lockless and the tags are
1458 * looked up in bulk, one radix tree node at a
1459 * time, so there is a sizable window for page
1460 * reclaim to evict a page we saw tagged.
1461 *
1462 * Skip over it.
a2c16d6c 1463 */
139b6a6f 1464 continue;
a2c16d6c 1465 }
a60637c8
NP
1466
1467 if (!page_cache_get_speculative(page))
1468 goto repeat;
1469
1470 /* Has the page moved? */
0fc9d104 1471 if (unlikely(page != *slot)) {
09cbfeaf 1472 put_page(page);
a60637c8
NP
1473 goto repeat;
1474 }
1475
1476 pages[ret] = page;
0fc9d104
KK
1477 if (++ret == nr_pages)
1478 break;
a60637c8 1479 }
5b280c0c 1480
a60637c8 1481 rcu_read_unlock();
1da177e4 1482
1da177e4
LT
1483 if (ret)
1484 *index = pages[ret - 1]->index + 1;
a60637c8 1485
1da177e4
LT
1486 return ret;
1487}
ef71c15c 1488EXPORT_SYMBOL(find_get_pages_tag);
1da177e4 1489
7e7f7749
RZ
1490/**
1491 * find_get_entries_tag - find and return entries that match @tag
1492 * @mapping: the address_space to search
1493 * @start: the starting page cache index
1494 * @tag: the tag index
1495 * @nr_entries: the maximum number of entries
1496 * @entries: where the resulting entries are placed
1497 * @indices: the cache indices corresponding to the entries in @entries
1498 *
1499 * Like find_get_entries, except we only return entries which are tagged with
1500 * @tag.
1501 */
1502unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
1503 int tag, unsigned int nr_entries,
1504 struct page **entries, pgoff_t *indices)
1505{
1506 void **slot;
1507 unsigned int ret = 0;
1508 struct radix_tree_iter iter;
1509
1510 if (!nr_entries)
1511 return 0;
1512
1513 rcu_read_lock();
7e7f7749
RZ
1514 radix_tree_for_each_tagged(slot, &mapping->page_tree,
1515 &iter, start, tag) {
1516 struct page *page;
1517repeat:
1518 page = radix_tree_deref_slot(slot);
1519 if (unlikely(!page))
1520 continue;
1521 if (radix_tree_exception(page)) {
1522 if (radix_tree_deref_retry(page)) {
2cf938aa
MW
1523 slot = radix_tree_iter_retry(&iter);
1524 continue;
7e7f7749
RZ
1525 }
1526
1527 /*
1528 * A shadow entry of a recently evicted page, a swap
1529 * entry from shmem/tmpfs or a DAX entry. Return it
1530 * without attempting to raise page count.
1531 */
1532 goto export;
1533 }
1534 if (!page_cache_get_speculative(page))
1535 goto repeat;
1536
1537 /* Has the page moved? */
1538 if (unlikely(page != *slot)) {
09cbfeaf 1539 put_page(page);
7e7f7749
RZ
1540 goto repeat;
1541 }
1542export:
1543 indices[ret] = iter.index;
1544 entries[ret] = page;
1545 if (++ret == nr_entries)
1546 break;
1547 }
1548 rcu_read_unlock();
1549 return ret;
1550}
1551EXPORT_SYMBOL(find_get_entries_tag);
1552
76d42bd9
WF
1553/*
1554 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
1555 * a _large_ part of the i/o request. Imagine the worst scenario:
1556 *
1557 * ---R__________________________________________B__________
1558 * ^ reading here ^ bad block(assume 4k)
1559 *
1560 * read(R) => miss => readahead(R...B) => media error => frustrating retries
1561 * => failing the whole request => read(R) => read(R+1) =>
1562 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
1563 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
1564 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
1565 *
1566 * It is going insane. Fix it by quickly scaling down the readahead size.
1567 */
1568static void shrink_readahead_size_eio(struct file *filp,
1569 struct file_ra_state *ra)
1570{
76d42bd9 1571 ra->ra_pages /= 4;
76d42bd9
WF
1572}
1573
485bb99b 1574/**
36e78914 1575 * do_generic_file_read - generic file read routine
485bb99b
RD
1576 * @filp: the file to read
1577 * @ppos: current file position
6e58e79d
AV
1578 * @iter: data destination
1579 * @written: already copied
485bb99b 1580 *
1da177e4 1581 * This is a generic file read routine, and uses the
485bb99b 1582 * mapping->a_ops->readpage() function for the actual low-level stuff.
1da177e4
LT
1583 *
1584 * This is really ugly. But the goto's actually try to clarify some
1585 * of the logic when it comes to error handling etc.
1da177e4 1586 */
6e58e79d
AV
1587static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
1588 struct iov_iter *iter, ssize_t written)
1da177e4 1589{
36e78914 1590 struct address_space *mapping = filp->f_mapping;
1da177e4 1591 struct inode *inode = mapping->host;
36e78914 1592 struct file_ra_state *ra = &filp->f_ra;
57f6b96c
FW
1593 pgoff_t index;
1594 pgoff_t last_index;
1595 pgoff_t prev_index;
1596 unsigned long offset; /* offset into pagecache page */
ec0f1637 1597 unsigned int prev_offset;
6e58e79d 1598 int error = 0;
1da177e4 1599
09cbfeaf
KS
1600 index = *ppos >> PAGE_SHIFT;
1601 prev_index = ra->prev_pos >> PAGE_SHIFT;
1602 prev_offset = ra->prev_pos & (PAGE_SIZE-1);
1603 last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
1604 offset = *ppos & ~PAGE_MASK;
1da177e4 1605
1da177e4
LT
1606 for (;;) {
1607 struct page *page;
57f6b96c 1608 pgoff_t end_index;
a32ea1e1 1609 loff_t isize;
1da177e4
LT
1610 unsigned long nr, ret;
1611
1da177e4 1612 cond_resched();
1da177e4
LT
1613find_page:
1614 page = find_get_page(mapping, index);
3ea89ee8 1615 if (!page) {
cf914a7d 1616 page_cache_sync_readahead(mapping,
7ff81078 1617 ra, filp,
3ea89ee8
FW
1618 index, last_index - index);
1619 page = find_get_page(mapping, index);
1620 if (unlikely(page == NULL))
1621 goto no_cached_page;
1622 }
1623 if (PageReadahead(page)) {
cf914a7d 1624 page_cache_async_readahead(mapping,
7ff81078 1625 ra, filp, page,
3ea89ee8 1626 index, last_index - index);
1da177e4 1627 }
8ab22b9a 1628 if (!PageUptodate(page)) {
ebded027
MG
1629 /*
1630 * See comment in do_read_cache_page on why
1631 * wait_on_page_locked is used to avoid unnecessarily
1632 * serialisations and why it's safe.
1633 */
1634 wait_on_page_locked_killable(page);
1635 if (PageUptodate(page))
1636 goto page_ok;
1637
09cbfeaf 1638 if (inode->i_blkbits == PAGE_SHIFT ||
8ab22b9a
HH
1639 !mapping->a_ops->is_partially_uptodate)
1640 goto page_not_up_to_date;
529ae9aa 1641 if (!trylock_page(page))
8ab22b9a 1642 goto page_not_up_to_date;
8d056cb9
DH
1643 /* Did it get truncated before we got the lock? */
1644 if (!page->mapping)
1645 goto page_not_up_to_date_locked;
8ab22b9a 1646 if (!mapping->a_ops->is_partially_uptodate(page,
6e58e79d 1647 offset, iter->count))
8ab22b9a
HH
1648 goto page_not_up_to_date_locked;
1649 unlock_page(page);
1650 }
1da177e4 1651page_ok:
a32ea1e1
N
1652 /*
1653 * i_size must be checked after we know the page is Uptodate.
1654 *
1655 * Checking i_size after the check allows us to calculate
1656 * the correct value for "nr", which means the zero-filled
1657 * part of the page is not copied back to userspace (unless
1658 * another truncate extends the file - this is desired though).
1659 */
1660
1661 isize = i_size_read(inode);
09cbfeaf 1662 end_index = (isize - 1) >> PAGE_SHIFT;
a32ea1e1 1663 if (unlikely(!isize || index > end_index)) {
09cbfeaf 1664 put_page(page);
a32ea1e1
N
1665 goto out;
1666 }
1667
1668 /* nr is the maximum number of bytes to copy from this page */
09cbfeaf 1669 nr = PAGE_SIZE;
a32ea1e1 1670 if (index == end_index) {
09cbfeaf 1671 nr = ((isize - 1) & ~PAGE_MASK) + 1;
a32ea1e1 1672 if (nr <= offset) {
09cbfeaf 1673 put_page(page);
a32ea1e1
N
1674 goto out;
1675 }
1676 }
1677 nr = nr - offset;
1da177e4
LT
1678
1679 /* If users can be writing to this page using arbitrary
1680 * virtual addresses, take care about potential aliasing
1681 * before reading the page on the kernel side.
1682 */
1683 if (mapping_writably_mapped(mapping))
1684 flush_dcache_page(page);
1685
1686 /*
ec0f1637
JK
1687 * When a sequential read accesses a page several times,
1688 * only mark it as accessed the first time.
1da177e4 1689 */
ec0f1637 1690 if (prev_index != index || offset != prev_offset)
1da177e4
LT
1691 mark_page_accessed(page);
1692 prev_index = index;
1693
1694 /*
1695 * Ok, we have the page, and it's up-to-date, so
1696 * now we can copy it to user space...
1da177e4 1697 */
6e58e79d
AV
1698
1699 ret = copy_page_to_iter(page, offset, nr, iter);
1da177e4 1700 offset += ret;
09cbfeaf
KS
1701 index += offset >> PAGE_SHIFT;
1702 offset &= ~PAGE_MASK;
6ce745ed 1703 prev_offset = offset;
1da177e4 1704
09cbfeaf 1705 put_page(page);
6e58e79d
AV
1706 written += ret;
1707 if (!iov_iter_count(iter))
1708 goto out;
1709 if (ret < nr) {
1710 error = -EFAULT;
1711 goto out;
1712 }
1713 continue;
1da177e4
LT
1714
1715page_not_up_to_date:
1716 /* Get exclusive access to the page ... */
85462323
ON
1717 error = lock_page_killable(page);
1718 if (unlikely(error))
1719 goto readpage_error;
1da177e4 1720
8ab22b9a 1721page_not_up_to_date_locked:
da6052f7 1722 /* Did it get truncated before we got the lock? */
1da177e4
LT
1723 if (!page->mapping) {
1724 unlock_page(page);
09cbfeaf 1725 put_page(page);
1da177e4
LT
1726 continue;
1727 }
1728
1729 /* Did somebody else fill it already? */
1730 if (PageUptodate(page)) {
1731 unlock_page(page);
1732 goto page_ok;
1733 }
1734
1735readpage:
91803b49
JM
1736 /*
1737 * A previous I/O error may have been due to temporary
1738 * failures, eg. multipath errors.
1739 * PG_error will be set again if readpage fails.
1740 */
1741 ClearPageError(page);
1da177e4
LT
1742 /* Start the actual read. The read will unlock the page. */
1743 error = mapping->a_ops->readpage(filp, page);
1744
994fc28c
ZB
1745 if (unlikely(error)) {
1746 if (error == AOP_TRUNCATED_PAGE) {
09cbfeaf 1747 put_page(page);
6e58e79d 1748 error = 0;
994fc28c
ZB
1749 goto find_page;
1750 }
1da177e4 1751 goto readpage_error;
994fc28c 1752 }
1da177e4
LT
1753
1754 if (!PageUptodate(page)) {
85462323
ON
1755 error = lock_page_killable(page);
1756 if (unlikely(error))
1757 goto readpage_error;
1da177e4
LT
1758 if (!PageUptodate(page)) {
1759 if (page->mapping == NULL) {
1760 /*
2ecdc82e 1761 * invalidate_mapping_pages got it
1da177e4
LT
1762 */
1763 unlock_page(page);
09cbfeaf 1764 put_page(page);
1da177e4
LT
1765 goto find_page;
1766 }
1767 unlock_page(page);
7ff81078 1768 shrink_readahead_size_eio(filp, ra);
85462323
ON
1769 error = -EIO;
1770 goto readpage_error;
1da177e4
LT
1771 }
1772 unlock_page(page);
1773 }
1774
1da177e4
LT
1775 goto page_ok;
1776
1777readpage_error:
1778 /* UHHUH! A synchronous read error occurred. Report it */
09cbfeaf 1779 put_page(page);
1da177e4
LT
1780 goto out;
1781
1782no_cached_page:
1783 /*
1784 * Ok, it wasn't cached, so we need to create a new
1785 * page..
1786 */
eb2be189
NP
1787 page = page_cache_alloc_cold(mapping);
1788 if (!page) {
6e58e79d 1789 error = -ENOMEM;
eb2be189 1790 goto out;
1da177e4 1791 }
6afdb859 1792 error = add_to_page_cache_lru(page, mapping, index,
c62d2555 1793 mapping_gfp_constraint(mapping, GFP_KERNEL));
1da177e4 1794 if (error) {
09cbfeaf 1795 put_page(page);
6e58e79d
AV
1796 if (error == -EEXIST) {
1797 error = 0;
1da177e4 1798 goto find_page;
6e58e79d 1799 }
1da177e4
LT
1800 goto out;
1801 }
1da177e4
LT
1802 goto readpage;
1803 }
1804
1805out:
7ff81078 1806 ra->prev_pos = prev_index;
09cbfeaf 1807 ra->prev_pos <<= PAGE_SHIFT;
7ff81078 1808 ra->prev_pos |= prev_offset;
1da177e4 1809
09cbfeaf 1810 *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
0c6aa263 1811 file_accessed(filp);
6e58e79d 1812 return written ? written : error;
1da177e4
LT
1813}
1814
485bb99b 1815/**
6abd2322 1816 * generic_file_read_iter - generic filesystem read routine
485bb99b 1817 * @iocb: kernel I/O control block
6abd2322 1818 * @iter: destination for the data read
485bb99b 1819 *
6abd2322 1820 * This is the "read_iter()" routine for all filesystems
1da177e4
LT
1821 * that can use the page cache directly.
1822 */
1823ssize_t
ed978a81 1824generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1da177e4 1825{
ed978a81 1826 struct file *file = iocb->ki_filp;
cb66a7a1 1827 ssize_t retval = 0;
e7080a43
NS
1828 size_t count = iov_iter_count(iter);
1829
1830 if (!count)
1831 goto out; /* skip atime */
1da177e4 1832
2ba48ce5 1833 if (iocb->ki_flags & IOCB_DIRECT) {
ed978a81
AV
1834 struct address_space *mapping = file->f_mapping;
1835 struct inode *inode = mapping->host;
543ade1f 1836 loff_t size;
1da177e4 1837
1da177e4 1838 size = i_size_read(inode);
c64fb5c7
CH
1839 retval = filemap_write_and_wait_range(mapping, iocb->ki_pos,
1840 iocb->ki_pos + count - 1);
9fe55eea 1841 if (!retval) {
ed978a81 1842 struct iov_iter data = *iter;
c8b8e32d 1843 retval = mapping->a_ops->direct_IO(iocb, &data);
9fe55eea 1844 }
d8d3d94b 1845
9fe55eea 1846 if (retval > 0) {
c64fb5c7 1847 iocb->ki_pos += retval;
ed978a81 1848 iov_iter_advance(iter, retval);
9fe55eea 1849 }
66f998f6 1850
9fe55eea
SW
1851 /*
1852 * Btrfs can have a short DIO read if we encounter
1853 * compressed extents, so if there was an error, or if
1854 * we've already read everything we wanted to, or if
1855 * there was a short read because we hit EOF, go ahead
1856 * and return. Otherwise fallthrough to buffered io for
fbbbad4b
MW
1857 * the rest of the read. Buffered reads will not work for
1858 * DAX files, so don't bother trying.
9fe55eea 1859 */
c64fb5c7 1860 if (retval < 0 || !iov_iter_count(iter) || iocb->ki_pos >= size ||
fbbbad4b 1861 IS_DAX(inode)) {
ed978a81 1862 file_accessed(file);
9fe55eea 1863 goto out;
0e0bcae3 1864 }
1da177e4
LT
1865 }
1866
c64fb5c7 1867 retval = do_generic_file_read(file, &iocb->ki_pos, iter, retval);
1da177e4
LT
1868out:
1869 return retval;
1870}
ed978a81 1871EXPORT_SYMBOL(generic_file_read_iter);
1da177e4 1872
1da177e4 1873#ifdef CONFIG_MMU
485bb99b
RD
1874/**
1875 * page_cache_read - adds requested page to the page cache if not already there
1876 * @file: file to read
1877 * @offset: page index
62eb320a 1878 * @gfp_mask: memory allocation flags
485bb99b 1879 *
1da177e4
LT
1880 * This adds the requested page to the page cache if it isn't already there,
1881 * and schedules an I/O to read in its contents from disk.
1882 */
c20cd45e 1883static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
1da177e4
LT
1884{
1885 struct address_space *mapping = file->f_mapping;
99dadfdd 1886 struct page *page;
994fc28c 1887 int ret;
1da177e4 1888
994fc28c 1889 do {
c20cd45e 1890 page = __page_cache_alloc(gfp_mask|__GFP_COLD);
994fc28c
ZB
1891 if (!page)
1892 return -ENOMEM;
1893
c20cd45e 1894 ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask & GFP_KERNEL);
994fc28c
ZB
1895 if (ret == 0)
1896 ret = mapping->a_ops->readpage(file, page);
1897 else if (ret == -EEXIST)
1898 ret = 0; /* losing race to add is OK */
1da177e4 1899
09cbfeaf 1900 put_page(page);
1da177e4 1901
994fc28c 1902 } while (ret == AOP_TRUNCATED_PAGE);
99dadfdd 1903
994fc28c 1904 return ret;
1da177e4
LT
1905}
1906
1907#define MMAP_LOTSAMISS (100)
1908
ef00e08e
LT
1909/*
1910 * Synchronous readahead happens when we don't even find
1911 * a page in the page cache at all.
1912 */
1913static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1914 struct file_ra_state *ra,
1915 struct file *file,
1916 pgoff_t offset)
1917{
ef00e08e
LT
1918 struct address_space *mapping = file->f_mapping;
1919
1920 /* If we don't want any read-ahead, don't bother */
64363aad 1921 if (vma->vm_flags & VM_RAND_READ)
ef00e08e 1922 return;
275b12bf
WF
1923 if (!ra->ra_pages)
1924 return;
ef00e08e 1925
64363aad 1926 if (vma->vm_flags & VM_SEQ_READ) {
7ffc59b4
WF
1927 page_cache_sync_readahead(mapping, ra, file, offset,
1928 ra->ra_pages);
ef00e08e
LT
1929 return;
1930 }
1931
207d04ba
AK
1932 /* Avoid banging the cache line if not needed */
1933 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
ef00e08e
LT
1934 ra->mmap_miss++;
1935
1936 /*
1937 * Do we miss much more than hit in this file? If so,
1938 * stop bothering with read-ahead. It will only hurt.
1939 */
1940 if (ra->mmap_miss > MMAP_LOTSAMISS)
1941 return;
1942
d30a1100
WF
1943 /*
1944 * mmap read-around
1945 */
600e19af
RG
1946 ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
1947 ra->size = ra->ra_pages;
1948 ra->async_size = ra->ra_pages / 4;
275b12bf 1949 ra_submit(ra, mapping, file);
ef00e08e
LT
1950}
1951
1952/*
1953 * Asynchronous readahead happens when we find the page and PG_readahead,
1954 * so we want to possibly extend the readahead further..
1955 */
1956static void do_async_mmap_readahead(struct vm_area_struct *vma,
1957 struct file_ra_state *ra,
1958 struct file *file,
1959 struct page *page,
1960 pgoff_t offset)
1961{
1962 struct address_space *mapping = file->f_mapping;
1963
1964 /* If we don't want any read-ahead, don't bother */
64363aad 1965 if (vma->vm_flags & VM_RAND_READ)
ef00e08e
LT
1966 return;
1967 if (ra->mmap_miss > 0)
1968 ra->mmap_miss--;
1969 if (PageReadahead(page))
2fad6f5d
WF
1970 page_cache_async_readahead(mapping, ra, file,
1971 page, offset, ra->ra_pages);
ef00e08e
LT
1972}
1973
485bb99b 1974/**
54cb8821 1975 * filemap_fault - read in file data for page fault handling
d0217ac0
NP
1976 * @vma: vma in which the fault was taken
1977 * @vmf: struct vm_fault containing details of the fault
485bb99b 1978 *
54cb8821 1979 * filemap_fault() is invoked via the vma operations vector for a
1da177e4
LT
1980 * mapped memory region to read in file data during a page fault.
1981 *
1982 * The goto's are kind of ugly, but this streamlines the normal case of having
1983 * it in the page cache, and handles the special cases reasonably without
1984 * having a lot of duplicated code.
9a95f3cf
PC
1985 *
1986 * vma->vm_mm->mmap_sem must be held on entry.
1987 *
1988 * If our return value has VM_FAULT_RETRY set, it's because
1989 * lock_page_or_retry() returned 0.
1990 * The mmap_sem has usually been released in this case.
1991 * See __lock_page_or_retry() for the exception.
1992 *
1993 * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
1994 * has not been released.
1995 *
1996 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
1da177e4 1997 */
d0217ac0 1998int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4
LT
1999{
2000 int error;
54cb8821 2001 struct file *file = vma->vm_file;
1da177e4
LT
2002 struct address_space *mapping = file->f_mapping;
2003 struct file_ra_state *ra = &file->f_ra;
2004 struct inode *inode = mapping->host;
ef00e08e 2005 pgoff_t offset = vmf->pgoff;
1da177e4 2006 struct page *page;
99e3e53f 2007 loff_t size;
83c54070 2008 int ret = 0;
1da177e4 2009
09cbfeaf
KS
2010 size = round_up(i_size_read(inode), PAGE_SIZE);
2011 if (offset >= size >> PAGE_SHIFT)
5307cc1a 2012 return VM_FAULT_SIGBUS;
1da177e4 2013
1da177e4 2014 /*
49426420 2015 * Do we have something in the page cache already?
1da177e4 2016 */
ef00e08e 2017 page = find_get_page(mapping, offset);
45cac65b 2018 if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
1da177e4 2019 /*
ef00e08e
LT
2020 * We found the page, so try async readahead before
2021 * waiting for the lock.
1da177e4 2022 */
ef00e08e 2023 do_async_mmap_readahead(vma, ra, file, page, offset);
45cac65b 2024 } else if (!page) {
ef00e08e
LT
2025 /* No page in the page cache at all */
2026 do_sync_mmap_readahead(vma, ra, file, offset);
2027 count_vm_event(PGMAJFAULT);
456f998e 2028 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
ef00e08e
LT
2029 ret = VM_FAULT_MAJOR;
2030retry_find:
b522c94d 2031 page = find_get_page(mapping, offset);
1da177e4
LT
2032 if (!page)
2033 goto no_cached_page;
2034 }
2035
d88c0922 2036 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
09cbfeaf 2037 put_page(page);
d065bd81 2038 return ret | VM_FAULT_RETRY;
d88c0922 2039 }
b522c94d
ML
2040
2041 /* Did it get truncated? */
2042 if (unlikely(page->mapping != mapping)) {
2043 unlock_page(page);
2044 put_page(page);
2045 goto retry_find;
2046 }
309381fe 2047 VM_BUG_ON_PAGE(page->index != offset, page);
b522c94d 2048
1da177e4 2049 /*
d00806b1
NP
2050 * We have a locked page in the page cache, now we need to check
2051 * that it's up-to-date. If not, it is going to be due to an error.
1da177e4 2052 */
d00806b1 2053 if (unlikely(!PageUptodate(page)))
1da177e4
LT
2054 goto page_not_uptodate;
2055
ef00e08e
LT
2056 /*
2057 * Found the page and have a reference on it.
2058 * We must recheck i_size under page lock.
2059 */
09cbfeaf
KS
2060 size = round_up(i_size_read(inode), PAGE_SIZE);
2061 if (unlikely(offset >= size >> PAGE_SHIFT)) {
d00806b1 2062 unlock_page(page);
09cbfeaf 2063 put_page(page);
5307cc1a 2064 return VM_FAULT_SIGBUS;
d00806b1
NP
2065 }
2066
d0217ac0 2067 vmf->page = page;
83c54070 2068 return ret | VM_FAULT_LOCKED;
1da177e4 2069
1da177e4
LT
2070no_cached_page:
2071 /*
2072 * We're only likely to ever get here if MADV_RANDOM is in
2073 * effect.
2074 */
c20cd45e 2075 error = page_cache_read(file, offset, vmf->gfp_mask);
1da177e4
LT
2076
2077 /*
2078 * The page we want has now been added to the page cache.
2079 * In the unlikely event that someone removed it in the
2080 * meantime, we'll just come back here and read it again.
2081 */
2082 if (error >= 0)
2083 goto retry_find;
2084
2085 /*
2086 * An error return from page_cache_read can result if the
2087 * system is low on memory, or a problem occurs while trying
2088 * to schedule I/O.
2089 */
2090 if (error == -ENOMEM)
d0217ac0
NP
2091 return VM_FAULT_OOM;
2092 return VM_FAULT_SIGBUS;
1da177e4
LT
2093
2094page_not_uptodate:
1da177e4
LT
2095 /*
2096 * Umm, take care of errors if the page isn't up-to-date.
2097 * Try to re-read it _once_. We do this synchronously,
2098 * because there really aren't any performance issues here
2099 * and we need to check for errors.
2100 */
1da177e4 2101 ClearPageError(page);
994fc28c 2102 error = mapping->a_ops->readpage(file, page);
3ef0f720
MS
2103 if (!error) {
2104 wait_on_page_locked(page);
2105 if (!PageUptodate(page))
2106 error = -EIO;
2107 }
09cbfeaf 2108 put_page(page);
d00806b1
NP
2109
2110 if (!error || error == AOP_TRUNCATED_PAGE)
994fc28c 2111 goto retry_find;
1da177e4 2112
d00806b1 2113 /* Things didn't work out. Return zero to tell the mm layer so. */
76d42bd9 2114 shrink_readahead_size_eio(file, ra);
d0217ac0 2115 return VM_FAULT_SIGBUS;
54cb8821
NP
2116}
2117EXPORT_SYMBOL(filemap_fault);
2118
f1820361
KS
2119void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
2120{
2121 struct radix_tree_iter iter;
2122 void **slot;
2123 struct file *file = vma->vm_file;
2124 struct address_space *mapping = file->f_mapping;
2125 loff_t size;
2126 struct page *page;
2127 unsigned long address = (unsigned long) vmf->virtual_address;
2128 unsigned long addr;
2129 pte_t *pte;
2130
2131 rcu_read_lock();
2132 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, vmf->pgoff) {
2133 if (iter.index > vmf->max_pgoff)
2134 break;
2135repeat:
2136 page = radix_tree_deref_slot(slot);
2137 if (unlikely(!page))
2138 goto next;
2139 if (radix_tree_exception(page)) {
2cf938aa
MW
2140 if (radix_tree_deref_retry(page)) {
2141 slot = radix_tree_iter_retry(&iter);
2142 continue;
2143 }
2144 goto next;
f1820361
KS
2145 }
2146
2147 if (!page_cache_get_speculative(page))
2148 goto repeat;
2149
2150 /* Has the page moved? */
2151 if (unlikely(page != *slot)) {
09cbfeaf 2152 put_page(page);
f1820361
KS
2153 goto repeat;
2154 }
2155
2156 if (!PageUptodate(page) ||
2157 PageReadahead(page) ||
2158 PageHWPoison(page))
2159 goto skip;
2160 if (!trylock_page(page))
2161 goto skip;
2162
2163 if (page->mapping != mapping || !PageUptodate(page))
2164 goto unlock;
2165
09cbfeaf
KS
2166 size = round_up(i_size_read(mapping->host), PAGE_SIZE);
2167 if (page->index >= size >> PAGE_SHIFT)
f1820361
KS
2168 goto unlock;
2169
2170 pte = vmf->pte + page->index - vmf->pgoff;
2171 if (!pte_none(*pte))
2172 goto unlock;
2173
2174 if (file->f_ra.mmap_miss > 0)
2175 file->f_ra.mmap_miss--;
2176 addr = address + (page->index - vmf->pgoff) * PAGE_SIZE;
5c0a85fa 2177 do_set_pte(vma, addr, page, pte, false, false, true);
f1820361
KS
2178 unlock_page(page);
2179 goto next;
2180unlock:
2181 unlock_page(page);
2182skip:
09cbfeaf 2183 put_page(page);
f1820361
KS
2184next:
2185 if (iter.index == vmf->max_pgoff)
2186 break;
2187 }
2188 rcu_read_unlock();
2189}
2190EXPORT_SYMBOL(filemap_map_pages);
2191
4fcf1c62
JK
2192int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2193{
2194 struct page *page = vmf->page;
496ad9aa 2195 struct inode *inode = file_inode(vma->vm_file);
4fcf1c62
JK
2196 int ret = VM_FAULT_LOCKED;
2197
14da9200 2198 sb_start_pagefault(inode->i_sb);
4fcf1c62
JK
2199 file_update_time(vma->vm_file);
2200 lock_page(page);
2201 if (page->mapping != inode->i_mapping) {
2202 unlock_page(page);
2203 ret = VM_FAULT_NOPAGE;
2204 goto out;
2205 }
14da9200
JK
2206 /*
2207 * We mark the page dirty already here so that when freeze is in
2208 * progress, we are guaranteed that writeback during freezing will
2209 * see the dirty page and writeprotect it again.
2210 */
2211 set_page_dirty(page);
1d1d1a76 2212 wait_for_stable_page(page);
4fcf1c62 2213out:
14da9200 2214 sb_end_pagefault(inode->i_sb);
4fcf1c62
JK
2215 return ret;
2216}
2217EXPORT_SYMBOL(filemap_page_mkwrite);
2218
f0f37e2f 2219const struct vm_operations_struct generic_file_vm_ops = {
54cb8821 2220 .fault = filemap_fault,
f1820361 2221 .map_pages = filemap_map_pages,
4fcf1c62 2222 .page_mkwrite = filemap_page_mkwrite,
1da177e4
LT
2223};
2224
2225/* This is used for a general mmap of a disk file */
2226
2227int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2228{
2229 struct address_space *mapping = file->f_mapping;
2230
2231 if (!mapping->a_ops->readpage)
2232 return -ENOEXEC;
2233 file_accessed(file);
2234 vma->vm_ops = &generic_file_vm_ops;
2235 return 0;
2236}
1da177e4
LT
2237
2238/*
2239 * This is for filesystems which do not implement ->writepage.
2240 */
2241int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
2242{
2243 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2244 return -EINVAL;
2245 return generic_file_mmap(file, vma);
2246}
2247#else
2248int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2249{
2250 return -ENOSYS;
2251}
2252int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
2253{
2254 return -ENOSYS;
2255}
2256#endif /* CONFIG_MMU */
2257
2258EXPORT_SYMBOL(generic_file_mmap);
2259EXPORT_SYMBOL(generic_file_readonly_mmap);
2260
67f9fd91
SL
2261static struct page *wait_on_page_read(struct page *page)
2262{
2263 if (!IS_ERR(page)) {
2264 wait_on_page_locked(page);
2265 if (!PageUptodate(page)) {
09cbfeaf 2266 put_page(page);
67f9fd91
SL
2267 page = ERR_PTR(-EIO);
2268 }
2269 }
2270 return page;
2271}
2272
32b63529 2273static struct page *do_read_cache_page(struct address_space *mapping,
57f6b96c 2274 pgoff_t index,
5e5358e7 2275 int (*filler)(void *, struct page *),
0531b2aa
LT
2276 void *data,
2277 gfp_t gfp)
1da177e4 2278{
eb2be189 2279 struct page *page;
1da177e4
LT
2280 int err;
2281repeat:
2282 page = find_get_page(mapping, index);
2283 if (!page) {
0531b2aa 2284 page = __page_cache_alloc(gfp | __GFP_COLD);
eb2be189
NP
2285 if (!page)
2286 return ERR_PTR(-ENOMEM);
e6f67b8c 2287 err = add_to_page_cache_lru(page, mapping, index, gfp);
eb2be189 2288 if (unlikely(err)) {
09cbfeaf 2289 put_page(page);
eb2be189
NP
2290 if (err == -EEXIST)
2291 goto repeat;
1da177e4 2292 /* Presumably ENOMEM for radix tree node */
1da177e4
LT
2293 return ERR_PTR(err);
2294 }
32b63529
MG
2295
2296filler:
1da177e4
LT
2297 err = filler(data, page);
2298 if (err < 0) {
09cbfeaf 2299 put_page(page);
32b63529 2300 return ERR_PTR(err);
1da177e4 2301 }
1da177e4 2302
32b63529
MG
2303 page = wait_on_page_read(page);
2304 if (IS_ERR(page))
2305 return page;
2306 goto out;
2307 }
1da177e4
LT
2308 if (PageUptodate(page))
2309 goto out;
2310
ebded027
MG
2311 /*
2312 * Page is not up to date and may be locked due one of the following
2313 * case a: Page is being filled and the page lock is held
2314 * case b: Read/write error clearing the page uptodate status
2315 * case c: Truncation in progress (page locked)
2316 * case d: Reclaim in progress
2317 *
2318 * Case a, the page will be up to date when the page is unlocked.
2319 * There is no need to serialise on the page lock here as the page
2320 * is pinned so the lock gives no additional protection. Even if the
2321 * the page is truncated, the data is still valid if PageUptodate as
2322 * it's a race vs truncate race.
2323 * Case b, the page will not be up to date
2324 * Case c, the page may be truncated but in itself, the data may still
2325 * be valid after IO completes as it's a read vs truncate race. The
2326 * operation must restart if the page is not uptodate on unlock but
2327 * otherwise serialising on page lock to stabilise the mapping gives
2328 * no additional guarantees to the caller as the page lock is
2329 * released before return.
2330 * Case d, similar to truncation. If reclaim holds the page lock, it
2331 * will be a race with remove_mapping that determines if the mapping
2332 * is valid on unlock but otherwise the data is valid and there is
2333 * no need to serialise with page lock.
2334 *
2335 * As the page lock gives no additional guarantee, we optimistically
2336 * wait on the page to be unlocked and check if it's up to date and
2337 * use the page if it is. Otherwise, the page lock is required to
2338 * distinguish between the different cases. The motivation is that we
2339 * avoid spurious serialisations and wakeups when multiple processes
2340 * wait on the same page for IO to complete.
2341 */
2342 wait_on_page_locked(page);
2343 if (PageUptodate(page))
2344 goto out;
2345
2346 /* Distinguish between all the cases under the safety of the lock */
1da177e4 2347 lock_page(page);
ebded027
MG
2348
2349 /* Case c or d, restart the operation */
1da177e4
LT
2350 if (!page->mapping) {
2351 unlock_page(page);
09cbfeaf 2352 put_page(page);
32b63529 2353 goto repeat;
1da177e4 2354 }
ebded027
MG
2355
2356 /* Someone else locked and filled the page in a very small window */
1da177e4
LT
2357 if (PageUptodate(page)) {
2358 unlock_page(page);
2359 goto out;
2360 }
32b63529
MG
2361 goto filler;
2362
c855ff37 2363out:
6fe6900e
NP
2364 mark_page_accessed(page);
2365 return page;
2366}
0531b2aa
LT
2367
2368/**
67f9fd91 2369 * read_cache_page - read into page cache, fill it if needed
0531b2aa
LT
2370 * @mapping: the page's address_space
2371 * @index: the page index
2372 * @filler: function to perform the read
5e5358e7 2373 * @data: first arg to filler(data, page) function, often left as NULL
0531b2aa 2374 *
0531b2aa 2375 * Read into the page cache. If a page already exists, and PageUptodate() is
67f9fd91 2376 * not set, try to fill the page and wait for it to become unlocked.
0531b2aa
LT
2377 *
2378 * If the page does not get brought uptodate, return -EIO.
2379 */
67f9fd91 2380struct page *read_cache_page(struct address_space *mapping,
0531b2aa 2381 pgoff_t index,
5e5358e7 2382 int (*filler)(void *, struct page *),
0531b2aa
LT
2383 void *data)
2384{
2385 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
2386}
67f9fd91 2387EXPORT_SYMBOL(read_cache_page);
0531b2aa
LT
2388
2389/**
2390 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
2391 * @mapping: the page's address_space
2392 * @index: the page index
2393 * @gfp: the page allocator flags to use if allocating
2394 *
2395 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
e6f67b8c 2396 * any new page allocations done using the specified allocation flags.
0531b2aa
LT
2397 *
2398 * If the page does not get brought uptodate, return -EIO.
2399 */
2400struct page *read_cache_page_gfp(struct address_space *mapping,
2401 pgoff_t index,
2402 gfp_t gfp)
2403{
2404 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
2405
67f9fd91 2406 return do_read_cache_page(mapping, index, filler, NULL, gfp);
0531b2aa
LT
2407}
2408EXPORT_SYMBOL(read_cache_page_gfp);
2409
1da177e4
LT
2410/*
2411 * Performs necessary checks before doing a write
2412 *
485bb99b 2413 * Can adjust writing position or amount of bytes to write.
1da177e4
LT
2414 * Returns appropriate error code that caller should return or
2415 * zero in case that write should be allowed.
2416 */
3309dd04 2417inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
1da177e4 2418{
3309dd04 2419 struct file *file = iocb->ki_filp;
1da177e4 2420 struct inode *inode = file->f_mapping->host;
59e99e5b 2421 unsigned long limit = rlimit(RLIMIT_FSIZE);
3309dd04 2422 loff_t pos;
1da177e4 2423
3309dd04
AV
2424 if (!iov_iter_count(from))
2425 return 0;
1da177e4 2426
0fa6b005 2427 /* FIXME: this is for backwards compatibility with 2.4 */
2ba48ce5 2428 if (iocb->ki_flags & IOCB_APPEND)
3309dd04 2429 iocb->ki_pos = i_size_read(inode);
1da177e4 2430
3309dd04 2431 pos = iocb->ki_pos;
1da177e4 2432
0fa6b005 2433 if (limit != RLIM_INFINITY) {
3309dd04 2434 if (iocb->ki_pos >= limit) {
0fa6b005
AV
2435 send_sig(SIGXFSZ, current, 0);
2436 return -EFBIG;
1da177e4 2437 }
3309dd04 2438 iov_iter_truncate(from, limit - (unsigned long)pos);
1da177e4
LT
2439 }
2440
2441 /*
2442 * LFS rule
2443 */
3309dd04 2444 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
1da177e4 2445 !(file->f_flags & O_LARGEFILE))) {
3309dd04 2446 if (pos >= MAX_NON_LFS)
1da177e4 2447 return -EFBIG;
3309dd04 2448 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
1da177e4
LT
2449 }
2450
2451 /*
2452 * Are we about to exceed the fs block limit ?
2453 *
2454 * If we have written data it becomes a short write. If we have
2455 * exceeded without writing data we send a signal and return EFBIG.
2456 * Linus frestrict idea will clean these up nicely..
2457 */
3309dd04
AV
2458 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2459 return -EFBIG;
1da177e4 2460
3309dd04
AV
2461 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2462 return iov_iter_count(from);
1da177e4
LT
2463}
2464EXPORT_SYMBOL(generic_write_checks);
2465
afddba49
NP
2466int pagecache_write_begin(struct file *file, struct address_space *mapping,
2467 loff_t pos, unsigned len, unsigned flags,
2468 struct page **pagep, void **fsdata)
2469{
2470 const struct address_space_operations *aops = mapping->a_ops;
2471
4e02ed4b 2472 return aops->write_begin(file, mapping, pos, len, flags,
afddba49 2473 pagep, fsdata);
afddba49
NP
2474}
2475EXPORT_SYMBOL(pagecache_write_begin);
2476
2477int pagecache_write_end(struct file *file, struct address_space *mapping,
2478 loff_t pos, unsigned len, unsigned copied,
2479 struct page *page, void *fsdata)
2480{
2481 const struct address_space_operations *aops = mapping->a_ops;
afddba49 2482
4e02ed4b 2483 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
afddba49
NP
2484}
2485EXPORT_SYMBOL(pagecache_write_end);
2486
1da177e4 2487ssize_t
1af5bb49 2488generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
1da177e4
LT
2489{
2490 struct file *file = iocb->ki_filp;
2491 struct address_space *mapping = file->f_mapping;
2492 struct inode *inode = mapping->host;
1af5bb49 2493 loff_t pos = iocb->ki_pos;
1da177e4 2494 ssize_t written;
a969e903
CH
2495 size_t write_len;
2496 pgoff_t end;
26978b8b 2497 struct iov_iter data;
1da177e4 2498
0c949334 2499 write_len = iov_iter_count(from);
09cbfeaf 2500 end = (pos + write_len - 1) >> PAGE_SHIFT;
a969e903 2501
48b47c56 2502 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
a969e903
CH
2503 if (written)
2504 goto out;
2505
2506 /*
2507 * After a write we want buffered reads to be sure to go to disk to get
2508 * the new data. We invalidate clean cached page from the region we're
2509 * about to write. We do this *before* the write so that we can return
6ccfa806 2510 * without clobbering -EIOCBQUEUED from ->direct_IO().
a969e903
CH
2511 */
2512 if (mapping->nrpages) {
2513 written = invalidate_inode_pages2_range(mapping,
09cbfeaf 2514 pos >> PAGE_SHIFT, end);
6ccfa806
HH
2515 /*
2516 * If a page can not be invalidated, return 0 to fall back
2517 * to buffered write.
2518 */
2519 if (written) {
2520 if (written == -EBUSY)
2521 return 0;
a969e903 2522 goto out;
6ccfa806 2523 }
a969e903
CH
2524 }
2525
26978b8b 2526 data = *from;
c8b8e32d 2527 written = mapping->a_ops->direct_IO(iocb, &data);
a969e903
CH
2528
2529 /*
2530 * Finally, try again to invalidate clean pages which might have been
2531 * cached by non-direct readahead, or faulted in by get_user_pages()
2532 * if the source of the write was an mmap'ed region of the file
2533 * we're writing. Either one is a pretty crazy thing to do,
2534 * so we don't support it 100%. If this invalidation
2535 * fails, tough, the write still worked...
2536 */
2537 if (mapping->nrpages) {
2538 invalidate_inode_pages2_range(mapping,
09cbfeaf 2539 pos >> PAGE_SHIFT, end);
a969e903
CH
2540 }
2541
1da177e4 2542 if (written > 0) {
0116651c 2543 pos += written;
f8579f86 2544 iov_iter_advance(from, written);
0116651c
NK
2545 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2546 i_size_write(inode, pos);
1da177e4
LT
2547 mark_inode_dirty(inode);
2548 }
5cb6c6c7 2549 iocb->ki_pos = pos;
1da177e4 2550 }
a969e903 2551out:
1da177e4
LT
2552 return written;
2553}
2554EXPORT_SYMBOL(generic_file_direct_write);
2555
eb2be189
NP
2556/*
2557 * Find or create a page at the given pagecache position. Return the locked
2558 * page. This function is specifically for buffered writes.
2559 */
54566b2c
NP
2560struct page *grab_cache_page_write_begin(struct address_space *mapping,
2561 pgoff_t index, unsigned flags)
eb2be189 2562{
eb2be189 2563 struct page *page;
bbddabe2 2564 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
0faa70cb 2565
54566b2c 2566 if (flags & AOP_FLAG_NOFS)
2457aec6
MG
2567 fgp_flags |= FGP_NOFS;
2568
2569 page = pagecache_get_page(mapping, index, fgp_flags,
45f87de5 2570 mapping_gfp_mask(mapping));
c585a267 2571 if (page)
2457aec6 2572 wait_for_stable_page(page);
eb2be189 2573
eb2be189
NP
2574 return page;
2575}
54566b2c 2576EXPORT_SYMBOL(grab_cache_page_write_begin);
eb2be189 2577
3b93f911 2578ssize_t generic_perform_write(struct file *file,
afddba49
NP
2579 struct iov_iter *i, loff_t pos)
2580{
2581 struct address_space *mapping = file->f_mapping;
2582 const struct address_space_operations *a_ops = mapping->a_ops;
2583 long status = 0;
2584 ssize_t written = 0;
674b892e
NP
2585 unsigned int flags = 0;
2586
2587 /*
2588 * Copies from kernel address space cannot fail (NFSD is a big user).
2589 */
777eda2c 2590 if (!iter_is_iovec(i))
674b892e 2591 flags |= AOP_FLAG_UNINTERRUPTIBLE;
afddba49
NP
2592
2593 do {
2594 struct page *page;
afddba49
NP
2595 unsigned long offset; /* Offset into pagecache page */
2596 unsigned long bytes; /* Bytes to write to page */
2597 size_t copied; /* Bytes copied from user */
2598 void *fsdata;
2599
09cbfeaf
KS
2600 offset = (pos & (PAGE_SIZE - 1));
2601 bytes = min_t(unsigned long, PAGE_SIZE - offset,
afddba49
NP
2602 iov_iter_count(i));
2603
2604again:
00a3d660
LT
2605 /*
2606 * Bring in the user page that we will copy from _first_.
2607 * Otherwise there's a nasty deadlock on copying from the
2608 * same page as we're writing to, without it being marked
2609 * up-to-date.
2610 *
2611 * Not only is this an optimisation, but it is also required
2612 * to check that the address is actually valid, when atomic
2613 * usercopies are used, below.
2614 */
2615 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2616 status = -EFAULT;
2617 break;
2618 }
2619
296291cd
JK
2620 if (fatal_signal_pending(current)) {
2621 status = -EINTR;
2622 break;
2623 }
2624
674b892e 2625 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
afddba49 2626 &page, &fsdata);
2457aec6 2627 if (unlikely(status < 0))
afddba49
NP
2628 break;
2629
931e80e4 2630 if (mapping_writably_mapped(mapping))
2631 flush_dcache_page(page);
00a3d660 2632
afddba49 2633 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
afddba49
NP
2634 flush_dcache_page(page);
2635
2636 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2637 page, fsdata);
2638 if (unlikely(status < 0))
2639 break;
2640 copied = status;
2641
2642 cond_resched();
2643
124d3b70 2644 iov_iter_advance(i, copied);
afddba49
NP
2645 if (unlikely(copied == 0)) {
2646 /*
2647 * If we were unable to copy any data at all, we must
2648 * fall back to a single segment length write.
2649 *
2650 * If we didn't fallback here, we could livelock
2651 * because not all segments in the iov can be copied at
2652 * once without a pagefault.
2653 */
09cbfeaf 2654 bytes = min_t(unsigned long, PAGE_SIZE - offset,
afddba49
NP
2655 iov_iter_single_seg_count(i));
2656 goto again;
2657 }
afddba49
NP
2658 pos += copied;
2659 written += copied;
2660
2661 balance_dirty_pages_ratelimited(mapping);
afddba49
NP
2662 } while (iov_iter_count(i));
2663
2664 return written ? written : status;
2665}
3b93f911 2666EXPORT_SYMBOL(generic_perform_write);
1da177e4 2667
e4dd9de3 2668/**
8174202b 2669 * __generic_file_write_iter - write data to a file
e4dd9de3 2670 * @iocb: IO state structure (file, offset, etc.)
8174202b 2671 * @from: iov_iter with data to write
e4dd9de3
JK
2672 *
2673 * This function does all the work needed for actually writing data to a
2674 * file. It does all basic checks, removes SUID from the file, updates
2675 * modification times and calls proper subroutines depending on whether we
2676 * do direct IO or a standard buffered write.
2677 *
2678 * It expects i_mutex to be grabbed unless we work on a block device or similar
2679 * object which does not need locking at all.
2680 *
2681 * This function does *not* take care of syncing data in case of O_SYNC write.
2682 * A caller has to handle it. This is mainly due to the fact that we want to
2683 * avoid syncing under i_mutex.
2684 */
8174202b 2685ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4
LT
2686{
2687 struct file *file = iocb->ki_filp;
fb5527e6 2688 struct address_space * mapping = file->f_mapping;
1da177e4 2689 struct inode *inode = mapping->host;
3b93f911 2690 ssize_t written = 0;
1da177e4 2691 ssize_t err;
3b93f911 2692 ssize_t status;
1da177e4 2693
1da177e4 2694 /* We can write back this queue in page reclaim */
de1414a6 2695 current->backing_dev_info = inode_to_bdi(inode);
5fa8e0a1 2696 err = file_remove_privs(file);
1da177e4
LT
2697 if (err)
2698 goto out;
2699
c3b2da31
JB
2700 err = file_update_time(file);
2701 if (err)
2702 goto out;
1da177e4 2703
2ba48ce5 2704 if (iocb->ki_flags & IOCB_DIRECT) {
0b8def9d 2705 loff_t pos, endbyte;
fb5527e6 2706
1af5bb49 2707 written = generic_file_direct_write(iocb, from);
1da177e4 2708 /*
fbbbad4b
MW
2709 * If the write stopped short of completing, fall back to
2710 * buffered writes. Some filesystems do this for writes to
2711 * holes, for example. For DAX files, a buffered write will
2712 * not succeed (even if it did, DAX does not handle dirty
2713 * page-cache pages correctly).
1da177e4 2714 */
0b8def9d 2715 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
fbbbad4b
MW
2716 goto out;
2717
0b8def9d 2718 status = generic_perform_write(file, from, pos = iocb->ki_pos);
fb5527e6 2719 /*
3b93f911 2720 * If generic_perform_write() returned a synchronous error
fb5527e6
JM
2721 * then we want to return the number of bytes which were
2722 * direct-written, or the error code if that was zero. Note
2723 * that this differs from normal direct-io semantics, which
2724 * will return -EFOO even if some bytes were written.
2725 */
60bb4529 2726 if (unlikely(status < 0)) {
3b93f911 2727 err = status;
fb5527e6
JM
2728 goto out;
2729 }
fb5527e6
JM
2730 /*
2731 * We need to ensure that the page cache pages are written to
2732 * disk and invalidated to preserve the expected O_DIRECT
2733 * semantics.
2734 */
3b93f911 2735 endbyte = pos + status - 1;
0b8def9d 2736 err = filemap_write_and_wait_range(mapping, pos, endbyte);
fb5527e6 2737 if (err == 0) {
0b8def9d 2738 iocb->ki_pos = endbyte + 1;
3b93f911 2739 written += status;
fb5527e6 2740 invalidate_mapping_pages(mapping,
09cbfeaf
KS
2741 pos >> PAGE_SHIFT,
2742 endbyte >> PAGE_SHIFT);
fb5527e6
JM
2743 } else {
2744 /*
2745 * We don't know how much we wrote, so just return
2746 * the number of bytes which were direct-written
2747 */
2748 }
2749 } else {
0b8def9d
AV
2750 written = generic_perform_write(file, from, iocb->ki_pos);
2751 if (likely(written > 0))
2752 iocb->ki_pos += written;
fb5527e6 2753 }
1da177e4
LT
2754out:
2755 current->backing_dev_info = NULL;
2756 return written ? written : err;
2757}
8174202b 2758EXPORT_SYMBOL(__generic_file_write_iter);
e4dd9de3 2759
e4dd9de3 2760/**
8174202b 2761 * generic_file_write_iter - write data to a file
e4dd9de3 2762 * @iocb: IO state structure
8174202b 2763 * @from: iov_iter with data to write
e4dd9de3 2764 *
8174202b 2765 * This is a wrapper around __generic_file_write_iter() to be used by most
e4dd9de3
JK
2766 * filesystems. It takes care of syncing the file in case of O_SYNC file
2767 * and acquires i_mutex as needed.
2768 */
8174202b 2769ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4
LT
2770{
2771 struct file *file = iocb->ki_filp;
148f948b 2772 struct inode *inode = file->f_mapping->host;
1da177e4 2773 ssize_t ret;
1da177e4 2774
5955102c 2775 inode_lock(inode);
3309dd04
AV
2776 ret = generic_write_checks(iocb, from);
2777 if (ret > 0)
5f380c7f 2778 ret = __generic_file_write_iter(iocb, from);
5955102c 2779 inode_unlock(inode);
1da177e4 2780
e2592217
CH
2781 if (ret > 0)
2782 ret = generic_write_sync(iocb, ret);
1da177e4
LT
2783 return ret;
2784}
8174202b 2785EXPORT_SYMBOL(generic_file_write_iter);
1da177e4 2786
cf9a2ae8
DH
2787/**
2788 * try_to_release_page() - release old fs-specific metadata on a page
2789 *
2790 * @page: the page which the kernel is trying to free
2791 * @gfp_mask: memory allocation flags (and I/O mode)
2792 *
2793 * The address_space is to try to release any data against the page
2794 * (presumably at page->private). If the release was successful, return `1'.
2795 * Otherwise return zero.
2796 *
266cf658
DH
2797 * This may also be called if PG_fscache is set on a page, indicating that the
2798 * page is known to the local caching routines.
2799 *
cf9a2ae8 2800 * The @gfp_mask argument specifies whether I/O may be performed to release
71baba4b 2801 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
cf9a2ae8 2802 *
cf9a2ae8
DH
2803 */
2804int try_to_release_page(struct page *page, gfp_t gfp_mask)
2805{
2806 struct address_space * const mapping = page->mapping;
2807
2808 BUG_ON(!PageLocked(page));
2809 if (PageWriteback(page))
2810 return 0;
2811
2812 if (mapping && mapping->a_ops->releasepage)
2813 return mapping->a_ops->releasepage(page, gfp_mask);
2814 return try_to_free_buffers(page);
2815}
2816
2817EXPORT_SYMBOL(try_to_release_page);