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

Cache object: 1f33c8faa5d0f338941f878c59b0f5fe


[ 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.