Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.2.4.7
      1 /*	$NetBSD: linux_file64.c,v 1.2.4.7 2002/04/01 07:44:25 nathanw 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.2.4.7 2002/04/01 07:44:25 nathanw 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/file.h>
     51 #include <sys/stat.h>
     52 #include <sys/filedesc.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/kernel.h>
     55 #include <sys/mount.h>
     56 #include <sys/malloc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/tty.h>
     59 #include <sys/conf.h>
     60 
     61 #include <sys/syscallargs.h>
     62 
     63 #include <compat/linux/common/linux_types.h>
     64 #include <compat/linux/common/linux_signal.h>
     65 #include <compat/linux/common/linux_fcntl.h>
     66 #include <compat/linux/common/linux_util.h>
     67 #include <compat/linux/common/linux_machdep.h>
     68 
     69 #include <compat/linux/linux_syscallargs.h>
     70 
     71 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     72 static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
     73 
     74 /*
     75  * Convert a NetBSD stat structure to a Linux stat structure.
     76  * Only the order of the fields and the padding in the structure
     77  * is different. linux_fakedev is a machine-dependent function
     78  * which optionally converts device driver major/minor numbers
     79  * (XXX horrible, but what can you do against code that compares
     80  * things against constant major device numbers? sigh)
     81  */
     82 static void
     83 bsd_to_linux_stat(bsp, lsp)
     84 	struct stat *bsp;
     85 	struct linux_stat64 *lsp;
     86 {
     87 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
     88 	lsp->lst_ino     = bsp->st_ino;
     89 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     90 	if (bsp->st_nlink >= (1 << 15))
     91 		lsp->lst_nlink = (1 << 15) - 1;
     92 	else
     93 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     94 	lsp->lst_uid     = bsp->st_uid;
     95 	lsp->lst_gid     = bsp->st_gid;
     96 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
     97 	lsp->lst_size    = bsp->st_size;
     98 	lsp->lst_blksize = bsp->st_blksize;
     99 	lsp->lst_blocks  = bsp->st_blocks;
    100 	lsp->lst_atime   = bsp->st_atime;
    101 	lsp->lst_mtime   = bsp->st_mtime;
    102 	lsp->lst_ctime   = bsp->st_ctime;
    103 	lsp->lst_ino64   = bsp->st_ino;
    104 }
    105 
    106 /*
    107  * The stat functions below are plain sailing. stat and lstat are handled
    108  * by one function to avoid code duplication.
    109  */
    110 int
    111 linux_sys_fstat64(l, v, retval)
    112 	struct lwp *l;
    113 	void *v;
    114 	register_t *retval;
    115 {
    116 	struct linux_sys_fstat64_args /* {
    117 		syscallarg(int) fd;
    118 		syscallarg(struct linux_stat64 *) sp;
    119 	} */ *uap = v;
    120 	struct proc *p = l->l_proc;
    121 	struct sys___fstat13_args fsa;
    122 	struct linux_stat64 tmplst;
    123 	struct stat *st,tmpst;
    124 	caddr_t sg;
    125 	int error;
    126 
    127 	sg = stackgap_init(p, 0);
    128 
    129 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    130 
    131 	SCARG(&fsa, fd) = SCARG(uap, fd);
    132 	SCARG(&fsa, sb) = st;
    133 
    134 	if ((error = sys___fstat13(l, &fsa, retval)))
    135 		return error;
    136 
    137 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    138 		return error;
    139 
    140 	bsd_to_linux_stat(&tmpst, &tmplst);
    141 
    142 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    143 		return error;
    144 
    145 	return 0;
    146 }
    147 
    148 static int
    149 linux_do_stat64(l, v, retval, dolstat)
    150 	struct lwp *l;
    151 	void *v;
    152 	register_t *retval;
    153 	int dolstat;
    154 {
    155 	struct proc *p = l->l_proc;
    156 	struct sys___stat13_args sa;
    157 	struct linux_stat64 tmplst;
    158 	struct stat *st, tmpst;
    159 	caddr_t sg;
    160 	int error;
    161 	struct linux_sys_stat64_args *uap = v;
    162 
    163 	sg = stackgap_init(p, 0);
    164 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    165 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    166 
    167 	SCARG(&sa, ub) = st;
    168 	SCARG(&sa, path) = SCARG(uap, path);
    169 
    170 	if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
    171 				sys___stat13(l, &sa, retval))))
    172 		return error;
    173 
    174 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    175 		return error;
    176 
    177 	bsd_to_linux_stat(&tmpst, &tmplst);
    178 
    179 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    180 		return error;
    181 
    182 	return 0;
    183 }
    184 
    185 int
    186 linux_sys_stat64(l, v, retval)
    187 	struct lwp *l;
    188 	void *v;
    189 	register_t *retval;
    190 {
    191 	struct linux_sys_stat64_args /* {
    192 		syscallarg(const char *) path;
    193 		syscallarg(struct linux_stat64 *) sp;
    194 	} */ *uap = v;
    195 
    196 	return linux_do_stat64(l, uap, retval, 0);
    197 }
    198 
    199 int
    200 linux_sys_lstat64(l, v, retval)
    201 	struct lwp *l;
    202 	void *v;
    203 	register_t *retval;
    204 {
    205 	struct linux_sys_lstat64_args /* {
    206 		syscallarg(const char *) path;
    207 		syscallarg(struct linux_stat64 *) sp;
    208 	} */ *uap = v;
    209 
    210 	return linux_do_stat64(l, uap, retval, 1);
    211 }
    212 
    213 int
    214 linux_sys_truncate64(l, v, retval)
    215 	struct lwp *l;
    216 	void *v;
    217 	register_t *retval;
    218 {
    219 	struct linux_sys_truncate64_args /* {
    220 		syscallarg(const char *) path;
    221 		syscallarg(off_t) length;
    222 	} */ *uap = v;
    223 	struct proc *p = l->l_proc;
    224 	caddr_t sg = stackgap_init(p, 0);
    225 
    226 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    227 
    228 	return sys_truncate(l, uap, retval);
    229 }
    230 
    231 #if defined(__mips__) || defined(__i386__) /* powerpc could use it too */
    232 static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
    233     const struct flock *));
    234 static void linux_to_bsd_flock64 __P((struct flock *,
    235     const struct linux_flock64 *));
    236 
    237 static void
    238 bsd_to_linux_flock64(lfp, bfp)
    239 	struct linux_flock64 *lfp;
    240 	const struct flock *bfp;
    241 {
    242 
    243 	lfp->l_start = bfp->l_start;
    244 	lfp->l_len = bfp->l_len;
    245 	lfp->l_pid = bfp->l_pid;
    246 	lfp->l_whence = bfp->l_whence;
    247 	switch (bfp->l_type) {
    248 	case F_RDLCK:
    249 		lfp->l_type = LINUX_F_RDLCK;
    250 		break;
    251 	case F_UNLCK:
    252 		lfp->l_type = LINUX_F_UNLCK;
    253 		break;
    254 	case F_WRLCK:
    255 		lfp->l_type = LINUX_F_WRLCK;
    256 		break;
    257 	}
    258 }
    259 
    260 static void
    261 linux_to_bsd_flock64(bfp, lfp)
    262 	struct flock *bfp;
    263 	const struct linux_flock64 *lfp;
    264 {
    265 
    266 	bfp->l_start = lfp->l_start;
    267 	bfp->l_len = lfp->l_len;
    268 	bfp->l_pid = lfp->l_pid;
    269 	bfp->l_whence = lfp->l_whence;
    270 	switch (lfp->l_type) {
    271 	case LINUX_F_RDLCK:
    272 		bfp->l_type = F_RDLCK;
    273 		break;
    274 	case LINUX_F_UNLCK:
    275 		bfp->l_type = F_UNLCK;
    276 		break;
    277 	case LINUX_F_WRLCK:
    278 		bfp->l_type = F_WRLCK;
    279 		break;
    280 	}
    281 }
    282 int
    283 linux_sys_fcntl64(l, v, retval)
    284 	struct lwp *l;
    285 	void *v;
    286 	register_t *retval;
    287 {
    288 	struct linux_sys_fcntl64_args /* {
    289 		syscallarg(int) fd;
    290 		syscallarg(int) cmd;
    291 		syscallarg(void *) arg;
    292 	} */ *uap = v;
    293 	struct proc *p = l->l_proc;
    294 	struct sys_fcntl_args fca;
    295 	struct linux_flock64 lfl;
    296 	struct flock bfl, *bfp;
    297 	int error;
    298 	caddr_t sg;
    299 	void *arg = SCARG(uap, arg);
    300 	int cmd = SCARG(uap, cmd);
    301 	int fd = SCARG(uap, fd);
    302 
    303 	switch (cmd) {
    304 	case LINUX_F_GETLK64:
    305 		sg = stackgap_init(p, 0);
    306 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    307 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    308 			return error;
    309 		linux_to_bsd_flock64(&bfl, &lfl);
    310 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    311 			return error;
    312 		SCARG(&fca, fd) = fd;
    313 		SCARG(&fca, cmd) = F_GETLK;
    314 		SCARG(&fca, arg) = bfp;
    315 		if ((error = sys_fcntl(p, &fca, retval)) != 0)
    316 			return error;
    317 		if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
    318 			return error;
    319 		bsd_to_linux_flock64(&lfl, &bfl);
    320 		return copyout(&lfl, arg, sizeof lfl);
    321 	case LINUX_F_SETLK64:
    322 	case LINUX_F_SETLKW64:
    323 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    324 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    325 			return error;
    326 		linux_to_bsd_flock64(&bfl, &lfl);
    327 		sg = stackgap_init(p, 0);
    328 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    329 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    330 			return error;
    331 		SCARG(&fca, fd) = fd;
    332 		SCARG(&fca, cmd) = cmd;
    333 		SCARG(&fca, arg) = bfp;
    334 		return sys_fcntl(p, &fca, retval);
    335 	default:
    336 		return linux_sys_fcntl(l, v, retval);
    337 	}
    338 
    339 	return error;
    340 }
    341 #endif /* __mips__ || __i386__ */
    342