[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / mm / filemap.c
blob6fbd980c25e5ff3a0f48559191fd20c0ff474541
1 /*
2 * linux/mm/filemap.c
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
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)
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>
31 * This is needed for the following functions:
32 * - try_to_release_page
33 * - block_invalidatepage
34 * - generic_osync_inode
36 * FIXME: remove all knowledge of the buffer layer from the core VM
38 #include <linux/buffer_head.h> /* for generic_osync_inode */
40 #include <asm/uaccess.h>
41 #include <asm/mman.h>
44 * Shared mappings implemented 30.11.1994. It's not fully working yet,
45 * though.
47 * Shared mappings now work. 15.8.1995 Bruno.
49 * finished 'unifying' the page and buffer cache and SMP-threaded the
50 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
52 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
56 * Lock ordering:
58 * ->i_shared_sem (vmtruncate)
59 * ->private_lock (__free_pte->__set_page_dirty_buffers)
60 * ->swap_list_lock
61 * ->swap_device_lock (exclusive_swap_page, others)
62 * ->mapping->page_lock
64 * ->i_sem
65 * ->i_shared_sem (truncate->invalidate_mmap_range)
67 * ->mmap_sem
68 * ->i_shared_sem (various places)
70 * ->mmap_sem
71 * ->lock_page (access_process_vm)
73 * ->mmap_sem
74 * ->i_sem (msync)
76 * ->inode_lock
77 * ->sb_lock (fs/fs-writeback.c)
78 * ->mapping->page_lock (__sync_single_inode)
80 * ->page_table_lock
81 * ->swap_device_lock (try_to_unmap_one)
82 * ->private_lock (try_to_unmap_one)
83 * ->page_lock (try_to_unmap_one)
84 * ->zone.lru_lock (follow_page->mark_page_accessed)
86 * ->task->proc_lock
87 * ->dcache_lock (proc_pid_lookup)
91 * Remove a page from the page cache and free it. Caller has to make
92 * sure the page is locked and that nobody else uses it - or that usage
93 * is safe. The caller must hold a write_lock on the mapping's page_lock.
95 void __remove_from_page_cache(struct page *page)
97 struct address_space *mapping = page->mapping;
99 radix_tree_delete(&mapping->page_tree, page->index);
100 list_del(&page->list);
101 page->mapping = NULL;
103 mapping->nrpages--;
104 pagecache_acct(-1);
107 void remove_from_page_cache(struct page *page)
109 struct address_space *mapping = page->mapping;
111 if (unlikely(!PageLocked(page)))
112 PAGE_BUG(page);
114 spin_lock(&mapping->page_lock);
115 __remove_from_page_cache(page);
116 spin_unlock(&mapping->page_lock);
119 static inline int sync_page(struct page *page)
121 struct address_space *mapping = page->mapping;
123 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
124 return mapping->a_ops->sync_page(page);
125 return 0;
129 * filemap_fdatawrite - start writeback against all of a mapping's dirty pages
130 * @mapping: address space structure to write
132 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
133 * opposed to a regular memory * cleansing writeback. The difference between
134 * these two operations is that if a dirty page/buffer is encountered, it must
135 * be waited upon, and not just skipped over.
137 static int __filemap_fdatawrite(struct address_space *mapping, int sync_mode)
139 int ret;
140 struct writeback_control wbc = {
141 .sync_mode = sync_mode,
142 .nr_to_write = mapping->nrpages * 2,
145 if (mapping->backing_dev_info->memory_backed)
146 return 0;
148 spin_lock(&mapping->page_lock);
149 list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
150 spin_unlock(&mapping->page_lock);
151 ret = do_writepages(mapping, &wbc);
152 return ret;
155 int filemap_fdatawrite(struct address_space *mapping)
157 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
160 EXPORT_SYMBOL(filemap_fdatawrite);
163 * This is a mostly non-blocking flush. Not suitable for data-integrity
164 * purposes - I/O may not be started against all dirty pages.
166 int filemap_flush(struct address_space *mapping)
168 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
171 EXPORT_SYMBOL(filemap_flush);
174 * filemap_fdatawait - walk the list of locked pages of the given address
175 * space and wait for all of them.
176 * @mapping: address space structure to wait for
178 int filemap_fdatawait(struct address_space * mapping)
180 int ret = 0;
181 int progress;
183 restart:
184 progress = 0;
185 spin_lock(&mapping->page_lock);
186 while (!list_empty(&mapping->locked_pages)) {
187 struct page *page;
189 page = list_entry(mapping->locked_pages.next,struct page,list);
190 list_del(&page->list);
191 if (PageDirty(page))
192 list_add(&page->list, &mapping->dirty_pages);
193 else
194 list_add(&page->list, &mapping->clean_pages);
196 if (!PageWriteback(page)) {
197 if (++progress > 32) {
198 if (need_resched()) {
199 spin_unlock(&mapping->page_lock);
200 __cond_resched();
201 goto restart;
204 continue;
207 progress = 0;
208 page_cache_get(page);
209 spin_unlock(&mapping->page_lock);
211 wait_on_page_writeback(page);
212 if (PageError(page))
213 ret = -EIO;
215 page_cache_release(page);
216 spin_lock(&mapping->page_lock);
218 spin_unlock(&mapping->page_lock);
220 /* Check for outstanding write errors */
221 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
222 ret = -ENOSPC;
223 if (test_and_clear_bit(AS_EIO, &mapping->flags))
224 ret = -EIO;
226 return ret;
229 EXPORT_SYMBOL(filemap_fdatawait);
232 * This adds a page to the page cache, starting out as locked, unreferenced,
233 * not uptodate and with no errors.
235 * This function is used for two things: adding newly allocated pagecache
236 * pages and for moving existing anon pages into swapcache.
238 * In the case of pagecache pages, the page is new, so we can just run
239 * SetPageLocked() against it. The other page state flags were set by
240 * rmqueue()
242 * In the case of swapcache, try_to_swap_out() has already locked the page, so
243 * SetPageLocked() is ugly-but-OK there too. The required page state has been
244 * set up by swap_out_add_to_swap_cache().
246 * This function does not add the page to the LRU. The caller must do that.
248 int add_to_page_cache(struct page *page, struct address_space *mapping,
249 pgoff_t offset, int gfp_mask)
251 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
253 if (error == 0) {
254 page_cache_get(page);
255 spin_lock(&mapping->page_lock);
256 error = radix_tree_insert(&mapping->page_tree, offset, page);
257 if (!error) {
258 SetPageLocked(page);
259 ___add_to_page_cache(page, mapping, offset);
260 } else {
261 page_cache_release(page);
263 spin_unlock(&mapping->page_lock);
264 radix_tree_preload_end();
266 return error;
269 EXPORT_SYMBOL(add_to_page_cache);
271 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
272 pgoff_t offset, int gfp_mask)
274 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
275 if (ret == 0)
276 lru_cache_add(page);
277 return ret;
281 * In order to wait for pages to become available there must be
282 * waitqueues associated with pages. By using a hash table of
283 * waitqueues where the bucket discipline is to maintain all
284 * waiters on the same queue and wake all when any of the pages
285 * become available, and for the woken contexts to check to be
286 * sure the appropriate page became available, this saves space
287 * at a cost of "thundering herd" phenomena during rare hash
288 * collisions.
290 static wait_queue_head_t *page_waitqueue(struct page *page)
292 const struct zone *zone = page_zone(page);
294 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
297 void fastcall wait_on_page_bit(struct page *page, int bit_nr)
299 wait_queue_head_t *waitqueue = page_waitqueue(page);
300 DEFINE_WAIT(wait);
302 do {
303 prepare_to_wait(waitqueue, &wait, TASK_UNINTERRUPTIBLE);
304 if (test_bit(bit_nr, &page->flags)) {
305 sync_page(page);
306 io_schedule();
308 } while (test_bit(bit_nr, &page->flags));
309 finish_wait(waitqueue, &wait);
312 EXPORT_SYMBOL(wait_on_page_bit);
315 * unlock_page() - unlock a locked page
317 * @page: the page
319 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
320 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
321 * mechananism between PageLocked pages and PageWriteback pages is shared.
322 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
324 * The first mb is necessary to safely close the critical section opened by the
325 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
326 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
327 * parallel wait_on_page_locked()).
329 void fastcall unlock_page(struct page *page)
331 wait_queue_head_t *waitqueue = page_waitqueue(page);
332 smp_mb__before_clear_bit();
333 if (!TestClearPageLocked(page))
334 BUG();
335 smp_mb__after_clear_bit();
336 if (waitqueue_active(waitqueue))
337 wake_up_all(waitqueue);
340 EXPORT_SYMBOL(unlock_page);
341 EXPORT_SYMBOL(lock_page);
344 * End writeback against a page.
346 void end_page_writeback(struct page *page)
348 wait_queue_head_t *waitqueue = page_waitqueue(page);
350 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
351 smp_mb__before_clear_bit();
352 if (!TestClearPageWriteback(page))
353 BUG();
354 smp_mb__after_clear_bit();
356 if (waitqueue_active(waitqueue))
357 wake_up_all(waitqueue);
360 EXPORT_SYMBOL(end_page_writeback);
363 * Get a lock on the page, assuming we need to sleep to get it.
365 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
366 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
367 * chances are that on the second loop, the block layer's plug list is empty,
368 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
370 void fastcall __lock_page(struct page *page)
372 wait_queue_head_t *wqh = page_waitqueue(page);
373 DEFINE_WAIT(wait);
375 while (TestSetPageLocked(page)) {
376 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
377 if (PageLocked(page)) {
378 sync_page(page);
379 io_schedule();
382 finish_wait(wqh, &wait);
385 EXPORT_SYMBOL(__lock_page);
388 * a rather lightweight function, finding and getting a reference to a
389 * hashed page atomically.
391 struct page * find_get_page(struct address_space *mapping, unsigned long offset)
393 struct page *page;
396 * We scan the hash list read-only. Addition to and removal from
397 * the hash-list needs a held write-lock.
399 spin_lock(&mapping->page_lock);
400 page = radix_tree_lookup(&mapping->page_tree, offset);
401 if (page)
402 page_cache_get(page);
403 spin_unlock(&mapping->page_lock);
404 return page;
407 EXPORT_SYMBOL(find_get_page);
410 * Same as above, but trylock it instead of incrementing the count.
412 struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
414 struct page *page;
416 spin_lock(&mapping->page_lock);
417 page = radix_tree_lookup(&mapping->page_tree, offset);
418 if (page && TestSetPageLocked(page))
419 page = NULL;
420 spin_unlock(&mapping->page_lock);
421 return page;
424 EXPORT_SYMBOL(find_trylock_page);
427 * find_lock_page - locate, pin and lock a pagecache page
429 * @mapping - the address_space to search
430 * @offset - the page index
432 * Locates the desired pagecache page, locks it, increments its reference
433 * count and returns its address.
435 * Returns zero if the page was not present. find_lock_page() may sleep.
437 struct page *find_lock_page(struct address_space *mapping,
438 unsigned long offset)
440 struct page *page;
442 spin_lock(&mapping->page_lock);
443 repeat:
444 page = radix_tree_lookup(&mapping->page_tree, offset);
445 if (page) {
446 page_cache_get(page);
447 if (TestSetPageLocked(page)) {
448 spin_unlock(&mapping->page_lock);
449 lock_page(page);
450 spin_lock(&mapping->page_lock);
452 /* Has the page been truncated while we slept? */
453 if (page->mapping != mapping || page->index != offset) {
454 unlock_page(page);
455 page_cache_release(page);
456 goto repeat;
460 spin_unlock(&mapping->page_lock);
461 return page;
464 EXPORT_SYMBOL(find_lock_page);
467 * find_or_create_page - locate or add a pagecache page
469 * @mapping - the page's address_space
470 * @index - the page's index into the mapping
471 * @gfp_mask - page allocation mode
473 * Locates a page in the pagecache. If the page is not present, a new page
474 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
475 * LRU list. The returned page is locked and has its reference count
476 * incremented.
478 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
479 * allocation!
481 * find_or_create_page() returns the desired page's address, or zero on
482 * memory exhaustion.
484 struct page *find_or_create_page(struct address_space *mapping,
485 unsigned long index, unsigned int gfp_mask)
487 struct page *page, *cached_page = NULL;
488 int err;
489 repeat:
490 page = find_lock_page(mapping, index);
491 if (!page) {
492 if (!cached_page) {
493 cached_page = alloc_page(gfp_mask);
494 if (!cached_page)
495 return NULL;
497 err = add_to_page_cache_lru(cached_page, mapping,
498 index, gfp_mask);
499 if (!err) {
500 page = cached_page;
501 cached_page = NULL;
502 } else if (err == -EEXIST)
503 goto repeat;
505 if (cached_page)
506 page_cache_release(cached_page);
507 return page;
510 EXPORT_SYMBOL(find_or_create_page);
513 * find_get_pages - gang pagecache lookup
514 * @mapping: The address_space to search
515 * @start: The starting page index
516 * @nr_pages: The maximum number of pages
517 * @pages: Where the resulting pages are placed
519 * find_get_pages() will search for and return a group of up to
520 * @nr_pages pages in the mapping. The pages are placed at @pages.
521 * find_get_pages() takes a reference against the returned pages.
523 * The search returns a group of mapping-contiguous pages with ascending
524 * indexes. There may be holes in the indices due to not-present pages.
526 * find_get_pages() returns the number of pages which were found.
528 unsigned int find_get_pages(struct address_space *mapping, pgoff_t start,
529 unsigned int nr_pages, struct page **pages)
531 unsigned int i;
532 unsigned int ret;
534 spin_lock(&mapping->page_lock);
535 ret = radix_tree_gang_lookup(&mapping->page_tree,
536 (void **)pages, start, nr_pages);
537 for (i = 0; i < ret; i++)
538 page_cache_get(pages[i]);
539 spin_unlock(&mapping->page_lock);
540 return ret;
544 * Same as grab_cache_page, but do not wait if the page is unavailable.
545 * This is intended for speculative data generators, where the data can
546 * be regenerated if the page couldn't be grabbed. This routine should
547 * be safe to call while holding the lock for another page.
549 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
550 * and deadlock against the caller's locked page.
552 struct page *
553 grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
555 struct page *page = find_get_page(mapping, index);
556 int gfp_mask;
558 if (page) {
559 if (!TestSetPageLocked(page))
560 return page;
561 page_cache_release(page);
562 return NULL;
564 gfp_mask = mapping_gfp_mask(mapping) & ~__GFP_FS;
565 page = alloc_pages(gfp_mask, 0);
566 if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
567 page_cache_release(page);
568 page = NULL;
570 return page;
573 EXPORT_SYMBOL(grab_cache_page_nowait);
576 * This is a generic file read routine, and uses the
577 * mapping->a_ops->readpage() function for the actual low-level
578 * stuff.
580 * This is really ugly. But the goto's actually try to clarify some
581 * of the logic when it comes to error handling etc.
582 * - note the struct file * is only passed for the use of readpage
584 void do_generic_mapping_read(struct address_space *mapping,
585 struct file_ra_state *ra,
586 struct file * filp,
587 loff_t *ppos,
588 read_descriptor_t * desc,
589 read_actor_t actor)
591 struct inode *inode = mapping->host;
592 unsigned long index, offset;
593 struct page *cached_page;
594 int error;
596 cached_page = NULL;
597 index = *ppos >> PAGE_CACHE_SHIFT;
598 offset = *ppos & ~PAGE_CACHE_MASK;
600 for (;;) {
601 struct page *page;
602 unsigned long end_index, nr, ret;
603 loff_t isize = i_size_read(inode);
605 end_index = isize >> PAGE_CACHE_SHIFT;
607 if (index > end_index)
608 break;
609 nr = PAGE_CACHE_SIZE;
610 if (index == end_index) {
611 nr = isize & ~PAGE_CACHE_MASK;
612 if (nr <= offset)
613 break;
616 cond_resched();
617 page_cache_readahead(mapping, ra, filp, index);
619 nr = nr - offset;
620 find_page:
621 page = find_get_page(mapping, index);
622 if (unlikely(page == NULL)) {
623 handle_ra_miss(mapping, ra, index);
624 goto no_cached_page;
626 if (!PageUptodate(page))
627 goto page_not_up_to_date;
628 page_ok:
629 /* If users can be writing to this page using arbitrary
630 * virtual addresses, take care about potential aliasing
631 * before reading the page on the kernel side.
633 if (!list_empty(&mapping->i_mmap_shared))
634 flush_dcache_page(page);
637 * Mark the page accessed if we read the beginning.
639 if (!offset)
640 mark_page_accessed(page);
643 * Ok, we have the page, and it's up-to-date, so
644 * now we can copy it to user space...
646 * The actor routine returns how many bytes were actually used..
647 * NOTE! This may not be the same as how much of a user buffer
648 * we filled up (we may be padding etc), so we can only update
649 * "pos" here (the actor routine has to update the user buffer
650 * pointers and the remaining count).
652 ret = actor(desc, page, offset, nr);
653 offset += ret;
654 index += offset >> PAGE_CACHE_SHIFT;
655 offset &= ~PAGE_CACHE_MASK;
657 page_cache_release(page);
658 if (ret == nr && desc->count)
659 continue;
660 break;
662 page_not_up_to_date:
663 if (PageUptodate(page))
664 goto page_ok;
666 /* Get exclusive access to the page ... */
667 lock_page(page);
669 /* Did it get unhashed before we got the lock? */
670 if (!page->mapping) {
671 unlock_page(page);
672 page_cache_release(page);
673 continue;
676 /* Did somebody else fill it already? */
677 if (PageUptodate(page)) {
678 unlock_page(page);
679 goto page_ok;
682 readpage:
683 /* ... and start the actual read. The read will unlock the page. */
684 error = mapping->a_ops->readpage(filp, page);
686 if (!error) {
687 if (PageUptodate(page))
688 goto page_ok;
689 wait_on_page_locked(page);
690 if (PageUptodate(page))
691 goto page_ok;
692 error = -EIO;
695 /* UHHUH! A synchronous read error occurred. Report it */
696 desc->error = error;
697 page_cache_release(page);
698 break;
700 no_cached_page:
702 * Ok, it wasn't cached, so we need to create a new
703 * page..
705 if (!cached_page) {
706 cached_page = page_cache_alloc_cold(mapping);
707 if (!cached_page) {
708 desc->error = -ENOMEM;
709 break;
712 error = add_to_page_cache_lru(cached_page, mapping,
713 index, GFP_KERNEL);
714 if (error) {
715 if (error == -EEXIST)
716 goto find_page;
717 desc->error = error;
718 break;
720 page = cached_page;
721 cached_page = NULL;
722 goto readpage;
725 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
726 if (cached_page)
727 page_cache_release(cached_page);
728 file_accessed(filp);
731 EXPORT_SYMBOL(do_generic_mapping_read);
733 int file_read_actor(read_descriptor_t *desc, struct page *page,
734 unsigned long offset, unsigned long size)
736 char *kaddr;
737 unsigned long left, count = desc->count;
739 if (size > count)
740 size = count;
743 * Faults on the destination of a read are common, so do it before
744 * taking the kmap.
746 if (!fault_in_pages_writeable(desc->buf, size)) {
747 kaddr = kmap_atomic(page, KM_USER0);
748 left = __copy_to_user(desc->buf, kaddr + offset, size);
749 kunmap_atomic(kaddr, KM_USER0);
750 if (left == 0)
751 goto success;
754 /* Do it the slow way */
755 kaddr = kmap(page);
756 left = __copy_to_user(desc->buf, kaddr + offset, size);
757 kunmap(page);
759 if (left) {
760 size -= left;
761 desc->error = -EFAULT;
763 success:
764 desc->count = count - size;
765 desc->written += size;
766 desc->buf += size;
767 return size;
771 * This is the "read()" routine for all filesystems
772 * that can use the page cache directly.
774 ssize_t
775 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
776 unsigned long nr_segs, loff_t *ppos)
778 struct file *filp = iocb->ki_filp;
779 ssize_t retval;
780 unsigned long seg;
781 size_t count;
783 count = 0;
784 for (seg = 0; seg < nr_segs; seg++) {
785 const struct iovec *iv = &iov[seg];
788 * If any segment has a negative length, or the cumulative
789 * length ever wraps negative then return -EINVAL.
791 count += iv->iov_len;
792 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
793 return -EINVAL;
794 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
795 continue;
796 if (seg == 0)
797 return -EFAULT;
798 nr_segs = seg;
799 count -= iv->iov_len; /* This segment is no good */
800 break;
803 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
804 if (filp->f_flags & O_DIRECT) {
805 loff_t pos = *ppos, size;
806 struct address_space *mapping;
807 struct inode *inode;
809 mapping = filp->f_mapping;
810 inode = mapping->host;
811 retval = 0;
812 if (!count)
813 goto out; /* skip atime */
814 size = i_size_read(inode);
815 if (pos < size) {
816 retval = generic_file_direct_IO(READ, iocb,
817 iov, pos, nr_segs);
818 if (retval >= 0 && !is_sync_kiocb(iocb))
819 retval = -EIOCBQUEUED;
820 if (retval > 0)
821 *ppos = pos + retval;
823 file_accessed(filp);
824 goto out;
827 retval = 0;
828 if (count) {
829 for (seg = 0; seg < nr_segs; seg++) {
830 read_descriptor_t desc;
832 desc.written = 0;
833 desc.buf = iov[seg].iov_base;
834 desc.count = iov[seg].iov_len;
835 if (desc.count == 0)
836 continue;
837 desc.error = 0;
838 do_generic_file_read(filp,ppos,&desc,file_read_actor);
839 retval += desc.written;
840 if (!retval) {
841 retval = desc.error;
842 break;
846 out:
847 return retval;
850 EXPORT_SYMBOL(__generic_file_aio_read);
852 ssize_t
853 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
855 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
857 BUG_ON(iocb->ki_pos != pos);
858 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
861 EXPORT_SYMBOL(generic_file_aio_read);
863 ssize_t
864 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
866 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
867 struct kiocb kiocb;
868 ssize_t ret;
870 init_sync_kiocb(&kiocb, filp);
871 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
872 if (-EIOCBQUEUED == ret)
873 ret = wait_on_sync_kiocb(&kiocb);
874 return ret;
877 EXPORT_SYMBOL(generic_file_read);
879 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
881 ssize_t written;
882 unsigned long count = desc->count;
883 struct file *file = (struct file *) desc->buf;
885 if (size > count)
886 size = count;
888 written = file->f_op->sendpage(file, page, offset,
889 size, &file->f_pos, size<count);
890 if (written < 0) {
891 desc->error = written;
892 written = 0;
894 desc->count = count - written;
895 desc->written += written;
896 return written;
899 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
900 size_t count, read_actor_t actor, void __user *target)
902 read_descriptor_t desc;
904 if (!count)
905 return 0;
907 desc.written = 0;
908 desc.count = count;
909 desc.buf = target;
910 desc.error = 0;
912 do_generic_file_read(in_file, ppos, &desc, actor);
913 if (desc.written)
914 return desc.written;
915 return desc.error;
918 EXPORT_SYMBOL(generic_file_sendfile);
920 static ssize_t
921 do_readahead(struct address_space *mapping, struct file *filp,
922 unsigned long index, unsigned long nr)
924 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
925 return -EINVAL;
927 force_page_cache_readahead(mapping, filp, index,
928 max_sane_readahead(nr));
929 return 0;
932 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
934 ssize_t ret;
935 struct file *file;
937 ret = -EBADF;
938 file = fget(fd);
939 if (file) {
940 if (file->f_mode & FMODE_READ) {
941 struct address_space *mapping = file->f_mapping;
942 unsigned long start = offset >> PAGE_CACHE_SHIFT;
943 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
944 unsigned long len = end - start + 1;
945 ret = do_readahead(mapping, file, start, len);
947 fput(file);
949 return ret;
952 #ifdef CONFIG_MMU
954 * This adds the requested page to the page cache if it isn't already there,
955 * and schedules an I/O to read in its contents from disk.
957 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
958 static int fastcall page_cache_read(struct file * file, unsigned long offset)
960 struct address_space *mapping = file->f_mapping;
961 struct page *page;
962 int error;
964 page = page_cache_alloc_cold(mapping);
965 if (!page)
966 return -ENOMEM;
968 error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
969 if (!error) {
970 error = mapping->a_ops->readpage(file, page);
971 page_cache_release(page);
972 return error;
976 * We arrive here in the unlikely event that someone
977 * raced with us and added our page to the cache first
978 * or we are out of memory for radix-tree nodes.
980 page_cache_release(page);
981 return error == -EEXIST ? 0 : error;
984 #define MMAP_READAROUND (16UL)
985 #define MMAP_LOTSAMISS (100)
988 * filemap_nopage() is invoked via the vma operations vector for a
989 * mapped memory region to read in file data during a page fault.
991 * The goto's are kind of ugly, but this streamlines the normal case of having
992 * it in the page cache, and handles the special cases reasonably without
993 * having a lot of duplicated code.
995 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int *type)
997 int error;
998 struct file *file = area->vm_file;
999 struct address_space *mapping = file->f_mapping;
1000 struct file_ra_state *ra = &file->f_ra;
1001 struct inode *inode = mapping->host;
1002 struct page *page;
1003 unsigned long size, pgoff, endoff;
1004 int did_readaround = 0, majmin = VM_FAULT_MINOR;
1006 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1007 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1009 retry_all:
1010 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1011 if (pgoff >= size)
1012 goto outside_data_content;
1014 /* If we don't want any read-ahead, don't bother */
1015 if (VM_RandomReadHint(area))
1016 goto no_cached_page;
1019 * The "size" of the file, as far as mmap is concerned, isn't bigger
1020 * than the mapping
1022 if (size > endoff)
1023 size = endoff;
1026 * The readahead code wants to be told about each and every page
1027 * so it can build and shrink its windows appropriately
1029 * For sequential accesses, we use the generic readahead logic.
1031 if (VM_SequentialReadHint(area))
1032 page_cache_readahead(mapping, ra, file, pgoff);
1035 * Do we have something in the page cache already?
1037 retry_find:
1038 page = find_get_page(mapping, pgoff);
1039 if (!page) {
1040 if (VM_SequentialReadHint(area)) {
1041 handle_ra_miss(mapping, ra, pgoff);
1042 goto no_cached_page;
1044 ra->mmap_miss++;
1047 * Do we miss much more than hit in this file? If so,
1048 * stop bothering with read-ahead. It will only hurt.
1050 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
1051 goto no_cached_page;
1054 * To keep the pgmajfault counter straight, we need to
1055 * check did_readaround, as this is an inner loop.
1057 if (!did_readaround) {
1058 majmin = VM_FAULT_MAJOR;
1059 inc_page_state(pgmajfault);
1061 did_readaround = 1;
1062 do_page_cache_readahead(mapping, file,
1063 pgoff & ~(MMAP_READAROUND-1), MMAP_READAROUND);
1064 goto retry_find;
1067 if (!did_readaround)
1068 ra->mmap_hit++;
1071 * Ok, found a page in the page cache, now we need to check
1072 * that it's up-to-date.
1074 if (!PageUptodate(page))
1075 goto page_not_uptodate;
1077 success:
1079 * Found the page and have a reference on it.
1081 mark_page_accessed(page);
1082 if (type)
1083 *type = majmin;
1084 return page;
1086 outside_data_content:
1088 * An external ptracer can access pages that normally aren't
1089 * accessible..
1091 if (area->vm_mm == current->mm)
1092 return NULL;
1093 /* Fall through to the non-read-ahead case */
1094 no_cached_page:
1096 * We're only likely to ever get here if MADV_RANDOM is in
1097 * effect.
1099 error = page_cache_read(file, pgoff);
1102 * The page we want has now been added to the page cache.
1103 * In the unlikely event that someone removed it in the
1104 * meantime, we'll just come back here and read it again.
1106 if (error >= 0)
1107 goto retry_find;
1110 * An error return from page_cache_read can result if the
1111 * system is low on memory, or a problem occurs while trying
1112 * to schedule I/O.
1114 if (error == -ENOMEM)
1115 return NOPAGE_OOM;
1116 return NULL;
1118 page_not_uptodate:
1119 if (!did_readaround) {
1120 majmin = VM_FAULT_MAJOR;
1121 inc_page_state(pgmajfault);
1123 lock_page(page);
1125 /* Did it get unhashed while we waited for it? */
1126 if (!page->mapping) {
1127 unlock_page(page);
1128 page_cache_release(page);
1129 goto retry_all;
1132 /* Did somebody else get it up-to-date? */
1133 if (PageUptodate(page)) {
1134 unlock_page(page);
1135 goto success;
1138 if (!mapping->a_ops->readpage(file, page)) {
1139 wait_on_page_locked(page);
1140 if (PageUptodate(page))
1141 goto success;
1145 * Umm, take care of errors if the page isn't up-to-date.
1146 * Try to re-read it _once_. We do this synchronously,
1147 * because there really aren't any performance issues here
1148 * and we need to check for errors.
1150 lock_page(page);
1152 /* Somebody truncated the page on us? */
1153 if (!page->mapping) {
1154 unlock_page(page);
1155 page_cache_release(page);
1156 goto retry_all;
1159 /* Somebody else successfully read it in? */
1160 if (PageUptodate(page)) {
1161 unlock_page(page);
1162 goto success;
1164 ClearPageError(page);
1165 if (!mapping->a_ops->readpage(file, page)) {
1166 wait_on_page_locked(page);
1167 if (PageUptodate(page))
1168 goto success;
1172 * Things didn't work out. Return zero to tell the
1173 * mm layer so, possibly freeing the page cache page first.
1175 page_cache_release(page);
1176 return NULL;
1179 EXPORT_SYMBOL(filemap_nopage);
1181 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1182 int nonblock)
1184 struct address_space *mapping = file->f_mapping;
1185 struct page *page;
1186 int error;
1189 * Do we have something in the page cache already?
1191 retry_find:
1192 page = find_get_page(mapping, pgoff);
1193 if (!page) {
1194 if (nonblock)
1195 return NULL;
1196 goto no_cached_page;
1200 * Ok, found a page in the page cache, now we need to check
1201 * that it's up-to-date.
1203 if (!PageUptodate(page))
1204 goto page_not_uptodate;
1206 success:
1208 * Found the page and have a reference on it.
1210 mark_page_accessed(page);
1211 return page;
1213 no_cached_page:
1214 error = page_cache_read(file, pgoff);
1217 * The page we want has now been added to the page cache.
1218 * In the unlikely event that someone removed it in the
1219 * meantime, we'll just come back here and read it again.
1221 if (error >= 0)
1222 goto retry_find;
1225 * An error return from page_cache_read can result if the
1226 * system is low on memory, or a problem occurs while trying
1227 * to schedule I/O.
1229 return NULL;
1231 page_not_uptodate:
1232 lock_page(page);
1234 /* Did it get unhashed while we waited for it? */
1235 if (!page->mapping) {
1236 unlock_page(page);
1237 goto err;
1240 /* Did somebody else get it up-to-date? */
1241 if (PageUptodate(page)) {
1242 unlock_page(page);
1243 goto success;
1246 if (!mapping->a_ops->readpage(file, page)) {
1247 wait_on_page_locked(page);
1248 if (PageUptodate(page))
1249 goto success;
1253 * Umm, take care of errors if the page isn't up-to-date.
1254 * Try to re-read it _once_. We do this synchronously,
1255 * because there really aren't any performance issues here
1256 * and we need to check for errors.
1258 lock_page(page);
1260 /* Somebody truncated the page on us? */
1261 if (!page->mapping) {
1262 unlock_page(page);
1263 goto err;
1265 /* Somebody else successfully read it in? */
1266 if (PageUptodate(page)) {
1267 unlock_page(page);
1268 goto success;
1271 ClearPageError(page);
1272 if (!mapping->a_ops->readpage(file, page)) {
1273 wait_on_page_locked(page);
1274 if (PageUptodate(page))
1275 goto success;
1279 * Things didn't work out. Return zero to tell the
1280 * mm layer so, possibly freeing the page cache page first.
1282 err:
1283 page_cache_release(page);
1285 return NULL;
1288 static int filemap_populate(struct vm_area_struct *vma,
1289 unsigned long addr,
1290 unsigned long len,
1291 pgprot_t prot,
1292 unsigned long pgoff,
1293 int nonblock)
1295 struct file *file = vma->vm_file;
1296 struct address_space *mapping = file->f_mapping;
1297 struct inode *inode = mapping->host;
1298 unsigned long size;
1299 struct mm_struct *mm = vma->vm_mm;
1300 struct page *page;
1301 int err;
1303 if (!nonblock)
1304 force_page_cache_readahead(mapping, vma->vm_file,
1305 pgoff, len >> PAGE_CACHE_SHIFT);
1307 repeat:
1308 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1309 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1310 return -EINVAL;
1312 page = filemap_getpage(file, pgoff, nonblock);
1313 if (!page && !nonblock)
1314 return -ENOMEM;
1315 if (page) {
1316 err = install_page(mm, vma, addr, page, prot);
1317 if (err) {
1318 page_cache_release(page);
1319 return err;
1321 } else {
1323 * If a nonlinear mapping then store the file page offset
1324 * in the pte.
1326 unsigned long pgidx;
1327 pgidx = (addr - vma->vm_start) >> PAGE_SHIFT;
1328 pgidx += vma->vm_pgoff;
1329 pgidx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
1330 if (pgoff != pgidx) {
1331 err = install_file_pte(mm, vma, addr, pgoff, prot);
1332 if (err)
1333 return err;
1337 len -= PAGE_SIZE;
1338 addr += PAGE_SIZE;
1339 pgoff++;
1340 if (len)
1341 goto repeat;
1343 return 0;
1346 static struct vm_operations_struct generic_file_vm_ops = {
1347 .nopage = filemap_nopage,
1348 .populate = filemap_populate,
1351 /* This is used for a general mmap of a disk file */
1353 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1355 struct address_space *mapping = file->f_mapping;
1357 if (!mapping->a_ops->readpage)
1358 return -ENOEXEC;
1359 file_accessed(file);
1360 vma->vm_ops = &generic_file_vm_ops;
1361 return 0;
1365 * This is for filesystems which do not implement ->writepage.
1367 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1369 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1370 return -EINVAL;
1371 return generic_file_mmap(file, vma);
1373 #else
1374 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1376 return -ENOSYS;
1378 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1380 return -ENOSYS;
1382 #endif /* CONFIG_MMU */
1384 EXPORT_SYMBOL(generic_file_mmap);
1385 EXPORT_SYMBOL(generic_file_readonly_mmap);
1387 static inline struct page *__read_cache_page(struct address_space *mapping,
1388 unsigned long index,
1389 int (*filler)(void *,struct page*),
1390 void *data)
1392 struct page *page, *cached_page = NULL;
1393 int err;
1394 repeat:
1395 page = find_get_page(mapping, index);
1396 if (!page) {
1397 if (!cached_page) {
1398 cached_page = page_cache_alloc_cold(mapping);
1399 if (!cached_page)
1400 return ERR_PTR(-ENOMEM);
1402 err = add_to_page_cache_lru(cached_page, mapping,
1403 index, GFP_KERNEL);
1404 if (err == -EEXIST)
1405 goto repeat;
1406 if (err < 0) {
1407 /* Presumably ENOMEM for radix tree node */
1408 page_cache_release(cached_page);
1409 return ERR_PTR(err);
1411 page = cached_page;
1412 cached_page = NULL;
1413 err = filler(data, page);
1414 if (err < 0) {
1415 page_cache_release(page);
1416 page = ERR_PTR(err);
1419 if (cached_page)
1420 page_cache_release(cached_page);
1421 return page;
1425 * Read into the page cache. If a page already exists,
1426 * and PageUptodate() is not set, try to fill the page.
1428 struct page *read_cache_page(struct address_space *mapping,
1429 unsigned long index,
1430 int (*filler)(void *,struct page*),
1431 void *data)
1433 struct page *page;
1434 int err;
1436 retry:
1437 page = __read_cache_page(mapping, index, filler, data);
1438 if (IS_ERR(page))
1439 goto out;
1440 mark_page_accessed(page);
1441 if (PageUptodate(page))
1442 goto out;
1444 lock_page(page);
1445 if (!page->mapping) {
1446 unlock_page(page);
1447 page_cache_release(page);
1448 goto retry;
1450 if (PageUptodate(page)) {
1451 unlock_page(page);
1452 goto out;
1454 err = filler(data, page);
1455 if (err < 0) {
1456 page_cache_release(page);
1457 page = ERR_PTR(err);
1459 out:
1460 return page;
1463 EXPORT_SYMBOL(read_cache_page);
1466 * If the page was newly created, increment its refcount and add it to the
1467 * caller's lru-buffering pagevec. This function is specifically for
1468 * generic_file_write().
1470 static inline struct page *
1471 __grab_cache_page(struct address_space *mapping, unsigned long index,
1472 struct page **cached_page, struct pagevec *lru_pvec)
1474 int err;
1475 struct page *page;
1476 repeat:
1477 page = find_lock_page(mapping, index);
1478 if (!page) {
1479 if (!*cached_page) {
1480 *cached_page = page_cache_alloc(mapping);
1481 if (!*cached_page)
1482 return NULL;
1484 err = add_to_page_cache(*cached_page, mapping,
1485 index, GFP_KERNEL);
1486 if (err == -EEXIST)
1487 goto repeat;
1488 if (err == 0) {
1489 page = *cached_page;
1490 page_cache_get(page);
1491 if (!pagevec_add(lru_pvec, page))
1492 __pagevec_lru_add(lru_pvec);
1493 *cached_page = NULL;
1496 return page;
1500 * The logic we want is
1502 * if suid or (sgid and xgrp)
1503 * remove privs
1505 int remove_suid(struct dentry *dentry)
1507 mode_t mode = dentry->d_inode->i_mode;
1508 int kill = 0;
1509 int result = 0;
1511 /* suid always must be killed */
1512 if (unlikely(mode & S_ISUID))
1513 kill = ATTR_KILL_SUID;
1516 * sgid without any exec bits is just a mandatory locking mark; leave
1517 * it alone. If some exec bits are set, it's a real sgid; kill it.
1519 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1520 kill |= ATTR_KILL_SGID;
1522 if (unlikely(kill && !capable(CAP_FSETID))) {
1523 struct iattr newattrs;
1525 newattrs.ia_valid = ATTR_FORCE | kill;
1526 result = notify_change(dentry, &newattrs);
1528 return result;
1530 EXPORT_SYMBOL(remove_suid);
1533 * Copy as much as we can into the page and return the number of bytes which
1534 * were sucessfully copied. If a fault is encountered then clear the page
1535 * out to (offset+bytes) and return the number of bytes which were copied.
1537 static inline size_t
1538 filemap_copy_from_user(struct page *page, unsigned long offset,
1539 const char __user *buf, unsigned bytes)
1541 char *kaddr;
1542 int left;
1544 kaddr = kmap_atomic(page, KM_USER0);
1545 left = __copy_from_user(kaddr + offset, buf, bytes);
1546 kunmap_atomic(kaddr, KM_USER0);
1548 if (left != 0) {
1549 /* Do it the slow way */
1550 kaddr = kmap(page);
1551 left = __copy_from_user(kaddr + offset, buf, bytes);
1552 kunmap(page);
1554 return bytes - left;
1557 static size_t
1558 __filemap_copy_from_user_iovec(char *vaddr,
1559 const struct iovec *iov, size_t base, size_t bytes)
1561 size_t copied = 0, left = 0;
1563 while (bytes) {
1564 char __user *buf = iov->iov_base + base;
1565 int copy = min(bytes, iov->iov_len - base);
1567 base = 0;
1568 left = __copy_from_user(vaddr, buf, copy);
1569 copied += copy;
1570 bytes -= copy;
1571 vaddr += copy;
1572 iov++;
1574 if (unlikely(left)) {
1575 /* zero the rest of the target like __copy_from_user */
1576 if (bytes)
1577 memset(vaddr, 0, bytes);
1578 break;
1581 return copied - left;
1585 * This has the same sideeffects and return value as filemap_copy_from_user().
1586 * The difference is that on a fault we need to memset the remainder of the
1587 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1588 * single-segment behaviour.
1590 static inline size_t
1591 filemap_copy_from_user_iovec(struct page *page, unsigned long offset,
1592 const struct iovec *iov, size_t base, size_t bytes)
1594 char *kaddr;
1595 size_t copied;
1597 kaddr = kmap_atomic(page, KM_USER0);
1598 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1599 base, bytes);
1600 kunmap_atomic(kaddr, KM_USER0);
1601 if (copied != bytes) {
1602 kaddr = kmap(page);
1603 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1604 base, bytes);
1605 kunmap(page);
1607 return copied;
1610 static inline void
1611 filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1613 const struct iovec *iov = *iovp;
1614 size_t base = *basep;
1616 while (bytes) {
1617 int copy = min(bytes, iov->iov_len - base);
1619 bytes -= copy;
1620 base += copy;
1621 if (iov->iov_len == base) {
1622 iov++;
1623 base = 0;
1626 *iovp = iov;
1627 *basep = base;
1631 * Performs necessary checks before doing a write
1633 * Can adjust writing position aor amount of bytes to write.
1634 * Returns appropriate error code that caller should return or
1635 * zero in case that write should be allowed.
1637 inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
1639 struct inode *inode = file->f_mapping->host;
1640 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1642 if (unlikely(*pos < 0))
1643 return -EINVAL;
1645 if (unlikely(file->f_error)) {
1646 int err = file->f_error;
1647 file->f_error = 0;
1648 return err;
1651 if (!isblk) {
1652 /* FIXME: this is for backwards compatibility with 2.4 */
1653 if (file->f_flags & O_APPEND)
1654 *pos = i_size_read(inode);
1656 if (limit != RLIM_INFINITY) {
1657 if (*pos >= limit) {
1658 send_sig(SIGXFSZ, current, 0);
1659 return -EFBIG;
1661 if (*count > limit - (typeof(limit))*pos) {
1662 *count = limit - (typeof(limit))*pos;
1668 * LFS rule
1670 if (unlikely(*pos + *count > MAX_NON_LFS &&
1671 !(file->f_flags & O_LARGEFILE))) {
1672 if (*pos >= MAX_NON_LFS) {
1673 send_sig(SIGXFSZ, current, 0);
1674 return -EFBIG;
1676 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1677 *count = MAX_NON_LFS - (unsigned long)*pos;
1682 * Are we about to exceed the fs block limit ?
1684 * If we have written data it becomes a short write. If we have
1685 * exceeded without writing data we send a signal and return EFBIG.
1686 * Linus frestrict idea will clean these up nicely..
1688 if (likely(!isblk)) {
1689 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1690 if (*count || *pos > inode->i_sb->s_maxbytes) {
1691 send_sig(SIGXFSZ, current, 0);
1692 return -EFBIG;
1694 /* zero-length writes at ->s_maxbytes are OK */
1697 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1698 *count = inode->i_sb->s_maxbytes - *pos;
1699 } else {
1700 loff_t isize;
1701 if (bdev_read_only(I_BDEV(inode)))
1702 return -EPERM;
1703 isize = i_size_read(inode);
1704 if (*pos >= isize) {
1705 if (*count || *pos > isize)
1706 return -ENOSPC;
1709 if (*pos + *count > isize)
1710 *count = isize - *pos;
1712 return 0;
1715 EXPORT_SYMBOL(generic_write_checks);
1718 * Write to a file through the page cache.
1720 * We put everything into the page cache prior to writing it. This is not a
1721 * problem when writing full pages. With partial pages, however, we first have
1722 * to read the data into the cache, then dirty the page, and finally schedule
1723 * it for writing by marking it dirty.
1724 * okir@monad.swb.de
1726 ssize_t
1727 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
1728 unsigned long nr_segs, loff_t *ppos)
1730 struct file *file = iocb->ki_filp;
1731 struct address_space * mapping = file->f_mapping;
1732 struct address_space_operations *a_ops = mapping->a_ops;
1733 size_t ocount; /* original count */
1734 size_t count; /* after file limit checks */
1735 struct inode *inode = mapping->host;
1736 long status = 0;
1737 loff_t pos;
1738 struct page *page;
1739 struct page *cached_page = NULL;
1740 const int isblk = S_ISBLK(inode->i_mode);
1741 ssize_t written;
1742 ssize_t err;
1743 size_t bytes;
1744 struct pagevec lru_pvec;
1745 const struct iovec *cur_iov = iov; /* current iovec */
1746 size_t iov_base = 0; /* offset in the current iovec */
1747 unsigned long seg;
1748 char __user *buf;
1750 ocount = 0;
1751 for (seg = 0; seg < nr_segs; seg++) {
1752 const struct iovec *iv = &iov[seg];
1755 * If any segment has a negative length, or the cumulative
1756 * length ever wraps negative then return -EINVAL.
1758 ocount += iv->iov_len;
1759 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
1760 return -EINVAL;
1761 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1762 continue;
1763 if (seg == 0)
1764 return -EFAULT;
1765 nr_segs = seg;
1766 ocount -= iv->iov_len; /* This segment is no good */
1767 break;
1770 count = ocount;
1771 pos = *ppos;
1772 pagevec_init(&lru_pvec, 0);
1774 /* We can write back this queue in page reclaim */
1775 current->backing_dev_info = mapping->backing_dev_info;
1776 written = 0;
1778 err = generic_write_checks(file, &pos, &count, isblk);
1779 if (err)
1780 goto out;
1782 if (count == 0)
1783 goto out;
1785 err = remove_suid(file->f_dentry);
1786 if (err)
1787 goto out;
1789 inode_update_time(inode, 1);
1791 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1792 if (unlikely(file->f_flags & O_DIRECT)) {
1793 if (count != ocount)
1794 nr_segs = iov_shorten((struct iovec *)iov,
1795 nr_segs, count);
1796 written = generic_file_direct_IO(WRITE, iocb,
1797 iov, pos, nr_segs);
1798 if (written > 0) {
1799 loff_t end = pos + written;
1800 if (end > i_size_read(inode) && !isblk) {
1801 i_size_write(inode, end);
1802 mark_inode_dirty(inode);
1804 *ppos = end;
1807 * Sync the fs metadata but not the minor inode changes and
1808 * of course not the data as we did direct DMA for the IO.
1810 if (written >= 0 && file->f_flags & O_SYNC)
1811 status = generic_osync_inode(inode, mapping, OSYNC_METADATA);
1812 if (written >= 0 && !is_sync_kiocb(iocb))
1813 written = -EIOCBQUEUED;
1814 goto out_status;
1817 buf = iov->iov_base;
1818 do {
1819 unsigned long index;
1820 unsigned long offset;
1821 size_t copied;
1823 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1824 index = pos >> PAGE_CACHE_SHIFT;
1825 bytes = PAGE_CACHE_SIZE - offset;
1826 if (bytes > count)
1827 bytes = count;
1830 * Bring in the user page that we will copy from _first_.
1831 * Otherwise there's a nasty deadlock on copying from the
1832 * same page as we're writing to, without it being marked
1833 * up-to-date.
1835 fault_in_pages_readable(buf, bytes);
1837 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1838 if (!page) {
1839 status = -ENOMEM;
1840 break;
1843 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1844 if (unlikely(status)) {
1845 loff_t isize = i_size_read(inode);
1847 * prepare_write() may have instantiated a few blocks
1848 * outside i_size. Trim these off again.
1850 unlock_page(page);
1851 page_cache_release(page);
1852 if (pos + bytes > isize)
1853 vmtruncate(inode, isize);
1854 break;
1856 if (likely(nr_segs == 1))
1857 copied = filemap_copy_from_user(page, offset,
1858 buf, bytes);
1859 else
1860 copied = filemap_copy_from_user_iovec(page, offset,
1861 cur_iov, iov_base, bytes);
1862 flush_dcache_page(page);
1863 status = a_ops->commit_write(file, page, offset, offset+bytes);
1864 if (likely(copied > 0)) {
1865 if (!status)
1866 status = copied;
1868 if (status >= 0) {
1869 written += status;
1870 count -= status;
1871 pos += status;
1872 buf += status;
1873 if (unlikely(nr_segs > 1))
1874 filemap_set_next_iovec(&cur_iov,
1875 &iov_base, status);
1878 if (unlikely(copied != bytes))
1879 if (status >= 0)
1880 status = -EFAULT;
1881 unlock_page(page);
1882 mark_page_accessed(page);
1883 page_cache_release(page);
1884 if (status < 0)
1885 break;
1886 balance_dirty_pages_ratelimited(mapping);
1887 cond_resched();
1888 } while (count);
1889 *ppos = pos;
1891 if (cached_page)
1892 page_cache_release(cached_page);
1895 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
1897 if (status >= 0) {
1898 if ((file->f_flags & O_SYNC) || IS_SYNC(inode))
1899 status = generic_osync_inode(inode, mapping,
1900 OSYNC_METADATA|OSYNC_DATA);
1903 out_status:
1904 err = written ? written : status;
1905 out:
1906 pagevec_lru_add(&lru_pvec);
1907 current->backing_dev_info = 0;
1908 return err;
1911 EXPORT_SYMBOL(generic_file_aio_write_nolock);
1913 ssize_t
1914 generic_file_write_nolock(struct file *file, const struct iovec *iov,
1915 unsigned long nr_segs, loff_t *ppos)
1917 struct kiocb kiocb;
1918 ssize_t ret;
1920 init_sync_kiocb(&kiocb, file);
1921 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
1922 if (-EIOCBQUEUED == ret)
1923 ret = wait_on_sync_kiocb(&kiocb);
1924 return ret;
1927 EXPORT_SYMBOL(generic_file_write_nolock);
1929 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
1930 size_t count, loff_t pos)
1932 struct file *file = iocb->ki_filp;
1933 struct inode *inode = file->f_mapping->host;
1934 ssize_t err;
1935 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1937 BUG_ON(iocb->ki_pos != pos);
1939 down(&inode->i_sem);
1940 err = generic_file_aio_write_nolock(iocb, &local_iov, 1,
1941 &iocb->ki_pos);
1942 up(&inode->i_sem);
1944 return err;
1947 EXPORT_SYMBOL(generic_file_aio_write);
1949 ssize_t generic_file_write(struct file *file, const char __user *buf,
1950 size_t count, loff_t *ppos)
1952 struct inode *inode = file->f_mapping->host;
1953 ssize_t err;
1954 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1956 down(&inode->i_sem);
1957 err = generic_file_write_nolock(file, &local_iov, 1, ppos);
1958 up(&inode->i_sem);
1960 return err;
1963 EXPORT_SYMBOL(generic_file_write);
1965 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
1966 unsigned long nr_segs, loff_t *ppos)
1968 struct kiocb kiocb;
1969 ssize_t ret;
1971 init_sync_kiocb(&kiocb, filp);
1972 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
1973 if (-EIOCBQUEUED == ret)
1974 ret = wait_on_sync_kiocb(&kiocb);
1975 return ret;
1978 EXPORT_SYMBOL(generic_file_readv);
1980 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
1981 unsigned long nr_segs, loff_t * ppos)
1983 struct inode *inode = file->f_mapping->host;
1984 ssize_t ret;
1986 down(&inode->i_sem);
1987 ret = generic_file_write_nolock(file, iov, nr_segs, ppos);
1988 up(&inode->i_sem);
1989 return ret;
1992 EXPORT_SYMBOL(generic_file_writev);
1994 ssize_t
1995 generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
1996 loff_t offset, unsigned long nr_segs)
1998 struct file *file = iocb->ki_filp;
1999 struct address_space *mapping = file->f_mapping;
2000 ssize_t retval;
2002 if (mapping->nrpages) {
2003 retval = filemap_fdatawrite(mapping);
2004 if (retval == 0)
2005 retval = filemap_fdatawait(mapping);
2006 if (retval)
2007 goto out;
2010 retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
2011 if (rw == WRITE && mapping->nrpages)
2012 invalidate_inode_pages2(mapping);
2013 out:
2014 return retval;
2017 EXPORT_SYMBOL_GPL(generic_file_direct_IO);