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