Blame view
fs/ramfs/file-nommu.c
6.63 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
642fb4d1f [PATCH] NOMMU: Pr... |
2 3 4 5 |
/* file-nommu.c: no-MMU version of ramfs * * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) |
642fb4d1f [PATCH] NOMMU: Pr... |
6 7 8 9 |
*/ #include <linux/module.h> #include <linux/fs.h> |
131612dfe [PATCH] ramfs bre... |
10 |
#include <linux/mm.h> |
642fb4d1f [PATCH] NOMMU: Pr... |
11 12 13 14 |
#include <linux/pagemap.h> #include <linux/highmem.h> #include <linux/init.h> #include <linux/string.h> |
642fb4d1f [PATCH] NOMMU: Pr... |
15 16 |
#include <linux/backing-dev.h> #include <linux/ramfs.h> |
642fb4d1f [PATCH] NOMMU: Pr... |
17 18 |
#include <linux/pagevec.h> #include <linux/mman.h> |
5c8053652 fs/ramfs/file-nom... |
19 |
#include <linux/sched.h> |
5a0e3ad6a include cleanup: ... |
20 |
#include <linux/slab.h> |
642fb4d1f [PATCH] NOMMU: Pr... |
21 |
|
7c0f6ba68 Replace <asm/uacc... |
22 |
#include <linux/uaccess.h> |
642fb4d1f [PATCH] NOMMU: Pr... |
23 24 25 |
#include "internal.h" static int ramfs_nommu_setattr(struct dentry *, struct iattr *); |
0fa9aa20c fs/ramfs/file-nom... |
26 27 28 29 30 31 |
static unsigned long ramfs_nommu_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma); |
642fb4d1f [PATCH] NOMMU: Pr... |
32 |
|
b4caecd48 fs: introduce f_o... |
33 34 35 36 37 |
static unsigned ramfs_mmap_capabilities(struct file *file) { return NOMMU_MAP_DIRECT | NOMMU_MAP_COPY | NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC; } |
4b6f5d20b [PATCH] Make most... |
38 |
const struct file_operations ramfs_file_operations = { |
b4caecd48 fs: introduce f_o... |
39 |
.mmap_capabilities = ramfs_mmap_capabilities, |
642fb4d1f [PATCH] NOMMU: Pr... |
40 41 |
.mmap = ramfs_nommu_mmap, .get_unmapped_area = ramfs_nommu_get_unmapped_area, |
aad4f8bb4 switch simple gen... |
42 |
.read_iter = generic_file_read_iter, |
8174202b3 write_iter varian... |
43 |
.write_iter = generic_file_write_iter, |
1b061d924 rename the generi... |
44 |
.fsync = noop_fsync, |
5ffc4ef45 sendfile: remove ... |
45 |
.splice_read = generic_file_splice_read, |
8d0207652 ->splice_write() ... |
46 |
.splice_write = iter_file_splice_write, |
642fb4d1f [PATCH] NOMMU: Pr... |
47 48 |
.llseek = generic_file_llseek, }; |
c5ef1c42c [PATCH] mark stru... |
49 |
const struct inode_operations ramfs_file_inode_operations = { |
642fb4d1f [PATCH] NOMMU: Pr... |
50 51 52 53 54 55 56 57 58 59 |
.setattr = ramfs_nommu_setattr, .getattr = simple_getattr, }; /*****************************************************************************/ /* * add a contiguous set of pages into a ramfs inode when it's truncated from * size 0 on the assumption that it's going to be used for an mmap of shared * memory */ |
4b19de6d1 mm: tiny-shmem no... |
60 |
int ramfs_nommu_expand_for_mapping(struct inode *inode, size_t newsize) |
642fb4d1f [PATCH] NOMMU: Pr... |
61 |
{ |
0f67b0b03 nommu: ramfs: rem... |
62 |
unsigned long npages, xpages, loop; |
642fb4d1f [PATCH] NOMMU: Pr... |
63 64 65 66 |
struct page *pages; unsigned order; void *data; int ret; |
063d99b4f mm, fs: obey gfp_... |
67 |
gfp_t gfp = mapping_gfp_mask(inode->i_mapping); |
642fb4d1f [PATCH] NOMMU: Pr... |
68 69 70 71 |
/* make various checks */ order = get_order(newsize); if (unlikely(order >= MAX_ORDER)) |
c08d3b0e3 truncate: use new... |
72 |
return -EFBIG; |
642fb4d1f [PATCH] NOMMU: Pr... |
73 |
|
c08d3b0e3 truncate: use new... |
74 75 76 |
ret = inode_newsize_ok(inode, newsize); if (ret) return ret; |
642fb4d1f [PATCH] NOMMU: Pr... |
77 78 79 80 81 |
i_size_write(inode, newsize); /* allocate enough contiguous pages to be able to satisfy the * request */ |
063d99b4f mm, fs: obey gfp_... |
82 |
pages = alloc_pages(gfp, order); |
642fb4d1f [PATCH] NOMMU: Pr... |
83 84 85 86 87 88 |
if (!pages) return -ENOMEM; /* split the high-order page into an array of single pages */ xpages = 1UL << order; npages = (newsize + PAGE_SIZE - 1) >> PAGE_SHIFT; |
84097518d [PATCH] mm: nommu... |
89 |
split_page(pages, order); |
642fb4d1f [PATCH] NOMMU: Pr... |
90 91 92 93 94 95 96 97 98 99 100 |
/* trim off any pages we don't actually require */ for (loop = npages; loop < xpages; loop++) __free_page(pages + loop); /* clear the memory we allocated */ newsize = PAGE_SIZE * npages; data = page_address(pages); memset(data, 0, newsize); /* attach all the pages to the inode's address space */ |
642fb4d1f [PATCH] NOMMU: Pr... |
101 102 |
for (loop = 0; loop < npages; loop++) { struct page *page = pages + loop; |
2678958e1 ramfs-nommu: use ... |
103 |
ret = add_to_page_cache_lru(page, inode->i_mapping, loop, |
063d99b4f mm, fs: obey gfp_... |
104 |
gfp); |
642fb4d1f [PATCH] NOMMU: Pr... |
105 106 |
if (ret < 0) goto add_error; |
020fe22ff nommu: ramfs: pag... |
107 108 |
/* prevent the page from being discarded on memory pressure */ SetPageDirty(page); |
fea9f718b fs: ramfs: file-n... |
109 |
SetPageUptodate(page); |
020fe22ff nommu: ramfs: pag... |
110 |
|
642fb4d1f [PATCH] NOMMU: Pr... |
111 |
unlock_page(page); |
b836aec53 ramfs: fix memlea... |
112 |
put_page(page); |
642fb4d1f [PATCH] NOMMU: Pr... |
113 |
} |
642fb4d1f [PATCH] NOMMU: Pr... |
114 |
return 0; |
c08d3b0e3 truncate: use new... |
115 |
add_error: |
2678958e1 ramfs-nommu: use ... |
116 117 |
while (loop < npages) __free_page(pages + loop++); |
642fb4d1f [PATCH] NOMMU: Pr... |
118 119 120 121 122 |
return ret; } /*****************************************************************************/ /* |
642fb4d1f [PATCH] NOMMU: Pr... |
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
* */ static int ramfs_nommu_resize(struct inode *inode, loff_t newsize, loff_t size) { int ret; /* assume a truncate from zero size is going to be for the purposes of * shared mmap */ if (size == 0) { if (unlikely(newsize >> 32)) return -EFBIG; return ramfs_nommu_expand_for_mapping(inode, newsize); } /* check that a decrease in size doesn't cut off any shared mappings */ if (newsize < size) { |
7e6608724 nommu: fix shared... |
140 |
ret = nommu_shrink_inode_mappings(inode, size, newsize); |
642fb4d1f [PATCH] NOMMU: Pr... |
141 142 143 |
if (ret < 0) return ret; } |
2c27c65ed check ATTR_SIZE c... |
144 145 |
truncate_setsize(inode, newsize); return 0; |
642fb4d1f [PATCH] NOMMU: Pr... |
146 147 148 149 150 151 152 153 154 |
} /*****************************************************************************/ /* * handle a change of attributes * - we're specifically interested in a change of size */ static int ramfs_nommu_setattr(struct dentry *dentry, struct iattr *ia) { |
2b0143b5c VFS: normal files... |
155 |
struct inode *inode = d_inode(dentry); |
642fb4d1f [PATCH] NOMMU: Pr... |
156 157 |
unsigned int old_ia_valid = ia->ia_valid; int ret = 0; |
85f6038f2 RAMFS NOMMU: miss... |
158 |
/* POSIX UID/GID verification for setting inode attributes */ |
31051c85b fs: Give dentry t... |
159 |
ret = setattr_prepare(dentry, ia); |
85f6038f2 RAMFS NOMMU: miss... |
160 161 |
if (ret) return ret; |
642fb4d1f [PATCH] NOMMU: Pr... |
162 163 |
/* pick out size-changing events */ if (ia->ia_valid & ATTR_SIZE) { |
3322e79a3 fs: convert simpl... |
164 |
loff_t size = inode->i_size; |
642fb4d1f [PATCH] NOMMU: Pr... |
165 166 167 168 169 170 171 172 173 174 175 |
if (ia->ia_size != size) { ret = ramfs_nommu_resize(inode, ia->ia_size, size); if (ret < 0 || ia->ia_valid == ATTR_SIZE) goto out; } else { /* we skipped the truncate but must still update * timestamps */ ia->ia_valid |= ATTR_MTIME|ATTR_CTIME; } } |
6a1a90ad1 rename generic_se... |
176 |
setattr_copy(inode, ia); |
642fb4d1f [PATCH] NOMMU: Pr... |
177 178 179 180 181 182 183 184 185 186 187 188 |
out: ia->ia_valid = old_ia_valid; return ret; } /*****************************************************************************/ /* * try to determine where a shared mapping can be made * - we require that: * - the pages to be mapped must exist * - the pages be physically contiguous in sequence */ |
0fa9aa20c fs/ramfs/file-nom... |
189 |
static unsigned long ramfs_nommu_get_unmapped_area(struct file *file, |
642fb4d1f [PATCH] NOMMU: Pr... |
190 191 192 193 |
unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { unsigned long maxpages, lpages, nr, loop, ret; |
496ad9aa8 new helper: file_... |
194 |
struct inode *inode = file_inode(file); |
642fb4d1f [PATCH] NOMMU: Pr... |
195 196 |
struct page **pages = NULL, **ptr, *page; loff_t isize; |
642fb4d1f [PATCH] NOMMU: Pr... |
197 198 199 |
/* the mapping mustn't extend beyond the EOF */ lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; isize = i_size_read(inode); |
63678c32e tmpfs/ramfs: fix ... |
200 |
ret = -ENOSYS; |
642fb4d1f [PATCH] NOMMU: Pr... |
201 202 203 204 205 206 207 208 |
maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT; if (pgoff >= maxpages) goto out; if (maxpages - pgoff < lpages) goto out; /* gang-find the pages */ |
6f4535ed7 fs/ramfs/file-nom... |
209 |
pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL); |
642fb4d1f [PATCH] NOMMU: Pr... |
210 |
if (!pages) |
0e8f989a2 NOMMU: Fix cleanu... |
211 |
goto out_free; |
642fb4d1f [PATCH] NOMMU: Pr... |
212 |
|
50b7d8568 ramfs: fix nommu ... |
213 |
nr = find_get_pages_contig(inode->i_mapping, pgoff, lpages, pages); |
642fb4d1f [PATCH] NOMMU: Pr... |
214 |
if (nr != lpages) |
0e8f989a2 NOMMU: Fix cleanu... |
215 |
goto out_free_pages; /* leave if some pages were missing */ |
642fb4d1f [PATCH] NOMMU: Pr... |
216 217 218 219 220 221 222 |
/* check the pages for physical adjacency */ ptr = pages; page = *ptr++; page++; for (loop = lpages; loop > 1; loop--) if (*ptr++ != page++) |
0e8f989a2 NOMMU: Fix cleanu... |
223 |
goto out_free_pages; |
642fb4d1f [PATCH] NOMMU: Pr... |
224 225 226 |
/* okay - all conditions fulfilled */ ret = (unsigned long) page_address(pages[0]); |
0e8f989a2 NOMMU: Fix cleanu... |
227 228 229 230 231 232 233 |
out_free_pages: ptr = pages; for (loop = nr; loop > 0; loop--) put_page(*ptr++); out_free: kfree(pages); out: |
642fb4d1f [PATCH] NOMMU: Pr... |
234 235 236 237 238 |
return ret; } /*****************************************************************************/ /* |
21ff82163 [PATCH] NOMMU: Fi... |
239 |
* set up a mapping for shared memory segments |
642fb4d1f [PATCH] NOMMU: Pr... |
240 |
*/ |
0fa9aa20c fs/ramfs/file-nom... |
241 |
static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma) |
642fb4d1f [PATCH] NOMMU: Pr... |
242 |
{ |
63678c32e tmpfs/ramfs: fix ... |
243 |
if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) |
2e92a3bae NOMMU: Fix SYSV I... |
244 245 246 247 248 |
return -ENOSYS; file_accessed(file); vma->vm_ops = &generic_file_vm_ops; return 0; |
642fb4d1f [PATCH] NOMMU: Pr... |
249 |
} |