1 /* $NetBSD: linux_file.c,v 1.97 2009/01/11 02:45:48 christos Exp $ */
4 * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Functions in multiarch:
34 * linux_sys_llseek : linux_llseek.c
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.97 2009/01/11 02:45:48 christos Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.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/vnode.h>
54 #include <sys/socketvar.h>
58 #include <sys/syscallargs.h>
59 #include <sys/vfs_syscalls.h>
61 #include <compat/linux/common/linux_types.h>
62 #include <compat/linux/common/linux_signal.h>
63 #include <compat/linux/common/linux_fcntl.h>
64 #include <compat/linux/common/linux_util.h>
65 #include <compat/linux/common/linux_machdep.h>
66 #include <compat/linux/common/linux_ipc.h>
67 #include <compat/linux/common/linux_sem.h>
69 #include <compat/linux/linux_syscallargs.h>
71 static int linux_to_bsd_ioflags(int);
72 static int bsd_to_linux_ioflags(int);
74 static void bsd_to_linux_stat(struct stat
*, struct linux_stat
*);
77 conv_linux_flock(linux
, flock
)
80 * Some file-related calls are handled here. The usual flag conversion
81 * an structure conversion is done, and alternate emul path searching.
85 * The next two functions convert between the Linux and NetBSD values
86 * of the flags used in open(2) and fcntl(2).
89 linux_to_bsd_ioflags(int lflags
)
93 res
|= cvtto_bsd_mask(lflags
, LINUX_O_WRONLY
, O_WRONLY
);
94 res
|= cvtto_bsd_mask(lflags
, LINUX_O_RDONLY
, O_RDONLY
);
95 res
|= cvtto_bsd_mask(lflags
, LINUX_O_RDWR
, O_RDWR
);
96 res
|= cvtto_bsd_mask(lflags
, LINUX_O_CREAT
, O_CREAT
);
97 res
|= cvtto_bsd_mask(lflags
, LINUX_O_EXCL
, O_EXCL
);
98 res
|= cvtto_bsd_mask(lflags
, LINUX_O_NOCTTY
, O_NOCTTY
);
99 res
|= cvtto_bsd_mask(lflags
, LINUX_O_TRUNC
, O_TRUNC
);
100 res
|= cvtto_bsd_mask(lflags
, LINUX_O_NDELAY
, O_NDELAY
);
101 res
|= cvtto_bsd_mask(lflags
, LINUX_O_SYNC
, O_FSYNC
);
102 res
|= cvtto_bsd_mask(lflags
, LINUX_FASYNC
, O_ASYNC
);
103 res
|= cvtto_bsd_mask(lflags
, LINUX_O_APPEND
, O_APPEND
);
109 bsd_to_linux_ioflags(int bflags
)
113 res
|= cvtto_linux_mask(bflags
, O_WRONLY
, LINUX_O_WRONLY
);
114 res
|= cvtto_linux_mask(bflags
, O_RDONLY
, LINUX_O_RDONLY
);
115 res
|= cvtto_linux_mask(bflags
, O_RDWR
, LINUX_O_RDWR
);
116 res
|= cvtto_linux_mask(bflags
, O_CREAT
, LINUX_O_CREAT
);
117 res
|= cvtto_linux_mask(bflags
, O_EXCL
, LINUX_O_EXCL
);
118 res
|= cvtto_linux_mask(bflags
, O_NOCTTY
, LINUX_O_NOCTTY
);
119 res
|= cvtto_linux_mask(bflags
, O_TRUNC
, LINUX_O_TRUNC
);
120 res
|= cvtto_linux_mask(bflags
, O_NDELAY
, LINUX_O_NDELAY
);
121 res
|= cvtto_linux_mask(bflags
, O_FSYNC
, LINUX_O_SYNC
);
122 res
|= cvtto_linux_mask(bflags
, O_ASYNC
, LINUX_FASYNC
);
123 res
|= cvtto_linux_mask(bflags
, O_APPEND
, LINUX_O_APPEND
);
129 * creat(2) is an obsolete function, but it's present as a Linux
130 * system call, so let's deal with it.
132 * Note: On the Alpha this doesn't really exist in Linux, but it's defined
133 * in syscalls.master anyway so this doesn't have to be special cased.
135 * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
138 linux_sys_creat(struct lwp
*l
, const struct linux_sys_creat_args
*uap
, register_t
*retval
)
141 syscallarg(const char *) path;
142 syscallarg(int) mode;
144 struct sys_open_args oa
;
146 SCARG(&oa
, path
) = SCARG(uap
, path
);
147 SCARG(&oa
, flags
) = O_CREAT
| O_TRUNC
| O_WRONLY
;
148 SCARG(&oa
, mode
) = SCARG(uap
, mode
);
150 return sys_open(l
, &oa
, retval
);
154 * open(2). Take care of the different flag values, and let the
155 * NetBSD syscall do the real work. See if this operation
156 * gives the current process a controlling terminal.
157 * (XXX is this necessary?)
160 linux_sys_open(struct lwp
*l
, const struct linux_sys_open_args
*uap
, register_t
*retval
)
163 syscallarg(const char *) path;
164 syscallarg(int) flags;
165 syscallarg(int) mode;
167 struct proc
*p
= l
->l_proc
;
169 struct sys_open_args boa
;
171 fl
= linux_to_bsd_ioflags(SCARG(uap
, flags
));
173 SCARG(&boa
, path
) = SCARG(uap
, path
);
174 SCARG(&boa
, flags
) = fl
;
175 SCARG(&boa
, mode
) = SCARG(uap
, mode
);
177 if ((error
= sys_open(l
, &boa
, retval
)))
181 * this bit from sunos_misc.c (and svr4_fcntl.c).
182 * If we are a session leader, and we don't have a controlling
183 * terminal yet, and the O_NOCTTY flag is not set, try to make
184 * this the controlling terminal.
186 if (!(fl
& O_NOCTTY
) && SESS_LEADER(p
) && !(p
->p_lflag
& PL_CONTROLT
)) {
189 fp
= fd_getfile(*retval
);
191 /* ignore any error, just give it a try */
193 if (fp
->f_type
== DTYPE_VNODE
) {
194 (fp
->f_ops
->fo_ioctl
) (fp
, TIOCSCTTY
, NULL
);
203 * Most actions in the fcntl() call are straightforward; simply
204 * pass control to the NetBSD system call. A few commands need
205 * conversions after the actual system call has done its work,
206 * because the flag values and lock structure are different.
209 linux_sys_fcntl(struct lwp
*l
, const struct linux_sys_fcntl_args
*uap
, register_t
*retval
)
214 syscallarg(void *) arg;
216 struct proc
*p
= l
->l_proc
;
220 struct sys_fcntl_args fca
;
229 cmd
= SCARG(uap
, cmd
);
230 arg
= SCARG(uap
, arg
);
247 SCARG(&fca
, fd
) = fd
;
248 SCARG(&fca
, cmd
) = F_GETFL
;
249 SCARG(&fca
, arg
) = arg
;
250 if ((error
= sys_fcntl(l
, &fca
, retval
)))
252 retval
[0] = bsd_to_linux_ioflags(retval
[0]);
255 case LINUX_F_SETFL
: {
258 val
= linux_to_bsd_ioflags((unsigned long)SCARG(uap
, arg
));
260 * Linux seems to have same semantics for sending SIGIO to the
261 * read side of socket, but slightly different semantics
262 * for SIGIO to the write side. Rather than sending the SIGIO
263 * every time it's possible to write (directly) more data, it
264 * only sends SIGIO if last write(2) failed due to insufficient
265 * memory to hold the data. This is compatible enough
266 * with NetBSD semantics to not do anything about the
269 * Linux does NOT send SIGIO for pipes. Deal with socketpair
270 * ones and DTYPE_PIPE ones. For these, we don't set
271 * the underlying flags (we don't pass O_ASYNC flag down
272 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
273 * so that F_GETFL would report the ASYNC i/o is on.
276 if (((fp1
= fd_getfile(fd
)) == NULL
))
278 if (((fp1
->f_type
== DTYPE_SOCKET
) && fp1
->f_data
279 && ((struct socket
*)fp1
->f_data
)->so_state
& SS_ISAPIPE
)
280 || (fp1
->f_type
== DTYPE_PIPE
))
283 /* not a pipe, do not modify anything */
289 SCARG(&fca
, fd
) = fd
;
290 SCARG(&fca
, cmd
) = F_SETFL
;
291 SCARG(&fca
, arg
) = (void *) val
;
293 error
= sys_fcntl(l
, &fca
, retval
);
295 /* Now set the FASYNC flag for pipes */
298 mutex_enter(&fp1
->f_lock
);
299 fp1
->f_flag
|= FASYNC
;
300 mutex_exit(&fp1
->f_lock
);
309 do_linux_getlk(fd
, cmd
, arg
, linux
, flock
);
313 do_linux_setlk(fd
, cmd
, arg
, linux
, flock
, LINUX_F_SETLK
);
318 * We need to route fcntl() for tty descriptors around normal
319 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
320 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
321 * this is not a problem.
323 if ((fp
= fd_getfile(fd
)) == NULL
)
326 /* Check it's a character device vnode */
327 if (fp
->f_type
!= DTYPE_VNODE
328 || (vp
= (struct vnode
*)fp
->f_data
) == NULL
329 || vp
->v_type
!= VCHR
) {
333 /* Not a tty, proceed with common fcntl() */
334 cmd
= cmd
== LINUX_F_SETOWN
? F_SETOWN
: F_GETOWN
;
338 error
= VOP_GETATTR(vp
, &va
, l
->l_cred
);
345 if ((tp
= cdev_tty(va
.va_rdev
)) == NULL
)
348 /* set tty pg_id appropriately */
349 mutex_enter(proc_lock
);
350 if (cmd
== LINUX_F_GETOWN
) {
351 retval
[0] = tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PGID
;
352 mutex_exit(proc_lock
);
355 if ((long)arg
<= 0) {
358 struct proc
*p1
= p_find((long)arg
, PFIND_LOCKED
| PFIND_UNLOCK_FAIL
);
361 pgid
= (long)p1
->p_pgrp
->pg_id
;
363 pgrp
= pg_find(pgid
, PFIND_LOCKED
);
364 if (pgrp
== NULL
|| pgrp
->pg_session
!= p
->p_session
) {
365 mutex_exit(proc_lock
);
369 mutex_exit(proc_lock
);
376 SCARG(&fca
, fd
) = fd
;
377 SCARG(&fca
, cmd
) = cmd
;
378 SCARG(&fca
, arg
) = arg
;
380 return sys_fcntl(l
, &fca
, retval
);
383 #if !defined(__amd64__)
385 * Convert a NetBSD stat structure to a Linux stat structure.
386 * Only the order of the fields and the padding in the structure
387 * is different. linux_fakedev is a machine-dependent function
388 * which optionally converts device driver major/minor numbers
389 * (XXX horrible, but what can you do against code that compares
390 * things against constant major device numbers? sigh)
393 bsd_to_linux_stat(struct stat
*bsp
, struct linux_stat
*lsp
)
396 lsp
->lst_dev
= linux_fakedev(bsp
->st_dev
, 0);
397 lsp
->lst_ino
= bsp
->st_ino
;
398 lsp
->lst_mode
= (linux_mode_t
)bsp
->st_mode
;
399 if (bsp
->st_nlink
>= (1 << 15))
400 lsp
->lst_nlink
= (1 << 15) - 1;
402 lsp
->lst_nlink
= (linux_nlink_t
)bsp
->st_nlink
;
403 lsp
->lst_uid
= bsp
->st_uid
;
404 lsp
->lst_gid
= bsp
->st_gid
;
405 lsp
->lst_rdev
= linux_fakedev(bsp
->st_rdev
, 1);
406 lsp
->lst_size
= bsp
->st_size
;
407 lsp
->lst_blksize
= bsp
->st_blksize
;
408 lsp
->lst_blocks
= bsp
->st_blocks
;
409 lsp
->lst_atime
= bsp
->st_atime
;
410 lsp
->lst_mtime
= bsp
->st_mtime
;
411 lsp
->lst_ctime
= bsp
->st_ctime
;
412 #ifdef LINUX_STAT_HAS_NSEC
413 lsp
->lst_atime_nsec
= bsp
->st_atimensec
;
414 lsp
->lst_mtime_nsec
= bsp
->st_mtimensec
;
415 lsp
->lst_ctime_nsec
= bsp
->st_ctimensec
;
420 * The stat functions below are plain sailing. stat and lstat are handled
421 * by one function to avoid code duplication.
424 linux_sys_fstat(struct lwp
*l
, const struct linux_sys_fstat_args
*uap
, register_t
*retval
)
428 syscallarg(linux_stat *) sp;
430 struct linux_stat tmplst
;
434 error
= do_sys_fstat(SCARG(uap
, fd
), &tmpst
);
437 bsd_to_linux_stat(&tmpst
, &tmplst
);
439 return copyout(&tmplst
, SCARG(uap
, sp
), sizeof tmplst
);
443 linux_stat1(const struct linux_sys_stat_args
*uap
, register_t
*retval
, int flags
)
445 struct linux_stat tmplst
;
449 error
= do_sys_stat(SCARG(uap
, path
), flags
, &tmpst
);
453 bsd_to_linux_stat(&tmpst
, &tmplst
);
455 return copyout(&tmplst
, SCARG(uap
, sp
), sizeof tmplst
);
459 linux_sys_stat(struct lwp
*l
, const struct linux_sys_stat_args
*uap
, register_t
*retval
)
462 syscallarg(const char *) path;
463 syscallarg(struct linux_stat *) sp;
466 return linux_stat1(uap
, retval
, FOLLOW
);
469 /* Note: this is "newlstat" in the Linux sources */
470 /* (we don't bother with the old lstat currently) */
472 linux_sys_lstat(struct lwp
*l
, const struct linux_sys_lstat_args
*uap
, register_t
*retval
)
475 syscallarg(const char *) path;
476 syscallarg(struct linux_stat *) sp;
479 return linux_stat1((const void *)uap
, retval
, NOFOLLOW
);
481 #endif /* !__amd64__ */
484 * The following syscalls are mostly here because of the alternate path check.
487 linux_sys_unlink(struct lwp
*l
, const struct linux_sys_unlink_args
*uap
, register_t
*retval
)
490 syscallarg(const char *) path;
495 error
= sys_unlink(l
, (const void *)uap
, retval
);
500 * Linux returns EISDIR if unlink(2) is called on a directory.
501 * We return EPERM in such cases. To emulate correct behaviour,
502 * check if the path points to directory and return EISDIR if this
505 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| TRYEMULROOT
, UIO_USERSPACE
,
507 if (namei(&nd
) == 0) {
510 if (vn_stat(nd
.ni_vp
, &sb
) == 0
511 && S_ISDIR(sb
.st_mode
))
521 linux_sys_mknod(struct lwp
*l
, const struct linux_sys_mknod_args
*uap
, register_t
*retval
)
524 syscallarg(const char *) path;
525 syscallarg(int) mode;
530 * BSD handles FIFOs separately
532 if (S_ISFIFO(SCARG(uap
, mode
))) {
533 struct sys_mkfifo_args bma
;
535 SCARG(&bma
, path
) = SCARG(uap
, path
);
536 SCARG(&bma
, mode
) = SCARG(uap
, mode
);
537 return sys_mkfifo(l
, &bma
, retval
);
541 * Linux device numbers uses 8 bits for minor and 8 bits
542 * for major. Due to how we map our major and minor,
543 * this just fits into our dev_t. Just mask off the
544 * upper 16bit to remove any random junk.
546 return do_sys_mknod(l
, SCARG(uap
, path
), SCARG(uap
, mode
),
547 SCARG(uap
, dev
) & 0xffff, retval
, UIO_USERSPACE
);
552 * This is just fsync() for now (just as it is in the Linux kernel)
553 * Note: this is not implemented under Linux on Alpha and Arm
554 * but should still be defined in our syscalls.master.
555 * (syscall #148 on the arm)
558 linux_sys_fdatasync(struct lwp
*l
, const struct linux_sys_fdatasync_args
*uap
, register_t
*retval
)
564 return sys_fsync(l
, (const void *)uap
, retval
);
571 linux_sys_pread(struct lwp
*l
, const struct linux_sys_pread_args
*uap
, register_t
*retval
)
575 syscallarg(void *) buf;
576 syscallarg(size_t) nbyte;
577 syscallarg(linux_off_t) offset;
579 struct sys_pread_args pra
;
581 SCARG(&pra
, fd
) = SCARG(uap
, fd
);
582 SCARG(&pra
, buf
) = SCARG(uap
, buf
);
583 SCARG(&pra
, nbyte
) = SCARG(uap
, nbyte
);
584 SCARG(&pra
, offset
) = SCARG(uap
, offset
);
586 return sys_pread(l
, &pra
, retval
);
593 linux_sys_pwrite(struct lwp
*l
, const struct linux_sys_pwrite_args
*uap
, register_t
*retval
)
597 syscallarg(void *) buf;
598 syscallarg(size_t) nbyte;
599 syscallarg(linux_off_t) offset;
601 struct sys_pwrite_args pra
;
603 SCARG(&pra
, fd
) = SCARG(uap
, fd
);
604 SCARG(&pra
, buf
) = SCARG(uap
, buf
);
605 SCARG(&pra
, nbyte
) = SCARG(uap
, nbyte
);
606 SCARG(&pra
, offset
) = SCARG(uap
, offset
);
608 return sys_pwrite(l
, &pra
, retval
);
611 #define LINUX_NOT_SUPPORTED(fun) \
613 fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
618 LINUX_NOT_SUPPORTED(linux_sys_setxattr
)
619 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr
)
620 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr
)
622 LINUX_NOT_SUPPORTED(linux_sys_getxattr
)
623 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr
)
624 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr
)
626 LINUX_NOT_SUPPORTED(linux_sys_listxattr
)
627 LINUX_NOT_SUPPORTED(linux_sys_llistxattr
)
628 LINUX_NOT_SUPPORTED(linux_sys_flistxattr
)
630 LINUX_NOT_SUPPORTED(linux_sys_removexattr
)
631 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr
)
632 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr
)