1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include "ubifs.h"
31
32
33
34
35
36
37
38
39
40struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
41 struct ubifs_znode *znode)
42{
43 int level, iip, level_search = 0;
44 struct ubifs_znode *zn;
45
46 ubifs_assert(zr);
47
48 if (unlikely(!znode))
49 return zr;
50
51 if (unlikely(znode == zr)) {
52 if (znode->level == 0)
53 return NULL;
54 return ubifs_tnc_find_child(zr, 0);
55 }
56
57 level = znode->level;
58
59 iip = znode->iip;
60 while (1) {
61 ubifs_assert(znode->level <= zr->level);
62
63
64
65
66
67 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
68 znode = znode->parent;
69 iip = znode->iip;
70 }
71
72 if (unlikely(znode->parent == zr &&
73 iip >= znode->parent->child_cnt)) {
74
75 level -= 1;
76 if (level_search || level < 0)
77
78
79
80
81
82
83 return NULL;
84
85 level_search = 1;
86 iip = -1;
87 znode = ubifs_tnc_find_child(zr, 0);
88 ubifs_assert(znode);
89 }
90
91
92 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
93 if (!zn) {
94
95 iip = znode->parent->child_cnt;
96 continue;
97 }
98
99
100 while (zn->level != level) {
101 znode = zn;
102 zn = ubifs_tnc_find_child(zn, 0);
103 if (!zn) {
104
105
106
107
108 iip = znode->iip;
109 break;
110 }
111 }
112
113 if (zn) {
114 ubifs_assert(zn->level >= 0);
115 return zn;
116 }
117 }
118}
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135int ubifs_search_zbranch(const struct ubifs_info *c,
136 const struct ubifs_znode *znode,
137 const union ubifs_key *key, int *n)
138{
139 int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
140 int uninitialized_var(cmp);
141 const struct ubifs_zbranch *zbr = &znode->zbranch[0];
142
143 ubifs_assert(end > beg);
144
145 while (end > beg) {
146 mid = (beg + end) >> 1;
147 cmp = keys_cmp(c, key, &zbr[mid].key);
148 if (cmp > 0)
149 beg = mid + 1;
150 else if (cmp < 0)
151 end = mid;
152 else {
153 *n = mid;
154 return 1;
155 }
156 }
157
158 *n = end - 1;
159
160
161 ubifs_assert(*n >= -1 && *n < znode->child_cnt);
162 if (*n == -1)
163 ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
164 else
165 ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
166 if (*n + 1 < znode->child_cnt)
167 ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
168
169 return 0;
170}
171
172
173
174
175
176
177
178
179struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
180{
181 if (unlikely(!znode))
182 return NULL;
183
184 while (znode->level > 0) {
185 struct ubifs_znode *child;
186
187 child = ubifs_tnc_find_child(znode, 0);
188 if (!child)
189 return znode;
190 znode = child;
191 }
192
193 return znode;
194}
195
196
197
198
199
200
201
202
203struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
204{
205 struct ubifs_znode *zn;
206
207 ubifs_assert(znode);
208 if (unlikely(!znode->parent))
209 return NULL;
210
211
212 zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
213 if (!zn)
214
215 return znode->parent;
216
217
218 return ubifs_tnc_postorder_first(zn);
219}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
236 struct ubifs_znode *znode)
237{
238 int i, err, type, cmp;
239 struct ubifs_idx_node *idx;
240
241 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
242 if (!idx)
243 return -ENOMEM;
244
245 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
246 if (err < 0) {
247 kfree(idx);
248 return err;
249 }
250
251 znode->child_cnt = le16_to_cpu(idx->child_cnt);
252 znode->level = le16_to_cpu(idx->level);
253
254 dbg_tnc("LEB %d:%d, level %d, %d branch",
255 lnum, offs, znode->level, znode->child_cnt);
256
257 if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
258 dbg_err("current fanout %d, branch count %d",
259 c->fanout, znode->child_cnt);
260 dbg_err("max levels %d, znode level %d",
261 UBIFS_MAX_LEVELS, znode->level);
262 err = 1;
263 goto out_dump;
264 }
265
266 for (i = 0; i < znode->child_cnt; i++) {
267 const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
268 struct ubifs_zbranch *zbr = &znode->zbranch[i];
269
270 key_read(c, &br->key, &zbr->key);
271 zbr->lnum = le32_to_cpu(br->lnum);
272 zbr->offs = le32_to_cpu(br->offs);
273 zbr->len = le32_to_cpu(br->len);
274 zbr->znode = NULL;
275
276
277
278 if (zbr->lnum < c->main_first ||
279 zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
280 zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
281 dbg_err("bad branch %d", i);
282 err = 2;
283 goto out_dump;
284 }
285
286 switch (key_type(c, &zbr->key)) {
287 case UBIFS_INO_KEY:
288 case UBIFS_DATA_KEY:
289 case UBIFS_DENT_KEY:
290 case UBIFS_XENT_KEY:
291 break;
292 default:
293 dbg_msg("bad key type at slot %d: %s", i,
294 DBGKEY(&zbr->key));
295 err = 3;
296 goto out_dump;
297 }
298
299 if (znode->level)
300 continue;
301
302 type = key_type(c, &zbr->key);
303 if (c->ranges[type].max_len == 0) {
304 if (zbr->len != c->ranges[type].len) {
305 dbg_err("bad target node (type %d) length (%d)",
306 type, zbr->len);
307 dbg_err("have to be %d", c->ranges[type].len);
308 err = 4;
309 goto out_dump;
310 }
311 } else if (zbr->len < c->ranges[type].min_len ||
312 zbr->len > c->ranges[type].max_len) {
313 dbg_err("bad target node (type %d) length (%d)",
314 type, zbr->len);
315 dbg_err("have to be in range of %d-%d",
316 c->ranges[type].min_len,
317 c->ranges[type].max_len);
318 err = 5;
319 goto out_dump;
320 }
321 }
322
323
324
325
326
327 for (i = 0; i < znode->child_cnt - 1; i++) {
328 const union ubifs_key *key1, *key2;
329
330 key1 = &znode->zbranch[i].key;
331 key2 = &znode->zbranch[i + 1].key;
332
333 cmp = keys_cmp(c, key1, key2);
334 if (cmp > 0) {
335 dbg_err("bad key order (keys %d and %d)", i, i + 1);
336 err = 6;
337 goto out_dump;
338 } else if (cmp == 0 && !is_hash_key(c, key1)) {
339
340 dbg_err("keys %d and %d are not hashed but equivalent",
341 i, i + 1);
342 err = 7;
343 goto out_dump;
344 }
345 }
346
347 kfree(idx);
348 return 0;
349
350out_dump:
351 ubifs_err("bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
352 dbg_dump_node(c, idx);
353 kfree(idx);
354 return -EINVAL;
355}
356
357
358
359
360
361
362
363
364
365
366
367
368struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
369 struct ubifs_zbranch *zbr,
370 struct ubifs_znode *parent, int iip)
371{
372 int err;
373 struct ubifs_znode *znode;
374
375 ubifs_assert(!zbr->znode);
376
377
378
379
380 znode = kzalloc(c->max_znode_sz, GFP_NOFS);
381 if (!znode)
382 return ERR_PTR(-ENOMEM);
383
384 err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
385 if (err)
386 goto out;
387
388 zbr->znode = znode;
389 znode->parent = parent;
390 znode->time = get_seconds();
391 znode->iip = iip;
392
393 return znode;
394
395out:
396 kfree(znode);
397 return ERR_PTR(err);
398}
399
400
401
402
403
404
405
406
407
408
409
410int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
411 void *node)
412{
413 union ubifs_key key1, *key = &zbr->key;
414 int err, type = key_type(c, key);
415
416 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum, zbr->offs);
417
418 if (err) {
419 dbg_tnc("key %s", DBGKEY(key));
420 return err;
421 }
422
423
424 key_read(c, node + UBIFS_KEY_OFFSET, &key1);
425 if (!keys_eq(c, key, &key1)) {
426 ubifs_err("bad key in node at LEB %d:%d",
427 zbr->lnum, zbr->offs);
428 dbg_tnc("looked for key %s found node's key %s",
429 DBGKEY(key), DBGKEY1(&key1));
430 dbg_dump_node(c, node);
431 return -EINVAL;
432 }
433
434 return 0;
435}
436