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
12 #include <linux/pagemap.h>
13 #include <linux/module.h>
14 #include <linux/uio.h>
15 #include <linux/rmap.h>
16 #include <linux/vs_memory.h>
17 #include <asm/tlbflush.h>
21 * This is a file read routine for execute in place files, and uses
22 * the mapping->a_ops->get_xip_page() function for the actual low-level
25 * Note the struct file* is not used at all. It may be NULL.
28 do_xip_mapping_read(struct address_space *mapping,
29 struct file_ra_state *_ra,
32 read_descriptor_t *desc,
35 struct inode *inode = mapping->host;
36 unsigned long index, end_index, offset;
39 BUG_ON(!mapping->a_ops->get_xip_page);
41 index = *ppos >> PAGE_CACHE_SHIFT;
42 offset = *ppos & ~PAGE_CACHE_MASK;
44 isize = i_size_read(inode);
48 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
51 unsigned long nr, ret;
53 /* nr is the maximum number of bytes to copy from this page */
55 if (index >= end_index) {
56 if (index > end_index)
58 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
65 page = mapping->a_ops->get_xip_page(mapping,
66 index*(PAGE_SIZE/512), 0);
69 if (unlikely(IS_ERR(page))) {
70 if (PTR_ERR(page) == -ENODATA) {
74 desc->error = PTR_ERR(page);
79 /* If users can be writing to this page using arbitrary
80 * virtual addresses, take care about potential aliasing
81 * before reading the page on the kernel side.
83 if (mapping_writably_mapped(mapping))
84 flush_dcache_page(page);
87 * Ok, we have the page, so now we can copy it to user space...
89 * The actor routine returns how many bytes were actually used..
90 * NOTE! This may not be the same as how much of a user buffer
91 * we filled up (we may be padding etc), so we can only update
92 * "pos" here (the actor routine has to update the user buffer
93 * pointers and the remaining count).
95 ret = actor(desc, page, offset, nr);
97 index += offset >> PAGE_CACHE_SHIFT;
98 offset &= ~PAGE_CACHE_MASK;
100 if (ret == nr && desc->count)
105 /* Did not get the page. Report it */
111 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
117 xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
119 read_descriptor_t desc;
121 if (!access_ok(VERIFY_WRITE, buf, len))
129 do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
130 ppos, &desc, file_read_actor);
137 EXPORT_SYMBOL_GPL(xip_file_read);
140 xip_file_sendfile(struct file *in_file, loff_t *ppos,
141 size_t count, read_actor_t actor, void *target)
143 read_descriptor_t desc;
150 desc.arg.data = target;
153 do_xip_mapping_read(in_file->f_mapping, &in_file->f_ra, in_file,
159 EXPORT_SYMBOL_GPL(xip_file_sendfile);
162 * __xip_unmap is invoked from xip_unmap and
165 * This function walks all vmas of the address_space and unmaps the
166 * ZERO_PAGE when found at pgoff. Should it go in rmap.c?
169 __xip_unmap (struct address_space * mapping,
172 struct vm_area_struct *vma;
173 struct mm_struct *mm;
174 struct prio_tree_iter iter;
175 unsigned long address;
181 spin_lock(&mapping->i_mmap_lock);
182 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
184 address = vma->vm_start +
185 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
186 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
188 pte = page_check_address(page, mm, address, &ptl);
190 /* Nuke the page table entry. */
191 flush_cache_page(vma, address, pte_pfn(*pte));
192 pteval = ptep_clear_flush(vma, address, pte);
193 page_remove_rmap(page, vma);
194 dec_mm_counter(mm, file_rss);
195 BUG_ON(pte_dirty(pteval));
196 pte_unmap_unlock(pte, ptl);
197 page_cache_release(page);
200 spin_unlock(&mapping->i_mmap_lock);
204 * xip_nopage() is invoked via the vma operations vector for a
205 * mapped memory region to read in file data during a page fault.
207 * This function is derived from filemap_nopage, but used for execute in place
210 xip_file_nopage(struct vm_area_struct * area,
211 unsigned long address,
214 struct file *file = area->vm_file;
215 struct address_space *mapping = file->f_mapping;
216 struct inode *inode = mapping->host;
218 unsigned long size, pgoff, endoff;
220 pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
222 endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
225 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
230 page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
234 if (PTR_ERR(page) != -ENODATA)
238 if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
239 (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
240 (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
241 /* maybe shared writable, allocate new block */
242 page = mapping->a_ops->get_xip_page (mapping,
243 pgoff*(PAGE_SIZE/512), 1);
246 /* unmap page at pgoff from all other vmas */
247 __xip_unmap(mapping, pgoff);
249 /* not shared and writable, use ZERO_PAGE() */
254 page_cache_get(page);
258 static struct vm_operations_struct xip_file_vm_ops = {
259 .nopage = xip_file_nopage,
262 int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
264 BUG_ON(!file->f_mapping->a_ops->get_xip_page);
267 vma->vm_ops = &xip_file_vm_ops;
270 EXPORT_SYMBOL_GPL(xip_file_mmap);
273 __xip_file_write(struct file *filp, const char __user *buf,
274 size_t count, loff_t pos, loff_t *ppos)
276 struct address_space * mapping = filp->f_mapping;
277 const struct address_space_operations *a_ops = mapping->a_ops;
278 struct inode *inode = mapping->host;
284 BUG_ON(!mapping->a_ops->get_xip_page);
288 unsigned long offset;
291 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
292 index = pos >> PAGE_CACHE_SHIFT;
293 bytes = PAGE_CACHE_SIZE - offset;
298 * Bring in the user page that we will copy from _first_.
299 * Otherwise there's a nasty deadlock on copying from the
300 * same page as we're writing to, without it being marked
303 fault_in_pages_readable(buf, bytes);
305 page = a_ops->get_xip_page(mapping,
306 index*(PAGE_SIZE/512), 0);
307 if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
308 /* we allocate a new page unmap it */
309 page = a_ops->get_xip_page(mapping,
310 index*(PAGE_SIZE/512), 1);
312 /* unmap page at pgoff from all other vmas */
313 __xip_unmap(mapping, index);
317 status = PTR_ERR(page);
321 copied = filemap_copy_from_user(page, offset, buf, bytes);
322 flush_dcache_page(page);
323 if (likely(copied > 0)) {
333 if (unlikely(copied != bytes))
341 * No need to use i_size_read() here, the i_size
342 * cannot change under us because we hold i_mutex.
344 if (pos > inode->i_size) {
345 i_size_write(inode, pos);
346 mark_inode_dirty(inode);
349 return written ? written : status;
353 xip_file_write(struct file *filp, const char __user *buf, size_t len,
356 struct address_space *mapping = filp->f_mapping;
357 struct inode *inode = mapping->host;
362 mutex_lock(&inode->i_mutex);
364 if (!access_ok(VERIFY_READ, buf, len)) {
372 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
374 /* We can write back this queue in page reclaim */
375 current->backing_dev_info = mapping->backing_dev_info;
377 ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
383 ret = remove_suid(filp->f_path.dentry);
387 file_update_time(filp);
389 ret = __xip_file_write (filp, buf, count, pos, ppos);
392 current->backing_dev_info = NULL;
394 mutex_unlock(&inode->i_mutex);
397 EXPORT_SYMBOL_GPL(xip_file_write);
400 * truncate a page used for execute in place
401 * functionality is analog to block_truncate_page but does use get_xip_page
402 * to get the page instead of page cache
405 xip_truncate_page(struct address_space *mapping, loff_t from)
407 pgoff_t index = from >> PAGE_CACHE_SHIFT;
408 unsigned offset = from & (PAGE_CACHE_SIZE-1);
414 BUG_ON(!mapping->a_ops->get_xip_page);
416 blocksize = 1 << mapping->host->i_blkbits;
417 length = offset & (blocksize - 1);
419 /* Block boundary? Nothing to do */
423 length = blocksize - length;
425 page = mapping->a_ops->get_xip_page(mapping,
426 index*(PAGE_SIZE/512), 0);
429 if (unlikely(IS_ERR(page))) {
430 if (PTR_ERR(page) == -ENODATA)
431 /* Hole? No need to truncate */
434 return PTR_ERR(page);
436 kaddr = kmap_atomic(page, KM_USER0);
437 memset(kaddr + offset, 0, length);
438 kunmap_atomic(kaddr, KM_USER0);
440 flush_dcache_page(page);
443 EXPORT_SYMBOL_GPL(xip_truncate_page);