xref: /netbsd-src/sys/fs/msdosfs/msdosfs_denode.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: msdosfs_denode.c,v 1.10 2005/08/29 23:57:35 xtraeme Exp $	*/
2 
3 /*-
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
42  * The author supplies this software to be publicly redistributed on the
43  * understanding that the author is not responsible for the correct
44  * functioning of this software in any circumstances and is not liable for
45  * any damages caused by this software.
46  *
47  * October 1992
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.10 2005/08/29 23:57:35 xtraeme Exp $");
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/mount.h>
56 #include <sys/malloc.h>
57 #include <sys/pool.h>
58 #include <sys/proc.h>
59 #include <sys/buf.h>
60 #include <sys/vnode.h>
61 #include <sys/kernel.h>		/* defines "time" */
62 #include <sys/dirent.h>
63 #include <sys/namei.h>
64 
65 #include <uvm/uvm_extern.h>
66 
67 #include <fs/msdosfs/bpb.h>
68 #include <fs/msdosfs/msdosfsmount.h>
69 #include <fs/msdosfs/direntry.h>
70 #include <fs/msdosfs/denode.h>
71 #include <fs/msdosfs/fat.h>
72 
73 LIST_HEAD(ihashhead, denode) *dehashtbl;
74 u_long dehash;			/* size of hash table - 1 */
75 #define	DEHASH(dev, dcl, doff) \
76     (((dev) + (dcl) + (doff) / sizeof(struct direntry)) & dehash)
77 
78 struct simplelock msdosfs_ihash_slock;
79 
80 POOL_INIT(msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0, "msdosnopl",
81     &pool_allocator_nointr);
82 
83 extern int prtactive;
84 
85 static const struct genfs_ops msdosfs_genfsops = {
86 	.gop_size = genfs_size,
87 	.gop_alloc = msdosfs_gop_alloc,
88 	.gop_write = genfs_gop_write,
89 	.gop_markupdate = msdosfs_gop_markupdate,
90 };
91 
92 static struct denode *msdosfs_hashget(dev_t, u_long, u_long);
93 static void msdosfs_hashins(struct denode *);
94 static void msdosfs_hashrem(struct denode *);
95 
96 #ifdef _LKM
97 MALLOC_DECLARE(M_MSDOSFSFAT);
98 #endif
99 
100 void
101 msdosfs_init()
102 {
103 #ifdef _LKM
104 	malloc_type_attach(M_MSDOSFSMNT);
105 	malloc_type_attach(M_MSDOSFSFAT);
106 	pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
107 	    "msdosnopl", &pool_allocator_nointr);
108 #endif
109 	dehashtbl = hashinit(desiredvnodes / 2, HASH_LIST, M_MSDOSFSMNT,
110 	    M_WAITOK, &dehash);
111 	simple_lock_init(&msdosfs_ihash_slock);
112 }
113 
114 /*
115  * Reinitialize inode hash table.
116  */
117 
118 void
119 msdosfs_reinit()
120 {
121 	struct denode *dep;
122 	struct ihashhead *oldhash, *hash;
123 	u_long oldmask, mask, val;
124 	int i;
125 
126 	hash = hashinit(desiredvnodes / 2, HASH_LIST, M_MSDOSFSMNT, M_WAITOK,
127 	    &mask);
128 
129 	simple_lock(&msdosfs_ihash_slock);
130 	oldhash = dehashtbl;
131 	oldmask = dehash;
132 	dehashtbl = hash;
133 	dehash = mask;
134 	for (i = 0; i <= oldmask; i++) {
135 		while ((dep = LIST_FIRST(&oldhash[i])) != NULL) {
136 			LIST_REMOVE(dep, de_hash);
137 			val = DEHASH(dep->de_dev, dep->de_dirclust,
138 			    dep->de_diroffset);
139 			LIST_INSERT_HEAD(&hash[val], dep, de_hash);
140 		}
141 	}
142 	simple_unlock(&msdosfs_ihash_slock);
143 	hashdone(oldhash, M_MSDOSFSMNT);
144 }
145 
146 void
147 msdosfs_done()
148 {
149 	hashdone(dehashtbl, M_MSDOSFSMNT);
150 #ifdef _LKM
151 	pool_destroy(&msdosfs_denode_pool);
152 	malloc_type_detach(M_MSDOSFSFAT);
153 	malloc_type_detach(M_MSDOSFSMNT);
154 #endif
155 }
156 
157 static struct denode *
158 msdosfs_hashget(dev, dirclust, diroff)
159 	dev_t dev;
160 	u_long dirclust;
161 	u_long diroff;
162 {
163 	struct denode *dep;
164 	struct vnode *vp;
165 
166 loop:
167 	simple_lock(&msdosfs_ihash_slock);
168 	LIST_FOREACH(dep, &dehashtbl[DEHASH(dev, dirclust, diroff)], de_hash) {
169 		if (dirclust == dep->de_dirclust &&
170 		    diroff == dep->de_diroffset &&
171 		    dev == dep->de_dev &&
172 		    dep->de_refcnt != 0) {
173 			vp = DETOV(dep);
174 			simple_lock(&vp->v_interlock);
175 			simple_unlock(&msdosfs_ihash_slock);
176 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
177 				goto loop;
178 			return (dep);
179 		}
180 	}
181 	simple_unlock(&msdosfs_ihash_slock);
182 	return (NULL);
183 }
184 
185 static void
186 msdosfs_hashins(dep)
187 	struct denode *dep;
188 {
189 	struct ihashhead *depp;
190 	int val;
191 
192 	simple_lock(&msdosfs_ihash_slock);
193 	val = DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
194 	depp = &dehashtbl[val];
195 	LIST_INSERT_HEAD(depp, dep, de_hash);
196 	simple_unlock(&msdosfs_ihash_slock);
197 }
198 
199 static void
200 msdosfs_hashrem(dep)
201 	struct denode *dep;
202 {
203 	simple_lock(&msdosfs_ihash_slock);
204 	LIST_REMOVE(dep, de_hash);
205 	simple_unlock(&msdosfs_ihash_slock);
206 }
207 
208 /*
209  * If deget() succeeds it returns with the gotten denode locked().
210  *
211  * pmp	     - address of msdosfsmount structure of the filesystem containing
212  *	       the denode of interest.  The pm_dev field and the address of
213  *	       the msdosfsmount structure are used.
214  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
215  *	       diroffset is relative to the beginning of the root directory,
216  *	       otherwise it is cluster relative.
217  * diroffset - offset past begin of cluster of denode we want
218  * depp	     - returns the address of the gotten denode.
219  */
220 int
221 deget(pmp, dirclust, diroffset, depp)
222 	struct msdosfsmount *pmp;	/* so we know the maj/min number */
223 	u_long dirclust;		/* cluster this dir entry came from */
224 	u_long diroffset;		/* index of entry within the cluster */
225 	struct denode **depp;		/* returns the addr of the gotten denode */
226 {
227 	int error;
228 	extern int (**msdosfs_vnodeop_p)(void *);
229 	struct direntry *direntptr;
230 	struct denode *ldep;
231 	struct vnode *nvp;
232 	struct buf *bp;
233 
234 #ifdef MSDOSFS_DEBUG
235 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
236 	    pmp, dirclust, diroffset, depp);
237 #endif
238 
239 	/*
240 	 * On FAT32 filesystems, root is a (more or less) normal
241 	 * directory
242 	 */
243 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
244 		dirclust = pmp->pm_rootdirblk;
245 
246 	/*
247 	 * See if the denode is in the denode cache. Use the location of
248 	 * the directory entry to compute the hash value. For subdir use
249 	 * address of "." entry. For root dir (if not FAT32) use cluster
250 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
251 	 *
252 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
253 	 * examined does not represent an unlinked but still open file.
254 	 * These files are not to be accessible even when the directory
255 	 * entry that represented the file happens to be reused while the
256 	 * deleted file is still open.
257 	 */
258 	ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset);
259 	if (ldep) {
260 		*depp = ldep;
261 		return (0);
262 	}
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(VT_MSDOSFS, pmp->pm_mountp,
270 			    msdosfs_vnodeop_p, &nvp);
271 	if (error) {
272 		*depp = 0;
273 		return (error);
274 	}
275 	ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
276 	memset(ldep, 0, sizeof *ldep);
277 	nvp->v_data = ldep;
278 	ldep->de_vnode = nvp;
279 	ldep->de_flag = 0;
280 	ldep->de_devvp = 0;
281 	ldep->de_lockf = 0;
282 	ldep->de_dev = pmp->pm_dev;
283 	ldep->de_dirclust = dirclust;
284 	ldep->de_diroffset = diroffset;
285 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
286 
287 	/*
288 	 * Insert the denode into the hash queue and lock the denode so it
289 	 * can't be accessed until we've read it in and have done what we
290 	 * need to it.
291 	 */
292 	vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
293 	msdosfs_hashins(ldep);
294 
295 	ldep->de_pmp = pmp;
296 	ldep->de_devvp = pmp->pm_devvp;
297 	ldep->de_refcnt = 1;
298 	/*
299 	 * Copy the directory entry into the denode area of the vnode.
300 	 */
301 	if ((dirclust == MSDOSFSROOT
302 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
303 	    && diroffset == MSDOSFSROOT_OFS) {
304 		/*
305 		 * Directory entry for the root directory. There isn't one,
306 		 * so we manufacture one. We should probably rummage
307 		 * through the root directory and find a label entry (if it
308 		 * exists), and then use the time and date from that entry
309 		 * as the time and date for the root denode.
310 		 */
311 		nvp->v_flag |= VROOT; /* should be further down		XXX */
312 
313 		ldep->de_Attributes = ATTR_DIRECTORY;
314 		if (FAT32(pmp))
315 			ldep->de_StartCluster = pmp->pm_rootdirblk;
316 			/* de_FileSize will be filled in further down */
317 		else {
318 			ldep->de_StartCluster = MSDOSFSROOT;
319 			ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
320 		}
321 		/*
322 		 * fill in time and date so that dos2unixtime() doesn't
323 		 * spit up when called from msdosfs_getattr() with root
324 		 * denode
325 		 */
326 		ldep->de_CHun = 0;
327 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
328 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
329 		    | (1 << DD_DAY_SHIFT);
330 		/* Jan 1, 1980	 */
331 		ldep->de_ADate = ldep->de_CDate;
332 		ldep->de_MTime = ldep->de_CTime;
333 		ldep->de_MDate = ldep->de_CDate;
334 		/* leave the other fields as garbage */
335 	} else {
336 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
337 		if (error)
338 			return (error);
339 		DE_INTERNALIZE(ldep, direntptr);
340 		brelse(bp);
341 	}
342 
343 	/*
344 	 * Fill in a few fields of the vnode and finish filling in the
345 	 * denode.  Then return the address of the found denode.
346 	 */
347 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
348 		/*
349 		 * Since DOS directory entries that describe directories
350 		 * have 0 in the filesize field, we take this opportunity
351 		 * to find out the length of the directory and plug it into
352 		 * the denode structure.
353 		 */
354 		u_long size;
355 
356 		nvp->v_type = VDIR;
357 		if (ldep->de_StartCluster != MSDOSFSROOT) {
358 			error = pcbmap(ldep, CLUST_END, 0, &size, 0);
359 			if (error == E2BIG) {
360 				ldep->de_FileSize = de_cn2off(pmp, size);
361 				error = 0;
362 			} else
363 				printf("deget(): pcbmap returned %d\n", error);
364 		}
365 	} else
366 		nvp->v_type = VREG;
367 	genfs_node_init(nvp, &msdosfs_genfsops);
368 	VREF(ldep->de_devvp);
369 	*depp = ldep;
370 	nvp->v_size = ldep->de_FileSize;
371 	return (0);
372 }
373 
374 int
375 deupdat(dep, waitfor)
376 	struct denode *dep;
377 	int waitfor;
378 {
379 
380 	return (VOP_UPDATE(DETOV(dep), NULL, NULL, waitfor ? UPDATE_WAIT : 0));
381 }
382 
383 /*
384  * Truncate the file described by dep to the length specified by length.
385  */
386 int
387 detrunc(dep, length, flags, cred, p)
388 	struct denode *dep;
389 	u_long length;
390 	int flags;
391 	struct ucred *cred;
392 	struct proc *p;
393 {
394 	int error;
395 	int allerror;
396 	u_long eofentry;
397 	u_long chaintofree;
398 	daddr_t bn, lastblock;
399 	int boff;
400 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
401 	struct buf *bp;
402 	struct msdosfsmount *pmp = dep->de_pmp;
403 
404 #ifdef MSDOSFS_DEBUG
405 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
406 #endif
407 
408 	/*
409 	 * Disallow attempts to truncate the root directory since it is of
410 	 * fixed size.  That's just the way dos filesystems are.  We use
411 	 * the VROOT bit in the vnode because checking for the directory
412 	 * bit and a startcluster of 0 in the denode is not adequate to
413 	 * recognize the root directory at this point in a file or
414 	 * directory's life.
415 	 */
416 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
417 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
418 		    dep->de_dirclust, dep->de_diroffset);
419 		return (EINVAL);
420 	}
421 
422 	uvm_vnp_setsize(DETOV(dep), length);
423 
424 	if (dep->de_FileSize < length)
425 		return (deextend(dep, length, cred));
426 	lastblock = de_clcount(pmp, length) - 1;
427 
428 	/*
429 	 * If the desired length is 0 then remember the starting cluster of
430 	 * the file and set the StartCluster field in the directory entry
431 	 * to 0.  If the desired length is not zero, then get the number of
432 	 * the last cluster in the shortened file.  Then get the number of
433 	 * the first cluster in the part of the file that is to be freed.
434 	 * Then set the next cluster pointer in the last cluster of the
435 	 * file to CLUST_EOFE.
436 	 */
437 	if (length == 0) {
438 		chaintofree = dep->de_StartCluster;
439 		dep->de_StartCluster = 0;
440 		eofentry = ~0;
441 	} else {
442 		error = pcbmap(dep, lastblock, 0, &eofentry, 0);
443 		if (error) {
444 #ifdef MSDOSFS_DEBUG
445 			printf("detrunc(): pcbmap fails %d\n", error);
446 #endif
447 			return (error);
448 		}
449 	}
450 
451 	fc_purge(dep, lastblock + 1);
452 
453 	/*
454 	 * If the new length is not a multiple of the cluster size then we
455 	 * must zero the tail end of the new last cluster in case it
456 	 * becomes part of the file again because of a seek.
457 	 */
458 	if ((boff = length & pmp->pm_crbomask) != 0) {
459 		if (isadir) {
460 			bn = cntobn(pmp, eofentry);
461 			error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
462 			    NOCRED, &bp);
463 			if (error) {
464 				brelse(bp);
465 #ifdef MSDOSFS_DEBUG
466 				printf("detrunc(): bread fails %d\n", error);
467 #endif
468 				return (error);
469 			}
470 			memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
471 			if (flags & IO_SYNC)
472 				bwrite(bp);
473 			else
474 				bdwrite(bp);
475 		} else {
476 			uvm_vnp_zerorange(DETOV(dep), length,
477 					  pmp->pm_bpcluster - boff);
478 		}
479 	}
480 
481 	/*
482 	 * Write out the updated directory entry.  Even if the update fails
483 	 * we free the trailing clusters.
484 	 */
485 	dep->de_FileSize = length;
486 	if (!isadir)
487 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
488 	vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
489 	allerror = deupdat(dep, 1);
490 #ifdef MSDOSFS_DEBUG
491 	printf("detrunc(): allerror %d, eofentry %lu\n",
492 	       allerror, eofentry);
493 #endif
494 
495 	/*
496 	 * If we need to break the cluster chain for the file then do it
497 	 * now.
498 	 */
499 	if (eofentry != ~0) {
500 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
501 				 &chaintofree, CLUST_EOFE);
502 		if (error) {
503 #ifdef MSDOSFS_DEBUG
504 			printf("detrunc(): fatentry errors %d\n", error);
505 #endif
506 			return (error);
507 		}
508 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
509 			    eofentry);
510 	}
511 
512 	/*
513 	 * Now free the clusters removed from the file because of the
514 	 * truncation.
515 	 */
516 	if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
517 		freeclusterchain(pmp, chaintofree);
518 
519 	return (allerror);
520 }
521 
522 /*
523  * Extend the file described by dep to length specified by length.
524  */
525 int
526 deextend(dep, length, cred)
527 	struct denode *dep;
528 	u_long length;
529 	struct ucred *cred;
530 {
531 	struct msdosfsmount *pmp = dep->de_pmp;
532 	u_long count, osize;
533 	int error;
534 
535 	/*
536 	 * The root of a DOS filesystem cannot be extended.
537 	 */
538 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
539 		return (EINVAL);
540 
541 	/*
542 	 * Directories cannot be extended.
543 	 */
544 	if (dep->de_Attributes & ATTR_DIRECTORY)
545 		return (EISDIR);
546 
547 	if (length <= dep->de_FileSize)
548 		panic("deextend: file too large");
549 
550 	/*
551 	 * Compute the number of clusters to allocate.
552 	 */
553 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
554 	if (count > 0) {
555 		if (count > pmp->pm_freeclustercount)
556 			return (ENOSPC);
557 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
558 		if (error) {
559 			/* truncate the added clusters away again */
560 			(void) detrunc(dep, dep->de_FileSize, 0, cred, NULL);
561 			return (error);
562 		}
563 	}
564 
565 	osize = dep->de_FileSize;
566 	dep->de_FileSize = length;
567 	uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
568 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
569 	uvm_vnp_zerorange(DETOV(dep), (off_t)osize,
570 	    (size_t)(dep->de_FileSize - osize));
571 	return (deupdat(dep, 1));
572 }
573 
574 /*
575  * Move a denode to its correct hash queue after the file it represents has
576  * been moved to a new directory.
577  */
578 void
579 reinsert(dep)
580 	struct denode *dep;
581 {
582 	/*
583 	 * Fix up the denode cache.  If the denode is for a directory,
584 	 * there is nothing to do since the hash is based on the starting
585 	 * cluster of the directory file and that hasn't changed.  If for a
586 	 * file the hash is based on the location of the directory entry,
587 	 * so we must remove it from the cache and re-enter it with the
588 	 * hash based on the new location of the directory entry.
589 	 */
590 	if (dep->de_Attributes & ATTR_DIRECTORY)
591 		return;
592 	msdosfs_hashrem(dep);
593 	msdosfs_hashins(dep);
594 }
595 
596 int
597 msdosfs_reclaim(v)
598 	void *v;
599 {
600 	struct vop_reclaim_args /* {
601 		struct vnode *a_vp;
602 	} */ *ap = v;
603 	struct vnode *vp = ap->a_vp;
604 	struct denode *dep = VTODE(vp);
605 
606 #ifdef MSDOSFS_DEBUG
607 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
608 	    dep, dep->de_Name, dep->de_refcnt);
609 #endif
610 
611 	if (prtactive && vp->v_usecount != 0)
612 		vprint("msdosfs_reclaim(): pushing active", vp);
613 	/*
614 	 * Remove the denode from its hash chain.
615 	 */
616 	msdosfs_hashrem(dep);
617 	/*
618 	 * Purge old data structures associated with the denode.
619 	 */
620 	cache_purge(vp);
621 	if (dep->de_devvp) {
622 		vrele(dep->de_devvp);
623 		dep->de_devvp = 0;
624 	}
625 #if 0 /* XXX */
626 	dep->de_flag = 0;
627 #endif
628 	pool_put(&msdosfs_denode_pool, dep);
629 	vp->v_data = NULL;
630 	return (0);
631 }
632 
633 int
634 msdosfs_inactive(v)
635 	void *v;
636 {
637 	struct vop_inactive_args /* {
638 		struct vnode *a_vp;
639 		struct proc *a_p;
640 	} */ *ap = v;
641 	struct proc *p = ap->a_p;
642 	struct vnode *vp = ap->a_vp;
643 	struct denode *dep = VTODE(vp);
644 	int error = 0;
645 
646 #ifdef MSDOSFS_DEBUG
647 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
648 #endif
649 
650 	if (prtactive && vp->v_usecount != 0)
651 		vprint("msdosfs_inactive(): pushing active", vp);
652 
653 	/*
654 	 * Get rid of denodes related to stale file handles.
655 	 */
656 	if (dep->de_Name[0] == SLOT_DELETED)
657 		goto out;
658 
659 	/*
660 	 * If the file has been deleted and it is on a read/write
661 	 * filesystem, then truncate the file, and mark the directory slot
662 	 * as empty.  (This may not be necessary for the dos filesystem.)
663 	 */
664 #ifdef MSDOSFS_DEBUG
665 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
666 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag,
667 		(vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
668 #endif
669 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
670 		if (dep->de_FileSize != 0) {
671 			error = detrunc(dep, (u_long)0, 0, NOCRED, NULL);
672 		}
673 		dep->de_Name[0] = SLOT_DELETED;
674 	}
675 	deupdat(dep, 0);
676 out:
677 	VOP_UNLOCK(vp, 0);
678 	/*
679 	 * If we are done with the denode, reclaim it
680 	 * so that it can be reused immediately.
681 	 */
682 #ifdef MSDOSFS_DEBUG
683 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
684 		vp->v_usecount, dep->de_Name[0]);
685 #endif
686 	if (dep->de_Name[0] == SLOT_DELETED)
687 		vrecycle(vp, (struct simplelock *)0, p);
688 	return (error);
689 }
690 
691 int
692 msdosfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
693     struct ucred *cred)
694 {
695 	return 0;
696 }
697 
698 void
699 msdosfs_gop_markupdate(struct vnode *vp, int flags)
700 {
701 	u_long mask = 0;
702 
703 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
704 		mask = DE_ACCESS;
705 	}
706 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
707 		mask |= DE_UPDATE;
708 	}
709 	if (mask) {
710 		struct denode *dep = VTODE(vp);
711 
712 		dep->de_flag |= mask;
713 	}
714 }
715