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