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
ceph_set_cached_acl(struct inode * inode,int type,struct posix_acl * acl)20 static 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
ceph_get_acl(struct inode * inode,int type,bool rcu)33 struct 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
56 retry:
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
ceph_set_acl(const struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * acl,int type)90 int ceph_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
91 struct posix_acl *acl, int type)
92 {
93 int ret = 0;
94 size_t size = 0;
95 const char *name = NULL;
96 char *value = NULL;
97 struct iattr newattrs;
98 struct inode *inode = d_inode(dentry);
99 struct timespec64 old_ctime = inode_get_ctime(inode);
100 umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
101
102 if (ceph_snap(inode) != CEPH_NOSNAP) {
103 ret = -EROFS;
104 goto out;
105 }
106
107 switch (type) {
108 case ACL_TYPE_ACCESS:
109 name = XATTR_NAME_POSIX_ACL_ACCESS;
110 if (acl) {
111 ret = posix_acl_update_mode(idmap, inode,
112 &new_mode, &acl);
113 if (ret)
114 goto out;
115 }
116 break;
117 case ACL_TYPE_DEFAULT:
118 if (!S_ISDIR(inode->i_mode)) {
119 ret = acl ? -EINVAL : 0;
120 goto out;
121 }
122 name = XATTR_NAME_POSIX_ACL_DEFAULT;
123 break;
124 default:
125 ret = -EINVAL;
126 goto out;
127 }
128
129 if (acl) {
130 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
131 if (!value) {
132 ret = -ENOMEM;
133 goto out;
134 }
135 }
136
137 if (new_mode != old_mode) {
138 newattrs.ia_ctime = current_time(inode);
139 newattrs.ia_mode = new_mode;
140 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
141 ret = __ceph_setattr(idmap, inode, &newattrs, NULL);
142 if (ret)
143 goto out_free;
144 }
145
146 ret = __ceph_setxattr(inode, name, value, size, 0);
147 if (ret) {
148 if (new_mode != old_mode) {
149 newattrs.ia_ctime = old_ctime;
150 newattrs.ia_mode = old_mode;
151 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
152 __ceph_setattr(idmap, inode, &newattrs, NULL);
153 }
154 goto out_free;
155 }
156
157 ceph_set_cached_acl(inode, type, acl);
158
159 out_free:
160 kfree(value);
161 out:
162 return ret;
163 }
164
ceph_pre_init_acls(struct inode * dir,umode_t * mode,struct ceph_acl_sec_ctx * as_ctx)165 int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
166 struct ceph_acl_sec_ctx *as_ctx)
167 {
168 struct posix_acl *acl, *default_acl;
169 size_t val_size1 = 0, val_size2 = 0;
170 struct ceph_pagelist *pagelist = NULL;
171 void *tmp_buf1 = NULL, *tmp_buf2 = NULL;
172 int err;
173
174 err = posix_acl_create(dir, mode, &default_acl, &acl);
175 if (err)
176 return err;
177
178 if (acl) {
179 err = posix_acl_equiv_mode(acl, mode);
180 if (err < 0)
181 goto out_err;
182 if (err == 0) {
183 posix_acl_release(acl);
184 acl = NULL;
185 }
186 }
187
188 if (!default_acl && !acl)
189 return 0;
190
191 err = -ENOMEM;
192 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
193 if (!pagelist)
194 goto out_err;
195
196 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
197 if (err)
198 goto out_err;
199
200 ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
201
202 if (acl) {
203 size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
204
205 err = -ENOMEM;
206 tmp_buf1 = posix_acl_to_xattr(&init_user_ns, acl,
207 &val_size1, GFP_KERNEL);
208 if (!tmp_buf1)
209 goto out_err;
210 err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
211 if (err)
212 goto out_err;
213 ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
214 len);
215 ceph_pagelist_encode_32(pagelist, val_size1);
216 ceph_pagelist_append(pagelist, tmp_buf1, val_size1);
217 }
218 if (default_acl) {
219 size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
220
221 err = -ENOMEM;
222 tmp_buf2 = posix_acl_to_xattr(&init_user_ns, default_acl,
223 &val_size2, GFP_KERNEL);
224 if (!tmp_buf2)
225 goto out_err;
226 err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
227 if (err)
228 goto out_err;
229 ceph_pagelist_encode_string(pagelist,
230 XATTR_NAME_POSIX_ACL_DEFAULT, len);
231 ceph_pagelist_encode_32(pagelist, val_size2);
232 ceph_pagelist_append(pagelist, tmp_buf2, val_size2);
233 }
234
235 kfree(tmp_buf1);
236 kfree(tmp_buf2);
237
238 as_ctx->acl = acl;
239 as_ctx->default_acl = default_acl;
240 as_ctx->pagelist = pagelist;
241 return 0;
242
243 out_err:
244 posix_acl_release(acl);
245 posix_acl_release(default_acl);
246 kfree(tmp_buf1);
247 kfree(tmp_buf2);
248 if (pagelist)
249 ceph_pagelist_release(pagelist);
250 return err;
251 }
252
ceph_init_inode_acls(struct inode * inode,struct ceph_acl_sec_ctx * as_ctx)253 void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
254 {
255 if (!inode)
256 return;
257 ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
258 ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
259 }
260