1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
8 * Lots of code in this file is copy from linux/fs/ext3/acl.c.
9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
17 #include <cluster/masklog.h>
31 * Convert from xattr value to acl struct.
33 static struct posix_acl
*ocfs2_acl_from_xattr(const void *value
, size_t size
)
36 struct posix_acl
*acl
;
40 if (size
< sizeof(struct posix_acl_entry
))
41 return ERR_PTR(-EINVAL
);
43 count
= size
/ sizeof(struct posix_acl_entry
);
45 acl
= posix_acl_alloc(count
, GFP_NOFS
);
47 return ERR_PTR(-ENOMEM
);
48 for (n
= 0; n
< count
; n
++) {
49 struct ocfs2_acl_entry
*entry
=
50 (struct ocfs2_acl_entry
*)value
;
52 acl
->a_entries
[n
].e_tag
= le16_to_cpu(entry
->e_tag
);
53 acl
->a_entries
[n
].e_perm
= le16_to_cpu(entry
->e_perm
);
54 switch(acl
->a_entries
[n
].e_tag
) {
56 acl
->a_entries
[n
].e_uid
=
57 make_kuid(&init_user_ns
,
58 le32_to_cpu(entry
->e_id
));
61 acl
->a_entries
[n
].e_gid
=
62 make_kgid(&init_user_ns
,
63 le32_to_cpu(entry
->e_id
));
68 value
+= sizeof(struct posix_acl_entry
);
75 * Convert acl struct to xattr value.
77 static void *ocfs2_acl_to_xattr(const struct posix_acl
*acl
, size_t *size
)
79 struct ocfs2_acl_entry
*entry
= NULL
;
83 *size
= acl
->a_count
* sizeof(struct posix_acl_entry
);
85 ocfs2_acl
= kmalloc(*size
, GFP_NOFS
);
87 return ERR_PTR(-ENOMEM
);
89 entry
= (struct ocfs2_acl_entry
*)ocfs2_acl
;
90 for (n
= 0; n
< acl
->a_count
; n
++, entry
++) {
91 entry
->e_tag
= cpu_to_le16(acl
->a_entries
[n
].e_tag
);
92 entry
->e_perm
= cpu_to_le16(acl
->a_entries
[n
].e_perm
);
93 switch(acl
->a_entries
[n
].e_tag
) {
95 entry
->e_id
= cpu_to_le32(
96 from_kuid(&init_user_ns
,
97 acl
->a_entries
[n
].e_uid
));
100 entry
->e_id
= cpu_to_le32(
101 from_kgid(&init_user_ns
,
102 acl
->a_entries
[n
].e_gid
));
105 entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
112 static struct posix_acl
*ocfs2_get_acl_nolock(struct inode
*inode
,
114 struct buffer_head
*di_bh
)
118 struct posix_acl
*acl
;
122 case ACL_TYPE_ACCESS
:
123 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
;
125 case ACL_TYPE_DEFAULT
:
126 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
129 return ERR_PTR(-EINVAL
);
132 retval
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
, "", NULL
, 0);
134 value
= kmalloc(retval
, GFP_NOFS
);
136 return ERR_PTR(-ENOMEM
);
137 retval
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
142 acl
= ocfs2_acl_from_xattr(value
, retval
);
143 else if (retval
== -ENODATA
|| retval
== 0)
146 acl
= ERR_PTR(retval
);
154 * Helper function to set i_mode in memory and disk. Some call paths
155 * will not have di_bh or a journal handle to pass, in which case it
156 * will create it's own.
158 static int ocfs2_acl_set_mode(struct inode
*inode
, struct buffer_head
*di_bh
,
159 handle_t
*handle
, umode_t new_mode
)
161 int ret
, commit_handle
= 0;
162 struct ocfs2_dinode
*di
;
165 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
173 if (handle
== NULL
) {
174 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
),
175 OCFS2_INODE_UPDATE_CREDITS
);
176 if (IS_ERR(handle
)) {
177 ret
= PTR_ERR(handle
);
185 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
186 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
187 OCFS2_JOURNAL_ACCESS_WRITE
);
193 inode
->i_mode
= new_mode
;
194 inode_set_ctime_current(inode
);
195 di
->i_mode
= cpu_to_le16(inode
->i_mode
);
196 di
->i_ctime
= cpu_to_le64(inode_get_ctime_sec(inode
));
197 di
->i_ctime_nsec
= cpu_to_le32(inode_get_ctime_nsec(inode
));
198 ocfs2_update_inode_fsync_trans(handle
, inode
, 0);
200 ocfs2_journal_dirty(handle
, di_bh
);
204 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
212 * Set the access or default ACL of an inode.
214 static int ocfs2_set_acl(handle_t
*handle
,
216 struct buffer_head
*di_bh
,
218 struct posix_acl
*acl
,
219 struct ocfs2_alloc_context
*meta_ac
,
220 struct ocfs2_alloc_context
*data_ac
)
227 if (S_ISLNK(inode
->i_mode
))
231 case ACL_TYPE_ACCESS
:
232 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
;
234 case ACL_TYPE_DEFAULT
:
235 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
236 if (!S_ISDIR(inode
->i_mode
))
237 return acl
? -EACCES
: 0;
244 value
= ocfs2_acl_to_xattr(acl
, &size
);
246 return (int)PTR_ERR(value
);
250 ret
= ocfs2_xattr_set_handle(handle
, inode
, di_bh
, name_index
,
254 ret
= ocfs2_xattr_set(inode
, name_index
, "", value
, size
, 0);
258 set_cached_acl(inode
, type
, acl
);
263 int ocfs2_iop_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
264 struct posix_acl
*acl
, int type
)
266 struct buffer_head
*bh
= NULL
;
267 int status
, had_lock
;
268 struct ocfs2_lock_holder oh
;
269 struct inode
*inode
= d_inode(dentry
);
271 had_lock
= ocfs2_inode_lock_tracker(inode
, &bh
, 1, &oh
);
274 if (type
== ACL_TYPE_ACCESS
&& acl
) {
277 status
= posix_acl_update_mode(&nop_mnt_idmap
, inode
, &mode
,
282 status
= ocfs2_acl_set_mode(inode
, bh
, NULL
, mode
);
286 status
= ocfs2_set_acl(NULL
, inode
, bh
, type
, acl
, NULL
, NULL
);
288 ocfs2_inode_unlock_tracker(inode
, 1, &oh
, had_lock
);
293 struct posix_acl
*ocfs2_iop_get_acl(struct inode
*inode
, int type
, bool rcu
)
295 struct ocfs2_super
*osb
;
296 struct buffer_head
*di_bh
= NULL
;
297 struct posix_acl
*acl
;
299 struct ocfs2_lock_holder oh
;
302 return ERR_PTR(-ECHILD
);
304 osb
= OCFS2_SB(inode
->i_sb
);
305 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
308 had_lock
= ocfs2_inode_lock_tracker(inode
, &di_bh
, 0, &oh
);
310 return ERR_PTR(had_lock
);
312 down_read(&OCFS2_I(inode
)->ip_xattr_sem
);
313 acl
= ocfs2_get_acl_nolock(inode
, type
, di_bh
);
314 up_read(&OCFS2_I(inode
)->ip_xattr_sem
);
316 ocfs2_inode_unlock_tracker(inode
, 0, &oh
, had_lock
);
321 int ocfs2_acl_chmod(struct inode
*inode
, struct buffer_head
*bh
)
323 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
324 struct posix_acl
*acl
;
327 if (S_ISLNK(inode
->i_mode
))
330 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
333 down_read(&OCFS2_I(inode
)->ip_xattr_sem
);
334 acl
= ocfs2_get_acl_nolock(inode
, ACL_TYPE_ACCESS
, bh
);
335 up_read(&OCFS2_I(inode
)->ip_xattr_sem
);
336 if (IS_ERR_OR_NULL(acl
))
337 return PTR_ERR_OR_ZERO(acl
);
338 ret
= __posix_acl_chmod(&acl
, GFP_KERNEL
, inode
->i_mode
);
341 ret
= ocfs2_set_acl(NULL
, inode
, NULL
, ACL_TYPE_ACCESS
,
343 posix_acl_release(acl
);
348 * Initialize the ACLs of a new inode. If parent directory has default ACL,
349 * then clone to new inode. Called from ocfs2_mknod.
351 int ocfs2_init_acl(handle_t
*handle
,
354 struct buffer_head
*di_bh
,
355 struct buffer_head
*dir_bh
,
356 struct ocfs2_alloc_context
*meta_ac
,
357 struct ocfs2_alloc_context
*data_ac
)
359 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
360 struct posix_acl
*acl
= NULL
;
364 if (!S_ISLNK(inode
->i_mode
)) {
365 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) {
366 down_read(&OCFS2_I(dir
)->ip_xattr_sem
);
367 acl
= ocfs2_get_acl_nolock(dir
, ACL_TYPE_DEFAULT
,
369 up_read(&OCFS2_I(dir
)->ip_xattr_sem
);
374 mode
= inode
->i_mode
& ~current_umask();
375 ret
= ocfs2_acl_set_mode(inode
, di_bh
, handle
, mode
);
382 if ((osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) && acl
) {
383 if (S_ISDIR(inode
->i_mode
)) {
384 ret
= ocfs2_set_acl(handle
, inode
, di_bh
,
385 ACL_TYPE_DEFAULT
, acl
,
390 mode
= inode
->i_mode
;
391 ret
= __posix_acl_create(&acl
, GFP_NOFS
, &mode
);
395 ret2
= ocfs2_acl_set_mode(inode
, di_bh
, handle
, mode
);
402 ret
= ocfs2_set_acl(handle
, inode
,
403 di_bh
, ACL_TYPE_ACCESS
,
404 acl
, meta_ac
, data_ac
);
408 posix_acl_release(acl
);