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