⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stree.c

📁 ARM 嵌入式 系统 设计与实例开发 实验教材 二源码
💻 C
📖 第 1 页 / 共 5 页
字号:
	if (!is_tree_node (p_s_bh, expected_level)) {	    reiserfs_warning ("vs-5150: search_by_key: "			      "invalid format found in block %ld. Fsck?\n", 			      p_s_bh->b_blocknr);	    pathrelse (p_s_search_path);	    return IO_ERROR;	}		/* ok, we have acquired next formatted node in the tree */	n_node_level = B_LEVEL (p_s_bh);	PROC_INFO_BH_STAT( p_s_sb, p_s_bh, n_node_level - 1 );	RFALSE( n_node_level < n_stop_level,		"vs-5152: tree level (%d) is less than stop level (%d)",		n_node_level, n_stop_level);	n_retval = bin_search( p_s_key, B_N_PITEM_HEAD(p_s_bh, 0),                B_NR_ITEMS(p_s_bh),                ( n_node_level == DISK_LEAF_NODE_LEVEL ) ? IH_SIZE : KEY_SIZE,                &(p_s_last_element->pe_position));	if (n_node_level == n_stop_level) {	    return n_retval;	}	/* we are not in the stop level */	if (n_retval == ITEM_FOUND)	    /* item has been found, so we choose the pointer which is to the right of the found one */	    p_s_last_element->pe_position++;	/* if item was not found we choose the position which is to	   the left of the found item. This requires no code,	   bin_search did it already.*/	/* So we have chosen a position in the current node which is	   an internal node.  Now we calculate child block number by	   position in the node. */	n_block_number = B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position);#ifdef SEARCH_BY_KEY_READA	/* if we are going to read leaf node, then calculate its right neighbor if possible */	if (n_node_level == DISK_LEAF_NODE_LEVEL + 1 && p_s_last_element->pe_position < B_NR_ITEMS (p_s_bh))	    right_neighbor_of_leaf_node = B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position + 1);#endif    }}/* Form the path to an item and position in this item which contains   file byte defined by p_s_key. If there is no such item   corresponding to the key, we point the path to the item with   maximal key less than p_s_key, and *p_n_pos_in_item is set to one   past the last entry/byte in the item.  If searching for entry in a   directory item, and it is not found, *p_n_pos_in_item is set to one   entry more than the entry with maximal key which is less than the   sought key.   Note that if there is no entry in this same node which is one more,   then we point to an imaginary entry.  for direct items, the   position is in units of bytes, for indirect items the position is   in units of blocknr entries, for directory items the position is in   units of directory entries.  *//* The function is NOT SCHEDULE-SAFE! */int search_for_position_by_key (struct super_block  * p_s_sb,         /* Pointer to the super block.          */				const struct cpu_key  * p_cpu_key,      /* Key to search (cpu variable)         */				struct path         * p_s_search_path /* Filled up by this function.          */    ) {    struct item_head    * p_le_ih; /* pointer to on-disk structure */    int                   n_blk_size;    loff_t item_offset, offset;    struct reiserfs_dir_entry de;    int retval;    /* If searching for directory entry. */    if ( is_direntry_cpu_key (p_cpu_key) )	return  search_by_entry_key (p_s_sb, p_cpu_key, p_s_search_path, &de);    /* If not searching for directory entry. */        /* If item is found. */    retval = search_item (p_s_sb, p_cpu_key, p_s_search_path);    if (retval == IO_ERROR)	return retval;    if ( retval == ITEM_FOUND )  {	RFALSE( ! ih_item_len(                B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_search_path),			       PATH_LAST_POSITION(p_s_search_path))),	        "PAP-5165: item length equals zero");	pos_in_item(p_s_search_path) = 0;	return POSITION_FOUND;    }    RFALSE( ! PATH_LAST_POSITION(p_s_search_path),	    "PAP-5170: position equals zero");    /* Item is not found. Set path to the previous item. */    p_le_ih = B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_search_path), --PATH_LAST_POSITION(p_s_search_path));    n_blk_size = p_s_sb->s_blocksize;    if (comp_short_keys (&(p_le_ih->ih_key), p_cpu_key)) {	return FILE_NOT_FOUND;    }    // FIXME: quite ugly this far    item_offset = le_ih_k_offset (p_le_ih);    offset = cpu_key_k_offset (p_cpu_key);    /* Needed byte is contained in the item pointed to by the path.*/    if (item_offset <= offset &&	item_offset + op_bytes_number (p_le_ih, n_blk_size) > offset) {	pos_in_item (p_s_search_path) = offset - item_offset;	if ( is_indirect_le_ih(p_le_ih) ) {	    pos_in_item (p_s_search_path) /= n_blk_size;	}	return POSITION_FOUND;    }    /* Needed byte is not contained in the item pointed to by the     path. Set pos_in_item out of the item. */    if ( is_indirect_le_ih (p_le_ih) )	pos_in_item (p_s_search_path) = ih_item_len(p_le_ih) / UNFM_P_SIZE;    else        pos_in_item (p_s_search_path) = ih_item_len( p_le_ih );      return POSITION_NOT_FOUND;}/* Compare given item and item pointed to by the path. */int comp_items (const struct item_head * stored_ih, const struct path * p_s_path){    struct buffer_head  * p_s_bh;    struct item_head    * ih;    /* Last buffer at the path is not in the tree. */    if ( ! B_IS_IN_TREE(p_s_bh = PATH_PLAST_BUFFER(p_s_path)) )	return 1;    /* Last path position is invalid. */    if ( PATH_LAST_POSITION(p_s_path) >= B_NR_ITEMS(p_s_bh) )	return 1;    /* we need only to know, whether it is the same item */    ih = get_ih (p_s_path);    return memcmp (stored_ih, ih, IH_SIZE);}/* unformatted nodes are not logged anymore, ever.  This is safe** now*/#define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)// block can not be forgotten as it is in I/O or held by someone#define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))// prepare for delete or cut of direct itemstatic inline int prepare_for_direct_item (struct path * path,					   struct item_head * le_ih,					   struct inode * inode,					   loff_t new_file_length,					   int * cut_size){    loff_t round_len;    if ( new_file_length == max_reiserfs_offset (inode) ) {	/* item has to be deleted */	*cut_size = -(IH_SIZE + ih_item_len(le_ih));	return M_DELETE;    }	    // new file gets truncated    if (get_inode_item_key_version (inode) == KEY_FORMAT_3_6) {	// 	round_len = ROUND_UP (new_file_length); 	/* this was n_new_file_length < le_ih ... */	if ( round_len < le_ih_k_offset (le_ih) )  {	    *cut_size = -(IH_SIZE + ih_item_len(le_ih));	    return M_DELETE; /* Delete this item. */	}	/* Calculate first position and size for cutting from item. */	pos_in_item (path) = round_len - (le_ih_k_offset (le_ih) - 1);	*cut_size = -(ih_item_len(le_ih) - pos_in_item(path));		return M_CUT; /* Cut from this item. */    }    // old file: items may have any length    if ( new_file_length < le_ih_k_offset (le_ih) )  {	*cut_size = -(IH_SIZE + ih_item_len(le_ih));	return M_DELETE; /* Delete this item. */    }    /* Calculate first position and size for cutting from item. */    *cut_size = -(ih_item_len(le_ih) -		      (pos_in_item (path) = new_file_length + 1 - le_ih_k_offset (le_ih)));    return M_CUT; /* Cut from this item. */}static inline int prepare_for_direntry_item (struct path * path,					     struct item_head * le_ih,					     struct inode * inode,					     loff_t new_file_length,					     int * cut_size){    if (le_ih_k_offset (le_ih) == DOT_OFFSET && 	new_file_length == max_reiserfs_offset (inode)) {	RFALSE( ih_entry_count (le_ih) != 2,	        "PAP-5220: incorrect empty directory item (%h)", le_ih);	*cut_size = -(IH_SIZE + ih_item_len(le_ih));	return M_DELETE; /* Delete the directory item containing "." and ".." entry. */    }        if ( ih_entry_count (le_ih) == 1 )  {	/* Delete the directory item such as there is one record only	   in this item*/	*cut_size = -(IH_SIZE + ih_item_len(le_ih));	return M_DELETE;    }        /* Cut one record from the directory item. */    *cut_size = -(DEH_SIZE + entry_length (get_last_bh (path), le_ih, pos_in_item (path)));    return M_CUT; }/*  If the path points to a directory or direct item, calculate mode and the size cut, for balance.    If the path points to an indirect item, remove some number of its unformatted nodes.    In case of file truncate calculate whether this item must be deleted/truncated or last    unformatted node of this item will be converted to a direct item.    This function returns a determination of what balance mode the calling function should employ. */static char  prepare_for_delete_or_cut(				       struct reiserfs_transaction_handle *th, 				       struct inode * inode,				       struct path         * p_s_path,				       const struct cpu_key      * p_s_item_key,				       int                 * p_n_removed,      /* Number of unformatted nodes which were removed										  from end of the file. */				       int                 * p_n_cut_size,				       unsigned long long    n_new_file_length /* MAX_KEY_OFFSET in case of delete. */    ) {    struct super_block  * p_s_sb = inode->i_sb;    struct item_head    * p_le_ih = PATH_PITEM_HEAD(p_s_path);    struct buffer_head  * p_s_bh = PATH_PLAST_BUFFER(p_s_path);    /* Stat_data item. */    if ( is_statdata_le_ih (p_le_ih) ) {	RFALSE( n_new_file_length != max_reiserfs_offset (inode),		"PAP-5210: mode must be M_DELETE");	*p_n_cut_size = -(IH_SIZE + ih_item_len(p_le_ih));	return M_DELETE;    }    /* Directory item. */    if ( is_direntry_le_ih (p_le_ih) )	return prepare_for_direntry_item (p_s_path, p_le_ih, inode, n_new_file_length, p_n_cut_size);    /* Direct item. */    if ( is_direct_le_ih (p_le_ih) )	return prepare_for_direct_item (p_s_path, p_le_ih, inode, n_new_file_length, p_n_cut_size);    /* Case of an indirect item. */    {	int                   n_unfm_number,    /* Number of the item unformatted nodes. */	    n_counter,	    n_blk_size;	__u32               * p_n_unfm_pointer; /* Pointer to the unformatted node number. */	__u32 tmp;	struct item_head      s_ih;           /* Item header. */	char                  c_mode;           /* Returned mode of the balance. */	int need_research;	n_blk_size = p_s_sb->s_blocksize;	/* Search for the needed object indirect item until there are no unformatted nodes to be removed. */	do  {	    need_research = 0;            p_s_bh = PATH_PLAST_BUFFER(p_s_path);	    /* Copy indirect item header to a temp variable. */	    copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));	    /* Calculate number of unformatted nodes in this item. */	    n_unfm_number = I_UNFM_NUM(&s_ih);	    RFALSE( ! is_indirect_le_ih(&s_ih) || ! n_unfm_number ||		    pos_in_item (p_s_path) + 1 !=  n_unfm_number,		    "PAP-5240: illegal item %h "		    "n_unfm_number = %d *p_n_pos_in_item = %d", 		    &s_ih, n_unfm_number, pos_in_item (p_s_path));	    /* Calculate balance mode and position in the item to remove unformatted nodes. */	    if ( n_new_file_length == max_reiserfs_offset (inode) ) {/* Case of delete. */		pos_in_item (p_s_path) = 0;		*p_n_cut_size = -(IH_SIZE + ih_item_len(&s_ih));		c_mode = M_DELETE;	    }	    else  { /* Case of truncate. */		if ( n_new_file_length < le_ih_k_offset (&s_ih) )  {		    pos_in_item (p_s_path) = 0;		    *p_n_cut_size = -(IH_SIZE + ih_item_len(&s_ih));		    c_mode = M_DELETE; /* Delete this item. */		}		else  {		    /* indirect item must be truncated starting from *p_n_pos_in_item-th position */		    pos_in_item (p_s_path) = (n_new_file_length + n_blk_size - le_ih_k_offset (&s_ih) ) >> p_s_sb->s_blocksize_bits;		    RFALSE( pos_in_item (p_s_path) > n_unfm_number,			    "PAP-5250: illegal position in the item");		    /* Either convert last unformatted node of indirect item to direct item or increase		       its free space.  */		    if ( pos_in_item (p_s_path) == n_unfm_number )  {			*p_n_cut_size = 0; /* Nothing to cut. */			return M_CONVERT; /* Maybe convert last unformatted node to the direct item. */		    }		    /* Calculate size to cut. */		    *p_n_cut_size = -(ih_item_len(&s_ih) - pos_in_item(p_s_path) * UNFM_P_SIZE);		    c_mode = M_CUT;     /* Cut from this indirect item. */		}	    }	    RFALSE( n_unfm_number <= pos_in_item (p_s_path),		    "PAP-5260: illegal position in the indirect item");	    /* pointers to be cut */	    n_unfm_number -= pos_in_item (p_s_path);	    /* Set pointer to the last unformatted node pointer that is to be cut. */	    p_n_unfm_pointer = (__u32 *)B_I_PITEM(p_s_bh, &s_ih) + I_UNFM_NUM(&s_ih) - 1 - *p_n_removed;	    /* We go through the unformatted nodes pointers of the indirect	       item and look for the unformatted nodes in the cache. If we	       found some of them we free it, zero corresponding indirect item	       entry and log buffer containing that indirect item. For this we	       need to prepare last path element for logging. If some	       unformatted node has b_count > 1 we must not free this	       unformatted node since it is in use. */	    reiserfs_prepare_for_journal(p_s_sb, p_s_bh, 1);	    // note: path could be changed, first line in for loop takes care	    // of it	    for (n_counter = *p_n_removed;		 n_counter < n_unfm_number; n_counter++, p_n_unfm_pointer-- ) {		if (item_moved (&s_ih, p_s_path)) {		    need_research = 1 ;		    break;		}		RFALSE( p_n_unfm_pointer < (__u32 *)B_I_PITEM(p_s_bh, &s_ih) ||			p_n_unfm_pointer > (__u32 *)B_I_PITEM(p_s_bh, &s_ih) + I_UNFM_NUM(&s_ih) - 1,			"vs-5265: pointer out of range");		/* Hole, nothing to remove. */		if ( ! get_block_num(p_n_unfm_pointer,0) )  { 			(*p_n_removed)++;			continue;		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -