1 /* $OpenBSD: msdosfs_denode.c,v 1.16 1999/04/28 09:28:16 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 timespec ts; 344 345 TIMEVAL_TO_TIMESPEC(&time, &ts); 346 return (VOP_UPDATE(DETOV(dep), &ts, &ts, waitfor)); 347 } 348 349 /* 350 * Truncate the file described by dep to the length specified by length. 351 */ 352 int 353 detrunc(dep, length, flags, cred, p) 354 struct denode *dep; 355 u_long length; 356 int flags; 357 struct ucred *cred; 358 struct proc *p; 359 { 360 int error; 361 int allerror; 362 int vflags; 363 u_long eofentry; 364 u_long chaintofree; 365 daddr_t bn; 366 int boff; 367 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 368 struct buf *bp; 369 struct msdosfsmount *pmp = dep->de_pmp; 370 371 #ifdef MSDOSFS_DEBUG 372 printf("detrunc(): file %s, length %ld, flags %d\n", dep->de_Name, length, flags); 373 #endif 374 375 /* 376 * Disallow attempts to truncate the root directory since it is of 377 * fixed size. That's just the way dos filesystems are. We use 378 * the VROOT bit in the vnode because checking for the directory 379 * bit and a startcluster of 0 in the denode is not adequate to 380 * recognize the root directory at this point in a file or 381 * directory's life. 382 */ 383 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { 384 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 385 dep->de_dirclust, dep->de_diroffset); 386 return (EINVAL); 387 } 388 389 #if defined(UVM) 390 uvm_vnp_setsize(DETOV(dep), length); 391 #else 392 vnode_pager_setsize(DETOV(dep), length); 393 #endif 394 395 if (dep->de_FileSize < length) 396 return (deextend(dep, length, cred)); 397 398 /* 399 * If the desired length is 0 then remember the starting cluster of 400 * the file and set the StartCluster field in the directory entry 401 * to 0. If the desired length is not zero, then get the number of 402 * the last cluster in the shortened file. Then get the number of 403 * the first cluster in the part of the file that is to be freed. 404 * Then set the next cluster pointer in the last cluster of the 405 * file to CLUST_EOFE. 406 */ 407 if (length == 0) { 408 chaintofree = dep->de_StartCluster; 409 dep->de_StartCluster = 0; 410 eofentry = ~0; 411 } else { 412 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 413 &eofentry, 0); 414 if (error) { 415 #ifdef MSDOSFS_DEBUG 416 printf("detrunc(): pcbmap fails %d\n", error); 417 #endif 418 return (error); 419 } 420 } 421 422 fc_purge(dep, de_clcount(pmp, length)); 423 424 /* 425 * If the new length is not a multiple of the cluster size then we 426 * must zero the tail end of the new last cluster in case it 427 * becomes part of the file again because of a seek. 428 */ 429 if ((boff = length & pmp->pm_crbomask) != 0) { 430 if (isadir) { 431 bn = cntobn(pmp, eofentry); 432 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 433 NOCRED, &bp); 434 } else { 435 bn = de_blk(pmp, length); 436 error = bread(DETOV(dep), bn, pmp->pm_bpcluster, 437 NOCRED, &bp); 438 } 439 if (error) { 440 brelse(bp); 441 #ifdef MSDOSFS_DEBUG 442 printf("detrunc(): bread fails %d\n", error); 443 #endif 444 return (error); 445 } 446 #if defined(UVM) 447 uvm_vnp_uncache(DETOV(dep)); 448 #else 449 vnode_pager_uncache(DETOV(dep)); /* what's this for? */ 450 #endif 451 /* 452 * is this the right place for it? 453 */ 454 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 455 if (flags & IO_SYNC) 456 bwrite(bp); 457 else 458 bdwrite(bp); 459 } 460 461 /* 462 * Write out the updated directory entry. Even if the update fails 463 * we free the trailing clusters. 464 */ 465 dep->de_FileSize = length; 466 if (!isadir) 467 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 468 vflags = (length > 0 ? V_SAVE : 0) | V_SAVEMETA; 469 vinvalbuf(DETOV(dep), vflags, cred, p, 0, 0); 470 allerror = deupdat(dep, 1); 471 #ifdef MSDOSFS_DEBUG 472 printf("detrunc(): allerror %d, eofentry %d\n", 473 allerror, eofentry); 474 #endif 475 476 /* 477 * If we need to break the cluster chain for the file then do it 478 * now. 479 */ 480 if (eofentry != ~0) { 481 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 482 &chaintofree, CLUST_EOFE); 483 if (error) { 484 #ifdef MSDOSFS_DEBUG 485 printf("detrunc(): fatentry errors %d\n", error); 486 #endif 487 return (error); 488 } 489 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 490 eofentry); 491 } 492 493 /* 494 * Now free the clusters removed from the file because of the 495 * truncation. 496 */ 497 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 498 freeclusterchain(pmp, chaintofree); 499 500 return (allerror); 501 } 502 503 /* 504 * Extend the file described by dep to length specified by length. 505 */ 506 int 507 deextend(dep, length, cred) 508 struct denode *dep; 509 u_long length; 510 struct ucred *cred; 511 { 512 struct msdosfsmount *pmp = dep->de_pmp; 513 u_long count; 514 int error; 515 516 /* 517 * The root of a DOS filesystem cannot be extended. 518 */ 519 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) 520 return (EINVAL); 521 522 /* 523 * Directories cannot be extended. 524 */ 525 if (dep->de_Attributes & ATTR_DIRECTORY) 526 return (EISDIR); 527 528 if (length <= dep->de_FileSize) 529 panic("deextend: file too large"); 530 531 /* 532 * Compute the number of clusters to allocate. 533 */ 534 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 535 if (count > 0) { 536 if (count > pmp->pm_freeclustercount) 537 return (ENOSPC); 538 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 539 if (error) { 540 /* truncate the added clusters away again */ 541 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 542 return (error); 543 } 544 } 545 546 dep->de_FileSize = length; 547 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 548 return (deupdat(dep, 1)); 549 } 550 551 /* 552 * Move a denode to its correct hash queue after the file it represents has 553 * been moved to a new directory. 554 */ 555 void 556 reinsert(dep) 557 struct denode *dep; 558 { 559 /* 560 * Fix up the denode cache. If the denode is for a directory, 561 * there is nothing to do since the hash is based on the starting 562 * cluster of the directory file and that hasn't changed. If for a 563 * file the hash is based on the location of the directory entry, 564 * so we must remove it from the cache and re-enter it with the 565 * hash based on the new location of the directory entry. 566 */ 567 if (dep->de_Attributes & ATTR_DIRECTORY) 568 return; 569 msdosfs_hashrem(dep); 570 msdosfs_hashins(dep); 571 } 572 573 int 574 msdosfs_reclaim(v) 575 void *v; 576 { 577 struct vop_reclaim_args /* { 578 struct vnode *a_vp; 579 } */ *ap = v; 580 struct vnode *vp = ap->a_vp; 581 struct denode *dep = VTODE(vp); 582 extern int prtactive; 583 584 #ifdef MSDOSFS_DEBUG 585 printf("msdosfs_reclaim(): dep %08x, file %s, refcnt %d\n", 586 dep, dep->de_Name, dep->de_refcnt); 587 #endif 588 589 if (prtactive && vp->v_usecount != 0) 590 vprint("msdosfs_reclaim(): pushing active", vp); 591 /* 592 * Remove the denode from its hash chain. 593 */ 594 msdosfs_hashrem(dep); 595 /* 596 * Purge old data structures associated with the denode. 597 */ 598 cache_purge(vp); 599 if (dep->de_devvp) { 600 vrele(dep->de_devvp); 601 dep->de_devvp = 0; 602 } 603 #if 0 /* XXX */ 604 dep->de_flag = 0; 605 #endif 606 FREE(dep, M_MSDOSFSNODE); 607 vp->v_data = NULL; 608 return (0); 609 } 610 611 int 612 msdosfs_inactive(v) 613 void *v; 614 { 615 struct vop_inactive_args /* { 616 struct vnode *a_vp; 617 struct proc *a_p; 618 } */ *ap = v; 619 struct vnode *vp = ap->a_vp; 620 struct denode *dep = VTODE(vp); 621 struct proc *p = ap->a_p; 622 int error; 623 extern int prtactive; 624 625 #ifdef MSDOSFS_DEBUG 626 printf("msdosfs_inactive(): dep %08x, de_Name[0] %x\n", dep, dep->de_Name[0]); 627 #endif 628 629 if (prtactive && vp->v_usecount != 0) 630 vprint("msdosfs_inactive(): pushing active", vp); 631 632 error = 0; 633 634 /* 635 * Get rid of denodes related to stale file handles. 636 */ 637 if (dep->de_Name[0] == SLOT_DELETED) 638 goto out; 639 640 /* 641 * If the file has been deleted and it is on a read/write 642 * filesystem, then truncate the file, and mark the directory slot 643 * as empty. (This may not be necessary for the dos filesystem.) 644 */ 645 #ifdef MSDOSFS_DEBUG 646 printf("msdosfs_inactive(): dep %08x, refcnt %d, mntflag %x, MNT_RDONLY %x\n", 647 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 648 #endif 649 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 650 error = detrunc(dep, (u_long)0, 0, NOCRED, NULL); 651 dep->de_Name[0] = SLOT_DELETED; 652 } 653 deupdat(dep, 0); 654 655 out: 656 VOP_UNLOCK(vp, 0, p); 657 /* 658 * If we are done with the denode, reclaim it 659 * so that it can be reused immediately. 660 */ 661 #ifdef MSDOSFS_DEBUG 662 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount, 663 dep->de_Name[0]); 664 #endif 665 if (dep->de_Name[0] == SLOT_DELETED) 666 vrecycle(vp, (struct simplelock *)0, p); 667 return (error); 668 } 669