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/fs/msdosfs/msdosfs_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: msdosfs_vnops.c,v 1.34.2.2 2008/02/01 14:50:26 riz Exp $       */
    2 
    3 /*-
    4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
    5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
    6  * All rights reserved.
    7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. All advertising materials mentioning features or use of this software
   18  *    must display the following acknowledgement:
   19  *      This product includes software developed by TooLs GmbH.
   20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
   21  *    derived from this software without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
   24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 /*
   35  * Written by Paul Popelka (paulp@uts.amdahl.com)
   36  *
   37  * You can do anything you want with this software, just don't say you wrote
   38  * it, and don't remove this notice.
   39  *
   40  * This software is provided "as is".
   41  *
   42  * The author supplies this software to be publicly redistributed on the
   43  * understanding that the author is not responsible for the correct
   44  * functioning of this software in any circumstances and is not liable for
   45  * any damages caused by this software.
   46  *
   47  * October 1992
   48  */
   49 
   50 #include <sys/cdefs.h>
   51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.34.2.2 2008/02/01 14:50:26 riz Exp $");
   52 
   53 #include <sys/param.h>
   54 #include <sys/systm.h>
   55 #include <sys/namei.h>
   56 #include <sys/resourcevar.h>    /* defines plimit structure in proc struct */
   57 #include <sys/kernel.h>
   58 #include <sys/file.h>           /* define FWRITE ... */
   59 #include <sys/stat.h>
   60 #include <sys/buf.h>
   61 #include <sys/proc.h>
   62 #include <sys/mount.h>
   63 #include <sys/vnode.h>
   64 #include <sys/signalvar.h>
   65 #include <sys/malloc.h>
   66 #include <sys/dirent.h>
   67 #include <sys/lockf.h>
   68 #include <sys/kauth.h>
   69 
   70 #include <miscfs/genfs/genfs.h>
   71 #include <miscfs/specfs/specdev.h> /* XXX */    /* defines v_rdev */
   72 
   73 #include <uvm/uvm_extern.h>
   74 
   75 #include <fs/msdosfs/bpb.h>
   76 #include <fs/msdosfs/direntry.h>
   77 #include <fs/msdosfs/denode.h>
   78 #include <fs/msdosfs/msdosfsmount.h>
   79 #include <fs/msdosfs/fat.h>
   80 
   81 /*
   82  * Some general notes:
   83  *
   84  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
   85  * read/written using the vnode for the filesystem. Blocks that represent
   86  * the contents of a file are read/written using the vnode for the file
   87  * (including directories when they are read/written as files). This
   88  * presents problems for the dos filesystem because data that should be in
   89  * an inode (if dos had them) resides in the directory itself.  Since we
   90  * must update directory entries without the benefit of having the vnode
   91  * for the directory we must use the vnode for the filesystem.  This means
   92  * that when a directory is actually read/written (via read, write, or
   93  * readdir, or seek) we must use the vnode for the filesystem instead of
   94  * the vnode for the directory as would happen in ufs. This is to insure we
   95  * retrieve the correct block from the buffer cache since the hash value is
   96  * based upon the vnode address and the desired block number.
   97  */
   98 
   99 /*
  100  * Create a regular file. On entry the directory to contain the file being
  101  * created is locked.  We must release before we return. We must also free
  102  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
  103  * only if the SAVESTART bit in cn_flags is clear on success.
  104  */
  105 int
  106 msdosfs_create(v)
  107         void *v;
  108 {
  109         struct vop_create_args /* {
  110                 struct vnode *a_dvp;
  111                 struct vnode **a_vpp;
  112                 struct componentname *a_cnp;
  113                 struct vattr *a_vap;
  114         } */ *ap = v;
  115         struct componentname *cnp = ap->a_cnp;
  116         struct denode ndirent;
  117         struct denode *dep;
  118         struct denode *pdep = VTODE(ap->a_dvp);
  119         int error;
  120 
  121 #ifdef MSDOSFS_DEBUG
  122         printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
  123 #endif
  124 
  125         /*
  126          * If this is the root directory and there is no space left we
  127          * can't do anything.  This is because the root directory can not
  128          * change size.
  129          */
  130         if (pdep->de_StartCluster == MSDOSFSROOT
  131             && pdep->de_fndoffset >= pdep->de_FileSize) {
  132                 error = ENOSPC;
  133                 goto bad;
  134         }
  135 
  136         /*
  137          * Create a directory entry for the file, then call createde() to
  138          * have it installed. NOTE: DOS files are always executable.  We
  139          * use the absence of the owner write bit to make the file
  140          * readonly.
  141          */
  142 #ifdef DIAGNOSTIC
  143         if ((cnp->cn_flags & HASBUF) == 0)
  144                 panic("msdosfs_create: no name");
  145 #endif
  146         memset(&ndirent, 0, sizeof(ndirent));
  147         if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
  148                 goto bad;
  149 
  150         ndirent.de_Attributes = (ap->a_vap->va_mode & S_IWUSR) ?
  151                                 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
  152         ndirent.de_StartCluster = 0;
  153         ndirent.de_FileSize = 0;
  154         ndirent.de_dev = pdep->de_dev;
  155         ndirent.de_devvp = pdep->de_devvp;
  156         ndirent.de_pmp = pdep->de_pmp;
  157         ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
  158         DETIMES(&ndirent, NULL, NULL, NULL, pdep->de_pmp->pm_gmtoff);
  159         if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
  160                 goto bad;
  161         if ((cnp->cn_flags & SAVESTART) == 0)
  162                 PNBUF_PUT(cnp->cn_pnbuf);
  163         VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  164         vput(ap->a_dvp);
  165         *ap->a_vpp = DETOV(dep);
  166         return (0);
  167 
  168 bad:
  169         PNBUF_PUT(cnp->cn_pnbuf);
  170         vput(ap->a_dvp);
  171         return (error);
  172 }
  173 
  174 int
  175 msdosfs_mknod(v)
  176         void *v;
  177 {
  178         struct vop_mknod_args /* {
  179                 struct vnode *a_dvp;
  180                 struct vnode **a_vpp;
  181                 struct componentname *a_cnp;
  182                 struct vattr *a_vap;
  183         } */ *ap = v;
  184 
  185         PNBUF_PUT(ap->a_cnp->cn_pnbuf);
  186         vput(ap->a_dvp);
  187         return (EINVAL);
  188 }
  189 
  190 int
  191 msdosfs_open(void *v)
  192 {
  193 #if 0
  194         struct vop_open_args /* {
  195                 struct vnode *a_vp;
  196                 int a_mode;
  197                 kauth_cred_t a_cred;
  198                 struct lwp *a_l;
  199         } */ *ap;
  200 #endif
  201 
  202         return (0);
  203 }
  204 
  205 int
  206 msdosfs_close(v)
  207         void *v;
  208 {
  209         struct vop_close_args /* {
  210                 struct vnode *a_vp;
  211                 int a_fflag;
  212                 kauth_cred_t a_cred;
  213                 struct lwp *a_l;
  214         } */ *ap = v;
  215         struct vnode *vp = ap->a_vp;
  216         struct denode *dep = VTODE(vp);
  217 
  218         simple_lock(&vp->v_interlock);
  219         if (vp->v_usecount > 1)
  220                 DETIMES(dep, NULL, NULL, NULL, dep->de_pmp->pm_gmtoff);
  221         simple_unlock(&vp->v_interlock);
  222         return (0);
  223 }
  224 
  225 int
  226 msdosfs_access(v)
  227         void *v;
  228 {
  229         struct vop_access_args /* {
  230                 struct vnode *a_vp;
  231                 int a_mode;
  232                 kauth_cred_t a_cred;
  233                 struct lwp *a_l;
  234         } */ *ap = v;
  235         struct vnode *vp = ap->a_vp;
  236         struct denode *dep = VTODE(vp);
  237         struct msdosfsmount *pmp = dep->de_pmp;
  238         mode_t mode = ap->a_mode;
  239 
  240         /*
  241          * Disallow write attempts on read-only file systems;
  242          * unless the file is a socket, fifo, or a block or
  243          * character device resident on the file system.
  244          */
  245         if (mode & VWRITE) {
  246                 switch (vp->v_type) {
  247                 case VDIR:
  248                 case VLNK:
  249                 case VREG:
  250                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  251                                 return (EROFS);
  252                 default:
  253                         break;
  254                 }
  255         }
  256 
  257         if ((dep->de_Attributes & ATTR_READONLY) == 0)
  258                 mode = S_IRWXU|S_IRWXG|S_IRWXO;
  259         else
  260                 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
  261         return (vaccess(ap->a_vp->v_type,
  262             mode & (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask),
  263             pmp->pm_uid, pmp->pm_gid, ap->a_mode, ap->a_cred));
  264 }
  265 
  266 int
  267 msdosfs_getattr(v)
  268         void *v;
  269 {
  270         struct vop_getattr_args /* {
  271                 struct vnode *a_vp;
  272                 struct vattr *a_vap;
  273                 kauth_cred_t a_cred;
  274                 struct lwp *a_l;
  275         } */ *ap = v;
  276         struct denode *dep = VTODE(ap->a_vp);
  277         struct msdosfsmount *pmp = dep->de_pmp;
  278         struct vattr *vap = ap->a_vap;
  279         mode_t mode;
  280         u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
  281         ino_t fileid;
  282 
  283         DETIMES(dep, NULL, NULL, NULL, pmp->pm_gmtoff);
  284         vap->va_fsid = dep->de_dev;
  285         /*
  286          * The following computation of the fileid must be the same as that
  287          * used in msdosfs_readdir() to compute d_fileno. If not, pwd
  288          * doesn't work.
  289          */
  290         if (dep->de_Attributes & ATTR_DIRECTORY) {
  291                 fileid = cntobn(pmp, (ino_t)dep->de_StartCluster) * dirsperblk;
  292                 if (dep->de_StartCluster == MSDOSFSROOT)
  293                         fileid = 1;
  294         } else {
  295                 fileid = cntobn(pmp, (ino_t)dep->de_dirclust) * dirsperblk;
  296                 if (dep->de_dirclust == MSDOSFSROOT)
  297                         fileid = roottobn(pmp, 0) * dirsperblk;
  298                 fileid += dep->de_diroffset / sizeof(struct direntry);
  299         }
  300         vap->va_fileid = fileid;
  301         if ((dep->de_Attributes & ATTR_READONLY) == 0)
  302                 mode = S_IRWXU|S_IRWXG|S_IRWXO;
  303         else
  304                 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
  305         vap->va_mode =
  306             mode & (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
  307         vap->va_uid = pmp->pm_uid;
  308         vap->va_gid = pmp->pm_gid;
  309         vap->va_nlink = 1;
  310         vap->va_rdev = 0;
  311         vap->va_size = ap->a_vp->v_size;
  312         dos2unixtime(dep->de_MDate, dep->de_MTime, 0, pmp->pm_gmtoff,
  313             &vap->va_mtime);
  314         if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
  315                 dos2unixtime(dep->de_ADate, 0, 0, pmp->pm_gmtoff,
  316                     &vap->va_atime);
  317                 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun,
  318                     pmp->pm_gmtoff, &vap->va_ctime);
  319         } else {
  320                 vap->va_atime = vap->va_mtime;
  321                 vap->va_ctime = vap->va_mtime;
  322         }
  323         vap->va_flags = 0;
  324         if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
  325                 vap->va_mode  |= S_ARCH1;
  326         vap->va_gen = 0;
  327         vap->va_blocksize = pmp->pm_bpcluster;
  328         vap->va_bytes =
  329             (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
  330         vap->va_type = ap->a_vp->v_type;
  331         return (0);
  332 }
  333 
  334 int
  335 msdosfs_setattr(v)
  336         void *v;
  337 {
  338         struct vop_setattr_args /* {
  339                 struct vnode *a_vp;
  340                 struct vattr *a_vap;
  341                 kauth_cred_t a_cred;
  342                 struct lwp *a_l;
  343         } */ *ap = v;
  344         int error = 0, de_changed = 0;
  345         struct denode *dep = VTODE(ap->a_vp);
  346         struct msdosfsmount *pmp = dep->de_pmp;
  347         struct vnode *vp  = ap->a_vp;
  348         struct vattr *vap = ap->a_vap;
  349         kauth_cred_t cred = ap->a_cred;
  350 
  351 #ifdef MSDOSFS_DEBUG
  352         printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
  353             ap->a_vp, vap, cred, ap->a_l);
  354 #endif
  355         /*
  356          * Note we silently ignore uid or gid changes.
  357          */
  358         if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
  359             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
  360             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
  361             (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL) ||
  362             (vap->va_uid != VNOVAL && vap->va_uid != pmp->pm_uid) ||
  363             (vap->va_gid != VNOVAL && vap->va_gid != pmp->pm_gid)) {
  364 #ifdef MSDOSFS_DEBUG
  365                 printf("msdosfs_setattr(): returning EINVAL\n");
  366                 printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %llx\n",
  367                     vap->va_type, vap->va_nlink, vap->va_fsid,
  368                     (unsigned long long)vap->va_fileid);
  369                 printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
  370                     vap->va_blocksize, vap->va_rdev, (long long)vap->va_bytes, vap->va_gen);
  371 #endif
  372                 return (EINVAL);
  373         }
  374         /*
  375          * Silently ignore attributes modifications on directories.
  376          */
  377         if (ap->a_vp->v_type == VDIR)
  378                 return 0;
  379 
  380         if (vap->va_size != VNOVAL) {
  381                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  382                         return (EROFS);
  383                 error = detrunc(dep, (u_long)vap->va_size, 0, cred, ap->a_l);
  384                 if (error)
  385                         return (error);
  386                 de_changed = 1;
  387         }
  388         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
  389                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  390                         return (EROFS);
  391                 if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
  392                     (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
  393                     &ap->a_l->l_acflag)) &&
  394                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
  395                     (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_l))))
  396                         return (error);
  397                 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
  398                     vap->va_atime.tv_sec != VNOVAL)
  399                         unix2dostime(&vap->va_atime, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
  400                 if (vap->va_mtime.tv_sec != VNOVAL)
  401                         unix2dostime(&vap->va_mtime, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
  402                 dep->de_Attributes |= ATTR_ARCHIVE;
  403                 dep->de_flag |= DE_MODIFIED;
  404                 de_changed = 1;
  405         }
  406         /*
  407          * DOS files only have the ability to have their writability
  408          * attribute set, so we use the owner write bit to set the readonly
  409          * attribute.
  410          */
  411         if (vap->va_mode != (mode_t)VNOVAL) {
  412                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  413                         return (EROFS);
  414                 if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
  415                     (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
  416                     &ap->a_l->l_acflag)))
  417                         return (error);
  418                 /* We ignore the read and execute bits. */
  419                 if (vap->va_mode & S_IWUSR)
  420                         dep->de_Attributes &= ~ATTR_READONLY;
  421                 else
  422                         dep->de_Attributes |= ATTR_READONLY;
  423                 dep->de_flag |= DE_MODIFIED;
  424                 de_changed = 1;
  425         }
  426         /*
  427          * Allow the `archived' bit to be toggled.
  428          */
  429         if (vap->va_flags != VNOVAL) {
  430                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  431                         return (EROFS);
  432                 if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
  433                     (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
  434                     &ap->a_l->l_acflag)))
  435                         return (error);
  436                 if (vap->va_flags & SF_ARCHIVED)
  437                         dep->de_Attributes &= ~ATTR_ARCHIVE;
  438                 else
  439                         dep->de_Attributes |= ATTR_ARCHIVE;
  440                 dep->de_flag |= DE_MODIFIED;
  441                 de_changed = 1;
  442         }
  443 
  444         if (de_changed) {
  445                 VN_KNOTE(vp, NOTE_ATTRIB);
  446                 return (deupdat(dep, 1));
  447         } else
  448                 return (0);
  449 }
  450 
  451 int
  452 msdosfs_read(v)
  453         void *v;
  454 {
  455         struct vop_read_args /* {
  456                 struct vnode *a_vp;
  457                 struct uio *a_uio;
  458                 int a_ioflag;
  459                 kauth_cred_t a_cred;
  460         } */ *ap = v;
  461         int error = 0, flags;
  462         int64_t diff;
  463         int blsize;
  464         long n;
  465         long on;
  466         daddr_t lbn;
  467         void *win;
  468         vsize_t bytelen;
  469         struct buf *bp;
  470         struct vnode *vp = ap->a_vp;
  471         struct denode *dep = VTODE(vp);
  472         struct msdosfsmount *pmp = dep->de_pmp;
  473         struct uio *uio = ap->a_uio;
  474 
  475         /*
  476          * If they didn't ask for any data, then we are done.
  477          */
  478 
  479         if (uio->uio_resid == 0)
  480                 return (0);
  481         if (uio->uio_offset < 0)
  482                 return (EINVAL);
  483         if (uio->uio_offset >= dep->de_FileSize)
  484                 return (0);
  485 
  486         if (vp->v_type == VREG) {
  487                 const int advice = IO_ADV_DECODE(ap->a_ioflag);
  488 
  489                 while (uio->uio_resid > 0) {
  490                         bytelen = MIN(dep->de_FileSize - uio->uio_offset,
  491                                       uio->uio_resid);
  492 
  493                         if (bytelen == 0)
  494                                 break;
  495                         win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
  496                                         &bytelen, advice, UBC_READ);
  497                         error = uiomove(win, bytelen, uio);
  498                         flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
  499                         ubc_release(win, flags);
  500                         if (error)
  501                                 break;
  502                 }
  503                 dep->de_flag |= DE_ACCESS;
  504                 goto out;
  505         }
  506 
  507         /* this loop is only for directories now */
  508         do {
  509                 lbn = de_cluster(pmp, uio->uio_offset);
  510                 on = uio->uio_offset & pmp->pm_crbomask;
  511                 n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
  512                 if (uio->uio_offset >= dep->de_FileSize)
  513                         return (0);
  514                 /* file size (and hence diff) may be up to 4GB */
  515                 diff = dep->de_FileSize - uio->uio_offset;
  516                 if (diff < n)
  517                         n = (long) diff;
  518 
  519                 /* convert cluster # to sector # */
  520                 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
  521                 if (error)
  522                         return (error);
  523 
  524                 /*
  525                  * If we are operating on a directory file then be sure to
  526                  * do i/o with the vnode for the filesystem instead of the
  527                  * vnode for the directory.
  528                  */
  529                 error = bread(pmp->pm_devvp, de_bn2kb(pmp, lbn), blsize,
  530                     NOCRED, &bp);
  531                 n = MIN(n, pmp->pm_bpcluster - bp->b_resid);
  532                 if (error) {
  533                         brelse(bp);
  534                         return (error);
  535                 }
  536                 error = uiomove(bp->b_data + on, (int) n, uio);
  537                 brelse(bp);
  538         } while (error == 0 && uio->uio_resid > 0 && n != 0);
  539 
  540 out:
  541         if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
  542                 error = deupdat(dep, 1);
  543         return (error);
  544 }
  545 
  546 /*
  547  * Write data to a file or directory.
  548  */
  549 int
  550 msdosfs_write(v)
  551         void *v;
  552 {
  553         struct vop_write_args /* {
  554                 struct vnode *a_vp;
  555                 struct uio *a_uio;
  556                 int a_ioflag;
  557                 kauth_cred_t a_cred;
  558         } */ *ap = v;
  559         int resid, flags, extended = 0;
  560         int error = 0;
  561         int ioflag = ap->a_ioflag;
  562         u_long osize;
  563         u_long count;
  564         void *win;
  565         vsize_t bytelen;
  566         off_t oldoff;
  567         struct uio *uio = ap->a_uio;
  568         struct proc *p = curproc;
  569         struct vnode *vp = ap->a_vp;
  570         struct denode *dep = VTODE(vp);
  571         struct msdosfsmount *pmp = dep->de_pmp;
  572         kauth_cred_t cred = ap->a_cred;
  573         boolean_t async;
  574 
  575 #ifdef MSDOSFS_DEBUG
  576         printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
  577             vp, uio, ioflag, cred);
  578         printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
  579             dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
  580 #endif
  581 
  582         switch (vp->v_type) {
  583         case VREG:
  584                 if (ioflag & IO_APPEND)
  585                         uio->uio_offset = dep->de_FileSize;
  586                 break;
  587         case VDIR:
  588                 return EISDIR;
  589         default:
  590                 panic("msdosfs_write(): bad file type");
  591         }
  592 
  593         if (uio->uio_offset < 0)
  594                 return (EINVAL);
  595 
  596         if (uio->uio_resid == 0)
  597                 return (0);
  598 
  599         /* Don't bother to try to write files larger than the fs limit */
  600         if (uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
  601                 return (EFBIG);
  602 
  603         /*
  604          * If they've exceeded their filesize limit, tell them about it.
  605          */
  606         if (((uio->uio_offset + uio->uio_resid) >
  607             p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
  608                 psignal(p, SIGXFSZ);
  609                 return (EFBIG);
  610         }
  611 
  612         /*
  613          * If the offset we are starting the write at is beyond the end of
  614          * the file, then they've done a seek.  Unix filesystems allow
  615          * files with holes in them, DOS doesn't so we must fill the hole
  616          * with zeroed blocks.
  617          */
  618         if (uio->uio_offset > dep->de_FileSize) {
  619                 if ((error = deextend(dep, uio->uio_offset, cred)) != 0)
  620                         return (error);
  621         }
  622 
  623         /*
  624          * Remember some values in case the write fails.
  625          */
  626         async = vp->v_mount->mnt_flag & MNT_ASYNC;
  627         resid = uio->uio_resid;
  628         osize = dep->de_FileSize;
  629 
  630         /*
  631          * If we write beyond the end of the file, extend it to its ultimate
  632          * size ahead of the time to hopefully get a contiguous area.
  633          */
  634         if (uio->uio_offset + resid > osize) {
  635                 count = de_clcount(pmp, uio->uio_offset + resid) -
  636                         de_clcount(pmp, osize);
  637                 if ((error = extendfile(dep, count, NULL, NULL, 0)) &&
  638                     (error != ENOSPC || (ioflag & IO_UNIT)))
  639                         goto errexit;
  640 
  641                 dep->de_FileSize = uio->uio_offset + resid;
  642                 uvm_vnp_setsize(vp, dep->de_FileSize);
  643                 extended = 1;
  644         }
  645 
  646         do {
  647                 oldoff = uio->uio_offset;
  648                 bytelen = uio->uio_resid;
  649 
  650                 win = ubc_alloc(&vp->v_uobj, oldoff, &bytelen, UVM_ADV_NORMAL,
  651                     UBC_WRITE);
  652                 error = uiomove(win, bytelen, uio);
  653                 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
  654                 ubc_release(win, flags);
  655                 if (error)
  656                         break;
  657 
  658                 /*
  659                  * flush what we just wrote if necessary.
  660                  * XXXUBC simplistic async flushing.
  661                  */
  662 
  663                 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
  664                         simple_lock(&vp->v_interlock);
  665                         error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
  666                             (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
  667                 }
  668         } while (error == 0 && uio->uio_resid > 0);
  669         if (error == 0 && ioflag & IO_SYNC) {
  670                 simple_lock(&vp->v_interlock);
  671                 error = VOP_PUTPAGES(vp, trunc_page(oldoff),
  672                     round_page(oldoff + bytelen), PGO_CLEANIT | PGO_SYNCIO);
  673         }
  674         dep->de_flag |= DE_UPDATE;
  675 
  676         /*
  677          * If the write failed and they want us to, truncate the file back
  678          * to the size it was before the write was attempted.
  679          */
  680 errexit:
  681         if (resid > uio->uio_resid)
  682                 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
  683         if (error) {
  684                 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
  685                 uio->uio_offset -= resid - uio->uio_resid;
  686                 uio->uio_resid = resid;
  687         } else if ((ioflag & IO_SYNC) == IO_SYNC)
  688                 error = deupdat(dep, 1);
  689         KASSERT(vp->v_size == dep->de_FileSize);
  690         return (error);
  691 }
  692 
  693 int
  694 msdosfs_update(struct vnode *vp, const struct timespec *acc,
  695     const struct timespec *mod, int flags)
  696 {
  697         struct buf *bp;
  698         struct direntry *dirp;
  699         struct denode *dep;
  700         int error;
  701 
  702         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  703                 return (0);
  704         dep = VTODE(vp);
  705         DETIMES(dep, acc, mod, NULL, dep->de_pmp->pm_gmtoff);
  706         if ((dep->de_flag & DE_MODIFIED) == 0)
  707                 return (0);
  708         dep->de_flag &= ~DE_MODIFIED;
  709         if (dep->de_Attributes & ATTR_DIRECTORY)
  710                 return (0);
  711         if (dep->de_refcnt <= 0)
  712                 return (0);
  713         error = readde(dep, &bp, &dirp);
  714         if (error)
  715                 return (error);
  716         DE_EXTERNALIZE(dirp, dep);
  717         if (flags & (UPDATE_WAIT|UPDATE_DIROP))
  718                 return (bwrite(bp));
  719         else {
  720                 bdwrite(bp);
  721                 return (0);
  722         }
  723 }
  724 
  725 /*
  726  * Flush the blocks of a file to disk.
  727  *
  728  * This function is worthless for vnodes that represent directories. Maybe we
  729  * could just do a sync if they try an fsync on a directory file.
  730  */
  731 int
  732 msdosfs_remove(v)
  733         void *v;
  734 {
  735         struct vop_remove_args /* {
  736                 struct vnode *a_dvp;
  737                 struct vnode *a_vp;
  738                 struct componentname *a_cnp;
  739         } */ *ap = v;
  740         struct denode *dep = VTODE(ap->a_vp);
  741         struct denode *ddep = VTODE(ap->a_dvp);
  742         int error;
  743 
  744         if (ap->a_vp->v_type == VDIR)
  745                 error = EPERM;
  746         else
  747                 error = removede(ddep, dep);
  748 #ifdef MSDOSFS_DEBUG
  749         printf("msdosfs_remove(), dep %p, v_usecount %d\n",
  750                 dep, ap->a_vp->v_usecount);
  751 #endif
  752         VN_KNOTE(ap->a_vp, NOTE_DELETE);
  753         VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  754         if (ddep == dep)
  755                 vrele(ap->a_vp);
  756         else
  757                 vput(ap->a_vp); /* causes msdosfs_inactive() to be called
  758                                  * via vrele() */
  759         vput(ap->a_dvp);
  760         return (error);
  761 }
  762 
  763 /*
  764  * DOS filesystems don't know what links are. But since we already called
  765  * msdosfs_lookup() with create and lockparent, the parent is locked so we
  766  * have to free it before we return the error.
  767  */
  768 int
  769 msdosfs_link(v)
  770         void *v;
  771 {
  772         struct vop_link_args /* {
  773                 struct vnode *a_dvp;
  774                 struct vnode *a_vp;
  775                 struct componentname *a_cnp;
  776         } */ *ap = v;
  777 
  778         VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
  779         vput(ap->a_dvp);
  780         return (EOPNOTSUPP);
  781 }
  782 
  783 /*
  784  * Renames on files require moving the denode to a new hash queue since the
  785  * denode's location is used to compute which hash queue to put the file
  786  * in. Unless it is a rename in place.  For example "mv a b".
  787  *
  788  * What follows is the basic algorithm:
  789  *
  790  * if (file move) {
  791  *      if (dest file exists) {
  792  *              remove dest file
  793  *      }
  794  *      if (dest and src in same directory) {
  795  *              rewrite name in existing directory slot
  796  *      } else {
  797  *              write new entry in dest directory
  798  *              update offset and dirclust in denode
  799  *              move denode to new hash chain
  800  *              clear old directory entry
  801  *      }
  802  * } else {
  803  *      directory move
  804  *      if (dest directory exists) {
  805  *              if (dest is not empty) {
  806  *                      return ENOTEMPTY
  807  *              }
  808  *              remove dest directory
  809  *      }
  810  *      if (dest and src in same directory) {
  811  *              rewrite name in existing entry
  812  *      } else {
  813  *              be sure dest is not a child of src directory
  814  *              write entry in dest directory
  815  *              update "." and ".." in moved directory
  816  *              update offset and dirclust in denode
  817  *              move denode to new hash chain
  818  *              clear old directory entry for moved directory
  819  *      }
  820  * }
  821  *
  822  * On entry:
  823  *      source's parent directory is unlocked
  824  *      source file or directory is unlocked
  825  *      destination's parent directory is locked
  826  *      destination file or directory is locked if it exists
  827  *
  828  * On exit:
  829  *      all denodes should be released
  830  *
  831  * Notes:
  832  * I'm not sure how the memory containing the pathnames pointed at by the
  833  * componentname structures is freed, there may be some memory bleeding
  834  * for each rename done.
  835  *
  836  * --More-- Notes:
  837  * This routine needs help.  badly.
  838  */
  839 int
  840 msdosfs_rename(v)
  841         void *v;
  842 {
  843         struct vop_rename_args /* {
  844                 struct vnode *a_fdvp;
  845                 struct vnode *a_fvp;
  846                 struct componentname *a_fcnp;
  847                 struct vnode *a_tdvp;
  848                 struct vnode *a_tvp;
  849                 struct componentname *a_tcnp;
  850         } */ *ap = v;
  851         struct vnode *tvp = ap->a_tvp;
  852         struct vnode *tdvp = ap->a_tdvp;
  853         struct vnode *fvp = ap->a_fvp;
  854         struct vnode *fdvp = ap->a_fdvp;
  855         struct componentname *tcnp = ap->a_tcnp;
  856         struct componentname *fcnp = ap->a_fcnp;
  857         struct lwp *l = tcnp->cn_lwp;
  858         struct denode *ip, *xp, *dp, *zp;
  859         u_char toname[11], oldname[11];
  860         u_long from_diroffset, to_diroffset;
  861         u_char to_count;
  862         int doingdirectory = 0, newparent = 0;
  863         int error;
  864         u_long cn;
  865         daddr_t bn;
  866         struct msdosfsmount *pmp;
  867         struct direntry *dotdotp;
  868         struct buf *bp;
  869         int fdvp_dorele = 0;
  870 
  871         pmp = VFSTOMSDOSFS(fdvp->v_mount);
  872 
  873 #ifdef DIAGNOSTIC
  874         if ((tcnp->cn_flags & HASBUF) == 0 ||
  875             (fcnp->cn_flags & HASBUF) == 0)
  876                 panic("msdosfs_rename: no name");
  877 #endif
  878         /*
  879          * Check for cross-device rename.
  880          */
  881         if ((fvp->v_mount != tdvp->v_mount) ||
  882             (tvp && (fvp->v_mount != tvp->v_mount))) {
  883                 error = EXDEV;
  884 abortit:
  885                 VOP_ABORTOP(tdvp, tcnp);
  886                 if (tdvp == tvp)
  887                         vrele(tdvp);
  888                 else
  889                         vput(tdvp);
  890                 if (tvp)
  891                         vput(tvp);
  892                 VOP_ABORTOP(fdvp, fcnp);
  893                 vrele(fdvp);
  894                 vrele(fvp);
  895                 return (error);
  896         }
  897 
  898         /*
  899          * If source and dest are the same, do nothing.
  900          */
  901         if (tvp == fvp) {
  902                 error = 0;
  903                 goto abortit;
  904         }
  905 
  906         /*
  907          * XXX: This can deadlock since we hold tdvp/tvp locked.
  908          * But I'm not going to fix it now.  If lockmgr detected the
  909          * deadlock, this might actually work... sorta.
  910          */
  911         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
  912                 goto abortit;
  913         dp = VTODE(fdvp);
  914         ip = VTODE(fvp);
  915 
  916         /*
  917          * Be sure we are not renaming ".", "..", or an alias of ".". This
  918          * leads to a crippled directory tree.  It's pretty tough to do a
  919          * "ls" or "pwd" with the "." directory entry missing, and "cd .."
  920          * doesn't work if the ".." entry is missing.
  921          */
  922         if (ip->de_Attributes & ATTR_DIRECTORY) {
  923                 /*
  924                  * Avoid ".", "..", and aliases of "." for obvious reasons.
  925                  */
  926                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
  927                     dp == ip ||
  928                     (fcnp->cn_flags & ISDOTDOT) ||
  929                     (tcnp->cn_flags & ISDOTDOT) ||
  930                     (ip->de_flag & DE_RENAME)) {
  931                         VOP_UNLOCK(fvp, 0);
  932                         error = EINVAL;
  933                         goto abortit;
  934                 }
  935                 ip->de_flag |= DE_RENAME;
  936                 doingdirectory++;
  937         }
  938         VN_KNOTE(fdvp, NOTE_WRITE);             /* XXXLUKEM/XXX: right place? */
  939 
  940         /*
  941          * When the target exists, both the directory
  942          * and target vnodes are returned locked.
  943          */
  944         dp = VTODE(tdvp);
  945         xp = tvp ? VTODE(tvp) : NULL;
  946         /*
  947          * Remember direntry place to use for destination
  948          */
  949         to_diroffset = dp->de_fndoffset;
  950         to_count = dp->de_fndcnt;
  951 
  952         /*
  953          * If ".." must be changed (ie the directory gets a new
  954          * parent) then the source directory must not be in the
  955          * directory hierarchy above the target, as this would
  956          * orphan everything below the source directory. Also
  957          * the user must have write permission in the source so
  958          * as to be able to change "..". We must repeat the call
  959          * to namei, as the parent directory is unlocked by the
  960          * call to doscheckpath().
  961          */
  962         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, l);
  963         VOP_UNLOCK(fvp, 0);
  964         if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
  965                 newparent = 1;
  966 
  967         /*
  968          * XXX: We can do this here because rename uses SAVEFART and
  969          * therefore fdvp has at least two references (one doesn't
  970          * belong to us, though, and that's evil).  We'll get
  971          * another "extra" reference when we do relookup(), so we
  972          * need to compensate.  We should *NOT* be doing this, but
  973          * it works, so whatever.
  974          */
  975         vrele(fdvp);
  976 
  977         if (doingdirectory && newparent) {
  978                 if (error)      /* write access check above */
  979                         goto tdvpbad;
  980                 if (xp != NULL)
  981                         vput(tvp);
  982                 tvp = NULL;
  983                 /*
  984                  * doscheckpath() vput()'s dp,
  985                  * so we have to do a relookup afterwards
  986                  */
  987                 if ((error = doscheckpath(ip, dp)) != 0)
  988                         goto out;
  989                 if ((tcnp->cn_flags & SAVESTART) == 0)
  990                         panic("msdosfs_rename: lost to startdir");
  991                 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
  992                 if ((error = relookup(tdvp, &tvp, tcnp)) != 0) {
  993                         VOP_UNLOCK(tdvp, 0);
  994                         goto out;
  995                 }
  996                 /*
  997                  * XXX: SAVESTART causes us to get a reference, but
  998                  * that's released already above in doscheckpath()
  999                  */
 1000                 dp = VTODE(tdvp);
 1001                 xp = tvp ? VTODE(tvp) : NULL;
 1002         }
 1003 
 1004         if (xp != NULL) {
 1005                 /*
 1006                  * Target must be empty if a directory and have no links
 1007                  * to it. Also, ensure source and target are compatible
 1008                  * (both directories, or both not directories).
 1009                  */
 1010                 if (xp->de_Attributes & ATTR_DIRECTORY) {
 1011                         if (!dosdirempty(xp)) {
 1012                                 error = ENOTEMPTY;
 1013                                 goto tdvpbad;
 1014                         }
 1015                         if (!doingdirectory) {
 1016                                 error = ENOTDIR;
 1017                                 goto tdvpbad;
 1018                         }
 1019                 } else if (doingdirectory) {
 1020                         error = EISDIR;
 1021                         goto tdvpbad;
 1022                 }
 1023                 if ((error = removede(dp, xp)) != 0)
 1024                         goto tdvpbad;
 1025                 VN_KNOTE(tdvp, NOTE_WRITE);
 1026                 VN_KNOTE(tvp, NOTE_DELETE);
 1027                 cache_purge(tvp);
 1028                 vput(tvp);
 1029                 tvp = NULL;
 1030                 xp = NULL;
 1031         }
 1032 
 1033         /*
 1034          * Convert the filename in tcnp into a dos filename. We copy this
 1035          * into the denode and directory entry for the destination
 1036          * file/directory.
 1037          */
 1038         if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0)
 1039                 goto abortit;
 1040 
 1041         /*
 1042          * Since from wasn't locked at various places above,
 1043          * have to do a relookup here.
 1044          */
 1045         fcnp->cn_flags &= ~MODMASK;
 1046         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
 1047         if ((fcnp->cn_flags & SAVESTART) == 0)
 1048                 panic("msdosfs_rename: lost from startdir");
 1049         VOP_UNLOCK(tdvp, 0);
 1050         vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
 1051         if ((error = relookup(fdvp, &fvp, fcnp))) {
 1052                 VOP_UNLOCK(fdvp, 0);
 1053                 vrele(ap->a_fvp);
 1054                 vrele(tdvp);
 1055                 return (error);
 1056         }
 1057         if (fvp == NULL) {
 1058                 /*
 1059                  * From name has disappeared.
 1060                  */
 1061                 if (doingdirectory)
 1062                         panic("rename: lost dir entry");
 1063                 vput(fdvp);
 1064                 vrele(ap->a_fvp);
 1065                 vrele(tdvp);
 1066                 return 0;
 1067         }
 1068         fdvp_dorele = 1;
 1069         VOP_UNLOCK(fdvp, 0);
 1070         xp = VTODE(fvp);
 1071         zp = VTODE(fdvp);
 1072         from_diroffset = zp->de_fndoffset;
 1073 
 1074         /*
 1075          * Ensure that the directory entry still exists and has not
 1076          * changed till now. If the source is a file the entry may
 1077          * have been unlinked or renamed. In either case there is
 1078          * no further work to be done. If the source is a directory
 1079          * then it cannot have been rmdir'ed or renamed; this is
 1080          * prohibited by the DE_RENAME flag.
 1081          */
 1082         if (xp != ip) {
 1083                 if (doingdirectory)
 1084                         panic("rename: lost dir entry");
 1085                 vrele(ap->a_fvp);
 1086                 VOP_UNLOCK(fvp, 0);
 1087                 xp = NULL;
 1088         } else {
 1089                 vrele(fvp);
 1090                 xp = NULL;
 1091 
 1092                 /*
 1093                  * First write a new entry in the destination
 1094                  * directory and mark the entry in the source directory
 1095                  * as deleted.  Then move the denode to the correct hash
 1096                  * chain for its new location in the filesystem.  And, if
 1097                  * we moved a directory, then update its .. entry to point
 1098                  * to the new parent directory.
 1099                  */
 1100                 memcpy(oldname, ip->de_Name, 11);
 1101                 memcpy(ip->de_Name, toname, 11);        /* update denode */
 1102                 dp->de_fndoffset = to_diroffset;
 1103                 dp->de_fndcnt = to_count;
 1104                 error = createde(ip, dp, (struct denode **)0, tcnp);
 1105                 if (error) {
 1106                         memcpy(ip->de_Name, oldname, 11);
 1107                         VOP_UNLOCK(fvp, 0);
 1108                         goto bad;
 1109                 }
 1110                 ip->de_refcnt++;
 1111                 zp->de_fndoffset = from_diroffset;
 1112                 if ((error = removede(zp, ip)) != 0) {
 1113                         /* XXX should really panic here, fs is corrupt */
 1114                         VOP_UNLOCK(fvp, 0);
 1115                         goto bad;
 1116                 }
 1117                 cache_purge(fvp);
 1118                 if (!doingdirectory) {
 1119                         error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
 1120                                        &ip->de_dirclust, 0);
 1121                         if (error) {
 1122                                 /* XXX should really panic here, fs is corrupt */
 1123                                 VOP_UNLOCK(fvp, 0);
 1124                                 goto bad;
 1125                         }
 1126                         ip->de_diroffset = to_diroffset;
 1127                         if (ip->de_dirclust != MSDOSFSROOT)
 1128                                 ip->de_diroffset &= pmp->pm_crbomask;
 1129                 }
 1130                 reinsert(ip);
 1131         }
 1132 
 1133         /*
 1134          * If we moved a directory to a new parent directory, then we must
 1135          * fixup the ".." entry in the moved directory.
 1136          */
 1137         if (doingdirectory && newparent) {
 1138                 cn = ip->de_StartCluster;
 1139                 if (cn == MSDOSFSROOT) {
 1140                         /* this should never happen */
 1141                         panic("msdosfs_rename: updating .. in root directory?");
 1142                 } else
 1143                         bn = cntobn(pmp, cn);
 1144                 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
 1145                     pmp->pm_bpcluster, NOCRED, &bp);
 1146                 if (error) {
 1147                         /* XXX should really panic here, fs is corrupt */
 1148                         brelse(bp);
 1149                         VOP_UNLOCK(fvp, 0);
 1150                         goto bad;
 1151                 }
 1152                 dotdotp = (struct direntry *)bp->b_data + 1;
 1153                 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
 1154                 if (FAT32(pmp)) {
 1155                         putushort(dotdotp->deHighClust,
 1156                                 dp->de_StartCluster >> 16);
 1157                 } else {
 1158                         putushort(dotdotp->deHighClust, 0);
 1159                 }
 1160                 if ((error = bwrite(bp)) != 0) {
 1161                         /* XXX should really panic here, fs is corrupt */
 1162                         VOP_UNLOCK(fvp, 0);
 1163                         goto bad;
 1164                 }
 1165         }
 1166 
 1167         VN_KNOTE(fvp, NOTE_RENAME);
 1168         VOP_UNLOCK(fvp, 0);
 1169 bad:
 1170         if (tvp)
 1171                 vput(tvp);
 1172         vrele(tdvp);
 1173 out:
 1174         ip->de_flag &= ~DE_RENAME;
 1175         if (fdvp_dorele)
 1176                 vrele(fdvp);
 1177         vrele(fvp);
 1178         return (error);
 1179 
 1180         /* XXX: uuuh */
 1181 tdvpbad:
 1182         VOP_UNLOCK(tdvp, 0);
 1183         goto bad;
 1184 }
 1185 
 1186 static const struct {
 1187         struct direntry dot;
 1188         struct direntry dotdot;
 1189 } dosdirtemplate = {
 1190         {       ".       ", "   ",                      /* the . entry */
 1191                 ATTR_DIRECTORY,                         /* file attribute */
 1192                 0,                                      /* reserved */
 1193                 0, { 0, 0 }, { 0, 0 },                  /* create time & date */
 1194                 { 0, 0 },                               /* access date */
 1195                 { 0, 0 },                               /* high bits of start cluster */
 1196                 { 210, 4 }, { 210, 4 },                 /* modify time & date */
 1197                 { 0, 0 },                               /* startcluster */
 1198                 { 0, 0, 0, 0 }                          /* filesize */
 1199         },
 1200         {       "..      ", "   ",                      /* the .. entry */
 1201                 ATTR_DIRECTORY,                         /* file attribute */
 1202                 0,                                      /* reserved */
 1203                 0, { 0, 0 }, { 0, 0 },                  /* create time & date */
 1204                 { 0, 0 },                               /* access date */
 1205                 { 0, 0 },                               /* high bits of start cluster */
 1206                 { 210, 4 }, { 210, 4 },                 /* modify time & date */
 1207                 { 0, 0 },                               /* startcluster */
 1208                 { 0, 0, 0, 0 }                          /* filesize */
 1209         }
 1210 };
 1211 
 1212 int
 1213 msdosfs_mkdir(v)
 1214         void *v;
 1215 {
 1216         struct vop_mkdir_args /* {
 1217                 struct vnode *a_dvp;
 1218                 struvt vnode **a_vpp;
 1219                 struvt componentname *a_cnp;
 1220                 struct vattr *a_vap;
 1221         } */ *ap = v;
 1222         struct componentname *cnp = ap->a_cnp;
 1223         struct denode ndirent;
 1224         struct denode *dep;
 1225         struct denode *pdep = VTODE(ap->a_dvp);
 1226         int error;
 1227         int bn;
 1228         u_long newcluster, pcl;
 1229         struct direntry *denp;
 1230         struct msdosfsmount *pmp = pdep->de_pmp;
 1231         struct buf *bp;
 1232         int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
 1233 
 1234         /*
 1235          * If this is the root directory and there is no space left we
 1236          * can't do anything.  This is because the root directory can not
 1237          * change size.
 1238          */
 1239         if (pdep->de_StartCluster == MSDOSFSROOT
 1240             && pdep->de_fndoffset >= pdep->de_FileSize) {
 1241                 error = ENOSPC;
 1242                 goto bad2;
 1243         }
 1244 
 1245         /*
 1246          * Allocate a cluster to hold the about to be created directory.
 1247          */
 1248         error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
 1249         if (error)
 1250                 goto bad2;
 1251 
 1252         memset(&ndirent, 0, sizeof(ndirent));
 1253         ndirent.de_pmp = pmp;
 1254         ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
 1255         DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
 1256 
 1257         /*
 1258          * Now fill the cluster with the "." and ".." entries. And write
 1259          * the cluster to disk.  This way it is there for the parent
 1260          * directory to be pointing at if there were a crash.
 1261          */
 1262         bn = cntobn(pmp, newcluster);
 1263         /* always succeeds */
 1264         bp = getblk(pmp->pm_devvp, de_bn2kb(pmp, bn), pmp->pm_bpcluster, 0, 0);
 1265         memset(bp->b_data, 0, pmp->pm_bpcluster);
 1266         memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
 1267         denp = (struct direntry *)bp->b_data;
 1268         putushort(denp[0].deStartCluster, newcluster);
 1269         putushort(denp[0].deCDate, ndirent.de_CDate);
 1270         putushort(denp[0].deCTime, ndirent.de_CTime);
 1271         denp[0].deCHundredth = ndirent.de_CHun;
 1272         putushort(denp[0].deADate, ndirent.de_ADate);
 1273         putushort(denp[0].deMDate, ndirent.de_MDate);
 1274         putushort(denp[0].deMTime, ndirent.de_MTime);
 1275         pcl = pdep->de_StartCluster;
 1276         if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
 1277                 pcl = 0;
 1278         putushort(denp[1].deStartCluster, pcl);
 1279         putushort(denp[1].deCDate, ndirent.de_CDate);
 1280         putushort(denp[1].deCTime, ndirent.de_CTime);
 1281         denp[1].deCHundredth = ndirent.de_CHun;
 1282         putushort(denp[1].deADate, ndirent.de_ADate);
 1283         putushort(denp[1].deMDate, ndirent.de_MDate);
 1284         putushort(denp[1].deMTime, ndirent.de_MTime);
 1285         if (FAT32(pmp)) {
 1286                 putushort(denp[0].deHighClust, newcluster >> 16);
 1287                 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
 1288         } else {
 1289                 putushort(denp[0].deHighClust, 0);
 1290                 putushort(denp[1].deHighClust, 0);
 1291         }
 1292 
 1293         if (async)
 1294                 bdwrite(bp);
 1295         else if ((error = bwrite(bp)) != 0)
 1296                 goto bad;
 1297 
 1298         /*
 1299          * Now build up a directory entry pointing to the newly allocated
 1300          * cluster.  This will be written to an empty slot in the parent
 1301          * directory.
 1302          */
 1303 #ifdef DIAGNOSTIC
 1304         if ((cnp->cn_flags & HASBUF) == 0)
 1305                 panic("msdosfs_mkdir: no name");
 1306 #endif
 1307         if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
 1308                 goto bad;
 1309 
 1310         ndirent.de_Attributes = ATTR_DIRECTORY;
 1311         ndirent.de_StartCluster = newcluster;
 1312         ndirent.de_FileSize = 0;
 1313         ndirent.de_dev = pdep->de_dev;
 1314         ndirent.de_devvp = pdep->de_devvp;
 1315         if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
 1316                 goto bad;
 1317         if ((cnp->cn_flags & SAVESTART) == 0)
 1318                 PNBUF_PUT(cnp->cn_pnbuf);
 1319         VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
 1320         vput(ap->a_dvp);
 1321         *ap->a_vpp = DETOV(dep);
 1322         return (0);
 1323 
 1324 bad:
 1325         clusterfree(pmp, newcluster, NULL);
 1326 bad2:
 1327         PNBUF_PUT(cnp->cn_pnbuf);
 1328         vput(ap->a_dvp);
 1329         return (error);
 1330 }
 1331 
 1332 int
 1333 msdosfs_rmdir(v)
 1334         void *v;
 1335 {
 1336         struct vop_rmdir_args /* {
 1337                 struct vnode *a_dvp;
 1338                 struct vnode *a_vp;
 1339                 struct componentname *a_cnp;
 1340         } */ *ap = v;
 1341         struct vnode *vp = ap->a_vp;
 1342         struct vnode *dvp = ap->a_dvp;
 1343         struct componentname *cnp = ap->a_cnp;
 1344         struct denode *ip, *dp;
 1345         int error;
 1346 
 1347         ip = VTODE(vp);
 1348         dp = VTODE(dvp);
 1349         /*
 1350          * No rmdir "." please.
 1351          */
 1352         if (dp == ip) {
 1353                 vrele(dvp);
 1354                 vput(vp);
 1355                 return (EINVAL);
 1356         }
 1357         /*
 1358          * Verify the directory is empty (and valid).
 1359          * (Rmdir ".." won't be valid since
 1360          *  ".." will contain a reference to
 1361          *  the current directory and thus be
 1362          *  non-empty.)
 1363          */
 1364         error = 0;
 1365         if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
 1366                 error = ENOTEMPTY;
 1367                 goto out;
 1368         }
 1369         /*
 1370          * Delete the entry from the directory.  For dos filesystems this
 1371          * gets rid of the directory entry on disk, the in memory copy
 1372          * still exists but the de_refcnt is <= 0.  This prevents it from
 1373          * being found by deget().  When the vput() on dep is done we give
 1374          * up access and eventually msdosfs_reclaim() will be called which
 1375          * will remove it from the denode cache.
 1376          */
 1377         if ((error = removede(dp, ip)) != 0)
 1378                 goto out;
 1379         /*
 1380          * This is where we decrement the link count in the parent
 1381          * directory.  Since dos filesystems don't do this we just purge
 1382          * the name cache and let go of the parent directory denode.
 1383          */
 1384         VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
 1385         cache_purge(dvp);
 1386         vput(dvp);
 1387         dvp = NULL;
 1388         /*
 1389          * Truncate the directory that is being deleted.
 1390          */
 1391         error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, cnp->cn_lwp);
 1392         cache_purge(vp);
 1393 out:
 1394         VN_KNOTE(vp, NOTE_DELETE);
 1395         if (dvp)
 1396                 vput(dvp);
 1397         vput(vp);
 1398         return (error);
 1399 }
 1400 
 1401 /*
 1402  * DOS filesystems don't know what symlinks are.
 1403  */
 1404 int
 1405 msdosfs_symlink(v)
 1406         void *v;
 1407 {
 1408         struct vop_symlink_args /* {
 1409                 struct vnode *a_dvp;
 1410                 struct vnode **a_vpp;
 1411                 struct componentname *a_cnp;
 1412                 struct vattr *a_vap;
 1413                 char *a_target;
 1414         } */ *ap = v;
 1415 
 1416         VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
 1417         vput(ap->a_dvp);
 1418         return (EOPNOTSUPP);
 1419 }
 1420 
 1421 int
 1422 msdosfs_readdir(v)
 1423         void *v;
 1424 {
 1425         struct vop_readdir_args /* {
 1426                 struct vnode *a_vp;
 1427                 struct uio *a_uio;
 1428                 kauth_cred_t a_cred;
 1429                 int *a_eofflag;
 1430                 off_t **a_cookies;
 1431                 int *a_ncookies;
 1432         } */ *ap = v;
 1433         int error = 0;
 1434         int diff;
 1435         long n;
 1436         int blsize;
 1437         long on;
 1438         long lost;
 1439         long count;
 1440         u_long cn;
 1441         ino_t fileno;
 1442         u_long dirsperblk;
 1443         long bias = 0;
 1444         daddr_t bn, lbn;
 1445         struct buf *bp;
 1446         struct denode *dep = VTODE(ap->a_vp);
 1447         struct msdosfsmount *pmp = dep->de_pmp;
 1448         struct direntry *dentp;
 1449         struct dirent dirbuf;
 1450         struct uio *uio = ap->a_uio;
 1451         off_t *cookies = NULL;
 1452         int ncookies = 0, nc = 0;
 1453         off_t offset, uio_off;
 1454         int chksum = -1;
 1455 
 1456 #ifdef MSDOSFS_DEBUG
 1457         printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
 1458             ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
 1459 #endif
 1460 
 1461         /*
 1462          * msdosfs_readdir() won't operate properly on regular files since
 1463          * it does i/o only with the filesystem vnode, and hence can
 1464          * retrieve the wrong block from the buffer cache for a plain file.
 1465          * So, fail attempts to readdir() on a plain file.
 1466          */
 1467         if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
 1468                 return (ENOTDIR);
 1469 
 1470         /*
 1471          * To be safe, initialize dirbuf
 1472          */
 1473         memset(dirbuf.d_name, 0, sizeof(dirbuf.d_name));
 1474 
 1475         /*
 1476          * If the user buffer is smaller than the size of one dos directory
 1477          * entry or the file offset is not a multiple of the size of a
 1478          * directory entry, then we fail the read.
 1479          */
 1480         count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
 1481         offset = uio->uio_offset;
 1482         if (count < sizeof(struct direntry) ||
 1483             (offset & (sizeof(struct direntry) - 1)))
 1484                 return (EINVAL);
 1485         lost = uio->uio_resid - count;
 1486         uio->uio_resid = count;
 1487         uio_off = uio->uio_offset;
 1488 
 1489         if (ap->a_ncookies) {
 1490                 nc = uio->uio_resid / 16;
 1491                 cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
 1492                 *ap->a_cookies = cookies;
 1493         }
 1494 
 1495         dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
 1496 
 1497         /*
 1498          * If they are reading from the root directory then, we simulate
 1499          * the . and .. entries since these don't exist in the root
 1500          * directory.  We also set the offset bias to make up for having to
 1501          * simulate these entries. By this I mean that at file offset 64 we
 1502          * read the first entry in the root directory that lives on disk.
 1503          */
 1504         if (dep->de_StartCluster == MSDOSFSROOT
 1505             || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
 1506 #if 0
 1507                 printf("msdosfs_readdir(): going after . or .. in root dir, "
 1508                     "offset %" PRIu64 "\n", offset);
 1509 #endif
 1510                 bias = 2 * sizeof(struct direntry);
 1511                 if (offset < bias) {
 1512                         for (n = (int)offset / sizeof(struct direntry);
 1513                              n < 2; n++) {
 1514                                 if (FAT32(pmp))
 1515                                         dirbuf.d_fileno = cntobn(pmp,
 1516                                              (ino_t)pmp->pm_rootdirblk)
 1517                                              * dirsperblk;
 1518                                 else
 1519                                         dirbuf.d_fileno = 1;
 1520                                 dirbuf.d_type = DT_DIR;
 1521                                 switch (n) {
 1522                                 case 0:
 1523                                         dirbuf.d_namlen = 1;
 1524                                         strlcpy(dirbuf.d_name, ".",
 1525                                             sizeof(dirbuf.d_name));
 1526                                         break;
 1527                                 case 1:
 1528                                         dirbuf.d_namlen = 2;
 1529                                         strlcpy(dirbuf.d_name, "..",
 1530                                             sizeof(dirbuf.d_name));
 1531                                         break;
 1532                                 }
 1533                                 dirbuf.d_reclen = _DIRENT_SIZE(&dirbuf);
 1534                                 if (uio->uio_resid < dirbuf.d_reclen)
 1535                                         goto out;
 1536                                 error = uiomove(&dirbuf,
 1537                                                 dirbuf.d_reclen, uio);
 1538                                 if (error)
 1539                                         goto out;
 1540                                 offset += sizeof(struct direntry);
 1541                                 uio_off = offset;
 1542                                 if (cookies) {
 1543                                         *cookies++ = offset;
 1544                                         ncookies++;
 1545                                         if (ncookies >= nc)
 1546                                                 goto out;
 1547                                 }
 1548                         }
 1549                 }
 1550         }
 1551 
 1552         while (uio->uio_resid > 0) {
 1553                 lbn = de_cluster(pmp, offset - bias);
 1554                 on = (offset - bias) & pmp->pm_crbomask;
 1555                 n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
 1556                 diff = dep->de_FileSize - (offset - bias);
 1557                 if (diff <= 0)
 1558                         break;
 1559                 n = MIN(n, diff);
 1560                 if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
 1561                         break;
 1562                 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
 1563                     NOCRED, &bp);
 1564                 if (error) {
 1565                         brelse(bp);
 1566                         return (error);
 1567                 }
 1568                 n = MIN(n, blsize - bp->b_resid);
 1569 
 1570                 /*
 1571                  * Convert from dos directory entries to fs-independent
 1572                  * directory entries.
 1573                  */
 1574                 for (dentp = (struct direntry *)(bp->b_data + on);
 1575                      (char *)dentp < bp->b_data + on + n;
 1576                      dentp++, offset += sizeof(struct direntry)) {
 1577 #if 0
 1578 
 1579                         printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
 1580                             dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
 1581 #endif
 1582                         /*
 1583                          * If this is an unused entry, we can stop.
 1584                          */
 1585                         if (dentp->deName[0] == SLOT_EMPTY) {
 1586                                 brelse(bp);
 1587                                 goto out;
 1588                         }
 1589                         /*
 1590                          * Skip deleted entries.
 1591                          */
 1592                         if (dentp->deName[0] == SLOT_DELETED) {
 1593                                 chksum = -1;
 1594                                 continue;
 1595                         }
 1596 
 1597                         /*
 1598                          * Handle Win95 long directory entries
 1599                          */
 1600                         if (dentp->deAttributes == ATTR_WIN95) {
 1601                                 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
 1602                                         continue;
 1603                                 chksum = win2unixfn((struct winentry *)dentp, &dirbuf, chksum);
 1604                                 continue;
 1605                         }
 1606 
 1607                         /*
 1608                          * Skip volume labels
 1609                          */
 1610                         if (dentp->deAttributes & ATTR_VOLUME) {
 1611                                 chksum = -1;
 1612                                 continue;
 1613                         }
 1614                         /*
 1615                          * This computation of d_fileno must match
 1616                          * the computation of va_fileid in
 1617                          * msdosfs_getattr.
 1618                          */
 1619                         if (dentp->deAttributes & ATTR_DIRECTORY) {
 1620                                 fileno = getushort(dentp->deStartCluster);
 1621                                 if (FAT32(pmp))
 1622                                         fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
 1623                                 /* if this is the root directory */
 1624                                 if (fileno == MSDOSFSROOT)
 1625                                         if (FAT32(pmp))
 1626                                                 fileno = cntobn(pmp,
 1627                                                     (ino_t)pmp->pm_rootdirblk)
 1628                                                     * dirsperblk;
 1629                                         else
 1630                                                 fileno = 1;
 1631                                 else
 1632                                         fileno = cntobn(pmp, fileno) * dirsperblk;
 1633                                 dirbuf.d_fileno = fileno;
 1634                                 dirbuf.d_type = DT_DIR;
 1635                         } else {
 1636                                 dirbuf.d_fileno = offset / sizeof(struct direntry);
 1637                                 dirbuf.d_type = DT_REG;
 1638                         }
 1639                         if (chksum != winChksum(dentp->deName))
 1640                                 dirbuf.d_namlen = dos2unixfn(dentp->deName,
 1641                                     (u_char *)dirbuf.d_name,
 1642                                     pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
 1643                         else
 1644                                 dirbuf.d_name[dirbuf.d_namlen] = 0;
 1645                         chksum = -1;
 1646                         dirbuf.d_reclen = _DIRENT_SIZE(&dirbuf);
 1647                         if (uio->uio_resid < dirbuf.d_reclen) {
 1648                                 brelse(bp);
 1649                                 goto out;
 1650                         }
 1651                         error = uiomove(&dirbuf,
 1652                                         dirbuf.d_reclen, uio);
 1653                         if (error) {
 1654                                 brelse(bp);
 1655                                 goto out;
 1656                         }
 1657                         uio_off = offset + sizeof(struct direntry);
 1658                         if (cookies) {
 1659                                 *cookies++ = offset + sizeof(struct direntry);
 1660                                 ncookies++;
 1661                                 if (ncookies >= nc) {
 1662                                         brelse(bp);
 1663                                         goto out;
 1664                                 }
 1665                         }
 1666                 }
 1667                 brelse(bp);
 1668         }
 1669 
 1670 out:
 1671         uio->uio_offset = uio_off;
 1672         uio->uio_resid += lost;
 1673         if (dep->de_FileSize - (offset - bias) <= 0)
 1674                 *ap->a_eofflag = 1;
 1675         else
 1676                 *ap->a_eofflag = 0;
 1677 
 1678         if (ap->a_ncookies) {
 1679                 if (error) {
 1680                         free(*ap->a_cookies, M_TEMP);
 1681                         *ap->a_ncookies = 0;
 1682                         *ap->a_cookies = NULL;
 1683                 } else
 1684                         *ap->a_ncookies = ncookies;
 1685         }
 1686         return (error);
 1687 }
 1688 
 1689 /*
 1690  * DOS filesystems don't know what symlinks are.
 1691  */
 1692 int
 1693 msdosfs_readlink(void *v)
 1694 {
 1695 #if 0
 1696         struct vop_readlink_args /* {
 1697                 struct vnode *a_vp;
 1698                 struct uio *a_uio;
 1699                 kauth_cred_t a_cred;
 1700         } */ *ap;
 1701 #endif
 1702 
 1703         return (EINVAL);
 1704 }
 1705 
 1706 /*
 1707  * vp  - address of vnode file the file
 1708  * bn  - which cluster we are interested in mapping to a filesystem block number.
 1709  * vpp - returns the vnode for the block special file holding the filesystem
 1710  *       containing the file of interest
 1711  * bnp - address of where to return the filesystem relative block number
 1712  */
 1713 int
 1714 msdosfs_bmap(v)
 1715         void *v;
 1716 {
 1717         struct vop_bmap_args /* {
 1718                 struct vnode *a_vp;
 1719                 daddr_t a_bn;
 1720                 struct vnode **a_vpp;
 1721                 daddr_t *a_bnp;
 1722                 int *a_runp;
 1723         } */ *ap = v;
 1724         struct denode *dep = VTODE(ap->a_vp);
 1725         int status;
 1726 
 1727         if (ap->a_vpp != NULL)
 1728                 *ap->a_vpp = dep->de_devvp;
 1729         if (ap->a_bnp == NULL)
 1730                 return (0);
 1731         if (ap->a_runp) {
 1732                 /*
 1733                  * Sequential clusters should be counted here.
 1734                  */
 1735                 *ap->a_runp = 0;
 1736         }
 1737         status = pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
 1738         /*
 1739          * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
 1740          */
 1741         *ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
 1742         return status;
 1743 }
 1744 
 1745 int
 1746 msdosfs_strategy(v)
 1747         void *v;
 1748 {
 1749         struct vop_strategy_args /* {
 1750                 struct vnode *a_vp;
 1751                 struct buf *a_bp;
 1752         } */ *ap = v;
 1753         struct vnode *vp = ap->a_vp;
 1754         struct buf *bp = ap->a_bp;
 1755         struct denode *dep = VTODE(bp->b_vp);
 1756         int error = 0;
 1757 
 1758         if (vp->v_type == VBLK || vp->v_type == VCHR)
 1759                 panic("msdosfs_strategy: spec");
 1760         /*
 1761          * If we don't already know the filesystem relative block number
 1762          * then get it using pcbmap().  If pcbmap() returns the block
 1763          * number as -1 then we've got a hole in the file.  DOS filesystems
 1764          * don't allow files with holes, so we shouldn't ever see this.
 1765          */
 1766         if (bp->b_blkno == bp->b_lblkno) {
 1767                 error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
 1768                                &bp->b_blkno, 0, 0);
 1769                 if (error)
 1770                         bp->b_blkno = -1;
 1771                 if (bp->b_blkno == -1)
 1772                         clrbuf(bp);
 1773                 else
 1774                         bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
 1775         }
 1776         if (bp->b_blkno == -1) {
 1777                 biodone(bp);
 1778                 return (error);
 1779         }
 1780 
 1781         /*
 1782          * Read/write the block from/to the disk that contains the desired
 1783          * file block.
 1784          */
 1785 
 1786         vp = dep->de_devvp;
 1787         return (VOP_STRATEGY(vp, bp));
 1788 }
 1789 
 1790 int
 1791 msdosfs_print(v)
 1792         void *v;
 1793 {
 1794         struct vop_print_args /* {
 1795                 struct vnode *vp;
 1796         } */ *ap = v;
 1797         struct denode *dep = VTODE(ap->a_vp);
 1798 
 1799         printf(
 1800             "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
 1801             dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
 1802         printf(" dev %d, %d ", major(dep->de_dev), minor(dep->de_dev));
 1803         lockmgr_printinfo(&ap->a_vp->v_lock);
 1804         printf("\n");
 1805         return (0);
 1806 }
 1807 
 1808 int
 1809 msdosfs_advlock(v)
 1810         void *v;
 1811 {
 1812         struct vop_advlock_args /* {
 1813                 struct vnode *a_vp;
 1814                 void *a_id;
 1815                 int a_op;
 1816                 struct flock *a_fl;
 1817                 int a_flags;
 1818         } */ *ap = v;
 1819         struct denode *dep = VTODE(ap->a_vp);
 1820 
 1821         return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
 1822 }
 1823 
 1824 int
 1825 msdosfs_pathconf(v)
 1826         void *v;
 1827 {
 1828         struct vop_pathconf_args /* {
 1829                 struct vnode *a_vp;
 1830                 int a_name;
 1831                 register_t *a_retval;
 1832         } */ *ap = v;
 1833 
 1834         switch (ap->a_name) {
 1835         case _PC_LINK_MAX:
 1836                 *ap->a_retval = 1;
 1837                 return (0);
 1838         case _PC_NAME_MAX:
 1839                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
 1840                 return (0);
 1841         case _PC_PATH_MAX:
 1842                 *ap->a_retval = PATH_MAX;
 1843                 return (0);
 1844         case _PC_CHOWN_RESTRICTED:
 1845                 *ap->a_retval = 1;
 1846                 return (0);
 1847         case _PC_NO_TRUNC:
 1848                 *ap->a_retval = 0;
 1849                 return (0);
 1850         case _PC_SYNC_IO:
 1851                 *ap->a_retval = 1;
 1852                 return (0);
 1853         case _PC_FILESIZEBITS:
 1854                 *ap->a_retval = 32;
 1855                 return (0);
 1856         default:
 1857                 return (EINVAL);
 1858         }
 1859         /* NOTREACHED */
 1860 }
 1861 
 1862 int
 1863 msdosfs_fsync(v)
 1864         void *v;
 1865 {
 1866         struct vop_fsync_args /* {
 1867                 struct vnode *a_vp;
 1868                 kauth_cred_t a_cred;
 1869                 int a_flags;
 1870                 off_t offlo;
 1871                 off_t offhi;
 1872                 struct lwp *a_l;
 1873         } */ *ap = v;
 1874         struct vnode *vp = ap->a_vp;
 1875         int wait;
 1876         int error;
 1877 
 1878         wait = (ap->a_flags & FSYNC_WAIT) != 0;
 1879         vflushbuf(vp, wait);
 1880         if ((ap->a_flags & FSYNC_DATAONLY) != 0)
 1881                 error = 0;
 1882         else
 1883                 error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
 1884 
 1885         if (error == 0 && ap->a_flags & FSYNC_CACHE) {
 1886                 struct denode *dep = VTODE(vp);
 1887                 struct vnode *devvp = dep->de_devvp;
 1888 
 1889                 int l = 0;
 1890                 error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
 1891                                           ap->a_l->l_cred, ap->a_l);
 1892         }
 1893 
 1894         return (error);
 1895 }
 1896 
 1897 void
 1898 msdosfs_detimes(struct denode *dep, const struct timespec *acc,
 1899     const struct timespec *mod, const struct timespec *cre, int gmtoff)
 1900 {
 1901         struct timespec *ts = NULL, tsb;
 1902 
 1903         KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
 1904         /* XXX just call getnanotime early and use result if needed? */
 1905         dep->de_flag |= DE_MODIFIED;
 1906         if (dep->de_flag & DE_UPDATE) {
 1907                 if (mod == NULL) {
 1908                         getnanotime(&tsb);
 1909                         mod = ts = &tsb;
 1910                 }
 1911                 unix2dostime(mod, gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
 1912                 dep->de_Attributes |= ATTR_ARCHIVE;
 1913         }
 1914         if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
 1915                 if (dep->de_flag & DE_ACCESS)  {
 1916                         if (acc == NULL)
 1917                                 acc = ts == NULL ?
 1918                                     (getnanotime(&tsb), ts = &tsb) : ts;
 1919                         unix2dostime(acc, gmtoff, &dep->de_ADate, NULL, NULL);
 1920                 }
 1921                 if (dep->de_flag & DE_CREATE) {
 1922                         if (cre == NULL)
 1923                                 cre = ts == NULL ?
 1924                                     (getnanotime(&tsb), ts = &tsb) : ts;
 1925                         unix2dostime(cre, gmtoff, &dep->de_CDate,
 1926                             &dep->de_CTime, &dep->de_CHun);
 1927                 }
 1928         }
 1929 
 1930         dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
 1931 }
 1932 
 1933 /* Global vfs data structures for msdosfs */
 1934 int (**msdosfs_vnodeop_p)(void *);
 1935 const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
 1936         { &vop_default_desc, vn_default_error },
 1937         { &vop_lookup_desc, msdosfs_lookup },           /* lookup */
 1938         { &vop_create_desc, msdosfs_create },           /* create */
 1939         { &vop_mknod_desc, msdosfs_mknod },             /* mknod */
 1940         { &vop_open_desc, msdosfs_open },               /* open */
 1941         { &vop_close_desc, msdosfs_close },             /* close */
 1942         { &vop_access_desc, msdosfs_access },           /* access */
 1943         { &vop_getattr_desc, msdosfs_getattr },         /* getattr */
 1944         { &vop_setattr_desc, msdosfs_setattr },         /* setattr */
 1945         { &vop_read_desc, msdosfs_read },               /* read */
 1946         { &vop_write_desc, msdosfs_write },             /* write */
 1947         { &vop_lease_desc, msdosfs_lease_check },       /* lease */
 1948         { &vop_fcntl_desc, genfs_fcntl },               /* fcntl */
 1949         { &vop_ioctl_desc, msdosfs_ioctl },             /* ioctl */
 1950         { &vop_poll_desc, msdosfs_poll },               /* poll */
 1951         { &vop_kqfilter_desc, genfs_kqfilter },         /* kqfilter */
 1952         { &vop_revoke_desc, msdosfs_revoke },           /* revoke */
 1953         { &vop_mmap_desc, msdosfs_mmap },               /* mmap */
 1954         { &vop_fsync_desc, msdosfs_fsync },             /* fsync */
 1955         { &vop_seek_desc, msdosfs_seek },               /* seek */
 1956         { &vop_remove_desc, msdosfs_remove },           /* remove */
 1957         { &vop_link_desc, msdosfs_link },               /* link */
 1958         { &vop_rename_desc, msdosfs_rename },           /* rename */
 1959         { &vop_mkdir_desc, msdosfs_mkdir },             /* mkdir */
 1960         { &vop_rmdir_desc, msdosfs_rmdir },             /* rmdir */
 1961         { &vop_symlink_desc, msdosfs_symlink },         /* symlink */
 1962         { &vop_readdir_desc, msdosfs_readdir },         /* readdir */
 1963         { &vop_readlink_desc, msdosfs_readlink },       /* readlink */
 1964         { &vop_abortop_desc, msdosfs_abortop },         /* abortop */
 1965         { &vop_inactive_desc, msdosfs_inactive },       /* inactive */
 1966         { &vop_reclaim_desc, msdosfs_reclaim },         /* reclaim */
 1967         { &vop_lock_desc, genfs_lock },                 /* lock */
 1968         { &vop_unlock_desc, genfs_unlock },             /* unlock */
 1969         { &vop_bmap_desc, msdosfs_bmap },               /* bmap */
 1970         { &vop_strategy_desc, msdosfs_strategy },       /* strategy */
 1971         { &vop_print_desc, msdosfs_print },             /* print */
 1972         { &vop_islocked_desc, genfs_islocked },         /* islocked */
 1973         { &vop_pathconf_desc, msdosfs_pathconf },       /* pathconf */
 1974         { &vop_advlock_desc, msdosfs_advlock },         /* advlock */
 1975         { &vop_bwrite_desc, vn_bwrite },                /* bwrite */
 1976         { &vop_getpages_desc, genfs_getpages },         /* getpages */
 1977         { &vop_putpages_desc, genfs_putpages },         /* putpages */
 1978         { NULL, NULL }
 1979 };
 1980 const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
 1981         { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };

Cache object: c6cfa508da02c52cdd0f8480a5914e4b


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