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