Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.32.2.1
      1 /*	$NetBSD: linux_file64.c,v 1.32.2.1 2006/05/24 10:57:28 yamt 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.32.2.1 2006/05/24 10:57:28 yamt Exp $");
     45 
     46 #include "opt_ktrace.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/namei.h>
     51 #include <sys/proc.h>
     52 #include <sys/dirent.h>
     53 #include <sys/file.h>
     54 #include <sys/stat.h>
     55 #include <sys/filedesc.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/kernel.h>
     58 #include <sys/mount.h>
     59 #include <sys/malloc.h>
     60 #include <sys/vnode.h>
     61 #include <sys/tty.h>
     62 #include <sys/conf.h>
     63 #if defined(KTRACE)
     64 #include <sys/ktrace.h>
     65 #endif /* defined(KTRACE) */
     66 
     67 #include <sys/sa.h>
     68 #include <sys/syscallargs.h>
     69 
     70 #include <compat/linux/common/linux_types.h>
     71 #include <compat/linux/common/linux_signal.h>
     72 #include <compat/linux/common/linux_fcntl.h>
     73 #include <compat/linux/common/linux_util.h>
     74 #include <compat/linux/common/linux_machdep.h>
     75 #include <compat/linux/common/linux_dirent.h>
     76 
     77 #include <compat/linux/linux_syscallargs.h>
     78 
     79 #ifndef alpha
     80 
     81 # ifndef COMPAT_LINUX32
     82 
     83 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     84 static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
     85 
     86 /*
     87  * Convert a NetBSD stat structure to a Linux stat structure.
     88  * Only the order of the fields and the padding in the structure
     89  * is different. linux_fakedev is a machine-dependent function
     90  * which optionally converts device driver major/minor numbers
     91  * (XXX horrible, but what can you do against code that compares
     92  * things against constant major device numbers? sigh)
     93  */
     94 static void
     95 bsd_to_linux_stat(bsp, lsp)
     96 	struct stat *bsp;
     97 	struct linux_stat64 *lsp;
     98 {
     99 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    100 	lsp->lst_ino     = bsp->st_ino;
    101 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    102 	if (bsp->st_nlink >= (1 << 15))
    103 		lsp->lst_nlink = (1 << 15) - 1;
    104 	else
    105 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    106 	lsp->lst_uid     = bsp->st_uid;
    107 	lsp->lst_gid     = bsp->st_gid;
    108 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    109 	lsp->lst_size    = bsp->st_size;
    110 	lsp->lst_blksize = bsp->st_blksize;
    111 	lsp->lst_blocks  = bsp->st_blocks;
    112 	lsp->lst_atime   = bsp->st_atime;
    113 	lsp->lst_mtime   = bsp->st_mtime;
    114 	lsp->lst_ctime   = bsp->st_ctime;
    115 #  ifdef LINUX_STAT64_HAS_NSEC
    116 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    117 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    118 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    119 #  endif
    120 #  if LINUX_STAT64_HAS_BROKEN_ST_INO
    121 	lsp->__lst_ino   = (linux_ino_t) bsp->st_ino;
    122 #  endif
    123 }
    124 
    125 /*
    126  * The stat functions below are plain sailing. stat and lstat are handled
    127  * by one function to avoid code duplication.
    128  */
    129 int
    130 linux_sys_fstat64(l, v, retval)
    131 	struct lwp *l;
    132 	void *v;
    133 	register_t *retval;
    134 {
    135 	struct linux_sys_fstat64_args /* {
    136 		syscallarg(int) fd;
    137 		syscallarg(struct linux_stat64 *) sp;
    138 	} */ *uap = v;
    139 	struct proc *p = l->l_proc;
    140 	struct sys___fstat30_args fsa;
    141 	struct linux_stat64 tmplst;
    142 	struct stat *st,tmpst;
    143 	caddr_t sg;
    144 	int error;
    145 
    146 	sg = stackgap_init(p, 0);
    147 
    148 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    149 
    150 	SCARG(&fsa, fd) = SCARG(uap, fd);
    151 	SCARG(&fsa, sb) = st;
    152 
    153 	if ((error = sys___fstat30(l, &fsa, retval)))
    154 		return error;
    155 
    156 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    157 		return error;
    158 
    159 	bsd_to_linux_stat(&tmpst, &tmplst);
    160 
    161 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    162 		return error;
    163 
    164 	return 0;
    165 }
    166 
    167 static int
    168 linux_do_stat64(l, v, retval, dolstat)
    169 	struct lwp *l;
    170 	void *v;
    171 	register_t *retval;
    172 	int dolstat;
    173 {
    174 	struct proc *p = l->l_proc;
    175 	struct sys___stat30_args sa;
    176 	struct linux_stat64 tmplst;
    177 	struct stat *st, tmpst;
    178 	caddr_t sg;
    179 	int error;
    180 	struct linux_sys_stat64_args *uap = v;
    181 
    182 	sg = stackgap_init(p, 0);
    183 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    184 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    185 
    186 	SCARG(&sa, ub) = st;
    187 	SCARG(&sa, path) = SCARG(uap, path);
    188 
    189 	if ((error = (dolstat ? sys___lstat30(l, &sa, retval) :
    190 				sys___stat30(l, &sa, retval))))
    191 		return error;
    192 
    193 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    194 		return error;
    195 
    196 	bsd_to_linux_stat(&tmpst, &tmplst);
    197 
    198 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    199 		return error;
    200 
    201 	return 0;
    202 }
    203 
    204 int
    205 linux_sys_stat64(l, v, retval)
    206 	struct lwp *l;
    207 	void *v;
    208 	register_t *retval;
    209 {
    210 	struct linux_sys_stat64_args /* {
    211 		syscallarg(const char *) path;
    212 		syscallarg(struct linux_stat64 *) sp;
    213 	} */ *uap = v;
    214 
    215 	return linux_do_stat64(l, uap, retval, 0);
    216 }
    217 
    218 int
    219 linux_sys_lstat64(l, v, retval)
    220 	struct lwp *l;
    221 	void *v;
    222 	register_t *retval;
    223 {
    224 	struct linux_sys_lstat64_args /* {
    225 		syscallarg(const char *) path;
    226 		syscallarg(struct linux_stat64 *) sp;
    227 	} */ *uap = v;
    228 
    229 	return linux_do_stat64(l, uap, retval, 1);
    230 }
    231 
    232 int
    233 linux_sys_truncate64(l, v, retval)
    234 	struct lwp *l;
    235 	void *v;
    236 	register_t *retval;
    237 {
    238 	struct linux_sys_truncate64_args /* {
    239 		syscallarg(const char *) path;
    240 		syscallarg(off_t) length;
    241 	} */ *uap = v;
    242 	struct sys_truncate_args ta;
    243 	struct proc *p = l->l_proc;
    244 	caddr_t sg = stackgap_init(p, 0);
    245 
    246 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    247 
    248 	/* Linux doesn't have the 'pad' pseudo-parameter */
    249 	SCARG(&ta, path) = SCARG(uap, path);
    250 	SCARG(&ta, pad) = 0;
    251 	SCARG(&ta, length) = SCARG(uap, length);
    252 
    253 	return sys_truncate(l, &ta, retval);
    254 }
    255 
    256 int
    257 linux_sys_ftruncate64(l, v, retval)
    258 	struct lwp *l;
    259 	void *v;
    260 	register_t *retval;
    261 {
    262 	struct linux_sys_ftruncate64_args /* {
    263 		syscallarg(unsigned int) fd;
    264 		syscallarg(off_t) length;
    265 	} */ *uap = v;
    266 	struct sys_ftruncate_args ta;
    267 
    268 	/* Linux doesn't have the 'pad' pseudo-parameter */
    269 	SCARG(&ta, fd) = SCARG(uap, fd);
    270 	SCARG(&ta, pad) = 0;
    271 	SCARG(&ta, length) = SCARG(uap, length);
    272 
    273 	return sys_ftruncate(l, &ta, retval);
    274 }
    275 # endif /* !COMPAT_LINUX32 */
    276 
    277 # if !defined(__m68k__) && (!defined(__amd64__) || defined(COMPAT_LINUX32))
    278 static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
    279     const struct flock *));
    280 static void linux_to_bsd_flock64 __P((struct flock *,
    281     const struct linux_flock64 *));
    282 
    283 static void
    284 bsd_to_linux_flock64(lfp, bfp)
    285 	struct linux_flock64 *lfp;
    286 	const struct flock *bfp;
    287 {
    288 
    289 	lfp->l_start = bfp->l_start;
    290 	lfp->l_len = bfp->l_len;
    291 	lfp->l_pid = bfp->l_pid;
    292 	lfp->l_whence = bfp->l_whence;
    293 	switch (bfp->l_type) {
    294 	case F_RDLCK:
    295 		lfp->l_type = LINUX_F_RDLCK;
    296 		break;
    297 	case F_UNLCK:
    298 		lfp->l_type = LINUX_F_UNLCK;
    299 		break;
    300 	case F_WRLCK:
    301 		lfp->l_type = LINUX_F_WRLCK;
    302 		break;
    303 	}
    304 }
    305 
    306 static void
    307 linux_to_bsd_flock64(bfp, lfp)
    308 	struct flock *bfp;
    309 	const struct linux_flock64 *lfp;
    310 {
    311 
    312 	bfp->l_start = lfp->l_start;
    313 	bfp->l_len = lfp->l_len;
    314 	bfp->l_pid = lfp->l_pid;
    315 	bfp->l_whence = lfp->l_whence;
    316 	switch (lfp->l_type) {
    317 	case LINUX_F_RDLCK:
    318 		bfp->l_type = F_RDLCK;
    319 		break;
    320 	case LINUX_F_UNLCK:
    321 		bfp->l_type = F_UNLCK;
    322 		break;
    323 	case LINUX_F_WRLCK:
    324 		bfp->l_type = F_WRLCK;
    325 		break;
    326 	}
    327 }
    328 
    329 int
    330 linux_sys_fcntl64(l, v, retval)
    331 	struct lwp *l;
    332 	void *v;
    333 	register_t *retval;
    334 {
    335 	struct linux_sys_fcntl64_args /* {
    336 		syscallarg(int) fd;
    337 		syscallarg(int) cmd;
    338 		syscallarg(void *) arg;
    339 	} */ *uap = v;
    340 	struct proc *p = l->l_proc;
    341 	struct sys_fcntl_args fca;
    342 	struct linux_flock64 lfl;
    343 	struct flock bfl, *bfp;
    344 	int error;
    345 	caddr_t sg;
    346 	void *arg = SCARG(uap, arg);
    347 	int cmd = SCARG(uap, cmd);
    348 	int fd = SCARG(uap, fd);
    349 
    350 	switch (cmd) {
    351 	case LINUX_F_GETLK64:
    352 		sg = stackgap_init(p, 0);
    353 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    354 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    355 			return error;
    356 		linux_to_bsd_flock64(&bfl, &lfl);
    357 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    358 			return error;
    359 		SCARG(&fca, fd) = fd;
    360 		SCARG(&fca, cmd) = F_GETLK;
    361 		SCARG(&fca, arg) = bfp;
    362 		if ((error = sys_fcntl(l, &fca, retval)) != 0)
    363 			return error;
    364 		if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
    365 			return error;
    366 		bsd_to_linux_flock64(&lfl, &bfl);
    367 		return copyout(&lfl, arg, sizeof lfl);
    368 	case LINUX_F_SETLK64:
    369 	case LINUX_F_SETLKW64:
    370 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    371 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    372 			return error;
    373 		linux_to_bsd_flock64(&bfl, &lfl);
    374 		sg = stackgap_init(p, 0);
    375 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    376 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    377 			return error;
    378 		SCARG(&fca, fd) = fd;
    379 		SCARG(&fca, cmd) = cmd;
    380 		SCARG(&fca, arg) = bfp;
    381 		return sys_fcntl(l, &fca, retval);
    382 	default:
    383 		return linux_sys_fcntl(l, v, retval);
    384 	}
    385 }
    386 # endif /* !m69k && !amd64  && !COMPAT_LINUX32 */
    387 
    388 #endif /* !alpha */
    389 
    390 /*
    391  * Linux 'readdir' call. This code is mostly taken from the
    392  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    393  * an attempt has been made to keep it a little cleaner.
    394  *
    395  * The d_off field contains the offset of the next valid entry,
    396  * unless the older Linux getdents(2), which used to have it set
    397  * to the offset of the entry itself. This function also doesn't
    398  * need to deal with the old count == 1 glibc problem.
    399  *
    400  * Read in BSD-style entries, convert them, and copy them out.
    401  *
    402  * Note that this doesn't handle union-mounted filesystems.
    403  */
    404 #ifndef COMPAT_LINUX32
    405 int
    406 linux_sys_getdents64(l, v, retval)
    407 	struct lwp *l;
    408 	void *v;
    409 	register_t *retval;
    410 {
    411 	struct linux_sys_getdents_args /* {
    412 		syscallarg(int) fd;
    413 		syscallarg(struct linux_dirent64 *) dent;
    414 		syscallarg(unsigned int) count;
    415 	} */ *uap = v;
    416 	struct proc *p = l->l_proc;
    417 	struct dirent *bdp;
    418 	struct vnode *vp;
    419 	caddr_t	inp, tbuf;		/* BSD-format */
    420 	int len, reclen;		/* BSD-format */
    421 	caddr_t outp;			/* Linux-format */
    422 	int resid, linux_reclen = 0;	/* Linux-format */
    423 	struct file *fp;
    424 	struct uio auio;
    425 	struct iovec aiov;
    426 	struct linux_dirent64 idb;
    427 	off_t off;		/* true file offset */
    428 	int buflen, error, eofflag, nbytes;
    429 	struct vattr va;
    430 	off_t *cookiebuf = NULL, *cookie;
    431 	int ncookies;
    432 
    433 	/* getvnode() will use the descriptor for us */
    434 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    435 		return (error);
    436 
    437 	if ((fp->f_flag & FREAD) == 0) {
    438 		error = EBADF;
    439 		goto out1;
    440 	}
    441 
    442 	vp = (struct vnode *)fp->f_data;
    443 	if (vp->v_type != VDIR) {
    444 		error = EINVAL;
    445 		goto out1;
    446 	}
    447 
    448 	if ((error = VOP_GETATTR(vp, &va, p->p_cred, l)))
    449 		goto out1;
    450 
    451 	nbytes = SCARG(uap, count);
    452 	buflen = min(MAXBSIZE, nbytes);
    453 	if (buflen < va.va_blocksize)
    454 		buflen = va.va_blocksize;
    455 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    456 
    457 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    458 	off = fp->f_offset;
    459 	outp = (caddr_t)SCARG(uap, dent);
    460 again:
    461 	aiov.iov_base = tbuf;
    462 	aiov.iov_len = buflen;
    463 	auio.uio_iov = &aiov;
    464 	auio.uio_iovcnt = 1;
    465 	auio.uio_rw = UIO_READ;
    466 	auio.uio_resid = buflen;
    467 	auio.uio_offset = off;
    468 	UIO_SETUP_SYSSPACE(&auio);
    469 	/*
    470          * First we read into the malloc'ed buffer, then
    471          * we massage it into user space, one record at a time.
    472          */
    473 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    474 	    &ncookies);
    475 	if (error)
    476 		goto out;
    477 
    478 	inp = tbuf;
    479 	resid = nbytes;
    480 	if ((len = buflen - auio.uio_resid) == 0)
    481 		goto eof;
    482 
    483 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    484 		bdp = (struct dirent *)inp;
    485 		reclen = bdp->d_reclen;
    486 		if (reclen & 3)
    487 			panic("linux_readdir");
    488 		if (bdp->d_fileno == 0) {
    489 			inp += reclen;	/* it is a hole; squish it out */
    490 			if (cookie)
    491 				off = *cookie++;
    492 			else
    493 				off += reclen;
    494 			continue;
    495 		}
    496 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    497 		if (reclen > len || resid < linux_reclen) {
    498 			/* entry too big for buffer, so just stop */
    499 			outp++;
    500 			break;
    501 		}
    502 		if (cookie)
    503 			off = *cookie++;	/* each entry points to next */
    504 		else
    505 			off += reclen;
    506 		/*
    507 		 * Massage in place to make a Linux-shaped dirent (otherwise
    508 		 * we have to worry about touching user memory outside of
    509 		 * the copyout() call).
    510 		 */
    511 		idb.d_ino = bdp->d_fileno;
    512 		idb.d_type = bdp->d_type;
    513 		idb.d_off = off;
    514 		idb.d_reclen = (u_short)linux_reclen;
    515 		strcpy(idb.d_name, bdp->d_name);
    516 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    517 			goto out;
    518 		/* advance past this real entry */
    519 		inp += reclen;
    520 		/* advance output past Linux-shaped entry */
    521 		outp += linux_reclen;
    522 		resid -= linux_reclen;
    523 	}
    524 
    525 	/* if we squished out the whole block, try again */
    526 	if (outp == (caddr_t)SCARG(uap, dent))
    527 		goto again;
    528 	fp->f_offset = off;	/* update the vnode offset */
    529 
    530 eof:
    531 	*retval = nbytes - resid;
    532 out:
    533 	VOP_UNLOCK(vp, 0);
    534 	if (cookiebuf)
    535 		free(cookiebuf, M_TEMP);
    536 	free(tbuf, M_TEMP);
    537 #if defined(KTRACE)
    538 	if (!error && outp > (const char *)SCARG(uap, dent) &&
    539 	    KTRPOINT(p, KTR_GENIO)) {
    540 		struct iovec iov;
    541 		size_t done = outp - (const char *)SCARG(uap, dent);
    542 
    543 		iov.iov_base = SCARG(uap, dent);
    544 		iov.iov_len = done;
    545 		ktrgenio(l, SCARG(uap, fd), UIO_READ, &iov, done, 0);
    546 	}
    547 #endif /* defined(KTRACE) */
    548 out1:
    549 	FILE_UNUSE(fp, l);
    550 	return error;
    551 }
    552 #endif /* !COMPAT_LINUX32 */
    553