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(struct cdev *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 struct cdev *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 struct cdev *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 if (error) { 516 brelse(bp); 517 #ifdef MSDOSFS_DEBUG 518 printf("detrunc(): bread fails %d\n", error); 519 #endif 520 return (error); 521 } 522 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 523 if (flags & IO_SYNC) 524 bwrite(bp); 525 else 526 bdwrite(bp); 527 } 528 } 529 530 /* 531 * Write out the updated directory entry. Even if the update fails 532 * we free the trailing clusters. 533 */ 534 dep->de_FileSize = length; 535 if (!isadir) 536 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 537 allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster); 538 #ifdef MSDOSFS_DEBUG 539 if (allerror) 540 printf("detrunc(): vtruncbuf error %d\n", allerror); 541 #endif 542 error = deupdat(dep, 1); 543 if (error && (allerror == 0)) 544 allerror = error; 545 #ifdef MSDOSFS_DEBUG 546 printf("detrunc(): allerror %d, eofentry %lu\n", 547 allerror, eofentry); 548 #endif 549 550 /* 551 * If we need to break the cluster chain for the file then do it 552 * now. 553 */ 554 if (eofentry != ~0) { 555 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 556 &chaintofree, CLUST_EOFE); 557 if (error) { 558 #ifdef MSDOSFS_DEBUG 559 printf("detrunc(): fatentry errors %d\n", error); 560 #endif 561 return (error); 562 } 563 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 564 eofentry); 565 } 566 567 /* 568 * Now free the clusters removed from the file because of the 569 * truncation. 570 */ 571 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 572 freeclusterchain(pmp, chaintofree); 573 574 return (allerror); 575 } 576 577 /* 578 * Extend the file described by dep to length specified by length. 579 */ 580 int 581 deextend(dep, length, cred) 582 struct denode *dep; 583 u_long length; 584 struct ucred *cred; 585 { 586 struct msdosfsmount *pmp = dep->de_pmp; 587 u_long count; 588 int error; 589 590 /* 591 * The root of a DOS filesystem cannot be extended. 592 */ 593 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 594 return (EINVAL); 595 596 /* 597 * Directories cannot be extended. 598 */ 599 if (dep->de_Attributes & ATTR_DIRECTORY) 600 return (EISDIR); 601 602 if (length <= dep->de_FileSize) 603 panic("deextend: file too large"); 604 605 /* 606 * Compute the number of clusters to allocate. 607 */ 608 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 609 if (count > 0) { 610 if (count > pmp->pm_freeclustercount) 611 return (ENOSPC); 612 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 613 if (error) { 614 /* truncate the added clusters away again */ 615 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 616 return (error); 617 } 618 } 619 dep->de_FileSize = length; 620 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 621 return (deupdat(dep, 1)); 622 } 623 624 /* 625 * Move a denode to its correct hash queue after the file it represents has 626 * been moved to a new directory. 627 */ 628 void 629 reinsert(dep) 630 struct denode *dep; 631 { 632 /* 633 * Fix up the denode cache. If the denode is for a directory, 634 * there is nothing to do since the hash is based on the starting 635 * cluster of the directory file and that hasn't changed. If for a 636 * file the hash is based on the location of the directory entry, 637 * so we must remove it from the cache and re-enter it with the 638 * hash based on the new location of the directory entry. 639 */ 640 if (dep->de_Attributes & ATTR_DIRECTORY) 641 return; 642 msdosfs_hashrem(dep); 643 msdosfs_hashins(dep); 644 } 645 646 int 647 msdosfs_reclaim(ap) 648 struct vop_reclaim_args /* { 649 struct vnode *a_vp; 650 } */ *ap; 651 { 652 struct vnode *vp = ap->a_vp; 653 struct denode *dep = VTODE(vp); 654 655 #ifdef MSDOSFS_DEBUG 656 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 657 dep, dep->de_Name, dep->de_refcnt); 658 #endif 659 660 if (prtactive && vrefcnt(vp) != 0) 661 vprint("msdosfs_reclaim(): pushing active", vp); 662 /* 663 * Remove the denode from its hash chain. 664 */ 665 msdosfs_hashrem(dep); 666 /* 667 * Purge old data structures associated with the denode. 668 */ 669 if (dep->de_devvp) { 670 vrele(dep->de_devvp); 671 dep->de_devvp = 0; 672 } 673 #if 0 /* XXX */ 674 dep->de_flag = 0; 675 #endif 676 FREE(dep, M_MSDOSFSNODE); 677 vp->v_data = NULL; 678 679 return (0); 680 } 681 682 int 683 msdosfs_inactive(ap) 684 struct vop_inactive_args /* { 685 struct vnode *a_vp; 686 struct thread *a_td; 687 } */ *ap; 688 { 689 struct vnode *vp = ap->a_vp; 690 struct denode *dep = VTODE(vp); 691 struct thread *td = ap->a_td; 692 int error = 0; 693 694 #ifdef MSDOSFS_DEBUG 695 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 696 #endif 697 698 if (prtactive && vrefcnt(vp) != 0) 699 vprint("msdosfs_inactive(): pushing active", vp); 700 701 /* 702 * Ignore denodes related to stale file handles. 703 */ 704 if (dep->de_Name[0] == SLOT_DELETED) 705 goto out; 706 707 /* 708 * If the file has been deleted and it is on a read/write 709 * filesystem, then truncate the file, and mark the directory slot 710 * as empty. (This may not be necessary for the dos filesystem.) 711 */ 712 #ifdef MSDOSFS_DEBUG 713 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 714 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 715 #endif 716 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 717 error = detrunc(dep, (u_long) 0, 0, NOCRED, td); 718 dep->de_flag |= DE_UPDATE; 719 dep->de_Name[0] = SLOT_DELETED; 720 } 721 deupdat(dep, 0); 722 723 out: 724 VOP_UNLOCK(vp, 0, td); 725 /* 726 * If we are done with the denode, reclaim it 727 * so that it can be reused immediately. 728 */ 729 #ifdef MSDOSFS_DEBUG 730 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 731 vrefcnt(vp), dep->de_Name[0]); 732 #endif 733 if (dep->de_Name[0] == SLOT_DELETED) 734 vrecycle(vp, NULL, td); 735 return (error); 736 } 737