1 /* $FreeBSD: src/sys/msdosfs/msdosfs_lookup.c,v 1.30.2.1 2000/11/03 15:55:39 bp Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_lookup.c,v 1.8 2004/03/01 06:33:21 dillon Exp $ */
3 /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/vnode.h>
57 #include <sys/namei.h>
58 #include <sys/mount.h>
63 #include "msdosfsmount.h"
67 * When we search a directory the blocks containing directory entries are
68 * read and examined. The directory entries contain information that would
69 * normally be in the inode of a unix filesystem. This means that some of
70 * a directory's contents may also be in memory resident denodes (sort of
71 * an inode). This can cause problems if we are searching while some other
72 * process is modifying a directory. To prevent one process from accessing
73 * incompletely modified directory information we depend upon being the
74 * sole owner of a directory block. bread/brelse provide this service.
75 * This being the case, when a process modifies a directory it must first
76 * acquire the disk block that contains the directory entry to be modified.
77 * Then update the disk block and the denode, and then write the disk block
78 * out to disk. This way disk blocks containing directory entries and in
79 * memory denode's will be in synch.
83 struct vop_cachedlookup_args /* {
86 struct componentname *a_cnp;
89 struct vnode *vdp = ap->a_dvp;
90 struct vnode **vpp = ap->a_vpp;
91 struct componentname *cnp = ap->a_cnp;
103 int isadir; /* ~0 if found direntry is a directory */
104 u_long scn; /* starting cluster number */
108 struct msdosfsmount *pmp;
110 struct direntry *dep = NULL;
111 u_char dosfilename[12];
112 int flags = cnp->cn_flags;
113 int nameiop = cnp->cn_nameiop;
114 struct thread *td = cnp->cn_td;
120 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
123 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
128 lockparent = flags & CNP_LOCKPARENT;
129 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
131 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
132 vdp, dp, dp->de_Attributes);
136 * If they are going after the . or .. entry in the root directory,
137 * they won't find it. DOS filesystems don't have them in the root
138 * directory. So, we fake it. deget() is in on this scam too.
140 if ((vdp->v_flag & VROOT) && cnp->cn_nameptr[0] == '.' &&
141 (cnp->cn_namelen == 1 ||
142 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
143 isadir = ATTR_DIRECTORY;
146 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
148 cluster = MSDOSFSROOT;
149 blkoff = MSDOSFSROOT_OFS;
153 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
155 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
156 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu)) {
162 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
163 cnp->cn_namelen) + 1;
167 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
168 cnp->cn_namelen) + 1;
171 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
175 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
178 * Suppress search for slots unless creating
179 * file and at end of pathname, in which case
180 * we watch for a place to put the new file in
181 * case it doesn't already exist.
184 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
185 (flags & CNP_ISLASTCN))
189 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
190 dosfilename, cnp->cn_namelen);
193 * Search the directory pointed at by vdp for the name pointed at
194 * 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, &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
237 if (slotcount < wincnt) {
241 if (dep->deName[0] == SLOT_EMPTY) {
247 * If there wasn't enough space for our winentries,
248 * forget about the empty space
250 if (slotcount < wincnt)
254 * Check for Win95 long filename entry
256 if (dep->deAttributes == ATTR_WIN95) {
257 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
260 chksum = winChkName((const u_char *)cnp->cn_nameptr,
262 (struct winentry *)dep,
264 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
266 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
272 * Ignore volume labels (anywhere, not just
273 * the root directory).
275 if (dep->deAttributes & ATTR_VOLUME) {
281 * Check for a checksum or name match
283 if (chksum != winChksum(dep->deName)
284 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
289 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
293 * Remember where this directory
294 * entry came from for whoever did
297 dp->de_fndoffset = diroff;
298 dp->de_fndcnt = wincnt - 1;
302 } /* for (blkoff = 0; .... */
304 * Release the buffer holding the directory cluster just
308 } /* for (frcn = 0; ; frcn++) */
312 * We hold no disk buffers at this point.
316 * Fixup the slot description to point to the place where
317 * we might put the new DOS direntry (putting the Win95
318 * long name entries before that)
324 if (wincnt > slotcount)
325 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
328 * If we get here we didn't find the entry we were looking for. But
329 * that's ok if we are creating or renaming and are at the end of
330 * the pathname and the directory hasn't been removed.
333 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
334 nameiop, dp->de_refcnt);
335 printf(" slotcount %d, slotoffset %d\n",
336 slotcount, slotoffset);
338 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
339 (flags & CNP_ISLASTCN) && dp->de_refcnt != 0) {
341 * Access for write is interpreted as allowing
342 * creation of files in the directory.
344 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_td);
348 * Return an indication of where the new directory
349 * entry should be put.
351 dp->de_fndoffset = slotoffset;
352 dp->de_fndcnt = wincnt - 1;
355 * We return with the directory locked, so that
356 * the parameters we set up above will still be
357 * valid if we actually decide to do a direnter().
358 * We return ni_vp == NULL to indicate that the entry
359 * does not currently exist; we leave a pointer to
360 * the (locked) directory inode in ndp->ni_dvp.
361 * The pathname buffer is saved so that the name
362 * can be obtained later.
364 * NB - if the directory is unlocked, then this
365 * information cannot be used.
367 cnp->cn_flags |= CNP_SAVENAME;
369 VOP_UNLOCK(vdp, NULL, 0, td);
370 cnp->cn_flags |= CNP_PDIRUNLOCK;
372 return (EJUSTRETURN);
375 * Insert name into cache (as non-existent) if appropriate.
377 if ((cnp->cn_flags & CNP_MAKEENTRY) && nameiop != NAMEI_CREATE)
378 cache_enter(vdp, NCPNULL, *vpp, cnp);
383 * NOTE: We still have the buffer with matched directory entry at
386 isadir = dep->deAttributes & ATTR_DIRECTORY;
387 scn = getushort(dep->deStartCluster);
389 scn |= getushort(dep->deHighClust) << 16;
390 if (scn == pmp->pm_rootdirblk) {
392 * There should actually be 0 here.
393 * Just ignore the error.
401 if (cluster == MSDOSFSROOT)
402 blkoff = MSDOSFSROOT_OFS;
405 } else if (cluster == MSDOSFSROOT)
409 * Now release buf to allow deget to read the entry again.
410 * Reserving it here and giving it to deget could result
418 * If we entered at foundroot, then we are looking for the . or ..
419 * entry of the filesystems root directory. isadir and scn were
420 * setup before jumping here. And, bp is already null.
422 if (FAT32(pmp) && scn == MSDOSFSROOT)
423 scn = pmp->pm_rootdirblk;
426 * If deleting, and at end of pathname, return
427 * parameters which can be used to remove file.
428 * If the wantparent flag isn't set, we return only
429 * the directory (in ndp->ni_dvp), otherwise we go
430 * on and lock the inode, being careful with ".".
432 if (nameiop == NAMEI_DELETE && (flags & CNP_ISLASTCN)) {
434 * Don't allow deleting the root.
436 if (blkoff == MSDOSFSROOT_OFS)
437 return EROFS; /* really? XXX */
440 * Write access to directory required to delete files.
442 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_td);
447 * Return pointer to current entry in dp->i_offset.
448 * Save directory inode pointer in ndp->ni_dvp for dirremove().
450 if (dp->de_StartCluster == scn && isadir) { /* "." */
455 error = deget(pmp, cluster, blkoff, &tdp);
460 VOP_UNLOCK(vdp, NULL, 0, td);
461 cnp->cn_flags |= CNP_PDIRUNLOCK;
467 * If rewriting (RENAME), return the inode and the
468 * information required to rewrite the present directory
469 * Must get inode of directory entry to verify it's a
470 * regular file, or empty directory.
472 if (nameiop == NAMEI_RENAME && wantparent &&
473 (flags & CNP_ISLASTCN)) {
474 if (blkoff == MSDOSFSROOT_OFS)
475 return EROFS; /* really? XXX */
477 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_td);
482 * Careful about locking second inode.
483 * This can only occur if the target is ".".
485 if (dp->de_StartCluster == scn && isadir)
488 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
491 cnp->cn_flags |= CNP_SAVENAME;
493 VOP_UNLOCK(vdp, NULL, 0, td);
494 cnp->cn_flags |= CNP_PDIRUNLOCK;
500 * Step through the translation in the name. We do not `vput' the
501 * directory because we may need it again if a symbolic link
502 * is relative to the current directory. Instead we save it
503 * unlocked as "pdp". We must get the target inode before unlocking
504 * the directory to insure that the inode will not be removed
505 * before we get it. We prevent deadlock by always fetching
506 * inodes from the root, moving down the directory tree. Thus
507 * when following backward pointers ".." we must unlock the
508 * parent directory before getting the requested directory.
509 * There is a potential race condition here if both the current
510 * and parent directories are removed before the VFS_VGET for the
511 * inode associated with ".." returns. We hope that this occurs
512 * infrequently since we cannot avoid this race condition without
513 * implementing a sophisticated deadlock detection algorithm.
514 * Note also that this simple deadlock detection scheme will not
515 * work if the file system has any hard links other than ".."
516 * that point backwards in the directory structure.
519 if (flags & CNP_ISDOTDOT) {
520 VOP_UNLOCK(pdp, NULL, 0, td);
521 cnp->cn_flags |= CNP_PDIRUNLOCK;
522 error = deget(pmp, cluster, blkoff, &tdp);
524 vn_lock(pdp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
525 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
528 if (lockparent && (flags & CNP_ISLASTCN)) {
529 error = vn_lock(pdp, NULL, LK_EXCLUSIVE, td);
534 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
537 } else if (dp->de_StartCluster == scn && isadir) {
538 VREF(vdp); /* we want ourself, ie "." */
541 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
543 if (!lockparent || !(flags & CNP_ISLASTCN)) {
544 VOP_UNLOCK(pdp, NULL, 0, td);
545 cnp->cn_flags |= CNP_PDIRUNLOCK;
551 * Insert name into cache if appropriate.
553 if (cnp->cn_flags & CNP_MAKEENTRY)
554 cache_enter(vdp, NCPNULL, *vpp, cnp);
559 * dep - directory entry to copy into the directory
560 * ddep - directory to add to
561 * depp - return the address of the denode for the created directory entry
563 * cnp - componentname needed for Win95 long filenames
566 createde(dep, ddep, depp, cnp)
569 struct denode **depp;
570 struct componentname *cnp;
573 u_long dirclust, diroffset;
574 struct direntry *ndep;
575 struct msdosfsmount *pmp = ddep->de_pmp;
581 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
582 dep, ddep, depp, cnp);
586 * If no space left in the directory then allocate another cluster
587 * and chain it onto the end of the file. There is one exception
588 * to this. That is, if the root directory has no more space it
589 * can NOT be expanded. extendfile() checks for and fails attempts
590 * to extend the root directory. We just return an error in that
593 if (ddep->de_fndoffset >= ddep->de_FileSize) {
594 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
596 dirclust = de_clcount(pmp, diroffset);
597 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
599 (void)detrunc(ddep, ddep->de_FileSize, 0, NULL);
604 * Update the size of the directory
606 ddep->de_FileSize += de_cn2off(pmp, dirclust);
610 * We just read in the cluster with space. Copy the new directory
611 * entry in. Then write it to disk. NOTE: DOS directories
612 * do not get smaller as clusters are emptied.
614 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
615 &bn, &dirclust, &blsize);
618 diroffset = ddep->de_fndoffset;
619 if (dirclust != MSDOSFSROOT)
620 diroffset &= pmp->pm_crbomask;
621 if ((error = bread(pmp->pm_devvp, bn, blsize, &bp)) != 0) {
625 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
627 DE_EXTERNALIZE(ndep, dep);
630 * Now write the Win95 long name
632 if (ddep->de_fndcnt > 0) {
633 u_int8_t chksum = winChksum(ndep->deName);
634 const u_char *un = (const u_char *)cnp->cn_nameptr;
635 int unlen = cnp->cn_namelen;
638 while (--ddep->de_fndcnt >= 0) {
639 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
640 if ((error = bwrite(bp)) != 0)
643 ddep->de_fndoffset -= sizeof(struct direntry);
651 error = bread(pmp->pm_devvp, bn, blsize, &bp);
656 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
659 ddep->de_fndoffset -= sizeof(struct direntry);
661 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
663 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
669 if ((error = bwrite(bp)) != 0)
673 * If they want us to return with the denode gotten.
676 if (dep->de_Attributes & ATTR_DIRECTORY) {
677 dirclust = dep->de_StartCluster;
678 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
679 dirclust = MSDOSFSROOT;
680 if (dirclust == MSDOSFSROOT)
681 diroffset = MSDOSFSROOT_OFS;
685 return deget(pmp, dirclust, diroffset, depp);
692 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
693 * return 0 if not empty or error.
704 struct msdosfsmount *pmp = dep->de_pmp;
705 struct direntry *dentp;
708 * Since the filesize field in directory entries for a directory is
709 * zero, we just have to feel our way through the directory until
710 * we hit end of file.
712 for (cn = 0;; cn++) {
713 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
715 return (1); /* it's empty */
718 error = bread(pmp->pm_devvp, bn, blsize, &bp);
723 for (dentp = (struct direntry *)bp->b_data;
724 (char *)dentp < bp->b_data + blsize;
726 if (dentp->deName[0] != SLOT_DELETED &&
727 (dentp->deAttributes & ATTR_VOLUME) == 0) {
729 * In dos directories an entry whose name
730 * starts with SLOT_EMPTY (0) starts the
731 * beginning of the unused part of the
732 * directory, so we can just return that it
735 if (dentp->deName[0] == SLOT_EMPTY) {
740 * Any names other than "." and ".." in a
741 * directory mean it is not empty.
743 if (bcmp(dentp->deName, ". ", 11) &&
744 bcmp(dentp->deName, ".. ", 11)) {
747 printf("dosdirempty(): entry found %02x, %02x\n",
748 dentp->deName[0], dentp->deName[1]);
750 return (0); /* not empty */
760 * Check to see if the directory described by target is in some
761 * subdirectory of source. This prevents something like the following from
762 * succeeding and leaving a bunch or files and directories orphaned. mv
763 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
765 * source - the inode for /a/b/c
766 * target - the inode for /a/b/c/d/e/f
768 * Returns 0 if target is NOT a subdirectory of source.
769 * Otherwise returns a non-zero error number.
770 * The target inode is always unlocked on return.
773 doscheckpath(source, target)
774 struct denode *source;
775 struct denode *target;
778 struct msdosfsmount *pmp;
781 struct buf *bp = NULL;
785 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
786 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
790 if (dep->de_StartCluster == source->de_StartCluster) {
794 if (dep->de_StartCluster == MSDOSFSROOT)
798 if (pmp != source->de_pmp)
799 panic("doscheckpath: source and target on different filesystems");
801 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
805 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
809 scn = dep->de_StartCluster;
810 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
811 pmp->pm_bpcluster, &bp);
815 ep = (struct direntry *) bp->b_data + 1;
816 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
817 bcmp(ep->deName, ".. ", 11) != 0) {
821 scn = getushort(ep->deStartCluster);
823 scn |= getushort(ep->deHighClust) << 16;
825 if (scn == source->de_StartCluster) {
829 if (scn == MSDOSFSROOT)
831 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
833 * scn should be 0 in this case,
834 * but we silently ignore the error.
842 /* NOTE: deget() clears dep on error */
843 if ((error = deget(pmp, scn, 0, &dep)) != 0)
849 if (error == ENOTDIR)
850 printf("doscheckpath(): .. not a directory?\n");
857 * Read in the disk block containing the directory entry (dirclu, dirofs)
858 * and return the address of the buf header, and the address of the
859 * directory entry within the block.
862 readep(pmp, dirclust, diroffset, bpp, epp)
863 struct msdosfsmount *pmp;
864 u_long dirclust, diroffset;
866 struct direntry **epp;
872 blsize = pmp->pm_bpcluster;
873 if (dirclust == MSDOSFSROOT
874 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
875 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
876 bn = detobn(pmp, dirclust, diroffset);
877 if ((error = bread(pmp->pm_devvp, bn, blsize, bpp)) != 0) {
883 *epp = bptoep(pmp, *bpp, diroffset);
888 * Read in the disk block containing the directory entry dep came from and
889 * return the address of the buf header, and the address of the directory
890 * entry within the block.
893 readde(dep, bpp, epp)
896 struct direntry **epp;
899 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
904 * Remove a directory entry. At this point the file represented by the
905 * directory entry to be removed is still full length until noone has it
906 * open. When the file no longer being used msdosfs_inactive() is called
907 * and will truncate the file to 0 length. When the vnode containing the
908 * denode is needed for some other purpose by VFS it will call
909 * msdosfs_reclaim() which will remove the denode from the denode cache.
913 struct denode *pdep; /* directory where the entry is removed */
914 struct denode *dep; /* file to be removed */
921 struct msdosfsmount *pmp = pdep->de_pmp;
922 u_long offset = pdep->de_fndoffset;
925 printf("removede(): filename %s, dep %p, offset %08lx\n",
926 dep->de_Name, dep, offset);
930 offset += sizeof(struct direntry);
932 offset -= sizeof(struct direntry);
933 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
936 error = bread(pmp->pm_devvp, bn, blsize, &bp);
941 ep = bptoep(pmp, bp, offset);
943 * Check whether, if we came here the second time, i.e.
944 * when underflowing into the previous block, the last
945 * entry in this block is a longfilename entry, too.
947 if (ep->deAttributes != ATTR_WIN95
948 && offset != pdep->de_fndoffset) {
952 offset += sizeof(struct direntry);
955 * We are a bit agressive here in that we delete any Win95
956 * entries preceding this entry, not just the ones we "own".
957 * Since these presumably aren't valid anyway,
958 * there should be no harm.
960 offset -= sizeof(struct direntry);
961 ep--->deName[0] = SLOT_DELETED;
962 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
963 || !(offset & pmp->pm_crbomask)
964 || ep->deAttributes != ATTR_WIN95)
967 if ((error = bwrite(bp)) != 0)
969 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
970 && !(offset & pmp->pm_crbomask)
976 * Create a unique DOS name in dvp
979 uniqdosname(dep, cnp, cp)
981 struct componentname *cnp;
984 struct msdosfsmount *pmp = dep->de_pmp;
985 struct direntry *dentp;
993 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
994 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
996 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
997 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu) ?
1000 for (gen = 1;; gen++) {
1002 * Generate DOS name with generation number
1004 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1005 cnp->cn_namelen, gen,
1006 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
1007 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu))
1008 return gen == 1 ? EINVAL : EEXIST;
1011 * Now look for a dir entry with this exact name
1013 for (cn = error = 0; !error; cn++) {
1014 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1015 if (error == E2BIG) /* EOF reached and not found */
1019 error = bread(pmp->pm_devvp, bn, blsize, &bp);
1024 for (dentp = (struct direntry *)bp->b_data;
1025 (char *)dentp < bp->b_data + blsize;
1027 if (dentp->deName[0] == SLOT_EMPTY) {
1029 * Last used entry and not found
1035 * Ignore volume labels and Win95 entries
1037 if (dentp->deAttributes & ATTR_VOLUME)
1039 if (!bcmp(dentp->deName, cp, 11)) {
1050 * Find any Win'95 long filename entry in directory dep
1056 struct msdosfsmount *pmp = dep->de_pmp;
1057 struct direntry *dentp;
1065 * Read through the directory looking for Win'95 entries
1066 * Note: Error currently handled just as EOF XXX
1068 for (cn = 0;; cn++) {
1069 if (pcbmap(dep, cn, &bn, 0, &blsize))
1071 if (bread(pmp->pm_devvp, bn, blsize, &bp)) {
1075 for (dentp = (struct direntry *)bp->b_data;
1076 (char *)dentp < bp->b_data + blsize;
1078 if (dentp->deName[0] == SLOT_EMPTY) {
1080 * Last used entry and not found
1085 if (dentp->deName[0] == SLOT_DELETED) {
1087 * Ignore deleted files
1088 * Note: might be an indication of Win'95 anyway XXX
1092 if (dentp->deAttributes == ATTR_WIN95) {