Home | History | Annotate | Line # | Download | only in chfs
chfs.h revision 1.3
      1  1.3  ahoka /*	$NetBSD: chfs.h,v 1.3 2011/11/24 21:38:44 ahoka Exp $	*/
      2  1.1  ahoka 
      3  1.1  ahoka /*-
      4  1.1  ahoka  * Copyright (c) 2010 Department of Software Engineering,
      5  1.1  ahoka  *		      University of Szeged, Hungary
      6  1.1  ahoka  * Copyright (C) 2009 Ferenc Havasi <havasi (at) inf.u-szeged.hu>
      7  1.1  ahoka  * Copyright (C) 2009 Zoltan Sogor <weth (at) inf.u-szeged.hu>
      8  1.1  ahoka  * Copyright (C) 2009 David Tengeri <dtengeri (at) inf.u-szeged.hu>
      9  1.1  ahoka  * Copyright (C) 2009 Tamas Toth <ttoth (at) inf.u-szeged.hu>
     10  1.1  ahoka  * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
     11  1.1  ahoka  * All rights reserved.
     12  1.1  ahoka  *
     13  1.1  ahoka  * This code is derived from software contributed to The NetBSD Foundation
     14  1.1  ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     15  1.1  ahoka  *
     16  1.1  ahoka  * Redistribution and use in source and binary forms, with or without
     17  1.1  ahoka  * modification, are permitted provided that the following conditions
     18  1.1  ahoka  * are met:
     19  1.1  ahoka  * 1. Redistributions of source code must retain the above copyright
     20  1.1  ahoka  *    notice, this list of conditions and the following disclaimer.
     21  1.1  ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  ahoka  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  ahoka  *    documentation and/or other materials provided with the distribution.
     24  1.1  ahoka  *
     25  1.1  ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26  1.1  ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  1.1  ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  1.1  ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29  1.1  ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     30  1.1  ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     31  1.1  ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     32  1.1  ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     33  1.1  ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  ahoka  * SUCH DAMAGE.
     36  1.1  ahoka  */
     37  1.1  ahoka 
     38  1.1  ahoka #ifndef __CHFS_H__
     39  1.1  ahoka #define __CHFS_H__
     40  1.1  ahoka 
     41  1.3  ahoka #if 0
     42  1.1  ahoka #define DBG_MSG
     43  1.1  ahoka #define DBG_MSG_GC
     44  1.3  ahoka #endif
     45  1.1  ahoka 
     46  1.1  ahoka #include <sys/param.h>
     47  1.1  ahoka #include <sys/kernel.h>
     48  1.1  ahoka #include <sys/cdefs.h>
     49  1.1  ahoka #include <sys/stdint.h>
     50  1.1  ahoka #include <sys/types.h>
     51  1.1  ahoka #include <sys/tree.h>
     52  1.1  ahoka #include <sys/queue.h>
     53  1.1  ahoka #include <sys/kmem.h>
     54  1.1  ahoka #include <sys/endian.h>
     55  1.1  ahoka #include <sys/rwlock.h>
     56  1.1  ahoka #include <sys/condvar.h>
     57  1.1  ahoka #include <sys/mutex.h>
     58  1.1  ahoka #include <sys/kthread.h>
     59  1.1  ahoka #include <sys/rbtree.h>
     60  1.1  ahoka #include <sys/vnode.h>
     61  1.1  ahoka #include <sys/mount.h>
     62  1.1  ahoka #include <sys/hash.h>
     63  1.1  ahoka #include <sys/module.h>
     64  1.1  ahoka #include <sys/dirent.h>
     65  1.1  ahoka 
     66  1.1  ahoka #include <ufs/ufs/quota.h>
     67  1.1  ahoka #include <ufs/ufs/ufsmount.h>
     68  1.1  ahoka #include <ufs/ufs/dir.h>
     69  1.1  ahoka 
     70  1.1  ahoka /* XXX shouldnt be defined here, but needed by chfs_inode.h */
     71  1.1  ahoka TAILQ_HEAD(chfs_dirent_list, chfs_dirent);
     72  1.1  ahoka 
     73  1.1  ahoka #include "chfs_pool.h"
     74  1.1  ahoka #include "ebh.h"
     75  1.1  ahoka #include "media.h"
     76  1.1  ahoka #include "chfs_inode.h"
     77  1.1  ahoka 
     78  1.1  ahoka #ifndef MOUNT_CHFS
     79  1.1  ahoka #define MOUNT_CHFS "chfs"
     80  1.1  ahoka #endif
     81  1.1  ahoka 
     82  1.1  ahoka #define CHFS_ROOTINO ROOTINO    /* ROOTINO == 2 */
     83  1.1  ahoka 
     84  1.1  ahoka enum {
     85  1.1  ahoka 	VNO_STATE_UNCHECKED,	/* CRC checks not yet done */
     86  1.1  ahoka 	VNO_STATE_CHECKING,	/* CRC checks in progress */
     87  1.1  ahoka 	VNO_STATE_PRESENT,	/* In core */
     88  1.1  ahoka 	VNO_STATE_CHECKEDABSENT,/* Checked, cleared again */
     89  1.1  ahoka 	VNO_STATE_GC,		/* GCing a 'pristine' node */
     90  1.1  ahoka 	VNO_STATE_READING,	/* In read_inode() */
     91  1.1  ahoka 	VNO_STATE_CLEARING	/* In clear_inode() */
     92  1.1  ahoka };
     93  1.1  ahoka 
     94  1.1  ahoka #define VNODECACHE_SIZE 128
     95  1.1  ahoka 
     96  1.1  ahoka #define MAX_READ_FREE(chmp) (((chmp)->chm_ebh)->eb_size / 8)
     97  1.1  ahoka /* an eraseblock will be clean if its dirty size is smaller than this */
     98  1.1  ahoka #define MAX_DIRTY_TO_CLEAN 255
     99  1.1  ahoka #define VERY_DIRTY(chmp, size) ((size) >= (((chmp)->chm_ebh)->eb_size / 2))
    100  1.1  ahoka 
    101  1.1  ahoka #define CHFS_PAD(x) (((x)+3)&~3)
    102  1.1  ahoka 
    103  1.1  ahoka enum {
    104  1.1  ahoka 	CHFS_NODE_OK = 0,
    105  1.1  ahoka 	CHFS_NODE_BADMAGIC,
    106  1.1  ahoka 	CHFS_NODE_BADCRC,
    107  1.1  ahoka 	CHFS_NODE_BADNAMECRC
    108  1.1  ahoka };
    109  1.1  ahoka 
    110  1.1  ahoka enum {
    111  1.1  ahoka 	CHFS_BLK_STATE_FREE = 100,
    112  1.1  ahoka 	CHFS_BLK_STATE_CLEAN,
    113  1.1  ahoka 	CHFS_BLK_STATE_PARTDIRTY,
    114  1.1  ahoka 	CHFS_BLK_STATE_ALLDIRTY
    115  1.1  ahoka };
    116  1.1  ahoka 
    117  1.1  ahoka extern struct pool chfs_inode_pool;
    118  1.1  ahoka extern const struct genfs_ops chfs_genfsops;
    119  1.1  ahoka 
    120  1.1  ahoka /**
    121  1.1  ahoka  * struct chfs_node_ref - a reference to a node
    122  1.1  ahoka  * @lnr: logical identifier of the eraseblock where the node is
    123  1.1  ahoka  * @offset: offset int hte eraseblock where the node starts
    124  1.1  ahoka  * @next: used at data and dirent nodes, it points to the next data node which
    125  1.1  ahoka  * 		  belongs to the same vnode
    126  1.1  ahoka  */
    127  1.1  ahoka struct chfs_node_ref
    128  1.1  ahoka {
    129  1.1  ahoka 	struct chfs_node_ref *nref_next;
    130  1.1  ahoka 	uint32_t nref_lnr;
    131  1.1  ahoka 	uint32_t nref_offset;
    132  1.1  ahoka };
    133  1.1  ahoka 
    134  1.1  ahoka /* Constants  for allocating node refs */
    135  1.1  ahoka #define REFS_BLOCK_LEN (255/sizeof(struct chfs_node_ref))
    136  1.1  ahoka #define REF_EMPTY_NODE (UINT_MAX)
    137  1.1  ahoka #define REF_LINK_TO_NEXT (UINT_MAX - 1)
    138  1.1  ahoka 
    139  1.1  ahoka enum {
    140  1.1  ahoka 	CHFS_NORMAL_NODE_MASK,
    141  1.1  ahoka 	CHFS_UNCHECKED_NODE_MASK,
    142  1.1  ahoka 	CHFS_OBSOLETE_NODE_MASK,
    143  1.1  ahoka 	CHFS_PRISTINE_NODE_MASK
    144  1.1  ahoka };
    145  1.1  ahoka 
    146  1.1  ahoka #define CHFS_REF_FLAGS(ref)		((ref)->nref_offset & 3)
    147  1.1  ahoka #define CHFS_REF_OBSOLETE(ref)	(((ref)->nref_offset & 3) == CHFS_OBSOLETE_NODE_MASK)
    148  1.1  ahoka #define CHFS_MARK_REF_NORMAL(ref)					      \
    149  1.1  ahoka 	do {								      \
    150  1.1  ahoka 		(ref)->nref_offset = CHFS_GET_OFS((ref)->nref_offset) | CHFS_NORMAL_NODE_MASK; \
    151  1.1  ahoka 	} while(0)
    152  1.1  ahoka 
    153  1.1  ahoka #define CHFS_GET_OFS(ofs) (ofs & ~ 3)
    154  1.1  ahoka 
    155  1.1  ahoka static inline struct chfs_node_ref *
    156  1.1  ahoka node_next(struct chfs_node_ref *nref)
    157  1.1  ahoka {
    158  1.1  ahoka 	//dbg("node next: %u : %u\n", nref->nref_lnr, nref->nref_offset);
    159  1.1  ahoka 	nref++;
    160  1.1  ahoka 	//dbg("nref++: %u : %u\n", nref->nref_lnr, nref->nref_offset);
    161  1.1  ahoka 
    162  1.1  ahoka 	if (nref->nref_lnr == REF_LINK_TO_NEXT) {
    163  1.1  ahoka 		//dbg("link to next\n");
    164  1.1  ahoka 		nref = nref->nref_next;
    165  1.1  ahoka 		if (!nref)
    166  1.1  ahoka 			return nref;
    167  1.1  ahoka 	}
    168  1.1  ahoka 
    169  1.1  ahoka 	if (nref->nref_lnr == REF_EMPTY_NODE) {
    170  1.1  ahoka 		//dbg("empty\n");
    171  1.1  ahoka 		return NULL;
    172  1.1  ahoka 	}
    173  1.1  ahoka 
    174  1.1  ahoka 	return nref;
    175  1.1  ahoka }
    176  1.1  ahoka 
    177  1.1  ahoka /**
    178  1.1  ahoka  * struct chfs_dirent - full representation of a directory entry
    179  1.1  ahoka  */
    180  1.1  ahoka struct chfs_dirent
    181  1.1  ahoka {
    182  1.1  ahoka 	struct chfs_node_ref *nref;
    183  1.1  ahoka //	struct chfs_dirent *next;
    184  1.1  ahoka 	TAILQ_ENTRY(chfs_dirent) fds;
    185  1.1  ahoka 	uint64_t version;
    186  1.1  ahoka 	ino_t vno;
    187  1.1  ahoka 	uint32_t nhash;
    188  1.1  ahoka 	enum vtype type;
    189  1.1  ahoka 	uint8_t  nsize;
    190  1.1  ahoka 	uint8_t  name[0];
    191  1.1  ahoka 
    192  1.1  ahoka 	/* used by chfs_alloc_dirent and free counterpart */
    193  1.1  ahoka //	size_t alloc_size;
    194  1.1  ahoka };
    195  1.1  ahoka 
    196  1.1  ahoka struct chfs_tmp_dnode {
    197  1.1  ahoka 	struct chfs_full_dnode *node;
    198  1.1  ahoka 	uint64_t version;
    199  1.1  ahoka 	uint32_t data_crc;
    200  1.1  ahoka 	//uint32_t partial_crc;
    201  1.1  ahoka 	//uint16_t csize;
    202  1.1  ahoka 	uint16_t overlapped;
    203  1.1  ahoka 	struct chfs_tmp_dnode *next;
    204  1.1  ahoka };
    205  1.1  ahoka 
    206  1.1  ahoka struct chfs_tmp_dnode_info {
    207  1.1  ahoka 	struct rb_node rb_node;
    208  1.1  ahoka 	struct chfs_tmp_dnode *tmpnode;
    209  1.1  ahoka };
    210  1.1  ahoka 
    211  1.1  ahoka struct chfs_readinode_info {
    212  1.1  ahoka 	struct rb_tree tdi_root;
    213  1.1  ahoka 	struct chfs_tmp_dnode_info *mdata_tn;
    214  1.1  ahoka 	uint64_t highest_version;
    215  1.1  ahoka 	struct chfs_node_ref *latest_ref;
    216  1.1  ahoka };
    217  1.1  ahoka 
    218  1.1  ahoka struct chfs_full_dnode {
    219  1.1  ahoka 	struct chfs_node_ref *nref;
    220  1.1  ahoka 	uint64_t ofs;
    221  1.1  ahoka 	uint32_t size;
    222  1.1  ahoka 	uint32_t frags;
    223  1.1  ahoka };
    224  1.1  ahoka 
    225  1.1  ahoka struct chfs_node_frag {
    226  1.1  ahoka 	struct rb_node rb_node;
    227  1.1  ahoka 	struct chfs_full_dnode *node;
    228  1.1  ahoka 	uint32_t size;
    229  1.1  ahoka 	uint64_t ofs;
    230  1.1  ahoka };
    231  1.1  ahoka 
    232  1.1  ahoka static inline struct chfs_node_frag *
    233  1.1  ahoka frag_first(struct rb_tree *tree)
    234  1.1  ahoka {
    235  1.1  ahoka 	struct chfs_node_frag *frag;
    236  1.1  ahoka 
    237  1.1  ahoka 	frag = (struct chfs_node_frag *)RB_TREE_MIN(tree);
    238  1.1  ahoka 
    239  1.1  ahoka 	return frag;
    240  1.1  ahoka }
    241  1.1  ahoka 
    242  1.1  ahoka static inline struct chfs_node_frag *
    243  1.1  ahoka frag_last(struct rb_tree *tree)
    244  1.1  ahoka {
    245  1.1  ahoka 	struct chfs_node_frag *frag;
    246  1.1  ahoka 
    247  1.1  ahoka 	frag = (struct chfs_node_frag *)RB_TREE_MAX(tree);
    248  1.1  ahoka 
    249  1.1  ahoka 	return frag;
    250  1.1  ahoka }
    251  1.1  ahoka 
    252  1.1  ahoka #define frag_next(tree, frag) (struct chfs_node_frag *)rb_tree_iterate(tree, frag, RB_DIR_RIGHT)
    253  1.1  ahoka #define frag_prev(tree, frag) (struct chfs_node_frag *)rb_tree_iterate(tree, frag, RB_DIR_LEFT)
    254  1.1  ahoka 
    255  1.1  ahoka 
    256  1.1  ahoka /* XXX hack
    257  1.1  ahoka    #ifndef CHFS_FRAG_TREE
    258  1.1  ahoka    #define CHFS_FRAG_TREE
    259  1.1  ahoka    RB_HEAD(chfs_frag_tree, chfs_node_frag);
    260  1.1  ahoka    #endif
    261  1.1  ahoka */
    262  1.1  ahoka 
    263  1.1  ahoka /* for prototypes, properly defined in chfs_inode.h */
    264  1.1  ahoka //struct chfs_inode_ext;
    265  1.1  ahoka 
    266  1.1  ahoka /**
    267  1.1  ahoka  * struct chfs_vnode_cache - in memory representation of a vnode
    268  1.1  ahoka  * @v: pointer to the vnode info node
    269  1.1  ahoka  * @dnode: pointer to the list of data nodes
    270  1.1  ahoka  * @dirents: pointer to the list of directory entries
    271  1.1  ahoka  * @vno_version: used only during scan, holds the current version number of
    272  1.1  ahoka  * 				 chfs_flash_vnode
    273  1.1  ahoka  * @scan_dirents: used only during scan, holds the full representation of
    274  1.1  ahoka  * 				  directory entries of this vnode
    275  1.1  ahoka  * @pvno: parent vnode number
    276  1.1  ahoka  * @nlink: number of links to this vnode
    277  1.1  ahoka  */
    278  1.1  ahoka struct chfs_vnode_cache {
    279  1.1  ahoka //	struct chfs_dirent *scan_dirents;
    280  1.1  ahoka 	void *p;
    281  1.1  ahoka 	struct chfs_dirent_list scan_dirents;
    282  1.1  ahoka 
    283  1.1  ahoka 	struct chfs_node_ref *v;
    284  1.1  ahoka 	struct chfs_node_ref *dnode;
    285  1.1  ahoka 	struct chfs_node_ref *dirents;
    286  1.1  ahoka 
    287  1.1  ahoka 	uint64_t *vno_version;
    288  1.1  ahoka 	uint64_t highest_version;
    289  1.1  ahoka 
    290  1.1  ahoka 	uint8_t flags;
    291  1.1  ahoka 	uint16_t state;
    292  1.1  ahoka 	ino_t vno;
    293  1.1  ahoka 	ino_t pvno;
    294  1.1  ahoka 	struct chfs_vnode_cache* next;
    295  1.1  ahoka 	uint32_t nlink;
    296  1.1  ahoka };
    297  1.1  ahoka 
    298  1.1  ahoka struct chfs_eraseblock
    299  1.1  ahoka {
    300  1.1  ahoka 	uint32_t lnr;
    301  1.1  ahoka 
    302  1.1  ahoka 	TAILQ_ENTRY(chfs_eraseblock) queue;
    303  1.1  ahoka 	//uint32_t bad_count;
    304  1.1  ahoka 	uint32_t unchecked_size;
    305  1.1  ahoka 	uint32_t used_size;
    306  1.1  ahoka 	uint32_t dirty_size;
    307  1.1  ahoka 	uint32_t free_size;
    308  1.1  ahoka 	uint32_t wasted_size;
    309  1.1  ahoka 
    310  1.1  ahoka 	struct chfs_node_ref *first_node;
    311  1.1  ahoka 	struct chfs_node_ref *last_node;
    312  1.1  ahoka 
    313  1.1  ahoka 	struct chfs_node_ref *gc_node;     /* Next block to be garbage collected */
    314  1.1  ahoka };
    315  1.1  ahoka 
    316  1.1  ahoka TAILQ_HEAD(chfs_eraseblock_queue, chfs_eraseblock);
    317  1.1  ahoka 
    318  1.1  ahoka #define ALLOC_NORMAL    0
    319  1.1  ahoka #define ALLOC_DELETION	1
    320  1.1  ahoka #define ALLOC_GC        2
    321  1.1  ahoka 
    322  1.1  ahoka struct garbage_collector_thread {
    323  1.1  ahoka 	lwp_t *gcth_thread;
    324  1.1  ahoka 	kcondvar_t gcth_wakeup;
    325  1.1  ahoka 	bool gcth_running;
    326  1.1  ahoka };
    327  1.1  ahoka 
    328  1.1  ahoka #define CHFS_MP_FLAG_SCANNING 2
    329  1.1  ahoka #define CHFS_MP_FLAG_BUILDING 4
    330  1.1  ahoka 
    331  1.1  ahoka /**
    332  1.1  ahoka  * struct chfs_mount - CHFS main descriptor structure
    333  1.1  ahoka  * @ebh: eraseblock handler
    334  1.1  ahoka  * @fl_index: index of flash device in the flash layer
    335  1.1  ahoka  * @fs_version: filesystem version descriptor
    336  1.1  ahoka  * @gbl_version: global version number
    337  1.1  ahoka  * @max_vno: max vnode id
    338  1.1  ahoka  * @chm_lock_mountfields:
    339  1.1  ahoka  * @vnocache_hash: hash table of vnode caches
    340  1.1  ahoka  * @vnocache_lock:
    341  1.1  ahoka  * @blocks: array of eraseblocks on flash
    342  1.1  ahoka  * @chm_root: used to protect all fields
    343  1.1  ahoka  * @free_size: free size on the flash
    344  1.1  ahoka  * @dirty_size: dirtied size on flash
    345  1.1  ahoka  * @unchecked_size: size of unchecked data on flash
    346  1.1  ahoka  * @free_queue: queue of free eraseblocks
    347  1.1  ahoka  * @clean_queue: queue of clean eraseblocks
    348  1.1  ahoka  * @dirty_queue: queue of dirty eraseblocks
    349  1.1  ahoka  * @very_dirty_queue: queue of very dirty eraseblocks
    350  1.1  ahoka  * @erase_pending_queue: queue of eraseblocks waiting for erasing
    351  1.1  ahoka  * @erasable_pending_wbuf_queue: queue of eraseblocks waiting for erasing and
    352  1.1  ahoka  * 								 have data to write to them
    353  1.1  ahoka  * @nextblock: next eraseblock to write to
    354  1.1  ahoka  *
    355  1.1  ahoka  * @nr_free_blocks: number of free blocks on the free_queue
    356  1.1  ahoka  * @nr_erasable_blocks: number of blocks that can be erased and are on the
    357  1.1  ahoka  * 						erasable_queue
    358  1.1  ahoka  *
    359  1.1  ahoka  */
    360  1.1  ahoka struct chfs_mount {
    361  1.1  ahoka 	struct mount *chm_fsmp;
    362  1.1  ahoka //	dev_t dev;
    363  1.1  ahoka //	struct vnode *devvp;
    364  1.1  ahoka 
    365  1.1  ahoka 	struct chfs_ebh *chm_ebh;
    366  1.1  ahoka //	int chm_fl_index;
    367  1.1  ahoka 	int chm_fs_version;
    368  1.1  ahoka 	uint64_t chm_gbl_version;
    369  1.1  ahoka 	ino_t chm_max_vno;
    370  1.1  ahoka 	ino_t chm_checked_vno;
    371  1.1  ahoka 	unsigned int chm_flags;
    372  1.1  ahoka 
    373  1.1  ahoka 	/* chm_lock_mountfields:
    374  1.1  ahoka 	 * Used to protect all the following fields. */
    375  1.1  ahoka 	kmutex_t chm_lock_mountfields;
    376  1.1  ahoka 
    377  1.1  ahoka 	struct chfs_vnode_cache **chm_vnocache_hash;
    378  1.1  ahoka 	/* chm_lock_vnocache:
    379  1.1  ahoka 	 * Used to protect the vnode cache.
    380  1.1  ahoka 	 * If you have to lock chm_lock_mountfields and also chm_lock_vnocache,
    381  1.1  ahoka 	 * you must lock chm_lock_mountfields first. */
    382  1.1  ahoka 	kmutex_t chm_lock_vnocache;
    383  1.1  ahoka 
    384  1.1  ahoka 	struct chfs_eraseblock *chm_blocks;
    385  1.1  ahoka 
    386  1.1  ahoka 	struct chfs_node *chm_root;
    387  1.1  ahoka 
    388  1.1  ahoka 	uint32_t chm_free_size;
    389  1.1  ahoka 	uint32_t chm_dirty_size;
    390  1.1  ahoka 	uint32_t chm_unchecked_size;
    391  1.1  ahoka 	uint32_t chm_used_size;
    392  1.1  ahoka 	uint32_t chm_wasted_size;
    393  1.1  ahoka 	/* chm_lock_sizes:
    394  1.1  ahoka 	 * Used to protect the (free, used, etc.) sizes of the FS
    395  1.1  ahoka 	 * (and also the sizes of each eraseblock).
    396  1.1  ahoka 	 * If you have to lock chm_lock_mountfields and also chm_lock_sizes,
    397  1.1  ahoka 	 * you must lock chm_lock_mountfields first. */
    398  1.1  ahoka 	kmutex_t chm_lock_sizes;
    399  1.1  ahoka 
    400  1.1  ahoka 	struct chfs_eraseblock_queue chm_free_queue;
    401  1.1  ahoka 	struct chfs_eraseblock_queue chm_clean_queue;
    402  1.1  ahoka 	struct chfs_eraseblock_queue chm_dirty_queue;
    403  1.1  ahoka 	struct chfs_eraseblock_queue chm_very_dirty_queue;
    404  1.1  ahoka 	struct chfs_eraseblock_queue chm_erasable_pending_wbuf_queue;
    405  1.1  ahoka 	struct chfs_eraseblock_queue chm_erase_pending_queue;
    406  1.1  ahoka 
    407  1.1  ahoka 	uint8_t chm_resv_blocks_deletion;
    408  1.1  ahoka 	uint8_t chm_resv_blocks_write;
    409  1.1  ahoka 	uint8_t chm_resv_blocks_gctrigger;
    410  1.1  ahoka 	uint8_t chm_resv_blocks_gcmerge;
    411  1.1  ahoka 	uint8_t chm_nospc_dirty;
    412  1.1  ahoka 
    413  1.1  ahoka 	uint8_t chm_vdirty_blocks_gctrigger;
    414  1.1  ahoka 
    415  1.1  ahoka 	struct chfs_eraseblock *chm_nextblock;
    416  1.1  ahoka 
    417  1.1  ahoka 	struct garbage_collector_thread chm_gc_thread;
    418  1.1  ahoka 	struct chfs_eraseblock *chm_gcblock;
    419  1.1  ahoka 
    420  1.1  ahoka 	int chm_nr_free_blocks;
    421  1.1  ahoka 	int chm_nr_erasable_blocks;
    422  1.1  ahoka 
    423  1.1  ahoka 	int32_t chm_fs_bmask;
    424  1.1  ahoka 	int32_t chm_fs_bsize;
    425  1.1  ahoka 	int32_t chm_fs_qbmask;
    426  1.1  ahoka 	int32_t chm_fs_bshift;
    427  1.1  ahoka 	int32_t chm_fs_fmask;
    428  1.1  ahoka 	int64_t chm_fs_qfmask;
    429  1.1  ahoka 
    430  1.1  ahoka 	/* TODO will we use these? */
    431  1.1  ahoka 	unsigned int		chm_pages_max;
    432  1.1  ahoka 	unsigned int		chm_pages_used;
    433  1.1  ahoka 	unsigned int		chm_nodes_max;
    434  1.1  ahoka 	unsigned int		chm_nodes_cnt;
    435  1.1  ahoka 	struct chfs_pool	chm_dirent_pool;
    436  1.1  ahoka 	struct chfs_pool	chm_node_pool;
    437  1.1  ahoka 	struct chfs_str_pool	chm_str_pool;
    438  1.1  ahoka 	/**/
    439  1.1  ahoka 
    440  1.1  ahoka 	size_t chm_wbuf_pagesize;
    441  1.1  ahoka 	unsigned char* chm_wbuf;
    442  1.1  ahoka 	size_t chm_wbuf_ofs;
    443  1.1  ahoka 	size_t chm_wbuf_len;
    444  1.1  ahoka 	/* chm_lock_wbuf:
    445  1.1  ahoka 	 * Used to protect the write buffer.
    446  1.1  ahoka 	 * If you have to lock chm_lock_mountfields and also chm_lock_wbuf,
    447  1.1  ahoka 	 * you must lock chm_lock_mountfields first. */
    448  1.1  ahoka 	krwlock_t chm_lock_wbuf;
    449  1.1  ahoka };
    450  1.1  ahoka 
    451  1.1  ahoka #define sleep_on_spinunlock(s)						      \
    452  1.1  ahoka 	do {								      \
    453  1.1  ahoka 		kmutex_t sleep_mtx;					      \
    454  1.1  ahoka 		kcondvar_t sleep_cnd;					      \
    455  1.1  ahoka 		cv_init(&sleep_cnd, "sleep_cnd");			      \
    456  1.1  ahoka 		mutex_init(&sleep_mtx, MUTEX_DEFAULT, IPL_NONE);	      \
    457  1.1  ahoka 		mutex_spin_exit(s);					      \
    458  1.1  ahoka 		mutex_enter(&sleep_mtx);				      \
    459  1.1  ahoka 		cv_timedwait(&sleep_cnd, &sleep_mtx, mstohz(50));	      \
    460  1.1  ahoka 		mutex_exit(&sleep_mtx);					      \
    461  1.1  ahoka 		mutex_destroy(&sleep_mtx);				      \
    462  1.1  ahoka 		cv_destroy(&sleep_cnd);					      \
    463  1.1  ahoka 	} while (0)
    464  1.1  ahoka #undef sleep_on_spinunlock
    465  1.1  ahoka 
    466  1.1  ahoka /*
    467  1.1  ahoka  * TODO we should move here all of these from the bottom of the file
    468  1.1  ahoka  * Macros/functions to convert from generic data structures to chfs
    469  1.1  ahoka  * specific ones.
    470  1.1  ahoka  */
    471  1.1  ahoka 
    472  1.1  ahoka #define	CHFS_OFFSET_DOT	0
    473  1.1  ahoka #define	CHFS_OFFSET_DOTDOT	1
    474  1.1  ahoka #define CHFS_OFFSET_EOF	2
    475  1.1  ahoka #define CHFS_OFFSET_FIRST	3
    476  1.1  ahoka 
    477  1.1  ahoka 
    478  1.1  ahoka /*---------------------------------------------------------------------------*/
    479  1.1  ahoka 
    480  1.1  ahoka /* chfs_build.c */
    481  1.1  ahoka void chfs_calc_trigger_levels(struct chfs_mount *);
    482  1.1  ahoka int chfs_build_filesystem(struct chfs_mount *);
    483  1.1  ahoka void chfs_build_set_vnodecache_nlink(struct chfs_mount *chmp,struct chfs_vnode_cache *vc);
    484  1.1  ahoka void chfs_build_remove_unlinked_vnode(struct chfs_mount *chmp,struct chfs_vnode_cache *vc, struct chfs_dirent_list *unlinked);
    485  1.1  ahoka 
    486  1.1  ahoka /* chfs_scan.c */
    487  1.1  ahoka int chfs_scan_eraseblock(struct chfs_mount *, struct chfs_eraseblock *);
    488  1.1  ahoka struct chfs_vnode_cache *chfs_scan_make_vnode_cache(struct chfs_mount *chmp, ino_t vno);
    489  1.1  ahoka int chfs_scan_check_node_hdr(struct chfs_flash_node_hdr *nhdr);
    490  1.1  ahoka int chfs_scan_check_vnode(struct chfs_mount *chmp, struct chfs_eraseblock *cheb, void *buf, off_t ofs);
    491  1.1  ahoka int chfs_scan_mark_dirent_obsolete(struct chfs_mount *chmp,struct chfs_vnode_cache *vc, struct chfs_dirent *fd);
    492  1.1  ahoka void chfs_add_fd_to_list(struct chfs_mount *chmp,struct chfs_dirent *new, struct chfs_vnode_cache *pvc);
    493  1.1  ahoka int chfs_scan_check_dirent_node(struct chfs_mount *chmp, struct chfs_eraseblock *cheb, void *buf, off_t ofs);
    494  1.1  ahoka int chfs_scan_check_data_node(struct chfs_mount *chmp, struct chfs_eraseblock *cheb, void *buf, off_t ofs);
    495  1.1  ahoka int chfs_scan_classify_cheb(struct chfs_mount *chmp,struct chfs_eraseblock *cheb);
    496  1.1  ahoka /* chfs_nodeops.c */
    497  1.1  ahoka int chfs_update_eb_dirty(struct chfs_mount *, struct chfs_eraseblock *, uint32_t);
    498  1.1  ahoka void chfs_add_node_to_list(struct chfs_mount *, struct chfs_vnode_cache *,
    499  1.1  ahoka     struct chfs_node_ref *, struct chfs_node_ref **);
    500  1.1  ahoka void chfs_add_fd_to_inode(struct chfs_mount *,
    501  1.1  ahoka     struct chfs_inode *, struct chfs_dirent *);
    502  1.1  ahoka void chfs_add_vnode_ref_to_vc(struct chfs_mount *, struct chfs_vnode_cache *,
    503  1.1  ahoka     struct chfs_node_ref *);
    504  1.1  ahoka struct chfs_node_ref* chfs_nref_next(struct chfs_node_ref *);
    505  1.1  ahoka int chfs_nref_len(struct chfs_mount *,
    506  1.1  ahoka     struct chfs_eraseblock *, struct chfs_node_ref *);
    507  1.1  ahoka int chfs_close_eraseblock(struct chfs_mount *,
    508  1.1  ahoka     struct chfs_eraseblock *);
    509  1.1  ahoka int chfs_reserve_space_normal(struct chfs_mount *, uint32_t, int);
    510  1.1  ahoka int chfs_reserve_space_gc(struct chfs_mount *, uint32_t);
    511  1.1  ahoka int chfs_reserve_space(struct chfs_mount *, uint32_t);
    512  1.1  ahoka void chfs_mark_node_obsolete(struct chfs_mount *, struct chfs_node_ref *);
    513  1.1  ahoka 
    514  1.1  ahoka static inline struct chfs_vnode_cache *
    515  1.1  ahoka chfs_nref_to_vc(struct chfs_node_ref *nref)
    516  1.1  ahoka {
    517  1.1  ahoka 	while (nref->nref_next) {
    518  1.1  ahoka 		nref = nref->nref_next;
    519  1.1  ahoka 		//dbg("lnr: %u, ofs: %u\n", nref->nref_lnr, nref->nref_offset);
    520  1.1  ahoka 		//dbg("vno: %llu\n", ((struct chfs_vnode_cache *)(nref))->vno);
    521  1.1  ahoka 		//dbg("scan_dirents: %p\n", ((struct chfs_vnode_cache *)(nref))->scan_dirents);
    522  1.1  ahoka 		if (nref->nref_lnr == REF_LINK_TO_NEXT) {
    523  1.1  ahoka 			dbg("Link to next!\n");
    524  1.1  ahoka 		} else if (nref->nref_lnr == REF_EMPTY_NODE) {
    525  1.1  ahoka 			dbg("Empty!\n");
    526  1.1  ahoka 		}
    527  1.1  ahoka 	}
    528  1.1  ahoka 	//dbg("vno: %llu\n", ((struct chfs_vnode_cache *)(nref))->vno);
    529  1.1  ahoka 
    530  1.1  ahoka 	//dbg("NREF_TO_VC: GET IT\n");
    531  1.1  ahoka 	//dbg("nref_next: %p, lnr: %u, ofs: %u\n", nref->nref_next, nref->nref_lnr, nref->nref_offset);
    532  1.1  ahoka 	struct chfs_vnode_cache *vc = (struct chfs_vnode_cache *) nref;
    533  1.2  ahoka 	dbg("vno: %ju, pvno: %ju, hv: %ju, nlink: %u\n", (intmax_t )vc->vno,
    534  1.2  ahoka 	    (intmax_t )vc->pvno, (intmax_t )vc->highest_version, vc->nlink);
    535  1.1  ahoka 	//return ((struct chfs_vnode_cache *)nref);
    536  1.1  ahoka 	return vc;
    537  1.1  ahoka }
    538  1.1  ahoka 
    539  1.1  ahoka 
    540  1.1  ahoka /* chfs_malloc.c */
    541  1.1  ahoka int chfs_alloc_pool_caches(void);
    542  1.1  ahoka void chfs_destroy_pool_caches(void);
    543  1.1  ahoka struct chfs_vnode_cache* chfs_vnode_cache_alloc(ino_t);
    544  1.1  ahoka void chfs_vnode_cache_free(struct chfs_vnode_cache *);
    545  1.1  ahoka struct chfs_node_ref* chfs_alloc_node_ref(
    546  1.1  ahoka 	struct chfs_eraseblock *);
    547  1.1  ahoka void chfs_free_node_refs(struct chfs_eraseblock *cheb);
    548  1.1  ahoka struct chfs_dirent* chfs_alloc_dirent(int);
    549  1.1  ahoka void chfs_free_dirent(struct chfs_dirent *);
    550  1.1  ahoka struct chfs_flash_vnode* chfs_alloc_flash_vnode(void);
    551  1.1  ahoka void chfs_free_flash_vnode(struct chfs_flash_vnode *);
    552  1.1  ahoka struct chfs_flash_dirent_node* chfs_alloc_flash_dirent(void);
    553  1.1  ahoka void chfs_free_flash_dirent(struct chfs_flash_dirent_node *);
    554  1.1  ahoka struct chfs_flash_data_node* chfs_alloc_flash_dnode(void);
    555  1.1  ahoka void chfs_free_flash_dnode(struct chfs_flash_data_node *);
    556  1.1  ahoka struct chfs_node_frag* chfs_alloc_node_frag(void);
    557  1.1  ahoka void chfs_free_node_frag(struct chfs_node_frag *);
    558  1.1  ahoka struct chfs_node_ref* chfs_alloc_refblock(void);
    559  1.1  ahoka void chfs_free_refblock(struct chfs_node_ref *nref);
    560  1.1  ahoka struct chfs_full_dnode* chfs_alloc_full_dnode(void);
    561  1.1  ahoka void chfs_free_full_dnode(struct chfs_full_dnode *fd);
    562  1.1  ahoka struct chfs_tmp_dnode * chfs_alloc_tmp_dnode(void);
    563  1.1  ahoka void chfs_free_tmp_dnode(struct chfs_tmp_dnode *);
    564  1.1  ahoka struct chfs_tmp_dnode_info * chfs_alloc_tmp_dnode_info(void);
    565  1.1  ahoka void chfs_free_tmp_dnode_info(struct chfs_tmp_dnode_info *);
    566  1.1  ahoka 
    567  1.1  ahoka /* chfs_readinode.c */
    568  1.1  ahoka int chfs_read_inode(struct chfs_mount *, struct chfs_inode *);
    569  1.1  ahoka int chfs_read_inode_internal(struct chfs_mount *, struct chfs_inode *);
    570  1.1  ahoka void chfs_kill_fragtree(struct rb_tree *);
    571  1.1  ahoka uint32_t chfs_truncate_fragtree(struct chfs_mount *,
    572  1.1  ahoka 	struct rb_tree *, uint32_t);
    573  1.1  ahoka int chfs_add_full_dnode_to_inode(struct chfs_mount *,
    574  1.1  ahoka     struct chfs_inode *,
    575  1.1  ahoka     struct chfs_full_dnode *);
    576  1.1  ahoka int chfs_read_data(struct chfs_mount*, struct vnode *,
    577  1.1  ahoka     struct buf *);
    578  1.1  ahoka 
    579  1.1  ahoka /* chfs_erase.c */
    580  1.1  ahoka int chfs_remap_leb(struct chfs_mount *chmp);
    581  1.1  ahoka 
    582  1.1  ahoka /* chfs_ihash.c */
    583  1.1  ahoka void chfs_ihashinit(void);
    584  1.1  ahoka void chfs_ihashreinit(void);
    585  1.1  ahoka void chfs_ihashdone(void);
    586  1.1  ahoka struct vnode *chfs_ihashlookup(dev_t, ino_t);
    587  1.1  ahoka struct vnode *chfs_ihashget(dev_t, ino_t, int);
    588  1.1  ahoka void chfs_ihashins(struct chfs_inode *);
    589  1.1  ahoka void chfs_ihashrem(struct chfs_inode *);
    590  1.1  ahoka 
    591  1.1  ahoka extern kmutex_t	chfs_ihash_lock;
    592  1.1  ahoka extern kmutex_t	chfs_hashlock;
    593  1.1  ahoka 
    594  1.1  ahoka /* chfs_gc.c */
    595  1.1  ahoka void chfs_gc_trigger(struct chfs_mount *);
    596  1.1  ahoka int chfs_gc_thread_should_wake(struct chfs_mount *);
    597  1.1  ahoka void chfs_gc_thread(void *);
    598  1.1  ahoka void chfs_gc_thread_start(struct chfs_mount *);
    599  1.1  ahoka void chfs_gc_thread_stop(struct chfs_mount *);
    600  1.1  ahoka int chfs_gcollect_pass(struct chfs_mount *);
    601  1.1  ahoka 
    602  1.1  ahoka /* chfs_vfsops.c*/
    603  1.1  ahoka int chfs_gop_alloc(struct vnode *vp, off_t off, off_t len,  int flags,kauth_cred_t cred);
    604  1.1  ahoka int chfs_mountfs(struct vnode *devvp, struct mount *mp);
    605  1.1  ahoka 
    606  1.1  ahoka /* chfs_vnops.c */
    607  1.1  ahoka extern int (**chfs_vnodeop_p)(void *);
    608  1.1  ahoka extern int (**chfs_specop_p)(void *);
    609  1.1  ahoka extern int (**chfs_fifoop_p)(void *);
    610  1.1  ahoka int chfs_lookup(void *);
    611  1.1  ahoka int chfs_create(void *);
    612  1.1  ahoka int chfs_mknod(void *);
    613  1.1  ahoka int chfs_open(void *);
    614  1.1  ahoka int chfs_close(void *);
    615  1.1  ahoka int chfs_access(void *);
    616  1.1  ahoka int chfs_getattr(void *);
    617  1.1  ahoka int chfs_setattr(void *);
    618  1.1  ahoka int chfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t);
    619  1.1  ahoka int chfs_chmod(struct vnode *, int, kauth_cred_t);
    620  1.1  ahoka int chfs_read(void *);
    621  1.1  ahoka int chfs_write(void *);
    622  1.1  ahoka int chfs_fsync(void *);
    623  1.1  ahoka int chfs_remove(void *);
    624  1.1  ahoka int chfs_link(void *);
    625  1.1  ahoka int chfs_rename(void *);
    626  1.1  ahoka int chfs_mkdir(void *);
    627  1.1  ahoka int chfs_rmdir(void *);
    628  1.1  ahoka int chfs_symlink(void *);
    629  1.1  ahoka int chfs_readdir(void *);
    630  1.1  ahoka int chfs_readlink(void *);
    631  1.1  ahoka int chfs_inactive(void *);
    632  1.1  ahoka int chfs_reclaim(void *);
    633  1.1  ahoka int chfs_advlock(void *);
    634  1.1  ahoka int chfs_strategy(void *);
    635  1.1  ahoka int chfs_bmap(void *);
    636  1.1  ahoka 
    637  1.1  ahoka /* chfs_vnode.c */
    638  1.1  ahoka struct vnode *chfs_vnode_lookup(struct chfs_mount *, ino_t);
    639  1.1  ahoka int chfs_readvnode(struct mount *, ino_t, struct vnode **);
    640  1.1  ahoka int chfs_readdirent(struct mount *, struct chfs_node_ref *,
    641  1.1  ahoka     struct chfs_inode *);
    642  1.1  ahoka int chfs_makeinode(int, struct vnode *, struct vnode **,
    643  1.1  ahoka     struct componentname *, int );
    644  1.1  ahoka void chfs_set_vnode_size(struct vnode *, size_t);
    645  1.1  ahoka void chfs_change_size_free(struct chfs_mount *,
    646  1.1  ahoka 	struct chfs_eraseblock *, int);
    647  1.1  ahoka void chfs_change_size_dirty(struct chfs_mount *,
    648  1.1  ahoka 	struct chfs_eraseblock *, int);
    649  1.1  ahoka void chfs_change_size_unchecked(struct chfs_mount *,
    650  1.1  ahoka 	struct chfs_eraseblock *, int);
    651  1.1  ahoka void chfs_change_size_used(struct chfs_mount *,
    652  1.1  ahoka 	struct chfs_eraseblock *, int);
    653  1.1  ahoka void chfs_change_size_wasted(struct chfs_mount *,
    654  1.1  ahoka 	struct chfs_eraseblock *, int);
    655  1.1  ahoka 
    656  1.1  ahoka /* chfs_vnode_cache.c */
    657  1.1  ahoka struct chfs_vnode_cache **chfs_vnocache_hash_init(void);
    658  1.1  ahoka void chfs_vnocache_hash_destroy(struct chfs_vnode_cache **);
    659  1.1  ahoka void chfs_vnode_cache_set_state(struct chfs_mount *,
    660  1.1  ahoka     struct chfs_vnode_cache *, int);
    661  1.1  ahoka struct chfs_vnode_cache* chfs_vnode_cache_get(
    662  1.1  ahoka 	struct chfs_mount *, ino_t);
    663  1.1  ahoka void chfs_vnode_cache_add(struct chfs_mount *,
    664  1.1  ahoka     struct chfs_vnode_cache *);
    665  1.1  ahoka void chfs_vnode_cache_remove(struct chfs_mount *,
    666  1.1  ahoka     struct chfs_vnode_cache *);
    667  1.1  ahoka 
    668  1.1  ahoka /* chfs_wbuf.c */
    669  1.1  ahoka int chfs_write_wbuf(struct chfs_mount*, const struct iovec *, long,
    670  1.1  ahoka     off_t, size_t *);
    671  1.1  ahoka int chfs_flush_pending_wbuf(struct chfs_mount *);
    672  1.1  ahoka 
    673  1.1  ahoka /* chfs_write.c */
    674  1.1  ahoka int chfs_write_flash_vnode(struct chfs_mount *, struct chfs_inode *, int);
    675  1.1  ahoka int chfs_write_flash_dirent(struct chfs_mount *, struct chfs_inode *,
    676  1.1  ahoka     struct chfs_inode *, struct chfs_dirent *, ino_t, int);
    677  1.1  ahoka int chfs_write_flash_dnode(struct chfs_mount *, struct vnode *,
    678  1.1  ahoka     struct buf *, struct chfs_full_dnode *);
    679  1.1  ahoka int chfs_do_link(struct chfs_inode *, struct chfs_inode *, const char *, int, enum vtype);
    680  1.1  ahoka int chfs_do_unlink(struct chfs_inode *, struct chfs_inode *, const char *, int);
    681  1.1  ahoka 
    682  1.1  ahoka /* chfs_subr.c */
    683  1.1  ahoka size_t chfs_mem_info(bool);
    684  1.1  ahoka struct chfs_dirent * chfs_dir_lookup(struct chfs_inode *,
    685  1.1  ahoka     struct componentname *);
    686  1.1  ahoka int chfs_filldir (struct uio *, ino_t, const char *, int, enum vtype);
    687  1.1  ahoka int chfs_chsize(struct vnode *, u_quad_t, kauth_cred_t);
    688  1.1  ahoka int chfs_chflags(struct vnode *, int, kauth_cred_t);
    689  1.1  ahoka void chfs_itimes(struct chfs_inode *, const struct timespec *,
    690  1.1  ahoka     const struct timespec *, const struct timespec *);
    691  1.1  ahoka int	chfs_update(struct vnode *, const struct timespec *,
    692  1.1  ahoka     const struct timespec *, int);
    693  1.1  ahoka //int	chfs_truncate(struct vnode *, off_t);
    694  1.1  ahoka 
    695  1.1  ahoka /*---------------------------------------------------------------------------*/
    696  1.1  ahoka 
    697  1.1  ahoka /* Some inline functions temporarily placed here */
    698  1.1  ahoka static inline int
    699  1.1  ahoka chfs_map_leb(struct chfs_mount *chmp, int lnr)
    700  1.1  ahoka {
    701  1.1  ahoka 	int err;
    702  1.1  ahoka 
    703  1.1  ahoka 	err = ebh_map_leb(chmp->chm_ebh, lnr);
    704  1.1  ahoka 	if (err)
    705  1.1  ahoka 		chfs_err("unmap leb %d failed, error: %d\n",lnr, err);
    706  1.1  ahoka 
    707  1.1  ahoka 	return err;
    708  1.1  ahoka 
    709  1.1  ahoka }
    710  1.1  ahoka 
    711  1.1  ahoka static inline int
    712  1.1  ahoka chfs_unmap_leb(struct chfs_mount *chmp, int lnr)
    713  1.1  ahoka {
    714  1.1  ahoka 	int err;
    715  1.1  ahoka 
    716  1.1  ahoka 	err = ebh_unmap_leb(chmp->chm_ebh, lnr);
    717  1.1  ahoka 	if (err)
    718  1.1  ahoka 		chfs_err("unmap leb %d failed, error: %d\n",lnr, err);
    719  1.1  ahoka 
    720  1.1  ahoka 	return err;
    721  1.1  ahoka }
    722  1.1  ahoka 
    723  1.1  ahoka static inline int
    724  1.1  ahoka chfs_read_leb(struct chfs_mount *chmp, int lnr, char *buf,
    725  1.1  ahoka     int offset, int len, size_t *retlen)
    726  1.1  ahoka {
    727  1.1  ahoka 	int err;
    728  1.1  ahoka 
    729  1.1  ahoka 	err = ebh_read_leb(chmp->chm_ebh, lnr, buf, offset, len, retlen);
    730  1.1  ahoka 	if (err)
    731  1.1  ahoka 		chfs_err("read leb %d:%d failed, error: %d\n",lnr, offset, err);
    732  1.1  ahoka 
    733  1.1  ahoka 	return err;
    734  1.1  ahoka }
    735  1.1  ahoka 
    736  1.1  ahoka static inline int chfs_write_leb(struct chfs_mount *chmp, int lnr, char *buf,
    737  1.1  ahoka     int offset, int len, size_t *retlen)
    738  1.1  ahoka {
    739  1.1  ahoka 	int err;
    740  1.1  ahoka 	err = ebh_write_leb(chmp->chm_ebh, lnr, buf, offset, len, retlen);
    741  1.1  ahoka 	if (err)
    742  1.1  ahoka 		chfs_err("write leb %d:%d failed, error: %d\n",lnr, offset, err);
    743  1.1  ahoka 
    744  1.1  ahoka 	return err;
    745  1.1  ahoka }
    746  1.1  ahoka 
    747  1.1  ahoka /******************************************************************************/
    748  1.1  ahoka /* Code from dummyfs.h														  */
    749  1.1  ahoka /******************************************************************************/
    750  1.1  ahoka /* --------------------------------------------------------------------- */
    751  1.1  ahoka 
    752  1.1  ahoka #define CHFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    753  1.1  ahoka 
    754  1.1  ahoka static __inline size_t
    755  1.1  ahoka CHFS_PAGES_MAX(struct chfs_mount *chmp)
    756  1.1  ahoka {
    757  1.1  ahoka 	size_t freepages;
    758  1.1  ahoka 
    759  1.1  ahoka 	freepages = chfs_mem_info(false);
    760  1.1  ahoka 	if (freepages < CHFS_PAGES_RESERVED)
    761  1.1  ahoka 		freepages = 0;
    762  1.1  ahoka 	else
    763  1.1  ahoka 		freepages -= CHFS_PAGES_RESERVED;
    764  1.1  ahoka 
    765  1.1  ahoka 	return MIN(chmp->chm_pages_max, freepages + chmp->chm_pages_used);
    766  1.1  ahoka }
    767  1.1  ahoka 
    768  1.1  ahoka #define	CHFS_ITIMES(ip, acc, mod, cre)				      \
    769  1.1  ahoka 	while ((ip)->iflag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
    770  1.1  ahoka 		chfs_itimes(ip, acc, mod, cre)
    771  1.1  ahoka /* used for KASSERTs */
    772  1.1  ahoka 
    773  1.1  ahoka #define IMPLIES(a, b) (!(a) || (b))
    774  1.1  ahoka #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    775  1.1  ahoka 
    776  1.1  ahoka /*
    777  1.1  ahoka   struct chfs_node;
    778  1.1  ahoka */
    779  1.1  ahoka /*
    780  1.1  ahoka  * Internal representation of a dummyfs directory entry.
    781  1.1  ahoka  */
    782  1.1  ahoka /*
    783  1.1  ahoka   struct chfs_dirent {
    784  1.1  ahoka   TAILQ_ENTRY(chfs_dirent)	chd_entries;
    785  1.1  ahoka   uint16_t			chd_namelen;
    786  1.1  ahoka   char *				chd_name;
    787  1.1  ahoka   struct chfs_node *		chd_node;
    788  1.1  ahoka   };
    789  1.1  ahoka 
    790  1.1  ahoka   TAILQ_HEAD(chfs_dir, chfs_dirent);
    791  1.1  ahoka 
    792  1.1  ahoka 
    793  1.1  ahoka */
    794  1.1  ahoka /*
    795  1.1  ahoka  * Internal representation of a dummyfs file system node.
    796  1.1  ahoka  */
    797  1.1  ahoka /*
    798  1.1  ahoka   struct chfs_node {
    799  1.1  ahoka   LIST_ENTRY(chfs_node)	chn_entries;
    800  1.1  ahoka   enum vtype		chn_type;
    801  1.1  ahoka   ino_t			chn_id;
    802  1.1  ahoka   int			chn_status;
    803  1.1  ahoka   #define	CHFS_NODE_ACCESSED	(1 << 1)
    804  1.1  ahoka   #define	CHFS_NODE_MODIFIED	(1 << 2)
    805  1.1  ahoka   #define	CHFS_NODE_CHANGED	(1 << 3)
    806  1.1  ahoka 
    807  1.1  ahoka   off_t			chn_size;
    808  1.1  ahoka 
    809  1.1  ahoka   uid_t			chn_uid;
    810  1.1  ahoka   gid_t			chn_gid;
    811  1.1  ahoka   mode_t			chn_mode;
    812  1.1  ahoka   int			chn_flags;
    813  1.1  ahoka   nlink_t			chn_links;
    814  1.1  ahoka   struct timespec		chn_atime;
    815  1.1  ahoka   struct timespec		chn_mtime;
    816  1.1  ahoka   struct timespec		chn_ctime;
    817  1.1  ahoka   struct timespec		chn_birthtime;
    818  1.1  ahoka   unsigned long		chn_gen;
    819  1.1  ahoka 
    820  1.1  ahoka   struct lockf *		chn_lockf;
    821  1.1  ahoka 
    822  1.1  ahoka   kmutex_t		chn_vlock;
    823  1.1  ahoka   struct vnode *		chn_vnode;
    824  1.1  ahoka 
    825  1.1  ahoka   union {
    826  1.1  ahoka   struct {
    827  1.1  ahoka   dev_t			chn_rdev;
    828  1.1  ahoka   } chn_dev;
    829  1.1  ahoka 
    830  1.1  ahoka   struct {
    831  1.1  ahoka   struct chfs_node *	chn_parent;
    832  1.1  ahoka 
    833  1.1  ahoka   struct chfs_dir	chn_dir;
    834  1.1  ahoka 
    835  1.1  ahoka   off_t			chn_readdir_lastn;
    836  1.1  ahoka   struct chfs_dirent *	chn_readdir_lastp;
    837  1.1  ahoka   } chn_dir;
    838  1.1  ahoka 
    839  1.1  ahoka   struct chn_lnk {
    840  1.1  ahoka   char *			chn_link;
    841  1.1  ahoka   } chn_lnk;
    842  1.1  ahoka 
    843  1.1  ahoka   struct chn_reg {
    844  1.1  ahoka   struct uvm_object *	chn_aobj;
    845  1.1  ahoka   size_t			chn_aobj_pages;
    846  1.1  ahoka   } chn_reg;
    847  1.1  ahoka   } chn_spec;
    848  1.1  ahoka   };
    849  1.1  ahoka */
    850  1.1  ahoka /*
    851  1.1  ahoka   LIST_HEAD(chfs_node_list, chfs_node);
    852  1.1  ahoka 
    853  1.1  ahoka   #define VTOCHN(vp)		((struct chfs_node *)(vp)->v_data)
    854  1.1  ahoka   #define CHNTOV(chnp)	((chnp)->chn_vnode)
    855  1.1  ahoka 
    856  1.1  ahoka */
    857  1.1  ahoka /*
    858  1.1  ahoka  * Ensures that the node pointed by 'node' is a directory and that its
    859  1.1  ahoka  * contents are consistent with respect to directories.
    860  1.1  ahoka  */
    861  1.1  ahoka /*
    862  1.1  ahoka   #ifdef someonefixthisplease
    863  1.1  ahoka   #define CHFS_VALIDATE_DIR(node) \
    864  1.1  ahoka   KASSERT((node)->chn_type == VDIR); \
    865  1.1  ahoka   KASSERT((node)->chn_size % sizeof(struct chfs_dirent) == 0); \
    866  1.1  ahoka   KASSERT((node)->chn_spec.chn_dir.chn_readdir_lastp == NULL || \
    867  1.1  ahoka   chfs_dircookie((node)->chn_spec.chn_dir.chn_readdir_lastp) == \
    868  1.1  ahoka   (node)->chn_spec.chn_dir.chn_readdir_lastn);
    869  1.1  ahoka   #else
    870  1.1  ahoka   #define CHFS_VALIDATE_DIR(node) \
    871  1.1  ahoka   KASSERT((node)->chn_type == VDIR); \
    872  1.1  ahoka   KASSERT((node)->chn_size % sizeof(struct chfs_dirent) == 0);
    873  1.1  ahoka   #endif
    874  1.1  ahoka 
    875  1.1  ahoka */
    876  1.1  ahoka 
    877  1.1  ahoka /* Returns the available space for the given file system. */
    878  1.1  ahoka /*
    879  1.1  ahoka   #define CHFS_PAGES_AVAIL(dmp)		\
    880  1.1  ahoka   ((ssize_t)(CHFS_PAGES_MAX(dmp) - (dmp)->chm_pages_used))
    881  1.1  ahoka 
    882  1.1  ahoka 
    883  1.1  ahoka 
    884  1.1  ahoka   static __inline
    885  1.1  ahoka   struct chfs_node *
    886  1.1  ahoka   VP_TO_CHFS_NODE(struct vnode *vp)
    887  1.1  ahoka   {
    888  1.1  ahoka   struct chfs_node *node;
    889  1.1  ahoka 
    890  1.1  ahoka   #ifdef KASSERT
    891  1.1  ahoka   KASSERT((vp) != NULL && (vp)->v_data != NULL);
    892  1.1  ahoka   #endif
    893  1.1  ahoka   node = (struct chfs_node *)vp->v_data;
    894  1.1  ahoka   return node;
    895  1.1  ahoka   }
    896  1.1  ahoka 
    897  1.1  ahoka 
    898  1.1  ahoka   static __inline
    899  1.1  ahoka   struct chfs_node *
    900  1.1  ahoka   VP_TO_CHFS_DIR(struct vnode *vp)
    901  1.1  ahoka   {
    902  1.1  ahoka   struct chfs_node *node;
    903  1.1  ahoka 
    904  1.1  ahoka   node = VP_TO_CHFS_NODE(vp);
    905  1.1  ahoka   #ifdef KASSERT
    906  1.1  ahoka   CHFS_VALIDATE_DIR(node);
    907  1.1  ahoka   #endif
    908  1.1  ahoka   return node;
    909  1.1  ahoka   }
    910  1.1  ahoka 
    911  1.1  ahoka 
    912  1.1  ahoka */
    913  1.1  ahoka #endif /* __CHFS_H__ */
    914