blob: 0a03357a1f8ed08277070275b494f3d2a7cc9c88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/fadvise.c
3 *
4 * Copyright (C) 2002, Linus Torvalds
5 *
6 * 11Jan2003 akpm@digeo.com
7 * Initial version.
8 */
9
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>
Andrew Mortonebcf28e2006-03-24 03:18:04 -080018#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
20
21#include <asm/unistd.h>
22
23/*
24 * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
25 * deactivate the pages and clear PG_Referenced.
Andrew Mortonebcf28e2006-03-24 03:18:04 -080026 *
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.
30 *
31 * LINUX_FADV_WRITE_WAIT: wait upon writeout of any dirty pages between file
32 * offsets `offset' and `offset+len'.
33 *
34 * By combining these two operations the application may do several things:
35 *
36 * LINUX_FADV_ASYNC_WRITE: push some or all of the dirty pages at the disk.
37 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39asmlinkage long sys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
40{
41 struct file *file = fget(fd);
42 struct address_space *mapping;
43 struct backing_dev_info *bdi;
Andrew Mortonebcf28e2006-03-24 03:18:04 -080044 loff_t endbyte; /* inclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 pgoff_t start_index;
46 pgoff_t end_index;
47 unsigned long nrpages;
48 int ret = 0;
49
50 if (!file)
51 return -EBADF;
52
Valentine Barshak87ba81d2006-01-08 01:03:44 -080053 if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
54 ret = -ESPIPE;
55 goto out;
56 }
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mapping = file->f_mapping;
59 if (!mapping || len < 0) {
60 ret = -EINVAL;
61 goto out;
62 }
63
Carsten Ottefe77ba62005-06-23 22:05:29 -070064 if (mapping->a_ops->get_xip_page)
65 /* no bad return value, but ignore advice */
66 goto out;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* Careful about overflows. Len == 0 means "as much as possible" */
69 endbyte = offset + len;
70 if (!len || endbyte < len)
71 endbyte = -1;
Andrew Mortonebcf28e2006-03-24 03:18:04 -080072 else
73 endbyte--; /* inclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 bdi = mapping->backing_dev_info;
76
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;
92 }
93
94 /* First and last PARTIAL page! */
95 start_index = offset >> PAGE_CACHE_SHIFT;
Andrew Mortonebcf28e2006-03-24 03:18:04 -080096 end_index = endbyte >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Careful about overflow on the "+1" */
99 nrpages = end_index - start_index + 1;
100 if (!nrpages)
101 nrpages = ~0UL;
102
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);
112
113 /* First and last FULL page! */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800114 start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 end_index = (endbyte >> PAGE_CACHE_SHIFT);
116
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800117 if (end_index >= start_index)
118 invalidate_mapping_pages(mapping, start_index,
119 end_index);
120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 default:
122 ret = -EINVAL;
123 }
124out:
125 fput(file);
126 return ret;
127}
128
129#ifdef __ARCH_WANT_SYS_FADVISE64
130
131asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
132{
133 return sys_fadvise64_64(fd, offset, len, advice);
134}
135
136#endif