Cleanup syscall code to look more like it's mips64 equivalent.
[linux-2.6/linux-mips.git] / mm / filemap.c
blobf5a308ccc1f3ebc8c7a0396c1b3ab7b5afd66fab
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
63 * ->mmap_sem
64 * ->i_shared_sem (various places)
66 * ->inode_lock
67 * ->sb_lock (fs/fs-writeback.c)
68 * ->mapping->page_lock (__sync_single_inode)
69 * ->page_table_lock
70 * ->swap_device_lock (try_to_unmap_one)
71 * ->private_lock (try_to_unmap_one)
72 * ->page_lock (try_to_unmap_one)
76 * Remove a page from the page cache and free it. Caller has to make
77 * sure the page is locked and that nobody else uses it - or that usage
78 * is safe. The caller must hold a write_lock on the mapping's page_lock.
80 void __remove_from_page_cache(struct page *page)
82 struct address_space *mapping = page->mapping;
84 radix_tree_delete(&mapping->page_tree, page->index);
85 list_del(&page->list);
86 page->mapping = NULL;
88 mapping->nrpages--;
89 pagecache_acct(-1);
92 void remove_from_page_cache(struct page *page)
94 struct address_space *mapping = page->mapping;
96 if (unlikely(!PageLocked(page)))
97 PAGE_BUG(page);
99 spin_lock(&mapping->page_lock);
100 __remove_from_page_cache(page);
101 spin_unlock(&mapping->page_lock);
104 static inline int sync_page(struct page *page)
106 struct address_space *mapping = page->mapping;
108 if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
109 return mapping->a_ops->sync_page(page);
110 return 0;
114 * filemap_fdatawrite - start writeback against all of a mapping's dirty pages
115 * @mapping: address space structure to write
117 * This is a "data integrity" operation, as opposed to a regular memory
118 * cleansing writeback. The difference between these two operations is that
119 * if a dirty page/buffer is encountered, it must be waited upon, and not just
120 * skipped over.
122 static int __filemap_fdatawrite(struct address_space *mapping, int sync_mode)
124 int ret;
125 struct writeback_control wbc = {
126 .sync_mode = sync_mode,
127 .nr_to_write = mapping->nrpages * 2,
130 if (mapping->backing_dev_info->memory_backed)
131 return 0;
133 spin_lock(&mapping->page_lock);
134 list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
135 spin_unlock(&mapping->page_lock);
136 ret = do_writepages(mapping, &wbc);
137 return ret;
140 int filemap_fdatawrite(struct address_space *mapping)
142 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
146 * This is a mostly non-blocking flush. Not suitable for data-integrity
147 * purposes.
149 int filemap_flush(struct address_space *mapping)
151 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
155 * filemap_fdatawait - walk the list of locked pages of the given address
156 * space and wait for all of them.
157 * @mapping: address space structure to wait for
159 int filemap_fdatawait(struct address_space * mapping)
161 int ret = 0;
162 int progress;
164 restart:
165 progress = 0;
166 spin_lock(&mapping->page_lock);
167 while (!list_empty(&mapping->locked_pages)) {
168 struct page *page;
170 page = list_entry(mapping->locked_pages.next,struct page,list);
171 list_del(&page->list);
172 if (PageDirty(page))
173 list_add(&page->list, &mapping->dirty_pages);
174 else
175 list_add(&page->list, &mapping->clean_pages);
177 if (!PageWriteback(page)) {
178 if (++progress > 32) {
179 if (need_resched()) {
180 spin_unlock(&mapping->page_lock);
181 __cond_resched();
182 goto restart;
185 continue;
188 progress = 0;
189 page_cache_get(page);
190 spin_unlock(&mapping->page_lock);
192 wait_on_page_writeback(page);
193 if (PageError(page))
194 ret = -EIO;
196 page_cache_release(page);
197 spin_lock(&mapping->page_lock);
199 spin_unlock(&mapping->page_lock);
200 return ret;
204 * This adds a page to the page cache, starting out as locked, unreferenced,
205 * not uptodate and with no errors.
207 * This function is used for two things: adding newly allocated pagecache
208 * pages and for moving existing anon pages into swapcache.
210 * In the case of pagecache pages, the page is new, so we can just run
211 * SetPageLocked() against it. The other page state flags were set by
212 * rmqueue()
214 * In the case of swapcache, try_to_swap_out() has already locked the page, so
215 * SetPageLocked() is ugly-but-OK there too. The required page state has been
216 * set up by swap_out_add_to_swap_cache().
218 * This function does not add the page to the LRU. The caller must do that.
220 int add_to_page_cache(struct page *page, struct address_space *mapping,
221 pgoff_t offset, int gfp_mask)
223 int error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
225 if (error == 0) {
226 page_cache_get(page);
227 spin_lock(&mapping->page_lock);
228 error = radix_tree_insert(&mapping->page_tree, offset, page);
229 if (!error) {
230 SetPageLocked(page);
231 ___add_to_page_cache(page, mapping, offset);
232 } else {
233 page_cache_release(page);
235 spin_unlock(&mapping->page_lock);
236 radix_tree_preload_end();
238 return error;
240 EXPORT_SYMBOL(add_to_page_cache);
242 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
243 pgoff_t offset, int gfp_mask)
245 int ret = add_to_page_cache(page, mapping, offset, gfp_mask);
246 if (ret == 0)
247 lru_cache_add(page);
248 return ret;
252 * In order to wait for pages to become available there must be
253 * waitqueues associated with pages. By using a hash table of
254 * waitqueues where the bucket discipline is to maintain all
255 * waiters on the same queue and wake all when any of the pages
256 * become available, and for the woken contexts to check to be
257 * sure the appropriate page became available, this saves space
258 * at a cost of "thundering herd" phenomena during rare hash
259 * collisions.
261 static wait_queue_head_t *page_waitqueue(struct page *page)
263 const struct zone *zone = page_zone(page);
265 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
268 void wait_on_page_bit(struct page *page, int bit_nr)
270 wait_queue_head_t *waitqueue = page_waitqueue(page);
271 DEFINE_WAIT(wait);
273 do {
274 prepare_to_wait(waitqueue, &wait, TASK_UNINTERRUPTIBLE);
275 if (test_bit(bit_nr, &page->flags)) {
276 sync_page(page);
277 io_schedule();
279 } while (test_bit(bit_nr, &page->flags));
280 finish_wait(waitqueue, &wait);
282 EXPORT_SYMBOL(wait_on_page_bit);
285 * unlock_page() - unlock a locked page
287 * @page: the page
289 * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
290 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
291 * mechananism between PageLocked pages and PageWriteback pages is shared.
292 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
294 * The first mb is necessary to safely close the critical section opened by the
295 * TestSetPageLocked(), the second mb is necessary to enforce ordering between
296 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
297 * parallel wait_on_page_locked()).
299 void unlock_page(struct page *page)
301 wait_queue_head_t *waitqueue = page_waitqueue(page);
302 smp_mb__before_clear_bit();
303 if (!TestClearPageLocked(page))
304 BUG();
305 smp_mb__after_clear_bit();
306 if (waitqueue_active(waitqueue))
307 wake_up_all(waitqueue);
311 * End writeback against a page.
313 void end_page_writeback(struct page *page)
315 wait_queue_head_t *waitqueue = page_waitqueue(page);
317 if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
318 smp_mb__before_clear_bit();
319 if (!TestClearPageWriteback(page))
320 BUG();
321 smp_mb__after_clear_bit();
323 if (waitqueue_active(waitqueue))
324 wake_up_all(waitqueue);
326 EXPORT_SYMBOL(end_page_writeback);
329 * Get a lock on the page, assuming we need to sleep to get it.
331 * Ugly: running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
332 * random driver's requestfn sets TASK_RUNNING, we could busywait. However
333 * chances are that on the second loop, the block layer's plug list is empty,
334 * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
336 void __lock_page(struct page *page)
338 wait_queue_head_t *wqh = page_waitqueue(page);
339 DEFINE_WAIT(wait);
341 while (TestSetPageLocked(page)) {
342 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
343 if (PageLocked(page)) {
344 sync_page(page);
345 io_schedule();
348 finish_wait(wqh, &wait);
350 EXPORT_SYMBOL(__lock_page);
353 * a rather lightweight function, finding and getting a reference to a
354 * hashed page atomically.
356 struct page * find_get_page(struct address_space *mapping, unsigned long offset)
358 struct page *page;
361 * We scan the hash list read-only. Addition to and removal from
362 * the hash-list needs a held write-lock.
364 spin_lock(&mapping->page_lock);
365 page = radix_tree_lookup(&mapping->page_tree, offset);
366 if (page)
367 page_cache_get(page);
368 spin_unlock(&mapping->page_lock);
369 return page;
373 * Same as above, but trylock it instead of incrementing the count.
375 struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
377 struct page *page;
379 spin_lock(&mapping->page_lock);
380 page = radix_tree_lookup(&mapping->page_tree, offset);
381 if (page && TestSetPageLocked(page))
382 page = NULL;
383 spin_unlock(&mapping->page_lock);
384 return page;
388 * find_lock_page - locate, pin and lock a pagecache page
390 * @mapping - the address_space to search
391 * @offset - the page index
393 * Locates the desired pagecache page, locks it, increments its reference
394 * count and returns its address.
396 * Returns zero if the page was not present. find_lock_page() may sleep.
398 struct page *find_lock_page(struct address_space *mapping,
399 unsigned long offset)
401 struct page *page;
403 spin_lock(&mapping->page_lock);
404 repeat:
405 page = radix_tree_lookup(&mapping->page_tree, offset);
406 if (page) {
407 page_cache_get(page);
408 if (TestSetPageLocked(page)) {
409 spin_unlock(&mapping->page_lock);
410 lock_page(page);
411 spin_lock(&mapping->page_lock);
413 /* Has the page been truncated while we slept? */
414 if (page->mapping != mapping || page->index != offset) {
415 unlock_page(page);
416 page_cache_release(page);
417 goto repeat;
421 spin_unlock(&mapping->page_lock);
422 return page;
426 * find_or_create_page - locate or add a pagecache page
428 * @mapping - the page's address_space
429 * @index - the page's index into the mapping
430 * @gfp_mask - page allocation mode
432 * Locates a page in the pagecache. If the page is not present, a new page
433 * is allocated using @gfp_mask and is added to the pagecache and to the VM's
434 * LRU list. The returned page is locked and has its reference count
435 * incremented.
437 * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
438 * allocation!
440 * find_or_create_page() returns the desired page's address, or zero on
441 * memory exhaustion.
443 struct page *find_or_create_page(struct address_space *mapping,
444 unsigned long index, unsigned int gfp_mask)
446 struct page *page, *cached_page = NULL;
447 int err;
448 repeat:
449 page = find_lock_page(mapping, index);
450 if (!page) {
451 if (!cached_page) {
452 cached_page = alloc_page(gfp_mask);
453 if (!cached_page)
454 return NULL;
456 err = add_to_page_cache_lru(cached_page, mapping,
457 index, gfp_mask);
458 if (!err) {
459 page = cached_page;
460 cached_page = NULL;
461 } else if (err == -EEXIST)
462 goto repeat;
464 if (cached_page)
465 page_cache_release(cached_page);
466 return page;
470 * find_get_pages - gang pagecache lookup
471 * @mapping: The address_space to search
472 * @start: The starting page index
473 * @nr_pages: The maximum number of pages
474 * @pages: Where the resulting pages are placed
476 * find_get_pages() will search for and return a group of up to
477 * @nr_pages pages in the mapping. The pages are placed at @pages.
478 * find_get_pages() takes a reference against the returned pages.
480 * The search returns a group of mapping-contiguous pages with ascending
481 * indexes. There may be holes in the indices due to not-present pages.
483 * find_get_pages() returns the number of pages which were found.
485 unsigned int find_get_pages(struct address_space *mapping, pgoff_t start,
486 unsigned int nr_pages, struct page **pages)
488 unsigned int i;
489 unsigned int ret;
491 spin_lock(&mapping->page_lock);
492 ret = radix_tree_gang_lookup(&mapping->page_tree,
493 (void **)pages, start, nr_pages);
494 for (i = 0; i < ret; i++)
495 page_cache_get(pages[i]);
496 spin_unlock(&mapping->page_lock);
497 return ret;
501 * Same as grab_cache_page, but do not wait if the page is unavailable.
502 * This is intended for speculative data generators, where the data can
503 * be regenerated if the page couldn't be grabbed. This routine should
504 * be safe to call while holding the lock for another page.
506 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
507 * and deadlock against the caller's locked page.
509 struct page *
510 grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
512 struct page *page = find_get_page(mapping, index);
513 int gfp_mask;
515 if (page) {
516 if (!TestSetPageLocked(page))
517 return page;
518 page_cache_release(page);
519 return NULL;
521 gfp_mask = mapping->gfp_mask & ~__GFP_FS;
522 page = alloc_pages(gfp_mask, 0);
523 if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
524 page_cache_release(page);
525 page = NULL;
527 return page;
531 * This is a generic file read routine, and uses the
532 * inode->i_op->readpage() function for the actual low-level
533 * stuff.
535 * This is really ugly. But the goto's actually try to clarify some
536 * of the logic when it comes to error handling etc.
537 * - note the struct file * is only passed for the use of readpage
539 void do_generic_mapping_read(struct address_space *mapping,
540 struct file_ra_state *ra,
541 struct file * filp,
542 loff_t *ppos,
543 read_descriptor_t * desc,
544 read_actor_t actor)
546 struct inode *inode = mapping->host;
547 unsigned long index, offset;
548 struct page *cached_page;
549 int error;
551 cached_page = NULL;
552 index = *ppos >> PAGE_CACHE_SHIFT;
553 offset = *ppos & ~PAGE_CACHE_MASK;
555 for (;;) {
556 struct page *page;
557 unsigned long end_index, nr, ret;
558 loff_t isize = i_size_read(inode);
560 end_index = isize >> PAGE_CACHE_SHIFT;
562 if (index > end_index)
563 break;
564 nr = PAGE_CACHE_SIZE;
565 if (index == end_index) {
566 nr = isize & ~PAGE_CACHE_MASK;
567 if (nr <= offset)
568 break;
571 cond_resched();
572 page_cache_readahead(mapping, ra, filp, index);
574 nr = nr - offset;
575 find_page:
576 page = find_get_page(mapping, index);
577 if (unlikely(page == NULL)) {
578 handle_ra_miss(mapping, ra, index);
579 goto no_cached_page;
581 if (!PageUptodate(page))
582 goto page_not_up_to_date;
583 page_ok:
584 /* If users can be writing to this page using arbitrary
585 * virtual addresses, take care about potential aliasing
586 * before reading the page on the kernel side.
588 if (!list_empty(&mapping->i_mmap_shared))
589 flush_dcache_page(page);
592 * Mark the page accessed if we read the beginning.
594 if (!offset)
595 mark_page_accessed(page);
598 * Ok, we have the page, and it's up-to-date, so
599 * now we can copy it to user space...
601 * The actor routine returns how many bytes were actually used..
602 * NOTE! This may not be the same as how much of a user buffer
603 * we filled up (we may be padding etc), so we can only update
604 * "pos" here (the actor routine has to update the user buffer
605 * pointers and the remaining count).
607 ret = actor(desc, page, offset, nr);
608 offset += ret;
609 index += offset >> PAGE_CACHE_SHIFT;
610 offset &= ~PAGE_CACHE_MASK;
612 page_cache_release(page);
613 if (ret == nr && desc->count)
614 continue;
615 break;
617 page_not_up_to_date:
618 if (PageUptodate(page))
619 goto page_ok;
621 /* Get exclusive access to the page ... */
622 lock_page(page);
624 /* Did it get unhashed before we got the lock? */
625 if (!page->mapping) {
626 unlock_page(page);
627 page_cache_release(page);
628 continue;
631 /* Did somebody else fill it already? */
632 if (PageUptodate(page)) {
633 unlock_page(page);
634 goto page_ok;
637 readpage:
638 /* ... and start the actual read. The read will unlock the page. */
639 error = mapping->a_ops->readpage(filp, page);
641 if (!error) {
642 if (PageUptodate(page))
643 goto page_ok;
644 wait_on_page_locked(page);
645 if (PageUptodate(page))
646 goto page_ok;
647 error = -EIO;
650 /* UHHUH! A synchronous read error occurred. Report it */
651 desc->error = error;
652 page_cache_release(page);
653 break;
655 no_cached_page:
657 * Ok, it wasn't cached, so we need to create a new
658 * page..
660 if (!cached_page) {
661 cached_page = page_cache_alloc_cold(mapping);
662 if (!cached_page) {
663 desc->error = -ENOMEM;
664 break;
667 error = add_to_page_cache_lru(cached_page, mapping,
668 index, GFP_KERNEL);
669 if (error) {
670 if (error == -EEXIST)
671 goto find_page;
672 desc->error = error;
673 break;
675 page = cached_page;
676 cached_page = NULL;
677 goto readpage;
680 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
681 if (cached_page)
682 page_cache_release(cached_page);
683 update_atime(inode);
686 int file_read_actor(read_descriptor_t *desc, struct page *page,
687 unsigned long offset, unsigned long size)
689 char *kaddr;
690 unsigned long left, count = desc->count;
692 if (size > count)
693 size = count;
696 * Faults on the destination of a read are common, so do it before
697 * taking the kmap.
699 if (!fault_in_pages_writeable(desc->buf, size)) {
700 kaddr = kmap_atomic(page, KM_USER0);
701 left = __copy_to_user(desc->buf, kaddr + offset, size);
702 kunmap_atomic(kaddr, KM_USER0);
703 if (left == 0)
704 goto success;
707 /* Do it the slow way */
708 kaddr = kmap(page);
709 left = __copy_to_user(desc->buf, kaddr + offset, size);
710 kunmap(page);
712 if (left) {
713 size -= left;
714 desc->error = -EFAULT;
716 success:
717 desc->count = count - size;
718 desc->written += size;
719 desc->buf += size;
720 return size;
724 * This is the "read()" routine for all filesystems
725 * that can use the page cache directly.
727 static ssize_t
728 __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
729 unsigned long nr_segs, loff_t *ppos)
731 struct file *filp = iocb->ki_filp;
732 ssize_t retval;
733 unsigned long seg;
734 size_t count;
736 count = 0;
737 for (seg = 0; seg < nr_segs; seg++) {
738 const struct iovec *iv = &iov[seg];
741 * If any segment has a negative length, or the cumulative
742 * length ever wraps negative then return -EINVAL.
744 count += iv->iov_len;
745 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
746 return -EINVAL;
747 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
748 continue;
749 if (seg == 0)
750 return -EFAULT;
751 nr_segs = seg;
752 count -= iv->iov_len; /* This segment is no good */
753 break;
756 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
757 if (filp->f_flags & O_DIRECT) {
758 loff_t pos = *ppos, size;
759 struct address_space *mapping;
760 struct inode *inode;
762 mapping = filp->f_dentry->d_inode->i_mapping;
763 inode = mapping->host;
764 retval = 0;
765 if (!count)
766 goto out; /* skip atime */
767 size = i_size_read(inode);
768 if (pos < size) {
769 retval = generic_file_direct_IO(READ, iocb,
770 iov, pos, nr_segs);
771 if (retval >= 0 && !is_sync_kiocb(iocb))
772 retval = -EIOCBQUEUED;
773 if (retval > 0)
774 *ppos = pos + retval;
776 update_atime(filp->f_dentry->d_inode);
777 goto out;
780 retval = 0;
781 if (count) {
782 for (seg = 0; seg < nr_segs; seg++) {
783 read_descriptor_t desc;
785 desc.written = 0;
786 desc.buf = iov[seg].iov_base;
787 desc.count = iov[seg].iov_len;
788 if (desc.count == 0)
789 continue;
790 desc.error = 0;
791 do_generic_file_read(filp,ppos,&desc,file_read_actor);
792 retval += desc.written;
793 if (!retval) {
794 retval = desc.error;
795 break;
799 out:
800 return retval;
803 ssize_t
804 generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
806 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
808 BUG_ON(iocb->ki_pos != pos);
809 return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
811 EXPORT_SYMBOL(generic_file_aio_read);
813 ssize_t
814 generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
816 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
817 struct kiocb kiocb;
818 ssize_t ret;
820 init_sync_kiocb(&kiocb, filp);
821 ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
822 if (-EIOCBQUEUED == ret)
823 ret = wait_on_sync_kiocb(&kiocb);
824 return ret;
827 int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
829 ssize_t written;
830 unsigned long count = desc->count;
831 struct file *file = (struct file *) desc->buf;
833 if (size > count)
834 size = count;
836 written = file->f_op->sendpage(file, page, offset,
837 size, &file->f_pos, size<count);
838 if (written < 0) {
839 desc->error = written;
840 written = 0;
842 desc->count = count - written;
843 desc->written += written;
844 return written;
847 ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
848 size_t count, read_actor_t actor, void __user *target)
850 read_descriptor_t desc;
852 if (!count)
853 return 0;
855 desc.written = 0;
856 desc.count = count;
857 desc.buf = target;
858 desc.error = 0;
860 do_generic_file_read(in_file, ppos, &desc, actor);
861 if (desc.written)
862 return desc.written;
863 return desc.error;
866 static ssize_t
867 do_readahead(struct address_space *mapping, struct file *filp,
868 unsigned long index, unsigned long nr)
870 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
871 return -EINVAL;
873 do_page_cache_readahead(mapping, filp, index, max_sane_readahead(nr));
874 return 0;
877 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
879 ssize_t ret;
880 struct file *file;
882 ret = -EBADF;
883 file = fget(fd);
884 if (file) {
885 if (file->f_mode & FMODE_READ) {
886 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
887 unsigned long start = offset >> PAGE_CACHE_SHIFT;
888 unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
889 unsigned long len = end - start + 1;
890 ret = do_readahead(mapping, file, start, len);
892 fput(file);
894 return ret;
897 #ifdef CONFIG_MMU
899 * This adds the requested page to the page cache if it isn't already there,
900 * and schedules an I/O to read in its contents from disk.
902 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
903 static int page_cache_read(struct file * file, unsigned long offset)
905 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
906 struct page *page;
907 int error;
909 page = page_cache_alloc_cold(mapping);
910 if (!page)
911 return -ENOMEM;
913 error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
914 if (!error) {
915 error = mapping->a_ops->readpage(file, page);
916 page_cache_release(page);
917 return error;
921 * We arrive here in the unlikely event that someone
922 * raced with us and added our page to the cache first
923 * or we are out of memory for radix-tree nodes.
925 page_cache_release(page);
926 return error == -EEXIST ? 0 : error;
929 #define MMAP_READAROUND (16UL)
930 #define MMAP_LOTSAMISS (100)
933 * filemap_nopage() is invoked via the vma operations vector for a
934 * mapped memory region to read in file data during a page fault.
936 * The goto's are kind of ugly, but this streamlines the normal case of having
937 * it in the page cache, and handles the special cases reasonably without
938 * having a lot of duplicated code.
940 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int unused)
942 int error;
943 struct file *file = area->vm_file;
944 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
945 struct file_ra_state *ra = &file->f_ra;
946 struct inode *inode = mapping->host;
947 struct page *page;
948 unsigned long size, pgoff, endoff;
949 int did_readaround = 0;
951 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
952 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
954 retry_all:
955 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
956 if (pgoff >= size)
957 goto outside_data_content;
959 /* If we don't want any read-ahead, don't bother */
960 if (VM_RandomReadHint(area))
961 goto no_cached_page;
964 * The "size" of the file, as far as mmap is concerned, isn't bigger
965 * than the mapping
967 if (size > endoff)
968 size = endoff;
971 * The readahead code wants to be told about each and every page
972 * so it can build and shrink its windows appropriately
974 * For sequential accesses, we use the generic readahead logic.
976 if (VM_SequentialReadHint(area))
977 page_cache_readahead(mapping, ra, file, pgoff);
980 * Do we have something in the page cache already?
982 retry_find:
983 page = find_get_page(mapping, pgoff);
984 if (!page) {
985 if (VM_SequentialReadHint(area)) {
986 handle_ra_miss(mapping, ra, pgoff);
987 goto no_cached_page;
989 ra->mmap_miss++;
992 * Do we miss much more than hit in this file? If so,
993 * stop bothering with read-ahead. It will only hurt.
995 if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
996 goto no_cached_page;
998 did_readaround = 1;
999 do_page_cache_readahead(mapping, file, pgoff & ~(MMAP_READAROUND-1), MMAP_READAROUND);
1000 goto retry_find;
1003 if (!did_readaround)
1004 ra->mmap_hit++;
1007 * Ok, found a page in the page cache, now we need to check
1008 * that it's up-to-date.
1010 if (!PageUptodate(page))
1011 goto page_not_uptodate;
1013 success:
1015 * Found the page and have a reference on it.
1017 mark_page_accessed(page);
1018 return page;
1020 outside_data_content:
1022 * An external ptracer can access pages that normally aren't
1023 * accessible..
1025 if (area->vm_mm == current->mm)
1026 return NULL;
1027 /* Fall through to the non-read-ahead case */
1028 no_cached_page:
1030 * We're only likely to ever get here if MADV_RANDOM is in
1031 * effect.
1033 error = page_cache_read(file, pgoff);
1036 * The page we want has now been added to the page cache.
1037 * In the unlikely event that someone removed it in the
1038 * meantime, we'll just come back here and read it again.
1040 if (error >= 0)
1041 goto retry_find;
1044 * An error return from page_cache_read can result if the
1045 * system is low on memory, or a problem occurs while trying
1046 * to schedule I/O.
1048 if (error == -ENOMEM)
1049 return NOPAGE_OOM;
1050 return NULL;
1052 page_not_uptodate:
1053 inc_page_state(pgmajfault);
1054 lock_page(page);
1056 /* Did it get unhashed while we waited for it? */
1057 if (!page->mapping) {
1058 unlock_page(page);
1059 page_cache_release(page);
1060 goto retry_all;
1063 /* Did somebody else get it up-to-date? */
1064 if (PageUptodate(page)) {
1065 unlock_page(page);
1066 goto success;
1069 if (!mapping->a_ops->readpage(file, page)) {
1070 wait_on_page_locked(page);
1071 if (PageUptodate(page))
1072 goto success;
1076 * Umm, take care of errors if the page isn't up-to-date.
1077 * Try to re-read it _once_. We do this synchronously,
1078 * because there really aren't any performance issues here
1079 * and we need to check for errors.
1081 lock_page(page);
1083 /* Somebody truncated the page on us? */
1084 if (!page->mapping) {
1085 unlock_page(page);
1086 page_cache_release(page);
1087 goto retry_all;
1090 /* Somebody else successfully read it in? */
1091 if (PageUptodate(page)) {
1092 unlock_page(page);
1093 goto success;
1095 ClearPageError(page);
1096 if (!mapping->a_ops->readpage(file, page)) {
1097 wait_on_page_locked(page);
1098 if (PageUptodate(page))
1099 goto success;
1103 * Things didn't work out. Return zero to tell the
1104 * mm layer so, possibly freeing the page cache page first.
1106 page_cache_release(page);
1107 return NULL;
1110 static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
1111 int nonblock)
1113 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
1114 struct page *page;
1115 int error;
1118 * Do we have something in the page cache already?
1120 retry_find:
1121 page = find_get_page(mapping, pgoff);
1122 if (!page) {
1123 if (nonblock)
1124 return NULL;
1125 goto no_cached_page;
1129 * Ok, found a page in the page cache, now we need to check
1130 * that it's up-to-date.
1132 if (!PageUptodate(page))
1133 goto page_not_uptodate;
1135 success:
1137 * Found the page and have a reference on it.
1139 mark_page_accessed(page);
1140 return page;
1142 no_cached_page:
1143 error = page_cache_read(file, pgoff);
1146 * The page we want has now been added to the page cache.
1147 * In the unlikely event that someone removed it in the
1148 * meantime, we'll just come back here and read it again.
1150 if (error >= 0)
1151 goto retry_find;
1154 * An error return from page_cache_read can result if the
1155 * system is low on memory, or a problem occurs while trying
1156 * to schedule I/O.
1158 return NULL;
1160 page_not_uptodate:
1161 lock_page(page);
1163 /* Did it get unhashed while we waited for it? */
1164 if (!page->mapping) {
1165 unlock_page(page);
1166 goto err;
1169 /* Did somebody else get it up-to-date? */
1170 if (PageUptodate(page)) {
1171 unlock_page(page);
1172 goto success;
1175 if (!mapping->a_ops->readpage(file, page)) {
1176 wait_on_page_locked(page);
1177 if (PageUptodate(page))
1178 goto success;
1182 * Umm, take care of errors if the page isn't up-to-date.
1183 * Try to re-read it _once_. We do this synchronously,
1184 * because there really aren't any performance issues here
1185 * and we need to check for errors.
1187 lock_page(page);
1189 /* Somebody truncated the page on us? */
1190 if (!page->mapping) {
1191 unlock_page(page);
1192 goto err;
1194 /* Somebody else successfully read it in? */
1195 if (PageUptodate(page)) {
1196 unlock_page(page);
1197 goto success;
1200 ClearPageError(page);
1201 if (!mapping->a_ops->readpage(file, page)) {
1202 wait_on_page_locked(page);
1203 if (PageUptodate(page))
1204 goto success;
1208 * Things didn't work out. Return zero to tell the
1209 * mm layer so, possibly freeing the page cache page first.
1211 err:
1212 page_cache_release(page);
1214 return NULL;
1217 static int filemap_populate(struct vm_area_struct *vma,
1218 unsigned long addr,
1219 unsigned long len,
1220 pgprot_t prot,
1221 unsigned long pgoff,
1222 int nonblock)
1224 struct file *file = vma->vm_file;
1225 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
1226 struct inode *inode = mapping->host;
1227 unsigned long size;
1228 struct mm_struct *mm = vma->vm_mm;
1229 struct page *page;
1230 int err;
1232 if (!nonblock)
1233 do_page_cache_readahead(mapping, vma->vm_file,
1234 pgoff, len >> PAGE_CACHE_SHIFT);
1236 repeat:
1237 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1238 if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
1239 return -EINVAL;
1241 page = filemap_getpage(file, pgoff, nonblock);
1242 if (!page && !nonblock)
1243 return -ENOMEM;
1244 if (page) {
1245 err = install_page(mm, vma, addr, page, prot);
1246 if (err) {
1247 page_cache_release(page);
1248 return err;
1252 len -= PAGE_SIZE;
1253 addr += PAGE_SIZE;
1254 pgoff++;
1255 if (len)
1256 goto repeat;
1258 return 0;
1261 static struct vm_operations_struct generic_file_vm_ops = {
1262 .nopage = filemap_nopage,
1263 .populate = filemap_populate,
1266 /* This is used for a general mmap of a disk file */
1268 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1270 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
1271 struct inode *inode = mapping->host;
1273 if (!mapping->a_ops->readpage)
1274 return -ENOEXEC;
1275 update_atime(inode);
1276 vma->vm_ops = &generic_file_vm_ops;
1277 return 0;
1281 * This is for filesystems which do not implement ->writepage.
1283 int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1285 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1286 return -EINVAL;
1287 return generic_file_mmap(file, vma);
1289 #else
1290 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1292 return -ENOSYS;
1294 int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1296 return -ENOSYS;
1298 #endif /* CONFIG_MMU */
1300 static inline struct page *__read_cache_page(struct address_space *mapping,
1301 unsigned long index,
1302 int (*filler)(void *,struct page*),
1303 void *data)
1305 struct page *page, *cached_page = NULL;
1306 int err;
1307 repeat:
1308 page = find_get_page(mapping, index);
1309 if (!page) {
1310 if (!cached_page) {
1311 cached_page = page_cache_alloc_cold(mapping);
1312 if (!cached_page)
1313 return ERR_PTR(-ENOMEM);
1315 err = add_to_page_cache_lru(cached_page, mapping,
1316 index, GFP_KERNEL);
1317 if (err == -EEXIST)
1318 goto repeat;
1319 if (err < 0) {
1320 /* Presumably ENOMEM for radix tree node */
1321 page_cache_release(cached_page);
1322 return ERR_PTR(err);
1324 page = cached_page;
1325 cached_page = NULL;
1326 err = filler(data, page);
1327 if (err < 0) {
1328 page_cache_release(page);
1329 page = ERR_PTR(err);
1332 if (cached_page)
1333 page_cache_release(cached_page);
1334 return page;
1338 * Read into the page cache. If a page already exists,
1339 * and PageUptodate() is not set, try to fill the page.
1341 struct page *read_cache_page(struct address_space *mapping,
1342 unsigned long index,
1343 int (*filler)(void *,struct page*),
1344 void *data)
1346 struct page *page;
1347 int err;
1349 retry:
1350 page = __read_cache_page(mapping, index, filler, data);
1351 if (IS_ERR(page))
1352 goto out;
1353 mark_page_accessed(page);
1354 if (PageUptodate(page))
1355 goto out;
1357 lock_page(page);
1358 if (!page->mapping) {
1359 unlock_page(page);
1360 page_cache_release(page);
1361 goto retry;
1363 if (PageUptodate(page)) {
1364 unlock_page(page);
1365 goto out;
1367 err = filler(data, page);
1368 if (err < 0) {
1369 page_cache_release(page);
1370 page = ERR_PTR(err);
1372 out:
1373 return page;
1377 * If the page was newly created, increment its refcount and add it to the
1378 * caller's lru-buffering pagevec. This function is specifically for
1379 * generic_file_write().
1381 static inline struct page *
1382 __grab_cache_page(struct address_space *mapping, unsigned long index,
1383 struct page **cached_page, struct pagevec *lru_pvec)
1385 int err;
1386 struct page *page;
1387 repeat:
1388 page = find_lock_page(mapping, index);
1389 if (!page) {
1390 if (!*cached_page) {
1391 *cached_page = page_cache_alloc(mapping);
1392 if (!*cached_page)
1393 return NULL;
1395 err = add_to_page_cache(*cached_page, mapping,
1396 index, GFP_KERNEL);
1397 if (err == -EEXIST)
1398 goto repeat;
1399 if (err == 0) {
1400 page = *cached_page;
1401 page_cache_get(page);
1402 if (!pagevec_add(lru_pvec, page))
1403 __pagevec_lru_add(lru_pvec);
1404 *cached_page = NULL;
1407 return page;
1410 void remove_suid(struct dentry *dentry)
1412 struct iattr newattrs;
1413 struct inode *inode = dentry->d_inode;
1414 unsigned int mode = inode->i_mode & (S_ISUID|S_ISGID|S_IXGRP);
1416 if (!(mode & S_IXGRP))
1417 mode &= S_ISUID;
1419 /* was any of the uid bits set? */
1420 if (mode && !capable(CAP_FSETID)) {
1421 newattrs.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
1422 notify_change(dentry, &newattrs);
1427 * Copy as much as we can into the page and return the number of bytes which
1428 * were sucessfully copied. If a fault is encountered then clear the page
1429 * out to (offset+bytes) and return the number of bytes which were copied.
1431 static inline size_t
1432 filemap_copy_from_user(struct page *page, unsigned long offset,
1433 const char __user *buf, unsigned bytes)
1435 char *kaddr;
1436 int left;
1438 kaddr = kmap_atomic(page, KM_USER0);
1439 left = __copy_from_user(kaddr + offset, buf, bytes);
1440 kunmap_atomic(kaddr, KM_USER0);
1442 if (left != 0) {
1443 /* Do it the slow way */
1444 kaddr = kmap(page);
1445 left = __copy_from_user(kaddr + offset, buf, bytes);
1446 kunmap(page);
1448 return bytes - left;
1451 static size_t
1452 __filemap_copy_from_user_iovec(char *vaddr,
1453 const struct iovec *iov, size_t base, size_t bytes)
1455 size_t copied = 0, left = 0;
1457 while (bytes) {
1458 char __user *buf = iov->iov_base + base;
1459 int copy = min(bytes, iov->iov_len - base);
1461 base = 0;
1462 left = __copy_from_user(vaddr, buf, copy);
1463 copied += copy;
1464 bytes -= copy;
1465 vaddr += copy;
1466 iov++;
1468 if (unlikely(left)) {
1469 /* zero the rest of the target like __copy_from_user */
1470 if (bytes)
1471 memset(vaddr, 0, bytes);
1472 break;
1475 return copied - left;
1479 * This has the same sideeffects and return value as filemap_copy_from_user().
1480 * The difference is that on a fault we need to memset the remainder of the
1481 * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
1482 * single-segment behaviour.
1484 static inline size_t
1485 filemap_copy_from_user_iovec(struct page *page, unsigned long offset,
1486 const struct iovec *iov, size_t base, size_t bytes)
1488 char *kaddr;
1489 size_t copied;
1491 kaddr = kmap_atomic(page, KM_USER0);
1492 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1493 base, bytes);
1494 kunmap_atomic(kaddr, KM_USER0);
1495 if (copied != bytes) {
1496 kaddr = kmap(page);
1497 copied = __filemap_copy_from_user_iovec(kaddr + offset, iov,
1498 base, bytes);
1499 kunmap(page);
1501 return copied;
1504 static inline void
1505 filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1507 const struct iovec *iov = *iovp;
1508 size_t base = *basep;
1510 while (bytes) {
1511 int copy = min(bytes, iov->iov_len - base);
1513 bytes -= copy;
1514 base += copy;
1515 if (iov->iov_len == base) {
1516 iov++;
1517 base = 0;
1520 *iovp = iov;
1521 *basep = base;
1525 * Performs necessary checks before doing a write
1527 * Can adjust writing position aor amount of bytes to write.
1528 * Returns appropriate error code that caller should return or
1529 * zero in case that write should be allowed.
1531 inline int generic_write_checks(struct inode *inode,
1532 struct file *file, loff_t *pos, size_t *count, int isblk)
1534 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1536 if (unlikely(*pos < 0))
1537 return -EINVAL;
1539 if (unlikely(file->f_error)) {
1540 int err = file->f_error;
1541 file->f_error = 0;
1542 return err;
1545 if (!isblk) {
1546 /* FIXME: this is for backwards compatibility with 2.4 */
1547 if (file->f_flags & O_APPEND)
1548 *pos = i_size_read(inode);
1550 if (limit != RLIM_INFINITY) {
1551 if (*pos >= limit) {
1552 send_sig(SIGXFSZ, current, 0);
1553 return -EFBIG;
1555 if (*count > limit - (typeof(limit))*pos) {
1556 *count = limit - (typeof(limit))*pos;
1562 * LFS rule
1564 if (unlikely(*pos + *count > MAX_NON_LFS &&
1565 !(file->f_flags & O_LARGEFILE))) {
1566 if (*pos >= MAX_NON_LFS) {
1567 send_sig(SIGXFSZ, current, 0);
1568 return -EFBIG;
1570 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
1571 *count = MAX_NON_LFS - (unsigned long)*pos;
1576 * Are we about to exceed the fs block limit ?
1578 * If we have written data it becomes a short write. If we have
1579 * exceeded without writing data we send a signal and return EFBIG.
1580 * Linus frestrict idea will clean these up nicely..
1582 if (likely(!isblk)) {
1583 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
1584 if (*count || *pos > inode->i_sb->s_maxbytes) {
1585 send_sig(SIGXFSZ, current, 0);
1586 return -EFBIG;
1588 /* zero-length writes at ->s_maxbytes are OK */
1591 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
1592 *count = inode->i_sb->s_maxbytes - *pos;
1593 } else {
1594 loff_t isize;
1595 if (bdev_read_only(inode->i_bdev))
1596 return -EPERM;
1597 isize = i_size_read(inode);
1598 if (*pos >= isize) {
1599 if (*count || *pos > isize)
1600 return -ENOSPC;
1603 if (*pos + *count > isize)
1604 *count = isize - *pos;
1606 return 0;
1608 EXPORT_SYMBOL(generic_write_checks);
1611 * Write to a file through the page cache.
1613 * We put everything into the page cache prior to writing it. This is not a
1614 * problem when writing full pages. With partial pages, however, we first have
1615 * to read the data into the cache, then dirty the page, and finally schedule
1616 * it for writing by marking it dirty.
1617 * okir@monad.swb.de
1619 ssize_t
1620 generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
1621 unsigned long nr_segs, loff_t *ppos)
1623 struct file *file = iocb->ki_filp;
1624 struct address_space * mapping = file->f_dentry->d_inode->i_mapping;
1625 struct address_space_operations *a_ops = mapping->a_ops;
1626 size_t ocount; /* original count */
1627 size_t count; /* after file limit checks */
1628 struct inode *inode = mapping->host;
1629 long status = 0;
1630 loff_t pos;
1631 struct page *page;
1632 struct page *cached_page = NULL;
1633 const int isblk = S_ISBLK(inode->i_mode);
1634 ssize_t written;
1635 ssize_t err;
1636 size_t bytes;
1637 struct pagevec lru_pvec;
1638 const struct iovec *cur_iov = iov; /* current iovec */
1639 size_t iov_base = 0; /* offset in the current iovec */
1640 unsigned long seg;
1641 char __user *buf;
1643 ocount = 0;
1644 for (seg = 0; seg < nr_segs; seg++) {
1645 const struct iovec *iv = &iov[seg];
1648 * If any segment has a negative length, or the cumulative
1649 * length ever wraps negative then return -EINVAL.
1651 ocount += iv->iov_len;
1652 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
1653 return -EINVAL;
1654 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1655 continue;
1656 if (seg == 0)
1657 return -EFAULT;
1658 nr_segs = seg;
1659 ocount -= iv->iov_len; /* This segment is no good */
1660 break;
1663 count = ocount;
1664 pos = *ppos;
1665 pagevec_init(&lru_pvec, 0);
1667 /* We can write back this queue in page reclaim */
1668 current->backing_dev_info = mapping->backing_dev_info;
1669 written = 0;
1671 err = generic_write_checks(inode, file, &pos, &count, isblk);
1672 if (err)
1673 goto out;
1676 if (count == 0)
1677 goto out;
1679 remove_suid(file->f_dentry);
1680 inode_update_time(inode, 1);
1682 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
1683 if (unlikely(file->f_flags & O_DIRECT)) {
1684 if (count != ocount)
1685 nr_segs = iov_shorten((struct iovec *)iov,
1686 nr_segs, count);
1687 written = generic_file_direct_IO(WRITE, iocb,
1688 iov, pos, nr_segs);
1689 if (written > 0) {
1690 loff_t end = pos + written;
1691 if (end > i_size_read(inode) && !isblk) {
1692 i_size_write(inode, end);
1693 mark_inode_dirty(inode);
1695 *ppos = end;
1698 * Sync the fs metadata but not the minor inode changes and
1699 * of course not the data as we did direct DMA for the IO.
1701 if (written >= 0 && file->f_flags & O_SYNC)
1702 status = generic_osync_inode(inode, OSYNC_METADATA);
1703 if (written >= 0 && !is_sync_kiocb(iocb))
1704 written = -EIOCBQUEUED;
1705 goto out_status;
1708 buf = iov->iov_base;
1709 do {
1710 unsigned long index;
1711 unsigned long offset;
1712 size_t copied;
1714 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1715 index = pos >> PAGE_CACHE_SHIFT;
1716 bytes = PAGE_CACHE_SIZE - offset;
1717 if (bytes > count)
1718 bytes = count;
1721 * Bring in the user page that we will copy from _first_.
1722 * Otherwise there's a nasty deadlock on copying from the
1723 * same page as we're writing to, without it being marked
1724 * up-to-date.
1726 fault_in_pages_readable(buf, bytes);
1728 page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
1729 if (!page) {
1730 status = -ENOMEM;
1731 break;
1734 status = a_ops->prepare_write(file, page, offset, offset+bytes);
1735 if (unlikely(status)) {
1736 loff_t isize = i_size_read(inode);
1738 * prepare_write() may have instantiated a few blocks
1739 * outside i_size. Trim these off again.
1741 unlock_page(page);
1742 page_cache_release(page);
1743 if (pos + bytes > isize)
1744 vmtruncate(inode, isize);
1745 break;
1747 if (likely(nr_segs == 1))
1748 copied = filemap_copy_from_user(page, offset,
1749 buf, bytes);
1750 else
1751 copied = filemap_copy_from_user_iovec(page, offset,
1752 cur_iov, iov_base, bytes);
1753 flush_dcache_page(page);
1754 status = a_ops->commit_write(file, page, offset, offset+bytes);
1755 if (likely(copied > 0)) {
1756 if (!status)
1757 status = copied;
1759 if (status >= 0) {
1760 written += status;
1761 count -= status;
1762 pos += status;
1763 buf += status;
1764 if (unlikely(nr_segs > 1))
1765 filemap_set_next_iovec(&cur_iov,
1766 &iov_base, status);
1769 if (unlikely(copied != bytes))
1770 if (status >= 0)
1771 status = -EFAULT;
1773 if (!PageReferenced(page))
1774 SetPageReferenced(page);
1775 unlock_page(page);
1776 page_cache_release(page);
1777 if (status < 0)
1778 break;
1779 balance_dirty_pages_ratelimited(mapping);
1780 cond_resched();
1781 } while (count);
1782 *ppos = pos;
1784 if (cached_page)
1785 page_cache_release(cached_page);
1788 * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
1790 if (status >= 0) {
1791 if ((file->f_flags & O_SYNC) || IS_SYNC(inode))
1792 status = generic_osync_inode(inode,
1793 OSYNC_METADATA|OSYNC_DATA);
1796 out_status:
1797 err = written ? written : status;
1798 out:
1799 pagevec_lru_add(&lru_pvec);
1800 current->backing_dev_info = 0;
1801 return err;
1804 ssize_t
1805 generic_file_write_nolock(struct file *file, const struct iovec *iov,
1806 unsigned long nr_segs, loff_t *ppos)
1808 struct kiocb kiocb;
1809 ssize_t ret;
1811 init_sync_kiocb(&kiocb, file);
1812 ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
1813 if (-EIOCBQUEUED == ret)
1814 ret = wait_on_sync_kiocb(&kiocb);
1815 return ret;
1818 ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
1819 size_t count, loff_t pos)
1821 struct file *file = iocb->ki_filp;
1822 struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
1823 ssize_t err;
1824 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1826 BUG_ON(iocb->ki_pos != pos);
1828 down(&inode->i_sem);
1829 err = generic_file_aio_write_nolock(iocb, &local_iov, 1,
1830 &iocb->ki_pos);
1831 up(&inode->i_sem);
1833 return err;
1835 EXPORT_SYMBOL(generic_file_aio_write);
1836 EXPORT_SYMBOL(generic_file_aio_write_nolock);
1838 ssize_t generic_file_write(struct file *file, const char __user *buf,
1839 size_t count, loff_t *ppos)
1841 struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
1842 ssize_t err;
1843 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1845 down(&inode->i_sem);
1846 err = generic_file_write_nolock(file, &local_iov, 1, ppos);
1847 up(&inode->i_sem);
1849 return err;
1852 ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
1853 unsigned long nr_segs, loff_t *ppos)
1855 struct kiocb kiocb;
1856 ssize_t ret;
1858 init_sync_kiocb(&kiocb, filp);
1859 ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
1860 if (-EIOCBQUEUED == ret)
1861 ret = wait_on_sync_kiocb(&kiocb);
1862 return ret;
1865 ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
1866 unsigned long nr_segs, loff_t * ppos)
1868 struct inode *inode = file->f_dentry->d_inode;
1869 ssize_t ret;
1871 down(&inode->i_sem);
1872 ret = generic_file_write_nolock(file, iov, nr_segs, ppos);
1873 up(&inode->i_sem);
1874 return ret;
1877 ssize_t
1878 generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
1879 loff_t offset, unsigned long nr_segs)
1881 struct file *file = iocb->ki_filp;
1882 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
1883 ssize_t retval;
1885 if (mapping->nrpages) {
1886 retval = filemap_fdatawrite(mapping);
1887 if (retval == 0)
1888 retval = filemap_fdatawait(mapping);
1889 if (retval)
1890 goto out;
1893 retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
1894 if (rw == WRITE && mapping->nrpages)
1895 invalidate_inode_pages2(mapping);
1896 out:
1897 return retval;