xref: /minix3/sys/fs/msdosfs/msdosfs_lookup.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: msdosfs_lookup.c,v 1.34 2015/03/28 19:24:05 maxv Exp $	*/
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
584d9c625SLionel Sambuc  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
684d9c625SLionel Sambuc  * All rights reserved.
784d9c625SLionel Sambuc  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
884d9c625SLionel Sambuc  *
984d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1084d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1184d9c625SLionel Sambuc  * are met:
1284d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1384d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1484d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1584d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1684d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1784d9c625SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
1884d9c625SLionel Sambuc  *    must display the following acknowledgement:
1984d9c625SLionel Sambuc  *	This product includes software developed by TooLs GmbH.
2084d9c625SLionel Sambuc  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2184d9c625SLionel Sambuc  *    derived from this software without specific prior written permission.
2284d9c625SLionel Sambuc  *
2384d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2484d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2584d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2684d9c625SLionel Sambuc  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2784d9c625SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2884d9c625SLionel Sambuc  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2984d9c625SLionel Sambuc  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3084d9c625SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3184d9c625SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3284d9c625SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3384d9c625SLionel Sambuc  */
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * Written by Paul Popelka (paulp@uts.amdahl.com)
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  * You can do anything you want with this software, just don't say you wrote
3884d9c625SLionel Sambuc  * it, and don't remove this notice.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * This software is provided "as is".
4184d9c625SLionel Sambuc  *
4284d9c625SLionel Sambuc  * The author supplies this software to be publicly redistributed on the
4384d9c625SLionel Sambuc  * understanding that the author is not responsible for the correct
4484d9c625SLionel Sambuc  * functioning of this software in any circumstances and is not liable for
4584d9c625SLionel Sambuc  * any damages caused by this software.
4684d9c625SLionel Sambuc  *
4784d9c625SLionel Sambuc  * October 1992
4884d9c625SLionel Sambuc  */
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
5184d9c625SLionel Sambuc #include "nbtool_config.h"
5284d9c625SLionel Sambuc #endif
5384d9c625SLionel Sambuc 
5484d9c625SLionel Sambuc #include <sys/cdefs.h>
55*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: msdosfs_lookup.c,v 1.34 2015/03/28 19:24:05 maxv Exp $");
5684d9c625SLionel Sambuc 
5784d9c625SLionel Sambuc #include <sys/param.h>
5884d9c625SLionel Sambuc 
5984d9c625SLionel Sambuc #ifdef _KERNEL
6084d9c625SLionel Sambuc #include <sys/systm.h>
6184d9c625SLionel Sambuc #include <sys/mount.h>
6284d9c625SLionel Sambuc #include <sys/kauth.h>
6384d9c625SLionel Sambuc #include <sys/namei.h>
6484d9c625SLionel Sambuc #include <sys/dirent.h>
6584d9c625SLionel Sambuc #include <sys/buf.h>
6684d9c625SLionel Sambuc #include <sys/vnode.h>
67*0a6a1f1dSLionel Sambuc #include <sys/atomic.h>
6884d9c625SLionel Sambuc #else
6984d9c625SLionel Sambuc #include <ffs/buf.h>
7084d9c625SLionel Sambuc #endif /* _KERNEL */
7184d9c625SLionel Sambuc 
7284d9c625SLionel Sambuc #include <fs/msdosfs/bpb.h>
7384d9c625SLionel Sambuc #include <fs/msdosfs/direntry.h>
7484d9c625SLionel Sambuc #include <fs/msdosfs/denode.h>
7584d9c625SLionel Sambuc #include <fs/msdosfs/msdosfsmount.h>
7684d9c625SLionel Sambuc #include <fs/msdosfs/fat.h>
7784d9c625SLionel Sambuc 
7884d9c625SLionel Sambuc 
7984d9c625SLionel Sambuc #ifdef _KERNEL
8084d9c625SLionel Sambuc /*
8184d9c625SLionel Sambuc  * When we search a directory the blocks containing directory entries are
8284d9c625SLionel Sambuc  * read and examined.  The directory entries contain information that would
8384d9c625SLionel Sambuc  * normally be in the inode of a unix filesystem.  This means that some of
8484d9c625SLionel Sambuc  * a directory's contents may also be in memory resident denodes (sort of
8584d9c625SLionel Sambuc  * an inode).  This can cause problems if we are searching while some other
8684d9c625SLionel Sambuc  * process is modifying a directory.  To prevent one process from accessing
8784d9c625SLionel Sambuc  * incompletely modified directory information we depend upon being the
8884d9c625SLionel Sambuc  * sole owner of a directory block.  bread/brelse provide this service.
8984d9c625SLionel Sambuc  * This being the case, when a process modifies a directory it must first
9084d9c625SLionel Sambuc  * acquire the disk block that contains the directory entry to be modified.
9184d9c625SLionel Sambuc  * Then update the disk block and the denode, and then write the disk block
9284d9c625SLionel Sambuc  * out to disk.  This way disk blocks containing directory entries and in
9384d9c625SLionel Sambuc  * memory denode's will be in synch.
9484d9c625SLionel Sambuc  */
9584d9c625SLionel Sambuc int
msdosfs_lookup(void * v)9684d9c625SLionel Sambuc msdosfs_lookup(void *v)
9784d9c625SLionel Sambuc {
98*0a6a1f1dSLionel Sambuc 	struct vop_lookup_v2_args /* {
9984d9c625SLionel Sambuc 		struct vnode *a_dvp;
10084d9c625SLionel Sambuc 		struct vnode **a_vpp;
10184d9c625SLionel Sambuc 		struct componentname *a_cnp;
10284d9c625SLionel Sambuc 	} */ *ap = v;
10384d9c625SLionel Sambuc 	struct vnode *vdp = ap->a_dvp;
10484d9c625SLionel Sambuc 	struct vnode **vpp = ap->a_vpp;
10584d9c625SLionel Sambuc 	struct componentname *cnp = ap->a_cnp;
10684d9c625SLionel Sambuc 	daddr_t bn;
10784d9c625SLionel Sambuc 	int error;
10884d9c625SLionel Sambuc 	int slotcount;
10984d9c625SLionel Sambuc 	int slotoffset = 0;
11084d9c625SLionel Sambuc 	int frcn;
11184d9c625SLionel Sambuc 	u_long cluster;
11284d9c625SLionel Sambuc 	int blkoff;
11384d9c625SLionel Sambuc 	int diroff;
11484d9c625SLionel Sambuc 	int blsize;
11584d9c625SLionel Sambuc 	int isadir;		/* ~0 if found direntry is a directory	 */
11684d9c625SLionel Sambuc 	u_long scn;		/* starting cluster number		 */
11784d9c625SLionel Sambuc 	struct denode *dp;
11884d9c625SLionel Sambuc 	struct msdosfsmount *pmp;
11984d9c625SLionel Sambuc 	struct buf *bp = 0;
12084d9c625SLionel Sambuc 	struct direntry *dep;
12184d9c625SLionel Sambuc 	u_char dosfilename[12];
12284d9c625SLionel Sambuc 	int flags;
12384d9c625SLionel Sambuc 	int nameiop = cnp->cn_nameiop;
12484d9c625SLionel Sambuc 	int wincnt = 1;
12584d9c625SLionel Sambuc 	int chksum = -1, chksum_ok;
12684d9c625SLionel Sambuc 	int olddos = 1;
12784d9c625SLionel Sambuc 
12884d9c625SLionel Sambuc 	flags = cnp->cn_flags;
12984d9c625SLionel Sambuc 
13084d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
13184d9c625SLionel Sambuc 	printf("msdosfs_lookup(): looking for %.*s\n",
13284d9c625SLionel Sambuc 		(int)cnp->cn_namelen, cnp->cn_nameptr);
13384d9c625SLionel Sambuc #endif
13484d9c625SLionel Sambuc 	dp = VTODE(vdp);
13584d9c625SLionel Sambuc 	pmp = dp->de_pmp;
13684d9c625SLionel Sambuc 	*vpp = NULL;
13784d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
13884d9c625SLionel Sambuc 	printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
13984d9c625SLionel Sambuc 	    vdp, dp, dp->de_Attributes);
14084d9c625SLionel Sambuc #endif
14184d9c625SLionel Sambuc 
14284d9c625SLionel Sambuc 	/*
14384d9c625SLionel Sambuc 	 * Check accessiblity of directory.
14484d9c625SLionel Sambuc 	 */
14584d9c625SLionel Sambuc 	if ((error = VOP_ACCESS(vdp, VEXEC, cnp->cn_cred)) != 0)
14684d9c625SLionel Sambuc 		return (error);
14784d9c625SLionel Sambuc 
14884d9c625SLionel Sambuc 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
14984d9c625SLionel Sambuc 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
15084d9c625SLionel Sambuc 		return (EROFS);
15184d9c625SLionel Sambuc 
15284d9c625SLionel Sambuc 	/*
15384d9c625SLionel Sambuc 	 * We now have a segment name to search for, and a directory to search.
15484d9c625SLionel Sambuc 	 *
15584d9c625SLionel Sambuc 	 * Before tediously performing a linear scan of the directory,
15684d9c625SLionel Sambuc 	 * check the name cache to see if the directory/name pair
15784d9c625SLionel Sambuc 	 * we are looking for is known already.
15884d9c625SLionel Sambuc 	 */
15984d9c625SLionel Sambuc 	if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
16084d9c625SLionel Sambuc 			 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
16184d9c625SLionel Sambuc 		return *vpp == NULLVP ? ENOENT: 0;
16284d9c625SLionel Sambuc 	}
16384d9c625SLionel Sambuc 
16484d9c625SLionel Sambuc 	/*
16584d9c625SLionel Sambuc 	 * If they are going after the . or .. entry in the root directory,
16684d9c625SLionel Sambuc 	 * they won't find it.  DOS filesystems don't have them in the root
16784d9c625SLionel Sambuc 	 * directory.  So, we fake it. deget() is in on this scam too.
16884d9c625SLionel Sambuc 	 */
16984d9c625SLionel Sambuc 	if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
17084d9c625SLionel Sambuc 	    (cnp->cn_namelen == 1 ||
17184d9c625SLionel Sambuc 		(cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
17284d9c625SLionel Sambuc 		isadir = ATTR_DIRECTORY;
17384d9c625SLionel Sambuc 		scn = MSDOSFSROOT;
17484d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
17584d9c625SLionel Sambuc 		printf("msdosfs_lookup(): looking for . or .. in root directory\n");
17684d9c625SLionel Sambuc #endif
17784d9c625SLionel Sambuc 		cluster = MSDOSFSROOT;
17884d9c625SLionel Sambuc 		blkoff = MSDOSFSROOT_OFS;
17984d9c625SLionel Sambuc 		goto foundroot;
18084d9c625SLionel Sambuc 	}
18184d9c625SLionel Sambuc 
18284d9c625SLionel Sambuc 	switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
18384d9c625SLionel Sambuc 	    cnp->cn_namelen, 0)) {
18484d9c625SLionel Sambuc 	case 0:
18584d9c625SLionel Sambuc 		return (EINVAL);
18684d9c625SLionel Sambuc 	case 1:
18784d9c625SLionel Sambuc 		break;
18884d9c625SLionel Sambuc 	case 2:
18984d9c625SLionel Sambuc 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
19084d9c625SLionel Sambuc 		    cnp->cn_namelen) + 1;
19184d9c625SLionel Sambuc 		break;
19284d9c625SLionel Sambuc 	case 3:
19384d9c625SLionel Sambuc 		olddos = 0;
19484d9c625SLionel Sambuc 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
19584d9c625SLionel Sambuc 		    cnp->cn_namelen) + 1;
19684d9c625SLionel Sambuc 		break;
19784d9c625SLionel Sambuc 	}
19884d9c625SLionel Sambuc 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
19984d9c625SLionel Sambuc 		wincnt = 1;
20084d9c625SLionel Sambuc 
20184d9c625SLionel Sambuc 	/*
20284d9c625SLionel Sambuc 	 * Suppress search for slots unless creating
20384d9c625SLionel Sambuc 	 * file and at end of pathname, in which case
20484d9c625SLionel Sambuc 	 * we watch for a place to put the new file in
20584d9c625SLionel Sambuc 	 * case it doesn't already exist.
20684d9c625SLionel Sambuc 	 */
20784d9c625SLionel Sambuc 	slotcount = wincnt;
20884d9c625SLionel Sambuc 	if ((nameiop == CREATE || nameiop == RENAME) &&
20984d9c625SLionel Sambuc 	    (flags & ISLASTCN))
21084d9c625SLionel Sambuc 		slotcount = 0;
21184d9c625SLionel Sambuc 
21284d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
21384d9c625SLionel Sambuc 	printf("msdosfs_lookup(): dos filename: %s\n", dosfilename);
21484d9c625SLionel Sambuc #endif
21584d9c625SLionel Sambuc 	/*
21684d9c625SLionel Sambuc 	 * Search the directory pointed at by vdp for the name pointed at
21784d9c625SLionel Sambuc 	 * by cnp->cn_nameptr.
21884d9c625SLionel Sambuc 	 */
219*0a6a1f1dSLionel Sambuc 
22084d9c625SLionel Sambuc 	/*
22184d9c625SLionel Sambuc 	 * The outer loop ranges over the clusters that make up the
22284d9c625SLionel Sambuc 	 * directory.  Note that the root directory is different from all
22384d9c625SLionel Sambuc 	 * other directories.  It has a fixed number of blocks that are not
22484d9c625SLionel Sambuc 	 * part of the pool of allocatable clusters.  So, we treat it a
22584d9c625SLionel Sambuc 	 * little differently. The root directory starts at "cluster" 0.
22684d9c625SLionel Sambuc 	 */
22784d9c625SLionel Sambuc 	diroff = 0;
22884d9c625SLionel Sambuc 	for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
22984d9c625SLionel Sambuc 		if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
23084d9c625SLionel Sambuc 			if (error == E2BIG)
23184d9c625SLionel Sambuc 				break;
23284d9c625SLionel Sambuc 			return (error);
23384d9c625SLionel Sambuc 		}
234*0a6a1f1dSLionel Sambuc 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
23584d9c625SLionel Sambuc 		    0, &bp);
23684d9c625SLionel Sambuc 		if (error) {
23784d9c625SLionel Sambuc 			return (error);
23884d9c625SLionel Sambuc 		}
23984d9c625SLionel Sambuc 		for (blkoff = 0; blkoff < blsize;
24084d9c625SLionel Sambuc 		     blkoff += sizeof(struct direntry),
24184d9c625SLionel Sambuc 		     diroff += sizeof(struct direntry)) {
24284d9c625SLionel Sambuc 			dep = (struct direntry *)((char *)bp->b_data + blkoff);
24384d9c625SLionel Sambuc 			/*
24484d9c625SLionel Sambuc 			 * If the slot is empty and we are still looking
24584d9c625SLionel Sambuc 			 * for an empty then remember this one.  If the
24684d9c625SLionel Sambuc 			 * slot is not empty then check to see if it
24784d9c625SLionel Sambuc 			 * matches what we are looking for.  If the slot
24884d9c625SLionel Sambuc 			 * has never been filled with anything, then the
24984d9c625SLionel Sambuc 			 * remainder of the directory has never been used,
25084d9c625SLionel Sambuc 			 * so there is no point in searching it.
25184d9c625SLionel Sambuc 			 */
25284d9c625SLionel Sambuc 			if (dep->deName[0] == SLOT_EMPTY ||
25384d9c625SLionel Sambuc 			    dep->deName[0] == SLOT_DELETED) {
25484d9c625SLionel Sambuc 				/*
25584d9c625SLionel Sambuc 				 * Drop memory of previous long matches
25684d9c625SLionel Sambuc 				 */
25784d9c625SLionel Sambuc 				chksum = -1;
25884d9c625SLionel Sambuc 
25984d9c625SLionel Sambuc 				if (slotcount < wincnt) {
26084d9c625SLionel Sambuc 					slotcount++;
26184d9c625SLionel Sambuc 					slotoffset = diroff;
26284d9c625SLionel Sambuc 				}
26384d9c625SLionel Sambuc 				if (dep->deName[0] == SLOT_EMPTY) {
26484d9c625SLionel Sambuc 					brelse(bp, 0);
26584d9c625SLionel Sambuc 					goto notfound;
26684d9c625SLionel Sambuc 				}
26784d9c625SLionel Sambuc 			} else {
26884d9c625SLionel Sambuc 				/*
26984d9c625SLionel Sambuc 				 * If there wasn't enough space for our
27084d9c625SLionel Sambuc 				 * winentries, forget about the empty space
27184d9c625SLionel Sambuc 				 */
27284d9c625SLionel Sambuc 				if (slotcount < wincnt)
27384d9c625SLionel Sambuc 					slotcount = 0;
27484d9c625SLionel Sambuc 
27584d9c625SLionel Sambuc 				/*
27684d9c625SLionel Sambuc 				 * Check for Win95 long filename entry
27784d9c625SLionel Sambuc 				 */
27884d9c625SLionel Sambuc 				if (dep->deAttributes == ATTR_WIN95) {
27984d9c625SLionel Sambuc 					if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
28084d9c625SLionel Sambuc 						continue;
28184d9c625SLionel Sambuc 
28284d9c625SLionel Sambuc 					chksum = winChkName((const u_char *)cnp->cn_nameptr,
28384d9c625SLionel Sambuc 							    cnp->cn_namelen,
28484d9c625SLionel Sambuc 							    (struct winentry *)dep,
28584d9c625SLionel Sambuc 							    chksum);
28684d9c625SLionel Sambuc 					continue;
28784d9c625SLionel Sambuc 				}
28884d9c625SLionel Sambuc 
28984d9c625SLionel Sambuc 				/*
29084d9c625SLionel Sambuc 				 * Ignore volume labels (anywhere, not just
29184d9c625SLionel Sambuc 				 * the root directory).
29284d9c625SLionel Sambuc 				 */
29384d9c625SLionel Sambuc 				if (dep->deAttributes & ATTR_VOLUME) {
29484d9c625SLionel Sambuc 					chksum = -1;
29584d9c625SLionel Sambuc 					continue;
29684d9c625SLionel Sambuc 				}
29784d9c625SLionel Sambuc 
29884d9c625SLionel Sambuc 				/*
29984d9c625SLionel Sambuc 				 * Check for a checksum or name match
30084d9c625SLionel Sambuc 				 */
30184d9c625SLionel Sambuc 				chksum_ok = (chksum == winChksum(dep->deName));
302*0a6a1f1dSLionel Sambuc 				if (!chksum_ok && (
303*0a6a1f1dSLionel Sambuc 					!olddos ||
304*0a6a1f1dSLionel Sambuc 					memcmp(&dosfilename[0],dep->deName,8) ||
305*0a6a1f1dSLionel Sambuc 					memcmp(&dosfilename[8],dep->deExtension,3))) {
30684d9c625SLionel Sambuc 					chksum = -1;
30784d9c625SLionel Sambuc 					continue;
30884d9c625SLionel Sambuc 				}
30984d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
31084d9c625SLionel Sambuc 				printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
31184d9c625SLionel Sambuc 				    blkoff, diroff);
31284d9c625SLionel Sambuc #endif
31384d9c625SLionel Sambuc 				/*
31484d9c625SLionel Sambuc 				 * Remember where this directory
31584d9c625SLionel Sambuc 				 * entry came from for whoever did
31684d9c625SLionel Sambuc 				 * this lookup.
31784d9c625SLionel Sambuc 				 */
31884d9c625SLionel Sambuc 				dp->de_fndoffset = diroff;
31984d9c625SLionel Sambuc 				if (chksum_ok && nameiop == RENAME) {
32084d9c625SLionel Sambuc 					/*
32184d9c625SLionel Sambuc 					 * Target had correct long name
32284d9c625SLionel Sambuc 					 * directory entries, reuse them
32384d9c625SLionel Sambuc 					 * as needed.
32484d9c625SLionel Sambuc 					 */
32584d9c625SLionel Sambuc 					dp->de_fndcnt = wincnt - 1;
32684d9c625SLionel Sambuc 				} else {
32784d9c625SLionel Sambuc 					/*
32884d9c625SLionel Sambuc 					 * Long name directory entries
32984d9c625SLionel Sambuc 					 * not present or corrupt, can only
33084d9c625SLionel Sambuc 					 * reuse dos directory entry.
33184d9c625SLionel Sambuc 					 */
33284d9c625SLionel Sambuc 					dp->de_fndcnt = 0;
33384d9c625SLionel Sambuc 				}
33484d9c625SLionel Sambuc 
33584d9c625SLionel Sambuc 				goto found;
33684d9c625SLionel Sambuc 			}
33784d9c625SLionel Sambuc 		}	/* for (blkoff = 0; .... */
33884d9c625SLionel Sambuc 		/*
33984d9c625SLionel Sambuc 		 * Release the buffer holding the directory cluster just
34084d9c625SLionel Sambuc 		 * searched.
34184d9c625SLionel Sambuc 		 */
34284d9c625SLionel Sambuc 		brelse(bp, 0);
34384d9c625SLionel Sambuc 	}	/* for (frcn = 0; ; frcn++) */
34484d9c625SLionel Sambuc 
34584d9c625SLionel Sambuc notfound:
34684d9c625SLionel Sambuc 	/*
34784d9c625SLionel Sambuc 	 * We hold no disk buffers at this point.
34884d9c625SLionel Sambuc 	 */
34984d9c625SLionel Sambuc 
35084d9c625SLionel Sambuc 	/*
35184d9c625SLionel Sambuc 	 * If we get here we didn't find the entry we were looking for. But
35284d9c625SLionel Sambuc 	 * that's ok if we are creating or renaming and are at the end of
35384d9c625SLionel Sambuc 	 * the pathname and the directory hasn't been removed.
35484d9c625SLionel Sambuc 	 */
35584d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
35684d9c625SLionel Sambuc 	printf("msdosfs_lookup(): op %d, refcnt %ld, slotcount %d, slotoffset %d\n",
35784d9c625SLionel Sambuc 	    nameiop, dp->de_refcnt, slotcount, slotoffset);
35884d9c625SLionel Sambuc #endif
35984d9c625SLionel Sambuc 	if ((nameiop == CREATE || nameiop == RENAME) &&
36084d9c625SLionel Sambuc 	    (flags & ISLASTCN) && dp->de_refcnt != 0) {
36184d9c625SLionel Sambuc 		/*
36284d9c625SLionel Sambuc 		 * Access for write is interpreted as allowing
36384d9c625SLionel Sambuc 		 * creation of files in the directory.
36484d9c625SLionel Sambuc 		 */
36584d9c625SLionel Sambuc 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
36684d9c625SLionel Sambuc 		if (error)
36784d9c625SLionel Sambuc 			return (error);
36884d9c625SLionel Sambuc 
36984d9c625SLionel Sambuc 		/*
37084d9c625SLionel Sambuc 		 * Fixup the slot description to point to the place where
37184d9c625SLionel Sambuc 		 * we might put the new DOS direntry (putting the Win95
37284d9c625SLionel Sambuc 		 * long name entries before that)
37384d9c625SLionel Sambuc 		 */
37484d9c625SLionel Sambuc 		if (!slotcount) {
37584d9c625SLionel Sambuc 			slotcount = 1;
37684d9c625SLionel Sambuc 			slotoffset = diroff;
37784d9c625SLionel Sambuc 		}
37884d9c625SLionel Sambuc 		if (wincnt > slotcount) {
37984d9c625SLionel Sambuc 			slotoffset +=
38084d9c625SLionel Sambuc 				sizeof(struct direntry) * (wincnt - slotcount);
38184d9c625SLionel Sambuc 		}
38284d9c625SLionel Sambuc 
38384d9c625SLionel Sambuc 		/*
38484d9c625SLionel Sambuc 		 * Return an indication of where the new directory
38584d9c625SLionel Sambuc 		 * entry should be put.
38684d9c625SLionel Sambuc 		 */
38784d9c625SLionel Sambuc 		dp->de_fndoffset = slotoffset;
38884d9c625SLionel Sambuc 		dp->de_fndcnt = wincnt - 1;
38984d9c625SLionel Sambuc 
39084d9c625SLionel Sambuc 		/*
39184d9c625SLionel Sambuc 		 * We return with the directory locked, so that
39284d9c625SLionel Sambuc 		 * the parameters we set up above will still be
39384d9c625SLionel Sambuc 		 * valid if we actually decide to do a direnter().
39484d9c625SLionel Sambuc 		 * We return ni_vp == NULL to indicate that the entry
39584d9c625SLionel Sambuc 		 * does not currently exist; we leave a pointer to
39684d9c625SLionel Sambuc 		 * the (locked) directory inode in ndp->ni_dvp.
39784d9c625SLionel Sambuc 		 *
39884d9c625SLionel Sambuc 		 * NB - if the directory is unlocked, then this
39984d9c625SLionel Sambuc 		 * information cannot be used.
40084d9c625SLionel Sambuc 		 */
40184d9c625SLionel Sambuc 		return (EJUSTRETURN);
40284d9c625SLionel Sambuc 	}
40384d9c625SLionel Sambuc 
40484d9c625SLionel Sambuc #if 0
40584d9c625SLionel Sambuc 	/*
40684d9c625SLionel Sambuc 	 * Insert name into cache (as non-existent) if appropriate.
40784d9c625SLionel Sambuc 	 *
40884d9c625SLionel Sambuc 	 * XXX Negative caching is broken for msdosfs because the name
40984d9c625SLionel Sambuc 	 * cache doesn't understand peculiarities such as case insensitivity
41084d9c625SLionel Sambuc 	 * and 8.3 filenames.  Hence, it may not invalidate all negative
41184d9c625SLionel Sambuc 	 * entries if a file with this name is later created.
41284d9c625SLionel Sambuc 	 * e.g. creating a file 'foo' won't invalidate a negative entry
41384d9c625SLionel Sambuc 	 * for 'FOO'.
41484d9c625SLionel Sambuc 	 */
41584d9c625SLionel Sambuc 	if (nameiop != CREATE)
41684d9c625SLionel Sambuc 		cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
41784d9c625SLionel Sambuc 			    cnp->cn_flags);
41884d9c625SLionel Sambuc #endif
41984d9c625SLionel Sambuc 
42084d9c625SLionel Sambuc 	return (ENOENT);
42184d9c625SLionel Sambuc 
42284d9c625SLionel Sambuc found:
42384d9c625SLionel Sambuc 	/*
42484d9c625SLionel Sambuc 	 * NOTE:  We still have the buffer with matched directory entry at
42584d9c625SLionel Sambuc 	 * this point.
42684d9c625SLionel Sambuc 	 */
42784d9c625SLionel Sambuc 	isadir = dep->deAttributes & ATTR_DIRECTORY;
42884d9c625SLionel Sambuc 	scn = getushort(dep->deStartCluster);
42984d9c625SLionel Sambuc 	if (FAT32(pmp)) {
43084d9c625SLionel Sambuc 		scn |= getushort(dep->deHighClust) << 16;
43184d9c625SLionel Sambuc 		if (scn == pmp->pm_rootdirblk) {
43284d9c625SLionel Sambuc 			/*
43384d9c625SLionel Sambuc 			 * There should actually be 0 here.
43484d9c625SLionel Sambuc 			 * Just ignore the error.
43584d9c625SLionel Sambuc 			 */
43684d9c625SLionel Sambuc 			scn = MSDOSFSROOT;
43784d9c625SLionel Sambuc 		}
43884d9c625SLionel Sambuc 	}
43984d9c625SLionel Sambuc 
44084d9c625SLionel Sambuc 	if (isadir) {
44184d9c625SLionel Sambuc 		cluster = scn;
44284d9c625SLionel Sambuc 		if (cluster == MSDOSFSROOT)
44384d9c625SLionel Sambuc 			blkoff = MSDOSFSROOT_OFS;
44484d9c625SLionel Sambuc 		else
44584d9c625SLionel Sambuc 			blkoff = 0;
44684d9c625SLionel Sambuc 	} else if (cluster == MSDOSFSROOT)
44784d9c625SLionel Sambuc 		blkoff = diroff;
44884d9c625SLionel Sambuc 
44984d9c625SLionel Sambuc 	/*
45084d9c625SLionel Sambuc 	 * Now release buf to allow deget to read the entry again.
45184d9c625SLionel Sambuc 	 * Reserving it here and giving it to deget could result
45284d9c625SLionel Sambuc 	 * in a deadlock.
45384d9c625SLionel Sambuc 	 */
45484d9c625SLionel Sambuc 	brelse(bp, 0);
45584d9c625SLionel Sambuc 
45684d9c625SLionel Sambuc foundroot:
45784d9c625SLionel Sambuc 	/*
45884d9c625SLionel Sambuc 	 * If we entered at foundroot, then we are looking for the . or ..
45984d9c625SLionel Sambuc 	 * entry of the filesystems root directory.  isadir and scn were
46084d9c625SLionel Sambuc 	 * setup before jumping here.  And, bp is already null.
46184d9c625SLionel Sambuc 	 */
46284d9c625SLionel Sambuc 	if (FAT32(pmp) && scn == MSDOSFSROOT)
46384d9c625SLionel Sambuc 		scn = pmp->pm_rootdirblk;
46484d9c625SLionel Sambuc 
46584d9c625SLionel Sambuc 	/*
46684d9c625SLionel Sambuc 	 * If deleting, and at end of pathname, return
46784d9c625SLionel Sambuc 	 * parameters which can be used to remove file.
46884d9c625SLionel Sambuc 	 * Lock the inode, being careful with ".".
46984d9c625SLionel Sambuc 	 */
47084d9c625SLionel Sambuc 	if (nameiop == DELETE && (flags & ISLASTCN)) {
47184d9c625SLionel Sambuc 		/*
47284d9c625SLionel Sambuc 		 * Don't allow deleting the root.
47384d9c625SLionel Sambuc 		 */
47484d9c625SLionel Sambuc 		if (blkoff == MSDOSFSROOT_OFS)
47584d9c625SLionel Sambuc 			return EINVAL;
47684d9c625SLionel Sambuc 
47784d9c625SLionel Sambuc 		/*
47884d9c625SLionel Sambuc 		 * Write access to directory required to delete files.
47984d9c625SLionel Sambuc 		 */
48084d9c625SLionel Sambuc 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
48184d9c625SLionel Sambuc 		if (error)
48284d9c625SLionel Sambuc 			return (error);
48384d9c625SLionel Sambuc 
48484d9c625SLionel Sambuc 		/*
48584d9c625SLionel Sambuc 		 * Return pointer to current entry in dp->i_offset.
48684d9c625SLionel Sambuc 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
48784d9c625SLionel Sambuc 		 */
48884d9c625SLionel Sambuc 		if (dp->de_StartCluster == scn && isadir) {	/* "." */
48984d9c625SLionel Sambuc 			vref(vdp);
49084d9c625SLionel Sambuc 			*vpp = vdp;
49184d9c625SLionel Sambuc 			return (0);
49284d9c625SLionel Sambuc 		}
493*0a6a1f1dSLionel Sambuc 		error = deget(pmp, cluster, blkoff, vpp);
494*0a6a1f1dSLionel Sambuc 		return error;
49584d9c625SLionel Sambuc 	}
49684d9c625SLionel Sambuc 
49784d9c625SLionel Sambuc 	/*
49884d9c625SLionel Sambuc 	 * If rewriting (RENAME), return the inode and the
49984d9c625SLionel Sambuc 	 * information required to rewrite the present directory
50084d9c625SLionel Sambuc 	 * Must get inode of directory entry to verify it's a
50184d9c625SLionel Sambuc 	 * regular file, or empty directory.
50284d9c625SLionel Sambuc 	 */
50384d9c625SLionel Sambuc 	if (nameiop == RENAME && (flags & ISLASTCN)) {
50484d9c625SLionel Sambuc 
50584d9c625SLionel Sambuc 		if (vdp->v_mount->mnt_flag & MNT_RDONLY)
50684d9c625SLionel Sambuc 			return (EROFS);
50784d9c625SLionel Sambuc 
50884d9c625SLionel Sambuc 		if (blkoff == MSDOSFSROOT_OFS)
50984d9c625SLionel Sambuc 			return EINVAL;
51084d9c625SLionel Sambuc 
51184d9c625SLionel Sambuc 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
51284d9c625SLionel Sambuc 		if (error)
51384d9c625SLionel Sambuc 			return (error);
51484d9c625SLionel Sambuc 
51584d9c625SLionel Sambuc 		/*
51684d9c625SLionel Sambuc 		 * Careful about locking second inode.
51784d9c625SLionel Sambuc 		 * This can only occur if the target is ".".
51884d9c625SLionel Sambuc 		 */
51984d9c625SLionel Sambuc 		if (dp->de_StartCluster == scn && isadir)
52084d9c625SLionel Sambuc 			return (EISDIR);
52184d9c625SLionel Sambuc 
522*0a6a1f1dSLionel Sambuc 		error = deget(pmp, cluster, blkoff, vpp);
52384d9c625SLionel Sambuc 		return error;
52484d9c625SLionel Sambuc 	}
525*0a6a1f1dSLionel Sambuc 
526*0a6a1f1dSLionel Sambuc 	if (dp->de_StartCluster == scn && isadir) {
52784d9c625SLionel Sambuc 		vref(vdp);	/* we want ourself, ie "." */
52884d9c625SLionel Sambuc 		*vpp = vdp;
529*0a6a1f1dSLionel Sambuc 	} else if ((error = deget(pmp, cluster, blkoff, vpp)) != 0) {
530*0a6a1f1dSLionel Sambuc 		return error;
53184d9c625SLionel Sambuc 	}
53284d9c625SLionel Sambuc 
53384d9c625SLionel Sambuc 	/*
53484d9c625SLionel Sambuc 	 * Insert name into cache if appropriate.
53584d9c625SLionel Sambuc 	 */
53684d9c625SLionel Sambuc 	cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
53784d9c625SLionel Sambuc 
53884d9c625SLionel Sambuc 	return 0;
53984d9c625SLionel Sambuc }
54084d9c625SLionel Sambuc #endif /* _KERNEL */
54184d9c625SLionel Sambuc 
54284d9c625SLionel Sambuc /*
54384d9c625SLionel Sambuc  * dep  - directory entry to copy into the directory
54484d9c625SLionel Sambuc  * ddep - directory to add to
54584d9c625SLionel Sambuc  * depp - return the address of the denode for the created directory entry
54684d9c625SLionel Sambuc  *	  if depp != 0
54784d9c625SLionel Sambuc  * cnp  - componentname needed for Win95 long filenames
54884d9c625SLionel Sambuc  */
54984d9c625SLionel Sambuc int
createde(struct denode * dep,struct denode * ddep,struct denode ** depp,struct componentname * cnp)55084d9c625SLionel Sambuc createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp)
55184d9c625SLionel Sambuc {
55284d9c625SLionel Sambuc 	int error, rberror;
55384d9c625SLionel Sambuc 	u_long dirclust, clusoffset;
55484d9c625SLionel Sambuc 	u_long fndoffset, havecnt = 0, wcnt = 1, i;
55584d9c625SLionel Sambuc 	struct direntry *ndep;
55684d9c625SLionel Sambuc 	struct msdosfsmount *pmp = ddep->de_pmp;
55784d9c625SLionel Sambuc 	struct buf *bp;
55884d9c625SLionel Sambuc 	daddr_t bn;
55984d9c625SLionel Sambuc 	int blsize;
56084d9c625SLionel Sambuc #ifdef _KERNEL
56184d9c625SLionel Sambuc 	int async = ddep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
56284d9c625SLionel Sambuc #else
56384d9c625SLionel Sambuc #define async 0
56484d9c625SLionel Sambuc #endif
56584d9c625SLionel Sambuc 
56684d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
56784d9c625SLionel Sambuc 	printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
56884d9c625SLionel Sambuc 	    dep, ddep, depp, cnp);
56984d9c625SLionel Sambuc #endif
57084d9c625SLionel Sambuc 
57184d9c625SLionel Sambuc 	/*
57284d9c625SLionel Sambuc 	 * If no space left in the directory then allocate another cluster
57384d9c625SLionel Sambuc 	 * and chain it onto the end of the file.  There is one exception
57484d9c625SLionel Sambuc 	 * to this.  That is, if the root directory has no more space it
57584d9c625SLionel Sambuc 	 * can NOT be expanded.  extendfile() checks for and fails attempts
57684d9c625SLionel Sambuc 	 * to extend the root directory.  We just return an error in that
57784d9c625SLionel Sambuc 	 * case.
57884d9c625SLionel Sambuc 	 */
57984d9c625SLionel Sambuc 	if (ddep->de_fndoffset >= ddep->de_FileSize) {
58084d9c625SLionel Sambuc 		u_long needlen = ddep->de_fndoffset + sizeof(struct direntry)
58184d9c625SLionel Sambuc 		    - ddep->de_FileSize;
58284d9c625SLionel Sambuc 		dirclust = de_clcount(pmp, needlen);
58384d9c625SLionel Sambuc 		if ((error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR)) != 0) {
58484d9c625SLionel Sambuc 			(void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
58584d9c625SLionel Sambuc 			goto err_norollback;
58684d9c625SLionel Sambuc 		}
58784d9c625SLionel Sambuc 
58884d9c625SLionel Sambuc 		/*
58984d9c625SLionel Sambuc 		 * Update the size of the directory
59084d9c625SLionel Sambuc 		 */
59184d9c625SLionel Sambuc 		ddep->de_FileSize += de_cn2off(pmp, dirclust);
59284d9c625SLionel Sambuc 	}
59384d9c625SLionel Sambuc 
59484d9c625SLionel Sambuc 	/*
59584d9c625SLionel Sambuc 	 * We just read in the cluster with space.  Copy the new directory
59684d9c625SLionel Sambuc 	 * entry in.  Then write it to disk. NOTE:  DOS directories
59784d9c625SLionel Sambuc 	 * do not get smaller as clusters are emptied.
59884d9c625SLionel Sambuc 	 */
59984d9c625SLionel Sambuc 	error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
60084d9c625SLionel Sambuc 		       &bn, &dirclust, &blsize);
60184d9c625SLionel Sambuc 	if (error)
60284d9c625SLionel Sambuc 		goto err_norollback;
60384d9c625SLionel Sambuc 	clusoffset = ddep->de_fndoffset;
60484d9c625SLionel Sambuc 	if (dirclust != MSDOSFSROOT)
60584d9c625SLionel Sambuc 		clusoffset &= pmp->pm_crbomask;
606*0a6a1f1dSLionel Sambuc 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
60784d9c625SLionel Sambuc 	    B_MODIFY, &bp)) != 0) {
60884d9c625SLionel Sambuc 		goto err_norollback;
60984d9c625SLionel Sambuc 	}
61084d9c625SLionel Sambuc 	ndep = bptoep(pmp, bp, clusoffset);
61184d9c625SLionel Sambuc 
61284d9c625SLionel Sambuc 	DE_EXTERNALIZE(ndep, dep);
61384d9c625SLionel Sambuc 
61484d9c625SLionel Sambuc 	/*
61584d9c625SLionel Sambuc 	 * Now write the Win95 long name
61684d9c625SLionel Sambuc 	 */
61784d9c625SLionel Sambuc 	if (ddep->de_fndcnt > 0) {
61884d9c625SLionel Sambuc 		u_int8_t chksum = winChksum(ndep->deName);
61984d9c625SLionel Sambuc 		const u_char *un = (const u_char *)cnp->cn_nameptr;
62084d9c625SLionel Sambuc 		int unlen = cnp->cn_namelen;
62184d9c625SLionel Sambuc 		u_long xhavecnt;
62284d9c625SLionel Sambuc 
62384d9c625SLionel Sambuc 		fndoffset = ddep->de_fndoffset;
62484d9c625SLionel Sambuc 		xhavecnt = ddep->de_fndcnt + 1;
62584d9c625SLionel Sambuc 
62684d9c625SLionel Sambuc 		for(; wcnt < xhavecnt; wcnt++) {
62784d9c625SLionel Sambuc 			if ((fndoffset & pmp->pm_crbomask) == 0) {
62884d9c625SLionel Sambuc 				/* we should never get here if ddep is root
62984d9c625SLionel Sambuc 				 * directory */
63084d9c625SLionel Sambuc 
63184d9c625SLionel Sambuc 				if (async)
63284d9c625SLionel Sambuc 					(void) bdwrite(bp);
63384d9c625SLionel Sambuc 				else if ((error = bwrite(bp)) != 0)
63484d9c625SLionel Sambuc 					goto rollback;
63584d9c625SLionel Sambuc 
63684d9c625SLionel Sambuc 				fndoffset -= sizeof(struct direntry);
63784d9c625SLionel Sambuc 				error = pcbmap(ddep,
63884d9c625SLionel Sambuc 					       de_cluster(pmp, fndoffset),
63984d9c625SLionel Sambuc 					       &bn, 0, &blsize);
64084d9c625SLionel Sambuc 				if (error)
64184d9c625SLionel Sambuc 					goto rollback;
64284d9c625SLionel Sambuc 
64384d9c625SLionel Sambuc 				error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
644*0a6a1f1dSLionel Sambuc 				    blsize, B_MODIFY, &bp);
64584d9c625SLionel Sambuc 				if (error) {
64684d9c625SLionel Sambuc 					goto rollback;
64784d9c625SLionel Sambuc 				}
64884d9c625SLionel Sambuc 				ndep = bptoep(pmp, bp,
64984d9c625SLionel Sambuc 						fndoffset & pmp->pm_crbomask);
65084d9c625SLionel Sambuc 			} else {
65184d9c625SLionel Sambuc 				ndep--;
65284d9c625SLionel Sambuc 				fndoffset -= sizeof(struct direntry);
65384d9c625SLionel Sambuc 			}
65484d9c625SLionel Sambuc 			if (!unix2winfn(un, unlen, (struct winentry *)ndep,
65584d9c625SLionel Sambuc 						wcnt, chksum))
65684d9c625SLionel Sambuc 				break;
65784d9c625SLionel Sambuc 		}
65884d9c625SLionel Sambuc 	}
65984d9c625SLionel Sambuc 
66084d9c625SLionel Sambuc 	if (async)
66184d9c625SLionel Sambuc 		bdwrite(bp);
66284d9c625SLionel Sambuc 	else if ((error = bwrite(bp)) != 0)
66384d9c625SLionel Sambuc 		goto rollback;
66484d9c625SLionel Sambuc 
66584d9c625SLionel Sambuc 	/*
66684d9c625SLionel Sambuc 	 * If they want us to return with the denode gotten.
66784d9c625SLionel Sambuc 	 */
66884d9c625SLionel Sambuc 	if (depp) {
66984d9c625SLionel Sambuc 		u_long diroffset = clusoffset;
670*0a6a1f1dSLionel Sambuc 
67184d9c625SLionel Sambuc 		if (dep->de_Attributes & ATTR_DIRECTORY) {
67284d9c625SLionel Sambuc 			dirclust = dep->de_StartCluster;
67384d9c625SLionel Sambuc 			if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
67484d9c625SLionel Sambuc 				dirclust = MSDOSFSROOT;
67584d9c625SLionel Sambuc 			if (dirclust == MSDOSFSROOT)
67684d9c625SLionel Sambuc 				diroffset = MSDOSFSROOT_OFS;
67784d9c625SLionel Sambuc 			else
67884d9c625SLionel Sambuc 				diroffset = 0;
67984d9c625SLionel Sambuc 		}
680*0a6a1f1dSLionel Sambuc #ifdef MAKEFS
681*0a6a1f1dSLionel Sambuc 		error = deget(pmp, dirclust, diroffset, depp);
682*0a6a1f1dSLionel Sambuc #else
683*0a6a1f1dSLionel Sambuc 		struct vnode *vp;
684*0a6a1f1dSLionel Sambuc 
685*0a6a1f1dSLionel Sambuc 		error = deget(pmp, dirclust, diroffset, &vp);
686*0a6a1f1dSLionel Sambuc 		if (error == 0)
687*0a6a1f1dSLionel Sambuc 			*depp = VTODE(vp);
688*0a6a1f1dSLionel Sambuc 		else
689*0a6a1f1dSLionel Sambuc 			*depp = NULL;
690*0a6a1f1dSLionel Sambuc #endif
691*0a6a1f1dSLionel Sambuc 		return error;
69284d9c625SLionel Sambuc 	}
69384d9c625SLionel Sambuc 
69484d9c625SLionel Sambuc 	return 0;
69584d9c625SLionel Sambuc 
69684d9c625SLionel Sambuc     rollback:
69784d9c625SLionel Sambuc 	/*
69884d9c625SLionel Sambuc 	 * Mark all slots modified so far as deleted. Note that we
69984d9c625SLionel Sambuc 	 * can't just call removede(), since directory is not in
70084d9c625SLionel Sambuc 	 * consistent state.
70184d9c625SLionel Sambuc 	 */
70284d9c625SLionel Sambuc 	fndoffset = ddep->de_fndoffset;
70384d9c625SLionel Sambuc 	rberror = pcbmap(ddep, de_cluster(pmp, fndoffset),
70484d9c625SLionel Sambuc 	       &bn, NULL, &blsize);
70584d9c625SLionel Sambuc 	if (rberror)
70684d9c625SLionel Sambuc 		goto err_norollback;
707*0a6a1f1dSLionel Sambuc 	if ((rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
70884d9c625SLionel Sambuc 	    B_MODIFY, &bp)) != 0) {
70984d9c625SLionel Sambuc 		goto err_norollback;
71084d9c625SLionel Sambuc 	}
71184d9c625SLionel Sambuc 	ndep = bptoep(pmp, bp, clusoffset);
71284d9c625SLionel Sambuc 
71384d9c625SLionel Sambuc 	havecnt = ddep->de_fndcnt + 1;
71484d9c625SLionel Sambuc 	for(i = wcnt; i <= havecnt; i++) {
71584d9c625SLionel Sambuc 		/* mark entry as deleted */
71684d9c625SLionel Sambuc 		ndep->deName[0] = SLOT_DELETED;
71784d9c625SLionel Sambuc 
71884d9c625SLionel Sambuc 		if ((fndoffset & pmp->pm_crbomask) == 0) {
71984d9c625SLionel Sambuc 			/* we should never get here if ddep is root
72084d9c625SLionel Sambuc 			 * directory */
72184d9c625SLionel Sambuc 
72284d9c625SLionel Sambuc 			if (async)
72384d9c625SLionel Sambuc 				bdwrite(bp);
72484d9c625SLionel Sambuc 			else if ((rberror = bwrite(bp)) != 0)
72584d9c625SLionel Sambuc 				goto err_norollback;
72684d9c625SLionel Sambuc 
72784d9c625SLionel Sambuc 			fndoffset -= sizeof(struct direntry);
72884d9c625SLionel Sambuc 			rberror = pcbmap(ddep,
72984d9c625SLionel Sambuc 				       de_cluster(pmp, fndoffset),
73084d9c625SLionel Sambuc 				       &bn, 0, &blsize);
73184d9c625SLionel Sambuc 			if (rberror)
73284d9c625SLionel Sambuc 				goto err_norollback;
73384d9c625SLionel Sambuc 
73484d9c625SLionel Sambuc 			rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
735*0a6a1f1dSLionel Sambuc 			    blsize, B_MODIFY, &bp);
73684d9c625SLionel Sambuc 			if (rberror) {
73784d9c625SLionel Sambuc 				goto err_norollback;
73884d9c625SLionel Sambuc 			}
73984d9c625SLionel Sambuc 			ndep = bptoep(pmp, bp, fndoffset);
74084d9c625SLionel Sambuc 		} else {
74184d9c625SLionel Sambuc 			ndep--;
74284d9c625SLionel Sambuc 			fndoffset -= sizeof(struct direntry);
74384d9c625SLionel Sambuc 		}
74484d9c625SLionel Sambuc 	}
74584d9c625SLionel Sambuc 
74684d9c625SLionel Sambuc 	/* ignore any further error */
74784d9c625SLionel Sambuc 	if (async)
74884d9c625SLionel Sambuc 		(void) bdwrite(bp);
74984d9c625SLionel Sambuc 	else
75084d9c625SLionel Sambuc 		(void) bwrite(bp);
75184d9c625SLionel Sambuc 
75284d9c625SLionel Sambuc     err_norollback:
75384d9c625SLionel Sambuc 	return error;
75484d9c625SLionel Sambuc }
75584d9c625SLionel Sambuc 
75684d9c625SLionel Sambuc /*
75784d9c625SLionel Sambuc  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
75884d9c625SLionel Sambuc  * return 0 if not empty or error.
75984d9c625SLionel Sambuc  */
76084d9c625SLionel Sambuc int
dosdirempty(struct denode * dep)76184d9c625SLionel Sambuc dosdirempty(struct denode *dep)
76284d9c625SLionel Sambuc {
76384d9c625SLionel Sambuc 	int blsize;
76484d9c625SLionel Sambuc 	int error;
76584d9c625SLionel Sambuc 	u_long cn;
76684d9c625SLionel Sambuc 	daddr_t bn;
76784d9c625SLionel Sambuc 	struct buf *bp;
76884d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
76984d9c625SLionel Sambuc 	struct direntry *dentp;
77084d9c625SLionel Sambuc 
77184d9c625SLionel Sambuc 	/*
77284d9c625SLionel Sambuc 	 * Since the filesize field in directory entries for a directory is
77384d9c625SLionel Sambuc 	 * zero, we just have to feel our way through the directory until
77484d9c625SLionel Sambuc 	 * we hit end of file.
77584d9c625SLionel Sambuc 	 */
77684d9c625SLionel Sambuc 	for (cn = 0;; cn++) {
77784d9c625SLionel Sambuc 		if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
77884d9c625SLionel Sambuc 			if (error == E2BIG)
77984d9c625SLionel Sambuc 				return (1);	/* it's empty */
78084d9c625SLionel Sambuc 			return (0);
78184d9c625SLionel Sambuc 		}
782*0a6a1f1dSLionel Sambuc 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
78384d9c625SLionel Sambuc 		    0, &bp);
78484d9c625SLionel Sambuc 		if (error) {
78584d9c625SLionel Sambuc 			return (0);
78684d9c625SLionel Sambuc 		}
78784d9c625SLionel Sambuc 		for (dentp = (struct direntry *)bp->b_data;
78884d9c625SLionel Sambuc 		     (char *)dentp < (char *)bp->b_data + blsize;
78984d9c625SLionel Sambuc 		     dentp++) {
79084d9c625SLionel Sambuc 			if (dentp->deName[0] != SLOT_DELETED &&
79184d9c625SLionel Sambuc 			    (dentp->deAttributes & ATTR_VOLUME) == 0) {
79284d9c625SLionel Sambuc 				/*
79384d9c625SLionel Sambuc 				 * In dos directories an entry whose name
79484d9c625SLionel Sambuc 				 * starts with SLOT_EMPTY (0) starts the
79584d9c625SLionel Sambuc 				 * beginning of the unused part of the
79684d9c625SLionel Sambuc 				 * directory, so we can just return that it
79784d9c625SLionel Sambuc 				 * is empty.
79884d9c625SLionel Sambuc 				 */
79984d9c625SLionel Sambuc 				if (dentp->deName[0] == SLOT_EMPTY) {
80084d9c625SLionel Sambuc 					brelse(bp, 0);
80184d9c625SLionel Sambuc 					return (1);
80284d9c625SLionel Sambuc 				}
80384d9c625SLionel Sambuc 				/*
80484d9c625SLionel Sambuc 				 * Any names other than "." and ".." in a
80584d9c625SLionel Sambuc 				 * directory mean it is not empty.
80684d9c625SLionel Sambuc 				 */
80784d9c625SLionel Sambuc 				if (memcmp(dentp->deName, ".          ", 11) &&
80884d9c625SLionel Sambuc 				    memcmp(dentp->deName, "..         ", 11)) {
80984d9c625SLionel Sambuc 					brelse(bp, 0);
81084d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
81184d9c625SLionel Sambuc 					printf("dosdirempty(): found %.11s, %d, %d\n",
81284d9c625SLionel Sambuc 					    dentp->deName, dentp->deName[0],
81384d9c625SLionel Sambuc 						dentp->deName[1]);
81484d9c625SLionel Sambuc #endif
81584d9c625SLionel Sambuc 					return (0);	/* not empty */
81684d9c625SLionel Sambuc 				}
81784d9c625SLionel Sambuc 			}
81884d9c625SLionel Sambuc 		}
81984d9c625SLionel Sambuc 		brelse(bp, 0);
82084d9c625SLionel Sambuc 	}
82184d9c625SLionel Sambuc 	/* NOTREACHED */
82284d9c625SLionel Sambuc }
82384d9c625SLionel Sambuc 
82484d9c625SLionel Sambuc /*
82584d9c625SLionel Sambuc  * Check to see if the directory described by target is in some
82684d9c625SLionel Sambuc  * subdirectory of source.  This prevents something like the following from
82784d9c625SLionel Sambuc  * succeeding and leaving a bunch or files and directories orphaned. mv
82884d9c625SLionel Sambuc  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
82984d9c625SLionel Sambuc  *
83084d9c625SLionel Sambuc  * source - the inode for /a/b/c
83184d9c625SLionel Sambuc  * target - the inode for /a/b/c/d/e/f
83284d9c625SLionel Sambuc  *
83384d9c625SLionel Sambuc  * Returns 0 if target is NOT a subdirectory of source.
83484d9c625SLionel Sambuc  * Otherwise returns a non-zero error number.
83584d9c625SLionel Sambuc  * The target inode is always unlocked on return.
83684d9c625SLionel Sambuc  */
83784d9c625SLionel Sambuc int
doscheckpath(struct denode * source,struct denode * target)83884d9c625SLionel Sambuc doscheckpath(struct denode *source, struct denode *target)
83984d9c625SLionel Sambuc {
84084d9c625SLionel Sambuc 	u_long scn;
84184d9c625SLionel Sambuc 	struct msdosfsmount *pmp;
84284d9c625SLionel Sambuc 	struct direntry *ep;
84384d9c625SLionel Sambuc 	struct denode *dep;
84484d9c625SLionel Sambuc 	struct buf *bp = NULL;
84584d9c625SLionel Sambuc 	int error = 0;
84684d9c625SLionel Sambuc 
84784d9c625SLionel Sambuc 	dep = target;
84884d9c625SLionel Sambuc 	if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
84984d9c625SLionel Sambuc 	    (source->de_Attributes & ATTR_DIRECTORY) == 0) {
85084d9c625SLionel Sambuc 		error = ENOTDIR;
85184d9c625SLionel Sambuc 		goto out;
85284d9c625SLionel Sambuc 	}
85384d9c625SLionel Sambuc 	if (dep->de_StartCluster == source->de_StartCluster) {
85484d9c625SLionel Sambuc 		error = EEXIST;
85584d9c625SLionel Sambuc 		goto out;
85684d9c625SLionel Sambuc 	}
85784d9c625SLionel Sambuc 	if (dep->de_StartCluster == MSDOSFSROOT)
85884d9c625SLionel Sambuc 		goto out;
85984d9c625SLionel Sambuc 	pmp = dep->de_pmp;
86084d9c625SLionel Sambuc #ifdef	DIAGNOSTIC
86184d9c625SLionel Sambuc 	if (pmp != source->de_pmp)
86284d9c625SLionel Sambuc 		panic("doscheckpath: source and target on different filesystems");
86384d9c625SLionel Sambuc #endif
86484d9c625SLionel Sambuc 	if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
86584d9c625SLionel Sambuc 		goto out;
86684d9c625SLionel Sambuc 
86784d9c625SLionel Sambuc 	for (;;) {
86884d9c625SLionel Sambuc 		if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
86984d9c625SLionel Sambuc 			error = ENOTDIR;
87084d9c625SLionel Sambuc 			break;
87184d9c625SLionel Sambuc 		}
87284d9c625SLionel Sambuc 		scn = dep->de_StartCluster;
87384d9c625SLionel Sambuc 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, cntobn(pmp, scn)),
874*0a6a1f1dSLionel Sambuc 			      pmp->pm_bpcluster, 0, &bp);
87584d9c625SLionel Sambuc 		if (error)
87684d9c625SLionel Sambuc 			break;
87784d9c625SLionel Sambuc 
87884d9c625SLionel Sambuc 		ep = (struct direntry *) bp->b_data + 1;
87984d9c625SLionel Sambuc 		if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
88084d9c625SLionel Sambuc 		    memcmp(ep->deName, "..         ", 11) != 0) {
88184d9c625SLionel Sambuc 			error = ENOTDIR;
88284d9c625SLionel Sambuc 			break;
88384d9c625SLionel Sambuc 		}
88484d9c625SLionel Sambuc 		scn = getushort(ep->deStartCluster);
88584d9c625SLionel Sambuc 		if (FAT32(pmp))
88684d9c625SLionel Sambuc 			scn |= getushort(ep->deHighClust) << 16;
88784d9c625SLionel Sambuc 
88884d9c625SLionel Sambuc 		if (scn == source->de_StartCluster) {
88984d9c625SLionel Sambuc 			error = EINVAL;
89084d9c625SLionel Sambuc 			break;
89184d9c625SLionel Sambuc 		}
89284d9c625SLionel Sambuc 		if (scn == MSDOSFSROOT)
89384d9c625SLionel Sambuc 			break;
89484d9c625SLionel Sambuc 		if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
89584d9c625SLionel Sambuc 			/*
89684d9c625SLionel Sambuc 			 * scn should be 0 in this case,
89784d9c625SLionel Sambuc 			 * but we silently ignore the error.
89884d9c625SLionel Sambuc 			 */
89984d9c625SLionel Sambuc 			break;
90084d9c625SLionel Sambuc 		}
90184d9c625SLionel Sambuc 
90284d9c625SLionel Sambuc 		vput(DETOV(dep));
90384d9c625SLionel Sambuc 		brelse(bp, 0);
90484d9c625SLionel Sambuc 		bp = NULL;
905*0a6a1f1dSLionel Sambuc #ifdef MAKEFS
90684d9c625SLionel Sambuc 		/* NOTE: deget() clears dep on error */
90784d9c625SLionel Sambuc 		if ((error = deget(pmp, scn, 0, &dep)) != 0)
90884d9c625SLionel Sambuc 			break;
909*0a6a1f1dSLionel Sambuc #else
910*0a6a1f1dSLionel Sambuc 		struct vnode *vp;
911*0a6a1f1dSLionel Sambuc 
912*0a6a1f1dSLionel Sambuc 		dep = NULL;
913*0a6a1f1dSLionel Sambuc 		error = deget(pmp, scn, 0, &vp);
914*0a6a1f1dSLionel Sambuc 		if (error)
915*0a6a1f1dSLionel Sambuc 			break;
916*0a6a1f1dSLionel Sambuc 		error = vn_lock(vp, LK_EXCLUSIVE);
917*0a6a1f1dSLionel Sambuc 		if (error) {
918*0a6a1f1dSLionel Sambuc 			vrele(vp);
919*0a6a1f1dSLionel Sambuc 			break;
920*0a6a1f1dSLionel Sambuc 		}
921*0a6a1f1dSLionel Sambuc 		dep = VTODE(vp);
922*0a6a1f1dSLionel Sambuc #endif
92384d9c625SLionel Sambuc 	}
92484d9c625SLionel Sambuc out:
92584d9c625SLionel Sambuc 	if (bp)
92684d9c625SLionel Sambuc 		brelse(bp, 0);
92784d9c625SLionel Sambuc 	if (error == ENOTDIR)
92884d9c625SLionel Sambuc 		printf("doscheckpath(): .. not a directory?\n");
92984d9c625SLionel Sambuc 	if (dep != NULL)
93084d9c625SLionel Sambuc 		vput(DETOV(dep));
93184d9c625SLionel Sambuc 	return (error);
93284d9c625SLionel Sambuc }
93384d9c625SLionel Sambuc 
93484d9c625SLionel Sambuc /*
93584d9c625SLionel Sambuc  * Read in the disk block containing the directory entry (dirclu, dirofs)
93684d9c625SLionel Sambuc  * and return the address of the buf header, and the address of the
93784d9c625SLionel Sambuc  * directory entry within the block.
93884d9c625SLionel Sambuc  */
93984d9c625SLionel Sambuc int
readep(struct msdosfsmount * pmp,u_long dirclust,u_long diroffset,struct buf ** bpp,struct direntry ** epp)94084d9c625SLionel Sambuc readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct buf **bpp, struct direntry **epp)
94184d9c625SLionel Sambuc {
94284d9c625SLionel Sambuc 	int error;
94384d9c625SLionel Sambuc 	daddr_t bn;
94484d9c625SLionel Sambuc 	int blsize;
94584d9c625SLionel Sambuc 
94684d9c625SLionel Sambuc 	blsize = pmp->pm_bpcluster;
94784d9c625SLionel Sambuc 	if (dirclust == MSDOSFSROOT
94884d9c625SLionel Sambuc 	    && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
94984d9c625SLionel Sambuc 		blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
95084d9c625SLionel Sambuc 	bn = detobn(pmp, dirclust, diroffset);
951*0a6a1f1dSLionel Sambuc 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
95284d9c625SLionel Sambuc 	    0, bpp)) != 0) {
95384d9c625SLionel Sambuc 		*bpp = NULL;
95484d9c625SLionel Sambuc 		return (error);
95584d9c625SLionel Sambuc 	}
95684d9c625SLionel Sambuc 	if (epp)
95784d9c625SLionel Sambuc 		*epp = bptoep(pmp, *bpp, diroffset);
95884d9c625SLionel Sambuc 	return (0);
95984d9c625SLionel Sambuc }
96084d9c625SLionel Sambuc 
96184d9c625SLionel Sambuc /*
96284d9c625SLionel Sambuc  * Read in the disk block containing the directory entry dep came from and
96384d9c625SLionel Sambuc  * return the address of the buf header, and the address of the directory
96484d9c625SLionel Sambuc  * entry within the block.
96584d9c625SLionel Sambuc  */
96684d9c625SLionel Sambuc int
readde(struct denode * dep,struct buf ** bpp,struct direntry ** epp)96784d9c625SLionel Sambuc readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
96884d9c625SLionel Sambuc {
96984d9c625SLionel Sambuc 	return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
97084d9c625SLionel Sambuc 			bpp, epp));
97184d9c625SLionel Sambuc }
97284d9c625SLionel Sambuc 
97384d9c625SLionel Sambuc /*
97484d9c625SLionel Sambuc  * Remove a directory entry. At this point the file represented by the
97584d9c625SLionel Sambuc  * directory entry to be removed is still full length until noone has it
97684d9c625SLionel Sambuc  * open.  When the file no longer being used msdosfs_inactive() is called
97784d9c625SLionel Sambuc  * and will truncate the file to 0 length.  When the vnode containing the
97884d9c625SLionel Sambuc  * denode is needed for some other purpose by VFS it will call
97984d9c625SLionel Sambuc  * msdosfs_reclaim() which will remove the denode from the denode cache.
98084d9c625SLionel Sambuc  */
98184d9c625SLionel Sambuc int
removede(struct denode * pdep,struct denode * dep)98284d9c625SLionel Sambuc removede(struct denode *pdep, struct denode *dep)
98384d9c625SLionel Sambuc 	/* pdep:	 directory where the entry is removed */
98484d9c625SLionel Sambuc 	/* dep:	 file to be removed */
98584d9c625SLionel Sambuc {
98684d9c625SLionel Sambuc 	int error;
98784d9c625SLionel Sambuc 	struct direntry *ep;
98884d9c625SLionel Sambuc 	struct buf *bp;
98984d9c625SLionel Sambuc 	daddr_t bn;
99084d9c625SLionel Sambuc 	int blsize;
99184d9c625SLionel Sambuc 	struct msdosfsmount *pmp = pdep->de_pmp;
99284d9c625SLionel Sambuc 	u_long offset = pdep->de_fndoffset;
99384d9c625SLionel Sambuc #ifdef _KERNEL
99484d9c625SLionel Sambuc 	int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
99584d9c625SLionel Sambuc #else
99684d9c625SLionel Sambuc #define async 0
99784d9c625SLionel Sambuc #endif
99884d9c625SLionel Sambuc 
99984d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
100084d9c625SLionel Sambuc 	printf("removede(): filename %s, dep %p, offset %08lx\n",
100184d9c625SLionel Sambuc 	    dep->de_Name, dep, offset);
100284d9c625SLionel Sambuc #endif
100384d9c625SLionel Sambuc 
1004*0a6a1f1dSLionel Sambuc 	if (--dep->de_refcnt == 0) {
1005*0a6a1f1dSLionel Sambuc #ifndef MAKEFS
1006*0a6a1f1dSLionel Sambuc 		struct denode_key old_key = dep->de_key;
1007*0a6a1f1dSLionel Sambuc 		struct denode_key new_key = dep->de_key;
1008*0a6a1f1dSLionel Sambuc 
1009*0a6a1f1dSLionel Sambuc 		KASSERT(new_key.dk_dirgen == NULL);
1010*0a6a1f1dSLionel Sambuc 		new_key.dk_dirgen = dep;
1011*0a6a1f1dSLionel Sambuc 		vcache_rekey_enter(pmp->pm_mountp, DETOV(dep), &old_key,
1012*0a6a1f1dSLionel Sambuc 		    sizeof(old_key), &new_key, sizeof(new_key));
1013*0a6a1f1dSLionel Sambuc 		dep->de_key = new_key;
1014*0a6a1f1dSLionel Sambuc 		vcache_rekey_exit(pmp->pm_mountp, DETOV(dep), &old_key,
1015*0a6a1f1dSLionel Sambuc 		    sizeof(old_key), &dep->de_key, sizeof(dep->de_key));
1016*0a6a1f1dSLionel Sambuc #endif
1017*0a6a1f1dSLionel Sambuc 	}
101884d9c625SLionel Sambuc 	offset += sizeof(struct direntry);
101984d9c625SLionel Sambuc 	do {
102084d9c625SLionel Sambuc 		offset -= sizeof(struct direntry);
102184d9c625SLionel Sambuc 		error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
102284d9c625SLionel Sambuc 		if (error)
102384d9c625SLionel Sambuc 			return error;
1024*0a6a1f1dSLionel Sambuc 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
102584d9c625SLionel Sambuc 		    B_MODIFY, &bp);
102684d9c625SLionel Sambuc 		if (error) {
102784d9c625SLionel Sambuc 			return error;
102884d9c625SLionel Sambuc 		}
102984d9c625SLionel Sambuc 		ep = bptoep(pmp, bp, offset);
103084d9c625SLionel Sambuc 		/*
103184d9c625SLionel Sambuc 		 * Check whether, if we came here the second time, i.e.
103284d9c625SLionel Sambuc 		 * when underflowing into the previous block, the last
103384d9c625SLionel Sambuc 		 * entry in this block is a longfilename entry, too.
103484d9c625SLionel Sambuc 		 */
103584d9c625SLionel Sambuc 		if (ep->deAttributes != ATTR_WIN95
103684d9c625SLionel Sambuc 		    && offset != pdep->de_fndoffset) {
103784d9c625SLionel Sambuc 			brelse(bp, 0);
103884d9c625SLionel Sambuc 			break;
103984d9c625SLionel Sambuc 		}
104084d9c625SLionel Sambuc 		offset += sizeof(struct direntry);
104184d9c625SLionel Sambuc 		while (1) {
104284d9c625SLionel Sambuc 			/*
104384d9c625SLionel Sambuc 			 * We are a bit agressive here in that we delete any Win95
104484d9c625SLionel Sambuc 			 * entries preceding this entry, not just the ones we "own".
104584d9c625SLionel Sambuc 			 * Since these presumably aren't valid anyway,
104684d9c625SLionel Sambuc 			 * there should be no harm.
104784d9c625SLionel Sambuc 			 */
104884d9c625SLionel Sambuc 			offset -= sizeof(struct direntry);
104984d9c625SLionel Sambuc 			ep--->deName[0] = SLOT_DELETED;
105084d9c625SLionel Sambuc 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
105184d9c625SLionel Sambuc 			    || !(offset & pmp->pm_crbomask)
105284d9c625SLionel Sambuc 			    || ep->deAttributes != ATTR_WIN95)
105384d9c625SLionel Sambuc 				break;
105484d9c625SLionel Sambuc 		}
105584d9c625SLionel Sambuc 		if (async)
105684d9c625SLionel Sambuc 			bdwrite(bp);
105784d9c625SLionel Sambuc 		else if ((error = bwrite(bp)) != 0)
105884d9c625SLionel Sambuc 			return error;
105984d9c625SLionel Sambuc 	} while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
106084d9c625SLionel Sambuc 	    && !(offset & pmp->pm_crbomask)
106184d9c625SLionel Sambuc 	    && offset);
106284d9c625SLionel Sambuc 	return 0;
106384d9c625SLionel Sambuc }
106484d9c625SLionel Sambuc 
106584d9c625SLionel Sambuc /*
106684d9c625SLionel Sambuc  * Create a unique DOS name in dvp
106784d9c625SLionel Sambuc  */
106884d9c625SLionel Sambuc int
uniqdosname(struct denode * dep,struct componentname * cnp,u_char * cp)106984d9c625SLionel Sambuc uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
107084d9c625SLionel Sambuc {
107184d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
107284d9c625SLionel Sambuc 	struct direntry *dentp;
107384d9c625SLionel Sambuc 	int gen;
107484d9c625SLionel Sambuc 	int blsize;
107584d9c625SLionel Sambuc 	u_long cn;
107684d9c625SLionel Sambuc 	daddr_t bn;
107784d9c625SLionel Sambuc 	struct buf *bp;
107884d9c625SLionel Sambuc 	int error;
107984d9c625SLionel Sambuc 
108084d9c625SLionel Sambuc 	for (gen = 1;; gen++) {
108184d9c625SLionel Sambuc 		/*
108284d9c625SLionel Sambuc 		 * Generate DOS name with generation number
108384d9c625SLionel Sambuc 		 */
108484d9c625SLionel Sambuc 		if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
108584d9c625SLionel Sambuc 		    cnp->cn_namelen, gen))
108684d9c625SLionel Sambuc 			return gen == 1 ? EINVAL : EEXIST;
108784d9c625SLionel Sambuc 
108884d9c625SLionel Sambuc 		/*
108984d9c625SLionel Sambuc 		 * Now look for a dir entry with this exact name
109084d9c625SLionel Sambuc 		 */
109184d9c625SLionel Sambuc 		for (cn = error = 0; !error; cn++) {
109284d9c625SLionel Sambuc 			if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
109384d9c625SLionel Sambuc 				if (error == E2BIG)	/* EOF reached and not found */
109484d9c625SLionel Sambuc 					return 0;
109584d9c625SLionel Sambuc 				return error;
109684d9c625SLionel Sambuc 			}
109784d9c625SLionel Sambuc 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1098*0a6a1f1dSLionel Sambuc 			    0, &bp);
109984d9c625SLionel Sambuc 			if (error) {
110084d9c625SLionel Sambuc 				return error;
110184d9c625SLionel Sambuc 			}
110284d9c625SLionel Sambuc 			for (dentp = (struct direntry *)bp->b_data;
110384d9c625SLionel Sambuc 			     (char *)dentp < (char *)bp->b_data + blsize;
110484d9c625SLionel Sambuc 			     dentp++) {
110584d9c625SLionel Sambuc 				if (dentp->deName[0] == SLOT_EMPTY) {
110684d9c625SLionel Sambuc 					/*
110784d9c625SLionel Sambuc 					 * Last used entry and not found
110884d9c625SLionel Sambuc 					 */
110984d9c625SLionel Sambuc 					brelse(bp, 0);
111084d9c625SLionel Sambuc 					return 0;
111184d9c625SLionel Sambuc 				}
111284d9c625SLionel Sambuc 				/*
111384d9c625SLionel Sambuc 				 * Ignore volume labels and Win95 entries
111484d9c625SLionel Sambuc 				 */
111584d9c625SLionel Sambuc 				if (dentp->deAttributes & ATTR_VOLUME)
111684d9c625SLionel Sambuc 					continue;
111784d9c625SLionel Sambuc 				if (!memcmp(dentp->deName, cp, 11)) {
111884d9c625SLionel Sambuc 					error = EEXIST;
111984d9c625SLionel Sambuc 					break;
112084d9c625SLionel Sambuc 				}
112184d9c625SLionel Sambuc 			}
112284d9c625SLionel Sambuc 			brelse(bp, 0);
112384d9c625SLionel Sambuc 		}
112484d9c625SLionel Sambuc 	}
112584d9c625SLionel Sambuc }
112684d9c625SLionel Sambuc 
112784d9c625SLionel Sambuc /*
112884d9c625SLionel Sambuc  * Find any Win'95 long filename entry in directory dep
112984d9c625SLionel Sambuc  */
113084d9c625SLionel Sambuc int
findwin95(struct denode * dep)113184d9c625SLionel Sambuc findwin95(struct denode *dep)
113284d9c625SLionel Sambuc {
113384d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
113484d9c625SLionel Sambuc 	struct direntry *dentp;
113584d9c625SLionel Sambuc 	int blsize, win95;
113684d9c625SLionel Sambuc 	u_long cn;
113784d9c625SLionel Sambuc 	daddr_t bn;
113884d9c625SLionel Sambuc 	struct buf *bp;
113984d9c625SLionel Sambuc 
114084d9c625SLionel Sambuc 	win95 = 1;
114184d9c625SLionel Sambuc 	/*
114284d9c625SLionel Sambuc 	 * Read through the directory looking for Win'95 entries
114384d9c625SLionel Sambuc 	 * XXX Note: Error currently handled just as EOF
114484d9c625SLionel Sambuc 	 */
114584d9c625SLionel Sambuc 	for (cn = 0;; cn++) {
114684d9c625SLionel Sambuc 		if (pcbmap(dep, cn, &bn, 0, &blsize))
114784d9c625SLionel Sambuc 			return win95;
1148*0a6a1f1dSLionel Sambuc 		if (bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
114984d9c625SLionel Sambuc 		    0, &bp)) {
115084d9c625SLionel Sambuc 			return win95;
115184d9c625SLionel Sambuc 		}
115284d9c625SLionel Sambuc 		for (dentp = (struct direntry *)bp->b_data;
115384d9c625SLionel Sambuc 		     (char *)dentp < (char *)bp->b_data + blsize;
115484d9c625SLionel Sambuc 		     dentp++) {
115584d9c625SLionel Sambuc 			if (dentp->deName[0] == SLOT_EMPTY) {
115684d9c625SLionel Sambuc 				/*
115784d9c625SLionel Sambuc 				 * Last used entry and not found
115884d9c625SLionel Sambuc 				 */
115984d9c625SLionel Sambuc 				brelse(bp, 0);
116084d9c625SLionel Sambuc 				return win95;
116184d9c625SLionel Sambuc 			}
116284d9c625SLionel Sambuc 			if (dentp->deName[0] == SLOT_DELETED) {
116384d9c625SLionel Sambuc 				/*
116484d9c625SLionel Sambuc 				 * Ignore deleted files
116584d9c625SLionel Sambuc 				 * Note: might be an indication of Win'95
116684d9c625SLionel Sambuc 				 * anyway	XXX
116784d9c625SLionel Sambuc 				 */
116884d9c625SLionel Sambuc 				continue;
116984d9c625SLionel Sambuc 			}
117084d9c625SLionel Sambuc 			if (dentp->deAttributes == ATTR_WIN95) {
117184d9c625SLionel Sambuc 				brelse(bp, 0);
117284d9c625SLionel Sambuc 				return 1;
117384d9c625SLionel Sambuc 			}
117484d9c625SLionel Sambuc 			win95 = 0;
117584d9c625SLionel Sambuc 		}
117684d9c625SLionel Sambuc 		brelse(bp, 0);
117784d9c625SLionel Sambuc 	}
117884d9c625SLionel Sambuc }
1179