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