1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/fs/ceph/acl.c
4 *
5 * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
6 */
7
8#include <linux/ceph/ceph_debug.h>
9#include <linux/fs.h>
10#include <linux/string.h>
11#include <linux/xattr.h>
12#include <linux/posix_acl_xattr.h>
13#include <linux/posix_acl.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
16
17#include "super.h"
18#include "mds_client.h"
19
20static inline void ceph_set_cached_acl(struct inode *inode,
21					int type, struct posix_acl *acl)
22{
23	struct ceph_inode_info *ci = ceph_inode(inode);
24
25	spin_lock(&ci->i_ceph_lock);
26	if (__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0))
27		set_cached_acl(inode, type, acl);
28	else
29		forget_cached_acl(inode, type);
30	spin_unlock(&ci->i_ceph_lock);
31}
32
33struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
34{
35	struct ceph_client *cl = ceph_inode_to_client(inode);
36	int size;
37	unsigned int retry_cnt = 0;
38	const char *name;
39	char *value = NULL;
40	struct posix_acl *acl;
41
42	if (rcu)
43		return ERR_PTR(-ECHILD);
44
45	switch (type) {
46	case ACL_TYPE_ACCESS:
47		name = XATTR_NAME_POSIX_ACL_ACCESS;
48		break;
49	case ACL_TYPE_DEFAULT:
50		name = XATTR_NAME_POSIX_ACL_DEFAULT;
51		break;
52	default:
53		BUG();
54	}
55
56retry:
57	size = __ceph_getxattr(inode, name, "", 0);
58	if (size > 0) {
59		value = kzalloc(size, GFP_NOFS);
60		if (!value)
61			return ERR_PTR(-ENOMEM);
62		size = __ceph_getxattr(inode, name, value, size);
63	}
64
65	if (size == -ERANGE && retry_cnt < 10) {
66		retry_cnt++;
67		kfree(value);
68		value = NULL;
69		goto retry;
70	}
71
72	if (size > 0) {
73		acl = posix_acl_from_xattr(&init_user_ns, value, size);
74	} else if (size == -ENODATA || size == 0) {
75		acl = NULL;
76	} else {
77		pr_err_ratelimited_client(cl, "%llx.%llx failed, err=%d\n",
78					  ceph_vinop(inode), size);
79		acl = ERR_PTR(-EIO);
80	}
81
82	kfree(value);
83
84	if (!IS_ERR(acl))
85		ceph_set_cached_acl(inode, type, acl);
86
87	return acl;
88}
89
90int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
91		 struct posix_acl *acl, int type)
92{
93	int ret = 0, size = 0;
94	const char *name = NULL;
95	char *value = NULL;
96	struct iattr newattrs;
97	struct inode *inode = d_inode(dentry);
98	struct timespec64 old_ctime = inode_get_ctime(inode);
99	umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
100
101	if (ceph_snap(inode) != CEPH_NOSNAP) {
102		ret = -EROFS;
103		goto out;
104	}
105
106	switch (type) {
107	case ACL_TYPE_ACCESS:
108		name = XATTR_NAME_POSIX_ACL_ACCESS;
109		if (acl) {
110			ret = posix_acl_update_mode(idmap, inode,
111						    &new_mode, &acl);
112			if (ret)
113				goto out;
114		}
115		break;
116	case ACL_TYPE_DEFAULT:
117		if (!S_ISDIR(inode->i_mode)) {
118			ret = acl ? -EINVAL : 0;
119			goto out;
120		}
121		name = XATTR_NAME_POSIX_ACL_DEFAULT;
122		break;
123	default:
124		ret = -EINVAL;
125		goto out;
126	}
127
128	if (acl) {
129		size = posix_acl_xattr_size(acl->a_count);
130		value = kmalloc(size, GFP_NOFS);
131		if (!value) {
132			ret = -ENOMEM;
133			goto out;
134		}
135
136		ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
137		if (ret < 0)
138			goto out_free;
139	}
140
141	if (new_mode != old_mode) {
142		newattrs.ia_ctime = current_time(inode);
143		newattrs.ia_mode = new_mode;
144		newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
145		ret = __ceph_setattr(idmap, inode, &newattrs, NULL);
146		if (ret)
147			goto out_free;
148	}
149
150	ret = __ceph_setxattr(inode, name, value, size, 0);
151	if (ret) {
152		if (new_mode != old_mode) {
153			newattrs.ia_ctime = old_ctime;
154			newattrs.ia_mode = old_mode;
155			newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
156			__ceph_setattr(idmap, inode, &newattrs, NULL);
157		}
158		goto out_free;
159	}
160
161	ceph_set_cached_acl(inode, type, acl);
162
163out_free:
164	kfree(value);
165out:
166	return ret;
167}
168
169int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
170		       struct ceph_acl_sec_ctx *as_ctx)
171{
172	struct posix_acl *acl, *default_acl;
173	size_t val_size1 = 0, val_size2 = 0;
174	struct ceph_pagelist *pagelist = NULL;
175	void *tmp_buf = NULL;
176	int err;
177
178	err = posix_acl_create(dir, mode, &default_acl, &acl);
179	if (err)
180		return err;
181
182	if (acl) {
183		err = posix_acl_equiv_mode(acl, mode);
184		if (err < 0)
185			goto out_err;
186		if (err == 0) {
187			posix_acl_release(acl);
188			acl = NULL;
189		}
190	}
191
192	if (!default_acl && !acl)
193		return 0;
194
195	if (acl)
196		val_size1 = posix_acl_xattr_size(acl->a_count);
197	if (default_acl)
198		val_size2 = posix_acl_xattr_size(default_acl->a_count);
199
200	err = -ENOMEM;
201	tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
202	if (!tmp_buf)
203		goto out_err;
204	pagelist = ceph_pagelist_alloc(GFP_KERNEL);
205	if (!pagelist)
206		goto out_err;
207
208	err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
209	if (err)
210		goto out_err;
211
212	ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
213
214	if (acl) {
215		size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
216		err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
217		if (err)
218			goto out_err;
219		ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
220					    len);
221		err = posix_acl_to_xattr(&init_user_ns, acl,
222					 tmp_buf, val_size1);
223		if (err < 0)
224			goto out_err;
225		ceph_pagelist_encode_32(pagelist, val_size1);
226		ceph_pagelist_append(pagelist, tmp_buf, val_size1);
227	}
228	if (default_acl) {
229		size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
230		err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
231		if (err)
232			goto out_err;
233		ceph_pagelist_encode_string(pagelist,
234					  XATTR_NAME_POSIX_ACL_DEFAULT, len);
235		err = posix_acl_to_xattr(&init_user_ns, default_acl,
236					 tmp_buf, val_size2);
237		if (err < 0)
238			goto out_err;
239		ceph_pagelist_encode_32(pagelist, val_size2);
240		ceph_pagelist_append(pagelist, tmp_buf, val_size2);
241	}
242
243	kfree(tmp_buf);
244
245	as_ctx->acl = acl;
246	as_ctx->default_acl = default_acl;
247	as_ctx->pagelist = pagelist;
248	return 0;
249
250out_err:
251	posix_acl_release(acl);
252	posix_acl_release(default_acl);
253	kfree(tmp_buf);
254	if (pagelist)
255		ceph_pagelist_release(pagelist);
256	return err;
257}
258
259void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
260{
261	if (!inode)
262		return;
263	ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
264	ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
265}
266