blob: 78b94f0b6d5dbd6e95ac1c5cb44937fc45d13e45 [file] [log] [blame]
Carsten Otteceffc072005-06-23 22:05:25 -07001/*
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>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070016#include <linux/mmu_notifier.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040017#include <linux/sched.h>
Nick Piggin538f8ea62008-08-20 14:09:20 -070018#include <linux/seqlock.h>
19#include <linux/mutex.h>
Carsten Otteceffc072005-06-23 22:05:25 -070020#include <asm/tlbflush.h>
Nick Piggin70688e42008-04-28 02:13:02 -070021#include <asm/io.h>
Carsten Otteceffc072005-06-23 22:05:25 -070022
23/*
Carsten Ottea76c0b92007-03-29 01:20:39 -070024 * We do use our own empty page to avoid interference with other users
25 * of ZERO_PAGE(), such as /dev/zero
26 */
Nick Piggin538f8ea62008-08-20 14:09:20 -070027static DEFINE_MUTEX(xip_sparse_mutex);
28static seqcount_t xip_sparse_seq = SEQCNT_ZERO;
Carsten Ottea76c0b92007-03-29 01:20:39 -070029static struct page *__xip_sparse_page;
30
Nick Piggin538f8ea62008-08-20 14:09:20 -070031/* called under xip_sparse_mutex */
Carsten Ottea76c0b92007-03-29 01:20:39 -070032static struct page *xip_sparse_page(void)
33{
34 if (!__xip_sparse_page) {
Akinobu Mitac51b1a12008-01-08 15:32:57 -080035 struct page *page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
36
Nick Piggin538f8ea62008-08-20 14:09:20 -070037 if (page)
38 __xip_sparse_page = page;
Carsten Ottea76c0b92007-03-29 01:20:39 -070039 }
40 return __xip_sparse_page;
41}
42
43/*
Carsten Otteceffc072005-06-23 22:05:25 -070044 * This is a file read routine for execute in place files, and uses
Nick Piggin70688e42008-04-28 02:13:02 -070045 * the mapping->a_ops->get_xip_mem() function for the actual low-level
Carsten Otteceffc072005-06-23 22:05:25 -070046 * stuff.
47 *
48 * Note the struct file* is not used at all. It may be NULL.
49 */
Nick Piggin70688e42008-04-28 02:13:02 -070050static ssize_t
Carsten Otteceffc072005-06-23 22:05:25 -070051do_xip_mapping_read(struct address_space *mapping,
52 struct file_ra_state *_ra,
53 struct file *filp,
Nick Piggin70688e42008-04-28 02:13:02 -070054 char __user *buf,
55 size_t len,
56 loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -070057{
58 struct inode *inode = mapping->host;
Jan Kara2004dc82008-02-08 04:20:11 -080059 pgoff_t index, end_index;
60 unsigned long offset;
Nick Piggin70688e42008-04-28 02:13:02 -070061 loff_t isize, pos;
62 size_t copied = 0, error = 0;
Carsten Otteceffc072005-06-23 22:05:25 -070063
Nick Piggin70688e42008-04-28 02:13:02 -070064 BUG_ON(!mapping->a_ops->get_xip_mem);
Carsten Otteceffc072005-06-23 22:05:25 -070065
Nick Piggin70688e42008-04-28 02:13:02 -070066 pos = *ppos;
67 index = pos >> PAGE_CACHE_SHIFT;
68 offset = pos & ~PAGE_CACHE_MASK;
Carsten Otteceffc072005-06-23 22:05:25 -070069
70 isize = i_size_read(inode);
71 if (!isize)
72 goto out;
73
74 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Nick Piggin70688e42008-04-28 02:13:02 -070075 do {
76 unsigned long nr, left;
77 void *xip_mem;
78 unsigned long xip_pfn;
79 int zero = 0;
Carsten Otteceffc072005-06-23 22:05:25 -070080
81 /* nr is the maximum number of bytes to copy from this page */
82 nr = PAGE_CACHE_SIZE;
83 if (index >= end_index) {
84 if (index > end_index)
85 goto out;
86 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
87 if (nr <= offset) {
88 goto out;
89 }
90 }
91 nr = nr - offset;
Martin Schwidefsky58984ce2009-04-02 16:56:42 -070092 if (nr > len - copied)
93 nr = len - copied;
Carsten Otteceffc072005-06-23 22:05:25 -070094
Nick Piggin70688e42008-04-28 02:13:02 -070095 error = mapping->a_ops->get_xip_mem(mapping, index, 0,
96 &xip_mem, &xip_pfn);
97 if (unlikely(error)) {
98 if (error == -ENODATA) {
Carsten Otteceffc072005-06-23 22:05:25 -070099 /* sparse */
Nick Piggin70688e42008-04-28 02:13:02 -0700100 zero = 1;
101 } else
Carsten Otteceffc072005-06-23 22:05:25 -0700102 goto out;
Carsten Otteafa597b2005-07-15 03:56:30 -0700103 }
Carsten Otteceffc072005-06-23 22:05:25 -0700104
105 /* If users can be writing to this page using arbitrary
106 * virtual addresses, take care about potential aliasing
107 * before reading the page on the kernel side.
108 */
109 if (mapping_writably_mapped(mapping))
Nick Piggin70688e42008-04-28 02:13:02 -0700110 /* address based flush */ ;
Carsten Otteceffc072005-06-23 22:05:25 -0700111
112 /*
Nick Piggin70688e42008-04-28 02:13:02 -0700113 * Ok, we have the mem, so now we can copy it to user space...
Carsten Otteceffc072005-06-23 22:05:25 -0700114 *
115 * The actor routine returns how many bytes were actually used..
116 * NOTE! This may not be the same as how much of a user buffer
117 * we filled up (we may be padding etc), so we can only update
118 * "pos" here (the actor routine has to update the user buffer
119 * pointers and the remaining count).
120 */
Nick Piggin70688e42008-04-28 02:13:02 -0700121 if (!zero)
122 left = __copy_to_user(buf+copied, xip_mem+offset, nr);
123 else
124 left = __clear_user(buf + copied, nr);
125
126 if (left) {
127 error = -EFAULT;
128 goto out;
129 }
130
131 copied += (nr - left);
132 offset += (nr - left);
Carsten Otteceffc072005-06-23 22:05:25 -0700133 index += offset >> PAGE_CACHE_SHIFT;
134 offset &= ~PAGE_CACHE_MASK;
Nick Piggin70688e42008-04-28 02:13:02 -0700135 } while (copied < len);
Carsten Otteceffc072005-06-23 22:05:25 -0700136
137out:
Nick Piggin70688e42008-04-28 02:13:02 -0700138 *ppos = pos + copied;
Carsten Otteceffc072005-06-23 22:05:25 -0700139 if (filp)
140 file_accessed(filp);
Nick Piggin70688e42008-04-28 02:13:02 -0700141
142 return (copied ? copied : error);
Carsten Otteceffc072005-06-23 22:05:25 -0700143}
144
Carsten Otteceffc072005-06-23 22:05:25 -0700145ssize_t
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700146xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -0700147{
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700148 if (!access_ok(VERIFY_WRITE, buf, len))
149 return -EFAULT;
150
Nick Piggin70688e42008-04-28 02:13:02 -0700151 return do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
152 buf, len, ppos);
Carsten Otteceffc072005-06-23 22:05:25 -0700153}
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700154EXPORT_SYMBOL_GPL(xip_file_read);
Carsten Otteceffc072005-06-23 22:05:25 -0700155
Carsten Otteceffc072005-06-23 22:05:25 -0700156/*
157 * __xip_unmap is invoked from xip_unmap and
158 * xip_write
159 *
160 * This function walks all vmas of the address_space and unmaps the
Carsten Ottea76c0b92007-03-29 01:20:39 -0700161 * __xip_sparse_page when found at pgoff.
Carsten Otteceffc072005-06-23 22:05:25 -0700162 */
163static void
164__xip_unmap (struct address_space * mapping,
165 unsigned long pgoff)
166{
167 struct vm_area_struct *vma;
168 struct mm_struct *mm;
169 struct prio_tree_iter iter;
170 unsigned long address;
171 pte_t *pte;
172 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700173 spinlock_t *ptl;
Hugh Dickins67b02f12005-10-29 18:16:31 -0700174 struct page *page;
Nick Piggin538f8ea62008-08-20 14:09:20 -0700175 unsigned count;
176 int locked = 0;
177
178 count = read_seqcount_begin(&xip_sparse_seq);
Carsten Otteceffc072005-06-23 22:05:25 -0700179
Carsten Ottea76c0b92007-03-29 01:20:39 -0700180 page = __xip_sparse_page;
181 if (!page)
182 return;
183
Nick Piggin538f8ea62008-08-20 14:09:20 -0700184retry:
Carsten Otteceffc072005-06-23 22:05:25 -0700185 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);
Nick Piggin479db0b2008-08-20 14:09:18 -0700191 pte = page_check_address(page, mm, address, &ptl, 1);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700192 if (pte) {
Carsten Otteceffc072005-06-23 22:05:25 -0700193 /* Nuke the page table entry. */
Geert Uytterhoeven082ff0a2005-07-12 13:58:18 -0700194 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700195 pteval = ptep_clear_flush_notify(vma, address, pte);
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800196 page_remove_rmap(page);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800197 dec_mm_counter(mm, MM_FILEPAGES);
Carsten Otteceffc072005-06-23 22:05:25 -0700198 BUG_ON(pte_dirty(pteval));
Hugh Dickinsc0718802005-10-29 18:16:31 -0700199 pte_unmap_unlock(pte, ptl);
Nick Pigginb5810032005-10-29 18:16:12 -0700200 page_cache_release(page);
Carsten Otteceffc072005-06-23 22:05:25 -0700201 }
202 }
203 spin_unlock(&mapping->i_mmap_lock);
Nick Piggin538f8ea62008-08-20 14:09:20 -0700204
205 if (locked) {
206 mutex_unlock(&xip_sparse_mutex);
207 } else if (read_seqcount_retry(&xip_sparse_seq, count)) {
208 mutex_lock(&xip_sparse_mutex);
209 locked = 1;
210 goto retry;
211 }
Carsten Otteceffc072005-06-23 22:05:25 -0700212}
213
214/*
Nick Piggin54cb8822007-07-19 01:46:59 -0700215 * xip_fault() is invoked via the vma operations vector for a
Carsten Otteceffc072005-06-23 22:05:25 -0700216 * mapped memory region to read in file data during a page fault.
217 *
Nick Piggin54cb8822007-07-19 01:46:59 -0700218 * This function is derived from filemap_fault, but used for execute in place
Carsten Otteceffc072005-06-23 22:05:25 -0700219 */
Nick Piggin70688e42008-04-28 02:13:02 -0700220static int xip_file_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Carsten Otteceffc072005-06-23 22:05:25 -0700221{
Nick Piggin70688e42008-04-28 02:13:02 -0700222 struct file *file = vma->vm_file;
Carsten Otteceffc072005-06-23 22:05:25 -0700223 struct address_space *mapping = file->f_mapping;
224 struct inode *inode = mapping->host;
Nick Piggin54cb8822007-07-19 01:46:59 -0700225 pgoff_t size;
Nick Piggin70688e42008-04-28 02:13:02 -0700226 void *xip_mem;
227 unsigned long xip_pfn;
228 struct page *page;
229 int error;
Carsten Otteceffc072005-06-23 22:05:25 -0700230
Nick Piggin54cb8822007-07-19 01:46:59 -0700231 /* XXX: are VM_FAULT_ codes OK? */
Nick Piggin538f8ea62008-08-20 14:09:20 -0700232again:
Carsten Otteceffc072005-06-23 22:05:25 -0700233 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Nick Piggind0217ac2007-07-19 01:47:03 -0700234 if (vmf->pgoff >= size)
235 return VM_FAULT_SIGBUS;
Carsten Otteceffc072005-06-23 22:05:25 -0700236
Nick Piggin70688e42008-04-28 02:13:02 -0700237 error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0,
238 &xip_mem, &xip_pfn);
239 if (likely(!error))
240 goto found;
241 if (error != -ENODATA)
Nick Piggind0217ac2007-07-19 01:47:03 -0700242 return VM_FAULT_OOM;
Carsten Otteceffc072005-06-23 22:05:25 -0700243
244 /* sparse block */
Nick Piggin70688e42008-04-28 02:13:02 -0700245 if ((vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
246 (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) &&
Carsten Otteceffc072005-06-23 22:05:25 -0700247 (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
Nick Piggin70688e42008-04-28 02:13:02 -0700248 int err;
249
Carsten Otteceffc072005-06-23 22:05:25 -0700250 /* maybe shared writable, allocate new block */
Nick Piggin14bac5a2008-08-20 14:09:20 -0700251 mutex_lock(&xip_sparse_mutex);
Nick Piggin70688e42008-04-28 02:13:02 -0700252 error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 1,
253 &xip_mem, &xip_pfn);
Nick Piggin14bac5a2008-08-20 14:09:20 -0700254 mutex_unlock(&xip_sparse_mutex);
Nick Piggin70688e42008-04-28 02:13:02 -0700255 if (error)
Nick Piggind0217ac2007-07-19 01:47:03 -0700256 return VM_FAULT_SIGBUS;
Nick Piggin70688e42008-04-28 02:13:02 -0700257 /* unmap sparse mappings at pgoff from all other vmas */
Nick Piggind0217ac2007-07-19 01:47:03 -0700258 __xip_unmap(mapping, vmf->pgoff);
Nick Piggin70688e42008-04-28 02:13:02 -0700259
260found:
261 err = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
262 xip_pfn);
263 if (err == -ENOMEM)
264 return VM_FAULT_OOM;
265 BUG_ON(err);
266 return VM_FAULT_NOPAGE;
Carsten Otteceffc072005-06-23 22:05:25 -0700267 } else {
Nick Piggin538f8ea62008-08-20 14:09:20 -0700268 int err, ret = VM_FAULT_OOM;
269
270 mutex_lock(&xip_sparse_mutex);
271 write_seqcount_begin(&xip_sparse_seq);
272 error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0,
273 &xip_mem, &xip_pfn);
274 if (unlikely(!error)) {
275 write_seqcount_end(&xip_sparse_seq);
276 mutex_unlock(&xip_sparse_mutex);
277 goto again;
278 }
279 if (error != -ENODATA)
280 goto out;
Carsten Ottea76c0b92007-03-29 01:20:39 -0700281 /* not shared and writable, use xip_sparse_page() */
282 page = xip_sparse_page();
Nick Piggind0217ac2007-07-19 01:47:03 -0700283 if (!page)
Nick Piggin538f8ea62008-08-20 14:09:20 -0700284 goto out;
285 err = vm_insert_page(vma, (unsigned long)vmf->virtual_address,
286 page);
287 if (err == -ENOMEM)
288 goto out;
Carsten Otteceffc072005-06-23 22:05:25 -0700289
Nick Piggin538f8ea62008-08-20 14:09:20 -0700290 ret = VM_FAULT_NOPAGE;
291out:
292 write_seqcount_end(&xip_sparse_seq);
293 mutex_unlock(&xip_sparse_mutex);
294
295 return ret;
Nick Piggin70688e42008-04-28 02:13:02 -0700296 }
Carsten Otteceffc072005-06-23 22:05:25 -0700297}
298
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400299static const struct vm_operations_struct xip_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700300 .fault = xip_file_fault,
Carsten Otteceffc072005-06-23 22:05:25 -0700301};
302
303int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
304{
Nick Piggin70688e42008-04-28 02:13:02 -0700305 BUG_ON(!file->f_mapping->a_ops->get_xip_mem);
Carsten Otteceffc072005-06-23 22:05:25 -0700306
307 file_accessed(file);
308 vma->vm_ops = &xip_file_vm_ops;
Nick Piggin70688e42008-04-28 02:13:02 -0700309 vma->vm_flags |= VM_CAN_NONLINEAR | VM_MIXEDMAP;
Carsten Otteceffc072005-06-23 22:05:25 -0700310 return 0;
311}
312EXPORT_SYMBOL_GPL(xip_file_mmap);
313
314static ssize_t
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700315__xip_file_write(struct file *filp, const char __user *buf,
316 size_t count, loff_t pos, loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -0700317{
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700318 struct address_space * mapping = filp->f_mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700319 const struct address_space_operations *a_ops = mapping->a_ops;
Carsten Otteceffc072005-06-23 22:05:25 -0700320 struct inode *inode = mapping->host;
321 long status = 0;
Carsten Otteceffc072005-06-23 22:05:25 -0700322 size_t bytes;
Carsten Otteceffc072005-06-23 22:05:25 -0700323 ssize_t written = 0;
324
Nick Piggin70688e42008-04-28 02:13:02 -0700325 BUG_ON(!mapping->a_ops->get_xip_mem);
Carsten Otteceffc072005-06-23 22:05:25 -0700326
Carsten Otteceffc072005-06-23 22:05:25 -0700327 do {
328 unsigned long index;
329 unsigned long offset;
330 size_t copied;
Nick Piggin70688e42008-04-28 02:13:02 -0700331 void *xip_mem;
332 unsigned long xip_pfn;
Carsten Otteceffc072005-06-23 22:05:25 -0700333
334 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
335 index = pos >> PAGE_CACHE_SHIFT;
336 bytes = PAGE_CACHE_SIZE - offset;
337 if (bytes > count)
338 bytes = count;
339
Nick Piggin70688e42008-04-28 02:13:02 -0700340 status = a_ops->get_xip_mem(mapping, index, 0,
341 &xip_mem, &xip_pfn);
342 if (status == -ENODATA) {
Carsten Otteceffc072005-06-23 22:05:25 -0700343 /* we allocate a new page unmap it */
Nick Piggin14bac5a2008-08-20 14:09:20 -0700344 mutex_lock(&xip_sparse_mutex);
Nick Piggin70688e42008-04-28 02:13:02 -0700345 status = a_ops->get_xip_mem(mapping, index, 1,
346 &xip_mem, &xip_pfn);
Nick Piggin14bac5a2008-08-20 14:09:20 -0700347 mutex_unlock(&xip_sparse_mutex);
Nick Piggin70688e42008-04-28 02:13:02 -0700348 if (!status)
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700349 /* unmap page at pgoff from all other vmas */
350 __xip_unmap(mapping, index);
Carsten Otteceffc072005-06-23 22:05:25 -0700351 }
352
Nick Piggin70688e42008-04-28 02:13:02 -0700353 if (status)
Carsten Otteceffc072005-06-23 22:05:25 -0700354 break;
Carsten Otteceffc072005-06-23 22:05:25 -0700355
Nick Piggin4a9e5ef2007-10-16 01:24:58 -0700356 copied = bytes -
Nick Piggin70688e42008-04-28 02:13:02 -0700357 __copy_from_user_nocache(xip_mem + offset, buf, bytes);
Nick Piggin4a9e5ef2007-10-16 01:24:58 -0700358
Carsten Otteceffc072005-06-23 22:05:25 -0700359 if (likely(copied > 0)) {
360 status = copied;
361
362 if (status >= 0) {
363 written += status;
364 count -= status;
365 pos += status;
366 buf += status;
Carsten Otteceffc072005-06-23 22:05:25 -0700367 }
368 }
369 if (unlikely(copied != bytes))
370 if (status >= 0)
371 status = -EFAULT;
372 if (status < 0)
373 break;
374 } while (count);
375 *ppos = pos;
376 /*
377 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800378 * cannot change under us because we hold i_mutex.
Carsten Otteceffc072005-06-23 22:05:25 -0700379 */
380 if (pos > inode->i_size) {
381 i_size_write(inode, pos);
382 mark_inode_dirty(inode);
383 }
384
385 return written ? written : status;
386}
387
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700388ssize_t
389xip_file_write(struct file *filp, const char __user *buf, size_t len,
390 loff_t *ppos)
Carsten Otteceffc072005-06-23 22:05:25 -0700391{
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700392 struct address_space *mapping = filp->f_mapping;
393 struct inode *inode = mapping->host;
394 size_t count;
395 loff_t pos;
396 ssize_t ret;
Carsten Otteceffc072005-06-23 22:05:25 -0700397
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800398 mutex_lock(&inode->i_mutex);
Carsten Otteceffc072005-06-23 22:05:25 -0700399
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700400 if (!access_ok(VERIFY_READ, buf, len)) {
401 ret=-EFAULT;
402 goto out_up;
Carsten Otteceffc072005-06-23 22:05:25 -0700403 }
404
Carsten Otteceffc072005-06-23 22:05:25 -0700405 pos = *ppos;
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700406 count = len;
Carsten Otteceffc072005-06-23 22:05:25 -0700407
408 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
409
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700410 /* We can write back this queue in page reclaim */
411 current->backing_dev_info = mapping->backing_dev_info;
Carsten Otteceffc072005-06-23 22:05:25 -0700412
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700413 ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
414 if (ret)
415 goto out_backing;
Carsten Otteceffc072005-06-23 22:05:25 -0700416 if (count == 0)
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700417 goto out_backing;
Carsten Otteceffc072005-06-23 22:05:25 -0700418
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200419 ret = file_remove_suid(filp);
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700420 if (ret)
421 goto out_backing;
Carsten Otteceffc072005-06-23 22:05:25 -0700422
Christoph Hellwig870f4812006-01-09 20:52:01 -0800423 file_update_time(filp);
Carsten Otteceffc072005-06-23 22:05:25 -0700424
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700425 ret = __xip_file_write (filp, buf, count, pos, ppos);
Carsten Otteceffc072005-06-23 22:05:25 -0700426
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700427 out_backing:
428 current->backing_dev_info = NULL;
429 out_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800430 mutex_unlock(&inode->i_mutex);
Carsten Otteceffc072005-06-23 22:05:25 -0700431 return ret;
432}
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700433EXPORT_SYMBOL_GPL(xip_file_write);
Carsten Otteceffc072005-06-23 22:05:25 -0700434
435/*
436 * truncate a page used for execute in place
Nick Piggin70688e42008-04-28 02:13:02 -0700437 * functionality is analog to block_truncate_page but does use get_xip_mem
Carsten Otteceffc072005-06-23 22:05:25 -0700438 * to get the page instead of page cache
439 */
440int
441xip_truncate_page(struct address_space *mapping, loff_t from)
442{
443 pgoff_t index = from >> PAGE_CACHE_SHIFT;
444 unsigned offset = from & (PAGE_CACHE_SIZE-1);
445 unsigned blocksize;
446 unsigned length;
Nick Piggin70688e42008-04-28 02:13:02 -0700447 void *xip_mem;
448 unsigned long xip_pfn;
449 int err;
Carsten Otteceffc072005-06-23 22:05:25 -0700450
Nick Piggin70688e42008-04-28 02:13:02 -0700451 BUG_ON(!mapping->a_ops->get_xip_mem);
Carsten Otteceffc072005-06-23 22:05:25 -0700452
453 blocksize = 1 << mapping->host->i_blkbits;
454 length = offset & (blocksize - 1);
455
456 /* Block boundary? Nothing to do */
457 if (!length)
458 return 0;
459
460 length = blocksize - length;
461
Nick Piggin70688e42008-04-28 02:13:02 -0700462 err = mapping->a_ops->get_xip_mem(mapping, index, 0,
463 &xip_mem, &xip_pfn);
464 if (unlikely(err)) {
465 if (err == -ENODATA)
Carsten Otteceffc072005-06-23 22:05:25 -0700466 /* Hole? No need to truncate */
467 return 0;
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700468 else
Nick Piggin70688e42008-04-28 02:13:02 -0700469 return err;
Carsten Otteafa597b2005-07-15 03:56:30 -0700470 }
Nick Piggin70688e42008-04-28 02:13:02 -0700471 memset(xip_mem + offset, 0, length);
Carsten Otteeb6fe0c2005-06-23 22:05:28 -0700472 return 0;
Carsten Otteceffc072005-06-23 22:05:25 -0700473}
474EXPORT_SYMBOL_GPL(xip_truncate_page);