Home | History | Annotate | Line # | Download | only in chfs
chfs_gc.c revision 1.2
      1  1.2    agc /*	$NetBSD: chfs_gc.c,v 1.2 2011/11/24 21:09:37 agc Exp $	*/
      2  1.1  ahoka 
      3  1.1  ahoka /*-
      4  1.1  ahoka  * Copyright (c) 2010 Department of Software Engineering,
      5  1.1  ahoka  *		      University of Szeged, Hungary
      6  1.1  ahoka  * Copyright (c) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
      7  1.1  ahoka  * Copyright (c) 2010 Adam Hoka <ahoka (at) NetBSD.org>
      8  1.1  ahoka  * All rights reserved.
      9  1.1  ahoka  *
     10  1.1  ahoka  * This code is derived from software contributed to The NetBSD Foundation
     11  1.1  ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     12  1.1  ahoka  *
     13  1.1  ahoka  * Redistribution and use in source and binary forms, with or without
     14  1.1  ahoka  * modification, are permitted provided that the following conditions
     15  1.1  ahoka  * are met:
     16  1.1  ahoka  * 1. Redistributions of source code must retain the above copyright
     17  1.1  ahoka  *    notice, this list of conditions and the following disclaimer.
     18  1.1  ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  ahoka  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  ahoka  *    documentation and/or other materials provided with the distribution.
     21  1.1  ahoka  *
     22  1.1  ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1  ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  1.1  ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  1.1  ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  1.1  ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.1  ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  ahoka  * SUCH DAMAGE.
     33  1.1  ahoka  */
     34  1.1  ahoka 
     35  1.1  ahoka #include "chfs.h"
     36  1.1  ahoka 
     37  1.1  ahoka void chfs_gc_release_inode(struct chfs_mount *,
     38  1.1  ahoka     struct chfs_inode *);
     39  1.1  ahoka struct chfs_inode *chfs_gc_fetch_inode(struct chfs_mount *,
     40  1.1  ahoka     ino_t, uint32_t);
     41  1.1  ahoka int chfs_check(struct chfs_mount *, struct chfs_vnode_cache *);
     42  1.1  ahoka void chfs_clear_inode(struct chfs_mount *, struct chfs_inode *);
     43  1.1  ahoka 
     44  1.1  ahoka 
     45  1.1  ahoka struct chfs_eraseblock *find_gc_block(struct chfs_mount *);
     46  1.1  ahoka int chfs_gcollect_pristine(struct chfs_mount *,
     47  1.1  ahoka     struct chfs_eraseblock *,
     48  1.1  ahoka     struct chfs_vnode_cache *, struct chfs_node_ref *);
     49  1.1  ahoka int chfs_gcollect_live(struct chfs_mount *,
     50  1.1  ahoka     struct chfs_eraseblock *, struct chfs_node_ref *,
     51  1.1  ahoka     struct chfs_inode *);
     52  1.1  ahoka int chfs_gcollect_vnode(struct chfs_mount *, struct chfs_inode *);
     53  1.1  ahoka int chfs_gcollect_dirent(struct chfs_mount *,
     54  1.1  ahoka     struct chfs_eraseblock *, struct chfs_inode *,
     55  1.1  ahoka     struct chfs_dirent *);
     56  1.1  ahoka int chfs_gcollect_deletion_dirent(struct chfs_mount *,
     57  1.1  ahoka     struct chfs_eraseblock *, struct chfs_inode *,
     58  1.1  ahoka     struct chfs_dirent *);
     59  1.1  ahoka int chfs_gcollect_dnode(struct chfs_mount *,
     60  1.1  ahoka     struct chfs_eraseblock *, struct chfs_inode *,
     61  1.1  ahoka     struct chfs_full_dnode *, uint32_t, uint32_t);
     62  1.1  ahoka 
     63  1.1  ahoka /* must be called with chm_lock_mountfields held */
     64  1.1  ahoka void
     65  1.1  ahoka chfs_gc_trigger(struct chfs_mount *chmp)
     66  1.1  ahoka {
     67  1.1  ahoka 	struct garbage_collector_thread *gc = &chmp->chm_gc_thread;
     68  1.1  ahoka 
     69  1.1  ahoka 	//mutex_enter(&chmp->chm_lock_sizes);
     70  1.1  ahoka 	if (gc->gcth_running &&
     71  1.1  ahoka 	    chfs_gc_thread_should_wake(chmp)) {
     72  1.1  ahoka 		cv_signal(&gc->gcth_wakeup);
     73  1.1  ahoka 	}
     74  1.1  ahoka 	//mutex_exit(&chmp->chm_lock_sizes);
     75  1.1  ahoka }
     76  1.1  ahoka 
     77  1.1  ahoka 
     78  1.1  ahoka void
     79  1.1  ahoka chfs_gc_thread(void *data)
     80  1.1  ahoka {
     81  1.1  ahoka 	struct chfs_mount *chmp = data;
     82  1.1  ahoka 	struct garbage_collector_thread *gc = &chmp->chm_gc_thread;
     83  1.1  ahoka 
     84  1.1  ahoka 	dbg_gc("[GC THREAD] thread started\n");
     85  1.1  ahoka 
     86  1.1  ahoka 	mutex_enter(&chmp->chm_lock_mountfields);
     87  1.1  ahoka 	while (gc->gcth_running) {
     88  1.1  ahoka 		/* we must call chfs_gc_thread_should_wake with chm_lock_mountfields
     89  1.1  ahoka 		 * held, which is a bit awkwardly done here, but we cant relly
     90  1.1  ahoka 		 * do it otherway with the current design...
     91  1.1  ahoka 		 */
     92  1.1  ahoka 		if (chfs_gc_thread_should_wake(chmp)) {
     93  1.1  ahoka //			mutex_exit(&chmp->chm_lock_mountfields);
     94  1.1  ahoka 			if (chfs_gcollect_pass(chmp) == ENOSPC) {
     95  1.1  ahoka 				dbg_gc("No space for garbage collection\n");
     96  1.1  ahoka 				panic("No space for garbage collection\n");
     97  1.1  ahoka 				/* XXX why break here? i have added a panic
     98  1.1  ahoka 				 * here to see if it gets triggered -ahoka
     99  1.1  ahoka 				 */
    100  1.1  ahoka 				break;
    101  1.1  ahoka 			}
    102  1.1  ahoka 			/* XXX gcollect_pass drops the mutex */
    103  1.1  ahoka 			mutex_enter(&chmp->chm_lock_mountfields);
    104  1.1  ahoka 		}
    105  1.1  ahoka 
    106  1.1  ahoka 		cv_timedwait_sig(&gc->gcth_wakeup,
    107  1.1  ahoka 		    &chmp->chm_lock_mountfields, mstohz(100));
    108  1.1  ahoka 	}
    109  1.1  ahoka 	mutex_exit(&chmp->chm_lock_mountfields);
    110  1.1  ahoka 
    111  1.1  ahoka 	dbg_gc("[GC THREAD] thread stopped\n");
    112  1.1  ahoka 	kthread_exit(0);
    113  1.1  ahoka }
    114  1.1  ahoka 
    115  1.1  ahoka void
    116  1.1  ahoka chfs_gc_thread_start(struct chfs_mount *chmp)
    117  1.1  ahoka {
    118  1.1  ahoka 	struct garbage_collector_thread *gc = &chmp->chm_gc_thread;
    119  1.1  ahoka 
    120  1.1  ahoka 	cv_init(&gc->gcth_wakeup, "chfsgccv");
    121  1.1  ahoka 
    122  1.1  ahoka 	gc->gcth_running = true;
    123  1.1  ahoka 	kthread_create(PRI_NONE, /*KTHREAD_MPSAFE |*/ KTHREAD_MUSTJOIN,
    124  1.1  ahoka 	    NULL, chfs_gc_thread, chmp, &gc->gcth_thread,
    125  1.1  ahoka 	    "chfsgcth");
    126  1.1  ahoka }
    127  1.1  ahoka 
    128  1.1  ahoka void
    129  1.1  ahoka chfs_gc_thread_stop(struct chfs_mount *chmp)
    130  1.1  ahoka {
    131  1.1  ahoka 	struct garbage_collector_thread *gc = &chmp->chm_gc_thread;
    132  1.1  ahoka 
    133  1.1  ahoka 	/* check if it is actually running. if not, do nothing */
    134  1.1  ahoka 	if (gc->gcth_running) {
    135  1.1  ahoka 		gc->gcth_running = false;
    136  1.1  ahoka 	} else {
    137  1.1  ahoka 		return;
    138  1.1  ahoka 	}
    139  1.1  ahoka 	cv_signal(&gc->gcth_wakeup);
    140  1.1  ahoka 	dbg_gc("[GC THREAD] stop signal sent\n");
    141  1.1  ahoka 
    142  1.1  ahoka 	kthread_join(gc->gcth_thread);
    143  1.1  ahoka #ifdef BROKEN_KTH_JOIN
    144  1.1  ahoka 	kpause("chfsthjoin", false, mstohz(1000), NULL);
    145  1.1  ahoka #endif
    146  1.1  ahoka 
    147  1.1  ahoka 	cv_destroy(&gc->gcth_wakeup);
    148  1.1  ahoka }
    149  1.1  ahoka 
    150  1.1  ahoka /* must be called with chm_lock_mountfields held */
    151  1.1  ahoka int
    152  1.1  ahoka chfs_gc_thread_should_wake(struct chfs_mount *chmp)
    153  1.1  ahoka {
    154  1.1  ahoka 	int nr_very_dirty = 0;
    155  1.1  ahoka 	struct chfs_eraseblock *cheb;
    156  1.1  ahoka 	uint32_t dirty;
    157  1.1  ahoka 
    158  1.1  ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    159  1.1  ahoka 
    160  1.1  ahoka 	if (!TAILQ_EMPTY(&chmp->chm_erase_pending_queue)) {
    161  1.1  ahoka 		dbg_gc("erase_pending\n");
    162  1.1  ahoka 		return 1;
    163  1.1  ahoka 	}
    164  1.1  ahoka 
    165  1.1  ahoka 	if (chmp->chm_unchecked_size) {
    166  1.1  ahoka 		dbg_gc("unchecked\n");
    167  1.1  ahoka 		return 1;
    168  1.1  ahoka 	}
    169  1.1  ahoka 
    170  1.1  ahoka 	dirty = chmp->chm_dirty_size - chmp->chm_nr_erasable_blocks *
    171  1.1  ahoka 	    chmp->chm_ebh->eb_size;
    172  1.1  ahoka 
    173  1.1  ahoka 	if (chmp->chm_nr_free_blocks + chmp->chm_nr_erasable_blocks <
    174  1.1  ahoka 	    chmp->chm_resv_blocks_gctrigger && (dirty > chmp->chm_nospc_dirty)) {
    175  1.1  ahoka 		dbg_gc("free: %d + erasable: %d < resv: %d\n",
    176  1.1  ahoka 		    chmp->chm_nr_free_blocks, chmp->chm_nr_erasable_blocks,
    177  1.1  ahoka 		    chmp->chm_resv_blocks_gctrigger);
    178  1.1  ahoka 		dbg_gc("dirty: %d > nospc_dirty: %d\n",
    179  1.1  ahoka 		    dirty, chmp->chm_nospc_dirty);
    180  1.1  ahoka 
    181  1.1  ahoka 		return 1;
    182  1.1  ahoka 	}
    183  1.1  ahoka 
    184  1.1  ahoka 	TAILQ_FOREACH(cheb, &chmp->chm_very_dirty_queue, queue) {
    185  1.1  ahoka 		nr_very_dirty++;
    186  1.1  ahoka 		if (nr_very_dirty == chmp->chm_vdirty_blocks_gctrigger) {
    187  1.1  ahoka 			dbg_gc("nr_very_dirty\n");
    188  1.1  ahoka 			return 1;
    189  1.1  ahoka 		}
    190  1.1  ahoka 	}
    191  1.1  ahoka 
    192  1.1  ahoka 	return 0;
    193  1.1  ahoka }
    194  1.1  ahoka 
    195  1.1  ahoka void
    196  1.1  ahoka chfs_gc_release_inode(struct chfs_mount *chmp,
    197  1.1  ahoka     struct chfs_inode *ip)
    198  1.1  ahoka {
    199  1.1  ahoka 	dbg_gc("release inode\n");
    200  1.1  ahoka 	//mutex_exit(&ip->inode_lock);
    201  1.1  ahoka 	//vput(ITOV(ip));
    202  1.1  ahoka }
    203  1.1  ahoka 
    204  1.1  ahoka struct chfs_inode *
    205  1.1  ahoka chfs_gc_fetch_inode(struct chfs_mount *chmp, ino_t vno,
    206  1.1  ahoka     uint32_t unlinked)
    207  1.1  ahoka {
    208  1.1  ahoka 	struct vnode *vp = NULL;
    209  1.1  ahoka 	struct chfs_vnode_cache *vc;
    210  1.1  ahoka 	struct chfs_inode *ip;
    211  1.2    agc 	dbg_gc("fetch inode %llu\n", (unsigned long long)vno);
    212  1.1  ahoka 
    213  1.1  ahoka 	if (unlinked) {
    214  1.1  ahoka 		dbg_gc("unlinked\n");
    215  1.1  ahoka 		vp = chfs_vnode_lookup(chmp, vno);
    216  1.1  ahoka 		if (!vp) {
    217  1.1  ahoka 			mutex_enter(&chmp->chm_lock_vnocache);
    218  1.1  ahoka 			vc = chfs_vnode_cache_get(chmp, vno);
    219  1.1  ahoka 			if (!vc) {
    220  1.1  ahoka 				mutex_exit(&chmp->chm_lock_vnocache);
    221  1.1  ahoka 				return NULL;
    222  1.1  ahoka 			}
    223  1.1  ahoka 			if (vc->state != VNO_STATE_CHECKEDABSENT) {
    224  1.1  ahoka 				//sleep_on_spinunlock(&chmp->chm_lock_vnocache);
    225  1.1  ahoka 				mutex_exit(&chmp->chm_lock_vnocache);
    226  1.1  ahoka 				/* XXX why do we need the delay here?! */
    227  1.1  ahoka //				kpause("chvncabs", true, mstohz(50), NULL);
    228  1.1  ahoka 				KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    229  1.1  ahoka 				cv_timedwait_sig(
    230  1.1  ahoka 					&chmp->chm_gc_thread.gcth_wakeup,
    231  1.1  ahoka 					&chmp->chm_lock_mountfields, mstohz(50));
    232  1.1  ahoka 
    233  1.1  ahoka //				KASSERT(!mutex_owned(&chmp->chm_lock_vnocache));
    234  1.1  ahoka 			} else {
    235  1.1  ahoka 				mutex_exit(&chmp->chm_lock_vnocache);
    236  1.1  ahoka 			}
    237  1.1  ahoka 			return NULL;
    238  1.1  ahoka 		}
    239  1.1  ahoka 	} else {
    240  1.1  ahoka 		dbg_gc("vnode lookup\n");
    241  1.1  ahoka 		vp = chfs_vnode_lookup(chmp, vno);
    242  1.1  ahoka 		//VFS_VGET(chmp->chm_fsmp, vno, &vp);
    243  1.1  ahoka 	}
    244  1.1  ahoka 	dbg_gc("vp to ip\n");
    245  1.1  ahoka 	ip = VTOI(vp);
    246  1.1  ahoka 	KASSERT(ip);
    247  1.1  ahoka 	//mutex_enter(&ip->inode_lock);
    248  1.1  ahoka 
    249  1.1  ahoka 	return ip;
    250  1.1  ahoka }
    251  1.1  ahoka 
    252  1.1  ahoka extern rb_tree_ops_t frag_rbtree_ops;
    253  1.1  ahoka 
    254  1.1  ahoka int
    255  1.1  ahoka chfs_check(struct chfs_mount *chmp, struct  chfs_vnode_cache *chvc)
    256  1.1  ahoka {
    257  1.1  ahoka 	struct chfs_inode *ip;
    258  1.1  ahoka 	struct vnode *vp;
    259  1.1  ahoka 	int ret;
    260  1.1  ahoka 
    261  1.1  ahoka 	ip = pool_get(&chfs_inode_pool, PR_WAITOK);
    262  1.1  ahoka 	if (!ip) {
    263  1.1  ahoka 		return ENOMEM;
    264  1.1  ahoka 	}
    265  1.1  ahoka 
    266  1.1  ahoka 	vp = kmem_zalloc(sizeof(struct vnode), KM_SLEEP);
    267  1.1  ahoka 
    268  1.1  ahoka 	ip->chvc = chvc;
    269  1.1  ahoka 	ip->vp = vp;
    270  1.1  ahoka 
    271  1.1  ahoka 	vp->v_data = ip;
    272  1.1  ahoka 
    273  1.1  ahoka 	rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
    274  1.1  ahoka 	TAILQ_INIT(&ip->dents);
    275  1.1  ahoka 
    276  1.1  ahoka 	ret = chfs_read_inode_internal(chmp, ip);
    277  1.1  ahoka 	if (!ret) {
    278  1.1  ahoka 		chfs_clear_inode(chmp, ip);
    279  1.1  ahoka 	}
    280  1.1  ahoka 
    281  1.1  ahoka 	pool_put(&chfs_inode_pool, ip);
    282  1.1  ahoka 
    283  1.1  ahoka 	return ret;
    284  1.1  ahoka }
    285  1.1  ahoka 
    286  1.1  ahoka void
    287  1.1  ahoka chfs_clear_inode(struct chfs_mount *chmp, struct chfs_inode *ip)
    288  1.1  ahoka {
    289  1.1  ahoka 	struct chfs_dirent *fd, *tmpfd;
    290  1.1  ahoka 	struct chfs_vnode_cache *chvc;
    291  1.1  ahoka 
    292  1.1  ahoka 
    293  1.1  ahoka 	/* XXX not sure if this is the correct locking */
    294  1.1  ahoka //	mutex_enter(&chmp->chm_lock_vnocache);
    295  1.1  ahoka 	chvc = ip->chvc;
    296  1.1  ahoka 	/* shouldnt this be: */
    297  1.1  ahoka 	//bool deleted = (chvc && !(chvc->pvno || chvc->nlink));
    298  1.1  ahoka 	int deleted = (chvc && !(chvc->pvno | chvc->nlink));
    299  1.1  ahoka 
    300  1.1  ahoka 	if (chvc && chvc->state != VNO_STATE_CHECKING) {
    301  1.1  ahoka //		chfs_vnode_cache_state_set(chmp, chvc, VNO_STATE_CLEARING);
    302  1.1  ahoka 		chvc->state = VNO_STATE_CLEARING;
    303  1.1  ahoka 	}
    304  1.1  ahoka 
    305  1.1  ahoka 	if (chvc->v && ((struct  chfs_vnode_cache *)chvc->v != chvc)) {
    306  1.1  ahoka 		if (deleted)
    307  1.1  ahoka 			chfs_mark_node_obsolete(chmp, chvc->v);
    308  1.1  ahoka 		//chfs_free_refblock(chvc->v);
    309  1.1  ahoka 	}
    310  1.1  ahoka //	mutex_enter(&chmp->chm_lock_vnocache);
    311  1.1  ahoka 
    312  1.1  ahoka 	chfs_kill_fragtree(&ip->fragtree);
    313  1.1  ahoka /*
    314  1.1  ahoka 	fd = TAILQ_FIRST(&ip->dents);
    315  1.1  ahoka 	while (fd) {
    316  1.1  ahoka 		TAILQ_REMOVE(&ip->dents, fd, fds);
    317  1.1  ahoka 		chfs_free_dirent(fd);
    318  1.1  ahoka 		fd = TAILQ_FIRST(&ip->dents);
    319  1.1  ahoka 	}
    320  1.1  ahoka */
    321  1.1  ahoka 
    322  1.1  ahoka 	TAILQ_FOREACH_SAFE(fd, &ip->dents, fds, tmpfd) {
    323  1.1  ahoka 		chfs_free_dirent(fd);
    324  1.1  ahoka 	}
    325  1.1  ahoka 
    326  1.1  ahoka 	if (chvc && chvc->state == VNO_STATE_CHECKING) {
    327  1.1  ahoka 		chfs_vnode_cache_set_state(chmp,
    328  1.1  ahoka 		    chvc, VNO_STATE_CHECKEDABSENT);
    329  1.1  ahoka 		if ((struct chfs_vnode_cache *)chvc->v == chvc &&
    330  1.1  ahoka 		    (struct chfs_vnode_cache *)chvc->dirents == chvc &&
    331  1.1  ahoka 		    (struct chfs_vnode_cache *)chvc->dnode == chvc)
    332  1.1  ahoka 			chfs_vnode_cache_remove(chmp, chvc);
    333  1.1  ahoka 	}
    334  1.1  ahoka 
    335  1.1  ahoka }
    336  1.1  ahoka 
    337  1.1  ahoka struct chfs_eraseblock *
    338  1.1  ahoka find_gc_block(struct chfs_mount *chmp)
    339  1.1  ahoka {
    340  1.1  ahoka 	struct chfs_eraseblock *ret;
    341  1.1  ahoka 	struct chfs_eraseblock_queue *nextqueue;
    342  1.1  ahoka 
    343  1.1  ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    344  1.1  ahoka 
    345  1.1  ahoka 	struct timespec now;
    346  1.1  ahoka 	vfs_timestamp(&now);
    347  1.1  ahoka 
    348  1.1  ahoka 	int n = now.tv_nsec % 128;
    349  1.1  ahoka 
    350  1.1  ahoka 	//dbg_gc("n = %d\n", n);
    351  1.1  ahoka again:
    352  1.1  ahoka /*    if (!TAILQ_EMPTY(&chmp->chm_bad_used_queue) && chmp->chm_nr_free_blocks > chmp->chm_nr_resv_blocks_gcbad) {
    353  1.1  ahoka       dbg_gc("Picking block from bad_used_queue to GC next\n");
    354  1.1  ahoka       nextqueue = &chmp->chm_bad_used_queue;
    355  1.1  ahoka       } else */if (n<50 && !TAILQ_EMPTY(&chmp->chm_erase_pending_queue)) {
    356  1.1  ahoka 		dbg_gc("Picking block from erase_pending_queue to GC next\n");
    357  1.1  ahoka 		nextqueue = &chmp->chm_erase_pending_queue;
    358  1.1  ahoka 	} else if (n<110 && !TAILQ_EMPTY(&chmp->chm_very_dirty_queue) ) {
    359  1.1  ahoka 		dbg_gc("Picking block from very_dirty_queue to GC next\n");
    360  1.1  ahoka 		nextqueue = &chmp->chm_very_dirty_queue;
    361  1.1  ahoka 	} else if (n<126 && !TAILQ_EMPTY(&chmp->chm_dirty_queue) ) {
    362  1.1  ahoka 		dbg_gc("Picking block from dirty_queue to GC next\n");
    363  1.1  ahoka 		nextqueue = &chmp->chm_dirty_queue;
    364  1.1  ahoka 	} else if (!TAILQ_EMPTY(&chmp->chm_clean_queue)) {
    365  1.1  ahoka 		dbg_gc("Picking block from clean_queue to GC next\n");
    366  1.1  ahoka 		nextqueue = &chmp->chm_clean_queue;
    367  1.1  ahoka 	} else if (!TAILQ_EMPTY(&chmp->chm_dirty_queue)) {
    368  1.1  ahoka 		dbg_gc("Picking block from dirty_queue to GC next"
    369  1.1  ahoka 		    " (clean_queue was empty)\n");
    370  1.1  ahoka 		nextqueue = &chmp->chm_dirty_queue;
    371  1.1  ahoka 	} else if (!TAILQ_EMPTY(&chmp->chm_very_dirty_queue)) {
    372  1.1  ahoka 		dbg_gc("Picking block from very_dirty_queue to GC next"
    373  1.1  ahoka 		    " (clean_queue and dirty_queue were empty)\n");
    374  1.1  ahoka 		nextqueue = &chmp->chm_very_dirty_queue;
    375  1.1  ahoka 	} else if (!TAILQ_EMPTY(&chmp->chm_erase_pending_queue)) {
    376  1.1  ahoka 		dbg_gc("Picking block from erase_pending_queue to GC next"
    377  1.1  ahoka 		    " (clean_queue and {very_,}dirty_queue were empty)\n");
    378  1.1  ahoka 		nextqueue = &chmp->chm_erase_pending_queue;
    379  1.1  ahoka 	} else if (!TAILQ_EMPTY(&chmp->chm_erasable_pending_wbuf_queue)) {
    380  1.1  ahoka 		dbg_gc("Synching wbuf in order to reuse "
    381  1.1  ahoka 		    "erasable_pendig_wbuf_queue blocks\n");
    382  1.1  ahoka 		rw_enter(&chmp->chm_lock_wbuf, RW_WRITER);
    383  1.1  ahoka 		chfs_flush_pending_wbuf(chmp);
    384  1.1  ahoka 		rw_exit(&chmp->chm_lock_wbuf);
    385  1.1  ahoka 		goto again;
    386  1.1  ahoka 	} else {
    387  1.1  ahoka 		dbg_gc("CHFS: no clean, dirty _or_ erasable"
    388  1.1  ahoka 		    " blocks to GC from! Where are they all?\n");
    389  1.1  ahoka 		return NULL;
    390  1.1  ahoka 	}
    391  1.1  ahoka 
    392  1.1  ahoka 	ret = TAILQ_FIRST(nextqueue);
    393  1.1  ahoka 	if (chmp->chm_nextblock) {
    394  1.1  ahoka 		dbg_gc("nextblock num: %u - gcblock num: %u\n",
    395  1.1  ahoka 		    chmp->chm_nextblock->lnr, ret->lnr);
    396  1.1  ahoka 		if (ret == chmp->chm_nextblock)
    397  1.1  ahoka 			goto again;
    398  1.1  ahoka 		//KASSERT(ret != chmp->chm_nextblock);
    399  1.1  ahoka 		//dbg_gc("first node lnr: %u ofs: %u\n", ret->first_node->lnr, ret->first_node->offset);
    400  1.1  ahoka 		//dbg_gc("last node lnr: %u ofs: %u\n", ret->last_node->lnr, ret->last_node->offset);
    401  1.1  ahoka 	}
    402  1.1  ahoka 	TAILQ_REMOVE(nextqueue, ret, queue);
    403  1.1  ahoka 	chmp->chm_gcblock = ret;
    404  1.1  ahoka 	ret->gc_node = ret->first_node;
    405  1.1  ahoka 
    406  1.1  ahoka 	if (!ret->gc_node) {
    407  1.1  ahoka 		dbg_gc("Oops! ret->gc_node at LEB: %u is NULL\n", ret->lnr);
    408  1.1  ahoka 		panic("CHFS BUG - one LEB's gc_node is NULL\n");
    409  1.1  ahoka 	}
    410  1.1  ahoka 
    411  1.1  ahoka 	/* TODO wasted size? */
    412  1.1  ahoka 	return ret;
    413  1.1  ahoka }
    414  1.1  ahoka 
    415  1.1  ahoka 
    416  1.1  ahoka int
    417  1.1  ahoka chfs_gcollect_pass(struct chfs_mount *chmp)
    418  1.1  ahoka {
    419  1.1  ahoka 	struct chfs_vnode_cache *vc;
    420  1.1  ahoka 	struct chfs_eraseblock *eb;
    421  1.1  ahoka 	struct chfs_node_ref *nref;
    422  1.1  ahoka 	uint32_t gcblock_dirty;
    423  1.1  ahoka 	struct chfs_inode *ip;
    424  1.1  ahoka 	ino_t vno, pvno;
    425  1.1  ahoka 	uint32_t nlink;
    426  1.1  ahoka 	int ret = 0;
    427  1.1  ahoka 
    428  1.1  ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    429  1.1  ahoka 
    430  1.1  ahoka //	mutex_enter(&chmp->chm_lock_mountfields);
    431  1.1  ahoka 	for (;;) {
    432  1.1  ahoka 		mutex_enter(&chmp->chm_lock_sizes);
    433  1.1  ahoka 
    434  1.1  ahoka 		dbg_gc("unchecked size == %u\n", chmp->chm_unchecked_size);
    435  1.1  ahoka 		if (!chmp->chm_unchecked_size)
    436  1.1  ahoka 			break;
    437  1.1  ahoka 
    438  1.1  ahoka 		if (chmp->chm_checked_vno > chmp->chm_max_vno) {
    439  1.1  ahoka 			mutex_exit(&chmp->chm_lock_sizes);
    440  1.1  ahoka 			mutex_exit(&chmp->chm_lock_mountfields);
    441  1.1  ahoka 			dbg_gc("checked_vno (#%llu) > max_vno (#%llu)\n",
    442  1.2    agc 			    (unsigned long long)chmp->chm_checked_vno,
    443  1.2    agc 			    (unsigned long long)chmp->chm_max_vno);
    444  1.1  ahoka 			return ENOSPC;
    445  1.1  ahoka 		}
    446  1.1  ahoka 
    447  1.1  ahoka 		mutex_exit(&chmp->chm_lock_sizes);
    448  1.1  ahoka 
    449  1.1  ahoka 		mutex_enter(&chmp->chm_lock_vnocache);
    450  1.2    agc 		dbg_gc("checking vno #%llu\n",
    451  1.2    agc 			(unsigned long long)chmp->chm_checked_vno);
    452  1.1  ahoka 		dbg_gc("get vnode cache\n");
    453  1.1  ahoka 		vc = chfs_vnode_cache_get(chmp, chmp->chm_checked_vno++);
    454  1.1  ahoka 
    455  1.1  ahoka 		if (!vc) {
    456  1.1  ahoka 			dbg_gc("!vc\n");
    457  1.1  ahoka 			mutex_exit(&chmp->chm_lock_vnocache);
    458  1.1  ahoka 			continue;
    459  1.1  ahoka 		}
    460  1.1  ahoka 
    461  1.1  ahoka 		if ((vc->pvno | vc->nlink) == 0) {
    462  1.1  ahoka 			dbg_gc("(pvno | nlink) == 0\n");
    463  1.1  ahoka 			mutex_exit(&chmp->chm_lock_vnocache);
    464  1.1  ahoka 			continue;
    465  1.1  ahoka 		}
    466  1.1  ahoka 
    467  1.1  ahoka 		dbg_gc("switch\n");
    468  1.1  ahoka 		switch (vc->state) {
    469  1.1  ahoka 		case VNO_STATE_CHECKEDABSENT:
    470  1.1  ahoka 		case VNO_STATE_PRESENT:
    471  1.1  ahoka 			mutex_exit(&chmp->chm_lock_vnocache);
    472  1.1  ahoka 			continue;
    473  1.1  ahoka 
    474  1.1  ahoka 		case VNO_STATE_GC:
    475  1.1  ahoka 		case VNO_STATE_CHECKING:
    476  1.1  ahoka 			mutex_exit(&chmp->chm_lock_vnocache);
    477  1.1  ahoka 			mutex_exit(&chmp->chm_lock_mountfields);
    478  1.1  ahoka 			dbg_gc("VNO_STATE GC or CHECKING\n");
    479  1.1  ahoka 			panic("CHFS BUG - vc state gc or checking\n");
    480  1.1  ahoka 
    481  1.1  ahoka 		case VNO_STATE_READING:
    482  1.1  ahoka 			chmp->chm_checked_vno--;
    483  1.1  ahoka 			mutex_exit(&chmp->chm_lock_vnocache);
    484  1.1  ahoka 			/* XXX why do we need the delay here?! */
    485  1.1  ahoka 			kpause("chvncrea", true, mstohz(50), NULL);
    486  1.1  ahoka 
    487  1.1  ahoka //			sleep_on_spinunlock(&chmp->chm_lock_vnocache);
    488  1.1  ahoka //			KASSERT(!mutex_owned(&chmp->chm_lock_vnocache));
    489  1.1  ahoka 			mutex_exit(&chmp->chm_lock_mountfields);
    490  1.1  ahoka 			return 0;
    491  1.1  ahoka 
    492  1.1  ahoka 		default:
    493  1.1  ahoka 			mutex_exit(&chmp->chm_lock_vnocache);
    494  1.1  ahoka 			mutex_exit(&chmp->chm_lock_mountfields);
    495  1.1  ahoka 			dbg_gc("default\n");
    496  1.1  ahoka 			panic("CHFS BUG - vc state is other what we"
    497  1.1  ahoka 			    " checked\n");
    498  1.1  ahoka 
    499  1.1  ahoka 		case VNO_STATE_UNCHECKED:
    500  1.1  ahoka 			;
    501  1.1  ahoka 		}
    502  1.1  ahoka 
    503  1.1  ahoka 		chfs_vnode_cache_set_state(chmp, vc, VNO_STATE_CHECKING);
    504  1.1  ahoka 
    505  1.1  ahoka 		/* XXX check if this is too heavy to call under
    506  1.1  ahoka 		 * chm_lock_vnocache
    507  1.1  ahoka 		 */
    508  1.1  ahoka 		ret = chfs_check(chmp, vc);
    509  1.1  ahoka 		dbg_gc("set state\n");
    510  1.1  ahoka 		chfs_vnode_cache_set_state(chmp,
    511  1.1  ahoka 		    vc, VNO_STATE_CHECKEDABSENT);
    512  1.1  ahoka 
    513  1.1  ahoka 		mutex_exit(&chmp->chm_lock_vnocache);
    514  1.1  ahoka 		mutex_exit(&chmp->chm_lock_mountfields);
    515  1.1  ahoka 
    516  1.1  ahoka 		return ret;
    517  1.1  ahoka 	}
    518  1.1  ahoka 
    519  1.1  ahoka 
    520  1.1  ahoka 	eb = chmp->chm_gcblock;
    521  1.1  ahoka 
    522  1.1  ahoka 	if (!eb) {
    523  1.1  ahoka 		eb = find_gc_block(chmp);
    524  1.1  ahoka 	}
    525  1.1  ahoka 
    526  1.1  ahoka 	if (!eb) {
    527  1.1  ahoka 		dbg_gc("!eb\n");
    528  1.1  ahoka 		if (!TAILQ_EMPTY(&chmp->chm_erase_pending_queue)) {
    529  1.1  ahoka 			mutex_exit(&chmp->chm_lock_sizes);
    530  1.1  ahoka 			mutex_exit(&chmp->chm_lock_mountfields);
    531  1.1  ahoka 			return EAGAIN;
    532  1.1  ahoka 		}
    533  1.1  ahoka 		mutex_exit(&chmp->chm_lock_sizes);
    534  1.1  ahoka 		mutex_exit(&chmp->chm_lock_mountfields);
    535  1.1  ahoka 		return EIO;
    536  1.1  ahoka 	}
    537  1.1  ahoka 
    538  1.1  ahoka 	if (!eb->used_size) {
    539  1.1  ahoka 		dbg_gc("!eb->used_size\n");
    540  1.1  ahoka 		goto eraseit;
    541  1.1  ahoka 	}
    542  1.1  ahoka 
    543  1.1  ahoka 	nref = eb->gc_node;
    544  1.1  ahoka 	//dbg_gc("gc use: %u\n", chmp->chm_nextblock->lnr);
    545  1.1  ahoka 	//dbg_gc("nref: %u %u\n", nref->nref_lnr, nref->nref_offset);
    546  1.1  ahoka 	gcblock_dirty = eb->dirty_size;
    547  1.1  ahoka 
    548  1.1  ahoka 	while(CHFS_REF_OBSOLETE(nref)) {
    549  1.1  ahoka 		//dbg_gc("obsoleted nref lnr: %u - offset: %u\n", nref->nref_lnr, nref->nref_offset);
    550  1.1  ahoka #ifdef DBG_MSG_GC
    551  1.1  ahoka 		if (nref == chmp->chm_blocks[nref->nref_lnr].last_node) {
    552  1.1  ahoka 			dbg_gc("THIS NODE IS THE LAST NODE OF ITS EB\n");
    553  1.1  ahoka 		}
    554  1.1  ahoka #endif
    555  1.1  ahoka 		nref = node_next(nref);
    556  1.1  ahoka 		if (!nref) {
    557  1.1  ahoka 			//dbg_gc("!nref\n");
    558  1.1  ahoka 			eb->gc_node = nref;
    559  1.1  ahoka 			mutex_exit(&chmp->chm_lock_sizes);
    560  1.1  ahoka 			mutex_exit(&chmp->chm_lock_mountfields);
    561  1.1  ahoka 			panic("CHFS BUG - nref is NULL)\n");
    562  1.1  ahoka 		}
    563  1.1  ahoka 	}
    564  1.1  ahoka 	eb->gc_node = nref;
    565  1.1  ahoka 	//dbg_gc("nref the chosen one lnr: %u - offset: %u\n", nref->nref_lnr, nref->nref_offset);
    566  1.1  ahoka 	KASSERT(nref->nref_lnr == chmp->chm_gcblock->lnr);
    567  1.1  ahoka 
    568  1.1  ahoka 	if (!nref->nref_next) {
    569  1.1  ahoka 		//dbg_gc("!nref->nref_next\n");
    570  1.1  ahoka 		mutex_exit(&chmp->chm_lock_sizes);
    571  1.1  ahoka 		if (CHFS_REF_FLAGS(nref) == CHFS_PRISTINE_NODE_MASK) {
    572  1.1  ahoka 			chfs_gcollect_pristine(chmp, eb, NULL, nref);
    573  1.1  ahoka 		} else {
    574  1.1  ahoka 			chfs_mark_node_obsolete(chmp, nref);
    575  1.1  ahoka 		}
    576  1.1  ahoka 		goto lock_size;
    577  1.1  ahoka 	}
    578  1.1  ahoka 
    579  1.1  ahoka 	dbg_gc("nref lnr: %u - offset: %u\n", nref->nref_lnr, nref->nref_offset);
    580  1.1  ahoka 	vc = chfs_nref_to_vc(nref);
    581  1.1  ahoka 
    582  1.1  ahoka 	mutex_exit(&chmp->chm_lock_sizes);
    583  1.1  ahoka 
    584  1.1  ahoka 	//dbg_gc("enter vnocache lock on #%llu\n", vc->vno);
    585  1.1  ahoka 	mutex_enter(&chmp->chm_lock_vnocache);
    586  1.1  ahoka 
    587  1.1  ahoka 	dbg_gc("switch\n");
    588  1.1  ahoka 	switch(vc->state) {
    589  1.1  ahoka         case VNO_STATE_CHECKEDABSENT:
    590  1.1  ahoka 		if (CHFS_REF_FLAGS(nref) == CHFS_PRISTINE_NODE_MASK) {
    591  1.1  ahoka 			chfs_vnode_cache_set_state(chmp, vc, VNO_STATE_GC);
    592  1.1  ahoka 		}
    593  1.1  ahoka 		break;
    594  1.1  ahoka 
    595  1.1  ahoka         case VNO_STATE_PRESENT:
    596  1.1  ahoka 		break;
    597  1.1  ahoka 
    598  1.1  ahoka         case VNO_STATE_UNCHECKED:
    599  1.1  ahoka         case VNO_STATE_CHECKING:
    600  1.1  ahoka         case VNO_STATE_GC:
    601  1.1  ahoka 		mutex_exit(&chmp->chm_lock_vnocache);
    602  1.1  ahoka 		mutex_exit(&chmp->chm_lock_mountfields);
    603  1.1  ahoka 		panic("CHFS BUG - vc state unchecked,"
    604  1.1  ahoka 		    " checking or gc (vno #%llu, num #%d)\n",
    605  1.2    agc 		    (unsigned long long)vc->vno, vc->state);
    606  1.1  ahoka 
    607  1.1  ahoka         case VNO_STATE_READING:
    608  1.1  ahoka 		mutex_exit(&chmp->chm_lock_vnocache);
    609  1.1  ahoka 		/* XXX why do we need the delay here?! */
    610  1.1  ahoka 		kpause("chvncrea", true, mstohz(50), NULL);
    611  1.1  ahoka 
    612  1.1  ahoka //		sleep_on_spinunlock(&chmp->chm_lock_vnocache);
    613  1.1  ahoka //		KASSERT(!mutex_owned(&chmp->chm_lock_vnocache));
    614  1.1  ahoka 		mutex_exit(&chmp->chm_lock_mountfields);
    615  1.1  ahoka 		return 0;
    616  1.1  ahoka 	}
    617  1.1  ahoka 
    618  1.1  ahoka 	if (vc->state == VNO_STATE_GC) {
    619  1.1  ahoka 		dbg_gc("vc->state == VNO_STATE_GC\n");
    620  1.1  ahoka 		mutex_exit(&chmp->chm_lock_vnocache);
    621  1.1  ahoka 		ret = chfs_gcollect_pristine(chmp, eb, NULL, nref);
    622  1.1  ahoka 
    623  1.1  ahoka //		chfs_vnode_cache_state_set(chmp,
    624  1.1  ahoka //		    vc, VNO_STATE_CHECKEDABSENT);
    625  1.1  ahoka 		/* XXX locking? */
    626  1.1  ahoka 		vc->state = VNO_STATE_CHECKEDABSENT;
    627  1.1  ahoka 		//TODO wake_up(&chmp->chm_vnocache_wq);
    628  1.1  ahoka 		if (ret != EBADF)
    629  1.1  ahoka 			goto test_gcnode;
    630  1.1  ahoka 		mutex_enter(&chmp->chm_lock_vnocache);
    631  1.1  ahoka 	}
    632  1.1  ahoka 
    633  1.1  ahoka 	vno = vc->vno;
    634  1.1  ahoka 	pvno = vc->pvno;
    635  1.1  ahoka 	nlink = vc->nlink;
    636  1.1  ahoka 	mutex_exit(&chmp->chm_lock_vnocache);
    637  1.1  ahoka 
    638  1.1  ahoka 	ip = chfs_gc_fetch_inode(chmp, vno, !(pvno | nlink));
    639  1.1  ahoka 
    640  1.1  ahoka 	if (!ip) {
    641  1.1  ahoka 		dbg_gc("!ip\n");
    642  1.1  ahoka 		ret = 0;
    643  1.1  ahoka 		goto lock_size;
    644  1.1  ahoka 	}
    645  1.1  ahoka 
    646  1.1  ahoka 	chfs_gcollect_live(chmp, eb, nref, ip);
    647  1.1  ahoka 
    648  1.1  ahoka 	chfs_gc_release_inode(chmp, ip);
    649  1.1  ahoka 
    650  1.1  ahoka test_gcnode:
    651  1.1  ahoka 	if (eb->dirty_size == gcblock_dirty &&
    652  1.1  ahoka 	    !CHFS_REF_OBSOLETE(eb->gc_node)) {
    653  1.1  ahoka 		dbg_gc("ERROR collecting node at %u failed.\n",
    654  1.1  ahoka 		    CHFS_GET_OFS(eb->gc_node->nref_offset));
    655  1.1  ahoka 
    656  1.1  ahoka 		ret = ENOSPC;
    657  1.1  ahoka 	}
    658  1.1  ahoka 
    659  1.1  ahoka lock_size:
    660  1.1  ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    661  1.1  ahoka 	mutex_enter(&chmp->chm_lock_sizes);
    662  1.1  ahoka eraseit:
    663  1.1  ahoka 	dbg_gc("eraseit\n");
    664  1.1  ahoka 
    665  1.1  ahoka 	if (chmp->chm_gcblock) {
    666  1.1  ahoka 		dbg_gc("eb used size = %u\n", chmp->chm_gcblock->used_size);
    667  1.1  ahoka 		dbg_gc("eb free size = %u\n", chmp->chm_gcblock->free_size);
    668  1.1  ahoka 		dbg_gc("eb dirty size = %u\n", chmp->chm_gcblock->dirty_size);
    669  1.1  ahoka 		dbg_gc("eb unchecked size = %u\n",
    670  1.1  ahoka 		    chmp->chm_gcblock->unchecked_size);
    671  1.1  ahoka 		dbg_gc("eb wasted size = %u\n", chmp->chm_gcblock->wasted_size);
    672  1.1  ahoka 
    673  1.1  ahoka 		KASSERT(chmp->chm_gcblock->used_size + chmp->chm_gcblock->free_size +
    674  1.1  ahoka 		    chmp->chm_gcblock->dirty_size +
    675  1.1  ahoka 		    chmp->chm_gcblock->unchecked_size +
    676  1.1  ahoka 		    chmp->chm_gcblock->wasted_size == chmp->chm_ebh->eb_size);
    677  1.1  ahoka 
    678  1.1  ahoka 	}
    679  1.1  ahoka 
    680  1.1  ahoka 	if (chmp->chm_gcblock && chmp->chm_gcblock->dirty_size +
    681  1.1  ahoka 	    chmp->chm_gcblock->wasted_size == chmp->chm_ebh->eb_size) {
    682  1.1  ahoka 		dbg_gc("Block at leb #%u completely obsoleted by GC, "
    683  1.1  ahoka 		    "Moving to erase_pending_queue\n", chmp->chm_gcblock->lnr);
    684  1.1  ahoka 		TAILQ_INSERT_TAIL(&chmp->chm_erase_pending_queue,
    685  1.1  ahoka 		    chmp->chm_gcblock, queue);
    686  1.1  ahoka 		chmp->chm_gcblock = NULL;
    687  1.1  ahoka 		chmp->chm_nr_erasable_blocks++;
    688  1.1  ahoka 		if (!TAILQ_EMPTY(&chmp->chm_erase_pending_queue)) {
    689  1.1  ahoka 			ret = chfs_remap_leb(chmp);
    690  1.1  ahoka 		}
    691  1.1  ahoka 	}
    692  1.1  ahoka 
    693  1.1  ahoka 	mutex_exit(&chmp->chm_lock_sizes);
    694  1.1  ahoka 	mutex_exit(&chmp->chm_lock_mountfields);
    695  1.1  ahoka 	dbg_gc("return\n");
    696  1.1  ahoka 	return ret;
    697  1.1  ahoka }
    698  1.1  ahoka 
    699  1.1  ahoka 
    700  1.1  ahoka int
    701  1.1  ahoka chfs_gcollect_pristine(struct chfs_mount *chmp, struct chfs_eraseblock *cheb,
    702  1.1  ahoka     struct chfs_vnode_cache *chvc, struct chfs_node_ref *nref)
    703  1.1  ahoka {
    704  1.1  ahoka 	struct chfs_node_ref *newnref;
    705  1.1  ahoka 	struct chfs_flash_node_hdr *nhdr;
    706  1.1  ahoka 	struct chfs_flash_vnode *fvnode;
    707  1.1  ahoka 	struct chfs_flash_dirent_node *fdirent;
    708  1.1  ahoka 	struct chfs_flash_data_node *fdata;
    709  1.1  ahoka 	int ret, retries = 0;
    710  1.1  ahoka 	uint32_t ofs, crc;
    711  1.1  ahoka 	size_t totlen = chfs_nref_len(chmp, cheb, nref);
    712  1.1  ahoka 	char *data;
    713  1.1  ahoka 	struct iovec vec;
    714  1.1  ahoka 	size_t retlen;
    715  1.1  ahoka 
    716  1.1  ahoka 	dbg_gc("gcollect_pristine\n");
    717  1.1  ahoka 
    718  1.1  ahoka 	data = kmem_alloc(totlen, KM_SLEEP);
    719  1.1  ahoka 	if (!data)
    720  1.1  ahoka 		return ENOMEM;
    721  1.1  ahoka 
    722  1.1  ahoka 	ofs = CHFS_GET_OFS(nref->nref_offset);
    723  1.1  ahoka 
    724  1.1  ahoka 	ret = chfs_read_leb(chmp, nref->nref_lnr, data, ofs, totlen, &retlen);
    725  1.1  ahoka 	if (ret) {
    726  1.1  ahoka 		dbg_gc("reading error\n");
    727  1.1  ahoka 		return ret;
    728  1.1  ahoka 	}
    729  1.1  ahoka 	if (retlen != totlen) {
    730  1.1  ahoka 		dbg_gc("read size error\n");
    731  1.1  ahoka 		return EIO;
    732  1.1  ahoka 	}
    733  1.1  ahoka 	nhdr = (struct chfs_flash_node_hdr *)data;
    734  1.1  ahoka 	/* check the header */
    735  1.1  ahoka 	if (le16toh(nhdr->magic) != CHFS_FS_MAGIC_BITMASK) {
    736  1.1  ahoka 		dbg_gc("node header magic number error\n");
    737  1.1  ahoka 		return EBADF;
    738  1.1  ahoka 	}
    739  1.1  ahoka 	crc = crc32(0, (uint8_t *)nhdr, CHFS_NODE_HDR_SIZE - 4);
    740  1.1  ahoka 	if (crc != le32toh(nhdr->hdr_crc)) {
    741  1.1  ahoka 		dbg_gc("node header crc error\n");
    742  1.1  ahoka 		return EBADF;
    743  1.1  ahoka 	}
    744  1.1  ahoka 
    745  1.1  ahoka 	switch(le16toh(nhdr->type)) {
    746  1.1  ahoka         case CHFS_NODETYPE_VNODE:
    747  1.1  ahoka 		fvnode = (struct chfs_flash_vnode *)data;
    748  1.1  ahoka 	        crc = crc32(0, (uint8_t *)fvnode, sizeof(struct chfs_flash_vnode) - 4);
    749  1.1  ahoka 	        if (crc != le32toh(fvnode->node_crc)) {
    750  1.1  ahoka 			dbg_gc("vnode crc error\n");
    751  1.1  ahoka 			return EBADF;
    752  1.1  ahoka 		}
    753  1.1  ahoka 		break;
    754  1.1  ahoka         case CHFS_NODETYPE_DIRENT:
    755  1.1  ahoka 		fdirent = (struct chfs_flash_dirent_node *)data;
    756  1.1  ahoka 	        crc = crc32(0, (uint8_t *)fdirent, sizeof(struct chfs_flash_dirent_node) - 4);
    757  1.1  ahoka 	        if (crc != le32toh(fdirent->node_crc)) {
    758  1.1  ahoka 			dbg_gc("dirent crc error\n");
    759  1.1  ahoka 			return EBADF;
    760  1.1  ahoka 		}
    761  1.1  ahoka 	        crc = crc32(0, fdirent->name, fdirent->nsize);
    762  1.1  ahoka 	        if (crc != le32toh(fdirent->name_crc)) {
    763  1.1  ahoka 			dbg_gc("dirent name crc error\n");
    764  1.1  ahoka 			return EBADF;
    765  1.1  ahoka 		}
    766  1.1  ahoka 		break;
    767  1.1  ahoka         case CHFS_NODETYPE_DATA:
    768  1.1  ahoka 		fdata = (struct chfs_flash_data_node *)data;
    769  1.1  ahoka 	        crc = crc32(0, (uint8_t *)fdata, sizeof(struct chfs_flash_data_node) - 4);
    770  1.1  ahoka 	        if (crc != le32toh(fdata->node_crc)) {
    771  1.1  ahoka 			dbg_gc("data node crc error\n");
    772  1.1  ahoka 			return EBADF;
    773  1.1  ahoka 		}
    774  1.1  ahoka 		break;
    775  1.1  ahoka         default:
    776  1.1  ahoka 		if (chvc) {
    777  1.1  ahoka 			dbg_gc("unknown node have vnode cache\n");
    778  1.1  ahoka 			return EBADF;
    779  1.1  ahoka 		}
    780  1.1  ahoka 	}
    781  1.1  ahoka 	/* CRC's OK, write node to its new place */
    782  1.1  ahoka retry:
    783  1.1  ahoka 	ret = chfs_reserve_space_gc(chmp, totlen);
    784  1.1  ahoka 	if (ret)
    785  1.1  ahoka 		return ret;
    786  1.1  ahoka 
    787  1.1  ahoka 	newnref = chfs_alloc_node_ref(chmp->chm_nextblock);
    788  1.1  ahoka 	if (!newnref)
    789  1.1  ahoka 		return ENOMEM;
    790  1.1  ahoka 
    791  1.1  ahoka 	ofs = chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
    792  1.1  ahoka 	newnref->nref_offset = ofs;
    793  1.1  ahoka 
    794  1.1  ahoka 	vec.iov_base = (void *)data;
    795  1.1  ahoka 	vec.iov_len = totlen;
    796  1.1  ahoka 	mutex_enter(&chmp->chm_lock_sizes);
    797  1.1  ahoka 	ret = chfs_write_wbuf(chmp, &vec, 1, ofs, &retlen);
    798  1.1  ahoka 
    799  1.1  ahoka 	if (ret || retlen != totlen) {
    800  1.1  ahoka 		chfs_err("error while writing out to the media\n");
    801  1.1  ahoka 		chfs_err("err: %d | size: %zu | retlen : %zu\n",
    802  1.1  ahoka 		    ret, totlen, retlen);
    803  1.1  ahoka 
    804  1.1  ahoka 		chfs_change_size_dirty(chmp, chmp->chm_nextblock, totlen);
    805  1.1  ahoka 		if (retries) {
    806  1.1  ahoka 			mutex_exit(&chmp->chm_lock_sizes);
    807  1.1  ahoka 			return EIO;
    808  1.1  ahoka 		}
    809  1.1  ahoka 
    810  1.1  ahoka 		retries++;
    811  1.1  ahoka 		mutex_exit(&chmp->chm_lock_sizes);
    812  1.1  ahoka 		goto retry;
    813  1.1  ahoka 	}
    814  1.1  ahoka 
    815  1.1  ahoka 	mutex_exit(&chmp->chm_lock_sizes);
    816  1.1  ahoka 	//TODO should we set free_size?
    817  1.1  ahoka 	chfs_mark_node_obsolete(chmp, nref);
    818  1.1  ahoka 	chfs_add_vnode_ref_to_vc(chmp, chvc, newnref);
    819  1.1  ahoka 	return 0;
    820  1.1  ahoka }
    821  1.1  ahoka 
    822  1.1  ahoka 
    823  1.1  ahoka int
    824  1.1  ahoka chfs_gcollect_live(struct chfs_mount *chmp,
    825  1.1  ahoka     struct chfs_eraseblock *cheb, struct chfs_node_ref *nref,
    826  1.1  ahoka     struct chfs_inode *ip)
    827  1.1  ahoka {
    828  1.1  ahoka 	struct chfs_node_frag *frag;
    829  1.1  ahoka 	struct chfs_full_dnode *fn = NULL;
    830  1.1  ahoka 	int start = 0, end = 0, nrfrags = 0;
    831  1.1  ahoka 	struct chfs_dirent *fd = NULL;
    832  1.1  ahoka 	int ret = 0;
    833  1.1  ahoka 	bool is_dirent;
    834  1.1  ahoka 
    835  1.1  ahoka 	dbg_gc("gcollect_live\n");
    836  1.1  ahoka 
    837  1.1  ahoka 	if (chmp->chm_gcblock != cheb) {
    838  1.1  ahoka 		dbg_gc("GC block is no longer gcblock. Restart.\n");
    839  1.1  ahoka 		goto upnout;
    840  1.1  ahoka 	}
    841  1.1  ahoka 
    842  1.1  ahoka 	if (CHFS_REF_OBSOLETE(nref)) {
    843  1.1  ahoka 		dbg_gc("node to be GC'd was obsoleted in the meantime.\n");
    844  1.1  ahoka 		goto upnout;
    845  1.1  ahoka 	}
    846  1.1  ahoka 
    847  1.1  ahoka 	/* It's a vnode? */
    848  1.1  ahoka 	if (ip->chvc->v == nref) {
    849  1.1  ahoka 		chfs_gcollect_vnode(chmp, ip);
    850  1.1  ahoka 		goto upnout;
    851  1.1  ahoka 	}
    852  1.1  ahoka 
    853  1.1  ahoka 	/* find fn */
    854  1.1  ahoka 	dbg_gc("find full dnode\n");
    855  1.1  ahoka 	for(frag = frag_first(&ip->fragtree);
    856  1.1  ahoka 	    frag; frag = frag_next(&ip->fragtree, frag)) {
    857  1.1  ahoka 		if (frag->node && frag->node->nref == nref) {
    858  1.1  ahoka 			fn = frag->node;
    859  1.1  ahoka 			end = frag->ofs + frag->size;
    860  1.1  ahoka 			if (!nrfrags++)
    861  1.1  ahoka 				start = frag->ofs;
    862  1.1  ahoka 			if (nrfrags == frag->node->frags)
    863  1.1  ahoka 				break;
    864  1.1  ahoka 		}
    865  1.1  ahoka 	}
    866  1.1  ahoka 
    867  1.1  ahoka 	/* It's a pristine node, or dnode (or hole? XXX have we hole nodes?) */
    868  1.1  ahoka 	if (fn) {
    869  1.1  ahoka 		if (CHFS_REF_FLAGS(nref) == CHFS_PRISTINE_NODE_MASK) {
    870  1.1  ahoka 			ret = chfs_gcollect_pristine(chmp,
    871  1.1  ahoka 			    cheb, ip->chvc, nref);
    872  1.1  ahoka 			if (!ret) {
    873  1.1  ahoka 				frag->node->nref = ip->chvc->v;
    874  1.1  ahoka 			}
    875  1.1  ahoka 			if (ret != EBADF)
    876  1.1  ahoka 				goto upnout;
    877  1.1  ahoka 		}
    878  1.1  ahoka 		//ret = chfs_gcollect_hole(chmp, cheb, ip, fn, start, end);
    879  1.1  ahoka 		ret = chfs_gcollect_dnode(chmp, cheb, ip, fn, start, end);
    880  1.1  ahoka 		goto upnout;
    881  1.1  ahoka 	}
    882  1.1  ahoka 
    883  1.1  ahoka 
    884  1.1  ahoka 	/* It's a dirent? */
    885  1.1  ahoka 	dbg_gc("find full dirent\n");
    886  1.1  ahoka 	is_dirent = false;
    887  1.1  ahoka 	TAILQ_FOREACH(fd, &ip->dents, fds) {
    888  1.1  ahoka 		if (fd->nref == nref) {
    889  1.1  ahoka 			is_dirent = true;
    890  1.1  ahoka 			break;
    891  1.1  ahoka 		}
    892  1.1  ahoka 	}
    893  1.1  ahoka 
    894  1.1  ahoka 	if (is_dirent && fd->vno) {
    895  1.1  ahoka 		ret = chfs_gcollect_dirent(chmp, cheb, ip, fd);
    896  1.1  ahoka 	} else if (is_dirent) {
    897  1.1  ahoka 		ret = chfs_gcollect_deletion_dirent(chmp, cheb, ip, fd);
    898  1.1  ahoka 	} else {
    899  1.1  ahoka 		dbg_gc("Nref at leb #%u offset 0x%08x wasn't in node list"
    900  1.1  ahoka 		    " for ino #%llu\n",
    901  1.2    agc 		    nref->nref_lnr, CHFS_GET_OFS(nref->nref_offset),
    902  1.2    agc 		    (unsigned long long)ip->ino);
    903  1.1  ahoka 		if (CHFS_REF_OBSOLETE(nref)) {
    904  1.1  ahoka 			dbg_gc("But it's obsolete so we don't mind"
    905  1.1  ahoka 			    " too much.\n");
    906  1.1  ahoka 		}
    907  1.1  ahoka 	}
    908  1.1  ahoka 
    909  1.1  ahoka upnout:
    910  1.1  ahoka 	return ret;
    911  1.1  ahoka }
    912  1.1  ahoka 
    913  1.1  ahoka int
    914  1.1  ahoka chfs_gcollect_vnode(struct chfs_mount *chmp, struct chfs_inode *ip)
    915  1.1  ahoka {
    916  1.1  ahoka 	int ret;
    917  1.1  ahoka 	dbg_gc("gcollect_vnode\n");
    918  1.1  ahoka 
    919  1.1  ahoka 	ret = chfs_write_flash_vnode(chmp, ip, ALLOC_GC);
    920  1.1  ahoka 
    921  1.1  ahoka 	return ret;
    922  1.1  ahoka }
    923  1.1  ahoka 
    924  1.1  ahoka int
    925  1.1  ahoka chfs_gcollect_dirent(struct chfs_mount *chmp,
    926  1.1  ahoka     struct chfs_eraseblock *cheb, struct chfs_inode *parent,
    927  1.1  ahoka     struct chfs_dirent *fd)
    928  1.1  ahoka {
    929  1.1  ahoka 	struct vnode *vnode = NULL;
    930  1.1  ahoka 	struct chfs_inode *ip;
    931  1.1  ahoka 	struct chfs_node_ref *prev;
    932  1.1  ahoka 	dbg_gc("gcollect_dirent\n");
    933  1.1  ahoka 
    934  1.1  ahoka 	vnode = chfs_vnode_lookup(chmp, fd->vno);
    935  1.1  ahoka 
    936  1.1  ahoka 	/* XXX maybe KASSERT or panic on this? */
    937  1.1  ahoka 	if (vnode == NULL) {
    938  1.1  ahoka 		return ENOENT;
    939  1.1  ahoka 	}
    940  1.1  ahoka 
    941  1.1  ahoka 	ip = VTOI(vnode);
    942  1.1  ahoka 
    943  1.1  ahoka 	prev = parent->chvc->dirents;
    944  1.1  ahoka 	if (prev == fd->nref) {
    945  1.1  ahoka 		parent->chvc->dirents = prev->nref_next;
    946  1.1  ahoka 		dbg_gc("fd nref removed from dirents list\n");
    947  1.1  ahoka 		prev = NULL;
    948  1.1  ahoka 	}
    949  1.1  ahoka 	while (prev) {
    950  1.1  ahoka 		if (prev->nref_next == fd->nref) {
    951  1.1  ahoka 			prev->nref_next = fd->nref->nref_next;
    952  1.1  ahoka 			dbg_gc("fd nref removed from dirents list\n");
    953  1.1  ahoka 			break;
    954  1.1  ahoka 		}
    955  1.1  ahoka 		prev = prev->nref_next;
    956  1.1  ahoka 	}
    957  1.1  ahoka 
    958  1.1  ahoka 	prev = fd->nref;
    959  1.1  ahoka 	chfs_mark_node_obsolete(chmp, fd->nref);
    960  1.1  ahoka 	return chfs_write_flash_dirent(chmp,
    961  1.1  ahoka 	    parent, ip, fd, fd->vno, ALLOC_GC);
    962  1.1  ahoka }
    963  1.1  ahoka 
    964  1.1  ahoka /* Check dirents what are marked as deleted. */
    965  1.1  ahoka int
    966  1.1  ahoka chfs_gcollect_deletion_dirent(struct chfs_mount *chmp,
    967  1.1  ahoka     struct chfs_eraseblock *cheb, struct chfs_inode *parent,
    968  1.1  ahoka     struct chfs_dirent *fd)
    969  1.1  ahoka {
    970  1.1  ahoka 	struct chfs_flash_dirent_node chfdn;
    971  1.1  ahoka 	struct chfs_node_ref *nref;
    972  1.1  ahoka 	size_t retlen, name_len, nref_len;
    973  1.1  ahoka 	uint32_t name_crc;
    974  1.1  ahoka 
    975  1.1  ahoka 	int ret;
    976  1.1  ahoka 
    977  1.1  ahoka 	struct vnode *vnode = NULL;
    978  1.1  ahoka 
    979  1.1  ahoka 	dbg_gc("gcollect_deletion_dirent\n");
    980  1.1  ahoka 
    981  1.1  ahoka 	name_len = strlen(fd->name);
    982  1.1  ahoka 	name_crc = crc32(0, fd->name, name_len);
    983  1.1  ahoka 
    984  1.1  ahoka 	nref_len = chfs_nref_len(chmp, cheb, fd->nref);
    985  1.1  ahoka 
    986  1.1  ahoka 	vnode = chfs_vnode_lookup(chmp, fd->vno);
    987  1.1  ahoka 
    988  1.1  ahoka 	//dbg_gc("ip from vnode\n");
    989  1.1  ahoka 	//VFS_VGET(chmp->chm_fsmp, fd->vno, &vnode);
    990  1.1  ahoka 	//ip = VTOI(vnode);
    991  1.1  ahoka 	//vput(vnode);
    992  1.1  ahoka 
    993  1.1  ahoka 	//dbg_gc("mutex enter erase_completion_lock\n");
    994  1.1  ahoka 
    995  1.1  ahoka //	dbg_gc("alloc chfdn\n");
    996  1.1  ahoka //	chfdn = kmem_alloc(nref_len, KM_SLEEP);
    997  1.1  ahoka //	if (!chfdn)
    998  1.1  ahoka //		return ENOMEM;
    999  1.1  ahoka 
   1000  1.1  ahoka 	for (nref = parent->chvc->dirents;
   1001  1.1  ahoka 	     nref != (void*)parent->chvc;
   1002  1.1  ahoka 	     nref = nref->nref_next) {
   1003  1.1  ahoka 
   1004  1.1  ahoka 		if (!CHFS_REF_OBSOLETE(nref))
   1005  1.1  ahoka 			continue;
   1006  1.1  ahoka 
   1007  1.1  ahoka 		/* if node refs have different length, skip */
   1008  1.1  ahoka 		if (chfs_nref_len(chmp, NULL, nref) != nref_len)
   1009  1.1  ahoka 			continue;
   1010  1.1  ahoka 
   1011  1.1  ahoka 		if (CHFS_GET_OFS(nref->nref_offset) ==
   1012  1.1  ahoka 		    CHFS_GET_OFS(fd->nref->nref_offset)) {
   1013  1.1  ahoka 			continue;
   1014  1.1  ahoka 		}
   1015  1.1  ahoka 
   1016  1.1  ahoka 		ret = chfs_read_leb(chmp,
   1017  1.1  ahoka 		    nref->nref_lnr, (void*)&chfdn, CHFS_GET_OFS(nref->nref_offset),
   1018  1.1  ahoka 		    nref_len, &retlen);
   1019  1.1  ahoka 
   1020  1.1  ahoka 		if (ret) {
   1021  1.1  ahoka 			dbg_gc("Read error: %d\n", ret);
   1022  1.1  ahoka 			continue;
   1023  1.1  ahoka 		}
   1024  1.1  ahoka 
   1025  1.1  ahoka 		if (retlen != nref_len) {
   1026  1.1  ahoka 			dbg_gc("Error reading node:"
   1027  1.1  ahoka 			    " read: %zu insted of: %zu\n", retlen, nref_len);
   1028  1.1  ahoka 			continue;
   1029  1.1  ahoka 		}
   1030  1.1  ahoka 
   1031  1.1  ahoka 		/* if node type doesn't match, skip */
   1032  1.1  ahoka 		if (le16toh(chfdn.type) != CHFS_NODETYPE_DIRENT)
   1033  1.1  ahoka 			continue;
   1034  1.1  ahoka 
   1035  1.1  ahoka 		/* if crc doesn't match, skip */
   1036  1.1  ahoka 		if (le32toh(chfdn.name_crc) != name_crc)
   1037  1.1  ahoka 			continue;
   1038  1.1  ahoka 
   1039  1.1  ahoka 		/* if length of name different, or this is an another deletion
   1040  1.1  ahoka 		 * dirent, skip
   1041  1.1  ahoka 		 */
   1042  1.1  ahoka 		if (chfdn.nsize != name_len || !le64toh(chfdn.vno))
   1043  1.1  ahoka 			continue;
   1044  1.1  ahoka 
   1045  1.1  ahoka 		/* check actual name */
   1046  1.1  ahoka 		if (memcmp(chfdn.name, fd->name, name_len))
   1047  1.1  ahoka 			continue;
   1048  1.1  ahoka 
   1049  1.1  ahoka //		kmem_free(chfdn, nref_len);
   1050  1.1  ahoka 
   1051  1.1  ahoka 		chfs_mark_node_obsolete(chmp, fd->nref);
   1052  1.1  ahoka 		return chfs_write_flash_dirent(chmp,
   1053  1.1  ahoka 		    parent, NULL, fd, fd->vno, ALLOC_GC);
   1054  1.1  ahoka 	}
   1055  1.1  ahoka 
   1056  1.1  ahoka //	kmem_free(chfdn, nref_len);
   1057  1.1  ahoka 
   1058  1.1  ahoka 	TAILQ_REMOVE(&parent->dents, fd, fds);
   1059  1.1  ahoka 	chfs_free_dirent(fd);
   1060  1.1  ahoka 	return 0;
   1061  1.1  ahoka }
   1062  1.1  ahoka 
   1063  1.1  ahoka int
   1064  1.1  ahoka chfs_gcollect_dnode(struct chfs_mount *chmp,
   1065  1.1  ahoka     struct chfs_eraseblock *orig_cheb, struct chfs_inode *ip,
   1066  1.1  ahoka     struct chfs_full_dnode *fn, uint32_t orig_start, uint32_t orig_end)
   1067  1.1  ahoka {
   1068  1.1  ahoka 	struct chfs_node_ref *nref, *prev;
   1069  1.1  ahoka 	struct chfs_full_dnode *newfn;
   1070  1.1  ahoka 	struct chfs_flash_data_node *fdnode;
   1071  1.1  ahoka 	int ret = 0, retries = 0;
   1072  1.1  ahoka 	uint32_t totlen;
   1073  1.1  ahoka 	char *data = NULL;
   1074  1.1  ahoka 	struct iovec vec;
   1075  1.1  ahoka 	size_t retlen;
   1076  1.1  ahoka 	dbg_gc("gcollect_dnode\n");
   1077  1.1  ahoka 
   1078  1.1  ahoka 	//uint32_t used_size;
   1079  1.1  ahoka 
   1080  1.1  ahoka /* TODO GC merging frags, should we use it?
   1081  1.1  ahoka 
   1082  1.1  ahoka    uint32_t start, end;
   1083  1.1  ahoka 
   1084  1.1  ahoka    start = orig_start;
   1085  1.1  ahoka    end = orig_end;
   1086  1.1  ahoka 
   1087  1.1  ahoka    if (chmp->chm_nr_free_blocks + chmp->chm_nr_erasable_blocks > chmp->chm_resv_blocks_gcmerge) {
   1088  1.1  ahoka    struct chfs_node_frag *frag;
   1089  1.1  ahoka    uint32_t min, max;
   1090  1.1  ahoka 
   1091  1.1  ahoka    min = start & (PAGE_CACHE_SIZE-1);
   1092  1.1  ahoka    max = min + PAGE_CACHE_SIZE;
   1093  1.1  ahoka 
   1094  1.1  ahoka    frag = (struct chfs_node_frag *)rb_tree_find_node_leq(&ip->i_chfs_ext.fragtree, &start);
   1095  1.1  ahoka    KASSERT(frag->ofs == start);
   1096  1.1  ahoka 
   1097  1.1  ahoka    while ((frag = frag_prev(&ip->i_chfs_ext.fragtree, frag)) && frag->ofs >= min) {
   1098  1.1  ahoka    if (frag->ofs > min) {
   1099  1.1  ahoka    start = frag->ofs;
   1100  1.1  ahoka    continue;
   1101  1.1  ahoka    }
   1102  1.1  ahoka 
   1103  1.1  ahoka    if (!frag->node || !frag->node->nref) {
   1104  1.1  ahoka    break;
   1105  1.1  ahoka    } else {
   1106  1.1  ahoka    struct chfs_node_ref *nref = frag->node->nref;
   1107  1.1  ahoka    struct chfs_eraseblock *cheb;
   1108  1.1  ahoka 
   1109  1.1  ahoka    cheb = &chmp->chm_blocks[nref->nref_lnr];
   1110  1.1  ahoka 
   1111  1.1  ahoka    if (cheb == chmp->chm_gcblock)
   1112  1.1  ahoka    start = frag->ofs;
   1113  1.1  ahoka 
   1114  1.1  ahoka    //TODO is this a clean block?
   1115  1.1  ahoka 
   1116  1.1  ahoka    start = frag->ofs;
   1117  1.1  ahoka    break;
   1118  1.1  ahoka    }
   1119  1.1  ahoka    }
   1120  1.1  ahoka 
   1121  1.1  ahoka    end--;
   1122  1.1  ahoka    frag = (struct chfs_node_frag *)rb_tree_find_node_leq(&ip->i_chfs_ext.fragtree, &(end));
   1123  1.1  ahoka 
   1124  1.1  ahoka    while ((frag = frag_next(&ip->i_chfs_ext.fragtree, frag)) && (frag->ofs + frag->size <= max)) {
   1125  1.1  ahoka    if (frag->ofs + frag->size < max) {
   1126  1.1  ahoka    end = frag->ofs + frag->size;
   1127  1.1  ahoka    continue;
   1128  1.1  ahoka    }
   1129  1.1  ahoka 
   1130  1.1  ahoka    if (!frag->node || !frag->node->nref) {
   1131  1.1  ahoka    break;
   1132  1.1  ahoka    } else {
   1133  1.1  ahoka    struct chfs_node_ref *nref = frag->node->nref;
   1134  1.1  ahoka    struct chfs_eraseblock *cheb;
   1135  1.1  ahoka 
   1136  1.1  ahoka    cheb = &chmp->chm_blocks[nref->nref_lnr];
   1137  1.1  ahoka 
   1138  1.1  ahoka    if (cheb == chmp->chm_gcblock)
   1139  1.1  ahoka    end = frag->ofs + frag->size;
   1140  1.1  ahoka 
   1141  1.1  ahoka    //TODO is this a clean block?
   1142  1.1  ahoka 
   1143  1.1  ahoka    end = frag->ofs + frag->size;
   1144  1.1  ahoka    break;
   1145  1.1  ahoka    }
   1146  1.1  ahoka    }
   1147  1.1  ahoka 
   1148  1.1  ahoka    KASSERT(end <=
   1149  1.1  ahoka    frag_last(&ip->i_chfs_ext.fragtree)->ofs +
   1150  1.1  ahoka    frag_last(&ip->i_chfs_ext.fragtree)->size);
   1151  1.1  ahoka    KASSERT(end >= orig_end);
   1152  1.1  ahoka    KASSERT(start <= orig_start);
   1153  1.1  ahoka    }
   1154  1.1  ahoka */
   1155  1.1  ahoka 	KASSERT(orig_cheb->lnr == fn->nref->nref_lnr);
   1156  1.1  ahoka 	totlen = chfs_nref_len(chmp, orig_cheb, fn->nref);
   1157  1.1  ahoka 	data = kmem_alloc(totlen, KM_SLEEP);
   1158  1.1  ahoka 
   1159  1.1  ahoka 	ret = chfs_read_leb(chmp, fn->nref->nref_lnr, data, fn->nref->nref_offset,
   1160  1.1  ahoka 	    totlen, &retlen);
   1161  1.1  ahoka 
   1162  1.1  ahoka 	fdnode = (struct chfs_flash_data_node *)data;
   1163  1.1  ahoka 	fdnode->version = htole64(++ip->chvc->highest_version);
   1164  1.1  ahoka 	fdnode->node_crc = htole32(crc32(0, (uint8_t *)fdnode,
   1165  1.1  ahoka 		sizeof(*fdnode) - 4));
   1166  1.1  ahoka 
   1167  1.1  ahoka 	vec.iov_base = (void *)data;
   1168  1.1  ahoka 	vec.iov_len = totlen;
   1169  1.1  ahoka 
   1170  1.1  ahoka retry:
   1171  1.1  ahoka 	ret = chfs_reserve_space_gc(chmp, totlen);
   1172  1.1  ahoka 	if (ret)
   1173  1.1  ahoka 		goto out;
   1174  1.1  ahoka 
   1175  1.1  ahoka 	nref = chfs_alloc_node_ref(chmp->chm_nextblock);
   1176  1.1  ahoka 	if (!nref) {
   1177  1.1  ahoka 		ret = ENOMEM;
   1178  1.1  ahoka 		goto out;
   1179  1.1  ahoka 	}
   1180  1.1  ahoka 
   1181  1.1  ahoka 	mutex_enter(&chmp->chm_lock_sizes);
   1182  1.1  ahoka 
   1183  1.1  ahoka 	nref->nref_offset = chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
   1184  1.1  ahoka 	KASSERT(nref->nref_offset % 4 == 0);
   1185  1.1  ahoka 	chfs_change_size_free(chmp, chmp->chm_nextblock, -totlen);
   1186  1.1  ahoka 
   1187  1.1  ahoka 	ret = chfs_write_wbuf(chmp, &vec, 1, nref->nref_offset, &retlen);
   1188  1.1  ahoka 	if (ret || retlen != totlen) {
   1189  1.1  ahoka 		chfs_err("error while writing out to the media\n");
   1190  1.1  ahoka 		chfs_err("err: %d | size: %d | retlen : %zu\n",
   1191  1.1  ahoka 		    ret, totlen, retlen);
   1192  1.1  ahoka 		chfs_change_size_dirty(chmp, chmp->chm_nextblock, totlen);
   1193  1.1  ahoka 		if (retries) {
   1194  1.1  ahoka 			ret = EIO;
   1195  1.1  ahoka 			mutex_exit(&chmp->chm_lock_sizes);
   1196  1.1  ahoka 			goto out;
   1197  1.1  ahoka 		}
   1198  1.1  ahoka 
   1199  1.1  ahoka 		retries++;
   1200  1.1  ahoka 		mutex_exit(&chmp->chm_lock_sizes);
   1201  1.1  ahoka 		goto retry;
   1202  1.1  ahoka 	}
   1203  1.1  ahoka 
   1204  1.1  ahoka 	dbg_gc("new nref lnr: %u - offset: %u\n", nref->nref_lnr, nref->nref_offset);
   1205  1.1  ahoka 
   1206  1.1  ahoka 	chfs_change_size_used(chmp, &chmp->chm_blocks[nref->nref_lnr], totlen);
   1207  1.1  ahoka 	mutex_exit(&chmp->chm_lock_sizes);
   1208  1.1  ahoka 	KASSERT(chmp->chm_blocks[nref->nref_lnr].used_size <= chmp->chm_ebh->eb_size);
   1209  1.1  ahoka 
   1210  1.1  ahoka 	newfn = chfs_alloc_full_dnode();
   1211  1.1  ahoka 	newfn->nref = nref;
   1212  1.1  ahoka 	newfn->ofs = fn->ofs;
   1213  1.1  ahoka 	newfn->size = fn->size;
   1214  1.1  ahoka 	newfn->frags = fn->frags;
   1215  1.1  ahoka 
   1216  1.1  ahoka 	//TODO should we remove fd from dnode list?
   1217  1.1  ahoka 
   1218  1.1  ahoka 	prev = ip->chvc->dnode;
   1219  1.1  ahoka 	if (prev == fn->nref) {
   1220  1.1  ahoka 		ip->chvc->dnode = prev->nref_next;
   1221  1.1  ahoka 		prev = NULL;
   1222  1.1  ahoka 	}
   1223  1.1  ahoka 	while (prev) {
   1224  1.1  ahoka 		if (prev->nref_next == fn->nref) {
   1225  1.1  ahoka 			prev->nref_next = fn->nref->nref_next;
   1226  1.1  ahoka 			break;
   1227  1.1  ahoka 		}
   1228  1.1  ahoka 		prev = prev->nref_next;
   1229  1.1  ahoka 	}
   1230  1.1  ahoka 
   1231  1.1  ahoka 	chfs_add_full_dnode_to_inode(chmp, ip, newfn);
   1232  1.1  ahoka 	chfs_add_node_to_list(chmp,
   1233  1.1  ahoka 	    ip->chvc, newfn->nref, &ip->chvc->dnode);
   1234  1.1  ahoka 
   1235  1.1  ahoka out:
   1236  1.1  ahoka 	kmem_free(data, totlen);
   1237  1.1  ahoka 	return ret;
   1238  1.1  ahoka }
   1239