switch ext2 to inode->i_acl
[pandora-kernel.git] / fs / ext2 / acl.c
1 /*
2  * linux/fs/ext2/acl.c
3  *
4  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5  */
6
7 #include <linux/capability.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/fs.h>
12 #include "ext2.h"
13 #include "xattr.h"
14 #include "acl.h"
15
16 /*
17  * Convert from filesystem to in-memory representation.
18  */
19 static struct posix_acl *
20 ext2_acl_from_disk(const void *value, size_t size)
21 {
22         const char *end = (char *)value + size;
23         int n, count;
24         struct posix_acl *acl;
25
26         if (!value)
27                 return NULL;
28         if (size < sizeof(ext2_acl_header))
29                  return ERR_PTR(-EINVAL);
30         if (((ext2_acl_header *)value)->a_version !=
31             cpu_to_le32(EXT2_ACL_VERSION))
32                 return ERR_PTR(-EINVAL);
33         value = (char *)value + sizeof(ext2_acl_header);
34         count = ext2_acl_count(size);
35         if (count < 0)
36                 return ERR_PTR(-EINVAL);
37         if (count == 0)
38                 return NULL;
39         acl = posix_acl_alloc(count, GFP_KERNEL);
40         if (!acl)
41                 return ERR_PTR(-ENOMEM);
42         for (n=0; n < count; n++) {
43                 ext2_acl_entry *entry =
44                         (ext2_acl_entry *)value;
45                 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
46                         goto fail;
47                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
48                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
49                 switch(acl->a_entries[n].e_tag) {
50                         case ACL_USER_OBJ:
51                         case ACL_GROUP_OBJ:
52                         case ACL_MASK:
53                         case ACL_OTHER:
54                                 value = (char *)value +
55                                         sizeof(ext2_acl_entry_short);
56                                 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
57                                 break;
58
59                         case ACL_USER:
60                         case ACL_GROUP:
61                                 value = (char *)value + sizeof(ext2_acl_entry);
62                                 if ((char *)value > end)
63                                         goto fail;
64                                 acl->a_entries[n].e_id =
65                                         le32_to_cpu(entry->e_id);
66                                 break;
67
68                         default:
69                                 goto fail;
70                 }
71         }
72         if (value != end)
73                 goto fail;
74         return acl;
75
76 fail:
77         posix_acl_release(acl);
78         return ERR_PTR(-EINVAL);
79 }
80
81 /*
82  * Convert from in-memory to filesystem representation.
83  */
84 static void *
85 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
86 {
87         ext2_acl_header *ext_acl;
88         char *e;
89         size_t n;
90
91         *size = ext2_acl_size(acl->a_count);
92         ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
93                         sizeof(ext2_acl_entry), GFP_KERNEL);
94         if (!ext_acl)
95                 return ERR_PTR(-ENOMEM);
96         ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
97         e = (char *)ext_acl + sizeof(ext2_acl_header);
98         for (n=0; n < acl->a_count; n++) {
99                 ext2_acl_entry *entry = (ext2_acl_entry *)e;
100                 entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
101                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
102                 switch(acl->a_entries[n].e_tag) {
103                         case ACL_USER:
104                         case ACL_GROUP:
105                                 entry->e_id =
106                                         cpu_to_le32(acl->a_entries[n].e_id);
107                                 e += sizeof(ext2_acl_entry);
108                                 break;
109
110                         case ACL_USER_OBJ:
111                         case ACL_GROUP_OBJ:
112                         case ACL_MASK:
113                         case ACL_OTHER:
114                                 e += sizeof(ext2_acl_entry_short);
115                                 break;
116
117                         default:
118                                 goto fail;
119                 }
120         }
121         return (char *)ext_acl;
122
123 fail:
124         kfree(ext_acl);
125         return ERR_PTR(-EINVAL);
126 }
127
128 static inline struct posix_acl *
129 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
130 {
131         struct posix_acl *acl = ACL_NOT_CACHED;
132
133         spin_lock(&inode->i_lock);
134         if (*i_acl != ACL_NOT_CACHED)
135                 acl = posix_acl_dup(*i_acl);
136         spin_unlock(&inode->i_lock);
137
138         return acl;
139 }
140
141 static inline void
142 ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
143                    struct posix_acl *acl)
144 {
145         spin_lock(&inode->i_lock);
146         if (*i_acl != ACL_NOT_CACHED)
147                 posix_acl_release(*i_acl);
148         *i_acl = posix_acl_dup(acl);
149         spin_unlock(&inode->i_lock);
150 }
151
152 /*
153  * inode->i_mutex: don't care
154  */
155 static struct posix_acl *
156 ext2_get_acl(struct inode *inode, int type)
157 {
158         int name_index;
159         char *value = NULL;
160         struct posix_acl *acl;
161         int retval;
162
163         if (!test_opt(inode->i_sb, POSIX_ACL))
164                 return NULL;
165
166         switch(type) {
167                 case ACL_TYPE_ACCESS:
168                         acl = ext2_iget_acl(inode, &inode->i_acl);
169                         if (acl != ACL_NOT_CACHED)
170                                 return acl;
171                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
172                         break;
173
174                 case ACL_TYPE_DEFAULT:
175                         acl = ext2_iget_acl(inode, &inode->i_default_acl);
176                         if (acl != ACL_NOT_CACHED)
177                                 return acl;
178                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
179                         break;
180
181                 default:
182                         return ERR_PTR(-EINVAL);
183         }
184         retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
185         if (retval > 0) {
186                 value = kmalloc(retval, GFP_KERNEL);
187                 if (!value)
188                         return ERR_PTR(-ENOMEM);
189                 retval = ext2_xattr_get(inode, name_index, "", value, retval);
190         }
191         if (retval > 0)
192                 acl = ext2_acl_from_disk(value, retval);
193         else if (retval == -ENODATA || retval == -ENOSYS)
194                 acl = NULL;
195         else
196                 acl = ERR_PTR(retval);
197         kfree(value);
198
199         if (!IS_ERR(acl)) {
200                 switch(type) {
201                         case ACL_TYPE_ACCESS:
202                                 ext2_iset_acl(inode, &inode->i_acl, acl);
203                                 break;
204
205                         case ACL_TYPE_DEFAULT:
206                                 ext2_iset_acl(inode, &inode->i_default_acl, acl);
207                                 break;
208                 }
209         }
210         return acl;
211 }
212
213 /*
214  * inode->i_mutex: down
215  */
216 static int
217 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
218 {
219         int name_index;
220         void *value = NULL;
221         size_t size = 0;
222         int error;
223
224         if (S_ISLNK(inode->i_mode))
225                 return -EOPNOTSUPP;
226         if (!test_opt(inode->i_sb, POSIX_ACL))
227                 return 0;
228
229         switch(type) {
230                 case ACL_TYPE_ACCESS:
231                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
232                         if (acl) {
233                                 mode_t mode = inode->i_mode;
234                                 error = posix_acl_equiv_mode(acl, &mode);
235                                 if (error < 0)
236                                         return error;
237                                 else {
238                                         inode->i_mode = mode;
239                                         mark_inode_dirty(inode);
240                                         if (error == 0)
241                                                 acl = NULL;
242                                 }
243                         }
244                         break;
245
246                 case ACL_TYPE_DEFAULT:
247                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
248                         if (!S_ISDIR(inode->i_mode))
249                                 return acl ? -EACCES : 0;
250                         break;
251
252                 default:
253                         return -EINVAL;
254         }
255         if (acl) {
256                 value = ext2_acl_to_disk(acl, &size);
257                 if (IS_ERR(value))
258                         return (int)PTR_ERR(value);
259         }
260
261         error = ext2_xattr_set(inode, name_index, "", value, size, 0);
262
263         kfree(value);
264         if (!error) {
265                 switch(type) {
266                         case ACL_TYPE_ACCESS:
267                                 ext2_iset_acl(inode, &inode->i_acl, acl);
268                                 break;
269
270                         case ACL_TYPE_DEFAULT:
271                                 ext2_iset_acl(inode, &inode->i_default_acl, acl);
272                                 break;
273                 }
274         }
275         return error;
276 }
277
278 static int
279 ext2_check_acl(struct inode *inode, int mask)
280 {
281         struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
282
283         if (IS_ERR(acl))
284                 return PTR_ERR(acl);
285         if (acl) {
286                 int error = posix_acl_permission(inode, acl, mask);
287                 posix_acl_release(acl);
288                 return error;
289         }
290
291         return -EAGAIN;
292 }
293
294 int
295 ext2_permission(struct inode *inode, int mask)
296 {
297         return generic_permission(inode, mask, ext2_check_acl);
298 }
299
300 /*
301  * Initialize the ACLs of a new inode. Called from ext2_new_inode.
302  *
303  * dir->i_mutex: down
304  * inode->i_mutex: up (access to inode is still exclusive)
305  */
306 int
307 ext2_init_acl(struct inode *inode, struct inode *dir)
308 {
309         struct posix_acl *acl = NULL;
310         int error = 0;
311
312         if (!S_ISLNK(inode->i_mode)) {
313                 if (test_opt(dir->i_sb, POSIX_ACL)) {
314                         acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
315                         if (IS_ERR(acl))
316                                 return PTR_ERR(acl);
317                 }
318                 if (!acl)
319                         inode->i_mode &= ~current_umask();
320         }
321         if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
322                struct posix_acl *clone;
323                mode_t mode;
324
325                 if (S_ISDIR(inode->i_mode)) {
326                         error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
327                         if (error)
328                                 goto cleanup;
329                 }
330                 clone = posix_acl_clone(acl, GFP_KERNEL);
331                 error = -ENOMEM;
332                 if (!clone)
333                         goto cleanup;
334                 mode = inode->i_mode;
335                 error = posix_acl_create_masq(clone, &mode);
336                 if (error >= 0) {
337                         inode->i_mode = mode;
338                         if (error > 0) {
339                                 /* This is an extended ACL */
340                                 error = ext2_set_acl(inode,
341                                                      ACL_TYPE_ACCESS, clone);
342                         }
343                 }
344                 posix_acl_release(clone);
345         }
346 cleanup:
347        posix_acl_release(acl);
348        return error;
349 }
350
351 /*
352  * Does chmod for an inode that may have an Access Control List. The
353  * inode->i_mode field must be updated to the desired value by the caller
354  * before calling this function.
355  * Returns 0 on success, or a negative error number.
356  *
357  * We change the ACL rather than storing some ACL entries in the file
358  * mode permission bits (which would be more efficient), because that
359  * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
360  * for directories) are added. There are no more bits available in the
361  * file mode.
362  *
363  * inode->i_mutex: down
364  */
365 int
366 ext2_acl_chmod(struct inode *inode)
367 {
368         struct posix_acl *acl, *clone;
369         int error;
370
371         if (!test_opt(inode->i_sb, POSIX_ACL))
372                 return 0;
373         if (S_ISLNK(inode->i_mode))
374                 return -EOPNOTSUPP;
375         acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
376         if (IS_ERR(acl) || !acl)
377                 return PTR_ERR(acl);
378         clone = posix_acl_clone(acl, GFP_KERNEL);
379         posix_acl_release(acl);
380         if (!clone)
381                 return -ENOMEM;
382         error = posix_acl_chmod_masq(clone, inode->i_mode);
383         if (!error)
384                 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
385         posix_acl_release(clone);
386         return error;
387 }
388
389 /*
390  * Extended attribut handlers
391  */
392 static size_t
393 ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
394                            const char *name, size_t name_len)
395 {
396         const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
397
398         if (!test_opt(inode->i_sb, POSIX_ACL))
399                 return 0;
400         if (list && size <= list_size)
401                 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
402         return size;
403 }
404
405 static size_t
406 ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
407                             const char *name, size_t name_len)
408 {
409         const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
410
411         if (!test_opt(inode->i_sb, POSIX_ACL))
412                 return 0;
413         if (list && size <= list_size)
414                 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
415         return size;
416 }
417
418 static int
419 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
420 {
421         struct posix_acl *acl;
422         int error;
423
424         if (!test_opt(inode->i_sb, POSIX_ACL))
425                 return -EOPNOTSUPP;
426
427         acl = ext2_get_acl(inode, type);
428         if (IS_ERR(acl))
429                 return PTR_ERR(acl);
430         if (acl == NULL)
431                 return -ENODATA;
432         error = posix_acl_to_xattr(acl, buffer, size);
433         posix_acl_release(acl);
434
435         return error;
436 }
437
438 static int
439 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
440                           void *buffer, size_t size)
441 {
442         if (strcmp(name, "") != 0)
443                 return -EINVAL;
444         return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
445 }
446
447 static int
448 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
449                            void *buffer, size_t size)
450 {
451         if (strcmp(name, "") != 0)
452                 return -EINVAL;
453         return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
454 }
455
456 static int
457 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
458                    size_t size)
459 {
460         struct posix_acl *acl;
461         int error;
462
463         if (!test_opt(inode->i_sb, POSIX_ACL))
464                 return -EOPNOTSUPP;
465         if (!is_owner_or_cap(inode))
466                 return -EPERM;
467
468         if (value) {
469                 acl = posix_acl_from_xattr(value, size);
470                 if (IS_ERR(acl))
471                         return PTR_ERR(acl);
472                 else if (acl) {
473                         error = posix_acl_valid(acl);
474                         if (error)
475                                 goto release_and_out;
476                 }
477         } else
478                 acl = NULL;
479
480         error = ext2_set_acl(inode, type, acl);
481
482 release_and_out:
483         posix_acl_release(acl);
484         return error;
485 }
486
487 static int
488 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
489                           const void *value, size_t size, int flags)
490 {
491         if (strcmp(name, "") != 0)
492                 return -EINVAL;
493         return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
494 }
495
496 static int
497 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
498                            const void *value, size_t size, int flags)
499 {
500         if (strcmp(name, "") != 0)
501                 return -EINVAL;
502         return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
503 }
504
505 struct xattr_handler ext2_xattr_acl_access_handler = {
506         .prefix = POSIX_ACL_XATTR_ACCESS,
507         .list   = ext2_xattr_list_acl_access,
508         .get    = ext2_xattr_get_acl_access,
509         .set    = ext2_xattr_set_acl_access,
510 };
511
512 struct xattr_handler ext2_xattr_acl_default_handler = {
513         .prefix = POSIX_ACL_XATTR_DEFAULT,
514         .list   = ext2_xattr_list_acl_default,
515         .get    = ext2_xattr_get_acl_default,
516         .set    = ext2_xattr_set_acl_default,
517 };