HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_lookup.c
blobde610f976bb5eb9429fa0b0396f674aeaefa0487
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 $ */
5 /*-
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * All rights reserved.
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
13 * are met:
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.
49 * October 1992
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/buf.h>
55 #include <sys/vnode.h>
56 #include <sys/proc.h>
57 #include <sys/namei.h>
58 #include <sys/mount.h>
60 #include "bpb.h"
61 #include "direntry.h"
62 #include "denode.h"
63 #include "msdosfsmount.h"
64 #include "fat.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)
84 int
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;
90 daddr_t bn;
91 int error;
92 int lockparent;
93 int wantparent;
94 int slotcount;
95 int slotoffset = 0;
96 int frcn;
97 u_long cluster;
98 int blkoff;
99 int diroff;
100 int blsize;
101 int isadir; /* ~0 if found direntry is a directory */
102 u_long scn; /* starting cluster number */
103 struct vnode *pdp;
104 struct denode *dp;
105 struct denode *tdp;
106 struct msdosfsmount *pmp;
107 struct buf *bp = 0;
108 struct direntry *dep = NULL;
109 u_char dosfilename[12];
110 int flags = cnp->cn_flags;
111 int nameiop = cnp->cn_nameiop;
112 int unlen;
114 int wincnt = 1;
115 int chksum = -1;
116 int olddos = 1;
117 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
119 #ifdef MSDOSFS_DEBUG
120 kprintf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
121 #endif
122 dp = VTODE(vdp);
123 pmp = dp->de_pmp;
124 *vpp = NULL;
125 lockparent = flags & CNP_LOCKPARENT;
126 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
127 #ifdef MSDOSFS_DEBUG
128 kprintf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
129 vdp, dp, dp->de_Attributes);
130 #endif
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;
141 scn = MSDOSFSROOT;
142 #ifdef MSDOSFS_DEBUG
143 kprintf("msdosfs_lookup(): looking for . or .. in root directory\n");
144 #endif
145 cluster = MSDOSFSROOT;
146 blkoff = MSDOSFSROOT_OFS;
147 goto foundroot;
150 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
151 cnp->cn_namelen, 0,
152 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
153 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu)) {
154 case 0:
155 return (EINVAL);
156 case 1:
157 break;
158 case 2:
159 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
160 cnp->cn_namelen) + 1;
161 break;
162 case 3:
163 olddos = 0;
164 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
165 cnp->cn_namelen) + 1;
166 break;
168 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
169 wincnt = 1;
170 olddos = 1;
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.
180 slotcount = wincnt;
181 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)
182 slotcount = 0;
184 #ifdef MSDOSFS_DEBUG
185 kprintf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
186 dosfilename, cnp->cn_namelen);
187 #endif
189 * Search the directory pointed at by vdp for the name pointed at
190 * by cnp->cn_nameptr.
192 tdp = NULL;
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.
200 diroff = 0;
201 for (frcn = 0;; frcn++) {
202 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
203 if (error) {
204 if (error == E2BIG)
205 break;
206 return (error);
208 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
209 if (error) {
210 brelse(bp);
211 return (error);
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
231 chksum = -1;
233 if (slotcount < wincnt) {
234 slotcount++;
235 slotoffset = diroff;
237 if (dep->deName[0] == SLOT_EMPTY) {
238 brelse(bp);
239 goto notfound;
241 } else {
243 * If there wasn't enough space for our winentries,
244 * forget about the empty space
246 if (slotcount < wincnt)
247 slotcount = 0;
250 * Check for Win95 long filename entry
252 if (dep->deAttributes == ATTR_WIN95) {
253 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
254 continue;
256 chksum = winChkName((const u_char *)cnp->cn_nameptr,
257 unlen,
258 (struct winentry *)dep,
259 chksum,
260 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
261 pmp->pm_u2w,
262 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
263 pmp->pm_ul);
264 continue;
268 * Ignore volume labels (anywhere, not just
269 * the root directory).
271 if (dep->deAttributes & ATTR_VOLUME) {
272 chksum = -1;
273 continue;
277 * Check for a checksum or name match
279 if (chksum != winChksum(dep->deName)
280 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
281 chksum = -1;
282 continue;
284 #ifdef MSDOSFS_DEBUG
285 kprintf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
286 blkoff, diroff);
287 #endif
289 * Remember where this directory
290 * entry came from for whoever did
291 * this lookup.
293 dp->de_fndoffset = diroff;
294 dp->de_fndcnt = wincnt - 1;
296 goto found;
298 } /* for (blkoff = 0; .... */
300 * Release the buffer holding the directory cluster just
301 * searched.
303 brelse(bp);
304 } /* for (frcn = 0; ; frcn++) */
306 notfound:
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)
316 if (!slotcount) {
317 slotcount = 1;
318 slotoffset = diroff;
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.
328 #ifdef MSDOSFS_DEBUG
329 kprintf("msdosfs_lookup(): op %d, refcnt %ld\n",
330 nameiop, dp->de_refcnt);
331 kprintf(" slotcount %d, slotoffset %d\n",
332 slotcount, slotoffset);
333 #endif
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);
341 if (error)
342 return (error);
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.
363 if (!lockparent) {
364 vn_unlock(vdp);
365 cnp->cn_flags |= CNP_PDIRUNLOCK;
367 return (EJUSTRETURN);
369 return (ENOENT);
371 found:
373 * NOTE: We still have the buffer with matched directory entry at
374 * this point.
376 isadir = dep->deAttributes & ATTR_DIRECTORY;
377 scn = getushort(dep->deStartCluster);
378 if (FAT32(pmp)) {
379 scn |= getushort(dep->deHighClust) << 16;
380 if (scn == pmp->pm_rootdirblk) {
382 * There should actually be 0 here.
383 * Just ignore the error.
385 scn = MSDOSFSROOT;
389 if (isadir) {
390 cluster = scn;
391 if (cluster == MSDOSFSROOT)
392 blkoff = MSDOSFSROOT_OFS;
393 else
394 blkoff = 0;
395 } else if (cluster == MSDOSFSROOT)
396 blkoff = diroff;
399 * Now release buf to allow deget to read the entry again.
400 * Reserving it here and giving it to deget could result
401 * in a deadlock.
403 brelse(bp);
404 bp = 0;
406 foundroot:
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);
433 if (error)
434 return (error);
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) { /* "." */
441 vref(vdp);
442 *vpp = vdp;
443 return (0);
445 error = deget(pmp, cluster, blkoff, &tdp);
446 if (error)
447 return (error);
448 *vpp = DETOV(tdp);
449 if (!lockparent) {
450 vn_unlock(vdp);
451 cnp->cn_flags |= CNP_PDIRUNLOCK;
453 return (0);
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);
467 if (error)
468 return (error);
471 * Careful about locking second inode.
472 * This can only occur if the target is ".".
474 if (dp->de_StartCluster == scn && isadir)
475 return (EISDIR);
477 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
478 return (error);
479 *vpp = DETOV(tdp);
480 if (!lockparent) {
481 vn_unlock(vdp);
482 cnp->cn_flags |= CNP_PDIRUNLOCK;
484 return (0);
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.
506 pdp = vdp;
507 if (flags & CNP_ISDOTDOT) {
508 vn_unlock(pdp);
509 cnp->cn_flags |= CNP_PDIRUNLOCK;
510 error = deget(pmp, cluster, blkoff, &tdp);
511 if (error) {
512 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
513 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
514 return (error);
516 if (lockparent) {
517 error = vn_lock(pdp, LK_EXCLUSIVE);
518 if (error) {
519 vput(DETOV(tdp));
520 return (error);
522 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
524 *vpp = DETOV(tdp);
525 } else if (dp->de_StartCluster == scn && isadir) {
526 vref(vdp); /* we want ourself, ie "." */
527 *vpp = vdp;
528 } else {
529 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
530 return (error);
531 if (!lockparent) {
532 vn_unlock(pdp);
533 cnp->cn_flags |= CNP_PDIRUNLOCK;
535 *vpp = DETOV(tdp);
537 return (0);
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
544 * if depp != 0
545 * cnp - componentname needed for Win95 long filenames
548 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
549 struct componentname *cnp)
551 int error;
552 u_long dirclust, diroffset;
553 struct direntry *ndep;
554 struct msdosfsmount *pmp = ddep->de_pmp;
555 struct buf *bp;
556 daddr_t bn;
557 int blsize;
559 #ifdef MSDOSFS_DEBUG
560 kprintf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
561 dep, ddep, depp, cnp);
562 #endif
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
570 * case.
572 if (ddep->de_fndoffset >= ddep->de_FileSize) {
573 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
574 - ddep->de_FileSize;
575 dirclust = de_clcount(pmp, diroffset);
576 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
577 if (error) {
578 detrunc(ddep, ddep->de_FileSize, 0);
579 return error;
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);
595 if (error)
596 return error;
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) {
601 brelse(bp);
602 return error;
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;
615 int cnt = 1;
617 while (--ddep->de_fndcnt >= 0) {
618 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
619 if ((error = bwrite(bp)) != 0)
620 return error;
622 ddep->de_fndoffset -= sizeof(struct direntry);
623 error = pcbmap(ddep,
624 de_cluster(pmp,
625 ddep->de_fndoffset),
626 &bn, NULL, &blsize);
627 if (error)
628 return error;
630 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
631 if (error) {
632 brelse(bp);
633 return error;
635 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
636 } else {
637 ndep--;
638 ddep->de_fndoffset -= sizeof(struct direntry);
640 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
641 cnt++, chksum,
642 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
643 pmp->pm_u2w))
644 break;
648 if ((error = bwrite(bp)) != 0)
649 return error;
652 * If they want us to return with the denode gotten.
654 if (depp) {
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;
661 else
662 diroffset = 0;
664 return deget(pmp, dirclust, diroffset, depp);
667 return 0;
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)
677 int blsize;
678 int error;
679 u_long cn;
680 daddr_t bn;
681 struct buf *bp;
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) {
692 if (error == E2BIG)
693 return (1); /* it's empty */
694 return (0);
696 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
697 if (error) {
698 brelse(bp);
699 return (0);
701 for (dentp = (struct direntry *)bp->b_data;
702 (char *)dentp < bp->b_data + blsize;
703 dentp++) {
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
711 * is empty.
713 if (dentp->deName[0] == SLOT_EMPTY) {
714 brelse(bp);
715 return (1);
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)) {
723 brelse(bp);
724 #ifdef MSDOSFS_DEBUG
725 kprintf("dosdirempty(): entry found %02x, %02x\n",
726 dentp->deName[0], dentp->deName[1]);
727 #endif
728 return (0); /* not empty */
732 brelse(bp);
734 /* NOTREACHED */
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)
753 daddr_t scn;
754 struct msdosfsmount *pmp;
755 struct direntry *ep;
756 struct denode *dep;
757 struct buf *bp = NULL;
758 int error = 0;
760 dep = target;
761 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
762 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
763 error = ENOTDIR;
764 goto out;
766 if (dep->de_StartCluster == source->de_StartCluster) {
767 error = EEXIST;
768 goto out;
770 if (dep->de_StartCluster == MSDOSFSROOT)
771 goto out;
772 pmp = dep->de_pmp;
773 #ifdef DIAGNOSTIC
774 if (pmp != source->de_pmp)
775 panic("doscheckpath: source and target on different filesystems");
776 #endif
777 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
778 goto out;
780 for (;;) {
781 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
782 error = ENOTDIR;
783 break;
785 scn = dep->de_StartCluster;
786 error = bread(pmp->pm_devvp, xcntodoff(pmp, scn),
787 pmp->pm_bpcluster, &bp);
788 if (error)
789 break;
791 ep = (struct direntry *) bp->b_data + 1;
792 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
793 bcmp(ep->deName, ".. ", 11) != 0) {
794 error = ENOTDIR;
795 break;
797 scn = getushort(ep->deStartCluster);
798 if (FAT32(pmp))
799 scn |= getushort(ep->deHighClust) << 16;
801 if (scn == source->de_StartCluster) {
802 error = EINVAL;
803 break;
805 if (scn == MSDOSFSROOT)
806 break;
807 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
809 * scn should be 0 in this case,
810 * but we silently ignore the error.
812 break;
815 vput(DETOV(dep));
816 brelse(bp);
817 bp = NULL;
818 /* NOTE: deget() clears dep on error */
819 if ((error = deget(pmp, scn, 0, &dep)) != 0)
820 break;
822 out:;
823 if (bp)
824 brelse(bp);
825 if (error == ENOTDIR)
826 kprintf("doscheckpath(): .. not a directory?\n");
827 if (dep != NULL)
828 vput(DETOV(dep));
829 return (error);
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)
841 int error;
842 daddr_t bn;
843 int blsize;
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) {
851 brelse(*bpp);
852 *bpp = NULL;
853 return (error);
855 if (epp)
856 *epp = bptoep(pmp, *bpp, diroffset);
857 return (0);
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,
869 bpp, epp));
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 */
884 int error;
885 struct direntry *ep;
886 struct buf *bp;
887 daddr_t bn;
888 int blsize;
889 struct msdosfsmount *pmp = pdep->de_pmp;
890 u_long offset = pdep->de_fndoffset;
892 #ifdef MSDOSFS_DEBUG
893 kprintf("removede(): filename %s, dep %p, offset %08lx\n",
894 dep->de_Name, dep, offset);
895 #endif
897 dep->de_refcnt--;
898 offset += sizeof(struct direntry);
899 do {
900 offset -= sizeof(struct direntry);
901 error = pcbmap(pdep, de_cluster(pmp, offset),
902 &bn, NULL, &blsize);
903 if (error)
904 return error;
905 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
906 if (error) {
907 brelse(bp);
908 return error;
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) {
918 brelse(bp);
919 break;
921 offset += sizeof(struct direntry);
922 while (1) {
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)
934 break;
936 if ((error = bwrite(bp)) != 0)
937 return error;
938 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
939 && !(offset & pmp->pm_crbomask)
940 && offset);
941 return 0;
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;
952 int gen;
953 int blsize;
954 u_long cn;
955 daddr_t bn;
956 struct buf *bp;
957 int error;
959 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
960 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
961 cnp->cn_namelen, 0,
962 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
963 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu) ?
964 0 : EINVAL);
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 */
982 return 0;
983 return error;
985 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
986 if (error) {
987 brelse(bp);
988 return error;
990 for (dentp = (struct direntry *)bp->b_data;
991 (char *)dentp < bp->b_data + blsize;
992 dentp++) {
993 if (dentp->deName[0] == SLOT_EMPTY) {
995 * Last used entry and not found
997 brelse(bp);
998 return 0;
1001 * Ignore volume labels and Win95 entries
1003 if (dentp->deAttributes & ATTR_VOLUME)
1004 continue;
1005 if (!bcmp(dentp->deName, cp, 11)) {
1006 error = EEXIST;
1007 break;
1010 brelse(bp);
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;
1023 int blsize, win95;
1024 u_long cn;
1025 daddr_t bn;
1026 struct buf *bp;
1028 win95 = 1;
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))
1035 return (win95);
1036 if (bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp)) {
1037 brelse(bp);
1038 return (win95);
1040 for (dentp = (struct direntry *)bp->b_data;
1041 (char *)dentp < bp->b_data + blsize;
1042 dentp++) {
1043 if (dentp->deName[0] == SLOT_EMPTY) {
1045 * Last used entry and not found
1047 brelse(bp);
1048 return (win95);
1050 if (dentp->deName[0] == SLOT_DELETED) {
1052 * Ignore deleted files
1053 * Note: might be an indication of Win'95 anyway XXX
1055 continue;
1057 if (dentp->deAttributes == ATTR_WIN95) {
1058 brelse(bp);
1059 return 1;
1061 win95 = 0;
1063 brelse(bp);