linux_file.c revision 1.80.2.1 1 /* $NetBSD: linux_file.c,v 1.80.2.1 2007/03/13 16:50:18 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998 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 * Functions in multiarch:
41 * linux_sys_llseek : linux_llseek.c
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.80.2.1 2007/03/13 16:50:18 ad Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.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/vnode.h>
60 #include <sys/tty.h>
61 #include <sys/socketvar.h>
62 #include <sys/conf.h>
63 #include <sys/pipe.h>
64
65 #include <sys/syscallargs.h>
66 #include <sys/vfs_syscalls.h>
67
68 #include <compat/linux/common/linux_types.h>
69 #include <compat/linux/common/linux_signal.h>
70 #include <compat/linux/common/linux_fcntl.h>
71 #include <compat/linux/common/linux_util.h>
72 #include <compat/linux/common/linux_machdep.h>
73
74 #include <compat/linux/linux_syscallargs.h>
75
76 static int linux_to_bsd_ioflags __P((int));
77 static int bsd_to_linux_ioflags __P((int));
78 static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
79 static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
80 #ifndef __amd64__
81 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
82 static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
83 #endif
84
85 /*
86 * Some file-related calls are handled here. The usual flag conversion
87 * an structure conversion is done, and alternate emul path searching.
88 */
89
90 /*
91 * The next two functions convert between the Linux and NetBSD values
92 * of the flags used in open(2) and fcntl(2).
93 */
94 static int
95 linux_to_bsd_ioflags(lflags)
96 int lflags;
97 {
98 int res = 0;
99
100 res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
101 res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
102 res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
103 res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
104 res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
105 res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
106 res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
107 res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
108 res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
109 res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
110 res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
111
112 return res;
113 }
114
115 static int
116 bsd_to_linux_ioflags(bflags)
117 int bflags;
118 {
119 int res = 0;
120
121 res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
122 res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
123 res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
124 res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
125 res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
126 res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
127 res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
128 res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
129 res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
130 res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
131 res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
132
133 return res;
134 }
135
136 /*
137 * creat(2) is an obsolete function, but it's present as a Linux
138 * system call, so let's deal with it.
139 *
140 * Note: On the Alpha this doesn't really exist in Linux, but it's defined
141 * in syscalls.master anyway so this doesn't have to be special cased.
142 *
143 * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
144 */
145 int
146 linux_sys_creat(l, v, retval)
147 struct lwp *l;
148 void *v;
149 register_t *retval;
150 {
151 struct linux_sys_creat_args /* {
152 syscallarg(const char *) path;
153 syscallarg(int) mode;
154 } */ *uap = v;
155 struct proc *p = l->l_proc;
156 struct sys_open_args oa;
157 void *sg;
158
159 sg = stackgap_init(p, 0);
160 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
161
162 SCARG(&oa, path) = SCARG(uap, path);
163 SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
164 SCARG(&oa, mode) = SCARG(uap, mode);
165
166 return sys_open(l, &oa, retval);
167 }
168
169 /*
170 * open(2). Take care of the different flag values, and let the
171 * NetBSD syscall do the real work. See if this operation
172 * gives the current process a controlling terminal.
173 * (XXX is this necessary?)
174 */
175 int
176 linux_sys_open(l, v, retval)
177 struct lwp *l;
178 void *v;
179 register_t *retval;
180 {
181 struct linux_sys_open_args /* {
182 syscallarg(const char *) path;
183 syscallarg(int) flags;
184 syscallarg(int) mode;
185 } */ *uap = v;
186 struct proc *p = l->l_proc;
187 int error, fl;
188 struct sys_open_args boa;
189 void *sg;
190
191 sg = stackgap_init(p, 0);
192
193 fl = linux_to_bsd_ioflags(SCARG(uap, flags));
194
195 if (fl & O_CREAT)
196 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
197 else
198 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
199
200 SCARG(&boa, path) = SCARG(uap, path);
201 SCARG(&boa, flags) = fl;
202 SCARG(&boa, mode) = SCARG(uap, mode);
203
204 if ((error = sys_open(l, &boa, retval)))
205 return error;
206
207 /*
208 * this bit from sunos_misc.c (and svr4_fcntl.c).
209 * If we are a session leader, and we don't have a controlling
210 * terminal yet, and the O_NOCTTY flag is not set, try to make
211 * this the controlling terminal.
212 */
213 if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
214 struct filedesc *fdp = p->p_fd;
215 struct file *fp;
216
217 fp = fd_getfile(fdp, *retval);
218
219 /* ignore any error, just give it a try */
220 if (fp != NULL) {
221 FILE_USE(fp);
222 if (fp->f_type == DTYPE_VNODE) {
223 (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
224 (void *) 0, l);
225 }
226 FILE_UNUSE(fp, l);
227 }
228 }
229 return 0;
230 }
231
232 /*
233 * The next two functions take care of converting the flock
234 * structure back and forth between Linux and NetBSD format.
235 * The only difference in the structures is the order of
236 * the fields, and the 'whence' value.
237 */
238 static void
239 bsd_to_linux_flock(bfp, lfp)
240 struct flock *bfp;
241 struct linux_flock *lfp;
242 {
243
244 lfp->l_start = bfp->l_start;
245 lfp->l_len = bfp->l_len;
246 lfp->l_pid = bfp->l_pid;
247 lfp->l_whence = bfp->l_whence;
248 switch (bfp->l_type) {
249 case F_RDLCK:
250 lfp->l_type = LINUX_F_RDLCK;
251 break;
252 case F_UNLCK:
253 lfp->l_type = LINUX_F_UNLCK;
254 break;
255 case F_WRLCK:
256 lfp->l_type = LINUX_F_WRLCK;
257 break;
258 }
259 }
260
261 static void
262 linux_to_bsd_flock(lfp, bfp)
263 struct linux_flock *lfp;
264 struct flock *bfp;
265 {
266
267 bfp->l_start = lfp->l_start;
268 bfp->l_len = lfp->l_len;
269 bfp->l_pid = lfp->l_pid;
270 bfp->l_whence = lfp->l_whence;
271 switch (lfp->l_type) {
272 case LINUX_F_RDLCK:
273 bfp->l_type = F_RDLCK;
274 break;
275 case LINUX_F_UNLCK:
276 bfp->l_type = F_UNLCK;
277 break;
278 case LINUX_F_WRLCK:
279 bfp->l_type = F_WRLCK;
280 break;
281 }
282 }
283
284 /*
285 * Most actions in the fcntl() call are straightforward; simply
286 * pass control to the NetBSD system call. A few commands need
287 * conversions after the actual system call has done its work,
288 * because the flag values and lock structure are different.
289 */
290 int
291 linux_sys_fcntl(l, v, retval)
292 struct lwp *l;
293 void *v;
294 register_t *retval;
295 {
296 struct linux_sys_fcntl_args /* {
297 syscallarg(int) fd;
298 syscallarg(int) cmd;
299 syscallarg(void *) arg;
300 } */ *uap = v;
301 struct proc *p = l->l_proc;
302 int fd, cmd, error;
303 u_long val;
304 void *arg, *sg;
305 struct linux_flock lfl;
306 struct flock *bfp, bfl;
307 struct sys_fcntl_args fca;
308 struct filedesc *fdp;
309 struct file *fp;
310 struct vnode *vp;
311 struct vattr va;
312 const struct cdevsw *cdev;
313 long pgid;
314 struct pgrp *pgrp;
315 struct tty *tp, *(*d_tty) __P((dev_t));
316
317 fd = SCARG(uap, fd);
318 cmd = SCARG(uap, cmd);
319 arg = (void *) SCARG(uap, arg);
320
321 switch (cmd) {
322 case LINUX_F_DUPFD:
323 cmd = F_DUPFD;
324 break;
325 case LINUX_F_GETFD:
326 cmd = F_GETFD;
327 break;
328 case LINUX_F_SETFD:
329 cmd = F_SETFD;
330 break;
331 case LINUX_F_GETFL:
332 SCARG(&fca, fd) = fd;
333 SCARG(&fca, cmd) = F_GETFL;
334 SCARG(&fca, arg) = arg;
335 if ((error = sys_fcntl(l, &fca, retval)))
336 return error;
337 retval[0] = bsd_to_linux_ioflags(retval[0]);
338 return 0;
339 case LINUX_F_SETFL: {
340 struct file *fp1 = NULL;
341
342 val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
343 /*
344 * Linux seems to have same semantics for sending SIGIO to the
345 * read side of socket, but slightly different semantics
346 * for SIGIO to the write side. Rather than sending the SIGIO
347 * every time it's possible to write (directly) more data, it
348 * only sends SIGIO if last write(2) failed due to insufficient
349 * memory to hold the data. This is compatible enough
350 * with NetBSD semantics to not do anything about the
351 * difference.
352 *
353 * Linux does NOT send SIGIO for pipes. Deal with socketpair
354 * ones and DTYPE_PIPE ones. For these, we don't set
355 * the underlying flags (we don't pass O_ASYNC flag down
356 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
357 * so that F_GETFL would report the ASYNC i/o is on.
358 */
359 if (val & O_ASYNC) {
360 if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
361 return (EBADF);
362
363 FILE_USE(fp1);
364
365 if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
366 && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
367 || (fp1->f_type == DTYPE_PIPE))
368 val &= ~O_ASYNC;
369 else {
370 /* not a pipe, do not modify anything */
371 FILE_UNUSE(fp1, l);
372 fp1 = NULL;
373 }
374 }
375
376 SCARG(&fca, fd) = fd;
377 SCARG(&fca, cmd) = F_SETFL;
378 SCARG(&fca, arg) = (void *) val;
379
380 error = sys_fcntl(l, &fca, retval);
381
382 /* Now set the FASYNC flag for pipes */
383 if (fp1) {
384 if (!error)
385 fp1->f_flag |= FASYNC;
386 FILE_UNUSE(fp1, l);
387 }
388
389 return (error);
390 }
391 case LINUX_F_GETLK:
392 sg = stackgap_init(p, 0);
393 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
394 if ((error = copyin(arg, &lfl, sizeof lfl)))
395 return error;
396 linux_to_bsd_flock(&lfl, &bfl);
397 if ((error = copyout(&bfl, bfp, sizeof bfl)))
398 return error;
399 SCARG(&fca, fd) = fd;
400 SCARG(&fca, cmd) = F_GETLK;
401 SCARG(&fca, arg) = bfp;
402 if ((error = sys_fcntl(l, &fca, retval)))
403 return error;
404 if ((error = copyin(bfp, &bfl, sizeof bfl)))
405 return error;
406 bsd_to_linux_flock(&bfl, &lfl);
407 return copyout(&lfl, arg, sizeof lfl);
408
409 case LINUX_F_SETLK:
410 case LINUX_F_SETLKW:
411 cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
412 if ((error = copyin(arg, &lfl, sizeof lfl)))
413 return error;
414 linux_to_bsd_flock(&lfl, &bfl);
415 sg = stackgap_init(p, 0);
416 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
417 if ((error = copyout(&bfl, bfp, sizeof bfl)))
418 return error;
419 arg = (void *)bfp;
420 break;
421
422 case LINUX_F_SETOWN:
423 case LINUX_F_GETOWN:
424 /*
425 * We need to route fcntl() for tty descriptors around normal
426 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
427 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
428 * this is not a problem.
429 */
430 fdp = p->p_fd;
431 if ((fp = fd_getfile(fdp, fd)) == NULL)
432 return EBADF;
433 FILE_USE(fp);
434
435 /* Check it's a character device vnode */
436 if (fp->f_type != DTYPE_VNODE
437 || (vp = (struct vnode *)fp->f_data) == NULL
438 || vp->v_type != VCHR) {
439 FILE_UNUSE(fp, l);
440
441 not_tty:
442 /* Not a tty, proceed with common fcntl() */
443 cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
444 break;
445 }
446
447 error = VOP_GETATTR(vp, &va, l->l_cred, l);
448
449 FILE_UNUSE(fp, l);
450
451 if (error)
452 return error;
453
454 cdev = cdevsw_lookup(va.va_rdev);
455 if (cdev == NULL)
456 return (ENXIO);
457 d_tty = cdev->d_tty;
458 if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
459 goto not_tty;
460
461 /* set tty pg_id appropriately */
462 if (cmd == LINUX_F_GETOWN) {
463 retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
464 return 0;
465 }
466 mutex_enter(&proclist_lock);
467 if ((long)arg <= 0) {
468 pgid = -(long)arg;
469 } else {
470 struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
471 if (p1 == NULL)
472 return (ESRCH);
473 pgid = (long)p1->p_pgrp->pg_id;
474 }
475 pgrp = pg_find(pgid, PFIND_LOCKED);
476 if (pgrp == NULL || pgrp->pg_session != p->p_session) {
477 mutex_exit(&proclist_lock);
478 return EPERM;
479 }
480 tp->t_pgrp = pgrp;
481 mutex_exit(&proclist_lock);
482 return 0;
483
484 default:
485 return EOPNOTSUPP;
486 }
487
488 SCARG(&fca, fd) = fd;
489 SCARG(&fca, cmd) = cmd;
490 SCARG(&fca, arg) = arg;
491
492 return sys_fcntl(l, &fca, retval);
493 }
494
495 #if !defined(__amd64__)
496 /*
497 * Convert a NetBSD stat structure to a Linux stat structure.
498 * Only the order of the fields and the padding in the structure
499 * is different. linux_fakedev is a machine-dependent function
500 * which optionally converts device driver major/minor numbers
501 * (XXX horrible, but what can you do against code that compares
502 * things against constant major device numbers? sigh)
503 */
504 static void
505 bsd_to_linux_stat(bsp, lsp)
506 struct stat *bsp;
507 struct linux_stat *lsp;
508 {
509
510 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
511 lsp->lst_ino = bsp->st_ino;
512 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
513 if (bsp->st_nlink >= (1 << 15))
514 lsp->lst_nlink = (1 << 15) - 1;
515 else
516 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
517 lsp->lst_uid = bsp->st_uid;
518 lsp->lst_gid = bsp->st_gid;
519 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
520 lsp->lst_size = bsp->st_size;
521 lsp->lst_blksize = bsp->st_blksize;
522 lsp->lst_blocks = bsp->st_blocks;
523 lsp->lst_atime = bsp->st_atime;
524 lsp->lst_mtime = bsp->st_mtime;
525 lsp->lst_ctime = bsp->st_ctime;
526 #ifdef LINUX_STAT_HAS_NSEC
527 lsp->lst_atime_nsec = bsp->st_atimensec;
528 lsp->lst_mtime_nsec = bsp->st_mtimensec;
529 lsp->lst_ctime_nsec = bsp->st_ctimensec;
530 #endif
531 }
532
533 /*
534 * The stat functions below are plain sailing. stat and lstat are handled
535 * by one function to avoid code duplication.
536 */
537 int
538 linux_sys_fstat(l, v, retval)
539 struct lwp *l;
540 void *v;
541 register_t *retval;
542 {
543 struct linux_sys_fstat_args /* {
544 syscallarg(int) fd;
545 syscallarg(linux_stat *) sp;
546 } */ *uap = v;
547 struct linux_stat tmplst;
548 struct stat tmpst;
549 int error;
550
551 error = do_sys_fstat(l, SCARG(uap, fd), &tmpst);
552 if (error != 0)
553 return error;
554 bsd_to_linux_stat(&tmpst, &tmplst);
555
556 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
557 }
558
559 static int
560 linux_stat1(l, v, retval, flags)
561 struct lwp *l;
562 void *v;
563 register_t *retval;
564 int flags;
565 {
566 struct linux_stat tmplst;
567 struct stat tmpst;
568 void *sg;
569 int error;
570 struct linux_sys_stat_args *uap = v;
571
572 sg = stackgap_init(l->l_proc, 0);
573 if (flags == NOFOLLOW)
574 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
575 else
576 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
577
578 error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
579 if (error != 0)
580 return error;
581
582 bsd_to_linux_stat(&tmpst, &tmplst);
583
584 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
585 }
586
587 int
588 linux_sys_stat(l, v, retval)
589 struct lwp *l;
590 void *v;
591 register_t *retval;
592 {
593 struct linux_sys_stat_args /* {
594 syscallarg(const char *) path;
595 syscallarg(struct linux_stat *) sp;
596 } */ *uap = v;
597
598 return linux_stat1(l, uap, retval, FOLLOW);
599 }
600
601 /* Note: this is "newlstat" in the Linux sources */
602 /* (we don't bother with the old lstat currently) */
603 int
604 linux_sys_lstat(l, v, retval)
605 struct lwp *l;
606 void *v;
607 register_t *retval;
608 {
609 struct linux_sys_lstat_args /* {
610 syscallarg(const char *) path;
611 syscallarg(struct linux_stat *) sp;
612 } */ *uap = v;
613
614 return linux_stat1(l, uap, retval, NOFOLLOW);
615 }
616 #endif /* !__amd64__ */
617
618 /*
619 * The following syscalls are mostly here because of the alternate path check.
620 */
621 int
622 linux_sys_access(l, v, retval)
623 struct lwp *l;
624 void *v;
625 register_t *retval;
626 {
627 struct linux_sys_access_args /* {
628 syscallarg(const char *) path;
629 syscallarg(int) flags;
630 } */ *uap = v;
631 struct proc *p = l->l_proc;
632 void *sg = stackgap_init(p, 0);
633
634 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
635
636 return sys_access(l, uap, retval);
637 }
638
639 int
640 linux_sys_unlink(l, v, retval)
641 struct lwp *l;
642 void *v;
643 register_t *retval;
644
645 {
646 struct linux_sys_unlink_args /* {
647 syscallarg(const char *) path;
648 } */ *uap = v;
649 struct proc *p = l->l_proc;
650 void *sg = stackgap_init(p, 0);
651 int error;
652 struct nameidata nd;
653
654 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
655
656 error = sys_unlink(l, uap, retval);
657 if (error != EPERM)
658 return (error);
659
660 /*
661 * Linux returns EISDIR if unlink(2) is called on a directory.
662 * We return EPERM in such cases. To emulate correct behaviour,
663 * check if the path points to directory and return EISDIR if this
664 * is the case.
665 */
666 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
667 SCARG(uap, path), l);
668 if (namei(&nd) == 0) {
669 struct stat sb;
670
671 if (vn_stat(nd.ni_vp, &sb, l) == 0
672 && S_ISDIR(sb.st_mode))
673 error = EISDIR;
674
675 vput(nd.ni_vp);
676 }
677
678 return (error);
679 }
680
681 int
682 linux_sys_chdir(l, v, retval)
683 struct lwp *l;
684 void *v;
685 register_t *retval;
686 {
687 struct linux_sys_chdir_args /* {
688 syscallarg(const char *) path;
689 } */ *uap = v;
690 struct proc *p = l->l_proc;
691 void *sg = stackgap_init(p, 0);
692
693 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
694
695 return sys_chdir(l, uap, retval);
696 }
697
698 int
699 linux_sys_mknod(l, v, retval)
700 struct lwp *l;
701 void *v;
702 register_t *retval;
703 {
704 struct linux_sys_mknod_args /* {
705 syscallarg(const char *) path;
706 syscallarg(int) mode;
707 syscallarg(int) dev;
708 } */ *uap = v;
709 struct proc *p = l->l_proc;
710 void *sg = stackgap_init(p, 0);
711
712 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
713
714 /*
715 * BSD handles FIFOs separately
716 */
717 if (S_ISFIFO(SCARG(uap, mode))) {
718 struct sys_mkfifo_args bma;
719
720 SCARG(&bma, path) = SCARG(uap, path);
721 SCARG(&bma, mode) = SCARG(uap, mode);
722 return sys_mkfifo(l, &bma, retval);
723 } else {
724 struct sys_mknod_args bma;
725
726 SCARG(&bma, path) = SCARG(uap, path);
727 SCARG(&bma, mode) = SCARG(uap, mode);
728 /*
729 * Linux device numbers uses 8 bits for minor and 8 bits
730 * for major. Due to how we map our major and minor,
731 * this just fints into our dev_t. Just mask off the
732 * upper 16bit to remove any random junk.
733 */
734 SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
735 return sys_mknod(l, &bma, retval);
736 }
737 }
738
739 int
740 linux_sys_chmod(l, v, retval)
741 struct lwp *l;
742 void *v;
743 register_t *retval;
744 {
745 struct linux_sys_chmod_args /* {
746 syscallarg(const char *) path;
747 syscallarg(int) mode;
748 } */ *uap = v;
749 struct proc *p = l->l_proc;
750 void *sg = stackgap_init(p, 0);
751
752 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
753
754 return sys_chmod(l, uap, retval);
755 }
756
757 #if defined(__i386__) || defined(__m68k__) || \
758 defined(__arm__)
759 int
760 linux_sys_chown16(l, v, retval)
761 struct lwp *l;
762 void *v;
763 register_t *retval;
764 {
765 struct linux_sys_chown16_args /* {
766 syscallarg(const char *) path;
767 syscallarg(int) uid;
768 syscallarg(int) gid;
769 } */ *uap = v;
770 struct proc *p = l->l_proc;
771 struct sys___posix_chown_args bca;
772 void *sg = stackgap_init(p, 0);
773
774 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
775
776 SCARG(&bca, path) = SCARG(uap, path);
777 SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
778 (uid_t)-1 : SCARG(uap, uid);
779 SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
780 (gid_t)-1 : SCARG(uap, gid);
781
782 return sys___posix_chown(l, &bca, retval);
783 }
784
785 int
786 linux_sys_fchown16(l, v, retval)
787 struct lwp *l;
788 void *v;
789 register_t *retval;
790 {
791 struct linux_sys_fchown16_args /* {
792 syscallarg(int) fd;
793 syscallarg(int) uid;
794 syscallarg(int) gid;
795 } */ *uap = v;
796 struct sys___posix_fchown_args bfa;
797
798 SCARG(&bfa, fd) = SCARG(uap, fd);
799 SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
800 (uid_t)-1 : SCARG(uap, uid);
801 SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
802 (gid_t)-1 : SCARG(uap, gid);
803
804 return sys___posix_fchown(l, &bfa, retval);
805 }
806
807 int
808 linux_sys_lchown16(l, v, retval)
809 struct lwp *l;
810 void *v;
811 register_t *retval;
812 {
813 struct linux_sys_lchown16_args /* {
814 syscallarg(char *) path;
815 syscallarg(int) uid;
816 syscallarg(int) gid;
817 } */ *uap = v;
818 struct proc *p = l->l_proc;
819 struct sys___posix_lchown_args bla;
820 void *sg = stackgap_init(p, 0);
821
822 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
823
824 SCARG(&bla, path) = SCARG(uap, path);
825 SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
826 (uid_t)-1 : SCARG(uap, uid);
827 SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
828 (gid_t)-1 : SCARG(uap, gid);
829
830 return sys___posix_lchown(l, &bla, retval);
831 }
832 #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
833 #if defined (__i386__) || defined (__m68k__) || defined(__amd64__) || \
834 defined (__powerpc__) || defined (__mips__) || defined (__arm__)
835 int
836 linux_sys_chown(l, v, retval)
837 struct lwp *l;
838 void *v;
839 register_t *retval;
840 {
841 struct linux_sys_chown_args /* {
842 syscallarg(char *) path;
843 syscallarg(int) uid;
844 syscallarg(int) gid;
845 } */ *uap = v;
846 struct proc *p = l->l_proc;
847 void *sg = stackgap_init(p, 0);
848
849 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
850
851 return sys___posix_chown(l, uap, retval);
852 }
853
854 int
855 linux_sys_lchown(l, v, retval)
856 struct lwp *l;
857 void *v;
858 register_t *retval;
859 {
860 struct linux_sys_lchown_args /* {
861 syscallarg(char *) path;
862 syscallarg(int) uid;
863 syscallarg(int) gid;
864 } */ *uap = v;
865 struct proc *p = l->l_proc;
866 void *sg = stackgap_init(p, 0);
867
868 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
869
870 return sys___posix_lchown(l, uap, retval);
871 }
872 #endif /* __i386__||__m68k__||__powerpc__||__mips__||__arm__ ||__amd64__ */
873
874 int
875 linux_sys_rename(l, v, retval)
876 struct lwp *l;
877 void *v;
878 register_t *retval;
879 {
880 struct linux_sys_rename_args /* {
881 syscallarg(const char *) from;
882 syscallarg(const char *) to;
883 } */ *uap = v;
884 struct proc *p = l->l_proc;
885 void *sg = stackgap_init(p, 0);
886
887 CHECK_ALT_EXIST(l, &sg, SCARG(uap, from));
888 CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
889
890 return sys___posix_rename(l, uap, retval);
891 }
892
893 int
894 linux_sys_mkdir(l, v, retval)
895 struct lwp *l;
896 void *v;
897 register_t *retval;
898 {
899 struct linux_sys_mkdir_args /* {
900 syscallarg(const char *) path;
901 syscallarg(int) mode;
902 } */ *uap = v;
903 struct proc *p = l->l_proc;
904 void *sg = stackgap_init(p, 0);
905
906 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
907
908 return sys_mkdir(l, uap, retval);
909 }
910
911 int
912 linux_sys_rmdir(l, v, retval)
913 struct lwp *l;
914 void *v;
915 register_t *retval;
916 {
917 struct linux_sys_rmdir_args /* {
918 syscallarg(const char *) path;
919 } */ *uap = v;
920 struct proc *p = l->l_proc;
921 void *sg = stackgap_init(p, 0);
922
923 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
924
925 return sys_rmdir(l, uap, retval);
926 }
927
928 int
929 linux_sys_symlink(l, v, retval)
930 struct lwp *l;
931 void *v;
932 register_t *retval;
933 {
934 struct linux_sys_symlink_args /* {
935 syscallarg(const char *) path;
936 syscallarg(const char *) to;
937 } */ *uap = v;
938 struct proc *p = l->l_proc;
939 void *sg = stackgap_init(p, 0);
940
941 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
942 CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
943
944 return sys_symlink(l, uap, retval);
945 }
946
947 int
948 linux_sys_link(l, v, retval)
949 struct lwp *l;
950 void *v;
951 register_t *retval;
952 {
953 struct linux_sys_link_args /* {
954 syscallarg(const char *) path;
955 syscallarg(const char *) link;
956 } */ *uap = v;
957 struct proc *p = l->l_proc;
958 void *sg = stackgap_init(p, 0);
959
960 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
961 CHECK_ALT_CREAT(l, &sg, SCARG(uap, link));
962
963 return sys_link(l, uap, retval);
964 }
965
966 int
967 linux_sys_readlink(l, v, retval)
968 struct lwp *l;
969 void *v;
970 register_t *retval;
971 {
972 struct linux_sys_readlink_args /* {
973 syscallarg(const char *) name;
974 syscallarg(char *) buf;
975 syscallarg(int) count;
976 } */ *uap = v;
977 struct proc *p = l->l_proc;
978 void *sg = stackgap_init(p, 0);
979
980 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, name));
981
982 return sys_readlink(l, uap, retval);
983 }
984
985 #if !defined(__amd64__)
986 int
987 linux_sys_truncate(l, v, retval)
988 struct lwp *l;
989 void *v;
990 register_t *retval;
991 {
992 struct linux_sys_truncate_args /* {
993 syscallarg(const char *) path;
994 syscallarg(long) length;
995 } */ *uap = v;
996 struct proc *p = l->l_proc;
997 void *sg = stackgap_init(p, 0);
998
999 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
1000
1001 return compat_43_sys_truncate(l, uap, retval);
1002 }
1003 #endif /* !__amd64__ */
1004
1005 /*
1006 * This is just fsync() for now (just as it is in the Linux kernel)
1007 * Note: this is not implemented under Linux on Alpha and Arm
1008 * but should still be defined in our syscalls.master.
1009 * (syscall #148 on the arm)
1010 */
1011 int
1012 linux_sys_fdatasync(l, v, retval)
1013 struct lwp *l;
1014 void *v;
1015 register_t *retval;
1016 {
1017 #ifdef notdef
1018 struct linux_sys_fdatasync_args /* {
1019 syscallarg(int) fd;
1020 } */ *uap = v;
1021 #endif
1022 return sys_fsync(l, v, retval);
1023 }
1024
1025 /*
1026 * pread(2).
1027 */
1028 int
1029 linux_sys_pread(l, v, retval)
1030 struct lwp *l;
1031 void *v;
1032 register_t *retval;
1033 {
1034 struct linux_sys_pread_args /* {
1035 syscallarg(int) fd;
1036 syscallarg(void *) buf;
1037 syscallarg(size_t) nbyte;
1038 syscallarg(linux_off_t) offset;
1039 } */ *uap = v;
1040 struct sys_pread_args pra;
1041
1042 SCARG(&pra, fd) = SCARG(uap, fd);
1043 SCARG(&pra, buf) = SCARG(uap, buf);
1044 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
1045 SCARG(&pra, offset) = SCARG(uap, offset);
1046
1047 return sys_pread(l, &pra, retval);
1048 }
1049
1050 /*
1051 * pwrite(2).
1052 */
1053 int
1054 linux_sys_pwrite(l, v, retval)
1055 struct lwp *l;
1056 void *v;
1057 register_t *retval;
1058 {
1059 struct linux_sys_pwrite_args /* {
1060 syscallarg(int) fd;
1061 syscallarg(void *) buf;
1062 syscallarg(size_t) nbyte;
1063 syscallarg(linux_off_t) offset;
1064 } */ *uap = v;
1065 struct sys_pwrite_args pra;
1066
1067 SCARG(&pra, fd) = SCARG(uap, fd);
1068 SCARG(&pra, buf) = SCARG(uap, buf);
1069 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
1070 SCARG(&pra, offset) = SCARG(uap, offset);
1071
1072 return sys_pwrite(l, &pra, retval);
1073 }
1074
1075 #define LINUX_NOT_SUPPORTED(fun) \
1076 int \
1077 fun(struct lwp *l, void *v, register_t *retval) \
1078 { \
1079 return EOPNOTSUPP; \
1080 }
1081
1082 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
1083 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
1084 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
1085
1086 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
1087 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
1088 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
1089
1090 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
1091 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
1092 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
1093
1094 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
1095 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
1096 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
1097