xref: /netbsd-src/sys/fs/msdosfs/msdosfs_denode.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: msdosfs_denode.c,v 1.33 2008/05/16 09:21:59 hannken Exp $	*/
2 
3 /*-
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
42  * The author supplies this software to be publicly redistributed on the
43  * understanding that the author is not responsible for the correct
44  * functioning of this software in any circumstances and is not liable for
45  * any damages caused by this software.
46  *
47  * October 1992
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.33 2008/05/16 09:21:59 hannken Exp $");
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/mount.h>
56 #include <sys/malloc.h>
57 #include <sys/pool.h>
58 #include <sys/proc.h>
59 #include <sys/buf.h>
60 #include <sys/vnode.h>
61 #include <sys/kernel.h>		/* defines "time" */
62 #include <sys/dirent.h>
63 #include <sys/namei.h>
64 #include <sys/kauth.h>
65 
66 #include <uvm/uvm_extern.h>
67 
68 #include <fs/msdosfs/bpb.h>
69 #include <fs/msdosfs/msdosfsmount.h>
70 #include <fs/msdosfs/direntry.h>
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
73 
74 LIST_HEAD(ihashhead, denode) *dehashtbl;
75 u_long dehash;			/* size of hash table - 1 */
76 #define	DEHASH(dev, dcl, doff) \
77     (((dev) + (dcl) + (doff) / sizeof(struct direntry)) & dehash)
78 
79 kmutex_t msdosfs_ihash_lock;
80 kmutex_t msdosfs_hashlock;
81 
82 struct pool msdosfs_denode_pool;
83 
84 extern int prtactive;
85 
86 static const struct genfs_ops msdosfs_genfsops = {
87 	.gop_size = genfs_size,
88 	.gop_alloc = msdosfs_gop_alloc,
89 	.gop_write = genfs_gop_write,
90 	.gop_markupdate = msdosfs_gop_markupdate,
91 };
92 
93 static struct denode *msdosfs_hashget(dev_t, u_long, u_long, int);
94 static void msdosfs_hashins(struct denode *);
95 static void msdosfs_hashrem(struct denode *);
96 
97 MALLOC_DECLARE(M_MSDOSFSFAT);
98 
99 void
100 msdosfs_init()
101 {
102 
103 	malloc_type_attach(M_MSDOSFSMNT);
104 	malloc_type_attach(M_MSDOSFSFAT);
105 	malloc_type_attach(M_MSDOSFSTMP);
106 	pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
107 	    "msdosnopl", &pool_allocator_nointr, IPL_NONE);
108 	dehashtbl = hashinit(desiredvnodes / 2, HASH_LIST, true, &dehash);
109 	mutex_init(&msdosfs_ihash_lock, MUTEX_DEFAULT, IPL_NONE);
110 	mutex_init(&msdosfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
111 }
112 
113 /*
114  * Reinitialize inode hash table.
115  */
116 
117 void
118 msdosfs_reinit()
119 {
120 	struct denode *dep;
121 	struct ihashhead *oldhash, *hash;
122 	u_long oldmask, mask, val;
123 	int i;
124 
125 	hash = hashinit(desiredvnodes / 2, HASH_LIST, true, &mask);
126 
127 	mutex_enter(&msdosfs_ihash_lock);
128 	oldhash = dehashtbl;
129 	oldmask = dehash;
130 	dehashtbl = hash;
131 	dehash = mask;
132 	for (i = 0; i <= oldmask; i++) {
133 		while ((dep = LIST_FIRST(&oldhash[i])) != NULL) {
134 			LIST_REMOVE(dep, de_hash);
135 			val = DEHASH(dep->de_dev, dep->de_dirclust,
136 			    dep->de_diroffset);
137 			LIST_INSERT_HEAD(&hash[val], dep, de_hash);
138 		}
139 	}
140 	mutex_exit(&msdosfs_ihash_lock);
141 	hashdone(oldhash, HASH_LIST, oldmask);
142 }
143 
144 void
145 msdosfs_done()
146 {
147 	hashdone(dehashtbl, HASH_LIST, dehash);
148 	pool_destroy(&msdosfs_denode_pool);
149 	mutex_destroy(&msdosfs_ihash_lock);
150 	mutex_destroy(&msdosfs_hashlock);
151 	malloc_type_detach(M_MSDOSFSTMP);
152 	malloc_type_detach(M_MSDOSFSFAT);
153 	malloc_type_detach(M_MSDOSFSMNT);
154 }
155 
156 static struct denode *
157 msdosfs_hashget(dev, dirclust, diroff, flags)
158 	dev_t dev;
159 	u_long dirclust;
160 	u_long diroff;
161 	int flags;
162 {
163 	struct denode *dep;
164 	struct vnode *vp;
165 
166 loop:
167 	mutex_enter(&msdosfs_ihash_lock);
168 	LIST_FOREACH(dep, &dehashtbl[DEHASH(dev, dirclust, diroff)], de_hash) {
169 		if (dirclust == dep->de_dirclust &&
170 		    diroff == dep->de_diroffset &&
171 		    dev == dep->de_dev &&
172 		    dep->de_refcnt != 0) {
173 			vp = DETOV(dep);
174 			if (flags == 0) {
175 				mutex_exit(&msdosfs_ihash_lock);
176 			} else {
177 				mutex_enter(&vp->v_interlock);
178 				mutex_exit(&msdosfs_ihash_lock);
179 				if (vget(vp, flags | LK_INTERLOCK))
180 					goto loop;
181 			}
182 			return (dep);
183 		}
184 	}
185 	mutex_exit(&msdosfs_ihash_lock);
186 	return (NULL);
187 }
188 
189 static void
190 msdosfs_hashins(dep)
191 	struct denode *dep;
192 {
193 	struct ihashhead *depp;
194 	int val;
195 
196 	KASSERT(mutex_owned(&msdosfs_hashlock));
197 
198 	mutex_enter(&msdosfs_ihash_lock);
199 	val = DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
200 	depp = &dehashtbl[val];
201 	LIST_INSERT_HEAD(depp, dep, de_hash);
202 	mutex_exit(&msdosfs_ihash_lock);
203 }
204 
205 static void
206 msdosfs_hashrem(dep)
207 	struct denode *dep;
208 {
209 	mutex_enter(&msdosfs_ihash_lock);
210 	LIST_REMOVE(dep, de_hash);
211 	mutex_exit(&msdosfs_ihash_lock);
212 }
213 
214 /*
215  * If deget() succeeds it returns with the gotten denode locked().
216  *
217  * pmp	     - address of msdosfsmount structure of the filesystem containing
218  *	       the denode of interest.  The pm_dev field and the address of
219  *	       the msdosfsmount structure are used.
220  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
221  *	       diroffset is relative to the beginning of the root directory,
222  *	       otherwise it is cluster relative.
223  * diroffset - offset past begin of cluster of denode we want
224  * depp	     - returns the address of the gotten denode.
225  */
226 int
227 deget(pmp, dirclust, diroffset, depp)
228 	struct msdosfsmount *pmp;	/* so we know the maj/min number */
229 	u_long dirclust;		/* cluster this dir entry came from */
230 	u_long diroffset;		/* index of entry within the cluster */
231 	struct denode **depp;		/* returns the addr of the gotten denode */
232 {
233 	int error;
234 	extern int (**msdosfs_vnodeop_p)(void *);
235 	struct direntry *direntptr;
236 	struct denode *ldep;
237 	struct vnode *nvp;
238 	struct buf *bp;
239 
240 #ifdef MSDOSFS_DEBUG
241 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
242 	    pmp, dirclust, diroffset, depp);
243 #endif
244 
245 	/*
246 	 * On FAT32 filesystems, root is a (more or less) normal
247 	 * directory
248 	 */
249 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
250 		dirclust = pmp->pm_rootdirblk;
251 
252 	/*
253 	 * See if the denode is in the denode cache. Use the location of
254 	 * the directory entry to compute the hash value. For subdir use
255 	 * address of "." entry. For root dir (if not FAT32) use cluster
256 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
257 	 *
258 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
259 	 * examined does not represent an unlinked but still open file.
260 	 * These files are not to be accessible even when the directory
261 	 * entry that represented the file happens to be reused while the
262 	 * deleted file is still open.
263 	 */
264  retry:
265 	ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset, LK_EXCLUSIVE);
266 	if (ldep) {
267 		*depp = ldep;
268 		return (0);
269 	}
270 
271 	/*
272 	 * Directory entry was not in cache, have to create a vnode and
273 	 * copy it from the passed disk buffer.
274 	 */
275 	/* getnewvnode() does a VREF() on the vnode */
276 	error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp,
277 			    msdosfs_vnodeop_p, &nvp);
278 	if (error) {
279 		*depp = 0;
280 		return (error);
281 	}
282 	ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
283 
284 	/*
285 	 * If someone beat us to it, put back the freshly allocated
286 	 * vnode/inode pair and retry.
287 	 */
288 	mutex_enter(&msdosfs_hashlock);
289 	if (msdosfs_hashget(pmp->pm_dev, dirclust, diroffset, 0)) {
290 		mutex_exit(&msdosfs_hashlock);
291 		ungetnewvnode(nvp);
292 		pool_put(&msdosfs_denode_pool, ldep);
293 		goto retry;
294 	}
295 	memset(ldep, 0, sizeof *ldep);
296 	nvp->v_data = ldep;
297 	ldep->de_vnode = nvp;
298 	ldep->de_flag = 0;
299 	ldep->de_devvp = 0;
300 	ldep->de_lockf = 0;
301 	ldep->de_dev = pmp->pm_dev;
302 	ldep->de_dirclust = dirclust;
303 	ldep->de_diroffset = diroffset;
304 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
305 
306 	/*
307 	 * Insert the denode into the hash queue and lock the denode so it
308 	 * can't be accessed until we've read it in and have done what we
309 	 * need to it.
310 	 */
311 	vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
312 	genfs_node_init(nvp, &msdosfs_genfsops);
313 	msdosfs_hashins(ldep);
314 	mutex_exit(&msdosfs_hashlock);
315 
316 	ldep->de_pmp = pmp;
317 	ldep->de_devvp = pmp->pm_devvp;
318 	ldep->de_refcnt = 1;
319 	/*
320 	 * Copy the directory entry into the denode area of the vnode.
321 	 */
322 	if ((dirclust == MSDOSFSROOT
323 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
324 	    && diroffset == MSDOSFSROOT_OFS) {
325 		/*
326 		 * Directory entry for the root directory. There isn't one,
327 		 * so we manufacture one. We should probably rummage
328 		 * through the root directory and find a label entry (if it
329 		 * exists), and then use the time and date from that entry
330 		 * as the time and date for the root denode.
331 		 */
332 		nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
333 
334 		ldep->de_Attributes = ATTR_DIRECTORY;
335 		if (FAT32(pmp))
336 			ldep->de_StartCluster = pmp->pm_rootdirblk;
337 			/* de_FileSize will be filled in further down */
338 		else {
339 			ldep->de_StartCluster = MSDOSFSROOT;
340 			ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
341 		}
342 		/*
343 		 * fill in time and date so that dos2unixtime() doesn't
344 		 * spit up when called from msdosfs_getattr() with root
345 		 * denode
346 		 */
347 		ldep->de_CHun = 0;
348 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
349 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
350 		    | (1 << DD_DAY_SHIFT);
351 		/* Jan 1, 1980	 */
352 		ldep->de_ADate = ldep->de_CDate;
353 		ldep->de_MTime = ldep->de_CTime;
354 		ldep->de_MDate = ldep->de_CDate;
355 		/* leave the other fields as garbage */
356 	} else {
357 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
358 		if (error) {
359 			ldep->de_devvp = NULL;
360 			ldep->de_Name[0] = SLOT_DELETED;
361 			vput(nvp);
362 			return (error);
363 		}
364 		DE_INTERNALIZE(ldep, direntptr);
365 		brelse(bp, 0);
366 	}
367 
368 	/*
369 	 * Fill in a few fields of the vnode and finish filling in the
370 	 * denode.  Then return the address of the found denode.
371 	 */
372 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
373 		/*
374 		 * Since DOS directory entries that describe directories
375 		 * have 0 in the filesize field, we take this opportunity
376 		 * to find out the length of the directory and plug it into
377 		 * the denode structure.
378 		 */
379 		u_long size;
380 
381 		nvp->v_type = VDIR;
382 		if (ldep->de_StartCluster != MSDOSFSROOT) {
383 			error = pcbmap(ldep, CLUST_END, 0, &size, 0);
384 			if (error == E2BIG) {
385 				ldep->de_FileSize = de_cn2off(pmp, size);
386 				error = 0;
387 			} else
388 				printf("deget(): pcbmap returned %d\n", error);
389 		}
390 	} else
391 		nvp->v_type = VREG;
392 	VREF(ldep->de_devvp);
393 	*depp = ldep;
394 	uvm_vnp_setsize(nvp, ldep->de_FileSize);
395 	return (0);
396 }
397 
398 int
399 deupdat(dep, waitfor)
400 	struct denode *dep;
401 	int waitfor;
402 {
403 
404 	return (msdosfs_update(DETOV(dep), NULL, NULL,
405 	    waitfor ? UPDATE_WAIT : 0));
406 }
407 
408 /*
409  * Truncate the file described by dep to the length specified by length.
410  */
411 int
412 detrunc(struct denode *dep, u_long length, int flags, kauth_cred_t cred)
413 {
414 	int error;
415 	int allerror;
416 	u_long eofentry;
417 	u_long chaintofree = 0;
418 	daddr_t bn, lastblock;
419 	int boff;
420 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
421 	struct buf *bp;
422 	struct msdosfsmount *pmp = dep->de_pmp;
423 
424 #ifdef MSDOSFS_DEBUG
425 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
426 #endif
427 
428 	/*
429 	 * Disallow attempts to truncate the root directory since it is of
430 	 * fixed size.  That's just the way dos filesystems are.  We use
431 	 * the VROOT bit in the vnode because checking for the directory
432 	 * bit and a startcluster of 0 in the denode is not adequate to
433 	 * recognize the root directory at this point in a file or
434 	 * directory's life.
435 	 */
436 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
437 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
438 		    dep->de_dirclust, dep->de_diroffset);
439 		return (EINVAL);
440 	}
441 
442 	uvm_vnp_setsize(DETOV(dep), length);
443 
444 	if (dep->de_FileSize < length)
445 		return (deextend(dep, length, cred));
446 	lastblock = de_clcount(pmp, length) - 1;
447 
448 	/*
449 	 * If the desired length is 0 then remember the starting cluster of
450 	 * the file and set the StartCluster field in the directory entry
451 	 * to 0.  If the desired length is not zero, then get the number of
452 	 * the last cluster in the shortened file.  Then get the number of
453 	 * the first cluster in the part of the file that is to be freed.
454 	 * Then set the next cluster pointer in the last cluster of the
455 	 * file to CLUST_EOFE.
456 	 */
457 	if (length == 0) {
458 		chaintofree = dep->de_StartCluster;
459 		dep->de_StartCluster = 0;
460 		eofentry = ~0;
461 	} else {
462 		error = pcbmap(dep, lastblock, 0, &eofentry, 0);
463 		if (error) {
464 #ifdef MSDOSFS_DEBUG
465 			printf("detrunc(): pcbmap fails %d\n", error);
466 #endif
467 			return (error);
468 		}
469 	}
470 
471 	fc_purge(dep, lastblock + 1);
472 
473 	/*
474 	 * If the new length is not a multiple of the cluster size then we
475 	 * must zero the tail end of the new last cluster in case it
476 	 * becomes part of the file again because of a seek.
477 	 */
478 	if ((boff = length & pmp->pm_crbomask) != 0) {
479 		if (isadir) {
480 			bn = cntobn(pmp, eofentry);
481 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
482 			    pmp->pm_bpcluster, NOCRED, B_MODIFY, &bp);
483 			if (error) {
484 				brelse(bp, 0);
485 #ifdef MSDOSFS_DEBUG
486 				printf("detrunc(): bread fails %d\n", error);
487 #endif
488 				return (error);
489 			}
490 			memset((char *)bp->b_data + boff, 0,
491 			    pmp->pm_bpcluster - boff);
492 			if (flags & IO_SYNC)
493 				bwrite(bp);
494 			else
495 				bdwrite(bp);
496 		} else {
497 			uvm_vnp_zerorange(DETOV(dep), length,
498 					  pmp->pm_bpcluster - boff);
499 		}
500 	}
501 
502 	/*
503 	 * Write out the updated directory entry.  Even if the update fails
504 	 * we free the trailing clusters.
505 	 */
506 	dep->de_FileSize = length;
507 	if (!isadir)
508 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
509 	vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
510 	allerror = deupdat(dep, 1);
511 #ifdef MSDOSFS_DEBUG
512 	printf("detrunc(): allerror %d, eofentry %lu\n",
513 	       allerror, eofentry);
514 #endif
515 
516 	/*
517 	 * If we need to break the cluster chain for the file then do it
518 	 * now.
519 	 */
520 	if (eofentry != ~0) {
521 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
522 				 &chaintofree, CLUST_EOFE);
523 		if (error) {
524 #ifdef MSDOSFS_DEBUG
525 			printf("detrunc(): fatentry errors %d\n", error);
526 #endif
527 			return (error);
528 		}
529 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
530 			    eofentry);
531 	}
532 
533 	/*
534 	 * Now free the clusters removed from the file because of the
535 	 * truncation.
536 	 */
537 	if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
538 		freeclusterchain(pmp, chaintofree);
539 
540 	return (allerror);
541 }
542 
543 /*
544  * Extend the file described by dep to length specified by length.
545  */
546 int
547 deextend(dep, length, cred)
548 	struct denode *dep;
549 	u_long length;
550 	kauth_cred_t cred;
551 {
552 	struct msdosfsmount *pmp = dep->de_pmp;
553 	u_long count, osize;
554 	int error;
555 
556 	/*
557 	 * The root of a DOS filesystem cannot be extended.
558 	 */
559 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
560 		return (EINVAL);
561 
562 	/*
563 	 * Directories cannot be extended.
564 	 */
565 	if (dep->de_Attributes & ATTR_DIRECTORY)
566 		return (EISDIR);
567 
568 	if (length <= dep->de_FileSize)
569 		panic("deextend: file too large");
570 
571 	/*
572 	 * Compute the number of clusters to allocate.
573 	 */
574 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
575 	if (count > 0) {
576 		if (count > pmp->pm_freeclustercount)
577 			return (ENOSPC);
578 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
579 		if (error) {
580 			/* truncate the added clusters away again */
581 			(void) detrunc(dep, dep->de_FileSize, 0, cred);
582 			return (error);
583 		}
584 	}
585 
586 	/*
587 	 * Zero extend file range; uvm_vnp_zerorange() uses ubc_alloc() and a
588 	 * memset(); we set the write size so ubc won't read in file data that
589 	 * is zero'd later.
590 	 */
591 	osize = dep->de_FileSize;
592 	dep->de_FileSize = length;
593 	uvm_vnp_setwritesize(DETOV(dep), (voff_t)dep->de_FileSize);
594 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
595 	uvm_vnp_zerorange(DETOV(dep), (off_t)osize,
596 	    (size_t)(dep->de_FileSize - osize));
597 	uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
598 	return (deupdat(dep, 1));
599 }
600 
601 /*
602  * Move a denode to its correct hash queue after the file it represents has
603  * been moved to a new directory.
604  */
605 void
606 reinsert(dep)
607 	struct denode *dep;
608 {
609 	/*
610 	 * Fix up the denode cache.  If the denode is for a directory,
611 	 * there is nothing to do since the hash is based on the starting
612 	 * cluster of the directory file and that hasn't changed.  If for a
613 	 * file the hash is based on the location of the directory entry,
614 	 * so we must remove it from the cache and re-enter it with the
615 	 * hash based on the new location of the directory entry.
616 	 */
617 	if (dep->de_Attributes & ATTR_DIRECTORY)
618 		return;
619 	mutex_enter(&msdosfs_hashlock);
620 	msdosfs_hashrem(dep);
621 	msdosfs_hashins(dep);
622 	mutex_exit(&msdosfs_hashlock);
623 }
624 
625 int
626 msdosfs_reclaim(v)
627 	void *v;
628 {
629 	struct vop_reclaim_args /* {
630 		struct vnode *a_vp;
631 	} */ *ap = v;
632 	struct vnode *vp = ap->a_vp;
633 	struct denode *dep = VTODE(vp);
634 
635 #ifdef MSDOSFS_DEBUG
636 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
637 	    dep, dep->de_Name, dep->de_refcnt);
638 #endif
639 
640 	if (prtactive && vp->v_usecount > 1)
641 		vprint("msdosfs_reclaim(): pushing active", vp);
642 	/*
643 	 * Remove the denode from its hash chain.
644 	 */
645 	msdosfs_hashrem(dep);
646 	/*
647 	 * Purge old data structures associated with the denode.
648 	 */
649 	cache_purge(vp);
650 	if (dep->de_devvp) {
651 		vrele(dep->de_devvp);
652 		dep->de_devvp = 0;
653 	}
654 #if 0 /* XXX */
655 	dep->de_flag = 0;
656 #endif
657 	genfs_node_destroy(vp);
658 	pool_put(&msdosfs_denode_pool, dep);
659 	vp->v_data = NULL;
660 	return (0);
661 }
662 
663 int
664 msdosfs_inactive(v)
665 	void *v;
666 {
667 	struct vop_inactive_args /* {
668 		struct vnode *a_vp;
669 		bool *a_recycle;
670 	} */ *ap = v;
671 	struct vnode *vp = ap->a_vp;
672 	struct denode *dep = VTODE(vp);
673 	int error = 0;
674 
675 #ifdef MSDOSFS_DEBUG
676 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
677 #endif
678 
679 	/*
680 	 * Get rid of denodes related to stale file handles.
681 	 */
682 	if (dep->de_Name[0] == SLOT_DELETED)
683 		goto out;
684 
685 	/*
686 	 * If the file has been deleted and it is on a read/write
687 	 * filesystem, then truncate the file, and mark the directory slot
688 	 * as empty.  (This may not be necessary for the dos filesystem.)
689 	 */
690 #ifdef MSDOSFS_DEBUG
691 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
692 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag,
693 		(vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
694 #endif
695 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
696 		if (dep->de_FileSize != 0) {
697 			error = detrunc(dep, (u_long)0, 0, NOCRED);
698 		}
699 		dep->de_Name[0] = SLOT_DELETED;
700 	}
701 	deupdat(dep, 0);
702 out:
703 	/*
704 	 * If we are done with the denode, reclaim it
705 	 * so that it can be reused immediately.
706 	 */
707 #ifdef MSDOSFS_DEBUG
708 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
709 		vp->v_usecount, dep->de_Name[0]);
710 #endif
711 	*ap->a_recycle = (dep->de_Name[0] == SLOT_DELETED);
712 	VOP_UNLOCK(vp, 0);
713 	return (error);
714 }
715 
716 int
717 msdosfs_gop_alloc(struct vnode *vp, off_t off,
718     off_t len, int flags, kauth_cred_t cred)
719 {
720 	return 0;
721 }
722 
723 void
724 msdosfs_gop_markupdate(struct vnode *vp, int flags)
725 {
726 	u_long mask = 0;
727 
728 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
729 		mask = DE_ACCESS;
730 	}
731 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
732 		mask |= DE_UPDATE;
733 	}
734 	if (mask) {
735 		struct denode *dep = VTODE(vp);
736 
737 		dep->de_flag |= mask;
738 	}
739 }
740