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.21 2006/12/23 00:41:29 swildner 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.
81 * msdosfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
82 * struct componentname *a_cnp)
85 msdosfs_lookup(struct vop_old_lookup_args
*ap
)
87 struct vnode
*vdp
= ap
->a_dvp
;
88 struct vnode
**vpp
= ap
->a_vpp
;
89 struct componentname
*cnp
= ap
->a_cnp
;
101 int isadir
; /* ~0 if found direntry is a directory */
102 u_long scn
; /* starting cluster number */
106 struct msdosfsmount
*pmp
;
108 struct direntry
*dep
= NULL
;
109 u_char dosfilename
[12];
110 int flags
= cnp
->cn_flags
;
111 int nameiop
= cnp
->cn_nameiop
;
117 cnp
->cn_flags
&= ~CNP_PDIRUNLOCK
;
120 kprintf("msdosfs_lookup(): looking for %s\n", cnp
->cn_nameptr
);
125 lockparent
= flags
& CNP_LOCKPARENT
;
126 wantparent
= flags
& (CNP_LOCKPARENT
| CNP_WANTPARENT
);
128 kprintf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
129 vdp
, dp
, dp
->de_Attributes
);
133 * If they are going after the . or .. entry in the root directory,
134 * they won't find it. DOS filesystems don't have them in the root
135 * directory. So, we fake it. deget() is in on this scam too.
137 if ((vdp
->v_flag
& VROOT
) && cnp
->cn_nameptr
[0] == '.' &&
138 (cnp
->cn_namelen
== 1 ||
139 (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.'))) {
140 isadir
= ATTR_DIRECTORY
;
143 kprintf("msdosfs_lookup(): looking for . or .. in root directory\n");
145 cluster
= MSDOSFSROOT
;
146 blkoff
= MSDOSFSROOT_OFS
;
150 switch (unix2dosfn((const u_char
*)cnp
->cn_nameptr
, dosfilename
,
152 pmp
->pm_flags
& MSDOSFSMNT_U2WTABLE
, pmp
->pm_u2d
,
153 pmp
->pm_flags
& MSDOSFSMNT_ULTABLE
, pmp
->pm_lu
)) {
159 wincnt
= winSlotCnt((const u_char
*)cnp
->cn_nameptr
,
160 cnp
->cn_namelen
) + 1;
164 wincnt
= winSlotCnt((const u_char
*)cnp
->cn_nameptr
,
165 cnp
->cn_namelen
) + 1;
168 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
) {
172 unlen
= winLenFixup(cnp
->cn_nameptr
, cnp
->cn_namelen
);
175 * Suppress search for slots unless creating
176 * file and at end of pathname, in which case
177 * we watch for a place to put the new file in
178 * case it doesn't already exist.
181 if (nameiop
== NAMEI_CREATE
|| nameiop
== NAMEI_RENAME
)
185 kprintf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
186 dosfilename
, cnp
->cn_namelen
);
189 * Search the directory pointed at by vdp for the name pointed at
190 * by cnp->cn_nameptr.
194 * The outer loop ranges over the clusters that make up the
195 * directory. Note that the root directory is different from all
196 * other directories. It has a fixed number of blocks that are not
197 * part of the pool of allocatable clusters. So, we treat it a
198 * little differently. The root directory starts at "cluster" 0.
201 for (frcn
= 0;; frcn
++) {
202 error
= pcbmap(dp
, frcn
, &bn
, &cluster
, &blsize
);
208 error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
);
213 for (blkoff
= 0; blkoff
< blsize
;
214 blkoff
+= sizeof(struct direntry
),
215 diroff
+= sizeof(struct direntry
)) {
216 dep
= (struct direntry
*)(bp
->b_data
+ blkoff
);
218 * If the slot is empty and we are still looking
219 * for an empty then remember this one. If the
220 * slot is not empty then check to see if it
221 * matches what we are looking for. If the slot
222 * has never been filled with anything, then the
223 * remainder of the directory has never been used,
224 * so there is no point in searching it.
226 if (dep
->deName
[0] == SLOT_EMPTY
||
227 dep
->deName
[0] == SLOT_DELETED
) {
229 * Drop memory of previous long matches
233 if (slotcount
< wincnt
) {
237 if (dep
->deName
[0] == SLOT_EMPTY
) {
243 * If there wasn't enough space for our winentries,
244 * forget about the empty space
246 if (slotcount
< wincnt
)
250 * Check for Win95 long filename entry
252 if (dep
->deAttributes
== ATTR_WIN95
) {
253 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
)
256 chksum
= winChkName((const u_char
*)cnp
->cn_nameptr
,
258 (struct winentry
*)dep
,
260 pmp
->pm_flags
& MSDOSFSMNT_U2WTABLE
,
262 pmp
->pm_flags
& MSDOSFSMNT_ULTABLE
,
268 * Ignore volume labels (anywhere, not just
269 * the root directory).
271 if (dep
->deAttributes
& ATTR_VOLUME
) {
277 * Check for a checksum or name match
279 if (chksum
!= winChksum(dep
->deName
)
280 && (!olddos
|| bcmp(dosfilename
, dep
->deName
, 11))) {
285 kprintf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
289 * Remember where this directory
290 * entry came from for whoever did
293 dp
->de_fndoffset
= diroff
;
294 dp
->de_fndcnt
= wincnt
- 1;
298 } /* for (blkoff = 0; .... */
300 * Release the buffer holding the directory cluster just
304 } /* for (frcn = 0; ; frcn++) */
308 * We hold no disk buffers at this point.
312 * Fixup the slot description to point to the place where
313 * we might put the new DOS direntry (putting the Win95
314 * long name entries before that)
320 if (wincnt
> slotcount
)
321 slotoffset
+= sizeof(struct direntry
) * (wincnt
- slotcount
);
324 * If we get here we didn't find the entry we were looking for. But
325 * that's ok if we are creating or renaming and are at the end of
326 * the pathname and the directory hasn't been removed.
329 kprintf("msdosfs_lookup(): op %d, refcnt %ld\n",
330 nameiop
, dp
->de_refcnt
);
331 kprintf(" slotcount %d, slotoffset %d\n",
332 slotcount
, slotoffset
);
334 if ((nameiop
== NAMEI_CREATE
|| nameiop
== NAMEI_RENAME
) &&
335 dp
->de_refcnt
!= 0) {
337 * Access for write is interpreted as allowing
338 * creation of files in the directory.
340 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
344 * Return an indication of where the new directory
345 * entry should be put.
347 dp
->de_fndoffset
= slotoffset
;
348 dp
->de_fndcnt
= wincnt
- 1;
351 * We return with the directory locked, so that
352 * the parameters we set up above will still be
353 * valid if we actually decide to do a direnter().
354 * We return ni_vp == NULL to indicate that the entry
355 * does not currently exist; we leave a pointer to
356 * the (locked) directory inode in ndp->ni_dvp.
357 * The pathname buffer is saved so that the name
358 * can be obtained later.
360 * NB - if the directory is unlocked, then this
361 * information cannot be used.
365 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
367 return (EJUSTRETURN
);
373 * NOTE: We still have the buffer with matched directory entry at
376 isadir
= dep
->deAttributes
& ATTR_DIRECTORY
;
377 scn
= getushort(dep
->deStartCluster
);
379 scn
|= getushort(dep
->deHighClust
) << 16;
380 if (scn
== pmp
->pm_rootdirblk
) {
382 * There should actually be 0 here.
383 * Just ignore the error.
391 if (cluster
== MSDOSFSROOT
)
392 blkoff
= MSDOSFSROOT_OFS
;
395 } else if (cluster
== MSDOSFSROOT
)
399 * Now release buf to allow deget to read the entry again.
400 * Reserving it here and giving it to deget could result
408 * If we entered at foundroot, then we are looking for the . or ..
409 * entry of the filesystems root directory. isadir and scn were
410 * setup before jumping here. And, bp is already null.
412 if (FAT32(pmp
) && scn
== MSDOSFSROOT
)
413 scn
= pmp
->pm_rootdirblk
;
416 * If deleting, and at end of pathname, return
417 * parameters which can be used to remove file.
418 * If the wantparent flag isn't set, we return only
419 * the directory (in ndp->ni_dvp), otherwise we go
420 * on and lock the inode, being careful with ".".
422 if (nameiop
== NAMEI_DELETE
) {
424 * Don't allow deleting the root.
426 if (blkoff
== MSDOSFSROOT_OFS
)
427 return EROFS
; /* really? XXX */
430 * Write access to directory required to delete files.
432 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
437 * Return pointer to current entry in dp->i_offset.
438 * Save directory inode pointer in ndp->ni_dvp for dirremove().
440 if (dp
->de_StartCluster
== scn
&& isadir
) { /* "." */
445 error
= deget(pmp
, cluster
, blkoff
, &tdp
);
451 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
457 * If rewriting (RENAME), return the inode and the
458 * information required to rewrite the present directory
459 * Must get inode of directory entry to verify it's a
460 * regular file, or empty directory.
462 if (nameiop
== NAMEI_RENAME
&& wantparent
) {
463 if (blkoff
== MSDOSFSROOT_OFS
)
464 return EROFS
; /* really? XXX */
466 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
471 * Careful about locking second inode.
472 * This can only occur if the target is ".".
474 if (dp
->de_StartCluster
== scn
&& isadir
)
477 if ((error
= deget(pmp
, cluster
, blkoff
, &tdp
)) != 0)
482 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
488 * Step through the translation in the name. We do not `vput' the
489 * directory because we may need it again if a symbolic link
490 * is relative to the current directory. Instead we save it
491 * unlocked as "pdp". We must get the target inode before unlocking
492 * the directory to insure that the inode will not be removed
493 * before we get it. We prevent deadlock by always fetching
494 * inodes from the root, moving down the directory tree. Thus
495 * when following backward pointers ".." we must unlock the
496 * parent directory before getting the requested directory.
497 * There is a potential race condition here if both the current
498 * and parent directories are removed before the VFS_VGET for the
499 * inode associated with ".." returns. We hope that this occurs
500 * infrequently since we cannot avoid this race condition without
501 * implementing a sophisticated deadlock detection algorithm.
502 * Note also that this simple deadlock detection scheme will not
503 * work if the file system has any hard links other than ".."
504 * that point backwards in the directory structure.
507 if (flags
& CNP_ISDOTDOT
) {
509 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
510 error
= deget(pmp
, cluster
, blkoff
, &tdp
);
512 vn_lock(pdp
, LK_EXCLUSIVE
| LK_RETRY
);
513 cnp
->cn_flags
&= ~CNP_PDIRUNLOCK
;
517 error
= vn_lock(pdp
, LK_EXCLUSIVE
);
522 cnp
->cn_flags
&= ~CNP_PDIRUNLOCK
;
525 } else if (dp
->de_StartCluster
== scn
&& isadir
) {
526 vref(vdp
); /* we want ourself, ie "." */
529 if ((error
= deget(pmp
, cluster
, blkoff
, &tdp
)) != 0)
533 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
541 * dep - directory entry to copy into the directory
542 * ddep - directory to add to
543 * depp - return the address of the denode for the created directory entry
545 * cnp - componentname needed for Win95 long filenames
548 createde(struct denode
*dep
, struct denode
*ddep
, struct denode
**depp
,
549 struct componentname
*cnp
)
552 u_long dirclust
, diroffset
;
553 struct direntry
*ndep
;
554 struct msdosfsmount
*pmp
= ddep
->de_pmp
;
560 kprintf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
561 dep
, ddep
, depp
, cnp
);
565 * If no space left in the directory then allocate another cluster
566 * and chain it onto the end of the file. There is one exception
567 * to this. That is, if the root directory has no more space it
568 * can NOT be expanded. extendfile() checks for and fails attempts
569 * to extend the root directory. We just return an error in that
572 if (ddep
->de_fndoffset
>= ddep
->de_FileSize
) {
573 diroffset
= ddep
->de_fndoffset
+ sizeof(struct direntry
)
575 dirclust
= de_clcount(pmp
, diroffset
);
576 error
= extendfile(ddep
, dirclust
, 0, 0, DE_CLEAR
);
578 detrunc(ddep
, ddep
->de_FileSize
, 0);
583 * Update the size of the directory
585 ddep
->de_FileSize
+= de_cn2off(pmp
, dirclust
);
589 * We just read in the cluster with space. Copy the new directory
590 * entry in. Then write it to disk. NOTE: DOS directories
591 * do not get smaller as clusters are emptied.
593 error
= pcbmap(ddep
, de_cluster(pmp
, ddep
->de_fndoffset
),
594 &bn
, &dirclust
, &blsize
);
597 diroffset
= ddep
->de_fndoffset
;
598 if (dirclust
!= MSDOSFSROOT
)
599 diroffset
&= pmp
->pm_crbomask
;
600 if ((error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
)) != 0) {
604 ndep
= bptoep(pmp
, bp
, ddep
->de_fndoffset
);
606 DE_EXTERNALIZE(ndep
, dep
);
609 * Now write the Win95 long name
611 if (ddep
->de_fndcnt
> 0) {
612 u_int8_t chksum
= winChksum(ndep
->deName
);
613 const u_char
*un
= (const u_char
*)cnp
->cn_nameptr
;
614 int unlen
= cnp
->cn_namelen
;
617 while (--ddep
->de_fndcnt
>= 0) {
618 if (!(ddep
->de_fndoffset
& pmp
->pm_crbomask
)) {
619 if ((error
= bwrite(bp
)) != 0)
622 ddep
->de_fndoffset
-= sizeof(struct direntry
);
630 error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
);
635 ndep
= bptoep(pmp
, bp
, ddep
->de_fndoffset
);
638 ddep
->de_fndoffset
-= sizeof(struct direntry
);
640 if (!unix2winfn(un
, unlen
, (struct winentry
*)ndep
,
642 pmp
->pm_flags
& MSDOSFSMNT_U2WTABLE
,
648 if ((error
= bwrite(bp
)) != 0)
652 * If they want us to return with the denode gotten.
655 if (dep
->de_Attributes
& ATTR_DIRECTORY
) {
656 dirclust
= dep
->de_StartCluster
;
657 if (FAT32(pmp
) && dirclust
== pmp
->pm_rootdirblk
)
658 dirclust
= MSDOSFSROOT
;
659 if (dirclust
== MSDOSFSROOT
)
660 diroffset
= MSDOSFSROOT_OFS
;
664 return deget(pmp
, dirclust
, diroffset
, depp
);
671 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
672 * return 0 if not empty or error.
675 dosdirempty(struct denode
*dep
)
682 struct msdosfsmount
*pmp
= dep
->de_pmp
;
683 struct direntry
*dentp
;
686 * Since the filesize field in directory entries for a directory is
687 * zero, we just have to feel our way through the directory until
688 * we hit end of file.
690 for (cn
= 0;; cn
++) {
691 if ((error
= pcbmap(dep
, cn
, &bn
, NULL
, &blsize
)) != 0) {
693 return (1); /* it's empty */
696 error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
);
701 for (dentp
= (struct direntry
*)bp
->b_data
;
702 (char *)dentp
< bp
->b_data
+ blsize
;
704 if (dentp
->deName
[0] != SLOT_DELETED
&&
705 (dentp
->deAttributes
& ATTR_VOLUME
) == 0) {
707 * In dos directories an entry whose name
708 * starts with SLOT_EMPTY (0) starts the
709 * beginning of the unused part of the
710 * directory, so we can just return that it
713 if (dentp
->deName
[0] == SLOT_EMPTY
) {
718 * Any names other than "." and ".." in a
719 * directory mean it is not empty.
721 if (bcmp(dentp
->deName
, ". ", 11) &&
722 bcmp(dentp
->deName
, ".. ", 11)) {
725 kprintf("dosdirempty(): entry found %02x, %02x\n",
726 dentp
->deName
[0], dentp
->deName
[1]);
728 return (0); /* not empty */
738 * Check to see if the directory described by target is in some
739 * subdirectory of source. This prevents something like the following from
740 * succeeding and leaving a bunch or files and directories orphaned. mv
741 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
743 * source - the inode for /a/b/c
744 * target - the inode for /a/b/c/d/e/f
746 * Returns 0 if target is NOT a subdirectory of source.
747 * Otherwise returns a non-zero error number.
748 * The target inode is always unlocked on return.
751 doscheckpath(struct denode
*source
, struct denode
*target
)
754 struct msdosfsmount
*pmp
;
757 struct buf
*bp
= NULL
;
761 if ((target
->de_Attributes
& ATTR_DIRECTORY
) == 0 ||
762 (source
->de_Attributes
& ATTR_DIRECTORY
) == 0) {
766 if (dep
->de_StartCluster
== source
->de_StartCluster
) {
770 if (dep
->de_StartCluster
== MSDOSFSROOT
)
774 if (pmp
!= source
->de_pmp
)
775 panic("doscheckpath: source and target on different filesystems");
777 if (FAT32(pmp
) && dep
->de_StartCluster
== pmp
->pm_rootdirblk
)
781 if ((dep
->de_Attributes
& ATTR_DIRECTORY
) == 0) {
785 scn
= dep
->de_StartCluster
;
786 error
= bread(pmp
->pm_devvp
, xcntodoff(pmp
, scn
),
787 pmp
->pm_bpcluster
, &bp
);
791 ep
= (struct direntry
*) bp
->b_data
+ 1;
792 if ((ep
->deAttributes
& ATTR_DIRECTORY
) == 0 ||
793 bcmp(ep
->deName
, ".. ", 11) != 0) {
797 scn
= getushort(ep
->deStartCluster
);
799 scn
|= getushort(ep
->deHighClust
) << 16;
801 if (scn
== source
->de_StartCluster
) {
805 if (scn
== MSDOSFSROOT
)
807 if (FAT32(pmp
) && scn
== pmp
->pm_rootdirblk
) {
809 * scn should be 0 in this case,
810 * but we silently ignore the error.
818 /* NOTE: deget() clears dep on error */
819 if ((error
= deget(pmp
, scn
, 0, &dep
)) != 0)
825 if (error
== ENOTDIR
)
826 kprintf("doscheckpath(): .. not a directory?\n");
833 * Read in the disk block containing the directory entry (dirclu, dirofs)
834 * and return the address of the buf header, and the address of the
835 * directory entry within the block.
838 readep(struct msdosfsmount
*pmp
, u_long dirclust
, u_long diroffset
,
839 struct buf
**bpp
, struct direntry
**epp
)
845 blsize
= pmp
->pm_bpcluster
;
846 if (dirclust
== MSDOSFSROOT
847 && de_blk(pmp
, diroffset
+ blsize
) > pmp
->pm_rootdirsize
)
848 blsize
= de_bn2off(pmp
, pmp
->pm_rootdirsize
) & pmp
->pm_crbomask
;
849 bn
= detobn(pmp
, dirclust
, diroffset
);
850 if ((error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, bpp
)) != 0) {
856 *epp
= bptoep(pmp
, *bpp
, diroffset
);
861 * Read in the disk block containing the directory entry dep came from and
862 * return the address of the buf header, and the address of the directory
863 * entry within the block.
866 readde(struct denode
*dep
, struct buf
**bpp
, struct direntry
**epp
)
868 return (readep(dep
->de_pmp
, dep
->de_dirclust
, dep
->de_diroffset
,
873 * Remove a directory entry. At this point the file represented by the
874 * directory entry to be removed is still full length until noone has it
875 * open. When the file no longer being used msdosfs_inactive() is called
876 * and will truncate the file to 0 length. When the vnode containing the
877 * denode is needed for some other purpose by VFS it will call
878 * msdosfs_reclaim() which will remove the denode from the denode cache.
881 removede(struct denode
*pdep
, /* directory where the entry is removed */
882 struct denode
*dep
) /* file to be removed */
889 struct msdosfsmount
*pmp
= pdep
->de_pmp
;
890 u_long offset
= pdep
->de_fndoffset
;
893 kprintf("removede(): filename %s, dep %p, offset %08lx\n",
894 dep
->de_Name
, dep
, offset
);
898 offset
+= sizeof(struct direntry
);
900 offset
-= sizeof(struct direntry
);
901 error
= pcbmap(pdep
, de_cluster(pmp
, offset
),
905 error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
);
910 ep
= bptoep(pmp
, bp
, offset
);
912 * Check whether, if we came here the second time, i.e.
913 * when underflowing into the previous block, the last
914 * entry in this block is a longfilename entry, too.
916 if (ep
->deAttributes
!= ATTR_WIN95
917 && offset
!= pdep
->de_fndoffset
) {
921 offset
+= sizeof(struct direntry
);
924 * We are a bit agressive here in that we delete any Win95
925 * entries preceding this entry, not just the ones we "own".
926 * Since these presumably aren't valid anyway,
927 * there should be no harm.
929 offset
-= sizeof(struct direntry
);
930 ep
--->deName
[0] = SLOT_DELETED
;
931 if ((pmp
->pm_flags
& MSDOSFSMNT_NOWIN95
)
932 || !(offset
& pmp
->pm_crbomask
)
933 || ep
->deAttributes
!= ATTR_WIN95
)
936 if ((error
= bwrite(bp
)) != 0)
938 } while (!(pmp
->pm_flags
& MSDOSFSMNT_NOWIN95
)
939 && !(offset
& pmp
->pm_crbomask
)
945 * Create a unique DOS name in dvp
948 uniqdosname(struct denode
*dep
, struct componentname
*cnp
, u_char
*cp
)
950 struct msdosfsmount
*pmp
= dep
->de_pmp
;
951 struct direntry
*dentp
;
959 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
)
960 return (unix2dosfn((const u_char
*)cnp
->cn_nameptr
, cp
,
962 pmp
->pm_flags
& MSDOSFSMNT_U2WTABLE
, pmp
->pm_u2d
,
963 pmp
->pm_flags
& MSDOSFSMNT_ULTABLE
, pmp
->pm_lu
) ?
966 for (gen
= 1;; gen
++) {
968 * Generate DOS name with generation number
970 if (!unix2dosfn((const u_char
*)cnp
->cn_nameptr
, cp
,
971 cnp
->cn_namelen
, gen
,
972 pmp
->pm_flags
& MSDOSFSMNT_U2WTABLE
, pmp
->pm_u2d
,
973 pmp
->pm_flags
& MSDOSFSMNT_ULTABLE
, pmp
->pm_lu
))
974 return gen
== 1 ? EINVAL
: EEXIST
;
977 * Now look for a dir entry with this exact name
979 for (cn
= error
= 0; !error
; cn
++) {
980 if ((error
= pcbmap(dep
, cn
, &bn
, 0, &blsize
)) != 0) {
981 if (error
== E2BIG
) /* EOF reached and not found */
985 error
= bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
);
990 for (dentp
= (struct direntry
*)bp
->b_data
;
991 (char *)dentp
< bp
->b_data
+ blsize
;
993 if (dentp
->deName
[0] == SLOT_EMPTY
) {
995 * Last used entry and not found
1001 * Ignore volume labels and Win95 entries
1003 if (dentp
->deAttributes
& ATTR_VOLUME
)
1005 if (!bcmp(dentp
->deName
, cp
, 11)) {
1016 * Find any Win'95 long filename entry in directory dep
1019 findwin95(struct denode
*dep
)
1021 struct msdosfsmount
*pmp
= dep
->de_pmp
;
1022 struct direntry
*dentp
;
1030 * Read through the directory looking for Win'95 entries
1031 * Note: Error currently handled just as EOF XXX
1033 for (cn
= 0;; cn
++) {
1034 if (pcbmap(dep
, cn
, &bn
, 0, &blsize
))
1036 if (bread(pmp
->pm_devvp
, de_bntodoff(pmp
, bn
), blsize
, &bp
)) {
1040 for (dentp
= (struct direntry
*)bp
->b_data
;
1041 (char *)dentp
< bp
->b_data
+ blsize
;
1043 if (dentp
->deName
[0] == SLOT_EMPTY
) {
1045 * Last used entry and not found
1050 if (dentp
->deName
[0] == SLOT_DELETED
) {
1052 * Ignore deleted files
1053 * Note: might be an indication of Win'95 anyway XXX
1057 if (dentp
->deAttributes
== ATTR_WIN95
) {