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/kern/vfs_vnops.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 /*      $NetBSD: vfs_vnops.c,v 1.129.2.1 2007/01/06 13:22:04 bouyer Exp $       */
    2 
    3 /*
    4  * Copyright (c) 1982, 1986, 1989, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * (c) UNIX System Laboratories, Inc.
    7  * All or some portions of this file are derived from material licensed
    8  * to the University of California by American Telephone and Telegraph
    9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   10  * the permission of UNIX System Laboratories, Inc.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.129.2.1 2007/01/06 13:22:04 bouyer Exp $");
   41 
   42 #include "fs_union.h"
   43 #include "veriexec.h"
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/kernel.h>
   48 #include <sys/file.h>
   49 #include <sys/stat.h>
   50 #include <sys/buf.h>
   51 #include <sys/proc.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mount.h>
   54 #include <sys/namei.h>
   55 #include <sys/vnode.h>
   56 #include <sys/ioctl.h>
   57 #include <sys/tty.h>
   58 #include <sys/poll.h>
   59 #include <sys/kauth.h>
   60 #include <sys/syslog.h>
   61 
   62 #include <miscfs/specfs/specdev.h>
   63 
   64 #include <uvm/uvm_extern.h>
   65 #include <uvm/uvm_readahead.h>
   66 
   67 #ifdef UNION
   68 #include <fs/union/union.h>
   69 #endif
   70 
   71 #if defined(LKM) || defined(UNION)
   72 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
   73 #endif
   74 
   75 #if NVERIEXEC > 0
   76 #include <sys/verified_exec.h>
   77 #endif /* NVERIEXEC > 0 */
   78 
   79 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
   80             kauth_cred_t cred, int flags);
   81 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
   82             kauth_cred_t cred, int flags);
   83 static int vn_closefile(struct file *fp, struct lwp *l);
   84 static int vn_poll(struct file *fp, int events, struct lwp *l);
   85 static int vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l);
   86 static int vn_statfile(struct file *fp, struct stat *sb, struct lwp *l);
   87 static int vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l);
   88 
   89 const struct fileops vnops = {
   90         vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
   91         vn_statfile, vn_closefile, vn_kqfilter
   92 };
   93 
   94 /*
   95  * Common code for vnode open operations.
   96  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
   97  */
   98 int
   99 vn_open(struct nameidata *ndp, int fmode, int cmode)
  100 {
  101         struct vnode *vp;
  102         struct mount *mp = NULL;        /* XXX: GCC */
  103         struct lwp *l = ndp->ni_cnd.cn_lwp;
  104         kauth_cred_t cred = l->l_cred;
  105         struct vattr va;
  106         int error;
  107         pathname_t pn = NULL;
  108 
  109 restart:
  110         if (fmode & O_CREAT) {
  111                 ndp->ni_cnd.cn_nameiop = CREATE;
  112                 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
  113                 if ((fmode & O_EXCL) == 0 &&
  114                     ((fmode & O_NOFOLLOW) == 0))
  115                         ndp->ni_cnd.cn_flags |= FOLLOW;
  116         } else {
  117                 ndp->ni_cnd.cn_nameiop = LOOKUP;
  118                 ndp->ni_cnd.cn_flags = LOCKLEAF;
  119                 if ((fmode & O_NOFOLLOW) == 0)
  120                         ndp->ni_cnd.cn_flags |= FOLLOW;
  121         }
  122 #if NVERIEXEC > 0
  123         error = pathname_get(ndp->ni_dirp, ndp->ni_segflg, &pn);
  124         if (error)
  125                 goto bad2;
  126         ndp->ni_dirp = pathname_path(pn);
  127         ndp->ni_segflg = UIO_SYSSPACE;
  128 #endif /* NVERIEXEC > 0 */
  129         error = namei(ndp);
  130         if (error)
  131                 goto bad2;
  132 
  133         vp = ndp->ni_vp;
  134 
  135 #if NVERIEXEC > 0
  136         error = veriexec_openchk(l, ndp->ni_vp, ndp->ni_dirp, fmode);
  137         if (error)
  138                 goto bad;
  139 #endif /* NVERIEXEC > 0 */
  140 
  141         if (fmode & O_CREAT) {
  142                 if (ndp->ni_vp == NULL) {
  143                         VATTR_NULL(&va);
  144                         va.va_type = VREG;
  145                         va.va_mode = cmode;
  146                         if (fmode & O_EXCL)
  147                                  va.va_vaflags |= VA_EXCLUSIVE;
  148                         if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
  149                                 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
  150                                 vput(ndp->ni_dvp);
  151                                 if ((error = vn_start_write(NULL, &mp,
  152                                     V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
  153                                         goto bad2;
  154                                 goto restart;
  155                         }
  156                         VOP_LEASE(ndp->ni_dvp, l, cred, LEASE_WRITE);
  157                         error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
  158                                            &ndp->ni_cnd, &va);
  159                         vn_finished_write(mp, 0);
  160                         if (error)
  161                                 goto bad2;
  162                         fmode &= ~O_TRUNC;
  163                         vp = ndp->ni_vp;
  164                 } else {
  165                         VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
  166                         if (ndp->ni_dvp == ndp->ni_vp)
  167                                 vrele(ndp->ni_dvp);
  168                         else
  169                                 vput(ndp->ni_dvp);
  170                         ndp->ni_dvp = NULL;
  171                         vp = ndp->ni_vp;
  172                         if (fmode & O_EXCL) {
  173                                 error = EEXIST;
  174                                 goto bad;
  175                         }
  176                         fmode &= ~O_CREAT;
  177                 }
  178         } else {
  179                 vp = ndp->ni_vp;
  180         }
  181         if (vp->v_type == VSOCK) {
  182                 error = EOPNOTSUPP;
  183                 goto bad;
  184         }
  185         if (ndp->ni_vp->v_type == VLNK) {
  186                 error = EFTYPE;
  187                 goto bad;
  188         }
  189 
  190         if ((fmode & O_CREAT) == 0) {
  191                 if (fmode & FREAD) {
  192                         if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
  193                                 goto bad;
  194                 }
  195 
  196                 if (fmode & (FWRITE | O_TRUNC)) {
  197                         if (vp->v_type == VDIR) {
  198                                 error = EISDIR;
  199                                 goto bad;
  200                         }
  201                         if ((error = vn_writechk(vp)) != 0 ||
  202                             (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
  203                                 goto bad;
  204                 }
  205         }
  206 
  207         if (fmode & O_TRUNC) {
  208                 VOP_UNLOCK(vp, 0);                      /* XXX */
  209 
  210                 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
  211                         vrele(vp);
  212                         goto bad2;
  213                 }
  214                 VOP_LEASE(vp, l, cred, LEASE_WRITE);
  215                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
  216                 VATTR_NULL(&va);
  217                 va.va_size = 0;
  218                 error = VOP_SETATTR(vp, &va, cred, l);
  219                 vn_finished_write(mp, 0);
  220                 if (error != 0)
  221                         goto bad;
  222         }
  223         if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
  224                 goto bad;
  225         if (vp->v_type == VREG &&
  226             uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
  227                 error = EIO;
  228                 goto bad;
  229         }
  230         if (fmode & FWRITE)
  231                 vp->v_writecount++;
  232 
  233 bad:
  234         if (error)
  235                 vput(vp);
  236 
  237 bad2:
  238         pathname_put(pn);
  239 
  240         return (error);
  241 }
  242 
  243 /*
  244  * Check for write permissions on the specified vnode.
  245  * Prototype text segments cannot be written.
  246  */
  247 int
  248 vn_writechk(struct vnode *vp)
  249 {
  250 
  251         /*
  252          * If the vnode is in use as a process's text,
  253          * we can't allow writing.
  254          */
  255         if (vp->v_flag & VTEXT)
  256                 return (ETXTBSY);
  257         return (0);
  258 }
  259 
  260 /*
  261  * Mark a vnode as having executable mappings.
  262  */
  263 void
  264 vn_markexec(struct vnode *vp)
  265 {
  266         if ((vp->v_flag & VEXECMAP) == 0) {
  267                 uvmexp.filepages -= vp->v_uobj.uo_npages;
  268                 uvmexp.execpages += vp->v_uobj.uo_npages;
  269         }
  270         vp->v_flag |= VEXECMAP;
  271 }
  272 
  273 /*
  274  * Mark a vnode as being the text of a process.
  275  * Fail if the vnode is currently writable.
  276  */
  277 int
  278 vn_marktext(struct vnode *vp)
  279 {
  280 
  281         if (vp->v_writecount != 0) {
  282                 KASSERT((vp->v_flag & VTEXT) == 0);
  283                 return (ETXTBSY);
  284         }
  285         vp->v_flag |= VTEXT;
  286         vn_markexec(vp);
  287         return (0);
  288 }
  289 
  290 /*
  291  * Vnode close call
  292  *
  293  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
  294  */
  295 int
  296 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
  297 {
  298         int error;
  299 
  300         if (flags & FWRITE)
  301                 vp->v_writecount--;
  302         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  303         error = VOP_CLOSE(vp, flags, cred, l);
  304         vput(vp);
  305         return (error);
  306 }
  307 
  308 /*
  309  * Package up an I/O request on a vnode into a uio and do it.
  310  */
  311 int
  312 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, off_t offset,
  313     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
  314     struct lwp *l)
  315 {
  316         struct uio auio;
  317         struct iovec aiov;
  318         struct mount *mp = NULL;
  319         int error;
  320 
  321         if ((ioflg & IO_NODELOCKED) == 0) {
  322                 if (rw == UIO_READ) {
  323                         vn_lock(vp, LK_SHARED | LK_RETRY);
  324                 } else /* UIO_WRITE */ {
  325                         if (vp->v_type != VCHR &&
  326                             (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
  327                             != 0)
  328                                 return (error);
  329                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  330                 }
  331         }
  332         auio.uio_iov = &aiov;
  333         auio.uio_iovcnt = 1;
  334         aiov.iov_base = base;
  335         aiov.iov_len = len;
  336         auio.uio_resid = len;
  337         auio.uio_offset = offset;
  338         auio.uio_rw = rw;
  339         if (segflg == UIO_SYSSPACE) {
  340                 UIO_SETUP_SYSSPACE(&auio);
  341         } else {
  342                 auio.uio_vmspace = l->l_proc->p_vmspace;
  343         }
  344         if (rw == UIO_READ) {
  345                 error = VOP_READ(vp, &auio, ioflg, cred);
  346         } else {
  347                 error = VOP_WRITE(vp, &auio, ioflg, cred);
  348         }
  349         if (aresid)
  350                 *aresid = auio.uio_resid;
  351         else
  352                 if (auio.uio_resid && error == 0)
  353                         error = EIO;
  354         if ((ioflg & IO_NODELOCKED) == 0) {
  355                 if (rw == UIO_WRITE)
  356                         vn_finished_write(mp, 0);
  357                 VOP_UNLOCK(vp, 0);
  358         }
  359         return (error);
  360 }
  361 
  362 int
  363 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
  364     struct lwp *l, off_t **cookies, int *ncookies)
  365 {
  366         struct vnode *vp = (struct vnode *)fp->f_data;
  367         struct iovec aiov;
  368         struct uio auio;
  369         int error, eofflag;
  370 
  371         /* Limit the size on any kernel buffers used by VOP_READDIR */
  372         count = min(MAXBSIZE, count);
  373 
  374 unionread:
  375         if (vp->v_type != VDIR)
  376                 return (EINVAL);
  377         aiov.iov_base = bf;
  378         aiov.iov_len = count;
  379         auio.uio_iov = &aiov;
  380         auio.uio_iovcnt = 1;
  381         auio.uio_rw = UIO_READ;
  382         if (segflg == UIO_SYSSPACE) {
  383                 UIO_SETUP_SYSSPACE(&auio);
  384         } else {
  385                 KASSERT(l == curlwp);
  386                 auio.uio_vmspace = l->l_proc->p_vmspace;
  387         }
  388         auio.uio_resid = count;
  389         vn_lock(vp, LK_SHARED | LK_RETRY);
  390         auio.uio_offset = fp->f_offset;
  391         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
  392                     ncookies);
  393         fp->f_offset = auio.uio_offset;
  394         VOP_UNLOCK(vp, 0);
  395         if (error)
  396                 return (error);
  397 
  398 #if defined(UNION) || defined(LKM)
  399         if (count == auio.uio_resid && vn_union_readdir_hook) {
  400                 struct vnode *ovp = vp;
  401 
  402                 error = (*vn_union_readdir_hook)(&vp, fp, l);
  403                 if (error)
  404                         return (error);
  405                 if (vp != ovp)
  406                         goto unionread;
  407         }
  408 #endif /* UNION || LKM */
  409 
  410         if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
  411             (vp->v_mount->mnt_flag & MNT_UNION)) {
  412                 struct vnode *tvp = vp;
  413                 vp = vp->v_mount->mnt_vnodecovered;
  414                 VREF(vp);
  415                 fp->f_data = vp;
  416                 fp->f_offset = 0;
  417                 vrele(tvp);
  418                 goto unionread;
  419         }
  420         *done = count - auio.uio_resid;
  421         return error;
  422 }
  423 
  424 /*
  425  * File table vnode read routine.
  426  */
  427 static int
  428 vn_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
  429     int flags)
  430 {
  431         struct vnode *vp = (struct vnode *)fp->f_data;
  432         int count, error, ioflag;
  433         struct lwp *l = curlwp;
  434 
  435         VOP_LEASE(vp, l, cred, LEASE_READ);
  436         ioflag = IO_ADV_ENCODE(fp->f_advice);
  437         if (fp->f_flag & FNONBLOCK)
  438                 ioflag |= IO_NDELAY;
  439         if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
  440                 ioflag |= IO_SYNC;
  441         if (fp->f_flag & FALTIO)
  442                 ioflag |= IO_ALTSEMANTICS;
  443         if (fp->f_flag & FDIRECT)
  444                 ioflag |= IO_DIRECT;
  445         vn_lock(vp, LK_SHARED | LK_RETRY);
  446         uio->uio_offset = *offset;
  447         count = uio->uio_resid;
  448         error = VOP_READ(vp, uio, ioflag, cred);
  449         if (flags & FOF_UPDATE_OFFSET)
  450                 *offset += count - uio->uio_resid;
  451         VOP_UNLOCK(vp, 0);
  452         return (error);
  453 }
  454 
  455 /*
  456  * File table vnode write routine.
  457  */
  458 static int
  459 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
  460     int flags)
  461 {
  462         struct vnode *vp = (struct vnode *)fp->f_data;
  463         struct mount *mp;
  464         int count, error, ioflag = IO_UNIT;
  465         struct lwp *l = curlwp;
  466 
  467         if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
  468                 ioflag |= IO_APPEND;
  469         if (fp->f_flag & FNONBLOCK)
  470                 ioflag |= IO_NDELAY;
  471         if (fp->f_flag & FFSYNC ||
  472             (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
  473                 ioflag |= IO_SYNC;
  474         else if (fp->f_flag & FDSYNC)
  475                 ioflag |= IO_DSYNC;
  476         if (fp->f_flag & FALTIO)
  477                 ioflag |= IO_ALTSEMANTICS;
  478         if (fp->f_flag & FDIRECT)
  479                 ioflag |= IO_DIRECT;
  480         mp = NULL;
  481         if (vp->v_type != VCHR &&
  482             (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
  483                 return (error);
  484         VOP_LEASE(vp, l, cred, LEASE_WRITE);
  485         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  486         uio->uio_offset = *offset;
  487         count = uio->uio_resid;
  488         error = VOP_WRITE(vp, uio, ioflag, cred);
  489         if (flags & FOF_UPDATE_OFFSET) {
  490                 if (ioflag & IO_APPEND)
  491                         *offset = uio->uio_offset;
  492                 else
  493                         *offset += count - uio->uio_resid;
  494         }
  495         VOP_UNLOCK(vp, 0);
  496         vn_finished_write(mp, 0);
  497         return (error);
  498 }
  499 
  500 /*
  501  * File table vnode stat routine.
  502  */
  503 static int
  504 vn_statfile(struct file *fp, struct stat *sb, struct lwp *l)
  505 {
  506         struct vnode *vp = (struct vnode *)fp->f_data;
  507 
  508         return vn_stat(vp, sb, l);
  509 }
  510 
  511 int
  512 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
  513 {
  514         struct vattr va;
  515         int error;
  516         mode_t mode;
  517 
  518         error = VOP_GETATTR(vp, &va, l->l_cred, l);
  519         if (error)
  520                 return (error);
  521         /*
  522          * Copy from vattr table
  523          */
  524         sb->st_dev = va.va_fsid;
  525         sb->st_ino = va.va_fileid;
  526         mode = va.va_mode;
  527         switch (vp->v_type) {
  528         case VREG:
  529                 mode |= S_IFREG;
  530                 break;
  531         case VDIR:
  532                 mode |= S_IFDIR;
  533                 break;
  534         case VBLK:
  535                 mode |= S_IFBLK;
  536                 break;
  537         case VCHR:
  538                 mode |= S_IFCHR;
  539                 break;
  540         case VLNK:
  541                 mode |= S_IFLNK;
  542                 break;
  543         case VSOCK:
  544                 mode |= S_IFSOCK;
  545                 break;
  546         case VFIFO:
  547                 mode |= S_IFIFO;
  548                 break;
  549         default:
  550                 return (EBADF);
  551         };
  552         sb->st_mode = mode;
  553         sb->st_nlink = va.va_nlink;
  554         sb->st_uid = va.va_uid;
  555         sb->st_gid = va.va_gid;
  556         sb->st_rdev = va.va_rdev;
  557         sb->st_size = va.va_size;
  558         sb->st_atimespec = va.va_atime;
  559         sb->st_mtimespec = va.va_mtime;
  560         sb->st_ctimespec = va.va_ctime;
  561         sb->st_birthtimespec = va.va_birthtime;
  562         sb->st_blksize = va.va_blocksize;
  563         sb->st_flags = va.va_flags;
  564         sb->st_gen = 0;
  565         sb->st_blocks = va.va_bytes / S_BLKSIZE;
  566         return (0);
  567 }
  568 
  569 /*
  570  * File table vnode fcntl routine.
  571  */
  572 static int
  573 vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l)
  574 {
  575         struct vnode *vp = ((struct vnode *)fp->f_data);
  576         int error;
  577 
  578         error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_cred, l);
  579         return (error);
  580 }
  581 
  582 /*
  583  * File table vnode ioctl routine.
  584  */
  585 static int
  586 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
  587 {
  588         struct vnode *vp = ((struct vnode *)fp->f_data);
  589         struct proc *p = l->l_proc;
  590         struct vattr vattr;
  591         int error;
  592 
  593         switch (vp->v_type) {
  594 
  595         case VREG:
  596         case VDIR:
  597                 if (com == FIONREAD) {
  598                         error = VOP_GETATTR(vp, &vattr, l->l_cred, l);
  599                         if (error)
  600                                 return (error);
  601                         *(int *)data = vattr.va_size - fp->f_offset;
  602                         return (0);
  603                 }
  604                 if ((com == FIONWRITE) || (com == FIONSPACE)) {
  605                         /*
  606                          * Files don't have send queues, so there never
  607                          * are any bytes in them, nor is there any
  608                          * open space in them.
  609                          */
  610                         *(int *)data = 0;
  611                         return (0);
  612                 }
  613                 if (com == FIOGETBMAP) {
  614                         daddr_t *block;
  615 
  616                         if (*(daddr_t *)data < 0)
  617                                 return (EINVAL);
  618                         block = (daddr_t *)data;
  619                         return (VOP_BMAP(vp, *block, NULL, block, NULL));
  620                 }
  621                 if (com == OFIOGETBMAP) {
  622                         daddr_t ibn, obn;
  623 
  624                         if (*(int32_t *)data < 0)
  625                                 return (EINVAL);
  626                         ibn = (daddr_t)*(int32_t *)data;
  627                         error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
  628                         *(int32_t *)data = (int32_t)obn;
  629                         return error;
  630                 }
  631                 if (com == FIONBIO || com == FIOASYNC)  /* XXX */
  632                         return (0);                     /* XXX */
  633                 /* fall into ... */
  634         case VFIFO:
  635         case VCHR:
  636         case VBLK:
  637                 error = VOP_IOCTL(vp, com, data, fp->f_flag,
  638                     l->l_cred, l);
  639                 if (error == 0 && com == TIOCSCTTY) {
  640                         if (p->p_session->s_ttyvp)
  641                                 vrele(p->p_session->s_ttyvp);
  642                         p->p_session->s_ttyvp = vp;
  643                         VREF(vp);
  644                 }
  645                 return (error);
  646 
  647         default:
  648                 return (EPASSTHROUGH);
  649         }
  650 }
  651 
  652 /*
  653  * File table vnode poll routine.
  654  */
  655 static int
  656 vn_poll(struct file *fp, int events, struct lwp *l)
  657 {
  658 
  659         return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
  660 }
  661 
  662 /*
  663  * File table vnode kqfilter routine.
  664  */
  665 int
  666 vn_kqfilter(struct file *fp, struct knote *kn)
  667 {
  668 
  669         return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
  670 }
  671 
  672 /*
  673  * Check that the vnode is still valid, and if so
  674  * acquire requested lock.
  675  */
  676 int
  677 vn_lock(struct vnode *vp, int flags)
  678 {
  679         int error;
  680 
  681 #if 0
  682         KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
  683             || (vp->v_flag & VONWORKLST) != 0);
  684 #endif
  685         KASSERT((flags &
  686             ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
  687             LK_SETRECURSE|LK_CANRECURSE))
  688             == 0);
  689 
  690         do {
  691                 if ((flags & LK_INTERLOCK) == 0)
  692                         simple_lock(&vp->v_interlock);
  693                 if (vp->v_flag & VXLOCK) {
  694                         if (flags & LK_NOWAIT) {
  695                                 simple_unlock(&vp->v_interlock);
  696                                 return EBUSY;
  697                         }
  698                         vp->v_flag |= VXWANT;
  699                         ltsleep(vp, PINOD | PNORELOCK,
  700                             "vn_lock", 0, &vp->v_interlock);
  701                         error = ENOENT;
  702                 } else {
  703                         error = VOP_LOCK(vp,
  704                             (flags & ~LK_RETRY) | LK_INTERLOCK);
  705                         if (error == 0 || error == EDEADLK || error == EBUSY)
  706                                 return (error);
  707                 }
  708                 flags &= ~LK_INTERLOCK;
  709         } while (flags & LK_RETRY);
  710         return (error);
  711 }
  712 
  713 /*
  714  * File table vnode close routine.
  715  */
  716 static int
  717 vn_closefile(struct file *fp, struct lwp *l)
  718 {
  719 
  720         return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
  721                 fp->f_cred, l));
  722 }
  723 
  724 /*
  725  * Enable LK_CANRECURSE on lock. Return prior status.
  726  */
  727 u_int
  728 vn_setrecurse(struct vnode *vp)
  729 {
  730         struct lock *lkp = &vp->v_lock;
  731         u_int retval = lkp->lk_flags & LK_CANRECURSE;
  732 
  733         lkp->lk_flags |= LK_CANRECURSE;
  734         return retval;
  735 }
  736 
  737 /*
  738  * Called when done with locksetrecurse.
  739  */
  740 void
  741 vn_restorerecurse(struct vnode *vp, u_int flags)
  742 {
  743         struct lock *lkp = &vp->v_lock;
  744 
  745         lkp->lk_flags &= ~LK_CANRECURSE;
  746         lkp->lk_flags |= flags;
  747 }
  748 
  749 int
  750 vn_cow_establish(struct vnode *vp,
  751     int (*func)(void *, struct buf *), void *cookie)
  752 {
  753         int s;
  754         struct spec_cow_entry *e;
  755 
  756         MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
  757             M_DEVBUF, M_WAITOK);
  758         e->ce_func = func;
  759         e->ce_cookie = cookie;
  760 
  761         SPEC_COW_LOCK(vp->v_specinfo, s);
  762         vp->v_spec_cow_req++;
  763         while (vp->v_spec_cow_count > 0)
  764                 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
  765                     &vp->v_spec_cow_slock);
  766 
  767         SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
  768 
  769         vp->v_spec_cow_req--;
  770         if (vp->v_spec_cow_req == 0)
  771                 wakeup(&vp->v_spec_cow_req);
  772         SPEC_COW_UNLOCK(vp->v_specinfo, s);
  773 
  774         return 0;
  775 }
  776 
  777 int
  778 vn_cow_disestablish(struct vnode *vp,
  779     int (*func)(void *, struct buf *), void *cookie)
  780 {
  781         int s;
  782         struct spec_cow_entry *e;
  783 
  784         SPEC_COW_LOCK(vp->v_specinfo, s);
  785         vp->v_spec_cow_req++;
  786         while (vp->v_spec_cow_count > 0)
  787                 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
  788                     &vp->v_spec_cow_slock);
  789 
  790         SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
  791                 if (e->ce_func == func && e->ce_cookie == cookie) {
  792                         SLIST_REMOVE(&vp->v_spec_cow_head, e,
  793                             spec_cow_entry, ce_list);
  794                         FREE(e, M_DEVBUF);
  795                         break;
  796                 }
  797 
  798         vp->v_spec_cow_req--;
  799         if (vp->v_spec_cow_req == 0)
  800                 wakeup(&vp->v_spec_cow_req);
  801         SPEC_COW_UNLOCK(vp->v_specinfo, s);
  802 
  803         return e ? 0 : EINVAL;
  804 }
  805 
  806 /*
  807  * Simplified in-kernel wrapper calls for extended attribute access.
  808  * Both calls pass in a NULL credential, authorizing a "kernel" access.
  809  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
  810  */
  811 int
  812 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
  813     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
  814 {
  815         struct uio auio;
  816         struct iovec aiov;
  817         int error;
  818 
  819         aiov.iov_len = *buflen;
  820         aiov.iov_base = bf;
  821 
  822         auio.uio_iov = &aiov;
  823         auio.uio_iovcnt = 1;
  824         auio.uio_rw = UIO_READ;
  825         auio.uio_offset = 0;
  826         auio.uio_resid = *buflen;
  827         UIO_SETUP_SYSSPACE(&auio);
  828 
  829         if ((ioflg & IO_NODELOCKED) == 0)
  830                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  831 
  832         error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
  833             l);
  834 
  835         if ((ioflg & IO_NODELOCKED) == 0)
  836                 VOP_UNLOCK(vp, 0);
  837 
  838         if (error == 0)
  839                 *buflen = *buflen - auio.uio_resid;
  840 
  841         return (error);
  842 }
  843 
  844 /*
  845  * XXX Failure mode if partially written?
  846  */
  847 int
  848 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
  849     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
  850 {
  851         struct uio auio;
  852         struct iovec aiov;
  853         struct mount *mp = NULL;        /* XXX: GCC */
  854         int error;
  855 
  856         aiov.iov_len = buflen;
  857         aiov.iov_base = __UNCONST(bf);          /* XXXUNCONST kills const */
  858 
  859         auio.uio_iov = &aiov;
  860         auio.uio_iovcnt = 1;
  861         auio.uio_rw = UIO_WRITE;
  862         auio.uio_offset = 0;
  863         auio.uio_resid = buflen;
  864         UIO_SETUP_SYSSPACE(&auio);
  865 
  866         if ((ioflg & IO_NODELOCKED) == 0) {
  867                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
  868                         return (error);
  869                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  870         }
  871 
  872         error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
  873 
  874         if ((ioflg & IO_NODELOCKED) == 0) {
  875                 vn_finished_write(mp, 0);
  876                 VOP_UNLOCK(vp, 0);
  877         }
  878 
  879         return (error);
  880 }
  881 
  882 int
  883 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
  884     const char *attrname, struct lwp *l)
  885 {
  886         struct mount *mp = NULL;        /* XXX: GCC */
  887         int error;
  888 
  889         if ((ioflg & IO_NODELOCKED) == 0) {
  890                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
  891                         return (error);
  892                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  893         }
  894 
  895         error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
  896         if (error == EOPNOTSUPP)
  897                 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
  898                     NULL, l);
  899 
  900         if ((ioflg & IO_NODELOCKED) == 0) {
  901                 vn_finished_write(mp, 0);
  902                 VOP_UNLOCK(vp, 0);
  903         }
  904 
  905         return (error);
  906 }
  907 
  908 /*
  909  * Preparing to start a filesystem write operation. If the operation is
  910  * permitted, then we bump the count of operations in progress and
  911  * proceed. If a suspend request is in progress, we wait until the
  912  * suspension is over, and then proceed.
  913  * V_PCATCH    adds PCATCH to the tsleep flags.
  914  * V_WAIT      waits until suspension is over. Otherwise returns EWOULDBLOCK.
  915  * V_SLEEPONLY wait, but do not bump the operations count.
  916  * V_LOWER     this is a lower level operation. No further vnodes should be
  917  *             locked. Otherwise it is a upper level operation. No vnodes
  918  *             should be locked.
  919  */
  920 int
  921 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
  922 {
  923         struct mount *mp;
  924         int error, mask, prio;
  925 
  926         /*
  927          * If a vnode is provided, get and return the mount point that
  928          * to which it will write.
  929          */
  930         if (vp != NULL) {
  931                 *mpp = vp->v_mount;
  932         }
  933         if ((mp = *mpp) == NULL)
  934                 return (0);
  935         mp = mp->mnt_leaf;
  936         /*
  937          * Check on status of suspension.
  938          */
  939         prio = PUSER - 1;
  940         if (flags & V_PCATCH)
  941                 prio |= PCATCH;
  942 
  943         if ((flags & V_LOWER) == 0)
  944                 mask = IMNT_SUSPEND;
  945         else
  946                 mask = IMNT_SUSPENDLOW;
  947 
  948         while ((mp->mnt_iflag & mask) != 0) {
  949                 if ((flags & V_WAIT) == 0)
  950                         return (EWOULDBLOCK);
  951                 error = tsleep(&mp->mnt_flag, prio, "suspfs", 0);
  952                 if (error)
  953                         return (error);
  954         }
  955         if (flags & V_SLEEPONLY)
  956                 return (0);
  957         simple_lock(&mp->mnt_slock);
  958         if ((flags & V_LOWER) == 0)
  959                 mp->mnt_writeopcountupper++;
  960         else
  961                 mp->mnt_writeopcountlower++;
  962         simple_unlock(&mp->mnt_slock);
  963         return (0);
  964 }
  965 
  966 /*
  967  * Filesystem write operation has completed. If we are suspending and this
  968  * operation is the last one, notify the suspender that the suspension is
  969  * now in effect.
  970  */
  971 void
  972 vn_finished_write(struct mount *mp, int flags)
  973 {
  974         if (mp == NULL)
  975                 return;
  976         mp = mp->mnt_leaf;
  977         simple_lock(&mp->mnt_slock);
  978         if ((flags & V_LOWER) == 0) {
  979                 mp->mnt_writeopcountupper--;
  980                 if (mp->mnt_writeopcountupper < 0)
  981                         printf("vn_finished_write: neg cnt upper=%d\n",
  982                                mp->mnt_writeopcountupper);
  983                 if ((mp->mnt_iflag & IMNT_SUSPEND) != 0 &&
  984                     mp->mnt_writeopcountupper <= 0)
  985                         wakeup(&mp->mnt_writeopcountupper);
  986         } else {
  987                 mp->mnt_writeopcountlower--;
  988                 if (mp->mnt_writeopcountlower < 0)
  989                         printf("vn_finished_write: neg cnt lower=%d\n",
  990                                mp->mnt_writeopcountlower);
  991                 if ((mp->mnt_iflag & IMNT_SUSPENDLOW) != 0 &&
  992                     mp->mnt_writeopcountupper <= 0)
  993                         wakeup(&mp->mnt_writeopcountlower);
  994         }
  995         simple_unlock(&mp->mnt_slock);
  996 }
  997 
  998 void
  999 vn_ra_allocctx(struct vnode *vp)
 1000 {
 1001         struct uvm_ractx *ra = NULL;
 1002 
 1003         if (vp->v_type != VREG) {
 1004                 return;
 1005         }
 1006         if (vp->v_ractx != NULL) {
 1007                 return;
 1008         }
 1009         simple_lock(&vp->v_interlock);
 1010         if (vp->v_ractx == NULL) {
 1011                 simple_unlock(&vp->v_interlock);
 1012                 ra = uvm_ra_allocctx();
 1013                 simple_lock(&vp->v_interlock);
 1014                 if (ra != NULL && vp->v_ractx == NULL) {
 1015                         vp->v_ractx = ra;
 1016                         ra = NULL;
 1017                 }
 1018         }
 1019         simple_unlock(&vp->v_interlock);
 1020         if (ra != NULL) {
 1021                 uvm_ra_freectx(ra);
 1022         }
 1023 }

Cache object: 9c259c81265529611d87f05f49b803fe


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