vserver 1.9.3
[linux-2.6.git] / fs / reiserfs / xattr_acl.c
1 #include <linux/fs.h>
2 #include <linux/posix_acl.h>
3 #include <linux/reiserfs_fs.h>
4 #include <linux/errno.h>
5 #include <linux/pagemap.h>
6 #include <linux/xattr.h>
7 #include <linux/xattr_acl.h>
8 #include <linux/reiserfs_xattr.h>
9 #include <linux/reiserfs_acl.h>
10 #include <asm/uaccess.h>
11
12 static int
13 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
14 {
15         struct posix_acl *acl;
16         int error;
17
18         if (!reiserfs_posixacl(inode->i_sb))
19                 return -EOPNOTSUPP;
20         if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
21                 return -EPERM;
22
23         if (value) {
24                 acl = posix_acl_from_xattr(value, size);
25                 if (IS_ERR(acl)) {
26                         return PTR_ERR(acl);
27                 } else if (acl) {
28                         error = posix_acl_valid(acl);
29                         if (error)
30                                 goto release_and_out;
31                 }
32         } else
33                 acl = NULL;
34
35         error = reiserfs_set_acl (inode, type, acl);
36
37 release_and_out:
38         posix_acl_release(acl);
39         return error;
40 }
41
42
43 static int
44 xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
45 {
46         struct posix_acl *acl;
47         int error;
48
49         if (!reiserfs_posixacl(inode->i_sb))
50                 return -EOPNOTSUPP;
51
52         acl = reiserfs_get_acl (inode, type);
53         if (IS_ERR(acl))
54                 return PTR_ERR(acl);
55         if (acl == NULL)
56                 return -ENODATA;
57         error = posix_acl_to_xattr(acl, buffer, size);
58         posix_acl_release(acl);
59
60         return error;
61 }
62
63
64 /*
65  * Convert from filesystem to in-memory representation.
66  */
67 static struct posix_acl *
68 posix_acl_from_disk(const void *value, size_t size)
69 {
70         const char *end = (char *)value + size;
71         int n, count;
72         struct posix_acl *acl;
73
74         if (!value)
75                 return NULL;
76         if (size < sizeof(reiserfs_acl_header))
77                  return ERR_PTR(-EINVAL);
78         if (((reiserfs_acl_header *)value)->a_version !=
79             cpu_to_le32(REISERFS_ACL_VERSION))
80                 return ERR_PTR(-EINVAL);
81         value = (char *)value + sizeof(reiserfs_acl_header);
82         count = reiserfs_acl_count(size);
83         if (count < 0)
84                 return ERR_PTR(-EINVAL);
85         if (count == 0)
86                 return NULL;
87         acl = posix_acl_alloc(count, GFP_NOFS);
88         if (!acl)
89                 return ERR_PTR(-ENOMEM);
90         for (n=0; n < count; n++) {
91                 reiserfs_acl_entry *entry =
92                         (reiserfs_acl_entry *)value;
93                 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
94                         goto fail;
95                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
96                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
97                 switch(acl->a_entries[n].e_tag) {
98                         case ACL_USER_OBJ:
99                         case ACL_GROUP_OBJ:
100                         case ACL_MASK:
101                         case ACL_OTHER:
102                                 value = (char *)value +
103                                         sizeof(reiserfs_acl_entry_short);
104                                 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
105                                 break;
106
107                         case ACL_USER:
108                         case ACL_GROUP:
109                                 value = (char *)value + sizeof(reiserfs_acl_entry);
110                                 if ((char *)value > end)
111                                         goto fail;
112                                 acl->a_entries[n].e_id =
113                                         le32_to_cpu(entry->e_id);
114                                 break;
115
116                         default:
117                                 goto fail;
118                 }
119         }
120         if (value != end)
121                 goto fail;
122         return acl;
123
124 fail:
125         posix_acl_release(acl);
126         return ERR_PTR(-EINVAL);
127 }
128
129 /*
130  * Convert from in-memory to filesystem representation.
131  */
132 static void *
133 posix_acl_to_disk(const struct posix_acl *acl, size_t *size)
134 {
135         reiserfs_acl_header *ext_acl;
136         char *e;
137         int n;
138
139         *size = reiserfs_acl_size(acl->a_count);
140         ext_acl = (reiserfs_acl_header *)kmalloc(sizeof(reiserfs_acl_header) +
141                 acl->a_count * sizeof(reiserfs_acl_entry), GFP_NOFS);
142         if (!ext_acl)
143                 return ERR_PTR(-ENOMEM);
144         ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
145         e = (char *)ext_acl + sizeof(reiserfs_acl_header);
146         for (n=0; n < acl->a_count; n++) {
147                 reiserfs_acl_entry *entry = (reiserfs_acl_entry *)e;
148                 entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
149                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
150                 switch(acl->a_entries[n].e_tag) {
151                         case ACL_USER:
152                         case ACL_GROUP:
153                                 entry->e_id =
154                                         cpu_to_le32(acl->a_entries[n].e_id);
155                                 e += sizeof(reiserfs_acl_entry);
156                                 break;
157
158                         case ACL_USER_OBJ:
159                         case ACL_GROUP_OBJ:
160                         case ACL_MASK:
161                         case ACL_OTHER:
162                                 e += sizeof(reiserfs_acl_entry_short);
163                                 break;
164
165                         default:
166                                 goto fail;
167                 }
168         }
169         return (char *)ext_acl;
170
171 fail:
172         kfree(ext_acl);
173         return ERR_PTR(-EINVAL);
174 }
175
176 /*
177  * Inode operation get_posix_acl().
178  *
179  * inode->i_sem: down
180  * BKL held [before 2.5.x]
181  */
182 struct posix_acl *
183 reiserfs_get_acl(struct inode *inode, int type)
184 {
185         char *name, *value;
186         struct posix_acl *acl, **p_acl;
187         size_t size;
188         int retval;
189         struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
190
191         switch (type) {
192             case ACL_TYPE_ACCESS:
193                 name = XATTR_NAME_ACL_ACCESS;
194                 p_acl = &reiserfs_i->i_acl_access;
195                 break;
196             case ACL_TYPE_DEFAULT:
197                 name = XATTR_NAME_ACL_DEFAULT;
198                 p_acl = &reiserfs_i->i_acl_default;
199                 break;
200             default:
201                 return ERR_PTR (-EINVAL);
202         }
203
204         if (IS_ERR (*p_acl)) {
205             if (PTR_ERR (*p_acl) == -ENODATA)
206                 return NULL;
207         } else if (*p_acl != NULL)
208             return posix_acl_dup (*p_acl);
209
210         size = reiserfs_xattr_get (inode, name, NULL, 0);
211         if ((int)size < 0) {
212             if (size == -ENODATA || size == -ENOSYS) {
213                 *p_acl = ERR_PTR (-ENODATA);
214                 return NULL;
215             }
216             return ERR_PTR (size);
217         }
218
219         value = kmalloc (size, GFP_NOFS);
220         if (!value)
221             return ERR_PTR (-ENOMEM);
222
223         retval = reiserfs_xattr_get(inode, name, value, size);
224         if (retval == -ENODATA || retval == -ENOSYS) {
225                 /* This shouldn't actually happen as it should have
226                    been caught above.. but just in case */
227                 acl = NULL;
228                 *p_acl = ERR_PTR (-ENODATA);
229         } else if (retval < 0) {
230                 acl = ERR_PTR(retval);
231         } else {
232                 acl = posix_acl_from_disk(value, retval);
233                 *p_acl = posix_acl_dup (acl);
234         }
235
236         kfree(value);
237         return acl;
238 }
239
240 /*
241  * Inode operation set_posix_acl().
242  *
243  * inode->i_sem: down
244  * BKL held [before 2.5.x]
245  */
246 int
247 reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
248 {
249         char *name;
250         void *value = NULL;
251         struct posix_acl **p_acl;
252         size_t size;
253         int error;
254         struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
255
256         if (S_ISLNK(inode->i_mode))
257                 return -EOPNOTSUPP;
258
259         switch (type) {
260             case ACL_TYPE_ACCESS:
261                 name = XATTR_NAME_ACL_ACCESS;
262                 p_acl = &reiserfs_i->i_acl_access;
263                 if (acl) {
264                     mode_t mode = inode->i_mode;
265                     error = posix_acl_equiv_mode (acl, &mode);
266                     if (error < 0)
267                         return error;
268                     else {
269                         inode->i_mode = mode;
270                         if (error == 0)
271                             acl = NULL;
272                     }
273                 }
274                 break;
275             case ACL_TYPE_DEFAULT:
276                 name = XATTR_NAME_ACL_DEFAULT;
277                 p_acl = &reiserfs_i->i_acl_default;
278                 if (!S_ISDIR (inode->i_mode))
279                     return acl ? -EACCES : 0;
280                 break;
281             default:
282                 return -EINVAL;
283         }
284
285         if (acl) {
286             value = posix_acl_to_disk(acl, &size);
287             if (IS_ERR(value))
288                 return (int)PTR_ERR(value);
289             error = reiserfs_xattr_set(inode, name, value, size, 0);
290         } else {
291             error = reiserfs_xattr_del (inode, name);
292             if (error == -ENODATA) {
293                 /* This may seem odd here, but it means that the ACL was set
294                  * with a value representable with mode bits. If there was
295                  * an ACL before, reiserfs_xattr_del already dirtied the inode.
296                  */
297                 mark_inode_dirty (inode);
298                 error = 0;
299             }
300         }
301
302         if (value)
303                 kfree(value);
304
305         if (!error) {
306             /* Release the old one */
307             if (!IS_ERR (*p_acl) && *p_acl)
308                 posix_acl_release (*p_acl);
309
310             if (acl == NULL)
311                 *p_acl = ERR_PTR (-ENODATA);
312             else
313                 *p_acl = posix_acl_dup (acl);
314         }
315
316         return error;
317 }
318
319 /* dir->i_sem: down,
320  * inode is new and not released into the wild yet */
321 int
322 reiserfs_inherit_default_acl (struct inode *dir, struct dentry *dentry, struct inode *inode)
323 {
324     struct posix_acl *acl;
325     int err = 0;
326
327     /* ACLs only get applied to files and directories */
328     if (S_ISLNK (inode->i_mode))
329         return 0;
330
331     /* ACLs can only be used on "new" objects, so if it's an old object
332      * there is nothing to inherit from */
333     if (get_inode_sd_version (dir) == STAT_DATA_V1)
334         goto apply_umask;
335
336     /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
337      * would be useless since permissions are ignored, and a pain because
338      * it introduces locking cycles */
339     if (is_reiserfs_priv_object (dir)) {
340         REISERFS_I(inode)->i_flags |= i_priv_object;
341         goto apply_umask;
342     }
343
344     acl = reiserfs_get_acl (dir, ACL_TYPE_DEFAULT);
345     if (IS_ERR (acl)) {
346         if (PTR_ERR (acl) == -ENODATA)
347             goto apply_umask;
348         return PTR_ERR (acl);
349     }
350
351     if (acl) {
352         struct posix_acl *acl_copy;
353         mode_t mode = inode->i_mode;
354         int need_acl;
355
356         /* Copy the default ACL to the default ACL of a new directory */
357         if (S_ISDIR (inode->i_mode)) {
358             err = reiserfs_set_acl (inode, ACL_TYPE_DEFAULT, acl);
359             if (err)
360                 goto cleanup;
361         }
362
363         /* Now we reconcile the new ACL and the mode,
364            potentially modifying both */
365         acl_copy = posix_acl_clone (acl, GFP_NOFS);
366         if (!acl_copy) {
367             err = -ENOMEM;
368             goto cleanup;
369         }
370
371
372         need_acl = posix_acl_create_masq (acl_copy, &mode);
373         if (need_acl >= 0) {
374             if (mode != inode->i_mode) {
375                 inode->i_mode = mode;
376             }
377
378             /* If we need an ACL.. */
379             if (need_acl > 0) {
380                 err = reiserfs_set_acl (inode, ACL_TYPE_ACCESS, acl_copy);
381                 if (err)
382                     goto cleanup_copy;
383             }
384         }
385 cleanup_copy:
386         posix_acl_release (acl_copy);
387 cleanup:
388         posix_acl_release (acl);
389     } else {
390 apply_umask:
391         /* no ACL, apply umask */
392         inode->i_mode &= ~current->fs->umask;
393     }
394
395     return err;
396 }
397
398 /* Looks up and caches the result of the default ACL.
399  * We do this so that we don't need to carry the xattr_sem into
400  * reiserfs_new_inode if we don't need to */
401 int
402 reiserfs_cache_default_acl (struct inode *inode)
403 {
404     int ret = 0;
405     if (reiserfs_posixacl (inode->i_sb) &&
406         !is_reiserfs_priv_object (inode)) {
407         struct posix_acl *acl;
408         reiserfs_read_lock_xattr_i (inode);
409         reiserfs_read_lock_xattrs (inode->i_sb);
410         acl = reiserfs_get_acl (inode, ACL_TYPE_DEFAULT);
411         reiserfs_read_unlock_xattrs (inode->i_sb);
412         reiserfs_read_unlock_xattr_i (inode);
413         ret = acl ? 1 : 0;
414         posix_acl_release (acl);
415     }
416
417     return ret;
418 }
419
420 int
421 reiserfs_acl_chmod (struct inode *inode)
422 {
423         struct posix_acl *acl, *clone;
424         int error;
425
426         if (S_ISLNK(inode->i_mode))
427                 return -EOPNOTSUPP;
428
429         if (get_inode_sd_version (inode) == STAT_DATA_V1 ||
430             !reiserfs_posixacl(inode->i_sb))
431         {
432             return 0;
433         }
434
435         reiserfs_read_lock_xattrs (inode->i_sb);
436         acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
437         reiserfs_read_unlock_xattrs (inode->i_sb);
438         if (!acl)
439                 return 0;
440         if (IS_ERR(acl))
441                 return PTR_ERR(acl);
442         clone = posix_acl_clone(acl, GFP_NOFS);
443         posix_acl_release(acl);
444         if (!clone)
445                 return -ENOMEM;
446         error = posix_acl_chmod_masq(clone, inode->i_mode);
447         if (!error) {
448                 int lock = !has_xattr_dir (inode);
449                 reiserfs_write_lock_xattr_i (inode);
450                 if (lock)
451                     reiserfs_write_lock_xattrs (inode->i_sb);
452                 else
453                     reiserfs_read_lock_xattrs (inode->i_sb);
454                 error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
455                 if (lock)
456                     reiserfs_write_unlock_xattrs (inode->i_sb);
457                 else
458                     reiserfs_read_unlock_xattrs (inode->i_sb);
459                 reiserfs_write_unlock_xattr_i (inode);
460         }
461         posix_acl_release(clone);
462         return error;
463 }
464
465 static int
466 posix_acl_access_get(struct inode *inode, const char *name,
467                           void *buffer, size_t size)
468 {
469         if (strlen(name) != sizeof(XATTR_NAME_ACL_ACCESS)-1)
470                 return -EINVAL;
471         return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
472 }
473
474 static int
475 posix_acl_access_set(struct inode *inode, const char *name,
476                           const void *value, size_t size, int flags)
477 {
478         if (strlen(name) != sizeof(XATTR_NAME_ACL_ACCESS)-1)
479                 return -EINVAL;
480         return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
481 }
482
483 static int
484 posix_acl_access_del (struct inode *inode, const char *name)
485 {
486     struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
487     struct posix_acl **acl = &reiserfs_i->i_acl_access;
488     if (strlen(name) != sizeof(XATTR_NAME_ACL_ACCESS)-1)
489         return -EINVAL;
490     if (!IS_ERR (*acl) && *acl) {
491         posix_acl_release (*acl);
492         *acl = ERR_PTR (-ENODATA);
493     }
494
495     return 0;
496 }
497
498 static int
499 posix_acl_access_list (struct inode *inode, const char *name, int namelen, char *out)
500 {
501     int len = namelen;
502     if (!reiserfs_posixacl (inode->i_sb))
503         return 0;
504     if (out)
505         memcpy (out, name, len);
506
507     return len;
508 }
509
510 struct reiserfs_xattr_handler posix_acl_access_handler = {
511         .prefix = XATTR_NAME_ACL_ACCESS,
512         .get = posix_acl_access_get,
513         .set = posix_acl_access_set,
514         .del = posix_acl_access_del,
515         .list = posix_acl_access_list,
516 };
517
518 static int
519 posix_acl_default_get (struct inode *inode, const char *name,
520                            void *buffer, size_t size)
521 {
522         if (strlen(name) != sizeof(XATTR_NAME_ACL_DEFAULT)-1)
523                 return -EINVAL;
524         return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
525 }
526
527 static int
528 posix_acl_default_set(struct inode *inode, const char *name,
529                            const void *value, size_t size, int flags)
530 {
531         if (strlen(name) != sizeof(XATTR_NAME_ACL_DEFAULT)-1)
532                 return -EINVAL;
533         return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
534 }
535
536 static int
537 posix_acl_default_del (struct inode *inode, const char *name)
538 {
539     struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
540     struct posix_acl **acl = &reiserfs_i->i_acl_default;
541     if (strlen(name) != sizeof(XATTR_NAME_ACL_DEFAULT)-1)
542         return -EINVAL;
543     if (!IS_ERR (*acl) && *acl) {
544         posix_acl_release (*acl);
545         *acl = ERR_PTR (-ENODATA);
546     }
547
548     return 0;
549 }
550
551 static int
552 posix_acl_default_list (struct inode *inode, const char *name, int namelen, char *out)
553 {
554     int len = namelen;
555     if (!reiserfs_posixacl (inode->i_sb))
556         return 0;
557     if (out)
558         memcpy (out, name, len);
559
560     return len;
561 }
562
563 struct reiserfs_xattr_handler posix_acl_default_handler = {
564         .prefix = XATTR_NAME_ACL_DEFAULT,
565         .get = posix_acl_default_get,
566         .set = posix_acl_default_set,
567         .del = posix_acl_default_del,
568         .list = posix_acl_default_list,
569 };