xref: /openbmc/linux/fs/9p/acl.c (revision 76381a42e4a5606774fd48413e6282cd7130ff2c)
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"
25*76381a42SAneesh Kumar K.V #include "v9fs.h"
2685ff872dSAneesh Kumar K.V 
2785ff872dSAneesh Kumar K.V static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
2885ff872dSAneesh Kumar K.V {
2985ff872dSAneesh Kumar K.V 	ssize_t size;
3085ff872dSAneesh Kumar K.V 	void *value = NULL;
3185ff872dSAneesh Kumar K.V 	struct posix_acl *acl = NULL;;
3285ff872dSAneesh Kumar K.V 
3385ff872dSAneesh Kumar K.V 	size = v9fs_fid_xattr_get(fid, name, NULL, 0);
3485ff872dSAneesh Kumar K.V 	if (size > 0) {
3585ff872dSAneesh Kumar K.V 		value = kzalloc(size, GFP_NOFS);
3685ff872dSAneesh Kumar K.V 		if (!value)
3785ff872dSAneesh Kumar K.V 			return ERR_PTR(-ENOMEM);
3885ff872dSAneesh Kumar K.V 		size = v9fs_fid_xattr_get(fid, name, value, size);
3985ff872dSAneesh Kumar K.V 		if (size > 0) {
4085ff872dSAneesh Kumar K.V 			acl = posix_acl_from_xattr(value, size);
4185ff872dSAneesh Kumar K.V 			if (IS_ERR(acl))
4285ff872dSAneesh Kumar K.V 				goto err_out;
4385ff872dSAneesh Kumar K.V 		}
4485ff872dSAneesh Kumar K.V 	} else if (size == -ENODATA || size == 0 ||
4585ff872dSAneesh Kumar K.V 		   size == -ENOSYS || size == -EOPNOTSUPP) {
4685ff872dSAneesh Kumar K.V 		acl = NULL;
4785ff872dSAneesh Kumar K.V 	} else
4885ff872dSAneesh Kumar K.V 		acl = ERR_PTR(-EIO);
4985ff872dSAneesh Kumar K.V 
5085ff872dSAneesh Kumar K.V err_out:
5185ff872dSAneesh Kumar K.V 	kfree(value);
5285ff872dSAneesh Kumar K.V 	return acl;
5385ff872dSAneesh Kumar K.V }
5485ff872dSAneesh Kumar K.V 
5585ff872dSAneesh Kumar K.V int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
5685ff872dSAneesh Kumar K.V {
5785ff872dSAneesh Kumar K.V 	int retval = 0;
5885ff872dSAneesh Kumar K.V 	struct posix_acl *pacl, *dacl;
59*76381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
6085ff872dSAneesh Kumar K.V 
61*76381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
62*76381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
63*76381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
64*76381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
65*76381a42SAneesh Kumar K.V 		return 0;
66*76381a42SAneesh Kumar K.V 	}
6785ff872dSAneesh Kumar K.V 	/* get the default/access acl values and cache them */
6885ff872dSAneesh Kumar K.V 	dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
6985ff872dSAneesh Kumar K.V 	pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
7085ff872dSAneesh Kumar K.V 
7185ff872dSAneesh Kumar K.V 	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
7285ff872dSAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
7385ff872dSAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
7485ff872dSAneesh Kumar K.V 		posix_acl_release(dacl);
7585ff872dSAneesh Kumar K.V 		posix_acl_release(pacl);
7685ff872dSAneesh Kumar K.V 	} else
7785ff872dSAneesh Kumar K.V 		retval = -EIO;
7885ff872dSAneesh Kumar K.V 
7985ff872dSAneesh Kumar K.V 	return retval;
8085ff872dSAneesh Kumar K.V }
8185ff872dSAneesh Kumar K.V 
8285ff872dSAneesh Kumar K.V static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
8385ff872dSAneesh Kumar K.V {
8485ff872dSAneesh Kumar K.V 	struct posix_acl *acl;
8585ff872dSAneesh Kumar K.V 	/*
8685ff872dSAneesh Kumar K.V 	 * 9p Always cache the acl value when
8785ff872dSAneesh Kumar K.V 	 * instantiating the inode (v9fs_inode_from_fid)
8885ff872dSAneesh Kumar K.V 	 */
8985ff872dSAneesh Kumar K.V 	acl = get_cached_acl(inode, type);
9085ff872dSAneesh Kumar K.V 	BUG_ON(acl == ACL_NOT_CACHED);
9185ff872dSAneesh Kumar K.V 	return acl;
9285ff872dSAneesh Kumar K.V }
9385ff872dSAneesh Kumar K.V 
9485ff872dSAneesh Kumar K.V int v9fs_check_acl(struct inode *inode, int mask)
9585ff872dSAneesh Kumar K.V {
96*76381a42SAneesh Kumar K.V 	struct posix_acl *acl;
97*76381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
98*76381a42SAneesh Kumar K.V 
99*76381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
100*76381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
101*76381a42SAneesh Kumar K.V 		/*
102*76381a42SAneesh Kumar K.V 		 * On access = client mode get the acl
103*76381a42SAneesh Kumar K.V 		 * values from the server
104*76381a42SAneesh Kumar K.V 		 */
105*76381a42SAneesh Kumar K.V 		return 0;
106*76381a42SAneesh Kumar K.V 	}
107*76381a42SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
10885ff872dSAneesh Kumar K.V 
10985ff872dSAneesh Kumar K.V 	if (IS_ERR(acl))
11085ff872dSAneesh Kumar K.V 		return PTR_ERR(acl);
11185ff872dSAneesh Kumar K.V 	if (acl) {
11285ff872dSAneesh Kumar K.V 		int error = posix_acl_permission(inode, acl, mask);
11385ff872dSAneesh Kumar K.V 		posix_acl_release(acl);
11485ff872dSAneesh Kumar K.V 		return error;
11585ff872dSAneesh Kumar K.V 	}
11685ff872dSAneesh Kumar K.V 	return -EAGAIN;
11785ff872dSAneesh Kumar K.V }
1187a4566b0SAneesh Kumar K.V 
1196e8dc555SAneesh Kumar K.V static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
1206e8dc555SAneesh Kumar K.V {
1216e8dc555SAneesh Kumar K.V 	int retval;
1226e8dc555SAneesh Kumar K.V 	char *name;
1236e8dc555SAneesh Kumar K.V 	size_t size;
1246e8dc555SAneesh Kumar K.V 	void *buffer;
1256e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
1266e8dc555SAneesh Kumar K.V 
1276e8dc555SAneesh Kumar K.V 	set_cached_acl(inode, type, acl);
1286e8dc555SAneesh Kumar K.V 	/* Set a setxattr request to server */
1296e8dc555SAneesh Kumar K.V 	size = posix_acl_xattr_size(acl->a_count);
1306e8dc555SAneesh Kumar K.V 	buffer = kmalloc(size, GFP_KERNEL);
1316e8dc555SAneesh Kumar K.V 	if (!buffer)
1326e8dc555SAneesh Kumar K.V 		return -ENOMEM;
1336e8dc555SAneesh Kumar K.V 	retval = posix_acl_to_xattr(acl, buffer, size);
1346e8dc555SAneesh Kumar K.V 	if (retval < 0)
1356e8dc555SAneesh Kumar K.V 		goto err_free_out;
1366e8dc555SAneesh Kumar K.V 	switch (type) {
1376e8dc555SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
1386e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
1396e8dc555SAneesh Kumar K.V 		break;
1406e8dc555SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
1416e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
1426e8dc555SAneesh Kumar K.V 		break;
1436e8dc555SAneesh Kumar K.V 	default:
1446e8dc555SAneesh Kumar K.V 		BUG();
1456e8dc555SAneesh Kumar K.V 	}
1466e8dc555SAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
1476e8dc555SAneesh Kumar K.V err_free_out:
1486e8dc555SAneesh Kumar K.V 	kfree(buffer);
1496e8dc555SAneesh Kumar K.V 	return retval;
1506e8dc555SAneesh Kumar K.V }
1516e8dc555SAneesh Kumar K.V 
1526e8dc555SAneesh Kumar K.V int v9fs_acl_chmod(struct dentry *dentry)
1536e8dc555SAneesh Kumar K.V {
1546e8dc555SAneesh Kumar K.V 	int retval = 0;
1556e8dc555SAneesh Kumar K.V 	struct posix_acl *acl, *clone;
1566e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
1576e8dc555SAneesh Kumar K.V 
1586e8dc555SAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
1596e8dc555SAneesh Kumar K.V 		return -EOPNOTSUPP;
1606e8dc555SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
1616e8dc555SAneesh Kumar K.V 	if (acl) {
1626e8dc555SAneesh Kumar K.V 		clone = posix_acl_clone(acl, GFP_KERNEL);
1636e8dc555SAneesh Kumar K.V 		posix_acl_release(acl);
1646e8dc555SAneesh Kumar K.V 		if (!clone)
1656e8dc555SAneesh Kumar K.V 			return -ENOMEM;
1666e8dc555SAneesh Kumar K.V 		retval = posix_acl_chmod_masq(clone, inode->i_mode);
1676e8dc555SAneesh Kumar K.V 		if (!retval)
1686e8dc555SAneesh Kumar K.V 			retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
1696e8dc555SAneesh Kumar K.V 		posix_acl_release(clone);
1706e8dc555SAneesh Kumar K.V 	}
1716e8dc555SAneesh Kumar K.V 	return retval;
1726e8dc555SAneesh Kumar K.V }
1736e8dc555SAneesh Kumar K.V 
174ad77dbceSAneesh Kumar K.V int v9fs_set_create_acl(struct dentry *dentry,
175ad77dbceSAneesh Kumar K.V 			struct posix_acl *dpacl, struct posix_acl *pacl)
176ad77dbceSAneesh Kumar K.V {
177ad77dbceSAneesh Kumar K.V 	if (dpacl)
178ad77dbceSAneesh Kumar K.V 		v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
179ad77dbceSAneesh Kumar K.V 	if (pacl)
180ad77dbceSAneesh Kumar K.V 		v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
181ad77dbceSAneesh Kumar K.V 	posix_acl_release(dpacl);
182ad77dbceSAneesh Kumar K.V 	posix_acl_release(pacl);
183ad77dbceSAneesh Kumar K.V 	return 0;
184ad77dbceSAneesh Kumar K.V }
185ad77dbceSAneesh Kumar K.V 
186ad77dbceSAneesh Kumar K.V int v9fs_acl_mode(struct inode *dir, mode_t *modep,
187ad77dbceSAneesh Kumar K.V 		  struct posix_acl **dpacl, struct posix_acl **pacl)
188ad77dbceSAneesh Kumar K.V {
189ad77dbceSAneesh Kumar K.V 	int retval = 0;
190ad77dbceSAneesh Kumar K.V 	mode_t mode = *modep;
191ad77dbceSAneesh Kumar K.V 	struct posix_acl *acl = NULL;
192ad77dbceSAneesh Kumar K.V 
193ad77dbceSAneesh Kumar K.V 	if (!S_ISLNK(mode)) {
194ad77dbceSAneesh Kumar K.V 		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
195ad77dbceSAneesh Kumar K.V 		if (IS_ERR(acl))
196ad77dbceSAneesh Kumar K.V 			return PTR_ERR(acl);
197ad77dbceSAneesh Kumar K.V 		if (!acl)
198ad77dbceSAneesh Kumar K.V 			mode &= ~current_umask();
199ad77dbceSAneesh Kumar K.V 	}
200ad77dbceSAneesh Kumar K.V 	if (acl) {
201ad77dbceSAneesh Kumar K.V 		struct posix_acl *clone;
202ad77dbceSAneesh Kumar K.V 
203ad77dbceSAneesh Kumar K.V 		if (S_ISDIR(mode))
204ad77dbceSAneesh Kumar K.V 			*dpacl = acl;
205ad77dbceSAneesh Kumar K.V 		clone = posix_acl_clone(acl, GFP_NOFS);
206ad77dbceSAneesh Kumar K.V 		retval = -ENOMEM;
207ad77dbceSAneesh Kumar K.V 		if (!clone)
208ad77dbceSAneesh Kumar K.V 			goto cleanup;
209ad77dbceSAneesh Kumar K.V 
210ad77dbceSAneesh Kumar K.V 		retval = posix_acl_create_masq(clone, &mode);
211ad77dbceSAneesh Kumar K.V 		if (retval < 0) {
212ad77dbceSAneesh Kumar K.V 			posix_acl_release(clone);
213ad77dbceSAneesh Kumar K.V 			goto cleanup;
214ad77dbceSAneesh Kumar K.V 		}
215ad77dbceSAneesh Kumar K.V 		if (retval > 0)
216ad77dbceSAneesh Kumar K.V 			*pacl = clone;
217ad77dbceSAneesh Kumar K.V 	}
218ad77dbceSAneesh Kumar K.V 	*modep  = mode;
219ad77dbceSAneesh Kumar K.V 	return 0;
220ad77dbceSAneesh Kumar K.V cleanup:
221ad77dbceSAneesh Kumar K.V 	posix_acl_release(acl);
222ad77dbceSAneesh Kumar K.V 	return retval;
223ad77dbceSAneesh Kumar K.V 
224ad77dbceSAneesh Kumar K.V }
225ad77dbceSAneesh Kumar K.V 
226*76381a42SAneesh Kumar K.V static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
227*76381a42SAneesh Kumar K.V 			       void *buffer, size_t size, int type)
228*76381a42SAneesh Kumar K.V {
229*76381a42SAneesh Kumar K.V 	char *full_name;
230*76381a42SAneesh Kumar K.V 
231*76381a42SAneesh Kumar K.V 	switch (type) {
232*76381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
233*76381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
234*76381a42SAneesh Kumar K.V 		break;
235*76381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
236*76381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
237*76381a42SAneesh Kumar K.V 		break;
238*76381a42SAneesh Kumar K.V 	default:
239*76381a42SAneesh Kumar K.V 		BUG();
240*76381a42SAneesh Kumar K.V 	}
241*76381a42SAneesh Kumar K.V 	return v9fs_xattr_get(dentry, full_name, buffer, size);
242*76381a42SAneesh Kumar K.V }
243*76381a42SAneesh Kumar K.V 
2447a4566b0SAneesh Kumar K.V static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
2457a4566b0SAneesh Kumar K.V 			      void *buffer, size_t size, int type)
2467a4566b0SAneesh Kumar K.V {
247*76381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
2487a4566b0SAneesh Kumar K.V 	struct posix_acl *acl;
2497a4566b0SAneesh Kumar K.V 	int error;
2507a4566b0SAneesh Kumar K.V 
2517a4566b0SAneesh Kumar K.V 	if (strcmp(name, "") != 0)
2527a4566b0SAneesh Kumar K.V 		return -EINVAL;
2537a4566b0SAneesh Kumar K.V 
254*76381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
255*76381a42SAneesh Kumar K.V 	/*
256*76381a42SAneesh Kumar K.V 	 * We allow set/get/list of acl when access=client is not specified
257*76381a42SAneesh Kumar K.V 	 */
258*76381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
259*76381a42SAneesh Kumar K.V 		return v9fs_remote_get_acl(dentry, name, buffer, size, type);
260*76381a42SAneesh Kumar K.V 
2617a4566b0SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(dentry->d_inode, type);
2627a4566b0SAneesh Kumar K.V 	if (IS_ERR(acl))
2637a4566b0SAneesh Kumar K.V 		return PTR_ERR(acl);
2647a4566b0SAneesh Kumar K.V 	if (acl == NULL)
2657a4566b0SAneesh Kumar K.V 		return -ENODATA;
2667a4566b0SAneesh Kumar K.V 	error = posix_acl_to_xattr(acl, buffer, size);
2677a4566b0SAneesh Kumar K.V 	posix_acl_release(acl);
2687a4566b0SAneesh Kumar K.V 
2697a4566b0SAneesh Kumar K.V 	return error;
2707a4566b0SAneesh Kumar K.V }
2717a4566b0SAneesh Kumar K.V 
272*76381a42SAneesh Kumar K.V static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
273*76381a42SAneesh Kumar K.V 			      const void *value, size_t size,
274*76381a42SAneesh Kumar K.V 			      int flags, int type)
275*76381a42SAneesh Kumar K.V {
276*76381a42SAneesh Kumar K.V 	char *full_name;
277*76381a42SAneesh Kumar K.V 
278*76381a42SAneesh Kumar K.V 	switch (type) {
279*76381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
280*76381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
281*76381a42SAneesh Kumar K.V 		break;
282*76381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
283*76381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
284*76381a42SAneesh Kumar K.V 		break;
285*76381a42SAneesh Kumar K.V 	default:
286*76381a42SAneesh Kumar K.V 		BUG();
287*76381a42SAneesh Kumar K.V 	}
288*76381a42SAneesh Kumar K.V 	return v9fs_xattr_set(dentry, full_name, value, size, flags);
289*76381a42SAneesh Kumar K.V }
290*76381a42SAneesh Kumar K.V 
291*76381a42SAneesh Kumar K.V 
2927a4566b0SAneesh Kumar K.V static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
2937a4566b0SAneesh Kumar K.V 			      const void *value, size_t size,
2947a4566b0SAneesh Kumar K.V 			      int flags, int type)
2957a4566b0SAneesh Kumar K.V {
29622d8dcdfSAneesh Kumar K.V 	int retval;
29722d8dcdfSAneesh Kumar K.V 	struct posix_acl *acl;
298*76381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
29922d8dcdfSAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
30022d8dcdfSAneesh Kumar K.V 
30122d8dcdfSAneesh Kumar K.V 	if (strcmp(name, "") != 0)
30222d8dcdfSAneesh Kumar K.V 		return -EINVAL;
303*76381a42SAneesh Kumar K.V 
304*76381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
305*76381a42SAneesh Kumar K.V 	/*
306*76381a42SAneesh Kumar K.V 	 * set the attribute on the remote. Without even looking at the
307*76381a42SAneesh Kumar K.V 	 * xattr value. We leave it to the server to validate
308*76381a42SAneesh Kumar K.V 	 */
309*76381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
310*76381a42SAneesh Kumar K.V 		return v9fs_remote_set_acl(dentry, name,
311*76381a42SAneesh Kumar K.V 					   value, size, flags, type);
312*76381a42SAneesh Kumar K.V 
31322d8dcdfSAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
31422d8dcdfSAneesh Kumar K.V 		return -EOPNOTSUPP;
31522d8dcdfSAneesh Kumar K.V 	if (!is_owner_or_cap(inode))
31622d8dcdfSAneesh Kumar K.V 		return -EPERM;
31722d8dcdfSAneesh Kumar K.V 	if (value) {
31822d8dcdfSAneesh Kumar K.V 		/* update the cached acl value */
31922d8dcdfSAneesh Kumar K.V 		acl = posix_acl_from_xattr(value, size);
32022d8dcdfSAneesh Kumar K.V 		if (IS_ERR(acl))
32122d8dcdfSAneesh Kumar K.V 			return PTR_ERR(acl);
32222d8dcdfSAneesh Kumar K.V 		else if (acl) {
32322d8dcdfSAneesh Kumar K.V 			retval = posix_acl_valid(acl);
32422d8dcdfSAneesh Kumar K.V 			if (retval)
32522d8dcdfSAneesh Kumar K.V 				goto err_out;
32622d8dcdfSAneesh Kumar K.V 		}
32722d8dcdfSAneesh Kumar K.V 	} else
32822d8dcdfSAneesh Kumar K.V 		acl = NULL;
32922d8dcdfSAneesh Kumar K.V 
33022d8dcdfSAneesh Kumar K.V 	switch (type) {
33122d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
33222d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
33322d8dcdfSAneesh Kumar K.V 		if (acl) {
33422d8dcdfSAneesh Kumar K.V 			mode_t mode = inode->i_mode;
33522d8dcdfSAneesh Kumar K.V 			retval = posix_acl_equiv_mode(acl, &mode);
33622d8dcdfSAneesh Kumar K.V 			if (retval < 0)
33722d8dcdfSAneesh Kumar K.V 				goto err_out;
33822d8dcdfSAneesh Kumar K.V 			else {
33922d8dcdfSAneesh Kumar K.V 				struct iattr iattr;
34022d8dcdfSAneesh Kumar K.V 				if (retval == 0) {
34122d8dcdfSAneesh Kumar K.V 					/*
34222d8dcdfSAneesh Kumar K.V 					 * ACL can be represented
34322d8dcdfSAneesh Kumar K.V 					 * by the mode bits. So don't
34422d8dcdfSAneesh Kumar K.V 					 * update ACL.
34522d8dcdfSAneesh Kumar K.V 					 */
34622d8dcdfSAneesh Kumar K.V 					acl = NULL;
34722d8dcdfSAneesh Kumar K.V 					value = NULL;
34822d8dcdfSAneesh Kumar K.V 					size = 0;
34922d8dcdfSAneesh Kumar K.V 				}
35022d8dcdfSAneesh Kumar K.V 				/* Updte the mode bits */
35122d8dcdfSAneesh Kumar K.V 				iattr.ia_mode = ((mode & S_IALLUGO) |
35222d8dcdfSAneesh Kumar K.V 						 (inode->i_mode & ~S_IALLUGO));
35322d8dcdfSAneesh Kumar K.V 				iattr.ia_valid = ATTR_MODE;
35422d8dcdfSAneesh Kumar K.V 				/* FIXME should we update ctime ?
35522d8dcdfSAneesh Kumar K.V 				 * What is the following setxattr update the
35622d8dcdfSAneesh Kumar K.V 				 * mode ?
35722d8dcdfSAneesh Kumar K.V 				 */
35822d8dcdfSAneesh Kumar K.V 				v9fs_vfs_setattr_dotl(dentry, &iattr);
35922d8dcdfSAneesh Kumar K.V 			}
36022d8dcdfSAneesh Kumar K.V 		}
36122d8dcdfSAneesh Kumar K.V 		break;
36222d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
36322d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
36422d8dcdfSAneesh Kumar K.V 		if (!S_ISDIR(inode->i_mode)) {
36522d8dcdfSAneesh Kumar K.V 			retval = -EINVAL;
36622d8dcdfSAneesh Kumar K.V 			goto err_out;
36722d8dcdfSAneesh Kumar K.V 		}
36822d8dcdfSAneesh Kumar K.V 		break;
36922d8dcdfSAneesh Kumar K.V 	default:
37022d8dcdfSAneesh Kumar K.V 		BUG();
37122d8dcdfSAneesh Kumar K.V 	}
37222d8dcdfSAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, value, size, flags);
37322d8dcdfSAneesh Kumar K.V 	if (!retval)
37422d8dcdfSAneesh Kumar K.V 		set_cached_acl(inode, type, acl);
37522d8dcdfSAneesh Kumar K.V err_out:
37622d8dcdfSAneesh Kumar K.V 	posix_acl_release(acl);
37722d8dcdfSAneesh Kumar K.V 	return retval;
3787a4566b0SAneesh Kumar K.V }
3797a4566b0SAneesh Kumar K.V 
3807a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = {
3817a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_ACCESS,
3827a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_ACCESS,
3837a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
3847a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
3857a4566b0SAneesh Kumar K.V };
3867a4566b0SAneesh Kumar K.V 
3877a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = {
3887a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_DEFAULT,
3897a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_DEFAULT,
3907a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
3917a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
3927a4566b0SAneesh Kumar K.V };
393