xfs: convert attr to use unsigned names
[safe/jmp/linux-2.6] / fs / xfs / linux-2.6 / xfs_acl.c
1 /*
2  * Copyright (c) 2008, Christoph Hellwig
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_acl.h"
20 #include "xfs_attr.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_inode.h"
23 #include "xfs_vnodeops.h"
24 #include "xfs_trace.h"
25 #include <linux/xattr.h>
26 #include <linux/posix_acl_xattr.h>
27
28
29 /*
30  * Locking scheme:
31  *  - all ACL updates are protected by inode->i_mutex, which is taken before
32  *    calling into this file.
33  */
34
35 STATIC struct posix_acl *
36 xfs_acl_from_disk(struct xfs_acl *aclp)
37 {
38         struct posix_acl_entry *acl_e;
39         struct posix_acl *acl;
40         struct xfs_acl_entry *ace;
41         int count, i;
42
43         count = be32_to_cpu(aclp->acl_cnt);
44
45         acl = posix_acl_alloc(count, GFP_KERNEL);
46         if (!acl)
47                 return ERR_PTR(-ENOMEM);
48
49         for (i = 0; i < count; i++) {
50                 acl_e = &acl->a_entries[i];
51                 ace = &aclp->acl_entry[i];
52
53                 /*
54                  * The tag is 32 bits on disk and 16 bits in core.
55                  *
56                  * Because every access to it goes through the core
57                  * format first this is not a problem.
58                  */
59                 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
60                 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
61
62                 switch (acl_e->e_tag) {
63                 case ACL_USER:
64                 case ACL_GROUP:
65                         acl_e->e_id = be32_to_cpu(ace->ae_id);
66                         break;
67                 case ACL_USER_OBJ:
68                 case ACL_GROUP_OBJ:
69                 case ACL_MASK:
70                 case ACL_OTHER:
71                         acl_e->e_id = ACL_UNDEFINED_ID;
72                         break;
73                 default:
74                         goto fail;
75                 }
76         }
77         return acl;
78
79 fail:
80         posix_acl_release(acl);
81         return ERR_PTR(-EINVAL);
82 }
83
84 STATIC void
85 xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
86 {
87         const struct posix_acl_entry *acl_e;
88         struct xfs_acl_entry *ace;
89         int i;
90
91         aclp->acl_cnt = cpu_to_be32(acl->a_count);
92         for (i = 0; i < acl->a_count; i++) {
93                 ace = &aclp->acl_entry[i];
94                 acl_e = &acl->a_entries[i];
95
96                 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
97                 ace->ae_id = cpu_to_be32(acl_e->e_id);
98                 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
99         }
100 }
101
102 struct posix_acl *
103 xfs_get_acl(struct inode *inode, int type)
104 {
105         struct xfs_inode *ip = XFS_I(inode);
106         struct posix_acl *acl;
107         struct xfs_acl *xfs_acl;
108         int len = sizeof(struct xfs_acl);
109         unsigned char *ea_name;
110         int error;
111
112         acl = get_cached_acl(inode, type);
113         if (acl != ACL_NOT_CACHED)
114                 return acl;
115
116         switch (type) {
117         case ACL_TYPE_ACCESS:
118                 ea_name = SGI_ACL_FILE;
119                 break;
120         case ACL_TYPE_DEFAULT:
121                 ea_name = SGI_ACL_DEFAULT;
122                 break;
123         default:
124                 BUG();
125         }
126
127         /*
128          * If we have a cached ACLs value just return it, not need to
129          * go out to the disk.
130          */
131
132         xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
133         if (!xfs_acl)
134                 return ERR_PTR(-ENOMEM);
135
136         error = -xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
137                                                         &len, ATTR_ROOT);
138         if (error) {
139                 /*
140                  * If the attribute doesn't exist make sure we have a negative
141                  * cache entry, for any other error assume it is transient and
142                  * leave the cache entry as ACL_NOT_CACHED.
143                  */
144                 if (error == -ENOATTR) {
145                         acl = NULL;
146                         goto out_update_cache;
147                 }
148                 goto out;
149         }
150
151         acl = xfs_acl_from_disk(xfs_acl);
152         if (IS_ERR(acl))
153                 goto out;
154
155  out_update_cache:
156         set_cached_acl(inode, type, acl);
157  out:
158         kfree(xfs_acl);
159         return acl;
160 }
161
162 STATIC int
163 xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
164 {
165         struct xfs_inode *ip = XFS_I(inode);
166         unsigned char *ea_name;
167         int error;
168
169         if (S_ISLNK(inode->i_mode))
170                 return -EOPNOTSUPP;
171
172         switch (type) {
173         case ACL_TYPE_ACCESS:
174                 ea_name = SGI_ACL_FILE;
175                 break;
176         case ACL_TYPE_DEFAULT:
177                 if (!S_ISDIR(inode->i_mode))
178                         return acl ? -EACCES : 0;
179                 ea_name = SGI_ACL_DEFAULT;
180                 break;
181         default:
182                 return -EINVAL;
183         }
184
185         if (acl) {
186                 struct xfs_acl *xfs_acl;
187                 int len;
188
189                 xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
190                 if (!xfs_acl)
191                         return -ENOMEM;
192
193                 xfs_acl_to_disk(xfs_acl, acl);
194                 len = sizeof(struct xfs_acl) -
195                         (sizeof(struct xfs_acl_entry) *
196                          (XFS_ACL_MAX_ENTRIES - acl->a_count));
197
198                 error = -xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
199                                 len, ATTR_ROOT);
200
201                 kfree(xfs_acl);
202         } else {
203                 /*
204                  * A NULL ACL argument means we want to remove the ACL.
205                  */
206                 error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT);
207
208                 /*
209                  * If the attribute didn't exist to start with that's fine.
210                  */
211                 if (error == -ENOATTR)
212                         error = 0;
213         }
214
215         if (!error)
216                 set_cached_acl(inode, type, acl);
217         return error;
218 }
219
220 int
221 xfs_check_acl(struct inode *inode, int mask)
222 {
223         struct xfs_inode *ip = XFS_I(inode);
224         struct posix_acl *acl;
225         int error = -EAGAIN;
226
227         xfs_itrace_entry(ip);
228
229         /*
230          * If there is no attribute fork no ACL exists on this inode and
231          * we can skip the whole exercise.
232          */
233         if (!XFS_IFORK_Q(ip))
234                 return -EAGAIN;
235
236         acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
237         if (IS_ERR(acl))
238                 return PTR_ERR(acl);
239         if (acl) {
240                 error = posix_acl_permission(inode, acl, mask);
241                 posix_acl_release(acl);
242         }
243
244         return error;
245 }
246
247 static int
248 xfs_set_mode(struct inode *inode, mode_t mode)
249 {
250         int error = 0;
251
252         if (mode != inode->i_mode) {
253                 struct iattr iattr;
254
255                 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
256                 iattr.ia_mode = mode;
257                 iattr.ia_ctime = current_fs_time(inode->i_sb);
258
259                 error = -xfs_setattr(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
260         }
261
262         return error;
263 }
264
265 static int
266 xfs_acl_exists(struct inode *inode, unsigned char *name)
267 {
268         int len = sizeof(struct xfs_acl);
269
270         return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
271                             ATTR_ROOT|ATTR_KERNOVAL) == 0);
272 }
273
274 int
275 posix_acl_access_exists(struct inode *inode)
276 {
277         return xfs_acl_exists(inode, SGI_ACL_FILE);
278 }
279
280 int
281 posix_acl_default_exists(struct inode *inode)
282 {
283         if (!S_ISDIR(inode->i_mode))
284                 return 0;
285         return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
286 }
287
288 /*
289  * No need for i_mutex because the inode is not yet exposed to the VFS.
290  */
291 int
292 xfs_inherit_acl(struct inode *inode, struct posix_acl *default_acl)
293 {
294         struct posix_acl *clone;
295         mode_t mode;
296         int error = 0, inherit = 0;
297
298         if (S_ISDIR(inode->i_mode)) {
299                 error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl);
300                 if (error)
301                         return error;
302         }
303
304         clone = posix_acl_clone(default_acl, GFP_KERNEL);
305         if (!clone)
306                 return -ENOMEM;
307
308         mode = inode->i_mode;
309         error = posix_acl_create_masq(clone, &mode);
310         if (error < 0)
311                 goto out_release_clone;
312
313         /*
314          * If posix_acl_create_masq returns a positive value we need to
315          * inherit a permission that can't be represented using the Unix
316          * mode bits and we actually need to set an ACL.
317          */
318         if (error > 0)
319                 inherit = 1;
320
321         error = xfs_set_mode(inode, mode);
322         if (error)
323                 goto out_release_clone;
324
325         if (inherit)
326                 error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
327
328  out_release_clone:
329         posix_acl_release(clone);
330         return error;
331 }
332
333 int
334 xfs_acl_chmod(struct inode *inode)
335 {
336         struct posix_acl *acl, *clone;
337         int error;
338
339         if (S_ISLNK(inode->i_mode))
340                 return -EOPNOTSUPP;
341
342         acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
343         if (IS_ERR(acl) || !acl)
344                 return PTR_ERR(acl);
345
346         clone = posix_acl_clone(acl, GFP_KERNEL);
347         posix_acl_release(acl);
348         if (!clone)
349                 return -ENOMEM;
350
351         error = posix_acl_chmod_masq(clone, inode->i_mode);
352         if (!error)
353                 error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
354
355         posix_acl_release(clone);
356         return error;
357 }
358
359 static int
360 xfs_xattr_acl_get(struct dentry *dentry, const char *name,
361                 void *value, size_t size, int type)
362 {
363         struct posix_acl *acl;
364         int error;
365
366         acl = xfs_get_acl(dentry->d_inode, type);
367         if (IS_ERR(acl))
368                 return PTR_ERR(acl);
369         if (acl == NULL)
370                 return -ENODATA;
371
372         error = posix_acl_to_xattr(acl, value, size);
373         posix_acl_release(acl);
374
375         return error;
376 }
377
378 static int
379 xfs_xattr_acl_set(struct dentry *dentry, const char *name,
380                 const void *value, size_t size, int flags, int type)
381 {
382         struct inode *inode = dentry->d_inode;
383         struct posix_acl *acl = NULL;
384         int error = 0;
385
386         if (flags & XATTR_CREATE)
387                 return -EINVAL;
388         if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
389                 return value ? -EACCES : 0;
390         if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
391                 return -EPERM;
392
393         if (!value)
394                 goto set_acl;
395
396         acl = posix_acl_from_xattr(value, size);
397         if (!acl) {
398                 /*
399                  * acl_set_file(3) may request that we set default ACLs with
400                  * zero length -- defend (gracefully) against that here.
401                  */
402                 goto out;
403         }
404         if (IS_ERR(acl)) {
405                 error = PTR_ERR(acl);
406                 goto out;
407         }
408
409         error = posix_acl_valid(acl);
410         if (error)
411                 goto out_release;
412
413         error = -EINVAL;
414         if (acl->a_count > XFS_ACL_MAX_ENTRIES)
415                 goto out_release;
416
417         if (type == ACL_TYPE_ACCESS) {
418                 mode_t mode = inode->i_mode;
419                 error = posix_acl_equiv_mode(acl, &mode);
420
421                 if (error <= 0) {
422                         posix_acl_release(acl);
423                         acl = NULL;
424
425                         if (error < 0)
426                                 return error;
427                 }
428
429                 error = xfs_set_mode(inode, mode);
430                 if (error)
431                         goto out_release;
432         }
433
434  set_acl:
435         error = xfs_set_acl(inode, type, acl);
436  out_release:
437         posix_acl_release(acl);
438  out:
439         return error;
440 }
441
442 struct xattr_handler xfs_xattr_acl_access_handler = {
443         .prefix = POSIX_ACL_XATTR_ACCESS,
444         .flags  = ACL_TYPE_ACCESS,
445         .get    = xfs_xattr_acl_get,
446         .set    = xfs_xattr_acl_set,
447 };
448
449 struct xattr_handler xfs_xattr_acl_default_handler = {
450         .prefix = POSIX_ACL_XATTR_DEFAULT,
451         .flags  = ACL_TYPE_DEFAULT,
452         .get    = xfs_xattr_acl_get,
453         .set    = xfs_xattr_acl_set,
454 };