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