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 return (error); 238 } 239 for (blkoff = 0; blkoff < blsize; 240 blkoff += sizeof(struct direntry), 241 diroff += sizeof(struct direntry)) { 242 dep = (struct direntry *)(bp->b_data + blkoff); 243 /* 244 * If the slot is empty and we are still looking 245 * for an empty then remember this one. If the 246 * slot is not empty then check to see if it 247 * matches what we are looking for. If the slot 248 * has never been filled with anything, then the 249 * remainder of the directory has never been used, 250 * so there is no point in searching it. 251 */ 252 if (dep->deName[0] == SLOT_EMPTY || 253 dep->deName[0] == SLOT_DELETED) { 254 /* 255 * Drop memory of previous long matches 256 */ 257 chksum = -1; 258 mbnambuf_init(&nb); 259 260 if (slotcount < wincnt) { 261 slotcount++; 262 slotoffset = diroff; 263 } 264 if (dep->deName[0] == SLOT_EMPTY) { 265 brelse(bp); 266 goto notfound; 267 } 268 } else { 269 /* 270 * If there wasn't enough space for our winentries, 271 * forget about the empty space 272 */ 273 if (slotcount < wincnt) 274 slotcount = 0; 275 276 /* 277 * Check for Win95 long filename entry 278 */ 279 if (dep->deAttributes == ATTR_WIN95) { 280 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 281 continue; 282 283 chksum = win2unixfn(&nb, 284 (struct winentry *)dep, chksum, 285 pmp); 286 continue; 287 } 288 289 chksum = winChkName(&nb, 290 (const u_char *)cnp->cn_nameptr, unlen, 291 chksum, pmp); 292 if (chksum == -2) { 293 chksum = -1; 294 continue; 295 } 296 297 /* 298 * Ignore volume labels (anywhere, not just 299 * the root directory). 300 */ 301 if (dep->deAttributes & ATTR_VOLUME) { 302 chksum = -1; 303 continue; 304 } 305 306 /* 307 * Check for a checksum or name match 308 */ 309 chksum_ok = (chksum == winChksum(dep->deName)); 310 if (!chksum_ok 311 && (!olddos || bcmp(dosfilename, dep->deName, 11))) { 312 chksum = -1; 313 continue; 314 } 315 #ifdef MSDOSFS_DEBUG 316 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n", 317 blkoff, diroff); 318 #endif 319 /* 320 * Remember where this directory 321 * entry came from for whoever did 322 * this lookup. 323 */ 324 dp->de_fndoffset = diroff; 325 if (chksum_ok && nameiop == RENAME) { 326 /* 327 * Target had correct long name 328 * directory entries, reuse them 329 * as needed. 330 */ 331 dp->de_fndcnt = wincnt - 1; 332 } else { 333 /* 334 * Long name directory entries 335 * not present or corrupt, can only 336 * reuse dos directory entry. 337 */ 338 dp->de_fndcnt = 0; 339 } 340 341 goto found; 342 } 343 } /* for (blkoff = 0; .... */ 344 /* 345 * Release the buffer holding the directory cluster just 346 * searched. 347 */ 348 brelse(bp); 349 } /* for (frcn = 0; ; frcn++) */ 350 351 notfound: 352 /* 353 * We hold no disk buffers at this point. 354 */ 355 356 /* 357 * Fixup the slot description to point to the place where 358 * we might put the new DOS direntry (putting the Win95 359 * long name entries before that) 360 */ 361 if (!slotcount) { 362 slotcount = 1; 363 slotoffset = diroff; 364 } 365 if (wincnt > slotcount) 366 slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 367 368 /* 369 * If we get here we didn't find the entry we were looking for. But 370 * that's ok if we are creating or renaming and are at the end of 371 * the pathname and the directory hasn't been removed. 372 */ 373 #ifdef MSDOSFS_DEBUG 374 printf("msdosfs_lookup(): op %d, refcnt %ld\n", 375 nameiop, dp->de_refcnt); 376 printf(" slotcount %d, slotoffset %d\n", 377 slotcount, slotoffset); 378 #endif 379 if ((nameiop == CREATE || nameiop == RENAME) && 380 (flags & ISLASTCN) && dp->de_refcnt != 0) { 381 /* 382 * Access for write is interpreted as allowing 383 * creation of files in the directory. 384 */ 385 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 386 if (error) 387 return (error); 388 /* 389 * Return an indication of where the new directory 390 * entry should be put. 391 */ 392 dp->de_fndoffset = slotoffset; 393 dp->de_fndcnt = wincnt - 1; 394 395 /* 396 * We return with the directory locked, so that 397 * the parameters we set up above will still be 398 * valid if we actually decide to do a direnter(). 399 * We return ni_vp == NULL to indicate that the entry 400 * does not currently exist; we leave a pointer to 401 * the (locked) directory inode in ndp->ni_dvp. 402 * The pathname buffer is saved so that the name 403 * can be obtained later. 404 * 405 * NB - if the directory is unlocked, then this 406 * information cannot be used. 407 */ 408 cnp->cn_flags |= SAVENAME; 409 return (EJUSTRETURN); 410 } 411 #if 0 412 /* 413 * Insert name into cache (as non-existent) if appropriate. 414 * 415 * XXX Negative caching is broken for msdosfs because the name 416 * cache doesn't understand peculiarities such as case insensitivity 417 * and 8.3 filenames. Hence, it may not invalidate all negative 418 * entries if a file with this name is later created. 419 */ 420 if ((cnp->cn_flags & MAKEENTRY) != 0) 421 cache_enter(vdp, *vpp, cnp); 422 #endif 423 return (ENOENT); 424 425 found: 426 /* 427 * NOTE: We still have the buffer with matched directory entry at 428 * this point. 429 */ 430 isadir = dep->deAttributes & ATTR_DIRECTORY; 431 scn = getushort(dep->deStartCluster); 432 if (FAT32(pmp)) { 433 scn |= getushort(dep->deHighClust) << 16; 434 if (scn == pmp->pm_rootdirblk) { 435 /* 436 * There should actually be 0 here. 437 * Just ignore the error. 438 */ 439 scn = MSDOSFSROOT; 440 } 441 } 442 443 if (isadir) { 444 cluster = scn; 445 if (cluster == MSDOSFSROOT) 446 blkoff = MSDOSFSROOT_OFS; 447 else 448 blkoff = 0; 449 } else if (cluster == MSDOSFSROOT) 450 blkoff = diroff; 451 452 /* 453 * Now release buf to allow deget to read the entry again. 454 * Reserving it here and giving it to deget could result 455 * in a deadlock. 456 */ 457 brelse(bp); 458 bp = NULL; 459 460 foundroot: 461 /* 462 * If we entered at foundroot, then we are looking for the . or .. 463 * entry of the filesystems root directory. isadir and scn were 464 * setup before jumping here. And, bp is already null. 465 */ 466 if (FAT32(pmp) && scn == MSDOSFSROOT) 467 scn = pmp->pm_rootdirblk; 468 469 if (dd_inum != NULL) { 470 *dd_inum = (uint64_t)pmp->pm_bpcluster * scn + blkoff; 471 return (0); 472 } 473 474 /* 475 * If deleting, and at end of pathname, return 476 * parameters which can be used to remove file. 477 */ 478 if (nameiop == DELETE && (flags & ISLASTCN)) { 479 /* 480 * Don't allow deleting the root. 481 */ 482 if (blkoff == MSDOSFSROOT_OFS) 483 return (EBUSY); 484 485 /* 486 * Write access to directory required to delete files. 487 */ 488 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 489 if (error) 490 return (error); 491 492 /* 493 * Return pointer to current entry in dp->i_offset. 494 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 495 */ 496 if (dp->de_StartCluster == scn && isadir) { /* "." */ 497 VREF(vdp); 498 *vpp = vdp; 499 return (0); 500 } 501 error = deget(pmp, cluster, blkoff, &tdp); 502 if (error) 503 return (error); 504 *vpp = DETOV(tdp); 505 return (0); 506 } 507 508 /* 509 * If rewriting (RENAME), return the inode and the 510 * information required to rewrite the present directory 511 * Must get inode of directory entry to verify it's a 512 * regular file, or empty directory. 513 */ 514 if (nameiop == RENAME && (flags & ISLASTCN)) { 515 if (blkoff == MSDOSFSROOT_OFS) 516 return (EBUSY); 517 518 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 519 if (error) 520 return (error); 521 522 /* 523 * Careful about locking second inode. 524 * This can only occur if the target is ".". 525 */ 526 if (dp->de_StartCluster == scn && isadir) 527 return (EISDIR); 528 529 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 530 return (error); 531 *vpp = DETOV(tdp); 532 cnp->cn_flags |= SAVENAME; 533 return (0); 534 } 535 536 /* 537 * Step through the translation in the name. We do not `vput' the 538 * directory because we may need it again if a symbolic link 539 * is relative to the current directory. Instead we save it 540 * unlocked as "pdp". We must get the target inode before unlocking 541 * the directory to insure that the inode will not be removed 542 * before we get it. We prevent deadlock by always fetching 543 * inodes from the root, moving down the directory tree. Thus 544 * when following backward pointers ".." we must unlock the 545 * parent directory before getting the requested directory. 546 */ 547 pdp = vdp; 548 if (flags & ISDOTDOT) { 549 dd_arg.cluster = cluster; 550 dd_arg.blkoff = blkoff; 551 error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot, 552 &dd_arg, cnp->cn_lkflags, vpp); 553 if (error != 0) { 554 *vpp = NULL; 555 return (error); 556 } 557 /* 558 * Recheck that ".." still points to the inode we 559 * looked up before pdp lock was dropped. 560 */ 561 error = msdosfs_lookup_(pdp, NULL, cnp, &inode1); 562 if (error) { 563 vput(*vpp); 564 *vpp = NULL; 565 return (error); 566 } 567 if (VTODE(*vpp)->de_inode != inode1) { 568 vput(*vpp); 569 goto restart; 570 } 571 } else if (dp->de_StartCluster == scn && isadir) { 572 VREF(vdp); /* we want ourself, ie "." */ 573 *vpp = vdp; 574 } else { 575 if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 576 return (error); 577 *vpp = DETOV(tdp); 578 } 579 580 /* 581 * Insert name into cache if appropriate. 582 */ 583 if (cnp->cn_flags & MAKEENTRY) 584 cache_enter(vdp, *vpp, cnp); 585 return (0); 586 } 587 588 /* 589 * dep - directory entry to copy into the directory 590 * ddep - directory to add to 591 * depp - return the address of the denode for the created directory entry 592 * if depp != 0 593 * cnp - componentname needed for Win95 long filenames 594 */ 595 int 596 createde(struct denode *dep, struct denode *ddep, struct denode **depp, 597 struct componentname *cnp) 598 { 599 int error; 600 u_long dirclust, diroffset; 601 struct direntry *ndep; 602 struct msdosfsmount *pmp = ddep->de_pmp; 603 struct buf *bp; 604 daddr_t bn; 605 int blsize; 606 607 #ifdef MSDOSFS_DEBUG 608 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 609 dep, ddep, depp, cnp); 610 #endif 611 612 /* 613 * If no space left in the directory then allocate another cluster 614 * and chain it onto the end of the file. There is one exception 615 * to this. That is, if the root directory has no more space it 616 * can NOT be expanded. extendfile() checks for and fails attempts 617 * to extend the root directory. We just return an error in that 618 * case. 619 */ 620 if (ddep->de_fndoffset >= ddep->de_FileSize) { 621 diroffset = ddep->de_fndoffset + sizeof(struct direntry) 622 - ddep->de_FileSize; 623 dirclust = de_clcount(pmp, diroffset); 624 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 625 if (error) { 626 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED); 627 return error; 628 } 629 630 /* 631 * Update the size of the directory 632 */ 633 ddep->de_FileSize += de_cn2off(pmp, dirclust); 634 } 635 636 /* 637 * We just read in the cluster with space. Copy the new directory 638 * entry in. Then write it to disk. NOTE: DOS directories 639 * do not get smaller as clusters are emptied. 640 */ 641 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 642 &bn, &dirclust, &blsize); 643 if (error) 644 return error; 645 diroffset = ddep->de_fndoffset; 646 if (dirclust != MSDOSFSROOT) 647 diroffset &= pmp->pm_crbomask; 648 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 649 brelse(bp); 650 return error; 651 } 652 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 653 654 DE_EXTERNALIZE(ndep, dep); 655 656 /* 657 * Now write the Win95 long name 658 */ 659 if (ddep->de_fndcnt > 0) { 660 uint8_t chksum = winChksum(ndep->deName); 661 const u_char *un = (const u_char *)cnp->cn_nameptr; 662 int unlen = cnp->cn_namelen; 663 int cnt = 1; 664 665 while (--ddep->de_fndcnt >= 0) { 666 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 667 if (DOINGASYNC(DETOV(ddep))) 668 bdwrite(bp); 669 else if ((error = bwrite(bp)) != 0) 670 return error; 671 672 ddep->de_fndoffset -= sizeof(struct direntry); 673 error = pcbmap(ddep, 674 de_cluster(pmp, 675 ddep->de_fndoffset), 676 &bn, 0, &blsize); 677 if (error) 678 return error; 679 680 error = bread(pmp->pm_devvp, bn, blsize, 681 NOCRED, &bp); 682 if (error) { 683 return error; 684 } 685 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 686 } else { 687 ndep--; 688 ddep->de_fndoffset -= sizeof(struct direntry); 689 } 690 if (!unix2winfn(un, unlen, (struct winentry *)ndep, 691 cnt++, chksum, pmp)) 692 break; 693 } 694 } 695 696 if (DOINGASYNC(DETOV(ddep))) 697 bdwrite(bp); 698 else if ((error = bwrite(bp)) != 0) 699 return error; 700 701 /* 702 * If they want us to return with the denode gotten. 703 */ 704 if (depp) { 705 if (dep->de_Attributes & ATTR_DIRECTORY) { 706 dirclust = dep->de_StartCluster; 707 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 708 dirclust = MSDOSFSROOT; 709 if (dirclust == MSDOSFSROOT) 710 diroffset = MSDOSFSROOT_OFS; 711 else 712 diroffset = 0; 713 } 714 return deget(pmp, dirclust, diroffset, depp); 715 } 716 717 return 0; 718 } 719 720 /* 721 * Be sure a directory is empty except for "." and "..". Return 1 if empty, 722 * return 0 if not empty or error. 723 */ 724 int 725 dosdirempty(struct denode *dep) 726 { 727 int blsize; 728 int error; 729 u_long cn; 730 daddr_t bn; 731 struct buf *bp; 732 struct msdosfsmount *pmp = dep->de_pmp; 733 struct direntry *dentp; 734 735 /* 736 * Since the filesize field in directory entries for a directory is 737 * zero, we just have to feel our way through the directory until 738 * we hit end of file. 739 */ 740 for (cn = 0;; cn++) { 741 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 742 if (error == E2BIG) 743 return (1); /* it's empty */ 744 return (0); 745 } 746 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 747 if (error) { 748 return (0); 749 } 750 for (dentp = (struct direntry *)bp->b_data; 751 (char *)dentp < bp->b_data + blsize; 752 dentp++) { 753 if (dentp->deName[0] != SLOT_DELETED && 754 (dentp->deAttributes & ATTR_VOLUME) == 0) { 755 /* 756 * In dos directories an entry whose name 757 * starts with SLOT_EMPTY (0) starts the 758 * beginning of the unused part of the 759 * directory, so we can just return that it 760 * is empty. 761 */ 762 if (dentp->deName[0] == SLOT_EMPTY) { 763 brelse(bp); 764 return (1); 765 } 766 /* 767 * Any names other than "." and ".." in a 768 * directory mean it is not empty. 769 */ 770 if (bcmp(dentp->deName, ". ", 11) && 771 bcmp(dentp->deName, ".. ", 11)) { 772 brelse(bp); 773 #ifdef MSDOSFS_DEBUG 774 printf("dosdirempty(): entry found %02x, %02x\n", 775 dentp->deName[0], dentp->deName[1]); 776 #endif 777 return (0); /* not empty */ 778 } 779 } 780 } 781 brelse(bp); 782 } 783 /* NOTREACHED */ 784 } 785 786 /* 787 * Check to see if the directory described by target is in some 788 * subdirectory of source. This prevents something like the following from 789 * succeeding and leaving a bunch or files and directories orphaned. mv 790 * /a/b/c /a/b/c/d/e/f Where c and f are directories. 791 * 792 * source - the inode for /a/b/c 793 * target - the inode for /a/b/c/d/e/f 794 * 795 * Returns 0 if target is NOT a subdirectory of source. 796 * Otherwise returns a non-zero error number. 797 * The target inode is always unlocked on return. 798 */ 799 int 800 doscheckpath(struct denode *source, struct denode *target) 801 { 802 daddr_t scn; 803 struct msdosfsmount *pmp; 804 struct direntry *ep; 805 struct denode *dep; 806 struct buf *bp = NULL; 807 int error = 0; 808 809 dep = target; 810 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 || 811 (source->de_Attributes & ATTR_DIRECTORY) == 0) { 812 error = ENOTDIR; 813 goto out; 814 } 815 if (dep->de_StartCluster == source->de_StartCluster) { 816 error = EEXIST; 817 goto out; 818 } 819 if (dep->de_StartCluster == MSDOSFSROOT) 820 goto out; 821 pmp = dep->de_pmp; 822 #ifdef DIAGNOSTIC 823 if (pmp != source->de_pmp) 824 panic("doscheckpath: source and target on different filesystems"); 825 #endif 826 if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk) 827 goto out; 828 829 for (;;) { 830 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 831 error = ENOTDIR; 832 break; 833 } 834 scn = dep->de_StartCluster; 835 error = bread(pmp->pm_devvp, cntobn(pmp, scn), 836 pmp->pm_bpcluster, NOCRED, &bp); 837 if (error) 838 break; 839 840 ep = (struct direntry *) bp->b_data + 1; 841 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 || 842 bcmp(ep->deName, ".. ", 11) != 0) { 843 error = ENOTDIR; 844 break; 845 } 846 scn = getushort(ep->deStartCluster); 847 if (FAT32(pmp)) 848 scn |= getushort(ep->deHighClust) << 16; 849 850 if (scn == source->de_StartCluster) { 851 error = EINVAL; 852 break; 853 } 854 if (scn == MSDOSFSROOT) 855 break; 856 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) { 857 /* 858 * scn should be 0 in this case, 859 * but we silently ignore the error. 860 */ 861 break; 862 } 863 864 vput(DETOV(dep)); 865 brelse(bp); 866 bp = NULL; 867 /* NOTE: deget() clears dep on error */ 868 if ((error = deget(pmp, scn, 0, &dep)) != 0) 869 break; 870 } 871 out:; 872 if (bp) 873 brelse(bp); 874 #ifdef MSDOSFS_DEBUG 875 if (error == ENOTDIR) 876 printf("doscheckpath(): .. not a directory?\n"); 877 #endif 878 if (dep != NULL) 879 vput(DETOV(dep)); 880 return (error); 881 } 882 883 /* 884 * Read in the disk block containing the directory entry (dirclu, dirofs) 885 * and return the address of the buf header, and the address of the 886 * directory entry within the block. 887 */ 888 int 889 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 890 struct buf **bpp, struct direntry **epp) 891 { 892 int error; 893 daddr_t bn; 894 int blsize; 895 896 blsize = pmp->pm_bpcluster; 897 if (dirclust == MSDOSFSROOT 898 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 899 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 900 bn = detobn(pmp, dirclust, diroffset); 901 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 902 brelse(*bpp); 903 *bpp = NULL; 904 return (error); 905 } 906 if (epp) 907 *epp = bptoep(pmp, *bpp, diroffset); 908 return (0); 909 } 910 911 /* 912 * Read in the disk block containing the directory entry dep came from and 913 * return the address of the buf header, and the address of the directory 914 * entry within the block. 915 */ 916 int 917 readde(struct denode *dep, struct buf **bpp, struct direntry **epp) 918 { 919 920 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 921 bpp, epp)); 922 } 923 924 /* 925 * Remove a directory entry. At this point the file represented by the 926 * directory entry to be removed is still full length until no one has it 927 * open. When the file no longer being used msdosfs_inactive() is called 928 * and will truncate the file to 0 length. When the vnode containing the 929 * denode is needed for some other purpose by VFS it will call 930 * msdosfs_reclaim() which will remove the denode from the denode cache. 931 * 932 * pdep directory where the entry is removed 933 * dep file to be removed 934 */ 935 int 936 removede(struct denode *pdep, struct denode *dep) 937 { 938 int error; 939 struct direntry *ep; 940 struct buf *bp; 941 daddr_t bn; 942 int blsize; 943 struct msdosfsmount *pmp = pdep->de_pmp; 944 u_long offset = pdep->de_fndoffset; 945 946 #ifdef MSDOSFS_DEBUG 947 printf("removede(): filename %s, dep %p, offset %08lx\n", 948 dep->de_Name, dep, offset); 949 #endif 950 951 dep->de_refcnt--; 952 offset += sizeof(struct direntry); 953 do { 954 offset -= sizeof(struct direntry); 955 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize); 956 if (error) 957 return error; 958 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 959 if (error) { 960 return error; 961 } 962 ep = bptoep(pmp, bp, offset); 963 /* 964 * Check whether, if we came here the second time, i.e. 965 * when underflowing into the previous block, the last 966 * entry in this block is a longfilename entry, too. 967 */ 968 if (ep->deAttributes != ATTR_WIN95 969 && offset != pdep->de_fndoffset) { 970 brelse(bp); 971 break; 972 } 973 offset += sizeof(struct direntry); 974 while (1) { 975 /* 976 * We are a bit aggressive here in that we delete any Win95 977 * entries preceding this entry, not just the ones we "own". 978 * Since these presumably aren't valid anyway, 979 * there should be no harm. 980 */ 981 offset -= sizeof(struct direntry); 982 ep--->deName[0] = SLOT_DELETED; 983 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) 984 || !(offset & pmp->pm_crbomask) 985 || ep->deAttributes != ATTR_WIN95) 986 break; 987 } 988 if (DOINGASYNC(DETOV(pdep))) 989 bdwrite(bp); 990 else if ((error = bwrite(bp)) != 0) 991 return error; 992 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95) 993 && !(offset & pmp->pm_crbomask) 994 && offset); 995 return 0; 996 } 997 998 /* 999 * Create a unique DOS name in dvp 1000 */ 1001 int 1002 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 1003 { 1004 struct msdosfsmount *pmp = dep->de_pmp; 1005 struct direntry *dentp; 1006 int gen; 1007 int blsize; 1008 u_long cn; 1009 daddr_t bn; 1010 struct buf *bp; 1011 int error; 1012 1013 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 1014 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 1015 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL); 1016 1017 for (gen = 1;; gen++) { 1018 /* 1019 * Generate DOS name with generation number 1020 */ 1021 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 1022 cnp->cn_namelen, gen, pmp)) 1023 return gen == 1 ? EINVAL : EEXIST; 1024 1025 /* 1026 * Now look for a dir entry with this exact name 1027 */ 1028 for (cn = error = 0; !error; cn++) { 1029 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 1030 if (error == E2BIG) /* EOF reached and not found */ 1031 return 0; 1032 return error; 1033 } 1034 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 1035 if (error) { 1036 return error; 1037 } 1038 for (dentp = (struct direntry *)bp->b_data; 1039 (char *)dentp < bp->b_data + blsize; 1040 dentp++) { 1041 if (dentp->deName[0] == SLOT_EMPTY) { 1042 /* 1043 * Last used entry and not found 1044 */ 1045 brelse(bp); 1046 return 0; 1047 } 1048 /* 1049 * Ignore volume labels and Win95 entries 1050 */ 1051 if (dentp->deAttributes & ATTR_VOLUME) 1052 continue; 1053 if (!bcmp(dentp->deName, cp, 11)) { 1054 error = EEXIST; 1055 break; 1056 } 1057 } 1058 brelse(bp); 1059 } 1060 } 1061 } 1062