1 /* $FreeBSD: src/sys/msdosfs/msdosfs_lookup.c,v 1.30.2.1 2000/11/03 15:55:39 bp Exp $ */
2 /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/vnode.h>
56 #include <sys/namei.h>
57 #include <sys/mount.h>
64 #include "msdosfsmount.h"
68 * When we search a directory the blocks containing directory entries are
69 * read and examined. The directory entries contain information that would
70 * normally be in the inode of a unix filesystem. This means that some of
71 * a directory's contents may also be in memory resident denodes (sort of
72 * an inode). This can cause problems if we are searching while some other
73 * process is modifying a directory. To prevent one process from accessing
74 * incompletely modified directory information we depend upon being the
75 * sole owner of a directory block. bread/brelse provide this service.
76 * This being the case, when a process modifies a directory it must first
77 * acquire the disk block that contains the directory entry to be modified.
78 * Then update the disk block and the denode, and then write the disk block
79 * out to disk. This way disk blocks containing directory entries and in
80 * memory denode's will be in synch.
82 * msdosfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
83 * struct componentname *a_cnp)
86 msdosfs_lookup(struct vop_old_lookup_args *ap)
89 struct vnode *vdp = ap->a_dvp;
90 struct vnode **vpp = ap->a_vpp;
91 struct componentname *cnp = ap->a_cnp;
103 int isadir; /* ~0 if found direntry is a directory */
104 u_long scn; /* starting cluster number */
108 struct msdosfsmount *pmp;
109 struct buf *bp = NULL;
110 struct direntry *dep = NULL;
111 u_char dosfilename[12];
112 int flags = cnp->cn_flags;
113 int nameiop = cnp->cn_nameiop;
119 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
122 kprintf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
127 lockparent = flags & CNP_LOCKPARENT;
128 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
130 kprintf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
131 vdp, dp, dp->de_Attributes);
135 * If they are going after the . or .. entry in the root directory,
136 * they won't find it. DOS filesystems don't have them in the root
137 * directory. So, we fake it. deget() is in on this scam too.
139 if ((vdp->v_flag & VROOT) && cnp->cn_nameptr[0] == '.' &&
140 (cnp->cn_namelen == 1 ||
141 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
142 isadir = ATTR_DIRECTORY;
145 kprintf("msdosfs_lookup(): looking for . or .. in root directory\n");
147 cluster = MSDOSFSROOT;
148 blkoff = MSDOSFSROOT_OFS;
151 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
152 cnp->cn_namelen, 0, pmp)) {
158 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
159 cnp->cn_namelen, pmp) + 1;
163 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
164 cnp->cn_namelen, pmp) + 1;
167 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
171 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
174 * Suppress search for slots unless creating
175 * file and at end of pathname, in which case
176 * we watch for a place to put the new file in
177 * case it doesn't already exist.
180 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)
184 kprintf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
185 dosfilename, cnp->cn_namelen);
188 * Search the directory pointed at by vdp for the name pointed at
189 * by cnp->cn_nameptr.
194 * The outer loop ranges over the clusters that make up the
195 * directory. Note that the root directory is different from all
196 * other directories. It has a fixed number of blocks that are not
197 * part of the pool of allocatable clusters. So, we treat it a
198 * little differently. The root directory starts at "cluster" 0.
201 for (frcn = 0;; frcn++) {
202 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
208 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
213 for (blkoff = 0; blkoff < blsize;
214 blkoff += sizeof(struct direntry),
215 diroff += sizeof(struct direntry)) {
216 dep = (struct direntry *)(bp->b_data + blkoff);
218 * If the slot is empty and we are still looking
219 * for an empty then remember this one. If the
220 * slot is not empty then check to see if it
221 * matches what we are looking for. If the slot
222 * has never been filled with anything, then the
223 * remainder of the directory has never been used,
224 * so there is no point in searching it.
226 if (dep->deName[0] == SLOT_EMPTY ||
227 dep->deName[0] == SLOT_DELETED) {
229 * Drop memory of previous long matches
234 if (slotcount < wincnt) {
238 if (dep->deName[0] == SLOT_EMPTY) {
244 * If there wasn't enough space for our winentries,
245 * forget about the empty space
247 if (slotcount < wincnt)
251 * Check for Win95 long filename entry
253 if (dep->deAttributes == ATTR_WIN95) {
254 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
256 chksum = win2unixfn(&nb,
257 (struct winentry *)dep, chksum,
262 chksum = winChkName(&nb,
263 (const u_char *)cnp->cn_nameptr, unlen,
271 * Ignore volume labels (anywhere, not just
272 * the root directory).
274 if (dep->deAttributes & ATTR_VOLUME) {
280 * Check for a checksum or name match
282 if (chksum != winChksum(dep->deName)
283 && (!olddos || bcmp(dosfilename,
289 kprintf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
293 * Remember where this directory
294 * entry came from for whoever did
297 dp->de_fndoffset = diroff;
298 dp->de_fndcnt = wincnt - 1;
302 } /* for (blkoff = 0; .... */
304 * Release the buffer holding the directory cluster just
308 } /* for (frcn = 0; ; frcn++) */
312 * We hold no disk buffers at this point.
316 * Fixup the slot description to point to the place where
317 * we might put the new DOS direntry (putting the Win95
318 * long name entries before that)
324 if (wincnt > slotcount)
325 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
328 * If we get here we didn't find the entry we were looking for. But
329 * that's ok if we are creating or renaming and are at the end of
330 * the pathname and the directory hasn't been removed.
333 kprintf("msdosfs_lookup(): op %d, refcnt %ld\n",
334 nameiop, dp->de_refcnt);
335 kprintf(" slotcount %d, slotoffset %d\n",
336 slotcount, slotoffset);
338 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
341 * Access for write is interpreted as allowing
342 * creation of files in the directory.
344 error = VOP_EACCESS(vdp, VWRITE, cnp->cn_cred);
348 * Return an indication of where the new directory
349 * entry should be put.
351 dp->de_fndoffset = slotoffset;
352 dp->de_fndcnt = wincnt - 1;
355 * We return with the directory locked, so that
356 * the parameters we set up above will still be
357 * valid if we actually decide to do a direnter().
358 * We return ni_vp == NULL to indicate that the entry
359 * does not currently exist; we leave a pointer to
360 * the (locked) directory inode in ndp->ni_dvp.
361 * The pathname buffer is saved so that the name
362 * can be obtained later.
364 * NB - if the directory is unlocked, then this
365 * information cannot be used.
369 cnp->cn_flags |= CNP_PDIRUNLOCK;
371 return (EJUSTRETURN);
377 * NOTE: We still have the buffer with matched directory entry at
380 isadir = dep->deAttributes & ATTR_DIRECTORY;
381 scn = getushort(dep->deStartCluster);
383 scn |= getushort(dep->deHighClust) << 16;
384 if (scn == pmp->pm_rootdirblk) {
386 * There should actually be 0 here.
387 * Just ignore the error.
395 if (cluster == MSDOSFSROOT)
396 blkoff = MSDOSFSROOT_OFS;
399 } else if (cluster == MSDOSFSROOT)
403 * Now release buf to allow deget to read the entry again.
404 * Reserving it here and giving it to deget could result
412 * If we entered at foundroot, then we are looking for the . or ..
413 * entry of the filesystems root directory. isadir and scn were
414 * setup before jumping here. And, bp is already null.
416 if (FAT32(pmp) && scn == MSDOSFSROOT)
417 scn = pmp->pm_rootdirblk;
420 * If deleting, and at end of pathname, return
421 * parameters which can be used to remove file.
422 * If the wantparent flag isn't set, we return only
423 * the directory (in ndp->ni_dvp), otherwise we go
424 * on and lock the inode, being careful with ".".
426 if (nameiop == NAMEI_DELETE) {
428 * Don't allow deleting the root.
430 if (blkoff == MSDOSFSROOT_OFS)
431 return EROFS; /* really? XXX */
434 * Write access to directory required to delete files.
436 error = VOP_EACCESS(vdp, VWRITE, cnp->cn_cred);
441 * Return pointer to current entry in dp->i_offset.
442 * Save directory inode pointer in ndp->ni_dvp for dirremove().
444 if (dp->de_StartCluster == scn && isadir) { /* "." */
449 error = deget(pmp, cluster, blkoff, &tdp);
455 cnp->cn_flags |= CNP_PDIRUNLOCK;
461 * If rewriting (RENAME), return the inode and the
462 * information required to rewrite the present directory
463 * Must get inode of directory entry to verify it's a
464 * regular file, or empty directory.
466 if (nameiop == NAMEI_RENAME && wantparent) {
467 if (blkoff == MSDOSFSROOT_OFS)
468 return EROFS; /* really? XXX */
470 error = VOP_EACCESS(vdp, VWRITE, cnp->cn_cred);
475 * Careful about locking second inode.
476 * This can only occur if the target is ".".
478 if (dp->de_StartCluster == scn && isadir)
481 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
486 cnp->cn_flags |= CNP_PDIRUNLOCK;
492 * Step through the translation in the name. We do not `vput' the
493 * directory because we may need it again if a symbolic link
494 * is relative to the current directory. Instead we save it
495 * unlocked as "pdp". We must get the target inode before unlocking
496 * the directory to insure that the inode will not be removed
497 * before we get it. We prevent deadlock by always fetching
498 * inodes from the root, moving down the directory tree. Thus
499 * when following backward pointers ".." we must unlock the
500 * parent directory before getting the requested directory.
501 * There is a potential race condition here if both the current
502 * and parent directories are removed before the VFS_VGET for the
503 * inode associated with ".." returns. We hope that this occurs
504 * infrequently since we cannot avoid this race condition without
505 * implementing a sophisticated deadlock detection algorithm.
506 * Note also that this simple deadlock detection scheme will not
507 * work if the file system has any hard links other than ".."
508 * that point backwards in the directory structure.
511 if (flags & CNP_ISDOTDOT) {
513 cnp->cn_flags |= CNP_PDIRUNLOCK;
514 error = deget(pmp, cluster, blkoff, &tdp);
516 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
517 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
521 error = vn_lock(pdp, LK_EXCLUSIVE | LK_FAILRECLAIM);
526 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
529 } else if (dp->de_StartCluster == scn && isadir) {
530 vref(vdp); /* we want ourself, ie "." */
533 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
537 cnp->cn_flags |= CNP_PDIRUNLOCK;
545 * dep - directory entry to copy into the directory
546 * ddep - directory to add to
547 * depp - return the address of the denode for the created directory entry
549 * cnp - componentname needed for Win95 long filenames
552 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
553 struct componentname *cnp)
556 u_long dirclust, diroffset;
557 struct direntry *ndep;
558 struct msdosfsmount *pmp = ddep->de_pmp;
564 kprintf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
565 dep, ddep, depp, cnp);
569 * If no space left in the directory then allocate another cluster
570 * and chain it onto the end of the file. There is one exception
571 * to this. That is, if the root directory has no more space it
572 * can NOT be expanded. extendfile() checks for and fails attempts
573 * to extend the root directory. We just return an error in that
576 if (ddep->de_fndoffset >= ddep->de_FileSize) {
577 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
579 dirclust = de_clcount(pmp, diroffset);
580 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
582 detrunc(ddep, ddep->de_FileSize, 0);
587 * Update the size of the directory
589 ddep->de_FileSize += de_cn2off(pmp, dirclust);
593 * We just read in the cluster with space. Copy the new directory
594 * entry in. Then write it to disk. NOTE: DOS directories
595 * do not get smaller as clusters are emptied.
597 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
598 &bn, &dirclust, &blsize);
601 diroffset = ddep->de_fndoffset;
602 if (dirclust != MSDOSFSROOT)
603 diroffset &= pmp->pm_crbomask;
604 if ((error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp)) != 0) {
608 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
610 DE_EXTERNALIZE(ndep, dep);
613 * Now write the Win95 long name
615 if (ddep->de_fndcnt > 0) {
616 u_int8_t chksum = winChksum(ndep->deName);
617 const u_char *un = (const u_char *)cnp->cn_nameptr;
618 int unlen = cnp->cn_namelen;
621 while (--ddep->de_fndcnt >= 0) {
622 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
623 if ((error = bwrite(bp)) != 0)
626 ddep->de_fndoffset -= sizeof(struct direntry);
634 error = bread(pmp->pm_devvp,
635 de_bntodoff(pmp, bn), blsize,
641 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
644 ddep->de_fndoffset -= sizeof(struct direntry);
646 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
653 if ((error = bwrite(bp)) != 0)
657 * If they want us to return with the denode gotten.
660 if (dep->de_Attributes & ATTR_DIRECTORY) {
661 dirclust = dep->de_StartCluster;
662 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
663 dirclust = MSDOSFSROOT;
664 if (dirclust == MSDOSFSROOT)
665 diroffset = MSDOSFSROOT_OFS;
669 return deget(pmp, dirclust, diroffset, depp);
676 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
677 * return 0 if not empty or error.
680 dosdirempty(struct denode *dep)
687 struct msdosfsmount *pmp = dep->de_pmp;
688 struct direntry *dentp;
691 * Since the filesize field in directory entries for a directory is
692 * zero, we just have to feel our way through the directory until
693 * we hit end of file.
695 for (cn = 0;; cn++) {
696 if ((error = pcbmap(dep, cn, &bn, NULL, &blsize)) != 0) {
698 return (1); /* it's empty */
701 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
706 for (dentp = (struct direntry *)bp->b_data;
707 (char *)dentp < bp->b_data + blsize;
709 if (dentp->deName[0] != SLOT_DELETED &&
710 (dentp->deAttributes & ATTR_VOLUME) == 0) {
712 * In dos directories an entry whose name
713 * starts with SLOT_EMPTY (0) starts the
714 * beginning of the unused part of the
715 * directory, so we can just return that it
718 if (dentp->deName[0] == SLOT_EMPTY) {
723 * Any names other than "." and ".." in a
724 * directory mean it is not empty.
726 if (bcmp(dentp->deName, ". ", 11) &&
727 bcmp(dentp->deName, ".. ", 11)) {
730 kprintf("dosdirempty(): entry found %02x, %02x\n",
731 dentp->deName[0], dentp->deName[1]);
733 return (0); /* not empty */
743 * Check to see if the directory described by target is in some
744 * subdirectory of source. This prevents something like the following from
745 * succeeding and leaving a bunch or files and directories orphaned. mv
746 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
748 * source - the inode for /a/b/c
749 * target - the inode for /a/b/c/d/e/f
751 * Returns 0 if target is NOT a subdirectory of source.
752 * Otherwise returns a non-zero error number.
753 * The target inode is always unlocked on return.
756 doscheckpath(struct denode *source, struct denode *target)
759 struct msdosfsmount *pmp;
762 struct buf *bp = NULL;
766 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
767 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
771 if (dep->de_StartCluster == source->de_StartCluster) {
775 if (dep->de_StartCluster == MSDOSFSROOT)
779 if (pmp != source->de_pmp)
780 panic("doscheckpath: source and target on different filesystems");
782 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
786 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
790 scn = dep->de_StartCluster;
791 error = bread(pmp->pm_devvp, xcntodoff(pmp, scn),
792 pmp->pm_bpcluster, &bp);
796 ep = (struct direntry *) bp->b_data + 1;
797 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
798 bcmp(ep->deName, ".. ", 11) != 0) {
802 scn = getushort(ep->deStartCluster);
804 scn |= getushort(ep->deHighClust) << 16;
806 if (scn == source->de_StartCluster) {
810 if (scn == MSDOSFSROOT)
812 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
814 * scn should be 0 in this case,
815 * but we silently ignore the error.
823 /* NOTE: deget() clears dep on error */
824 if ((error = deget(pmp, scn, 0, &dep)) != 0)
830 if (error == ENOTDIR)
831 kprintf("doscheckpath(): .. not a directory?\n");
838 * Read in the disk block containing the directory entry (dirclu, dirofs)
839 * and return the address of the buf header, and the address of the
840 * directory entry within the block.
843 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
844 struct buf **bpp, struct direntry **epp)
850 blsize = pmp->pm_bpcluster;
851 if (dirclust == MSDOSFSROOT
852 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
853 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
854 bn = detobn(pmp, dirclust, diroffset);
855 if ((error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, bpp)) != 0) {
861 *epp = bptoep(pmp, *bpp, diroffset);
866 * Read in the disk block containing the directory entry dep came from and
867 * return the address of the buf header, and the address of the directory
868 * entry within the block.
871 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
873 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
878 * Remove a directory entry. At this point the file represented by the
879 * directory entry to be removed is still full length until noone has it
880 * open. When the file no longer being used msdosfs_inactive() is called
881 * and will truncate the file to 0 length. When the vnode containing the
882 * denode is needed for some other purpose by VFS it will call
883 * msdosfs_reclaim() which will remove the denode from the denode cache.
886 removede(struct denode *pdep, /* directory where the entry is removed */
887 struct denode *dep) /* file to be removed */
894 struct msdosfsmount *pmp = pdep->de_pmp;
895 u_long offset = pdep->de_fndoffset;
898 kprintf("removede(): filename %s, dep %p, offset %08lx\n",
899 dep->de_Name, dep, offset);
902 KKASSERT(dep->de_refcnt > 0);
904 offset += sizeof(struct direntry);
906 offset -= sizeof(struct direntry);
907 error = pcbmap(pdep, de_cluster(pmp, offset),
911 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
916 ep = bptoep(pmp, bp, offset);
918 * Check whether, if we came here the second time, i.e.
919 * when underflowing into the previous block, the last
920 * entry in this block is a longfilename entry, too.
922 if (ep->deAttributes != ATTR_WIN95
923 && offset != pdep->de_fndoffset) {
927 offset += sizeof(struct direntry);
930 * We are a bit agressive here in that we delete any Win95
931 * entries preceding this entry, not just the ones we "own".
932 * Since these presumably aren't valid anyway,
933 * there should be no harm.
935 offset -= sizeof(struct direntry);
936 ep--->deName[0] = SLOT_DELETED;
937 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
938 || !(offset & pmp->pm_crbomask)
939 || ep->deAttributes != ATTR_WIN95)
942 if ((error = bwrite(bp)) != 0)
944 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
945 && !(offset & pmp->pm_crbomask)
951 * Create a unique DOS name in dvp
954 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
956 struct msdosfsmount *pmp = dep->de_pmp;
957 struct direntry *dentp;
965 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
966 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
967 cnp->cn_namelen, 0, pmp) ?
970 for (gen = 1;; gen++) {
972 * Generate DOS name with generation number
974 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
975 cnp->cn_namelen, gen, pmp))
976 return gen == 1 ? EINVAL : EEXIST;
979 * Now look for a dir entry with this exact name
981 for (cn = error = 0; !error; cn++) {
982 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
983 if (error == E2BIG) /* EOF reached and not found */
987 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn),
993 for (dentp = (struct direntry *)bp->b_data;
994 (char *)dentp < bp->b_data + blsize;
996 if (dentp->deName[0] == SLOT_EMPTY) {
998 * Last used entry and not found
1004 * Ignore volume labels and Win95 entries
1006 if (dentp->deAttributes & ATTR_VOLUME)
1008 if (!bcmp(dentp->deName, cp, 11)) {
1019 * Find any Win'95 long filename entry in directory dep
1022 findwin95(struct denode *dep)
1024 struct msdosfsmount *pmp = dep->de_pmp;
1025 struct direntry *dentp;
1033 * Read through the directory looking for Win'95 entries
1034 * Note: Error currently handled just as EOF XXX
1036 for (cn = 0;; cn++) {
1037 if (pcbmap(dep, cn, &bn, 0, &blsize))
1039 if (bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp)) {
1043 for (dentp = (struct direntry *)bp->b_data;
1044 (char *)dentp < bp->b_data + blsize;
1046 if (dentp->deName[0] == SLOT_EMPTY) {
1048 * Last used entry and not found
1053 if (dentp->deName[0] == SLOT_DELETED) {
1055 * Ignore deleted files
1056 * Note: might be an indication of Win'95 anyway XXX
1060 if (dentp->deAttributes == ATTR_WIN95) {