]> err.no Git - linux-2.6/blobdiff - fs/hugetlbfs/inode.c
[PATCH] nfsd4: recovery lookup dir check
[linux-2.6] / fs / hugetlbfs / inode.c
index 8f94feb24c0a4621f85ad7d4d0f5469025be7615..f568102da1e8df3be5126e91a50914d55dc5c0bb 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/highmem.h>
 #include <linux/init.h>
 #include <linux/string.h>
+#include <linux/capability.h>
 #include <linux/backing-dev.h>
 #include <linux/hugetlb.h>
 #include <linux/pagevec.h>
@@ -45,16 +46,61 @@ static struct backing_dev_info hugetlbfs_backing_dev_info = {
 
 int sysctl_hugetlb_shm_group;
 
+static void huge_pagevec_release(struct pagevec *pvec)
+{
+       int i;
+
+       for (i = 0; i < pagevec_count(pvec); ++i)
+               put_page(pvec->pages[i]);
+
+       pagevec_reinit(pvec);
+}
+
+/*
+ * huge_pages_needed tries to determine the number of new huge pages that
+ * will be required to fully populate this VMA.  This will be equal to
+ * the size of the VMA in huge pages minus the number of huge pages
+ * (covered by this VMA) that are found in the page cache.
+ *
+ * Result is in bytes to be compatible with is_hugepage_mem_enough()
+ */
+static unsigned long
+huge_pages_needed(struct address_space *mapping, struct vm_area_struct *vma)
+{
+       int i;
+       struct pagevec pvec;
+       unsigned long start = vma->vm_start;
+       unsigned long end = vma->vm_end;
+       unsigned long hugepages = (end - start) >> HPAGE_SHIFT;
+       pgoff_t next = vma->vm_pgoff;
+       pgoff_t endpg = next + ((end - start) >> PAGE_SHIFT);
+
+       pagevec_init(&pvec, 0);
+       while (next < endpg) {
+               if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
+                       break;
+               for (i = 0; i < pagevec_count(&pvec); i++) {
+                       struct page *page = pvec.pages[i];
+                       if (page->index > next)
+                               next = page->index;
+                       if (page->index >= endpg)
+                               break;
+                       next++;
+                       hugepages--;
+               }
+               huge_pagevec_release(&pvec);
+       }
+       return hugepages << HPAGE_SHIFT;
+}
+
 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
        struct inode *inode = file->f_dentry->d_inode;
        struct address_space *mapping = inode->i_mapping;
+       unsigned long bytes;
        loff_t len, vma_len;
        int ret;
 
-       if ((vma->vm_flags & (VM_MAYSHARE | VM_WRITE)) == VM_WRITE)
-               return -EINVAL;
-
        if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
                return -EINVAL;
 
@@ -67,9 +113,13 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
        if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
                return -EINVAL;
 
+       bytes = huge_pages_needed(mapping, vma);
+       if (!is_hugepage_mem_enough(bytes))
+               return -ENOMEM;
+
        vma_len = (loff_t)(vma->vm_end - vma->vm_start);
 
-       down(&inode->i_sem);
+       mutex_lock(&inode->i_mutex);
        file_accessed(file);
        vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
        vma->vm_ops = &hugetlb_vm_ops;
@@ -79,14 +129,12 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
        if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
                goto out;
 
-       ret = hugetlb_prefault(mapping, vma);
-       if (ret)
-               goto out;
-
+       ret = 0;
+       hugetlb_prefault_arch_hook(vma->vm_mm);
        if (inode->i_size < len)
                inode->i_size = len;
 out:
-       up(&inode->i_sem);
+       mutex_unlock(&inode->i_mutex);
 
        return ret;
 }
@@ -171,16 +219,6 @@ static int hugetlbfs_commit_write(struct file *file,
        return -EINVAL;
 }
 
-static void huge_pagevec_release(struct pagevec *pvec)
-{
-       int i;
-
-       for (i = 0; i < pagevec_count(pvec); ++i)
-               put_page(pvec->pages[i]);
-
-       pagevec_reinit(pvec);
-}
-
 static void truncate_huge_page(struct page *page)
 {
        clear_page_dirty(page);
@@ -364,7 +402,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
                inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
                inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
                info = HUGETLBFS_I(inode);
-               mpol_shared_policy_init(&info->policy);
+               mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
                switch (mode & S_IFMT) {
                default:
                        init_special_inode(inode, mode, dev);
@@ -472,10 +510,14 @@ static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
        buf->f_bsize = HPAGE_SIZE;
        if (sbinfo) {
                spin_lock(&sbinfo->stat_lock);
-               buf->f_blocks = sbinfo->max_blocks;
-               buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
-               buf->f_files = sbinfo->max_inodes;
-               buf->f_ffree = sbinfo->free_inodes;
+               /* If no limits set, just report 0 for max/free/used
+                * blocks, like simple_statfs() */
+               if (sbinfo->max_blocks >= 0) {
+                       buf->f_blocks = sbinfo->max_blocks;
+                       buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
+                       buf->f_files = sbinfo->max_inodes;
+                       buf->f_ffree = sbinfo->free_inodes;
+               }
                spin_unlock(&sbinfo->stat_lock);
        }
        buf->f_namelen = NAME_MAX;