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

Cache object: 67e11a8d2764266c7cfe392459c875c6


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