blob: 9c71fc554021b86e9c9aba28dec3d6964734affb [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
2 * fs/f2fs/acl.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * Portions of this code from linux/fs/ext2/acl.c
8 *
9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15#include <linux/f2fs_fs.h>
16#include "f2fs.h"
17#include "xattr.h"
18#include "acl.h"
19
20#define get_inode_mode(i) ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
21 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
22
23static inline size_t f2fs_acl_size(int count)
24{
25 if (count <= 4) {
26 return sizeof(struct f2fs_acl_header) +
27 count * sizeof(struct f2fs_acl_entry_short);
28 } else {
29 return sizeof(struct f2fs_acl_header) +
30 4 * sizeof(struct f2fs_acl_entry_short) +
31 (count - 4) * sizeof(struct f2fs_acl_entry);
32 }
33}
34
35static inline int f2fs_acl_count(size_t size)
36{
37 ssize_t s;
38 size -= sizeof(struct f2fs_acl_header);
39 s = size - 4 * sizeof(struct f2fs_acl_entry_short);
40 if (s < 0) {
41 if (size % sizeof(struct f2fs_acl_entry_short))
42 return -1;
43 return size / sizeof(struct f2fs_acl_entry_short);
44 } else {
45 if (s % sizeof(struct f2fs_acl_entry))
46 return -1;
47 return s / sizeof(struct f2fs_acl_entry) + 4;
48 }
49}
50
51static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
52{
53 int i, count;
54 struct posix_acl *acl;
55 struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
56 struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
57 const char *end = value + size;
58
59 if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
60 return ERR_PTR(-EINVAL);
61
62 count = f2fs_acl_count(size);
63 if (count < 0)
64 return ERR_PTR(-EINVAL);
65 if (count == 0)
66 return NULL;
67
68 acl = posix_acl_alloc(count, GFP_KERNEL);
69 if (!acl)
70 return ERR_PTR(-ENOMEM);
71
72 for (i = 0; i < count; i++) {
73
74 if ((char *)entry > end)
75 goto fail;
76
77 acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
78 acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
79
80 switch (acl->a_entries[i].e_tag) {
81 case ACL_USER_OBJ:
82 case ACL_GROUP_OBJ:
83 case ACL_MASK:
84 case ACL_OTHER:
85 acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
86 entry = (struct f2fs_acl_entry *)((char *)entry +
87 sizeof(struct f2fs_acl_entry_short));
88 break;
89
90 case ACL_USER:
Linus Torvalds8005ecc2012-12-20 13:54:51 -080091 case ACL_GROUP:
Evan McClain1a808fb2014-03-20 09:12:16 -040092 acl->a_entries[i].e_id = le32_to_cpu(entry->e_id);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080093 entry = (struct f2fs_acl_entry *)((char *)entry +
94 sizeof(struct f2fs_acl_entry));
95 break;
96 default:
97 goto fail;
98 }
99 }
100 if ((char *)entry != end)
101 goto fail;
102 return acl;
103fail:
104 posix_acl_release(acl);
105 return ERR_PTR(-EINVAL);
106}
107
108static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
109{
110 struct f2fs_acl_header *f2fs_acl;
111 struct f2fs_acl_entry *entry;
112 int i;
113
114 f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
115 sizeof(struct f2fs_acl_entry), GFP_KERNEL);
116 if (!f2fs_acl)
117 return ERR_PTR(-ENOMEM);
118
119 f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
120 entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
121
122 for (i = 0; i < acl->a_count; i++) {
123
124 entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
125 entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
126
127 switch (acl->a_entries[i].e_tag) {
128 case ACL_USER:
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800129 case ACL_GROUP:
Evan McClain1a808fb2014-03-20 09:12:16 -0400130 entry->e_id = cpu_to_le32(acl->a_entries[i].e_id);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800131 entry = (struct f2fs_acl_entry *)((char *)entry +
132 sizeof(struct f2fs_acl_entry));
133 break;
134 case ACL_USER_OBJ:
135 case ACL_GROUP_OBJ:
136 case ACL_MASK:
137 case ACL_OTHER:
138 entry = (struct f2fs_acl_entry *)((char *)entry +
139 sizeof(struct f2fs_acl_entry_short));
140 break;
141 default:
142 goto fail;
143 }
144 }
145 *size = f2fs_acl_size(acl->a_count);
146 return (void *)f2fs_acl;
147
148fail:
149 kfree(f2fs_acl);
150 return ERR_PTR(-EINVAL);
151}
152
153struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
154{
155 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
156 int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
157 void *value = NULL;
158 struct posix_acl *acl;
159 int retval;
160
161 if (!test_opt(sbi, POSIX_ACL))
162 return NULL;
163
164 acl = get_cached_acl(inode, type);
165 if (acl != ACL_NOT_CACHED)
166 return acl;
167
168 if (type == ACL_TYPE_ACCESS)
169 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
170
171 retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
172 if (retval > 0) {
Jaegeuk Kim395204b2014-03-20 22:21:08 +0900173 value = kmalloc(retval, GFP_F2FS_ZERO);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800174 if (!value)
175 return ERR_PTR(-ENOMEM);
176 retval = f2fs_getxattr(inode, name_index, "", value, retval);
177 }
178
179 if (retval > 0)
180 acl = f2fs_acl_from_disk(value, retval);
181 else if (retval == -ENODATA)
182 acl = NULL;
183 else
184 acl = ERR_PTR(retval);
185 kfree(value);
186
187 if (!IS_ERR(acl))
188 set_cached_acl(inode, type, acl);
189
190 return acl;
191}
192
193static int f2fs_set_acl(struct inode *inode, int type,
194 struct posix_acl *acl, struct page *ipage)
195{
196 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
197 struct f2fs_inode_info *fi = F2FS_I(inode);
198 int name_index;
199 void *value = NULL;
200 size_t size = 0;
201 int error;
202
203 if (!test_opt(sbi, POSIX_ACL))
204 return 0;
205 if (S_ISLNK(inode->i_mode))
206 return -EOPNOTSUPP;
207
208 switch (type) {
209 case ACL_TYPE_ACCESS:
210 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
211 if (acl) {
212 error = posix_acl_equiv_mode(acl, &inode->i_mode);
213 if (error < 0)
214 return error;
215 set_acl_inode(fi, inode->i_mode);
216 if (error == 0)
217 acl = NULL;
218 }
219 break;
220
221 case ACL_TYPE_DEFAULT:
222 name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
223 if (!S_ISDIR(inode->i_mode))
224 return acl ? -EACCES : 0;
225 break;
226
227 default:
228 return -EINVAL;
229 }
230
231 if (acl) {
232 value = f2fs_acl_to_disk(acl, &size);
233 if (IS_ERR(value)) {
234 cond_clear_inode_flag(fi, FI_ACL_MODE);
235 return (int)PTR_ERR(value);
236 }
237 }
238
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900239 error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800240
241 kfree(value);
242 if (!error)
243 set_cached_acl(inode, type, acl);
244
245 cond_clear_inode_flag(fi, FI_ACL_MODE);
246 return error;
247}
248
249int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage)
250{
251 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
252 struct posix_acl *acl = NULL;
253 int error = 0;
254
255 if (!S_ISLNK(inode->i_mode)) {
256 if (test_opt(sbi, POSIX_ACL)) {
257 acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT);
258 if (IS_ERR(acl))
259 return PTR_ERR(acl);
260 }
261 if (!acl)
262 inode->i_mode &= ~current_umask();
263 }
264
265 if (!test_opt(sbi, POSIX_ACL) || !acl)
266 goto cleanup;
267
268 if (S_ISDIR(inode->i_mode)) {
269 error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl, ipage);
270 if (error)
271 goto cleanup;
272 }
273 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
274 if (error < 0)
275 return error;
276 if (error > 0)
277 error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, ipage);
278cleanup:
279 posix_acl_release(acl);
280 return error;
281}
282
283int f2fs_acl_chmod(struct inode *inode)
284{
285 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
286 struct posix_acl *acl;
287 int error;
288 mode_t mode = get_inode_mode(inode);
289
290 if (!test_opt(sbi, POSIX_ACL))
291 return 0;
292 if (S_ISLNK(mode))
293 return -EOPNOTSUPP;
294
295 acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
296 if (IS_ERR(acl) || !acl)
297 return PTR_ERR(acl);
298
299 error = posix_acl_chmod(&acl, GFP_KERNEL, mode);
300 if (error)
301 return error;
302
303 error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, NULL);
304 posix_acl_release(acl);
305 return error;
306}
307
308static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
309 size_t list_size, const char *name, size_t name_len, int type)
310{
311 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
312 const char *xname = POSIX_ACL_XATTR_DEFAULT;
313 size_t size;
314
315 if (!test_opt(sbi, POSIX_ACL))
316 return 0;
317
318 if (type == ACL_TYPE_ACCESS)
319 xname = POSIX_ACL_XATTR_ACCESS;
320
321 size = strlen(xname) + 1;
322 if (list && size <= list_size)
323 memcpy(list, xname, size);
324 return size;
325}
326
327static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
328 void *buffer, size_t size, int type)
329{
330 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
331 struct posix_acl *acl;
332 int error;
333
334 if (strcmp(name, "") != 0)
335 return -EINVAL;
336 if (!test_opt(sbi, POSIX_ACL))
337 return -EOPNOTSUPP;
338
339 acl = f2fs_get_acl(dentry->d_inode, type);
340 if (IS_ERR(acl))
341 return PTR_ERR(acl);
342 if (!acl)
343 return -ENODATA;
Evan McClain1a808fb2014-03-20 09:12:16 -0400344 error = posix_acl_to_xattr(acl, buffer, size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800345 posix_acl_release(acl);
346
347 return error;
348}
349
350static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
351 const void *value, size_t size, int flags, int type)
352{
353 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
354 struct inode *inode = dentry->d_inode;
355 struct posix_acl *acl = NULL;
356 int error;
357
358 if (strcmp(name, "") != 0)
359 return -EINVAL;
360 if (!test_opt(sbi, POSIX_ACL))
361 return -EOPNOTSUPP;
362 if (!inode_owner_or_capable(inode))
363 return -EPERM;
364
365 if (value) {
Evan McClain1a808fb2014-03-20 09:12:16 -0400366 acl = posix_acl_from_xattr(value, size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800367 if (IS_ERR(acl))
368 return PTR_ERR(acl);
369 if (acl) {
370 error = posix_acl_valid(acl);
371 if (error)
372 goto release_and_out;
373 }
374 } else {
375 acl = NULL;
376 }
377
378 error = f2fs_set_acl(inode, type, acl, NULL);
379
380release_and_out:
381 posix_acl_release(acl);
382 return error;
383}
384
385const struct xattr_handler f2fs_xattr_acl_default_handler = {
386 .prefix = POSIX_ACL_XATTR_DEFAULT,
387 .flags = ACL_TYPE_DEFAULT,
388 .list = f2fs_xattr_list_acl,
389 .get = f2fs_xattr_get_acl,
390 .set = f2fs_xattr_set_acl,
391};
392
393const struct xattr_handler f2fs_xattr_acl_access_handler = {
394 .prefix = POSIX_ACL_XATTR_ACCESS,
395 .flags = ACL_TYPE_ACCESS,
396 .list = f2fs_xattr_list_acl,
397 .get = f2fs_xattr_get_acl,
398 .set = f2fs_xattr_set_acl,
399};