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