Blame view

fs/ramfs/file-nommu.c 6.62 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 *);
f5e54d6e5   Christoph Hellwig   [PATCH] mark addr...
30
  const struct address_space_operations ramfs_aops = {
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
31
  	.readpage		= simple_readpage,
800d15a53   Nick Piggin   implement simple ...
32
33
  	.write_begin		= simple_write_begin,
  	.write_end		= simple_write_end,
466262963   Ken Chen   [PATCH] convert r...
34
  	.set_page_dirty		= __set_page_dirty_no_writeback,
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
35
  };
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
36
  const struct file_operations ramfs_file_operations = {
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
37
38
  	.mmap			= ramfs_nommu_mmap,
  	.get_unmapped_area	= ramfs_nommu_get_unmapped_area,
543ade1fc   Badari Pulavarty   [PATCH] Streamlin...
39
40
41
42
  	.read			= do_sync_read,
  	.aio_read		= generic_file_aio_read,
  	.write			= do_sync_write,
  	.aio_write		= generic_file_aio_write,
1b061d924   Christoph Hellwig   rename the generi...
43
  	.fsync			= noop_fsync,
5ffc4ef45   Jens Axboe   sendfile: remove ...
44
  	.splice_read		= generic_file_splice_read,
8b3d3567f   Octavian Purdila   ramfs: enable spl...
45
  	.splice_write		= generic_file_splice_write,
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
46
47
  	.llseek			= generic_file_llseek,
  };
c5ef1c42c   Arjan van de Ven   [PATCH] mark stru...
48
  const struct inode_operations ramfs_file_inode_operations = {
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
49
50
51
52
53
54
55
56
57
58
  	.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...
59
  int ramfs_nommu_expand_for_mapping(struct inode *inode, size_t newsize)
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
60
  {
0f67b0b03   Mike Frysinger   nommu: ramfs: rem...
61
  	unsigned long npages, xpages, loop;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
62
63
64
65
66
67
68
69
  	struct page *pages;
  	unsigned order;
  	void *data;
  	int ret;
  
  	/* make various checks */
  	order = get_order(newsize);
  	if (unlikely(order >= MAX_ORDER))
c08d3b0e3   npiggin@suse.de   truncate: use new...
70
  		return -EFBIG;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
71

c08d3b0e3   npiggin@suse.de   truncate: use new...
72
73
74
  	ret = inode_newsize_ok(inode, newsize);
  	if (ret)
  		return ret;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
75
76
77
78
79
80
81
82
83
84
85
86
  
  	i_size_write(inode, newsize);
  
  	/* allocate enough contiguous pages to be able to satisfy the
  	 * request */
  	pages = alloc_pages(mapping_gfp_mask(inode->i_mapping), order);
  	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...
87
  	split_page(pages, order);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
88
89
90
91
92
93
94
95
96
97
98
  
  	/* 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...
99
100
  	for (loop = 0; loop < npages; loop++) {
  		struct page *page = pages + loop;
2678958e1   Johannes Weiner   ramfs-nommu: use ...
101
102
  		ret = add_to_page_cache_lru(page, inode->i_mapping, loop,
  					GFP_KERNEL);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
103
104
  		if (ret < 0)
  			goto add_error;
020fe22ff   Enrik Berkhan   nommu: ramfs: pag...
105
106
  		/* prevent the page from being discarded on memory pressure */
  		SetPageDirty(page);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
107
  		unlock_page(page);
b836aec53   Bob Liu   ramfs: fix memlea...
108
  		put_page(page);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
109
  	}
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
110
  	return 0;
c08d3b0e3   npiggin@suse.de   truncate: use new...
111
  add_error:
2678958e1   Johannes Weiner   ramfs-nommu: use ...
112
113
  	while (loop < npages)
  		__free_page(pages + loop++);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
114
115
116
117
118
  	return ret;
  }
  
  /*****************************************************************************/
  /*
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
   *
   */
  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...
136
  		ret = nommu_shrink_inode_mappings(inode, size, newsize);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
137
138
139
  		if (ret < 0)
  			return ret;
  	}
2c27c65ed   Christoph Hellwig   check ATTR_SIZE c...
140
141
  	truncate_setsize(inode, newsize);
  	return 0;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
142
143
144
145
146
147
148
149
150
151
152
153
  }
  
  /*****************************************************************************/
  /*
   * 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)
  {
  	struct inode *inode = dentry->d_inode;
  	unsigned int old_ia_valid = ia->ia_valid;
  	int ret = 0;
85f6038f2   Bryan Wu   RAMFS NOMMU: miss...
154
155
156
157
  	/* POSIX UID/GID verification for setting inode attributes */
  	ret = inode_change_ok(inode, ia);
  	if (ret)
  		return ret;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
158
159
  	/* pick out size-changing events */
  	if (ia->ia_valid & ATTR_SIZE) {
3322e79a3   Nick Piggin   fs: convert simpl...
160
  		loff_t size = inode->i_size;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
161
162
163
164
165
166
167
168
169
170
171
  		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...
172
  	setattr_copy(inode, ia);
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
   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
   */
  unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
  					    unsigned long addr, unsigned long len,
  					    unsigned long pgoff, unsigned long flags)
  {
  	unsigned long maxpages, lpages, nr, loop, ret;
a57c4d65f   Josef Sipek   [PATCH] struct pa...
190
  	struct inode *inode = file->f_path.dentry->d_inode;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  	struct page **pages = NULL, **ptr, *page;
  	loff_t isize;
  
  	if (!(flags & MAP_SHARED))
  		return addr;
  
  	/* the mapping mustn't extend beyond the EOF */
  	lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  	isize = i_size_read(inode);
  
  	ret = -EINVAL;
  	maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  	if (pgoff >= maxpages)
  		goto out;
  
  	if (maxpages - pgoff < lpages)
  		goto out;
  
  	/* gang-find the pages */
  	ret = -ENOMEM;
  	pages = kzalloc(lpages * sizeof(struct page *), GFP_KERNEL);
  	if (!pages)
0e8f989a2   David Howells   NOMMU: Fix cleanu...
213
  		goto out_free;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
214
215
216
  
  	nr = find_get_pages(inode->i_mapping, pgoff, lpages, pages);
  	if (nr != lpages)
0e8f989a2   David Howells   NOMMU: Fix cleanu...
217
  		goto out_free_pages; /* leave if some pages were missing */
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
218
219
220
221
222
223
224
  
  	/* 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...
225
  			goto out_free_pages;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
226
227
228
  
  	/* okay - all conditions fulfilled */
  	ret = (unsigned long) page_address(pages[0]);
0e8f989a2   David Howells   NOMMU: Fix cleanu...
229
230
231
232
233
234
235
  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...
236
237
238
239
240
  	return ret;
  }
  
  /*****************************************************************************/
  /*
21ff82163   David Howells   [PATCH] NOMMU: Fi...
241
   * set up a mapping for shared memory segments
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
242
243
244
   */
  int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
  {
2e92a3bae   David Howells   NOMMU: Fix SYSV I...
245
246
247
248
249
250
  	if (!(vma->vm_flags & VM_SHARED))
  		return -ENOSYS;
  
  	file_accessed(file);
  	vma->vm_ops = &generic_file_vm_ops;
  	return 0;
642fb4d1f   David Howells   [PATCH] NOMMU: Pr...
251
  }