1 /* $OpenBSD: msdosfs_denode.c,v 1.43 2011/07/04 04:30:41 tedu 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 struct vops msdosfs_vops; 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, &msdosfs_vops, &nvp); 215 if (error) { 216 *depp = 0; 217 return (error); 218 } 219 ldep = malloc(sizeof(*ldep), M_MSDOSFSNODE, M_WAITOK | M_ZERO); 220 lockinit(&ldep->de_lock, PINOD, "denode", 0, 0); 221 nvp->v_data = ldep; 222 ldep->de_vnode = nvp; 223 ldep->de_flag = 0; 224 ldep->de_devvp = 0; 225 ldep->de_lockf = 0; 226 ldep->de_dev = pmp->pm_dev; 227 ldep->de_dirclust = dirclust; 228 ldep->de_diroffset = diroffset; 229 fc_purge(ldep, 0); /* init the fat cache for this denode */ 230 231 /* 232 * Insert the denode into the hash queue and lock the denode so it 233 * can't be accessed until we've read it in and have done what we 234 * need to it. 235 */ 236 vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY, p); 237 error = msdosfs_hashins(ldep); 238 239 if (error) { 240 vput (nvp); 241 242 if (error == EEXIST) 243 goto retry; 244 245 return (error); 246 } 247 248 ldep->de_pmp = pmp; 249 ldep->de_devvp = pmp->pm_devvp; 250 ldep->de_refcnt = 1; 251 /* 252 * Copy the directory entry into the denode area of the vnode. 253 */ 254 if ((dirclust == MSDOSFSROOT 255 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 256 && diroffset == MSDOSFSROOT_OFS) { 257 /* 258 * Directory entry for the root directory. There isn't one, 259 * so we manufacture one. We should probably rummage 260 * through the root directory and find a label entry (if it 261 * exists), and then use the time and date from that entry 262 * as the time and date for the root denode. 263 */ 264 nvp->v_flag |= VROOT; /* should be further down XXX */ 265 266 ldep->de_Attributes = ATTR_DIRECTORY; 267 if (FAT32(pmp)) 268 ldep->de_StartCluster = pmp->pm_rootdirblk; 269 /* de_FileSize will be filled in further down */ 270 else { 271 ldep->de_StartCluster = MSDOSFSROOT; 272 ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec; 273 } 274 /* 275 * fill in time and date so that dos2unixtime() doesn't 276 * spit up when called from msdosfs_getattr() with root 277 * denode 278 */ 279 ldep->de_CTime = 0x0000; /* 00:00:00 */ 280 ldep->de_CTimeHundredth = 0; 281 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 282 | (1 << DD_DAY_SHIFT); 283 /* Jan 1, 1980 */ 284 ldep->de_ADate = ldep->de_CDate; 285 ldep->de_MTime = ldep->de_CTime; 286 ldep->de_MDate = ldep->de_CDate; 287 /* leave the other fields as garbage */ 288 } else { 289 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 290 if (error) 291 return (error); 292 DE_INTERNALIZE(ldep, direntptr); 293 brelse(bp); 294 } 295 296 /* 297 * Fill in a few fields of the vnode and finish filling in the 298 * denode. Then return the address of the found denode. 299 */ 300 if (ldep->de_Attributes & ATTR_DIRECTORY) { 301 /* 302 * Since DOS directory entries that describe directories 303 * have 0 in the filesize field, we take this opportunity 304 * to find out the length of the directory and plug it into 305 * the denode structure. 306 */ 307 uint32_t size; 308 309 nvp->v_type = VDIR; 310 if (ldep->de_StartCluster != MSDOSFSROOT) { 311 error = pcbmap(ldep, 0xffff, 0, &size, 0); 312 if (error == E2BIG) { 313 ldep->de_FileSize = de_cn2off(pmp, size); 314 error = 0; 315 } else { 316 printf("deget(): pcbmap returned %d\n", error); 317 return (error); 318 } 319 } 320 } else 321 nvp->v_type = VREG; 322 vref(ldep->de_devvp); 323 *depp = ldep; 324 return (0); 325 } 326 327 int 328 deupdat(struct denode *dep, int waitfor) 329 { 330 struct buf *bp; 331 struct direntry *dirp; 332 int error; 333 struct timespec ts; 334 335 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 336 return (0); 337 getnanotime(&ts); 338 DETIMES(dep, &ts, &ts, &ts); 339 if ((dep->de_flag & DE_MODIFIED) == 0) 340 return (0); 341 dep->de_flag &= ~DE_MODIFIED; 342 if (dep->de_Attributes & ATTR_DIRECTORY) 343 return (0); 344 if (dep->de_refcnt <= 0) 345 return (0); 346 error = readde(dep, &bp, &dirp); 347 if (error) 348 return (error); 349 DE_EXTERNALIZE(dirp, dep); 350 if (waitfor) 351 return (bwrite(bp)); 352 else { 353 bdwrite(bp); 354 return (0); 355 } 356 } 357 358 /* 359 * Truncate the file described by dep to the length specified by length. 360 */ 361 int 362 detrunc(struct denode *dep, uint32_t length, int flags, struct ucred *cred, 363 struct proc *p) 364 { 365 int error; 366 int allerror; 367 int vflags; 368 uint32_t eofentry; 369 uint32_t chaintofree = 0; 370 daddr64_t bn; 371 int boff; 372 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 373 struct buf *bp; 374 struct msdosfsmount *pmp = dep->de_pmp; 375 376 #ifdef MSDOSFS_DEBUG 377 printf("detrunc(): file %s, length %ld, flags %d\n", dep->de_Name, length, flags); 378 #endif 379 380 /* 381 * Disallow attempts to truncate the root directory since it is of 382 * fixed size. That's just the way dos filesystems are. We use 383 * the VROOT bit in the vnode because checking for the directory 384 * bit and a startcluster of 0 in the denode is not adequate to 385 * recognize the root directory at this point in a file or 386 * directory's life. 387 */ 388 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { 389 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 390 dep->de_dirclust, dep->de_diroffset); 391 return (EINVAL); 392 } 393 394 uvm_vnp_setsize(DETOV(dep), length); 395 396 if (dep->de_FileSize < length) 397 return (deextend(dep, length, cred)); 398 399 /* 400 * If the desired length is 0 then remember the starting cluster of 401 * the file and set the StartCluster field in the directory entry 402 * to 0. If the desired length is not zero, then get the number of 403 * the last cluster in the shortened file. Then get the number of 404 * the first cluster in the part of the file that is to be freed. 405 * Then set the next cluster pointer in the last cluster of the 406 * file to CLUST_EOFE. 407 */ 408 if (length == 0) { 409 chaintofree = dep->de_StartCluster; 410 dep->de_StartCluster = 0; 411 eofentry = ~0; 412 } else { 413 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 414 &eofentry, 0); 415 if (error) { 416 #ifdef MSDOSFS_DEBUG 417 printf("detrunc(): pcbmap fails %d\n", error); 418 #endif 419 return (error); 420 } 421 } 422 423 fc_purge(dep, de_clcount(pmp, length)); 424 425 /* 426 * If the new length is not a multiple of the cluster size then we 427 * must zero the tail end of the new last cluster in case it 428 * becomes part of the file again because of a seek. 429 */ 430 if ((boff = length & pmp->pm_crbomask) != 0) { 431 if (isadir) { 432 bn = cntobn(pmp, eofentry); 433 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp); 434 } else { 435 bn = de_blk(pmp, length); 436 error = bread(DETOV(dep), bn, pmp->pm_bpcluster, &bp); 437 } 438 if (error) { 439 brelse(bp); 440 #ifdef MSDOSFS_DEBUG 441 printf("detrunc(): bread fails %d\n", error); 442 #endif 443 return (error); 444 } 445 uvm_vnp_uncache(DETOV(dep)); 446 /* 447 * is this the right place for it? 448 */ 449 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 450 if (flags & IO_SYNC) 451 bwrite(bp); 452 else 453 bdwrite(bp); 454 } 455 456 /* 457 * Write out the updated directory entry. Even if the update fails 458 * we free the trailing clusters. 459 */ 460 dep->de_FileSize = length; 461 if (!isadir) 462 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 463 vflags = (length > 0 ? V_SAVE : 0) | V_SAVEMETA; 464 vinvalbuf(DETOV(dep), vflags, cred, p, 0, 0); 465 allerror = deupdat(dep, 1); 466 #ifdef MSDOSFS_DEBUG 467 printf("detrunc(): allerror %d, eofentry %d\n", 468 allerror, eofentry); 469 #endif 470 471 /* 472 * If we need to break the cluster chain for the file then do it 473 * now. 474 */ 475 if (eofentry != ~0) { 476 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 477 &chaintofree, CLUST_EOFE); 478 if (error) { 479 #ifdef MSDOSFS_DEBUG 480 printf("detrunc(): fatentry errors %d\n", error); 481 #endif 482 return (error); 483 } 484 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 485 eofentry); 486 } 487 488 /* 489 * Now free the clusters removed from the file because of the 490 * truncation. 491 */ 492 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 493 freeclusterchain(pmp, chaintofree); 494 495 return (allerror); 496 } 497 498 /* 499 * Extend the file described by dep to length specified by length. 500 */ 501 int 502 deextend(struct denode *dep, uint32_t length, struct ucred *cred) 503 { 504 struct msdosfsmount *pmp = dep->de_pmp; 505 uint32_t count; 506 int error; 507 508 /* 509 * The root of a DOS filesystem cannot be extended. 510 */ 511 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) 512 return (EINVAL); 513 514 /* 515 * Directories cannot be extended. 516 */ 517 if (dep->de_Attributes & ATTR_DIRECTORY) 518 return (EISDIR); 519 520 if (length <= dep->de_FileSize) 521 panic("deextend: file too large"); 522 523 /* 524 * Compute the number of clusters to allocate. 525 */ 526 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 527 if (count > 0) { 528 if (count > pmp->pm_freeclustercount) 529 return (ENOSPC); 530 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 531 if (error) { 532 /* truncate the added clusters away again */ 533 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 534 return (error); 535 } 536 } 537 538 dep->de_FileSize = length; 539 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 540 return (deupdat(dep, 1)); 541 } 542 543 /* 544 * Move a denode to its correct hash queue after the file it represents has 545 * been moved to a new directory. 546 */ 547 void 548 reinsert(struct denode *dep) 549 { 550 /* 551 * Fix up the denode cache. If the denode is for a directory, 552 * there is nothing to do since the hash is based on the starting 553 * cluster of the directory file and that hasn't changed. If for a 554 * file the hash is based on the location of the directory entry, 555 * so we must remove it from the cache and re-enter it with the 556 * hash based on the new location of the directory entry. 557 */ 558 if (dep->de_Attributes & ATTR_DIRECTORY) 559 return; 560 msdosfs_hashrem(dep); 561 msdosfs_hashins(dep); 562 } 563 564 int 565 msdosfs_reclaim(void *v) 566 { 567 struct vop_reclaim_args *ap = v; 568 struct vnode *vp = ap->a_vp; 569 struct denode *dep = VTODE(vp); 570 #ifdef DIAGNOSTIC 571 extern int prtactive; 572 573 if (prtactive && vp->v_usecount != 0) 574 vprint("msdosfs_reclaim(): pushing active", vp); 575 #endif 576 577 #ifdef MSDOSFS_DEBUG 578 printf("msdosfs_reclaim(): dep %08x, file %s, refcnt %d\n", 579 dep, dep->de_Name, dep->de_refcnt); 580 #endif 581 582 /* 583 * Remove the denode from its hash chain. 584 */ 585 msdosfs_hashrem(dep); 586 /* 587 * Purge old data structures associated with the denode. 588 */ 589 cache_purge(vp); 590 if (dep->de_devvp) { 591 vrele(dep->de_devvp); 592 dep->de_devvp = 0; 593 } 594 #if 0 /* XXX */ 595 dep->de_flag = 0; 596 #endif 597 free(dep, M_MSDOSFSNODE); 598 vp->v_data = NULL; 599 return (0); 600 } 601 602 int 603 msdosfs_inactive(void *v) 604 { 605 struct vop_inactive_args *ap = v; 606 struct vnode *vp = ap->a_vp; 607 struct denode *dep = VTODE(vp); 608 struct proc *p = ap->a_p; 609 int error; 610 #ifdef DIAGNOSTIC 611 extern int prtactive; 612 613 if (prtactive && vp->v_usecount != 0) 614 vprint("msdosfs_inactive(): pushing active", vp); 615 #endif 616 617 #ifdef MSDOSFS_DEBUG 618 printf("msdosfs_inactive(): dep %08x, de_Name[0] %x\n", dep, dep->de_Name[0]); 619 #endif 620 621 error = 0; 622 623 /* 624 * Get rid of denodes related to stale file handles. 625 */ 626 if (dep->de_Name[0] == SLOT_DELETED) 627 goto out; 628 629 /* 630 * If the file has been deleted and it is on a read/write 631 * filesystem, then truncate the file, and mark the directory slot 632 * as empty. (This may not be necessary for the dos filesystem.) 633 */ 634 #ifdef MSDOSFS_DEBUG 635 printf("msdosfs_inactive(): dep %08x, refcnt %d, mntflag %x, MNT_RDONLY %x\n", 636 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 637 #endif 638 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 639 error = detrunc(dep, (uint32_t)0, 0, NOCRED, NULL); 640 dep->de_Name[0] = SLOT_DELETED; 641 } 642 deupdat(dep, 0); 643 644 out: 645 VOP_UNLOCK(vp, 0, p); 646 /* 647 * If we are done with the denode, reclaim it 648 * so that it can be reused immediately. 649 */ 650 #ifdef MSDOSFS_DEBUG 651 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount, 652 dep->de_Name[0]); 653 #endif 654 if (dep->de_Name[0] == SLOT_DELETED) 655 vrecycle(vp, p); 656 return (error); 657 } 658