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.
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 */
47 unsigned long nrpages;
53 if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
58 mapping = file->f_mapping;
59 if (!mapping || len < 0) {
64 if (mapping->a_ops->get_xip_page)
65 /* no bad return value, but ignore advice */
68 /* Careful about overflows. Len == 0 means "as much as possible" */
69 endbyte = offset + len;
70 if (!len || endbyte < len)
73 endbyte--; /* inclusive */
75 bdi = mapping->backing_dev_info;
78 case POSIX_FADV_NORMAL:
79 file->f_ra.ra_pages = bdi->ra_pages;
81 case POSIX_FADV_RANDOM:
82 file->f_ra.ra_pages = 0;
84 case POSIX_FADV_SEQUENTIAL:
85 file->f_ra.ra_pages = bdi->ra_pages * 2;
87 case POSIX_FADV_WILLNEED:
88 case POSIX_FADV_NOREUSE:
89 if (!mapping->a_ops->readpage) {
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;
103 ret = force_page_cache_readahead(mapping, file,
105 max_sane_readahead(nrpages));
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,
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);