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