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