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>
53 #include <sys/namei.h>
56 #include <sys/vnode.h>
57 #include <sys/mount.h>
59 #include <fs/msdosfs/bpb.h>
60 #include <fs/msdosfs/msdosfsmount.h>
61 #include <fs/msdosfs/direntry.h>
62 #include <fs/msdosfs/denode.h>
63 #include <fs/msdosfs/fat.h>
66 * When we search a directory the blocks containing directory entries are
67 * read and examined. The directory entries contain information that would
68 * normally be in the inode of a unix filesystem. This means that some of
69 * a directory's contents may also be in memory resident denodes (sort of
70 * an inode). This can cause problems if we are searching while some other
71 * process is modifying a directory. To prevent one process from accessing
72 * incompletely modified directory information we depend upon being the
73 * sole owner of a directory block. bread/brelse provide this service.
74 * This being the case, when a process modifies a directory it must first
75 * acquire the disk block that contains the directory entry to be modified.
76 * Then update the disk block and the denode, and then write the disk block
77 * out to disk. This way disk blocks containing directory entries and in
78 * memory denode's will be in synch.
82 struct vop_cachedlookup_args /* {
85 struct componentname *a_cnp;
88 struct vnode *vdp = ap->a_dvp;
89 struct vnode **vpp = ap->a_vpp;
90 struct componentname *cnp = ap->a_cnp;
100 int isadir; /* ~0 if found direntry is a directory */
101 u_long scn; /* starting cluster number */
105 struct msdosfsmount *pmp;
107 struct direntry *dep = NULL;
108 u_char dosfilename[12];
109 int flags = cnp->cn_flags;
110 int nameiop = cnp->cn_nameiop;
111 struct thread *td = cnp->cn_thread;
115 int chksum = -1, chksum_ok;
119 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
125 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
126 vdp, dp, dp->de_Attributes);
130 * If they are going after the . or .. entry in the root directory,
131 * they won't find it. DOS filesystems don't have them in the root
132 * directory. So, we fake it. deget() is in on this scam too.
134 if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
135 (cnp->cn_namelen == 1 ||
136 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
137 isadir = ATTR_DIRECTORY;
140 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
142 cluster = MSDOSFSROOT;
143 blkoff = MSDOSFSROOT_OFS;
147 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
148 cnp->cn_namelen, 0, pmp)) {
154 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
155 cnp->cn_namelen, pmp) + 1;
159 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
160 cnp->cn_namelen, pmp) + 1;
163 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
167 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
170 * Suppress search for slots unless creating
171 * file and at end of pathname, in which case
172 * we watch for a place to put the new file in
173 * case it doesn't already exist.
176 if ((nameiop == CREATE || nameiop == RENAME) &&
181 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
182 dosfilename, cnp->cn_namelen);
185 * Search the directory pointed at by vdp for the name pointed at
186 * by cnp->cn_nameptr.
191 * The outer loop ranges over the clusters that make up the
192 * directory. Note that the root directory is different from all
193 * other directories. It has a fixed number of blocks that are not
194 * part of the pool of allocatable clusters. So, we treat it a
195 * little differently. The root directory starts at "cluster" 0.
198 for (frcn = 0;; frcn++) {
199 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
205 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
210 for (blkoff = 0; blkoff < blsize;
211 blkoff += sizeof(struct direntry),
212 diroff += sizeof(struct direntry)) {
213 dep = (struct direntry *)(bp->b_data + blkoff);
215 * If the slot is empty and we are still looking
216 * for an empty then remember this one. If the
217 * slot is not empty then check to see if it
218 * matches what we are looking for. If the slot
219 * has never been filled with anything, then the
220 * remainder of the directory has never been used,
221 * so there is no point in searching it.
223 if (dep->deName[0] == SLOT_EMPTY ||
224 dep->deName[0] == SLOT_DELETED) {
226 * Drop memory of previous long matches
231 if (slotcount < wincnt) {
235 if (dep->deName[0] == SLOT_EMPTY) {
241 * If there wasn't enough space for our winentries,
242 * forget about the empty space
244 if (slotcount < wincnt)
248 * Check for Win95 long filename entry
250 if (dep->deAttributes == ATTR_WIN95) {
251 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
254 chksum = win2unixfn((struct winentry *)dep,
260 chksum = winChkName((const u_char *)cnp->cn_nameptr,
270 * Ignore volume labels (anywhere, not just
271 * the root directory).
273 if (dep->deAttributes & ATTR_VOLUME) {
279 * Check for a checksum or name match
281 chksum_ok = (chksum == winChksum(dep));
283 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
288 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
292 * Remember where this directory
293 * entry came from for whoever did
296 dp->de_fndoffset = diroff;
297 if (chksum_ok && nameiop == RENAME) {
299 * Target had correct long name
300 * directory entries, reuse them
303 dp->de_fndcnt = wincnt - 1;
306 * Long name directory entries
307 * not present or corrupt, can only
308 * reuse dos directory entry.
315 } /* for (blkoff = 0; .... */
317 * Release the buffer holding the directory cluster just
321 } /* for (frcn = 0; ; frcn++) */
325 * We hold no disk buffers at this point.
329 * Fixup the slot description to point to the place where
330 * we might put the new DOS direntry (putting the Win95
331 * long name entries before that)
337 if (wincnt > slotcount)
338 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
341 * If we get here we didn't find the entry we were looking for. But
342 * that's ok if we are creating or renaming and are at the end of
343 * the pathname and the directory hasn't been removed.
346 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
347 nameiop, dp->de_refcnt);
348 printf(" slotcount %d, slotoffset %d\n",
349 slotcount, slotoffset);
351 if ((nameiop == CREATE || nameiop == RENAME) &&
352 (flags & ISLASTCN) && dp->de_refcnt != 0) {
354 * Access for write is interpreted as allowing
355 * creation of files in the directory.
357 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
361 * Return an indication of where the new directory
362 * entry should be put.
364 dp->de_fndoffset = slotoffset;
365 dp->de_fndcnt = wincnt - 1;
368 * We return with the directory locked, so that
369 * the parameters we set up above will still be
370 * valid if we actually decide to do a direnter().
371 * We return ni_vp == NULL to indicate that the entry
372 * does not currently exist; we leave a pointer to
373 * the (locked) directory inode in ndp->ni_dvp.
374 * The pathname buffer is saved so that the name
375 * can be obtained later.
377 * NB - if the directory is unlocked, then this
378 * information cannot be used.
380 cnp->cn_flags |= SAVENAME;
381 return (EJUSTRETURN);
385 * Insert name into cache (as non-existent) if appropriate.
387 * XXX Negative caching is broken for msdosfs because the name
388 * cache doesn't understand peculiarities such as case insensitivity
389 * and 8.3 filenames. Hence, it may not invalidate all negative
390 * entries if a file with this name is later created.
392 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
393 cache_enter(vdp, *vpp, cnp);
399 * NOTE: We still have the buffer with matched directory entry at
402 isadir = dep->deAttributes & ATTR_DIRECTORY;
403 scn = getushort(dep->deStartCluster);
405 scn |= getushort(dep->deHighClust) << 16;
406 if (scn == pmp->pm_rootdirblk) {
408 * There should actually be 0 here.
409 * Just ignore the error.
417 if (cluster == MSDOSFSROOT)
418 blkoff = MSDOSFSROOT_OFS;
421 } else if (cluster == MSDOSFSROOT)
425 * Now release buf to allow deget to read the entry again.
426 * Reserving it here and giving it to deget could result
434 * If we entered at foundroot, then we are looking for the . or ..
435 * entry of the filesystems root directory. isadir and scn were
436 * setup before jumping here. And, bp is already null.
438 if (FAT32(pmp) && scn == MSDOSFSROOT)
439 scn = pmp->pm_rootdirblk;
442 * If deleting, and at end of pathname, return
443 * parameters which can be used to remove file.
445 if (nameiop == DELETE && (flags & ISLASTCN)) {
447 * Don't allow deleting the root.
449 if (blkoff == MSDOSFSROOT_OFS)
450 return EROFS; /* really? XXX */
453 * Write access to directory required to delete files.
455 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
460 * Return pointer to current entry in dp->i_offset.
461 * Save directory inode pointer in ndp->ni_dvp for dirremove().
463 if (dp->de_StartCluster == scn && isadir) { /* "." */
468 error = deget(pmp, cluster, blkoff, &tdp);
476 * If rewriting (RENAME), return the inode and the
477 * information required to rewrite the present directory
478 * Must get inode of directory entry to verify it's a
479 * regular file, or empty directory.
481 if (nameiop == RENAME && (flags & ISLASTCN)) {
482 if (blkoff == MSDOSFSROOT_OFS)
483 return EROFS; /* really? XXX */
485 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
490 * Careful about locking second inode.
491 * This can only occur if the target is ".".
493 if (dp->de_StartCluster == scn && isadir)
496 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
499 cnp->cn_flags |= SAVENAME;
504 * Step through the translation in the name. We do not `vput' the
505 * directory because we may need it again if a symbolic link
506 * is relative to the current directory. Instead we save it
507 * unlocked as "pdp". We must get the target inode before unlocking
508 * the directory to insure that the inode will not be removed
509 * before we get it. We prevent deadlock by always fetching
510 * inodes from the root, moving down the directory tree. Thus
511 * when following backward pointers ".." we must unlock the
512 * parent directory before getting the requested directory.
513 * There is a potential race condition here if both the current
514 * and parent directories are removed before the VFS_VGET for the
515 * inode associated with ".." returns. We hope that this occurs
516 * infrequently since we cannot avoid this race condition without
517 * implementing a sophisticated deadlock detection algorithm.
518 * Note also that this simple deadlock detection scheme will not
519 * work if the filesystem has any hard links other than ".."
520 * that point backwards in the directory structure.
523 if (flags & ISDOTDOT) {
524 VOP_UNLOCK(pdp, 0, td);
525 error = deget(pmp, cluster, blkoff, &tdp);
526 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
530 } else if (dp->de_StartCluster == scn && isadir) {
531 VREF(vdp); /* we want ourself, ie "." */
534 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
540 * Insert name into cache if appropriate.
542 if (cnp->cn_flags & MAKEENTRY)
543 cache_enter(vdp, *vpp, cnp);
548 * dep - directory entry to copy into the directory
549 * ddep - directory to add to
550 * depp - return the address of the denode for the created directory entry
552 * cnp - componentname needed for Win95 long filenames
555 createde(dep, ddep, depp, cnp)
558 struct denode **depp;
559 struct componentname *cnp;
562 u_long dirclust, diroffset;
563 struct direntry *ndep;
564 struct msdosfsmount *pmp = ddep->de_pmp;
570 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
571 dep, ddep, depp, cnp);
575 * If no space left in the directory then allocate another cluster
576 * and chain it onto the end of the file. There is one exception
577 * to this. That is, if the root directory has no more space it
578 * can NOT be expanded. extendfile() checks for and fails attempts
579 * to extend the root directory. We just return an error in that
582 if (ddep->de_fndoffset >= ddep->de_FileSize) {
583 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
585 dirclust = de_clcount(pmp, diroffset);
586 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
588 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL);
593 * Update the size of the directory
595 ddep->de_FileSize += de_cn2off(pmp, dirclust);
599 * We just read in the cluster with space. Copy the new directory
600 * entry in. Then write it to disk. NOTE: DOS directories
601 * do not get smaller as clusters are emptied.
603 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
604 &bn, &dirclust, &blsize);
607 diroffset = ddep->de_fndoffset;
608 if (dirclust != MSDOSFSROOT)
609 diroffset &= pmp->pm_crbomask;
610 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
614 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
616 DE_EXTERNALIZE(ndep, dep);
619 * Now write the Win95 long name
621 if (ddep->de_fndcnt > 0) {
622 u_int8_t chksum = winChksum(ndep);
623 const u_char *un = (const u_char *)cnp->cn_nameptr;
624 int unlen = cnp->cn_namelen;
627 while (--ddep->de_fndcnt >= 0) {
628 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
629 if ((error = bwrite(bp)) != 0)
632 ddep->de_fndoffset -= sizeof(struct direntry);
640 error = bread(pmp->pm_devvp, bn, blsize,
646 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
649 ddep->de_fndoffset -= sizeof(struct direntry);
651 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
657 if ((error = bwrite(bp)) != 0)
661 * If they want us to return with the denode gotten.
664 if (dep->de_Attributes & ATTR_DIRECTORY) {
665 dirclust = dep->de_StartCluster;
666 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
667 dirclust = MSDOSFSROOT;
668 if (dirclust == MSDOSFSROOT)
669 diroffset = MSDOSFSROOT_OFS;
673 return deget(pmp, dirclust, diroffset, depp);
680 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
681 * return 0 if not empty or error.
692 struct msdosfsmount *pmp = dep->de_pmp;
693 struct direntry *dentp;
696 * Since the filesize field in directory entries for a directory is
697 * zero, we just have to feel our way through the directory until
698 * we hit end of file.
700 for (cn = 0;; cn++) {
701 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
703 return (1); /* it's empty */
706 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
711 for (dentp = (struct direntry *)bp->b_data;
712 (char *)dentp < bp->b_data + blsize;
714 if (dentp->deName[0] != SLOT_DELETED &&
715 (dentp->deAttributes & ATTR_VOLUME) == 0) {
717 * In dos directories an entry whose name
718 * starts with SLOT_EMPTY (0) starts the
719 * beginning of the unused part of the
720 * directory, so we can just return that it
723 if (dentp->deName[0] == SLOT_EMPTY) {
728 * Any names other than "." and ".." in a
729 * directory mean it is not empty.
731 if (bcmp(dentp->deName, ". ", 11) &&
732 bcmp(dentp->deName, ".. ", 11)) {
735 printf("dosdirempty(): entry found %02x, %02x\n",
736 dentp->deName[0], dentp->deName[1]);
738 return (0); /* not empty */
748 * Check to see if the directory described by target is in some
749 * subdirectory of source. This prevents something like the following from
750 * succeeding and leaving a bunch or files and directories orphaned. mv
751 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
753 * source - the inode for /a/b/c
754 * target - the inode for /a/b/c/d/e/f
756 * Returns 0 if target is NOT a subdirectory of source.
757 * Otherwise returns a non-zero error number.
758 * The target inode is always unlocked on return.
761 doscheckpath(source, target)
762 struct denode *source;
763 struct denode *target;
766 struct msdosfsmount *pmp;
769 struct buf *bp = NULL;
773 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
774 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
778 if (dep->de_StartCluster == source->de_StartCluster) {
782 if (dep->de_StartCluster == MSDOSFSROOT)
786 if (pmp != source->de_pmp)
787 panic("doscheckpath: source and target on different filesystems");
789 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
793 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
797 scn = dep->de_StartCluster;
798 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
799 pmp->pm_bpcluster, NOCRED, &bp);
803 ep = (struct direntry *) bp->b_data + 1;
804 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
805 bcmp(ep->deName, ".. ", 11) != 0) {
809 scn = getushort(ep->deStartCluster);
811 scn |= getushort(ep->deHighClust) << 16;
813 if (scn == source->de_StartCluster) {
817 if (scn == MSDOSFSROOT)
819 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
821 * scn should be 0 in this case,
822 * but we silently ignore the error.
830 /* NOTE: deget() clears dep on error */
831 if ((error = deget(pmp, scn, 0, &dep)) != 0)
837 if (error == ENOTDIR)
838 printf("doscheckpath(): .. not a directory?\n");
845 * Read in the disk block containing the directory entry (dirclu, dirofs)
846 * and return the address of the buf header, and the address of the
847 * directory entry within the block.
850 readep(pmp, dirclust, diroffset, bpp, epp)
851 struct msdosfsmount *pmp;
852 u_long dirclust, diroffset;
854 struct direntry **epp;
860 blsize = pmp->pm_bpcluster;
861 if (dirclust == MSDOSFSROOT
862 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
863 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
864 bn = detobn(pmp, dirclust, diroffset);
865 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
871 *epp = bptoep(pmp, *bpp, diroffset);
876 * Read in the disk block containing the directory entry dep came from and
877 * return the address of the buf header, and the address of the directory
878 * entry within the block.
881 readde(dep, bpp, epp)
884 struct direntry **epp;
887 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
892 * Remove a directory entry. At this point the file represented by the
893 * directory entry to be removed is still full length until noone has it
894 * open. When the file no longer being used msdosfs_inactive() is called
895 * and will truncate the file to 0 length. When the vnode containing the
896 * denode is needed for some other purpose by VFS it will call
897 * msdosfs_reclaim() which will remove the denode from the denode cache.
901 struct denode *pdep; /* directory where the entry is removed */
902 struct denode *dep; /* file to be removed */
909 struct msdosfsmount *pmp = pdep->de_pmp;
910 u_long offset = pdep->de_fndoffset;
913 printf("removede(): filename %s, dep %p, offset %08lx\n",
914 dep->de_Name, dep, offset);
918 offset += sizeof(struct direntry);
920 offset -= sizeof(struct direntry);
921 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
924 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
929 ep = bptoep(pmp, bp, offset);
931 * Check whether, if we came here the second time, i.e.
932 * when underflowing into the previous block, the last
933 * entry in this block is a longfilename entry, too.
935 if (ep->deAttributes != ATTR_WIN95
936 && offset != pdep->de_fndoffset) {
940 offset += sizeof(struct direntry);
943 * We are a bit agressive here in that we delete any Win95
944 * entries preceding this entry, not just the ones we "own".
945 * Since these presumably aren't valid anyway,
946 * there should be no harm.
948 offset -= sizeof(struct direntry);
949 ep--->deName[0] = SLOT_DELETED;
950 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
951 || !(offset & pmp->pm_crbomask)
952 || ep->deAttributes != ATTR_WIN95)
955 if ((error = bwrite(bp)) != 0)
957 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
958 && !(offset & pmp->pm_crbomask)
964 * Create a unique DOS name in dvp
967 uniqdosname(dep, cnp, cp)
969 struct componentname *cnp;
972 struct msdosfsmount *pmp = dep->de_pmp;
973 struct direntry *dentp;
981 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
982 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
983 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
985 for (gen = 1;; gen++) {
987 * Generate DOS name with generation number
989 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
990 cnp->cn_namelen, gen, pmp))
991 return gen == 1 ? EINVAL : EEXIST;
994 * Now look for a dir entry with this exact name
996 for (cn = error = 0; !error; cn++) {
997 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
998 if (error == E2BIG) /* EOF reached and not found */
1002 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1007 for (dentp = (struct direntry *)bp->b_data;
1008 (char *)dentp < bp->b_data + blsize;
1010 if (dentp->deName[0] == SLOT_EMPTY) {
1012 * Last used entry and not found
1018 * Ignore volume labels and Win95 entries
1020 if (dentp->deAttributes & ATTR_VOLUME)
1022 if (!bcmp(dentp->deName, cp, 11)) {
1033 * Find any Win'95 long filename entry in directory dep
1039 struct msdosfsmount *pmp = dep->de_pmp;
1040 struct direntry *dentp;
1048 * Read through the directory looking for Win'95 entries
1049 * Note: Error currently handled just as EOF XXX
1051 for (cn = 0;; cn++) {
1052 if (pcbmap(dep, cn, &bn, 0, &blsize))
1054 if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) {
1058 for (dentp = (struct direntry *)bp->b_data;
1059 (char *)dentp < bp->b_data + blsize;
1061 if (dentp->deName[0] == SLOT_EMPTY) {
1063 * Last used entry and not found
1068 if (dentp->deName[0] == SLOT_DELETED) {
1070 * Ignore deleted files
1071 * Note: might be an indication of Win'95 anyway XXX
1075 if (dentp->deAttributes == ATTR_WIN95) {