]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/filemap_xip.c
[PATCH] core remove PageReserved
[net-next-2.6.git] / mm / filemap_xip.c
CommitLineData
ceffc078
CO
1/*
2 * linux/mm/filemap_xip.c
3 *
4 * Copyright (C) 2005 IBM Corporation
5 * Author: Carsten Otte <cotte@de.ibm.com>
6 *
7 * derived from linux/mm/filemap.c - Copyright (C) Linus Torvalds
8 *
9 */
10
11#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/module.h>
14#include <linux/uio.h>
15#include <linux/rmap.h>
16#include <asm/tlbflush.h>
17#include "filemap.h"
18
19/*
20 * This is a file read routine for execute in place files, and uses
21 * the mapping->a_ops->get_xip_page() function for the actual low-level
22 * stuff.
23 *
24 * Note the struct file* is not used at all. It may be NULL.
25 */
26static void
27do_xip_mapping_read(struct address_space *mapping,
28 struct file_ra_state *_ra,
29 struct file *filp,
30 loff_t *ppos,
31 read_descriptor_t *desc,
32 read_actor_t actor)
33{
34 struct inode *inode = mapping->host;
35 unsigned long index, end_index, offset;
36 loff_t isize;
37
38 BUG_ON(!mapping->a_ops->get_xip_page);
39
40 index = *ppos >> PAGE_CACHE_SHIFT;
41 offset = *ppos & ~PAGE_CACHE_MASK;
42
43 isize = i_size_read(inode);
44 if (!isize)
45 goto out;
46
47 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
48 for (;;) {
49 struct page *page;
50 unsigned long nr, ret;
51
52 /* nr is the maximum number of bytes to copy from this page */
53 nr = PAGE_CACHE_SIZE;
54 if (index >= end_index) {
55 if (index > end_index)
56 goto out;
57 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
58 if (nr <= offset) {
59 goto out;
60 }
61 }
62 nr = nr - offset;
63
64 page = mapping->a_ops->get_xip_page(mapping,
65 index*(PAGE_SIZE/512), 0);
66 if (!page)
67 goto no_xip_page;
68 if (unlikely(IS_ERR(page))) {
69 if (PTR_ERR(page) == -ENODATA) {
70 /* sparse */
afa597ba 71 page = ZERO_PAGE(0);
ceffc078
CO
72 } else {
73 desc->error = PTR_ERR(page);
74 goto out;
75 }
afa597ba 76 }
ceffc078
CO
77
78 /* If users can be writing to this page using arbitrary
79 * virtual addresses, take care about potential aliasing
80 * before reading the page on the kernel side.
81 */
82 if (mapping_writably_mapped(mapping))
83 flush_dcache_page(page);
84
85 /*
afa597ba 86 * Ok, we have the page, so now we can copy it to user space...
ceffc078
CO
87 *
88 * The actor routine returns how many bytes were actually used..
89 * NOTE! This may not be the same as how much of a user buffer
90 * we filled up (we may be padding etc), so we can only update
91 * "pos" here (the actor routine has to update the user buffer
92 * pointers and the remaining count).
93 */
94 ret = actor(desc, page, offset, nr);
95 offset += ret;
96 index += offset >> PAGE_CACHE_SHIFT;
97 offset &= ~PAGE_CACHE_MASK;
98
99 if (ret == nr && desc->count)
100 continue;
101 goto out;
102
103no_xip_page:
104 /* Did not get the page. Report it */
105 desc->error = -EIO;
106 goto out;
107 }
108
109out:
110 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
111 if (filp)
112 file_accessed(filp);
113}
114
ceffc078 115ssize_t
eb6fe0c3 116xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
ceffc078 117{
eb6fe0c3 118 read_descriptor_t desc;
ceffc078 119
eb6fe0c3
CO
120 if (!access_ok(VERIFY_WRITE, buf, len))
121 return -EFAULT;
ceffc078 122
eb6fe0c3
CO
123 desc.written = 0;
124 desc.arg.buf = buf;
125 desc.count = len;
126 desc.error = 0;
ceffc078 127
eb6fe0c3
CO
128 do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
129 ppos, &desc, file_read_actor);
130
131 if (desc.written)
132 return desc.written;
133 else
134 return desc.error;
ceffc078 135}
eb6fe0c3 136EXPORT_SYMBOL_GPL(xip_file_read);
ceffc078
CO
137
138ssize_t
139xip_file_sendfile(struct file *in_file, loff_t *ppos,
140 size_t count, read_actor_t actor, void *target)
141{
142 read_descriptor_t desc;
143
144 if (!count)
145 return 0;
146
147 desc.written = 0;
148 desc.count = count;
149 desc.arg.data = target;
150 desc.error = 0;
151
152 do_xip_mapping_read(in_file->f_mapping, &in_file->f_ra, in_file,
153 ppos, &desc, actor);
154 if (desc.written)
155 return desc.written;
156 return desc.error;
157}
158EXPORT_SYMBOL_GPL(xip_file_sendfile);
159
160/*
161 * __xip_unmap is invoked from xip_unmap and
162 * xip_write
163 *
164 * This function walks all vmas of the address_space and unmaps the
afa597ba 165 * ZERO_PAGE when found at pgoff. Should it go in rmap.c?
ceffc078
CO
166 */
167static void
168__xip_unmap (struct address_space * mapping,
169 unsigned long pgoff)
170{
171 struct vm_area_struct *vma;
172 struct mm_struct *mm;
173 struct prio_tree_iter iter;
174 unsigned long address;
175 pte_t *pte;
176 pte_t pteval;
b5810039 177 struct page *page = ZERO_PAGE(address);
ceffc078
CO
178
179 spin_lock(&mapping->i_mmap_lock);
180 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
181 mm = vma->vm_mm;
182 address = vma->vm_start +
183 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
184 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
185 /*
186 * We need the page_table_lock to protect us from page faults,
187 * munmap, fork, etc...
188 */
b5810039 189 pte = page_check_address(page, mm, address);
ceffc078
CO
190 if (!IS_ERR(pte)) {
191 /* Nuke the page table entry. */
082ff0a9 192 flush_cache_page(vma, address, pte_pfn(*pte));
ceffc078 193 pteval = ptep_clear_flush(vma, address, pte);
b5810039
NP
194 page_remove_rmap(page);
195 dec_mm_counter(mm, file_rss);
ceffc078
CO
196 BUG_ON(pte_dirty(pteval));
197 pte_unmap(pte);
198 spin_unlock(&mm->page_table_lock);
b5810039 199 page_cache_release(page);
ceffc078
CO
200 }
201 }
202 spin_unlock(&mapping->i_mmap_lock);
203}
204
205/*
206 * xip_nopage() is invoked via the vma operations vector for a
207 * mapped memory region to read in file data during a page fault.
208 *
209 * This function is derived from filemap_nopage, but used for execute in place
210 */
211static struct page *
212xip_file_nopage(struct vm_area_struct * area,
213 unsigned long address,
214 int *type)
215{
216 struct file *file = area->vm_file;
217 struct address_space *mapping = file->f_mapping;
218 struct inode *inode = mapping->host;
219 struct page *page;
220 unsigned long size, pgoff, endoff;
221
222 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
223 + area->vm_pgoff;
224 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
225 + area->vm_pgoff;
226
227 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
228 if (pgoff >= size) {
229 return NULL;
230 }
231
232 page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
233 if (!IS_ERR(page)) {
b5810039 234 goto out;
ceffc078
CO
235 }
236 if (PTR_ERR(page) != -ENODATA)
237 return NULL;
238
239 /* sparse block */
240 if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
241 (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
242 (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
243 /* maybe shared writable, allocate new block */
244 page = mapping->a_ops->get_xip_page (mapping,
245 pgoff*(PAGE_SIZE/512), 1);
246 if (IS_ERR(page))
247 return NULL;
ceffc078
CO
248 /* unmap page at pgoff from all other vmas */
249 __xip_unmap(mapping, pgoff);
250 } else {
afa597ba
CO
251 /* not shared and writable, use ZERO_PAGE() */
252 page = ZERO_PAGE(address);
ceffc078
CO
253 }
254
b5810039
NP
255out:
256 page_cache_get(page);
ceffc078
CO
257 return page;
258}
259
260static struct vm_operations_struct xip_file_vm_ops = {
261 .nopage = xip_file_nopage,
262};
263
264int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
265{
266 BUG_ON(!file->f_mapping->a_ops->get_xip_page);
267
268 file_accessed(file);
269 vma->vm_ops = &xip_file_vm_ops;
270 return 0;
271}
272EXPORT_SYMBOL_GPL(xip_file_mmap);
273
274static ssize_t
eb6fe0c3
CO
275__xip_file_write(struct file *filp, const char __user *buf,
276 size_t count, loff_t pos, loff_t *ppos)
ceffc078 277{
eb6fe0c3 278 struct address_space * mapping = filp->f_mapping;
ceffc078
CO
279 struct address_space_operations *a_ops = mapping->a_ops;
280 struct inode *inode = mapping->host;
281 long status = 0;
282 struct page *page;
283 size_t bytes;
ceffc078
CO
284 ssize_t written = 0;
285
286 BUG_ON(!mapping->a_ops->get_xip_page);
287
ceffc078
CO
288 do {
289 unsigned long index;
290 unsigned long offset;
291 size_t copied;
292
293 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
294 index = pos >> PAGE_CACHE_SHIFT;
295 bytes = PAGE_CACHE_SIZE - offset;
296 if (bytes > count)
297 bytes = count;
298
299 /*
300 * Bring in the user page that we will copy from _first_.
301 * Otherwise there's a nasty deadlock on copying from the
302 * same page as we're writing to, without it being marked
303 * up-to-date.
304 */
305 fault_in_pages_readable(buf, bytes);
306
307 page = a_ops->get_xip_page(mapping,
eb6fe0c3 308 index*(PAGE_SIZE/512), 0);
ceffc078
CO
309 if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
310 /* we allocate a new page unmap it */
311 page = a_ops->get_xip_page(mapping,
eb6fe0c3 312 index*(PAGE_SIZE/512), 1);
ceffc078 313 if (!IS_ERR(page))
eb6fe0c3
CO
314 /* unmap page at pgoff from all other vmas */
315 __xip_unmap(mapping, index);
ceffc078
CO
316 }
317
318 if (IS_ERR(page)) {
319 status = PTR_ERR(page);
320 break;
321 }
322
eb6fe0c3 323 copied = filemap_copy_from_user(page, offset, buf, bytes);
ceffc078
CO
324 flush_dcache_page(page);
325 if (likely(copied > 0)) {
326 status = copied;
327
328 if (status >= 0) {
329 written += status;
330 count -= status;
331 pos += status;
332 buf += status;
ceffc078
CO
333 }
334 }
335 if (unlikely(copied != bytes))
336 if (status >= 0)
337 status = -EFAULT;
338 if (status < 0)
339 break;
340 } while (count);
341 *ppos = pos;
342 /*
343 * No need to use i_size_read() here, the i_size
344 * cannot change under us because we hold i_sem.
345 */
346 if (pos > inode->i_size) {
347 i_size_write(inode, pos);
348 mark_inode_dirty(inode);
349 }
350
351 return written ? written : status;
352}
353
eb6fe0c3
CO
354ssize_t
355xip_file_write(struct file *filp, const char __user *buf, size_t len,
356 loff_t *ppos)
ceffc078 357{
eb6fe0c3
CO
358 struct address_space *mapping = filp->f_mapping;
359 struct inode *inode = mapping->host;
360 size_t count;
361 loff_t pos;
362 ssize_t ret;
ceffc078 363
eb6fe0c3 364 down(&inode->i_sem);
ceffc078 365
eb6fe0c3
CO
366 if (!access_ok(VERIFY_READ, buf, len)) {
367 ret=-EFAULT;
368 goto out_up;
ceffc078
CO
369 }
370
ceffc078 371 pos = *ppos;
eb6fe0c3 372 count = len;
ceffc078
CO
373
374 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
375
eb6fe0c3
CO
376 /* We can write back this queue in page reclaim */
377 current->backing_dev_info = mapping->backing_dev_info;
ceffc078 378
eb6fe0c3
CO
379 ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
380 if (ret)
381 goto out_backing;
ceffc078 382 if (count == 0)
eb6fe0c3 383 goto out_backing;
ceffc078 384
eb6fe0c3
CO
385 ret = remove_suid(filp->f_dentry);
386 if (ret)
387 goto out_backing;
ceffc078
CO
388
389 inode_update_time(inode, 1);
390
eb6fe0c3 391 ret = __xip_file_write (filp, buf, count, pos, ppos);
ceffc078 392
eb6fe0c3
CO
393 out_backing:
394 current->backing_dev_info = NULL;
395 out_up:
ceffc078
CO
396 up(&inode->i_sem);
397 return ret;
398}
eb6fe0c3 399EXPORT_SYMBOL_GPL(xip_file_write);
ceffc078
CO
400
401/*
402 * truncate a page used for execute in place
403 * functionality is analog to block_truncate_page but does use get_xip_page
404 * to get the page instead of page cache
405 */
406int
407xip_truncate_page(struct address_space *mapping, loff_t from)
408{
409 pgoff_t index = from >> PAGE_CACHE_SHIFT;
410 unsigned offset = from & (PAGE_CACHE_SIZE-1);
411 unsigned blocksize;
412 unsigned length;
413 struct page *page;
414 void *kaddr;
ceffc078
CO
415
416 BUG_ON(!mapping->a_ops->get_xip_page);
417
418 blocksize = 1 << mapping->host->i_blkbits;
419 length = offset & (blocksize - 1);
420
421 /* Block boundary? Nothing to do */
422 if (!length)
423 return 0;
424
425 length = blocksize - length;
426
427 page = mapping->a_ops->get_xip_page(mapping,
428 index*(PAGE_SIZE/512), 0);
ceffc078 429 if (!page)
eb6fe0c3 430 return -ENOMEM;
ceffc078 431 if (unlikely(IS_ERR(page))) {
eb6fe0c3 432 if (PTR_ERR(page) == -ENODATA)
ceffc078
CO
433 /* Hole? No need to truncate */
434 return 0;
eb6fe0c3
CO
435 else
436 return PTR_ERR(page);
afa597ba 437 }
ceffc078
CO
438 kaddr = kmap_atomic(page, KM_USER0);
439 memset(kaddr + offset, 0, length);
440 kunmap_atomic(kaddr, KM_USER0);
441
442 flush_dcache_page(page);
eb6fe0c3 443 return 0;
ceffc078
CO
444}
445EXPORT_SYMBOL_GPL(xip_truncate_page);