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 | */ | |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/slab.h> | |
14 | #include <linux/compiler.h> | |
15 | #include <linux/fs.h> | |
c22ce143 | 16 | #include <linux/uaccess.h> |
1da177e4 | 17 | #include <linux/aio.h> |
c59ede7b | 18 | #include <linux/capability.h> |
1da177e4 LT |
19 | #include <linux/kernel_stat.h> |
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> | |
32 | #include <linux/syscalls.h> | |
44110fe3 | 33 | #include <linux/cpuset.h> |
2f718ffc | 34 | #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */ |
8a9f3ccd | 35 | #include <linux/memcontrol.h> |
4f98a2fe | 36 | #include <linux/mm_inline.h> /* for page_is_file_cache() */ |
0f8053a5 NP |
37 | #include "internal.h" |
38 | ||
1da177e4 | 39 | /* |
1da177e4 LT |
40 | * FIXME: remove all knowledge of the buffer layer from the core VM |
41 | */ | |
148f948b | 42 | #include <linux/buffer_head.h> /* for try_to_free_buffers */ |
1da177e4 | 43 | |
1da177e4 LT |
44 | #include <asm/mman.h> |
45 | ||
46 | /* | |
47 | * Shared mappings implemented 30.11.1994. It's not fully working yet, | |
48 | * though. | |
49 | * | |
50 | * Shared mappings now work. 15.8.1995 Bruno. | |
51 | * | |
52 | * finished 'unifying' the page and buffer cache and SMP-threaded the | |
53 | * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> | |
54 | * | |
55 | * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> | |
56 | */ | |
57 | ||
58 | /* | |
59 | * Lock ordering: | |
60 | * | |
25d9e2d1 | 61 | * ->i_mmap_lock (truncate_pagecache) |
1da177e4 | 62 | * ->private_lock (__free_pte->__set_page_dirty_buffers) |
5d337b91 HD |
63 | * ->swap_lock (exclusive_swap_page, others) |
64 | * ->mapping->tree_lock | |
1da177e4 | 65 | * |
1b1dcc1b | 66 | * ->i_mutex |
1da177e4 LT |
67 | * ->i_mmap_lock (truncate->unmap_mapping_range) |
68 | * | |
69 | * ->mmap_sem | |
70 | * ->i_mmap_lock | |
b8072f09 | 71 | * ->page_table_lock or pte_lock (various, mainly in memory.c) |
1da177e4 LT |
72 | * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) |
73 | * | |
74 | * ->mmap_sem | |
75 | * ->lock_page (access_process_vm) | |
76 | * | |
82591e6e NP |
77 | * ->i_mutex (generic_file_buffered_write) |
78 | * ->mmap_sem (fault_in_pages_readable->do_page_fault) | |
1da177e4 | 79 | * |
1b1dcc1b | 80 | * ->i_mutex |
1da177e4 LT |
81 | * ->i_alloc_sem (various) |
82 | * | |
83 | * ->inode_lock | |
84 | * ->sb_lock (fs/fs-writeback.c) | |
85 | * ->mapping->tree_lock (__sync_single_inode) | |
86 | * | |
87 | * ->i_mmap_lock | |
88 | * ->anon_vma.lock (vma_adjust) | |
89 | * | |
90 | * ->anon_vma.lock | |
b8072f09 | 91 | * ->page_table_lock or pte_lock (anon_vma_prepare and various) |
1da177e4 | 92 | * |
b8072f09 | 93 | * ->page_table_lock or pte_lock |
5d337b91 | 94 | * ->swap_lock (try_to_unmap_one) |
1da177e4 LT |
95 | * ->private_lock (try_to_unmap_one) |
96 | * ->tree_lock (try_to_unmap_one) | |
97 | * ->zone.lru_lock (follow_page->mark_page_accessed) | |
053837fc | 98 | * ->zone.lru_lock (check_pte_range->isolate_lru_page) |
1da177e4 LT |
99 | * ->private_lock (page_remove_rmap->set_page_dirty) |
100 | * ->tree_lock (page_remove_rmap->set_page_dirty) | |
101 | * ->inode_lock (page_remove_rmap->set_page_dirty) | |
102 | * ->inode_lock (zap_pte_range->set_page_dirty) | |
103 | * ->private_lock (zap_pte_range->__set_page_dirty_buffers) | |
104 | * | |
105 | * ->task->proc_lock | |
106 | * ->dcache_lock (proc_pid_lookup) | |
6a46079c AK |
107 | * |
108 | * (code doesn't rely on that order, so you could switch it around) | |
109 | * ->tasklist_lock (memory_failure, collect_procs_ao) | |
110 | * ->i_mmap_lock | |
1da177e4 LT |
111 | */ |
112 | ||
113 | /* | |
114 | * Remove a page from the page cache and free it. Caller has to make | |
115 | * sure the page is locked and that nobody else uses it - or that usage | |
19fd6231 | 116 | * is safe. The caller must hold the mapping's tree_lock. |
1da177e4 LT |
117 | */ |
118 | void __remove_from_page_cache(struct page *page) | |
119 | { | |
120 | struct address_space *mapping = page->mapping; | |
121 | ||
122 | radix_tree_delete(&mapping->page_tree, page->index); | |
123 | page->mapping = NULL; | |
124 | mapping->nrpages--; | |
347ce434 | 125 | __dec_zone_page_state(page, NR_FILE_PAGES); |
4b02108a KM |
126 | if (PageSwapBacked(page)) |
127 | __dec_zone_page_state(page, NR_SHMEM); | |
45426812 | 128 | BUG_ON(page_mapped(page)); |
3a692790 LT |
129 | |
130 | /* | |
131 | * Some filesystems seem to re-dirty the page even after | |
132 | * the VM has canceled the dirty bit (eg ext3 journaling). | |
133 | * | |
134 | * Fix it up by doing a final dirty accounting check after | |
135 | * having removed the page entirely. | |
136 | */ | |
137 | if (PageDirty(page) && mapping_cap_account_dirty(mapping)) { | |
138 | dec_zone_page_state(page, NR_FILE_DIRTY); | |
139 | dec_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE); | |
140 | } | |
1da177e4 LT |
141 | } |
142 | ||
143 | void remove_from_page_cache(struct page *page) | |
144 | { | |
145 | struct address_space *mapping = page->mapping; | |
146 | ||
cd7619d6 | 147 | BUG_ON(!PageLocked(page)); |
1da177e4 | 148 | |
19fd6231 | 149 | spin_lock_irq(&mapping->tree_lock); |
1da177e4 | 150 | __remove_from_page_cache(page); |
19fd6231 | 151 | spin_unlock_irq(&mapping->tree_lock); |
e767e056 | 152 | mem_cgroup_uncharge_cache_page(page); |
1da177e4 LT |
153 | } |
154 | ||
a3b3d134 | 155 | static int sync_page(void *word) |
1da177e4 LT |
156 | { |
157 | struct address_space *mapping; | |
158 | struct page *page; | |
159 | ||
07808b74 | 160 | page = container_of((unsigned long *)word, struct page, flags); |
1da177e4 LT |
161 | |
162 | /* | |
dd1d5afc WLII |
163 | * page_mapping() is being called without PG_locked held. |
164 | * Some knowledge of the state and use of the page is used to | |
165 | * reduce the requirements down to a memory barrier. | |
166 | * The danger here is of a stale page_mapping() return value | |
167 | * indicating a struct address_space different from the one it's | |
168 | * associated with when it is associated with one. | |
169 | * After smp_mb(), it's either the correct page_mapping() for | |
170 | * the page, or an old page_mapping() and the page's own | |
171 | * page_mapping() has gone NULL. | |
172 | * The ->sync_page() address_space operation must tolerate | |
173 | * page_mapping() going NULL. By an amazing coincidence, | |
174 | * this comes about because none of the users of the page | |
175 | * in the ->sync_page() methods make essential use of the | |
176 | * page_mapping(), merely passing the page down to the backing | |
177 | * device's unplug functions when it's non-NULL, which in turn | |
4c21e2f2 | 178 | * ignore it for all cases but swap, where only page_private(page) is |
dd1d5afc WLII |
179 | * of interest. When page_mapping() does go NULL, the entire |
180 | * call stack gracefully ignores the page and returns. | |
181 | * -- wli | |
1da177e4 LT |
182 | */ |
183 | smp_mb(); | |
184 | mapping = page_mapping(page); | |
185 | if (mapping && mapping->a_ops && mapping->a_ops->sync_page) | |
186 | mapping->a_ops->sync_page(page); | |
3001eabb | 187 | |
a3b3d134 JA |
188 | if (!in_aio(current)) |
189 | io_schedule(); | |
190 | ||
1da177e4 LT |
191 | return 0; |
192 | } | |
193 | ||
a3b3d134 | 194 | static int sync_page_killable(void *word) |
3001eabb | 195 | { |
a3b3d134 | 196 | int ret = sync_page(word); |
3001eabb | 197 | |
a3b3d134 JA |
198 | if (!ret && fatal_signal_pending(current)) |
199 | ret = -EINTR; | |
3001eabb | 200 | |
a3b3d134 | 201 | return ret; |
2687a356 MW |
202 | } |
203 | ||
1da177e4 | 204 | /** |
485bb99b | 205 | * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range |
67be2dd1 MW |
206 | * @mapping: address space structure to write |
207 | * @start: offset in bytes where the range starts | |
469eb4d0 | 208 | * @end: offset in bytes where the range ends (inclusive) |
67be2dd1 | 209 | * @sync_mode: enable synchronous operation |
1da177e4 | 210 | * |
485bb99b RD |
211 | * Start writeback against all of a mapping's dirty pages that lie |
212 | * within the byte offsets <start, end> inclusive. | |
213 | * | |
1da177e4 | 214 | * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as |
485bb99b | 215 | * opposed to a regular memory cleansing writeback. The difference between |
1da177e4 LT |
216 | * these two operations is that if a dirty page/buffer is encountered, it must |
217 | * be waited upon, and not just skipped over. | |
218 | */ | |
ebcf28e1 AM |
219 | int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
220 | loff_t end, int sync_mode) | |
1da177e4 LT |
221 | { |
222 | int ret; | |
223 | struct writeback_control wbc = { | |
224 | .sync_mode = sync_mode, | |
05fe478d | 225 | .nr_to_write = LONG_MAX, |
111ebb6e OH |
226 | .range_start = start, |
227 | .range_end = end, | |
1da177e4 LT |
228 | }; |
229 | ||
230 | if (!mapping_cap_writeback_dirty(mapping)) | |
231 | return 0; | |
232 | ||
233 | ret = do_writepages(mapping, &wbc); | |
234 | return ret; | |
235 | } | |
236 | ||
237 | static inline int __filemap_fdatawrite(struct address_space *mapping, | |
238 | int sync_mode) | |
239 | { | |
111ebb6e | 240 | return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode); |
1da177e4 LT |
241 | } |
242 | ||
243 | int filemap_fdatawrite(struct address_space *mapping) | |
244 | { | |
245 | return __filemap_fdatawrite(mapping, WB_SYNC_ALL); | |
246 | } | |
247 | EXPORT_SYMBOL(filemap_fdatawrite); | |
248 | ||
f4c0a0fd | 249 | int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
ebcf28e1 | 250 | loff_t end) |
1da177e4 LT |
251 | { |
252 | return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL); | |
253 | } | |
f4c0a0fd | 254 | EXPORT_SYMBOL(filemap_fdatawrite_range); |
1da177e4 | 255 | |
485bb99b RD |
256 | /** |
257 | * filemap_flush - mostly a non-blocking flush | |
258 | * @mapping: target address_space | |
259 | * | |
1da177e4 LT |
260 | * This is a mostly non-blocking flush. Not suitable for data-integrity |
261 | * purposes - I/O may not be started against all dirty pages. | |
262 | */ | |
263 | int filemap_flush(struct address_space *mapping) | |
264 | { | |
265 | return __filemap_fdatawrite(mapping, WB_SYNC_NONE); | |
266 | } | |
267 | EXPORT_SYMBOL(filemap_flush); | |
268 | ||
485bb99b RD |
269 | /** |
270 | * wait_on_page_writeback_range - wait for writeback to complete | |
271 | * @mapping: target address_space | |
272 | * @start: beginning page index | |
273 | * @end: ending page index | |
274 | * | |
1da177e4 LT |
275 | * Wait for writeback to complete against pages indexed by start->end |
276 | * inclusive | |
277 | */ | |
ebcf28e1 | 278 | int wait_on_page_writeback_range(struct address_space *mapping, |
1da177e4 LT |
279 | pgoff_t start, pgoff_t end) |
280 | { | |
281 | struct pagevec pvec; | |
282 | int nr_pages; | |
283 | int ret = 0; | |
284 | pgoff_t index; | |
285 | ||
286 | if (end < start) | |
287 | return 0; | |
288 | ||
289 | pagevec_init(&pvec, 0); | |
290 | index = start; | |
291 | while ((index <= end) && | |
292 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
293 | PAGECACHE_TAG_WRITEBACK, | |
294 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) { | |
295 | unsigned i; | |
296 | ||
297 | for (i = 0; i < nr_pages; i++) { | |
298 | struct page *page = pvec.pages[i]; | |
299 | ||
300 | /* until radix tree lookup accepts end_index */ | |
301 | if (page->index > end) | |
302 | continue; | |
303 | ||
304 | wait_on_page_writeback(page); | |
305 | if (PageError(page)) | |
306 | ret = -EIO; | |
307 | } | |
308 | pagevec_release(&pvec); | |
309 | cond_resched(); | |
310 | } | |
311 | ||
312 | /* Check for outstanding write errors */ | |
313 | if (test_and_clear_bit(AS_ENOSPC, &mapping->flags)) | |
314 | ret = -ENOSPC; | |
315 | if (test_and_clear_bit(AS_EIO, &mapping->flags)) | |
316 | ret = -EIO; | |
317 | ||
318 | return ret; | |
319 | } | |
320 | ||
d3bccb6f JK |
321 | /** |
322 | * filemap_fdatawait_range - wait for all under-writeback pages to complete in a given range | |
323 | * @mapping: address space structure to wait for | |
324 | * @start: offset in bytes where the range starts | |
325 | * @end: offset in bytes where the range ends (inclusive) | |
326 | * | |
327 | * Walk the list of under-writeback pages of the given address space | |
328 | * in the given range and wait for all of them. | |
329 | * | |
330 | * This is just a simple wrapper so that callers don't have to convert offsets | |
331 | * to page indexes themselves | |
332 | */ | |
333 | int filemap_fdatawait_range(struct address_space *mapping, loff_t start, | |
334 | loff_t end) | |
335 | { | |
336 | return wait_on_page_writeback_range(mapping, start >> PAGE_CACHE_SHIFT, | |
337 | end >> PAGE_CACHE_SHIFT); | |
338 | } | |
339 | EXPORT_SYMBOL(filemap_fdatawait_range); | |
340 | ||
1da177e4 | 341 | /** |
485bb99b | 342 | * filemap_fdatawait - wait for all under-writeback pages to complete |
1da177e4 | 343 | * @mapping: address space structure to wait for |
485bb99b RD |
344 | * |
345 | * Walk the list of under-writeback pages of the given address space | |
346 | * and wait for all of them. | |
1da177e4 LT |
347 | */ |
348 | int filemap_fdatawait(struct address_space *mapping) | |
349 | { | |
350 | loff_t i_size = i_size_read(mapping->host); | |
351 | ||
352 | if (i_size == 0) | |
353 | return 0; | |
354 | ||
355 | return wait_on_page_writeback_range(mapping, 0, | |
356 | (i_size - 1) >> PAGE_CACHE_SHIFT); | |
357 | } | |
358 | EXPORT_SYMBOL(filemap_fdatawait); | |
359 | ||
360 | int filemap_write_and_wait(struct address_space *mapping) | |
361 | { | |
28fd1298 | 362 | int err = 0; |
1da177e4 LT |
363 | |
364 | if (mapping->nrpages) { | |
28fd1298 OH |
365 | err = filemap_fdatawrite(mapping); |
366 | /* | |
367 | * Even if the above returned error, the pages may be | |
368 | * written partially (e.g. -ENOSPC), so we wait for it. | |
369 | * But the -EIO is special case, it may indicate the worst | |
370 | * thing (e.g. bug) happened, so we avoid waiting for it. | |
371 | */ | |
372 | if (err != -EIO) { | |
373 | int err2 = filemap_fdatawait(mapping); | |
374 | if (!err) | |
375 | err = err2; | |
376 | } | |
1da177e4 | 377 | } |
28fd1298 | 378 | return err; |
1da177e4 | 379 | } |
28fd1298 | 380 | EXPORT_SYMBOL(filemap_write_and_wait); |
1da177e4 | 381 | |
485bb99b RD |
382 | /** |
383 | * filemap_write_and_wait_range - write out & wait on a file range | |
384 | * @mapping: the address_space for the pages | |
385 | * @lstart: offset in bytes where the range starts | |
386 | * @lend: offset in bytes where the range ends (inclusive) | |
387 | * | |
469eb4d0 AM |
388 | * Write out and wait upon file offsets lstart->lend, inclusive. |
389 | * | |
390 | * Note that `lend' is inclusive (describes the last byte to be written) so | |
391 | * that this function can be used to write to the very end-of-file (end = -1). | |
392 | */ | |
1da177e4 LT |
393 | int filemap_write_and_wait_range(struct address_space *mapping, |
394 | loff_t lstart, loff_t lend) | |
395 | { | |
28fd1298 | 396 | int err = 0; |
1da177e4 LT |
397 | |
398 | if (mapping->nrpages) { | |
28fd1298 OH |
399 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
400 | WB_SYNC_ALL); | |
401 | /* See comment of filemap_write_and_wait() */ | |
402 | if (err != -EIO) { | |
403 | int err2 = wait_on_page_writeback_range(mapping, | |
404 | lstart >> PAGE_CACHE_SHIFT, | |
405 | lend >> PAGE_CACHE_SHIFT); | |
406 | if (!err) | |
407 | err = err2; | |
408 | } | |
1da177e4 | 409 | } |
28fd1298 | 410 | return err; |
1da177e4 | 411 | } |
f6995585 | 412 | EXPORT_SYMBOL(filemap_write_and_wait_range); |
1da177e4 | 413 | |
485bb99b | 414 | /** |
e286781d | 415 | * add_to_page_cache_locked - add a locked page to the pagecache |
485bb99b RD |
416 | * @page: page to add |
417 | * @mapping: the page's address_space | |
418 | * @offset: page index | |
419 | * @gfp_mask: page allocation mode | |
420 | * | |
e286781d | 421 | * This function is used to add a page to the pagecache. It must be locked. |
1da177e4 LT |
422 | * This function does not add the page to the LRU. The caller must do that. |
423 | */ | |
e286781d | 424 | int add_to_page_cache_locked(struct page *page, struct address_space *mapping, |
6daa0e28 | 425 | pgoff_t offset, gfp_t gfp_mask) |
1da177e4 | 426 | { |
e286781d NP |
427 | int error; |
428 | ||
429 | VM_BUG_ON(!PageLocked(page)); | |
430 | ||
431 | error = mem_cgroup_cache_charge(page, current->mm, | |
2c26fdd7 | 432 | gfp_mask & GFP_RECLAIM_MASK); |
35c754d7 BS |
433 | if (error) |
434 | goto out; | |
1da177e4 | 435 | |
35c754d7 | 436 | error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM); |
1da177e4 | 437 | if (error == 0) { |
e286781d NP |
438 | page_cache_get(page); |
439 | page->mapping = mapping; | |
440 | page->index = offset; | |
441 | ||
19fd6231 | 442 | spin_lock_irq(&mapping->tree_lock); |
1da177e4 | 443 | error = radix_tree_insert(&mapping->page_tree, offset, page); |
e286781d | 444 | if (likely(!error)) { |
1da177e4 | 445 | mapping->nrpages++; |
347ce434 | 446 | __inc_zone_page_state(page, NR_FILE_PAGES); |
4b02108a KM |
447 | if (PageSwapBacked(page)) |
448 | __inc_zone_page_state(page, NR_SHMEM); | |
e767e056 | 449 | spin_unlock_irq(&mapping->tree_lock); |
e286781d NP |
450 | } else { |
451 | page->mapping = NULL; | |
e767e056 | 452 | spin_unlock_irq(&mapping->tree_lock); |
69029cd5 | 453 | mem_cgroup_uncharge_cache_page(page); |
e286781d NP |
454 | page_cache_release(page); |
455 | } | |
1da177e4 | 456 | radix_tree_preload_end(); |
35c754d7 | 457 | } else |
69029cd5 | 458 | mem_cgroup_uncharge_cache_page(page); |
8a9f3ccd | 459 | out: |
1da177e4 LT |
460 | return error; |
461 | } | |
e286781d | 462 | EXPORT_SYMBOL(add_to_page_cache_locked); |
1da177e4 LT |
463 | |
464 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, | |
6daa0e28 | 465 | pgoff_t offset, gfp_t gfp_mask) |
1da177e4 | 466 | { |
4f98a2fe RR |
467 | int ret; |
468 | ||
469 | /* | |
470 | * Splice_read and readahead add shmem/tmpfs pages into the page cache | |
471 | * before shmem_readpage has a chance to mark them as SwapBacked: they | |
472 | * need to go on the active_anon lru below, and mem_cgroup_cache_charge | |
473 | * (called in add_to_page_cache) needs to know where they're going too. | |
474 | */ | |
475 | if (mapping_cap_swap_backed(mapping)) | |
476 | SetPageSwapBacked(page); | |
477 | ||
478 | ret = add_to_page_cache(page, mapping, offset, gfp_mask); | |
479 | if (ret == 0) { | |
480 | if (page_is_file_cache(page)) | |
481 | lru_cache_add_file(page); | |
482 | else | |
483 | lru_cache_add_active_anon(page); | |
484 | } | |
1da177e4 LT |
485 | return ret; |
486 | } | |
18bc0bbd | 487 | EXPORT_SYMBOL_GPL(add_to_page_cache_lru); |
1da177e4 | 488 | |
44110fe3 | 489 | #ifdef CONFIG_NUMA |
2ae88149 | 490 | struct page *__page_cache_alloc(gfp_t gfp) |
44110fe3 PJ |
491 | { |
492 | if (cpuset_do_page_mem_spread()) { | |
493 | int n = cpuset_mem_spread_node(); | |
6484eb3e | 494 | return alloc_pages_exact_node(n, gfp, 0); |
44110fe3 | 495 | } |
2ae88149 | 496 | return alloc_pages(gfp, 0); |
44110fe3 | 497 | } |
2ae88149 | 498 | EXPORT_SYMBOL(__page_cache_alloc); |
44110fe3 PJ |
499 | #endif |
500 | ||
db37648c NP |
501 | static int __sleep_on_page_lock(void *word) |
502 | { | |
503 | io_schedule(); | |
504 | return 0; | |
505 | } | |
506 | ||
1da177e4 LT |
507 | /* |
508 | * In order to wait for pages to become available there must be | |
509 | * waitqueues associated with pages. By using a hash table of | |
510 | * waitqueues where the bucket discipline is to maintain all | |
511 | * waiters on the same queue and wake all when any of the pages | |
512 | * become available, and for the woken contexts to check to be | |
513 | * sure the appropriate page became available, this saves space | |
514 | * at a cost of "thundering herd" phenomena during rare hash | |
515 | * collisions. | |
516 | */ | |
517 | static wait_queue_head_t *page_waitqueue(struct page *page) | |
518 | { | |
519 | const struct zone *zone = page_zone(page); | |
520 | ||
521 | return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)]; | |
522 | } | |
523 | ||
524 | static inline void wake_up_page(struct page *page, int bit) | |
525 | { | |
526 | __wake_up_bit(page_waitqueue(page), &page->flags, bit); | |
527 | } | |
528 | ||
f80e69e7 JA |
529 | int wait_on_page_bit_async(struct page *page, int bit_nr, |
530 | struct wait_bit_queue *wait) | |
3001eabb JA |
531 | { |
532 | int ret = 0; | |
533 | ||
534 | if (test_bit(bit_nr, &page->flags)) { | |
f80e69e7 | 535 | DEFINE_WAIT_BIT(stack_wait, &page->flags, bit_nr); |
3001eabb JA |
536 | int (*fn)(void *) = sync_page; |
537 | ||
f80e69e7 JA |
538 | if (!wait) { |
539 | fn = sync_page; | |
540 | wait = &stack_wait; | |
541 | } else { | |
a3b3d134 | 542 | fn = sync_page_killable; |
f80e69e7 JA |
543 | wait->key.flags = &page->flags; |
544 | wait->key.bit_nr = bit_nr; | |
3001eabb JA |
545 | } |
546 | ||
f80e69e7 | 547 | ret = __wait_on_bit(page_waitqueue(page), wait, fn, |
3001eabb JA |
548 | TASK_UNINTERRUPTIBLE); |
549 | } | |
550 | ||
551 | return ret; | |
552 | } | |
f80e69e7 | 553 | EXPORT_SYMBOL(wait_on_page_bit_async); |
3001eabb | 554 | |
920c7a5d | 555 | void wait_on_page_bit(struct page *page, int bit_nr) |
1da177e4 | 556 | { |
f80e69e7 | 557 | wait_on_page_bit_async(page, bit_nr, NULL); |
1da177e4 LT |
558 | } |
559 | EXPORT_SYMBOL(wait_on_page_bit); | |
560 | ||
3001eabb | 561 | |
385e1ca5 DH |
562 | /** |
563 | * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue | |
697f619f RD |
564 | * @page: Page defining the wait queue of interest |
565 | * @waiter: Waiter to add to the queue | |
385e1ca5 DH |
566 | * |
567 | * Add an arbitrary @waiter to the wait queue for the nominated @page. | |
568 | */ | |
569 | void add_page_wait_queue(struct page *page, wait_queue_t *waiter) | |
570 | { | |
571 | wait_queue_head_t *q = page_waitqueue(page); | |
572 | unsigned long flags; | |
573 | ||
574 | spin_lock_irqsave(&q->lock, flags); | |
575 | __add_wait_queue(q, waiter); | |
576 | spin_unlock_irqrestore(&q->lock, flags); | |
577 | } | |
578 | EXPORT_SYMBOL_GPL(add_page_wait_queue); | |
579 | ||
1da177e4 | 580 | /** |
485bb99b | 581 | * unlock_page - unlock a locked page |
1da177e4 LT |
582 | * @page: the page |
583 | * | |
584 | * Unlocks the page and wakes up sleepers in ___wait_on_page_locked(). | |
585 | * Also wakes sleepers in wait_on_page_writeback() because the wakeup | |
586 | * mechananism between PageLocked pages and PageWriteback pages is shared. | |
587 | * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. | |
588 | * | |
8413ac9d NP |
589 | * The mb is necessary to enforce ordering between the clear_bit and the read |
590 | * of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()). | |
1da177e4 | 591 | */ |
920c7a5d | 592 | void unlock_page(struct page *page) |
1da177e4 | 593 | { |
8413ac9d NP |
594 | VM_BUG_ON(!PageLocked(page)); |
595 | clear_bit_unlock(PG_locked, &page->flags); | |
596 | smp_mb__after_clear_bit(); | |
1da177e4 LT |
597 | wake_up_page(page, PG_locked); |
598 | } | |
599 | EXPORT_SYMBOL(unlock_page); | |
600 | ||
485bb99b RD |
601 | /** |
602 | * end_page_writeback - end writeback against a page | |
603 | * @page: the page | |
1da177e4 LT |
604 | */ |
605 | void end_page_writeback(struct page *page) | |
606 | { | |
ac6aadb2 MS |
607 | if (TestClearPageReclaim(page)) |
608 | rotate_reclaimable_page(page); | |
609 | ||
610 | if (!test_clear_page_writeback(page)) | |
611 | BUG(); | |
612 | ||
1da177e4 LT |
613 | smp_mb__after_clear_bit(); |
614 | wake_up_page(page, PG_writeback); | |
615 | } | |
616 | EXPORT_SYMBOL(end_page_writeback); | |
617 | ||
485bb99b | 618 | /** |
0dea3635 | 619 | * __lock_page_async - get a lock on the page, assuming we need to sleep to get it |
485bb99b | 620 | * @page: the page to lock |
1da177e4 | 621 | * |
485bb99b | 622 | * Ugly. Running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some |
1da177e4 LT |
623 | * random driver's requestfn sets TASK_RUNNING, we could busywait. However |
624 | * chances are that on the second loop, the block layer's plug list is empty, | |
625 | * so sync_page() will then return in state TASK_UNINTERRUPTIBLE. | |
626 | */ | |
a3b3d134 | 627 | int __lock_page_async(struct page *page, struct wait_bit_queue *wq) |
2687a356 | 628 | { |
81433ee6 | 629 | int (*fn)(void *) = sync_page; |
0dea3635 | 630 | |
81433ee6 | 631 | if (!is_sync_wait_bit_queue(wq)) |
0dea3635 | 632 | fn = sync_page_killable; |
2687a356 | 633 | |
0dea3635 | 634 | return __wait_on_bit_lock(page_waitqueue(page), wq, fn, |
a3b3d134 JA |
635 | TASK_UNINTERRUPTIBLE); |
636 | } | |
637 | EXPORT_SYMBOL(__lock_page_async); | |
2687a356 | 638 | |
7682486b RD |
639 | /** |
640 | * __lock_page_nosync - get a lock on the page, without calling sync_page() | |
641 | * @page: the page to lock | |
642 | * | |
db37648c NP |
643 | * Variant of lock_page that does not require the caller to hold a reference |
644 | * on the page's mapping. | |
645 | */ | |
920c7a5d | 646 | void __lock_page_nosync(struct page *page) |
db37648c NP |
647 | { |
648 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); | |
649 | __wait_on_bit_lock(page_waitqueue(page), &wait, __sleep_on_page_lock, | |
650 | TASK_UNINTERRUPTIBLE); | |
651 | } | |
652 | ||
485bb99b RD |
653 | /** |
654 | * find_get_page - find and get a page reference | |
655 | * @mapping: the address_space to search | |
656 | * @offset: the page index | |
657 | * | |
da6052f7 NP |
658 | * Is there a pagecache struct page at the given (mapping, offset) tuple? |
659 | * If yes, increment its refcount and return it; if no, return NULL. | |
1da177e4 | 660 | */ |
a60637c8 | 661 | struct page *find_get_page(struct address_space *mapping, pgoff_t offset) |
1da177e4 | 662 | { |
a60637c8 | 663 | void **pagep; |
1da177e4 LT |
664 | struct page *page; |
665 | ||
a60637c8 NP |
666 | rcu_read_lock(); |
667 | repeat: | |
668 | page = NULL; | |
669 | pagep = radix_tree_lookup_slot(&mapping->page_tree, offset); | |
670 | if (pagep) { | |
671 | page = radix_tree_deref_slot(pagep); | |
672 | if (unlikely(!page || page == RADIX_TREE_RETRY)) | |
673 | goto repeat; | |
674 | ||
675 | if (!page_cache_get_speculative(page)) | |
676 | goto repeat; | |
677 | ||
678 | /* | |
679 | * Has the page moved? | |
680 | * This is part of the lockless pagecache protocol. See | |
681 | * include/linux/pagemap.h for details. | |
682 | */ | |
683 | if (unlikely(page != *pagep)) { | |
684 | page_cache_release(page); | |
685 | goto repeat; | |
686 | } | |
687 | } | |
688 | rcu_read_unlock(); | |
689 | ||
1da177e4 LT |
690 | return page; |
691 | } | |
1da177e4 LT |
692 | EXPORT_SYMBOL(find_get_page); |
693 | ||
1da177e4 LT |
694 | /** |
695 | * find_lock_page - locate, pin and lock a pagecache page | |
67be2dd1 MW |
696 | * @mapping: the address_space to search |
697 | * @offset: the page index | |
1da177e4 LT |
698 | * |
699 | * Locates the desired pagecache page, locks it, increments its reference | |
700 | * count and returns its address. | |
701 | * | |
702 | * Returns zero if the page was not present. find_lock_page() may sleep. | |
703 | */ | |
a60637c8 | 704 | struct page *find_lock_page(struct address_space *mapping, pgoff_t offset) |
1da177e4 LT |
705 | { |
706 | struct page *page; | |
707 | ||
1da177e4 | 708 | repeat: |
a60637c8 | 709 | page = find_get_page(mapping, offset); |
1da177e4 | 710 | if (page) { |
a60637c8 NP |
711 | lock_page(page); |
712 | /* Has the page been truncated? */ | |
713 | if (unlikely(page->mapping != mapping)) { | |
714 | unlock_page(page); | |
715 | page_cache_release(page); | |
716 | goto repeat; | |
1da177e4 | 717 | } |
a60637c8 | 718 | VM_BUG_ON(page->index != offset); |
1da177e4 | 719 | } |
1da177e4 LT |
720 | return page; |
721 | } | |
1da177e4 LT |
722 | EXPORT_SYMBOL(find_lock_page); |
723 | ||
724 | /** | |
725 | * find_or_create_page - locate or add a pagecache page | |
67be2dd1 MW |
726 | * @mapping: the page's address_space |
727 | * @index: the page's index into the mapping | |
728 | * @gfp_mask: page allocation mode | |
1da177e4 LT |
729 | * |
730 | * Locates a page in the pagecache. If the page is not present, a new page | |
731 | * is allocated using @gfp_mask and is added to the pagecache and to the VM's | |
732 | * LRU list. The returned page is locked and has its reference count | |
733 | * incremented. | |
734 | * | |
735 | * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic | |
736 | * allocation! | |
737 | * | |
738 | * find_or_create_page() returns the desired page's address, or zero on | |
739 | * memory exhaustion. | |
740 | */ | |
741 | struct page *find_or_create_page(struct address_space *mapping, | |
57f6b96c | 742 | pgoff_t index, gfp_t gfp_mask) |
1da177e4 | 743 | { |
eb2be189 | 744 | struct page *page; |
1da177e4 LT |
745 | int err; |
746 | repeat: | |
747 | page = find_lock_page(mapping, index); | |
748 | if (!page) { | |
eb2be189 NP |
749 | page = __page_cache_alloc(gfp_mask); |
750 | if (!page) | |
751 | return NULL; | |
67d58ac4 NP |
752 | /* |
753 | * We want a regular kernel memory (not highmem or DMA etc) | |
754 | * allocation for the radix tree nodes, but we need to honour | |
755 | * the context-specific requirements the caller has asked for. | |
756 | * GFP_RECLAIM_MASK collects those requirements. | |
757 | */ | |
758 | err = add_to_page_cache_lru(page, mapping, index, | |
759 | (gfp_mask & GFP_RECLAIM_MASK)); | |
eb2be189 NP |
760 | if (unlikely(err)) { |
761 | page_cache_release(page); | |
762 | page = NULL; | |
763 | if (err == -EEXIST) | |
764 | goto repeat; | |
1da177e4 | 765 | } |
1da177e4 | 766 | } |
1da177e4 LT |
767 | return page; |
768 | } | |
1da177e4 LT |
769 | EXPORT_SYMBOL(find_or_create_page); |
770 | ||
771 | /** | |
772 | * find_get_pages - gang pagecache lookup | |
773 | * @mapping: The address_space to search | |
774 | * @start: The starting page index | |
775 | * @nr_pages: The maximum number of pages | |
776 | * @pages: Where the resulting pages are placed | |
777 | * | |
778 | * find_get_pages() will search for and return a group of up to | |
779 | * @nr_pages pages in the mapping. The pages are placed at @pages. | |
780 | * find_get_pages() takes a reference against the returned pages. | |
781 | * | |
782 | * The search returns a group of mapping-contiguous pages with ascending | |
783 | * indexes. There may be holes in the indices due to not-present pages. | |
784 | * | |
785 | * find_get_pages() returns the number of pages which were found. | |
786 | */ | |
787 | unsigned find_get_pages(struct address_space *mapping, pgoff_t start, | |
788 | unsigned int nr_pages, struct page **pages) | |
789 | { | |
790 | unsigned int i; | |
791 | unsigned int ret; | |
a60637c8 NP |
792 | unsigned int nr_found; |
793 | ||
794 | rcu_read_lock(); | |
795 | restart: | |
796 | nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, | |
797 | (void ***)pages, start, nr_pages); | |
798 | ret = 0; | |
799 | for (i = 0; i < nr_found; i++) { | |
800 | struct page *page; | |
801 | repeat: | |
802 | page = radix_tree_deref_slot((void **)pages[i]); | |
803 | if (unlikely(!page)) | |
804 | continue; | |
805 | /* | |
806 | * this can only trigger if nr_found == 1, making livelock | |
807 | * a non issue. | |
808 | */ | |
809 | if (unlikely(page == RADIX_TREE_RETRY)) | |
810 | goto restart; | |
811 | ||
812 | if (!page_cache_get_speculative(page)) | |
813 | goto repeat; | |
814 | ||
815 | /* Has the page moved? */ | |
816 | if (unlikely(page != *((void **)pages[i]))) { | |
817 | page_cache_release(page); | |
818 | goto repeat; | |
819 | } | |
1da177e4 | 820 | |
a60637c8 NP |
821 | pages[ret] = page; |
822 | ret++; | |
823 | } | |
824 | rcu_read_unlock(); | |
1da177e4 LT |
825 | return ret; |
826 | } | |
827 | ||
ebf43500 JA |
828 | /** |
829 | * find_get_pages_contig - gang contiguous pagecache lookup | |
830 | * @mapping: The address_space to search | |
831 | * @index: The starting page index | |
832 | * @nr_pages: The maximum number of pages | |
833 | * @pages: Where the resulting pages are placed | |
834 | * | |
835 | * find_get_pages_contig() works exactly like find_get_pages(), except | |
836 | * that the returned number of pages are guaranteed to be contiguous. | |
837 | * | |
838 | * find_get_pages_contig() returns the number of pages which were found. | |
839 | */ | |
840 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, | |
841 | unsigned int nr_pages, struct page **pages) | |
842 | { | |
843 | unsigned int i; | |
844 | unsigned int ret; | |
a60637c8 NP |
845 | unsigned int nr_found; |
846 | ||
847 | rcu_read_lock(); | |
848 | restart: | |
849 | nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, | |
850 | (void ***)pages, index, nr_pages); | |
851 | ret = 0; | |
852 | for (i = 0; i < nr_found; i++) { | |
853 | struct page *page; | |
854 | repeat: | |
855 | page = radix_tree_deref_slot((void **)pages[i]); | |
856 | if (unlikely(!page)) | |
857 | continue; | |
858 | /* | |
859 | * this can only trigger if nr_found == 1, making livelock | |
860 | * a non issue. | |
861 | */ | |
862 | if (unlikely(page == RADIX_TREE_RETRY)) | |
863 | goto restart; | |
ebf43500 | 864 | |
a60637c8 | 865 | if (page->mapping == NULL || page->index != index) |
ebf43500 JA |
866 | break; |
867 | ||
a60637c8 NP |
868 | if (!page_cache_get_speculative(page)) |
869 | goto repeat; | |
870 | ||
871 | /* Has the page moved? */ | |
872 | if (unlikely(page != *((void **)pages[i]))) { | |
873 | page_cache_release(page); | |
874 | goto repeat; | |
875 | } | |
876 | ||
877 | pages[ret] = page; | |
878 | ret++; | |
ebf43500 JA |
879 | index++; |
880 | } | |
a60637c8 NP |
881 | rcu_read_unlock(); |
882 | return ret; | |
ebf43500 | 883 | } |
ef71c15c | 884 | EXPORT_SYMBOL(find_get_pages_contig); |
ebf43500 | 885 | |
485bb99b RD |
886 | /** |
887 | * find_get_pages_tag - find and return pages that match @tag | |
888 | * @mapping: the address_space to search | |
889 | * @index: the starting page index | |
890 | * @tag: the tag index | |
891 | * @nr_pages: the maximum number of pages | |
892 | * @pages: where the resulting pages are placed | |
893 | * | |
1da177e4 | 894 | * Like find_get_pages, except we only return pages which are tagged with |
485bb99b | 895 | * @tag. We update @index to index the next page for the traversal. |
1da177e4 LT |
896 | */ |
897 | unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, | |
898 | int tag, unsigned int nr_pages, struct page **pages) | |
899 | { | |
900 | unsigned int i; | |
901 | unsigned int ret; | |
a60637c8 NP |
902 | unsigned int nr_found; |
903 | ||
904 | rcu_read_lock(); | |
905 | restart: | |
906 | nr_found = radix_tree_gang_lookup_tag_slot(&mapping->page_tree, | |
907 | (void ***)pages, *index, nr_pages, tag); | |
908 | ret = 0; | |
909 | for (i = 0; i < nr_found; i++) { | |
910 | struct page *page; | |
911 | repeat: | |
912 | page = radix_tree_deref_slot((void **)pages[i]); | |
913 | if (unlikely(!page)) | |
914 | continue; | |
915 | /* | |
916 | * this can only trigger if nr_found == 1, making livelock | |
917 | * a non issue. | |
918 | */ | |
919 | if (unlikely(page == RADIX_TREE_RETRY)) | |
920 | goto restart; | |
921 | ||
922 | if (!page_cache_get_speculative(page)) | |
923 | goto repeat; | |
924 | ||
925 | /* Has the page moved? */ | |
926 | if (unlikely(page != *((void **)pages[i]))) { | |
927 | page_cache_release(page); | |
928 | goto repeat; | |
929 | } | |
930 | ||
931 | pages[ret] = page; | |
932 | ret++; | |
933 | } | |
934 | rcu_read_unlock(); | |
1da177e4 | 935 | |
1da177e4 LT |
936 | if (ret) |
937 | *index = pages[ret - 1]->index + 1; | |
a60637c8 | 938 | |
1da177e4 LT |
939 | return ret; |
940 | } | |
ef71c15c | 941 | EXPORT_SYMBOL(find_get_pages_tag); |
1da177e4 | 942 | |
485bb99b RD |
943 | /** |
944 | * grab_cache_page_nowait - returns locked page at given index in given cache | |
945 | * @mapping: target address_space | |
946 | * @index: the page index | |
947 | * | |
72fd4a35 | 948 | * Same as grab_cache_page(), but do not wait if the page is unavailable. |
1da177e4 LT |
949 | * This is intended for speculative data generators, where the data can |
950 | * be regenerated if the page couldn't be grabbed. This routine should | |
951 | * be safe to call while holding the lock for another page. | |
952 | * | |
953 | * Clear __GFP_FS when allocating the page to avoid recursion into the fs | |
954 | * and deadlock against the caller's locked page. | |
955 | */ | |
956 | struct page * | |
57f6b96c | 957 | grab_cache_page_nowait(struct address_space *mapping, pgoff_t index) |
1da177e4 LT |
958 | { |
959 | struct page *page = find_get_page(mapping, index); | |
1da177e4 LT |
960 | |
961 | if (page) { | |
529ae9aa | 962 | if (trylock_page(page)) |
1da177e4 LT |
963 | return page; |
964 | page_cache_release(page); | |
965 | return NULL; | |
966 | } | |
2ae88149 | 967 | page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS); |
67d58ac4 | 968 | if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) { |
1da177e4 LT |
969 | page_cache_release(page); |
970 | page = NULL; | |
971 | } | |
972 | return page; | |
973 | } | |
1da177e4 LT |
974 | EXPORT_SYMBOL(grab_cache_page_nowait); |
975 | ||
76d42bd9 WF |
976 | /* |
977 | * CD/DVDs are error prone. When a medium error occurs, the driver may fail | |
978 | * a _large_ part of the i/o request. Imagine the worst scenario: | |
979 | * | |
980 | * ---R__________________________________________B__________ | |
981 | * ^ reading here ^ bad block(assume 4k) | |
982 | * | |
983 | * read(R) => miss => readahead(R...B) => media error => frustrating retries | |
984 | * => failing the whole request => read(R) => read(R+1) => | |
985 | * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) => | |
986 | * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) => | |
987 | * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ...... | |
988 | * | |
989 | * It is going insane. Fix it by quickly scaling down the readahead size. | |
990 | */ | |
991 | static void shrink_readahead_size_eio(struct file *filp, | |
992 | struct file_ra_state *ra) | |
993 | { | |
76d42bd9 | 994 | ra->ra_pages /= 4; |
76d42bd9 WF |
995 | } |
996 | ||
485bb99b | 997 | /** |
36e78914 | 998 | * do_generic_file_read - generic file read routine |
485bb99b RD |
999 | * @filp: the file to read |
1000 | * @ppos: current file position | |
1001 | * @desc: read_descriptor | |
1002 | * @actor: read method | |
1003 | * | |
1da177e4 | 1004 | * This is a generic file read routine, and uses the |
485bb99b | 1005 | * mapping->a_ops->readpage() function for the actual low-level stuff. |
1da177e4 LT |
1006 | * |
1007 | * This is really ugly. But the goto's actually try to clarify some | |
1008 | * of the logic when it comes to error handling etc. | |
1da177e4 | 1009 | */ |
36e78914 CH |
1010 | static void do_generic_file_read(struct file *filp, loff_t *ppos, |
1011 | read_descriptor_t *desc, read_actor_t actor) | |
1da177e4 | 1012 | { |
36e78914 | 1013 | struct address_space *mapping = filp->f_mapping; |
1da177e4 | 1014 | struct inode *inode = mapping->host; |
36e78914 | 1015 | struct file_ra_state *ra = &filp->f_ra; |
57f6b96c FW |
1016 | pgoff_t index; |
1017 | pgoff_t last_index; | |
1018 | pgoff_t prev_index; | |
1019 | unsigned long offset; /* offset into pagecache page */ | |
ec0f1637 | 1020 | unsigned int prev_offset; |
1da177e4 | 1021 | int error; |
1da177e4 | 1022 | |
1da177e4 | 1023 | index = *ppos >> PAGE_CACHE_SHIFT; |
7ff81078 FW |
1024 | prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT; |
1025 | prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1); | |
1da177e4 LT |
1026 | last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT; |
1027 | offset = *ppos & ~PAGE_CACHE_MASK; | |
1028 | ||
1da177e4 LT |
1029 | for (;;) { |
1030 | struct page *page; | |
57f6b96c | 1031 | pgoff_t end_index; |
a32ea1e1 | 1032 | loff_t isize; |
1da177e4 LT |
1033 | unsigned long nr, ret; |
1034 | ||
1da177e4 | 1035 | cond_resched(); |
1da177e4 LT |
1036 | find_page: |
1037 | page = find_get_page(mapping, index); | |
3ea89ee8 | 1038 | if (!page) { |
cf914a7d | 1039 | page_cache_sync_readahead(mapping, |
7ff81078 | 1040 | ra, filp, |
3ea89ee8 FW |
1041 | index, last_index - index); |
1042 | page = find_get_page(mapping, index); | |
1043 | if (unlikely(page == NULL)) | |
1044 | goto no_cached_page; | |
1045 | } | |
1046 | if (PageReadahead(page)) { | |
cf914a7d | 1047 | page_cache_async_readahead(mapping, |
7ff81078 | 1048 | ra, filp, page, |
3ea89ee8 | 1049 | index, last_index - index); |
1da177e4 | 1050 | } |
8ab22b9a HH |
1051 | if (!PageUptodate(page)) { |
1052 | if (inode->i_blkbits == PAGE_CACHE_SHIFT || | |
1053 | !mapping->a_ops->is_partially_uptodate) | |
1054 | goto page_not_up_to_date; | |
529ae9aa | 1055 | if (!trylock_page(page)) |
8ab22b9a HH |
1056 | goto page_not_up_to_date; |
1057 | if (!mapping->a_ops->is_partially_uptodate(page, | |
1058 | desc, offset)) | |
1059 | goto page_not_up_to_date_locked; | |
1060 | unlock_page(page); | |
1061 | } | |
1da177e4 | 1062 | page_ok: |
a32ea1e1 N |
1063 | /* |
1064 | * i_size must be checked after we know the page is Uptodate. | |
1065 | * | |
1066 | * Checking i_size after the check allows us to calculate | |
1067 | * the correct value for "nr", which means the zero-filled | |
1068 | * part of the page is not copied back to userspace (unless | |
1069 | * another truncate extends the file - this is desired though). | |
1070 | */ | |
1071 | ||
1072 | isize = i_size_read(inode); | |
1073 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; | |
1074 | if (unlikely(!isize || index > end_index)) { | |
1075 | page_cache_release(page); | |
1076 | goto out; | |
1077 | } | |
1078 | ||
1079 | /* nr is the maximum number of bytes to copy from this page */ | |
1080 | nr = PAGE_CACHE_SIZE; | |
1081 | if (index == end_index) { | |
1082 | nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; | |
1083 | if (nr <= offset) { | |
1084 | page_cache_release(page); | |
1085 | goto out; | |
1086 | } | |
1087 | } | |
1088 | nr = nr - offset; | |
1da177e4 LT |
1089 | |
1090 | /* If users can be writing to this page using arbitrary | |
1091 | * virtual addresses, take care about potential aliasing | |
1092 | * before reading the page on the kernel side. | |
1093 | */ | |
1094 | if (mapping_writably_mapped(mapping)) | |
1095 | flush_dcache_page(page); | |
1096 | ||
1097 | /* | |
ec0f1637 JK |
1098 | * When a sequential read accesses a page several times, |
1099 | * only mark it as accessed the first time. | |
1da177e4 | 1100 | */ |
ec0f1637 | 1101 | if (prev_index != index || offset != prev_offset) |
1da177e4 LT |
1102 | mark_page_accessed(page); |
1103 | prev_index = index; | |
1104 | ||
1105 | /* | |
1106 | * Ok, we have the page, and it's up-to-date, so | |
1107 | * now we can copy it to user space... | |
1108 | * | |
1109 | * The actor routine returns how many bytes were actually used.. | |
1110 | * NOTE! This may not be the same as how much of a user buffer | |
1111 | * we filled up (we may be padding etc), so we can only update | |
1112 | * "pos" here (the actor routine has to update the user buffer | |
1113 | * pointers and the remaining count). | |
1114 | */ | |
1115 | ret = actor(desc, page, offset, nr); | |
1116 | offset += ret; | |
1117 | index += offset >> PAGE_CACHE_SHIFT; | |
1118 | offset &= ~PAGE_CACHE_MASK; | |
6ce745ed | 1119 | prev_offset = offset; |
1da177e4 LT |
1120 | |
1121 | page_cache_release(page); | |
1122 | if (ret == nr && desc->count) | |
1123 | continue; | |
1124 | goto out; | |
1125 | ||
1126 | page_not_up_to_date: | |
1127 | /* Get exclusive access to the page ... */ | |
81433ee6 | 1128 | error = lock_page_async(page, current->io_wait); |
85462323 ON |
1129 | if (unlikely(error)) |
1130 | goto readpage_error; | |
1da177e4 | 1131 | |
8ab22b9a | 1132 | page_not_up_to_date_locked: |
da6052f7 | 1133 | /* Did it get truncated before we got the lock? */ |
1da177e4 LT |
1134 | if (!page->mapping) { |
1135 | unlock_page(page); | |
1136 | page_cache_release(page); | |
1137 | continue; | |
1138 | } | |
1139 | ||
1140 | /* Did somebody else fill it already? */ | |
1141 | if (PageUptodate(page)) { | |
1142 | unlock_page(page); | |
1143 | goto page_ok; | |
1144 | } | |
1145 | ||
1146 | readpage: | |
1147 | /* Start the actual read. The read will unlock the page. */ | |
1148 | error = mapping->a_ops->readpage(filp, page); | |
1149 | ||
994fc28c ZB |
1150 | if (unlikely(error)) { |
1151 | if (error == AOP_TRUNCATED_PAGE) { | |
1152 | page_cache_release(page); | |
1153 | goto find_page; | |
1154 | } | |
1da177e4 | 1155 | goto readpage_error; |
994fc28c | 1156 | } |
1da177e4 LT |
1157 | |
1158 | if (!PageUptodate(page)) { | |
81433ee6 | 1159 | error = lock_page_async(page, current->io_wait); |
85462323 ON |
1160 | if (unlikely(error)) |
1161 | goto readpage_error; | |
1da177e4 LT |
1162 | if (!PageUptodate(page)) { |
1163 | if (page->mapping == NULL) { | |
1164 | /* | |
1165 | * invalidate_inode_pages got it | |
1166 | */ | |
1167 | unlock_page(page); | |
1168 | page_cache_release(page); | |
1169 | goto find_page; | |
1170 | } | |
1171 | unlock_page(page); | |
7ff81078 | 1172 | shrink_readahead_size_eio(filp, ra); |
85462323 ON |
1173 | error = -EIO; |
1174 | goto readpage_error; | |
1da177e4 LT |
1175 | } |
1176 | unlock_page(page); | |
1177 | } | |
1178 | ||
1da177e4 LT |
1179 | goto page_ok; |
1180 | ||
1181 | readpage_error: | |
1182 | /* UHHUH! A synchronous read error occurred. Report it */ | |
1183 | desc->error = error; | |
1184 | page_cache_release(page); | |
1185 | goto out; | |
1186 | ||
1187 | no_cached_page: | |
1188 | /* | |
1189 | * Ok, it wasn't cached, so we need to create a new | |
1190 | * page.. | |
1191 | */ | |
eb2be189 NP |
1192 | page = page_cache_alloc_cold(mapping); |
1193 | if (!page) { | |
1194 | desc->error = -ENOMEM; | |
1195 | goto out; | |
1da177e4 | 1196 | } |
eb2be189 | 1197 | error = add_to_page_cache_lru(page, mapping, |
1da177e4 LT |
1198 | index, GFP_KERNEL); |
1199 | if (error) { | |
eb2be189 | 1200 | page_cache_release(page); |
1da177e4 LT |
1201 | if (error == -EEXIST) |
1202 | goto find_page; | |
1203 | desc->error = error; | |
1204 | goto out; | |
1205 | } | |
1da177e4 LT |
1206 | goto readpage; |
1207 | } | |
1208 | ||
1209 | out: | |
7ff81078 FW |
1210 | ra->prev_pos = prev_index; |
1211 | ra->prev_pos <<= PAGE_CACHE_SHIFT; | |
1212 | ra->prev_pos |= prev_offset; | |
1da177e4 | 1213 | |
f4e6b498 | 1214 | *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset; |
0c6aa263 | 1215 | file_accessed(filp); |
1da177e4 | 1216 | } |
1da177e4 LT |
1217 | |
1218 | int file_read_actor(read_descriptor_t *desc, struct page *page, | |
1219 | unsigned long offset, unsigned long size) | |
1220 | { | |
1221 | char *kaddr; | |
1222 | unsigned long left, count = desc->count; | |
1223 | ||
1224 | if (size > count) | |
1225 | size = count; | |
1226 | ||
1227 | /* | |
1228 | * Faults on the destination of a read are common, so do it before | |
1229 | * taking the kmap. | |
1230 | */ | |
1231 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { | |
1232 | kaddr = kmap_atomic(page, KM_USER0); | |
1233 | left = __copy_to_user_inatomic(desc->arg.buf, | |
1234 | kaddr + offset, size); | |
1235 | kunmap_atomic(kaddr, KM_USER0); | |
1236 | if (left == 0) | |
1237 | goto success; | |
1238 | } | |
1239 | ||
1240 | /* Do it the slow way */ | |
1241 | kaddr = kmap(page); | |
1242 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); | |
1243 | kunmap(page); | |
1244 | ||
1245 | if (left) { | |
1246 | size -= left; | |
1247 | desc->error = -EFAULT; | |
1248 | } | |
1249 | success: | |
1250 | desc->count = count - size; | |
1251 | desc->written += size; | |
1252 | desc->arg.buf += size; | |
1253 | return size; | |
1254 | } | |
1255 | ||
0ceb3314 DM |
1256 | /* |
1257 | * Performs necessary checks before doing a write | |
1258 | * @iov: io vector request | |
1259 | * @nr_segs: number of segments in the iovec | |
1260 | * @count: number of bytes to write | |
1261 | * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE | |
1262 | * | |
1263 | * Adjust number of segments and amount of bytes to write (nr_segs should be | |
1264 | * properly initialized first). Returns appropriate error code that caller | |
1265 | * should return or zero in case that write should be allowed. | |
1266 | */ | |
1267 | int generic_segment_checks(const struct iovec *iov, | |
1268 | unsigned long *nr_segs, size_t *count, int access_flags) | |
1269 | { | |
1270 | unsigned long seg; | |
1271 | size_t cnt = 0; | |
1272 | for (seg = 0; seg < *nr_segs; seg++) { | |
1273 | const struct iovec *iv = &iov[seg]; | |
1274 | ||
1275 | /* | |
1276 | * If any segment has a negative length, or the cumulative | |
1277 | * length ever wraps negative then return -EINVAL. | |
1278 | */ | |
1279 | cnt += iv->iov_len; | |
1280 | if (unlikely((ssize_t)(cnt|iv->iov_len) < 0)) | |
1281 | return -EINVAL; | |
1282 | if (access_ok(access_flags, iv->iov_base, iv->iov_len)) | |
1283 | continue; | |
1284 | if (seg == 0) | |
1285 | return -EFAULT; | |
1286 | *nr_segs = seg; | |
1287 | cnt -= iv->iov_len; /* This segment is no good */ | |
1288 | break; | |
1289 | } | |
1290 | *count = cnt; | |
1291 | return 0; | |
1292 | } | |
1293 | EXPORT_SYMBOL(generic_segment_checks); | |
1294 | ||
485bb99b | 1295 | /** |
b2abacf3 | 1296 | * generic_file_aio_read - generic filesystem read routine |
485bb99b RD |
1297 | * @iocb: kernel I/O control block |
1298 | * @iov: io vector request | |
1299 | * @nr_segs: number of segments in the iovec | |
b2abacf3 | 1300 | * @pos: current file position |
485bb99b | 1301 | * |
1da177e4 LT |
1302 | * This is the "read()" routine for all filesystems |
1303 | * that can use the page cache directly. | |
1304 | */ | |
1305 | ssize_t | |
543ade1f BP |
1306 | generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
1307 | unsigned long nr_segs, loff_t pos) | |
1da177e4 LT |
1308 | { |
1309 | struct file *filp = iocb->ki_filp; | |
1310 | ssize_t retval; | |
1311 | unsigned long seg; | |
1312 | size_t count; | |
543ade1f | 1313 | loff_t *ppos = &iocb->ki_pos; |
1da177e4 LT |
1314 | |
1315 | count = 0; | |
0ceb3314 DM |
1316 | retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); |
1317 | if (retval) | |
1318 | return retval; | |
1da177e4 LT |
1319 | |
1320 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ | |
1321 | if (filp->f_flags & O_DIRECT) { | |
543ade1f | 1322 | loff_t size; |
1da177e4 LT |
1323 | struct address_space *mapping; |
1324 | struct inode *inode; | |
1325 | ||
1326 | mapping = filp->f_mapping; | |
1327 | inode = mapping->host; | |
1da177e4 LT |
1328 | if (!count) |
1329 | goto out; /* skip atime */ | |
1330 | size = i_size_read(inode); | |
1331 | if (pos < size) { | |
48b47c56 NP |
1332 | retval = filemap_write_and_wait_range(mapping, pos, |
1333 | pos + iov_length(iov, nr_segs) - 1); | |
a969e903 CH |
1334 | if (!retval) { |
1335 | retval = mapping->a_ops->direct_IO(READ, iocb, | |
1336 | iov, pos, nr_segs); | |
1337 | } | |
1da177e4 LT |
1338 | if (retval > 0) |
1339 | *ppos = pos + retval; | |
11fa977e HD |
1340 | if (retval) { |
1341 | file_accessed(filp); | |
1342 | goto out; | |
1343 | } | |
0e0bcae3 | 1344 | } |
1da177e4 LT |
1345 | } |
1346 | ||
11fa977e HD |
1347 | for (seg = 0; seg < nr_segs; seg++) { |
1348 | read_descriptor_t desc; | |
1da177e4 | 1349 | |
11fa977e HD |
1350 | desc.written = 0; |
1351 | desc.arg.buf = iov[seg].iov_base; | |
1352 | desc.count = iov[seg].iov_len; | |
1353 | if (desc.count == 0) | |
1354 | continue; | |
1355 | desc.error = 0; | |
1356 | do_generic_file_read(filp, ppos, &desc, file_read_actor); | |
1357 | retval += desc.written; | |
1358 | if (desc.error) { | |
1359 | retval = retval ?: desc.error; | |
1360 | break; | |
1da177e4 | 1361 | } |
11fa977e HD |
1362 | if (desc.count > 0) |
1363 | break; | |
1da177e4 LT |
1364 | } |
1365 | out: | |
1366 | return retval; | |
1367 | } | |
1da177e4 LT |
1368 | EXPORT_SYMBOL(generic_file_aio_read); |
1369 | ||
1da177e4 LT |
1370 | static ssize_t |
1371 | do_readahead(struct address_space *mapping, struct file *filp, | |
57f6b96c | 1372 | pgoff_t index, unsigned long nr) |
1da177e4 LT |
1373 | { |
1374 | if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage) | |
1375 | return -EINVAL; | |
1376 | ||
f7e839dd | 1377 | force_page_cache_readahead(mapping, filp, index, nr); |
1da177e4 LT |
1378 | return 0; |
1379 | } | |
1380 | ||
6673e0c3 | 1381 | SYSCALL_DEFINE(readahead)(int fd, loff_t offset, size_t count) |
1da177e4 LT |
1382 | { |
1383 | ssize_t ret; | |
1384 | struct file *file; | |
1385 | ||
1386 | ret = -EBADF; | |
1387 | file = fget(fd); | |
1388 | if (file) { | |
1389 | if (file->f_mode & FMODE_READ) { | |
1390 | struct address_space *mapping = file->f_mapping; | |
57f6b96c FW |
1391 | pgoff_t start = offset >> PAGE_CACHE_SHIFT; |
1392 | pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT; | |
1da177e4 LT |
1393 | unsigned long len = end - start + 1; |
1394 | ret = do_readahead(mapping, file, start, len); | |
1395 | } | |
1396 | fput(file); | |
1397 | } | |
1398 | return ret; | |
1399 | } | |
6673e0c3 HC |
1400 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
1401 | asmlinkage long SyS_readahead(long fd, loff_t offset, long count) | |
1402 | { | |
1403 | return SYSC_readahead((int) fd, offset, (size_t) count); | |
1404 | } | |
1405 | SYSCALL_ALIAS(sys_readahead, SyS_readahead); | |
1406 | #endif | |
1da177e4 LT |
1407 | |
1408 | #ifdef CONFIG_MMU | |
485bb99b RD |
1409 | /** |
1410 | * page_cache_read - adds requested page to the page cache if not already there | |
1411 | * @file: file to read | |
1412 | * @offset: page index | |
1413 | * | |
1da177e4 LT |
1414 | * This adds the requested page to the page cache if it isn't already there, |
1415 | * and schedules an I/O to read in its contents from disk. | |
1416 | */ | |
920c7a5d | 1417 | static int page_cache_read(struct file *file, pgoff_t offset) |
1da177e4 LT |
1418 | { |
1419 | struct address_space *mapping = file->f_mapping; | |
1420 | struct page *page; | |
994fc28c | 1421 | int ret; |
1da177e4 | 1422 | |
994fc28c ZB |
1423 | do { |
1424 | page = page_cache_alloc_cold(mapping); | |
1425 | if (!page) | |
1426 | return -ENOMEM; | |
1427 | ||
1428 | ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL); | |
1429 | if (ret == 0) | |
1430 | ret = mapping->a_ops->readpage(file, page); | |
1431 | else if (ret == -EEXIST) | |
1432 | ret = 0; /* losing race to add is OK */ | |
1da177e4 | 1433 | |
1da177e4 | 1434 | page_cache_release(page); |
1da177e4 | 1435 | |
994fc28c ZB |
1436 | } while (ret == AOP_TRUNCATED_PAGE); |
1437 | ||
1438 | return ret; | |
1da177e4 LT |
1439 | } |
1440 | ||
1441 | #define MMAP_LOTSAMISS (100) | |
1442 | ||
ef00e08e LT |
1443 | /* |
1444 | * Synchronous readahead happens when we don't even find | |
1445 | * a page in the page cache at all. | |
1446 | */ | |
1447 | static void do_sync_mmap_readahead(struct vm_area_struct *vma, | |
1448 | struct file_ra_state *ra, | |
1449 | struct file *file, | |
1450 | pgoff_t offset) | |
1451 | { | |
1452 | unsigned long ra_pages; | |
1453 | struct address_space *mapping = file->f_mapping; | |
1454 | ||
1455 | /* If we don't want any read-ahead, don't bother */ | |
1456 | if (VM_RandomReadHint(vma)) | |
1457 | return; | |
1458 | ||
70ac23cf WF |
1459 | if (VM_SequentialReadHint(vma) || |
1460 | offset - 1 == (ra->prev_pos >> PAGE_CACHE_SHIFT)) { | |
7ffc59b4 WF |
1461 | page_cache_sync_readahead(mapping, ra, file, offset, |
1462 | ra->ra_pages); | |
ef00e08e LT |
1463 | return; |
1464 | } | |
1465 | ||
1466 | if (ra->mmap_miss < INT_MAX) | |
1467 | ra->mmap_miss++; | |
1468 | ||
1469 | /* | |
1470 | * Do we miss much more than hit in this file? If so, | |
1471 | * stop bothering with read-ahead. It will only hurt. | |
1472 | */ | |
1473 | if (ra->mmap_miss > MMAP_LOTSAMISS) | |
1474 | return; | |
1475 | ||
d30a1100 WF |
1476 | /* |
1477 | * mmap read-around | |
1478 | */ | |
ef00e08e LT |
1479 | ra_pages = max_sane_readahead(ra->ra_pages); |
1480 | if (ra_pages) { | |
d30a1100 WF |
1481 | ra->start = max_t(long, 0, offset - ra_pages/2); |
1482 | ra->size = ra_pages; | |
1483 | ra->async_size = 0; | |
1484 | ra_submit(ra, mapping, file); | |
ef00e08e LT |
1485 | } |
1486 | } | |
1487 | ||
1488 | /* | |
1489 | * Asynchronous readahead happens when we find the page and PG_readahead, | |
1490 | * so we want to possibly extend the readahead further.. | |
1491 | */ | |
1492 | static void do_async_mmap_readahead(struct vm_area_struct *vma, | |
1493 | struct file_ra_state *ra, | |
1494 | struct file *file, | |
1495 | struct page *page, | |
1496 | pgoff_t offset) | |
1497 | { | |
1498 | struct address_space *mapping = file->f_mapping; | |
1499 | ||
1500 | /* If we don't want any read-ahead, don't bother */ | |
1501 | if (VM_RandomReadHint(vma)) | |
1502 | return; | |
1503 | if (ra->mmap_miss > 0) | |
1504 | ra->mmap_miss--; | |
1505 | if (PageReadahead(page)) | |
2fad6f5d WF |
1506 | page_cache_async_readahead(mapping, ra, file, |
1507 | page, offset, ra->ra_pages); | |
ef00e08e LT |
1508 | } |
1509 | ||
485bb99b | 1510 | /** |
54cb8821 | 1511 | * filemap_fault - read in file data for page fault handling |
d0217ac0 NP |
1512 | * @vma: vma in which the fault was taken |
1513 | * @vmf: struct vm_fault containing details of the fault | |
485bb99b | 1514 | * |
54cb8821 | 1515 | * filemap_fault() is invoked via the vma operations vector for a |
1da177e4 LT |
1516 | * mapped memory region to read in file data during a page fault. |
1517 | * | |
1518 | * The goto's are kind of ugly, but this streamlines the normal case of having | |
1519 | * it in the page cache, and handles the special cases reasonably without | |
1520 | * having a lot of duplicated code. | |
1521 | */ | |
d0217ac0 | 1522 | int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 LT |
1523 | { |
1524 | int error; | |
54cb8821 | 1525 | struct file *file = vma->vm_file; |
1da177e4 LT |
1526 | struct address_space *mapping = file->f_mapping; |
1527 | struct file_ra_state *ra = &file->f_ra; | |
1528 | struct inode *inode = mapping->host; | |
ef00e08e | 1529 | pgoff_t offset = vmf->pgoff; |
1da177e4 | 1530 | struct page *page; |
2004dc8e | 1531 | pgoff_t size; |
83c54070 | 1532 | int ret = 0; |
1da177e4 | 1533 | |
1da177e4 | 1534 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
ef00e08e | 1535 | if (offset >= size) |
5307cc1a | 1536 | return VM_FAULT_SIGBUS; |
1da177e4 | 1537 | |
1da177e4 LT |
1538 | /* |
1539 | * Do we have something in the page cache already? | |
1540 | */ | |
ef00e08e LT |
1541 | page = find_get_page(mapping, offset); |
1542 | if (likely(page)) { | |
1da177e4 | 1543 | /* |
ef00e08e LT |
1544 | * We found the page, so try async readahead before |
1545 | * waiting for the lock. | |
1da177e4 | 1546 | */ |
ef00e08e LT |
1547 | do_async_mmap_readahead(vma, ra, file, page, offset); |
1548 | lock_page(page); | |
1da177e4 | 1549 | |
ef00e08e LT |
1550 | /* Did it get truncated? */ |
1551 | if (unlikely(page->mapping != mapping)) { | |
1552 | unlock_page(page); | |
1553 | put_page(page); | |
1554 | goto no_cached_page; | |
1da177e4 | 1555 | } |
ef00e08e LT |
1556 | } else { |
1557 | /* No page in the page cache at all */ | |
1558 | do_sync_mmap_readahead(vma, ra, file, offset); | |
1559 | count_vm_event(PGMAJFAULT); | |
1560 | ret = VM_FAULT_MAJOR; | |
1561 | retry_find: | |
1562 | page = find_lock_page(mapping, offset); | |
1da177e4 LT |
1563 | if (!page) |
1564 | goto no_cached_page; | |
1565 | } | |
1566 | ||
1da177e4 | 1567 | /* |
d00806b1 NP |
1568 | * We have a locked page in the page cache, now we need to check |
1569 | * that it's up-to-date. If not, it is going to be due to an error. | |
1da177e4 | 1570 | */ |
d00806b1 | 1571 | if (unlikely(!PageUptodate(page))) |
1da177e4 LT |
1572 | goto page_not_uptodate; |
1573 | ||
ef00e08e LT |
1574 | /* |
1575 | * Found the page and have a reference on it. | |
1576 | * We must recheck i_size under page lock. | |
1577 | */ | |
d00806b1 | 1578 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
ef00e08e | 1579 | if (unlikely(offset >= size)) { |
d00806b1 | 1580 | unlock_page(page); |
745ad48e | 1581 | page_cache_release(page); |
5307cc1a | 1582 | return VM_FAULT_SIGBUS; |
d00806b1 NP |
1583 | } |
1584 | ||
ef00e08e | 1585 | ra->prev_pos = (loff_t)offset << PAGE_CACHE_SHIFT; |
d0217ac0 | 1586 | vmf->page = page; |
83c54070 | 1587 | return ret | VM_FAULT_LOCKED; |
1da177e4 | 1588 | |
1da177e4 LT |
1589 | no_cached_page: |
1590 | /* | |
1591 | * We're only likely to ever get here if MADV_RANDOM is in | |
1592 | * effect. | |
1593 | */ | |
ef00e08e | 1594 | error = page_cache_read(file, offset); |
1da177e4 LT |
1595 | |
1596 | /* | |
1597 | * The page we want has now been added to the page cache. | |
1598 | * In the unlikely event that someone removed it in the | |
1599 | * meantime, we'll just come back here and read it again. | |
1600 | */ | |
1601 | if (error >= 0) | |
1602 | goto retry_find; | |
1603 | ||
1604 | /* | |
1605 | * An error return from page_cache_read can result if the | |
1606 | * system is low on memory, or a problem occurs while trying | |
1607 | * to schedule I/O. | |
1608 | */ | |
1609 | if (error == -ENOMEM) | |
d0217ac0 NP |
1610 | return VM_FAULT_OOM; |
1611 | return VM_FAULT_SIGBUS; | |
1da177e4 LT |
1612 | |
1613 | page_not_uptodate: | |
1da177e4 LT |
1614 | /* |
1615 | * Umm, take care of errors if the page isn't up-to-date. | |
1616 | * Try to re-read it _once_. We do this synchronously, | |
1617 | * because there really aren't any performance issues here | |
1618 | * and we need to check for errors. | |
1619 | */ | |
1da177e4 | 1620 | ClearPageError(page); |
994fc28c | 1621 | error = mapping->a_ops->readpage(file, page); |
3ef0f720 MS |
1622 | if (!error) { |
1623 | wait_on_page_locked(page); | |
1624 | if (!PageUptodate(page)) | |
1625 | error = -EIO; | |
1626 | } | |
d00806b1 NP |
1627 | page_cache_release(page); |
1628 | ||
1629 | if (!error || error == AOP_TRUNCATED_PAGE) | |
994fc28c | 1630 | goto retry_find; |
1da177e4 | 1631 | |
d00806b1 | 1632 | /* Things didn't work out. Return zero to tell the mm layer so. */ |
76d42bd9 | 1633 | shrink_readahead_size_eio(file, ra); |
d0217ac0 | 1634 | return VM_FAULT_SIGBUS; |
54cb8821 NP |
1635 | } |
1636 | EXPORT_SYMBOL(filemap_fault); | |
1637 | ||
1da177e4 | 1638 | struct vm_operations_struct generic_file_vm_ops = { |
54cb8821 | 1639 | .fault = filemap_fault, |
1da177e4 LT |
1640 | }; |
1641 | ||
1642 | /* This is used for a general mmap of a disk file */ | |
1643 | ||
1644 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) | |
1645 | { | |
1646 | struct address_space *mapping = file->f_mapping; | |
1647 | ||
1648 | if (!mapping->a_ops->readpage) | |
1649 | return -ENOEXEC; | |
1650 | file_accessed(file); | |
1651 | vma->vm_ops = &generic_file_vm_ops; | |
d0217ac0 | 1652 | vma->vm_flags |= VM_CAN_NONLINEAR; |
1da177e4 LT |
1653 | return 0; |
1654 | } | |
1da177e4 LT |
1655 | |
1656 | /* | |
1657 | * This is for filesystems which do not implement ->writepage. | |
1658 | */ | |
1659 | int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) | |
1660 | { | |
1661 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) | |
1662 | return -EINVAL; | |
1663 | return generic_file_mmap(file, vma); | |
1664 | } | |
1665 | #else | |
1666 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) | |
1667 | { | |
1668 | return -ENOSYS; | |
1669 | } | |
1670 | int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma) | |
1671 | { | |
1672 | return -ENOSYS; | |
1673 | } | |
1674 | #endif /* CONFIG_MMU */ | |
1675 | ||
1676 | EXPORT_SYMBOL(generic_file_mmap); | |
1677 | EXPORT_SYMBOL(generic_file_readonly_mmap); | |
1678 | ||
6fe6900e | 1679 | static struct page *__read_cache_page(struct address_space *mapping, |
57f6b96c | 1680 | pgoff_t index, |
1da177e4 LT |
1681 | int (*filler)(void *,struct page*), |
1682 | void *data) | |
1683 | { | |
eb2be189 | 1684 | struct page *page; |
1da177e4 LT |
1685 | int err; |
1686 | repeat: | |
1687 | page = find_get_page(mapping, index); | |
1688 | if (!page) { | |
eb2be189 NP |
1689 | page = page_cache_alloc_cold(mapping); |
1690 | if (!page) | |
1691 | return ERR_PTR(-ENOMEM); | |
1692 | err = add_to_page_cache_lru(page, mapping, index, GFP_KERNEL); | |
1693 | if (unlikely(err)) { | |
1694 | page_cache_release(page); | |
1695 | if (err == -EEXIST) | |
1696 | goto repeat; | |
1da177e4 | 1697 | /* Presumably ENOMEM for radix tree node */ |
1da177e4 LT |
1698 | return ERR_PTR(err); |
1699 | } | |
1da177e4 LT |
1700 | err = filler(data, page); |
1701 | if (err < 0) { | |
1702 | page_cache_release(page); | |
1703 | page = ERR_PTR(err); | |
1704 | } | |
1705 | } | |
1da177e4 LT |
1706 | return page; |
1707 | } | |
1708 | ||
7682486b RD |
1709 | /** |
1710 | * read_cache_page_async - read into page cache, fill it if needed | |
1711 | * @mapping: the page's address_space | |
1712 | * @index: the page index | |
1713 | * @filler: function to perform the read | |
1714 | * @data: destination for read data | |
1715 | * | |
6fe6900e NP |
1716 | * Same as read_cache_page, but don't wait for page to become unlocked |
1717 | * after submitting it to the filler. | |
7682486b RD |
1718 | * |
1719 | * Read into the page cache. If a page already exists, and PageUptodate() is | |
1720 | * not set, try to fill the page but don't wait for it to become unlocked. | |
1721 | * | |
1722 | * If the page does not get brought uptodate, return -EIO. | |
1da177e4 | 1723 | */ |
6fe6900e | 1724 | struct page *read_cache_page_async(struct address_space *mapping, |
57f6b96c | 1725 | pgoff_t index, |
1da177e4 LT |
1726 | int (*filler)(void *,struct page*), |
1727 | void *data) | |
1728 | { | |
1729 | struct page *page; | |
1730 | int err; | |
1731 | ||
1732 | retry: | |
1733 | page = __read_cache_page(mapping, index, filler, data); | |
1734 | if (IS_ERR(page)) | |
c855ff37 | 1735 | return page; |
1da177e4 LT |
1736 | if (PageUptodate(page)) |
1737 | goto out; | |
1738 | ||
1739 | lock_page(page); | |
1740 | if (!page->mapping) { | |
1741 | unlock_page(page); | |
1742 | page_cache_release(page); | |
1743 | goto retry; | |
1744 | } | |
1745 | if (PageUptodate(page)) { | |
1746 | unlock_page(page); | |
1747 | goto out; | |
1748 | } | |
1749 | err = filler(data, page); | |
1750 | if (err < 0) { | |
1751 | page_cache_release(page); | |
c855ff37 | 1752 | return ERR_PTR(err); |
1da177e4 | 1753 | } |
c855ff37 | 1754 | out: |
6fe6900e NP |
1755 | mark_page_accessed(page); |
1756 | return page; | |
1757 | } | |
1758 | EXPORT_SYMBOL(read_cache_page_async); | |
1759 | ||
1760 | /** | |
1761 | * read_cache_page - read into page cache, fill it if needed | |
1762 | * @mapping: the page's address_space | |
1763 | * @index: the page index | |
1764 | * @filler: function to perform the read | |
1765 | * @data: destination for read data | |
1766 | * | |
1767 | * Read into the page cache. If a page already exists, and PageUptodate() is | |
1768 | * not set, try to fill the page then wait for it to become unlocked. | |
1769 | * | |
1770 | * If the page does not get brought uptodate, return -EIO. | |
1771 | */ | |
1772 | struct page *read_cache_page(struct address_space *mapping, | |
57f6b96c | 1773 | pgoff_t index, |
6fe6900e NP |
1774 | int (*filler)(void *,struct page*), |
1775 | void *data) | |
1776 | { | |
1777 | struct page *page; | |
f80e69e7 | 1778 | int err; |
6fe6900e NP |
1779 | |
1780 | page = read_cache_page_async(mapping, index, filler, data); | |
1781 | if (IS_ERR(page)) | |
1782 | goto out; | |
f80e69e7 JA |
1783 | err = wait_on_page_locked_async(page, current->io_wait); |
1784 | if (err) { | |
1785 | page = ERR_PTR(err); | |
1786 | goto out; | |
1787 | } | |
6fe6900e NP |
1788 | if (!PageUptodate(page)) { |
1789 | page_cache_release(page); | |
1790 | page = ERR_PTR(-EIO); | |
1791 | } | |
1da177e4 LT |
1792 | out: |
1793 | return page; | |
1794 | } | |
1da177e4 LT |
1795 | EXPORT_SYMBOL(read_cache_page); |
1796 | ||
1da177e4 LT |
1797 | /* |
1798 | * The logic we want is | |
1799 | * | |
1800 | * if suid or (sgid and xgrp) | |
1801 | * remove privs | |
1802 | */ | |
01de85e0 | 1803 | int should_remove_suid(struct dentry *dentry) |
1da177e4 LT |
1804 | { |
1805 | mode_t mode = dentry->d_inode->i_mode; | |
1806 | int kill = 0; | |
1da177e4 LT |
1807 | |
1808 | /* suid always must be killed */ | |
1809 | if (unlikely(mode & S_ISUID)) | |
1810 | kill = ATTR_KILL_SUID; | |
1811 | ||
1812 | /* | |
1813 | * sgid without any exec bits is just a mandatory locking mark; leave | |
1814 | * it alone. If some exec bits are set, it's a real sgid; kill it. | |
1815 | */ | |
1816 | if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) | |
1817 | kill |= ATTR_KILL_SGID; | |
1818 | ||
7f5ff766 | 1819 | if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode))) |
01de85e0 | 1820 | return kill; |
1da177e4 | 1821 | |
01de85e0 JA |
1822 | return 0; |
1823 | } | |
d23a147b | 1824 | EXPORT_SYMBOL(should_remove_suid); |
01de85e0 | 1825 | |
7f3d4ee1 | 1826 | static int __remove_suid(struct dentry *dentry, int kill) |
01de85e0 JA |
1827 | { |
1828 | struct iattr newattrs; | |
1829 | ||
1830 | newattrs.ia_valid = ATTR_FORCE | kill; | |
1831 | return notify_change(dentry, &newattrs); | |
1832 | } | |
1833 | ||
2f1936b8 | 1834 | int file_remove_suid(struct file *file) |
01de85e0 | 1835 | { |
2f1936b8 | 1836 | struct dentry *dentry = file->f_path.dentry; |
b5376771 SH |
1837 | int killsuid = should_remove_suid(dentry); |
1838 | int killpriv = security_inode_need_killpriv(dentry); | |
1839 | int error = 0; | |
01de85e0 | 1840 | |
b5376771 SH |
1841 | if (killpriv < 0) |
1842 | return killpriv; | |
1843 | if (killpriv) | |
1844 | error = security_inode_killpriv(dentry); | |
1845 | if (!error && killsuid) | |
1846 | error = __remove_suid(dentry, killsuid); | |
01de85e0 | 1847 | |
b5376771 | 1848 | return error; |
1da177e4 | 1849 | } |
2f1936b8 | 1850 | EXPORT_SYMBOL(file_remove_suid); |
1da177e4 | 1851 | |
2f718ffc | 1852 | static size_t __iovec_copy_from_user_inatomic(char *vaddr, |
1da177e4 LT |
1853 | const struct iovec *iov, size_t base, size_t bytes) |
1854 | { | |
f1800536 | 1855 | size_t copied = 0, left = 0; |
1da177e4 LT |
1856 | |
1857 | while (bytes) { | |
1858 | char __user *buf = iov->iov_base + base; | |
1859 | int copy = min(bytes, iov->iov_len - base); | |
1860 | ||
1861 | base = 0; | |
f1800536 | 1862 | left = __copy_from_user_inatomic(vaddr, buf, copy); |
1da177e4 LT |
1863 | copied += copy; |
1864 | bytes -= copy; | |
1865 | vaddr += copy; | |
1866 | iov++; | |
1867 | ||
01408c49 | 1868 | if (unlikely(left)) |
1da177e4 | 1869 | break; |
1da177e4 LT |
1870 | } |
1871 | return copied - left; | |
1872 | } | |
1873 | ||
2f718ffc NP |
1874 | /* |
1875 | * Copy as much as we can into the page and return the number of bytes which | |
1876 | * were sucessfully copied. If a fault is encountered then return the number of | |
1877 | * bytes which were copied. | |
1878 | */ | |
1879 | size_t iov_iter_copy_from_user_atomic(struct page *page, | |
1880 | struct iov_iter *i, unsigned long offset, size_t bytes) | |
1881 | { | |
1882 | char *kaddr; | |
1883 | size_t copied; | |
1884 | ||
1885 | BUG_ON(!in_atomic()); | |
1886 | kaddr = kmap_atomic(page, KM_USER0); | |
1887 | if (likely(i->nr_segs == 1)) { | |
1888 | int left; | |
1889 | char __user *buf = i->iov->iov_base + i->iov_offset; | |
f1800536 | 1890 | left = __copy_from_user_inatomic(kaddr + offset, buf, bytes); |
2f718ffc NP |
1891 | copied = bytes - left; |
1892 | } else { | |
1893 | copied = __iovec_copy_from_user_inatomic(kaddr + offset, | |
1894 | i->iov, i->iov_offset, bytes); | |
1895 | } | |
1896 | kunmap_atomic(kaddr, KM_USER0); | |
1897 | ||
1898 | return copied; | |
1899 | } | |
89e10787 | 1900 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); |
2f718ffc NP |
1901 | |
1902 | /* | |
1903 | * This has the same sideeffects and return value as | |
1904 | * iov_iter_copy_from_user_atomic(). | |
1905 | * The difference is that it attempts to resolve faults. | |
1906 | * Page must not be locked. | |
1907 | */ | |
1908 | size_t iov_iter_copy_from_user(struct page *page, | |
1909 | struct iov_iter *i, unsigned long offset, size_t bytes) | |
1910 | { | |
1911 | char *kaddr; | |
1912 | size_t copied; | |
1913 | ||
1914 | kaddr = kmap(page); | |
1915 | if (likely(i->nr_segs == 1)) { | |
1916 | int left; | |
1917 | char __user *buf = i->iov->iov_base + i->iov_offset; | |
f1800536 | 1918 | left = __copy_from_user(kaddr + offset, buf, bytes); |
2f718ffc NP |
1919 | copied = bytes - left; |
1920 | } else { | |
1921 | copied = __iovec_copy_from_user_inatomic(kaddr + offset, | |
1922 | i->iov, i->iov_offset, bytes); | |
1923 | } | |
1924 | kunmap(page); | |
1925 | return copied; | |
1926 | } | |
89e10787 | 1927 | EXPORT_SYMBOL(iov_iter_copy_from_user); |
2f718ffc | 1928 | |
f7009264 | 1929 | void iov_iter_advance(struct iov_iter *i, size_t bytes) |
2f718ffc | 1930 | { |
f7009264 NP |
1931 | BUG_ON(i->count < bytes); |
1932 | ||
2f718ffc NP |
1933 | if (likely(i->nr_segs == 1)) { |
1934 | i->iov_offset += bytes; | |
f7009264 | 1935 | i->count -= bytes; |
2f718ffc NP |
1936 | } else { |
1937 | const struct iovec *iov = i->iov; | |
1938 | size_t base = i->iov_offset; | |
1939 | ||
124d3b70 NP |
1940 | /* |
1941 | * The !iov->iov_len check ensures we skip over unlikely | |
f7009264 | 1942 | * zero-length segments (without overruning the iovec). |
124d3b70 | 1943 | */ |
94ad374a | 1944 | while (bytes || unlikely(i->count && !iov->iov_len)) { |
f7009264 | 1945 | int copy; |
2f718ffc | 1946 | |
f7009264 NP |
1947 | copy = min(bytes, iov->iov_len - base); |
1948 | BUG_ON(!i->count || i->count < copy); | |
1949 | i->count -= copy; | |
2f718ffc NP |
1950 | bytes -= copy; |
1951 | base += copy; | |
1952 | if (iov->iov_len == base) { | |
1953 | iov++; | |
1954 | base = 0; | |
1955 | } | |
1956 | } | |
1957 | i->iov = iov; | |
1958 | i->iov_offset = base; | |
1959 | } | |
1960 | } | |
89e10787 | 1961 | EXPORT_SYMBOL(iov_iter_advance); |
2f718ffc | 1962 | |
afddba49 NP |
1963 | /* |
1964 | * Fault in the first iovec of the given iov_iter, to a maximum length | |
1965 | * of bytes. Returns 0 on success, or non-zero if the memory could not be | |
1966 | * accessed (ie. because it is an invalid address). | |
1967 | * | |
1968 | * writev-intensive code may want this to prefault several iovecs -- that | |
1969 | * would be possible (callers must not rely on the fact that _only_ the | |
1970 | * first iovec will be faulted with the current implementation). | |
1971 | */ | |
1972 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) | |
2f718ffc | 1973 | { |
2f718ffc | 1974 | char __user *buf = i->iov->iov_base + i->iov_offset; |
afddba49 NP |
1975 | bytes = min(bytes, i->iov->iov_len - i->iov_offset); |
1976 | return fault_in_pages_readable(buf, bytes); | |
2f718ffc | 1977 | } |
89e10787 | 1978 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
2f718ffc NP |
1979 | |
1980 | /* | |
1981 | * Return the count of just the current iov_iter segment. | |
1982 | */ | |
1983 | size_t iov_iter_single_seg_count(struct iov_iter *i) | |
1984 | { | |
1985 | const struct iovec *iov = i->iov; | |
1986 | if (i->nr_segs == 1) | |
1987 | return i->count; | |
1988 | else | |
1989 | return min(i->count, iov->iov_len - i->iov_offset); | |
1990 | } | |
89e10787 | 1991 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
2f718ffc | 1992 | |
1da177e4 LT |
1993 | /* |
1994 | * Performs necessary checks before doing a write | |
1995 | * | |
485bb99b | 1996 | * Can adjust writing position or amount of bytes to write. |
1da177e4 LT |
1997 | * Returns appropriate error code that caller should return or |
1998 | * zero in case that write should be allowed. | |
1999 | */ | |
2000 | inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk) | |
2001 | { | |
2002 | struct inode *inode = file->f_mapping->host; | |
2003 | unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; | |
2004 | ||
2005 | if (unlikely(*pos < 0)) | |
2006 | return -EINVAL; | |
2007 | ||
1da177e4 LT |
2008 | if (!isblk) { |
2009 | /* FIXME: this is for backwards compatibility with 2.4 */ | |
2010 | if (file->f_flags & O_APPEND) | |
2011 | *pos = i_size_read(inode); | |
2012 | ||
2013 | if (limit != RLIM_INFINITY) { | |
2014 | if (*pos >= limit) { | |
2015 | send_sig(SIGXFSZ, current, 0); | |
2016 | return -EFBIG; | |
2017 | } | |
2018 | if (*count > limit - (typeof(limit))*pos) { | |
2019 | *count = limit - (typeof(limit))*pos; | |
2020 | } | |
2021 | } | |
2022 | } | |
2023 | ||
2024 | /* | |
2025 | * LFS rule | |
2026 | */ | |
2027 | if (unlikely(*pos + *count > MAX_NON_LFS && | |
2028 | !(file->f_flags & O_LARGEFILE))) { | |
2029 | if (*pos >= MAX_NON_LFS) { | |
1da177e4 LT |
2030 | return -EFBIG; |
2031 | } | |
2032 | if (*count > MAX_NON_LFS - (unsigned long)*pos) { | |
2033 | *count = MAX_NON_LFS - (unsigned long)*pos; | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | /* | |
2038 | * Are we about to exceed the fs block limit ? | |
2039 | * | |
2040 | * If we have written data it becomes a short write. If we have | |
2041 | * exceeded without writing data we send a signal and return EFBIG. | |
2042 | * Linus frestrict idea will clean these up nicely.. | |
2043 | */ | |
2044 | if (likely(!isblk)) { | |
2045 | if (unlikely(*pos >= inode->i_sb->s_maxbytes)) { | |
2046 | if (*count || *pos > inode->i_sb->s_maxbytes) { | |
1da177e4 LT |
2047 | return -EFBIG; |
2048 | } | |
2049 | /* zero-length writes at ->s_maxbytes are OK */ | |
2050 | } | |
2051 | ||
2052 | if (unlikely(*pos + *count > inode->i_sb->s_maxbytes)) | |
2053 | *count = inode->i_sb->s_maxbytes - *pos; | |
2054 | } else { | |
9361401e | 2055 | #ifdef CONFIG_BLOCK |
1da177e4 LT |
2056 | loff_t isize; |
2057 | if (bdev_read_only(I_BDEV(inode))) | |
2058 | return -EPERM; | |
2059 | isize = i_size_read(inode); | |
2060 | if (*pos >= isize) { | |
2061 | if (*count || *pos > isize) | |
2062 | return -ENOSPC; | |
2063 | } | |
2064 | ||
2065 | if (*pos + *count > isize) | |
2066 | *count = isize - *pos; | |
9361401e DH |
2067 | #else |
2068 | return -EPERM; | |
2069 | #endif | |
1da177e4 LT |
2070 | } |
2071 | return 0; | |
2072 | } | |
2073 | EXPORT_SYMBOL(generic_write_checks); | |
2074 | ||
afddba49 NP |
2075 | int pagecache_write_begin(struct file *file, struct address_space *mapping, |
2076 | loff_t pos, unsigned len, unsigned flags, | |
2077 | struct page **pagep, void **fsdata) | |
2078 | { | |
2079 | const struct address_space_operations *aops = mapping->a_ops; | |
2080 | ||
4e02ed4b | 2081 | return aops->write_begin(file, mapping, pos, len, flags, |
afddba49 | 2082 | pagep, fsdata); |
afddba49 NP |
2083 | } |
2084 | EXPORT_SYMBOL(pagecache_write_begin); | |
2085 | ||
2086 | int pagecache_write_end(struct file *file, struct address_space *mapping, | |
2087 | loff_t pos, unsigned len, unsigned copied, | |
2088 | struct page *page, void *fsdata) | |
2089 | { | |
2090 | const struct address_space_operations *aops = mapping->a_ops; | |
afddba49 | 2091 | |
4e02ed4b NP |
2092 | mark_page_accessed(page); |
2093 | return aops->write_end(file, mapping, pos, len, copied, page, fsdata); | |
afddba49 NP |
2094 | } |
2095 | EXPORT_SYMBOL(pagecache_write_end); | |
2096 | ||
1da177e4 LT |
2097 | ssize_t |
2098 | generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov, | |
2099 | unsigned long *nr_segs, loff_t pos, loff_t *ppos, | |
2100 | size_t count, size_t ocount) | |
2101 | { | |
2102 | struct file *file = iocb->ki_filp; | |
2103 | struct address_space *mapping = file->f_mapping; | |
2104 | struct inode *inode = mapping->host; | |
2105 | ssize_t written; | |
a969e903 CH |
2106 | size_t write_len; |
2107 | pgoff_t end; | |
1da177e4 LT |
2108 | |
2109 | if (count != ocount) | |
2110 | *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count); | |
2111 | ||
a969e903 CH |
2112 | write_len = iov_length(iov, *nr_segs); |
2113 | end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT; | |
a969e903 | 2114 | |
48b47c56 | 2115 | written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1); |
a969e903 CH |
2116 | if (written) |
2117 | goto out; | |
2118 | ||
2119 | /* | |
2120 | * After a write we want buffered reads to be sure to go to disk to get | |
2121 | * the new data. We invalidate clean cached page from the region we're | |
2122 | * about to write. We do this *before* the write so that we can return | |
6ccfa806 | 2123 | * without clobbering -EIOCBQUEUED from ->direct_IO(). |
a969e903 CH |
2124 | */ |
2125 | if (mapping->nrpages) { | |
2126 | written = invalidate_inode_pages2_range(mapping, | |
2127 | pos >> PAGE_CACHE_SHIFT, end); | |
6ccfa806 HH |
2128 | /* |
2129 | * If a page can not be invalidated, return 0 to fall back | |
2130 | * to buffered write. | |
2131 | */ | |
2132 | if (written) { | |
2133 | if (written == -EBUSY) | |
2134 | return 0; | |
a969e903 | 2135 | goto out; |
6ccfa806 | 2136 | } |
a969e903 CH |
2137 | } |
2138 | ||
2139 | written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs); | |
2140 | ||
2141 | /* | |
2142 | * Finally, try again to invalidate clean pages which might have been | |
2143 | * cached by non-direct readahead, or faulted in by get_user_pages() | |
2144 | * if the source of the write was an mmap'ed region of the file | |
2145 | * we're writing. Either one is a pretty crazy thing to do, | |
2146 | * so we don't support it 100%. If this invalidation | |
2147 | * fails, tough, the write still worked... | |
2148 | */ | |
2149 | if (mapping->nrpages) { | |
2150 | invalidate_inode_pages2_range(mapping, | |
2151 | pos >> PAGE_CACHE_SHIFT, end); | |
2152 | } | |
2153 | ||
1da177e4 LT |
2154 | if (written > 0) { |
2155 | loff_t end = pos + written; | |
2156 | if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { | |
2157 | i_size_write(inode, end); | |
2158 | mark_inode_dirty(inode); | |
2159 | } | |
2160 | *ppos = end; | |
2161 | } | |
a969e903 | 2162 | out: |
1da177e4 LT |
2163 | return written; |
2164 | } | |
2165 | EXPORT_SYMBOL(generic_file_direct_write); | |
2166 | ||
eb2be189 NP |
2167 | /* |
2168 | * Find or create a page at the given pagecache position. Return the locked | |
2169 | * page. This function is specifically for buffered writes. | |
2170 | */ | |
54566b2c NP |
2171 | struct page *grab_cache_page_write_begin(struct address_space *mapping, |
2172 | pgoff_t index, unsigned flags) | |
eb2be189 NP |
2173 | { |
2174 | int status; | |
2175 | struct page *page; | |
54566b2c NP |
2176 | gfp_t gfp_notmask = 0; |
2177 | if (flags & AOP_FLAG_NOFS) | |
2178 | gfp_notmask = __GFP_FS; | |
eb2be189 NP |
2179 | repeat: |
2180 | page = find_lock_page(mapping, index); | |
2181 | if (likely(page)) | |
2182 | return page; | |
2183 | ||
54566b2c | 2184 | page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~gfp_notmask); |
eb2be189 NP |
2185 | if (!page) |
2186 | return NULL; | |
54566b2c NP |
2187 | status = add_to_page_cache_lru(page, mapping, index, |
2188 | GFP_KERNEL & ~gfp_notmask); | |
eb2be189 NP |
2189 | if (unlikely(status)) { |
2190 | page_cache_release(page); | |
2191 | if (status == -EEXIST) | |
2192 | goto repeat; | |
2193 | return NULL; | |
2194 | } | |
2195 | return page; | |
2196 | } | |
54566b2c | 2197 | EXPORT_SYMBOL(grab_cache_page_write_begin); |
eb2be189 | 2198 | |
afddba49 NP |
2199 | static ssize_t generic_perform_write(struct file *file, |
2200 | struct iov_iter *i, loff_t pos) | |
2201 | { | |
2202 | struct address_space *mapping = file->f_mapping; | |
2203 | const struct address_space_operations *a_ops = mapping->a_ops; | |
2204 | long status = 0; | |
2205 | ssize_t written = 0; | |
674b892e NP |
2206 | unsigned int flags = 0; |
2207 | ||
2208 | /* | |
2209 | * Copies from kernel address space cannot fail (NFSD is a big user). | |
2210 | */ | |
2211 | if (segment_eq(get_fs(), KERNEL_DS)) | |
2212 | flags |= AOP_FLAG_UNINTERRUPTIBLE; | |
afddba49 NP |
2213 | |
2214 | do { | |
2215 | struct page *page; | |
2216 | pgoff_t index; /* Pagecache index for current page */ | |
2217 | unsigned long offset; /* Offset into pagecache page */ | |
2218 | unsigned long bytes; /* Bytes to write to page */ | |
2219 | size_t copied; /* Bytes copied from user */ | |
2220 | void *fsdata; | |
2221 | ||
2222 | offset = (pos & (PAGE_CACHE_SIZE - 1)); | |
2223 | index = pos >> PAGE_CACHE_SHIFT; | |
2224 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, | |
2225 | iov_iter_count(i)); | |
2226 | ||
2227 | again: | |
2228 | ||
2229 | /* | |
2230 | * Bring in the user page that we will copy from _first_. | |
2231 | * Otherwise there's a nasty deadlock on copying from the | |
2232 | * same page as we're writing to, without it being marked | |
2233 | * up-to-date. | |
2234 | * | |
2235 | * Not only is this an optimisation, but it is also required | |
2236 | * to check that the address is actually valid, when atomic | |
2237 | * usercopies are used, below. | |
2238 | */ | |
2239 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { | |
2240 | status = -EFAULT; | |
2241 | break; | |
2242 | } | |
2243 | ||
674b892e | 2244 | status = a_ops->write_begin(file, mapping, pos, bytes, flags, |
afddba49 NP |
2245 | &page, &fsdata); |
2246 | if (unlikely(status)) | |
2247 | break; | |
2248 | ||
2249 | pagefault_disable(); | |
2250 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); | |
2251 | pagefault_enable(); | |
2252 | flush_dcache_page(page); | |
2253 | ||
c8236db9 | 2254 | mark_page_accessed(page); |
afddba49 NP |
2255 | status = a_ops->write_end(file, mapping, pos, bytes, copied, |
2256 | page, fsdata); | |
2257 | if (unlikely(status < 0)) | |
2258 | break; | |
2259 | copied = status; | |
2260 | ||
2261 | cond_resched(); | |
2262 | ||
124d3b70 | 2263 | iov_iter_advance(i, copied); |
afddba49 NP |
2264 | if (unlikely(copied == 0)) { |
2265 | /* | |
2266 | * If we were unable to copy any data at all, we must | |
2267 | * fall back to a single segment length write. | |
2268 | * | |
2269 | * If we didn't fallback here, we could livelock | |
2270 | * because not all segments in the iov can be copied at | |
2271 | * once without a pagefault. | |
2272 | */ | |
2273 | bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, | |
2274 | iov_iter_single_seg_count(i)); | |
2275 | goto again; | |
2276 | } | |
afddba49 NP |
2277 | pos += copied; |
2278 | written += copied; | |
2279 | ||
2280 | balance_dirty_pages_ratelimited(mapping); | |
2281 | ||
2282 | } while (iov_iter_count(i)); | |
2283 | ||
2284 | return written ? written : status; | |
2285 | } | |
2286 | ||
2287 | ssize_t | |
2288 | generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov, | |
2289 | unsigned long nr_segs, loff_t pos, loff_t *ppos, | |
2290 | size_t count, ssize_t written) | |
2291 | { | |
2292 | struct file *file = iocb->ki_filp; | |
2293 | struct address_space *mapping = file->f_mapping; | |
afddba49 NP |
2294 | ssize_t status; |
2295 | struct iov_iter i; | |
2296 | ||
2297 | iov_iter_init(&i, iov, nr_segs, count, written); | |
4e02ed4b | 2298 | status = generic_perform_write(file, &i, pos); |
1da177e4 | 2299 | |
1da177e4 | 2300 | if (likely(status >= 0)) { |
afddba49 NP |
2301 | written += status; |
2302 | *ppos = pos + status; | |
1da177e4 LT |
2303 | } |
2304 | ||
2305 | /* | |
2306 | * If we get here for O_DIRECT writes then we must have fallen through | |
2307 | * to buffered writes (block instantiation inside i_size). So we sync | |
2308 | * the file data here, to try to honour O_DIRECT expectations. | |
2309 | */ | |
2310 | if (unlikely(file->f_flags & O_DIRECT) && written) | |
48b47c56 NP |
2311 | status = filemap_write_and_wait_range(mapping, |
2312 | pos, pos + written - 1); | |
1da177e4 | 2313 | |
1da177e4 LT |
2314 | return written ? written : status; |
2315 | } | |
2316 | EXPORT_SYMBOL(generic_file_buffered_write); | |
2317 | ||
e4dd9de3 JK |
2318 | /** |
2319 | * __generic_file_aio_write - write data to a file | |
2320 | * @iocb: IO state structure (file, offset, etc.) | |
2321 | * @iov: vector with data to write | |
2322 | * @nr_segs: number of segments in the vector | |
2323 | * @ppos: position where to write | |
2324 | * | |
2325 | * This function does all the work needed for actually writing data to a | |
2326 | * file. It does all basic checks, removes SUID from the file, updates | |
2327 | * modification times and calls proper subroutines depending on whether we | |
2328 | * do direct IO or a standard buffered write. | |
2329 | * | |
2330 | * It expects i_mutex to be grabbed unless we work on a block device or similar | |
2331 | * object which does not need locking at all. | |
2332 | * | |
2333 | * This function does *not* take care of syncing data in case of O_SYNC write. | |
2334 | * A caller has to handle it. This is mainly due to the fact that we want to | |
2335 | * avoid syncing under i_mutex. | |
2336 | */ | |
2337 | ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
2338 | unsigned long nr_segs, loff_t *ppos) | |
1da177e4 LT |
2339 | { |
2340 | struct file *file = iocb->ki_filp; | |
fb5527e6 | 2341 | struct address_space * mapping = file->f_mapping; |
1da177e4 LT |
2342 | size_t ocount; /* original count */ |
2343 | size_t count; /* after file limit checks */ | |
2344 | struct inode *inode = mapping->host; | |
1da177e4 LT |
2345 | loff_t pos; |
2346 | ssize_t written; | |
2347 | ssize_t err; | |
2348 | ||
2349 | ocount = 0; | |
0ceb3314 DM |
2350 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
2351 | if (err) | |
2352 | return err; | |
1da177e4 LT |
2353 | |
2354 | count = ocount; | |
2355 | pos = *ppos; | |
2356 | ||
2357 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
2358 | ||
2359 | /* We can write back this queue in page reclaim */ | |
2360 | current->backing_dev_info = mapping->backing_dev_info; | |
2361 | written = 0; | |
2362 | ||
2363 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
2364 | if (err) | |
2365 | goto out; | |
2366 | ||
2367 | if (count == 0) | |
2368 | goto out; | |
2369 | ||
2f1936b8 | 2370 | err = file_remove_suid(file); |
1da177e4 LT |
2371 | if (err) |
2372 | goto out; | |
2373 | ||
870f4817 | 2374 | file_update_time(file); |
1da177e4 LT |
2375 | |
2376 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ | |
2377 | if (unlikely(file->f_flags & O_DIRECT)) { | |
fb5527e6 JM |
2378 | loff_t endbyte; |
2379 | ssize_t written_buffered; | |
2380 | ||
2381 | written = generic_file_direct_write(iocb, iov, &nr_segs, pos, | |
2382 | ppos, count, ocount); | |
1da177e4 LT |
2383 | if (written < 0 || written == count) |
2384 | goto out; | |
2385 | /* | |
2386 | * direct-io write to a hole: fall through to buffered I/O | |
2387 | * for completing the rest of the request. | |
2388 | */ | |
2389 | pos += written; | |
2390 | count -= written; | |
fb5527e6 JM |
2391 | written_buffered = generic_file_buffered_write(iocb, iov, |
2392 | nr_segs, pos, ppos, count, | |
2393 | written); | |
2394 | /* | |
2395 | * If generic_file_buffered_write() retuned a synchronous error | |
2396 | * then we want to return the number of bytes which were | |
2397 | * direct-written, or the error code if that was zero. Note | |
2398 | * that this differs from normal direct-io semantics, which | |
2399 | * will return -EFOO even if some bytes were written. | |
2400 | */ | |
2401 | if (written_buffered < 0) { | |
2402 | err = written_buffered; | |
2403 | goto out; | |
2404 | } | |
1da177e4 | 2405 | |
fb5527e6 JM |
2406 | /* |
2407 | * We need to ensure that the page cache pages are written to | |
2408 | * disk and invalidated to preserve the expected O_DIRECT | |
2409 | * semantics. | |
2410 | */ | |
2411 | endbyte = pos + written_buffered - written - 1; | |
ef51c976 MF |
2412 | err = do_sync_mapping_range(file->f_mapping, pos, endbyte, |
2413 | SYNC_FILE_RANGE_WAIT_BEFORE| | |
2414 | SYNC_FILE_RANGE_WRITE| | |
2415 | SYNC_FILE_RANGE_WAIT_AFTER); | |
fb5527e6 JM |
2416 | if (err == 0) { |
2417 | written = written_buffered; | |
2418 | invalidate_mapping_pages(mapping, | |
2419 | pos >> PAGE_CACHE_SHIFT, | |
2420 | endbyte >> PAGE_CACHE_SHIFT); | |
2421 | } else { | |
2422 | /* | |
2423 | * We don't know how much we wrote, so just return | |
2424 | * the number of bytes which were direct-written | |
2425 | */ | |
2426 | } | |
2427 | } else { | |
2428 | written = generic_file_buffered_write(iocb, iov, nr_segs, | |
2429 | pos, ppos, count, written); | |
2430 | } | |
1da177e4 LT |
2431 | out: |
2432 | current->backing_dev_info = NULL; | |
2433 | return written ? written : err; | |
2434 | } | |
e4dd9de3 JK |
2435 | EXPORT_SYMBOL(__generic_file_aio_write); |
2436 | ||
e4dd9de3 JK |
2437 | /** |
2438 | * generic_file_aio_write - write data to a file | |
2439 | * @iocb: IO state structure | |
2440 | * @iov: vector with data to write | |
2441 | * @nr_segs: number of segments in the vector | |
2442 | * @pos: position in file where to write | |
2443 | * | |
2444 | * This is a wrapper around __generic_file_aio_write() to be used by most | |
2445 | * filesystems. It takes care of syncing the file in case of O_SYNC file | |
2446 | * and acquires i_mutex as needed. | |
2447 | */ | |
027445c3 BP |
2448 | ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, |
2449 | unsigned long nr_segs, loff_t pos) | |
1da177e4 LT |
2450 | { |
2451 | struct file *file = iocb->ki_filp; | |
148f948b | 2452 | struct inode *inode = file->f_mapping->host; |
1da177e4 | 2453 | ssize_t ret; |
1da177e4 LT |
2454 | |
2455 | BUG_ON(iocb->ki_pos != pos); | |
2456 | ||
1b1dcc1b | 2457 | mutex_lock(&inode->i_mutex); |
e4dd9de3 | 2458 | ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); |
1b1dcc1b | 2459 | mutex_unlock(&inode->i_mutex); |
1da177e4 | 2460 | |
148f948b | 2461 | if (ret > 0 || ret == -EIOCBQUEUED) { |
1da177e4 LT |
2462 | ssize_t err; |
2463 | ||
148f948b | 2464 | err = generic_write_sync(file, pos, ret); |
c7b50db2 | 2465 | if (err < 0 && ret > 0) |
1da177e4 LT |
2466 | ret = err; |
2467 | } | |
2468 | return ret; | |
2469 | } | |
2470 | EXPORT_SYMBOL(generic_file_aio_write); | |
2471 | ||
cf9a2ae8 DH |
2472 | /** |
2473 | * try_to_release_page() - release old fs-specific metadata on a page | |
2474 | * | |
2475 | * @page: the page which the kernel is trying to free | |
2476 | * @gfp_mask: memory allocation flags (and I/O mode) | |
2477 | * | |
2478 | * The address_space is to try to release any data against the page | |
2479 | * (presumably at page->private). If the release was successful, return `1'. | |
2480 | * Otherwise return zero. | |
2481 | * | |
266cf658 DH |
2482 | * This may also be called if PG_fscache is set on a page, indicating that the |
2483 | * page is known to the local caching routines. | |
2484 | * | |
cf9a2ae8 | 2485 | * The @gfp_mask argument specifies whether I/O may be performed to release |
3f31fddf | 2486 | * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS). |
cf9a2ae8 | 2487 | * |
cf9a2ae8 DH |
2488 | */ |
2489 | int try_to_release_page(struct page *page, gfp_t gfp_mask) | |
2490 | { | |
2491 | struct address_space * const mapping = page->mapping; | |
2492 | ||
2493 | BUG_ON(!PageLocked(page)); | |
2494 | if (PageWriteback(page)) | |
2495 | return 0; | |
2496 | ||
2497 | if (mapping && mapping->a_ops->releasepage) | |
2498 | return mapping->a_ops->releasepage(page, gfp_mask); | |
2499 | return try_to_free_buffers(page); | |
2500 | } | |
2501 | ||
2502 | EXPORT_SYMBOL(try_to_release_page); |