Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.19
      1 /*	$NetBSD: linux_file64.c,v 1.19 2003/01/18 08:02:52 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.19 2003/01/18 08:02:52 thorpej Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/namei.h>
     49 #include <sys/proc.h>
     50 #include <sys/dirent.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/filedesc.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/mount.h>
     57 #include <sys/malloc.h>
     58 #include <sys/vnode.h>
     59 #include <sys/tty.h>
     60 #include <sys/conf.h>
     61 
     62 #include <sys/sa.h>
     63 #include <sys/syscallargs.h>
     64 
     65 #include <compat/linux/common/linux_types.h>
     66 #include <compat/linux/common/linux_signal.h>
     67 #include <compat/linux/common/linux_fcntl.h>
     68 #include <compat/linux/common/linux_util.h>
     69 #include <compat/linux/common/linux_machdep.h>
     70 #include <compat/linux/common/linux_dirent.h>
     71 
     72 #include <compat/linux/linux_syscallargs.h>
     73 
     74 #ifndef alpha
     75 
     76 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     77 static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
     78 
     79 /*
     80  * Convert a NetBSD stat structure to a Linux stat structure.
     81  * Only the order of the fields and the padding in the structure
     82  * is different. linux_fakedev is a machine-dependent function
     83  * which optionally converts device driver major/minor numbers
     84  * (XXX horrible, but what can you do against code that compares
     85  * things against constant major device numbers? sigh)
     86  */
     87 static void
     88 bsd_to_linux_stat(bsp, lsp)
     89 	struct stat *bsp;
     90 	struct linux_stat64 *lsp;
     91 {
     92 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
     93 	lsp->lst_ino     = bsp->st_ino;
     94 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     95 	if (bsp->st_nlink >= (1 << 15))
     96 		lsp->lst_nlink = (1 << 15) - 1;
     97 	else
     98 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     99 	lsp->lst_uid     = bsp->st_uid;
    100 	lsp->lst_gid     = bsp->st_gid;
    101 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    102 	lsp->lst_size    = bsp->st_size;
    103 	lsp->lst_blksize = bsp->st_blksize;
    104 	lsp->lst_blocks  = bsp->st_blocks;
    105 	lsp->lst_atime   = bsp->st_atime;
    106 	lsp->lst_mtime   = bsp->st_mtime;
    107 	lsp->lst_ctime   = bsp->st_ctime;
    108 #if LINUX_STAT64_HAS_BROKEN_ST_INO
    109 	lsp->__lst_ino   = (linux_ino_t) bsp->st_ino;
    110 #endif
    111 }
    112 
    113 /*
    114  * The stat functions below are plain sailing. stat and lstat are handled
    115  * by one function to avoid code duplication.
    116  */
    117 int
    118 linux_sys_fstat64(l, v, retval)
    119 	struct lwp *l;
    120 	void *v;
    121 	register_t *retval;
    122 {
    123 	struct linux_sys_fstat64_args /* {
    124 		syscallarg(int) fd;
    125 		syscallarg(struct linux_stat64 *) sp;
    126 	} */ *uap = v;
    127 	struct proc *p = l->l_proc;
    128 	struct sys___fstat13_args fsa;
    129 	struct linux_stat64 tmplst;
    130 	struct stat *st,tmpst;
    131 	caddr_t sg;
    132 	int error;
    133 
    134 	sg = stackgap_init(p, 0);
    135 
    136 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    137 
    138 	SCARG(&fsa, fd) = SCARG(uap, fd);
    139 	SCARG(&fsa, sb) = st;
    140 
    141 	if ((error = sys___fstat13(l, &fsa, retval)))
    142 		return error;
    143 
    144 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    145 		return error;
    146 
    147 	bsd_to_linux_stat(&tmpst, &tmplst);
    148 
    149 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    150 		return error;
    151 
    152 	return 0;
    153 }
    154 
    155 static int
    156 linux_do_stat64(l, v, retval, dolstat)
    157 	struct lwp *l;
    158 	void *v;
    159 	register_t *retval;
    160 	int dolstat;
    161 {
    162 	struct proc *p = l->l_proc;
    163 	struct sys___stat13_args sa;
    164 	struct linux_stat64 tmplst;
    165 	struct stat *st, tmpst;
    166 	caddr_t sg;
    167 	int error;
    168 	struct linux_sys_stat64_args *uap = v;
    169 
    170 	sg = stackgap_init(p, 0);
    171 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    172 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    173 
    174 	SCARG(&sa, ub) = st;
    175 	SCARG(&sa, path) = SCARG(uap, path);
    176 
    177 	if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
    178 				sys___stat13(l, &sa, retval))))
    179 		return error;
    180 
    181 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    182 		return error;
    183 
    184 	bsd_to_linux_stat(&tmpst, &tmplst);
    185 
    186 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    187 		return error;
    188 
    189 	return 0;
    190 }
    191 
    192 int
    193 linux_sys_stat64(l, v, retval)
    194 	struct lwp *l;
    195 	void *v;
    196 	register_t *retval;
    197 {
    198 	struct linux_sys_stat64_args /* {
    199 		syscallarg(const char *) path;
    200 		syscallarg(struct linux_stat64 *) sp;
    201 	} */ *uap = v;
    202 
    203 	return linux_do_stat64(l, uap, retval, 0);
    204 }
    205 
    206 int
    207 linux_sys_lstat64(l, v, retval)
    208 	struct lwp *l;
    209 	void *v;
    210 	register_t *retval;
    211 {
    212 	struct linux_sys_lstat64_args /* {
    213 		syscallarg(const char *) path;
    214 		syscallarg(struct linux_stat64 *) sp;
    215 	} */ *uap = v;
    216 
    217 	return linux_do_stat64(l, uap, retval, 1);
    218 }
    219 
    220 int
    221 linux_sys_truncate64(l, v, retval)
    222 	struct lwp *l;
    223 	void *v;
    224 	register_t *retval;
    225 {
    226 	struct linux_sys_truncate64_args /* {
    227 		syscallarg(const char *) path;
    228 		syscallarg(off_t) length;
    229 	} */ *uap = v;
    230 	struct proc *p = l->l_proc;
    231 	caddr_t sg = stackgap_init(p, 0);
    232 
    233 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    234 
    235 	return sys_truncate(l, uap, retval);
    236 }
    237 
    238 #if !defined(__m68k__)
    239 static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
    240     const struct flock *));
    241 static void linux_to_bsd_flock64 __P((struct flock *,
    242     const struct linux_flock64 *));
    243 
    244 static void
    245 bsd_to_linux_flock64(lfp, bfp)
    246 	struct linux_flock64 *lfp;
    247 	const struct flock *bfp;
    248 {
    249 
    250 	lfp->l_start = bfp->l_start;
    251 	lfp->l_len = bfp->l_len;
    252 	lfp->l_pid = bfp->l_pid;
    253 	lfp->l_whence = bfp->l_whence;
    254 	switch (bfp->l_type) {
    255 	case F_RDLCK:
    256 		lfp->l_type = LINUX_F_RDLCK;
    257 		break;
    258 	case F_UNLCK:
    259 		lfp->l_type = LINUX_F_UNLCK;
    260 		break;
    261 	case F_WRLCK:
    262 		lfp->l_type = LINUX_F_WRLCK;
    263 		break;
    264 	}
    265 }
    266 
    267 static void
    268 linux_to_bsd_flock64(bfp, lfp)
    269 	struct flock *bfp;
    270 	const struct linux_flock64 *lfp;
    271 {
    272 
    273 	bfp->l_start = lfp->l_start;
    274 	bfp->l_len = lfp->l_len;
    275 	bfp->l_pid = lfp->l_pid;
    276 	bfp->l_whence = lfp->l_whence;
    277 	switch (lfp->l_type) {
    278 	case LINUX_F_RDLCK:
    279 		bfp->l_type = F_RDLCK;
    280 		break;
    281 	case LINUX_F_UNLCK:
    282 		bfp->l_type = F_UNLCK;
    283 		break;
    284 	case LINUX_F_WRLCK:
    285 		bfp->l_type = F_WRLCK;
    286 		break;
    287 	}
    288 }
    289 
    290 int
    291 linux_sys_fcntl64(l, v, retval)
    292 	struct lwp *l;
    293 	void *v;
    294 	register_t *retval;
    295 {
    296 	struct linux_sys_fcntl64_args /* {
    297 		syscallarg(int) fd;
    298 		syscallarg(int) cmd;
    299 		syscallarg(void *) arg;
    300 	} */ *uap = v;
    301 	struct proc *p = l->l_proc;
    302 	struct sys_fcntl_args fca;
    303 	struct linux_flock64 lfl;
    304 	struct flock bfl, *bfp;
    305 	int error;
    306 	caddr_t sg;
    307 	void *arg = SCARG(uap, arg);
    308 	int cmd = SCARG(uap, cmd);
    309 	int fd = SCARG(uap, fd);
    310 
    311 	switch (cmd) {
    312 	case LINUX_F_GETLK64:
    313 		sg = stackgap_init(p, 0);
    314 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    315 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    316 			return error;
    317 		linux_to_bsd_flock64(&bfl, &lfl);
    318 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    319 			return error;
    320 		SCARG(&fca, fd) = fd;
    321 		SCARG(&fca, cmd) = F_GETLK;
    322 		SCARG(&fca, arg) = bfp;
    323 		if ((error = sys_fcntl(l, &fca, retval)) != 0)
    324 			return error;
    325 		if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
    326 			return error;
    327 		bsd_to_linux_flock64(&lfl, &bfl);
    328 		return copyout(&lfl, arg, sizeof lfl);
    329 	case LINUX_F_SETLK64:
    330 	case LINUX_F_SETLKW64:
    331 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    332 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    333 			return error;
    334 		linux_to_bsd_flock64(&bfl, &lfl);
    335 		sg = stackgap_init(p, 0);
    336 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    337 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    338 			return error;
    339 		SCARG(&fca, fd) = fd;
    340 		SCARG(&fca, cmd) = cmd;
    341 		SCARG(&fca, arg) = bfp;
    342 		return sys_fcntl(l, &fca, retval);
    343 	default:
    344 		return linux_sys_fcntl(l, v, retval);
    345 	}
    346 }
    347 #endif /* !m68k */
    348 
    349 #endif /* !alpha */
    350 
    351 /*
    352  * Linux 'readdir' call. This code is mostly taken from the
    353  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    354  * an attempt has been made to keep it a little cleaner.
    355  *
    356  * The d_off field contains the offset of the next valid entry,
    357  * unless the older Linux getdents(2), which used to have it set
    358  * to the offset of the entry itself. This function also doesn't
    359  * need to deal with the old count == 1 glibc problem.
    360  *
    361  * Read in BSD-style entries, convert them, and copy them out.
    362  *
    363  * Note that this doesn't handle union-mounted filesystems.
    364  */
    365 int
    366 linux_sys_getdents64(l, v, retval)
    367 	struct lwp *l;
    368 	void *v;
    369 	register_t *retval;
    370 {
    371 	struct linux_sys_getdents_args /* {
    372 		syscallarg(int) fd;
    373 		syscallarg(struct linux_dirent64 *) dent;
    374 		syscallarg(unsigned int) count;
    375 	} */ *uap = v;
    376 	struct proc *p = l->l_proc;
    377 	struct dirent *bdp;
    378 	struct vnode *vp;
    379 	caddr_t	inp, buf;		/* BSD-format */
    380 	int len, reclen;		/* BSD-format */
    381 	caddr_t outp;			/* Linux-format */
    382 	int resid, linux_reclen = 0;	/* Linux-format */
    383 	struct file *fp;
    384 	struct uio auio;
    385 	struct iovec aiov;
    386 	struct linux_dirent64 idb;
    387 	off_t off;		/* true file offset */
    388 	int buflen, error, eofflag, nbytes;
    389 	struct vattr va;
    390 	off_t *cookiebuf = NULL, *cookie;
    391 	int ncookies;
    392 
    393 	/* getvnode() will use the descriptor for us */
    394 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    395 		return (error);
    396 
    397 	if ((fp->f_flag & FREAD) == 0) {
    398 		error = EBADF;
    399 		goto out1;
    400 	}
    401 
    402 	vp = (struct vnode *)fp->f_data;
    403 	if (vp->v_type != VDIR) {
    404 		error = EINVAL;
    405 		goto out1;
    406 	}
    407 
    408 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    409 		goto out1;
    410 
    411 	nbytes = SCARG(uap, count);
    412 	buflen = min(MAXBSIZE, nbytes);
    413 	if (buflen < va.va_blocksize)
    414 		buflen = va.va_blocksize;
    415 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    416 
    417 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    418 	off = fp->f_offset;
    419 again:
    420 	aiov.iov_base = buf;
    421 	aiov.iov_len = buflen;
    422 	auio.uio_iov = &aiov;
    423 	auio.uio_iovcnt = 1;
    424 	auio.uio_rw = UIO_READ;
    425 	auio.uio_segflg = UIO_SYSSPACE;
    426 	auio.uio_procp = p;
    427 	auio.uio_resid = buflen;
    428 	auio.uio_offset = off;
    429 	/*
    430          * First we read into the malloc'ed buffer, then
    431          * we massage it into user space, one record at a time.
    432          */
    433 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    434 	    &ncookies);
    435 	if (error)
    436 		goto out;
    437 
    438 	inp = buf;
    439 	outp = (caddr_t)SCARG(uap, dent);
    440 	resid = nbytes;
    441 	if ((len = buflen - auio.uio_resid) == 0)
    442 		goto eof;
    443 
    444 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    445 		bdp = (struct dirent *)inp;
    446 		reclen = bdp->d_reclen;
    447 		if (reclen & 3)
    448 			panic("linux_readdir");
    449 		if (bdp->d_fileno == 0) {
    450 			inp += reclen;	/* it is a hole; squish it out */
    451 			off = *cookie++;
    452 			continue;
    453 		}
    454 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    455 		if (reclen > len || resid < linux_reclen) {
    456 			/* entry too big for buffer, so just stop */
    457 			outp++;
    458 			break;
    459 		}
    460 		off = *cookie++;	/* each entry points to next */
    461 		/*
    462 		 * Massage in place to make a Linux-shaped dirent (otherwise
    463 		 * we have to worry about touching user memory outside of
    464 		 * the copyout() call).
    465 		 */
    466 		idb.d_ino = bdp->d_fileno;
    467 		idb.d_type = bdp->d_type;
    468 		idb.d_off = off;
    469 		idb.d_reclen = (u_short)linux_reclen;
    470 		strcpy(idb.d_name, bdp->d_name);
    471 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    472 			goto out;
    473 		/* advance past this real entry */
    474 		inp += reclen;
    475 		/* advance output past Linux-shaped entry */
    476 		outp += linux_reclen;
    477 		resid -= linux_reclen;
    478 	}
    479 
    480 	/* if we squished out the whole block, try again */
    481 	if (outp == (caddr_t)SCARG(uap, dent))
    482 		goto again;
    483 	fp->f_offset = off;	/* update the vnode offset */
    484 
    485 eof:
    486 	*retval = nbytes - resid;
    487 out:
    488 	VOP_UNLOCK(vp, 0);
    489 	if (cookiebuf)
    490 		free(cookiebuf, M_TEMP);
    491 	free(buf, M_TEMP);
    492 out1:
    493 	FILE_UNUSE(fp, p);
    494 	return error;
    495 }
    496