xref: /netbsd-src/sys/fs/msdosfs/msdosfs_denode.c (revision 8086f46e37a8dc0192d6888e1de22553ed3fb87c)
1*8086f46eSthorpej /*	$NetBSD: msdosfs_denode.c,v 1.60 2021/10/23 16:58:17 thorpej Exp $	*/
298d58548Sjdolecek 
398d58548Sjdolecek /*-
498d58548Sjdolecek  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
598d58548Sjdolecek  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
698d58548Sjdolecek  * All rights reserved.
798d58548Sjdolecek  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
898d58548Sjdolecek  *
998d58548Sjdolecek  * Redistribution and use in source and binary forms, with or without
1098d58548Sjdolecek  * modification, are permitted provided that the following conditions
1198d58548Sjdolecek  * are met:
1298d58548Sjdolecek  * 1. Redistributions of source code must retain the above copyright
1398d58548Sjdolecek  *    notice, this list of conditions and the following disclaimer.
1498d58548Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
1598d58548Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
1698d58548Sjdolecek  *    documentation and/or other materials provided with the distribution.
1798d58548Sjdolecek  * 3. All advertising materials mentioning features or use of this software
1898d58548Sjdolecek  *    must display the following acknowledgement:
1998d58548Sjdolecek  *	This product includes software developed by TooLs GmbH.
2098d58548Sjdolecek  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2198d58548Sjdolecek  *    derived from this software without specific prior written permission.
2298d58548Sjdolecek  *
2398d58548Sjdolecek  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2498d58548Sjdolecek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2598d58548Sjdolecek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2698d58548Sjdolecek  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2798d58548Sjdolecek  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2898d58548Sjdolecek  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2998d58548Sjdolecek  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3098d58548Sjdolecek  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3198d58548Sjdolecek  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3298d58548Sjdolecek  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3398d58548Sjdolecek  */
3498d58548Sjdolecek /*
3598d58548Sjdolecek  * Written by Paul Popelka (paulp@uts.amdahl.com)
3698d58548Sjdolecek  *
3798d58548Sjdolecek  * You can do anything you want with this software, just don't say you wrote
3898d58548Sjdolecek  * it, and don't remove this notice.
3998d58548Sjdolecek  *
4098d58548Sjdolecek  * This software is provided "as is".
4198d58548Sjdolecek  *
4298d58548Sjdolecek  * The author supplies this software to be publicly redistributed on the
4398d58548Sjdolecek  * understanding that the author is not responsible for the correct
4498d58548Sjdolecek  * functioning of this software in any circumstances and is not liable for
4598d58548Sjdolecek  * any damages caused by this software.
4698d58548Sjdolecek  *
4798d58548Sjdolecek  * October 1992
4898d58548Sjdolecek  */
4998d58548Sjdolecek 
5098d58548Sjdolecek #include <sys/cdefs.h>
51*8086f46eSthorpej __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.60 2021/10/23 16:58:17 thorpej Exp $");
5298d58548Sjdolecek 
5398d58548Sjdolecek #include <sys/param.h>
5498d58548Sjdolecek #include <sys/systm.h>
5598d58548Sjdolecek #include <sys/mount.h>
5698d58548Sjdolecek #include <sys/malloc.h>
5798d58548Sjdolecek #include <sys/pool.h>
5898d58548Sjdolecek #include <sys/proc.h>
5998d58548Sjdolecek #include <sys/buf.h>
6098d58548Sjdolecek #include <sys/vnode.h>
6198d58548Sjdolecek #include <sys/kernel.h>		/* defines "time" */
6298d58548Sjdolecek #include <sys/dirent.h>
6398d58548Sjdolecek #include <sys/namei.h>
64fc9422c9Selad #include <sys/kauth.h>
6598d58548Sjdolecek 
6698d58548Sjdolecek #include <uvm/uvm_extern.h>
6798d58548Sjdolecek 
6898d58548Sjdolecek #include <fs/msdosfs/bpb.h>
6998d58548Sjdolecek #include <fs/msdosfs/msdosfsmount.h>
7098d58548Sjdolecek #include <fs/msdosfs/direntry.h>
7198d58548Sjdolecek #include <fs/msdosfs/denode.h>
7298d58548Sjdolecek #include <fs/msdosfs/fat.h>
7398d58548Sjdolecek 
74835b0326Spooka struct pool msdosfs_denode_pool;
7598d58548Sjdolecek 
76fff1c84cShannken struct fh_key {
77fff1c84cShannken 	struct msdosfsmount *fhk_mount;
78fff1c84cShannken 	uint32_t fhk_dircluster;
79fff1c84cShannken 	uint32_t fhk_diroffset;
80fff1c84cShannken };
81fff1c84cShannken struct fh_node {
82fff1c84cShannken 	struct rb_node fh_rbnode;
83fff1c84cShannken 	struct fh_key fh_key;
84fff1c84cShannken #define fh_mount	fh_key.fhk_mount
85fff1c84cShannken #define fh_dircluster	fh_key.fhk_dircluster
86fff1c84cShannken #define fh_diroffset	fh_key.fhk_diroffset
87fff1c84cShannken 	uint32_t fh_gen;
88fff1c84cShannken };
89fff1c84cShannken 
90fff1c84cShannken static int
fh_compare_node_fh(void * ctx,const void * b,const void * key)91fff1c84cShannken fh_compare_node_fh(void *ctx, const void *b, const void *key)
92fff1c84cShannken {
93fff1c84cShannken 	const struct fh_node * const pnp = b;
94fff1c84cShannken 	const struct fh_key * const fhp = key;
95fff1c84cShannken 
96fff1c84cShannken 	/* msdosfs_fh_destroy() below depends on first sorting on fh_mount. */
97fff1c84cShannken 	if (pnp->fh_mount != fhp->fhk_mount)
98fff1c84cShannken 		return (intptr_t)pnp->fh_mount - (intptr_t)fhp->fhk_mount;
99fff1c84cShannken 	if (pnp->fh_dircluster != fhp->fhk_dircluster)
100fff1c84cShannken 		return pnp->fh_dircluster - fhp->fhk_dircluster;
101fff1c84cShannken 	return pnp->fh_diroffset - fhp->fhk_diroffset;
102fff1c84cShannken }
103fff1c84cShannken 
104fff1c84cShannken static int
fh_compare_nodes(void * ctx,const void * parent,const void * node)105fff1c84cShannken fh_compare_nodes(void *ctx, const void *parent, const void *node)
106fff1c84cShannken {
107fff1c84cShannken 	const struct fh_node * const np = node;
108fff1c84cShannken 
109fff1c84cShannken 	return fh_compare_node_fh(ctx, parent, &np->fh_key);
110fff1c84cShannken }
111fff1c84cShannken 
112fff1c84cShannken static uint32_t fh_generation;
113fff1c84cShannken static kmutex_t fh_lock;
114fff1c84cShannken static struct pool fh_pool;
115fff1c84cShannken static rb_tree_t fh_rbtree;
116fff1c84cShannken static const rb_tree_ops_t fh_rbtree_ops = {
117fff1c84cShannken 	.rbto_compare_nodes = fh_compare_nodes,
118fff1c84cShannken 	.rbto_compare_key = fh_compare_node_fh,
119fff1c84cShannken 	.rbto_node_offset = offsetof(struct fh_node, fh_rbnode),
120fff1c84cShannken 	.rbto_context = NULL
121fff1c84cShannken };
122fff1c84cShannken 
12344d128faSyamt static const struct genfs_ops msdosfs_genfsops = {
12444d128faSyamt 	.gop_size = genfs_size,
12544d128faSyamt 	.gop_alloc = msdosfs_gop_alloc,
12644d128faSyamt 	.gop_write = genfs_gop_write,
127b7bfe828Syamt 	.gop_markupdate = msdosfs_gop_markupdate,
128e406c140Schs 	.gop_putrange = genfs_gop_putrange,
12998d58548Sjdolecek };
13098d58548Sjdolecek 
131284a91c3Satatat MALLOC_DECLARE(M_MSDOSFSFAT);
132284a91c3Satatat 
13398d58548Sjdolecek void
msdosfs_init(void)134b8817e4aScegger msdosfs_init(void)
13598d58548Sjdolecek {
136835b0326Spooka 
137284a91c3Satatat 	malloc_type_attach(M_MSDOSFSMNT);
138284a91c3Satatat 	malloc_type_attach(M_MSDOSFSFAT);
1390ae0a486Srumble 	malloc_type_attach(M_MSDOSFSTMP);
1401d3a6a32Satatat 	pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
14159d979c5Sad 	    "msdosnopl", &pool_allocator_nointr, IPL_NONE);
142fff1c84cShannken 	pool_init(&fh_pool, sizeof(struct fh_node), 0, 0, 0,
143fff1c84cShannken 	    "msdosfhpl", &pool_allocator_nointr, IPL_NONE);
144fff1c84cShannken 	rb_tree_init(&fh_rbtree, &fh_rbtree_ops);
145fff1c84cShannken 	mutex_init(&fh_lock, MUTEX_DEFAULT, IPL_NONE);
14698d58548Sjdolecek }
14798d58548Sjdolecek 
14898d58548Sjdolecek /*
149798256c9Shannken  * Reinitialize.
15098d58548Sjdolecek  */
15198d58548Sjdolecek 
15298d58548Sjdolecek void
msdosfs_reinit(void)153b8817e4aScegger msdosfs_reinit(void)
15498d58548Sjdolecek {
15598d58548Sjdolecek 
15698d58548Sjdolecek }
15798d58548Sjdolecek 
15898d58548Sjdolecek void
msdosfs_done(void)159b8817e4aScegger msdosfs_done(void)
16098d58548Sjdolecek {
1611d3a6a32Satatat 	pool_destroy(&msdosfs_denode_pool);
162fff1c84cShannken 	pool_destroy(&fh_pool);
163fff1c84cShannken 	mutex_destroy(&fh_lock);
1640ae0a486Srumble 	malloc_type_detach(M_MSDOSFSTMP);
165284a91c3Satatat 	malloc_type_detach(M_MSDOSFSFAT);
166284a91c3Satatat 	malloc_type_detach(M_MSDOSFSMNT);
16798d58548Sjdolecek }
16898d58548Sjdolecek 
16998d58548Sjdolecek /*
170798256c9Shannken  * If deget() succeeds it returns with the gotten denode unlocked.
17198d58548Sjdolecek  *
17298d58548Sjdolecek  * pmp	     - address of msdosfsmount structure of the filesystem containing
17398d58548Sjdolecek  *	       the denode of interest.  The pm_dev field and the address of
17498d58548Sjdolecek  *	       the msdosfsmount structure are used.
17598d58548Sjdolecek  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
17698d58548Sjdolecek  *	       diroffset is relative to the beginning of the root directory,
17798d58548Sjdolecek  *	       otherwise it is cluster relative.
17898d58548Sjdolecek  * diroffset - offset past begin of cluster of denode we want
179798256c9Shannken  * vpp	     - returns the address of the gotten vnode.
18098d58548Sjdolecek  */
18198d58548Sjdolecek int
msdosfs_deget(struct msdosfsmount * pmp,u_long dirclust,u_long diroffset,struct vnode ** vpp)182*8086f46eSthorpej msdosfs_deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
183798256c9Shannken     struct vnode **vpp)
18482357f6dSdsl 	/* pmp:	 so we know the maj/min number */
18582357f6dSdsl 	/* dirclust:		 cluster this dir entry came from */
18682357f6dSdsl 	/* diroffset:		 index of entry within the cluster */
187798256c9Shannken 	/* vpp:			 returns the addr of the gotten vnode */
18898d58548Sjdolecek {
18998d58548Sjdolecek 	int error;
190798256c9Shannken 	struct denode_key key;
19198d58548Sjdolecek 
19298d58548Sjdolecek 	/*
19398d58548Sjdolecek 	 * On FAT32 filesystems, root is a (more or less) normal
19498d58548Sjdolecek 	 * directory
19598d58548Sjdolecek 	 */
19698d58548Sjdolecek 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
19798d58548Sjdolecek 		dirclust = pmp->pm_rootdirblk;
19898d58548Sjdolecek 
199798256c9Shannken 	memset(&key, 0, sizeof(key));
200798256c9Shannken 	key.dk_dirclust = dirclust;
201798256c9Shannken 	key.dk_diroffset = diroffset;
202798256c9Shannken 	/* key.dk_dirgen = NULL; */
203798256c9Shannken 
204798256c9Shannken 	error = vcache_get(pmp->pm_mountp, &key, sizeof(key), vpp);
205798256c9Shannken 	return error;
20698d58548Sjdolecek }
20798d58548Sjdolecek 
208798256c9Shannken int
msdosfs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)209798256c9Shannken msdosfs_loadvnode(struct mount *mp, struct vnode *vp,
210798256c9Shannken     const void *key, size_t key_len, const void **new_key)
211798256c9Shannken {
212798256c9Shannken 	bool is_root;
213798256c9Shannken 	int error;
214798256c9Shannken 	extern int (**msdosfs_vnodeop_p)(void *);
215798256c9Shannken 	struct msdosfsmount *pmp;
216798256c9Shannken 	struct direntry *direntptr;
217798256c9Shannken 	struct denode *ldep;
218798256c9Shannken 	struct buf *bp;
219798256c9Shannken 	struct denode_key dkey;
220798256c9Shannken 
221798256c9Shannken 	KASSERT(key_len == sizeof(dkey));
222798256c9Shannken 	memcpy(&dkey, key, key_len);
223798256c9Shannken 	KASSERT(dkey.dk_dirgen == NULL);
224798256c9Shannken 
225798256c9Shannken 	pmp = VFSTOMSDOSFS(mp);
226798256c9Shannken 	is_root = ((dkey.dk_dirclust == MSDOSFSROOT ||
227798256c9Shannken 	    (FAT32(pmp) && dkey.dk_dirclust == pmp->pm_rootdirblk)) &&
228798256c9Shannken 	    dkey.dk_diroffset == MSDOSFSROOT_OFS);
229798256c9Shannken 
230798256c9Shannken #ifdef MSDOSFS_DEBUG
231798256c9Shannken 	printf("loadvnode(pmp %p, dirclust %lu, diroffset %lx, vp %p)\n",
232798256c9Shannken 	    pmp, dkey.dk_dirclust, dkey.dk_diroffset, vp);
233798256c9Shannken #endif
234798256c9Shannken 
23598d58548Sjdolecek 	ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
23698d58548Sjdolecek 	memset(ldep, 0, sizeof *ldep);
237798256c9Shannken 	/* ldep->de_flag = 0; */
238798256c9Shannken 	/* ldep->de_devvp = 0; */
239798256c9Shannken 	/* ldep->de_lockf = 0; */
24098d58548Sjdolecek 	ldep->de_dev = pmp->pm_dev;
241798256c9Shannken 	ldep->de_dirclust = dkey.dk_dirclust;
242798256c9Shannken 	ldep->de_diroffset = dkey.dk_diroffset;
24398d58548Sjdolecek 	ldep->de_pmp = pmp;
24498d58548Sjdolecek 	ldep->de_devvp = pmp->pm_devvp;
24598d58548Sjdolecek 	ldep->de_refcnt = 1;
246*8086f46eSthorpej 	msdosfs_fc_purge(ldep, 0);	/* init the FAT cache for this denode */
247798256c9Shannken 
24898d58548Sjdolecek 	/*
24998d58548Sjdolecek 	 * Copy the directory entry into the denode area of the vnode.
25098d58548Sjdolecek 	 */
251798256c9Shannken 	if (is_root) {
25298d58548Sjdolecek 		/*
25398d58548Sjdolecek 		 * Directory entry for the root directory. There isn't one,
25498d58548Sjdolecek 		 * so we manufacture one. We should probably rummage
25598d58548Sjdolecek 		 * through the root directory and find a label entry (if it
25698d58548Sjdolecek 		 * exists), and then use the time and date from that entry
25798d58548Sjdolecek 		 * as the time and date for the root denode.
25898d58548Sjdolecek 		 */
25998d58548Sjdolecek 		ldep->de_Attributes = ATTR_DIRECTORY;
26098d58548Sjdolecek 		if (FAT32(pmp))
26198d58548Sjdolecek 			ldep->de_StartCluster = pmp->pm_rootdirblk;
26298d58548Sjdolecek 			/* de_FileSize will be filled in further down */
26398d58548Sjdolecek 		else {
26498d58548Sjdolecek 			ldep->de_StartCluster = MSDOSFSROOT;
265798256c9Shannken 			ldep->de_FileSize = pmp->pm_rootdirsize *
266798256c9Shannken 			    pmp->pm_BytesPerSec;
26798d58548Sjdolecek 		}
26898d58548Sjdolecek 		/*
26998d58548Sjdolecek 		 * fill in time and date so that dos2unixtime() doesn't
27098d58548Sjdolecek 		 * spit up when called from msdosfs_getattr() with root
27198d58548Sjdolecek 		 * denode
27298d58548Sjdolecek 		 */
27398d58548Sjdolecek 		ldep->de_CHun = 0;
27498d58548Sjdolecek 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
27598d58548Sjdolecek 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
27698d58548Sjdolecek 		    | (1 << DD_DAY_SHIFT);
27798d58548Sjdolecek 		/* Jan 1, 1980	 */
27898d58548Sjdolecek 		ldep->de_ADate = ldep->de_CDate;
27998d58548Sjdolecek 		ldep->de_MTime = ldep->de_CTime;
28098d58548Sjdolecek 		ldep->de_MDate = ldep->de_CDate;
28198d58548Sjdolecek 		/* leave the other fields as garbage */
28298d58548Sjdolecek 	} else {
283*8086f46eSthorpej 		error = msdosfs_readep(pmp, ldep->de_dirclust,
284*8086f46eSthorpej 		    ldep->de_diroffset, &bp, &direntptr);
285c4ad0e4dSpooka 		if (error) {
286798256c9Shannken 			pool_put(&msdosfs_denode_pool, ldep);
287798256c9Shannken 			return error;
288c4ad0e4dSpooka 		}
28998d58548Sjdolecek 		DE_INTERNALIZE(ldep, direntptr);
2909f56dfa5Sad 		brelse(bp, 0);
29198d58548Sjdolecek 	}
29298d58548Sjdolecek 
29398d58548Sjdolecek 	/*
29498d58548Sjdolecek 	 * Fill in a few fields of the vnode and finish filling in the
295798256c9Shannken 	 * denode.
29698d58548Sjdolecek 	 */
29798d58548Sjdolecek 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
29898d58548Sjdolecek 		/*
29998d58548Sjdolecek 		 * Since DOS directory entries that describe directories
30098d58548Sjdolecek 		 * have 0 in the filesize field, we take this opportunity
30198d58548Sjdolecek 		 * to find out the length of the directory and plug it into
30298d58548Sjdolecek 		 * the denode structure.
30398d58548Sjdolecek 		 */
30498d58548Sjdolecek 		u_long size;
30598d58548Sjdolecek 
306798256c9Shannken 		vp->v_type = VDIR;
30798d58548Sjdolecek 		if (ldep->de_StartCluster != MSDOSFSROOT) {
308*8086f46eSthorpej 			error = msdosfs_pcbmap(ldep, CLUST_END, 0, &size, 0);
30998d58548Sjdolecek 			if (error == E2BIG) {
31098d58548Sjdolecek 				ldep->de_FileSize = de_cn2off(pmp, size);
31198d58548Sjdolecek 				error = 0;
31298d58548Sjdolecek 			} else
313798256c9Shannken 				printf("loadvnode(): pcbmap returned %d\n",
314798256c9Shannken 				    error);
31598d58548Sjdolecek 		}
31698d58548Sjdolecek 	} else
317798256c9Shannken 		vp->v_type = VREG;
318c3183f32Spooka 	vref(ldep->de_devvp);
319798256c9Shannken 	if (is_root)
320798256c9Shannken 		vp->v_vflag |= VV_ROOT;
321798256c9Shannken 	vp->v_tag = VT_MSDOSFS;
322798256c9Shannken 	vp->v_op = msdosfs_vnodeop_p;
323798256c9Shannken 	vp->v_data = ldep;
324798256c9Shannken 	ldep->de_vnode = vp;
325798256c9Shannken 	genfs_node_init(vp, &msdosfs_genfsops);
326798256c9Shannken 	uvm_vnp_setsize(vp, ldep->de_FileSize);
327798256c9Shannken 	*new_key = &ldep->de_key;
328798256c9Shannken 
329798256c9Shannken 	return 0;
33098d58548Sjdolecek }
33198d58548Sjdolecek 
33298d58548Sjdolecek int
msdosfs_deupdat(struct denode * dep,int waitfor)333*8086f46eSthorpej msdosfs_deupdat(struct denode *dep, int waitfor)
33498d58548Sjdolecek {
33598d58548Sjdolecek 
336a748ea88Syamt 	return (msdosfs_update(DETOV(dep), NULL, NULL,
337a748ea88Syamt 	    waitfor ? UPDATE_WAIT : 0));
33898d58548Sjdolecek }
33998d58548Sjdolecek 
34098d58548Sjdolecek /*
34198d58548Sjdolecek  * Truncate the file described by dep to the length specified by length.
34298d58548Sjdolecek  */
34398d58548Sjdolecek int
msdosfs_detrunc(struct denode * dep,u_long length,int flags,kauth_cred_t cred)344*8086f46eSthorpej msdosfs_detrunc(struct denode *dep, u_long length, int flags, kauth_cred_t cred)
34598d58548Sjdolecek {
34698d58548Sjdolecek 	int error;
34798d58548Sjdolecek 	int allerror;
34898d58548Sjdolecek 	u_long eofentry;
3499071fc0fSchristos 	u_long chaintofree = 0;
35098d58548Sjdolecek 	daddr_t bn, lastblock;
35198d58548Sjdolecek 	int boff;
35298d58548Sjdolecek 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
35398d58548Sjdolecek 	struct buf *bp;
35498d58548Sjdolecek 	struct msdosfsmount *pmp = dep->de_pmp;
35598d58548Sjdolecek 
35698d58548Sjdolecek #ifdef MSDOSFS_DEBUG
35798d58548Sjdolecek 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
35898d58548Sjdolecek #endif
35998d58548Sjdolecek 
36098d58548Sjdolecek 	/*
36198d58548Sjdolecek 	 * Disallow attempts to truncate the root directory since it is of
36298d58548Sjdolecek 	 * fixed size.  That's just the way dos filesystems are.  We use
36398d58548Sjdolecek 	 * the VROOT bit in the vnode because checking for the directory
36498d58548Sjdolecek 	 * bit and a startcluster of 0 in the denode is not adequate to
36598d58548Sjdolecek 	 * recognize the root directory at this point in a file or
36698d58548Sjdolecek 	 * directory's life.
36798d58548Sjdolecek 	 */
3687dad9f73Sad 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
36998d58548Sjdolecek 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
37098d58548Sjdolecek 		    dep->de_dirclust, dep->de_diroffset);
37198d58548Sjdolecek 		return (EINVAL);
37298d58548Sjdolecek 	}
37398d58548Sjdolecek 
37498d58548Sjdolecek 	uvm_vnp_setsize(DETOV(dep), length);
37598d58548Sjdolecek 
37698d58548Sjdolecek 	if (dep->de_FileSize < length)
377*8086f46eSthorpej 		return (msdosfs_deextend(dep, length, cred));
37898d58548Sjdolecek 	lastblock = de_clcount(pmp, length) - 1;
37998d58548Sjdolecek 
38098d58548Sjdolecek 	/*
38198d58548Sjdolecek 	 * If the desired length is 0 then remember the starting cluster of
38298d58548Sjdolecek 	 * the file and set the StartCluster field in the directory entry
38398d58548Sjdolecek 	 * to 0.  If the desired length is not zero, then get the number of
38498d58548Sjdolecek 	 * the last cluster in the shortened file.  Then get the number of
38598d58548Sjdolecek 	 * the first cluster in the part of the file that is to be freed.
38698d58548Sjdolecek 	 * Then set the next cluster pointer in the last cluster of the
38798d58548Sjdolecek 	 * file to CLUST_EOFE.
38898d58548Sjdolecek 	 */
38998d58548Sjdolecek 	if (length == 0) {
39098d58548Sjdolecek 		chaintofree = dep->de_StartCluster;
39198d58548Sjdolecek 		dep->de_StartCluster = 0;
39298d58548Sjdolecek 		eofentry = ~0;
39398d58548Sjdolecek 	} else {
394*8086f46eSthorpej 		error = msdosfs_pcbmap(dep, lastblock, 0, &eofentry, 0);
39598d58548Sjdolecek 		if (error) {
39698d58548Sjdolecek #ifdef MSDOSFS_DEBUG
39798d58548Sjdolecek 			printf("detrunc(): pcbmap fails %d\n", error);
39898d58548Sjdolecek #endif
39998d58548Sjdolecek 			return (error);
40098d58548Sjdolecek 		}
40198d58548Sjdolecek 	}
40298d58548Sjdolecek 
40398d58548Sjdolecek 	/*
40498d58548Sjdolecek 	 * If the new length is not a multiple of the cluster size then we
40598d58548Sjdolecek 	 * must zero the tail end of the new last cluster in case it
40698d58548Sjdolecek 	 * becomes part of the file again because of a seek.
40798d58548Sjdolecek 	 */
40898d58548Sjdolecek 	if ((boff = length & pmp->pm_crbomask) != 0) {
40998d58548Sjdolecek 		if (isadir) {
41098d58548Sjdolecek 			bn = cntobn(pmp, eofentry);
4114f0ca272Sscw 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
4126e392401Smaxv 			    pmp->pm_bpcluster, B_MODIFY, &bp);
41398d58548Sjdolecek 			if (error) {
41498d58548Sjdolecek #ifdef MSDOSFS_DEBUG
41598d58548Sjdolecek 				printf("detrunc(): bread fails %d\n", error);
41698d58548Sjdolecek #endif
41798d58548Sjdolecek 				return (error);
41898d58548Sjdolecek 			}
41953524e44Schristos 			memset((char *)bp->b_data + boff, 0,
42053524e44Schristos 			    pmp->pm_bpcluster - boff);
42198d58548Sjdolecek 			if (flags & IO_SYNC)
42298d58548Sjdolecek 				bwrite(bp);
42398d58548Sjdolecek 			else
42498d58548Sjdolecek 				bdwrite(bp);
42598d58548Sjdolecek 		} else {
426d296304eShannken 			ubc_zerorange(&DETOV(dep)->v_uobj, length,
427d296304eShannken 				      pmp->pm_bpcluster - boff,
428f5ad84fdSad 				      UBC_VNODE_FLAGS(DETOV(dep)));
42998d58548Sjdolecek 		}
43098d58548Sjdolecek 	}
43198d58548Sjdolecek 
43298d58548Sjdolecek 	/*
43398d58548Sjdolecek 	 * Write out the updated directory entry.  Even if the update fails
43498d58548Sjdolecek 	 * we free the trailing clusters.
43598d58548Sjdolecek 	 */
43698d58548Sjdolecek 	dep->de_FileSize = length;
43798d58548Sjdolecek 	if (!isadir)
43898d58548Sjdolecek 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
43998d58548Sjdolecek 	vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
440*8086f46eSthorpej 	allerror = msdosfs_deupdat(dep, 1);
44198d58548Sjdolecek #ifdef MSDOSFS_DEBUG
44298d58548Sjdolecek 	printf("detrunc(): allerror %d, eofentry %lu\n",
44398d58548Sjdolecek 	       allerror, eofentry);
44498d58548Sjdolecek #endif
44598d58548Sjdolecek 
446*8086f46eSthorpej 	msdosfs_fc_purge(dep, lastblock + 1);
447b7bd2486Shannken 
44898d58548Sjdolecek 	/*
44998d58548Sjdolecek 	 * If we need to break the cluster chain for the file then do it
45098d58548Sjdolecek 	 * now.
45198d58548Sjdolecek 	 */
45298d58548Sjdolecek 	if (eofentry != ~0) {
453*8086f46eSthorpej 		error = msdosfs_fatentry(FAT_GET_AND_SET, pmp, eofentry,
45498d58548Sjdolecek 				 &chaintofree, CLUST_EOFE);
45598d58548Sjdolecek 		if (error) {
45698d58548Sjdolecek #ifdef MSDOSFS_DEBUG
45798d58548Sjdolecek 			printf("detrunc(): fatentry errors %d\n", error);
45898d58548Sjdolecek #endif
45998d58548Sjdolecek 			return (error);
46098d58548Sjdolecek 		}
46198d58548Sjdolecek 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
46298d58548Sjdolecek 			    eofentry);
46398d58548Sjdolecek 	}
46498d58548Sjdolecek 
46598d58548Sjdolecek 	/*
46698d58548Sjdolecek 	 * Now free the clusters removed from the file because of the
46798d58548Sjdolecek 	 * truncation.
46898d58548Sjdolecek 	 */
46998d58548Sjdolecek 	if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
470*8086f46eSthorpej 		msdosfs_freeclusterchain(pmp, chaintofree);
47198d58548Sjdolecek 
47298d58548Sjdolecek 	return (allerror);
47398d58548Sjdolecek }
47498d58548Sjdolecek 
47598d58548Sjdolecek /*
47698d58548Sjdolecek  * Extend the file described by dep to length specified by length.
47798d58548Sjdolecek  */
47898d58548Sjdolecek int
msdosfs_deextend(struct denode * dep,u_long length,kauth_cred_t cred)479*8086f46eSthorpej msdosfs_deextend(struct denode *dep, u_long length, kauth_cred_t cred)
48098d58548Sjdolecek {
48198d58548Sjdolecek 	struct msdosfsmount *pmp = dep->de_pmp;
48298d58548Sjdolecek 	u_long count, osize;
48398d58548Sjdolecek 	int error;
48498d58548Sjdolecek 
48598d58548Sjdolecek 	/*
48698d58548Sjdolecek 	 * The root of a DOS filesystem cannot be extended.
48798d58548Sjdolecek 	 */
4887dad9f73Sad 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
48998d58548Sjdolecek 		return (EINVAL);
49098d58548Sjdolecek 
49198d58548Sjdolecek 	/*
49298d58548Sjdolecek 	 * Directories cannot be extended.
49398d58548Sjdolecek 	 */
49498d58548Sjdolecek 	if (dep->de_Attributes & ATTR_DIRECTORY)
49598d58548Sjdolecek 		return (EISDIR);
49698d58548Sjdolecek 
49798d58548Sjdolecek 	if (length <= dep->de_FileSize)
49898d58548Sjdolecek 		panic("deextend: file too large");
49998d58548Sjdolecek 
50098d58548Sjdolecek 	/*
50198d58548Sjdolecek 	 * Compute the number of clusters to allocate.
50298d58548Sjdolecek 	 */
50398d58548Sjdolecek 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
50498d58548Sjdolecek 	if (count > 0) {
50598d58548Sjdolecek 		if (count > pmp->pm_freeclustercount)
50698d58548Sjdolecek 			return (ENOSPC);
507*8086f46eSthorpej 		error = msdosfs_extendfile(dep, count, NULL, NULL, DE_CLEAR);
50898d58548Sjdolecek 		if (error) {
50998d58548Sjdolecek 			/* truncate the added clusters away again */
510*8086f46eSthorpej 			(void) msdosfs_detrunc(dep, dep->de_FileSize, 0, cred);
51198d58548Sjdolecek 			return (error);
51298d58548Sjdolecek 		}
51398d58548Sjdolecek 	}
51498d58548Sjdolecek 
515a661982bSreinoud 	/*
516d296304eShannken 	 * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
517a661982bSreinoud 	 * memset(); we set the write size so ubc won't read in file data that
518a661982bSreinoud 	 * is zero'd later.
519a661982bSreinoud 	 */
52098d58548Sjdolecek 	osize = dep->de_FileSize;
52198d58548Sjdolecek 	dep->de_FileSize = length;
522a661982bSreinoud 	uvm_vnp_setwritesize(DETOV(dep), (voff_t)dep->de_FileSize);
52398d58548Sjdolecek 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
524d296304eShannken 	ubc_zerorange(&DETOV(dep)->v_uobj, (off_t)osize,
525d296304eShannken 	    (size_t)(round_page(dep->de_FileSize) - osize),
526f5ad84fdSad 	    UBC_VNODE_FLAGS(DETOV(dep)));
527a661982bSreinoud 	uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
528*8086f46eSthorpej 	return (msdosfs_deupdat(dep, 1));
52998d58548Sjdolecek }
53098d58548Sjdolecek 
53198d58548Sjdolecek int
msdosfs_reclaim(void * v)532454af1c0Sdsl msdosfs_reclaim(void *v)
53398d58548Sjdolecek {
5347f7aad09Sriastradh 	struct vop_reclaim_v2_args /* {
53598d58548Sjdolecek 		struct vnode *a_vp;
53698d58548Sjdolecek 	} */ *ap = v;
53798d58548Sjdolecek 	struct vnode *vp = ap->a_vp;
53898d58548Sjdolecek 	struct denode *dep = VTODE(vp);
53998d58548Sjdolecek 
5407f7aad09Sriastradh 	VOP_UNLOCK(vp);
5417f7aad09Sriastradh 
54298d58548Sjdolecek #ifdef MSDOSFS_DEBUG
54398d58548Sjdolecek 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
54498d58548Sjdolecek 	    dep, dep->de_Name, dep->de_refcnt);
54598d58548Sjdolecek #endif
54698d58548Sjdolecek 
54798d58548Sjdolecek 	/*
54898d58548Sjdolecek 	 * Purge old data structures associated with the denode.
54998d58548Sjdolecek 	 */
55098d58548Sjdolecek 	if (dep->de_devvp) {
55198d58548Sjdolecek 		vrele(dep->de_devvp);
55298d58548Sjdolecek 		dep->de_devvp = 0;
55398d58548Sjdolecek 	}
55498d58548Sjdolecek #if 0 /* XXX */
55598d58548Sjdolecek 	dep->de_flag = 0;
55698d58548Sjdolecek #endif
557de57916fShannken 	/*
558de57916fShannken 	 * To interlock with msdosfs_sync().
559de57916fShannken 	 */
560adbb9ec2Sad 	genfs_node_destroy(vp);
561de57916fShannken 	mutex_enter(vp->v_interlock);
56298d58548Sjdolecek 	vp->v_data = NULL;
563de57916fShannken 	mutex_exit(vp->v_interlock);
564de57916fShannken 	pool_put(&msdosfs_denode_pool, dep);
56598d58548Sjdolecek 	return (0);
56698d58548Sjdolecek }
56798d58548Sjdolecek 
56898d58548Sjdolecek int
msdosfs_inactive(void * v)569454af1c0Sdsl msdosfs_inactive(void *v)
57098d58548Sjdolecek {
57187fb3229Sriastradh 	struct vop_inactive_v2_args /* {
57298d58548Sjdolecek 		struct vnode *a_vp;
5734a780c9aSad 		bool *a_recycle;
57498d58548Sjdolecek 	} */ *ap = v;
57598d58548Sjdolecek 	struct vnode *vp = ap->a_vp;
57698d58548Sjdolecek 	struct denode *dep = VTODE(vp);
57798d58548Sjdolecek 	int error = 0;
57898d58548Sjdolecek 
57998d58548Sjdolecek #ifdef MSDOSFS_DEBUG
58098d58548Sjdolecek 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
58198d58548Sjdolecek #endif
58298d58548Sjdolecek 
58398d58548Sjdolecek 	/*
58498d58548Sjdolecek 	 * Get rid of denodes related to stale file handles.
58598d58548Sjdolecek 	 */
58698d58548Sjdolecek 	if (dep->de_Name[0] == SLOT_DELETED)
58798d58548Sjdolecek 		goto out;
58898d58548Sjdolecek 
58998d58548Sjdolecek 	/*
59098d58548Sjdolecek 	 * If the file has been deleted and it is on a read/write
59198d58548Sjdolecek 	 * filesystem, then truncate the file, and mark the directory slot
59298d58548Sjdolecek 	 * as empty.  (This may not be necessary for the dos filesystem.)
59398d58548Sjdolecek 	 */
59498d58548Sjdolecek #ifdef MSDOSFS_DEBUG
59598d58548Sjdolecek 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
59698d58548Sjdolecek 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag,
59798d58548Sjdolecek 		(vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
59898d58548Sjdolecek #endif
59998d58548Sjdolecek 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
60098d58548Sjdolecek 		if (dep->de_FileSize != 0) {
601*8086f46eSthorpej 			error = msdosfs_detrunc(dep, (u_long)0, 0, NOCRED);
60298d58548Sjdolecek 		}
60398d58548Sjdolecek 		dep->de_Name[0] = SLOT_DELETED;
604fff1c84cShannken 		msdosfs_fh_remove(dep->de_pmp,
605fff1c84cShannken 		    dep->de_dirclust, dep->de_diroffset);
60698d58548Sjdolecek 	}
607*8086f46eSthorpej 	msdosfs_deupdat(dep, 0);
60898d58548Sjdolecek out:
60998d58548Sjdolecek 	/*
61098d58548Sjdolecek 	 * If we are done with the denode, reclaim it
61198d58548Sjdolecek 	 * so that it can be reused immediately.
61298d58548Sjdolecek 	 */
61398d58548Sjdolecek #ifdef MSDOSFS_DEBUG
61423bf8800Sad 	printf("msdosfs_inactive(): usecount %d, de_Name[0] %x\n",
61523bf8800Sad 		vrefcnt(vp), dep->de_Name[0]);
61698d58548Sjdolecek #endif
6174a780c9aSad 	*ap->a_recycle = (dep->de_Name[0] == SLOT_DELETED);
61887fb3229Sriastradh 
61998d58548Sjdolecek 	return (error);
62098d58548Sjdolecek }
62198d58548Sjdolecek 
62298d58548Sjdolecek int
msdosfs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)623168cd830Schristos msdosfs_gop_alloc(struct vnode *vp, off_t off,
624168cd830Schristos     off_t len, int flags, kauth_cred_t cred)
62598d58548Sjdolecek {
62698d58548Sjdolecek 	return 0;
62798d58548Sjdolecek }
628b7bfe828Syamt 
629b7bfe828Syamt void
msdosfs_gop_markupdate(struct vnode * vp,int flags)630b7bfe828Syamt msdosfs_gop_markupdate(struct vnode *vp, int flags)
631b7bfe828Syamt {
632b7bfe828Syamt 	u_long mask = 0;
633b7bfe828Syamt 
634b7bfe828Syamt 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
635b7bfe828Syamt 		mask = DE_ACCESS;
636b7bfe828Syamt 	}
637b7bfe828Syamt 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
638b7bfe828Syamt 		mask |= DE_UPDATE;
639b7bfe828Syamt 	}
640b7bfe828Syamt 	if (mask) {
641b7bfe828Syamt 		struct denode *dep = VTODE(vp);
642b7bfe828Syamt 
643b7bfe828Syamt 		dep->de_flag |= mask;
644b7bfe828Syamt 	}
645b7bfe828Syamt }
646fff1c84cShannken 
647fff1c84cShannken int
msdosfs_fh_enter(struct msdosfsmount * pmp,uint32_t dircluster,uint32_t diroffset,uint32_t * genp)648fff1c84cShannken msdosfs_fh_enter(struct msdosfsmount *pmp,
649fff1c84cShannken      uint32_t dircluster, uint32_t diroffset, uint32_t *genp)
650fff1c84cShannken {
651fff1c84cShannken 	struct fh_key fhkey;
652fff1c84cShannken 	struct fh_node *fhp;
653fff1c84cShannken 
654fff1c84cShannken 	fhkey.fhk_mount = pmp;
655fff1c84cShannken 	fhkey.fhk_dircluster = dircluster;
656fff1c84cShannken 	fhkey.fhk_diroffset = diroffset;
657fff1c84cShannken 
658fff1c84cShannken 	mutex_enter(&fh_lock);
659fff1c84cShannken 	fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
660fff1c84cShannken 	if (fhp == NULL) {
661fff1c84cShannken 		mutex_exit(&fh_lock);
662fff1c84cShannken 		fhp = pool_get(&fh_pool, PR_WAITOK);
663fff1c84cShannken 		mutex_enter(&fh_lock);
664fff1c84cShannken 		fhp->fh_key = fhkey;
665fff1c84cShannken 		fhp->fh_gen = fh_generation++;
666fff1c84cShannken 		rb_tree_insert_node(&fh_rbtree, fhp);
667fff1c84cShannken 	}
668fff1c84cShannken 	*genp = fhp->fh_gen;
669fff1c84cShannken 	mutex_exit(&fh_lock);
670fff1c84cShannken 	return 0;
671fff1c84cShannken }
672fff1c84cShannken 
673fff1c84cShannken int
msdosfs_fh_remove(struct msdosfsmount * pmp,uint32_t dircluster,uint32_t diroffset)674fff1c84cShannken msdosfs_fh_remove(struct msdosfsmount *pmp,
675fff1c84cShannken      uint32_t dircluster, uint32_t diroffset)
676fff1c84cShannken {
677fff1c84cShannken 	struct fh_key fhkey;
678fff1c84cShannken 	struct fh_node *fhp;
679fff1c84cShannken 
680fff1c84cShannken 	fhkey.fhk_mount = pmp;
681fff1c84cShannken 	fhkey.fhk_dircluster = dircluster;
682fff1c84cShannken 	fhkey.fhk_diroffset = diroffset;
683fff1c84cShannken 
684fff1c84cShannken 	mutex_enter(&fh_lock);
685fff1c84cShannken 	fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
686fff1c84cShannken 	if (fhp == NULL) {
687fff1c84cShannken 		mutex_exit(&fh_lock);
688fff1c84cShannken 		return ENOENT;
689fff1c84cShannken 	}
690fff1c84cShannken 	rb_tree_remove_node(&fh_rbtree, fhp);
691fff1c84cShannken 	mutex_exit(&fh_lock);
692fff1c84cShannken 	pool_put(&fh_pool, fhp);
693fff1c84cShannken 	return 0;
694fff1c84cShannken }
695fff1c84cShannken 
696fff1c84cShannken int
msdosfs_fh_lookup(struct msdosfsmount * pmp,uint32_t dircluster,uint32_t diroffset,uint32_t * genp)697fff1c84cShannken msdosfs_fh_lookup(struct msdosfsmount *pmp,
698fff1c84cShannken      uint32_t dircluster, uint32_t diroffset, uint32_t *genp)
699fff1c84cShannken {
700fff1c84cShannken 	struct fh_key fhkey;
701fff1c84cShannken 	struct fh_node *fhp;
702fff1c84cShannken 
703fff1c84cShannken 	fhkey.fhk_mount = pmp;
704fff1c84cShannken 	fhkey.fhk_dircluster = dircluster;
705fff1c84cShannken 	fhkey.fhk_diroffset = diroffset;
706fff1c84cShannken 
707fff1c84cShannken 	mutex_enter(&fh_lock);
708fff1c84cShannken 	fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
709fff1c84cShannken 	if (fhp == NULL) {
710fff1c84cShannken 		mutex_exit(&fh_lock);
711fff1c84cShannken 		return ESTALE;
712fff1c84cShannken 	}
713fff1c84cShannken 	*genp = fhp->fh_gen;
714fff1c84cShannken 	mutex_exit(&fh_lock);
715fff1c84cShannken 	return 0;
716fff1c84cShannken }
717fff1c84cShannken 
718fff1c84cShannken void
msdosfs_fh_destroy(struct msdosfsmount * pmp)719fff1c84cShannken msdosfs_fh_destroy(struct msdosfsmount *pmp)
720fff1c84cShannken {
721fff1c84cShannken 	struct fh_key fhkey;
722fff1c84cShannken 	struct fh_node *fhp, *nfhp;
723fff1c84cShannken 
724fff1c84cShannken 	fhkey.fhk_mount = pmp;
725fff1c84cShannken 	fhkey.fhk_dircluster = 0;
726fff1c84cShannken 	fhkey.fhk_diroffset = 0;
727fff1c84cShannken 
728fff1c84cShannken 	mutex_enter(&fh_lock);
729fff1c84cShannken 	for (fhp = rb_tree_find_node_geq(&fh_rbtree, &fhkey);
730fff1c84cShannken 	    fhp != NULL && fhp->fh_mount == pmp; fhp = nfhp) {
731fff1c84cShannken 		nfhp = rb_tree_iterate(&fh_rbtree, fhp, RB_DIR_RIGHT);
732fff1c84cShannken 		rb_tree_remove_node(&fh_rbtree, fhp);
733fff1c84cShannken 		pool_put(&fh_pool, fhp);
734fff1c84cShannken 	}
735fff1c84cShannken #ifdef DIAGNOSTIC
736fff1c84cShannken 	RB_TREE_FOREACH(fhp, &fh_rbtree) {
737fff1c84cShannken 		KASSERT(fhp->fh_mount != pmp);
738fff1c84cShannken 	}
739fff1c84cShannken #endif
740fff1c84cShannken 	mutex_exit(&fh_lock);
741fff1c84cShannken }
742