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>
67 msdosfs_lookup_checker(struct msdosfsmount *pmp, struct vnode *dvp,
68 struct denode *tdp, struct vnode **vpp)
75 * Lookup assumes that directory cannot be hardlinked.
76 * Corrupted msdosfs filesystem could break this assumption.
80 msdosfs_integrity_error(pmp);
90 msdosfs_lookup(struct vop_cachedlookup_args *ap)
93 return (msdosfs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL,
103 msdosfs_deget_dotdot(struct mount *mp, void *arg, int lkflags,
106 struct deget_dotdot *dd_arg;
108 struct msdosfsmount *pmp;
111 pmp = VFSTOMSDOSFS(mp);
113 error = deget(pmp, dd_arg->cluster, dd_arg->blkoff,
121 * When we search a directory the blocks containing directory entries are
122 * read and examined. The directory entries contain information that would
123 * normally be in the inode of a unix filesystem. This means that some of
124 * a directory's contents may also be in memory resident denodes (sort of
125 * an inode). This can cause problems if we are searching while some other
126 * process is modifying a directory. To prevent one process from accessing
127 * incompletely modified directory information we depend upon being the
128 * sole owner of a directory block. bread/brelse provide this service.
129 * This being the case, when a process modifies a directory it must first
130 * acquire the disk block that contains the directory entry to be modified.
131 * Then update the disk block and the denode, and then write the disk block
132 * out to disk. This way disk blocks containing directory entries and in
133 * memory denode's will be in synch.
136 msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname
137 *cnp, daddr_t *scnp, u_long *blkoffp)
149 int isadir; /* ~0 if found direntry is a directory */
150 daddr_t scn; /* starting cluster number */
154 struct msdosfsmount *pmp;
155 struct buf *bp = NULL;
156 struct direntry *dep = NULL;
157 struct deget_dotdot dd_arg;
158 u_char dosfilename[12];
159 int flags = cnp->cn_flags;
160 int nameiop = cnp->cn_nameiop;
165 int chksum = -1, chksum_ok;
169 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
174 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
175 vdp, dp, dp->de_Attributes);
182 * If they are going after the . or .. entry in the root directory,
183 * they won't find it. DOS filesystems don't have them in the root
184 * directory. So, we fake it. deget() is in on this scam too.
186 if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
187 (cnp->cn_namelen == 1 ||
188 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
189 isadir = ATTR_DIRECTORY;
192 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
194 cluster = MSDOSFSROOT;
195 blkoff = MSDOSFSROOT_OFS;
199 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
200 cnp->cn_namelen, 0, pmp)) {
206 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
207 cnp->cn_namelen, pmp) + 1;
211 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
212 cnp->cn_namelen, pmp) + 1;
215 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
219 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
222 * Suppress search for slots unless creating
223 * file and at end of pathname, in which case
224 * we watch for a place to put the new file in
225 * case it doesn't already exist.
228 if ((nameiop == CREATE || nameiop == RENAME) &&
233 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
234 dosfilename, cnp->cn_namelen);
237 * Search the directory pointed at by vdp for the name pointed at
238 * by cnp->cn_nameptr.
243 * The outer loop ranges over the clusters that make up the
244 * directory. Note that the root directory is different from all
245 * other directories. It has a fixed number of blocks that are not
246 * part of the pool of allocatable clusters. So, we treat it a
247 * little differently. The root directory starts at "cluster" 0.
250 for (frcn = 0;; frcn++) {
251 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
257 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
261 for (blkoff = 0; blkoff < blsize;
262 blkoff += sizeof(struct direntry),
263 diroff += sizeof(struct direntry)) {
264 dep = (struct direntry *)(bp->b_data + blkoff);
266 * If the slot is empty and we are still looking
267 * for an empty then remember this one. If the
268 * slot is not empty then check to see if it
269 * matches what we are looking for. If the slot
270 * has never been filled with anything, then the
271 * remainder of the directory has never been used,
272 * so there is no point in searching it.
274 if (dep->deName[0] == SLOT_EMPTY ||
275 dep->deName[0] == SLOT_DELETED) {
277 * Drop memory of previous long matches
282 if (slotcount < wincnt) {
286 if (dep->deName[0] == SLOT_EMPTY) {
292 * If there wasn't enough space for our winentries,
293 * forget about the empty space
295 if (slotcount < wincnt)
299 * Check for Win95 long filename entry
301 if (dep->deAttributes == ATTR_WIN95) {
302 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
305 chksum = win2unixfn(&nb,
306 (struct winentry *)dep, chksum,
311 chksum = winChkName(&nb,
312 (const u_char *)cnp->cn_nameptr, unlen,
320 * Ignore volume labels (anywhere, not just
321 * the root directory).
323 if (dep->deAttributes & ATTR_VOLUME) {
329 * Check for a checksum or name match
331 chksum_ok = (chksum == winChksum(dep->deName));
333 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
338 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
342 * Remember where this directory
343 * entry came from for whoever did
346 dp->de_fndoffset = diroff;
347 if (chksum_ok && nameiop == RENAME) {
349 * Target had correct long name
350 * directory entries, reuse them
353 dp->de_fndcnt = wincnt - 1;
356 * Long name directory entries
357 * not present or corrupt, can only
358 * reuse dos directory entry.
365 } /* for (blkoff = 0; .... */
367 * Release the buffer holding the directory cluster just
371 } /* for (frcn = 0; ; frcn++) */
375 * We hold no disk buffers at this point.
379 * Fixup the slot description to point to the place where
380 * we might put the new DOS direntry (putting the Win95
381 * long name entries before that)
387 if (wincnt > slotcount)
388 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
391 * If we get here we didn't find the entry we were looking for. But
392 * that's ok if we are creating or renaming and are at the end of
393 * the pathname and the directory hasn't been removed.
396 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
397 nameiop, dp->de_refcnt);
398 printf(" slotcount %d, slotoffset %d\n",
399 slotcount, slotoffset);
401 if ((nameiop == CREATE || nameiop == RENAME) &&
402 (flags & ISLASTCN) && dp->de_refcnt != 0) {
404 * Access for write is interpreted as allowing
405 * creation of files in the directory.
407 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, curthread);
411 * Return an indication of where the new directory
412 * entry should be put.
414 dp->de_fndoffset = slotoffset;
415 dp->de_fndcnt = wincnt - 1;
418 * We return with the directory locked, so that
419 * the parameters we set up above will still be
420 * valid if we actually decide to do a direnter().
421 * We return ni_vp == NULL to indicate that the entry
422 * does not currently exist; we leave a pointer to
423 * the (locked) directory inode in ndp->ni_dvp.
424 * The pathname buffer is saved so that the name
425 * can be obtained later.
427 * NB - if the directory is unlocked, then this
428 * information cannot be used.
430 cnp->cn_flags |= SAVENAME;
431 return (EJUSTRETURN);
435 * Insert name into cache (as non-existent) if appropriate.
437 * XXX Negative caching is broken for msdosfs because the name
438 * cache doesn't understand peculiarities such as case insensitivity
439 * and 8.3 filenames. Hence, it may not invalidate all negative
440 * entries if a file with this name is later created.
442 if ((cnp->cn_flags & MAKEENTRY) != 0)
443 cache_enter(vdp, *vpp, cnp);
449 * NOTE: We still have the buffer with matched directory entry at
452 isadir = dep->deAttributes & ATTR_DIRECTORY;
453 scn = getushort(dep->deStartCluster);
455 scn |= getushort(dep->deHighClust) << 16;
456 if (scn == pmp->pm_rootdirblk) {
458 * There should actually be 0 here.
459 * Just ignore the error.
467 if (cluster == MSDOSFSROOT)
468 blkoff = MSDOSFSROOT_OFS;
471 } else if (cluster == MSDOSFSROOT)
475 * Now release buf to allow deget to read the entry again.
476 * Reserving it here and giving it to deget could result
484 * If we entered at foundroot, then we are looking for the . or ..
485 * entry of the filesystems root directory. isadir and scn were
486 * setup before jumping here. And, bp is already null.
488 if (FAT32(pmp) && scn == MSDOSFSROOT)
489 scn = pmp->pm_rootdirblk;
498 * If deleting, and at end of pathname, return
499 * parameters which can be used to remove file.
501 if (nameiop == DELETE && (flags & ISLASTCN)) {
503 * Don't allow deleting the root.
505 if (blkoff == MSDOSFSROOT_OFS)
509 * Write access to directory required to delete files.
511 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, curthread);
516 * Return pointer to current entry in dp->i_offset.
517 * Save directory inode pointer in ndp->ni_dvp for dirremove().
519 if (dp->de_StartCluster == scn && isadir) { /* "." */
524 error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, &tdp);
527 return (msdosfs_lookup_checker(pmp, vdp, tdp, vpp));
531 * If rewriting (RENAME), return the inode and the
532 * information required to rewrite the present directory
533 * Must get inode of directory entry to verify it's a
534 * regular file, or empty directory.
536 if (nameiop == RENAME && (flags & ISLASTCN)) {
537 if (blkoff == MSDOSFSROOT_OFS)
540 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, curthread);
545 * Careful about locking second inode.
546 * This can only occur if the target is ".".
548 if (dp->de_StartCluster == scn && isadir)
551 if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE,
554 if ((error = msdosfs_lookup_checker(pmp, vdp, tdp, vpp))
557 cnp->cn_flags |= SAVENAME;
562 * Step through the translation in the name. We do not `vput' the
563 * directory because we may need it again if a symbolic link
564 * is relative to the current directory. Instead we save it
565 * unlocked as "pdp". We must get the target inode before unlocking
566 * the directory to insure that the inode will not be removed
567 * before we get it. We prevent deadlock by always fetching
568 * inodes from the root, moving down the directory tree. Thus
569 * when following backward pointers ".." we must unlock the
570 * parent directory before getting the requested directory.
573 if (flags & ISDOTDOT) {
574 dd_arg.cluster = cluster;
575 dd_arg.blkoff = blkoff;
576 error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot,
577 &dd_arg, cnp->cn_lkflags, vpp);
583 * Recheck that ".." still points to the inode we
584 * looked up before pdp lock was dropped.
586 error = msdosfs_lookup_ino(pdp, NULL, cnp, &scn, &blkoff);
592 if (FAT32(pmp) && scn == MSDOSFSROOT)
593 scn = pmp->pm_rootdirblk;
594 inode1 = scn * pmp->pm_bpcluster + blkoff;
595 if (VTODE(*vpp)->de_inode != inode1) {
599 error = msdosfs_lookup_checker(pmp, vdp, VTODE(*vpp), vpp);
602 } else if (dp->de_StartCluster == scn && isadir) {
603 if (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.') {
604 /* fs is corrupted, non-dot lookup returned dvp */
605 msdosfs_integrity_error(pmp);
608 VREF(vdp); /* we want ourself, ie "." */
611 if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE,
614 if ((error = msdosfs_lookup_checker(pmp, vdp, tdp, vpp)) != 0)
619 * Insert name into cache if appropriate.
621 if (cnp->cn_flags & MAKEENTRY)
622 cache_enter(vdp, *vpp, cnp);
627 * dep - directory entry to copy into the directory
628 * ddep - directory to add to
629 * depp - return the address of the denode for the created directory entry
631 * cnp - componentname needed for Win95 long filenames
634 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
635 struct componentname *cnp)
638 u_long dirclust, diroffset;
639 struct direntry *ndep;
640 struct msdosfsmount *pmp = ddep->de_pmp;
646 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
647 dep, ddep, depp, cnp);
651 * If no space left in the directory then allocate another cluster
652 * and chain it onto the end of the file. There is one exception
653 * to this. That is, if the root directory has no more space it
654 * can NOT be expanded. extendfile() checks for and fails attempts
655 * to extend the root directory. We just return an error in that
658 if (ddep->de_fndoffset >= ddep->de_FileSize) {
659 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
661 dirclust = de_clcount(pmp, diroffset);
662 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
664 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
669 * Update the size of the directory
671 ddep->de_FileSize += de_cn2off(pmp, dirclust);
675 * We just read in the cluster with space. Copy the new directory
676 * entry in. Then write it to disk. NOTE: DOS directories
677 * do not get smaller as clusters are emptied.
679 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
680 &bn, &dirclust, &blsize);
683 diroffset = ddep->de_fndoffset;
684 if (dirclust != MSDOSFSROOT)
685 diroffset &= pmp->pm_crbomask;
686 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
690 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
692 DE_EXTERNALIZE(ndep, dep);
695 * Now write the Win95 long name
697 if (ddep->de_fndcnt > 0) {
698 uint8_t chksum = winChksum(ndep->deName);
699 const u_char *un = (const u_char *)cnp->cn_nameptr;
700 int unlen = cnp->cn_namelen;
703 while (--ddep->de_fndcnt >= 0) {
704 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
705 if (DOINGASYNC(DETOV(ddep)))
707 else if ((error = bwrite(bp)) != 0)
710 ddep->de_fndoffset -= sizeof(struct direntry);
718 error = bread(pmp->pm_devvp, bn, blsize,
723 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
726 ddep->de_fndoffset -= sizeof(struct direntry);
728 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
734 if (DOINGASYNC(DETOV(ddep)))
736 else if ((error = bwrite(bp)) != 0)
740 * If they want us to return with the denode gotten.
743 if (dep->de_Attributes & ATTR_DIRECTORY) {
744 dirclust = dep->de_StartCluster;
745 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
746 dirclust = MSDOSFSROOT;
747 if (dirclust == MSDOSFSROOT)
748 diroffset = MSDOSFSROOT_OFS;
752 return (deget(pmp, dirclust, diroffset, LK_EXCLUSIVE, depp));
759 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
760 * return 0 if not empty or error.
763 dosdirempty(struct denode *dep)
770 struct msdosfsmount *pmp = dep->de_pmp;
771 struct direntry *dentp;
774 * Since the filesize field in directory entries for a directory is
775 * zero, we just have to feel our way through the directory until
776 * we hit end of file.
778 for (cn = 0;; cn++) {
779 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
781 return (1); /* it's empty */
784 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
788 for (dentp = (struct direntry *)bp->b_data;
789 (char *)dentp < bp->b_data + blsize;
791 if (dentp->deName[0] != SLOT_DELETED &&
792 (dentp->deAttributes & ATTR_VOLUME) == 0) {
794 * In dos directories an entry whose name
795 * starts with SLOT_EMPTY (0) starts the
796 * beginning of the unused part of the
797 * directory, so we can just return that it
800 if (dentp->deName[0] == SLOT_EMPTY) {
805 * Any names other than "." and ".." in a
806 * directory mean it is not empty.
808 if (bcmp(dentp->deName, ". ", 11) &&
809 bcmp(dentp->deName, ".. ", 11)) {
812 printf("dosdirempty(): entry found %02x, %02x\n",
813 dentp->deName[0], dentp->deName[1]);
815 return (0); /* not empty */
825 * Check to see if the directory described by target is in some
826 * subdirectory of source. This prevents something like the following from
827 * succeeding and leaving a bunch or files and directories orphaned. mv
828 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
830 * source - the inode for /a/b/c
831 * target - the inode for /a/b/c/d/e/f
833 * Returns 0 if target is NOT a subdirectory of source.
834 * Otherwise returns a non-zero error number.
837 doscheckpath(struct denode *source, struct denode *target, daddr_t *wait_scn)
840 struct msdosfsmount *pmp;
843 struct buf *bp = NULL;
848 pmp = target->de_pmp;
849 lockmgr_assert(&pmp->pm_checkpath_lock, KA_XLOCKED);
850 KASSERT(pmp == source->de_pmp,
851 ("doscheckpath: source and target on different filesystems"));
853 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
854 (source->de_Attributes & ATTR_DIRECTORY) == 0)
857 if (target->de_StartCluster == source->de_StartCluster)
860 if (target->de_StartCluster == MSDOSFSROOT ||
861 (FAT32(pmp) && target->de_StartCluster == pmp->pm_rootdirblk))
865 vget(DETOV(dep), LK_EXCLUSIVE);
867 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
871 scn = dep->de_StartCluster;
872 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
873 pmp->pm_bpcluster, NOCRED, &bp);
877 ep = (struct direntry *)bp->b_data + 1;
878 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
879 bcmp(ep->deName, ".. ", 11) != 0) {
885 scn = getushort(ep->deStartCluster);
887 scn |= getushort(ep->deHighClust) << 16;
890 if (scn == source->de_StartCluster) {
894 if (scn == MSDOSFSROOT)
896 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
898 * scn should be 0 in this case,
899 * but we silently ignore the error.
906 /* NOTE: deget() clears dep on error */
907 error = deget(pmp, scn, 0, LK_EXCLUSIVE | LK_NOWAIT, &dep);
914 if (error == ENOTDIR)
915 printf("doscheckpath(): .. not a directory?\n");
923 * Read in the disk block containing the directory entry (dirclu, dirofs)
924 * and return the address of the buf header, and the address of the
925 * directory entry within the block.
928 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
929 struct buf **bpp, struct direntry **epp)
935 blsize = pmp->pm_bpcluster;
936 if (dirclust == MSDOSFSROOT
937 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
938 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
939 bn = detobn(pmp, dirclust, diroffset);
940 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
946 *epp = bptoep(pmp, *bpp, diroffset);
951 * Read in the disk block containing the directory entry dep came from and
952 * return the address of the buf header, and the address of the directory
953 * entry within the block.
956 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
959 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
964 * Remove a directory entry. At this point the file represented by the
965 * directory entry to be removed is still full length until no one has it
966 * open. When the file no longer being used msdosfs_inactive() is called
967 * and will truncate the file to 0 length. When the vnode containing the
968 * denode is needed for some other purpose by VFS it will call
969 * msdosfs_reclaim() which will remove the denode from the denode cache.
971 * pdep directory where the entry is removed
972 * dep file to be removed
975 removede(struct denode *pdep, struct denode *dep)
982 struct msdosfsmount *pmp = pdep->de_pmp;
983 u_long offset = pdep->de_fndoffset;
986 printf("removede(): filename %s, dep %p, offset %08lx\n",
987 dep->de_Name, dep, offset);
991 offset += sizeof(struct direntry);
993 offset -= sizeof(struct direntry);
994 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
997 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1001 ep = bptoep(pmp, bp, offset);
1003 * Check whether, if we came here the second time, i.e.
1004 * when underflowing into the previous block, the last
1005 * entry in this block is a longfilename entry, too.
1007 if (ep->deAttributes != ATTR_WIN95
1008 && offset != pdep->de_fndoffset) {
1012 offset += sizeof(struct direntry);
1015 * We are a bit aggressive here in that we delete any Win95
1016 * entries preceding this entry, not just the ones we "own".
1017 * Since these presumably aren't valid anyway,
1018 * there should be no harm.
1020 offset -= sizeof(struct direntry);
1021 ep--->deName[0] = SLOT_DELETED;
1022 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1023 || !(offset & pmp->pm_crbomask)
1024 || ep->deAttributes != ATTR_WIN95)
1027 if (DOINGASYNC(DETOV(pdep)))
1029 else if ((error = bwrite(bp)) != 0)
1031 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1032 && !(offset & pmp->pm_crbomask)
1038 * Create a unique DOS name in dvp
1041 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
1043 struct msdosfsmount *pmp = dep->de_pmp;
1044 struct direntry *dentp;
1052 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1053 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1054 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
1056 for (gen = 1;; gen++) {
1058 * Generate DOS name with generation number
1060 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1061 cnp->cn_namelen, gen, pmp))
1062 return gen == 1 ? EINVAL : EEXIST;
1065 * Now look for a dir entry with this exact name
1067 for (cn = error = 0; !error; cn++) {
1068 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1069 if (error == E2BIG) /* EOF reached and not found */
1073 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1077 for (dentp = (struct direntry *)bp->b_data;
1078 (char *)dentp < bp->b_data + blsize;
1080 if (dentp->deName[0] == SLOT_EMPTY) {
1082 * Last used entry and not found
1088 * Ignore volume labels and Win95 entries
1090 if (dentp->deAttributes & ATTR_VOLUME)
1092 if (!bcmp(dentp->deName, cp, 11)) {