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