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