blob: 46b014623fd63def50aad0164f851e5a9979454a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/time.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/string.h>
Al Virof466c6f2012-03-17 01:16:43 -04008#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/buffer_head.h>
10
Jeff Mahoney098297b2014-04-23 10:00:36 -040011/*
12 * To make any changes in the tree we find a node that contains item
13 * to be changed/deleted or position in the node we insert a new item
14 * to. We call this node S. To do balancing we need to decide what we
15 * will shift to left/right neighbor, or to a new node, where new item
16 * will be etc. To make this analysis simpler we build virtual
17 * node. Virtual node is an array of items, that will replace items of
18 * node S. (For instance if we are going to delete an item, virtual
19 * node does not contain it). Virtual node keeps information about
20 * item sizes and types, mergeability of first and last items, sizes
21 * of all entries in directory item. We use this array of items when
22 * calculating what we can shift to neighbors and how many nodes we
23 * have to have if we do not any shiftings, if we shift to left/right
24 * neighbor or to both.
25 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Jeff Mahoney098297b2014-04-23 10:00:36 -040027/*
28 * Takes item number in virtual node, returns number of item
29 * that it has in source buffer
30 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070031static inline int old_item_num(int new_num, int affected_item_num, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070033 if (mode == M_PASTE || mode == M_CUT || new_num < affected_item_num)
34 return new_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070036 if (mode == M_INSERT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 RFALSE(new_num == 0,
39 "vs-8005: for INSERT mode and item number of inserted item");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070041 return new_num - 1;
42 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044 RFALSE(mode != M_DELETE,
45 "vs-8010: old_item_num: mode must be M_DELETE (mode = \'%c\'",
46 mode);
47 /* delete mode */
48 return new_num + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051static void create_virtual_node(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 struct item_head *ih;
54 struct virtual_node *vn = tb->tb_vn;
55 int new_num;
56 struct buffer_head *Sh; /* this comes from tb->S[h] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058 Sh = PATH_H_PBUFFER(tb->tb_path, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 /* size of changed node */
61 vn->vn_size =
62 MAX_CHILD_SIZE(Sh) - B_FREE_SPACE(Sh) + tb->insert_size[h];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 /* for internal nodes array if virtual items is not created */
65 if (h) {
66 vn->vn_nr_item = (vn->vn_size - DC_SIZE) / (DC_SIZE + KEY_SIZE);
67 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070 /* number of items in virtual node */
71 vn->vn_nr_item =
72 B_NR_ITEMS(Sh) + ((vn->vn_mode == M_INSERT) ? 1 : 0) -
73 ((vn->vn_mode == M_DELETE) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070075 /* first virtual item */
76 vn->vn_vi = (struct virtual_item *)(tb->tb_vn + 1);
77 memset(vn->vn_vi, 0, vn->vn_nr_item * sizeof(struct virtual_item));
78 vn->vn_free_ptr += vn->vn_nr_item * sizeof(struct virtual_item);
79
80 /* first item in the node */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040081 ih = item_head(Sh, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082
83 /* define the mergeability for 0-th item (if it is not being deleted) */
Jeff Mahoneya228bf82014-04-23 10:00:42 -040084 if (op_is_left_mergeable(&ih->ih_key, Sh->b_size)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 && (vn->vn_mode != M_DELETE || vn->vn_affected_item_num))
86 vn->vn_vi[0].vi_type |= VI_TYPE_LEFT_MERGEABLE;
87
Jeff Mahoney098297b2014-04-23 10:00:36 -040088 /*
89 * go through all items that remain in the virtual
90 * node (except for the new (inserted) one)
91 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092 for (new_num = 0; new_num < vn->vn_nr_item; new_num++) {
93 int j;
94 struct virtual_item *vi = vn->vn_vi + new_num;
95 int is_affected =
96 ((new_num != vn->vn_affected_item_num) ? 0 : 1);
97
98 if (is_affected && vn->vn_mode == M_INSERT)
99 continue;
100
101 /* get item number in source node */
102 j = old_item_num(new_num, vn->vn_affected_item_num,
103 vn->vn_mode);
104
105 vi->vi_item_len += ih_item_len(ih + j) + IH_SIZE;
106 vi->vi_ih = ih + j;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400107 vi->vi_item = ih_item_body(Sh, ih + j);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 vi->vi_uarea = vn->vn_free_ptr;
109
Jeff Mahoney098297b2014-04-23 10:00:36 -0400110 /*
111 * FIXME: there is no check that item operation did not
112 * consume too much memory
113 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700114 vn->vn_free_ptr +=
115 op_create_vi(vn, vi, is_affected, tb->insert_size[0]);
116 if (tb->vn_buf + tb->vn_buf_size < vn->vn_free_ptr)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400117 reiserfs_panic(tb->tb_sb, "vs-8030",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118 "virtual node space consumed");
119
120 if (!is_affected)
121 /* this is not being changed */
122 continue;
123
124 if (vn->vn_mode == M_PASTE || vn->vn_mode == M_CUT) {
125 vn->vn_vi[new_num].vi_item_len += tb->insert_size[0];
Jeff Mahoney098297b2014-04-23 10:00:36 -0400126 /* pointer to data which is going to be pasted */
127 vi->vi_new_data = vn->vn_data;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 }
129 }
130
131 /* virtual inserted item is not defined yet */
132 if (vn->vn_mode == M_INSERT) {
133 struct virtual_item *vi = vn->vn_vi + vn->vn_affected_item_num;
134
Al Viro9dce07f2008-03-29 03:07:28 +0000135 RFALSE(vn->vn_ins_ih == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 "vs-8040: item header of inserted item is not specified");
137 vi->vi_item_len = tb->insert_size[0];
138 vi->vi_ih = vn->vn_ins_ih;
139 vi->vi_item = vn->vn_data;
140 vi->vi_uarea = vn->vn_free_ptr;
141
142 op_create_vi(vn, vi, 0 /*not pasted or cut */ ,
143 tb->insert_size[0]);
144 }
145
Jeff Mahoney098297b2014-04-23 10:00:36 -0400146 /*
147 * set right merge flag we take right delimiting key and
148 * check whether it is a mergeable item
149 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150 if (tb->CFR[0]) {
151 struct reiserfs_key *key;
152
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400153 key = internal_key(tb->CFR[0], tb->rkey[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 if (op_is_left_mergeable(key, Sh->b_size)
155 && (vn->vn_mode != M_DELETE
156 || vn->vn_affected_item_num != B_NR_ITEMS(Sh) - 1))
157 vn->vn_vi[vn->vn_nr_item - 1].vi_type |=
158 VI_TYPE_RIGHT_MERGEABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 if (op_is_left_mergeable(key, Sh->b_size) &&
162 !(vn->vn_mode != M_DELETE
163 || vn->vn_affected_item_num != B_NR_ITEMS(Sh) - 1)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400164 /*
165 * we delete last item and it could be merged
166 * with right neighbor's first item
167 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 if (!
169 (B_NR_ITEMS(Sh) == 1
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400170 && is_direntry_le_ih(item_head(Sh, 0))
171 && ih_entry_count(item_head(Sh, 0)) == 1)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400172 /*
173 * node contains more than 1 item, or item
174 * is not directory item, or this item
175 * contains more than 1 entry
176 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 print_block(Sh, 0, -1, -1);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400178 reiserfs_panic(tb->tb_sb, "vs-8045",
179 "rdkey %k, affected item==%d "
180 "(mode==%c) Must be %c",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181 key, vn->vn_affected_item_num,
182 vn->vn_mode, M_DELETE);
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800183 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 }
188}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Jeff Mahoney098297b2014-04-23 10:00:36 -0400190/*
191 * Using virtual node check, how many items can be
192 * shifted to left neighbor
193 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194static void check_left(struct tree_balance *tb, int h, int cur_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 int i;
197 struct virtual_node *vn = tb->tb_vn;
198 struct virtual_item *vi;
199 int d_size, ih_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 RFALSE(cur_free < 0, "vs-8050: cur_free (%d) < 0", cur_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 /* internal level */
204 if (h > 0) {
205 tb->lnum[h] = cur_free / (DC_SIZE + KEY_SIZE);
206 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208
209 /* leaf level */
210
211 if (!cur_free || !vn->vn_nr_item) {
212 /* no free space or nothing to move */
213 tb->lnum[h] = 0;
214 tb->lbytes = -1;
215 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217
218 RFALSE(!PATH_H_PPARENT(tb->tb_path, 0),
219 "vs-8055: parent does not exist or invalid");
220
221 vi = vn->vn_vi;
222 if ((unsigned int)cur_free >=
223 (vn->vn_size -
224 ((vi->vi_type & VI_TYPE_LEFT_MERGEABLE) ? IH_SIZE : 0))) {
225 /* all contents of S[0] fits into L[0] */
226
227 RFALSE(vn->vn_mode == M_INSERT || vn->vn_mode == M_PASTE,
228 "vs-8055: invalid mode or balance condition failed");
229
230 tb->lnum[0] = vn->vn_nr_item;
231 tb->lbytes = -1;
232 return;
233 }
234
235 d_size = 0, ih_size = IH_SIZE;
236
237 /* first item may be merge with last item in left neighbor */
238 if (vi->vi_type & VI_TYPE_LEFT_MERGEABLE)
239 d_size = -((int)IH_SIZE), ih_size = 0;
240
241 tb->lnum[0] = 0;
242 for (i = 0; i < vn->vn_nr_item;
243 i++, ih_size = IH_SIZE, d_size = 0, vi++) {
244 d_size += vi->vi_item_len;
245 if (cur_free >= d_size) {
246 /* the item can be shifted entirely */
247 cur_free -= d_size;
248 tb->lnum[0]++;
249 continue;
250 }
251
252 /* the item cannot be shifted entirely, try to split it */
Jeff Mahoney098297b2014-04-23 10:00:36 -0400253 /*
254 * check whether L[0] can hold ih and at least one byte
255 * of the item body
256 */
257
258 /* cannot shift even a part of the current item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700259 if (cur_free <= ih_size) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 tb->lbytes = -1;
261 return;
262 }
263 cur_free -= ih_size;
264
265 tb->lbytes = op_check_left(vi, cur_free, 0, 0);
266 if (tb->lbytes != -1)
267 /* count partially shifted item */
268 tb->lnum[0]++;
269
270 break;
271 }
272
273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Jeff Mahoney098297b2014-04-23 10:00:36 -0400276/*
277 * Using virtual node check, how many items can be
278 * shifted to right neighbor
279 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280static void check_right(struct tree_balance *tb, int h, int cur_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700282 int i;
283 struct virtual_node *vn = tb->tb_vn;
284 struct virtual_item *vi;
285 int d_size, ih_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 RFALSE(cur_free < 0, "vs-8070: cur_free < 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700289 /* internal level */
290 if (h > 0) {
291 tb->rnum[h] = cur_free / (DC_SIZE + KEY_SIZE);
292 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 /* leaf level */
296
297 if (!cur_free || !vn->vn_nr_item) {
298 /* no free space */
299 tb->rnum[h] = 0;
300 tb->rbytes = -1;
301 return;
302 }
303
304 RFALSE(!PATH_H_PPARENT(tb->tb_path, 0),
305 "vs-8075: parent does not exist or invalid");
306
307 vi = vn->vn_vi + vn->vn_nr_item - 1;
308 if ((unsigned int)cur_free >=
309 (vn->vn_size -
310 ((vi->vi_type & VI_TYPE_RIGHT_MERGEABLE) ? IH_SIZE : 0))) {
311 /* all contents of S[0] fits into R[0] */
312
313 RFALSE(vn->vn_mode == M_INSERT || vn->vn_mode == M_PASTE,
314 "vs-8080: invalid mode or balance condition failed");
315
316 tb->rnum[h] = vn->vn_nr_item;
317 tb->rbytes = -1;
318 return;
319 }
320
321 d_size = 0, ih_size = IH_SIZE;
322
323 /* last item may be merge with first item in right neighbor */
324 if (vi->vi_type & VI_TYPE_RIGHT_MERGEABLE)
325 d_size = -(int)IH_SIZE, ih_size = 0;
326
327 tb->rnum[0] = 0;
328 for (i = vn->vn_nr_item - 1; i >= 0;
329 i--, d_size = 0, ih_size = IH_SIZE, vi--) {
330 d_size += vi->vi_item_len;
331 if (cur_free >= d_size) {
332 /* the item can be shifted entirely */
333 cur_free -= d_size;
334 tb->rnum[0]++;
335 continue;
336 }
337
Jeff Mahoney098297b2014-04-23 10:00:36 -0400338 /*
339 * check whether R[0] can hold ih and at least one
340 * byte of the item body
341 */
342
343 /* cannot shift even a part of the current item */
344 if (cur_free <= ih_size) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700345 tb->rbytes = -1;
346 return;
347 }
348
Jeff Mahoney098297b2014-04-23 10:00:36 -0400349 /*
350 * R[0] can hold the header of the item and at least
351 * one byte of its body
352 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 cur_free -= ih_size; /* cur_free is still > 0 */
354
355 tb->rbytes = op_check_right(vi, cur_free);
356 if (tb->rbytes != -1)
357 /* count partially shifted item */
358 tb->rnum[0]++;
359
360 break;
361 }
362
363 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * from - number of items, which are shifted to left neighbor entirely
368 * to - number of item, which are shifted to right neighbor entirely
Jeff Mahoney098297b2014-04-23 10:00:36 -0400369 * from_bytes - number of bytes of boundary item (or directory entries)
370 * which are shifted to left neighbor
371 * to_bytes - number of bytes of boundary item (or directory entries)
372 * which are shifted to right neighbor
373 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374static int get_num_ver(int mode, struct tree_balance *tb, int h,
375 int from, int from_bytes,
376 int to, int to_bytes, short *snum012, int flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378 int i;
379 int cur_free;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700380 int units;
381 struct virtual_node *vn = tb->tb_vn;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700382 int total_node_size, max_node_size, current_item_size;
383 int needed_nodes;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400384
385 /* position of item we start filling node from */
386 int start_item;
387
388 /* position of item we finish filling node by */
389 int end_item;
390
391 /*
392 * number of first bytes (entries for directory) of start_item-th item
393 * we do not include into node that is being filled
394 */
395 int start_bytes;
396
397 /*
398 * number of last bytes (entries for directory) of end_item-th item
399 * we do node include into node that is being filled
400 */
401 int end_bytes;
402
403 /*
404 * these are positions in virtual item of items, that are split
405 * between S[0] and S1new and S1new and S2new
406 */
407 int split_item_positions[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 split_item_positions[0] = -1;
410 split_item_positions[1] = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Jeff Mahoney098297b2014-04-23 10:00:36 -0400412 /*
413 * We only create additional nodes if we are in insert or paste mode
414 * or we are in replace mode at the internal level. If h is 0 and
415 * the mode is M_REPLACE then in fix_nodes we change the mode to
416 * paste or insert before we get here in the code.
417 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700418 RFALSE(tb->insert_size[h] < 0 || (mode != M_INSERT && mode != M_PASTE),
419 "vs-8100: insert_size < 0 in overflow");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 max_node_size = MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jeff Mahoney098297b2014-04-23 10:00:36 -0400423 /*
424 * snum012 [0-2] - number of items, that lay
425 * to S[0], first new node and second new node
426 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700427 snum012[3] = -1; /* s1bytes */
428 snum012[4] = -1; /* s2bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430 /* internal level */
431 if (h > 0) {
432 i = ((to - from) * (KEY_SIZE + DC_SIZE) + DC_SIZE);
433 if (i == max_node_size)
434 return 1;
435 return (i / max_node_size + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438 /* leaf level */
439 needed_nodes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 total_node_size = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 cur_free = max_node_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jeff Mahoney098297b2014-04-23 10:00:36 -0400443 /* start from 'from'-th item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 start_item = from;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400445 /* skip its first 'start_bytes' units */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446 start_bytes = ((from_bytes != -1) ? from_bytes : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jeff Mahoney098297b2014-04-23 10:00:36 -0400448 /* last included item is the 'end_item'-th one */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700449 end_item = vn->vn_nr_item - to - 1;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400450 /* do not count last 'end_bytes' units of 'end_item'-th item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451 end_bytes = (to_bytes != -1) ? to_bytes : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jeff Mahoney098297b2014-04-23 10:00:36 -0400453 /*
454 * go through all item beginning from the start_item-th item
455 * and ending by the end_item-th item. Do not count first
456 * 'start_bytes' units of 'start_item'-th item and last
457 * 'end_bytes' of 'end_item'-th item
458 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 for (i = start_item; i <= end_item; i++) {
460 struct virtual_item *vi = vn->vn_vi + i;
461 int skip_from_end = ((i == end_item) ? end_bytes : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700463 RFALSE(needed_nodes > 3, "vs-8105: too many nodes are needed");
464
465 /* get size of current item */
466 current_item_size = vi->vi_item_len;
467
Jeff Mahoney098297b2014-04-23 10:00:36 -0400468 /*
469 * do not take in calculation head part (from_bytes)
470 * of from-th item
471 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 current_item_size -=
473 op_part_size(vi, 0 /*from start */ , start_bytes);
474
475 /* do not take in calculation tail part of last item */
476 current_item_size -=
477 op_part_size(vi, 1 /*from end */ , skip_from_end);
478
479 /* if item fits into current node entierly */
480 if (total_node_size + current_item_size <= max_node_size) {
481 snum012[needed_nodes - 1]++;
482 total_node_size += current_item_size;
483 start_bytes = 0;
484 continue;
485 }
486
Jeff Mahoney098297b2014-04-23 10:00:36 -0400487 /*
488 * virtual item length is longer, than max size of item in
489 * a node. It is impossible for direct item
490 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491 if (current_item_size > max_node_size) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 RFALSE(is_direct_le_ih(vi->vi_ih),
493 "vs-8110: "
494 "direct item length is %d. It can not be longer than %d",
495 current_item_size, max_node_size);
496 /* we will try to split it */
497 flow = 1;
498 }
499
Jeff Mahoney098297b2014-04-23 10:00:36 -0400500 /* as we do not split items, take new node and continue */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 if (!flow) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502 needed_nodes++;
503 i--;
504 total_node_size = 0;
505 continue;
506 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400507
508 /*
509 * calculate number of item units which fit into node being
510 * filled
511 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 {
513 int free_space;
514
515 free_space = max_node_size - total_node_size - IH_SIZE;
516 units =
517 op_check_left(vi, free_space, start_bytes,
518 skip_from_end);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400519 /*
520 * nothing fits into current node, take new
521 * node and continue
522 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 if (units == -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700524 needed_nodes++, i--, total_node_size = 0;
525 continue;
526 }
527 }
528
529 /* something fits into the current node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700530 start_bytes += units;
531 snum012[needed_nodes - 1 + 3] = units;
532
533 if (needed_nodes > 2)
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400534 reiserfs_warning(tb->tb_sb, "vs-8111",
535 "split_item_position is out of range");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700536 snum012[needed_nodes - 1]++;
537 split_item_positions[needed_nodes - 1] = i;
538 needed_nodes++;
539 /* continue from the same item with start_bytes != -1 */
540 start_item = i;
541 i--;
542 total_node_size = 0;
543 }
544
Jeff Mahoney098297b2014-04-23 10:00:36 -0400545 /*
546 * sum012[4] (if it is not -1) contains number of units of which
547 * are to be in S1new, snum012[3] - to be in S0. They are supposed
548 * to be S1bytes and S2bytes correspondingly, so recalculate
549 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700550 if (snum012[4] > 0) {
551 int split_item_num;
552 int bytes_to_r, bytes_to_l;
553 int bytes_to_S1new;
554
555 split_item_num = split_item_positions[1];
556 bytes_to_l =
557 ((from == split_item_num
558 && from_bytes != -1) ? from_bytes : 0);
559 bytes_to_r =
560 ((end_item == split_item_num
561 && end_bytes != -1) ? end_bytes : 0);
562 bytes_to_S1new =
563 ((split_item_positions[0] ==
564 split_item_positions[1]) ? snum012[3] : 0);
565
Jeff Mahoney098297b2014-04-23 10:00:36 -0400566 /* s2bytes */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 snum012[4] =
568 op_unit_num(&vn->vn_vi[split_item_num]) - snum012[4] -
569 bytes_to_r - bytes_to_l - bytes_to_S1new;
570
571 if (vn->vn_vi[split_item_num].vi_index != TYPE_DIRENTRY &&
572 vn->vn_vi[split_item_num].vi_index != TYPE_INDIRECT)
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400573 reiserfs_warning(tb->tb_sb, "vs-8115",
574 "not directory or indirect item");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700575 }
576
577 /* now we know S2bytes, calculate S1bytes */
578 if (snum012[3] > 0) {
579 int split_item_num;
580 int bytes_to_r, bytes_to_l;
581 int bytes_to_S2new;
582
583 split_item_num = split_item_positions[0];
584 bytes_to_l =
585 ((from == split_item_num
586 && from_bytes != -1) ? from_bytes : 0);
587 bytes_to_r =
588 ((end_item == split_item_num
589 && end_bytes != -1) ? end_bytes : 0);
590 bytes_to_S2new =
591 ((split_item_positions[0] == split_item_positions[1]
592 && snum012[4] != -1) ? snum012[4] : 0);
593
Jeff Mahoney098297b2014-04-23 10:00:36 -0400594 /* s1bytes */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700595 snum012[3] =
596 op_unit_num(&vn->vn_vi[split_item_num]) - snum012[3] -
597 bytes_to_r - bytes_to_l - bytes_to_S2new;
598 }
599
600 return needed_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jeff Mahoney098297b2014-04-23 10:00:36 -0400604/*
605 * Set parameters for balancing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * Performs write of results of analysis of balancing into structure tb,
Jeff Mahoney0222e652009-03-30 14:02:44 -0400607 * where it will later be used by the functions that actually do the balancing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Parameters:
609 * tb tree_balance structure;
610 * h current level of the node;
611 * lnum number of items from S[h] that must be shifted to L[h];
612 * rnum number of items from S[h] that must be shifted to R[h];
613 * blk_num number of blocks that S[h] will be splitted into;
614 * s012 number of items that fall into splitted nodes.
Jeff Mahoney098297b2014-04-23 10:00:36 -0400615 * lbytes number of bytes which flow to the left neighbor from the
616 * item that is not not shifted entirely
617 * rbytes number of bytes which flow to the right neighbor from the
618 * item that is not not shifted entirely
619 * s1bytes number of bytes which flow to the first new node when
620 * S[0] splits (this number is contained in s012 array)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623static void set_parameters(struct tree_balance *tb, int h, int lnum,
624 int rnum, int blk_num, short *s012, int lb, int rb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627 tb->lnum[h] = lnum;
628 tb->rnum[h] = rnum;
629 tb->blknum[h] = blk_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jeff Mahoney098297b2014-04-23 10:00:36 -0400631 /* only for leaf level */
632 if (h == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 if (s012 != NULL) {
634 tb->s0num = *s012++,
635 tb->s1num = *s012++, tb->s2num = *s012++;
636 tb->s1bytes = *s012++;
637 tb->s2bytes = *s012;
638 }
639 tb->lbytes = lb;
640 tb->rbytes = rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700642 PROC_INFO_ADD(tb->tb_sb, lnum[h], lnum);
643 PROC_INFO_ADD(tb->tb_sb, rnum[h], rnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 PROC_INFO_ADD(tb->tb_sb, lbytes[h], lb);
646 PROC_INFO_ADD(tb->tb_sb, rbytes[h], rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Jeff Mahoney098297b2014-04-23 10:00:36 -0400649/*
650 * check if node disappears if we shift tb->lnum[0] items to left
651 * neighbor and tb->rnum[0] to the right one.
652 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653static int is_leaf_removable(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 struct virtual_node *vn = tb->tb_vn;
656 int to_left, to_right;
657 int size;
658 int remain_items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Jeff Mahoney098297b2014-04-23 10:00:36 -0400660 /*
661 * number of items that will be shifted to left (right) neighbor
662 * entirely
663 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664 to_left = tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0);
665 to_right = tb->rnum[0] - ((tb->rbytes != -1) ? 1 : 0);
666 remain_items = vn->vn_nr_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668 /* how many items remain in S[0] after shiftings to neighbors */
669 remain_items -= (to_left + to_right);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jeff Mahoney098297b2014-04-23 10:00:36 -0400671 /* all content of node can be shifted to neighbors */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 if (remain_items < 1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700673 set_parameters(tb, 0, to_left, vn->vn_nr_item - to_left, 0,
674 NULL, -1, -1);
675 return 1;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jeff Mahoney098297b2014-04-23 10:00:36 -0400678 /* S[0] is not removable */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 if (remain_items > 1 || tb->lbytes == -1 || tb->rbytes == -1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jeff Mahoney098297b2014-04-23 10:00:36 -0400682 /* check whether we can divide 1 remaining item between neighbors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700684 /* get size of remaining item (in item units) */
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400685 size = op_unit_num(&vn->vn_vi[to_left]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700687 if (tb->lbytes + tb->rbytes >= size) {
688 set_parameters(tb, 0, to_left + 1, to_right + 1, 0, NULL,
689 tb->lbytes, -1);
690 return 1;
691 }
692
693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/* check whether L, S, R can be joined in one node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697static int are_leaves_removable(struct tree_balance *tb, int lfree, int rfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700699 struct virtual_node *vn = tb->tb_vn;
700 int ih_size;
701 struct buffer_head *S0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700703 S0 = PATH_H_PBUFFER(tb->tb_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 ih_size = 0;
706 if (vn->vn_nr_item) {
707 if (vn->vn_vi[0].vi_type & VI_TYPE_LEFT_MERGEABLE)
708 ih_size += IH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 if (vn->vn_vi[vn->vn_nr_item - 1].
711 vi_type & VI_TYPE_RIGHT_MERGEABLE)
712 ih_size += IH_SIZE;
713 } else {
714 /* there was only one item and it will be deleted */
715 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 RFALSE(B_NR_ITEMS(S0) != 1,
718 "vs-8125: item number must be 1: it is %d",
719 B_NR_ITEMS(S0));
720
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400721 ih = item_head(S0, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 if (tb->CFR[0]
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400723 && !comp_short_le_keys(&ih->ih_key,
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400724 internal_key(tb->CFR[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700725 tb->rkey[0])))
Jeff Mahoney098297b2014-04-23 10:00:36 -0400726 /*
727 * Directory must be in correct state here: that is
728 * somewhere at the left side should exist first
729 * directory item. But the item being deleted can
730 * not be that first one because its right neighbor
731 * is item of the same directory. (But first item
732 * always gets deleted in last turn). So, neighbors
733 * of deleted item can be merged, so we can save
734 * ih_size
735 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 if (is_direntry_le_ih(ih)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700737 ih_size = IH_SIZE;
738
Jeff Mahoney098297b2014-04-23 10:00:36 -0400739 /*
740 * we might check that left neighbor exists
741 * and is of the same directory
742 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 RFALSE(le_ih_k_offset(ih) == DOT_OFFSET,
744 "vs-8130: first directory item can not be removed until directory is not empty");
745 }
746
747 }
748
749 if (MAX_CHILD_SIZE(S0) + vn->vn_size <= rfree + lfree + ih_size) {
750 set_parameters(tb, 0, -1, -1, -1, NULL, -1, -1);
751 PROC_INFO_INC(tb->tb_sb, leaves_removable);
752 return 1;
753 }
754 return 0;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/* when we do not split item, lnum and rnum are numbers of entire items */
759#define SET_PAR_SHIFT_LEFT \
760if (h)\
761{\
762 int to_l;\
763 \
764 to_l = (MAX_NR_KEY(Sh)+1 - lpar + vn->vn_nr_item + 1) / 2 -\
765 (MAX_NR_KEY(Sh) + 1 - lpar);\
766 \
767 set_parameters (tb, h, to_l, 0, lnver, NULL, -1, -1);\
768}\
769else \
770{\
771 if (lset==LEFT_SHIFT_FLOW)\
772 set_parameters (tb, h, lpar, 0, lnver, snum012+lset,\
773 tb->lbytes, -1);\
774 else\
775 set_parameters (tb, h, lpar - (tb->lbytes!=-1), 0, lnver, snum012+lset,\
776 -1, -1);\
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779#define SET_PAR_SHIFT_RIGHT \
780if (h)\
781{\
782 int to_r;\
783 \
784 to_r = (MAX_NR_KEY(Sh)+1 - rpar + vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 - rpar);\
785 \
786 set_parameters (tb, h, 0, to_r, rnver, NULL, -1, -1);\
787}\
788else \
789{\
790 if (rset==RIGHT_SHIFT_FLOW)\
791 set_parameters (tb, h, 0, rpar, rnver, snum012+rset,\
792 -1, tb->rbytes);\
793 else\
794 set_parameters (tb, h, 0, rpar - (tb->rbytes!=-1), rnver, snum012+rset,\
795 -1, -1);\
796}
797
Jeff Mahoneya063ae12009-03-30 14:02:48 -0400798static void free_buffers_in_tb(struct tree_balance *tb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700799{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400800 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jeff Mahoneya063ae12009-03-30 14:02:48 -0400802 pathrelse(tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jeff Mahoneyee939612009-03-30 14:02:50 -0400804 for (i = 0; i < MAX_HEIGHT; i++) {
805 brelse(tb->L[i]);
806 brelse(tb->R[i]);
807 brelse(tb->FL[i]);
808 brelse(tb->FR[i]);
809 brelse(tb->CFL[i]);
810 brelse(tb->CFR[i]);
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400811
Jeff Mahoneyee939612009-03-30 14:02:50 -0400812 tb->L[i] = NULL;
813 tb->R[i] = NULL;
814 tb->FL[i] = NULL;
815 tb->FR[i] = NULL;
816 tb->CFL[i] = NULL;
817 tb->CFR[i] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Jeff Mahoney098297b2014-04-23 10:00:36 -0400821/*
822 * Get new buffers for storing new nodes that are created while balancing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked;
824 * CARRY_ON - schedule didn't occur while the function worked;
825 * NO_DISK_SPACE - no disk space.
826 */
827/* The function is NOT SCHEDULE-SAFE! */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400828static int get_empty_nodes(struct tree_balance *tb, int h)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400830 struct buffer_head *new_bh, *Sh = PATH_H_PBUFFER(tb->tb_path, h);
Jeff Mahoneyee939612009-03-30 14:02:50 -0400831 b_blocknr_t *blocknr, blocknrs[MAX_AMOUNT_NEEDED] = { 0, };
Jeff Mahoney098297b2014-04-23 10:00:36 -0400832 int counter, number_of_freeblk;
833 int amount_needed; /* number of needed empty blocks */
834 int retval = CARRY_ON;
Jeff Mahoneya063ae12009-03-30 14:02:48 -0400835 struct super_block *sb = tb->tb_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jeff Mahoney098297b2014-04-23 10:00:36 -0400837 /*
838 * number_of_freeblk is the number of empty blocks which have been
839 * acquired for use by the balancing algorithm minus the number of
840 * empty blocks used in the previous levels of the analysis,
841 * number_of_freeblk = tb->cur_blknum can be non-zero if a schedule
842 * occurs after empty blocks are acquired, and the balancing analysis
843 * is then restarted, amount_needed is the number needed by this
844 * level (h) of the balancing analysis.
845 *
846 * Note that for systems with many processes writing, it would be
847 * more layout optimal to calculate the total number needed by all
848 * levels and then to run reiserfs_new_blocks to get all of them at
849 * once.
850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jeff Mahoney098297b2014-04-23 10:00:36 -0400852 /*
853 * Initiate number_of_freeblk to the amount acquired prior to the
854 * restart of the analysis or 0 if not restarted, then subtract the
855 * amount needed by all of the levels of the tree below h.
856 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400857 /* blknum includes S[h], so we subtract 1 in this calculation */
858 for (counter = 0, number_of_freeblk = tb->cur_blknum;
859 counter < h; counter++)
860 number_of_freeblk -=
861 (tb->blknum[counter]) ? (tb->blknum[counter] -
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700862 1) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700864 /* Allocate missing empty blocks. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400865 /* if Sh == 0 then we are getting a new root */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400866 amount_needed = (Sh) ? (tb->blknum[h] - 1) : 1;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400867 /*
868 * Amount_needed = the amount that we need more than the
869 * amount that we have.
870 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400871 if (amount_needed > number_of_freeblk)
872 amount_needed -= number_of_freeblk;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400873 else /* If we have enough already then there is nothing to do. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700874 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Jeff Mahoney098297b2014-04-23 10:00:36 -0400876 /*
877 * No need to check quota - is not allocated for blocks used
878 * for formatted nodes
879 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400880 if (reiserfs_new_form_blocknrs(tb, blocknrs,
881 amount_needed) == NO_DISK_SPACE)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882 return NO_DISK_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700884 /* for each blocknumber we just got, get a buffer and stick it on FEB */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400885 for (blocknr = blocknrs, counter = 0;
886 counter < amount_needed; blocknr++, counter++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400888 RFALSE(!*blocknr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 "PAP-8135: reiserfs_new_blocknrs failed when got new blocks");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400891 new_bh = sb_getblk(sb, *blocknr);
892 RFALSE(buffer_dirty(new_bh) ||
893 buffer_journaled(new_bh) ||
894 buffer_journal_dirty(new_bh),
Adam Buchbinderfebe29d2009-12-04 15:24:22 -0500895 "PAP-8140: journaled or dirty buffer %b for the new block",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400896 new_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898 /* Put empty buffers into the array. */
Jeff Mahoneya063ae12009-03-30 14:02:48 -0400899 RFALSE(tb->FEB[tb->cur_blknum],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700900 "PAP-8141: busy slot for new buffer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400902 set_buffer_journal_new(new_bh);
903 tb->FEB[tb->cur_blknum++] = new_bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Jeff Mahoneyee939612009-03-30 14:02:50 -0400906 if (retval == CARRY_ON && FILESYSTEM_CHANGED_TB(tb))
907 retval = REPEAT_SEARCH;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700908
Jeff Mahoneyee939612009-03-30 14:02:50 -0400909 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Jeff Mahoney098297b2014-04-23 10:00:36 -0400912/*
913 * Get free space of the left neighbor, which is stored in the parent
914 * node of the left neighbor.
915 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700916static int get_lfree(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700918 struct buffer_head *l, *f;
919 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Al Viro9dce07f2008-03-29 03:07:28 +0000921 if ((f = PATH_H_PPARENT(tb->tb_path, h)) == NULL ||
922 (l = tb->FL[h]) == NULL)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700925 if (f == l)
926 order = PATH_H_B_ITEM_ORDER(tb->tb_path, h) - 1;
927 else {
928 order = B_NR_ITEMS(l);
929 f = l;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700932 return (MAX_CHILD_SIZE(f) - dc_size(B_N_CHILD(f, order)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Jeff Mahoney098297b2014-04-23 10:00:36 -0400935/*
936 * Get free space of the right neighbor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * which is stored in the parent node of the right neighbor.
938 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700939static int get_rfree(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700941 struct buffer_head *r, *f;
942 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Al Viro9dce07f2008-03-29 03:07:28 +0000944 if ((f = PATH_H_PPARENT(tb->tb_path, h)) == NULL ||
945 (r = tb->FR[h]) == NULL)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700948 if (f == r)
949 order = PATH_H_B_ITEM_ORDER(tb->tb_path, h) + 1;
950 else {
951 order = 0;
952 f = r;
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700955 return (MAX_CHILD_SIZE(f) - dc_size(B_N_CHILD(f, order)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/* Check whether left neighbor is in memory. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400960static int is_left_neighbor_in_cache(struct tree_balance *tb, int h)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700961{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400962 struct buffer_head *father, *left;
Jeff Mahoneya063ae12009-03-30 14:02:48 -0400963 struct super_block *sb = tb->tb_sb;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400964 b_blocknr_t left_neighbor_blocknr;
965 int left_neighbor_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Jeff Mahoneya063ae12009-03-30 14:02:48 -0400967 /* Father of the left neighbor does not exist. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400968 if (!tb->FL[h])
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700971 /* Calculate father of the node to be balanced. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400972 father = PATH_H_PBUFFER(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400974 RFALSE(!father ||
975 !B_IS_IN_TREE(father) ||
Jeff Mahoneyee939612009-03-30 14:02:50 -0400976 !B_IS_IN_TREE(tb->FL[h]) ||
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400977 !buffer_uptodate(father) ||
Jeff Mahoneyee939612009-03-30 14:02:50 -0400978 !buffer_uptodate(tb->FL[h]),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 "vs-8165: F[h] (%b) or FL[h] (%b) is invalid",
Jeff Mahoneyee939612009-03-30 14:02:50 -0400980 father, tb->FL[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Jeff Mahoney098297b2014-04-23 10:00:36 -0400982 /*
983 * Get position of the pointer to the left neighbor
984 * into the left father.
985 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400986 left_neighbor_position = (father == tb->FL[h]) ?
987 tb->lkey[h] : B_NR_ITEMS(tb->FL[h]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700988 /* Get left neighbor block number. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400989 left_neighbor_blocknr =
990 B_N_CHILD_NUM(tb->FL[h], left_neighbor_position);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700991 /* Look for the left neighbor in the cache. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400992 if ((left = sb_find_get_block(sb, left_neighbor_blocknr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700994 RFALSE(buffer_uptodate(left) && !B_IS_IN_TREE(left),
995 "vs-8170: left neighbor (%b %z) is not in the tree",
996 left, left);
997 put_bh(left);
998 return 1;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#define LEFT_PARENTS 'l'
1005#define RIGHT_PARENTS 'r'
1006
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001007static void decrement_key(struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Jeff Mahoney098297b2014-04-23 10:00:36 -04001009 /* call item specific function for this key */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001010 item_ops[cpu_key_k_type(key)]->decrement_key(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Jeff Mahoney098297b2014-04-23 10:00:36 -04001013/*
1014 * Calculate far left/right parent of the left/right neighbor of the
1015 * current node, that is calculate the left/right (FL[h]/FR[h]) neighbor
1016 * of the parent F[h].
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * Calculate left/right common parent of the current node and L[h]/R[h].
1018 * Calculate left/right delimiting key position.
Jeff Mahoney098297b2014-04-23 10:00:36 -04001019 * Returns: PATH_INCORRECT - path in the tree is not correct
1020 * SCHEDULE_OCCURRED - schedule occurred while the function worked
1021 * CARRY_ON - schedule didn't occur while the function
1022 * worked
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001024static int get_far_parent(struct tree_balance *tb,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001025 int h,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001026 struct buffer_head **pfather,
1027 struct buffer_head **pcom_father, char c_lr_par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001029 struct buffer_head *parent;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001030 INITIALIZE_PATH(s_path_to_neighbor_father);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001031 struct treepath *path = tb->tb_path;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001032 struct cpu_key s_lr_father_key;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001033 int counter,
1034 position = INT_MAX,
1035 first_last_position = 0,
1036 path_offset = PATH_H_PATH_OFFSET(path, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Jeff Mahoney098297b2014-04-23 10:00:36 -04001038 /*
1039 * Starting from F[h] go upwards in the tree, and look for the common
1040 * ancestor of F[h], and its neighbor l/r, that should be obtained.
1041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Jeff Mahoneyee939612009-03-30 14:02:50 -04001043 counter = path_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Jeff Mahoneyee939612009-03-30 14:02:50 -04001045 RFALSE(counter < FIRST_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001046 "PAP-8180: invalid path length");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Jeff Mahoneyee939612009-03-30 14:02:50 -04001048 for (; counter > FIRST_PATH_ELEMENT_OFFSET; counter--) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001049 /*
1050 * Check whether parent of the current buffer in the path
1051 * is really parent in the tree.
1052 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 if (!B_IS_IN_TREE
Jeff Mahoneyee939612009-03-30 14:02:50 -04001054 (parent = PATH_OFFSET_PBUFFER(path, counter - 1)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 return REPEAT_SEARCH;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001057 /* Check whether position in the parent is correct. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001058 if ((position =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001059 PATH_OFFSET_POSITION(path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001060 counter - 1)) >
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001061 B_NR_ITEMS(parent))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 return REPEAT_SEARCH;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001063
1064 /*
1065 * Check whether parent at the path really points
1066 * to the child.
1067 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001068 if (B_N_CHILD_NUM(parent, position) !=
1069 PATH_OFFSET_PBUFFER(path, counter)->b_blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070 return REPEAT_SEARCH;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001071
1072 /*
1073 * Return delimiting key if position in the parent is not
1074 * equal to first/last one.
1075 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001076 if (c_lr_par == RIGHT_PARENTS)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001077 first_last_position = B_NR_ITEMS(parent);
1078 if (position != first_last_position) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001079 *pcom_father = parent;
1080 get_bh(*pcom_father);
1081 /*(*pcom_father = parent)->b_count++; */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001082 break;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 /* if we are in the root of the tree, then there is no common father */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001087 if (counter == FIRST_PATH_ELEMENT_OFFSET) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001088 /*
1089 * Check whether first buffer in the path is the
1090 * root of the tree.
1091 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001092 if (PATH_OFFSET_PBUFFER
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001093 (tb->tb_path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 FIRST_PATH_ELEMENT_OFFSET)->b_blocknr ==
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001095 SB_ROOT_BLOCK(tb->tb_sb)) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001096 *pfather = *pcom_father = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001097 return CARRY_ON;
1098 }
1099 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001102 RFALSE(B_LEVEL(*pcom_father) <= DISK_LEAF_NODE_LEVEL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001103 "PAP-8185: (%b %z) level too small",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001104 *pcom_father, *pcom_father);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 /* Check whether the common parent is locked. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001108 if (buffer_locked(*pcom_father)) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001109
1110 /* Release the write lock while the buffer is busy */
Jeff Mahoney278f6672013-08-08 17:34:46 -04001111 int depth = reiserfs_write_unlock_nested(tb->tb_sb);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001112 __wait_on_buffer(*pcom_father);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001113 reiserfs_write_lock_nested(tb->tb_sb, depth);
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001114 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001115 brelse(*pcom_father);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 return REPEAT_SEARCH;
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Jeff Mahoney098297b2014-04-23 10:00:36 -04001120 /*
1121 * So, we got common parent of the current node and its
1122 * left/right neighbor. Now we are getting the parent of the
1123 * left/right neighbor.
1124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001126 /* Form key to get parent of the left/right neighbor. */
1127 le_key2cpu_key(&s_lr_father_key,
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001128 internal_key(*pcom_father,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001129 (c_lr_par ==
Jeff Mahoneyee939612009-03-30 14:02:50 -04001130 LEFT_PARENTS) ? (tb->lkey[h - 1] =
1131 position -
1132 1) : (tb->rkey[h -
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 1] =
Jeff Mahoneyee939612009-03-30 14:02:50 -04001134 position)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001136 if (c_lr_par == LEFT_PARENTS)
1137 decrement_key(&s_lr_father_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001139 if (search_by_key
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001140 (tb->tb_sb, &s_lr_father_key, &s_path_to_neighbor_father,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001141 h + 1) == IO_ERROR)
Jeff Mahoney098297b2014-04-23 10:00:36 -04001142 /* path is released */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001145 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -04001146 pathrelse(&s_path_to_neighbor_father);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001147 brelse(*pcom_father);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 return REPEAT_SEARCH;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001151 *pfather = PATH_PLAST_BUFFER(&s_path_to_neighbor_father);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001152
Jeff Mahoneyee939612009-03-30 14:02:50 -04001153 RFALSE(B_LEVEL(*pfather) != h + 1,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001154 "PAP-8190: (%b %z) level too small", *pfather, *pfather);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001155 RFALSE(s_path_to_neighbor_father.path_length <
1156 FIRST_PATH_ELEMENT_OFFSET, "PAP-8192: path length is too small");
1157
1158 s_path_to_neighbor_father.path_length--;
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -04001159 pathrelse(&s_path_to_neighbor_father);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001160 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Jeff Mahoney098297b2014-04-23 10:00:36 -04001163/*
1164 * Get parents of neighbors of node in the path(S[path_offset]) and
1165 * common parents of S[path_offset] and L[path_offset]/R[path_offset]:
1166 * F[path_offset], FL[path_offset], FR[path_offset], CFL[path_offset],
1167 * CFR[path_offset].
1168 * Calculate numbers of left and right delimiting keys position:
1169 * lkey[path_offset], rkey[path_offset].
1170 * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked
1171 * CARRY_ON - schedule didn't occur while the function worked
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001173static int get_parents(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001175 struct treepath *path = tb->tb_path;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001176 int position,
1177 ret,
1178 path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001179 struct buffer_head *curf, *curcf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001181 /* Current node is the root of the tree or will be root of the tree */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001182 if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001183 /*
1184 * The root can not have parents.
1185 * Release nodes which previously were obtained as
1186 * parents of the current node neighbors.
1187 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001188 brelse(tb->FL[h]);
1189 brelse(tb->CFL[h]);
1190 brelse(tb->FR[h]);
1191 brelse(tb->CFR[h]);
1192 tb->FL[h] = NULL;
1193 tb->CFL[h] = NULL;
1194 tb->FR[h] = NULL;
1195 tb->CFR[h] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 return CARRY_ON;
1197 }
1198
Jeff Mahoneyee939612009-03-30 14:02:50 -04001199 /* Get parent FL[path_offset] of L[path_offset]. */
1200 position = PATH_OFFSET_POSITION(path, path_offset - 1);
1201 if (position) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001202 /* Current node is not the first child of its parent. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001203 curf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
1204 curcf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001205 get_bh(curf);
1206 get_bh(curf);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001207 tb->lkey[h] = position - 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001208 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001209 /*
1210 * Calculate current parent of L[path_offset], which is the
1211 * left neighbor of the current node. Calculate current
1212 * common parent of L[path_offset] and the current node.
1213 * Note that CFL[path_offset] not equal FL[path_offset] and
1214 * CFL[path_offset] not equal F[path_offset].
1215 * Calculate lkey[path_offset].
1216 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001217 if ((ret = get_far_parent(tb, h + 1, &curf,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001218 &curcf,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219 LEFT_PARENTS)) != CARRY_ON)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001220 return ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001221 }
1222
Jeff Mahoneyee939612009-03-30 14:02:50 -04001223 brelse(tb->FL[h]);
1224 tb->FL[h] = curf; /* New initialization of FL[h]. */
1225 brelse(tb->CFL[h]);
1226 tb->CFL[h] = curcf; /* New initialization of CFL[h]. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001228 RFALSE((curf && !B_IS_IN_TREE(curf)) ||
1229 (curcf && !B_IS_IN_TREE(curcf)),
1230 "PAP-8195: FL (%b) or CFL (%b) is invalid", curf, curcf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jeff Mahoney098297b2014-04-23 10:00:36 -04001232 /* Get parent FR[h] of R[h]. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Jeff Mahoney098297b2014-04-23 10:00:36 -04001234 /* Current node is the last child of F[h]. FR[h] != F[h]. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001235 if (position == B_NR_ITEMS(PATH_H_PBUFFER(path, h + 1))) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001236 /*
1237 * Calculate current parent of R[h], which is the right
1238 * neighbor of F[h]. Calculate current common parent of
1239 * R[h] and current node. Note that CFR[h] not equal
1240 * FR[path_offset] and CFR[h] not equal F[h].
1241 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001242 if ((ret =
1243 get_far_parent(tb, h + 1, &curf, &curcf,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 RIGHT_PARENTS)) != CARRY_ON)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001245 return ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001247 /* Current node is not the last child of its parent F[h]. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001248 curf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
1249 curcf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001250 get_bh(curf);
1251 get_bh(curf);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001252 tb->rkey[h] = position;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jeff Mahoneyee939612009-03-30 14:02:50 -04001255 brelse(tb->FR[h]);
1256 /* New initialization of FR[path_offset]. */
1257 tb->FR[h] = curf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jeff Mahoneyee939612009-03-30 14:02:50 -04001259 brelse(tb->CFR[h]);
1260 /* New initialization of CFR[path_offset]. */
1261 tb->CFR[h] = curcf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001263 RFALSE((curf && !B_IS_IN_TREE(curf)) ||
1264 (curcf && !B_IS_IN_TREE(curcf)),
1265 "PAP-8205: FR (%b) or CFR (%b) is invalid", curf, curcf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266
1267 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Jeff Mahoney098297b2014-04-23 10:00:36 -04001270/*
1271 * it is possible to remove node as result of shiftings to
1272 * neighbors even when we insert or paste item.
1273 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001274static inline int can_node_be_removed(int mode, int lfree, int sfree, int rfree,
1275 struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277 struct buffer_head *Sh = PATH_H_PBUFFER(tb->tb_path, h);
1278 int levbytes = tb->insert_size[h];
1279 struct item_head *ih;
1280 struct reiserfs_key *r_key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001282 ih = item_head(Sh, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001283 if (tb->CFR[h])
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001284 r_key = internal_key(tb->CFR[h], tb->rkey[h]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285
1286 if (lfree + rfree + sfree < MAX_CHILD_SIZE(Sh) + levbytes
1287 /* shifting may merge items which might save space */
1288 -
1289 ((!h
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001290 && op_is_left_mergeable(&ih->ih_key, Sh->b_size)) ? IH_SIZE : 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291 -
1292 ((!h && r_key
1293 && op_is_left_mergeable(r_key, Sh->b_size)) ? IH_SIZE : 0)
1294 + ((h) ? KEY_SIZE : 0)) {
1295 /* node can not be removed */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001296 if (sfree >= levbytes) {
1297 /* new item fits into node S[h] without any shifting */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 if (!h)
1299 tb->s0num =
1300 B_NR_ITEMS(Sh) +
1301 ((mode == M_INSERT) ? 1 : 0);
1302 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
1303 return NO_BALANCING_NEEDED;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001306 PROC_INFO_INC(tb->tb_sb, can_node_be_removed[h]);
1307 return !NO_BALANCING_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Jeff Mahoney098297b2014-04-23 10:00:36 -04001310/*
1311 * Check whether current node S[h] is balanced when increasing its size by
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * Inserting or Pasting.
1313 * Calculate parameters for balancing for current level h.
1314 * Parameters:
1315 * tb tree_balance structure;
1316 * h current level of the node;
1317 * inum item number in S[h];
1318 * mode i - insert, p - paste;
Jeff Mahoney0222e652009-03-30 14:02:44 -04001319 * Returns: 1 - schedule occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * 0 - balancing for higher levels needed;
1321 * -1 - no balancing for higher levels needed;
1322 * -2 - no disk space.
1323 */
1324/* ip means Inserting or Pasting */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001325static int ip_check_balance(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001327 struct virtual_node *vn = tb->tb_vn;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001328 /*
1329 * Number of bytes that must be inserted into (value is negative
1330 * if bytes are deleted) buffer which contains node being balanced.
1331 * The mnemonic is that the attempted change in node space used
1332 * level is levbytes bytes.
1333 */
1334 int levbytes;
1335 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337 int lfree, sfree, rfree /* free space in L, S and R */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Jeff Mahoney098297b2014-04-23 10:00:36 -04001339 /*
1340 * nver is short for number of vertixes, and lnver is the number if
1341 * we shift to the left, rnver is the number if we shift to the
1342 * right, and lrnver is the number if we shift in both directions.
1343 * The goal is to minimize first the number of vertixes, and second,
1344 * the number of vertixes whose contents are changed by shifting,
1345 * and third the number of uncached vertixes whose contents are
1346 * changed by shifting and must be read from disk.
1347 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348 int nver, lnver, rnver, lrnver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jeff Mahoney098297b2014-04-23 10:00:36 -04001350 /*
1351 * used at leaf level only, S0 = S[0] is the node being balanced,
1352 * sInum [ I = 0,1,2 ] is the number of items that will
1353 * remain in node SI after balancing. S1 and S2 are new
1354 * nodes that might be created.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001356
1357 /*
1358 * we perform 8 calls to get_num_ver(). For each call we
1359 * calculate five parameters. where 4th parameter is s1bytes
1360 * and 5th - s2bytes
1361 *
1362 * s0num, s1num, s2num for 8 cases
1363 * 0,1 - do not shift and do not shift but bottle
1364 * 2 - shift only whole item to left
1365 * 3 - shift to left and bottle as much as possible
1366 * 4,5 - shift to right (whole items and as much as possible
1367 * 6,7 - shift to both directions (whole items and as much as possible)
1368 */
1369 short snum012[40] = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001371 /* Sh is the node whose balance is currently being checked */
1372 struct buffer_head *Sh;
1373
1374 Sh = PATH_H_PBUFFER(tb->tb_path, h);
1375 levbytes = tb->insert_size[h];
1376
1377 /* Calculate balance parameters for creating new root. */
1378 if (!Sh) {
1379 if (!h)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001380 reiserfs_panic(tb->tb_sb, "vs-8210",
1381 "S[0] can not be 0");
Jeff Mahoneyee939612009-03-30 14:02:50 -04001382 switch (ret = get_empty_nodes(tb, h)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001383 /* no balancing for higher levels needed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 case CARRY_ON:
1385 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001386 return NO_BALANCING_NEEDED;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001387
1388 case NO_DISK_SPACE:
1389 case REPEAT_SEARCH:
Jeff Mahoneyee939612009-03-30 14:02:50 -04001390 return ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001391 default:
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001392 reiserfs_panic(tb->tb_sb, "vs-8215", "incorrect "
1393 "return value of get_empty_nodes");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001394 }
1395 }
1396
Jeff Mahoney098297b2014-04-23 10:00:36 -04001397 /* get parents of S[h] neighbors. */
1398 ret = get_parents(tb, h);
1399 if (ret != CARRY_ON)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001400 return ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001401
1402 sfree = B_FREE_SPACE(Sh);
1403
1404 /* get free space of neighbors */
1405 rfree = get_rfree(tb, h);
1406 lfree = get_lfree(tb, h);
1407
Jeff Mahoney098297b2014-04-23 10:00:36 -04001408 /* and new item fits into node S[h] without any shifting */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001409 if (can_node_be_removed(vn->vn_mode, lfree, sfree, rfree, tb, h) ==
1410 NO_BALANCING_NEEDED)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001411 return NO_BALANCING_NEEDED;
1412
1413 create_virtual_node(tb, h);
1414
Jeff Mahoney0222e652009-03-30 14:02:44 -04001415 /*
Jeff Mahoney098297b2014-04-23 10:00:36 -04001416 * determine maximal number of items we can shift to the left
1417 * neighbor (in tb structure) and the maximal number of bytes
1418 * that can flow to the left neighbor from the left most liquid
1419 * item that cannot be shifted from S[0] entirely (returned value)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001420 */
1421 check_left(tb, h, lfree);
1422
1423 /*
Jeff Mahoney098297b2014-04-23 10:00:36 -04001424 * determine maximal number of items we can shift to the right
1425 * neighbor (in tb structure) and the maximal number of bytes
1426 * that can flow to the right neighbor from the right most liquid
1427 * item that cannot be shifted from S[0] entirely (returned value)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 */
1429 check_right(tb, h, rfree);
1430
Jeff Mahoney098297b2014-04-23 10:00:36 -04001431 /*
1432 * all contents of internal node S[h] can be moved into its
1433 * neighbors, S[h] will be removed after balancing
1434 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001435 if (h && (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1)) {
1436 int to_r;
1437
Jeff Mahoney098297b2014-04-23 10:00:36 -04001438 /*
1439 * Since we are working on internal nodes, and our internal
1440 * nodes have fixed size entries, then we can balance by the
1441 * number of items rather than the space they consume. In this
1442 * routine we set the left node equal to the right node,
1443 * allowing a difference of less than or equal to 1 child
1444 * pointer.
1445 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001446 to_r =
1447 ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] - tb->rnum[h] +
1448 vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 -
1449 tb->rnum[h]);
1450 set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r, 0, NULL,
1451 -1, -1);
1452 return CARRY_ON;
1453 }
1454
Jeff Mahoney098297b2014-04-23 10:00:36 -04001455 /*
1456 * this checks balance condition, that any two neighboring nodes
1457 * can not fit in one node
1458 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001459 RFALSE(h &&
1460 (tb->lnum[h] >= vn->vn_nr_item + 1 ||
1461 tb->rnum[h] >= vn->vn_nr_item + 1),
1462 "vs-8220: tree is not balanced on internal level");
1463 RFALSE(!h && ((tb->lnum[h] >= vn->vn_nr_item && (tb->lbytes == -1)) ||
1464 (tb->rnum[h] >= vn->vn_nr_item && (tb->rbytes == -1))),
1465 "vs-8225: tree is not balanced on leaf level");
1466
Jeff Mahoney098297b2014-04-23 10:00:36 -04001467 /*
1468 * all contents of S[0] can be moved into its neighbors
1469 * S[0] will be removed after balancing.
1470 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001471 if (!h && is_leaf_removable(tb))
1472 return CARRY_ON;
1473
Jeff Mahoney098297b2014-04-23 10:00:36 -04001474 /*
1475 * why do we perform this check here rather than earlier??
1476 * Answer: we can win 1 node in some cases above. Moreover we
1477 * checked it above, when we checked, that S[0] is not removable
1478 * in principle
1479 */
1480
1481 /* new item fits into node S[h] without any shifting */
1482 if (sfree >= levbytes) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001483 if (!h)
1484 tb->s0num = vn->vn_nr_item;
1485 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
1486 return NO_BALANCING_NEEDED;
1487 }
1488
1489 {
1490 int lpar, rpar, nset, lset, rset, lrset;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001491 /* regular overflowing of the node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001492
Jeff Mahoney098297b2014-04-23 10:00:36 -04001493 /*
1494 * get_num_ver works in 2 modes (FLOW & NO_FLOW)
1495 * lpar, rpar - number of items we can shift to left/right
1496 * neighbor (including splitting item)
1497 * nset, lset, rset, lrset - shows, whether flowing items
1498 * give better packing
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500#define FLOW 1
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001501#define NO_FLOW 0 /* do not any splitting */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Jeff Mahoney098297b2014-04-23 10:00:36 -04001503 /* we choose one of the following */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504#define NOTHING_SHIFT_NO_FLOW 0
1505#define NOTHING_SHIFT_FLOW 5
1506#define LEFT_SHIFT_NO_FLOW 10
1507#define LEFT_SHIFT_FLOW 15
1508#define RIGHT_SHIFT_NO_FLOW 20
1509#define RIGHT_SHIFT_FLOW 25
1510#define LR_SHIFT_NO_FLOW 30
1511#define LR_SHIFT_FLOW 35
1512
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001513 lpar = tb->lnum[h];
1514 rpar = tb->rnum[h];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Jeff Mahoney098297b2014-04-23 10:00:36 -04001516 /*
1517 * calculate number of blocks S[h] must be split into when
1518 * nothing is shifted to the neighbors, as well as number of
1519 * items in each part of the split node (s012 numbers),
1520 * and number of bytes (s1bytes) of the shared drop which
1521 * flow to S1 if any
1522 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001523 nset = NOTHING_SHIFT_NO_FLOW;
1524 nver = get_num_ver(vn->vn_mode, tb, h,
1525 0, -1, h ? vn->vn_nr_item : 0, -1,
1526 snum012, NO_FLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001528 if (!h) {
1529 int nver1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Jeff Mahoney098297b2014-04-23 10:00:36 -04001531 /*
1532 * note, that in this case we try to bottle
1533 * between S[0] and S1 (S1 - the first new node)
1534 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001535 nver1 = get_num_ver(vn->vn_mode, tb, h,
1536 0, -1, 0, -1,
1537 snum012 + NOTHING_SHIFT_FLOW, FLOW);
1538 if (nver > nver1)
1539 nset = NOTHING_SHIFT_FLOW, nver = nver1;
1540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jeff Mahoney098297b2014-04-23 10:00:36 -04001542 /*
1543 * calculate number of blocks S[h] must be split into when
1544 * l_shift_num first items and l_shift_bytes of the right
1545 * most liquid item to be shifted are shifted to the left
1546 * neighbor, as well as number of items in each part of the
1547 * splitted node (s012 numbers), and number of bytes
1548 * (s1bytes) of the shared drop which flow to S1 if any
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001549 */
1550 lset = LEFT_SHIFT_NO_FLOW;
1551 lnver = get_num_ver(vn->vn_mode, tb, h,
1552 lpar - ((h || tb->lbytes == -1) ? 0 : 1),
1553 -1, h ? vn->vn_nr_item : 0, -1,
1554 snum012 + LEFT_SHIFT_NO_FLOW, NO_FLOW);
1555 if (!h) {
1556 int lnver1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001558 lnver1 = get_num_ver(vn->vn_mode, tb, h,
1559 lpar -
1560 ((tb->lbytes != -1) ? 1 : 0),
1561 tb->lbytes, 0, -1,
1562 snum012 + LEFT_SHIFT_FLOW, FLOW);
1563 if (lnver > lnver1)
1564 lset = LEFT_SHIFT_FLOW, lnver = lnver1;
1565 }
1566
Jeff Mahoney098297b2014-04-23 10:00:36 -04001567 /*
1568 * calculate number of blocks S[h] must be split into when
1569 * r_shift_num first items and r_shift_bytes of the left most
1570 * liquid item to be shifted are shifted to the right neighbor,
1571 * as well as number of items in each part of the splitted
1572 * node (s012 numbers), and number of bytes (s1bytes) of the
1573 * shared drop which flow to S1 if any
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001574 */
1575 rset = RIGHT_SHIFT_NO_FLOW;
1576 rnver = get_num_ver(vn->vn_mode, tb, h,
1577 0, -1,
1578 h ? (vn->vn_nr_item - rpar) : (rpar -
1579 ((tb->
1580 rbytes !=
1581 -1) ? 1 :
1582 0)), -1,
1583 snum012 + RIGHT_SHIFT_NO_FLOW, NO_FLOW);
1584 if (!h) {
1585 int rnver1;
1586
1587 rnver1 = get_num_ver(vn->vn_mode, tb, h,
1588 0, -1,
1589 (rpar -
1590 ((tb->rbytes != -1) ? 1 : 0)),
1591 tb->rbytes,
1592 snum012 + RIGHT_SHIFT_FLOW, FLOW);
1593
1594 if (rnver > rnver1)
1595 rset = RIGHT_SHIFT_FLOW, rnver = rnver1;
1596 }
1597
Jeff Mahoney098297b2014-04-23 10:00:36 -04001598 /*
1599 * calculate number of blocks S[h] must be split into when
1600 * items are shifted in both directions, as well as number
1601 * of items in each part of the splitted node (s012 numbers),
1602 * and number of bytes (s1bytes) of the shared drop which
1603 * flow to S1 if any
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001604 */
1605 lrset = LR_SHIFT_NO_FLOW;
1606 lrnver = get_num_ver(vn->vn_mode, tb, h,
1607 lpar - ((h || tb->lbytes == -1) ? 0 : 1),
1608 -1,
1609 h ? (vn->vn_nr_item - rpar) : (rpar -
1610 ((tb->
1611 rbytes !=
1612 -1) ? 1 :
1613 0)), -1,
1614 snum012 + LR_SHIFT_NO_FLOW, NO_FLOW);
1615 if (!h) {
1616 int lrnver1;
1617
1618 lrnver1 = get_num_ver(vn->vn_mode, tb, h,
1619 lpar -
1620 ((tb->lbytes != -1) ? 1 : 0),
1621 tb->lbytes,
1622 (rpar -
1623 ((tb->rbytes != -1) ? 1 : 0)),
1624 tb->rbytes,
1625 snum012 + LR_SHIFT_FLOW, FLOW);
1626 if (lrnver > lrnver1)
1627 lrset = LR_SHIFT_FLOW, lrnver = lrnver1;
1628 }
1629
Jeff Mahoney098297b2014-04-23 10:00:36 -04001630 /*
1631 * Our general shifting strategy is:
1632 * 1) to minimized number of new nodes;
1633 * 2) to minimized number of neighbors involved in shifting;
1634 * 3) to minimized number of disk reads;
1635 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001636
1637 /* we can win TWO or ONE nodes by shifting in both directions */
1638 if (lrnver < lnver && lrnver < rnver) {
1639 RFALSE(h &&
1640 (tb->lnum[h] != 1 ||
1641 tb->rnum[h] != 1 ||
1642 lrnver != 1 || rnver != 2 || lnver != 2
1643 || h != 1), "vs-8230: bad h");
1644 if (lrset == LR_SHIFT_FLOW)
1645 set_parameters(tb, h, tb->lnum[h], tb->rnum[h],
1646 lrnver, snum012 + lrset,
1647 tb->lbytes, tb->rbytes);
1648 else
1649 set_parameters(tb, h,
1650 tb->lnum[h] -
1651 ((tb->lbytes == -1) ? 0 : 1),
1652 tb->rnum[h] -
1653 ((tb->rbytes == -1) ? 0 : 1),
1654 lrnver, snum012 + lrset, -1, -1);
1655
1656 return CARRY_ON;
1657 }
1658
Jeff Mahoney098297b2014-04-23 10:00:36 -04001659 /*
1660 * if shifting doesn't lead to better packing
1661 * then don't shift
1662 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001663 if (nver == lrnver) {
1664 set_parameters(tb, h, 0, 0, nver, snum012 + nset, -1,
1665 -1);
1666 return CARRY_ON;
1667 }
1668
Jeff Mahoney098297b2014-04-23 10:00:36 -04001669 /*
1670 * now we know that for better packing shifting in only one
1671 * direction either to the left or to the right is required
1672 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001673
Jeff Mahoney098297b2014-04-23 10:00:36 -04001674 /*
1675 * if shifting to the left is better than
1676 * shifting to the right
1677 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001678 if (lnver < rnver) {
1679 SET_PAR_SHIFT_LEFT;
1680 return CARRY_ON;
1681 }
1682
Jeff Mahoney098297b2014-04-23 10:00:36 -04001683 /*
1684 * if shifting to the right is better than
1685 * shifting to the left
1686 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001687 if (lnver > rnver) {
1688 SET_PAR_SHIFT_RIGHT;
1689 return CARRY_ON;
1690 }
1691
Jeff Mahoney098297b2014-04-23 10:00:36 -04001692 /*
1693 * now shifting in either direction gives the same number
1694 * of nodes and we can make use of the cached neighbors
1695 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001696 if (is_left_neighbor_in_cache(tb, h)) {
1697 SET_PAR_SHIFT_LEFT;
1698 return CARRY_ON;
1699 }
1700
Jeff Mahoney098297b2014-04-23 10:00:36 -04001701 /*
1702 * shift to the right independently on whether the
1703 * right neighbor in cache or not
1704 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001705 SET_PAR_SHIFT_RIGHT;
1706 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Jeff Mahoney098297b2014-04-23 10:00:36 -04001710/*
1711 * Check whether current node S[h] is balanced when Decreasing its size by
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * Deleting or Cutting for INTERNAL node of S+tree.
1713 * Calculate parameters for balancing for current level h.
1714 * Parameters:
1715 * tb tree_balance structure;
1716 * h current level of the node;
1717 * inum item number in S[h];
1718 * mode i - insert, p - paste;
Jeff Mahoney0222e652009-03-30 14:02:44 -04001719 * Returns: 1 - schedule occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * 0 - balancing for higher levels needed;
1721 * -1 - no balancing for higher levels needed;
1722 * -2 - no disk space.
1723 *
1724 * Note: Items of internal nodes have fixed size, so the balance condition for
1725 * the internal part of S+tree is as for the B-trees.
1726 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001727static int dc_check_balance_internal(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001729 struct virtual_node *vn = tb->tb_vn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Jeff Mahoney098297b2014-04-23 10:00:36 -04001731 /*
1732 * Sh is the node whose balance is currently being checked,
1733 * and Fh is its father.
1734 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001735 struct buffer_head *Sh, *Fh;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001736 int maxsize, ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001737 int lfree, rfree /* free space in L and R */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001739 Sh = PATH_H_PBUFFER(tb->tb_path, h);
1740 Fh = PATH_H_PPARENT(tb->tb_path, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001742 maxsize = MAX_CHILD_SIZE(Sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jeff Mahoney098297b2014-04-23 10:00:36 -04001744 /*
1745 * using tb->insert_size[h], which is negative in this case,
1746 * create_virtual_node calculates:
1747 * new_nr_item = number of items node would have if operation is
1748 * performed without balancing (new_nr_item);
1749 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001750 create_virtual_node(tb, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001752 if (!Fh) { /* S[h] is the root. */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001753 /* no balancing for higher levels needed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001754 if (vn->vn_nr_item > 0) {
1755 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001756 return NO_BALANCING_NEEDED;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001757 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001758 /*
1759 * new_nr_item == 0.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001760 * Current root will be deleted resulting in
Jeff Mahoney098297b2014-04-23 10:00:36 -04001761 * decrementing the tree height.
1762 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001763 set_parameters(tb, h, 0, 0, 0, NULL, -1, -1);
1764 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
Jeff Mahoneyee939612009-03-30 14:02:50 -04001767 if ((ret = get_parents(tb, h)) != CARRY_ON)
1768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001770 /* get free space of neighbors */
1771 rfree = get_rfree(tb, h);
1772 lfree = get_lfree(tb, h);
1773
1774 /* determine maximal number of items we can fit into neighbors */
1775 check_left(tb, h, lfree);
1776 check_right(tb, h, rfree);
1777
Jeff Mahoney098297b2014-04-23 10:00:36 -04001778 /*
1779 * Balance condition for the internal node is valid.
1780 * In this case we balance only if it leads to better packing.
1781 */
1782 if (vn->vn_nr_item >= MIN_NR_KEY(Sh)) {
1783 /*
1784 * Here we join S[h] with one of its neighbors,
1785 * which is impossible with greater values of new_nr_item.
1786 */
1787 if (vn->vn_nr_item == MIN_NR_KEY(Sh)) {
1788 /* All contents of S[h] can be moved to L[h]. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001789 if (tb->lnum[h] >= vn->vn_nr_item + 1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001790 int n;
1791 int order_L;
1792
1793 order_L =
1794 ((n =
1795 PATH_H_B_ITEM_ORDER(tb->tb_path,
1796 h)) ==
1797 0) ? B_NR_ITEMS(tb->FL[h]) : n - 1;
1798 n = dc_size(B_N_CHILD(tb->FL[h], order_L)) /
1799 (DC_SIZE + KEY_SIZE);
1800 set_parameters(tb, h, -n - 1, 0, 0, NULL, -1,
1801 -1);
1802 return CARRY_ON;
1803 }
1804
Jeff Mahoney098297b2014-04-23 10:00:36 -04001805 /* All contents of S[h] can be moved to R[h]. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001806 if (tb->rnum[h] >= vn->vn_nr_item + 1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001807 int n;
1808 int order_R;
1809
1810 order_R =
1811 ((n =
1812 PATH_H_B_ITEM_ORDER(tb->tb_path,
1813 h)) ==
1814 B_NR_ITEMS(Fh)) ? 0 : n + 1;
1815 n = dc_size(B_N_CHILD(tb->FR[h], order_R)) /
1816 (DC_SIZE + KEY_SIZE);
1817 set_parameters(tb, h, 0, -n - 1, 0, NULL, -1,
1818 -1);
1819 return CARRY_ON;
1820 }
1821 }
1822
Jeff Mahoney098297b2014-04-23 10:00:36 -04001823 /*
1824 * All contents of S[h] can be moved to the neighbors
1825 * (L[h] & R[h]).
1826 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001827 if (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001828 int to_r;
1829
1830 to_r =
1831 ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] -
1832 tb->rnum[h] + vn->vn_nr_item + 1) / 2 -
1833 (MAX_NR_KEY(Sh) + 1 - tb->rnum[h]);
1834 set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r,
1835 0, NULL, -1, -1);
1836 return CARRY_ON;
1837 }
1838
1839 /* Balancing does not lead to better packing. */
1840 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
1841 return NO_BALANCING_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843
Jeff Mahoney098297b2014-04-23 10:00:36 -04001844 /*
1845 * Current node contain insufficient number of items.
1846 * Balancing is required.
1847 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001848 /* Check whether we can merge S[h] with left neighbor. */
1849 if (tb->lnum[h] >= vn->vn_nr_item + 1)
1850 if (is_left_neighbor_in_cache(tb, h)
1851 || tb->rnum[h] < vn->vn_nr_item + 1 || !tb->FR[h]) {
1852 int n;
1853 int order_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001855 order_L =
1856 ((n =
1857 PATH_H_B_ITEM_ORDER(tb->tb_path,
1858 h)) ==
1859 0) ? B_NR_ITEMS(tb->FL[h]) : n - 1;
1860 n = dc_size(B_N_CHILD(tb->FL[h], order_L)) / (DC_SIZE +
1861 KEY_SIZE);
1862 set_parameters(tb, h, -n - 1, 0, 0, NULL, -1, -1);
1863 return CARRY_ON;
1864 }
1865
1866 /* Check whether we can merge S[h] with right neighbor. */
1867 if (tb->rnum[h] >= vn->vn_nr_item + 1) {
1868 int n;
1869 int order_R;
1870
1871 order_R =
1872 ((n =
1873 PATH_H_B_ITEM_ORDER(tb->tb_path,
1874 h)) == B_NR_ITEMS(Fh)) ? 0 : (n + 1);
1875 n = dc_size(B_N_CHILD(tb->FR[h], order_R)) / (DC_SIZE +
1876 KEY_SIZE);
1877 set_parameters(tb, h, 0, -n - 1, 0, NULL, -1, -1);
1878 return CARRY_ON;
1879 }
1880
1881 /* All contents of S[h] can be moved to the neighbors (L[h] & R[h]). */
1882 if (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1) {
1883 int to_r;
1884
1885 to_r =
1886 ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] - tb->rnum[h] +
1887 vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 -
1888 tb->rnum[h]);
1889 set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r, 0, NULL,
1890 -1, -1);
1891 return CARRY_ON;
1892 }
1893
1894 /* For internal nodes try to borrow item from a neighbor */
1895 RFALSE(!tb->FL[h] && !tb->FR[h], "vs-8235: trying to borrow for root");
1896
1897 /* Borrow one or two items from caching neighbor */
1898 if (is_left_neighbor_in_cache(tb, h) || !tb->FR[h]) {
1899 int from_l;
1900
1901 from_l =
1902 (MAX_NR_KEY(Sh) + 1 - tb->lnum[h] + vn->vn_nr_item +
1903 1) / 2 - (vn->vn_nr_item + 1);
1904 set_parameters(tb, h, -from_l, 0, 1, NULL, -1, -1);
1905 return CARRY_ON;
1906 }
1907
1908 set_parameters(tb, h, 0,
1909 -((MAX_NR_KEY(Sh) + 1 - tb->rnum[h] + vn->vn_nr_item +
1910 1) / 2 - (vn->vn_nr_item + 1)), 1, NULL, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Jeff Mahoney098297b2014-04-23 10:00:36 -04001914/*
1915 * Check whether current node S[h] is balanced when Decreasing its size by
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 * Deleting or Truncating for LEAF node of S+tree.
1917 * Calculate parameters for balancing for current level h.
1918 * Parameters:
1919 * tb tree_balance structure;
1920 * h current level of the node;
1921 * inum item number in S[h];
1922 * mode i - insert, p - paste;
Jeff Mahoney0222e652009-03-30 14:02:44 -04001923 * Returns: 1 - schedule occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * 0 - balancing for higher levels needed;
1925 * -1 - no balancing for higher levels needed;
1926 * -2 - no disk space.
1927 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001928static int dc_check_balance_leaf(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001930 struct virtual_node *vn = tb->tb_vn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Jeff Mahoney098297b2014-04-23 10:00:36 -04001932 /*
1933 * Number of bytes that must be deleted from
1934 * (value is negative if bytes are deleted) buffer which
1935 * contains node being balanced. The mnemonic is that the
1936 * attempted change in node space used level is levbytes bytes.
1937 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001938 int levbytes;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001939
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001940 /* the maximal item size */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001941 int maxsize, ret;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001942
1943 /*
1944 * S0 is the node whose balance is currently being checked,
1945 * and F0 is its father.
1946 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001947 struct buffer_head *S0, *F0;
1948 int lfree, rfree /* free space in L and R */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001950 S0 = PATH_H_PBUFFER(tb->tb_path, 0);
1951 F0 = PATH_H_PPARENT(tb->tb_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001953 levbytes = tb->insert_size[h];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001955 maxsize = MAX_CHILD_SIZE(S0); /* maximal possible size of an item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001957 if (!F0) { /* S[0] is the root now. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001959 RFALSE(-levbytes >= maxsize - B_FREE_SPACE(S0),
1960 "vs-8240: attempt to create empty buffer tree");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001962 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
1963 return NO_BALANCING_NEEDED;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Jeff Mahoneyee939612009-03-30 14:02:50 -04001966 if ((ret = get_parents(tb, h)) != CARRY_ON)
1967 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001969 /* get free space of neighbors */
1970 rfree = get_rfree(tb, h);
1971 lfree = get_lfree(tb, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001973 create_virtual_node(tb, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001975 /* if 3 leaves can be merge to one, set parameters and return */
1976 if (are_leaves_removable(tb, lfree, rfree))
1977 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Jeff Mahoney098297b2014-04-23 10:00:36 -04001979 /*
1980 * determine maximal number of items we can shift to the left/right
1981 * neighbor and the maximal number of bytes that can flow to the
1982 * left/right neighbor from the left/right most liquid item that
1983 * cannot be shifted from S[0] entirely
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001984 */
1985 check_left(tb, h, lfree);
1986 check_right(tb, h, rfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001988 /* check whether we can merge S with left neighbor. */
1989 if (tb->lnum[0] >= vn->vn_nr_item && tb->lbytes == -1)
1990 if (is_left_neighbor_in_cache(tb, h) || ((tb->rnum[0] - ((tb->rbytes == -1) ? 0 : 1)) < vn->vn_nr_item) || /* S can not be merged with R */
1991 !tb->FR[h]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001993 RFALSE(!tb->FL[h],
1994 "vs-8245: dc_check_balance_leaf: FL[h] must exist");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001996 /* set parameter to merge S[0] with its left neighbor */
1997 set_parameters(tb, h, -1, 0, 0, NULL, -1, -1);
1998 return CARRY_ON;
1999 }
2000
2001 /* check whether we can merge S[0] with right neighbor. */
2002 if (tb->rnum[0] >= vn->vn_nr_item && tb->rbytes == -1) {
2003 set_parameters(tb, h, 0, -1, 0, NULL, -1, -1);
2004 return CARRY_ON;
2005 }
2006
Jeff Mahoney098297b2014-04-23 10:00:36 -04002007 /*
2008 * All contents of S[0] can be moved to the neighbors (L[0] & R[0]).
2009 * Set parameters and return
2010 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002011 if (is_leaf_removable(tb))
2012 return CARRY_ON;
2013
2014 /* Balancing is not required. */
2015 tb->s0num = vn->vn_nr_item;
2016 set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
2017 return NO_BALANCING_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Jeff Mahoney098297b2014-04-23 10:00:36 -04002020/*
2021 * Check whether current node S[h] is balanced when Decreasing its size by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 * Deleting or Cutting.
2023 * Calculate parameters for balancing for current level h.
2024 * Parameters:
2025 * tb tree_balance structure;
2026 * h current level of the node;
2027 * inum item number in S[h];
2028 * mode d - delete, c - cut.
Jeff Mahoney0222e652009-03-30 14:02:44 -04002029 * Returns: 1 - schedule occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 * 0 - balancing for higher levels needed;
2031 * -1 - no balancing for higher levels needed;
2032 * -2 - no disk space.
2033 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002034static int dc_check_balance(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002036 RFALSE(!(PATH_H_PBUFFER(tb->tb_path, h)),
2037 "vs-8250: S is not initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002039 if (h)
2040 return dc_check_balance_internal(tb, h);
2041 else
2042 return dc_check_balance_leaf(tb, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Jeff Mahoney098297b2014-04-23 10:00:36 -04002045/*
2046 * Check whether current node S[h] is balanced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 * Calculate parameters for balancing for current level h.
2048 * Parameters:
2049 *
2050 * tb tree_balance structure:
2051 *
Jeff Mahoney098297b2014-04-23 10:00:36 -04002052 * tb is a large structure that must be read about in the header
2053 * file at the same time as this procedure if the reader is
2054 * to successfully understand this procedure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 *
2056 * h current level of the node;
2057 * inum item number in S[h];
2058 * mode i - insert, p - paste, d - delete, c - cut.
Jeff Mahoney0222e652009-03-30 14:02:44 -04002059 * Returns: 1 - schedule occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * 0 - balancing for higher levels needed;
2061 * -1 - no balancing for higher levels needed;
2062 * -2 - no disk space.
2063 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002064static int check_balance(int mode,
2065 struct tree_balance *tb,
2066 int h,
2067 int inum,
2068 int pos_in_item,
2069 struct item_head *ins_ih, const void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002071 struct virtual_node *vn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002073 vn = tb->tb_vn = (struct virtual_node *)(tb->vn_buf);
2074 vn->vn_free_ptr = (char *)(tb->tb_vn + 1);
2075 vn->vn_mode = mode;
2076 vn->vn_affected_item_num = inum;
2077 vn->vn_pos_in_item = pos_in_item;
2078 vn->vn_ins_ih = ins_ih;
2079 vn->vn_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002081 RFALSE(mode == M_INSERT && !vn->vn_ins_ih,
2082 "vs-8255: ins_ih can not be 0 in insert mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Jeff Mahoney098297b2014-04-23 10:00:36 -04002084 /* Calculate balance parameters when size of node is increasing. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002085 if (tb->insert_size[h] > 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002086 return ip_check_balance(tb, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002088 /* Calculate balance parameters when size of node is decreasing. */
2089 return dc_check_balance(tb, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/* Check whether parent at the path is the really parent of the current node.*/
Jeff Mahoneyee939612009-03-30 14:02:50 -04002093static int get_direct_parent(struct tree_balance *tb, int h)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002094{
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002095 struct buffer_head *bh;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002096 struct treepath *path = tb->tb_path;
Jeff Mahoneyee939612009-03-30 14:02:50 -04002097 int position,
2098 path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002100 /* We are in the root or in the new root. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002101 if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002102
Jeff Mahoneyee939612009-03-30 14:02:50 -04002103 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET - 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002104 "PAP-8260: invalid offset in the path");
2105
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002106 if (PATH_OFFSET_PBUFFER(path, FIRST_PATH_ELEMENT_OFFSET)->
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002107 b_blocknr == SB_ROOT_BLOCK(tb->tb_sb)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002108 /* Root is not changed. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002109 PATH_OFFSET_PBUFFER(path, path_offset - 1) = NULL;
2110 PATH_OFFSET_POSITION(path, path_offset - 1) = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002111 return CARRY_ON;
2112 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04002113 /* Root is changed and we must recalculate the path. */
2114 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Jeff Mahoney098297b2014-04-23 10:00:36 -04002117 /* Parent in the path is not in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002118 if (!B_IS_IN_TREE
Jeff Mahoneyee939612009-03-30 14:02:50 -04002119 (bh = PATH_OFFSET_PBUFFER(path, path_offset - 1)))
Jeff Mahoney098297b2014-04-23 10:00:36 -04002120 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Jeff Mahoneyee939612009-03-30 14:02:50 -04002122 if ((position =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002123 PATH_OFFSET_POSITION(path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04002124 path_offset - 1)) > B_NR_ITEMS(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002125 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Jeff Mahoney098297b2014-04-23 10:00:36 -04002127 /* Parent in the path is not parent of the current node in the tree. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002128 if (B_N_CHILD_NUM(bh, position) !=
2129 PATH_OFFSET_PBUFFER(path, path_offset)->b_blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002130 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002132 if (buffer_locked(bh)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -04002133 int depth = reiserfs_write_unlock_nested(tb->tb_sb);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002134 __wait_on_buffer(bh);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002135 reiserfs_write_lock_nested(tb->tb_sb, depth);
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002136 if (FILESYSTEM_CHANGED_TB(tb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002137 return REPEAT_SEARCH;
2138 }
2139
Jeff Mahoney098297b2014-04-23 10:00:36 -04002140 /*
2141 * Parent in the path is unlocked and really parent
2142 * of the current node.
2143 */
2144 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Jeff Mahoney098297b2014-04-23 10:00:36 -04002147/*
2148 * Using lnum[h] and rnum[h] we should determine what neighbors
Jeff Mahoneyee939612009-03-30 14:02:50 -04002149 * of S[h] we
2150 * need in order to balance S[h], and get them if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked;
2152 * CARRY_ON - schedule didn't occur while the function worked;
2153 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002154static int get_neighbors(struct tree_balance *tb, int h)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002155{
Jeff Mahoneyee939612009-03-30 14:02:50 -04002156 int child_position,
2157 path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h + 1);
2158 unsigned long son_number;
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002159 struct super_block *sb = tb->tb_sb;
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002160 struct buffer_head *bh;
Jeff Mahoney278f6672013-08-08 17:34:46 -04002161 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Jeff Mahoneyee939612009-03-30 14:02:50 -04002163 PROC_INFO_INC(sb, get_neighbors[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Jeff Mahoneyee939612009-03-30 14:02:50 -04002165 if (tb->lnum[h]) {
2166 /* We need left neighbor to balance S[h]. */
2167 PROC_INFO_INC(sb, need_l_neighbor[h]);
2168 bh = PATH_OFFSET_PBUFFER(tb->tb_path, path_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Jeff Mahoneyee939612009-03-30 14:02:50 -04002170 RFALSE(bh == tb->FL[h] &&
2171 !PATH_OFFSET_POSITION(tb->tb_path, path_offset),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002172 "PAP-8270: invalid position in the parent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Jeff Mahoneyee939612009-03-30 14:02:50 -04002174 child_position =
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002175 (bh ==
Jeff Mahoneyee939612009-03-30 14:02:50 -04002176 tb->FL[h]) ? tb->lkey[h] : B_NR_ITEMS(tb->
2177 FL[h]);
2178 son_number = B_N_CHILD_NUM(tb->FL[h], child_position);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002179 depth = reiserfs_write_unlock_nested(tb->tb_sb);
Jeff Mahoneyee939612009-03-30 14:02:50 -04002180 bh = sb_bread(sb, son_number);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002181 reiserfs_write_lock_nested(tb->tb_sb, depth);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002182 if (!bh)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002183 return IO_ERROR;
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002184 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002185 brelse(bh);
Jeff Mahoneyee939612009-03-30 14:02:50 -04002186 PROC_INFO_INC(sb, get_neighbors_restart[h]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002187 return REPEAT_SEARCH;
2188 }
2189
Jeff Mahoneyee939612009-03-30 14:02:50 -04002190 RFALSE(!B_IS_IN_TREE(tb->FL[h]) ||
2191 child_position > B_NR_ITEMS(tb->FL[h]) ||
2192 B_N_CHILD_NUM(tb->FL[h], child_position) !=
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002193 bh->b_blocknr, "PAP-8275: invalid parent");
2194 RFALSE(!B_IS_IN_TREE(bh), "PAP-8280: invalid child");
Jeff Mahoneyee939612009-03-30 14:02:50 -04002195 RFALSE(!h &&
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002196 B_FREE_SPACE(bh) !=
2197 MAX_CHILD_SIZE(bh) -
Jeff Mahoneyee939612009-03-30 14:02:50 -04002198 dc_size(B_N_CHILD(tb->FL[0], child_position)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002199 "PAP-8290: invalid child size of left neighbor");
2200
Jeff Mahoneyee939612009-03-30 14:02:50 -04002201 brelse(tb->L[h]);
2202 tb->L[h] = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Jeff Mahoneyee939612009-03-30 14:02:50 -04002205 /* We need right neighbor to balance S[path_offset]. */
Jeff Mahoney098297b2014-04-23 10:00:36 -04002206 if (tb->rnum[h]) {
Jeff Mahoneyee939612009-03-30 14:02:50 -04002207 PROC_INFO_INC(sb, need_r_neighbor[h]);
2208 bh = PATH_OFFSET_PBUFFER(tb->tb_path, path_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Jeff Mahoneyee939612009-03-30 14:02:50 -04002210 RFALSE(bh == tb->FR[h] &&
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002211 PATH_OFFSET_POSITION(tb->tb_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04002212 path_offset) >=
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002213 B_NR_ITEMS(bh),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002214 "PAP-8295: invalid position in the parent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Jeff Mahoneyee939612009-03-30 14:02:50 -04002216 child_position =
2217 (bh == tb->FR[h]) ? tb->rkey[h] + 1 : 0;
2218 son_number = B_N_CHILD_NUM(tb->FR[h], child_position);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002219 depth = reiserfs_write_unlock_nested(tb->tb_sb);
Jeff Mahoneyee939612009-03-30 14:02:50 -04002220 bh = sb_bread(sb, son_number);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002221 reiserfs_write_lock_nested(tb->tb_sb, depth);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002222 if (!bh)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002223 return IO_ERROR;
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002224 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002225 brelse(bh);
Jeff Mahoneyee939612009-03-30 14:02:50 -04002226 PROC_INFO_INC(sb, get_neighbors_restart[h]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002227 return REPEAT_SEARCH;
2228 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04002229 brelse(tb->R[h]);
2230 tb->R[h] = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Jeff Mahoneyee939612009-03-30 14:02:50 -04002232 RFALSE(!h
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002233 && B_FREE_SPACE(bh) !=
2234 MAX_CHILD_SIZE(bh) -
Jeff Mahoneyee939612009-03-30 14:02:50 -04002235 dc_size(B_N_CHILD(tb->FR[0], child_position)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002236 "PAP-8300: invalid child size of right neighbor (%d != %d - %d)",
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002237 B_FREE_SPACE(bh), MAX_CHILD_SIZE(bh),
Jeff Mahoneyee939612009-03-30 14:02:50 -04002238 dc_size(B_N_CHILD(tb->FR[0], child_position)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002241 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002244static int get_virtual_node_size(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002246 int max_num_of_items;
2247 int max_num_of_entries;
2248 unsigned long blocksize = sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250#define MIN_NAME_LEN 1
2251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002252 max_num_of_items = (blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN);
2253 max_num_of_entries = (blocksize - BLKH_SIZE - IH_SIZE) /
2254 (DEH_SIZE + MIN_NAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002256 return sizeof(struct virtual_node) +
2257 max(max_num_of_items * sizeof(struct virtual_item),
2258 sizeof(struct virtual_item) + sizeof(struct direntry_uarea) +
2259 (max_num_of_entries - 1) * sizeof(__u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Jeff Mahoney098297b2014-04-23 10:00:36 -04002262/*
2263 * maybe we should fail balancing we are going to perform when kmalloc
2264 * fails several times. But now it will loop until kmalloc gets
2265 * required memory
2266 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002267static int get_mem_for_virtual_node(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002269 int check_fs = 0;
2270 int size;
2271 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002273 size = get_virtual_node_size(tb->tb_sb, PATH_PLAST_BUFFER(tb->tb_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Jeff Mahoney098297b2014-04-23 10:00:36 -04002275 /* we have to allocate more memory for virtual node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002276 if (size > tb->vn_buf_size) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002277 if (tb->vn_buf) {
2278 /* free memory allocated before */
Pekka Enbergd739b422006-02-01 03:06:43 -08002279 kfree(tb->vn_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002280 /* this is not needed if kfree is atomic */
2281 check_fs = 1;
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002284 /* virtual node requires now more memory */
2285 tb->vn_buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002287 /* get memory for virtual item */
Pekka Enbergd739b422006-02-01 03:06:43 -08002288 buf = kmalloc(size, GFP_ATOMIC | __GFP_NOWARN);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002289 if (!buf) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04002290 /*
2291 * getting memory with GFP_KERNEL priority may involve
2292 * balancing now (due to indirect_to_direct conversion
2293 * on dcache shrinking). So, release path and collected
2294 * resources here
2295 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002296 free_buffers_in_tb(tb);
Pekka Enbergd739b422006-02-01 03:06:43 -08002297 buf = kmalloc(size, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002298 if (!buf) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002299 tb->vn_buf_size = 0;
2300 }
2301 tb->vn_buf = buf;
2302 schedule();
2303 return REPEAT_SEARCH;
2304 }
2305
2306 tb->vn_buf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002309 if (check_fs && FILESYSTEM_CHANGED_TB(tb))
2310 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002312 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002316static void tb_buffer_sanity_check(struct super_block *sb,
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002317 struct buffer_head *bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002318 const char *descr, int level)
2319{
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002320 if (bh) {
2321 if (atomic_read(&(bh->b_count)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002323 reiserfs_panic(sb, "jmacd-1", "negative or zero "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002324 "reference counter for buffer %s[%d] "
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002325 "(%b)", descr, level, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002327 if (!buffer_uptodate(bh))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002328 reiserfs_panic(sb, "jmacd-2", "buffer is not up "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002329 "to date %s[%d] (%b)",
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002330 descr, level, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002332 if (!B_IS_IN_TREE(bh))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002333 reiserfs_panic(sb, "jmacd-3", "buffer is not "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002334 "in tree %s[%d] (%b)",
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002335 descr, level, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002337 if (bh->b_bdev != sb->s_bdev)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002338 reiserfs_panic(sb, "jmacd-4", "buffer has wrong "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002339 "device %s[%d] (%b)",
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002340 descr, level, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002342 if (bh->b_size != sb->s_blocksize)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002343 reiserfs_panic(sb, "jmacd-5", "buffer has wrong "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002344 "blocksize %s[%d] (%b)",
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002345 descr, level, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002347 if (bh->b_blocknr > SB_BLOCK_COUNT(sb))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002348 reiserfs_panic(sb, "jmacd-6", "buffer block "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002349 "number too high %s[%d] (%b)",
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002350 descr, level, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353#else
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002354static void tb_buffer_sanity_check(struct super_block *sb,
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04002355 struct buffer_head *bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002356 const char *descr, int level)
2357{;
2358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359#endif
2360
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002361static int clear_all_dirty_bits(struct super_block *s, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002363 return reiserfs_prepare_for_journal(s, bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002366static int wait_tb_buffers_until_unlocked(struct tree_balance *tb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002367{
2368 struct buffer_head *locked;
2369#ifdef CONFIG_REISERFS_CHECK
2370 int repeat_counter = 0;
2371#endif
2372 int i;
2373
2374 do {
2375
2376 locked = NULL;
2377
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002378 for (i = tb->tb_path->path_length;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002379 !locked && i > ILLEGAL_PATH_ELEMENT_OFFSET; i--) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002380 if (PATH_OFFSET_PBUFFER(tb->tb_path, i)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04002381 /*
2382 * if I understand correctly, we can only
2383 * be sure the last buffer in the path is
2384 * in the tree --clm
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002385 */
2386#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002387 if (PATH_PLAST_BUFFER(tb->tb_path) ==
2388 PATH_OFFSET_PBUFFER(tb->tb_path, i))
2389 tb_buffer_sanity_check(tb->tb_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002390 PATH_OFFSET_PBUFFER
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002391 (tb->tb_path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002392 i), "S",
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002393 tb->tb_path->
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002394 path_length - i);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002395#endif
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002396 if (!clear_all_dirty_bits(tb->tb_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002397 PATH_OFFSET_PBUFFER
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002398 (tb->tb_path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002399 i))) {
2400 locked =
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002401 PATH_OFFSET_PBUFFER(tb->tb_path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002402 i);
2403 }
2404 }
2405 }
2406
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002407 for (i = 0; !locked && i < MAX_HEIGHT && tb->insert_size[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002408 i++) {
2409
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002410 if (tb->lnum[i]) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002411
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002412 if (tb->L[i]) {
2413 tb_buffer_sanity_check(tb->tb_sb,
2414 tb->L[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002415 "L", i);
2416 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002417 (tb->tb_sb, tb->L[i]))
2418 locked = tb->L[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002419 }
2420
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002421 if (!locked && tb->FL[i]) {
2422 tb_buffer_sanity_check(tb->tb_sb,
2423 tb->FL[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002424 "FL", i);
2425 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002426 (tb->tb_sb, tb->FL[i]))
2427 locked = tb->FL[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002428 }
2429
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002430 if (!locked && tb->CFL[i]) {
2431 tb_buffer_sanity_check(tb->tb_sb,
2432 tb->CFL[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002433 "CFL", i);
2434 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002435 (tb->tb_sb, tb->CFL[i]))
2436 locked = tb->CFL[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002437 }
2438
2439 }
2440
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002441 if (!locked && (tb->rnum[i])) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002442
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002443 if (tb->R[i]) {
2444 tb_buffer_sanity_check(tb->tb_sb,
2445 tb->R[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002446 "R", i);
2447 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002448 (tb->tb_sb, tb->R[i]))
2449 locked = tb->R[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002450 }
2451
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002452 if (!locked && tb->FR[i]) {
2453 tb_buffer_sanity_check(tb->tb_sb,
2454 tb->FR[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002455 "FR", i);
2456 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002457 (tb->tb_sb, tb->FR[i]))
2458 locked = tb->FR[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002459 }
2460
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002461 if (!locked && tb->CFR[i]) {
2462 tb_buffer_sanity_check(tb->tb_sb,
2463 tb->CFR[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002464 "CFR", i);
2465 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002466 (tb->tb_sb, tb->CFR[i]))
2467 locked = tb->CFR[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002468 }
2469 }
2470 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04002471
2472 /*
2473 * as far as I can tell, this is not required. The FEB list
2474 * seems to be full of newly allocated nodes, which will
2475 * never be locked, dirty, or anything else.
2476 * To be safe, I'm putting in the checks and waits in.
2477 * For the moment, they are needed to keep the code in
2478 * journal.c from complaining about the buffer.
2479 * That code is inside CONFIG_REISERFS_CHECK as well. --clm
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002480 */
2481 for (i = 0; !locked && i < MAX_FEB_SIZE; i++) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002482 if (tb->FEB[i]) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002483 if (!clear_all_dirty_bits
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002484 (tb->tb_sb, tb->FEB[i]))
2485 locked = tb->FEB[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002486 }
2487 }
2488
2489 if (locked) {
Jeff Mahoney278f6672013-08-08 17:34:46 -04002490 int depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002491#ifdef CONFIG_REISERFS_CHECK
2492 repeat_counter++;
2493 if ((repeat_counter % 10000) == 0) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002494 reiserfs_warning(tb->tb_sb, "reiserfs-8200",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002495 "too many iterations waiting "
2496 "for buffer to unlock "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002497 "(%b)", locked);
2498
2499 /* Don't loop forever. Try to recover from possible error. */
2500
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002501 return (FILESYSTEM_CHANGED_TB(tb)) ?
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002502 REPEAT_SEARCH : CARRY_ON;
2503 }
2504#endif
Jeff Mahoney278f6672013-08-08 17:34:46 -04002505 depth = reiserfs_write_unlock_nested(tb->tb_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002506 __wait_on_buffer(locked);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002507 reiserfs_write_lock_nested(tb->tb_sb, depth);
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002508 if (FILESYSTEM_CHANGED_TB(tb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002509 return REPEAT_SEARCH;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002510 }
2511
2512 } while (locked);
2513
2514 return CARRY_ON;
2515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Jeff Mahoney098297b2014-04-23 10:00:36 -04002517/*
2518 * Prepare for balancing, that is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 * get all necessary parents, and neighbors;
2520 * analyze what and where should be moved;
2521 * get sufficient number of new nodes;
2522 * Balancing will start only after all resources will be collected at a time.
Jeff Mahoney0222e652009-03-30 14:02:44 -04002523 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 * When ported to SMP kernels, only at the last moment after all needed nodes
2525 * are collected in cache, will the resources be locked using the usual
2526 * textbook ordered lock acquisition algorithms. Note that ensuring that
Jeff Mahoney098297b2014-04-23 10:00:36 -04002527 * this code neither write locks what it does not need to write lock nor locks
2528 * out of order will be a pain in the butt that could have been avoided.
2529 * Grumble grumble. -Hans
Jeff Mahoney0222e652009-03-30 14:02:44 -04002530 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 * fix is meant in the sense of render unchanging
Jeff Mahoney0222e652009-03-30 14:02:44 -04002532 *
Jeff Mahoney098297b2014-04-23 10:00:36 -04002533 * Latency might be improved by first gathering a list of what buffers
2534 * are needed and then getting as many of them in parallel as possible? -Hans
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 *
2536 * Parameters:
2537 * op_mode i - insert, d - delete, c - cut (truncate), p - paste (append)
2538 * tb tree_balance structure;
2539 * inum item number in S[h];
2540 * pos_in_item - comment this if you can
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002541 * ins_ih item head of item being inserted
2542 * data inserted item or data to be pasted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 * Returns: 1 - schedule occurred while the function worked;
2544 * 0 - schedule didn't occur while the function worked;
Jeff Mahoney0222e652009-03-30 14:02:44 -04002545 * -1 - if no_disk_space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 */
2547
Jeff Mahoneyee939612009-03-30 14:02:50 -04002548int fix_nodes(int op_mode, struct tree_balance *tb,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002549 struct item_head *ins_ih, const void *data)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002550{
Jeff Mahoneyee939612009-03-30 14:02:50 -04002551 int ret, h, item_num = PATH_LAST_POSITION(tb->tb_path);
2552 int pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Jeff Mahoney098297b2014-04-23 10:00:36 -04002554 /*
2555 * we set wait_tb_buffers_run when we have to restore any dirty
2556 * bits cleared during wait_tb_buffers_run
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002557 */
2558 int wait_tb_buffers_run = 0;
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002559 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002561 ++REISERFS_SB(tb->tb_sb)->s_fix_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Jeff Mahoneyee939612009-03-30 14:02:50 -04002563 pos_in_item = tb->tb_path->pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002565 tb->fs_gen = get_generation(tb->tb_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Jeff Mahoney098297b2014-04-23 10:00:36 -04002567 /*
2568 * we prepare and log the super here so it will already be in the
2569 * transaction when do_balance needs to change it.
2570 * This way do_balance won't have to schedule when trying to prepare
2571 * the super for logging
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002572 */
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002573 reiserfs_prepare_for_journal(tb->tb_sb,
2574 SB_BUFFER_WITH_SB(tb->tb_sb), 1);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04002575 journal_mark_dirty(tb->transaction_handle,
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002576 SB_BUFFER_WITH_SB(tb->tb_sb));
2577 if (FILESYSTEM_CHANGED_TB(tb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002578 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002580 /* if it possible in indirect_to_direct conversion */
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002581 if (buffer_locked(tbS0)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -04002582 int depth = reiserfs_write_unlock_nested(tb->tb_sb);
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002583 __wait_on_buffer(tbS0);
Jeff Mahoney278f6672013-08-08 17:34:46 -04002584 reiserfs_write_lock_nested(tb->tb_sb, depth);
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002585 if (FILESYSTEM_CHANGED_TB(tb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002586 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002588#ifdef CONFIG_REISERFS_CHECK
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02002589 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002590 print_cur_tb("fix_nodes");
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002591 reiserfs_panic(tb->tb_sb, "PAP-8305",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002592 "there is pending do_balance");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002593 }
2594
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002595 if (!buffer_uptodate(tbS0) || !B_IS_IN_TREE(tbS0))
2596 reiserfs_panic(tb->tb_sb, "PAP-8320", "S[0] (%b %z) is "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002597 "not uptodate at the beginning of fix_nodes "
2598 "or not in tree (mode %c)",
Jeff Mahoneyee939612009-03-30 14:02:50 -04002599 tbS0, tbS0, op_mode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002600
2601 /* Check parameters. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002602 switch (op_mode) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002603 case M_INSERT:
Jeff Mahoneyee939612009-03-30 14:02:50 -04002604 if (item_num <= 0 || item_num > B_NR_ITEMS(tbS0))
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002605 reiserfs_panic(tb->tb_sb, "PAP-8330", "Incorrect "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002606 "item number %d (in S0 - %d) in case "
Jeff Mahoneyee939612009-03-30 14:02:50 -04002607 "of insert", item_num,
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002608 B_NR_ITEMS(tbS0));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002609 break;
2610 case M_PASTE:
2611 case M_DELETE:
2612 case M_CUT:
Jeff Mahoneyee939612009-03-30 14:02:50 -04002613 if (item_num < 0 || item_num >= B_NR_ITEMS(tbS0)) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002614 print_block(tbS0, 0, -1, -1);
2615 reiserfs_panic(tb->tb_sb, "PAP-8335", "Incorrect "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002616 "item number(%d); mode = %c "
2617 "insert_size = %d",
Jeff Mahoneyee939612009-03-30 14:02:50 -04002618 item_num, op_mode,
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002619 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002620 }
2621 break;
2622 default:
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002623 reiserfs_panic(tb->tb_sb, "PAP-8340", "Incorrect mode "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002624 "of operation");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626#endif
2627
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002628 if (get_mem_for_virtual_node(tb) == REPEAT_SEARCH)
Jeff Mahoney098297b2014-04-23 10:00:36 -04002629 /* FIXME: maybe -ENOMEM when tb->vn_buf == 0? Now just repeat */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002630 return REPEAT_SEARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Jeff Mahoneyee939612009-03-30 14:02:50 -04002632 /* Starting from the leaf level; for all levels h of the tree. */
2633 for (h = 0; h < MAX_HEIGHT && tb->insert_size[h]; h++) {
2634 ret = get_direct_parent(tb, h);
2635 if (ret != CARRY_ON)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002636 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Jeff Mahoneyee939612009-03-30 14:02:50 -04002638 ret = check_balance(op_mode, tb, h, item_num,
2639 pos_in_item, ins_ih, data);
2640 if (ret != CARRY_ON) {
2641 if (ret == NO_BALANCING_NEEDED) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002642 /* No balancing for higher levels needed. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002643 ret = get_neighbors(tb, h);
2644 if (ret != CARRY_ON)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002645 goto repeat;
Jeff Mahoneyee939612009-03-30 14:02:50 -04002646 if (h != MAX_HEIGHT - 1)
2647 tb->insert_size[h + 1] = 0;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002648 /*
2649 * ok, analysis and resource gathering
2650 * are complete
2651 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002652 break;
2653 }
2654 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Jeff Mahoneyee939612009-03-30 14:02:50 -04002657 ret = get_neighbors(tb, h);
2658 if (ret != CARRY_ON)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002659 goto repeat;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002660
Jeff Mahoney098297b2014-04-23 10:00:36 -04002661 /*
2662 * No disk space, or schedule occurred and analysis may be
2663 * invalid and needs to be redone.
2664 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002665 ret = get_empty_nodes(tb, h);
2666 if (ret != CARRY_ON)
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002667 goto repeat;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002668
Jeff Mahoney098297b2014-04-23 10:00:36 -04002669 /*
2670 * We have a positive insert size but no nodes exist on this
2671 * level, this means that we are creating a new root.
2672 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002673 if (!PATH_H_PBUFFER(tb->tb_path, h)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002674
Jeff Mahoneyee939612009-03-30 14:02:50 -04002675 RFALSE(tb->blknum[h] != 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002676 "PAP-8350: creating new empty root");
2677
Jeff Mahoneyee939612009-03-30 14:02:50 -04002678 if (h < MAX_HEIGHT - 1)
2679 tb->insert_size[h + 1] = 0;
2680 } else if (!PATH_H_PBUFFER(tb->tb_path, h + 1)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04002681 /*
2682 * The tree needs to be grown, so this node S[h]
2683 * which is the root node is split into two nodes,
2684 * and a new node (S[h+1]) will be created to
2685 * become the root node.
2686 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002687 if (tb->blknum[h] > 1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002688
Jeff Mahoneyee939612009-03-30 14:02:50 -04002689 RFALSE(h == MAX_HEIGHT - 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002690 "PAP-8355: attempt to create too high of a tree");
2691
Jeff Mahoneyee939612009-03-30 14:02:50 -04002692 tb->insert_size[h + 1] =
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002693 (DC_SIZE +
Jeff Mahoneyee939612009-03-30 14:02:50 -04002694 KEY_SIZE) * (tb->blknum[h] - 1) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002695 DC_SIZE;
Jeff Mahoneyee939612009-03-30 14:02:50 -04002696 } else if (h < MAX_HEIGHT - 1)
2697 tb->insert_size[h + 1] = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002698 } else
Jeff Mahoneyee939612009-03-30 14:02:50 -04002699 tb->insert_size[h + 1] =
2700 (DC_SIZE + KEY_SIZE) * (tb->blknum[h] - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002701 }
2702
Jeff Mahoneyee939612009-03-30 14:02:50 -04002703 ret = wait_tb_buffers_until_unlocked(tb);
2704 if (ret == CARRY_ON) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002705 if (FILESYSTEM_CHANGED_TB(tb)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002706 wait_tb_buffers_run = 1;
Jeff Mahoneyee939612009-03-30 14:02:50 -04002707 ret = REPEAT_SEARCH;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002708 goto repeat;
2709 } else {
2710 return CARRY_ON;
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002713 wait_tb_buffers_run = 1;
2714 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
2716
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002717repeat:
Jeff Mahoney098297b2014-04-23 10:00:36 -04002718 /*
2719 * fix_nodes was unable to perform its calculation due to
2720 * filesystem got changed under us, lack of free disk space or i/o
2721 * failure. If the first is the case - the search will be
2722 * repeated. For now - free all resources acquired so far except
2723 * for the new allocated nodes
2724 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002725 {
2726 int i;
2727
2728 /* Release path buffers. */
2729 if (wait_tb_buffers_run) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002730 pathrelse_and_restore(tb->tb_sb, tb->tb_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002731 } else {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002732 pathrelse(tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002734 /* brelse all resources collected for balancing */
2735 for (i = 0; i < MAX_HEIGHT; i++) {
2736 if (wait_tb_buffers_run) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002737 reiserfs_restore_prepared_buffer(tb->tb_sb,
2738 tb->L[i]);
2739 reiserfs_restore_prepared_buffer(tb->tb_sb,
2740 tb->R[i]);
2741 reiserfs_restore_prepared_buffer(tb->tb_sb,
2742 tb->FL[i]);
2743 reiserfs_restore_prepared_buffer(tb->tb_sb,
2744 tb->FR[i]);
2745 reiserfs_restore_prepared_buffer(tb->tb_sb,
2746 tb->
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002747 CFL[i]);
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002748 reiserfs_restore_prepared_buffer(tb->tb_sb,
2749 tb->
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002750 CFR[i]);
2751 }
2752
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002753 brelse(tb->L[i]);
2754 brelse(tb->R[i]);
2755 brelse(tb->FL[i]);
2756 brelse(tb->FR[i]);
2757 brelse(tb->CFL[i]);
2758 brelse(tb->CFR[i]);
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -04002759
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002760 tb->L[i] = NULL;
2761 tb->R[i] = NULL;
2762 tb->FL[i] = NULL;
2763 tb->FR[i] = NULL;
2764 tb->CFL[i] = NULL;
2765 tb->CFR[i] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002766 }
2767
2768 if (wait_tb_buffers_run) {
2769 for (i = 0; i < MAX_FEB_SIZE; i++) {
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002770 if (tb->FEB[i])
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002771 reiserfs_restore_prepared_buffer
Jeff Mahoneya063ae12009-03-30 14:02:48 -04002772 (tb->tb_sb, tb->FEB[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002773 }
2774 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04002775 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778}
2779
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002780void unfix_nodes(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002782 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002784 /* Release path buffers. */
2785 pathrelse_and_restore(tb->tb_sb, tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002787 /* brelse all resources collected for balancing */
2788 for (i = 0; i < MAX_HEIGHT; i++) {
2789 reiserfs_restore_prepared_buffer(tb->tb_sb, tb->L[i]);
2790 reiserfs_restore_prepared_buffer(tb->tb_sb, tb->R[i]);
2791 reiserfs_restore_prepared_buffer(tb->tb_sb, tb->FL[i]);
2792 reiserfs_restore_prepared_buffer(tb->tb_sb, tb->FR[i]);
2793 reiserfs_restore_prepared_buffer(tb->tb_sb, tb->CFL[i]);
2794 reiserfs_restore_prepared_buffer(tb->tb_sb, tb->CFR[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002796 brelse(tb->L[i]);
2797 brelse(tb->R[i]);
2798 brelse(tb->FL[i]);
2799 brelse(tb->FR[i]);
2800 brelse(tb->CFL[i]);
2801 brelse(tb->CFR[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002803
2804 /* deal with list of allocated (used and unused) nodes */
2805 for (i = 0; i < MAX_FEB_SIZE; i++) {
2806 if (tb->FEB[i]) {
2807 b_blocknr_t blocknr = tb->FEB[i]->b_blocknr;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002808 /*
2809 * de-allocated block which was not used by
2810 * balancing and bforget about buffer for it
2811 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002812 brelse(tb->FEB[i]);
2813 reiserfs_free_block(tb->transaction_handle, NULL,
2814 blocknr, 0);
2815 }
2816 if (tb->used[i]) {
2817 /* release used as new nodes including a new root */
2818 brelse(tb->used[i]);
2819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Pekka Enbergd739b422006-02-01 03:06:43 -08002822 kfree(tb->vn_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002824}