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