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/param.h>
29 #include <sys/extattr.h>
30 #include <sys/fcntl.h>
31 #include <sys/namei.h>
33 #include <sys/syscallsubr.h>
36 #include <machine/../linux32/linux.h>
37 #include <machine/../linux32/linux32_proto.h>
39 #include <machine/../linux/linux.h>
40 #include <machine/../linux/linux_proto.h>
43 #include <compat/linux/linux_util.h>
45 #define LINUX_XATTR_SIZE_MAX 65536
46 #define LINUX_XATTR_LIST_MAX 65536
47 #define LINUX_XATTR_NAME_MAX 255
49 #define LINUX_XATTR_CREATE 0x1
50 #define LINUX_XATTR_REPLACE 0x2
51 #define LINUX_XATTR_FLAGS LINUX_XATTR_CREATE|LINUX_XATTR_REPLACE
53 struct listxattr_args {
61 struct setxattr_args {
71 struct getxattr_args {
80 struct removexattr_args {
87 static char *extattr_namespace_names[] = EXTATTR_NAMESPACE_NAMES;
91 error_to_xattrerror(int attrnamespace, int error)
94 if (attrnamespace == EXTATTR_NAMESPACE_SYSTEM && error == EPERM)
101 xatrr_to_extattr(const char *uattrname, int *attrnamespace, char *attrname)
103 char uname[LINUX_XATTR_NAME_MAX + 1], *dot;
107 error = copyinstr(uattrname, uname, sizeof(uname), &cplen);
110 if (cplen == sizeof(uname))
112 dot = strchr(uname, '.');
116 for (*attrnamespace = EXTATTR_NAMESPACE_USER;
117 *attrnamespace < nitems(extattr_namespace_names);
118 (*attrnamespace)++) {
119 if (bcmp(uname, extattr_namespace_names[*attrnamespace],
120 dot - uname + 1) == 0) {
122 len = strlen(dot) + 1;
123 bcopy(dot, attrname, len);
131 listxattr(struct thread *td, struct listxattr_args *args)
133 char attrname[LINUX_XATTR_NAME_MAX + 1];
134 char *data, *prefix, *key;
137 unsigned char keylen;
138 size_t sz, cnt, rs, prefixlen, pairlen;
139 int attrnamespace, error;
142 sz = min(LINUX_XATTR_LIST_MAX, args->size);
144 sz = LINUX_XATTR_LIST_MAX;
146 data = malloc(sz, M_LINUX, M_WAITOK);
147 auio.uio_iov = &aiov;
149 auio.uio_rw = UIO_READ;
150 auio.uio_segflg = UIO_SYSSPACE;
153 for (attrnamespace = EXTATTR_NAMESPACE_USER;
154 attrnamespace < nitems(extattr_namespace_names);
156 aiov.iov_base = data;
161 if (args->path != NULL)
162 error = kern_extattr_list_path(td, args->path,
163 attrnamespace, &auio, args->follow, UIO_USERSPACE);
165 error = kern_extattr_list_fd(td, args->fd,
166 attrnamespace, &auio);
167 rs = sz - auio.uio_resid;
170 if (error != 0 || rs == 0)
172 prefix = extattr_namespace_names[attrnamespace];
173 prefixlen = strlen(prefix);
176 keylen = (unsigned char)key[0];
177 pairlen = prefixlen + 1 + keylen + 1;
179 if (cnt > LINUX_XATTR_LIST_MAX) {
184 * If size is specified as zero, return the current size
185 * of the list of extended attribute names.
187 if ((args->size > 0 && cnt > args->size) ||
188 pairlen >= sizeof(attrname)) {
193 if (args->list != NULL && args->size > 0) {
194 sprintf(attrname, "%s.%.*s", prefix, keylen, key);
195 error = copyout(attrname, args->list, pairlen);
198 args->list += pairlen;
205 td->td_retval[0] = cnt;
207 return (error_to_xattrerror(attrnamespace, error));
211 linux_listxattr(struct thread *td, struct linux_listxattr_args *args)
213 struct listxattr_args eargs = {
221 return (listxattr(td, &eargs));
225 linux_llistxattr(struct thread *td, struct linux_llistxattr_args *args)
227 struct listxattr_args eargs = {
235 return (listxattr(td, &eargs));
239 linux_flistxattr(struct thread *td, struct linux_flistxattr_args *args)
241 struct listxattr_args eargs = {
249 return (listxattr(td, &eargs));
253 removexattr(struct thread *td, struct removexattr_args *args)
255 char attrname[LINUX_XATTR_NAME_MAX + 1];
256 int attrnamespace, error;
258 error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
261 if (args->path != NULL)
262 error = kern_extattr_delete_path(td, args->path, attrnamespace,
263 attrname, args->follow, UIO_USERSPACE);
265 error = kern_extattr_delete_fd(td, args->fd, attrnamespace,
267 return (error_to_xattrerror(attrnamespace, error));
271 linux_removexattr(struct thread *td, struct linux_removexattr_args *args)
273 struct removexattr_args eargs = {
280 return (removexattr(td, &eargs));
284 linux_lremovexattr(struct thread *td, struct linux_lremovexattr_args *args)
286 struct removexattr_args eargs = {
293 return (removexattr(td, &eargs));
297 linux_fremovexattr(struct thread *td, struct linux_fremovexattr_args *args)
299 struct removexattr_args eargs = {
306 return (removexattr(td, &eargs));
310 getxattr(struct thread *td, struct getxattr_args *args)
312 char attrname[LINUX_XATTR_NAME_MAX + 1];
313 int attrnamespace, error;
315 error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
318 if (args->path != NULL)
319 error = kern_extattr_get_path(td, args->path, attrnamespace,
320 attrname, args->value, args->size, args->follow, UIO_USERSPACE);
322 error = kern_extattr_get_fd(td, args->fd, attrnamespace,
323 attrname, args->value, args->size);
324 return (error == EPERM ? ENOATTR : error);
328 linux_getxattr(struct thread *td, struct linux_getxattr_args *args)
330 struct getxattr_args eargs = {
334 .value = args->value,
339 return (getxattr(td, &eargs));
343 linux_lgetxattr(struct thread *td, struct linux_lgetxattr_args *args)
345 struct getxattr_args eargs = {
349 .value = args->value,
354 return (getxattr(td, &eargs));
358 linux_fgetxattr(struct thread *td, struct linux_fgetxattr_args *args)
360 struct getxattr_args eargs = {
364 .value = args->value,
369 return (getxattr(td, &eargs));
373 setxattr(struct thread *td, struct setxattr_args *args)
375 char attrname[LINUX_XATTR_NAME_MAX + 1];
376 int attrnamespace, error;
378 if ((args->flags & ~(LINUX_XATTR_FLAGS)) != 0 ||
379 args->flags == (LINUX_XATTR_FLAGS))
381 error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
385 if ((args->flags & (LINUX_XATTR_FLAGS)) != 0 ) {
386 if (args->path != NULL)
387 error = kern_extattr_get_path(td, args->path,
388 attrnamespace, attrname, NULL, args->size,
389 args->follow, UIO_USERSPACE);
391 error = kern_extattr_get_fd(td, args->fd,
392 attrnamespace, attrname, NULL, args->size);
393 if ((args->flags & LINUX_XATTR_CREATE) != 0) {
396 else if (error == ENOATTR)
402 if (args->path != NULL)
403 error = kern_extattr_set_path(td, args->path, attrnamespace,
404 attrname, args->value, args->size, args->follow,
407 error = kern_extattr_set_fd(td, args->fd, attrnamespace,
408 attrname, args->value, args->size);
410 td->td_retval[0] = 0;
411 return (error_to_xattrerror(attrnamespace, error));
415 linux_setxattr(struct thread *td, struct linux_setxattr_args *args)
417 struct setxattr_args eargs = {
421 .value = args->value,
423 .flags = args->flags,
427 return (setxattr(td, &eargs));
431 linux_lsetxattr(struct thread *td, struct linux_lsetxattr_args *args)
433 struct setxattr_args eargs = {
437 .value = args->value,
439 .flags = args->flags,
443 return (setxattr(td, &eargs));
447 linux_fsetxattr(struct thread *td, struct linux_fsetxattr_args *args)
449 struct setxattr_args eargs = {
453 .value = args->value,
455 .flags = args->flags,
459 return (setxattr(td, &eargs));