Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_lookup.c
CommitLineData
984263bc
MD
1/* $FreeBSD: src/sys/msdosfs/msdosfs_lookup.c,v 1.30.2.1 2000/11/03 15:55:39 bp Exp $ */
2/* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/namei.h>
54#include <sys/buf.h>
55#include <sys/vnode.h>
56#include <sys/mount.h>
57
58#include <msdosfs/bpb.h>
59#include <msdosfs/direntry.h>
60#include <msdosfs/denode.h>
61#include <msdosfs/msdosfsmount.h>
62#include <msdosfs/fat.h>
63
64/*
65 * When we search a directory the blocks containing directory entries are
66 * read and examined. The directory entries contain information that would
67 * normally be in the inode of a unix filesystem. This means that some of
68 * a directory's contents may also be in memory resident denodes (sort of
69 * an inode). This can cause problems if we are searching while some other
70 * process is modifying a directory. To prevent one process from accessing
71 * incompletely modified directory information we depend upon being the
72 * sole owner of a directory block. bread/brelse provide this service.
73 * This being the case, when a process modifies a directory it must first
74 * acquire the disk block that contains the directory entry to be modified.
75 * Then update the disk block and the denode, and then write the disk block
76 * out to disk. This way disk blocks containing directory entries and in
77 * memory denode's will be in synch.
78 */
79int
80msdosfs_lookup(ap)
81 struct vop_cachedlookup_args /* {
82 struct vnode *a_dvp;
83 struct vnode **a_vpp;
84 struct componentname *a_cnp;
85 } */ *ap;
86{
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 struct proc *p = cnp->cn_proc;
113 int unlen;
114
115 int wincnt = 1;
116 int chksum = -1;
117 int olddos = 1;
118 cnp->cn_flags &= ~PDIRUNLOCK;
119
120#ifdef MSDOSFS_DEBUG
121 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
122#endif
123 dp = VTODE(vdp);
124 pmp = dp->de_pmp;
125 *vpp = NULL;
126 lockparent = flags & LOCKPARENT;
127 wantparent = flags & (LOCKPARENT | WANTPARENT);
128#ifdef MSDOSFS_DEBUG
129 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
130 vdp, dp, dp->de_Attributes);
131#endif
132
133 /*
134 * If they are going after the . or .. entry in the root directory,
135 * they won't find it. DOS filesystems don't have them in the root
136 * directory. So, we fake it. deget() is in on this scam too.
137 */
138 if ((vdp->v_flag & VROOT) && cnp->cn_nameptr[0] == '.' &&
139 (cnp->cn_namelen == 1 ||
140 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
141 isadir = ATTR_DIRECTORY;
142 scn = MSDOSFSROOT;
143#ifdef MSDOSFS_DEBUG
144 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
145#endif
146 cluster = MSDOSFSROOT;
147 blkoff = MSDOSFSROOT_OFS;
148 goto foundroot;
149 }
150
151 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
152 cnp->cn_namelen, 0,
153 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
154 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu)) {
155 case 0:
156 return (EINVAL);
157 case 1:
158 break;
159 case 2:
160 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
161 cnp->cn_namelen) + 1;
162 break;
163 case 3:
164 olddos = 0;
165 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
166 cnp->cn_namelen) + 1;
167 break;
168 }
169 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
170 wincnt = 1;
171 olddos = 1;
172 }
173 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
174
175 /*
176 * Suppress search for slots unless creating
177 * file and at end of pathname, in which case
178 * we watch for a place to put the new file in
179 * case it doesn't already exist.
180 */
181 slotcount = wincnt;
182 if ((nameiop == CREATE || nameiop == RENAME) &&
183 (flags & ISLASTCN))
184 slotcount = 0;
185
186#ifdef MSDOSFS_DEBUG
187 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
188 dosfilename, cnp->cn_namelen);
189#endif
190 /*
191 * Search the directory pointed at by vdp for the name pointed at
192 * by cnp->cn_nameptr.
193 */
194 tdp = NULL;
195 /*
196 * The outer loop ranges over the clusters that make up the
197 * directory. Note that the root directory is different from all
198 * other directories. It has a fixed number of blocks that are not
199 * part of the pool of allocatable clusters. So, we treat it a
200 * little differently. The root directory starts at "cluster" 0.
201 */
202 diroff = 0;
203 for (frcn = 0;; frcn++) {
204 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
205 if (error) {
206 if (error == E2BIG)
207 break;
208 return (error);
209 }
210 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
211 if (error) {
212 brelse(bp);
213 return (error);
214 }
215 for (blkoff = 0; blkoff < blsize;
216 blkoff += sizeof(struct direntry),
217 diroff += sizeof(struct direntry)) {
218 dep = (struct direntry *)(bp->b_data + blkoff);
219 /*
220 * If the slot is empty and we are still looking
221 * for an empty then remember this one. If the
222 * slot is not empty then check to see if it
223 * matches what we are looking for. If the slot
224 * has never been filled with anything, then the
225 * remainder of the directory has never been used,
226 * so there is no point in searching it.
227 */
228 if (dep->deName[0] == SLOT_EMPTY ||
229 dep->deName[0] == SLOT_DELETED) {
230 /*
231 * Drop memory of previous long matches
232 */
233 chksum = -1;
234
235 if (slotcount < wincnt) {
236 slotcount++;
237 slotoffset = diroff;
238 }
239 if (dep->deName[0] == SLOT_EMPTY) {
240 brelse(bp);
241 goto notfound;
242 }
243 } else {
244 /*
245 * If there wasn't enough space for our winentries,
246 * forget about the empty space
247 */
248 if (slotcount < wincnt)
249 slotcount = 0;
250
251 /*
252 * Check for Win95 long filename entry
253 */
254 if (dep->deAttributes == ATTR_WIN95) {
255 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
256 continue;
257
258 chksum = winChkName((const u_char *)cnp->cn_nameptr,
259 unlen,
260 (struct winentry *)dep,
261 chksum,
262 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
263 pmp->pm_u2w,
264 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
265 pmp->pm_ul);
266 continue;
267 }
268
269 /*
270 * Ignore volume labels (anywhere, not just
271 * the root directory).
272 */
273 if (dep->deAttributes & ATTR_VOLUME) {
274 chksum = -1;
275 continue;
276 }
277
278 /*
279 * Check for a checksum or name match
280 */
281 if (chksum != winChksum(dep->deName)
282 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
283 chksum = -1;
284 continue;
285 }
286#ifdef MSDOSFS_DEBUG
287 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
288 blkoff, diroff);
289#endif
290 /*
291 * Remember where this directory
292 * entry came from for whoever did
293 * this lookup.
294 */
295 dp->de_fndoffset = diroff;
296 dp->de_fndcnt = wincnt - 1;
297
298 goto found;
299 }
300 } /* for (blkoff = 0; .... */
301 /*
302 * Release the buffer holding the directory cluster just
303 * searched.
304 */
305 brelse(bp);
306 } /* for (frcn = 0; ; frcn++) */
307
308notfound:
309 /*
310 * We hold no disk buffers at this point.
311 */
312
313 /*
314 * Fixup the slot description to point to the place where
315 * we might put the new DOS direntry (putting the Win95
316 * long name entries before that)
317 */
318 if (!slotcount) {
319 slotcount = 1;
320 slotoffset = diroff;
321 }
322 if (wincnt > slotcount)
323 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
324
325 /*
326 * If we get here we didn't find the entry we were looking for. But
327 * that's ok if we are creating or renaming and are at the end of
328 * the pathname and the directory hasn't been removed.
329 */
330#ifdef MSDOSFS_DEBUG
331 printf("msdosfs_lookup(): op %d, refcnt %ld\n",
332 nameiop, dp->de_refcnt);
333 printf(" slotcount %d, slotoffset %d\n",
334 slotcount, slotoffset);
335#endif
336 if ((nameiop == CREATE || nameiop == RENAME) &&
337 (flags & ISLASTCN) && dp->de_refcnt != 0) {
338 /*
339 * Access for write is interpreted as allowing
340 * creation of files in the directory.
341 */
342 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc);
343 if (error)
344 return (error);
345 /*
346 * Return an indication of where the new directory
347 * entry should be put.
348 */
349 dp->de_fndoffset = slotoffset;
350 dp->de_fndcnt = wincnt - 1;
351
352 /*
353 * We return with the directory locked, so that
354 * the parameters we set up above will still be
355 * valid if we actually decide to do a direnter().
356 * We return ni_vp == NULL to indicate that the entry
357 * does not currently exist; we leave a pointer to
358 * the (locked) directory inode in ndp->ni_dvp.
359 * The pathname buffer is saved so that the name
360 * can be obtained later.
361 *
362 * NB - if the directory is unlocked, then this
363 * information cannot be used.
364 */
365 cnp->cn_flags |= SAVENAME;
366 if (!lockparent) {
367 VOP_UNLOCK(vdp, 0, p);
368 cnp->cn_flags |= PDIRUNLOCK;
369 }
370 return (EJUSTRETURN);
371 }
372 /*
373 * Insert name into cache (as non-existent) if appropriate.
374 */
375 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
376 cache_enter(vdp, *vpp, cnp);
377 return (ENOENT);
378
379found:
380 /*
381 * NOTE: We still have the buffer with matched directory entry at
382 * this point.
383 */
384 isadir = dep->deAttributes & ATTR_DIRECTORY;
385 scn = getushort(dep->deStartCluster);
386 if (FAT32(pmp)) {
387 scn |= getushort(dep->deHighClust) << 16;
388 if (scn == pmp->pm_rootdirblk) {
389 /*
390 * There should actually be 0 here.
391 * Just ignore the error.
392 */
393 scn = MSDOSFSROOT;
394 }
395 }
396
397 if (isadir) {
398 cluster = scn;
399 if (cluster == MSDOSFSROOT)
400 blkoff = MSDOSFSROOT_OFS;
401 else
402 blkoff = 0;
403 } else if (cluster == MSDOSFSROOT)
404 blkoff = diroff;
405
406 /*
407 * Now release buf to allow deget to read the entry again.
408 * Reserving it here and giving it to deget could result
409 * in a deadlock.
410 */
411 brelse(bp);
412 bp = 0;
413
414foundroot:
415 /*
416 * If we entered at foundroot, then we are looking for the . or ..
417 * entry of the filesystems root directory. isadir and scn were
418 * setup before jumping here. And, bp is already null.
419 */
420 if (FAT32(pmp) && scn == MSDOSFSROOT)
421 scn = pmp->pm_rootdirblk;
422
423 /*
424 * If deleting, and at end of pathname, return
425 * parameters which can be used to remove file.
426 * If the wantparent flag isn't set, we return only
427 * the directory (in ndp->ni_dvp), otherwise we go
428 * on and lock the inode, being careful with ".".
429 */
430 if (nameiop == DELETE && (flags & ISLASTCN)) {
431 /*
432 * Don't allow deleting the root.
433 */
434 if (blkoff == MSDOSFSROOT_OFS)
435 return EROFS; /* really? XXX */
436
437 /*
438 * Write access to directory required to delete files.
439 */
440 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc);
441 if (error)
442 return (error);
443
444 /*
445 * Return pointer to current entry in dp->i_offset.
446 * Save directory inode pointer in ndp->ni_dvp for dirremove().
447 */
448 if (dp->de_StartCluster == scn && isadir) { /* "." */
449 VREF(vdp);
450 *vpp = vdp;
451 return (0);
452 }
453 error = deget(pmp, cluster, blkoff, &tdp);
454 if (error)
455 return (error);
456 *vpp = DETOV(tdp);
457 if (!lockparent) {
458 VOP_UNLOCK(vdp, 0, p);
459 cnp->cn_flags |= PDIRUNLOCK;
460 }
461 return (0);
462 }
463
464 /*
465 * If rewriting (RENAME), return the inode and the
466 * information required to rewrite the present directory
467 * Must get inode of directory entry to verify it's a
468 * regular file, or empty directory.
469 */
470 if (nameiop == RENAME && wantparent &&
471 (flags & ISLASTCN)) {
472 if (blkoff == MSDOSFSROOT_OFS)
473 return EROFS; /* really? XXX */
474
475 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc);
476 if (error)
477 return (error);
478
479 /*
480 * Careful about locking second inode.
481 * This can only occur if the target is ".".
482 */
483 if (dp->de_StartCluster == scn && isadir)
484 return (EISDIR);
485
486 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
487 return (error);
488 *vpp = DETOV(tdp);
489 cnp->cn_flags |= SAVENAME;
490 if (!lockparent) {
491 VOP_UNLOCK(vdp, 0, p);
492 cnp->cn_flags |= PDIRUNLOCK;
493 }
494 return (0);
495 }
496
497 /*
498 * Step through the translation in the name. We do not `vput' the
499 * directory because we may need it again if a symbolic link
500 * is relative to the current directory. Instead we save it
501 * unlocked as "pdp". We must get the target inode before unlocking
502 * the directory to insure that the inode will not be removed
503 * before we get it. We prevent deadlock by always fetching
504 * inodes from the root, moving down the directory tree. Thus
505 * when following backward pointers ".." we must unlock the
506 * parent directory before getting the requested directory.
507 * There is a potential race condition here if both the current
508 * and parent directories are removed before the VFS_VGET for the
509 * inode associated with ".." returns. We hope that this occurs
510 * infrequently since we cannot avoid this race condition without
511 * implementing a sophisticated deadlock detection algorithm.
512 * Note also that this simple deadlock detection scheme will not
513 * work if the file system has any hard links other than ".."
514 * that point backwards in the directory structure.
515 */
516 pdp = vdp;
517 if (flags & ISDOTDOT) {
518 VOP_UNLOCK(pdp, 0, p);
519 cnp->cn_flags |= PDIRUNLOCK;
520 error = deget(pmp, cluster, blkoff, &tdp);
521 if (error) {
522 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p);
523 cnp->cn_flags &= ~PDIRUNLOCK;
524 return (error);
525 }
526 if (lockparent && (flags & ISLASTCN)) {
527 error = vn_lock(pdp, LK_EXCLUSIVE, p);
528 if (error) {
529 vput(DETOV(tdp));
530 return (error);
531 }
532 cnp->cn_flags &= ~PDIRUNLOCK;
533 }
534 *vpp = DETOV(tdp);
535 } else if (dp->de_StartCluster == scn && isadir) {
536 VREF(vdp); /* we want ourself, ie "." */
537 *vpp = vdp;
538 } else {
539 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
540 return (error);
541 if (!lockparent || !(flags & ISLASTCN)) {
542 VOP_UNLOCK(pdp, 0, p);
543 cnp->cn_flags |= PDIRUNLOCK;
544 }
545 *vpp = DETOV(tdp);
546 }
547
548 /*
549 * Insert name into cache if appropriate.
550 */
551 if (cnp->cn_flags & MAKEENTRY)
552 cache_enter(vdp, *vpp, cnp);
553 return (0);
554}
555
556/*
557 * dep - directory entry to copy into the directory
558 * ddep - directory to add to
559 * depp - return the address of the denode for the created directory entry
560 * if depp != 0
561 * cnp - componentname needed for Win95 long filenames
562 */
563int
564createde(dep, ddep, depp, cnp)
565 struct denode *dep;
566 struct denode *ddep;
567 struct denode **depp;
568 struct componentname *cnp;
569{
570 int error;
571 u_long dirclust, diroffset;
572 struct direntry *ndep;
573 struct msdosfsmount *pmp = ddep->de_pmp;
574 struct buf *bp;
575 daddr_t bn;
576 int blsize;
577
578#ifdef MSDOSFS_DEBUG
579 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
580 dep, ddep, depp, cnp);
581#endif
582
583 /*
584 * If no space left in the directory then allocate another cluster
585 * and chain it onto the end of the file. There is one exception
586 * to this. That is, if the root directory has no more space it
587 * can NOT be expanded. extendfile() checks for and fails attempts
588 * to extend the root directory. We just return an error in that
589 * case.
590 */
591 if (ddep->de_fndoffset >= ddep->de_FileSize) {
592 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
593 - ddep->de_FileSize;
594 dirclust = de_clcount(pmp, diroffset);
595 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
596 if (error) {
597 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL);
598 return error;
599 }
600
601 /*
602 * Update the size of the directory
603 */
604 ddep->de_FileSize += de_cn2off(pmp, dirclust);
605 }
606
607 /*
608 * We just read in the cluster with space. Copy the new directory
609 * entry in. Then write it to disk. NOTE: DOS directories
610 * do not get smaller as clusters are emptied.
611 */
612 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
613 &bn, &dirclust, &blsize);
614 if (error)
615 return error;
616 diroffset = ddep->de_fndoffset;
617 if (dirclust != MSDOSFSROOT)
618 diroffset &= pmp->pm_crbomask;
619 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
620 brelse(bp);
621 return error;
622 }
623 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
624
625 DE_EXTERNALIZE(ndep, dep);
626
627 /*
628 * Now write the Win95 long name
629 */
630 if (ddep->de_fndcnt > 0) {
631 u_int8_t chksum = winChksum(ndep->deName);
632 const u_char *un = (const u_char *)cnp->cn_nameptr;
633 int unlen = cnp->cn_namelen;
634 int cnt = 1;
635
636 while (--ddep->de_fndcnt >= 0) {
637 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
638 if ((error = bwrite(bp)) != 0)
639 return error;
640
641 ddep->de_fndoffset -= sizeof(struct direntry);
642 error = pcbmap(ddep,
643 de_cluster(pmp,
644 ddep->de_fndoffset),
645 &bn, 0, &blsize);
646 if (error)
647 return error;
648
649 error = bread(pmp->pm_devvp, bn, blsize,
650 NOCRED, &bp);
651 if (error) {
652 brelse(bp);
653 return error;
654 }
655 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
656 } else {
657 ndep--;
658 ddep->de_fndoffset -= sizeof(struct direntry);
659 }
660 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
661 cnt++, chksum,
662 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
663 pmp->pm_u2w))
664 break;
665 }
666 }
667
668 if ((error = bwrite(bp)) != 0)
669 return error;
670
671 /*
672 * If they want us to return with the denode gotten.
673 */
674 if (depp) {
675 if (dep->de_Attributes & ATTR_DIRECTORY) {
676 dirclust = dep->de_StartCluster;
677 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
678 dirclust = MSDOSFSROOT;
679 if (dirclust == MSDOSFSROOT)
680 diroffset = MSDOSFSROOT_OFS;
681 else
682 diroffset = 0;
683 }
684 return deget(pmp, dirclust, diroffset, depp);
685 }
686
687 return 0;
688}
689
690/*
691 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
692 * return 0 if not empty or error.
693 */
694int
695dosdirempty(dep)
696 struct denode *dep;
697{
698 int blsize;
699 int error;
700 u_long cn;
701 daddr_t bn;
702 struct buf *bp;
703 struct msdosfsmount *pmp = dep->de_pmp;
704 struct direntry *dentp;
705
706 /*
707 * Since the filesize field in directory entries for a directory is
708 * zero, we just have to feel our way through the directory until
709 * we hit end of file.
710 */
711 for (cn = 0;; cn++) {
712 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
713 if (error == E2BIG)
714 return (1); /* it's empty */
715 return (0);
716 }
717 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
718 if (error) {
719 brelse(bp);
720 return (0);
721 }
722 for (dentp = (struct direntry *)bp->b_data;
723 (char *)dentp < bp->b_data + blsize;
724 dentp++) {
725 if (dentp->deName[0] != SLOT_DELETED &&
726 (dentp->deAttributes & ATTR_VOLUME) == 0) {
727 /*
728 * In dos directories an entry whose name
729 * starts with SLOT_EMPTY (0) starts the
730 * beginning of the unused part of the
731 * directory, so we can just return that it
732 * is empty.
733 */
734 if (dentp->deName[0] == SLOT_EMPTY) {
735 brelse(bp);
736 return (1);
737 }
738 /*
739 * Any names other than "." and ".." in a
740 * directory mean it is not empty.
741 */
742 if (bcmp(dentp->deName, ". ", 11) &&
743 bcmp(dentp->deName, ".. ", 11)) {
744 brelse(bp);
745#ifdef MSDOSFS_DEBUG
746 printf("dosdirempty(): entry found %02x, %02x\n",
747 dentp->deName[0], dentp->deName[1]);
748#endif
749 return (0); /* not empty */
750 }
751 }
752 }
753 brelse(bp);
754 }
755 /* NOTREACHED */
756}
757
758/*
759 * Check to see if the directory described by target is in some
760 * subdirectory of source. This prevents something like the following from
761 * succeeding and leaving a bunch or files and directories orphaned. mv
762 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
763 *
764 * source - the inode for /a/b/c
765 * target - the inode for /a/b/c/d/e/f
766 *
767 * Returns 0 if target is NOT a subdirectory of source.
768 * Otherwise returns a non-zero error number.
769 * The target inode is always unlocked on return.
770 */
771int
772doscheckpath(source, target)
773 struct denode *source;
774 struct denode *target;
775{
776 daddr_t scn;
777 struct msdosfsmount *pmp;
778 struct direntry *ep;
779 struct denode *dep;
780 struct buf *bp = NULL;
781 int error = 0;
782
783 dep = target;
784 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
785 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
786 error = ENOTDIR;
787 goto out;
788 }
789 if (dep->de_StartCluster == source->de_StartCluster) {
790 error = EEXIST;
791 goto out;
792 }
793 if (dep->de_StartCluster == MSDOSFSROOT)
794 goto out;
795 pmp = dep->de_pmp;
796#ifdef DIAGNOSTIC
797 if (pmp != source->de_pmp)
798 panic("doscheckpath: source and target on different filesystems");
799#endif
800 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
801 goto out;
802
803 for (;;) {
804 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
805 error = ENOTDIR;
806 break;
807 }
808 scn = dep->de_StartCluster;
809 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
810 pmp->pm_bpcluster, NOCRED, &bp);
811 if (error)
812 break;
813
814 ep = (struct direntry *) bp->b_data + 1;
815 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
816 bcmp(ep->deName, ".. ", 11) != 0) {
817 error = ENOTDIR;
818 break;
819 }
820 scn = getushort(ep->deStartCluster);
821 if (FAT32(pmp))
822 scn |= getushort(ep->deHighClust) << 16;
823
824 if (scn == source->de_StartCluster) {
825 error = EINVAL;
826 break;
827 }
828 if (scn == MSDOSFSROOT)
829 break;
830 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
831 /*
832 * scn should be 0 in this case,
833 * but we silently ignore the error.
834 */
835 break;
836 }
837
838 vput(DETOV(dep));
839 brelse(bp);
840 bp = NULL;
841 /* NOTE: deget() clears dep on error */
842 if ((error = deget(pmp, scn, 0, &dep)) != 0)
843 break;
844 }
845out:;
846 if (bp)
847 brelse(bp);
848 if (error == ENOTDIR)
849 printf("doscheckpath(): .. not a directory?\n");
850 if (dep != NULL)
851 vput(DETOV(dep));
852 return (error);
853}
854
855/*
856 * Read in the disk block containing the directory entry (dirclu, dirofs)
857 * and return the address of the buf header, and the address of the
858 * directory entry within the block.
859 */
860int
861readep(pmp, dirclust, diroffset, bpp, epp)
862 struct msdosfsmount *pmp;
863 u_long dirclust, diroffset;
864 struct buf **bpp;
865 struct direntry **epp;
866{
867 int error;
868 daddr_t bn;
869 int blsize;
870
871 blsize = pmp->pm_bpcluster;
872 if (dirclust == MSDOSFSROOT
873 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
874 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
875 bn = detobn(pmp, dirclust, diroffset);
876 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
877 brelse(*bpp);
878 *bpp = NULL;
879 return (error);
880 }
881 if (epp)
882 *epp = bptoep(pmp, *bpp, diroffset);
883 return (0);
884}
885
886/*
887 * Read in the disk block containing the directory entry dep came from and
888 * return the address of the buf header, and the address of the directory
889 * entry within the block.
890 */
891int
892readde(dep, bpp, epp)
893 struct denode *dep;
894 struct buf **bpp;
895 struct direntry **epp;
896{
897
898 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
899 bpp, epp));
900}
901
902/*
903 * Remove a directory entry. At this point the file represented by the
904 * directory entry to be removed is still full length until noone has it
905 * open. When the file no longer being used msdosfs_inactive() is called
906 * and will truncate the file to 0 length. When the vnode containing the
907 * denode is needed for some other purpose by VFS it will call
908 * msdosfs_reclaim() which will remove the denode from the denode cache.
909 */
910int
911removede(pdep, dep)
912 struct denode *pdep; /* directory where the entry is removed */
913 struct denode *dep; /* file to be removed */
914{
915 int error;
916 struct direntry *ep;
917 struct buf *bp;
918 daddr_t bn;
919 int blsize;
920 struct msdosfsmount *pmp = pdep->de_pmp;
921 u_long offset = pdep->de_fndoffset;
922
923#ifdef MSDOSFS_DEBUG
924 printf("removede(): filename %s, dep %p, offset %08lx\n",
925 dep->de_Name, dep, offset);
926#endif
927
928 dep->de_refcnt--;
929 offset += sizeof(struct direntry);
930 do {
931 offset -= sizeof(struct direntry);
932 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
933 if (error)
934 return error;
935 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
936 if (error) {
937 brelse(bp);
938 return error;
939 }
940 ep = bptoep(pmp, bp, offset);
941 /*
942 * Check whether, if we came here the second time, i.e.
943 * when underflowing into the previous block, the last
944 * entry in this block is a longfilename entry, too.
945 */
946 if (ep->deAttributes != ATTR_WIN95
947 && offset != pdep->de_fndoffset) {
948 brelse(bp);
949 break;
950 }
951 offset += sizeof(struct direntry);
952 while (1) {
953 /*
954 * We are a bit agressive here in that we delete any Win95
955 * entries preceding this entry, not just the ones we "own".
956 * Since these presumably aren't valid anyway,
957 * there should be no harm.
958 */
959 offset -= sizeof(struct direntry);
960 ep--->deName[0] = SLOT_DELETED;
961 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
962 || !(offset & pmp->pm_crbomask)
963 || ep->deAttributes != ATTR_WIN95)
964 break;
965 }
966 if ((error = bwrite(bp)) != 0)
967 return error;
968 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
969 && !(offset & pmp->pm_crbomask)
970 && offset);
971 return 0;
972}
973
974/*
975 * Create a unique DOS name in dvp
976 */
977int
978uniqdosname(dep, cnp, cp)
979 struct denode *dep;
980 struct componentname *cnp;
981 u_char *cp;
982{
983 struct msdosfsmount *pmp = dep->de_pmp;
984 struct direntry *dentp;
985 int gen;
986 int blsize;
987 u_long cn;
988 daddr_t bn;
989 struct buf *bp;
990 int error;
991
992 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
993 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
994 cnp->cn_namelen, 0,
995 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
996 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu) ?
997 0 : EINVAL);
998
999 for (gen = 1;; gen++) {
1000 /*
1001 * Generate DOS name with generation number
1002 */
1003 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1004 cnp->cn_namelen, gen,
1005 pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d,
1006 pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu))
1007 return gen == 1 ? EINVAL : EEXIST;
1008
1009 /*
1010 * Now look for a dir entry with this exact name
1011 */
1012 for (cn = error = 0; !error; cn++) {
1013 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1014 if (error == E2BIG) /* EOF reached and not found */
1015 return 0;
1016 return error;
1017 }
1018 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1019 if (error) {
1020 brelse(bp);
1021 return error;
1022 }
1023 for (dentp = (struct direntry *)bp->b_data;
1024 (char *)dentp < bp->b_data + blsize;
1025 dentp++) {
1026 if (dentp->deName[0] == SLOT_EMPTY) {
1027 /*
1028 * Last used entry and not found
1029 */
1030 brelse(bp);
1031 return 0;
1032 }
1033 /*
1034 * Ignore volume labels and Win95 entries
1035 */
1036 if (dentp->deAttributes & ATTR_VOLUME)
1037 continue;
1038 if (!bcmp(dentp->deName, cp, 11)) {
1039 error = EEXIST;
1040 break;
1041 }
1042 }
1043 brelse(bp);
1044 }
1045 }
1046}
1047
1048/*
1049 * Find any Win'95 long filename entry in directory dep
1050 */
1051int
1052findwin95(dep)
1053 struct denode *dep;
1054{
1055 struct msdosfsmount *pmp = dep->de_pmp;
1056 struct direntry *dentp;
1057 int blsize, win95;
1058 u_long cn;
1059 daddr_t bn;
1060 struct buf *bp;
1061
1062 win95 = 1;
1063 /*
1064 * Read through the directory looking for Win'95 entries
1065 * Note: Error currently handled just as EOF XXX
1066 */
1067 for (cn = 0;; cn++) {
1068 if (pcbmap(dep, cn, &bn, 0, &blsize))
1069 return (win95);
1070 if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) {
1071 brelse(bp);
1072 return (win95);
1073 }
1074 for (dentp = (struct direntry *)bp->b_data;
1075 (char *)dentp < bp->b_data + blsize;
1076 dentp++) {
1077 if (dentp->deName[0] == SLOT_EMPTY) {
1078 /*
1079 * Last used entry and not found
1080 */
1081 brelse(bp);
1082 return (win95);
1083 }
1084 if (dentp->deName[0] == SLOT_DELETED) {
1085 /*
1086 * Ignore deleted files
1087 * Note: might be an indication of Win'95 anyway XXX
1088 */
1089 continue;
1090 }
1091 if (dentp->deAttributes == ATTR_WIN95) {
1092 brelse(bp);
1093 return 1;
1094 }
1095 win95 = 0;
1096 }
1097 brelse(bp);
1098 }
1099}