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