Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 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> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 17 | #include <asm/tlbflush.h> |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
Carsten Otte | a76c0b9 | 2007-03-29 01:20:39 -0700 | [diff] [blame] | 20 | * We do use our own empty page to avoid interference with other users |
| 21 | * of ZERO_PAGE(), such as /dev/zero |
| 22 | */ |
| 23 | static struct page *__xip_sparse_page; |
| 24 | |
| 25 | static struct page *xip_sparse_page(void) |
| 26 | { |
| 27 | if (!__xip_sparse_page) { |
| 28 | unsigned long zeroes = get_zeroed_page(GFP_HIGHUSER); |
| 29 | if (zeroes) { |
| 30 | static DEFINE_SPINLOCK(xip_alloc_lock); |
| 31 | spin_lock(&xip_alloc_lock); |
| 32 | if (!__xip_sparse_page) |
| 33 | __xip_sparse_page = virt_to_page(zeroes); |
| 34 | else |
| 35 | free_page(zeroes); |
| 36 | spin_unlock(&xip_alloc_lock); |
| 37 | } |
| 38 | } |
| 39 | return __xip_sparse_page; |
| 40 | } |
| 41 | |
| 42 | /* |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 43 | * This is a file read routine for execute in place files, and uses |
| 44 | * the mapping->a_ops->get_xip_page() function for the actual low-level |
| 45 | * stuff. |
| 46 | * |
| 47 | * Note the struct file* is not used at all. It may be NULL. |
| 48 | */ |
| 49 | static void |
| 50 | do_xip_mapping_read(struct address_space *mapping, |
| 51 | struct file_ra_state *_ra, |
| 52 | struct file *filp, |
| 53 | loff_t *ppos, |
| 54 | read_descriptor_t *desc, |
| 55 | read_actor_t actor) |
| 56 | { |
| 57 | struct inode *inode = mapping->host; |
| 58 | unsigned long index, end_index, offset; |
| 59 | loff_t isize; |
| 60 | |
| 61 | BUG_ON(!mapping->a_ops->get_xip_page); |
| 62 | |
| 63 | index = *ppos >> PAGE_CACHE_SHIFT; |
| 64 | offset = *ppos & ~PAGE_CACHE_MASK; |
| 65 | |
| 66 | isize = i_size_read(inode); |
| 67 | if (!isize) |
| 68 | goto out; |
| 69 | |
| 70 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 71 | for (;;) { |
| 72 | struct page *page; |
| 73 | unsigned long nr, ret; |
| 74 | |
| 75 | /* nr is the maximum number of bytes to copy from this page */ |
| 76 | nr = PAGE_CACHE_SIZE; |
| 77 | if (index >= end_index) { |
| 78 | if (index > end_index) |
| 79 | goto out; |
| 80 | nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 81 | if (nr <= offset) { |
| 82 | goto out; |
| 83 | } |
| 84 | } |
| 85 | nr = nr - offset; |
| 86 | |
| 87 | page = mapping->a_ops->get_xip_page(mapping, |
| 88 | index*(PAGE_SIZE/512), 0); |
| 89 | if (!page) |
| 90 | goto no_xip_page; |
| 91 | if (unlikely(IS_ERR(page))) { |
| 92 | if (PTR_ERR(page) == -ENODATA) { |
| 93 | /* sparse */ |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 94 | page = ZERO_PAGE(0); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 95 | } else { |
| 96 | desc->error = PTR_ERR(page); |
| 97 | goto out; |
| 98 | } |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 99 | } |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 100 | |
| 101 | /* If users can be writing to this page using arbitrary |
| 102 | * virtual addresses, take care about potential aliasing |
| 103 | * before reading the page on the kernel side. |
| 104 | */ |
| 105 | if (mapping_writably_mapped(mapping)) |
| 106 | flush_dcache_page(page); |
| 107 | |
| 108 | /* |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 109 | * Ok, we have the page, so now we can copy it to user space... |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 110 | * |
| 111 | * The actor routine returns how many bytes were actually used.. |
| 112 | * NOTE! This may not be the same as how much of a user buffer |
| 113 | * we filled up (we may be padding etc), so we can only update |
| 114 | * "pos" here (the actor routine has to update the user buffer |
| 115 | * pointers and the remaining count). |
| 116 | */ |
| 117 | ret = actor(desc, page, offset, nr); |
| 118 | offset += ret; |
| 119 | index += offset >> PAGE_CACHE_SHIFT; |
| 120 | offset &= ~PAGE_CACHE_MASK; |
| 121 | |
| 122 | if (ret == nr && desc->count) |
| 123 | continue; |
| 124 | goto out; |
| 125 | |
| 126 | no_xip_page: |
| 127 | /* Did not get the page. Report it */ |
| 128 | desc->error = -EIO; |
| 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | out: |
| 133 | *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; |
| 134 | if (filp) |
| 135 | file_accessed(filp); |
| 136 | } |
| 137 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 138 | ssize_t |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 139 | xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 140 | { |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 141 | read_descriptor_t desc; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 142 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 143 | if (!access_ok(VERIFY_WRITE, buf, len)) |
| 144 | return -EFAULT; |
| 145 | |
| 146 | desc.written = 0; |
| 147 | desc.arg.buf = buf; |
| 148 | desc.count = len; |
| 149 | desc.error = 0; |
| 150 | |
| 151 | do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp, |
| 152 | ppos, &desc, file_read_actor); |
| 153 | |
| 154 | if (desc.written) |
| 155 | return desc.written; |
| 156 | else |
| 157 | return desc.error; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 158 | } |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 159 | EXPORT_SYMBOL_GPL(xip_file_read); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 160 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 161 | /* |
| 162 | * __xip_unmap is invoked from xip_unmap and |
| 163 | * xip_write |
| 164 | * |
| 165 | * This function walks all vmas of the address_space and unmaps the |
Carsten Otte | a76c0b9 | 2007-03-29 01:20:39 -0700 | [diff] [blame] | 166 | * __xip_sparse_page when found at pgoff. |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 167 | */ |
| 168 | static void |
| 169 | __xip_unmap (struct address_space * mapping, |
| 170 | unsigned long pgoff) |
| 171 | { |
| 172 | struct vm_area_struct *vma; |
| 173 | struct mm_struct *mm; |
| 174 | struct prio_tree_iter iter; |
| 175 | unsigned long address; |
| 176 | pte_t *pte; |
| 177 | pte_t pteval; |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 178 | spinlock_t *ptl; |
Hugh Dickins | 67b02f1 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 179 | struct page *page; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 180 | |
Carsten Otte | a76c0b9 | 2007-03-29 01:20:39 -0700 | [diff] [blame] | 181 | page = __xip_sparse_page; |
| 182 | if (!page) |
| 183 | return; |
| 184 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 185 | spin_lock(&mapping->i_mmap_lock); |
| 186 | vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
| 187 | mm = vma->vm_mm; |
| 188 | address = vma->vm_start + |
| 189 | ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); |
| 190 | BUG_ON(address < vma->vm_start || address >= vma->vm_end); |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 191 | pte = page_check_address(page, mm, address, &ptl); |
| 192 | if (pte) { |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 193 | /* Nuke the page table entry. */ |
Geert Uytterhoeven | 082ff0a | 2005-07-12 13:58:18 -0700 | [diff] [blame] | 194 | flush_cache_page(vma, address, pte_pfn(*pte)); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 195 | pteval = ptep_clear_flush(vma, address, pte); |
Nick Piggin | 7de6b80 | 2006-12-22 01:09:33 -0800 | [diff] [blame] | 196 | page_remove_rmap(page, vma); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 197 | dec_mm_counter(mm, file_rss); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 198 | BUG_ON(pte_dirty(pteval)); |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 199 | pte_unmap_unlock(pte, ptl); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 200 | page_cache_release(page); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | spin_unlock(&mapping->i_mmap_lock); |
| 204 | } |
| 205 | |
| 206 | /* |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 207 | * xip_fault() is invoked via the vma operations vector for a |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 208 | * mapped memory region to read in file data during a page fault. |
| 209 | * |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 210 | * This function is derived from filemap_fault, but used for execute in place |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 211 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 212 | static int xip_file_fault(struct vm_area_struct *area, struct vm_fault *vmf) |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 213 | { |
| 214 | struct file *file = area->vm_file; |
| 215 | struct address_space *mapping = file->f_mapping; |
| 216 | struct inode *inode = mapping->host; |
| 217 | struct page *page; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 218 | pgoff_t size; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 219 | |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 220 | /* XXX: are VM_FAULT_ codes OK? */ |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 221 | |
| 222 | size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 223 | if (vmf->pgoff >= size) |
| 224 | return VM_FAULT_SIGBUS; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 225 | |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 226 | page = mapping->a_ops->get_xip_page(mapping, |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 227 | vmf->pgoff*(PAGE_SIZE/512), 0); |
Carsten Otte | a76c0b9 | 2007-03-29 01:20:39 -0700 | [diff] [blame] | 228 | if (!IS_ERR(page)) |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 229 | goto out; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 230 | if (PTR_ERR(page) != -ENODATA) |
| 231 | return VM_FAULT_OOM; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 232 | |
| 233 | /* sparse block */ |
| 234 | if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) && |
| 235 | (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) && |
| 236 | (!(mapping->host->i_sb->s_flags & MS_RDONLY))) { |
| 237 | /* maybe shared writable, allocate new block */ |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 238 | page = mapping->a_ops->get_xip_page(mapping, |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 239 | vmf->pgoff*(PAGE_SIZE/512), 1); |
| 240 | if (IS_ERR(page)) |
| 241 | return VM_FAULT_SIGBUS; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 242 | /* unmap page at pgoff from all other vmas */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 243 | __xip_unmap(mapping, vmf->pgoff); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 244 | } else { |
Carsten Otte | a76c0b9 | 2007-03-29 01:20:39 -0700 | [diff] [blame] | 245 | /* not shared and writable, use xip_sparse_page() */ |
| 246 | page = xip_sparse_page(); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 247 | if (!page) |
| 248 | return VM_FAULT_OOM; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 251 | out: |
| 252 | page_cache_get(page); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 253 | vmf->page = page; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 254 | return 0; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static struct vm_operations_struct xip_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 258 | .fault = xip_file_fault, |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | int xip_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 262 | { |
| 263 | BUG_ON(!file->f_mapping->a_ops->get_xip_page); |
| 264 | |
| 265 | file_accessed(file); |
| 266 | vma->vm_ops = &xip_file_vm_ops; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 267 | vma->vm_flags |= VM_CAN_NONLINEAR; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | EXPORT_SYMBOL_GPL(xip_file_mmap); |
| 271 | |
| 272 | static ssize_t |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 273 | __xip_file_write(struct file *filp, const char __user *buf, |
| 274 | size_t count, loff_t pos, loff_t *ppos) |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 275 | { |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 276 | struct address_space * mapping = filp->f_mapping; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 277 | const struct address_space_operations *a_ops = mapping->a_ops; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 278 | struct inode *inode = mapping->host; |
| 279 | long status = 0; |
| 280 | struct page *page; |
| 281 | size_t bytes; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 282 | ssize_t written = 0; |
| 283 | |
| 284 | BUG_ON(!mapping->a_ops->get_xip_page); |
| 285 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 286 | do { |
| 287 | unsigned long index; |
| 288 | unsigned long offset; |
| 289 | size_t copied; |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 290 | char *kaddr; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 291 | |
| 292 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ |
| 293 | index = pos >> PAGE_CACHE_SHIFT; |
| 294 | bytes = PAGE_CACHE_SIZE - offset; |
| 295 | if (bytes > count) |
| 296 | bytes = count; |
| 297 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 298 | page = a_ops->get_xip_page(mapping, |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 299 | index*(PAGE_SIZE/512), 0); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 300 | if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) { |
| 301 | /* we allocate a new page unmap it */ |
| 302 | page = a_ops->get_xip_page(mapping, |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 303 | index*(PAGE_SIZE/512), 1); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 304 | if (!IS_ERR(page)) |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 305 | /* unmap page at pgoff from all other vmas */ |
| 306 | __xip_unmap(mapping, index); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (IS_ERR(page)) { |
| 310 | status = PTR_ERR(page); |
| 311 | break; |
| 312 | } |
| 313 | |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 314 | fault_in_pages_readable(buf, bytes); |
| 315 | kaddr = kmap_atomic(page, KM_USER0); |
| 316 | copied = bytes - |
Nick Piggin | 369b8f5 | 2007-12-04 23:45:25 -0800 | [diff] [blame] | 317 | __copy_from_user_inatomic_nocache(kaddr + offset, buf, bytes); |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 318 | kunmap_atomic(kaddr, KM_USER0); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 319 | flush_dcache_page(page); |
Nick Piggin | 4a9e5ef | 2007-10-16 01:24:58 -0700 | [diff] [blame] | 320 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 321 | if (likely(copied > 0)) { |
| 322 | status = copied; |
| 323 | |
| 324 | if (status >= 0) { |
| 325 | written += status; |
| 326 | count -= status; |
| 327 | pos += status; |
| 328 | buf += status; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | if (unlikely(copied != bytes)) |
| 332 | if (status >= 0) |
| 333 | status = -EFAULT; |
| 334 | if (status < 0) |
| 335 | break; |
| 336 | } while (count); |
| 337 | *ppos = pos; |
| 338 | /* |
| 339 | * No need to use i_size_read() here, the i_size |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 340 | * cannot change under us because we hold i_mutex. |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 341 | */ |
| 342 | if (pos > inode->i_size) { |
| 343 | i_size_write(inode, pos); |
| 344 | mark_inode_dirty(inode); |
| 345 | } |
| 346 | |
| 347 | return written ? written : status; |
| 348 | } |
| 349 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 350 | ssize_t |
| 351 | xip_file_write(struct file *filp, const char __user *buf, size_t len, |
| 352 | loff_t *ppos) |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 353 | { |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 354 | struct address_space *mapping = filp->f_mapping; |
| 355 | struct inode *inode = mapping->host; |
| 356 | size_t count; |
| 357 | loff_t pos; |
| 358 | ssize_t ret; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 359 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 360 | mutex_lock(&inode->i_mutex); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 361 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 362 | if (!access_ok(VERIFY_READ, buf, len)) { |
| 363 | ret=-EFAULT; |
| 364 | goto out_up; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 367 | pos = *ppos; |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 368 | count = len; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 369 | |
| 370 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 371 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 372 | /* We can write back this queue in page reclaim */ |
| 373 | current->backing_dev_info = mapping->backing_dev_info; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 374 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 375 | ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode)); |
| 376 | if (ret) |
| 377 | goto out_backing; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 378 | if (count == 0) |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 379 | goto out_backing; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 380 | |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 381 | ret = remove_suid(filp->f_path.dentry); |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 382 | if (ret) |
| 383 | goto out_backing; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 384 | |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 385 | file_update_time(filp); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 386 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 387 | ret = __xip_file_write (filp, buf, count, pos, ppos); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 388 | |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 389 | out_backing: |
| 390 | current->backing_dev_info = NULL; |
| 391 | out_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 392 | mutex_unlock(&inode->i_mutex); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 393 | return ret; |
| 394 | } |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 395 | EXPORT_SYMBOL_GPL(xip_file_write); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * truncate a page used for execute in place |
| 399 | * functionality is analog to block_truncate_page but does use get_xip_page |
| 400 | * to get the page instead of page cache |
| 401 | */ |
| 402 | int |
| 403 | xip_truncate_page(struct address_space *mapping, loff_t from) |
| 404 | { |
| 405 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 406 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 407 | unsigned blocksize; |
| 408 | unsigned length; |
| 409 | struct page *page; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 410 | |
| 411 | BUG_ON(!mapping->a_ops->get_xip_page); |
| 412 | |
| 413 | blocksize = 1 << mapping->host->i_blkbits; |
| 414 | length = offset & (blocksize - 1); |
| 415 | |
| 416 | /* Block boundary? Nothing to do */ |
| 417 | if (!length) |
| 418 | return 0; |
| 419 | |
| 420 | length = blocksize - length; |
| 421 | |
| 422 | page = mapping->a_ops->get_xip_page(mapping, |
| 423 | index*(PAGE_SIZE/512), 0); |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 424 | if (!page) |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 425 | return -ENOMEM; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 426 | if (unlikely(IS_ERR(page))) { |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 427 | if (PTR_ERR(page) == -ENODATA) |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 428 | /* Hole? No need to truncate */ |
| 429 | return 0; |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 430 | else |
| 431 | return PTR_ERR(page); |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 432 | } |
Nate Diller | 01f2705 | 2007-05-09 02:35:07 -0700 | [diff] [blame] | 433 | zero_user_page(page, offset, length, KM_USER0); |
Carsten Otte | eb6fe0c | 2005-06-23 22:05:28 -0700 | [diff] [blame] | 434 | return 0; |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 435 | } |
| 436 | EXPORT_SYMBOL_GPL(xip_truncate_page); |