Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / hugetlbfs / inode.c
index 2af3338..3a5b4e9 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>
@@ -34,7 +35,7 @@
 
 static struct super_operations hugetlbfs_ops;
 static struct address_space_operations hugetlbfs_aops;
-struct file_operations hugetlbfs_file_operations;
+const struct file_operations hugetlbfs_file_operations;
 static struct inode_operations hugetlbfs_dir_inode_operations;
 static struct inode_operations hugetlbfs_inode_operations;
 
@@ -45,16 +46,23 @@ 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);
+}
+
 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;
+       struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
        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;
 
@@ -69,7 +77,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 
        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,20 +87,22 @@ 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;
+       if (vma->vm_flags & VM_MAYSHARE)
+               if (hugetlb_extend_reservation(info, len >> HPAGE_SHIFT) != 0)
+                       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;
 }
 
 /*
- * Called under down_write(mmap_sem), page_table_lock is not held
+ * Called under down_write(mmap_sem).
  */
 
 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
@@ -122,6 +132,9 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 
        start_addr = mm->free_area_cache;
 
+       if (len <= mm->cached_hole_size)
+               start_addr = TASK_UNMAPPED_BASE;
+
 full_search:
        addr = ALIGN(start_addr, HPAGE_SIZE);
 
@@ -168,16 +181,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);
@@ -186,13 +189,18 @@ static void truncate_huge_page(struct page *page)
        put_page(page);
 }
 
-static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
+static void truncate_hugepages(struct inode *inode, loff_t lstart)
 {
+       struct address_space *mapping = &inode->i_data;
        const pgoff_t start = lstart >> HPAGE_SHIFT;
        struct pagevec pvec;
        pgoff_t next;
        int i;
 
+       hugetlb_truncate_reservation(HUGETLBFS_I(inode),
+                                    lstart >> HPAGE_SHIFT);
+       if (!mapping->nrpages)
+               return;
        pagevec_init(&pvec, 0);
        next = start;
        while (1) {
@@ -221,66 +229,40 @@ static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
 
 static void hugetlbfs_delete_inode(struct inode *inode)
 {
-       struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
-
-       hlist_del_init(&inode->i_hash);
-       list_del_init(&inode->i_list);
-       list_del_init(&inode->i_sb_list);
-       inode->i_state |= I_FREEING;
-       inodes_stat.nr_inodes--;
-       spin_unlock(&inode_lock);
-
-       if (inode->i_data.nrpages)
-               truncate_hugepages(&inode->i_data, 0);
-
-       security_inode_delete(inode);
-
-       if (sbinfo->free_inodes >= 0) {
-               spin_lock(&sbinfo->stat_lock);
-               sbinfo->free_inodes++;
-               spin_unlock(&sbinfo->stat_lock);
-       }
-
+       truncate_hugepages(inode, 0);
        clear_inode(inode);
-       destroy_inode(inode);
 }
 
 static void hugetlbfs_forget_inode(struct inode *inode)
 {
-       struct super_block *super_block = inode->i_sb;
-       struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
-
-       if (hlist_unhashed(&inode->i_hash))
-               goto out_truncate;
+       struct super_block *sb = inode->i_sb;
 
-       if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
-               list_del(&inode->i_list);
-               list_add(&inode->i_list, &inode_unused);
-       }
-       inodes_stat.nr_unused++;
-       if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
+       if (!hlist_unhashed(&inode->i_hash)) {
+               if (!(inode->i_state & (I_DIRTY|I_LOCK)))
+                       list_move(&inode->i_list, &inode_unused);
+               inodes_stat.nr_unused++;
+               if (!sb || (sb->s_flags & MS_ACTIVE)) {
+                       spin_unlock(&inode_lock);
+                       return;
+               }
+               inode->i_state |= I_WILL_FREE;
                spin_unlock(&inode_lock);
-               return;
+               /*
+                * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
+                * in our backing_dev_info.
+                */
+               write_inode_now(inode, 1);
+               spin_lock(&inode_lock);
+               inode->i_state &= ~I_WILL_FREE;
+               inodes_stat.nr_unused--;
+               hlist_del_init(&inode->i_hash);
        }
-
-       /* write_inode_now() ? */
-       inodes_stat.nr_unused--;
-       hlist_del_init(&inode->i_hash);
-out_truncate:
        list_del_init(&inode->i_list);
        list_del_init(&inode->i_sb_list);
        inode->i_state |= I_FREEING;
        inodes_stat.nr_inodes--;
        spin_unlock(&inode_lock);
-       if (inode->i_data.nrpages)
-               truncate_hugepages(&inode->i_data, 0);
-
-       if (sbinfo->free_inodes >= 0) {
-               spin_lock(&sbinfo->stat_lock);
-               sbinfo->free_inodes++;
-               spin_unlock(&sbinfo->stat_lock);
-       }
-
+       truncate_hugepages(inode, 0);
        clear_inode(inode);
        destroy_inode(inode);
 }
@@ -288,7 +270,7 @@ out_truncate:
 static void hugetlbfs_drop_inode(struct inode *inode)
 {
        if (!inode->i_nlink)
-               hugetlbfs_delete_inode(inode);
+               generic_delete_inode(inode);
        else
                hugetlbfs_forget_inode(inode);
 }
@@ -305,7 +287,6 @@ hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
 
        vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
                unsigned long h_vm_pgoff;
-               unsigned long v_length;
                unsigned long v_offset;
 
                h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
@@ -316,11 +297,8 @@ hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
                if (h_vm_pgoff >= h_pgoff)
                        v_offset = 0;
 
-               v_length = vma->vm_end - vma->vm_start;
-
-               zap_hugepage_range(vma,
-                               vma->vm_start + v_offset,
-                               v_length - v_offset);
+               unmap_hugepage_range(vma,
+                               vma->vm_start + v_offset, vma->vm_end);
        }
 }
 
@@ -343,7 +321,7 @@ static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
        if (!prio_tree_empty(&mapping->i_mmap))
                hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
        spin_unlock(&mapping->i_mmap_lock);
-       truncate_hugepages(mapping, offset);
+       truncate_hugepages(inode, offset);
        return 0;
 }
 
@@ -376,17 +354,6 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
                                        gid_t gid, int mode, dev_t dev)
 {
        struct inode *inode;
-       struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
-
-       if (sbinfo->free_inodes >= 0) {
-               spin_lock(&sbinfo->stat_lock);
-               if (!sbinfo->free_inodes) {
-                       spin_unlock(&sbinfo->stat_lock);
-                       return NULL;
-               }
-               sbinfo->free_inodes--;
-               spin_unlock(&sbinfo->stat_lock);
-       }
 
        inode = new_inode(sb);
        if (inode) {
@@ -400,7 +367,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);
@@ -508,10 +475,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;
@@ -528,29 +499,52 @@ static void hugetlbfs_put_super(struct super_block *sb)
        }
 }
 
+static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
+{
+       if (sbinfo->free_inodes >= 0) {
+               spin_lock(&sbinfo->stat_lock);
+               if (unlikely(!sbinfo->free_inodes)) {
+                       spin_unlock(&sbinfo->stat_lock);
+                       return 0;
+               }
+               sbinfo->free_inodes--;
+               spin_unlock(&sbinfo->stat_lock);
+       }
+
+       return 1;
+}
+
+static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
+{
+       if (sbinfo->free_inodes >= 0) {
+               spin_lock(&sbinfo->stat_lock);
+               sbinfo->free_inodes++;
+               spin_unlock(&sbinfo->stat_lock);
+       }
+}
+
+
 static kmem_cache_t *hugetlbfs_inode_cachep;
 
 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
 {
+       struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
        struct hugetlbfs_inode_info *p;
 
+       if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
+               return NULL;
        p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
-       if (!p)
+       if (unlikely(!p)) {
+               hugetlbfs_inc_free_inodes(sbinfo);
                return NULL;
+       }
+       p->prereserved_hpages = 0;
        return &p->vfs_inode;
 }
 
-static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
-{
-       struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
-
-       if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
-           SLAB_CTOR_CONSTRUCTOR)
-               inode_init_once(&ei->vfs_inode);
-}
-
 static void hugetlbfs_destroy_inode(struct inode *inode)
 {
+       hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
        mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
        kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
 }
@@ -562,7 +556,17 @@ static struct address_space_operations hugetlbfs_aops = {
        .set_page_dirty = hugetlbfs_set_page_dirty,
 };
 
-struct file_operations hugetlbfs_file_operations = {
+
+static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
+{
+       struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
+
+       if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
+           SLAB_CTOR_CONSTRUCTOR)
+               inode_init_once(&ei->vfs_inode);
+}
+
+const struct file_operations hugetlbfs_file_operations = {
        .mmap                   = hugetlbfs_file_mmap,
        .fsync                  = simple_sync_file,
        .get_unmapped_area      = hugetlb_get_unmapped_area,
@@ -589,6 +593,7 @@ static struct super_operations hugetlbfs_ops = {
        .alloc_inode    = hugetlbfs_alloc_inode,
        .destroy_inode  = hugetlbfs_destroy_inode,
        .statfs         = hugetlbfs_statfs,
+       .delete_inode   = hugetlbfs_delete_inode,
        .drop_inode     = hugetlbfs_drop_inode,
        .put_super      = hugetlbfs_put_super,
 };
@@ -732,21 +737,6 @@ static struct file_system_type hugetlbfs_fs_type = {
 
 static struct vfsmount *hugetlbfs_vfsmount;
 
-/*
- * Return the next identifier for a shm file
- */
-static unsigned long hugetlbfs_counter(void)
-{
-       static DEFINE_SPINLOCK(lock);
-       static unsigned long counter;
-       unsigned long ret;
-
-       spin_lock(&lock);
-       ret = ++counter;
-       spin_unlock(&lock);
-       return ret;
-}
-
 static int can_do_hugetlb_shm(void)
 {
        return likely(capable(CAP_IPC_LOCK) ||
@@ -762,18 +752,16 @@ struct file *hugetlb_zero_setup(size_t size)
        struct dentry *dentry, *root;
        struct qstr quick_string;
        char buf[16];
+       static atomic_t counter;
 
        if (!can_do_hugetlb_shm())
                return ERR_PTR(-EPERM);
 
-       if (!is_hugepage_mem_enough(size))
-               return ERR_PTR(-ENOMEM);
-
        if (!user_shm_lock(size, current->user))
                return ERR_PTR(-ENOMEM);
 
        root = hugetlbfs_vfsmount->mnt_root;
-       snprintf(buf, 16, "%lu", hugetlbfs_counter());
+       snprintf(buf, 16, "%u", atomic_inc_return(&counter));
        quick_string.name = buf;
        quick_string.len = strlen(quick_string.name);
        quick_string.hash = 0;
@@ -792,6 +780,11 @@ struct file *hugetlb_zero_setup(size_t size)
        if (!inode)
                goto out_file;
 
+       error = -ENOMEM;
+       if (hugetlb_extend_reservation(HUGETLBFS_I(inode),
+                                      size >> HPAGE_SHIFT) != 0)
+               goto out_inode;
+
        d_instantiate(dentry, inode);
        inode->i_size = size;
        inode->i_nlink = 0;
@@ -802,6 +795,8 @@ struct file *hugetlb_zero_setup(size_t size)
        file->f_mode = FMODE_WRITE | FMODE_READ;
        return file;
 
+out_inode:
+       iput(inode);
 out_file:
        put_filp(file);
 out_dentry: