2 /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
53 #include <sys/param.h>
54 #include <sys/systm.h>
56 #include <sys/mount.h>
57 #include <sys/namei.h>
58 #include <sys/vnode.h>
60 #include <fs/msdosfs/bpb.h>
61 #include <fs/msdosfs/direntry.h>
62 #include <fs/msdosfs/denode.h>
63 #include <fs/msdosfs/fat.h>
64 #include <fs/msdosfs/msdosfsmount.h>
66 static int msdosfs_lookup_(struct vnode *vdp, struct vnode **vpp,
67 struct componentname *cnp, uint64_t *inum);
70 msdosfs_lookup(struct vop_cachedlookup_args *ap)
73 return (msdosfs_lookup_(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
82 msdosfs_deget_dotdot(struct mount *mp, void *arg, int lkflags,
85 struct deget_dotdot *dd_arg;
87 struct msdosfsmount *pmp;
90 pmp = VFSTOMSDOSFS(mp);
92 error = deget(pmp, dd_arg->cluster, dd_arg->blkoff, &rdp);
99 * When we search a directory the blocks containing directory entries are
100 * read and examined. The directory entries contain information that would
101 * normally be in the inode of a unix filesystem. This means that some of
102 * a directory's contents may also be in memory resident denodes (sort of
103 * an inode). This can cause problems if we are searching while some other
104 * process is modifying a directory. To prevent one process from accessing
105 * incompletely modified directory information we depend upon being the
106 * sole owner of a directory block. bread/brelse provide this service.
107 * This being the case, when a process modifies a directory it must first
108 * acquire the disk block that contains the directory entry to be modified.
109 * Then update the disk block and the denode, and then write the disk block
110 * out to disk. This way disk blocks containing directory entries and in
111 * memory denode's will be in synch.
114 msdosfs_lookup_(struct vnode *vdp, struct vnode **vpp,
115 struct componentname *cnp, uint64_t *dd_inum)
127 int isadir; /* ~0 if found direntry is a directory */
128 u_long scn; /* starting cluster number */
132 struct msdosfsmount *pmp;
133 struct buf *bp = NULL;
134 struct direntry *dep = NULL;
135 struct deget_dotdot dd_arg;
136 u_char dosfilename[12];
137 int flags = cnp->cn_flags;
138 int nameiop = cnp->cn_nameiop;
143 int chksum = -1, chksum_ok;
147 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
152 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
153 vdp, dp, dp->de_Attributes);
160 * If they are going after the . or .. entry in the root directory,
161 * they won't find it. DOS filesystems don't have them in the root
162 * directory. So, we fake it. deget() is in on this scam too.
164 if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
165 (cnp->cn_namelen == 1 ||
166 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
167 isadir = ATTR_DIRECTORY;
170 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
172 cluster = MSDOSFSROOT;
173 blkoff = MSDOSFSROOT_OFS;
177 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
178 cnp->cn_namelen, 0, pmp)) {
184 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
185 cnp->cn_namelen, pmp) + 1;
189 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
190 cnp->cn_namelen, pmp) + 1;
193 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
197 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
200 * Suppress search for slots unless creating
201 * file and at end of pathname, in which case
202 * we watch for a place to put the new file in
203 * case it doesn't already exist.
206 if ((nameiop == CREATE || nameiop == RENAME) &&
211 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
212 dosfilename, cnp->cn_namelen);
215 * Search the directory pointed at by vdp for the name pointed at
216 * by cnp->cn_nameptr.
221 * The outer loop ranges over the clusters that make up the
222 * directory. Note that the root directory is different from all
223 * other directories. It has a fixed number of blocks that are not
224 * part of the pool of allocatable clusters. So, we treat it a
225 * little differently. The root directory starts at "cluster" 0.
228 for (frcn = 0;; frcn++) {
229 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
235 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
240 for (blkoff = 0; blkoff < blsize;
241 blkoff += sizeof(struct direntry),
242 diroff += sizeof(struct direntry)) {
243 dep = (struct direntry *)(bp->b_data + blkoff);
245 * If the slot is empty and we are still looking
246 * for an empty then remember this one. If the
247 * slot is not empty then check to see if it
248 * matches what we are looking for. If the slot
249 * has never been filled with anything, then the
250 * remainder of the directory has never been used,
251 * so there is no point in searching it.
253 if (dep->deName[0] == SLOT_EMPTY ||
254 dep->deName[0] == SLOT_DELETED) {
256 * Drop memory of previous long matches
261 if (slotcount < wincnt) {
265 if (dep->deName[0] == SLOT_EMPTY) {
271 * If there wasn't enough space for our winentries,
272 * forget about the empty space
274 if (slotcount < wincnt)
278 * Check for Win95 long filename entry
280 if (dep->deAttributes == ATTR_WIN95) {
281 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
284 chksum = win2unixfn(&nb,
285 (struct winentry *)dep, chksum,
290 chksum = winChkName(&nb,
291 (const u_char *)cnp->cn_nameptr, unlen,
299 * Ignore volume labels (anywhere, not just
300 * the root directory).
302 if (dep->deAttributes & ATTR_VOLUME) {
308 * Check for a checksum or name match
310 chksum_ok = (chksum == winChksum(dep->deName));
312 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
317 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
321 * Remember where this directory
322 * entry came from for whoever did
325 dp->de_fndoffset = diroff;
326 if (chksum_ok && nameiop == RENAME) {
328 * Target had correct long name
329 * directory entries, reuse them
332 dp->de_fndcnt = wincnt - 1;
335 * Long name directory entries
336 * not present or corrupt, can only
337 * reuse dos directory entry.
344 } /* for (blkoff = 0; .... */
346 * Release the buffer holding the directory cluster just
350 } /* for (frcn = 0; ; frcn++) */
354 * We hold no disk buffers at this point.
358 * Fixup the slot description to point to the place where
359 * we might put the new DOS direntry (putting the Win95
360 * long name entries before that)
366 if (wincnt > slotcount)
367 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
370 * If we get here we didn't find the entry we were looking for. But
371 * that's ok if we are creating or renaming and are at the end of
372 * the pathname and the directory hasn't been removed.
375 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
376 nameiop, dp->de_refcnt);
377 printf(" slotcount %d, slotoffset %d\n",
378 slotcount, slotoffset);
380 if ((nameiop == CREATE || nameiop == RENAME) &&
381 (flags & ISLASTCN) && dp->de_refcnt != 0) {
383 * Access for write is interpreted as allowing
384 * creation of files in the directory.
386 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
390 * Return an indication of where the new directory
391 * entry should be put.
393 dp->de_fndoffset = slotoffset;
394 dp->de_fndcnt = wincnt - 1;
397 * We return with the directory locked, so that
398 * the parameters we set up above will still be
399 * valid if we actually decide to do a direnter().
400 * We return ni_vp == NULL to indicate that the entry
401 * does not currently exist; we leave a pointer to
402 * the (locked) directory inode in ndp->ni_dvp.
403 * The pathname buffer is saved so that the name
404 * can be obtained later.
406 * NB - if the directory is unlocked, then this
407 * information cannot be used.
409 cnp->cn_flags |= SAVENAME;
410 return (EJUSTRETURN);
414 * Insert name into cache (as non-existent) if appropriate.
416 * XXX Negative caching is broken for msdosfs because the name
417 * cache doesn't understand peculiarities such as case insensitivity
418 * and 8.3 filenames. Hence, it may not invalidate all negative
419 * entries if a file with this name is later created.
421 if ((cnp->cn_flags & MAKEENTRY) != 0)
422 cache_enter(vdp, *vpp, cnp);
428 * NOTE: We still have the buffer with matched directory entry at
431 isadir = dep->deAttributes & ATTR_DIRECTORY;
432 scn = getushort(dep->deStartCluster);
434 scn |= getushort(dep->deHighClust) << 16;
435 if (scn == pmp->pm_rootdirblk) {
437 * There should actually be 0 here.
438 * Just ignore the error.
446 if (cluster == MSDOSFSROOT)
447 blkoff = MSDOSFSROOT_OFS;
450 } else if (cluster == MSDOSFSROOT)
454 * Now release buf to allow deget to read the entry again.
455 * Reserving it here and giving it to deget could result
463 * If we entered at foundroot, then we are looking for the . or ..
464 * entry of the filesystems root directory. isadir and scn were
465 * setup before jumping here. And, bp is already null.
467 if (FAT32(pmp) && scn == MSDOSFSROOT)
468 scn = pmp->pm_rootdirblk;
470 if (dd_inum != NULL) {
471 *dd_inum = (uint64_t)pmp->pm_bpcluster * scn + blkoff;
476 * If deleting, and at end of pathname, return
477 * parameters which can be used to remove file.
479 if (nameiop == DELETE && (flags & ISLASTCN)) {
481 * Don't allow deleting the root.
483 if (blkoff == MSDOSFSROOT_OFS)
487 * Write access to directory required to delete files.
489 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
494 * Return pointer to current entry in dp->i_offset.
495 * Save directory inode pointer in ndp->ni_dvp for dirremove().
497 if (dp->de_StartCluster == scn && isadir) { /* "." */
502 error = deget(pmp, cluster, blkoff, &tdp);
510 * If rewriting (RENAME), return the inode and the
511 * information required to rewrite the present directory
512 * Must get inode of directory entry to verify it's a
513 * regular file, or empty directory.
515 if (nameiop == RENAME && (flags & ISLASTCN)) {
516 if (blkoff == MSDOSFSROOT_OFS)
519 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
524 * Careful about locking second inode.
525 * This can only occur if the target is ".".
527 if (dp->de_StartCluster == scn && isadir)
530 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
533 cnp->cn_flags |= SAVENAME;
538 * Step through the translation in the name. We do not `vput' the
539 * directory because we may need it again if a symbolic link
540 * is relative to the current directory. Instead we save it
541 * unlocked as "pdp". We must get the target inode before unlocking
542 * the directory to insure that the inode will not be removed
543 * before we get it. We prevent deadlock by always fetching
544 * inodes from the root, moving down the directory tree. Thus
545 * when following backward pointers ".." we must unlock the
546 * parent directory before getting the requested directory.
549 if (flags & ISDOTDOT) {
550 dd_arg.cluster = cluster;
551 dd_arg.blkoff = blkoff;
552 error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot,
553 &dd_arg, cnp->cn_lkflags, vpp);
559 * Recheck that ".." still points to the inode we
560 * looked up before pdp lock was dropped.
562 error = msdosfs_lookup_(pdp, NULL, cnp, &inode1);
568 if (VTODE(*vpp)->de_inode != inode1) {
572 } else if (dp->de_StartCluster == scn && isadir) {
573 VREF(vdp); /* we want ourself, ie "." */
576 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
582 * Insert name into cache if appropriate.
584 if (cnp->cn_flags & MAKEENTRY)
585 cache_enter(vdp, *vpp, cnp);
590 * dep - directory entry to copy into the directory
591 * ddep - directory to add to
592 * depp - return the address of the denode for the created directory entry
594 * cnp - componentname needed for Win95 long filenames
597 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
598 struct componentname *cnp)
601 u_long dirclust, diroffset;
602 struct direntry *ndep;
603 struct msdosfsmount *pmp = ddep->de_pmp;
609 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
610 dep, ddep, depp, cnp);
614 * If no space left in the directory then allocate another cluster
615 * and chain it onto the end of the file. There is one exception
616 * to this. That is, if the root directory has no more space it
617 * can NOT be expanded. extendfile() checks for and fails attempts
618 * to extend the root directory. We just return an error in that
621 if (ddep->de_fndoffset >= ddep->de_FileSize) {
622 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
624 dirclust = de_clcount(pmp, diroffset);
625 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
627 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
632 * Update the size of the directory
634 ddep->de_FileSize += de_cn2off(pmp, dirclust);
638 * We just read in the cluster with space. Copy the new directory
639 * entry in. Then write it to disk. NOTE: DOS directories
640 * do not get smaller as clusters are emptied.
642 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
643 &bn, &dirclust, &blsize);
646 diroffset = ddep->de_fndoffset;
647 if (dirclust != MSDOSFSROOT)
648 diroffset &= pmp->pm_crbomask;
649 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
653 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
655 DE_EXTERNALIZE(ndep, dep);
658 * Now write the Win95 long name
660 if (ddep->de_fndcnt > 0) {
661 uint8_t chksum = winChksum(ndep->deName);
662 const u_char *un = (const u_char *)cnp->cn_nameptr;
663 int unlen = cnp->cn_namelen;
666 while (--ddep->de_fndcnt >= 0) {
667 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
668 if (DOINGASYNC(DETOV(ddep)))
670 else if ((error = bwrite(bp)) != 0)
673 ddep->de_fndoffset -= sizeof(struct direntry);
681 error = bread(pmp->pm_devvp, bn, blsize,
687 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
690 ddep->de_fndoffset -= sizeof(struct direntry);
692 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
698 if (DOINGASYNC(DETOV(ddep)))
700 else if ((error = bwrite(bp)) != 0)
704 * If they want us to return with the denode gotten.
707 if (dep->de_Attributes & ATTR_DIRECTORY) {
708 dirclust = dep->de_StartCluster;
709 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
710 dirclust = MSDOSFSROOT;
711 if (dirclust == MSDOSFSROOT)
712 diroffset = MSDOSFSROOT_OFS;
716 return deget(pmp, dirclust, diroffset, depp);
723 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
724 * return 0 if not empty or error.
727 dosdirempty(struct denode *dep)
734 struct msdosfsmount *pmp = dep->de_pmp;
735 struct direntry *dentp;
738 * Since the filesize field in directory entries for a directory is
739 * zero, we just have to feel our way through the directory until
740 * we hit end of file.
742 for (cn = 0;; cn++) {
743 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
745 return (1); /* it's empty */
748 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
753 for (dentp = (struct direntry *)bp->b_data;
754 (char *)dentp < bp->b_data + blsize;
756 if (dentp->deName[0] != SLOT_DELETED &&
757 (dentp->deAttributes & ATTR_VOLUME) == 0) {
759 * In dos directories an entry whose name
760 * starts with SLOT_EMPTY (0) starts the
761 * beginning of the unused part of the
762 * directory, so we can just return that it
765 if (dentp->deName[0] == SLOT_EMPTY) {
770 * Any names other than "." and ".." in a
771 * directory mean it is not empty.
773 if (bcmp(dentp->deName, ". ", 11) &&
774 bcmp(dentp->deName, ".. ", 11)) {
777 printf("dosdirempty(): entry found %02x, %02x\n",
778 dentp->deName[0], dentp->deName[1]);
780 return (0); /* not empty */
790 * Check to see if the directory described by target is in some
791 * subdirectory of source. This prevents something like the following from
792 * succeeding and leaving a bunch or files and directories orphaned. mv
793 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
795 * source - the inode for /a/b/c
796 * target - the inode for /a/b/c/d/e/f
798 * Returns 0 if target is NOT a subdirectory of source.
799 * Otherwise returns a non-zero error number.
800 * The target inode is always unlocked on return.
803 doscheckpath(struct denode *source, struct denode *target)
806 struct msdosfsmount *pmp;
809 struct buf *bp = NULL;
813 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
814 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
818 if (dep->de_StartCluster == source->de_StartCluster) {
822 if (dep->de_StartCluster == MSDOSFSROOT)
826 if (pmp != source->de_pmp)
827 panic("doscheckpath: source and target on different filesystems");
829 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
833 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
837 scn = dep->de_StartCluster;
838 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
839 pmp->pm_bpcluster, NOCRED, &bp);
843 ep = (struct direntry *) bp->b_data + 1;
844 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
845 bcmp(ep->deName, ".. ", 11) != 0) {
849 scn = getushort(ep->deStartCluster);
851 scn |= getushort(ep->deHighClust) << 16;
853 if (scn == source->de_StartCluster) {
857 if (scn == MSDOSFSROOT)
859 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
861 * scn should be 0 in this case,
862 * but we silently ignore the error.
870 /* NOTE: deget() clears dep on error */
871 if ((error = deget(pmp, scn, 0, &dep)) != 0)
878 if (error == ENOTDIR)
879 printf("doscheckpath(): .. not a directory?\n");
887 * Read in the disk block containing the directory entry (dirclu, dirofs)
888 * and return the address of the buf header, and the address of the
889 * directory entry within the block.
892 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
893 struct buf **bpp, struct direntry **epp)
899 blsize = pmp->pm_bpcluster;
900 if (dirclust == MSDOSFSROOT
901 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
902 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
903 bn = detobn(pmp, dirclust, diroffset);
904 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
910 *epp = bptoep(pmp, *bpp, diroffset);
915 * Read in the disk block containing the directory entry dep came from and
916 * return the address of the buf header, and the address of the directory
917 * entry within the block.
920 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
923 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
928 * Remove a directory entry. At this point the file represented by the
929 * directory entry to be removed is still full length until no one has it
930 * open. When the file no longer being used msdosfs_inactive() is called
931 * and will truncate the file to 0 length. When the vnode containing the
932 * denode is needed for some other purpose by VFS it will call
933 * msdosfs_reclaim() which will remove the denode from the denode cache.
935 * pdep directory where the entry is removed
936 * dep file to be removed
939 removede(struct denode *pdep, struct denode *dep)
946 struct msdosfsmount *pmp = pdep->de_pmp;
947 u_long offset = pdep->de_fndoffset;
950 printf("removede(): filename %s, dep %p, offset %08lx\n",
951 dep->de_Name, dep, offset);
955 offset += sizeof(struct direntry);
957 offset -= sizeof(struct direntry);
958 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
961 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
966 ep = bptoep(pmp, bp, offset);
968 * Check whether, if we came here the second time, i.e.
969 * when underflowing into the previous block, the last
970 * entry in this block is a longfilename entry, too.
972 if (ep->deAttributes != ATTR_WIN95
973 && offset != pdep->de_fndoffset) {
977 offset += sizeof(struct direntry);
980 * We are a bit aggressive here in that we delete any Win95
981 * entries preceding this entry, not just the ones we "own".
982 * Since these presumably aren't valid anyway,
983 * there should be no harm.
985 offset -= sizeof(struct direntry);
986 ep--->deName[0] = SLOT_DELETED;
987 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
988 || !(offset & pmp->pm_crbomask)
989 || ep->deAttributes != ATTR_WIN95)
992 if (DOINGASYNC(DETOV(pdep)))
994 else if ((error = bwrite(bp)) != 0)
996 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
997 && !(offset & pmp->pm_crbomask)
1003 * Create a unique DOS name in dvp
1006 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
1008 struct msdosfsmount *pmp = dep->de_pmp;
1009 struct direntry *dentp;
1017 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1018 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1019 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
1021 for (gen = 1;; gen++) {
1023 * Generate DOS name with generation number
1025 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1026 cnp->cn_namelen, gen, pmp))
1027 return gen == 1 ? EINVAL : EEXIST;
1030 * Now look for a dir entry with this exact name
1032 for (cn = error = 0; !error; cn++) {
1033 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1034 if (error == E2BIG) /* EOF reached and not found */
1038 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &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
1054 * Ignore volume labels and Win95 entries
1056 if (dentp->deAttributes & ATTR_VOLUME)
1058 if (!bcmp(dentp->deName, cp, 11)) {