1 /* $NetBSD: msdosfs_lookup.c,v 1.34 2015/03/28 19:24:05 maxv Exp $ */
4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 * You can do anything you want with this software, just don't say you wrote
38 * it, and don't remove this notice.
40 * This software is provided "as is".
42 * The author supplies this software to be publicly redistributed on the
43 * understanding that the author is not responsible for the correct
44 * functioning of this software in any circumstances and is not liable for
45 * any damages caused by this software.
50 #if HAVE_NBTOOL_CONFIG_H
51 #include "nbtool_config.h"
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: msdosfs_lookup.c,v 1.34 2015/03/28 19:24:05 maxv Exp $");
57 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/mount.h>
62 #include <sys/kauth.h>
63 #include <sys/namei.h>
64 #include <sys/dirent.h>
66 #include <sys/vnode.h>
67 #include <sys/atomic.h>
72 #include <fs/msdosfs/bpb.h>
73 #include <fs/msdosfs/direntry.h>
74 #include <fs/msdosfs/denode.h>
75 #include <fs/msdosfs/msdosfsmount.h>
76 #include <fs/msdosfs/fat.h>
81 * When we search a directory the blocks containing directory entries are
82 * read and examined. The directory entries contain information that would
83 * normally be in the inode of a unix filesystem. This means that some of
84 * a directory's contents may also be in memory resident denodes (sort of
85 * an inode). This can cause problems if we are searching while some other
86 * process is modifying a directory. To prevent one process from accessing
87 * incompletely modified directory information we depend upon being the
88 * sole owner of a directory block. bread/brelse provide this service.
89 * This being the case, when a process modifies a directory it must first
90 * acquire the disk block that contains the directory entry to be modified.
91 * Then update the disk block and the denode, and then write the disk block
92 * out to disk. This way disk blocks containing directory entries and in
93 * memory denode's will be in synch.
96 msdosfs_lookup(void *v
)
98 struct vop_lookup_v2_args
/* {
100 struct vnode **a_vpp;
101 struct componentname *a_cnp;
103 struct vnode
*vdp
= ap
->a_dvp
;
104 struct vnode
**vpp
= ap
->a_vpp
;
105 struct componentname
*cnp
= ap
->a_cnp
;
115 int isadir
; /* ~0 if found direntry is a directory */
116 u_long scn
; /* starting cluster number */
118 struct msdosfsmount
*pmp
;
120 struct direntry
*dep
;
121 u_char dosfilename
[12];
123 int nameiop
= cnp
->cn_nameiop
;
125 int chksum
= -1, chksum_ok
;
128 flags
= cnp
->cn_flags
;
131 printf("msdosfs_lookup(): looking for %.*s\n",
132 (int)cnp
->cn_namelen
, cnp
->cn_nameptr
);
138 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
139 vdp
, dp
, dp
->de_Attributes
);
143 * Check accessiblity of directory.
145 if ((error
= VOP_ACCESS(vdp
, VEXEC
, cnp
->cn_cred
)) != 0)
148 if ((flags
& ISLASTCN
) && (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
149 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
153 * We now have a segment name to search for, and a directory to search.
155 * Before tediously performing a linear scan of the directory,
156 * check the name cache to see if the directory/name pair
157 * we are looking for is known already.
159 if (cache_lookup(vdp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
160 cnp
->cn_nameiop
, cnp
->cn_flags
, NULL
, vpp
)) {
161 return *vpp
== NULLVP
? ENOENT
: 0;
165 * If they are going after the . or .. entry in the root directory,
166 * they won't find it. DOS filesystems don't have them in the root
167 * directory. So, we fake it. deget() is in on this scam too.
169 if ((vdp
->v_vflag
& VV_ROOT
) && cnp
->cn_nameptr
[0] == '.' &&
170 (cnp
->cn_namelen
== 1 ||
171 (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.'))) {
172 isadir
= ATTR_DIRECTORY
;
175 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
177 cluster
= MSDOSFSROOT
;
178 blkoff
= MSDOSFSROOT_OFS
;
182 switch (unix2dosfn((const u_char
*)cnp
->cn_nameptr
, dosfilename
,
183 cnp
->cn_namelen
, 0)) {
189 wincnt
= winSlotCnt((const u_char
*)cnp
->cn_nameptr
,
190 cnp
->cn_namelen
) + 1;
194 wincnt
= winSlotCnt((const u_char
*)cnp
->cn_nameptr
,
195 cnp
->cn_namelen
) + 1;
198 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
)
202 * Suppress search for slots unless creating
203 * file and at end of pathname, in which case
204 * we watch for a place to put the new file in
205 * case it doesn't already exist.
208 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
213 printf("msdosfs_lookup(): dos filename: %s\n", dosfilename
);
216 * Search the directory pointed at by vdp for the name pointed at
217 * 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; diroff
< dp
->de_FileSize
; frcn
++) {
229 if ((error
= pcbmap(dp
, frcn
, &bn
, &cluster
, &blsize
)) != 0) {
234 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
239 for (blkoff
= 0; blkoff
< blsize
;
240 blkoff
+= sizeof(struct direntry
),
241 diroff
+= sizeof(struct direntry
)) {
242 dep
= (struct direntry
*)((char *)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
259 if (slotcount
< wincnt
) {
263 if (dep
->deName
[0] == SLOT_EMPTY
) {
269 * If there wasn't enough space for our
270 * winentries, forget about the empty space
272 if (slotcount
< wincnt
)
276 * Check for Win95 long filename entry
278 if (dep
->deAttributes
== ATTR_WIN95
) {
279 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
)
282 chksum
= winChkName((const u_char
*)cnp
->cn_nameptr
,
284 (struct winentry
*)dep
,
290 * Ignore volume labels (anywhere, not just
291 * the root directory).
293 if (dep
->deAttributes
& ATTR_VOLUME
) {
299 * Check for a checksum or name match
301 chksum_ok
= (chksum
== winChksum(dep
->deName
));
304 memcmp(&dosfilename
[0],dep
->deName
,8) ||
305 memcmp(&dosfilename
[8],dep
->deExtension
,3))) {
310 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
314 * Remember where this directory
315 * entry came from for whoever did
318 dp
->de_fndoffset
= diroff
;
319 if (chksum_ok
&& nameiop
== RENAME
) {
321 * Target had correct long name
322 * directory entries, reuse them
325 dp
->de_fndcnt
= wincnt
- 1;
328 * Long name directory entries
329 * not present or corrupt, can only
330 * reuse dos directory entry.
337 } /* for (blkoff = 0; .... */
339 * Release the buffer holding the directory cluster just
343 } /* for (frcn = 0; ; frcn++) */
347 * We hold no disk buffers at this point.
351 * If we get here we didn't find the entry we were looking for. But
352 * that's ok if we are creating or renaming and are at the end of
353 * the pathname and the directory hasn't been removed.
356 printf("msdosfs_lookup(): op %d, refcnt %ld, slotcount %d, slotoffset %d\n",
357 nameiop
, dp
->de_refcnt
, slotcount
, slotoffset
);
359 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
360 (flags
& ISLASTCN
) && dp
->de_refcnt
!= 0) {
362 * Access for write is interpreted as allowing
363 * creation of files in the directory.
365 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
370 * Fixup the slot description to point to the place where
371 * we might put the new DOS direntry (putting the Win95
372 * long name entries before that)
378 if (wincnt
> slotcount
) {
380 sizeof(struct direntry
) * (wincnt
- slotcount
);
384 * Return an indication of where the new directory
385 * entry should be put.
387 dp
->de_fndoffset
= slotoffset
;
388 dp
->de_fndcnt
= wincnt
- 1;
391 * We return with the directory locked, so that
392 * the parameters we set up above will still be
393 * valid if we actually decide to do a direnter().
394 * We return ni_vp == NULL to indicate that the entry
395 * does not currently exist; we leave a pointer to
396 * the (locked) directory inode in ndp->ni_dvp.
398 * NB - if the directory is unlocked, then this
399 * information cannot be used.
401 return (EJUSTRETURN
);
406 * Insert name into cache (as non-existent) if appropriate.
408 * XXX Negative caching is broken for msdosfs because the name
409 * cache doesn't understand peculiarities such as case insensitivity
410 * and 8.3 filenames. Hence, it may not invalidate all negative
411 * entries if a file with this name is later created.
412 * e.g. creating a file 'foo' won't invalidate a negative entry
415 if (nameiop
!= CREATE
)
416 cache_enter(vdp
, *vpp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
424 * NOTE: We still have the buffer with matched directory entry at
427 isadir
= dep
->deAttributes
& ATTR_DIRECTORY
;
428 scn
= getushort(dep
->deStartCluster
);
430 scn
|= getushort(dep
->deHighClust
) << 16;
431 if (scn
== pmp
->pm_rootdirblk
) {
433 * There should actually be 0 here.
434 * Just ignore the error.
442 if (cluster
== MSDOSFSROOT
)
443 blkoff
= MSDOSFSROOT_OFS
;
446 } else if (cluster
== MSDOSFSROOT
)
450 * Now release buf to allow deget to read the entry again.
451 * Reserving it here and giving it to deget could result
458 * If we entered at foundroot, then we are looking for the . or ..
459 * entry of the filesystems root directory. isadir and scn were
460 * setup before jumping here. And, bp is already null.
462 if (FAT32(pmp
) && scn
== MSDOSFSROOT
)
463 scn
= pmp
->pm_rootdirblk
;
466 * If deleting, and at end of pathname, return
467 * parameters which can be used to remove file.
468 * Lock the inode, being careful with ".".
470 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
472 * Don't allow deleting the root.
474 if (blkoff
== MSDOSFSROOT_OFS
)
478 * Write access to directory required to delete files.
480 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
485 * Return pointer to current entry in dp->i_offset.
486 * Save directory inode pointer in ndp->ni_dvp for dirremove().
488 if (dp
->de_StartCluster
== scn
&& isadir
) { /* "." */
493 error
= deget(pmp
, cluster
, blkoff
, vpp
);
498 * If rewriting (RENAME), return the inode and the
499 * information required to rewrite the present directory
500 * Must get inode of directory entry to verify it's a
501 * regular file, or empty directory.
503 if (nameiop
== RENAME
&& (flags
& ISLASTCN
)) {
505 if (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
)
508 if (blkoff
== MSDOSFSROOT_OFS
)
511 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
516 * Careful about locking second inode.
517 * This can only occur if the target is ".".
519 if (dp
->de_StartCluster
== scn
&& isadir
)
522 error
= deget(pmp
, cluster
, blkoff
, vpp
);
526 if (dp
->de_StartCluster
== scn
&& isadir
) {
527 vref(vdp
); /* we want ourself, ie "." */
529 } else if ((error
= deget(pmp
, cluster
, blkoff
, vpp
)) != 0) {
534 * Insert name into cache if appropriate.
536 cache_enter(vdp
, *vpp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_flags
);
543 * dep - directory entry to copy into the directory
544 * ddep - directory to add to
545 * depp - return the address of the denode for the created directory entry
547 * cnp - componentname needed for Win95 long filenames
550 createde(struct denode
*dep
, struct denode
*ddep
, struct denode
**depp
, struct componentname
*cnp
)
553 u_long dirclust
, clusoffset
;
554 u_long fndoffset
, havecnt
= 0, wcnt
= 1, i
;
555 struct direntry
*ndep
;
556 struct msdosfsmount
*pmp
= ddep
->de_pmp
;
561 int async
= ddep
->de_pmp
->pm_mountp
->mnt_flag
& MNT_ASYNC
;
567 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
568 dep
, ddep
, depp
, cnp
);
572 * If no space left in the directory then allocate another cluster
573 * and chain it onto the end of the file. There is one exception
574 * to this. That is, if the root directory has no more space it
575 * can NOT be expanded. extendfile() checks for and fails attempts
576 * to extend the root directory. We just return an error in that
579 if (ddep
->de_fndoffset
>= ddep
->de_FileSize
) {
580 u_long needlen
= ddep
->de_fndoffset
+ sizeof(struct direntry
)
582 dirclust
= de_clcount(pmp
, needlen
);
583 if ((error
= extendfile(ddep
, dirclust
, 0, 0, DE_CLEAR
)) != 0) {
584 (void)detrunc(ddep
, ddep
->de_FileSize
, 0, NOCRED
);
589 * Update the size of the directory
591 ddep
->de_FileSize
+= de_cn2off(pmp
, dirclust
);
595 * We just read in the cluster with space. Copy the new directory
596 * entry in. Then write it to disk. NOTE: DOS directories
597 * do not get smaller as clusters are emptied.
599 error
= pcbmap(ddep
, de_cluster(pmp
, ddep
->de_fndoffset
),
600 &bn
, &dirclust
, &blsize
);
603 clusoffset
= ddep
->de_fndoffset
;
604 if (dirclust
!= MSDOSFSROOT
)
605 clusoffset
&= pmp
->pm_crbomask
;
606 if ((error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
607 B_MODIFY
, &bp
)) != 0) {
610 ndep
= bptoep(pmp
, bp
, clusoffset
);
612 DE_EXTERNALIZE(ndep
, dep
);
615 * Now write the Win95 long name
617 if (ddep
->de_fndcnt
> 0) {
618 u_int8_t chksum
= winChksum(ndep
->deName
);
619 const u_char
*un
= (const u_char
*)cnp
->cn_nameptr
;
620 int unlen
= cnp
->cn_namelen
;
623 fndoffset
= ddep
->de_fndoffset
;
624 xhavecnt
= ddep
->de_fndcnt
+ 1;
626 for(; wcnt
< xhavecnt
; wcnt
++) {
627 if ((fndoffset
& pmp
->pm_crbomask
) == 0) {
628 /* we should never get here if ddep is root
633 else if ((error
= bwrite(bp
)) != 0)
636 fndoffset
-= sizeof(struct direntry
);
638 de_cluster(pmp
, fndoffset
),
643 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
),
644 blsize
, B_MODIFY
, &bp
);
648 ndep
= bptoep(pmp
, bp
,
649 fndoffset
& pmp
->pm_crbomask
);
652 fndoffset
-= sizeof(struct direntry
);
654 if (!unix2winfn(un
, unlen
, (struct winentry
*)ndep
,
662 else if ((error
= bwrite(bp
)) != 0)
666 * If they want us to return with the denode gotten.
669 u_long diroffset
= clusoffset
;
671 if (dep
->de_Attributes
& ATTR_DIRECTORY
) {
672 dirclust
= dep
->de_StartCluster
;
673 if (FAT32(pmp
) && dirclust
== pmp
->pm_rootdirblk
)
674 dirclust
= MSDOSFSROOT
;
675 if (dirclust
== MSDOSFSROOT
)
676 diroffset
= MSDOSFSROOT_OFS
;
681 error
= deget(pmp
, dirclust
, diroffset
, depp
);
685 error
= deget(pmp
, dirclust
, diroffset
, &vp
);
698 * Mark all slots modified so far as deleted. Note that we
699 * can't just call removede(), since directory is not in
702 fndoffset
= ddep
->de_fndoffset
;
703 rberror
= pcbmap(ddep
, de_cluster(pmp
, fndoffset
),
707 if ((rberror
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
708 B_MODIFY
, &bp
)) != 0) {
711 ndep
= bptoep(pmp
, bp
, clusoffset
);
713 havecnt
= ddep
->de_fndcnt
+ 1;
714 for(i
= wcnt
; i
<= havecnt
; i
++) {
715 /* mark entry as deleted */
716 ndep
->deName
[0] = SLOT_DELETED
;
718 if ((fndoffset
& pmp
->pm_crbomask
) == 0) {
719 /* we should never get here if ddep is root
724 else if ((rberror
= bwrite(bp
)) != 0)
727 fndoffset
-= sizeof(struct direntry
);
728 rberror
= pcbmap(ddep
,
729 de_cluster(pmp
, fndoffset
),
734 rberror
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
),
735 blsize
, B_MODIFY
, &bp
);
739 ndep
= bptoep(pmp
, bp
, fndoffset
);
742 fndoffset
-= sizeof(struct direntry
);
746 /* ignore any further error */
757 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
758 * return 0 if not empty or error.
761 dosdirempty(struct denode
*dep
)
768 struct msdosfsmount
*pmp
= dep
->de_pmp
;
769 struct direntry
*dentp
;
772 * Since the filesize field in directory entries for a directory is
773 * zero, we just have to feel our way through the directory until
774 * we hit end of file.
776 for (cn
= 0;; cn
++) {
777 if ((error
= pcbmap(dep
, cn
, &bn
, 0, &blsize
)) != 0) {
779 return (1); /* it's empty */
782 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
787 for (dentp
= (struct direntry
*)bp
->b_data
;
788 (char *)dentp
< (char *)bp
->b_data
+ blsize
;
790 if (dentp
->deName
[0] != SLOT_DELETED
&&
791 (dentp
->deAttributes
& ATTR_VOLUME
) == 0) {
793 * In dos directories an entry whose name
794 * starts with SLOT_EMPTY (0) starts the
795 * beginning of the unused part of the
796 * directory, so we can just return that it
799 if (dentp
->deName
[0] == SLOT_EMPTY
) {
804 * Any names other than "." and ".." in a
805 * directory mean it is not empty.
807 if (memcmp(dentp
->deName
, ". ", 11) &&
808 memcmp(dentp
->deName
, ".. ", 11)) {
811 printf("dosdirempty(): found %.11s, %d, %d\n",
812 dentp
->deName
, dentp
->deName
[0],
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.
835 * The target inode is always unlocked on return.
838 doscheckpath(struct denode
*source
, struct denode
*target
)
841 struct msdosfsmount
*pmp
;
844 struct buf
*bp
= NULL
;
848 if ((target
->de_Attributes
& ATTR_DIRECTORY
) == 0 ||
849 (source
->de_Attributes
& ATTR_DIRECTORY
) == 0) {
853 if (dep
->de_StartCluster
== source
->de_StartCluster
) {
857 if (dep
->de_StartCluster
== MSDOSFSROOT
)
861 if (pmp
!= source
->de_pmp
)
862 panic("doscheckpath: source and target on different filesystems");
864 if (FAT32(pmp
) && dep
->de_StartCluster
== pmp
->pm_rootdirblk
)
868 if ((dep
->de_Attributes
& ATTR_DIRECTORY
) == 0) {
872 scn
= dep
->de_StartCluster
;
873 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, cntobn(pmp
, scn
)),
874 pmp
->pm_bpcluster
, 0, &bp
);
878 ep
= (struct direntry
*) bp
->b_data
+ 1;
879 if ((ep
->deAttributes
& ATTR_DIRECTORY
) == 0 ||
880 memcmp(ep
->deName
, ".. ", 11) != 0) {
884 scn
= getushort(ep
->deStartCluster
);
886 scn
|= getushort(ep
->deHighClust
) << 16;
888 if (scn
== source
->de_StartCluster
) {
892 if (scn
== MSDOSFSROOT
)
894 if (FAT32(pmp
) && scn
== pmp
->pm_rootdirblk
) {
896 * scn should be 0 in this case,
897 * but we silently ignore the error.
906 /* NOTE: deget() clears dep on error */
907 if ((error
= deget(pmp
, scn
, 0, &dep
)) != 0)
913 error
= deget(pmp
, scn
, 0, &vp
);
916 error
= vn_lock(vp
, LK_EXCLUSIVE
);
927 if (error
== ENOTDIR
)
928 printf("doscheckpath(): .. not a directory?\n");
935 * Read in the disk block containing the directory entry (dirclu, dirofs)
936 * and return the address of the buf header, and the address of the
937 * directory entry within the block.
940 readep(struct msdosfsmount
*pmp
, u_long dirclust
, u_long diroffset
, struct buf
**bpp
, struct direntry
**epp
)
946 blsize
= pmp
->pm_bpcluster
;
947 if (dirclust
== MSDOSFSROOT
948 && de_blk(pmp
, diroffset
+ blsize
) > pmp
->pm_rootdirsize
)
949 blsize
= de_bn2off(pmp
, pmp
->pm_rootdirsize
) & pmp
->pm_crbomask
;
950 bn
= detobn(pmp
, dirclust
, diroffset
);
951 if ((error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
957 *epp
= bptoep(pmp
, *bpp
, diroffset
);
962 * Read in the disk block containing the directory entry dep came from and
963 * return the address of the buf header, and the address of the directory
964 * entry within the block.
967 readde(struct denode
*dep
, struct buf
**bpp
, struct direntry
**epp
)
969 return (readep(dep
->de_pmp
, dep
->de_dirclust
, dep
->de_diroffset
,
974 * Remove a directory entry. At this point the file represented by the
975 * directory entry to be removed is still full length until noone has it
976 * open. When the file no longer being used msdosfs_inactive() is called
977 * and will truncate the file to 0 length. When the vnode containing the
978 * denode is needed for some other purpose by VFS it will call
979 * msdosfs_reclaim() which will remove the denode from the denode cache.
982 removede(struct denode
*pdep
, struct denode
*dep
)
983 /* pdep: directory where the entry is removed */
984 /* dep: file to be removed */
991 struct msdosfsmount
*pmp
= pdep
->de_pmp
;
992 u_long offset
= pdep
->de_fndoffset
;
994 int async
= pdep
->de_pmp
->pm_mountp
->mnt_flag
& MNT_ASYNC
;
1000 printf("removede(): filename %s, dep %p, offset %08lx\n",
1001 dep
->de_Name
, dep
, offset
);
1004 if (--dep
->de_refcnt
== 0) {
1006 struct denode_key old_key
= dep
->de_key
;
1007 struct denode_key new_key
= dep
->de_key
;
1009 KASSERT(new_key
.dk_dirgen
== NULL
);
1010 new_key
.dk_dirgen
= dep
;
1011 vcache_rekey_enter(pmp
->pm_mountp
, DETOV(dep
), &old_key
,
1012 sizeof(old_key
), &new_key
, sizeof(new_key
));
1013 dep
->de_key
= new_key
;
1014 vcache_rekey_exit(pmp
->pm_mountp
, DETOV(dep
), &old_key
,
1015 sizeof(old_key
), &dep
->de_key
, sizeof(dep
->de_key
));
1018 offset
+= sizeof(struct direntry
);
1020 offset
-= sizeof(struct direntry
);
1021 error
= pcbmap(pdep
, de_cluster(pmp
, offset
), &bn
, 0, &blsize
);
1024 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
1029 ep
= bptoep(pmp
, bp
, offset
);
1031 * Check whether, if we came here the second time, i.e.
1032 * when underflowing into the previous block, the last
1033 * entry in this block is a longfilename entry, too.
1035 if (ep
->deAttributes
!= ATTR_WIN95
1036 && offset
!= pdep
->de_fndoffset
) {
1040 offset
+= sizeof(struct direntry
);
1043 * We are a bit agressive here in that we delete any Win95
1044 * entries preceding this entry, not just the ones we "own".
1045 * Since these presumably aren't valid anyway,
1046 * there should be no harm.
1048 offset
-= sizeof(struct direntry
);
1049 ep
--->deName
[0] = SLOT_DELETED
;
1050 if ((pmp
->pm_flags
& MSDOSFSMNT_NOWIN95
)
1051 || !(offset
& pmp
->pm_crbomask
)
1052 || ep
->deAttributes
!= ATTR_WIN95
)
1057 else if ((error
= bwrite(bp
)) != 0)
1059 } while (!(pmp
->pm_flags
& MSDOSFSMNT_NOWIN95
)
1060 && !(offset
& pmp
->pm_crbomask
)
1066 * Create a unique DOS name in dvp
1069 uniqdosname(struct denode
*dep
, struct componentname
*cnp
, u_char
*cp
)
1071 struct msdosfsmount
*pmp
= dep
->de_pmp
;
1072 struct direntry
*dentp
;
1080 for (gen
= 1;; gen
++) {
1082 * Generate DOS name with generation number
1084 if (!unix2dosfn((const u_char
*)cnp
->cn_nameptr
, cp
,
1085 cnp
->cn_namelen
, gen
))
1086 return gen
== 1 ? EINVAL
: EEXIST
;
1089 * Now look for a dir entry with this exact name
1091 for (cn
= error
= 0; !error
; cn
++) {
1092 if ((error
= pcbmap(dep
, cn
, &bn
, 0, &blsize
)) != 0) {
1093 if (error
== E2BIG
) /* EOF reached and not found */
1097 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
1102 for (dentp
= (struct direntry
*)bp
->b_data
;
1103 (char *)dentp
< (char *)bp
->b_data
+ blsize
;
1105 if (dentp
->deName
[0] == SLOT_EMPTY
) {
1107 * Last used entry and not found
1113 * Ignore volume labels and Win95 entries
1115 if (dentp
->deAttributes
& ATTR_VOLUME
)
1117 if (!memcmp(dentp
->deName
, cp
, 11)) {
1128 * Find any Win'95 long filename entry in directory dep
1131 findwin95(struct denode
*dep
)
1133 struct msdosfsmount
*pmp
= dep
->de_pmp
;
1134 struct direntry
*dentp
;
1142 * Read through the directory looking for Win'95 entries
1143 * XXX Note: Error currently handled just as EOF
1145 for (cn
= 0;; cn
++) {
1146 if (pcbmap(dep
, cn
, &bn
, 0, &blsize
))
1148 if (bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
1152 for (dentp
= (struct direntry
*)bp
->b_data
;
1153 (char *)dentp
< (char *)bp
->b_data
+ blsize
;
1155 if (dentp
->deName
[0] == SLOT_EMPTY
) {
1157 * Last used entry and not found
1162 if (dentp
->deName
[0] == SLOT_DELETED
) {
1164 * Ignore deleted files
1165 * Note: might be an indication of Win'95
1170 if (dentp
->deAttributes
== ATTR_WIN95
) {