blob: 00cf18cff98fedea42fc0a048dedbd46d6686062 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsd3e3b7ea2017-07-06 15:50:27 +01002/* Extended attribute handling for AFS. We use xattrs to get and set metadata
3 * instead of providing pioctl().
4 *
5 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
David Howellsd3e3b7ea2017-07-06 15:50:27 +01007 */
8
9#include <linux/slab.h>
10#include <linux/fs.h>
11#include <linux/xattr.h>
12#include "internal.h"
13
14static const char afs_xattr_list[] =
David Howells260f0822019-04-25 14:26:52 +010015 "afs.acl\0"
David Howellsd3e3b7ea2017-07-06 15:50:27 +010016 "afs.cell\0"
17 "afs.fid\0"
David Howellsae465782019-04-30 18:30:21 +010018 "afs.volume\0"
19 "afs.yfs.acl\0"
20 "afs.yfs.acl_inherited\0"
21 "afs.yfs.acl_num_cleaned\0"
22 "afs.yfs.vol_acl";
David Howellsd3e3b7ea2017-07-06 15:50:27 +010023
24/*
25 * Retrieve a list of the supported xattrs.
26 */
27ssize_t afs_listxattr(struct dentry *dentry, char *buffer, size_t size)
28{
29 if (size == 0)
30 return sizeof(afs_xattr_list);
31 if (size < sizeof(afs_xattr_list))
32 return -ERANGE;
33 memcpy(buffer, afs_xattr_list, sizeof(afs_xattr_list));
34 return sizeof(afs_xattr_list);
35}
36
37/*
David Howellse49c7b22020-04-10 20:51:51 +010038 * Deal with the result of a successful fetch ACL operation.
39 */
40static void afs_acl_success(struct afs_operation *op)
41{
42 afs_vnode_commit_status(op, &op->file[0]);
43}
44
45static void afs_acl_put(struct afs_operation *op)
46{
47 kfree(op->acl);
48}
49
50static const struct afs_operation_ops afs_fetch_acl_operation = {
51 .issue_afs_rpc = afs_fs_fetch_acl,
52 .success = afs_acl_success,
53 .put = afs_acl_put,
54};
55
56/*
David Howells260f0822019-04-25 14:26:52 +010057 * Get a file's ACL.
58 */
59static int afs_xattr_get_acl(const struct xattr_handler *handler,
60 struct dentry *dentry,
61 struct inode *inode, const char *name,
Mark Salyzyn3484eba2019-11-04 08:57:10 -080062 void *buffer, size_t size, int flags)
David Howells260f0822019-04-25 14:26:52 +010063{
David Howellse49c7b22020-04-10 20:51:51 +010064 struct afs_operation *op;
David Howells260f0822019-04-25 14:26:52 +010065 struct afs_vnode *vnode = AFS_FS_I(inode);
66 struct afs_acl *acl = NULL;
David Howellse49c7b22020-04-10 20:51:51 +010067 int ret;
David Howellsa58823a2019-05-09 15:16:10 +010068
David Howellse49c7b22020-04-10 20:51:51 +010069 op = afs_alloc_operation(NULL, vnode->volume);
70 if (IS_ERR(op))
71 return -ENOMEM;
David Howells260f0822019-04-25 14:26:52 +010072
David Howellse49c7b22020-04-10 20:51:51 +010073 afs_op_set_vnode(op, 0, vnode);
74 op->ops = &afs_fetch_acl_operation;
David Howells260f0822019-04-25 14:26:52 +010075
David Howellse49c7b22020-04-10 20:51:51 +010076 afs_begin_vnode_operation(op);
77 afs_wait_for_operation(op);
78 acl = op->acl;
79 op->acl = NULL;
80 ret = afs_put_operation(op);
David Howells260f0822019-04-25 14:26:52 +010081
82 if (ret == 0) {
83 ret = acl->size;
84 if (size > 0) {
David Howellscc1dd5c2019-05-12 08:05:10 +010085 if (acl->size <= size)
86 memcpy(buffer, acl->data, acl->size);
87 else
David Howellse49c7b22020-04-10 20:51:51 +010088 op->error = -ERANGE;
David Howells260f0822019-04-25 14:26:52 +010089 }
David Howells260f0822019-04-25 14:26:52 +010090 }
91
David Howellse49c7b22020-04-10 20:51:51 +010092 kfree(acl);
David Howells260f0822019-04-25 14:26:52 +010093 return ret;
94}
95
David Howellse49c7b22020-04-10 20:51:51 +010096static bool afs_make_acl(struct afs_operation *op,
97 const void *buffer, size_t size)
98{
99 struct afs_acl *acl;
100
101 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
102 if (!acl) {
103 afs_op_nomem(op);
104 return false;
105 }
106
107 acl->size = size;
108 memcpy(acl->data, buffer, size);
109 op->acl = acl;
110 return true;
111}
112
113static const struct afs_operation_ops afs_store_acl_operation = {
114 .issue_afs_rpc = afs_fs_store_acl,
115 .success = afs_acl_success,
116 .put = afs_acl_put,
117};
118
Joe Gorseb10494a2019-04-25 14:26:52 +0100119/*
120 * Set a file's AFS3 ACL.
121 */
122static int afs_xattr_set_acl(const struct xattr_handler *handler,
123 struct dentry *dentry,
124 struct inode *inode, const char *name,
125 const void *buffer, size_t size, int flags)
126{
David Howellse49c7b22020-04-10 20:51:51 +0100127 struct afs_operation *op;
Joe Gorseb10494a2019-04-25 14:26:52 +0100128 struct afs_vnode *vnode = AFS_FS_I(inode);
Joe Gorseb10494a2019-04-25 14:26:52 +0100129
130 if (flags == XATTR_CREATE)
131 return -EINVAL;
132
David Howellse49c7b22020-04-10 20:51:51 +0100133 op = afs_alloc_operation(NULL, vnode->volume);
134 if (IS_ERR(op))
135 return -ENOMEM;
Joe Gorseb10494a2019-04-25 14:26:52 +0100136
David Howellse49c7b22020-04-10 20:51:51 +0100137 afs_op_set_vnode(op, 0, vnode);
138 if (!afs_make_acl(op, buffer, size))
139 return afs_put_operation(op);
David Howellsa58823a2019-05-09 15:16:10 +0100140
David Howellse49c7b22020-04-10 20:51:51 +0100141 op->ops = &afs_store_acl_operation;
142 return afs_do_sync_operation(op);
Joe Gorseb10494a2019-04-25 14:26:52 +0100143}
144
David Howells260f0822019-04-25 14:26:52 +0100145static const struct xattr_handler afs_xattr_afs_acl_handler = {
Joe Gorseb10494a2019-04-25 14:26:52 +0100146 .name = "afs.acl",
147 .get = afs_xattr_get_acl,
148 .set = afs_xattr_set_acl,
David Howells260f0822019-04-25 14:26:52 +0100149};
150
David Howellse49c7b22020-04-10 20:51:51 +0100151static void yfs_acl_put(struct afs_operation *op)
152{
153 yfs_free_opaque_acl(op->yacl);
154}
155
156static const struct afs_operation_ops yfs_fetch_opaque_acl_operation = {
157 .issue_yfs_rpc = yfs_fs_fetch_opaque_acl,
158 .success = afs_acl_success,
159 /* Don't free op->yacl in .put here */
160};
161
David Howells260f0822019-04-25 14:26:52 +0100162/*
David Howellsae465782019-04-30 18:30:21 +0100163 * Get a file's YFS ACL.
164 */
165static int afs_xattr_get_yfs(const struct xattr_handler *handler,
166 struct dentry *dentry,
167 struct inode *inode, const char *name,
Mark Salyzyn3484eba2019-11-04 08:57:10 -0800168 void *buffer, size_t size, int flags)
David Howellsae465782019-04-30 18:30:21 +0100169{
David Howellse49c7b22020-04-10 20:51:51 +0100170 struct afs_operation *op;
David Howellsae465782019-04-30 18:30:21 +0100171 struct afs_vnode *vnode = AFS_FS_I(inode);
172 struct yfs_acl *yacl = NULL;
David Howellsae465782019-04-30 18:30:21 +0100173 char buf[16], *data;
David Howells773e0c42019-05-12 08:31:23 +0100174 int which = 0, dsize, ret = -ENOMEM;
David Howellsae465782019-04-30 18:30:21 +0100175
176 if (strcmp(name, "acl") == 0)
177 which = 0;
178 else if (strcmp(name, "acl_inherited") == 0)
179 which = 1;
180 else if (strcmp(name, "acl_num_cleaned") == 0)
181 which = 2;
182 else if (strcmp(name, "vol_acl") == 0)
183 which = 3;
184 else
185 return -EOPNOTSUPP;
186
David Howells773e0c42019-05-12 08:31:23 +0100187 yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
188 if (!yacl)
189 goto error;
190
David Howellsae465782019-04-30 18:30:21 +0100191 if (which == 0)
David Howells773e0c42019-05-12 08:31:23 +0100192 yacl->flags |= YFS_ACL_WANT_ACL;
David Howellsae465782019-04-30 18:30:21 +0100193 else if (which == 3)
David Howells773e0c42019-05-12 08:31:23 +0100194 yacl->flags |= YFS_ACL_WANT_VOL_ACL;
David Howellsae465782019-04-30 18:30:21 +0100195
David Howellse49c7b22020-04-10 20:51:51 +0100196 op = afs_alloc_operation(NULL, vnode->volume);
197 if (IS_ERR(op))
David Howellsa58823a2019-05-09 15:16:10 +0100198 goto error_yacl;
199
David Howellse49c7b22020-04-10 20:51:51 +0100200 afs_op_set_vnode(op, 0, vnode);
201 op->yacl = yacl;
202 op->ops = &yfs_fetch_opaque_acl_operation;
David Howellsae465782019-04-30 18:30:21 +0100203
David Howellse49c7b22020-04-10 20:51:51 +0100204 afs_begin_vnode_operation(op);
205 afs_wait_for_operation(op);
206 ret = afs_put_operation(op);
David Howellsa58823a2019-05-09 15:16:10 +0100207
David Howellse49c7b22020-04-10 20:51:51 +0100208 if (ret == 0) {
209 switch (which) {
210 case 0:
211 data = yacl->acl->data;
212 dsize = yacl->acl->size;
213 break;
214 case 1:
215 data = buf;
216 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
217 break;
218 case 2:
219 data = buf;
220 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
221 break;
222 case 3:
223 data = yacl->vol_acl->data;
224 dsize = yacl->vol_acl->size;
225 break;
226 default:
227 ret = -EOPNOTSUPP;
228 goto error_yacl;
David Howellsae465782019-04-30 18:30:21 +0100229 }
230
David Howellse49c7b22020-04-10 20:51:51 +0100231 ret = dsize;
232 if (size > 0) {
233 if (dsize <= size)
234 memcpy(buffer, data, dsize);
235 else
236 ret = -ERANGE;
David Howells773e0c42019-05-12 08:31:23 +0100237 }
David Howells773e0c42019-05-12 08:31:23 +0100238 }
239
David Howells773e0c42019-05-12 08:31:23 +0100240error_yacl:
241 yfs_free_opaque_acl(yacl);
242error:
David Howellsae465782019-04-30 18:30:21 +0100243 return ret;
244}
245
David Howellse49c7b22020-04-10 20:51:51 +0100246static const struct afs_operation_ops yfs_store_opaque_acl2_operation = {
247 .issue_yfs_rpc = yfs_fs_store_opaque_acl2,
248 .success = afs_acl_success,
249 .put = yfs_acl_put,
250};
251
David Howellsf5e45462019-05-01 14:05:27 +0100252/*
253 * Set a file's YFS ACL.
254 */
255static int afs_xattr_set_yfs(const struct xattr_handler *handler,
256 struct dentry *dentry,
257 struct inode *inode, const char *name,
258 const void *buffer, size_t size, int flags)
259{
David Howellse49c7b22020-04-10 20:51:51 +0100260 struct afs_operation *op;
David Howellsf5e45462019-05-01 14:05:27 +0100261 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howellsf5e45462019-05-01 14:05:27 +0100262
263 if (flags == XATTR_CREATE ||
264 strcmp(name, "acl") != 0)
265 return -EINVAL;
266
David Howellse49c7b22020-04-10 20:51:51 +0100267 op = afs_alloc_operation(NULL, vnode->volume);
268 if (IS_ERR(op))
269 return -ENOMEM;
David Howellsf5e45462019-05-01 14:05:27 +0100270
David Howellse49c7b22020-04-10 20:51:51 +0100271 afs_op_set_vnode(op, 0, vnode);
272 if (!afs_make_acl(op, buffer, size))
273 return afs_put_operation(op);
David Howellsf5e45462019-05-01 14:05:27 +0100274
David Howellse49c7b22020-04-10 20:51:51 +0100275 op->ops = &yfs_store_opaque_acl2_operation;
276 return afs_do_sync_operation(op);
David Howellsf5e45462019-05-01 14:05:27 +0100277}
278
David Howellsae465782019-04-30 18:30:21 +0100279static const struct xattr_handler afs_xattr_yfs_handler = {
280 .prefix = "afs.yfs.",
281 .get = afs_xattr_get_yfs,
David Howellsf5e45462019-05-01 14:05:27 +0100282 .set = afs_xattr_set_yfs,
David Howellsae465782019-04-30 18:30:21 +0100283};
284
285/*
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100286 * Get the name of the cell on which a file resides.
287 */
288static int afs_xattr_get_cell(const struct xattr_handler *handler,
289 struct dentry *dentry,
290 struct inode *inode, const char *name,
Mark Salyzyn3484eba2019-11-04 08:57:10 -0800291 void *buffer, size_t size, int flags)
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100292{
293 struct afs_vnode *vnode = AFS_FS_I(inode);
294 struct afs_cell *cell = vnode->volume->cell;
295 size_t namelen;
296
David Howells989782d2017-11-02 15:27:50 +0000297 namelen = cell->name_len;
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100298 if (size == 0)
299 return namelen;
300 if (namelen > size)
301 return -ERANGE;
David Howellsc73aa412019-05-01 13:27:09 +0100302 memcpy(buffer, cell->name, namelen);
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100303 return namelen;
304}
305
306static const struct xattr_handler afs_xattr_afs_cell_handler = {
307 .name = "afs.cell",
308 .get = afs_xattr_get_cell,
309};
310
311/*
312 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
313 * hex numbers separated by colons.
314 */
315static int afs_xattr_get_fid(const struct xattr_handler *handler,
316 struct dentry *dentry,
317 struct inode *inode, const char *name,
Mark Salyzyn3484eba2019-11-04 08:57:10 -0800318 void *buffer, size_t size, int flags)
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100319{
320 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howellsa2f611a2019-05-01 15:58:24 +0100321 char text[16 + 1 + 24 + 1 + 8 + 1];
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100322 size_t len;
323
David Howellsa2f611a2019-05-01 15:58:24 +0100324 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
325 * uniquifier is 32-bit.
326 */
Mark Salyzyna8626382019-11-05 07:43:49 -0800327 len = scnprintf(text, sizeof(text), "%llx:", vnode->fid.vid);
David Howellsa2f611a2019-05-01 15:58:24 +0100328 if (vnode->fid.vnode_hi)
Mark Salyzyna8626382019-11-05 07:43:49 -0800329 len += scnprintf(text + len, sizeof(text) - len, "%x%016llx",
330 vnode->fid.vnode_hi, vnode->fid.vnode);
David Howellsa2f611a2019-05-01 15:58:24 +0100331 else
Mark Salyzyna8626382019-11-05 07:43:49 -0800332 len += scnprintf(text + len, sizeof(text) - len, "%llx",
333 vnode->fid.vnode);
334 len += scnprintf(text + len, sizeof(text) - len, ":%x",
335 vnode->fid.unique);
David Howellsa2f611a2019-05-01 15:58:24 +0100336
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100337 if (size == 0)
338 return len;
339 if (len > size)
340 return -ERANGE;
341 memcpy(buffer, text, len);
342 return len;
343}
344
345static const struct xattr_handler afs_xattr_afs_fid_handler = {
346 .name = "afs.fid",
347 .get = afs_xattr_get_fid,
348};
349
350/*
351 * Get the name of the volume on which a file resides.
352 */
353static int afs_xattr_get_volume(const struct xattr_handler *handler,
354 struct dentry *dentry,
355 struct inode *inode, const char *name,
Mark Salyzyn3484eba2019-11-04 08:57:10 -0800356 void *buffer, size_t size, int flags)
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100357{
358 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howellsd2ddc772017-11-02 15:27:50 +0000359 const char *volname = vnode->volume->name;
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100360 size_t namelen;
361
362 namelen = strlen(volname);
363 if (size == 0)
364 return namelen;
365 if (namelen > size)
366 return -ERANGE;
David Howellsc73aa412019-05-01 13:27:09 +0100367 memcpy(buffer, volname, namelen);
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100368 return namelen;
369}
370
371static const struct xattr_handler afs_xattr_afs_volume_handler = {
372 .name = "afs.volume",
373 .get = afs_xattr_get_volume,
374};
375
376const struct xattr_handler *afs_xattr_handlers[] = {
David Howells260f0822019-04-25 14:26:52 +0100377 &afs_xattr_afs_acl_handler,
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100378 &afs_xattr_afs_cell_handler,
379 &afs_xattr_afs_fid_handler,
380 &afs_xattr_afs_volume_handler,
David Howellsae465782019-04-30 18:30:21 +0100381 &afs_xattr_yfs_handler, /* afs.yfs. prefix */
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100382 NULL
383};