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