xref: /freebsd/sys/fs/msdosfs/msdosfs_lookup.c (revision 95d42526e92cb2a9842d71d3c585aabf32da7534)
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 /*-
5d63027b6SPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
6d63027b6SPedro F. Giffuni  *
7952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
927a0bc89SDoug Rabson  * All rights reserved.
1027a0bc89SDoug Rabson  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
1127a0bc89SDoug Rabson  *
1227a0bc89SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1327a0bc89SDoug Rabson  * modification, are permitted provided that the following conditions
1427a0bc89SDoug Rabson  * are met:
1527a0bc89SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1627a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1727a0bc89SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1827a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1927a0bc89SDoug Rabson  *    documentation and/or other materials provided with the distribution.
2027a0bc89SDoug Rabson  * 3. All advertising materials mentioning features or use of this software
2127a0bc89SDoug Rabson  *    must display the following acknowledgement:
2227a0bc89SDoug Rabson  *	This product includes software developed by TooLs GmbH.
2327a0bc89SDoug Rabson  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2427a0bc89SDoug Rabson  *    derived from this software without specific prior written permission.
2527a0bc89SDoug Rabson  *
2627a0bc89SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2727a0bc89SDoug Rabson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2827a0bc89SDoug Rabson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2927a0bc89SDoug Rabson  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3027a0bc89SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3127a0bc89SDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3227a0bc89SDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3327a0bc89SDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3427a0bc89SDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3527a0bc89SDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3627a0bc89SDoug Rabson  */
37d167cf6fSWarner Losh /*-
3827a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3927a0bc89SDoug Rabson  *
4027a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
4127a0bc89SDoug Rabson  * it, and don't remove this notice.
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * This software is provided "as is".
4427a0bc89SDoug Rabson  *
4527a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4627a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4727a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4827a0bc89SDoug Rabson  * any damages caused by this software.
4927a0bc89SDoug Rabson  *
5027a0bc89SDoug Rabson  * October 1992
5127a0bc89SDoug Rabson  */
5227a0bc89SDoug Rabson 
5327a0bc89SDoug Rabson #include <sys/param.h>
54919d1ea2SPeter Wemm #include <sys/systm.h>
5527a0bc89SDoug Rabson #include <sys/buf.h>
5627a0bc89SDoug Rabson #include <sys/mount.h>
57a878a31cSBruce Evans #include <sys/namei.h>
58a878a31cSBruce Evans #include <sys/vnode.h>
5927a0bc89SDoug Rabson 
601166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h>
611166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h>
621166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h>
631166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h>
64a878a31cSBruce Evans #include <fs/msdosfs/msdosfsmount.h>
6527a0bc89SDoug Rabson 
66db811dd7SKonstantin Belousov int
67db811dd7SKonstantin Belousov msdosfs_lookup(struct vop_cachedlookup_args *ap)
68db811dd7SKonstantin Belousov {
69db811dd7SKonstantin Belousov 
70*95d42526SKonstantin Belousov 	return (msdosfs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL,
71*95d42526SKonstantin Belousov 	    NULL));
72db811dd7SKonstantin Belousov }
73db811dd7SKonstantin Belousov 
74a6945216SKonstantin Belousov struct deget_dotdot {
75a6945216SKonstantin Belousov 	u_long cluster;
76a6945216SKonstantin Belousov 	int blkoff;
77a6945216SKonstantin Belousov };
78a6945216SKonstantin Belousov 
79a6945216SKonstantin Belousov static int
80a6945216SKonstantin Belousov msdosfs_deget_dotdot(struct mount *mp, void *arg, int lkflags,
81a6945216SKonstantin Belousov     struct vnode **rvp)
82a6945216SKonstantin Belousov {
83a6945216SKonstantin Belousov 	struct deget_dotdot *dd_arg;
84a6945216SKonstantin Belousov 	struct denode *rdp;
85a6945216SKonstantin Belousov 	struct msdosfsmount *pmp;
86a6945216SKonstantin Belousov 	int error;
87a6945216SKonstantin Belousov 
88a6945216SKonstantin Belousov 	pmp = VFSTOMSDOSFS(mp);
89a6945216SKonstantin Belousov 	dd_arg = arg;
90ae7e8a02SKonstantin Belousov 	error = deget(pmp, dd_arg->cluster, dd_arg->blkoff,
91ae7e8a02SKonstantin Belousov 	    LK_EXCLUSIVE, &rdp);
92a6945216SKonstantin Belousov 	if (error == 0)
93a6945216SKonstantin Belousov 		*rvp = DETOV(rdp);
94a6945216SKonstantin Belousov 	return (error);
95a6945216SKonstantin Belousov }
96a6945216SKonstantin Belousov 
9727a0bc89SDoug Rabson /*
9827a0bc89SDoug Rabson  * When we search a directory the blocks containing directory entries are
9927a0bc89SDoug Rabson  * read and examined.  The directory entries contain information that would
10027a0bc89SDoug Rabson  * normally be in the inode of a unix filesystem.  This means that some of
10127a0bc89SDoug Rabson  * a directory's contents may also be in memory resident denodes (sort of
10227a0bc89SDoug Rabson  * an inode).  This can cause problems if we are searching while some other
10327a0bc89SDoug Rabson  * process is modifying a directory.  To prevent one process from accessing
10427a0bc89SDoug Rabson  * incompletely modified directory information we depend upon being the
105b84d2921SPoul-Henning Kamp  * sole owner of a directory block.  bread/brelse provide this service.
10627a0bc89SDoug Rabson  * This being the case, when a process modifies a directory it must first
10727a0bc89SDoug Rabson  * acquire the disk block that contains the directory entry to be modified.
10827a0bc89SDoug Rabson  * Then update the disk block and the denode, and then write the disk block
10927a0bc89SDoug Rabson  * out to disk.  This way disk blocks containing directory entries and in
11027a0bc89SDoug Rabson  * memory denode's will be in synch.
11127a0bc89SDoug Rabson  */
11292d4e088SKonstantin Belousov int
113*95d42526SKonstantin Belousov msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname
114*95d42526SKonstantin Belousov     *cnp, daddr_t *scnp, u_long *blkoffp)
11527a0bc89SDoug Rabson {
116c2819440SBruce Evans 	struct mbnambuf nb;
11727a0bc89SDoug Rabson 	daddr_t bn;
11827a0bc89SDoug Rabson 	int error;
119952a6212SJordan K. Hubbard 	int slotcount;
120952a6212SJordan K. Hubbard 	int slotoffset = 0;
12127a0bc89SDoug Rabson 	int frcn;
12227a0bc89SDoug Rabson 	u_long cluster;
123*95d42526SKonstantin Belousov 	u_long blkoff;
12427a0bc89SDoug Rabson 	int diroff;
125952a6212SJordan K. Hubbard 	int blsize;
12627a0bc89SDoug Rabson 	int isadir;		/* ~0 if found direntry is a directory	 */
127*95d42526SKonstantin Belousov 	daddr_t scn;		/* starting cluster number		 */
12827a0bc89SDoug Rabson 	struct vnode *pdp;
12927a0bc89SDoug Rabson 	struct denode *dp;
13027a0bc89SDoug Rabson 	struct denode *tdp;
13127a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
132f7a3729cSKevin Lo 	struct buf *bp = NULL;
13327a0bc89SDoug Rabson 	struct direntry *dep = NULL;
134a6945216SKonstantin Belousov 	struct deget_dotdot dd_arg;
13527a0bc89SDoug Rabson 	u_char dosfilename[12];
13627a0bc89SDoug Rabson 	int flags = cnp->cn_flags;
13727a0bc89SDoug Rabson 	int nameiop = cnp->cn_nameiop;
13869a36f24SMike Smith 	int unlen;
13923c53312SEd Maste 	uint64_t inode1;
14027a0bc89SDoug Rabson 
141952a6212SJordan K. Hubbard 	int wincnt = 1;
1426a5bf04aSTim J. Robbins 	int chksum = -1, chksum_ok;
143952a6212SJordan K. Hubbard 	int olddos = 1;
144952a6212SJordan K. Hubbard 
14527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
14627a0bc89SDoug Rabson 	printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
14727a0bc89SDoug Rabson #endif
14827a0bc89SDoug Rabson 	dp = VTODE(vdp);
14927a0bc89SDoug Rabson 	pmp = dp->de_pmp;
15027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
151952a6212SJordan K. Hubbard 	printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
15227a0bc89SDoug Rabson 	    vdp, dp, dp->de_Attributes);
15327a0bc89SDoug Rabson #endif
15427a0bc89SDoug Rabson 
155db811dd7SKonstantin Belousov  restart:
15684caee6bSKonstantin Belousov 	if (vpp != NULL)
15784caee6bSKonstantin Belousov 		*vpp = NULL;
15827a0bc89SDoug Rabson 	/*
15927a0bc89SDoug Rabson 	 * If they are going after the . or .. entry in the root directory,
16027a0bc89SDoug Rabson 	 * they won't find it.  DOS filesystems don't have them in the root
16127a0bc89SDoug Rabson 	 * directory.  So, we fake it. deget() is in on this scam too.
16227a0bc89SDoug Rabson 	 */
163e6e370a7SJeff Roberson 	if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
16427a0bc89SDoug Rabson 	    (cnp->cn_namelen == 1 ||
16527a0bc89SDoug Rabson 		(cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
16627a0bc89SDoug Rabson 		isadir = ATTR_DIRECTORY;
16727a0bc89SDoug Rabson 		scn = MSDOSFSROOT;
16827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
16927a0bc89SDoug Rabson 		printf("msdosfs_lookup(): looking for . or .. in root directory\n");
17027a0bc89SDoug Rabson #endif
17127a0bc89SDoug Rabson 		cluster = MSDOSFSROOT;
172952a6212SJordan K. Hubbard 		blkoff = MSDOSFSROOT_OFS;
17327a0bc89SDoug Rabson 		goto foundroot;
17427a0bc89SDoug Rabson 	}
17527a0bc89SDoug Rabson 
176952a6212SJordan K. Hubbard 	switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
177c4f02a89SMax Khon 	    cnp->cn_namelen, 0, pmp)) {
178952a6212SJordan K. Hubbard 	case 0:
179952a6212SJordan K. Hubbard 		return (EINVAL);
180952a6212SJordan K. Hubbard 	case 1:
181952a6212SJordan K. Hubbard 		break;
182952a6212SJordan K. Hubbard 	case 2:
183952a6212SJordan K. Hubbard 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
184c4f02a89SMax Khon 		    cnp->cn_namelen, pmp) + 1;
185952a6212SJordan K. Hubbard 		break;
186952a6212SJordan K. Hubbard 	case 3:
187952a6212SJordan K. Hubbard 		olddos = 0;
188952a6212SJordan K. Hubbard 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
189c4f02a89SMax Khon 		    cnp->cn_namelen, pmp) + 1;
190952a6212SJordan K. Hubbard 		break;
19127a0bc89SDoug Rabson 	}
192011cdb57SDmitrij Tejblum 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
193952a6212SJordan K. Hubbard 		wincnt = 1;
194011cdb57SDmitrij Tejblum 		olddos = 1;
195011cdb57SDmitrij Tejblum 	}
19669a36f24SMike Smith 	unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
19727a0bc89SDoug Rabson 
198952a6212SJordan K. Hubbard 	/*
199952a6212SJordan K. Hubbard 	 * Suppress search for slots unless creating
200952a6212SJordan K. Hubbard 	 * file and at end of pathname, in which case
201952a6212SJordan K. Hubbard 	 * we watch for a place to put the new file in
202952a6212SJordan K. Hubbard 	 * case it doesn't already exist.
203952a6212SJordan K. Hubbard 	 */
204952a6212SJordan K. Hubbard 	slotcount = wincnt;
205952a6212SJordan K. Hubbard 	if ((nameiop == CREATE || nameiop == RENAME) &&
206952a6212SJordan K. Hubbard 	    (flags & ISLASTCN))
207952a6212SJordan K. Hubbard 		slotcount = 0;
208952a6212SJordan K. Hubbard 
20927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
210952a6212SJordan K. Hubbard 	printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
21127a0bc89SDoug Rabson 	    dosfilename, cnp->cn_namelen);
21227a0bc89SDoug Rabson #endif
21327a0bc89SDoug Rabson 	/*
21427a0bc89SDoug Rabson 	 * Search the directory pointed at by vdp for the name pointed at
21527a0bc89SDoug Rabson 	 * by cnp->cn_nameptr.
21627a0bc89SDoug Rabson 	 */
21727a0bc89SDoug Rabson 	tdp = NULL;
218c2819440SBruce Evans 	mbnambuf_init(&nb);
21927a0bc89SDoug Rabson 	/*
22027a0bc89SDoug Rabson 	 * The outer loop ranges over the clusters that make up the
22127a0bc89SDoug Rabson 	 * directory.  Note that the root directory is different from all
22227a0bc89SDoug Rabson 	 * other directories.  It has a fixed number of blocks that are not
22327a0bc89SDoug Rabson 	 * part of the pool of allocatable clusters.  So, we treat it a
22427a0bc89SDoug Rabson 	 * little differently. The root directory starts at "cluster" 0.
22527a0bc89SDoug Rabson 	 */
226952a6212SJordan K. Hubbard 	diroff = 0;
22727a0bc89SDoug Rabson 	for (frcn = 0;; frcn++) {
228952a6212SJordan K. Hubbard 		error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
229c3c6d51eSPoul-Henning Kamp 		if (error) {
23027a0bc89SDoug Rabson 			if (error == E2BIG)
23127a0bc89SDoug Rabson 				break;
232952a6212SJordan K. Hubbard 			return (error);
23327a0bc89SDoug Rabson 		}
234952a6212SJordan K. Hubbard 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
235952a6212SJordan K. Hubbard 		if (error) {
236952a6212SJordan K. Hubbard 			return (error);
237952a6212SJordan K. Hubbard 		}
238952a6212SJordan K. Hubbard 		for (blkoff = 0; blkoff < blsize;
239952a6212SJordan K. Hubbard 		     blkoff += sizeof(struct direntry),
240952a6212SJordan K. Hubbard 		     diroff += sizeof(struct direntry)) {
241952a6212SJordan K. Hubbard 			dep = (struct direntry *)(bp->b_data + blkoff);
24227a0bc89SDoug Rabson 			/*
24327a0bc89SDoug Rabson 			 * If the slot is empty and we are still looking
24427a0bc89SDoug Rabson 			 * for an empty then remember this one.  If the
24527a0bc89SDoug Rabson 			 * slot is not empty then check to see if it
24627a0bc89SDoug Rabson 			 * matches what we are looking for.  If the slot
24727a0bc89SDoug Rabson 			 * has never been filled with anything, then the
24827a0bc89SDoug Rabson 			 * remainder of the directory has never been used,
24927a0bc89SDoug Rabson 			 * so there is no point in searching it.
25027a0bc89SDoug Rabson 			 */
25127a0bc89SDoug Rabson 			if (dep->deName[0] == SLOT_EMPTY ||
25227a0bc89SDoug Rabson 			    dep->deName[0] == SLOT_DELETED) {
253952a6212SJordan K. Hubbard 				/*
254952a6212SJordan K. Hubbard 				 * Drop memory of previous long matches
255952a6212SJordan K. Hubbard 				 */
256952a6212SJordan K. Hubbard 				chksum = -1;
257c2819440SBruce Evans 				mbnambuf_init(&nb);
258952a6212SJordan K. Hubbard 
259952a6212SJordan K. Hubbard 				if (slotcount < wincnt) {
260952a6212SJordan K. Hubbard 					slotcount++;
26127a0bc89SDoug Rabson 					slotoffset = diroff;
26227a0bc89SDoug Rabson 				}
26327a0bc89SDoug Rabson 				if (dep->deName[0] == SLOT_EMPTY) {
26427a0bc89SDoug Rabson 					brelse(bp);
26527a0bc89SDoug Rabson 					goto notfound;
26627a0bc89SDoug Rabson 				}
26727a0bc89SDoug Rabson 			} else {
26827a0bc89SDoug Rabson 				/*
269952a6212SJordan K. Hubbard 				 * If there wasn't enough space for our winentries,
270952a6212SJordan K. Hubbard 				 * forget about the empty space
271952a6212SJordan K. Hubbard 				 */
272952a6212SJordan K. Hubbard 				if (slotcount < wincnt)
273952a6212SJordan K. Hubbard 					slotcount = 0;
274952a6212SJordan K. Hubbard 
275952a6212SJordan K. Hubbard 				/*
276952a6212SJordan K. Hubbard 				 * Check for Win95 long filename entry
277952a6212SJordan K. Hubbard 				 */
278952a6212SJordan K. Hubbard 				if (dep->deAttributes == ATTR_WIN95) {
279952a6212SJordan K. Hubbard 					if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
280952a6212SJordan K. Hubbard 						continue;
281952a6212SJordan K. Hubbard 
282c2819440SBruce Evans 					chksum = win2unixfn(&nb,
283c2819440SBruce Evans 					    (struct winentry *)dep, chksum,
284c4f02a89SMax Khon 					    pmp);
285c4f02a89SMax Khon 					continue;
286c4f02a89SMax Khon 				}
287c4f02a89SMax Khon 
288c2819440SBruce Evans 				chksum = winChkName(&nb,
289c2819440SBruce Evans 				    (const u_char *)cnp->cn_nameptr, unlen,
290c2819440SBruce Evans 				    chksum, pmp);
291c4f02a89SMax Khon 				if (chksum == -2) {
292c4f02a89SMax Khon 					chksum = -1;
293952a6212SJordan K. Hubbard 					continue;
294952a6212SJordan K. Hubbard 				}
295952a6212SJordan K. Hubbard 
296952a6212SJordan K. Hubbard 				/*
29727a0bc89SDoug Rabson 				 * Ignore volume labels (anywhere, not just
29827a0bc89SDoug Rabson 				 * the root directory).
29927a0bc89SDoug Rabson 				 */
300952a6212SJordan K. Hubbard 				if (dep->deAttributes & ATTR_VOLUME) {
301952a6212SJordan K. Hubbard 					chksum = -1;
302952a6212SJordan K. Hubbard 					continue;
303952a6212SJordan K. Hubbard 				}
304952a6212SJordan K. Hubbard 
305952a6212SJordan K. Hubbard 				/*
306952a6212SJordan K. Hubbard 				 * Check for a checksum or name match
307952a6212SJordan K. Hubbard 				 */
30848d1bcf8SKonstantin Belousov 				chksum_ok = (chksum == winChksum(dep->deName));
3096a5bf04aSTim J. Robbins 				if (!chksum_ok
310952a6212SJordan K. Hubbard 				    && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
311952a6212SJordan K. Hubbard 					chksum = -1;
312952a6212SJordan K. Hubbard 					continue;
313952a6212SJordan K. Hubbard 				}
31427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
315952a6212SJordan K. Hubbard 				printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
316952a6212SJordan K. Hubbard 				    blkoff, diroff);
31727a0bc89SDoug Rabson #endif
31827a0bc89SDoug Rabson 				/*
31927a0bc89SDoug Rabson 				 * Remember where this directory
32027a0bc89SDoug Rabson 				 * entry came from for whoever did
321952a6212SJordan K. Hubbard 				 * this lookup.
32227a0bc89SDoug Rabson 				 */
32327a0bc89SDoug Rabson 				dp->de_fndoffset = diroff;
3246a5bf04aSTim J. Robbins 				if (chksum_ok && nameiop == RENAME) {
3256a5bf04aSTim J. Robbins 					/*
3266a5bf04aSTim J. Robbins 					 * Target had correct long name
3276a5bf04aSTim J. Robbins 					 * directory entries, reuse them
3286a5bf04aSTim J. Robbins 					 * as needed.
3296a5bf04aSTim J. Robbins 					 */
330191e6fd0SDmitrij Tejblum 					dp->de_fndcnt = wincnt - 1;
3316a5bf04aSTim J. Robbins 				} else {
3326a5bf04aSTim J. Robbins 					/*
3336a5bf04aSTim J. Robbins 					 * Long name directory entries
3346a5bf04aSTim J. Robbins 					 * not present or corrupt, can only
3356a5bf04aSTim J. Robbins 					 * reuse dos directory entry.
3366a5bf04aSTim J. Robbins 					 */
3376a5bf04aSTim J. Robbins 					dp->de_fndcnt = 0;
3386a5bf04aSTim J. Robbins 				}
339952a6212SJordan K. Hubbard 
34027a0bc89SDoug Rabson 				goto found;
34127a0bc89SDoug Rabson 			}
342952a6212SJordan K. Hubbard 		}	/* for (blkoff = 0; .... */
34327a0bc89SDoug Rabson 		/*
34427a0bc89SDoug Rabson 		 * Release the buffer holding the directory cluster just
34527a0bc89SDoug Rabson 		 * searched.
34627a0bc89SDoug Rabson 		 */
34727a0bc89SDoug Rabson 		brelse(bp);
34827a0bc89SDoug Rabson 	}	/* for (frcn = 0; ; frcn++) */
349952a6212SJordan K. Hubbard 
350952a6212SJordan K. Hubbard notfound:
35127a0bc89SDoug Rabson 	/*
35227a0bc89SDoug Rabson 	 * We hold no disk buffers at this point.
35327a0bc89SDoug Rabson 	 */
35427a0bc89SDoug Rabson 
35527a0bc89SDoug Rabson 	/*
356952a6212SJordan K. Hubbard 	 * Fixup the slot description to point to the place where
357952a6212SJordan K. Hubbard 	 * we might put the new DOS direntry (putting the Win95
358952a6212SJordan K. Hubbard 	 * long name entries before that)
359952a6212SJordan K. Hubbard 	 */
360952a6212SJordan K. Hubbard 	if (!slotcount) {
361952a6212SJordan K. Hubbard 		slotcount = 1;
362952a6212SJordan K. Hubbard 		slotoffset = diroff;
363952a6212SJordan K. Hubbard 	}
364952a6212SJordan K. Hubbard 	if (wincnt > slotcount)
365952a6212SJordan K. Hubbard 		slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
366952a6212SJordan K. Hubbard 
367952a6212SJordan K. Hubbard 	/*
36827a0bc89SDoug Rabson 	 * If we get here we didn't find the entry we were looking for. But
36927a0bc89SDoug Rabson 	 * that's ok if we are creating or renaming and are at the end of
37027a0bc89SDoug Rabson 	 * the pathname and the directory hasn't been removed.
37127a0bc89SDoug Rabson 	 */
37227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
373952a6212SJordan K. Hubbard 	printf("msdosfs_lookup(): op %d, refcnt %ld\n",
374952a6212SJordan K. Hubbard 	    nameiop, dp->de_refcnt);
375952a6212SJordan K. Hubbard 	printf("               slotcount %d, slotoffset %d\n",
376952a6212SJordan K. Hubbard 	       slotcount, slotoffset);
37727a0bc89SDoug Rabson #endif
37827a0bc89SDoug Rabson 	if ((nameiop == CREATE || nameiop == RENAME) &&
37927a0bc89SDoug Rabson 	    (flags & ISLASTCN) && dp->de_refcnt != 0) {
380952a6212SJordan K. Hubbard 		/*
381952a6212SJordan K. Hubbard 		 * Access for write is interpreted as allowing
382952a6212SJordan K. Hubbard 		 * creation of files in the directory.
383952a6212SJordan K. Hubbard 		 */
384b40ce416SJulian Elischer 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
385d8762fa6SBruce Evans 		if (error)
386952a6212SJordan K. Hubbard 			return (error);
387952a6212SJordan K. Hubbard 		/*
388952a6212SJordan K. Hubbard 		 * Return an indication of where the new directory
389952a6212SJordan K. Hubbard 		 * entry should be put.
390952a6212SJordan K. Hubbard 		 */
39127a0bc89SDoug Rabson 		dp->de_fndoffset = slotoffset;
392952a6212SJordan K. Hubbard 		dp->de_fndcnt = wincnt - 1;
393952a6212SJordan K. Hubbard 
394952a6212SJordan K. Hubbard 		/*
395952a6212SJordan K. Hubbard 		 * We return with the directory locked, so that
396952a6212SJordan K. Hubbard 		 * the parameters we set up above will still be
397952a6212SJordan K. Hubbard 		 * valid if we actually decide to do a direnter().
398952a6212SJordan K. Hubbard 		 * We return ni_vp == NULL to indicate that the entry
399952a6212SJordan K. Hubbard 		 * does not currently exist; we leave a pointer to
400952a6212SJordan K. Hubbard 		 * the (locked) directory inode in ndp->ni_dvp.
401952a6212SJordan K. Hubbard 		 * The pathname buffer is saved so that the name
402952a6212SJordan K. Hubbard 		 * can be obtained later.
403952a6212SJordan K. Hubbard 		 *
404952a6212SJordan K. Hubbard 		 * NB - if the directory is unlocked, then this
405952a6212SJordan K. Hubbard 		 * information cannot be used.
406952a6212SJordan K. Hubbard 		 */
40727a0bc89SDoug Rabson 		cnp->cn_flags |= SAVENAME;
408952a6212SJordan K. Hubbard 		return (EJUSTRETURN);
40927a0bc89SDoug Rabson 	}
41023e8fcafSDavid Schultz #if 0
41127a0bc89SDoug Rabson 	/*
412952a6212SJordan K. Hubbard 	 * Insert name into cache (as non-existent) if appropriate.
41323e8fcafSDavid Schultz 	 *
41423e8fcafSDavid Schultz 	 * XXX Negative caching is broken for msdosfs because the name
41523e8fcafSDavid Schultz 	 * cache doesn't understand peculiarities such as case insensitivity
41623e8fcafSDavid Schultz 	 * and 8.3 filenames.  Hence, it may not invalidate all negative
41723e8fcafSDavid Schultz 	 * entries if a file with this name is later created.
41827a0bc89SDoug Rabson 	 */
4196c21f6edSKonstantin Belousov 	if ((cnp->cn_flags & MAKEENTRY) != 0)
42027a0bc89SDoug Rabson 		cache_enter(vdp, *vpp, cnp);
42123e8fcafSDavid Schultz #endif
422952a6212SJordan K. Hubbard 	return (ENOENT);
42327a0bc89SDoug Rabson 
424952a6212SJordan K. Hubbard found:
42527a0bc89SDoug Rabson 	/*
42627a0bc89SDoug Rabson 	 * NOTE:  We still have the buffer with matched directory entry at
42727a0bc89SDoug Rabson 	 * this point.
42827a0bc89SDoug Rabson 	 */
42927a0bc89SDoug Rabson 	isadir = dep->deAttributes & ATTR_DIRECTORY;
43027a0bc89SDoug Rabson 	scn = getushort(dep->deStartCluster);
431952a6212SJordan K. Hubbard 	if (FAT32(pmp)) {
432952a6212SJordan K. Hubbard 		scn |= getushort(dep->deHighClust) << 16;
433952a6212SJordan K. Hubbard 		if (scn == pmp->pm_rootdirblk) {
434952a6212SJordan K. Hubbard 			/*
435952a6212SJordan K. Hubbard 			 * There should actually be 0 here.
436952a6212SJordan K. Hubbard 			 * Just ignore the error.
437952a6212SJordan K. Hubbard 			 */
438952a6212SJordan K. Hubbard 			scn = MSDOSFSROOT;
439952a6212SJordan K. Hubbard 		}
440952a6212SJordan K. Hubbard 	}
44127a0bc89SDoug Rabson 
442952a6212SJordan K. Hubbard 	if (isadir) {
443952a6212SJordan K. Hubbard 		cluster = scn;
444952a6212SJordan K. Hubbard 		if (cluster == MSDOSFSROOT)
445952a6212SJordan K. Hubbard 			blkoff = MSDOSFSROOT_OFS;
446952a6212SJordan K. Hubbard 		else
447952a6212SJordan K. Hubbard 			blkoff = 0;
448952a6212SJordan K. Hubbard 	} else if (cluster == MSDOSFSROOT)
449952a6212SJordan K. Hubbard 		blkoff = diroff;
450952a6212SJordan K. Hubbard 
451952a6212SJordan K. Hubbard 	/*
452952a6212SJordan K. Hubbard 	 * Now release buf to allow deget to read the entry again.
453952a6212SJordan K. Hubbard 	 * Reserving it here and giving it to deget could result
454952a6212SJordan K. Hubbard 	 * in a deadlock.
455952a6212SJordan K. Hubbard 	 */
456952a6212SJordan K. Hubbard 	brelse(bp);
4570d3e502fSPedro F. Giffuni 	bp = NULL;
458952a6212SJordan K. Hubbard 
459952a6212SJordan K. Hubbard foundroot:
46027a0bc89SDoug Rabson 	/*
46127a0bc89SDoug Rabson 	 * If we entered at foundroot, then we are looking for the . or ..
46227a0bc89SDoug Rabson 	 * entry of the filesystems root directory.  isadir and scn were
463952a6212SJordan K. Hubbard 	 * setup before jumping here.  And, bp is already null.
46427a0bc89SDoug Rabson 	 */
465952a6212SJordan K. Hubbard 	if (FAT32(pmp) && scn == MSDOSFSROOT)
466952a6212SJordan K. Hubbard 		scn = pmp->pm_rootdirblk;
46727a0bc89SDoug Rabson 
468*95d42526SKonstantin Belousov 	if (scnp != NULL) {
469*95d42526SKonstantin Belousov 		*scnp = cluster;
470*95d42526SKonstantin Belousov 		*blkoffp = blkoff;
471db811dd7SKonstantin Belousov 		return (0);
472db811dd7SKonstantin Belousov 	}
473db811dd7SKonstantin Belousov 
47427a0bc89SDoug Rabson 	/*
475952a6212SJordan K. Hubbard 	 * If deleting, and at end of pathname, return
476952a6212SJordan K. Hubbard 	 * parameters which can be used to remove file.
47727a0bc89SDoug Rabson 	 */
47827a0bc89SDoug Rabson 	if (nameiop == DELETE && (flags & ISLASTCN)) {
479952a6212SJordan K. Hubbard 		/*
480952a6212SJordan K. Hubbard 		 * Don't allow deleting the root.
481952a6212SJordan K. Hubbard 		 */
482952a6212SJordan K. Hubbard 		if (blkoff == MSDOSFSROOT_OFS)
4839ba671deSKonstantin Belousov 			return (EBUSY);
484952a6212SJordan K. Hubbard 
485952a6212SJordan K. Hubbard 		/*
486952a6212SJordan K. Hubbard 		 * Write access to directory required to delete files.
487952a6212SJordan K. Hubbard 		 */
488b40ce416SJulian Elischer 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
489952a6212SJordan K. Hubbard 		if (error)
490952a6212SJordan K. Hubbard 			return (error);
491952a6212SJordan K. Hubbard 
492952a6212SJordan K. Hubbard 		/*
493952a6212SJordan K. Hubbard 		 * Return pointer to current entry in dp->i_offset.
494952a6212SJordan K. Hubbard 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
495952a6212SJordan K. Hubbard 		 */
49627a0bc89SDoug Rabson 		if (dp->de_StartCluster == scn && isadir) {	/* "." */
49727a0bc89SDoug Rabson 			VREF(vdp);
49827a0bc89SDoug Rabson 			*vpp = vdp;
499952a6212SJordan K. Hubbard 			return (0);
50027a0bc89SDoug Rabson 		}
501ae7e8a02SKonstantin Belousov 		error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, &tdp);
502952a6212SJordan K. Hubbard 		if (error)
503952a6212SJordan K. Hubbard 			return (error);
50427a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
505952a6212SJordan K. Hubbard 		return (0);
50627a0bc89SDoug Rabson 	}
50727a0bc89SDoug Rabson 
50827a0bc89SDoug Rabson 	/*
509952a6212SJordan K. Hubbard 	 * If rewriting (RENAME), return the inode and the
510952a6212SJordan K. Hubbard 	 * information required to rewrite the present directory
511952a6212SJordan K. Hubbard 	 * Must get inode of directory entry to verify it's a
512952a6212SJordan K. Hubbard 	 * regular file, or empty directory.
51327a0bc89SDoug Rabson 	 */
514fcc9c112SJeff Roberson 	if (nameiop == RENAME && (flags & ISLASTCN)) {
515952a6212SJordan K. Hubbard 		if (blkoff == MSDOSFSROOT_OFS)
5169ba671deSKonstantin Belousov 			return (EBUSY);
517952a6212SJordan K. Hubbard 
518b40ce416SJulian Elischer 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
519952a6212SJordan K. Hubbard 		if (error)
520952a6212SJordan K. Hubbard 			return (error);
521952a6212SJordan K. Hubbard 
522952a6212SJordan K. Hubbard 		/*
523952a6212SJordan K. Hubbard 		 * Careful about locking second inode.
524952a6212SJordan K. Hubbard 		 * This can only occur if the target is ".".
525952a6212SJordan K. Hubbard 		 */
526952a6212SJordan K. Hubbard 		if (dp->de_StartCluster == scn && isadir)
527952a6212SJordan K. Hubbard 			return (EISDIR);
528952a6212SJordan K. Hubbard 
529ae7e8a02SKonstantin Belousov 		if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE,
530ae7e8a02SKonstantin Belousov 		    &tdp)) != 0)
531952a6212SJordan K. Hubbard 			return (error);
53227a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
53327a0bc89SDoug Rabson 		cnp->cn_flags |= SAVENAME;
534952a6212SJordan K. Hubbard 		return (0);
53527a0bc89SDoug Rabson 	}
53627a0bc89SDoug Rabson 
53727a0bc89SDoug Rabson 	/*
538952a6212SJordan K. Hubbard 	 * Step through the translation in the name.  We do not `vput' the
539952a6212SJordan K. Hubbard 	 * directory because we may need it again if a symbolic link
540952a6212SJordan K. Hubbard 	 * is relative to the current directory.  Instead we save it
541952a6212SJordan K. Hubbard 	 * unlocked as "pdp".  We must get the target inode before unlocking
542952a6212SJordan K. Hubbard 	 * the directory to insure that the inode will not be removed
543952a6212SJordan K. Hubbard 	 * before we get it.  We prevent deadlock by always fetching
544952a6212SJordan K. Hubbard 	 * inodes from the root, moving down the directory tree. Thus
545952a6212SJordan K. Hubbard 	 * when following backward pointers ".." we must unlock the
546952a6212SJordan K. Hubbard 	 * parent directory before getting the requested directory.
54727a0bc89SDoug Rabson 	 */
54827a0bc89SDoug Rabson 	pdp = vdp;
54927a0bc89SDoug Rabson 	if (flags & ISDOTDOT) {
550a6945216SKonstantin Belousov 		dd_arg.cluster = cluster;
551a6945216SKonstantin Belousov 		dd_arg.blkoff = blkoff;
552a6945216SKonstantin Belousov 		error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot,
553a6945216SKonstantin Belousov 		    &dd_arg, cnp->cn_lkflags, vpp);
554a6945216SKonstantin Belousov 		if (error != 0) {
55584caee6bSKonstantin Belousov 			*vpp = NULL;
556952a6212SJordan K. Hubbard 			return (error);
55784caee6bSKonstantin Belousov 		}
558db811dd7SKonstantin Belousov 		/*
559db811dd7SKonstantin Belousov 		 * Recheck that ".." still points to the inode we
560db811dd7SKonstantin Belousov 		 * looked up before pdp lock was dropped.
561db811dd7SKonstantin Belousov 		 */
562*95d42526SKonstantin Belousov 		error = msdosfs_lookup_ino(pdp, NULL, cnp, &scn, &blkoff);
563db811dd7SKonstantin Belousov 		if (error) {
564db811dd7SKonstantin Belousov 			vput(*vpp);
56584caee6bSKonstantin Belousov 			*vpp = NULL;
566db811dd7SKonstantin Belousov 			return (error);
567db811dd7SKonstantin Belousov 		}
568*95d42526SKonstantin Belousov 		if (FAT32(pmp) && scn == MSDOSFSROOT)
569*95d42526SKonstantin Belousov 			scn = pmp->pm_rootdirblk;
570*95d42526SKonstantin Belousov 		inode1 = scn * pmp->pm_bpcluster + blkoff;
571db811dd7SKonstantin Belousov 		if (VTODE(*vpp)->de_inode != inode1) {
572db811dd7SKonstantin Belousov 			vput(*vpp);
573db811dd7SKonstantin Belousov 			goto restart;
574db811dd7SKonstantin Belousov 		}
575952a6212SJordan K. Hubbard 	} else if (dp->de_StartCluster == scn && isadir) {
576952a6212SJordan K. Hubbard 		VREF(vdp);	/* we want ourself, ie "." */
57727a0bc89SDoug Rabson 		*vpp = vdp;
57827a0bc89SDoug Rabson 	} else {
579ae7e8a02SKonstantin Belousov 		if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE,
580ae7e8a02SKonstantin Belousov 		    &tdp)) != 0)
581952a6212SJordan K. Hubbard 			return (error);
58227a0bc89SDoug Rabson 		*vpp = DETOV(tdp);
58327a0bc89SDoug Rabson 	}
58427a0bc89SDoug Rabson 
58527a0bc89SDoug Rabson 	/*
586952a6212SJordan K. Hubbard 	 * Insert name into cache if appropriate.
58727a0bc89SDoug Rabson 	 */
58827a0bc89SDoug Rabson 	if (cnp->cn_flags & MAKEENTRY)
58927a0bc89SDoug Rabson 		cache_enter(vdp, *vpp, cnp);
590952a6212SJordan K. Hubbard 	return (0);
59127a0bc89SDoug Rabson }
59227a0bc89SDoug Rabson 
59327a0bc89SDoug Rabson /*
59427a0bc89SDoug Rabson  * dep  - directory entry to copy into the directory
59527a0bc89SDoug Rabson  * ddep - directory to add to
59627a0bc89SDoug Rabson  * depp - return the address of the denode for the created directory entry
59727a0bc89SDoug Rabson  *	  if depp != 0
598952a6212SJordan K. Hubbard  * cnp  - componentname needed for Win95 long filenames
59927a0bc89SDoug Rabson  */
60027a0bc89SDoug Rabson int
60110c9700fSEd Maste createde(struct denode *dep, struct denode *ddep, struct denode **depp,
60210c9700fSEd Maste     struct componentname *cnp)
60327a0bc89SDoug Rabson {
60427a0bc89SDoug Rabson 	int error;
60527a0bc89SDoug Rabson 	u_long dirclust, diroffset;
60627a0bc89SDoug Rabson 	struct direntry *ndep;
60727a0bc89SDoug Rabson 	struct msdosfsmount *pmp = ddep->de_pmp;
60827a0bc89SDoug Rabson 	struct buf *bp;
609952a6212SJordan K. Hubbard 	daddr_t bn;
610952a6212SJordan K. Hubbard 	int blsize;
61127a0bc89SDoug Rabson 
61227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
613952a6212SJordan K. Hubbard 	printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
614952a6212SJordan K. Hubbard 	    dep, ddep, depp, cnp);
61527a0bc89SDoug Rabson #endif
61627a0bc89SDoug Rabson 
61727a0bc89SDoug Rabson 	/*
61827a0bc89SDoug Rabson 	 * If no space left in the directory then allocate another cluster
61927a0bc89SDoug Rabson 	 * and chain it onto the end of the file.  There is one exception
62027a0bc89SDoug Rabson 	 * to this.  That is, if the root directory has no more space it
62127a0bc89SDoug Rabson 	 * can NOT be expanded.  extendfile() checks for and fails attempts
62227a0bc89SDoug Rabson 	 * to extend the root directory.  We just return an error in that
62327a0bc89SDoug Rabson 	 * case.
62427a0bc89SDoug Rabson 	 */
625952a6212SJordan K. Hubbard 	if (ddep->de_fndoffset >= ddep->de_FileSize) {
626952a6212SJordan K. Hubbard 		diroffset = ddep->de_fndoffset + sizeof(struct direntry)
627952a6212SJordan K. Hubbard 		    - ddep->de_FileSize;
628952a6212SJordan K. Hubbard 		dirclust = de_clcount(pmp, diroffset);
629952a6212SJordan K. Hubbard 		error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
630952a6212SJordan K. Hubbard 		if (error) {
631c52fd858SEdward Tomasz Napierala 			(void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
63227a0bc89SDoug Rabson 			return error;
633952a6212SJordan K. Hubbard 		}
634952a6212SJordan K. Hubbard 
63527a0bc89SDoug Rabson 		/*
63627a0bc89SDoug Rabson 		 * Update the size of the directory
63727a0bc89SDoug Rabson 		 */
638952a6212SJordan K. Hubbard 		ddep->de_FileSize += de_cn2off(pmp, dirclust);
639952a6212SJordan K. Hubbard 	}
640952a6212SJordan K. Hubbard 
64127a0bc89SDoug Rabson 	/*
642952a6212SJordan K. Hubbard 	 * We just read in the cluster with space.  Copy the new directory
64327a0bc89SDoug Rabson 	 * entry in.  Then write it to disk. NOTE:  DOS directories
64427a0bc89SDoug Rabson 	 * do not get smaller as clusters are emptied.
64527a0bc89SDoug Rabson 	 */
646952a6212SJordan K. Hubbard 	error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
647952a6212SJordan K. Hubbard 		       &bn, &dirclust, &blsize);
64827a0bc89SDoug Rabson 	if (error)
64927a0bc89SDoug Rabson 		return error;
650952a6212SJordan K. Hubbard 	diroffset = ddep->de_fndoffset;
651952a6212SJordan K. Hubbard 	if (dirclust != MSDOSFSROOT)
652952a6212SJordan K. Hubbard 		diroffset &= pmp->pm_crbomask;
653952a6212SJordan K. Hubbard 	if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
654952a6212SJordan K. Hubbard 		brelse(bp);
655952a6212SJordan K. Hubbard 		return error;
65627a0bc89SDoug Rabson 	}
657952a6212SJordan K. Hubbard 	ndep = bptoep(pmp, bp, ddep->de_fndoffset);
658952a6212SJordan K. Hubbard 
65927a0bc89SDoug Rabson 	DE_EXTERNALIZE(ndep, dep);
66027a0bc89SDoug Rabson 
66127a0bc89SDoug Rabson 	/*
662952a6212SJordan K. Hubbard 	 * Now write the Win95 long name
663952a6212SJordan K. Hubbard 	 */
664952a6212SJordan K. Hubbard 	if (ddep->de_fndcnt > 0) {
66523c53312SEd Maste 		uint8_t chksum = winChksum(ndep->deName);
666952a6212SJordan K. Hubbard 		const u_char *un = (const u_char *)cnp->cn_nameptr;
667952a6212SJordan K. Hubbard 		int unlen = cnp->cn_namelen;
668952a6212SJordan K. Hubbard 		int cnt = 1;
669952a6212SJordan K. Hubbard 
670952a6212SJordan K. Hubbard 		while (--ddep->de_fndcnt >= 0) {
671952a6212SJordan K. Hubbard 			if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
6722aacee77SKonstantin Belousov 				if (DOINGASYNC(DETOV(ddep)))
673cb65c1eeSBruce Evans 					bdwrite(bp);
674cb65c1eeSBruce Evans 				else if ((error = bwrite(bp)) != 0)
675952a6212SJordan K. Hubbard 					return error;
676952a6212SJordan K. Hubbard 
677952a6212SJordan K. Hubbard 				ddep->de_fndoffset -= sizeof(struct direntry);
678952a6212SJordan K. Hubbard 				error = pcbmap(ddep,
679952a6212SJordan K. Hubbard 					       de_cluster(pmp,
680952a6212SJordan K. Hubbard 							  ddep->de_fndoffset),
681952a6212SJordan K. Hubbard 					       &bn, 0, &blsize);
682952a6212SJordan K. Hubbard 				if (error)
683952a6212SJordan K. Hubbard 					return error;
684952a6212SJordan K. Hubbard 
685952a6212SJordan K. Hubbard 				error = bread(pmp->pm_devvp, bn, blsize,
686952a6212SJordan K. Hubbard 					      NOCRED, &bp);
687952a6212SJordan K. Hubbard 				if (error) {
688952a6212SJordan K. Hubbard 					return error;
689952a6212SJordan K. Hubbard 				}
690952a6212SJordan K. Hubbard 				ndep = bptoep(pmp, bp, ddep->de_fndoffset);
691952a6212SJordan K. Hubbard 			} else {
692952a6212SJordan K. Hubbard 				ndep--;
693952a6212SJordan K. Hubbard 				ddep->de_fndoffset -= sizeof(struct direntry);
694952a6212SJordan K. Hubbard 			}
69513df76f2SAndrey A. Chernov 			if (!unix2winfn(un, unlen, (struct winentry *)ndep,
696c4f02a89SMax Khon 					cnt++, chksum, pmp))
697952a6212SJordan K. Hubbard 				break;
698952a6212SJordan K. Hubbard 		}
699952a6212SJordan K. Hubbard 	}
700952a6212SJordan K. Hubbard 
7012aacee77SKonstantin Belousov 	if (DOINGASYNC(DETOV(ddep)))
702cb65c1eeSBruce Evans 		bdwrite(bp);
703cb65c1eeSBruce Evans 	else if ((error = bwrite(bp)) != 0)
704952a6212SJordan K. Hubbard 		return error;
705952a6212SJordan K. Hubbard 
706952a6212SJordan K. Hubbard 	/*
70727a0bc89SDoug Rabson 	 * If they want us to return with the denode gotten.
70827a0bc89SDoug Rabson 	 */
70927a0bc89SDoug Rabson 	if (depp) {
710952a6212SJordan K. Hubbard 		if (dep->de_Attributes & ATTR_DIRECTORY) {
711952a6212SJordan K. Hubbard 			dirclust = dep->de_StartCluster;
712952a6212SJordan K. Hubbard 			if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
713952a6212SJordan K. Hubbard 				dirclust = MSDOSFSROOT;
714952a6212SJordan K. Hubbard 			if (dirclust == MSDOSFSROOT)
715952a6212SJordan K. Hubbard 				diroffset = MSDOSFSROOT_OFS;
716952a6212SJordan K. Hubbard 			else
717952a6212SJordan K. Hubbard 				diroffset = 0;
71827a0bc89SDoug Rabson 		}
719ae7e8a02SKonstantin Belousov 		return (deget(pmp, dirclust, diroffset, LK_EXCLUSIVE, depp));
72027a0bc89SDoug Rabson 	}
721952a6212SJordan K. Hubbard 
72227a0bc89SDoug Rabson 	return 0;
72327a0bc89SDoug Rabson }
72427a0bc89SDoug Rabson 
72527a0bc89SDoug Rabson /*
72627a0bc89SDoug Rabson  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
72727a0bc89SDoug Rabson  * return 0 if not empty or error.
72827a0bc89SDoug Rabson  */
72927a0bc89SDoug Rabson int
73010c9700fSEd Maste dosdirempty(struct denode *dep)
73127a0bc89SDoug Rabson {
732952a6212SJordan K. Hubbard 	int blsize;
73327a0bc89SDoug Rabson 	int error;
73427a0bc89SDoug Rabson 	u_long cn;
73527a0bc89SDoug Rabson 	daddr_t bn;
73627a0bc89SDoug Rabson 	struct buf *bp;
73727a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
73827a0bc89SDoug Rabson 	struct direntry *dentp;
73927a0bc89SDoug Rabson 
74027a0bc89SDoug Rabson 	/*
74127a0bc89SDoug Rabson 	 * Since the filesize field in directory entries for a directory is
74227a0bc89SDoug Rabson 	 * zero, we just have to feel our way through the directory until
74327a0bc89SDoug Rabson 	 * we hit end of file.
74427a0bc89SDoug Rabson 	 */
74527a0bc89SDoug Rabson 	for (cn = 0;; cn++) {
746952a6212SJordan K. Hubbard 		if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
74727a0bc89SDoug Rabson 			if (error == E2BIG)
748952a6212SJordan K. Hubbard 				return (1);	/* it's empty */
749952a6212SJordan K. Hubbard 			return (0);
750952a6212SJordan K. Hubbard 		}
751952a6212SJordan K. Hubbard 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
752952a6212SJordan K. Hubbard 		if (error) {
753952a6212SJordan K. Hubbard 			return (0);
754952a6212SJordan K. Hubbard 		}
755952a6212SJordan K. Hubbard 		for (dentp = (struct direntry *)bp->b_data;
756952a6212SJordan K. Hubbard 		     (char *)dentp < bp->b_data + blsize;
757952a6212SJordan K. Hubbard 		     dentp++) {
758952a6212SJordan K. Hubbard 			if (dentp->deName[0] != SLOT_DELETED &&
759952a6212SJordan K. Hubbard 			    (dentp->deAttributes & ATTR_VOLUME) == 0) {
76027a0bc89SDoug Rabson 				/*
76127a0bc89SDoug Rabson 				 * In dos directories an entry whose name
76227a0bc89SDoug Rabson 				 * starts with SLOT_EMPTY (0) starts the
76327a0bc89SDoug Rabson 				 * beginning of the unused part of the
76427a0bc89SDoug Rabson 				 * directory, so we can just return that it
76527a0bc89SDoug Rabson 				 * is empty.
76627a0bc89SDoug Rabson 				 */
76727a0bc89SDoug Rabson 				if (dentp->deName[0] == SLOT_EMPTY) {
76827a0bc89SDoug Rabson 					brelse(bp);
769952a6212SJordan K. Hubbard 					return (1);
77027a0bc89SDoug Rabson 				}
77127a0bc89SDoug Rabson 				/*
77227a0bc89SDoug Rabson 				 * Any names other than "." and ".." in a
77327a0bc89SDoug Rabson 				 * directory mean it is not empty.
77427a0bc89SDoug Rabson 				 */
77527a0bc89SDoug Rabson 				if (bcmp(dentp->deName, ".          ", 11) &&
77627a0bc89SDoug Rabson 				    bcmp(dentp->deName, "..         ", 11)) {
77727a0bc89SDoug Rabson 					brelse(bp);
77827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
779952a6212SJordan K. Hubbard 					printf("dosdirempty(): entry found %02x, %02x\n",
780952a6212SJordan K. Hubbard 					    dentp->deName[0], dentp->deName[1]);
78127a0bc89SDoug Rabson #endif
782952a6212SJordan K. Hubbard 					return (0);	/* not empty */
78327a0bc89SDoug Rabson 				}
78427a0bc89SDoug Rabson 			}
78527a0bc89SDoug Rabson 		}
78627a0bc89SDoug Rabson 		brelse(bp);
78727a0bc89SDoug Rabson 	}
78827a0bc89SDoug Rabson 	/* NOTREACHED */
78927a0bc89SDoug Rabson }
79027a0bc89SDoug Rabson 
79127a0bc89SDoug Rabson /*
79227a0bc89SDoug Rabson  * Check to see if the directory described by target is in some
79327a0bc89SDoug Rabson  * subdirectory of source.  This prevents something like the following from
79427a0bc89SDoug Rabson  * succeeding and leaving a bunch or files and directories orphaned. mv
79527a0bc89SDoug Rabson  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
79627a0bc89SDoug Rabson  *
79727a0bc89SDoug Rabson  * source - the inode for /a/b/c
79827a0bc89SDoug Rabson  * target - the inode for /a/b/c/d/e/f
79927a0bc89SDoug Rabson  *
80027a0bc89SDoug Rabson  * Returns 0 if target is NOT a subdirectory of source.
80127a0bc89SDoug Rabson  * Otherwise returns a non-zero error number.
80227a0bc89SDoug Rabson  */
80327a0bc89SDoug Rabson int
804*95d42526SKonstantin Belousov doscheckpath(struct denode *source, struct denode *target, daddr_t *wait_scn)
80527a0bc89SDoug Rabson {
80627a0bc89SDoug Rabson 	daddr_t scn;
80727a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
80827a0bc89SDoug Rabson 	struct direntry *ep;
80927a0bc89SDoug Rabson 	struct denode *dep;
81027a0bc89SDoug Rabson 	struct buf *bp = NULL;
81127a0bc89SDoug Rabson 	int error = 0;
81227a0bc89SDoug Rabson 
813*95d42526SKonstantin Belousov 	*wait_scn = 0;
814952a6212SJordan K. Hubbard 
815*95d42526SKonstantin Belousov 	pmp = target->de_pmp;
816*95d42526SKonstantin Belousov 	KASSERT(pmp == source->de_pmp,
817*95d42526SKonstantin Belousov 	    ("doscheckpath: source and target on different filesystems"));
818*95d42526SKonstantin Belousov 
819*95d42526SKonstantin Belousov 	if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
820*95d42526SKonstantin Belousov 	    (source->de_Attributes & ATTR_DIRECTORY) == 0)
821*95d42526SKonstantin Belousov 		return (ENOTDIR);
822*95d42526SKonstantin Belousov 
823*95d42526SKonstantin Belousov 	if (target->de_StartCluster == source->de_StartCluster)
824*95d42526SKonstantin Belousov 		return (EEXIST);
825*95d42526SKonstantin Belousov 
826*95d42526SKonstantin Belousov 	if (target->de_StartCluster == MSDOSFSROOT ||
827*95d42526SKonstantin Belousov 	    (FAT32(pmp) && target->de_StartCluster == pmp->pm_rootdirblk))
828*95d42526SKonstantin Belousov 		return (0);
829*95d42526SKonstantin Belousov 
830*95d42526SKonstantin Belousov 	dep = target;
831*95d42526SKonstantin Belousov 	vget(DETOV(dep), LK_EXCLUSIVE);
83227a0bc89SDoug Rabson 	for (;;) {
83327a0bc89SDoug Rabson 		if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
83427a0bc89SDoug Rabson 			error = ENOTDIR;
835952a6212SJordan K. Hubbard 			break;
83627a0bc89SDoug Rabson 		}
83727a0bc89SDoug Rabson 		scn = dep->de_StartCluster;
83827a0bc89SDoug Rabson 		error = bread(pmp->pm_devvp, cntobn(pmp, scn),
83927a0bc89SDoug Rabson 		    pmp->pm_bpcluster, NOCRED, &bp);
840*95d42526SKonstantin Belousov 		if (error != 0)
84127a0bc89SDoug Rabson 			break;
842952a6212SJordan K. Hubbard 
84327a0bc89SDoug Rabson 		ep = (struct direntry *)bp->b_data + 1;
84427a0bc89SDoug Rabson 		if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
84527a0bc89SDoug Rabson 		    bcmp(ep->deName, "..         ", 11) != 0) {
84627a0bc89SDoug Rabson 			error = ENOTDIR;
847*95d42526SKonstantin Belousov 			brelse(bp);
84827a0bc89SDoug Rabson 			break;
84927a0bc89SDoug Rabson 		}
850*95d42526SKonstantin Belousov 
85127a0bc89SDoug Rabson 		scn = getushort(ep->deStartCluster);
852952a6212SJordan K. Hubbard 		if (FAT32(pmp))
853952a6212SJordan K. Hubbard 			scn |= getushort(ep->deHighClust) << 16;
854*95d42526SKonstantin Belousov 		brelse(bp);
855952a6212SJordan K. Hubbard 
85627a0bc89SDoug Rabson 		if (scn == source->de_StartCluster) {
85727a0bc89SDoug Rabson 			error = EINVAL;
85827a0bc89SDoug Rabson 			break;
85927a0bc89SDoug Rabson 		}
86027a0bc89SDoug Rabson 		if (scn == MSDOSFSROOT)
86127a0bc89SDoug Rabson 			break;
862952a6212SJordan K. Hubbard 		if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
863952a6212SJordan K. Hubbard 			/*
864952a6212SJordan K. Hubbard 			 * scn should be 0 in this case,
865952a6212SJordan K. Hubbard 			 * but we silently ignore the error.
866952a6212SJordan K. Hubbard 			 */
867952a6212SJordan K. Hubbard 			break;
868952a6212SJordan K. Hubbard 		}
869952a6212SJordan K. Hubbard 
87027a0bc89SDoug Rabson 		vput(DETOV(dep));
871*95d42526SKonstantin Belousov 		dep = NULL;
872952a6212SJordan K. Hubbard 		/* NOTE: deget() clears dep on error */
873*95d42526SKonstantin Belousov 		error = deget(pmp, scn, 0, LK_EXCLUSIVE | LK_NOWAIT, &dep);
874*95d42526SKonstantin Belousov 		if (error != 0) {
875*95d42526SKonstantin Belousov 			*wait_scn = scn;
87627a0bc89SDoug Rabson 			break;
87727a0bc89SDoug Rabson 		}
878*95d42526SKonstantin Belousov 	}
87954cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
88027a0bc89SDoug Rabson 	if (error == ENOTDIR)
88127a0bc89SDoug Rabson 		printf("doscheckpath(): .. not a directory?\n");
88254cf9198SKonstantin Belousov #endif
88327a0bc89SDoug Rabson 	if (dep != NULL)
88427a0bc89SDoug Rabson 		vput(DETOV(dep));
885952a6212SJordan K. Hubbard 	return (error);
88627a0bc89SDoug Rabson }
88727a0bc89SDoug Rabson 
88827a0bc89SDoug Rabson /*
88927a0bc89SDoug Rabson  * Read in the disk block containing the directory entry (dirclu, dirofs)
89027a0bc89SDoug Rabson  * and return the address of the buf header, and the address of the
89127a0bc89SDoug Rabson  * directory entry within the block.
89227a0bc89SDoug Rabson  */
89327a0bc89SDoug Rabson int
89410c9700fSEd Maste readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
89510c9700fSEd Maste     struct buf **bpp, struct direntry **epp)
89627a0bc89SDoug Rabson {
89727a0bc89SDoug Rabson 	int error;
89827a0bc89SDoug Rabson 	daddr_t bn;
899952a6212SJordan K. Hubbard 	int blsize;
90027a0bc89SDoug Rabson 
901952a6212SJordan K. Hubbard 	blsize = pmp->pm_bpcluster;
902952a6212SJordan K. Hubbard 	if (dirclust == MSDOSFSROOT
903952a6212SJordan K. Hubbard 	    && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
904952a6212SJordan K. Hubbard 		blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
905952a6212SJordan K. Hubbard 	bn = detobn(pmp, dirclust, diroffset);
906952a6212SJordan K. Hubbard 	if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
907952a6212SJordan K. Hubbard 		brelse(*bpp);
90827a0bc89SDoug Rabson 		*bpp = NULL;
909952a6212SJordan K. Hubbard 		return (error);
91027a0bc89SDoug Rabson 	}
91127a0bc89SDoug Rabson 	if (epp)
912952a6212SJordan K. Hubbard 		*epp = bptoep(pmp, *bpp, diroffset);
913952a6212SJordan K. Hubbard 	return (0);
91427a0bc89SDoug Rabson }
91527a0bc89SDoug Rabson 
91627a0bc89SDoug Rabson /*
91727a0bc89SDoug Rabson  * Read in the disk block containing the directory entry dep came from and
91827a0bc89SDoug Rabson  * return the address of the buf header, and the address of the directory
91927a0bc89SDoug Rabson  * entry within the block.
92027a0bc89SDoug Rabson  */
92127a0bc89SDoug Rabson int
92210c9700fSEd Maste readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
92327a0bc89SDoug Rabson {
924952a6212SJordan K. Hubbard 
925952a6212SJordan K. Hubbard 	return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
926952a6212SJordan K. Hubbard 	    bpp, epp));
927952a6212SJordan K. Hubbard }
928952a6212SJordan K. Hubbard 
929952a6212SJordan K. Hubbard /*
930952a6212SJordan K. Hubbard  * Remove a directory entry. At this point the file represented by the
931952a6212SJordan K. Hubbard  * directory entry to be removed is still full length until no one has it
932952a6212SJordan K. Hubbard  * open.  When the file no longer being used msdosfs_inactive() is called
933952a6212SJordan K. Hubbard  * and will truncate the file to 0 length.  When the vnode containing the
934952a6212SJordan K. Hubbard  * denode is needed for some other purpose by VFS it will call
935952a6212SJordan K. Hubbard  * msdosfs_reclaim() which will remove the denode from the denode cache.
9364882501bSEd Maste  *
9374882501bSEd Maste  * pdep	directory where the entry is removed
9384882501bSEd Maste  * dep	file to be removed
939952a6212SJordan K. Hubbard  */
940952a6212SJordan K. Hubbard int
94110c9700fSEd Maste removede(struct denode *pdep, struct denode *dep)
942952a6212SJordan K. Hubbard {
943952a6212SJordan K. Hubbard 	int error;
944952a6212SJordan K. Hubbard 	struct direntry *ep;
945952a6212SJordan K. Hubbard 	struct buf *bp;
946952a6212SJordan K. Hubbard 	daddr_t bn;
947952a6212SJordan K. Hubbard 	int blsize;
948952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = pdep->de_pmp;
949952a6212SJordan K. Hubbard 	u_long offset = pdep->de_fndoffset;
950952a6212SJordan K. Hubbard 
951952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG
952952a6212SJordan K. Hubbard 	printf("removede(): filename %s, dep %p, offset %08lx\n",
953952a6212SJordan K. Hubbard 	    dep->de_Name, dep, offset);
954952a6212SJordan K. Hubbard #endif
955952a6212SJordan K. Hubbard 
956952a6212SJordan K. Hubbard 	dep->de_refcnt--;
957952a6212SJordan K. Hubbard 	offset += sizeof(struct direntry);
958952a6212SJordan K. Hubbard 	do {
959952a6212SJordan K. Hubbard 		offset -= sizeof(struct direntry);
960952a6212SJordan K. Hubbard 		error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
961952a6212SJordan K. Hubbard 		if (error)
962952a6212SJordan K. Hubbard 			return error;
963952a6212SJordan K. Hubbard 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
964952a6212SJordan K. Hubbard 		if (error) {
965952a6212SJordan K. Hubbard 			return error;
966952a6212SJordan K. Hubbard 		}
967952a6212SJordan K. Hubbard 		ep = bptoep(pmp, bp, offset);
968952a6212SJordan K. Hubbard 		/*
969952a6212SJordan K. Hubbard 		 * Check whether, if we came here the second time, i.e.
970952a6212SJordan K. Hubbard 		 * when underflowing into the previous block, the last
971952a6212SJordan K. Hubbard 		 * entry in this block is a longfilename entry, too.
972952a6212SJordan K. Hubbard 		 */
973952a6212SJordan K. Hubbard 		if (ep->deAttributes != ATTR_WIN95
974952a6212SJordan K. Hubbard 		    && offset != pdep->de_fndoffset) {
975952a6212SJordan K. Hubbard 			brelse(bp);
976952a6212SJordan K. Hubbard 			break;
977952a6212SJordan K. Hubbard 		}
978952a6212SJordan K. Hubbard 		offset += sizeof(struct direntry);
979952a6212SJordan K. Hubbard 		while (1) {
980952a6212SJordan K. Hubbard 			/*
981b3a15dddSPedro F. Giffuni 			 * We are a bit aggressive here in that we delete any Win95
982952a6212SJordan K. Hubbard 			 * entries preceding this entry, not just the ones we "own".
983952a6212SJordan K. Hubbard 			 * Since these presumably aren't valid anyway,
984952a6212SJordan K. Hubbard 			 * there should be no harm.
985952a6212SJordan K. Hubbard 			 */
986952a6212SJordan K. Hubbard 			offset -= sizeof(struct direntry);
987952a6212SJordan K. Hubbard 			ep--->deName[0] = SLOT_DELETED;
988952a6212SJordan K. Hubbard 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
989952a6212SJordan K. Hubbard 			    || !(offset & pmp->pm_crbomask)
990952a6212SJordan K. Hubbard 			    || ep->deAttributes != ATTR_WIN95)
991952a6212SJordan K. Hubbard 				break;
992952a6212SJordan K. Hubbard 		}
9932aacee77SKonstantin Belousov 		if (DOINGASYNC(DETOV(pdep)))
994cb65c1eeSBruce Evans 			bdwrite(bp);
995cb65c1eeSBruce Evans 		else if ((error = bwrite(bp)) != 0)
996952a6212SJordan K. Hubbard 			return error;
997952a6212SJordan K. Hubbard 	} while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
998952a6212SJordan K. Hubbard 	    && !(offset & pmp->pm_crbomask)
999952a6212SJordan K. Hubbard 	    && offset);
1000952a6212SJordan K. Hubbard 	return 0;
1001952a6212SJordan K. Hubbard }
1002952a6212SJordan K. Hubbard 
1003952a6212SJordan K. Hubbard /*
1004952a6212SJordan K. Hubbard  * Create a unique DOS name in dvp
1005952a6212SJordan K. Hubbard  */
1006952a6212SJordan K. Hubbard int
100710c9700fSEd Maste uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
1008952a6212SJordan K. Hubbard {
1009952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = dep->de_pmp;
1010952a6212SJordan K. Hubbard 	struct direntry *dentp;
1011952a6212SJordan K. Hubbard 	int gen;
1012952a6212SJordan K. Hubbard 	int blsize;
1013952a6212SJordan K. Hubbard 	u_long cn;
1014952a6212SJordan K. Hubbard 	daddr_t bn;
1015952a6212SJordan K. Hubbard 	struct buf *bp;
1016952a6212SJordan K. Hubbard 	int error;
1017952a6212SJordan K. Hubbard 
1018af9f1d50SDmitrij Tejblum 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1019011cdb57SDmitrij Tejblum 		return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1020c4f02a89SMax Khon 		    cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
1021011cdb57SDmitrij Tejblum 
1022952a6212SJordan K. Hubbard 	for (gen = 1;; gen++) {
1023952a6212SJordan K. Hubbard 		/*
1024952a6212SJordan K. Hubbard 		 * Generate DOS name with generation number
1025952a6212SJordan K. Hubbard 		 */
1026952a6212SJordan K. Hubbard 		if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1027c4f02a89SMax Khon 		    cnp->cn_namelen, gen, pmp))
1028952a6212SJordan K. Hubbard 			return gen == 1 ? EINVAL : EEXIST;
1029952a6212SJordan K. Hubbard 
1030952a6212SJordan K. Hubbard 		/*
1031952a6212SJordan K. Hubbard 		 * Now look for a dir entry with this exact name
1032952a6212SJordan K. Hubbard 		 */
1033952a6212SJordan K. Hubbard 		for (cn = error = 0; !error; cn++) {
1034952a6212SJordan K. Hubbard 			if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1035952a6212SJordan K. Hubbard 				if (error == E2BIG)	/* EOF reached and not found */
1036952a6212SJordan K. Hubbard 					return 0;
1037952a6212SJordan K. Hubbard 				return error;
1038952a6212SJordan K. Hubbard 			}
1039952a6212SJordan K. Hubbard 			error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1040952a6212SJordan K. Hubbard 			if (error) {
1041952a6212SJordan K. Hubbard 				return error;
1042952a6212SJordan K. Hubbard 			}
1043952a6212SJordan K. Hubbard 			for (dentp = (struct direntry *)bp->b_data;
1044952a6212SJordan K. Hubbard 			     (char *)dentp < bp->b_data + blsize;
1045952a6212SJordan K. Hubbard 			     dentp++) {
1046952a6212SJordan K. Hubbard 				if (dentp->deName[0] == SLOT_EMPTY) {
1047952a6212SJordan K. Hubbard 					/*
1048952a6212SJordan K. Hubbard 					 * Last used entry and not found
1049952a6212SJordan K. Hubbard 					 */
1050952a6212SJordan K. Hubbard 					brelse(bp);
1051952a6212SJordan K. Hubbard 					return 0;
1052952a6212SJordan K. Hubbard 				}
1053952a6212SJordan K. Hubbard 				/*
1054952a6212SJordan K. Hubbard 				 * Ignore volume labels and Win95 entries
1055952a6212SJordan K. Hubbard 				 */
1056952a6212SJordan K. Hubbard 				if (dentp->deAttributes & ATTR_VOLUME)
1057952a6212SJordan K. Hubbard 					continue;
1058952a6212SJordan K. Hubbard 				if (!bcmp(dentp->deName, cp, 11)) {
1059952a6212SJordan K. Hubbard 					error = EEXIST;
1060952a6212SJordan K. Hubbard 					break;
1061952a6212SJordan K. Hubbard 				}
1062952a6212SJordan K. Hubbard 			}
1063952a6212SJordan K. Hubbard 			brelse(bp);
1064952a6212SJordan K. Hubbard 		}
1065952a6212SJordan K. Hubbard 	}
1066952a6212SJordan K. Hubbard }
1067