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