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