]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/afs/xattr.c
afs: Get YFS ACLs and information through xattrs
[linux.git] / fs / afs / xattr.c
1 /* Extended attribute handling for AFS.  We use xattrs to get and set metadata
2  * instead of providing pioctl().
3  *
4  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
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.
11  */
12
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 #include <linux/xattr.h>
16 #include "internal.h"
17
18 static const char afs_xattr_list[] =
19         "afs.acl\0"
20         "afs.cell\0"
21         "afs.fid\0"
22         "afs.volume\0"
23         "afs.yfs.acl\0"
24         "afs.yfs.acl_inherited\0"
25         "afs.yfs.acl_num_cleaned\0"
26         "afs.yfs.vol_acl";
27
28 /*
29  * Retrieve a list of the supported xattrs.
30  */
31 ssize_t afs_listxattr(struct dentry *dentry, char *buffer, size_t size)
32 {
33         if (size == 0)
34                 return sizeof(afs_xattr_list);
35         if (size < sizeof(afs_xattr_list))
36                 return -ERANGE;
37         memcpy(buffer, afs_xattr_list, sizeof(afs_xattr_list));
38         return sizeof(afs_xattr_list);
39 }
40
41 /*
42  * Get a file's ACL.
43  */
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)
48 {
49         struct afs_fs_cursor fc;
50         struct afs_vnode *vnode = AFS_FS_I(inode);
51         struct afs_acl *acl = NULL;
52         struct key *key;
53         int ret;
54
55         key = afs_request_key(vnode->volume->cell);
56         if (IS_ERR(key))
57                 return PTR_ERR(key);
58
59         ret = -ERESTARTSYS;
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);
64                 }
65
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);
69         }
70
71         if (ret == 0) {
72                 ret = acl->size;
73                 if (size > 0) {
74                         ret = -ERANGE;
75                         if (acl->size > size)
76                                 return -ERANGE;
77                         memcpy(buffer, acl->data, acl->size);
78                         ret = acl->size;
79                 }
80                 kfree(acl);
81         }
82
83         key_put(key);
84         return ret;
85 }
86
87 /*
88  * Set a file's AFS3 ACL.
89  */
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)
94 {
95         struct afs_fs_cursor fc;
96         struct afs_vnode *vnode = AFS_FS_I(inode);
97         struct afs_acl *acl = NULL;
98         struct key *key;
99         int ret;
100
101         if (flags == XATTR_CREATE)
102                 return -EINVAL;
103
104         key = afs_request_key(vnode->volume->cell);
105         if (IS_ERR(key))
106                 return PTR_ERR(key);
107
108         acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
109         if (!acl) {
110                 key_put(key);
111                 return -ENOMEM;
112         }
113
114         acl->size = size;
115         memcpy(acl->data, buffer, size);
116
117         ret = -ERESTARTSYS;
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);
122                 }
123
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);
127         }
128
129         kfree(acl);
130         key_put(key);
131         return ret;
132 }
133
134 static const struct xattr_handler afs_xattr_afs_acl_handler = {
135         .name   = "afs.acl",
136         .get    = afs_xattr_get_acl,
137         .set    = afs_xattr_set_acl,
138 };
139
140 /*
141  * Get a file's YFS ACL.
142  */
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)
147 {
148         struct afs_fs_cursor fc;
149         struct afs_vnode *vnode = AFS_FS_I(inode);
150         struct yfs_acl *yacl = NULL;
151         struct key *key;
152         unsigned int flags = 0;
153         char buf[16], *data;
154         int which = 0, dsize, ret;
155
156         if (strcmp(name, "acl") == 0)
157                 which = 0;
158         else if (strcmp(name, "acl_inherited") == 0)
159                 which = 1;
160         else if (strcmp(name, "acl_num_cleaned") == 0)
161                 which = 2;
162         else if (strcmp(name, "vol_acl") == 0)
163                 which = 3;
164         else
165                 return -EOPNOTSUPP;
166
167         if (which == 0)
168                 flags |= YFS_ACL_WANT_ACL;
169         else if (which == 3)
170                 flags |= YFS_ACL_WANT_VOL_ACL;
171
172         key = afs_request_key(vnode->volume->cell);
173         if (IS_ERR(key))
174                 return PTR_ERR(key);
175
176         ret = -ERESTARTSYS;
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);
181                 }
182
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);
186         }
187
188         if (ret == 0) {
189                 switch (which) {
190                 case 0:
191                         data = yacl->acl->data;
192                         dsize = yacl->acl->size;
193                         break;
194                 case 1:
195                         data = buf;
196                         dsize = snprintf(buf, sizeof(buf), "%u",
197                                          yacl->inherit_flag);
198                         break;
199                 case 2:
200                         data = buf;
201                         dsize = snprintf(buf, sizeof(buf), "%u",
202                                          yacl->num_cleaned);
203                         break;
204                 case 3:
205                         data = yacl->vol_acl->data;
206                         dsize = yacl->vol_acl->size;
207                         break;
208                 default:
209                         ret = -EOPNOTSUPP;
210                         goto out;
211                 }
212
213                 ret = dsize;
214                 if (size > 0) {
215                         if (dsize > size) {
216                                 ret = -ERANGE;
217                                 goto out;
218                         }
219                         memcpy(buffer, data, dsize);
220                 }
221         }
222
223 out:
224         yfs_free_opaque_acl(yacl);
225         key_put(key);
226         return ret;
227 }
228
229 static const struct xattr_handler afs_xattr_yfs_handler = {
230         .prefix = "afs.yfs.",
231         .get    = afs_xattr_get_yfs,
232 };
233
234 /*
235  * Get the name of the cell on which a file resides.
236  */
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)
241 {
242         struct afs_vnode *vnode = AFS_FS_I(inode);
243         struct afs_cell *cell = vnode->volume->cell;
244         size_t namelen;
245
246         namelen = cell->name_len;
247         if (size == 0)
248                 return namelen;
249         if (namelen > size)
250                 return -ERANGE;
251         memcpy(buffer, cell->name, namelen);
252         return namelen;
253 }
254
255 static const struct xattr_handler afs_xattr_afs_cell_handler = {
256         .name   = "afs.cell",
257         .get    = afs_xattr_get_cell,
258 };
259
260 /*
261  * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
262  * hex numbers separated by colons.
263  */
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)
268 {
269         struct afs_vnode *vnode = AFS_FS_I(inode);
270         char text[16 + 1 + 24 + 1 + 8 + 1];
271         size_t len;
272
273         /* The volume ID is 64-bit, the vnode ID is 96-bit and the
274          * uniquifier is 32-bit.
275          */
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);
280         else
281                 len += sprintf(text + len, "%llx", vnode->fid.vnode);
282         len += sprintf(text + len, ":%x", vnode->fid.unique);
283
284         if (size == 0)
285                 return len;
286         if (len > size)
287                 return -ERANGE;
288         memcpy(buffer, text, len);
289         return len;
290 }
291
292 static const struct xattr_handler afs_xattr_afs_fid_handler = {
293         .name   = "afs.fid",
294         .get    = afs_xattr_get_fid,
295 };
296
297 /*
298  * Get the name of the volume on which a file resides.
299  */
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)
304 {
305         struct afs_vnode *vnode = AFS_FS_I(inode);
306         const char *volname = vnode->volume->name;
307         size_t namelen;
308
309         namelen = strlen(volname);
310         if (size == 0)
311                 return namelen;
312         if (namelen > size)
313                 return -ERANGE;
314         memcpy(buffer, volname, namelen);
315         return namelen;
316 }
317
318 static const struct xattr_handler afs_xattr_afs_volume_handler = {
319         .name   = "afs.volume",
320         .get    = afs_xattr_get_volume,
321 };
322
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 */
329         NULL
330 };