linux_file.c revision 1.69.2.9 1 1.69.2.9 yamt /* $NetBSD: linux_file.c,v 1.69.2.9 2008/03/24 09:38:41 yamt Exp $ */
2 1.23 erh
3 1.23 erh /*-
4 1.69.2.9 yamt * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
5 1.23 erh * All rights reserved.
6 1.23 erh *
7 1.23 erh * This code is derived from software contributed to The NetBSD Foundation
8 1.25 fvdl * by Frank van der Linden and Eric Haszlakiewicz.
9 1.23 erh *
10 1.23 erh * Redistribution and use in source and binary forms, with or without
11 1.23 erh * modification, are permitted provided that the following conditions
12 1.23 erh * are met:
13 1.23 erh * 1. Redistributions of source code must retain the above copyright
14 1.23 erh * notice, this list of conditions and the following disclaimer.
15 1.23 erh * 2. Redistributions in binary form must reproduce the above copyright
16 1.23 erh * notice, this list of conditions and the following disclaimer in the
17 1.23 erh * documentation and/or other materials provided with the distribution.
18 1.23 erh * 3. All advertising materials mentioning features or use of this software
19 1.23 erh * must display the following acknowledgement:
20 1.23 erh * This product includes software developed by the NetBSD
21 1.23 erh * Foundation, Inc. and its contributors.
22 1.23 erh * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.23 erh * contributors may be used to endorse or promote products derived
24 1.23 erh * from this software without specific prior written permission.
25 1.23 erh *
26 1.23 erh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.23 erh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.23 erh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.23 erh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.23 erh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.23 erh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.23 erh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.23 erh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.23 erh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.23 erh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.23 erh * POSSIBILITY OF SUCH DAMAGE.
37 1.1 fvdl */
38 1.1 fvdl
39 1.23 erh /*
40 1.23 erh * Functions in multiarch:
41 1.23 erh * linux_sys_llseek : linux_llseek.c
42 1.23 erh */
43 1.43 lukem
44 1.43 lukem #include <sys/cdefs.h>
45 1.69.2.9 yamt __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.69.2.9 2008/03/24 09:38:41 yamt Exp $");
46 1.23 erh
47 1.1 fvdl #include <sys/param.h>
48 1.1 fvdl #include <sys/systm.h>
49 1.1 fvdl #include <sys/namei.h>
50 1.1 fvdl #include <sys/proc.h>
51 1.1 fvdl #include <sys/file.h>
52 1.1 fvdl #include <sys/stat.h>
53 1.1 fvdl #include <sys/filedesc.h>
54 1.1 fvdl #include <sys/ioctl.h>
55 1.1 fvdl #include <sys/kernel.h>
56 1.1 fvdl #include <sys/mount.h>
57 1.1 fvdl #include <sys/malloc.h>
58 1.69.2.4 yamt #include <sys/namei.h>
59 1.13 fvdl #include <sys/vnode.h>
60 1.13 fvdl #include <sys/tty.h>
61 1.39 manu #include <sys/socketvar.h>
62 1.13 fvdl #include <sys/conf.h>
63 1.41 jdolecek #include <sys/pipe.h>
64 1.1 fvdl
65 1.1 fvdl #include <sys/syscallargs.h>
66 1.69.2.4 yamt #include <sys/vfs_syscalls.h>
67 1.1 fvdl
68 1.24 christos #include <compat/linux/common/linux_types.h>
69 1.24 christos #include <compat/linux/common/linux_signal.h>
70 1.24 christos #include <compat/linux/common/linux_fcntl.h>
71 1.24 christos #include <compat/linux/common/linux_util.h>
72 1.24 christos #include <compat/linux/common/linux_machdep.h>
73 1.69.2.5 yamt #include <compat/linux/common/linux_ipc.h>
74 1.69.2.5 yamt #include <compat/linux/common/linux_sem.h>
75 1.24 christos
76 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
77 1.14 christos
78 1.69.2.6 yamt static int linux_to_bsd_ioflags(int);
79 1.69.2.6 yamt static int bsd_to_linux_ioflags(int);
80 1.67 manu #ifndef __amd64__
81 1.69.2.6 yamt static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
82 1.67 manu #endif
83 1.14 christos
84 1.69.2.8 yamt conv_linux_flock(linux, flock)
85 1.69.2.8 yamt
86 1.1 fvdl /*
87 1.1 fvdl * Some file-related calls are handled here. The usual flag conversion
88 1.1 fvdl * an structure conversion is done, and alternate emul path searching.
89 1.1 fvdl */
90 1.1 fvdl
91 1.1 fvdl /*
92 1.1 fvdl * The next two functions convert between the Linux and NetBSD values
93 1.1 fvdl * of the flags used in open(2) and fcntl(2).
94 1.1 fvdl */
95 1.1 fvdl static int
96 1.69.2.7 yamt linux_to_bsd_ioflags(int lflags)
97 1.1 fvdl {
98 1.1 fvdl int res = 0;
99 1.1 fvdl
100 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
101 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
102 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
103 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
104 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
105 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
106 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
107 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
108 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
109 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
110 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
111 1.1 fvdl
112 1.1 fvdl return res;
113 1.1 fvdl }
114 1.1 fvdl
115 1.1 fvdl static int
116 1.69.2.7 yamt bsd_to_linux_ioflags(int bflags)
117 1.1 fvdl {
118 1.1 fvdl int res = 0;
119 1.1 fvdl
120 1.1 fvdl res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
121 1.1 fvdl res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
122 1.1 fvdl res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
123 1.1 fvdl res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
124 1.1 fvdl res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
125 1.1 fvdl res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
126 1.1 fvdl res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
127 1.1 fvdl res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
128 1.1 fvdl res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
129 1.1 fvdl res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
130 1.1 fvdl res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
131 1.1 fvdl
132 1.1 fvdl return res;
133 1.1 fvdl }
134 1.1 fvdl
135 1.1 fvdl /*
136 1.1 fvdl * creat(2) is an obsolete function, but it's present as a Linux
137 1.1 fvdl * system call, so let's deal with it.
138 1.1 fvdl *
139 1.23 erh * Note: On the Alpha this doesn't really exist in Linux, but it's defined
140 1.23 erh * in syscalls.master anyway so this doesn't have to be special cased.
141 1.23 erh *
142 1.1 fvdl * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
143 1.1 fvdl */
144 1.1 fvdl int
145 1.69.2.7 yamt linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
146 1.11 thorpej {
147 1.69.2.7 yamt /* {
148 1.27 christos syscallarg(const char *) path;
149 1.1 fvdl syscallarg(int) mode;
150 1.69.2.7 yamt } */
151 1.12 mycroft struct sys_open_args oa;
152 1.1 fvdl
153 1.1 fvdl SCARG(&oa, path) = SCARG(uap, path);
154 1.1 fvdl SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
155 1.1 fvdl SCARG(&oa, mode) = SCARG(uap, mode);
156 1.12 mycroft
157 1.56 thorpej return sys_open(l, &oa, retval);
158 1.1 fvdl }
159 1.1 fvdl
160 1.1 fvdl /*
161 1.1 fvdl * open(2). Take care of the different flag values, and let the
162 1.1 fvdl * NetBSD syscall do the real work. See if this operation
163 1.1 fvdl * gives the current process a controlling terminal.
164 1.1 fvdl * (XXX is this necessary?)
165 1.1 fvdl */
166 1.1 fvdl int
167 1.69.2.7 yamt linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
168 1.11 thorpej {
169 1.69.2.7 yamt /* {
170 1.27 christos syscallarg(const char *) path;
171 1.1 fvdl syscallarg(int) flags;
172 1.1 fvdl syscallarg(int) mode;
173 1.69.2.7 yamt } */
174 1.56 thorpej struct proc *p = l->l_proc;
175 1.1 fvdl int error, fl;
176 1.12 mycroft struct sys_open_args boa;
177 1.1 fvdl
178 1.2 fvdl fl = linux_to_bsd_ioflags(SCARG(uap, flags));
179 1.1 fvdl
180 1.1 fvdl SCARG(&boa, path) = SCARG(uap, path);
181 1.1 fvdl SCARG(&boa, flags) = fl;
182 1.1 fvdl SCARG(&boa, mode) = SCARG(uap, mode);
183 1.2 fvdl
184 1.56 thorpej if ((error = sys_open(l, &boa, retval)))
185 1.1 fvdl return error;
186 1.1 fvdl
187 1.1 fvdl /*
188 1.1 fvdl * this bit from sunos_misc.c (and svr4_fcntl.c).
189 1.1 fvdl * If we are a session leader, and we don't have a controlling
190 1.1 fvdl * terminal yet, and the O_NOCTTY flag is not set, try to make
191 1.1 fvdl * this the controlling terminal.
192 1.65 perry */
193 1.69.2.3 yamt if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
194 1.69.2.9 yamt file_t *fp;
195 1.38 thorpej
196 1.69.2.9 yamt fp = fd_getfile(*retval);
197 1.1 fvdl
198 1.1 fvdl /* ignore any error, just give it a try */
199 1.57 yamt if (fp != NULL) {
200 1.57 yamt if (fp->f_type == DTYPE_VNODE) {
201 1.69.2.9 yamt (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
202 1.57 yamt }
203 1.69.2.9 yamt fd_putfile(*retval);
204 1.57 yamt }
205 1.1 fvdl }
206 1.1 fvdl return 0;
207 1.1 fvdl }
208 1.1 fvdl
209 1.1 fvdl /*
210 1.1 fvdl * Most actions in the fcntl() call are straightforward; simply
211 1.1 fvdl * pass control to the NetBSD system call. A few commands need
212 1.1 fvdl * conversions after the actual system call has done its work,
213 1.1 fvdl * because the flag values and lock structure are different.
214 1.1 fvdl */
215 1.1 fvdl int
216 1.69.2.7 yamt linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
217 1.11 thorpej {
218 1.69.2.7 yamt /* {
219 1.1 fvdl syscallarg(int) fd;
220 1.1 fvdl syscallarg(int) cmd;
221 1.1 fvdl syscallarg(void *) arg;
222 1.69.2.7 yamt } */
223 1.56 thorpej struct proc *p = l->l_proc;
224 1.23 erh int fd, cmd, error;
225 1.23 erh u_long val;
226 1.69.2.4 yamt void *arg;
227 1.12 mycroft struct sys_fcntl_args fca;
228 1.69.2.9 yamt file_t *fp;
229 1.13 fvdl struct vnode *vp;
230 1.13 fvdl struct vattr va;
231 1.53 gehenna const struct cdevsw *cdev;
232 1.13 fvdl long pgid;
233 1.13 fvdl struct pgrp *pgrp;
234 1.69.2.6 yamt struct tty *tp, *(*d_tty)(dev_t);
235 1.1 fvdl
236 1.1 fvdl fd = SCARG(uap, fd);
237 1.1 fvdl cmd = SCARG(uap, cmd);
238 1.69.2.8 yamt arg = SCARG(uap, arg);
239 1.1 fvdl
240 1.1 fvdl switch (cmd) {
241 1.69.2.8 yamt
242 1.1 fvdl case LINUX_F_DUPFD:
243 1.1 fvdl cmd = F_DUPFD;
244 1.1 fvdl break;
245 1.69.2.8 yamt
246 1.1 fvdl case LINUX_F_GETFD:
247 1.1 fvdl cmd = F_GETFD;
248 1.1 fvdl break;
249 1.69.2.8 yamt
250 1.1 fvdl case LINUX_F_SETFD:
251 1.1 fvdl cmd = F_SETFD;
252 1.1 fvdl break;
253 1.69.2.8 yamt
254 1.1 fvdl case LINUX_F_GETFL:
255 1.1 fvdl SCARG(&fca, fd) = fd;
256 1.1 fvdl SCARG(&fca, cmd) = F_GETFL;
257 1.1 fvdl SCARG(&fca, arg) = arg;
258 1.56 thorpej if ((error = sys_fcntl(l, &fca, retval)))
259 1.1 fvdl return error;
260 1.1 fvdl retval[0] = bsd_to_linux_ioflags(retval[0]);
261 1.1 fvdl return 0;
262 1.69.2.8 yamt
263 1.41 jdolecek case LINUX_F_SETFL: {
264 1.69.2.9 yamt file_t *fp1 = NULL;
265 1.41 jdolecek
266 1.23 erh val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
267 1.39 manu /*
268 1.41 jdolecek * Linux seems to have same semantics for sending SIGIO to the
269 1.64 abs * read side of socket, but slightly different semantics
270 1.41 jdolecek * for SIGIO to the write side. Rather than sending the SIGIO
271 1.41 jdolecek * every time it's possible to write (directly) more data, it
272 1.41 jdolecek * only sends SIGIO if last write(2) failed due to insufficient
273 1.41 jdolecek * memory to hold the data. This is compatible enough
274 1.41 jdolecek * with NetBSD semantics to not do anything about the
275 1.41 jdolecek * difference.
276 1.65 perry *
277 1.41 jdolecek * Linux does NOT send SIGIO for pipes. Deal with socketpair
278 1.41 jdolecek * ones and DTYPE_PIPE ones. For these, we don't set
279 1.41 jdolecek * the underlying flags (we don't pass O_ASYNC flag down
280 1.41 jdolecek * to sys_fcntl()), but set the FASYNC flag for file descriptor,
281 1.41 jdolecek * so that F_GETFL would report the ASYNC i/o is on.
282 1.39 manu */
283 1.41 jdolecek if (val & O_ASYNC) {
284 1.69.2.9 yamt if (((fp1 = fd_getfile(fd)) == NULL))
285 1.39 manu return (EBADF);
286 1.69 christos if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
287 1.69 christos && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
288 1.69 christos || (fp1->f_type == DTYPE_PIPE))
289 1.41 jdolecek val &= ~O_ASYNC;
290 1.41 jdolecek else {
291 1.41 jdolecek /* not a pipe, do not modify anything */
292 1.69.2.9 yamt fd_putfile(fd);
293 1.69 christos fp1 = NULL;
294 1.39 manu }
295 1.41 jdolecek }
296 1.41 jdolecek
297 1.41 jdolecek SCARG(&fca, fd) = fd;
298 1.41 jdolecek SCARG(&fca, cmd) = F_SETFL;
299 1.69.2.4 yamt SCARG(&fca, arg) = (void *) val;
300 1.39 manu
301 1.56 thorpej error = sys_fcntl(l, &fca, retval);
302 1.41 jdolecek
303 1.41 jdolecek /* Now set the FASYNC flag for pipes */
304 1.69 christos if (fp1) {
305 1.69.2.9 yamt if (!error) {
306 1.69.2.9 yamt mutex_enter(&fp1->f_lock);
307 1.69 christos fp1->f_flag |= FASYNC;
308 1.69.2.9 yamt mutex_exit(&fp1->f_lock);
309 1.69.2.9 yamt }
310 1.69.2.9 yamt fd_putfile(fd);
311 1.39 manu }
312 1.41 jdolecek
313 1.41 jdolecek return (error);
314 1.41 jdolecek }
315 1.69.2.8 yamt
316 1.1 fvdl case LINUX_F_GETLK:
317 1.69.2.8 yamt do_linux_getlk(fd, cmd, arg, linux, flock);
318 1.47 christos
319 1.1 fvdl case LINUX_F_SETLK:
320 1.1 fvdl case LINUX_F_SETLKW:
321 1.69.2.8 yamt do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
322 1.47 christos
323 1.1 fvdl case LINUX_F_SETOWN:
324 1.65 perry case LINUX_F_GETOWN:
325 1.13 fvdl /*
326 1.49 jdolecek * We need to route fcntl() for tty descriptors around normal
327 1.49 jdolecek * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
328 1.49 jdolecek * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
329 1.49 jdolecek * this is not a problem.
330 1.13 fvdl */
331 1.69.2.9 yamt if ((fp = fd_getfile(fd)) == NULL)
332 1.13 fvdl return EBADF;
333 1.60 jdolecek
334 1.60 jdolecek /* Check it's a character device vnode */
335 1.60 jdolecek if (fp->f_type != DTYPE_VNODE
336 1.60 jdolecek || (vp = (struct vnode *)fp->f_data) == NULL
337 1.60 jdolecek || vp->v_type != VCHR) {
338 1.69.2.9 yamt fd_putfile(fd);
339 1.60 jdolecek
340 1.53 gehenna not_tty:
341 1.49 jdolecek /* Not a tty, proceed with common fcntl() */
342 1.13 fvdl cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
343 1.49 jdolecek break;
344 1.49 jdolecek }
345 1.47 christos
346 1.69.2.6 yamt error = VOP_GETATTR(vp, &va, l->l_cred);
347 1.60 jdolecek
348 1.69.2.9 yamt fd_putfile(fd);
349 1.60 jdolecek
350 1.60 jdolecek if (error)
351 1.49 jdolecek return error;
352 1.60 jdolecek
353 1.53 gehenna cdev = cdevsw_lookup(va.va_rdev);
354 1.53 gehenna if (cdev == NULL)
355 1.53 gehenna return (ENXIO);
356 1.53 gehenna d_tty = cdev->d_tty;
357 1.49 jdolecek if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
358 1.53 gehenna goto not_tty;
359 1.49 jdolecek
360 1.49 jdolecek /* set tty pg_id appropriately */
361 1.49 jdolecek if (cmd == LINUX_F_GETOWN) {
362 1.58 dsl retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
363 1.49 jdolecek return 0;
364 1.49 jdolecek }
365 1.69.2.4 yamt mutex_enter(&proclist_lock);
366 1.49 jdolecek if ((long)arg <= 0) {
367 1.49 jdolecek pgid = -(long)arg;
368 1.49 jdolecek } else {
369 1.69.2.3 yamt struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
370 1.49 jdolecek if (p1 == NULL)
371 1.49 jdolecek return (ESRCH);
372 1.49 jdolecek pgid = (long)p1->p_pgrp->pg_id;
373 1.13 fvdl }
374 1.69.2.3 yamt pgrp = pg_find(pgid, PFIND_LOCKED);
375 1.69.2.3 yamt if (pgrp == NULL || pgrp->pg_session != p->p_session) {
376 1.69.2.4 yamt mutex_exit(&proclist_lock);
377 1.49 jdolecek return EPERM;
378 1.69.2.3 yamt }
379 1.49 jdolecek tp->t_pgrp = pgrp;
380 1.69.2.4 yamt mutex_exit(&proclist_lock);
381 1.49 jdolecek return 0;
382 1.47 christos
383 1.1 fvdl default:
384 1.1 fvdl return EOPNOTSUPP;
385 1.1 fvdl }
386 1.1 fvdl
387 1.1 fvdl SCARG(&fca, fd) = fd;
388 1.1 fvdl SCARG(&fca, cmd) = cmd;
389 1.1 fvdl SCARG(&fca, arg) = arg;
390 1.12 mycroft
391 1.56 thorpej return sys_fcntl(l, &fca, retval);
392 1.1 fvdl }
393 1.1 fvdl
394 1.67 manu #if !defined(__amd64__)
395 1.1 fvdl /*
396 1.1 fvdl * Convert a NetBSD stat structure to a Linux stat structure.
397 1.1 fvdl * Only the order of the fields and the padding in the structure
398 1.9 fvdl * is different. linux_fakedev is a machine-dependent function
399 1.9 fvdl * which optionally converts device driver major/minor numbers
400 1.9 fvdl * (XXX horrible, but what can you do against code that compares
401 1.9 fvdl * things against constant major device numbers? sigh)
402 1.1 fvdl */
403 1.1 fvdl static void
404 1.69.2.7 yamt bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
405 1.1 fvdl {
406 1.12 mycroft
407 1.45 christos lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
408 1.1 fvdl lsp->lst_ino = bsp->st_ino;
409 1.19 christos lsp->lst_mode = (linux_mode_t)bsp->st_mode;
410 1.19 christos if (bsp->st_nlink >= (1 << 15))
411 1.19 christos lsp->lst_nlink = (1 << 15) - 1;
412 1.19 christos else
413 1.19 christos lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
414 1.1 fvdl lsp->lst_uid = bsp->st_uid;
415 1.1 fvdl lsp->lst_gid = bsp->st_gid;
416 1.45 christos lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
417 1.1 fvdl lsp->lst_size = bsp->st_size;
418 1.1 fvdl lsp->lst_blksize = bsp->st_blksize;
419 1.1 fvdl lsp->lst_blocks = bsp->st_blocks;
420 1.1 fvdl lsp->lst_atime = bsp->st_atime;
421 1.1 fvdl lsp->lst_mtime = bsp->st_mtime;
422 1.1 fvdl lsp->lst_ctime = bsp->st_ctime;
423 1.66 christos #ifdef LINUX_STAT_HAS_NSEC
424 1.66 christos lsp->lst_atime_nsec = bsp->st_atimensec;
425 1.66 christos lsp->lst_mtime_nsec = bsp->st_mtimensec;
426 1.66 christos lsp->lst_ctime_nsec = bsp->st_ctimensec;
427 1.66 christos #endif
428 1.1 fvdl }
429 1.1 fvdl
430 1.1 fvdl /*
431 1.1 fvdl * The stat functions below are plain sailing. stat and lstat are handled
432 1.1 fvdl * by one function to avoid code duplication.
433 1.1 fvdl */
434 1.1 fvdl int
435 1.69.2.7 yamt linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
436 1.11 thorpej {
437 1.69.2.7 yamt /* {
438 1.1 fvdl syscallarg(int) fd;
439 1.1 fvdl syscallarg(linux_stat *) sp;
440 1.69.2.7 yamt } */
441 1.1 fvdl struct linux_stat tmplst;
442 1.69.2.4 yamt struct stat tmpst;
443 1.1 fvdl int error;
444 1.1 fvdl
445 1.69.2.9 yamt error = do_sys_fstat(SCARG(uap, fd), &tmpst);
446 1.69.2.4 yamt if (error != 0)
447 1.1 fvdl return error;
448 1.1 fvdl bsd_to_linux_stat(&tmpst, &tmplst);
449 1.1 fvdl
450 1.69.2.4 yamt return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
451 1.1 fvdl }
452 1.1 fvdl
453 1.1 fvdl static int
454 1.69.2.9 yamt linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval, int flags)
455 1.1 fvdl {
456 1.1 fvdl struct linux_stat tmplst;
457 1.69.2.4 yamt struct stat tmpst;
458 1.1 fvdl int error;
459 1.1 fvdl
460 1.69.2.9 yamt error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
461 1.69.2.4 yamt if (error != 0)
462 1.1 fvdl return error;
463 1.1 fvdl
464 1.1 fvdl bsd_to_linux_stat(&tmpst, &tmplst);
465 1.1 fvdl
466 1.69.2.4 yamt return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
467 1.1 fvdl }
468 1.1 fvdl
469 1.1 fvdl int
470 1.69.2.7 yamt linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
471 1.11 thorpej {
472 1.69.2.7 yamt /* {
473 1.27 christos syscallarg(const char *) path;
474 1.1 fvdl syscallarg(struct linux_stat *) sp;
475 1.69.2.7 yamt } */
476 1.11 thorpej
477 1.69.2.9 yamt return linux_stat1(uap, retval, FOLLOW);
478 1.1 fvdl }
479 1.1 fvdl
480 1.23 erh /* Note: this is "newlstat" in the Linux sources */
481 1.23 erh /* (we don't bother with the old lstat currently) */
482 1.1 fvdl int
483 1.69.2.7 yamt linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
484 1.11 thorpej {
485 1.69.2.7 yamt /* {
486 1.27 christos syscallarg(const char *) path;
487 1.1 fvdl syscallarg(struct linux_stat *) sp;
488 1.69.2.7 yamt } */
489 1.11 thorpej
490 1.69.2.9 yamt return linux_stat1((const void *)uap, retval, NOFOLLOW);
491 1.1 fvdl }
492 1.67 manu #endif /* !__amd64__ */
493 1.1 fvdl
494 1.1 fvdl /*
495 1.10 fvdl * The following syscalls are mostly here because of the alternate path check.
496 1.1 fvdl */
497 1.1 fvdl int
498 1.69.2.7 yamt linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
499 1.2 fvdl {
500 1.69.2.7 yamt /* {
501 1.27 christos syscallarg(const char *) path;
502 1.69.2.7 yamt } */
503 1.63 jdolecek int error;
504 1.63 jdolecek struct nameidata nd;
505 1.2 fvdl
506 1.69.2.7 yamt error = sys_unlink(l, (const void *)uap, retval);
507 1.63 jdolecek if (error != EPERM)
508 1.63 jdolecek return (error);
509 1.63 jdolecek
510 1.63 jdolecek /*
511 1.63 jdolecek * Linux returns EISDIR if unlink(2) is called on a directory.
512 1.63 jdolecek * We return EPERM in such cases. To emulate correct behaviour,
513 1.63 jdolecek * check if the path points to directory and return EISDIR if this
514 1.63 jdolecek * is the case.
515 1.63 jdolecek */
516 1.69.2.4 yamt NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
517 1.69.2.7 yamt SCARG(uap, path));
518 1.63 jdolecek if (namei(&nd) == 0) {
519 1.63 jdolecek struct stat sb;
520 1.63 jdolecek
521 1.69.2.9 yamt if (vn_stat(nd.ni_vp, &sb) == 0
522 1.63 jdolecek && S_ISDIR(sb.st_mode))
523 1.63 jdolecek error = EISDIR;
524 1.63 jdolecek
525 1.63 jdolecek vput(nd.ni_vp);
526 1.63 jdolecek }
527 1.63 jdolecek
528 1.63 jdolecek return (error);
529 1.2 fvdl }
530 1.2 fvdl
531 1.10 fvdl int
532 1.69.2.7 yamt linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
533 1.11 thorpej {
534 1.69.2.7 yamt /* {
535 1.27 christos syscallarg(const char *) path;
536 1.2 fvdl syscallarg(int) mode;
537 1.2 fvdl syscallarg(int) dev;
538 1.69.2.7 yamt } */
539 1.2 fvdl
540 1.10 fvdl /*
541 1.40 wiz * BSD handles FIFOs separately
542 1.10 fvdl */
543 1.69.2.2 yamt if (S_ISFIFO(SCARG(uap, mode))) {
544 1.54 jdolecek struct sys_mkfifo_args bma;
545 1.54 jdolecek
546 1.21 thorpej SCARG(&bma, path) = SCARG(uap, path);
547 1.21 thorpej SCARG(&bma, mode) = SCARG(uap, mode);
548 1.56 thorpej return sys_mkfifo(l, &bma, retval);
549 1.54 jdolecek } else {
550 1.54 jdolecek struct sys_mknod_args bma;
551 1.54 jdolecek
552 1.54 jdolecek SCARG(&bma, path) = SCARG(uap, path);
553 1.54 jdolecek SCARG(&bma, mode) = SCARG(uap, mode);
554 1.54 jdolecek /*
555 1.54 jdolecek * Linux device numbers uses 8 bits for minor and 8 bits
556 1.54 jdolecek * for major. Due to how we map our major and minor,
557 1.69.2.4 yamt * this just fits into our dev_t. Just mask off the
558 1.54 jdolecek * upper 16bit to remove any random junk.
559 1.54 jdolecek */
560 1.54 jdolecek SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
561 1.56 thorpej return sys_mknod(l, &bma, retval);
562 1.54 jdolecek }
563 1.2 fvdl }
564 1.2 fvdl
565 1.15 fvdl /*
566 1.15 fvdl * This is just fsync() for now (just as it is in the Linux kernel)
567 1.23 erh * Note: this is not implemented under Linux on Alpha and Arm
568 1.23 erh * but should still be defined in our syscalls.master.
569 1.23 erh * (syscall #148 on the arm)
570 1.15 fvdl */
571 1.15 fvdl int
572 1.69.2.7 yamt linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
573 1.15 fvdl {
574 1.69.2.7 yamt /* {
575 1.15 fvdl syscallarg(int) fd;
576 1.69.2.7 yamt } */
577 1.69.2.7 yamt
578 1.69.2.7 yamt return sys_fsync(l, (const void *)uap, retval);
579 1.28 tron }
580 1.28 tron
581 1.28 tron /*
582 1.28 tron * pread(2).
583 1.28 tron */
584 1.28 tron int
585 1.69.2.7 yamt linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
586 1.28 tron {
587 1.69.2.7 yamt /* {
588 1.28 tron syscallarg(int) fd;
589 1.28 tron syscallarg(void *) buf;
590 1.28 tron syscallarg(size_t) nbyte;
591 1.28 tron syscallarg(linux_off_t) offset;
592 1.69.2.7 yamt } */
593 1.28 tron struct sys_pread_args pra;
594 1.28 tron
595 1.28 tron SCARG(&pra, fd) = SCARG(uap, fd);
596 1.28 tron SCARG(&pra, buf) = SCARG(uap, buf);
597 1.28 tron SCARG(&pra, nbyte) = SCARG(uap, nbyte);
598 1.28 tron SCARG(&pra, offset) = SCARG(uap, offset);
599 1.28 tron
600 1.62 jdolecek return sys_pread(l, &pra, retval);
601 1.28 tron }
602 1.28 tron
603 1.28 tron /*
604 1.28 tron * pwrite(2).
605 1.28 tron */
606 1.28 tron int
607 1.69.2.7 yamt linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
608 1.28 tron {
609 1.69.2.7 yamt /* {
610 1.28 tron syscallarg(int) fd;
611 1.28 tron syscallarg(void *) buf;
612 1.28 tron syscallarg(size_t) nbyte;
613 1.28 tron syscallarg(linux_off_t) offset;
614 1.69.2.7 yamt } */
615 1.28 tron struct sys_pwrite_args pra;
616 1.28 tron
617 1.28 tron SCARG(&pra, fd) = SCARG(uap, fd);
618 1.28 tron SCARG(&pra, buf) = SCARG(uap, buf);
619 1.28 tron SCARG(&pra, nbyte) = SCARG(uap, nbyte);
620 1.28 tron SCARG(&pra, offset) = SCARG(uap, offset);
621 1.28 tron
622 1.62 jdolecek return sys_pwrite(l, &pra, retval);
623 1.1 fvdl }
624 1.68 fvdl
625 1.69.2.2 yamt #define LINUX_NOT_SUPPORTED(fun) \
626 1.69.2.2 yamt int \
627 1.69.2.7 yamt fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
628 1.69.2.2 yamt { \
629 1.69.2.2 yamt return EOPNOTSUPP; \
630 1.69.2.2 yamt }
631 1.69.2.2 yamt
632 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_setxattr)
633 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
634 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
635 1.69.2.2 yamt
636 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_getxattr)
637 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
638 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
639 1.69.2.2 yamt
640 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_listxattr)
641 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
642 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
643 1.69.2.2 yamt
644 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_removexattr)
645 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
646 1.69.2.2 yamt LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
647 1.69.2.7 yamt
648