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