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