1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-4-Clause 6 * 7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 9 * All rights reserved. 10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by TooLs GmbH. 23 * 4. The name of TooLs GmbH may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 /*- 38 * Written by Paul Popelka (paulp@uts.amdahl.com) 39 * 40 * You can do anything you want with this software, just don't say you wrote 41 * it, and don't remove this notice. 42 * 43 * This software is provided "as is". 44 * 45 * The author supplies this software to be publicly redistributed on the 46 * understanding that the author is not responsible for the correct 47 * functioning of this software in any circumstances and is not liable for 48 * any damages caused by this software. 49 * 50 * October 1992 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/buf.h> 56 #include <sys/clock.h> 57 #include <sys/kernel.h> 58 #include <sys/malloc.h> 59 #include <sys/mount.h> 60 #include <sys/vmmeter.h> 61 #include <sys/vnode.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_extern.h> 65 66 #include <fs/msdosfs/bpb.h> 67 #include <fs/msdosfs/direntry.h> 68 #include <fs/msdosfs/denode.h> 69 #include <fs/msdosfs/fat.h> 70 #include <fs/msdosfs/msdosfsmount.h> 71 72 static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part"); 73 74 static int 75 de_vncmpf(struct vnode *vp, void *arg) 76 { 77 struct denode *de; 78 uint64_t *a; 79 80 a = arg; 81 de = VTODE(vp); 82 return (de->de_inode != *a) || (de->de_refcnt <= 0); 83 } 84 85 /* 86 * If deget() succeeds it returns with the gotten denode locked(). 87 * 88 * pmp - address of msdosfsmount structure of the filesystem containing 89 * the denode of interest. The address of 90 * the msdosfsmount structure are used. 91 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 92 * diroffset is relative to the beginning of the root directory, 93 * otherwise it is cluster relative. 94 * diroffset - offset past begin of cluster of denode we want 95 * depp - returns the address of the gotten denode. 96 */ 97 int 98 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 99 struct denode **depp) 100 { 101 int error; 102 uint64_t inode; 103 struct mount *mntp = pmp->pm_mountp; 104 struct direntry *direntptr; 105 struct denode *ldep; 106 struct vnode *nvp, *xvp; 107 struct buf *bp; 108 109 #ifdef MSDOSFS_DEBUG 110 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 111 pmp, dirclust, diroffset, depp); 112 #endif 113 114 /* 115 * On FAT32 filesystems, root is a (more or less) normal 116 * directory 117 */ 118 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 119 dirclust = pmp->pm_rootdirblk; 120 121 /* 122 * See if the denode is in the denode cache. Use the location of 123 * the directory entry to compute the hash value. For subdir use 124 * address of "." entry. For root dir (if not FAT32) use cluster 125 * MSDOSFSROOT, offset MSDOSFSROOT_OFS 126 * 127 * NOTE: de_vncmpf will explicitly skip any denodes that do not have 128 * a de_refcnt > 0. This insures that that we do not attempt to use 129 * a denode that represents an unlinked but still open file. 130 * These files are not to be accessible even when the directory 131 * entry that represented the file happens to be reused while the 132 * deleted file is still open. 133 */ 134 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 135 136 error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp, 137 de_vncmpf, &inode); 138 if (error) 139 return (error); 140 if (nvp != NULL) { 141 *depp = VTODE(nvp); 142 KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust")); 143 KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset")); 144 return (0); 145 } 146 ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO); 147 148 /* 149 * Directory entry was not in cache, have to create a vnode and 150 * copy it from the passed disk buffer. 151 */ 152 /* getnewvnode() does a VREF() on the vnode */ 153 error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp); 154 if (error) { 155 *depp = NULL; 156 free(ldep, M_MSDOSFSNODE); 157 return error; 158 } 159 nvp->v_data = ldep; 160 ldep->de_vnode = nvp; 161 ldep->de_flag = 0; 162 ldep->de_dirclust = dirclust; 163 ldep->de_diroffset = diroffset; 164 ldep->de_inode = inode; 165 cluster_init_vn(&ldep->de_clusterw); 166 lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL); 167 fc_purge(ldep, 0); /* init the FAT cache for this denode */ 168 error = insmntque(nvp, mntp); 169 if (error != 0) { 170 free(ldep, M_MSDOSFSNODE); 171 *depp = NULL; 172 return (error); 173 } 174 error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp, 175 de_vncmpf, &inode); 176 if (error) { 177 *depp = NULL; 178 return (error); 179 } 180 if (xvp != NULL) { 181 *depp = xvp->v_data; 182 return (0); 183 } 184 185 ldep->de_pmp = pmp; 186 ldep->de_refcnt = 1; 187 /* 188 * Copy the directory entry into the denode area of the vnode. 189 */ 190 if ((dirclust == MSDOSFSROOT 191 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 192 && diroffset == MSDOSFSROOT_OFS) { 193 /* 194 * Directory entry for the root directory. There isn't one, 195 * so we manufacture one. We should probably rummage 196 * through the root directory and find a label entry (if it 197 * exists), and then use the time and date from that entry 198 * as the time and date for the root denode. 199 */ 200 nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 201 202 ldep->de_Attributes = ATTR_DIRECTORY; 203 ldep->de_LowerCase = 0; 204 if (FAT32(pmp)) 205 ldep->de_StartCluster = pmp->pm_rootdirblk; 206 /* de_FileSize will be filled in further down */ 207 else { 208 ldep->de_StartCluster = MSDOSFSROOT; 209 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 210 } 211 /* 212 * fill in time and date so that fattime2timespec() doesn't 213 * spit up when called from msdosfs_getattr() with root 214 * denode 215 */ 216 ldep->de_CHun = 0; 217 ldep->de_CTime = 0x0000; /* 00:00:00 */ 218 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 219 | (1 << DD_DAY_SHIFT); 220 /* Jan 1, 1980 */ 221 ldep->de_ADate = ldep->de_CDate; 222 ldep->de_MTime = ldep->de_CTime; 223 ldep->de_MDate = ldep->de_CDate; 224 /* leave the other fields as garbage */ 225 } else { 226 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 227 if (error) { 228 /* 229 * The denode does not contain anything useful, so 230 * it would be wrong to leave it on its hash chain. 231 * Arrange for vput() to just forget about it. 232 */ 233 ldep->de_Name[0] = SLOT_DELETED; 234 vgone(nvp); 235 vput(nvp); 236 *depp = NULL; 237 return (error); 238 } 239 (void)DE_INTERNALIZE(ldep, direntptr); 240 brelse(bp); 241 } 242 243 /* 244 * Fill in a few fields of the vnode and finish filling in the 245 * denode. Then return the address of the found denode. 246 */ 247 if (ldep->de_Attributes & ATTR_DIRECTORY) { 248 /* 249 * Since DOS directory entries that describe directories 250 * have 0 in the filesize field, we take this opportunity 251 * to find out the length of the directory and plug it into 252 * the denode structure. 253 */ 254 u_long size; 255 256 /* 257 * XXX it sometimes happens that the "." entry has cluster 258 * number 0 when it shouldn't. Use the actual cluster number 259 * instead of what is written in directory entry. 260 */ 261 if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 262 #ifdef MSDOSFS_DEBUG 263 printf("deget(): \".\" entry at clust %lu != %lu\n", 264 dirclust, ldep->de_StartCluster); 265 #endif 266 ldep->de_StartCluster = dirclust; 267 } 268 269 nvp->v_type = VDIR; 270 if (ldep->de_StartCluster != MSDOSFSROOT) { 271 error = pcbmap(ldep, 0xffff, 0, &size, 0); 272 if (error == E2BIG) { 273 ldep->de_FileSize = de_cn2off(pmp, size); 274 error = 0; 275 } else { 276 #ifdef MSDOSFS_DEBUG 277 printf("deget(): pcbmap returned %d\n", error); 278 #endif 279 } 280 } 281 } else 282 nvp->v_type = VREG; 283 ldep->de_modrev = init_va_filerev(); 284 *depp = ldep; 285 return (0); 286 } 287 288 int 289 deupdat(struct denode *dep, int waitfor) 290 { 291 struct direntry dir; 292 struct timespec ts; 293 struct buf *bp; 294 struct direntry *dirp; 295 int error; 296 297 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) { 298 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS | 299 DE_MODIFIED); 300 return (0); 301 } 302 vfs_timestamp(&ts); 303 DETIMES(dep, &ts, &ts, &ts); 304 if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0) 305 return (0); 306 dep->de_flag &= ~DE_MODIFIED; 307 if (DETOV(dep)->v_vflag & VV_ROOT) 308 return (EINVAL); 309 if (dep->de_refcnt <= 0) 310 return (0); 311 error = readde(dep, &bp, &dirp); 312 if (error) 313 return (error); 314 DE_EXTERNALIZE(&dir, dep); 315 if (bcmp(dirp, &dir, sizeof(dir)) == 0) { 316 if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) { 317 brelse(bp); 318 return (0); 319 } 320 } else 321 *dirp = dir; 322 if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) 323 bp->b_flags |= B_CLUSTEROK; 324 if (waitfor) 325 error = bwrite(bp); 326 else if (vm_page_count_severe() || buf_dirty_count_severe()) 327 bawrite(bp); 328 else 329 bdwrite(bp); 330 return (error); 331 } 332 333 /* 334 * Truncate the file described by dep to the length specified by length. 335 */ 336 int 337 detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred) 338 { 339 int error; 340 int allerror; 341 u_long eofentry; 342 u_long chaintofree; 343 daddr_t bn; 344 int boff; 345 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 346 struct buf *bp; 347 struct msdosfsmount *pmp = dep->de_pmp; 348 349 #ifdef MSDOSFS_DEBUG 350 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 351 #endif 352 353 /* 354 * Disallow attempts to truncate the root directory since it is of 355 * fixed size. That's just the way dos filesystems are. We use 356 * the VROOT bit in the vnode because checking for the directory 357 * bit and a startcluster of 0 in the denode is not adequate to 358 * recognize the root directory at this point in a file or 359 * directory's life. 360 */ 361 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 362 #ifdef MSDOSFS_DEBUG 363 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 364 dep->de_dirclust, dep->de_diroffset); 365 #endif 366 return (EINVAL); 367 } 368 369 if (dep->de_FileSize < length) { 370 vnode_pager_setsize(DETOV(dep), length); 371 return deextend(dep, length, cred); 372 } 373 374 /* 375 * If the desired length is 0 then remember the starting cluster of 376 * the file and set the StartCluster field in the directory entry 377 * to 0. If the desired length is not zero, then get the number of 378 * the last cluster in the shortened file. Then get the number of 379 * the first cluster in the part of the file that is to be freed. 380 * Then set the next cluster pointer in the last cluster of the 381 * file to CLUST_EOFE. 382 */ 383 if (length == 0) { 384 chaintofree = dep->de_StartCluster; 385 dep->de_StartCluster = 0; 386 eofentry = ~0; 387 } else { 388 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 389 &eofentry, 0); 390 if (error) { 391 #ifdef MSDOSFS_DEBUG 392 printf("detrunc(): pcbmap fails %d\n", error); 393 #endif 394 return (error); 395 } 396 } 397 398 fc_purge(dep, de_clcount(pmp, length)); 399 400 /* 401 * If the new length is not a multiple of the cluster size then we 402 * must zero the tail end of the new last cluster in case it 403 * becomes part of the file again because of a seek. 404 */ 405 if ((boff = length & pmp->pm_crbomask) != 0) { 406 if (isadir) { 407 bn = cntobn(pmp, eofentry); 408 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 409 NOCRED, &bp); 410 } else { 411 error = bread(DETOV(dep), de_cluster(pmp, length), 412 pmp->pm_bpcluster, cred, &bp); 413 } 414 if (error) { 415 #ifdef MSDOSFS_DEBUG 416 printf("detrunc(): bread fails %d\n", error); 417 #endif 418 return (error); 419 } 420 memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 421 if ((flags & IO_SYNC) != 0) 422 bwrite(bp); 423 else 424 bdwrite(bp); 425 } 426 427 /* 428 * Write out the updated directory entry. Even if the update fails 429 * we free the trailing clusters. 430 */ 431 dep->de_FileSize = length; 432 if (!isadir) 433 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 434 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster); 435 #ifdef MSDOSFS_DEBUG 436 if (allerror) 437 printf("detrunc(): vtruncbuf error %d\n", allerror); 438 #endif 439 error = deupdat(dep, !DOINGASYNC((DETOV(dep)))); 440 if (error != 0 && allerror == 0) 441 allerror = error; 442 #ifdef MSDOSFS_DEBUG 443 printf("detrunc(): allerror %d, eofentry %lu\n", 444 allerror, eofentry); 445 #endif 446 447 /* 448 * If we need to break the cluster chain for the file then do it 449 * now. 450 */ 451 if (eofentry != ~0) { 452 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 453 &chaintofree, CLUST_EOFE); 454 if (error) { 455 #ifdef MSDOSFS_DEBUG 456 printf("detrunc(): fatentry errors %d\n", error); 457 #endif 458 return (error); 459 } 460 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 461 eofentry); 462 } 463 464 /* 465 * Now free the clusters removed from the file because of the 466 * truncation. 467 */ 468 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 469 freeclusterchain(pmp, chaintofree); 470 471 return (allerror); 472 } 473 474 /* 475 * Extend the file described by dep to length specified by length. 476 */ 477 int 478 deextend(struct denode *dep, u_long length, struct ucred *cred) 479 { 480 struct msdosfsmount *pmp = dep->de_pmp; 481 u_long count; 482 int error; 483 484 /* 485 * The root of a DOS filesystem cannot be extended. 486 */ 487 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 488 return (EINVAL); 489 490 /* 491 * Directories cannot be extended. 492 */ 493 if (dep->de_Attributes & ATTR_DIRECTORY) 494 return (EISDIR); 495 496 if (length <= dep->de_FileSize) 497 panic("deextend: file too large"); 498 499 /* 500 * Compute the number of clusters to allocate. 501 */ 502 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 503 if (count > 0) { 504 if (count > pmp->pm_freeclustercount) 505 return (ENOSPC); 506 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 507 if (error) { 508 /* truncate the added clusters away again */ 509 (void) detrunc(dep, dep->de_FileSize, 0, cred); 510 return (error); 511 } 512 } 513 dep->de_FileSize = length; 514 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 515 return (deupdat(dep, !DOINGASYNC(DETOV(dep)))); 516 } 517 518 /* 519 * Move a denode to its correct hash queue after the file it represents has 520 * been moved to a new directory. 521 */ 522 void 523 reinsert(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(struct vop_reclaim_args *ap) 547 { 548 struct vnode *vp = ap->a_vp; 549 struct denode *dep = VTODE(vp); 550 551 #ifdef MSDOSFS_DEBUG 552 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 553 dep, dep->de_Name, dep->de_refcnt); 554 #endif 555 556 /* 557 * Remove the denode from its hash chain. 558 */ 559 vfs_hash_remove(vp); 560 /* 561 * Purge old data structures associated with the denode. 562 */ 563 #if 0 /* XXX */ 564 dep->de_flag = 0; 565 #endif 566 free(dep, M_MSDOSFSNODE); 567 vp->v_data = NULL; 568 569 return (0); 570 } 571 572 int 573 msdosfs_inactive(struct vop_inactive_args *ap) 574 { 575 struct vnode *vp = ap->a_vp; 576 struct denode *dep = VTODE(vp); 577 int error = 0; 578 579 #ifdef MSDOSFS_DEBUG 580 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 581 #endif 582 583 /* 584 * Ignore denodes related to stale file handles. 585 */ 586 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY) 587 goto out; 588 589 /* 590 * If the file has been deleted and it is on a read/write 591 * filesystem, then truncate the file, and mark the directory slot 592 * as empty. (This may not be necessary for the dos filesystem.) 593 */ 594 #ifdef MSDOSFS_DEBUG 595 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n", 596 dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag, 597 (unsigned long long)MNT_RDONLY); 598 #endif 599 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 600 error = detrunc(dep, (u_long) 0, 0, NOCRED); 601 dep->de_flag |= DE_UPDATE; 602 dep->de_Name[0] = SLOT_DELETED; 603 } 604 deupdat(dep, 0); 605 606 out: 607 /* 608 * If we are done with the denode, reclaim it 609 * so that it can be reused immediately. 610 */ 611 #ifdef MSDOSFS_DEBUG 612 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 613 vrefcnt(vp), dep->de_Name[0]); 614 #endif 615 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY) 616 vrecycle(vp); 617 return (error); 618 } 619