xref: /openbmc/linux/fs/9p/acl.c (revision d344b0fb72e00339625464c5a29711906fa70b8b)
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"
2576381a42SAneesh 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;
31009ca389SJoe Perches 	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;
5976381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
6085ff872dSAneesh Kumar K.V 
6176381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
6276381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
6376381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
6476381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
6576381a42SAneesh Kumar K.V 		return 0;
6676381a42SAneesh 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 	} else
7585ff872dSAneesh Kumar K.V 		retval = -EIO;
7685ff872dSAneesh Kumar K.V 
77c61fa0d6SVenkateswararao Jujjuri (JV) 	if (!IS_ERR(dacl))
78c61fa0d6SVenkateswararao Jujjuri (JV) 		posix_acl_release(dacl);
79c61fa0d6SVenkateswararao Jujjuri (JV) 
80c61fa0d6SVenkateswararao Jujjuri (JV) 	if (!IS_ERR(pacl))
81c61fa0d6SVenkateswararao Jujjuri (JV) 		posix_acl_release(pacl);
82c61fa0d6SVenkateswararao Jujjuri (JV) 
8385ff872dSAneesh Kumar K.V 	return retval;
8485ff872dSAneesh Kumar K.V }
8585ff872dSAneesh Kumar K.V 
8685ff872dSAneesh Kumar K.V static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
8785ff872dSAneesh Kumar K.V {
8885ff872dSAneesh Kumar K.V 	struct posix_acl *acl;
8985ff872dSAneesh Kumar K.V 	/*
9085ff872dSAneesh Kumar K.V 	 * 9p Always cache the acl value when
9185ff872dSAneesh Kumar K.V 	 * instantiating the inode (v9fs_inode_from_fid)
9285ff872dSAneesh Kumar K.V 	 */
9385ff872dSAneesh Kumar K.V 	acl = get_cached_acl(inode, type);
9485ff872dSAneesh Kumar K.V 	BUG_ON(acl == ACL_NOT_CACHED);
9585ff872dSAneesh Kumar K.V 	return acl;
9685ff872dSAneesh Kumar K.V }
9785ff872dSAneesh Kumar K.V 
98b74c79e9SNick Piggin int v9fs_check_acl(struct inode *inode, int mask, unsigned int flags)
9985ff872dSAneesh Kumar K.V {
10076381a42SAneesh Kumar K.V 	struct posix_acl *acl;
10176381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
10276381a42SAneesh Kumar K.V 
103b74c79e9SNick Piggin 	if (flags & IPERM_FLAG_RCU)
104b74c79e9SNick Piggin 		return -ECHILD;
105b74c79e9SNick Piggin 
10676381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
10776381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
10876381a42SAneesh Kumar K.V 		/*
10976381a42SAneesh Kumar K.V 		 * On access = client mode get the acl
11076381a42SAneesh Kumar K.V 		 * values from the server
11176381a42SAneesh Kumar K.V 		 */
11276381a42SAneesh Kumar K.V 		return 0;
11376381a42SAneesh Kumar K.V 	}
11476381a42SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
11585ff872dSAneesh Kumar K.V 
11685ff872dSAneesh Kumar K.V 	if (IS_ERR(acl))
11785ff872dSAneesh Kumar K.V 		return PTR_ERR(acl);
11885ff872dSAneesh Kumar K.V 	if (acl) {
11985ff872dSAneesh Kumar K.V 		int error = posix_acl_permission(inode, acl, mask);
12085ff872dSAneesh Kumar K.V 		posix_acl_release(acl);
12185ff872dSAneesh Kumar K.V 		return error;
12285ff872dSAneesh Kumar K.V 	}
12385ff872dSAneesh Kumar K.V 	return -EAGAIN;
12485ff872dSAneesh Kumar K.V }
1257a4566b0SAneesh Kumar K.V 
1266e8dc555SAneesh Kumar K.V static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
1276e8dc555SAneesh Kumar K.V {
1286e8dc555SAneesh Kumar K.V 	int retval;
1296e8dc555SAneesh Kumar K.V 	char *name;
1306e8dc555SAneesh Kumar K.V 	size_t size;
1316e8dc555SAneesh Kumar K.V 	void *buffer;
1326e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
1336e8dc555SAneesh Kumar K.V 
1346e8dc555SAneesh Kumar K.V 	set_cached_acl(inode, type, acl);
135*d344b0fbSVenkateswararao Jujjuri (JV) 
136*d344b0fbSVenkateswararao Jujjuri (JV) 	if (!acl)
137*d344b0fbSVenkateswararao Jujjuri (JV) 		return 0;
138*d344b0fbSVenkateswararao Jujjuri (JV) 
1396e8dc555SAneesh Kumar K.V 	/* Set a setxattr request to server */
1406e8dc555SAneesh Kumar K.V 	size = posix_acl_xattr_size(acl->a_count);
1416e8dc555SAneesh Kumar K.V 	buffer = kmalloc(size, GFP_KERNEL);
1426e8dc555SAneesh Kumar K.V 	if (!buffer)
1436e8dc555SAneesh Kumar K.V 		return -ENOMEM;
1446e8dc555SAneesh Kumar K.V 	retval = posix_acl_to_xattr(acl, buffer, size);
1456e8dc555SAneesh Kumar K.V 	if (retval < 0)
1466e8dc555SAneesh Kumar K.V 		goto err_free_out;
1476e8dc555SAneesh Kumar K.V 	switch (type) {
1486e8dc555SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
1496e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
1506e8dc555SAneesh Kumar K.V 		break;
1516e8dc555SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
1526e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
1536e8dc555SAneesh Kumar K.V 		break;
1546e8dc555SAneesh Kumar K.V 	default:
1556e8dc555SAneesh Kumar K.V 		BUG();
1566e8dc555SAneesh Kumar K.V 	}
1576e8dc555SAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
1586e8dc555SAneesh Kumar K.V err_free_out:
1596e8dc555SAneesh Kumar K.V 	kfree(buffer);
1606e8dc555SAneesh Kumar K.V 	return retval;
1616e8dc555SAneesh Kumar K.V }
1626e8dc555SAneesh Kumar K.V 
1636e8dc555SAneesh Kumar K.V int v9fs_acl_chmod(struct dentry *dentry)
1646e8dc555SAneesh Kumar K.V {
1656e8dc555SAneesh Kumar K.V 	int retval = 0;
1666e8dc555SAneesh Kumar K.V 	struct posix_acl *acl, *clone;
1676e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
1686e8dc555SAneesh Kumar K.V 
1696e8dc555SAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
1706e8dc555SAneesh Kumar K.V 		return -EOPNOTSUPP;
1716e8dc555SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
1726e8dc555SAneesh Kumar K.V 	if (acl) {
1736e8dc555SAneesh Kumar K.V 		clone = posix_acl_clone(acl, GFP_KERNEL);
1746e8dc555SAneesh Kumar K.V 		posix_acl_release(acl);
1756e8dc555SAneesh Kumar K.V 		if (!clone)
1766e8dc555SAneesh Kumar K.V 			return -ENOMEM;
1776e8dc555SAneesh Kumar K.V 		retval = posix_acl_chmod_masq(clone, inode->i_mode);
1786e8dc555SAneesh Kumar K.V 		if (!retval)
1796e8dc555SAneesh Kumar K.V 			retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
1806e8dc555SAneesh Kumar K.V 		posix_acl_release(clone);
1816e8dc555SAneesh Kumar K.V 	}
1826e8dc555SAneesh Kumar K.V 	return retval;
1836e8dc555SAneesh Kumar K.V }
1846e8dc555SAneesh Kumar K.V 
185ad77dbceSAneesh Kumar K.V int v9fs_set_create_acl(struct dentry *dentry,
186ad77dbceSAneesh Kumar K.V 			struct posix_acl *dpacl, struct posix_acl *pacl)
187ad77dbceSAneesh Kumar K.V {
188ad77dbceSAneesh Kumar K.V 	v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
189ad77dbceSAneesh Kumar K.V 	v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
190ad77dbceSAneesh Kumar K.V 	posix_acl_release(dpacl);
191ad77dbceSAneesh Kumar K.V 	posix_acl_release(pacl);
192ad77dbceSAneesh Kumar K.V 	return 0;
193ad77dbceSAneesh Kumar K.V }
194ad77dbceSAneesh Kumar K.V 
195ad77dbceSAneesh Kumar K.V int v9fs_acl_mode(struct inode *dir, mode_t *modep,
196ad77dbceSAneesh Kumar K.V 		  struct posix_acl **dpacl, struct posix_acl **pacl)
197ad77dbceSAneesh Kumar K.V {
198ad77dbceSAneesh Kumar K.V 	int retval = 0;
199ad77dbceSAneesh Kumar K.V 	mode_t mode = *modep;
200ad77dbceSAneesh Kumar K.V 	struct posix_acl *acl = NULL;
201ad77dbceSAneesh Kumar K.V 
202ad77dbceSAneesh Kumar K.V 	if (!S_ISLNK(mode)) {
203ad77dbceSAneesh Kumar K.V 		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
204ad77dbceSAneesh Kumar K.V 		if (IS_ERR(acl))
205ad77dbceSAneesh Kumar K.V 			return PTR_ERR(acl);
206ad77dbceSAneesh Kumar K.V 		if (!acl)
207ad77dbceSAneesh Kumar K.V 			mode &= ~current_umask();
208ad77dbceSAneesh Kumar K.V 	}
209ad77dbceSAneesh Kumar K.V 	if (acl) {
210ad77dbceSAneesh Kumar K.V 		struct posix_acl *clone;
211ad77dbceSAneesh Kumar K.V 
212ad77dbceSAneesh Kumar K.V 		if (S_ISDIR(mode))
213ad77dbceSAneesh Kumar K.V 			*dpacl = acl;
214ad77dbceSAneesh Kumar K.V 		clone = posix_acl_clone(acl, GFP_NOFS);
215ad77dbceSAneesh Kumar K.V 		retval = -ENOMEM;
216ad77dbceSAneesh Kumar K.V 		if (!clone)
217ad77dbceSAneesh Kumar K.V 			goto cleanup;
218ad77dbceSAneesh Kumar K.V 
219ad77dbceSAneesh Kumar K.V 		retval = posix_acl_create_masq(clone, &mode);
220ad77dbceSAneesh Kumar K.V 		if (retval < 0) {
221ad77dbceSAneesh Kumar K.V 			posix_acl_release(clone);
222ad77dbceSAneesh Kumar K.V 			goto cleanup;
223ad77dbceSAneesh Kumar K.V 		}
224ad77dbceSAneesh Kumar K.V 		if (retval > 0)
225ad77dbceSAneesh Kumar K.V 			*pacl = clone;
226ad77dbceSAneesh Kumar K.V 	}
227ad77dbceSAneesh Kumar K.V 	*modep  = mode;
228ad77dbceSAneesh Kumar K.V 	return 0;
229ad77dbceSAneesh Kumar K.V cleanup:
230ad77dbceSAneesh Kumar K.V 	posix_acl_release(acl);
231ad77dbceSAneesh Kumar K.V 	return retval;
232ad77dbceSAneesh Kumar K.V 
233ad77dbceSAneesh Kumar K.V }
234ad77dbceSAneesh Kumar K.V 
23576381a42SAneesh Kumar K.V static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
23676381a42SAneesh Kumar K.V 			       void *buffer, size_t size, int type)
23776381a42SAneesh Kumar K.V {
23876381a42SAneesh Kumar K.V 	char *full_name;
23976381a42SAneesh Kumar K.V 
24076381a42SAneesh Kumar K.V 	switch (type) {
24176381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
24276381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
24376381a42SAneesh Kumar K.V 		break;
24476381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
24576381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
24676381a42SAneesh Kumar K.V 		break;
24776381a42SAneesh Kumar K.V 	default:
24876381a42SAneesh Kumar K.V 		BUG();
24976381a42SAneesh Kumar K.V 	}
25076381a42SAneesh Kumar K.V 	return v9fs_xattr_get(dentry, full_name, buffer, size);
25176381a42SAneesh Kumar K.V }
25276381a42SAneesh Kumar K.V 
2537a4566b0SAneesh Kumar K.V static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
2547a4566b0SAneesh Kumar K.V 			      void *buffer, size_t size, int type)
2557a4566b0SAneesh Kumar K.V {
25676381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
2577a4566b0SAneesh Kumar K.V 	struct posix_acl *acl;
2587a4566b0SAneesh Kumar K.V 	int error;
2597a4566b0SAneesh Kumar K.V 
2607a4566b0SAneesh Kumar K.V 	if (strcmp(name, "") != 0)
2617a4566b0SAneesh Kumar K.V 		return -EINVAL;
2627a4566b0SAneesh Kumar K.V 
26376381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
26476381a42SAneesh Kumar K.V 	/*
26576381a42SAneesh Kumar K.V 	 * We allow set/get/list of acl when access=client is not specified
26676381a42SAneesh Kumar K.V 	 */
26776381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
26876381a42SAneesh Kumar K.V 		return v9fs_remote_get_acl(dentry, name, buffer, size, type);
26976381a42SAneesh Kumar K.V 
2707a4566b0SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(dentry->d_inode, type);
2717a4566b0SAneesh Kumar K.V 	if (IS_ERR(acl))
2727a4566b0SAneesh Kumar K.V 		return PTR_ERR(acl);
2737a4566b0SAneesh Kumar K.V 	if (acl == NULL)
2747a4566b0SAneesh Kumar K.V 		return -ENODATA;
2757a4566b0SAneesh Kumar K.V 	error = posix_acl_to_xattr(acl, buffer, size);
2767a4566b0SAneesh Kumar K.V 	posix_acl_release(acl);
2777a4566b0SAneesh Kumar K.V 
2787a4566b0SAneesh Kumar K.V 	return error;
2797a4566b0SAneesh Kumar K.V }
2807a4566b0SAneesh Kumar K.V 
28176381a42SAneesh Kumar K.V static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
28276381a42SAneesh Kumar K.V 			      const void *value, size_t size,
28376381a42SAneesh Kumar K.V 			      int flags, int type)
28476381a42SAneesh Kumar K.V {
28576381a42SAneesh Kumar K.V 	char *full_name;
28676381a42SAneesh Kumar K.V 
28776381a42SAneesh Kumar K.V 	switch (type) {
28876381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
28976381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
29076381a42SAneesh Kumar K.V 		break;
29176381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
29276381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
29376381a42SAneesh Kumar K.V 		break;
29476381a42SAneesh Kumar K.V 	default:
29576381a42SAneesh Kumar K.V 		BUG();
29676381a42SAneesh Kumar K.V 	}
29776381a42SAneesh Kumar K.V 	return v9fs_xattr_set(dentry, full_name, value, size, flags);
29876381a42SAneesh Kumar K.V }
29976381a42SAneesh Kumar K.V 
30076381a42SAneesh Kumar K.V 
3017a4566b0SAneesh Kumar K.V static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
3027a4566b0SAneesh Kumar K.V 			      const void *value, size_t size,
3037a4566b0SAneesh Kumar K.V 			      int flags, int type)
3047a4566b0SAneesh Kumar K.V {
30522d8dcdfSAneesh Kumar K.V 	int retval;
30622d8dcdfSAneesh Kumar K.V 	struct posix_acl *acl;
30776381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
30822d8dcdfSAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
30922d8dcdfSAneesh Kumar K.V 
31022d8dcdfSAneesh Kumar K.V 	if (strcmp(name, "") != 0)
31122d8dcdfSAneesh Kumar K.V 		return -EINVAL;
31276381a42SAneesh Kumar K.V 
31376381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
31476381a42SAneesh Kumar K.V 	/*
31576381a42SAneesh Kumar K.V 	 * set the attribute on the remote. Without even looking at the
31676381a42SAneesh Kumar K.V 	 * xattr value. We leave it to the server to validate
31776381a42SAneesh Kumar K.V 	 */
31876381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
31976381a42SAneesh Kumar K.V 		return v9fs_remote_set_acl(dentry, name,
32076381a42SAneesh Kumar K.V 					   value, size, flags, type);
32176381a42SAneesh Kumar K.V 
32222d8dcdfSAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
32322d8dcdfSAneesh Kumar K.V 		return -EOPNOTSUPP;
32422d8dcdfSAneesh Kumar K.V 	if (!is_owner_or_cap(inode))
32522d8dcdfSAneesh Kumar K.V 		return -EPERM;
32622d8dcdfSAneesh Kumar K.V 	if (value) {
32722d8dcdfSAneesh Kumar K.V 		/* update the cached acl value */
32822d8dcdfSAneesh Kumar K.V 		acl = posix_acl_from_xattr(value, size);
32922d8dcdfSAneesh Kumar K.V 		if (IS_ERR(acl))
33022d8dcdfSAneesh Kumar K.V 			return PTR_ERR(acl);
33122d8dcdfSAneesh Kumar K.V 		else if (acl) {
33222d8dcdfSAneesh Kumar K.V 			retval = posix_acl_valid(acl);
33322d8dcdfSAneesh Kumar K.V 			if (retval)
33422d8dcdfSAneesh Kumar K.V 				goto err_out;
33522d8dcdfSAneesh Kumar K.V 		}
33622d8dcdfSAneesh Kumar K.V 	} else
33722d8dcdfSAneesh Kumar K.V 		acl = NULL;
33822d8dcdfSAneesh Kumar K.V 
33922d8dcdfSAneesh Kumar K.V 	switch (type) {
34022d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
34122d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
34222d8dcdfSAneesh Kumar K.V 		if (acl) {
34322d8dcdfSAneesh Kumar K.V 			mode_t mode = inode->i_mode;
34422d8dcdfSAneesh Kumar K.V 			retval = posix_acl_equiv_mode(acl, &mode);
34522d8dcdfSAneesh Kumar K.V 			if (retval < 0)
34622d8dcdfSAneesh Kumar K.V 				goto err_out;
34722d8dcdfSAneesh Kumar K.V 			else {
34822d8dcdfSAneesh Kumar K.V 				struct iattr iattr;
34922d8dcdfSAneesh Kumar K.V 				if (retval == 0) {
35022d8dcdfSAneesh Kumar K.V 					/*
35122d8dcdfSAneesh Kumar K.V 					 * ACL can be represented
35222d8dcdfSAneesh Kumar K.V 					 * by the mode bits. So don't
35322d8dcdfSAneesh Kumar K.V 					 * update ACL.
35422d8dcdfSAneesh Kumar K.V 					 */
35522d8dcdfSAneesh Kumar K.V 					acl = NULL;
35622d8dcdfSAneesh Kumar K.V 					value = NULL;
35722d8dcdfSAneesh Kumar K.V 					size = 0;
35822d8dcdfSAneesh Kumar K.V 				}
35922d8dcdfSAneesh Kumar K.V 				/* Updte the mode bits */
36022d8dcdfSAneesh Kumar K.V 				iattr.ia_mode = ((mode & S_IALLUGO) |
36122d8dcdfSAneesh Kumar K.V 						 (inode->i_mode & ~S_IALLUGO));
36222d8dcdfSAneesh Kumar K.V 				iattr.ia_valid = ATTR_MODE;
36322d8dcdfSAneesh Kumar K.V 				/* FIXME should we update ctime ?
36422d8dcdfSAneesh Kumar K.V 				 * What is the following setxattr update the
36522d8dcdfSAneesh Kumar K.V 				 * mode ?
36622d8dcdfSAneesh Kumar K.V 				 */
36722d8dcdfSAneesh Kumar K.V 				v9fs_vfs_setattr_dotl(dentry, &iattr);
36822d8dcdfSAneesh Kumar K.V 			}
36922d8dcdfSAneesh Kumar K.V 		}
37022d8dcdfSAneesh Kumar K.V 		break;
37122d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
37222d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
37322d8dcdfSAneesh Kumar K.V 		if (!S_ISDIR(inode->i_mode)) {
3746f81c115SAneesh Kumar K.V 			retval = acl ? -EINVAL : 0;
37522d8dcdfSAneesh Kumar K.V 			goto err_out;
37622d8dcdfSAneesh Kumar K.V 		}
37722d8dcdfSAneesh Kumar K.V 		break;
37822d8dcdfSAneesh Kumar K.V 	default:
37922d8dcdfSAneesh Kumar K.V 		BUG();
38022d8dcdfSAneesh Kumar K.V 	}
38122d8dcdfSAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, value, size, flags);
38222d8dcdfSAneesh Kumar K.V 	if (!retval)
38322d8dcdfSAneesh Kumar K.V 		set_cached_acl(inode, type, acl);
38422d8dcdfSAneesh Kumar K.V err_out:
38522d8dcdfSAneesh Kumar K.V 	posix_acl_release(acl);
38622d8dcdfSAneesh Kumar K.V 	return retval;
3877a4566b0SAneesh Kumar K.V }
3887a4566b0SAneesh Kumar K.V 
3897a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = {
3907a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_ACCESS,
3917a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_ACCESS,
3927a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
3937a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
3947a4566b0SAneesh Kumar K.V };
3957a4566b0SAneesh Kumar K.V 
3967a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = {
3977a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_DEFAULT,
3987a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_DEFAULT,
3997a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
4007a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
4017a4566b0SAneesh Kumar K.V };
402