Home | History | Annotate | Line # | Download | only in common
linux_fcntl64.c revision 1.1
      1 /*	$NetBSD: linux_fcntl64.c,v 1.1 2008/02/02 19:37:53 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_fcntl64.c,v 1.1 2008/02/02 19:37:53 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 # if !defined(__m68k__) && (!defined(__amd64__) || defined(COMPAT_LINUX32))
     78 static void bsd_to_linux_flock64(struct linux_flock64 *,
     79     const struct flock *);
     80 static void linux_to_bsd_flock64(struct flock *,
     81     const struct linux_flock64 *);
     82 
     83 static void
     84 bsd_to_linux_flock64(struct linux_flock64 *lfp, const struct flock *bfp)
     85 {
     86 
     87 	lfp->l_start = bfp->l_start;
     88 	lfp->l_len = bfp->l_len;
     89 	lfp->l_pid = bfp->l_pid;
     90 	lfp->l_whence = bfp->l_whence;
     91 	switch (bfp->l_type) {
     92 	case F_RDLCK:
     93 		lfp->l_type = LINUX_F_RDLCK;
     94 		break;
     95 	case F_UNLCK:
     96 		lfp->l_type = LINUX_F_UNLCK;
     97 		break;
     98 	case F_WRLCK:
     99 		lfp->l_type = LINUX_F_WRLCK;
    100 		break;
    101 	}
    102 }
    103 
    104 static void
    105 linux_to_bsd_flock64(struct flock *bfp, const struct linux_flock64 *lfp)
    106 {
    107 
    108 	bfp->l_start = lfp->l_start;
    109 	bfp->l_len = lfp->l_len;
    110 	bfp->l_pid = lfp->l_pid;
    111 	bfp->l_whence = lfp->l_whence;
    112 	switch (lfp->l_type) {
    113 	case LINUX_F_RDLCK:
    114 		bfp->l_type = F_RDLCK;
    115 		break;
    116 	case LINUX_F_UNLCK:
    117 		bfp->l_type = F_UNLCK;
    118 		break;
    119 	case LINUX_F_WRLCK:
    120 		bfp->l_type = F_WRLCK;
    121 		break;
    122 	}
    123 }
    124 
    125 int
    126 linux_sys_fcntl64(struct lwp *l, const struct linux_sys_fcntl64_args *uap, register_t *retval)
    127 {
    128 	/* {
    129 		syscallarg(int) fd;
    130 		syscallarg(int) cmd;
    131 		syscallarg(void *) arg;
    132 	} */
    133 	struct linux_flock64 lfl;
    134 	struct flock bfl;
    135 	int error;
    136 	void *arg = SCARG(uap, arg);
    137 	int cmd = SCARG(uap, cmd);
    138 	int fd = SCARG(uap, fd);
    139 
    140 	switch (cmd) {
    141 	case LINUX_F_GETLK64:
    142 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    143 			return error;
    144 		linux_to_bsd_flock64(&bfl, &lfl);
    145 		error = do_fcntl_lock(l, fd, F_GETLK, &bfl);
    146 		if (error != 0)
    147 			return error;
    148 		bsd_to_linux_flock64(&lfl, &bfl);
    149 		return copyout(&lfl, arg, sizeof lfl);
    150 	case LINUX_F_SETLK64:
    151 	case LINUX_F_SETLKW64:
    152 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    153 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    154 			return error;
    155 		linux_to_bsd_flock64(&bfl, &lfl);
    156 		return do_fcntl_lock(l, fd, cmd, &bfl);
    157 	default:
    158 		return linux_sys_fcntl(l, (const void *)uap, retval);
    159 	}
    160 }
    161 # endif /* !m69k && !amd64  && !COMPAT_LINUX32 */
    162