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.
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 user_namespace *mnt_userns,
124 struct dentry *dentry,
125 struct inode *inode, const char *name,
126 const void *buffer, size_t size, int flags)
128 struct afs_operation *op;
129 struct afs_vnode *vnode = AFS_FS_I(inode);
131 if (flags == XATTR_CREATE)
134 op = afs_alloc_operation(NULL, vnode->volume);
138 afs_op_set_vnode(op, 0, vnode);
139 if (!afs_make_acl(op, buffer, size))
140 return afs_put_operation(op);
142 op->ops = &afs_store_acl_operation;
143 return afs_do_sync_operation(op);
146 static const struct xattr_handler afs_xattr_afs_acl_handler = {
148 .get = afs_xattr_get_acl,
149 .set = afs_xattr_set_acl,
152 static const struct afs_operation_ops yfs_fetch_opaque_acl_operation = {
153 .issue_yfs_rpc = yfs_fs_fetch_opaque_acl,
154 .success = afs_acl_success,
155 /* Don't free op->yacl in .put here */
159 * Get a file's YFS ACL.
161 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
162 struct dentry *dentry,
163 struct inode *inode, const char *name,
164 void *buffer, size_t size)
166 struct afs_operation *op;
167 struct afs_vnode *vnode = AFS_FS_I(inode);
168 struct yfs_acl *yacl = NULL;
170 int which = 0, dsize, ret = -ENOMEM;
172 if (strcmp(name, "acl") == 0)
174 else if (strcmp(name, "acl_inherited") == 0)
176 else if (strcmp(name, "acl_num_cleaned") == 0)
178 else if (strcmp(name, "vol_acl") == 0)
183 yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
188 yacl->flags |= YFS_ACL_WANT_ACL;
190 yacl->flags |= YFS_ACL_WANT_VOL_ACL;
192 op = afs_alloc_operation(NULL, vnode->volume);
196 afs_op_set_vnode(op, 0, vnode);
198 op->ops = &yfs_fetch_opaque_acl_operation;
200 afs_begin_vnode_operation(op);
201 afs_wait_for_operation(op);
202 ret = afs_put_operation(op);
207 data = yacl->acl->data;
208 dsize = yacl->acl->size;
212 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
216 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
219 data = yacl->vol_acl->data;
220 dsize = yacl->vol_acl->size;
230 memcpy(buffer, data, dsize);
237 yfs_free_opaque_acl(yacl);
242 static const struct afs_operation_ops yfs_store_opaque_acl2_operation = {
243 .issue_yfs_rpc = yfs_fs_store_opaque_acl2,
244 .success = afs_acl_success,
249 * Set a file's YFS ACL.
251 static int afs_xattr_set_yfs(const struct xattr_handler *handler,
252 struct user_namespace *mnt_userns,
253 struct dentry *dentry,
254 struct inode *inode, const char *name,
255 const void *buffer, size_t size, int flags)
257 struct afs_operation *op;
258 struct afs_vnode *vnode = AFS_FS_I(inode);
260 if (flags == XATTR_CREATE ||
261 strcmp(name, "acl") != 0)
264 op = afs_alloc_operation(NULL, vnode->volume);
268 afs_op_set_vnode(op, 0, vnode);
269 if (!afs_make_acl(op, buffer, size))
270 return afs_put_operation(op);
272 op->ops = &yfs_store_opaque_acl2_operation;
273 return afs_do_sync_operation(op);
276 static const struct xattr_handler afs_xattr_yfs_handler = {
277 .prefix = "afs.yfs.",
278 .get = afs_xattr_get_yfs,
279 .set = afs_xattr_set_yfs,
283 * Get the name of the cell on which a file resides.
285 static int afs_xattr_get_cell(const struct xattr_handler *handler,
286 struct dentry *dentry,
287 struct inode *inode, const char *name,
288 void *buffer, size_t size)
290 struct afs_vnode *vnode = AFS_FS_I(inode);
291 struct afs_cell *cell = vnode->volume->cell;
294 namelen = cell->name_len;
299 memcpy(buffer, cell->name, namelen);
303 static const struct xattr_handler afs_xattr_afs_cell_handler = {
305 .get = afs_xattr_get_cell,
309 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
310 * hex numbers separated by colons.
312 static int afs_xattr_get_fid(const struct xattr_handler *handler,
313 struct dentry *dentry,
314 struct inode *inode, const char *name,
315 void *buffer, size_t size)
317 struct afs_vnode *vnode = AFS_FS_I(inode);
318 char text[16 + 1 + 24 + 1 + 8 + 1];
321 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
322 * uniquifier is 32-bit.
324 len = scnprintf(text, sizeof(text), "%llx:", vnode->fid.vid);
325 if (vnode->fid.vnode_hi)
326 len += scnprintf(text + len, sizeof(text) - len, "%x%016llx",
327 vnode->fid.vnode_hi, vnode->fid.vnode);
329 len += scnprintf(text + len, sizeof(text) - len, "%llx",
331 len += scnprintf(text + len, sizeof(text) - len, ":%x",
338 memcpy(buffer, text, len);
342 static const struct xattr_handler afs_xattr_afs_fid_handler = {
344 .get = afs_xattr_get_fid,
348 * Get the name of the volume on which a file resides.
350 static int afs_xattr_get_volume(const struct xattr_handler *handler,
351 struct dentry *dentry,
352 struct inode *inode, const char *name,
353 void *buffer, size_t size)
355 struct afs_vnode *vnode = AFS_FS_I(inode);
356 const char *volname = vnode->volume->name;
359 namelen = strlen(volname);
364 memcpy(buffer, volname, namelen);
368 static const struct xattr_handler afs_xattr_afs_volume_handler = {
369 .name = "afs.volume",
370 .get = afs_xattr_get_volume,
373 const struct xattr_handler *afs_xattr_handlers[] = {
374 &afs_xattr_afs_acl_handler,
375 &afs_xattr_afs_cell_handler,
376 &afs_xattr_afs_fid_handler,
377 &afs_xattr_afs_volume_handler,
378 &afs_xattr_yfs_handler, /* afs.yfs. prefix */