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/mount.h>
55 #include <sys/namei.h>
56 #include <sys/vnode.h>
58 #include <fs/msdosfs/bpb.h>
59 #include <fs/msdosfs/direntry.h>
60 #include <fs/msdosfs/denode.h>
61 #include <fs/msdosfs/fat.h>
62 #include <fs/msdosfs/msdosfsmount.h>
64 static int msdosfs_lookup_(struct vnode *vdp, struct vnode **vpp,
65 struct componentname *cnp, u_int64_t *inum);
66 static int msdosfs_deget_dotdot(struct vnode *vp, u_long cluster, int blkoff,
70 msdosfs_lookup(struct vop_cachedlookup_args *ap)
73 return (msdosfs_lookup_(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
77 * When we search a directory the blocks containing directory entries are
78 * read and examined. The directory entries contain information that would
79 * normally be in the inode of a unix filesystem. This means that some of
80 * a directory's contents may also be in memory resident denodes (sort of
81 * an inode). This can cause problems if we are searching while some other
82 * process is modifying a directory. To prevent one process from accessing
83 * incompletely modified directory information we depend upon being the
84 * sole owner of a directory block. bread/brelse provide this service.
85 * This being the case, when a process modifies a directory it must first
86 * acquire the disk block that contains the directory entry to be modified.
87 * Then update the disk block and the denode, and then write the disk block
88 * out to disk. This way disk blocks containing directory entries and in
89 * memory denode's will be in synch.
92 msdosfs_lookup_(struct vnode *vdp, struct vnode **vpp,
93 struct componentname *cnp, u_int64_t *dd_inum)
105 int isadir; /* ~0 if found direntry is a directory */
106 u_long scn; /* starting cluster number */
110 struct msdosfsmount *pmp;
112 struct direntry *dep = NULL;
113 u_char dosfilename[12];
114 int flags = cnp->cn_flags;
115 int nameiop = cnp->cn_nameiop;
120 int chksum = -1, chksum_ok;
124 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
129 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
130 vdp, dp, dp->de_Attributes);
137 * If they are going after the . or .. entry in the root directory,
138 * they won't find it. DOS filesystems don't have them in the root
139 * directory. So, we fake it. deget() is in on this scam too.
141 if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
142 (cnp->cn_namelen == 1 ||
143 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
144 isadir = ATTR_DIRECTORY;
147 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
149 cluster = MSDOSFSROOT;
150 blkoff = MSDOSFSROOT_OFS;
154 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
155 cnp->cn_namelen, 0, pmp)) {
161 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
162 cnp->cn_namelen, pmp) + 1;
166 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
167 cnp->cn_namelen, pmp) + 1;
170 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
174 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
177 * Suppress search for slots unless creating
178 * file and at end of pathname, in which case
179 * we watch for a place to put the new file in
180 * case it doesn't already exist.
183 if ((nameiop == CREATE || nameiop == RENAME) &&
188 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
189 dosfilename, cnp->cn_namelen);
192 * Search the directory pointed at by vdp for the name pointed at
193 * by cnp->cn_nameptr.
198 * The outer loop ranges over the clusters that make up the
199 * directory. Note that the root directory is different from all
200 * other directories. It has a fixed number of blocks that are not
201 * part of the pool of allocatable clusters. So, we treat it a
202 * little differently. The root directory starts at "cluster" 0.
205 for (frcn = 0;; frcn++) {
206 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
212 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
217 for (blkoff = 0; blkoff < blsize;
218 blkoff += sizeof(struct direntry),
219 diroff += sizeof(struct direntry)) {
220 dep = (struct direntry *)(bp->b_data + blkoff);
222 * If the slot is empty and we are still looking
223 * for an empty then remember this one. If the
224 * slot is not empty then check to see if it
225 * matches what we are looking for. If the slot
226 * has never been filled with anything, then the
227 * remainder of the directory has never been used,
228 * so there is no point in searching it.
230 if (dep->deName[0] == SLOT_EMPTY ||
231 dep->deName[0] == SLOT_DELETED) {
233 * Drop memory of previous long matches
238 if (slotcount < wincnt) {
242 if (dep->deName[0] == SLOT_EMPTY) {
248 * If there wasn't enough space for our winentries,
249 * forget about the empty space
251 if (slotcount < wincnt)
255 * Check for Win95 long filename entry
257 if (dep->deAttributes == ATTR_WIN95) {
258 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
261 chksum = win2unixfn(&nb,
262 (struct winentry *)dep, chksum,
267 chksum = winChkName(&nb,
268 (const u_char *)cnp->cn_nameptr, unlen,
276 * Ignore volume labels (anywhere, not just
277 * the root directory).
279 if (dep->deAttributes & ATTR_VOLUME) {
285 * Check for a checksum or name match
287 chksum_ok = (chksum == winChksum(dep->deName));
289 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
294 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
298 * Remember where this directory
299 * entry came from for whoever did
302 dp->de_fndoffset = diroff;
303 if (chksum_ok && nameiop == RENAME) {
305 * Target had correct long name
306 * directory entries, reuse them
309 dp->de_fndcnt = wincnt - 1;
312 * Long name directory entries
313 * not present or corrupt, can only
314 * reuse dos directory entry.
321 } /* for (blkoff = 0; .... */
323 * Release the buffer holding the directory cluster just
327 } /* for (frcn = 0; ; frcn++) */
331 * We hold no disk buffers at this point.
335 * Fixup the slot description to point to the place where
336 * we might put the new DOS direntry (putting the Win95
337 * long name entries before that)
343 if (wincnt > slotcount)
344 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
347 * If we get here we didn't find the entry we were looking for. But
348 * that's ok if we are creating or renaming and are at the end of
349 * the pathname and the directory hasn't been removed.
352 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
353 nameiop, dp->de_refcnt);
354 printf(" slotcount %d, slotoffset %d\n",
355 slotcount, slotoffset);
357 if ((nameiop == CREATE || nameiop == RENAME) &&
358 (flags & ISLASTCN) && dp->de_refcnt != 0) {
360 * Access for write is interpreted as allowing
361 * creation of files in the directory.
363 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
367 * Return an indication of where the new directory
368 * entry should be put.
370 dp->de_fndoffset = slotoffset;
371 dp->de_fndcnt = wincnt - 1;
374 * We return with the directory locked, so that
375 * the parameters we set up above will still be
376 * valid if we actually decide to do a direnter().
377 * We return ni_vp == NULL to indicate that the entry
378 * does not currently exist; we leave a pointer to
379 * the (locked) directory inode in ndp->ni_dvp.
380 * The pathname buffer is saved so that the name
381 * can be obtained later.
383 * NB - if the directory is unlocked, then this
384 * information cannot be used.
386 cnp->cn_flags |= SAVENAME;
387 return (EJUSTRETURN);
391 * Insert name into cache (as non-existent) if appropriate.
393 * XXX Negative caching is broken for msdosfs because the name
394 * cache doesn't understand peculiarities such as case insensitivity
395 * and 8.3 filenames. Hence, it may not invalidate all negative
396 * entries if a file with this name is later created.
398 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
399 cache_enter(vdp, *vpp, cnp);
405 * NOTE: We still have the buffer with matched directory entry at
408 isadir = dep->deAttributes & ATTR_DIRECTORY;
409 scn = getushort(dep->deStartCluster);
411 scn |= getushort(dep->deHighClust) << 16;
412 if (scn == pmp->pm_rootdirblk) {
414 * There should actually be 0 here.
415 * Just ignore the error.
423 if (cluster == MSDOSFSROOT)
424 blkoff = MSDOSFSROOT_OFS;
427 } else if (cluster == MSDOSFSROOT)
431 * Now release buf to allow deget to read the entry again.
432 * Reserving it here and giving it to deget could result
440 * If we entered at foundroot, then we are looking for the . or ..
441 * entry of the filesystems root directory. isadir and scn were
442 * setup before jumping here. And, bp is already null.
444 if (FAT32(pmp) && scn == MSDOSFSROOT)
445 scn = pmp->pm_rootdirblk;
447 if (dd_inum != NULL) {
448 *dd_inum = (uint64_t)pmp->pm_bpcluster * scn + blkoff;
453 * If deleting, and at end of pathname, return
454 * parameters which can be used to remove file.
456 if (nameiop == DELETE && (flags & ISLASTCN)) {
458 * Don't allow deleting the root.
460 if (blkoff == MSDOSFSROOT_OFS)
464 * Write access to directory required to delete files.
466 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
471 * Return pointer to current entry in dp->i_offset.
472 * Save directory inode pointer in ndp->ni_dvp for dirremove().
474 if (dp->de_StartCluster == scn && isadir) { /* "." */
479 error = deget(pmp, cluster, blkoff, &tdp);
487 * If rewriting (RENAME), return the inode and the
488 * information required to rewrite the present directory
489 * Must get inode of directory entry to verify it's a
490 * regular file, or empty directory.
492 if (nameiop == RENAME && (flags & ISLASTCN)) {
493 if (blkoff == MSDOSFSROOT_OFS)
496 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
501 * Careful about locking second inode.
502 * This can only occur if the target is ".".
504 if (dp->de_StartCluster == scn && isadir)
507 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
510 cnp->cn_flags |= SAVENAME;
515 * Step through the translation in the name. We do not `vput' the
516 * directory because we may need it again if a symbolic link
517 * is relative to the current directory. Instead we save it
518 * unlocked as "pdp". We must get the target inode before unlocking
519 * the directory to insure that the inode will not be removed
520 * before we get it. We prevent deadlock by always fetching
521 * inodes from the root, moving down the directory tree. Thus
522 * when following backward pointers ".." we must unlock the
523 * parent directory before getting the requested directory.
526 if (flags & ISDOTDOT) {
527 error = msdosfs_deget_dotdot(pdp, cluster, blkoff, vpp);
533 * Recheck that ".." still points to the inode we
534 * looked up before pdp lock was dropped.
536 error = msdosfs_lookup_(pdp, NULL, cnp, &inode1);
542 if (VTODE(*vpp)->de_inode != inode1) {
546 } else if (dp->de_StartCluster == scn && isadir) {
547 VREF(vdp); /* we want ourself, ie "." */
550 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
556 * Insert name into cache if appropriate.
558 if (cnp->cn_flags & MAKEENTRY)
559 cache_enter(vdp, *vpp, cnp);
564 msdosfs_deget_dotdot(struct vnode *vp, u_long cluster, int blkoff,
568 struct msdosfsmount *pmp;
573 pmp = VFSTOMSDOSFS(mp);
574 ltype = VOP_ISLOCKED(vp);
575 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
576 ("msdosfs_deget_dotdot: vp not locked"));
578 error = vfs_busy(mp, MBF_NOWAIT);
582 error = vfs_busy(mp, 0);
583 vn_lock(vp, ltype | LK_RETRY);
587 if (vp->v_iflag & VI_DOOMED) {
593 error = deget(pmp, cluster, blkoff, &rdp);
598 vn_lock(vp, ltype | LK_RETRY);
599 if (vp->v_iflag & VI_DOOMED) {
612 * dep - directory entry to copy into the directory
613 * ddep - directory to add to
614 * depp - return the address of the denode for the created directory entry
616 * cnp - componentname needed for Win95 long filenames
619 createde(dep, ddep, depp, cnp)
622 struct denode **depp;
623 struct componentname *cnp;
626 u_long dirclust, diroffset;
627 struct direntry *ndep;
628 struct msdosfsmount *pmp = ddep->de_pmp;
634 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
635 dep, ddep, depp, cnp);
639 * If no space left in the directory then allocate another cluster
640 * and chain it onto the end of the file. There is one exception
641 * to this. That is, if the root directory has no more space it
642 * can NOT be expanded. extendfile() checks for and fails attempts
643 * to extend the root directory. We just return an error in that
646 if (ddep->de_fndoffset >= ddep->de_FileSize) {
647 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
649 dirclust = de_clcount(pmp, diroffset);
650 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
652 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL);
657 * Update the size of the directory
659 ddep->de_FileSize += de_cn2off(pmp, dirclust);
663 * We just read in the cluster with space. Copy the new directory
664 * entry in. Then write it to disk. NOTE: DOS directories
665 * do not get smaller as clusters are emptied.
667 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
668 &bn, &dirclust, &blsize);
671 diroffset = ddep->de_fndoffset;
672 if (dirclust != MSDOSFSROOT)
673 diroffset &= pmp->pm_crbomask;
674 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
678 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
680 DE_EXTERNALIZE(ndep, dep);
683 * Now write the Win95 long name
685 if (ddep->de_fndcnt > 0) {
686 u_int8_t chksum = winChksum(ndep->deName);
687 const u_char *un = (const u_char *)cnp->cn_nameptr;
688 int unlen = cnp->cn_namelen;
691 while (--ddep->de_fndcnt >= 0) {
692 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
693 if (DETOV(ddep)->v_mount->mnt_flag & MNT_ASYNC)
695 else if ((error = bwrite(bp)) != 0)
698 ddep->de_fndoffset -= sizeof(struct direntry);
706 error = bread(pmp->pm_devvp, bn, blsize,
712 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
715 ddep->de_fndoffset -= sizeof(struct direntry);
717 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
723 if (DETOV(ddep)->v_mount->mnt_flag & MNT_ASYNC)
725 else if ((error = bwrite(bp)) != 0)
729 * If they want us to return with the denode gotten.
732 if (dep->de_Attributes & ATTR_DIRECTORY) {
733 dirclust = dep->de_StartCluster;
734 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
735 dirclust = MSDOSFSROOT;
736 if (dirclust == MSDOSFSROOT)
737 diroffset = MSDOSFSROOT_OFS;
741 return deget(pmp, dirclust, diroffset, depp);
748 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
749 * return 0 if not empty or error.
760 struct msdosfsmount *pmp = dep->de_pmp;
761 struct direntry *dentp;
764 * Since the filesize field in directory entries for a directory is
765 * zero, we just have to feel our way through the directory until
766 * we hit end of file.
768 for (cn = 0;; cn++) {
769 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
771 return (1); /* it's empty */
774 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
779 for (dentp = (struct direntry *)bp->b_data;
780 (char *)dentp < bp->b_data + blsize;
782 if (dentp->deName[0] != SLOT_DELETED &&
783 (dentp->deAttributes & ATTR_VOLUME) == 0) {
785 * In dos directories an entry whose name
786 * starts with SLOT_EMPTY (0) starts the
787 * beginning of the unused part of the
788 * directory, so we can just return that it
791 if (dentp->deName[0] == SLOT_EMPTY) {
796 * Any names other than "." and ".." in a
797 * directory mean it is not empty.
799 if (bcmp(dentp->deName, ". ", 11) &&
800 bcmp(dentp->deName, ".. ", 11)) {
803 printf("dosdirempty(): entry found %02x, %02x\n",
804 dentp->deName[0], dentp->deName[1]);
806 return (0); /* not empty */
816 * Check to see if the directory described by target is in some
817 * subdirectory of source. This prevents something like the following from
818 * succeeding and leaving a bunch or files and directories orphaned. mv
819 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
821 * source - the inode for /a/b/c
822 * target - the inode for /a/b/c/d/e/f
824 * Returns 0 if target is NOT a subdirectory of source.
825 * Otherwise returns a non-zero error number.
826 * The target inode is always unlocked on return.
829 doscheckpath(source, target)
830 struct denode *source;
831 struct denode *target;
834 struct msdosfsmount *pmp;
837 struct buf *bp = NULL;
841 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
842 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
846 if (dep->de_StartCluster == source->de_StartCluster) {
850 if (dep->de_StartCluster == MSDOSFSROOT)
854 if (pmp != source->de_pmp)
855 panic("doscheckpath: source and target on different filesystems");
857 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
861 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
865 scn = dep->de_StartCluster;
866 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
867 pmp->pm_bpcluster, NOCRED, &bp);
871 ep = (struct direntry *) bp->b_data + 1;
872 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
873 bcmp(ep->deName, ".. ", 11) != 0) {
877 scn = getushort(ep->deStartCluster);
879 scn |= getushort(ep->deHighClust) << 16;
881 if (scn == source->de_StartCluster) {
885 if (scn == MSDOSFSROOT)
887 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
889 * scn should be 0 in this case,
890 * but we silently ignore the error.
898 /* NOTE: deget() clears dep on error */
899 if ((error = deget(pmp, scn, 0, &dep)) != 0)
906 if (error == ENOTDIR)
907 printf("doscheckpath(): .. not a directory?\n");
915 * Read in the disk block containing the directory entry (dirclu, dirofs)
916 * and return the address of the buf header, and the address of the
917 * directory entry within the block.
920 readep(pmp, dirclust, diroffset, bpp, epp)
921 struct msdosfsmount *pmp;
922 u_long dirclust, diroffset;
924 struct direntry **epp;
930 blsize = pmp->pm_bpcluster;
931 if (dirclust == MSDOSFSROOT
932 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
933 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
934 bn = detobn(pmp, dirclust, diroffset);
935 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
941 *epp = bptoep(pmp, *bpp, diroffset);
946 * Read in the disk block containing the directory entry dep came from and
947 * return the address of the buf header, and the address of the directory
948 * entry within the block.
951 readde(dep, bpp, epp)
954 struct direntry **epp;
957 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
962 * Remove a directory entry. At this point the file represented by the
963 * directory entry to be removed is still full length until noone has it
964 * open. When the file no longer being used msdosfs_inactive() is called
965 * and will truncate the file to 0 length. When the vnode containing the
966 * denode is needed for some other purpose by VFS it will call
967 * msdosfs_reclaim() which will remove the denode from the denode cache.
971 struct denode *pdep; /* directory where the entry is removed */
972 struct denode *dep; /* file to be removed */
979 struct msdosfsmount *pmp = pdep->de_pmp;
980 u_long offset = pdep->de_fndoffset;
983 printf("removede(): filename %s, dep %p, offset %08lx\n",
984 dep->de_Name, dep, offset);
988 offset += sizeof(struct direntry);
990 offset -= sizeof(struct direntry);
991 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
994 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
999 ep = bptoep(pmp, bp, offset);
1001 * Check whether, if we came here the second time, i.e.
1002 * when underflowing into the previous block, the last
1003 * entry in this block is a longfilename entry, too.
1005 if (ep->deAttributes != ATTR_WIN95
1006 && offset != pdep->de_fndoffset) {
1010 offset += sizeof(struct direntry);
1013 * We are a bit agressive here in that we delete any Win95
1014 * entries preceding this entry, not just the ones we "own".
1015 * Since these presumably aren't valid anyway,
1016 * there should be no harm.
1018 offset -= sizeof(struct direntry);
1019 ep--->deName[0] = SLOT_DELETED;
1020 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1021 || !(offset & pmp->pm_crbomask)
1022 || ep->deAttributes != ATTR_WIN95)
1025 if (DETOV(pdep)->v_mount->mnt_flag & MNT_ASYNC)
1027 else if ((error = bwrite(bp)) != 0)
1029 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1030 && !(offset & pmp->pm_crbomask)
1036 * Create a unique DOS name in dvp
1039 uniqdosname(dep, cnp, cp)
1041 struct componentname *cnp;
1044 struct msdosfsmount *pmp = dep->de_pmp;
1045 struct direntry *dentp;
1053 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1054 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1055 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
1057 for (gen = 1;; gen++) {
1059 * Generate DOS name with generation number
1061 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1062 cnp->cn_namelen, gen, pmp))
1063 return gen == 1 ? EINVAL : EEXIST;
1066 * Now look for a dir entry with this exact name
1068 for (cn = error = 0; !error; cn++) {
1069 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1070 if (error == E2BIG) /* EOF reached and not found */
1074 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1079 for (dentp = (struct direntry *)bp->b_data;
1080 (char *)dentp < bp->b_data + blsize;
1082 if (dentp->deName[0] == SLOT_EMPTY) {
1084 * Last used entry and not found
1090 * Ignore volume labels and Win95 entries
1092 if (dentp->deAttributes & ATTR_VOLUME)
1094 if (!bcmp(dentp->deName, cp, 11)) {
1105 * Find any Win'95 long filename entry in directory dep
1111 struct msdosfsmount *pmp = dep->de_pmp;
1112 struct direntry *dentp;
1120 * Read through the directory looking for Win'95 entries
1121 * Note: Error currently handled just as EOF XXX
1123 for (cn = 0;; cn++) {
1124 if (pcbmap(dep, cn, &bn, 0, &blsize))
1126 if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) {
1130 for (dentp = (struct direntry *)bp->b_data;
1131 (char *)dentp < bp->b_data + blsize;
1133 if (dentp->deName[0] == SLOT_EMPTY) {
1135 * Last used entry and not found
1140 if (dentp->deName[0] == SLOT_DELETED) {
1142 * Ignore deleted files
1143 * Note: might be an indication of Win'95 anyway XXX
1147 if (dentp->deAttributes == ATTR_WIN95) {