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