acl.c 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright IBM Corporation, 2010
 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <linux/module.h>
#include <linux/fs.h>
#include <net/9p/9p.h>
#include <net/9p/client.h>
#include <linux/slab.h>
20
#include <linux/sched.h>
21 22 23
#include <linux/posix_acl_xattr.h>
#include "xattr.h"
#include "acl.h"
24
#include "v9fs.h"
A
Aneesh Kumar K.V 已提交
25
#include "v9fs_vfs.h"
26
#include "fid.h"
27 28 29 30 31

static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
{
	ssize_t size;
	void *value = NULL;
32
	struct posix_acl *acl = NULL;
33 34 35 36 37 38 39 40

	size = v9fs_fid_xattr_get(fid, name, NULL, 0);
	if (size > 0) {
		value = kzalloc(size, GFP_NOFS);
		if (!value)
			return ERR_PTR(-ENOMEM);
		size = v9fs_fid_xattr_get(fid, name, value, size);
		if (size > 0) {
41
			acl = posix_acl_from_xattr(&init_user_ns, value, size);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
			if (IS_ERR(acl))
				goto err_out;
		}
	} else if (size == -ENODATA || size == 0 ||
		   size == -ENOSYS || size == -EOPNOTSUPP) {
		acl = NULL;
	} else
		acl = ERR_PTR(-EIO);

err_out:
	kfree(value);
	return acl;
}

int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
{
	int retval = 0;
	struct posix_acl *pacl, *dacl;
60
	struct v9fs_session_info *v9ses;
61

62
	v9ses = v9fs_inode2v9ses(inode);
63 64
	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
65 66 67 68
		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
		return 0;
	}
69
	/* get the default/access acl values and cache them */
70 71
	dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
	pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
72 73 74 75 76 77 78

	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
	} else
		retval = -EIO;

79 80 81 82 83 84
	if (!IS_ERR(dacl))
		posix_acl_release(dacl);

	if (!IS_ERR(pacl))
		posix_acl_release(pacl);

85 86 87 88 89 90 91 92 93 94 95
	return retval;
}

static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
{
	struct posix_acl *acl;
	/*
	 * 9p Always cache the acl value when
	 * instantiating the inode (v9fs_inode_from_fid)
	 */
	acl = get_cached_acl(inode, type);
96
	BUG_ON(is_uncached_acl(acl));
97 98 99
	return acl;
}

100
struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type)
101
{
102 103 104
	struct v9fs_session_info *v9ses;

	v9ses = v9fs_inode2v9ses(inode);
105 106
	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
107
		/*
108
		 * On access = client  and acl = on mode get the acl
109 110
		 * values from the server
		 */
111
		return NULL;
112
	}
113 114
	return v9fs_get_cached_acl(inode, type);

115
}
116

117
static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
A
Aneesh Kumar K.V 已提交
118 119 120 121 122
{
	int retval;
	char *name;
	size_t size;
	void *buffer;
123 124 125
	if (!acl)
		return 0;

A
Aneesh Kumar K.V 已提交
126 127 128 129 130
	/* Set a setxattr request to server */
	size = posix_acl_xattr_size(acl->a_count);
	buffer = kmalloc(size, GFP_KERNEL);
	if (!buffer)
		return -ENOMEM;
131
	retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
A
Aneesh Kumar K.V 已提交
132 133 134 135
	if (retval < 0)
		goto err_free_out;
	switch (type) {
	case ACL_TYPE_ACCESS:
136
		name = XATTR_NAME_POSIX_ACL_ACCESS;
A
Aneesh Kumar K.V 已提交
137 138
		break;
	case ACL_TYPE_DEFAULT:
139
		name = XATTR_NAME_POSIX_ACL_DEFAULT;
A
Aneesh Kumar K.V 已提交
140 141 142 143
		break;
	default:
		BUG();
	}
144
	retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
A
Aneesh Kumar K.V 已提交
145 146 147 148 149
err_free_out:
	kfree(buffer);
	return retval;
}

150
int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
A
Aneesh Kumar K.V 已提交
151 152
{
	int retval = 0;
153
	struct posix_acl *acl;
A
Aneesh Kumar K.V 已提交
154 155 156 157 158

	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;
	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
	if (acl) {
159
		retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
160 161
		if (retval)
			return retval;
162
		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
163
		retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
A
Aneesh Kumar K.V 已提交
164 165 166 167 168
		posix_acl_release(acl);
	}
	return retval;
}

169
int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
170
			struct posix_acl *dacl, struct posix_acl *acl)
171
{
172 173 174 175
	set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
	set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
	v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
	v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
176 177 178
	return 0;
}

179 180 181 182 183 184 185
void v9fs_put_acl(struct posix_acl *dacl,
		  struct posix_acl *acl)
{
	posix_acl_release(dacl);
	posix_acl_release(acl);
}

A
Al Viro 已提交
186
int v9fs_acl_mode(struct inode *dir, umode_t *modep,
187 188 189
		  struct posix_acl **dpacl, struct posix_acl **pacl)
{
	int retval = 0;
A
Al Viro 已提交
190
	umode_t mode = *modep;
191 192 193 194 195 196 197 198 199 200 201
	struct posix_acl *acl = NULL;

	if (!S_ISLNK(mode)) {
		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
		if (IS_ERR(acl))
			return PTR_ERR(acl);
		if (!acl)
			mode &= ~current_umask();
	}
	if (acl) {
		if (S_ISDIR(mode))
A
Al Viro 已提交
202
			*dpacl = posix_acl_dup(acl);
203
		retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
204 205
		if (retval < 0)
			return retval;
206
		if (retval > 0)
207
			*pacl = acl;
A
Al Viro 已提交
208
		else
209
			posix_acl_release(acl);
210 211 212 213 214
	}
	*modep  = mode;
	return 0;
}

215
static int v9fs_xattr_get_acl(const struct xattr_handler *handler,
216 217
			      struct dentry *dentry, struct inode *inode,
			      const char *name, void *buffer, size_t size)
218
{
219
	struct v9fs_session_info *v9ses;
220 221 222
	struct posix_acl *acl;
	int error;

A
Aneesh Kumar K.V 已提交
223
	v9ses = v9fs_dentry2v9ses(dentry);
224 225 226 227
	/*
	 * We allow set/get/list of acl when access=client is not specified
	 */
	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
228
		return v9fs_xattr_get(dentry, handler->name, buffer, size);
229

230
	acl = v9fs_get_cached_acl(inode, handler->flags);
231 232 233 234
	if (IS_ERR(acl))
		return PTR_ERR(acl);
	if (acl == NULL)
		return -ENODATA;
235
	error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
236 237 238 239 240
	posix_acl_release(acl);

	return error;
}

241 242 243
static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
			      struct dentry *dentry, const char *name,
			      const void *value, size_t size, int flags)
244
{
245 246
	int retval;
	struct posix_acl *acl;
247
	struct v9fs_session_info *v9ses;
248
	struct inode *inode = d_inode(dentry);
249

A
Aneesh Kumar K.V 已提交
250
	v9ses = v9fs_dentry2v9ses(dentry);
251 252 253 254 255
	/*
	 * set the attribute on the remote. Without even looking at the
	 * xattr value. We leave it to the server to validate
	 */
	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
256
		return v9fs_xattr_set(dentry, handler->name, value, size,
A
Andreas Gruenbacher 已提交
257
				      flags);
258

259 260
	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;
261
	if (!inode_owner_or_capable(inode))
262 263 264
		return -EPERM;
	if (value) {
		/* update the cached acl value */
265
		acl = posix_acl_from_xattr(&init_user_ns, value, size);
266 267 268 269 270 271 272 273 274 275
		if (IS_ERR(acl))
			return PTR_ERR(acl);
		else if (acl) {
			retval = posix_acl_valid(acl);
			if (retval)
				goto err_out;
		}
	} else
		acl = NULL;

276
	switch (handler->flags) {
277 278
	case ACL_TYPE_ACCESS:
		if (acl) {
279
			umode_t mode = inode->i_mode;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
			retval = posix_acl_equiv_mode(acl, &mode);
			if (retval < 0)
				goto err_out;
			else {
				struct iattr iattr;
				if (retval == 0) {
					/*
					 * ACL can be represented
					 * by the mode bits. So don't
					 * update ACL.
					 */
					acl = NULL;
					value = NULL;
					size = 0;
				}
				/* Updte the mode bits */
				iattr.ia_mode = ((mode & S_IALLUGO) |
						 (inode->i_mode & ~S_IALLUGO));
				iattr.ia_valid = ATTR_MODE;
				/* FIXME should we update ctime ?
				 * What is the following setxattr update the
				 * mode ?
				 */
				v9fs_vfs_setattr_dotl(dentry, &iattr);
			}
		}
		break;
	case ACL_TYPE_DEFAULT:
		if (!S_ISDIR(inode->i_mode)) {
309
			retval = acl ? -EINVAL : 0;
310 311 312 313 314 315
			goto err_out;
		}
		break;
	default:
		BUG();
	}
316
	retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
317
	if (!retval)
318
		set_cached_acl(inode, handler->flags, acl);
319 320 321
err_out:
	posix_acl_release(acl);
	return retval;
322 323 324
}

const struct xattr_handler v9fs_xattr_acl_access_handler = {
325
	.name	= XATTR_NAME_POSIX_ACL_ACCESS,
326 327 328 329 330 331
	.flags	= ACL_TYPE_ACCESS,
	.get	= v9fs_xattr_get_acl,
	.set	= v9fs_xattr_set_acl,
};

const struct xattr_handler v9fs_xattr_acl_default_handler = {
332
	.name	= XATTR_NAME_POSIX_ACL_DEFAULT,
333 334 335 336
	.flags	= ACL_TYPE_DEFAULT,
	.get	= v9fs_xattr_get_acl,
	.set	= v9fs_xattr_set_acl,
};