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