blob: 65ffc321f0c0f223e4c6115e91c53d12d8319399 [file] [log] [blame]
Carsten Otteceffc072005-06-23 22:05:25 -07001/*
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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Carsten Otteceffc072005-06-23 22:05:25 -070017#include <asm/tlbflush.h>
18#include "filemap.h"
19
20/*
Carsten Ottea76c0b92007-03-29 01:20:39 -070021 * We do use our own empty page to avoid interference with other users
22 * of ZERO_PAGE(), such as /dev/zero
23 */
24static struct page *__xip_sparse_page;
25
26static struct page *xip_sparse_page(void)
27{
28 if (!__xip_sparse_page) {
29 unsigned long zeroes = get_zeroed_page(GFP_HIGHUSER);
30 if (zeroes) {
31 static DEFINE_SPINLOCK(xip_alloc_lock);
32 spin_lock(&xip_alloc_lock);
33 if (!__xip_sparse_page)
34 __xip_sparse_page = virt_to_page(zeroes);
35 else
36 free_page(zeroes);
37 spin_unlock(&xip_alloc_lock);
38 }
39 }
40 return __xip_sparse_page;
41}
42
43/*
Carsten Otteceffc072005-06-23 22:05:25 -070044 * This is a file read routine for execute in place files, and uses
45 * the mapping->a_ops->get_xip_page() function for the actual low-level
46 * stuff.
47 *
48 * Note the struct file* is not used at all. It may be NULL.
49 */
50static void
51do_xip_mapping_read(struct address_space *mapping,
52 struct file_ra_state *_ra,
53 struct file *filp,
54 loff_t *ppos,
55 read_descriptor_t *desc,
56 read_actor_t actor)
57{
58 struct inode *inode = mapping->host;
59 unsigned long index, end_index, offset;
60 loff_t isize;
61
62 BUG_ON(!mapping->a_ops->get_xip_page);
63
64 index = *ppos >> PAGE_CACHE_SHIFT;
65 offset = *ppos & ~PAGE_CACHE_MASK;
66
67 isize = i_size_read(inode);
68 if (!isize)
69 goto out;
70
71 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
72 for (;;) {
73 struct page *page;
74 unsigned long nr, ret;
75
76 /* nr is the maximum number of bytes to copy from this page */
77 nr = PAGE_CACHE_SIZE;
78 if (index >= end_index) {
79 if (index > end_index)
80 goto out;
81 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
82 if (nr <= offset) {
83 goto out;
84 }
85 }
86 nr = nr - offset;
87
88 page = mapping->a_ops->get_xip_page(mapping,
89 index*(PAGE_SIZE/512), 0);
90 if (!page)
91 goto no_xip_page;
92 if (unlikely(IS_ERR(page))) {
93 if (PTR_ERR(page) == -ENODATA) {
94 /* sparse */
Carsten Otteafa597b2005-07-15 03:56:30 -070095 page = ZERO_PAGE(0);
Carsten Otteceffc072005-06-23 22:05:25 -070096 } else {
97 desc->error = PTR_ERR(page);
98 goto out;
99 }
Carsten Otteafa597b2005-07-15 03:56:30 -0700100 }
Carsten Otteceffc072005-06-23 22:05:25 -0700101
102 /* If users can be writing to this page using arbitrary
103 * virtual addresses, take care about potential aliasing
104 * before reading the page on the kernel side.
105 */
106 if (mapping_writably_mapped(mapping))
107 flush_dcache_page(page);
108
109 /*
Carsten Otteafa597b2005-07-15 03:56:30 -0700110 * Ok, we have the page, so now we can copy it to user space...
Carsten Otteceffc072005-06-23 22:05:25 -0700111 *
112 * The actor routine returns how many bytes were actually used..
113 * NOTE! This may not be the same as how much of a user buffer
114 * we filled up (we may be padding etc), so we can only update
115 * "pos" here (the actor routine has to update the user buffer
116 * pointers and the remaining count).
117 */
118 ret = actor(desc, page, offset, nr);
119 offset += ret;
120 index += offset >> PAGE_CACHE_SHIFT;
121 offset &= ~PAGE_CACHE_MASK;
122
123 if (ret == nr && desc->count)
124 continue;
125 goto out;
126
127no_xip_page:
128 /* Did not get the page. Report it */
129 desc->error = -EIO;
130 goto out;
131 }
132
133out:
134 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
135 if (filp)
136 file_accessed(filp);
137}
138
Carsten Otteceffc072005-06-23 22:05:25 -0700139ssize_t
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700140xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -0700141{
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700142 read_descriptor_t desc;
Carsten Otteceffc072005-06-23 22:05:25 -0700143
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700144 if (!access_ok(VERIFY_WRITE, buf, len))
145 return -EFAULT;
146
147 desc.written = 0;
148 desc.arg.buf = buf;
149 desc.count = len;
150 desc.error = 0;
151
152 do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
153 ppos, &desc, file_read_actor);
154
155 if (desc.written)
156 return desc.written;
157 else
158 return desc.error;
Carsten Otteceffc072005-06-23 22:05:25 -0700159}
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700160EXPORT_SYMBOL_GPL(xip_file_read);
Carsten Otteceffc072005-06-23 22:05:25 -0700161
Carsten Otteceffc072005-06-23 22:05:25 -0700162/*
163 * __xip_unmap is invoked from xip_unmap and
164 * xip_write
165 *
166 * This function walks all vmas of the address_space and unmaps the
Carsten Ottea76c0b92007-03-29 01:20:39 -0700167 * __xip_sparse_page when found at pgoff.
Carsten Otteceffc072005-06-23 22:05:25 -0700168 */
169static void
170__xip_unmap (struct address_space * mapping,
171 unsigned long pgoff)
172{
173 struct vm_area_struct *vma;
174 struct mm_struct *mm;
175 struct prio_tree_iter iter;
176 unsigned long address;
177 pte_t *pte;
178 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700179 spinlock_t *ptl;
Hugh Dickins67b02f12005-10-29 18:16:31 -0700180 struct page *page;
Carsten Otteceffc072005-06-23 22:05:25 -0700181
Carsten Ottea76c0b92007-03-29 01:20:39 -0700182 page = __xip_sparse_page;
183 if (!page)
184 return;
185
Carsten Otteceffc072005-06-23 22:05:25 -0700186 spin_lock(&mapping->i_mmap_lock);
187 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
188 mm = vma->vm_mm;
189 address = vma->vm_start +
190 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
191 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700192 pte = page_check_address(page, mm, address, &ptl);
193 if (pte) {
Carsten Otteceffc072005-06-23 22:05:25 -0700194 /* Nuke the page table entry. */
Geert Uytterhoeven082ff0a2005-07-12 13:58:18 -0700195 flush_cache_page(vma, address, pte_pfn(*pte));
Carsten Otteceffc072005-06-23 22:05:25 -0700196 pteval = ptep_clear_flush(vma, address, pte);
Nick Piggin7de6b802006-12-22 01:09:33 -0800197 page_remove_rmap(page, vma);
Nick Pigginb5810032005-10-29 18:16:12 -0700198 dec_mm_counter(mm, file_rss);
Carsten Otteceffc072005-06-23 22:05:25 -0700199 BUG_ON(pte_dirty(pteval));
Hugh Dickinsc0718802005-10-29 18:16:31 -0700200 pte_unmap_unlock(pte, ptl);
Nick Pigginb5810032005-10-29 18:16:12 -0700201 page_cache_release(page);
Carsten Otteceffc072005-06-23 22:05:25 -0700202 }
203 }
204 spin_unlock(&mapping->i_mmap_lock);
205}
206
207/*
208 * xip_nopage() is invoked via the vma operations vector for a
209 * mapped memory region to read in file data during a page fault.
210 *
211 * This function is derived from filemap_nopage, but used for execute in place
212 */
213static struct page *
214xip_file_nopage(struct vm_area_struct * area,
215 unsigned long address,
216 int *type)
217{
218 struct file *file = area->vm_file;
219 struct address_space *mapping = file->f_mapping;
220 struct inode *inode = mapping->host;
221 struct page *page;
222 unsigned long size, pgoff, endoff;
223
224 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
225 + area->vm_pgoff;
226 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
227 + area->vm_pgoff;
228
229 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Carsten Ottea76c0b92007-03-29 01:20:39 -0700230 if (pgoff >= size)
231 return NOPAGE_SIGBUS;
Carsten Otteceffc072005-06-23 22:05:25 -0700232
233 page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
Carsten Ottea76c0b92007-03-29 01:20:39 -0700234 if (!IS_ERR(page))
Nick Pigginb5810032005-10-29 18:16:12 -0700235 goto out;
Carsten Otteceffc072005-06-23 22:05:25 -0700236 if (PTR_ERR(page) != -ENODATA)
Carsten Ottea76c0b92007-03-29 01:20:39 -0700237 return NOPAGE_SIGBUS;
Carsten Otteceffc072005-06-23 22:05:25 -0700238
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))
Carsten Ottea76c0b92007-03-29 01:20:39 -0700247 return NOPAGE_SIGBUS;
Carsten Otteceffc072005-06-23 22:05:25 -0700248 /* unmap page at pgoff from all other vmas */
249 __xip_unmap(mapping, pgoff);
250 } else {
Carsten Ottea76c0b92007-03-29 01:20:39 -0700251 /* not shared and writable, use xip_sparse_page() */
252 page = xip_sparse_page();
253 if (!page)
254 return NOPAGE_OOM;
Carsten Otteceffc072005-06-23 22:05:25 -0700255 }
256
Nick Pigginb5810032005-10-29 18:16:12 -0700257out:
258 page_cache_get(page);
Carsten Otteceffc072005-06-23 22:05:25 -0700259 return page;
260}
261
262static struct vm_operations_struct xip_file_vm_ops = {
263 .nopage = xip_file_nopage,
264};
265
266int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
267{
268 BUG_ON(!file->f_mapping->a_ops->get_xip_page);
269
270 file_accessed(file);
271 vma->vm_ops = &xip_file_vm_ops;
272 return 0;
273}
274EXPORT_SYMBOL_GPL(xip_file_mmap);
275
276static ssize_t
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700277__xip_file_write(struct file *filp, const char __user *buf,
278 size_t count, loff_t pos, loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -0700279{
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700280 struct address_space * mapping = filp->f_mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700281 const struct address_space_operations *a_ops = mapping->a_ops;
Carsten Otteceffc072005-06-23 22:05:25 -0700282 struct inode *inode = mapping->host;
283 long status = 0;
284 struct page *page;
285 size_t bytes;
Carsten Otteceffc072005-06-23 22:05:25 -0700286 ssize_t written = 0;
287
288 BUG_ON(!mapping->a_ops->get_xip_page);
289
Carsten Otteceffc072005-06-23 22:05:25 -0700290 do {
291 unsigned long index;
292 unsigned long offset;
293 size_t copied;
294
295 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
296 index = pos >> PAGE_CACHE_SHIFT;
297 bytes = PAGE_CACHE_SIZE - offset;
298 if (bytes > count)
299 bytes = count;
300
301 /*
302 * Bring in the user page that we will copy from _first_.
303 * Otherwise there's a nasty deadlock on copying from the
304 * same page as we're writing to, without it being marked
305 * up-to-date.
306 */
307 fault_in_pages_readable(buf, bytes);
308
309 page = a_ops->get_xip_page(mapping,
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700310 index*(PAGE_SIZE/512), 0);
Carsten Otteceffc072005-06-23 22:05:25 -0700311 if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
312 /* we allocate a new page unmap it */
313 page = a_ops->get_xip_page(mapping,
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700314 index*(PAGE_SIZE/512), 1);
Carsten Otteceffc072005-06-23 22:05:25 -0700315 if (!IS_ERR(page))
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700316 /* unmap page at pgoff from all other vmas */
317 __xip_unmap(mapping, index);
Carsten Otteceffc072005-06-23 22:05:25 -0700318 }
319
320 if (IS_ERR(page)) {
321 status = PTR_ERR(page);
322 break;
323 }
324
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700325 copied = filemap_copy_from_user(page, offset, buf, bytes);
Carsten Otteceffc072005-06-23 22:05:25 -0700326 flush_dcache_page(page);
327 if (likely(copied > 0)) {
328 status = copied;
329
330 if (status >= 0) {
331 written += status;
332 count -= status;
333 pos += status;
334 buf += status;
Carsten Otteceffc072005-06-23 22:05:25 -0700335 }
336 }
337 if (unlikely(copied != bytes))
338 if (status >= 0)
339 status = -EFAULT;
340 if (status < 0)
341 break;
342 } while (count);
343 *ppos = pos;
344 /*
345 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800346 * cannot change under us because we hold i_mutex.
Carsten Otteceffc072005-06-23 22:05:25 -0700347 */
348 if (pos > inode->i_size) {
349 i_size_write(inode, pos);
350 mark_inode_dirty(inode);
351 }
352
353 return written ? written : status;
354}
355
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700356ssize_t
357xip_file_write(struct file *filp, const char __user *buf, size_t len,
358 loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -0700359{
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700360 struct address_space *mapping = filp->f_mapping;
361 struct inode *inode = mapping->host;
362 size_t count;
363 loff_t pos;
364 ssize_t ret;
Carsten Otteceffc072005-06-23 22:05:25 -0700365
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800366 mutex_lock(&inode->i_mutex);
Carsten Otteceffc072005-06-23 22:05:25 -0700367
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700368 if (!access_ok(VERIFY_READ, buf, len)) {
369 ret=-EFAULT;
370 goto out_up;
Carsten Otteceffc072005-06-23 22:05:25 -0700371 }
372
Carsten Otteceffc072005-06-23 22:05:25 -0700373 pos = *ppos;
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700374 count = len;
Carsten Otteceffc072005-06-23 22:05:25 -0700375
376 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
377
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700378 /* We can write back this queue in page reclaim */
379 current->backing_dev_info = mapping->backing_dev_info;
Carsten Otteceffc072005-06-23 22:05:25 -0700380
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700381 ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
382 if (ret)
383 goto out_backing;
Carsten Otteceffc072005-06-23 22:05:25 -0700384 if (count == 0)
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700385 goto out_backing;
Carsten Otteceffc072005-06-23 22:05:25 -0700386
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800387 ret = remove_suid(filp->f_path.dentry);
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700388 if (ret)
389 goto out_backing;
Carsten Otteceffc072005-06-23 22:05:25 -0700390
Christoph Hellwig870f4812006-01-09 20:52:01 -0800391 file_update_time(filp);
Carsten Otteceffc072005-06-23 22:05:25 -0700392
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700393 ret = __xip_file_write (filp, buf, count, pos, ppos);
Carsten Otteceffc072005-06-23 22:05:25 -0700394
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700395 out_backing:
396 current->backing_dev_info = NULL;
397 out_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800398 mutex_unlock(&inode->i_mutex);
Carsten Otteceffc072005-06-23 22:05:25 -0700399 return ret;
400}
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700401EXPORT_SYMBOL_GPL(xip_file_write);
Carsten Otteceffc072005-06-23 22:05:25 -0700402
403/*
404 * truncate a page used for execute in place
405 * functionality is analog to block_truncate_page but does use get_xip_page
406 * to get the page instead of page cache
407 */
408int
409xip_truncate_page(struct address_space *mapping, loff_t from)
410{
411 pgoff_t index = from >> PAGE_CACHE_SHIFT;
412 unsigned offset = from & (PAGE_CACHE_SIZE-1);
413 unsigned blocksize;
414 unsigned length;
415 struct page *page;
Carsten Otteceffc072005-06-23 22:05:25 -0700416
417 BUG_ON(!mapping->a_ops->get_xip_page);
418
419 blocksize = 1 << mapping->host->i_blkbits;
420 length = offset & (blocksize - 1);
421
422 /* Block boundary? Nothing to do */
423 if (!length)
424 return 0;
425
426 length = blocksize - length;
427
428 page = mapping->a_ops->get_xip_page(mapping,
429 index*(PAGE_SIZE/512), 0);
Carsten Otteceffc072005-06-23 22:05:25 -0700430 if (!page)
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700431 return -ENOMEM;
Carsten Otteceffc072005-06-23 22:05:25 -0700432 if (unlikely(IS_ERR(page))) {
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700433 if (PTR_ERR(page) == -ENODATA)
Carsten Otteceffc072005-06-23 22:05:25 -0700434 /* Hole? No need to truncate */
435 return 0;
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700436 else
437 return PTR_ERR(page);
Carsten Otteafa597b2005-07-15 03:56:30 -0700438 }
Nate Diller01f27052007-05-09 02:35:07 -0700439 zero_user_page(page, offset, length, KM_USER0);
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700440 return 0;
Carsten Otteceffc072005-06-23 22:05:25 -0700441}
442EXPORT_SYMBOL_GPL(xip_truncate_page);