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