[CONNECTOR]: Replace delayed work with usual work queue.
[linux-2.6/verdex.git] / fs / jffs2 / acl.c
blob73f0d60f73a51ff1898d7740e7dd28c68ae856c2
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2006 NEC Corporation
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/fs.h>
14 #include <linux/sched.h>
15 #include <linux/time.h>
16 #include <linux/crc32.h>
17 #include <linux/jffs2.h>
18 #include <linux/xattr.h>
19 #include <linux/posix_acl_xattr.h>
20 #include <linux/mtd/mtd.h>
21 #include "nodelist.h"
23 static size_t jffs2_acl_size(int count)
25 if (count <= 4) {
26 return sizeof(struct jffs2_acl_header)
27 + count * sizeof(struct jffs2_acl_entry_short);
28 } else {
29 return sizeof(struct jffs2_acl_header)
30 + 4 * sizeof(struct jffs2_acl_entry_short)
31 + (count - 4) * sizeof(struct jffs2_acl_entry);
35 static int jffs2_acl_count(size_t size)
37 size_t s;
39 size -= sizeof(struct jffs2_acl_header);
40 s = size - 4 * sizeof(struct jffs2_acl_entry_short);
41 if (s < 0) {
42 if (size % sizeof(struct jffs2_acl_entry_short))
43 return -1;
44 return size / sizeof(struct jffs2_acl_entry_short);
45 } else {
46 if (s % sizeof(struct jffs2_acl_entry))
47 return -1;
48 return s / sizeof(struct jffs2_acl_entry) + 4;
52 static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
54 void *end = value + size;
55 struct jffs2_acl_header *header = value;
56 struct jffs2_acl_entry *entry;
57 struct posix_acl *acl;
58 uint32_t ver;
59 int i, count;
61 if (!value)
62 return NULL;
63 if (size < sizeof(struct jffs2_acl_header))
64 return ERR_PTR(-EINVAL);
65 ver = je32_to_cpu(header->a_version);
66 if (ver != JFFS2_ACL_VERSION) {
67 JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
68 return ERR_PTR(-EINVAL);
71 value += sizeof(struct jffs2_acl_header);
72 count = jffs2_acl_count(size);
73 if (count < 0)
74 return ERR_PTR(-EINVAL);
75 if (count == 0)
76 return NULL;
78 acl = posix_acl_alloc(count, GFP_KERNEL);
79 if (!acl)
80 return ERR_PTR(-ENOMEM);
82 for (i=0; i < count; i++) {
83 entry = value;
84 if (value + sizeof(struct jffs2_acl_entry_short) > end)
85 goto fail;
86 acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
87 acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
88 switch (acl->a_entries[i].e_tag) {
89 case ACL_USER_OBJ:
90 case ACL_GROUP_OBJ:
91 case ACL_MASK:
92 case ACL_OTHER:
93 value += sizeof(struct jffs2_acl_entry_short);
94 acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
95 break;
97 case ACL_USER:
98 case ACL_GROUP:
99 value += sizeof(struct jffs2_acl_entry);
100 if (value > end)
101 goto fail;
102 acl->a_entries[i].e_id = je32_to_cpu(entry->e_id);
103 break;
105 default:
106 goto fail;
109 if (value != end)
110 goto fail;
111 return acl;
112 fail:
113 posix_acl_release(acl);
114 return ERR_PTR(-EINVAL);
117 static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
119 struct jffs2_acl_header *header;
120 struct jffs2_acl_entry *entry;
121 void *e;
122 size_t i;
124 *size = jffs2_acl_size(acl->a_count);
125 header = kmalloc(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
126 if (!header)
127 return ERR_PTR(-ENOMEM);
128 header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
129 e = header + 1;
130 for (i=0; i < acl->a_count; i++) {
131 entry = e;
132 entry->e_tag = cpu_to_je16(acl->a_entries[i].e_tag);
133 entry->e_perm = cpu_to_je16(acl->a_entries[i].e_perm);
134 switch(acl->a_entries[i].e_tag) {
135 case ACL_USER:
136 case ACL_GROUP:
137 entry->e_id = cpu_to_je32(acl->a_entries[i].e_id);
138 e += sizeof(struct jffs2_acl_entry);
139 break;
141 case ACL_USER_OBJ:
142 case ACL_GROUP_OBJ:
143 case ACL_MASK:
144 case ACL_OTHER:
145 e += sizeof(struct jffs2_acl_entry_short);
146 break;
148 default:
149 goto fail;
152 return header;
153 fail:
154 kfree(header);
155 return ERR_PTR(-EINVAL);
158 static struct posix_acl *jffs2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
160 struct posix_acl *acl = JFFS2_ACL_NOT_CACHED;
162 spin_lock(&inode->i_lock);
163 if (*i_acl != JFFS2_ACL_NOT_CACHED)
164 acl = posix_acl_dup(*i_acl);
165 spin_unlock(&inode->i_lock);
166 return acl;
169 static void jffs2_iset_acl(struct inode *inode, struct posix_acl **i_acl, struct posix_acl *acl)
171 spin_lock(&inode->i_lock);
172 if (*i_acl != JFFS2_ACL_NOT_CACHED)
173 posix_acl_release(*i_acl);
174 *i_acl = posix_acl_dup(acl);
175 spin_unlock(&inode->i_lock);
178 static struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
180 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
181 struct posix_acl *acl;
182 char *value = NULL;
183 int rc, xprefix;
185 switch (type) {
186 case ACL_TYPE_ACCESS:
187 acl = jffs2_iget_acl(inode, &f->i_acl_access);
188 if (acl != JFFS2_ACL_NOT_CACHED)
189 return acl;
190 xprefix = JFFS2_XPREFIX_ACL_ACCESS;
191 break;
192 case ACL_TYPE_DEFAULT:
193 acl = jffs2_iget_acl(inode, &f->i_acl_default);
194 if (acl != JFFS2_ACL_NOT_CACHED)
195 return acl;
196 xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
197 break;
198 default:
199 return ERR_PTR(-EINVAL);
201 rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
202 if (rc > 0) {
203 value = kmalloc(rc, GFP_KERNEL);
204 if (!value)
205 return ERR_PTR(-ENOMEM);
206 rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
208 if (rc > 0) {
209 acl = jffs2_acl_from_medium(value, rc);
210 } else if (rc == -ENODATA || rc == -ENOSYS) {
211 acl = NULL;
212 } else {
213 acl = ERR_PTR(rc);
215 if (value)
216 kfree(value);
217 if (!IS_ERR(acl)) {
218 switch (type) {
219 case ACL_TYPE_ACCESS:
220 jffs2_iset_acl(inode, &f->i_acl_access, acl);
221 break;
222 case ACL_TYPE_DEFAULT:
223 jffs2_iset_acl(inode, &f->i_acl_default, acl);
224 break;
227 return acl;
230 static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
232 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
233 size_t size = 0;
234 char *value = NULL;
235 int rc, xprefix;
237 if (S_ISLNK(inode->i_mode))
238 return -EOPNOTSUPP;
240 switch (type) {
241 case ACL_TYPE_ACCESS:
242 xprefix = JFFS2_XPREFIX_ACL_ACCESS;
243 if (acl) {
244 mode_t mode = inode->i_mode;
245 rc = posix_acl_equiv_mode(acl, &mode);
246 if (rc < 0)
247 return rc;
248 if (inode->i_mode != mode) {
249 inode->i_mode = mode;
250 jffs2_dirty_inode(inode);
252 if (rc == 0)
253 acl = NULL;
255 break;
256 case ACL_TYPE_DEFAULT:
257 xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
258 if (!S_ISDIR(inode->i_mode))
259 return acl ? -EACCES : 0;
260 break;
261 default:
262 return -EINVAL;
264 if (acl) {
265 value = jffs2_acl_to_medium(acl, &size);
266 if (IS_ERR(value))
267 return PTR_ERR(value);
270 rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
271 if (!value && rc == -ENODATA)
272 rc = 0;
273 if (value)
274 kfree(value);
275 if (!rc) {
276 switch(type) {
277 case ACL_TYPE_ACCESS:
278 jffs2_iset_acl(inode, &f->i_acl_access, acl);
279 break;
280 case ACL_TYPE_DEFAULT:
281 jffs2_iset_acl(inode, &f->i_acl_default, acl);
282 break;
285 return rc;
288 static int jffs2_check_acl(struct inode *inode, int mask)
290 struct posix_acl *acl;
291 int rc;
293 acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
294 if (IS_ERR(acl))
295 return PTR_ERR(acl);
296 if (acl) {
297 rc = posix_acl_permission(inode, acl, mask);
298 posix_acl_release(acl);
299 return rc;
301 return -EAGAIN;
304 int jffs2_permission(struct inode *inode, int mask, struct nameidata *nd)
306 return generic_permission(inode, mask, jffs2_check_acl);
309 int jffs2_init_acl(struct inode *inode, struct inode *dir)
311 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
312 struct posix_acl *acl = NULL, *clone;
313 mode_t mode;
314 int rc = 0;
316 f->i_acl_access = JFFS2_ACL_NOT_CACHED;
317 f->i_acl_default = JFFS2_ACL_NOT_CACHED;
318 if (!S_ISLNK(inode->i_mode)) {
319 acl = jffs2_get_acl(dir, ACL_TYPE_DEFAULT);
320 if (IS_ERR(acl))
321 return PTR_ERR(acl);
322 if (!acl)
323 inode->i_mode &= ~current->fs->umask;
325 if (acl) {
326 if (S_ISDIR(inode->i_mode)) {
327 rc = jffs2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
328 if (rc)
329 goto cleanup;
331 clone = posix_acl_clone(acl, GFP_KERNEL);
332 rc = -ENOMEM;
333 if (!clone)
334 goto cleanup;
335 mode = inode->i_mode;
336 rc = posix_acl_create_masq(clone, &mode);
337 if (rc >= 0) {
338 inode->i_mode = mode;
339 if (rc > 0)
340 rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
342 posix_acl_release(clone);
344 cleanup:
345 posix_acl_release(acl);
346 return rc;
349 void jffs2_clear_acl(struct jffs2_inode_info *f)
351 if (f->i_acl_access && f->i_acl_access != JFFS2_ACL_NOT_CACHED) {
352 posix_acl_release(f->i_acl_access);
353 f->i_acl_access = JFFS2_ACL_NOT_CACHED;
355 if (f->i_acl_default && f->i_acl_default != JFFS2_ACL_NOT_CACHED) {
356 posix_acl_release(f->i_acl_default);
357 f->i_acl_default = JFFS2_ACL_NOT_CACHED;
361 int jffs2_acl_chmod(struct inode *inode)
363 struct posix_acl *acl, *clone;
364 int rc;
366 if (S_ISLNK(inode->i_mode))
367 return -EOPNOTSUPP;
368 acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
369 if (IS_ERR(acl) || !acl)
370 return PTR_ERR(acl);
371 clone = posix_acl_clone(acl, GFP_KERNEL);
372 posix_acl_release(acl);
373 if (!clone)
374 return -ENOMEM;
375 rc = posix_acl_chmod_masq(clone, inode->i_mode);
376 if (!rc)
377 rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
378 posix_acl_release(clone);
379 return rc;
382 static size_t jffs2_acl_access_listxattr(struct inode *inode, char *list, size_t list_size,
383 const char *name, size_t name_len)
385 const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);
387 if (list && retlen <= list_size)
388 strcpy(list, POSIX_ACL_XATTR_ACCESS);
389 return retlen;
392 static size_t jffs2_acl_default_listxattr(struct inode *inode, char *list, size_t list_size,
393 const char *name, size_t name_len)
395 const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);
397 if (list && retlen <= list_size)
398 strcpy(list, POSIX_ACL_XATTR_DEFAULT);
399 return retlen;
402 static int jffs2_acl_getxattr(struct inode *inode, int type, void *buffer, size_t size)
404 struct posix_acl *acl;
405 int rc;
407 acl = jffs2_get_acl(inode, type);
408 if (IS_ERR(acl))
409 return PTR_ERR(acl);
410 if (!acl)
411 return -ENODATA;
412 rc = posix_acl_to_xattr(acl, buffer, size);
413 posix_acl_release(acl);
415 return rc;
418 static int jffs2_acl_access_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
420 if (name[0] != '\0')
421 return -EINVAL;
422 return jffs2_acl_getxattr(inode, ACL_TYPE_ACCESS, buffer, size);
425 static int jffs2_acl_default_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
427 if (name[0] != '\0')
428 return -EINVAL;
429 return jffs2_acl_getxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
432 static int jffs2_acl_setxattr(struct inode *inode, int type, const void *value, size_t size)
434 struct posix_acl *acl;
435 int rc;
437 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
438 return -EPERM;
440 if (value) {
441 acl = posix_acl_from_xattr(value, size);
442 if (IS_ERR(acl))
443 return PTR_ERR(acl);
444 if (acl) {
445 rc = posix_acl_valid(acl);
446 if (rc)
447 goto out;
449 } else {
450 acl = NULL;
452 rc = jffs2_set_acl(inode, type, acl);
453 out:
454 posix_acl_release(acl);
455 return rc;
458 static int jffs2_acl_access_setxattr(struct inode *inode, const char *name,
459 const void *buffer, size_t size, int flags)
461 if (name[0] != '\0')
462 return -EINVAL;
463 return jffs2_acl_setxattr(inode, ACL_TYPE_ACCESS, buffer, size);
466 static int jffs2_acl_default_setxattr(struct inode *inode, const char *name,
467 const void *buffer, size_t size, int flags)
469 if (name[0] != '\0')
470 return -EINVAL;
471 return jffs2_acl_setxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
474 struct xattr_handler jffs2_acl_access_xattr_handler = {
475 .prefix = POSIX_ACL_XATTR_ACCESS,
476 .list = jffs2_acl_access_listxattr,
477 .get = jffs2_acl_access_getxattr,
478 .set = jffs2_acl_access_setxattr,
481 struct xattr_handler jffs2_acl_default_xattr_handler = {
482 .prefix = POSIX_ACL_XATTR_DEFAULT,
483 .list = jffs2_acl_default_listxattr,
484 .get = jffs2_acl_default_getxattr,
485 .set = jffs2_acl_default_setxattr,