Blame view

fs/ramfs/file-nommu.c 6.82 KB
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /* file-nommu.c: no-MMU version of ramfs
   *
   * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version
   * 2 of the License, or (at your option) any later version.
   */
  
  #include <linux/module.h>
  #include <linux/fs.h>
131612dfe   Dimitri Gorokhovik   [PATCH] ramfs bre...
14
  #include <linux/mm.h>
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
15
16
17
18
  #include <linux/pagemap.h>
  #include <linux/highmem.h>
  #include <linux/init.h>
  #include <linux/string.h>
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
19
20
  #include <linux/backing-dev.h>
  #include <linux/ramfs.h>
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
21
22
  #include <linux/pagevec.h>
  #include <linux/mman.h>
5c8053652   Catalin Marinas   fs/ramfs/file-nom...
23
  #include <linux/sched.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
24
  #include <linux/slab.h>
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
25
26
27
28
29
  
  #include <asm/uaccess.h>
  #include "internal.h"
  
  static int ramfs_nommu_setattr(struct dentry *, struct iattr *);
0fa9aa20c   Axel Lin   fs/ramfs/file-nom...
30
31
32
33
34
35
  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   David Howells   [PATCH] NOMMU: Pr...
36

b4caecd48   Christoph Hellwig   fs: introduce f_o...
37
38
39
40
41
  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   Arjan van de Ven   [PATCH] Make most...
42
  const struct file_operations ramfs_file_operations = {
b4caecd48   Christoph Hellwig   fs: introduce f_o...
43
  	.mmap_capabilities	= ramfs_mmap_capabilities,
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
44
45
  	.mmap			= ramfs_nommu_mmap,
  	.get_unmapped_area	= ramfs_nommu_get_unmapped_area,
aad4f8bb4   Al Viro   switch simple gen...
46
  	.read_iter		= generic_file_read_iter,
8174202b3   Al Viro   write_iter varian...
47
  	.write_iter		= generic_file_write_iter,
1b061d924   Christoph Hellwig   rename the generi...
48
  	.fsync			= noop_fsync,
5ffc4ef45   Jens Axboe   sendfile: remove ...
49
  	.splice_read		= generic_file_splice_read,
8d0207652   Al Viro   ->splice_write() ...
50
  	.splice_write		= iter_file_splice_write,
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
51
52
  	.llseek			= generic_file_llseek,
  };
c5ef1c42c   Arjan van de Ven   [PATCH] mark stru...
53
  const struct inode_operations ramfs_file_inode_operations = {
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
54
55
56
57
58
59
60
61
62
63
  	.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   Nick Piggin   mm: tiny-shmem no...
64
  int ramfs_nommu_expand_for_mapping(struct inode *inode, size_t newsize)
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
65
  {
0f67b0b03   Mike Frysinger   nommu: ramfs: rem...
66
  	unsigned long npages, xpages, loop;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
67
68
69
70
  	struct page *pages;
  	unsigned order;
  	void *data;
  	int ret;
063d99b4f   Michal Hocko   mm, fs: obey gfp_...
71
  	gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
72
73
74
75
  
  	/* make various checks */
  	order = get_order(newsize);
  	if (unlikely(order >= MAX_ORDER))
c08d3b0e3   npiggin@suse.de   truncate: use new...
76
  		return -EFBIG;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
77

c08d3b0e3   npiggin@suse.de   truncate: use new...
78
79
80
  	ret = inode_newsize_ok(inode, newsize);
  	if (ret)
  		return ret;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
81
82
83
84
85
  
  	i_size_write(inode, newsize);
  
  	/* allocate enough contiguous pages to be able to satisfy the
  	 * request */
063d99b4f   Michal Hocko   mm, fs: obey gfp_...
86
  	pages = alloc_pages(gfp, order);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
87
88
89
90
91
92
  	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   Nick Piggin   [PATCH] mm: nommu...
93
  	split_page(pages, order);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
94
95
96
97
98
99
100
101
102
103
104
  
  	/* 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   David Howells   [PATCH] NOMMU: Pr...
105
106
  	for (loop = 0; loop < npages; loop++) {
  		struct page *page = pages + loop;
2678958e1   Johannes Weiner   ramfs-nommu: use ...
107
  		ret = add_to_page_cache_lru(page, inode->i_mapping, loop,
063d99b4f   Michal Hocko   mm, fs: obey gfp_...
108
  					gfp);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
109
110
  		if (ret < 0)
  			goto add_error;
020fe22ff   Enrik Berkhan   nommu: ramfs: pag...
111
112
  		/* prevent the page from being discarded on memory pressure */
  		SetPageDirty(page);
fea9f718b   Bob Liu   fs: ramfs: file-n...
113
  		SetPageUptodate(page);
020fe22ff   Enrik Berkhan   nommu: ramfs: pag...
114

642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
115
  		unlock_page(page);
b836aec53   Bob Liu   ramfs: fix memlea...
116
  		put_page(page);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
117
  	}
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
118
  	return 0;
c08d3b0e3   npiggin@suse.de   truncate: use new...
119
  add_error:
2678958e1   Johannes Weiner   ramfs-nommu: use ...
120
121
  	while (loop < npages)
  		__free_page(pages + loop++);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
122
123
124
125
126
  	return ret;
  }
  
  /*****************************************************************************/
  /*
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
   *
   */
  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   David Howells   nommu: fix shared...
144
  		ret = nommu_shrink_inode_mappings(inode, size, newsize);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
145
146
147
  		if (ret < 0)
  			return ret;
  	}
2c27c65ed   Christoph Hellwig   check ATTR_SIZE c...
148
149
  	truncate_setsize(inode, newsize);
  	return 0;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
150
151
152
153
154
155
156
157
158
  }
  
  /*****************************************************************************/
  /*
   * 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   David Howells   VFS: normal files...
159
  	struct inode *inode = d_inode(dentry);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
160
161
  	unsigned int old_ia_valid = ia->ia_valid;
  	int ret = 0;
85f6038f2   Bryan Wu   RAMFS NOMMU: miss...
162
  	/* POSIX UID/GID verification for setting inode attributes */
31051c85b   Jan Kara   fs: Give dentry t...
163
  	ret = setattr_prepare(dentry, ia);
85f6038f2   Bryan Wu   RAMFS NOMMU: miss...
164
165
  	if (ret)
  		return ret;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
166
167
  	/* pick out size-changing events */
  	if (ia->ia_valid & ATTR_SIZE) {
3322e79a3   Nick Piggin   fs: convert simpl...
168
  		loff_t size = inode->i_size;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
169
170
171
172
173
174
175
176
177
178
179
  		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   Christoph Hellwig   rename generic_se...
180
  	setattr_copy(inode, ia);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
181
182
183
184
185
186
187
188
189
190
191
192
   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   Axel Lin   fs/ramfs/file-nom...
193
  static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
194
195
196
197
  					    unsigned long addr, unsigned long len,
  					    unsigned long pgoff, unsigned long flags)
  {
  	unsigned long maxpages, lpages, nr, loop, ret;
496ad9aa8   Al Viro   new helper: file_...
198
  	struct inode *inode = file_inode(file);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
199
200
  	struct page **pages = NULL, **ptr, *page;
  	loff_t isize;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
201
202
203
  	/* the mapping mustn't extend beyond the EOF */
  	lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  	isize = i_size_read(inode);
63678c32e   Rich Felker   tmpfs/ramfs: fix ...
204
  	ret = -ENOSYS;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
205
206
207
208
209
210
211
212
  	maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  	if (pgoff >= maxpages)
  		goto out;
  
  	if (maxpages - pgoff < lpages)
  		goto out;
  
  	/* gang-find the pages */
6f4535ed7   Fabian Frederick   fs/ramfs/file-nom...
213
  	pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
214
  	if (!pages)
0e8f989a2   David Howells   NOMMU: Fix cleanu...
215
  		goto out_free;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
216
217
218
  
  	nr = find_get_pages(inode->i_mapping, pgoff, lpages, pages);
  	if (nr != lpages)
0e8f989a2   David Howells   NOMMU: Fix cleanu...
219
  		goto out_free_pages; /* leave if some pages were missing */
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
220
221
222
223
224
225
226
  
  	/* check the pages for physical adjacency */
  	ptr = pages;
  	page = *ptr++;
  	page++;
  	for (loop = lpages; loop > 1; loop--)
  		if (*ptr++ != page++)
0e8f989a2   David Howells   NOMMU: Fix cleanu...
227
  			goto out_free_pages;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
228
229
230
  
  	/* okay - all conditions fulfilled */
  	ret = (unsigned long) page_address(pages[0]);
0e8f989a2   David Howells   NOMMU: Fix cleanu...
231
232
233
234
235
236
237
  out_free_pages:
  	ptr = pages;
  	for (loop = nr; loop > 0; loop--)
  		put_page(*ptr++);
  out_free:
  	kfree(pages);
  out:
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
238
239
240
241
242
  	return ret;
  }
  
  /*****************************************************************************/
  /*
21ff82163   David Howells   [PATCH] NOMMU: Fi...
243
   * set up a mapping for shared memory segments
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
244
   */
0fa9aa20c   Axel Lin   fs/ramfs/file-nom...
245
  static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
246
  {
63678c32e   Rich Felker   tmpfs/ramfs: fix ...
247
  	if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
2e92a3bae   David Howells   NOMMU: Fix SYSV I...
248
249
250
251
252
  		return -ENOSYS;
  
  	file_accessed(file);
  	vma->vm_ops = &generic_file_vm_ops;
  	return 0;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
253
  }