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>
15 * Deal with the result of a successful fetch ACL operation.
17 static void afs_acl_success(struct afs_operation *op)
19 afs_vnode_commit_status(op, &op->file[0]);
22 static void afs_acl_put(struct afs_operation *op)
27 static const struct afs_operation_ops afs_fetch_acl_operation = {
28 .issue_afs_rpc = afs_fs_fetch_acl,
29 .success = afs_acl_success,
36 static int afs_xattr_get_acl(const struct xattr_handler *handler,
37 struct dentry *dentry,
38 struct inode *inode, const char *name,
39 void *buffer, size_t size)
41 struct afs_operation *op;
42 struct afs_vnode *vnode = AFS_FS_I(inode);
43 struct afs_acl *acl = NULL;
46 op = afs_alloc_operation(NULL, vnode->volume);
50 afs_op_set_vnode(op, 0, vnode);
51 op->ops = &afs_fetch_acl_operation;
53 afs_begin_vnode_operation(op);
54 afs_wait_for_operation(op);
57 ret = afs_put_operation(op);
62 if (acl->size <= size)
63 memcpy(buffer, acl->data, acl->size);
73 static bool afs_make_acl(struct afs_operation *op,
74 const void *buffer, size_t size)
78 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
85 memcpy(acl->data, buffer, size);
90 static const struct afs_operation_ops afs_store_acl_operation = {
91 .issue_afs_rpc = afs_fs_store_acl,
92 .success = afs_acl_success,
97 * Set a file's AFS3 ACL.
99 static int afs_xattr_set_acl(const struct xattr_handler *handler,
100 struct dentry *dentry,
101 struct inode *inode, const char *name,
102 const void *buffer, size_t size, int flags)
104 struct afs_operation *op;
105 struct afs_vnode *vnode = AFS_FS_I(inode);
107 if (flags == XATTR_CREATE)
110 op = afs_alloc_operation(NULL, vnode->volume);
114 afs_op_set_vnode(op, 0, vnode);
115 if (!afs_make_acl(op, buffer, size))
116 return afs_put_operation(op);
118 op->ops = &afs_store_acl_operation;
119 return afs_do_sync_operation(op);
122 static const struct xattr_handler afs_xattr_afs_acl_handler = {
124 .get = afs_xattr_get_acl,
125 .set = afs_xattr_set_acl,
128 static const struct afs_operation_ops yfs_fetch_opaque_acl_operation = {
129 .issue_yfs_rpc = yfs_fs_fetch_opaque_acl,
130 .success = afs_acl_success,
131 /* Don't free op->yacl in .put here */
135 * Get a file's YFS ACL.
137 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
138 struct dentry *dentry,
139 struct inode *inode, const char *name,
140 void *buffer, size_t size)
142 struct afs_operation *op;
143 struct afs_vnode *vnode = AFS_FS_I(inode);
144 struct yfs_acl *yacl = NULL;
146 int which = 0, dsize, ret = -ENOMEM;
148 if (strcmp(name, "acl") == 0)
150 else if (strcmp(name, "acl_inherited") == 0)
152 else if (strcmp(name, "acl_num_cleaned") == 0)
154 else if (strcmp(name, "vol_acl") == 0)
159 yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
164 yacl->flags |= YFS_ACL_WANT_ACL;
166 yacl->flags |= YFS_ACL_WANT_VOL_ACL;
168 op = afs_alloc_operation(NULL, vnode->volume);
172 afs_op_set_vnode(op, 0, vnode);
174 op->ops = &yfs_fetch_opaque_acl_operation;
176 afs_begin_vnode_operation(op);
177 afs_wait_for_operation(op);
178 ret = afs_put_operation(op);
183 data = yacl->acl->data;
184 dsize = yacl->acl->size;
188 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
192 dsize = scnprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
195 data = yacl->vol_acl->data;
196 dsize = yacl->vol_acl->size;
206 memcpy(buffer, data, dsize);
210 } else if (ret == -ENOTSUPP) {
215 yfs_free_opaque_acl(yacl);
220 static const struct afs_operation_ops yfs_store_opaque_acl2_operation = {
221 .issue_yfs_rpc = yfs_fs_store_opaque_acl2,
222 .success = afs_acl_success,
227 * Set a file's YFS ACL.
229 static int afs_xattr_set_yfs(const struct xattr_handler *handler,
230 struct dentry *dentry,
231 struct inode *inode, const char *name,
232 const void *buffer, size_t size, int flags)
234 struct afs_operation *op;
235 struct afs_vnode *vnode = AFS_FS_I(inode);
238 if (flags == XATTR_CREATE ||
239 strcmp(name, "acl") != 0)
242 op = afs_alloc_operation(NULL, vnode->volume);
246 afs_op_set_vnode(op, 0, vnode);
247 if (!afs_make_acl(op, buffer, size))
248 return afs_put_operation(op);
250 op->ops = &yfs_store_opaque_acl2_operation;
251 ret = afs_do_sync_operation(op);
252 if (ret == -ENOTSUPP)
257 static const struct xattr_handler afs_xattr_yfs_handler = {
258 .prefix = "afs.yfs.",
259 .get = afs_xattr_get_yfs,
260 .set = afs_xattr_set_yfs,
264 * Get the name of the cell on which a file resides.
266 static int afs_xattr_get_cell(const struct xattr_handler *handler,
267 struct dentry *dentry,
268 struct inode *inode, const char *name,
269 void *buffer, size_t size)
271 struct afs_vnode *vnode = AFS_FS_I(inode);
272 struct afs_cell *cell = vnode->volume->cell;
275 namelen = cell->name_len;
280 memcpy(buffer, cell->name, namelen);
284 static const struct xattr_handler afs_xattr_afs_cell_handler = {
286 .get = afs_xattr_get_cell,
290 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
291 * hex numbers separated by colons.
293 static int afs_xattr_get_fid(const struct xattr_handler *handler,
294 struct dentry *dentry,
295 struct inode *inode, const char *name,
296 void *buffer, size_t size)
298 struct afs_vnode *vnode = AFS_FS_I(inode);
299 char text[16 + 1 + 24 + 1 + 8 + 1];
302 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
303 * uniquifier is 32-bit.
305 len = scnprintf(text, sizeof(text), "%llx:", vnode->fid.vid);
306 if (vnode->fid.vnode_hi)
307 len += scnprintf(text + len, sizeof(text) - len, "%x%016llx",
308 vnode->fid.vnode_hi, vnode->fid.vnode);
310 len += scnprintf(text + len, sizeof(text) - len, "%llx",
312 len += scnprintf(text + len, sizeof(text) - len, ":%x",
319 memcpy(buffer, text, len);
323 static const struct xattr_handler afs_xattr_afs_fid_handler = {
325 .get = afs_xattr_get_fid,
329 * Get the name of the volume on which a file resides.
331 static int afs_xattr_get_volume(const struct xattr_handler *handler,
332 struct dentry *dentry,
333 struct inode *inode, const char *name,
334 void *buffer, size_t size)
336 struct afs_vnode *vnode = AFS_FS_I(inode);
337 const char *volname = vnode->volume->name;
340 namelen = strlen(volname);
345 memcpy(buffer, volname, namelen);
349 static const struct xattr_handler afs_xattr_afs_volume_handler = {
350 .name = "afs.volume",
351 .get = afs_xattr_get_volume,
354 const struct xattr_handler *afs_xattr_handlers[] = {
355 &afs_xattr_afs_acl_handler,
356 &afs_xattr_afs_cell_handler,
357 &afs_xattr_afs_fid_handler,
358 &afs_xattr_afs_volume_handler,
359 &afs_xattr_yfs_handler, /* afs.yfs. prefix */