The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linux/linux_file.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7fba89a90bafba79c49673bfcf7624ef


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.