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