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);
239 for (blkoff = 0; blkoff < blsize;
240 blkoff += sizeof(struct direntry),
241 diroff += sizeof(struct direntry)) {
242 dep = (struct direntry *)(bp->b_data + blkoff);
244 * If the slot is empty and we are still looking
245 * for an empty then remember this one. If the
246 * slot is not empty then check to see if it
247 * matches what we are looking for. If the slot
248 * has never been filled with anything, then the
249 * remainder of the directory has never been used,
250 * so there is no point in searching it.
252 if (dep->deName[0] == SLOT_EMPTY ||
253 dep->deName[0] == SLOT_DELETED) {
255 * Drop memory of previous long matches
260 if (slotcount < wincnt) {
264 if (dep->deName[0] == SLOT_EMPTY) {
270 * If there wasn't enough space for our winentries,
271 * forget about the empty space
273 if (slotcount < wincnt)
277 * Check for Win95 long filename entry
279 if (dep->deAttributes == ATTR_WIN95) {
280 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
283 chksum = win2unixfn(&nb,
284 (struct winentry *)dep, chksum,
289 chksum = winChkName(&nb,
290 (const u_char *)cnp->cn_nameptr, unlen,
298 * Ignore volume labels (anywhere, not just
299 * the root directory).
301 if (dep->deAttributes & ATTR_VOLUME) {
307 * Check for a checksum or name match
309 chksum_ok = (chksum == winChksum(dep->deName));
311 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
316 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
320 * Remember where this directory
321 * entry came from for whoever did
324 dp->de_fndoffset = diroff;
325 if (chksum_ok && nameiop == RENAME) {
327 * Target had correct long name
328 * directory entries, reuse them
331 dp->de_fndcnt = wincnt - 1;
334 * Long name directory entries
335 * not present or corrupt, can only
336 * reuse dos directory entry.
343 } /* for (blkoff = 0; .... */
345 * Release the buffer holding the directory cluster just
349 } /* for (frcn = 0; ; frcn++) */
353 * We hold no disk buffers at this point.
357 * Fixup the slot description to point to the place where
358 * we might put the new DOS direntry (putting the Win95
359 * long name entries before that)
365 if (wincnt > slotcount)
366 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
369 * If we get here we didn't find the entry we were looking for. But
370 * that's ok if we are creating or renaming and are at the end of
371 * the pathname and the directory hasn't been removed.
374 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
375 nameiop, dp->de_refcnt);
376 printf(" slotcount %d, slotoffset %d\n",
377 slotcount, slotoffset);
379 if ((nameiop == CREATE || nameiop == RENAME) &&
380 (flags & ISLASTCN) && dp->de_refcnt != 0) {
382 * Access for write is interpreted as allowing
383 * creation of files in the directory.
385 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
389 * Return an indication of where the new directory
390 * entry should be put.
392 dp->de_fndoffset = slotoffset;
393 dp->de_fndcnt = wincnt - 1;
396 * We return with the directory locked, so that
397 * the parameters we set up above will still be
398 * valid if we actually decide to do a direnter().
399 * We return ni_vp == NULL to indicate that the entry
400 * does not currently exist; we leave a pointer to
401 * the (locked) directory inode in ndp->ni_dvp.
402 * The pathname buffer is saved so that the name
403 * can be obtained later.
405 * NB - if the directory is unlocked, then this
406 * information cannot be used.
408 cnp->cn_flags |= SAVENAME;
409 return (EJUSTRETURN);
413 * Insert name into cache (as non-existent) if appropriate.
415 * XXX Negative caching is broken for msdosfs because the name
416 * cache doesn't understand peculiarities such as case insensitivity
417 * and 8.3 filenames. Hence, it may not invalidate all negative
418 * entries if a file with this name is later created.
420 if ((cnp->cn_flags & MAKEENTRY) != 0)
421 cache_enter(vdp, *vpp, cnp);
427 * NOTE: We still have the buffer with matched directory entry at
430 isadir = dep->deAttributes & ATTR_DIRECTORY;
431 scn = getushort(dep->deStartCluster);
433 scn |= getushort(dep->deHighClust) << 16;
434 if (scn == pmp->pm_rootdirblk) {
436 * There should actually be 0 here.
437 * Just ignore the error.
445 if (cluster == MSDOSFSROOT)
446 blkoff = MSDOSFSROOT_OFS;
449 } else if (cluster == MSDOSFSROOT)
453 * Now release buf to allow deget to read the entry again.
454 * Reserving it here and giving it to deget could result
462 * If we entered at foundroot, then we are looking for the . or ..
463 * entry of the filesystems root directory. isadir and scn were
464 * setup before jumping here. And, bp is already null.
466 if (FAT32(pmp) && scn == MSDOSFSROOT)
467 scn = pmp->pm_rootdirblk;
469 if (dd_inum != NULL) {
470 *dd_inum = (uint64_t)pmp->pm_bpcluster * scn + blkoff;
475 * If deleting, and at end of pathname, return
476 * parameters which can be used to remove file.
478 if (nameiop == DELETE && (flags & ISLASTCN)) {
480 * Don't allow deleting the root.
482 if (blkoff == MSDOSFSROOT_OFS)
486 * Write access to directory required to delete files.
488 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
493 * Return pointer to current entry in dp->i_offset.
494 * Save directory inode pointer in ndp->ni_dvp for dirremove().
496 if (dp->de_StartCluster == scn && isadir) { /* "." */
501 error = deget(pmp, cluster, blkoff, &tdp);
509 * If rewriting (RENAME), return the inode and the
510 * information required to rewrite the present directory
511 * Must get inode of directory entry to verify it's a
512 * regular file, or empty directory.
514 if (nameiop == RENAME && (flags & ISLASTCN)) {
515 if (blkoff == MSDOSFSROOT_OFS)
518 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
523 * Careful about locking second inode.
524 * This can only occur if the target is ".".
526 if (dp->de_StartCluster == scn && isadir)
529 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
532 cnp->cn_flags |= SAVENAME;
537 * Step through the translation in the name. We do not `vput' the
538 * directory because we may need it again if a symbolic link
539 * is relative to the current directory. Instead we save it
540 * unlocked as "pdp". We must get the target inode before unlocking
541 * the directory to insure that the inode will not be removed
542 * before we get it. We prevent deadlock by always fetching
543 * inodes from the root, moving down the directory tree. Thus
544 * when following backward pointers ".." we must unlock the
545 * parent directory before getting the requested directory.
548 if (flags & ISDOTDOT) {
549 dd_arg.cluster = cluster;
550 dd_arg.blkoff = blkoff;
551 error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot,
552 &dd_arg, cnp->cn_lkflags, vpp);
558 * Recheck that ".." still points to the inode we
559 * looked up before pdp lock was dropped.
561 error = msdosfs_lookup_(pdp, NULL, cnp, &inode1);
567 if (VTODE(*vpp)->de_inode != inode1) {
571 } else if (dp->de_StartCluster == scn && isadir) {
572 VREF(vdp); /* we want ourself, ie "." */
575 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
581 * Insert name into cache if appropriate.
583 if (cnp->cn_flags & MAKEENTRY)
584 cache_enter(vdp, *vpp, cnp);
589 * dep - directory entry to copy into the directory
590 * ddep - directory to add to
591 * depp - return the address of the denode for the created directory entry
593 * cnp - componentname needed for Win95 long filenames
596 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
597 struct componentname *cnp)
600 u_long dirclust, diroffset;
601 struct direntry *ndep;
602 struct msdosfsmount *pmp = ddep->de_pmp;
608 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
609 dep, ddep, depp, cnp);
613 * If no space left in the directory then allocate another cluster
614 * and chain it onto the end of the file. There is one exception
615 * to this. That is, if the root directory has no more space it
616 * can NOT be expanded. extendfile() checks for and fails attempts
617 * to extend the root directory. We just return an error in that
620 if (ddep->de_fndoffset >= ddep->de_FileSize) {
621 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
623 dirclust = de_clcount(pmp, diroffset);
624 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
626 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
631 * Update the size of the directory
633 ddep->de_FileSize += de_cn2off(pmp, dirclust);
637 * We just read in the cluster with space. Copy the new directory
638 * entry in. Then write it to disk. NOTE: DOS directories
639 * do not get smaller as clusters are emptied.
641 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
642 &bn, &dirclust, &blsize);
645 diroffset = ddep->de_fndoffset;
646 if (dirclust != MSDOSFSROOT)
647 diroffset &= pmp->pm_crbomask;
648 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
652 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
654 DE_EXTERNALIZE(ndep, dep);
657 * Now write the Win95 long name
659 if (ddep->de_fndcnt > 0) {
660 uint8_t chksum = winChksum(ndep->deName);
661 const u_char *un = (const u_char *)cnp->cn_nameptr;
662 int unlen = cnp->cn_namelen;
665 while (--ddep->de_fndcnt >= 0) {
666 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
667 if (DOINGASYNC(DETOV(ddep)))
669 else if ((error = bwrite(bp)) != 0)
672 ddep->de_fndoffset -= sizeof(struct direntry);
680 error = bread(pmp->pm_devvp, bn, blsize,
685 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
688 ddep->de_fndoffset -= sizeof(struct direntry);
690 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
696 if (DOINGASYNC(DETOV(ddep)))
698 else if ((error = bwrite(bp)) != 0)
702 * If they want us to return with the denode gotten.
705 if (dep->de_Attributes & ATTR_DIRECTORY) {
706 dirclust = dep->de_StartCluster;
707 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
708 dirclust = MSDOSFSROOT;
709 if (dirclust == MSDOSFSROOT)
710 diroffset = MSDOSFSROOT_OFS;
714 return deget(pmp, dirclust, diroffset, depp);
721 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
722 * return 0 if not empty or error.
725 dosdirempty(struct denode *dep)
732 struct msdosfsmount *pmp = dep->de_pmp;
733 struct direntry *dentp;
736 * Since the filesize field in directory entries for a directory is
737 * zero, we just have to feel our way through the directory until
738 * we hit end of file.
740 for (cn = 0;; cn++) {
741 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
743 return (1); /* it's empty */
746 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
750 for (dentp = (struct direntry *)bp->b_data;
751 (char *)dentp < bp->b_data + blsize;
753 if (dentp->deName[0] != SLOT_DELETED &&
754 (dentp->deAttributes & ATTR_VOLUME) == 0) {
756 * In dos directories an entry whose name
757 * starts with SLOT_EMPTY (0) starts the
758 * beginning of the unused part of the
759 * directory, so we can just return that it
762 if (dentp->deName[0] == SLOT_EMPTY) {
767 * Any names other than "." and ".." in a
768 * directory mean it is not empty.
770 if (bcmp(dentp->deName, ". ", 11) &&
771 bcmp(dentp->deName, ".. ", 11)) {
774 printf("dosdirempty(): entry found %02x, %02x\n",
775 dentp->deName[0], dentp->deName[1]);
777 return (0); /* not empty */
787 * Check to see if the directory described by target is in some
788 * subdirectory of source. This prevents something like the following from
789 * succeeding and leaving a bunch or files and directories orphaned. mv
790 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
792 * source - the inode for /a/b/c
793 * target - the inode for /a/b/c/d/e/f
795 * Returns 0 if target is NOT a subdirectory of source.
796 * Otherwise returns a non-zero error number.
797 * The target inode is always unlocked on return.
800 doscheckpath(struct denode *source, struct denode *target)
803 struct msdosfsmount *pmp;
806 struct buf *bp = NULL;
810 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
811 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
815 if (dep->de_StartCluster == source->de_StartCluster) {
819 if (dep->de_StartCluster == MSDOSFSROOT)
823 if (pmp != source->de_pmp)
824 panic("doscheckpath: source and target on different filesystems");
826 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
830 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
834 scn = dep->de_StartCluster;
835 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
836 pmp->pm_bpcluster, NOCRED, &bp);
840 ep = (struct direntry *) bp->b_data + 1;
841 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
842 bcmp(ep->deName, ".. ", 11) != 0) {
846 scn = getushort(ep->deStartCluster);
848 scn |= getushort(ep->deHighClust) << 16;
850 if (scn == source->de_StartCluster) {
854 if (scn == MSDOSFSROOT)
856 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
858 * scn should be 0 in this case,
859 * but we silently ignore the error.
867 /* NOTE: deget() clears dep on error */
868 if ((error = deget(pmp, scn, 0, &dep)) != 0)
875 if (error == ENOTDIR)
876 printf("doscheckpath(): .. not a directory?\n");
884 * Read in the disk block containing the directory entry (dirclu, dirofs)
885 * and return the address of the buf header, and the address of the
886 * directory entry within the block.
889 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
890 struct buf **bpp, struct direntry **epp)
896 blsize = pmp->pm_bpcluster;
897 if (dirclust == MSDOSFSROOT
898 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
899 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
900 bn = detobn(pmp, dirclust, diroffset);
901 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
907 *epp = bptoep(pmp, *bpp, diroffset);
912 * Read in the disk block containing the directory entry dep came from and
913 * return the address of the buf header, and the address of the directory
914 * entry within the block.
917 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
920 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
925 * Remove a directory entry. At this point the file represented by the
926 * directory entry to be removed is still full length until no one has it
927 * open. When the file no longer being used msdosfs_inactive() is called
928 * and will truncate the file to 0 length. When the vnode containing the
929 * denode is needed for some other purpose by VFS it will call
930 * msdosfs_reclaim() which will remove the denode from the denode cache.
932 * pdep directory where the entry is removed
933 * dep file to be removed
936 removede(struct denode *pdep, struct denode *dep)
943 struct msdosfsmount *pmp = pdep->de_pmp;
944 u_long offset = pdep->de_fndoffset;
947 printf("removede(): filename %s, dep %p, offset %08lx\n",
948 dep->de_Name, dep, offset);
952 offset += sizeof(struct direntry);
954 offset -= sizeof(struct direntry);
955 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
958 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
962 ep = bptoep(pmp, bp, offset);
964 * Check whether, if we came here the second time, i.e.
965 * when underflowing into the previous block, the last
966 * entry in this block is a longfilename entry, too.
968 if (ep->deAttributes != ATTR_WIN95
969 && offset != pdep->de_fndoffset) {
973 offset += sizeof(struct direntry);
976 * We are a bit aggressive here in that we delete any Win95
977 * entries preceding this entry, not just the ones we "own".
978 * Since these presumably aren't valid anyway,
979 * there should be no harm.
981 offset -= sizeof(struct direntry);
982 ep--->deName[0] = SLOT_DELETED;
983 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
984 || !(offset & pmp->pm_crbomask)
985 || ep->deAttributes != ATTR_WIN95)
988 if (DOINGASYNC(DETOV(pdep)))
990 else if ((error = bwrite(bp)) != 0)
992 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
993 && !(offset & pmp->pm_crbomask)
999 * Create a unique DOS name in dvp
1002 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
1004 struct msdosfsmount *pmp = dep->de_pmp;
1005 struct direntry *dentp;
1013 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1014 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1015 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
1017 for (gen = 1;; gen++) {
1019 * Generate DOS name with generation number
1021 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1022 cnp->cn_namelen, gen, pmp))
1023 return gen == 1 ? EINVAL : EEXIST;
1026 * Now look for a dir entry with this exact name
1028 for (cn = error = 0; !error; cn++) {
1029 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1030 if (error == E2BIG) /* EOF reached and not found */
1034 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1038 for (dentp = (struct direntry *)bp->b_data;
1039 (char *)dentp < bp->b_data + blsize;
1041 if (dentp->deName[0] == SLOT_EMPTY) {
1043 * Last used entry and not found
1049 * Ignore volume labels and Win95 entries
1051 if (dentp->deAttributes & ATTR_VOLUME)
1053 if (!bcmp(dentp->deName, cp, 11)) {