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 "ubifs.h"
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_ZNODE, &cnode->flags));
114 __set_bit(COW_ZNODE, &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 done_ltab = 1;
305 c->ltab_lnum = lnum;
306 c->ltab_offs = offs;
307 offs += c->ltab_sz;
308 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
309 }
310
311 alen = ALIGN(offs, c->min_io_size);
312 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
313 dbg_chk_lpt_sz(c, 4, alen - offs);
314 err = dbg_chk_lpt_sz(c, 3, alen);
315 if (err)
316 return err;
317 return 0;
318
319no_space:
320 ubifs_err("LPT out of space");
321 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
322 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
323 dbg_dump_lpt_info(c);
324 dbg_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, UBI_SHORTTERM);
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_clear_bit();
462 clear_bit(COW_ZNODE, &cnode->flags);
463 smp_mb__after_clear_bit();
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 UBI_SHORTTERM);
477 if (err)
478 return err;
479 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
480 err = realloc_lpt_leb(c, &lnum);
481 if (err)
482 goto no_space;
483 offs = from = 0;
484 ubifs_assert(lnum >= c->lpt_first &&
485 lnum <= c->lpt_last);
486 err = ubifs_leb_unmap(c, lnum);
487 if (err)
488 return err;
489 }
490 done_lsave = 1;
491 ubifs_pack_lsave(c, buf + offs, c->lsave);
492 offs += c->lsave_sz;
493 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
494 }
495
496
497 if (!done_ltab) {
498 if (offs + c->ltab_sz > c->leb_size) {
499 wlen = offs - from;
500 alen = ALIGN(wlen, c->min_io_size);
501 memset(buf + offs, 0xff, alen - wlen);
502 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
503 UBI_SHORTTERM);
504 if (err)
505 return err;
506 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
507 err = realloc_lpt_leb(c, &lnum);
508 if (err)
509 goto no_space;
510 offs = from = 0;
511 ubifs_assert(lnum >= c->lpt_first &&
512 lnum <= c->lpt_last);
513 err = ubifs_leb_unmap(c, lnum);
514 if (err)
515 return err;
516 }
517 done_ltab = 1;
518 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
519 offs += c->ltab_sz;
520 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
521 }
522
523
524 wlen = offs - from;
525 alen = ALIGN(wlen, c->min_io_size);
526 memset(buf + offs, 0xff, alen - wlen);
527 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
528 if (err)
529 return err;
530
531 dbg_chk_lpt_sz(c, 4, alen - wlen);
532 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
533 if (err)
534 return err;
535
536 c->nhead_lnum = lnum;
537 c->nhead_offs = ALIGN(offs, c->min_io_size);
538
539 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
540 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
541 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
542 if (c->big_lpt)
543 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
544
545 return 0;
546
547no_space:
548 ubifs_err("LPT out of space mismatch");
549 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
550 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
551 dbg_dump_lpt_info(c);
552 dbg_dump_lpt_lebs(c);
553 dump_stack();
554 return err;
555}
556
557
558
559
560
561
562
563
564
565
566static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
567 struct ubifs_pnode *pnode)
568{
569 struct ubifs_nnode *nnode;
570 int iip;
571
572
573 nnode = pnode->parent;
574 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
575 if (nnode->nbranch[iip].lnum)
576 return ubifs_get_pnode(c, nnode, iip);
577 }
578
579
580 do {
581 iip = nnode->iip + 1;
582 nnode = nnode->parent;
583 if (!nnode)
584 return NULL;
585 for (; iip < UBIFS_LPT_FANOUT; iip++) {
586 if (nnode->nbranch[iip].lnum)
587 break;
588 }
589 } while (iip >= UBIFS_LPT_FANOUT);
590
591
592 nnode = ubifs_get_nnode(c, nnode, iip);
593 if (IS_ERR(nnode))
594 return (void *)nnode;
595
596
597 while (nnode->level > 1) {
598 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
599 if (nnode->nbranch[iip].lnum)
600 break;
601 }
602 if (iip >= UBIFS_LPT_FANOUT) {
603
604
605
606
607 iip = 0;
608 }
609 nnode = ubifs_get_nnode(c, nnode, iip);
610 if (IS_ERR(nnode))
611 return (void *)nnode;
612 }
613
614 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
615 if (nnode->nbranch[iip].lnum)
616 break;
617 if (iip >= UBIFS_LPT_FANOUT)
618
619 iip = 0;
620 return ubifs_get_pnode(c, nnode, iip);
621}
622
623
624
625
626
627
628
629
630
631static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
632{
633 int err, h, iip, shft;
634 struct ubifs_nnode *nnode;
635
636 if (!c->nroot) {
637 err = ubifs_read_nnode(c, NULL, 0);
638 if (err)
639 return ERR_PTR(err);
640 }
641 i <<= UBIFS_LPT_FANOUT_SHIFT;
642 nnode = c->nroot;
643 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
644 for (h = 1; h < c->lpt_hght; h++) {
645 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
646 shft -= UBIFS_LPT_FANOUT_SHIFT;
647 nnode = ubifs_get_nnode(c, nnode, iip);
648 if (IS_ERR(nnode))
649 return ERR_CAST(nnode);
650 }
651 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
652 return ubifs_get_pnode(c, nnode, iip);
653}
654
655
656
657
658
659
660static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
661{
662 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
663 c->pnode_sz);
664}
665
666
667
668
669
670
671static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
672{
673
674 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
675 struct ubifs_nnode *nnode;
676
677 c->dirty_pn_cnt += 1;
678 add_pnode_dirt(c, pnode);
679
680 nnode = pnode->parent;
681 while (nnode) {
682 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
683 c->dirty_nn_cnt += 1;
684 ubifs_add_nnode_dirt(c, nnode);
685 nnode = nnode->parent;
686 } else
687 break;
688 }
689 }
690}
691
692
693
694
695
696
697
698
699
700
701
702
703static int make_tree_dirty(struct ubifs_info *c)
704{
705 struct ubifs_pnode *pnode;
706
707 pnode = pnode_lookup(c, 0);
708 if (IS_ERR(pnode))
709 return PTR_ERR(pnode);
710
711 while (pnode) {
712 do_make_pnode_dirty(c, pnode);
713 pnode = next_pnode_to_dirty(c, pnode);
714 if (IS_ERR(pnode))
715 return PTR_ERR(pnode);
716 }
717 return 0;
718}
719
720
721
722
723
724
725
726
727static int need_write_all(struct ubifs_info *c)
728{
729 long long free = 0;
730 int i;
731
732 for (i = 0; i < c->lpt_lebs; i++) {
733 if (i + c->lpt_first == c->nhead_lnum)
734 free += c->leb_size - c->nhead_offs;
735 else if (c->ltab[i].free == c->leb_size)
736 free += c->leb_size;
737 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
738 free += c->leb_size;
739 }
740
741 if (free <= c->lpt_sz * 2)
742 return 1;
743 return 0;
744}
745
746
747
748
749
750
751
752
753
754static void lpt_tgc_start(struct ubifs_info *c)
755{
756 int i;
757
758 for (i = 0; i < c->lpt_lebs; i++) {
759 if (i + c->lpt_first == c->nhead_lnum)
760 continue;
761 if (c->ltab[i].dirty > 0 &&
762 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
763 c->ltab[i].tgc = 1;
764 c->ltab[i].free = c->leb_size;
765 c->ltab[i].dirty = 0;
766 dbg_lp("LEB %d", i + c->lpt_first);
767 }
768 }
769}
770
771
772
773
774
775
776
777
778
779
780static int lpt_tgc_end(struct ubifs_info *c)
781{
782 int i, err;
783
784 for (i = 0; i < c->lpt_lebs; i++)
785 if (c->ltab[i].tgc) {
786 err = ubifs_leb_unmap(c, i + c->lpt_first);
787 if (err)
788 return err;
789 c->ltab[i].tgc = 0;
790 dbg_lp("LEB %d", i + c->lpt_first);
791 }
792 return 0;
793}
794
795
796
797
798
799
800
801
802
803
804
805
806
807static void populate_lsave(struct ubifs_info *c)
808{
809 struct ubifs_lprops *lprops;
810 struct ubifs_lpt_heap *heap;
811 int i, cnt = 0;
812
813 ubifs_assert(c->big_lpt);
814 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
815 c->lpt_drty_flgs |= LSAVE_DIRTY;
816 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
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 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1154 if (err) {
1155 ubifs_err("cannot read LEB %d, error %d", lnum, 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#ifdef CONFIG_UBIFS_FS_DEBUG
1490
1491
1492
1493
1494
1495
1496static int dbg_is_all_ff(uint8_t *buf, int len)
1497{
1498 int i;
1499
1500 for (i = 0; i < len; i++)
1501 if (buf[i] != 0xff)
1502 return 0;
1503 return 1;
1504}
1505
1506
1507
1508
1509
1510
1511
1512static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1513{
1514 struct ubifs_nnode *nnode;
1515 int hght;
1516
1517
1518 nnode = first_nnode(c, &hght);
1519 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1520 struct ubifs_nbranch *branch;
1521
1522 cond_resched();
1523 if (nnode->parent) {
1524 branch = &nnode->parent->nbranch[nnode->iip];
1525 if (branch->lnum != lnum || branch->offs != offs)
1526 continue;
1527 if (test_bit(DIRTY_CNODE, &nnode->flags))
1528 return 1;
1529 return 0;
1530 } else {
1531 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1532 continue;
1533 if (test_bit(DIRTY_CNODE, &nnode->flags))
1534 return 1;
1535 return 0;
1536 }
1537 }
1538 return 1;
1539}
1540
1541
1542
1543
1544
1545
1546
1547static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1548{
1549 int i, cnt;
1550
1551 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1552 for (i = 0; i < cnt; i++) {
1553 struct ubifs_pnode *pnode;
1554 struct ubifs_nbranch *branch;
1555
1556 cond_resched();
1557 pnode = pnode_lookup(c, i);
1558 if (IS_ERR(pnode))
1559 return PTR_ERR(pnode);
1560 branch = &pnode->parent->nbranch[pnode->iip];
1561 if (branch->lnum != lnum || branch->offs != offs)
1562 continue;
1563 if (test_bit(DIRTY_CNODE, &pnode->flags))
1564 return 1;
1565 return 0;
1566 }
1567 return 1;
1568}
1569
1570
1571
1572
1573
1574
1575
1576static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1577{
1578 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1579 return 1;
1580 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1581}
1582
1583
1584
1585
1586
1587
1588
1589static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1590{
1591 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1592 return 1;
1593 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1594}
1595
1596
1597
1598
1599
1600
1601
1602
1603static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1604 int offs)
1605{
1606 switch (node_type) {
1607 case UBIFS_LPT_NNODE:
1608 return dbg_is_nnode_dirty(c, lnum, offs);
1609 case UBIFS_LPT_PNODE:
1610 return dbg_is_pnode_dirty(c, lnum, offs);
1611 case UBIFS_LPT_LTAB:
1612 return dbg_is_ltab_dirty(c, lnum, offs);
1613 case UBIFS_LPT_LSAVE:
1614 return dbg_is_lsave_dirty(c, lnum, offs);
1615 }
1616 return 1;
1617}
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1628{
1629 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1630 int ret;
1631 void *buf = c->dbg->buf;
1632
1633 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1634 return 0;
1635
1636 dbg_lp("LEB %d", lnum);
1637 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1638 if (err) {
1639 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1640 return err;
1641 }
1642 while (1) {
1643 if (!is_a_node(c, buf, len)) {
1644 int i, pad_len;
1645
1646 pad_len = get_pad_len(c, buf, len);
1647 if (pad_len) {
1648 buf += pad_len;
1649 len -= pad_len;
1650 dirty += pad_len;
1651 continue;
1652 }
1653 if (!dbg_is_all_ff(buf, len)) {
1654 dbg_msg("invalid empty space in LEB %d at %d",
1655 lnum, c->leb_size - len);
1656 err = -EINVAL;
1657 }
1658 i = lnum - c->lpt_first;
1659 if (len != c->ltab[i].free) {
1660 dbg_msg("invalid free space in LEB %d "
1661 "(free %d, expected %d)",
1662 lnum, len, c->ltab[i].free);
1663 err = -EINVAL;
1664 }
1665 if (dirty != c->ltab[i].dirty) {
1666 dbg_msg("invalid dirty space in LEB %d "
1667 "(dirty %d, expected %d)",
1668 lnum, dirty, c->ltab[i].dirty);
1669 err = -EINVAL;
1670 }
1671 return err;
1672 }
1673 node_type = get_lpt_node_type(c, buf, &node_num);
1674 node_len = get_lpt_node_len(c, node_type);
1675 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1676 if (ret == 1)
1677 dirty += node_len;
1678 buf += node_len;
1679 len -= node_len;
1680 }
1681}
1682
1683
1684
1685
1686
1687
1688
1689int dbg_check_ltab(struct ubifs_info *c)
1690{
1691 int lnum, err, i, cnt;
1692
1693 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1694 return 0;
1695
1696
1697 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1698 for (i = 0; i < cnt; i++) {
1699 struct ubifs_pnode *pnode;
1700
1701 pnode = pnode_lookup(c, i);
1702 if (IS_ERR(pnode))
1703 return PTR_ERR(pnode);
1704 cond_resched();
1705 }
1706
1707
1708 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1709 if (err)
1710 return err;
1711
1712
1713 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1714 err = dbg_check_ltab_lnum(c, lnum);
1715 if (err) {
1716 dbg_err("failed at LEB %d", lnum);
1717 return err;
1718 }
1719 }
1720
1721 dbg_lp("succeeded");
1722 return 0;
1723}
1724
1725
1726
1727
1728
1729
1730
1731int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1732{
1733 long long free = 0;
1734 int i;
1735
1736 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1737 return 0;
1738
1739 for (i = 0; i < c->lpt_lebs; i++) {
1740 if (c->ltab[i].tgc || c->ltab[i].cmt)
1741 continue;
1742 if (i + c->lpt_first == c->nhead_lnum)
1743 free += c->leb_size - c->nhead_offs;
1744 else if (c->ltab[i].free == c->leb_size)
1745 free += c->leb_size;
1746 }
1747 if (free < c->lpt_sz) {
1748 dbg_err("LPT space error: free %lld lpt_sz %lld",
1749 free, c->lpt_sz);
1750 dbg_dump_lpt_info(c);
1751 dbg_dump_lpt_lebs(c);
1752 dump_stack();
1753 return -EINVAL;
1754 }
1755 return 0;
1756}
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1773{
1774 struct ubifs_debug_info *d = c->dbg;
1775 long long chk_lpt_sz, lpt_sz;
1776 int err = 0;
1777
1778 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1779 return 0;
1780
1781 switch (action) {
1782 case 0:
1783 d->chk_lpt_sz = 0;
1784 d->chk_lpt_sz2 = 0;
1785 d->chk_lpt_lebs = 0;
1786 d->chk_lpt_wastage = 0;
1787 if (c->dirty_pn_cnt > c->pnode_cnt) {
1788 dbg_err("dirty pnodes %d exceed max %d",
1789 c->dirty_pn_cnt, c->pnode_cnt);
1790 err = -EINVAL;
1791 }
1792 if (c->dirty_nn_cnt > c->nnode_cnt) {
1793 dbg_err("dirty nnodes %d exceed max %d",
1794 c->dirty_nn_cnt, c->nnode_cnt);
1795 err = -EINVAL;
1796 }
1797 return err;
1798 case 1:
1799 d->chk_lpt_sz += len;
1800 return 0;
1801 case 2:
1802 d->chk_lpt_sz += len;
1803 d->chk_lpt_wastage += len;
1804 d->chk_lpt_lebs += 1;
1805 return 0;
1806 case 3:
1807 chk_lpt_sz = c->leb_size;
1808 chk_lpt_sz *= d->chk_lpt_lebs;
1809 chk_lpt_sz += len - c->nhead_offs;
1810 if (d->chk_lpt_sz != chk_lpt_sz) {
1811 dbg_err("LPT wrote %lld but space used was %lld",
1812 d->chk_lpt_sz, chk_lpt_sz);
1813 err = -EINVAL;
1814 }
1815 if (d->chk_lpt_sz > c->lpt_sz) {
1816 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1817 d->chk_lpt_sz, c->lpt_sz);
1818 err = -EINVAL;
1819 }
1820 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1821 dbg_err("LPT layout size %lld but wrote %lld",
1822 d->chk_lpt_sz, d->chk_lpt_sz2);
1823 err = -EINVAL;
1824 }
1825 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1826 dbg_err("LPT new nhead offs: expected %d was %d",
1827 d->new_nhead_offs, len);
1828 err = -EINVAL;
1829 }
1830 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1831 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1832 lpt_sz += c->ltab_sz;
1833 if (c->big_lpt)
1834 lpt_sz += c->lsave_sz;
1835 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1836 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1837 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1838 err = -EINVAL;
1839 }
1840 if (err) {
1841 dbg_dump_lpt_info(c);
1842 dbg_dump_lpt_lebs(c);
1843 dump_stack();
1844 }
1845 d->chk_lpt_sz2 = d->chk_lpt_sz;
1846 d->chk_lpt_sz = 0;
1847 d->chk_lpt_wastage = 0;
1848 d->chk_lpt_lebs = 0;
1849 d->new_nhead_offs = len;
1850 return err;
1851 case 4:
1852 d->chk_lpt_sz += len;
1853 d->chk_lpt_wastage += len;
1854 return 0;
1855 default:
1856 return -EINVAL;
1857 }
1858}
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1871{
1872 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1873 void *buf = c->dbg->buf;
1874
1875 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1876 current->pid, lnum);
1877 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1878 if (err) {
1879 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1880 return;
1881 }
1882 while (1) {
1883 offs = c->leb_size - len;
1884 if (!is_a_node(c, buf, len)) {
1885 int pad_len;
1886
1887 pad_len = get_pad_len(c, buf, len);
1888 if (pad_len) {
1889 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1890 lnum, offs, pad_len);
1891 buf += pad_len;
1892 len -= pad_len;
1893 continue;
1894 }
1895 if (len)
1896 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1897 lnum, offs, len);
1898 break;
1899 }
1900
1901 node_type = get_lpt_node_type(c, buf, &node_num);
1902 switch (node_type) {
1903 case UBIFS_LPT_PNODE:
1904 {
1905 node_len = c->pnode_sz;
1906 if (c->big_lpt)
1907 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1908 lnum, offs, node_num);
1909 else
1910 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1911 lnum, offs);
1912 break;
1913 }
1914 case UBIFS_LPT_NNODE:
1915 {
1916 int i;
1917 struct ubifs_nnode nnode;
1918
1919 node_len = c->nnode_sz;
1920 if (c->big_lpt)
1921 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1922 lnum, offs, node_num);
1923 else
1924 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1925 lnum, offs);
1926 err = ubifs_unpack_nnode(c, buf, &nnode);
1927 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1928 printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
1929 nnode.nbranch[i].offs);
1930 if (i != UBIFS_LPT_FANOUT - 1)
1931 printk(KERN_CONT ", ");
1932 }
1933 printk(KERN_CONT "\n");
1934 break;
1935 }
1936 case UBIFS_LPT_LTAB:
1937 node_len = c->ltab_sz;
1938 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1939 lnum, offs);
1940 break;
1941 case UBIFS_LPT_LSAVE:
1942 node_len = c->lsave_sz;
1943 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1944 break;
1945 default:
1946 ubifs_err("LPT node type %d not recognized", node_type);
1947 return;
1948 }
1949
1950 buf += node_len;
1951 len -= node_len;
1952 }
1953
1954 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1955 current->pid, lnum);
1956}
1957
1958
1959
1960
1961
1962
1963
1964
1965void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1966{
1967 int i;
1968
1969 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
1970 current->pid);
1971 for (i = 0; i < c->lpt_lebs; i++)
1972 dump_lpt_leb(c, i + c->lpt_first);
1973 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
1974 current->pid);
1975}
1976
1977#endif
1978