kill boilerplates around posix_acl_create_masq()
[pandora-kernel.git] / fs / 9p / acl.c
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
27 static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
28 {
29         ssize_t size;
30         void *value = NULL;
31         struct posix_acl *acl = NULL;
32
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
50 err_out:
51         kfree(value);
52         return acl;
53 }
54
55 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
56 {
57         int retval = 0;
58         struct posix_acl *pacl, *dacl;
59         struct v9fs_session_info *v9ses;
60
61         v9ses = v9fs_inode2v9ses(inode);
62         if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
63                         ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
64                 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
65                 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
66                 return 0;
67         }
68         /* 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);
75         } else
76                 retval = -EIO;
77
78         if (!IS_ERR(dacl))
79                 posix_acl_release(dacl);
80
81         if (!IS_ERR(pacl))
82                 posix_acl_release(pacl);
83
84         return retval;
85 }
86
87 static 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
99 int v9fs_check_acl(struct inode *inode, int mask)
100 {
101         struct posix_acl *acl;
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 -EAGAIN;
112         }
113         acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
114
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 }
124
125 static 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);
134
135         if (!acl)
136                 return 0;
137
138         /* 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);
157 err_free_out:
158         kfree(buffer);
159         return retval;
160 }
161
162 int v9fs_acl_chmod(struct dentry *dentry)
163 {
164         int retval = 0;
165         struct posix_acl *acl;
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                 retval = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
173                 if (retval)
174                         return retval;
175                 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, acl);
176                 posix_acl_release(acl);
177         }
178         return retval;
179 }
180
181 int v9fs_set_create_acl(struct dentry *dentry,
182                         struct posix_acl **dpacl, struct posix_acl **pacl)
183 {
184         if (dentry) {
185                 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, *dpacl);
186                 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, *pacl);
187         }
188         posix_acl_release(*dpacl);
189         posix_acl_release(*pacl);
190         *dpacl = *pacl = NULL;
191         return 0;
192 }
193
194 int v9fs_acl_mode(struct inode *dir, mode_t *modep,
195                   struct posix_acl **dpacl, struct posix_acl **pacl)
196 {
197         int retval = 0;
198         mode_t mode = *modep;
199         struct posix_acl *acl = NULL;
200
201         if (!S_ISLNK(mode)) {
202                 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
203                 if (IS_ERR(acl))
204                         return PTR_ERR(acl);
205                 if (!acl)
206                         mode &= ~current_umask();
207         }
208         if (acl) {
209                 if (S_ISDIR(mode))
210                         *dpacl = posix_acl_dup(acl);
211                 retval = posix_acl_create(&acl, GFP_NOFS, &mode);
212                 if (retval < 0)
213                         return retval;
214                 if (retval > 0)
215                         *pacl = acl;
216                 else
217                         posix_acl_release(acl);
218         }
219         *modep  = mode;
220         return 0;
221 }
222
223 static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
224                                void *buffer, size_t size, int type)
225 {
226         char *full_name;
227
228         switch (type) {
229         case ACL_TYPE_ACCESS:
230                 full_name =  POSIX_ACL_XATTR_ACCESS;
231                 break;
232         case ACL_TYPE_DEFAULT:
233                 full_name = POSIX_ACL_XATTR_DEFAULT;
234                 break;
235         default:
236                 BUG();
237         }
238         return v9fs_xattr_get(dentry, full_name, buffer, size);
239 }
240
241 static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
242                               void *buffer, size_t size, int type)
243 {
244         struct v9fs_session_info *v9ses;
245         struct posix_acl *acl;
246         int error;
247
248         if (strcmp(name, "") != 0)
249                 return -EINVAL;
250
251         v9ses = v9fs_dentry2v9ses(dentry);
252         /*
253          * We allow set/get/list of acl when access=client is not specified
254          */
255         if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
256                 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
257
258         acl = v9fs_get_cached_acl(dentry->d_inode, type);
259         if (IS_ERR(acl))
260                 return PTR_ERR(acl);
261         if (acl == NULL)
262                 return -ENODATA;
263         error = posix_acl_to_xattr(acl, buffer, size);
264         posix_acl_release(acl);
265
266         return error;
267 }
268
269 static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
270                               const void *value, size_t size,
271                               int flags, int type)
272 {
273         char *full_name;
274
275         switch (type) {
276         case ACL_TYPE_ACCESS:
277                 full_name =  POSIX_ACL_XATTR_ACCESS;
278                 break;
279         case ACL_TYPE_DEFAULT:
280                 full_name = POSIX_ACL_XATTR_DEFAULT;
281                 break;
282         default:
283                 BUG();
284         }
285         return v9fs_xattr_set(dentry, full_name, value, size, flags);
286 }
287
288
289 static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
290                               const void *value, size_t size,
291                               int flags, int type)
292 {
293         int retval;
294         struct posix_acl *acl;
295         struct v9fs_session_info *v9ses;
296         struct inode *inode = dentry->d_inode;
297
298         if (strcmp(name, "") != 0)
299                 return -EINVAL;
300
301         v9ses = v9fs_dentry2v9ses(dentry);
302         /*
303          * set the attribute on the remote. Without even looking at the
304          * xattr value. We leave it to the server to validate
305          */
306         if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
307                 return v9fs_remote_set_acl(dentry, name,
308                                            value, size, flags, type);
309
310         if (S_ISLNK(inode->i_mode))
311                 return -EOPNOTSUPP;
312         if (!inode_owner_or_capable(inode))
313                 return -EPERM;
314         if (value) {
315                 /* update the cached acl value */
316                 acl = posix_acl_from_xattr(value, size);
317                 if (IS_ERR(acl))
318                         return PTR_ERR(acl);
319                 else if (acl) {
320                         retval = posix_acl_valid(acl);
321                         if (retval)
322                                 goto err_out;
323                 }
324         } else
325                 acl = NULL;
326
327         switch (type) {
328         case ACL_TYPE_ACCESS:
329                 name = POSIX_ACL_XATTR_ACCESS;
330                 if (acl) {
331                         mode_t mode = inode->i_mode;
332                         retval = posix_acl_equiv_mode(acl, &mode);
333                         if (retval < 0)
334                                 goto err_out;
335                         else {
336                                 struct iattr iattr;
337                                 if (retval == 0) {
338                                         /*
339                                          * ACL can be represented
340                                          * by the mode bits. So don't
341                                          * update ACL.
342                                          */
343                                         acl = NULL;
344                                         value = NULL;
345                                         size = 0;
346                                 }
347                                 /* Updte the mode bits */
348                                 iattr.ia_mode = ((mode & S_IALLUGO) |
349                                                  (inode->i_mode & ~S_IALLUGO));
350                                 iattr.ia_valid = ATTR_MODE;
351                                 /* FIXME should we update ctime ?
352                                  * What is the following setxattr update the
353                                  * mode ?
354                                  */
355                                 v9fs_vfs_setattr_dotl(dentry, &iattr);
356                         }
357                 }
358                 break;
359         case ACL_TYPE_DEFAULT:
360                 name = POSIX_ACL_XATTR_DEFAULT;
361                 if (!S_ISDIR(inode->i_mode)) {
362                         retval = acl ? -EINVAL : 0;
363                         goto err_out;
364                 }
365                 break;
366         default:
367                 BUG();
368         }
369         retval = v9fs_xattr_set(dentry, name, value, size, flags);
370         if (!retval)
371                 set_cached_acl(inode, type, acl);
372 err_out:
373         posix_acl_release(acl);
374         return retval;
375 }
376
377 const struct xattr_handler v9fs_xattr_acl_access_handler = {
378         .prefix = POSIX_ACL_XATTR_ACCESS,
379         .flags  = ACL_TYPE_ACCESS,
380         .get    = v9fs_xattr_get_acl,
381         .set    = v9fs_xattr_set_acl,
382 };
383
384 const struct xattr_handler v9fs_xattr_acl_default_handler = {
385         .prefix = POSIX_ACL_XATTR_DEFAULT,
386         .flags  = ACL_TYPE_DEFAULT,
387         .get    = v9fs_xattr_get_acl,
388         .set    = v9fs_xattr_set_acl,
389 };