Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.68 2021/09/07 11:43:02 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.68 2021/09/07 11:43:02 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 	SCARG(&nuap, fd) = SCARG(uap, fd);
    237 	SCARG(&nuap, length) = SCARG(uap, length);
    238 	return sys_ftruncate(l, &nuap, retval);
    239 }
    240 
    241 /*
    242  * Truncate a file given its path name.
    243  */
    244 /* ARGSUSED */
    245 int
    246 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    247 {
    248 	/* {
    249 		syscallarg(char *) path;
    250 		syscallarg(long) length;
    251 	} */
    252 	struct sys_truncate_args /* {
    253 		syscallarg(char *) path;
    254 		syscallarg(int) pad;
    255 		syscallarg(off_t) length;
    256 	} */ nuap;
    257 
    258 	SCARG(&nuap, path) = SCARG(uap, path);
    259 	SCARG(&nuap, length) = SCARG(uap, length);
    260 	return (sys_truncate(l, &nuap, retval));
    261 }
    262 
    263 
    264 /*
    265  * Reposition read/write file offset.
    266  */
    267 int
    268 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    269 {
    270 	/* {
    271 		syscallarg(int) fd;
    272 		syscallarg(long) offset;
    273 		syscallarg(int) whence;
    274 	} */
    275 	struct sys_lseek_args /* {
    276 		syscallarg(int) fd;
    277 		syscallarg(int) pad;
    278 		syscallarg(off_t) offset;
    279 		syscallarg(int) whence;
    280 	} */ nuap;
    281 	off_t qret;
    282 	int error;
    283 
    284 	SCARG(&nuap, fd) = SCARG(uap, fd);
    285 	SCARG(&nuap, offset) = SCARG(uap, offset);
    286 	SCARG(&nuap, whence) = SCARG(uap, whence);
    287 	error = sys_lseek(l, &nuap, (register_t *)&qret);
    288 	*(long *)retval = qret;
    289 	return (error);
    290 }
    291 
    292 
    293 /*
    294  * Create a file.
    295  */
    296 int
    297 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    298 {
    299 	/* {
    300 		syscallarg(char *) path;
    301 		syscallarg(int) mode;
    302 	} */
    303 	struct sys_open_args /* {
    304 		syscallarg(char *) path;
    305 		syscallarg(int) flags;
    306 		syscallarg(int) mode;
    307 	} */ nuap;
    308 
    309 	SCARG(&nuap, path) = SCARG(uap, path);
    310 	SCARG(&nuap, mode) = SCARG(uap, mode);
    311 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    312 	return (sys_open(l, &nuap, retval));
    313 }
    314 
    315 /*ARGSUSED*/
    316 int
    317 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    318 {
    319 
    320 	return (ENOSYS);
    321 }
    322 
    323 
    324 /*
    325  * Read a block of directory entries in a file system independent format.
    326  */
    327 int
    328 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    329 {
    330 	/* {
    331 		syscallarg(int) fd;
    332 		syscallarg(char *) buf;
    333 		syscallarg(u_int) count;
    334 		syscallarg(long *) basep;
    335 	} */
    336 	struct dirent *bdp;
    337 	struct vnode *vp;
    338 	void *tbuf;			/* Current-format */
    339 	char *inp;			/* Current-format */
    340 	int len, reclen;		/* Current-format */
    341 	char *outp;			/* Dirent12-format */
    342 	int resid, old_reclen = 0;	/* Dirent12-format */
    343 	struct file *fp;
    344 	struct uio auio;
    345 	struct iovec aiov;
    346 	struct dirent43 idb;
    347 	off_t off;		/* true file offset */
    348 	int buflen, error, eofflag, nbytes;
    349 	struct vattr va;
    350 	off_t *cookiebuf = NULL, *cookie;
    351 	int ncookies;
    352 	long loff;
    353 
    354 	/* fd_getvnode() will use the descriptor for us */
    355 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    356 		return (error);
    357 
    358 	if ((fp->f_flag & FREAD) == 0) {
    359 		error = EBADF;
    360 		goto out1;
    361 	}
    362 
    363 	vp = fp->f_vnode;
    364 	if (vp->v_type != VDIR) {
    365 		error = ENOTDIR;
    366 		goto out1;
    367 	}
    368 
    369 	vn_lock(vp, LK_SHARED | LK_RETRY);
    370 	error = VOP_GETATTR(vp, &va, l->l_cred);
    371 	VOP_UNLOCK(vp);
    372 	if (error)
    373 		goto out1;
    374 
    375 	loff = fp->f_offset;
    376 	nbytes = SCARG(uap, count);
    377 	buflen = uimin(MAXBSIZE, nbytes);
    378 	if (buflen < va.va_blocksize)
    379 		buflen = va.va_blocksize;
    380 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    381 
    382 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    383 	off = fp->f_offset;
    384 again:
    385 	aiov.iov_base = tbuf;
    386 	aiov.iov_len = buflen;
    387 	auio.uio_iov = &aiov;
    388 	auio.uio_iovcnt = 1;
    389 	auio.uio_rw = UIO_READ;
    390 	auio.uio_resid = buflen;
    391 	auio.uio_offset = off;
    392 	UIO_SETUP_SYSSPACE(&auio);
    393 	/*
    394          * First we read into the malloc'ed buffer, then
    395          * we massage it into user space, one record at a time.
    396          */
    397 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    398 	    &ncookies);
    399 	if (error)
    400 		goto out;
    401 
    402 	inp = (char *)tbuf;
    403 	outp = SCARG(uap, buf);
    404 	resid = nbytes;
    405 	if ((len = buflen - auio.uio_resid) == 0)
    406 		goto eof;
    407 
    408 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    409 		bdp = (struct dirent *)inp;
    410 		reclen = bdp->d_reclen;
    411 		if (reclen & 3) {
    412 			error = EIO;
    413 			goto out;
    414 		}
    415 		if (bdp->d_fileno == 0) {
    416 			inp += reclen;	/* it is a hole; squish it out */
    417 			if (cookie)
    418 				off = *cookie++;
    419 			else
    420 				off += reclen;
    421 			continue;
    422 		}
    423 		memset(&idb, 0, sizeof(idb));
    424 		if (bdp->d_namlen >= sizeof(idb.d_name))
    425 			idb.d_namlen = sizeof(idb.d_name) - 1;
    426 		else
    427 			idb.d_namlen = bdp->d_namlen;
    428 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    429 		if (reclen > len || resid < old_reclen) {
    430 			/* entry too big for buffer, so just stop */
    431 			outp++;
    432 			break;
    433 		}
    434 		/*
    435 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    436 		 * we have to worry about touching user memory outside of
    437 		 * the copyout() call).
    438 		 */
    439 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    440 		idb.d_reclen = (uint16_t)old_reclen;
    441 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    442 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    443 		memset(idb.d_name + idb.d_namlen, 0,
    444 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    445 		if ((error = copyout(&idb, outp, old_reclen)))
    446 			goto out;
    447 		/* advance past this real entry */
    448 		inp += reclen;
    449 		if (cookie)
    450 			off = *cookie++; /* each entry points to itself */
    451 		else
    452 			off += reclen;
    453 		/* advance output past Dirent12-shaped entry */
    454 		outp += old_reclen;
    455 		resid -= old_reclen;
    456 	}
    457 
    458 	/* if we squished out the whole block, try again */
    459 	if (outp == SCARG(uap, buf)) {
    460 		if (cookiebuf)
    461 			free(cookiebuf, M_TEMP);
    462 		cookiebuf = NULL;
    463 		goto again;
    464 	}
    465 	fp->f_offset = off;	/* update the vnode offset */
    466 
    467 eof:
    468 	*retval = nbytes - resid;
    469 out:
    470 	VOP_UNLOCK(vp);
    471 	if (cookiebuf)
    472 		free(cookiebuf, M_TEMP);
    473 	free(tbuf, M_TEMP);
    474 out1:
    475 	fd_putfile(SCARG(uap, fd));
    476 	if (error)
    477 		return error;
    478 	return copyout(&loff, SCARG(uap, basep), sizeof(loff));
    479 }
    480 
    481 int
    482 vfs_syscalls_43_init(void)
    483 {
    484 
    485 	return syscall_establish(NULL, vfs_syscalls_43_syscalls);
    486 }
    487 
    488 int
    489 vfs_syscalls_43_fini(void)
    490 {
    491 
    492 	return syscall_disestablish(NULL, vfs_syscalls_43_syscalls);
    493 }
    494