1 /* $NetBSD: msdosfs_lookup.c,v 1.19 2009/03/14 21:04:23 dsl 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 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_lookup.c,v 1.19 2009/03/14 21:04:23 dsl Exp $");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/dirent.h>
60 #include <sys/kauth.h>
62 #include <fs/msdosfs/bpb.h>
63 #include <fs/msdosfs/direntry.h>
64 #include <fs/msdosfs/denode.h>
65 #include <fs/msdosfs/msdosfsmount.h>
66 #include <fs/msdosfs/fat.h>
69 * When we search a directory the blocks containing directory entries are
70 * read and examined. The directory entries contain information that would
71 * normally be in the inode of a unix filesystem. This means that some of
72 * a directory's contents may also be in memory resident denodes (sort of
73 * an inode). This can cause problems if we are searching while some other
74 * process is modifying a directory. To prevent one process from accessing
75 * incompletely modified directory information we depend upon being the
76 * sole owner of a directory block. bread/brelse provide this service.
77 * This being the case, when a process modifies a directory it must first
78 * acquire the disk block that contains the directory entry to be modified.
79 * Then update the disk block and the denode, and then write the disk block
80 * out to disk. This way disk blocks containing directory entries and in
81 * memory denode's will be in synch.
84 msdosfs_lookup(void *v
)
86 struct vop_lookup_args
/* {
89 struct componentname *a_cnp;
91 struct vnode
*vdp
= ap
->a_dvp
;
92 struct vnode
**vpp
= ap
->a_vpp
;
93 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
;
111 u_char dosfilename
[12];
113 int nameiop
= cnp
->cn_nameiop
;
115 int chksum
= -1, chksum_ok
;
118 flags
= cnp
->cn_flags
;
121 printf("msdosfs_lookup(): looking for %.*s\n",
122 (int)cnp
->cn_namelen
, cnp
->cn_nameptr
);
128 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
129 vdp
, dp
, dp
->de_Attributes
);
133 * Check accessiblity of directory.
135 if ((error
= VOP_ACCESS(vdp
, VEXEC
, cnp
->cn_cred
)) != 0)
138 if ((flags
& ISLASTCN
) && (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
139 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
143 * We now have a segment name to search for, and a directory to search.
145 * Before tediously performing a linear scan of the directory,
146 * check the name cache to see if the directory/name pair
147 * we are looking for is known already.
149 if ((error
= cache_lookup(vdp
, vpp
, cnp
)) >= 0)
153 * If they are going after the . or .. entry in the root directory,
154 * they won't find it. DOS filesystems don't have them in the root
155 * directory. So, we fake it. deget() is in on this scam too.
157 if ((vdp
->v_vflag
& VV_ROOT
) && cnp
->cn_nameptr
[0] == '.' &&
158 (cnp
->cn_namelen
== 1 ||
159 (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.'))) {
160 isadir
= ATTR_DIRECTORY
;
163 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
165 cluster
= MSDOSFSROOT
;
166 blkoff
= MSDOSFSROOT_OFS
;
170 switch (unix2dosfn((const u_char
*)cnp
->cn_nameptr
, dosfilename
,
171 cnp
->cn_namelen
, 0)) {
177 wincnt
= winSlotCnt((const u_char
*)cnp
->cn_nameptr
,
178 cnp
->cn_namelen
) + 1;
182 wincnt
= winSlotCnt((const u_char
*)cnp
->cn_nameptr
,
183 cnp
->cn_namelen
) + 1;
186 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
)
190 * Suppress search for slots unless creating
191 * file and at end of pathname, in which case
192 * we watch for a place to put the new file in
193 * case it doesn't already exist.
196 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
201 printf("msdosfs_lookup(): dos filename: %s\n", dosfilename
);
204 * Search the directory pointed at by vdp for the name pointed at
205 * by cnp->cn_nameptr.
209 * The outer loop ranges over the clusters that make up the
210 * directory. Note that the root directory is different from all
211 * other directories. It has a fixed number of blocks that are not
212 * part of the pool of allocatable clusters. So, we treat it a
213 * little differently. The root directory starts at "cluster" 0.
216 for (frcn
= 0; diroff
< dp
->de_FileSize
; frcn
++) {
217 if ((error
= pcbmap(dp
, frcn
, &bn
, &cluster
, &blsize
)) != 0) {
222 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
228 for (blkoff
= 0; blkoff
< blsize
;
229 blkoff
+= sizeof(struct direntry
),
230 diroff
+= sizeof(struct direntry
)) {
231 dep
= (struct direntry
*)((char *)bp
->b_data
+ blkoff
);
233 * If the slot is empty and we are still looking
234 * for an empty then remember this one. If the
235 * slot is not empty then check to see if it
236 * matches what we are looking for. If the slot
237 * has never been filled with anything, then the
238 * remainder of the directory has never been used,
239 * so there is no point in searching it.
241 if (dep
->deName
[0] == SLOT_EMPTY
||
242 dep
->deName
[0] == SLOT_DELETED
) {
244 * Drop memory of previous long matches
248 if (slotcount
< wincnt
) {
252 if (dep
->deName
[0] == SLOT_EMPTY
) {
258 * If there wasn't enough space for our
259 * winentries, forget about the empty space
261 if (slotcount
< wincnt
)
265 * Check for Win95 long filename entry
267 if (dep
->deAttributes
== ATTR_WIN95
) {
268 if (pmp
->pm_flags
& MSDOSFSMNT_SHORTNAME
)
271 chksum
= winChkName((const u_char
*)cnp
->cn_nameptr
,
273 (struct winentry
*)dep
,
279 * Ignore volume labels (anywhere, not just
280 * the root directory).
282 if (dep
->deAttributes
& ATTR_VOLUME
) {
288 * Check for a checksum or name match
290 chksum_ok
= (chksum
== winChksum(dep
->deName
));
292 && (!olddos
|| memcmp(dosfilename
, dep
->deName
, 11))) {
297 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
301 * Remember where this directory
302 * entry came from for whoever did
305 dp
->de_fndoffset
= diroff
;
306 if (chksum_ok
&& nameiop
== RENAME
) {
308 * Target had correct long name
309 * directory entries, reuse them
312 dp
->de_fndcnt
= wincnt
- 1;
315 * Long name directory entries
316 * not present or corrupt, can only
317 * reuse dos directory entry.
324 } /* for (blkoff = 0; .... */
326 * Release the buffer holding the directory cluster just
330 } /* for (frcn = 0; ; frcn++) */
334 * We hold no disk buffers at this point.
338 * If we get here we didn't find the entry we were looking for. But
339 * that's ok if we are creating or renaming and are at the end of
340 * the pathname and the directory hasn't been removed.
343 printf("msdosfs_lookup(): op %d, refcnt %ld, slotcount %d, slotoffset %d\n",
344 nameiop
, dp
->de_refcnt
, slotcount
, slotoffset
);
346 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
347 (flags
& ISLASTCN
) && dp
->de_refcnt
!= 0) {
349 * Access for write is interpreted as allowing
350 * creation of files in the directory.
352 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
357 * Fixup the slot description to point to the place where
358 * we might put the new DOS direntry (putting the Win95
359 * long name entries before that)
365 if (wincnt
> slotcount
) {
367 sizeof(struct direntry
) * (wincnt
- slotcount
);
371 * Return an indication of where the new directory
372 * entry should be put.
374 dp
->de_fndoffset
= slotoffset
;
375 dp
->de_fndcnt
= wincnt
- 1;
378 * We return with the directory locked, so that
379 * the parameters we set up above will still be
380 * valid if we actually decide to do a direnter().
381 * We return ni_vp == NULL to indicate that the entry
382 * does not currently exist; we leave a pointer to
383 * the (locked) directory inode in ndp->ni_dvp.
384 * The pathname buffer is saved so that the name
385 * can be obtained later.
387 * NB - if the directory is unlocked, then this
388 * information cannot be used.
390 cnp
->cn_flags
|= SAVENAME
;
391 return (EJUSTRETURN
);
396 * Insert name into cache (as non-existent) if appropriate.
398 * XXX Negative caching is broken for msdosfs because the name
399 * cache doesn't understand peculiarities such as case insensitivity
400 * and 8.3 filenames. Hence, it may not invalidate all negative
401 * entries if a file with this name is later created.
402 * e.g. creating a file 'foo' won't invalidate a negative entry
405 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
406 cache_enter(vdp
, *vpp
, cnp
);
413 * NOTE: We still have the buffer with matched directory entry at
416 isadir
= dep
->deAttributes
& ATTR_DIRECTORY
;
417 scn
= getushort(dep
->deStartCluster
);
419 scn
|= getushort(dep
->deHighClust
) << 16;
420 if (scn
== pmp
->pm_rootdirblk
) {
422 * There should actually be 0 here.
423 * Just ignore the error.
431 if (cluster
== MSDOSFSROOT
)
432 blkoff
= MSDOSFSROOT_OFS
;
435 } else if (cluster
== MSDOSFSROOT
)
439 * Now release buf to allow deget to read the entry again.
440 * Reserving it here and giving it to deget could result
447 * If we entered at foundroot, then we are looking for the . or ..
448 * entry of the filesystems root directory. isadir and scn were
449 * setup before jumping here. And, bp is already null.
451 if (FAT32(pmp
) && scn
== MSDOSFSROOT
)
452 scn
= pmp
->pm_rootdirblk
;
455 * If deleting, and at end of pathname, return
456 * parameters which can be used to remove file.
457 * Lock the inode, being careful with ".".
459 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
461 * Don't allow deleting the root.
463 if (blkoff
== MSDOSFSROOT_OFS
)
464 return EROFS
; /* really? XXX */
467 * Write access to directory required to delete files.
469 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
474 * Return pointer to current entry in dp->i_offset.
475 * Save directory inode pointer in ndp->ni_dvp for dirremove().
477 if (dp
->de_StartCluster
== scn
&& isadir
) { /* "." */
482 if ((error
= deget(pmp
, cluster
, blkoff
, &tdp
)) != 0)
489 * If rewriting (RENAME), return the inode and the
490 * information required to rewrite the present directory
491 * Must get inode of directory entry to verify it's a
492 * regular file, or empty directory.
494 if (nameiop
== RENAME
&& (flags
& ISLASTCN
)) {
496 if (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
)
499 if (blkoff
== MSDOSFSROOT_OFS
)
500 return EROFS
; /* really? XXX */
502 error
= VOP_ACCESS(vdp
, VWRITE
, cnp
->cn_cred
);
507 * Careful about locking second inode.
508 * This can only occur if the target is ".".
510 if (dp
->de_StartCluster
== scn
&& isadir
)
513 if ((error
= deget(pmp
, cluster
, blkoff
, &tdp
)) != 0)
516 cnp
->cn_flags
|= SAVENAME
;
521 * Step through the translation in the name. We do not `vput' the
522 * directory because we may need it again if a symbolic link
523 * is relative to the current directory. Instead we save it
524 * unlocked as "pdp". We must get the target inode before unlocking
525 * the directory to insure that the inode will not be removed
526 * before we get it. We prevent deadlock by always fetching
527 * inodes from the root, moving down the directory tree. Thus
528 * when following backward pointers ".." we must unlock the
529 * parent directory before getting the requested directory.
530 * There is a potential race condition here if both the current
531 * and parent directories are removed before the VFS_VGET for the
532 * inode associated with ".." returns. We hope that this occurs
533 * infrequently since we cannot avoid this race condition without
534 * implementing a sophisticated deadlock detection algorithm.
535 * Note also that this simple deadlock detection scheme will not
536 * work if the file system has any hard links other than ".."
537 * that point backwards in the directory structure.
540 if (flags
& ISDOTDOT
) {
541 VOP_UNLOCK(pdp
, 0); /* race to get the inode */
542 error
= deget(pmp
, cluster
, blkoff
, &tdp
);
543 vn_lock(pdp
, LK_EXCLUSIVE
| LK_RETRY
);
548 } else if (dp
->de_StartCluster
== scn
&& isadir
) {
549 vref(vdp
); /* we want ourself, ie "." */
552 if ((error
= deget(pmp
, cluster
, blkoff
, &tdp
)) != 0)
558 * Insert name into cache if appropriate.
560 if (cnp
->cn_flags
& MAKEENTRY
)
561 cache_enter(vdp
, *vpp
, cnp
);
567 * dep - directory entry to copy into the directory
568 * ddep - directory to add to
569 * depp - return the address of the denode for the created directory entry
571 * cnp - componentname needed for Win95 long filenames
574 createde(struct denode
*dep
, struct denode
*ddep
, struct denode
**depp
, struct componentname
*cnp
)
577 u_long dirclust
, clusoffset
;
578 u_long fndoffset
, havecnt
=0, wcnt
=1;
579 struct direntry
*ndep
;
580 struct msdosfsmount
*pmp
= ddep
->de_pmp
;
584 int async
= ddep
->de_pmp
->pm_mountp
->mnt_flag
& MNT_ASYNC
;
587 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
588 dep
, ddep
, depp
, cnp
);
592 * If no space left in the directory then allocate another cluster
593 * and chain it onto the end of the file. There is one exception
594 * to this. That is, if the root directory has no more space it
595 * can NOT be expanded. extendfile() checks for and fails attempts
596 * to extend the root directory. We just return an error in that
599 if (ddep
->de_fndoffset
>= ddep
->de_FileSize
) {
600 u_long needlen
= ddep
->de_fndoffset
+ sizeof(struct direntry
)
602 dirclust
= de_clcount(pmp
, needlen
);
603 if ((error
= extendfile(ddep
, dirclust
, 0, 0, DE_CLEAR
)) != 0) {
604 (void)detrunc(ddep
, ddep
->de_FileSize
, 0, NOCRED
);
609 * Update the size of the directory
611 ddep
->de_FileSize
+= de_cn2off(pmp
, dirclust
);
615 * We just read in the cluster with space. Copy the new directory
616 * entry in. Then write it to disk. NOTE: DOS directories
617 * do not get smaller as clusters are emptied.
619 error
= pcbmap(ddep
, de_cluster(pmp
, ddep
->de_fndoffset
),
620 &bn
, &dirclust
, &blsize
);
623 clusoffset
= ddep
->de_fndoffset
;
624 if (dirclust
!= MSDOSFSROOT
)
625 clusoffset
&= pmp
->pm_crbomask
;
626 if ((error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
627 B_MODIFY
, &bp
)) != 0) {
631 ndep
= bptoep(pmp
, bp
, clusoffset
);
633 DE_EXTERNALIZE(ndep
, dep
);
636 * Now write the Win95 long name
638 if (ddep
->de_fndcnt
> 0) {
639 u_int8_t chksum
= winChksum(ndep
->deName
);
640 const u_char
*un
= (const u_char
*)cnp
->cn_nameptr
;
641 int unlen
= cnp
->cn_namelen
;
644 fndoffset
= ddep
->de_fndoffset
;
645 xhavecnt
= ddep
->de_fndcnt
+ 1;
647 for(; wcnt
< xhavecnt
; wcnt
++) {
648 if ((fndoffset
& pmp
->pm_crbomask
) == 0) {
649 /* we should never get here if ddep is root
654 else if ((error
= bwrite(bp
)) != 0)
657 fndoffset
-= sizeof(struct direntry
);
659 de_cluster(pmp
, fndoffset
),
664 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
),
665 blsize
, NOCRED
, B_MODIFY
, &bp
);
670 ndep
= bptoep(pmp
, bp
,
671 fndoffset
& pmp
->pm_crbomask
);
674 fndoffset
-= sizeof(struct direntry
);
676 if (!unix2winfn(un
, unlen
, (struct winentry
*)ndep
,
684 else if ((error
= bwrite(bp
)) != 0)
688 * If they want us to return with the denode gotten.
691 u_long diroffset
= clusoffset
;
692 if (dep
->de_Attributes
& ATTR_DIRECTORY
) {
693 dirclust
= dep
->de_StartCluster
;
694 if (FAT32(pmp
) && dirclust
== pmp
->pm_rootdirblk
)
695 dirclust
= MSDOSFSROOT
;
696 if (dirclust
== MSDOSFSROOT
)
697 diroffset
= MSDOSFSROOT_OFS
;
701 return deget(pmp
, dirclust
, diroffset
, depp
);
708 * Mark all slots modified so far as deleted. Note that we
709 * can't just call removede(), since directory is not in
712 fndoffset
= ddep
->de_fndoffset
;
713 rberror
= pcbmap(ddep
, de_cluster(pmp
, fndoffset
),
717 if ((rberror
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
718 B_MODIFY
, &bp
)) != 0) {
722 ndep
= bptoep(pmp
, bp
, clusoffset
);
724 havecnt
= ddep
->de_fndcnt
+ 1;
725 for(i
=wcnt
; i
<= havecnt
; i
++) {
726 /* mark entry as deleted */
727 ndep
->deName
[0] = SLOT_DELETED
;
729 if ((fndoffset
& pmp
->pm_crbomask
) == 0) {
730 /* we should never get here if ddep is root
735 else if ((rberror
= bwrite(bp
)) != 0)
738 fndoffset
-= sizeof(struct direntry
);
739 rberror
= pcbmap(ddep
,
740 de_cluster(pmp
, fndoffset
),
745 rberror
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
),
746 blsize
, NOCRED
, B_MODIFY
, &bp
);
751 ndep
= bptoep(pmp
, bp
, fndoffset
);
754 fndoffset
-= sizeof(struct direntry
);
758 /* ignore any further error */
769 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
770 * return 0 if not empty or error.
773 dosdirempty(struct denode
*dep
)
780 struct msdosfsmount
*pmp
= dep
->de_pmp
;
781 struct direntry
*dentp
;
784 * Since the filesize field in directory entries for a directory is
785 * zero, we just have to feel our way through the directory until
786 * we hit end of file.
788 for (cn
= 0;; cn
++) {
789 if ((error
= pcbmap(dep
, cn
, &bn
, 0, &blsize
)) != 0) {
791 return (1); /* it's empty */
794 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
800 for (dentp
= (struct direntry
*)bp
->b_data
;
801 (char *)dentp
< (char *)bp
->b_data
+ blsize
;
803 if (dentp
->deName
[0] != SLOT_DELETED
&&
804 (dentp
->deAttributes
& ATTR_VOLUME
) == 0) {
806 * In dos directories an entry whose name
807 * starts with SLOT_EMPTY (0) starts the
808 * beginning of the unused part of the
809 * directory, so we can just return that it
812 if (dentp
->deName
[0] == SLOT_EMPTY
) {
817 * Any names other than "." and ".." in a
818 * directory mean it is not empty.
820 if (memcmp(dentp
->deName
, ". ", 11) &&
821 memcmp(dentp
->deName
, ".. ", 11)) {
824 printf("dosdirempty(): found %.11s, %d, %d\n",
825 dentp
->deName
, dentp
->deName
[0],
828 return (0); /* not empty */
838 * Check to see if the directory described by target is in some
839 * subdirectory of source. This prevents something like the following from
840 * succeeding and leaving a bunch or files and directories orphaned. mv
841 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
843 * source - the inode for /a/b/c
844 * target - the inode for /a/b/c/d/e/f
846 * Returns 0 if target is NOT a subdirectory of source.
847 * Otherwise returns a non-zero error number.
848 * The target inode is always unlocked on return.
851 doscheckpath(struct denode
*source
, struct denode
*target
)
854 struct msdosfsmount
*pmp
;
857 struct buf
*bp
= NULL
;
861 if ((target
->de_Attributes
& ATTR_DIRECTORY
) == 0 ||
862 (source
->de_Attributes
& ATTR_DIRECTORY
) == 0) {
866 if (dep
->de_StartCluster
== source
->de_StartCluster
) {
870 if (dep
->de_StartCluster
== MSDOSFSROOT
)
874 if (pmp
!= source
->de_pmp
)
875 panic("doscheckpath: source and target on different filesystems");
877 if (FAT32(pmp
) && dep
->de_StartCluster
== pmp
->pm_rootdirblk
)
881 if ((dep
->de_Attributes
& ATTR_DIRECTORY
) == 0) {
885 scn
= dep
->de_StartCluster
;
886 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, cntobn(pmp
, scn
)),
887 pmp
->pm_bpcluster
, NOCRED
, 0, &bp
);
891 ep
= (struct direntry
*) bp
->b_data
+ 1;
892 if ((ep
->deAttributes
& ATTR_DIRECTORY
) == 0 ||
893 memcmp(ep
->deName
, ".. ", 11) != 0) {
897 scn
= getushort(ep
->deStartCluster
);
899 scn
|= getushort(ep
->deHighClust
) << 16;
901 if (scn
== source
->de_StartCluster
) {
905 if (scn
== MSDOSFSROOT
)
907 if (FAT32(pmp
) && scn
== pmp
->pm_rootdirblk
) {
909 * scn should be 0 in this case,
910 * but we silently ignore the error.
918 /* NOTE: deget() clears dep on error */
919 if ((error
= deget(pmp
, scn
, 0, &dep
)) != 0)
925 if (error
== ENOTDIR
)
926 printf("doscheckpath(): .. not a directory?\n");
933 * Read in the disk block containing the directory entry (dirclu, dirofs)
934 * and return the address of the buf header, and the address of the
935 * directory entry within the block.
938 readep(struct msdosfsmount
*pmp
, u_long dirclust
, u_long diroffset
, struct buf
**bpp
, struct direntry
**epp
)
944 blsize
= pmp
->pm_bpcluster
;
945 if (dirclust
== MSDOSFSROOT
946 && de_blk(pmp
, diroffset
+ blsize
) > pmp
->pm_rootdirsize
)
947 blsize
= de_bn2off(pmp
, pmp
->pm_rootdirsize
) & pmp
->pm_crbomask
;
948 bn
= detobn(pmp
, dirclust
, diroffset
);
949 if ((error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
956 *epp
= bptoep(pmp
, *bpp
, diroffset
);
961 * Read in the disk block containing the directory entry dep came from and
962 * return the address of the buf header, and the address of the directory
963 * entry within the block.
966 readde(struct denode
*dep
, struct buf
**bpp
, struct direntry
**epp
)
968 return (readep(dep
->de_pmp
, dep
->de_dirclust
, dep
->de_diroffset
,
973 * Remove a directory entry. At this point the file represented by the
974 * directory entry to be removed is still full length until noone has it
975 * open. When the file no longer being used msdosfs_inactive() is called
976 * and will truncate the file to 0 length. When the vnode containing the
977 * denode is needed for some other purpose by VFS it will call
978 * msdosfs_reclaim() which will remove the denode from the denode cache.
981 removede(struct denode
*pdep
, struct denode
*dep
)
982 /* pdep: directory where the entry is removed */
983 /* dep: file to be removed */
990 struct msdosfsmount
*pmp
= pdep
->de_pmp
;
991 u_long offset
= pdep
->de_fndoffset
;
992 int async
= pdep
->de_pmp
->pm_mountp
->mnt_flag
& MNT_ASYNC
;
995 printf("removede(): filename %s, dep %p, offset %08lx\n",
996 dep
->de_Name
, dep
, offset
);
1000 offset
+= sizeof(struct direntry
);
1002 offset
-= sizeof(struct direntry
);
1003 error
= pcbmap(pdep
, de_cluster(pmp
, offset
), &bn
, 0, &blsize
);
1006 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
1012 ep
= bptoep(pmp
, bp
, offset
);
1014 * Check whether, if we came here the second time, i.e.
1015 * when underflowing into the previous block, the last
1016 * entry in this block is a longfilename entry, too.
1018 if (ep
->deAttributes
!= ATTR_WIN95
1019 && offset
!= pdep
->de_fndoffset
) {
1023 offset
+= sizeof(struct direntry
);
1026 * We are a bit agressive here in that we delete any Win95
1027 * entries preceding this entry, not just the ones we "own".
1028 * Since these presumably aren't valid anyway,
1029 * there should be no harm.
1031 offset
-= sizeof(struct direntry
);
1032 ep
--->deName
[0] = SLOT_DELETED
;
1033 if ((pmp
->pm_flags
& MSDOSFSMNT_NOWIN95
)
1034 || !(offset
& pmp
->pm_crbomask
)
1035 || ep
->deAttributes
!= ATTR_WIN95
)
1040 else if ((error
= bwrite(bp
)) != 0)
1042 } while (!(pmp
->pm_flags
& MSDOSFSMNT_NOWIN95
)
1043 && !(offset
& pmp
->pm_crbomask
)
1049 * Create a unique DOS name in dvp
1052 uniqdosname(struct denode
*dep
, struct componentname
*cnp
, u_char
*cp
)
1054 struct msdosfsmount
*pmp
= dep
->de_pmp
;
1055 struct direntry
*dentp
;
1063 for (gen
= 1;; gen
++) {
1065 * Generate DOS name with generation number
1067 if (!unix2dosfn((const u_char
*)cnp
->cn_nameptr
, cp
,
1068 cnp
->cn_namelen
, gen
))
1069 return gen
== 1 ? EINVAL
: EEXIST
;
1072 * Now look for a dir entry with this exact name
1074 for (cn
= error
= 0; !error
; cn
++) {
1075 if ((error
= pcbmap(dep
, cn
, &bn
, 0, &blsize
)) != 0) {
1076 if (error
== E2BIG
) /* EOF reached and not found */
1080 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
,
1086 for (dentp
= (struct direntry
*)bp
->b_data
;
1087 (char *)dentp
< (char *)bp
->b_data
+ blsize
;
1089 if (dentp
->deName
[0] == SLOT_EMPTY
) {
1091 * Last used entry and not found
1097 * Ignore volume labels and Win95 entries
1099 if (dentp
->deAttributes
& ATTR_VOLUME
)
1101 if (!memcmp(dentp
->deName
, cp
, 11)) {
1112 * Find any Win'95 long filename entry in directory dep
1115 findwin95(struct denode
*dep
)
1117 struct msdosfsmount
*pmp
= dep
->de_pmp
;
1118 struct direntry
*dentp
;
1126 * Read through the directory looking for Win'95 entries
1127 * XXX Note: Error currently handled just as EOF
1129 for (cn
= 0;; cn
++) {
1130 if (pcbmap(dep
, cn
, &bn
, 0, &blsize
))
1132 if (bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
), blsize
, NOCRED
,
1137 for (dentp
= (struct direntry
*)bp
->b_data
;
1138 (char *)dentp
< (char *)bp
->b_data
+ blsize
;
1140 if (dentp
->deName
[0] == SLOT_EMPTY
) {
1142 * Last used entry and not found
1147 if (dentp
->deName
[0] == SLOT_DELETED
) {
1149 * Ignore deleted files
1150 * Note: might be an indication of Win'95
1155 if (dentp
->deAttributes
== ATTR_WIN95
) {