]>
Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/acl.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> | |
5 | */ | |
6 | ||
b8cb5a54 | 7 | #include <linux/quotaops.h> |
3dcf5451 CH |
8 | #include "ext4_jbd2.h" |
9 | #include "ext4.h" | |
ac27a0ec DK |
10 | #include "xattr.h" |
11 | #include "acl.h" | |
12 | ||
13 | /* | |
14 | * Convert from filesystem to in-memory representation. | |
15 | */ | |
16 | static struct posix_acl * | |
617ba13b | 17 | ext4_acl_from_disk(const void *value, size_t size) |
ac27a0ec DK |
18 | { |
19 | const char *end = (char *)value + size; | |
20 | int n, count; | |
21 | struct posix_acl *acl; | |
22 | ||
23 | if (!value) | |
24 | return NULL; | |
617ba13b | 25 | if (size < sizeof(ext4_acl_header)) |
ac27a0ec | 26 | return ERR_PTR(-EINVAL); |
617ba13b MC |
27 | if (((ext4_acl_header *)value)->a_version != |
28 | cpu_to_le32(EXT4_ACL_VERSION)) | |
ac27a0ec | 29 | return ERR_PTR(-EINVAL); |
617ba13b MC |
30 | value = (char *)value + sizeof(ext4_acl_header); |
31 | count = ext4_acl_count(size); | |
ac27a0ec DK |
32 | if (count < 0) |
33 | return ERR_PTR(-EINVAL); | |
34 | if (count == 0) | |
35 | return NULL; | |
216553c4 | 36 | acl = posix_acl_alloc(count, GFP_NOFS); |
ac27a0ec DK |
37 | if (!acl) |
38 | return ERR_PTR(-ENOMEM); | |
2b2d6d01 | 39 | for (n = 0; n < count; n++) { |
617ba13b MC |
40 | ext4_acl_entry *entry = |
41 | (ext4_acl_entry *)value; | |
42 | if ((char *)value + sizeof(ext4_acl_entry_short) > end) | |
ac27a0ec DK |
43 | goto fail; |
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); | |
2b2d6d01 TT |
46 | |
47 | switch (acl->a_entries[n].e_tag) { | |
48 | case ACL_USER_OBJ: | |
49 | case ACL_GROUP_OBJ: | |
50 | case ACL_MASK: | |
51 | case ACL_OTHER: | |
52 | value = (char *)value + | |
53 | sizeof(ext4_acl_entry_short); | |
2b2d6d01 TT |
54 | break; |
55 | ||
56 | case ACL_USER: | |
af84df93 EB |
57 | value = (char *)value + sizeof(ext4_acl_entry); |
58 | if ((char *)value > end) | |
59 | goto fail; | |
60 | acl->a_entries[n].e_uid = | |
61 | make_kuid(&init_user_ns, | |
62 | le32_to_cpu(entry->e_id)); | |
63 | break; | |
2b2d6d01 TT |
64 | case ACL_GROUP: |
65 | value = (char *)value + sizeof(ext4_acl_entry); | |
66 | if ((char *)value > end) | |
ac27a0ec | 67 | goto fail; |
af84df93 EB |
68 | acl->a_entries[n].e_gid = |
69 | make_kgid(&init_user_ns, | |
70 | le32_to_cpu(entry->e_id)); | |
2b2d6d01 TT |
71 | break; |
72 | ||
73 | default: | |
74 | goto fail; | |
ac27a0ec DK |
75 | } |
76 | } | |
77 | if (value != end) | |
78 | goto fail; | |
79 | return acl; | |
80 | ||
81 | fail: | |
82 | posix_acl_release(acl); | |
83 | return ERR_PTR(-EINVAL); | |
84 | } | |
85 | ||
86 | /* | |
87 | * Convert from in-memory to filesystem representation. | |
88 | */ | |
89 | static void * | |
617ba13b | 90 | ext4_acl_to_disk(const struct posix_acl *acl, size_t *size) |
ac27a0ec | 91 | { |
617ba13b | 92 | ext4_acl_header *ext_acl; |
ac27a0ec DK |
93 | char *e; |
94 | size_t n; | |
95 | ||
617ba13b MC |
96 | *size = ext4_acl_size(acl->a_count); |
97 | ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * | |
216553c4 | 98 | sizeof(ext4_acl_entry), GFP_NOFS); |
ac27a0ec DK |
99 | if (!ext_acl) |
100 | return ERR_PTR(-ENOMEM); | |
617ba13b MC |
101 | ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); |
102 | e = (char *)ext_acl + sizeof(ext4_acl_header); | |
2b2d6d01 | 103 | for (n = 0; n < acl->a_count; n++) { |
af84df93 | 104 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
617ba13b | 105 | ext4_acl_entry *entry = (ext4_acl_entry *)e; |
af84df93 EB |
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) { | |
2b2d6d01 | 109 | case ACL_USER: |
af84df93 EB |
110 | entry->e_id = cpu_to_le32( |
111 | from_kuid(&init_user_ns, acl_e->e_uid)); | |
112 | e += sizeof(ext4_acl_entry); | |
113 | break; | |
2b2d6d01 | 114 | case ACL_GROUP: |
af84df93 EB |
115 | entry->e_id = cpu_to_le32( |
116 | from_kgid(&init_user_ns, acl_e->e_gid)); | |
2b2d6d01 TT |
117 | e += sizeof(ext4_acl_entry); |
118 | break; | |
119 | ||
120 | case ACL_USER_OBJ: | |
121 | case ACL_GROUP_OBJ: | |
122 | case ACL_MASK: | |
123 | case ACL_OTHER: | |
124 | e += sizeof(ext4_acl_entry_short); | |
125 | break; | |
126 | ||
127 | default: | |
128 | goto fail; | |
ac27a0ec DK |
129 | } |
130 | } | |
131 | return (char *)ext_acl; | |
132 | ||
133 | fail: | |
134 | kfree(ext_acl); | |
135 | return ERR_PTR(-EINVAL); | |
136 | } | |
137 | ||
ac27a0ec DK |
138 | /* |
139 | * Inode operation get_posix_acl(). | |
140 | * | |
141 | * inode->i_mutex: don't care | |
142 | */ | |
4e34e719 | 143 | struct posix_acl * |
617ba13b | 144 | ext4_get_acl(struct inode *inode, int type) |
ac27a0ec | 145 | { |
ac27a0ec DK |
146 | int name_index; |
147 | char *value = NULL; | |
148 | struct posix_acl *acl; | |
149 | int retval; | |
150 | ||
2b2d6d01 TT |
151 | switch (type) { |
152 | case ACL_TYPE_ACCESS: | |
2b2d6d01 TT |
153 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
154 | break; | |
2b2d6d01 | 155 | case ACL_TYPE_DEFAULT: |
2b2d6d01 TT |
156 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
157 | break; | |
2b2d6d01 | 158 | default: |
073aaa1b | 159 | BUG(); |
ac27a0ec | 160 | } |
617ba13b | 161 | retval = ext4_xattr_get(inode, name_index, "", NULL, 0); |
ac27a0ec | 162 | if (retval > 0) { |
216553c4 | 163 | value = kmalloc(retval, GFP_NOFS); |
ac27a0ec DK |
164 | if (!value) |
165 | return ERR_PTR(-ENOMEM); | |
617ba13b | 166 | retval = ext4_xattr_get(inode, name_index, "", value, retval); |
ac27a0ec DK |
167 | } |
168 | if (retval > 0) | |
617ba13b | 169 | acl = ext4_acl_from_disk(value, retval); |
ac27a0ec DK |
170 | else if (retval == -ENODATA || retval == -ENOSYS) |
171 | acl = NULL; | |
172 | else | |
173 | acl = ERR_PTR(retval); | |
174 | kfree(value); | |
175 | ||
ac27a0ec DK |
176 | return acl; |
177 | } | |
178 | ||
179 | /* | |
180 | * Set the access or default ACL of an inode. | |
181 | * | |
617ba13b | 182 | * inode->i_mutex: down unless called from ext4_new_inode |
ac27a0ec DK |
183 | */ |
184 | static int | |
64e178a7 | 185 | __ext4_set_acl(handle_t *handle, struct inode *inode, int type, |
af65207c | 186 | struct posix_acl *acl, int xattr_flags) |
ac27a0ec | 187 | { |
ac27a0ec DK |
188 | int name_index; |
189 | void *value = NULL; | |
190 | size_t size = 0; | |
191 | int error; | |
192 | ||
2b2d6d01 TT |
193 | switch (type) { |
194 | case ACL_TYPE_ACCESS: | |
195 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; | |
2b2d6d01 | 196 | break; |
ac27a0ec | 197 | |
2b2d6d01 TT |
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; | |
202 | break; | |
ac27a0ec | 203 | |
2b2d6d01 TT |
204 | default: |
205 | return -EINVAL; | |
ac27a0ec DK |
206 | } |
207 | if (acl) { | |
617ba13b | 208 | value = ext4_acl_to_disk(acl, &size); |
ac27a0ec DK |
209 | if (IS_ERR(value)) |
210 | return (int)PTR_ERR(value); | |
211 | } | |
212 | ||
617ba13b | 213 | error = ext4_xattr_set_handle(handle, inode, name_index, "", |
af65207c | 214 | value, size, xattr_flags); |
ac27a0ec DK |
215 | |
216 | kfree(value); | |
397e4341 | 217 | if (!error) { |
073aaa1b | 218 | set_cached_acl(inode, type, acl); |
397e4341 | 219 | } |
2b2d6d01 | 220 | |
ac27a0ec DK |
221 | return error; |
222 | } | |
223 | ||
ac27a0ec | 224 | int |
64e178a7 | 225 | ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
ac27a0ec | 226 | { |
bc26ab5f | 227 | handle_t *handle; |
c1a5d5f6 TE |
228 | int error, credits, retries = 0; |
229 | size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0; | |
a3bb2d55 JK |
230 | umode_t mode = inode->i_mode; |
231 | int update_mode = 0; | |
bc26ab5f | 232 | |
b8cb5a54 TE |
233 | error = dquot_initialize(inode); |
234 | if (error) | |
235 | return error; | |
bc26ab5f | 236 | retry: |
af65207c TE |
237 | error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */, |
238 | &credits); | |
dec214d0 TE |
239 | if (error) |
240 | return error; | |
241 | ||
c1a5d5f6 | 242 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
64e178a7 CH |
243 | if (IS_ERR(handle)) |
244 | return PTR_ERR(handle); | |
245 | ||
a3bb2d55 JK |
246 | if ((type == ACL_TYPE_ACCESS) && acl) { |
247 | error = posix_acl_update_mode(inode, &mode, &acl); | |
248 | if (error) | |
249 | goto out_stop; | |
250 | update_mode = 1; | |
251 | } | |
252 | ||
af65207c | 253 | error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */); |
a3bb2d55 JK |
254 | if (!error && update_mode) { |
255 | inode->i_mode = mode; | |
256 | inode->i_ctime = current_time(inode); | |
257 | ext4_mark_inode_dirty(handle, inode); | |
258 | } | |
259 | out_stop: | |
bc26ab5f | 260 | ext4_journal_stop(handle); |
64e178a7 | 261 | if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
bc26ab5f | 262 | goto retry; |
ac27a0ec DK |
263 | return error; |
264 | } | |
265 | ||
266 | /* | |
64e178a7 CH |
267 | * Initialize the ACLs of a new inode. Called from ext4_new_inode. |
268 | * | |
269 | * dir->i_mutex: down | |
270 | * inode->i_mutex: up (access to inode is still exclusive) | |
ac27a0ec | 271 | */ |
64e178a7 CH |
272 | int |
273 | ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) | |
ac27a0ec | 274 | { |
64e178a7 | 275 | struct posix_acl *default_acl, *acl; |
ac27a0ec DK |
276 | int error; |
277 | ||
64e178a7 CH |
278 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
279 | if (error) | |
280 | return error; | |
ac27a0ec | 281 | |
64e178a7 CH |
282 | if (default_acl) { |
283 | error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT, | |
af65207c | 284 | default_acl, XATTR_CREATE); |
64e178a7 CH |
285 | posix_acl_release(default_acl); |
286 | } | |
287 | if (acl) { | |
288 | if (!error) | |
289 | error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, | |
af65207c | 290 | acl, XATTR_CREATE); |
64e178a7 | 291 | posix_acl_release(acl); |
24ec19b0 | 292 | } |
ac27a0ec DK |
293 | return error; |
294 | } |