xref: /minix/sys/fs/msdosfs/msdosfs_denode.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: msdosfs_denode.c,v 1.51 2015/03/28 19:24:05 maxv Exp $	*/
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
584d9c625SLionel Sambuc  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
684d9c625SLionel Sambuc  * All rights reserved.
784d9c625SLionel Sambuc  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
884d9c625SLionel Sambuc  *
984d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1084d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1184d9c625SLionel Sambuc  * are met:
1284d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1384d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1484d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1584d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1684d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1784d9c625SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
1884d9c625SLionel Sambuc  *    must display the following acknowledgement:
1984d9c625SLionel Sambuc  *	This product includes software developed by TooLs GmbH.
2084d9c625SLionel Sambuc  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2184d9c625SLionel Sambuc  *    derived from this software without specific prior written permission.
2284d9c625SLionel Sambuc  *
2384d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2484d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2584d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2684d9c625SLionel Sambuc  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2784d9c625SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2884d9c625SLionel Sambuc  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2984d9c625SLionel Sambuc  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3084d9c625SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3184d9c625SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3284d9c625SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3384d9c625SLionel Sambuc  */
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * Written by Paul Popelka (paulp@uts.amdahl.com)
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  * You can do anything you want with this software, just don't say you wrote
3884d9c625SLionel Sambuc  * it, and don't remove this notice.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * This software is provided "as is".
4184d9c625SLionel Sambuc  *
4284d9c625SLionel Sambuc  * The author supplies this software to be publicly redistributed on the
4384d9c625SLionel Sambuc  * understanding that the author is not responsible for the correct
4484d9c625SLionel Sambuc  * functioning of this software in any circumstances and is not liable for
4584d9c625SLionel Sambuc  * any damages caused by this software.
4684d9c625SLionel Sambuc  *
4784d9c625SLionel Sambuc  * October 1992
4884d9c625SLionel Sambuc  */
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc #include <sys/cdefs.h>
51*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.51 2015/03/28 19:24:05 maxv Exp $");
5284d9c625SLionel Sambuc 
5384d9c625SLionel Sambuc #include <sys/param.h>
5484d9c625SLionel Sambuc #include <sys/systm.h>
5584d9c625SLionel Sambuc #include <sys/mount.h>
5684d9c625SLionel Sambuc #include <sys/fstrans.h>
5784d9c625SLionel Sambuc #include <sys/malloc.h>
5884d9c625SLionel Sambuc #include <sys/pool.h>
5984d9c625SLionel Sambuc #include <sys/proc.h>
6084d9c625SLionel Sambuc #include <sys/buf.h>
6184d9c625SLionel Sambuc #include <sys/vnode.h>
6284d9c625SLionel Sambuc #include <sys/kernel.h>		/* defines "time" */
6384d9c625SLionel Sambuc #include <sys/dirent.h>
6484d9c625SLionel Sambuc #include <sys/namei.h>
6584d9c625SLionel Sambuc #include <sys/kauth.h>
6684d9c625SLionel Sambuc 
6784d9c625SLionel Sambuc #include <uvm/uvm_extern.h>
6884d9c625SLionel Sambuc 
6984d9c625SLionel Sambuc #include <fs/msdosfs/bpb.h>
7084d9c625SLionel Sambuc #include <fs/msdosfs/msdosfsmount.h>
7184d9c625SLionel Sambuc #include <fs/msdosfs/direntry.h>
7284d9c625SLionel Sambuc #include <fs/msdosfs/denode.h>
7384d9c625SLionel Sambuc #include <fs/msdosfs/fat.h>
7484d9c625SLionel Sambuc 
7584d9c625SLionel Sambuc struct pool msdosfs_denode_pool;
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc extern int prtactive;
7884d9c625SLionel Sambuc 
7984d9c625SLionel Sambuc struct fh_key {
8084d9c625SLionel Sambuc 	struct msdosfsmount *fhk_mount;
8184d9c625SLionel Sambuc 	uint32_t fhk_dircluster;
8284d9c625SLionel Sambuc 	uint32_t fhk_diroffset;
8384d9c625SLionel Sambuc };
8484d9c625SLionel Sambuc struct fh_node {
8584d9c625SLionel Sambuc 	struct rb_node fh_rbnode;
8684d9c625SLionel Sambuc 	struct fh_key fh_key;
8784d9c625SLionel Sambuc #define fh_mount	fh_key.fhk_mount
8884d9c625SLionel Sambuc #define fh_dircluster	fh_key.fhk_dircluster
8984d9c625SLionel Sambuc #define fh_diroffset	fh_key.fhk_diroffset
9084d9c625SLionel Sambuc 	uint32_t fh_gen;
9184d9c625SLionel Sambuc };
9284d9c625SLionel Sambuc 
9384d9c625SLionel Sambuc static int
fh_compare_node_fh(void * ctx,const void * b,const void * key)9484d9c625SLionel Sambuc fh_compare_node_fh(void *ctx, const void *b, const void *key)
9584d9c625SLionel Sambuc {
9684d9c625SLionel Sambuc 	const struct fh_node * const pnp = b;
9784d9c625SLionel Sambuc 	const struct fh_key * const fhp = key;
9884d9c625SLionel Sambuc 
9984d9c625SLionel Sambuc 	/* msdosfs_fh_destroy() below depends on first sorting on fh_mount. */
10084d9c625SLionel Sambuc 	if (pnp->fh_mount != fhp->fhk_mount)
10184d9c625SLionel Sambuc 		return (intptr_t)pnp->fh_mount - (intptr_t)fhp->fhk_mount;
10284d9c625SLionel Sambuc 	if (pnp->fh_dircluster != fhp->fhk_dircluster)
10384d9c625SLionel Sambuc 		return pnp->fh_dircluster - fhp->fhk_dircluster;
10484d9c625SLionel Sambuc 	return pnp->fh_diroffset - fhp->fhk_diroffset;
10584d9c625SLionel Sambuc }
10684d9c625SLionel Sambuc 
10784d9c625SLionel Sambuc static int
fh_compare_nodes(void * ctx,const void * parent,const void * node)10884d9c625SLionel Sambuc fh_compare_nodes(void *ctx, const void *parent, const void *node)
10984d9c625SLionel Sambuc {
11084d9c625SLionel Sambuc 	const struct fh_node * const np = node;
11184d9c625SLionel Sambuc 
11284d9c625SLionel Sambuc 	return fh_compare_node_fh(ctx, parent, &np->fh_key);
11384d9c625SLionel Sambuc }
11484d9c625SLionel Sambuc 
11584d9c625SLionel Sambuc static uint32_t fh_generation;
11684d9c625SLionel Sambuc static kmutex_t fh_lock;
11784d9c625SLionel Sambuc static struct pool fh_pool;
11884d9c625SLionel Sambuc static rb_tree_t fh_rbtree;
11984d9c625SLionel Sambuc static const rb_tree_ops_t fh_rbtree_ops = {
12084d9c625SLionel Sambuc 	.rbto_compare_nodes = fh_compare_nodes,
12184d9c625SLionel Sambuc 	.rbto_compare_key = fh_compare_node_fh,
12284d9c625SLionel Sambuc 	.rbto_node_offset = offsetof(struct fh_node, fh_rbnode),
12384d9c625SLionel Sambuc 	.rbto_context = NULL
12484d9c625SLionel Sambuc };
12584d9c625SLionel Sambuc 
12684d9c625SLionel Sambuc static const struct genfs_ops msdosfs_genfsops = {
12784d9c625SLionel Sambuc 	.gop_size = genfs_size,
12884d9c625SLionel Sambuc 	.gop_alloc = msdosfs_gop_alloc,
12984d9c625SLionel Sambuc 	.gop_write = genfs_gop_write,
13084d9c625SLionel Sambuc 	.gop_markupdate = msdosfs_gop_markupdate,
13184d9c625SLionel Sambuc };
13284d9c625SLionel Sambuc 
13384d9c625SLionel Sambuc MALLOC_DECLARE(M_MSDOSFSFAT);
13484d9c625SLionel Sambuc 
13584d9c625SLionel Sambuc void
msdosfs_init(void)13684d9c625SLionel Sambuc msdosfs_init(void)
13784d9c625SLionel Sambuc {
13884d9c625SLionel Sambuc 
13984d9c625SLionel Sambuc 	malloc_type_attach(M_MSDOSFSMNT);
14084d9c625SLionel Sambuc 	malloc_type_attach(M_MSDOSFSFAT);
14184d9c625SLionel Sambuc 	malloc_type_attach(M_MSDOSFSTMP);
14284d9c625SLionel Sambuc 	pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
14384d9c625SLionel Sambuc 	    "msdosnopl", &pool_allocator_nointr, IPL_NONE);
14484d9c625SLionel Sambuc 	pool_init(&fh_pool, sizeof(struct fh_node), 0, 0, 0,
14584d9c625SLionel Sambuc 	    "msdosfhpl", &pool_allocator_nointr, IPL_NONE);
14684d9c625SLionel Sambuc 	rb_tree_init(&fh_rbtree, &fh_rbtree_ops);
14784d9c625SLionel Sambuc 	mutex_init(&fh_lock, MUTEX_DEFAULT, IPL_NONE);
14884d9c625SLionel Sambuc }
14984d9c625SLionel Sambuc 
15084d9c625SLionel Sambuc /*
151*0a6a1f1dSLionel Sambuc  * Reinitialize.
15284d9c625SLionel Sambuc  */
15384d9c625SLionel Sambuc 
15484d9c625SLionel Sambuc void
msdosfs_reinit(void)15584d9c625SLionel Sambuc msdosfs_reinit(void)
15684d9c625SLionel Sambuc {
15784d9c625SLionel Sambuc 
15884d9c625SLionel Sambuc }
15984d9c625SLionel Sambuc 
16084d9c625SLionel Sambuc void
msdosfs_done(void)16184d9c625SLionel Sambuc msdosfs_done(void)
16284d9c625SLionel Sambuc {
16384d9c625SLionel Sambuc 	pool_destroy(&msdosfs_denode_pool);
16484d9c625SLionel Sambuc 	pool_destroy(&fh_pool);
16584d9c625SLionel Sambuc 	mutex_destroy(&fh_lock);
16684d9c625SLionel Sambuc 	malloc_type_detach(M_MSDOSFSTMP);
16784d9c625SLionel Sambuc 	malloc_type_detach(M_MSDOSFSFAT);
16884d9c625SLionel Sambuc 	malloc_type_detach(M_MSDOSFSMNT);
16984d9c625SLionel Sambuc }
17084d9c625SLionel Sambuc 
17184d9c625SLionel Sambuc /*
172*0a6a1f1dSLionel Sambuc  * If deget() succeeds it returns with the gotten denode unlocked.
17384d9c625SLionel Sambuc  *
17484d9c625SLionel Sambuc  * pmp	     - address of msdosfsmount structure of the filesystem containing
17584d9c625SLionel Sambuc  *	       the denode of interest.  The pm_dev field and the address of
17684d9c625SLionel Sambuc  *	       the msdosfsmount structure are used.
17784d9c625SLionel Sambuc  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
17884d9c625SLionel Sambuc  *	       diroffset is relative to the beginning of the root directory,
17984d9c625SLionel Sambuc  *	       otherwise it is cluster relative.
18084d9c625SLionel Sambuc  * diroffset - offset past begin of cluster of denode we want
181*0a6a1f1dSLionel Sambuc  * vpp	     - returns the address of the gotten vnode.
18284d9c625SLionel Sambuc  */
18384d9c625SLionel Sambuc int
deget(struct msdosfsmount * pmp,u_long dirclust,u_long diroffset,struct vnode ** vpp)184*0a6a1f1dSLionel Sambuc deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
185*0a6a1f1dSLionel Sambuc     struct vnode **vpp)
18684d9c625SLionel Sambuc 	/* pmp:	 so we know the maj/min number */
18784d9c625SLionel Sambuc 	/* dirclust:		 cluster this dir entry came from */
18884d9c625SLionel Sambuc 	/* diroffset:		 index of entry within the cluster */
189*0a6a1f1dSLionel Sambuc 	/* vpp:			 returns the addr of the gotten vnode */
19084d9c625SLionel Sambuc {
19184d9c625SLionel Sambuc 	int error;
192*0a6a1f1dSLionel Sambuc 	struct denode_key key;
19384d9c625SLionel Sambuc 
19484d9c625SLionel Sambuc 	/*
19584d9c625SLionel Sambuc 	 * On FAT32 filesystems, root is a (more or less) normal
19684d9c625SLionel Sambuc 	 * directory
19784d9c625SLionel Sambuc 	 */
19884d9c625SLionel Sambuc 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
19984d9c625SLionel Sambuc 		dirclust = pmp->pm_rootdirblk;
20084d9c625SLionel Sambuc 
201*0a6a1f1dSLionel Sambuc 	memset(&key, 0, sizeof(key));
202*0a6a1f1dSLionel Sambuc 	key.dk_dirclust = dirclust;
203*0a6a1f1dSLionel Sambuc 	key.dk_diroffset = diroffset;
204*0a6a1f1dSLionel Sambuc 	/* key.dk_dirgen = NULL; */
205*0a6a1f1dSLionel Sambuc 
206*0a6a1f1dSLionel Sambuc 	error = vcache_get(pmp->pm_mountp, &key, sizeof(key), vpp);
207*0a6a1f1dSLionel Sambuc 	return error;
20884d9c625SLionel Sambuc }
20984d9c625SLionel Sambuc 
210*0a6a1f1dSLionel Sambuc int
msdosfs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)211*0a6a1f1dSLionel Sambuc msdosfs_loadvnode(struct mount *mp, struct vnode *vp,
212*0a6a1f1dSLionel Sambuc     const void *key, size_t key_len, const void **new_key)
213*0a6a1f1dSLionel Sambuc {
214*0a6a1f1dSLionel Sambuc 	bool is_root;
215*0a6a1f1dSLionel Sambuc 	int error;
216*0a6a1f1dSLionel Sambuc 	extern int (**msdosfs_vnodeop_p)(void *);
217*0a6a1f1dSLionel Sambuc 	struct msdosfsmount *pmp;
218*0a6a1f1dSLionel Sambuc 	struct direntry *direntptr;
219*0a6a1f1dSLionel Sambuc 	struct denode *ldep;
220*0a6a1f1dSLionel Sambuc 	struct buf *bp;
221*0a6a1f1dSLionel Sambuc 	struct denode_key dkey;
222*0a6a1f1dSLionel Sambuc 
223*0a6a1f1dSLionel Sambuc 	KASSERT(key_len == sizeof(dkey));
224*0a6a1f1dSLionel Sambuc 	memcpy(&dkey, key, key_len);
225*0a6a1f1dSLionel Sambuc 	KASSERT(dkey.dk_dirgen == NULL);
226*0a6a1f1dSLionel Sambuc 
227*0a6a1f1dSLionel Sambuc 	pmp = VFSTOMSDOSFS(mp);
228*0a6a1f1dSLionel Sambuc 	is_root = ((dkey.dk_dirclust == MSDOSFSROOT ||
229*0a6a1f1dSLionel Sambuc 	    (FAT32(pmp) && dkey.dk_dirclust == pmp->pm_rootdirblk)) &&
230*0a6a1f1dSLionel Sambuc 	    dkey.dk_diroffset == MSDOSFSROOT_OFS);
231*0a6a1f1dSLionel Sambuc 
232*0a6a1f1dSLionel Sambuc #ifdef MSDOSFS_DEBUG
233*0a6a1f1dSLionel Sambuc 	printf("loadvnode(pmp %p, dirclust %lu, diroffset %lx, vp %p)\n",
234*0a6a1f1dSLionel Sambuc 	    pmp, dkey.dk_dirclust, dkey.dk_diroffset, vp);
235*0a6a1f1dSLionel Sambuc #endif
236*0a6a1f1dSLionel Sambuc 
23784d9c625SLionel Sambuc 	ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
23884d9c625SLionel Sambuc 	memset(ldep, 0, sizeof *ldep);
239*0a6a1f1dSLionel Sambuc 	/* ldep->de_flag = 0; */
240*0a6a1f1dSLionel Sambuc 	/* ldep->de_devvp = 0; */
241*0a6a1f1dSLionel Sambuc 	/* ldep->de_lockf = 0; */
24284d9c625SLionel Sambuc 	ldep->de_dev = pmp->pm_dev;
243*0a6a1f1dSLionel Sambuc 	ldep->de_dirclust = dkey.dk_dirclust;
244*0a6a1f1dSLionel Sambuc 	ldep->de_diroffset = dkey.dk_diroffset;
24584d9c625SLionel Sambuc 	ldep->de_pmp = pmp;
24684d9c625SLionel Sambuc 	ldep->de_devvp = pmp->pm_devvp;
24784d9c625SLionel Sambuc 	ldep->de_refcnt = 1;
248*0a6a1f1dSLionel Sambuc 	fc_purge(ldep, 0);	/* init the FAT cache for this denode */
249*0a6a1f1dSLionel Sambuc 
25084d9c625SLionel Sambuc 	/*
25184d9c625SLionel Sambuc 	 * Copy the directory entry into the denode area of the vnode.
25284d9c625SLionel Sambuc 	 */
253*0a6a1f1dSLionel Sambuc 	if (is_root) {
25484d9c625SLionel Sambuc 		/*
25584d9c625SLionel Sambuc 		 * Directory entry for the root directory. There isn't one,
25684d9c625SLionel Sambuc 		 * so we manufacture one. We should probably rummage
25784d9c625SLionel Sambuc 		 * through the root directory and find a label entry (if it
25884d9c625SLionel Sambuc 		 * exists), and then use the time and date from that entry
25984d9c625SLionel Sambuc 		 * as the time and date for the root denode.
26084d9c625SLionel Sambuc 		 */
26184d9c625SLionel Sambuc 		ldep->de_Attributes = ATTR_DIRECTORY;
26284d9c625SLionel Sambuc 		if (FAT32(pmp))
26384d9c625SLionel Sambuc 			ldep->de_StartCluster = pmp->pm_rootdirblk;
26484d9c625SLionel Sambuc 			/* de_FileSize will be filled in further down */
26584d9c625SLionel Sambuc 		else {
26684d9c625SLionel Sambuc 			ldep->de_StartCluster = MSDOSFSROOT;
267*0a6a1f1dSLionel Sambuc 			ldep->de_FileSize = pmp->pm_rootdirsize *
268*0a6a1f1dSLionel Sambuc 			    pmp->pm_BytesPerSec;
26984d9c625SLionel Sambuc 		}
27084d9c625SLionel Sambuc 		/*
27184d9c625SLionel Sambuc 		 * fill in time and date so that dos2unixtime() doesn't
27284d9c625SLionel Sambuc 		 * spit up when called from msdosfs_getattr() with root
27384d9c625SLionel Sambuc 		 * denode
27484d9c625SLionel Sambuc 		 */
27584d9c625SLionel Sambuc 		ldep->de_CHun = 0;
27684d9c625SLionel Sambuc 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
27784d9c625SLionel Sambuc 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
27884d9c625SLionel Sambuc 		    | (1 << DD_DAY_SHIFT);
27984d9c625SLionel Sambuc 		/* Jan 1, 1980	 */
28084d9c625SLionel Sambuc 		ldep->de_ADate = ldep->de_CDate;
28184d9c625SLionel Sambuc 		ldep->de_MTime = ldep->de_CTime;
28284d9c625SLionel Sambuc 		ldep->de_MDate = ldep->de_CDate;
28384d9c625SLionel Sambuc 		/* leave the other fields as garbage */
28484d9c625SLionel Sambuc 	} else {
285*0a6a1f1dSLionel Sambuc 		error = readep(pmp, ldep->de_dirclust, ldep->de_diroffset,
286*0a6a1f1dSLionel Sambuc 		    &bp, &direntptr);
28784d9c625SLionel Sambuc 		if (error) {
288*0a6a1f1dSLionel Sambuc 			pool_put(&msdosfs_denode_pool, ldep);
289*0a6a1f1dSLionel Sambuc 			return error;
29084d9c625SLionel Sambuc 		}
29184d9c625SLionel Sambuc 		DE_INTERNALIZE(ldep, direntptr);
29284d9c625SLionel Sambuc 		brelse(bp, 0);
29384d9c625SLionel Sambuc 	}
29484d9c625SLionel Sambuc 
29584d9c625SLionel Sambuc 	/*
29684d9c625SLionel Sambuc 	 * Fill in a few fields of the vnode and finish filling in the
297*0a6a1f1dSLionel Sambuc 	 * denode.
29884d9c625SLionel Sambuc 	 */
29984d9c625SLionel Sambuc 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
30084d9c625SLionel Sambuc 		/*
30184d9c625SLionel Sambuc 		 * Since DOS directory entries that describe directories
30284d9c625SLionel Sambuc 		 * have 0 in the filesize field, we take this opportunity
30384d9c625SLionel Sambuc 		 * to find out the length of the directory and plug it into
30484d9c625SLionel Sambuc 		 * the denode structure.
30584d9c625SLionel Sambuc 		 */
30684d9c625SLionel Sambuc 		u_long size;
30784d9c625SLionel Sambuc 
308*0a6a1f1dSLionel Sambuc 		vp->v_type = VDIR;
30984d9c625SLionel Sambuc 		if (ldep->de_StartCluster != MSDOSFSROOT) {
31084d9c625SLionel Sambuc 			error = pcbmap(ldep, CLUST_END, 0, &size, 0);
31184d9c625SLionel Sambuc 			if (error == E2BIG) {
31284d9c625SLionel Sambuc 				ldep->de_FileSize = de_cn2off(pmp, size);
31384d9c625SLionel Sambuc 				error = 0;
31484d9c625SLionel Sambuc 			} else
315*0a6a1f1dSLionel Sambuc 				printf("loadvnode(): pcbmap returned %d\n",
316*0a6a1f1dSLionel Sambuc 				    error);
31784d9c625SLionel Sambuc 		}
31884d9c625SLionel Sambuc 	} else
319*0a6a1f1dSLionel Sambuc 		vp->v_type = VREG;
32084d9c625SLionel Sambuc 	vref(ldep->de_devvp);
321*0a6a1f1dSLionel Sambuc 	if (is_root)
322*0a6a1f1dSLionel Sambuc 		vp->v_vflag |= VV_ROOT;
323*0a6a1f1dSLionel Sambuc 	vp->v_tag = VT_MSDOSFS;
324*0a6a1f1dSLionel Sambuc 	vp->v_op = msdosfs_vnodeop_p;
325*0a6a1f1dSLionel Sambuc 	vp->v_data = ldep;
326*0a6a1f1dSLionel Sambuc 	ldep->de_vnode = vp;
327*0a6a1f1dSLionel Sambuc 	genfs_node_init(vp, &msdosfs_genfsops);
328*0a6a1f1dSLionel Sambuc 	uvm_vnp_setsize(vp, ldep->de_FileSize);
329*0a6a1f1dSLionel Sambuc 	*new_key = &ldep->de_key;
330*0a6a1f1dSLionel Sambuc 
331*0a6a1f1dSLionel Sambuc 	return 0;
33284d9c625SLionel Sambuc }
33384d9c625SLionel Sambuc 
33484d9c625SLionel Sambuc int
deupdat(struct denode * dep,int waitfor)33584d9c625SLionel Sambuc deupdat(struct denode *dep, int waitfor)
33684d9c625SLionel Sambuc {
33784d9c625SLionel Sambuc 
33884d9c625SLionel Sambuc 	return (msdosfs_update(DETOV(dep), NULL, NULL,
33984d9c625SLionel Sambuc 	    waitfor ? UPDATE_WAIT : 0));
34084d9c625SLionel Sambuc }
34184d9c625SLionel Sambuc 
34284d9c625SLionel Sambuc /*
34384d9c625SLionel Sambuc  * Truncate the file described by dep to the length specified by length.
34484d9c625SLionel Sambuc  */
34584d9c625SLionel Sambuc int
detrunc(struct denode * dep,u_long length,int flags,kauth_cred_t cred)34684d9c625SLionel Sambuc detrunc(struct denode *dep, u_long length, int flags, kauth_cred_t cred)
34784d9c625SLionel Sambuc {
34884d9c625SLionel Sambuc 	int error;
34984d9c625SLionel Sambuc 	int allerror;
35084d9c625SLionel Sambuc 	u_long eofentry;
35184d9c625SLionel Sambuc 	u_long chaintofree = 0;
35284d9c625SLionel Sambuc 	daddr_t bn, lastblock;
35384d9c625SLionel Sambuc 	int boff;
35484d9c625SLionel Sambuc 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
35584d9c625SLionel Sambuc 	struct buf *bp;
35684d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
35784d9c625SLionel Sambuc 
35884d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
35984d9c625SLionel Sambuc 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
36084d9c625SLionel Sambuc #endif
36184d9c625SLionel Sambuc 
36284d9c625SLionel Sambuc 	/*
36384d9c625SLionel Sambuc 	 * Disallow attempts to truncate the root directory since it is of
36484d9c625SLionel Sambuc 	 * fixed size.  That's just the way dos filesystems are.  We use
36584d9c625SLionel Sambuc 	 * the VROOT bit in the vnode because checking for the directory
36684d9c625SLionel Sambuc 	 * bit and a startcluster of 0 in the denode is not adequate to
36784d9c625SLionel Sambuc 	 * recognize the root directory at this point in a file or
36884d9c625SLionel Sambuc 	 * directory's life.
36984d9c625SLionel Sambuc 	 */
37084d9c625SLionel Sambuc 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
37184d9c625SLionel Sambuc 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
37284d9c625SLionel Sambuc 		    dep->de_dirclust, dep->de_diroffset);
37384d9c625SLionel Sambuc 		return (EINVAL);
37484d9c625SLionel Sambuc 	}
37584d9c625SLionel Sambuc 
37684d9c625SLionel Sambuc 	uvm_vnp_setsize(DETOV(dep), length);
37784d9c625SLionel Sambuc 
37884d9c625SLionel Sambuc 	if (dep->de_FileSize < length)
37984d9c625SLionel Sambuc 		return (deextend(dep, length, cred));
38084d9c625SLionel Sambuc 	lastblock = de_clcount(pmp, length) - 1;
38184d9c625SLionel Sambuc 
38284d9c625SLionel Sambuc 	/*
38384d9c625SLionel Sambuc 	 * If the desired length is 0 then remember the starting cluster of
38484d9c625SLionel Sambuc 	 * the file and set the StartCluster field in the directory entry
38584d9c625SLionel Sambuc 	 * to 0.  If the desired length is not zero, then get the number of
38684d9c625SLionel Sambuc 	 * the last cluster in the shortened file.  Then get the number of
38784d9c625SLionel Sambuc 	 * the first cluster in the part of the file that is to be freed.
38884d9c625SLionel Sambuc 	 * Then set the next cluster pointer in the last cluster of the
38984d9c625SLionel Sambuc 	 * file to CLUST_EOFE.
39084d9c625SLionel Sambuc 	 */
39184d9c625SLionel Sambuc 	if (length == 0) {
39284d9c625SLionel Sambuc 		chaintofree = dep->de_StartCluster;
39384d9c625SLionel Sambuc 		dep->de_StartCluster = 0;
39484d9c625SLionel Sambuc 		eofentry = ~0;
39584d9c625SLionel Sambuc 	} else {
39684d9c625SLionel Sambuc 		error = pcbmap(dep, lastblock, 0, &eofentry, 0);
39784d9c625SLionel Sambuc 		if (error) {
39884d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
39984d9c625SLionel Sambuc 			printf("detrunc(): pcbmap fails %d\n", error);
40084d9c625SLionel Sambuc #endif
40184d9c625SLionel Sambuc 			return (error);
40284d9c625SLionel Sambuc 		}
40384d9c625SLionel Sambuc 	}
40484d9c625SLionel Sambuc 
40584d9c625SLionel Sambuc 	/*
40684d9c625SLionel Sambuc 	 * If the new length is not a multiple of the cluster size then we
40784d9c625SLionel Sambuc 	 * must zero the tail end of the new last cluster in case it
40884d9c625SLionel Sambuc 	 * becomes part of the file again because of a seek.
40984d9c625SLionel Sambuc 	 */
41084d9c625SLionel Sambuc 	if ((boff = length & pmp->pm_crbomask) != 0) {
41184d9c625SLionel Sambuc 		if (isadir) {
41284d9c625SLionel Sambuc 			bn = cntobn(pmp, eofentry);
41384d9c625SLionel Sambuc 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
414*0a6a1f1dSLionel Sambuc 			    pmp->pm_bpcluster, B_MODIFY, &bp);
41584d9c625SLionel Sambuc 			if (error) {
41684d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
41784d9c625SLionel Sambuc 				printf("detrunc(): bread fails %d\n", error);
41884d9c625SLionel Sambuc #endif
41984d9c625SLionel Sambuc 				return (error);
42084d9c625SLionel Sambuc 			}
42184d9c625SLionel Sambuc 			memset((char *)bp->b_data + boff, 0,
42284d9c625SLionel Sambuc 			    pmp->pm_bpcluster - boff);
42384d9c625SLionel Sambuc 			if (flags & IO_SYNC)
42484d9c625SLionel Sambuc 				bwrite(bp);
42584d9c625SLionel Sambuc 			else
42684d9c625SLionel Sambuc 				bdwrite(bp);
42784d9c625SLionel Sambuc 		} else {
42884d9c625SLionel Sambuc 			ubc_zerorange(&DETOV(dep)->v_uobj, length,
42984d9c625SLionel Sambuc 				      pmp->pm_bpcluster - boff,
43084d9c625SLionel Sambuc 				      UBC_UNMAP_FLAG(DETOV(dep)));
43184d9c625SLionel Sambuc 		}
43284d9c625SLionel Sambuc 	}
43384d9c625SLionel Sambuc 
43484d9c625SLionel Sambuc 	/*
43584d9c625SLionel Sambuc 	 * Write out the updated directory entry.  Even if the update fails
43684d9c625SLionel Sambuc 	 * we free the trailing clusters.
43784d9c625SLionel Sambuc 	 */
43884d9c625SLionel Sambuc 	dep->de_FileSize = length;
43984d9c625SLionel Sambuc 	if (!isadir)
44084d9c625SLionel Sambuc 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
44184d9c625SLionel Sambuc 	vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
44284d9c625SLionel Sambuc 	allerror = deupdat(dep, 1);
44384d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
44484d9c625SLionel Sambuc 	printf("detrunc(): allerror %d, eofentry %lu\n",
44584d9c625SLionel Sambuc 	       allerror, eofentry);
44684d9c625SLionel Sambuc #endif
44784d9c625SLionel Sambuc 
44884d9c625SLionel Sambuc 	fc_purge(dep, lastblock + 1);
44984d9c625SLionel Sambuc 
45084d9c625SLionel Sambuc 	/*
45184d9c625SLionel Sambuc 	 * If we need to break the cluster chain for the file then do it
45284d9c625SLionel Sambuc 	 * now.
45384d9c625SLionel Sambuc 	 */
45484d9c625SLionel Sambuc 	if (eofentry != ~0) {
45584d9c625SLionel Sambuc 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
45684d9c625SLionel Sambuc 				 &chaintofree, CLUST_EOFE);
45784d9c625SLionel Sambuc 		if (error) {
45884d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
45984d9c625SLionel Sambuc 			printf("detrunc(): fatentry errors %d\n", error);
46084d9c625SLionel Sambuc #endif
46184d9c625SLionel Sambuc 			return (error);
46284d9c625SLionel Sambuc 		}
46384d9c625SLionel Sambuc 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
46484d9c625SLionel Sambuc 			    eofentry);
46584d9c625SLionel Sambuc 	}
46684d9c625SLionel Sambuc 
46784d9c625SLionel Sambuc 	/*
46884d9c625SLionel Sambuc 	 * Now free the clusters removed from the file because of the
46984d9c625SLionel Sambuc 	 * truncation.
47084d9c625SLionel Sambuc 	 */
47184d9c625SLionel Sambuc 	if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
47284d9c625SLionel Sambuc 		freeclusterchain(pmp, chaintofree);
47384d9c625SLionel Sambuc 
47484d9c625SLionel Sambuc 	return (allerror);
47584d9c625SLionel Sambuc }
47684d9c625SLionel Sambuc 
47784d9c625SLionel Sambuc /*
47884d9c625SLionel Sambuc  * Extend the file described by dep to length specified by length.
47984d9c625SLionel Sambuc  */
48084d9c625SLionel Sambuc int
deextend(struct denode * dep,u_long length,kauth_cred_t cred)48184d9c625SLionel Sambuc deextend(struct denode *dep, u_long length, kauth_cred_t cred)
48284d9c625SLionel Sambuc {
48384d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
48484d9c625SLionel Sambuc 	u_long count, osize;
48584d9c625SLionel Sambuc 	int error;
48684d9c625SLionel Sambuc 
48784d9c625SLionel Sambuc 	/*
48884d9c625SLionel Sambuc 	 * The root of a DOS filesystem cannot be extended.
48984d9c625SLionel Sambuc 	 */
49084d9c625SLionel Sambuc 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
49184d9c625SLionel Sambuc 		return (EINVAL);
49284d9c625SLionel Sambuc 
49384d9c625SLionel Sambuc 	/*
49484d9c625SLionel Sambuc 	 * Directories cannot be extended.
49584d9c625SLionel Sambuc 	 */
49684d9c625SLionel Sambuc 	if (dep->de_Attributes & ATTR_DIRECTORY)
49784d9c625SLionel Sambuc 		return (EISDIR);
49884d9c625SLionel Sambuc 
49984d9c625SLionel Sambuc 	if (length <= dep->de_FileSize)
50084d9c625SLionel Sambuc 		panic("deextend: file too large");
50184d9c625SLionel Sambuc 
50284d9c625SLionel Sambuc 	/*
50384d9c625SLionel Sambuc 	 * Compute the number of clusters to allocate.
50484d9c625SLionel Sambuc 	 */
50584d9c625SLionel Sambuc 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
50684d9c625SLionel Sambuc 	if (count > 0) {
50784d9c625SLionel Sambuc 		if (count > pmp->pm_freeclustercount)
50884d9c625SLionel Sambuc 			return (ENOSPC);
50984d9c625SLionel Sambuc 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
51084d9c625SLionel Sambuc 		if (error) {
51184d9c625SLionel Sambuc 			/* truncate the added clusters away again */
51284d9c625SLionel Sambuc 			(void) detrunc(dep, dep->de_FileSize, 0, cred);
51384d9c625SLionel Sambuc 			return (error);
51484d9c625SLionel Sambuc 		}
51584d9c625SLionel Sambuc 	}
51684d9c625SLionel Sambuc 
51784d9c625SLionel Sambuc 	/*
51884d9c625SLionel Sambuc 	 * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
51984d9c625SLionel Sambuc 	 * memset(); we set the write size so ubc won't read in file data that
52084d9c625SLionel Sambuc 	 * is zero'd later.
52184d9c625SLionel Sambuc 	 */
52284d9c625SLionel Sambuc 	osize = dep->de_FileSize;
52384d9c625SLionel Sambuc 	dep->de_FileSize = length;
52484d9c625SLionel Sambuc 	uvm_vnp_setwritesize(DETOV(dep), (voff_t)dep->de_FileSize);
52584d9c625SLionel Sambuc 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
52684d9c625SLionel Sambuc 	ubc_zerorange(&DETOV(dep)->v_uobj, (off_t)osize,
52784d9c625SLionel Sambuc 	    (size_t)(round_page(dep->de_FileSize) - osize),
52884d9c625SLionel Sambuc 	    UBC_UNMAP_FLAG(DETOV(dep)));
52984d9c625SLionel Sambuc 	uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
53084d9c625SLionel Sambuc 	return (deupdat(dep, 1));
53184d9c625SLionel Sambuc }
53284d9c625SLionel Sambuc 
53384d9c625SLionel Sambuc int
msdosfs_reclaim(void * v)53484d9c625SLionel Sambuc msdosfs_reclaim(void *v)
53584d9c625SLionel Sambuc {
53684d9c625SLionel Sambuc 	struct vop_reclaim_args /* {
53784d9c625SLionel Sambuc 		struct vnode *a_vp;
53884d9c625SLionel Sambuc 	} */ *ap = v;
53984d9c625SLionel Sambuc 	struct vnode *vp = ap->a_vp;
540*0a6a1f1dSLionel Sambuc 	struct mount *mp = vp->v_mount;
54184d9c625SLionel Sambuc 	struct denode *dep = VTODE(vp);
54284d9c625SLionel Sambuc 
543*0a6a1f1dSLionel Sambuc 	fstrans_start(mp, FSTRANS_LAZY);
54484d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
54584d9c625SLionel Sambuc 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
54684d9c625SLionel Sambuc 	    dep, dep->de_Name, dep->de_refcnt);
54784d9c625SLionel Sambuc #endif
54884d9c625SLionel Sambuc 
54984d9c625SLionel Sambuc 	if (prtactive && vp->v_usecount > 1)
55084d9c625SLionel Sambuc 		vprint("msdosfs_reclaim(): pushing active", vp);
55184d9c625SLionel Sambuc 	/*
552*0a6a1f1dSLionel Sambuc 	 * Remove the denode from the vnode cache.
55384d9c625SLionel Sambuc 	 */
554*0a6a1f1dSLionel Sambuc 	vcache_remove(vp->v_mount, &dep->de_key, sizeof(dep->de_key));
55584d9c625SLionel Sambuc 	/*
55684d9c625SLionel Sambuc 	 * Purge old data structures associated with the denode.
55784d9c625SLionel Sambuc 	 */
55884d9c625SLionel Sambuc 	if (dep->de_devvp) {
55984d9c625SLionel Sambuc 		vrele(dep->de_devvp);
56084d9c625SLionel Sambuc 		dep->de_devvp = 0;
56184d9c625SLionel Sambuc 	}
56284d9c625SLionel Sambuc #if 0 /* XXX */
56384d9c625SLionel Sambuc 	dep->de_flag = 0;
56484d9c625SLionel Sambuc #endif
565*0a6a1f1dSLionel Sambuc 	/*
566*0a6a1f1dSLionel Sambuc 	 * To interlock with msdosfs_sync().
567*0a6a1f1dSLionel Sambuc 	 */
56884d9c625SLionel Sambuc 	genfs_node_destroy(vp);
569*0a6a1f1dSLionel Sambuc 	mutex_enter(vp->v_interlock);
57084d9c625SLionel Sambuc 	vp->v_data = NULL;
571*0a6a1f1dSLionel Sambuc 	mutex_exit(vp->v_interlock);
572*0a6a1f1dSLionel Sambuc 	pool_put(&msdosfs_denode_pool, dep);
573*0a6a1f1dSLionel Sambuc 	fstrans_done(mp);
57484d9c625SLionel Sambuc 	return (0);
57584d9c625SLionel Sambuc }
57684d9c625SLionel Sambuc 
57784d9c625SLionel Sambuc int
msdosfs_inactive(void * v)57884d9c625SLionel Sambuc msdosfs_inactive(void *v)
57984d9c625SLionel Sambuc {
58084d9c625SLionel Sambuc 	struct vop_inactive_args /* {
58184d9c625SLionel Sambuc 		struct vnode *a_vp;
58284d9c625SLionel Sambuc 		bool *a_recycle;
58384d9c625SLionel Sambuc 	} */ *ap = v;
58484d9c625SLionel Sambuc 	struct vnode *vp = ap->a_vp;
58584d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
58684d9c625SLionel Sambuc 	struct denode *dep = VTODE(vp);
58784d9c625SLionel Sambuc 	int error = 0;
58884d9c625SLionel Sambuc 
58984d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
59084d9c625SLionel Sambuc 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
59184d9c625SLionel Sambuc #endif
59284d9c625SLionel Sambuc 
59384d9c625SLionel Sambuc 	fstrans_start(mp, FSTRANS_LAZY);
59484d9c625SLionel Sambuc 	/*
59584d9c625SLionel Sambuc 	 * Get rid of denodes related to stale file handles.
59684d9c625SLionel Sambuc 	 */
59784d9c625SLionel Sambuc 	if (dep->de_Name[0] == SLOT_DELETED)
59884d9c625SLionel Sambuc 		goto out;
59984d9c625SLionel Sambuc 
60084d9c625SLionel Sambuc 	/*
60184d9c625SLionel Sambuc 	 * If the file has been deleted and it is on a read/write
60284d9c625SLionel Sambuc 	 * filesystem, then truncate the file, and mark the directory slot
60384d9c625SLionel Sambuc 	 * as empty.  (This may not be necessary for the dos filesystem.)
60484d9c625SLionel Sambuc 	 */
60584d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
60684d9c625SLionel Sambuc 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
60784d9c625SLionel Sambuc 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag,
60884d9c625SLionel Sambuc 		(vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
60984d9c625SLionel Sambuc #endif
61084d9c625SLionel Sambuc 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
61184d9c625SLionel Sambuc 		if (dep->de_FileSize != 0) {
61284d9c625SLionel Sambuc 			error = detrunc(dep, (u_long)0, 0, NOCRED);
61384d9c625SLionel Sambuc 		}
61484d9c625SLionel Sambuc 		dep->de_Name[0] = SLOT_DELETED;
61584d9c625SLionel Sambuc 		msdosfs_fh_remove(dep->de_pmp,
61684d9c625SLionel Sambuc 		    dep->de_dirclust, dep->de_diroffset);
61784d9c625SLionel Sambuc 	}
61884d9c625SLionel Sambuc 	deupdat(dep, 0);
61984d9c625SLionel Sambuc out:
62084d9c625SLionel Sambuc 	/*
62184d9c625SLionel Sambuc 	 * If we are done with the denode, reclaim it
62284d9c625SLionel Sambuc 	 * so that it can be reused immediately.
62384d9c625SLionel Sambuc 	 */
62484d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
62584d9c625SLionel Sambuc 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
62684d9c625SLionel Sambuc 		vp->v_usecount, dep->de_Name[0]);
62784d9c625SLionel Sambuc #endif
62884d9c625SLionel Sambuc 	*ap->a_recycle = (dep->de_Name[0] == SLOT_DELETED);
62984d9c625SLionel Sambuc 	VOP_UNLOCK(vp);
63084d9c625SLionel Sambuc 	fstrans_done(mp);
63184d9c625SLionel Sambuc 	return (error);
63284d9c625SLionel Sambuc }
63384d9c625SLionel Sambuc 
63484d9c625SLionel Sambuc int
msdosfs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)63584d9c625SLionel Sambuc msdosfs_gop_alloc(struct vnode *vp, off_t off,
63684d9c625SLionel Sambuc     off_t len, int flags, kauth_cred_t cred)
63784d9c625SLionel Sambuc {
63884d9c625SLionel Sambuc 	return 0;
63984d9c625SLionel Sambuc }
64084d9c625SLionel Sambuc 
64184d9c625SLionel Sambuc void
msdosfs_gop_markupdate(struct vnode * vp,int flags)64284d9c625SLionel Sambuc msdosfs_gop_markupdate(struct vnode *vp, int flags)
64384d9c625SLionel Sambuc {
64484d9c625SLionel Sambuc 	u_long mask = 0;
64584d9c625SLionel Sambuc 
64684d9c625SLionel Sambuc 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
64784d9c625SLionel Sambuc 		mask = DE_ACCESS;
64884d9c625SLionel Sambuc 	}
64984d9c625SLionel Sambuc 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
65084d9c625SLionel Sambuc 		mask |= DE_UPDATE;
65184d9c625SLionel Sambuc 	}
65284d9c625SLionel Sambuc 	if (mask) {
65384d9c625SLionel Sambuc 		struct denode *dep = VTODE(vp);
65484d9c625SLionel Sambuc 
65584d9c625SLionel Sambuc 		dep->de_flag |= mask;
65684d9c625SLionel Sambuc 	}
65784d9c625SLionel Sambuc }
65884d9c625SLionel Sambuc 
65984d9c625SLionel Sambuc int
msdosfs_fh_enter(struct msdosfsmount * pmp,uint32_t dircluster,uint32_t diroffset,uint32_t * genp)66084d9c625SLionel Sambuc msdosfs_fh_enter(struct msdosfsmount *pmp,
66184d9c625SLionel Sambuc      uint32_t dircluster, uint32_t diroffset, uint32_t *genp)
66284d9c625SLionel Sambuc {
66384d9c625SLionel Sambuc 	struct fh_key fhkey;
66484d9c625SLionel Sambuc 	struct fh_node *fhp;
66584d9c625SLionel Sambuc 
66684d9c625SLionel Sambuc 	fhkey.fhk_mount = pmp;
66784d9c625SLionel Sambuc 	fhkey.fhk_dircluster = dircluster;
66884d9c625SLionel Sambuc 	fhkey.fhk_diroffset = diroffset;
66984d9c625SLionel Sambuc 
67084d9c625SLionel Sambuc 	mutex_enter(&fh_lock);
67184d9c625SLionel Sambuc 	fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
67284d9c625SLionel Sambuc 	if (fhp == NULL) {
67384d9c625SLionel Sambuc 		mutex_exit(&fh_lock);
67484d9c625SLionel Sambuc 		fhp = pool_get(&fh_pool, PR_WAITOK);
67584d9c625SLionel Sambuc 		mutex_enter(&fh_lock);
67684d9c625SLionel Sambuc 		fhp->fh_key = fhkey;
67784d9c625SLionel Sambuc 		fhp->fh_gen = fh_generation++;
67884d9c625SLionel Sambuc 		rb_tree_insert_node(&fh_rbtree, fhp);
67984d9c625SLionel Sambuc 	}
68084d9c625SLionel Sambuc 	*genp = fhp->fh_gen;
68184d9c625SLionel Sambuc 	mutex_exit(&fh_lock);
68284d9c625SLionel Sambuc 	return 0;
68384d9c625SLionel Sambuc }
68484d9c625SLionel Sambuc 
68584d9c625SLionel Sambuc int
msdosfs_fh_remove(struct msdosfsmount * pmp,uint32_t dircluster,uint32_t diroffset)68684d9c625SLionel Sambuc msdosfs_fh_remove(struct msdosfsmount *pmp,
68784d9c625SLionel Sambuc      uint32_t dircluster, uint32_t diroffset)
68884d9c625SLionel Sambuc {
68984d9c625SLionel Sambuc 	struct fh_key fhkey;
69084d9c625SLionel Sambuc 	struct fh_node *fhp;
69184d9c625SLionel Sambuc 
69284d9c625SLionel Sambuc 	fhkey.fhk_mount = pmp;
69384d9c625SLionel Sambuc 	fhkey.fhk_dircluster = dircluster;
69484d9c625SLionel Sambuc 	fhkey.fhk_diroffset = diroffset;
69584d9c625SLionel Sambuc 
69684d9c625SLionel Sambuc 	mutex_enter(&fh_lock);
69784d9c625SLionel Sambuc 	fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
69884d9c625SLionel Sambuc 	if (fhp == NULL) {
69984d9c625SLionel Sambuc 		mutex_exit(&fh_lock);
70084d9c625SLionel Sambuc 		return ENOENT;
70184d9c625SLionel Sambuc 	}
70284d9c625SLionel Sambuc 	rb_tree_remove_node(&fh_rbtree, fhp);
70384d9c625SLionel Sambuc 	mutex_exit(&fh_lock);
70484d9c625SLionel Sambuc 	pool_put(&fh_pool, fhp);
70584d9c625SLionel Sambuc 	return 0;
70684d9c625SLionel Sambuc }
70784d9c625SLionel Sambuc 
70884d9c625SLionel Sambuc int
msdosfs_fh_lookup(struct msdosfsmount * pmp,uint32_t dircluster,uint32_t diroffset,uint32_t * genp)70984d9c625SLionel Sambuc msdosfs_fh_lookup(struct msdosfsmount *pmp,
71084d9c625SLionel Sambuc      uint32_t dircluster, uint32_t diroffset, uint32_t *genp)
71184d9c625SLionel Sambuc {
71284d9c625SLionel Sambuc 	struct fh_key fhkey;
71384d9c625SLionel Sambuc 	struct fh_node *fhp;
71484d9c625SLionel Sambuc 
71584d9c625SLionel Sambuc 	fhkey.fhk_mount = pmp;
71684d9c625SLionel Sambuc 	fhkey.fhk_dircluster = dircluster;
71784d9c625SLionel Sambuc 	fhkey.fhk_diroffset = diroffset;
71884d9c625SLionel Sambuc 
71984d9c625SLionel Sambuc 	mutex_enter(&fh_lock);
72084d9c625SLionel Sambuc 	fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
72184d9c625SLionel Sambuc 	if (fhp == NULL) {
72284d9c625SLionel Sambuc 		mutex_exit(&fh_lock);
72384d9c625SLionel Sambuc 		return ESTALE;
72484d9c625SLionel Sambuc 	}
72584d9c625SLionel Sambuc 	*genp = fhp->fh_gen;
72684d9c625SLionel Sambuc 	mutex_exit(&fh_lock);
72784d9c625SLionel Sambuc 	return 0;
72884d9c625SLionel Sambuc }
72984d9c625SLionel Sambuc 
73084d9c625SLionel Sambuc void
msdosfs_fh_destroy(struct msdosfsmount * pmp)73184d9c625SLionel Sambuc msdosfs_fh_destroy(struct msdosfsmount *pmp)
73284d9c625SLionel Sambuc {
73384d9c625SLionel Sambuc 	struct fh_key fhkey;
73484d9c625SLionel Sambuc 	struct fh_node *fhp, *nfhp;
73584d9c625SLionel Sambuc 
73684d9c625SLionel Sambuc 	fhkey.fhk_mount = pmp;
73784d9c625SLionel Sambuc 	fhkey.fhk_dircluster = 0;
73884d9c625SLionel Sambuc 	fhkey.fhk_diroffset = 0;
73984d9c625SLionel Sambuc 
74084d9c625SLionel Sambuc 	mutex_enter(&fh_lock);
74184d9c625SLionel Sambuc 	for (fhp = rb_tree_find_node_geq(&fh_rbtree, &fhkey);
74284d9c625SLionel Sambuc 	    fhp != NULL && fhp->fh_mount == pmp; fhp = nfhp) {
74384d9c625SLionel Sambuc 		nfhp = rb_tree_iterate(&fh_rbtree, fhp, RB_DIR_RIGHT);
74484d9c625SLionel Sambuc 		rb_tree_remove_node(&fh_rbtree, fhp);
74584d9c625SLionel Sambuc 		pool_put(&fh_pool, fhp);
74684d9c625SLionel Sambuc 	}
74784d9c625SLionel Sambuc #ifdef DIAGNOSTIC
74884d9c625SLionel Sambuc 	RB_TREE_FOREACH(fhp, &fh_rbtree) {
74984d9c625SLionel Sambuc 		KASSERT(fhp->fh_mount != pmp);
75084d9c625SLionel Sambuc 	}
75184d9c625SLionel Sambuc #endif
75284d9c625SLionel Sambuc 	mutex_exit(&fh_lock);
75384d9c625SLionel Sambuc }
754