Merge remote branch 'tip/x86/tsc' into fortglx/2.6.38/tip/x86/tsc
[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_vfs.h"
25 #include "v9fs.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                 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
64                 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
65                 return 0;
66         }
67         /* 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);
74                 posix_acl_release(dacl);
75                 posix_acl_release(pacl);
76         } else
77                 retval = -EIO;
78
79         return retval;
80 }
81
82 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
83 {
84         struct posix_acl *acl;
85         /*
86          * 9p Always cache the acl value when
87          * instantiating the inode (v9fs_inode_from_fid)
88          */
89         acl = get_cached_acl(inode, type);
90         BUG_ON(acl == ACL_NOT_CACHED);
91         return acl;
92 }
93
94 int v9fs_check_acl(struct inode *inode, int mask)
95 {
96         struct posix_acl *acl;
97         struct v9fs_session_info *v9ses;
98
99         v9ses = v9fs_inode2v9ses(inode);
100         if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
101                 /*
102                  * On access = client mode get the acl
103                  * values from the server
104                  */
105                 return 0;
106         }
107         acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
108
109         if (IS_ERR(acl))
110                 return PTR_ERR(acl);
111         if (acl) {
112                 int error = posix_acl_permission(inode, acl, mask);
113                 posix_acl_release(acl);
114                 return error;
115         }
116         return -EAGAIN;
117 }
118
119 static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
120 {
121         int retval;
122         char *name;
123         size_t size;
124         void *buffer;
125         struct inode *inode = dentry->d_inode;
126
127         set_cached_acl(inode, type, acl);
128         /* Set a setxattr request to server */
129         size = posix_acl_xattr_size(acl->a_count);
130         buffer = kmalloc(size, GFP_KERNEL);
131         if (!buffer)
132                 return -ENOMEM;
133         retval = posix_acl_to_xattr(acl, buffer, size);
134         if (retval < 0)
135                 goto err_free_out;
136         switch (type) {
137         case ACL_TYPE_ACCESS:
138                 name = POSIX_ACL_XATTR_ACCESS;
139                 break;
140         case ACL_TYPE_DEFAULT:
141                 name = POSIX_ACL_XATTR_DEFAULT;
142                 break;
143         default:
144                 BUG();
145         }
146         retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
147 err_free_out:
148         kfree(buffer);
149         return retval;
150 }
151
152 int v9fs_acl_chmod(struct dentry *dentry)
153 {
154         int retval = 0;
155         struct posix_acl *acl, *clone;
156         struct inode *inode = dentry->d_inode;
157
158         if (S_ISLNK(inode->i_mode))
159                 return -EOPNOTSUPP;
160         acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
161         if (acl) {
162                 clone = posix_acl_clone(acl, GFP_KERNEL);
163                 posix_acl_release(acl);
164                 if (!clone)
165                         return -ENOMEM;
166                 retval = posix_acl_chmod_masq(clone, inode->i_mode);
167                 if (!retval)
168                         retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
169                 posix_acl_release(clone);
170         }
171         return retval;
172 }
173
174 int v9fs_set_create_acl(struct dentry *dentry,
175                         struct posix_acl *dpacl, struct posix_acl *pacl)
176 {
177         if (dpacl)
178                 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
179         if (pacl)
180                 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
181         posix_acl_release(dpacl);
182         posix_acl_release(pacl);
183         return 0;
184 }
185
186 int v9fs_acl_mode(struct inode *dir, mode_t *modep,
187                   struct posix_acl **dpacl, struct posix_acl **pacl)
188 {
189         int retval = 0;
190         mode_t mode = *modep;
191         struct posix_acl *acl = NULL;
192
193         if (!S_ISLNK(mode)) {
194                 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
195                 if (IS_ERR(acl))
196                         return PTR_ERR(acl);
197                 if (!acl)
198                         mode &= ~current_umask();
199         }
200         if (acl) {
201                 struct posix_acl *clone;
202
203                 if (S_ISDIR(mode))
204                         *dpacl = acl;
205                 clone = posix_acl_clone(acl, GFP_NOFS);
206                 retval = -ENOMEM;
207                 if (!clone)
208                         goto cleanup;
209
210                 retval = posix_acl_create_masq(clone, &mode);
211                 if (retval < 0) {
212                         posix_acl_release(clone);
213                         goto cleanup;
214                 }
215                 if (retval > 0)
216                         *pacl = clone;
217         }
218         *modep  = mode;
219         return 0;
220 cleanup:
221         posix_acl_release(acl);
222         return retval;
223
224 }
225
226 static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
227                                void *buffer, size_t size, int type)
228 {
229         char *full_name;
230
231         switch (type) {
232         case ACL_TYPE_ACCESS:
233                 full_name =  POSIX_ACL_XATTR_ACCESS;
234                 break;
235         case ACL_TYPE_DEFAULT:
236                 full_name = POSIX_ACL_XATTR_DEFAULT;
237                 break;
238         default:
239                 BUG();
240         }
241         return v9fs_xattr_get(dentry, full_name, buffer, size);
242 }
243
244 static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
245                               void *buffer, size_t size, int type)
246 {
247         struct v9fs_session_info *v9ses;
248         struct posix_acl *acl;
249         int error;
250
251         if (strcmp(name, "") != 0)
252                 return -EINVAL;
253
254         v9ses = v9fs_inode2v9ses(dentry->d_inode);
255         /*
256          * We allow set/get/list of acl when access=client is not specified
257          */
258         if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
259                 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
260
261         acl = v9fs_get_cached_acl(dentry->d_inode, type);
262         if (IS_ERR(acl))
263                 return PTR_ERR(acl);
264         if (acl == NULL)
265                 return -ENODATA;
266         error = posix_acl_to_xattr(acl, buffer, size);
267         posix_acl_release(acl);
268
269         return error;
270 }
271
272 static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
273                               const void *value, size_t size,
274                               int flags, int type)
275 {
276         char *full_name;
277
278         switch (type) {
279         case ACL_TYPE_ACCESS:
280                 full_name =  POSIX_ACL_XATTR_ACCESS;
281                 break;
282         case ACL_TYPE_DEFAULT:
283                 full_name = POSIX_ACL_XATTR_DEFAULT;
284                 break;
285         default:
286                 BUG();
287         }
288         return v9fs_xattr_set(dentry, full_name, value, size, flags);
289 }
290
291
292 static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
293                               const void *value, size_t size,
294                               int flags, int type)
295 {
296         int retval;
297         struct posix_acl *acl;
298         struct v9fs_session_info *v9ses;
299         struct inode *inode = dentry->d_inode;
300
301         if (strcmp(name, "") != 0)
302                 return -EINVAL;
303
304         v9ses = v9fs_inode2v9ses(dentry->d_inode);
305         /*
306          * set the attribute on the remote. Without even looking at the
307          * xattr value. We leave it to the server to validate
308          */
309         if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
310                 return v9fs_remote_set_acl(dentry, name,
311                                            value, size, flags, type);
312
313         if (S_ISLNK(inode->i_mode))
314                 return -EOPNOTSUPP;
315         if (!is_owner_or_cap(inode))
316                 return -EPERM;
317         if (value) {
318                 /* update the cached acl value */
319                 acl = posix_acl_from_xattr(value, size);
320                 if (IS_ERR(acl))
321                         return PTR_ERR(acl);
322                 else if (acl) {
323                         retval = posix_acl_valid(acl);
324                         if (retval)
325                                 goto err_out;
326                 }
327         } else
328                 acl = NULL;
329
330         switch (type) {
331         case ACL_TYPE_ACCESS:
332                 name = POSIX_ACL_XATTR_ACCESS;
333                 if (acl) {
334                         mode_t mode = inode->i_mode;
335                         retval = posix_acl_equiv_mode(acl, &mode);
336                         if (retval < 0)
337                                 goto err_out;
338                         else {
339                                 struct iattr iattr;
340                                 if (retval == 0) {
341                                         /*
342                                          * ACL can be represented
343                                          * by the mode bits. So don't
344                                          * update ACL.
345                                          */
346                                         acl = NULL;
347                                         value = NULL;
348                                         size = 0;
349                                 }
350                                 /* Updte the mode bits */
351                                 iattr.ia_mode = ((mode & S_IALLUGO) |
352                                                  (inode->i_mode & ~S_IALLUGO));
353                                 iattr.ia_valid = ATTR_MODE;
354                                 /* FIXME should we update ctime ?
355                                  * What is the following setxattr update the
356                                  * mode ?
357                                  */
358                                 v9fs_vfs_setattr_dotl(dentry, &iattr);
359                         }
360                 }
361                 break;
362         case ACL_TYPE_DEFAULT:
363                 name = POSIX_ACL_XATTR_DEFAULT;
364                 if (!S_ISDIR(inode->i_mode)) {
365                         retval = -EINVAL;
366                         goto err_out;
367                 }
368                 break;
369         default:
370                 BUG();
371         }
372         retval = v9fs_xattr_set(dentry, name, value, size, flags);
373         if (!retval)
374                 set_cached_acl(inode, type, acl);
375 err_out:
376         posix_acl_release(acl);
377         return retval;
378 }
379
380 const struct xattr_handler v9fs_xattr_acl_access_handler = {
381         .prefix = POSIX_ACL_XATTR_ACCESS,
382         .flags  = ACL_TYPE_ACCESS,
383         .get    = v9fs_xattr_get_acl,
384         .set    = v9fs_xattr_set_acl,
385 };
386
387 const struct xattr_handler v9fs_xattr_acl_default_handler = {
388         .prefix = POSIX_ACL_XATTR_DEFAULT,
389         .flags  = ACL_TYPE_DEFAULT,
390         .get    = v9fs_xattr_get_acl,
391         .set    = v9fs_xattr_set_acl,
392 };