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