4 * Copyright (C) 2002, Linus Torvalds
6 * 11Jan2003 akpm@digeo.com
10 #include <linux/kernel.h>
11 #include <linux/file.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
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 */
58 unsigned long nrpages;
64 if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
69 mapping = file->f_mapping;
70 if (!mapping || len < 0) {
75 if (mapping->a_ops->get_xip_page)
76 /* no bad return value, but ignore advice */
79 /* Careful about overflows. Len == 0 means "as much as possible" */
80 endbyte = offset + len;
81 if (!len || endbyte < len)
84 endbyte--; /* inclusive */
86 bdi = mapping->backing_dev_info;
89 case POSIX_FADV_NORMAL:
90 file->f_ra.ra_pages = bdi->ra_pages;
92 case POSIX_FADV_RANDOM:
93 file->f_ra.ra_pages = 0;
95 case POSIX_FADV_SEQUENTIAL:
96 file->f_ra.ra_pages = bdi->ra_pages * 2;
98 case POSIX_FADV_WILLNEED:
99 case POSIX_FADV_NOREUSE:
100 if (!mapping->a_ops->readpage) {
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;
114 ret = force_page_cache_readahead(mapping, file,
116 max_sane_readahead(nrpages));
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,
132 case LINUX_FADV_ASYNC_WRITE:
133 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
136 case LINUX_FADV_WRITE_WAIT:
137 ret = wait_on_page_writeback_range(mapping,
138 offset >> PAGE_CACHE_SHIFT,
139 endbyte >> PAGE_CACHE_SHIFT);
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);