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