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