Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.65
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.65 2020/02/27 18:19:16 pgoyette 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.65 2020/02/27 18:19:16 pgoyette 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 void cvttimespec(struct timespec *, struct timespec50 *);
     79 static void cvtstat(struct stat *, struct stat43 *);
     80 
     81 static struct syscall_package vfs_syscalls_43_syscalls[] = {
     82 	{ SYS_compat_43_oquota,     0, (sy_call_t *)compat_43_sys_quota },
     83 	{ SYS_compat_43_stat43,     0, (sy_call_t *)compat_43_sys_stat },
     84 	{ SYS_compat_43_lstat43,    0, (sy_call_t *)compat_43_sys_lstat },
     85 	{ SYS_compat_43_fstat43,    0, (sy_call_t *)compat_43_sys_fstat },
     86 	{ SYS_compat_43_otruncate,  0, (sy_call_t *)compat_43_sys_ftruncate },
     87 	{ SYS_compat_43_oftruncate, 0, (sy_call_t *)compat_43_sys_ftruncate },
     88 	{ SYS_compat_43_olseek,     0, (sy_call_t *)compat_43_sys_lseek },
     89 	{ SYS_compat_43_ocreat,     0, (sy_call_t *)compat_43_sys_creat },
     90 	{ SYS_compat_43_ogetdirentries, 0,
     91 	    (sy_call_t *)compat_43_sys_getdirentries },
     92 	{ 0, 0, NULL }
     93 };
     94 
     95 /*
     96  * Convert from an old to a new timespec structure.
     97  */
     98 static void
     99 cvttimespec(struct timespec *ts, struct timespec50 *ots)
    100 {
    101 
    102 	if (ts->tv_sec > INT_MAX) {
    103 #if defined(DEBUG) || 1
    104 		static bool first = true;
    105 
    106 		if (first) {
    107 			first = false;
    108 			printf("%s[%s:%d]: time_t does not fit\n",
    109 			    __func__, curlwp->l_proc->p_comm,
    110 			    curlwp->l_lid);
    111 		}
    112 #endif
    113 		ots->tv_sec = INT_MAX;
    114 	} else
    115 		ots->tv_sec = ts->tv_sec;
    116 	ots->tv_nsec = ts->tv_nsec;
    117 }
    118 
    119 /*
    120  * Convert from an old to a new stat structure.
    121  */
    122 static void
    123 cvtstat(struct stat *st, struct stat43 *ost)
    124 {
    125 
    126 	/* Handle any padding. */
    127 	memset(ost, 0, sizeof *ost);
    128 	ost->st_dev = st->st_dev;
    129 	ost->st_ino = st->st_ino;
    130 	ost->st_mode = st->st_mode & 0xffff;
    131 	ost->st_nlink = st->st_nlink;
    132 	ost->st_uid = st->st_uid;
    133 	ost->st_gid = st->st_gid;
    134 	ost->st_rdev = st->st_rdev;
    135 	if (st->st_size < (quad_t)1 << 32)
    136 		ost->st_size = st->st_size;
    137 	else
    138 		ost->st_size = -2;
    139 	cvttimespec(&st->st_atimespec, &ost->st_atimespec);
    140 	cvttimespec(&st->st_mtimespec, &ost->st_mtimespec);
    141 	cvttimespec(&st->st_ctimespec, &ost->st_ctimespec);
    142 	ost->st_blksize = st->st_blksize;
    143 	ost->st_blocks = st->st_blocks;
    144 	ost->st_flags = st->st_flags;
    145 	ost->st_gen = st->st_gen;
    146 }
    147 
    148 /*
    149  * Get file status; this version follows links.
    150  */
    151 /* ARGSUSED */
    152 int
    153 compat_43_sys_stat(struct lwp *l, const struct compat_43_sys_stat_args *uap, register_t *retval)
    154 {
    155 	/* {
    156 		syscallarg(char *) path;
    157 		syscallarg(struct stat43 *) ub;
    158 	} */
    159 	struct stat sb;
    160 	struct stat43 osb;
    161 	int error;
    162 
    163 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    164 	if (error)
    165 		return (error);
    166 	cvtstat(&sb, &osb);
    167 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    168 	return (error);
    169 }
    170 
    171 /*
    172  * Get file status; this version does not follow links.
    173  */
    174 /* ARGSUSED */
    175 int
    176 compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
    177 {
    178 	/* {
    179 		syscallarg(char *) path;
    180 		syscallarg(struct ostat *) ub;
    181 	} */
    182 	struct vnode *vp, *dvp;
    183 	struct stat sb, sb1;
    184 	struct stat43 osb;
    185 	int error;
    186 	struct pathbuf *pb;
    187 	struct nameidata nd;
    188 	int ndflags;
    189 
    190 	error = pathbuf_copyin(SCARG(uap, path), &pb);
    191 	if (error) {
    192 		return error;
    193 	}
    194 
    195 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
    196 again:
    197 	NDINIT(&nd, LOOKUP, ndflags, pb);
    198 	if ((error = namei(&nd))) {
    199 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    200 			/*
    201 			 * Should only happen on '/'. Retry without LOCKPARENT;
    202 			 * this is safe since the vnode won't be a VLNK.
    203 			 */
    204 			ndflags &= ~LOCKPARENT;
    205 			goto again;
    206 		}
    207 		pathbuf_destroy(pb);
    208 		return (error);
    209 	}
    210 	/*
    211 	 * For symbolic links, always return the attributes of its
    212 	 * containing directory, except for mode, size, and links.
    213 	 */
    214 	vp = nd.ni_vp;
    215 	dvp = nd.ni_dvp;
    216 	pathbuf_destroy(pb);
    217 	if (vp->v_type != VLNK) {
    218 		if ((ndflags & LOCKPARENT) != 0) {
    219 			if (dvp == vp)
    220 				vrele(dvp);
    221 			else
    222 				vput(dvp);
    223 		}
    224 		error = vn_stat(vp, &sb);
    225 		vput(vp);
    226 		if (error)
    227 			return (error);
    228 	} else {
    229 		error = vn_stat(dvp, &sb);
    230 		vput(dvp);
    231 		if (error) {
    232 			vput(vp);
    233 			return (error);
    234 		}
    235 		error = vn_stat(vp, &sb1);
    236 		vput(vp);
    237 		if (error)
    238 			return (error);
    239 		sb.st_mode &= ~S_IFDIR;
    240 		sb.st_mode |= S_IFLNK;
    241 		sb.st_nlink = sb1.st_nlink;
    242 		sb.st_size = sb1.st_size;
    243 		sb.st_blocks = sb1.st_blocks;
    244 	}
    245 	cvtstat(&sb, &osb);
    246 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    247 	return (error);
    248 }
    249 
    250 /*
    251  * Return status information about a file descriptor.
    252  */
    253 /* ARGSUSED */
    254 int
    255 compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    256 {
    257 	/* {
    258 		syscallarg(int) fd;
    259 		syscallarg(struct stat43 *) sb;
    260 	} */
    261 	struct stat ub;
    262 	struct stat43 oub;
    263 	int error;
    264 
    265 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    266 	if (error == 0) {
    267 		cvtstat(&ub, &oub);
    268 		error = copyout((void *)&oub, (void *)SCARG(uap, sb),
    269 		    sizeof (oub));
    270 	}
    271 
    272 	return (error);
    273 }
    274 
    275 
    276 /*
    277  * Truncate a file given a file descriptor.
    278  */
    279 /* ARGSUSED */
    280 int
    281 compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    282 {
    283 	/* {
    284 		syscallarg(int) fd;
    285 		syscallarg(long) length;
    286 	} */
    287 	struct sys_ftruncate_args /* {
    288 		syscallarg(int) fd;
    289 		syscallarg(int) pad;
    290 		syscallarg(off_t) length;
    291 	} */ nuap;
    292 
    293 	SCARG(&nuap, fd) = SCARG(uap, fd);
    294 	SCARG(&nuap, length) = SCARG(uap, length);
    295 	return (sys_ftruncate(l, &nuap, retval));
    296 }
    297 
    298 /*
    299  * Truncate a file given its path name.
    300  */
    301 /* ARGSUSED */
    302 int
    303 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    304 {
    305 	/* {
    306 		syscallarg(char *) path;
    307 		syscallarg(long) length;
    308 	} */
    309 	struct sys_truncate_args /* {
    310 		syscallarg(char *) path;
    311 		syscallarg(int) pad;
    312 		syscallarg(off_t) length;
    313 	} */ nuap;
    314 
    315 	SCARG(&nuap, path) = SCARG(uap, path);
    316 	SCARG(&nuap, length) = SCARG(uap, length);
    317 	return (sys_truncate(l, &nuap, retval));
    318 }
    319 
    320 
    321 /*
    322  * Reposition read/write file offset.
    323  */
    324 int
    325 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    326 {
    327 	/* {
    328 		syscallarg(int) fd;
    329 		syscallarg(long) offset;
    330 		syscallarg(int) whence;
    331 	} */
    332 	struct sys_lseek_args /* {
    333 		syscallarg(int) fd;
    334 		syscallarg(int) pad;
    335 		syscallarg(off_t) offset;
    336 		syscallarg(int) whence;
    337 	} */ nuap;
    338 	off_t qret;
    339 	int error;
    340 
    341 	SCARG(&nuap, fd) = SCARG(uap, fd);
    342 	SCARG(&nuap, offset) = SCARG(uap, offset);
    343 	SCARG(&nuap, whence) = SCARG(uap, whence);
    344 	error = sys_lseek(l, &nuap, (register_t *)&qret);
    345 	*(long *)retval = qret;
    346 	return (error);
    347 }
    348 
    349 
    350 /*
    351  * Create a file.
    352  */
    353 int
    354 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    355 {
    356 	/* {
    357 		syscallarg(char *) path;
    358 		syscallarg(int) mode;
    359 	} */
    360 	struct sys_open_args /* {
    361 		syscallarg(char *) path;
    362 		syscallarg(int) flags;
    363 		syscallarg(int) mode;
    364 	} */ nuap;
    365 
    366 	SCARG(&nuap, path) = SCARG(uap, path);
    367 	SCARG(&nuap, mode) = SCARG(uap, mode);
    368 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    369 	return (sys_open(l, &nuap, retval));
    370 }
    371 
    372 /*ARGSUSED*/
    373 int
    374 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    375 {
    376 
    377 	return (ENOSYS);
    378 }
    379 
    380 
    381 /*
    382  * Read a block of directory entries in a file system independent format.
    383  */
    384 int
    385 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    386 {
    387 	/* {
    388 		syscallarg(int) fd;
    389 		syscallarg(char *) buf;
    390 		syscallarg(u_int) count;
    391 		syscallarg(long *) basep;
    392 	} */
    393 	struct dirent *bdp;
    394 	struct vnode *vp;
    395 	void *tbuf;			/* Current-format */
    396 	char *inp;			/* Current-format */
    397 	int len, reclen;		/* Current-format */
    398 	char *outp;			/* Dirent12-format */
    399 	int resid, old_reclen = 0;	/* Dirent12-format */
    400 	struct file *fp;
    401 	struct uio auio;
    402 	struct iovec aiov;
    403 	struct dirent43 idb;
    404 	off_t off;		/* true file offset */
    405 	int buflen, error, eofflag, nbytes;
    406 	struct vattr va;
    407 	off_t *cookiebuf = NULL, *cookie;
    408 	int ncookies;
    409 	long loff;
    410 
    411 	/* fd_getvnode() will use the descriptor for us */
    412 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    413 		return (error);
    414 
    415 	if ((fp->f_flag & FREAD) == 0) {
    416 		error = EBADF;
    417 		goto out1;
    418 	}
    419 
    420 	vp = fp->f_vnode;
    421 	if (vp->v_type != VDIR) {
    422 		error = ENOTDIR;
    423 		goto out1;
    424 	}
    425 
    426 	vn_lock(vp, LK_SHARED | LK_RETRY);
    427 	error = VOP_GETATTR(vp, &va, l->l_cred);
    428 	VOP_UNLOCK(vp);
    429 	if (error)
    430 		goto out1;
    431 
    432 	loff = fp->f_offset;
    433 	nbytes = SCARG(uap, count);
    434 	buflen = uimin(MAXBSIZE, nbytes);
    435 	if (buflen < va.va_blocksize)
    436 		buflen = va.va_blocksize;
    437 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    438 
    439 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    440 	off = fp->f_offset;
    441 again:
    442 	aiov.iov_base = tbuf;
    443 	aiov.iov_len = buflen;
    444 	auio.uio_iov = &aiov;
    445 	auio.uio_iovcnt = 1;
    446 	auio.uio_rw = UIO_READ;
    447 	auio.uio_resid = buflen;
    448 	auio.uio_offset = off;
    449 	UIO_SETUP_SYSSPACE(&auio);
    450 	/*
    451          * First we read into the malloc'ed buffer, then
    452          * we massage it into user space, one record at a time.
    453          */
    454 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    455 	    &ncookies);
    456 	if (error)
    457 		goto out;
    458 
    459 	inp = (char *)tbuf;
    460 	outp = SCARG(uap, buf);
    461 	resid = nbytes;
    462 	if ((len = buflen - auio.uio_resid) == 0)
    463 		goto eof;
    464 
    465 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    466 		bdp = (struct dirent *)inp;
    467 		reclen = bdp->d_reclen;
    468 		if (reclen & 3) {
    469 			error = EIO;
    470 			goto out;
    471 		}
    472 		if (bdp->d_fileno == 0) {
    473 			inp += reclen;	/* it is a hole; squish it out */
    474 			if (cookie)
    475 				off = *cookie++;
    476 			else
    477 				off += reclen;
    478 			continue;
    479 		}
    480 		if (bdp->d_namlen >= sizeof(idb.d_name))
    481 			idb.d_namlen = sizeof(idb.d_name) - 1;
    482 		else
    483 			idb.d_namlen = bdp->d_namlen;
    484 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    485 		if (reclen > len || resid < old_reclen) {
    486 			/* entry too big for buffer, so just stop */
    487 			outp++;
    488 			break;
    489 		}
    490 		/*
    491 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    492 		 * we have to worry about touching user memory outside of
    493 		 * the copyout() call).
    494 		 */
    495 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    496 		idb.d_reclen = (uint16_t)old_reclen;
    497 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    498 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    499 		memset(idb.d_name + idb.d_namlen, 0,
    500 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    501 		if ((error = copyout(&idb, outp, old_reclen)))
    502 			goto out;
    503 		/* advance past this real entry */
    504 		inp += reclen;
    505 		if (cookie)
    506 			off = *cookie++; /* each entry points to itself */
    507 		else
    508 			off += reclen;
    509 		/* advance output past Dirent12-shaped entry */
    510 		outp += old_reclen;
    511 		resid -= old_reclen;
    512 	}
    513 
    514 	/* if we squished out the whole block, try again */
    515 	if (outp == SCARG(uap, buf)) {
    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 	if (error)
    533 		return error;
    534 	return copyout(&loff, SCARG(uap, basep), sizeof(long));
    535 }
    536 
    537 int
    538 vfs_syscalls_43_init(void)
    539 {
    540 
    541 	return syscall_establish(NULL, vfs_syscalls_43_syscalls);
    542 }
    543 
    544 int
    545 vfs_syscalls_43_fini(void)
    546 {
    547 
    548 	return syscall_disestablish(NULL, vfs_syscalls_43_syscalls);
    549 }
    550