Home | History | Annotate | Line # | Download | only in chfs
chfs_readinode.c revision 1.8.4.1
      1  1.8.4.1  msaitoh /*	$NetBSD: chfs_readinode.c,v 1.8.4.1 2014/09/08 18:57:58 msaitoh 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 David Tengeri <dtengeri (at) inf.u-szeged.hu>
      7      1.1    ahoka  * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
      8      1.1    ahoka  * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
      9      1.1    ahoka  * All rights reserved.
     10      1.1    ahoka  *
     11      1.1    ahoka  * This code is derived from software contributed to The NetBSD Foundation
     12      1.1    ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     13      1.1    ahoka  *
     14      1.1    ahoka  * Redistribution and use in source and binary forms, with or without
     15      1.1    ahoka  * modification, are permitted provided that the following conditions
     16      1.1    ahoka  * are met:
     17      1.1    ahoka  * 1. Redistributions of source code must retain the above copyright
     18      1.1    ahoka  *    notice, this list of conditions and the following disclaimer.
     19      1.1    ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     20      1.1    ahoka  *    notice, this list of conditions and the following disclaimer in the
     21      1.1    ahoka  *    documentation and/or other materials provided with the distribution.
     22      1.1    ahoka  *
     23      1.1    ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24      1.1    ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25      1.1    ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26      1.1    ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27      1.1    ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     28      1.1    ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     29      1.1    ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     30      1.1    ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     31      1.1    ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1    ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1    ahoka  * SUCH DAMAGE.
     34      1.1    ahoka  */
     35      1.1    ahoka 
     36      1.1    ahoka #include <sys/buf.h>
     37      1.1    ahoka 
     38      1.1    ahoka #include "chfs.h"
     39      1.1    ahoka 
     40      1.1    ahoka /* tmp node operations */
     41      1.1    ahoka int chfs_check_td_data(struct chfs_mount *,
     42      1.1    ahoka     struct chfs_tmp_dnode *);
     43      1.1    ahoka int chfs_check_td_node(struct chfs_mount *,
     44      1.1    ahoka     struct chfs_tmp_dnode *);
     45      1.1    ahoka struct chfs_node_ref *chfs_first_valid_data_ref(struct chfs_node_ref *);
     46      1.1    ahoka int chfs_add_tmp_dnode_to_tree(struct chfs_mount *,
     47      1.1    ahoka     struct chfs_readinode_info *,
     48      1.1    ahoka     struct chfs_tmp_dnode *);
     49      1.1    ahoka void chfs_add_tmp_dnode_to_tdi(struct chfs_tmp_dnode_info *,
     50      1.1    ahoka 	struct chfs_tmp_dnode *);
     51      1.1    ahoka void chfs_remove_tmp_dnode_from_tdi(struct chfs_tmp_dnode_info *,
     52      1.1    ahoka 	struct chfs_tmp_dnode *);
     53      1.1    ahoka static void chfs_kill_td(struct chfs_mount *,
     54      1.1    ahoka     struct chfs_tmp_dnode *);
     55      1.1    ahoka static void chfs_kill_tdi(struct chfs_mount *,
     56      1.1    ahoka     struct chfs_tmp_dnode_info *);
     57      1.1    ahoka /* frag node operations */
     58      1.1    ahoka struct chfs_node_frag *new_fragment(struct chfs_full_dnode *,
     59      1.1    ahoka     uint32_t,
     60      1.1    ahoka     uint32_t);
     61      1.1    ahoka int no_overlapping_node(struct rb_tree *, struct chfs_node_frag *,
     62      1.1    ahoka     struct chfs_node_frag *, uint32_t);
     63      1.1    ahoka int chfs_add_frag_to_fragtree(struct chfs_mount *,
     64      1.1    ahoka     struct rb_tree *,
     65      1.1    ahoka     struct chfs_node_frag *);
     66      1.1    ahoka void chfs_obsolete_node_frag(struct chfs_mount *,
     67      1.1    ahoka     struct chfs_node_frag *);
     68      1.1    ahoka /* general node operations */
     69      1.1    ahoka int chfs_get_data_nodes(struct chfs_mount *,
     70      1.1    ahoka     struct chfs_inode *,
     71      1.1    ahoka     struct chfs_readinode_info *);
     72      1.1    ahoka int chfs_build_fragtree(struct chfs_mount *,
     73      1.1    ahoka     struct chfs_inode *,
     74      1.1    ahoka     struct chfs_readinode_info *);
     75      1.1    ahoka 
     76      1.1    ahoka 
     77      1.1    ahoka 
     78      1.6    ttoth /* tmp node rbtree operations */
     79      1.1    ahoka static signed int
     80      1.1    ahoka tmp_node_compare_nodes(void *ctx, const void *n1, const void *n2)
     81      1.1    ahoka {
     82      1.1    ahoka 	const struct chfs_tmp_dnode_info *tdi1 = n1;
     83      1.1    ahoka 	const struct chfs_tmp_dnode_info *tdi2 = n2;
     84      1.1    ahoka 
     85      1.1    ahoka 	return (tdi1->tmpnode->node->ofs - tdi2->tmpnode->node->ofs);
     86      1.1    ahoka }
     87      1.1    ahoka 
     88      1.1    ahoka static signed int
     89      1.1    ahoka tmp_node_compare_key(void *ctx, const void *n, const void *key)
     90      1.1    ahoka {
     91      1.1    ahoka 	const struct chfs_tmp_dnode_info *tdi = n;
     92      1.1    ahoka 	uint64_t ofs =  *(const uint64_t *)key;
     93      1.1    ahoka 
     94      1.1    ahoka 	return (tdi->tmpnode->node->ofs - ofs);
     95      1.1    ahoka }
     96      1.1    ahoka 
     97      1.1    ahoka const rb_tree_ops_t tmp_node_rbtree_ops = {
     98      1.1    ahoka 	.rbto_compare_nodes = tmp_node_compare_nodes,
     99      1.1    ahoka 	.rbto_compare_key = tmp_node_compare_key,
    100      1.1    ahoka 	.rbto_node_offset = offsetof(struct chfs_tmp_dnode_info, rb_node),
    101      1.1    ahoka 	.rbto_context = NULL
    102      1.1    ahoka };
    103      1.1    ahoka 
    104      1.1    ahoka 
    105      1.6    ttoth /* frag node rbtree operations */
    106      1.1    ahoka static signed int
    107      1.1    ahoka frag_compare_nodes(void *ctx, const void *n1, const void *n2)
    108      1.1    ahoka {
    109      1.1    ahoka 	const struct chfs_node_frag *frag1 = n1;
    110      1.1    ahoka 	const struct chfs_node_frag *frag2 = n2;
    111      1.1    ahoka 
    112      1.1    ahoka 	return (frag1->ofs - frag2->ofs);
    113      1.1    ahoka }
    114      1.1    ahoka 
    115      1.1    ahoka static signed int
    116      1.1    ahoka frag_compare_key(void *ctx, const void *n, const void *key)
    117      1.1    ahoka {
    118      1.1    ahoka 	const struct chfs_node_frag *frag = n;
    119      1.1    ahoka 	uint64_t ofs = *(const uint64_t *)key;
    120      1.1    ahoka 
    121      1.1    ahoka 	return (frag->ofs - ofs);
    122      1.1    ahoka }
    123      1.1    ahoka 
    124      1.1    ahoka const rb_tree_ops_t frag_rbtree_ops = {
    125      1.1    ahoka 	.rbto_compare_nodes = frag_compare_nodes,
    126      1.1    ahoka 	.rbto_compare_key   = frag_compare_key,
    127      1.1    ahoka 	.rbto_node_offset = offsetof(struct chfs_node_frag, rb_node),
    128      1.1    ahoka 	.rbto_context = NULL
    129      1.1    ahoka };
    130      1.1    ahoka 
    131      1.1    ahoka 
    132      1.1    ahoka /*
    133      1.6    ttoth  * chfs_check_td_data - checks the data CRC of the node
    134      1.1    ahoka  *
    135      1.1    ahoka  * Returns: 0 - if everything OK;
    136      1.1    ahoka  * 	    	1 - if CRC is incorrect;
    137      1.1    ahoka  * 	    	2 - else;
    138      1.1    ahoka  *	    	error code if an error occured.
    139      1.1    ahoka  */
    140      1.1    ahoka int
    141      1.1    ahoka chfs_check_td_data(struct chfs_mount *chmp,
    142      1.1    ahoka     struct chfs_tmp_dnode *td)
    143      1.1    ahoka {
    144      1.1    ahoka 	int err;
    145      1.1    ahoka 	size_t retlen, len, totlen;
    146      1.1    ahoka 	uint32_t crc;
    147      1.1    ahoka 	uint64_t ofs;
    148      1.1    ahoka 	char *buf;
    149      1.1    ahoka 	struct chfs_node_ref *nref = td->node->nref;
    150      1.1    ahoka 
    151      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    152      1.1    ahoka 	KASSERT(!mutex_owned(&chmp->chm_lock_sizes));
    153      1.1    ahoka 
    154      1.1    ahoka 	ofs = CHFS_GET_OFS(nref->nref_offset) + sizeof(struct chfs_flash_data_node);
    155      1.1    ahoka 	len = td->node->size;
    156      1.1    ahoka 	if (!len)
    157      1.1    ahoka 		return 0;
    158      1.1    ahoka 
    159      1.6    ttoth 	/* Read data. */
    160      1.1    ahoka 	buf = kmem_alloc(len, KM_SLEEP);
    161      1.1    ahoka 	if (!buf) {
    162      1.1    ahoka 		dbg("allocating error\n");
    163      1.1    ahoka 		return 2;
    164      1.1    ahoka 	}
    165      1.1    ahoka 	err = chfs_read_leb(chmp, nref->nref_lnr, buf, ofs, len, &retlen);
    166      1.1    ahoka 	if (err) {
    167      1.7  mbalmer 		dbg("error while reading: %d\n", err);
    168      1.1    ahoka 		err = 2;
    169      1.1    ahoka 		goto out;
    170      1.1    ahoka 	}
    171      1.1    ahoka 
    172      1.6    ttoth 	/* Check crc. */
    173      1.1    ahoka 	if (len != retlen) {
    174      1.1    ahoka 		dbg("len:%zu, retlen:%zu\n", len, retlen);
    175      1.1    ahoka 		err = 2;
    176      1.1    ahoka 		goto out;
    177      1.1    ahoka 	}
    178      1.1    ahoka 	crc = crc32(0, (uint8_t *)buf, len);
    179      1.1    ahoka 
    180      1.1    ahoka 	if (crc != td->data_crc) {
    181      1.1    ahoka 		dbg("crc failed, calculated: 0x%x, orig: 0x%x\n", crc, td->data_crc);
    182      1.1    ahoka 		kmem_free(buf, len);
    183      1.1    ahoka 		return 1;
    184      1.1    ahoka 	}
    185      1.1    ahoka 
    186      1.6    ttoth 	/* Correct sizes. */
    187      1.3    ttoth 	CHFS_MARK_REF_NORMAL(nref);
    188      1.1    ahoka 	totlen = CHFS_PAD(sizeof(struct chfs_flash_data_node) + len);
    189      1.1    ahoka 
    190      1.1    ahoka 	mutex_enter(&chmp->chm_lock_sizes);
    191      1.1    ahoka 	chfs_change_size_unchecked(chmp, &chmp->chm_blocks[nref->nref_lnr], -totlen);
    192      1.1    ahoka 	chfs_change_size_used(chmp, &chmp->chm_blocks[nref->nref_lnr], totlen);
    193      1.1    ahoka 	mutex_exit(&chmp->chm_lock_sizes);
    194      1.1    ahoka 	KASSERT(chmp->chm_blocks[nref->nref_lnr].used_size <= chmp->chm_ebh->eb_size);
    195      1.1    ahoka 
    196      1.1    ahoka 	err = 0;
    197      1.1    ahoka out:
    198      1.1    ahoka 	kmem_free(buf, len);
    199      1.1    ahoka 	return err;
    200      1.1    ahoka }
    201      1.1    ahoka 
    202      1.6    ttoth /* chfs_check_td_node - checks a temporary node */
    203      1.1    ahoka int
    204      1.1    ahoka chfs_check_td_node(struct chfs_mount *chmp, struct chfs_tmp_dnode *td)
    205      1.1    ahoka {
    206      1.1    ahoka 	int ret;
    207      1.1    ahoka 
    208      1.1    ahoka 	if (CHFS_REF_FLAGS(td->node->nref) != CHFS_UNCHECKED_NODE_MASK)
    209      1.1    ahoka 		return 0;
    210      1.1    ahoka 
    211      1.1    ahoka 	ret = chfs_check_td_data(chmp, td);
    212      1.1    ahoka 	return ret;
    213      1.1    ahoka }
    214      1.1    ahoka 
    215      1.6    ttoth /*
    216      1.6    ttoth  * chfs_first_valid_data_ref -
    217      1.6    ttoth  * returns the first valid nref after the given nref
    218      1.6    ttoth  */
    219      1.1    ahoka struct chfs_node_ref *
    220      1.1    ahoka chfs_first_valid_data_ref(struct chfs_node_ref *nref)
    221      1.1    ahoka {
    222      1.1    ahoka 	while (nref) {
    223      1.1    ahoka 		if (!CHFS_REF_OBSOLETE(nref)) {
    224      1.1    ahoka #ifdef DGB_MSG_GC
    225      1.1    ahoka 			if (nref->nref_lnr == REF_EMPTY_NODE) {
    226      1.1    ahoka 				dbg("FIRST VALID IS EMPTY!\n");
    227      1.1    ahoka 			}
    228      1.1    ahoka #endif
    229      1.1    ahoka 			return nref;
    230      1.1    ahoka 		}
    231      1.1    ahoka 
    232      1.1    ahoka 		if (nref->nref_next) {
    233      1.1    ahoka 			nref = nref->nref_next;
    234      1.1    ahoka 		} else
    235      1.1    ahoka 			break;
    236      1.1    ahoka 	}
    237      1.1    ahoka 	return NULL;
    238      1.1    ahoka }
    239      1.1    ahoka 
    240      1.6    ttoth /*
    241      1.6    ttoth  * chfs_add_tmp_dnode_to_tdi -
    242      1.6    ttoth  * adds a temporary node to a temporary node descriptor
    243      1.6    ttoth  */
    244      1.1    ahoka void
    245      1.1    ahoka chfs_add_tmp_dnode_to_tdi(struct chfs_tmp_dnode_info *tdi,
    246      1.1    ahoka 	struct chfs_tmp_dnode *td)
    247      1.1    ahoka {
    248      1.1    ahoka 	if (!tdi->tmpnode) {
    249      1.6    ttoth 	/* The chain is empty. */
    250      1.1    ahoka 		tdi->tmpnode = td;
    251      1.1    ahoka 	} else {
    252      1.6    ttoth 	/* Insert into the chain. */
    253      1.1    ahoka 		struct chfs_tmp_dnode *tmp = tdi->tmpnode;
    254      1.1    ahoka 		while (tmp->next) {
    255      1.1    ahoka 			tmp = tmp->next;
    256      1.1    ahoka 		}
    257      1.1    ahoka 		tmp->next = td;
    258      1.1    ahoka 	}
    259      1.1    ahoka }
    260      1.1    ahoka 
    261      1.6    ttoth /*
    262      1.6    ttoth  * chfs_remove_tmp_dnode_from_tdi -
    263      1.6    ttoth  * removes a temporary node from its descriptor
    264      1.6    ttoth  */
    265      1.1    ahoka void
    266      1.1    ahoka chfs_remove_tmp_dnode_from_tdi(struct chfs_tmp_dnode_info *tdi,
    267      1.1    ahoka 	struct chfs_tmp_dnode *td)
    268      1.1    ahoka {
    269      1.1    ahoka 	if (tdi->tmpnode == td) {
    270      1.6    ttoth 	/* It's the first in the chain. */
    271      1.1    ahoka 		tdi->tmpnode = tdi->tmpnode->next;
    272      1.1    ahoka 	} else {
    273      1.6    ttoth 	/* Remove from the middle of the chain. */
    274      1.1    ahoka 		struct chfs_tmp_dnode *tmp = tdi->tmpnode->next;
    275      1.1    ahoka 		while (tmp->next && tmp->next != td) {
    276      1.1    ahoka 			tmp = tmp->next;
    277      1.1    ahoka 		}
    278      1.1    ahoka 		if (tmp->next) {
    279      1.1    ahoka 			tmp->next = td->next;
    280      1.1    ahoka 		}
    281      1.1    ahoka 	}
    282      1.1    ahoka }
    283      1.1    ahoka 
    284      1.6    ttoth /* chfs_kill_td - removes all components of a temporary node */
    285      1.1    ahoka static void
    286      1.1    ahoka chfs_kill_td(struct chfs_mount *chmp,
    287      1.1    ahoka     struct chfs_tmp_dnode *td)
    288      1.1    ahoka {
    289      1.3    ttoth 	struct chfs_vnode_cache *vc;
    290      1.3    ttoth 	if (td->node) {
    291      1.3    ttoth 		mutex_enter(&chmp->chm_lock_vnocache);
    292      1.6    ttoth 		/* Remove the node from the vnode cache's data node chain. */
    293      1.3    ttoth 		vc = chfs_nref_to_vc(td->node->nref);
    294      1.3    ttoth 		chfs_remove_and_obsolete(chmp, vc, td->node->nref, &vc->dnode);
    295      1.3    ttoth 		mutex_exit(&chmp->chm_lock_vnocache);
    296      1.1    ahoka 	}
    297      1.1    ahoka 
    298      1.1    ahoka 	chfs_free_tmp_dnode(td);
    299      1.1    ahoka }
    300      1.1    ahoka 
    301      1.6    ttoth /* chfs_kill_tdi - removes a temporary node descriptor */
    302      1.1    ahoka static void
    303      1.1    ahoka chfs_kill_tdi(struct chfs_mount *chmp,
    304      1.1    ahoka     struct chfs_tmp_dnode_info *tdi)
    305      1.1    ahoka {
    306      1.1    ahoka 	struct chfs_tmp_dnode *next, *tmp = tdi->tmpnode;
    307      1.1    ahoka 
    308      1.6    ttoth 	/* Iterate the chain and remove all temporary node from it. */
    309      1.1    ahoka 	while (tmp) {
    310      1.1    ahoka 		next = tmp->next;
    311      1.1    ahoka 		chfs_kill_td(chmp, tmp);
    312      1.1    ahoka 		tmp = next;
    313      1.1    ahoka 	}
    314      1.1    ahoka 
    315      1.1    ahoka 	chfs_free_tmp_dnode_info(tdi);
    316      1.1    ahoka }
    317      1.1    ahoka 
    318      1.6    ttoth /*
    319      1.6    ttoth  * chfs_add_tmp_dnode_to_tree -
    320      1.6    ttoth  * adds a temporary node to the temporary tree
    321      1.6    ttoth  */
    322      1.1    ahoka int
    323      1.1    ahoka chfs_add_tmp_dnode_to_tree(struct chfs_mount *chmp,
    324      1.1    ahoka     struct chfs_readinode_info *rii,
    325      1.1    ahoka     struct chfs_tmp_dnode *newtd)
    326      1.1    ahoka {
    327      1.1    ahoka 	uint64_t end_ofs = newtd->node->ofs + newtd->node->size;
    328      1.1    ahoka 	struct chfs_tmp_dnode_info *this;
    329      1.1    ahoka 	struct rb_node *node, *prev_node;
    330      1.1    ahoka 	struct chfs_tmp_dnode_info *newtdi;
    331      1.1    ahoka 
    332      1.1    ahoka 	node = rb_tree_find_node(&rii->tdi_root, &newtd->node->ofs);
    333      1.1    ahoka 	if (node) {
    334      1.1    ahoka 		this = (struct chfs_tmp_dnode_info *)node;
    335      1.1    ahoka 		while (this->tmpnode->overlapped) {
    336      1.1    ahoka 			prev_node = rb_tree_iterate(&rii->tdi_root, node, RB_DIR_LEFT);
    337      1.1    ahoka 			if (!prev_node) {
    338      1.1    ahoka 				this->tmpnode->overlapped = 0;
    339      1.1    ahoka 				break;
    340      1.1    ahoka 			}
    341      1.1    ahoka 			node = prev_node;
    342      1.1    ahoka 			this = (struct chfs_tmp_dnode_info *)node;
    343      1.1    ahoka 		}
    344      1.1    ahoka 	}
    345      1.6    ttoth 
    346      1.1    ahoka 	while (node) {
    347      1.1    ahoka 		this = (struct chfs_tmp_dnode_info *)node;
    348      1.1    ahoka 		if (this->tmpnode->node->ofs > end_ofs)
    349      1.1    ahoka 			break;
    350      1.1    ahoka 
    351      1.1    ahoka 		struct chfs_tmp_dnode *tmp_td = this->tmpnode;
    352      1.1    ahoka 		while (tmp_td) {
    353      1.1    ahoka 			if (tmp_td->version == newtd->version) {
    354      1.6    ttoth 				/* This is a new version of an old node. */
    355      1.1    ahoka 				if (!chfs_check_td_node(chmp, tmp_td)) {
    356      1.1    ahoka 					dbg("calling kill td 0\n");
    357      1.1    ahoka 					chfs_kill_td(chmp, newtd);
    358      1.1    ahoka 					return 0;
    359      1.1    ahoka 				} else {
    360      1.1    ahoka 					chfs_remove_tmp_dnode_from_tdi(this, tmp_td);
    361      1.1    ahoka 					chfs_kill_td(chmp, tmp_td);
    362      1.1    ahoka 					chfs_add_tmp_dnode_to_tdi(this, newtd);
    363      1.1    ahoka 					return 0;
    364      1.1    ahoka 				}
    365      1.1    ahoka 			}
    366      1.1    ahoka 			if (tmp_td->version < newtd->version &&
    367      1.1    ahoka 				tmp_td->node->ofs >= newtd->node->ofs &&
    368      1.1    ahoka 				tmp_td->node->ofs + tmp_td->node->size <= end_ofs) {
    369      1.1    ahoka 				/* New node entirely overlaps 'this' */
    370      1.1    ahoka 				if (chfs_check_td_node(chmp, newtd)) {
    371      1.1    ahoka 					dbg("calling kill td 2\n");
    372      1.1    ahoka 					chfs_kill_td(chmp, newtd);
    373      1.1    ahoka 					return 0;
    374      1.1    ahoka 				}
    375      1.1    ahoka 				/* ... and is good. Kill 'this' and any subsequent nodes which are also overlapped */
    376      1.1    ahoka 				while (tmp_td && tmp_td->node->ofs + tmp_td->node->size <= end_ofs) {
    377      1.1    ahoka 					struct rb_node *next = rb_tree_iterate(&rii->tdi_root, this, RB_DIR_RIGHT);
    378      1.1    ahoka 					struct chfs_tmp_dnode_info *next_tdi = (struct chfs_tmp_dnode_info *)next;
    379      1.1    ahoka 					struct chfs_tmp_dnode *next_td = NULL;
    380      1.1    ahoka 					if (tmp_td->next) {
    381      1.1    ahoka 						next_td = tmp_td->next;
    382      1.1    ahoka 					} else if (next_tdi) {
    383      1.1    ahoka 						next_td = next_tdi->tmpnode;
    384      1.1    ahoka 					}
    385      1.1    ahoka 					if (tmp_td->version < newtd->version) {
    386      1.1    ahoka 						chfs_remove_tmp_dnode_from_tdi(this, tmp_td);
    387      1.1    ahoka 						chfs_kill_td(chmp, tmp_td);
    388      1.1    ahoka 						if (!this->tmpnode) {
    389      1.1    ahoka 							rb_tree_remove_node(&rii->tdi_root, this);
    390      1.1    ahoka 							chfs_kill_tdi(chmp, this);
    391      1.1    ahoka 							this = next_tdi;
    392      1.1    ahoka 						}
    393      1.1    ahoka 					}
    394      1.1    ahoka 					tmp_td = next_td;
    395      1.1    ahoka 				}
    396      1.1    ahoka 				continue;
    397      1.1    ahoka 			}
    398      1.1    ahoka 			if (tmp_td->version > newtd->version &&
    399      1.1    ahoka 				tmp_td->node->ofs <= newtd->node->ofs &&
    400      1.1    ahoka 				tmp_td->node->ofs + tmp_td->node->size >= end_ofs) {
    401      1.1    ahoka 				/* New node entirely overlapped by 'this' */
    402      1.1    ahoka 				if (!chfs_check_td_node(chmp, tmp_td)) {
    403      1.2      agc 					dbg("this version: %llu\n",
    404      1.2      agc 						(unsigned long long)tmp_td->version);
    405      1.2      agc 					dbg("this ofs: %llu, size: %u\n",
    406      1.2      agc 						(unsigned long long)tmp_td->node->ofs,
    407      1.2      agc 						tmp_td->node->size);
    408      1.1    ahoka 					dbg("calling kill td 4\n");
    409      1.1    ahoka 					chfs_kill_td(chmp, newtd);
    410      1.1    ahoka 					return 0;
    411      1.1    ahoka 				}
    412      1.1    ahoka 				/* ... but 'this' was bad. Replace it... */
    413      1.1    ahoka 				chfs_remove_tmp_dnode_from_tdi(this, tmp_td);
    414      1.1    ahoka 				chfs_kill_td(chmp, tmp_td);
    415      1.1    ahoka 				if (!this->tmpnode) {
    416      1.1    ahoka 					rb_tree_remove_node(&rii->tdi_root, this);
    417      1.1    ahoka 					chfs_kill_tdi(chmp, this);
    418      1.1    ahoka 				}
    419      1.1    ahoka 				dbg("calling kill td 5\n");
    420      1.1    ahoka 				chfs_kill_td(chmp, newtd);
    421      1.1    ahoka 				break;
    422      1.1    ahoka 			}
    423      1.1    ahoka 			tmp_td = tmp_td->next;
    424      1.1    ahoka 		}
    425      1.1    ahoka 		node = rb_tree_iterate(&rii->tdi_root, node, RB_DIR_RIGHT);
    426      1.1    ahoka 	}
    427      1.1    ahoka 
    428      1.1    ahoka 	newtdi = chfs_alloc_tmp_dnode_info();
    429      1.1    ahoka 	chfs_add_tmp_dnode_to_tdi(newtdi, newtd);
    430      1.1    ahoka 	/* We neither completely obsoleted nor were completely
    431      1.1    ahoka 	   obsoleted by an earlier node. Insert into the tree */
    432      1.1    ahoka 	struct chfs_tmp_dnode_info *tmp_tdi = rb_tree_insert_node(&rii->tdi_root, newtdi);
    433      1.1    ahoka 	if (tmp_tdi != newtdi) {
    434      1.3    ttoth 		chfs_remove_tmp_dnode_from_tdi(newtdi, newtd);
    435      1.1    ahoka 		chfs_add_tmp_dnode_to_tdi(tmp_tdi, newtd);
    436      1.1    ahoka 		chfs_kill_tdi(chmp, newtdi);
    437      1.1    ahoka 	}
    438      1.1    ahoka 
    439      1.1    ahoka 	/* If there's anything behind that overlaps us, note it */
    440      1.1    ahoka 	node = rb_tree_iterate(&rii->tdi_root, node, RB_DIR_LEFT);
    441      1.1    ahoka 	if (node) {
    442      1.1    ahoka 		while (1) {
    443      1.1    ahoka 			this = (struct chfs_tmp_dnode_info *)node;
    444      1.1    ahoka 			if (this->tmpnode->node->ofs + this->tmpnode->node->size > newtd->node->ofs) {
    445      1.1    ahoka 				newtd->overlapped = 1;
    446      1.1    ahoka 			}
    447      1.1    ahoka 			if (!this->tmpnode->overlapped)
    448      1.1    ahoka 				break;
    449      1.1    ahoka 
    450      1.1    ahoka 			prev_node = rb_tree_iterate(&rii->tdi_root, node, RB_DIR_LEFT);
    451      1.1    ahoka 			if (!prev_node) {
    452      1.1    ahoka 				this->tmpnode->overlapped = 0;
    453      1.1    ahoka 				break;
    454      1.1    ahoka 			}
    455      1.1    ahoka 			node = prev_node;
    456      1.1    ahoka 		}
    457      1.1    ahoka 	}
    458      1.1    ahoka 
    459      1.1    ahoka 	/* If the new node overlaps anything ahead, note it */
    460      1.1    ahoka 	node = rb_tree_iterate(&rii->tdi_root, node, RB_DIR_RIGHT);
    461      1.1    ahoka 	this = (struct chfs_tmp_dnode_info *)node;
    462      1.1    ahoka 	while (this && this->tmpnode->node->ofs < end_ofs) {
    463      1.1    ahoka 		this->tmpnode->overlapped = 1;
    464      1.1    ahoka 		node = rb_tree_iterate(&rii->tdi_root, node, RB_DIR_RIGHT);
    465      1.1    ahoka 		this = (struct chfs_tmp_dnode_info *)node;
    466      1.1    ahoka 	}
    467      1.1    ahoka 	return 0;
    468      1.1    ahoka }
    469      1.1    ahoka 
    470      1.1    ahoka 
    471      1.6    ttoth /* new_fragment - creates a new fragment for a data node */
    472      1.1    ahoka struct chfs_node_frag *
    473      1.1    ahoka new_fragment(struct chfs_full_dnode *fdn, uint32_t ofs, uint32_t size)
    474      1.1    ahoka {
    475      1.1    ahoka 	struct chfs_node_frag *newfrag;
    476      1.1    ahoka 	newfrag = chfs_alloc_node_frag();
    477      1.1    ahoka 	if (newfrag) {
    478      1.6    ttoth 		/* Initialize fragment. */
    479      1.1    ahoka 		newfrag->ofs = ofs;
    480      1.1    ahoka 		newfrag->size = size;
    481      1.1    ahoka 		newfrag->node = fdn;
    482      1.3    ttoth 		if (newfrag->node) {
    483      1.3    ttoth 			newfrag->node->frags++;
    484      1.3    ttoth 		}
    485      1.1    ahoka 	} else {
    486      1.1    ahoka 		chfs_err("cannot allocate a chfs_node_frag object\n");
    487      1.1    ahoka 	}
    488      1.1    ahoka 	return newfrag;
    489      1.1    ahoka }
    490      1.1    ahoka 
    491      1.6    ttoth /*
    492      1.6    ttoth  * no_overlapping_node - inserts a node to the fragtree
    493      1.6    ttoth  * Puts hole frag into the holes between fragments.
    494      1.6    ttoth  */
    495      1.1    ahoka int
    496      1.1    ahoka no_overlapping_node(struct rb_tree *fragtree,
    497      1.1    ahoka     struct chfs_node_frag *newfrag,
    498      1.1    ahoka     struct chfs_node_frag *this, uint32_t lastend)
    499      1.1    ahoka {
    500      1.1    ahoka 	if (lastend < newfrag->node->ofs) {
    501      1.1    ahoka 		struct chfs_node_frag *holefrag;
    502      1.1    ahoka 
    503      1.1    ahoka 		holefrag = new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
    504      1.1    ahoka 		if (!holefrag) {
    505      1.1    ahoka 			chfs_free_node_frag(newfrag);
    506      1.1    ahoka 			return ENOMEM;
    507      1.1    ahoka 		}
    508      1.1    ahoka 
    509      1.1    ahoka 		rb_tree_insert_node(fragtree, holefrag);
    510      1.1    ahoka 	}
    511      1.1    ahoka 
    512      1.1    ahoka 	rb_tree_insert_node(fragtree, newfrag);
    513      1.1    ahoka 
    514      1.1    ahoka 	return 0;
    515      1.1    ahoka }
    516      1.1    ahoka 
    517      1.6    ttoth /*
    518      1.6    ttoth  * chfs_add_frag_to_fragtree -
    519      1.6    ttoth  * adds a fragment to a data node's fragtree
    520      1.6    ttoth  */
    521      1.1    ahoka int
    522      1.1    ahoka chfs_add_frag_to_fragtree(struct chfs_mount *chmp,
    523      1.1    ahoka     struct rb_tree *fragtree,
    524      1.1    ahoka     struct chfs_node_frag *newfrag)
    525      1.1    ahoka {
    526      1.1    ahoka 	struct chfs_node_frag *this;
    527      1.1    ahoka 	uint32_t lastend;
    528      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    529      1.1    ahoka 
    530      1.6    ttoth 	/* Find the offset of frag which is before the new one. */
    531      1.1    ahoka 	this = (struct chfs_node_frag *)rb_tree_find_node_leq(fragtree, &newfrag->ofs);
    532      1.1    ahoka 
    533      1.1    ahoka 	if (this) {
    534      1.1    ahoka 		lastend = this->ofs + this->size;
    535      1.1    ahoka 	} else {
    536      1.1    ahoka 		lastend = 0;
    537      1.1    ahoka 	}
    538      1.1    ahoka 
    539      1.6    ttoth 	/* New fragment is end of the file and there is no overlapping. */
    540      1.1    ahoka 	if (lastend <= newfrag->ofs) {
    541      1.1    ahoka 		if (lastend && (lastend - 1) >> PAGE_SHIFT == newfrag->ofs >> PAGE_SHIFT) {
    542      1.1    ahoka 			if (this->node)
    543      1.1    ahoka 				CHFS_MARK_REF_NORMAL(this->node->nref);
    544      1.1    ahoka 			CHFS_MARK_REF_NORMAL(newfrag->node->nref);
    545      1.1    ahoka 		}
    546      1.1    ahoka 		return no_overlapping_node(fragtree, newfrag, this, lastend);
    547      1.1    ahoka 	}
    548      1.1    ahoka 
    549      1.1    ahoka 	if (newfrag->ofs > this->ofs) {
    550      1.1    ahoka 		CHFS_MARK_REF_NORMAL(newfrag->node->nref);
    551      1.1    ahoka 		if (this->node)
    552      1.1    ahoka 			CHFS_MARK_REF_NORMAL(this->node->nref);
    553      1.1    ahoka 
    554      1.1    ahoka 		if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
    555      1.6    ttoth 			/* Newfrag is inside of this. */
    556      1.1    ahoka 			struct chfs_node_frag *newfrag2;
    557      1.1    ahoka 
    558      1.1    ahoka 			newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
    559      1.1    ahoka 			    this->ofs + this->size - newfrag->ofs - newfrag->size);
    560      1.1    ahoka 			if (!newfrag2)
    561      1.1    ahoka 				return ENOMEM;
    562      1.1    ahoka 
    563      1.1    ahoka 			this->size = newfrag->ofs - this->ofs;
    564      1.1    ahoka 
    565      1.1    ahoka 			rb_tree_insert_node(fragtree, newfrag);
    566      1.1    ahoka 			rb_tree_insert_node(fragtree, newfrag2);
    567      1.1    ahoka 
    568      1.1    ahoka 			return 0;
    569      1.1    ahoka 		}
    570      1.6    ttoth 		/* Newfrag is bottom of this. */
    571      1.1    ahoka 		this->size = newfrag->ofs - this->ofs;
    572      1.1    ahoka 		rb_tree_insert_node(fragtree, newfrag);
    573      1.1    ahoka 	} else {
    574      1.6    ttoth 		/* Newfrag start at same point */
    575      1.1    ahoka 		//TODO replace instead of remove and insert
    576      1.1    ahoka 		rb_tree_remove_node(fragtree, this);
    577      1.1    ahoka 		rb_tree_insert_node(fragtree, newfrag);
    578      1.1    ahoka 
    579      1.1    ahoka 		if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
    580      1.1    ahoka 			chfs_obsolete_node_frag(chmp, this);
    581      1.1    ahoka 		} else {
    582      1.1    ahoka 			this->ofs += newfrag->size;
    583      1.1    ahoka 			this->size -= newfrag->size;
    584      1.1    ahoka 
    585      1.1    ahoka 			rb_tree_insert_node(fragtree, this);
    586      1.1    ahoka 			return 0;
    587      1.1    ahoka 		}
    588      1.1    ahoka 	}
    589      1.1    ahoka 	/* OK, now we have newfrag added in the correct place in the tree, but
    590      1.1    ahoka 	   frag_next(newfrag) may be a fragment which is overlapped by it
    591      1.1    ahoka 	*/
    592      1.1    ahoka 	while ((this = frag_next(fragtree, newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
    593      1.1    ahoka 		rb_tree_remove_node(fragtree, this);
    594      1.1    ahoka 		chfs_obsolete_node_frag(chmp, this);
    595      1.1    ahoka 	}
    596      1.1    ahoka 
    597      1.1    ahoka 	if (!this || newfrag->ofs + newfrag->size == this->ofs)
    598      1.1    ahoka 		return 0;
    599      1.1    ahoka 
    600      1.1    ahoka 	this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
    601      1.1    ahoka 	this->ofs = newfrag->ofs + newfrag->size;
    602      1.1    ahoka 
    603      1.1    ahoka 	if (this->node)
    604      1.1    ahoka 		CHFS_MARK_REF_NORMAL(this->node->nref);
    605      1.1    ahoka 	CHFS_MARK_REF_NORMAL(newfrag->node->nref);
    606      1.1    ahoka 
    607      1.1    ahoka 	return 0;
    608      1.1    ahoka }
    609      1.1    ahoka 
    610      1.6    ttoth /*
    611      1.6    ttoth  * chfs_remove_frags_of_node -
    612      1.6    ttoth  * removes all fragments from a fragtree and DOESN'T OBSOLETE them
    613      1.6    ttoth  */
    614      1.1    ahoka void
    615      1.3    ttoth chfs_remove_frags_of_node(struct chfs_mount *chmp, struct rb_tree *fragtree,
    616      1.3    ttoth 	struct chfs_node_ref *nref)
    617      1.3    ttoth {
    618      1.3    ttoth 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    619      1.3    ttoth 	struct chfs_node_frag *this, *next;
    620      1.3    ttoth 
    621      1.5    ttoth 	if (nref == NULL) {
    622      1.5    ttoth 		return;
    623      1.5    ttoth 	}
    624      1.5    ttoth 
    625      1.6    ttoth 	/* Iterate the tree and clean all elements. */
    626      1.3    ttoth 	this = (struct chfs_node_frag *)RB_TREE_MIN(fragtree);
    627      1.3    ttoth 	while (this) {
    628      1.3    ttoth 		next = frag_next(fragtree, this);
    629      1.3    ttoth 		if (this->node->nref == nref) {
    630      1.3    ttoth 			rb_tree_remove_node(fragtree, this);
    631      1.4    ttoth 			chfs_free_node_frag(this);
    632      1.3    ttoth 		}
    633      1.3    ttoth 		this = next;
    634      1.3    ttoth 	}
    635      1.3    ttoth }
    636      1.3    ttoth 
    637      1.6    ttoth /*
    638      1.6    ttoth  * chfs_kill_fragtree -
    639      1.6    ttoth  * removes all fragments from a fragtree and OBSOLETES them
    640      1.6    ttoth  */
    641      1.3    ttoth void
    642      1.3    ttoth chfs_kill_fragtree(struct chfs_mount *chmp, struct rb_tree *fragtree)
    643      1.1    ahoka {
    644      1.3    ttoth 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    645      1.1    ahoka 	struct chfs_node_frag *this, *next;
    646      1.1    ahoka 
    647      1.6    ttoth 	/* Iterate the tree and clean all elements. */
    648      1.1    ahoka 	this = (struct chfs_node_frag *)RB_TREE_MIN(fragtree);
    649      1.1    ahoka 	while (this) {
    650      1.1    ahoka 		next = frag_next(fragtree, this);
    651      1.1    ahoka 		rb_tree_remove_node(fragtree, this);
    652      1.3    ttoth 		chfs_obsolete_node_frag(chmp, this);
    653      1.1    ahoka 		this = next;
    654      1.1    ahoka 	}
    655      1.1    ahoka }
    656      1.1    ahoka 
    657      1.6    ttoth /* chfs_truncate_fragtree - truncates the tree to a specified size */
    658      1.1    ahoka uint32_t
    659      1.1    ahoka chfs_truncate_fragtree(struct chfs_mount *chmp,
    660      1.1    ahoka 	struct rb_tree *fragtree, uint32_t size)
    661      1.1    ahoka {
    662      1.3    ttoth 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    663      1.1    ahoka 	struct chfs_node_frag *frag;
    664      1.1    ahoka 
    665      1.1    ahoka 	dbg("truncate to size: %u\n", size);
    666      1.1    ahoka 
    667      1.1    ahoka 	frag = (struct chfs_node_frag *)rb_tree_find_node_leq(fragtree, &size);
    668      1.1    ahoka 
    669      1.1    ahoka 	/* Find the last frag before size and set its new size. */
    670      1.1    ahoka 	if (frag && frag->ofs != size) {
    671      1.1    ahoka 		if (frag->ofs + frag->size > size) {
    672      1.1    ahoka 			frag->size = size - frag->ofs;
    673      1.1    ahoka 		}
    674      1.1    ahoka 		frag = frag_next(fragtree, frag);
    675      1.1    ahoka 	}
    676      1.1    ahoka 
    677      1.1    ahoka 	/* Delete frags after new size. */
    678      1.1    ahoka 	while (frag && frag->ofs >= size) {
    679      1.1    ahoka 		struct chfs_node_frag *next = frag_next(fragtree, frag);
    680      1.1    ahoka 
    681      1.1    ahoka 		rb_tree_remove_node(fragtree, frag);
    682      1.1    ahoka 		chfs_obsolete_node_frag(chmp, frag);
    683      1.1    ahoka 		frag = next;
    684      1.1    ahoka 	}
    685      1.1    ahoka 
    686      1.1    ahoka 	if (size == 0) {
    687      1.1    ahoka 		return 0;
    688      1.1    ahoka 	}
    689      1.1    ahoka 
    690      1.1    ahoka 	frag = frag_last(fragtree);
    691      1.1    ahoka 
    692      1.1    ahoka 	if (!frag) {
    693      1.1    ahoka 		return 0;
    694      1.1    ahoka 	}
    695      1.1    ahoka 
    696      1.1    ahoka 	if (frag->ofs + frag->size < size) {
    697      1.1    ahoka 		return frag->ofs + frag->size;
    698      1.1    ahoka 	}
    699      1.1    ahoka 
    700      1.1    ahoka 	/* FIXME Should we check the postion of the last node? (PAGE_CACHE size, etc.) */
    701      1.1    ahoka 	if (frag->node && (frag->ofs & (PAGE_SIZE - 1)) == 0) {
    702      1.3    ttoth 		frag->node->nref->nref_offset =
    703      1.3    ttoth 			CHFS_GET_OFS(frag->node->nref->nref_offset) | CHFS_PRISTINE_NODE_MASK;
    704      1.1    ahoka 	}
    705      1.1    ahoka 
    706      1.1    ahoka 	return size;
    707      1.1    ahoka }
    708      1.1    ahoka 
    709      1.6    ttoth /* chfs_obsolete_node_frag - obsoletes a fragment of a node */
    710      1.1    ahoka void
    711      1.1    ahoka chfs_obsolete_node_frag(struct chfs_mount *chmp,
    712      1.1    ahoka     struct chfs_node_frag *this)
    713      1.1    ahoka {
    714      1.3    ttoth 	struct chfs_vnode_cache *vc;
    715      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    716      1.1    ahoka 	if (this->node) {
    717      1.6    ttoth 	/* The fragment is in a node. */
    718      1.3    ttoth 		KASSERT(this->node->frags != 0);
    719      1.1    ahoka 		this->node->frags--;
    720      1.3    ttoth 		if (this->node->frags == 0) {
    721      1.6    ttoth 		/* This is the last fragment. (There is no more.) */
    722      1.3    ttoth 			KASSERT(!CHFS_REF_OBSOLETE(this->node->nref));
    723      1.3    ttoth 			mutex_enter(&chmp->chm_lock_vnocache);
    724      1.3    ttoth 			vc = chfs_nref_to_vc(this->node->nref);
    725      1.3    ttoth 			dbg("[MARK] lnr: %u ofs: %u\n", this->node->nref->nref_lnr,
    726      1.3    ttoth 				this->node->nref->nref_offset);
    727      1.3    ttoth 
    728      1.3    ttoth 			chfs_remove_and_obsolete(chmp, vc, this->node->nref, &vc->dnode);
    729      1.3    ttoth 			mutex_exit(&chmp->chm_lock_vnocache);
    730      1.3    ttoth 
    731      1.1    ahoka 			chfs_free_full_dnode(this->node);
    732      1.1    ahoka 		} else {
    733      1.6    ttoth 		/* There is more frags in the node. */
    734      1.1    ahoka 			CHFS_MARK_REF_NORMAL(this->node->nref);
    735      1.1    ahoka 		}
    736      1.1    ahoka 	}
    737      1.1    ahoka 	chfs_free_node_frag(this);
    738      1.1    ahoka }
    739      1.1    ahoka 
    740      1.6    ttoth /* chfs_add_full_dnode_to_inode - adds a data node to an inode */
    741      1.1    ahoka int
    742      1.1    ahoka chfs_add_full_dnode_to_inode(struct chfs_mount *chmp,
    743      1.1    ahoka     struct chfs_inode *ip,
    744      1.1    ahoka     struct chfs_full_dnode *fd)
    745      1.1    ahoka {
    746      1.1    ahoka 	int ret;
    747      1.1    ahoka 	struct chfs_node_frag *newfrag;
    748      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    749      1.1    ahoka 
    750      1.1    ahoka 	if (unlikely(!fd->size))
    751      1.1    ahoka 		return 0;
    752      1.1    ahoka 
    753      1.6    ttoth 	/* Create a new fragment from the data node and add it to the fragtree. */
    754      1.1    ahoka 	newfrag = new_fragment(fd, fd->ofs, fd->size);
    755      1.1    ahoka 	if (unlikely(!newfrag))
    756      1.1    ahoka 		return ENOMEM;
    757      1.1    ahoka 
    758      1.1    ahoka 	ret = chfs_add_frag_to_fragtree(chmp, &ip->fragtree, newfrag);
    759      1.1    ahoka 	if (ret)
    760      1.1    ahoka 		return ret;
    761      1.1    ahoka 
    762      1.6    ttoth 	/* Check previous fragment. */
    763      1.1    ahoka 	if (newfrag->ofs & (PAGE_SIZE - 1)) {
    764      1.1    ahoka 		struct chfs_node_frag *prev = frag_prev(&ip->fragtree, newfrag);
    765      1.1    ahoka 
    766      1.1    ahoka 		CHFS_MARK_REF_NORMAL(fd->nref);
    767      1.1    ahoka 		if (prev->node)
    768      1.1    ahoka 			CHFS_MARK_REF_NORMAL(prev->node->nref);
    769      1.1    ahoka 	}
    770      1.1    ahoka 
    771      1.6    ttoth 	/* Check next fragment. */
    772      1.1    ahoka 	if ((newfrag->ofs+newfrag->size) & (PAGE_SIZE - 1)) {
    773      1.1    ahoka 		struct chfs_node_frag *next = frag_next(&ip->fragtree, newfrag);
    774      1.1    ahoka 
    775      1.1    ahoka 		if (next) {
    776      1.1    ahoka 			CHFS_MARK_REF_NORMAL(fd->nref);
    777      1.1    ahoka 			if (next->node)
    778      1.1    ahoka 				CHFS_MARK_REF_NORMAL(next->node->nref);
    779      1.1    ahoka 		}
    780      1.1    ahoka 	}
    781      1.1    ahoka 
    782      1.1    ahoka 	return 0;
    783      1.1    ahoka }
    784      1.1    ahoka 
    785      1.1    ahoka 
    786      1.6    ttoth /* chfs_get_data_nodes - get temporary nodes of an inode */
    787      1.1    ahoka int
    788      1.1    ahoka chfs_get_data_nodes(struct chfs_mount *chmp,
    789      1.1    ahoka     struct chfs_inode *ip,
    790      1.1    ahoka     struct chfs_readinode_info *rii)
    791      1.1    ahoka {
    792      1.1    ahoka 	uint32_t crc;
    793      1.1    ahoka 	int err;
    794      1.1    ahoka 	size_t len, retlen;
    795      1.1    ahoka 	struct chfs_node_ref *nref;
    796      1.1    ahoka 	struct chfs_flash_data_node *dnode;
    797      1.1    ahoka 	struct chfs_tmp_dnode *td;
    798      1.1    ahoka 	char* buf;
    799      1.1    ahoka 
    800      1.1    ahoka 	len = sizeof(struct chfs_flash_data_node);
    801      1.1    ahoka 	buf = kmem_alloc(len, KM_SLEEP);
    802      1.1    ahoka 
    803      1.1    ahoka 	dnode = kmem_alloc(len, KM_SLEEP);
    804  1.8.4.1  msaitoh 	if (!dnode) {
    805  1.8.4.1  msaitoh 		kmem_free(buf, len);
    806      1.1    ahoka 		return ENOMEM;
    807  1.8.4.1  msaitoh 	}
    808      1.1    ahoka 
    809      1.1    ahoka 	nref = chfs_first_valid_data_ref(ip->chvc->dnode);
    810      1.1    ahoka 
    811      1.6    ttoth 	/* Update highest version. */
    812      1.1    ahoka 	rii->highest_version = ip->chvc->highest_version;
    813      1.1    ahoka 
    814      1.1    ahoka 	while(nref && (struct chfs_vnode_cache *)nref != ip->chvc) {
    815      1.1    ahoka 		err = chfs_read_leb(chmp, nref->nref_lnr, buf, CHFS_GET_OFS(nref->nref_offset), len, &retlen);
    816      1.1    ahoka 		if (err || len != retlen)
    817      1.1    ahoka 			goto out;
    818      1.1    ahoka 		dnode = (struct chfs_flash_data_node*)buf;
    819      1.1    ahoka 
    820      1.6    ttoth 		/* Check header crc. */
    821      1.1    ahoka 		crc = crc32(0, (uint8_t *)dnode, CHFS_NODE_HDR_SIZE - 4);
    822      1.1    ahoka 		if (crc != le32toh(dnode->hdr_crc)) {
    823      1.1    ahoka 			chfs_err("CRC check failed. calc: 0x%x orig: 0x%x\n", crc, le32toh(dnode->hdr_crc));
    824      1.1    ahoka 			goto cont;
    825      1.1    ahoka 		}
    826      1.6    ttoth 
    827      1.6    ttoth 		/* Check header magic bitmask. */
    828      1.1    ahoka 		if (le16toh(dnode->magic) != CHFS_FS_MAGIC_BITMASK) {
    829      1.1    ahoka 			chfs_err("Wrong magic bitmask.\n");
    830      1.1    ahoka 			goto cont;
    831      1.1    ahoka 		}
    832      1.6    ttoth 
    833      1.6    ttoth 		/* Check node crc. */
    834      1.1    ahoka 		crc = crc32(0, (uint8_t *)dnode, sizeof(*dnode) - 4);
    835      1.1    ahoka 		if (crc != le32toh(dnode->node_crc)) {
    836      1.1    ahoka 			chfs_err("Node CRC check failed. calc: 0x%x orig: 0x%x\n", crc, le32toh(dnode->node_crc));
    837      1.1    ahoka 			goto cont;
    838      1.1    ahoka 		}
    839      1.6    ttoth 
    840      1.1    ahoka 		td = chfs_alloc_tmp_dnode();
    841      1.1    ahoka 		if (!td) {
    842      1.1    ahoka 			chfs_err("Can't allocate tmp dnode info.\n");
    843      1.1    ahoka 			err = ENOMEM;
    844      1.1    ahoka 			goto out;
    845      1.1    ahoka 		}
    846      1.6    ttoth 
    847      1.1    ahoka 		/* We don't check data crc here, just add nodes to tmp frag tree, because
    848      1.1    ahoka 		 * we don't want to check nodes which have been overlapped by a new node
    849      1.1    ahoka 		 * with a higher version number.
    850      1.1    ahoka 		 */
    851      1.1    ahoka 		td->node = chfs_alloc_full_dnode();
    852      1.1    ahoka 		if (!td->node) {
    853      1.1    ahoka 			chfs_err("Can't allocate full dnode info.\n");
    854      1.1    ahoka 			err = ENOMEM;
    855      1.1    ahoka 			goto out_tmp_dnode;
    856      1.1    ahoka 		}
    857      1.1    ahoka 		td->version = le64toh(dnode->version);
    858      1.1    ahoka 		td->node->ofs = le64toh(dnode->offset);
    859      1.1    ahoka 		td->data_crc = le32toh(dnode->data_crc);
    860      1.1    ahoka 		td->node->nref = nref;
    861      1.1    ahoka 		td->node->size = le32toh(dnode->data_length);
    862      1.3    ttoth 		td->node->frags = 1;
    863      1.1    ahoka 		td->overlapped = 0;
    864      1.1    ahoka 
    865      1.1    ahoka 		if (td->version > rii->highest_version) {
    866      1.1    ahoka 			rii->highest_version = td->version;
    867      1.1    ahoka 		}
    868      1.1    ahoka 
    869      1.6    ttoth 		/* Add node to the tree. */
    870      1.1    ahoka 		err = chfs_add_tmp_dnode_to_tree(chmp, rii, td);
    871      1.1    ahoka 		if (err)
    872      1.1    ahoka 			goto out_full_dnode;
    873      1.1    ahoka 
    874      1.1    ahoka cont:
    875      1.1    ahoka 		nref = chfs_first_valid_data_ref(nref->nref_next);
    876      1.1    ahoka 	}
    877      1.1    ahoka 
    878      1.1    ahoka 	ip->chvc->highest_version = rii->highest_version;
    879      1.1    ahoka 	return 0;
    880      1.1    ahoka 
    881      1.1    ahoka out_full_dnode:
    882      1.1    ahoka 	chfs_free_full_dnode(td->node);
    883      1.1    ahoka out_tmp_dnode:
    884      1.1    ahoka 	chfs_free_tmp_dnode(td);
    885      1.1    ahoka out:
    886      1.1    ahoka 	kmem_free(buf, len);
    887      1.1    ahoka 	kmem_free(dnode, len);
    888      1.1    ahoka 	return err;
    889      1.1    ahoka }
    890      1.1    ahoka 
    891      1.1    ahoka 
    892      1.6    ttoth /* chfs_build_fragtree - builds fragtree from temporary tree */
    893      1.1    ahoka int
    894      1.1    ahoka chfs_build_fragtree(struct chfs_mount *chmp, struct chfs_inode *ip,
    895      1.1    ahoka     struct chfs_readinode_info *rii)
    896      1.1    ahoka {
    897      1.1    ahoka 	struct chfs_tmp_dnode_info *pen, *last, *this;
    898      1.6    ttoth 	struct rb_tree ver_tree;    /* version tree, used only temporary */
    899      1.1    ahoka 	uint64_t high_ver = 0;
    900      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
    901      1.1    ahoka 
    902      1.1    ahoka 	rb_tree_init(&ver_tree, &tmp_node_rbtree_ops);
    903      1.1    ahoka 
    904      1.6    ttoth 	/* Update highest version and latest node reference. */
    905      1.1    ahoka 	if (rii->mdata_tn) {
    906      1.1    ahoka 		high_ver = rii->mdata_tn->tmpnode->version;
    907      1.1    ahoka 		rii->latest_ref = rii->mdata_tn->tmpnode->node->nref;
    908      1.1    ahoka 	}
    909      1.1    ahoka 
    910      1.6    ttoth 	/* Iterate the temporary tree in reverse order. */
    911      1.1    ahoka 	pen = (struct chfs_tmp_dnode_info *)RB_TREE_MAX(&rii->tdi_root);
    912      1.1    ahoka 
    913      1.1    ahoka 	while((last = pen)) {
    914      1.1    ahoka 		pen = (struct chfs_tmp_dnode_info *)rb_tree_iterate(&rii->tdi_root, last, RB_DIR_LEFT);
    915      1.1    ahoka 
    916      1.6    ttoth 		/* We build here a version tree from overlapped nodes. */
    917      1.1    ahoka 		rb_tree_remove_node(&rii->tdi_root, last);
    918      1.1    ahoka 		rb_tree_insert_node(&ver_tree, last);
    919      1.1    ahoka 
    920      1.1    ahoka 		if (last->tmpnode->overlapped) {
    921      1.1    ahoka 			if (pen)
    922      1.1    ahoka 				continue;
    923      1.1    ahoka 
    924      1.1    ahoka 			last->tmpnode->overlapped = 0;
    925      1.1    ahoka 		}
    926      1.1    ahoka 
    927      1.1    ahoka 		this = (struct chfs_tmp_dnode_info *)RB_TREE_MAX(&ver_tree);
    928      1.1    ahoka 
    929      1.6    ttoth 		/* Start to build the fragtree. */
    930      1.1    ahoka 		while (this) {
    931      1.1    ahoka 			struct chfs_tmp_dnode_info *vers_next;
    932      1.1    ahoka 			int ret;
    933      1.1    ahoka 
    934      1.1    ahoka 			vers_next = (struct chfs_tmp_dnode_info *)rb_tree_iterate(&ver_tree, this, RB_DIR_LEFT);
    935      1.1    ahoka 			rb_tree_remove_node(&ver_tree, this);
    936      1.1    ahoka 
    937      1.1    ahoka 			struct chfs_tmp_dnode *tmp_td = this->tmpnode;
    938      1.1    ahoka 			while (tmp_td) {
    939      1.1    ahoka 				struct chfs_tmp_dnode *next_td = tmp_td->next;
    940      1.1    ahoka 
    941      1.6    ttoth 				/* Check temporary node. */
    942      1.1    ahoka 				if (chfs_check_td_node(chmp, tmp_td)) {
    943      1.1    ahoka 					if (next_td) {
    944      1.1    ahoka 						chfs_remove_tmp_dnode_from_tdi(this, tmp_td);
    945      1.3    ttoth 						chfs_kill_td(chmp, tmp_td);
    946      1.1    ahoka 					} else {
    947      1.1    ahoka 						break;
    948      1.1    ahoka 					}
    949      1.1    ahoka 				} else {
    950      1.1    ahoka 					if (tmp_td->version > high_ver) {
    951      1.1    ahoka 						high_ver = tmp_td->version;
    952      1.2      agc 						dbg("highver: %llu\n", (unsigned long long)high_ver);
    953      1.1    ahoka 						rii->latest_ref = tmp_td->node->nref;
    954      1.1    ahoka 					}
    955      1.1    ahoka 
    956      1.6    ttoth 					/* Add node to inode and its fragtree. */
    957      1.1    ahoka 					ret = chfs_add_full_dnode_to_inode(chmp, ip, tmp_td->node);
    958      1.1    ahoka 					if (ret) {
    959      1.6    ttoth 						/* On error, clean the whole version tree. */
    960      1.1    ahoka 						while (1) {
    961      1.1    ahoka 							vers_next = (struct chfs_tmp_dnode_info *)rb_tree_iterate(&ver_tree, this, RB_DIR_LEFT);
    962      1.1    ahoka 							while (tmp_td) {
    963      1.1    ahoka 								next_td = tmp_td->next;
    964      1.3    ttoth 
    965      1.1    ahoka 								chfs_free_full_dnode(tmp_td->node);
    966      1.1    ahoka 								chfs_remove_tmp_dnode_from_tdi(this, tmp_td);
    967      1.3    ttoth 								chfs_kill_td(chmp, tmp_td);
    968      1.1    ahoka 								tmp_td = next_td;
    969      1.1    ahoka 							}
    970      1.1    ahoka 							chfs_free_tmp_dnode_info(this);
    971      1.1    ahoka 							this = vers_next;
    972      1.1    ahoka 							if (!this)
    973      1.1    ahoka 								break;
    974      1.1    ahoka 							rb_tree_remove_node(&ver_tree, vers_next);
    975      1.3    ttoth 							chfs_kill_tdi(chmp, vers_next);
    976      1.1    ahoka 						}
    977      1.1    ahoka 						return ret;
    978      1.1    ahoka 					}
    979      1.1    ahoka 
    980      1.6    ttoth 					/* Remove temporary node from temporary descriptor.
    981      1.6    ttoth 					 * Shouldn't obsolete tmp_td here, because tmp_td->node
    982      1.6    ttoth 					 * was added to the inode. */
    983      1.1    ahoka 					chfs_remove_tmp_dnode_from_tdi(this, tmp_td);
    984      1.1    ahoka 					chfs_free_tmp_dnode(tmp_td);
    985      1.1    ahoka 				}
    986      1.1    ahoka 				tmp_td = next_td;
    987      1.1    ahoka 			}
    988      1.6    ttoth 			/* Continue with the previous element of version tree. */
    989      1.1    ahoka 			chfs_kill_tdi(chmp, this);
    990      1.1    ahoka 			this = vers_next;
    991      1.1    ahoka 		}
    992      1.1    ahoka 	}
    993      1.1    ahoka 
    994      1.1    ahoka 	return 0;
    995      1.1    ahoka }
    996      1.1    ahoka 
    997      1.6    ttoth /* chfs_read_inode - checks the state of the inode then reads and builds it */
    998      1.1    ahoka int chfs_read_inode(struct chfs_mount *chmp, struct chfs_inode *ip)
    999      1.1    ahoka {
   1000      1.1    ahoka 	struct chfs_vnode_cache *vc = ip->chvc;
   1001      1.1    ahoka 
   1002      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
   1003      1.1    ahoka 
   1004      1.1    ahoka retry:
   1005      1.3    ttoth 	mutex_enter(&chmp->chm_lock_vnocache);
   1006      1.1    ahoka 	switch (vc->state) {
   1007      1.6    ttoth 		case VNO_STATE_UNCHECKED:
   1008      1.6    ttoth 			/* FALLTHROUGH */
   1009      1.6    ttoth 		case VNO_STATE_CHECKEDABSENT:
   1010      1.6    ttoth 			vc->state = VNO_STATE_READING;
   1011      1.6    ttoth 			break;
   1012      1.6    ttoth 		case VNO_STATE_CHECKING:
   1013      1.6    ttoth 			/* FALLTHROUGH */
   1014      1.6    ttoth 		case VNO_STATE_GC:
   1015      1.6    ttoth 			mutex_exit(&chmp->chm_lock_vnocache);
   1016      1.6    ttoth 			goto retry;
   1017      1.6    ttoth 			break;
   1018      1.6    ttoth 		case VNO_STATE_PRESENT:
   1019      1.6    ttoth 			/* FALLTHROUGH */
   1020      1.6    ttoth 		case VNO_STATE_READING:
   1021      1.6    ttoth 			chfs_err("Reading inode #%llu in state %d!\n",
   1022      1.6    ttoth 				(unsigned long long)vc->vno, vc->state);
   1023      1.6    ttoth 			chfs_err("wants to read a nonexistent ino %llu\n",
   1024      1.6    ttoth 				(unsigned long long)vc->vno);
   1025      1.6    ttoth 			return ENOENT;
   1026      1.6    ttoth 		default:
   1027      1.6    ttoth 			panic("BUG() Bad vno cache state.");
   1028      1.1    ahoka 	}
   1029      1.3    ttoth 	mutex_exit(&chmp->chm_lock_vnocache);
   1030      1.1    ahoka 
   1031      1.1    ahoka 	return chfs_read_inode_internal(chmp, ip);
   1032      1.1    ahoka }
   1033      1.1    ahoka 
   1034      1.1    ahoka /*
   1035      1.6    ttoth  * chfs_read_inode_internal - reads and builds an inode
   1036      1.6    ttoth  * Firstly get temporary nodes then build fragtree.
   1037      1.1    ahoka  */
   1038      1.1    ahoka int
   1039      1.1    ahoka chfs_read_inode_internal(struct chfs_mount *chmp, struct chfs_inode *ip)
   1040      1.1    ahoka {
   1041      1.1    ahoka 	int err;
   1042      1.1    ahoka 	size_t len, retlen;
   1043      1.1    ahoka 	char* buf;
   1044      1.1    ahoka 	struct chfs_readinode_info rii;
   1045      1.1    ahoka 	struct chfs_flash_vnode *fvnode;
   1046      1.1    ahoka 
   1047      1.1    ahoka 	KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
   1048      1.1    ahoka 
   1049      1.1    ahoka 	len = sizeof(*fvnode);
   1050      1.1    ahoka 
   1051      1.1    ahoka 	memset(&rii, 0, sizeof(rii));
   1052      1.1    ahoka 
   1053      1.1    ahoka 	rb_tree_init(&rii.tdi_root, &tmp_node_rbtree_ops);
   1054      1.1    ahoka 
   1055      1.6    ttoth 	/* Build a temporary node tree. */
   1056      1.1    ahoka 	err = chfs_get_data_nodes(chmp, ip, &rii);
   1057      1.1    ahoka 	if (err) {
   1058      1.1    ahoka 		if (ip->chvc->state == VNO_STATE_READING)
   1059      1.1    ahoka 			ip->chvc->state = VNO_STATE_CHECKEDABSENT;
   1060      1.1    ahoka 		/* FIXME Should we kill fragtree or something here? */
   1061      1.1    ahoka 		return err;
   1062      1.1    ahoka 	}
   1063      1.1    ahoka 
   1064      1.6    ttoth 	/* Build fragtree from temp nodes. */
   1065      1.1    ahoka 	rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
   1066      1.6    ttoth 
   1067      1.1    ahoka 	err = chfs_build_fragtree(chmp, ip, &rii);
   1068      1.1    ahoka 	if (err) {
   1069      1.1    ahoka 		if (ip->chvc->state == VNO_STATE_READING)
   1070      1.1    ahoka 			ip->chvc->state = VNO_STATE_CHECKEDABSENT;
   1071      1.1    ahoka 		/* FIXME Should we kill fragtree or something here? */
   1072      1.1    ahoka 		return err;
   1073      1.1    ahoka 	}
   1074      1.1    ahoka 
   1075      1.1    ahoka 	if (!rii.latest_ref) {
   1076      1.1    ahoka 		return 0;
   1077      1.1    ahoka 	}
   1078      1.1    ahoka 
   1079      1.1    ahoka 	buf = kmem_alloc(len, KM_SLEEP);
   1080      1.1    ahoka 	if (!buf)
   1081      1.1    ahoka 		return ENOMEM;
   1082      1.1    ahoka 
   1083      1.6    ttoth 	/* Set inode size from its vnode information node. */
   1084      1.1    ahoka 	err = chfs_read_leb(chmp, ip->chvc->v->nref_lnr, buf, CHFS_GET_OFS(ip->chvc->v->nref_offset), len, &retlen);
   1085      1.1    ahoka 	if (err || retlen != len) {
   1086      1.1    ahoka 		kmem_free(buf, len);
   1087      1.1    ahoka 		return err?err:EIO;
   1088      1.1    ahoka 	}
   1089      1.1    ahoka 
   1090      1.1    ahoka 	fvnode = (struct chfs_flash_vnode*)buf;
   1091      1.1    ahoka 
   1092      1.1    ahoka 	dbg("set size from v: %u\n", fvnode->dn_size);
   1093      1.1    ahoka 	chfs_set_vnode_size(ITOV(ip), fvnode->dn_size);
   1094      1.1    ahoka 	uint32_t retsize = chfs_truncate_fragtree(chmp, &ip->fragtree, fvnode->dn_size);
   1095      1.1    ahoka 	if (retsize != fvnode->dn_size) {
   1096      1.1    ahoka 		dbg("Truncating failed. It is %u instead of %u\n", retsize, fvnode->dn_size);
   1097      1.1    ahoka 	}
   1098      1.1    ahoka 
   1099      1.1    ahoka 	kmem_free(buf, len);
   1100      1.1    ahoka 
   1101      1.1    ahoka 	if (ip->chvc->state == VNO_STATE_READING) {
   1102      1.1    ahoka 		ip->chvc->state = VNO_STATE_PRESENT;
   1103      1.1    ahoka 	}
   1104      1.1    ahoka 
   1105      1.1    ahoka 	return 0;
   1106      1.1    ahoka }
   1107      1.1    ahoka 
   1108      1.6    ttoth /* chfs_read_data - reads and checks data of a file */
   1109      1.1    ahoka int
   1110      1.1    ahoka chfs_read_data(struct chfs_mount* chmp, struct vnode *vp,
   1111      1.1    ahoka     struct buf *bp)
   1112      1.1    ahoka {
   1113      1.1    ahoka 	off_t ofs;
   1114      1.1    ahoka 	struct chfs_node_frag *frag;
   1115      1.1    ahoka 	char * buf;
   1116      1.1    ahoka 	int err = 0;
   1117      1.1    ahoka 	size_t size, retlen;
   1118      1.1    ahoka 	uint32_t crc;
   1119      1.1    ahoka 	struct chfs_inode *ip = VTOI(vp);
   1120      1.1    ahoka 	struct chfs_flash_data_node *dnode;
   1121      1.1    ahoka 	struct chfs_node_ref *nref;
   1122      1.1    ahoka 
   1123      1.1    ahoka 	memset(bp->b_data, 0, bp->b_bcount);
   1124      1.1    ahoka 
   1125      1.6    ttoth 	/* Calculate the size of the file from its fragtree. */
   1126      1.1    ahoka 	ofs = bp->b_blkno * PAGE_SIZE;
   1127      1.1    ahoka 	frag = (struct chfs_node_frag *)rb_tree_find_node_leq(&ip->fragtree, &ofs);
   1128      1.1    ahoka 
   1129      1.1    ahoka 	if (!frag || frag->ofs > ofs || frag->ofs + frag->size <= ofs) {
   1130      1.5    ttoth 		bp->b_resid = 0;
   1131      1.1    ahoka 		dbg("not found in frag tree\n");
   1132      1.1    ahoka 		return 0;
   1133      1.1    ahoka 	}
   1134      1.1    ahoka 
   1135      1.1    ahoka 	if (!frag->node) {
   1136      1.1    ahoka 		dbg("no node in frag\n");
   1137      1.1    ahoka 		return 0;
   1138      1.1    ahoka 	}
   1139      1.1    ahoka 
   1140      1.1    ahoka 	nref = frag->node->nref;
   1141      1.1    ahoka 	size = sizeof(*dnode) + frag->size;
   1142      1.1    ahoka 
   1143      1.1    ahoka 	buf = kmem_alloc(size, KM_SLEEP);
   1144      1.1    ahoka 
   1145      1.6    ttoth 	/* Read node from flash. */
   1146      1.1    ahoka 	dbg("reading from lnr: %u, offset: %u, size: %zu\n", nref->nref_lnr, CHFS_GET_OFS(nref->nref_offset), size);
   1147      1.1    ahoka 	err = chfs_read_leb(chmp, nref->nref_lnr, buf, CHFS_GET_OFS(nref->nref_offset), size, &retlen);
   1148      1.1    ahoka 	if (err) {
   1149      1.1    ahoka 		chfs_err("error after reading: %d\n", err);
   1150      1.1    ahoka 		goto out;
   1151      1.1    ahoka 	}
   1152      1.1    ahoka 	if (retlen != size) {
   1153      1.1    ahoka 		chfs_err("retlen: %zu != size: %zu\n", retlen, size);
   1154      1.1    ahoka 		err = EIO;
   1155      1.1    ahoka 		goto out;
   1156      1.1    ahoka 	}
   1157      1.1    ahoka 
   1158      1.6    ttoth 	/* Read data from flash. */
   1159      1.1    ahoka 	dnode = (struct chfs_flash_data_node *)buf;
   1160      1.1    ahoka 	crc = crc32(0, (uint8_t *)dnode, CHFS_NODE_HDR_SIZE - 4);
   1161      1.1    ahoka 	if (crc != le32toh(dnode->hdr_crc)) {
   1162      1.1    ahoka 		chfs_err("CRC check failed. calc: 0x%x orig: 0x%x\n", crc, le32toh(dnode->hdr_crc));
   1163      1.1    ahoka 		err = EIO;
   1164      1.1    ahoka 		goto out;
   1165      1.1    ahoka 	}
   1166      1.6    ttoth 
   1167      1.6    ttoth 	/* Check header magic bitmask. */
   1168      1.1    ahoka 	if (le16toh(dnode->magic) != CHFS_FS_MAGIC_BITMASK) {
   1169      1.1    ahoka 		chfs_err("Wrong magic bitmask.\n");
   1170      1.1    ahoka 		err = EIO;
   1171      1.1    ahoka 		goto out;
   1172      1.1    ahoka 	}
   1173      1.6    ttoth 
   1174      1.6    ttoth 	/* Check crc of node. */
   1175      1.1    ahoka 	crc = crc32(0, (uint8_t *)dnode, sizeof(*dnode) - 4);
   1176      1.1    ahoka 	if (crc != le32toh(dnode->node_crc)) {
   1177      1.1    ahoka 		chfs_err("Node CRC check failed. calc: 0x%x orig: 0x%x\n", crc, le32toh(dnode->node_crc));
   1178      1.1    ahoka 		err = EIO;
   1179      1.1    ahoka 		goto out;
   1180      1.1    ahoka 	}
   1181      1.6    ttoth 
   1182      1.6    ttoth 	/* Check crc of data. */
   1183      1.1    ahoka 	crc = crc32(0, (uint8_t *)dnode->data, dnode->data_length);
   1184      1.1    ahoka 	if (crc != le32toh(dnode->data_crc)) {
   1185      1.1    ahoka 		chfs_err("Data CRC check failed. calc: 0x%x orig: 0x%x\n", crc, le32toh(dnode->data_crc));
   1186      1.1    ahoka 		err = EIO;
   1187      1.1    ahoka 		goto out;
   1188      1.1    ahoka 	}
   1189      1.1    ahoka 
   1190      1.1    ahoka 	memcpy(bp->b_data, dnode->data, dnode->data_length);
   1191      1.1    ahoka 	bp->b_resid = 0;
   1192      1.1    ahoka 
   1193      1.1    ahoka out:
   1194      1.1    ahoka 	kmem_free(buf, size);
   1195      1.1    ahoka 	return err;
   1196      1.1    ahoka }
   1197