linux_file64.c revision 1.67 1 /* $NetBSD: linux_file64.c,v 1.67 2021/11/25 02:27:08 ryo Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.67 2021/11/25 02:27:08 ryo Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/dirent.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/filedesc.h>
47 #include <sys/ioctl.h>
48 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <sys/namei.h>
52 #include <sys/vfs_syscalls.h>
53 #include <sys/vnode.h>
54 #include <sys/tty.h>
55 #include <sys/conf.h>
56
57 #include <sys/syscallargs.h>
58
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_fcntl.h>
62 #include <compat/linux/common/linux_util.h>
63 #include <compat/linux/common/linux_machdep.h>
64 #include <compat/linux/common/linux_dirent.h>
65 #include <compat/linux/common/linux_ipc.h>
66 #include <compat/linux/common/linux_sem.h>
67
68 #include <compat/linux/linux_syscall.h>
69 #include <compat/linux/linux_syscallargs.h>
70
71 static void bsd_to_linux_stat64(struct stat *, struct linux_stat64 *);
72
73 /*
74 * Convert a NetBSD stat structure to a Linux stat structure.
75 * Only the order of the fields and the padding in the structure
76 * is different. linux_fakedev is a machine-dependent function
77 * which optionally converts device driver major/minor numbers
78 * (XXX horrible, but what can you do against code that compares
79 * things against constant major device numbers? sigh)
80 */
81 static void
82 bsd_to_linux_stat64(struct stat *bsp, struct linux_stat64 *lsp)
83 {
84 memset(lsp, 0, sizeof(*lsp));
85 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
86 lsp->lst_ino = bsp->st_ino;
87 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
88 if (bsp->st_nlink >= (1 << 15))
89 lsp->lst_nlink = (1 << 15) - 1;
90 else
91 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
92 lsp->lst_uid = bsp->st_uid;
93 lsp->lst_gid = bsp->st_gid;
94 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
95 lsp->lst_size = bsp->st_size;
96 lsp->lst_blksize = bsp->st_blksize;
97 lsp->lst_blocks = bsp->st_blocks;
98 lsp->lst_atime = bsp->st_atime;
99 lsp->lst_mtime = bsp->st_mtime;
100 lsp->lst_ctime = bsp->st_ctime;
101 # ifdef LINUX_STAT64_HAS_NSEC
102 lsp->lst_atime_nsec = bsp->st_atimensec;
103 lsp->lst_mtime_nsec = bsp->st_mtimensec;
104 lsp->lst_ctime_nsec = bsp->st_ctimensec;
105 # endif
106 # if LINUX_STAT64_HAS_BROKEN_ST_INO
107 lsp->__lst_ino = (linux_ino_t) bsp->st_ino;
108 # endif
109 }
110
111 int
112 bsd_to_linux_statx(struct stat *st, struct linux_statx *stx,
113 unsigned int mask)
114 {
115 if (mask & STATX__RESERVED)
116 return EINVAL;
117
118 /* XXX: STATX_MNT_ID is not supported */
119 unsigned int rmask = STATX_TYPE | STATX_MODE | STATX_NLINK |
120 STATX_UID | STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME |
121 STATX_INO | STATX_SIZE | STATX_BLOCKS | STATX_BTIME;
122
123 memset(stx, 0, sizeof(*stx));
124
125 if ((st->st_flags & UF_NODUMP) != 0)
126 stx->stx_attributes |= STATX_ATTR_NODUMP;
127 if ((st->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) != 0)
128 stx->stx_attributes |= STATX_ATTR_IMMUTABLE;
129 if ((st->st_flags & (UF_APPEND|SF_APPEND)) != 0)
130 stx->stx_attributes |= STATX_ATTR_APPEND;
131
132 stx->stx_attributes_mask =
133 STATX_ATTR_NODUMP | STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
134
135 stx->stx_blksize = st->st_blksize;
136
137 stx->stx_nlink = st->st_nlink;
138 stx->stx_uid = st->st_uid;
139 stx->stx_gid = st->st_gid;
140 stx->stx_mode |= st->st_mode & S_IFMT;
141 stx->stx_mode |= st->st_mode & ~S_IFMT;
142 stx->stx_ino = st->st_ino;
143 stx->stx_size = st->st_size;
144 stx->stx_blocks = st->st_blocks;
145
146 stx->stx_atime.tv_sec = st->st_atime;
147 stx->stx_atime.tv_nsec = st->st_atimensec;
148
149 /* some filesystem has no birthtime returns 0 or -1 */
150 if ((st->st_birthtime == 0 && st->st_birthtimensec == 0) ||
151 (st->st_birthtime == (time_t)-1 &&
152 st->st_birthtimensec == (long)-1)) {
153 rmask &= ~STATX_BTIME;
154 } else {
155 stx->stx_btime.tv_sec = st->st_birthtime;
156 stx->stx_btime.tv_nsec = st->st_birthtimensec;
157 }
158
159 stx->stx_ctime.tv_sec = st->st_ctime;
160 stx->stx_ctime.tv_nsec = st->st_ctimensec;
161
162 stx->stx_mtime.tv_sec = st->st_mtime;
163 stx->stx_mtime.tv_nsec = st->st_mtimensec;
164
165 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
166 stx->stx_rdev_major = major(st->st_rdev);
167 stx->stx_rdev_minor = minor(st->st_rdev);
168 } else {
169 stx->stx_dev_major = major(st->st_rdev);
170 stx->stx_dev_minor = minor(st->st_rdev);
171 }
172
173 stx->stx_mask = rmask;
174
175 return 0;
176 }
177
178 /*
179 * The stat functions below are plain sailing. stat and lstat are handled
180 * by one function to avoid code duplication.
181 */
182 int
183 linux_sys_fstat64(struct lwp *l, const struct linux_sys_fstat64_args *uap, register_t *retval)
184 {
185 /* {
186 syscallarg(int) fd;
187 syscallarg(struct linux_stat64 *) sp;
188 } */
189 struct linux_stat64 tmplst;
190 struct stat tmpst;
191 int error;
192
193 error = do_sys_fstat(SCARG(uap, fd), &tmpst);
194 if (error != 0)
195 return error;
196
197 bsd_to_linux_stat64(&tmpst, &tmplst);
198
199 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
200 }
201
202 #if !defined(__aarch64__)
203 static int
204 linux_do_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval, int flags)
205 {
206 struct linux_stat64 tmplst;
207 struct stat tmpst;
208 int error;
209
210 error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
211 if (error != 0)
212 return error;
213
214 bsd_to_linux_stat64(&tmpst, &tmplst);
215
216 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
217 }
218
219 int
220 linux_sys_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval)
221 {
222 /* {
223 syscallarg(const char *) path;
224 syscallarg(struct linux_stat64 *) sp;
225 } */
226
227 return linux_do_stat64(l, uap, retval, FOLLOW);
228 }
229
230 int
231 linux_sys_lstat64(struct lwp *l, const struct linux_sys_lstat64_args *uap, register_t *retval)
232 {
233 /* {
234 syscallarg(const char *) path;
235 syscallarg(struct linux_stat64 *) sp;
236 } */
237
238 return linux_do_stat64(l, (const void *)uap, retval, NOFOLLOW);
239 }
240 #endif
241
242 /*
243 * This is an internal function for the *statat() variant of linux,
244 * which returns struct stat, but flags and other handling are
245 * the same as in linux.
246 */
247 int
248 linux_statat(struct lwp *l, int fd, const char *path, int lflag,
249 struct stat *st)
250 {
251 struct vnode *vp;
252 int error, nd_flag;
253 uint8_t c;
254
255 if (lflag & LINUX_AT_EMPTY_PATH) {
256 /*
257 * If path is null string:
258 */
259 error = ufetch_8(path, &c);
260 if (error != 0)
261 return error;
262 if (c == '\0') {
263 if (fd == LINUX_AT_FDCWD) {
264 /*
265 * operate on current directory
266 */
267 vp = l->l_proc->p_cwdi->cwdi_cdir;
268 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
269 error = vn_stat(vp, st);
270 VOP_UNLOCK(vp);
271 } else {
272 /*
273 * operate on fd
274 */
275 error = do_sys_fstat(fd, st);
276 }
277 return error;
278 }
279 }
280
281 if (lflag & LINUX_AT_SYMLINK_NOFOLLOW)
282 nd_flag = NOFOLLOW;
283 else
284 nd_flag = FOLLOW;
285
286 return do_sys_statat(l, fd, path, nd_flag, st);
287 }
288
289 int
290 linux_sys_fstatat64(struct lwp *l, const struct linux_sys_fstatat64_args *uap, register_t *retval)
291 {
292 /* {
293 syscallarg(int) fd;
294 syscallarg(const char *) path;
295 syscallarg(struct linux_stat64 *) sp;
296 syscallarg(int) flag;
297 } */
298 struct linux_stat64 tmplst;
299 struct stat tmpst;
300 int error;
301
302 error = linux_statat(l, SCARG(uap, fd), SCARG(uap, path),
303 SCARG(uap, flag), &tmpst);
304 if (error != 0)
305 return error;
306
307 bsd_to_linux_stat64(&tmpst, &tmplst);
308
309 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
310 }
311
312 #ifdef LINUX_SYS_statx
313 int
314 linux_sys_statx(struct lwp *l, const struct linux_sys_statx_args *uap,
315 register_t *retval)
316 {
317 /* {
318 syscallarg(int) fd;
319 syscallarg(const char *) path;
320 syscallarg(int) flag;
321 syscallarg(unsigned int) mask;
322 syscallarg(struct linux_statx *) sp;
323 } */
324 struct linux_statx stx;
325 struct stat st;
326 int error;
327
328 error = linux_statat(l, SCARG(uap, fd), SCARG(uap, path),
329 SCARG(uap, flag), &st);
330 if (error != 0)
331 return error;
332
333 error = bsd_to_linux_statx(&st, &stx, SCARG(uap, mask));
334 if (error != 0)
335 return error;
336
337 return copyout(&stx, SCARG(uap, sp), sizeof stx);
338 }
339 #endif /* LINUX_SYS_statx */
340
341 #ifndef __alpha__
342 int
343 linux_sys_truncate64(struct lwp *l, const struct linux_sys_truncate64_args *uap, register_t *retval)
344 {
345 /* {
346 syscallarg(const char *) path;
347 syscallarg(off_t) length;
348 } */
349 struct sys_truncate_args ta;
350
351 /* Linux doesn't have the 'pad' pseudo-parameter */
352 SCARG(&ta, path) = SCARG(uap, path);
353 SCARG(&ta, PAD) = 0;
354 SCARG(&ta, length) = SCARG(uap, length);
355
356 return sys_truncate(l, &ta, retval);
357 }
358
359 int
360 linux_sys_ftruncate64(struct lwp *l, const struct linux_sys_ftruncate64_args *uap, register_t *retval)
361 {
362 /* {
363 syscallarg(unsigned int) fd;
364 syscallarg(off_t) length;
365 } */
366 struct sys_ftruncate_args ta;
367
368 /* Linux doesn't have the 'pad' pseudo-parameter */
369 SCARG(&ta, fd) = SCARG(uap, fd);
370 SCARG(&ta, PAD) = 0;
371 SCARG(&ta, length) = SCARG(uap, length);
372
373 return sys_ftruncate(l, &ta, retval);
374 }
375 #endif /* __alpha__ */
376
377 /*
378 * Linux 'readdir' call. This code is mostly taken from the
379 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
380 * an attempt has been made to keep it a little cleaner.
381 *
382 * The d_off field contains the offset of the next valid entry,
383 * unless the older Linux getdents(2), which used to have it set
384 * to the offset of the entry itself. This function also doesn't
385 * need to deal with the old count == 1 glibc problem.
386 *
387 * Read in BSD-style entries, convert them, and copy them out.
388 *
389 * Note that this doesn't handle union-mounted filesystems.
390 */
391 int
392 linux_sys_getdents64(struct lwp *l, const struct linux_sys_getdents64_args *uap, register_t *retval)
393 {
394 /* {
395 syscallarg(int) fd;
396 syscallarg(struct linux_dirent64 *) dent;
397 syscallarg(unsigned int) count;
398 } */
399 struct dirent *bdp;
400 struct vnode *vp;
401 char *inp, *tbuf; /* BSD-format */
402 int len, reclen; /* BSD-format */
403 char *outp; /* Linux-format */
404 int resid, linux_reclen = 0; /* Linux-format */
405 file_t *fp;
406 struct uio auio;
407 struct iovec aiov;
408 struct linux_dirent64 idb;
409 off_t off; /* true file offset */
410 int buflen, error, eofflag, nbytes;
411 struct vattr va;
412 off_t *cookiebuf = NULL, *cookie;
413 int ncookies;
414
415 /* fd_getvnode() will use the descriptor for us */
416 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
417 return (error);
418
419 if ((fp->f_flag & FREAD) == 0) {
420 error = EBADF;
421 goto out1;
422 }
423
424 vp = (struct vnode *)fp->f_data;
425 if (vp->v_type != VDIR) {
426 error = ENOTDIR;
427 goto out1;
428 }
429
430 vn_lock(vp, LK_SHARED | LK_RETRY);
431 error = VOP_GETATTR(vp, &va, l->l_cred);
432 VOP_UNLOCK(vp);
433 if (error)
434 goto out1;
435
436 nbytes = SCARG(uap, count);
437 buflen = uimin(MAXBSIZE, nbytes);
438 if (buflen < va.va_blocksize)
439 buflen = va.va_blocksize;
440 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
441
442 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
443 off = fp->f_offset;
444 again:
445 aiov.iov_base = tbuf;
446 aiov.iov_len = buflen;
447 auio.uio_iov = &aiov;
448 auio.uio_iovcnt = 1;
449 auio.uio_rw = UIO_READ;
450 auio.uio_resid = buflen;
451 auio.uio_offset = off;
452 UIO_SETUP_SYSSPACE(&auio);
453 /*
454 * First we read into the malloc'ed buffer, then
455 * we massage it into user space, one record at a time.
456 */
457 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
458 &ncookies);
459 if (error)
460 goto out;
461
462 inp = tbuf;
463 outp = (void *)SCARG(uap, dent);
464 resid = nbytes;
465 if ((len = buflen - auio.uio_resid) == 0)
466 goto eof;
467
468 for (cookie = cookiebuf; len > 0; len -= reclen) {
469 bdp = (struct dirent *)inp;
470 reclen = bdp->d_reclen;
471 if (reclen & 3) {
472 error = EIO;
473 goto out;
474 }
475 if (bdp->d_fileno == 0) {
476 inp += reclen; /* it is a hole; squish it out */
477 if (cookie)
478 off = *cookie++;
479 else
480 off += reclen;
481 continue;
482 }
483 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
484 if (reclen > len || resid < linux_reclen) {
485 /* entry too big for buffer, so just stop */
486 outp++;
487 break;
488 }
489 if (cookie)
490 off = *cookie++; /* each entry points to next */
491 else
492 off += reclen;
493 /*
494 * Massage in place to make a Linux-shaped dirent (otherwise
495 * we have to worry about touching user memory outside of
496 * the copyout() call).
497 */
498 memset(&idb, 0, sizeof(idb));
499 idb.d_ino = bdp->d_fileno;
500 idb.d_type = bdp->d_type;
501 idb.d_off = off;
502 idb.d_reclen = (u_short)linux_reclen;
503 memcpy(idb.d_name, bdp->d_name, MIN(sizeof(idb.d_name),
504 bdp->d_namlen + 1));
505 if ((error = copyout((void *)&idb, outp, linux_reclen)))
506 goto out;
507 /* advance past this real entry */
508 inp += reclen;
509 /* advance output past Linux-shaped entry */
510 outp += linux_reclen;
511 resid -= linux_reclen;
512 }
513
514 /* if we squished out the whole block, try again */
515 if (outp == (void *)SCARG(uap, dent)) {
516 if (cookiebuf)
517 free(cookiebuf, M_TEMP);
518 cookiebuf = NULL;
519 goto again;
520 }
521 fp->f_offset = off; /* update the vnode offset */
522
523 eof:
524 *retval = nbytes - resid;
525 out:
526 VOP_UNLOCK(vp);
527 if (cookiebuf)
528 free(cookiebuf, M_TEMP);
529 free(tbuf, M_TEMP);
530 out1:
531 fd_putfile(SCARG(uap, fd));
532 return error;
533 }
534