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