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