Home | History | Annotate | Line # | Download | only in chfs
chfs_gc.c revision 1.5
      1 /*	$NetBSD: chfs_gc.c,v 1.5 2013/10/20 17:18:38 christos 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 		return ret;
    730 	}
    731 	if (retlen != totlen) {
    732 		dbg_gc("read size error\n");
    733 		return EIO;
    734 	}
    735 	nhdr = (struct chfs_flash_node_hdr *)data;
    736 
    737 	/* Check the header. */
    738 	if (le16toh(nhdr->magic) != CHFS_FS_MAGIC_BITMASK) {
    739 		dbg_gc("node header magic number error\n");
    740 		return EBADF;
    741 	}
    742 	crc = crc32(0, (uint8_t *)nhdr, CHFS_NODE_HDR_SIZE - 4);
    743 	if (crc != le32toh(nhdr->hdr_crc)) {
    744 		dbg_gc("node header crc error\n");
    745 		return EBADF;
    746 	}
    747 
    748 	/* Read the remaining parts. */
    749 	switch(le16toh(nhdr->type)) {
    750         case CHFS_NODETYPE_VNODE:
    751 		/* vnode information node */
    752 			fvnode = (struct chfs_flash_vnode *)data;
    753 	        crc = crc32(0, (uint8_t *)fvnode, sizeof(struct chfs_flash_vnode) - 4);
    754 	        if (crc != le32toh(fvnode->node_crc)) {
    755 				dbg_gc("vnode crc error\n");
    756 				return EBADF;
    757 			}
    758 			break;
    759         case CHFS_NODETYPE_DIRENT:
    760 		/* dirent node */
    761 			fdirent = (struct chfs_flash_dirent_node *)data;
    762 	        crc = crc32(0, (uint8_t *)fdirent, sizeof(struct chfs_flash_dirent_node) - 4);
    763 	        if (crc != le32toh(fdirent->node_crc)) {
    764 				dbg_gc("dirent crc error\n");
    765 				return EBADF;
    766 			}
    767 	        crc = crc32(0, fdirent->name, fdirent->nsize);
    768 	        if (crc != le32toh(fdirent->name_crc)) {
    769 				dbg_gc("dirent name crc error\n");
    770 				return EBADF;
    771 			}
    772 			break;
    773         case CHFS_NODETYPE_DATA:
    774 		/* data node */
    775 			fdata = (struct chfs_flash_data_node *)data;
    776 	        crc = crc32(0, (uint8_t *)fdata, sizeof(struct chfs_flash_data_node) - 4);
    777 	        if (crc != le32toh(fdata->node_crc)) {
    778 				dbg_gc("data node crc error\n");
    779 				return EBADF;
    780 			}
    781 			break;
    782         default:
    783 		/* unknown node */
    784 			if (chvc) {
    785 				dbg_gc("unknown node have vnode cache\n");
    786 				return EBADF;
    787 			}
    788 	}
    789 	/* CRC's OK, write node to its new place */
    790 retry:
    791 	ret = chfs_reserve_space_gc(chmp, totlen);
    792 	if (ret)
    793 		return ret;
    794 
    795 	newnref = chfs_alloc_node_ref(chmp->chm_nextblock);
    796 	if (!newnref)
    797 		return ENOMEM;
    798 
    799 	ofs = chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
    800 	newnref->nref_offset = ofs;
    801 
    802 	/* write out the whole node */
    803 	vec.iov_base = (void *)data;
    804 	vec.iov_len = totlen;
    805 	mutex_enter(&chmp->chm_lock_sizes);
    806 	ret = chfs_write_wbuf(chmp, &vec, 1, ofs, &retlen);
    807 
    808 	if (ret || retlen != totlen) {
    809 		/* error while writing */
    810 		chfs_err("error while writing out to the media\n");
    811 		chfs_err("err: %d | size: %zu | retlen : %zu\n",
    812 		    ret, totlen, retlen);
    813 
    814 		chfs_change_size_dirty(chmp, chmp->chm_nextblock, totlen);
    815 		if (retries) {
    816 			mutex_exit(&chmp->chm_lock_sizes);
    817 			return EIO;
    818 		}
    819 
    820 		/* try again */
    821 		retries++;
    822 		mutex_exit(&chmp->chm_lock_sizes);
    823 		goto retry;
    824 	}
    825 
    826 	/* update vnode information */
    827 	mutex_exit(&chmp->chm_lock_sizes);
    828 	//TODO should we set free_size?
    829 	mutex_enter(&chmp->chm_lock_vnocache);
    830 	chfs_add_vnode_ref_to_vc(chmp, chvc, newnref);
    831 	mutex_exit(&chmp->chm_lock_vnocache);
    832 	return 0;
    833 }
    834 
    835 
    836 /* chfs_gcollect_live - collects a living node */
    837 int
    838 chfs_gcollect_live(struct chfs_mount *chmp,
    839     struct chfs_eraseblock *cheb, struct chfs_node_ref *nref,
    840     struct chfs_inode *ip)
    841 {
    842 	struct chfs_node_frag *frag;
    843 	struct chfs_full_dnode *fn = NULL;
    844 	int start = 0, end = 0, nrfrags = 0;
    845 	struct chfs_dirent *fd = NULL;
    846 	int ret = 0;
    847 	bool is_dirent;
    848 
    849 	dbg_gc("gcollect_live\n");
    850 
    851 	if (chmp->chm_gcblock != cheb) {
    852 		dbg_gc("GC block is no longer gcblock. Restart.\n");
    853 		goto upnout;
    854 	}
    855 
    856 	if (CHFS_REF_OBSOLETE(nref)) {
    857 		dbg_gc("node to be GC'd was obsoleted in the meantime.\n");
    858 		goto upnout;
    859 	}
    860 
    861 	/* It's a vnode? */
    862 	if (ip->chvc->v == nref) {
    863 		chfs_gcollect_vnode(chmp, ip);
    864 		goto upnout;
    865 	}
    866 
    867 	/* Find data node. */
    868 	dbg_gc("find full dnode\n");
    869 	for(frag = frag_first(&ip->fragtree);
    870 	    frag; frag = frag_next(&ip->fragtree, frag)) {
    871 		if (frag->node && frag->node->nref == nref) {
    872 			fn = frag->node;
    873 			end = frag->ofs + frag->size;
    874 			if (!nrfrags++)
    875 				start = frag->ofs;
    876 			if (nrfrags == frag->node->frags)
    877 				break;
    878 		}
    879 	}
    880 
    881 	/* It's a pristine node, or dnode (or hole? XXX have we hole nodes?) */
    882 	if (fn) {
    883 		if (CHFS_REF_FLAGS(nref) == CHFS_PRISTINE_NODE_MASK) {
    884 			ret = chfs_gcollect_pristine(chmp,
    885 			    cheb, ip->chvc, nref);
    886 			if (!ret) {
    887 				frag->node->nref = ip->chvc->v;
    888 			}
    889 			if (ret != EBADF)
    890 				goto upnout;
    891 		}
    892 		ret = chfs_gcollect_dnode(chmp, cheb, ip, fn, start, end);
    893 		goto upnout;
    894 	}
    895 
    896 	/* Is it a dirent? */
    897 	dbg_gc("find full dirent\n");
    898 	is_dirent = false;
    899 	TAILQ_FOREACH(fd, &ip->dents, fds) {
    900 		if (fd->nref == nref) {
    901 			is_dirent = true;
    902 			break;
    903 		}
    904 	}
    905 
    906 	if (is_dirent && fd->vno) {
    907 		/* Living dirent. */
    908 		ret = chfs_gcollect_dirent(chmp, cheb, ip, fd);
    909 	} else if (is_dirent) {
    910 		/* Already deleted dirent. */
    911 		ret = chfs_gcollect_deletion_dirent(chmp, cheb, ip, fd);
    912 	} else {
    913 		dbg_gc("Nref at leb #%u offset 0x%08x wasn't in node list"
    914 		    " for ino #%llu\n",
    915 		    nref->nref_lnr, CHFS_GET_OFS(nref->nref_offset),
    916 		    (unsigned long long)ip->ino);
    917 		if (CHFS_REF_OBSOLETE(nref)) {
    918 			dbg_gc("But it's obsolete so we don't mind"
    919 			    " too much.\n");
    920 		}
    921 	}
    922 
    923 upnout:
    924 	return ret;
    925 }
    926 
    927 /* chfs_gcollect_vnode - collects a vnode information node */
    928 int
    929 chfs_gcollect_vnode(struct chfs_mount *chmp, struct chfs_inode *ip)
    930 {
    931 	int ret;
    932 	dbg_gc("gcollect_vnode\n");
    933 
    934 	/* Simply write the new vnode information to the flash
    935 	 * with GC's space allocation */
    936 	ret = chfs_write_flash_vnode(chmp, ip, ALLOC_GC);
    937 
    938 	return ret;
    939 }
    940 
    941 /* chfs_gcollect_dirent - collects a dirent */
    942 int
    943 chfs_gcollect_dirent(struct chfs_mount *chmp,
    944     struct chfs_eraseblock *cheb, struct chfs_inode *parent,
    945     struct chfs_dirent *fd)
    946 {
    947 	struct vnode *vnode = NULL;
    948 	struct chfs_inode *ip;
    949 	dbg_gc("gcollect_dirent\n");
    950 
    951 	/* Find vnode. */
    952 	vnode = chfs_vnode_lookup(chmp, fd->vno);
    953 
    954 	/* XXX maybe KASSERT or panic on this? */
    955 	if (vnode == NULL) {
    956 		return ENOENT;
    957 	}
    958 
    959 	ip = VTOI(vnode);
    960 
    961 	/* Remove and obsolete the previous version. */
    962 	mutex_enter(&chmp->chm_lock_vnocache);
    963 	chfs_remove_and_obsolete(chmp, parent->chvc, fd->nref,
    964 		&parent->chvc->dirents);
    965 	mutex_exit(&chmp->chm_lock_vnocache);
    966 
    967 	/* Write the new dirent to the flash. */
    968 	return chfs_write_flash_dirent(chmp,
    969 	    parent, ip, fd, fd->vno, ALLOC_GC);
    970 }
    971 
    972 /*
    973  * chfs_gcollect_deletion_dirent -
    974  * collects a dirent what was marked as deleted
    975  */
    976 int
    977 chfs_gcollect_deletion_dirent(struct chfs_mount *chmp,
    978     struct chfs_eraseblock *cheb, struct chfs_inode *parent,
    979     struct chfs_dirent *fd)
    980 {
    981 	struct chfs_flash_dirent_node chfdn;
    982 	struct chfs_node_ref *nref;
    983 	size_t retlen, name_len, nref_len;
    984 	uint32_t name_crc;
    985 
    986 	int ret;
    987 
    988 	dbg_gc("gcollect_deletion_dirent\n");
    989 
    990 	/* Check node. */
    991 	name_len = strlen(fd->name);
    992 	name_crc = crc32(0, fd->name, name_len);
    993 
    994 	nref_len = chfs_nref_len(chmp, cheb, fd->nref);
    995 
    996 	(void)chfs_vnode_lookup(chmp, fd->vno);
    997 
    998 	/* Find it in parent dirents. */
    999 	for (nref = parent->chvc->dirents;
   1000 	     nref != (void*)parent->chvc;
   1001 	     nref = nref->nref_next) {
   1002 
   1003 		if (!CHFS_REF_OBSOLETE(nref))
   1004 			continue;
   1005 
   1006 		/* if node refs have different length, skip */
   1007 		if (chfs_nref_len(chmp, NULL, nref) != nref_len)
   1008 			continue;
   1009 
   1010 		if (CHFS_GET_OFS(nref->nref_offset) ==
   1011 		    CHFS_GET_OFS(fd->nref->nref_offset)) {
   1012 			continue;
   1013 		}
   1014 
   1015 		/* read it from flash */
   1016 		ret = chfs_read_leb(chmp,
   1017 		    nref->nref_lnr, (void*)&chfdn, CHFS_GET_OFS(nref->nref_offset),
   1018 		    nref_len, &retlen);
   1019 
   1020 		if (ret) {
   1021 			dbg_gc("Read error: %d\n", ret);
   1022 			continue;
   1023 		}
   1024 
   1025 		if (retlen != nref_len) {
   1026 			dbg_gc("Error reading node:"
   1027 			    " read: %zu insted of: %zu\n", retlen, nref_len);
   1028 			continue;
   1029 		}
   1030 
   1031 		/* if node type doesn't match, skip */
   1032 		if (le16toh(chfdn.type) != CHFS_NODETYPE_DIRENT)
   1033 			continue;
   1034 
   1035 		/* if crc doesn't match, skip */
   1036 		if (le32toh(chfdn.name_crc) != name_crc)
   1037 			continue;
   1038 
   1039 		/* if length of name different, or this is an another deletion
   1040 		 * dirent, skip
   1041 		 */
   1042 		if (chfdn.nsize != name_len || !le64toh(chfdn.vno))
   1043 			continue;
   1044 
   1045 		/* check actual name */
   1046 		if (memcmp(chfdn.name, fd->name, name_len))
   1047 			continue;
   1048 
   1049 		mutex_enter(&chmp->chm_lock_vnocache);
   1050 		chfs_remove_and_obsolete(chmp, parent->chvc, fd->nref,
   1051 			&parent->chvc->dirents);
   1052 		mutex_exit(&chmp->chm_lock_vnocache);
   1053 		return chfs_write_flash_dirent(chmp,
   1054 		    parent, NULL, fd, fd->vno, ALLOC_GC);
   1055 	}
   1056 
   1057 	/* Simply remove it from the parent dirents. */
   1058 	TAILQ_REMOVE(&parent->dents, fd, fds);
   1059 	chfs_free_dirent(fd);
   1060 	return 0;
   1061 }
   1062 
   1063 /* chfs_gcollect_dnode - */
   1064 int
   1065 chfs_gcollect_dnode(struct chfs_mount *chmp,
   1066     struct chfs_eraseblock *orig_cheb, struct chfs_inode *ip,
   1067     struct chfs_full_dnode *fn, uint32_t orig_start, uint32_t orig_end)
   1068 {
   1069 	struct chfs_node_ref *nref;
   1070 	struct chfs_full_dnode *newfn;
   1071 	struct chfs_flash_data_node *fdnode;
   1072 	int ret = 0, retries = 0;
   1073 	uint32_t totlen;
   1074 	char *data = NULL;
   1075 	struct iovec vec;
   1076 	size_t retlen;
   1077 	dbg_gc("gcollect_dnode\n");
   1078 
   1079 	//TODO merge frags
   1080 
   1081 	KASSERT(orig_cheb->lnr == fn->nref->nref_lnr);
   1082 	totlen = chfs_nref_len(chmp, orig_cheb, fn->nref);
   1083 	data = kmem_alloc(totlen, KM_SLEEP);
   1084 
   1085 	/* Read the node from the flash. */
   1086 	ret = chfs_read_leb(chmp, fn->nref->nref_lnr, data, fn->nref->nref_offset,
   1087 	    totlen, &retlen);
   1088 
   1089 	fdnode = (struct chfs_flash_data_node *)data;
   1090 	fdnode->version = htole64(++ip->chvc->highest_version);
   1091 	fdnode->node_crc = htole32(crc32(0, (uint8_t *)fdnode,
   1092 		sizeof(*fdnode) - 4));
   1093 
   1094 	vec.iov_base = (void *)data;
   1095 	vec.iov_len = totlen;
   1096 
   1097 retry:
   1098 	/* Set the next block where we can write. */
   1099 	ret = chfs_reserve_space_gc(chmp, totlen);
   1100 	if (ret)
   1101 		goto out;
   1102 
   1103 	nref = chfs_alloc_node_ref(chmp->chm_nextblock);
   1104 	if (!nref) {
   1105 		ret = ENOMEM;
   1106 		goto out;
   1107 	}
   1108 
   1109 	mutex_enter(&chmp->chm_lock_sizes);
   1110 
   1111 	nref->nref_offset = chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
   1112 	KASSERT(nref->nref_offset % 4 == 0);
   1113 	chfs_change_size_free(chmp, chmp->chm_nextblock, -totlen);
   1114 
   1115 	/* Write it to the writebuffer. */
   1116 	ret = chfs_write_wbuf(chmp, &vec, 1, nref->nref_offset, &retlen);
   1117 	if (ret || retlen != totlen) {
   1118 		/* error during writing */
   1119 		chfs_err("error while writing out to the media\n");
   1120 		chfs_err("err: %d | size: %d | retlen : %zu\n",
   1121 		    ret, totlen, retlen);
   1122 		chfs_change_size_dirty(chmp, chmp->chm_nextblock, totlen);
   1123 		if (retries) {
   1124 			ret = EIO;
   1125 			mutex_exit(&chmp->chm_lock_sizes);
   1126 			goto out;
   1127 		}
   1128 
   1129 		/* try again */
   1130 		retries++;
   1131 		mutex_exit(&chmp->chm_lock_sizes);
   1132 		goto retry;
   1133 	}
   1134 
   1135 	dbg_gc("new nref lnr: %u - offset: %u\n", nref->nref_lnr, nref->nref_offset);
   1136 
   1137 	chfs_change_size_used(chmp, &chmp->chm_blocks[nref->nref_lnr], totlen);
   1138 	mutex_exit(&chmp->chm_lock_sizes);
   1139 	KASSERT(chmp->chm_blocks[nref->nref_lnr].used_size <= chmp->chm_ebh->eb_size);
   1140 
   1141 	/* Set fields of the new node. */
   1142 	newfn = chfs_alloc_full_dnode();
   1143 	newfn->nref = nref;
   1144 	newfn->ofs = fn->ofs;
   1145 	newfn->size = fn->size;
   1146 	newfn->frags = 0;
   1147 
   1148 	mutex_enter(&chmp->chm_lock_vnocache);
   1149 	/* Remove every part of the old node. */
   1150 	chfs_remove_frags_of_node(chmp, &ip->fragtree, fn->nref);
   1151 	chfs_remove_and_obsolete(chmp, ip->chvc, fn->nref, &ip->chvc->dnode);
   1152 
   1153 	/* Add the new nref to inode. */
   1154 	chfs_add_full_dnode_to_inode(chmp, ip, newfn);
   1155 	chfs_add_node_to_list(chmp,
   1156 	    ip->chvc, newfn->nref, &ip->chvc->dnode);
   1157 	mutex_exit(&chmp->chm_lock_vnocache);
   1158 
   1159 out:
   1160 	kmem_free(data, totlen);
   1161 	return ret;
   1162 }
   1163