xattr.c 8.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
D
David Howells 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
/* Extended attribute handling for AFS.  We use xattrs to get and set metadata
 * instead of providing pioctl().
 *
 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 */

#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/xattr.h>
#include "internal.h"

static const char afs_xattr_list[] =
D
David Howells 已提交
15
	"afs.acl\0"
D
David Howells 已提交
16 17
	"afs.cell\0"
	"afs.fid\0"
18 19 20 21 22
	"afs.volume\0"
	"afs.yfs.acl\0"
	"afs.yfs.acl_inherited\0"
	"afs.yfs.acl_num_cleaned\0"
	"afs.yfs.vol_acl";
D
David Howells 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36

/*
 * Retrieve a list of the supported xattrs.
 */
ssize_t afs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{
	if (size == 0)
		return sizeof(afs_xattr_list);
	if (size < sizeof(afs_xattr_list))
		return -ERANGE;
	memcpy(buffer, afs_xattr_list, sizeof(afs_xattr_list));
	return sizeof(afs_xattr_list);
}

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * Deal with the result of a successful fetch ACL operation.
 */
static void afs_acl_success(struct afs_operation *op)
{
	afs_vnode_commit_status(op, &op->file[0]);
}

static void afs_acl_put(struct afs_operation *op)
{
	kfree(op->acl);
}

static const struct afs_operation_ops afs_fetch_acl_operation = {
	.issue_afs_rpc	= afs_fs_fetch_acl,
	.success	= afs_acl_success,
	.put		= afs_acl_put,
};

D
David Howells 已提交
56 57 58 59 60 61 62 63
/*
 * Get a file's ACL.
 */
static int afs_xattr_get_acl(const struct xattr_handler *handler,
			     struct dentry *dentry,
			     struct inode *inode, const char *name,
			     void *buffer, size_t size)
{
64
	struct afs_operation *op;
D
David Howells 已提交
65 66
	struct afs_vnode *vnode = AFS_FS_I(inode);
	struct afs_acl *acl = NULL;
67
	int ret;
68

69 70 71
	op = afs_alloc_operation(NULL, vnode->volume);
	if (IS_ERR(op))
		return -ENOMEM;
D
David Howells 已提交
72

73 74
	afs_op_set_vnode(op, 0, vnode);
	op->ops = &afs_fetch_acl_operation;
D
David Howells 已提交
75

76 77 78 79 80
	afs_begin_vnode_operation(op);
	afs_wait_for_operation(op);
	acl = op->acl;
	op->acl = NULL;
	ret = afs_put_operation(op);
D
David Howells 已提交
81 82 83 84

	if (ret == 0) {
		ret = acl->size;
		if (size > 0) {
85 86 87
			if (acl->size <= size)
				memcpy(buffer, acl->data, acl->size);
			else
88
				ret = -ERANGE;
D
David Howells 已提交
89 90 91
		}
	}

92
	kfree(acl);
D
David Howells 已提交
93 94 95
	return ret;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static bool afs_make_acl(struct afs_operation *op,
			 const void *buffer, size_t size)
{
	struct afs_acl *acl;

	acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
	if (!acl) {
		afs_op_nomem(op);
		return false;
	}

	acl->size = size;
	memcpy(acl->data, buffer, size);
	op->acl = acl;
	return true;
}

static const struct afs_operation_ops afs_store_acl_operation = {
	.issue_afs_rpc	= afs_fs_store_acl,
	.success	= afs_acl_success,
	.put		= afs_acl_put,
};

J
Joe Gorse 已提交
119 120 121 122 123 124 125 126
/*
 * Set a file's AFS3 ACL.
 */
static int afs_xattr_set_acl(const struct xattr_handler *handler,
                             struct dentry *dentry,
                             struct inode *inode, const char *name,
                             const void *buffer, size_t size, int flags)
{
127
	struct afs_operation *op;
J
Joe Gorse 已提交
128 129 130 131 132
	struct afs_vnode *vnode = AFS_FS_I(inode);

	if (flags == XATTR_CREATE)
		return -EINVAL;

133 134 135
	op = afs_alloc_operation(NULL, vnode->volume);
	if (IS_ERR(op))
		return -ENOMEM;
136

137 138 139
	afs_op_set_vnode(op, 0, vnode);
	if (!afs_make_acl(op, buffer, size))
		return afs_put_operation(op);
J
Joe Gorse 已提交
140

141 142
	op->ops = &afs_store_acl_operation;
	return afs_do_sync_operation(op);
J
Joe Gorse 已提交
143 144
}

D
David Howells 已提交
145
static const struct xattr_handler afs_xattr_afs_acl_handler = {
J
Joe Gorse 已提交
146 147 148
	.name   = "afs.acl",
	.get    = afs_xattr_get_acl,
	.set    = afs_xattr_set_acl,
D
David Howells 已提交
149 150
};

151 152 153 154 155 156 157 158 159 160 161
static void yfs_acl_put(struct afs_operation *op)
{
	yfs_free_opaque_acl(op->yacl);
}

static const struct afs_operation_ops yfs_fetch_opaque_acl_operation = {
	.issue_yfs_rpc	= yfs_fs_fetch_opaque_acl,
	.success	= afs_acl_success,
	/* Don't free op->yacl in .put here */
};

162 163 164 165 166 167 168 169
/*
 * Get a file's YFS ACL.
 */
static int afs_xattr_get_yfs(const struct xattr_handler *handler,
			     struct dentry *dentry,
			     struct inode *inode, const char *name,
			     void *buffer, size_t size)
{
170
	struct afs_operation *op;
171 172 173
	struct afs_vnode *vnode = AFS_FS_I(inode);
	struct yfs_acl *yacl = NULL;
	char buf[16], *data;
174
	int which = 0, dsize, ret = -ENOMEM;
175 176 177 178 179 180 181 182 183 184 185 186

	if (strcmp(name, "acl") == 0)
		which = 0;
	else if (strcmp(name, "acl_inherited") == 0)
		which = 1;
	else if (strcmp(name, "acl_num_cleaned") == 0)
		which = 2;
	else if (strcmp(name, "vol_acl") == 0)
		which = 3;
	else
		return -EOPNOTSUPP;

187 188 189 190
	yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
	if (!yacl)
		goto error;

191
	if (which == 0)
192
		yacl->flags |= YFS_ACL_WANT_ACL;
193
	else if (which == 3)
194
		yacl->flags |= YFS_ACL_WANT_VOL_ACL;
195

196 197
	op = afs_alloc_operation(NULL, vnode->volume);
	if (IS_ERR(op))
198 199
		goto error_yacl;

200 201 202
	afs_op_set_vnode(op, 0, vnode);
	op->yacl = yacl;
	op->ops = &yfs_fetch_opaque_acl_operation;
203

204 205 206
	afs_begin_vnode_operation(op);
	afs_wait_for_operation(op);
	ret = afs_put_operation(op);
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	if (ret == 0) {
		switch (which) {
		case 0:
			data = yacl->acl->data;
			dsize = yacl->acl->size;
			break;
		case 1:
			data = buf;
			dsize = scnprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
			break;
		case 2:
			data = buf;
			dsize = scnprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
			break;
		case 3:
			data = yacl->vol_acl->data;
			dsize = yacl->vol_acl->size;
			break;
		default:
			ret = -EOPNOTSUPP;
			goto error_yacl;
229 230
		}

231 232 233 234 235 236
		ret = dsize;
		if (size > 0) {
			if (dsize <= size)
				memcpy(buffer, data, dsize);
			else
				ret = -ERANGE;
237 238 239
		}
	}

240 241 242
error_yacl:
	yfs_free_opaque_acl(yacl);
error:
243 244 245
	return ret;
}

246 247 248 249 250 251
static const struct afs_operation_ops yfs_store_opaque_acl2_operation = {
	.issue_yfs_rpc	= yfs_fs_store_opaque_acl2,
	.success	= afs_acl_success,
	.put		= yfs_acl_put,
};

D
David Howells 已提交
252 253 254 255 256 257 258 259
/*
 * Set a file's YFS ACL.
 */
static int afs_xattr_set_yfs(const struct xattr_handler *handler,
                             struct dentry *dentry,
                             struct inode *inode, const char *name,
                             const void *buffer, size_t size, int flags)
{
260
	struct afs_operation *op;
D
David Howells 已提交
261 262 263 264 265 266
	struct afs_vnode *vnode = AFS_FS_I(inode);

	if (flags == XATTR_CREATE ||
	    strcmp(name, "acl") != 0)
		return -EINVAL;

267 268 269
	op = afs_alloc_operation(NULL, vnode->volume);
	if (IS_ERR(op))
		return -ENOMEM;
D
David Howells 已提交
270

271 272 273
	afs_op_set_vnode(op, 0, vnode);
	if (!afs_make_acl(op, buffer, size))
		return afs_put_operation(op);
D
David Howells 已提交
274

275 276
	op->ops = &yfs_store_opaque_acl2_operation;
	return afs_do_sync_operation(op);
D
David Howells 已提交
277 278
}

279 280 281
static const struct xattr_handler afs_xattr_yfs_handler = {
	.prefix	= "afs.yfs.",
	.get	= afs_xattr_get_yfs,
D
David Howells 已提交
282
	.set	= afs_xattr_set_yfs,
283 284
};

D
David Howells 已提交
285 286 287 288 289 290 291 292 293 294 295 296
/*
 * Get the name of the cell on which a file resides.
 */
static int afs_xattr_get_cell(const struct xattr_handler *handler,
			      struct dentry *dentry,
			      struct inode *inode, const char *name,
			      void *buffer, size_t size)
{
	struct afs_vnode *vnode = AFS_FS_I(inode);
	struct afs_cell *cell = vnode->volume->cell;
	size_t namelen;

297
	namelen = cell->name_len;
D
David Howells 已提交
298 299 300 301
	if (size == 0)
		return namelen;
	if (namelen > size)
		return -ERANGE;
302
	memcpy(buffer, cell->name, namelen);
D
David Howells 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	return namelen;
}

static const struct xattr_handler afs_xattr_afs_cell_handler = {
	.name	= "afs.cell",
	.get	= afs_xattr_get_cell,
};

/*
 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
 * hex numbers separated by colons.
 */
static int afs_xattr_get_fid(const struct xattr_handler *handler,
			     struct dentry *dentry,
			     struct inode *inode, const char *name,
			     void *buffer, size_t size)
{
	struct afs_vnode *vnode = AFS_FS_I(inode);
321
	char text[16 + 1 + 24 + 1 + 8 + 1];
D
David Howells 已提交
322 323
	size_t len;

324 325 326
	/* The volume ID is 64-bit, the vnode ID is 96-bit and the
	 * uniquifier is 32-bit.
	 */
M
Mark Salyzyn 已提交
327
	len = scnprintf(text, sizeof(text), "%llx:", vnode->fid.vid);
328
	if (vnode->fid.vnode_hi)
M
Mark Salyzyn 已提交
329 330
		len += scnprintf(text + len, sizeof(text) - len, "%x%016llx",
				vnode->fid.vnode_hi, vnode->fid.vnode);
331
	else
M
Mark Salyzyn 已提交
332 333 334 335
		len += scnprintf(text + len, sizeof(text) - len, "%llx",
				 vnode->fid.vnode);
	len += scnprintf(text + len, sizeof(text) - len, ":%x",
			 vnode->fid.unique);
336

D
David Howells 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	if (size == 0)
		return len;
	if (len > size)
		return -ERANGE;
	memcpy(buffer, text, len);
	return len;
}

static const struct xattr_handler afs_xattr_afs_fid_handler = {
	.name	= "afs.fid",
	.get	= afs_xattr_get_fid,
};

/*
 * Get the name of the volume on which a file resides.
 */
static int afs_xattr_get_volume(const struct xattr_handler *handler,
			      struct dentry *dentry,
			      struct inode *inode, const char *name,
			      void *buffer, size_t size)
{
	struct afs_vnode *vnode = AFS_FS_I(inode);
359
	const char *volname = vnode->volume->name;
D
David Howells 已提交
360 361 362 363 364 365 366
	size_t namelen;

	namelen = strlen(volname);
	if (size == 0)
		return namelen;
	if (namelen > size)
		return -ERANGE;
367
	memcpy(buffer, volname, namelen);
D
David Howells 已提交
368 369 370 371 372 373 374 375 376
	return namelen;
}

static const struct xattr_handler afs_xattr_afs_volume_handler = {
	.name	= "afs.volume",
	.get	= afs_xattr_get_volume,
};

const struct xattr_handler *afs_xattr_handlers[] = {
D
David Howells 已提交
377
	&afs_xattr_afs_acl_handler,
D
David Howells 已提交
378 379 380
	&afs_xattr_afs_cell_handler,
	&afs_xattr_afs_fid_handler,
	&afs_xattr_afs_volume_handler,
381
	&afs_xattr_yfs_handler,		/* afs.yfs. prefix */
D
David Howells 已提交
382 383
	NULL
};