tmpfs,ceph,gfs2,isofs,reiserfs,xfs: fix fh_len checking
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / shmem.c
index d4e184e..67afba5 100644 (file)
@@ -77,13 +77,6 @@ static struct vfsmount *shm_mnt;
 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
 #define SHORT_SYMLINK_LEN 128
 
-struct shmem_xattr {
-       struct list_head list;  /* anchored by shmem_inode_info->xattr_list */
-       char *name;             /* xattr name */
-       size_t size;
-       char value[0];
-};
-
 /*
  * shmem_fallocate and shmem_writepage communicate via inode->i_private
  * (with i_mutex making sure that it has only one user at a time):
@@ -636,7 +629,6 @@ static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
 static void shmem_evict_inode(struct inode *inode)
 {
        struct shmem_inode_info *info = SHMEM_I(inode);
-       struct shmem_xattr *xattr, *nxattr;
 
        if (inode->i_mapping->a_ops == &shmem_aops) {
                shmem_unacct_size(info->flags, inode->i_size);
@@ -650,10 +642,7 @@ static void shmem_evict_inode(struct inode *inode)
        } else
                kfree(info->symlink);
 
-       list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
-               kfree(xattr->name);
-               kfree(xattr);
-       }
+       simple_xattrs_free(&info->xattrs);
        BUG_ON(inode->i_blocks);
        shmem_free_inode(inode->i_sb);
        clear_inode(inode);
@@ -1350,7 +1339,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
 {
        file_accessed(file);
        vma->vm_ops = &shmem_vm_ops;
-       vma->vm_flags |= VM_CAN_NONLINEAR;
        return 0;
 }
 
@@ -1377,7 +1365,7 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
                spin_lock_init(&info->lock);
                info->flags = flags & VM_NORESERVE;
                INIT_LIST_HEAD(&info->swaplist);
-               INIT_LIST_HEAD(&info->xattr_list);
+               simple_xattrs_init(&info->xattrs);
                cache_no_acl(inode);
 
                switch (mode & S_IFMT) {
@@ -2060,28 +2048,6 @@ static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *co
  */
 
 /*
- * 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,
@@ -2090,11 +2056,11 @@ static int shmem_initxattrs(struct inode *inode,
 {
        struct shmem_inode_info *info = SHMEM_I(inode);
        const struct xattr *xattr;
-       struct shmem_xattr *new_xattr;
+       struct simple_xattr *new_xattr;
        size_t len;
 
        for (xattr = xattr_array; xattr->name != NULL; xattr++) {
-               new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
+               new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
                if (!new_xattr)
                        return -ENOMEM;
 
@@ -2111,91 +2077,12 @@ static int shmem_initxattrs(struct inode *inode,
                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);
+               simple_xattr_list_add(&info->xattrs, new_xattr);
        }
 
        return 0;
 }
 
-static int shmem_xattr_get(struct dentry *dentry, const char *name,
-                          void *buffer, size_t size)
-{
-       struct shmem_inode_info *info;
-       struct shmem_xattr *xattr;
-       int ret = -ENODATA;
-
-       info = SHMEM_I(dentry->d_inode);
-
-       spin_lock(&info->lock);
-       list_for_each_entry(xattr, &info->xattr_list, list) {
-               if (strcmp(name, xattr->name))
-                       continue;
-
-               ret = xattr->size;
-               if (buffer) {
-                       if (size < xattr->size)
-                               ret = -ERANGE;
-                       else
-                               memcpy(buffer, xattr->value, xattr->size);
-               }
-               break;
-       }
-       spin_unlock(&info->lock);
-       return ret;
-}
-
-static int shmem_xattr_set(struct inode *inode, const char *name,
-                          const void *value, size_t size, int flags)
-{
-       struct shmem_inode_info *info = SHMEM_I(inode);
-       struct shmem_xattr *xattr;
-       struct shmem_xattr *new_xattr = NULL;
-       int err = 0;
-
-       /* value == NULL means remove */
-       if (value) {
-               new_xattr = shmem_xattr_alloc(value, size);
-               if (!new_xattr)
-                       return -ENOMEM;
-
-               new_xattr->name = kstrdup(name, GFP_KERNEL);
-               if (!new_xattr->name) {
-                       kfree(new_xattr);
-                       return -ENOMEM;
-               }
-       }
-
-       spin_lock(&info->lock);
-       list_for_each_entry(xattr, &info->xattr_list, list) {
-               if (!strcmp(name, xattr->name)) {
-                       if (flags & XATTR_CREATE) {
-                               xattr = new_xattr;
-                               err = -EEXIST;
-                       } else if (new_xattr) {
-                               list_replace(&xattr->list, &new_xattr->list);
-                       } else {
-                               list_del(&xattr->list);
-                       }
-                       goto out;
-               }
-       }
-       if (flags & XATTR_REPLACE) {
-               xattr = new_xattr;
-               err = -ENODATA;
-       } else {
-               list_add(&new_xattr->list, &info->xattr_list);
-               xattr = NULL;
-       }
-out:
-       spin_unlock(&info->lock);
-       if (xattr)
-               kfree(xattr->name);
-       kfree(xattr);
-       return err;
-}
-
 static const struct xattr_handler *shmem_xattr_handlers[] = {
 #ifdef CONFIG_TMPFS_POSIX_ACL
        &generic_acl_access_handler,
@@ -2226,6 +2113,7 @@ static int shmem_xattr_validate(const char *name)
 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
                              void *buffer, size_t size)
 {
+       struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
        int err;
 
        /*
@@ -2240,12 +2128,13 @@ static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
        if (err)
                return err;
 
-       return shmem_xattr_get(dentry, name, buffer, size);
+       return simple_xattr_get(&info->xattrs, name, buffer, size);
 }
 
 static int shmem_setxattr(struct dentry *dentry, const char *name,
                          const void *value, size_t size, int flags)
 {
+       struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
        int err;
 
        /*
@@ -2260,15 +2149,12 @@ static int shmem_setxattr(struct dentry *dentry, const char *name,
        if (err)
                return err;
 
-       if (size == 0)
-               value = "";  /* empty EA, do not remove */
-
-       return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
-
+       return simple_xattr_set(&info->xattrs, name, value, size, flags);
 }
 
 static int shmem_removexattr(struct dentry *dentry, const char *name)
 {
+       struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
        int err;
 
        /*
@@ -2283,45 +2169,13 @@ static int shmem_removexattr(struct dentry *dentry, const char *name)
        if (err)
                return err;
 
-       return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
-}
-
-static bool xattr_is_trusted(const char *name)
-{
-       return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
+       return simple_xattr_remove(&info->xattrs, name);
 }
 
 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
 {
-       bool trusted = capable(CAP_SYS_ADMIN);
-       struct shmem_xattr *xattr;
-       struct shmem_inode_info *info;
-       size_t used = 0;
-
-       info = SHMEM_I(dentry->d_inode);
-
-       spin_lock(&info->lock);
-       list_for_each_entry(xattr, &info->xattr_list, list) {
-               size_t len;
-
-               /* skip "trusted." attributes for unprivileged callers */
-               if (!trusted && xattr_is_trusted(xattr->name))
-                       continue;
-
-               len = strlen(xattr->name) + 1;
-               used += len;
-               if (buffer) {
-                       if (size < used) {
-                               used = -ERANGE;
-                               break;
-                       }
-                       memcpy(buffer, xattr->name, len);
-                       buffer += len;
-               }
-       }
-       spin_unlock(&info->lock);
-
-       return used;
+       struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
+       return simple_xattr_list(&info->xattrs, buffer, size);
 }
 #endif /* CONFIG_TMPFS_XATTR */
 
@@ -2366,12 +2220,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) {
@@ -2788,6 +2644,7 @@ static const struct vm_operations_struct shmem_vm_ops = {
        .set_policy     = shmem_set_policy,
        .get_policy     = shmem_get_policy,
 #endif
+       .remap_pages    = generic_file_remap_pages,
 };
 
 static struct dentry *shmem_mount(struct file_system_type *fs_type,
@@ -2981,7 +2838,6 @@ int shmem_zero_setup(struct vm_area_struct *vma)
                fput(vma->vm_file);
        vma->vm_file = file;
        vma->vm_ops = &shmem_vm_ops;
-       vma->vm_flags |= VM_CAN_NONLINEAR;
        return 0;
 }