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