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 int 67 msdosfs_lookup(struct vop_cachedlookup_args *ap) 68 { 69 70 return (msdosfs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL, 71 NULL)); 72 } 73 74 struct deget_dotdot { 75 u_long cluster; 76 int blkoff; 77 }; 78 79 static int 80 msdosfs_deget_dotdot(struct mount *mp, void *arg, int lkflags, 81 struct vnode **rvp) 82 { 83 struct deget_dotdot *dd_arg; 84 struct denode *rdp; 85 struct msdosfsmount *pmp; 86 int error; 87 88 pmp = VFSTOMSDOSFS(mp); 89 dd_arg = arg; 90 error = deget(pmp, dd_arg->cluster, dd_arg->blkoff, 91 LK_EXCLUSIVE, &rdp); 92 if (error == 0) 93 *rvp = DETOV(rdp); 94 return (error); 95 } 96 97 /* 98 * When we search a directory the blocks containing directory entries are 99 * read and examined. The directory entries contain information that would 100 * normally be in the inode of a unix filesystem. This means that some of 101 * a directory's contents may also be in memory resident denodes (sort of 102 * an inode). This can cause problems if we are searching while some other 103 * process is modifying a directory. To prevent one process from accessing 104 * incompletely modified directory information we depend upon being the 105 * sole owner of a directory block. bread/brelse provide this service. 106 * This being the case, when a process modifies a directory it must first 107 * acquire the disk block that contains the directory entry to be modified. 108 * Then update the disk block and the denode, and then write the disk block 109 * out to disk. This way disk blocks containing directory entries and in 110 * memory denode's will be in synch. 111 */ 112 int 113 msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname 114 *cnp, daddr_t *scnp, u_long *blkoffp) 115 { 116 struct mbnambuf nb; 117 daddr_t bn; 118 int error; 119 int slotcount; 120 int slotoffset = 0; 121 int frcn; 122 u_long cluster; 123 u_long blkoff; 124 int diroff; 125 int blsize; 126 int isadir; /* ~0 if found direntry is a directory */ 127 daddr_t scn; /* starting cluster number */ 128 struct vnode *pdp; 129 struct denode *dp; 130 struct denode *tdp; 131 struct msdosfsmount *pmp; 132 struct buf *bp = NULL; 133 struct direntry *dep = NULL; 134 struct deget_dotdot dd_arg; 135 u_char dosfilename[12]; 136 int flags = cnp->cn_flags; 137 int nameiop = cnp->cn_nameiop; 138 int unlen; 139 uint64_t inode1; 140 141 int wincnt = 1; 142 int chksum = -1, chksum_ok; 143 int olddos = 1; 144 145 #ifdef MSDOSFS_DEBUG 146 printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr); 147 #endif 148 dp = VTODE(vdp); 149 pmp = dp->de_pmp; 150 #ifdef MSDOSFS_DEBUG 151 printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n", 152 vdp, dp, dp->de_Attributes); 153 #endif 154 155 restart: 156 if (vpp != NULL) 157 *vpp = NULL; 158 /* 159 * If they are going after the . or .. entry in the root directory, 160 * they won't find it. DOS filesystems don't have them in the root 161 * directory. So, we fake it. deget() is in on this scam too. 162 */ 163 if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' && 164 (cnp->cn_namelen == 1 || 165 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) { 166 isadir = ATTR_DIRECTORY; 167 scn = MSDOSFSROOT; 168 #ifdef MSDOSFS_DEBUG 169 printf("msdosfs_lookup(): looking for . or .. in root directory\n"); 170 #endif 171 cluster = MSDOSFSROOT; 172 blkoff = MSDOSFSROOT_OFS; 173 goto foundroot; 174 } 175 176 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename, 177 cnp->cn_namelen, 0, pmp)) { 178 case 0: 179 return (EINVAL); 180 case 1: 181 break; 182 case 2: 183 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 184 cnp->cn_namelen, pmp) + 1; 185 break; 186 case 3: 187 olddos = 0; 188 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 189 cnp->cn_namelen, pmp) + 1; 190 break; 191 } 192 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) { 193 wincnt = 1; 194 olddos = 1; 195 } 196 unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen); 197 198 /* 199 * Suppress search for slots unless creating 200 * file and at end of pathname, in which case 201 * we watch for a place to put the new file in 202 * case it doesn't already exist. 203 */ 204 slotcount = wincnt; 205 if ((nameiop == CREATE || nameiop == RENAME) && 206 (flags & ISLASTCN)) 207 slotcount = 0; 208 209 #ifdef MSDOSFS_DEBUG 210 printf("msdosfs_lookup(): dos version of filename %s, length %ld\n", 211 dosfilename, cnp->cn_namelen); 212 #endif 213 /* 214 * Search the directory pointed at by vdp for the name pointed at 215 * by cnp->cn_nameptr. 216 */ 217 tdp = NULL; 218 mbnambuf_init(&nb); 219 /* 220 * The outer loop ranges over the clusters that make up the 221 * directory. Note that the root directory is different from all 222 * other directories. It has a fixed number of blocks that are not 223 * part of the pool of allocatable clusters. So, we treat it a 224 * little differently. The root directory starts at "cluster" 0. 225 */ 226 diroff = 0; 227 for (frcn = 0;; frcn++) { 228 error = pcbmap(dp, frcn, &bn, &cluster, &blsize); 229 if (error) { 230 if (error == E2BIG) 231 break; 232 return (error); 233 } 234 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 235 if (error) { 236 return (error); 237 } 238 for (blkoff = 0; blkoff < blsize; 239 blkoff += sizeof(struct direntry), 240 diroff += sizeof(struct direntry)) { 241 dep = (struct direntry *)(bp->b_data + blkoff); 242 /* 243 * If the slot is empty and we are still looking 244 * for an empty then remember this one. If the 245 * slot is not empty then check to see if it 246 * matches what we are looking for. If the slot 247 * has never been filled with anything, then the 248 * remainder of the directory has never been used, 249 * so there is no point in searching it. 250 */ 251 if (dep->deName[0] == SLOT_EMPTY || 252 dep->deName[0] == SLOT_DELETED) { 253 /* 254 * Drop memory of previous long matches 255 */ 256 chksum = -1; 257 mbnambuf_init(&nb); 258 259 if (slotcount < wincnt) { 260 slotcount++; 261 slotoffset = diroff; 262 } 263 if (dep->deName[0] == SLOT_EMPTY) { 264 brelse(bp); 265 goto notfound; 266 } 267 } else { 268 /* 269 * If there wasn't enough space for our winentries, 270 * forget about the empty space 271 */ 272 if (slotcount < wincnt) 273 slotcount = 0; 274 275 /* 276 * Check for Win95 long filename entry 277 */ 278 if (dep->deAttributes == ATTR_WIN95) { 279 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 280 continue; 281 282 chksum = win2unixfn(&nb, 283 (struct winentry *)dep, chksum, 284 pmp); 285 continue; 286 } 287 288 chksum = winChkName(&nb, 289 (const u_char *)cnp->cn_nameptr, unlen, 290 chksum, pmp); 291 if (chksum == -2) { 292 chksum = -1; 293 continue; 294 } 295 296 /* 297 * Ignore volume labels (anywhere, not just 298 * the root directory). 299 */ 300 if (dep->deAttributes & ATTR_VOLUME) { 301 chksum = -1; 302 continue; 303 } 304 305 /* 306 * Check for a checksum or name match 307 */ 308 chksum_ok = (chksum == winChksum(dep->deName)); 309 if (!chksum_ok 310 && (!olddos || bcmp(dosfilename, dep->deName, 11))) { 311 chksum = -1; 312 continue; 313 } 314 #ifdef MSDOSFS_DEBUG 315 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n", 316 blkoff, diroff); 317 #endif 318 /* 319 * Remember where this directory 320 * entry came from for whoever did 321 * this lookup. 322 */ 323 dp->de_fndoffset = diroff; 324 if (chksum_ok && nameiop == RENAME) { 325 /* 326 * Target had correct long name 327 * directory entries, reuse them 328 * as needed. 329 */ 330 dp->de_fndcnt = wincnt - 1; 331 } else { 332 /* 333 * Long name directory entries 334 * not present or corrupt, can only 335 * reuse dos directory entry. 336 */ 337 dp->de_fndcnt = 0; 338 } 339 340 goto found; 341 } 342 } /* for (blkoff = 0; .... */ 343 /* 344 * Release the buffer holding the directory cluster just 345 * searched. 346 */ 347 brelse(bp); 348 } /* for (frcn = 0; ; frcn++) */ 349 350 notfound: 351 /* 352 * We hold no disk buffers at this point. 353 */ 354 355 /* 356 * Fixup the slot description to point to the place where 357 * we might put the new DOS direntry (putting the Win95 358 * long name entries before that) 359 */ 360 if (!slotcount) { 361 slotcount = 1; 362 slotoffset = diroff; 363 } 364 if (wincnt > slotcount) 365 slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 366 367 /* 368 * If we get here we didn't find the entry we were looking for. But 369 * that's ok if we are creating or renaming and are at the end of 370 * the pathname and the directory hasn't been removed. 371 */ 372 #ifdef MSDOSFS_DEBUG 373 printf("msdosfs_lookup(): op %d, refcnt %ld\n", 374 nameiop, dp->de_refcnt); 375 printf(" slotcount %d, slotoffset %d\n", 376 slotcount, slotoffset); 377 #endif 378 if ((nameiop == CREATE || nameiop == RENAME) && 379 (flags & ISLASTCN) && dp->de_refcnt != 0) { 380 /* 381 * Access for write is interpreted as allowing 382 * creation of files in the directory. 383 */ 384 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, curthread); 385 if (error) 386 return (error); 387 /* 388 * Return an indication of where the new directory 389 * entry should be put. 390 */ 391 dp->de_fndoffset = slotoffset; 392 dp->de_fndcnt = wincnt - 1; 393 394 /* 395 * We return with the directory locked, so that 396 * the parameters we set up above will still be 397 * valid if we actually decide to do a direnter(). 398 * We return ni_vp == NULL to indicate that the entry 399 * does not currently exist; we leave a pointer to 400 * the (locked) directory inode in ndp->ni_dvp. 401 * The pathname buffer is saved so that the name 402 * can be obtained later. 403 * 404 * NB - if the directory is unlocked, then this 405 * information cannot be used. 406 */ 407 cnp->cn_flags |= SAVENAME; 408 return (EJUSTRETURN); 409 } 410 #if 0 411 /* 412 * Insert name into cache (as non-existent) if appropriate. 413 * 414 * XXX Negative caching is broken for msdosfs because the name 415 * cache doesn't understand peculiarities such as case insensitivity 416 * and 8.3 filenames. Hence, it may not invalidate all negative 417 * entries if a file with this name is later created. 418 */ 419 if ((cnp->cn_flags & MAKEENTRY) != 0) 420 cache_enter(vdp, *vpp, cnp); 421 #endif 422 return (ENOENT); 423 424 found: 425 /* 426 * NOTE: We still have the buffer with matched directory entry at 427 * this point. 428 */ 429 isadir = dep->deAttributes & ATTR_DIRECTORY; 430 scn = getushort(dep->deStartCluster); 431 if (FAT32(pmp)) { 432 scn |= getushort(dep->deHighClust) << 16; 433 if (scn == pmp->pm_rootdirblk) { 434 /* 435 * There should actually be 0 here. 436 * Just ignore the error. 437 */ 438 scn = MSDOSFSROOT; 439 } 440 } 441 442 if (isadir) { 443 cluster = scn; 444 if (cluster == MSDOSFSROOT) 445 blkoff = MSDOSFSROOT_OFS; 446 else 447 blkoff = 0; 448 } else if (cluster == MSDOSFSROOT) 449 blkoff = diroff; 450 451 /* 452 * Now release buf to allow deget to read the entry again. 453 * Reserving it here and giving it to deget could result 454 * in a deadlock. 455 */ 456 brelse(bp); 457 bp = NULL; 458 459 foundroot: 460 /* 461 * If we entered at foundroot, then we are looking for the . or .. 462 * entry of the filesystems root directory. isadir and scn were 463 * setup before jumping here. And, bp is already null. 464 */ 465 if (FAT32(pmp) && scn == MSDOSFSROOT) 466 scn = pmp->pm_rootdirblk; 467 468 if (scnp != NULL) { 469 *scnp = cluster; 470 *blkoffp = 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, curthread); 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, LK_EXCLUSIVE, &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, curthread); 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, LK_EXCLUSIVE, 530 &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_ino(pdp, NULL, cnp, &scn, &blkoff); 563 if (error) { 564 vput(*vpp); 565 *vpp = NULL; 566 return (error); 567 } 568 if (FAT32(pmp) && scn == MSDOSFSROOT) 569 scn = pmp->pm_rootdirblk; 570 inode1 = scn * pmp->pm_bpcluster + blkoff; 571 if (VTODE(*vpp)->de_inode != inode1) { 572 vput(*vpp); 573 goto restart; 574 } 575 } else if (dp->de_StartCluster == scn && isadir) { 576 VREF(vdp); /* we want ourself, ie "." */ 577 *vpp = vdp; 578 } else { 579 if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, 580 &tdp)) != 0) 581 return (error); 582 *vpp = DETOV(tdp); 583 } 584 585 /* 586 * Insert name into cache if appropriate. 587 */ 588 if (cnp->cn_flags & MAKEENTRY) 589 cache_enter(vdp, *vpp, cnp); 590 return (0); 591 } 592 593 /* 594 * dep - directory entry to copy into the directory 595 * ddep - directory to add to 596 * depp - return the address of the denode for the created directory entry 597 * if depp != 0 598 * cnp - componentname needed for Win95 long filenames 599 */ 600 int 601 createde(struct denode *dep, struct denode *ddep, struct denode **depp, 602 struct componentname *cnp) 603 { 604 int error; 605 u_long dirclust, diroffset; 606 struct direntry *ndep; 607 struct msdosfsmount *pmp = ddep->de_pmp; 608 struct buf *bp; 609 daddr_t bn; 610 int blsize; 611 612 #ifdef MSDOSFS_DEBUG 613 printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 614 dep, ddep, depp, cnp); 615 #endif 616 617 /* 618 * If no space left in the directory then allocate another cluster 619 * and chain it onto the end of the file. There is one exception 620 * to this. That is, if the root directory has no more space it 621 * can NOT be expanded. extendfile() checks for and fails attempts 622 * to extend the root directory. We just return an error in that 623 * case. 624 */ 625 if (ddep->de_fndoffset >= ddep->de_FileSize) { 626 diroffset = ddep->de_fndoffset + sizeof(struct direntry) 627 - ddep->de_FileSize; 628 dirclust = de_clcount(pmp, diroffset); 629 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 630 if (error) { 631 (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED); 632 return error; 633 } 634 635 /* 636 * Update the size of the directory 637 */ 638 ddep->de_FileSize += de_cn2off(pmp, dirclust); 639 } 640 641 /* 642 * We just read in the cluster with space. Copy the new directory 643 * entry in. Then write it to disk. NOTE: DOS directories 644 * do not get smaller as clusters are emptied. 645 */ 646 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 647 &bn, &dirclust, &blsize); 648 if (error) 649 return error; 650 diroffset = ddep->de_fndoffset; 651 if (dirclust != MSDOSFSROOT) 652 diroffset &= pmp->pm_crbomask; 653 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 654 brelse(bp); 655 return error; 656 } 657 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 658 659 DE_EXTERNALIZE(ndep, dep); 660 661 /* 662 * Now write the Win95 long name 663 */ 664 if (ddep->de_fndcnt > 0) { 665 uint8_t chksum = winChksum(ndep->deName); 666 const u_char *un = (const u_char *)cnp->cn_nameptr; 667 int unlen = cnp->cn_namelen; 668 int cnt = 1; 669 670 while (--ddep->de_fndcnt >= 0) { 671 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 672 if (DOINGASYNC(DETOV(ddep))) 673 bdwrite(bp); 674 else if ((error = bwrite(bp)) != 0) 675 return error; 676 677 ddep->de_fndoffset -= sizeof(struct direntry); 678 error = pcbmap(ddep, 679 de_cluster(pmp, 680 ddep->de_fndoffset), 681 &bn, 0, &blsize); 682 if (error) 683 return error; 684 685 error = bread(pmp->pm_devvp, bn, blsize, 686 NOCRED, &bp); 687 if (error) { 688 return error; 689 } 690 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 691 } else { 692 ndep--; 693 ddep->de_fndoffset -= sizeof(struct direntry); 694 } 695 if (!unix2winfn(un, unlen, (struct winentry *)ndep, 696 cnt++, chksum, pmp)) 697 break; 698 } 699 } 700 701 if (DOINGASYNC(DETOV(ddep))) 702 bdwrite(bp); 703 else if ((error = bwrite(bp)) != 0) 704 return error; 705 706 /* 707 * If they want us to return with the denode gotten. 708 */ 709 if (depp) { 710 if (dep->de_Attributes & ATTR_DIRECTORY) { 711 dirclust = dep->de_StartCluster; 712 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 713 dirclust = MSDOSFSROOT; 714 if (dirclust == MSDOSFSROOT) 715 diroffset = MSDOSFSROOT_OFS; 716 else 717 diroffset = 0; 718 } 719 return (deget(pmp, dirclust, diroffset, LK_EXCLUSIVE, depp)); 720 } 721 722 return 0; 723 } 724 725 /* 726 * Be sure a directory is empty except for "." and "..". Return 1 if empty, 727 * return 0 if not empty or error. 728 */ 729 int 730 dosdirempty(struct denode *dep) 731 { 732 int blsize; 733 int error; 734 u_long cn; 735 daddr_t bn; 736 struct buf *bp; 737 struct msdosfsmount *pmp = dep->de_pmp; 738 struct direntry *dentp; 739 740 /* 741 * Since the filesize field in directory entries for a directory is 742 * zero, we just have to feel our way through the directory until 743 * we hit end of file. 744 */ 745 for (cn = 0;; cn++) { 746 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 747 if (error == E2BIG) 748 return (1); /* it's empty */ 749 return (0); 750 } 751 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 752 if (error) { 753 return (0); 754 } 755 for (dentp = (struct direntry *)bp->b_data; 756 (char *)dentp < bp->b_data + blsize; 757 dentp++) { 758 if (dentp->deName[0] != SLOT_DELETED && 759 (dentp->deAttributes & ATTR_VOLUME) == 0) { 760 /* 761 * In dos directories an entry whose name 762 * starts with SLOT_EMPTY (0) starts the 763 * beginning of the unused part of the 764 * directory, so we can just return that it 765 * is empty. 766 */ 767 if (dentp->deName[0] == SLOT_EMPTY) { 768 brelse(bp); 769 return (1); 770 } 771 /* 772 * Any names other than "." and ".." in a 773 * directory mean it is not empty. 774 */ 775 if (bcmp(dentp->deName, ". ", 11) && 776 bcmp(dentp->deName, ".. ", 11)) { 777 brelse(bp); 778 #ifdef MSDOSFS_DEBUG 779 printf("dosdirempty(): entry found %02x, %02x\n", 780 dentp->deName[0], dentp->deName[1]); 781 #endif 782 return (0); /* not empty */ 783 } 784 } 785 } 786 brelse(bp); 787 } 788 /* NOTREACHED */ 789 } 790 791 /* 792 * Check to see if the directory described by target is in some 793 * subdirectory of source. This prevents something like the following from 794 * succeeding and leaving a bunch or files and directories orphaned. mv 795 * /a/b/c /a/b/c/d/e/f Where c and f are directories. 796 * 797 * source - the inode for /a/b/c 798 * target - the inode for /a/b/c/d/e/f 799 * 800 * Returns 0 if target is NOT a subdirectory of source. 801 * Otherwise returns a non-zero error number. 802 */ 803 int 804 doscheckpath(struct denode *source, struct denode *target, daddr_t *wait_scn) 805 { 806 daddr_t scn; 807 struct msdosfsmount *pmp; 808 struct direntry *ep; 809 struct denode *dep; 810 struct buf *bp = NULL; 811 int error = 0; 812 813 *wait_scn = 0; 814 815 pmp = target->de_pmp; 816 lockmgr_assert(&pmp->pm_checkpath_lock, KA_XLOCKED); 817 KASSERT(pmp == source->de_pmp, 818 ("doscheckpath: source and target on different filesystems")); 819 820 if ((target->de_Attributes & ATTR_DIRECTORY) == 0 || 821 (source->de_Attributes & ATTR_DIRECTORY) == 0) 822 return (ENOTDIR); 823 824 if (target->de_StartCluster == source->de_StartCluster) 825 return (EEXIST); 826 827 if (target->de_StartCluster == MSDOSFSROOT || 828 (FAT32(pmp) && target->de_StartCluster == pmp->pm_rootdirblk)) 829 return (0); 830 831 dep = target; 832 vget(DETOV(dep), LK_EXCLUSIVE); 833 for (;;) { 834 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 835 error = ENOTDIR; 836 break; 837 } 838 scn = dep->de_StartCluster; 839 error = bread(pmp->pm_devvp, cntobn(pmp, scn), 840 pmp->pm_bpcluster, NOCRED, &bp); 841 if (error != 0) 842 break; 843 844 ep = (struct direntry *)bp->b_data + 1; 845 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 || 846 bcmp(ep->deName, ".. ", 11) != 0) { 847 error = ENOTDIR; 848 brelse(bp); 849 break; 850 } 851 852 scn = getushort(ep->deStartCluster); 853 if (FAT32(pmp)) 854 scn |= getushort(ep->deHighClust) << 16; 855 brelse(bp); 856 857 if (scn == source->de_StartCluster) { 858 error = EINVAL; 859 break; 860 } 861 if (scn == MSDOSFSROOT) 862 break; 863 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) { 864 /* 865 * scn should be 0 in this case, 866 * but we silently ignore the error. 867 */ 868 break; 869 } 870 871 vput(DETOV(dep)); 872 dep = NULL; 873 /* NOTE: deget() clears dep on error */ 874 error = deget(pmp, scn, 0, LK_EXCLUSIVE | LK_NOWAIT, &dep); 875 if (error != 0) { 876 *wait_scn = scn; 877 break; 878 } 879 } 880 #ifdef MSDOSFS_DEBUG 881 if (error == ENOTDIR) 882 printf("doscheckpath(): .. not a directory?\n"); 883 #endif 884 if (dep != NULL) 885 vput(DETOV(dep)); 886 return (error); 887 } 888 889 /* 890 * Read in the disk block containing the directory entry (dirclu, dirofs) 891 * and return the address of the buf header, and the address of the 892 * directory entry within the block. 893 */ 894 int 895 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 896 struct buf **bpp, struct direntry **epp) 897 { 898 int error; 899 daddr_t bn; 900 int blsize; 901 902 blsize = pmp->pm_bpcluster; 903 if (dirclust == MSDOSFSROOT 904 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 905 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 906 bn = detobn(pmp, dirclust, diroffset); 907 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 908 brelse(*bpp); 909 *bpp = NULL; 910 return (error); 911 } 912 if (epp) 913 *epp = bptoep(pmp, *bpp, diroffset); 914 return (0); 915 } 916 917 /* 918 * Read in the disk block containing the directory entry dep came from and 919 * return the address of the buf header, and the address of the directory 920 * entry within the block. 921 */ 922 int 923 readde(struct denode *dep, struct buf **bpp, struct direntry **epp) 924 { 925 926 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 927 bpp, epp)); 928 } 929 930 /* 931 * Remove a directory entry. At this point the file represented by the 932 * directory entry to be removed is still full length until no one has it 933 * open. When the file no longer being used msdosfs_inactive() is called 934 * and will truncate the file to 0 length. When the vnode containing the 935 * denode is needed for some other purpose by VFS it will call 936 * msdosfs_reclaim() which will remove the denode from the denode cache. 937 * 938 * pdep directory where the entry is removed 939 * dep file to be removed 940 */ 941 int 942 removede(struct denode *pdep, struct denode *dep) 943 { 944 int error; 945 struct direntry *ep; 946 struct buf *bp; 947 daddr_t bn; 948 int blsize; 949 struct msdosfsmount *pmp = pdep->de_pmp; 950 u_long offset = pdep->de_fndoffset; 951 952 #ifdef MSDOSFS_DEBUG 953 printf("removede(): filename %s, dep %p, offset %08lx\n", 954 dep->de_Name, dep, offset); 955 #endif 956 957 dep->de_refcnt--; 958 offset += sizeof(struct direntry); 959 do { 960 offset -= sizeof(struct direntry); 961 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize); 962 if (error) 963 return error; 964 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 965 if (error) { 966 return error; 967 } 968 ep = bptoep(pmp, bp, offset); 969 /* 970 * Check whether, if we came here the second time, i.e. 971 * when underflowing into the previous block, the last 972 * entry in this block is a longfilename entry, too. 973 */ 974 if (ep->deAttributes != ATTR_WIN95 975 && offset != pdep->de_fndoffset) { 976 brelse(bp); 977 break; 978 } 979 offset += sizeof(struct direntry); 980 while (1) { 981 /* 982 * We are a bit aggressive here in that we delete any Win95 983 * entries preceding this entry, not just the ones we "own". 984 * Since these presumably aren't valid anyway, 985 * there should be no harm. 986 */ 987 offset -= sizeof(struct direntry); 988 ep--->deName[0] = SLOT_DELETED; 989 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) 990 || !(offset & pmp->pm_crbomask) 991 || ep->deAttributes != ATTR_WIN95) 992 break; 993 } 994 if (DOINGASYNC(DETOV(pdep))) 995 bdwrite(bp); 996 else if ((error = bwrite(bp)) != 0) 997 return error; 998 } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95) 999 && !(offset & pmp->pm_crbomask) 1000 && offset); 1001 return 0; 1002 } 1003 1004 /* 1005 * Create a unique DOS name in dvp 1006 */ 1007 int 1008 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 1009 { 1010 struct msdosfsmount *pmp = dep->de_pmp; 1011 struct direntry *dentp; 1012 int gen; 1013 int blsize; 1014 u_long cn; 1015 daddr_t bn; 1016 struct buf *bp; 1017 int error; 1018 1019 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 1020 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 1021 cnp->cn_namelen, 0, pmp) ? 0 : EINVAL); 1022 1023 for (gen = 1;; gen++) { 1024 /* 1025 * Generate DOS name with generation number 1026 */ 1027 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 1028 cnp->cn_namelen, gen, pmp)) 1029 return gen == 1 ? EINVAL : EEXIST; 1030 1031 /* 1032 * Now look for a dir entry with this exact name 1033 */ 1034 for (cn = error = 0; !error; cn++) { 1035 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 1036 if (error == E2BIG) /* EOF reached and not found */ 1037 return 0; 1038 return error; 1039 } 1040 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 1041 if (error) { 1042 return error; 1043 } 1044 for (dentp = (struct direntry *)bp->b_data; 1045 (char *)dentp < bp->b_data + blsize; 1046 dentp++) { 1047 if (dentp->deName[0] == SLOT_EMPTY) { 1048 /* 1049 * Last used entry and not found 1050 */ 1051 brelse(bp); 1052 return 0; 1053 } 1054 /* 1055 * Ignore volume labels and Win95 entries 1056 */ 1057 if (dentp->deAttributes & ATTR_VOLUME) 1058 continue; 1059 if (!bcmp(dentp->deName, cp, 11)) { 1060 error = EEXIST; 1061 break; 1062 } 1063 } 1064 brelse(bp); 1065 } 1066 } 1067 } 1068