1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/fs.h>
17#include <net/9p/9p.h>
18#include <net/9p/client.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21#include <linux/posix_acl_xattr.h>
22#include "xattr.h"
23#include "acl.h"
24#include "v9fs.h"
25#include "v9fs_vfs.h"
26#include "fid.h"
27
28static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
29{
30	ssize_t size;
31	void *value = NULL;
32	struct posix_acl *acl = NULL;
33
34	size = v9fs_fid_xattr_get(fid, name, NULL, 0);
35	if (size > 0) {
36		value = kzalloc(size, GFP_NOFS);
37		if (!value)
38			return ERR_PTR(-ENOMEM);
39		size = v9fs_fid_xattr_get(fid, name, value, size);
40		if (size > 0) {
41			acl = posix_acl_from_xattr(&init_user_ns, value, size);
42			if (IS_ERR(acl))
43				goto err_out;
44		}
45	} else if (size == -ENODATA || size == 0 ||
46		   size == -ENOSYS || size == -EOPNOTSUPP) {
47		acl = NULL;
48	} else
49		acl = ERR_PTR(-EIO);
50
51err_out:
52	kfree(value);
53	return acl;
54}
55
56int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
57{
58	int retval = 0;
59	struct posix_acl *pacl, *dacl;
60	struct v9fs_session_info *v9ses;
61
62	v9ses = v9fs_inode2v9ses(inode);
63	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
64			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
65		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
66		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
67		return 0;
68	}
69	/* get the default/access acl values and cache them */
70	dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
71	pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
72
73	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
74		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
75		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
76	} else
77		retval = -EIO;
78
79	if (!IS_ERR(dacl))
80		posix_acl_release(dacl);
81
82	if (!IS_ERR(pacl))
83		posix_acl_release(pacl);
84
85	return retval;
86}
87
88static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
89{
90	struct posix_acl *acl;
91	/*
92	 * 9p Always cache the acl value when
93	 * instantiating the inode (v9fs_inode_from_fid)
94	 */
95	acl = get_cached_acl(inode, type);
96	BUG_ON(acl == ACL_NOT_CACHED);
97	return acl;
98}
99
100struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type)
101{
102	struct v9fs_session_info *v9ses;
103
104	v9ses = v9fs_inode2v9ses(inode);
105	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
106			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
107		/*
108		 * On access = client  and acl = on mode get the acl
109		 * values from the server
110		 */
111		return NULL;
112	}
113	return v9fs_get_cached_acl(inode, type);
114
115}
116
117static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
118{
119	int retval;
120	char *name;
121	size_t size;
122	void *buffer;
123	if (!acl)
124		return 0;
125
126	/* Set a setxattr request to server */
127	size = posix_acl_xattr_size(acl->a_count);
128	buffer = kmalloc(size, GFP_KERNEL);
129	if (!buffer)
130		return -ENOMEM;
131	retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
132	if (retval < 0)
133		goto err_free_out;
134	switch (type) {
135	case ACL_TYPE_ACCESS:
136		name = POSIX_ACL_XATTR_ACCESS;
137		break;
138	case ACL_TYPE_DEFAULT:
139		name = POSIX_ACL_XATTR_DEFAULT;
140		break;
141	default:
142		BUG();
143	}
144	retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
145err_free_out:
146	kfree(buffer);
147	return retval;
148}
149
150int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
151{
152	int retval = 0;
153	struct posix_acl *acl;
154
155	if (S_ISLNK(inode->i_mode))
156		return -EOPNOTSUPP;
157	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
158	if (acl) {
159		retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
160		if (retval)
161			return retval;
162		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
163		retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
164		posix_acl_release(acl);
165	}
166	return retval;
167}
168
169int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
170			struct posix_acl *dacl, struct posix_acl *acl)
171{
172	set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
173	set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
174	v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
175	v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
176	return 0;
177}
178
179void v9fs_put_acl(struct posix_acl *dacl,
180		  struct posix_acl *acl)
181{
182	posix_acl_release(dacl);
183	posix_acl_release(acl);
184}
185
186int v9fs_acl_mode(struct inode *dir, umode_t *modep,
187		  struct posix_acl **dpacl, struct posix_acl **pacl)
188{
189	int retval = 0;
190	umode_t mode = *modep;
191	struct posix_acl *acl = NULL;
192
193	if (!S_ISLNK(mode)) {
194		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
195		if (IS_ERR(acl))
196			return PTR_ERR(acl);
197		if (!acl)
198			mode &= ~current_umask();
199	}
200	if (acl) {
201		if (S_ISDIR(mode))
202			*dpacl = posix_acl_dup(acl);
203		retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
204		if (retval < 0)
205			return retval;
206		if (retval > 0)
207			*pacl = acl;
208		else
209			posix_acl_release(acl);
210	}
211	*modep  = mode;
212	return 0;
213}
214
215static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
216			       void *buffer, size_t size, int type)
217{
218	char *full_name;
219
220	switch (type) {
221	case ACL_TYPE_ACCESS:
222		full_name =  POSIX_ACL_XATTR_ACCESS;
223		break;
224	case ACL_TYPE_DEFAULT:
225		full_name = POSIX_ACL_XATTR_DEFAULT;
226		break;
227	default:
228		BUG();
229	}
230	return v9fs_xattr_get(dentry, full_name, buffer, size);
231}
232
233static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
234			      void *buffer, size_t size, int type)
235{
236	struct v9fs_session_info *v9ses;
237	struct posix_acl *acl;
238	int error;
239
240	if (strcmp(name, "") != 0)
241		return -EINVAL;
242
243	v9ses = v9fs_dentry2v9ses(dentry);
244	/*
245	 * We allow set/get/list of acl when access=client is not specified
246	 */
247	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
248		return v9fs_remote_get_acl(dentry, name, buffer, size, type);
249
250	acl = v9fs_get_cached_acl(d_inode(dentry), type);
251	if (IS_ERR(acl))
252		return PTR_ERR(acl);
253	if (acl == NULL)
254		return -ENODATA;
255	error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
256	posix_acl_release(acl);
257
258	return error;
259}
260
261static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
262			      const void *value, size_t size,
263			      int flags, int type)
264{
265	char *full_name;
266
267	switch (type) {
268	case ACL_TYPE_ACCESS:
269		full_name =  POSIX_ACL_XATTR_ACCESS;
270		break;
271	case ACL_TYPE_DEFAULT:
272		full_name = POSIX_ACL_XATTR_DEFAULT;
273		break;
274	default:
275		BUG();
276	}
277	return v9fs_xattr_set(dentry, full_name, value, size, flags);
278}
279
280
281static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
282			      const void *value, size_t size,
283			      int flags, int type)
284{
285	int retval;
286	struct posix_acl *acl;
287	struct v9fs_session_info *v9ses;
288	struct inode *inode = d_inode(dentry);
289
290	if (strcmp(name, "") != 0)
291		return -EINVAL;
292
293	v9ses = v9fs_dentry2v9ses(dentry);
294	/*
295	 * set the attribute on the remote. Without even looking at the
296	 * xattr value. We leave it to the server to validate
297	 */
298	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
299		return v9fs_remote_set_acl(dentry, name,
300					   value, size, flags, type);
301
302	if (S_ISLNK(inode->i_mode))
303		return -EOPNOTSUPP;
304	if (!inode_owner_or_capable(inode))
305		return -EPERM;
306	if (value) {
307		/* update the cached acl value */
308		acl = posix_acl_from_xattr(&init_user_ns, value, size);
309		if (IS_ERR(acl))
310			return PTR_ERR(acl);
311		else if (acl) {
312			retval = posix_acl_valid(acl);
313			if (retval)
314				goto err_out;
315		}
316	} else
317		acl = NULL;
318
319	switch (type) {
320	case ACL_TYPE_ACCESS:
321		name = POSIX_ACL_XATTR_ACCESS;
322		if (acl) {
323			umode_t mode = inode->i_mode;
324			retval = posix_acl_equiv_mode(acl, &mode);
325			if (retval < 0)
326				goto err_out;
327			else {
328				struct iattr iattr;
329				if (retval == 0) {
330					/*
331					 * ACL can be represented
332					 * by the mode bits. So don't
333					 * update ACL.
334					 */
335					acl = NULL;
336					value = NULL;
337					size = 0;
338				}
339				/* Updte the mode bits */
340				iattr.ia_mode = ((mode & S_IALLUGO) |
341						 (inode->i_mode & ~S_IALLUGO));
342				iattr.ia_valid = ATTR_MODE;
343				/* FIXME should we update ctime ?
344				 * What is the following setxattr update the
345				 * mode ?
346				 */
347				v9fs_vfs_setattr_dotl(dentry, &iattr);
348			}
349		}
350		break;
351	case ACL_TYPE_DEFAULT:
352		name = POSIX_ACL_XATTR_DEFAULT;
353		if (!S_ISDIR(inode->i_mode)) {
354			retval = acl ? -EINVAL : 0;
355			goto err_out;
356		}
357		break;
358	default:
359		BUG();
360	}
361	retval = v9fs_xattr_set(dentry, name, value, size, flags);
362	if (!retval)
363		set_cached_acl(inode, type, acl);
364err_out:
365	posix_acl_release(acl);
366	return retval;
367}
368
369const struct xattr_handler v9fs_xattr_acl_access_handler = {
370	.prefix	= POSIX_ACL_XATTR_ACCESS,
371	.flags	= ACL_TYPE_ACCESS,
372	.get	= v9fs_xattr_get_acl,
373	.set	= v9fs_xattr_set_acl,
374};
375
376const struct xattr_handler v9fs_xattr_acl_default_handler = {
377	.prefix	= POSIX_ACL_XATTR_DEFAULT,
378	.flags	= ACL_TYPE_DEFAULT,
379	.get	= v9fs_xattr_get_acl,
380	.set	= v9fs_xattr_set_acl,
381};
382