xref: /openbmc/linux/fs/f2fs/acl.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
17c1a000dSChao Yu // SPDX-License-Identifier: GPL-2.0
20a8165d7SJaegeuk Kim /*
3af48b85bSJaegeuk Kim  * fs/f2fs/acl.c
4af48b85bSJaegeuk Kim  *
5af48b85bSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6af48b85bSJaegeuk Kim  *             http://www.samsung.com/
7af48b85bSJaegeuk Kim  *
8af48b85bSJaegeuk Kim  * Portions of this code from linux/fs/ext2/acl.c
9af48b85bSJaegeuk Kim  *
10af48b85bSJaegeuk Kim  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
11af48b85bSJaegeuk Kim  */
12af48b85bSJaegeuk Kim #include <linux/f2fs_fs.h>
13af48b85bSJaegeuk Kim #include "f2fs.h"
14af48b85bSJaegeuk Kim #include "xattr.h"
15af48b85bSJaegeuk Kim #include "acl.h"
16af48b85bSJaegeuk Kim 
f2fs_acl_size(int count)17af48b85bSJaegeuk Kim static inline size_t f2fs_acl_size(int count)
18af48b85bSJaegeuk Kim {
19af48b85bSJaegeuk Kim 	if (count <= 4) {
20af48b85bSJaegeuk Kim 		return sizeof(struct f2fs_acl_header) +
21af48b85bSJaegeuk Kim 			count * sizeof(struct f2fs_acl_entry_short);
22af48b85bSJaegeuk Kim 	} else {
23af48b85bSJaegeuk Kim 		return sizeof(struct f2fs_acl_header) +
24af48b85bSJaegeuk Kim 			4 * sizeof(struct f2fs_acl_entry_short) +
25af48b85bSJaegeuk Kim 			(count - 4) * sizeof(struct f2fs_acl_entry);
26af48b85bSJaegeuk Kim 	}
27af48b85bSJaegeuk Kim }
28af48b85bSJaegeuk Kim 
f2fs_acl_count(size_t size)29af48b85bSJaegeuk Kim static inline int f2fs_acl_count(size_t size)
30af48b85bSJaegeuk Kim {
31af48b85bSJaegeuk Kim 	ssize_t s;
325f029c04SYi Zhuang 
33af48b85bSJaegeuk Kim 	size -= sizeof(struct f2fs_acl_header);
34af48b85bSJaegeuk Kim 	s = size - 4 * sizeof(struct f2fs_acl_entry_short);
35af48b85bSJaegeuk Kim 	if (s < 0) {
36af48b85bSJaegeuk Kim 		if (size % sizeof(struct f2fs_acl_entry_short))
37af48b85bSJaegeuk Kim 			return -1;
38af48b85bSJaegeuk Kim 		return size / sizeof(struct f2fs_acl_entry_short);
39af48b85bSJaegeuk Kim 	} else {
40af48b85bSJaegeuk Kim 		if (s % sizeof(struct f2fs_acl_entry))
41af48b85bSJaegeuk Kim 			return -1;
42af48b85bSJaegeuk Kim 		return s / sizeof(struct f2fs_acl_entry) + 4;
43af48b85bSJaegeuk Kim 	}
44af48b85bSJaegeuk Kim }
45af48b85bSJaegeuk Kim 
f2fs_acl_from_disk(const char * value,size_t size)46af48b85bSJaegeuk Kim static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
47af48b85bSJaegeuk Kim {
48af48b85bSJaegeuk Kim 	int i, count;
49af48b85bSJaegeuk Kim 	struct posix_acl *acl;
50af48b85bSJaegeuk Kim 	struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
51af48b85bSJaegeuk Kim 	struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
52af48b85bSJaegeuk Kim 	const char *end = value + size;
53af48b85bSJaegeuk Kim 
541618e6e2SChengguang Xu 	if (size < sizeof(struct f2fs_acl_header))
551618e6e2SChengguang Xu 		return ERR_PTR(-EINVAL);
561618e6e2SChengguang Xu 
57af48b85bSJaegeuk Kim 	if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
58af48b85bSJaegeuk Kim 		return ERR_PTR(-EINVAL);
59af48b85bSJaegeuk Kim 
60af48b85bSJaegeuk Kim 	count = f2fs_acl_count(size);
61af48b85bSJaegeuk Kim 	if (count < 0)
62af48b85bSJaegeuk Kim 		return ERR_PTR(-EINVAL);
63af48b85bSJaegeuk Kim 	if (count == 0)
64af48b85bSJaegeuk Kim 		return NULL;
65af48b85bSJaegeuk Kim 
66dd802406SJaegeuk Kim 	acl = posix_acl_alloc(count, GFP_NOFS);
67af48b85bSJaegeuk Kim 	if (!acl)
68af48b85bSJaegeuk Kim 		return ERR_PTR(-ENOMEM);
69af48b85bSJaegeuk Kim 
70af48b85bSJaegeuk Kim 	for (i = 0; i < count; i++) {
71af48b85bSJaegeuk Kim 
72af48b85bSJaegeuk Kim 		if ((char *)entry > end)
73af48b85bSJaegeuk Kim 			goto fail;
74af48b85bSJaegeuk Kim 
75af48b85bSJaegeuk Kim 		acl->a_entries[i].e_tag  = le16_to_cpu(entry->e_tag);
76af48b85bSJaegeuk Kim 		acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
77af48b85bSJaegeuk Kim 
78af48b85bSJaegeuk Kim 		switch (acl->a_entries[i].e_tag) {
79af48b85bSJaegeuk Kim 		case ACL_USER_OBJ:
80af48b85bSJaegeuk Kim 		case ACL_GROUP_OBJ:
81af48b85bSJaegeuk Kim 		case ACL_MASK:
82af48b85bSJaegeuk Kim 		case ACL_OTHER:
83af48b85bSJaegeuk Kim 			entry = (struct f2fs_acl_entry *)((char *)entry +
84af48b85bSJaegeuk Kim 					sizeof(struct f2fs_acl_entry_short));
85af48b85bSJaegeuk Kim 			break;
86af48b85bSJaegeuk Kim 
87af48b85bSJaegeuk Kim 		case ACL_USER:
88af48b85bSJaegeuk Kim 			acl->a_entries[i].e_uid =
89af48b85bSJaegeuk Kim 				make_kuid(&init_user_ns,
90af48b85bSJaegeuk Kim 						le32_to_cpu(entry->e_id));
91af48b85bSJaegeuk Kim 			entry = (struct f2fs_acl_entry *)((char *)entry +
92af48b85bSJaegeuk Kim 					sizeof(struct f2fs_acl_entry));
93af48b85bSJaegeuk Kim 			break;
94af48b85bSJaegeuk Kim 		case ACL_GROUP:
95af48b85bSJaegeuk Kim 			acl->a_entries[i].e_gid =
96af48b85bSJaegeuk Kim 				make_kgid(&init_user_ns,
97af48b85bSJaegeuk Kim 						le32_to_cpu(entry->e_id));
98af48b85bSJaegeuk Kim 			entry = (struct f2fs_acl_entry *)((char *)entry +
99af48b85bSJaegeuk Kim 					sizeof(struct f2fs_acl_entry));
100af48b85bSJaegeuk Kim 			break;
101af48b85bSJaegeuk Kim 		default:
102af48b85bSJaegeuk Kim 			goto fail;
103af48b85bSJaegeuk Kim 		}
104af48b85bSJaegeuk Kim 	}
105af48b85bSJaegeuk Kim 	if ((char *)entry != end)
106af48b85bSJaegeuk Kim 		goto fail;
107af48b85bSJaegeuk Kim 	return acl;
108af48b85bSJaegeuk Kim fail:
109af48b85bSJaegeuk Kim 	posix_acl_release(acl);
110af48b85bSJaegeuk Kim 	return ERR_PTR(-EINVAL);
111af48b85bSJaegeuk Kim }
112af48b85bSJaegeuk Kim 
f2fs_acl_to_disk(struct f2fs_sb_info * sbi,const struct posix_acl * acl,size_t * size)1131ecc0c5cSChao Yu static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
1141ecc0c5cSChao Yu 				const struct posix_acl *acl, size_t *size)
115af48b85bSJaegeuk Kim {
116af48b85bSJaegeuk Kim 	struct f2fs_acl_header *f2fs_acl;
117af48b85bSJaegeuk Kim 	struct f2fs_acl_entry *entry;
118af48b85bSJaegeuk Kim 	int i;
119af48b85bSJaegeuk Kim 
1201ecc0c5cSChao Yu 	f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
1211ecc0c5cSChao Yu 			acl->a_count * sizeof(struct f2fs_acl_entry),
1221ecc0c5cSChao Yu 			GFP_NOFS);
123af48b85bSJaegeuk Kim 	if (!f2fs_acl)
124af48b85bSJaegeuk Kim 		return ERR_PTR(-ENOMEM);
125af48b85bSJaegeuk Kim 
126af48b85bSJaegeuk Kim 	f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
127af48b85bSJaegeuk Kim 	entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
128af48b85bSJaegeuk Kim 
129af48b85bSJaegeuk Kim 	for (i = 0; i < acl->a_count; i++) {
130af48b85bSJaegeuk Kim 
131af48b85bSJaegeuk Kim 		entry->e_tag  = cpu_to_le16(acl->a_entries[i].e_tag);
132af48b85bSJaegeuk Kim 		entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
133af48b85bSJaegeuk Kim 
134af48b85bSJaegeuk Kim 		switch (acl->a_entries[i].e_tag) {
135af48b85bSJaegeuk Kim 		case ACL_USER:
136af48b85bSJaegeuk Kim 			entry->e_id = cpu_to_le32(
137af48b85bSJaegeuk Kim 					from_kuid(&init_user_ns,
138af48b85bSJaegeuk Kim 						acl->a_entries[i].e_uid));
139af48b85bSJaegeuk Kim 			entry = (struct f2fs_acl_entry *)((char *)entry +
140af48b85bSJaegeuk Kim 					sizeof(struct f2fs_acl_entry));
141af48b85bSJaegeuk Kim 			break;
142af48b85bSJaegeuk Kim 		case ACL_GROUP:
143af48b85bSJaegeuk Kim 			entry->e_id = cpu_to_le32(
144af48b85bSJaegeuk Kim 					from_kgid(&init_user_ns,
145af48b85bSJaegeuk Kim 						acl->a_entries[i].e_gid));
146af48b85bSJaegeuk Kim 			entry = (struct f2fs_acl_entry *)((char *)entry +
147af48b85bSJaegeuk Kim 					sizeof(struct f2fs_acl_entry));
148af48b85bSJaegeuk Kim 			break;
149af48b85bSJaegeuk Kim 		case ACL_USER_OBJ:
150af48b85bSJaegeuk Kim 		case ACL_GROUP_OBJ:
151af48b85bSJaegeuk Kim 		case ACL_MASK:
152af48b85bSJaegeuk Kim 		case ACL_OTHER:
153af48b85bSJaegeuk Kim 			entry = (struct f2fs_acl_entry *)((char *)entry +
154af48b85bSJaegeuk Kim 					sizeof(struct f2fs_acl_entry_short));
155af48b85bSJaegeuk Kim 			break;
156af48b85bSJaegeuk Kim 		default:
157af48b85bSJaegeuk Kim 			goto fail;
158af48b85bSJaegeuk Kim 		}
159af48b85bSJaegeuk Kim 	}
160af48b85bSJaegeuk Kim 	*size = f2fs_acl_size(acl->a_count);
161af48b85bSJaegeuk Kim 	return (void *)f2fs_acl;
162af48b85bSJaegeuk Kim 
163af48b85bSJaegeuk Kim fail:
164c8eb7024SChao Yu 	kfree(f2fs_acl);
165af48b85bSJaegeuk Kim 	return ERR_PTR(-EINVAL);
166af48b85bSJaegeuk Kim }
167af48b85bSJaegeuk Kim 
__f2fs_get_acl(struct inode * inode,int type,struct page * dpage)168bce8d112SJaegeuk Kim static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
169bce8d112SJaegeuk Kim 						struct page *dpage)
170af48b85bSJaegeuk Kim {
171af48b85bSJaegeuk Kim 	int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
172af48b85bSJaegeuk Kim 	void *value = NULL;
173af48b85bSJaegeuk Kim 	struct posix_acl *acl;
174af48b85bSJaegeuk Kim 	int retval;
175af48b85bSJaegeuk Kim 
176af48b85bSJaegeuk Kim 	if (type == ACL_TYPE_ACCESS)
177af48b85bSJaegeuk Kim 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
178af48b85bSJaegeuk Kim 
179bce8d112SJaegeuk Kim 	retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
180af48b85bSJaegeuk Kim 	if (retval > 0) {
1811ecc0c5cSChao Yu 		value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
182af48b85bSJaegeuk Kim 		if (!value)
183af48b85bSJaegeuk Kim 			return ERR_PTR(-ENOMEM);
184bce8d112SJaegeuk Kim 		retval = f2fs_getxattr(inode, name_index, "", value,
185bce8d112SJaegeuk Kim 							retval, dpage);
186af48b85bSJaegeuk Kim 	}
187af48b85bSJaegeuk Kim 
188c1b75eabSJaegeuk Kim 	if (retval > 0)
189c1b75eabSJaegeuk Kim 		acl = f2fs_acl_from_disk(value, retval);
190c1b75eabSJaegeuk Kim 	else if (retval == -ENODATA)
191af48b85bSJaegeuk Kim 		acl = NULL;
192af48b85bSJaegeuk Kim 	else
193af48b85bSJaegeuk Kim 		acl = ERR_PTR(retval);
194c8eb7024SChao Yu 	kfree(value);
195c1b75eabSJaegeuk Kim 
196af48b85bSJaegeuk Kim 	return acl;
197af48b85bSJaegeuk Kim }
198af48b85bSJaegeuk Kim 
f2fs_get_acl(struct inode * inode,int type,bool rcu)1990cad6246SMiklos Szeredi struct posix_acl *f2fs_get_acl(struct inode *inode, int type, bool rcu)
200bce8d112SJaegeuk Kim {
2010cad6246SMiklos Szeredi 	if (rcu)
2020cad6246SMiklos Szeredi 		return ERR_PTR(-ECHILD);
2030cad6246SMiklos Szeredi 
204bce8d112SJaegeuk Kim 	return __f2fs_get_acl(inode, type, NULL);
205bce8d112SJaegeuk Kim }
206bce8d112SJaegeuk Kim 
f2fs_acl_update_mode(struct mnt_idmap * idmap,struct inode * inode,umode_t * mode_p,struct posix_acl ** acl)2079452e93eSChristian Brauner static int f2fs_acl_update_mode(struct mnt_idmap *idmap,
208984fc4e7SChao Yu 				struct inode *inode, umode_t *mode_p,
20917232e83SChao Yu 				struct posix_acl **acl)
21017232e83SChao Yu {
21117232e83SChao Yu 	umode_t mode = inode->i_mode;
21217232e83SChao Yu 	int error;
21317232e83SChao Yu 
21417232e83SChao Yu 	if (is_inode_flag_set(inode, FI_ACL_MODE))
21517232e83SChao Yu 		mode = F2FS_I(inode)->i_acl_mode;
21617232e83SChao Yu 
21717232e83SChao Yu 	error = posix_acl_equiv_mode(*acl, &mode);
21817232e83SChao Yu 	if (error < 0)
21917232e83SChao Yu 		return error;
22017232e83SChao Yu 	if (error == 0)
22117232e83SChao Yu 		*acl = NULL;
222*e67fe633SChristian Brauner 	if (!vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)) &&
2239452e93eSChristian Brauner 	    !capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
22417232e83SChao Yu 		mode &= ~S_ISGID;
22517232e83SChao Yu 	*mode_p = mode;
22617232e83SChao Yu 	return 0;
22717232e83SChao Yu }
22817232e83SChao Yu 
__f2fs_set_acl(struct mnt_idmap * idmap,struct inode * inode,int type,struct posix_acl * acl,struct page * ipage)2299452e93eSChristian Brauner static int __f2fs_set_acl(struct mnt_idmap *idmap,
230984fc4e7SChao Yu 			struct inode *inode, int type,
2312ed2d5b3SJaegeuk Kim 			struct posix_acl *acl, struct page *ipage)
232af48b85bSJaegeuk Kim {
233af48b85bSJaegeuk Kim 	int name_index;
234af48b85bSJaegeuk Kim 	void *value = NULL;
235af48b85bSJaegeuk Kim 	size_t size = 0;
236af48b85bSJaegeuk Kim 	int error;
23714af20fcSErnesto A. Fernández 	umode_t mode = inode->i_mode;
238af48b85bSJaegeuk Kim 
239af48b85bSJaegeuk Kim 	switch (type) {
240af48b85bSJaegeuk Kim 	case ACL_TYPE_ACCESS:
241af48b85bSJaegeuk Kim 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
242c925dc16SJaegeuk Kim 		if (acl && !ipage) {
2439452e93eSChristian Brauner 			error = f2fs_acl_update_mode(idmap, inode,
244984fc4e7SChao Yu 								&mode, &acl);
24507393101SJan Kara 			if (error)
246af48b85bSJaegeuk Kim 				return error;
24714af20fcSErnesto A. Fernández 			set_acl_inode(inode, mode);
248af48b85bSJaegeuk Kim 		}
249af48b85bSJaegeuk Kim 		break;
250af48b85bSJaegeuk Kim 
251af48b85bSJaegeuk Kim 	case ACL_TYPE_DEFAULT:
252af48b85bSJaegeuk Kim 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
253af48b85bSJaegeuk Kim 		if (!S_ISDIR(inode->i_mode))
254af48b85bSJaegeuk Kim 			return acl ? -EACCES : 0;
255af48b85bSJaegeuk Kim 		break;
256af48b85bSJaegeuk Kim 
257af48b85bSJaegeuk Kim 	default:
258af48b85bSJaegeuk Kim 		return -EINVAL;
259af48b85bSJaegeuk Kim 	}
260af48b85bSJaegeuk Kim 
261af48b85bSJaegeuk Kim 	if (acl) {
2621ecc0c5cSChao Yu 		value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
263af48b85bSJaegeuk Kim 		if (IS_ERR(value)) {
26491942321SJaegeuk Kim 			clear_inode_flag(inode, FI_ACL_MODE);
26568390dd9SZhang Shengju 			return PTR_ERR(value);
266af48b85bSJaegeuk Kim 		}
267af48b85bSJaegeuk Kim 	}
268af48b85bSJaegeuk Kim 
269c02745efSJaegeuk Kim 	error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
270af48b85bSJaegeuk Kim 
271c8eb7024SChao Yu 	kfree(value);
272af48b85bSJaegeuk Kim 	if (!error)
273af48b85bSJaegeuk Kim 		set_cached_acl(inode, type, acl);
274af48b85bSJaegeuk Kim 
27591942321SJaegeuk Kim 	clear_inode_flag(inode, FI_ACL_MODE);
276af48b85bSJaegeuk Kim 	return error;
277af48b85bSJaegeuk Kim }
278af48b85bSJaegeuk Kim 
f2fs_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * acl,int type)27913e83a49SChristian Brauner int f2fs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
280549c7297SChristian Brauner 		 struct posix_acl *acl, int type)
281a6dda0e6SChristoph Hellwig {
282138060baSChristian Brauner 	struct inode *inode = d_inode(dentry);
283138060baSChristian Brauner 
2841f227a3eSJaegeuk Kim 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2851f227a3eSJaegeuk Kim 		return -EIO;
2861f227a3eSJaegeuk Kim 
2879452e93eSChristian Brauner 	return __f2fs_set_acl(idmap, inode, type, acl, NULL);
288a6dda0e6SChristoph Hellwig }
289a6dda0e6SChristoph Hellwig 
290bce8d112SJaegeuk Kim /*
291bce8d112SJaegeuk Kim  * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
292bce8d112SJaegeuk Kim  * are copied from posix_acl.c
293bce8d112SJaegeuk Kim  */
f2fs_acl_clone(const struct posix_acl * acl,gfp_t flags)294bce8d112SJaegeuk Kim static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
295bce8d112SJaegeuk Kim 							gfp_t flags)
296af48b85bSJaegeuk Kim {
297bce8d112SJaegeuk Kim 	struct posix_acl *clone = NULL;
298bce8d112SJaegeuk Kim 
299bce8d112SJaegeuk Kim 	if (acl) {
300bce8d112SJaegeuk Kim 		int size = sizeof(struct posix_acl) + acl->a_count *
301bce8d112SJaegeuk Kim 				sizeof(struct posix_acl_entry);
302bce8d112SJaegeuk Kim 		clone = kmemdup(acl, size, flags);
303bce8d112SJaegeuk Kim 		if (clone)
30466717260SElena Reshetova 			refcount_set(&clone->a_refcount, 1);
305bce8d112SJaegeuk Kim 	}
306bce8d112SJaegeuk Kim 	return clone;
307bce8d112SJaegeuk Kim }
308bce8d112SJaegeuk Kim 
f2fs_acl_create_masq(struct posix_acl * acl,umode_t * mode_p)309bce8d112SJaegeuk Kim static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
310bce8d112SJaegeuk Kim {
311bce8d112SJaegeuk Kim 	struct posix_acl_entry *pa, *pe;
312bce8d112SJaegeuk Kim 	struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
313bce8d112SJaegeuk Kim 	umode_t mode = *mode_p;
314bce8d112SJaegeuk Kim 	int not_equiv = 0;
315bce8d112SJaegeuk Kim 
316bce8d112SJaegeuk Kim 	/* assert(atomic_read(acl->a_refcount) == 1); */
317bce8d112SJaegeuk Kim 
318bce8d112SJaegeuk Kim 	FOREACH_ACL_ENTRY(pa, acl, pe) {
319bce8d112SJaegeuk Kim 		switch (pa->e_tag) {
320bce8d112SJaegeuk Kim 		case ACL_USER_OBJ:
321bce8d112SJaegeuk Kim 			pa->e_perm &= (mode >> 6) | ~S_IRWXO;
322bce8d112SJaegeuk Kim 			mode &= (pa->e_perm << 6) | ~S_IRWXU;
323bce8d112SJaegeuk Kim 			break;
324bce8d112SJaegeuk Kim 
325bce8d112SJaegeuk Kim 		case ACL_USER:
326bce8d112SJaegeuk Kim 		case ACL_GROUP:
327bce8d112SJaegeuk Kim 			not_equiv = 1;
328bce8d112SJaegeuk Kim 			break;
329bce8d112SJaegeuk Kim 
330bce8d112SJaegeuk Kim 		case ACL_GROUP_OBJ:
331bce8d112SJaegeuk Kim 			group_obj = pa;
332bce8d112SJaegeuk Kim 			break;
333bce8d112SJaegeuk Kim 
334bce8d112SJaegeuk Kim 		case ACL_OTHER:
335bce8d112SJaegeuk Kim 			pa->e_perm &= mode | ~S_IRWXO;
336bce8d112SJaegeuk Kim 			mode &= pa->e_perm | ~S_IRWXO;
337bce8d112SJaegeuk Kim 			break;
338bce8d112SJaegeuk Kim 
339bce8d112SJaegeuk Kim 		case ACL_MASK:
340bce8d112SJaegeuk Kim 			mask_obj = pa;
341bce8d112SJaegeuk Kim 			not_equiv = 1;
342bce8d112SJaegeuk Kim 			break;
343bce8d112SJaegeuk Kim 
344bce8d112SJaegeuk Kim 		default:
345bce8d112SJaegeuk Kim 			return -EIO;
346bce8d112SJaegeuk Kim 		}
347bce8d112SJaegeuk Kim 	}
348bce8d112SJaegeuk Kim 
349bce8d112SJaegeuk Kim 	if (mask_obj) {
350bce8d112SJaegeuk Kim 		mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
351bce8d112SJaegeuk Kim 		mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
352bce8d112SJaegeuk Kim 	} else {
353bce8d112SJaegeuk Kim 		if (!group_obj)
354bce8d112SJaegeuk Kim 			return -EIO;
355bce8d112SJaegeuk Kim 		group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
356bce8d112SJaegeuk Kim 		mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
357bce8d112SJaegeuk Kim 	}
358bce8d112SJaegeuk Kim 
359bce8d112SJaegeuk Kim 	*mode_p = (*mode_p & ~S_IRWXUGO) | mode;
360bce8d112SJaegeuk Kim 	return not_equiv;
361bce8d112SJaegeuk Kim }
362bce8d112SJaegeuk Kim 
f2fs_acl_create(struct inode * dir,umode_t * mode,struct posix_acl ** default_acl,struct posix_acl ** acl,struct page * dpage)363bce8d112SJaegeuk Kim static int f2fs_acl_create(struct inode *dir, umode_t *mode,
364bce8d112SJaegeuk Kim 		struct posix_acl **default_acl, struct posix_acl **acl,
365bce8d112SJaegeuk Kim 		struct page *dpage)
366bce8d112SJaegeuk Kim {
367bce8d112SJaegeuk Kim 	struct posix_acl *p;
368272e083fSChao Yu 	struct posix_acl *clone;
369bce8d112SJaegeuk Kim 	int ret;
370bce8d112SJaegeuk Kim 
371272e083fSChao Yu 	*acl = NULL;
372272e083fSChao Yu 	*default_acl = NULL;
373272e083fSChao Yu 
374bce8d112SJaegeuk Kim 	if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
375272e083fSChao Yu 		return 0;
376bce8d112SJaegeuk Kim 
377bce8d112SJaegeuk Kim 	p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
378272e083fSChao Yu 	if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
379272e083fSChao Yu 		*mode &= ~current_umask();
380272e083fSChao Yu 		return 0;
381bce8d112SJaegeuk Kim 	}
382272e083fSChao Yu 	if (IS_ERR(p))
383272e083fSChao Yu 		return PTR_ERR(p);
384bce8d112SJaegeuk Kim 
385272e083fSChao Yu 	clone = f2fs_acl_clone(p, GFP_NOFS);
386f6176473STiezhu Yang 	if (!clone) {
387f6176473STiezhu Yang 		ret = -ENOMEM;
388f6176473STiezhu Yang 		goto release_acl;
389f6176473STiezhu Yang 	}
390bce8d112SJaegeuk Kim 
391272e083fSChao Yu 	ret = f2fs_acl_create_masq(clone, mode);
39283dfe53cSChao Yu 	if (ret < 0)
393f6176473STiezhu Yang 		goto release_clone;
394bce8d112SJaegeuk Kim 
395272e083fSChao Yu 	if (ret == 0)
396272e083fSChao Yu 		posix_acl_release(clone);
397272e083fSChao Yu 	else
398272e083fSChao Yu 		*acl = clone;
399bce8d112SJaegeuk Kim 
400272e083fSChao Yu 	if (!S_ISDIR(*mode))
401bce8d112SJaegeuk Kim 		posix_acl_release(p);
402272e083fSChao Yu 	else
403bce8d112SJaegeuk Kim 		*default_acl = p;
404bce8d112SJaegeuk Kim 
405bce8d112SJaegeuk Kim 	return 0;
40683dfe53cSChao Yu 
407f6176473STiezhu Yang release_clone:
408272e083fSChao Yu 	posix_acl_release(clone);
409f6176473STiezhu Yang release_acl:
41083dfe53cSChao Yu 	posix_acl_release(p);
411f6176473STiezhu Yang 	return ret;
412bce8d112SJaegeuk Kim }
413bce8d112SJaegeuk Kim 
f2fs_init_acl(struct inode * inode,struct inode * dir,struct page * ipage,struct page * dpage)414bce8d112SJaegeuk Kim int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
415bce8d112SJaegeuk Kim 							struct page *dpage)
416bce8d112SJaegeuk Kim {
417bce8d112SJaegeuk Kim 	struct posix_acl *default_acl = NULL, *acl = NULL;
418beb78181SZhang Qilong 	int error;
419af48b85bSJaegeuk Kim 
420bce8d112SJaegeuk Kim 	error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
421af48b85bSJaegeuk Kim 	if (error)
422af48b85bSJaegeuk Kim 		return error;
423b8b60e1aSJaegeuk Kim 
4247c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
425205b9822SJaegeuk Kim 
426a6dda0e6SChristoph Hellwig 	if (default_acl) {
427984fc4e7SChao Yu 		error = __f2fs_set_acl(NULL, inode, ACL_TYPE_DEFAULT, default_acl,
428a6dda0e6SChristoph Hellwig 				       ipage);
429a6dda0e6SChristoph Hellwig 		posix_acl_release(default_acl);
430313ed62aSChengguang Xu 	} else {
431313ed62aSChengguang Xu 		inode->i_default_acl = NULL;
432af48b85bSJaegeuk Kim 	}
433af48b85bSJaegeuk Kim 	if (acl) {
4343b6709b7SKinglong Mee 		if (!error)
435984fc4e7SChao Yu 			error = __f2fs_set_acl(NULL, inode, ACL_TYPE_ACCESS, acl,
436a6dda0e6SChristoph Hellwig 					       ipage);
437af48b85bSJaegeuk Kim 		posix_acl_release(acl);
438313ed62aSChengguang Xu 	} else {
439313ed62aSChengguang Xu 		inode->i_acl = NULL;
440a6dda0e6SChristoph Hellwig 	}
441a6dda0e6SChristoph Hellwig 
442af48b85bSJaegeuk Kim 	return error;
443af48b85bSJaegeuk Kim }
444