This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / ext3 / acl.c
1 /*
2  * linux/fs/ext3/acl.c
3  *
4  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5  */
6
7 #include <linux/init.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/namei.h> 
12 #include <linux/ext3_jbd.h>
13 #include <linux/ext3_fs.h>
14 #include "xattr.h"
15 #include "acl.h"
16
17 /*
18  * Convert from filesystem to in-memory representation.
19  */
20 static struct posix_acl *
21 ext3_acl_from_disk(const void *value, size_t size)
22 {
23         const char *end = (char *)value + size;
24         int n, count;
25         struct posix_acl *acl;
26
27         if (!value)
28                 return NULL;
29         if (size < sizeof(ext3_acl_header))
30                  return ERR_PTR(-EINVAL);
31         if (((ext3_acl_header *)value)->a_version !=
32             cpu_to_le32(EXT3_ACL_VERSION))
33                 return ERR_PTR(-EINVAL);
34         value = (char *)value + sizeof(ext3_acl_header);
35         count = ext3_acl_count(size);
36         if (count < 0)
37                 return ERR_PTR(-EINVAL);
38         if (count == 0)
39                 return NULL;
40         acl = posix_acl_alloc(count, GFP_KERNEL);
41         if (!acl)
42                 return ERR_PTR(-ENOMEM);
43         for (n=0; n < count; n++) {
44                 ext3_acl_entry *entry =
45                         (ext3_acl_entry *)value;
46                 if ((char *)value + sizeof(ext3_acl_entry_short) > end)
47                         goto fail;
48                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
49                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
50                 switch(acl->a_entries[n].e_tag) {
51                         case ACL_USER_OBJ:
52                         case ACL_GROUP_OBJ:
53                         case ACL_MASK:
54                         case ACL_OTHER:
55                                 value = (char *)value +
56                                         sizeof(ext3_acl_entry_short);
57                                 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
58                                 break;
59
60                         case ACL_USER:
61                         case ACL_GROUP:
62                                 value = (char *)value + sizeof(ext3_acl_entry);
63                                 if ((char *)value > end)
64                                         goto fail;
65                                 acl->a_entries[n].e_id =
66                                         le32_to_cpu(entry->e_id);
67                                 break;
68
69                         default:
70                                 goto fail;
71                 }
72         }
73         if (value != end)
74                 goto fail;
75         return acl;
76
77 fail:
78         posix_acl_release(acl);
79         return ERR_PTR(-EINVAL);
80 }
81
82 /*
83  * Convert from in-memory to filesystem representation.
84  */
85 static void *
86 ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
87 {
88         ext3_acl_header *ext_acl;
89         char *e;
90         size_t n;
91
92         *size = ext3_acl_size(acl->a_count);
93         ext_acl = (ext3_acl_header *)kmalloc(sizeof(ext3_acl_header) +
94                 acl->a_count * sizeof(ext3_acl_entry), GFP_KERNEL);
95         if (!ext_acl)
96                 return ERR_PTR(-ENOMEM);
97         ext_acl->a_version = cpu_to_le32(EXT3_ACL_VERSION);
98         e = (char *)ext_acl + sizeof(ext3_acl_header);
99         for (n=0; n < acl->a_count; n++) {
100                 ext3_acl_entry *entry = (ext3_acl_entry *)e;
101                 entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
102                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
103                 switch(acl->a_entries[n].e_tag) {
104                         case ACL_USER:
105                         case ACL_GROUP:
106                                 entry->e_id =
107                                         cpu_to_le32(acl->a_entries[n].e_id);
108                                 e += sizeof(ext3_acl_entry);
109                                 break;
110
111                         case ACL_USER_OBJ:
112                         case ACL_GROUP_OBJ:
113                         case ACL_MASK:
114                         case ACL_OTHER:
115                                 e += sizeof(ext3_acl_entry_short);
116                                 break;
117
118                         default:
119                                 goto fail;
120                 }
121         }
122         return (char *)ext_acl;
123
124 fail:
125         kfree(ext_acl);
126         return ERR_PTR(-EINVAL);
127 }
128
129 static inline struct posix_acl *
130 ext3_iget_acl(struct inode *inode, struct posix_acl **i_acl)
131 {
132         struct posix_acl *acl = EXT3_ACL_NOT_CACHED;
133
134         spin_lock(&inode->i_lock);
135         if (*i_acl != EXT3_ACL_NOT_CACHED)
136                 acl = posix_acl_dup(*i_acl);
137         spin_unlock(&inode->i_lock);
138
139         return acl;
140 }
141
142 static inline void
143 ext3_iset_acl(struct inode *inode, struct posix_acl **i_acl,
144                   struct posix_acl *acl)
145 {
146         spin_lock(&inode->i_lock);
147         if (*i_acl != EXT3_ACL_NOT_CACHED)
148                 posix_acl_release(*i_acl);
149         *i_acl = posix_acl_dup(acl);
150         spin_unlock(&inode->i_lock);
151 }
152
153 /*
154  * Inode operation get_posix_acl().
155  *
156  * inode->i_sem: don't care
157  */
158 static struct posix_acl *
159 ext3_get_acl(struct inode *inode, int type)
160 {
161         struct ext3_inode_info *ei = EXT3_I(inode);
162         int name_index;
163         char *value = NULL;
164         struct posix_acl *acl;
165         int retval;
166
167         if (!test_opt(inode->i_sb, POSIX_ACL))
168                 return NULL;
169
170         switch(type) {
171                 case ACL_TYPE_ACCESS:
172                         acl = ext3_iget_acl(inode, &ei->i_acl);
173                         if (acl != EXT3_ACL_NOT_CACHED)
174                                 return acl;
175                         name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
176                         break;
177
178                 case ACL_TYPE_DEFAULT:
179                         acl = ext3_iget_acl(inode, &ei->i_default_acl);
180                         if (acl != EXT3_ACL_NOT_CACHED)
181                                 return acl;
182                         name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
183                         break;
184
185                 default:
186                         return ERR_PTR(-EINVAL);
187         }
188         retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
189         if (retval > 0) {
190                 value = kmalloc(retval, GFP_KERNEL);
191                 if (!value)
192                         return ERR_PTR(-ENOMEM);
193                 retval = ext3_xattr_get(inode, name_index, "", value, retval);
194         }
195         if (retval > 0)
196                 acl = ext3_acl_from_disk(value, retval);
197         else if (retval == -ENODATA || retval == -ENOSYS)
198                 acl = NULL;
199         else
200                 acl = ERR_PTR(retval);
201         if (value)
202                 kfree(value);
203
204         if (!IS_ERR(acl)) {
205                 switch(type) {
206                         case ACL_TYPE_ACCESS:
207                                 ext3_iset_acl(inode, &ei->i_acl, acl);
208                                 break;
209
210                         case ACL_TYPE_DEFAULT:
211                                 ext3_iset_acl(inode, &ei->i_default_acl, acl);
212                                 break;
213                 }
214         }
215         return acl;
216 }
217
218 /*
219  * Set the access or default ACL of an inode.
220  *
221  * inode->i_sem: down unless called from ext3_new_inode
222  */
223 static int
224 ext3_set_acl(handle_t *handle, struct inode *inode, int type,
225              struct posix_acl *acl)
226 {
227         struct ext3_inode_info *ei = EXT3_I(inode);
228         int name_index;
229         void *value = NULL;
230         size_t size;
231         int error;
232
233         if (S_ISLNK(inode->i_mode))
234                 return -EOPNOTSUPP;
235
236         switch(type) {
237                 case ACL_TYPE_ACCESS:
238                         name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
239                         if (acl) {
240                                 mode_t mode = inode->i_mode;
241                                 error = posix_acl_equiv_mode(acl, &mode);
242                                 if (error < 0)
243                                         return error;
244                                 else {
245                                         inode->i_mode = mode;
246                                         ext3_mark_inode_dirty(handle, inode);
247                                         if (error == 0)
248                                                 acl = NULL;
249                                 }
250                         }
251                         break;
252
253                 case ACL_TYPE_DEFAULT:
254                         name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
255                         if (!S_ISDIR(inode->i_mode))
256                                 return acl ? -EACCES : 0;
257                         break;
258
259                 default:
260                         return -EINVAL;
261         }
262         if (acl) {
263                 if (acl->a_count > EXT3_ACL_MAX_ENTRIES)
264                         return -EINVAL;
265                 value = ext3_acl_to_disk(acl, &size);
266                 if (IS_ERR(value))
267                         return (int)PTR_ERR(value);
268         }
269
270         error = ext3_xattr_set_handle(handle, inode, name_index, "",
271                                       value, size, 0);
272
273         if (value)
274                 kfree(value);
275         if (!error) {
276                 switch(type) {
277                         case ACL_TYPE_ACCESS:
278                                 ext3_iset_acl(inode, &ei->i_acl, acl);
279                                 break;
280
281                         case ACL_TYPE_DEFAULT:
282                                 ext3_iset_acl(inode, &ei->i_default_acl, acl);
283                                 break;
284                 }
285         }
286         return error;
287 }
288
289 /*
290  * Inode operation permission().
291  *
292  * inode->i_sem: don't care
293  */
294 int
295 ext3_permission(struct inode *inode, int mask, struct nameidata *nd)
296 {
297         int mode = inode->i_mode;
298
299         /* Nobody gets write access to a read-only fs */
300         if ((mask & MAY_WRITE) && (IS_RDONLY(inode) ||
301             (nd && nd->mnt && MNT_IS_RDONLY(nd->mnt))) &&
302             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
303                 return -EROFS;
304         /* Nobody gets write access to an immutable file */
305         if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
306             return -EACCES;
307         if (current->fsuid == inode->i_uid) {
308                 mode >>= 6;
309         } else if (test_opt(inode->i_sb, POSIX_ACL)) {
310                 struct posix_acl *acl;
311
312                 /* The access ACL cannot grant access if the group class
313                    permission bits don't contain all requested permissions. */
314                 if (((mode >> 3) & mask & S_IRWXO) != mask)
315                         goto check_groups;
316                 acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
317                 if (acl) {
318                         int error = posix_acl_permission(inode, acl, mask);
319                         posix_acl_release(acl);
320                         if (error == -EACCES)
321                                 goto check_capabilities;
322                         return error;
323                 } else
324                         goto check_groups;
325         } else {
326 check_groups:
327                 if (in_group_p(inode->i_gid))
328                         mode >>= 3;
329         }
330         if ((mode & mask & S_IRWXO) == mask)
331                 return 0;
332
333 check_capabilities:
334         /* Allowed to override Discretionary Access Control? */
335         if (!(mask & MAY_EXEC) ||
336             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
337                 if (capable(CAP_DAC_OVERRIDE))
338                         return 0;
339         /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
340         if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
341             (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
342                 return 0;
343         return -EACCES;
344 }
345
346 /*
347  * Initialize the ACLs of a new inode. Called from ext3_new_inode.
348  *
349  * dir->i_sem: down
350  * inode->i_sem: up (access to inode is still exclusive)
351  */
352 int
353 ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
354 {
355         struct posix_acl *acl = NULL;
356         int error = 0;
357
358         if (!S_ISLNK(inode->i_mode)) {
359                 if (test_opt(dir->i_sb, POSIX_ACL)) {
360                         acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
361                         if (IS_ERR(acl))
362                                 return PTR_ERR(acl);
363                 }
364                 if (!acl)
365                         inode->i_mode &= ~current->fs->umask;
366         }
367         if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
368                 struct posix_acl *clone;
369                 mode_t mode;
370
371                 if (S_ISDIR(inode->i_mode)) {
372                         error = ext3_set_acl(handle, inode,
373                                              ACL_TYPE_DEFAULT, acl);
374                         if (error)
375                                 goto cleanup;
376                 }
377                 clone = posix_acl_clone(acl, GFP_KERNEL);
378                 error = -ENOMEM;
379                 if (!clone)
380                         goto cleanup;
381
382                 mode = inode->i_mode;
383                 error = posix_acl_create_masq(clone, &mode);
384                 if (error >= 0) {
385                         inode->i_mode = mode;
386                         if (error > 0) {
387                                 /* This is an extended ACL */
388                                 error = ext3_set_acl(handle, inode,
389                                                      ACL_TYPE_ACCESS, clone);
390                         }
391                 }
392                 posix_acl_release(clone);
393         }
394 cleanup:
395         posix_acl_release(acl);
396         return error;
397 }
398
399 /*
400  * Does chmod for an inode that may have an Access Control List. The
401  * inode->i_mode field must be updated to the desired value by the caller
402  * before calling this function.
403  * Returns 0 on success, or a negative error number.
404  *
405  * We change the ACL rather than storing some ACL entries in the file
406  * mode permission bits (which would be more efficient), because that
407  * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
408  * for directories) are added. There are no more bits available in the
409  * file mode.
410  *
411  * inode->i_sem: down
412  */
413 int
414 ext3_acl_chmod(struct inode *inode)
415 {
416         struct posix_acl *acl, *clone;
417         int error;
418
419         if (S_ISLNK(inode->i_mode))
420                 return -EOPNOTSUPP;
421         if (!test_opt(inode->i_sb, POSIX_ACL))
422                 return 0;
423         acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
424         if (IS_ERR(acl) || !acl)
425                 return PTR_ERR(acl);
426         clone = posix_acl_clone(acl, GFP_KERNEL);
427         posix_acl_release(acl);
428         if (!clone)
429                 return -ENOMEM;
430         error = posix_acl_chmod_masq(clone, inode->i_mode);
431         if (!error) {
432                 handle_t *handle;
433                 int retries = 0;
434
435         retry:
436                 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS);
437                 if (IS_ERR(handle)) {
438                         error = PTR_ERR(handle);
439                         ext3_std_error(inode->i_sb, error);
440                         goto out;
441                 }
442                 error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
443                 ext3_journal_stop(handle);
444                 if (error == -ENOSPC &&
445                     ext3_should_retry_alloc(inode->i_sb, &retries))
446                         goto retry;
447         }
448 out:
449         posix_acl_release(clone);
450         return error;
451 }
452
453 /*
454  * Extended attribute handlers
455  */
456 static size_t
457 ext3_xattr_list_acl_access(char *list, struct inode *inode,
458                            const char *name, int name_len)
459 {
460         const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
461
462         if (!test_opt(inode->i_sb, POSIX_ACL))
463                 return 0;
464         if (list)
465                 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
466         return size;
467 }
468
469 static size_t
470 ext3_xattr_list_acl_default(char *list, struct inode *inode,
471                             const char *name, int name_len)
472 {
473         const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
474
475         if (!test_opt(inode->i_sb, POSIX_ACL))
476                 return 0;
477         if (list)
478                 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
479         return size;
480 }
481
482 static int
483 ext3_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
484 {
485         struct posix_acl *acl;
486         int error;
487
488         if (!test_opt(inode->i_sb, POSIX_ACL))
489                 return -EOPNOTSUPP;
490
491         acl = ext3_get_acl(inode, type);
492         if (IS_ERR(acl))
493                 return PTR_ERR(acl);
494         if (acl == NULL)
495                 return -ENODATA;
496         error = posix_acl_to_xattr(acl, buffer, size);
497         posix_acl_release(acl);
498
499         return error;
500 }
501
502 static int
503 ext3_xattr_get_acl_access(struct inode *inode, const char *name,
504                           void *buffer, size_t size)
505 {
506         if (strcmp(name, "") != 0)
507                 return -EINVAL;
508         return ext3_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
509 }
510
511 static int
512 ext3_xattr_get_acl_default(struct inode *inode, const char *name,
513                            void *buffer, size_t size)
514 {
515         if (strcmp(name, "") != 0)
516                 return -EINVAL;
517         return ext3_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
518 }
519
520 static int
521 ext3_xattr_set_acl(struct inode *inode, int type, const void *value,
522                    size_t size)
523 {
524         handle_t *handle;
525         struct posix_acl *acl;
526         int error, retries = 0;
527
528         if (!test_opt(inode->i_sb, POSIX_ACL))
529                 return -EOPNOTSUPP;
530         if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
531                 return -EPERM;
532
533         if (value) {
534                 acl = posix_acl_from_xattr(value, size);
535                 if (IS_ERR(acl))
536                         return PTR_ERR(acl);
537                 else if (acl) {
538                         error = posix_acl_valid(acl);
539                         if (error)
540                                 goto release_and_out;
541                 }
542         } else
543                 acl = NULL;
544
545 retry:
546         handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS);
547         if (IS_ERR(handle))
548                 return PTR_ERR(handle);
549         error = ext3_set_acl(handle, inode, type, acl);
550         ext3_journal_stop(handle);
551         if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
552                 goto retry;
553
554 release_and_out:
555         posix_acl_release(acl);
556         return error;
557 }
558
559 static int
560 ext3_xattr_set_acl_access(struct inode *inode, const char *name,
561                           const void *value, size_t size, int flags)
562 {
563         if (strcmp(name, "") != 0)
564                 return -EINVAL;
565         return ext3_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
566 }
567
568 static int
569 ext3_xattr_set_acl_default(struct inode *inode, const char *name,
570                            const void *value, size_t size, int flags)
571 {
572         if (strcmp(name, "") != 0)
573                 return -EINVAL;
574         return ext3_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
575 }
576
577 struct ext3_xattr_handler ext3_xattr_acl_access_handler = {
578         .prefix = XATTR_NAME_ACL_ACCESS,
579         .list   = ext3_xattr_list_acl_access,
580         .get    = ext3_xattr_get_acl_access,
581         .set    = ext3_xattr_set_acl_access,
582 };
583
584 struct ext3_xattr_handler ext3_xattr_acl_default_handler = {
585         .prefix = XATTR_NAME_ACL_DEFAULT,
586         .list   = ext3_xattr_list_acl_default,
587         .get    = ext3_xattr_get_acl_default,
588         .set    = ext3_xattr_set_acl_default,
589 };
590
591 void
592 exit_ext3_acl(void)
593 {
594         ext3_xattr_unregister(EXT3_XATTR_INDEX_POSIX_ACL_ACCESS,
595                               &ext3_xattr_acl_access_handler);
596         ext3_xattr_unregister(EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT,
597                               &ext3_xattr_acl_default_handler);
598 }
599
600 int __init
601 init_ext3_acl(void)
602 {
603         int error;
604
605         error = ext3_xattr_register(EXT3_XATTR_INDEX_POSIX_ACL_ACCESS,
606                                     &ext3_xattr_acl_access_handler);
607         if (error)
608                 goto fail;
609         error = ext3_xattr_register(EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT,
610                                     &ext3_xattr_acl_default_handler);
611         if (error)
612                 goto fail;
613         return 0;
614
615 fail:
616         exit_ext3_acl();
617         return error;
618 }