Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/compiler.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/aio.h> |
| 18 | #include <linux/kernel_stat.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/swap.h> |
| 21 | #include <linux/mman.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/file.h> |
| 24 | #include <linux/uio.h> |
| 25 | #include <linux/hash.h> |
| 26 | #include <linux/writeback.h> |
| 27 | #include <linux/pagevec.h> |
| 28 | #include <linux/blkdev.h> |
| 29 | #include <linux/security.h> |
| 30 | #include <linux/syscalls.h> |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 31 | #include "filemap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * FIXME: remove all knowledge of the buffer layer from the core VM |
| 34 | */ |
| 35 | #include <linux/buffer_head.h> /* for generic_osync_inode */ |
| 36 | |
| 37 | #include <asm/uaccess.h> |
| 38 | #include <asm/mman.h> |
| 39 | |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 40 | static ssize_t |
| 41 | generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 42 | loff_t offset, unsigned long nr_segs); |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Shared mappings implemented 30.11.1994. It's not fully working yet, |
| 46 | * though. |
| 47 | * |
| 48 | * Shared mappings now work. 15.8.1995 Bruno. |
| 49 | * |
| 50 | * finished 'unifying' the page and buffer cache and SMP-threaded the |
| 51 | * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> |
| 52 | * |
| 53 | * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Lock ordering: |
| 58 | * |
| 59 | * ->i_mmap_lock (vmtruncate) |
| 60 | * ->private_lock (__free_pte->__set_page_dirty_buffers) |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 61 | * ->swap_lock (exclusive_swap_page, others) |
| 62 | * ->mapping->tree_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * |
| 64 | * ->i_sem |
| 65 | * ->i_mmap_lock (truncate->unmap_mapping_range) |
| 66 | * |
| 67 | * ->mmap_sem |
| 68 | * ->i_mmap_lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 69 | * ->page_table_lock or pte_lock (various, mainly in memory.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) |
| 71 | * |
| 72 | * ->mmap_sem |
| 73 | * ->lock_page (access_process_vm) |
| 74 | * |
| 75 | * ->mmap_sem |
| 76 | * ->i_sem (msync) |
| 77 | * |
| 78 | * ->i_sem |
| 79 | * ->i_alloc_sem (various) |
| 80 | * |
| 81 | * ->inode_lock |
| 82 | * ->sb_lock (fs/fs-writeback.c) |
| 83 | * ->mapping->tree_lock (__sync_single_inode) |
| 84 | * |
| 85 | * ->i_mmap_lock |
| 86 | * ->anon_vma.lock (vma_adjust) |
| 87 | * |
| 88 | * ->anon_vma.lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 89 | * ->page_table_lock or pte_lock (anon_vma_prepare and various) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 91 | * ->page_table_lock or pte_lock |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 92 | * ->swap_lock (try_to_unmap_one) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * ->private_lock (try_to_unmap_one) |
| 94 | * ->tree_lock (try_to_unmap_one) |
| 95 | * ->zone.lru_lock (follow_page->mark_page_accessed) |
| 96 | * ->private_lock (page_remove_rmap->set_page_dirty) |
| 97 | * ->tree_lock (page_remove_rmap->set_page_dirty) |
| 98 | * ->inode_lock (page_remove_rmap->set_page_dirty) |
| 99 | * ->inode_lock (zap_pte_range->set_page_dirty) |
| 100 | * ->private_lock (zap_pte_range->__set_page_dirty_buffers) |
| 101 | * |
| 102 | * ->task->proc_lock |
| 103 | * ->dcache_lock (proc_pid_lookup) |
| 104 | */ |
| 105 | |
| 106 | /* |
| 107 | * Remove a page from the page cache and free it. Caller has to make |
| 108 | * sure the page is locked and that nobody else uses it - or that usage |
| 109 | * is safe. The caller must hold a write_lock on the mapping's tree_lock. |
| 110 | */ |
| 111 | void __remove_from_page_cache(struct page *page) |
| 112 | { |
| 113 | struct address_space *mapping = page->mapping; |
| 114 | |
| 115 | radix_tree_delete(&mapping->page_tree, page->index); |
| 116 | page->mapping = NULL; |
| 117 | mapping->nrpages--; |
| 118 | pagecache_acct(-1); |
| 119 | } |
| 120 | |
| 121 | void remove_from_page_cache(struct page *page) |
| 122 | { |
| 123 | struct address_space *mapping = page->mapping; |
| 124 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 125 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | write_lock_irq(&mapping->tree_lock); |
| 128 | __remove_from_page_cache(page); |
| 129 | write_unlock_irq(&mapping->tree_lock); |
| 130 | } |
| 131 | |
| 132 | static int sync_page(void *word) |
| 133 | { |
| 134 | struct address_space *mapping; |
| 135 | struct page *page; |
| 136 | |
Andi Kleen | 07808b7 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 137 | page = container_of((unsigned long *)word, struct page, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | /* |
William Lee Irwin III | dd1d5af | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 140 | * page_mapping() is being called without PG_locked held. |
| 141 | * Some knowledge of the state and use of the page is used to |
| 142 | * reduce the requirements down to a memory barrier. |
| 143 | * The danger here is of a stale page_mapping() return value |
| 144 | * indicating a struct address_space different from the one it's |
| 145 | * associated with when it is associated with one. |
| 146 | * After smp_mb(), it's either the correct page_mapping() for |
| 147 | * the page, or an old page_mapping() and the page's own |
| 148 | * page_mapping() has gone NULL. |
| 149 | * The ->sync_page() address_space operation must tolerate |
| 150 | * page_mapping() going NULL. By an amazing coincidence, |
| 151 | * this comes about because none of the users of the page |
| 152 | * in the ->sync_page() methods make essential use of the |
| 153 | * page_mapping(), merely passing the page down to the backing |
| 154 | * device's unplug functions when it's non-NULL, which in turn |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 155 | * ignore it for all cases but swap, where only page_private(page) is |
William Lee Irwin III | dd1d5af | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 156 | * of interest. When page_mapping() does go NULL, the entire |
| 157 | * call stack gracefully ignores the page and returns. |
| 158 | * -- wli |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | */ |
| 160 | smp_mb(); |
| 161 | mapping = page_mapping(page); |
| 162 | if (mapping && mapping->a_ops && mapping->a_ops->sync_page) |
| 163 | mapping->a_ops->sync_page(page); |
| 164 | io_schedule(); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * filemap_fdatawrite_range - start writeback against all of a mapping's |
| 170 | * dirty pages that lie within the byte offsets <start, end> |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 171 | * @mapping: address space structure to write |
| 172 | * @start: offset in bytes where the range starts |
| 173 | * @end: offset in bytes where the range ends |
| 174 | * @sync_mode: enable synchronous operation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * |
| 176 | * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as |
| 177 | * opposed to a regular memory * cleansing writeback. The difference between |
| 178 | * these two operations is that if a dirty page/buffer is encountered, it must |
| 179 | * be waited upon, and not just skipped over. |
| 180 | */ |
| 181 | static int __filemap_fdatawrite_range(struct address_space *mapping, |
| 182 | loff_t start, loff_t end, int sync_mode) |
| 183 | { |
| 184 | int ret; |
| 185 | struct writeback_control wbc = { |
| 186 | .sync_mode = sync_mode, |
| 187 | .nr_to_write = mapping->nrpages * 2, |
| 188 | .start = start, |
| 189 | .end = end, |
| 190 | }; |
| 191 | |
| 192 | if (!mapping_cap_writeback_dirty(mapping)) |
| 193 | return 0; |
| 194 | |
| 195 | ret = do_writepages(mapping, &wbc); |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static inline int __filemap_fdatawrite(struct address_space *mapping, |
| 200 | int sync_mode) |
| 201 | { |
| 202 | return __filemap_fdatawrite_range(mapping, 0, 0, sync_mode); |
| 203 | } |
| 204 | |
| 205 | int filemap_fdatawrite(struct address_space *mapping) |
| 206 | { |
| 207 | return __filemap_fdatawrite(mapping, WB_SYNC_ALL); |
| 208 | } |
| 209 | EXPORT_SYMBOL(filemap_fdatawrite); |
| 210 | |
| 211 | static int filemap_fdatawrite_range(struct address_space *mapping, |
| 212 | loff_t start, loff_t end) |
| 213 | { |
| 214 | return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * This is a mostly non-blocking flush. Not suitable for data-integrity |
| 219 | * purposes - I/O may not be started against all dirty pages. |
| 220 | */ |
| 221 | int filemap_flush(struct address_space *mapping) |
| 222 | { |
| 223 | return __filemap_fdatawrite(mapping, WB_SYNC_NONE); |
| 224 | } |
| 225 | EXPORT_SYMBOL(filemap_flush); |
| 226 | |
| 227 | /* |
| 228 | * Wait for writeback to complete against pages indexed by start->end |
| 229 | * inclusive |
| 230 | */ |
| 231 | static int wait_on_page_writeback_range(struct address_space *mapping, |
| 232 | pgoff_t start, pgoff_t end) |
| 233 | { |
| 234 | struct pagevec pvec; |
| 235 | int nr_pages; |
| 236 | int ret = 0; |
| 237 | pgoff_t index; |
| 238 | |
| 239 | if (end < start) |
| 240 | return 0; |
| 241 | |
| 242 | pagevec_init(&pvec, 0); |
| 243 | index = start; |
| 244 | while ((index <= end) && |
| 245 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 246 | PAGECACHE_TAG_WRITEBACK, |
| 247 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) { |
| 248 | unsigned i; |
| 249 | |
| 250 | for (i = 0; i < nr_pages; i++) { |
| 251 | struct page *page = pvec.pages[i]; |
| 252 | |
| 253 | /* until radix tree lookup accepts end_index */ |
| 254 | if (page->index > end) |
| 255 | continue; |
| 256 | |
| 257 | wait_on_page_writeback(page); |
| 258 | if (PageError(page)) |
| 259 | ret = -EIO; |
| 260 | } |
| 261 | pagevec_release(&pvec); |
| 262 | cond_resched(); |
| 263 | } |
| 264 | |
| 265 | /* Check for outstanding write errors */ |
| 266 | if (test_and_clear_bit(AS_ENOSPC, &mapping->flags)) |
| 267 | ret = -ENOSPC; |
| 268 | if (test_and_clear_bit(AS_EIO, &mapping->flags)) |
| 269 | ret = -EIO; |
| 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Write and wait upon all the pages in the passed range. This is a "data |
| 276 | * integrity" operation. It waits upon in-flight writeout before starting and |
| 277 | * waiting upon new writeout. If there was an IO error, return it. |
| 278 | * |
| 279 | * We need to re-take i_sem during the generic_osync_inode list walk because |
| 280 | * it is otherwise livelockable. |
| 281 | */ |
| 282 | int sync_page_range(struct inode *inode, struct address_space *mapping, |
| 283 | loff_t pos, size_t count) |
| 284 | { |
| 285 | pgoff_t start = pos >> PAGE_CACHE_SHIFT; |
| 286 | pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT; |
| 287 | int ret; |
| 288 | |
| 289 | if (!mapping_cap_writeback_dirty(mapping) || !count) |
| 290 | return 0; |
| 291 | ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1); |
| 292 | if (ret == 0) { |
| 293 | down(&inode->i_sem); |
| 294 | ret = generic_osync_inode(inode, mapping, OSYNC_METADATA); |
| 295 | up(&inode->i_sem); |
| 296 | } |
| 297 | if (ret == 0) |
| 298 | ret = wait_on_page_writeback_range(mapping, start, end); |
| 299 | return ret; |
| 300 | } |
| 301 | EXPORT_SYMBOL(sync_page_range); |
| 302 | |
| 303 | /* |
| 304 | * Note: Holding i_sem across sync_page_range_nolock is not a good idea |
| 305 | * as it forces O_SYNC writers to different parts of the same file |
| 306 | * to be serialised right until io completion. |
| 307 | */ |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 308 | static int sync_page_range_nolock(struct inode *inode, |
| 309 | struct address_space *mapping, |
| 310 | loff_t pos, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
| 312 | pgoff_t start = pos >> PAGE_CACHE_SHIFT; |
| 313 | pgoff_t end = (pos + count - 1) >> PAGE_CACHE_SHIFT; |
| 314 | int ret; |
| 315 | |
| 316 | if (!mapping_cap_writeback_dirty(mapping) || !count) |
| 317 | return 0; |
| 318 | ret = filemap_fdatawrite_range(mapping, pos, pos + count - 1); |
| 319 | if (ret == 0) |
| 320 | ret = generic_osync_inode(inode, mapping, OSYNC_METADATA); |
| 321 | if (ret == 0) |
| 322 | ret = wait_on_page_writeback_range(mapping, start, end); |
| 323 | return ret; |
| 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * filemap_fdatawait - walk the list of under-writeback pages of the given |
| 328 | * address space and wait for all of them. |
| 329 | * |
| 330 | * @mapping: address space structure to wait for |
| 331 | */ |
| 332 | int filemap_fdatawait(struct address_space *mapping) |
| 333 | { |
| 334 | loff_t i_size = i_size_read(mapping->host); |
| 335 | |
| 336 | if (i_size == 0) |
| 337 | return 0; |
| 338 | |
| 339 | return wait_on_page_writeback_range(mapping, 0, |
| 340 | (i_size - 1) >> PAGE_CACHE_SHIFT); |
| 341 | } |
| 342 | EXPORT_SYMBOL(filemap_fdatawait); |
| 343 | |
| 344 | int filemap_write_and_wait(struct address_space *mapping) |
| 345 | { |
| 346 | int retval = 0; |
| 347 | |
| 348 | if (mapping->nrpages) { |
| 349 | retval = filemap_fdatawrite(mapping); |
| 350 | if (retval == 0) |
| 351 | retval = filemap_fdatawait(mapping); |
| 352 | } |
| 353 | return retval; |
| 354 | } |
| 355 | |
| 356 | int filemap_write_and_wait_range(struct address_space *mapping, |
| 357 | loff_t lstart, loff_t lend) |
| 358 | { |
| 359 | int retval = 0; |
| 360 | |
| 361 | if (mapping->nrpages) { |
| 362 | retval = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 363 | WB_SYNC_ALL); |
| 364 | if (retval == 0) |
| 365 | retval = wait_on_page_writeback_range(mapping, |
| 366 | lstart >> PAGE_CACHE_SHIFT, |
| 367 | lend >> PAGE_CACHE_SHIFT); |
| 368 | } |
| 369 | return retval; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * This function is used to add newly allocated pagecache pages: |
| 374 | * the page is new, so we can just run SetPageLocked() against it. |
| 375 | * The other page state flags were set by rmqueue(). |
| 376 | * |
| 377 | * This function does not add the page to the LRU. The caller must do that. |
| 378 | */ |
| 379 | int add_to_page_cache(struct page *page, struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 380 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM); |
| 383 | |
| 384 | if (error == 0) { |
| 385 | write_lock_irq(&mapping->tree_lock); |
| 386 | error = radix_tree_insert(&mapping->page_tree, offset, page); |
| 387 | if (!error) { |
| 388 | page_cache_get(page); |
| 389 | SetPageLocked(page); |
| 390 | page->mapping = mapping; |
| 391 | page->index = offset; |
| 392 | mapping->nrpages++; |
| 393 | pagecache_acct(1); |
| 394 | } |
| 395 | write_unlock_irq(&mapping->tree_lock); |
| 396 | radix_tree_preload_end(); |
| 397 | } |
| 398 | return error; |
| 399 | } |
| 400 | |
| 401 | EXPORT_SYMBOL(add_to_page_cache); |
| 402 | |
| 403 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 404 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
| 406 | int ret = add_to_page_cache(page, mapping, offset, gfp_mask); |
| 407 | if (ret == 0) |
| 408 | lru_cache_add(page); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * In order to wait for pages to become available there must be |
| 414 | * waitqueues associated with pages. By using a hash table of |
| 415 | * waitqueues where the bucket discipline is to maintain all |
| 416 | * waiters on the same queue and wake all when any of the pages |
| 417 | * become available, and for the woken contexts to check to be |
| 418 | * sure the appropriate page became available, this saves space |
| 419 | * at a cost of "thundering herd" phenomena during rare hash |
| 420 | * collisions. |
| 421 | */ |
| 422 | static wait_queue_head_t *page_waitqueue(struct page *page) |
| 423 | { |
| 424 | const struct zone *zone = page_zone(page); |
| 425 | |
| 426 | return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)]; |
| 427 | } |
| 428 | |
| 429 | static inline void wake_up_page(struct page *page, int bit) |
| 430 | { |
| 431 | __wake_up_bit(page_waitqueue(page), &page->flags, bit); |
| 432 | } |
| 433 | |
| 434 | void fastcall wait_on_page_bit(struct page *page, int bit_nr) |
| 435 | { |
| 436 | DEFINE_WAIT_BIT(wait, &page->flags, bit_nr); |
| 437 | |
| 438 | if (test_bit(bit_nr, &page->flags)) |
| 439 | __wait_on_bit(page_waitqueue(page), &wait, sync_page, |
| 440 | TASK_UNINTERRUPTIBLE); |
| 441 | } |
| 442 | EXPORT_SYMBOL(wait_on_page_bit); |
| 443 | |
| 444 | /** |
| 445 | * unlock_page() - unlock a locked page |
| 446 | * |
| 447 | * @page: the page |
| 448 | * |
| 449 | * Unlocks the page and wakes up sleepers in ___wait_on_page_locked(). |
| 450 | * Also wakes sleepers in wait_on_page_writeback() because the wakeup |
| 451 | * mechananism between PageLocked pages and PageWriteback pages is shared. |
| 452 | * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. |
| 453 | * |
| 454 | * The first mb is necessary to safely close the critical section opened by the |
| 455 | * TestSetPageLocked(), the second mb is necessary to enforce ordering between |
| 456 | * the clear_bit and the read of the waitqueue (to avoid SMP races with a |
| 457 | * parallel wait_on_page_locked()). |
| 458 | */ |
| 459 | void fastcall unlock_page(struct page *page) |
| 460 | { |
| 461 | smp_mb__before_clear_bit(); |
| 462 | if (!TestClearPageLocked(page)) |
| 463 | BUG(); |
| 464 | smp_mb__after_clear_bit(); |
| 465 | wake_up_page(page, PG_locked); |
| 466 | } |
| 467 | EXPORT_SYMBOL(unlock_page); |
| 468 | |
| 469 | /* |
| 470 | * End writeback against a page. |
| 471 | */ |
| 472 | void end_page_writeback(struct page *page) |
| 473 | { |
| 474 | if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) { |
| 475 | if (!test_clear_page_writeback(page)) |
| 476 | BUG(); |
| 477 | } |
| 478 | smp_mb__after_clear_bit(); |
| 479 | wake_up_page(page, PG_writeback); |
| 480 | } |
| 481 | EXPORT_SYMBOL(end_page_writeback); |
| 482 | |
| 483 | /* |
| 484 | * Get a lock on the page, assuming we need to sleep to get it. |
| 485 | * |
| 486 | * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some |
| 487 | * random driver's requestfn sets TASK_RUNNING, we could busywait. However |
| 488 | * chances are that on the second loop, the block layer's plug list is empty, |
| 489 | * so sync_page() will then return in state TASK_UNINTERRUPTIBLE. |
| 490 | */ |
| 491 | void fastcall __lock_page(struct page *page) |
| 492 | { |
| 493 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); |
| 494 | |
| 495 | __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page, |
| 496 | TASK_UNINTERRUPTIBLE); |
| 497 | } |
| 498 | EXPORT_SYMBOL(__lock_page); |
| 499 | |
| 500 | /* |
| 501 | * a rather lightweight function, finding and getting a reference to a |
| 502 | * hashed page atomically. |
| 503 | */ |
| 504 | struct page * find_get_page(struct address_space *mapping, unsigned long offset) |
| 505 | { |
| 506 | struct page *page; |
| 507 | |
| 508 | read_lock_irq(&mapping->tree_lock); |
| 509 | page = radix_tree_lookup(&mapping->page_tree, offset); |
| 510 | if (page) |
| 511 | page_cache_get(page); |
| 512 | read_unlock_irq(&mapping->tree_lock); |
| 513 | return page; |
| 514 | } |
| 515 | |
| 516 | EXPORT_SYMBOL(find_get_page); |
| 517 | |
| 518 | /* |
| 519 | * Same as above, but trylock it instead of incrementing the count. |
| 520 | */ |
| 521 | struct page *find_trylock_page(struct address_space *mapping, unsigned long offset) |
| 522 | { |
| 523 | struct page *page; |
| 524 | |
| 525 | read_lock_irq(&mapping->tree_lock); |
| 526 | page = radix_tree_lookup(&mapping->page_tree, offset); |
| 527 | if (page && TestSetPageLocked(page)) |
| 528 | page = NULL; |
| 529 | read_unlock_irq(&mapping->tree_lock); |
| 530 | return page; |
| 531 | } |
| 532 | |
| 533 | EXPORT_SYMBOL(find_trylock_page); |
| 534 | |
| 535 | /** |
| 536 | * find_lock_page - locate, pin and lock a pagecache page |
| 537 | * |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 538 | * @mapping: the address_space to search |
| 539 | * @offset: the page index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | * |
| 541 | * Locates the desired pagecache page, locks it, increments its reference |
| 542 | * count and returns its address. |
| 543 | * |
| 544 | * Returns zero if the page was not present. find_lock_page() may sleep. |
| 545 | */ |
| 546 | struct page *find_lock_page(struct address_space *mapping, |
| 547 | unsigned long offset) |
| 548 | { |
| 549 | struct page *page; |
| 550 | |
| 551 | read_lock_irq(&mapping->tree_lock); |
| 552 | repeat: |
| 553 | page = radix_tree_lookup(&mapping->page_tree, offset); |
| 554 | if (page) { |
| 555 | page_cache_get(page); |
| 556 | if (TestSetPageLocked(page)) { |
| 557 | read_unlock_irq(&mapping->tree_lock); |
Nikita Danilov | bbfbb7c | 2006-01-06 00:11:08 -0800 | [diff] [blame^] | 558 | __lock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | read_lock_irq(&mapping->tree_lock); |
| 560 | |
| 561 | /* Has the page been truncated while we slept? */ |
Nikita Danilov | bbfbb7c | 2006-01-06 00:11:08 -0800 | [diff] [blame^] | 562 | if (unlikely(page->mapping != mapping || |
| 563 | page->index != offset)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | unlock_page(page); |
| 565 | page_cache_release(page); |
| 566 | goto repeat; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | read_unlock_irq(&mapping->tree_lock); |
| 571 | return page; |
| 572 | } |
| 573 | |
| 574 | EXPORT_SYMBOL(find_lock_page); |
| 575 | |
| 576 | /** |
| 577 | * find_or_create_page - locate or add a pagecache page |
| 578 | * |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 579 | * @mapping: the page's address_space |
| 580 | * @index: the page's index into the mapping |
| 581 | * @gfp_mask: page allocation mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | * |
| 583 | * Locates a page in the pagecache. If the page is not present, a new page |
| 584 | * is allocated using @gfp_mask and is added to the pagecache and to the VM's |
| 585 | * LRU list. The returned page is locked and has its reference count |
| 586 | * incremented. |
| 587 | * |
| 588 | * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic |
| 589 | * allocation! |
| 590 | * |
| 591 | * find_or_create_page() returns the desired page's address, or zero on |
| 592 | * memory exhaustion. |
| 593 | */ |
| 594 | struct page *find_or_create_page(struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 595 | unsigned long index, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
| 597 | struct page *page, *cached_page = NULL; |
| 598 | int err; |
| 599 | repeat: |
| 600 | page = find_lock_page(mapping, index); |
| 601 | if (!page) { |
| 602 | if (!cached_page) { |
| 603 | cached_page = alloc_page(gfp_mask); |
| 604 | if (!cached_page) |
| 605 | return NULL; |
| 606 | } |
| 607 | err = add_to_page_cache_lru(cached_page, mapping, |
| 608 | index, gfp_mask); |
| 609 | if (!err) { |
| 610 | page = cached_page; |
| 611 | cached_page = NULL; |
| 612 | } else if (err == -EEXIST) |
| 613 | goto repeat; |
| 614 | } |
| 615 | if (cached_page) |
| 616 | page_cache_release(cached_page); |
| 617 | return page; |
| 618 | } |
| 619 | |
| 620 | EXPORT_SYMBOL(find_or_create_page); |
| 621 | |
| 622 | /** |
| 623 | * find_get_pages - gang pagecache lookup |
| 624 | * @mapping: The address_space to search |
| 625 | * @start: The starting page index |
| 626 | * @nr_pages: The maximum number of pages |
| 627 | * @pages: Where the resulting pages are placed |
| 628 | * |
| 629 | * find_get_pages() will search for and return a group of up to |
| 630 | * @nr_pages pages in the mapping. The pages are placed at @pages. |
| 631 | * find_get_pages() takes a reference against the returned pages. |
| 632 | * |
| 633 | * The search returns a group of mapping-contiguous pages with ascending |
| 634 | * indexes. There may be holes in the indices due to not-present pages. |
| 635 | * |
| 636 | * find_get_pages() returns the number of pages which were found. |
| 637 | */ |
| 638 | unsigned find_get_pages(struct address_space *mapping, pgoff_t start, |
| 639 | unsigned int nr_pages, struct page **pages) |
| 640 | { |
| 641 | unsigned int i; |
| 642 | unsigned int ret; |
| 643 | |
| 644 | read_lock_irq(&mapping->tree_lock); |
| 645 | ret = radix_tree_gang_lookup(&mapping->page_tree, |
| 646 | (void **)pages, start, nr_pages); |
| 647 | for (i = 0; i < ret; i++) |
| 648 | page_cache_get(pages[i]); |
| 649 | read_unlock_irq(&mapping->tree_lock); |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Like find_get_pages, except we only return pages which are tagged with |
| 655 | * `tag'. We update *index to index the next page for the traversal. |
| 656 | */ |
| 657 | unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, |
| 658 | int tag, unsigned int nr_pages, struct page **pages) |
| 659 | { |
| 660 | unsigned int i; |
| 661 | unsigned int ret; |
| 662 | |
| 663 | read_lock_irq(&mapping->tree_lock); |
| 664 | ret = radix_tree_gang_lookup_tag(&mapping->page_tree, |
| 665 | (void **)pages, *index, nr_pages, tag); |
| 666 | for (i = 0; i < ret; i++) |
| 667 | page_cache_get(pages[i]); |
| 668 | if (ret) |
| 669 | *index = pages[ret - 1]->index + 1; |
| 670 | read_unlock_irq(&mapping->tree_lock); |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Same as grab_cache_page, but do not wait if the page is unavailable. |
| 676 | * This is intended for speculative data generators, where the data can |
| 677 | * be regenerated if the page couldn't be grabbed. This routine should |
| 678 | * be safe to call while holding the lock for another page. |
| 679 | * |
| 680 | * Clear __GFP_FS when allocating the page to avoid recursion into the fs |
| 681 | * and deadlock against the caller's locked page. |
| 682 | */ |
| 683 | struct page * |
| 684 | grab_cache_page_nowait(struct address_space *mapping, unsigned long index) |
| 685 | { |
| 686 | struct page *page = find_get_page(mapping, index); |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 687 | gfp_t gfp_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | if (page) { |
| 690 | if (!TestSetPageLocked(page)) |
| 691 | return page; |
| 692 | page_cache_release(page); |
| 693 | return NULL; |
| 694 | } |
| 695 | gfp_mask = mapping_gfp_mask(mapping) & ~__GFP_FS; |
| 696 | page = alloc_pages(gfp_mask, 0); |
| 697 | if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) { |
| 698 | page_cache_release(page); |
| 699 | page = NULL; |
| 700 | } |
| 701 | return page; |
| 702 | } |
| 703 | |
| 704 | EXPORT_SYMBOL(grab_cache_page_nowait); |
| 705 | |
| 706 | /* |
| 707 | * This is a generic file read routine, and uses the |
| 708 | * mapping->a_ops->readpage() function for the actual low-level |
| 709 | * stuff. |
| 710 | * |
| 711 | * This is really ugly. But the goto's actually try to clarify some |
| 712 | * of the logic when it comes to error handling etc. |
| 713 | * |
| 714 | * Note the struct file* is only passed for the use of readpage. It may be |
| 715 | * NULL. |
| 716 | */ |
| 717 | void do_generic_mapping_read(struct address_space *mapping, |
| 718 | struct file_ra_state *_ra, |
| 719 | struct file *filp, |
| 720 | loff_t *ppos, |
| 721 | read_descriptor_t *desc, |
| 722 | read_actor_t actor) |
| 723 | { |
| 724 | struct inode *inode = mapping->host; |
| 725 | unsigned long index; |
| 726 | unsigned long end_index; |
| 727 | unsigned long offset; |
| 728 | unsigned long last_index; |
| 729 | unsigned long next_index; |
| 730 | unsigned long prev_index; |
| 731 | loff_t isize; |
| 732 | struct page *cached_page; |
| 733 | int error; |
| 734 | struct file_ra_state ra = *_ra; |
| 735 | |
| 736 | cached_page = NULL; |
| 737 | index = *ppos >> PAGE_CACHE_SHIFT; |
| 738 | next_index = index; |
| 739 | prev_index = ra.prev_page; |
| 740 | last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT; |
| 741 | offset = *ppos & ~PAGE_CACHE_MASK; |
| 742 | |
| 743 | isize = i_size_read(inode); |
| 744 | if (!isize) |
| 745 | goto out; |
| 746 | |
| 747 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 748 | for (;;) { |
| 749 | struct page *page; |
| 750 | unsigned long nr, ret; |
| 751 | |
| 752 | /* nr is the maximum number of bytes to copy from this page */ |
| 753 | nr = PAGE_CACHE_SIZE; |
| 754 | if (index >= end_index) { |
| 755 | if (index > end_index) |
| 756 | goto out; |
| 757 | nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 758 | if (nr <= offset) { |
| 759 | goto out; |
| 760 | } |
| 761 | } |
| 762 | nr = nr - offset; |
| 763 | |
| 764 | cond_resched(); |
| 765 | if (index == next_index) |
| 766 | next_index = page_cache_readahead(mapping, &ra, filp, |
| 767 | index, last_index - index); |
| 768 | |
| 769 | find_page: |
| 770 | page = find_get_page(mapping, index); |
| 771 | if (unlikely(page == NULL)) { |
| 772 | handle_ra_miss(mapping, &ra, index); |
| 773 | goto no_cached_page; |
| 774 | } |
| 775 | if (!PageUptodate(page)) |
| 776 | goto page_not_up_to_date; |
| 777 | page_ok: |
| 778 | |
| 779 | /* If users can be writing to this page using arbitrary |
| 780 | * virtual addresses, take care about potential aliasing |
| 781 | * before reading the page on the kernel side. |
| 782 | */ |
| 783 | if (mapping_writably_mapped(mapping)) |
| 784 | flush_dcache_page(page); |
| 785 | |
| 786 | /* |
| 787 | * When (part of) the same page is read multiple times |
| 788 | * in succession, only mark it as accessed the first time. |
| 789 | */ |
| 790 | if (prev_index != index) |
| 791 | mark_page_accessed(page); |
| 792 | prev_index = index; |
| 793 | |
| 794 | /* |
| 795 | * Ok, we have the page, and it's up-to-date, so |
| 796 | * now we can copy it to user space... |
| 797 | * |
| 798 | * The actor routine returns how many bytes were actually used.. |
| 799 | * NOTE! This may not be the same as how much of a user buffer |
| 800 | * we filled up (we may be padding etc), so we can only update |
| 801 | * "pos" here (the actor routine has to update the user buffer |
| 802 | * pointers and the remaining count). |
| 803 | */ |
| 804 | ret = actor(desc, page, offset, nr); |
| 805 | offset += ret; |
| 806 | index += offset >> PAGE_CACHE_SHIFT; |
| 807 | offset &= ~PAGE_CACHE_MASK; |
| 808 | |
| 809 | page_cache_release(page); |
| 810 | if (ret == nr && desc->count) |
| 811 | continue; |
| 812 | goto out; |
| 813 | |
| 814 | page_not_up_to_date: |
| 815 | /* Get exclusive access to the page ... */ |
| 816 | lock_page(page); |
| 817 | |
| 818 | /* Did it get unhashed before we got the lock? */ |
| 819 | if (!page->mapping) { |
| 820 | unlock_page(page); |
| 821 | page_cache_release(page); |
| 822 | continue; |
| 823 | } |
| 824 | |
| 825 | /* Did somebody else fill it already? */ |
| 826 | if (PageUptodate(page)) { |
| 827 | unlock_page(page); |
| 828 | goto page_ok; |
| 829 | } |
| 830 | |
| 831 | readpage: |
| 832 | /* Start the actual read. The read will unlock the page. */ |
| 833 | error = mapping->a_ops->readpage(filp, page); |
| 834 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 835 | if (unlikely(error)) { |
| 836 | if (error == AOP_TRUNCATED_PAGE) { |
| 837 | page_cache_release(page); |
| 838 | goto find_page; |
| 839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | goto readpage_error; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | if (!PageUptodate(page)) { |
| 844 | lock_page(page); |
| 845 | if (!PageUptodate(page)) { |
| 846 | if (page->mapping == NULL) { |
| 847 | /* |
| 848 | * invalidate_inode_pages got it |
| 849 | */ |
| 850 | unlock_page(page); |
| 851 | page_cache_release(page); |
| 852 | goto find_page; |
| 853 | } |
| 854 | unlock_page(page); |
| 855 | error = -EIO; |
| 856 | goto readpage_error; |
| 857 | } |
| 858 | unlock_page(page); |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * i_size must be checked after we have done ->readpage. |
| 863 | * |
| 864 | * Checking i_size after the readpage allows us to calculate |
| 865 | * the correct value for "nr", which means the zero-filled |
| 866 | * part of the page is not copied back to userspace (unless |
| 867 | * another truncate extends the file - this is desired though). |
| 868 | */ |
| 869 | isize = i_size_read(inode); |
| 870 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 871 | if (unlikely(!isize || index > end_index)) { |
| 872 | page_cache_release(page); |
| 873 | goto out; |
| 874 | } |
| 875 | |
| 876 | /* nr is the maximum number of bytes to copy from this page */ |
| 877 | nr = PAGE_CACHE_SIZE; |
| 878 | if (index == end_index) { |
| 879 | nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 880 | if (nr <= offset) { |
| 881 | page_cache_release(page); |
| 882 | goto out; |
| 883 | } |
| 884 | } |
| 885 | nr = nr - offset; |
| 886 | goto page_ok; |
| 887 | |
| 888 | readpage_error: |
| 889 | /* UHHUH! A synchronous read error occurred. Report it */ |
| 890 | desc->error = error; |
| 891 | page_cache_release(page); |
| 892 | goto out; |
| 893 | |
| 894 | no_cached_page: |
| 895 | /* |
| 896 | * Ok, it wasn't cached, so we need to create a new |
| 897 | * page.. |
| 898 | */ |
| 899 | if (!cached_page) { |
| 900 | cached_page = page_cache_alloc_cold(mapping); |
| 901 | if (!cached_page) { |
| 902 | desc->error = -ENOMEM; |
| 903 | goto out; |
| 904 | } |
| 905 | } |
| 906 | error = add_to_page_cache_lru(cached_page, mapping, |
| 907 | index, GFP_KERNEL); |
| 908 | if (error) { |
| 909 | if (error == -EEXIST) |
| 910 | goto find_page; |
| 911 | desc->error = error; |
| 912 | goto out; |
| 913 | } |
| 914 | page = cached_page; |
| 915 | cached_page = NULL; |
| 916 | goto readpage; |
| 917 | } |
| 918 | |
| 919 | out: |
| 920 | *_ra = ra; |
| 921 | |
| 922 | *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; |
| 923 | if (cached_page) |
| 924 | page_cache_release(cached_page); |
| 925 | if (filp) |
| 926 | file_accessed(filp); |
| 927 | } |
| 928 | |
| 929 | EXPORT_SYMBOL(do_generic_mapping_read); |
| 930 | |
| 931 | int file_read_actor(read_descriptor_t *desc, struct page *page, |
| 932 | unsigned long offset, unsigned long size) |
| 933 | { |
| 934 | char *kaddr; |
| 935 | unsigned long left, count = desc->count; |
| 936 | |
| 937 | if (size > count) |
| 938 | size = count; |
| 939 | |
| 940 | /* |
| 941 | * Faults on the destination of a read are common, so do it before |
| 942 | * taking the kmap. |
| 943 | */ |
| 944 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 945 | kaddr = kmap_atomic(page, KM_USER0); |
| 946 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 947 | kaddr + offset, size); |
| 948 | kunmap_atomic(kaddr, KM_USER0); |
| 949 | if (left == 0) |
| 950 | goto success; |
| 951 | } |
| 952 | |
| 953 | /* Do it the slow way */ |
| 954 | kaddr = kmap(page); |
| 955 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 956 | kunmap(page); |
| 957 | |
| 958 | if (left) { |
| 959 | size -= left; |
| 960 | desc->error = -EFAULT; |
| 961 | } |
| 962 | success: |
| 963 | desc->count = count - size; |
| 964 | desc->written += size; |
| 965 | desc->arg.buf += size; |
| 966 | return size; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * This is the "read()" routine for all filesystems |
| 971 | * that can use the page cache directly. |
| 972 | */ |
| 973 | ssize_t |
| 974 | __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 975 | unsigned long nr_segs, loff_t *ppos) |
| 976 | { |
| 977 | struct file *filp = iocb->ki_filp; |
| 978 | ssize_t retval; |
| 979 | unsigned long seg; |
| 980 | size_t count; |
| 981 | |
| 982 | count = 0; |
| 983 | for (seg = 0; seg < nr_segs; seg++) { |
| 984 | const struct iovec *iv = &iov[seg]; |
| 985 | |
| 986 | /* |
| 987 | * If any segment has a negative length, or the cumulative |
| 988 | * length ever wraps negative then return -EINVAL. |
| 989 | */ |
| 990 | count += iv->iov_len; |
| 991 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 992 | return -EINVAL; |
| 993 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 994 | continue; |
| 995 | if (seg == 0) |
| 996 | return -EFAULT; |
| 997 | nr_segs = seg; |
| 998 | count -= iv->iov_len; /* This segment is no good */ |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 1003 | if (filp->f_flags & O_DIRECT) { |
| 1004 | loff_t pos = *ppos, size; |
| 1005 | struct address_space *mapping; |
| 1006 | struct inode *inode; |
| 1007 | |
| 1008 | mapping = filp->f_mapping; |
| 1009 | inode = mapping->host; |
| 1010 | retval = 0; |
| 1011 | if (!count) |
| 1012 | goto out; /* skip atime */ |
| 1013 | size = i_size_read(inode); |
| 1014 | if (pos < size) { |
| 1015 | retval = generic_file_direct_IO(READ, iocb, |
| 1016 | iov, pos, nr_segs); |
Suparna Bhattacharya | b5c44c2 | 2005-05-21 16:33:36 -0700 | [diff] [blame] | 1017 | if (retval > 0 && !is_sync_kiocb(iocb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | retval = -EIOCBQUEUED; |
| 1019 | if (retval > 0) |
| 1020 | *ppos = pos + retval; |
| 1021 | } |
| 1022 | file_accessed(filp); |
| 1023 | goto out; |
| 1024 | } |
| 1025 | |
| 1026 | retval = 0; |
| 1027 | if (count) { |
| 1028 | for (seg = 0; seg < nr_segs; seg++) { |
| 1029 | read_descriptor_t desc; |
| 1030 | |
| 1031 | desc.written = 0; |
| 1032 | desc.arg.buf = iov[seg].iov_base; |
| 1033 | desc.count = iov[seg].iov_len; |
| 1034 | if (desc.count == 0) |
| 1035 | continue; |
| 1036 | desc.error = 0; |
| 1037 | do_generic_file_read(filp,ppos,&desc,file_read_actor); |
| 1038 | retval += desc.written; |
Tejun Heo | 39e88ca | 2005-10-30 15:02:40 -0800 | [diff] [blame] | 1039 | if (desc.error) { |
| 1040 | retval = retval ?: desc.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | out: |
| 1046 | return retval; |
| 1047 | } |
| 1048 | |
| 1049 | EXPORT_SYMBOL(__generic_file_aio_read); |
| 1050 | |
| 1051 | ssize_t |
| 1052 | generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos) |
| 1053 | { |
| 1054 | struct iovec local_iov = { .iov_base = buf, .iov_len = count }; |
| 1055 | |
| 1056 | BUG_ON(iocb->ki_pos != pos); |
| 1057 | return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos); |
| 1058 | } |
| 1059 | |
| 1060 | EXPORT_SYMBOL(generic_file_aio_read); |
| 1061 | |
| 1062 | ssize_t |
| 1063 | generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) |
| 1064 | { |
| 1065 | struct iovec local_iov = { .iov_base = buf, .iov_len = count }; |
| 1066 | struct kiocb kiocb; |
| 1067 | ssize_t ret; |
| 1068 | |
| 1069 | init_sync_kiocb(&kiocb, filp); |
| 1070 | ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos); |
| 1071 | if (-EIOCBQUEUED == ret) |
| 1072 | ret = wait_on_sync_kiocb(&kiocb); |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | EXPORT_SYMBOL(generic_file_read); |
| 1077 | |
| 1078 | int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size) |
| 1079 | { |
| 1080 | ssize_t written; |
| 1081 | unsigned long count = desc->count; |
| 1082 | struct file *file = desc->arg.data; |
| 1083 | |
| 1084 | if (size > count) |
| 1085 | size = count; |
| 1086 | |
| 1087 | written = file->f_op->sendpage(file, page, offset, |
| 1088 | size, &file->f_pos, size<count); |
| 1089 | if (written < 0) { |
| 1090 | desc->error = written; |
| 1091 | written = 0; |
| 1092 | } |
| 1093 | desc->count = count - written; |
| 1094 | desc->written += written; |
| 1095 | return written; |
| 1096 | } |
| 1097 | |
| 1098 | ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos, |
| 1099 | size_t count, read_actor_t actor, void *target) |
| 1100 | { |
| 1101 | read_descriptor_t desc; |
| 1102 | |
| 1103 | if (!count) |
| 1104 | return 0; |
| 1105 | |
| 1106 | desc.written = 0; |
| 1107 | desc.count = count; |
| 1108 | desc.arg.data = target; |
| 1109 | desc.error = 0; |
| 1110 | |
| 1111 | do_generic_file_read(in_file, ppos, &desc, actor); |
| 1112 | if (desc.written) |
| 1113 | return desc.written; |
| 1114 | return desc.error; |
| 1115 | } |
| 1116 | |
| 1117 | EXPORT_SYMBOL(generic_file_sendfile); |
| 1118 | |
| 1119 | static ssize_t |
| 1120 | do_readahead(struct address_space *mapping, struct file *filp, |
| 1121 | unsigned long index, unsigned long nr) |
| 1122 | { |
| 1123 | if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage) |
| 1124 | return -EINVAL; |
| 1125 | |
| 1126 | force_page_cache_readahead(mapping, filp, index, |
| 1127 | max_sane_readahead(nr)); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count) |
| 1132 | { |
| 1133 | ssize_t ret; |
| 1134 | struct file *file; |
| 1135 | |
| 1136 | ret = -EBADF; |
| 1137 | file = fget(fd); |
| 1138 | if (file) { |
| 1139 | if (file->f_mode & FMODE_READ) { |
| 1140 | struct address_space *mapping = file->f_mapping; |
| 1141 | unsigned long start = offset >> PAGE_CACHE_SHIFT; |
| 1142 | unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT; |
| 1143 | unsigned long len = end - start + 1; |
| 1144 | ret = do_readahead(mapping, file, start, len); |
| 1145 | } |
| 1146 | fput(file); |
| 1147 | } |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | #ifdef CONFIG_MMU |
| 1152 | /* |
| 1153 | * This adds the requested page to the page cache if it isn't already there, |
| 1154 | * and schedules an I/O to read in its contents from disk. |
| 1155 | */ |
| 1156 | static int FASTCALL(page_cache_read(struct file * file, unsigned long offset)); |
| 1157 | static int fastcall page_cache_read(struct file * file, unsigned long offset) |
| 1158 | { |
| 1159 | struct address_space *mapping = file->f_mapping; |
| 1160 | struct page *page; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1161 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1163 | do { |
| 1164 | page = page_cache_alloc_cold(mapping); |
| 1165 | if (!page) |
| 1166 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1168 | ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL); |
| 1169 | if (ret == 0) |
| 1170 | ret = mapping->a_ops->readpage(file, page); |
| 1171 | else if (ret == -EEXIST) |
| 1172 | ret = 0; /* losing race to add is OK */ |
| 1173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1176 | } while (ret == AOP_TRUNCATED_PAGE); |
| 1177 | |
| 1178 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | #define MMAP_LOTSAMISS (100) |
| 1182 | |
| 1183 | /* |
| 1184 | * filemap_nopage() is invoked via the vma operations vector for a |
| 1185 | * mapped memory region to read in file data during a page fault. |
| 1186 | * |
| 1187 | * The goto's are kind of ugly, but this streamlines the normal case of having |
| 1188 | * it in the page cache, and handles the special cases reasonably without |
| 1189 | * having a lot of duplicated code. |
| 1190 | */ |
| 1191 | struct page *filemap_nopage(struct vm_area_struct *area, |
| 1192 | unsigned long address, int *type) |
| 1193 | { |
| 1194 | int error; |
| 1195 | struct file *file = area->vm_file; |
| 1196 | struct address_space *mapping = file->f_mapping; |
| 1197 | struct file_ra_state *ra = &file->f_ra; |
| 1198 | struct inode *inode = mapping->host; |
| 1199 | struct page *page; |
| 1200 | unsigned long size, pgoff; |
| 1201 | int did_readaround = 0, majmin = VM_FAULT_MINOR; |
| 1202 | |
| 1203 | pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff; |
| 1204 | |
| 1205 | retry_all: |
| 1206 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 1207 | if (pgoff >= size) |
| 1208 | goto outside_data_content; |
| 1209 | |
| 1210 | /* If we don't want any read-ahead, don't bother */ |
| 1211 | if (VM_RandomReadHint(area)) |
| 1212 | goto no_cached_page; |
| 1213 | |
| 1214 | /* |
| 1215 | * The readahead code wants to be told about each and every page |
| 1216 | * so it can build and shrink its windows appropriately |
| 1217 | * |
| 1218 | * For sequential accesses, we use the generic readahead logic. |
| 1219 | */ |
| 1220 | if (VM_SequentialReadHint(area)) |
| 1221 | page_cache_readahead(mapping, ra, file, pgoff, 1); |
| 1222 | |
| 1223 | /* |
| 1224 | * Do we have something in the page cache already? |
| 1225 | */ |
| 1226 | retry_find: |
| 1227 | page = find_get_page(mapping, pgoff); |
| 1228 | if (!page) { |
| 1229 | unsigned long ra_pages; |
| 1230 | |
| 1231 | if (VM_SequentialReadHint(area)) { |
| 1232 | handle_ra_miss(mapping, ra, pgoff); |
| 1233 | goto no_cached_page; |
| 1234 | } |
| 1235 | ra->mmap_miss++; |
| 1236 | |
| 1237 | /* |
| 1238 | * Do we miss much more than hit in this file? If so, |
| 1239 | * stop bothering with read-ahead. It will only hurt. |
| 1240 | */ |
| 1241 | if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS) |
| 1242 | goto no_cached_page; |
| 1243 | |
| 1244 | /* |
| 1245 | * To keep the pgmajfault counter straight, we need to |
| 1246 | * check did_readaround, as this is an inner loop. |
| 1247 | */ |
| 1248 | if (!did_readaround) { |
| 1249 | majmin = VM_FAULT_MAJOR; |
| 1250 | inc_page_state(pgmajfault); |
| 1251 | } |
| 1252 | did_readaround = 1; |
| 1253 | ra_pages = max_sane_readahead(file->f_ra.ra_pages); |
| 1254 | if (ra_pages) { |
| 1255 | pgoff_t start = 0; |
| 1256 | |
| 1257 | if (pgoff > ra_pages / 2) |
| 1258 | start = pgoff - ra_pages / 2; |
| 1259 | do_page_cache_readahead(mapping, file, start, ra_pages); |
| 1260 | } |
| 1261 | page = find_get_page(mapping, pgoff); |
| 1262 | if (!page) |
| 1263 | goto no_cached_page; |
| 1264 | } |
| 1265 | |
| 1266 | if (!did_readaround) |
| 1267 | ra->mmap_hit++; |
| 1268 | |
| 1269 | /* |
| 1270 | * Ok, found a page in the page cache, now we need to check |
| 1271 | * that it's up-to-date. |
| 1272 | */ |
| 1273 | if (!PageUptodate(page)) |
| 1274 | goto page_not_uptodate; |
| 1275 | |
| 1276 | success: |
| 1277 | /* |
| 1278 | * Found the page and have a reference on it. |
| 1279 | */ |
| 1280 | mark_page_accessed(page); |
| 1281 | if (type) |
| 1282 | *type = majmin; |
| 1283 | return page; |
| 1284 | |
| 1285 | outside_data_content: |
| 1286 | /* |
| 1287 | * An external ptracer can access pages that normally aren't |
| 1288 | * accessible.. |
| 1289 | */ |
| 1290 | if (area->vm_mm == current->mm) |
| 1291 | return NULL; |
| 1292 | /* Fall through to the non-read-ahead case */ |
| 1293 | no_cached_page: |
| 1294 | /* |
| 1295 | * We're only likely to ever get here if MADV_RANDOM is in |
| 1296 | * effect. |
| 1297 | */ |
| 1298 | error = page_cache_read(file, pgoff); |
| 1299 | grab_swap_token(); |
| 1300 | |
| 1301 | /* |
| 1302 | * The page we want has now been added to the page cache. |
| 1303 | * In the unlikely event that someone removed it in the |
| 1304 | * meantime, we'll just come back here and read it again. |
| 1305 | */ |
| 1306 | if (error >= 0) |
| 1307 | goto retry_find; |
| 1308 | |
| 1309 | /* |
| 1310 | * An error return from page_cache_read can result if the |
| 1311 | * system is low on memory, or a problem occurs while trying |
| 1312 | * to schedule I/O. |
| 1313 | */ |
| 1314 | if (error == -ENOMEM) |
| 1315 | return NOPAGE_OOM; |
| 1316 | return NULL; |
| 1317 | |
| 1318 | page_not_uptodate: |
| 1319 | if (!did_readaround) { |
| 1320 | majmin = VM_FAULT_MAJOR; |
| 1321 | inc_page_state(pgmajfault); |
| 1322 | } |
| 1323 | lock_page(page); |
| 1324 | |
| 1325 | /* Did it get unhashed while we waited for it? */ |
| 1326 | if (!page->mapping) { |
| 1327 | unlock_page(page); |
| 1328 | page_cache_release(page); |
| 1329 | goto retry_all; |
| 1330 | } |
| 1331 | |
| 1332 | /* Did somebody else get it up-to-date? */ |
| 1333 | if (PageUptodate(page)) { |
| 1334 | unlock_page(page); |
| 1335 | goto success; |
| 1336 | } |
| 1337 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1338 | error = mapping->a_ops->readpage(file, page); |
| 1339 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | wait_on_page_locked(page); |
| 1341 | if (PageUptodate(page)) |
| 1342 | goto success; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1343 | } else if (error == AOP_TRUNCATED_PAGE) { |
| 1344 | page_cache_release(page); |
| 1345 | goto retry_find; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Umm, take care of errors if the page isn't up-to-date. |
| 1350 | * Try to re-read it _once_. We do this synchronously, |
| 1351 | * because there really aren't any performance issues here |
| 1352 | * and we need to check for errors. |
| 1353 | */ |
| 1354 | lock_page(page); |
| 1355 | |
| 1356 | /* Somebody truncated the page on us? */ |
| 1357 | if (!page->mapping) { |
| 1358 | unlock_page(page); |
| 1359 | page_cache_release(page); |
| 1360 | goto retry_all; |
| 1361 | } |
| 1362 | |
| 1363 | /* Somebody else successfully read it in? */ |
| 1364 | if (PageUptodate(page)) { |
| 1365 | unlock_page(page); |
| 1366 | goto success; |
| 1367 | } |
| 1368 | ClearPageError(page); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1369 | error = mapping->a_ops->readpage(file, page); |
| 1370 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | wait_on_page_locked(page); |
| 1372 | if (PageUptodate(page)) |
| 1373 | goto success; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1374 | } else if (error == AOP_TRUNCATED_PAGE) { |
| 1375 | page_cache_release(page); |
| 1376 | goto retry_find; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * Things didn't work out. Return zero to tell the |
| 1381 | * mm layer so, possibly freeing the page cache page first. |
| 1382 | */ |
| 1383 | page_cache_release(page); |
| 1384 | return NULL; |
| 1385 | } |
| 1386 | |
| 1387 | EXPORT_SYMBOL(filemap_nopage); |
| 1388 | |
| 1389 | static struct page * filemap_getpage(struct file *file, unsigned long pgoff, |
| 1390 | int nonblock) |
| 1391 | { |
| 1392 | struct address_space *mapping = file->f_mapping; |
| 1393 | struct page *page; |
| 1394 | int error; |
| 1395 | |
| 1396 | /* |
| 1397 | * Do we have something in the page cache already? |
| 1398 | */ |
| 1399 | retry_find: |
| 1400 | page = find_get_page(mapping, pgoff); |
| 1401 | if (!page) { |
| 1402 | if (nonblock) |
| 1403 | return NULL; |
| 1404 | goto no_cached_page; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * Ok, found a page in the page cache, now we need to check |
| 1409 | * that it's up-to-date. |
| 1410 | */ |
Jeff Moyer | d345734 | 2005-04-16 15:24:05 -0700 | [diff] [blame] | 1411 | if (!PageUptodate(page)) { |
| 1412 | if (nonblock) { |
| 1413 | page_cache_release(page); |
| 1414 | return NULL; |
| 1415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | goto page_not_uptodate; |
Jeff Moyer | d345734 | 2005-04-16 15:24:05 -0700 | [diff] [blame] | 1417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
| 1419 | success: |
| 1420 | /* |
| 1421 | * Found the page and have a reference on it. |
| 1422 | */ |
| 1423 | mark_page_accessed(page); |
| 1424 | return page; |
| 1425 | |
| 1426 | no_cached_page: |
| 1427 | error = page_cache_read(file, pgoff); |
| 1428 | |
| 1429 | /* |
| 1430 | * The page we want has now been added to the page cache. |
| 1431 | * In the unlikely event that someone removed it in the |
| 1432 | * meantime, we'll just come back here and read it again. |
| 1433 | */ |
| 1434 | if (error >= 0) |
| 1435 | goto retry_find; |
| 1436 | |
| 1437 | /* |
| 1438 | * An error return from page_cache_read can result if the |
| 1439 | * system is low on memory, or a problem occurs while trying |
| 1440 | * to schedule I/O. |
| 1441 | */ |
| 1442 | return NULL; |
| 1443 | |
| 1444 | page_not_uptodate: |
| 1445 | lock_page(page); |
| 1446 | |
| 1447 | /* Did it get unhashed while we waited for it? */ |
| 1448 | if (!page->mapping) { |
| 1449 | unlock_page(page); |
| 1450 | goto err; |
| 1451 | } |
| 1452 | |
| 1453 | /* Did somebody else get it up-to-date? */ |
| 1454 | if (PageUptodate(page)) { |
| 1455 | unlock_page(page); |
| 1456 | goto success; |
| 1457 | } |
| 1458 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1459 | error = mapping->a_ops->readpage(file, page); |
| 1460 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | wait_on_page_locked(page); |
| 1462 | if (PageUptodate(page)) |
| 1463 | goto success; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1464 | } else if (error == AOP_TRUNCATED_PAGE) { |
| 1465 | page_cache_release(page); |
| 1466 | goto retry_find; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * Umm, take care of errors if the page isn't up-to-date. |
| 1471 | * Try to re-read it _once_. We do this synchronously, |
| 1472 | * because there really aren't any performance issues here |
| 1473 | * and we need to check for errors. |
| 1474 | */ |
| 1475 | lock_page(page); |
| 1476 | |
| 1477 | /* Somebody truncated the page on us? */ |
| 1478 | if (!page->mapping) { |
| 1479 | unlock_page(page); |
| 1480 | goto err; |
| 1481 | } |
| 1482 | /* Somebody else successfully read it in? */ |
| 1483 | if (PageUptodate(page)) { |
| 1484 | unlock_page(page); |
| 1485 | goto success; |
| 1486 | } |
| 1487 | |
| 1488 | ClearPageError(page); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1489 | error = mapping->a_ops->readpage(file, page); |
| 1490 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | wait_on_page_locked(page); |
| 1492 | if (PageUptodate(page)) |
| 1493 | goto success; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1494 | } else if (error == AOP_TRUNCATED_PAGE) { |
| 1495 | page_cache_release(page); |
| 1496 | goto retry_find; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * Things didn't work out. Return zero to tell the |
| 1501 | * mm layer so, possibly freeing the page cache page first. |
| 1502 | */ |
| 1503 | err: |
| 1504 | page_cache_release(page); |
| 1505 | |
| 1506 | return NULL; |
| 1507 | } |
| 1508 | |
| 1509 | int filemap_populate(struct vm_area_struct *vma, unsigned long addr, |
| 1510 | unsigned long len, pgprot_t prot, unsigned long pgoff, |
| 1511 | int nonblock) |
| 1512 | { |
| 1513 | struct file *file = vma->vm_file; |
| 1514 | struct address_space *mapping = file->f_mapping; |
| 1515 | struct inode *inode = mapping->host; |
| 1516 | unsigned long size; |
| 1517 | struct mm_struct *mm = vma->vm_mm; |
| 1518 | struct page *page; |
| 1519 | int err; |
| 1520 | |
| 1521 | if (!nonblock) |
| 1522 | force_page_cache_readahead(mapping, vma->vm_file, |
| 1523 | pgoff, len >> PAGE_CACHE_SHIFT); |
| 1524 | |
| 1525 | repeat: |
| 1526 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 1527 | if (pgoff + (len >> PAGE_CACHE_SHIFT) > size) |
| 1528 | return -EINVAL; |
| 1529 | |
| 1530 | page = filemap_getpage(file, pgoff, nonblock); |
Paolo 'Blaisorblade' Giarrusso | d44ed4f | 2005-09-03 15:54:55 -0700 | [diff] [blame] | 1531 | |
| 1532 | /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as |
| 1533 | * done in shmem_populate calling shmem_getpage */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | if (!page && !nonblock) |
| 1535 | return -ENOMEM; |
Paolo 'Blaisorblade' Giarrusso | d44ed4f | 2005-09-03 15:54:55 -0700 | [diff] [blame] | 1536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | if (page) { |
| 1538 | err = install_page(mm, vma, addr, page, prot); |
| 1539 | if (err) { |
| 1540 | page_cache_release(page); |
| 1541 | return err; |
| 1542 | } |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 1543 | } else if (vma->vm_flags & VM_NONLINEAR) { |
Paolo 'Blaisorblade' Giarrusso | d44ed4f | 2005-09-03 15:54:55 -0700 | [diff] [blame] | 1544 | /* No page was found just because we can't read it in now (being |
| 1545 | * here implies nonblock != 0), but the page may exist, so set |
| 1546 | * the PTE to fault it in later. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | err = install_file_pte(mm, vma, addr, pgoff, prot); |
| 1548 | if (err) |
| 1549 | return err; |
| 1550 | } |
| 1551 | |
| 1552 | len -= PAGE_SIZE; |
| 1553 | addr += PAGE_SIZE; |
| 1554 | pgoff++; |
| 1555 | if (len) |
| 1556 | goto repeat; |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
Nikita Danilov | b145946 | 2005-10-29 18:17:02 -0700 | [diff] [blame] | 1560 | EXPORT_SYMBOL(filemap_populate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
| 1562 | struct vm_operations_struct generic_file_vm_ops = { |
| 1563 | .nopage = filemap_nopage, |
| 1564 | .populate = filemap_populate, |
| 1565 | }; |
| 1566 | |
| 1567 | /* This is used for a general mmap of a disk file */ |
| 1568 | |
| 1569 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 1570 | { |
| 1571 | struct address_space *mapping = file->f_mapping; |
| 1572 | |
| 1573 | if (!mapping->a_ops->readpage) |
| 1574 | return -ENOEXEC; |
| 1575 | file_accessed(file); |
| 1576 | vma->vm_ops = &generic_file_vm_ops; |
| 1577 | return 0; |
| 1578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
| 1580 | /* |
| 1581 | * This is for filesystems which do not implement ->writepage. |
| 1582 | */ |
| 1583 | int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) |
| 1584 | { |
| 1585 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 1586 | return -EINVAL; |
| 1587 | return generic_file_mmap(file, vma); |
| 1588 | } |
| 1589 | #else |
| 1590 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 1591 | { |
| 1592 | return -ENOSYS; |
| 1593 | } |
| 1594 | int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma) |
| 1595 | { |
| 1596 | return -ENOSYS; |
| 1597 | } |
| 1598 | #endif /* CONFIG_MMU */ |
| 1599 | |
| 1600 | EXPORT_SYMBOL(generic_file_mmap); |
| 1601 | EXPORT_SYMBOL(generic_file_readonly_mmap); |
| 1602 | |
| 1603 | static inline struct page *__read_cache_page(struct address_space *mapping, |
| 1604 | unsigned long index, |
| 1605 | int (*filler)(void *,struct page*), |
| 1606 | void *data) |
| 1607 | { |
| 1608 | struct page *page, *cached_page = NULL; |
| 1609 | int err; |
| 1610 | repeat: |
| 1611 | page = find_get_page(mapping, index); |
| 1612 | if (!page) { |
| 1613 | if (!cached_page) { |
| 1614 | cached_page = page_cache_alloc_cold(mapping); |
| 1615 | if (!cached_page) |
| 1616 | return ERR_PTR(-ENOMEM); |
| 1617 | } |
| 1618 | err = add_to_page_cache_lru(cached_page, mapping, |
| 1619 | index, GFP_KERNEL); |
| 1620 | if (err == -EEXIST) |
| 1621 | goto repeat; |
| 1622 | if (err < 0) { |
| 1623 | /* Presumably ENOMEM for radix tree node */ |
| 1624 | page_cache_release(cached_page); |
| 1625 | return ERR_PTR(err); |
| 1626 | } |
| 1627 | page = cached_page; |
| 1628 | cached_page = NULL; |
| 1629 | err = filler(data, page); |
| 1630 | if (err < 0) { |
| 1631 | page_cache_release(page); |
| 1632 | page = ERR_PTR(err); |
| 1633 | } |
| 1634 | } |
| 1635 | if (cached_page) |
| 1636 | page_cache_release(cached_page); |
| 1637 | return page; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Read into the page cache. If a page already exists, |
| 1642 | * and PageUptodate() is not set, try to fill the page. |
| 1643 | */ |
| 1644 | struct page *read_cache_page(struct address_space *mapping, |
| 1645 | unsigned long index, |
| 1646 | int (*filler)(void *,struct page*), |
| 1647 | void *data) |
| 1648 | { |
| 1649 | struct page *page; |
| 1650 | int err; |
| 1651 | |
| 1652 | retry: |
| 1653 | page = __read_cache_page(mapping, index, filler, data); |
| 1654 | if (IS_ERR(page)) |
| 1655 | goto out; |
| 1656 | mark_page_accessed(page); |
| 1657 | if (PageUptodate(page)) |
| 1658 | goto out; |
| 1659 | |
| 1660 | lock_page(page); |
| 1661 | if (!page->mapping) { |
| 1662 | unlock_page(page); |
| 1663 | page_cache_release(page); |
| 1664 | goto retry; |
| 1665 | } |
| 1666 | if (PageUptodate(page)) { |
| 1667 | unlock_page(page); |
| 1668 | goto out; |
| 1669 | } |
| 1670 | err = filler(data, page); |
| 1671 | if (err < 0) { |
| 1672 | page_cache_release(page); |
| 1673 | page = ERR_PTR(err); |
| 1674 | } |
| 1675 | out: |
| 1676 | return page; |
| 1677 | } |
| 1678 | |
| 1679 | EXPORT_SYMBOL(read_cache_page); |
| 1680 | |
| 1681 | /* |
| 1682 | * If the page was newly created, increment its refcount and add it to the |
| 1683 | * caller's lru-buffering pagevec. This function is specifically for |
| 1684 | * generic_file_write(). |
| 1685 | */ |
| 1686 | static inline struct page * |
| 1687 | __grab_cache_page(struct address_space *mapping, unsigned long index, |
| 1688 | struct page **cached_page, struct pagevec *lru_pvec) |
| 1689 | { |
| 1690 | int err; |
| 1691 | struct page *page; |
| 1692 | repeat: |
| 1693 | page = find_lock_page(mapping, index); |
| 1694 | if (!page) { |
| 1695 | if (!*cached_page) { |
| 1696 | *cached_page = page_cache_alloc(mapping); |
| 1697 | if (!*cached_page) |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | err = add_to_page_cache(*cached_page, mapping, |
| 1701 | index, GFP_KERNEL); |
| 1702 | if (err == -EEXIST) |
| 1703 | goto repeat; |
| 1704 | if (err == 0) { |
| 1705 | page = *cached_page; |
| 1706 | page_cache_get(page); |
| 1707 | if (!pagevec_add(lru_pvec, page)) |
| 1708 | __pagevec_lru_add(lru_pvec); |
| 1709 | *cached_page = NULL; |
| 1710 | } |
| 1711 | } |
| 1712 | return page; |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | * The logic we want is |
| 1717 | * |
| 1718 | * if suid or (sgid and xgrp) |
| 1719 | * remove privs |
| 1720 | */ |
| 1721 | int remove_suid(struct dentry *dentry) |
| 1722 | { |
| 1723 | mode_t mode = dentry->d_inode->i_mode; |
| 1724 | int kill = 0; |
| 1725 | int result = 0; |
| 1726 | |
| 1727 | /* suid always must be killed */ |
| 1728 | if (unlikely(mode & S_ISUID)) |
| 1729 | kill = ATTR_KILL_SUID; |
| 1730 | |
| 1731 | /* |
| 1732 | * sgid without any exec bits is just a mandatory locking mark; leave |
| 1733 | * it alone. If some exec bits are set, it's a real sgid; kill it. |
| 1734 | */ |
| 1735 | if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) |
| 1736 | kill |= ATTR_KILL_SGID; |
| 1737 | |
| 1738 | if (unlikely(kill && !capable(CAP_FSETID))) { |
| 1739 | struct iattr newattrs; |
| 1740 | |
| 1741 | newattrs.ia_valid = ATTR_FORCE | kill; |
| 1742 | result = notify_change(dentry, &newattrs); |
| 1743 | } |
| 1744 | return result; |
| 1745 | } |
| 1746 | EXPORT_SYMBOL(remove_suid); |
| 1747 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 1748 | size_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | __filemap_copy_from_user_iovec(char *vaddr, |
| 1750 | const struct iovec *iov, size_t base, size_t bytes) |
| 1751 | { |
| 1752 | size_t copied = 0, left = 0; |
| 1753 | |
| 1754 | while (bytes) { |
| 1755 | char __user *buf = iov->iov_base + base; |
| 1756 | int copy = min(bytes, iov->iov_len - base); |
| 1757 | |
| 1758 | base = 0; |
| 1759 | left = __copy_from_user_inatomic(vaddr, buf, copy); |
| 1760 | copied += copy; |
| 1761 | bytes -= copy; |
| 1762 | vaddr += copy; |
| 1763 | iov++; |
| 1764 | |
| 1765 | if (unlikely(left)) { |
| 1766 | /* zero the rest of the target like __copy_from_user */ |
| 1767 | if (bytes) |
| 1768 | memset(vaddr, 0, bytes); |
| 1769 | break; |
| 1770 | } |
| 1771 | } |
| 1772 | return copied - left; |
| 1773 | } |
| 1774 | |
| 1775 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | * Performs necessary checks before doing a write |
| 1777 | * |
| 1778 | * Can adjust writing position aor amount of bytes to write. |
| 1779 | * Returns appropriate error code that caller should return or |
| 1780 | * zero in case that write should be allowed. |
| 1781 | */ |
| 1782 | inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk) |
| 1783 | { |
| 1784 | struct inode *inode = file->f_mapping->host; |
| 1785 | unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 1786 | |
| 1787 | if (unlikely(*pos < 0)) |
| 1788 | return -EINVAL; |
| 1789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | if (!isblk) { |
| 1791 | /* FIXME: this is for backwards compatibility with 2.4 */ |
| 1792 | if (file->f_flags & O_APPEND) |
| 1793 | *pos = i_size_read(inode); |
| 1794 | |
| 1795 | if (limit != RLIM_INFINITY) { |
| 1796 | if (*pos >= limit) { |
| 1797 | send_sig(SIGXFSZ, current, 0); |
| 1798 | return -EFBIG; |
| 1799 | } |
| 1800 | if (*count > limit - (typeof(limit))*pos) { |
| 1801 | *count = limit - (typeof(limit))*pos; |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * LFS rule |
| 1808 | */ |
| 1809 | if (unlikely(*pos + *count > MAX_NON_LFS && |
| 1810 | !(file->f_flags & O_LARGEFILE))) { |
| 1811 | if (*pos >= MAX_NON_LFS) { |
| 1812 | send_sig(SIGXFSZ, current, 0); |
| 1813 | return -EFBIG; |
| 1814 | } |
| 1815 | if (*count > MAX_NON_LFS - (unsigned long)*pos) { |
| 1816 | *count = MAX_NON_LFS - (unsigned long)*pos; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | /* |
| 1821 | * Are we about to exceed the fs block limit ? |
| 1822 | * |
| 1823 | * If we have written data it becomes a short write. If we have |
| 1824 | * exceeded without writing data we send a signal and return EFBIG. |
| 1825 | * Linus frestrict idea will clean these up nicely.. |
| 1826 | */ |
| 1827 | if (likely(!isblk)) { |
| 1828 | if (unlikely(*pos >= inode->i_sb->s_maxbytes)) { |
| 1829 | if (*count || *pos > inode->i_sb->s_maxbytes) { |
| 1830 | send_sig(SIGXFSZ, current, 0); |
| 1831 | return -EFBIG; |
| 1832 | } |
| 1833 | /* zero-length writes at ->s_maxbytes are OK */ |
| 1834 | } |
| 1835 | |
| 1836 | if (unlikely(*pos + *count > inode->i_sb->s_maxbytes)) |
| 1837 | *count = inode->i_sb->s_maxbytes - *pos; |
| 1838 | } else { |
| 1839 | loff_t isize; |
| 1840 | if (bdev_read_only(I_BDEV(inode))) |
| 1841 | return -EPERM; |
| 1842 | isize = i_size_read(inode); |
| 1843 | if (*pos >= isize) { |
| 1844 | if (*count || *pos > isize) |
| 1845 | return -ENOSPC; |
| 1846 | } |
| 1847 | |
| 1848 | if (*pos + *count > isize) |
| 1849 | *count = isize - *pos; |
| 1850 | } |
| 1851 | return 0; |
| 1852 | } |
| 1853 | EXPORT_SYMBOL(generic_write_checks); |
| 1854 | |
| 1855 | ssize_t |
| 1856 | generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov, |
| 1857 | unsigned long *nr_segs, loff_t pos, loff_t *ppos, |
| 1858 | size_t count, size_t ocount) |
| 1859 | { |
| 1860 | struct file *file = iocb->ki_filp; |
| 1861 | struct address_space *mapping = file->f_mapping; |
| 1862 | struct inode *inode = mapping->host; |
| 1863 | ssize_t written; |
| 1864 | |
| 1865 | if (count != ocount) |
| 1866 | *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count); |
| 1867 | |
| 1868 | written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs); |
| 1869 | if (written > 0) { |
| 1870 | loff_t end = pos + written; |
| 1871 | if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { |
| 1872 | i_size_write(inode, end); |
| 1873 | mark_inode_dirty(inode); |
| 1874 | } |
| 1875 | *ppos = end; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Sync the fs metadata but not the minor inode changes and |
| 1880 | * of course not the data as we did direct DMA for the IO. |
| 1881 | * i_sem is held, which protects generic_osync_inode() from |
| 1882 | * livelocking. |
| 1883 | */ |
Hifumi Hisashi | 1e8a81c | 2005-06-25 14:54:32 -0700 | [diff] [blame] | 1884 | if (written >= 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 1885 | int err = generic_osync_inode(inode, mapping, OSYNC_METADATA); |
| 1886 | if (err < 0) |
| 1887 | written = err; |
| 1888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | if (written == count && !is_sync_kiocb(iocb)) |
| 1890 | written = -EIOCBQUEUED; |
| 1891 | return written; |
| 1892 | } |
| 1893 | EXPORT_SYMBOL(generic_file_direct_write); |
| 1894 | |
| 1895 | ssize_t |
| 1896 | generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov, |
| 1897 | unsigned long nr_segs, loff_t pos, loff_t *ppos, |
| 1898 | size_t count, ssize_t written) |
| 1899 | { |
| 1900 | struct file *file = iocb->ki_filp; |
| 1901 | struct address_space * mapping = file->f_mapping; |
| 1902 | struct address_space_operations *a_ops = mapping->a_ops; |
| 1903 | struct inode *inode = mapping->host; |
| 1904 | long status = 0; |
| 1905 | struct page *page; |
| 1906 | struct page *cached_page = NULL; |
| 1907 | size_t bytes; |
| 1908 | struct pagevec lru_pvec; |
| 1909 | const struct iovec *cur_iov = iov; /* current iovec */ |
| 1910 | size_t iov_base = 0; /* offset in the current iovec */ |
| 1911 | char __user *buf; |
| 1912 | |
| 1913 | pagevec_init(&lru_pvec, 0); |
| 1914 | |
| 1915 | /* |
| 1916 | * handle partial DIO write. Adjust cur_iov if needed. |
| 1917 | */ |
| 1918 | if (likely(nr_segs == 1)) |
| 1919 | buf = iov->iov_base + written; |
| 1920 | else { |
| 1921 | filemap_set_next_iovec(&cur_iov, &iov_base, written); |
akpm@osdl.org | f021e92 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 1922 | buf = cur_iov->iov_base + iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | do { |
| 1926 | unsigned long index; |
| 1927 | unsigned long offset; |
Martin Schwidefsky | a511718 | 2005-06-06 13:35:54 -0700 | [diff] [blame] | 1928 | unsigned long maxlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | size_t copied; |
| 1930 | |
| 1931 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ |
| 1932 | index = pos >> PAGE_CACHE_SHIFT; |
| 1933 | bytes = PAGE_CACHE_SIZE - offset; |
| 1934 | if (bytes > count) |
| 1935 | bytes = count; |
| 1936 | |
| 1937 | /* |
| 1938 | * Bring in the user page that we will copy from _first_. |
| 1939 | * Otherwise there's a nasty deadlock on copying from the |
| 1940 | * same page as we're writing to, without it being marked |
| 1941 | * up-to-date. |
| 1942 | */ |
Martin Schwidefsky | a511718 | 2005-06-06 13:35:54 -0700 | [diff] [blame] | 1943 | maxlen = cur_iov->iov_len - iov_base; |
| 1944 | if (maxlen > bytes) |
| 1945 | maxlen = bytes; |
| 1946 | fault_in_pages_readable(buf, maxlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
| 1948 | page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec); |
| 1949 | if (!page) { |
| 1950 | status = -ENOMEM; |
| 1951 | break; |
| 1952 | } |
| 1953 | |
| 1954 | status = a_ops->prepare_write(file, page, offset, offset+bytes); |
| 1955 | if (unlikely(status)) { |
| 1956 | loff_t isize = i_size_read(inode); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1957 | |
| 1958 | if (status != AOP_TRUNCATED_PAGE) |
| 1959 | unlock_page(page); |
| 1960 | page_cache_release(page); |
| 1961 | if (status == AOP_TRUNCATED_PAGE) |
| 1962 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | /* |
| 1964 | * prepare_write() may have instantiated a few blocks |
| 1965 | * outside i_size. Trim these off again. |
| 1966 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | if (pos + bytes > isize) |
| 1968 | vmtruncate(inode, isize); |
| 1969 | break; |
| 1970 | } |
| 1971 | if (likely(nr_segs == 1)) |
| 1972 | copied = filemap_copy_from_user(page, offset, |
| 1973 | buf, bytes); |
| 1974 | else |
| 1975 | copied = filemap_copy_from_user_iovec(page, offset, |
| 1976 | cur_iov, iov_base, bytes); |
| 1977 | flush_dcache_page(page); |
| 1978 | status = a_ops->commit_write(file, page, offset, offset+bytes); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 1979 | if (status == AOP_TRUNCATED_PAGE) { |
| 1980 | page_cache_release(page); |
| 1981 | continue; |
| 1982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | if (likely(copied > 0)) { |
| 1984 | if (!status) |
| 1985 | status = copied; |
| 1986 | |
| 1987 | if (status >= 0) { |
| 1988 | written += status; |
| 1989 | count -= status; |
| 1990 | pos += status; |
| 1991 | buf += status; |
akpm@osdl.org | f021e92 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 1992 | if (unlikely(nr_segs > 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | filemap_set_next_iovec(&cur_iov, |
| 1994 | &iov_base, status); |
Badari Pulavarty | b0cfbd9 | 2005-06-25 14:55:42 -0700 | [diff] [blame] | 1995 | if (count) |
| 1996 | buf = cur_iov->iov_base + |
| 1997 | iov_base; |
Martin Schwidefsky | a511718 | 2005-06-06 13:35:54 -0700 | [diff] [blame] | 1998 | } else { |
| 1999 | iov_base += status; |
akpm@osdl.org | f021e92 | 2005-05-01 08:58:35 -0700 | [diff] [blame] | 2000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | if (unlikely(copied != bytes)) |
| 2004 | if (status >= 0) |
| 2005 | status = -EFAULT; |
| 2006 | unlock_page(page); |
| 2007 | mark_page_accessed(page); |
| 2008 | page_cache_release(page); |
| 2009 | if (status < 0) |
| 2010 | break; |
| 2011 | balance_dirty_pages_ratelimited(mapping); |
| 2012 | cond_resched(); |
| 2013 | } while (count); |
| 2014 | *ppos = pos; |
| 2015 | |
| 2016 | if (cached_page) |
| 2017 | page_cache_release(cached_page); |
| 2018 | |
| 2019 | /* |
| 2020 | * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC |
| 2021 | */ |
| 2022 | if (likely(status >= 0)) { |
| 2023 | if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2024 | if (!a_ops->writepage || !is_sync_kiocb(iocb)) |
| 2025 | status = generic_osync_inode(inode, mapping, |
| 2026 | OSYNC_METADATA|OSYNC_DATA); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | * If we get here for O_DIRECT writes then we must have fallen through |
| 2032 | * to buffered writes (block instantiation inside i_size). So we sync |
| 2033 | * the file data here, to try to honour O_DIRECT expectations. |
| 2034 | */ |
| 2035 | if (unlikely(file->f_flags & O_DIRECT) && written) |
| 2036 | status = filemap_write_and_wait(mapping); |
| 2037 | |
| 2038 | pagevec_lru_add(&lru_pvec); |
| 2039 | return written ? written : status; |
| 2040 | } |
| 2041 | EXPORT_SYMBOL(generic_file_buffered_write); |
| 2042 | |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 2043 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov, |
| 2045 | unsigned long nr_segs, loff_t *ppos) |
| 2046 | { |
| 2047 | struct file *file = iocb->ki_filp; |
| 2048 | struct address_space * mapping = file->f_mapping; |
| 2049 | size_t ocount; /* original count */ |
| 2050 | size_t count; /* after file limit checks */ |
| 2051 | struct inode *inode = mapping->host; |
| 2052 | unsigned long seg; |
| 2053 | loff_t pos; |
| 2054 | ssize_t written; |
| 2055 | ssize_t err; |
| 2056 | |
| 2057 | ocount = 0; |
| 2058 | for (seg = 0; seg < nr_segs; seg++) { |
| 2059 | const struct iovec *iv = &iov[seg]; |
| 2060 | |
| 2061 | /* |
| 2062 | * If any segment has a negative length, or the cumulative |
| 2063 | * length ever wraps negative then return -EINVAL. |
| 2064 | */ |
| 2065 | ocount += iv->iov_len; |
| 2066 | if (unlikely((ssize_t)(ocount|iv->iov_len) < 0)) |
| 2067 | return -EINVAL; |
| 2068 | if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len)) |
| 2069 | continue; |
| 2070 | if (seg == 0) |
| 2071 | return -EFAULT; |
| 2072 | nr_segs = seg; |
| 2073 | ocount -= iv->iov_len; /* This segment is no good */ |
| 2074 | break; |
| 2075 | } |
| 2076 | |
| 2077 | count = ocount; |
| 2078 | pos = *ppos; |
| 2079 | |
| 2080 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 2081 | |
| 2082 | /* We can write back this queue in page reclaim */ |
| 2083 | current->backing_dev_info = mapping->backing_dev_info; |
| 2084 | written = 0; |
| 2085 | |
| 2086 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 2087 | if (err) |
| 2088 | goto out; |
| 2089 | |
| 2090 | if (count == 0) |
| 2091 | goto out; |
| 2092 | |
| 2093 | err = remove_suid(file->f_dentry); |
| 2094 | if (err) |
| 2095 | goto out; |
| 2096 | |
| 2097 | inode_update_time(inode, 1); |
| 2098 | |
| 2099 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 2100 | if (unlikely(file->f_flags & O_DIRECT)) { |
| 2101 | written = generic_file_direct_write(iocb, iov, |
| 2102 | &nr_segs, pos, ppos, count, ocount); |
| 2103 | if (written < 0 || written == count) |
| 2104 | goto out; |
| 2105 | /* |
| 2106 | * direct-io write to a hole: fall through to buffered I/O |
| 2107 | * for completing the rest of the request. |
| 2108 | */ |
| 2109 | pos += written; |
| 2110 | count -= written; |
| 2111 | } |
| 2112 | |
| 2113 | written = generic_file_buffered_write(iocb, iov, nr_segs, |
| 2114 | pos, ppos, count, written); |
| 2115 | out: |
| 2116 | current->backing_dev_info = NULL; |
| 2117 | return written ? written : err; |
| 2118 | } |
| 2119 | EXPORT_SYMBOL(generic_file_aio_write_nolock); |
| 2120 | |
| 2121 | ssize_t |
| 2122 | generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov, |
| 2123 | unsigned long nr_segs, loff_t *ppos) |
| 2124 | { |
| 2125 | struct file *file = iocb->ki_filp; |
| 2126 | struct address_space *mapping = file->f_mapping; |
| 2127 | struct inode *inode = mapping->host; |
| 2128 | ssize_t ret; |
| 2129 | loff_t pos = *ppos; |
| 2130 | |
| 2131 | ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos); |
| 2132 | |
| 2133 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2134 | int err; |
| 2135 | |
| 2136 | err = sync_page_range_nolock(inode, mapping, pos, ret); |
| 2137 | if (err < 0) |
| 2138 | ret = err; |
| 2139 | } |
| 2140 | return ret; |
| 2141 | } |
| 2142 | |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 2143 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | __generic_file_write_nolock(struct file *file, const struct iovec *iov, |
| 2145 | unsigned long nr_segs, loff_t *ppos) |
| 2146 | { |
| 2147 | struct kiocb kiocb; |
| 2148 | ssize_t ret; |
| 2149 | |
| 2150 | init_sync_kiocb(&kiocb, file); |
| 2151 | ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos); |
| 2152 | if (ret == -EIOCBQUEUED) |
| 2153 | ret = wait_on_sync_kiocb(&kiocb); |
| 2154 | return ret; |
| 2155 | } |
| 2156 | |
| 2157 | ssize_t |
| 2158 | generic_file_write_nolock(struct file *file, const struct iovec *iov, |
| 2159 | unsigned long nr_segs, loff_t *ppos) |
| 2160 | { |
| 2161 | struct kiocb kiocb; |
| 2162 | ssize_t ret; |
| 2163 | |
| 2164 | init_sync_kiocb(&kiocb, file); |
| 2165 | ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos); |
| 2166 | if (-EIOCBQUEUED == ret) |
| 2167 | ret = wait_on_sync_kiocb(&kiocb); |
| 2168 | return ret; |
| 2169 | } |
| 2170 | EXPORT_SYMBOL(generic_file_write_nolock); |
| 2171 | |
| 2172 | ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf, |
| 2173 | size_t count, loff_t pos) |
| 2174 | { |
| 2175 | struct file *file = iocb->ki_filp; |
| 2176 | struct address_space *mapping = file->f_mapping; |
| 2177 | struct inode *inode = mapping->host; |
| 2178 | ssize_t ret; |
| 2179 | struct iovec local_iov = { .iov_base = (void __user *)buf, |
| 2180 | .iov_len = count }; |
| 2181 | |
| 2182 | BUG_ON(iocb->ki_pos != pos); |
| 2183 | |
| 2184 | down(&inode->i_sem); |
| 2185 | ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1, |
| 2186 | &iocb->ki_pos); |
| 2187 | up(&inode->i_sem); |
| 2188 | |
| 2189 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2190 | ssize_t err; |
| 2191 | |
| 2192 | err = sync_page_range(inode, mapping, pos, ret); |
| 2193 | if (err < 0) |
| 2194 | ret = err; |
| 2195 | } |
| 2196 | return ret; |
| 2197 | } |
| 2198 | EXPORT_SYMBOL(generic_file_aio_write); |
| 2199 | |
| 2200 | ssize_t generic_file_write(struct file *file, const char __user *buf, |
| 2201 | size_t count, loff_t *ppos) |
| 2202 | { |
| 2203 | struct address_space *mapping = file->f_mapping; |
| 2204 | struct inode *inode = mapping->host; |
| 2205 | ssize_t ret; |
| 2206 | struct iovec local_iov = { .iov_base = (void __user *)buf, |
| 2207 | .iov_len = count }; |
| 2208 | |
| 2209 | down(&inode->i_sem); |
| 2210 | ret = __generic_file_write_nolock(file, &local_iov, 1, ppos); |
| 2211 | up(&inode->i_sem); |
| 2212 | |
| 2213 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2214 | ssize_t err; |
| 2215 | |
| 2216 | err = sync_page_range(inode, mapping, *ppos - ret, ret); |
| 2217 | if (err < 0) |
| 2218 | ret = err; |
| 2219 | } |
| 2220 | return ret; |
| 2221 | } |
| 2222 | EXPORT_SYMBOL(generic_file_write); |
| 2223 | |
| 2224 | ssize_t generic_file_readv(struct file *filp, const struct iovec *iov, |
| 2225 | unsigned long nr_segs, loff_t *ppos) |
| 2226 | { |
| 2227 | struct kiocb kiocb; |
| 2228 | ssize_t ret; |
| 2229 | |
| 2230 | init_sync_kiocb(&kiocb, filp); |
| 2231 | ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos); |
| 2232 | if (-EIOCBQUEUED == ret) |
| 2233 | ret = wait_on_sync_kiocb(&kiocb); |
| 2234 | return ret; |
| 2235 | } |
| 2236 | EXPORT_SYMBOL(generic_file_readv); |
| 2237 | |
| 2238 | ssize_t generic_file_writev(struct file *file, const struct iovec *iov, |
| 2239 | unsigned long nr_segs, loff_t *ppos) |
| 2240 | { |
| 2241 | struct address_space *mapping = file->f_mapping; |
| 2242 | struct inode *inode = mapping->host; |
| 2243 | ssize_t ret; |
| 2244 | |
| 2245 | down(&inode->i_sem); |
| 2246 | ret = __generic_file_write_nolock(file, iov, nr_segs, ppos); |
| 2247 | up(&inode->i_sem); |
| 2248 | |
| 2249 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 2250 | int err; |
| 2251 | |
| 2252 | err = sync_page_range(inode, mapping, *ppos - ret, ret); |
| 2253 | if (err < 0) |
| 2254 | ret = err; |
| 2255 | } |
| 2256 | return ret; |
| 2257 | } |
| 2258 | EXPORT_SYMBOL(generic_file_writev); |
| 2259 | |
| 2260 | /* |
| 2261 | * Called under i_sem for writes to S_ISREG files. Returns -EIO if something |
| 2262 | * went wrong during pagecache shootdown. |
| 2263 | */ |
Adrian Bunk | 5ce7852 | 2005-09-10 00:26:28 -0700 | [diff] [blame] | 2264 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 2266 | loff_t offset, unsigned long nr_segs) |
| 2267 | { |
| 2268 | struct file *file = iocb->ki_filp; |
| 2269 | struct address_space *mapping = file->f_mapping; |
| 2270 | ssize_t retval; |
| 2271 | size_t write_len = 0; |
| 2272 | |
| 2273 | /* |
| 2274 | * If it's a write, unmap all mmappings of the file up-front. This |
| 2275 | * will cause any pte dirty bits to be propagated into the pageframes |
| 2276 | * for the subsequent filemap_write_and_wait(). |
| 2277 | */ |
| 2278 | if (rw == WRITE) { |
| 2279 | write_len = iov_length(iov, nr_segs); |
| 2280 | if (mapping_mapped(mapping)) |
| 2281 | unmap_mapping_range(mapping, offset, write_len, 0); |
| 2282 | } |
| 2283 | |
| 2284 | retval = filemap_write_and_wait(mapping); |
| 2285 | if (retval == 0) { |
| 2286 | retval = mapping->a_ops->direct_IO(rw, iocb, iov, |
| 2287 | offset, nr_segs); |
| 2288 | if (rw == WRITE && mapping->nrpages) { |
| 2289 | pgoff_t end = (offset + write_len - 1) |
| 2290 | >> PAGE_CACHE_SHIFT; |
| 2291 | int err = invalidate_inode_pages2_range(mapping, |
| 2292 | offset >> PAGE_CACHE_SHIFT, end); |
| 2293 | if (err) |
| 2294 | retval = err; |
| 2295 | } |
| 2296 | } |
| 2297 | return retval; |
| 2298 | } |