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