blob: 0a2e480477a5cc78d84366869652ad385d98f289 [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.V22d8dcd2010-09-28 00:27:40 +053024#include "v9fs_vfs.h"
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +053025#include "v9fs.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);
62 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
63 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
64 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
65 return 0;
66 }
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053067 /* get the default/access acl values and cache them */
68 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
69 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
70
71 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
72 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
73 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053074 } else
75 retval = -EIO;
76
Venkateswararao Jujjuri (JV)c61fa0d2011-01-13 15:28:39 -080077 if (!IS_ERR(dacl))
78 posix_acl_release(dacl);
79
80 if (!IS_ERR(pacl))
81 posix_acl_release(pacl);
82
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053083 return retval;
84}
85
86static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
87{
88 struct posix_acl *acl;
89 /*
90 * 9p Always cache the acl value when
91 * instantiating the inode (v9fs_inode_from_fid)
92 */
93 acl = get_cached_acl(inode, type);
94 BUG_ON(acl == ACL_NOT_CACHED);
95 return acl;
96}
97
Nick Pigginb74c79e2011-01-07 17:49:58 +110098int v9fs_check_acl(struct inode *inode, int mask, unsigned int flags)
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053099{
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530100 struct posix_acl *acl;
101 struct v9fs_session_info *v9ses;
102
Nick Pigginb74c79e2011-01-07 17:49:58 +1100103 if (flags & IPERM_FLAG_RCU)
104 return -ECHILD;
105
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530106 v9ses = v9fs_inode2v9ses(inode);
107 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
108 /*
109 * On access = client mode get the acl
110 * values from the server
111 */
112 return 0;
113 }
114 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +0530115
116 if (IS_ERR(acl))
117 return PTR_ERR(acl);
118 if (acl) {
119 int error = posix_acl_permission(inode, acl, mask);
120 posix_acl_release(acl);
121 return error;
122 }
123 return -EAGAIN;
124}
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530125
Aneesh Kumar K.V6e8dc552010-09-28 00:27:40 +0530126static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
127{
128 int retval;
129 char *name;
130 size_t size;
131 void *buffer;
132 struct inode *inode = dentry->d_inode;
133
134 set_cached_acl(inode, type, acl);
Venkateswararao Jujjuri (JV)d344b0f2011-01-13 16:33:00 -0800135
136 if (!acl)
137 return 0;
138
Aneesh Kumar K.V6e8dc552010-09-28 00:27:40 +0530139 /* Set a setxattr request to server */
140 size = posix_acl_xattr_size(acl->a_count);
141 buffer = kmalloc(size, GFP_KERNEL);
142 if (!buffer)
143 return -ENOMEM;
144 retval = posix_acl_to_xattr(acl, buffer, size);
145 if (retval < 0)
146 goto err_free_out;
147 switch (type) {
148 case ACL_TYPE_ACCESS:
149 name = POSIX_ACL_XATTR_ACCESS;
150 break;
151 case ACL_TYPE_DEFAULT:
152 name = POSIX_ACL_XATTR_DEFAULT;
153 break;
154 default:
155 BUG();
156 }
157 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
158err_free_out:
159 kfree(buffer);
160 return retval;
161}
162
163int v9fs_acl_chmod(struct dentry *dentry)
164{
165 int retval = 0;
166 struct posix_acl *acl, *clone;
167 struct inode *inode = dentry->d_inode;
168
169 if (S_ISLNK(inode->i_mode))
170 return -EOPNOTSUPP;
171 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
172 if (acl) {
173 clone = posix_acl_clone(acl, GFP_KERNEL);
174 posix_acl_release(acl);
175 if (!clone)
176 return -ENOMEM;
177 retval = posix_acl_chmod_masq(clone, inode->i_mode);
178 if (!retval)
179 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
180 posix_acl_release(clone);
181 }
182 return retval;
183}
184
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530185int v9fs_set_create_acl(struct dentry *dentry,
186 struct posix_acl *dpacl, struct posix_acl *pacl)
187{
Venkateswararao Jujjuri (JV)d344b0f2011-01-13 16:33:00 -0800188 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
189 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
Aneesh Kumar K.Vad77dbc2010-09-28 00:27:40 +0530190 posix_acl_release(dpacl);
191 posix_acl_release(pacl);
192 return 0;
193}
194
195int v9fs_acl_mode(struct inode *dir, mode_t *modep,
196 struct posix_acl **dpacl, struct posix_acl **pacl)
197{
198 int retval = 0;
199 mode_t mode = *modep;
200 struct posix_acl *acl = NULL;
201
202 if (!S_ISLNK(mode)) {
203 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
204 if (IS_ERR(acl))
205 return PTR_ERR(acl);
206 if (!acl)
207 mode &= ~current_umask();
208 }
209 if (acl) {
210 struct posix_acl *clone;
211
212 if (S_ISDIR(mode))
213 *dpacl = acl;
214 clone = posix_acl_clone(acl, GFP_NOFS);
215 retval = -ENOMEM;
216 if (!clone)
217 goto cleanup;
218
219 retval = posix_acl_create_masq(clone, &mode);
220 if (retval < 0) {
221 posix_acl_release(clone);
222 goto cleanup;
223 }
224 if (retval > 0)
225 *pacl = clone;
226 }
227 *modep = mode;
228 return 0;
229cleanup:
230 posix_acl_release(acl);
231 return retval;
232
233}
234
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530235static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
236 void *buffer, size_t size, int type)
237{
238 char *full_name;
239
240 switch (type) {
241 case ACL_TYPE_ACCESS:
242 full_name = POSIX_ACL_XATTR_ACCESS;
243 break;
244 case ACL_TYPE_DEFAULT:
245 full_name = POSIX_ACL_XATTR_DEFAULT;
246 break;
247 default:
248 BUG();
249 }
250 return v9fs_xattr_get(dentry, full_name, buffer, size);
251}
252
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530253static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
254 void *buffer, size_t size, int type)
255{
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530256 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530257 struct posix_acl *acl;
258 int error;
259
260 if (strcmp(name, "") != 0)
261 return -EINVAL;
262
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530263 v9ses = v9fs_inode2v9ses(dentry->d_inode);
264 /*
265 * We allow set/get/list of acl when access=client is not specified
266 */
267 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
268 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
269
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530270 acl = v9fs_get_cached_acl(dentry->d_inode, type);
271 if (IS_ERR(acl))
272 return PTR_ERR(acl);
273 if (acl == NULL)
274 return -ENODATA;
275 error = posix_acl_to_xattr(acl, buffer, size);
276 posix_acl_release(acl);
277
278 return error;
279}
280
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530281static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
282 const void *value, size_t size,
283 int flags, int type)
284{
285 char *full_name;
286
287 switch (type) {
288 case ACL_TYPE_ACCESS:
289 full_name = POSIX_ACL_XATTR_ACCESS;
290 break;
291 case ACL_TYPE_DEFAULT:
292 full_name = POSIX_ACL_XATTR_DEFAULT;
293 break;
294 default:
295 BUG();
296 }
297 return v9fs_xattr_set(dentry, full_name, value, size, flags);
298}
299
300
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530301static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
302 const void *value, size_t size,
303 int flags, int type)
304{
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530305 int retval;
306 struct posix_acl *acl;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530307 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530308 struct inode *inode = dentry->d_inode;
309
310 if (strcmp(name, "") != 0)
311 return -EINVAL;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530312
313 v9ses = v9fs_inode2v9ses(dentry->d_inode);
314 /*
315 * set the attribute on the remote. Without even looking at the
316 * xattr value. We leave it to the server to validate
317 */
318 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
319 return v9fs_remote_set_acl(dentry, name,
320 value, size, flags, type);
321
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530322 if (S_ISLNK(inode->i_mode))
323 return -EOPNOTSUPP;
324 if (!is_owner_or_cap(inode))
325 return -EPERM;
326 if (value) {
327 /* update the cached acl value */
328 acl = posix_acl_from_xattr(value, size);
329 if (IS_ERR(acl))
330 return PTR_ERR(acl);
331 else if (acl) {
332 retval = posix_acl_valid(acl);
333 if (retval)
334 goto err_out;
335 }
336 } else
337 acl = NULL;
338
339 switch (type) {
340 case ACL_TYPE_ACCESS:
341 name = POSIX_ACL_XATTR_ACCESS;
342 if (acl) {
343 mode_t mode = inode->i_mode;
344 retval = posix_acl_equiv_mode(acl, &mode);
345 if (retval < 0)
346 goto err_out;
347 else {
348 struct iattr iattr;
349 if (retval == 0) {
350 /*
351 * ACL can be represented
352 * by the mode bits. So don't
353 * update ACL.
354 */
355 acl = NULL;
356 value = NULL;
357 size = 0;
358 }
359 /* Updte the mode bits */
360 iattr.ia_mode = ((mode & S_IALLUGO) |
361 (inode->i_mode & ~S_IALLUGO));
362 iattr.ia_valid = ATTR_MODE;
363 /* FIXME should we update ctime ?
364 * What is the following setxattr update the
365 * mode ?
366 */
367 v9fs_vfs_setattr_dotl(dentry, &iattr);
368 }
369 }
370 break;
371 case ACL_TYPE_DEFAULT:
372 name = POSIX_ACL_XATTR_DEFAULT;
373 if (!S_ISDIR(inode->i_mode)) {
Aneesh Kumar K.V6f81c112010-12-10 12:19:31 +0530374 retval = acl ? -EINVAL : 0;
Aneesh Kumar K.V22d8dcd2010-09-28 00:27:40 +0530375 goto err_out;
376 }
377 break;
378 default:
379 BUG();
380 }
381 retval = v9fs_xattr_set(dentry, name, value, size, flags);
382 if (!retval)
383 set_cached_acl(inode, type, acl);
384err_out:
385 posix_acl_release(acl);
386 return retval;
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530387}
388
389const struct xattr_handler v9fs_xattr_acl_access_handler = {
390 .prefix = POSIX_ACL_XATTR_ACCESS,
391 .flags = ACL_TYPE_ACCESS,
392 .get = v9fs_xattr_get_acl,
393 .set = v9fs_xattr_set_acl,
394};
395
396const struct xattr_handler v9fs_xattr_acl_default_handler = {
397 .prefix = POSIX_ACL_XATTR_DEFAULT,
398 .flags = ACL_TYPE_DEFAULT,
399 .get = v9fs_xattr_get_acl,
400 .set = v9fs_xattr_set_acl,
401};