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.86.2.11 2006/05/31 21:18:59 tron 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.86.2.11 2006/05/31 21:18:59 tron Exp $");
   41 
   42 #include "opt_verified_exec.h"
   43 
   44 #include "fs_union.h"
   45 
   46 #include <sys/param.h>
   47 #include <sys/systm.h>
   48 #include <sys/kernel.h>
   49 #include <sys/file.h>
   50 #include <sys/stat.h>
   51 #include <sys/buf.h>
   52 #include <sys/proc.h>
   53 #include <sys/malloc.h>
   54 #include <sys/mount.h>
   55 #include <sys/namei.h>
   56 #include <sys/vnode.h>
   57 #include <sys/ioctl.h>
   58 #include <sys/tty.h>
   59 #include <sys/poll.h>
   60 
   61 #include <miscfs/specfs/specdev.h>
   62 
   63 #include <uvm/uvm_extern.h>
   64 
   65 #ifdef UNION
   66 #include <fs/union/union.h>
   67 #endif
   68 
   69 #if defined(LKM) || defined(UNION)
   70 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct proc *);
   71 #endif
   72 
   73 #ifdef VERIFIED_EXEC
   74 #include <sys/verified_exec.h>
   75 #endif
   76 
   77 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
   78             struct ucred *cred, int flags);
   79 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
   80             struct ucred *cred, int flags);
   81 static int vn_closefile(struct file *fp, struct proc *p);
   82 static int vn_poll(struct file *fp, int events, struct proc *p);
   83 static int vn_fcntl(struct file *fp, u_int com, void *data, struct proc *p);
   84 static int vn_statfile(struct file *fp, struct stat *sb, struct proc *p);
   85 static int vn_ioctl(struct file *fp, u_long com, void *data, struct proc *p);
   86 
   87 const struct fileops vnops = {
   88         vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
   89         vn_statfile, vn_closefile, vn_kqfilter
   90 };
   91 
   92 /*
   93  * Common code for vnode open operations.
   94  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
   95  */
   96 int
   97 vn_open(ndp, fmode, cmode)
   98         struct nameidata *ndp;
   99         int fmode, cmode;
  100 {
  101         struct vnode *vp;
  102         struct mount *mp;
  103         struct proc *p = ndp->ni_cnd.cn_proc;
  104         struct ucred *cred = p->p_ucred;
  105         struct vattr va;
  106         int error;
  107 #ifdef VERIFIED_EXEC
  108         struct veriexec_hash_entry *vhe = NULL;
  109         char pathbuf[MAXPATHLEN];
  110         size_t pathlen;
  111         int (*copyfun)(const void *, void *, size_t, size_t *) =
  112             ndp->ni_segflg == UIO_SYSSPACE ? copystr : copyinstr;
  113 #endif /* VERIFIED_EXEC */
  114                         
  115 #ifdef VERIFIED_EXEC
  116         error = (*copyfun)(ndp->ni_dirp, pathbuf, sizeof(pathbuf), &pathlen);
  117         if (error) {
  118                 if (veriexec_verbose >= 1)
  119                         printf("veriexec: Can't copy path. (error=%d)\n",  
  120                             error);
  121 
  122                 return (error);
  123         }
  124 #endif /* VERIFIED_EXEC */
  125 
  126 restart:
  127         if (fmode & O_CREAT) {
  128                 ndp->ni_cnd.cn_nameiop = CREATE;
  129                 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
  130                 if ((fmode & O_EXCL) == 0 &&
  131                     ((fmode & O_NOFOLLOW) == 0))
  132                         ndp->ni_cnd.cn_flags |= FOLLOW;
  133                 if ((error = namei(ndp)) != 0)
  134                         return (error);
  135                 if (ndp->ni_vp == NULL) {
  136 #ifdef VERIFIED_EXEC
  137                         /* Lockdown mode: Prevent creation of new files. */
  138                         if (veriexec_strict >= 3) {
  139                                 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
  140 
  141                                 printf("Veriexec: vn_open: Preventing "
  142                                        "new file creation in %s.\n",
  143                                        pathbuf);
  144 
  145                                 vp = ndp->ni_dvp;
  146                                 error = EPERM;
  147                                 goto bad;
  148                         }
  149 #endif /* VERIFIED_EXEC */
  150 
  151                         VATTR_NULL(&va);
  152                         va.va_type = VREG;
  153                         va.va_mode = cmode;
  154                         if (fmode & O_EXCL)
  155                                  va.va_vaflags |= VA_EXCLUSIVE;
  156                         if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
  157                                 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
  158                                 vput(ndp->ni_dvp);
  159                                 if ((error = vn_start_write(NULL, &mp,
  160                                     V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
  161                                         return (error);
  162                                 goto restart;
  163                         }
  164                         VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
  165                         error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
  166                                            &ndp->ni_cnd, &va);
  167                         vn_finished_write(mp, 0);
  168                         if (error)
  169                                 return (error);
  170                         fmode &= ~O_TRUNC;
  171                         vp = ndp->ni_vp;
  172                 } else {
  173                         VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
  174                         if (ndp->ni_dvp == ndp->ni_vp)
  175                                 vrele(ndp->ni_dvp);
  176                         else
  177                                 vput(ndp->ni_dvp);
  178                         ndp->ni_dvp = NULL;
  179                         vp = ndp->ni_vp;
  180                         if (fmode & O_EXCL) {
  181                                 error = EEXIST;
  182                                 goto bad;
  183                         }
  184                         fmode &= ~O_CREAT;
  185                 }
  186         } else {
  187                 ndp->ni_cnd.cn_nameiop = LOOKUP;
  188                 ndp->ni_cnd.cn_flags = LOCKLEAF;
  189                 if ((fmode & O_NOFOLLOW) == 0)
  190                         ndp->ni_cnd.cn_flags |= FOLLOW;
  191                 if ((error = namei(ndp)) != 0)
  192                         return (error);
  193                 vp = ndp->ni_vp;
  194         }
  195         if (vp->v_type == VSOCK) {
  196                 error = EOPNOTSUPP;
  197                 goto bad;
  198         }
  199         if (ndp->ni_vp->v_type == VLNK) {
  200                 error = EFTYPE;
  201                 goto bad;
  202         }
  203 
  204 #ifdef VERIFIED_EXEC
  205         if ((error = VOP_GETATTR(vp, &va, cred, p)) != 0)
  206                 goto bad;
  207 #endif
  208 
  209         if ((fmode & O_CREAT) == 0) {
  210 #ifdef VERIFIED_EXEC
  211                 if ((error = veriexec_verify(p, vp, &va, pathbuf,
  212                                              VERIEXEC_FILE, &vhe)) != 0)
  213                         goto bad;
  214 #endif
  215 
  216                 if (fmode & FREAD) {
  217                         if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
  218                                 goto bad;
  219                 }
  220 
  221                 if (fmode & (FWRITE | O_TRUNC)) {
  222                         if (vp->v_type == VDIR) {
  223                                 error = EISDIR;
  224                                 goto bad;
  225                         }
  226                         if ((error = vn_writechk(vp)) != 0 ||
  227                             (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
  228                                 goto bad;
  229 #ifdef VERIFIED_EXEC
  230                         if (vhe != NULL) {
  231                                 veriexec_report("Write access request.",
  232                                                 pathbuf, &va, p,
  233                                                 REPORT_NOVERBOSE,
  234                                                 REPORT_ALARM,
  235                                                 REPORT_NOPANIC);
  236 
  237                                 /* IPS mode: Deny writing to monitored files. */
  238                                 if (veriexec_strict >= 2) {
  239                                         error = EPERM;
  240                                         goto bad;
  241                                 } else {
  242                                         vhe->status = FINGERPRINT_NOTEVAL;
  243                                 }
  244                         }
  245 #endif
  246                 }
  247         }
  248 
  249         if (fmode & O_TRUNC) {
  250                 VOP_UNLOCK(vp, 0);                      /* XXX */
  251                 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
  252                         vrele(vp);
  253                         return (error);
  254                 }
  255                 VOP_LEASE(vp, p, cred, LEASE_WRITE);
  256                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
  257                 VATTR_NULL(&va);
  258                 va.va_size = 0;
  259                 error = VOP_SETATTR(vp, &va, cred, p);
  260                 vn_finished_write(mp, 0);
  261                 if (error != 0)
  262                         goto bad;
  263         }
  264         if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
  265                 goto bad;
  266         if (vp->v_type == VREG &&
  267             uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
  268                 error = EIO;
  269                 goto bad;
  270         }
  271         if (fmode & FWRITE)
  272                 vp->v_writecount++;
  273 
  274         return (0);
  275 bad:
  276         vput(vp);
  277         return (error);
  278 }
  279 
  280 /*
  281  * Check for write permissions on the specified vnode.
  282  * Prototype text segments cannot be written.
  283  */
  284 int
  285 vn_writechk(vp)
  286         struct vnode *vp;
  287 {
  288 
  289         /*
  290          * If the vnode is in use as a process's text,
  291          * we can't allow writing.
  292          */
  293         if (vp->v_flag & VTEXT)
  294                 return (ETXTBSY);
  295         return (0);
  296 }
  297 
  298 /*
  299  * Mark a vnode as having executable mappings.
  300  */
  301 void
  302 vn_markexec(vp)
  303         struct vnode *vp;
  304 {
  305         if ((vp->v_flag & VEXECMAP) == 0) {
  306                 uvmexp.filepages -= vp->v_uobj.uo_npages;
  307                 uvmexp.execpages += vp->v_uobj.uo_npages;
  308         }
  309         vp->v_flag |= VEXECMAP;
  310 }
  311 
  312 /*
  313  * Mark a vnode as being the text of a process.
  314  * Fail if the vnode is currently writable.
  315  */
  316 int
  317 vn_marktext(vp)
  318         struct vnode *vp;
  319 {
  320 
  321         if (vp->v_writecount != 0) {
  322                 KASSERT((vp->v_flag & VTEXT) == 0);
  323                 return (ETXTBSY);
  324         }
  325         vp->v_flag |= VTEXT;
  326         vn_markexec(vp);
  327         return (0);
  328 }
  329 
  330 /*
  331  * Vnode close call
  332  *
  333  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
  334  */
  335 int
  336 vn_close(vp, flags, cred, p)
  337         struct vnode *vp;
  338         int flags;
  339         struct ucred *cred;
  340         struct proc *p;
  341 {
  342         int error;
  343 
  344         if (flags & FWRITE)
  345                 vp->v_writecount--;
  346         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  347         error = VOP_CLOSE(vp, flags, cred, p);
  348         vput(vp);
  349         return (error);
  350 }
  351 
  352 /*
  353  * Package up an I/O request on a vnode into a uio and do it.
  354  */
  355 int
  356 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
  357         enum uio_rw rw;
  358         struct vnode *vp;
  359         caddr_t base;
  360         int len;
  361         off_t offset;
  362         enum uio_seg segflg;
  363         int ioflg;
  364         struct ucred *cred;
  365         size_t *aresid;
  366         struct proc *p;
  367 {
  368         struct uio auio;
  369         struct iovec aiov;
  370         struct mount *mp;
  371         int error;
  372 
  373         if ((ioflg & IO_NODELOCKED) == 0) {
  374                 if (rw == UIO_READ) {
  375                         vn_lock(vp, LK_SHARED | LK_RETRY);
  376                 } else /* UIO_WRITE */ {
  377                         if (vp->v_type != VCHR &&
  378                             (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
  379                             != 0)
  380                                 return (error);
  381                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  382                 }
  383         }
  384         auio.uio_iov = &aiov;
  385         auio.uio_iovcnt = 1;
  386         aiov.iov_base = base;
  387         aiov.iov_len = len;
  388         auio.uio_resid = len;
  389         auio.uio_offset = offset;
  390         auio.uio_segflg = segflg;
  391         auio.uio_rw = rw;
  392         auio.uio_procp = p;
  393         if (rw == UIO_READ) {
  394                 error = VOP_READ(vp, &auio, ioflg, cred);
  395         } else {
  396                 error = VOP_WRITE(vp, &auio, ioflg, cred);
  397         }
  398         if (aresid)
  399                 *aresid = auio.uio_resid;
  400         else
  401                 if (auio.uio_resid && error == 0)
  402                         error = EIO;
  403         if ((ioflg & IO_NODELOCKED) == 0) {
  404                 if (rw == UIO_WRITE)
  405                         vn_finished_write(mp, 0);
  406                 VOP_UNLOCK(vp, 0);
  407         }
  408         return (error);
  409 }
  410 
  411 int
  412 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
  413         struct file *fp;
  414         char *buf;
  415         int segflg, *done, *ncookies;
  416         u_int count;
  417         struct proc *p;
  418         off_t **cookies;
  419 {
  420         struct vnode *vp = (struct vnode *)fp->f_data;
  421         struct iovec aiov;
  422         struct uio auio;
  423         int error, eofflag;
  424 
  425         /* Limit the size on any kernel buffers used by VOP_READDIR */
  426         count = min(MAXBSIZE, count);
  427 
  428 unionread:
  429         if (vp->v_type != VDIR)
  430                 return (EINVAL);
  431         aiov.iov_base = buf;
  432         aiov.iov_len = count;
  433         auio.uio_iov = &aiov;
  434         auio.uio_iovcnt = 1;
  435         auio.uio_rw = UIO_READ;
  436         auio.uio_segflg = segflg;
  437         auio.uio_procp = p;
  438         auio.uio_resid = count;
  439         vn_lock(vp, LK_SHARED | LK_RETRY);
  440         auio.uio_offset = fp->f_offset;
  441         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
  442                     ncookies);
  443         fp->f_offset = auio.uio_offset;
  444         VOP_UNLOCK(vp, 0);
  445         if (error)
  446                 return (error);
  447 
  448 #if defined(UNION) || defined(LKM)
  449         if (count == auio.uio_resid && vn_union_readdir_hook) {
  450                 struct vnode *ovp = vp;
  451 
  452                 error = (*vn_union_readdir_hook)(&vp, fp, p);
  453                 if (error)
  454                         return (error);
  455                 if (vp != ovp)
  456                         goto unionread;
  457         }
  458 #endif /* UNION || LKM */
  459 
  460         if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
  461             (vp->v_mount->mnt_flag & MNT_UNION)) {
  462                 struct vnode *tvp = vp;
  463                 vp = vp->v_mount->mnt_vnodecovered;
  464                 VREF(vp);
  465                 fp->f_data = vp;
  466                 fp->f_offset = 0;
  467                 vrele(tvp);
  468                 goto unionread;
  469         }
  470         *done = count - auio.uio_resid;
  471         return error;
  472 }
  473 
  474 /*
  475  * File table vnode read routine.
  476  */
  477 static int
  478 vn_read(fp, offset, uio, cred, flags)
  479         struct file *fp;
  480         off_t *offset;
  481         struct uio *uio;
  482         struct ucred *cred;
  483         int flags;
  484 {
  485         struct vnode *vp = (struct vnode *)fp->f_data;
  486         int count, error, ioflag = 0;
  487 
  488         VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
  489         if (fp->f_flag & FNONBLOCK)
  490                 ioflag |= IO_NDELAY;
  491         if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
  492                 ioflag |= IO_SYNC;
  493         if (fp->f_flag & FALTIO)
  494                 ioflag |= IO_ALTSEMANTICS;
  495         vn_lock(vp, LK_SHARED | LK_RETRY);
  496         uio->uio_offset = *offset;
  497         count = uio->uio_resid;
  498         error = VOP_READ(vp, uio, ioflag, cred);
  499         if (flags & FOF_UPDATE_OFFSET)
  500                 *offset += count - uio->uio_resid;
  501         VOP_UNLOCK(vp, 0);
  502         return (error);
  503 }
  504 
  505 /*
  506  * File table vnode write routine.
  507  */
  508 static int
  509 vn_write(fp, offset, uio, cred, flags)
  510         struct file *fp;
  511         off_t *offset;
  512         struct uio *uio;
  513         struct ucred *cred;
  514         int flags;
  515 {
  516         struct vnode *vp = (struct vnode *)fp->f_data;
  517         struct mount *mp;
  518         int count, error, ioflag = IO_UNIT;
  519 
  520         if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
  521                 ioflag |= IO_APPEND;
  522         if (fp->f_flag & FNONBLOCK)
  523                 ioflag |= IO_NDELAY;
  524         if (fp->f_flag & FFSYNC ||
  525             (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
  526                 ioflag |= IO_SYNC;
  527         else if (fp->f_flag & FDSYNC)
  528                 ioflag |= IO_DSYNC;
  529         if (fp->f_flag & FALTIO)
  530                 ioflag |= IO_ALTSEMANTICS;
  531         mp = NULL;
  532         if (vp->v_type != VCHR &&
  533             (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
  534                 return (error);
  535         VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
  536         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  537         uio->uio_offset = *offset;
  538         count = uio->uio_resid;
  539         error = VOP_WRITE(vp, uio, ioflag, cred);
  540         if (flags & FOF_UPDATE_OFFSET) {
  541                 if (ioflag & IO_APPEND)
  542                         *offset = uio->uio_offset;
  543                 else
  544                         *offset += count - uio->uio_resid;
  545         }
  546         VOP_UNLOCK(vp, 0);
  547         vn_finished_write(mp, 0);
  548         return (error);
  549 }
  550 
  551 /*
  552  * File table vnode stat routine.
  553  */
  554 static int
  555 vn_statfile(fp, sb, p)
  556         struct file *fp;
  557         struct stat *sb;
  558         struct proc *p;
  559 {
  560         struct vnode *vp = (struct vnode *)fp->f_data;
  561 
  562         return vn_stat(vp, sb, p);
  563 }
  564 
  565 int
  566 vn_stat(vp, sb, p)
  567         struct vnode *vp;
  568         struct stat *sb;
  569         struct proc *p;
  570 {
  571         struct vattr va;
  572         int error;
  573         mode_t mode;
  574 
  575         error = VOP_GETATTR(vp, &va, p->p_ucred, p);
  576         if (error)
  577                 return (error);
  578         /*
  579          * Copy from vattr table
  580          */
  581         sb->st_dev = va.va_fsid;
  582         sb->st_ino = va.va_fileid;
  583         mode = va.va_mode;
  584         switch (vp->v_type) {
  585         case VREG:
  586                 mode |= S_IFREG;
  587                 break;
  588         case VDIR:
  589                 mode |= S_IFDIR;
  590                 break;
  591         case VBLK:
  592                 mode |= S_IFBLK;
  593                 break;
  594         case VCHR:
  595                 mode |= S_IFCHR;
  596                 break;
  597         case VLNK:
  598                 mode |= S_IFLNK;
  599                 break;
  600         case VSOCK:
  601                 mode |= S_IFSOCK;
  602                 break;
  603         case VFIFO:
  604                 mode |= S_IFIFO;
  605                 break;
  606         default:
  607                 return (EBADF);
  608         };
  609         sb->st_mode = mode;
  610         sb->st_nlink = va.va_nlink;
  611         sb->st_uid = va.va_uid;
  612         sb->st_gid = va.va_gid;
  613         sb->st_rdev = va.va_rdev;
  614         sb->st_size = va.va_size;
  615         sb->st_atimespec = va.va_atime;
  616         sb->st_mtimespec = va.va_mtime;
  617         sb->st_ctimespec = va.va_ctime;
  618         sb->st_birthtimespec = va.va_birthtime;
  619         sb->st_blksize = va.va_blocksize;
  620         sb->st_flags = va.va_flags;
  621         sb->st_gen = 0;
  622         sb->st_blocks = va.va_bytes / S_BLKSIZE;
  623         return (0);
  624 }
  625 
  626 /*
  627  * File table vnode fcntl routine.
  628  */
  629 static int
  630 vn_fcntl(fp, com, data, p)
  631         struct file *fp;
  632         u_int com;
  633         void *data;
  634         struct proc *p;
  635 {
  636         struct vnode *vp = ((struct vnode *)fp->f_data);
  637         int error;
  638 
  639         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  640         error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
  641         VOP_UNLOCK(vp, 0);
  642         return (error);
  643 }
  644 
  645 /*
  646  * File table vnode ioctl routine.
  647  */
  648 static int
  649 vn_ioctl(fp, com, data, p)
  650         struct file *fp;
  651         u_long com;
  652         void *data;
  653         struct proc *p;
  654 {
  655         struct vnode *vp = ((struct vnode *)fp->f_data);
  656         struct vattr vattr;
  657         int error;
  658 
  659         switch (vp->v_type) {
  660 
  661         case VREG:
  662         case VDIR:
  663                 if (com == FIONREAD) {
  664                         error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
  665                         if (error)
  666                                 return (error);
  667                         *(int *)data = vattr.va_size - fp->f_offset;
  668                         return (0);
  669                 }
  670                 if ((com == FIONWRITE) || (com == FIONSPACE)) {
  671                         /*
  672                          * Files don't have send queues, so there never
  673                          * are any bytes in them, nor is there any
  674                          * open space in them.
  675                          */
  676                         *(int *)data = 0;
  677                         return (0);
  678                 }
  679                 if (com == FIOGETBMAP) {
  680                         daddr_t *block;
  681 
  682                         if (*(daddr_t *)data < 0)
  683                                 return (EINVAL);
  684                         block = (daddr_t *)data;
  685                         return (VOP_BMAP(vp, *block, NULL, block, NULL));
  686                 }
  687                 if (com == OFIOGETBMAP) {
  688                         daddr_t ibn, obn;
  689 
  690                         if (*(int32_t *)data < 0)
  691                                 return (EINVAL);
  692                         ibn = (daddr_t)*(int32_t *)data;
  693                         error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
  694                         *(int32_t *)data = (int32_t)obn;
  695                         return error;
  696                 }
  697                 if (com == FIONBIO || com == FIOASYNC)  /* XXX */
  698                         return (0);                     /* XXX */
  699                 /* fall into ... */
  700         case VFIFO:
  701         case VCHR:
  702         case VBLK:
  703                 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
  704                 if (error == 0 && com == TIOCSCTTY) {
  705                         if (p->p_session->s_ttyvp)
  706                                 vrele(p->p_session->s_ttyvp);
  707                         p->p_session->s_ttyvp = vp;
  708                         VREF(vp);
  709                 }
  710                 return (error);
  711 
  712         default:
  713                 return (EPASSTHROUGH);
  714         }
  715 }
  716 
  717 /*
  718  * File table vnode poll routine.
  719  */
  720 static int
  721 vn_poll(fp, events, p)
  722         struct file *fp;
  723         int events;
  724         struct proc *p;
  725 {
  726 
  727         return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
  728 }
  729 
  730 /*
  731  * File table vnode kqfilter routine.
  732  */
  733 int
  734 vn_kqfilter(fp, kn)
  735         struct file *fp;
  736         struct knote *kn;
  737 {
  738 
  739         return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
  740 }
  741 
  742 /*
  743  * Check that the vnode is still valid, and if so
  744  * acquire requested lock.
  745  */
  746 int
  747 vn_lock(vp, flags)
  748         struct vnode *vp;
  749         int flags;
  750 {
  751         int error;
  752 
  753 #if 0
  754         KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
  755             || (vp->v_flag & VONWORKLST) != 0);
  756 #endif
  757 
  758         do {
  759                 if ((flags & LK_INTERLOCK) == 0)
  760                         simple_lock(&vp->v_interlock);
  761                 if (vp->v_flag & VXLOCK) {
  762                         if (flags & LK_NOWAIT) {
  763                                 simple_unlock(&vp->v_interlock);
  764                                 return EBUSY;
  765                         }
  766                         vp->v_flag |= VXWANT;
  767                         ltsleep(vp, PINOD | PNORELOCK,
  768                             "vn_lock", 0, &vp->v_interlock);
  769                         error = ENOENT;
  770                 } else {
  771                         error = VOP_LOCK(vp,
  772                             (flags & ~LK_RETRY) | LK_INTERLOCK);
  773                         if (error == 0 || error == EDEADLK || error == EBUSY)
  774                                 return (error);
  775                 }
  776                 flags &= ~LK_INTERLOCK;
  777         } while (flags & LK_RETRY);
  778         return (error);
  779 }
  780 
  781 /*
  782  * File table vnode close routine.
  783  */
  784 static int
  785 vn_closefile(fp, p)
  786         struct file *fp;
  787         struct proc *p;
  788 {
  789 
  790         return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
  791                 fp->f_cred, p));
  792 }
  793 
  794 /*
  795  * Enable LK_CANRECURSE on lock. Return prior status.
  796  */
  797 u_int
  798 vn_setrecurse(vp)
  799         struct vnode *vp;
  800 {
  801         struct lock *lkp = &vp->v_lock;
  802         u_int retval = lkp->lk_flags & LK_CANRECURSE;
  803 
  804         lkp->lk_flags |= LK_CANRECURSE;
  805         return retval;
  806 }
  807 
  808 /*
  809  * Called when done with locksetrecurse.
  810  */
  811 void
  812 vn_restorerecurse(vp, flags)
  813         struct vnode *vp;
  814         u_int flags;
  815 {
  816         struct lock *lkp = &vp->v_lock;
  817 
  818         lkp->lk_flags &= ~LK_CANRECURSE;
  819         lkp->lk_flags |= flags;
  820 }
  821 
  822 int
  823 vn_cow_establish(struct vnode *vp,
  824     int (*func)(void *, struct buf *), void *cookie)
  825 {
  826         int s;
  827         struct spec_cow_entry *e;
  828 
  829         MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
  830             M_DEVBUF, M_WAITOK);
  831         e->ce_func = func;
  832         e->ce_cookie = cookie;
  833 
  834         SPEC_COW_LOCK(vp->v_specinfo, s);
  835         vp->v_spec_cow_req++;
  836         while (vp->v_spec_cow_count > 0)
  837                 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
  838                     &vp->v_spec_cow_slock);
  839 
  840         SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
  841 
  842         vp->v_spec_cow_req--;
  843         if (vp->v_spec_cow_req == 0)
  844                 wakeup(&vp->v_spec_cow_req);
  845         SPEC_COW_UNLOCK(vp->v_specinfo, s);
  846 
  847         return 0;
  848 }
  849 
  850 int
  851 vn_cow_disestablish(struct vnode *vp,
  852     int (*func)(void *, struct buf *), void *cookie)
  853 {
  854         int s;
  855         struct spec_cow_entry *e;
  856 
  857         SPEC_COW_LOCK(vp->v_specinfo, s);
  858         vp->v_spec_cow_req++;
  859         while (vp->v_spec_cow_count > 0)
  860                 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
  861                     &vp->v_spec_cow_slock);
  862 
  863         SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
  864                 if (e->ce_func == func && e->ce_cookie == cookie) {
  865                         SLIST_REMOVE(&vp->v_spec_cow_head, e,
  866                             spec_cow_entry, ce_list);
  867                         FREE(e, M_DEVBUF);
  868                         break;
  869                 }
  870 
  871         vp->v_spec_cow_req--;
  872         if (vp->v_spec_cow_req == 0)
  873                 wakeup(&vp->v_spec_cow_req);
  874         SPEC_COW_UNLOCK(vp->v_specinfo, s);
  875 
  876         return e ? 0 : EINVAL;
  877 }
  878 
  879 /*
  880  * Simplified in-kernel wrapper calls for extended attribute access.
  881  * Both calls pass in a NULL credential, authorizing a "kernel" access.
  882  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
  883  */
  884 int
  885 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
  886     const char *attrname, size_t *buflen, void *buf, struct proc *p)
  887 {
  888         struct uio auio;
  889         struct iovec aiov;
  890         int error;
  891 
  892         aiov.iov_len = *buflen;
  893         aiov.iov_base = buf;
  894 
  895         auio.uio_iov = &aiov;
  896         auio.uio_iovcnt = 1;
  897         auio.uio_rw = UIO_READ;
  898         auio.uio_segflg = UIO_SYSSPACE;
  899         auio.uio_procp = p;
  900         auio.uio_offset = 0;
  901         auio.uio_resid = *buflen;
  902 
  903         if ((ioflg & IO_NODELOCKED) == 0)
  904                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  905 
  906         error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
  907             p);
  908 
  909         if ((ioflg & IO_NODELOCKED) == 0)
  910                 VOP_UNLOCK(vp, 0);
  911 
  912         if (error == 0)
  913                 *buflen = *buflen - auio.uio_resid;
  914 
  915         return (error);
  916 }
  917 
  918 /*
  919  * XXX Failure mode if partially written?
  920  */
  921 int
  922 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
  923     const char *attrname, size_t buflen, const void *buf, struct proc *p)
  924 {
  925         struct uio auio;
  926         struct iovec aiov;
  927         struct mount *mp;
  928         int error;
  929 
  930         aiov.iov_len = buflen;
  931         aiov.iov_base = (caddr_t) buf;          /* XXX kills const */
  932 
  933         auio.uio_iov = &aiov;
  934         auio.uio_iovcnt = 1;
  935         auio.uio_rw = UIO_WRITE;
  936         auio.uio_segflg = UIO_SYSSPACE;
  937         auio.uio_procp = p;
  938         auio.uio_offset = 0;
  939         auio.uio_resid = buflen;
  940 
  941         if ((ioflg & IO_NODELOCKED) == 0) {
  942                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
  943                         return (error);
  944                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  945         }
  946 
  947         error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p);
  948 
  949         if ((ioflg & IO_NODELOCKED) == 0) {
  950                 vn_finished_write(mp, 0);
  951                 VOP_UNLOCK(vp, 0);
  952         }
  953 
  954         return (error);
  955 }
  956 
  957 int
  958 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
  959     const char *attrname, struct proc *p)
  960 {
  961         struct mount *mp;
  962         int error;
  963 
  964         if ((ioflg & IO_NODELOCKED) == 0) {
  965                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
  966                         return (error);
  967                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  968         }
  969 
  970         error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, p);
  971         if (error == EOPNOTSUPP)
  972                 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
  973                     NULL, p);
  974 
  975         if ((ioflg & IO_NODELOCKED) == 0) {
  976                 vn_finished_write(mp, 0);
  977                 VOP_UNLOCK(vp, 0);
  978         }
  979 
  980         return (error);
  981 }

Cache object: 117340fd7363f7f111659f93d98d1be4


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