tizen 2.3.1 release
[kernel/linux-3.0.git] / mm / shmem.c
index e76f74e..65c738e 100644 (file)
@@ -1177,19 +1177,20 @@ static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
 static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
                        struct shmem_inode_info *info, unsigned long idx)
 {
-       struct mempolicy mpol, *spol;
        struct vm_area_struct pvma;
        struct page *page;
 
-       spol = mpol_cond_copy(&mpol,
-                               mpol_shared_policy_lookup(&info->policy, idx));
-
        /* Create a pseudo vma that just contains the policy */
        pvma.vm_start = 0;
        pvma.vm_pgoff = idx;
        pvma.vm_ops = NULL;
-       pvma.vm_policy = spol;
+       pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
+
        page = swapin_readahead(entry, gfp, &pvma, 0);
+
+       /* Drop reference taken by mpol_shared_policy_lookup() */
+       mpol_cond_put(pvma.vm_policy);
+
        return page;
 }
 
@@ -1197,6 +1198,7 @@ static struct page *shmem_alloc_page(gfp_t gfp,
                        struct shmem_inode_info *info, unsigned long idx)
 {
        struct vm_area_struct pvma;
+       struct page *page;
 
        /* Create a pseudo vma that just contains the policy */
        pvma.vm_start = 0;
@@ -1204,10 +1206,12 @@ static struct page *shmem_alloc_page(gfp_t gfp,
        pvma.vm_ops = NULL;
        pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
 
-       /*
-        * alloc_page_vma() will drop the shared policy reference
-        */
-       return alloc_page_vma(gfp, &pvma, 0);
+       page = alloc_page_vma(gfp, &pvma, 0);
+
+       /* Drop reference taken by mpol_shared_policy_lookup() */
+       mpol_cond_put(pvma.vm_policy);
+
+       return page;
 }
 #else /* !CONFIG_NUMA */
 #ifdef CONFIG_TMPFS
@@ -1691,6 +1695,12 @@ static int shmem_readpage(struct file *file, struct page *page)
        return error;
 }
 
+#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,
@@ -1887,8 +1897,8 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
        inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
        if (inode) {
                error = security_inode_init_security(inode, dir,
-                                                    &dentry->d_name, NULL,
-                                                    NULL, NULL);
+                                                    &dentry->d_name,
+                                                    shmem_initxattrs, NULL);
                if (error) {
                        if (error != -EOPNOTSUPP) {
                                iput(inode);
@@ -2027,8 +2037,8 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
        if (!inode)
                return -ENOSPC;
 
-       error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
-                                            NULL, NULL);
+       error = security_inode_init_security(inode, dir, &dentry->d_name,
+                                            shmem_initxattrs, NULL);
        if (error) {
                if (error != -EOPNOTSUPP) {
                        iput(inode);
@@ -2099,6 +2109,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)
 {
@@ -2126,24 +2196,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;
 
@@ -2152,9 +2215,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);
@@ -2254,7 +2314,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);
 
 }
 
@@ -2274,7 +2334,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)
@@ -2357,12 +2417,14 @@ static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
 {
        struct inode *inode;
        struct dentry *dentry = NULL;
-       u64 inum = fid->raw[2];
-       inum = (inum << 32) | fid->raw[1];
+       u64 inum;
 
        if (fh_len < 3)
                return NULL;
 
+       inum = fid->raw[2];
+       inum = (inum << 32) | fid->raw[1];
+
        inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
                        shmem_match, fid->raw);
        if (inode) {
@@ -2508,6 +2570,7 @@ static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
        unsigned long inodes;
        int error = -EINVAL;
 
+       config.mpol = NULL;
        if (shmem_parse_options(data, &config, true))
                return error;
 
@@ -2533,8 +2596,13 @@ static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
        sbinfo->max_inodes  = config.max_inodes;
        sbinfo->free_inodes = config.max_inodes - inodes;
 
-       mpol_put(sbinfo->mpol);
-       sbinfo->mpol        = config.mpol;      /* transfers initial ref */
+       /*
+        * Preserve previous mempolicy unless mpol remount option was specified.
+        */
+       if (config.mpol) {
+               mpol_put(sbinfo->mpol);
+               sbinfo->mpol = config.mpol;     /* transfers initial ref */
+       }
 out:
        spin_unlock(&sbinfo->stat_lock);
        return error;