4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Portions of this code from linux/fs/ext2/acl.c
9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
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.
15 #include <linux/f2fs_fs.h>
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))
23 static inline size_t f2fs_acl_size(int count)
26 return sizeof(struct f2fs_acl_header) +
27 count * sizeof(struct f2fs_acl_entry_short);
29 return sizeof(struct f2fs_acl_header) +
30 4 * sizeof(struct f2fs_acl_entry_short) +
31 (count - 4) * sizeof(struct f2fs_acl_entry);
35 static inline int f2fs_acl_count(size_t size)
38 size -= sizeof(struct f2fs_acl_header);
39 s = size - 4 * sizeof(struct f2fs_acl_entry_short);
41 if (size % sizeof(struct f2fs_acl_entry_short))
43 return size / sizeof(struct f2fs_acl_entry_short);
45 if (s % sizeof(struct f2fs_acl_entry))
47 return s / sizeof(struct f2fs_acl_entry) + 4;
51 static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
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;
59 if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
60 return ERR_PTR(-EINVAL);
62 count = f2fs_acl_count(size);
64 return ERR_PTR(-EINVAL);
68 acl = posix_acl_alloc(count, GFP_KERNEL);
70 return ERR_PTR(-ENOMEM);
72 for (i = 0; i < count; i++) {
74 if ((char *)entry > end)
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);
80 switch (acl->a_entries[i].e_tag) {
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));
91 acl->a_entries[i].e_uid =
92 make_kuid(&init_user_ns,
93 le32_to_cpu(entry->e_id));
94 entry = (struct f2fs_acl_entry *)((char *)entry +
95 sizeof(struct f2fs_acl_entry));
98 acl->a_entries[i].e_gid =
99 make_kgid(&init_user_ns,
100 le32_to_cpu(entry->e_id));
101 entry = (struct f2fs_acl_entry *)((char *)entry +
102 sizeof(struct f2fs_acl_entry));
108 if ((char *)entry != end)
112 posix_acl_release(acl);
113 return ERR_PTR(-EINVAL);
116 static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
118 struct f2fs_acl_header *f2fs_acl;
119 struct f2fs_acl_entry *entry;
122 f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
123 sizeof(struct f2fs_acl_entry), GFP_KERNEL);
125 return ERR_PTR(-ENOMEM);
127 f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
128 entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
130 for (i = 0; i < acl->a_count; i++) {
132 entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
133 entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
135 switch (acl->a_entries[i].e_tag) {
137 entry->e_id = cpu_to_le32(
138 from_kuid(&init_user_ns,
139 acl->a_entries[i].e_uid));
140 entry = (struct f2fs_acl_entry *)((char *)entry +
141 sizeof(struct f2fs_acl_entry));
144 entry->e_id = cpu_to_le32(
145 from_kgid(&init_user_ns,
146 acl->a_entries[i].e_gid));
147 entry = (struct f2fs_acl_entry *)((char *)entry +
148 sizeof(struct f2fs_acl_entry));
154 entry = (struct f2fs_acl_entry *)((char *)entry +
155 sizeof(struct f2fs_acl_entry_short));
161 *size = f2fs_acl_size(acl->a_count);
162 return (void *)f2fs_acl;
166 return ERR_PTR(-EINVAL);
169 struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
171 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
172 int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
174 struct posix_acl *acl;
177 if (!test_opt(sbi, POSIX_ACL))
180 acl = get_cached_acl(inode, type);
181 if (acl != ACL_NOT_CACHED)
184 if (type == ACL_TYPE_ACCESS)
185 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
187 retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
189 value = kmalloc(retval, GFP_KERNEL);
191 return ERR_PTR(-ENOMEM);
192 retval = f2fs_getxattr(inode, name_index, "", value, retval);
196 if (retval == -ENODATA)
199 acl = ERR_PTR(retval);
201 acl = f2fs_acl_from_disk(value, retval);
205 set_cached_acl(inode, type, acl);
210 static int f2fs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
212 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
213 struct f2fs_inode_info *fi = F2FS_I(inode);
219 if (!test_opt(sbi, POSIX_ACL))
221 if (S_ISLNK(inode->i_mode))
225 case ACL_TYPE_ACCESS:
226 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
228 error = posix_acl_equiv_mode(acl, &inode->i_mode);
231 set_acl_inode(fi, inode->i_mode);
237 case ACL_TYPE_DEFAULT:
238 name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
239 if (!S_ISDIR(inode->i_mode))
240 return acl ? -EACCES : 0;
248 value = f2fs_acl_to_disk(acl, &size);
250 cond_clear_inode_flag(fi, FI_ACL_MODE);
251 return (int)PTR_ERR(value);
255 error = f2fs_setxattr(inode, name_index, "", value, size);
259 set_cached_acl(inode, type, acl);
261 cond_clear_inode_flag(fi, FI_ACL_MODE);
265 int f2fs_init_acl(struct inode *inode, struct inode *dir)
267 struct posix_acl *acl = NULL;
268 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
271 if (!S_ISLNK(inode->i_mode)) {
272 if (test_opt(sbi, POSIX_ACL)) {
273 acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT);
278 inode->i_mode &= ~current_umask();
281 if (test_opt(sbi, POSIX_ACL) && acl) {
283 if (S_ISDIR(inode->i_mode)) {
284 error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
288 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
292 error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
295 posix_acl_release(acl);
299 int f2fs_acl_chmod(struct inode *inode)
301 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
302 struct posix_acl *acl;
304 mode_t mode = get_inode_mode(inode);
306 if (!test_opt(sbi, POSIX_ACL))
311 acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
312 if (IS_ERR(acl) || !acl)
315 error = posix_acl_chmod(&acl, GFP_KERNEL, mode);
318 error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
319 posix_acl_release(acl);
323 static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
324 size_t list_size, const char *name, size_t name_len, int type)
326 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
327 const char *xname = POSIX_ACL_XATTR_DEFAULT;
330 if (!test_opt(sbi, POSIX_ACL))
333 if (type == ACL_TYPE_ACCESS)
334 xname = POSIX_ACL_XATTR_ACCESS;
336 size = strlen(xname) + 1;
337 if (list && size <= list_size)
338 memcpy(list, xname, size);
342 static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
343 void *buffer, size_t size, int type)
345 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
346 struct posix_acl *acl;
349 if (strcmp(name, "") != 0)
351 if (!test_opt(sbi, POSIX_ACL))
354 acl = f2fs_get_acl(dentry->d_inode, type);
359 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
360 posix_acl_release(acl);
365 static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
366 const void *value, size_t size, int flags, int type)
368 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
369 struct inode *inode = dentry->d_inode;
370 struct posix_acl *acl = NULL;
373 if (strcmp(name, "") != 0)
375 if (!test_opt(sbi, POSIX_ACL))
377 if (!inode_owner_or_capable(inode))
381 acl = posix_acl_from_xattr(&init_user_ns, value, size);
385 error = posix_acl_valid(acl);
387 goto release_and_out;
393 error = f2fs_set_acl(inode, type, acl);
396 posix_acl_release(acl);
400 const struct xattr_handler f2fs_xattr_acl_default_handler = {
401 .prefix = POSIX_ACL_XATTR_DEFAULT,
402 .flags = ACL_TYPE_DEFAULT,
403 .list = f2fs_xattr_list_acl,
404 .get = f2fs_xattr_get_acl,
405 .set = f2fs_xattr_set_acl,
408 const struct xattr_handler f2fs_xattr_acl_access_handler = {
409 .prefix = POSIX_ACL_XATTR_ACCESS,
410 .flags = ACL_TYPE_ACCESS,
411 .list = f2fs_xattr_list_acl,
412 .get = f2fs_xattr_get_acl,
413 .set = f2fs_xattr_set_acl,