[PATCH] add page_mkwrite() vm_operations method
[wrt350n-kernel.git] / mm / fadvise.c
blob0a03357a1f8ed08277070275b494f3d2a7cc9c88
1 /*
2 * mm/fadvise.c
4 * Copyright (C) 2002, Linus Torvalds
6 * 11Jan2003 akpm@digeo.com
7 * Initial version.
8 */
10 #include <linux/kernel.h>
11 #include <linux/file.h>
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/pagemap.h>
15 #include <linux/backing-dev.h>
16 #include <linux/pagevec.h>
17 #include <linux/fadvise.h>
18 #include <linux/writeback.h>
19 #include <linux/syscalls.h>
21 #include <asm/unistd.h>
24 * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
25 * deactivate the pages and clear PG_Referenced.
27 * LINUX_FADV_ASYNC_WRITE: start async writeout of any dirty pages between file
28 * offsets `offset' and `offset+len' inclusive. Any pages which are currently
29 * under writeout are skipped, whether or not they are dirty.
31 * LINUX_FADV_WRITE_WAIT: wait upon writeout of any dirty pages between file
32 * offsets `offset' and `offset+len'.
34 * By combining these two operations the application may do several things:
36 * LINUX_FADV_ASYNC_WRITE: push some or all of the dirty pages at the disk.
39 asmlinkage long sys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
41 struct file *file = fget(fd);
42 struct address_space *mapping;
43 struct backing_dev_info *bdi;
44 loff_t endbyte; /* inclusive */
45 pgoff_t start_index;
46 pgoff_t end_index;
47 unsigned long nrpages;
48 int ret = 0;
50 if (!file)
51 return -EBADF;
53 if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
54 ret = -ESPIPE;
55 goto out;
58 mapping = file->f_mapping;
59 if (!mapping || len < 0) {
60 ret = -EINVAL;
61 goto out;
64 if (mapping->a_ops->get_xip_page)
65 /* no bad return value, but ignore advice */
66 goto out;
68 /* Careful about overflows. Len == 0 means "as much as possible" */
69 endbyte = offset + len;
70 if (!len || endbyte < len)
71 endbyte = -1;
72 else
73 endbyte--; /* inclusive */
75 bdi = mapping->backing_dev_info;
77 switch (advice) {
78 case POSIX_FADV_NORMAL:
79 file->f_ra.ra_pages = bdi->ra_pages;
80 break;
81 case POSIX_FADV_RANDOM:
82 file->f_ra.ra_pages = 0;
83 break;
84 case POSIX_FADV_SEQUENTIAL:
85 file->f_ra.ra_pages = bdi->ra_pages * 2;
86 break;
87 case POSIX_FADV_WILLNEED:
88 case POSIX_FADV_NOREUSE:
89 if (!mapping->a_ops->readpage) {
90 ret = -EINVAL;
91 break;
94 /* First and last PARTIAL page! */
95 start_index = offset >> PAGE_CACHE_SHIFT;
96 end_index = endbyte >> PAGE_CACHE_SHIFT;
98 /* Careful about overflow on the "+1" */
99 nrpages = end_index - start_index + 1;
100 if (!nrpages)
101 nrpages = ~0UL;
103 ret = force_page_cache_readahead(mapping, file,
104 start_index,
105 max_sane_readahead(nrpages));
106 if (ret > 0)
107 ret = 0;
108 break;
109 case POSIX_FADV_DONTNEED:
110 if (!bdi_write_congested(mapping->backing_dev_info))
111 filemap_flush(mapping);
113 /* First and last FULL page! */
114 start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
115 end_index = (endbyte >> PAGE_CACHE_SHIFT);
117 if (end_index >= start_index)
118 invalidate_mapping_pages(mapping, start_index,
119 end_index);
120 break;
121 default:
122 ret = -EINVAL;
124 out:
125 fput(file);
126 return ret;
129 #ifdef __ARCH_WANT_SYS_FADVISE64
131 asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
133 return sys_fadvise64_64(fd, offset, len, advice);
136 #endif