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