Blame view
mm/fadvise.c
5.35 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 |
/* * mm/fadvise.c * * Copyright (C) 2002, Linus Torvalds * |
e1f8e8744 Remove Andrew Mor... |
7 |
* 11Jan2003 Andrew Morton |
1da177e4c Linux-2.6.12-rc2 |
8 9 10 11 12 13 14 15 16 17 18 |
* Initial version. */ #include <linux/kernel.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/pagemap.h> #include <linux/backing-dev.h> #include <linux/pagevec.h> #include <linux/fadvise.h> |
ebcf28e1c [PATCH] fadvise()... |
19 |
#include <linux/writeback.h> |
1da177e4c Linux-2.6.12-rc2 |
20 |
#include <linux/syscalls.h> |
67d46b296 mm/fadvise.c: dra... |
21 |
#include <linux/swap.h> |
1da177e4c Linux-2.6.12-rc2 |
22 23 24 25 26 27 28 |
#include <asm/unistd.h> /* * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could * deactivate the pages and clear PG_Referenced. */ |
9d5b7c956 mm: add ksys_fadv... |
29 |
|
45cd0faae vfs: add the fadv... |
30 31 |
static int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice) |
1da177e4c Linux-2.6.12-rc2 |
32 |
{ |
e748dcd09 vfs: remove get_x... |
33 |
struct inode *inode; |
1da177e4c Linux-2.6.12-rc2 |
34 35 |
struct address_space *mapping; struct backing_dev_info *bdi; |
ebcf28e1c [PATCH] fadvise()... |
36 |
loff_t endbyte; /* inclusive */ |
1da177e4c Linux-2.6.12-rc2 |
37 38 39 |
pgoff_t start_index; pgoff_t end_index; unsigned long nrpages; |
1da177e4c Linux-2.6.12-rc2 |
40 |
|
45cd0faae vfs: add the fadv... |
41 42 43 |
inode = file_inode(file); if (S_ISFIFO(inode->i_mode)) return -ESPIPE; |
1da177e4c Linux-2.6.12-rc2 |
44 |
|
45cd0faae vfs: add the fadv... |
45 46 47 |
mapping = file->f_mapping; if (!mapping || len < 0) return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
48 |
|
3a77d2148 mm: fadvise: avoi... |
49 50 51 |
bdi = inode_to_bdi(mapping->host); if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) { |
b5beb1caf check ADVICE of f... |
52 53 54 55 56 57 58 59 60 61 |
switch (advice) { case POSIX_FADV_NORMAL: case POSIX_FADV_RANDOM: case POSIX_FADV_SEQUENTIAL: case POSIX_FADV_WILLNEED: case POSIX_FADV_NOREUSE: case POSIX_FADV_DONTNEED: /* no bad return value, but ignore advice */ break; default: |
45cd0faae vfs: add the fadv... |
62 |
return -EINVAL; |
b5beb1caf check ADVICE of f... |
63 |
} |
45cd0faae vfs: add the fadv... |
64 |
return 0; |
b5beb1caf check ADVICE of f... |
65 |
} |
fe77ba6f4 [PATCH] xip: madv... |
66 |
|
a718e28f5 mm/fadvise.c: fix... |
67 68 69 70 71 72 |
/* * Careful about overflows. Len == 0 means "as much as possible". Use * unsigned math because signed overflows are undefined and UBSan * complains. */ endbyte = (u64)offset + (u64)len; |
1da177e4c Linux-2.6.12-rc2 |
73 74 |
if (!len || endbyte < len) endbyte = -1; |
ebcf28e1c [PATCH] fadvise()... |
75 76 |
else endbyte--; /* inclusive */ |
1da177e4c Linux-2.6.12-rc2 |
77 |
|
1da177e4c Linux-2.6.12-rc2 |
78 79 |
switch (advice) { case POSIX_FADV_NORMAL: |
45cd0faae vfs: add the fadv... |
80 81 82 83 |
file->f_ra.ra_pages = bdi->ra_pages; spin_lock(&file->f_lock); file->f_mode &= ~FMODE_RANDOM; spin_unlock(&file->f_lock); |
1da177e4c Linux-2.6.12-rc2 |
84 85 |
break; case POSIX_FADV_RANDOM: |
45cd0faae vfs: add the fadv... |
86 87 88 |
spin_lock(&file->f_lock); file->f_mode |= FMODE_RANDOM; spin_unlock(&file->f_lock); |
1da177e4c Linux-2.6.12-rc2 |
89 90 |
break; case POSIX_FADV_SEQUENTIAL: |
45cd0faae vfs: add the fadv... |
91 92 93 94 |
file->f_ra.ra_pages = bdi->ra_pages * 2; spin_lock(&file->f_lock); file->f_mode &= ~FMODE_RANDOM; spin_unlock(&file->f_lock); |
1da177e4c Linux-2.6.12-rc2 |
95 96 |
break; case POSIX_FADV_WILLNEED: |
1da177e4c Linux-2.6.12-rc2 |
97 |
/* First and last PARTIAL page! */ |
09cbfeaf1 mm, fs: get rid o... |
98 99 |
start_index = offset >> PAGE_SHIFT; end_index = endbyte >> PAGE_SHIFT; |
1da177e4c Linux-2.6.12-rc2 |
100 101 102 103 104 |
/* Careful about overflow on the "+1" */ nrpages = end_index - start_index + 1; if (!nrpages) nrpages = ~0UL; |
3d3727cdb mm, fadvise: don'... |
105 106 107 108 109 |
/* * Ignore return value because fadvise() shall return * success even if filesystem can't retrieve a hint, */ |
45cd0faae vfs: add the fadv... |
110 |
force_page_cache_readahead(mapping, file, start_index, nrpages); |
1da177e4c Linux-2.6.12-rc2 |
111 |
break; |
60c371bc7 [PATCH] fadvise()... |
112 113 |
case POSIX_FADV_NOREUSE: break; |
1da177e4c Linux-2.6.12-rc2 |
114 |
case POSIX_FADV_DONTNEED: |
703c27088 writeback: implem... |
115 |
if (!inode_write_congested(mapping->host)) |
ad8a1b558 fadvise: only ini... |
116 117 |
__filemap_fdatawrite_range(mapping, offset, endbyte, WB_SYNC_NONE); |
1da177e4c Linux-2.6.12-rc2 |
118 |
|
441c228f8 mm: fadvise: docu... |
119 120 121 122 123 |
/* * First and last FULL page! Partial pages are deliberately * preserved on the expectation that it is better to preserve * needed memory than to discard unneeded memory. */ |
09cbfeaf1 mm, fs: get rid o... |
124 125 |
start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT; end_index = (endbyte >> PAGE_SHIFT); |
a7ab400d6 mm/fadvise: disca... |
126 127 128 129 130 131 132 133 134 |
/* * The page at end_index will be inclusively discarded according * by invalidate_mapping_pages(), so subtracting 1 from * end_index means we will skip the last page. But if endbyte * is page aligned or is at the end of file, we should not skip * that page - discarding the last page is safe enough. */ if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK && endbyte != inode->i_size - 1) { |
18aba41cb mm/fadvise.c: do ... |
135 136 137 138 139 140 141 142 143 144 |
/* First page is tricky as 0 - 1 = -1, but pgoff_t * is unsigned, so the end_index >= start_index * check below would be true and we'll discard the whole * file cache which is not what was asked. */ if (end_index == 0) break; end_index--; } |
1da177e4c Linux-2.6.12-rc2 |
145 |
|
67d46b296 mm/fadvise.c: dra... |
146 |
if (end_index >= start_index) { |
4dd72b4a4 mm: fadvise: avoi... |
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
unsigned long count; /* * It's common to FADV_DONTNEED right after * the read or write that instantiates the * pages, in which case there will be some * sitting on the local LRU cache. Try to * avoid the expensive remote drain and the * second cache tree walk below by flushing * them out right away. */ lru_add_drain(); count = invalidate_mapping_pages(mapping, |
67d46b296 mm/fadvise.c: dra... |
161 162 163 164 165 166 167 168 169 170 171 |
start_index, end_index); /* * If fewer pages were invalidated than expected then * it is possible that some of the pages were on * a per-cpu pagevec for a remote CPU. Drain all * pagevecs and try again. */ if (count < (end_index - start_index + 1)) { lru_add_drain_all(); invalidate_mapping_pages(mapping, start_index, |
ebcf28e1c [PATCH] fadvise()... |
172 |
end_index); |
67d46b296 mm/fadvise.c: dra... |
173 174 |
} } |
ebcf28e1c [PATCH] fadvise()... |
175 |
break; |
1da177e4c Linux-2.6.12-rc2 |
176 |
default: |
45cd0faae vfs: add the fadv... |
177 |
return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
178 |
} |
45cd0faae vfs: add the fadv... |
179 180 181 182 183 184 185 186 187 188 189 |
return 0; } int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice) { if (file->f_op->fadvise) return file->f_op->fadvise(file, offset, len, advice); return generic_fadvise(file, offset, len, advice); } EXPORT_SYMBOL(vfs_fadvise); |
3d8f76153 vfs: implement re... |
190 |
#ifdef CONFIG_ADVISE_SYSCALLS |
45cd0faae vfs: add the fadv... |
191 192 193 194 195 196 197 198 199 |
int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice) { struct fd f = fdget(fd); int ret; if (!f.file) return -EBADF; ret = vfs_fadvise(f.file, offset, len, advice); |
2903ff019 switch simple cas... |
200 |
fdput(f); |
1da177e4c Linux-2.6.12-rc2 |
201 202 |
return ret; } |
9d5b7c956 mm: add ksys_fadv... |
203 204 205 206 |
SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice) { return ksys_fadvise64_64(fd, offset, len, advice); } |
1da177e4c Linux-2.6.12-rc2 |
207 |
#ifdef __ARCH_WANT_SYS_FADVISE64 |
4a0fd5bf0 teach SYSCALL_DEF... |
208 |
SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice) |
1da177e4c Linux-2.6.12-rc2 |
209 |
{ |
9d5b7c956 mm: add ksys_fadv... |
210 |
return ksys_fadvise64_64(fd, offset, len, advice); |
1da177e4c Linux-2.6.12-rc2 |
211 212 213 |
} #endif |
3d8f76153 vfs: implement re... |
214 |
#endif |