1024b7d6aSDominique Martinet // SPDX-License-Identifier: LGPL-2.1 285ff872dSAneesh Kumar K.V /* 385ff872dSAneesh Kumar K.V * Copyright IBM Corporation, 2010 485ff872dSAneesh Kumar K.V * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> 585ff872dSAneesh Kumar K.V */ 685ff872dSAneesh Kumar K.V 785ff872dSAneesh Kumar K.V #include <linux/module.h> 885ff872dSAneesh Kumar K.V #include <linux/fs.h> 985ff872dSAneesh Kumar K.V #include <net/9p/9p.h> 1085ff872dSAneesh Kumar K.V #include <net/9p/client.h> 1185ff872dSAneesh Kumar K.V #include <linux/slab.h> 1222d8dcdfSAneesh Kumar K.V #include <linux/sched.h> 1385ff872dSAneesh Kumar K.V #include <linux/posix_acl_xattr.h> 1485ff872dSAneesh Kumar K.V #include "xattr.h" 1585ff872dSAneesh Kumar K.V #include "acl.h" 1676381a42SAneesh Kumar K.V #include "v9fs.h" 175ffc0cb3SAneesh Kumar K.V #include "v9fs_vfs.h" 180f235caeSAl Viro #include "fid.h" 1985ff872dSAneesh Kumar K.V 2085ff872dSAneesh Kumar K.V static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name) 2185ff872dSAneesh Kumar K.V { 2285ff872dSAneesh Kumar K.V ssize_t size; 2385ff872dSAneesh Kumar K.V void *value = NULL; 24009ca389SJoe Perches struct posix_acl *acl = NULL; 2585ff872dSAneesh Kumar K.V 2685ff872dSAneesh Kumar K.V size = v9fs_fid_xattr_get(fid, name, NULL, 0); 2785ff872dSAneesh Kumar K.V if (size > 0) { 2885ff872dSAneesh Kumar K.V value = kzalloc(size, GFP_NOFS); 2985ff872dSAneesh Kumar K.V if (!value) 3085ff872dSAneesh Kumar K.V return ERR_PTR(-ENOMEM); 3185ff872dSAneesh Kumar K.V size = v9fs_fid_xattr_get(fid, name, value, size); 3285ff872dSAneesh Kumar K.V if (size > 0) { 335f3a4a28SEric W. Biederman acl = posix_acl_from_xattr(&init_user_ns, value, size); 3485ff872dSAneesh Kumar K.V if (IS_ERR(acl)) 3585ff872dSAneesh Kumar K.V goto err_out; 3685ff872dSAneesh Kumar K.V } 3785ff872dSAneesh Kumar K.V } else if (size == -ENODATA || size == 0 || 3885ff872dSAneesh Kumar K.V size == -ENOSYS || size == -EOPNOTSUPP) { 3985ff872dSAneesh Kumar K.V acl = NULL; 4085ff872dSAneesh Kumar K.V } else 4185ff872dSAneesh Kumar K.V acl = ERR_PTR(-EIO); 4285ff872dSAneesh Kumar K.V 4385ff872dSAneesh Kumar K.V err_out: 4485ff872dSAneesh Kumar K.V kfree(value); 4585ff872dSAneesh Kumar K.V return acl; 4685ff872dSAneesh Kumar K.V } 4785ff872dSAneesh Kumar K.V 4885ff872dSAneesh Kumar K.V int v9fs_get_acl(struct inode *inode, struct p9_fid *fid) 4985ff872dSAneesh Kumar K.V { 5085ff872dSAneesh Kumar K.V int retval = 0; 5185ff872dSAneesh Kumar K.V struct posix_acl *pacl, *dacl; 5276381a42SAneesh Kumar K.V struct v9fs_session_info *v9ses; 5385ff872dSAneesh Kumar K.V 5476381a42SAneesh Kumar K.V v9ses = v9fs_inode2v9ses(inode); 55e782ef71SVenkateswararao Jujjuri (JV) if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) || 56e782ef71SVenkateswararao Jujjuri (JV) ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) { 5776381a42SAneesh Kumar K.V set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL); 5876381a42SAneesh Kumar K.V set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 5976381a42SAneesh Kumar K.V return 0; 6076381a42SAneesh Kumar K.V } 6185ff872dSAneesh Kumar K.V /* get the default/access acl values and cache them */ 6297d79299SAndreas Gruenbacher dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT); 6397d79299SAndreas Gruenbacher pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS); 6485ff872dSAneesh Kumar K.V 6585ff872dSAneesh Kumar K.V if (!IS_ERR(dacl) && !IS_ERR(pacl)) { 6685ff872dSAneesh Kumar K.V set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); 6785ff872dSAneesh Kumar K.V set_cached_acl(inode, ACL_TYPE_ACCESS, pacl); 6885ff872dSAneesh Kumar K.V } else 6985ff872dSAneesh Kumar K.V retval = -EIO; 7085ff872dSAneesh Kumar K.V 71c61fa0d6SVenkateswararao Jujjuri (JV) if (!IS_ERR(dacl)) 72c61fa0d6SVenkateswararao Jujjuri (JV) posix_acl_release(dacl); 73c61fa0d6SVenkateswararao Jujjuri (JV) 74c61fa0d6SVenkateswararao Jujjuri (JV) if (!IS_ERR(pacl)) 75c61fa0d6SVenkateswararao Jujjuri (JV) posix_acl_release(pacl); 76c61fa0d6SVenkateswararao Jujjuri (JV) 7785ff872dSAneesh Kumar K.V return retval; 7885ff872dSAneesh Kumar K.V } 7985ff872dSAneesh Kumar K.V 8085ff872dSAneesh Kumar K.V static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type) 8185ff872dSAneesh Kumar K.V { 8285ff872dSAneesh Kumar K.V struct posix_acl *acl; 8385ff872dSAneesh Kumar K.V /* 8485ff872dSAneesh Kumar K.V * 9p Always cache the acl value when 8585ff872dSAneesh Kumar K.V * instantiating the inode (v9fs_inode_from_fid) 8685ff872dSAneesh Kumar K.V */ 8785ff872dSAneesh Kumar K.V acl = get_cached_acl(inode, type); 88b8a7a3a6SAndreas Gruenbacher BUG_ON(is_uncached_acl(acl)); 8985ff872dSAneesh Kumar K.V return acl; 9085ff872dSAneesh Kumar K.V } 9185ff872dSAneesh Kumar K.V 920cad6246SMiklos Szeredi struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type, bool rcu) 9385ff872dSAneesh Kumar K.V { 9476381a42SAneesh Kumar K.V struct v9fs_session_info *v9ses; 9576381a42SAneesh Kumar K.V 960cad6246SMiklos Szeredi if (rcu) 970cad6246SMiklos Szeredi return ERR_PTR(-ECHILD); 980cad6246SMiklos Szeredi 9976381a42SAneesh Kumar K.V v9ses = v9fs_inode2v9ses(inode); 100e782ef71SVenkateswararao Jujjuri (JV) if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) || 101e782ef71SVenkateswararao Jujjuri (JV) ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) { 10276381a42SAneesh Kumar K.V /* 103e782ef71SVenkateswararao Jujjuri (JV) * On access = client and acl = on mode get the acl 10476381a42SAneesh Kumar K.V * values from the server 10576381a42SAneesh Kumar K.V */ 1064e34e719SChristoph Hellwig return NULL; 10776381a42SAneesh Kumar K.V } 1084e34e719SChristoph Hellwig return v9fs_get_cached_acl(inode, type); 10985ff872dSAneesh Kumar K.V 11085ff872dSAneesh Kumar K.V } 1117a4566b0SAneesh Kumar K.V 1120f235caeSAl Viro static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl) 1136e8dc555SAneesh Kumar K.V { 1146e8dc555SAneesh Kumar K.V int retval; 1156e8dc555SAneesh Kumar K.V char *name; 1166e8dc555SAneesh Kumar K.V size_t size; 1176e8dc555SAneesh Kumar K.V void *buffer; 118*6e195b0fSDominique Martinet 119d344b0fbSVenkateswararao Jujjuri (JV) if (!acl) 120d344b0fbSVenkateswararao Jujjuri (JV) return 0; 121d344b0fbSVenkateswararao Jujjuri (JV) 1226e8dc555SAneesh Kumar K.V /* Set a setxattr request to server */ 1236e8dc555SAneesh Kumar K.V size = posix_acl_xattr_size(acl->a_count); 1246e8dc555SAneesh Kumar K.V buffer = kmalloc(size, GFP_KERNEL); 1256e8dc555SAneesh Kumar K.V if (!buffer) 1266e8dc555SAneesh Kumar K.V return -ENOMEM; 1275f3a4a28SEric W. Biederman retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); 1286e8dc555SAneesh Kumar K.V if (retval < 0) 1296e8dc555SAneesh Kumar K.V goto err_free_out; 1306e8dc555SAneesh Kumar K.V switch (type) { 1316e8dc555SAneesh Kumar K.V case ACL_TYPE_ACCESS: 13297d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_ACCESS; 1336e8dc555SAneesh Kumar K.V break; 1346e8dc555SAneesh Kumar K.V case ACL_TYPE_DEFAULT: 13597d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_DEFAULT; 1366e8dc555SAneesh Kumar K.V break; 1376e8dc555SAneesh Kumar K.V default: 1386e8dc555SAneesh Kumar K.V BUG(); 1396e8dc555SAneesh Kumar K.V } 1400f235caeSAl Viro retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0); 1416e8dc555SAneesh Kumar K.V err_free_out: 1426e8dc555SAneesh Kumar K.V kfree(buffer); 1436e8dc555SAneesh Kumar K.V return retval; 1446e8dc555SAneesh Kumar K.V } 1456e8dc555SAneesh Kumar K.V 146be308f07SAl Viro int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid) 1476e8dc555SAneesh Kumar K.V { 1486e8dc555SAneesh Kumar K.V int retval = 0; 149bc26ab5fSAl Viro struct posix_acl *acl; 1506e8dc555SAneesh Kumar K.V 1516e8dc555SAneesh Kumar K.V if (S_ISLNK(inode->i_mode)) 1526e8dc555SAneesh Kumar K.V return -EOPNOTSUPP; 1536e8dc555SAneesh Kumar K.V acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); 1546e8dc555SAneesh Kumar K.V if (acl) { 1555bf3258fSChristoph Hellwig retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); 156bc26ab5fSAl Viro if (retval) 157bc26ab5fSAl Viro return retval; 1587f165aaaSAl Viro set_cached_acl(inode, ACL_TYPE_ACCESS, acl); 1590f235caeSAl Viro retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl); 1606e8dc555SAneesh Kumar K.V posix_acl_release(acl); 1616e8dc555SAneesh Kumar K.V } 1626e8dc555SAneesh Kumar K.V return retval; 1636e8dc555SAneesh Kumar K.V } 1646e8dc555SAneesh Kumar K.V 1653592ac44SAl Viro int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid, 1665fa6300aSAl Viro struct posix_acl *dacl, struct posix_acl *acl) 167ad77dbceSAneesh Kumar K.V { 1683592ac44SAl Viro set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); 1693592ac44SAl Viro set_cached_acl(inode, ACL_TYPE_ACCESS, acl); 1705fa6300aSAl Viro v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl); 1715fa6300aSAl Viro v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl); 172ad77dbceSAneesh Kumar K.V return 0; 173ad77dbceSAneesh Kumar K.V } 174ad77dbceSAneesh Kumar K.V 1755fa6300aSAl Viro void v9fs_put_acl(struct posix_acl *dacl, 1765fa6300aSAl Viro struct posix_acl *acl) 1775fa6300aSAl Viro { 1785fa6300aSAl Viro posix_acl_release(dacl); 1795fa6300aSAl Viro posix_acl_release(acl); 1805fa6300aSAl Viro } 1815fa6300aSAl Viro 182d3fb6120SAl Viro int v9fs_acl_mode(struct inode *dir, umode_t *modep, 183ad77dbceSAneesh Kumar K.V struct posix_acl **dpacl, struct posix_acl **pacl) 184ad77dbceSAneesh Kumar K.V { 185ad77dbceSAneesh Kumar K.V int retval = 0; 186d3fb6120SAl Viro umode_t mode = *modep; 187ad77dbceSAneesh Kumar K.V struct posix_acl *acl = NULL; 188ad77dbceSAneesh Kumar K.V 189ad77dbceSAneesh Kumar K.V if (!S_ISLNK(mode)) { 190ad77dbceSAneesh Kumar K.V acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT); 191ad77dbceSAneesh Kumar K.V if (IS_ERR(acl)) 192ad77dbceSAneesh Kumar K.V return PTR_ERR(acl); 193ad77dbceSAneesh Kumar K.V if (!acl) 194ad77dbceSAneesh Kumar K.V mode &= ~current_umask(); 195ad77dbceSAneesh Kumar K.V } 196ad77dbceSAneesh Kumar K.V if (acl) { 197ad77dbceSAneesh Kumar K.V if (S_ISDIR(mode)) 1981ec95bf3SAl Viro *dpacl = posix_acl_dup(acl); 19937bc1539SChristoph Hellwig retval = __posix_acl_create(&acl, GFP_NOFS, &mode); 200826cae2fSAl Viro if (retval < 0) 201826cae2fSAl Viro return retval; 202ad77dbceSAneesh Kumar K.V if (retval > 0) 203826cae2fSAl Viro *pacl = acl; 2041ec95bf3SAl Viro else 205826cae2fSAl Viro posix_acl_release(acl); 206ad77dbceSAneesh Kumar K.V } 207ad77dbceSAneesh Kumar K.V *modep = mode; 208ad77dbceSAneesh Kumar K.V return 0; 209ad77dbceSAneesh Kumar K.V } 210ad77dbceSAneesh Kumar K.V 211d9a82a04SAndreas Gruenbacher static int v9fs_xattr_get_acl(const struct xattr_handler *handler, 212b296821aSAl Viro struct dentry *dentry, struct inode *inode, 213b296821aSAl Viro const char *name, void *buffer, size_t size) 2147a4566b0SAneesh Kumar K.V { 21576381a42SAneesh Kumar K.V struct v9fs_session_info *v9ses; 2167a4566b0SAneesh Kumar K.V struct posix_acl *acl; 2177a4566b0SAneesh Kumar K.V int error; 2187a4566b0SAneesh Kumar K.V 21942869c8aSAneesh Kumar K.V v9ses = v9fs_dentry2v9ses(dentry); 22076381a42SAneesh Kumar K.V /* 22176381a42SAneesh Kumar K.V * We allow set/get/list of acl when access=client is not specified 22276381a42SAneesh Kumar K.V */ 22376381a42SAneesh Kumar K.V if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) 22498e9cb57SAndreas Gruenbacher return v9fs_xattr_get(dentry, handler->name, buffer, size); 22576381a42SAneesh Kumar K.V 226b296821aSAl Viro acl = v9fs_get_cached_acl(inode, handler->flags); 2277a4566b0SAneesh Kumar K.V if (IS_ERR(acl)) 2287a4566b0SAneesh Kumar K.V return PTR_ERR(acl); 2297a4566b0SAneesh Kumar K.V if (acl == NULL) 2307a4566b0SAneesh Kumar K.V return -ENODATA; 2315f3a4a28SEric W. Biederman error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); 2327a4566b0SAneesh Kumar K.V posix_acl_release(acl); 2337a4566b0SAneesh Kumar K.V 2347a4566b0SAneesh Kumar K.V return error; 2357a4566b0SAneesh Kumar K.V } 2367a4566b0SAneesh Kumar K.V 237d9a82a04SAndreas Gruenbacher static int v9fs_xattr_set_acl(const struct xattr_handler *handler, 238e65ce2a5SChristian Brauner struct user_namespace *mnt_userns, 23959301226SAl Viro struct dentry *dentry, struct inode *inode, 24059301226SAl Viro const char *name, const void *value, 24159301226SAl Viro size_t size, int flags) 2427a4566b0SAneesh Kumar K.V { 24322d8dcdfSAneesh Kumar K.V int retval; 24422d8dcdfSAneesh Kumar K.V struct posix_acl *acl; 24576381a42SAneesh Kumar K.V struct v9fs_session_info *v9ses; 24622d8dcdfSAneesh Kumar K.V 24742869c8aSAneesh Kumar K.V v9ses = v9fs_dentry2v9ses(dentry); 24876381a42SAneesh Kumar K.V /* 24976381a42SAneesh Kumar K.V * set the attribute on the remote. Without even looking at the 25076381a42SAneesh Kumar K.V * xattr value. We leave it to the server to validate 25176381a42SAneesh Kumar K.V */ 25276381a42SAneesh Kumar K.V if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) 25398e9cb57SAndreas Gruenbacher return v9fs_xattr_set(dentry, handler->name, value, size, 254e409de99SAndreas Gruenbacher flags); 25576381a42SAneesh Kumar K.V 25622d8dcdfSAneesh Kumar K.V if (S_ISLNK(inode->i_mode)) 25722d8dcdfSAneesh Kumar K.V return -EOPNOTSUPP; 25821cb47beSChristian Brauner if (!inode_owner_or_capable(&init_user_ns, inode)) 25922d8dcdfSAneesh Kumar K.V return -EPERM; 26022d8dcdfSAneesh Kumar K.V if (value) { 26122d8dcdfSAneesh Kumar K.V /* update the cached acl value */ 2625f3a4a28SEric W. Biederman acl = posix_acl_from_xattr(&init_user_ns, value, size); 26322d8dcdfSAneesh Kumar K.V if (IS_ERR(acl)) 26422d8dcdfSAneesh Kumar K.V return PTR_ERR(acl); 26522d8dcdfSAneesh Kumar K.V else if (acl) { 2660d4d717fSEric W. Biederman retval = posix_acl_valid(inode->i_sb->s_user_ns, acl); 26722d8dcdfSAneesh Kumar K.V if (retval) 26822d8dcdfSAneesh Kumar K.V goto err_out; 26922d8dcdfSAneesh Kumar K.V } 27022d8dcdfSAneesh Kumar K.V } else 27122d8dcdfSAneesh Kumar K.V acl = NULL; 27222d8dcdfSAneesh Kumar K.V 273d9a82a04SAndreas Gruenbacher switch (handler->flags) { 27422d8dcdfSAneesh Kumar K.V case ACL_TYPE_ACCESS: 27522d8dcdfSAneesh Kumar K.V if (acl) { 276e02a53d9SDominique Martinet struct iattr iattr = { 0 }; 277b5c66babSCong Wang struct posix_acl *old_acl = acl; 27807393101SJan Kara 279549c7297SChristian Brauner retval = posix_acl_update_mode(&init_user_ns, inode, 280e65ce2a5SChristian Brauner &iattr.ia_mode, &acl); 28107393101SJan Kara if (retval) 28207393101SJan Kara goto err_out; 28307393101SJan Kara if (!acl) { 28422d8dcdfSAneesh Kumar K.V /* 28522d8dcdfSAneesh Kumar K.V * ACL can be represented 28622d8dcdfSAneesh Kumar K.V * by the mode bits. So don't 28722d8dcdfSAneesh Kumar K.V * update ACL. 28822d8dcdfSAneesh Kumar K.V */ 289b5c66babSCong Wang posix_acl_release(old_acl); 29022d8dcdfSAneesh Kumar K.V value = NULL; 29122d8dcdfSAneesh Kumar K.V size = 0; 29222d8dcdfSAneesh Kumar K.V } 29322d8dcdfSAneesh Kumar K.V iattr.ia_valid = ATTR_MODE; 29422d8dcdfSAneesh Kumar K.V /* FIXME should we update ctime ? 29522d8dcdfSAneesh Kumar K.V * What is the following setxattr update the 29622d8dcdfSAneesh Kumar K.V * mode ? 29722d8dcdfSAneesh Kumar K.V */ 298549c7297SChristian Brauner v9fs_vfs_setattr_dotl(&init_user_ns, dentry, &iattr); 29922d8dcdfSAneesh Kumar K.V } 30022d8dcdfSAneesh Kumar K.V break; 30122d8dcdfSAneesh Kumar K.V case ACL_TYPE_DEFAULT: 30222d8dcdfSAneesh Kumar K.V if (!S_ISDIR(inode->i_mode)) { 3036f81c115SAneesh Kumar K.V retval = acl ? -EINVAL : 0; 30422d8dcdfSAneesh Kumar K.V goto err_out; 30522d8dcdfSAneesh Kumar K.V } 30622d8dcdfSAneesh Kumar K.V break; 30722d8dcdfSAneesh Kumar K.V default: 30822d8dcdfSAneesh Kumar K.V BUG(); 30922d8dcdfSAneesh Kumar K.V } 31098e9cb57SAndreas Gruenbacher retval = v9fs_xattr_set(dentry, handler->name, value, size, flags); 31122d8dcdfSAneesh Kumar K.V if (!retval) 312d9a82a04SAndreas Gruenbacher set_cached_acl(inode, handler->flags, acl); 31322d8dcdfSAneesh Kumar K.V err_out: 31422d8dcdfSAneesh Kumar K.V posix_acl_release(acl); 31522d8dcdfSAneesh Kumar K.V return retval; 3167a4566b0SAneesh Kumar K.V } 3177a4566b0SAneesh Kumar K.V 3187a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = { 31998e9cb57SAndreas Gruenbacher .name = XATTR_NAME_POSIX_ACL_ACCESS, 3207a4566b0SAneesh Kumar K.V .flags = ACL_TYPE_ACCESS, 3217a4566b0SAneesh Kumar K.V .get = v9fs_xattr_get_acl, 3227a4566b0SAneesh Kumar K.V .set = v9fs_xattr_set_acl, 3237a4566b0SAneesh Kumar K.V }; 3247a4566b0SAneesh Kumar K.V 3257a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = { 32698e9cb57SAndreas Gruenbacher .name = XATTR_NAME_POSIX_ACL_DEFAULT, 3277a4566b0SAneesh Kumar K.V .flags = ACL_TYPE_DEFAULT, 3287a4566b0SAneesh Kumar K.V .get = v9fs_xattr_get_acl, 3297a4566b0SAneesh Kumar K.V .set = v9fs_xattr_set_acl, 3307a4566b0SAneesh Kumar K.V }; 331