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