blob: 61dfaf7b7d20c05023e4a649eec20954ab12d1e3 [file] [log] [blame]
Seth Forshee703c7362016-08-29 08:46:36 -05001/*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2016 Miklos Szeredi <miklos@szeredi.hu>
4 *
5 * This program can be distributed under the terms of the GNU GPL.
6 * See the file COPYING.
7 */
8
9#include "fuse_i.h"
10
11#include <linux/xattr.h>
Seth Forshee60bcc882016-08-29 08:46:37 -050012#include <linux/posix_acl_xattr.h>
Seth Forshee703c7362016-08-29 08:46:36 -050013
Seth Forshee60bcc882016-08-29 08:46:37 -050014int fuse_setxattr(struct inode *inode, const char *name, const void *value,
Vivek Goyal52a4c952021-03-25 11:18:22 -040015 size_t size, int flags, unsigned int extra_flags)
Seth Forshee703c7362016-08-29 08:46:36 -050016{
Max Reitzfcee2162020-05-06 17:44:12 +020017 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -050018 FUSE_ARGS(args);
19 struct fuse_setxattr_in inarg;
20 int err;
21
Max Reitzfcee2162020-05-06 17:44:12 +020022 if (fm->fc->no_setxattr)
Seth Forshee703c7362016-08-29 08:46:36 -050023 return -EOPNOTSUPP;
24
25 memset(&inarg, 0, sizeof(inarg));
26 inarg.size = size;
27 inarg.flags = flags;
Vivek Goyal52a4c952021-03-25 11:18:22 -040028 inarg.setxattr_flags = extra_flags;
29
Miklos Szeredid5b48542019-09-10 15:04:08 +020030 args.opcode = FUSE_SETXATTR;
31 args.nodeid = get_node_id(inode);
32 args.in_numargs = 3;
Vivek Goyal52a4c952021-03-25 11:18:22 -040033 args.in_args[0].size = fm->fc->setxattr_ext ?
34 sizeof(inarg) : FUSE_COMPAT_SETXATTR_IN_SIZE;
Miklos Szeredid5b48542019-09-10 15:04:08 +020035 args.in_args[0].value = &inarg;
36 args.in_args[1].size = strlen(name) + 1;
37 args.in_args[1].value = name;
38 args.in_args[2].size = size;
39 args.in_args[2].value = value;
Max Reitzfcee2162020-05-06 17:44:12 +020040 err = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -050041 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +020042 fm->fc->no_setxattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -050043 err = -EOPNOTSUPP;
44 }
45 if (!err) {
46 fuse_invalidate_attr(inode);
47 fuse_update_ctime(inode);
48 }
49 return err;
50}
51
Seth Forshee60bcc882016-08-29 08:46:37 -050052ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
53 size_t size)
Seth Forshee703c7362016-08-29 08:46:36 -050054{
Max Reitzfcee2162020-05-06 17:44:12 +020055 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -050056 FUSE_ARGS(args);
57 struct fuse_getxattr_in inarg;
58 struct fuse_getxattr_out outarg;
59 ssize_t ret;
60
Max Reitzfcee2162020-05-06 17:44:12 +020061 if (fm->fc->no_getxattr)
Seth Forshee703c7362016-08-29 08:46:36 -050062 return -EOPNOTSUPP;
63
64 memset(&inarg, 0, sizeof(inarg));
65 inarg.size = size;
Miklos Szeredid5b48542019-09-10 15:04:08 +020066 args.opcode = FUSE_GETXATTR;
67 args.nodeid = get_node_id(inode);
68 args.in_numargs = 2;
69 args.in_args[0].size = sizeof(inarg);
70 args.in_args[0].value = &inarg;
71 args.in_args[1].size = strlen(name) + 1;
72 args.in_args[1].value = name;
Seth Forshee703c7362016-08-29 08:46:36 -050073 /* This is really two different operations rolled into one */
Miklos Szeredid5b48542019-09-10 15:04:08 +020074 args.out_numargs = 1;
Seth Forshee703c7362016-08-29 08:46:36 -050075 if (size) {
Miklos Szeredi1f4e9d02019-09-10 15:04:08 +020076 args.out_argvar = true;
Miklos Szeredid5b48542019-09-10 15:04:08 +020077 args.out_args[0].size = size;
78 args.out_args[0].value = value;
Seth Forshee703c7362016-08-29 08:46:36 -050079 } else {
Miklos Szeredid5b48542019-09-10 15:04:08 +020080 args.out_args[0].size = sizeof(outarg);
81 args.out_args[0].value = &outarg;
Seth Forshee703c7362016-08-29 08:46:36 -050082 }
Max Reitzfcee2162020-05-06 17:44:12 +020083 ret = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -050084 if (!ret && !size)
Miklos Szeredi63401cc2016-10-03 11:06:05 +020085 ret = min_t(ssize_t, outarg.size, XATTR_SIZE_MAX);
Seth Forshee703c7362016-08-29 08:46:36 -050086 if (ret == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +020087 fm->fc->no_getxattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -050088 ret = -EOPNOTSUPP;
89 }
90 return ret;
91}
92
93static int fuse_verify_xattr_list(char *list, size_t size)
94{
95 size_t origsize = size;
96
97 while (size) {
98 size_t thislen = strnlen(list, size);
99
100 if (!thislen || thislen == size)
101 return -EIO;
102
103 size -= thislen + 1;
104 list += thislen + 1;
105 }
106
107 return origsize;
108}
109
110ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
111{
112 struct inode *inode = d_inode(entry);
Max Reitzfcee2162020-05-06 17:44:12 +0200113 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -0500114 FUSE_ARGS(args);
115 struct fuse_getxattr_in inarg;
116 struct fuse_getxattr_out outarg;
117 ssize_t ret;
118
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100119 if (fuse_is_bad(inode))
120 return -EIO;
121
Max Reitzfcee2162020-05-06 17:44:12 +0200122 if (!fuse_allow_current_process(fm->fc))
Seth Forshee703c7362016-08-29 08:46:36 -0500123 return -EACCES;
124
Max Reitzfcee2162020-05-06 17:44:12 +0200125 if (fm->fc->no_listxattr)
Seth Forshee703c7362016-08-29 08:46:36 -0500126 return -EOPNOTSUPP;
127
128 memset(&inarg, 0, sizeof(inarg));
129 inarg.size = size;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200130 args.opcode = FUSE_LISTXATTR;
131 args.nodeid = get_node_id(inode);
132 args.in_numargs = 1;
133 args.in_args[0].size = sizeof(inarg);
134 args.in_args[0].value = &inarg;
Seth Forshee703c7362016-08-29 08:46:36 -0500135 /* This is really two different operations rolled into one */
Miklos Szeredid5b48542019-09-10 15:04:08 +0200136 args.out_numargs = 1;
Seth Forshee703c7362016-08-29 08:46:36 -0500137 if (size) {
Miklos Szeredi1f4e9d02019-09-10 15:04:08 +0200138 args.out_argvar = true;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200139 args.out_args[0].size = size;
140 args.out_args[0].value = list;
Seth Forshee703c7362016-08-29 08:46:36 -0500141 } else {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200142 args.out_args[0].size = sizeof(outarg);
143 args.out_args[0].value = &outarg;
Seth Forshee703c7362016-08-29 08:46:36 -0500144 }
Max Reitzfcee2162020-05-06 17:44:12 +0200145 ret = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -0500146 if (!ret && !size)
Miklos Szeredi63401cc2016-10-03 11:06:05 +0200147 ret = min_t(ssize_t, outarg.size, XATTR_LIST_MAX);
Seth Forshee703c7362016-08-29 08:46:36 -0500148 if (ret > 0 && size)
149 ret = fuse_verify_xattr_list(list, ret);
150 if (ret == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +0200151 fm->fc->no_listxattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -0500152 ret = -EOPNOTSUPP;
153 }
154 return ret;
155}
156
Seth Forshee60bcc882016-08-29 08:46:37 -0500157int fuse_removexattr(struct inode *inode, const char *name)
Seth Forshee703c7362016-08-29 08:46:36 -0500158{
Max Reitzfcee2162020-05-06 17:44:12 +0200159 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -0500160 FUSE_ARGS(args);
161 int err;
162
Max Reitzfcee2162020-05-06 17:44:12 +0200163 if (fm->fc->no_removexattr)
Seth Forshee703c7362016-08-29 08:46:36 -0500164 return -EOPNOTSUPP;
165
Miklos Szeredid5b48542019-09-10 15:04:08 +0200166 args.opcode = FUSE_REMOVEXATTR;
167 args.nodeid = get_node_id(inode);
168 args.in_numargs = 1;
169 args.in_args[0].size = strlen(name) + 1;
170 args.in_args[0].value = name;
Max Reitzfcee2162020-05-06 17:44:12 +0200171 err = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -0500172 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +0200173 fm->fc->no_removexattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -0500174 err = -EOPNOTSUPP;
175 }
176 if (!err) {
177 fuse_invalidate_attr(inode);
178 fuse_update_ctime(inode);
179 }
180 return err;
181}
182
183static int fuse_xattr_get(const struct xattr_handler *handler,
184 struct dentry *dentry, struct inode *inode,
185 const char *name, void *value, size_t size)
186{
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100187 if (fuse_is_bad(inode))
188 return -EIO;
189
Seth Forshee703c7362016-08-29 08:46:36 -0500190 return fuse_getxattr(inode, name, value, size);
191}
192
193static int fuse_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100194 struct user_namespace *mnt_userns,
Seth Forshee703c7362016-08-29 08:46:36 -0500195 struct dentry *dentry, struct inode *inode,
196 const char *name, const void *value, size_t size,
197 int flags)
198{
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100199 if (fuse_is_bad(inode))
200 return -EIO;
201
Seth Forshee703c7362016-08-29 08:46:36 -0500202 if (!value)
203 return fuse_removexattr(inode, name);
204
Vivek Goyal52a4c952021-03-25 11:18:22 -0400205 return fuse_setxattr(inode, name, value, size, flags, 0);
Seth Forshee703c7362016-08-29 08:46:36 -0500206}
207
Eric W. Biedermane45b2542018-05-04 11:47:28 -0500208static bool no_xattr_list(struct dentry *dentry)
209{
210 return false;
211}
212
213static int no_xattr_get(const struct xattr_handler *handler,
214 struct dentry *dentry, struct inode *inode,
215 const char *name, void *value, size_t size)
216{
217 return -EOPNOTSUPP;
218}
219
220static int no_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100221 struct user_namespace *mnt_userns,
Eric W. Biedermane45b2542018-05-04 11:47:28 -0500222 struct dentry *dentry, struct inode *nodee,
223 const char *name, const void *value,
224 size_t size, int flags)
225{
226 return -EOPNOTSUPP;
227}
228
Seth Forshee703c7362016-08-29 08:46:36 -0500229static const struct xattr_handler fuse_xattr_handler = {
230 .prefix = "",
231 .get = fuse_xattr_get,
232 .set = fuse_xattr_set,
233};
234
235const struct xattr_handler *fuse_xattr_handlers[] = {
236 &fuse_xattr_handler,
237 NULL
238};
Seth Forshee60bcc882016-08-29 08:46:37 -0500239
240const struct xattr_handler *fuse_acl_xattr_handlers[] = {
241 &posix_acl_access_xattr_handler,
242 &posix_acl_default_xattr_handler,
243 &fuse_xattr_handler,
244 NULL
245};
Eric W. Biedermane45b2542018-05-04 11:47:28 -0500246
247static const struct xattr_handler fuse_no_acl_access_xattr_handler = {
248 .name = XATTR_NAME_POSIX_ACL_ACCESS,
249 .flags = ACL_TYPE_ACCESS,
250 .list = no_xattr_list,
251 .get = no_xattr_get,
252 .set = no_xattr_set,
253};
254
255static const struct xattr_handler fuse_no_acl_default_xattr_handler = {
256 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
257 .flags = ACL_TYPE_ACCESS,
258 .list = no_xattr_list,
259 .get = no_xattr_get,
260 .set = no_xattr_set,
261};
262
263const struct xattr_handler *fuse_no_acl_xattr_handlers[] = {
264 &fuse_no_acl_access_xattr_handler,
265 &fuse_no_acl_default_xattr_handler,
266 &fuse_xattr_handler,
267 NULL
268};