Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.40.2.3
      1  1.40.2.3       mjf /*	$NetBSD: linux_file64.c,v 1.40.2.3 2008/02/18 21:05:27 mjf Exp $	*/
      2       1.1  jdolecek 
      3       1.1  jdolecek /*-
      4       1.1  jdolecek  * Copyright (c) 1995, 1998, 2000 The NetBSD Foundation, Inc.
      5       1.1  jdolecek  * All rights reserved.
      6       1.1  jdolecek  *
      7       1.1  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  jdolecek  * by Frank van der Linden and Eric Haszlakiewicz.
      9       1.1  jdolecek  *
     10       1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     11       1.1  jdolecek  * modification, are permitted provided that the following conditions
     12       1.1  jdolecek  * are met:
     13       1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15       1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     18       1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     19       1.1  jdolecek  *    must display the following acknowledgement:
     20       1.1  jdolecek  *	This product includes software developed by the NetBSD
     21       1.1  jdolecek  *	Foundation, Inc. and its contributors.
     22       1.1  jdolecek  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  jdolecek  *    contributors may be used to endorse or promote products derived
     24       1.1  jdolecek  *    from this software without specific prior written permission.
     25       1.1  jdolecek  *
     26       1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  jdolecek  */
     38       1.1  jdolecek 
     39       1.1  jdolecek /*
     40       1.1  jdolecek  * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
     41       1.1  jdolecek  */
     42       1.6     lukem 
     43       1.6     lukem #include <sys/cdefs.h>
     44  1.40.2.3       mjf __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.40.2.3 2008/02/18 21:05:27 mjf Exp $");
     45       1.1  jdolecek 
     46       1.1  jdolecek #include <sys/param.h>
     47       1.1  jdolecek #include <sys/systm.h>
     48       1.1  jdolecek #include <sys/namei.h>
     49       1.1  jdolecek #include <sys/proc.h>
     50      1.11      tron #include <sys/dirent.h>
     51       1.1  jdolecek #include <sys/file.h>
     52       1.1  jdolecek #include <sys/stat.h>
     53       1.1  jdolecek #include <sys/filedesc.h>
     54       1.1  jdolecek #include <sys/ioctl.h>
     55       1.1  jdolecek #include <sys/kernel.h>
     56       1.1  jdolecek #include <sys/mount.h>
     57       1.1  jdolecek #include <sys/malloc.h>
     58      1.37       dsl #include <sys/namei.h>
     59      1.37       dsl #include <sys/vfs_syscalls.h>
     60       1.1  jdolecek #include <sys/vnode.h>
     61       1.1  jdolecek #include <sys/tty.h>
     62       1.1  jdolecek #include <sys/conf.h>
     63       1.1  jdolecek 
     64       1.1  jdolecek #include <sys/syscallargs.h>
     65       1.1  jdolecek 
     66       1.1  jdolecek #include <compat/linux/common/linux_types.h>
     67       1.1  jdolecek #include <compat/linux/common/linux_signal.h>
     68       1.1  jdolecek #include <compat/linux/common/linux_fcntl.h>
     69       1.1  jdolecek #include <compat/linux/common/linux_util.h>
     70       1.1  jdolecek #include <compat/linux/common/linux_machdep.h>
     71      1.11      tron #include <compat/linux/common/linux_dirent.h>
     72      1.40     njoly #include <compat/linux/common/linux_ipc.h>
     73      1.40     njoly #include <compat/linux/common/linux_sem.h>
     74       1.1  jdolecek 
     75       1.1  jdolecek #include <compat/linux/linux_syscallargs.h>
     76       1.1  jdolecek 
     77      1.15      matt #ifndef alpha
     78      1.15      matt 
     79  1.40.2.1       mjf static void bsd_to_linux_stat(struct stat *, struct linux_stat64 *);
     80       1.1  jdolecek 
     81       1.1  jdolecek /*
     82       1.1  jdolecek  * Convert a NetBSD stat structure to a Linux stat structure.
     83       1.1  jdolecek  * Only the order of the fields and the padding in the structure
     84       1.1  jdolecek  * is different. linux_fakedev is a machine-dependent function
     85       1.1  jdolecek  * which optionally converts device driver major/minor numbers
     86       1.1  jdolecek  * (XXX horrible, but what can you do against code that compares
     87       1.1  jdolecek  * things against constant major device numbers? sigh)
     88       1.1  jdolecek  */
     89       1.1  jdolecek static void
     90  1.40.2.2       mjf bsd_to_linux_stat(struct stat *bsp, struct linux_stat64 *lsp)
     91       1.1  jdolecek {
     92       1.7  christos 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
     93       1.1  jdolecek 	lsp->lst_ino     = bsp->st_ino;
     94       1.1  jdolecek 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     95       1.1  jdolecek 	if (bsp->st_nlink >= (1 << 15))
     96       1.1  jdolecek 		lsp->lst_nlink = (1 << 15) - 1;
     97       1.1  jdolecek 	else
     98       1.1  jdolecek 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     99       1.1  jdolecek 	lsp->lst_uid     = bsp->st_uid;
    100       1.1  jdolecek 	lsp->lst_gid     = bsp->st_gid;
    101       1.7  christos 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    102       1.1  jdolecek 	lsp->lst_size    = bsp->st_size;
    103       1.1  jdolecek 	lsp->lst_blksize = bsp->st_blksize;
    104       1.1  jdolecek 	lsp->lst_blocks  = bsp->st_blocks;
    105       1.1  jdolecek 	lsp->lst_atime   = bsp->st_atime;
    106       1.1  jdolecek 	lsp->lst_mtime   = bsp->st_mtime;
    107       1.1  jdolecek 	lsp->lst_ctime   = bsp->st_ctime;
    108      1.31      manu #  ifdef LINUX_STAT64_HAS_NSEC
    109      1.25  christos 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    110      1.25  christos 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    111      1.25  christos 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    112      1.31      manu #  endif
    113      1.31      manu #  if LINUX_STAT64_HAS_BROKEN_ST_INO
    114      1.16  jdolecek 	lsp->__lst_ino   = (linux_ino_t) bsp->st_ino;
    115      1.31      manu #  endif
    116       1.1  jdolecek }
    117       1.1  jdolecek 
    118       1.1  jdolecek /*
    119       1.1  jdolecek  * The stat functions below are plain sailing. stat and lstat are handled
    120       1.1  jdolecek  * by one function to avoid code duplication.
    121       1.1  jdolecek  */
    122       1.1  jdolecek int
    123  1.40.2.2       mjf linux_sys_fstat64(struct lwp *l, const struct linux_sys_fstat64_args *uap, register_t *retval)
    124       1.1  jdolecek {
    125  1.40.2.2       mjf 	/* {
    126       1.1  jdolecek 		syscallarg(int) fd;
    127       1.5      manu 		syscallarg(struct linux_stat64 *) sp;
    128  1.40.2.2       mjf 	} */
    129       1.1  jdolecek 	struct linux_stat64 tmplst;
    130      1.37       dsl 	struct stat tmpst;
    131       1.1  jdolecek 	int error;
    132       1.1  jdolecek 
    133      1.37       dsl 	error = do_sys_fstat(l,  SCARG(uap, fd), &tmpst);
    134      1.37       dsl 	if (error != 0)
    135       1.1  jdolecek 		return error;
    136       1.1  jdolecek 
    137       1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    138       1.1  jdolecek 
    139      1.37       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    140       1.1  jdolecek }
    141       1.1  jdolecek 
    142       1.1  jdolecek static int
    143  1.40.2.2       mjf linux_do_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval, int flags)
    144       1.1  jdolecek {
    145       1.1  jdolecek 	struct linux_stat64 tmplst;
    146      1.37       dsl 	struct stat tmpst;
    147       1.1  jdolecek 	int error;
    148       1.1  jdolecek 
    149      1.37       dsl 	error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
    150      1.37       dsl 	if (error != 0)
    151       1.1  jdolecek 		return error;
    152       1.1  jdolecek 
    153       1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    154       1.1  jdolecek 
    155      1.37       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    156       1.1  jdolecek }
    157       1.1  jdolecek 
    158       1.1  jdolecek int
    159  1.40.2.2       mjf linux_sys_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval)
    160       1.1  jdolecek {
    161  1.40.2.2       mjf 	/* {
    162       1.1  jdolecek 		syscallarg(const char *) path;
    163       1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    164  1.40.2.2       mjf 	} */
    165       1.1  jdolecek 
    166      1.37       dsl 	return linux_do_stat64(l, uap, retval, FOLLOW);
    167       1.1  jdolecek }
    168       1.1  jdolecek 
    169       1.1  jdolecek int
    170  1.40.2.2       mjf linux_sys_lstat64(struct lwp *l, const struct linux_sys_lstat64_args *uap, register_t *retval)
    171       1.1  jdolecek {
    172  1.40.2.2       mjf 	/* {
    173       1.1  jdolecek 		syscallarg(const char *) path;
    174       1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    175  1.40.2.2       mjf 	} */
    176       1.1  jdolecek 
    177  1.40.2.2       mjf 	return linux_do_stat64(l, (const void *)uap, retval, NOFOLLOW);
    178       1.2  jdolecek }
    179       1.2  jdolecek 
    180       1.2  jdolecek int
    181  1.40.2.2       mjf linux_sys_truncate64(struct lwp *l, const struct linux_sys_truncate64_args *uap, register_t *retval)
    182       1.2  jdolecek {
    183  1.40.2.2       mjf 	/* {
    184       1.2  jdolecek 		syscallarg(const char *) path;
    185       1.2  jdolecek 		syscallarg(off_t) length;
    186  1.40.2.2       mjf 	} */
    187      1.22  jdolecek 	struct sys_truncate_args ta;
    188       1.2  jdolecek 
    189      1.22  jdolecek 	/* Linux doesn't have the 'pad' pseudo-parameter */
    190      1.22  jdolecek 	SCARG(&ta, path) = SCARG(uap, path);
    191      1.22  jdolecek 	SCARG(&ta, pad) = 0;
    192      1.22  jdolecek 	SCARG(&ta, length) = SCARG(uap, length);
    193      1.22  jdolecek 
    194      1.22  jdolecek 	return sys_truncate(l, &ta, retval);
    195      1.22  jdolecek }
    196      1.22  jdolecek 
    197      1.22  jdolecek int
    198  1.40.2.2       mjf linux_sys_ftruncate64(struct lwp *l, const struct linux_sys_ftruncate64_args *uap, register_t *retval)
    199      1.22  jdolecek {
    200  1.40.2.2       mjf 	/* {
    201      1.22  jdolecek 		syscallarg(unsigned int) fd;
    202      1.22  jdolecek 		syscallarg(off_t) length;
    203  1.40.2.2       mjf 	} */
    204      1.22  jdolecek 	struct sys_ftruncate_args ta;
    205      1.22  jdolecek 
    206      1.22  jdolecek 	/* Linux doesn't have the 'pad' pseudo-parameter */
    207      1.22  jdolecek 	SCARG(&ta, fd) = SCARG(uap, fd);
    208      1.22  jdolecek 	SCARG(&ta, pad) = 0;
    209      1.22  jdolecek 	SCARG(&ta, length) = SCARG(uap, length);
    210      1.22  jdolecek 
    211      1.22  jdolecek 	return sys_ftruncate(l, &ta, retval);
    212       1.1  jdolecek }
    213      1.15      matt #endif /* !alpha */
    214      1.11      tron 
    215      1.11      tron /*
    216      1.11      tron  * Linux 'readdir' call. This code is mostly taken from the
    217      1.11      tron  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    218      1.18  jdolecek  * an attempt has been made to keep it a little cleaner.
    219      1.11      tron  *
    220      1.18  jdolecek  * The d_off field contains the offset of the next valid entry,
    221      1.18  jdolecek  * unless the older Linux getdents(2), which used to have it set
    222      1.18  jdolecek  * to the offset of the entry itself. This function also doesn't
    223      1.18  jdolecek  * need to deal with the old count == 1 glibc problem.
    224      1.11      tron  *
    225      1.11      tron  * Read in BSD-style entries, convert them, and copy them out.
    226      1.11      tron  *
    227      1.11      tron  * Note that this doesn't handle union-mounted filesystems.
    228      1.11      tron  */
    229      1.11      tron int
    230  1.40.2.2       mjf linux_sys_getdents64(struct lwp *l, const struct linux_sys_getdents64_args *uap, register_t *retval)
    231      1.11      tron {
    232  1.40.2.2       mjf 	/* {
    233      1.11      tron 		syscallarg(int) fd;
    234      1.11      tron 		syscallarg(struct linux_dirent64 *) dent;
    235      1.11      tron 		syscallarg(unsigned int) count;
    236  1.40.2.2       mjf 	} */
    237      1.11      tron 	struct dirent *bdp;
    238      1.11      tron 	struct vnode *vp;
    239      1.36  christos 	char *inp, *tbuf;		/* BSD-format */
    240      1.11      tron 	int len, reclen;		/* BSD-format */
    241      1.36  christos 	char *outp;			/* Linux-format */
    242      1.11      tron 	int resid, linux_reclen = 0;	/* Linux-format */
    243      1.11      tron 	struct file *fp;
    244      1.11      tron 	struct uio auio;
    245      1.11      tron 	struct iovec aiov;
    246      1.11      tron 	struct linux_dirent64 idb;
    247      1.11      tron 	off_t off;		/* true file offset */
    248      1.18  jdolecek 	int buflen, error, eofflag, nbytes;
    249      1.11      tron 	struct vattr va;
    250      1.11      tron 	off_t *cookiebuf = NULL, *cookie;
    251      1.11      tron 	int ncookies;
    252      1.11      tron 
    253      1.11      tron 	/* getvnode() will use the descriptor for us */
    254      1.34        ad 	if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
    255      1.11      tron 		return (error);
    256      1.11      tron 
    257      1.11      tron 	if ((fp->f_flag & FREAD) == 0) {
    258      1.11      tron 		error = EBADF;
    259      1.11      tron 		goto out1;
    260      1.11      tron 	}
    261      1.11      tron 
    262      1.11      tron 	vp = (struct vnode *)fp->f_data;
    263      1.11      tron 	if (vp->v_type != VDIR) {
    264      1.11      tron 		error = EINVAL;
    265      1.11      tron 		goto out1;
    266      1.11      tron 	}
    267      1.11      tron 
    268  1.40.2.1       mjf 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
    269      1.11      tron 		goto out1;
    270      1.11      tron 
    271      1.11      tron 	nbytes = SCARG(uap, count);
    272      1.18  jdolecek 	buflen = min(MAXBSIZE, nbytes);
    273      1.18  jdolecek 	if (buflen < va.va_blocksize)
    274      1.18  jdolecek 		buflen = va.va_blocksize;
    275      1.28  christos 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    276      1.11      tron 
    277      1.11      tron 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    278      1.11      tron 	off = fp->f_offset;
    279      1.11      tron again:
    280      1.28  christos 	aiov.iov_base = tbuf;
    281      1.11      tron 	aiov.iov_len = buflen;
    282      1.11      tron 	auio.uio_iov = &aiov;
    283      1.11      tron 	auio.uio_iovcnt = 1;
    284      1.11      tron 	auio.uio_rw = UIO_READ;
    285      1.11      tron 	auio.uio_resid = buflen;
    286      1.11      tron 	auio.uio_offset = off;
    287      1.32      yamt 	UIO_SETUP_SYSSPACE(&auio);
    288      1.11      tron 	/*
    289      1.11      tron          * First we read into the malloc'ed buffer, then
    290      1.11      tron          * we massage it into user space, one record at a time.
    291      1.11      tron          */
    292      1.11      tron 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    293      1.11      tron 	    &ncookies);
    294      1.11      tron 	if (error)
    295      1.11      tron 		goto out;
    296      1.11      tron 
    297      1.28  christos 	inp = tbuf;
    298      1.36  christos 	outp = (void *)SCARG(uap, dent);
    299      1.11      tron 	resid = nbytes;
    300      1.11      tron 	if ((len = buflen - auio.uio_resid) == 0)
    301      1.11      tron 		goto eof;
    302      1.11      tron 
    303      1.11      tron 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    304      1.11      tron 		bdp = (struct dirent *)inp;
    305      1.11      tron 		reclen = bdp->d_reclen;
    306      1.11      tron 		if (reclen & 3)
    307      1.11      tron 			panic("linux_readdir");
    308      1.11      tron 		if (bdp->d_fileno == 0) {
    309      1.11      tron 			inp += reclen;	/* it is a hole; squish it out */
    310      1.26  christos 			if (cookie)
    311      1.26  christos 				off = *cookie++;
    312      1.26  christos 			else
    313      1.26  christos 				off += reclen;
    314      1.11      tron 			continue;
    315      1.11      tron 		}
    316      1.11      tron 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    317      1.11      tron 		if (reclen > len || resid < linux_reclen) {
    318      1.11      tron 			/* entry too big for buffer, so just stop */
    319      1.11      tron 			outp++;
    320      1.11      tron 			break;
    321      1.11      tron 		}
    322      1.26  christos 		if (cookie)
    323      1.26  christos 			off = *cookie++;	/* each entry points to next */
    324      1.26  christos 		else
    325      1.26  christos 			off += reclen;
    326      1.11      tron 		/*
    327      1.11      tron 		 * Massage in place to make a Linux-shaped dirent (otherwise
    328      1.11      tron 		 * we have to worry about touching user memory outside of
    329      1.11      tron 		 * the copyout() call).
    330      1.11      tron 		 */
    331      1.11      tron 		idb.d_ino = bdp->d_fileno;
    332      1.11      tron 		idb.d_type = bdp->d_type;
    333      1.18  jdolecek 		idb.d_off = off;
    334      1.18  jdolecek 		idb.d_reclen = (u_short)linux_reclen;
    335      1.11      tron 		strcpy(idb.d_name, bdp->d_name);
    336      1.36  christos 		if ((error = copyout((void *)&idb, outp, linux_reclen)))
    337      1.11      tron 			goto out;
    338      1.11      tron 		/* advance past this real entry */
    339      1.11      tron 		inp += reclen;
    340      1.11      tron 		/* advance output past Linux-shaped entry */
    341      1.11      tron 		outp += linux_reclen;
    342      1.11      tron 		resid -= linux_reclen;
    343      1.11      tron 	}
    344      1.11      tron 
    345      1.11      tron 	/* if we squished out the whole block, try again */
    346      1.36  christos 	if (outp == (void *)SCARG(uap, dent))
    347      1.11      tron 		goto again;
    348      1.11      tron 	fp->f_offset = off;	/* update the vnode offset */
    349      1.11      tron 
    350      1.11      tron eof:
    351      1.11      tron 	*retval = nbytes - resid;
    352      1.11      tron out:
    353      1.11      tron 	VOP_UNLOCK(vp, 0);
    354      1.11      tron 	if (cookiebuf)
    355      1.11      tron 		free(cookiebuf, M_TEMP);
    356      1.28  christos 	free(tbuf, M_TEMP);
    357      1.11      tron out1:
    358      1.30  christos 	FILE_UNUSE(fp, l);
    359       1.3      manu 	return error;
    360       1.3      manu }
    361