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