acl.c 5.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
G
Guangliang Zhao 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * linux/fs/ceph/acl.c
 *
 * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
 */

#include <linux/ceph/ceph_debug.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/xattr.h>
#include <linux/posix_acl_xattr.h>
#include <linux/posix_acl.h>
#include <linux/sched.h>
#include <linux/slab.h>

#include "super.h"

static inline void ceph_set_cached_acl(struct inode *inode,
					int type, struct posix_acl *acl)
{
	struct ceph_inode_info *ci = ceph_inode(inode);

	spin_lock(&ci->i_ceph_lock);
	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0))
		set_cached_acl(inode, type, acl);
27 28
	else
		forget_cached_acl(inode, type);
G
Guangliang Zhao 已提交
29 30 31 32 33 34
	spin_unlock(&ci->i_ceph_lock);
}

struct posix_acl *ceph_get_acl(struct inode *inode, int type)
{
	int size;
35
	unsigned int retry_cnt = 0;
G
Guangliang Zhao 已提交
36 37 38 39 40 41
	const char *name;
	char *value = NULL;
	struct posix_acl *acl;

	switch (type) {
	case ACL_TYPE_ACCESS:
42
		name = XATTR_NAME_POSIX_ACL_ACCESS;
G
Guangliang Zhao 已提交
43 44
		break;
	case ACL_TYPE_DEFAULT:
45
		name = XATTR_NAME_POSIX_ACL_DEFAULT;
G
Guangliang Zhao 已提交
46 47 48 49 50
		break;
	default:
		BUG();
	}

51
retry:
G
Guangliang Zhao 已提交
52 53 54 55 56 57 58 59
	size = __ceph_getxattr(inode, name, "", 0);
	if (size > 0) {
		value = kzalloc(size, GFP_NOFS);
		if (!value)
			return ERR_PTR(-ENOMEM);
		size = __ceph_getxattr(inode, name, value, size);
	}

60 61 62 63 64 65 66 67
	if (size == -ERANGE && retry_cnt < 10) {
		retry_cnt++;
		kfree(value);
		value = NULL;
		goto retry;
	}

	if (size > 0) {
G
Guangliang Zhao 已提交
68
		acl = posix_acl_from_xattr(&init_user_ns, value, size);
69
	} else if (size == -ENODATA || size == 0) {
G
Guangliang Zhao 已提交
70
		acl = NULL;
71 72 73
	} else {
		pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n",
				   ceph_vinop(inode), size);
G
Guangliang Zhao 已提交
74
		acl = ERR_PTR(-EIO);
75
	}
G
Guangliang Zhao 已提交
76 77 78 79 80 81 82 83 84

	kfree(value);

	if (!IS_ERR(acl))
		ceph_set_cached_acl(inode, type, acl);

	return acl;
}

S
Sage Weil 已提交
85
int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
G
Guangliang Zhao 已提交
86 87 88 89 90
{
	int ret = 0, size = 0;
	const char *name = NULL;
	char *value = NULL;
	struct iattr newattrs;
91
	struct timespec64 old_ctime = inode->i_ctime;
G
Guangliang Zhao 已提交
92 93
	umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;

94 95 96 97 98
	if (ceph_snap(inode) != CEPH_NOSNAP) {
		ret = -EROFS;
		goto out;
	}

G
Guangliang Zhao 已提交
99 100
	switch (type) {
	case ACL_TYPE_ACCESS:
101
		name = XATTR_NAME_POSIX_ACL_ACCESS;
G
Guangliang Zhao 已提交
102
		if (acl) {
103 104
			ret = posix_acl_update_mode(inode, &new_mode, &acl);
			if (ret)
G
Guangliang Zhao 已提交
105 106 107 108 109 110 111 112
				goto out;
		}
		break;
	case ACL_TYPE_DEFAULT:
		if (!S_ISDIR(inode->i_mode)) {
			ret = acl ? -EINVAL : 0;
			goto out;
		}
113
		name = XATTR_NAME_POSIX_ACL_DEFAULT;
G
Guangliang Zhao 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		break;
	default:
		ret = -EINVAL;
		goto out;
	}

	if (acl) {
		size = posix_acl_xattr_size(acl->a_count);
		value = kmalloc(size, GFP_NOFS);
		if (!value) {
			ret = -ENOMEM;
			goto out;
		}

		ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
		if (ret < 0)
			goto out_free;
	}

	if (new_mode != old_mode) {
Y
Yan, Zheng 已提交
134
		newattrs.ia_ctime = current_time(inode);
G
Guangliang Zhao 已提交
135
		newattrs.ia_mode = new_mode;
136
		newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
137
		ret = __ceph_setattr(inode, &newattrs);
G
Guangliang Zhao 已提交
138
		if (ret)
139
			goto out_free;
G
Guangliang Zhao 已提交
140 141
	}

142
	ret = __ceph_setxattr(inode, name, value, size, 0);
G
Guangliang Zhao 已提交
143 144
	if (ret) {
		if (new_mode != old_mode) {
145
			newattrs.ia_ctime = old_ctime;
G
Guangliang Zhao 已提交
146
			newattrs.ia_mode = old_mode;
147
			newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
148
			__ceph_setattr(inode, &newattrs);
G
Guangliang Zhao 已提交
149
		}
150
		goto out_free;
G
Guangliang Zhao 已提交
151 152 153 154 155 156 157 158 159 160
	}

	ceph_set_cached_acl(inode, type, acl);

out_free:
	kfree(value);
out:
	return ret;
}

161
int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
162
		       struct ceph_acl_sec_ctx *as_ctx)
G
Guangliang Zhao 已提交
163
{
164 165 166 167 168 169 170 171 172 173 174
	struct posix_acl *acl, *default_acl;
	size_t val_size1 = 0, val_size2 = 0;
	struct ceph_pagelist *pagelist = NULL;
	void *tmp_buf = NULL;
	int err;

	err = posix_acl_create(dir, mode, &default_acl, &acl);
	if (err)
		return err;

	if (acl) {
175 176
		err = posix_acl_equiv_mode(acl, mode);
		if (err < 0)
177
			goto out_err;
178
		if (err == 0) {
179 180
			posix_acl_release(acl);
			acl = NULL;
181 182
		}
	}
G
Guangliang Zhao 已提交
183

184 185 186 187 188 189 190 191 192
	if (!default_acl && !acl)
		return 0;

	if (acl)
		val_size1 = posix_acl_xattr_size(acl->a_count);
	if (default_acl)
		val_size2 = posix_acl_xattr_size(default_acl->a_count);

	err = -ENOMEM;
193
	tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
194 195
	if (!tmp_buf)
		goto out_err;
196
	pagelist = ceph_pagelist_alloc(GFP_KERNEL);
197 198 199 200 201 202 203 204 205
	if (!pagelist)
		goto out_err;

	err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
	if (err)
		goto out_err;

	ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);

206
	if (acl) {
207
		size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
208 209 210
		err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
		if (err)
			goto out_err;
211
		ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
212 213 214 215 216 217 218
					    len);
		err = posix_acl_to_xattr(&init_user_ns, acl,
					 tmp_buf, val_size1);
		if (err < 0)
			goto out_err;
		ceph_pagelist_encode_32(pagelist, val_size1);
		ceph_pagelist_append(pagelist, tmp_buf, val_size1);
219
	}
220
	if (default_acl) {
221
		size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
222 223 224
		err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
		if (err)
			goto out_err;
225 226
		ceph_pagelist_encode_string(pagelist,
					  XATTR_NAME_POSIX_ACL_DEFAULT, len);
227 228 229 230 231 232 233 234 235 236
		err = posix_acl_to_xattr(&init_user_ns, default_acl,
					 tmp_buf, val_size2);
		if (err < 0)
			goto out_err;
		ceph_pagelist_encode_32(pagelist, val_size2);
		ceph_pagelist_append(pagelist, tmp_buf, val_size2);
	}

	kfree(tmp_buf);

237 238 239
	as_ctx->acl = acl;
	as_ctx->default_acl = default_acl;
	as_ctx->pagelist = pagelist;
240 241 242 243 244 245 246 247 248 249 250
	return 0;

out_err:
	posix_acl_release(acl);
	posix_acl_release(default_acl);
	kfree(tmp_buf);
	if (pagelist)
		ceph_pagelist_release(pagelist);
	return err;
}

251
void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
252 253 254
{
	if (!inode)
		return;
255 256
	ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
	ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
G
Guangliang Zhao 已提交
257
}