185ff872dSAneesh Kumar K.V /* 285ff872dSAneesh Kumar K.V * Copyright IBM Corporation, 2010 385ff872dSAneesh Kumar K.V * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> 485ff872dSAneesh Kumar K.V * 585ff872dSAneesh Kumar K.V * This program is free software; you can redistribute it and/or modify it 685ff872dSAneesh Kumar K.V * under the terms of version 2.1 of the GNU Lesser General Public License 785ff872dSAneesh Kumar K.V * as published by the Free Software Foundation. 885ff872dSAneesh Kumar K.V * 985ff872dSAneesh Kumar K.V * This program is distributed in the hope that it would be useful, but 1085ff872dSAneesh Kumar K.V * WITHOUT ANY WARRANTY; without even the implied warranty of 1185ff872dSAneesh Kumar K.V * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1285ff872dSAneesh Kumar K.V * 1385ff872dSAneesh Kumar K.V */ 1485ff872dSAneesh Kumar K.V 1585ff872dSAneesh Kumar K.V #include <linux/module.h> 1685ff872dSAneesh Kumar K.V #include <linux/fs.h> 1785ff872dSAneesh Kumar K.V #include <net/9p/9p.h> 1885ff872dSAneesh Kumar K.V #include <net/9p/client.h> 1985ff872dSAneesh Kumar K.V #include <linux/slab.h> 2022d8dcdfSAneesh Kumar K.V #include <linux/sched.h> 2185ff872dSAneesh Kumar K.V #include <linux/posix_acl_xattr.h> 2285ff872dSAneesh Kumar K.V #include "xattr.h" 2385ff872dSAneesh Kumar K.V #include "acl.h" 2422d8dcdfSAneesh Kumar K.V #include "v9fs_vfs.h" 2585ff872dSAneesh Kumar K.V 2685ff872dSAneesh Kumar K.V static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name) 2785ff872dSAneesh Kumar K.V { 2885ff872dSAneesh Kumar K.V ssize_t size; 2985ff872dSAneesh Kumar K.V void *value = NULL; 3085ff872dSAneesh Kumar K.V struct posix_acl *acl = NULL;; 3185ff872dSAneesh Kumar K.V 3285ff872dSAneesh Kumar K.V size = v9fs_fid_xattr_get(fid, name, NULL, 0); 3385ff872dSAneesh Kumar K.V if (size > 0) { 3485ff872dSAneesh Kumar K.V value = kzalloc(size, GFP_NOFS); 3585ff872dSAneesh Kumar K.V if (!value) 3685ff872dSAneesh Kumar K.V return ERR_PTR(-ENOMEM); 3785ff872dSAneesh Kumar K.V size = v9fs_fid_xattr_get(fid, name, value, size); 3885ff872dSAneesh Kumar K.V if (size > 0) { 3985ff872dSAneesh Kumar K.V acl = posix_acl_from_xattr(value, size); 4085ff872dSAneesh Kumar K.V if (IS_ERR(acl)) 4185ff872dSAneesh Kumar K.V goto err_out; 4285ff872dSAneesh Kumar K.V } 4385ff872dSAneesh Kumar K.V } else if (size == -ENODATA || size == 0 || 4485ff872dSAneesh Kumar K.V size == -ENOSYS || size == -EOPNOTSUPP) { 4585ff872dSAneesh Kumar K.V acl = NULL; 4685ff872dSAneesh Kumar K.V } else 4785ff872dSAneesh Kumar K.V acl = ERR_PTR(-EIO); 4885ff872dSAneesh Kumar K.V 4985ff872dSAneesh Kumar K.V err_out: 5085ff872dSAneesh Kumar K.V kfree(value); 5185ff872dSAneesh Kumar K.V return acl; 5285ff872dSAneesh Kumar K.V } 5385ff872dSAneesh Kumar K.V 5485ff872dSAneesh Kumar K.V int v9fs_get_acl(struct inode *inode, struct p9_fid *fid) 5585ff872dSAneesh Kumar K.V { 5685ff872dSAneesh Kumar K.V int retval = 0; 5785ff872dSAneesh Kumar K.V struct posix_acl *pacl, *dacl; 5885ff872dSAneesh Kumar K.V 5985ff872dSAneesh Kumar K.V /* get the default/access acl values and cache them */ 6085ff872dSAneesh Kumar K.V dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT); 6185ff872dSAneesh Kumar K.V pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS); 6285ff872dSAneesh Kumar K.V 6385ff872dSAneesh Kumar K.V if (!IS_ERR(dacl) && !IS_ERR(pacl)) { 6485ff872dSAneesh Kumar K.V set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); 6585ff872dSAneesh Kumar K.V set_cached_acl(inode, ACL_TYPE_ACCESS, pacl); 6685ff872dSAneesh Kumar K.V posix_acl_release(dacl); 6785ff872dSAneesh Kumar K.V posix_acl_release(pacl); 6885ff872dSAneesh Kumar K.V } else 6985ff872dSAneesh Kumar K.V retval = -EIO; 7085ff872dSAneesh Kumar K.V 7185ff872dSAneesh Kumar K.V return retval; 7285ff872dSAneesh Kumar K.V } 7385ff872dSAneesh Kumar K.V 7485ff872dSAneesh Kumar K.V static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type) 7585ff872dSAneesh Kumar K.V { 7685ff872dSAneesh Kumar K.V struct posix_acl *acl; 7785ff872dSAneesh Kumar K.V /* 7885ff872dSAneesh Kumar K.V * 9p Always cache the acl value when 7985ff872dSAneesh Kumar K.V * instantiating the inode (v9fs_inode_from_fid) 8085ff872dSAneesh Kumar K.V */ 8185ff872dSAneesh Kumar K.V acl = get_cached_acl(inode, type); 8285ff872dSAneesh Kumar K.V BUG_ON(acl == ACL_NOT_CACHED); 8385ff872dSAneesh Kumar K.V return acl; 8485ff872dSAneesh Kumar K.V } 8585ff872dSAneesh Kumar K.V 8685ff872dSAneesh Kumar K.V int v9fs_check_acl(struct inode *inode, int mask) 8785ff872dSAneesh Kumar K.V { 8885ff872dSAneesh Kumar K.V struct posix_acl *acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); 8985ff872dSAneesh Kumar K.V 9085ff872dSAneesh Kumar K.V if (IS_ERR(acl)) 9185ff872dSAneesh Kumar K.V return PTR_ERR(acl); 9285ff872dSAneesh Kumar K.V if (acl) { 9385ff872dSAneesh Kumar K.V int error = posix_acl_permission(inode, acl, mask); 9485ff872dSAneesh Kumar K.V posix_acl_release(acl); 9585ff872dSAneesh Kumar K.V return error; 9685ff872dSAneesh Kumar K.V } 9785ff872dSAneesh Kumar K.V return -EAGAIN; 9885ff872dSAneesh Kumar K.V } 997a4566b0SAneesh Kumar K.V 1006e8dc555SAneesh Kumar K.V static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl) 1016e8dc555SAneesh Kumar K.V { 1026e8dc555SAneesh Kumar K.V int retval; 1036e8dc555SAneesh Kumar K.V char *name; 1046e8dc555SAneesh Kumar K.V size_t size; 1056e8dc555SAneesh Kumar K.V void *buffer; 1066e8dc555SAneesh Kumar K.V struct inode *inode = dentry->d_inode; 1076e8dc555SAneesh Kumar K.V 1086e8dc555SAneesh Kumar K.V set_cached_acl(inode, type, acl); 1096e8dc555SAneesh Kumar K.V /* Set a setxattr request to server */ 1106e8dc555SAneesh Kumar K.V size = posix_acl_xattr_size(acl->a_count); 1116e8dc555SAneesh Kumar K.V buffer = kmalloc(size, GFP_KERNEL); 1126e8dc555SAneesh Kumar K.V if (!buffer) 1136e8dc555SAneesh Kumar K.V return -ENOMEM; 1146e8dc555SAneesh Kumar K.V retval = posix_acl_to_xattr(acl, buffer, size); 1156e8dc555SAneesh Kumar K.V if (retval < 0) 1166e8dc555SAneesh Kumar K.V goto err_free_out; 1176e8dc555SAneesh Kumar K.V switch (type) { 1186e8dc555SAneesh Kumar K.V case ACL_TYPE_ACCESS: 1196e8dc555SAneesh Kumar K.V name = POSIX_ACL_XATTR_ACCESS; 1206e8dc555SAneesh Kumar K.V break; 1216e8dc555SAneesh Kumar K.V case ACL_TYPE_DEFAULT: 1226e8dc555SAneesh Kumar K.V name = POSIX_ACL_XATTR_DEFAULT; 1236e8dc555SAneesh Kumar K.V break; 1246e8dc555SAneesh Kumar K.V default: 1256e8dc555SAneesh Kumar K.V BUG(); 1266e8dc555SAneesh Kumar K.V } 1276e8dc555SAneesh Kumar K.V retval = v9fs_xattr_set(dentry, name, buffer, size, 0); 1286e8dc555SAneesh Kumar K.V err_free_out: 1296e8dc555SAneesh Kumar K.V kfree(buffer); 1306e8dc555SAneesh Kumar K.V return retval; 1316e8dc555SAneesh Kumar K.V } 1326e8dc555SAneesh Kumar K.V 1336e8dc555SAneesh Kumar K.V int v9fs_acl_chmod(struct dentry *dentry) 1346e8dc555SAneesh Kumar K.V { 1356e8dc555SAneesh Kumar K.V int retval = 0; 1366e8dc555SAneesh Kumar K.V struct posix_acl *acl, *clone; 1376e8dc555SAneesh Kumar K.V struct inode *inode = dentry->d_inode; 1386e8dc555SAneesh Kumar K.V 1396e8dc555SAneesh Kumar K.V if (S_ISLNK(inode->i_mode)) 1406e8dc555SAneesh Kumar K.V return -EOPNOTSUPP; 1416e8dc555SAneesh Kumar K.V acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); 1426e8dc555SAneesh Kumar K.V if (acl) { 1436e8dc555SAneesh Kumar K.V clone = posix_acl_clone(acl, GFP_KERNEL); 1446e8dc555SAneesh Kumar K.V posix_acl_release(acl); 1456e8dc555SAneesh Kumar K.V if (!clone) 1466e8dc555SAneesh Kumar K.V return -ENOMEM; 1476e8dc555SAneesh Kumar K.V retval = posix_acl_chmod_masq(clone, inode->i_mode); 1486e8dc555SAneesh Kumar K.V if (!retval) 1496e8dc555SAneesh Kumar K.V retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone); 1506e8dc555SAneesh Kumar K.V posix_acl_release(clone); 1516e8dc555SAneesh Kumar K.V } 1526e8dc555SAneesh Kumar K.V return retval; 1536e8dc555SAneesh Kumar K.V } 1546e8dc555SAneesh Kumar K.V 155*ad77dbceSAneesh Kumar K.V int v9fs_set_create_acl(struct dentry *dentry, 156*ad77dbceSAneesh Kumar K.V struct posix_acl *dpacl, struct posix_acl *pacl) 157*ad77dbceSAneesh Kumar K.V { 158*ad77dbceSAneesh Kumar K.V if (dpacl) 159*ad77dbceSAneesh Kumar K.V v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl); 160*ad77dbceSAneesh Kumar K.V if (pacl) 161*ad77dbceSAneesh Kumar K.V v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl); 162*ad77dbceSAneesh Kumar K.V posix_acl_release(dpacl); 163*ad77dbceSAneesh Kumar K.V posix_acl_release(pacl); 164*ad77dbceSAneesh Kumar K.V return 0; 165*ad77dbceSAneesh Kumar K.V } 166*ad77dbceSAneesh Kumar K.V 167*ad77dbceSAneesh Kumar K.V int v9fs_acl_mode(struct inode *dir, mode_t *modep, 168*ad77dbceSAneesh Kumar K.V struct posix_acl **dpacl, struct posix_acl **pacl) 169*ad77dbceSAneesh Kumar K.V { 170*ad77dbceSAneesh Kumar K.V int retval = 0; 171*ad77dbceSAneesh Kumar K.V mode_t mode = *modep; 172*ad77dbceSAneesh Kumar K.V struct posix_acl *acl = NULL; 173*ad77dbceSAneesh Kumar K.V 174*ad77dbceSAneesh Kumar K.V if (!S_ISLNK(mode)) { 175*ad77dbceSAneesh Kumar K.V acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT); 176*ad77dbceSAneesh Kumar K.V if (IS_ERR(acl)) 177*ad77dbceSAneesh Kumar K.V return PTR_ERR(acl); 178*ad77dbceSAneesh Kumar K.V if (!acl) 179*ad77dbceSAneesh Kumar K.V mode &= ~current_umask(); 180*ad77dbceSAneesh Kumar K.V } 181*ad77dbceSAneesh Kumar K.V if (acl) { 182*ad77dbceSAneesh Kumar K.V struct posix_acl *clone; 183*ad77dbceSAneesh Kumar K.V 184*ad77dbceSAneesh Kumar K.V if (S_ISDIR(mode)) 185*ad77dbceSAneesh Kumar K.V *dpacl = acl; 186*ad77dbceSAneesh Kumar K.V clone = posix_acl_clone(acl, GFP_NOFS); 187*ad77dbceSAneesh Kumar K.V retval = -ENOMEM; 188*ad77dbceSAneesh Kumar K.V if (!clone) 189*ad77dbceSAneesh Kumar K.V goto cleanup; 190*ad77dbceSAneesh Kumar K.V 191*ad77dbceSAneesh Kumar K.V retval = posix_acl_create_masq(clone, &mode); 192*ad77dbceSAneesh Kumar K.V if (retval < 0) { 193*ad77dbceSAneesh Kumar K.V posix_acl_release(clone); 194*ad77dbceSAneesh Kumar K.V goto cleanup; 195*ad77dbceSAneesh Kumar K.V } 196*ad77dbceSAneesh Kumar K.V if (retval > 0) 197*ad77dbceSAneesh Kumar K.V *pacl = clone; 198*ad77dbceSAneesh Kumar K.V } 199*ad77dbceSAneesh Kumar K.V *modep = mode; 200*ad77dbceSAneesh Kumar K.V return 0; 201*ad77dbceSAneesh Kumar K.V cleanup: 202*ad77dbceSAneesh Kumar K.V posix_acl_release(acl); 203*ad77dbceSAneesh Kumar K.V return retval; 204*ad77dbceSAneesh Kumar K.V 205*ad77dbceSAneesh Kumar K.V } 206*ad77dbceSAneesh Kumar K.V 2077a4566b0SAneesh Kumar K.V static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name, 2087a4566b0SAneesh Kumar K.V void *buffer, size_t size, int type) 2097a4566b0SAneesh Kumar K.V { 2107a4566b0SAneesh Kumar K.V struct posix_acl *acl; 2117a4566b0SAneesh Kumar K.V int error; 2127a4566b0SAneesh Kumar K.V 2137a4566b0SAneesh Kumar K.V if (strcmp(name, "") != 0) 2147a4566b0SAneesh Kumar K.V return -EINVAL; 2157a4566b0SAneesh Kumar K.V 2167a4566b0SAneesh Kumar K.V acl = v9fs_get_cached_acl(dentry->d_inode, type); 2177a4566b0SAneesh Kumar K.V if (IS_ERR(acl)) 2187a4566b0SAneesh Kumar K.V return PTR_ERR(acl); 2197a4566b0SAneesh Kumar K.V if (acl == NULL) 2207a4566b0SAneesh Kumar K.V return -ENODATA; 2217a4566b0SAneesh Kumar K.V error = posix_acl_to_xattr(acl, buffer, size); 2227a4566b0SAneesh Kumar K.V posix_acl_release(acl); 2237a4566b0SAneesh Kumar K.V 2247a4566b0SAneesh Kumar K.V return error; 2257a4566b0SAneesh Kumar K.V } 2267a4566b0SAneesh Kumar K.V 2277a4566b0SAneesh Kumar K.V static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name, 2287a4566b0SAneesh Kumar K.V const void *value, size_t size, 2297a4566b0SAneesh Kumar K.V int flags, int type) 2307a4566b0SAneesh Kumar K.V { 23122d8dcdfSAneesh Kumar K.V int retval; 23222d8dcdfSAneesh Kumar K.V struct posix_acl *acl; 23322d8dcdfSAneesh Kumar K.V struct inode *inode = dentry->d_inode; 23422d8dcdfSAneesh Kumar K.V 23522d8dcdfSAneesh Kumar K.V if (strcmp(name, "") != 0) 23622d8dcdfSAneesh Kumar K.V return -EINVAL; 23722d8dcdfSAneesh Kumar K.V if (S_ISLNK(inode->i_mode)) 23822d8dcdfSAneesh Kumar K.V return -EOPNOTSUPP; 23922d8dcdfSAneesh Kumar K.V if (!is_owner_or_cap(inode)) 24022d8dcdfSAneesh Kumar K.V return -EPERM; 24122d8dcdfSAneesh Kumar K.V if (value) { 24222d8dcdfSAneesh Kumar K.V /* update the cached acl value */ 24322d8dcdfSAneesh Kumar K.V acl = posix_acl_from_xattr(value, size); 24422d8dcdfSAneesh Kumar K.V if (IS_ERR(acl)) 24522d8dcdfSAneesh Kumar K.V return PTR_ERR(acl); 24622d8dcdfSAneesh Kumar K.V else if (acl) { 24722d8dcdfSAneesh Kumar K.V retval = posix_acl_valid(acl); 24822d8dcdfSAneesh Kumar K.V if (retval) 24922d8dcdfSAneesh Kumar K.V goto err_out; 25022d8dcdfSAneesh Kumar K.V } 25122d8dcdfSAneesh Kumar K.V } else 25222d8dcdfSAneesh Kumar K.V acl = NULL; 25322d8dcdfSAneesh Kumar K.V 25422d8dcdfSAneesh Kumar K.V switch (type) { 25522d8dcdfSAneesh Kumar K.V case ACL_TYPE_ACCESS: 25622d8dcdfSAneesh Kumar K.V name = POSIX_ACL_XATTR_ACCESS; 25722d8dcdfSAneesh Kumar K.V if (acl) { 25822d8dcdfSAneesh Kumar K.V mode_t mode = inode->i_mode; 25922d8dcdfSAneesh Kumar K.V retval = posix_acl_equiv_mode(acl, &mode); 26022d8dcdfSAneesh Kumar K.V if (retval < 0) 26122d8dcdfSAneesh Kumar K.V goto err_out; 26222d8dcdfSAneesh Kumar K.V else { 26322d8dcdfSAneesh Kumar K.V struct iattr iattr; 26422d8dcdfSAneesh Kumar K.V if (retval == 0) { 26522d8dcdfSAneesh Kumar K.V /* 26622d8dcdfSAneesh Kumar K.V * ACL can be represented 26722d8dcdfSAneesh Kumar K.V * by the mode bits. So don't 26822d8dcdfSAneesh Kumar K.V * update ACL. 26922d8dcdfSAneesh Kumar K.V */ 27022d8dcdfSAneesh Kumar K.V acl = NULL; 27122d8dcdfSAneesh Kumar K.V value = NULL; 27222d8dcdfSAneesh Kumar K.V size = 0; 27322d8dcdfSAneesh Kumar K.V } 27422d8dcdfSAneesh Kumar K.V /* Updte the mode bits */ 27522d8dcdfSAneesh Kumar K.V iattr.ia_mode = ((mode & S_IALLUGO) | 27622d8dcdfSAneesh Kumar K.V (inode->i_mode & ~S_IALLUGO)); 27722d8dcdfSAneesh Kumar K.V iattr.ia_valid = ATTR_MODE; 27822d8dcdfSAneesh Kumar K.V /* FIXME should we update ctime ? 27922d8dcdfSAneesh Kumar K.V * What is the following setxattr update the 28022d8dcdfSAneesh Kumar K.V * mode ? 28122d8dcdfSAneesh Kumar K.V */ 28222d8dcdfSAneesh Kumar K.V v9fs_vfs_setattr_dotl(dentry, &iattr); 28322d8dcdfSAneesh Kumar K.V } 28422d8dcdfSAneesh Kumar K.V } 28522d8dcdfSAneesh Kumar K.V break; 28622d8dcdfSAneesh Kumar K.V case ACL_TYPE_DEFAULT: 28722d8dcdfSAneesh Kumar K.V name = POSIX_ACL_XATTR_DEFAULT; 28822d8dcdfSAneesh Kumar K.V if (!S_ISDIR(inode->i_mode)) { 28922d8dcdfSAneesh Kumar K.V retval = -EINVAL; 29022d8dcdfSAneesh Kumar K.V goto err_out; 29122d8dcdfSAneesh Kumar K.V } 29222d8dcdfSAneesh Kumar K.V break; 29322d8dcdfSAneesh Kumar K.V default: 29422d8dcdfSAneesh Kumar K.V BUG(); 29522d8dcdfSAneesh Kumar K.V } 29622d8dcdfSAneesh Kumar K.V retval = v9fs_xattr_set(dentry, name, value, size, flags); 29722d8dcdfSAneesh Kumar K.V if (!retval) 29822d8dcdfSAneesh Kumar K.V set_cached_acl(inode, type, acl); 29922d8dcdfSAneesh Kumar K.V err_out: 30022d8dcdfSAneesh Kumar K.V posix_acl_release(acl); 30122d8dcdfSAneesh Kumar K.V return retval; 3027a4566b0SAneesh Kumar K.V } 3037a4566b0SAneesh Kumar K.V 3047a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = { 3057a4566b0SAneesh Kumar K.V .prefix = POSIX_ACL_XATTR_ACCESS, 3067a4566b0SAneesh Kumar K.V .flags = ACL_TYPE_ACCESS, 3077a4566b0SAneesh Kumar K.V .get = v9fs_xattr_get_acl, 3087a4566b0SAneesh Kumar K.V .set = v9fs_xattr_set_acl, 3097a4566b0SAneesh Kumar K.V }; 3107a4566b0SAneesh Kumar K.V 3117a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = { 3127a4566b0SAneesh Kumar K.V .prefix = POSIX_ACL_XATTR_DEFAULT, 3137a4566b0SAneesh Kumar K.V .flags = ACL_TYPE_DEFAULT, 3147a4566b0SAneesh Kumar K.V .get = v9fs_xattr_get_acl, 3157a4566b0SAneesh Kumar K.V .set = v9fs_xattr_set_acl, 3167a4566b0SAneesh Kumar K.V }; 317