xref: /linux/mm/fadvise.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
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 #include <linux/writeback.h>
19 #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.
26  *
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  *
38  */
39 asmlinkage 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;
44 	loff_t endbyte;			/* inclusive */
45 	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 
53 	if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
54 		ret = -ESPIPE;
55 		goto out;
56 	}
57 
58 	mapping = file->f_mapping;
59 	if (!mapping || len < 0) {
60 		ret = -EINVAL;
61 		goto out;
62 	}
63 
64 	if (mapping->a_ops->get_xip_page)
65 		/* no bad return value, but ignore advice */
66 		goto out;
67 
68 	/* Careful about overflows. Len == 0 means "as much as possible" */
69 	endbyte = offset + len;
70 	if (!len || endbyte < len)
71 		endbyte = -1;
72 	else
73 		endbyte--;		/* inclusive */
74 
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;
96 		end_index = endbyte >> PAGE_CACHE_SHIFT;
97 
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! */
114 		start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
115 		end_index = (endbyte >> PAGE_CACHE_SHIFT);
116 
117 		if (end_index >= start_index)
118 			invalidate_mapping_pages(mapping, start_index,
119 						end_index);
120 		break;
121 	default:
122 		ret = -EINVAL;
123 	}
124 out:
125 	fput(file);
126 	return ret;
127 }
128 
129 #ifdef __ARCH_WANT_SYS_FADVISE64
130 
131 asmlinkage 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
137