[PATCH] bcm43xx: don't set the channel on a device, which is down.
[linux-2.6/sactl.git] / mm / fadvise.c
blob907c39257ca06aea630915c9a46103518a4d631a
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.
38 * LINUX_FADV_WRITE_WAIT, LINUX_FADV_ASYNC_WRITE: push all of the currently
39 * dirty pages at the disk.
41 * LINUX_FADV_WRITE_WAIT, LINUX_FADV_ASYNC_WRITE, LINUX_FADV_WRITE_WAIT: push
42 * all of the currently dirty pages at the disk, wait until they have been
43 * written.
45 * It should be noted that none of these operations write out the file's
46 * metadata. So unless the application is strictly performing overwrites of
47 * already-instantiated disk blocks, there are no guarantees here that the data
48 * will be available after a crash.
50 asmlinkage long sys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
52 struct file *file = fget(fd);
53 struct address_space *mapping;
54 struct backing_dev_info *bdi;
55 loff_t endbyte; /* inclusive */
56 pgoff_t start_index;
57 pgoff_t end_index;
58 unsigned long nrpages;
59 int ret = 0;
61 if (!file)
62 return -EBADF;
64 if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
65 ret = -ESPIPE;
66 goto out;
69 mapping = file->f_mapping;
70 if (!mapping || len < 0) {
71 ret = -EINVAL;
72 goto out;
75 if (mapping->a_ops->get_xip_page)
76 /* no bad return value, but ignore advice */
77 goto out;
79 /* Careful about overflows. Len == 0 means "as much as possible" */
80 endbyte = offset + len;
81 if (!len || endbyte < len)
82 endbyte = -1;
83 else
84 endbyte--; /* inclusive */
86 bdi = mapping->backing_dev_info;
88 switch (advice) {
89 case POSIX_FADV_NORMAL:
90 file->f_ra.ra_pages = bdi->ra_pages;
91 break;
92 case POSIX_FADV_RANDOM:
93 file->f_ra.ra_pages = 0;
94 break;
95 case POSIX_FADV_SEQUENTIAL:
96 file->f_ra.ra_pages = bdi->ra_pages * 2;
97 break;
98 case POSIX_FADV_WILLNEED:
99 case POSIX_FADV_NOREUSE:
100 if (!mapping->a_ops->readpage) {
101 ret = -EINVAL;
102 break;
105 /* First and last PARTIAL page! */
106 start_index = offset >> PAGE_CACHE_SHIFT;
107 end_index = endbyte >> PAGE_CACHE_SHIFT;
109 /* Careful about overflow on the "+1" */
110 nrpages = end_index - start_index + 1;
111 if (!nrpages)
112 nrpages = ~0UL;
114 ret = force_page_cache_readahead(mapping, file,
115 start_index,
116 max_sane_readahead(nrpages));
117 if (ret > 0)
118 ret = 0;
119 break;
120 case POSIX_FADV_DONTNEED:
121 if (!bdi_write_congested(mapping->backing_dev_info))
122 filemap_flush(mapping);
124 /* First and last FULL page! */
125 start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
126 end_index = (endbyte >> PAGE_CACHE_SHIFT);
128 if (end_index >= start_index)
129 invalidate_mapping_pages(mapping, start_index,
130 end_index);
131 break;
132 case LINUX_FADV_ASYNC_WRITE:
133 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
134 WB_SYNC_NONE);
135 break;
136 case LINUX_FADV_WRITE_WAIT:
137 ret = wait_on_page_writeback_range(mapping,
138 offset >> PAGE_CACHE_SHIFT,
139 endbyte >> PAGE_CACHE_SHIFT);
140 break;
141 default:
142 ret = -EINVAL;
144 out:
145 fput(file);
146 return ret;
149 #ifdef __ARCH_WANT_SYS_FADVISE64
151 asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
153 return sys_fadvise64_64(fd, offset, len, advice);
156 #endif