Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / mm / shmem.c
index 2d35772..f99ff3e 100644 (file)
@@ -28,7 +28,7 @@
 #include <linux/pagemap.h>
 #include <linux/file.h>
 #include <linux/mm.h>
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/swap.h>
 
 static struct vfsmount *shm_mnt;
@@ -127,7 +127,7 @@ static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
 static inline int shmem_acct_size(unsigned long flags, loff_t size)
 {
        return (flags & VM_NORESERVE) ?
-               0 : security_vm_enough_memory_kern(VM_ACCT(size));
+               0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
 }
 
 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
@@ -145,7 +145,7 @@ static inline void shmem_unacct_size(unsigned long flags, loff_t size)
 static inline int shmem_acct_block(unsigned long flags)
 {
        return (flags & VM_NORESERVE) ?
-               security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
+               security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
 }
 
 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
@@ -379,7 +379,7 @@ static int shmem_free_swap(struct address_space *mapping,
 /*
  * Pagevec may contain swap entries, so shuffle up pages before releasing.
  */
-static void shmem_pagevec_release(struct pagevec *pvec)
+static void shmem_deswap_pagevec(struct pagevec *pvec)
 {
        int i, j;
 
@@ -389,7 +389,36 @@ static void shmem_pagevec_release(struct pagevec *pvec)
                        pvec->pages[j++] = page;
        }
        pvec->nr = j;
-       pagevec_release(pvec);
+}
+
+/*
+ * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
+ */
+void shmem_unlock_mapping(struct address_space *mapping)
+{
+       struct pagevec pvec;
+       pgoff_t indices[PAGEVEC_SIZE];
+       pgoff_t index = 0;
+
+       pagevec_init(&pvec, 0);
+       /*
+        * Minor point, but we might as well stop if someone else SHM_LOCKs it.
+        */
+       while (!mapping_unevictable(mapping)) {
+               /*
+                * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
+                * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
+                */
+               pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
+                                       PAGEVEC_SIZE, pvec.pages, indices);
+               if (!pvec.nr)
+                       break;
+               index = indices[pvec.nr - 1] + 1;
+               shmem_deswap_pagevec(&pvec);
+               check_move_unevictable_pages(pvec.pages, pvec.nr);
+               pagevec_release(&pvec);
+               cond_resched();
+       }
 }
 
 /*
@@ -440,7 +469,8 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
                        }
                        unlock_page(page);
                }
-               shmem_pagevec_release(&pvec);
+               shmem_deswap_pagevec(&pvec);
+               pagevec_release(&pvec);
                mem_cgroup_uncharge_end();
                cond_resched();
                index++;
@@ -470,7 +500,8 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
                        continue;
                }
                if (index == start && indices[0] > end) {
-                       shmem_pagevec_release(&pvec);
+                       shmem_deswap_pagevec(&pvec);
+                       pagevec_release(&pvec);
                        break;
                }
                mem_cgroup_uncharge_start();
@@ -494,7 +525,8 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
                        }
                        unlock_page(page);
                }
-               shmem_pagevec_release(&pvec);
+               shmem_deswap_pagevec(&pvec);
+               pagevec_release(&pvec);
                mem_cgroup_uncharge_end();
                index++;
        }
@@ -1068,7 +1100,6 @@ int shmem_lock(struct file *file, int lock, struct user_struct *user)
                user_shm_unlock(inode->i_size, user);
                info->flags &= ~VM_LOCKED;
                mapping_clear_unevictable(file->f_mapping);
-               scan_mapping_unevictable_pages(file->f_mapping);
        }
        retval = 0;
 
@@ -1086,7 +1117,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
 }
 
 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
-                                    int mode, dev_t dev, unsigned long flags)
+                                    umode_t mode, dev_t dev, unsigned long flags)
 {
        struct inode *inode;
        struct shmem_inode_info *info;
@@ -1147,6 +1178,12 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
 static const struct inode_operations shmem_symlink_inode_operations;
 static const struct inode_operations shmem_short_symlink_operations;
 
+#ifdef CONFIG_TMPFS_XATTR
+static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
+#else
+#define shmem_initxattrs NULL
+#endif
+
 static int
 shmem_write_begin(struct file *file, struct address_space *mapping,
                        loff_t pos, unsigned len, unsigned flags,
@@ -1450,7 +1487,7 @@ static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  * File creation. Allocate an inode, and we're done..
  */
 static int
-shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
+shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
 {
        struct inode *inode;
        int error = -ENOSPC;
@@ -1459,7 +1496,7 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
        if (inode) {
                error = security_inode_init_security(inode, dir,
                                                     &dentry->d_name,
-                                                    NULL, NULL);
+                                                    shmem_initxattrs, NULL);
                if (error) {
                        if (error != -EOPNOTSUPP) {
                                iput(inode);
@@ -1483,7 +1520,7 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
        return error;
 }
 
-static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
+static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
 {
        int error;
 
@@ -1493,7 +1530,7 @@ static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        return 0;
 }
 
-static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
+static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
                struct nameidata *nd)
 {
        return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
@@ -1599,7 +1636,7 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
                return -ENOSPC;
 
        error = security_inode_init_security(inode, dir, &dentry->d_name,
-                                            NULL, NULL);
+                                            shmem_initxattrs, NULL);
        if (error) {
                if (error != -EOPNOTSUPP) {
                        iput(inode);
@@ -1625,9 +1662,9 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
                }
                inode->i_mapping->a_ops = &shmem_aops;
                inode->i_op = &shmem_symlink_inode_operations;
-               kaddr = kmap_atomic(page, KM_USER0);
+               kaddr = kmap_atomic(page);
                memcpy(kaddr, symname, len);
-               kunmap_atomic(kaddr, KM_USER0);
+               kunmap_atomic(kaddr);
                set_page_dirty(page);
                unlock_page(page);
                page_cache_release(page);
@@ -1673,6 +1710,66 @@ static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *co
  * filesystem level, though.
  */
 
+/*
+ * Allocate new xattr and copy in the value; but leave the name to callers.
+ */
+static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
+{
+       struct shmem_xattr *new_xattr;
+       size_t len;
+
+       /* wrap around? */
+       len = sizeof(*new_xattr) + size;
+       if (len <= sizeof(*new_xattr))
+               return NULL;
+
+       new_xattr = kmalloc(len, GFP_KERNEL);
+       if (!new_xattr)
+               return NULL;
+
+       new_xattr->size = size;
+       memcpy(new_xattr->value, value, size);
+       return new_xattr;
+}
+
+/*
+ * Callback for security_inode_init_security() for acquiring xattrs.
+ */
+static int shmem_initxattrs(struct inode *inode,
+                           const struct xattr *xattr_array,
+                           void *fs_info)
+{
+       struct shmem_inode_info *info = SHMEM_I(inode);
+       const struct xattr *xattr;
+       struct shmem_xattr *new_xattr;
+       size_t len;
+
+       for (xattr = xattr_array; xattr->name != NULL; xattr++) {
+               new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
+               if (!new_xattr)
+                       return -ENOMEM;
+
+               len = strlen(xattr->name) + 1;
+               new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
+                                         GFP_KERNEL);
+               if (!new_xattr->name) {
+                       kfree(new_xattr);
+                       return -ENOMEM;
+               }
+
+               memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
+                      XATTR_SECURITY_PREFIX_LEN);
+               memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
+                      xattr->name, len);
+
+               spin_lock(&info->lock);
+               list_add(&new_xattr->list, &info->xattr_list);
+               spin_unlock(&info->lock);
+       }
+
+       return 0;
+}
+
 static int shmem_xattr_get(struct dentry *dentry, const char *name,
                           void *buffer, size_t size)
 {
@@ -1700,24 +1797,17 @@ static int shmem_xattr_get(struct dentry *dentry, const char *name,
        return ret;
 }
 
-static int shmem_xattr_set(struct dentry *dentry, const char *name,
+static int shmem_xattr_set(struct inode *inode, const char *name,
                           const void *value, size_t size, int flags)
 {
-       struct inode *inode = dentry->d_inode;
        struct shmem_inode_info *info = SHMEM_I(inode);
        struct shmem_xattr *xattr;
        struct shmem_xattr *new_xattr = NULL;
-       size_t len;
        int err = 0;
 
        /* value == NULL means remove */
        if (value) {
-               /* wrap around? */
-               len = sizeof(*new_xattr) + size;
-               if (len <= sizeof(*new_xattr))
-                       return -ENOMEM;
-
-               new_xattr = kmalloc(len, GFP_KERNEL);
+               new_xattr = shmem_xattr_alloc(value, size);
                if (!new_xattr)
                        return -ENOMEM;
 
@@ -1726,9 +1816,6 @@ static int shmem_xattr_set(struct dentry *dentry, const char *name,
                        kfree(new_xattr);
                        return -ENOMEM;
                }
-
-               new_xattr->size = size;
-               memcpy(new_xattr->value, value, size);
        }
 
        spin_lock(&info->lock);
@@ -1827,7 +1914,7 @@ static int shmem_setxattr(struct dentry *dentry, const char *name,
        if (size == 0)
                value = "";  /* empty EA, do not remove */
 
-       return shmem_xattr_set(dentry, name, value, size, flags);
+       return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
 
 }
 
@@ -1847,7 +1934,7 @@ static int shmem_removexattr(struct dentry *dentry, const char *name)
        if (err)
                return err;
 
-       return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
+       return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
 }
 
 static bool xattr_is_trusted(const char *name)
@@ -2112,9 +2199,9 @@ out:
        return error;
 }
 
-static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
+static int shmem_show_options(struct seq_file *seq, struct dentry *root)
 {
-       struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
+       struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
 
        if (sbinfo->max_blocks != shmem_default_max_blocks())
                seq_printf(seq, ",size=%luk",
@@ -2122,7 +2209,7 @@ static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
        if (sbinfo->max_inodes != shmem_default_max_inodes())
                seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
        if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
-               seq_printf(seq, ",mode=%03o", sbinfo->mode);
+               seq_printf(seq, ",mode=%03ho", sbinfo->mode);
        if (sbinfo->uid != 0)
                seq_printf(seq, ",uid=%u", sbinfo->uid);
        if (sbinfo->gid != 0)
@@ -2144,7 +2231,6 @@ static void shmem_put_super(struct super_block *sb)
 int shmem_fill_super(struct super_block *sb, void *data, int silent)
 {
        struct inode *inode;
-       struct dentry *root;
        struct shmem_sb_info *sbinfo;
        int err = -ENOMEM;
 
@@ -2201,14 +2287,11 @@ int shmem_fill_super(struct super_block *sb, void *data, int silent)
                goto failed;
        inode->i_uid = sbinfo->uid;
        inode->i_gid = sbinfo->gid;
-       root = d_alloc_root(inode);
-       if (!root)
-               goto failed_iput;
-       sb->s_root = root;
+       sb->s_root = d_make_root(inode);
+       if (!sb->s_root)
+               goto failed;
        return 0;
 
-failed_iput:
-       iput(inode);
 failed:
        shmem_put_super(sb);
        return err;
@@ -2228,13 +2311,12 @@ static struct inode *shmem_alloc_inode(struct super_block *sb)
 static void shmem_destroy_callback(struct rcu_head *head)
 {
        struct inode *inode = container_of(head, struct inode, i_rcu);
-       INIT_LIST_HEAD(&inode->i_dentry);
        kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
 }
 
 static void shmem_destroy_inode(struct inode *inode)
 {
-       if ((inode->i_mode & S_IFMT) == S_IFREG)
+       if (S_ISREG(inode->i_mode))
                mpol_free_shared_policy(&SHMEM_I(inode)->policy);
        call_rcu(&inode->i_rcu, shmem_destroy_callback);
 }
@@ -2440,6 +2522,10 @@ int shmem_lock(struct file *file, int lock, struct user_struct *user)
        return 0;
 }
 
+void shmem_unlock_mapping(struct address_space *mapping)
+{
+}
+
 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 {
        truncate_inode_pages_range(inode->i_mapping, lstart, lend);
@@ -2497,7 +2583,7 @@ struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags
 
        d_instantiate(path.dentry, inode);
        inode->i_size = size;
-       inode->i_nlink = 0;     /* It is unlinked */
+       clear_nlink(inode);     /* It is unlinked */
 #ifndef CONFIG_MMU
        error = ramfs_nommu_expand_for_mapping(inode, size);
        if (error)