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