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