1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Extended attribute handling for AFS. We use xattrs to get and set metadata
3 * instead of providing pioctl().
5 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/slab.h>
11 #include <linux/xattr.h>
14 static const char afs_xattr_list[] =
20 "afs.yfs.acl_inherited\0"
21 "afs.yfs.acl_num_cleaned\0"
25 * Retrieve a list of the supported xattrs.
27 ssize_t afs_listxattr(struct dentry *dentry, char *buffer, size_t size)
30 return sizeof(afs_xattr_list);
31 if (size < sizeof(afs_xattr_list))
33 memcpy(buffer, afs_xattr_list, sizeof(afs_xattr_list));
34 return sizeof(afs_xattr_list);
38 * Deal with the result of a successful fetch ACL operation.
40 static void afs_acl_success(struct afs_operation *op)
42 afs_vnode_commit_status(op, &op->file[0]);
45 static void afs_acl_put(struct afs_operation *op)
50 static const struct afs_operation_ops afs_fetch_acl_operation = {
51 .issue_afs_rpc = afs_fs_fetch_acl,
52 .success = afs_acl_success,
59 static int afs_xattr_get_acl(const struct xattr_handler *handler,
60 struct dentry *dentry,
61 struct inode *inode, const char *name,
62 void *buffer, size_t size)
64 struct afs_operation *op;
65 struct afs_vnode *vnode = AFS_FS_I(inode);
66 struct afs_acl *acl = NULL;
69 op = afs_alloc_operation(NULL, vnode->volume);
73 afs_op_set_vnode(op, 0, vnode);
74 op->ops = &afs_fetch_acl_operation;
76 afs_begin_vnode_operation(op);
77 afs_wait_for_operation(op);
80 ret = afs_put_operation(op);
85 if (acl->size <= size)
86 memcpy(buffer, acl->data, acl->size);
96 static bool afs_make_acl(struct afs_operation *op,
97 const void *buffer, size_t size)
101 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
108 memcpy(acl->data, buffer, size);
113 static const struct afs_operation_ops afs_store_acl_operation = {
114 .issue_afs_rpc = afs_fs_store_acl,
115 .success = afs_acl_success,
120 * Set a file's AFS3 ACL.
122 static 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)
127 struct afs_operation *op;
128 struct afs_vnode *vnode = AFS_FS_I(inode);
130 if (flags == XATTR_CREATE)
133 op = afs_alloc_operation(NULL, vnode->volume);
137 afs_op_set_vnode(op, 0, vnode);
138 if (!afs_make_acl(op, buffer, size))
139 return afs_put_operation(op);
141 op->ops = &afs_store_acl_operation;
142 return afs_do_sync_operation(op);
145 static const struct xattr_handler afs_xattr_afs_acl_handler = {
147 .get = afs_xattr_get_acl,
148 .set = afs_xattr_set_acl,
151 static void yfs_acl_put(struct afs_operation *op)
153 yfs_free_opaque_acl(op->yacl);
156 static 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 */
163 * Get a file's YFS ACL.
165 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
166 struct dentry *dentry,
167 struct inode *inode, const char *name,
168 void *buffer, size_t size)
170 struct afs_operation *op;
171 struct afs_vnode *vnode = AFS_FS_I(inode);
172 struct yfs_acl *yacl = NULL;
174 int which = 0, dsize, ret = -ENOMEM;
176 if (strcmp(name, "acl") == 0)
178 else if (strcmp(name, "acl_inherited") == 0)
180 else if (strcmp(name, "acl_num_cleaned") == 0)
182 else if (strcmp(name, "vol_acl") == 0)
187 yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
192 yacl->flags |= YFS_ACL_WANT_ACL;
194 yacl->flags |= YFS_ACL_WANT_VOL_ACL;
196 op = afs_alloc_operation(NULL, vnode->volume);
200 afs_op_set_vnode(op, 0, vnode);
202 op->ops = &yfs_fetch_opaque_acl_operation;
204 afs_begin_vnode_operation(op);
205 afs_wait_for_operation(op);
206 ret = afs_put_operation(op);
211 data = yacl->acl->data;
212 dsize = yacl->acl->size;
216 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
220 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
223 data = yacl->vol_acl->data;
224 dsize = yacl->vol_acl->size;
234 memcpy(buffer, data, dsize);
241 yfs_free_opaque_acl(yacl);
246 static const struct afs_operation_ops yfs_store_opaque_acl2_operation = {
247 .issue_yfs_rpc = yfs_fs_store_opaque_acl2,
248 .success = afs_acl_success,
253 * Set a file's YFS ACL.
255 static 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)
260 struct afs_operation *op;
261 struct afs_vnode *vnode = AFS_FS_I(inode);
263 if (flags == XATTR_CREATE ||
264 strcmp(name, "acl") != 0)
267 op = afs_alloc_operation(NULL, vnode->volume);
271 afs_op_set_vnode(op, 0, vnode);
272 if (!afs_make_acl(op, buffer, size))
273 return afs_put_operation(op);
275 op->ops = &yfs_store_opaque_acl2_operation;
276 return afs_do_sync_operation(op);
279 static const struct xattr_handler afs_xattr_yfs_handler = {
280 .prefix = "afs.yfs.",
281 .get = afs_xattr_get_yfs,
282 .set = afs_xattr_set_yfs,
286 * Get the name of the cell on which a file resides.
288 static int afs_xattr_get_cell(const struct xattr_handler *handler,
289 struct dentry *dentry,
290 struct inode *inode, const char *name,
291 void *buffer, size_t size)
293 struct afs_vnode *vnode = AFS_FS_I(inode);
294 struct afs_cell *cell = vnode->volume->cell;
297 namelen = cell->name_len;
302 memcpy(buffer, cell->name, namelen);
306 static const struct xattr_handler afs_xattr_afs_cell_handler = {
308 .get = afs_xattr_get_cell,
312 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
313 * hex numbers separated by colons.
315 static int afs_xattr_get_fid(const struct xattr_handler *handler,
316 struct dentry *dentry,
317 struct inode *inode, const char *name,
318 void *buffer, size_t size)
320 struct afs_vnode *vnode = AFS_FS_I(inode);
321 char text[16 + 1 + 24 + 1 + 8 + 1];
324 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
325 * uniquifier is 32-bit.
327 len = scnprintf(text, sizeof(text), "%llx:", vnode->fid.vid);
328 if (vnode->fid.vnode_hi)
329 len += scnprintf(text + len, sizeof(text) - len, "%x%016llx",
330 vnode->fid.vnode_hi, vnode->fid.vnode);
332 len += scnprintf(text + len, sizeof(text) - len, "%llx",
334 len += scnprintf(text + len, sizeof(text) - len, ":%x",
341 memcpy(buffer, text, len);
345 static const struct xattr_handler afs_xattr_afs_fid_handler = {
347 .get = afs_xattr_get_fid,
351 * Get the name of the volume on which a file resides.
353 static int afs_xattr_get_volume(const struct xattr_handler *handler,
354 struct dentry *dentry,
355 struct inode *inode, const char *name,
356 void *buffer, size_t size)
358 struct afs_vnode *vnode = AFS_FS_I(inode);
359 const char *volname = vnode->volume->name;
362 namelen = strlen(volname);
367 memcpy(buffer, volname, namelen);
371 static const struct xattr_handler afs_xattr_afs_volume_handler = {
372 .name = "afs.volume",
373 .get = afs_xattr_get_volume,
376 const struct xattr_handler *afs_xattr_handlers[] = {
377 &afs_xattr_afs_acl_handler,
378 &afs_xattr_afs_cell_handler,
379 &afs_xattr_afs_fid_handler,
380 &afs_xattr_afs_volume_handler,
381 &afs_xattr_yfs_handler, /* afs.yfs. prefix */