xref: /dragonfly/sys/vfs/msdosfs/msdosfs_denode.c (revision aa8d5dcb)
1 /* $FreeBSD: src/sys/msdosfs/msdosfs_denode.c,v 1.47.2.3 2002/08/22 16:20:15 trhodes Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_denode.c,v 1.10 2004/03/01 06:33:21 dillon Exp $ */
3 /*	$NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $	*/
4 
5 /*-
6  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8  * All rights reserved.
9  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by TooLs GmbH.
22  * 4. The name of TooLs GmbH may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 /*
37  * Written by Paul Popelka (paulp@uts.amdahl.com)
38  *
39  * You can do anything you want with this software, just don't say you wrote
40  * it, and don't remove this notice.
41  *
42  * This software is provided "as is".
43  *
44  * The author supplies this software to be publicly redistributed on the
45  * understanding that the author is not responsible for the correct
46  * functioning of this software in any circumstances and is not liable for
47  * any damages caused by this software.
48  *
49  * October 1992
50  */
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/mount.h>
56 #include <sys/malloc.h>
57 #include <sys/proc.h>
58 #include <sys/buf.h>
59 #include <sys/vnode.h>
60 
61 #include <vm/vm.h>
62 #include <vm/vm_extern.h>
63 
64 #include "bpb.h"
65 #include "msdosfsmount.h"
66 #include "direntry.h"
67 #include "denode.h"
68 #include "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 lwkt_token dehash_token;
77 
78 union _qcvt {
79 	quad_t qcvt;
80 	long val[2];
81 };
82 #define SETHIGH(q, h) { \
83 	union _qcvt tmp; \
84 	tmp.qcvt = (q); \
85 	tmp.val[_QUAD_HIGHWORD] = (h); \
86 	(q) = tmp.qcvt; \
87 }
88 #define SETLOW(q, l) { \
89 	union _qcvt tmp; \
90 	tmp.qcvt = (q); \
91 	tmp.val[_QUAD_LOWWORD] = (l); \
92 	(q) = tmp.qcvt; \
93 }
94 
95 static struct denode *
96 		msdosfs_hashget (dev_t dev, u_long dirclust,
97 				     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 	dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, &dehash);
107 	lwkt_token_init(&dehash_token);
108 	return (0);
109 }
110 
111 int
112 msdosfs_uninit(vfsp)
113 	struct vfsconf *vfsp;
114 {
115 
116 	if (dehashtbl)
117 		free(dehashtbl, M_MSDOSFSMNT);
118 	return (0);
119 }
120 
121 static struct denode *
122 msdosfs_hashget(dev, dirclust, diroff)
123 	dev_t dev;
124 	u_long dirclust;
125 	u_long diroff;
126 {
127 	struct thread *td = curthread;	/* XXX */
128 	struct denode *dep;
129 	lwkt_tokref ilock;
130 	lwkt_tokref vlock;
131 	struct vnode *vp;
132 
133 	lwkt_gettoken(&ilock, &dehash_token);
134 loop:
135 	for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
136 		if (dirclust != dep->de_dirclust
137 		    || diroff != dep->de_diroffset
138 		    || dev != dep->de_dev
139 		    || dep->de_refcnt == 0) {
140 			continue;
141 		}
142 		vp = DETOV(dep);
143 		lwkt_gettoken(&vlock, vp->v_interlock);
144 		/*
145 		 * We must check to see if the inode has been ripped
146 		 * out from under us after blocking.
147 		 */
148 		for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
149 			if (dirclust == dep->de_dirclust
150 			    && diroff == dep->de_diroffset
151 			    && dev == dep->de_dev
152 			    && dep->de_refcnt != 0) {
153 				break;
154 			}
155 		}
156 		if (dep == NULL || DETOV(dep) != vp) {
157 			lwkt_reltoken(&vlock);
158 			goto loop;
159 		}
160 		if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td))
161 			goto loop;
162 		lwkt_reltoken(&ilock);
163 		return (dep);
164 	}
165 	lwkt_reltoken(&ilock);
166 	return (NULL);
167 }
168 
169 static void
170 msdosfs_hashins(dep)
171 	struct denode *dep;
172 {
173 	struct denode **depp, *deq;
174 	lwkt_tokref ilock;
175 
176 	lwkt_gettoken(&ilock, &dehash_token);
177 	depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
178 	deq = *depp;
179 	if (deq)
180 		deq->de_prev = &dep->de_next;
181 	dep->de_next = deq;
182 	dep->de_prev = depp;
183 	*depp = dep;
184 	lwkt_reltoken(&ilock);
185 }
186 
187 static void
188 msdosfs_hashrem(dep)
189 	struct denode *dep;
190 {
191 	struct denode *deq;
192 	lwkt_tokref ilock;
193 
194 	lwkt_gettoken(&ilock, &dehash_token);
195 	deq = dep->de_next;
196 	if (deq)
197 		deq->de_prev = dep->de_prev;
198 	*dep->de_prev = deq;
199 #ifdef DIAGNOSTIC
200 	dep->de_next = NULL;
201 	dep->de_prev = NULL;
202 #endif
203 	lwkt_reltoken(&ilock);
204 }
205 
206 /*
207  * If deget() succeeds it returns with the gotten denode locked().
208  *
209  * pmp	     - address of msdosfsmount structure of the filesystem containing
210  *	       the denode of interest.  The pm_dev field and the address of
211  *	       the msdosfsmount structure are used.
212  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
213  *	       diroffset is relative to the beginning of the root directory,
214  *	       otherwise it is cluster relative.
215  * diroffset - offset past begin of cluster of denode we want
216  * depp	     - returns the address of the gotten denode.
217  */
218 int
219 deget(pmp, dirclust, diroffset, depp)
220 	struct msdosfsmount *pmp;	/* so we know the maj/min number */
221 	u_long dirclust;		/* cluster this dir entry came from */
222 	u_long diroffset;		/* index of entry within the cluster */
223 	struct denode **depp;		/* returns the addr of the gotten denode */
224 {
225 	struct thread *td = curthread;	/* XXX */
226 	int error;
227 	dev_t dev = pmp->pm_dev;
228 	struct mount *mntp = pmp->pm_mountp;
229 	struct direntry *direntptr;
230 	struct denode *ldep;
231 	struct vnode *nvp;
232 	struct buf *bp;
233 	struct timeval tv;
234 
235 #ifdef MSDOSFS_DEBUG
236 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
237 	    pmp, dirclust, diroffset, depp);
238 #endif
239 
240 	/*
241 	 * On FAT32 filesystems, root is a (more or less) normal
242 	 * directory
243 	 */
244 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
245 		dirclust = pmp->pm_rootdirblk;
246 
247 	/*
248 	 * See if the denode is in the denode cache. Use the location of
249 	 * the directory entry to compute the hash value. For subdir use
250 	 * address of "." entry. For root dir (if not FAT32) use cluster
251 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
252 	 *
253 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
254 	 * examined does not represent an unlinked but still open file.
255 	 * These files are not to be accessible even when the directory
256 	 * entry that represented the file happens to be reused while the
257 	 * deleted file is still open.
258 	 */
259 	ldep = msdosfs_hashget(dev, dirclust, diroffset);
260 	if (ldep) {
261 		*depp = ldep;
262 		return (0);
263 	}
264 
265 	/*
266 	 * Do the MALLOC before the getnewvnode since doing so afterward
267 	 * might cause a bogus v_data pointer to get dereferenced
268 	 * elsewhere if MALLOC should block.
269 	 */
270 	MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK);
271 
272 	/*
273 	 * Directory entry was not in cache, have to create a vnode and
274 	 * copy it from the passed disk buffer.
275 	 */
276 	/* getnewvnode() does a VREF() on the vnode */
277 	error = getnewvnode(VT_MSDOSFS, mntp, msdosfs_vnodeop_p, &nvp);
278 	if (error) {
279 		*depp = NULL;
280 		FREE(ldep, M_MSDOSFSNODE);
281 		return error;
282 	}
283 	bzero((caddr_t)ldep, sizeof *ldep);
284 	lockinit(&ldep->de_lock, 0, "denode", VLKTIMEOUT, 0);
285 	nvp->v_data = ldep;
286 	ldep->de_vnode = nvp;
287 	ldep->de_flag = 0;
288 	ldep->de_devvp = 0;
289 	ldep->de_dev = dev;
290 	ldep->de_dirclust = dirclust;
291 	ldep->de_diroffset = diroffset;
292 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
293 
294 	/*
295 	 * Lock the denode so that it can't be accessed until we've read
296 	 * it in and have done what we need to it.  Do this here instead
297 	 * of at the start of msdosfs_hashins() so that reinsert() can
298 	 * call msdosfs_hashins() with a locked denode.
299 	 */
300 	if (lockmgr(&ldep->de_lock, LK_EXCLUSIVE, NULL, td))
301 		panic("deget: unexpected lock failure");
302 
303 	/*
304 	 * Insert the denode into the hash queue.
305 	 */
306 	msdosfs_hashins(ldep);
307 
308 	ldep->de_pmp = pmp;
309 	ldep->de_refcnt = 1;
310 	/*
311 	 * Copy the directory entry into the denode area of the vnode.
312 	 */
313 	if ((dirclust == MSDOSFSROOT
314 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
315 	    && diroffset == MSDOSFSROOT_OFS) {
316 		/*
317 		 * Directory entry for the root directory. There isn't one,
318 		 * so we manufacture one. We should probably rummage
319 		 * through the root directory and find a label entry (if it
320 		 * exists), and then use the time and date from that entry
321 		 * as the time and date for the root denode.
322 		 */
323 		nvp->v_flag |= VROOT; /* should be further down		XXX */
324 
325 		ldep->de_Attributes = ATTR_DIRECTORY;
326 		ldep->de_LowerCase = 0;
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 * DEV_BSIZE;
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 		/*
380 		 * XXX Sometimes, these arrives that . entry have cluster
381 		 * number 0, when it shouldn't.  Use real cluster number
382 		 * instead of what is written in directory entry.
383 		 */
384 		if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
385 			printf("deget(): . entry at clust %ld != %ld\n",
386 					dirclust, ldep->de_StartCluster);
387 			ldep->de_StartCluster = dirclust;
388 		}
389 
390 		nvp->v_type = VDIR;
391 		if (ldep->de_StartCluster != MSDOSFSROOT) {
392 			error = pcbmap(ldep, 0xffff, 0, &size, 0);
393 			if (error == E2BIG) {
394 				ldep->de_FileSize = de_cn2off(pmp, size);
395 				error = 0;
396 			} else
397 				printf("deget(): pcbmap returned %d\n", error);
398 		}
399 	} else
400 		nvp->v_type = VREG;
401 	getmicrouptime(&tv);
402 	SETHIGH(ldep->de_modrev, tv.tv_sec);
403 	SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
404 	ldep->de_devvp = pmp->pm_devvp;
405 	VREF(ldep->de_devvp);
406 	*depp = ldep;
407 	return (0);
408 }
409 
410 int
411 deupdat(dep, waitfor)
412 	struct denode *dep;
413 	int waitfor;
414 {
415 	int error;
416 	struct buf *bp;
417 	struct direntry *dirp;
418 	struct timespec ts;
419 
420 	if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
421 		return (0);
422 	getnanotime(&ts);
423 	DETIMES(dep, &ts, &ts, &ts);
424 	if ((dep->de_flag & DE_MODIFIED) == 0)
425 		return (0);
426 	dep->de_flag &= ~DE_MODIFIED;
427 	if (dep->de_Attributes & ATTR_DIRECTORY)
428 		return (0);
429 	if (dep->de_refcnt <= 0)
430 		return (0);
431 	error = readde(dep, &bp, &dirp);
432 	if (error)
433 		return (error);
434 	DE_EXTERNALIZE(dirp, dep);
435 	if (waitfor)
436 		return (bwrite(bp));
437 	else {
438 		bdwrite(bp);
439 		return (0);
440 	}
441 }
442 
443 /*
444  * Truncate the file described by dep to the length specified by length.
445  */
446 int
447 detrunc(dep, length, flags, td)
448 	struct denode *dep;
449 	u_long length;
450 	int flags;
451 	struct thread *td;
452 {
453 	int error;
454 	int allerror;
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 
463 #ifdef MSDOSFS_DEBUG
464 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
465 #endif
466 
467 	/*
468 	 * Disallow attempts to truncate the root directory since it is of
469 	 * fixed size.  That's just the way dos filesystems are.  We use
470 	 * the VROOT bit in the vnode because checking for the directory
471 	 * bit and a startcluster of 0 in the denode is not adequate to
472 	 * recognize the root directory at this point in a file or
473 	 * directory's life.
474 	 */
475 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
476 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
477 		    dep->de_dirclust, dep->de_diroffset);
478 		return (EINVAL);
479 	}
480 
481 
482 	if (dep->de_FileSize < length) {
483 		vnode_pager_setsize(DETOV(dep), length);
484 		return deextend(dep, length);
485 	}
486 
487 	/*
488 	 * If the desired length is 0 then remember the starting cluster of
489 	 * the file and set the StartCluster field in the directory entry
490 	 * to 0.  If the desired length is not zero, then get the number of
491 	 * the last cluster in the shortened file.  Then get the number of
492 	 * the first cluster in the part of the file that is to be freed.
493 	 * Then set the next cluster pointer in the last cluster of the
494 	 * file to CLUST_EOFE.
495 	 */
496 	if (length == 0) {
497 		chaintofree = dep->de_StartCluster;
498 		dep->de_StartCluster = 0;
499 		eofentry = ~0;
500 	} else {
501 		error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
502 			       &eofentry, 0);
503 		if (error) {
504 #ifdef MSDOSFS_DEBUG
505 			printf("detrunc(): pcbmap fails %d\n", error);
506 #endif
507 			return (error);
508 		}
509 	}
510 
511 	fc_purge(dep, de_clcount(pmp, length));
512 
513 	/*
514 	 * If the new length is not a multiple of the cluster size then we
515 	 * must zero the tail end of the new last cluster in case it
516 	 * becomes part of the file again because of a seek.
517 	 */
518 	if ((boff = length & pmp->pm_crbomask) != 0) {
519 		if (isadir) {
520 			bn = cntobn(pmp, eofentry);
521 			error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
522 		} else {
523 			bn = de_blk(pmp, length);
524 			error = bread(DETOV(dep), bn, pmp->pm_bpcluster, &bp);
525 		}
526 		if (error) {
527 			brelse(bp);
528 #ifdef MSDOSFS_DEBUG
529 			printf("detrunc(): bread fails %d\n", error);
530 #endif
531 			return (error);
532 		}
533 		/*
534 		 * is this the right place for it?
535 		 */
536 		bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
537 		if (flags & IO_SYNC)
538 			bwrite(bp);
539 		else
540 			bdwrite(bp);
541 	}
542 
543 	/*
544 	 * Write out the updated directory entry.  Even if the update fails
545 	 * we free the trailing clusters.
546 	 */
547 	dep->de_FileSize = length;
548 	if (!isadir)
549 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
550 	allerror = vtruncbuf(DETOV(dep), td, length, pmp->pm_bpcluster);
551 #ifdef MSDOSFS_DEBUG
552 	if (allerror)
553 		printf("detrunc(): vtruncbuf error %d\n", allerror);
554 #endif
555 	error = deupdat(dep, 1);
556 	if (error && (allerror == 0))
557 		allerror = error;
558 #ifdef MSDOSFS_DEBUG
559 	printf("detrunc(): allerror %d, eofentry %lu\n",
560 	       allerror, eofentry);
561 #endif
562 
563 	/*
564 	 * If we need to break the cluster chain for the file then do it
565 	 * now.
566 	 */
567 	if (eofentry != ~0) {
568 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
569 				 &chaintofree, CLUST_EOFE);
570 		if (error) {
571 #ifdef MSDOSFS_DEBUG
572 			printf("detrunc(): fatentry errors %d\n", error);
573 #endif
574 			return (error);
575 		}
576 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
577 			    eofentry);
578 	}
579 
580 	/*
581 	 * Now free the clusters removed from the file because of the
582 	 * truncation.
583 	 */
584 	if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
585 		freeclusterchain(pmp, chaintofree);
586 
587 	return (allerror);
588 }
589 
590 /*
591  * Extend the file described by dep to length specified by length.
592  */
593 int
594 deextend(dep, length)
595 	struct denode *dep;
596 	u_long length;
597 {
598 	struct msdosfsmount *pmp = dep->de_pmp;
599 	u_long count;
600 	int error;
601 
602 	/*
603 	 * The root of a DOS filesystem cannot be extended.
604 	 */
605 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
606 		return (EINVAL);
607 
608 	/*
609 	 * Directories cannot be extended.
610 	 */
611 	if (dep->de_Attributes & ATTR_DIRECTORY)
612 		return (EISDIR);
613 
614 	if (length <= dep->de_FileSize)
615 		panic("deextend: file too large");
616 
617 	/*
618 	 * Compute the number of clusters to allocate.
619 	 */
620 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
621 	if (count > 0) {
622 		if (count > pmp->pm_freeclustercount)
623 			return (ENOSPC);
624 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
625 		if (error) {
626 			/* truncate the added clusters away again */
627 			(void) detrunc(dep, dep->de_FileSize, 0, NULL);
628 			return (error);
629 		}
630 	}
631 	dep->de_FileSize = length;
632 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
633 	return (deupdat(dep, 1));
634 }
635 
636 /*
637  * Move a denode to its correct hash queue after the file it represents has
638  * been moved to a new directory.
639  */
640 void
641 reinsert(dep)
642 	struct denode *dep;
643 {
644 	/*
645 	 * Fix up the denode cache.  If the denode is for a directory,
646 	 * there is nothing to do since the hash is based on the starting
647 	 * cluster of the directory file and that hasn't changed.  If for a
648 	 * file the hash is based on the location of the directory entry,
649 	 * so we must remove it from the cache and re-enter it with the
650 	 * hash based on the new location of the directory entry.
651 	 */
652 	if (dep->de_Attributes & ATTR_DIRECTORY)
653 		return;
654 	msdosfs_hashrem(dep);
655 	msdosfs_hashins(dep);
656 }
657 
658 int
659 msdosfs_reclaim(ap)
660 	struct vop_reclaim_args /* {
661 		struct vnode *a_vp;
662 	} */ *ap;
663 {
664 	struct vnode *vp = ap->a_vp;
665 	struct denode *dep = VTODE(vp);
666 
667 #ifdef MSDOSFS_DEBUG
668 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
669 	    dep, dep->de_Name, dep->de_refcnt);
670 #endif
671 
672 	if (prtactive && vp->v_usecount != 0)
673 		vprint("msdosfs_reclaim(): pushing active", vp);
674 	/*
675 	 * Remove the denode from its hash chain.
676 	 */
677 	msdosfs_hashrem(dep);
678 	/*
679 	 * Purge old data structures associated with the denode.
680 	 */
681 	cache_purge(vp);
682 	if (dep->de_devvp) {
683 		vrele(dep->de_devvp);
684 		dep->de_devvp = 0;
685 	}
686 #if 0 /* XXX */
687 	dep->de_flag = 0;
688 #endif
689 	FREE(dep, M_MSDOSFSNODE);
690 	vp->v_data = NULL;
691 
692 	return (0);
693 }
694 
695 int
696 msdosfs_inactive(ap)
697 	struct vop_inactive_args /* {
698 		struct vnode *a_vp;
699 		struct thread *a_td;
700 	} */ *ap;
701 {
702 	struct vnode *vp = ap->a_vp;
703 	struct denode *dep = VTODE(vp);
704 	int error = 0;
705 
706 #ifdef MSDOSFS_DEBUG
707 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
708 #endif
709 
710 	if (prtactive && vp->v_usecount != 0)
711 		vprint("msdosfs_inactive(): pushing active", vp);
712 
713 	/*
714 	 * Ignore denodes related to stale file handles.
715 	 */
716 	if (dep->de_Name[0] == SLOT_DELETED)
717 		goto out;
718 
719 	/*
720 	 * If the file has been deleted and it is on a read/write
721 	 * filesystem, then truncate the file, and mark the directory slot
722 	 * as empty.  (This may not be necessary for the dos filesystem.)
723 	 */
724 #ifdef MSDOSFS_DEBUG
725 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
726 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
727 #endif
728 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
729 		error = detrunc(dep, (u_long) 0, 0, ap->a_td);
730 		dep->de_flag |= DE_UPDATE;
731 		dep->de_Name[0] = SLOT_DELETED;
732 	}
733 	deupdat(dep, 0);
734 
735 out:
736 	VOP_UNLOCK(vp, NULL, 0, ap->a_td);
737 	/*
738 	 * If we are done with the denode, reclaim it
739 	 * so that it can be reused immediately.
740 	 */
741 #ifdef MSDOSFS_DEBUG
742 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount,
743 	       dep->de_Name[0]);
744 #endif
745 	if (dep->de_Name[0] == SLOT_DELETED)
746 		vrecycle(vp, NULL, ap->a_td);
747 	return (error);
748 }
749