1 /* Extended attribute handling for AFS. We use xattrs to get and set metadata
2 * instead of providing pioctl().
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
13 #include <linux/slab.h>
15 #include <linux/xattr.h>
18 static const char afs_xattr_list[] =
24 "afs.yfs.acl_inherited\0"
25 "afs.yfs.acl_num_cleaned\0"
29 * Retrieve a list of the supported xattrs.
31 ssize_t afs_listxattr(struct dentry *dentry, char *buffer, size_t size)
34 return sizeof(afs_xattr_list);
35 if (size < sizeof(afs_xattr_list))
37 memcpy(buffer, afs_xattr_list, sizeof(afs_xattr_list));
38 return sizeof(afs_xattr_list);
44 static int afs_xattr_get_acl(const struct xattr_handler *handler,
45 struct dentry *dentry,
46 struct inode *inode, const char *name,
47 void *buffer, size_t size)
49 struct afs_fs_cursor fc;
50 struct afs_vnode *vnode = AFS_FS_I(inode);
51 struct afs_acl *acl = NULL;
55 key = afs_request_key(vnode->volume->cell);
60 if (afs_begin_vnode_operation(&fc, vnode, key)) {
61 while (afs_select_fileserver(&fc)) {
62 fc.cb_break = afs_calc_vnode_cb_break(vnode);
63 acl = afs_fs_fetch_acl(&fc);
66 afs_check_for_remote_deletion(&fc, fc.vnode);
67 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
68 ret = afs_end_vnode_operation(&fc);
77 memcpy(buffer, acl->data, acl->size);
88 * Set a file's AFS3 ACL.
90 static int afs_xattr_set_acl(const struct xattr_handler *handler,
91 struct dentry *dentry,
92 struct inode *inode, const char *name,
93 const void *buffer, size_t size, int flags)
95 struct afs_fs_cursor fc;
96 struct afs_vnode *vnode = AFS_FS_I(inode);
97 struct afs_acl *acl = NULL;
101 if (flags == XATTR_CREATE)
104 key = afs_request_key(vnode->volume->cell);
108 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
115 memcpy(acl->data, buffer, size);
118 if (afs_begin_vnode_operation(&fc, vnode, key)) {
119 while (afs_select_fileserver(&fc)) {
120 fc.cb_break = afs_calc_vnode_cb_break(vnode);
121 afs_fs_store_acl(&fc, acl);
124 afs_check_for_remote_deletion(&fc, fc.vnode);
125 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
126 ret = afs_end_vnode_operation(&fc);
134 static const struct xattr_handler afs_xattr_afs_acl_handler = {
136 .get = afs_xattr_get_acl,
137 .set = afs_xattr_set_acl,
141 * Get a file's YFS ACL.
143 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
144 struct dentry *dentry,
145 struct inode *inode, const char *name,
146 void *buffer, size_t size)
148 struct afs_fs_cursor fc;
149 struct afs_vnode *vnode = AFS_FS_I(inode);
150 struct yfs_acl *yacl = NULL;
152 unsigned int flags = 0;
154 int which = 0, dsize, ret;
156 if (strcmp(name, "acl") == 0)
158 else if (strcmp(name, "acl_inherited") == 0)
160 else if (strcmp(name, "acl_num_cleaned") == 0)
162 else if (strcmp(name, "vol_acl") == 0)
168 flags |= YFS_ACL_WANT_ACL;
170 flags |= YFS_ACL_WANT_VOL_ACL;
172 key = afs_request_key(vnode->volume->cell);
177 if (afs_begin_vnode_operation(&fc, vnode, key)) {
178 while (afs_select_fileserver(&fc)) {
179 fc.cb_break = afs_calc_vnode_cb_break(vnode);
180 yacl = yfs_fs_fetch_opaque_acl(&fc, flags);
183 afs_check_for_remote_deletion(&fc, fc.vnode);
184 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
185 ret = afs_end_vnode_operation(&fc);
191 data = yacl->acl->data;
192 dsize = yacl->acl->size;
196 dsize = snprintf(buf, sizeof(buf), "%u",
201 dsize = snprintf(buf, sizeof(buf), "%u",
205 data = yacl->vol_acl->data;
206 dsize = yacl->vol_acl->size;
219 memcpy(buffer, data, dsize);
224 yfs_free_opaque_acl(yacl);
229 static const struct xattr_handler afs_xattr_yfs_handler = {
230 .prefix = "afs.yfs.",
231 .get = afs_xattr_get_yfs,
235 * Get the name of the cell on which a file resides.
237 static int afs_xattr_get_cell(const struct xattr_handler *handler,
238 struct dentry *dentry,
239 struct inode *inode, const char *name,
240 void *buffer, size_t size)
242 struct afs_vnode *vnode = AFS_FS_I(inode);
243 struct afs_cell *cell = vnode->volume->cell;
246 namelen = cell->name_len;
251 memcpy(buffer, cell->name, namelen);
255 static const struct xattr_handler afs_xattr_afs_cell_handler = {
257 .get = afs_xattr_get_cell,
261 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
262 * hex numbers separated by colons.
264 static int afs_xattr_get_fid(const struct xattr_handler *handler,
265 struct dentry *dentry,
266 struct inode *inode, const char *name,
267 void *buffer, size_t size)
269 struct afs_vnode *vnode = AFS_FS_I(inode);
270 char text[16 + 1 + 24 + 1 + 8 + 1];
273 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
274 * uniquifier is 32-bit.
276 len = sprintf(text, "%llx:", vnode->fid.vid);
277 if (vnode->fid.vnode_hi)
278 len += sprintf(text + len, "%x%016llx",
279 vnode->fid.vnode_hi, vnode->fid.vnode);
281 len += sprintf(text + len, "%llx", vnode->fid.vnode);
282 len += sprintf(text + len, ":%x", vnode->fid.unique);
288 memcpy(buffer, text, len);
292 static const struct xattr_handler afs_xattr_afs_fid_handler = {
294 .get = afs_xattr_get_fid,
298 * Get the name of the volume on which a file resides.
300 static int afs_xattr_get_volume(const struct xattr_handler *handler,
301 struct dentry *dentry,
302 struct inode *inode, const char *name,
303 void *buffer, size_t size)
305 struct afs_vnode *vnode = AFS_FS_I(inode);
306 const char *volname = vnode->volume->name;
309 namelen = strlen(volname);
314 memcpy(buffer, volname, namelen);
318 static const struct xattr_handler afs_xattr_afs_volume_handler = {
319 .name = "afs.volume",
320 .get = afs_xattr_get_volume,
323 const struct xattr_handler *afs_xattr_handlers[] = {
324 &afs_xattr_afs_acl_handler,
325 &afs_xattr_afs_cell_handler,
326 &afs_xattr_afs_fid_handler,
327 &afs_xattr_afs_volume_handler,
328 &afs_xattr_yfs_handler, /* afs.yfs. prefix */