Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.69 2026/09/20 13:45:00 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.69 2026/09/20 13:45:00 riastradh Exp $");
     41 
     42 #if defined(_KERNEL_OPT)
     43 #include "opt_compat_netbsd.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/filedesc.h>
     49 #include <sys/kernel.h>
     50 #include <sys/proc.h>
     51 #include <sys/file.h>
     52 #include <sys/vnode.h>
     53 #include <sys/namei.h>
     54 #include <sys/dirent.h>
     55 #include <sys/socket.h>
     56 #include <sys/socketvar.h>
     57 #include <sys/stat.h>
     58 #include <sys/malloc.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/fcntl.h>
     61 #include <sys/syslog.h>
     62 #include <sys/unistd.h>
     63 #include <sys/resourcevar.h>
     64 
     65 #include <sys/mount.h>
     66 #include <sys/syscall.h>
     67 #include <sys/syscallvar.h>
     68 #include <sys/syscallargs.h>
     69 #include <sys/vfs_syscalls.h>
     70 
     71 #include <compat/sys/stat.h>
     72 #include <compat/sys/mount.h>
     73 #include <compat/sys/dirent.h>
     74 
     75 #include <compat/common/compat_util.h>
     76 #include <compat/common/compat_mod.h>
     77 
     78 static struct syscall_package vfs_syscalls_43_syscalls[] = {
     79 	{ SYS_compat_43_oquota,     0, (sy_call_t *)compat_43_sys_quota },
     80 	{ SYS_compat_43_stat43,     0, (sy_call_t *)compat_43_sys_stat },
     81 	{ SYS_compat_43_lstat43,    0, (sy_call_t *)compat_43_sys_lstat },
     82 	{ SYS_compat_43_fstat43,    0, (sy_call_t *)compat_43_sys_fstat },
     83 	{ SYS_compat_43_otruncate,  0, (sy_call_t *)compat_43_sys_ftruncate },
     84 	{ SYS_compat_43_oftruncate, 0, (sy_call_t *)compat_43_sys_ftruncate },
     85 	{ SYS_compat_43_olseek,     0, (sy_call_t *)compat_43_sys_lseek },
     86 	{ SYS_compat_43_ocreat,     0, (sy_call_t *)compat_43_sys_creat },
     87 	{ SYS_compat_43_ogetdirentries, 0,
     88 	    (sy_call_t *)compat_43_sys_getdirentries },
     89 	{ 0, 0, NULL }
     90 };
     91 
     92 /*
     93  * Convert from an old to a new timespec structure.
     94  */
     95 static void
     96 cvttimespec(struct timespec50 *ots, const struct timespec *ts)
     97 {
     98 
     99 	if (ts->tv_sec > INT_MAX) {
    100 #if defined(DEBUG) || 1
    101 		static bool first = true;
    102 
    103 		if (first) {
    104 			first = false;
    105 			printf("%s[%s:%d]: time_t does not fit\n",
    106 			    __func__, curlwp->l_proc->p_comm,
    107 			    curlwp->l_lid);
    108 		}
    109 #endif
    110 		ots->tv_sec = INT_MAX;
    111 	} else
    112 		ots->tv_sec = ts->tv_sec;
    113 	ots->tv_nsec = ts->tv_nsec;
    114 }
    115 
    116 /*
    117  * Convert from an old to a new stat structure.
    118  */
    119 static void
    120 cvtstat(struct stat43 *ost, const struct stat *st)
    121 {
    122 
    123 	/* Handle any padding. */
    124 	memset(ost, 0, sizeof(*ost));
    125 	ost->st_dev = st->st_dev;
    126 	ost->st_ino = st->st_ino;
    127 	ost->st_mode = st->st_mode & 0xffff;
    128 	ost->st_nlink = st->st_nlink;
    129 	ost->st_uid = st->st_uid;
    130 	ost->st_gid = st->st_gid;
    131 	ost->st_rdev = st->st_rdev;
    132 	if (st->st_size < (quad_t)1 << 32)
    133 		ost->st_size = st->st_size;
    134 	else
    135 		ost->st_size = -2;
    136 	cvttimespec(&ost->st_atimespec, &st->st_atimespec);
    137 	cvttimespec(&ost->st_mtimespec, &st->st_mtimespec);
    138 	cvttimespec(&ost->st_ctimespec, &st->st_ctimespec);
    139 	ost->st_blksize = st->st_blksize;
    140 	ost->st_blocks = st->st_blocks;
    141 	ost->st_flags = st->st_flags;
    142 	ost->st_gen = st->st_gen;
    143 }
    144 
    145 /*
    146  * Get file status; this version follows links.
    147  */
    148 /* ARGSUSED */
    149 int
    150 compat_43_sys_stat(struct lwp *l, const struct compat_43_sys_stat_args *uap, register_t *retval)
    151 {
    152 	/* {
    153 		syscallarg(char *) path;
    154 		syscallarg(struct stat43 *) ub;
    155 	} */
    156 	struct stat sb;
    157 	struct stat43 osb;
    158 	int error;
    159 
    160 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    161 	if (error)
    162 		return error;
    163 	cvtstat(&osb, &sb);
    164 	return copyout(&osb, SCARG(uap, ub), sizeof(osb));
    165 }
    166 
    167 /*
    168  * Get file status; this version does not follow links.
    169  */
    170 /* ARGSUSED */
    171 int
    172 compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
    173 {
    174 	/* {
    175 		syscallarg(char *) path;
    176 		syscallarg(struct stat43 *) ub;
    177 	} */
    178 	struct stat sb;
    179 	struct stat43 osb;
    180 	int error;
    181 
    182 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
    183 	if (error)
    184 		return error;
    185 
    186 	/*
    187 	 * For symbolic links, BSD4.3 returned the attributes of its
    188 	 * containing directory, except for mode, size, and links.
    189 	 * This is no longer emulated, the parent directory is not consulted.
    190 	 */
    191 	cvtstat(&osb, &sb);
    192 	return copyout(&osb, SCARG(uap, ub), sizeof(osb));
    193 }
    194 
    195 /*
    196  * Return status information about a file descriptor.
    197  */
    198 /* ARGSUSED */
    199 int
    200 compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    201 {
    202 	/* {
    203 		syscallarg(int) fd;
    204 		syscallarg(struct stat43 *) sb;
    205 	} */
    206 	struct stat sb;
    207 	struct stat43 osb;
    208 	int error;
    209 
    210 	error = do_sys_fstat(SCARG(uap, fd), &sb);
    211 	if (error)
    212 		return error;
    213 
    214 	cvtstat(&osb, &sb);
    215 	return copyout(&osb, SCARG(uap, sb), sizeof(osb));
    216 }
    217 
    218 
    219 /*
    220  * Truncate a file given a file descriptor.
    221  */
    222 /* ARGSUSED */
    223 int
    224 compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    225 {
    226 	/* {
    227 		syscallarg(int) fd;
    228 		syscallarg(long) length;
    229 	} */
    230 	struct sys_ftruncate_args /* {
    231 		syscallarg(int) fd;
    232 		syscallarg(int) pad;
    233 		syscallarg(off_t) length;
    234 	} */ nuap;
    235 
    236 	memset(&nuap, 0, sizeof(nuap));
    237 	SCARG(&nuap, fd) = SCARG(uap, fd);
    238 	SCARG(&nuap, length) = SCARG(uap, length);
    239 	return sys_ftruncate(l, &nuap, retval);
    240 }
    241 
    242 /*
    243  * Truncate a file given its path name.
    244  */
    245 /* ARGSUSED */
    246 int
    247 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    248 {
    249 	/* {
    250 		syscallarg(char *) path;
    251 		syscallarg(long) length;
    252 	} */
    253 	struct sys_truncate_args /* {
    254 		syscallarg(char *) path;
    255 		syscallarg(int) pad;
    256 		syscallarg(off_t) length;
    257 	} */ nuap;
    258 
    259 	memset(&nuap, 0, sizeof(nuap));
    260 	SCARG(&nuap, path) = SCARG(uap, path);
    261 	SCARG(&nuap, length) = SCARG(uap, length);
    262 	return (sys_truncate(l, &nuap, retval));
    263 }
    264 
    265 
    266 /*
    267  * Reposition read/write file offset.
    268  */
    269 int
    270 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    271 {
    272 	/* {
    273 		syscallarg(int) fd;
    274 		syscallarg(long) offset;
    275 		syscallarg(int) whence;
    276 	} */
    277 	struct sys_lseek_args /* {
    278 		syscallarg(int) fd;
    279 		syscallarg(int) pad;
    280 		syscallarg(off_t) offset;
    281 		syscallarg(int) whence;
    282 	} */ nuap;
    283 	off_t qret;
    284 	int error;
    285 
    286 	memset(&nuap, 0, sizeof(nuap));
    287 	SCARG(&nuap, fd) = SCARG(uap, fd);
    288 	SCARG(&nuap, offset) = SCARG(uap, offset);
    289 	SCARG(&nuap, whence) = SCARG(uap, whence);
    290 	error = sys_lseek(l, &nuap, (register_t *)&qret);
    291 	*(long *)retval = qret;
    292 	return (error);
    293 }
    294 
    295 
    296 /*
    297  * Create a file.
    298  */
    299 int
    300 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    301 {
    302 	/* {
    303 		syscallarg(char *) path;
    304 		syscallarg(int) mode;
    305 	} */
    306 	struct sys_open_args /* {
    307 		syscallarg(char *) path;
    308 		syscallarg(int) flags;
    309 		syscallarg(int) mode;
    310 	} */ nuap;
    311 
    312 	memset(&nuap, 0, sizeof(nuap));
    313 	SCARG(&nuap, path) = SCARG(uap, path);
    314 	SCARG(&nuap, mode) = SCARG(uap, mode);
    315 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    316 	return (sys_open(l, &nuap, retval));
    317 }
    318 
    319 /*ARGSUSED*/
    320 int
    321 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    322 {
    323 
    324 	return (ENOSYS);
    325 }
    326 
    327 
    328 /*
    329  * Read a block of directory entries in a file system independent format.
    330  */
    331 int
    332 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    333 {
    334 	/* {
    335 		syscallarg(int) fd;
    336 		syscallarg(char *) buf;
    337 		syscallarg(u_int) count;
    338 		syscallarg(long *) basep;
    339 	} */
    340 	struct dirent *bdp;
    341 	struct vnode *vp;
    342 	void *tbuf;			/* Current-format */
    343 	char *inp;			/* Current-format */
    344 	int len, reclen;		/* Current-format */
    345 	char *outp;			/* Dirent12-format */
    346 	int resid, old_reclen = 0;	/* Dirent12-format */
    347 	struct file *fp;
    348 	struct uio auio;
    349 	struct iovec aiov;
    350 	struct dirent43 idb;
    351 	off_t off;		/* true file offset */
    352 	int buflen, error, eofflag, nbytes;
    353 	struct vattr va;
    354 	off_t *cookiebuf = NULL, *cookie;
    355 	int ncookies;
    356 	long loff;
    357 
    358 	/* fd_getvnode() will use the descriptor for us */
    359 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    360 		return (error);
    361 
    362 	if ((fp->f_flag & FREAD) == 0) {
    363 		error = EBADF;
    364 		goto out1;
    365 	}
    366 
    367 	vp = fp->f_vnode;
    368 	if (vp->v_type != VDIR) {
    369 		error = ENOTDIR;
    370 		goto out1;
    371 	}
    372 
    373 	vn_lock(vp, LK_SHARED | LK_RETRY);
    374 	error = VOP_GETATTR(vp, &va, l->l_cred);
    375 	VOP_UNLOCK(vp);
    376 	if (error)
    377 		goto out1;
    378 
    379 	loff = fp->f_offset;
    380 	nbytes = SCARG(uap, count);
    381 	buflen = uimin(MAXBSIZE, nbytes);
    382 	if (buflen < va.va_blocksize)
    383 		buflen = va.va_blocksize;
    384 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    385 
    386 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    387 	off = fp->f_offset;
    388 again:
    389 	aiov.iov_base = tbuf;
    390 	aiov.iov_len = buflen;
    391 	auio.uio_iov = &aiov;
    392 	auio.uio_iovcnt = 1;
    393 	auio.uio_rw = UIO_READ;
    394 	auio.uio_resid = buflen;
    395 	auio.uio_offset = off;
    396 	UIO_SETUP_SYSSPACE(&auio);
    397 	/*
    398          * First we read into the malloc'ed buffer, then
    399          * we massage it into user space, one record at a time.
    400          */
    401 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    402 	    &ncookies);
    403 	if (error)
    404 		goto out;
    405 
    406 	inp = (char *)tbuf;
    407 	outp = SCARG(uap, buf);
    408 	resid = nbytes;
    409 	if ((len = buflen - auio.uio_resid) == 0)
    410 		goto eof;
    411 
    412 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    413 		bdp = (struct dirent *)inp;
    414 		reclen = bdp->d_reclen;
    415 		if (reclen & 3) {
    416 			error = EIO;
    417 			goto out;
    418 		}
    419 		if (bdp->d_fileno == 0) {
    420 			inp += reclen;	/* it is a hole; squish it out */
    421 			if (cookie)
    422 				off = *cookie++;
    423 			else
    424 				off += reclen;
    425 			continue;
    426 		}
    427 		memset(&idb, 0, sizeof(idb));
    428 		if (bdp->d_namlen >= sizeof(idb.d_name))
    429 			idb.d_namlen = sizeof(idb.d_name) - 1;
    430 		else
    431 			idb.d_namlen = bdp->d_namlen;
    432 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    433 		if (reclen > len || resid < old_reclen) {
    434 			/* entry too big for buffer, so just stop */
    435 			outp++;
    436 			break;
    437 		}
    438 		/*
    439 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    440 		 * we have to worry about touching user memory outside of
    441 		 * the copyout() call).
    442 		 */
    443 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    444 		idb.d_reclen = (uint16_t)old_reclen;
    445 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    446 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    447 		memset(idb.d_name + idb.d_namlen, 0,
    448 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    449 		if ((error = copyout(&idb, outp, old_reclen)))
    450 			goto out;
    451 		/* advance past this real entry */
    452 		inp += reclen;
    453 		if (cookie)
    454 			off = *cookie++; /* each entry points to itself */
    455 		else
    456 			off += reclen;
    457 		/* advance output past Dirent12-shaped entry */
    458 		outp += old_reclen;
    459 		resid -= old_reclen;
    460 	}
    461 
    462 	/* if we squished out the whole block, try again */
    463 	if (outp == SCARG(uap, buf)) {
    464 		if (cookiebuf)
    465 			free(cookiebuf, M_TEMP);
    466 		cookiebuf = NULL;
    467 		goto again;
    468 	}
    469 	fp->f_offset = off;	/* update the vnode offset */
    470 
    471 eof:
    472 	*retval = nbytes - resid;
    473 out:
    474 	VOP_UNLOCK(vp);
    475 	if (cookiebuf)
    476 		free(cookiebuf, M_TEMP);
    477 	free(tbuf, M_TEMP);
    478 out1:
    479 	fd_putfile(SCARG(uap, fd));
    480 	if (error)
    481 		return error;
    482 	return copyout(&loff, SCARG(uap, basep), sizeof(loff));
    483 }
    484 
    485 int
    486 vfs_syscalls_43_init(void)
    487 {
    488 
    489 	return syscall_establish(NULL, vfs_syscalls_43_syscalls);
    490 }
    491 
    492 int
    493 vfs_syscalls_43_fini(void)
    494 {
    495 
    496 	return syscall_disestablish(NULL, vfs_syscalls_43_syscalls);
    497 }
    498