blob: 605e5a3506ec25b0898292eb1098e77b7f82b3d5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mike Marshall5db11c22015-07-17 10:38:12 -04002/*
3 * (C) 2001 Clemson University and The University of Chicago
4 *
5 * See COPYING in top-level directory.
6 */
7
8#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -05009#include "orangefs-kernel.h"
10#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040011#include <linux/posix_acl_xattr.h>
Mike Marshall5db11c22015-07-17 10:38:12 -040012
Miklos Szeredi0cad6242021-08-18 22:08:24 +020013struct posix_acl *orangefs_get_acl(struct inode *inode, int type, bool rcu)
Mike Marshall5db11c22015-07-17 10:38:12 -040014{
15 struct posix_acl *acl;
16 int ret;
17 char *key = NULL, *value = NULL;
18
Miklos Szeredi0cad6242021-08-18 22:08:24 +020019 if (rcu)
20 return ERR_PTR(-ECHILD);
21
Mike Marshall5db11c22015-07-17 10:38:12 -040022 switch (type) {
23 case ACL_TYPE_ACCESS:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020024 key = XATTR_NAME_POSIX_ACL_ACCESS;
Mike Marshall5db11c22015-07-17 10:38:12 -040025 break;
26 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020027 key = XATTR_NAME_POSIX_ACL_DEFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -040028 break;
29 default:
Yi Liu8bb8aef2015-11-24 15:12:14 -050030 gossip_err("orangefs_get_acl: bogus value of type %d\n", type);
Mike Marshall5db11c22015-07-17 10:38:12 -040031 return ERR_PTR(-EINVAL);
32 }
33 /*
34 * Rather than incurring a network call just to determine the exact
35 * length of the attribute, I just allocate a max length to save on
36 * the network call. Conceivably, we could pass NULL to
Yi Liu8bb8aef2015-11-24 15:12:14 -050037 * orangefs_inode_getxattr() to probe the length of the value, but
Mike Marshall5db11c22015-07-17 10:38:12 -040038 * I don't do that for now.
39 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050040 value = kmalloc(ORANGEFS_MAX_XATTR_VALUELEN, GFP_KERNEL);
Markus Elfring0b082732017-08-17 21:35:16 +020041 if (!value)
Mike Marshall5db11c22015-07-17 10:38:12 -040042 return ERR_PTR(-ENOMEM);
43
44 gossip_debug(GOSSIP_ACL_DEBUG,
45 "inode %pU, key %s, type %d\n",
46 get_khandle_from_ino(inode),
47 key,
48 type);
Andreas Gruenbacherd373a712016-06-04 11:02:33 +020049 ret = orangefs_inode_getxattr(inode, key, value,
50 ORANGEFS_MAX_XATTR_VALUELEN);
Mike Marshall5db11c22015-07-17 10:38:12 -040051 /* if the key exists, convert it to an in-memory rep */
52 if (ret > 0) {
53 acl = posix_acl_from_xattr(&init_user_ns, value, ret);
54 } else if (ret == -ENODATA || ret == -ENOSYS) {
55 acl = NULL;
56 } else {
57 gossip_err("inode %pU retrieving acl's failed with error %d\n",
58 get_khandle_from_ino(inode),
59 ret);
60 acl = ERR_PTR(ret);
61 }
62 /* kfree(NULL) is safe, so don't worry if value ever got used */
63 kfree(value);
64 return acl;
65}
66
Jan Karab5accbb2017-06-22 15:31:13 +020067static int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl,
68 int type)
Mike Marshall5db11c22015-07-17 10:38:12 -040069{
Mike Marshall5db11c22015-07-17 10:38:12 -040070 int error = 0;
71 void *value = NULL;
72 size_t size = 0;
73 const char *name = NULL;
74
75 switch (type) {
76 case ACL_TYPE_ACCESS:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020077 name = XATTR_NAME_POSIX_ACL_ACCESS;
Mike Marshall5db11c22015-07-17 10:38:12 -040078 break;
79 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020080 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -040081 break;
82 default:
83 gossip_err("%s: invalid type %d!\n", __func__, type);
84 return -EINVAL;
85 }
86
87 gossip_debug(GOSSIP_ACL_DEBUG,
88 "%s: inode %pU, key %s type %d\n",
89 __func__, get_khandle_from_ino(inode),
90 name,
91 type);
92
93 if (acl) {
94 size = posix_acl_xattr_size(acl->a_count);
95 value = kmalloc(size, GFP_KERNEL);
96 if (!value)
97 return -ENOMEM;
98
99 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
100 if (error < 0)
101 goto out;
102 }
103
104 gossip_debug(GOSSIP_ACL_DEBUG,
105 "%s: name %s, value %p, size %zd, acl %p\n",
106 __func__, name, value, size, acl);
107 /*
108 * Go ahead and set the extended attribute now. NOTE: Suppose acl
109 * was NULL, then value will be NULL and size will be 0 and that
110 * will xlate to a removexattr. However, we don't want removexattr
111 * complain if attributes does not exist.
112 */
Andreas Gruenbacherd373a712016-06-04 11:02:33 +0200113 error = orangefs_inode_setxattr(inode, name, value, size, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400114
115out:
116 kfree(value);
117 if (!error)
118 set_cached_acl(inode, type, acl);
119 return error;
120}
121
Christian Brauner549c7292021-01-21 14:19:43 +0100122int orangefs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
123 struct posix_acl *acl, int type)
Jan Karab5accbb2017-06-22 15:31:13 +0200124{
125 int error;
Mike Marshall4bef6902017-08-10 13:50:50 -0400126 struct iattr iattr;
127 int rc;
Jan Karab5accbb2017-06-22 15:31:13 +0200128
Mike Marshall476af912020-07-09 15:02:32 -0400129 memset(&iattr, 0, sizeof iattr);
130
Jan Karab5accbb2017-06-22 15:31:13 +0200131 if (type == ACL_TYPE_ACCESS && acl) {
Mike Marshall4bef6902017-08-10 13:50:50 -0400132 /*
133 * posix_acl_update_mode checks to see if the permissions
134 * described by the ACL can be encoded into the
135 * object's mode. If so, it sets "acl" to NULL
136 * and "mode" to the new desired value. It is up to
137 * us to propagate the new mode back to the server...
138 */
Christian Braunere65ce2a2021-01-21 14:19:27 +0100139 error = posix_acl_update_mode(&init_user_ns, inode,
140 &iattr.ia_mode, &acl);
Jan Karab5accbb2017-06-22 15:31:13 +0200141 if (error) {
142 gossip_err("%s: posix_acl_update_mode err: %d\n",
143 __func__,
144 error);
145 return error;
146 }
147
Mike Marshall476af912020-07-09 15:02:32 -0400148 if (inode->i_mode != iattr.ia_mode)
Mike Marshall4bef6902017-08-10 13:50:50 -0400149 iattr.ia_valid = ATTR_MODE;
Mike Marshall4bef6902017-08-10 13:50:50 -0400150
Jan Karab5accbb2017-06-22 15:31:13 +0200151 }
Mike Marshall476af912020-07-09 15:02:32 -0400152
153 rc = __orangefs_set_acl(inode, acl, type);
154
155 if (!rc && (iattr.ia_valid == ATTR_MODE))
156 rc = __orangefs_setattr(inode, &iattr);
157
158 return rc;
Jan Karab5accbb2017-06-22 15:31:13 +0200159}
160
Yi Liu8bb8aef2015-11-24 15:12:14 -0500161int orangefs_init_acl(struct inode *inode, struct inode *dir)
Mike Marshall5db11c22015-07-17 10:38:12 -0400162{
Mike Marshall5db11c22015-07-17 10:38:12 -0400163 struct posix_acl *default_acl, *acl;
164 umode_t mode = inode->i_mode;
Martin Brandenburga55f2d82017-11-07 15:01:40 -0500165 struct iattr iattr;
Mike Marshall5db11c22015-07-17 10:38:12 -0400166 int error = 0;
167
Mike Marshall5db11c22015-07-17 10:38:12 -0400168 error = posix_acl_create(dir, &mode, &default_acl, &acl);
169 if (error)
170 return error;
171
172 if (default_acl) {
Jan Karab5accbb2017-06-22 15:31:13 +0200173 error = __orangefs_set_acl(inode, default_acl,
174 ACL_TYPE_DEFAULT);
Mike Marshall5db11c22015-07-17 10:38:12 -0400175 posix_acl_release(default_acl);
Chengguang Xu052d1272018-08-31 22:33:52 +0800176 } else {
177 inode->i_default_acl = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400178 }
179
180 if (acl) {
181 if (!error)
Jan Karab5accbb2017-06-22 15:31:13 +0200182 error = __orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
Mike Marshall5db11c22015-07-17 10:38:12 -0400183 posix_acl_release(acl);
Chengguang Xu052d1272018-08-31 22:33:52 +0800184 } else {
185 inode->i_acl = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400186 }
187
188 /* If mode of the inode was changed, then do a forcible ->setattr */
189 if (mode != inode->i_mode) {
Martin Brandenburga55f2d82017-11-07 15:01:40 -0500190 memset(&iattr, 0, sizeof iattr);
Mike Marshall5db11c22015-07-17 10:38:12 -0400191 inode->i_mode = mode;
Martin Brandenburga55f2d82017-11-07 15:01:40 -0500192 iattr.ia_mode = mode;
193 iattr.ia_valid |= ATTR_MODE;
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000194 __orangefs_setattr(inode, &iattr);
Mike Marshall5db11c22015-07-17 10:38:12 -0400195 }
196
197 return error;
198}