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
31
32
33
34#include "ubifs.h"
35#ifndef __UBOOT__
36#include <log.h>
37#include <dm/devres.h>
38#include <linux/crc16.h>
39#include <linux/math64.h>
40#include <linux/slab.h>
41#else
42#include <linux/compat.h>
43#include <linux/err.h>
44#include <ubi_uboot.h>
45#include "crc16.h"
46#endif
47
48
49
50
51
52
53
54
55static void do_calc_lpt_geom(struct ubifs_info *c)
56{
57 int i, n, bits, per_leb_wastage, max_pnode_cnt;
58 long long sz, tot_wastage;
59
60 n = c->main_lebs + c->max_leb_cnt - c->leb_cnt;
61 max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
62
63 c->lpt_hght = 1;
64 n = UBIFS_LPT_FANOUT;
65 while (n < max_pnode_cnt) {
66 c->lpt_hght += 1;
67 n <<= UBIFS_LPT_FANOUT_SHIFT;
68 }
69
70 c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
71
72 n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT);
73 c->nnode_cnt = n;
74 for (i = 1; i < c->lpt_hght; i++) {
75 n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
76 c->nnode_cnt += n;
77 }
78
79 c->space_bits = fls(c->leb_size) - 3;
80 c->lpt_lnum_bits = fls(c->lpt_lebs);
81 c->lpt_offs_bits = fls(c->leb_size - 1);
82 c->lpt_spc_bits = fls(c->leb_size);
83
84 n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
85 c->pcnt_bits = fls(n - 1);
86
87 c->lnum_bits = fls(c->max_leb_cnt - 1);
88
89 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
90 (c->big_lpt ? c->pcnt_bits : 0) +
91 (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT;
92 c->pnode_sz = (bits + 7) / 8;
93
94 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
95 (c->big_lpt ? c->pcnt_bits : 0) +
96 (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT;
97 c->nnode_sz = (bits + 7) / 8;
98
99 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
100 c->lpt_lebs * c->lpt_spc_bits * 2;
101 c->ltab_sz = (bits + 7) / 8;
102
103 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
104 c->lnum_bits * c->lsave_cnt;
105 c->lsave_sz = (bits + 7) / 8;
106
107
108 c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
109 c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
110 c->lpt_sz += c->ltab_sz;
111 if (c->big_lpt)
112 c->lpt_sz += c->lsave_sz;
113
114
115 sz = c->lpt_sz;
116 per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz);
117 sz += per_leb_wastage;
118 tot_wastage = per_leb_wastage;
119 while (sz > c->leb_size) {
120 sz += per_leb_wastage;
121 sz -= c->leb_size;
122 tot_wastage += per_leb_wastage;
123 }
124 tot_wastage += ALIGN(sz, c->min_io_size) - sz;
125 c->lpt_sz += tot_wastage;
126}
127
128
129
130
131
132
133
134int ubifs_calc_lpt_geom(struct ubifs_info *c)
135{
136 int lebs_needed;
137 long long sz;
138
139 do_calc_lpt_geom(c);
140
141
142 sz = c->lpt_sz * 2;
143 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
144 if (lebs_needed > c->lpt_lebs) {
145 ubifs_err(c, "too few LPT LEBs");
146 return -EINVAL;
147 }
148
149
150 if (c->ltab_sz > c->leb_size) {
151 ubifs_err(c, "LPT ltab too big");
152 return -EINVAL;
153 }
154
155 c->check_lpt_free = c->big_lpt;
156 return 0;
157}
158
159
160
161
162
163
164
165
166
167
168
169
170
171static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
172 int *big_lpt)
173{
174 int i, lebs_needed;
175 long long sz;
176
177
178 c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
179 c->main_lebs = *main_lebs - c->lpt_lebs;
180 if (c->main_lebs <= 0)
181 return -EINVAL;
182
183
184 c->big_lpt = 0;
185
186
187
188
189
190 do_calc_lpt_geom(c);
191
192
193 if (c->lpt_sz > c->leb_size) {
194
195 c->big_lpt = 1;
196 do_calc_lpt_geom(c);
197 }
198
199
200 for (i = 0; i < 64 ; i++) {
201 sz = c->lpt_sz * 4;
202 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
203 if (lebs_needed > c->lpt_lebs) {
204
205 c->lpt_lebs = lebs_needed;
206 c->main_lebs = *main_lebs - c->lpt_lebs;
207 if (c->main_lebs <= 0)
208 return -EINVAL;
209 do_calc_lpt_geom(c);
210 continue;
211 }
212 if (c->ltab_sz > c->leb_size) {
213 ubifs_err(c, "LPT ltab too big");
214 return -EINVAL;
215 }
216 *main_lebs = c->main_lebs;
217 *big_lpt = c->big_lpt;
218 return 0;
219 }
220 return -EINVAL;
221}
222
223
224
225
226
227
228
229
230static void pack_bits(uint8_t **addr, int *pos, uint32_t val, int nrbits)
231{
232 uint8_t *p = *addr;
233 int b = *pos;
234
235 ubifs_assert(nrbits > 0);
236 ubifs_assert(nrbits <= 32);
237 ubifs_assert(*pos >= 0);
238 ubifs_assert(*pos < 8);
239 ubifs_assert((val >> nrbits) == 0 || nrbits == 32);
240 if (b) {
241 *p |= ((uint8_t)val) << b;
242 nrbits += b;
243 if (nrbits > 8) {
244 *++p = (uint8_t)(val >>= (8 - b));
245 if (nrbits > 16) {
246 *++p = (uint8_t)(val >>= 8);
247 if (nrbits > 24) {
248 *++p = (uint8_t)(val >>= 8);
249 if (nrbits > 32)
250 *++p = (uint8_t)(val >>= 8);
251 }
252 }
253 }
254 } else {
255 *p = (uint8_t)val;
256 if (nrbits > 8) {
257 *++p = (uint8_t)(val >>= 8);
258 if (nrbits > 16) {
259 *++p = (uint8_t)(val >>= 8);
260 if (nrbits > 24)
261 *++p = (uint8_t)(val >>= 8);
262 }
263 }
264 }
265 b = nrbits & 7;
266 if (b == 0)
267 p++;
268 *addr = p;
269 *pos = b;
270}
271
272
273
274
275
276
277
278
279
280uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
281{
282 const int k = 32 - nrbits;
283 uint8_t *p = *addr;
284 int b = *pos;
285 uint32_t uninitialized_var(val);
286 const int bytes = (nrbits + b + 7) >> 3;
287
288 ubifs_assert(nrbits > 0);
289 ubifs_assert(nrbits <= 32);
290 ubifs_assert(*pos >= 0);
291 ubifs_assert(*pos < 8);
292 if (b) {
293 switch (bytes) {
294 case 2:
295 val = p[1];
296 break;
297 case 3:
298 val = p[1] | ((uint32_t)p[2] << 8);
299 break;
300 case 4:
301 val = p[1] | ((uint32_t)p[2] << 8) |
302 ((uint32_t)p[3] << 16);
303 break;
304 case 5:
305 val = p[1] | ((uint32_t)p[2] << 8) |
306 ((uint32_t)p[3] << 16) |
307 ((uint32_t)p[4] << 24);
308 }
309 val <<= (8 - b);
310 val |= *p >> b;
311 nrbits += b;
312 } else {
313 switch (bytes) {
314 case 1:
315 val = p[0];
316 break;
317 case 2:
318 val = p[0] | ((uint32_t)p[1] << 8);
319 break;
320 case 3:
321 val = p[0] | ((uint32_t)p[1] << 8) |
322 ((uint32_t)p[2] << 16);
323 break;
324 case 4:
325 val = p[0] | ((uint32_t)p[1] << 8) |
326 ((uint32_t)p[2] << 16) |
327 ((uint32_t)p[3] << 24);
328 break;
329 }
330 }
331 val <<= k;
332 val >>= k;
333 b = nrbits & 7;
334 p += nrbits >> 3;
335 *addr = p;
336 *pos = b;
337 ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
338 return val;
339}
340
341
342
343
344
345
346
347void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
348 struct ubifs_pnode *pnode)
349{
350 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
351 int i, pos = 0;
352 uint16_t crc;
353
354 pack_bits(&addr, &pos, UBIFS_LPT_PNODE, UBIFS_LPT_TYPE_BITS);
355 if (c->big_lpt)
356 pack_bits(&addr, &pos, pnode->num, c->pcnt_bits);
357 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
358 pack_bits(&addr, &pos, pnode->lprops[i].free >> 3,
359 c->space_bits);
360 pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3,
361 c->space_bits);
362 if (pnode->lprops[i].flags & LPROPS_INDEX)
363 pack_bits(&addr, &pos, 1, 1);
364 else
365 pack_bits(&addr, &pos, 0, 1);
366 }
367 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
368 c->pnode_sz - UBIFS_LPT_CRC_BYTES);
369 addr = buf;
370 pos = 0;
371 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
372}
373
374
375
376
377
378
379
380void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
381 struct ubifs_nnode *nnode)
382{
383 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
384 int i, pos = 0;
385 uint16_t crc;
386
387 pack_bits(&addr, &pos, UBIFS_LPT_NNODE, UBIFS_LPT_TYPE_BITS);
388 if (c->big_lpt)
389 pack_bits(&addr, &pos, nnode->num, c->pcnt_bits);
390 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
391 int lnum = nnode->nbranch[i].lnum;
392
393 if (lnum == 0)
394 lnum = c->lpt_last + 1;
395 pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits);
396 pack_bits(&addr, &pos, nnode->nbranch[i].offs,
397 c->lpt_offs_bits);
398 }
399 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
400 c->nnode_sz - UBIFS_LPT_CRC_BYTES);
401 addr = buf;
402 pos = 0;
403 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
404}
405
406
407
408
409
410
411
412void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
413 struct ubifs_lpt_lprops *ltab)
414{
415 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
416 int i, pos = 0;
417 uint16_t crc;
418
419 pack_bits(&addr, &pos, UBIFS_LPT_LTAB, UBIFS_LPT_TYPE_BITS);
420 for (i = 0; i < c->lpt_lebs; i++) {
421 pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits);
422 pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits);
423 }
424 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
425 c->ltab_sz - UBIFS_LPT_CRC_BYTES);
426 addr = buf;
427 pos = 0;
428 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
429}
430
431
432
433
434
435
436
437void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave)
438{
439 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
440 int i, pos = 0;
441 uint16_t crc;
442
443 pack_bits(&addr, &pos, UBIFS_LPT_LSAVE, UBIFS_LPT_TYPE_BITS);
444 for (i = 0; i < c->lsave_cnt; i++)
445 pack_bits(&addr, &pos, lsave[i], c->lnum_bits);
446 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
447 c->lsave_sz - UBIFS_LPT_CRC_BYTES);
448 addr = buf;
449 pos = 0;
450 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
451}
452
453
454
455
456
457
458
459void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty)
460{
461 if (!dirty || !lnum)
462 return;
463 dbg_lp("LEB %d add %d to %d",
464 lnum, dirty, c->ltab[lnum - c->lpt_first].dirty);
465 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
466 c->ltab[lnum - c->lpt_first].dirty += dirty;
467}
468
469
470
471
472
473
474
475
476static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
477{
478 dbg_lp("LEB %d free %d dirty %d to %d %d",
479 lnum, c->ltab[lnum - c->lpt_first].free,
480 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
481 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
482 c->ltab[lnum - c->lpt_first].free = free;
483 c->ltab[lnum - c->lpt_first].dirty = dirty;
484}
485
486
487
488
489
490
491void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode)
492{
493 struct ubifs_nnode *np = nnode->parent;
494
495 if (np)
496 ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum,
497 c->nnode_sz);
498 else {
499 ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz);
500 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
501 c->lpt_drty_flgs |= LTAB_DIRTY;
502 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
503 }
504 }
505}
506
507
508
509
510
511
512static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
513{
514 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
515 c->pnode_sz);
516}
517
518
519
520
521
522
523
524
525
526
527
528
529static int calc_nnode_num(int row, int col)
530{
531 int num, bits;
532
533 num = 1;
534 while (row--) {
535 bits = (col & (UBIFS_LPT_FANOUT - 1));
536 col >>= UBIFS_LPT_FANOUT_SHIFT;
537 num <<= UBIFS_LPT_FANOUT_SHIFT;
538 num |= bits;
539 }
540 return num;
541}
542
543
544
545
546
547
548
549
550
551
552
553
554
555static int calc_nnode_num_from_parent(const struct ubifs_info *c,
556 struct ubifs_nnode *parent, int iip)
557{
558 int num, shft;
559
560 if (!parent)
561 return 1;
562 shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
563 num = parent->num ^ (1 << shft);
564 num |= (UBIFS_LPT_FANOUT + iip) << shft;
565 return num;
566}
567
568
569
570
571
572
573
574
575
576
577
578
579
580static int calc_pnode_num_from_parent(const struct ubifs_info *c,
581 struct ubifs_nnode *parent, int iip)
582{
583 int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
584
585 for (i = 0; i < n; i++) {
586 num <<= UBIFS_LPT_FANOUT_SHIFT;
587 num |= pnum & (UBIFS_LPT_FANOUT - 1);
588 pnum >>= UBIFS_LPT_FANOUT_SHIFT;
589 }
590 num <<= UBIFS_LPT_FANOUT_SHIFT;
591 num |= iip;
592 return num;
593}
594
595
596
597
598
599
600
601
602
603
604
605int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
606 int *lpt_lebs, int *big_lpt)
607{
608 int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row;
609 int blnum, boffs, bsz, bcnt;
610 struct ubifs_pnode *pnode = NULL;
611 struct ubifs_nnode *nnode = NULL;
612 void *buf = NULL, *p;
613 struct ubifs_lpt_lprops *ltab = NULL;
614 int *lsave = NULL;
615
616 err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
617 if (err)
618 return err;
619 *lpt_lebs = c->lpt_lebs;
620
621
622 c->lpt_first = lpt_first;
623
624 c->lpt_last = lpt_first + c->lpt_lebs - 1;
625
626 c->main_first = c->leb_cnt - *main_lebs;
627
628 lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL);
629 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
630 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
631 buf = vmalloc(c->leb_size);
632 ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
633 if (!pnode || !nnode || !buf || !ltab || !lsave) {
634 err = -ENOMEM;
635 goto out;
636 }
637
638 ubifs_assert(!c->ltab);
639 c->ltab = ltab;
640
641
642 for (i = 0; i < c->lpt_lebs; i++) {
643 ltab[i].free = c->leb_size;
644 ltab[i].dirty = 0;
645 ltab[i].tgc = 0;
646 ltab[i].cmt = 0;
647 }
648
649 lnum = lpt_first;
650 p = buf;
651
652 cnt = c->pnode_cnt;
653
654
655
656
657
658 node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
659 iopos = ALIGN(node_sz, c->min_io_size);
660 pnode->lprops[0].free = c->leb_size - iopos;
661 pnode->lprops[0].dirty = iopos - node_sz;
662 pnode->lprops[0].flags = LPROPS_INDEX;
663
664 node_sz = UBIFS_INO_NODE_SZ;
665 iopos = ALIGN(node_sz, c->min_io_size);
666 pnode->lprops[1].free = c->leb_size - iopos;
667 pnode->lprops[1].dirty = iopos - node_sz;
668
669 for (i = 2; i < UBIFS_LPT_FANOUT; i++)
670 pnode->lprops[i].free = c->leb_size;
671
672
673 ubifs_pack_pnode(c, p, pnode);
674 p += c->pnode_sz;
675 len = c->pnode_sz;
676 pnode->num += 1;
677
678
679 pnode->lprops[0].free = c->leb_size;
680 pnode->lprops[0].dirty = 0;
681 pnode->lprops[0].flags = 0;
682
683 pnode->lprops[1].free = c->leb_size;
684 pnode->lprops[1].dirty = 0;
685
686
687
688
689
690 blnum = lnum;
691 boffs = 0;
692 bcnt = cnt;
693 bsz = c->pnode_sz;
694
695
696 for (i = 1; i < cnt; i++) {
697 if (len + c->pnode_sz > c->leb_size) {
698 alen = ALIGN(len, c->min_io_size);
699 set_ltab(c, lnum, c->leb_size - alen, alen - len);
700 memset(p, 0xff, alen - len);
701 err = ubifs_leb_change(c, lnum++, buf, alen);
702 if (err)
703 goto out;
704 p = buf;
705 len = 0;
706 }
707 ubifs_pack_pnode(c, p, pnode);
708 p += c->pnode_sz;
709 len += c->pnode_sz;
710
711
712
713
714
715 pnode->num += 1;
716 }
717
718 row = 0;
719 for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
720 row += 1;
721
722 while (1) {
723
724 cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
725 for (i = 0; i < cnt; i++) {
726 if (len + c->nnode_sz > c->leb_size) {
727 alen = ALIGN(len, c->min_io_size);
728 set_ltab(c, lnum, c->leb_size - alen,
729 alen - len);
730 memset(p, 0xff, alen - len);
731 err = ubifs_leb_change(c, lnum++, buf, alen);
732 if (err)
733 goto out;
734 p = buf;
735 len = 0;
736 }
737
738 if (cnt == 1) {
739 c->lpt_lnum = lnum;
740 c->lpt_offs = len;
741 }
742
743 for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
744 if (bcnt) {
745 if (boffs + bsz > c->leb_size) {
746 blnum += 1;
747 boffs = 0;
748 }
749 nnode->nbranch[j].lnum = blnum;
750 nnode->nbranch[j].offs = boffs;
751 boffs += bsz;
752 bcnt--;
753 } else {
754 nnode->nbranch[j].lnum = 0;
755 nnode->nbranch[j].offs = 0;
756 }
757 }
758 nnode->num = calc_nnode_num(row, i);
759 ubifs_pack_nnode(c, p, nnode);
760 p += c->nnode_sz;
761 len += c->nnode_sz;
762 }
763
764 if (cnt == 1)
765 break;
766
767 bcnt = cnt;
768 bsz = c->nnode_sz;
769 row -= 1;
770 }
771
772 if (*big_lpt) {
773
774 if (len + c->lsave_sz > c->leb_size) {
775 alen = ALIGN(len, c->min_io_size);
776 set_ltab(c, lnum, c->leb_size - alen, alen - len);
777 memset(p, 0xff, alen - len);
778 err = ubifs_leb_change(c, lnum++, buf, alen);
779 if (err)
780 goto out;
781 p = buf;
782 len = 0;
783 }
784
785 c->lsave_lnum = lnum;
786 c->lsave_offs = len;
787
788 for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
789 lsave[i] = c->main_first + i;
790 for (; i < c->lsave_cnt; i++)
791 lsave[i] = c->main_first;
792
793 ubifs_pack_lsave(c, p, lsave);
794 p += c->lsave_sz;
795 len += c->lsave_sz;
796 }
797
798
799 if (len + c->ltab_sz > c->leb_size) {
800 alen = ALIGN(len, c->min_io_size);
801 set_ltab(c, lnum, c->leb_size - alen, alen - len);
802 memset(p, 0xff, alen - len);
803 err = ubifs_leb_change(c, lnum++, buf, alen);
804 if (err)
805 goto out;
806 p = buf;
807 len = 0;
808 }
809
810 c->ltab_lnum = lnum;
811 c->ltab_offs = len;
812
813
814 len += c->ltab_sz;
815 alen = ALIGN(len, c->min_io_size);
816 set_ltab(c, lnum, c->leb_size - alen, alen - len);
817
818 ubifs_pack_ltab(c, p, ltab);
819 p += c->ltab_sz;
820
821
822 memset(p, 0xff, alen - len);
823 err = ubifs_leb_change(c, lnum, buf, alen);
824 if (err)
825 goto out;
826
827 c->nhead_lnum = lnum;
828 c->nhead_offs = ALIGN(len, c->min_io_size);
829
830 dbg_lp("space_bits %d", c->space_bits);
831 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
832 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
833 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
834 dbg_lp("pcnt_bits %d", c->pcnt_bits);
835 dbg_lp("lnum_bits %d", c->lnum_bits);
836 dbg_lp("pnode_sz %d", c->pnode_sz);
837 dbg_lp("nnode_sz %d", c->nnode_sz);
838 dbg_lp("ltab_sz %d", c->ltab_sz);
839 dbg_lp("lsave_sz %d", c->lsave_sz);
840 dbg_lp("lsave_cnt %d", c->lsave_cnt);
841 dbg_lp("lpt_hght %d", c->lpt_hght);
842 dbg_lp("big_lpt %d", c->big_lpt);
843 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
844 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
845 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
846 if (c->big_lpt)
847 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
848out:
849 c->ltab = NULL;
850 kfree(lsave);
851 vfree(ltab);
852 vfree(buf);
853 kfree(nnode);
854 kfree(pnode);
855 return err;
856}
857
858
859
860
861
862
863
864
865
866static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
867{
868 int i;
869
870 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
871 int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
872 int lnum = pnode->lprops[i].lnum;
873
874 if (!lnum)
875 return;
876 ubifs_add_to_cat(c, &pnode->lprops[i], cat);
877 }
878}
879
880
881
882
883
884
885
886
887
888
889
890static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
891 struct ubifs_pnode *new_pnode)
892{
893 int i;
894
895 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
896 if (!new_pnode->lprops[i].lnum)
897 return;
898 ubifs_replace_cat(c, &old_pnode->lprops[i],
899 &new_pnode->lprops[i]);
900 }
901}
902
903
904
905
906
907
908
909
910
911static int check_lpt_crc(const struct ubifs_info *c, void *buf, int len)
912{
913 int pos = 0;
914 uint8_t *addr = buf;
915 uint16_t crc, calc_crc;
916
917 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
918 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
919 len - UBIFS_LPT_CRC_BYTES);
920 if (crc != calc_crc) {
921 ubifs_err(c, "invalid crc in LPT node: crc %hx calc %hx",
922 crc, calc_crc);
923 dump_stack();
924 return -EINVAL;
925 }
926 return 0;
927}
928
929
930
931
932
933
934
935
936
937
938static int check_lpt_type(const struct ubifs_info *c, uint8_t **addr,
939 int *pos, int type)
940{
941 int node_type;
942
943 node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
944 if (node_type != type) {
945 ubifs_err(c, "invalid type (%d) in LPT node type %d",
946 node_type, type);
947 dump_stack();
948 return -EINVAL;
949 }
950 return 0;
951}
952
953
954
955
956
957
958
959
960
961static int unpack_pnode(const struct ubifs_info *c, void *buf,
962 struct ubifs_pnode *pnode)
963{
964 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
965 int i, pos = 0, err;
966
967 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_PNODE);
968 if (err)
969 return err;
970 if (c->big_lpt)
971 pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
972 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
973 struct ubifs_lprops * const lprops = &pnode->lprops[i];
974
975 lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
976 lprops->free <<= 3;
977 lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
978 lprops->dirty <<= 3;
979
980 if (ubifs_unpack_bits(&addr, &pos, 1))
981 lprops->flags = LPROPS_INDEX;
982 else
983 lprops->flags = 0;
984 lprops->flags |= ubifs_categorize_lprops(c, lprops);
985 }
986 err = check_lpt_crc(c, buf, c->pnode_sz);
987 return err;
988}
989
990
991
992
993
994
995
996
997
998int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
999 struct ubifs_nnode *nnode)
1000{
1001 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1002 int i, pos = 0, err;
1003
1004 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_NNODE);
1005 if (err)
1006 return err;
1007 if (c->big_lpt)
1008 nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1009 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1010 int lnum;
1011
1012 lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
1013 c->lpt_first;
1014 if (lnum == c->lpt_last + 1)
1015 lnum = 0;
1016 nnode->nbranch[i].lnum = lnum;
1017 nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
1018 c->lpt_offs_bits);
1019 }
1020 err = check_lpt_crc(c, buf, c->nnode_sz);
1021 return err;
1022}
1023
1024
1025
1026
1027
1028
1029
1030
1031static int unpack_ltab(const struct ubifs_info *c, void *buf)
1032{
1033 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1034 int i, pos = 0, err;
1035
1036 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LTAB);
1037 if (err)
1038 return err;
1039 for (i = 0; i < c->lpt_lebs; i++) {
1040 int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1041 int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1042
1043 if (free < 0 || free > c->leb_size || dirty < 0 ||
1044 dirty > c->leb_size || free + dirty > c->leb_size)
1045 return -EINVAL;
1046
1047 c->ltab[i].free = free;
1048 c->ltab[i].dirty = dirty;
1049 c->ltab[i].tgc = 0;
1050 c->ltab[i].cmt = 0;
1051 }
1052 err = check_lpt_crc(c, buf, c->ltab_sz);
1053 return err;
1054}
1055
1056#ifndef __UBOOT__
1057
1058
1059
1060
1061
1062
1063
1064static int unpack_lsave(const struct ubifs_info *c, void *buf)
1065{
1066 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1067 int i, pos = 0, err;
1068
1069 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LSAVE);
1070 if (err)
1071 return err;
1072 for (i = 0; i < c->lsave_cnt; i++) {
1073 int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
1074
1075 if (lnum < c->main_first || lnum >= c->leb_cnt)
1076 return -EINVAL;
1077 c->lsave[i] = lnum;
1078 }
1079 err = check_lpt_crc(c, buf, c->lsave_sz);
1080 return err;
1081}
1082#endif
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1094 struct ubifs_nnode *parent, int iip)
1095{
1096 int i, lvl, max_offs;
1097
1098 if (c->big_lpt) {
1099 int num = calc_nnode_num_from_parent(c, parent, iip);
1100
1101 if (nnode->num != num)
1102 return -EINVAL;
1103 }
1104 lvl = parent ? parent->level - 1 : c->lpt_hght;
1105 if (lvl < 1)
1106 return -EINVAL;
1107 if (lvl == 1)
1108 max_offs = c->leb_size - c->pnode_sz;
1109 else
1110 max_offs = c->leb_size - c->nnode_sz;
1111 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1112 int lnum = nnode->nbranch[i].lnum;
1113 int offs = nnode->nbranch[i].offs;
1114
1115 if (lnum == 0) {
1116 if (offs != 0)
1117 return -EINVAL;
1118 continue;
1119 }
1120 if (lnum < c->lpt_first || lnum > c->lpt_last)
1121 return -EINVAL;
1122 if (offs < 0 || offs > max_offs)
1123 return -EINVAL;
1124 }
1125 return 0;
1126}
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1138 struct ubifs_nnode *parent, int iip)
1139{
1140 int i;
1141
1142 if (c->big_lpt) {
1143 int num = calc_pnode_num_from_parent(c, parent, iip);
1144
1145 if (pnode->num != num)
1146 return -EINVAL;
1147 }
1148 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1149 int free = pnode->lprops[i].free;
1150 int dirty = pnode->lprops[i].dirty;
1151
1152 if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1153 (free & 7))
1154 return -EINVAL;
1155 if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1156 return -EINVAL;
1157 if (dirty + free > c->leb_size)
1158 return -EINVAL;
1159 }
1160 return 0;
1161}
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171static void set_pnode_lnum(const struct ubifs_info *c,
1172 struct ubifs_pnode *pnode)
1173{
1174 int i, lnum;
1175
1176 lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1177 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1178 if (lnum >= c->leb_cnt)
1179 return;
1180 pnode->lprops[i].lnum = lnum++;
1181 }
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1193{
1194 struct ubifs_nbranch *branch = NULL;
1195 struct ubifs_nnode *nnode = NULL;
1196 void *buf = c->lpt_nod_buf;
1197 int err, lnum, offs;
1198
1199 if (parent) {
1200 branch = &parent->nbranch[iip];
1201 lnum = branch->lnum;
1202 offs = branch->offs;
1203 } else {
1204 lnum = c->lpt_lnum;
1205 offs = c->lpt_offs;
1206 }
1207 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1208 if (!nnode) {
1209 err = -ENOMEM;
1210 goto out;
1211 }
1212 if (lnum == 0) {
1213
1214
1215
1216
1217
1218
1219 if (c->big_lpt)
1220 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1221 } else {
1222 err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1);
1223 if (err)
1224 goto out;
1225 err = ubifs_unpack_nnode(c, buf, nnode);
1226 if (err)
1227 goto out;
1228 }
1229 err = validate_nnode(c, nnode, parent, iip);
1230 if (err)
1231 goto out;
1232 if (!c->big_lpt)
1233 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1234 if (parent) {
1235 branch->nnode = nnode;
1236 nnode->level = parent->level - 1;
1237 } else {
1238 c->nroot = nnode;
1239 nnode->level = c->lpt_hght;
1240 }
1241 nnode->parent = parent;
1242 nnode->iip = iip;
1243 return 0;
1244
1245out:
1246 ubifs_err(c, "error %d reading nnode at %d:%d", err, lnum, offs);
1247 dump_stack();
1248 kfree(nnode);
1249 return err;
1250}
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1261{
1262 struct ubifs_nbranch *branch;
1263 struct ubifs_pnode *pnode = NULL;
1264 void *buf = c->lpt_nod_buf;
1265 int err, lnum, offs;
1266
1267 branch = &parent->nbranch[iip];
1268 lnum = branch->lnum;
1269 offs = branch->offs;
1270 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1271 if (!pnode)
1272 return -ENOMEM;
1273
1274 if (lnum == 0) {
1275
1276
1277
1278
1279
1280 int i;
1281
1282 if (c->big_lpt)
1283 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1284 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1285 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1286
1287 lprops->free = c->leb_size;
1288 lprops->flags = ubifs_categorize_lprops(c, lprops);
1289 }
1290 } else {
1291 err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1);
1292 if (err)
1293 goto out;
1294 err = unpack_pnode(c, buf, pnode);
1295 if (err)
1296 goto out;
1297 }
1298 err = validate_pnode(c, pnode, parent, iip);
1299 if (err)
1300 goto out;
1301 if (!c->big_lpt)
1302 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1303 branch->pnode = pnode;
1304 pnode->parent = parent;
1305 pnode->iip = iip;
1306 set_pnode_lnum(c, pnode);
1307 c->pnodes_have += 1;
1308 return 0;
1309
1310out:
1311 ubifs_err(c, "error %d reading pnode at %d:%d", err, lnum, offs);
1312 ubifs_dump_pnode(c, pnode, parent, iip);
1313 dump_stack();
1314 ubifs_err(c, "calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1315 kfree(pnode);
1316 return err;
1317}
1318
1319
1320
1321
1322
1323
1324
1325static int read_ltab(struct ubifs_info *c)
1326{
1327 int err;
1328 void *buf;
1329
1330 buf = vmalloc(c->ltab_sz);
1331 if (!buf)
1332 return -ENOMEM;
1333 err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1);
1334 if (err)
1335 goto out;
1336 err = unpack_ltab(c, buf);
1337out:
1338 vfree(buf);
1339 return err;
1340}
1341
1342#ifndef __UBOOT__
1343
1344
1345
1346
1347
1348
1349static int read_lsave(struct ubifs_info *c)
1350{
1351 int err, i;
1352 void *buf;
1353
1354 buf = vmalloc(c->lsave_sz);
1355 if (!buf)
1356 return -ENOMEM;
1357 err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs,
1358 c->lsave_sz, 1);
1359 if (err)
1360 goto out;
1361 err = unpack_lsave(c, buf);
1362 if (err)
1363 goto out;
1364 for (i = 0; i < c->lsave_cnt; i++) {
1365 int lnum = c->lsave[i];
1366 struct ubifs_lprops *lprops;
1367
1368
1369
1370
1371
1372 if (lnum >= c->leb_cnt)
1373 continue;
1374 lprops = ubifs_lpt_lookup(c, lnum);
1375 if (IS_ERR(lprops)) {
1376 err = PTR_ERR(lprops);
1377 goto out;
1378 }
1379 }
1380out:
1381 vfree(buf);
1382 return err;
1383}
1384#endif
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1396 struct ubifs_nnode *parent, int iip)
1397{
1398 struct ubifs_nbranch *branch;
1399 struct ubifs_nnode *nnode;
1400 int err;
1401
1402 branch = &parent->nbranch[iip];
1403 nnode = branch->nnode;
1404 if (nnode)
1405 return nnode;
1406 err = ubifs_read_nnode(c, parent, iip);
1407 if (err)
1408 return ERR_PTR(err);
1409 return branch->nnode;
1410}
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1422 struct ubifs_nnode *parent, int iip)
1423{
1424 struct ubifs_nbranch *branch;
1425 struct ubifs_pnode *pnode;
1426 int err;
1427
1428 branch = &parent->nbranch[iip];
1429 pnode = branch->pnode;
1430 if (pnode)
1431 return pnode;
1432 err = read_pnode(c, parent, iip);
1433 if (err)
1434 return ERR_PTR(err);
1435 update_cats(c, branch->pnode);
1436 return branch->pnode;
1437}
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1448{
1449 int err, i, h, iip, shft;
1450 struct ubifs_nnode *nnode;
1451 struct ubifs_pnode *pnode;
1452
1453 if (!c->nroot) {
1454 err = ubifs_read_nnode(c, NULL, 0);
1455 if (err)
1456 return ERR_PTR(err);
1457 }
1458 nnode = c->nroot;
1459 i = lnum - c->main_first;
1460 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1461 for (h = 1; h < c->lpt_hght; h++) {
1462 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1463 shft -= UBIFS_LPT_FANOUT_SHIFT;
1464 nnode = ubifs_get_nnode(c, nnode, iip);
1465 if (IS_ERR(nnode))
1466 return ERR_CAST(nnode);
1467 }
1468 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1469 pnode = ubifs_get_pnode(c, nnode, iip);
1470 if (IS_ERR(pnode))
1471 return ERR_CAST(pnode);
1472 iip = (i & (UBIFS_LPT_FANOUT - 1));
1473 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1474 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1475 pnode->lprops[iip].flags);
1476 return &pnode->lprops[iip];
1477}
1478
1479
1480
1481
1482
1483
1484
1485
1486static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1487 struct ubifs_nnode *nnode)
1488{
1489 struct ubifs_nnode *n;
1490 int i;
1491
1492 if (!test_bit(COW_CNODE, &nnode->flags)) {
1493
1494 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1495 c->dirty_nn_cnt += 1;
1496 ubifs_add_nnode_dirt(c, nnode);
1497 }
1498 return nnode;
1499 }
1500
1501
1502 n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1503 if (unlikely(!n))
1504 return ERR_PTR(-ENOMEM);
1505
1506 memcpy(n, nnode, sizeof(struct ubifs_nnode));
1507 n->cnext = NULL;
1508 __set_bit(DIRTY_CNODE, &n->flags);
1509 __clear_bit(COW_CNODE, &n->flags);
1510
1511
1512 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1513 struct ubifs_nbranch *branch = &n->nbranch[i];
1514
1515 if (branch->cnode)
1516 branch->cnode->parent = n;
1517 }
1518
1519 ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
1520 __set_bit(OBSOLETE_CNODE, &nnode->flags);
1521
1522 c->dirty_nn_cnt += 1;
1523 ubifs_add_nnode_dirt(c, nnode);
1524 if (nnode->parent)
1525 nnode->parent->nbranch[n->iip].nnode = n;
1526 else
1527 c->nroot = n;
1528 return n;
1529}
1530
1531
1532
1533
1534
1535
1536
1537
1538static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1539 struct ubifs_pnode *pnode)
1540{
1541 struct ubifs_pnode *p;
1542
1543 if (!test_bit(COW_CNODE, &pnode->flags)) {
1544
1545 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1546 c->dirty_pn_cnt += 1;
1547 add_pnode_dirt(c, pnode);
1548 }
1549 return pnode;
1550 }
1551
1552
1553 p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1554 if (unlikely(!p))
1555 return ERR_PTR(-ENOMEM);
1556
1557 memcpy(p, pnode, sizeof(struct ubifs_pnode));
1558 p->cnext = NULL;
1559 __set_bit(DIRTY_CNODE, &p->flags);
1560 __clear_bit(COW_CNODE, &p->flags);
1561 replace_cats(c, pnode, p);
1562
1563 ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1564 __set_bit(OBSOLETE_CNODE, &pnode->flags);
1565
1566 c->dirty_pn_cnt += 1;
1567 add_pnode_dirt(c, pnode);
1568 pnode->parent->nbranch[p->iip].pnode = p;
1569 return p;
1570}
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1581{
1582 int err, i, h, iip, shft;
1583 struct ubifs_nnode *nnode;
1584 struct ubifs_pnode *pnode;
1585
1586 if (!c->nroot) {
1587 err = ubifs_read_nnode(c, NULL, 0);
1588 if (err)
1589 return ERR_PTR(err);
1590 }
1591 nnode = c->nroot;
1592 nnode = dirty_cow_nnode(c, nnode);
1593 if (IS_ERR(nnode))
1594 return ERR_CAST(nnode);
1595 i = lnum - c->main_first;
1596 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1597 for (h = 1; h < c->lpt_hght; h++) {
1598 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1599 shft -= UBIFS_LPT_FANOUT_SHIFT;
1600 nnode = ubifs_get_nnode(c, nnode, iip);
1601 if (IS_ERR(nnode))
1602 return ERR_CAST(nnode);
1603 nnode = dirty_cow_nnode(c, nnode);
1604 if (IS_ERR(nnode))
1605 return ERR_CAST(nnode);
1606 }
1607 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1608 pnode = ubifs_get_pnode(c, nnode, iip);
1609 if (IS_ERR(pnode))
1610 return ERR_CAST(pnode);
1611 pnode = dirty_cow_pnode(c, pnode);
1612 if (IS_ERR(pnode))
1613 return ERR_CAST(pnode);
1614 iip = (i & (UBIFS_LPT_FANOUT - 1));
1615 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1616 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1617 pnode->lprops[iip].flags);
1618 ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1619 return &pnode->lprops[iip];
1620}
1621
1622
1623
1624
1625
1626
1627
1628static int lpt_init_rd(struct ubifs_info *c)
1629{
1630 int err, i;
1631
1632 c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1633 if (!c->ltab)
1634 return -ENOMEM;
1635
1636 i = max_t(int, c->nnode_sz, c->pnode_sz);
1637 c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1638 if (!c->lpt_nod_buf)
1639 return -ENOMEM;
1640
1641 for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1642 c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
1643 GFP_KERNEL);
1644 if (!c->lpt_heap[i].arr)
1645 return -ENOMEM;
1646 c->lpt_heap[i].cnt = 0;
1647 c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1648 }
1649
1650 c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1651 if (!c->dirty_idx.arr)
1652 return -ENOMEM;
1653 c->dirty_idx.cnt = 0;
1654 c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1655
1656 err = read_ltab(c);
1657 if (err)
1658 return err;
1659
1660 dbg_lp("space_bits %d", c->space_bits);
1661 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1662 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1663 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1664 dbg_lp("pcnt_bits %d", c->pcnt_bits);
1665 dbg_lp("lnum_bits %d", c->lnum_bits);
1666 dbg_lp("pnode_sz %d", c->pnode_sz);
1667 dbg_lp("nnode_sz %d", c->nnode_sz);
1668 dbg_lp("ltab_sz %d", c->ltab_sz);
1669 dbg_lp("lsave_sz %d", c->lsave_sz);
1670 dbg_lp("lsave_cnt %d", c->lsave_cnt);
1671 dbg_lp("lpt_hght %d", c->lpt_hght);
1672 dbg_lp("big_lpt %d", c->big_lpt);
1673 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1674 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1675 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1676 if (c->big_lpt)
1677 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1678
1679 return 0;
1680}
1681
1682#ifndef __UBOOT__
1683
1684
1685
1686
1687
1688
1689
1690
1691static int lpt_init_wr(struct ubifs_info *c)
1692{
1693 int err, i;
1694
1695 c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1696 if (!c->ltab_cmt)
1697 return -ENOMEM;
1698
1699 c->lpt_buf = vmalloc(c->leb_size);
1700 if (!c->lpt_buf)
1701 return -ENOMEM;
1702
1703 if (c->big_lpt) {
1704 c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1705 if (!c->lsave)
1706 return -ENOMEM;
1707 err = read_lsave(c);
1708 if (err)
1709 return err;
1710 }
1711
1712 for (i = 0; i < c->lpt_lebs; i++)
1713 if (c->ltab[i].free == c->leb_size) {
1714 err = ubifs_leb_unmap(c, i + c->lpt_first);
1715 if (err)
1716 return err;
1717 }
1718
1719 return 0;
1720}
1721#endif
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1736{
1737 int err;
1738
1739 if (rd) {
1740 err = lpt_init_rd(c);
1741 if (err)
1742 goto out_err;
1743 }
1744
1745#ifndef __UBOOT__
1746 if (wr) {
1747 err = lpt_init_wr(c);
1748 if (err)
1749 goto out_err;
1750 }
1751#endif
1752
1753 return 0;
1754
1755out_err:
1756#ifndef __UBOOT__
1757 if (wr)
1758 ubifs_lpt_free(c, 1);
1759#endif
1760 if (rd)
1761 ubifs_lpt_free(c, 0);
1762 return err;
1763}
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776struct lpt_scan_node {
1777 union {
1778 struct ubifs_nnode nnode;
1779 struct ubifs_pnode pnode;
1780 struct ubifs_cnode cnode;
1781 };
1782 int in_tree;
1783 union {
1784 struct ubifs_nnode *nnode;
1785 struct ubifs_pnode *pnode;
1786 struct ubifs_cnode *cnode;
1787 } ptr;
1788};
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1801 struct lpt_scan_node *path,
1802 struct ubifs_nnode *parent, int iip)
1803{
1804 struct ubifs_nbranch *branch;
1805 struct ubifs_nnode *nnode;
1806 void *buf = c->lpt_nod_buf;
1807 int err;
1808
1809 branch = &parent->nbranch[iip];
1810 nnode = branch->nnode;
1811 if (nnode) {
1812 path->in_tree = 1;
1813 path->ptr.nnode = nnode;
1814 return nnode;
1815 }
1816 nnode = &path->nnode;
1817 path->in_tree = 0;
1818 path->ptr.nnode = nnode;
1819 memset(nnode, 0, sizeof(struct ubifs_nnode));
1820 if (branch->lnum == 0) {
1821
1822
1823
1824
1825
1826
1827 if (c->big_lpt)
1828 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1829 } else {
1830 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1831 c->nnode_sz, 1);
1832 if (err)
1833 return ERR_PTR(err);
1834 err = ubifs_unpack_nnode(c, buf, nnode);
1835 if (err)
1836 return ERR_PTR(err);
1837 }
1838 err = validate_nnode(c, nnode, parent, iip);
1839 if (err)
1840 return ERR_PTR(err);
1841 if (!c->big_lpt)
1842 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1843 nnode->level = parent->level - 1;
1844 nnode->parent = parent;
1845 nnode->iip = iip;
1846 return nnode;
1847}
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1860 struct lpt_scan_node *path,
1861 struct ubifs_nnode *parent, int iip)
1862{
1863 struct ubifs_nbranch *branch;
1864 struct ubifs_pnode *pnode;
1865 void *buf = c->lpt_nod_buf;
1866 int err;
1867
1868 branch = &parent->nbranch[iip];
1869 pnode = branch->pnode;
1870 if (pnode) {
1871 path->in_tree = 1;
1872 path->ptr.pnode = pnode;
1873 return pnode;
1874 }
1875 pnode = &path->pnode;
1876 path->in_tree = 0;
1877 path->ptr.pnode = pnode;
1878 memset(pnode, 0, sizeof(struct ubifs_pnode));
1879 if (branch->lnum == 0) {
1880
1881
1882
1883
1884
1885 int i;
1886
1887 if (c->big_lpt)
1888 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1889 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1890 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1891
1892 lprops->free = c->leb_size;
1893 lprops->flags = ubifs_categorize_lprops(c, lprops);
1894 }
1895 } else {
1896 ubifs_assert(branch->lnum >= c->lpt_first &&
1897 branch->lnum <= c->lpt_last);
1898 ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1899 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1900 c->pnode_sz, 1);
1901 if (err)
1902 return ERR_PTR(err);
1903 err = unpack_pnode(c, buf, pnode);
1904 if (err)
1905 return ERR_PTR(err);
1906 }
1907 err = validate_pnode(c, pnode, parent, iip);
1908 if (err)
1909 return ERR_PTR(err);
1910 if (!c->big_lpt)
1911 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1912 pnode->parent = parent;
1913 pnode->iip = iip;
1914 set_pnode_lnum(c, pnode);
1915 return pnode;
1916}
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1929 ubifs_lpt_scan_callback scan_cb, void *data)
1930{
1931 int err = 0, i, h, iip, shft;
1932 struct ubifs_nnode *nnode;
1933 struct ubifs_pnode *pnode;
1934 struct lpt_scan_node *path;
1935
1936 if (start_lnum == -1) {
1937 start_lnum = end_lnum + 1;
1938 if (start_lnum >= c->leb_cnt)
1939 start_lnum = c->main_first;
1940 }
1941
1942 ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1943 ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1944
1945 if (!c->nroot) {
1946 err = ubifs_read_nnode(c, NULL, 0);
1947 if (err)
1948 return err;
1949 }
1950
1951 path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1952 GFP_NOFS);
1953 if (!path)
1954 return -ENOMEM;
1955
1956 path[0].ptr.nnode = c->nroot;
1957 path[0].in_tree = 1;
1958again:
1959
1960 nnode = c->nroot;
1961 i = start_lnum - c->main_first;
1962 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1963 for (h = 1; h < c->lpt_hght; h++) {
1964 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1965 shft -= UBIFS_LPT_FANOUT_SHIFT;
1966 nnode = scan_get_nnode(c, path + h, nnode, iip);
1967 if (IS_ERR(nnode)) {
1968 err = PTR_ERR(nnode);
1969 goto out;
1970 }
1971 }
1972 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1973 pnode = scan_get_pnode(c, path + h, nnode, iip);
1974 if (IS_ERR(pnode)) {
1975 err = PTR_ERR(pnode);
1976 goto out;
1977 }
1978 iip = (i & (UBIFS_LPT_FANOUT - 1));
1979
1980
1981 while (1) {
1982 struct ubifs_lprops *lprops = &pnode->lprops[iip];
1983 int ret, lnum = lprops->lnum;
1984
1985 ret = scan_cb(c, lprops, path[h].in_tree, data);
1986 if (ret < 0) {
1987 err = ret;
1988 goto out;
1989 }
1990 if (ret & LPT_SCAN_ADD) {
1991
1992 for (h = 1; h < c->lpt_hght; h++) {
1993 const size_t sz = sizeof(struct ubifs_nnode);
1994 struct ubifs_nnode *parent;
1995
1996 if (path[h].in_tree)
1997 continue;
1998 nnode = kmemdup(&path[h].nnode, sz, GFP_NOFS);
1999 if (!nnode) {
2000 err = -ENOMEM;
2001 goto out;
2002 }
2003 parent = nnode->parent;
2004 parent->nbranch[nnode->iip].nnode = nnode;
2005 path[h].ptr.nnode = nnode;
2006 path[h].in_tree = 1;
2007 path[h + 1].cnode.parent = nnode;
2008 }
2009 if (path[h].in_tree)
2010 ubifs_ensure_cat(c, lprops);
2011 else {
2012 const size_t sz = sizeof(struct ubifs_pnode);
2013 struct ubifs_nnode *parent;
2014
2015 pnode = kmemdup(&path[h].pnode, sz, GFP_NOFS);
2016 if (!pnode) {
2017 err = -ENOMEM;
2018 goto out;
2019 }
2020 parent = pnode->parent;
2021 parent->nbranch[pnode->iip].pnode = pnode;
2022 path[h].ptr.pnode = pnode;
2023 path[h].in_tree = 1;
2024 update_cats(c, pnode);
2025 c->pnodes_have += 1;
2026 }
2027 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2028 c->nroot, 0, 0);
2029 if (err)
2030 goto out;
2031 err = dbg_check_cats(c);
2032 if (err)
2033 goto out;
2034 }
2035 if (ret & LPT_SCAN_STOP) {
2036 err = 0;
2037 break;
2038 }
2039
2040 if (lnum == end_lnum) {
2041
2042
2043
2044
2045 err = -ENOSPC;
2046 goto out;
2047 }
2048 if (lnum + 1 >= c->leb_cnt) {
2049
2050 start_lnum = c->main_first;
2051 goto again;
2052 }
2053 if (iip + 1 < UBIFS_LPT_FANOUT) {
2054
2055 iip += 1;
2056 continue;
2057 }
2058
2059 iip = pnode->iip;
2060 while (1) {
2061 h -= 1;
2062 ubifs_assert(h >= 0);
2063 nnode = path[h].ptr.nnode;
2064 if (iip + 1 < UBIFS_LPT_FANOUT)
2065 break;
2066 iip = nnode->iip;
2067 }
2068
2069 iip += 1;
2070
2071 h += 1;
2072 for (; h < c->lpt_hght; h++) {
2073 nnode = scan_get_nnode(c, path + h, nnode, iip);
2074 if (IS_ERR(nnode)) {
2075 err = PTR_ERR(nnode);
2076 goto out;
2077 }
2078 iip = 0;
2079 }
2080 pnode = scan_get_pnode(c, path + h, nnode, iip);
2081 if (IS_ERR(pnode)) {
2082 err = PTR_ERR(pnode);
2083 goto out;
2084 }
2085 iip = 0;
2086 }
2087out:
2088 kfree(path);
2089 return err;
2090}
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2101 int col)
2102{
2103 int i;
2104
2105 if (pnode->num != col) {
2106 ubifs_err(c, "pnode num %d expected %d parent num %d iip %d",
2107 pnode->num, col, pnode->parent->num, pnode->iip);
2108 return -EINVAL;
2109 }
2110 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2111 struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2112 int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2113 c->main_first;
2114 int found, cat = lprops->flags & LPROPS_CAT_MASK;
2115 struct ubifs_lpt_heap *heap;
2116 struct list_head *list = NULL;
2117
2118 if (lnum >= c->leb_cnt)
2119 continue;
2120 if (lprops->lnum != lnum) {
2121 ubifs_err(c, "bad LEB number %d expected %d",
2122 lprops->lnum, lnum);
2123 return -EINVAL;
2124 }
2125 if (lprops->flags & LPROPS_TAKEN) {
2126 if (cat != LPROPS_UNCAT) {
2127 ubifs_err(c, "LEB %d taken but not uncat %d",
2128 lprops->lnum, cat);
2129 return -EINVAL;
2130 }
2131 continue;
2132 }
2133 if (lprops->flags & LPROPS_INDEX) {
2134 switch (cat) {
2135 case LPROPS_UNCAT:
2136 case LPROPS_DIRTY_IDX:
2137 case LPROPS_FRDI_IDX:
2138 break;
2139 default:
2140 ubifs_err(c, "LEB %d index but cat %d",
2141 lprops->lnum, cat);
2142 return -EINVAL;
2143 }
2144 } else {
2145 switch (cat) {
2146 case LPROPS_UNCAT:
2147 case LPROPS_DIRTY:
2148 case LPROPS_FREE:
2149 case LPROPS_EMPTY:
2150 case LPROPS_FREEABLE:
2151 break;
2152 default:
2153 ubifs_err(c, "LEB %d not index but cat %d",
2154 lprops->lnum, cat);
2155 return -EINVAL;
2156 }
2157 }
2158 switch (cat) {
2159 case LPROPS_UNCAT:
2160 list = &c->uncat_list;
2161 break;
2162 case LPROPS_EMPTY:
2163 list = &c->empty_list;
2164 break;
2165 case LPROPS_FREEABLE:
2166 list = &c->freeable_list;
2167 break;
2168 case LPROPS_FRDI_IDX:
2169 list = &c->frdi_idx_list;
2170 break;
2171 }
2172 found = 0;
2173 switch (cat) {
2174 case LPROPS_DIRTY:
2175 case LPROPS_DIRTY_IDX:
2176 case LPROPS_FREE:
2177 heap = &c->lpt_heap[cat - 1];
2178 if (lprops->hpos < heap->cnt &&
2179 heap->arr[lprops->hpos] == lprops)
2180 found = 1;
2181 break;
2182 case LPROPS_UNCAT:
2183 case LPROPS_EMPTY:
2184 case LPROPS_FREEABLE:
2185 case LPROPS_FRDI_IDX:
2186 list_for_each_entry(lp, list, list)
2187 if (lprops == lp) {
2188 found = 1;
2189 break;
2190 }
2191 break;
2192 }
2193 if (!found) {
2194 ubifs_err(c, "LEB %d cat %d not found in cat heap/list",
2195 lprops->lnum, cat);
2196 return -EINVAL;
2197 }
2198 switch (cat) {
2199 case LPROPS_EMPTY:
2200 if (lprops->free != c->leb_size) {
2201 ubifs_err(c, "LEB %d cat %d free %d dirty %d",
2202 lprops->lnum, cat, lprops->free,
2203 lprops->dirty);
2204 return -EINVAL;
2205 }
2206 break;
2207 case LPROPS_FREEABLE:
2208 case LPROPS_FRDI_IDX:
2209 if (lprops->free + lprops->dirty != c->leb_size) {
2210 ubifs_err(c, "LEB %d cat %d free %d dirty %d",
2211 lprops->lnum, cat, lprops->free,
2212 lprops->dirty);
2213 return -EINVAL;
2214 }
2215 break;
2216 }
2217 }
2218 return 0;
2219}
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2231 int row, int col)
2232{
2233 struct ubifs_nnode *nnode, *nn;
2234 struct ubifs_cnode *cn;
2235 int num, iip = 0, err;
2236
2237 if (!dbg_is_chk_lprops(c))
2238 return 0;
2239
2240 while (cnode) {
2241 ubifs_assert(row >= 0);
2242 nnode = cnode->parent;
2243 if (cnode->level) {
2244
2245 num = calc_nnode_num(row, col);
2246 if (cnode->num != num) {
2247 ubifs_err(c, "nnode num %d expected %d parent num %d iip %d",
2248 cnode->num, num,
2249 (nnode ? nnode->num : 0), cnode->iip);
2250 return -EINVAL;
2251 }
2252 nn = (struct ubifs_nnode *)cnode;
2253 while (iip < UBIFS_LPT_FANOUT) {
2254 cn = nn->nbranch[iip].cnode;
2255 if (cn) {
2256
2257 row += 1;
2258 col <<= UBIFS_LPT_FANOUT_SHIFT;
2259 col += iip;
2260 iip = 0;
2261 cnode = cn;
2262 break;
2263 }
2264
2265 iip += 1;
2266 }
2267 if (iip < UBIFS_LPT_FANOUT)
2268 continue;
2269 } else {
2270 struct ubifs_pnode *pnode;
2271
2272
2273 pnode = (struct ubifs_pnode *)cnode;
2274 err = dbg_chk_pnode(c, pnode, col);
2275 if (err)
2276 return err;
2277 }
2278
2279 row -= 1;
2280 col >>= UBIFS_LPT_FANOUT_SHIFT;
2281 iip = cnode->iip + 1;
2282 cnode = (struct ubifs_cnode *)nnode;
2283 }
2284 return 0;
2285}
2286