blob: e98f56d3787dff9f68bbd80772f8690f903ee114 [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
Al Viro9c2c7032011-06-20 19:06:22 -0400104 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100105 return -ECHILD;
106
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530107 v9ses = v9fs_inode2v9ses(inode);
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800108 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
109 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530110 /*
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800111 * On access = client and acl = on mode get the acl
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530112 * values from the server
113 */
114 return 0;
115 }
116 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +0530117
118 if (IS_ERR(acl))
119 return PTR_ERR(acl);
120 if (acl) {
121 int error = posix_acl_permission(inode, acl, mask);
122 posix_acl_release(acl);
123 return error;
124 }
125 return -EAGAIN;
126}
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530127
Aneesh Kumar K.V6e8dc552010-09-28 00:27:40 +0530128static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
129{
130 int retval;
131 char *name;
132 size_t size;
133 void *buffer;
134 struct inode *inode = dentry->d_inode;
135
136 set_cached_acl(inode, type, acl);
Venkateswararao Jujjuri (JV)d344b0f2011-01-13 16:33:00 -0800137
138 if (!acl)
139 return 0;
140
Aneesh Kumar K.V6e8dc552010-09-28 00:27:40 +0530141 /* Set a setxattr request to server */
142 size = posix_acl_xattr_size(acl->a_count);
143 buffer = kmalloc(size, GFP_KERNEL);
144 if (!buffer)
145 return -ENOMEM;
146 retval = posix_acl_to_xattr(acl, buffer, size);
147 if (retval < 0)
148 goto err_free_out;
149 switch (type) {
150 case ACL_TYPE_ACCESS:
151 name = POSIX_ACL_XATTR_ACCESS;
152 break;
153 case ACL_TYPE_DEFAULT:
154 name = POSIX_ACL_XATTR_DEFAULT;
155 break;
156 default:
157 BUG();
158 }
159 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
160err_free_out:
161 kfree(buffer);
162 return retval;
163}
164
165int v9fs_acl_chmod(struct dentry *dentry)
166{
167 int retval = 0;
168 struct posix_acl *acl, *clone;
169 struct inode *inode = dentry->d_inode;
170
171 if (S_ISLNK(inode->i_mode))
172 return -EOPNOTSUPP;
173 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
174 if (acl) {
175 clone = posix_acl_clone(acl, GFP_KERNEL);
176 posix_acl_release(acl);
177 if (!clone)
178 return -ENOMEM;
179 retval = posix_acl_chmod_masq(clone, inode->i_mode);
180 if (!retval)
181 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
182 posix_acl_release(clone);
183 }
184 return retval;
185}
186
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530187int v9fs_set_create_acl(struct dentry *dentry,
188 struct posix_acl *dpacl, struct posix_acl *pacl)
189{
Venkateswararao Jujjuri (JV)d344b0f2011-01-13 16:33:00 -0800190 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
191 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530192 posix_acl_release(dpacl);
193 posix_acl_release(pacl);
194 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))
215 *dpacl = acl;
216 clone = posix_acl_clone(acl, GFP_NOFS);
217 retval = -ENOMEM;
218 if (!clone)
219 goto cleanup;
220
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;
228 }
229 *modep = mode;
230 return 0;
231cleanup:
232 posix_acl_release(acl);
233 return retval;
234
235}
236
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530237static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
238 void *buffer, size_t size, int type)
239{
240 char *full_name;
241
242 switch (type) {
243 case ACL_TYPE_ACCESS:
244 full_name = POSIX_ACL_XATTR_ACCESS;
245 break;
246 case ACL_TYPE_DEFAULT:
247 full_name = POSIX_ACL_XATTR_DEFAULT;
248 break;
249 default:
250 BUG();
251 }
252 return v9fs_xattr_get(dentry, full_name, buffer, size);
253}
254
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530255static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
256 void *buffer, size_t size, int type)
257{
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530258 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530259 struct posix_acl *acl;
260 int error;
261
262 if (strcmp(name, "") != 0)
263 return -EINVAL;
264
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530265 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530266 /*
267 * We allow set/get/list of acl when access=client is not specified
268 */
269 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
270 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
271
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530272 acl = v9fs_get_cached_acl(dentry->d_inode, type);
273 if (IS_ERR(acl))
274 return PTR_ERR(acl);
275 if (acl == NULL)
276 return -ENODATA;
277 error = posix_acl_to_xattr(acl, buffer, size);
278 posix_acl_release(acl);
279
280 return error;
281}
282
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530283static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
284 const void *value, size_t size,
285 int flags, int type)
286{
287 char *full_name;
288
289 switch (type) {
290 case ACL_TYPE_ACCESS:
291 full_name = POSIX_ACL_XATTR_ACCESS;
292 break;
293 case ACL_TYPE_DEFAULT:
294 full_name = POSIX_ACL_XATTR_DEFAULT;
295 break;
296 default:
297 BUG();
298 }
299 return v9fs_xattr_set(dentry, full_name, value, size, flags);
300}
301
302
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530303static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
304 const void *value, size_t size,
305 int flags, int type)
306{
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530307 int retval;
308 struct posix_acl *acl;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530309 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530310 struct inode *inode = dentry->d_inode;
311
312 if (strcmp(name, "") != 0)
313 return -EINVAL;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530314
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530315 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530316 /*
317 * set the attribute on the remote. Without even looking at the
318 * xattr value. We leave it to the server to validate
319 */
320 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
321 return v9fs_remote_set_acl(dentry, name,
322 value, size, flags, type);
323
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530324 if (S_ISLNK(inode->i_mode))
325 return -EOPNOTSUPP;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700326 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530327 return -EPERM;
328 if (value) {
329 /* update the cached acl value */
330 acl = posix_acl_from_xattr(value, size);
331 if (IS_ERR(acl))
332 return PTR_ERR(acl);
333 else if (acl) {
334 retval = posix_acl_valid(acl);
335 if (retval)
336 goto err_out;
337 }
338 } else
339 acl = NULL;
340
341 switch (type) {
342 case ACL_TYPE_ACCESS:
343 name = POSIX_ACL_XATTR_ACCESS;
344 if (acl) {
345 mode_t mode = inode->i_mode;
346 retval = posix_acl_equiv_mode(acl, &mode);
347 if (retval < 0)
348 goto err_out;
349 else {
350 struct iattr iattr;
351 if (retval == 0) {
352 /*
353 * ACL can be represented
354 * by the mode bits. So don't
355 * update ACL.
356 */
357 acl = NULL;
358 value = NULL;
359 size = 0;
360 }
361 /* Updte the mode bits */
362 iattr.ia_mode = ((mode & S_IALLUGO) |
363 (inode->i_mode & ~S_IALLUGO));
364 iattr.ia_valid = ATTR_MODE;
365 /* FIXME should we update ctime ?
366 * What is the following setxattr update the
367 * mode ?
368 */
369 v9fs_vfs_setattr_dotl(dentry, &iattr);
370 }
371 }
372 break;
373 case ACL_TYPE_DEFAULT:
374 name = POSIX_ACL_XATTR_DEFAULT;
375 if (!S_ISDIR(inode->i_mode)) {
Aneesh Kumar K.V6f81c112010-12-10 12:19:31 +0530376 retval = acl ? -EINVAL : 0;
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530377 goto err_out;
378 }
379 break;
380 default:
381 BUG();
382 }
383 retval = v9fs_xattr_set(dentry, name, value, size, flags);
384 if (!retval)
385 set_cached_acl(inode, type, acl);
386err_out:
387 posix_acl_release(acl);
388 return retval;
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530389}
390
391const struct xattr_handler v9fs_xattr_acl_access_handler = {
392 .prefix = POSIX_ACL_XATTR_ACCESS,
393 .flags = ACL_TYPE_ACCESS,
394 .get = v9fs_xattr_get_acl,
395 .set = v9fs_xattr_set_acl,
396};
397
398const struct xattr_handler v9fs_xattr_acl_default_handler = {
399 .prefix = POSIX_ACL_XATTR_DEFAULT,
400 .flags = ACL_TYPE_DEFAULT,
401 .get = v9fs_xattr_get_acl,
402 .set = v9fs_xattr_set_acl,
403};