Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.64.4.2
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.64.4.2 2021/08/15 09:27:50 martin 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.64.4.2 2021/08/15 09:27:50 martin 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 vnode *vp, *dvp;
    179 	struct stat sb, sb1;
    180 	struct stat43 osb;
    181 	int error;
    182 	struct pathbuf *pb;
    183 	struct nameidata nd;
    184 	int ndflags;
    185 
    186 	error = pathbuf_copyin(SCARG(uap, path), &pb);
    187 	if (error) {
    188 		return error;
    189 	}
    190 
    191 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
    192 again:
    193 	NDINIT(&nd, LOOKUP, ndflags, pb);
    194 	if ((error = namei(&nd))) {
    195 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    196 			/*
    197 			 * Should only happen on '/'. Retry without LOCKPARENT;
    198 			 * this is safe since the vnode won't be a VLNK.
    199 			 */
    200 			ndflags &= ~LOCKPARENT;
    201 			goto again;
    202 		}
    203 		pathbuf_destroy(pb);
    204 		return (error);
    205 	}
    206 	/*
    207 	 * For symbolic links, always return the attributes of its
    208 	 * containing directory, except for mode, size, and links.
    209 	 */
    210 	vp = nd.ni_vp;
    211 	dvp = nd.ni_dvp;
    212 	pathbuf_destroy(pb);
    213 	if (vp->v_type != VLNK) {
    214 		if ((ndflags & LOCKPARENT) != 0) {
    215 			if (dvp == vp)
    216 				vrele(dvp);
    217 			else
    218 				vput(dvp);
    219 		}
    220 		error = vn_stat(vp, &sb);
    221 		vput(vp);
    222 		if (error)
    223 			return (error);
    224 	} else {
    225 		error = vn_stat(dvp, &sb);
    226 		vput(dvp);
    227 		if (error) {
    228 			vput(vp);
    229 			return (error);
    230 		}
    231 		error = vn_stat(vp, &sb1);
    232 		vput(vp);
    233 		if (error)
    234 			return (error);
    235 		sb.st_mode &= ~S_IFDIR;
    236 		sb.st_mode |= S_IFLNK;
    237 		sb.st_nlink = sb1.st_nlink;
    238 		sb.st_size = sb1.st_size;
    239 		sb.st_blocks = sb1.st_blocks;
    240 	}
    241 	cvtstat(&osb, &sb);
    242 	return copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    243 }
    244 
    245 /*
    246  * Return status information about a file descriptor.
    247  */
    248 /* ARGSUSED */
    249 int
    250 compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    251 {
    252 	/* {
    253 		syscallarg(int) fd;
    254 		syscallarg(struct stat43 *) sb;
    255 	} */
    256 	struct stat sb;
    257 	struct stat43 osb;
    258 	int error;
    259 
    260 	error = do_sys_fstat(SCARG(uap, fd), &sb);
    261 	if (error)
    262 		return error;
    263 
    264 	cvtstat(&osb, &sb);
    265 	return copyout(&osb, SCARG(uap, sb), sizeof(osb));
    266 }
    267 
    268 
    269 /*
    270  * Truncate a file given a file descriptor.
    271  */
    272 /* ARGSUSED */
    273 int
    274 compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    275 {
    276 	/* {
    277 		syscallarg(int) fd;
    278 		syscallarg(long) length;
    279 	} */
    280 	struct sys_ftruncate_args /* {
    281 		syscallarg(int) fd;
    282 		syscallarg(int) pad;
    283 		syscallarg(off_t) length;
    284 	} */ nuap;
    285 
    286 	SCARG(&nuap, fd) = SCARG(uap, fd);
    287 	SCARG(&nuap, length) = SCARG(uap, length);
    288 	return sys_ftruncate(l, &nuap, retval);
    289 }
    290 
    291 /*
    292  * Truncate a file given its path name.
    293  */
    294 /* ARGSUSED */
    295 int
    296 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    297 {
    298 	/* {
    299 		syscallarg(char *) path;
    300 		syscallarg(long) length;
    301 	} */
    302 	struct sys_truncate_args /* {
    303 		syscallarg(char *) path;
    304 		syscallarg(int) pad;
    305 		syscallarg(off_t) length;
    306 	} */ nuap;
    307 
    308 	SCARG(&nuap, path) = SCARG(uap, path);
    309 	SCARG(&nuap, length) = SCARG(uap, length);
    310 	return (sys_truncate(l, &nuap, retval));
    311 }
    312 
    313 
    314 /*
    315  * Reposition read/write file offset.
    316  */
    317 int
    318 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    319 {
    320 	/* {
    321 		syscallarg(int) fd;
    322 		syscallarg(long) offset;
    323 		syscallarg(int) whence;
    324 	} */
    325 	struct sys_lseek_args /* {
    326 		syscallarg(int) fd;
    327 		syscallarg(int) pad;
    328 		syscallarg(off_t) offset;
    329 		syscallarg(int) whence;
    330 	} */ nuap;
    331 	off_t qret;
    332 	int error;
    333 
    334 	SCARG(&nuap, fd) = SCARG(uap, fd);
    335 	SCARG(&nuap, offset) = SCARG(uap, offset);
    336 	SCARG(&nuap, whence) = SCARG(uap, whence);
    337 	error = sys_lseek(l, &nuap, (register_t *)&qret);
    338 	*(long *)retval = qret;
    339 	return (error);
    340 }
    341 
    342 
    343 /*
    344  * Create a file.
    345  */
    346 int
    347 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    348 {
    349 	/* {
    350 		syscallarg(char *) path;
    351 		syscallarg(int) mode;
    352 	} */
    353 	struct sys_open_args /* {
    354 		syscallarg(char *) path;
    355 		syscallarg(int) flags;
    356 		syscallarg(int) mode;
    357 	} */ nuap;
    358 
    359 	SCARG(&nuap, path) = SCARG(uap, path);
    360 	SCARG(&nuap, mode) = SCARG(uap, mode);
    361 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    362 	return (sys_open(l, &nuap, retval));
    363 }
    364 
    365 /*ARGSUSED*/
    366 int
    367 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    368 {
    369 
    370 	return (ENOSYS);
    371 }
    372 
    373 
    374 /*
    375  * Read a block of directory entries in a file system independent format.
    376  */
    377 int
    378 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    379 {
    380 	/* {
    381 		syscallarg(int) fd;
    382 		syscallarg(char *) buf;
    383 		syscallarg(u_int) count;
    384 		syscallarg(long *) basep;
    385 	} */
    386 	struct dirent *bdp;
    387 	struct vnode *vp;
    388 	void *tbuf;			/* Current-format */
    389 	char *inp;			/* Current-format */
    390 	int len, reclen;		/* Current-format */
    391 	char *outp;			/* Dirent12-format */
    392 	int resid, old_reclen = 0;	/* Dirent12-format */
    393 	struct file *fp;
    394 	struct uio auio;
    395 	struct iovec aiov;
    396 	struct dirent43 idb;
    397 	off_t off;		/* true file offset */
    398 	int buflen, error, eofflag, nbytes;
    399 	struct vattr va;
    400 	off_t *cookiebuf = NULL, *cookie;
    401 	int ncookies;
    402 	long loff;
    403 
    404 	/* fd_getvnode() will use the descriptor for us */
    405 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    406 		return (error);
    407 
    408 	if ((fp->f_flag & FREAD) == 0) {
    409 		error = EBADF;
    410 		goto out1;
    411 	}
    412 
    413 	vp = fp->f_vnode;
    414 	if (vp->v_type != VDIR) {
    415 		error = ENOTDIR;
    416 		goto out1;
    417 	}
    418 
    419 	vn_lock(vp, LK_SHARED | LK_RETRY);
    420 	error = VOP_GETATTR(vp, &va, l->l_cred);
    421 	VOP_UNLOCK(vp);
    422 	if (error)
    423 		goto out1;
    424 
    425 	loff = fp->f_offset;
    426 	nbytes = SCARG(uap, count);
    427 	buflen = uimin(MAXBSIZE, nbytes);
    428 	if (buflen < va.va_blocksize)
    429 		buflen = va.va_blocksize;
    430 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    431 
    432 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    433 	off = fp->f_offset;
    434 again:
    435 	aiov.iov_base = tbuf;
    436 	aiov.iov_len = buflen;
    437 	auio.uio_iov = &aiov;
    438 	auio.uio_iovcnt = 1;
    439 	auio.uio_rw = UIO_READ;
    440 	auio.uio_resid = buflen;
    441 	auio.uio_offset = off;
    442 	UIO_SETUP_SYSSPACE(&auio);
    443 	/*
    444          * First we read into the malloc'ed buffer, then
    445          * we massage it into user space, one record at a time.
    446          */
    447 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    448 	    &ncookies);
    449 	if (error)
    450 		goto out;
    451 
    452 	inp = (char *)tbuf;
    453 	outp = SCARG(uap, buf);
    454 	resid = nbytes;
    455 	if ((len = buflen - auio.uio_resid) == 0)
    456 		goto eof;
    457 
    458 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    459 		bdp = (struct dirent *)inp;
    460 		reclen = bdp->d_reclen;
    461 		if (reclen & 3) {
    462 			error = EIO;
    463 			goto out;
    464 		}
    465 		if (bdp->d_fileno == 0) {
    466 			inp += reclen;	/* it is a hole; squish it out */
    467 			if (cookie)
    468 				off = *cookie++;
    469 			else
    470 				off += reclen;
    471 			continue;
    472 		}
    473 		if (bdp->d_namlen >= sizeof(idb.d_name))
    474 			idb.d_namlen = sizeof(idb.d_name) - 1;
    475 		else
    476 			idb.d_namlen = bdp->d_namlen;
    477 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    478 		if (reclen > len || resid < old_reclen) {
    479 			/* entry too big for buffer, so just stop */
    480 			outp++;
    481 			break;
    482 		}
    483 		/*
    484 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    485 		 * we have to worry about touching user memory outside of
    486 		 * the copyout() call).
    487 		 */
    488 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    489 		idb.d_reclen = (uint16_t)old_reclen;
    490 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    491 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    492 		memset(idb.d_name + idb.d_namlen, 0,
    493 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    494 		if ((error = copyout(&idb, outp, old_reclen)))
    495 			goto out;
    496 		/* advance past this real entry */
    497 		inp += reclen;
    498 		if (cookie)
    499 			off = *cookie++; /* each entry points to itself */
    500 		else
    501 			off += reclen;
    502 		/* advance output past Dirent12-shaped entry */
    503 		outp += old_reclen;
    504 		resid -= old_reclen;
    505 	}
    506 
    507 	/* if we squished out the whole block, try again */
    508 	if (outp == SCARG(uap, buf)) {
    509 		if (cookiebuf)
    510 			free(cookiebuf, M_TEMP);
    511 		cookiebuf = NULL;
    512 		goto again;
    513 	}
    514 	fp->f_offset = off;	/* update the vnode offset */
    515 
    516 eof:
    517 	*retval = nbytes - resid;
    518 out:
    519 	VOP_UNLOCK(vp);
    520 	if (cookiebuf)
    521 		free(cookiebuf, M_TEMP);
    522 	free(tbuf, M_TEMP);
    523 out1:
    524 	fd_putfile(SCARG(uap, fd));
    525 	if (error)
    526 		return error;
    527 	return copyout(&loff, SCARG(uap, basep), sizeof(loff));
    528 }
    529 
    530 int
    531 vfs_syscalls_43_init(void)
    532 {
    533 
    534 	return syscall_establish(NULL, vfs_syscalls_43_syscalls);
    535 }
    536 
    537 int
    538 vfs_syscalls_43_fini(void)
    539 {
    540 
    541 	return syscall_disestablish(NULL, vfs_syscalls_43_syscalls);
    542 }
    543