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