afs: Fix accessing YFS xattrs on a non-YFS server
[linux-2.6-microblaze.git] / fs / afs / xattr.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Extended attribute handling for AFS.  We use xattrs to get and set metadata
3  * instead of providing pioctl().
4  *
5  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
6  * Written by David Howells (dhowells@redhat.com)
7  */
8
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/xattr.h>
12 #include "internal.h"
13
14 static const char afs_xattr_list[] =
15         "afs.acl\0"
16         "afs.cell\0"
17         "afs.fid\0"
18         "afs.volume\0"
19         "afs.yfs.acl\0"
20         "afs.yfs.acl_inherited\0"
21         "afs.yfs.acl_num_cleaned\0"
22         "afs.yfs.vol_acl";
23
24 /*
25  * Retrieve a list of the supported xattrs.
26  */
27 ssize_t afs_listxattr(struct dentry *dentry, char *buffer, size_t size)
28 {
29         if (size == 0)
30                 return sizeof(afs_xattr_list);
31         if (size < sizeof(afs_xattr_list))
32                 return -ERANGE;
33         memcpy(buffer, afs_xattr_list, sizeof(afs_xattr_list));
34         return sizeof(afs_xattr_list);
35 }
36
37 /*
38  * Deal with the result of a successful fetch ACL operation.
39  */
40 static void afs_acl_success(struct afs_operation *op)
41 {
42         afs_vnode_commit_status(op, &op->file[0]);
43 }
44
45 static void afs_acl_put(struct afs_operation *op)
46 {
47         kfree(op->acl);
48 }
49
50 static const struct afs_operation_ops afs_fetch_acl_operation = {
51         .issue_afs_rpc  = afs_fs_fetch_acl,
52         .success        = afs_acl_success,
53         .put            = afs_acl_put,
54 };
55
56 /*
57  * Get a file's ACL.
58  */
59 static int afs_xattr_get_acl(const struct xattr_handler *handler,
60                              struct dentry *dentry,
61                              struct inode *inode, const char *name,
62                              void *buffer, size_t size)
63 {
64         struct afs_operation *op;
65         struct afs_vnode *vnode = AFS_FS_I(inode);
66         struct afs_acl *acl = NULL;
67         int ret;
68
69         op = afs_alloc_operation(NULL, vnode->volume);
70         if (IS_ERR(op))
71                 return -ENOMEM;
72
73         afs_op_set_vnode(op, 0, vnode);
74         op->ops = &afs_fetch_acl_operation;
75
76         afs_begin_vnode_operation(op);
77         afs_wait_for_operation(op);
78         acl = op->acl;
79         op->acl = NULL;
80         ret = afs_put_operation(op);
81
82         if (ret == 0) {
83                 ret = acl->size;
84                 if (size > 0) {
85                         if (acl->size <= size)
86                                 memcpy(buffer, acl->data, acl->size);
87                         else
88                                 ret = -ERANGE;
89                 }
90         }
91
92         kfree(acl);
93         return ret;
94 }
95
96 static bool afs_make_acl(struct afs_operation *op,
97                          const void *buffer, size_t size)
98 {
99         struct afs_acl *acl;
100
101         acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL);
102         if (!acl) {
103                 afs_op_nomem(op);
104                 return false;
105         }
106
107         acl->size = size;
108         memcpy(acl->data, buffer, size);
109         op->acl = acl;
110         return true;
111 }
112
113 static const struct afs_operation_ops afs_store_acl_operation = {
114         .issue_afs_rpc  = afs_fs_store_acl,
115         .success        = afs_acl_success,
116         .put            = afs_acl_put,
117 };
118
119 /*
120  * Set a file's AFS3 ACL.
121  */
122 static int afs_xattr_set_acl(const struct xattr_handler *handler,
123                              struct user_namespace *mnt_userns,
124                              struct dentry *dentry,
125                              struct inode *inode, const char *name,
126                              const void *buffer, size_t size, int flags)
127 {
128         struct afs_operation *op;
129         struct afs_vnode *vnode = AFS_FS_I(inode);
130
131         if (flags == XATTR_CREATE)
132                 return -EINVAL;
133
134         op = afs_alloc_operation(NULL, vnode->volume);
135         if (IS_ERR(op))
136                 return -ENOMEM;
137
138         afs_op_set_vnode(op, 0, vnode);
139         if (!afs_make_acl(op, buffer, size))
140                 return afs_put_operation(op);
141
142         op->ops = &afs_store_acl_operation;
143         return afs_do_sync_operation(op);
144 }
145
146 static const struct xattr_handler afs_xattr_afs_acl_handler = {
147         .name   = "afs.acl",
148         .get    = afs_xattr_get_acl,
149         .set    = afs_xattr_set_acl,
150 };
151
152 static const struct afs_operation_ops yfs_fetch_opaque_acl_operation = {
153         .issue_yfs_rpc  = yfs_fs_fetch_opaque_acl,
154         .success        = afs_acl_success,
155         /* Don't free op->yacl in .put here */
156 };
157
158 /*
159  * Get a file's YFS ACL.
160  */
161 static int afs_xattr_get_yfs(const struct xattr_handler *handler,
162                              struct dentry *dentry,
163                              struct inode *inode, const char *name,
164                              void *buffer, size_t size)
165 {
166         struct afs_operation *op;
167         struct afs_vnode *vnode = AFS_FS_I(inode);
168         struct yfs_acl *yacl = NULL;
169         char buf[16], *data;
170         int which = 0, dsize, ret = -ENOMEM;
171
172         if (strcmp(name, "acl") == 0)
173                 which = 0;
174         else if (strcmp(name, "acl_inherited") == 0)
175                 which = 1;
176         else if (strcmp(name, "acl_num_cleaned") == 0)
177                 which = 2;
178         else if (strcmp(name, "vol_acl") == 0)
179                 which = 3;
180         else
181                 return -EOPNOTSUPP;
182
183         yacl = kzalloc(sizeof(struct yfs_acl), GFP_KERNEL);
184         if (!yacl)
185                 goto error;
186
187         if (which == 0)
188                 yacl->flags |= YFS_ACL_WANT_ACL;
189         else if (which == 3)
190                 yacl->flags |= YFS_ACL_WANT_VOL_ACL;
191
192         op = afs_alloc_operation(NULL, vnode->volume);
193         if (IS_ERR(op))
194                 goto error_yacl;
195
196         afs_op_set_vnode(op, 0, vnode);
197         op->yacl = yacl;
198         op->ops = &yfs_fetch_opaque_acl_operation;
199
200         afs_begin_vnode_operation(op);
201         afs_wait_for_operation(op);
202         ret = afs_put_operation(op);
203
204         if (ret == 0) {
205                 switch (which) {
206                 case 0:
207                         data = yacl->acl->data;
208                         dsize = yacl->acl->size;
209                         break;
210                 case 1:
211                         data = buf;
212                         dsize = scnprintf(buf, sizeof(buf), "%u", yacl->inherit_flag);
213                         break;
214                 case 2:
215                         data = buf;
216                         dsize = scnprintf(buf, sizeof(buf), "%u", yacl->num_cleaned);
217                         break;
218                 case 3:
219                         data = yacl->vol_acl->data;
220                         dsize = yacl->vol_acl->size;
221                         break;
222                 default:
223                         ret = -EOPNOTSUPP;
224                         goto error_yacl;
225                 }
226
227                 ret = dsize;
228                 if (size > 0) {
229                         if (dsize <= size)
230                                 memcpy(buffer, data, dsize);
231                         else
232                                 ret = -ERANGE;
233                 }
234         } else if (ret == -ENOTSUPP) {
235                 ret = -ENODATA;
236         }
237
238 error_yacl:
239         yfs_free_opaque_acl(yacl);
240 error:
241         return ret;
242 }
243
244 static const struct afs_operation_ops yfs_store_opaque_acl2_operation = {
245         .issue_yfs_rpc  = yfs_fs_store_opaque_acl2,
246         .success        = afs_acl_success,
247         .put            = afs_acl_put,
248 };
249
250 /*
251  * Set a file's YFS ACL.
252  */
253 static int afs_xattr_set_yfs(const struct xattr_handler *handler,
254                              struct user_namespace *mnt_userns,
255                              struct dentry *dentry,
256                              struct inode *inode, const char *name,
257                              const void *buffer, size_t size, int flags)
258 {
259         struct afs_operation *op;
260         struct afs_vnode *vnode = AFS_FS_I(inode);
261         int ret;
262
263         if (flags == XATTR_CREATE ||
264             strcmp(name, "acl") != 0)
265                 return -EINVAL;
266
267         op = afs_alloc_operation(NULL, vnode->volume);
268         if (IS_ERR(op))
269                 return -ENOMEM;
270
271         afs_op_set_vnode(op, 0, vnode);
272         if (!afs_make_acl(op, buffer, size))
273                 return afs_put_operation(op);
274
275         op->ops = &yfs_store_opaque_acl2_operation;
276         ret = afs_do_sync_operation(op);
277         if (ret == -ENOTSUPP)
278                 ret = -ENODATA;
279         return ret;
280 }
281
282 static const struct xattr_handler afs_xattr_yfs_handler = {
283         .prefix = "afs.yfs.",
284         .get    = afs_xattr_get_yfs,
285         .set    = afs_xattr_set_yfs,
286 };
287
288 /*
289  * Get the name of the cell on which a file resides.
290  */
291 static int afs_xattr_get_cell(const struct xattr_handler *handler,
292                               struct dentry *dentry,
293                               struct inode *inode, const char *name,
294                               void *buffer, size_t size)
295 {
296         struct afs_vnode *vnode = AFS_FS_I(inode);
297         struct afs_cell *cell = vnode->volume->cell;
298         size_t namelen;
299
300         namelen = cell->name_len;
301         if (size == 0)
302                 return namelen;
303         if (namelen > size)
304                 return -ERANGE;
305         memcpy(buffer, cell->name, namelen);
306         return namelen;
307 }
308
309 static const struct xattr_handler afs_xattr_afs_cell_handler = {
310         .name   = "afs.cell",
311         .get    = afs_xattr_get_cell,
312 };
313
314 /*
315  * Get the volume ID, vnode ID and vnode uniquifier of a file as a sequence of
316  * hex numbers separated by colons.
317  */
318 static int afs_xattr_get_fid(const struct xattr_handler *handler,
319                              struct dentry *dentry,
320                              struct inode *inode, const char *name,
321                              void *buffer, size_t size)
322 {
323         struct afs_vnode *vnode = AFS_FS_I(inode);
324         char text[16 + 1 + 24 + 1 + 8 + 1];
325         size_t len;
326
327         /* The volume ID is 64-bit, the vnode ID is 96-bit and the
328          * uniquifier is 32-bit.
329          */
330         len = scnprintf(text, sizeof(text), "%llx:", vnode->fid.vid);
331         if (vnode->fid.vnode_hi)
332                 len += scnprintf(text + len, sizeof(text) - len, "%x%016llx",
333                                 vnode->fid.vnode_hi, vnode->fid.vnode);
334         else
335                 len += scnprintf(text + len, sizeof(text) - len, "%llx",
336                                  vnode->fid.vnode);
337         len += scnprintf(text + len, sizeof(text) - len, ":%x",
338                          vnode->fid.unique);
339
340         if (size == 0)
341                 return len;
342         if (len > size)
343                 return -ERANGE;
344         memcpy(buffer, text, len);
345         return len;
346 }
347
348 static const struct xattr_handler afs_xattr_afs_fid_handler = {
349         .name   = "afs.fid",
350         .get    = afs_xattr_get_fid,
351 };
352
353 /*
354  * Get the name of the volume on which a file resides.
355  */
356 static int afs_xattr_get_volume(const struct xattr_handler *handler,
357                               struct dentry *dentry,
358                               struct inode *inode, const char *name,
359                               void *buffer, size_t size)
360 {
361         struct afs_vnode *vnode = AFS_FS_I(inode);
362         const char *volname = vnode->volume->name;
363         size_t namelen;
364
365         namelen = strlen(volname);
366         if (size == 0)
367                 return namelen;
368         if (namelen > size)
369                 return -ERANGE;
370         memcpy(buffer, volname, namelen);
371         return namelen;
372 }
373
374 static const struct xattr_handler afs_xattr_afs_volume_handler = {
375         .name   = "afs.volume",
376         .get    = afs_xattr_get_volume,
377 };
378
379 const struct xattr_handler *afs_xattr_handlers[] = {
380         &afs_xattr_afs_acl_handler,
381         &afs_xattr_afs_cell_handler,
382         &afs_xattr_afs_fid_handler,
383         &afs_xattr_afs_volume_handler,
384         &afs_xattr_yfs_handler,         /* afs.yfs. prefix */
385         NULL
386 };