^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * acl.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2004, 2008 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * CREDITS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Lots of code in this file is copy from linux/fs/ext3/acl.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <cluster/masklog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "ocfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "dlmglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "file.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "journal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "ocfs2_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Convert from xattr value to acl struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int n, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (size < sizeof(struct posix_acl_entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) count = size / sizeof(struct posix_acl_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) acl = posix_acl_alloc(count, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) for (n = 0; n < count; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct ocfs2_acl_entry *entry =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) (struct ocfs2_acl_entry *)value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) switch(acl->a_entries[n].e_tag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) case ACL_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) acl->a_entries[n].e_uid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) make_kuid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) le32_to_cpu(entry->e_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) case ACL_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) acl->a_entries[n].e_gid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) make_kgid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) le32_to_cpu(entry->e_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) value += sizeof(struct posix_acl_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Convert acl struct to xattr value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct ocfs2_acl_entry *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) char *ocfs2_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *size = acl->a_count * sizeof(struct posix_acl_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ocfs2_acl = kmalloc(*size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!ocfs2_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) entry = (struct ocfs2_acl_entry *)ocfs2_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) for (n = 0; n < acl->a_count; n++, entry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) switch(acl->a_entries[n].e_tag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case ACL_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) entry->e_id = cpu_to_le32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) from_kuid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) acl->a_entries[n].e_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case ACL_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) entry->e_id = cpu_to_le32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) from_kgid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) acl->a_entries[n].e_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return ocfs2_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct buffer_head *di_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int name_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) char *value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) case ACL_TYPE_ACCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) case ACL_TYPE_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (retval > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) value = kmalloc(retval, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "", value, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (retval > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) acl = ocfs2_acl_from_xattr(value, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) else if (retval == -ENODATA || retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) acl = ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Helper function to set i_mode in memory and disk. Some call paths
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * will not have di_bh or a journal handle to pass, in which case it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * will create it's own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) handle_t *handle, umode_t new_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int ret, commit_handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (di_bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = ocfs2_read_inode_block(inode, &di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) get_bh(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (handle == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) OCFS2_INODE_UPDATE_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) commit_handle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) inode->i_mode = new_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) di->i_mode = cpu_to_le16(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ocfs2_update_inode_fsync_trans(handle, inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (commit_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) out_brelse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * Set the access or default ACL of an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int ocfs2_set_acl(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct posix_acl *acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct ocfs2_alloc_context *meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct ocfs2_alloc_context *data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int name_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) void *value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) size_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (S_ISLNK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) case ACL_TYPE_ACCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case ACL_TYPE_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return acl ? -EACCES : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) value = ocfs2_acl_to_xattr(acl, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (IS_ERR(value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return (int)PTR_ERR(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) "", value, size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) meta_ac, data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) set_cached_acl(inode, type, acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int status, had_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct ocfs2_lock_holder oh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (had_lock < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return had_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (type == ACL_TYPE_ACCESS && acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) status = posix_acl_update_mode(inode, &mode, &acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) status = ocfs2_acl_set_mode(inode, bh, NULL, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct ocfs2_super *osb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int had_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct ocfs2_lock_holder oh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (had_lock < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return ERR_PTR(had_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) down_read(&OCFS2_I(inode)->ip_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) acl = ocfs2_get_acl_nolock(inode, type, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) up_read(&OCFS2_I(inode)->ip_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int ocfs2_acl_chmod(struct inode *inode, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (S_ISLNK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) down_read(&OCFS2_I(inode)->ip_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) up_read(&OCFS2_I(inode)->ip_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (IS_ERR_OR_NULL(acl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return PTR_ERR_OR_ZERO(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) acl, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) posix_acl_release(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * Initialize the ACLs of a new inode. If parent directory has default ACL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * then clone to new inode. Called from ocfs2_mknod.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) int ocfs2_init_acl(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct buffer_head *dir_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct ocfs2_alloc_context *meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct ocfs2_alloc_context *data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct posix_acl *acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int ret = 0, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!S_ISLNK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) down_read(&OCFS2_I(dir)->ip_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) up_read(&OCFS2_I(dir)->ip_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (IS_ERR(acl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return PTR_ERR(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) mode = inode->i_mode & ~current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ret = ocfs2_set_acl(handle, inode, di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ACL_TYPE_DEFAULT, acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) meta_ac, data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ret = __posix_acl_create(&acl, GFP_NOFS, &mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (ret2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) mlog_errno(ret2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = ocfs2_set_acl(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) di_bh, ACL_TYPE_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) acl, meta_ac, data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) posix_acl_release(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }