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/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);
31 static DEFINE_SPINLOCK(xip_alloc_lock);
32 spin_lock(&xip_alloc_lock);
33 if (!__xip_sparse_page)
34 __xip_sparse_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
48 * Note the struct file* is not used at all. It may be NULL.
51 do_xip_mapping_read(struct address_space *mapping,
52 struct file_ra_state *_ra,
55 read_descriptor_t *desc,
58 struct inode *inode = mapping->host;
59 pgoff_t index, end_index;
63 BUG_ON(!mapping->a_ops->get_xip_page);
65 index = *ppos >> PAGE_CACHE_SHIFT;
66 offset = *ppos & ~PAGE_CACHE_MASK;
68 isize = i_size_read(inode);
72 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
75 unsigned long nr, ret;
77 /* nr is the maximum number of bytes to copy from this page */
79 if (index >= end_index) {
80 if (index > end_index)
82 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
89 page = mapping->a_ops->get_xip_page(mapping,
90 index*(PAGE_SIZE/512), 0);
93 if (unlikely(IS_ERR(page))) {
94 if (PTR_ERR(page) == -ENODATA) {
98 desc->error = PTR_ERR(page);
103 /* If users can be writing to this page using arbitrary
104 * virtual addresses, take care about potential aliasing
105 * before reading the page on the kernel side.
107 if (mapping_writably_mapped(mapping))
108 flush_dcache_page(page);
111 * Ok, we have the page, so now we can copy it to user space...
113 * The actor routine returns how many bytes were actually used..
114 * NOTE! This may not be the same as how much of a user buffer
115 * we filled up (we may be padding etc), so we can only update
116 * "pos" here (the actor routine has to update the user buffer
117 * pointers and the remaining count).
119 ret = actor(desc, page, offset, nr);
121 index += offset >> PAGE_CACHE_SHIFT;
122 offset &= ~PAGE_CACHE_MASK;
124 if (ret == nr && desc->count)
129 /* Did not get the page. Report it */
135 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
141 xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
143 read_descriptor_t desc;
145 if (!access_ok(VERIFY_WRITE, buf, len))
153 do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
154 ppos, &desc, file_read_actor);
161 EXPORT_SYMBOL_GPL(xip_file_read);
164 * __xip_unmap is invoked from xip_unmap and
167 * This function walks all vmas of the address_space and unmaps the
168 * __xip_sparse_page when found at pgoff.
171 __xip_unmap (struct address_space * mapping,
174 struct vm_area_struct *vma;
175 struct mm_struct *mm;
176 struct prio_tree_iter iter;
177 unsigned long address;
183 page = __xip_sparse_page;
187 spin_lock(&mapping->i_mmap_lock);
188 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
190 address = vma->vm_start +
191 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
192 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
193 pte = page_check_address(page, mm, address, &ptl);
195 /* Nuke the page table entry. */
196 flush_cache_page(vma, address, pte_pfn(*pte));
197 pteval = ptep_clear_flush(vma, address, pte);
198 page_remove_rmap(page, vma);
199 dec_mm_counter(mm, file_rss);
200 BUG_ON(pte_dirty(pteval));
201 pte_unmap_unlock(pte, ptl);
202 page_cache_release(page);
205 spin_unlock(&mapping->i_mmap_lock);
209 * xip_fault() is invoked via the vma operations vector for a
210 * mapped memory region to read in file data during a page fault.
212 * This function is derived from filemap_fault, but used for execute in place
214 static int xip_file_fault(struct vm_area_struct *area, struct vm_fault *vmf)
216 struct file *file = area->vm_file;
217 struct address_space *mapping = file->f_mapping;
218 struct inode *inode = mapping->host;
222 /* XXX: are VM_FAULT_ codes OK? */
224 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
225 if (vmf->pgoff >= size)
226 return VM_FAULT_SIGBUS;
228 page = mapping->a_ops->get_xip_page(mapping,
229 vmf->pgoff*(PAGE_SIZE/512), 0);
232 if (PTR_ERR(page) != -ENODATA)
236 if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
237 (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
238 (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
239 /* maybe shared writable, allocate new block */
240 page = mapping->a_ops->get_xip_page(mapping,
241 vmf->pgoff*(PAGE_SIZE/512), 1);
243 return VM_FAULT_SIGBUS;
244 /* unmap page at pgoff from all other vmas */
245 __xip_unmap(mapping, vmf->pgoff);
247 /* not shared and writable, use xip_sparse_page() */
248 page = xip_sparse_page();
254 page_cache_get(page);
259 static struct vm_operations_struct xip_file_vm_ops = {
260 .fault = xip_file_fault,
263 int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
265 BUG_ON(!file->f_mapping->a_ops->get_xip_page);
268 vma->vm_ops = &xip_file_vm_ops;
269 vma->vm_flags |= VM_CAN_NONLINEAR;
272 EXPORT_SYMBOL_GPL(xip_file_mmap);
275 __xip_file_write(struct file *filp, const char __user *buf,
276 size_t count, loff_t pos, loff_t *ppos)
278 struct address_space * mapping = filp->f_mapping;
279 const struct address_space_operations *a_ops = mapping->a_ops;
280 struct inode *inode = mapping->host;
286 BUG_ON(!mapping->a_ops->get_xip_page);
290 unsigned long offset;
294 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
295 index = pos >> PAGE_CACHE_SHIFT;
296 bytes = PAGE_CACHE_SIZE - offset;
300 page = a_ops->get_xip_page(mapping,
301 index*(PAGE_SIZE/512), 0);
302 if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
303 /* we allocate a new page unmap it */
304 page = a_ops->get_xip_page(mapping,
305 index*(PAGE_SIZE/512), 1);
307 /* unmap page at pgoff from all other vmas */
308 __xip_unmap(mapping, index);
312 status = PTR_ERR(page);
316 fault_in_pages_readable(buf, bytes);
317 kaddr = kmap_atomic(page, KM_USER0);
319 __copy_from_user_inatomic_nocache(kaddr + offset, buf, bytes);
320 kunmap_atomic(kaddr, KM_USER0);
321 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);
413 BUG_ON(!mapping->a_ops->get_xip_page);
415 blocksize = 1 << mapping->host->i_blkbits;
416 length = offset & (blocksize - 1);
418 /* Block boundary? Nothing to do */
422 length = blocksize - length;
424 page = mapping->a_ops->get_xip_page(mapping,
425 index*(PAGE_SIZE/512), 0);
428 if (unlikely(IS_ERR(page))) {
429 if (PTR_ERR(page) == -ENODATA)
430 /* Hole? No need to truncate */
433 return PTR_ERR(page);
435 zero_user(page, offset, length);
438 EXPORT_SYMBOL_GPL(xip_truncate_page);