4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 #include <linux/quotaops.h>
14 * Convert from filesystem to in-memory representation.
16 static struct posix_acl
*
17 ext4_acl_from_disk(const void *value
, size_t size
)
19 const char *end
= (char *)value
+ size
;
21 struct posix_acl
*acl
;
25 if (size
< sizeof(ext4_acl_header
))
26 return ERR_PTR(-EINVAL
);
27 if (((ext4_acl_header
*)value
)->a_version
!=
28 cpu_to_le32(EXT4_ACL_VERSION
))
29 return ERR_PTR(-EINVAL
);
30 value
= (char *)value
+ sizeof(ext4_acl_header
);
31 count
= ext4_acl_count(size
);
33 return ERR_PTR(-EINVAL
);
36 acl
= posix_acl_alloc(count
, GFP_NOFS
);
38 return ERR_PTR(-ENOMEM
);
39 for (n
= 0; n
< count
; n
++) {
40 ext4_acl_entry
*entry
=
41 (ext4_acl_entry
*)value
;
42 if ((char *)value
+ sizeof(ext4_acl_entry_short
) > end
)
44 acl
->a_entries
[n
].e_tag
= le16_to_cpu(entry
->e_tag
);
45 acl
->a_entries
[n
].e_perm
= le16_to_cpu(entry
->e_perm
);
47 switch (acl
->a_entries
[n
].e_tag
) {
52 value
= (char *)value
+
53 sizeof(ext4_acl_entry_short
);
57 value
= (char *)value
+ sizeof(ext4_acl_entry
);
58 if ((char *)value
> end
)
60 acl
->a_entries
[n
].e_uid
=
61 make_kuid(&init_user_ns
,
62 le32_to_cpu(entry
->e_id
));
65 value
= (char *)value
+ sizeof(ext4_acl_entry
);
66 if ((char *)value
> end
)
68 acl
->a_entries
[n
].e_gid
=
69 make_kgid(&init_user_ns
,
70 le32_to_cpu(entry
->e_id
));
82 posix_acl_release(acl
);
83 return ERR_PTR(-EINVAL
);
87 * Convert from in-memory to filesystem representation.
90 ext4_acl_to_disk(const struct posix_acl
*acl
, size_t *size
)
92 ext4_acl_header
*ext_acl
;
96 *size
= ext4_acl_size(acl
->a_count
);
97 ext_acl
= kmalloc(sizeof(ext4_acl_header
) + acl
->a_count
*
98 sizeof(ext4_acl_entry
), GFP_NOFS
);
100 return ERR_PTR(-ENOMEM
);
101 ext_acl
->a_version
= cpu_to_le32(EXT4_ACL_VERSION
);
102 e
= (char *)ext_acl
+ sizeof(ext4_acl_header
);
103 for (n
= 0; n
< acl
->a_count
; n
++) {
104 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
105 ext4_acl_entry
*entry
= (ext4_acl_entry
*)e
;
106 entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
107 entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
108 switch (acl_e
->e_tag
) {
110 entry
->e_id
= cpu_to_le32(
111 from_kuid(&init_user_ns
, acl_e
->e_uid
));
112 e
+= sizeof(ext4_acl_entry
);
115 entry
->e_id
= cpu_to_le32(
116 from_kgid(&init_user_ns
, acl_e
->e_gid
));
117 e
+= sizeof(ext4_acl_entry
);
124 e
+= sizeof(ext4_acl_entry_short
);
131 return (char *)ext_acl
;
135 return ERR_PTR(-EINVAL
);
139 * Inode operation get_posix_acl().
141 * inode->i_mutex: don't care
144 ext4_get_acl(struct inode
*inode
, int type
)
148 struct posix_acl
*acl
;
152 case ACL_TYPE_ACCESS
:
153 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
;
155 case ACL_TYPE_DEFAULT
:
156 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
;
161 retval
= ext4_xattr_get(inode
, name_index
, "", NULL
, 0);
163 value
= kmalloc(retval
, GFP_NOFS
);
165 return ERR_PTR(-ENOMEM
);
166 retval
= ext4_xattr_get(inode
, name_index
, "", value
, retval
);
169 acl
= ext4_acl_from_disk(value
, retval
);
170 else if (retval
== -ENODATA
|| retval
== -ENOSYS
)
173 acl
= ERR_PTR(retval
);
180 * Set the access or default ACL of an inode.
182 * inode->i_mutex: down unless called from ext4_new_inode
185 __ext4_set_acl(handle_t
*handle
, struct inode
*inode
, int type
,
186 struct posix_acl
*acl
, int xattr_flags
)
194 case ACL_TYPE_ACCESS
:
195 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
;
198 case ACL_TYPE_DEFAULT
:
199 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
;
200 if (!S_ISDIR(inode
->i_mode
))
201 return acl
? -EACCES
: 0;
208 value
= ext4_acl_to_disk(acl
, &size
);
210 return (int)PTR_ERR(value
);
213 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, "",
214 value
, size
, xattr_flags
);
218 set_cached_acl(inode
, type
, acl
);
225 ext4_set_acl(struct inode
*inode
, struct posix_acl
*acl
, int type
)
228 int error
, credits
, retries
= 0;
229 size_t acl_size
= acl
? ext4_acl_size(acl
->a_count
) : 0;
230 umode_t mode
= inode
->i_mode
;
233 error
= dquot_initialize(inode
);
237 error
= ext4_xattr_set_credits(inode
, acl_size
, false /* is_create */,
242 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
244 return PTR_ERR(handle
);
246 if ((type
== ACL_TYPE_ACCESS
) && acl
) {
247 error
= posix_acl_update_mode(inode
, &mode
, &acl
);
253 error
= __ext4_set_acl(handle
, inode
, type
, acl
, 0 /* xattr_flags */);
254 if (!error
&& update_mode
) {
255 inode
->i_mode
= mode
;
256 inode
->i_ctime
= current_time(inode
);
257 ext4_mark_inode_dirty(handle
, inode
);
260 ext4_journal_stop(handle
);
261 if (error
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
267 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
270 * inode->i_mutex: up (access to inode is still exclusive)
273 ext4_init_acl(handle_t
*handle
, struct inode
*inode
, struct inode
*dir
)
275 struct posix_acl
*default_acl
, *acl
;
278 error
= posix_acl_create(dir
, &inode
->i_mode
, &default_acl
, &acl
);
283 error
= __ext4_set_acl(handle
, inode
, ACL_TYPE_DEFAULT
,
284 default_acl
, XATTR_CREATE
);
285 posix_acl_release(default_acl
);
289 error
= __ext4_set_acl(handle
, inode
, ACL_TYPE_ACCESS
,
291 posix_acl_release(acl
);