b43/nphy.c: include headers to avoid build breakage on some platforms
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / filemap_xip.c
blobf874ae818ad3812f7cd051972c823c743a154d33
1 /*
2 * linux/mm/filemap_xip.c
4 * Copyright (C) 2005 IBM Corporation
5 * Author: Carsten Otte <cotte@de.ibm.com>
7 * derived from linux/mm/filemap.c - Copyright (C) Linus Torvalds
9 */
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 <linux/sched.h>
17 #include <asm/tlbflush.h>
20 * We do use our own empty page to avoid interference with other users
21 * of ZERO_PAGE(), such as /dev/zero
23 static struct page *__xip_sparse_page;
25 static struct page *xip_sparse_page(void)
27 if (!__xip_sparse_page) {
28 struct page *page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
30 if (page) {
31 static DEFINE_SPINLOCK(xip_alloc_lock);
32 spin_lock(&xip_alloc_lock);
33 if (!__xip_sparse_page)
34 __xip_sparse_page = page;
35 else
36 __free_page(page);
37 spin_unlock(&xip_alloc_lock);
40 return __xip_sparse_page;
44 * 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.
48 * Note the struct file* is not used at all. It may be NULL.
50 static void
51 do_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)
58 struct inode *inode = mapping->host;
59 unsigned long index, end_index, offset;
60 loff_t isize;
62 BUG_ON(!mapping->a_ops->get_xip_page);
64 index = *ppos >> PAGE_CACHE_SHIFT;
65 offset = *ppos & ~PAGE_CACHE_MASK;
67 isize = i_size_read(inode);
68 if (!isize)
69 goto out;
71 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
72 for (;;) {
73 struct page *page;
74 unsigned long nr, ret;
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;
86 nr = nr - offset;
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 */
95 page = ZERO_PAGE(0);
96 } else {
97 desc->error = PTR_ERR(page);
98 goto out;
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.
106 if (mapping_writably_mapped(mapping))
107 flush_dcache_page(page);
110 * Ok, we have the page, so now we can copy it to user space...
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).
118 ret = actor(desc, page, offset, nr);
119 offset += ret;
120 index += offset >> PAGE_CACHE_SHIFT;
121 offset &= ~PAGE_CACHE_MASK;
123 if (ret == nr && desc->count)
124 continue;
125 goto out;
127 no_xip_page:
128 /* Did not get the page. Report it */
129 desc->error = -EIO;
130 goto out;
133 out:
134 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
135 if (filp)
136 file_accessed(filp);
139 ssize_t
140 xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
142 read_descriptor_t desc;
144 if (!access_ok(VERIFY_WRITE, buf, len))
145 return -EFAULT;
147 desc.written = 0;
148 desc.arg.buf = buf;
149 desc.count = len;
150 desc.error = 0;
152 do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
153 ppos, &desc, file_read_actor);
155 if (desc.written)
156 return desc.written;
157 else
158 return desc.error;
160 EXPORT_SYMBOL_GPL(xip_file_read);
163 * __xip_unmap is invoked from xip_unmap and
164 * xip_write
166 * This function walks all vmas of the address_space and unmaps the
167 * __xip_sparse_page when found at pgoff.
169 static void
170 __xip_unmap (struct address_space * mapping,
171 unsigned long pgoff)
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;
179 spinlock_t *ptl;
180 struct page *page;
182 page = __xip_sparse_page;
183 if (!page)
184 return;
186 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);
192 pte = page_check_address(page, mm, address, &ptl);
193 if (pte) {
194 /* Nuke the page table entry. */
195 flush_cache_page(vma, address, pte_pfn(*pte));
196 pteval = ptep_clear_flush(vma, address, pte);
197 page_remove_rmap(page, vma);
198 dec_mm_counter(mm, file_rss);
199 BUG_ON(pte_dirty(pteval));
200 pte_unmap_unlock(pte, ptl);
201 page_cache_release(page);
204 spin_unlock(&mapping->i_mmap_lock);
208 * xip_fault() is invoked via the vma operations vector for a
209 * mapped memory region to read in file data during a page fault.
211 * This function is derived from filemap_fault, but used for execute in place
213 static int xip_file_fault(struct vm_area_struct *area, struct vm_fault *vmf)
215 struct file *file = area->vm_file;
216 struct address_space *mapping = file->f_mapping;
217 struct inode *inode = mapping->host;
218 struct page *page;
219 pgoff_t size;
221 /* XXX: are VM_FAULT_ codes OK? */
223 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
224 if (vmf->pgoff >= size)
225 return VM_FAULT_SIGBUS;
227 page = mapping->a_ops->get_xip_page(mapping,
228 vmf->pgoff*(PAGE_SIZE/512), 0);
229 if (!IS_ERR(page))
230 goto out;
231 if (PTR_ERR(page) != -ENODATA)
232 return VM_FAULT_OOM;
234 /* sparse block */
235 if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
236 (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
237 (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
238 /* maybe shared writable, allocate new block */
239 page = mapping->a_ops->get_xip_page(mapping,
240 vmf->pgoff*(PAGE_SIZE/512), 1);
241 if (IS_ERR(page))
242 return VM_FAULT_SIGBUS;
243 /* unmap page at pgoff from all other vmas */
244 __xip_unmap(mapping, vmf->pgoff);
245 } else {
246 /* not shared and writable, use xip_sparse_page() */
247 page = xip_sparse_page();
248 if (!page)
249 return VM_FAULT_OOM;
252 out:
253 page_cache_get(page);
254 vmf->page = page;
255 return 0;
258 static struct vm_operations_struct xip_file_vm_ops = {
259 .fault = xip_file_fault,
262 int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
264 BUG_ON(!file->f_mapping->a_ops->get_xip_page);
266 file_accessed(file);
267 vma->vm_ops = &xip_file_vm_ops;
268 vma->vm_flags |= VM_CAN_NONLINEAR;
269 return 0;
271 EXPORT_SYMBOL_GPL(xip_file_mmap);
273 static ssize_t
274 __xip_file_write(struct file *filp, const char __user *buf,
275 size_t count, loff_t pos, loff_t *ppos)
277 struct address_space * mapping = filp->f_mapping;
278 const struct address_space_operations *a_ops = mapping->a_ops;
279 struct inode *inode = mapping->host;
280 long status = 0;
281 struct page *page;
282 size_t bytes;
283 ssize_t written = 0;
285 BUG_ON(!mapping->a_ops->get_xip_page);
287 do {
288 unsigned long index;
289 unsigned long offset;
290 size_t copied;
291 char *kaddr;
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;
299 page = a_ops->get_xip_page(mapping,
300 index*(PAGE_SIZE/512), 0);
301 if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
302 /* we allocate a new page unmap it */
303 page = a_ops->get_xip_page(mapping,
304 index*(PAGE_SIZE/512), 1);
305 if (!IS_ERR(page))
306 /* unmap page at pgoff from all other vmas */
307 __xip_unmap(mapping, index);
310 if (IS_ERR(page)) {
311 status = PTR_ERR(page);
312 break;
315 fault_in_pages_readable(buf, bytes);
316 kaddr = kmap_atomic(page, KM_USER0);
317 copied = bytes -
318 __copy_from_user_inatomic_nocache(kaddr + offset, buf, bytes);
319 kunmap_atomic(kaddr, KM_USER0);
320 flush_dcache_page(page);
322 if (likely(copied > 0)) {
323 status = copied;
325 if (status >= 0) {
326 written += status;
327 count -= status;
328 pos += status;
329 buf += status;
332 if (unlikely(copied != bytes))
333 if (status >= 0)
334 status = -EFAULT;
335 if (status < 0)
336 break;
337 } while (count);
338 *ppos = pos;
340 * No need to use i_size_read() here, the i_size
341 * cannot change under us because we hold i_mutex.
343 if (pos > inode->i_size) {
344 i_size_write(inode, pos);
345 mark_inode_dirty(inode);
348 return written ? written : status;
351 ssize_t
352 xip_file_write(struct file *filp, const char __user *buf, size_t len,
353 loff_t *ppos)
355 struct address_space *mapping = filp->f_mapping;
356 struct inode *inode = mapping->host;
357 size_t count;
358 loff_t pos;
359 ssize_t ret;
361 mutex_lock(&inode->i_mutex);
363 if (!access_ok(VERIFY_READ, buf, len)) {
364 ret=-EFAULT;
365 goto out_up;
368 pos = *ppos;
369 count = len;
371 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
373 /* We can write back this queue in page reclaim */
374 current->backing_dev_info = mapping->backing_dev_info;
376 ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
377 if (ret)
378 goto out_backing;
379 if (count == 0)
380 goto out_backing;
382 ret = remove_suid(filp->f_path.dentry);
383 if (ret)
384 goto out_backing;
386 file_update_time(filp);
388 ret = __xip_file_write (filp, buf, count, pos, ppos);
390 out_backing:
391 current->backing_dev_info = NULL;
392 out_up:
393 mutex_unlock(&inode->i_mutex);
394 return ret;
396 EXPORT_SYMBOL_GPL(xip_file_write);
399 * truncate a page used for execute in place
400 * functionality is analog to block_truncate_page but does use get_xip_page
401 * to get the page instead of page cache
404 xip_truncate_page(struct address_space *mapping, loff_t from)
406 pgoff_t index = from >> PAGE_CACHE_SHIFT;
407 unsigned offset = from & (PAGE_CACHE_SIZE-1);
408 unsigned blocksize;
409 unsigned length;
410 struct page *page;
412 BUG_ON(!mapping->a_ops->get_xip_page);
414 blocksize = 1 << mapping->host->i_blkbits;
415 length = offset & (blocksize - 1);
417 /* Block boundary? Nothing to do */
418 if (!length)
419 return 0;
421 length = blocksize - length;
423 page = mapping->a_ops->get_xip_page(mapping,
424 index*(PAGE_SIZE/512), 0);
425 if (!page)
426 return -ENOMEM;
427 if (unlikely(IS_ERR(page))) {
428 if (PTR_ERR(page) == -ENODATA)
429 /* Hole? No need to truncate */
430 return 0;
431 else
432 return PTR_ERR(page);
434 zero_user_page(page, offset, length, KM_USER0);
435 return 0;
437 EXPORT_SYMBOL_GPL(xip_truncate_page);