Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.45
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.45 2007/12/20 23:02:45 dsl 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.45 2007/12/20 23:02:45 dsl Exp $");
     41 
     42 #if defined(_KERNEL_OPT)
     43 #include "fs_union.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 #include <sys/sysctl.h>
     65 
     66 #include <sys/mount.h>
     67 #include <sys/syscallargs.h>
     68 #include <sys/vfs_syscalls.h>
     69 
     70 #include <compat/sys/stat.h>
     71 #include <compat/sys/mount.h>
     72 
     73 static void cvtstat(struct stat *, struct stat43 *);
     74 
     75 /*
     76  * Convert from an old to a new stat structure.
     77  */
     78 static void
     79 cvtstat(struct stat *st, struct stat43 *ost)
     80 {
     81 
     82 	ost->st_dev = st->st_dev;
     83 	ost->st_ino = st->st_ino;
     84 	ost->st_mode = st->st_mode & 0xffff;
     85 	ost->st_nlink = st->st_nlink;
     86 	ost->st_uid = st->st_uid;
     87 	ost->st_gid = st->st_gid;
     88 	ost->st_rdev = st->st_rdev;
     89 	if (st->st_size < (quad_t)1 << 32)
     90 		ost->st_size = st->st_size;
     91 	else
     92 		ost->st_size = -2;
     93 	ost->st_atime = st->st_atime;
     94 	ost->st_mtime = st->st_mtime;
     95 	ost->st_ctime = st->st_ctime;
     96 	ost->st_blksize = st->st_blksize;
     97 	ost->st_blocks = st->st_blocks;
     98 	ost->st_flags = st->st_flags;
     99 	ost->st_gen = st->st_gen;
    100 }
    101 
    102 /*
    103  * Get file status; this version follows links.
    104  */
    105 /* ARGSUSED */
    106 int
    107 compat_43_sys_stat(struct lwp *l, const struct compat_43_sys_stat_args *uap, register_t *retval)
    108 {
    109 	/* {
    110 		syscallarg(char *) path;
    111 		syscallarg(struct stat43 *) ub;
    112 	} */
    113 	struct stat sb;
    114 	struct stat43 osb;
    115 	int error;
    116 
    117 	error = do_sys_stat(l, SCARG(uap, path), FOLLOW, &sb);
    118 	if (error)
    119 		return (error);
    120 	cvtstat(&sb, &osb);
    121 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    122 	return (error);
    123 }
    124 
    125 /*
    126  * Get file status; this version does not follow links.
    127  */
    128 /* ARGSUSED */
    129 int
    130 compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
    131 {
    132 	/* {
    133 		syscallarg(char *) path;
    134 		syscallarg(struct ostat *) ub;
    135 	} */
    136 	struct vnode *vp, *dvp;
    137 	struct stat sb, sb1;
    138 	struct stat43 osb;
    139 	int error;
    140 	struct nameidata nd;
    141 	int ndflags;
    142 
    143 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
    144 again:
    145 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, SCARG(uap, path));
    146 	if ((error = namei(&nd))) {
    147 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    148 			/*
    149 			 * Should only happen on '/'. Retry without LOCKPARENT;
    150 			 * this is safe since the vnode won't be a VLNK.
    151 			 */
    152 			ndflags &= ~LOCKPARENT;
    153 			goto again;
    154 		}
    155 		return (error);
    156 	}
    157 	/*
    158 	 * For symbolic links, always return the attributes of its
    159 	 * containing directory, except for mode, size, and links.
    160 	 */
    161 	vp = nd.ni_vp;
    162 	dvp = nd.ni_dvp;
    163 	if (vp->v_type != VLNK) {
    164 		if ((ndflags & LOCKPARENT) != 0) {
    165 			if (dvp == vp)
    166 				vrele(dvp);
    167 			else
    168 				vput(dvp);
    169 		}
    170 		error = vn_stat(vp, &sb, l);
    171 		vput(vp);
    172 		if (error)
    173 			return (error);
    174 	} else {
    175 		error = vn_stat(dvp, &sb, l);
    176 		vput(dvp);
    177 		if (error) {
    178 			vput(vp);
    179 			return (error);
    180 		}
    181 		error = vn_stat(vp, &sb1, l);
    182 		vput(vp);
    183 		if (error)
    184 			return (error);
    185 		sb.st_mode &= ~S_IFDIR;
    186 		sb.st_mode |= S_IFLNK;
    187 		sb.st_nlink = sb1.st_nlink;
    188 		sb.st_size = sb1.st_size;
    189 		sb.st_blocks = sb1.st_blocks;
    190 	}
    191 	cvtstat(&sb, &osb);
    192 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    193 	return (error);
    194 }
    195 
    196 /*
    197  * Return status information about a file descriptor.
    198  */
    199 /* ARGSUSED */
    200 int
    201 compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    202 {
    203 	/* {
    204 		syscallarg(int) fd;
    205 		syscallarg(struct stat43 *) sb;
    206 	} */
    207 	struct proc *p = l->l_proc;
    208 	int fd = SCARG(uap, fd);
    209 	struct filedesc *fdp = p->p_fd;
    210 	struct file *fp;
    211 	struct stat ub;
    212 	struct stat43 oub;
    213 	int error;
    214 
    215 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    216 		return (EBADF);
    217 
    218 	FILE_USE(fp);
    219 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    220 	FILE_UNUSE(fp, l);
    221 
    222 	if (error == 0) {
    223 		cvtstat(&ub, &oub);
    224 		error = copyout((void *)&oub, (void *)SCARG(uap, sb),
    225 		    sizeof (oub));
    226 	}
    227 
    228 
    229 	return (error);
    230 }
    231 
    232 
    233 /*
    234  * Truncate a file given a file descriptor.
    235  */
    236 /* ARGSUSED */
    237 int
    238 compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    239 {
    240 	/* {
    241 		syscallarg(int) fd;
    242 		syscallarg(long) length;
    243 	} */
    244 	struct sys_ftruncate_args /* {
    245 		syscallarg(int) fd;
    246 		syscallarg(int) pad;
    247 		syscallarg(off_t) length;
    248 	} */ nuap;
    249 
    250 	SCARG(&nuap, fd) = SCARG(uap, fd);
    251 	SCARG(&nuap, length) = SCARG(uap, length);
    252 	return (sys_ftruncate(l, &nuap, retval));
    253 }
    254 
    255 /*
    256  * Truncate a file given its path name.
    257  */
    258 /* ARGSUSED */
    259 int
    260 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    261 {
    262 	/* {
    263 		syscallarg(char *) path;
    264 		syscallarg(long) length;
    265 	} */
    266 	struct sys_truncate_args /* {
    267 		syscallarg(char *) path;
    268 		syscallarg(int) pad;
    269 		syscallarg(off_t) length;
    270 	} */ nuap;
    271 
    272 	SCARG(&nuap, path) = SCARG(uap, path);
    273 	SCARG(&nuap, length) = SCARG(uap, length);
    274 	return (sys_truncate(l, &nuap, retval));
    275 }
    276 
    277 
    278 /*
    279  * Reposition read/write file offset.
    280  */
    281 int
    282 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    283 {
    284 	/* {
    285 		syscallarg(int) fd;
    286 		syscallarg(long) offset;
    287 		syscallarg(int) whence;
    288 	} */
    289 	struct sys_lseek_args /* {
    290 		syscallarg(int) fd;
    291 		syscallarg(int) pad;
    292 		syscallarg(off_t) offset;
    293 		syscallarg(int) whence;
    294 	} */ nuap;
    295 	off_t qret;
    296 	int error;
    297 
    298 	SCARG(&nuap, fd) = SCARG(uap, fd);
    299 	SCARG(&nuap, offset) = SCARG(uap, offset);
    300 	SCARG(&nuap, whence) = SCARG(uap, whence);
    301 	error = sys_lseek(l, &nuap, (void *)&qret);
    302 	*(long *)retval = qret;
    303 	return (error);
    304 }
    305 
    306 
    307 /*
    308  * Create a file.
    309  */
    310 int
    311 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    312 {
    313 	/* {
    314 		syscallarg(char *) path;
    315 		syscallarg(int) mode;
    316 	} */
    317 	struct sys_open_args /* {
    318 		syscallarg(char *) path;
    319 		syscallarg(int) flags;
    320 		syscallarg(int) mode;
    321 	} */ nuap;
    322 
    323 	SCARG(&nuap, path) = SCARG(uap, path);
    324 	SCARG(&nuap, mode) = SCARG(uap, mode);
    325 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    326 	return (sys_open(l, &nuap, retval));
    327 }
    328 
    329 /*ARGSUSED*/
    330 int
    331 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    332 {
    333 
    334 	return (ENOSYS);
    335 }
    336 
    337 
    338 /*
    339  * Read a block of directory entries in a file system independent format.
    340  */
    341 int
    342 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    343 {
    344 	/* {
    345 		syscallarg(int) fd;
    346 		syscallarg(char *) buf;
    347 		syscallarg(u_int) count;
    348 		syscallarg(long *) basep;
    349 	} */
    350 	struct proc *p = l->l_proc;
    351 	struct vnode *vp;
    352 	struct file *fp;
    353 	struct uio auio, kuio;
    354 	struct iovec aiov, kiov;
    355 	struct dirent *dp, *edp;
    356 	char *dirbuf;
    357 	size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count));
    358 
    359 	int error, eofflag, readcnt;
    360 	long loff;
    361 
    362 	/* getvnode() will use the descriptor for us */
    363 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    364 		return (error);
    365 	if ((fp->f_flag & FREAD) == 0) {
    366 		error = EBADF;
    367 		goto out;
    368 	}
    369 	vp = (struct vnode *)fp->f_data;
    370 unionread:
    371 	if (vp->v_type != VDIR) {
    372 		error = EINVAL;
    373 		goto out;
    374 	}
    375 	aiov.iov_base = SCARG(uap, buf);
    376 	aiov.iov_len = count;
    377 	auio.uio_iov = &aiov;
    378 	auio.uio_iovcnt = 1;
    379 	auio.uio_rw = UIO_READ;
    380 	auio.uio_resid = count;
    381 	KASSERT(l == curlwp);
    382 	auio.uio_vmspace = l->l_proc->p_vmspace;
    383 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    384 	loff = auio.uio_offset = fp->f_offset;
    385 #	if (BYTE_ORDER != LITTLE_ENDIAN)
    386 		if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) {
    387 			error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
    388 			    (off_t **)0, (int *)0);
    389 			fp->f_offset = auio.uio_offset;
    390 		} else
    391 #	endif
    392 	{
    393 		kuio = auio;
    394 		kuio.uio_iov = &kiov;
    395 		kiov.iov_len = count;
    396 		dirbuf = malloc(count, M_TEMP, M_WAITOK);
    397 		kiov.iov_base = dirbuf;
    398 		UIO_SETUP_SYSSPACE(&kuio);
    399 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
    400 			    (off_t **)0, (int *)0);
    401 		fp->f_offset = kuio.uio_offset;
    402 		if (error == 0) {
    403 			readcnt = count - kuio.uio_resid;
    404 			edp = (struct dirent *)&dirbuf[readcnt];
    405 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
    406 #				if (BYTE_ORDER == LITTLE_ENDIAN)
    407 					/*
    408 					 * The expected low byte of
    409 					 * dp->d_namlen is our dp->d_type.
    410 					 * The high MBZ byte of dp->d_namlen
    411 					 * is our dp->d_namlen.
    412 					 */
    413 					dp->d_type = dp->d_namlen;
    414 					dp->d_namlen = 0;
    415 #				else
    416 					/*
    417 					 * The dp->d_type is the high byte
    418 					 * of the expected dp->d_namlen,
    419 					 * so must be zero'ed.
    420 					 */
    421 					dp->d_type = 0;
    422 #				endif
    423 				if (dp->d_reclen > 0) {
    424 					dp = (struct dirent *)
    425 					    ((char *)dp + dp->d_reclen);
    426 				} else {
    427 					error = EIO;
    428 					break;
    429 				}
    430 			}
    431 			if (dp >= edp)
    432 				error = uiomove(dirbuf, readcnt, &auio);
    433 		}
    434 		free(dirbuf, M_TEMP);
    435 	}
    436 	VOP_UNLOCK(vp, 0);
    437 	if (error)
    438 		goto out;
    439 
    440 #ifdef UNION
    441 {
    442 	extern int (**union_vnodeop_p)(void *);
    443 	extern struct vnode *union_dircache(struct vnode *);
    444 
    445 	if ((count == auio.uio_resid) &&
    446 	    (vp->v_op == union_vnodeop_p)) {
    447 		struct vnode *lvp;
    448 
    449 		lvp = union_dircache(vp);
    450 		if (lvp != NULLVP) {
    451 			struct vattr va;
    452 
    453 			/*
    454 			 * If the directory is opaque,
    455 			 * then don't show lower entries
    456 			 */
    457 			error = VOP_GETATTR(vp, &va, fp->f_cred);
    458 			if (va.va_flags & OPAQUE) {
    459 				vput(lvp);
    460 				lvp = NULL;
    461 			}
    462 		}
    463 
    464 		if (lvp != NULLVP) {
    465 			error = VOP_OPEN(lvp, FREAD, fp->f_cred);
    466 			VOP_UNLOCK(lvp, 0);
    467 
    468 			if (error) {
    469 				vrele(lvp);
    470 				goto out;
    471 			}
    472 			fp->f_data = (void *) lvp;
    473 			fp->f_offset = 0;
    474 			error = vn_close(vp, FREAD, fp->f_cred, l);
    475 			if (error)
    476 				goto out;
    477 			vp = lvp;
    478 			goto unionread;
    479 		}
    480 	}
    481 }
    482 #endif /* UNION */
    483 
    484 	if ((count == auio.uio_resid) &&
    485 	    (vp->v_vflag & VV_ROOT) &&
    486 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    487 		struct vnode *tvp = vp;
    488 		vp = vp->v_mount->mnt_vnodecovered;
    489 		VREF(vp);
    490 		fp->f_data = (void *) vp;
    491 		fp->f_offset = 0;
    492 		vrele(tvp);
    493 		goto unionread;
    494 	}
    495 	error = copyout((void *)&loff, (void *)SCARG(uap, basep),
    496 	    sizeof(long));
    497 	*retval = count - auio.uio_resid;
    498  out:
    499 	FILE_UNUSE(fp, l);
    500 	return (error);
    501 }
    502 
    503 /*
    504  * sysctl helper routine for vfs.generic.conf lookups.
    505  */
    506 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
    507 static int
    508 sysctl_vfs_generic_conf(SYSCTLFN_ARGS)
    509 {
    510         struct vfsconf vfc;
    511         extern const char * const mountcompatnames[];
    512         extern int nmountcompatnames;
    513 	struct sysctlnode node;
    514 	struct vfsops *vfsp;
    515 	u_int vfsnum;
    516 
    517 	if (namelen != 1)
    518 		return (ENOTDIR);
    519 	vfsnum = name[0];
    520 	if (vfsnum >= nmountcompatnames ||
    521 	    mountcompatnames[vfsnum] == NULL)
    522 		return (EOPNOTSUPP);
    523 	vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]);
    524 	if (vfsp == NULL)
    525 		return (EOPNOTSUPP);
    526 
    527 	vfc.vfc_vfsops = vfsp;
    528 	strncpy(vfc.vfc_name, vfsp->vfs_name, sizeof(vfc.vfc_name));
    529 	vfc.vfc_typenum = vfsnum;
    530 	vfc.vfc_refcount = vfsp->vfs_refcount;
    531 	vfc.vfc_flags = 0;
    532 	vfc.vfc_mountroot = vfsp->vfs_mountroot;
    533 	vfc.vfc_next = NULL;
    534 	vfs_delref(vfsp);
    535 
    536 	node = *rnode;
    537 	node.sysctl_data = &vfc;
    538 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    539 }
    540 
    541 /*
    542  * Top level filesystem related information gathering.
    543  */
    544 SYSCTL_SETUP(compat_sysctl_vfs_setup, "compat sysctl vfs subtree setup")
    545 {
    546 	extern int nmountcompatnames;
    547 
    548 	sysctl_createv(clog, 0, NULL, NULL,
    549 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    550 		       CTLTYPE_INT, "maxtypenum",
    551 		       SYSCTL_DESCR("Highest valid filesystem type number"),
    552 		       NULL, nmountcompatnames, NULL, 0,
    553 		       CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL);
    554 	sysctl_createv(clog, 0, NULL, NULL,
    555 		       CTLFLAG_PERMANENT,
    556 		       CTLTYPE_STRUCT, "conf",
    557 		       SYSCTL_DESCR("Filesystem configuration information"),
    558 		       sysctl_vfs_generic_conf, 0, NULL,
    559 		       sizeof(struct vfsconf),
    560 		       CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL);
    561 }
    562 #endif
    563