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
);
59 value
= (char *)value
+ sizeof(ext2_acl_entry
);
60 if ((char *)value
> end
)
62 acl
->a_entries
[n
].e_uid
=
63 make_kuid(&init_user_ns
,
64 le32_to_cpu(entry
->e_id
));
67 value
= (char *)value
+ sizeof(ext2_acl_entry
);
68 if ((char *)value
> end
)
70 acl
->a_entries
[n
].e_gid
=
71 make_kgid(&init_user_ns
,
72 le32_to_cpu(entry
->e_id
));
84 posix_acl_release(acl
);
85 return ERR_PTR(-EINVAL
);
89 * Convert from in-memory to filesystem representation.
92 ext2_acl_to_disk(const struct posix_acl
*acl
, size_t *size
)
94 ext2_acl_header
*ext_acl
;
98 *size
= ext2_acl_size(acl
->a_count
);
99 ext_acl
= kmalloc(sizeof(ext2_acl_header
) + acl
->a_count
*
100 sizeof(ext2_acl_entry
), GFP_KERNEL
);
102 return ERR_PTR(-ENOMEM
);
103 ext_acl
->a_version
= cpu_to_le32(EXT2_ACL_VERSION
);
104 e
= (char *)ext_acl
+ sizeof(ext2_acl_header
);
105 for (n
=0; n
< acl
->a_count
; n
++) {
106 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
107 ext2_acl_entry
*entry
= (ext2_acl_entry
*)e
;
108 entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
109 entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
110 switch(acl_e
->e_tag
) {
112 entry
->e_id
= cpu_to_le32(
113 from_kuid(&init_user_ns
, acl_e
->e_uid
));
114 e
+= sizeof(ext2_acl_entry
);
117 entry
->e_id
= cpu_to_le32(
118 from_kgid(&init_user_ns
, acl_e
->e_gid
));
119 e
+= sizeof(ext2_acl_entry
);
126 e
+= sizeof(ext2_acl_entry_short
);
133 return (char *)ext_acl
;
137 return ERR_PTR(-EINVAL
);
141 * inode->i_mutex: don't care
144 ext2_get_acl(struct inode
*inode
, int type
)
148 struct posix_acl
*acl
;
151 if (!test_opt(inode
->i_sb
, POSIX_ACL
))
154 acl
= get_cached_acl(inode
, type
);
155 if (acl
!= ACL_NOT_CACHED
)
159 case ACL_TYPE_ACCESS
:
160 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_ACCESS
;
162 case ACL_TYPE_DEFAULT
:
163 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
168 retval
= ext2_xattr_get(inode
, name_index
, "", NULL
, 0);
170 value
= kmalloc(retval
, GFP_KERNEL
);
172 return ERR_PTR(-ENOMEM
);
173 retval
= ext2_xattr_get(inode
, name_index
, "", value
, retval
);
176 acl
= ext2_acl_from_disk(value
, retval
);
177 else if (retval
== -ENODATA
|| retval
== -ENOSYS
)
180 acl
= ERR_PTR(retval
);
184 set_cached_acl(inode
, type
, acl
);
190 * inode->i_mutex: down
193 ext2_set_acl(struct inode
*inode
, int type
, struct posix_acl
*acl
)
200 if (S_ISLNK(inode
->i_mode
))
202 if (!test_opt(inode
->i_sb
, POSIX_ACL
))
206 case ACL_TYPE_ACCESS
:
207 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_ACCESS
;
209 error
= posix_acl_equiv_mode(acl
, &inode
->i_mode
);
213 inode
->i_ctime
= CURRENT_TIME_SEC
;
214 mark_inode_dirty(inode
);
221 case ACL_TYPE_DEFAULT
:
222 name_index
= EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
223 if (!S_ISDIR(inode
->i_mode
))
224 return acl
? -EACCES
: 0;
231 value
= ext2_acl_to_disk(acl
, &size
);
233 return (int)PTR_ERR(value
);
236 error
= ext2_xattr_set(inode
, name_index
, "", value
, size
, 0);
240 set_cached_acl(inode
, type
, acl
);
245 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
248 * inode->i_mutex: up (access to inode is still exclusive)
251 ext2_init_acl(struct inode
*inode
, struct inode
*dir
)
253 struct posix_acl
*acl
= NULL
;
256 if (!S_ISLNK(inode
->i_mode
)) {
257 if (test_opt(dir
->i_sb
, POSIX_ACL
)) {
258 acl
= ext2_get_acl(dir
, ACL_TYPE_DEFAULT
);
263 inode
->i_mode
&= ~current_umask();
265 if (test_opt(inode
->i_sb
, POSIX_ACL
) && acl
) {
266 if (S_ISDIR(inode
->i_mode
)) {
267 error
= ext2_set_acl(inode
, ACL_TYPE_DEFAULT
, acl
);
271 error
= posix_acl_create(&acl
, GFP_KERNEL
, &inode
->i_mode
);
275 /* This is an extended ACL */
276 error
= ext2_set_acl(inode
, ACL_TYPE_ACCESS
, acl
);
280 posix_acl_release(acl
);
285 * Does chmod for an inode that may have an Access Control List. The
286 * inode->i_mode field must be updated to the desired value by the caller
287 * before calling this function.
288 * Returns 0 on success, or a negative error number.
290 * We change the ACL rather than storing some ACL entries in the file
291 * mode permission bits (which would be more efficient), because that
292 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
293 * for directories) are added. There are no more bits available in the
296 * inode->i_mutex: down
299 ext2_acl_chmod(struct inode
*inode
)
301 struct posix_acl
*acl
;
304 if (!test_opt(inode
->i_sb
, POSIX_ACL
))
306 if (S_ISLNK(inode
->i_mode
))
308 acl
= ext2_get_acl(inode
, ACL_TYPE_ACCESS
);
309 if (IS_ERR(acl
) || !acl
)
311 error
= posix_acl_chmod(&acl
, GFP_KERNEL
, inode
->i_mode
);
314 error
= ext2_set_acl(inode
, ACL_TYPE_ACCESS
, acl
);
315 posix_acl_release(acl
);
320 * Extended attribut handlers
323 ext2_xattr_list_acl_access(struct dentry
*dentry
, char *list
, size_t list_size
,
324 const char *name
, size_t name_len
, int type
)
326 const size_t size
= sizeof(POSIX_ACL_XATTR_ACCESS
);
328 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
330 if (list
&& size
<= list_size
)
331 memcpy(list
, POSIX_ACL_XATTR_ACCESS
, size
);
336 ext2_xattr_list_acl_default(struct dentry
*dentry
, char *list
, size_t list_size
,
337 const char *name
, size_t name_len
, int type
)
339 const size_t size
= sizeof(POSIX_ACL_XATTR_DEFAULT
);
341 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
343 if (list
&& size
<= list_size
)
344 memcpy(list
, POSIX_ACL_XATTR_DEFAULT
, size
);
349 ext2_xattr_get_acl(struct dentry
*dentry
, const char *name
, void *buffer
,
350 size_t size
, int type
)
352 struct posix_acl
*acl
;
355 if (strcmp(name
, "") != 0)
357 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
360 acl
= ext2_get_acl(dentry
->d_inode
, type
);
365 error
= posix_acl_to_xattr(&init_user_ns
, acl
, buffer
, size
);
366 posix_acl_release(acl
);
372 ext2_xattr_set_acl(struct dentry
*dentry
, const char *name
, const void *value
,
373 size_t size
, int flags
, int type
)
375 struct posix_acl
*acl
;
378 if (strcmp(name
, "") != 0)
380 if (!test_opt(dentry
->d_sb
, POSIX_ACL
))
382 if (!inode_owner_or_capable(dentry
->d_inode
))
386 acl
= posix_acl_from_xattr(&init_user_ns
, value
, size
);
390 error
= posix_acl_valid(acl
);
392 goto release_and_out
;
397 error
= ext2_set_acl(dentry
->d_inode
, type
, acl
);
400 posix_acl_release(acl
);
404 const struct xattr_handler ext2_xattr_acl_access_handler
= {
405 .prefix
= POSIX_ACL_XATTR_ACCESS
,
406 .flags
= ACL_TYPE_ACCESS
,
407 .list
= ext2_xattr_list_acl_access
,
408 .get
= ext2_xattr_get_acl
,
409 .set
= ext2_xattr_set_acl
,
412 const struct xattr_handler ext2_xattr_acl_default_handler
= {
413 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
414 .flags
= ACL_TYPE_DEFAULT
,
415 .list
= ext2_xattr_list_acl_default
,
416 .get
= ext2_xattr_get_acl
,
417 .set
= ext2_xattr_set_acl
,