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