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