Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_lookup.c revision 1.21
      1 /*	$NetBSD: msdosfs_lookup.c,v 1.21 2010/06/24 13:03:09 hannken 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.21 2010/06/24 13:03:09 hannken 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 		 * The pathname buffer is saved so that the name
    385 		 * can be obtained later.
    386 		 *
    387 		 * NB - if the directory is unlocked, then this
    388 		 * information cannot be used.
    389 		 */
    390 		cnp->cn_flags |= SAVENAME;
    391 		return (EJUSTRETURN);
    392 	}
    393 
    394 #if 0
    395 	/*
    396 	 * Insert name into cache (as non-existent) if appropriate.
    397 	 *
    398 	 * XXX Negative caching is broken for msdosfs because the name
    399 	 * cache doesn't understand peculiarities such as case insensitivity
    400 	 * and 8.3 filenames.  Hence, it may not invalidate all negative
    401 	 * entries if a file with this name is later created.
    402 	 * e.g. creating a file 'foo' won't invalidate a negative entry
    403 	 * for 'FOO'.
    404 	 */
    405 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
    406 		cache_enter(vdp, *vpp, cnp);
    407 #endif
    408 
    409 	return (ENOENT);
    410 
    411 found:
    412 	/*
    413 	 * NOTE:  We still have the buffer with matched directory entry at
    414 	 * this point.
    415 	 */
    416 	isadir = dep->deAttributes & ATTR_DIRECTORY;
    417 	scn = getushort(dep->deStartCluster);
    418 	if (FAT32(pmp)) {
    419 		scn |= getushort(dep->deHighClust) << 16;
    420 		if (scn == pmp->pm_rootdirblk) {
    421 			/*
    422 			 * There should actually be 0 here.
    423 			 * Just ignore the error.
    424 			 */
    425 			scn = MSDOSFSROOT;
    426 		}
    427 	}
    428 
    429 	if (isadir) {
    430 		cluster = scn;
    431 		if (cluster == MSDOSFSROOT)
    432 			blkoff = MSDOSFSROOT_OFS;
    433 		else
    434 			blkoff = 0;
    435 	} else if (cluster == MSDOSFSROOT)
    436 		blkoff = diroff;
    437 
    438 	/*
    439 	 * Now release buf to allow deget to read the entry again.
    440 	 * Reserving it here and giving it to deget could result
    441 	 * in a deadlock.
    442 	 */
    443 	brelse(bp, 0);
    444 
    445 foundroot:
    446 	/*
    447 	 * If we entered at foundroot, then we are looking for the . or ..
    448 	 * entry of the filesystems root directory.  isadir and scn were
    449 	 * setup before jumping here.  And, bp is already null.
    450 	 */
    451 	if (FAT32(pmp) && scn == MSDOSFSROOT)
    452 		scn = pmp->pm_rootdirblk;
    453 
    454 	/*
    455 	 * If deleting, and at end of pathname, return
    456 	 * parameters which can be used to remove file.
    457 	 * Lock the inode, being careful with ".".
    458 	 */
    459 	if (nameiop == DELETE && (flags & ISLASTCN)) {
    460 		/*
    461 		 * Don't allow deleting the root.
    462 		 */
    463 		if (blkoff == MSDOSFSROOT_OFS)
    464 			return EROFS;			/* really? XXX */
    465 
    466 		/*
    467 		 * Write access to directory required to delete files.
    468 		 */
    469 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
    470 		if (error)
    471 			return (error);
    472 
    473 		/*
    474 		 * Return pointer to current entry in dp->i_offset.
    475 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
    476 		 */
    477 		if (dp->de_StartCluster == scn && isadir) {	/* "." */
    478 			vref(vdp);
    479 			*vpp = vdp;
    480 			return (0);
    481 		}
    482 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
    483 			return (error);
    484 		*vpp = DETOV(tdp);
    485 		return (0);
    486 	}
    487 
    488 	/*
    489 	 * If rewriting (RENAME), return the inode and the
    490 	 * information required to rewrite the present directory
    491 	 * Must get inode of directory entry to verify it's a
    492 	 * regular file, or empty directory.
    493 	 */
    494 	if (nameiop == RENAME && (flags & ISLASTCN)) {
    495 
    496 		if (vdp->v_mount->mnt_flag & MNT_RDONLY)
    497 			return (EROFS);
    498 
    499 		if (blkoff == MSDOSFSROOT_OFS)
    500 			return EROFS;				/* really? XXX */
    501 
    502 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
    503 		if (error)
    504 			return (error);
    505 
    506 		/*
    507 		 * Careful about locking second inode.
    508 		 * This can only occur if the target is ".".
    509 		 */
    510 		if (dp->de_StartCluster == scn && isadir)
    511 			return (EISDIR);
    512 
    513 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
    514 			return (error);
    515 		*vpp = DETOV(tdp);
    516 		cnp->cn_flags |= SAVENAME;
    517 		return (0);
    518 	}
    519 
    520 	/*
    521 	 * Step through the translation in the name.  We do not `vput' the
    522 	 * directory because we may need it again if a symbolic link
    523 	 * is relative to the current directory.  Instead we save it
    524 	 * unlocked as "pdp".  We must get the target inode before unlocking
    525 	 * the directory to insure that the inode will not be removed
    526 	 * before we get it.  We prevent deadlock by always fetching
    527 	 * inodes from the root, moving down the directory tree. Thus
    528 	 * when following backward pointers ".." we must unlock the
    529 	 * parent directory before getting the requested directory.
    530 	 * There is a potential race condition here if both the current
    531 	 * and parent directories are removed before the VFS_VGET for the
    532 	 * inode associated with ".." returns.  We hope that this occurs
    533 	 * infrequently since we cannot avoid this race condition without
    534 	 * implementing a sophisticated deadlock detection algorithm.
    535 	 * Note also that this simple deadlock detection scheme will not
    536 	 * work if the file system has any hard links other than ".."
    537 	 * that point backwards in the directory structure.
    538 	 */
    539 	pdp = vdp;
    540 	if (flags & ISDOTDOT) {
    541 		VOP_UNLOCK(pdp);	/* race to get the inode */
    542 		error = deget(pmp, cluster, blkoff, &tdp);
    543 		vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
    544 		if (error) {
    545 			return error;
    546 		}
    547 		*vpp = DETOV(tdp);
    548 	} else if (dp->de_StartCluster == scn && isadir) {
    549 		vref(vdp);	/* we want ourself, ie "." */
    550 		*vpp = vdp;
    551 	} else {
    552 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
    553 			return (error);
    554 		*vpp = DETOV(tdp);
    555 	}
    556 
    557 	/*
    558 	 * Insert name into cache if appropriate.
    559 	 */
    560 	if (cnp->cn_flags & MAKEENTRY)
    561 		cache_enter(vdp, *vpp, cnp);
    562 
    563 	return (0);
    564 }
    565 
    566 /*
    567  * dep  - directory entry to copy into the directory
    568  * ddep - directory to add to
    569  * depp - return the address of the denode for the created directory entry
    570  *	  if depp != 0
    571  * cnp  - componentname needed for Win95 long filenames
    572  */
    573 int
    574 createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp)
    575 {
    576 	int error, rberror;
    577 	u_long dirclust, clusoffset;
    578 	u_long fndoffset, havecnt=0, wcnt=1;
    579 	struct direntry *ndep;
    580 	struct msdosfsmount *pmp = ddep->de_pmp;
    581 	struct buf *bp;
    582 	daddr_t bn;
    583 	int blsize, i;
    584 	int async = ddep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
    585 
    586 #ifdef MSDOSFS_DEBUG
    587 	printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
    588 	    dep, ddep, depp, cnp);
    589 #endif
    590 
    591 	/*
    592 	 * If no space left in the directory then allocate another cluster
    593 	 * and chain it onto the end of the file.  There is one exception
    594 	 * to this.  That is, if the root directory has no more space it
    595 	 * can NOT be expanded.  extendfile() checks for and fails attempts
    596 	 * to extend the root directory.  We just return an error in that
    597 	 * case.
    598 	 */
    599 	if (ddep->de_fndoffset >= ddep->de_FileSize) {
    600 		u_long needlen = ddep->de_fndoffset + sizeof(struct direntry)
    601 		    - ddep->de_FileSize;
    602 		dirclust = de_clcount(pmp, needlen);
    603 		if ((error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR)) != 0) {
    604 			(void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
    605 			goto err_norollback;
    606 		}
    607 
    608 		/*
    609 		 * Update the size of the directory
    610 		 */
    611 		ddep->de_FileSize += de_cn2off(pmp, dirclust);
    612 	}
    613 
    614 	/*
    615 	 * We just read in the cluster with space.  Copy the new directory
    616 	 * entry in.  Then write it to disk. NOTE:  DOS directories
    617 	 * do not get smaller as clusters are emptied.
    618 	 */
    619 	error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
    620 		       &bn, &dirclust, &blsize);
    621 	if (error)
    622 		goto err_norollback;
    623 	clusoffset = ddep->de_fndoffset;
    624 	if (dirclust != MSDOSFSROOT)
    625 		clusoffset &= pmp->pm_crbomask;
    626 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    627 	    B_MODIFY, &bp)) != 0) {
    628 		brelse(bp, 0);
    629 		goto err_norollback;
    630 	}
    631 	ndep = bptoep(pmp, bp, clusoffset);
    632 
    633 	DE_EXTERNALIZE(ndep, dep);
    634 
    635 	/*
    636 	 * Now write the Win95 long name
    637 	 */
    638 	if (ddep->de_fndcnt > 0) {
    639 		u_int8_t chksum = winChksum(ndep->deName);
    640 		const u_char *un = (const u_char *)cnp->cn_nameptr;
    641 		int unlen = cnp->cn_namelen;
    642 		u_long xhavecnt;
    643 
    644 		fndoffset = ddep->de_fndoffset;
    645 		xhavecnt = ddep->de_fndcnt + 1;
    646 
    647 		for(; wcnt < xhavecnt; wcnt++) {
    648 			if ((fndoffset & pmp->pm_crbomask) == 0) {
    649 				/* we should never get here if ddep is root
    650 				 * directory */
    651 
    652 				if (async)
    653 					(void) bdwrite(bp);
    654 				else if ((error = bwrite(bp)) != 0)
    655 					goto rollback;
    656 
    657 				fndoffset -= sizeof(struct direntry);
    658 				error = pcbmap(ddep,
    659 					       de_cluster(pmp, fndoffset),
    660 					       &bn, 0, &blsize);
    661 				if (error)
    662 					goto rollback;
    663 
    664 				error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
    665 				    blsize, NOCRED, B_MODIFY, &bp);
    666 				if (error) {
    667 					brelse(bp, 0);
    668 					goto rollback;
    669 				}
    670 				ndep = bptoep(pmp, bp,
    671 						fndoffset & pmp->pm_crbomask);
    672 			} else {
    673 				ndep--;
    674 				fndoffset -= sizeof(struct direntry);
    675 			}
    676 			if (!unix2winfn(un, unlen, (struct winentry *)ndep,
    677 						wcnt, chksum))
    678 				break;
    679 		}
    680 	}
    681 
    682 	if (async)
    683 		bdwrite(bp);
    684 	else if ((error = bwrite(bp)) != 0)
    685 		goto rollback;
    686 
    687 	/*
    688 	 * If they want us to return with the denode gotten.
    689 	 */
    690 	if (depp) {
    691 		u_long diroffset = clusoffset;
    692 		if (dep->de_Attributes & ATTR_DIRECTORY) {
    693 			dirclust = dep->de_StartCluster;
    694 			if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
    695 				dirclust = MSDOSFSROOT;
    696 			if (dirclust == MSDOSFSROOT)
    697 				diroffset = MSDOSFSROOT_OFS;
    698 			else
    699 				diroffset = 0;
    700 		}
    701 		return deget(pmp, dirclust, diroffset, depp);
    702 	}
    703 
    704 	return 0;
    705 
    706     rollback:
    707 	/*
    708 	 * Mark all slots modified so far as deleted. Note that we
    709 	 * can't just call removede(), since directory is not in
    710 	 * consistent state.
    711 	 */
    712 	fndoffset = ddep->de_fndoffset;
    713 	rberror = pcbmap(ddep, de_cluster(pmp, fndoffset),
    714 	       &bn, NULL, &blsize);
    715 	if (rberror)
    716 		goto err_norollback;
    717 	if ((rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    718 	    B_MODIFY, &bp)) != 0) {
    719 		brelse(bp, 0);
    720 		goto err_norollback;
    721 	}
    722 	ndep = bptoep(pmp, bp, clusoffset);
    723 
    724 	havecnt = ddep->de_fndcnt + 1;
    725 	for(i=wcnt; i <= havecnt; i++) {
    726 		/* mark entry as deleted */
    727 		ndep->deName[0] = SLOT_DELETED;
    728 
    729 		if ((fndoffset & pmp->pm_crbomask) == 0) {
    730 			/* we should never get here if ddep is root
    731 			 * directory */
    732 
    733 			if (async)
    734 				bdwrite(bp);
    735 			else if ((rberror = bwrite(bp)) != 0)
    736 				goto err_norollback;
    737 
    738 			fndoffset -= sizeof(struct direntry);
    739 			rberror = pcbmap(ddep,
    740 				       de_cluster(pmp, fndoffset),
    741 				       &bn, 0, &blsize);
    742 			if (rberror)
    743 				goto err_norollback;
    744 
    745 			rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
    746 			    blsize, NOCRED, B_MODIFY, &bp);
    747 			if (rberror) {
    748 				brelse(bp, 0);
    749 				goto err_norollback;
    750 			}
    751 			ndep = bptoep(pmp, bp, fndoffset);
    752 		} else {
    753 			ndep--;
    754 			fndoffset -= sizeof(struct direntry);
    755 		}
    756 	}
    757 
    758 	/* ignore any further error */
    759 	if (async)
    760 		(void) bdwrite(bp);
    761 	else
    762 		(void) bwrite(bp);
    763 
    764     err_norollback:
    765 	return error;
    766 }
    767 
    768 /*
    769  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
    770  * return 0 if not empty or error.
    771  */
    772 int
    773 dosdirempty(struct denode *dep)
    774 {
    775 	int blsize;
    776 	int error;
    777 	u_long cn;
    778 	daddr_t bn;
    779 	struct buf *bp;
    780 	struct msdosfsmount *pmp = dep->de_pmp;
    781 	struct direntry *dentp;
    782 
    783 	/*
    784 	 * Since the filesize field in directory entries for a directory is
    785 	 * zero, we just have to feel our way through the directory until
    786 	 * we hit end of file.
    787 	 */
    788 	for (cn = 0;; cn++) {
    789 		if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
    790 			if (error == E2BIG)
    791 				return (1);	/* it's empty */
    792 			return (0);
    793 		}
    794 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    795 		    0, &bp);
    796 		if (error) {
    797 			brelse(bp, 0);
    798 			return (0);
    799 		}
    800 		for (dentp = (struct direntry *)bp->b_data;
    801 		     (char *)dentp < (char *)bp->b_data + blsize;
    802 		     dentp++) {
    803 			if (dentp->deName[0] != SLOT_DELETED &&
    804 			    (dentp->deAttributes & ATTR_VOLUME) == 0) {
    805 				/*
    806 				 * In dos directories an entry whose name
    807 				 * starts with SLOT_EMPTY (0) starts the
    808 				 * beginning of the unused part of the
    809 				 * directory, so we can just return that it
    810 				 * is empty.
    811 				 */
    812 				if (dentp->deName[0] == SLOT_EMPTY) {
    813 					brelse(bp, 0);
    814 					return (1);
    815 				}
    816 				/*
    817 				 * Any names other than "." and ".." in a
    818 				 * directory mean it is not empty.
    819 				 */
    820 				if (memcmp(dentp->deName, ".          ", 11) &&
    821 				    memcmp(dentp->deName, "..         ", 11)) {
    822 					brelse(bp, 0);
    823 #ifdef MSDOSFS_DEBUG
    824 					printf("dosdirempty(): found %.11s, %d, %d\n",
    825 					    dentp->deName, dentp->deName[0],
    826 						dentp->deName[1]);
    827 #endif
    828 					return (0);	/* not empty */
    829 				}
    830 			}
    831 		}
    832 		brelse(bp, 0);
    833 	}
    834 	/* NOTREACHED */
    835 }
    836 
    837 /*
    838  * Check to see if the directory described by target is in some
    839  * subdirectory of source.  This prevents something like the following from
    840  * succeeding and leaving a bunch or files and directories orphaned. mv
    841  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
    842  *
    843  * source - the inode for /a/b/c
    844  * target - the inode for /a/b/c/d/e/f
    845  *
    846  * Returns 0 if target is NOT a subdirectory of source.
    847  * Otherwise returns a non-zero error number.
    848  * The target inode is always unlocked on return.
    849  */
    850 int
    851 doscheckpath(struct denode *source, struct denode *target)
    852 {
    853 	u_long scn;
    854 	struct msdosfsmount *pmp;
    855 	struct direntry *ep;
    856 	struct denode *dep;
    857 	struct buf *bp = NULL;
    858 	int error = 0;
    859 
    860 	dep = target;
    861 	if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
    862 	    (source->de_Attributes & ATTR_DIRECTORY) == 0) {
    863 		error = ENOTDIR;
    864 		goto out;
    865 	}
    866 	if (dep->de_StartCluster == source->de_StartCluster) {
    867 		error = EEXIST;
    868 		goto out;
    869 	}
    870 	if (dep->de_StartCluster == MSDOSFSROOT)
    871 		goto out;
    872 	pmp = dep->de_pmp;
    873 #ifdef	DIAGNOSTIC
    874 	if (pmp != source->de_pmp)
    875 		panic("doscheckpath: source and target on different filesystems");
    876 #endif
    877 	if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
    878 		goto out;
    879 
    880 	for (;;) {
    881 		if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
    882 			error = ENOTDIR;
    883 			break;
    884 		}
    885 		scn = dep->de_StartCluster;
    886 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, cntobn(pmp, scn)),
    887 			      pmp->pm_bpcluster, NOCRED, 0, &bp);
    888 		if (error)
    889 			break;
    890 
    891 		ep = (struct direntry *) bp->b_data + 1;
    892 		if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
    893 		    memcmp(ep->deName, "..         ", 11) != 0) {
    894 			error = ENOTDIR;
    895 			break;
    896 		}
    897 		scn = getushort(ep->deStartCluster);
    898 		if (FAT32(pmp))
    899 			scn |= getushort(ep->deHighClust) << 16;
    900 
    901 		if (scn == source->de_StartCluster) {
    902 			error = EINVAL;
    903 			break;
    904 		}
    905 		if (scn == MSDOSFSROOT)
    906 			break;
    907 		if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
    908 			/*
    909 			 * scn should be 0 in this case,
    910 			 * but we silently ignore the error.
    911 			 */
    912 			break;
    913 		}
    914 
    915 		vput(DETOV(dep));
    916 		brelse(bp, 0);
    917 		bp = NULL;
    918 		/* NOTE: deget() clears dep on error */
    919 		if ((error = deget(pmp, scn, 0, &dep)) != 0)
    920 			break;
    921 	}
    922 out:
    923 	if (bp)
    924 		brelse(bp, 0);
    925 	if (error == ENOTDIR)
    926 		printf("doscheckpath(): .. not a directory?\n");
    927 	if (dep != NULL)
    928 		vput(DETOV(dep));
    929 	return (error);
    930 }
    931 
    932 /*
    933  * Read in the disk block containing the directory entry (dirclu, dirofs)
    934  * and return the address of the buf header, and the address of the
    935  * directory entry within the block.
    936  */
    937 int
    938 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct buf **bpp, struct direntry **epp)
    939 {
    940 	int error;
    941 	daddr_t bn;
    942 	int blsize;
    943 
    944 	blsize = pmp->pm_bpcluster;
    945 	if (dirclust == MSDOSFSROOT
    946 	    && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
    947 		blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
    948 	bn = detobn(pmp, dirclust, diroffset);
    949 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
    950 	    0, bpp)) != 0) {
    951 		brelse(*bpp, 0);
    952 		*bpp = NULL;
    953 		return (error);
    954 	}
    955 	if (epp)
    956 		*epp = bptoep(pmp, *bpp, diroffset);
    957 	return (0);
    958 }
    959 
    960 /*
    961  * Read in the disk block containing the directory entry dep came from and
    962  * return the address of the buf header, and the address of the directory
    963  * entry within the block.
    964  */
    965 int
    966 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
    967 {
    968 	return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
    969 			bpp, epp));
    970 }
    971 
    972 /*
    973  * Remove a directory entry. At this point the file represented by the
    974  * directory entry to be removed is still full length until noone has it
    975  * open.  When the file no longer being used msdosfs_inactive() is called
    976  * and will truncate the file to 0 length.  When the vnode containing the
    977  * denode is needed for some other purpose by VFS it will call
    978  * msdosfs_reclaim() which will remove the denode from the denode cache.
    979  */
    980 int
    981 removede(struct denode *pdep, struct denode *dep)
    982 	/* pdep:	 directory where the entry is removed */
    983 	/* dep:	 file to be removed */
    984 {
    985 	int error;
    986 	struct direntry *ep;
    987 	struct buf *bp;
    988 	daddr_t bn;
    989 	int blsize;
    990 	struct msdosfsmount *pmp = pdep->de_pmp;
    991 	u_long offset = pdep->de_fndoffset;
    992 	int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
    993 
    994 #ifdef MSDOSFS_DEBUG
    995 	printf("removede(): filename %s, dep %p, offset %08lx\n",
    996 	    dep->de_Name, dep, offset);
    997 #endif
    998 
    999 	dep->de_refcnt--;
   1000 	offset += sizeof(struct direntry);
   1001 	do {
   1002 		offset -= sizeof(struct direntry);
   1003 		error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
   1004 		if (error)
   1005 			return error;
   1006 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
   1007 		    B_MODIFY, &bp);
   1008 		if (error) {
   1009 			brelse(bp, 0);
   1010 			return error;
   1011 		}
   1012 		ep = bptoep(pmp, bp, offset);
   1013 		/*
   1014 		 * Check whether, if we came here the second time, i.e.
   1015 		 * when underflowing into the previous block, the last
   1016 		 * entry in this block is a longfilename entry, too.
   1017 		 */
   1018 		if (ep->deAttributes != ATTR_WIN95
   1019 		    && offset != pdep->de_fndoffset) {
   1020 			brelse(bp, 0);
   1021 			break;
   1022 		}
   1023 		offset += sizeof(struct direntry);
   1024 		while (1) {
   1025 			/*
   1026 			 * We are a bit agressive here in that we delete any Win95
   1027 			 * entries preceding this entry, not just the ones we "own".
   1028 			 * Since these presumably aren't valid anyway,
   1029 			 * there should be no harm.
   1030 			 */
   1031 			offset -= sizeof(struct direntry);
   1032 			ep--->deName[0] = SLOT_DELETED;
   1033 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
   1034 			    || !(offset & pmp->pm_crbomask)
   1035 			    || ep->deAttributes != ATTR_WIN95)
   1036 				break;
   1037 		}
   1038 		if (async)
   1039 			bdwrite(bp);
   1040 		else if ((error = bwrite(bp)) != 0)
   1041 			return error;
   1042 	} while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
   1043 	    && !(offset & pmp->pm_crbomask)
   1044 	    && offset);
   1045 	return 0;
   1046 }
   1047 
   1048 /*
   1049  * Create a unique DOS name in dvp
   1050  */
   1051 int
   1052 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
   1053 {
   1054 	struct msdosfsmount *pmp = dep->de_pmp;
   1055 	struct direntry *dentp;
   1056 	int gen;
   1057 	int blsize;
   1058 	u_long cn;
   1059 	daddr_t bn;
   1060 	struct buf *bp;
   1061 	int error;
   1062 
   1063 	for (gen = 1;; gen++) {
   1064 		/*
   1065 		 * Generate DOS name with generation number
   1066 		 */
   1067 		if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
   1068 		    cnp->cn_namelen, gen))
   1069 			return gen == 1 ? EINVAL : EEXIST;
   1070 
   1071 		/*
   1072 		 * Now look for a dir entry with this exact name
   1073 		 */
   1074 		for (cn = error = 0; !error; cn++) {
   1075 			if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
   1076 				if (error == E2BIG)	/* EOF reached and not found */
   1077 					return 0;
   1078 				return error;
   1079 			}
   1080 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
   1081 			    NOCRED, 0, &bp);
   1082 			if (error) {
   1083 				brelse(bp, 0);
   1084 				return error;
   1085 			}
   1086 			for (dentp = (struct direntry *)bp->b_data;
   1087 			     (char *)dentp < (char *)bp->b_data + blsize;
   1088 			     dentp++) {
   1089 				if (dentp->deName[0] == SLOT_EMPTY) {
   1090 					/*
   1091 					 * Last used entry and not found
   1092 					 */
   1093 					brelse(bp, 0);
   1094 					return 0;
   1095 				}
   1096 				/*
   1097 				 * Ignore volume labels and Win95 entries
   1098 				 */
   1099 				if (dentp->deAttributes & ATTR_VOLUME)
   1100 					continue;
   1101 				if (!memcmp(dentp->deName, cp, 11)) {
   1102 					error = EEXIST;
   1103 					break;
   1104 				}
   1105 			}
   1106 			brelse(bp, 0);
   1107 		}
   1108 	}
   1109 }
   1110 
   1111 /*
   1112  * Find any Win'95 long filename entry in directory dep
   1113  */
   1114 int
   1115 findwin95(struct denode *dep)
   1116 {
   1117 	struct msdosfsmount *pmp = dep->de_pmp;
   1118 	struct direntry *dentp;
   1119 	int blsize, win95;
   1120 	u_long cn;
   1121 	daddr_t bn;
   1122 	struct buf *bp;
   1123 
   1124 	win95 = 1;
   1125 	/*
   1126 	 * Read through the directory looking for Win'95 entries
   1127 	 * XXX Note: Error currently handled just as EOF
   1128 	 */
   1129 	for (cn = 0;; cn++) {
   1130 		if (pcbmap(dep, cn, &bn, 0, &blsize))
   1131 			return win95;
   1132 		if (bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
   1133 		    0, &bp)) {
   1134 			brelse(bp, 0);
   1135 			return win95;
   1136 		}
   1137 		for (dentp = (struct direntry *)bp->b_data;
   1138 		     (char *)dentp < (char *)bp->b_data + blsize;
   1139 		     dentp++) {
   1140 			if (dentp->deName[0] == SLOT_EMPTY) {
   1141 				/*
   1142 				 * Last used entry and not found
   1143 				 */
   1144 				brelse(bp, 0);
   1145 				return win95;
   1146 			}
   1147 			if (dentp->deName[0] == SLOT_DELETED) {
   1148 				/*
   1149 				 * Ignore deleted files
   1150 				 * Note: might be an indication of Win'95
   1151 				 * anyway	XXX
   1152 				 */
   1153 				continue;
   1154 			}
   1155 			if (dentp->deAttributes == ATTR_WIN95) {
   1156 				brelse(bp, 0);
   1157 				return 1;
   1158 			}
   1159 			win95 = 0;
   1160 		}
   1161 		brelse(bp, 0);
   1162 	}
   1163 }
   1164