1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
5 * Functions to add/delete new nodes to/from the tree.
7 * Functions from this file are used by carry (see carry*) to handle:
9 * . insertion of new formatted node into tree
11 * . addition of new tree root, increasing tree height
13 * . removing tree root, decreasing tree height
22 #include "plugin/plugin.h"
26 #include "block_alloc.h"
27 #include "tree_walk.h"
31 #include <linux/err.h>
33 static int add_child_ptr(znode
* parent
, znode
* child
);
34 /* warning only issued if error is not -E_REPEAT */
35 #define ewarning( error, ... ) \
36 if( ( error ) != -E_REPEAT ) \
37 warning( __VA_ARGS__ )
39 /* allocate new node on the @level and immediately on the right of @brother. */
40 znode
* reiser4_new_node(znode
* brother
/* existing left neighbor
42 tree_level level
/* tree level at which new node is to
47 reiser4_block_nr blocknr
;
49 assert("nikita-930", brother
!= NULL
);
50 assert("umka-264", level
< REAL_MAX_ZTREE_HEIGHT
);
52 retcode
= assign_fake_blocknr_formatted(&blocknr
);
55 zget(znode_get_tree(brother
), &blocknr
, NULL
, level
,
56 reiser4_ctx_gfp_mask_get());
58 ewarning(PTR_ERR(result
), "nikita-929",
59 "Cannot allocate znode for carry: %li",
63 /* cheap test, can be executed even when debugging is off */
64 if (!znode_just_created(result
)) {
65 warning("nikita-2213",
66 "Allocated already existing block: %llu",
67 (unsigned long long)blocknr
);
69 return ERR_PTR(RETERR(-EIO
));
72 assert("nikita-931", result
!= NULL
);
73 result
->nplug
= znode_get_tree(brother
)->nplug
;
74 assert("nikita-933", result
->nplug
!= NULL
);
76 retcode
= zinit_new(result
, reiser4_ctx_gfp_mask_get());
78 ZF_SET(result
, JNODE_CREATED
);
82 result
= ERR_PTR(retcode
);
85 /* failure to allocate new node during balancing.
86 This should never happen. Ever. Returning -E_REPEAT
87 is not viable solution, because "out of disk space"
88 is not transient error that will go away by itself.
90 ewarning(retcode
, "nikita-928",
91 "Cannot allocate block for carry: %i", retcode
);
92 result
= ERR_PTR(retcode
);
94 assert("nikita-1071", result
!= NULL
);
98 /* allocate new root and add it to the tree
100 This helper function is called by add_new_root().
103 znode
*reiser4_add_tree_root(znode
* old_root
/* existing tree root */ ,
104 znode
* fake
/* "fake" znode */ )
106 reiser4_tree
*tree
= znode_get_tree(old_root
);
107 znode
*new_root
= NULL
; /* to shut gcc up */
110 assert("nikita-1069", old_root
!= NULL
);
111 assert("umka-262", fake
!= NULL
);
112 assert("umka-263", tree
!= NULL
);
114 /* "fake" znode---one always hanging just above current root. This
115 node is locked when new root is created or existing root is
116 deleted. Downward tree traversal takes lock on it before taking
117 lock on a root node. This avoids race conditions with root
121 assert("nikita-1348", znode_above_root(fake
));
122 assert("nikita-1211", znode_is_root(old_root
));
125 if (tree
->height
>= REAL_MAX_ZTREE_HEIGHT
) {
126 warning("nikita-1344", "Tree is too tall: %i", tree
->height
);
127 /* ext2 returns -ENOSPC when it runs out of free inodes with a
128 following comment (fs/ext2/ialloc.c:441): Is it really
133 result
= RETERR(-ENOSPC
);
135 /* Allocate block for new root. It's not that
136 important where it will be allocated, as root is
137 almost always in memory. Moreover, allocate on
138 flush can be going here.
140 assert("nikita-1448", znode_is_root(old_root
));
141 new_root
= reiser4_new_node(fake
, tree
->height
+ 1);
142 if (!IS_ERR(new_root
) && (result
= zload(new_root
)) == 0) {
147 longterm_lock_znode(&rlh
, new_root
,
151 parent_coord_t
*in_parent
;
153 znode_make_dirty(fake
);
155 /* new root is a child of "fake" node */
156 write_lock_tree(tree
);
160 /* recalculate max balance overhead */
161 tree
->estimate_one_insert
=
162 estimate_one_insert_item(tree
);
164 tree
->root_block
= *znode_get_block(new_root
);
165 in_parent
= &new_root
->in_parent
;
166 init_parent_coord(in_parent
, fake
);
167 /* manually insert new root into sibling
168 * list. With this all nodes involved into
169 * balancing are connected after balancing is
170 * done---useful invariant to check. */
171 sibling_list_insert_nolock(new_root
, NULL
);
172 write_unlock_tree(tree
);
174 /* insert into new root pointer to the
176 assert("nikita-1110",
178 node_is_empty(new_root
)));
180 znode_set_ld_key(new_root
, reiser4_min_key());
181 znode_set_rd_key(new_root
, reiser4_max_key());
182 write_unlock_dk(tree
);
184 ZF_CLR(old_root
, JNODE_LEFT_CONNECTED
);
185 ZF_CLR(old_root
, JNODE_RIGHT_CONNECTED
);
186 ZF_SET(old_root
, JNODE_ORPHAN
);
188 result
= add_child_ptr(new_root
, old_root
);
195 new_root
= ERR_PTR(result
);
199 /* build &reiser4_item_data for inserting child pointer
201 Build &reiser4_item_data that can be later used to insert pointer to @child
205 void build_child_ptr_data(znode
* child
/* node pointer to which will be
207 reiser4_item_data
* data
/* where to store result */ )
209 assert("nikita-1116", child
!= NULL
);
210 assert("nikita-1117", data
!= NULL
);
213 * NOTE: use address of child's blocknr as address of data to be
214 * inserted. As result of this data gets into on-disk structure in cpu
215 * byte order. internal's create_hook converts it to little endian byte
218 data
->data
= (char *)znode_get_block(child
);
219 /* data -> data is kernel space */
221 data
->length
= sizeof(reiser4_block_nr
);
222 /* FIXME-VS: hardcoded internal item? */
224 /* AUDIT: Is it possible that "item_plugin_by_id" may find nothing? */
225 data
->iplug
= item_plugin_by_id(NODE_POINTER_ID
);
228 /* add pointer to @child into empty @parent.
230 This is used when pointer to old root is inserted into new root which is
233 static int add_child_ptr(znode
* parent
, znode
* child
)
236 reiser4_item_data data
;
240 assert("nikita-1111", parent
!= NULL
);
241 assert("nikita-1112", child
!= NULL
);
242 assert("nikita-1115",
243 znode_get_level(parent
) == znode_get_level(child
) + 1);
245 result
= zload(parent
);
248 assert("nikita-1113", node_is_empty(parent
));
249 coord_init_first_unit(&coord
, parent
);
251 build_child_ptr_data(child
, &data
);
254 read_lock_dk(znode_get_tree(parent
));
255 key
= *znode_get_ld_key(child
);
256 read_unlock_dk(znode_get_tree(parent
));
258 result
= node_plugin_by_node(parent
)->create_item(&coord
, &key
, &data
,
260 znode_make_dirty(parent
);
265 /* actually remove tree root */
266 static int reiser4_kill_root(reiser4_tree
* tree
/* tree from which root is
268 znode
* old_root
/* root node that is being
270 znode
* new_root
/* new root---sole child of
272 const reiser4_block_nr
* new_root_blk
/* disk address of
277 lock_handle handle_for_uber
;
279 assert("umka-265", tree
!= NULL
);
280 assert("nikita-1198", new_root
!= NULL
);
281 assert("nikita-1199",
282 znode_get_level(new_root
) + 1 == znode_get_level(old_root
));
284 assert("nikita-1201", znode_is_write_locked(old_root
));
286 assert("nikita-1203",
287 disk_addr_eq(new_root_blk
, znode_get_block(new_root
)));
289 init_lh(&handle_for_uber
);
290 /* obtain and lock "fake" znode protecting changes in tree height. */
291 result
= get_uber_znode(tree
, ZNODE_WRITE_LOCK
, ZNODE_LOCK_HIPRI
,
294 uber
= handle_for_uber
.node
;
296 znode_make_dirty(uber
);
298 /* don't take long term lock a @new_root. Take spinlock. */
300 write_lock_tree(tree
);
302 tree
->root_block
= *new_root_blk
;
305 /* recalculate max balance overhead */
306 tree
->estimate_one_insert
= estimate_one_insert_item(tree
);
308 assert("nikita-1202",
309 tree
->height
== znode_get_level(new_root
));
311 /* new root is child on "fake" node */
312 init_parent_coord(&new_root
->in_parent
, uber
);
315 /* sibling_list_insert_nolock(new_root, NULL); */
316 write_unlock_tree(tree
);
318 /* reinitialise old root. */
319 result
= node_plugin_by_node(old_root
)->init(old_root
);
320 znode_make_dirty(old_root
);
322 assert("nikita-1279", node_is_empty(old_root
));
323 ZF_SET(old_root
, JNODE_HEARD_BANSHEE
);
324 old_root
->c_count
= 0;
327 done_lh(&handle_for_uber
);
334 This function removes tree root, decreasing tree height by one. Tree root
335 and its only child (that is going to become new tree root) are write locked
338 To remove tree root we need to take lock on special "fake" znode that
339 protects changes of tree height. See comments in reiser4_add_tree_root() for
342 Also parent pointers have to be updated in
343 old and new root. To simplify code, function is split into two parts: outer
344 reiser4_kill_tree_root() collects all necessary arguments and calls
345 reiser4_kill_root() to do the actual job.
348 int reiser4_kill_tree_root(znode
* old_root
/* tree root that we are
356 assert("umka-266", current_tree
!= NULL
);
357 assert("nikita-1194", old_root
!= NULL
);
358 assert("nikita-1196", znode_is_root(old_root
));
359 assert("nikita-1200", node_num_items(old_root
) == 1);
360 assert("nikita-1401", znode_is_write_locked(old_root
));
362 coord_init_first_unit(&down_link
, old_root
);
364 tree
= znode_get_tree(old_root
);
365 new_root
= child_znode(&down_link
, old_root
, 0, 1);
366 if (!IS_ERR(new_root
)) {
368 reiser4_kill_root(tree
, old_root
, new_root
,
369 znode_get_block(new_root
));
372 result
= PTR_ERR(new_root
);
379 c-indentation-style: "K&R"