linux_file.c revision 298653
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: stable/10/sys/compat/linux/linux_file.c 298653 2016-04-26 17:39:54Z pfg $"); 31 32#include "opt_compat.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/capsicum.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/malloc.h> 44#include <sys/mount.h> 45#include <sys/mutex.h> 46#include <sys/namei.h> 47#include <sys/proc.h> 48#include <sys/stat.h> 49#include <sys/sx.h> 50#include <sys/syscallsubr.h> 51#include <sys/sysproto.h> 52#include <sys/tty.h> 53#include <sys/unistd.h> 54#include <sys/vnode.h> 55 56#include <security/mac/mac_framework.h> 57 58#ifdef COMPAT_LINUX32 59#include <machine/../linux32/linux.h> 60#include <machine/../linux32/linux32_proto.h> 61#else 62#include <machine/../linux/linux.h> 63#include <machine/../linux/linux_proto.h> 64#endif 65#include <compat/linux/linux_misc.h> 66#include <compat/linux/linux_util.h> 67#include <compat/linux/linux_file.h> 68 69int 70linux_creat(struct thread *td, struct linux_creat_args *args) 71{ 72 char *path; 73 int error; 74 75 LCONVPATHEXIST(td, args->path, &path); 76 77#ifdef DEBUG 78 if (ldebug(creat)) 79 printf(ARGS(creat, "%s, %d"), path, args->mode); 80#endif 81 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC, 82 args->mode); 83 LFREEPATH(path); 84 return (error); 85} 86 87 88static int 89linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode) 90{ 91 cap_rights_t rights; 92 struct proc *p = td->td_proc; 93 struct file *fp; 94 int fd; 95 int bsd_flags, error; 96 97 bsd_flags = 0; 98 switch (l_flags & LINUX_O_ACCMODE) { 99 case LINUX_O_WRONLY: 100 bsd_flags |= O_WRONLY; 101 break; 102 case LINUX_O_RDWR: 103 bsd_flags |= O_RDWR; 104 break; 105 default: 106 bsd_flags |= O_RDONLY; 107 } 108 if (l_flags & LINUX_O_NDELAY) 109 bsd_flags |= O_NONBLOCK; 110 if (l_flags & LINUX_O_APPEND) 111 bsd_flags |= O_APPEND; 112 if (l_flags & LINUX_O_SYNC) 113 bsd_flags |= O_FSYNC; 114 if (l_flags & LINUX_O_NONBLOCK) 115 bsd_flags |= O_NONBLOCK; 116 if (l_flags & LINUX_FASYNC) 117 bsd_flags |= O_ASYNC; 118 if (l_flags & LINUX_O_CREAT) 119 bsd_flags |= O_CREAT; 120 if (l_flags & LINUX_O_TRUNC) 121 bsd_flags |= O_TRUNC; 122 if (l_flags & LINUX_O_EXCL) 123 bsd_flags |= O_EXCL; 124 if (l_flags & LINUX_O_NOCTTY) 125 bsd_flags |= O_NOCTTY; 126 if (l_flags & LINUX_O_DIRECT) 127 bsd_flags |= O_DIRECT; 128 if (l_flags & LINUX_O_NOFOLLOW) 129 bsd_flags |= O_NOFOLLOW; 130 if (l_flags & LINUX_O_DIRECTORY) 131 bsd_flags |= O_DIRECTORY; 132 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */ 133 134 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode); 135 if (error != 0) 136 goto done; 137 138 if (bsd_flags & O_NOCTTY) 139 goto done; 140 141 /* 142 * XXX In between kern_open() and fget(), another process 143 * having the same filedesc could use that fd without 144 * checking below. 145 */ 146 fd = td->td_retval[0]; 147 if (fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp) == 0) { 148 if (fp->f_type != DTYPE_VNODE) { 149 fdrop(fp, td); 150 goto done; 151 } 152 sx_slock(&proctree_lock); 153 PROC_LOCK(p); 154 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 155 PROC_UNLOCK(p); 156 sx_sunlock(&proctree_lock); 157 /* XXXPJD: Verify if TIOCSCTTY is allowed. */ 158 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, 159 td->td_ucred, td); 160 } else { 161 PROC_UNLOCK(p); 162 sx_sunlock(&proctree_lock); 163 } 164 fdrop(fp, td); 165 } 166 167done: 168#ifdef DEBUG 169 if (ldebug(open)) 170 printf(LMSG("open returns error %d"), error); 171#endif 172 LFREEPATH(path); 173 return (error); 174} 175 176int 177linux_openat(struct thread *td, struct linux_openat_args *args) 178{ 179 char *path; 180 int dfd; 181 182 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 183 if (args->flags & LINUX_O_CREAT) 184 LCONVPATH_AT(td, args->filename, &path, 1, dfd); 185 else 186 LCONVPATH_AT(td, args->filename, &path, 0, dfd); 187#ifdef DEBUG 188 if (ldebug(openat)) 189 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd, 190 path, args->flags, args->mode); 191#endif 192 return (linux_common_open(td, dfd, path, args->flags, args->mode)); 193} 194 195int 196linux_open(struct thread *td, struct linux_open_args *args) 197{ 198 char *path; 199 200 if (args->flags & LINUX_O_CREAT) 201 LCONVPATHCREAT(td, args->path, &path); 202 else 203 LCONVPATHEXIST(td, args->path, &path); 204 205#ifdef DEBUG 206 if (ldebug(open)) 207 printf(ARGS(open, "%s, 0x%x, 0x%x"), 208 path, args->flags, args->mode); 209#endif 210 211 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode)); 212} 213 214int 215linux_lseek(struct thread *td, struct linux_lseek_args *args) 216{ 217 218 struct lseek_args /* { 219 int fd; 220 int pad; 221 off_t offset; 222 int whence; 223 } */ tmp_args; 224 int error; 225 226#ifdef DEBUG 227 if (ldebug(lseek)) 228 printf(ARGS(lseek, "%d, %ld, %d"), 229 args->fdes, (long)args->off, args->whence); 230#endif 231 tmp_args.fd = args->fdes; 232 tmp_args.offset = (off_t)args->off; 233 tmp_args.whence = args->whence; 234 error = sys_lseek(td, &tmp_args); 235 return error; 236} 237 238#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 239int 240linux_llseek(struct thread *td, struct linux_llseek_args *args) 241{ 242 struct lseek_args bsd_args; 243 int error; 244 off_t off; 245 246#ifdef DEBUG 247 if (ldebug(llseek)) 248 printf(ARGS(llseek, "%d, %d:%d, %d"), 249 args->fd, args->ohigh, args->olow, args->whence); 250#endif 251 off = (args->olow) | (((off_t) args->ohigh) << 32); 252 253 bsd_args.fd = args->fd; 254 bsd_args.offset = off; 255 bsd_args.whence = args->whence; 256 257 if ((error = sys_lseek(td, &bsd_args))) 258 return error; 259 260 if ((error = copyout(td->td_retval, args->res, sizeof (off_t)))) 261 return error; 262 263 td->td_retval[0] = 0; 264 return 0; 265} 266 267int 268linux_readdir(struct thread *td, struct linux_readdir_args *args) 269{ 270 struct linux_getdents_args lda; 271 272 lda.fd = args->fd; 273 lda.dent = args->dent; 274 lda.count = 1; 275 return linux_getdents(td, &lda); 276} 277#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 278 279/* 280 * Note that linux_getdents(2) and linux_getdents64(2) have the same 281 * arguments. They only differ in the definition of struct dirent they 282 * operate on. We use this to common the code, with the exception of 283 * accessing struct dirent. Note that linux_readdir(2) is implemented 284 * by means of linux_getdents(2). In this case we never operate on 285 * struct dirent64 and thus don't need to handle it... 286 */ 287 288struct l_dirent { 289 l_ulong d_ino; 290 l_off_t d_off; 291 l_ushort d_reclen; 292 char d_name[LINUX_NAME_MAX + 1]; 293}; 294 295struct l_dirent64 { 296 uint64_t d_ino; 297 int64_t d_off; 298 l_ushort d_reclen; 299 u_char d_type; 300 char d_name[LINUX_NAME_MAX + 1]; 301}; 302 303/* 304 * Linux uses the last byte in the dirent buffer to store d_type, 305 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes. 306 */ 307#define LINUX_RECLEN(namlen) \ 308 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong)) 309 310#define LINUX_RECLEN64(namlen) \ 311 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \ 312 sizeof(uint64_t)) 313 314#define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \ 315 LINUX_RECLEN64(LINUX_NAME_MAX)) 316#define LINUX_DIRBLKSIZ 512 317 318static int 319getdents_common(struct thread *td, struct linux_getdents64_args *args, 320 int is64bit) 321{ 322 struct dirent *bdp; 323 struct vnode *vp; 324 caddr_t inp, buf; /* BSD-format */ 325 int len, reclen; /* BSD-format */ 326 caddr_t outp; /* Linux-format */ 327 int resid, linuxreclen=0; /* Linux-format */ 328 caddr_t lbuf; /* Linux-format */ 329 cap_rights_t rights; 330 struct file *fp; 331 struct uio auio; 332 struct iovec aiov; 333 off_t off; 334 struct l_dirent *linux_dirent; 335 struct l_dirent64 *linux_dirent64; 336 int buflen, error, eofflag, nbytes, justone; 337 u_long *cookies = NULL, *cookiep; 338 int ncookies; 339 340 nbytes = args->count; 341 if (nbytes == 1) { 342 /* readdir(2) case. Always struct dirent. */ 343 if (is64bit) 344 return (EINVAL); 345 nbytes = sizeof(*linux_dirent); 346 justone = 1; 347 } else 348 justone = 0; 349 350 error = getvnode(td->td_proc->p_fd, args->fd, 351 cap_rights_init(&rights, CAP_READ), &fp); 352 if (error != 0) 353 return (error); 354 355 if ((fp->f_flag & FREAD) == 0) { 356 fdrop(fp, td); 357 return (EBADF); 358 } 359 360 off = foffset_lock(fp, 0); 361 vp = fp->f_vnode; 362 if (vp->v_type != VDIR) { 363 foffset_unlock(fp, off, 0); 364 fdrop(fp, td); 365 return (EINVAL); 366 } 367 368 369 buflen = max(LINUX_DIRBLKSIZ, nbytes); 370 buflen = min(buflen, MAXBSIZE); 371 buf = malloc(buflen, M_LINUX, M_WAITOK); 372 lbuf = malloc(LINUX_MAXRECLEN, M_LINUX, M_WAITOK | M_ZERO); 373 vn_lock(vp, LK_SHARED | LK_RETRY); 374 375 aiov.iov_base = buf; 376 aiov.iov_len = buflen; 377 auio.uio_iov = &aiov; 378 auio.uio_iovcnt = 1; 379 auio.uio_rw = UIO_READ; 380 auio.uio_segflg = UIO_SYSSPACE; 381 auio.uio_td = td; 382 auio.uio_resid = buflen; 383 auio.uio_offset = off; 384 385#ifdef MAC 386 /* 387 * Do directory search MAC check using non-cached credentials. 388 */ 389 if ((error = mac_vnode_check_readdir(td->td_ucred, vp))) 390 goto out; 391#endif /* MAC */ 392 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, 393 &cookies))) 394 goto out; 395 396 inp = buf; 397 outp = (caddr_t)args->dirent; 398 resid = nbytes; 399 if ((len = buflen - auio.uio_resid) <= 0) 400 goto eof; 401 402 cookiep = cookies; 403 404 if (cookies) { 405 /* 406 * When using cookies, the vfs has the option of reading from 407 * a different offset than that supplied (UFS truncates the 408 * offset to a block boundary to make sure that it never reads 409 * partway through a directory entry, even if the directory 410 * has been compacted). 411 */ 412 while (len > 0 && ncookies > 0 && *cookiep <= off) { 413 bdp = (struct dirent *) inp; 414 len -= bdp->d_reclen; 415 inp += bdp->d_reclen; 416 cookiep++; 417 ncookies--; 418 } 419 } 420 421 while (len > 0) { 422 if (cookiep && ncookies == 0) 423 break; 424 bdp = (struct dirent *) inp; 425 reclen = bdp->d_reclen; 426 if (reclen & 3) { 427 error = EFAULT; 428 goto out; 429 } 430 431 if (bdp->d_fileno == 0) { 432 inp += reclen; 433 if (cookiep) { 434 off = *cookiep++; 435 ncookies--; 436 } else 437 off += reclen; 438 439 len -= reclen; 440 continue; 441 } 442 443 linuxreclen = (is64bit) 444 ? LINUX_RECLEN64(bdp->d_namlen) 445 : LINUX_RECLEN(bdp->d_namlen); 446 447 if (reclen > len || resid < linuxreclen) { 448 outp++; 449 break; 450 } 451 452 if (justone) { 453 /* readdir(2) case. */ 454 linux_dirent = (struct l_dirent*)lbuf; 455 linux_dirent->d_ino = bdp->d_fileno; 456 linux_dirent->d_off = (l_off_t)linuxreclen; 457 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen; 458 strlcpy(linux_dirent->d_name, bdp->d_name, 459 linuxreclen - offsetof(struct l_dirent, d_name)); 460 error = copyout(linux_dirent, outp, linuxreclen); 461 } 462 if (is64bit) { 463 linux_dirent64 = (struct l_dirent64*)lbuf; 464 linux_dirent64->d_ino = bdp->d_fileno; 465 linux_dirent64->d_off = (cookiep) 466 ? (l_off_t)*cookiep 467 : (l_off_t)(off + reclen); 468 linux_dirent64->d_reclen = (l_ushort)linuxreclen; 469 linux_dirent64->d_type = bdp->d_type; 470 strlcpy(linux_dirent64->d_name, bdp->d_name, 471 linuxreclen - offsetof(struct l_dirent64, d_name)); 472 error = copyout(linux_dirent64, outp, linuxreclen); 473 } else if (!justone) { 474 linux_dirent = (struct l_dirent*)lbuf; 475 linux_dirent->d_ino = bdp->d_fileno; 476 linux_dirent->d_off = (cookiep) 477 ? (l_off_t)*cookiep 478 : (l_off_t)(off + reclen); 479 linux_dirent->d_reclen = (l_ushort)linuxreclen; 480 /* 481 * Copy d_type to last byte of l_dirent buffer 482 */ 483 lbuf[linuxreclen-1] = bdp->d_type; 484 strlcpy(linux_dirent->d_name, bdp->d_name, 485 linuxreclen - offsetof(struct l_dirent, d_name)-1); 486 error = copyout(linux_dirent, outp, linuxreclen); 487 } 488 489 if (error) 490 goto out; 491 492 inp += reclen; 493 if (cookiep) { 494 off = *cookiep++; 495 ncookies--; 496 } else 497 off += reclen; 498 499 outp += linuxreclen; 500 resid -= linuxreclen; 501 len -= reclen; 502 if (justone) 503 break; 504 } 505 506 if (outp == (caddr_t)args->dirent) { 507 nbytes = resid; 508 goto eof; 509 } 510 511 if (justone) 512 nbytes = resid + linuxreclen; 513 514eof: 515 td->td_retval[0] = nbytes - resid; 516 517out: 518 free(cookies, M_TEMP); 519 520 VOP_UNLOCK(vp, 0); 521 foffset_unlock(fp, off, 0); 522 fdrop(fp, td); 523 free(buf, M_LINUX); 524 free(lbuf, M_LINUX); 525 return (error); 526} 527 528int 529linux_getdents(struct thread *td, struct linux_getdents_args *args) 530{ 531 532#ifdef DEBUG 533 if (ldebug(getdents)) 534 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count); 535#endif 536 537 return (getdents_common(td, (struct linux_getdents64_args*)args, 0)); 538} 539 540int 541linux_getdents64(struct thread *td, struct linux_getdents64_args *args) 542{ 543 544#ifdef DEBUG 545 if (ldebug(getdents64)) 546 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count); 547#endif 548 549 return (getdents_common(td, args, 1)); 550} 551 552/* 553 * These exist mainly for hooks for doing /compat/linux translation. 554 */ 555 556int 557linux_access(struct thread *td, struct linux_access_args *args) 558{ 559 char *path; 560 int error; 561 562 /* linux convention */ 563 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 564 return (EINVAL); 565 566 LCONVPATHEXIST(td, args->path, &path); 567 568#ifdef DEBUG 569 if (ldebug(access)) 570 printf(ARGS(access, "%s, %d"), path, args->amode); 571#endif 572 error = kern_access(td, path, UIO_SYSSPACE, args->amode); 573 LFREEPATH(path); 574 575 return (error); 576} 577 578int 579linux_faccessat(struct thread *td, struct linux_faccessat_args *args) 580{ 581 char *path; 582 int error, dfd; 583 584 /* linux convention */ 585 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 586 return (EINVAL); 587 588 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 589 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 590 591#ifdef DEBUG 592 if (ldebug(access)) 593 printf(ARGS(access, "%s, %d"), path, args->amode); 594#endif 595 596 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode); 597 LFREEPATH(path); 598 599 return (error); 600} 601 602int 603linux_unlink(struct thread *td, struct linux_unlink_args *args) 604{ 605 char *path; 606 int error; 607 struct stat st; 608 609 LCONVPATHEXIST(td, args->path, &path); 610 611#ifdef DEBUG 612 if (ldebug(unlink)) 613 printf(ARGS(unlink, "%s"), path); 614#endif 615 616 error = kern_unlink(td, path, UIO_SYSSPACE); 617 if (error == EPERM) 618 /* Introduce POSIX noncompliant behaviour of Linux */ 619 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0) 620 if (S_ISDIR(st.st_mode)) 621 error = EISDIR; 622 LFREEPATH(path); 623 return (error); 624} 625 626int 627linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args) 628{ 629 char *path; 630 int error, dfd; 631 struct stat st; 632 633 if (args->flag & ~LINUX_AT_REMOVEDIR) 634 return (EINVAL); 635 636 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 637 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 638 639#ifdef DEBUG 640 if (ldebug(unlinkat)) 641 printf(ARGS(unlinkat, "%s"), path); 642#endif 643 644 if (args->flag & LINUX_AT_REMOVEDIR) 645 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE); 646 else 647 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0); 648 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) { 649 /* Introduce POSIX noncompliant behaviour of Linux */ 650 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path, 651 UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode)) 652 error = EISDIR; 653 } 654 LFREEPATH(path); 655 return (error); 656} 657int 658linux_chdir(struct thread *td, struct linux_chdir_args *args) 659{ 660 char *path; 661 int error; 662 663 LCONVPATHEXIST(td, args->path, &path); 664 665#ifdef DEBUG 666 if (ldebug(chdir)) 667 printf(ARGS(chdir, "%s"), path); 668#endif 669 error = kern_chdir(td, path, UIO_SYSSPACE); 670 LFREEPATH(path); 671 return (error); 672} 673 674int 675linux_chmod(struct thread *td, struct linux_chmod_args *args) 676{ 677 char *path; 678 int error; 679 680 LCONVPATHEXIST(td, args->path, &path); 681 682#ifdef DEBUG 683 if (ldebug(chmod)) 684 printf(ARGS(chmod, "%s, %d"), path, args->mode); 685#endif 686 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode); 687 LFREEPATH(path); 688 return (error); 689} 690 691int 692linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args) 693{ 694 char *path; 695 int error, dfd; 696 697 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 698 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 699 700#ifdef DEBUG 701 if (ldebug(fchmodat)) 702 printf(ARGS(fchmodat, "%s, %d"), path, args->mode); 703#endif 704 705 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0); 706 LFREEPATH(path); 707 return (error); 708} 709 710int 711linux_mkdir(struct thread *td, struct linux_mkdir_args *args) 712{ 713 char *path; 714 int error; 715 716 LCONVPATHCREAT(td, args->path, &path); 717 718#ifdef DEBUG 719 if (ldebug(mkdir)) 720 printf(ARGS(mkdir, "%s, %d"), path, args->mode); 721#endif 722 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode); 723 LFREEPATH(path); 724 return (error); 725} 726 727int 728linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args) 729{ 730 char *path; 731 int error, dfd; 732 733 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 734 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd); 735 736#ifdef DEBUG 737 if (ldebug(mkdirat)) 738 printf(ARGS(mkdirat, "%s, %d"), path, args->mode); 739#endif 740 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode); 741 LFREEPATH(path); 742 return (error); 743} 744 745int 746linux_rmdir(struct thread *td, struct linux_rmdir_args *args) 747{ 748 char *path; 749 int error; 750 751 LCONVPATHEXIST(td, args->path, &path); 752 753#ifdef DEBUG 754 if (ldebug(rmdir)) 755 printf(ARGS(rmdir, "%s"), path); 756#endif 757 error = kern_rmdir(td, path, UIO_SYSSPACE); 758 LFREEPATH(path); 759 return (error); 760} 761 762int 763linux_rename(struct thread *td, struct linux_rename_args *args) 764{ 765 char *from, *to; 766 int error; 767 768 LCONVPATHEXIST(td, args->from, &from); 769 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 770 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 771 if (to == NULL) { 772 LFREEPATH(from); 773 return (error); 774 } 775 776#ifdef DEBUG 777 if (ldebug(rename)) 778 printf(ARGS(rename, "%s, %s"), from, to); 779#endif 780 error = kern_rename(td, from, to, UIO_SYSSPACE); 781 LFREEPATH(from); 782 LFREEPATH(to); 783 return (error); 784} 785 786int 787linux_renameat(struct thread *td, struct linux_renameat_args *args) 788{ 789 char *from, *to; 790 int error, olddfd, newdfd; 791 792 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 793 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 794 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd); 795 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 796 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 797 if (to == NULL) { 798 LFREEPATH(from); 799 return (error); 800 } 801 802#ifdef DEBUG 803 if (ldebug(renameat)) 804 printf(ARGS(renameat, "%s, %s"), from, to); 805#endif 806 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE); 807 LFREEPATH(from); 808 LFREEPATH(to); 809 return (error); 810} 811 812int 813linux_symlink(struct thread *td, struct linux_symlink_args *args) 814{ 815 char *path, *to; 816 int error; 817 818 LCONVPATHEXIST(td, args->path, &path); 819 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 820 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 821 if (to == NULL) { 822 LFREEPATH(path); 823 return (error); 824 } 825 826#ifdef DEBUG 827 if (ldebug(symlink)) 828 printf(ARGS(symlink, "%s, %s"), path, to); 829#endif 830 error = kern_symlink(td, path, to, UIO_SYSSPACE); 831 LFREEPATH(path); 832 LFREEPATH(to); 833 return (error); 834} 835 836int 837linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args) 838{ 839 char *path, *to; 840 int error, dfd; 841 842 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 843 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd); 844 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 845 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd); 846 if (to == NULL) { 847 LFREEPATH(path); 848 return (error); 849 } 850 851#ifdef DEBUG 852 if (ldebug(symlinkat)) 853 printf(ARGS(symlinkat, "%s, %s"), path, to); 854#endif 855 856 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE); 857 LFREEPATH(path); 858 LFREEPATH(to); 859 return (error); 860} 861 862int 863linux_readlink(struct thread *td, struct linux_readlink_args *args) 864{ 865 char *name; 866 int error; 867 868 LCONVPATHEXIST(td, args->name, &name); 869 870#ifdef DEBUG 871 if (ldebug(readlink)) 872 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf, 873 args->count); 874#endif 875 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE, 876 args->count); 877 LFREEPATH(name); 878 return (error); 879} 880 881int 882linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args) 883{ 884 char *name; 885 int error, dfd; 886 887 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 888 LCONVPATHEXIST_AT(td, args->path, &name, dfd); 889 890#ifdef DEBUG 891 if (ldebug(readlinkat)) 892 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf, 893 args->bufsiz); 894#endif 895 896 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf, 897 UIO_USERSPACE, args->bufsiz); 898 LFREEPATH(name); 899 return (error); 900} 901 902int 903linux_truncate(struct thread *td, struct linux_truncate_args *args) 904{ 905 char *path; 906 int error; 907 908 LCONVPATHEXIST(td, args->path, &path); 909 910#ifdef DEBUG 911 if (ldebug(truncate)) 912 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length); 913#endif 914 915 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 916 LFREEPATH(path); 917 return (error); 918} 919 920#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 921int 922linux_truncate64(struct thread *td, struct linux_truncate64_args *args) 923{ 924 char *path; 925 int error; 926 927 LCONVPATHEXIST(td, args->path, &path); 928 929#ifdef DEBUG 930 if (ldebug(truncate64)) 931 printf(ARGS(truncate64, "%s, %jd"), path, args->length); 932#endif 933 934 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 935 LFREEPATH(path); 936 return (error); 937} 938#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 939 940int 941linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args) 942{ 943 struct ftruncate_args /* { 944 int fd; 945 int pad; 946 off_t length; 947 } */ nuap; 948 949 nuap.fd = args->fd; 950 nuap.length = args->length; 951 return (sys_ftruncate(td, &nuap)); 952} 953 954int 955linux_link(struct thread *td, struct linux_link_args *args) 956{ 957 char *path, *to; 958 int error; 959 960 LCONVPATHEXIST(td, args->path, &path); 961 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 962 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 963 if (to == NULL) { 964 LFREEPATH(path); 965 return (error); 966 } 967 968#ifdef DEBUG 969 if (ldebug(link)) 970 printf(ARGS(link, "%s, %s"), path, to); 971#endif 972 error = kern_link(td, path, to, UIO_SYSSPACE); 973 LFREEPATH(path); 974 LFREEPATH(to); 975 return (error); 976} 977 978int 979linux_linkat(struct thread *td, struct linux_linkat_args *args) 980{ 981 char *path, *to; 982 int error, olddfd, newdfd, follow; 983 984 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW) 985 return (EINVAL); 986 987 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 988 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 989 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd); 990 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 991 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 992 if (to == NULL) { 993 LFREEPATH(path); 994 return (error); 995 } 996 997#ifdef DEBUG 998 if (ldebug(linkat)) 999 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path, 1000 args->newdfd, to, args->flag); 1001#endif 1002 1003 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW : 1004 FOLLOW; 1005 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow); 1006 LFREEPATH(path); 1007 LFREEPATH(to); 1008 return (error); 1009} 1010 1011int 1012linux_fdatasync(td, uap) 1013 struct thread *td; 1014 struct linux_fdatasync_args *uap; 1015{ 1016 struct fsync_args bsd; 1017 1018 bsd.fd = uap->fd; 1019 return sys_fsync(td, &bsd); 1020} 1021 1022int 1023linux_pread(td, uap) 1024 struct thread *td; 1025 struct linux_pread_args *uap; 1026{ 1027 struct pread_args bsd; 1028 cap_rights_t rights; 1029 struct vnode *vp; 1030 int error; 1031 1032 bsd.fd = uap->fd; 1033 bsd.buf = uap->buf; 1034 bsd.nbyte = uap->nbyte; 1035 bsd.offset = uap->offset; 1036 1037 error = sys_pread(td, &bsd); 1038 1039 if (error == 0) { 1040 /* This seems to violate POSIX but linux does it */ 1041 error = fgetvp(td, uap->fd, 1042 cap_rights_init(&rights, CAP_PREAD), &vp); 1043 if (error != 0) 1044 return (error); 1045 if (vp->v_type == VDIR) { 1046 vrele(vp); 1047 return (EISDIR); 1048 } 1049 vrele(vp); 1050 } 1051 1052 return (error); 1053} 1054 1055int 1056linux_pwrite(td, uap) 1057 struct thread *td; 1058 struct linux_pwrite_args *uap; 1059{ 1060 struct pwrite_args bsd; 1061 1062 bsd.fd = uap->fd; 1063 bsd.buf = uap->buf; 1064 bsd.nbyte = uap->nbyte; 1065 bsd.offset = uap->offset; 1066 return sys_pwrite(td, &bsd); 1067} 1068 1069int 1070linux_mount(struct thread *td, struct linux_mount_args *args) 1071{ 1072 char fstypename[MFSNAMELEN]; 1073 char mntonname[MNAMELEN], mntfromname[MNAMELEN]; 1074 int error; 1075 int fsflags; 1076 1077 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1, 1078 NULL); 1079 if (error) 1080 return (error); 1081 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL); 1082 if (error) 1083 return (error); 1084 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL); 1085 if (error) 1086 return (error); 1087 1088#ifdef DEBUG 1089 if (ldebug(mount)) 1090 printf(ARGS(mount, "%s, %s, %s"), 1091 fstypename, mntfromname, mntonname); 1092#endif 1093 1094 if (strcmp(fstypename, "ext2") == 0) { 1095 strcpy(fstypename, "ext2fs"); 1096 } else if (strcmp(fstypename, "proc") == 0) { 1097 strcpy(fstypename, "linprocfs"); 1098 } else if (strcmp(fstypename, "vfat") == 0) { 1099 strcpy(fstypename, "msdosfs"); 1100 } 1101 1102 fsflags = 0; 1103 1104 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) { 1105 /* 1106 * Linux SYNC flag is not included; the closest equivalent 1107 * FreeBSD has is !ASYNC, which is our default. 1108 */ 1109 if (args->rwflag & LINUX_MS_RDONLY) 1110 fsflags |= MNT_RDONLY; 1111 if (args->rwflag & LINUX_MS_NOSUID) 1112 fsflags |= MNT_NOSUID; 1113 if (args->rwflag & LINUX_MS_NOEXEC) 1114 fsflags |= MNT_NOEXEC; 1115 if (args->rwflag & LINUX_MS_REMOUNT) 1116 fsflags |= MNT_UPDATE; 1117 } 1118 1119 error = kernel_vmount(fsflags, 1120 "fstype", fstypename, 1121 "fspath", mntonname, 1122 "from", mntfromname, 1123 NULL); 1124 return (error); 1125} 1126 1127#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1128int 1129linux_oldumount(struct thread *td, struct linux_oldumount_args *args) 1130{ 1131 struct linux_umount_args args2; 1132 1133 args2.path = args->path; 1134 args2.flags = 0; 1135 return (linux_umount(td, &args2)); 1136} 1137#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1138 1139int 1140linux_umount(struct thread *td, struct linux_umount_args *args) 1141{ 1142 struct unmount_args bsd; 1143 1144 bsd.path = args->path; 1145 bsd.flags = args->flags; /* XXX correct? */ 1146 return (sys_unmount(td, &bsd)); 1147} 1148 1149/* 1150 * fcntl family of syscalls 1151 */ 1152 1153struct l_flock { 1154 l_short l_type; 1155 l_short l_whence; 1156 l_off_t l_start; 1157 l_off_t l_len; 1158 l_pid_t l_pid; 1159} 1160#if defined(__amd64__) && defined(COMPAT_LINUX32) 1161__packed 1162#endif 1163; 1164 1165static void 1166linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock) 1167{ 1168 switch (linux_flock->l_type) { 1169 case LINUX_F_RDLCK: 1170 bsd_flock->l_type = F_RDLCK; 1171 break; 1172 case LINUX_F_WRLCK: 1173 bsd_flock->l_type = F_WRLCK; 1174 break; 1175 case LINUX_F_UNLCK: 1176 bsd_flock->l_type = F_UNLCK; 1177 break; 1178 default: 1179 bsd_flock->l_type = -1; 1180 break; 1181 } 1182 bsd_flock->l_whence = linux_flock->l_whence; 1183 bsd_flock->l_start = (off_t)linux_flock->l_start; 1184 bsd_flock->l_len = (off_t)linux_flock->l_len; 1185 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1186 bsd_flock->l_sysid = 0; 1187} 1188 1189static void 1190bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock) 1191{ 1192 switch (bsd_flock->l_type) { 1193 case F_RDLCK: 1194 linux_flock->l_type = LINUX_F_RDLCK; 1195 break; 1196 case F_WRLCK: 1197 linux_flock->l_type = LINUX_F_WRLCK; 1198 break; 1199 case F_UNLCK: 1200 linux_flock->l_type = LINUX_F_UNLCK; 1201 break; 1202 } 1203 linux_flock->l_whence = bsd_flock->l_whence; 1204 linux_flock->l_start = (l_off_t)bsd_flock->l_start; 1205 linux_flock->l_len = (l_off_t)bsd_flock->l_len; 1206 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1207} 1208 1209#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1210struct l_flock64 { 1211 l_short l_type; 1212 l_short l_whence; 1213 l_loff_t l_start; 1214 l_loff_t l_len; 1215 l_pid_t l_pid; 1216} 1217#if defined(__amd64__) && defined(COMPAT_LINUX32) 1218__packed 1219#endif 1220; 1221 1222static void 1223linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock) 1224{ 1225 switch (linux_flock->l_type) { 1226 case LINUX_F_RDLCK: 1227 bsd_flock->l_type = F_RDLCK; 1228 break; 1229 case LINUX_F_WRLCK: 1230 bsd_flock->l_type = F_WRLCK; 1231 break; 1232 case LINUX_F_UNLCK: 1233 bsd_flock->l_type = F_UNLCK; 1234 break; 1235 default: 1236 bsd_flock->l_type = -1; 1237 break; 1238 } 1239 bsd_flock->l_whence = linux_flock->l_whence; 1240 bsd_flock->l_start = (off_t)linux_flock->l_start; 1241 bsd_flock->l_len = (off_t)linux_flock->l_len; 1242 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1243 bsd_flock->l_sysid = 0; 1244} 1245 1246static void 1247bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) 1248{ 1249 switch (bsd_flock->l_type) { 1250 case F_RDLCK: 1251 linux_flock->l_type = LINUX_F_RDLCK; 1252 break; 1253 case F_WRLCK: 1254 linux_flock->l_type = LINUX_F_WRLCK; 1255 break; 1256 case F_UNLCK: 1257 linux_flock->l_type = LINUX_F_UNLCK; 1258 break; 1259 } 1260 linux_flock->l_whence = bsd_flock->l_whence; 1261 linux_flock->l_start = (l_loff_t)bsd_flock->l_start; 1262 linux_flock->l_len = (l_loff_t)bsd_flock->l_len; 1263 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1264} 1265#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1266 1267static int 1268fcntl_common(struct thread *td, struct linux_fcntl_args *args) 1269{ 1270 struct l_flock linux_flock; 1271 struct flock bsd_flock; 1272 cap_rights_t rights; 1273 struct file *fp; 1274 long arg; 1275 int error, result; 1276 1277 switch (args->cmd) { 1278 case LINUX_F_DUPFD: 1279 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg)); 1280 1281 case LINUX_F_GETFD: 1282 return (kern_fcntl(td, args->fd, F_GETFD, 0)); 1283 1284 case LINUX_F_SETFD: 1285 return (kern_fcntl(td, args->fd, F_SETFD, args->arg)); 1286 1287 case LINUX_F_GETFL: 1288 error = kern_fcntl(td, args->fd, F_GETFL, 0); 1289 result = td->td_retval[0]; 1290 td->td_retval[0] = 0; 1291 if (result & O_RDONLY) 1292 td->td_retval[0] |= LINUX_O_RDONLY; 1293 if (result & O_WRONLY) 1294 td->td_retval[0] |= LINUX_O_WRONLY; 1295 if (result & O_RDWR) 1296 td->td_retval[0] |= LINUX_O_RDWR; 1297 if (result & O_NDELAY) 1298 td->td_retval[0] |= LINUX_O_NONBLOCK; 1299 if (result & O_APPEND) 1300 td->td_retval[0] |= LINUX_O_APPEND; 1301 if (result & O_FSYNC) 1302 td->td_retval[0] |= LINUX_O_SYNC; 1303 if (result & O_ASYNC) 1304 td->td_retval[0] |= LINUX_FASYNC; 1305#ifdef LINUX_O_NOFOLLOW 1306 if (result & O_NOFOLLOW) 1307 td->td_retval[0] |= LINUX_O_NOFOLLOW; 1308#endif 1309#ifdef LINUX_O_DIRECT 1310 if (result & O_DIRECT) 1311 td->td_retval[0] |= LINUX_O_DIRECT; 1312#endif 1313 return (error); 1314 1315 case LINUX_F_SETFL: 1316 arg = 0; 1317 if (args->arg & LINUX_O_NDELAY) 1318 arg |= O_NONBLOCK; 1319 if (args->arg & LINUX_O_APPEND) 1320 arg |= O_APPEND; 1321 if (args->arg & LINUX_O_SYNC) 1322 arg |= O_FSYNC; 1323 if (args->arg & LINUX_FASYNC) 1324 arg |= O_ASYNC; 1325#ifdef LINUX_O_NOFOLLOW 1326 if (args->arg & LINUX_O_NOFOLLOW) 1327 arg |= O_NOFOLLOW; 1328#endif 1329#ifdef LINUX_O_DIRECT 1330 if (args->arg & LINUX_O_DIRECT) 1331 arg |= O_DIRECT; 1332#endif 1333 return (kern_fcntl(td, args->fd, F_SETFL, arg)); 1334 1335 case LINUX_F_GETLK: 1336 error = copyin((void *)args->arg, &linux_flock, 1337 sizeof(linux_flock)); 1338 if (error) 1339 return (error); 1340 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1341 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1342 if (error) 1343 return (error); 1344 bsd_to_linux_flock(&bsd_flock, &linux_flock); 1345 return (copyout(&linux_flock, (void *)args->arg, 1346 sizeof(linux_flock))); 1347 1348 case LINUX_F_SETLK: 1349 error = copyin((void *)args->arg, &linux_flock, 1350 sizeof(linux_flock)); 1351 if (error) 1352 return (error); 1353 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1354 return (kern_fcntl(td, args->fd, F_SETLK, 1355 (intptr_t)&bsd_flock)); 1356 1357 case LINUX_F_SETLKW: 1358 error = copyin((void *)args->arg, &linux_flock, 1359 sizeof(linux_flock)); 1360 if (error) 1361 return (error); 1362 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1363 return (kern_fcntl(td, args->fd, F_SETLKW, 1364 (intptr_t)&bsd_flock)); 1365 1366 case LINUX_F_GETOWN: 1367 return (kern_fcntl(td, args->fd, F_GETOWN, 0)); 1368 1369 case LINUX_F_SETOWN: 1370 /* 1371 * XXX some Linux applications depend on F_SETOWN having no 1372 * significant effect for pipes (SIGIO is not delivered for 1373 * pipes under Linux-2.2.35 at least). 1374 */ 1375 error = fget(td, args->fd, 1376 cap_rights_init(&rights, CAP_FCNTL), &fp); 1377 if (error) 1378 return (error); 1379 if (fp->f_type == DTYPE_PIPE) { 1380 fdrop(fp, td); 1381 return (EINVAL); 1382 } 1383 fdrop(fp, td); 1384 1385 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg)); 1386 1387 case LINUX_F_DUPFD_CLOEXEC: 1388 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg)); 1389 } 1390 1391 return (EINVAL); 1392} 1393 1394int 1395linux_fcntl(struct thread *td, struct linux_fcntl_args *args) 1396{ 1397 1398#ifdef DEBUG 1399 if (ldebug(fcntl)) 1400 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd); 1401#endif 1402 1403 return (fcntl_common(td, args)); 1404} 1405 1406#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1407int 1408linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args) 1409{ 1410 struct l_flock64 linux_flock; 1411 struct flock bsd_flock; 1412 struct linux_fcntl_args fcntl_args; 1413 int error; 1414 1415#ifdef DEBUG 1416 if (ldebug(fcntl64)) 1417 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd); 1418#endif 1419 1420 switch (args->cmd) { 1421 case LINUX_F_GETLK64: 1422 error = copyin((void *)args->arg, &linux_flock, 1423 sizeof(linux_flock)); 1424 if (error) 1425 return (error); 1426 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1427 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1428 if (error) 1429 return (error); 1430 bsd_to_linux_flock64(&bsd_flock, &linux_flock); 1431 return (copyout(&linux_flock, (void *)args->arg, 1432 sizeof(linux_flock))); 1433 1434 case LINUX_F_SETLK64: 1435 error = copyin((void *)args->arg, &linux_flock, 1436 sizeof(linux_flock)); 1437 if (error) 1438 return (error); 1439 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1440 return (kern_fcntl(td, args->fd, F_SETLK, 1441 (intptr_t)&bsd_flock)); 1442 1443 case LINUX_F_SETLKW64: 1444 error = copyin((void *)args->arg, &linux_flock, 1445 sizeof(linux_flock)); 1446 if (error) 1447 return (error); 1448 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1449 return (kern_fcntl(td, args->fd, F_SETLKW, 1450 (intptr_t)&bsd_flock)); 1451 } 1452 1453 fcntl_args.fd = args->fd; 1454 fcntl_args.cmd = args->cmd; 1455 fcntl_args.arg = args->arg; 1456 return (fcntl_common(td, &fcntl_args)); 1457} 1458#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1459 1460int 1461linux_chown(struct thread *td, struct linux_chown_args *args) 1462{ 1463 char *path; 1464 int error; 1465 1466 LCONVPATHEXIST(td, args->path, &path); 1467 1468#ifdef DEBUG 1469 if (ldebug(chown)) 1470 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid); 1471#endif 1472 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid); 1473 LFREEPATH(path); 1474 return (error); 1475} 1476 1477int 1478linux_fchownat(struct thread *td, struct linux_fchownat_args *args) 1479{ 1480 char *path; 1481 int error, dfd, flag; 1482 1483 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 1484 return (EINVAL); 1485 1486 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1487 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 1488 1489#ifdef DEBUG 1490 if (ldebug(fchownat)) 1491 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid); 1492#endif 1493 1494 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 : 1495 AT_SYMLINK_NOFOLLOW; 1496 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid, 1497 flag); 1498 LFREEPATH(path); 1499 return (error); 1500} 1501 1502int 1503linux_lchown(struct thread *td, struct linux_lchown_args *args) 1504{ 1505 char *path; 1506 int error; 1507 1508 LCONVPATHEXIST(td, args->path, &path); 1509 1510#ifdef DEBUG 1511 if (ldebug(lchown)) 1512 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid); 1513#endif 1514 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid); 1515 LFREEPATH(path); 1516 return (error); 1517} 1518 1519static int 1520convert_fadvice(int advice) 1521{ 1522 switch (advice) { 1523 case LINUX_POSIX_FADV_NORMAL: 1524 return (POSIX_FADV_NORMAL); 1525 case LINUX_POSIX_FADV_RANDOM: 1526 return (POSIX_FADV_RANDOM); 1527 case LINUX_POSIX_FADV_SEQUENTIAL: 1528 return (POSIX_FADV_SEQUENTIAL); 1529 case LINUX_POSIX_FADV_WILLNEED: 1530 return (POSIX_FADV_WILLNEED); 1531 case LINUX_POSIX_FADV_DONTNEED: 1532 return (POSIX_FADV_DONTNEED); 1533 case LINUX_POSIX_FADV_NOREUSE: 1534 return (POSIX_FADV_NOREUSE); 1535 default: 1536 return (-1); 1537 } 1538} 1539 1540int 1541linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args) 1542{ 1543 int advice; 1544 1545 advice = convert_fadvice(args->advice); 1546 if (advice == -1) 1547 return (EINVAL); 1548 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1549 advice)); 1550} 1551 1552#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1553int 1554linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args) 1555{ 1556 int advice; 1557 1558 advice = convert_fadvice(args->advice); 1559 if (advice == -1) 1560 return (EINVAL); 1561 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1562 advice)); 1563} 1564#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1565 1566int 1567linux_pipe(struct thread *td, struct linux_pipe_args *args) 1568{ 1569 int fildes[2]; 1570 int error; 1571 1572#ifdef DEBUG 1573 if (ldebug(pipe)) 1574 printf(ARGS(pipe, "*")); 1575#endif 1576 1577 error = kern_pipe2(td, fildes, 0); 1578 if (error) 1579 return (error); 1580 1581 /* XXX: Close descriptors on error. */ 1582 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1583} 1584 1585int 1586linux_pipe2(struct thread *td, struct linux_pipe2_args *args) 1587{ 1588 int fildes[2]; 1589 int error, flags; 1590 1591#ifdef DEBUG 1592 if (ldebug(pipe2)) 1593 printf(ARGS(pipe2, "*, %d"), args->flags); 1594#endif 1595 1596 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0) 1597 return (EINVAL); 1598 1599 flags = 0; 1600 if ((args->flags & LINUX_O_NONBLOCK) != 0) 1601 flags |= O_NONBLOCK; 1602 if ((args->flags & LINUX_O_CLOEXEC) != 0) 1603 flags |= O_CLOEXEC; 1604 error = kern_pipe2(td, fildes, flags); 1605 if (error) 1606 return (error); 1607 1608 /* XXX: Close descriptors on error. */ 1609 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1610} 1611 1612int 1613linux_dup3(struct thread *td, struct linux_dup3_args *args) 1614{ 1615 int cmd; 1616 intptr_t newfd; 1617 1618 if (args->oldfd == args->newfd) 1619 return (EINVAL); 1620 if ((args->flags & ~LINUX_O_CLOEXEC) != 0) 1621 return (EINVAL); 1622 if (args->flags & LINUX_O_CLOEXEC) 1623 cmd = F_DUP2FD_CLOEXEC; 1624 else 1625 cmd = F_DUP2FD; 1626 1627 newfd = args->newfd; 1628 return (kern_fcntl(td, args->oldfd, cmd, newfd)); 1629} 1630 1631int 1632linux_fallocate(struct thread *td, struct linux_fallocate_args *args) 1633{ 1634 1635 /* 1636 * We emulate only posix_fallocate system call for which 1637 * mode should be 0. 1638 */ 1639 if (args->mode != 0) 1640 return (ENOSYS); 1641 1642 return (kern_posix_fallocate(td, args->fd, args->offset, 1643 args->len)); 1644} 1645