2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/extattr.h>
33 #include <sys/fcntl.h>
34 #include <sys/namei.h>
36 #include <sys/syscallsubr.h>
39 #include <machine/../linux32/linux.h>
40 #include <machine/../linux32/linux32_proto.h>
42 #include <machine/../linux/linux.h>
43 #include <machine/../linux/linux_proto.h>
46 #include <compat/linux/linux_util.h>
48 #define LINUX_XATTR_SIZE_MAX 65536
49 #define LINUX_XATTR_LIST_MAX 65536
50 #define LINUX_XATTR_NAME_MAX 255
52 #define LINUX_XATTR_CREATE 0x1
53 #define LINUX_XATTR_REPLACE 0x2
54 #define LINUX_XATTR_FLAGS LINUX_XATTR_CREATE|LINUX_XATTR_REPLACE
56 struct listxattr_args {
64 struct setxattr_args {
74 static char *extattr_namespace_names[] = EXTATTR_NAMESPACE_NAMES;
78 xatrr_to_extattr(const char *uattrname, int *attrnamespace, char *attrname)
80 char uname[LINUX_XATTR_NAME_MAX + 1], *dot;
84 error = copyinstr(uattrname, uname, sizeof(uname), &cplen);
87 if (cplen == sizeof(uname))
89 dot = strchr(uname, '.');
93 for (*attrnamespace = EXTATTR_NAMESPACE_USER;
94 *attrnamespace < nitems(extattr_namespace_names);
96 if (bcmp(uname, extattr_namespace_names[*attrnamespace],
97 dot - uname + 1) == 0) {
99 len = strlen(dot) + 1;
100 bcopy(dot, attrname, len);
108 listxattr(struct thread *td, struct listxattr_args *args)
110 char attrname[LINUX_XATTR_NAME_MAX + 1];
111 char *data, *prefix, *key;
114 unsigned char keylen;
115 size_t sz, cnt, rs, prefixlen, pairlen;
116 int attrnamespace, error;
119 sz = min(LINUX_XATTR_LIST_MAX, args->size);
121 sz = LINUX_XATTR_LIST_MAX;
123 data = malloc(sz, M_LINUX, M_WAITOK);
124 auio.uio_iov = &aiov;
126 auio.uio_rw = UIO_READ;
127 auio.uio_segflg = UIO_SYSSPACE;
130 for (attrnamespace = EXTATTR_NAMESPACE_USER;
131 attrnamespace < nitems(extattr_namespace_names);
133 aiov.iov_base = data;
138 if (args->path != NULL)
139 error = kern_extattr_list_path(td, args->path,
140 attrnamespace, &auio, args->follow, UIO_USERSPACE);
142 error = kern_extattr_list_fd(td, args->fd,
143 attrnamespace, &auio);
144 rs = sz - auio.uio_resid;
145 if (error != 0 || rs == 0)
147 prefix = extattr_namespace_names[attrnamespace];
148 prefixlen = strlen(prefix);
151 keylen = (unsigned char)key[0];
152 pairlen = prefixlen + 1 + keylen + 1;
153 if (cnt + pairlen > LINUX_XATTR_LIST_MAX) {
157 if ((args->list != NULL && cnt > args->size) ||
158 pairlen >= sizeof(attrname)) {
163 if (args->list != NULL) {
164 sprintf(attrname, "%s.%.*s", prefix, keylen, key);
165 error = copyout(attrname, args->list, pairlen);
168 args->list += pairlen;
176 td->td_retval[0] = cnt;
182 linux_listxattr(struct thread *td, struct linux_listxattr_args *args)
184 struct listxattr_args eargs = {
192 return (listxattr(td, &eargs));
196 linux_llistxattr(struct thread *td, struct linux_llistxattr_args *args)
198 struct listxattr_args eargs = {
206 return (listxattr(td, &eargs));
210 linux_flistxattr(struct thread *td, struct linux_flistxattr_args *args)
212 struct listxattr_args eargs = {
220 return (listxattr(td, &eargs));
224 linux_path_removexattr(struct thread *td, const char *upath, const char *uname,
227 char attrname[LINUX_XATTR_NAME_MAX + 1];
228 int attrnamespace, error;
230 error = xatrr_to_extattr(uname, &attrnamespace, attrname);
234 return (kern_extattr_delete_path(td, upath, attrnamespace,
235 attrname, follow, UIO_USERSPACE));
239 linux_removexattr(struct thread *td, struct linux_removexattr_args *args)
242 return (linux_path_removexattr(td, args->path, args->name,
247 linux_lremovexattr(struct thread *td, struct linux_lremovexattr_args *args)
250 return (linux_path_removexattr(td, args->path, args->name,
255 linux_fremovexattr(struct thread *td, struct linux_fremovexattr_args *args)
257 char attrname[LINUX_XATTR_NAME_MAX + 1];
258 int attrnamespace, error;
260 error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
263 return (kern_extattr_delete_fd(td, args->fd, attrnamespace,
268 linux_path_getxattr(struct thread *td, const char *upath, const char *uname,
269 void *value, l_size_t size, int follow)
271 char attrname[LINUX_XATTR_NAME_MAX + 1];
272 int attrnamespace, error;
274 error = xatrr_to_extattr(uname, &attrnamespace, attrname);
278 return (kern_extattr_get_path(td, upath, attrnamespace,
279 attrname, value, size, follow, UIO_USERSPACE));
283 linux_getxattr(struct thread *td, struct linux_getxattr_args *args)
286 return (linux_path_getxattr(td, args->path, args->name,
287 args->value, args->size, FOLLOW));
291 linux_lgetxattr(struct thread *td, struct linux_lgetxattr_args *args)
294 return (linux_path_getxattr(td, args->path, args->name,
295 args->value, args->size, NOFOLLOW));
299 linux_fgetxattr(struct thread *td, struct linux_fgetxattr_args *args)
301 char attrname[LINUX_XATTR_NAME_MAX + 1];
302 int attrnamespace, error;
304 error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
307 return (kern_extattr_get_fd(td, args->fd, attrnamespace,
308 attrname, args->value, args->size));
312 setxattr(struct thread *td, struct setxattr_args *args)
314 char attrname[LINUX_XATTR_NAME_MAX + 1];
315 int attrnamespace, error;
317 if ((args->flags & ~(LINUX_XATTR_FLAGS)) != 0 ||
318 args->flags == (LINUX_XATTR_FLAGS))
320 error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
324 if ((args->flags & (LINUX_XATTR_FLAGS)) != 0 ) {
325 if (args->path != NULL)
326 error = kern_extattr_get_path(td, args->path,
327 attrnamespace, attrname, NULL, args->size,
328 args->follow, UIO_USERSPACE);
330 error = kern_extattr_get_fd(td, args->fd,
331 attrnamespace, attrname, NULL, args->size);
332 if ((args->flags & LINUX_XATTR_CREATE) != 0) {
335 else if (error == ENOATTR)
341 if (args->path != NULL)
342 error = kern_extattr_set_path(td, args->path, attrnamespace,
343 attrname, args->value, args->size, args->follow,
346 error = kern_extattr_set_fd(td, args->fd, attrnamespace,
347 attrname, args->value, args->size);
349 td->td_retval[0] = 0;
354 linux_setxattr(struct thread *td, struct linux_setxattr_args *args)
356 struct setxattr_args eargs = {
360 .value = args->value,
362 .flags = args->flags,
366 return (setxattr(td, &eargs));
370 linux_lsetxattr(struct thread *td, struct linux_lsetxattr_args *args)
372 struct setxattr_args eargs = {
376 .value = args->value,
378 .flags = args->flags,
382 return (setxattr(td, &eargs));
386 linux_fsetxattr(struct thread *td, struct linux_fsetxattr_args *args)
388 struct setxattr_args eargs = {
392 .value = args->value,
394 .flags = args->flags,
398 return (setxattr(td, &eargs));