ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / mm / fadvise.c
1 /*
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>
18
19 /*
20  * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
21  * deactivate the pages and clear PG_Referenced.
22  */
23 asmlinkage long sys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
24 {
25         struct file *file = fget(fd);
26         struct address_space *mapping;
27         struct backing_dev_info *bdi;
28         loff_t endbyte;
29         pgoff_t start_index;
30         pgoff_t end_index;
31         unsigned long nrpages;
32         int ret = 0;
33
34         if (!file)
35                 return -EBADF;
36
37         mapping = file->f_mapping;
38         if (!mapping || len < 0) {
39                 ret = -EINVAL;
40                 goto out;
41         }
42
43         /* Careful about overflows. Len == 0 means "as much as possible" */
44         endbyte = offset + len;
45         if (!len || endbyte < len)
46                 endbyte = -1;
47
48         bdi = mapping->backing_dev_info;
49
50         switch (advice) {
51         case POSIX_FADV_NORMAL:
52                 file->f_ra.ra_pages = bdi->ra_pages;
53                 break;
54         case POSIX_FADV_RANDOM:
55                 file->f_ra.ra_pages = 0;
56                 break;
57         case POSIX_FADV_SEQUENTIAL:
58                 file->f_ra.ra_pages = bdi->ra_pages * 2;
59                 break;
60         case POSIX_FADV_WILLNEED:
61         case POSIX_FADV_NOREUSE:
62                 if (!mapping->a_ops->readpage) {
63                         ret = -EINVAL;
64                         break;
65                 }
66
67                 /* First and last PARTIAL page! */
68                 start_index = offset >> PAGE_CACHE_SHIFT;
69                 end_index = (endbyte-1) >> PAGE_CACHE_SHIFT;
70
71                 /* Careful about overflow on the "+1" */
72                 nrpages = end_index - start_index + 1;
73                 if (!nrpages)
74                         nrpages = ~0UL;
75                 
76                 ret = force_page_cache_readahead(mapping, file,
77                                 start_index,
78                                 max_sane_readahead(nrpages));
79                 if (ret > 0)
80                         ret = 0;
81                 break;
82         case POSIX_FADV_DONTNEED:
83                 if (!bdi_write_congested(mapping->backing_dev_info))
84                         filemap_flush(mapping);
85
86                 /* First and last FULL page! */
87                 start_index = (offset + (PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
88                 end_index = (endbyte >> PAGE_CACHE_SHIFT);
89
90                 if (end_index > start_index)
91                         invalidate_mapping_pages(mapping, start_index, end_index-1);
92                 break;
93         default:
94                 ret = -EINVAL;
95         }
96 out:
97         fput(file);
98         return ret;
99 }
100
101 asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
102 {
103         return sys_fadvise64_64(fd, offset, len, advice);
104 }
105