]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/compat/linux/linux_file.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / sys / compat / linux / linux_file.c
1 /*-
2  * Copyright (c) 1994-1995 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_compat.h"
33 #include "opt_mac.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/lock.h>
43 #include <sys/mac.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/stat.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysproto.h>
51 #include <sys/unistd.h>
52 #include <sys/tty.h>
53 #include <sys/vnode.h>
54
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/ufsmount.h>
58
59 #include "opt_compat.h"
60
61 #ifdef COMPAT_LINUX32
62 #include <machine/../linux32/linux.h>
63 #include <machine/../linux32/linux32_proto.h>
64 #else
65 #include <machine/../linux/linux.h>
66 #include <machine/../linux/linux_proto.h>
67 #endif
68 #include <compat/linux/linux_util.h>
69
70 #ifndef __alpha__
71 int
72 linux_creat(struct thread *td, struct linux_creat_args *args)
73 {
74     char *path;
75     int error;
76
77     LCONVPATHEXIST(td, args->path, &path);
78
79 #ifdef DEBUG
80         if (ldebug(creat))
81                 printf(ARGS(creat, "%s, %d"), path, args->mode);
82 #endif
83     error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
84         args->mode);
85     LFREEPATH(path);
86     return (error);
87 }
88 #endif /*!__alpha__*/
89
90 int
91 linux_open(struct thread *td, struct linux_open_args *args)
92 {
93     struct proc *p = td->td_proc;
94     char *path;
95     int bsd_flags, error;
96
97     if (args->flags & LINUX_O_CREAT)
98         LCONVPATHCREAT(td, args->path, &path);
99     else
100         LCONVPATHEXIST(td, args->path, &path);
101
102 #ifdef DEBUG
103         if (ldebug(open))
104                 printf(ARGS(open, "%s, 0x%x, 0x%x"),
105                     path, args->flags, args->mode);
106 #endif
107     bsd_flags = 0;
108     if (args->flags & LINUX_O_RDONLY)
109         bsd_flags |= O_RDONLY;
110     if (args->flags & LINUX_O_WRONLY)
111         bsd_flags |= O_WRONLY;
112     if (args->flags & LINUX_O_RDWR)
113         bsd_flags |= O_RDWR;
114     if (args->flags & LINUX_O_NDELAY)
115         bsd_flags |= O_NONBLOCK;
116     if (args->flags & LINUX_O_APPEND)
117         bsd_flags |= O_APPEND;
118     if (args->flags & LINUX_O_SYNC)
119         bsd_flags |= O_FSYNC;
120     if (args->flags & LINUX_O_NONBLOCK)
121         bsd_flags |= O_NONBLOCK;
122     if (args->flags & LINUX_FASYNC)
123         bsd_flags |= O_ASYNC;
124     if (args->flags & LINUX_O_CREAT)
125         bsd_flags |= O_CREAT;
126     if (args->flags & LINUX_O_TRUNC)
127         bsd_flags |= O_TRUNC;
128     if (args->flags & LINUX_O_EXCL)
129         bsd_flags |= O_EXCL;
130     if (args->flags & LINUX_O_NOCTTY)
131         bsd_flags |= O_NOCTTY;
132
133     error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, args->mode);
134     PROC_LOCK(p);
135     if (!error && !(bsd_flags & O_NOCTTY) &&
136         SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
137         struct file *fp;
138
139         PROC_UNLOCK(p);
140         error = fget(td, td->td_retval[0], &fp);
141         if (!error) {
142                 if (fp->f_type == DTYPE_VNODE)
143                         fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
144                             td);
145             fdrop(fp, td);
146         }
147     } else {
148         PROC_UNLOCK(p);
149 #ifdef DEBUG
150         if (ldebug(open))
151                 printf(LMSG("open returns error %d"), error);
152 #endif
153     }
154     LFREEPATH(path);
155     return error;
156 }
157
158 int
159 linux_lseek(struct thread *td, struct linux_lseek_args *args)
160 {
161
162     struct lseek_args /* {
163         int fd;
164         int pad;
165         off_t offset;
166         int whence;
167     } */ tmp_args;
168     int error;
169
170 #ifdef DEBUG
171         if (ldebug(lseek))
172                 printf(ARGS(lseek, "%d, %ld, %d"),
173                     args->fdes, (long)args->off, args->whence);
174 #endif
175     tmp_args.fd = args->fdes;
176     tmp_args.offset = (off_t)args->off;
177     tmp_args.whence = args->whence;
178     error = lseek(td, &tmp_args);
179     return error;
180 }
181
182 #ifndef __alpha__
183 int
184 linux_llseek(struct thread *td, struct linux_llseek_args *args)
185 {
186         struct lseek_args bsd_args;
187         int error;
188         off_t off;
189
190 #ifdef DEBUG
191         if (ldebug(llseek))
192                 printf(ARGS(llseek, "%d, %d:%d, %d"),
193                     args->fd, args->ohigh, args->olow, args->whence);
194 #endif
195         off = (args->olow) | (((off_t) args->ohigh) << 32);
196
197         bsd_args.fd = args->fd;
198         bsd_args.offset = off;
199         bsd_args.whence = args->whence;
200
201         if ((error = lseek(td, &bsd_args)))
202                 return error;
203
204         if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
205                 return error;
206
207         td->td_retval[0] = 0;
208         return 0;
209 }
210 #endif /*!__alpha__*/
211
212 #ifndef __alpha__
213 int
214 linux_readdir(struct thread *td, struct linux_readdir_args *args)
215 {
216         struct linux_getdents_args lda;
217
218         lda.fd = args->fd;
219         lda.dent = args->dent;
220         lda.count = 1;
221         return linux_getdents(td, &lda);
222 }
223 #endif /*!__alpha__*/
224
225 /*
226  * Note that linux_getdents(2) and linux_getdents64(2) have the same
227  * arguments. They only differ in the definition of struct dirent they
228  * operate on. We use this to common the code, with the exception of
229  * accessing struct dirent. Note that linux_readdir(2) is implemented
230  * by means of linux_getdents(2). In this case we never operate on
231  * struct dirent64 and thus don't need to handle it...
232  */
233
234 struct l_dirent {
235         l_long          d_ino;
236         l_off_t         d_off;
237         l_ushort        d_reclen;
238         char            d_name[LINUX_NAME_MAX + 1];
239 };
240
241 struct l_dirent64 {
242         uint64_t        d_ino;
243         int64_t         d_off;
244         l_ushort        d_reclen;
245         u_char          d_type;
246         char            d_name[LINUX_NAME_MAX + 1];
247 };
248
249 #define LINUX_RECLEN(de,namlen) \
250     ALIGN((((char *)&(de)->d_name - (char *)de) + (namlen) + 1))
251
252 #define LINUX_DIRBLKSIZ         512
253
254 static int
255 getdents_common(struct thread *td, struct linux_getdents64_args *args,
256     int is64bit)
257 {
258         struct dirent *bdp;
259         struct vnode *vp;
260         caddr_t inp, buf;               /* BSD-format */
261         int len, reclen;                /* BSD-format */
262         caddr_t outp;                   /* Linux-format */
263         int resid, linuxreclen=0;       /* Linux-format */
264         struct file *fp;
265         struct uio auio;
266         struct iovec aiov;
267         off_t off;
268         struct l_dirent linux_dirent;
269         struct l_dirent64 linux_dirent64;
270         int buflen, error, eofflag, nbytes, justone;
271         u_long *cookies = NULL, *cookiep;
272         int ncookies, vfslocked;
273
274         nbytes = args->count;
275         if (nbytes == 1) {
276                 /* readdir(2) case. Always struct dirent. */
277                 if (is64bit)
278                         return (EINVAL);
279                 nbytes = sizeof(linux_dirent);
280                 justone = 1;
281         } else
282                 justone = 0;
283
284         if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
285                 return (error);
286
287         if ((fp->f_flag & FREAD) == 0) {
288                 fdrop(fp, td);
289                 return (EBADF);
290         }
291
292         vp = fp->f_vnode;
293         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
294         if (vp->v_type != VDIR) {
295                 VFS_UNLOCK_GIANT(vfslocked);
296                 fdrop(fp, td);
297                 return (EINVAL);
298         }
299
300         off = fp->f_offset;
301
302         buflen = max(LINUX_DIRBLKSIZ, nbytes);
303         buflen = min(buflen, MAXBSIZE);
304         buf = malloc(buflen, M_TEMP, M_WAITOK);
305         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
306
307 again:
308         aiov.iov_base = buf;
309         aiov.iov_len = buflen;
310         auio.uio_iov = &aiov;
311         auio.uio_iovcnt = 1;
312         auio.uio_rw = UIO_READ;
313         auio.uio_segflg = UIO_SYSSPACE;
314         auio.uio_td = td;
315         auio.uio_resid = buflen;
316         auio.uio_offset = off;
317
318         if (cookies) {
319                 free(cookies, M_TEMP);
320                 cookies = NULL;
321         }
322
323 #ifdef MAC
324         /*
325          * Do directory search MAC check using non-cached credentials.
326          */
327         if ((error = mac_check_vnode_readdir(td->td_ucred, vp)))
328                 goto out;
329 #endif /* MAC */
330         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
331                  &cookies)))
332                 goto out;
333
334         inp = buf;
335         outp = (caddr_t)args->dirent;
336         resid = nbytes;
337         if ((len = buflen - auio.uio_resid) <= 0)
338                 goto eof;
339
340         cookiep = cookies;
341
342         if (cookies) {
343                 /*
344                  * When using cookies, the vfs has the option of reading from
345                  * a different offset than that supplied (UFS truncates the
346                  * offset to a block boundary to make sure that it never reads
347                  * partway through a directory entry, even if the directory
348                  * has been compacted).
349                  */
350                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
351                         bdp = (struct dirent *) inp;
352                         len -= bdp->d_reclen;
353                         inp += bdp->d_reclen;
354                         cookiep++;
355                         ncookies--;
356                 }
357         }
358
359         while (len > 0) {
360                 if (cookiep && ncookies == 0)
361                         break;
362                 bdp = (struct dirent *) inp;
363                 reclen = bdp->d_reclen;
364                 if (reclen & 3) {
365                         error = EFAULT;
366                         goto out;
367                 }
368
369                 if (bdp->d_fileno == 0) {
370                         inp += reclen;
371                         if (cookiep) {
372                                 off = *cookiep++;
373                                 ncookies--;
374                         } else
375                                 off += reclen;
376
377                         len -= reclen;
378                         continue;
379                 }
380
381                 linuxreclen = (is64bit)
382                     ? LINUX_RECLEN(&linux_dirent64, bdp->d_namlen)
383                     : LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
384
385                 if (reclen > len || resid < linuxreclen) {
386                         outp++;
387                         break;
388                 }
389
390                 if (justone) {
391                         /* readdir(2) case. */
392                         linux_dirent.d_ino = (l_long)bdp->d_fileno;
393                         linux_dirent.d_off = (l_off_t)linuxreclen;
394                         linux_dirent.d_reclen = (l_ushort)bdp->d_namlen;
395                         strcpy(linux_dirent.d_name, bdp->d_name);
396                         error = copyout(&linux_dirent, outp, linuxreclen);
397                 } else {
398                         if (is64bit) {
399                                 linux_dirent64.d_ino = bdp->d_fileno;
400                                 linux_dirent64.d_off = (cookiep)
401                                     ? (l_off_t)*cookiep
402                                     : (l_off_t)(off + reclen);
403                                 linux_dirent64.d_reclen =
404                                     (l_ushort)linuxreclen;
405                                 linux_dirent64.d_type = bdp->d_type;
406                                 strcpy(linux_dirent64.d_name, bdp->d_name);
407                                 error = copyout(&linux_dirent64, outp,
408                                     linuxreclen);
409                         } else {
410                                 linux_dirent.d_ino = bdp->d_fileno;
411                                 linux_dirent.d_off = (cookiep)
412                                     ? (l_off_t)*cookiep
413                                     : (l_off_t)(off + reclen);
414                                 linux_dirent.d_reclen = (l_ushort)linuxreclen;
415                                 strcpy(linux_dirent.d_name, bdp->d_name);
416                                 error = copyout(&linux_dirent, outp,
417                                     linuxreclen);
418                         }
419                 }
420                 if (error)
421                         goto out;
422
423                 inp += reclen;
424                 if (cookiep) {
425                         off = *cookiep++;
426                         ncookies--;
427                 } else
428                         off += reclen;
429
430                 outp += linuxreclen;
431                 resid -= linuxreclen;
432                 len -= reclen;
433                 if (justone)
434                         break;
435         }
436
437         if (outp == (caddr_t)args->dirent)
438                 goto again;
439
440         fp->f_offset = off;
441         if (justone)
442                 nbytes = resid + linuxreclen;
443
444 eof:
445         td->td_retval[0] = nbytes - resid;
446
447 out:
448         if (cookies)
449                 free(cookies, M_TEMP);
450
451         VOP_UNLOCK(vp, 0, td);
452         VFS_UNLOCK_GIANT(vfslocked);
453         fdrop(fp, td);
454         free(buf, M_TEMP);
455         return (error);
456 }
457
458 int
459 linux_getdents(struct thread *td, struct linux_getdents_args *args)
460 {
461
462 #ifdef DEBUG
463         if (ldebug(getdents))
464                 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
465 #endif
466
467         return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
468 }
469
470 int
471 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
472 {
473
474 #ifdef DEBUG
475         if (ldebug(getdents64))
476                 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
477 #endif
478
479         return (getdents_common(td, args, 1));
480 }
481
482 /*
483  * These exist mainly for hooks for doing /compat/linux translation.
484  */
485
486 int
487 linux_access(struct thread *td, struct linux_access_args *args)
488 {
489         char *path;
490         int error;
491
492         /* linux convention */
493         if (args->flags & ~(F_OK | X_OK | W_OK | R_OK))
494                 return (EINVAL);
495
496         LCONVPATHEXIST(td, args->path, &path);
497
498 #ifdef DEBUG
499         if (ldebug(access))
500                 printf(ARGS(access, "%s, %d"), path, args->flags);
501 #endif
502         error = kern_access(td, path, UIO_SYSSPACE, args->flags);
503         LFREEPATH(path);
504
505         return (error);
506 }
507
508 int
509 linux_unlink(struct thread *td, struct linux_unlink_args *args)
510 {
511         char *path;
512         int error;
513         struct stat st;
514
515         LCONVPATHEXIST(td, args->path, &path);
516
517 #ifdef DEBUG
518         if (ldebug(unlink))
519                 printf(ARGS(unlink, "%s"), path);
520 #endif
521
522         error = kern_unlink(td, path, UIO_SYSSPACE);
523         if (error == EPERM)
524                 /* Introduce POSIX noncompliant behaviour of Linux */
525                 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
526                         if (S_ISDIR(st.st_mode))
527                                 error = EISDIR;
528         LFREEPATH(path);
529         return (error);
530 }
531
532 int
533 linux_chdir(struct thread *td, struct linux_chdir_args *args)
534 {
535         char *path;
536         int error;
537
538         LCONVPATHEXIST(td, args->path, &path);
539
540 #ifdef DEBUG
541         if (ldebug(chdir))
542                 printf(ARGS(chdir, "%s"), path);
543 #endif
544         error = kern_chdir(td, path, UIO_SYSSPACE);
545         LFREEPATH(path);
546         return (error);
547 }
548
549 int
550 linux_chmod(struct thread *td, struct linux_chmod_args *args)
551 {
552         char *path;
553         int error;
554
555         LCONVPATHEXIST(td, args->path, &path);
556
557 #ifdef DEBUG
558         if (ldebug(chmod))
559                 printf(ARGS(chmod, "%s, %d"), path, args->mode);
560 #endif
561         error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
562         LFREEPATH(path);
563         return (error);
564 }
565
566 int
567 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
568 {
569         char *path;
570         int error;
571
572         LCONVPATHCREAT(td, args->path, &path);
573
574 #ifdef DEBUG
575         if (ldebug(mkdir))
576                 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
577 #endif
578         error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
579         LFREEPATH(path);
580         return (error);
581 }
582
583 int
584 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
585 {
586         char *path;
587         int error;
588
589         LCONVPATHEXIST(td, args->path, &path);
590
591 #ifdef DEBUG
592         if (ldebug(rmdir))
593                 printf(ARGS(rmdir, "%s"), path);
594 #endif
595         error = kern_rmdir(td, path, UIO_SYSSPACE);
596         LFREEPATH(path);
597         return (error);
598 }
599
600 int
601 linux_rename(struct thread *td, struct linux_rename_args *args)
602 {
603         char *from, *to;
604         int error;
605
606         LCONVPATHEXIST(td, args->from, &from);
607         /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
608         error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
609         if (to == NULL) {
610                 LFREEPATH(from);
611                 return (error);
612         }
613
614 #ifdef DEBUG
615         if (ldebug(rename))
616                 printf(ARGS(rename, "%s, %s"), from, to);
617 #endif
618         error = kern_rename(td, from, to, UIO_SYSSPACE);
619         LFREEPATH(from);
620         LFREEPATH(to);
621         return (error);
622 }
623
624 int
625 linux_symlink(struct thread *td, struct linux_symlink_args *args)
626 {
627         char *path, *to;
628         int error;
629
630         LCONVPATHEXIST(td, args->path, &path);
631         /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
632         error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
633         if (to == NULL) {
634                 LFREEPATH(path);
635                 return (error);
636         }
637
638 #ifdef DEBUG
639         if (ldebug(symlink))
640                 printf(ARGS(symlink, "%s, %s"), path, to);
641 #endif
642         error = kern_symlink(td, path, to, UIO_SYSSPACE);
643         LFREEPATH(path);
644         LFREEPATH(to);
645         return (error);
646 }
647
648 int
649 linux_readlink(struct thread *td, struct linux_readlink_args *args)
650 {
651         char *name;
652         int error;
653
654         LCONVPATHEXIST(td, args->name, &name);
655
656 #ifdef DEBUG
657         if (ldebug(readlink))
658                 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
659                     args->count);
660 #endif
661         error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
662             args->count);
663         LFREEPATH(name);
664         return (error);
665 }
666
667 int
668 linux_truncate(struct thread *td, struct linux_truncate_args *args)
669 {
670         char *path;
671         int error;
672
673         LCONVPATHEXIST(td, args->path, &path);
674
675 #ifdef DEBUG
676         if (ldebug(truncate))
677                 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
678 #endif
679
680         error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
681         LFREEPATH(path);
682         return (error);
683 }
684
685 int
686 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
687 {
688         struct ftruncate_args /* {
689                 int fd;
690                 int pad;
691                 off_t length;
692                 } */ nuap;
693            
694         nuap.fd = args->fd;
695         nuap.pad = 0;
696         nuap.length = args->length;
697         return (ftruncate(td, &nuap));
698 }
699
700 int
701 linux_link(struct thread *td, struct linux_link_args *args)
702 {
703         char *path, *to;
704         int error;
705
706         LCONVPATHEXIST(td, args->path, &path);
707         /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
708         error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
709         if (to == NULL) {
710                 LFREEPATH(path);
711                 return (error);
712         }
713
714 #ifdef DEBUG
715         if (ldebug(link))
716                 printf(ARGS(link, "%s, %s"), path, to);
717 #endif
718         error = kern_link(td, path, to, UIO_SYSSPACE);
719         LFREEPATH(path);
720         LFREEPATH(to);
721         return (error);
722 }
723
724 #ifndef __alpha__
725 int
726 linux_fdatasync(td, uap)
727         struct thread *td;
728         struct linux_fdatasync_args *uap;
729 {
730         struct fsync_args bsd;
731
732         bsd.fd = uap->fd;
733         return fsync(td, &bsd);
734 }
735 #endif /*!__alpha__*/
736
737 int
738 linux_pread(td, uap)
739         struct thread *td;
740         struct linux_pread_args *uap;
741 {
742         struct pread_args bsd;
743         struct vnode *vp;
744         int error;
745
746         bsd.fd = uap->fd;
747         bsd.buf = uap->buf;
748         bsd.nbyte = uap->nbyte;
749         bsd.offset = uap->offset;
750
751         error = pread(td, &bsd);
752
753         if (error == 0) {
754                 /* This seems to violate POSIX but linux does it */
755                 if ((error = fgetvp(td, uap->fd, &vp)) != 0)
756                         return (error);
757                 if (vp->v_type == VDIR) {
758                         vrele(vp);
759                         return (EISDIR);
760                 }
761                 vrele(vp);
762         }
763
764         return (error);
765 }
766
767 int
768 linux_pwrite(td, uap)
769         struct thread *td;
770         struct linux_pwrite_args *uap;
771 {
772         struct pwrite_args bsd;
773
774         bsd.fd = uap->fd;
775         bsd.buf = uap->buf;
776         bsd.nbyte = uap->nbyte;
777         bsd.offset = uap->offset;
778         return pwrite(td, &bsd);
779 }
780
781 int
782 linux_mount(struct thread *td, struct linux_mount_args *args)
783 {
784         struct ufs_args ufs;
785         char fstypename[MFSNAMELEN];
786         char mntonname[MNAMELEN], mntfromname[MNAMELEN];
787         int error;
788         int fsflags;
789         void *fsdata;
790
791         error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
792             NULL);
793         if (error)
794                 return (error);
795         error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
796         if (error)
797                 return (error);
798         error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
799         if (error)
800                 return (error);
801
802 #ifdef DEBUG
803         if (ldebug(mount))
804                 printf(ARGS(mount, "%s, %s, %s"),
805                     fstypename, mntfromname, mntonname);
806 #endif
807
808         if (strcmp(fstypename, "ext2") == 0) {
809                 strcpy(fstypename, "ext2fs");
810                 fsdata = &ufs;
811                 ufs.fspec = mntfromname;
812 #define DEFAULT_ROOTID          -2
813                 ufs.export.ex_root = DEFAULT_ROOTID;
814                 ufs.export.ex_flags =
815                     args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
816         } else if (strcmp(fstypename, "proc") == 0) {
817                 strcpy(fstypename, "linprocfs");
818                 fsdata = NULL;
819         } else {
820                 return (ENODEV);
821         }
822
823         fsflags = 0;
824
825         if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
826                 /*
827                  * Linux SYNC flag is not included; the closest equivalent
828                  * FreeBSD has is !ASYNC, which is our default.
829                  */
830                 if (args->rwflag & LINUX_MS_RDONLY)
831                         fsflags |= MNT_RDONLY;
832                 if (args->rwflag & LINUX_MS_NOSUID)
833                         fsflags |= MNT_NOSUID;
834                 if (args->rwflag & LINUX_MS_NOEXEC)
835                         fsflags |= MNT_NOEXEC;
836                 if (args->rwflag & LINUX_MS_REMOUNT)
837                         fsflags |= MNT_UPDATE;
838         }
839
840         if (strcmp(fstypename, "linprocfs") == 0) {
841                 error = kernel_vmount(fsflags,
842                         "fstype", fstypename,
843                         "fspath", mntonname,
844                         NULL);
845         } else
846                 error = EOPNOTSUPP;
847         return (error);
848 }
849
850 int
851 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
852 {
853         struct linux_umount_args args2;
854
855         args2.path = args->path;
856         args2.flags = 0;
857         return (linux_umount(td, &args2));
858 }
859
860 int
861 linux_umount(struct thread *td, struct linux_umount_args *args)
862 {
863         struct unmount_args bsd;
864
865         bsd.path = args->path;
866         bsd.flags = args->flags;        /* XXX correct? */
867         return (unmount(td, &bsd));
868 }
869
870 /*
871  * fcntl family of syscalls
872  */
873
874 struct l_flock {
875         l_short         l_type;
876         l_short         l_whence;
877         l_off_t         l_start;
878         l_off_t         l_len;
879         l_pid_t         l_pid;
880 }
881 #if defined(__amd64__) && defined(COMPAT_LINUX32)
882 __packed
883 #endif
884 ;
885
886 static void
887 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
888 {
889         switch (linux_flock->l_type) {
890         case LINUX_F_RDLCK:
891                 bsd_flock->l_type = F_RDLCK;
892                 break;
893         case LINUX_F_WRLCK:
894                 bsd_flock->l_type = F_WRLCK;
895                 break;
896         case LINUX_F_UNLCK:
897                 bsd_flock->l_type = F_UNLCK;
898                 break;
899         default:
900                 bsd_flock->l_type = -1;
901                 break;
902         }
903         bsd_flock->l_whence = linux_flock->l_whence;
904         bsd_flock->l_start = (off_t)linux_flock->l_start;
905         bsd_flock->l_len = (off_t)linux_flock->l_len;
906         bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
907 }
908
909 static void
910 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
911 {
912         switch (bsd_flock->l_type) {
913         case F_RDLCK:
914                 linux_flock->l_type = LINUX_F_RDLCK;
915                 break;
916         case F_WRLCK:
917                 linux_flock->l_type = LINUX_F_WRLCK;
918                 break;
919         case F_UNLCK:
920                 linux_flock->l_type = LINUX_F_UNLCK;
921                 break;
922         }
923         linux_flock->l_whence = bsd_flock->l_whence;
924         linux_flock->l_start = (l_off_t)bsd_flock->l_start;
925         linux_flock->l_len = (l_off_t)bsd_flock->l_len;
926         linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
927 }
928
929 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
930 struct l_flock64 {
931         l_short         l_type;
932         l_short         l_whence;
933         l_loff_t        l_start;
934         l_loff_t        l_len;
935         l_pid_t         l_pid;
936 }
937 #if defined(__amd64__) && defined(COMPAT_LINUX32)
938 __packed
939 #endif
940 ;
941
942 static void
943 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
944 {
945         switch (linux_flock->l_type) {
946         case LINUX_F_RDLCK:
947                 bsd_flock->l_type = F_RDLCK;
948                 break;
949         case LINUX_F_WRLCK:
950                 bsd_flock->l_type = F_WRLCK;
951                 break;
952         case LINUX_F_UNLCK:
953                 bsd_flock->l_type = F_UNLCK;
954                 break;
955         default:
956                 bsd_flock->l_type = -1;
957                 break;
958         }
959         bsd_flock->l_whence = linux_flock->l_whence;
960         bsd_flock->l_start = (off_t)linux_flock->l_start;
961         bsd_flock->l_len = (off_t)linux_flock->l_len;
962         bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
963         bsd_flock->l_sysid = 0;
964 }
965
966 static void
967 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
968 {
969         switch (bsd_flock->l_type) {
970         case F_RDLCK:
971                 linux_flock->l_type = LINUX_F_RDLCK;
972                 break;
973         case F_WRLCK:
974                 linux_flock->l_type = LINUX_F_WRLCK;
975                 break;
976         case F_UNLCK:
977                 linux_flock->l_type = LINUX_F_UNLCK;
978                 break;
979         }
980         linux_flock->l_whence = bsd_flock->l_whence;
981         linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
982         linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
983         linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
984 }
985 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
986
987 #if defined(__alpha__)
988 #define linux_fcntl64_args      linux_fcntl_args
989 #endif
990
991 static int
992 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
993 {
994         struct l_flock linux_flock;
995         struct flock bsd_flock;
996         struct file *fp;
997         long arg;
998         int error, result;
999
1000         switch (args->cmd) {
1001         case LINUX_F_DUPFD:
1002                 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1003
1004         case LINUX_F_GETFD:
1005                 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1006
1007         case LINUX_F_SETFD:
1008                 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1009
1010         case LINUX_F_GETFL:
1011                 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1012                 result = td->td_retval[0];
1013                 td->td_retval[0] = 0;
1014                 if (result & O_RDONLY)
1015                         td->td_retval[0] |= LINUX_O_RDONLY;
1016                 if (result & O_WRONLY)
1017                         td->td_retval[0] |= LINUX_O_WRONLY;
1018                 if (result & O_RDWR)
1019                         td->td_retval[0] |= LINUX_O_RDWR;
1020                 if (result & O_NDELAY)
1021                         td->td_retval[0] |= LINUX_O_NONBLOCK;
1022                 if (result & O_APPEND)
1023                         td->td_retval[0] |= LINUX_O_APPEND;
1024                 if (result & O_FSYNC)
1025                         td->td_retval[0] |= LINUX_O_SYNC;
1026                 if (result & O_ASYNC)
1027                         td->td_retval[0] |= LINUX_FASYNC;
1028 #ifdef LINUX_O_NOFOLLOW
1029                 if (result & O_NOFOLLOW)
1030                         td->td_retval[0] |= LINUX_O_NOFOLLOW;
1031 #endif
1032 #ifdef LINUX_O_DIRECT
1033                 if (result & O_DIRECT)
1034                         td->td_retval[0] |= LINUX_O_DIRECT;
1035 #endif
1036                 return (error);
1037
1038         case LINUX_F_SETFL:
1039                 arg = 0;
1040                 if (args->arg & LINUX_O_NDELAY)
1041                         arg |= O_NONBLOCK;
1042                 if (args->arg & LINUX_O_APPEND)
1043                         arg |= O_APPEND;
1044                 if (args->arg & LINUX_O_SYNC)
1045                         arg |= O_FSYNC;
1046                 if (args->arg & LINUX_FASYNC)
1047                         arg |= O_ASYNC;
1048 #ifdef LINUX_O_NOFOLLOW
1049                 if (args->arg & LINUX_O_NOFOLLOW)
1050                         arg |= O_NOFOLLOW;
1051 #endif
1052 #ifdef LINUX_O_DIRECT
1053                 if (args->arg & LINUX_O_DIRECT)
1054                         arg |= O_DIRECT;
1055 #endif
1056                 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1057
1058         case LINUX_F_GETLK:
1059                 error = copyin((void *)args->arg, &linux_flock,
1060                     sizeof(linux_flock));
1061                 if (error)
1062                         return (error);
1063                 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1064                 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1065                 if (error)
1066                         return (error);
1067                 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1068                 return (copyout(&linux_flock, (void *)args->arg,
1069                     sizeof(linux_flock)));
1070
1071         case LINUX_F_SETLK:
1072                 error = copyin((void *)args->arg, &linux_flock,
1073                     sizeof(linux_flock));
1074                 if (error)
1075                         return (error);
1076                 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1077                 return (kern_fcntl(td, args->fd, F_SETLK,
1078                     (intptr_t)&bsd_flock));
1079
1080         case LINUX_F_SETLKW:
1081                 error = copyin((void *)args->arg, &linux_flock,
1082                     sizeof(linux_flock));
1083                 if (error)
1084                         return (error);
1085                 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1086                 return (kern_fcntl(td, args->fd, F_SETLKW,
1087                      (intptr_t)&bsd_flock));
1088
1089         case LINUX_F_GETOWN:
1090                 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1091
1092         case LINUX_F_SETOWN:
1093                 /*
1094                  * XXX some Linux applications depend on F_SETOWN having no
1095                  * significant effect for pipes (SIGIO is not delivered for
1096                  * pipes under Linux-2.2.35 at least).
1097                  */
1098                 error = fget(td, args->fd, &fp);
1099                 if (error)
1100                         return (error);
1101                 if (fp->f_type == DTYPE_PIPE) {
1102                         fdrop(fp, td);
1103                         return (EINVAL);
1104                 }
1105                 fdrop(fp, td);
1106
1107                 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1108         }
1109
1110         return (EINVAL);
1111 }
1112
1113 int
1114 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1115 {
1116         struct linux_fcntl64_args args64;
1117
1118 #ifdef DEBUG
1119         if (ldebug(fcntl))
1120                 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1121 #endif
1122
1123         args64.fd = args->fd;
1124         args64.cmd = args->cmd;
1125         args64.arg = args->arg;
1126         return (fcntl_common(td, &args64));
1127 }
1128
1129 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1130 int
1131 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1132 {
1133         struct l_flock64 linux_flock;
1134         struct flock bsd_flock;
1135         int error;
1136
1137 #ifdef DEBUG
1138         if (ldebug(fcntl64))
1139                 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1140 #endif
1141
1142         switch (args->cmd) {
1143         case LINUX_F_GETLK64:
1144                 error = copyin((void *)args->arg, &linux_flock,
1145                     sizeof(linux_flock));
1146                 if (error)
1147                         return (error);
1148                 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1149                 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1150                 if (error)
1151                         return (error);
1152                 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1153                 return (copyout(&linux_flock, (void *)args->arg,
1154                             sizeof(linux_flock)));
1155
1156         case LINUX_F_SETLK64:
1157                 error = copyin((void *)args->arg, &linux_flock,
1158                     sizeof(linux_flock));
1159                 if (error)
1160                         return (error);
1161                 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1162                 return (kern_fcntl(td, args->fd, F_SETLK,
1163                     (intptr_t)&bsd_flock));
1164
1165         case LINUX_F_SETLKW64:
1166                 error = copyin((void *)args->arg, &linux_flock,
1167                     sizeof(linux_flock));
1168                 if (error)
1169                         return (error);
1170                 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1171                 return (kern_fcntl(td, args->fd, F_SETLKW,
1172                     (intptr_t)&bsd_flock));
1173         }
1174
1175         return (fcntl_common(td, args));
1176 }
1177 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1178
1179 int
1180 linux_chown(struct thread *td, struct linux_chown_args *args)
1181 {
1182         char *path;
1183         int error;
1184
1185         LCONVPATHEXIST(td, args->path, &path);
1186
1187 #ifdef DEBUG
1188         if (ldebug(chown))
1189                 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1190 #endif
1191         error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1192         LFREEPATH(path);
1193         return (error);
1194 }
1195
1196 int
1197 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1198 {
1199         char *path;
1200         int error;
1201
1202         LCONVPATHEXIST(td, args->path, &path);
1203
1204 #ifdef DEBUG
1205         if (ldebug(lchown))
1206                 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1207 #endif
1208         error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1209         LFREEPATH(path);
1210         return (error);
1211 }