1 /* $OpenBSD: msdosfs_denode.c,v 1.37 2009/08/13 22:34:29 jasper 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 <uvm/uvm_extern.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(dev_t, uint32_t, uint32_t); 76 static int msdosfs_hashins(struct denode *); 77 static void msdosfs_hashrem(struct denode *); 78 79 /*ARGSUSED*/ 80 int 81 msdosfs_init(struct vfsconf *vfsp) 82 { 83 dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, M_WAITOK, &dehash); 84 return (0); 85 } 86 87 static struct denode * 88 msdosfs_hashget(dev_t dev, uint32_t dirclust, uint32_t diroff) 89 { 90 struct denode *dep; 91 struct proc *p = curproc; /* XXX */ 92 93 for (;;) 94 for (dep = dehashtbl[DEHASH(dev, dirclust, diroff)];; 95 dep = dep->de_next) { 96 if (dep == NULL) 97 return (NULL); 98 if (dirclust == dep->de_dirclust && 99 diroff == dep->de_diroffset && 100 dev == dep->de_dev && 101 dep->de_refcnt != 0) { 102 struct vnode *vp = DETOV(dep); 103 104 if (!vget(vp, LK_EXCLUSIVE, p)) 105 return (dep); 106 break; 107 } 108 } 109 /* NOTREACHED */ 110 } 111 112 static int 113 msdosfs_hashins(struct denode *dep) 114 { 115 struct denode **depp, *deq; 116 117 depp = &dehashtbl[DEHASH(dep->de_dev, dep->de_dirclust, 118 dep->de_diroffset)]; 119 120 for (deq = *depp; deq; deq = deq->de_next) { 121 if (dep->de_dirclust == deq->de_dirclust && 122 dep->de_diroffset == deq->de_diroffset && 123 dep->de_dev == deq->de_dev && 124 deq->de_refcnt != 0) { 125 return (EEXIST); 126 } 127 } 128 129 if ((deq = *depp) != NULL) 130 deq->de_prev = &dep->de_next; 131 dep->de_next = deq; 132 dep->de_prev = depp; 133 *depp = dep; 134 return (0); 135 } 136 137 static void 138 msdosfs_hashrem(struct denode *dep) 139 { 140 struct denode *deq; 141 142 if (dep->de_prev == NULL) 143 return; 144 145 if ((deq = dep->de_next) != NULL) 146 deq->de_prev = dep->de_prev; 147 *dep->de_prev = deq; 148 #ifdef DIAGNOSTIC 149 dep->de_next = NULL; 150 dep->de_prev = NULL; 151 #endif 152 } 153 154 /* 155 * If deget() succeeds it returns with the gotten denode locked(). 156 * 157 * pmp - address of msdosfsmount structure of the filesystem containing 158 * the denode of interest. The pm_dev field and the address of 159 * the msdosfsmount structure are used. 160 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 161 * diroffset is relative to the beginning of the root directory, 162 * otherwise it is cluster relative. 163 * diroffset - offset past begin of cluster of denode we want 164 * depp - returns the address of the gotten denode. 165 */ 166 int 167 deget(struct msdosfsmount *pmp, uint32_t dirclust, uint32_t diroffset, 168 struct denode **depp) 169 { 170 int error; 171 extern int (**msdosfs_vnodeop_p)(void *); 172 struct direntry *direntptr; 173 struct denode *ldep; 174 struct vnode *nvp; 175 struct buf *bp; 176 struct proc *p = curproc; /* XXX */ 177 178 #ifdef MSDOSFS_DEBUG 179 printf("deget(pmp %08x, dirclust %d, diroffset %x, depp %08x)\n", 180 pmp, dirclust, diroffset, depp); 181 #endif 182 183 /* 184 * On FAT32 filesystems, root is a (more or less) normal 185 * directory 186 */ 187 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 188 dirclust = pmp->pm_rootdirblk; 189 190 /* 191 * See if the denode is in the denode cache. Use the location of 192 * the directory entry to compute the hash value. For subdir use 193 * address of "." entry. For root dir (if not FAT32) use cluster 194 * MSDOSFSROOT, offset MSDOSFSROOT_OFS 195 * 196 * NOTE: The check for de_refcnt > 0 below insures the denode being 197 * examined does not represent an unlinked but still open file. 198 * These files are not to be accessible even when the directory 199 * entry that represented the file happens to be reused while the 200 * deleted file is still open. 201 */ 202 retry: 203 ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset); 204 if (ldep) { 205 *depp = ldep; 206 return (0); 207 } 208 209 /* 210 * Directory entry was not in cache, have to create a vnode and 211 * copy it from the passed disk buffer. 212 */ 213 /* getnewvnode() does a vref() on the vnode */ 214 error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp, 215 msdosfs_vnodeop_p, &nvp); 216 if (error) { 217 *depp = 0; 218 return (error); 219 } 220 ldep = malloc(sizeof(*ldep), M_MSDOSFSNODE, M_WAITOK | M_ZERO); 221 lockinit(&ldep->de_lock, PINOD, "denode", 0, 0); 222 nvp->v_data = ldep; 223 ldep->de_vnode = nvp; 224 ldep->de_flag = 0; 225 ldep->de_devvp = 0; 226 ldep->de_lockf = 0; 227 ldep->de_dev = pmp->pm_dev; 228 ldep->de_dirclust = dirclust; 229 ldep->de_diroffset = diroffset; 230 fc_purge(ldep, 0); /* init the fat cache for this denode */ 231 232 /* 233 * Insert the denode into the hash queue and lock the denode so it 234 * can't be accessed until we've read it in and have done what we 235 * need to it. 236 */ 237 vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY, p); 238 error = msdosfs_hashins(ldep); 239 240 if (error) { 241 vput (nvp); 242 243 if (error == EEXIST) 244 goto retry; 245 246 return (error); 247 } 248 249 ldep->de_pmp = pmp; 250 ldep->de_devvp = pmp->pm_devvp; 251 ldep->de_refcnt = 1; 252 /* 253 * Copy the directory entry into the denode area of the vnode. 254 */ 255 if ((dirclust == MSDOSFSROOT 256 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 257 && diroffset == MSDOSFSROOT_OFS) { 258 /* 259 * Directory entry for the root directory. There isn't one, 260 * so we manufacture one. We should probably rummage 261 * through the root directory and find a label entry (if it 262 * exists), and then use the time and date from that entry 263 * as the time and date for the root denode. 264 */ 265 nvp->v_flag |= VROOT; /* should be further down XXX */ 266 267 ldep->de_Attributes = ATTR_DIRECTORY; 268 if (FAT32(pmp)) 269 ldep->de_StartCluster = pmp->pm_rootdirblk; 270 /* de_FileSize will be filled in further down */ 271 else { 272 ldep->de_StartCluster = MSDOSFSROOT; 273 ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec; 274 } 275 /* 276 * fill in time and date so that dos2unixtime() doesn't 277 * spit up when called from msdosfs_getattr() with root 278 * denode 279 */ 280 ldep->de_CTime = 0x0000; /* 00:00:00 */ 281 ldep->de_CTimeHundredth = 0; 282 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 283 | (1 << DD_DAY_SHIFT); 284 /* Jan 1, 1980 */ 285 ldep->de_ADate = ldep->de_CDate; 286 ldep->de_MTime = ldep->de_CTime; 287 ldep->de_MDate = ldep->de_CDate; 288 /* leave the other fields as garbage */ 289 } else { 290 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 291 if (error) 292 return (error); 293 DE_INTERNALIZE(ldep, direntptr); 294 brelse(bp); 295 } 296 297 /* 298 * Fill in a few fields of the vnode and finish filling in the 299 * denode. Then return the address of the found denode. 300 */ 301 if (ldep->de_Attributes & ATTR_DIRECTORY) { 302 /* 303 * Since DOS directory entries that describe directories 304 * have 0 in the filesize field, we take this opportunity 305 * to find out the length of the directory and plug it into 306 * the denode structure. 307 */ 308 uint32_t size; 309 310 nvp->v_type = VDIR; 311 if (ldep->de_StartCluster != MSDOSFSROOT) { 312 error = pcbmap(ldep, 0xffff, 0, &size, 0); 313 if (error == E2BIG) { 314 ldep->de_FileSize = de_cn2off(pmp, size); 315 error = 0; 316 } else { 317 printf("deget(): pcbmap returned %d\n", error); 318 return (error); 319 } 320 } 321 } else 322 nvp->v_type = VREG; 323 vref(ldep->de_devvp); 324 *depp = ldep; 325 return (0); 326 } 327 328 int 329 deupdat(struct denode *dep, int waitfor) 330 { 331 struct buf *bp; 332 struct direntry *dirp; 333 int error; 334 struct timespec ts; 335 336 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 337 return (0); 338 getnanotime(&ts); 339 DETIMES(dep, &ts, &ts, &ts); 340 if ((dep->de_flag & DE_MODIFIED) == 0) 341 return (0); 342 dep->de_flag &= ~DE_MODIFIED; 343 if (dep->de_Attributes & ATTR_DIRECTORY) 344 return (0); 345 if (dep->de_refcnt <= 0) 346 return (0); 347 error = readde(dep, &bp, &dirp); 348 if (error) 349 return (error); 350 DE_EXTERNALIZE(dirp, dep); 351 if (waitfor) 352 return (bwrite(bp)); 353 else { 354 bdwrite(bp); 355 return (0); 356 } 357 } 358 359 /* 360 * Truncate the file described by dep to the length specified by length. 361 */ 362 int 363 detrunc(struct denode *dep, uint32_t length, int flags, struct ucred *cred, 364 struct proc *p) 365 { 366 int error; 367 int allerror; 368 int vflags; 369 uint32_t eofentry; 370 uint32_t chaintofree; 371 daddr64_t bn; 372 int boff; 373 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 374 struct buf *bp; 375 struct msdosfsmount *pmp = dep->de_pmp; 376 377 #ifdef MSDOSFS_DEBUG 378 printf("detrunc(): file %s, length %ld, flags %d\n", dep->de_Name, length, flags); 379 #endif 380 381 /* 382 * Disallow attempts to truncate the root directory since it is of 383 * fixed size. That's just the way dos filesystems are. We use 384 * the VROOT bit in the vnode because checking for the directory 385 * bit and a startcluster of 0 in the denode is not adequate to 386 * recognize the root directory at this point in a file or 387 * directory's life. 388 */ 389 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { 390 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 391 dep->de_dirclust, dep->de_diroffset); 392 return (EINVAL); 393 } 394 395 uvm_vnp_setsize(DETOV(dep), length); 396 397 if (dep->de_FileSize < length) 398 return (deextend(dep, length, cred)); 399 400 /* 401 * If the desired length is 0 then remember the starting cluster of 402 * the file and set the StartCluster field in the directory entry 403 * to 0. If the desired length is not zero, then get the number of 404 * the last cluster in the shortened file. Then get the number of 405 * the first cluster in the part of the file that is to be freed. 406 * Then set the next cluster pointer in the last cluster of the 407 * file to CLUST_EOFE. 408 */ 409 if (length == 0) { 410 chaintofree = dep->de_StartCluster; 411 dep->de_StartCluster = 0; 412 eofentry = ~0; 413 } else { 414 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 415 &eofentry, 0); 416 if (error) { 417 #ifdef MSDOSFS_DEBUG 418 printf("detrunc(): pcbmap fails %d\n", error); 419 #endif 420 return (error); 421 } 422 } 423 424 fc_purge(dep, de_clcount(pmp, length)); 425 426 /* 427 * If the new length is not a multiple of the cluster size then we 428 * must zero the tail end of the new last cluster in case it 429 * becomes part of the file again because of a seek. 430 */ 431 if ((boff = length & pmp->pm_crbomask) != 0) { 432 if (isadir) { 433 bn = cntobn(pmp, eofentry); 434 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 435 NOCRED, &bp); 436 } else { 437 bn = de_blk(pmp, length); 438 error = bread(DETOV(dep), bn, pmp->pm_bpcluster, 439 NOCRED, &bp); 440 } 441 if (error) { 442 brelse(bp); 443 #ifdef MSDOSFS_DEBUG 444 printf("detrunc(): bread fails %d\n", error); 445 #endif 446 return (error); 447 } 448 uvm_vnp_uncache(DETOV(dep)); 449 /* 450 * is this the right place for it? 451 */ 452 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 453 if (flags & IO_SYNC) 454 bwrite(bp); 455 else 456 bdwrite(bp); 457 } 458 459 /* 460 * Write out the updated directory entry. Even if the update fails 461 * we free the trailing clusters. 462 */ 463 dep->de_FileSize = length; 464 if (!isadir) 465 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 466 vflags = (length > 0 ? V_SAVE : 0) | V_SAVEMETA; 467 vinvalbuf(DETOV(dep), vflags, cred, p, 0, 0); 468 allerror = deupdat(dep, 1); 469 #ifdef MSDOSFS_DEBUG 470 printf("detrunc(): allerror %d, eofentry %d\n", 471 allerror, eofentry); 472 #endif 473 474 /* 475 * If we need to break the cluster chain for the file then do it 476 * now. 477 */ 478 if (eofentry != ~0) { 479 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 480 &chaintofree, CLUST_EOFE); 481 if (error) { 482 #ifdef MSDOSFS_DEBUG 483 printf("detrunc(): fatentry errors %d\n", error); 484 #endif 485 return (error); 486 } 487 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 488 eofentry); 489 } 490 491 /* 492 * Now free the clusters removed from the file because of the 493 * truncation. 494 */ 495 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 496 freeclusterchain(pmp, chaintofree); 497 498 return (allerror); 499 } 500 501 /* 502 * Extend the file described by dep to length specified by length. 503 */ 504 int 505 deextend(struct denode *dep, uint32_t length, struct ucred *cred) 506 { 507 struct msdosfsmount *pmp = dep->de_pmp; 508 uint32_t count; 509 int error; 510 511 /* 512 * The root of a DOS filesystem cannot be extended. 513 */ 514 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) 515 return (EINVAL); 516 517 /* 518 * Directories cannot be extended. 519 */ 520 if (dep->de_Attributes & ATTR_DIRECTORY) 521 return (EISDIR); 522 523 if (length <= dep->de_FileSize) 524 panic("deextend: file too large"); 525 526 /* 527 * Compute the number of clusters to allocate. 528 */ 529 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 530 if (count > 0) { 531 if (count > pmp->pm_freeclustercount) 532 return (ENOSPC); 533 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 534 if (error) { 535 /* truncate the added clusters away again */ 536 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 537 return (error); 538 } 539 } 540 541 dep->de_FileSize = length; 542 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 543 return (deupdat(dep, 1)); 544 } 545 546 /* 547 * Move a denode to its correct hash queue after the file it represents has 548 * been moved to a new directory. 549 */ 550 void 551 reinsert(struct denode *dep) 552 { 553 /* 554 * Fix up the denode cache. If the denode is for a directory, 555 * there is nothing to do since the hash is based on the starting 556 * cluster of the directory file and that hasn't changed. If for a 557 * file the hash is based on the location of the directory entry, 558 * so we must remove it from the cache and re-enter it with the 559 * hash based on the new location of the directory entry. 560 */ 561 if (dep->de_Attributes & ATTR_DIRECTORY) 562 return; 563 msdosfs_hashrem(dep); 564 msdosfs_hashins(dep); 565 } 566 567 int 568 msdosfs_reclaim(void *v) 569 { 570 struct vop_reclaim_args *ap = v; 571 struct vnode *vp = ap->a_vp; 572 struct denode *dep = VTODE(vp); 573 #ifdef DIAGNOSTIC 574 extern int prtactive; 575 576 if (prtactive && vp->v_usecount != 0) 577 vprint("msdosfs_reclaim(): pushing active", vp); 578 #endif 579 580 #ifdef MSDOSFS_DEBUG 581 printf("msdosfs_reclaim(): dep %08x, file %s, refcnt %d\n", 582 dep, dep->de_Name, dep->de_refcnt); 583 #endif 584 585 /* 586 * Remove the denode from its hash chain. 587 */ 588 msdosfs_hashrem(dep); 589 /* 590 * Purge old data structures associated with the denode. 591 */ 592 cache_purge(vp); 593 if (dep->de_devvp) { 594 vrele(dep->de_devvp); 595 dep->de_devvp = 0; 596 } 597 #if 0 /* XXX */ 598 dep->de_flag = 0; 599 #endif 600 free(dep, M_MSDOSFSNODE); 601 vp->v_data = NULL; 602 return (0); 603 } 604 605 int 606 msdosfs_inactive(void *v) 607 { 608 struct vop_inactive_args *ap = v; 609 struct vnode *vp = ap->a_vp; 610 struct denode *dep = VTODE(vp); 611 struct proc *p = ap->a_p; 612 int error; 613 #ifdef DIAGNOSTIC 614 extern int prtactive; 615 616 if (prtactive && vp->v_usecount != 0) 617 vprint("msdosfs_inactive(): pushing active", vp); 618 #endif 619 620 #ifdef MSDOSFS_DEBUG 621 printf("msdosfs_inactive(): dep %08x, de_Name[0] %x\n", dep, dep->de_Name[0]); 622 #endif 623 624 error = 0; 625 626 /* 627 * Get rid of denodes related to stale file handles. 628 */ 629 if (dep->de_Name[0] == SLOT_DELETED) 630 goto out; 631 632 /* 633 * If the file has been deleted and it is on a read/write 634 * filesystem, then truncate the file, and mark the directory slot 635 * as empty. (This may not be necessary for the dos filesystem.) 636 */ 637 #ifdef MSDOSFS_DEBUG 638 printf("msdosfs_inactive(): dep %08x, refcnt %d, mntflag %x, MNT_RDONLY %x\n", 639 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 640 #endif 641 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 642 error = detrunc(dep, (uint32_t)0, 0, NOCRED, NULL); 643 dep->de_Name[0] = SLOT_DELETED; 644 } 645 deupdat(dep, 0); 646 647 out: 648 VOP_UNLOCK(vp, 0, p); 649 /* 650 * If we are done with the denode, reclaim it 651 * so that it can be reused immediately. 652 */ 653 #ifdef MSDOSFS_DEBUG 654 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount, 655 dep->de_Name[0]); 656 #endif 657 if (dep->de_Name[0] == SLOT_DELETED) 658 vrecycle(vp, p); 659 return (error); 660 } 661