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