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