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>
14 #include <linux/time.h>
15 #include <linux/crc32.h>
16 #include <linux/jffs2.h>
17 #include <linux/xattr.h>
18 #include <linux/posix_acl_xattr.h>
19 #include <linux/mtd/mtd.h>
22 static size_t jffs2_acl_size(int count
)
25 return sizeof(struct jffs2_acl_header
)
26 + count
* sizeof(struct jffs2_acl_entry_short
);
28 return sizeof(struct jffs2_acl_header
)
29 + 4 * sizeof(struct jffs2_acl_entry_short
)
30 + (count
- 4) * sizeof(struct jffs2_acl_entry
);
34 static int jffs2_acl_count(size_t size
)
38 size
-= sizeof(struct jffs2_acl_header
);
39 s
= size
- 4 * sizeof(struct jffs2_acl_entry_short
);
41 if (size
% sizeof(struct jffs2_acl_entry_short
))
43 return size
/ sizeof(struct jffs2_acl_entry_short
);
45 if (s
% sizeof(struct jffs2_acl_entry
))
47 return s
/ sizeof(struct jffs2_acl_entry
) + 4;
51 static struct posix_acl
*jffs2_acl_from_medium(void *value
, size_t size
)
53 void *end
= value
+ size
;
54 struct jffs2_acl_header
*header
= value
;
55 struct jffs2_acl_entry
*entry
;
56 struct posix_acl
*acl
;
62 if (size
< sizeof(struct jffs2_acl_header
))
63 return ERR_PTR(-EINVAL
);
64 ver
= je32_to_cpu(header
->a_version
);
65 if (ver
!= JFFS2_ACL_VERSION
) {
66 JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver
);
67 return ERR_PTR(-EINVAL
);
70 value
+= sizeof(struct jffs2_acl_header
);
71 count
= jffs2_acl_count(size
);
73 return ERR_PTR(-EINVAL
);
77 acl
= posix_acl_alloc(count
, GFP_KERNEL
);
79 return ERR_PTR(-ENOMEM
);
81 for (i
=0; i
< count
; i
++) {
83 if (value
+ sizeof(struct jffs2_acl_entry_short
) > end
)
85 acl
->a_entries
[i
].e_tag
= je16_to_cpu(entry
->e_tag
);
86 acl
->a_entries
[i
].e_perm
= je16_to_cpu(entry
->e_perm
);
87 switch (acl
->a_entries
[i
].e_tag
) {
92 value
+= sizeof(struct jffs2_acl_entry_short
);
93 acl
->a_entries
[i
].e_id
= ACL_UNDEFINED_ID
;
98 value
+= sizeof(struct jffs2_acl_entry
);
101 acl
->a_entries
[i
].e_id
= je32_to_cpu(entry
->e_id
);
112 posix_acl_release(acl
);
113 return ERR_PTR(-EINVAL
);
116 static void *jffs2_acl_to_medium(const struct posix_acl
*acl
, size_t *size
)
118 struct jffs2_acl_header
*header
;
119 struct jffs2_acl_entry
*entry
;
123 *size
= jffs2_acl_size(acl
->a_count
);
124 header
= kmalloc(sizeof(*header
) + acl
->a_count
* sizeof(*entry
), GFP_KERNEL
);
126 return ERR_PTR(-ENOMEM
);
127 header
->a_version
= cpu_to_je32(JFFS2_ACL_VERSION
);
129 for (i
=0; i
< acl
->a_count
; i
++) {
131 entry
->e_tag
= cpu_to_je16(acl
->a_entries
[i
].e_tag
);
132 entry
->e_perm
= cpu_to_je16(acl
->a_entries
[i
].e_perm
);
133 switch(acl
->a_entries
[i
].e_tag
) {
136 entry
->e_id
= cpu_to_je32(acl
->a_entries
[i
].e_id
);
137 e
+= sizeof(struct jffs2_acl_entry
);
144 e
+= sizeof(struct jffs2_acl_entry_short
);
154 return ERR_PTR(-EINVAL
);
157 static struct posix_acl
*jffs2_iget_acl(struct inode
*inode
, struct posix_acl
**i_acl
)
159 struct posix_acl
*acl
= JFFS2_ACL_NOT_CACHED
;
161 spin_lock(&inode
->i_lock
);
162 if (*i_acl
!= JFFS2_ACL_NOT_CACHED
)
163 acl
= posix_acl_dup(*i_acl
);
164 spin_unlock(&inode
->i_lock
);
168 static void jffs2_iset_acl(struct inode
*inode
, struct posix_acl
**i_acl
, struct posix_acl
*acl
)
170 spin_lock(&inode
->i_lock
);
171 if (*i_acl
!= JFFS2_ACL_NOT_CACHED
)
172 posix_acl_release(*i_acl
);
173 *i_acl
= posix_acl_dup(acl
);
174 spin_unlock(&inode
->i_lock
);
177 static struct posix_acl
*jffs2_get_acl(struct inode
*inode
, int type
)
179 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
180 struct posix_acl
*acl
;
185 case ACL_TYPE_ACCESS
:
186 acl
= jffs2_iget_acl(inode
, &f
->i_acl_access
);
187 if (acl
!= JFFS2_ACL_NOT_CACHED
)
189 xprefix
= JFFS2_XPREFIX_ACL_ACCESS
;
191 case ACL_TYPE_DEFAULT
:
192 acl
= jffs2_iget_acl(inode
, &f
->i_acl_default
);
193 if (acl
!= JFFS2_ACL_NOT_CACHED
)
195 xprefix
= JFFS2_XPREFIX_ACL_DEFAULT
;
198 return ERR_PTR(-EINVAL
);
200 rc
= do_jffs2_getxattr(inode
, xprefix
, "", NULL
, 0);
202 value
= kmalloc(rc
, GFP_KERNEL
);
204 return ERR_PTR(-ENOMEM
);
205 rc
= do_jffs2_getxattr(inode
, xprefix
, "", value
, rc
);
208 acl
= jffs2_acl_from_medium(value
, rc
);
209 } else if (rc
== -ENODATA
|| rc
== -ENOSYS
) {
218 case ACL_TYPE_ACCESS
:
219 jffs2_iset_acl(inode
, &f
->i_acl_access
, acl
);
221 case ACL_TYPE_DEFAULT
:
222 jffs2_iset_acl(inode
, &f
->i_acl_default
, acl
);
229 static int jffs2_set_acl(struct inode
*inode
, int type
, struct posix_acl
*acl
)
231 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
236 if (S_ISLNK(inode
->i_mode
))
240 case ACL_TYPE_ACCESS
:
241 xprefix
= JFFS2_XPREFIX_ACL_ACCESS
;
243 mode_t mode
= inode
->i_mode
;
244 rc
= posix_acl_equiv_mode(acl
, &mode
);
247 if (inode
->i_mode
!= mode
) {
248 inode
->i_mode
= mode
;
249 jffs2_dirty_inode(inode
);
255 case ACL_TYPE_DEFAULT
:
256 xprefix
= JFFS2_XPREFIX_ACL_DEFAULT
;
257 if (!S_ISDIR(inode
->i_mode
))
258 return acl
? -EACCES
: 0;
264 value
= jffs2_acl_to_medium(acl
, &size
);
266 return PTR_ERR(value
);
269 rc
= do_jffs2_setxattr(inode
, xprefix
, "", value
, size
, 0);
270 if (!value
&& rc
== -ENODATA
)
276 case ACL_TYPE_ACCESS
:
277 jffs2_iset_acl(inode
, &f
->i_acl_access
, acl
);
279 case ACL_TYPE_DEFAULT
:
280 jffs2_iset_acl(inode
, &f
->i_acl_default
, acl
);
287 static int jffs2_check_acl(struct inode
*inode
, int mask
)
289 struct posix_acl
*acl
;
292 acl
= jffs2_get_acl(inode
, ACL_TYPE_ACCESS
);
296 rc
= posix_acl_permission(inode
, acl
, mask
);
297 posix_acl_release(acl
);
303 int jffs2_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
305 return generic_permission(inode
, mask
, jffs2_check_acl
);
308 int jffs2_init_acl(struct inode
*inode
, struct inode
*dir
)
310 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
311 struct posix_acl
*acl
= NULL
, *clone
;
315 f
->i_acl_access
= JFFS2_ACL_NOT_CACHED
;
316 f
->i_acl_default
= JFFS2_ACL_NOT_CACHED
;
317 if (!S_ISLNK(inode
->i_mode
)) {
318 acl
= jffs2_get_acl(dir
, ACL_TYPE_DEFAULT
);
322 inode
->i_mode
&= ~current
->fs
->umask
;
325 if (S_ISDIR(inode
->i_mode
)) {
326 rc
= jffs2_set_acl(inode
, ACL_TYPE_DEFAULT
, acl
);
330 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
334 mode
= inode
->i_mode
;
335 rc
= posix_acl_create_masq(clone
, &mode
);
337 inode
->i_mode
= mode
;
339 rc
= jffs2_set_acl(inode
, ACL_TYPE_ACCESS
, clone
);
341 posix_acl_release(clone
);
344 posix_acl_release(acl
);
348 void jffs2_clear_acl(struct inode
*inode
)
350 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(inode
);
352 if (f
->i_acl_access
&& f
->i_acl_access
!= JFFS2_ACL_NOT_CACHED
) {
353 posix_acl_release(f
->i_acl_access
);
354 f
->i_acl_access
= JFFS2_ACL_NOT_CACHED
;
356 if (f
->i_acl_default
&& f
->i_acl_default
!= JFFS2_ACL_NOT_CACHED
) {
357 posix_acl_release(f
->i_acl_default
);
358 f
->i_acl_default
= JFFS2_ACL_NOT_CACHED
;
362 int jffs2_acl_chmod(struct inode
*inode
)
364 struct posix_acl
*acl
, *clone
;
367 if (S_ISLNK(inode
->i_mode
))
369 acl
= jffs2_get_acl(inode
, ACL_TYPE_ACCESS
);
370 if (IS_ERR(acl
) || !acl
)
372 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
373 posix_acl_release(acl
);
376 rc
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
378 rc
= jffs2_set_acl(inode
, ACL_TYPE_ACCESS
, clone
);
379 posix_acl_release(clone
);
383 static size_t jffs2_acl_access_listxattr(struct inode
*inode
, char *list
, size_t list_size
,
384 const char *name
, size_t name_len
)
386 const int retlen
= sizeof(POSIX_ACL_XATTR_ACCESS
);
388 if (list
&& retlen
<= list_size
)
389 strcpy(list
, POSIX_ACL_XATTR_ACCESS
);
393 static size_t jffs2_acl_default_listxattr(struct inode
*inode
, char *list
, size_t list_size
,
394 const char *name
, size_t name_len
)
396 const int retlen
= sizeof(POSIX_ACL_XATTR_DEFAULT
);
398 if (list
&& retlen
<= list_size
)
399 strcpy(list
, POSIX_ACL_XATTR_DEFAULT
);
403 static int jffs2_acl_getxattr(struct inode
*inode
, int type
, void *buffer
, size_t size
)
405 struct posix_acl
*acl
;
408 acl
= jffs2_get_acl(inode
, type
);
413 rc
= posix_acl_to_xattr(acl
, buffer
, size
);
414 posix_acl_release(acl
);
419 static int jffs2_acl_access_getxattr(struct inode
*inode
, const char *name
, void *buffer
, size_t size
)
423 return jffs2_acl_getxattr(inode
, ACL_TYPE_ACCESS
, buffer
, size
);
426 static int jffs2_acl_default_getxattr(struct inode
*inode
, const char *name
, void *buffer
, size_t size
)
430 return jffs2_acl_getxattr(inode
, ACL_TYPE_DEFAULT
, buffer
, size
);
433 static int jffs2_acl_setxattr(struct inode
*inode
, int type
, const void *value
, size_t size
)
435 struct posix_acl
*acl
;
438 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
442 acl
= posix_acl_from_xattr(value
, size
);
446 rc
= posix_acl_valid(acl
);
453 rc
= jffs2_set_acl(inode
, type
, acl
);
455 posix_acl_release(acl
);
459 static int jffs2_acl_access_setxattr(struct inode
*inode
, const char *name
,
460 const void *buffer
, size_t size
, int flags
)
464 return jffs2_acl_setxattr(inode
, ACL_TYPE_ACCESS
, buffer
, size
);
467 static int jffs2_acl_default_setxattr(struct inode
*inode
, const char *name
,
468 const void *buffer
, size_t size
, int flags
)
472 return jffs2_acl_setxattr(inode
, ACL_TYPE_DEFAULT
, buffer
, size
);
475 struct xattr_handler jffs2_acl_access_xattr_handler
= {
476 .prefix
= POSIX_ACL_XATTR_ACCESS
,
477 .list
= jffs2_acl_access_listxattr
,
478 .get
= jffs2_acl_access_getxattr
,
479 .set
= jffs2_acl_access_setxattr
,
482 struct xattr_handler jffs2_acl_default_xattr_handler
= {
483 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
484 .list
= jffs2_acl_default_listxattr
,
485 .get
= jffs2_acl_default_getxattr
,
486 .set
= jffs2_acl_default_setxattr
,