blob: df4d7a171d7f2ae3f733af24c8a4981b5f023681 [file] [log] [blame]
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +05301/*
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>
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +053020#include <linux/sched.h>
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053021#include <linux/posix_acl_xattr.h>
22#include "xattr.h"
23#include "acl.h"
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +053024#include "v9fs.h"
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +053025#include "v9fs_vfs.h"
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053026
27static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
28{
29 ssize_t size;
30 void *value = NULL;
Joe Perches009ca382010-11-15 03:04:51 +000031 struct posix_acl *acl = NULL;
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053032
33 size = v9fs_fid_xattr_get(fid, name, NULL, 0);
34 if (size > 0) {
35 value = kzalloc(size, GFP_NOFS);
36 if (!value)
37 return ERR_PTR(-ENOMEM);
38 size = v9fs_fid_xattr_get(fid, name, value, size);
39 if (size > 0) {
40 acl = posix_acl_from_xattr(value, size);
41 if (IS_ERR(acl))
42 goto err_out;
43 }
44 } else if (size == -ENODATA || size == 0 ||
45 size == -ENOSYS || size == -EOPNOTSUPP) {
46 acl = NULL;
47 } else
48 acl = ERR_PTR(-EIO);
49
50err_out:
51 kfree(value);
52 return acl;
53}
54
55int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
56{
57 int retval = 0;
58 struct posix_acl *pacl, *dacl;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +053059 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053060
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +053061 v9ses = v9fs_inode2v9ses(inode);
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080062 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
63 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +053064 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
65 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
66 return 0;
67 }
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053068 /* get the default/access acl values and cache them */
69 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
70 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
71
72 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
73 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
74 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053075 } else
76 retval = -EIO;
77
Venkateswararao Jujjuri (JV)c61fa0d2011-01-13 15:28:39 -080078 if (!IS_ERR(dacl))
79 posix_acl_release(dacl);
80
81 if (!IS_ERR(pacl))
82 posix_acl_release(pacl);
83
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053084 return retval;
85}
86
87static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
88{
89 struct posix_acl *acl;
90 /*
91 * 9p Always cache the acl value when
92 * instantiating the inode (v9fs_inode_from_fid)
93 */
94 acl = get_cached_acl(inode, type);
95 BUG_ON(acl == ACL_NOT_CACHED);
96 return acl;
97}
98
Al Viro7e401452011-06-20 19:12:17 -040099int v9fs_check_acl(struct inode *inode, int mask)
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +0530100{
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530101 struct posix_acl *acl;
102 struct v9fs_session_info *v9ses;
103
104 v9ses = v9fs_inode2v9ses(inode);
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800105 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
106 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530107 /*
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800108 * On access = client and acl = on mode get the acl
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530109 * values from the server
110 */
Christoph Hellwigebbb0ef2011-07-23 17:36:38 +0200111 return -EAGAIN;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530112 }
113 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +0530114
115 if (IS_ERR(acl))
116 return PTR_ERR(acl);
117 if (acl) {
118 int error = posix_acl_permission(inode, acl, mask);
119 posix_acl_release(acl);
120 return error;
121 }
122 return -EAGAIN;
123}
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530124
Aneesh Kumar K.V6e8dc552010-09-28 00:27:40 +0530125static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
126{
127 int retval;
128 char *name;
129 size_t size;
130 void *buffer;
131 struct inode *inode = dentry->d_inode;
132
133 set_cached_acl(inode, type, acl);
Venkateswararao Jujjuri (JV)d344b0f2011-01-13 16:33:00 -0800134
135 if (!acl)
136 return 0;
137
Aneesh Kumar K.V6e8dc552010-09-28 00:27:40 +0530138 /* Set a setxattr request to server */
139 size = posix_acl_xattr_size(acl->a_count);
140 buffer = kmalloc(size, GFP_KERNEL);
141 if (!buffer)
142 return -ENOMEM;
143 retval = posix_acl_to_xattr(acl, buffer, size);
144 if (retval < 0)
145 goto err_free_out;
146 switch (type) {
147 case ACL_TYPE_ACCESS:
148 name = POSIX_ACL_XATTR_ACCESS;
149 break;
150 case ACL_TYPE_DEFAULT:
151 name = POSIX_ACL_XATTR_DEFAULT;
152 break;
153 default:
154 BUG();
155 }
156 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
157err_free_out:
158 kfree(buffer);
159 return retval;
160}
161
162int v9fs_acl_chmod(struct dentry *dentry)
163{
164 int retval = 0;
165 struct posix_acl *acl, *clone;
166 struct inode *inode = dentry->d_inode;
167
168 if (S_ISLNK(inode->i_mode))
169 return -EOPNOTSUPP;
170 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
171 if (acl) {
172 clone = posix_acl_clone(acl, GFP_KERNEL);
173 posix_acl_release(acl);
174 if (!clone)
175 return -ENOMEM;
176 retval = posix_acl_chmod_masq(clone, inode->i_mode);
177 if (!retval)
178 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
179 posix_acl_release(clone);
180 }
181 return retval;
182}
183
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530184int v9fs_set_create_acl(struct dentry *dentry,
Al Viro1ec95bf2011-07-23 02:28:13 -0400185 struct posix_acl **dpacl, struct posix_acl **pacl)
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530186{
Al Viro1ec95bf2011-07-23 02:28:13 -0400187 if (dentry) {
188 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, *dpacl);
189 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, *pacl);
190 }
191 posix_acl_release(*dpacl);
192 posix_acl_release(*pacl);
193 *dpacl = *pacl = NULL;
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530194 return 0;
195}
196
197int v9fs_acl_mode(struct inode *dir, mode_t *modep,
198 struct posix_acl **dpacl, struct posix_acl **pacl)
199{
200 int retval = 0;
201 mode_t mode = *modep;
202 struct posix_acl *acl = NULL;
203
204 if (!S_ISLNK(mode)) {
205 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
206 if (IS_ERR(acl))
207 return PTR_ERR(acl);
208 if (!acl)
209 mode &= ~current_umask();
210 }
211 if (acl) {
212 struct posix_acl *clone;
213
214 if (S_ISDIR(mode))
Al Viro1ec95bf2011-07-23 02:28:13 -0400215 *dpacl = posix_acl_dup(acl);
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530216 clone = posix_acl_clone(acl, GFP_NOFS);
Al Viro1ec95bf2011-07-23 02:28:13 -0400217 posix_acl_release(acl);
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530218 if (!clone)
Al Viro1ec95bf2011-07-23 02:28:13 -0400219 return -ENOMEM;
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530220
221 retval = posix_acl_create_masq(clone, &mode);
222 if (retval < 0) {
223 posix_acl_release(clone);
224 goto cleanup;
225 }
226 if (retval > 0)
227 *pacl = clone;
Al Viro1ec95bf2011-07-23 02:28:13 -0400228 else
229 posix_acl_release(clone);
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530230 }
231 *modep = mode;
232 return 0;
233cleanup:
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530234 return retval;
235
236}
237
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530238static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
239 void *buffer, size_t size, int type)
240{
241 char *full_name;
242
243 switch (type) {
244 case ACL_TYPE_ACCESS:
245 full_name = POSIX_ACL_XATTR_ACCESS;
246 break;
247 case ACL_TYPE_DEFAULT:
248 full_name = POSIX_ACL_XATTR_DEFAULT;
249 break;
250 default:
251 BUG();
252 }
253 return v9fs_xattr_get(dentry, full_name, buffer, size);
254}
255
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530256static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
257 void *buffer, size_t size, int type)
258{
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530259 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530260 struct posix_acl *acl;
261 int error;
262
263 if (strcmp(name, "") != 0)
264 return -EINVAL;
265
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530266 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530267 /*
268 * We allow set/get/list of acl when access=client is not specified
269 */
270 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
271 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
272
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530273 acl = v9fs_get_cached_acl(dentry->d_inode, type);
274 if (IS_ERR(acl))
275 return PTR_ERR(acl);
276 if (acl == NULL)
277 return -ENODATA;
278 error = posix_acl_to_xattr(acl, buffer, size);
279 posix_acl_release(acl);
280
281 return error;
282}
283
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530284static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
285 const void *value, size_t size,
286 int flags, int type)
287{
288 char *full_name;
289
290 switch (type) {
291 case ACL_TYPE_ACCESS:
292 full_name = POSIX_ACL_XATTR_ACCESS;
293 break;
294 case ACL_TYPE_DEFAULT:
295 full_name = POSIX_ACL_XATTR_DEFAULT;
296 break;
297 default:
298 BUG();
299 }
300 return v9fs_xattr_set(dentry, full_name, value, size, flags);
301}
302
303
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530304static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
305 const void *value, size_t size,
306 int flags, int type)
307{
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530308 int retval;
309 struct posix_acl *acl;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530310 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530311 struct inode *inode = dentry->d_inode;
312
313 if (strcmp(name, "") != 0)
314 return -EINVAL;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530315
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530316 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530317 /*
318 * set the attribute on the remote. Without even looking at the
319 * xattr value. We leave it to the server to validate
320 */
321 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
322 return v9fs_remote_set_acl(dentry, name,
323 value, size, flags, type);
324
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530325 if (S_ISLNK(inode->i_mode))
326 return -EOPNOTSUPP;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700327 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530328 return -EPERM;
329 if (value) {
330 /* update the cached acl value */
331 acl = posix_acl_from_xattr(value, size);
332 if (IS_ERR(acl))
333 return PTR_ERR(acl);
334 else if (acl) {
335 retval = posix_acl_valid(acl);
336 if (retval)
337 goto err_out;
338 }
339 } else
340 acl = NULL;
341
342 switch (type) {
343 case ACL_TYPE_ACCESS:
344 name = POSIX_ACL_XATTR_ACCESS;
345 if (acl) {
346 mode_t mode = inode->i_mode;
347 retval = posix_acl_equiv_mode(acl, &mode);
348 if (retval < 0)
349 goto err_out;
350 else {
351 struct iattr iattr;
352 if (retval == 0) {
353 /*
354 * ACL can be represented
355 * by the mode bits. So don't
356 * update ACL.
357 */
358 acl = NULL;
359 value = NULL;
360 size = 0;
361 }
362 /* Updte the mode bits */
363 iattr.ia_mode = ((mode & S_IALLUGO) |
364 (inode->i_mode & ~S_IALLUGO));
365 iattr.ia_valid = ATTR_MODE;
366 /* FIXME should we update ctime ?
367 * What is the following setxattr update the
368 * mode ?
369 */
370 v9fs_vfs_setattr_dotl(dentry, &iattr);
371 }
372 }
373 break;
374 case ACL_TYPE_DEFAULT:
375 name = POSIX_ACL_XATTR_DEFAULT;
376 if (!S_ISDIR(inode->i_mode)) {
Aneesh Kumar K.V6f81c112010-12-10 12:19:31 +0530377 retval = acl ? -EINVAL : 0;
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530378 goto err_out;
379 }
380 break;
381 default:
382 BUG();
383 }
384 retval = v9fs_xattr_set(dentry, name, value, size, flags);
385 if (!retval)
386 set_cached_acl(inode, type, acl);
387err_out:
388 posix_acl_release(acl);
389 return retval;
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530390}
391
392const struct xattr_handler v9fs_xattr_acl_access_handler = {
393 .prefix = POSIX_ACL_XATTR_ACCESS,
394 .flags = ACL_TYPE_ACCESS,
395 .get = v9fs_xattr_get_acl,
396 .set = v9fs_xattr_set_acl,
397};
398
399const struct xattr_handler v9fs_xattr_acl_default_handler = {
400 .prefix = POSIX_ACL_XATTR_DEFAULT,
401 .flags = ACL_TYPE_DEFAULT,
402 .get = v9fs_xattr_get_acl,
403 .set = v9fs_xattr_set_acl,
404};