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