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