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