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