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