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