nfsd4: fail when delegreturn gets a non-delegation stateid
[safe/jmp/linux-2.6] / mm / mincore.c
index b44d7f8..8cb508f 100644 (file)
@@ -12,6 +12,8 @@
 #include <linux/mm.h>
 #include <linux/mman.h>
 #include <linux/syscalls.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
 
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
  * and is up to date; i.e. that no page-in operation would be required
  * at this time if an application were to map and access this page.
  */
-static unsigned char mincore_page(struct vm_area_struct * vma,
-       unsigned long pgoff)
+static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
 {
        unsigned char present = 0;
-       struct address_space * as = vma->vm_file->f_mapping;
-       struct page * page;
+       struct page *page;
 
-       page = find_get_page(as, pgoff);
+       /*
+        * When tmpfs swaps out a page from a file, any process mapping that
+        * file will not get a swp_entry_t in its pte, but rather it is like
+        * any other file mapping (ie. marked !present and faulted in with
+        * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
+        *
+        * However when tmpfs moves the page from pagecache and into swapcache,
+        * it is still in core, but the find_get_page below won't find it.
+        * No big deal, but make a note of it.
+        */
+       page = find_get_page(mapping, pgoff);
        if (page) {
                present = PageUptodate(page);
                page_cache_release(page);
@@ -45,51 +55,100 @@ static unsigned char mincore_page(struct vm_area_struct * vma,
  */
 static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pages)
 {
-       unsigned long i, nr, pgoff;
+       pgd_t *pgd;
+       pud_t *pud;
+       pmd_t *pmd;
+       pte_t *ptep;
+       spinlock_t *ptl;
+       unsigned long nr;
+       int i;
+       pgoff_t pgoff;
        struct vm_area_struct *vma = find_vma(current->mm, addr);
 
        /*
-        * find_vma() didn't find anything: the address
-        * is above everything we have mapped.
+        * find_vma() didn't find anything above us, or we're
+        * in an unmapped hole in the address space: ENOMEM.
         */
-       if (!vma) {
-               memset(vec, 0, pages);
-               return pages;
-       }
+       if (!vma || addr < vma->vm_start)
+               return -ENOMEM;
 
        /*
-        * find_vma() found something, but we might be
-        * below it: check for that.
+        * Calculate how many pages there are left in the last level of the
+        * PTE array for our address.
         */
-       if (addr < vma->vm_start) {
-               unsigned long gap = (vma->vm_start - addr) >> PAGE_SHIFT;
-               if (gap > pages)
-                       gap = pages;
-               memset(vec, 0, gap);
-               return gap;
-       }
+       nr = PTRS_PER_PTE - ((addr >> PAGE_SHIFT) & (PTRS_PER_PTE-1));
 
        /*
-        * Ok, got it. But check whether it's a segment we support
-        * mincore() on. Right now, we don't do any anonymous mappings.
+        * Don't overrun this vma
         */
-       if (!vma->vm_file)
-               return -ENOMEM;
+       nr = min(nr, (vma->vm_end - addr) >> PAGE_SHIFT);
 
        /*
-        * Calculate how many pages there are left in the vma, and
-        * what the pgoff is for our address.
+        * Don't return more than the caller asked for
         */
-       nr = (vma->vm_end - addr) >> PAGE_SHIFT;
-       if (nr > pages)
-               nr = pages;
+       nr = min(nr, pages);
+
+       pgd = pgd_offset(vma->vm_mm, addr);
+       if (pgd_none_or_clear_bad(pgd))
+               goto none_mapped;
+       pud = pud_offset(pgd, addr);
+       if (pud_none_or_clear_bad(pud))
+               goto none_mapped;
+       pmd = pmd_offset(pud, addr);
+       if (pmd_none_or_clear_bad(pmd))
+               goto none_mapped;
+
+       ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
+       for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
+               unsigned char present;
+               pte_t pte = *ptep;
+
+               if (pte_present(pte)) {
+                       present = 1;
+
+               } else if (pte_none(pte)) {
+                       if (vma->vm_file) {
+                               pgoff = linear_page_index(vma, addr);
+                               present = mincore_page(vma->vm_file->f_mapping,
+                                                       pgoff);
+                       } else
+                               present = 0;
+
+               } else if (pte_file(pte)) {
+                       pgoff = pte_to_pgoff(pte);
+                       present = mincore_page(vma->vm_file->f_mapping, pgoff);
+
+               } else { /* pte is a swap entry */
+                       swp_entry_t entry = pte_to_swp_entry(pte);
+                       if (is_migration_entry(entry)) {
+                               /* migration entries are always uptodate */
+                               present = 1;
+                       } else {
+#ifdef CONFIG_SWAP
+                               pgoff = entry.val;
+                               present = mincore_page(&swapper_space, pgoff);
+#else
+                               WARN_ON(1);
+                               present = 1;
+#endif
+                       }
+               }
+
+               vec[i] = present;
+       }
+       pte_unmap_unlock(ptep-1, ptl);
 
-       pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
-       pgoff += vma->vm_pgoff;
+       return nr;
 
-       /* And then we just fill the sucker in.. */
-       for (i = 0 ; i < nr; i++, pgoff++)
-               vec[i] = mincore_page(vma, pgoff);
+none_mapped:
+       if (vma->vm_file) {
+               pgoff = linear_page_index(vma, addr);
+               for (i = 0; i < nr; i++, pgoff++)
+                       vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
+       } else {
+               for (i = 0; i < nr; i++)
+                       vec[i] = 0;
+       }
 
        return nr;
 }
@@ -118,8 +177,8 @@ static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pag
  *             mapped
  *  -EAGAIN - A kernel resource was temporarily unavailable.
  */
-asmlinkage long sys_mincore(unsigned long start, size_t len,
-       unsigned char __user * vec)
+SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
+               unsigned char __user *, vec)
 {
        long retval;
        unsigned long pages;
@@ -142,7 +201,7 @@ asmlinkage long sys_mincore(unsigned long start, size_t len,
 
        tmp = (void *) __get_free_page(GFP_USER);
        if (!tmp)
-               return -ENOMEM;
+               return -EAGAIN;
 
        retval = 0;
        while (pages) {
@@ -151,7 +210,7 @@ asmlinkage long sys_mincore(unsigned long start, size_t len,
                 * the temporary buffer size.
                 */
                down_read(&current->mm->mmap_sem);
-               retval = do_mincore(start, tmp, max(pages, PAGE_SIZE));
+               retval = do_mincore(start, tmp, min(pages, PAGE_SIZE));
                up_read(&current->mm->mmap_sem);
 
                if (retval <= 0)