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