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