upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.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/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/vs_base.h>
13 #include "ext2.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 ext2_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(ext2_acl_header))
30                  return ERR_PTR(-EINVAL);
31         if (((ext2_acl_header *)value)->a_version !=
32             cpu_to_le32(EXT2_ACL_VERSION))
33                 return ERR_PTR(-EINVAL);
34         value = (char *)value + sizeof(ext2_acl_header);
35         count = ext2_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                 ext2_acl_entry *entry =
45                         (ext2_acl_entry *)value;
46                 if ((char *)value + sizeof(ext2_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(ext2_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(ext2_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 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
87 {
88         ext2_acl_header *ext_acl;
89         char *e;
90         size_t n;
91
92         *size = ext2_acl_size(acl->a_count);
93         ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
94                 acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
95         if (!ext_acl)
96                 return ERR_PTR(-ENOMEM);
97         ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
98         e = (char *)ext_acl + sizeof(ext2_acl_header);
99         for (n=0; n < acl->a_count; n++) {
100                 ext2_acl_entry *entry = (ext2_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(ext2_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(ext2_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 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
131 {
132         struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
133
134         spin_lock(&inode->i_lock);
135         if (*i_acl != EXT2_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 ext2_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 != EXT2_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->i_sem: don't care
155  */
156 static struct posix_acl *
157 ext2_get_acl(struct inode *inode, int type)
158 {
159         struct ext2_inode_info *ei = EXT2_I(inode);
160         int name_index;
161         char *value = NULL;
162         struct posix_acl *acl;
163         int retval;
164
165         if (!test_opt(inode->i_sb, POSIX_ACL))
166                 return NULL;
167
168         switch(type) {
169                 case ACL_TYPE_ACCESS:
170                         acl = ext2_iget_acl(inode, &ei->i_acl);
171                         if (acl != EXT2_ACL_NOT_CACHED)
172                                 return acl;
173                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
174                         break;
175
176                 case ACL_TYPE_DEFAULT:
177                         acl = ext2_iget_acl(inode, &ei->i_default_acl);
178                         if (acl != EXT2_ACL_NOT_CACHED)
179                                 return acl;
180                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
181                         break;
182
183                 default:
184                         return ERR_PTR(-EINVAL);
185         }
186         retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
187         if (retval > 0) {
188                 value = kmalloc(retval, GFP_KERNEL);
189                 if (!value)
190                         return ERR_PTR(-ENOMEM);
191                 retval = ext2_xattr_get(inode, name_index, "", value, retval);
192         }
193         if (retval > 0)
194                 acl = ext2_acl_from_disk(value, retval);
195         else if (retval == -ENODATA || retval == -ENOSYS)
196                 acl = NULL;
197         else
198                 acl = ERR_PTR(retval);
199         if (value)
200                 kfree(value);
201
202         if (!IS_ERR(acl)) {
203                 switch(type) {
204                         case ACL_TYPE_ACCESS:
205                                 ext2_iset_acl(inode, &ei->i_acl, acl);
206                                 break;
207
208                         case ACL_TYPE_DEFAULT:
209                                 ext2_iset_acl(inode, &ei->i_default_acl, acl);
210                                 break;
211                 }
212         }
213         return acl;
214 }
215
216 /*
217  * inode->i_sem: down
218  */
219 static int
220 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
221 {
222         struct ext2_inode_info *ei = EXT2_I(inode);
223         int name_index;
224         void *value = NULL;
225         size_t size;
226         int error;
227
228         if (S_ISLNK(inode->i_mode))
229                 return -EOPNOTSUPP;
230         if (!test_opt(inode->i_sb, POSIX_ACL))
231                 return 0;
232
233         switch(type) {
234                 case ACL_TYPE_ACCESS:
235                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
236                         if (acl) {
237                                 mode_t mode = inode->i_mode;
238                                 error = posix_acl_equiv_mode(acl, &mode);
239                                 if (error < 0)
240                                         return error;
241                                 else {
242                                         inode->i_mode = mode;
243                                         mark_inode_dirty(inode);
244                                         if (error == 0)
245                                                 acl = NULL;
246                                 }
247                         }
248                         break;
249
250                 case ACL_TYPE_DEFAULT:
251                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
252                         if (!S_ISDIR(inode->i_mode))
253                                 return acl ? -EACCES : 0;
254                         break;
255
256                 default:
257                         return -EINVAL;
258         }
259         if (acl) {
260                 if (acl->a_count > EXT2_ACL_MAX_ENTRIES)
261                         return -EINVAL;
262                 value = ext2_acl_to_disk(acl, &size);
263                 if (IS_ERR(value))
264                         return (int)PTR_ERR(value);
265         }
266
267         error = ext2_xattr_set(inode, name_index, "", value, size, 0);
268
269         if (value)
270                 kfree(value);
271         if (!error) {
272                 switch(type) {
273                         case ACL_TYPE_ACCESS:
274                                 ext2_iset_acl(inode, &ei->i_acl, acl);
275                                 break;
276
277                         case ACL_TYPE_DEFAULT:
278                                 ext2_iset_acl(inode, &ei->i_default_acl, acl);
279                                 break;
280                 }
281         }
282         return error;
283 }
284
285 int
286 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
287 {
288         int mode = inode->i_mode;
289
290 #warning MEF Get new BME patch, which I believe pushes these checks higher
291         /* Nobody gets write access to a read-only fs */
292         if ((mask & MAY_WRITE) && (IS_RDONLY(inode) ||
293             (nd && MNT_IS_RDONLY(nd->mnt))) &&
294             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
295                 return -EROFS;
296
297         return generic_permission(inode, mask, 0);
298 }
299
300 /*
301  * Initialize the ACLs of a new inode. Called from ext2_new_inode.
302  *
303  * dir->i_sem: down
304  * inode->i_sem: 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->fs->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_sem: 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(XATTR_NAME_ACL_ACCESS);
397
398         if (!test_opt(inode->i_sb, POSIX_ACL))
399                 return 0;
400         if (list && size <= list_size)
401                 memcpy(list, XATTR_NAME_ACL_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(XATTR_NAME_ACL_DEFAULT);
410
411         if (!test_opt(inode->i_sb, POSIX_ACL))
412                 return 0;
413         if (list && size <= list_size)
414                 memcpy(list, XATTR_NAME_ACL_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 ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
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 = XATTR_NAME_ACL_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 = XATTR_NAME_ACL_DEFAULT,
514         .list   = ext2_xattr_list_acl_default,
515         .get    = ext2_xattr_get_acl_default,
516         .set    = ext2_xattr_set_acl_default,
517 };