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