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/kernel.h> 57 #include <sys/malloc.h> 58 #include <sys/proc.h> 59 #include <sys/mount.h> 60 #include <sys/vnode.h> 61 #include <sys/time.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_extern.h> 65 #include <vm/vm_page2.h> 66 67 #include <sys/buf2.h> 68 69 #include <vfs/msdosfs/bpb.h> 70 #include <vfs/msdosfs/direntry.h> 71 #include <vfs/msdosfs/denode.h> 72 #include <vfs/msdosfs/fat.h> 73 #include <vfs/msdosfs/msdosfsmount.h> 74 75 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part"); 76 77 /* 78 * Hash table caching denode instances. 79 * 80 * denodes are keyed by the disk location (cluster num, entry offset) of the 81 * directory entry of the file they represent. 82 * 83 * denodes representing deleted but still opened files are left in this cache 84 * until reclaimed. Deleted directory entries can be reused when files are 85 * renamed or new files created. As a consequence, several denodes associated 86 * with the same entry may coexist in this cache as long as a single one of 87 * them map to an existing file (de_refcnt > 0). 88 * 89 * R/w access to this cache is protected by dehash_token. 90 */ 91 static struct denode **dehashtbl; 92 static u_long dehash; /* size of hash table - 1 */ 93 static struct lwkt_token dehash_token; 94 95 #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \ 96 sizeof(struct direntry)) & dehash]) 97 98 /*ARGSUSED*/ 99 int 100 msdosfs_init(struct vfsconf *vfsp) 101 { 102 dehash = vfs_inodehashsize(); 103 dehashtbl = kmalloc(sizeof(void *) * dehash, 104 M_MSDOSFSMNT, M_WAITOK|M_ZERO); 105 --dehash; 106 lwkt_token_init(&dehash_token, "msdosihash"); 107 108 return (0); 109 } 110 111 int 112 msdosfs_uninit(struct vfsconf *vfsp) 113 { 114 if (dehashtbl) 115 kfree(dehashtbl, M_MSDOSFSMNT); 116 return (0); 117 } 118 119 static struct denode * 120 msdosfs_hashget(cdev_t dev, u_long dirclust, u_long diroff) 121 { 122 struct denode *dep; 123 struct vnode *vp; 124 125 lwkt_gettoken(&dehash_token); 126 loop: 127 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) { 128 if (dirclust != dep->de_dirclust 129 || diroff != dep->de_diroffset 130 || dev != dep->de_dev 131 || dep->de_refcnt <= 0) { 132 continue; 133 } 134 vp = DETOV(dep); 135 if (vget(vp, LK_EXCLUSIVE)) 136 goto loop; 137 138 /* 139 * We must check to see if the inode has been ripped 140 * out from under us after blocking. 141 */ 142 for (dep = DEHASH(dev, dirclust, diroff); dep; 143 dep = dep->de_next) { 144 if (dirclust == dep->de_dirclust 145 && diroff == dep->de_diroffset 146 && dev == dep->de_dev 147 && dep->de_refcnt > 0) { 148 break; 149 } 150 } 151 if (dep == NULL || DETOV(dep) != vp) { 152 vput(vp); 153 goto loop; 154 } 155 lwkt_reltoken(&dehash_token); 156 return (dep); 157 } 158 lwkt_reltoken(&dehash_token); 159 return (NULL); 160 } 161 162 /* 163 * Try to insert specified denode into the hash table. Return 0 on success 164 * and EBUSY if there is already a denode with the same key. 165 */ 166 static 167 int 168 msdosfs_hashins(struct denode *dep) 169 { 170 struct denode **depp, *deq; 171 172 lwkt_gettoken(&dehash_token); 173 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset); 174 while ((deq = *depp) != NULL) { 175 if (deq->de_dev == dep->de_dev && 176 deq->de_dirclust == dep->de_dirclust && 177 deq->de_diroffset == dep->de_diroffset && 178 deq->de_refcnt > 0) { 179 lwkt_reltoken(&dehash_token); 180 return(EBUSY); 181 } 182 depp = &deq->de_next; 183 } 184 dep->de_next = NULL; 185 *depp = dep; 186 lwkt_reltoken(&dehash_token); 187 return(0); 188 } 189 190 static 191 void 192 msdosfs_hashrem(struct denode *dep) 193 { 194 struct denode **depp, *deq; 195 196 lwkt_gettoken(&dehash_token); 197 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset); 198 while ((deq = *depp) != NULL) { 199 if (dep == deq) 200 break; 201 depp = &deq->de_next; 202 } 203 KKASSERT(dep == deq); 204 *depp = dep->de_next; 205 dep->de_next = NULL; 206 lwkt_reltoken(&dehash_token); 207 } 208 209 void 210 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset) 211 { 212 int error; 213 214 lwkt_gettoken(&dehash_token); 215 msdosfs_hashrem(ip); 216 ip->de_dirclust = new_dirclust; 217 ip->de_diroffset = new_diroffset; 218 error = msdosfs_hashins(ip); 219 KASSERT(!error, ("msdosfs_reinsert: insertion failed %d", error)); 220 lwkt_reltoken(&dehash_token); 221 } 222 223 /* 224 * If deget() succeeds it returns with the gotten denode locked(). 225 * 226 * pmp - address of msdosfsmount structure of the filesystem containing 227 * the denode of interest. The address of 228 * the msdosfsmount structure are used. 229 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 230 * diroffset is relative to the beginning of the root directory, 231 * otherwise it is cluster relative. 232 * diroffset - offset past begin of cluster of denode we want 233 * depp - returns the address of the gotten denode. 234 */ 235 int 236 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 237 struct denode **depp) 238 { 239 int error; 240 cdev_t dev = pmp->pm_dev; 241 struct mount *mntp = pmp->pm_mountp; 242 struct direntry *direntptr; 243 struct denode *ldep; 244 struct vnode *nvp; 245 struct buf *bp; 246 247 mprintf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 248 pmp, dirclust, diroffset, depp); 249 250 /* 251 * On FAT32 filesystems, root is a (more or less) normal 252 * directory 253 */ 254 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 255 dirclust = pmp->pm_rootdirblk; 256 257 again: 258 /* 259 * See if the denode is in the denode cache. Use the location of 260 * the directory entry to compute the hash value. For subdir use 261 * address of "." entry. For root dir (if not FAT32) use cluster 262 * MSDOSFSROOT, offset MSDOSFSROOT_OFS 263 * 264 * NOTE: The check for de_refcnt > 0 below insures the denode being 265 * examined does not represent an unlinked but still open file. 266 * These files are not to be accessible even when the directory 267 * entry that represented the file happens to be reused while the 268 * deleted file is still open. 269 */ 270 ldep = msdosfs_hashget(dev, dirclust, diroffset); 271 if (ldep) { 272 *depp = ldep; 273 return (0); 274 } 275 276 /* 277 * Do the MALLOC before the getnewvnode since doing so afterward 278 * might cause a bogus v_data pointer to get dereferenced 279 * elsewhere if MALLOC should block. 280 */ 281 ldep = kmalloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO); 282 283 /* 284 * Directory entry was not in cache, have to create a vnode and 285 * copy it from the passed disk buffer. 286 */ 287 288 /* getnewvnode() does a vref() on the vnode */ 289 error = getnewvnode(VT_MSDOSFS, mntp, &nvp, VLKTIMEOUT, 0); 290 if (error) { 291 *depp = NULL; 292 kfree(ldep, M_MSDOSFSNODE); 293 return error; 294 } 295 296 ldep->de_vnode = nvp; 297 ldep->de_flag = 0; 298 ldep->de_devvp = 0; 299 ldep->de_dev = dev; 300 ldep->de_dirclust = dirclust; 301 ldep->de_diroffset = diroffset; 302 fc_purge(ldep, 0); /* init the fat cache for this denode */ 303 304 /* 305 * Insert the denode into the hash queue. If a collision occurs 306 * throw away the vnode and try again. 307 */ 308 error = msdosfs_hashins(ldep); 309 if (error == EBUSY) { 310 nvp->v_type = VBAD; 311 vx_put(nvp); 312 kfree(ldep, M_MSDOSFSNODE); 313 goto again; 314 } else if (error) { 315 nvp->v_type = VBAD; 316 vx_put(nvp); 317 kfree(ldep, M_MSDOSFSNODE); 318 *depp = NULL; 319 return (EINVAL); 320 } 321 nvp->v_data = ldep; 322 ldep->de_pmp = pmp; 323 ldep->de_refcnt = 1; 324 /* 325 * Copy the directory entry into the denode area of the vnode. 326 */ 327 if ((dirclust == MSDOSFSROOT 328 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 329 && diroffset == MSDOSFSROOT_OFS) { 330 /* 331 * Directory entry for the root directory. There isn't one, 332 * so we manufacture one. We should probably rummage 333 * through the root directory and find a label entry (if it 334 * exists), and then use the time and date from that entry 335 * as the time and date for the root denode. 336 */ 337 vsetflags(nvp, VROOT); /* should be further down XXX */ 338 339 ldep->de_Attributes = ATTR_DIRECTORY; 340 ldep->de_LowerCase = 0; 341 if (FAT32(pmp)) { 342 ldep->de_StartCluster = pmp->pm_rootdirblk; 343 /* de_FileSize will be filled in further down */ 344 } else { 345 ldep->de_StartCluster = MSDOSFSROOT; 346 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 347 } 348 /* 349 * fill in time and date so that fattime2timespec() doesn't 350 * spit up when called from msdosfs_getattr() with root 351 * denode 352 */ 353 ldep->de_CHun = 0; 354 ldep->de_CTime = 0x0000; /* 00:00:00 */ 355 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 356 | (1 << DD_DAY_SHIFT); 357 /* Jan 1, 1980 */ 358 ldep->de_ADate = ldep->de_CDate; 359 ldep->de_MTime = ldep->de_CTime; 360 ldep->de_MDate = ldep->de_CDate; 361 /* leave the other fields as garbage */ 362 } else { 363 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 364 if (error) { 365 /* 366 * The denode does not contain anything useful, so 367 * it would be wrong to leave it on its hash chain. 368 * Arrange for vput() to just forget about it. 369 */ 370 ldep->de_Name[0] = SLOT_DELETED; 371 nvp->v_type = VBAD; 372 373 vx_put(nvp); 374 *depp = NULL; 375 return (error); 376 } 377 DE_INTERNALIZE(ldep, direntptr); 378 brelse(bp); 379 } 380 381 /* 382 * Fill in a few fields of the vnode and finish filling in the 383 * denode. Then return the address of the found denode. 384 */ 385 if (ldep->de_Attributes & ATTR_DIRECTORY) { 386 /* 387 * Since DOS directory entries that describe directories 388 * have 0 in the filesize field, we take this opportunity 389 * to find out the length of the directory and plug it into 390 * the denode structure. 391 */ 392 u_long size; 393 394 /* 395 * XXX it sometimes happens that the "." entry has cluster 396 * number 0 when it shouldn't. Use the actual cluster number 397 * instead of what is written in directory entry. 398 */ 399 if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 400 kprintf("deget(): \".\" entry at clust %lu != %lu\n", 401 dirclust, ldep->de_StartCluster); 402 ldep->de_StartCluster = dirclust; 403 } 404 405 nvp->v_type = VDIR; 406 if (ldep->de_StartCluster != MSDOSFSROOT) { 407 error = pcbmap(ldep, 0xffff, NULL, &size, NULL); 408 if (error == E2BIG) { 409 ldep->de_FileSize = de_cn2off(pmp, size); 410 error = 0; 411 } else 412 kprintf("deget(): pcbmap returned %d\n", error); 413 } 414 } else 415 nvp->v_type = VREG; 416 417 ldep->de_modrev = init_va_filerev(); 418 ldep->de_devvp = pmp->pm_devvp; 419 vref(ldep->de_devvp); 420 vinitvmio(nvp, ldep->de_FileSize, PAGE_SIZE, -1); 421 /* 422 * Leave nvp locked and refd so the returned inode is effectively 423 * locked and refd. 424 */ 425 *depp = ldep; 426 return (0); 427 } 428 429 int 430 deupdat(struct denode *dep, int waitfor) 431 { 432 struct direntry dir; 433 struct timespec ts; 434 struct buf *bp; 435 struct direntry *dirp; 436 int error; 437 438 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) { 439 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS | 440 DE_MODIFIED); 441 return (0); 442 } 443 vfs_timestamp(&ts); 444 DETIMES(dep, &ts, &ts, &ts); 445 if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0) 446 return (0); 447 dep->de_flag &= ~DE_MODIFIED; 448 if (DETOV(dep)->v_flag & VROOT) 449 return (EINVAL); 450 if (dep->de_refcnt <= 0) 451 return (0); 452 error = readde(dep, &bp, &dirp); 453 if (error) 454 return (error); 455 DE_EXTERNALIZE(&dir, dep); 456 if (bcmp(dirp, &dir, sizeof(dir)) == 0) { 457 if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) { 458 brelse(bp); 459 return (0); 460 } 461 } else 462 *dirp = dir; 463 if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) 464 bp->b_flags |= B_CLUSTEROK; 465 if (waitfor) 466 error = bwrite(bp); 467 else if (vm_page_count_severe() || buf_dirty_count_severe()) 468 bawrite(bp); 469 else 470 bdwrite(bp); 471 return (error); 472 } 473 474 /* 475 * Truncate the file described by dep to the length specified by length. 476 */ 477 int 478 detrunc(struct denode *dep, u_long length, int flags) 479 { 480 int error; 481 int allerror; 482 u_long eofentry; 483 u_long chaintofree; 484 daddr_t bn; 485 int boff; 486 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 487 struct buf *bp; 488 struct msdosfsmount *pmp = dep->de_pmp; 489 490 mprintf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, 491 length, flags); 492 493 /* 494 * Disallow attempts to truncate the root directory since it is of 495 * fixed size. That's just the way dos filesystems are. We use 496 * the VROOT bit in the vnode because checking for the directory 497 * bit and a startcluster of 0 in the denode is not adequate to 498 * recognize the root directory at this point in a file or 499 * directory's life. 500 */ 501 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { 502 kprintf("detrunc(): can't truncate root directory, clust %ld, " 503 "offset %ld\n", 504 dep->de_dirclust, dep->de_diroffset); 505 return (EINVAL); 506 } 507 508 if (dep->de_FileSize < length) { 509 vnode_pager_setsize(DETOV(dep), length); 510 return deextend(dep, length); 511 } 512 513 /* 514 * If the desired length is 0 then remember the starting cluster of 515 * the file and set the StartCluster field in the directory entry 516 * to 0. If the desired length is not zero, then get the number of 517 * the last cluster in the shortened file. Then get the number of 518 * the first cluster in the part of the file that is to be freed. 519 * Then set the next cluster pointer in the last cluster of the 520 * file to CLUST_EOFE. 521 */ 522 if (length == 0) { 523 chaintofree = dep->de_StartCluster; 524 dep->de_StartCluster = 0; 525 eofentry = ~0; 526 } else { 527 error = pcbmap(dep, de_clcount(pmp, length) - 1, 528 NULL, &eofentry, NULL); 529 if (error) { 530 mprintf("detrunc(): pcbmap fails %d\n", error); 531 return (error); 532 } 533 } 534 535 fc_purge(dep, de_clcount(pmp, length)); 536 537 /* 538 * If the new length is not a multiple of the cluster size then we 539 * must zero the tail end of the new last cluster in case it 540 * becomes part of the file again because of a seek. 541 */ 542 if ((boff = length & pmp->pm_crbomask) != 0) { 543 if (isadir) { 544 bn = cntobn(pmp, eofentry); 545 error = bread(pmp->pm_devvp, de_bn2doff(pmp, bn), 546 pmp->pm_bpcluster, &bp); 547 } else { 548 u_long cn = de_cluster(pmp, length); 549 error = bread(DETOV(dep), de_cn2doff(pmp, cn), 550 pmp->pm_bpcluster, &bp); 551 } 552 if (error) { 553 brelse(bp); 554 mprintf("detrunc(): bread fails %d\n", error); 555 return (error); 556 } 557 memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 558 if ((flags & IO_SYNC) != 0) 559 bwrite(bp); 560 else 561 bdwrite(bp); 562 } 563 564 /* 565 * Write out the updated directory entry. Even if the update fails 566 * we free the trailing clusters. 567 */ 568 dep->de_FileSize = length; 569 if (!isadir) 570 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 571 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster); 572 #ifdef MSDOSFS_DEBUG 573 if (allerror) 574 kprintf("detrunc(): vtruncbuf error %d\n", allerror); 575 #endif 576 error = deupdat(dep, !DOINGASYNC((DETOV(dep)))); 577 if (error != 0 && allerror == 0) 578 allerror = error; 579 mprintf("detrunc(): allerror %d, eofentry %lu\n", 580 allerror, eofentry); 581 582 /* 583 * If we need to break the cluster chain for the file then do it 584 * now. 585 */ 586 if (eofentry != ~0) { 587 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 588 &chaintofree, CLUST_EOFE); 589 if (error) { 590 mprintf("detrunc(): fatentry errors %d\n", error); 591 return (error); 592 } 593 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 594 eofentry); 595 } 596 597 /* 598 * Now free the clusters removed from the file because of the 599 * truncation. 600 */ 601 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 602 freeclusterchain(pmp, chaintofree); 603 604 return (allerror); 605 } 606 607 /* 608 * Extend the file described by dep to length specified by length. 609 */ 610 int 611 deextend(struct denode *dep, u_long length) 612 { 613 struct msdosfsmount *pmp = dep->de_pmp; 614 u_long count; 615 int error; 616 617 /* 618 * The root of a DOS filesystem cannot be extended. 619 */ 620 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) 621 return (EINVAL); 622 623 /* 624 * Directories cannot be extended. 625 */ 626 if (dep->de_Attributes & ATTR_DIRECTORY) 627 return (EISDIR); 628 629 if (length <= dep->de_FileSize) 630 panic("deextend: file too large"); 631 632 /* 633 * Compute the number of clusters to allocate. 634 */ 635 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 636 if (count > 0) { 637 if (count > pmp->pm_freeclustercount) 638 return (ENOSPC); 639 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 640 if (error) { 641 /* truncate the added clusters away again */ 642 detrunc(dep, dep->de_FileSize, 0); 643 return (error); 644 } 645 } 646 dep->de_FileSize = length; 647 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 648 return (deupdat(dep, !DOINGASYNC(DETOV(dep)))); 649 } 650 651 int 652 msdosfs_reclaim(struct vop_reclaim_args *ap) 653 { 654 struct vnode *vp = ap->a_vp; 655 struct denode *dep = VTODE(vp); 656 657 mprintf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 658 dep, dep ? (char *)dep->de_Name : "?", dep ? dep->de_refcnt : -1); 659 660 if (prtactive && VREFCNT(vp) > 1) 661 vprint("msdosfs_reclaim(): pushing active", vp); 662 /* 663 * Remove the denode from its hash chain. 664 */ 665 vp->v_data = NULL; 666 if (dep) { 667 msdosfs_hashrem(dep); 668 if (dep->de_devvp) { 669 vrele(dep->de_devvp); 670 dep->de_devvp = 0; 671 } 672 kfree(dep, M_MSDOSFSNODE); 673 } 674 return (0); 675 } 676 677 int 678 msdosfs_inactive(struct vop_inactive_args *ap) 679 { 680 struct vnode *vp = ap->a_vp; 681 struct denode *dep = VTODE(vp); 682 int error = 0; 683 684 mprintf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", 685 dep, (dep ? dep->de_Name[0] : 0)); 686 687 if (prtactive && VREFCNT(vp) > 1) 688 vprint("msdosfs_inactive(): pushing active", vp); 689 690 /* 691 * Ignore denodes related to stale file handles. 692 */ 693 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY) 694 goto out; 695 696 /* 697 * If the file has been deleted and it is on a read/write 698 * filesystem, then truncate the file, and mark the directory slot 699 * as empty. (This may not be necessary for the dos filesystem.) 700 */ 701 mprintf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n", 702 dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag, 703 (unsigned long long)MNT_RDONLY); 704 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 705 error = detrunc(dep, (u_long) 0, 0); 706 dep->de_flag |= DE_UPDATE; 707 dep->de_Name[0] = SLOT_DELETED; 708 } 709 deupdat(dep, 0); 710 711 out: 712 /* 713 * If we are done with the denode, reclaim it 714 * so that it can be reused immediately. 715 */ 716 mprintf("msdosfs_inactive(): v_refcnt 0x%08x, de_Name[0] %x\n", 717 vp->v_refcnt, (dep ? dep->de_Name[0] : 0)); 718 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY) 719 vrecycle(vp); 720 return (error); 721 } 722