The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/msdosfs/msdosfs_vnops.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: ecee0af5f585525ab0310065cf9dea04


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