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