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