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->i_ctime = current_time(inode);
195 di->i_mode = cpu_to_le16(inode->i_mode);
196 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
197 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
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 user_namespace *mnt_userns, struct inode *inode,
264 struct posix_acl *acl, int type)
266 struct buffer_head *bh = NULL;
267 int status, had_lock;
268 struct ocfs2_lock_holder oh;
270 had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
273 if (type == ACL_TYPE_ACCESS && acl) {
276 status = posix_acl_update_mode(&init_user_ns, inode, &mode,
281 status = ocfs2_acl_set_mode(inode, bh, NULL, mode);
285 status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
287 ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
292 struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type, bool rcu)
294 struct ocfs2_super *osb;
295 struct buffer_head *di_bh = NULL;
296 struct posix_acl *acl;
298 struct ocfs2_lock_holder oh;
301 return ERR_PTR(-ECHILD);
303 osb = OCFS2_SB(inode->i_sb);
304 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
307 had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
309 return ERR_PTR(had_lock);
311 down_read(&OCFS2_I(inode)->ip_xattr_sem);
312 acl = ocfs2_get_acl_nolock(inode, type, di_bh);
313 up_read(&OCFS2_I(inode)->ip_xattr_sem);
315 ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
320 int ocfs2_acl_chmod(struct inode *inode, struct buffer_head *bh)
322 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
323 struct posix_acl *acl;
326 if (S_ISLNK(inode->i_mode))
329 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
332 down_read(&OCFS2_I(inode)->ip_xattr_sem);
333 acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, bh);
334 up_read(&OCFS2_I(inode)->ip_xattr_sem);
335 if (IS_ERR_OR_NULL(acl))
336 return PTR_ERR_OR_ZERO(acl);
337 ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
340 ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
342 posix_acl_release(acl);
347 * Initialize the ACLs of a new inode. If parent directory has default ACL,
348 * then clone to new inode. Called from ocfs2_mknod.
350 int ocfs2_init_acl(handle_t *handle,
353 struct buffer_head *di_bh,
354 struct buffer_head *dir_bh,
355 struct ocfs2_alloc_context *meta_ac,
356 struct ocfs2_alloc_context *data_ac)
358 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
359 struct posix_acl *acl = NULL;
363 if (!S_ISLNK(inode->i_mode)) {
364 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
365 down_read(&OCFS2_I(dir)->ip_xattr_sem);
366 acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
368 up_read(&OCFS2_I(dir)->ip_xattr_sem);
373 mode = inode->i_mode & ~current_umask();
374 ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
381 if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
382 if (S_ISDIR(inode->i_mode)) {
383 ret = ocfs2_set_acl(handle, inode, di_bh,
384 ACL_TYPE_DEFAULT, acl,
389 mode = inode->i_mode;
390 ret = __posix_acl_create(&acl, GFP_NOFS, &mode);
394 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
401 ret = ocfs2_set_acl(handle, inode,
402 di_bh, ACL_TYPE_ACCESS,
403 acl, meta_ac, data_ac);
407 posix_acl_release(acl);