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

Cache object: 5174669492ca4e17473b5276ca226674


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