This commit was manufactured by cvs2svn to create tag 'before-xenU'.
[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                 value = ext2_acl_to_disk(acl, &size);
261                 if (IS_ERR(value))
262                         return (int)PTR_ERR(value);
263         }
264
265         error = ext2_xattr_set(inode, name_index, "", value, size, 0);
266
267         if (value)
268                 kfree(value);
269         if (!error) {
270                 switch(type) {
271                         case ACL_TYPE_ACCESS:
272                                 ext2_iset_acl(inode, &ei->i_acl, acl);
273                                 break;
274
275                         case ACL_TYPE_DEFAULT:
276                                 ext2_iset_acl(inode, &ei->i_default_acl, acl);
277                                 break;
278                 }
279         }
280         return error;
281 }
282
283 static int
284 ext2_check_acl(struct inode *inode, int mask)
285 {
286         struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
287
288         if (IS_ERR(acl))
289                 return PTR_ERR(acl);
290         if (acl) {
291                 int error = posix_acl_permission(inode, acl, mask);
292                 posix_acl_release(acl);
293                 return error;
294         }
295
296         return -EAGAIN;
297 }
298
299 int
300 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
301 {
302         return generic_permission(inode, mask, ext2_check_acl);
303 }
304
305 /*
306  * Initialize the ACLs of a new inode. Called from ext2_new_inode.
307  *
308  * dir->i_sem: down
309  * inode->i_sem: up (access to inode is still exclusive)
310  */
311 int
312 ext2_init_acl(struct inode *inode, struct inode *dir)
313 {
314         struct posix_acl *acl = NULL;
315         int error = 0;
316
317         if (!S_ISLNK(inode->i_mode)) {
318                 if (test_opt(dir->i_sb, POSIX_ACL)) {
319                         acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
320                         if (IS_ERR(acl))
321                                 return PTR_ERR(acl);
322                 }
323                 if (!acl)
324                         inode->i_mode &= ~current->fs->umask;
325         }
326         if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
327                struct posix_acl *clone;
328                mode_t mode;
329
330                 if (S_ISDIR(inode->i_mode)) {
331                         error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
332                         if (error)
333                                 goto cleanup;
334                 }
335                 clone = posix_acl_clone(acl, GFP_KERNEL);
336                 error = -ENOMEM;
337                 if (!clone)
338                         goto cleanup;
339                 mode = inode->i_mode;
340                 error = posix_acl_create_masq(clone, &mode);
341                 if (error >= 0) {
342                         inode->i_mode = mode;
343                         if (error > 0) {
344                                 /* This is an extended ACL */
345                                 error = ext2_set_acl(inode,
346                                                      ACL_TYPE_ACCESS, clone);
347                         }
348                 }
349                 posix_acl_release(clone);
350         }
351 cleanup:
352        posix_acl_release(acl);
353        return error;
354 }
355
356 /*
357  * Does chmod for an inode that may have an Access Control List. The
358  * inode->i_mode field must be updated to the desired value by the caller
359  * before calling this function.
360  * Returns 0 on success, or a negative error number.
361  *
362  * We change the ACL rather than storing some ACL entries in the file
363  * mode permission bits (which would be more efficient), because that
364  * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
365  * for directories) are added. There are no more bits available in the
366  * file mode.
367  *
368  * inode->i_sem: down
369  */
370 int
371 ext2_acl_chmod(struct inode *inode)
372 {
373         struct posix_acl *acl, *clone;
374         int error;
375
376         if (!test_opt(inode->i_sb, POSIX_ACL))
377                 return 0;
378         if (S_ISLNK(inode->i_mode))
379                 return -EOPNOTSUPP;
380         acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
381         if (IS_ERR(acl) || !acl)
382                 return PTR_ERR(acl);
383         clone = posix_acl_clone(acl, GFP_KERNEL);
384         posix_acl_release(acl);
385         if (!clone)
386                 return -ENOMEM;
387         error = posix_acl_chmod_masq(clone, inode->i_mode);
388         if (!error)
389                 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
390         posix_acl_release(clone);
391         return error;
392 }
393
394 /*
395  * Extended attribut handlers
396  */
397 static size_t
398 ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
399                            const char *name, size_t name_len)
400 {
401         const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
402
403         if (!test_opt(inode->i_sb, POSIX_ACL))
404                 return 0;
405         if (list && size <= list_size)
406                 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
407         return size;
408 }
409
410 static size_t
411 ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
412                             const char *name, size_t name_len)
413 {
414         const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
415
416         if (!test_opt(inode->i_sb, POSIX_ACL))
417                 return 0;
418         if (list && size <= list_size)
419                 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
420         return size;
421 }
422
423 static int
424 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
425 {
426         struct posix_acl *acl;
427         int error;
428
429         if (!test_opt(inode->i_sb, POSIX_ACL))
430                 return -EOPNOTSUPP;
431
432         acl = ext2_get_acl(inode, type);
433         if (IS_ERR(acl))
434                 return PTR_ERR(acl);
435         if (acl == NULL)
436                 return -ENODATA;
437         error = posix_acl_to_xattr(acl, buffer, size);
438         posix_acl_release(acl);
439
440         return error;
441 }
442
443 static int
444 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
445                           void *buffer, size_t size)
446 {
447         if (strcmp(name, "") != 0)
448                 return -EINVAL;
449         return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
450 }
451
452 static int
453 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
454                            void *buffer, size_t size)
455 {
456         if (strcmp(name, "") != 0)
457                 return -EINVAL;
458         return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
459 }
460
461 static int
462 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
463                    size_t size)
464 {
465         struct posix_acl *acl;
466         int error;
467
468         if (!test_opt(inode->i_sb, POSIX_ACL))
469                 return -EOPNOTSUPP;
470         if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
471                 return -EPERM;
472
473         if (value) {
474                 acl = posix_acl_from_xattr(value, size);
475                 if (IS_ERR(acl))
476                         return PTR_ERR(acl);
477                 else if (acl) {
478                         error = posix_acl_valid(acl);
479                         if (error)
480                                 goto release_and_out;
481                 }
482         } else
483                 acl = NULL;
484
485         error = ext2_set_acl(inode, type, acl);
486
487 release_and_out:
488         posix_acl_release(acl);
489         return error;
490 }
491
492 static int
493 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
494                           const void *value, size_t size, int flags)
495 {
496         if (strcmp(name, "") != 0)
497                 return -EINVAL;
498         return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
499 }
500
501 static int
502 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
503                            const void *value, size_t size, int flags)
504 {
505         if (strcmp(name, "") != 0)
506                 return -EINVAL;
507         return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
508 }
509
510 struct xattr_handler ext2_xattr_acl_access_handler = {
511         .prefix = XATTR_NAME_ACL_ACCESS,
512         .list   = ext2_xattr_list_acl_access,
513         .get    = ext2_xattr_get_acl_access,
514         .set    = ext2_xattr_set_acl_access,
515 };
516
517 struct xattr_handler ext2_xattr_acl_default_handler = {
518         .prefix = XATTR_NAME_ACL_DEFAULT,
519         .list   = ext2_xattr_list_acl_default,
520         .get    = ext2_xattr_get_acl_default,
521         .set    = ext2_xattr_set_acl_default,
522 };