xref: /openbmc/linux/fs/9p/acl.c (revision 42869c8adae72366fc6c4f3924ce3d6c3735c4a3)
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"
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);
62e782ef71SVenkateswararao Jujjuri (JV) 	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
63e782ef71SVenkateswararao Jujjuri (JV) 			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
6476381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
6576381a42SAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
6676381a42SAneesh Kumar K.V 		return 0;
6776381a42SAneesh Kumar K.V 	}
6885ff872dSAneesh Kumar K.V 	/* get the default/access acl values and cache them */
6985ff872dSAneesh Kumar K.V 	dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
7085ff872dSAneesh Kumar K.V 	pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
7185ff872dSAneesh Kumar K.V 
7285ff872dSAneesh Kumar K.V 	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
7385ff872dSAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
7485ff872dSAneesh Kumar K.V 		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
7585ff872dSAneesh Kumar K.V 	} else
7685ff872dSAneesh Kumar K.V 		retval = -EIO;
7785ff872dSAneesh Kumar K.V 
78c61fa0d6SVenkateswararao Jujjuri (JV) 	if (!IS_ERR(dacl))
79c61fa0d6SVenkateswararao Jujjuri (JV) 		posix_acl_release(dacl);
80c61fa0d6SVenkateswararao Jujjuri (JV) 
81c61fa0d6SVenkateswararao Jujjuri (JV) 	if (!IS_ERR(pacl))
82c61fa0d6SVenkateswararao Jujjuri (JV) 		posix_acl_release(pacl);
83c61fa0d6SVenkateswararao Jujjuri (JV) 
8485ff872dSAneesh Kumar K.V 	return retval;
8585ff872dSAneesh Kumar K.V }
8685ff872dSAneesh Kumar K.V 
8785ff872dSAneesh Kumar K.V static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
8885ff872dSAneesh Kumar K.V {
8985ff872dSAneesh Kumar K.V 	struct posix_acl *acl;
9085ff872dSAneesh Kumar K.V 	/*
9185ff872dSAneesh Kumar K.V 	 * 9p Always cache the acl value when
9285ff872dSAneesh Kumar K.V 	 * instantiating the inode (v9fs_inode_from_fid)
9385ff872dSAneesh Kumar K.V 	 */
9485ff872dSAneesh Kumar K.V 	acl = get_cached_acl(inode, type);
9585ff872dSAneesh Kumar K.V 	BUG_ON(acl == ACL_NOT_CACHED);
9685ff872dSAneesh Kumar K.V 	return acl;
9785ff872dSAneesh Kumar K.V }
9885ff872dSAneesh Kumar K.V 
99b74c79e9SNick Piggin int v9fs_check_acl(struct inode *inode, int mask, unsigned int flags)
10085ff872dSAneesh Kumar K.V {
10176381a42SAneesh Kumar K.V 	struct posix_acl *acl;
10276381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
10376381a42SAneesh Kumar K.V 
104b74c79e9SNick Piggin 	if (flags & IPERM_FLAG_RCU)
105b74c79e9SNick Piggin 		return -ECHILD;
106b74c79e9SNick Piggin 
10776381a42SAneesh Kumar K.V 	v9ses = v9fs_inode2v9ses(inode);
108e782ef71SVenkateswararao Jujjuri (JV) 	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
109e782ef71SVenkateswararao Jujjuri (JV) 			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
11076381a42SAneesh Kumar K.V 		/*
111e782ef71SVenkateswararao Jujjuri (JV) 		 * On access = client  and acl = on mode get the acl
11276381a42SAneesh Kumar K.V 		 * values from the server
11376381a42SAneesh Kumar K.V 		 */
11476381a42SAneesh Kumar K.V 		return 0;
11576381a42SAneesh Kumar K.V 	}
11676381a42SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
11785ff872dSAneesh Kumar K.V 
11885ff872dSAneesh Kumar K.V 	if (IS_ERR(acl))
11985ff872dSAneesh Kumar K.V 		return PTR_ERR(acl);
12085ff872dSAneesh Kumar K.V 	if (acl) {
12185ff872dSAneesh Kumar K.V 		int error = posix_acl_permission(inode, acl, mask);
12285ff872dSAneesh Kumar K.V 		posix_acl_release(acl);
12385ff872dSAneesh Kumar K.V 		return error;
12485ff872dSAneesh Kumar K.V 	}
12585ff872dSAneesh Kumar K.V 	return -EAGAIN;
12685ff872dSAneesh Kumar K.V }
1277a4566b0SAneesh Kumar K.V 
1286e8dc555SAneesh Kumar K.V static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
1296e8dc555SAneesh Kumar K.V {
1306e8dc555SAneesh Kumar K.V 	int retval;
1316e8dc555SAneesh Kumar K.V 	char *name;
1326e8dc555SAneesh Kumar K.V 	size_t size;
1336e8dc555SAneesh Kumar K.V 	void *buffer;
1346e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
1356e8dc555SAneesh Kumar K.V 
1366e8dc555SAneesh Kumar K.V 	set_cached_acl(inode, type, acl);
137d344b0fbSVenkateswararao Jujjuri (JV) 
138d344b0fbSVenkateswararao Jujjuri (JV) 	if (!acl)
139d344b0fbSVenkateswararao Jujjuri (JV) 		return 0;
140d344b0fbSVenkateswararao Jujjuri (JV) 
1416e8dc555SAneesh Kumar K.V 	/* Set a setxattr request to server */
1426e8dc555SAneesh Kumar K.V 	size = posix_acl_xattr_size(acl->a_count);
1436e8dc555SAneesh Kumar K.V 	buffer = kmalloc(size, GFP_KERNEL);
1446e8dc555SAneesh Kumar K.V 	if (!buffer)
1456e8dc555SAneesh Kumar K.V 		return -ENOMEM;
1466e8dc555SAneesh Kumar K.V 	retval = posix_acl_to_xattr(acl, buffer, size);
1476e8dc555SAneesh Kumar K.V 	if (retval < 0)
1486e8dc555SAneesh Kumar K.V 		goto err_free_out;
1496e8dc555SAneesh Kumar K.V 	switch (type) {
1506e8dc555SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
1516e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
1526e8dc555SAneesh Kumar K.V 		break;
1536e8dc555SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
1546e8dc555SAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
1556e8dc555SAneesh Kumar K.V 		break;
1566e8dc555SAneesh Kumar K.V 	default:
1576e8dc555SAneesh Kumar K.V 		BUG();
1586e8dc555SAneesh Kumar K.V 	}
1596e8dc555SAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
1606e8dc555SAneesh Kumar K.V err_free_out:
1616e8dc555SAneesh Kumar K.V 	kfree(buffer);
1626e8dc555SAneesh Kumar K.V 	return retval;
1636e8dc555SAneesh Kumar K.V }
1646e8dc555SAneesh Kumar K.V 
1656e8dc555SAneesh Kumar K.V int v9fs_acl_chmod(struct dentry *dentry)
1666e8dc555SAneesh Kumar K.V {
1676e8dc555SAneesh Kumar K.V 	int retval = 0;
1686e8dc555SAneesh Kumar K.V 	struct posix_acl *acl, *clone;
1696e8dc555SAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
1706e8dc555SAneesh Kumar K.V 
1716e8dc555SAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
1726e8dc555SAneesh Kumar K.V 		return -EOPNOTSUPP;
1736e8dc555SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
1746e8dc555SAneesh Kumar K.V 	if (acl) {
1756e8dc555SAneesh Kumar K.V 		clone = posix_acl_clone(acl, GFP_KERNEL);
1766e8dc555SAneesh Kumar K.V 		posix_acl_release(acl);
1776e8dc555SAneesh Kumar K.V 		if (!clone)
1786e8dc555SAneesh Kumar K.V 			return -ENOMEM;
1796e8dc555SAneesh Kumar K.V 		retval = posix_acl_chmod_masq(clone, inode->i_mode);
1806e8dc555SAneesh Kumar K.V 		if (!retval)
1816e8dc555SAneesh Kumar K.V 			retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
1826e8dc555SAneesh Kumar K.V 		posix_acl_release(clone);
1836e8dc555SAneesh Kumar K.V 	}
1846e8dc555SAneesh Kumar K.V 	return retval;
1856e8dc555SAneesh Kumar K.V }
1866e8dc555SAneesh Kumar K.V 
187ad77dbceSAneesh Kumar K.V int v9fs_set_create_acl(struct dentry *dentry,
188ad77dbceSAneesh Kumar K.V 			struct posix_acl *dpacl, struct posix_acl *pacl)
189ad77dbceSAneesh Kumar K.V {
190ad77dbceSAneesh Kumar K.V 	v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
191ad77dbceSAneesh Kumar K.V 	v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
192ad77dbceSAneesh Kumar K.V 	posix_acl_release(dpacl);
193ad77dbceSAneesh Kumar K.V 	posix_acl_release(pacl);
194ad77dbceSAneesh Kumar K.V 	return 0;
195ad77dbceSAneesh Kumar K.V }
196ad77dbceSAneesh Kumar K.V 
197ad77dbceSAneesh Kumar K.V int v9fs_acl_mode(struct inode *dir, mode_t *modep,
198ad77dbceSAneesh Kumar K.V 		  struct posix_acl **dpacl, struct posix_acl **pacl)
199ad77dbceSAneesh Kumar K.V {
200ad77dbceSAneesh Kumar K.V 	int retval = 0;
201ad77dbceSAneesh Kumar K.V 	mode_t mode = *modep;
202ad77dbceSAneesh Kumar K.V 	struct posix_acl *acl = NULL;
203ad77dbceSAneesh Kumar K.V 
204ad77dbceSAneesh Kumar K.V 	if (!S_ISLNK(mode)) {
205ad77dbceSAneesh Kumar K.V 		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
206ad77dbceSAneesh Kumar K.V 		if (IS_ERR(acl))
207ad77dbceSAneesh Kumar K.V 			return PTR_ERR(acl);
208ad77dbceSAneesh Kumar K.V 		if (!acl)
209ad77dbceSAneesh Kumar K.V 			mode &= ~current_umask();
210ad77dbceSAneesh Kumar K.V 	}
211ad77dbceSAneesh Kumar K.V 	if (acl) {
212ad77dbceSAneesh Kumar K.V 		struct posix_acl *clone;
213ad77dbceSAneesh Kumar K.V 
214ad77dbceSAneesh Kumar K.V 		if (S_ISDIR(mode))
215ad77dbceSAneesh Kumar K.V 			*dpacl = acl;
216ad77dbceSAneesh Kumar K.V 		clone = posix_acl_clone(acl, GFP_NOFS);
217ad77dbceSAneesh Kumar K.V 		retval = -ENOMEM;
218ad77dbceSAneesh Kumar K.V 		if (!clone)
219ad77dbceSAneesh Kumar K.V 			goto cleanup;
220ad77dbceSAneesh Kumar K.V 
221ad77dbceSAneesh Kumar K.V 		retval = posix_acl_create_masq(clone, &mode);
222ad77dbceSAneesh Kumar K.V 		if (retval < 0) {
223ad77dbceSAneesh Kumar K.V 			posix_acl_release(clone);
224ad77dbceSAneesh Kumar K.V 			goto cleanup;
225ad77dbceSAneesh Kumar K.V 		}
226ad77dbceSAneesh Kumar K.V 		if (retval > 0)
227ad77dbceSAneesh Kumar K.V 			*pacl = clone;
228ad77dbceSAneesh Kumar K.V 	}
229ad77dbceSAneesh Kumar K.V 	*modep  = mode;
230ad77dbceSAneesh Kumar K.V 	return 0;
231ad77dbceSAneesh Kumar K.V cleanup:
232ad77dbceSAneesh Kumar K.V 	posix_acl_release(acl);
233ad77dbceSAneesh Kumar K.V 	return retval;
234ad77dbceSAneesh Kumar K.V 
235ad77dbceSAneesh Kumar K.V }
236ad77dbceSAneesh Kumar K.V 
23776381a42SAneesh Kumar K.V static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
23876381a42SAneesh Kumar K.V 			       void *buffer, size_t size, int type)
23976381a42SAneesh Kumar K.V {
24076381a42SAneesh Kumar K.V 	char *full_name;
24176381a42SAneesh Kumar K.V 
24276381a42SAneesh Kumar K.V 	switch (type) {
24376381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
24476381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
24576381a42SAneesh Kumar K.V 		break;
24676381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
24776381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
24876381a42SAneesh Kumar K.V 		break;
24976381a42SAneesh Kumar K.V 	default:
25076381a42SAneesh Kumar K.V 		BUG();
25176381a42SAneesh Kumar K.V 	}
25276381a42SAneesh Kumar K.V 	return v9fs_xattr_get(dentry, full_name, buffer, size);
25376381a42SAneesh Kumar K.V }
25476381a42SAneesh Kumar K.V 
2557a4566b0SAneesh Kumar K.V static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
2567a4566b0SAneesh Kumar K.V 			      void *buffer, size_t size, int type)
2577a4566b0SAneesh Kumar K.V {
25876381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
2597a4566b0SAneesh Kumar K.V 	struct posix_acl *acl;
2607a4566b0SAneesh Kumar K.V 	int error;
2617a4566b0SAneesh Kumar K.V 
2627a4566b0SAneesh Kumar K.V 	if (strcmp(name, "") != 0)
2637a4566b0SAneesh Kumar K.V 		return -EINVAL;
2647a4566b0SAneesh Kumar K.V 
265*42869c8aSAneesh Kumar K.V 	v9ses = v9fs_dentry2v9ses(dentry);
26676381a42SAneesh Kumar K.V 	/*
26776381a42SAneesh Kumar K.V 	 * We allow set/get/list of acl when access=client is not specified
26876381a42SAneesh Kumar K.V 	 */
26976381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
27076381a42SAneesh Kumar K.V 		return v9fs_remote_get_acl(dentry, name, buffer, size, type);
27176381a42SAneesh Kumar K.V 
2727a4566b0SAneesh Kumar K.V 	acl = v9fs_get_cached_acl(dentry->d_inode, type);
2737a4566b0SAneesh Kumar K.V 	if (IS_ERR(acl))
2747a4566b0SAneesh Kumar K.V 		return PTR_ERR(acl);
2757a4566b0SAneesh Kumar K.V 	if (acl == NULL)
2767a4566b0SAneesh Kumar K.V 		return -ENODATA;
2777a4566b0SAneesh Kumar K.V 	error = posix_acl_to_xattr(acl, buffer, size);
2787a4566b0SAneesh Kumar K.V 	posix_acl_release(acl);
2797a4566b0SAneesh Kumar K.V 
2807a4566b0SAneesh Kumar K.V 	return error;
2817a4566b0SAneesh Kumar K.V }
2827a4566b0SAneesh Kumar K.V 
28376381a42SAneesh Kumar K.V static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
28476381a42SAneesh Kumar K.V 			      const void *value, size_t size,
28576381a42SAneesh Kumar K.V 			      int flags, int type)
28676381a42SAneesh Kumar K.V {
28776381a42SAneesh Kumar K.V 	char *full_name;
28876381a42SAneesh Kumar K.V 
28976381a42SAneesh Kumar K.V 	switch (type) {
29076381a42SAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
29176381a42SAneesh Kumar K.V 		full_name =  POSIX_ACL_XATTR_ACCESS;
29276381a42SAneesh Kumar K.V 		break;
29376381a42SAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
29476381a42SAneesh Kumar K.V 		full_name = POSIX_ACL_XATTR_DEFAULT;
29576381a42SAneesh Kumar K.V 		break;
29676381a42SAneesh Kumar K.V 	default:
29776381a42SAneesh Kumar K.V 		BUG();
29876381a42SAneesh Kumar K.V 	}
29976381a42SAneesh Kumar K.V 	return v9fs_xattr_set(dentry, full_name, value, size, flags);
30076381a42SAneesh Kumar K.V }
30176381a42SAneesh Kumar K.V 
30276381a42SAneesh Kumar K.V 
3037a4566b0SAneesh Kumar K.V static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
3047a4566b0SAneesh Kumar K.V 			      const void *value, size_t size,
3057a4566b0SAneesh Kumar K.V 			      int flags, int type)
3067a4566b0SAneesh Kumar K.V {
30722d8dcdfSAneesh Kumar K.V 	int retval;
30822d8dcdfSAneesh Kumar K.V 	struct posix_acl *acl;
30976381a42SAneesh Kumar K.V 	struct v9fs_session_info *v9ses;
31022d8dcdfSAneesh Kumar K.V 	struct inode *inode = dentry->d_inode;
31122d8dcdfSAneesh Kumar K.V 
31222d8dcdfSAneesh Kumar K.V 	if (strcmp(name, "") != 0)
31322d8dcdfSAneesh Kumar K.V 		return -EINVAL;
31476381a42SAneesh Kumar K.V 
315*42869c8aSAneesh Kumar K.V 	v9ses = v9fs_dentry2v9ses(dentry);
31676381a42SAneesh Kumar K.V 	/*
31776381a42SAneesh Kumar K.V 	 * set the attribute on the remote. Without even looking at the
31876381a42SAneesh Kumar K.V 	 * xattr value. We leave it to the server to validate
31976381a42SAneesh Kumar K.V 	 */
32076381a42SAneesh Kumar K.V 	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
32176381a42SAneesh Kumar K.V 		return v9fs_remote_set_acl(dentry, name,
32276381a42SAneesh Kumar K.V 					   value, size, flags, type);
32376381a42SAneesh Kumar K.V 
32422d8dcdfSAneesh Kumar K.V 	if (S_ISLNK(inode->i_mode))
32522d8dcdfSAneesh Kumar K.V 		return -EOPNOTSUPP;
32622d8dcdfSAneesh Kumar K.V 	if (!is_owner_or_cap(inode))
32722d8dcdfSAneesh Kumar K.V 		return -EPERM;
32822d8dcdfSAneesh Kumar K.V 	if (value) {
32922d8dcdfSAneesh Kumar K.V 		/* update the cached acl value */
33022d8dcdfSAneesh Kumar K.V 		acl = posix_acl_from_xattr(value, size);
33122d8dcdfSAneesh Kumar K.V 		if (IS_ERR(acl))
33222d8dcdfSAneesh Kumar K.V 			return PTR_ERR(acl);
33322d8dcdfSAneesh Kumar K.V 		else if (acl) {
33422d8dcdfSAneesh Kumar K.V 			retval = posix_acl_valid(acl);
33522d8dcdfSAneesh Kumar K.V 			if (retval)
33622d8dcdfSAneesh Kumar K.V 				goto err_out;
33722d8dcdfSAneesh Kumar K.V 		}
33822d8dcdfSAneesh Kumar K.V 	} else
33922d8dcdfSAneesh Kumar K.V 		acl = NULL;
34022d8dcdfSAneesh Kumar K.V 
34122d8dcdfSAneesh Kumar K.V 	switch (type) {
34222d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_ACCESS:
34322d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_ACCESS;
34422d8dcdfSAneesh Kumar K.V 		if (acl) {
34522d8dcdfSAneesh Kumar K.V 			mode_t mode = inode->i_mode;
34622d8dcdfSAneesh Kumar K.V 			retval = posix_acl_equiv_mode(acl, &mode);
34722d8dcdfSAneesh Kumar K.V 			if (retval < 0)
34822d8dcdfSAneesh Kumar K.V 				goto err_out;
34922d8dcdfSAneesh Kumar K.V 			else {
35022d8dcdfSAneesh Kumar K.V 				struct iattr iattr;
35122d8dcdfSAneesh Kumar K.V 				if (retval == 0) {
35222d8dcdfSAneesh Kumar K.V 					/*
35322d8dcdfSAneesh Kumar K.V 					 * ACL can be represented
35422d8dcdfSAneesh Kumar K.V 					 * by the mode bits. So don't
35522d8dcdfSAneesh Kumar K.V 					 * update ACL.
35622d8dcdfSAneesh Kumar K.V 					 */
35722d8dcdfSAneesh Kumar K.V 					acl = NULL;
35822d8dcdfSAneesh Kumar K.V 					value = NULL;
35922d8dcdfSAneesh Kumar K.V 					size = 0;
36022d8dcdfSAneesh Kumar K.V 				}
36122d8dcdfSAneesh Kumar K.V 				/* Updte the mode bits */
36222d8dcdfSAneesh Kumar K.V 				iattr.ia_mode = ((mode & S_IALLUGO) |
36322d8dcdfSAneesh Kumar K.V 						 (inode->i_mode & ~S_IALLUGO));
36422d8dcdfSAneesh Kumar K.V 				iattr.ia_valid = ATTR_MODE;
36522d8dcdfSAneesh Kumar K.V 				/* FIXME should we update ctime ?
36622d8dcdfSAneesh Kumar K.V 				 * What is the following setxattr update the
36722d8dcdfSAneesh Kumar K.V 				 * mode ?
36822d8dcdfSAneesh Kumar K.V 				 */
36922d8dcdfSAneesh Kumar K.V 				v9fs_vfs_setattr_dotl(dentry, &iattr);
37022d8dcdfSAneesh Kumar K.V 			}
37122d8dcdfSAneesh Kumar K.V 		}
37222d8dcdfSAneesh Kumar K.V 		break;
37322d8dcdfSAneesh Kumar K.V 	case ACL_TYPE_DEFAULT:
37422d8dcdfSAneesh Kumar K.V 		name = POSIX_ACL_XATTR_DEFAULT;
37522d8dcdfSAneesh Kumar K.V 		if (!S_ISDIR(inode->i_mode)) {
3766f81c115SAneesh Kumar K.V 			retval = acl ? -EINVAL : 0;
37722d8dcdfSAneesh Kumar K.V 			goto err_out;
37822d8dcdfSAneesh Kumar K.V 		}
37922d8dcdfSAneesh Kumar K.V 		break;
38022d8dcdfSAneesh Kumar K.V 	default:
38122d8dcdfSAneesh Kumar K.V 		BUG();
38222d8dcdfSAneesh Kumar K.V 	}
38322d8dcdfSAneesh Kumar K.V 	retval = v9fs_xattr_set(dentry, name, value, size, flags);
38422d8dcdfSAneesh Kumar K.V 	if (!retval)
38522d8dcdfSAneesh Kumar K.V 		set_cached_acl(inode, type, acl);
38622d8dcdfSAneesh Kumar K.V err_out:
38722d8dcdfSAneesh Kumar K.V 	posix_acl_release(acl);
38822d8dcdfSAneesh Kumar K.V 	return retval;
3897a4566b0SAneesh Kumar K.V }
3907a4566b0SAneesh Kumar K.V 
3917a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_access_handler = {
3927a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_ACCESS,
3937a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_ACCESS,
3947a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
3957a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
3967a4566b0SAneesh Kumar K.V };
3977a4566b0SAneesh Kumar K.V 
3987a4566b0SAneesh Kumar K.V const struct xattr_handler v9fs_xattr_acl_default_handler = {
3997a4566b0SAneesh Kumar K.V 	.prefix	= POSIX_ACL_XATTR_DEFAULT,
4007a4566b0SAneesh Kumar K.V 	.flags	= ACL_TYPE_DEFAULT,
4017a4566b0SAneesh Kumar K.V 	.get	= v9fs_xattr_get_acl,
4027a4566b0SAneesh Kumar K.V 	.set	= v9fs_xattr_set_acl,
4037a4566b0SAneesh Kumar K.V };
404