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[] =
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);
40 static int afs_xattr_get_acl(const struct xattr_handler *handler,
41 struct dentry *dentry,
42 struct inode *inode, const char *name,
43 void *buffer, size_t size)
45 struct afs_fs_cursor fc;
46 struct afs_vnode *vnode = AFS_FS_I(inode);
47 struct afs_acl *acl = NULL;
51 key = afs_request_key(vnode->volume->cell);
56 if (afs_begin_vnode_operation(&fc, vnode, key)) {
57 while (afs_select_fileserver(&fc)) {
58 fc.cb_break = afs_calc_vnode_cb_break(vnode);
59 acl = afs_fs_fetch_acl(&fc);
62 afs_check_for_remote_deletion(&fc, fc.vnode);
63 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
64 ret = afs_end_vnode_operation(&fc);
73 memcpy(buffer, acl->data, acl->size);
84 * Set a file's AFS3 ACL.
86 static int afs_xattr_set_acl(const struct xattr_handler *handler,
87 struct dentry *dentry,
88 struct inode *inode, const char *name,
89 const void *buffer, size_t size, int flags)
91 struct afs_fs_cursor fc;
92 struct afs_vnode *vnode = AFS_FS_I(inode);
93 struct afs_acl *acl = NULL;
97 if (flags == XATTR_CREATE)
100 key = afs_request_key(vnode->volume->cell);
104 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
111 memcpy(acl->data, buffer, size);
114 if (afs_begin_vnode_operation(&fc, vnode, key)) {
115 while (afs_select_fileserver(&fc)) {
116 fc.cb_break = afs_calc_vnode_cb_break(vnode);
117 afs_fs_store_acl(&fc, acl);
120 afs_check_for_remote_deletion(&fc, fc.vnode);
121 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
122 ret = afs_end_vnode_operation(&fc);
130 static const struct xattr_handler afs_xattr_afs_acl_handler = {
132 .get = afs_xattr_get_acl,
133 .set = afs_xattr_set_acl,
137 * Get the name of the cell on which a file resides.
139 static int afs_xattr_get_cell(const struct xattr_handler *handler,
140 struct dentry *dentry,
141 struct inode *inode, const char *name,
142 void *buffer, size_t size)
144 struct afs_vnode *vnode = AFS_FS_I(inode);
145 struct afs_cell *cell = vnode->volume->cell;
148 namelen = cell->name_len;
153 memcpy(buffer, cell->name, namelen);
157 static const struct xattr_handler afs_xattr_afs_cell_handler = {
159 .get = afs_xattr_get_cell,
163 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
164 * hex numbers separated by colons.
166 static int afs_xattr_get_fid(const struct xattr_handler *handler,
167 struct dentry *dentry,
168 struct inode *inode, const char *name,
169 void *buffer, size_t size)
171 struct afs_vnode *vnode = AFS_FS_I(inode);
172 char text[16 + 1 + 24 + 1 + 8 + 1];
175 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
176 * uniquifier is 32-bit.
178 len = sprintf(text, "%llx:", vnode->fid.vid);
179 if (vnode->fid.vnode_hi)
180 len += sprintf(text + len, "%x%016llx",
181 vnode->fid.vnode_hi, vnode->fid.vnode);
183 len += sprintf(text + len, "%llx", vnode->fid.vnode);
184 len += sprintf(text + len, ":%x", vnode->fid.unique);
190 memcpy(buffer, text, len);
194 static const struct xattr_handler afs_xattr_afs_fid_handler = {
196 .get = afs_xattr_get_fid,
200 * Get the name of the volume on which a file resides.
202 static int afs_xattr_get_volume(const struct xattr_handler *handler,
203 struct dentry *dentry,
204 struct inode *inode, const char *name,
205 void *buffer, size_t size)
207 struct afs_vnode *vnode = AFS_FS_I(inode);
208 const char *volname = vnode->volume->name;
211 namelen = strlen(volname);
216 memcpy(buffer, volname, namelen);
220 static const struct xattr_handler afs_xattr_afs_volume_handler = {
221 .name = "afs.volume",
222 .get = afs_xattr_get_volume,
225 const struct xattr_handler *afs_xattr_handlers[] = {
226 &afs_xattr_afs_acl_handler,
227 &afs_xattr_afs_cell_handler,
228 &afs_xattr_afs_fid_handler,
229 &afs_xattr_afs_volume_handler,