^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) 2001 Clemson University and The University of Chicago
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * See COPYING in top-level directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "orangefs-kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "orangefs-bufmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/posix_acl_xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct posix_acl *orangefs_get_acl(struct inode *inode, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) char *key = NULL, *value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) case ACL_TYPE_ACCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) key = XATTR_NAME_POSIX_ACL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) case ACL_TYPE_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) key = XATTR_NAME_POSIX_ACL_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) gossip_err("orangefs_get_acl: bogus value of type %d\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Rather than incurring a network call just to determine the exact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * length of the attribute, I just allocate a max length to save on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * the network call. Conceivably, we could pass NULL to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * orangefs_inode_getxattr() to probe the length of the value, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * I don't do that for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) value = kmalloc(ORANGEFS_MAX_XATTR_VALUELEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) gossip_debug(GOSSIP_ACL_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) "inode %pU, key %s, type %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) get_khandle_from_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ret = orangefs_inode_getxattr(inode, key, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ORANGEFS_MAX_XATTR_VALUELEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* if the key exists, convert it to an in-memory rep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) acl = posix_acl_from_xattr(&init_user_ns, value, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } else if (ret == -ENODATA || ret == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) gossip_err("inode %pU retrieving acl's failed with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) get_khandle_from_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) acl = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* kfree(NULL) is safe, so don't worry if value ever got used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void *value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) size_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) case ACL_TYPE_ACCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) name = XATTR_NAME_POSIX_ACL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) case ACL_TYPE_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) name = XATTR_NAME_POSIX_ACL_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) gossip_err("%s: invalid type %d!\n", __func__, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) gossip_debug(GOSSIP_ACL_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) "%s: inode %pU, key %s type %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) __func__, get_khandle_from_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) size = posix_acl_xattr_size(acl->a_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) value = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) gossip_debug(GOSSIP_ACL_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) "%s: name %s, value %p, size %zd, acl %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) __func__, name, value, size, acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Go ahead and set the extended attribute now. NOTE: Suppose acl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * was NULL, then value will be NULL and size will be 0 and that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * will xlate to a removexattr. However, we don't want removexattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * complain if attributes does not exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) error = orangefs_inode_setxattr(inode, name, value, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) set_cached_acl(inode, type, acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct iattr iattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) memset(&iattr, 0, sizeof iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (type == ACL_TYPE_ACCESS && acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * posix_acl_update_mode checks to see if the permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * described by the ACL can be encoded into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * object's mode. If so, it sets "acl" to NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * and "mode" to the new desired value. It is up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * us to propagate the new mode back to the server...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) error = posix_acl_update_mode(inode, &iattr.ia_mode, &acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) gossip_err("%s: posix_acl_update_mode err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return error;
^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 (inode->i_mode != iattr.ia_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) iattr.ia_valid = ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) rc = __orangefs_set_acl(inode, acl, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!rc && (iattr.ia_valid == ATTR_MODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) rc = __orangefs_setattr(inode, &iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return rc;
^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) int orangefs_init_acl(struct inode *inode, struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct posix_acl *default_acl, *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) umode_t mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct iattr iattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) error = posix_acl_create(dir, &mode, &default_acl, &acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (default_acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) error = __orangefs_set_acl(inode, default_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ACL_TYPE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) posix_acl_release(default_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) inode->i_default_acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) error = __orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) posix_acl_release(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) inode->i_acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* If mode of the inode was changed, then do a forcible ->setattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (mode != inode->i_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) memset(&iattr, 0, sizeof iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) iattr.ia_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) iattr.ia_valid |= ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) __orangefs_setattr(inode, &iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }