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

Cache object: c92b344b34639bb416acd588f623398b


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