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