xref: /openbmc/linux/fs/ocfs2/acl.c (revision 57904291176fa16a981cefca5cbe1a0b50196792)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fa60ce2cSMasahiro Yamada /*
3929fb014STiger Yang  * acl.c
4929fb014STiger Yang  *
5929fb014STiger Yang  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
6929fb014STiger Yang  *
7929fb014STiger Yang  * CREDITS:
8929fb014STiger Yang  * Lots of code in this file is copy from linux/fs/ext3/acl.c.
9929fb014STiger Yang  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
10929fb014STiger Yang  */
11929fb014STiger Yang 
12929fb014STiger Yang #include <linux/init.h>
13929fb014STiger Yang #include <linux/module.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15929fb014STiger Yang #include <linux/string.h>
16929fb014STiger Yang 
17929fb014STiger Yang #include <cluster/masklog.h>
18929fb014STiger Yang 
19929fb014STiger Yang #include "ocfs2.h"
20929fb014STiger Yang #include "alloc.h"
21929fb014STiger Yang #include "dlmglue.h"
22929fb014STiger Yang #include "file.h"
23fcefd25aSMark Fasheh #include "inode.h"
24fcefd25aSMark Fasheh #include "journal.h"
25929fb014STiger Yang #include "ocfs2_fs.h"
26929fb014STiger Yang 
27929fb014STiger Yang #include "xattr.h"
28929fb014STiger Yang #include "acl.h"
29929fb014STiger Yang 
30929fb014STiger Yang /*
31929fb014STiger Yang  * Convert from xattr value to acl struct.
32929fb014STiger Yang  */
ocfs2_acl_from_xattr(const void * value,size_t size)33929fb014STiger Yang static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
34929fb014STiger Yang {
35929fb014STiger Yang 	int n, count;
36929fb014STiger Yang 	struct posix_acl *acl;
37929fb014STiger Yang 
38929fb014STiger Yang 	if (!value)
39929fb014STiger Yang 		return NULL;
40929fb014STiger Yang 	if (size < sizeof(struct posix_acl_entry))
41929fb014STiger Yang 		return ERR_PTR(-EINVAL);
42929fb014STiger Yang 
43929fb014STiger Yang 	count = size / sizeof(struct posix_acl_entry);
44929fb014STiger Yang 
45929fb014STiger Yang 	acl = posix_acl_alloc(count, GFP_NOFS);
46929fb014STiger Yang 	if (!acl)
47929fb014STiger Yang 		return ERR_PTR(-ENOMEM);
48929fb014STiger Yang 	for (n = 0; n < count; n++) {
49929fb014STiger Yang 		struct ocfs2_acl_entry *entry =
50929fb014STiger Yang 			(struct ocfs2_acl_entry *)value;
51929fb014STiger Yang 
52929fb014STiger Yang 		acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
53929fb014STiger Yang 		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
549522751cSEric W. Biederman 		switch(acl->a_entries[n].e_tag) {
559522751cSEric W. Biederman 		case ACL_USER:
569522751cSEric W. Biederman 			acl->a_entries[n].e_uid =
579522751cSEric W. Biederman 				make_kuid(&init_user_ns,
589522751cSEric W. Biederman 					  le32_to_cpu(entry->e_id));
599522751cSEric W. Biederman 			break;
609522751cSEric W. Biederman 		case ACL_GROUP:
619522751cSEric W. Biederman 			acl->a_entries[n].e_gid =
629522751cSEric W. Biederman 				make_kgid(&init_user_ns,
639522751cSEric W. Biederman 					  le32_to_cpu(entry->e_id));
649522751cSEric W. Biederman 			break;
659522751cSEric W. Biederman 		default:
669522751cSEric W. Biederman 			break;
679522751cSEric W. Biederman 		}
68929fb014STiger Yang 		value += sizeof(struct posix_acl_entry);
69929fb014STiger Yang 
70929fb014STiger Yang 	}
71929fb014STiger Yang 	return acl;
72929fb014STiger Yang }
73929fb014STiger Yang 
74929fb014STiger Yang /*
75929fb014STiger Yang  * Convert acl struct to xattr value.
76929fb014STiger Yang  */
ocfs2_acl_to_xattr(const struct posix_acl * acl,size_t * size)77929fb014STiger Yang static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
78929fb014STiger Yang {
79929fb014STiger Yang 	struct ocfs2_acl_entry *entry = NULL;
80929fb014STiger Yang 	char *ocfs2_acl;
81929fb014STiger Yang 	size_t n;
82929fb014STiger Yang 
83929fb014STiger Yang 	*size = acl->a_count * sizeof(struct posix_acl_entry);
84929fb014STiger Yang 
85929fb014STiger Yang 	ocfs2_acl = kmalloc(*size, GFP_NOFS);
86929fb014STiger Yang 	if (!ocfs2_acl)
87929fb014STiger Yang 		return ERR_PTR(-ENOMEM);
88929fb014STiger Yang 
89929fb014STiger Yang 	entry = (struct ocfs2_acl_entry *)ocfs2_acl;
90929fb014STiger Yang 	for (n = 0; n < acl->a_count; n++, entry++) {
91929fb014STiger Yang 		entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
92929fb014STiger Yang 		entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
939522751cSEric W. Biederman 		switch(acl->a_entries[n].e_tag) {
949522751cSEric W. Biederman 		case ACL_USER:
959522751cSEric W. Biederman 			entry->e_id = cpu_to_le32(
969522751cSEric W. Biederman 				from_kuid(&init_user_ns,
979522751cSEric W. Biederman 					  acl->a_entries[n].e_uid));
989522751cSEric W. Biederman 			break;
999522751cSEric W. Biederman 		case ACL_GROUP:
1009522751cSEric W. Biederman 			entry->e_id = cpu_to_le32(
1019522751cSEric W. Biederman 				from_kgid(&init_user_ns,
1029522751cSEric W. Biederman 					  acl->a_entries[n].e_gid));
1039522751cSEric W. Biederman 			break;
1049522751cSEric W. Biederman 		default:
1059522751cSEric W. Biederman 			entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1069522751cSEric W. Biederman 			break;
1079522751cSEric W. Biederman 		}
108929fb014STiger Yang 	}
109929fb014STiger Yang 	return ocfs2_acl;
110929fb014STiger Yang }
111929fb014STiger Yang 
ocfs2_get_acl_nolock(struct inode * inode,int type,struct buffer_head * di_bh)112929fb014STiger Yang static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
113929fb014STiger Yang 					      int type,
114929fb014STiger Yang 					      struct buffer_head *di_bh)
115929fb014STiger Yang {
116929fb014STiger Yang 	int name_index;
117929fb014STiger Yang 	char *value = NULL;
118929fb014STiger Yang 	struct posix_acl *acl;
119929fb014STiger Yang 	int retval;
120929fb014STiger Yang 
121929fb014STiger Yang 	switch (type) {
122929fb014STiger Yang 	case ACL_TYPE_ACCESS:
123929fb014STiger Yang 		name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
124929fb014STiger Yang 		break;
125929fb014STiger Yang 	case ACL_TYPE_DEFAULT:
126929fb014STiger Yang 		name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
127929fb014STiger Yang 		break;
128929fb014STiger Yang 	default:
129929fb014STiger Yang 		return ERR_PTR(-EINVAL);
130929fb014STiger Yang 	}
131929fb014STiger Yang 
132929fb014STiger Yang 	retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
133929fb014STiger Yang 	if (retval > 0) {
134929fb014STiger Yang 		value = kmalloc(retval, GFP_NOFS);
135929fb014STiger Yang 		if (!value)
136929fb014STiger Yang 			return ERR_PTR(-ENOMEM);
137929fb014STiger Yang 		retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
138929fb014STiger Yang 						"", value, retval);
139929fb014STiger Yang 	}
140929fb014STiger Yang 
141929fb014STiger Yang 	if (retval > 0)
142929fb014STiger Yang 		acl = ocfs2_acl_from_xattr(value, retval);
143929fb014STiger Yang 	else if (retval == -ENODATA || retval == 0)
144929fb014STiger Yang 		acl = NULL;
145929fb014STiger Yang 	else
146929fb014STiger Yang 		acl = ERR_PTR(retval);
147929fb014STiger Yang 
148929fb014STiger Yang 	kfree(value);
149929fb014STiger Yang 
150929fb014STiger Yang 	return acl;
151929fb014STiger Yang }
152929fb014STiger Yang 
153929fb014STiger Yang /*
154fcefd25aSMark Fasheh  * Helper function to set i_mode in memory and disk. Some call paths
155fcefd25aSMark Fasheh  * will not have di_bh or a journal handle to pass, in which case it
156fcefd25aSMark Fasheh  * will create it's own.
157fcefd25aSMark Fasheh  */
ocfs2_acl_set_mode(struct inode * inode,struct buffer_head * di_bh,handle_t * handle,umode_t new_mode)158fcefd25aSMark Fasheh static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
159fcefd25aSMark Fasheh 			      handle_t *handle, umode_t new_mode)
160fcefd25aSMark Fasheh {
161fcefd25aSMark Fasheh 	int ret, commit_handle = 0;
162fcefd25aSMark Fasheh 	struct ocfs2_dinode *di;
163fcefd25aSMark Fasheh 
164fcefd25aSMark Fasheh 	if (di_bh == NULL) {
165fcefd25aSMark Fasheh 		ret = ocfs2_read_inode_block(inode, &di_bh);
166fcefd25aSMark Fasheh 		if (ret) {
167fcefd25aSMark Fasheh 			mlog_errno(ret);
168fcefd25aSMark Fasheh 			goto out;
169fcefd25aSMark Fasheh 		}
170fcefd25aSMark Fasheh 	} else
171fcefd25aSMark Fasheh 		get_bh(di_bh);
172fcefd25aSMark Fasheh 
173fcefd25aSMark Fasheh 	if (handle == NULL) {
174fcefd25aSMark Fasheh 		handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
175fcefd25aSMark Fasheh 					   OCFS2_INODE_UPDATE_CREDITS);
176fcefd25aSMark Fasheh 		if (IS_ERR(handle)) {
177fcefd25aSMark Fasheh 			ret = PTR_ERR(handle);
178fcefd25aSMark Fasheh 			mlog_errno(ret);
179fcefd25aSMark Fasheh 			goto out_brelse;
180fcefd25aSMark Fasheh 		}
181fcefd25aSMark Fasheh 
182fcefd25aSMark Fasheh 		commit_handle = 1;
183fcefd25aSMark Fasheh 	}
184fcefd25aSMark Fasheh 
185fcefd25aSMark Fasheh 	di = (struct ocfs2_dinode *)di_bh->b_data;
186fcefd25aSMark Fasheh 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
187fcefd25aSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
188fcefd25aSMark Fasheh 	if (ret) {
189fcefd25aSMark Fasheh 		mlog_errno(ret);
190fcefd25aSMark Fasheh 		goto out_commit;
191fcefd25aSMark Fasheh 	}
192fcefd25aSMark Fasheh 
193fcefd25aSMark Fasheh 	inode->i_mode = new_mode;
1946861de97SJeff Layton 	inode_set_ctime_current(inode);
195fcefd25aSMark Fasheh 	di->i_mode = cpu_to_le16(inode->i_mode);
196*10fc3a18SJeff Layton 	di->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
197*10fc3a18SJeff Layton 	di->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
1986fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
199fcefd25aSMark Fasheh 
200fcefd25aSMark Fasheh 	ocfs2_journal_dirty(handle, di_bh);
201fcefd25aSMark Fasheh 
202fcefd25aSMark Fasheh out_commit:
203fcefd25aSMark Fasheh 	if (commit_handle)
204fcefd25aSMark Fasheh 		ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
205fcefd25aSMark Fasheh out_brelse:
206fcefd25aSMark Fasheh 	brelse(di_bh);
207fcefd25aSMark Fasheh out:
208fcefd25aSMark Fasheh 	return ret;
209fcefd25aSMark Fasheh }
210fcefd25aSMark Fasheh 
211fcefd25aSMark Fasheh /*
212929fb014STiger Yang  * Set the access or default ACL of an inode.
213929fb014STiger Yang  */
ocfs2_set_acl(handle_t * handle,struct inode * inode,struct buffer_head * di_bh,int type,struct posix_acl * acl,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac)21401ffb56bSJan Kara static int ocfs2_set_acl(handle_t *handle,
215929fb014STiger Yang 			 struct inode *inode,
216929fb014STiger Yang 			 struct buffer_head *di_bh,
217929fb014STiger Yang 			 int type,
218929fb014STiger Yang 			 struct posix_acl *acl,
219929fb014STiger Yang 			 struct ocfs2_alloc_context *meta_ac,
220929fb014STiger Yang 			 struct ocfs2_alloc_context *data_ac)
221929fb014STiger Yang {
222929fb014STiger Yang 	int name_index;
223929fb014STiger Yang 	void *value = NULL;
224929fb014STiger Yang 	size_t size = 0;
225929fb014STiger Yang 	int ret;
226929fb014STiger Yang 
227929fb014STiger Yang 	if (S_ISLNK(inode->i_mode))
228929fb014STiger Yang 		return -EOPNOTSUPP;
229929fb014STiger Yang 
230929fb014STiger Yang 	switch (type) {
231929fb014STiger Yang 	case ACL_TYPE_ACCESS:
232929fb014STiger Yang 		name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
233929fb014STiger Yang 		break;
234929fb014STiger Yang 	case ACL_TYPE_DEFAULT:
235929fb014STiger Yang 		name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
236929fb014STiger Yang 		if (!S_ISDIR(inode->i_mode))
237929fb014STiger Yang 			return acl ? -EACCES : 0;
238929fb014STiger Yang 		break;
239929fb014STiger Yang 	default:
240929fb014STiger Yang 		return -EINVAL;
241929fb014STiger Yang 	}
242929fb014STiger Yang 
243929fb014STiger Yang 	if (acl) {
244929fb014STiger Yang 		value = ocfs2_acl_to_xattr(acl, &size);
245929fb014STiger Yang 		if (IS_ERR(value))
246929fb014STiger Yang 			return (int)PTR_ERR(value);
247929fb014STiger Yang 	}
248929fb014STiger Yang 
249929fb014STiger Yang 	if (handle)
250929fb014STiger Yang 		ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
251929fb014STiger Yang 					     "", value, size, 0,
252929fb014STiger Yang 					     meta_ac, data_ac);
253929fb014STiger Yang 	else
254929fb014STiger Yang 		ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
255929fb014STiger Yang 
256929fb014STiger Yang 	kfree(value);
257504ec37dSGang He 	if (!ret)
258504ec37dSGang He 		set_cached_acl(inode, type, acl);
259929fb014STiger Yang 
260929fb014STiger Yang 	return ret;
261929fb014STiger Yang }
262929fb014STiger Yang 
ocfs2_iop_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * acl,int type)26313e83a49SChristian Brauner int ocfs2_iop_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
264549c7297SChristian Brauner 		      struct posix_acl *acl, int type)
265702e5bc6SChristoph Hellwig {
266743b5f14STariq Saeed 	struct buffer_head *bh = NULL;
267b891fa50SEric Ren 	int status, had_lock;
268b891fa50SEric Ren 	struct ocfs2_lock_holder oh;
269138060baSChristian Brauner 	struct inode *inode = d_inode(dentry);
270743b5f14STariq Saeed 
271b891fa50SEric Ren 	had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
272b891fa50SEric Ren 	if (had_lock < 0)
273b891fa50SEric Ren 		return had_lock;
27419ec8e48SJan Kara 	if (type == ACL_TYPE_ACCESS && acl) {
27519ec8e48SJan Kara 		umode_t mode;
27619ec8e48SJan Kara 
277700b7940SChristian Brauner 		status = posix_acl_update_mode(&nop_mnt_idmap, inode, &mode,
278e65ce2a5SChristian Brauner 					       &acl);
27919ec8e48SJan Kara 		if (status)
28019ec8e48SJan Kara 			goto unlock;
28119ec8e48SJan Kara 
28219ec8e48SJan Kara 		status = ocfs2_acl_set_mode(inode, bh, NULL, mode);
28319ec8e48SJan Kara 		if (status)
28419ec8e48SJan Kara 			goto unlock;
28519ec8e48SJan Kara 	}
286743b5f14STariq Saeed 	status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
28719ec8e48SJan Kara unlock:
288b891fa50SEric Ren 	ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
289743b5f14STariq Saeed 	brelse(bh);
290743b5f14STariq Saeed 	return status;
291702e5bc6SChristoph Hellwig }
292702e5bc6SChristoph Hellwig 
ocfs2_iop_get_acl(struct inode * inode,int type,bool rcu)2930cad6246SMiklos Szeredi struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type, bool rcu)
29423fc2702STiger Yang {
295b74c79e9SNick Piggin 	struct ocfs2_super *osb;
296845b6cf3SJiaju Zhang 	struct buffer_head *di_bh = NULL;
297845b6cf3SJiaju Zhang 	struct posix_acl *acl;
298b891fa50SEric Ren 	int had_lock;
299b891fa50SEric Ren 	struct ocfs2_lock_holder oh;
30023fc2702STiger Yang 
3010cad6246SMiklos Szeredi 	if (rcu)
3020cad6246SMiklos Szeredi 		return ERR_PTR(-ECHILD);
3030cad6246SMiklos Szeredi 
304b74c79e9SNick Piggin 	osb = OCFS2_SB(inode->i_sb);
305845b6cf3SJiaju Zhang 	if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
3064e34e719SChristoph Hellwig 		return NULL;
307b891fa50SEric Ren 
308b891fa50SEric Ren 	had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
309b891fa50SEric Ren 	if (had_lock < 0)
310b891fa50SEric Ren 		return ERR_PTR(had_lock);
311845b6cf3SJiaju Zhang 
31216c8d569Spiaojun 	down_read(&OCFS2_I(inode)->ip_xattr_sem);
3134e34e719SChristoph Hellwig 	acl = ocfs2_get_acl_nolock(inode, type, di_bh);
31416c8d569Spiaojun 	up_read(&OCFS2_I(inode)->ip_xattr_sem);
315845b6cf3SJiaju Zhang 
316b891fa50SEric Ren 	ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
317845b6cf3SJiaju Zhang 	brelse(di_bh);
3184e34e719SChristoph Hellwig 	return acl;
31923fc2702STiger Yang }
3205ee0fbd5SJunxiao Bi 
ocfs2_acl_chmod(struct inode * inode,struct buffer_head * bh)3215ee0fbd5SJunxiao Bi int ocfs2_acl_chmod(struct inode *inode, struct buffer_head *bh)
3225ee0fbd5SJunxiao Bi {
3235ee0fbd5SJunxiao Bi 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3245ee0fbd5SJunxiao Bi 	struct posix_acl *acl;
3255ee0fbd5SJunxiao Bi 	int ret;
3265ee0fbd5SJunxiao Bi 
3275ee0fbd5SJunxiao Bi 	if (S_ISLNK(inode->i_mode))
3285ee0fbd5SJunxiao Bi 		return -EOPNOTSUPP;
3295ee0fbd5SJunxiao Bi 
3305ee0fbd5SJunxiao Bi 	if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
3315ee0fbd5SJunxiao Bi 		return 0;
3325ee0fbd5SJunxiao Bi 
33316c8d569Spiaojun 	down_read(&OCFS2_I(inode)->ip_xattr_sem);
3345ee0fbd5SJunxiao Bi 	acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, bh);
33516c8d569Spiaojun 	up_read(&OCFS2_I(inode)->ip_xattr_sem);
336188c523eSDing Xiang 	if (IS_ERR_OR_NULL(acl))
337188c523eSDing Xiang 		return PTR_ERR_OR_ZERO(acl);
3385ee0fbd5SJunxiao Bi 	ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
3395ee0fbd5SJunxiao Bi 	if (ret)
3405ee0fbd5SJunxiao Bi 		return ret;
3415ee0fbd5SJunxiao Bi 	ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
3425ee0fbd5SJunxiao Bi 			    acl, NULL, NULL);
3435ee0fbd5SJunxiao Bi 	posix_acl_release(acl);
3445ee0fbd5SJunxiao Bi 	return ret;
3455ee0fbd5SJunxiao Bi }
346c25a1e06SJunxiao Bi 
347c25a1e06SJunxiao Bi /*
348c25a1e06SJunxiao Bi  * Initialize the ACLs of a new inode. If parent directory has default ACL,
349c25a1e06SJunxiao Bi  * then clone to new inode. Called from ocfs2_mknod.
350c25a1e06SJunxiao Bi  */
ocfs2_init_acl(handle_t * handle,struct inode * inode,struct inode * dir,struct buffer_head * di_bh,struct buffer_head * dir_bh,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac)351c25a1e06SJunxiao Bi int ocfs2_init_acl(handle_t *handle,
352c25a1e06SJunxiao Bi 		   struct inode *inode,
353c25a1e06SJunxiao Bi 		   struct inode *dir,
354c25a1e06SJunxiao Bi 		   struct buffer_head *di_bh,
355c25a1e06SJunxiao Bi 		   struct buffer_head *dir_bh,
356c25a1e06SJunxiao Bi 		   struct ocfs2_alloc_context *meta_ac,
357c25a1e06SJunxiao Bi 		   struct ocfs2_alloc_context *data_ac)
358c25a1e06SJunxiao Bi {
359c25a1e06SJunxiao Bi 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
360c25a1e06SJunxiao Bi 	struct posix_acl *acl = NULL;
361c25a1e06SJunxiao Bi 	int ret = 0, ret2;
362c25a1e06SJunxiao Bi 	umode_t mode;
363c25a1e06SJunxiao Bi 
364c25a1e06SJunxiao Bi 	if (!S_ISLNK(inode->i_mode)) {
365c25a1e06SJunxiao Bi 		if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
36616c8d569Spiaojun 			down_read(&OCFS2_I(dir)->ip_xattr_sem);
367c25a1e06SJunxiao Bi 			acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
368c25a1e06SJunxiao Bi 						   dir_bh);
36916c8d569Spiaojun 			up_read(&OCFS2_I(dir)->ip_xattr_sem);
370c25a1e06SJunxiao Bi 			if (IS_ERR(acl))
371c25a1e06SJunxiao Bi 				return PTR_ERR(acl);
372c25a1e06SJunxiao Bi 		}
373c25a1e06SJunxiao Bi 		if (!acl) {
374c25a1e06SJunxiao Bi 			mode = inode->i_mode & ~current_umask();
375c25a1e06SJunxiao Bi 			ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
376c25a1e06SJunxiao Bi 			if (ret) {
377c25a1e06SJunxiao Bi 				mlog_errno(ret);
378c25a1e06SJunxiao Bi 				goto cleanup;
379c25a1e06SJunxiao Bi 			}
380c25a1e06SJunxiao Bi 		}
381c25a1e06SJunxiao Bi 	}
382c25a1e06SJunxiao Bi 	if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
383c25a1e06SJunxiao Bi 		if (S_ISDIR(inode->i_mode)) {
384c25a1e06SJunxiao Bi 			ret = ocfs2_set_acl(handle, inode, di_bh,
385c25a1e06SJunxiao Bi 					    ACL_TYPE_DEFAULT, acl,
386c25a1e06SJunxiao Bi 					    meta_ac, data_ac);
387c25a1e06SJunxiao Bi 			if (ret)
388c25a1e06SJunxiao Bi 				goto cleanup;
389c25a1e06SJunxiao Bi 		}
390c25a1e06SJunxiao Bi 		mode = inode->i_mode;
391c25a1e06SJunxiao Bi 		ret = __posix_acl_create(&acl, GFP_NOFS, &mode);
392c25a1e06SJunxiao Bi 		if (ret < 0)
393c25a1e06SJunxiao Bi 			return ret;
394c25a1e06SJunxiao Bi 
395c25a1e06SJunxiao Bi 		ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
396c25a1e06SJunxiao Bi 		if (ret2) {
397c25a1e06SJunxiao Bi 			mlog_errno(ret2);
398c25a1e06SJunxiao Bi 			ret = ret2;
399c25a1e06SJunxiao Bi 			goto cleanup;
400c25a1e06SJunxiao Bi 		}
401c25a1e06SJunxiao Bi 		if (ret > 0) {
402c25a1e06SJunxiao Bi 			ret = ocfs2_set_acl(handle, inode,
403c25a1e06SJunxiao Bi 					    di_bh, ACL_TYPE_ACCESS,
404c25a1e06SJunxiao Bi 					    acl, meta_ac, data_ac);
405c25a1e06SJunxiao Bi 		}
406c25a1e06SJunxiao Bi 	}
407c25a1e06SJunxiao Bi cleanup:
408c25a1e06SJunxiao Bi 	posix_acl_release(acl);
409c25a1e06SJunxiao Bi 	return ret;
410c25a1e06SJunxiao Bi }
411