Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_lookup.c revision 1.23.14.1
      1 /*	$NetBSD: msdosfs_lookup.c,v 1.23.14.1 2012/08/12 12:59:50 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
      5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
      6  * All rights reserved.
      7  * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 /*
     35  * Written by Paul Popelka (paulp (at) uts.amdahl.com)
     36  *
     37  * You can do anything you want with this software, just don't say you wrote
     38  * it, and don't remove this notice.
     39  *
     40  * This software is provided "as is".
     41  *
     42  * The author supplies this software to be publicly redistributed on the
     43  * understanding that the author is not responsible for the correct
     44  * functioning of this software in any circumstances and is not liable for
     45  * any damages caused by this software.
     46  *
     47  * October 1992
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_lookup.c,v 1.23.14.1 2012/08/12 12:59:50 martin Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/namei.h>
     56 #include <sys/buf.h>
     57 #include <sys/vnode.h>
     58 #include <sys/mount.h>
     59 #include <sys/dirent.h>
     60 #include <sys/kauth.h>
     61 
     62 #include <fs/msdosfs/bpb.h>
     63 #include <fs/msdosfs/direntry.h>
     64 #include <fs/msdosfs/denode.h>
     65 #include <fs/msdosfs/msdosfsmount.h>
     66 #include <fs/msdosfs/fat.h>
     67 
     68 /*
     69  * When we search a directory the blocks containing directory entries are
     70  * read and examined.  The directory entries contain information that would
     71  * normally be in the inode of a unix filesystem.  This means that some of
     72  * a directory's contents may also be in memory resident denodes (sort of
     73  * an inode).  This can cause problems if we are searching while some other
     74  * process is modifying a directory.  To prevent one process from accessing
     75  * incompletely modified directory information we depend upon being the
     76  * sole owner of a directory block.  bread/brelse provide this service.
     77  * This being the case, when a process modifies a directory it must first
     78  * acquire the disk block that contains the directory entry to be modified.
     79  * Then update the disk block and the denode, and then write the disk block
     80  * out to disk.  This way disk blocks containing directory entries and in
     81  * memory denode's will be in synch.
     82  */
     83 int
     84 msdosfs_lookup(void *v)
     85 {
     86 	struct vop_lookup_args /* {
     87 		struct vnode *a_dvp;
     88 		struct vnode **a_vpp;
     89 		struct componentname *a_cnp;
     90 	} */ *ap = v;
     91 	struct vnode *vdp = ap->a_dvp;
     92 	struct vnode **vpp = ap->a_vpp;
     93 	struct componentname *cnp = ap->a_cnp;
     94 	daddr_t bn;
     95 	int error;
     96 	int slotcount;
     97 	int slotoffset = 0;
     98 	int frcn;
     99 	u_long cluster;
    100 	int blkoff;
    101 	int diroff;
    102 	int blsize;
    103 	int isadir;		/* ~0 if found direntry is a directory	 */
    104 	u_long scn;		/* starting cluster number		 */
    105 	struct vnode *pdp;
    106 	struct denode *dp;
    107 	struct denode *tdp;
    108 	struct msdosfsmount *pmp;
    109 	struct buf *bp = 0;
    110 	struct direntry *dep;
    111 	u_char dosfilename[12];
    112 	int flags;
    113 	int nameiop = cnp->cn_nameiop;
    114 	int wincnt = 1;
    115 	int chksum = -1, chksum_ok;
    116 	int olddos = 1;
    117 
    118 	flags = cnp->cn_flags;
    119 
    120 #ifdef MSDOSFS_DEBUG
    121 	printf("msdosfs_lookup(): looking for %.*s\n",
    122 		(int)cnp->cn_namelen, cnp->cn_nameptr);
    123 #endif
    124 	dp = VTODE(vdp);
    125 	pmp = dp->de_pmp;
    126 	*vpp = NULL;
    127 #ifdef MSDOSFS_DEBUG
    128 	printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
    129 	    vdp, dp, dp->de_Attributes);
    130 #endif
    131 
    132 	/*
    133 	 * Check accessiblity of directory.
    134 	 */
    135 	if ((error = VOP_ACCESS(vdp, VEXEC, cnp->cn_cred)) != 0)
    136 		return (error);
    137 
    138 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
    139 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    140 		return (EROFS);
    141 
    142 	/*
    143 	 * We now have a segment name to search for, and a directory to search.
    144 	 *
    145 	 * Before tediously performing a linear scan of the directory,
    146 	 * check the name cache to see if the directory/name pair
    147 	 * we are looking for is known already.
    148 	 */
    149 	if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
    150 		return (error);
    151 
    152 	/*
    153 	 * If they are going after the . or .. entry in the root directory,
    154 	 * they won't find it.  DOS filesystems don't have them in the root
    155 	 * directory.  So, we fake it. deget() is in on this scam too.
    156 	 */
    157 	if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
    158 	    (cnp->cn_namelen == 1 ||
    159 		(cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
    160 		isadir = ATTR_DIRECTORY;
    161 		scn = MSDOSFSROOT;
    162 #ifdef MSDOSFS_DEBUG
    163 		printf("msdosfs_lookup(): looking for . or .. in root directory\n");
    164 #endif
    165 		cluster = MSDOSFSROOT;
    166 		blkoff = MSDOSFSROOT_OFS;
    167 		goto foundroot;
    168 	}
    169 
    170 	switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
    171 	    cnp->cn_namelen, 0)) {
    172 	case 0:
    173 		return (EINVAL);
    174 	case 1:
    175 		break;
    176 	case 2:
    177 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
    178 		    cnp->cn_namelen) + 1;
    179 		break;
    180 	case 3:
    181 		olddos = 0;
    182 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
    183 		    cnp->cn_namelen) + 1;
    184 		break;
    185 	}
    186 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
    187 		wincnt = 1;
    188 
    189 	/*
    190 	 * Suppress search for slots unless creating
    191 	 * file and at end of pathname, in which case
    192 	 * we watch for a place to put the new file in
    193 	 * case it doesn't already exist.
    194 	 */
    195 	slotcount = wincnt;
    196 	if ((nameiop == CREATE || nameiop == RENAME) &&
    197 	    (flags & ISLASTCN))
    198 		slotcount = 0;
    199 
    200 #ifdef MSDOSFS_DEBUG
    201 	printf("msdosfs_lookup(): dos filename: %s\n", dosfilename);
    202 #endif
    203 	/*
    204 	 * Search the directory pointed at by vdp for the name pointed at
    205 	 * by cnp->cn_nameptr.
    206 	 */
    207 	tdp = NULL;
    208 	/*
    209 	 * The outer loop ranges over the clusters that make up the
    210 	 * directory.  Note that the root directory is different from all
    211 	 * other directories.  It has a fixed number of blocks that are not
    212 	 * part of the pool of allocatable clusters.  So, we treat it a
    213 	 * little differently. The root directory starts at "cluster" 0.
    214 	 */
    215 	diroff = 0;
    216 	for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
    217 		if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
    218 			if (error == E2BIG)
    219 				break;
    220 			return (error);
    221 		}
    222 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    223 		    0, &bp);
    224 		if (error) {
    225 			brelse(bp, 0);
    226 			return (error);
    227 		}
    228 		for (blkoff = 0; blkoff < blsize;
    229 		     blkoff += sizeof(struct direntry),
    230 		     diroff += sizeof(struct direntry)) {
    231 			dep = (struct direntry *)((char *)bp->b_data + blkoff);
    232 			/*
    233 			 * If the slot is empty and we are still looking
    234 			 * for an empty then remember this one.  If the
    235 			 * slot is not empty then check to see if it
    236 			 * matches what we are looking for.  If the slot
    237 			 * has never been filled with anything, then the
    238 			 * remainder of the directory has never been used,
    239 			 * so there is no point in searching it.
    240 			 */
    241 			if (dep->deName[0] == SLOT_EMPTY ||
    242 			    dep->deName[0] == SLOT_DELETED) {
    243 				/*
    244 				 * Drop memory of previous long matches
    245 				 */
    246 				chksum = -1;
    247 
    248 				if (slotcount < wincnt) {
    249 					slotcount++;
    250 					slotoffset = diroff;
    251 				}
    252 				if (dep->deName[0] == SLOT_EMPTY) {
    253 					brelse(bp, 0);
    254 					goto notfound;
    255 				}
    256 			} else {
    257 				/*
    258 				 * If there wasn't enough space for our
    259 				 * winentries, forget about the empty space
    260 				 */
    261 				if (slotcount < wincnt)
    262 					slotcount = 0;
    263 
    264 				/*
    265 				 * Check for Win95 long filename entry
    266 				 */
    267 				if (dep->deAttributes == ATTR_WIN95) {
    268 					if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
    269 						continue;
    270 
    271 					chksum = winChkName((const u_char *)cnp->cn_nameptr,
    272 							    cnp->cn_namelen,
    273 							    (struct winentry *)dep,
    274 							    chksum);
    275 					continue;
    276 				}
    277 
    278 				/*
    279 				 * Ignore volume labels (anywhere, not just
    280 				 * the root directory).
    281 				 */
    282 				if (dep->deAttributes & ATTR_VOLUME) {
    283 					chksum = -1;
    284 					continue;
    285 				}
    286 
    287 				/*
    288 				 * Check for a checksum or name match
    289 				 */
    290 				chksum_ok = (chksum == winChksum(dep->deName));
    291 				if (!chksum_ok
    292 				    && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
    293 					chksum = -1;
    294 					continue;
    295 				}
    296 #ifdef MSDOSFS_DEBUG
    297 				printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
    298 				    blkoff, diroff);
    299 #endif
    300 				/*
    301 				 * Remember where this directory
    302 				 * entry came from for whoever did
    303 				 * this lookup.
    304 				 */
    305 				dp->de_fndoffset = diroff;
    306 				if (chksum_ok && nameiop == RENAME) {
    307 					/*
    308 					 * Target had correct long name
    309 					 * directory entries, reuse them
    310 					 * as needed.
    311 					 */
    312 					dp->de_fndcnt = wincnt - 1;
    313 				} else {
    314 					/*
    315 					 * Long name directory entries
    316 					 * not present or corrupt, can only
    317 					 * reuse dos directory entry.
    318 					 */
    319 					dp->de_fndcnt = 0;
    320 				}
    321 
    322 				goto found;
    323 			}
    324 		}	/* for (blkoff = 0; .... */
    325 		/*
    326 		 * Release the buffer holding the directory cluster just
    327 		 * searched.
    328 		 */
    329 		brelse(bp, 0);
    330 	}	/* for (frcn = 0; ; frcn++) */
    331 
    332 notfound:
    333 	/*
    334 	 * We hold no disk buffers at this point.
    335 	 */
    336 
    337 	/*
    338 	 * If we get here we didn't find the entry we were looking for. But
    339 	 * that's ok if we are creating or renaming and are at the end of
    340 	 * the pathname and the directory hasn't been removed.
    341 	 */
    342 #ifdef MSDOSFS_DEBUG
    343 	printf("msdosfs_lookup(): op %d, refcnt %ld, slotcount %d, slotoffset %d\n",
    344 	    nameiop, dp->de_refcnt, slotcount, slotoffset);
    345 #endif
    346 	if ((nameiop == CREATE || nameiop == RENAME) &&
    347 	    (flags & ISLASTCN) && dp->de_refcnt != 0) {
    348 		/*
    349 		 * Access for write is interpreted as allowing
    350 		 * creation of files in the directory.
    351 		 */
    352 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
    353 		if (error)
    354 			return (error);
    355 
    356 		/*
    357 		 * Fixup the slot description to point to the place where
    358 		 * we might put the new DOS direntry (putting the Win95
    359 		 * long name entries before that)
    360 		 */
    361 		if (!slotcount) {
    362 			slotcount = 1;
    363 			slotoffset = diroff;
    364 		}
    365 		if (wincnt > slotcount) {
    366 			slotoffset +=
    367 				sizeof(struct direntry) * (wincnt - slotcount);
    368 		}
    369 
    370 		/*
    371 		 * Return an indication of where the new directory
    372 		 * entry should be put.
    373 		 */
    374 		dp->de_fndoffset = slotoffset;
    375 		dp->de_fndcnt = wincnt - 1;
    376 
    377 		/*
    378 		 * We return with the directory locked, so that
    379 		 * the parameters we set up above will still be
    380 		 * valid if we actually decide to do a direnter().
    381 		 * We return ni_vp == NULL to indicate that the entry
    382 		 * does not currently exist; we leave a pointer to
    383 		 * the (locked) directory inode in ndp->ni_dvp.
    384 		 *
    385 		 * NB - if the directory is unlocked, then this
    386 		 * information cannot be used.
    387 		 */
    388 		return (EJUSTRETURN);
    389 	}
    390 
    391 #if 0
    392 	/*
    393 	 * Insert name into cache (as non-existent) if appropriate.
    394 	 *
    395 	 * XXX Negative caching is broken for msdosfs because the name
    396 	 * cache doesn't understand peculiarities such as case insensitivity
    397 	 * and 8.3 filenames.  Hence, it may not invalidate all negative
    398 	 * entries if a file with this name is later created.
    399 	 * e.g. creating a file 'foo' won't invalidate a negative entry
    400 	 * for 'FOO'.
    401 	 */
    402 	if (nameiop != CREATE)
    403 		cache_enter(vdp, *vpp, cnp);
    404 #endif
    405 
    406 	return (ENOENT);
    407 
    408 found:
    409 	/*
    410 	 * NOTE:  We still have the buffer with matched directory entry at
    411 	 * this point.
    412 	 */
    413 	isadir = dep->deAttributes & ATTR_DIRECTORY;
    414 	scn = getushort(dep->deStartCluster);
    415 	if (FAT32(pmp)) {
    416 		scn |= getushort(dep->deHighClust) << 16;
    417 		if (scn == pmp->pm_rootdirblk) {
    418 			/*
    419 			 * There should actually be 0 here.
    420 			 * Just ignore the error.
    421 			 */
    422 			scn = MSDOSFSROOT;
    423 		}
    424 	}
    425 
    426 	if (isadir) {
    427 		cluster = scn;
    428 		if (cluster == MSDOSFSROOT)
    429 			blkoff = MSDOSFSROOT_OFS;
    430 		else
    431 			blkoff = 0;
    432 	} else if (cluster == MSDOSFSROOT)
    433 		blkoff = diroff;
    434 
    435 	/*
    436 	 * Now release buf to allow deget to read the entry again.
    437 	 * Reserving it here and giving it to deget could result
    438 	 * in a deadlock.
    439 	 */
    440 	brelse(bp, 0);
    441 
    442 foundroot:
    443 	/*
    444 	 * If we entered at foundroot, then we are looking for the . or ..
    445 	 * entry of the filesystems root directory.  isadir and scn were
    446 	 * setup before jumping here.  And, bp is already null.
    447 	 */
    448 	if (FAT32(pmp) && scn == MSDOSFSROOT)
    449 		scn = pmp->pm_rootdirblk;
    450 
    451 	/*
    452 	 * If deleting, and at end of pathname, return
    453 	 * parameters which can be used to remove file.
    454 	 * Lock the inode, being careful with ".".
    455 	 */
    456 	if (nameiop == DELETE && (flags & ISLASTCN)) {
    457 		/*
    458 		 * Don't allow deleting the root.
    459 		 */
    460 		if (blkoff == MSDOSFSROOT_OFS)
    461 			return EINVAL;
    462 
    463 		/*
    464 		 * Write access to directory required to delete files.
    465 		 */
    466 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
    467 		if (error)
    468 			return (error);
    469 
    470 		/*
    471 		 * Return pointer to current entry in dp->i_offset.
    472 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
    473 		 */
    474 		if (dp->de_StartCluster == scn && isadir) {	/* "." */
    475 			vref(vdp);
    476 			*vpp = vdp;
    477 			return (0);
    478 		}
    479 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
    480 			return (error);
    481 		*vpp = DETOV(tdp);
    482 		return (0);
    483 	}
    484 
    485 	/*
    486 	 * If rewriting (RENAME), return the inode and the
    487 	 * information required to rewrite the present directory
    488 	 * Must get inode of directory entry to verify it's a
    489 	 * regular file, or empty directory.
    490 	 */
    491 	if (nameiop == RENAME && (flags & ISLASTCN)) {
    492 
    493 		if (vdp->v_mount->mnt_flag & MNT_RDONLY)
    494 			return (EROFS);
    495 
    496 		if (blkoff == MSDOSFSROOT_OFS)
    497 			return EINVAL;
    498 
    499 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
    500 		if (error)
    501 			return (error);
    502 
    503 		/*
    504 		 * Careful about locking second inode.
    505 		 * This can only occur if the target is ".".
    506 		 */
    507 		if (dp->de_StartCluster == scn && isadir)
    508 			return (EISDIR);
    509 
    510 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
    511 			return (error);
    512 		*vpp = DETOV(tdp);
    513 		return (0);
    514 	}
    515 
    516 	/*
    517 	 * Step through the translation in the name.  We do not `vput' the
    518 	 * directory because we may need it again if a symbolic link
    519 	 * is relative to the current directory.  Instead we save it
    520 	 * unlocked as "pdp".  We must get the target inode before unlocking
    521 	 * the directory to insure that the inode will not be removed
    522 	 * before we get it.  We prevent deadlock by always fetching
    523 	 * inodes from the root, moving down the directory tree. Thus
    524 	 * when following backward pointers ".." we must unlock the
    525 	 * parent directory before getting the requested directory.
    526 	 * There is a potential race condition here if both the current
    527 	 * and parent directories are removed before the VFS_VGET for the
    528 	 * inode associated with ".." returns.  We hope that this occurs
    529 	 * infrequently since we cannot avoid this race condition without
    530 	 * implementing a sophisticated deadlock detection algorithm.
    531 	 * Note also that this simple deadlock detection scheme will not
    532 	 * work if the file system has any hard links other than ".."
    533 	 * that point backwards in the directory structure.
    534 	 */
    535 	pdp = vdp;
    536 	if (flags & ISDOTDOT) {
    537 		VOP_UNLOCK(pdp);	/* race to get the inode */
    538 		error = deget(pmp, cluster, blkoff, &tdp);
    539 		vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
    540 		if (error) {
    541 			return error;
    542 		}
    543 		*vpp = DETOV(tdp);
    544 	} else if (dp->de_StartCluster == scn && isadir) {
    545 		vref(vdp);	/* we want ourself, ie "." */
    546 		*vpp = vdp;
    547 	} else {
    548 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
    549 			return (error);
    550 		*vpp = DETOV(tdp);
    551 	}
    552 
    553 	/*
    554 	 * Insert name into cache if appropriate.
    555 	 */
    556 	cache_enter(vdp, *vpp, cnp);
    557 
    558 	return 0;
    559 }
    560 
    561 /*
    562  * dep  - directory entry to copy into the directory
    563  * ddep - directory to add to
    564  * depp - return the address of the denode for the created directory entry
    565  *	  if depp != 0
    566  * cnp  - componentname needed for Win95 long filenames
    567  */
    568 int
    569 createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp)
    570 {
    571 	int error, rberror;
    572 	u_long dirclust, clusoffset;
    573 	u_long fndoffset, havecnt=0, wcnt=1;
    574 	struct direntry *ndep;
    575 	struct msdosfsmount *pmp = ddep->de_pmp;
    576 	struct buf *bp;
    577 	daddr_t bn;
    578 	int blsize, i;
    579 	int async = ddep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
    580 
    581 #ifdef MSDOSFS_DEBUG
    582 	printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
    583 	    dep, ddep, depp, cnp);
    584 #endif
    585 
    586 	/*
    587 	 * If no space left in the directory then allocate another cluster
    588 	 * and chain it onto the end of the file.  There is one exception
    589 	 * to this.  That is, if the root directory has no more space it
    590 	 * can NOT be expanded.  extendfile() checks for and fails attempts
    591 	 * to extend the root directory.  We just return an error in that
    592 	 * case.
    593 	 */
    594 	if (ddep->de_fndoffset >= ddep->de_FileSize) {
    595 		u_long needlen = ddep->de_fndoffset + sizeof(struct direntry)
    596 		    - ddep->de_FileSize;
    597 		dirclust = de_clcount(pmp, needlen);
    598 		if ((error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR)) != 0) {
    599 			(void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
    600 			goto err_norollback;
    601 		}
    602 
    603 		/*
    604 		 * Update the size of the directory
    605 		 */
    606 		ddep->de_FileSize += de_cn2off(pmp, dirclust);
    607 	}
    608 
    609 	/*
    610 	 * We just read in the cluster with space.  Copy the new directory
    611 	 * entry in.  Then write it to disk. NOTE:  DOS directories
    612 	 * do not get smaller as clusters are emptied.
    613 	 */
    614 	error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
    615 		       &bn, &dirclust, &blsize);
    616 	if (error)
    617 		goto err_norollback;
    618 	clusoffset = ddep->de_fndoffset;
    619 	if (dirclust != MSDOSFSROOT)
    620 		clusoffset &= pmp->pm_crbomask;
    621 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    622 	    B_MODIFY, &bp)) != 0) {
    623 		brelse(bp, 0);
    624 		goto err_norollback;
    625 	}
    626 	ndep = bptoep(pmp, bp, clusoffset);
    627 
    628 	DE_EXTERNALIZE(ndep, dep);
    629 
    630 	/*
    631 	 * Now write the Win95 long name
    632 	 */
    633 	if (ddep->de_fndcnt > 0) {
    634 		u_int8_t chksum = winChksum(ndep->deName);
    635 		const u_char *un = (const u_char *)cnp->cn_nameptr;
    636 		int unlen = cnp->cn_namelen;
    637 		u_long xhavecnt;
    638 
    639 		fndoffset = ddep->de_fndoffset;
    640 		xhavecnt = ddep->de_fndcnt + 1;
    641 
    642 		for(; wcnt < xhavecnt; wcnt++) {
    643 			if ((fndoffset & pmp->pm_crbomask) == 0) {
    644 				/* we should never get here if ddep is root
    645 				 * directory */
    646 
    647 				if (async)
    648 					(void) bdwrite(bp);
    649 				else if ((error = bwrite(bp)) != 0)
    650 					goto rollback;
    651 
    652 				fndoffset -= sizeof(struct direntry);
    653 				error = pcbmap(ddep,
    654 					       de_cluster(pmp, fndoffset),
    655 					       &bn, 0, &blsize);
    656 				if (error)
    657 					goto rollback;
    658 
    659 				error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
    660 				    blsize, NOCRED, B_MODIFY, &bp);
    661 				if (error) {
    662 					brelse(bp, 0);
    663 					goto rollback;
    664 				}
    665 				ndep = bptoep(pmp, bp,
    666 						fndoffset & pmp->pm_crbomask);
    667 			} else {
    668 				ndep--;
    669 				fndoffset -= sizeof(struct direntry);
    670 			}
    671 			if (!unix2winfn(un, unlen, (struct winentry *)ndep,
    672 						wcnt, chksum))
    673 				break;
    674 		}
    675 	}
    676 
    677 	if (async)
    678 		bdwrite(bp);
    679 	else if ((error = bwrite(bp)) != 0)
    680 		goto rollback;
    681 
    682 	/*
    683 	 * If they want us to return with the denode gotten.
    684 	 */
    685 	if (depp) {
    686 		u_long diroffset = clusoffset;
    687 		if (dep->de_Attributes & ATTR_DIRECTORY) {
    688 			dirclust = dep->de_StartCluster;
    689 			if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
    690 				dirclust = MSDOSFSROOT;
    691 			if (dirclust == MSDOSFSROOT)
    692 				diroffset = MSDOSFSROOT_OFS;
    693 			else
    694 				diroffset = 0;
    695 		}
    696 		return deget(pmp, dirclust, diroffset, depp);
    697 	}
    698 
    699 	return 0;
    700 
    701     rollback:
    702 	/*
    703 	 * Mark all slots modified so far as deleted. Note that we
    704 	 * can't just call removede(), since directory is not in
    705 	 * consistent state.
    706 	 */
    707 	fndoffset = ddep->de_fndoffset;
    708 	rberror = pcbmap(ddep, de_cluster(pmp, fndoffset),
    709 	       &bn, NULL, &blsize);
    710 	if (rberror)
    711 		goto err_norollback;
    712 	if ((rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    713 	    B_MODIFY, &bp)) != 0) {
    714 		brelse(bp, 0);
    715 		goto err_norollback;
    716 	}
    717 	ndep = bptoep(pmp, bp, clusoffset);
    718 
    719 	havecnt = ddep->de_fndcnt + 1;
    720 	for(i=wcnt; i <= havecnt; i++) {
    721 		/* mark entry as deleted */
    722 		ndep->deName[0] = SLOT_DELETED;
    723 
    724 		if ((fndoffset & pmp->pm_crbomask) == 0) {
    725 			/* we should never get here if ddep is root
    726 			 * directory */
    727 
    728 			if (async)
    729 				bdwrite(bp);
    730 			else if ((rberror = bwrite(bp)) != 0)
    731 				goto err_norollback;
    732 
    733 			fndoffset -= sizeof(struct direntry);
    734 			rberror = pcbmap(ddep,
    735 				       de_cluster(pmp, fndoffset),
    736 				       &bn, 0, &blsize);
    737 			if (rberror)
    738 				goto err_norollback;
    739 
    740 			rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
    741 			    blsize, NOCRED, B_MODIFY, &bp);
    742 			if (rberror) {
    743 				brelse(bp, 0);
    744 				goto err_norollback;
    745 			}
    746 			ndep = bptoep(pmp, bp, fndoffset);
    747 		} else {
    748 			ndep--;
    749 			fndoffset -= sizeof(struct direntry);
    750 		}
    751 	}
    752 
    753 	/* ignore any further error */
    754 	if (async)
    755 		(void) bdwrite(bp);
    756 	else
    757 		(void) bwrite(bp);
    758 
    759     err_norollback:
    760 	return error;
    761 }
    762 
    763 /*
    764  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
    765  * return 0 if not empty or error.
    766  */
    767 int
    768 dosdirempty(struct denode *dep)
    769 {
    770 	int blsize;
    771 	int error;
    772 	u_long cn;
    773 	daddr_t bn;
    774 	struct buf *bp;
    775 	struct msdosfsmount *pmp = dep->de_pmp;
    776 	struct direntry *dentp;
    777 
    778 	/*
    779 	 * Since the filesize field in directory entries for a directory is
    780 	 * zero, we just have to feel our way through the directory until
    781 	 * we hit end of file.
    782 	 */
    783 	for (cn = 0;; cn++) {
    784 		if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
    785 			if (error == E2BIG)
    786 				return (1);	/* it's empty */
    787 			return (0);
    788 		}
    789 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    790 		    0, &bp);
    791 		if (error) {
    792 			brelse(bp, 0);
    793 			return (0);
    794 		}
    795 		for (dentp = (struct direntry *)bp->b_data;
    796 		     (char *)dentp < (char *)bp->b_data + blsize;
    797 		     dentp++) {
    798 			if (dentp->deName[0] != SLOT_DELETED &&
    799 			    (dentp->deAttributes & ATTR_VOLUME) == 0) {
    800 				/*
    801 				 * In dos directories an entry whose name
    802 				 * starts with SLOT_EMPTY (0) starts the
    803 				 * beginning of the unused part of the
    804 				 * directory, so we can just return that it
    805 				 * is empty.
    806 				 */
    807 				if (dentp->deName[0] == SLOT_EMPTY) {
    808 					brelse(bp, 0);
    809 					return (1);
    810 				}
    811 				/*
    812 				 * Any names other than "." and ".." in a
    813 				 * directory mean it is not empty.
    814 				 */
    815 				if (memcmp(dentp->deName, ".          ", 11) &&
    816 				    memcmp(dentp->deName, "..         ", 11)) {
    817 					brelse(bp, 0);
    818 #ifdef MSDOSFS_DEBUG
    819 					printf("dosdirempty(): found %.11s, %d, %d\n",
    820 					    dentp->deName, dentp->deName[0],
    821 						dentp->deName[1]);
    822 #endif
    823 					return (0);	/* not empty */
    824 				}
    825 			}
    826 		}
    827 		brelse(bp, 0);
    828 	}
    829 	/* NOTREACHED */
    830 }
    831 
    832 /*
    833  * Check to see if the directory described by target is in some
    834  * subdirectory of source.  This prevents something like the following from
    835  * succeeding and leaving a bunch or files and directories orphaned. mv
    836  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
    837  *
    838  * source - the inode for /a/b/c
    839  * target - the inode for /a/b/c/d/e/f
    840  *
    841  * Returns 0 if target is NOT a subdirectory of source.
    842  * Otherwise returns a non-zero error number.
    843  * The target inode is always unlocked on return.
    844  */
    845 int
    846 doscheckpath(struct denode *source, struct denode *target)
    847 {
    848 	u_long scn;
    849 	struct msdosfsmount *pmp;
    850 	struct direntry *ep;
    851 	struct denode *dep;
    852 	struct buf *bp = NULL;
    853 	int error = 0;
    854 
    855 	dep = target;
    856 	if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
    857 	    (source->de_Attributes & ATTR_DIRECTORY) == 0) {
    858 		error = ENOTDIR;
    859 		goto out;
    860 	}
    861 	if (dep->de_StartCluster == source->de_StartCluster) {
    862 		error = EEXIST;
    863 		goto out;
    864 	}
    865 	if (dep->de_StartCluster == MSDOSFSROOT)
    866 		goto out;
    867 	pmp = dep->de_pmp;
    868 #ifdef	DIAGNOSTIC
    869 	if (pmp != source->de_pmp)
    870 		panic("doscheckpath: source and target on different filesystems");
    871 #endif
    872 	if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
    873 		goto out;
    874 
    875 	for (;;) {
    876 		if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
    877 			error = ENOTDIR;
    878 			break;
    879 		}
    880 		scn = dep->de_StartCluster;
    881 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, cntobn(pmp, scn)),
    882 			      pmp->pm_bpcluster, NOCRED, 0, &bp);
    883 		if (error)
    884 			break;
    885 
    886 		ep = (struct direntry *) bp->b_data + 1;
    887 		if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
    888 		    memcmp(ep->deName, "..         ", 11) != 0) {
    889 			error = ENOTDIR;
    890 			break;
    891 		}
    892 		scn = getushort(ep->deStartCluster);
    893 		if (FAT32(pmp))
    894 			scn |= getushort(ep->deHighClust) << 16;
    895 
    896 		if (scn == source->de_StartCluster) {
    897 			error = EINVAL;
    898 			break;
    899 		}
    900 		if (scn == MSDOSFSROOT)
    901 			break;
    902 		if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
    903 			/*
    904 			 * scn should be 0 in this case,
    905 			 * but we silently ignore the error.
    906 			 */
    907 			break;
    908 		}
    909 
    910 		vput(DETOV(dep));
    911 		brelse(bp, 0);
    912 		bp = NULL;
    913 		/* NOTE: deget() clears dep on error */
    914 		if ((error = deget(pmp, scn, 0, &dep)) != 0)
    915 			break;
    916 	}
    917 out:
    918 	if (bp)
    919 		brelse(bp, 0);
    920 	if (error == ENOTDIR)
    921 		printf("doscheckpath(): .. not a directory?\n");
    922 	if (dep != NULL)
    923 		vput(DETOV(dep));
    924 	return (error);
    925 }
    926 
    927 /*
    928  * Read in the disk block containing the directory entry (dirclu, dirofs)
    929  * and return the address of the buf header, and the address of the
    930  * directory entry within the block.
    931  */
    932 int
    933 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct buf **bpp, struct direntry **epp)
    934 {
    935 	int error;
    936 	daddr_t bn;
    937 	int blsize;
    938 
    939 	blsize = pmp->pm_bpcluster;
    940 	if (dirclust == MSDOSFSROOT
    941 	    && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
    942 		blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
    943 	bn = detobn(pmp, dirclust, diroffset);
    944 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    945 	    0, bpp)) != 0) {
    946 		brelse(*bpp, 0);
    947 		*bpp = NULL;
    948 		return (error);
    949 	}
    950 	if (epp)
    951 		*epp = bptoep(pmp, *bpp, diroffset);
    952 	return (0);
    953 }
    954 
    955 /*
    956  * Read in the disk block containing the directory entry dep came from and
    957  * return the address of the buf header, and the address of the directory
    958  * entry within the block.
    959  */
    960 int
    961 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
    962 {
    963 	return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
    964 			bpp, epp));
    965 }
    966 
    967 /*
    968  * Remove a directory entry. At this point the file represented by the
    969  * directory entry to be removed is still full length until noone has it
    970  * open.  When the file no longer being used msdosfs_inactive() is called
    971  * and will truncate the file to 0 length.  When the vnode containing the
    972  * denode is needed for some other purpose by VFS it will call
    973  * msdosfs_reclaim() which will remove the denode from the denode cache.
    974  */
    975 int
    976 removede(struct denode *pdep, struct denode *dep)
    977 	/* pdep:	 directory where the entry is removed */
    978 	/* dep:	 file to be removed */
    979 {
    980 	int error;
    981 	struct direntry *ep;
    982 	struct buf *bp;
    983 	daddr_t bn;
    984 	int blsize;
    985 	struct msdosfsmount *pmp = pdep->de_pmp;
    986 	u_long offset = pdep->de_fndoffset;
    987 	int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
    988 
    989 #ifdef MSDOSFS_DEBUG
    990 	printf("removede(): filename %s, dep %p, offset %08lx\n",
    991 	    dep->de_Name, dep, offset);
    992 #endif
    993 
    994 	dep->de_refcnt--;
    995 	offset += sizeof(struct direntry);
    996 	do {
    997 		offset -= sizeof(struct direntry);
    998 		error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
    999 		if (error)
   1000 			return error;
   1001 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
   1002 		    B_MODIFY, &bp);
   1003 		if (error) {
   1004 			brelse(bp, 0);
   1005 			return error;
   1006 		}
   1007 		ep = bptoep(pmp, bp, offset);
   1008 		/*
   1009 		 * Check whether, if we came here the second time, i.e.
   1010 		 * when underflowing into the previous block, the last
   1011 		 * entry in this block is a longfilename entry, too.
   1012 		 */
   1013 		if (ep->deAttributes != ATTR_WIN95
   1014 		    && offset != pdep->de_fndoffset) {
   1015 			brelse(bp, 0);
   1016 			break;
   1017 		}
   1018 		offset += sizeof(struct direntry);
   1019 		while (1) {
   1020 			/*
   1021 			 * We are a bit agressive here in that we delete any Win95
   1022 			 * entries preceding this entry, not just the ones we "own".
   1023 			 * Since these presumably aren't valid anyway,
   1024 			 * there should be no harm.
   1025 			 */
   1026 			offset -= sizeof(struct direntry);
   1027 			ep--->deName[0] = SLOT_DELETED;
   1028 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
   1029 			    || !(offset & pmp->pm_crbomask)
   1030 			    || ep->deAttributes != ATTR_WIN95)
   1031 				break;
   1032 		}
   1033 		if (async)
   1034 			bdwrite(bp);
   1035 		else if ((error = bwrite(bp)) != 0)
   1036 			return error;
   1037 	} while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
   1038 	    && !(offset & pmp->pm_crbomask)
   1039 	    && offset);
   1040 	return 0;
   1041 }
   1042 
   1043 /*
   1044  * Create a unique DOS name in dvp
   1045  */
   1046 int
   1047 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
   1048 {
   1049 	struct msdosfsmount *pmp = dep->de_pmp;
   1050 	struct direntry *dentp;
   1051 	int gen;
   1052 	int blsize;
   1053 	u_long cn;
   1054 	daddr_t bn;
   1055 	struct buf *bp;
   1056 	int error;
   1057 
   1058 	for (gen = 1;; gen++) {
   1059 		/*
   1060 		 * Generate DOS name with generation number
   1061 		 */
   1062 		if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
   1063 		    cnp->cn_namelen, gen))
   1064 			return gen == 1 ? EINVAL : EEXIST;
   1065 
   1066 		/*
   1067 		 * Now look for a dir entry with this exact name
   1068 		 */
   1069 		for (cn = error = 0; !error; cn++) {
   1070 			if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
   1071 				if (error == E2BIG)	/* EOF reached and not found */
   1072 					return 0;
   1073 				return error;
   1074 			}
   1075 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
   1076 			    NOCRED, 0, &bp);
   1077 			if (error) {
   1078 				brelse(bp, 0);
   1079 				return error;
   1080 			}
   1081 			for (dentp = (struct direntry *)bp->b_data;
   1082 			     (char *)dentp < (char *)bp->b_data + blsize;
   1083 			     dentp++) {
   1084 				if (dentp->deName[0] == SLOT_EMPTY) {
   1085 					/*
   1086 					 * Last used entry and not found
   1087 					 */
   1088 					brelse(bp, 0);
   1089 					return 0;
   1090 				}
   1091 				/*
   1092 				 * Ignore volume labels and Win95 entries
   1093 				 */
   1094 				if (dentp->deAttributes & ATTR_VOLUME)
   1095 					continue;
   1096 				if (!memcmp(dentp->deName, cp, 11)) {
   1097 					error = EEXIST;
   1098 					break;
   1099 				}
   1100 			}
   1101 			brelse(bp, 0);
   1102 		}
   1103 	}
   1104 }
   1105 
   1106 /*
   1107  * Find any Win'95 long filename entry in directory dep
   1108  */
   1109 int
   1110 findwin95(struct denode *dep)
   1111 {
   1112 	struct msdosfsmount *pmp = dep->de_pmp;
   1113 	struct direntry *dentp;
   1114 	int blsize, win95;
   1115 	u_long cn;
   1116 	daddr_t bn;
   1117 	struct buf *bp;
   1118 
   1119 	win95 = 1;
   1120 	/*
   1121 	 * Read through the directory looking for Win'95 entries
   1122 	 * XXX Note: Error currently handled just as EOF
   1123 	 */
   1124 	for (cn = 0;; cn++) {
   1125 		if (pcbmap(dep, cn, &bn, 0, &blsize))
   1126 			return win95;
   1127 		if (bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
   1128 		    0, &bp)) {
   1129 			brelse(bp, 0);
   1130 			return win95;
   1131 		}
   1132 		for (dentp = (struct direntry *)bp->b_data;
   1133 		     (char *)dentp < (char *)bp->b_data + blsize;
   1134 		     dentp++) {
   1135 			if (dentp->deName[0] == SLOT_EMPTY) {
   1136 				/*
   1137 				 * Last used entry and not found
   1138 				 */
   1139 				brelse(bp, 0);
   1140 				return win95;
   1141 			}
   1142 			if (dentp->deName[0] == SLOT_DELETED) {
   1143 				/*
   1144 				 * Ignore deleted files
   1145 				 * Note: might be an indication of Win'95
   1146 				 * anyway	XXX
   1147 				 */
   1148 				continue;
   1149 			}
   1150 			if (dentp->deAttributes == ATTR_WIN95) {
   1151 				brelse(bp, 0);
   1152 				return 1;
   1153 			}
   1154 			win95 = 0;
   1155 		}
   1156 		brelse(bp, 0);
   1157 	}
   1158 }
   1159