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 100*6e8dc555SAneesh Kumar K.V static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl) 101*6e8dc555SAneesh Kumar K.V { 102*6e8dc555SAneesh Kumar K.V int retval; 103*6e8dc555SAneesh Kumar K.V char *name; 104*6e8dc555SAneesh Kumar K.V size_t size; 105*6e8dc555SAneesh Kumar K.V void *buffer; 106*6e8dc555SAneesh Kumar K.V struct inode *inode = dentry->d_inode; 107*6e8dc555SAneesh Kumar K.V 108*6e8dc555SAneesh Kumar K.V set_cached_acl(inode, type, acl); 109*6e8dc555SAneesh Kumar K.V /* Set a setxattr request to server */ 110*6e8dc555SAneesh Kumar K.V size = posix_acl_xattr_size(acl->a_count); 111*6e8dc555SAneesh Kumar K.V buffer = kmalloc(size, GFP_KERNEL); 112*6e8dc555SAneesh Kumar K.V if (!buffer) 113*6e8dc555SAneesh Kumar K.V return -ENOMEM; 114*6e8dc555SAneesh Kumar K.V retval = posix_acl_to_xattr(acl, buffer, size); 115*6e8dc555SAneesh Kumar K.V if (retval < 0) 116*6e8dc555SAneesh Kumar K.V goto err_free_out; 117*6e8dc555SAneesh Kumar K.V switch (type) { 118*6e8dc555SAneesh Kumar K.V case ACL_TYPE_ACCESS: 119*6e8dc555SAneesh Kumar K.V name = POSIX_ACL_XATTR_ACCESS; 120*6e8dc555SAneesh Kumar K.V break; 121*6e8dc555SAneesh Kumar K.V case ACL_TYPE_DEFAULT: 122*6e8dc555SAneesh Kumar K.V name = POSIX_ACL_XATTR_DEFAULT; 123*6e8dc555SAneesh Kumar K.V break; 124*6e8dc555SAneesh Kumar K.V default: 125*6e8dc555SAneesh Kumar K.V BUG(); 126*6e8dc555SAneesh Kumar K.V } 127*6e8dc555SAneesh Kumar K.V retval = v9fs_xattr_set(dentry, name, buffer, size, 0); 128*6e8dc555SAneesh Kumar K.V err_free_out: 129*6e8dc555SAneesh Kumar K.V kfree(buffer); 130*6e8dc555SAneesh Kumar K.V return retval; 131*6e8dc555SAneesh Kumar K.V } 132*6e8dc555SAneesh Kumar K.V 133*6e8dc555SAneesh Kumar K.V int v9fs_acl_chmod(struct dentry *dentry) 134*6e8dc555SAneesh Kumar K.V { 135*6e8dc555SAneesh Kumar K.V int retval = 0; 136*6e8dc555SAneesh Kumar K.V struct posix_acl *acl, *clone; 137*6e8dc555SAneesh Kumar K.V struct inode *inode = dentry->d_inode; 138*6e8dc555SAneesh Kumar K.V 139*6e8dc555SAneesh Kumar K.V if (S_ISLNK(inode->i_mode)) 140*6e8dc555SAneesh Kumar K.V return -EOPNOTSUPP; 141*6e8dc555SAneesh Kumar K.V acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); 142*6e8dc555SAneesh Kumar K.V if (acl) { 143*6e8dc555SAneesh Kumar K.V clone = posix_acl_clone(acl, GFP_KERNEL); 144*6e8dc555SAneesh Kumar K.V posix_acl_release(acl); 145*6e8dc555SAneesh Kumar K.V if (!clone) 146*6e8dc555SAneesh Kumar K.V return -ENOMEM; 147*6e8dc555SAneesh Kumar K.V retval = posix_acl_chmod_masq(clone, inode->i_mode); 148*6e8dc555SAneesh Kumar K.V if (!retval) 149*6e8dc555SAneesh Kumar K.V retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone); 150*6e8dc555SAneesh Kumar K.V posix_acl_release(clone); 151*6e8dc555SAneesh Kumar K.V } 152*6e8dc555SAneesh Kumar K.V return retval; 153*6e8dc555SAneesh Kumar K.V } 154*6e8dc555SAneesh Kumar K.V 1557a4566b0SAneesh Kumar K.V static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name, 1567a4566b0SAneesh Kumar K.V void *buffer, size_t size, int type) 1577a4566b0SAneesh Kumar K.V { 1587a4566b0SAneesh Kumar K.V struct posix_acl *acl; 1597a4566b0SAneesh Kumar K.V int error; 1607a4566b0SAneesh Kumar K.V 1617a4566b0SAneesh Kumar K.V if (strcmp(name, "") != 0) 1627a4566b0SAneesh Kumar K.V return -EINVAL; 1637a4566b0SAneesh Kumar K.V 1647a4566b0SAneesh Kumar K.V acl = v9fs_get_cached_acl(dentry->d_inode, type); 1657a4566b0SAneesh Kumar K.V if (IS_ERR(acl)) 1667a4566b0SAneesh Kumar K.V return PTR_ERR(acl); 1677a4566b0SAneesh Kumar K.V if (acl == NULL) 1687a4566b0SAneesh Kumar K.V return -ENODATA; 1697a4566b0SAneesh Kumar K.V error = posix_acl_to_xattr(acl, buffer, size); 1707a4566b0SAneesh Kumar K.V posix_acl_release(acl); 1717a4566b0SAneesh Kumar K.V 1727a4566b0SAneesh Kumar K.V return error; 1737a4566b0SAneesh Kumar K.V } 1747a4566b0SAneesh Kumar K.V 1757a4566b0SAneesh Kumar K.V static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name, 1767a4566b0SAneesh Kumar K.V const void *value, size_t size, 1777a4566b0SAneesh Kumar K.V int flags, int type) 1787a4566b0SAneesh Kumar K.V { 17922d8dcdfSAneesh Kumar K.V int retval; 18022d8dcdfSAneesh Kumar K.V struct posix_acl *acl; 18122d8dcdfSAneesh Kumar K.V struct inode *inode = dentry->d_inode; 18222d8dcdfSAneesh Kumar K.V 18322d8dcdfSAneesh Kumar K.V if (strcmp(name, "") != 0) 18422d8dcdfSAneesh Kumar K.V return -EINVAL; 18522d8dcdfSAneesh Kumar K.V if (S_ISLNK(inode->i_mode)) 18622d8dcdfSAneesh Kumar K.V return -EOPNOTSUPP; 18722d8dcdfSAneesh Kumar K.V if (!is_owner_or_cap(inode)) 18822d8dcdfSAneesh Kumar K.V return -EPERM; 18922d8dcdfSAneesh Kumar K.V if (value) { 19022d8dcdfSAneesh Kumar K.V /* update the cached acl value */ 19122d8dcdfSAneesh Kumar K.V acl = posix_acl_from_xattr(value, size); 19222d8dcdfSAneesh Kumar K.V if (IS_ERR(acl)) 19322d8dcdfSAneesh Kumar K.V return PTR_ERR(acl); 19422d8dcdfSAneesh Kumar K.V else if (acl) { 19522d8dcdfSAneesh Kumar K.V retval = posix_acl_valid(acl); 19622d8dcdfSAneesh Kumar K.V if (retval) 19722d8dcdfSAneesh Kumar K.V goto err_out; 19822d8dcdfSAneesh Kumar K.V } 19922d8dcdfSAneesh Kumar K.V } else 20022d8dcdfSAneesh Kumar K.V acl = NULL; 20122d8dcdfSAneesh Kumar K.V 20222d8dcdfSAneesh Kumar K.V switch (type) { 20322d8dcdfSAneesh Kumar K.V case ACL_TYPE_ACCESS: 20422d8dcdfSAneesh Kumar K.V name = POSIX_ACL_XATTR_ACCESS; 20522d8dcdfSAneesh Kumar K.V if (acl) { 20622d8dcdfSAneesh Kumar K.V mode_t mode = inode->i_mode; 20722d8dcdfSAneesh Kumar K.V retval = posix_acl_equiv_mode(acl, &mode); 20822d8dcdfSAneesh Kumar K.V if (retval < 0) 20922d8dcdfSAneesh Kumar K.V goto err_out; 21022d8dcdfSAneesh Kumar K.V else { 21122d8dcdfSAneesh Kumar K.V struct iattr iattr; 21222d8dcdfSAneesh Kumar K.V if (retval == 0) { 21322d8dcdfSAneesh Kumar K.V /* 21422d8dcdfSAneesh Kumar K.V * ACL can be represented 21522d8dcdfSAneesh Kumar K.V * by the mode bits. So don't 21622d8dcdfSAneesh Kumar K.V * update ACL. 21722d8dcdfSAneesh Kumar K.V */ 21822d8dcdfSAneesh Kumar K.V acl = NULL; 21922d8dcdfSAneesh Kumar K.V value = NULL; 22022d8dcdfSAneesh Kumar K.V size = 0; 22122d8dcdfSAneesh Kumar K.V } 22222d8dcdfSAneesh Kumar K.V /* Updte the mode bits */ 22322d8dcdfSAneesh Kumar K.V iattr.ia_mode = ((mode & S_IALLUGO) | 22422d8dcdfSAneesh Kumar K.V (inode->i_mode & ~S_IALLUGO)); 22522d8dcdfSAneesh Kumar K.V iattr.ia_valid = ATTR_MODE; 22622d8dcdfSAneesh Kumar K.V /* FIXME should we update ctime ? 22722d8dcdfSAneesh Kumar K.V * What is the following setxattr update the 22822d8dcdfSAneesh Kumar K.V * mode ? 22922d8dcdfSAneesh Kumar K.V */ 23022d8dcdfSAneesh Kumar K.V v9fs_vfs_setattr_dotl(dentry, &iattr); 23122d8dcdfSAneesh Kumar K.V } 23222d8dcdfSAneesh Kumar K.V } 23322d8dcdfSAneesh Kumar K.V break; 23422d8dcdfSAneesh Kumar K.V case ACL_TYPE_DEFAULT: 23522d8dcdfSAneesh Kumar K.V name = POSIX_ACL_XATTR_DEFAULT; 23622d8dcdfSAneesh Kumar K.V if (!S_ISDIR(inode->i_mode)) { 23722d8dcdfSAneesh Kumar K.V retval = -EINVAL; 23822d8dcdfSAneesh Kumar K.V goto err_out; 23922d8dcdfSAneesh Kumar K.V } 24022d8dcdfSAneesh Kumar K.V break; 24122d8dcdfSAneesh Kumar K.V default: 24222d8dcdfSAneesh Kumar K.V BUG(); 24322d8dcdfSAneesh Kumar K.V } 24422d8dcdfSAneesh Kumar K.V retval = v9fs_xattr_set(dentry, name, value, size, flags); 24522d8dcdfSAneesh Kumar K.V if (!retval) 24622d8dcdfSAneesh Kumar K.V set_cached_acl(inode, type, acl); 24722d8dcdfSAneesh Kumar K.V err_out: 24822d8dcdfSAneesh Kumar K.V posix_acl_release(acl); 24922d8dcdfSAneesh Kumar K.V return retval; 2507a4566b0SAneesh Kumar K.V } 2517a4566b0SAneesh Kumar K.V 2527a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = { 2537a4566b0SAneesh Kumar K.V .prefix = POSIX_ACL_XATTR_ACCESS, 2547a4566b0SAneesh Kumar K.V .flags = ACL_TYPE_ACCESS, 2557a4566b0SAneesh Kumar K.V .get = v9fs_xattr_get_acl, 2567a4566b0SAneesh Kumar K.V .set = v9fs_xattr_set_acl, 2577a4566b0SAneesh Kumar K.V }; 2587a4566b0SAneesh Kumar K.V 2597a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = { 2607a4566b0SAneesh Kumar K.V .prefix = POSIX_ACL_XATTR_DEFAULT, 2617a4566b0SAneesh Kumar K.V .flags = ACL_TYPE_DEFAULT, 2627a4566b0SAneesh Kumar K.V .get = v9fs_xattr_get_acl, 2637a4566b0SAneesh Kumar K.V .set = v9fs_xattr_set_acl, 2647a4566b0SAneesh Kumar K.V }; 265