xref: /openbmc/linux/fs/9p/acl.c (revision 0f235caeaed6bbde6d455e172a83fa693ca485a1)
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"
2476381a42SAneesh Kumar K.V #include "v9fs.h"
255ffc0cb3SAneesh Kumar K.V #include "v9fs_vfs.h"
26*0f235caeSAl Viro #include "fid.h"
2785ff872dSAneesh Kumar K.V 
2885ff872dSAneesh Kumar K.V static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
2985ff872dSAneesh Kumar K.V {
3085ff872dSAneesh Kumar K.V 	ssize_t size;
3185ff872dSAneesh Kumar K.V 	void *value = NULL;
32009ca389SJoe Perches 	struct posix_acl *acl = NULL;
3385ff872dSAneesh Kumar K.V 
3485ff872dSAneesh Kumar K.V 	size = v9fs_fid_xattr_get(fid, name, NULL, 0);
3585ff872dSAneesh Kumar K.V 	if (size > 0) {
3685ff872dSAneesh Kumar K.V 		value = kzalloc(size, GFP_NOFS);
3785ff872dSAneesh Kumar K.V 		if (!value)
3885ff872dSAneesh Kumar K.V 			return ERR_PTR(-ENOMEM);
3985ff872dSAneesh Kumar K.V 		size = v9fs_fid_xattr_get(fid, name, value, size);
4085ff872dSAneesh Kumar K.V 		if (size > 0) {
415f3a4a28SEric W. Biederman 			acl = posix_acl_from_xattr(&init_user_ns, value, size);
4285ff872dSAneesh Kumar K.V 			if (IS_ERR(acl))
4385ff872dSAneesh Kumar K.V 				goto err_out;
4485ff872dSAneesh Kumar K.V 		}
4585ff872dSAneesh Kumar K.V 	} else if (size == -ENODATA || size == 0 ||
4685ff872dSAneesh Kumar K.V 		   size == -ENOSYS || size == -EOPNOTSUPP) {
4785ff872dSAneesh Kumar K.V 		acl = NULL;
4885ff872dSAneesh Kumar K.V 	} else
4985ff872dSAneesh Kumar K.V 		acl = ERR_PTR(-EIO);
5085ff872dSAneesh Kumar K.V 
5185ff872dSAneesh Kumar K.V err_out:
5285ff872dSAneesh Kumar K.V 	kfree(value);
5385ff872dSAneesh Kumar K.V 	return acl;
5485ff872dSAneesh Kumar K.V }
5585ff872dSAneesh Kumar K.V 
5685ff872dSAneesh Kumar K.V int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
5785ff872dSAneesh Kumar K.V {
5885ff872dSAneesh Kumar K.V 	int retval = 0;
5985ff872dSAneesh Kumar K.V 	struct posix_acl *pacl, *dacl;
6076381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
6185ff872dSAneesh Kumar K.V 
6276381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
63e782ef71SVenkateswararao Jujjuri (JV) 	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
64e782ef71SVenkateswararao Jujjuri (JV) 			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
6576381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
6676381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
6776381a42SAneesh Kumar K.V 		return 0;
6876381a42SAneesh Kumar K.V 	}
6985ff872dSAneesh Kumar K.V 	/* get the default/access acl values and cache them */
7085ff872dSAneesh Kumar K.V 	dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
7185ff872dSAneesh Kumar K.V 	pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
7285ff872dSAneesh Kumar K.V 
7385ff872dSAneesh Kumar K.V 	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
7485ff872dSAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
7585ff872dSAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
7685ff872dSAneesh Kumar K.V 	} else
7785ff872dSAneesh Kumar K.V 		retval = -EIO;
7885ff872dSAneesh Kumar K.V 
79c61fa0d6SVenkateswararao Jujjuri (JV) 	if (!IS_ERR(dacl))
80c61fa0d6SVenkateswararao Jujjuri (JV) 		posix_acl_release(dacl);
81c61fa0d6SVenkateswararao Jujjuri (JV) 
82c61fa0d6SVenkateswararao Jujjuri (JV) 	if (!IS_ERR(pacl))
83c61fa0d6SVenkateswararao Jujjuri (JV) 		posix_acl_release(pacl);
84c61fa0d6SVenkateswararao Jujjuri (JV) 
8585ff872dSAneesh Kumar K.V 	return retval;
8685ff872dSAneesh Kumar K.V }
8785ff872dSAneesh Kumar K.V 
8885ff872dSAneesh Kumar K.V static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
8985ff872dSAneesh Kumar K.V {
9085ff872dSAneesh Kumar K.V 	struct posix_acl *acl;
9185ff872dSAneesh Kumar K.V 	/*
9285ff872dSAneesh Kumar K.V 	 * 9p Always cache the acl value when
9385ff872dSAneesh Kumar K.V 	 * instantiating the inode (v9fs_inode_from_fid)
9485ff872dSAneesh Kumar K.V 	 */
9585ff872dSAneesh Kumar K.V 	acl = get_cached_acl(inode, type);
9685ff872dSAneesh Kumar K.V 	BUG_ON(acl == ACL_NOT_CACHED);
9785ff872dSAneesh Kumar K.V 	return acl;
9885ff872dSAneesh Kumar K.V }
9985ff872dSAneesh Kumar K.V 
1004e34e719SChristoph Hellwig struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type)
10185ff872dSAneesh Kumar K.V {
10276381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
10376381a42SAneesh Kumar K.V 
10476381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
105e782ef71SVenkateswararao Jujjuri (JV) 	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
106e782ef71SVenkateswararao Jujjuri (JV) 			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
10776381a42SAneesh Kumar K.V 		/*
108e782ef71SVenkateswararao Jujjuri (JV) 		 * On access = client  and acl = on mode get the acl
10976381a42SAneesh Kumar K.V 		 * values from the server
11076381a42SAneesh Kumar K.V 		 */
1114e34e719SChristoph Hellwig 		return NULL;
11276381a42SAneesh Kumar K.V 	}
1134e34e719SChristoph Hellwig 	return v9fs_get_cached_acl(inode, type);
11485ff872dSAneesh Kumar K.V 
11585ff872dSAneesh Kumar K.V }
1167a4566b0SAneesh Kumar K.V 
117*0f235caeSAl Viro static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
1186e8dc555SAneesh Kumar K.V {
1196e8dc555SAneesh Kumar K.V 	int retval;
1206e8dc555SAneesh Kumar K.V 	char *name;
1216e8dc555SAneesh Kumar K.V 	size_t size;
1226e8dc555SAneesh Kumar K.V 	void *buffer;
123d344b0fbSVenkateswararao Jujjuri (JV) 	if (!acl)
124d344b0fbSVenkateswararao Jujjuri (JV) 		return 0;
125d344b0fbSVenkateswararao Jujjuri (JV) 
1266e8dc555SAneesh Kumar K.V 	/* Set a setxattr request to server */
1276e8dc555SAneesh Kumar K.V 	size = posix_acl_xattr_size(acl->a_count);
1286e8dc555SAneesh Kumar K.V 	buffer = kmalloc(size, GFP_KERNEL);
1296e8dc555SAneesh Kumar K.V 	if (!buffer)
1306e8dc555SAneesh Kumar K.V 		return -ENOMEM;
1315f3a4a28SEric W. Biederman 	retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
1326e8dc555SAneesh Kumar K.V 	if (retval < 0)
1336e8dc555SAneesh Kumar K.V 		goto err_free_out;
1346e8dc555SAneesh Kumar K.V 	switch (type) {
1356e8dc555SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
1366e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
1376e8dc555SAneesh Kumar K.V 		break;
1386e8dc555SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
1396e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
1406e8dc555SAneesh Kumar K.V 		break;
1416e8dc555SAneesh Kumar K.V 	default:
1426e8dc555SAneesh Kumar K.V 		BUG();
1436e8dc555SAneesh Kumar K.V 	}
144*0f235caeSAl Viro 	retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
1456e8dc555SAneesh Kumar K.V err_free_out:
1466e8dc555SAneesh Kumar K.V 	kfree(buffer);
1476e8dc555SAneesh Kumar K.V 	return retval;
1486e8dc555SAneesh Kumar K.V }
1496e8dc555SAneesh Kumar K.V 
1506e8dc555SAneesh Kumar K.V int v9fs_acl_chmod(struct dentry *dentry)
1516e8dc555SAneesh Kumar K.V {
1526e8dc555SAneesh Kumar K.V 	int retval = 0;
153bc26ab5fSAl Viro 	struct posix_acl *acl;
1546e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
155*0f235caeSAl Viro 	struct p9_fid *fid = v9fs_fid_lookup(dentry);
1566e8dc555SAneesh Kumar K.V 
1576e8dc555SAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
1586e8dc555SAneesh Kumar K.V 		return -EOPNOTSUPP;
159*0f235caeSAl Viro 	if (IS_ERR(fid))
160*0f235caeSAl Viro 		return PTR_ERR(fid);
1616e8dc555SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
1626e8dc555SAneesh Kumar K.V 	if (acl) {
163bc26ab5fSAl Viro 		retval = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
164bc26ab5fSAl Viro 		if (retval)
165bc26ab5fSAl Viro 			return retval;
1667f165aaaSAl Viro 		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
167*0f235caeSAl Viro 		retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
1686e8dc555SAneesh Kumar K.V 		posix_acl_release(acl);
1696e8dc555SAneesh Kumar K.V 	}
1706e8dc555SAneesh Kumar K.V 	return retval;
1716e8dc555SAneesh Kumar K.V }
1726e8dc555SAneesh Kumar K.V 
173ad77dbceSAneesh Kumar K.V int v9fs_set_create_acl(struct dentry *dentry,
1741ec95bf3SAl Viro 			struct posix_acl **dpacl, struct posix_acl **pacl)
175ad77dbceSAneesh Kumar K.V {
1761ec95bf3SAl Viro 	if (dentry) {
177*0f235caeSAl Viro 		struct p9_fid *fid = v9fs_fid_lookup(dentry);
1787f165aaaSAl Viro 		set_cached_acl(dentry->d_inode, ACL_TYPE_DEFAULT, *dpacl);
1797f165aaaSAl Viro 		set_cached_acl(dentry->d_inode, ACL_TYPE_ACCESS, *pacl);
180*0f235caeSAl Viro 		if (!IS_ERR(fid)) {
181*0f235caeSAl Viro 			v9fs_set_acl(fid, ACL_TYPE_DEFAULT, *dpacl);
182*0f235caeSAl Viro 			v9fs_set_acl(fid, ACL_TYPE_ACCESS, *pacl);
183*0f235caeSAl Viro 		}
1841ec95bf3SAl Viro 	}
1851ec95bf3SAl Viro 	posix_acl_release(*dpacl);
1861ec95bf3SAl Viro 	posix_acl_release(*pacl);
1871ec95bf3SAl Viro 	*dpacl = *pacl = NULL;
188ad77dbceSAneesh Kumar K.V 	return 0;
189ad77dbceSAneesh Kumar K.V }
190ad77dbceSAneesh Kumar K.V 
191d3fb6120SAl Viro int v9fs_acl_mode(struct inode *dir, umode_t *modep,
192ad77dbceSAneesh Kumar K.V 		  struct posix_acl **dpacl, struct posix_acl **pacl)
193ad77dbceSAneesh Kumar K.V {
194ad77dbceSAneesh Kumar K.V 	int retval = 0;
195d3fb6120SAl Viro 	umode_t mode = *modep;
196ad77dbceSAneesh Kumar K.V 	struct posix_acl *acl = NULL;
197ad77dbceSAneesh Kumar K.V 
198ad77dbceSAneesh Kumar K.V 	if (!S_ISLNK(mode)) {
199ad77dbceSAneesh Kumar K.V 		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
200ad77dbceSAneesh Kumar K.V 		if (IS_ERR(acl))
201ad77dbceSAneesh Kumar K.V 			return PTR_ERR(acl);
202ad77dbceSAneesh Kumar K.V 		if (!acl)
203ad77dbceSAneesh Kumar K.V 			mode &= ~current_umask();
204ad77dbceSAneesh Kumar K.V 	}
205ad77dbceSAneesh Kumar K.V 	if (acl) {
206ad77dbceSAneesh Kumar K.V 		if (S_ISDIR(mode))
2071ec95bf3SAl Viro 			*dpacl = posix_acl_dup(acl);
208826cae2fSAl Viro 		retval = posix_acl_create(&acl, GFP_NOFS, &mode);
209826cae2fSAl Viro 		if (retval < 0)
210826cae2fSAl Viro 			return retval;
211ad77dbceSAneesh Kumar K.V 		if (retval > 0)
212826cae2fSAl Viro 			*pacl = acl;
2131ec95bf3SAl Viro 		else
214826cae2fSAl Viro 			posix_acl_release(acl);
215ad77dbceSAneesh Kumar K.V 	}
216ad77dbceSAneesh Kumar K.V 	*modep  = mode;
217ad77dbceSAneesh Kumar K.V 	return 0;
218ad77dbceSAneesh Kumar K.V }
219ad77dbceSAneesh Kumar K.V 
22076381a42SAneesh Kumar K.V static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
22176381a42SAneesh Kumar K.V 			       void *buffer, size_t size, int type)
22276381a42SAneesh Kumar K.V {
22376381a42SAneesh Kumar K.V 	char *full_name;
22476381a42SAneesh Kumar K.V 
22576381a42SAneesh Kumar K.V 	switch (type) {
22676381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
22776381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
22876381a42SAneesh Kumar K.V 		break;
22976381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
23076381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
23176381a42SAneesh Kumar K.V 		break;
23276381a42SAneesh Kumar K.V 	default:
23376381a42SAneesh Kumar K.V 		BUG();
23476381a42SAneesh Kumar K.V 	}
23576381a42SAneesh Kumar K.V 	return v9fs_xattr_get(dentry, full_name, buffer, size);
23676381a42SAneesh Kumar K.V }
23776381a42SAneesh Kumar K.V 
2387a4566b0SAneesh Kumar K.V static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
2397a4566b0SAneesh Kumar K.V 			      void *buffer, size_t size, int type)
2407a4566b0SAneesh Kumar K.V {
24176381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
2427a4566b0SAneesh Kumar K.V 	struct posix_acl *acl;
2437a4566b0SAneesh Kumar K.V 	int error;
2447a4566b0SAneesh Kumar K.V 
2457a4566b0SAneesh Kumar K.V 	if (strcmp(name, "") != 0)
2467a4566b0SAneesh Kumar K.V 		return -EINVAL;
2477a4566b0SAneesh Kumar K.V 
24842869c8aSAneesh Kumar K.V 	v9ses = v9fs_dentry2v9ses(dentry);
24976381a42SAneesh Kumar K.V 	/*
25076381a42SAneesh Kumar K.V 	 * We allow set/get/list of acl when access=client is not specified
25176381a42SAneesh Kumar K.V 	 */
25276381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
25376381a42SAneesh Kumar K.V 		return v9fs_remote_get_acl(dentry, name, buffer, size, type);
25476381a42SAneesh Kumar K.V 
2557a4566b0SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(dentry->d_inode, type);
2567a4566b0SAneesh Kumar K.V 	if (IS_ERR(acl))
2577a4566b0SAneesh Kumar K.V 		return PTR_ERR(acl);
2587a4566b0SAneesh Kumar K.V 	if (acl == NULL)
2597a4566b0SAneesh Kumar K.V 		return -ENODATA;
2605f3a4a28SEric W. Biederman 	error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
2617a4566b0SAneesh Kumar K.V 	posix_acl_release(acl);
2627a4566b0SAneesh Kumar K.V 
2637a4566b0SAneesh Kumar K.V 	return error;
2647a4566b0SAneesh Kumar K.V }
2657a4566b0SAneesh Kumar K.V 
26676381a42SAneesh Kumar K.V static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
26776381a42SAneesh Kumar K.V 			      const void *value, size_t size,
26876381a42SAneesh Kumar K.V 			      int flags, int type)
26976381a42SAneesh Kumar K.V {
27076381a42SAneesh Kumar K.V 	char *full_name;
27176381a42SAneesh Kumar K.V 
27276381a42SAneesh Kumar K.V 	switch (type) {
27376381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
27476381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
27576381a42SAneesh Kumar K.V 		break;
27676381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
27776381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
27876381a42SAneesh Kumar K.V 		break;
27976381a42SAneesh Kumar K.V 	default:
28076381a42SAneesh Kumar K.V 		BUG();
28176381a42SAneesh Kumar K.V 	}
28276381a42SAneesh Kumar K.V 	return v9fs_xattr_set(dentry, full_name, value, size, flags);
28376381a42SAneesh Kumar K.V }
28476381a42SAneesh Kumar K.V 
28576381a42SAneesh Kumar K.V 
2867a4566b0SAneesh Kumar K.V static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
2877a4566b0SAneesh Kumar K.V 			      const void *value, size_t size,
2887a4566b0SAneesh Kumar K.V 			      int flags, int type)
2897a4566b0SAneesh Kumar K.V {
29022d8dcdfSAneesh Kumar K.V 	int retval;
29122d8dcdfSAneesh Kumar K.V 	struct posix_acl *acl;
29276381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
29322d8dcdfSAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
29422d8dcdfSAneesh Kumar K.V 
29522d8dcdfSAneesh Kumar K.V 	if (strcmp(name, "") != 0)
29622d8dcdfSAneesh Kumar K.V 		return -EINVAL;
29776381a42SAneesh Kumar K.V 
29842869c8aSAneesh Kumar K.V 	v9ses = v9fs_dentry2v9ses(dentry);
29976381a42SAneesh Kumar K.V 	/*
30076381a42SAneesh Kumar K.V 	 * set the attribute on the remote. Without even looking at the
30176381a42SAneesh Kumar K.V 	 * xattr value. We leave it to the server to validate
30276381a42SAneesh Kumar K.V 	 */
30376381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
30476381a42SAneesh Kumar K.V 		return v9fs_remote_set_acl(dentry, name,
30576381a42SAneesh Kumar K.V 					   value, size, flags, type);
30676381a42SAneesh Kumar K.V 
30722d8dcdfSAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
30822d8dcdfSAneesh Kumar K.V 		return -EOPNOTSUPP;
3092e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
31022d8dcdfSAneesh Kumar K.V 		return -EPERM;
31122d8dcdfSAneesh Kumar K.V 	if (value) {
31222d8dcdfSAneesh Kumar K.V 		/* update the cached acl value */
3135f3a4a28SEric W. Biederman 		acl = posix_acl_from_xattr(&init_user_ns, value, size);
31422d8dcdfSAneesh Kumar K.V 		if (IS_ERR(acl))
31522d8dcdfSAneesh Kumar K.V 			return PTR_ERR(acl);
31622d8dcdfSAneesh Kumar K.V 		else if (acl) {
31722d8dcdfSAneesh Kumar K.V 			retval = posix_acl_valid(acl);
31822d8dcdfSAneesh Kumar K.V 			if (retval)
31922d8dcdfSAneesh Kumar K.V 				goto err_out;
32022d8dcdfSAneesh Kumar K.V 		}
32122d8dcdfSAneesh Kumar K.V 	} else
32222d8dcdfSAneesh Kumar K.V 		acl = NULL;
32322d8dcdfSAneesh Kumar K.V 
32422d8dcdfSAneesh Kumar K.V 	switch (type) {
32522d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
32622d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
32722d8dcdfSAneesh Kumar K.V 		if (acl) {
328d6952123SAl Viro 			umode_t mode = inode->i_mode;
32922d8dcdfSAneesh Kumar K.V 			retval = posix_acl_equiv_mode(acl, &mode);
33022d8dcdfSAneesh Kumar K.V 			if (retval < 0)
33122d8dcdfSAneesh Kumar K.V 				goto err_out;
33222d8dcdfSAneesh Kumar K.V 			else {
33322d8dcdfSAneesh Kumar K.V 				struct iattr iattr;
33422d8dcdfSAneesh Kumar K.V 				if (retval == 0) {
33522d8dcdfSAneesh Kumar K.V 					/*
33622d8dcdfSAneesh Kumar K.V 					 * ACL can be represented
33722d8dcdfSAneesh Kumar K.V 					 * by the mode bits. So don't
33822d8dcdfSAneesh Kumar K.V 					 * update ACL.
33922d8dcdfSAneesh Kumar K.V 					 */
34022d8dcdfSAneesh Kumar K.V 					acl = NULL;
34122d8dcdfSAneesh Kumar K.V 					value = NULL;
34222d8dcdfSAneesh Kumar K.V 					size = 0;
34322d8dcdfSAneesh Kumar K.V 				}
34422d8dcdfSAneesh Kumar K.V 				/* Updte the mode bits */
34522d8dcdfSAneesh Kumar K.V 				iattr.ia_mode = ((mode & S_IALLUGO) |
34622d8dcdfSAneesh Kumar K.V 						 (inode->i_mode & ~S_IALLUGO));
34722d8dcdfSAneesh Kumar K.V 				iattr.ia_valid = ATTR_MODE;
34822d8dcdfSAneesh Kumar K.V 				/* FIXME should we update ctime ?
34922d8dcdfSAneesh Kumar K.V 				 * What is the following setxattr update the
35022d8dcdfSAneesh Kumar K.V 				 * mode ?
35122d8dcdfSAneesh Kumar K.V 				 */
35222d8dcdfSAneesh Kumar K.V 				v9fs_vfs_setattr_dotl(dentry, &iattr);
35322d8dcdfSAneesh Kumar K.V 			}
35422d8dcdfSAneesh Kumar K.V 		}
35522d8dcdfSAneesh Kumar K.V 		break;
35622d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
35722d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
35822d8dcdfSAneesh Kumar K.V 		if (!S_ISDIR(inode->i_mode)) {
3596f81c115SAneesh Kumar K.V 			retval = acl ? -EINVAL : 0;
36022d8dcdfSAneesh Kumar K.V 			goto err_out;
36122d8dcdfSAneesh Kumar K.V 		}
36222d8dcdfSAneesh Kumar K.V 		break;
36322d8dcdfSAneesh Kumar K.V 	default:
36422d8dcdfSAneesh Kumar K.V 		BUG();
36522d8dcdfSAneesh Kumar K.V 	}
36622d8dcdfSAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, value, size, flags);
36722d8dcdfSAneesh Kumar K.V 	if (!retval)
36822d8dcdfSAneesh Kumar K.V 		set_cached_acl(inode, type, acl);
36922d8dcdfSAneesh Kumar K.V err_out:
37022d8dcdfSAneesh Kumar K.V 	posix_acl_release(acl);
37122d8dcdfSAneesh Kumar K.V 	return retval;
3727a4566b0SAneesh Kumar K.V }
3737a4566b0SAneesh Kumar K.V 
3747a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = {
3757a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_ACCESS,
3767a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_ACCESS,
3777a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
3787a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
3797a4566b0SAneesh Kumar K.V };
3807a4566b0SAneesh Kumar K.V 
3817a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = {
3827a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_DEFAULT,
3837a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_DEFAULT,
3847a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
3857a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
3867a4566b0SAneesh Kumar K.V };
387