1 /* $OpenBSD: msdosfs_lookup.c,v 1.15 2005/03/02 00:35:04 tom Exp $ */
2 /* $NetBSD: msdosfs_lookup.c,v 1.34 1997/10/18 22:12:27 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 #include <sys/dirent.h>
58
59 #include <msdosfs/bpb.h>
60 #include <msdosfs/direntry.h>
61 #include <msdosfs/denode.h>
62 #include <msdosfs/msdosfsmount.h>
63 #include <msdosfs/fat.h>
64
65 /*
66 * When we search a directory the blocks containing directory entries are
67 * read and examined. The directory entries contain information that would
68 * normally be in the inode of a unix filesystem. This means that some of
69 * a directory's contents may also be in memory resident denodes (sort of
70 * an inode). This can cause problems if we are searching while some other
71 * process is modifying a directory. To prevent one process from accessing
72 * incompletely modified directory information we depend upon being the
73 * sole owner of a directory block. bread/brelse provide this service.
74 * This being the case, when a process modifies a directory it must first
75 * acquire the disk block that contains the directory entry to be modified.
76 * Then update the disk block and the denode, and then write the disk block
77 * out to disk. This way disk blocks containing directory entries and in
78 * memory denode's will be in synch.
79 */
80 int
msdosfs_lookup(v)81 msdosfs_lookup(v)
82 void *v;
83 {
84 struct vop_lookup_args /* {
85 struct vnode *a_dvp;
86 struct vnode **a_vpp;
87 struct componentname *a_cnp;
88 } */ *ap = v;
89 struct vnode *vdp = ap->a_dvp;
90 struct vnode **vpp = ap->a_vpp;
91 struct componentname *cnp = ap->a_cnp;
92 struct proc *p = cnp->cn_proc;
93 daddr_t bn;
94 int error;
95 int lockparent;
96 int wantparent;
97 int slotcount;
98 int slotoffset = 0;
99 int frcn;
100 uint32_t cluster;
101 int blkoff;
102 int diroff;
103 int blsize;
104 int isadir; /* ~0 if found direntry is a directory */
105 uint32_t scn; /* starting cluster number */
106 struct vnode *pdp;
107 struct denode *dp;
108 struct denode *tdp;
109 struct msdosfsmount *pmp;
110 struct buf *bp = 0;
111 struct direntry *dep;
112 u_char dosfilename[12];
113 u_char *adjp;
114 int adjlen;
115 int flags;
116 int nameiop = cnp->cn_nameiop;
117 int wincnt = 1;
118 int chksum = -1;
119 int olddos = 1;
120
121 cnp->cn_flags &= ~PDIRUNLOCK; /* XXX why this ?? */
122 flags = cnp->cn_flags;
123
124 #ifdef MSDOSFS_DEBUG
125 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
126 #endif
127 dp = VTODE(vdp);
128 pmp = dp->de_pmp;
129 *vpp = NULL;
130 lockparent = flags & LOCKPARENT;
131 wantparent = flags & (LOCKPARENT | WANTPARENT);
132 #ifdef MSDOSFS_DEBUG
133 printf("msdosfs_lookup(): vdp %08x, dp %08x, Attr %02x\n",
134 vdp, dp, dp->de_Attributes);
135 #endif
136
137 /*
138 * Check accessiblity of directory.
139 */
140 if ((dp->de_Attributes & ATTR_DIRECTORY) == 0)
141 return (ENOTDIR);
142 if ((error = VOP_ACCESS(vdp, VEXEC, cnp->cn_cred, cnp->cn_proc)) != 0)
143 return (error);
144
145 /*
146 * We now have a segment name to search for, and a directory to search.
147 *
148 * Before tediously performing a linear scan of the directory,
149 * check the name cache to see if the directory/name pair
150 * we are looking for is known already.
151 */
152 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
153 return (error);
154
155 /*
156 * If they are going after the . or .. entry in the root directory,
157 * they won't find it. DOS filesystems don't have them in the root
158 * directory. So, we fake it. deget() is in on this scam too.
159 */
160 if ((vdp->v_flag & VROOT) && cnp->cn_nameptr[0] == '.' &&
161 (cnp->cn_namelen == 1 ||
162 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
163 isadir = ATTR_DIRECTORY;
164 scn = MSDOSFSROOT;
165 #ifdef MSDOSFS_DEBUG
166 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
167 #endif
168 cluster = MSDOSFSROOT;
169 blkoff = MSDOSFSROOT_OFS;
170 goto foundroot;
171 }
172
173 switch (unix2dosfn((u_char *)cnp->cn_nameptr, dosfilename, cnp->cn_namelen, 0)) {
174 case 0:
175 return (EINVAL);
176 case 1:
177 break;
178 case 2:
179 wincnt = winSlotCnt((u_char *)cnp->cn_nameptr, cnp->cn_namelen) + 1;
180 break;
181 case 3:
182 olddos = 0;
183 wincnt = winSlotCnt((u_char *)cnp->cn_nameptr, cnp->cn_namelen) + 1;
184 break;
185 }
186 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
187 wincnt = 1;
188
189 /*
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.
194 */
195 slotcount = wincnt;
196 if ((nameiop == CREATE || nameiop == RENAME) &&
197 (flags & ISLASTCN))
198 slotcount = 0;
199
200 #ifdef MSDOSFS_DEBUG
201 printf("msdosfs_lookup(): dos version of filename %s, length %d\n",
202 dosfilename, cnp->cn_namelen);
203 #endif
204 /*
205 * We want to search the directory pointed to by vdp for the name
206 * pointed to by cnp->cn_nameptr.
207 *
208 * XXX UNIX allows filenames with trailing dots and blanks; we don't.
209 * Most of the routines in msdosfs_conv.c adjust for this, but
210 * winChkName() does not, so we do it here. Otherwise, a file
211 * such as ".foobar." cannot be retrieved properly.
212 *
213 * (Note that this is also faster: perform the adjustment once,
214 * rather than on each call to winChkName. However, it is still
215 * a nasty hack.)
216 */
217 adjp = cnp->cn_nameptr;
218 adjlen = cnp->cn_namelen;
219
220 for (adjp += adjlen; adjlen > 0; adjlen--)
221 if (*--adjp != ' ' && *adjp != '.')
222 break;
223
224 tdp = NULL;
225 /*
226 * The outer loop ranges over the clusters that make up the
227 * directory. Note that the root directory is different from all
228 * other directories. It has a fixed number of blocks that are not
229 * part of the pool of allocatable clusters. So, we treat it a
230 * little differently. The root directory starts at "cluster" 0.
231 */
232 diroff = 0;
233 for (frcn = 0;; frcn++) {
234 if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
235 if (error == E2BIG)
236 break;
237 return (error);
238 }
239 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
240 if (error) {
241 brelse(bp);
242 return (error);
243 }
244 for (blkoff = 0; blkoff < blsize;
245 blkoff += sizeof(struct direntry),
246 diroff += sizeof(struct direntry)) {
247 dep = (struct direntry *)(bp->b_data + blkoff);
248 /*
249 * If the slot is empty and we are still looking
250 * for an empty then remember this one. If the
251 * slot is not empty then check to see if it
252 * matches what we are looking for. If the slot
253 * has never been filled with anything, then the
254 * remainder of the directory has never been used,
255 * so there is no point in searching it.
256 */
257 if (dep->deName[0] == SLOT_EMPTY ||
258 dep->deName[0] == SLOT_DELETED) {
259 /*
260 * Drop memory of previous long matches
261 */
262 chksum = -1;
263
264 if (slotcount < wincnt) {
265 slotcount++;
266 slotoffset = diroff;
267 }
268 if (dep->deName[0] == SLOT_EMPTY) {
269 brelse(bp);
270 goto notfound;
271 }
272 } else {
273 /*
274 * If there wasn't enough space for our winentries,
275 * forget about the empty space
276 */
277 if (slotcount < wincnt)
278 slotcount = 0;
279
280 /*
281 * Check for Win95 long filename entry
282 */
283 if (dep->deAttributes == ATTR_WIN95) {
284 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
285 continue;
286
287 chksum = winChkName((u_char *)cnp->cn_nameptr,
288 adjlen,
289 (struct winentry *)dep,
290 chksum);
291 continue;
292 }
293
294 /*
295 * Ignore volume labels (anywhere, not just
296 * the root directory).
297 */
298 if (dep->deAttributes & ATTR_VOLUME) {
299 chksum = -1;
300 continue;
301 }
302
303 /*
304 * Check for a checksum or name match
305 */
306 if (chksum != winChksum(dep->deName)
307 && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
308 chksum = -1;
309 continue;
310 }
311 #ifdef MSDOSFS_DEBUG
312 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
313 blkoff, diroff);
314 #endif
315 /*
316 * Remember where this directory
317 * entry came from for whoever did
318 * this lookup.
319 */
320 dp->de_fndoffset = diroff;
321 dp->de_fndcnt = 0; /* unused anyway */
322
323 goto found;
324 }
325 } /* for (blkoff = 0; .... */
326 /*
327 * Release the buffer holding the directory cluster just
328 * searched.
329 */
330 brelse(bp);
331 } /* for (frcn = 0; ; frcn++) */
332
333 notfound:;
334 /*
335 * We hold no disk buffers at this point.
336 */
337
338 /*
339 * Fixup the slot description to point to the place where
340 * we might put the new DOS direntry (putting the Win95
341 * long name entries before that)
342 */
343 if (!slotcount) {
344 slotcount = 1;
345 slotoffset = diroff;
346 }
347 if (wincnt > slotcount)
348 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
349
350 /*
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.
354 */
355 #ifdef MSDOSFS_DEBUG
356 printf("msdosfs_lookup(): op %d, refcnt %d\n",
357 nameiop, dp->de_refcnt);
358 printf(" slotcount %d, slotoffset %d\n",
359 slotcount, slotoffset);
360 #endif
361 if ((nameiop == CREATE || nameiop == RENAME) &&
362 (flags & ISLASTCN) && dp->de_refcnt != 0) {
363 /*
364 * Access for write is interpreted as allowing
365 * creation of files in the directory.
366 */
367 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc);
368 if (error)
369 return (error);
370 /*
371 * Return an indication of where the new directory
372 * entry should be put.
373 */
374 dp->de_fndoffset = slotoffset;
375 dp->de_fndcnt = wincnt - 1;
376
377 /*
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.
386 *
387 * NB - if the directory is unlocked, then this
388 * information cannot be used.
389 */
390 cnp->cn_flags |= SAVENAME;
391 if (!lockparent) {
392 VOP_UNLOCK(vdp, 0, p);
393 cnp->cn_flags |= PDIRUNLOCK;
394 }
395 return (EJUSTRETURN);
396 }
397 /*
398 * Insert name into cache (as non-existent) if appropriate.
399 */
400 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
401 cache_enter(vdp, *vpp, cnp);
402 return (ENOENT);
403
404 found:;
405 /*
406 * NOTE: We still have the buffer with matched directory entry at
407 * this point.
408 */
409 isadir = dep->deAttributes & ATTR_DIRECTORY;
410 scn = getushort(dep->deStartCluster);
411 if (FAT32(pmp)) {
412 scn |= getushort(dep->deHighClust) << 16;
413 if (scn == pmp->pm_rootdirblk) {
414 /*
415 * There should actually be 0 here.
416 * Just ignore the error.
417 */
418 scn = MSDOSFSROOT;
419 }
420 }
421
422 if (cluster == MSDOSFSROOT)
423 blkoff = diroff;
424
425 if (isadir) {
426 cluster = scn;
427 if (cluster == MSDOSFSROOT)
428 blkoff = MSDOSFSROOT_OFS;
429 else
430 blkoff = 0;
431 }
432
433 /*
434 * Now release buf to allow deget to read the entry again.
435 * Reserving it here and giving it to deget could result
436 * in a deadlock.
437 */
438 brelse(bp);
439
440 foundroot:;
441 /*
442 * If we entered at foundroot, then we are looking for the . or ..
443 * entry of the filesystems root directory. isadir and scn were
444 * setup before jumping here. And, bp is already null.
445 */
446 if (FAT32(pmp) && scn == MSDOSFSROOT)
447 scn = pmp->pm_rootdirblk;
448
449 /*
450 * If deleting, and at end of pathname, return
451 * parameters which can be used to remove file.
452 * If the wantparent flag isn't set, we return only
453 * the directory (in ndp->ni_dvp), otherwise we go
454 * on and lock the inode, being careful with ".".
455 */
456 if (nameiop == DELETE && (flags & ISLASTCN)) {
457 /*
458 * Don't allow deleting the root.
459 */
460 if (blkoff == MSDOSFSROOT_OFS)
461 return EROFS; /* really? XXX */
462
463 /*
464 * Write access to directory required to delete files.
465 */
466 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc);
467 if (error)
468 return (error);
469
470 /*
471 * Return pointer to current entry in dp->i_offset.
472 * Save directory inode pointer in ndp->ni_dvp for dirremove().
473 */
474 if (dp->de_StartCluster == scn && isadir) { /* "." */
475 VREF(vdp);
476 *vpp = vdp;
477 return (0);
478 }
479 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
480 return (error);
481 *vpp = DETOV(tdp);
482 if (!lockparent) {
483 VOP_UNLOCK(vdp, 0, p);
484 cnp->cn_flags |= PDIRUNLOCK;
485 }
486 return (0);
487 }
488
489 /*
490 * If rewriting (RENAME), return the inode and the
491 * information required to rewrite the present directory
492 * Must get inode of directory entry to verify it's a
493 * regular file, or empty directory.
494 */
495 if (nameiop == RENAME && wantparent &&
496 (flags & ISLASTCN)) {
497 if (blkoff == MSDOSFSROOT_OFS)
498 return EROFS; /* really? XXX */
499
500 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc);
501 if (error)
502 return (error);
503
504 /*
505 * Careful about locking second inode.
506 * This can only occur if the target is ".".
507 */
508 if (dp->de_StartCluster == scn && isadir)
509 return (EISDIR);
510
511 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
512 return (error);
513 *vpp = DETOV(tdp);
514 cnp->cn_flags |= SAVENAME;
515 if (!lockparent)
516 VOP_UNLOCK(vdp, 0, p);
517 return (0);
518 }
519
520 /*
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.
538 */
539 pdp = vdp;
540 if (flags & ISDOTDOT) {
541 VOP_UNLOCK(pdp, 0, p); /* race to get the inode */
542 cnp->cn_flags |= PDIRUNLOCK;
543 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) {
544 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p) == 0)
545 cnp->cn_flags &= ~PDIRUNLOCK;
546 return (error);
547 }
548 if (lockparent && (flags & ISLASTCN)) {
549 if ((error = vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY,
550 p))) {
551 vput(DETOV(tdp));
552 return (error);
553 }
554 cnp->cn_flags &= ~PDIRUNLOCK;
555 }
556 *vpp = DETOV(tdp);
557 } else if (dp->de_StartCluster == scn && isadir) {
558 VREF(vdp); /* we want ourself, ie "." */
559 *vpp = vdp;
560 } else {
561 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0)
562 return (error);
563 if (!lockparent || !(flags & ISLASTCN)) {
564 VOP_UNLOCK(pdp, 0, p);
565 cnp->cn_flags |= PDIRUNLOCK;
566 }
567 *vpp = DETOV(tdp);
568 }
569
570 /*
571 * Insert name into cache if appropriate.
572 */
573 if (cnp->cn_flags & MAKEENTRY)
574 cache_enter(vdp, *vpp, cnp);
575 return (0);
576 }
577
578 /*
579 * dep - directory entry to copy into the directory
580 * ddep - directory to add to
581 * depp - return the address of the denode for the created directory entry
582 * if depp != 0
583 * cnp - componentname needed for Win95 long filenames
584 */
585 int
createde(dep,ddep,depp,cnp)586 createde(dep, ddep, depp, cnp)
587 struct denode *dep;
588 struct denode *ddep;
589 struct denode **depp;
590 struct componentname *cnp;
591 {
592 int error;
593 uint32_t dirclust, diroffset;
594 struct direntry *ndep;
595 struct msdosfsmount *pmp = ddep->de_pmp;
596 struct buf *bp;
597 daddr_t bn;
598 int blsize;
599
600 #ifdef MSDOSFS_DEBUG
601 printf("createde(dep %08x, ddep %08x, depp %08x, cnp %08x)\n",
602 dep, ddep, depp, cnp);
603 #endif
604
605 /*
606 * If no space left in the directory then allocate another cluster
607 * and chain it onto the end of the file. There is one exception
608 * to this. That is, if the root directory has no more space it
609 * can NOT be expanded. extendfile() checks for and fails attempts
610 * to extend the root directory. We just return an error in that
611 * case.
612 */
613 if (ddep->de_fndoffset >= ddep->de_FileSize) {
614 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
615 - ddep->de_FileSize;
616 dirclust = de_clcount(pmp, diroffset);
617 if ((error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR)) != 0) {
618 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL);
619 return error;
620 }
621
622 /*
623 * Update the size of the directory
624 */
625 ddep->de_FileSize += de_cn2off(pmp, dirclust);
626 }
627
628 /*
629 * We just read in the cluster with space. Copy the new directory
630 * entry in. Then write it to disk. NOTE: DOS directories
631 * do not get smaller as clusters are emptied.
632 */
633 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
634 &bn, &dirclust, &blsize);
635 if (error)
636 return error;
637 diroffset = ddep->de_fndoffset;
638 if (dirclust != MSDOSFSROOT)
639 diroffset &= pmp->pm_crbomask;
640 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
641 brelse(bp);
642 return error;
643 }
644 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
645
646 DE_EXTERNALIZE(ndep, dep);
647
648 /*
649 * Now write the Win95 long name
650 */
651 if (ddep->de_fndcnt > 0) {
652 u_int8_t chksum = winChksum(ndep->deName);
653 u_char *un = (u_char *)cnp->cn_nameptr;
654 int unlen = cnp->cn_namelen;
655 int cnt = 1;
656
657 while (--ddep->de_fndcnt >= 0) {
658 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
659 if ((error = bwrite(bp)) != 0)
660 return error;
661
662 ddep->de_fndoffset -= sizeof(struct direntry);
663 error = pcbmap(ddep,
664 de_cluster(pmp,
665 ddep->de_fndoffset),
666 &bn, 0, &blsize);
667 if (error)
668 return error;
669
670 error = bread(pmp->pm_devvp, bn, blsize,
671 NOCRED, &bp);
672 if (error) {
673 brelse(bp);
674 return error;
675 }
676 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
677 } else {
678 ndep--;
679 ddep->de_fndoffset -= sizeof(struct direntry);
680 }
681 if (!unix2winfn(un, unlen, (struct winentry *)ndep, cnt++, chksum))
682 break;
683 }
684 }
685
686 if ((error = bwrite(bp)) != 0)
687 return error;
688
689 /*
690 * If they want us to return with the denode gotten.
691 */
692 if (depp) {
693 if (dep->de_Attributes & ATTR_DIRECTORY) {
694 dirclust = dep->de_StartCluster;
695 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
696 dirclust = MSDOSFSROOT;
697 if (dirclust == MSDOSFSROOT)
698 diroffset = MSDOSFSROOT_OFS;
699 else
700 diroffset = 0;
701 }
702 return deget(pmp, dirclust, diroffset, depp);
703 }
704
705 return 0;
706 }
707
708 /*
709 * Be sure a directory is empty except for "." and "..". Return 1 if empty,
710 * return 0 if not empty or error.
711 */
712 int
dosdirempty(dep)713 dosdirempty(dep)
714 struct denode *dep;
715 {
716 int blsize;
717 int error;
718 uint32_t cn;
719 daddr_t bn;
720 struct buf *bp;
721 struct msdosfsmount *pmp = dep->de_pmp;
722 struct direntry *dentp;
723
724 /*
725 * Since the filesize field in directory entries for a directory is
726 * zero, we just have to feel our way through the directory until
727 * we hit end of file.
728 */
729 for (cn = 0;; cn++) {
730 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
731 if (error == E2BIG)
732 return (1); /* it's empty */
733 return (0);
734 }
735 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
736 if (error) {
737 brelse(bp);
738 return (0);
739 }
740 for (dentp = (struct direntry *)bp->b_data;
741 (char *)dentp < bp->b_data + blsize;
742 dentp++) {
743 if (dentp->deName[0] != SLOT_DELETED &&
744 (dentp->deAttributes & ATTR_VOLUME) == 0) {
745 /*
746 * In dos directories an entry whose name
747 * starts with SLOT_EMPTY (0) starts the
748 * beginning of the unused part of the
749 * directory, so we can just return that it
750 * is empty.
751 */
752 if (dentp->deName[0] == SLOT_EMPTY) {
753 brelse(bp);
754 return (1);
755 }
756 /*
757 * Any names other than "." and ".." in a
758 * directory mean it is not empty.
759 */
760 if (bcmp(dentp->deName, ". ", 11) &&
761 bcmp(dentp->deName, ".. ", 11)) {
762 brelse(bp);
763 #ifdef MSDOSFS_DEBUG
764 printf("dosdirempty(): entry found %02x, %02x\n",
765 dentp->deName[0], dentp->deName[1]);
766 #endif
767 return (0); /* not empty */
768 }
769 }
770 }
771 brelse(bp);
772 }
773 /* NOTREACHED */
774 }
775
776 /*
777 * Check to see if the directory described by target is in some
778 * subdirectory of source. This prevents something like the following from
779 * succeeding and leaving a bunch or files and directories orphaned. mv
780 * /a/b/c /a/b/c/d/e/f Where c and f are directories.
781 *
782 * source - the inode for /a/b/c
783 * target - the inode for /a/b/c/d/e/f
784 *
785 * Returns 0 if target is NOT a subdirectory of source.
786 * Otherwise returns a non-zero error number.
787 * The target inode is always unlocked on return.
788 */
789 int
doscheckpath(source,target)790 doscheckpath(source, target)
791 struct denode *source;
792 struct denode *target;
793 {
794 daddr_t scn;
795 struct msdosfsmount *pmp;
796 struct direntry *ep;
797 struct denode *dep;
798 struct buf *bp = NULL;
799 int error = 0;
800
801 dep = target;
802 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
803 (source->de_Attributes & ATTR_DIRECTORY) == 0) {
804 error = ENOTDIR;
805 goto out;
806 }
807 if (dep->de_StartCluster == source->de_StartCluster) {
808 error = EEXIST;
809 goto out;
810 }
811 if (dep->de_StartCluster == MSDOSFSROOT)
812 goto out;
813 pmp = dep->de_pmp;
814 #ifdef DIAGNOSTIC
815 if (pmp != source->de_pmp)
816 panic("doscheckpath: source and target on different filesystems");
817 #endif
818 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
819 goto out;
820
821 for (;;) {
822 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
823 error = ENOTDIR;
824 break;
825 }
826 scn = dep->de_StartCluster;
827 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
828 pmp->pm_bpcluster, NOCRED, &bp);
829 if (error)
830 break;
831
832 ep = (struct direntry *) bp->b_data + 1;
833 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
834 bcmp(ep->deName, ".. ", 11) != 0) {
835 error = ENOTDIR;
836 break;
837 }
838 scn = getushort(ep->deStartCluster);
839 if (FAT32(pmp))
840 scn |= getushort(ep->deHighClust) << 16;
841
842 if (scn == source->de_StartCluster) {
843 error = EINVAL;
844 break;
845 }
846 if (scn == MSDOSFSROOT)
847 break;
848 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
849 /*
850 * scn should be 0 in this case,
851 * but we silently ignore the error.
852 */
853 break;
854 }
855
856 vput(DETOV(dep));
857 brelse(bp);
858 bp = NULL;
859 /* NOTE: deget() clears dep on error */
860 if ((error = deget(pmp, scn, 0, &dep)) != 0)
861 break;
862 }
863 out:;
864 if (bp)
865 brelse(bp);
866 if (error == ENOTDIR)
867 printf("doscheckpath(): .. not a directory?\n");
868 if (dep != NULL)
869 vput(DETOV(dep));
870 return (error);
871 }
872
873 /*
874 * Read in the disk block containing the directory entry (dirclu, dirofs)
875 * and return the address of the buf header, and the address of the
876 * directory entry within the block.
877 */
878 int
readep(pmp,dirclust,diroffset,bpp,epp)879 readep(pmp, dirclust, diroffset, bpp, epp)
880 struct msdosfsmount *pmp;
881 uint32_t dirclust, diroffset;
882 struct buf **bpp;
883 struct direntry **epp;
884 {
885 int error;
886 daddr_t bn;
887 int blsize;
888 uint32_t boff;
889
890 boff = diroffset & ~pmp->pm_crbomask;
891 blsize = pmp->pm_bpcluster;
892 if (dirclust == MSDOSFSROOT
893 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
894 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
895 bn = detobn(pmp, dirclust, diroffset);
896 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
897 brelse(*bpp);
898 *bpp = NULL;
899 return (error);
900 }
901 if (epp)
902 *epp = bptoep(pmp, *bpp, diroffset);
903 return (0);
904 }
905
906 /*
907 * Read in the disk block containing the directory entry dep came from and
908 * return the address of the buf header, and the address of the directory
909 * entry within the block.
910 */
911 int
readde(dep,bpp,epp)912 readde(dep, bpp, epp)
913 struct denode *dep;
914 struct buf **bpp;
915 struct direntry **epp;
916 {
917
918 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
919 bpp, epp));
920 }
921
922 /*
923 * Remove a directory entry. At this point the file represented by the
924 * directory entry to be removed is still full length until noone has it
925 * open. When the file no longer being used msdosfs_inactive() is called
926 * and will truncate the file to 0 length. When the vnode containing the
927 * denode is needed for some other purpose by VFS it will call
928 * msdosfs_reclaim() which will remove the denode from the denode cache.
929 */
930 int
removede(pdep,dep)931 removede(pdep, dep)
932 struct denode *pdep; /* directory where the entry is removed */
933 struct denode *dep; /* file to be removed */
934 {
935 int error;
936 struct direntry *ep;
937 struct buf *bp;
938 daddr_t bn;
939 int blsize;
940 struct msdosfsmount *pmp = pdep->de_pmp;
941 uint32_t offset = pdep->de_fndoffset;
942
943 #ifdef MSDOSFS_DEBUG
944 printf("removede(): filename %s, dep %08x, offset %08x\n",
945 dep->de_Name, dep, offset);
946 #endif
947
948 dep->de_refcnt--;
949 offset += sizeof(struct direntry);
950 do {
951 offset -= sizeof(struct direntry);
952 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
953 if (error)
954 return error;
955 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
956 if (error) {
957 brelse(bp);
958 return error;
959 }
960 ep = bptoep(pmp, bp, offset);
961 /*
962 * Check whether, if we came here the second time, i.e.
963 * when underflowing into the previous block, the last
964 * entry in this block is a longfilename entry, too.
965 */
966 if (ep->deAttributes != ATTR_WIN95
967 && offset != pdep->de_fndoffset) {
968 brelse(bp);
969 break;
970 }
971 offset += sizeof(struct direntry);
972 while (1) {
973 /*
974 * We are a bit agressive here in that we delete any Win95
975 * entries preceding this entry, not just the ones we "own".
976 * Since these presumably aren't valid anyway,
977 * there should be no harm.
978 */
979 offset -= sizeof(struct direntry);
980 ep--->deName[0] = SLOT_DELETED;
981 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
982 || !(offset & pmp->pm_crbomask)
983 || ep->deAttributes != ATTR_WIN95)
984 break;
985 }
986 if ((error = bwrite(bp)) != 0)
987 return error;
988 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
989 && !(offset & pmp->pm_crbomask)
990 && offset);
991 return 0;
992 }
993
994 /*
995 * Create a unique DOS name in dvp
996 */
997 int
uniqdosname(dep,cnp,cp)998 uniqdosname(dep, cnp, cp)
999 struct denode *dep;
1000 struct componentname *cnp;
1001 u_char *cp;
1002 {
1003 struct msdosfsmount *pmp = dep->de_pmp;
1004 struct direntry *dentp;
1005 int gen;
1006 int blsize;
1007 uint32_t cn;
1008 daddr_t bn;
1009 struct buf *bp;
1010 int error;
1011
1012 for (gen = 1;; gen++) {
1013 /*
1014 * Generate DOS name with generation number
1015 */
1016 if (!unix2dosfn((u_char *)cnp->cn_nameptr, cp, cnp->cn_namelen, gen))
1017 return gen == 1 ? EINVAL : EEXIST;
1018
1019 /*
1020 * Now look for a dir entry with this exact name
1021 */
1022 for (cn = error = 0; !error; cn++) {
1023 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1024 if (error == E2BIG) /* EOF reached and not found */
1025 return 0;
1026 return error;
1027 }
1028 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1029 if (error) {
1030 brelse(bp);
1031 return error;
1032 }
1033 for (dentp = (struct direntry *)bp->b_data;
1034 (char *)dentp < bp->b_data + blsize;
1035 dentp++) {
1036 if (dentp->deName[0] == SLOT_EMPTY) {
1037 /*
1038 * Last used entry and not found
1039 */
1040 brelse(bp);
1041 return 0;
1042 }
1043 /*
1044 * Ignore volume labels and Win95 entries
1045 */
1046 if (dentp->deAttributes & ATTR_VOLUME)
1047 continue;
1048 if (!bcmp(dentp->deName, cp, 11)) {
1049 error = EEXIST;
1050 break;
1051 }
1052 }
1053 brelse(bp);
1054 }
1055 }
1056
1057 return (EEXIST);
1058 }
1059
1060 /*
1061 * Find any Win'95 long filename entry in directory dep
1062 */
1063 int
findwin95(dep)1064 findwin95(dep)
1065 struct denode *dep;
1066 {
1067 struct msdosfsmount *pmp = dep->de_pmp;
1068 struct direntry *dentp;
1069 int blsize;
1070 uint32_t cn;
1071 daddr_t bn;
1072 struct buf *bp;
1073
1074 /*
1075 * Read through the directory looking for Win'95 entries
1076 * Note: Error currently handled just as EOF XXX
1077 */
1078 for (cn = 0;; cn++) {
1079 if (pcbmap(dep, cn, &bn, 0, &blsize))
1080 return 0;
1081 if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) {
1082 brelse(bp);
1083 return 0;
1084 }
1085 for (dentp = (struct direntry *)bp->b_data;
1086 (char *)dentp < bp->b_data + blsize;
1087 dentp++) {
1088 if (dentp->deName[0] == SLOT_EMPTY) {
1089 /*
1090 * Last used entry and not found
1091 */
1092 brelse(bp);
1093 return 0;
1094 }
1095 if (dentp->deName[0] == SLOT_DELETED) {
1096 /*
1097 * Ignore deleted files
1098 * Note: might be an indication of Win'95 anyway XXX
1099 */
1100 continue;
1101 }
1102 if (dentp->deAttributes == ATTR_WIN95) {
1103 brelse(bp);
1104 return 1;
1105 }
1106 }
1107 brelse(bp);
1108 }
1109 }
1110