Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1
0002 /*
0003  * Copyright IBM Corporation, 2010
0004  * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/fs.h>
0009 #include <net/9p/9p.h>
0010 #include <net/9p/client.h>
0011 #include <linux/slab.h>
0012 #include <linux/sched.h>
0013 #include <linux/posix_acl_xattr.h>
0014 #include "xattr.h"
0015 #include "acl.h"
0016 #include "v9fs.h"
0017 #include "v9fs_vfs.h"
0018 #include "fid.h"
0019 
0020 static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
0021 {
0022     ssize_t size;
0023     void *value = NULL;
0024     struct posix_acl *acl = NULL;
0025 
0026     size = v9fs_fid_xattr_get(fid, name, NULL, 0);
0027     if (size > 0) {
0028         value = kzalloc(size, GFP_NOFS);
0029         if (!value)
0030             return ERR_PTR(-ENOMEM);
0031         size = v9fs_fid_xattr_get(fid, name, value, size);
0032         if (size > 0) {
0033             acl = posix_acl_from_xattr(&init_user_ns, value, size);
0034             if (IS_ERR(acl))
0035                 goto err_out;
0036         }
0037     } else if (size == -ENODATA || size == 0 ||
0038            size == -ENOSYS || size == -EOPNOTSUPP) {
0039         acl = NULL;
0040     } else
0041         acl = ERR_PTR(-EIO);
0042 
0043 err_out:
0044     kfree(value);
0045     return acl;
0046 }
0047 
0048 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
0049 {
0050     int retval = 0;
0051     struct posix_acl *pacl, *dacl;
0052     struct v9fs_session_info *v9ses;
0053 
0054     v9ses = v9fs_inode2v9ses(inode);
0055     if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
0056             ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
0057         set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
0058         set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
0059         return 0;
0060     }
0061     /* get the default/access acl values and cache them */
0062     dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
0063     pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
0064 
0065     if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
0066         set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
0067         set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
0068     } else
0069         retval = -EIO;
0070 
0071     if (!IS_ERR(dacl))
0072         posix_acl_release(dacl);
0073 
0074     if (!IS_ERR(pacl))
0075         posix_acl_release(pacl);
0076 
0077     return retval;
0078 }
0079 
0080 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
0081 {
0082     struct posix_acl *acl;
0083     /*
0084      * 9p Always cache the acl value when
0085      * instantiating the inode (v9fs_inode_from_fid)
0086      */
0087     acl = get_cached_acl(inode, type);
0088     BUG_ON(is_uncached_acl(acl));
0089     return acl;
0090 }
0091 
0092 struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type, bool rcu)
0093 {
0094     struct v9fs_session_info *v9ses;
0095 
0096     if (rcu)
0097         return ERR_PTR(-ECHILD);
0098 
0099     v9ses = v9fs_inode2v9ses(inode);
0100     if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
0101             ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
0102         /*
0103          * On access = client  and acl = on mode get the acl
0104          * values from the server
0105          */
0106         return NULL;
0107     }
0108     return v9fs_get_cached_acl(inode, type);
0109 
0110 }
0111 
0112 static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
0113 {
0114     int retval;
0115     char *name;
0116     size_t size;
0117     void *buffer;
0118 
0119     if (!acl)
0120         return 0;
0121 
0122     /* Set a setxattr request to server */
0123     size = posix_acl_xattr_size(acl->a_count);
0124     buffer = kmalloc(size, GFP_KERNEL);
0125     if (!buffer)
0126         return -ENOMEM;
0127     retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
0128     if (retval < 0)
0129         goto err_free_out;
0130     switch (type) {
0131     case ACL_TYPE_ACCESS:
0132         name = XATTR_NAME_POSIX_ACL_ACCESS;
0133         break;
0134     case ACL_TYPE_DEFAULT:
0135         name = XATTR_NAME_POSIX_ACL_DEFAULT;
0136         break;
0137     default:
0138         BUG();
0139     }
0140     retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
0141 err_free_out:
0142     kfree(buffer);
0143     return retval;
0144 }
0145 
0146 int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
0147 {
0148     int retval = 0;
0149     struct posix_acl *acl;
0150 
0151     if (S_ISLNK(inode->i_mode))
0152         return -EOPNOTSUPP;
0153     acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
0154     if (acl) {
0155         retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
0156         if (retval)
0157             return retval;
0158         set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
0159         retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
0160         posix_acl_release(acl);
0161     }
0162     return retval;
0163 }
0164 
0165 int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
0166             struct posix_acl *dacl, struct posix_acl *acl)
0167 {
0168     set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
0169     set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
0170     v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
0171     v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
0172     return 0;
0173 }
0174 
0175 void v9fs_put_acl(struct posix_acl *dacl,
0176           struct posix_acl *acl)
0177 {
0178     posix_acl_release(dacl);
0179     posix_acl_release(acl);
0180 }
0181 
0182 int v9fs_acl_mode(struct inode *dir, umode_t *modep,
0183           struct posix_acl **dpacl, struct posix_acl **pacl)
0184 {
0185     int retval = 0;
0186     umode_t mode = *modep;
0187     struct posix_acl *acl = NULL;
0188 
0189     if (!S_ISLNK(mode)) {
0190         acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
0191         if (IS_ERR(acl))
0192             return PTR_ERR(acl);
0193         if (!acl)
0194             mode &= ~current_umask();
0195     }
0196     if (acl) {
0197         if (S_ISDIR(mode))
0198             *dpacl = posix_acl_dup(acl);
0199         retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
0200         if (retval < 0)
0201             return retval;
0202         if (retval > 0)
0203             *pacl = acl;
0204         else
0205             posix_acl_release(acl);
0206     }
0207     *modep  = mode;
0208     return 0;
0209 }
0210 
0211 static int v9fs_xattr_get_acl(const struct xattr_handler *handler,
0212                   struct dentry *dentry, struct inode *inode,
0213                   const char *name, void *buffer, size_t size)
0214 {
0215     struct v9fs_session_info *v9ses;
0216     struct posix_acl *acl;
0217     int error;
0218 
0219     v9ses = v9fs_dentry2v9ses(dentry);
0220     /*
0221      * We allow set/get/list of acl when access=client is not specified
0222      */
0223     if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
0224         return v9fs_xattr_get(dentry, handler->name, buffer, size);
0225 
0226     acl = v9fs_get_cached_acl(inode, handler->flags);
0227     if (IS_ERR(acl))
0228         return PTR_ERR(acl);
0229     if (acl == NULL)
0230         return -ENODATA;
0231     error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
0232     posix_acl_release(acl);
0233 
0234     return error;
0235 }
0236 
0237 static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
0238                   struct user_namespace *mnt_userns,
0239                   struct dentry *dentry, struct inode *inode,
0240                   const char *name, const void *value,
0241                   size_t size, int flags)
0242 {
0243     int retval;
0244     struct posix_acl *acl;
0245     struct v9fs_session_info *v9ses;
0246 
0247     v9ses = v9fs_dentry2v9ses(dentry);
0248     /*
0249      * set the attribute on the remote. Without even looking at the
0250      * xattr value. We leave it to the server to validate
0251      */
0252     if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
0253         return v9fs_xattr_set(dentry, handler->name, value, size,
0254                       flags);
0255 
0256     if (S_ISLNK(inode->i_mode))
0257         return -EOPNOTSUPP;
0258     if (!inode_owner_or_capable(&init_user_ns, inode))
0259         return -EPERM;
0260     if (value) {
0261         /* update the cached acl value */
0262         acl = posix_acl_from_xattr(&init_user_ns, value, size);
0263         if (IS_ERR(acl))
0264             return PTR_ERR(acl);
0265         else if (acl) {
0266             retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
0267             if (retval)
0268                 goto err_out;
0269         }
0270     } else
0271         acl = NULL;
0272 
0273     switch (handler->flags) {
0274     case ACL_TYPE_ACCESS:
0275         if (acl) {
0276             struct iattr iattr = { 0 };
0277             struct posix_acl *old_acl = acl;
0278 
0279             retval = posix_acl_update_mode(&init_user_ns, inode,
0280                                &iattr.ia_mode, &acl);
0281             if (retval)
0282                 goto err_out;
0283             if (!acl) {
0284                 /*
0285                  * ACL can be represented
0286                  * by the mode bits. So don't
0287                  * update ACL.
0288                  */
0289                 posix_acl_release(old_acl);
0290                 value = NULL;
0291                 size = 0;
0292             }
0293             iattr.ia_valid = ATTR_MODE;
0294             /* FIXME should we update ctime ?
0295              * What is the following setxattr update the
0296              * mode ?
0297              */
0298             v9fs_vfs_setattr_dotl(&init_user_ns, dentry, &iattr);
0299         }
0300         break;
0301     case ACL_TYPE_DEFAULT:
0302         if (!S_ISDIR(inode->i_mode)) {
0303             retval = acl ? -EINVAL : 0;
0304             goto err_out;
0305         }
0306         break;
0307     default:
0308         BUG();
0309     }
0310     retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
0311     if (!retval)
0312         set_cached_acl(inode, handler->flags, acl);
0313 err_out:
0314     posix_acl_release(acl);
0315     return retval;
0316 }
0317 
0318 const struct xattr_handler v9fs_xattr_acl_access_handler = {
0319     .name   = XATTR_NAME_POSIX_ACL_ACCESS,
0320     .flags  = ACL_TYPE_ACCESS,
0321     .get    = v9fs_xattr_get_acl,
0322     .set    = v9fs_xattr_set_acl,
0323 };
0324 
0325 const struct xattr_handler v9fs_xattr_acl_default_handler = {
0326     .name   = XATTR_NAME_POSIX_ACL_DEFAULT,
0327     .flags  = ACL_TYPE_DEFAULT,
0328     .get    = v9fs_xattr_get_acl,
0329     .set    = v9fs_xattr_set_acl,
0330 };