xref: /freebsd/sys/fs/msdosfs/msdosfs_lookup.c (revision e6e370a7fe930e04cec38bcc2e06be127ed7ee02)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
2952a6212SJordan K. Hubbard /*	$NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $	*/
327a0bc89SDoug Rabson 
427a0bc89SDoug Rabson /*-
5952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
727a0bc89SDoug Rabson  * All rights reserved.
827a0bc89SDoug Rabson  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
927a0bc89SDoug Rabson  *
1027a0bc89SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1127a0bc89SDoug Rabson  * modification, are permitted provided that the following conditions
1227a0bc89SDoug Rabson  * are met:
1327a0bc89SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1427a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1527a0bc89SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1627a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1727a0bc89SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1827a0bc89SDoug Rabson  * 3. All advertising materials mentioning features or use of this software
1927a0bc89SDoug Rabson  *    must display the following acknowledgement:
2027a0bc89SDoug Rabson  *	This product includes software developed by TooLs GmbH.
2127a0bc89SDoug Rabson  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2227a0bc89SDoug Rabson  *    derived from this software without specific prior written permission.
2327a0bc89SDoug Rabson  *
2427a0bc89SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2527a0bc89SDoug Rabson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2627a0bc89SDoug Rabson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2727a0bc89SDoug Rabson  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2827a0bc89SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2927a0bc89SDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3027a0bc89SDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3127a0bc89SDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3227a0bc89SDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3327a0bc89SDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3427a0bc89SDoug Rabson  */
3527a0bc89SDoug Rabson /*
3627a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3727a0bc89SDoug Rabson  *
3827a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
3927a0bc89SDoug Rabson  * it, and don't remove this notice.
4027a0bc89SDoug Rabson  *
4127a0bc89SDoug Rabson  * This software is provided "as is".
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4427a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4527a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4627a0bc89SDoug Rabson  * any damages caused by this software.
4727a0bc89SDoug Rabson  *
4827a0bc89SDoug Rabson  * October 1992
4927a0bc89SDoug Rabson  */
5027a0bc89SDoug Rabson 
5127a0bc89SDoug Rabson #include <sys/param.h>
52919d1ea2SPeter Wemm #include <sys/systm.h>
5327a0bc89SDoug Rabson #include <sys/namei.h>
549626b608SPoul-Henning Kamp #include <sys/bio.h>
5527a0bc89SDoug Rabson #include <sys/buf.h>
5627a0bc89SDoug Rabson #include <sys/vnode.h>
5727a0bc89SDoug Rabson #include <sys/mount.h>
5827a0bc89SDoug Rabson 
591166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h>
601166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h>
611166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h>
621166fb51SRuslan Ermilov #include <fs/msdosfs/msdosfsmount.h>
631166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h>
6427a0bc89SDoug Rabson 
6527a0bc89SDoug Rabson /*
6627a0bc89SDoug Rabson  * When we search a directory the blocks containing directory entries are
6727a0bc89SDoug Rabson  * read and examined.  The directory entries contain information that would
6827a0bc89SDoug Rabson  * normally be in the inode of a unix filesystem.  This means that some of
6927a0bc89SDoug Rabson  * a directory's contents may also be in memory resident denodes (sort of
7027a0bc89SDoug Rabson  * an inode).  This can cause problems if we are searching while some other
7127a0bc89SDoug Rabson  * process is modifying a directory.  To prevent one process from accessing
7227a0bc89SDoug Rabson  * incompletely modified directory information we depend upon being the
73b84d2921SPoul-Henning Kamp  * sole owner of a directory block.  bread/brelse provide this service.
7427a0bc89SDoug Rabson  * This being the case, when a process modifies a directory it must first
7527a0bc89SDoug Rabson  * acquire the disk block that contains the directory entry to be modified.
7627a0bc89SDoug Rabson  * Then update the disk block and the denode, and then write the disk block
7727a0bc89SDoug Rabson  * out to disk.  This way disk blocks containing directory entries and in
7827a0bc89SDoug Rabson  * memory denode's will be in synch.
7927a0bc89SDoug Rabson  */
8027a0bc89SDoug Rabson int
8127a0bc89SDoug Rabson msdosfs_lookup(ap)
820fa2443fSPoul-Henning Kamp 	struct vop_cachedlookup_args /* {
8327a0bc89SDoug Rabson 		struct vnode *a_dvp;
8427a0bc89SDoug Rabson 		struct vnode **a_vpp;
8527a0bc89SDoug Rabson 		struct componentname *a_cnp;
8627a0bc89SDoug Rabson 	} */ *ap;
8727a0bc89SDoug Rabson {
8827a0bc89SDoug Rabson 	struct vnode *vdp = ap->a_dvp;
8927a0bc89SDoug Rabson 	struct vnode **vpp = ap->a_vpp;
9027a0bc89SDoug Rabson 	struct componentname *cnp = ap->a_cnp;
9127a0bc89SDoug Rabson 	daddr_t bn;
9227a0bc89SDoug Rabson 	int error;
9327a0bc89SDoug Rabson 	int lockparent;
9427a0bc89SDoug Rabson 	int wantparent;
95952a6212SJordan K. Hubbard 	int slotcount;
96952a6212SJordan K. Hubbard 	int slotoffset = 0;
9727a0bc89SDoug Rabson 	int frcn;
9827a0bc89SDoug Rabson 	u_long cluster;
99952a6212SJordan K. Hubbard 	int blkoff;
10027a0bc89SDoug Rabson 	int diroff;
101952a6212SJordan K. Hubbard 	int blsize;
10227a0bc89SDoug Rabson 	int isadir;		/* ~0 if found direntry is a directory	 */
10327a0bc89SDoug Rabson 	u_long scn;		/* starting cluster number		 */
10427a0bc89SDoug Rabson 	struct vnode *pdp;
10527a0bc89SDoug Rabson 	struct denode *dp;
10627a0bc89SDoug Rabson 	struct denode *tdp;
10727a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
10827a0bc89SDoug Rabson 	struct buf *bp = 0;
10927a0bc89SDoug Rabson 	struct direntry *dep = NULL;
11027a0bc89SDoug Rabson 	u_char dosfilename[12];
11127a0bc89SDoug Rabson 	int flags = cnp->cn_flags;
11227a0bc89SDoug Rabson 	int nameiop = cnp->cn_nameiop;
113b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
11469a36f24SMike Smith 	int unlen;
11527a0bc89SDoug Rabson 
116952a6212SJordan K. Hubbard 	int wincnt = 1;
117952a6212SJordan K. Hubbard 	int chksum = -1;
118952a6212SJordan K. Hubbard 	int olddos = 1;
119e7b1ac75SBoris Popov 	cnp->cn_flags &= ~PDIRUNLOCK;
120952a6212SJordan K. Hubbard 
12127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
12227a0bc89SDoug Rabson 	printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
12327a0bc89SDoug Rabson #endif
12427a0bc89SDoug Rabson 	dp = VTODE(vdp);
12527a0bc89SDoug Rabson 	pmp = dp->de_pmp;
12627a0bc89SDoug Rabson 	*vpp = NULL;
12727a0bc89SDoug Rabson 	lockparent = flags & LOCKPARENT;
12827a0bc89SDoug Rabson 	wantparent = flags & (LOCKPARENT | WANTPARENT);
12927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
130952a6212SJordan K. Hubbard 	printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
13127a0bc89SDoug Rabson 	    vdp, dp, dp->de_Attributes);
13227a0bc89SDoug Rabson #endif
13327a0bc89SDoug Rabson 
13427a0bc89SDoug Rabson 	/*
13527a0bc89SDoug Rabson 	 * If they are going after the . or .. entry in the root directory,
13627a0bc89SDoug Rabson 	 * they won't find it.  DOS filesystems don't have them in the root
13727a0bc89SDoug Rabson 	 * directory.  So, we fake it. deget() is in on this scam too.
13827a0bc89SDoug Rabson 	 */
139e6e370a7SJeff Roberson 	if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
14027a0bc89SDoug Rabson 	    (cnp->cn_namelen == 1 ||
14127a0bc89SDoug Rabson 		(cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
14227a0bc89SDoug Rabson 		isadir = ATTR_DIRECTORY;
14327a0bc89SDoug Rabson 		scn = MSDOSFSROOT;
14427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
14527a0bc89SDoug Rabson 		printf("msdosfs_lookup(): looking for . or .. in root directory\n");
14627a0bc89SDoug Rabson #endif
14727a0bc89SDoug Rabson 		cluster = MSDOSFSROOT;
148952a6212SJordan K. Hubbard 		blkoff = MSDOSFSROOT_OFS;
14927a0bc89SDoug Rabson 		goto foundroot;
15027a0bc89SDoug Rabson 	}
15127a0bc89SDoug Rabson 
152952a6212SJordan K. Hubbard 	switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
1537391f611SAndrey A. Chernov 	    cnp->cn_namelen, 0,
1547391f611SAndrey A. Chernov 	    pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
1557391f611SAndrey A. Chernov 	    pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu)) {
156952a6212SJordan K. Hubbard 	case 0:
157952a6212SJordan K. Hubbard 		return (EINVAL);
158952a6212SJordan K. Hubbard 	case 1:
159952a6212SJordan K. Hubbard 		break;
160952a6212SJordan K. Hubbard 	case 2:
161952a6212SJordan K. Hubbard 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
162952a6212SJordan K. Hubbard 		    cnp->cn_namelen) + 1;
163952a6212SJordan K. Hubbard 		break;
164952a6212SJordan K. Hubbard 	case 3:
165952a6212SJordan K. Hubbard 		olddos = 0;
166952a6212SJordan K. Hubbard 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
167952a6212SJordan K. Hubbard 		    cnp->cn_namelen) + 1;
168952a6212SJordan K. Hubbard 		break;
16927a0bc89SDoug Rabson 	}
170011cdb57SDmitrij Tejblum 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
171952a6212SJordan K. Hubbard 		wincnt = 1;
172011cdb57SDmitrij Tejblum 		olddos = 1;
173011cdb57SDmitrij Tejblum 	}
17469a36f24SMike Smith 	unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
17527a0bc89SDoug Rabson 
176952a6212SJordan K. Hubbard 	/*
177952a6212SJordan K. Hubbard 	 * Suppress search for slots unless creating
178952a6212SJordan K. Hubbard 	 * file and at end of pathname, in which case
179952a6212SJordan K. Hubbard 	 * we watch for a place to put the new file in
180952a6212SJordan K. Hubbard 	 * case it doesn't already exist.
181952a6212SJordan K. Hubbard 	 */
182952a6212SJordan K. Hubbard 	slotcount = wincnt;
183952a6212SJordan K. Hubbard 	if ((nameiop == CREATE || nameiop == RENAME) &&
184952a6212SJordan K. Hubbard 	    (flags & ISLASTCN))
185952a6212SJordan K. Hubbard 		slotcount = 0;
186952a6212SJordan K. Hubbard 
18727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
188952a6212SJordan K. Hubbard 	printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
18927a0bc89SDoug Rabson 	    dosfilename, cnp->cn_namelen);
19027a0bc89SDoug Rabson #endif
19127a0bc89SDoug Rabson 	/*
19227a0bc89SDoug Rabson 	 * Search the directory pointed at by vdp for the name pointed at
19327a0bc89SDoug Rabson 	 * by cnp->cn_nameptr.
19427a0bc89SDoug Rabson 	 */
19527a0bc89SDoug Rabson 	tdp = NULL;
19627a0bc89SDoug Rabson 	/*
19727a0bc89SDoug Rabson 	 * The outer loop ranges over the clusters that make up the
19827a0bc89SDoug Rabson 	 * directory.  Note that the root directory is different from all
19927a0bc89SDoug Rabson 	 * other directories.  It has a fixed number of blocks that are not
20027a0bc89SDoug Rabson 	 * part of the pool of allocatable clusters.  So, we treat it a
20127a0bc89SDoug Rabson 	 * little differently. The root directory starts at "cluster" 0.
20227a0bc89SDoug Rabson 	 */
203952a6212SJordan K. Hubbard 	diroff = 0;
20427a0bc89SDoug Rabson 	for (frcn = 0;; frcn++) {
205952a6212SJordan K. Hubbard 		error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
206c3c6d51eSPoul-Henning Kamp 		if (error) {
20727a0bc89SDoug Rabson 			if (error == E2BIG)
20827a0bc89SDoug Rabson 				break;
209952a6212SJordan K. Hubbard 			return (error);
21027a0bc89SDoug Rabson 		}
211952a6212SJordan K. Hubbard 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
212952a6212SJordan K. Hubbard 		if (error) {
213952a6212SJordan K. Hubbard 			brelse(bp);
214952a6212SJordan K. Hubbard 			return (error);
215952a6212SJordan K. Hubbard 		}
216952a6212SJordan K. Hubbard 		for (blkoff = 0; blkoff < blsize;
217952a6212SJordan K. Hubbard 		     blkoff += sizeof(struct direntry),
218952a6212SJordan K. Hubbard 		     diroff += sizeof(struct direntry)) {
219952a6212SJordan K. Hubbard 			dep = (struct direntry *)(bp->b_data + blkoff);
22027a0bc89SDoug Rabson 			/*
22127a0bc89SDoug Rabson 			 * If the slot is empty and we are still looking
22227a0bc89SDoug Rabson 			 * for an empty then remember this one.  If the
22327a0bc89SDoug Rabson 			 * slot is not empty then check to see if it
22427a0bc89SDoug Rabson 			 * matches what we are looking for.  If the slot
22527a0bc89SDoug Rabson 			 * has never been filled with anything, then the
22627a0bc89SDoug Rabson 			 * remainder of the directory has never been used,
22727a0bc89SDoug Rabson 			 * so there is no point in searching it.
22827a0bc89SDoug Rabson 			 */
22927a0bc89SDoug Rabson 			if (dep->deName[0] == SLOT_EMPTY ||
23027a0bc89SDoug Rabson 			    dep->deName[0] == SLOT_DELETED) {
231952a6212SJordan K. Hubbard 				/*
232952a6212SJordan K. Hubbard 				 * Drop memory of previous long matches
233952a6212SJordan K. Hubbard 				 */
234952a6212SJordan K. Hubbard 				chksum = -1;
235952a6212SJordan K. Hubbard 
236952a6212SJordan K. Hubbard 				if (slotcount < wincnt) {
237952a6212SJordan K. Hubbard 					slotcount++;
23827a0bc89SDoug Rabson 					slotoffset = diroff;
23927a0bc89SDoug Rabson 				}
24027a0bc89SDoug Rabson 				if (dep->deName[0] == SLOT_EMPTY) {
24127a0bc89SDoug Rabson 					brelse(bp);
24227a0bc89SDoug Rabson 					goto notfound;
24327a0bc89SDoug Rabson 				}
24427a0bc89SDoug Rabson 			} else {
24527a0bc89SDoug Rabson 				/*
246952a6212SJordan K. Hubbard 				 * If there wasn't enough space for our winentries,
247952a6212SJordan K. Hubbard 				 * forget about the empty space
248952a6212SJordan K. Hubbard 				 */
249952a6212SJordan K. Hubbard 				if (slotcount < wincnt)
250952a6212SJordan K. Hubbard 					slotcount = 0;
251952a6212SJordan K. Hubbard 
252952a6212SJordan K. Hubbard 				/*
253952a6212SJordan K. Hubbard 				 * Check for Win95 long filename entry
254952a6212SJordan K. Hubbard 				 */
255952a6212SJordan K. Hubbard 				if (dep->deAttributes == ATTR_WIN95) {
256952a6212SJordan K. Hubbard 					if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
257952a6212SJordan K. Hubbard 						continue;
258952a6212SJordan K. Hubbard 
259952a6212SJordan K. Hubbard 					chksum = winChkName((const u_char *)cnp->cn_nameptr,
26069a36f24SMike Smith 							    unlen,
261952a6212SJordan K. Hubbard 							    (struct winentry *)dep,
2624d63452fSAndrey A. Chernov 							    chksum,
2637391f611SAndrey A. Chernov 							    pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
2647391f611SAndrey A. Chernov 							    pmp->pm_u2w,
2657391f611SAndrey A. Chernov 							    pmp->pm_flags & MSDOSFSMNT_ULTABLE,
2665114f1d7SAndrey A. Chernov 							    pmp->pm_ul);
267952a6212SJordan K. Hubbard 					continue;
268952a6212SJordan K. Hubbard 				}
269952a6212SJordan K. Hubbard 
270952a6212SJordan K. Hubbard 				/*
27127a0bc89SDoug Rabson 				 * Ignore volume labels (anywhere, not just
27227a0bc89SDoug Rabson 				 * the root directory).
27327a0bc89SDoug Rabson 				 */
274952a6212SJordan K. Hubbard 				if (dep->deAttributes & ATTR_VOLUME) {
275952a6212SJordan K. Hubbard 					chksum = -1;
276952a6212SJordan K. Hubbard 					continue;
277952a6212SJordan K. Hubbard 				}
278952a6212SJordan K. Hubbard 
279952a6212SJordan K. Hubbard 				/*
280952a6212SJordan K. Hubbard 				 * Check for a checksum or name match
281952a6212SJordan K. Hubbard 				 */
282952a6212SJordan K. Hubbard 				if (chksum != winChksum(dep->deName)
283952a6212SJordan K. Hubbard 				    && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
284952a6212SJordan K. Hubbard 					chksum = -1;
285952a6212SJordan K. Hubbard 					continue;
286952a6212SJordan K. Hubbard 				}
28727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
288952a6212SJordan K. Hubbard 				printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
289952a6212SJordan K. Hubbard 				    blkoff, diroff);
29027a0bc89SDoug Rabson #endif
29127a0bc89SDoug Rabson 				/*
29227a0bc89SDoug Rabson 				 * Remember where this directory
29327a0bc89SDoug Rabson 				 * entry came from for whoever did
294952a6212SJordan K. Hubbard 				 * this lookup.
29527a0bc89SDoug Rabson 				 */
29627a0bc89SDoug Rabson 				dp->de_fndoffset = diroff;
297191e6fd0SDmitrij Tejblum 				dp->de_fndcnt = wincnt - 1;
298952a6212SJordan K. Hubbard 
29927a0bc89SDoug Rabson 				goto found;
30027a0bc89SDoug Rabson 			}
301952a6212SJordan K. Hubbard 		}	/* for (blkoff = 0; .... */
30227a0bc89SDoug Rabson 		/*
30327a0bc89SDoug Rabson 		 * Release the buffer holding the directory cluster just
30427a0bc89SDoug Rabson 		 * searched.
30527a0bc89SDoug Rabson 		 */
30627a0bc89SDoug Rabson 		brelse(bp);
30727a0bc89SDoug Rabson 	}	/* for (frcn = 0; ; frcn++) */
308952a6212SJordan K. Hubbard 
309952a6212SJordan K. Hubbard notfound:
31027a0bc89SDoug Rabson 	/*
31127a0bc89SDoug Rabson 	 * We hold no disk buffers at this point.
31227a0bc89SDoug Rabson 	 */
31327a0bc89SDoug Rabson 
31427a0bc89SDoug Rabson 	/*
315952a6212SJordan K. Hubbard 	 * Fixup the slot description to point to the place where
316952a6212SJordan K. Hubbard 	 * we might put the new DOS direntry (putting the Win95
317952a6212SJordan K. Hubbard 	 * long name entries before that)
318952a6212SJordan K. Hubbard 	 */
319952a6212SJordan K. Hubbard 	if (!slotcount) {
320952a6212SJordan K. Hubbard 		slotcount = 1;
321952a6212SJordan K. Hubbard 		slotoffset = diroff;
322952a6212SJordan K. Hubbard 	}
323952a6212SJordan K. Hubbard 	if (wincnt > slotcount)
324952a6212SJordan K. Hubbard 		slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
325952a6212SJordan K. Hubbard 
326952a6212SJordan K. Hubbard 	/*
32727a0bc89SDoug Rabson 	 * If we get here we didn't find the entry we were looking for. But
32827a0bc89SDoug Rabson 	 * that's ok if we are creating or renaming and are at the end of
32927a0bc89SDoug Rabson 	 * the pathname and the directory hasn't been removed.
33027a0bc89SDoug Rabson 	 */
33127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
332952a6212SJordan K. Hubbard 	printf("msdosfs_lookup(): op %d, refcnt %ld\n",
333952a6212SJordan K. Hubbard 	    nameiop, dp->de_refcnt);
334952a6212SJordan K. Hubbard 	printf("               slotcount %d, slotoffset %d\n",
335952a6212SJordan K. Hubbard 	       slotcount, slotoffset);
33627a0bc89SDoug Rabson #endif
33727a0bc89SDoug Rabson 	if ((nameiop == CREATE || nameiop == RENAME) &&
33827a0bc89SDoug Rabson 	    (flags & ISLASTCN) && dp->de_refcnt != 0) {
339952a6212SJordan K. Hubbard 		/*
340952a6212SJordan K. Hubbard 		 * Access for write is interpreted as allowing
341952a6212SJordan K. Hubbard 		 * creation of files in the directory.
342952a6212SJordan K. Hubbard 		 */
343b40ce416SJulian Elischer 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
344d8762fa6SBruce Evans 		if (error)
345952a6212SJordan K. Hubbard 			return (error);
346952a6212SJordan K. Hubbard 		/*
347952a6212SJordan K. Hubbard 		 * Return an indication of where the new directory
348952a6212SJordan K. Hubbard 		 * entry should be put.
349952a6212SJordan K. Hubbard 		 */
35027a0bc89SDoug Rabson 		dp->de_fndoffset = slotoffset;
351952a6212SJordan K. Hubbard 		dp->de_fndcnt = wincnt - 1;
352952a6212SJordan K. Hubbard 
353952a6212SJordan K. Hubbard 		/*
354952a6212SJordan K. Hubbard 		 * We return with the directory locked, so that
355952a6212SJordan K. Hubbard 		 * the parameters we set up above will still be
356952a6212SJordan K. Hubbard 		 * valid if we actually decide to do a direnter().
357952a6212SJordan K. Hubbard 		 * We return ni_vp == NULL to indicate that the entry
358952a6212SJordan K. Hubbard 		 * does not currently exist; we leave a pointer to
359952a6212SJordan K. Hubbard 		 * the (locked) directory inode in ndp->ni_dvp.
360952a6212SJordan K. Hubbard 		 * The pathname buffer is saved so that the name
361952a6212SJordan K. Hubbard 		 * can be obtained later.
362952a6212SJordan K. Hubbard 		 *
363952a6212SJordan K. Hubbard 		 * NB - if the directory is unlocked, then this
364952a6212SJordan K. Hubbard 		 * information cannot be used.
365952a6212SJordan K. Hubbard 		 */
36627a0bc89SDoug Rabson 		cnp->cn_flags |= SAVENAME;
367e7b1ac75SBoris Popov 		if (!lockparent) {
368b40ce416SJulian Elischer 			VOP_UNLOCK(vdp, 0, td);
369e7b1ac75SBoris Popov 			cnp->cn_flags |= PDIRUNLOCK;
370e7b1ac75SBoris Popov 		}
371952a6212SJordan K. Hubbard 		return (EJUSTRETURN);
37227a0bc89SDoug Rabson 	}
37327a0bc89SDoug Rabson 	/*
374952a6212SJordan K. Hubbard 	 * Insert name into cache (as non-existent) if appropriate.
37527a0bc89SDoug Rabson 	 */
37627a0bc89SDoug Rabson 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
37727a0bc89SDoug Rabson 		cache_enter(vdp, *vpp, cnp);
378952a6212SJordan K. Hubbard 	return (ENOENT);
37927a0bc89SDoug Rabson 
380952a6212SJordan K. Hubbard found:
38127a0bc89SDoug Rabson 	/*
38227a0bc89SDoug Rabson 	 * NOTE:  We still have the buffer with matched directory entry at
38327a0bc89SDoug Rabson 	 * this point.
38427a0bc89SDoug Rabson 	 */
38527a0bc89SDoug Rabson 	isadir = dep->deAttributes & ATTR_DIRECTORY;
38627a0bc89SDoug Rabson 	scn = getushort(dep->deStartCluster);
387952a6212SJordan K. Hubbard 	if (FAT32(pmp)) {
388952a6212SJordan K. Hubbard 		scn |= getushort(dep->deHighClust) << 16;
389952a6212SJordan K. Hubbard 		if (scn == pmp->pm_rootdirblk) {
390952a6212SJordan K. Hubbard 			/*
391952a6212SJordan K. Hubbard 			 * There should actually be 0 here.
392952a6212SJordan K. Hubbard 			 * Just ignore the error.
393952a6212SJordan K. Hubbard 			 */
394952a6212SJordan K. Hubbard 			scn = MSDOSFSROOT;
395952a6212SJordan K. Hubbard 		}
396952a6212SJordan K. Hubbard 	}
39727a0bc89SDoug Rabson 
398952a6212SJordan K. Hubbard 	if (isadir) {
399952a6212SJordan K. Hubbard 		cluster = scn;
400952a6212SJordan K. Hubbard 		if (cluster == MSDOSFSROOT)
401952a6212SJordan K. Hubbard 			blkoff = MSDOSFSROOT_OFS;
402952a6212SJordan K. Hubbard 		else
403952a6212SJordan K. Hubbard 			blkoff = 0;
404952a6212SJordan K. Hubbard 	} else if (cluster == MSDOSFSROOT)
405952a6212SJordan K. Hubbard 		blkoff = diroff;
406952a6212SJordan K. Hubbard 
407952a6212SJordan K. Hubbard 	/*
408952a6212SJordan K. Hubbard 	 * Now release buf to allow deget to read the entry again.
409952a6212SJordan K. Hubbard 	 * Reserving it here and giving it to deget could result
410952a6212SJordan K. Hubbard 	 * in a deadlock.
411952a6212SJordan K. Hubbard 	 */
412952a6212SJordan K. Hubbard 	brelse(bp);
413952a6212SJordan K. Hubbard 	bp = 0;
414952a6212SJordan K. Hubbard 
415952a6212SJordan K. Hubbard foundroot:
41627a0bc89SDoug Rabson 	/*
41727a0bc89SDoug Rabson 	 * If we entered at foundroot, then we are looking for the . or ..
41827a0bc89SDoug Rabson 	 * entry of the filesystems root directory.  isadir and scn were
419952a6212SJordan K. Hubbard 	 * setup before jumping here.  And, bp is already null.
42027a0bc89SDoug Rabson 	 */
421952a6212SJordan K. Hubbard 	if (FAT32(pmp) && scn == MSDOSFSROOT)
422952a6212SJordan K. Hubbard 		scn = pmp->pm_rootdirblk;
42327a0bc89SDoug Rabson 
42427a0bc89SDoug Rabson 	/*
425952a6212SJordan K. Hubbard 	 * If deleting, and at end of pathname, return
426952a6212SJordan K. Hubbard 	 * parameters which can be used to remove file.
427952a6212SJordan K. Hubbard 	 * If the wantparent flag isn't set, we return only
428952a6212SJordan K. Hubbard 	 * the directory (in ndp->ni_dvp), otherwise we go
429952a6212SJordan K. Hubbard 	 * on and lock the inode, being careful with ".".
43027a0bc89SDoug Rabson 	 */
43127a0bc89SDoug Rabson 	if (nameiop == DELETE && (flags & ISLASTCN)) {
432952a6212SJordan K. Hubbard 		/*
433952a6212SJordan K. Hubbard 		 * Don't allow deleting the root.
434952a6212SJordan K. Hubbard 		 */
435952a6212SJordan K. Hubbard 		if (blkoff == MSDOSFSROOT_OFS)
436952a6212SJordan K. Hubbard 			return EROFS;				/* really? XXX */
437952a6212SJordan K. Hubbard 
438952a6212SJordan K. Hubbard 		/*
439952a6212SJordan K. Hubbard 		 * Write access to directory required to delete files.
440952a6212SJordan K. Hubbard 		 */
441b40ce416SJulian Elischer 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
442952a6212SJordan K. Hubbard 		if (error)
443952a6212SJordan K. Hubbard 			return (error);
444952a6212SJordan K. Hubbard 
445952a6212SJordan K. Hubbard 		/*
446952a6212SJordan K. Hubbard 		 * Return pointer to current entry in dp->i_offset.
447952a6212SJordan K. Hubbard 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
448952a6212SJordan K. Hubbard 		 */
44927a0bc89SDoug Rabson 		if (dp->de_StartCluster == scn && isadir) {	/* "." */
45027a0bc89SDoug Rabson 			VREF(vdp);
45127a0bc89SDoug Rabson 			*vpp = vdp;
452952a6212SJordan K. Hubbard 			return (0);
45327a0bc89SDoug Rabson 		}
454952a6212SJordan K. Hubbard 		error = deget(pmp, cluster, blkoff, &tdp);
455952a6212SJordan K. Hubbard 		if (error)
456952a6212SJordan K. Hubbard 			return (error);
45727a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
458e7b1ac75SBoris Popov 		if (!lockparent) {
459b40ce416SJulian Elischer 			VOP_UNLOCK(vdp, 0, td);
460e7b1ac75SBoris Popov 			cnp->cn_flags |= PDIRUNLOCK;
461e7b1ac75SBoris Popov 		}
462952a6212SJordan K. Hubbard 		return (0);
46327a0bc89SDoug Rabson 	}
46427a0bc89SDoug Rabson 
46527a0bc89SDoug Rabson 	/*
466952a6212SJordan K. Hubbard 	 * If rewriting (RENAME), return the inode and the
467952a6212SJordan K. Hubbard 	 * information required to rewrite the present directory
468952a6212SJordan K. Hubbard 	 * Must get inode of directory entry to verify it's a
469952a6212SJordan K. Hubbard 	 * regular file, or empty directory.
47027a0bc89SDoug Rabson 	 */
471952a6212SJordan K. Hubbard 	if (nameiop == RENAME && wantparent &&
472952a6212SJordan K. Hubbard 	    (flags & ISLASTCN)) {
473952a6212SJordan K. Hubbard 		if (blkoff == MSDOSFSROOT_OFS)
474952a6212SJordan K. Hubbard 			return EROFS;				/* really? XXX */
475952a6212SJordan K. Hubbard 
476b40ce416SJulian Elischer 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
477952a6212SJordan K. Hubbard 		if (error)
478952a6212SJordan K. Hubbard 			return (error);
479952a6212SJordan K. Hubbard 
480952a6212SJordan K. Hubbard 		/*
481952a6212SJordan K. Hubbard 		 * Careful about locking second inode.
482952a6212SJordan K. Hubbard 		 * This can only occur if the target is ".".
483952a6212SJordan K. Hubbard 		 */
484952a6212SJordan K. Hubbard 		if (dp->de_StartCluster == scn && isadir)
485952a6212SJordan K. Hubbard 			return (EISDIR);
486952a6212SJordan K. Hubbard 
487952a6212SJordan K. Hubbard 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
488952a6212SJordan K. Hubbard 			return (error);
48927a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
49027a0bc89SDoug Rabson 		cnp->cn_flags |= SAVENAME;
491e7b1ac75SBoris Popov 		if (!lockparent) {
492b40ce416SJulian Elischer 			VOP_UNLOCK(vdp, 0, td);
493e7b1ac75SBoris Popov 			cnp->cn_flags |= PDIRUNLOCK;
494e7b1ac75SBoris Popov 		}
495952a6212SJordan K. Hubbard 		return (0);
49627a0bc89SDoug Rabson 	}
49727a0bc89SDoug Rabson 
49827a0bc89SDoug Rabson 	/*
499952a6212SJordan K. Hubbard 	 * Step through the translation in the name.  We do not `vput' the
500952a6212SJordan K. Hubbard 	 * directory because we may need it again if a symbolic link
501952a6212SJordan K. Hubbard 	 * is relative to the current directory.  Instead we save it
502952a6212SJordan K. Hubbard 	 * unlocked as "pdp".  We must get the target inode before unlocking
503952a6212SJordan K. Hubbard 	 * the directory to insure that the inode will not be removed
504952a6212SJordan K. Hubbard 	 * before we get it.  We prevent deadlock by always fetching
505952a6212SJordan K. Hubbard 	 * inodes from the root, moving down the directory tree. Thus
506952a6212SJordan K. Hubbard 	 * when following backward pointers ".." we must unlock the
507952a6212SJordan K. Hubbard 	 * parent directory before getting the requested directory.
508952a6212SJordan K. Hubbard 	 * There is a potential race condition here if both the current
509952a6212SJordan K. Hubbard 	 * and parent directories are removed before the VFS_VGET for the
510952a6212SJordan K. Hubbard 	 * inode associated with ".." returns.  We hope that this occurs
511952a6212SJordan K. Hubbard 	 * infrequently since we cannot avoid this race condition without
512952a6212SJordan K. Hubbard 	 * implementing a sophisticated deadlock detection algorithm.
513952a6212SJordan K. Hubbard 	 * Note also that this simple deadlock detection scheme will not
514952a6212SJordan K. Hubbard 	 * work if the filesystem has any hard links other than ".."
515952a6212SJordan K. Hubbard 	 * that point backwards in the directory structure.
51627a0bc89SDoug Rabson 	 */
51727a0bc89SDoug Rabson 	pdp = vdp;
51827a0bc89SDoug Rabson 	if (flags & ISDOTDOT) {
519b40ce416SJulian Elischer 		VOP_UNLOCK(pdp, 0, td);
520e7b1ac75SBoris Popov 		cnp->cn_flags |= PDIRUNLOCK;
521952a6212SJordan K. Hubbard 		error = deget(pmp, cluster, blkoff,  &tdp);
52227a0bc89SDoug Rabson 		if (error) {
523b40ce416SJulian Elischer 			vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
524e7b1ac75SBoris Popov 			cnp->cn_flags &= ~PDIRUNLOCK;
525952a6212SJordan K. Hubbard 			return (error);
52627a0bc89SDoug Rabson 		}
527e7b1ac75SBoris Popov 		if (lockparent && (flags & ISLASTCN)) {
528b40ce416SJulian Elischer 			error = vn_lock(pdp, LK_EXCLUSIVE, td);
529e7b1ac75SBoris Popov 			if (error) {
53027a0bc89SDoug Rabson 				vput(DETOV(tdp));
531952a6212SJordan K. Hubbard 				return (error);
53227a0bc89SDoug Rabson 			}
533e7b1ac75SBoris Popov 			cnp->cn_flags &= ~PDIRUNLOCK;
534e7b1ac75SBoris Popov 		}
53527a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
536952a6212SJordan K. Hubbard 	} else if (dp->de_StartCluster == scn && isadir) {
537952a6212SJordan K. Hubbard 		VREF(vdp);	/* we want ourself, ie "." */
53827a0bc89SDoug Rabson 		*vpp = vdp;
53927a0bc89SDoug Rabson 	} else {
540952a6212SJordan K. Hubbard 		if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
541952a6212SJordan K. Hubbard 			return (error);
542e7b1ac75SBoris Popov 		if (!lockparent || !(flags & ISLASTCN)) {
543b40ce416SJulian Elischer 			VOP_UNLOCK(pdp, 0, td);
544e7b1ac75SBoris Popov 			cnp->cn_flags |= PDIRUNLOCK;
545e7b1ac75SBoris Popov 		}
54627a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
54727a0bc89SDoug Rabson 	}
54827a0bc89SDoug Rabson 
54927a0bc89SDoug Rabson 	/*
550952a6212SJordan K. Hubbard 	 * Insert name into cache if appropriate.
55127a0bc89SDoug Rabson 	 */
55227a0bc89SDoug Rabson 	if (cnp->cn_flags & MAKEENTRY)
55327a0bc89SDoug Rabson 		cache_enter(vdp, *vpp, cnp);
554952a6212SJordan K. Hubbard 	return (0);
55527a0bc89SDoug Rabson }
55627a0bc89SDoug Rabson 
55727a0bc89SDoug Rabson /*
55827a0bc89SDoug Rabson  * dep  - directory entry to copy into the directory
55927a0bc89SDoug Rabson  * ddep - directory to add to
56027a0bc89SDoug Rabson  * depp - return the address of the denode for the created directory entry
56127a0bc89SDoug Rabson  *	  if depp != 0
562952a6212SJordan K. Hubbard  * cnp  - componentname needed for Win95 long filenames
56327a0bc89SDoug Rabson  */
56427a0bc89SDoug Rabson int
565952a6212SJordan K. Hubbard createde(dep, ddep, depp, cnp)
56627a0bc89SDoug Rabson 	struct denode *dep;
56727a0bc89SDoug Rabson 	struct denode *ddep;
56827a0bc89SDoug Rabson 	struct denode **depp;
569952a6212SJordan K. Hubbard 	struct componentname *cnp;
57027a0bc89SDoug Rabson {
57127a0bc89SDoug Rabson 	int error;
57227a0bc89SDoug Rabson 	u_long dirclust, diroffset;
57327a0bc89SDoug Rabson 	struct direntry *ndep;
57427a0bc89SDoug Rabson 	struct msdosfsmount *pmp = ddep->de_pmp;
57527a0bc89SDoug Rabson 	struct buf *bp;
576952a6212SJordan K. Hubbard 	daddr_t bn;
577952a6212SJordan K. Hubbard 	int blsize;
57827a0bc89SDoug Rabson 
57927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
580952a6212SJordan K. Hubbard 	printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
581952a6212SJordan K. Hubbard 	    dep, ddep, depp, cnp);
58227a0bc89SDoug Rabson #endif
58327a0bc89SDoug Rabson 
58427a0bc89SDoug Rabson 	/*
58527a0bc89SDoug Rabson 	 * If no space left in the directory then allocate another cluster
58627a0bc89SDoug Rabson 	 * and chain it onto the end of the file.  There is one exception
58727a0bc89SDoug Rabson 	 * to this.  That is, if the root directory has no more space it
58827a0bc89SDoug Rabson 	 * can NOT be expanded.  extendfile() checks for and fails attempts
58927a0bc89SDoug Rabson 	 * to extend the root directory.  We just return an error in that
59027a0bc89SDoug Rabson 	 * case.
59127a0bc89SDoug Rabson 	 */
592952a6212SJordan K. Hubbard 	if (ddep->de_fndoffset >= ddep->de_FileSize) {
593952a6212SJordan K. Hubbard 		diroffset = ddep->de_fndoffset + sizeof(struct direntry)
594952a6212SJordan K. Hubbard 		    - ddep->de_FileSize;
595952a6212SJordan K. Hubbard 		dirclust = de_clcount(pmp, diroffset);
596952a6212SJordan K. Hubbard 		error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
597952a6212SJordan K. Hubbard 		if (error) {
598952a6212SJordan K. Hubbard 			(void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL);
59927a0bc89SDoug Rabson 			return error;
600952a6212SJordan K. Hubbard 		}
601952a6212SJordan K. Hubbard 
60227a0bc89SDoug Rabson 		/*
60327a0bc89SDoug Rabson 		 * Update the size of the directory
60427a0bc89SDoug Rabson 		 */
605952a6212SJordan K. Hubbard 		ddep->de_FileSize += de_cn2off(pmp, dirclust);
606952a6212SJordan K. Hubbard 	}
607952a6212SJordan K. Hubbard 
60827a0bc89SDoug Rabson 	/*
609952a6212SJordan K. Hubbard 	 * We just read in the cluster with space.  Copy the new directory
61027a0bc89SDoug Rabson 	 * entry in.  Then write it to disk. NOTE:  DOS directories
61127a0bc89SDoug Rabson 	 * do not get smaller as clusters are emptied.
61227a0bc89SDoug Rabson 	 */
613952a6212SJordan K. Hubbard 	error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
614952a6212SJordan K. Hubbard 		       &bn, &dirclust, &blsize);
61527a0bc89SDoug Rabson 	if (error)
61627a0bc89SDoug Rabson 		return error;
617952a6212SJordan K. Hubbard 	diroffset = ddep->de_fndoffset;
618952a6212SJordan K. Hubbard 	if (dirclust != MSDOSFSROOT)
619952a6212SJordan K. Hubbard 		diroffset &= pmp->pm_crbomask;
620952a6212SJordan K. Hubbard 	if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
621952a6212SJordan K. Hubbard 		brelse(bp);
622952a6212SJordan K. Hubbard 		return error;
62327a0bc89SDoug Rabson 	}
624952a6212SJordan K. Hubbard 	ndep = bptoep(pmp, bp, ddep->de_fndoffset);
625952a6212SJordan K. Hubbard 
62627a0bc89SDoug Rabson 	DE_EXTERNALIZE(ndep, dep);
62727a0bc89SDoug Rabson 
62827a0bc89SDoug Rabson 	/*
629952a6212SJordan K. Hubbard 	 * Now write the Win95 long name
630952a6212SJordan K. Hubbard 	 */
631952a6212SJordan K. Hubbard 	if (ddep->de_fndcnt > 0) {
632952a6212SJordan K. Hubbard 		u_int8_t chksum = winChksum(ndep->deName);
633952a6212SJordan K. Hubbard 		const u_char *un = (const u_char *)cnp->cn_nameptr;
634952a6212SJordan K. Hubbard 		int unlen = cnp->cn_namelen;
635952a6212SJordan K. Hubbard 		int cnt = 1;
636952a6212SJordan K. Hubbard 
637952a6212SJordan K. Hubbard 		while (--ddep->de_fndcnt >= 0) {
638952a6212SJordan K. Hubbard 			if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
639952a6212SJordan K. Hubbard 				if ((error = bwrite(bp)) != 0)
640952a6212SJordan K. Hubbard 					return error;
641952a6212SJordan K. Hubbard 
642952a6212SJordan K. Hubbard 				ddep->de_fndoffset -= sizeof(struct direntry);
643952a6212SJordan K. Hubbard 				error = pcbmap(ddep,
644952a6212SJordan K. Hubbard 					       de_cluster(pmp,
645952a6212SJordan K. Hubbard 							  ddep->de_fndoffset),
646952a6212SJordan K. Hubbard 					       &bn, 0, &blsize);
647952a6212SJordan K. Hubbard 				if (error)
648952a6212SJordan K. Hubbard 					return error;
649952a6212SJordan K. Hubbard 
650952a6212SJordan K. Hubbard 				error = bread(pmp->pm_devvp, bn, blsize,
651952a6212SJordan K. Hubbard 					      NOCRED, &bp);
652952a6212SJordan K. Hubbard 				if (error) {
653952a6212SJordan K. Hubbard 					brelse(bp);
654952a6212SJordan K. Hubbard 					return error;
655952a6212SJordan K. Hubbard 				}
656952a6212SJordan K. Hubbard 				ndep = bptoep(pmp, bp, ddep->de_fndoffset);
657952a6212SJordan K. Hubbard 			} else {
658952a6212SJordan K. Hubbard 				ndep--;
659952a6212SJordan K. Hubbard 				ddep->de_fndoffset -= sizeof(struct direntry);
660952a6212SJordan K. Hubbard 			}
66113df76f2SAndrey A. Chernov 			if (!unix2winfn(un, unlen, (struct winentry *)ndep,
66213df76f2SAndrey A. Chernov 					cnt++, chksum,
6637391f611SAndrey A. Chernov 					pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
6647391f611SAndrey A. Chernov 					pmp->pm_u2w))
665952a6212SJordan K. Hubbard 				break;
666952a6212SJordan K. Hubbard 		}
667952a6212SJordan K. Hubbard 	}
668952a6212SJordan K. Hubbard 
669952a6212SJordan K. Hubbard 	if ((error = bwrite(bp)) != 0)
670952a6212SJordan K. Hubbard 		return error;
671952a6212SJordan K. Hubbard 
672952a6212SJordan K. Hubbard 	/*
67327a0bc89SDoug Rabson 	 * If they want us to return with the denode gotten.
67427a0bc89SDoug Rabson 	 */
67527a0bc89SDoug Rabson 	if (depp) {
676952a6212SJordan K. Hubbard 		if (dep->de_Attributes & ATTR_DIRECTORY) {
677952a6212SJordan K. Hubbard 			dirclust = dep->de_StartCluster;
678952a6212SJordan K. Hubbard 			if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
679952a6212SJordan K. Hubbard 				dirclust = MSDOSFSROOT;
680952a6212SJordan K. Hubbard 			if (dirclust == MSDOSFSROOT)
681952a6212SJordan K. Hubbard 				diroffset = MSDOSFSROOT_OFS;
682952a6212SJordan K. Hubbard 			else
683952a6212SJordan K. Hubbard 				diroffset = 0;
68427a0bc89SDoug Rabson 		}
685952a6212SJordan K. Hubbard 		return deget(pmp, dirclust, diroffset, depp);
68627a0bc89SDoug Rabson 	}
687952a6212SJordan K. Hubbard 
68827a0bc89SDoug Rabson 	return 0;
68927a0bc89SDoug Rabson }
69027a0bc89SDoug Rabson 
69127a0bc89SDoug Rabson /*
69227a0bc89SDoug Rabson  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
69327a0bc89SDoug Rabson  * return 0 if not empty or error.
69427a0bc89SDoug Rabson  */
69527a0bc89SDoug Rabson int
69627a0bc89SDoug Rabson dosdirempty(dep)
69727a0bc89SDoug Rabson 	struct denode *dep;
69827a0bc89SDoug Rabson {
699952a6212SJordan K. Hubbard 	int blsize;
70027a0bc89SDoug Rabson 	int error;
70127a0bc89SDoug Rabson 	u_long cn;
70227a0bc89SDoug Rabson 	daddr_t bn;
70327a0bc89SDoug Rabson 	struct buf *bp;
70427a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
70527a0bc89SDoug Rabson 	struct direntry *dentp;
70627a0bc89SDoug Rabson 
70727a0bc89SDoug Rabson 	/*
70827a0bc89SDoug Rabson 	 * Since the filesize field in directory entries for a directory is
70927a0bc89SDoug Rabson 	 * zero, we just have to feel our way through the directory until
71027a0bc89SDoug Rabson 	 * we hit end of file.
71127a0bc89SDoug Rabson 	 */
71227a0bc89SDoug Rabson 	for (cn = 0;; cn++) {
713952a6212SJordan K. Hubbard 		if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
71427a0bc89SDoug Rabson 			if (error == E2BIG)
715952a6212SJordan K. Hubbard 				return (1);	/* it's empty */
716952a6212SJordan K. Hubbard 			return (0);
717952a6212SJordan K. Hubbard 		}
718952a6212SJordan K. Hubbard 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
719952a6212SJordan K. Hubbard 		if (error) {
720952a6212SJordan K. Hubbard 			brelse(bp);
721952a6212SJordan K. Hubbard 			return (0);
722952a6212SJordan K. Hubbard 		}
723952a6212SJordan K. Hubbard 		for (dentp = (struct direntry *)bp->b_data;
724952a6212SJordan K. Hubbard 		     (char *)dentp < bp->b_data + blsize;
725952a6212SJordan K. Hubbard 		     dentp++) {
726952a6212SJordan K. Hubbard 			if (dentp->deName[0] != SLOT_DELETED &&
727952a6212SJordan K. Hubbard 			    (dentp->deAttributes & ATTR_VOLUME) == 0) {
72827a0bc89SDoug Rabson 				/*
72927a0bc89SDoug Rabson 				 * In dos directories an entry whose name
73027a0bc89SDoug Rabson 				 * starts with SLOT_EMPTY (0) starts the
73127a0bc89SDoug Rabson 				 * beginning of the unused part of the
73227a0bc89SDoug Rabson 				 * directory, so we can just return that it
73327a0bc89SDoug Rabson 				 * is empty.
73427a0bc89SDoug Rabson 				 */
73527a0bc89SDoug Rabson 				if (dentp->deName[0] == SLOT_EMPTY) {
73627a0bc89SDoug Rabson 					brelse(bp);
737952a6212SJordan K. Hubbard 					return (1);
73827a0bc89SDoug Rabson 				}
73927a0bc89SDoug Rabson 				/*
74027a0bc89SDoug Rabson 				 * Any names other than "." and ".." in a
74127a0bc89SDoug Rabson 				 * directory mean it is not empty.
74227a0bc89SDoug Rabson 				 */
74327a0bc89SDoug Rabson 				if (bcmp(dentp->deName, ".          ", 11) &&
74427a0bc89SDoug Rabson 				    bcmp(dentp->deName, "..         ", 11)) {
74527a0bc89SDoug Rabson 					brelse(bp);
74627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
747952a6212SJordan K. Hubbard 					printf("dosdirempty(): entry found %02x, %02x\n",
748952a6212SJordan K. Hubbard 					    dentp->deName[0], dentp->deName[1]);
74927a0bc89SDoug Rabson #endif
750952a6212SJordan K. Hubbard 					return (0);	/* not empty */
75127a0bc89SDoug Rabson 				}
75227a0bc89SDoug Rabson 			}
75327a0bc89SDoug Rabson 		}
75427a0bc89SDoug Rabson 		brelse(bp);
75527a0bc89SDoug Rabson 	}
75627a0bc89SDoug Rabson 	/* NOTREACHED */
75727a0bc89SDoug Rabson }
75827a0bc89SDoug Rabson 
75927a0bc89SDoug Rabson /*
76027a0bc89SDoug Rabson  * Check to see if the directory described by target is in some
76127a0bc89SDoug Rabson  * subdirectory of source.  This prevents something like the following from
76227a0bc89SDoug Rabson  * succeeding and leaving a bunch or files and directories orphaned. mv
76327a0bc89SDoug Rabson  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
76427a0bc89SDoug Rabson  *
76527a0bc89SDoug Rabson  * source - the inode for /a/b/c
76627a0bc89SDoug Rabson  * target - the inode for /a/b/c/d/e/f
76727a0bc89SDoug Rabson  *
76827a0bc89SDoug Rabson  * Returns 0 if target is NOT a subdirectory of source.
76927a0bc89SDoug Rabson  * Otherwise returns a non-zero error number.
77027a0bc89SDoug Rabson  * The target inode is always unlocked on return.
77127a0bc89SDoug Rabson  */
77227a0bc89SDoug Rabson int
77327a0bc89SDoug Rabson doscheckpath(source, target)
77427a0bc89SDoug Rabson 	struct denode *source;
77527a0bc89SDoug Rabson 	struct denode *target;
77627a0bc89SDoug Rabson {
77727a0bc89SDoug Rabson 	daddr_t scn;
77827a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
77927a0bc89SDoug Rabson 	struct direntry *ep;
78027a0bc89SDoug Rabson 	struct denode *dep;
78127a0bc89SDoug Rabson 	struct buf *bp = NULL;
78227a0bc89SDoug Rabson 	int error = 0;
78327a0bc89SDoug Rabson 
78427a0bc89SDoug Rabson 	dep = target;
78527a0bc89SDoug Rabson 	if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
78627a0bc89SDoug Rabson 	    (source->de_Attributes & ATTR_DIRECTORY) == 0) {
78727a0bc89SDoug Rabson 		error = ENOTDIR;
78827a0bc89SDoug Rabson 		goto out;
78927a0bc89SDoug Rabson 	}
79027a0bc89SDoug Rabson 	if (dep->de_StartCluster == source->de_StartCluster) {
79127a0bc89SDoug Rabson 		error = EEXIST;
79227a0bc89SDoug Rabson 		goto out;
79327a0bc89SDoug Rabson 	}
79427a0bc89SDoug Rabson 	if (dep->de_StartCluster == MSDOSFSROOT)
79527a0bc89SDoug Rabson 		goto out;
796952a6212SJordan K. Hubbard 	pmp = dep->de_pmp;
797952a6212SJordan K. Hubbard #ifdef	DIAGNOSTIC
798952a6212SJordan K. Hubbard 	if (pmp != source->de_pmp)
799952a6212SJordan K. Hubbard 		panic("doscheckpath: source and target on different filesystems");
800952a6212SJordan K. Hubbard #endif
801952a6212SJordan K. Hubbard 	if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
802952a6212SJordan K. Hubbard 		goto out;
803952a6212SJordan K. Hubbard 
80427a0bc89SDoug Rabson 	for (;;) {
80527a0bc89SDoug Rabson 		if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
80627a0bc89SDoug Rabson 			error = ENOTDIR;
807952a6212SJordan K. Hubbard 			break;
80827a0bc89SDoug Rabson 		}
80927a0bc89SDoug Rabson 		scn = dep->de_StartCluster;
81027a0bc89SDoug Rabson 		error = bread(pmp->pm_devvp, cntobn(pmp, scn),
81127a0bc89SDoug Rabson 			      pmp->pm_bpcluster, NOCRED, &bp);
812952a6212SJordan K. Hubbard 		if (error)
81327a0bc89SDoug Rabson 			break;
814952a6212SJordan K. Hubbard 
81527a0bc89SDoug Rabson 		ep = (struct direntry *) bp->b_data + 1;
81627a0bc89SDoug Rabson 		if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
81727a0bc89SDoug Rabson 		    bcmp(ep->deName, "..         ", 11) != 0) {
81827a0bc89SDoug Rabson 			error = ENOTDIR;
81927a0bc89SDoug Rabson 			break;
82027a0bc89SDoug Rabson 		}
82127a0bc89SDoug Rabson 		scn = getushort(ep->deStartCluster);
822952a6212SJordan K. Hubbard 		if (FAT32(pmp))
823952a6212SJordan K. Hubbard 			scn |= getushort(ep->deHighClust) << 16;
824952a6212SJordan K. Hubbard 
82527a0bc89SDoug Rabson 		if (scn == source->de_StartCluster) {
82627a0bc89SDoug Rabson 			error = EINVAL;
82727a0bc89SDoug Rabson 			break;
82827a0bc89SDoug Rabson 		}
82927a0bc89SDoug Rabson 		if (scn == MSDOSFSROOT)
83027a0bc89SDoug Rabson 			break;
831952a6212SJordan K. Hubbard 		if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
832952a6212SJordan K. Hubbard 			/*
833952a6212SJordan K. Hubbard 			 * scn should be 0 in this case,
834952a6212SJordan K. Hubbard 			 * but we silently ignore the error.
835952a6212SJordan K. Hubbard 			 */
836952a6212SJordan K. Hubbard 			break;
837952a6212SJordan K. Hubbard 		}
838952a6212SJordan K. Hubbard 
83927a0bc89SDoug Rabson 		vput(DETOV(dep));
84027a0bc89SDoug Rabson 		brelse(bp);
84127a0bc89SDoug Rabson 		bp = NULL;
842952a6212SJordan K. Hubbard 		/* NOTE: deget() clears dep on error */
843952a6212SJordan K. Hubbard 		if ((error = deget(pmp, scn, 0, &dep)) != 0)
84427a0bc89SDoug Rabson 			break;
84527a0bc89SDoug Rabson 	}
84627a0bc89SDoug Rabson out:;
84727a0bc89SDoug Rabson 	if (bp)
84827a0bc89SDoug Rabson 		brelse(bp);
84927a0bc89SDoug Rabson 	if (error == ENOTDIR)
85027a0bc89SDoug Rabson 		printf("doscheckpath(): .. not a directory?\n");
85127a0bc89SDoug Rabson 	if (dep != NULL)
85227a0bc89SDoug Rabson 		vput(DETOV(dep));
853952a6212SJordan K. Hubbard 	return (error);
85427a0bc89SDoug Rabson }
85527a0bc89SDoug Rabson 
85627a0bc89SDoug Rabson /*
85727a0bc89SDoug Rabson  * Read in the disk block containing the directory entry (dirclu, dirofs)
85827a0bc89SDoug Rabson  * and return the address of the buf header, and the address of the
85927a0bc89SDoug Rabson  * directory entry within the block.
86027a0bc89SDoug Rabson  */
86127a0bc89SDoug Rabson int
862952a6212SJordan K. Hubbard readep(pmp, dirclust, diroffset, bpp, epp)
86327a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
864952a6212SJordan K. Hubbard 	u_long dirclust, diroffset;
86527a0bc89SDoug Rabson 	struct buf **bpp;
86627a0bc89SDoug Rabson 	struct direntry **epp;
86727a0bc89SDoug Rabson {
86827a0bc89SDoug Rabson 	int error;
86927a0bc89SDoug Rabson 	daddr_t bn;
870952a6212SJordan K. Hubbard 	int blsize;
87127a0bc89SDoug Rabson 
872952a6212SJordan K. Hubbard 	blsize = pmp->pm_bpcluster;
873952a6212SJordan K. Hubbard 	if (dirclust == MSDOSFSROOT
874952a6212SJordan K. Hubbard 	    && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
875952a6212SJordan K. Hubbard 		blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
876952a6212SJordan K. Hubbard 	bn = detobn(pmp, dirclust, diroffset);
877952a6212SJordan K. Hubbard 	if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
878952a6212SJordan K. Hubbard 		brelse(*bpp);
87927a0bc89SDoug Rabson 		*bpp = NULL;
880952a6212SJordan K. Hubbard 		return (error);
88127a0bc89SDoug Rabson 	}
88227a0bc89SDoug Rabson 	if (epp)
883952a6212SJordan K. Hubbard 		*epp = bptoep(pmp, *bpp, diroffset);
884952a6212SJordan K. Hubbard 	return (0);
88527a0bc89SDoug Rabson }
88627a0bc89SDoug Rabson 
88727a0bc89SDoug Rabson /*
88827a0bc89SDoug Rabson  * Read in the disk block containing the directory entry dep came from and
88927a0bc89SDoug Rabson  * return the address of the buf header, and the address of the directory
89027a0bc89SDoug Rabson  * entry within the block.
89127a0bc89SDoug Rabson  */
89227a0bc89SDoug Rabson int
89327a0bc89SDoug Rabson readde(dep, bpp, epp)
89427a0bc89SDoug Rabson 	struct denode *dep;
89527a0bc89SDoug Rabson 	struct buf **bpp;
89627a0bc89SDoug Rabson 	struct direntry **epp;
89727a0bc89SDoug Rabson {
898952a6212SJordan K. Hubbard 
899952a6212SJordan K. Hubbard 	return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
900952a6212SJordan K. Hubbard 	    bpp, epp));
901952a6212SJordan K. Hubbard }
902952a6212SJordan K. Hubbard 
903952a6212SJordan K. Hubbard /*
904952a6212SJordan K. Hubbard  * Remove a directory entry. At this point the file represented by the
905952a6212SJordan K. Hubbard  * directory entry to be removed is still full length until noone has it
906952a6212SJordan K. Hubbard  * open.  When the file no longer being used msdosfs_inactive() is called
907952a6212SJordan K. Hubbard  * and will truncate the file to 0 length.  When the vnode containing the
908952a6212SJordan K. Hubbard  * denode is needed for some other purpose by VFS it will call
909952a6212SJordan K. Hubbard  * msdosfs_reclaim() which will remove the denode from the denode cache.
910952a6212SJordan K. Hubbard  */
911952a6212SJordan K. Hubbard int
912952a6212SJordan K. Hubbard removede(pdep, dep)
913952a6212SJordan K. Hubbard 	struct denode *pdep;	/* directory where the entry is removed */
914952a6212SJordan K. Hubbard 	struct denode *dep;	/* file to be removed */
915952a6212SJordan K. Hubbard {
916952a6212SJordan K. Hubbard 	int error;
917952a6212SJordan K. Hubbard 	struct direntry *ep;
918952a6212SJordan K. Hubbard 	struct buf *bp;
919952a6212SJordan K. Hubbard 	daddr_t bn;
920952a6212SJordan K. Hubbard 	int blsize;
921952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = pdep->de_pmp;
922952a6212SJordan K. Hubbard 	u_long offset = pdep->de_fndoffset;
923952a6212SJordan K. Hubbard 
924952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG
925952a6212SJordan K. Hubbard 	printf("removede(): filename %s, dep %p, offset %08lx\n",
926952a6212SJordan K. Hubbard 	    dep->de_Name, dep, offset);
927952a6212SJordan K. Hubbard #endif
928952a6212SJordan K. Hubbard 
929952a6212SJordan K. Hubbard 	dep->de_refcnt--;
930952a6212SJordan K. Hubbard 	offset += sizeof(struct direntry);
931952a6212SJordan K. Hubbard 	do {
932952a6212SJordan K. Hubbard 		offset -= sizeof(struct direntry);
933952a6212SJordan K. Hubbard 		error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
934952a6212SJordan K. Hubbard 		if (error)
935952a6212SJordan K. Hubbard 			return error;
936952a6212SJordan K. Hubbard 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
937952a6212SJordan K. Hubbard 		if (error) {
938952a6212SJordan K. Hubbard 			brelse(bp);
939952a6212SJordan K. Hubbard 			return error;
940952a6212SJordan K. Hubbard 		}
941952a6212SJordan K. Hubbard 		ep = bptoep(pmp, bp, offset);
942952a6212SJordan K. Hubbard 		/*
943952a6212SJordan K. Hubbard 		 * Check whether, if we came here the second time, i.e.
944952a6212SJordan K. Hubbard 		 * when underflowing into the previous block, the last
945952a6212SJordan K. Hubbard 		 * entry in this block is a longfilename entry, too.
946952a6212SJordan K. Hubbard 		 */
947952a6212SJordan K. Hubbard 		if (ep->deAttributes != ATTR_WIN95
948952a6212SJordan K. Hubbard 		    && offset != pdep->de_fndoffset) {
949952a6212SJordan K. Hubbard 			brelse(bp);
950952a6212SJordan K. Hubbard 			break;
951952a6212SJordan K. Hubbard 		}
952952a6212SJordan K. Hubbard 		offset += sizeof(struct direntry);
953952a6212SJordan K. Hubbard 		while (1) {
954952a6212SJordan K. Hubbard 			/*
955952a6212SJordan K. Hubbard 			 * We are a bit agressive here in that we delete any Win95
956952a6212SJordan K. Hubbard 			 * entries preceding this entry, not just the ones we "own".
957952a6212SJordan K. Hubbard 			 * Since these presumably aren't valid anyway,
958952a6212SJordan K. Hubbard 			 * there should be no harm.
959952a6212SJordan K. Hubbard 			 */
960952a6212SJordan K. Hubbard 			offset -= sizeof(struct direntry);
961952a6212SJordan K. Hubbard 			ep--->deName[0] = SLOT_DELETED;
962952a6212SJordan K. Hubbard 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
963952a6212SJordan K. Hubbard 			    || !(offset & pmp->pm_crbomask)
964952a6212SJordan K. Hubbard 			    || ep->deAttributes != ATTR_WIN95)
965952a6212SJordan K. Hubbard 				break;
966952a6212SJordan K. Hubbard 		}
967952a6212SJordan K. Hubbard 		if ((error = bwrite(bp)) != 0)
968952a6212SJordan K. Hubbard 			return error;
969952a6212SJordan K. Hubbard 	} while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
970952a6212SJordan K. Hubbard 	    && !(offset & pmp->pm_crbomask)
971952a6212SJordan K. Hubbard 	    && offset);
972952a6212SJordan K. Hubbard 	return 0;
973952a6212SJordan K. Hubbard }
974952a6212SJordan K. Hubbard 
975952a6212SJordan K. Hubbard /*
976952a6212SJordan K. Hubbard  * Create a unique DOS name in dvp
977952a6212SJordan K. Hubbard  */
978952a6212SJordan K. Hubbard int
979952a6212SJordan K. Hubbard uniqdosname(dep, cnp, cp)
980952a6212SJordan K. Hubbard 	struct denode *dep;
981952a6212SJordan K. Hubbard 	struct componentname *cnp;
982952a6212SJordan K. Hubbard 	u_char *cp;
983952a6212SJordan K. Hubbard {
984952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = dep->de_pmp;
985952a6212SJordan K. Hubbard 	struct direntry *dentp;
986952a6212SJordan K. Hubbard 	int gen;
987952a6212SJordan K. Hubbard 	int blsize;
988952a6212SJordan K. Hubbard 	u_long cn;
989952a6212SJordan K. Hubbard 	daddr_t bn;
990952a6212SJordan K. Hubbard 	struct buf *bp;
991952a6212SJordan K. Hubbard 	int error;
992952a6212SJordan K. Hubbard 
993af9f1d50SDmitrij Tejblum 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
994011cdb57SDmitrij Tejblum 		return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
995011cdb57SDmitrij Tejblum 		    cnp->cn_namelen, 0,
996011cdb57SDmitrij Tejblum 		    pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
997011cdb57SDmitrij Tejblum 		    pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu) ?
998011cdb57SDmitrij Tejblum 		    0 : EINVAL);
999011cdb57SDmitrij Tejblum 
1000952a6212SJordan K. Hubbard 	for (gen = 1;; gen++) {
1001952a6212SJordan K. Hubbard 		/*
1002952a6212SJordan K. Hubbard 		 * Generate DOS name with generation number
1003952a6212SJordan K. Hubbard 		 */
1004952a6212SJordan K. Hubbard 		if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
10057391f611SAndrey A. Chernov 		    cnp->cn_namelen, gen,
10067391f611SAndrey A. Chernov 		    pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
10077391f611SAndrey A. Chernov 		    pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu))
1008952a6212SJordan K. Hubbard 			return gen == 1 ? EINVAL : EEXIST;
1009952a6212SJordan K. Hubbard 
1010952a6212SJordan K. Hubbard 		/*
1011952a6212SJordan K. Hubbard 		 * Now look for a dir entry with this exact name
1012952a6212SJordan K. Hubbard 		 */
1013952a6212SJordan K. Hubbard 		for (cn = error = 0; !error; cn++) {
1014952a6212SJordan K. Hubbard 			if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1015952a6212SJordan K. Hubbard 				if (error == E2BIG)	/* EOF reached and not found */
1016952a6212SJordan K. Hubbard 					return 0;
1017952a6212SJordan K. Hubbard 				return error;
1018952a6212SJordan K. Hubbard 			}
1019952a6212SJordan K. Hubbard 			error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1020952a6212SJordan K. Hubbard 			if (error) {
1021952a6212SJordan K. Hubbard 				brelse(bp);
1022952a6212SJordan K. Hubbard 				return error;
1023952a6212SJordan K. Hubbard 			}
1024952a6212SJordan K. Hubbard 			for (dentp = (struct direntry *)bp->b_data;
1025952a6212SJordan K. Hubbard 			     (char *)dentp < bp->b_data + blsize;
1026952a6212SJordan K. Hubbard 			     dentp++) {
1027952a6212SJordan K. Hubbard 				if (dentp->deName[0] == SLOT_EMPTY) {
1028952a6212SJordan K. Hubbard 					/*
1029952a6212SJordan K. Hubbard 					 * Last used entry and not found
1030952a6212SJordan K. Hubbard 					 */
1031952a6212SJordan K. Hubbard 					brelse(bp);
1032952a6212SJordan K. Hubbard 					return 0;
1033952a6212SJordan K. Hubbard 				}
1034952a6212SJordan K. Hubbard 				/*
1035952a6212SJordan K. Hubbard 				 * Ignore volume labels and Win95 entries
1036952a6212SJordan K. Hubbard 				 */
1037952a6212SJordan K. Hubbard 				if (dentp->deAttributes & ATTR_VOLUME)
1038952a6212SJordan K. Hubbard 					continue;
1039952a6212SJordan K. Hubbard 				if (!bcmp(dentp->deName, cp, 11)) {
1040952a6212SJordan K. Hubbard 					error = EEXIST;
1041952a6212SJordan K. Hubbard 					break;
1042952a6212SJordan K. Hubbard 				}
1043952a6212SJordan K. Hubbard 			}
1044952a6212SJordan K. Hubbard 			brelse(bp);
1045952a6212SJordan K. Hubbard 		}
1046952a6212SJordan K. Hubbard 	}
1047952a6212SJordan K. Hubbard }
1048952a6212SJordan K. Hubbard 
1049952a6212SJordan K. Hubbard /*
1050952a6212SJordan K. Hubbard  * Find any Win'95 long filename entry in directory dep
1051952a6212SJordan K. Hubbard  */
1052952a6212SJordan K. Hubbard int
1053952a6212SJordan K. Hubbard findwin95(dep)
1054952a6212SJordan K. Hubbard 	struct denode *dep;
1055952a6212SJordan K. Hubbard {
1056952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = dep->de_pmp;
1057952a6212SJordan K. Hubbard 	struct direntry *dentp;
105857081f7bSDmitrij Tejblum 	int blsize, win95;
1059952a6212SJordan K. Hubbard 	u_long cn;
1060952a6212SJordan K. Hubbard 	daddr_t bn;
1061952a6212SJordan K. Hubbard 	struct buf *bp;
1062952a6212SJordan K. Hubbard 
106357081f7bSDmitrij Tejblum 	win95 = 1;
1064952a6212SJordan K. Hubbard 	/*
1065952a6212SJordan K. Hubbard 	 * Read through the directory looking for Win'95 entries
1066952a6212SJordan K. Hubbard 	 * Note: Error currently handled just as EOF			XXX
1067952a6212SJordan K. Hubbard 	 */
1068952a6212SJordan K. Hubbard 	for (cn = 0;; cn++) {
1069952a6212SJordan K. Hubbard 		if (pcbmap(dep, cn, &bn, 0, &blsize))
107057081f7bSDmitrij Tejblum 			return (win95);
1071952a6212SJordan K. Hubbard 		if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) {
1072952a6212SJordan K. Hubbard 			brelse(bp);
107357081f7bSDmitrij Tejblum 			return (win95);
1074952a6212SJordan K. Hubbard 		}
1075952a6212SJordan K. Hubbard 		for (dentp = (struct direntry *)bp->b_data;
1076952a6212SJordan K. Hubbard 		     (char *)dentp < bp->b_data + blsize;
1077952a6212SJordan K. Hubbard 		     dentp++) {
1078952a6212SJordan K. Hubbard 			if (dentp->deName[0] == SLOT_EMPTY) {
1079952a6212SJordan K. Hubbard 				/*
1080952a6212SJordan K. Hubbard 				 * Last used entry and not found
1081952a6212SJordan K. Hubbard 				 */
1082952a6212SJordan K. Hubbard 				brelse(bp);
108357081f7bSDmitrij Tejblum 				return (win95);
1084952a6212SJordan K. Hubbard 			}
1085952a6212SJordan K. Hubbard 			if (dentp->deName[0] == SLOT_DELETED) {
1086952a6212SJordan K. Hubbard 				/*
1087952a6212SJordan K. Hubbard 				 * Ignore deleted files
1088952a6212SJordan K. Hubbard 				 * Note: might be an indication of Win'95 anyway	XXX
1089952a6212SJordan K. Hubbard 				 */
1090952a6212SJordan K. Hubbard 				continue;
1091952a6212SJordan K. Hubbard 			}
1092952a6212SJordan K. Hubbard 			if (dentp->deAttributes == ATTR_WIN95) {
1093952a6212SJordan K. Hubbard 				brelse(bp);
1094952a6212SJordan K. Hubbard 				return 1;
1095952a6212SJordan K. Hubbard 			}
109657081f7bSDmitrij Tejblum 			win95 = 0;
1097952a6212SJordan K. Hubbard 		}
1098952a6212SJordan K. Hubbard 		brelse(bp);
1099952a6212SJordan K. Hubbard 	}
110027a0bc89SDoug Rabson }
1101