1 /* $OpenBSD: msdosfs_denode.c,v 1.18 2001/06/27 04:58:45 art Exp $ */ 2 /* $NetBSD: msdosfs_denode.c,v 1.23 1997/10/17 11:23:58 ws Exp $ */ 3 4 /*- 5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 /* 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/mount.h> 54 #include <sys/malloc.h> 55 #include <sys/proc.h> 56 #include <sys/buf.h> 57 #include <sys/vnode.h> 58 #include <sys/kernel.h> /* defines "time" */ 59 #include <sys/dirent.h> 60 #include <sys/namei.h> 61 62 #include <vm/vm.h> 63 64 #include <msdosfs/bpb.h> 65 #include <msdosfs/msdosfsmount.h> 66 #include <msdosfs/direntry.h> 67 #include <msdosfs/denode.h> 68 #include <msdosfs/fat.h> 69 70 struct denode **dehashtbl; 71 u_long dehash; /* size of hash table - 1 */ 72 #define DEHASH(dev, dcl, doff) (((dev) + (dcl) + (doff) / sizeof(struct direntry)) \ 73 & dehash) 74 75 static struct denode *msdosfs_hashget __P((dev_t, u_long, u_long)); 76 static int msdosfs_hashins __P((struct denode *)); 77 static void msdosfs_hashrem __P((struct denode *)); 78 79 /*ARGSUSED*/ 80 int 81 msdosfs_init(vfsp) 82 struct vfsconf *vfsp; 83 { 84 dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, M_WAITOK, &dehash); 85 return (0); 86 } 87 88 static struct denode * 89 msdosfs_hashget(dev, dirclust, diroff) 90 dev_t dev; 91 u_long dirclust; 92 u_long diroff; 93 { 94 struct denode *dep; 95 struct proc *p = curproc; /* XXX */ 96 97 for (;;) 98 for (dep = dehashtbl[DEHASH(dev, dirclust, diroff)];; 99 dep = dep->de_next) { 100 if (dep == NULL) 101 return (NULL); 102 if (dirclust == dep->de_dirclust && 103 diroff == dep->de_diroffset && 104 dev == dep->de_dev && 105 dep->de_refcnt != 0) { 106 struct vnode *vp = DETOV(dep); 107 108 simple_lock(&vp->v_interlock); 109 if (!vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) 110 return (dep); 111 break; 112 } 113 } 114 /* NOTREACHED */ 115 } 116 117 static int 118 msdosfs_hashins(dep) 119 struct denode *dep; 120 { 121 struct denode **depp, *deq; 122 123 depp = &dehashtbl[DEHASH(dep->de_dev, dep->de_dirclust, 124 dep->de_diroffset)]; 125 126 for (deq = *depp; deq; deq = deq->de_next) { 127 if (dep->de_dirclust == deq->de_dirclust && 128 dep->de_diroffset == deq->de_diroffset && 129 dep->de_dev == deq->de_dev && 130 deq->de_refcnt != 0) { 131 return (EEXIST); 132 } 133 } 134 135 if ((deq = *depp) != NULL) 136 deq->de_prev = &dep->de_next; 137 dep->de_next = deq; 138 dep->de_prev = depp; 139 *depp = dep; 140 return (0); 141 } 142 143 static void 144 msdosfs_hashrem(dep) 145 struct denode *dep; 146 { 147 struct denode *deq; 148 149 if (dep->de_prev == NULL) 150 return; 151 152 if ((deq = dep->de_next) != NULL) 153 deq->de_prev = dep->de_prev; 154 *dep->de_prev = deq; 155 #ifdef DIAGNOSTIC 156 dep->de_next = NULL; 157 dep->de_prev = NULL; 158 #endif 159 } 160 161 /* 162 * If deget() succeeds it returns with the gotten denode locked(). 163 * 164 * pmp - address of msdosfsmount structure of the filesystem containing 165 * the denode of interest. The pm_dev field and the address of 166 * the msdosfsmount structure are used. 167 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 168 * diroffset is relative to the beginning of the root directory, 169 * otherwise it is cluster relative. 170 * diroffset - offset past begin of cluster of denode we want 171 * depp - returns the address of the gotten denode. 172 */ 173 int 174 deget(pmp, dirclust, diroffset, depp) 175 struct msdosfsmount *pmp; /* so we know the maj/min number */ 176 u_long dirclust; /* cluster this dir entry came from */ 177 u_long diroffset; /* index of entry within the cluster */ 178 struct denode **depp; /* returns the addr of the gotten denode */ 179 { 180 int error; 181 extern int (**msdosfs_vnodeop_p) __P((void *)); 182 struct direntry *direntptr; 183 struct denode *ldep; 184 struct vnode *nvp; 185 struct buf *bp; 186 struct proc *p = curproc; /* XXX */ 187 188 #ifdef MSDOSFS_DEBUG 189 printf("deget(pmp %08x, dirclust %d, diroffset %x, depp %08x)\n", 190 pmp, dirclust, diroffset, depp); 191 #endif 192 193 /* 194 * On FAT32 filesystems, root is a (more or less) normal 195 * directory 196 */ 197 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 198 dirclust = pmp->pm_rootdirblk; 199 200 /* 201 * See if the denode is in the denode cache. Use the location of 202 * the directory entry to compute the hash value. For subdir use 203 * address of "." entry. For root dir (if not FAT32) use cluster 204 * MSDOSFSROOT, offset MSDOSFSROOT_OFS 205 * 206 * NOTE: The check for de_refcnt > 0 below insures the denode being 207 * examined does not represent an unlinked but still open file. 208 * These files are not to be accessible even when the directory 209 * entry that represented the file happens to be reused while the 210 * deleted file is still open. 211 */ 212 retry: 213 ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset); 214 if (ldep) { 215 *depp = ldep; 216 return (0); 217 } 218 219 /* 220 * Directory entry was not in cache, have to create a vnode and 221 * copy it from the passed disk buffer. 222 */ 223 /* getnewvnode() does a VREF() on the vnode */ 224 error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp, 225 msdosfs_vnodeop_p, &nvp); 226 if (error) { 227 *depp = 0; 228 return (error); 229 } 230 MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, 231 M_WAITOK); 232 bzero((caddr_t)ldep, sizeof *ldep); 233 lockinit(&ldep->de_lock, PINOD, "denode", 0, 0); 234 nvp->v_data = ldep; 235 ldep->de_vnode = nvp; 236 ldep->de_flag = 0; 237 ldep->de_devvp = 0; 238 ldep->de_lockf = 0; 239 ldep->de_dev = pmp->pm_dev; 240 ldep->de_dirclust = dirclust; 241 ldep->de_diroffset = diroffset; 242 fc_purge(ldep, 0); /* init the fat cache for this denode */ 243 244 /* 245 * Insert the denode into the hash queue and lock the denode so it 246 * can't be accessed until we've read it in and have done what we 247 * need to it. 248 */ 249 vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY, p); 250 error = msdosfs_hashins(ldep); 251 252 if (error) { 253 vput (nvp); 254 255 if (error == EEXIST) 256 goto retry; 257 258 return (error); 259 } 260 261 ldep->de_pmp = pmp; 262 ldep->de_devvp = pmp->pm_devvp; 263 ldep->de_refcnt = 1; 264 /* 265 * Copy the directory entry into the denode area of the vnode. 266 */ 267 if ((dirclust == MSDOSFSROOT 268 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 269 && diroffset == MSDOSFSROOT_OFS) { 270 /* 271 * Directory entry for the root directory. There isn't one, 272 * so we manufacture one. We should probably rummage 273 * through the root directory and find a label entry (if it 274 * exists), and then use the time and date from that entry 275 * as the time and date for the root denode. 276 */ 277 nvp->v_flag |= VROOT; /* should be further down XXX */ 278 279 ldep->de_Attributes = ATTR_DIRECTORY; 280 if (FAT32(pmp)) 281 ldep->de_StartCluster = pmp->pm_rootdirblk; 282 /* de_FileSize will be filled in further down */ 283 else { 284 ldep->de_StartCluster = MSDOSFSROOT; 285 ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec; 286 } 287 /* 288 * fill in time and date so that dos2unixtime() doesn't 289 * spit up when called from msdosfs_getattr() with root 290 * denode 291 */ 292 ldep->de_CTime = 0x0000; /* 00:00:00 */ 293 ldep->de_CTimeHundredth = 0; 294 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 295 | (1 << DD_DAY_SHIFT); 296 /* Jan 1, 1980 */ 297 ldep->de_ADate = ldep->de_CDate; 298 ldep->de_MTime = ldep->de_CTime; 299 ldep->de_MDate = ldep->de_CDate; 300 /* leave the other fields as garbage */ 301 } else { 302 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 303 if (error) 304 return (error); 305 DE_INTERNALIZE(ldep, direntptr); 306 brelse(bp); 307 } 308 309 /* 310 * Fill in a few fields of the vnode and finish filling in the 311 * denode. Then return the address of the found denode. 312 */ 313 if (ldep->de_Attributes & ATTR_DIRECTORY) { 314 /* 315 * Since DOS directory entries that describe directories 316 * have 0 in the filesize field, we take this opportunity 317 * to find out the length of the directory and plug it into 318 * the denode structure. 319 */ 320 u_long size; 321 322 nvp->v_type = VDIR; 323 if (ldep->de_StartCluster != MSDOSFSROOT) { 324 error = pcbmap(ldep, 0xffff, 0, &size, 0); 325 if (error == E2BIG) { 326 ldep->de_FileSize = de_cn2off(pmp, size); 327 error = 0; 328 } else 329 printf("deget(): pcbmap returned %d\n", error); 330 } 331 } else 332 nvp->v_type = VREG; 333 VREF(ldep->de_devvp); 334 *depp = ldep; 335 return (0); 336 } 337 338 int 339 deupdat(dep, waitfor) 340 struct denode *dep; 341 int waitfor; 342 { 343 struct buf *bp; 344 struct direntry *dirp; 345 int error; 346 struct timespec ts; 347 348 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 349 return (0); 350 TIMEVAL_TO_TIMESPEC(&time, &ts); 351 DETIMES(dep, &ts, &ts, &ts); 352 if ((dep->de_flag & DE_MODIFIED) == 0) 353 return (0); 354 dep->de_flag &= ~DE_MODIFIED; 355 if (dep->de_Attributes & ATTR_DIRECTORY) 356 return (0); 357 if (dep->de_refcnt <= 0) 358 return (0); 359 error = readde(dep, &bp, &dirp); 360 if (error) 361 return (error); 362 DE_EXTERNALIZE(dirp, dep); 363 if (waitfor) 364 return (bwrite(bp)); 365 else { 366 bdwrite(bp); 367 return (0); 368 } 369 } 370 371 /* 372 * Truncate the file described by dep to the length specified by length. 373 */ 374 int 375 detrunc(dep, length, flags, cred, p) 376 struct denode *dep; 377 u_long length; 378 int flags; 379 struct ucred *cred; 380 struct proc *p; 381 { 382 int error; 383 int allerror; 384 int vflags; 385 u_long eofentry; 386 u_long chaintofree; 387 daddr_t bn; 388 int boff; 389 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 390 struct buf *bp; 391 struct msdosfsmount *pmp = dep->de_pmp; 392 393 #ifdef MSDOSFS_DEBUG 394 printf("detrunc(): file %s, length %ld, flags %d\n", dep->de_Name, length, flags); 395 #endif 396 397 /* 398 * Disallow attempts to truncate the root directory since it is of 399 * fixed size. That's just the way dos filesystems are. We use 400 * the VROOT bit in the vnode because checking for the directory 401 * bit and a startcluster of 0 in the denode is not adequate to 402 * recognize the root directory at this point in a file or 403 * directory's life. 404 */ 405 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { 406 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 407 dep->de_dirclust, dep->de_diroffset); 408 return (EINVAL); 409 } 410 411 uvm_vnp_setsize(DETOV(dep), length); 412 413 if (dep->de_FileSize < length) 414 return (deextend(dep, length, cred)); 415 416 /* 417 * If the desired length is 0 then remember the starting cluster of 418 * the file and set the StartCluster field in the directory entry 419 * to 0. If the desired length is not zero, then get the number of 420 * the last cluster in the shortened file. Then get the number of 421 * the first cluster in the part of the file that is to be freed. 422 * Then set the next cluster pointer in the last cluster of the 423 * file to CLUST_EOFE. 424 */ 425 if (length == 0) { 426 chaintofree = dep->de_StartCluster; 427 dep->de_StartCluster = 0; 428 eofentry = ~0; 429 } else { 430 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 431 &eofentry, 0); 432 if (error) { 433 #ifdef MSDOSFS_DEBUG 434 printf("detrunc(): pcbmap fails %d\n", error); 435 #endif 436 return (error); 437 } 438 } 439 440 fc_purge(dep, de_clcount(pmp, length)); 441 442 /* 443 * If the new length is not a multiple of the cluster size then we 444 * must zero the tail end of the new last cluster in case it 445 * becomes part of the file again because of a seek. 446 */ 447 if ((boff = length & pmp->pm_crbomask) != 0) { 448 if (isadir) { 449 bn = cntobn(pmp, eofentry); 450 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 451 NOCRED, &bp); 452 } else { 453 bn = de_blk(pmp, length); 454 error = bread(DETOV(dep), bn, pmp->pm_bpcluster, 455 NOCRED, &bp); 456 } 457 if (error) { 458 brelse(bp); 459 #ifdef MSDOSFS_DEBUG 460 printf("detrunc(): bread fails %d\n", error); 461 #endif 462 return (error); 463 } 464 uvm_vnp_uncache(DETOV(dep)); 465 /* 466 * is this the right place for it? 467 */ 468 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 469 if (flags & IO_SYNC) 470 bwrite(bp); 471 else 472 bdwrite(bp); 473 } 474 475 /* 476 * Write out the updated directory entry. Even if the update fails 477 * we free the trailing clusters. 478 */ 479 dep->de_FileSize = length; 480 if (!isadir) 481 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 482 vflags = (length > 0 ? V_SAVE : 0) | V_SAVEMETA; 483 vinvalbuf(DETOV(dep), vflags, cred, p, 0, 0); 484 allerror = deupdat(dep, 1); 485 #ifdef MSDOSFS_DEBUG 486 printf("detrunc(): allerror %d, eofentry %d\n", 487 allerror, eofentry); 488 #endif 489 490 /* 491 * If we need to break the cluster chain for the file then do it 492 * now. 493 */ 494 if (eofentry != ~0) { 495 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 496 &chaintofree, CLUST_EOFE); 497 if (error) { 498 #ifdef MSDOSFS_DEBUG 499 printf("detrunc(): fatentry errors %d\n", error); 500 #endif 501 return (error); 502 } 503 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 504 eofentry); 505 } 506 507 /* 508 * Now free the clusters removed from the file because of the 509 * truncation. 510 */ 511 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 512 freeclusterchain(pmp, chaintofree); 513 514 return (allerror); 515 } 516 517 /* 518 * Extend the file described by dep to length specified by length. 519 */ 520 int 521 deextend(dep, length, cred) 522 struct denode *dep; 523 u_long length; 524 struct ucred *cred; 525 { 526 struct msdosfsmount *pmp = dep->de_pmp; 527 u_long count; 528 int error; 529 530 /* 531 * The root of a DOS filesystem cannot be extended. 532 */ 533 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) 534 return (EINVAL); 535 536 /* 537 * Directories cannot be extended. 538 */ 539 if (dep->de_Attributes & ATTR_DIRECTORY) 540 return (EISDIR); 541 542 if (length <= dep->de_FileSize) 543 panic("deextend: file too large"); 544 545 /* 546 * Compute the number of clusters to allocate. 547 */ 548 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 549 if (count > 0) { 550 if (count > pmp->pm_freeclustercount) 551 return (ENOSPC); 552 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 553 if (error) { 554 /* truncate the added clusters away again */ 555 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 556 return (error); 557 } 558 } 559 560 dep->de_FileSize = length; 561 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 562 return (deupdat(dep, 1)); 563 } 564 565 /* 566 * Move a denode to its correct hash queue after the file it represents has 567 * been moved to a new directory. 568 */ 569 void 570 reinsert(dep) 571 struct denode *dep; 572 { 573 /* 574 * Fix up the denode cache. If the denode is for a directory, 575 * there is nothing to do since the hash is based on the starting 576 * cluster of the directory file and that hasn't changed. If for a 577 * file the hash is based on the location of the directory entry, 578 * so we must remove it from the cache and re-enter it with the 579 * hash based on the new location of the directory entry. 580 */ 581 if (dep->de_Attributes & ATTR_DIRECTORY) 582 return; 583 msdosfs_hashrem(dep); 584 msdosfs_hashins(dep); 585 } 586 587 int 588 msdosfs_reclaim(v) 589 void *v; 590 { 591 struct vop_reclaim_args /* { 592 struct vnode *a_vp; 593 } */ *ap = v; 594 struct vnode *vp = ap->a_vp; 595 struct denode *dep = VTODE(vp); 596 extern int prtactive; 597 598 #ifdef MSDOSFS_DEBUG 599 printf("msdosfs_reclaim(): dep %08x, file %s, refcnt %d\n", 600 dep, dep->de_Name, dep->de_refcnt); 601 #endif 602 603 if (prtactive && vp->v_usecount != 0) 604 vprint("msdosfs_reclaim(): pushing active", vp); 605 /* 606 * Remove the denode from its hash chain. 607 */ 608 msdosfs_hashrem(dep); 609 /* 610 * Purge old data structures associated with the denode. 611 */ 612 cache_purge(vp); 613 if (dep->de_devvp) { 614 vrele(dep->de_devvp); 615 dep->de_devvp = 0; 616 } 617 #if 0 /* XXX */ 618 dep->de_flag = 0; 619 #endif 620 FREE(dep, M_MSDOSFSNODE); 621 vp->v_data = NULL; 622 return (0); 623 } 624 625 int 626 msdosfs_inactive(v) 627 void *v; 628 { 629 struct vop_inactive_args /* { 630 struct vnode *a_vp; 631 struct proc *a_p; 632 } */ *ap = v; 633 struct vnode *vp = ap->a_vp; 634 struct denode *dep = VTODE(vp); 635 struct proc *p = ap->a_p; 636 int error; 637 extern int prtactive; 638 639 #ifdef MSDOSFS_DEBUG 640 printf("msdosfs_inactive(): dep %08x, de_Name[0] %x\n", dep, dep->de_Name[0]); 641 #endif 642 643 if (prtactive && vp->v_usecount != 0) 644 vprint("msdosfs_inactive(): pushing active", vp); 645 646 error = 0; 647 648 /* 649 * Get rid of denodes related to stale file handles. 650 */ 651 if (dep->de_Name[0] == SLOT_DELETED) 652 goto out; 653 654 /* 655 * If the file has been deleted and it is on a read/write 656 * filesystem, then truncate the file, and mark the directory slot 657 * as empty. (This may not be necessary for the dos filesystem.) 658 */ 659 #ifdef MSDOSFS_DEBUG 660 printf("msdosfs_inactive(): dep %08x, refcnt %d, mntflag %x, MNT_RDONLY %x\n", 661 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 662 #endif 663 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 664 error = detrunc(dep, (u_long)0, 0, NOCRED, NULL); 665 dep->de_Name[0] = SLOT_DELETED; 666 } 667 deupdat(dep, 0); 668 669 out: 670 VOP_UNLOCK(vp, 0, p); 671 /* 672 * If we are done with the denode, reclaim it 673 * so that it can be reused immediately. 674 */ 675 #ifdef MSDOSFS_DEBUG 676 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount, 677 dep->de_Name[0]); 678 #endif 679 if (dep->de_Name[0] == SLOT_DELETED) 680 vrecycle(vp, (struct simplelock *)0, p); 681 return (error); 682 } 683