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