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);
74 if (acl->size <= size)
75 memcpy(buffer, acl->data, acl->size);
87 * Set a file's AFS3 ACL.
89 static int afs_xattr_set_acl(const struct xattr_handler *handler,
90 struct dentry *dentry,
91 struct inode *inode, const char *name,
92 const void *buffer, size_t size, int flags)
94 struct afs_fs_cursor fc;
95 struct afs_vnode *vnode = AFS_FS_I(inode);
96 struct afs_acl *acl = NULL;
100 if (flags == XATTR_CREATE)
103 key = afs_request_key(vnode->volume->cell);
107 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
114 memcpy(acl->data, buffer, size);
117 if (afs_begin_vnode_operation(&fc, vnode, key)) {
118 while (afs_select_fileserver(&fc)) {
119 fc.cb_break = afs_calc_vnode_cb_break(vnode);
120 afs_fs_store_acl(&fc, acl);
123 afs_check_for_remote_deletion(&fc, fc.vnode);
124 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
125 ret = afs_end_vnode_operation(&fc);
133 static const struct xattr_handler afs_xattr_afs_acl_handler = {
135 .get = afs_xattr_get_acl,
136 .set = afs_xattr_set_acl,
140 * Get a file's YFS ACL.
142 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
143 struct dentry *dentry,
144 struct inode *inode, const char *name,
145 void *buffer, size_t size)
147 struct afs_fs_cursor fc;
148 struct afs_vnode *vnode = AFS_FS_I(inode);
149 struct yfs_acl *yacl = NULL;
151 unsigned int flags = 0;
153 int which = 0, dsize, ret;
155 if (strcmp(name, "acl") == 0)
157 else if (strcmp(name, "acl_inherited") == 0)
159 else if (strcmp(name, "acl_num_cleaned") == 0)
161 else if (strcmp(name, "vol_acl") == 0)
167 flags |= YFS_ACL_WANT_ACL;
169 flags |= YFS_ACL_WANT_VOL_ACL;
171 key = afs_request_key(vnode->volume->cell);
176 if (afs_begin_vnode_operation(&fc, vnode, key)) {
177 while (afs_select_fileserver(&fc)) {
178 fc.cb_break = afs_calc_vnode_cb_break(vnode);
179 yacl = yfs_fs_fetch_opaque_acl(&fc, flags);
182 afs_check_for_remote_deletion(&fc, fc.vnode);
183 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
184 ret = afs_end_vnode_operation(&fc);
190 data = yacl->acl->data;
191 dsize = yacl->acl->size;
195 dsize = snprintf(buf, sizeof(buf), "%u",
200 dsize = snprintf(buf, sizeof(buf), "%u",
204 data = yacl->vol_acl->data;
205 dsize = yacl->vol_acl->size;
218 memcpy(buffer, data, dsize);
223 yfs_free_opaque_acl(yacl);
229 * Set a file's YFS ACL.
231 static int afs_xattr_set_yfs(const struct xattr_handler *handler,
232 struct dentry *dentry,
233 struct inode *inode, const char *name,
234 const void *buffer, size_t size, int flags)
236 struct afs_fs_cursor fc;
237 struct afs_vnode *vnode = AFS_FS_I(inode);
238 struct afs_acl *acl = NULL;
242 if (flags == XATTR_CREATE ||
243 strcmp(name, "acl") != 0)
246 key = afs_request_key(vnode->volume->cell);
250 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
257 memcpy(acl->data, buffer, size);
260 if (afs_begin_vnode_operation(&fc, vnode, key)) {
261 while (afs_select_fileserver(&fc)) {
262 fc.cb_break = afs_calc_vnode_cb_break(vnode);
263 yfs_fs_store_opaque_acl2(&fc, acl);
266 afs_check_for_remote_deletion(&fc, fc.vnode);
267 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
268 ret = afs_end_vnode_operation(&fc);
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 = sprintf(text, "%llx:", vnode->fid.vid);
325 if (vnode->fid.vnode_hi)
326 len += sprintf(text + len, "%x%016llx",
327 vnode->fid.vnode_hi, vnode->fid.vnode);
329 len += sprintf(text + len, "%llx", vnode->fid.vnode);
330 len += sprintf(text + len, ":%x", vnode->fid.unique);
336 memcpy(buffer, text, len);
340 static const struct xattr_handler afs_xattr_afs_fid_handler = {
342 .get = afs_xattr_get_fid,
346 * Get the name of the volume on which a file resides.
348 static int afs_xattr_get_volume(const struct xattr_handler *handler,
349 struct dentry *dentry,
350 struct inode *inode, const char *name,
351 void *buffer, size_t size)
353 struct afs_vnode *vnode = AFS_FS_I(inode);
354 const char *volname = vnode->volume->name;
357 namelen = strlen(volname);
362 memcpy(buffer, volname, namelen);
366 static const struct xattr_handler afs_xattr_afs_volume_handler = {
367 .name = "afs.volume",
368 .get = afs_xattr_get_volume,
371 const struct xattr_handler *afs_xattr_handlers[] = {
372 &afs_xattr_afs_acl_handler,
373 &afs_xattr_afs_cell_handler,
374 &afs_xattr_afs_fid_handler,
375 &afs_xattr_afs_volume_handler,
376 &afs_xattr_yfs_handler, /* afs.yfs. prefix */