1 // SPDX-License-Identifier: GPL-2.0-only
8 #include <linux/ceph/ceph_debug.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>
18 #include "mds_client.h"
20 static inline void ceph_set_cached_acl(struct inode *inode,
21 int type, struct posix_acl *acl)
23 struct ceph_inode_info *ci = ceph_inode(inode);
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);
29 forget_cached_acl(inode, type);
30 spin_unlock(&ci->i_ceph_lock);
33 struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
35 struct ceph_client *cl = ceph_inode_to_client(inode);
37 unsigned int retry_cnt = 0;
40 struct posix_acl *acl;
43 return ERR_PTR(-ECHILD);
47 name = XATTR_NAME_POSIX_ACL_ACCESS;
49 case ACL_TYPE_DEFAULT:
50 name = XATTR_NAME_POSIX_ACL_DEFAULT;
57 size = __ceph_getxattr(inode, name, "", 0);
59 value = kzalloc(size, GFP_NOFS);
61 return ERR_PTR(-ENOMEM);
62 size = __ceph_getxattr(inode, name, value, size);
65 if (size == -ERANGE && retry_cnt < 10) {
73 acl = posix_acl_from_xattr(&init_user_ns, value, size);
74 } else if (size == -ENODATA || size == 0) {
77 pr_err_ratelimited_client(cl, "%llx.%llx failed, err=%d\n",
78 ceph_vinop(inode), size);
85 ceph_set_cached_acl(inode, type, acl);
90 int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
91 struct posix_acl *acl, int type)
93 int ret = 0, size = 0;
94 const char *name = 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;
101 if (ceph_snap(inode) != CEPH_NOSNAP) {
107 case ACL_TYPE_ACCESS:
108 name = XATTR_NAME_POSIX_ACL_ACCESS;
110 ret = posix_acl_update_mode(idmap, inode,
116 case ACL_TYPE_DEFAULT:
117 if (!S_ISDIR(inode->i_mode)) {
118 ret = acl ? -EINVAL : 0;
121 name = XATTR_NAME_POSIX_ACL_DEFAULT;
129 size = posix_acl_xattr_size(acl->a_count);
130 value = kmalloc(size, GFP_NOFS);
136 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
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);
150 ret = __ceph_setxattr(inode, name, value, size, 0);
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);
161 ceph_set_cached_acl(inode, type, acl);
169 int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
170 struct ceph_acl_sec_ctx *as_ctx)
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;
178 err = posix_acl_create(dir, mode, &default_acl, &acl);
183 err = posix_acl_equiv_mode(acl, mode);
187 posix_acl_release(acl);
192 if (!default_acl && !acl)
196 val_size1 = posix_acl_xattr_size(acl->a_count);
198 val_size2 = posix_acl_xattr_size(default_acl->a_count);
201 tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
204 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
208 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
212 ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
215 size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
216 err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
219 ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
221 err = posix_acl_to_xattr(&init_user_ns, acl,
225 ceph_pagelist_encode_32(pagelist, val_size1);
226 ceph_pagelist_append(pagelist, tmp_buf, val_size1);
229 size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
230 err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
233 ceph_pagelist_encode_string(pagelist,
234 XATTR_NAME_POSIX_ACL_DEFAULT, len);
235 err = posix_acl_to_xattr(&init_user_ns, default_acl,
239 ceph_pagelist_encode_32(pagelist, val_size2);
240 ceph_pagelist_append(pagelist, tmp_buf, val_size2);
246 as_ctx->default_acl = default_acl;
247 as_ctx->pagelist = pagelist;
251 posix_acl_release(acl);
252 posix_acl_release(default_acl);
255 ceph_pagelist_release(pagelist);
259 void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
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);