pandora: defconfig: update
[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 /*
129  * inode->i_mutex: don't care
130  */
131 struct posix_acl *
132 ext2_get_acl(struct inode *inode, int type)
133 {
134         int name_index;
135         char *value = NULL;
136         struct posix_acl *acl;
137         int retval;
138
139         if (!test_opt(inode->i_sb, POSIX_ACL))
140                 return NULL;
141
142         acl = get_cached_acl(inode, type);
143         if (acl != ACL_NOT_CACHED)
144                 return acl;
145
146         switch (type) {
147         case ACL_TYPE_ACCESS:
148                 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
149                 break;
150         case ACL_TYPE_DEFAULT:
151                 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
152                 break;
153         default:
154                 BUG();
155         }
156         retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
157         if (retval > 0) {
158                 value = kmalloc(retval, GFP_KERNEL);
159                 if (!value)
160                         return ERR_PTR(-ENOMEM);
161                 retval = ext2_xattr_get(inode, name_index, "", value, retval);
162         }
163         if (retval > 0)
164                 acl = ext2_acl_from_disk(value, retval);
165         else if (retval == -ENODATA || retval == -ENOSYS)
166                 acl = NULL;
167         else
168                 acl = ERR_PTR(retval);
169         kfree(value);
170
171         if (!IS_ERR(acl))
172                 set_cached_acl(inode, type, acl);
173
174         return acl;
175 }
176
177 static int
178 __ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
179 {
180         int name_index;
181         void *value = NULL;
182         size_t size = 0;
183         int error;
184
185         if (S_ISLNK(inode->i_mode))
186                 return -EOPNOTSUPP;
187         if (!test_opt(inode->i_sb, POSIX_ACL))
188                 return 0;
189
190         switch(type) {
191                 case ACL_TYPE_ACCESS:
192                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
193                         break;
194
195                 case ACL_TYPE_DEFAULT:
196                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
197                         if (!S_ISDIR(inode->i_mode))
198                                 return acl ? -EACCES : 0;
199                         break;
200
201                 default:
202                         return -EINVAL;
203         }
204         if (acl) {
205                 value = ext2_acl_to_disk(acl, &size);
206                 if (IS_ERR(value))
207                         return (int)PTR_ERR(value);
208         }
209
210         error = ext2_xattr_set(inode, name_index, "", value, size, 0);
211
212         kfree(value);
213         if (!error)
214                 set_cached_acl(inode, type, acl);
215         return error;
216 }
217
218 /*
219  * inode->i_mutex: down
220  */
221 static int
222 ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
223 {
224         int error;
225         int update_mode = 0;
226         umode_t mode = inode->i_mode;
227
228         if (type == ACL_TYPE_ACCESS && acl) {
229                 error = posix_acl_update_mode(inode, &mode, &acl);
230                 if (error)
231                         return error;
232                 update_mode = 1;
233         }
234         error = __ext2_set_acl(inode, acl, type);
235         if (!error && update_mode) {
236                 inode->i_mode = mode;
237                 inode->i_ctime = CURRENT_TIME_SEC;
238                 mark_inode_dirty(inode);
239         }
240         return error;
241 }
242
243 /*
244  * Initialize the ACLs of a new inode. Called from ext2_new_inode.
245  *
246  * dir->i_mutex: down
247  * inode->i_mutex: up (access to inode is still exclusive)
248  */
249 int
250 ext2_init_acl(struct inode *inode, struct inode *dir)
251 {
252         struct posix_acl *acl = NULL;
253         int error = 0;
254
255         if (!S_ISLNK(inode->i_mode)) {
256                 if (test_opt(dir->i_sb, POSIX_ACL)) {
257                         acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
258                         if (IS_ERR(acl))
259                                 return PTR_ERR(acl);
260                 }
261                 if (!acl)
262                         inode->i_mode &= ~current_umask();
263         }
264         if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
265                 if (S_ISDIR(inode->i_mode)) {
266                         error = __ext2_set_acl(inode, acl, ACL_TYPE_DEFAULT);
267                         if (error)
268                                 goto cleanup;
269                 }
270                 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
271                 if (error < 0)
272                         return error;
273                 if (error > 0) {
274                         /* This is an extended ACL */
275                         error = __ext2_set_acl(inode, acl, ACL_TYPE_ACCESS);
276                 }
277         }
278 cleanup:
279        posix_acl_release(acl);
280        return error;
281 }
282
283 /*
284  * Does chmod for an inode that may have an Access Control List. The
285  * inode->i_mode field must be updated to the desired value by the caller
286  * before calling this function.
287  * Returns 0 on success, or a negative error number.
288  *
289  * We change the ACL rather than storing some ACL entries in the file
290  * mode permission bits (which would be more efficient), because that
291  * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
292  * for directories) are added. There are no more bits available in the
293  * file mode.
294  *
295  * inode->i_mutex: down
296  */
297 int
298 ext2_acl_chmod(struct inode *inode)
299 {
300         struct posix_acl *acl;
301         int error;
302
303         if (!test_opt(inode->i_sb, POSIX_ACL))
304                 return 0;
305         if (S_ISLNK(inode->i_mode))
306                 return -EOPNOTSUPP;
307         acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
308         if (IS_ERR(acl) || !acl)
309                 return PTR_ERR(acl);
310         error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
311         if (error)
312                 return error;
313         error = ext2_set_acl(inode, acl, ACL_TYPE_ACCESS);
314         posix_acl_release(acl);
315         return error;
316 }
317
318 /*
319  * Extended attribut handlers
320  */
321 static size_t
322 ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
323                            const char *name, size_t name_len, int type)
324 {
325         const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
326
327         if (!test_opt(dentry->d_sb, POSIX_ACL))
328                 return 0;
329         if (list && size <= list_size)
330                 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
331         return size;
332 }
333
334 static size_t
335 ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
336                             const char *name, size_t name_len, int type)
337 {
338         const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
339
340         if (!test_opt(dentry->d_sb, POSIX_ACL))
341                 return 0;
342         if (list && size <= list_size)
343                 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
344         return size;
345 }
346
347 static int
348 ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
349                    size_t size, int type)
350 {
351         struct posix_acl *acl;
352         int error;
353
354         if (strcmp(name, "") != 0)
355                 return -EINVAL;
356         if (!test_opt(dentry->d_sb, POSIX_ACL))
357                 return -EOPNOTSUPP;
358
359         acl = ext2_get_acl(dentry->d_inode, type);
360         if (IS_ERR(acl))
361                 return PTR_ERR(acl);
362         if (acl == NULL)
363                 return -ENODATA;
364         error = posix_acl_to_xattr(acl, buffer, size);
365         posix_acl_release(acl);
366
367         return error;
368 }
369
370 static int
371 ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
372                    size_t size, int flags, int type)
373 {
374         struct posix_acl *acl;
375         int error;
376
377         if (strcmp(name, "") != 0)
378                 return -EINVAL;
379         if (!test_opt(dentry->d_sb, POSIX_ACL))
380                 return -EOPNOTSUPP;
381         if (!inode_owner_or_capable(dentry->d_inode))
382                 return -EPERM;
383
384         if (value) {
385                 acl = posix_acl_from_xattr(value, size);
386                 if (IS_ERR(acl))
387                         return PTR_ERR(acl);
388                 else if (acl) {
389                         error = posix_acl_valid(acl);
390                         if (error)
391                                 goto release_and_out;
392                 }
393         } else
394                 acl = NULL;
395
396         error = ext2_set_acl(dentry->d_inode, acl, type);
397
398 release_and_out:
399         posix_acl_release(acl);
400         return error;
401 }
402
403 const struct xattr_handler ext2_xattr_acl_access_handler = {
404         .prefix = POSIX_ACL_XATTR_ACCESS,
405         .flags  = ACL_TYPE_ACCESS,
406         .list   = ext2_xattr_list_acl_access,
407         .get    = ext2_xattr_get_acl,
408         .set    = ext2_xattr_set_acl,
409 };
410
411 const struct xattr_handler ext2_xattr_acl_default_handler = {
412         .prefix = POSIX_ACL_XATTR_DEFAULT,
413         .flags  = ACL_TYPE_DEFAULT,
414         .list   = ext2_xattr_list_acl_default,
415         .get    = ext2_xattr_get_acl,
416         .set    = ext2_xattr_set_acl,
417 };