4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 #include <linux/capability.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
17 * Convert from filesystem to in-memory representation.
19 static struct posix_acl
*
20 ext2_acl_from_disk(const void *value
, size_t size
)
22 const char *end
= (char *)value
+ size
;
24 struct posix_acl
*acl
;
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
);
36 return ERR_PTR(-EINVAL
);
39 acl
= posix_acl_alloc(count
, GFP_KERNEL
);
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
)
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
) {
54 value
= (char *)value
+
55 sizeof(ext2_acl_entry_short
);
56 acl
->a_entries
[n
].e_id
= ACL_UNDEFINED_ID
;
61 value
= (char *)value
+ sizeof(ext2_acl_entry
);
62 if ((char *)value
> end
)
64 acl
->a_entries
[n
].e_id
=
65 le32_to_cpu(entry
->e_id
);
77 posix_acl_release(acl
);
78 return ERR_PTR(-EINVAL
);
82 * Convert from in-memory to filesystem representation.
85 ext2_acl_to_disk(const struct posix_acl
*acl
, size_t *size
)
87 ext2_acl_header
*ext_acl
;
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
);
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
) {
106 cpu_to_le32(acl
->a_entries
[n
].e_id
);
107 e
+= sizeof(ext2_acl_entry
);
114 e
+= sizeof(ext2_acl_entry_short
);
121 return (char *)ext_acl
;
125 return ERR_PTR(-EINVAL
);
129 * inode->i_mutex: don't care
131 static struct posix_acl
*
132 ext2_get_acl(struct inode
*inode
, int type
)
136 struct posix_acl
*acl
;
139 if (!test_opt(inode
->i_sb
, POSIX_ACL
))
142 acl
= get_cached_acl(inode
, type
);
143 if (acl
!= ACL_NOT_CACHED
)
147 case ACL_TYPE_ACCESS
:
148 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_ACCESS
;
150 case ACL_TYPE_DEFAULT
:
151 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
156 retval
= ext2_xattr_get(inode
, name_index
, "", NULL
, 0);
158 value
= kmalloc(retval
, GFP_KERNEL
);
160 return ERR_PTR(-ENOMEM
);
161 retval
= ext2_xattr_get(inode
, name_index
, "", value
, retval
);
164 acl
= ext2_acl_from_disk(value
, retval
);
165 else if (retval
== -ENODATA
|| retval
== -ENOSYS
)
168 acl
= ERR_PTR(retval
);
172 set_cached_acl(inode
, type
, acl
);
178 * inode->i_mutex: down
181 ext2_set_acl(struct inode
*inode
, int type
, struct posix_acl
*acl
)
188 if (S_ISLNK(inode
->i_mode
))
190 if (!test_opt(inode
->i_sb
, POSIX_ACL
))
194 case ACL_TYPE_ACCESS
:
195 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_ACCESS
;
197 mode_t mode
= inode
->i_mode
;
198 error
= posix_acl_equiv_mode(acl
, &mode
);
202 inode
->i_mode
= mode
;
203 inode
->i_ctime
= CURRENT_TIME_SEC
;
204 mark_inode_dirty(inode
);
211 case ACL_TYPE_DEFAULT
:
212 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
213 if (!S_ISDIR(inode
->i_mode
))
214 return acl
? -EACCES
: 0;
221 value
= ext2_acl_to_disk(acl
, &size
);
223 return (int)PTR_ERR(value
);
226 error
= ext2_xattr_set(inode
, name_index
, "", value
, size
, 0);
230 set_cached_acl(inode
, type
, acl
);
235 ext2_check_acl(struct inode
*inode
, int mask
, unsigned int flags
)
237 struct posix_acl
*acl
;
239 if (flags
& IPERM_FLAG_RCU
) {
240 if (!negative_cached_acl(inode
, ACL_TYPE_ACCESS
))
245 acl
= ext2_get_acl(inode
, ACL_TYPE_ACCESS
);
249 int error
= posix_acl_permission(inode
, acl
, mask
);
250 posix_acl_release(acl
);
258 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
261 * inode->i_mutex: up (access to inode is still exclusive)
264 ext2_init_acl(struct inode
*inode
, struct inode
*dir
)
266 struct posix_acl
*acl
= NULL
;
269 if (!S_ISLNK(inode
->i_mode
)) {
270 if (test_opt(dir
->i_sb
, POSIX_ACL
)) {
271 acl
= ext2_get_acl(dir
, ACL_TYPE_DEFAULT
);
276 inode
->i_mode
&= ~current_umask();
278 if (test_opt(inode
->i_sb
, POSIX_ACL
) && acl
) {
279 struct posix_acl
*clone
;
282 if (S_ISDIR(inode
->i_mode
)) {
283 error
= ext2_set_acl(inode
, ACL_TYPE_DEFAULT
, acl
);
287 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
291 mode
= inode
->i_mode
;
292 error
= posix_acl_create_masq(clone
, &mode
);
294 inode
->i_mode
= mode
;
296 /* This is an extended ACL */
297 error
= ext2_set_acl(inode
,
298 ACL_TYPE_ACCESS
, clone
);
301 posix_acl_release(clone
);
304 posix_acl_release(acl
);
309 * Does chmod for an inode that may have an Access Control List. The
310 * inode->i_mode field must be updated to the desired value by the caller
311 * before calling this function.
312 * Returns 0 on success, or a negative error number.
314 * We change the ACL rather than storing some ACL entries in the file
315 * mode permission bits (which would be more efficient), because that
316 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
317 * for directories) are added. There are no more bits available in the
320 * inode->i_mutex: down
323 ext2_acl_chmod(struct inode
*inode
)
325 struct posix_acl
*acl
, *clone
;
328 if (!test_opt(inode
->i_sb
, POSIX_ACL
))
330 if (S_ISLNK(inode
->i_mode
))
332 acl
= ext2_get_acl(inode
, ACL_TYPE_ACCESS
);
333 if (IS_ERR(acl
) || !acl
)
335 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
336 posix_acl_release(acl
);
339 error
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
341 error
= ext2_set_acl(inode
, ACL_TYPE_ACCESS
, clone
);
342 posix_acl_release(clone
);
347 * Extended attribut handlers
350 ext2_xattr_list_acl_access(struct dentry
*dentry
, char *list
, size_t list_size
,
351 const char *name
, size_t name_len
, int type
)
353 const size_t size
= sizeof(POSIX_ACL_XATTR_ACCESS
);
355 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
357 if (list
&& size
<= list_size
)
358 memcpy(list
, POSIX_ACL_XATTR_ACCESS
, size
);
363 ext2_xattr_list_acl_default(struct dentry
*dentry
, char *list
, size_t list_size
,
364 const char *name
, size_t name_len
, int type
)
366 const size_t size
= sizeof(POSIX_ACL_XATTR_DEFAULT
);
368 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
370 if (list
&& size
<= list_size
)
371 memcpy(list
, POSIX_ACL_XATTR_DEFAULT
, size
);
376 ext2_xattr_get_acl(struct dentry
*dentry
, const char *name
, void *buffer
,
377 size_t size
, int type
)
379 struct posix_acl
*acl
;
382 if (strcmp(name
, "") != 0)
384 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
387 acl
= ext2_get_acl(dentry
->d_inode
, type
);
392 error
= posix_acl_to_xattr(acl
, buffer
, size
);
393 posix_acl_release(acl
);
399 ext2_xattr_set_acl(struct dentry
*dentry
, const char *name
, const void *value
,
400 size_t size
, int flags
, int type
)
402 struct posix_acl
*acl
;
405 if (strcmp(name
, "") != 0)
407 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
409 if (!inode_owner_or_capable(dentry
->d_inode
))
413 acl
= posix_acl_from_xattr(value
, size
);
417 error
= posix_acl_valid(acl
);
419 goto release_and_out
;
424 error
= ext2_set_acl(dentry
->d_inode
, type
, acl
);
427 posix_acl_release(acl
);
431 const struct xattr_handler ext2_xattr_acl_access_handler
= {
432 .prefix
= POSIX_ACL_XATTR_ACCESS
,
433 .flags
= ACL_TYPE_ACCESS
,
434 .list
= ext2_xattr_list_acl_access
,
435 .get
= ext2_xattr_get_acl
,
436 .set
= ext2_xattr_set_acl
,
439 const struct xattr_handler ext2_xattr_acl_default_handler
= {
440 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
441 .flags
= ACL_TYPE_DEFAULT
,
442 .list
= ext2_xattr_list_acl_default
,
443 .get
= ext2_xattr_get_acl
,
444 .set
= ext2_xattr_set_acl
,