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.
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_status_cb *scb;
51 struct afs_vnode *vnode = AFS_FS_I(inode);
52 struct afs_acl *acl = NULL;
56 scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS);
60 key = afs_request_key(vnode->volume->cell);
67 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
68 afs_dataversion_t data_version = vnode->status.data_version;
70 while (afs_select_fileserver(&fc)) {
71 fc.cb_break = afs_calc_vnode_cb_break(vnode);
72 acl = afs_fs_fetch_acl(&fc, scb);
75 afs_check_for_remote_deletion(&fc, fc.vnode);
76 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
78 ret = afs_end_vnode_operation(&fc);
84 if (acl->size <= size)
85 memcpy(buffer, acl->data, acl->size);
100 * Set a file's AFS3 ACL.
102 static int afs_xattr_set_acl(const struct xattr_handler *handler,
103 struct dentry *dentry,
104 struct inode *inode, const char *name,
105 const void *buffer, size_t size, int flags)
107 struct afs_fs_cursor fc;
108 struct afs_status_cb *scb;
109 struct afs_vnode *vnode = AFS_FS_I(inode);
110 struct afs_acl *acl = NULL;
114 if (flags == XATTR_CREATE)
117 scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS);
121 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
125 key = afs_request_key(vnode->volume->cell);
132 memcpy(acl->data, buffer, size);
135 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
136 afs_dataversion_t data_version = vnode->status.data_version;
138 while (afs_select_fileserver(&fc)) {
139 fc.cb_break = afs_calc_vnode_cb_break(vnode);
140 afs_fs_store_acl(&fc, acl, scb);
143 afs_check_for_remote_deletion(&fc, fc.vnode);
144 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
146 ret = afs_end_vnode_operation(&fc);
158 static const struct xattr_handler afs_xattr_afs_acl_handler = {
160 .get = afs_xattr_get_acl,
161 .set = afs_xattr_set_acl,
165 * Get a file's YFS ACL.
167 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
168 struct dentry *dentry,
169 struct inode *inode, const char *name,
170 void *buffer, size_t size)
172 struct afs_fs_cursor fc;
173 struct afs_status_cb *scb;
174 struct afs_vnode *vnode = AFS_FS_I(inode);
175 struct yfs_acl *yacl = NULL;
178 int which = 0, dsize, ret = -ENOMEM;
180 if (strcmp(name, "acl") == 0)
182 else if (strcmp(name, "acl_inherited") == 0)
184 else if (strcmp(name, "acl_num_cleaned") == 0)
186 else if (strcmp(name, "vol_acl") == 0)
191 yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
196 yacl->flags |= YFS_ACL_WANT_ACL;
198 yacl->flags |= YFS_ACL_WANT_VOL_ACL;
200 scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS);
204 key = afs_request_key(vnode->volume->cell);
211 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
212 afs_dataversion_t data_version = vnode->status.data_version;
214 while (afs_select_fileserver(&fc)) {
215 fc.cb_break = afs_calc_vnode_cb_break(vnode);
216 yfs_fs_fetch_opaque_acl(&fc, yacl, scb);
219 afs_check_for_remote_deletion(&fc, fc.vnode);
220 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
222 ret = afs_end_vnode_operation(&fc);
230 data = yacl->acl->data;
231 dsize = yacl->acl->size;
235 dsize = snprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
239 dsize = snprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
242 data = yacl->vol_acl->data;
243 dsize = yacl->vol_acl->size;
256 memcpy(buffer, data, dsize);
264 yfs_free_opaque_acl(yacl);
270 * Set a file's YFS ACL.
272 static int afs_xattr_set_yfs(const struct xattr_handler *handler,
273 struct dentry *dentry,
274 struct inode *inode, const char *name,
275 const void *buffer, size_t size, int flags)
277 struct afs_fs_cursor fc;
278 struct afs_status_cb *scb;
279 struct afs_vnode *vnode = AFS_FS_I(inode);
280 struct afs_acl *acl = NULL;
284 if (flags == XATTR_CREATE ||
285 strcmp(name, "acl") != 0)
288 scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS);
292 acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
297 memcpy(acl->data, buffer, size);
299 key = afs_request_key(vnode->volume->cell);
306 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
307 afs_dataversion_t data_version = vnode->status.data_version;
309 while (afs_select_fileserver(&fc)) {
310 fc.cb_break = afs_calc_vnode_cb_break(vnode);
311 yfs_fs_store_opaque_acl2(&fc, acl, scb);
314 afs_check_for_remote_deletion(&fc, fc.vnode);
315 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
317 ret = afs_end_vnode_operation(&fc);
329 static const struct xattr_handler afs_xattr_yfs_handler = {
330 .prefix = "afs.yfs.",
331 .get = afs_xattr_get_yfs,
332 .set = afs_xattr_set_yfs,
336 * Get the name of the cell on which a file resides.
338 static int afs_xattr_get_cell(const struct xattr_handler *handler,
339 struct dentry *dentry,
340 struct inode *inode, const char *name,
341 void *buffer, size_t size)
343 struct afs_vnode *vnode = AFS_FS_I(inode);
344 struct afs_cell *cell = vnode->volume->cell;
347 namelen = cell->name_len;
352 memcpy(buffer, cell->name, namelen);
356 static const struct xattr_handler afs_xattr_afs_cell_handler = {
358 .get = afs_xattr_get_cell,
362 * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
363 * hex numbers separated by colons.
365 static int afs_xattr_get_fid(const struct xattr_handler *handler,
366 struct dentry *dentry,
367 struct inode *inode, const char *name,
368 void *buffer, size_t size)
370 struct afs_vnode *vnode = AFS_FS_I(inode);
371 char text[16 + 1 + 24 + 1 + 8 + 1];
374 /* The volume ID is 64-bit, the vnode ID is 96-bit and the
375 * uniquifier is 32-bit.
377 len = sprintf(text, "%llx:", vnode->fid.vid);
378 if (vnode->fid.vnode_hi)
379 len += sprintf(text + len, "%x%016llx",
380 vnode->fid.vnode_hi, vnode->fid.vnode);
382 len += sprintf(text + len, "%llx", vnode->fid.vnode);
383 len += sprintf(text + len, ":%x", vnode->fid.unique);
389 memcpy(buffer, text, len);
393 static const struct xattr_handler afs_xattr_afs_fid_handler = {
395 .get = afs_xattr_get_fid,
399 * Get the name of the volume on which a file resides.
401 static int afs_xattr_get_volume(const struct xattr_handler *handler,
402 struct dentry *dentry,
403 struct inode *inode, const char *name,
404 void *buffer, size_t size)
406 struct afs_vnode *vnode = AFS_FS_I(inode);
407 const char *volname = vnode->volume->name;
410 namelen = strlen(volname);
415 memcpy(buffer, volname, namelen);
419 static const struct xattr_handler afs_xattr_afs_volume_handler = {
420 .name = "afs.volume",
421 .get = afs_xattr_get_volume,
424 const struct xattr_handler *afs_xattr_handlers[] = {
425 &afs_xattr_afs_acl_handler,
426 &afs_xattr_afs_cell_handler,
427 &afs_xattr_afs_fid_handler,
428 &afs_xattr_afs_volume_handler,
429 &afs_xattr_yfs_handler, /* afs.yfs. prefix */