forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpt_commit.c
1997 lines (1828 loc) · 50.5 KB
/
lpt_commit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* This file is part of UBIFS.
*
* Copyright (C) 2006-2008 Nokia Corporation.
*
* Authors: Adrian Hunter
* Artem Bityutskiy (Битюцкий Артём)
*/
/*
* This file implements commit-related functionality of the LEB properties
* subsystem.
*/
#include <linux/crc16.h>
#include <linux/slab.h>
#include <linux/random.h>
#include "ubifs.h"
static int dbg_populate_lsave(struct ubifs_info *c);
/**
* first_dirty_cnode - find first dirty cnode.
* @c: UBIFS file-system description object
* @nnode: nnode at which to start
*
* This function returns the first dirty cnode or %NULL if there is not one.
*/
static struct ubifs_cnode *first_dirty_cnode(const struct ubifs_info *c, struct ubifs_nnode *nnode)
{
ubifs_assert(c, nnode);
while (1) {
int i, cont = 0;
for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
struct ubifs_cnode *cnode;
cnode = nnode->nbranch[i].cnode;
if (cnode &&
test_bit(DIRTY_CNODE, &cnode->flags)) {
if (cnode->level == 0)
return cnode;
nnode = (struct ubifs_nnode *)cnode;
cont = 1;
break;
}
}
if (!cont)
return (struct ubifs_cnode *)nnode;
}
}
/**
* next_dirty_cnode - find next dirty cnode.
* @c: UBIFS file-system description object
* @cnode: cnode from which to begin searching
*
* This function returns the next dirty cnode or %NULL if there is not one.
*/
static struct ubifs_cnode *next_dirty_cnode(const struct ubifs_info *c, struct ubifs_cnode *cnode)
{
struct ubifs_nnode *nnode;
int i;
ubifs_assert(c, cnode);
nnode = cnode->parent;
if (!nnode)
return NULL;
for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
cnode = nnode->nbranch[i].cnode;
if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
if (cnode->level == 0)
return cnode; /* cnode is a pnode */
/* cnode is a nnode */
return first_dirty_cnode(c, (struct ubifs_nnode *)cnode);
}
}
return (struct ubifs_cnode *)nnode;
}
/**
* get_cnodes_to_commit - create list of dirty cnodes to commit.
* @c: UBIFS file-system description object
*
* This function returns the number of cnodes to commit.
*/
static int get_cnodes_to_commit(struct ubifs_info *c)
{
struct ubifs_cnode *cnode, *cnext;
int cnt = 0;
if (!c->nroot)
return 0;
if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
return 0;
c->lpt_cnext = first_dirty_cnode(c, c->nroot);
cnode = c->lpt_cnext;
if (!cnode)
return 0;
cnt += 1;
while (1) {
ubifs_assert(c, !test_bit(COW_CNODE, &cnode->flags));
__set_bit(COW_CNODE, &cnode->flags);
cnext = next_dirty_cnode(c, cnode);
if (!cnext) {
cnode->cnext = c->lpt_cnext;
break;
}
cnode->cnext = cnext;
cnode = cnext;
cnt += 1;
}
dbg_cmt("committing %d cnodes", cnt);
dbg_lp("committing %d cnodes", cnt);
ubifs_assert(c, cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
return cnt;
}
/**
* upd_ltab - update LPT LEB properties.
* @c: UBIFS file-system description object
* @lnum: LEB number
* @free: amount of free space
* @dirty: amount of dirty space to add
*/
static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
{
dbg_lp("LEB %d free %d dirty %d to %d +%d",
lnum, c->ltab[lnum - c->lpt_first].free,
c->ltab[lnum - c->lpt_first].dirty, free, dirty);
ubifs_assert(c, lnum >= c->lpt_first && lnum <= c->lpt_last);
c->ltab[lnum - c->lpt_first].free = free;
c->ltab[lnum - c->lpt_first].dirty += dirty;
}
/**
* alloc_lpt_leb - allocate an LPT LEB that is empty.
* @c: UBIFS file-system description object
* @lnum: LEB number is passed and returned here
*
* This function finds the next empty LEB in the ltab starting from @lnum. If a
* an empty LEB is found it is returned in @lnum and the function returns %0.
* Otherwise the function returns -ENOSPC. Note however, that LPT is designed
* never to run out of space.
*/
static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
{
int i, n;
n = *lnum - c->lpt_first + 1;
for (i = n; i < c->lpt_lebs; i++) {
if (c->ltab[i].tgc || c->ltab[i].cmt)
continue;
if (c->ltab[i].free == c->leb_size) {
c->ltab[i].cmt = 1;
*lnum = i + c->lpt_first;
return 0;
}
}
for (i = 0; i < n; i++) {
if (c->ltab[i].tgc || c->ltab[i].cmt)
continue;
if (c->ltab[i].free == c->leb_size) {
c->ltab[i].cmt = 1;
*lnum = i + c->lpt_first;
return 0;
}
}
return -ENOSPC;
}
/**
* layout_cnodes - layout cnodes for commit.
* @c: UBIFS file-system description object
*
* This function returns %0 on success and a negative error code on failure.
*/
static int layout_cnodes(struct ubifs_info *c)
{
int lnum, offs, len, alen, done_lsave, done_ltab, err;
struct ubifs_cnode *cnode;
err = dbg_chk_lpt_sz(c, 0, 0);
if (err)
return err;
cnode = c->lpt_cnext;
if (!cnode)
return 0;
lnum = c->nhead_lnum;
offs = c->nhead_offs;
/* Try to place lsave and ltab nicely */
done_lsave = !c->big_lpt;
done_ltab = 0;
if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
done_lsave = 1;
c->lsave_lnum = lnum;
c->lsave_offs = offs;
offs += c->lsave_sz;
dbg_chk_lpt_sz(c, 1, c->lsave_sz);
}
if (offs + c->ltab_sz <= c->leb_size) {
done_ltab = 1;
c->ltab_lnum = lnum;
c->ltab_offs = offs;
offs += c->ltab_sz;
dbg_chk_lpt_sz(c, 1, c->ltab_sz);
}
do {
if (cnode->level) {
len = c->nnode_sz;
c->dirty_nn_cnt -= 1;
} else {
len = c->pnode_sz;
c->dirty_pn_cnt -= 1;
}
while (offs + len > c->leb_size) {
alen = ALIGN(offs, c->min_io_size);
upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
err = alloc_lpt_leb(c, &lnum);
if (err)
goto no_space;
offs = 0;
ubifs_assert(c, lnum >= c->lpt_first &&
lnum <= c->lpt_last);
/* Try to place lsave and ltab nicely */
if (!done_lsave) {
done_lsave = 1;
c->lsave_lnum = lnum;
c->lsave_offs = offs;
offs += c->lsave_sz;
dbg_chk_lpt_sz(c, 1, c->lsave_sz);
continue;
}
if (!done_ltab) {
done_ltab = 1;
c->ltab_lnum = lnum;
c->ltab_offs = offs;
offs += c->ltab_sz;
dbg_chk_lpt_sz(c, 1, c->ltab_sz);
continue;
}
break;
}
if (cnode->parent) {
cnode->parent->nbranch[cnode->iip].lnum = lnum;
cnode->parent->nbranch[cnode->iip].offs = offs;
} else {
c->lpt_lnum = lnum;
c->lpt_offs = offs;
}
offs += len;
dbg_chk_lpt_sz(c, 1, len);
cnode = cnode->cnext;
} while (cnode && cnode != c->lpt_cnext);
/* Make sure to place LPT's save table */
if (!done_lsave) {
if (offs + c->lsave_sz > c->leb_size) {
alen = ALIGN(offs, c->min_io_size);
upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
err = alloc_lpt_leb(c, &lnum);
if (err)
goto no_space;
offs = 0;
ubifs_assert(c, lnum >= c->lpt_first &&
lnum <= c->lpt_last);
}
done_lsave = 1;
c->lsave_lnum = lnum;
c->lsave_offs = offs;
offs += c->lsave_sz;
dbg_chk_lpt_sz(c, 1, c->lsave_sz);
}
/* Make sure to place LPT's own lprops table */
if (!done_ltab) {
if (offs + c->ltab_sz > c->leb_size) {
alen = ALIGN(offs, c->min_io_size);
upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
err = alloc_lpt_leb(c, &lnum);
if (err)
goto no_space;
offs = 0;
ubifs_assert(c, lnum >= c->lpt_first &&
lnum <= c->lpt_last);
}
c->ltab_lnum = lnum;
c->ltab_offs = offs;
offs += c->ltab_sz;
dbg_chk_lpt_sz(c, 1, c->ltab_sz);
}
alen = ALIGN(offs, c->min_io_size);
upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
dbg_chk_lpt_sz(c, 4, alen - offs);
err = dbg_chk_lpt_sz(c, 3, alen);
if (err)
return err;
return 0;
no_space:
ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
lnum, offs, len, done_ltab, done_lsave);
ubifs_dump_lpt_info(c);
ubifs_dump_lpt_lebs(c);
dump_stack();
return err;
}
/**
* realloc_lpt_leb - allocate an LPT LEB that is empty.
* @c: UBIFS file-system description object
* @lnum: LEB number is passed and returned here
*
* This function duplicates exactly the results of the function alloc_lpt_leb.
* It is used during end commit to reallocate the same LEB numbers that were
* allocated by alloc_lpt_leb during start commit.
*
* This function finds the next LEB that was allocated by the alloc_lpt_leb
* function starting from @lnum. If a LEB is found it is returned in @lnum and
* the function returns %0. Otherwise the function returns -ENOSPC.
* Note however, that LPT is designed never to run out of space.
*/
static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
{
int i, n;
n = *lnum - c->lpt_first + 1;
for (i = n; i < c->lpt_lebs; i++)
if (c->ltab[i].cmt) {
c->ltab[i].cmt = 0;
*lnum = i + c->lpt_first;
return 0;
}
for (i = 0; i < n; i++)
if (c->ltab[i].cmt) {
c->ltab[i].cmt = 0;
*lnum = i + c->lpt_first;
return 0;
}
return -ENOSPC;
}
/**
* write_cnodes - write cnodes for commit.
* @c: UBIFS file-system description object
*
* This function returns %0 on success and a negative error code on failure.
*/
static int write_cnodes(struct ubifs_info *c)
{
int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
struct ubifs_cnode *cnode;
void *buf = c->lpt_buf;
cnode = c->lpt_cnext;
if (!cnode)
return 0;
lnum = c->nhead_lnum;
offs = c->nhead_offs;
from = offs;
/* Ensure empty LEB is unmapped */
if (offs == 0) {
err = ubifs_leb_unmap(c, lnum);
if (err)
return err;
}
/* Try to place lsave and ltab nicely */
done_lsave = !c->big_lpt;
done_ltab = 0;
if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
done_lsave = 1;
ubifs_pack_lsave(c, buf + offs, c->lsave);
offs += c->lsave_sz;
dbg_chk_lpt_sz(c, 1, c->lsave_sz);
}
if (offs + c->ltab_sz <= c->leb_size) {
done_ltab = 1;
ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
offs += c->ltab_sz;
dbg_chk_lpt_sz(c, 1, c->ltab_sz);
}
/* Loop for each cnode */
do {
if (cnode->level)
len = c->nnode_sz;
else
len = c->pnode_sz;
while (offs + len > c->leb_size) {
wlen = offs - from;
if (wlen) {
alen = ALIGN(wlen, c->min_io_size);
memset(buf + offs, 0xff, alen - wlen);
err = ubifs_leb_write(c, lnum, buf + from, from,
alen);
if (err)
return err;
}
dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
err = realloc_lpt_leb(c, &lnum);
if (err)
goto no_space;
offs = from = 0;
ubifs_assert(c, lnum >= c->lpt_first &&
lnum <= c->lpt_last);
err = ubifs_leb_unmap(c, lnum);
if (err)
return err;
/* Try to place lsave and ltab nicely */
if (!done_lsave) {
done_lsave = 1;
ubifs_pack_lsave(c, buf + offs, c->lsave);
offs += c->lsave_sz;
dbg_chk_lpt_sz(c, 1, c->lsave_sz);
continue;
}
if (!done_ltab) {
done_ltab = 1;
ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
offs += c->ltab_sz;
dbg_chk_lpt_sz(c, 1, c->ltab_sz);
continue;
}
break;
}
if (cnode->level)
ubifs_pack_nnode(c, buf + offs,
(struct ubifs_nnode *)cnode);
else
ubifs_pack_pnode(c, buf + offs,
(struct ubifs_pnode *)cnode);
/*
* The reason for the barriers is the same as in case of TNC.
* See comment in 'write_index()'. 'dirty_cow_nnode()' and
* 'dirty_cow_pnode()' are the functions for which this is
* important.
*/
clear_bit(DIRTY_CNODE, &cnode->flags);
smp_mb__before_atomic();
clear_bit(COW_CNODE, &cnode->flags);
smp_mb__after_atomic();
offs += len;
dbg_chk_lpt_sz(c, 1, len);
cnode = cnode->cnext;
} while (cnode && cnode != c->lpt_cnext);
/* Make sure to place LPT's save table */
if (!done_lsave) {
if (offs + c->lsave_sz > c->leb_size) {
wlen = offs - from;
alen = ALIGN(wlen, c->min_io_size);
memset(buf + offs, 0xff, alen - wlen);
err = ubifs_leb_write(c, lnum, buf + from, from, alen);
if (err)
return err;
dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
err = realloc_lpt_leb(c, &lnum);
if (err)
goto no_space;
offs = from = 0;
ubifs_assert(c, lnum >= c->lpt_first &&
lnum <= c->lpt_last);
err = ubifs_leb_unmap(c, lnum);
if (err)
return err;
}
done_lsave = 1;
ubifs_pack_lsave(c, buf + offs, c->lsave);
offs += c->lsave_sz;
dbg_chk_lpt_sz(c, 1, c->lsave_sz);
}
/* Make sure to place LPT's own lprops table */
if (!done_ltab) {
if (offs + c->ltab_sz > c->leb_size) {
wlen = offs - from;
alen = ALIGN(wlen, c->min_io_size);
memset(buf + offs, 0xff, alen - wlen);
err = ubifs_leb_write(c, lnum, buf + from, from, alen);
if (err)
return err;
dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
err = realloc_lpt_leb(c, &lnum);
if (err)
goto no_space;
offs = from = 0;
ubifs_assert(c, lnum >= c->lpt_first &&
lnum <= c->lpt_last);
err = ubifs_leb_unmap(c, lnum);
if (err)
return err;
}
ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
offs += c->ltab_sz;
dbg_chk_lpt_sz(c, 1, c->ltab_sz);
}
/* Write remaining data in buffer */
wlen = offs - from;
alen = ALIGN(wlen, c->min_io_size);
memset(buf + offs, 0xff, alen - wlen);
err = ubifs_leb_write(c, lnum, buf + from, from, alen);
if (err)
return err;
dbg_chk_lpt_sz(c, 4, alen - wlen);
err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
if (err)
return err;
c->nhead_lnum = lnum;
c->nhead_offs = ALIGN(offs, c->min_io_size);
dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
if (c->big_lpt)
dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
return 0;
no_space:
ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
lnum, offs, len, done_ltab, done_lsave);
ubifs_dump_lpt_info(c);
ubifs_dump_lpt_lebs(c);
dump_stack();
return err;
}
/**
* next_pnode_to_dirty - find next pnode to dirty.
* @c: UBIFS file-system description object
* @pnode: pnode
*
* This function returns the next pnode to dirty or %NULL if there are no more
* pnodes. Note that pnodes that have never been written (lnum == 0) are
* skipped.
*/
static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
struct ubifs_pnode *pnode)
{
struct ubifs_nnode *nnode;
int iip;
/* Try to go right */
nnode = pnode->parent;
for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
if (nnode->nbranch[iip].lnum)
return ubifs_get_pnode(c, nnode, iip);
}
/* Go up while can't go right */
do {
iip = nnode->iip + 1;
nnode = nnode->parent;
if (!nnode)
return NULL;
for (; iip < UBIFS_LPT_FANOUT; iip++) {
if (nnode->nbranch[iip].lnum)
break;
}
} while (iip >= UBIFS_LPT_FANOUT);
/* Go right */
nnode = ubifs_get_nnode(c, nnode, iip);
if (IS_ERR(nnode))
return (void *)nnode;
/* Go down to level 1 */
while (nnode->level > 1) {
for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
if (nnode->nbranch[iip].lnum)
break;
}
if (iip >= UBIFS_LPT_FANOUT) {
/*
* Should not happen, but we need to keep going
* if it does.
*/
iip = 0;
}
nnode = ubifs_get_nnode(c, nnode, iip);
if (IS_ERR(nnode))
return (void *)nnode;
}
for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
if (nnode->nbranch[iip].lnum)
break;
if (iip >= UBIFS_LPT_FANOUT)
/* Should not happen, but we need to keep going if it does */
iip = 0;
return ubifs_get_pnode(c, nnode, iip);
}
/**
* add_pnode_dirt - add dirty space to LPT LEB properties.
* @c: UBIFS file-system description object
* @pnode: pnode for which to add dirt
*/
static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
{
ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
c->pnode_sz);
}
/**
* do_make_pnode_dirty - mark a pnode dirty.
* @c: UBIFS file-system description object
* @pnode: pnode to mark dirty
*/
static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
{
/* Assumes cnext list is empty i.e. not called during commit */
if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
struct ubifs_nnode *nnode;
c->dirty_pn_cnt += 1;
add_pnode_dirt(c, pnode);
/* Mark parent and ancestors dirty too */
nnode = pnode->parent;
while (nnode) {
if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
c->dirty_nn_cnt += 1;
ubifs_add_nnode_dirt(c, nnode);
nnode = nnode->parent;
} else
break;
}
}
}
/**
* make_tree_dirty - mark the entire LEB properties tree dirty.
* @c: UBIFS file-system description object
*
* This function is used by the "small" LPT model to cause the entire LEB
* properties tree to be written. The "small" LPT model does not use LPT
* garbage collection because it is more efficient to write the entire tree
* (because it is small).
*
* This function returns %0 on success and a negative error code on failure.
*/
static int make_tree_dirty(struct ubifs_info *c)
{
struct ubifs_pnode *pnode;
pnode = ubifs_pnode_lookup(c, 0);
if (IS_ERR(pnode))
return PTR_ERR(pnode);
while (pnode) {
do_make_pnode_dirty(c, pnode);
pnode = next_pnode_to_dirty(c, pnode);
if (IS_ERR(pnode))
return PTR_ERR(pnode);
}
return 0;
}
/**
* need_write_all - determine if the LPT area is running out of free space.
* @c: UBIFS file-system description object
*
* This function returns %1 if the LPT area is running out of free space and %0
* if it is not.
*/
static int need_write_all(struct ubifs_info *c)
{
long long free = 0;
int i;
for (i = 0; i < c->lpt_lebs; i++) {
if (i + c->lpt_first == c->nhead_lnum)
free += c->leb_size - c->nhead_offs;
else if (c->ltab[i].free == c->leb_size)
free += c->leb_size;
else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
free += c->leb_size;
}
/* Less than twice the size left */
if (free <= c->lpt_sz * 2)
return 1;
return 0;
}
/**
* lpt_tgc_start - start trivial garbage collection of LPT LEBs.
* @c: UBIFS file-system description object
*
* LPT trivial garbage collection is where a LPT LEB contains only dirty and
* free space and so may be reused as soon as the next commit is completed.
* This function is called during start commit to mark LPT LEBs for trivial GC.
*/
static void lpt_tgc_start(struct ubifs_info *c)
{
int i;
for (i = 0; i < c->lpt_lebs; i++) {
if (i + c->lpt_first == c->nhead_lnum)
continue;
if (c->ltab[i].dirty > 0 &&
c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
c->ltab[i].tgc = 1;
c->ltab[i].free = c->leb_size;
c->ltab[i].dirty = 0;
dbg_lp("LEB %d", i + c->lpt_first);
}
}
}
/**
* lpt_tgc_end - end trivial garbage collection of LPT LEBs.
* @c: UBIFS file-system description object
*
* LPT trivial garbage collection is where a LPT LEB contains only dirty and
* free space and so may be reused as soon as the next commit is completed.
* This function is called after the commit is completed (master node has been
* written) and un-maps LPT LEBs that were marked for trivial GC.
*/
static int lpt_tgc_end(struct ubifs_info *c)
{
int i, err;
for (i = 0; i < c->lpt_lebs; i++)
if (c->ltab[i].tgc) {
err = ubifs_leb_unmap(c, i + c->lpt_first);
if (err)
return err;
c->ltab[i].tgc = 0;
dbg_lp("LEB %d", i + c->lpt_first);
}
return 0;
}
/**
* populate_lsave - fill the lsave array with important LEB numbers.
* @c: the UBIFS file-system description object
*
* This function is only called for the "big" model. It records a small number
* of LEB numbers of important LEBs. Important LEBs are ones that are (from
* most important to least important): empty, freeable, freeable index, dirty
* index, dirty or free. Upon mount, we read this list of LEB numbers and bring
* their pnodes into memory. That will stop us from having to scan the LPT
* straight away. For the "small" model we assume that scanning the LPT is no
* big deal.
*/
static void populate_lsave(struct ubifs_info *c)
{
struct ubifs_lprops *lprops;
struct ubifs_lpt_heap *heap;
int i, cnt = 0;
ubifs_assert(c, c->big_lpt);
if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
c->lpt_drty_flgs |= LSAVE_DIRTY;
ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
}
if (dbg_populate_lsave(c))
return;
list_for_each_entry(lprops, &c->empty_list, list) {
c->lsave[cnt++] = lprops->lnum;
if (cnt >= c->lsave_cnt)
return;
}
list_for_each_entry(lprops, &c->freeable_list, list) {
c->lsave[cnt++] = lprops->lnum;
if (cnt >= c->lsave_cnt)
return;
}
list_for_each_entry(lprops, &c->frdi_idx_list, list) {
c->lsave[cnt++] = lprops->lnum;
if (cnt >= c->lsave_cnt)
return;
}
heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
for (i = 0; i < heap->cnt; i++) {
c->lsave[cnt++] = heap->arr[i]->lnum;
if (cnt >= c->lsave_cnt)
return;
}
heap = &c->lpt_heap[LPROPS_DIRTY - 1];
for (i = 0; i < heap->cnt; i++) {
c->lsave[cnt++] = heap->arr[i]->lnum;
if (cnt >= c->lsave_cnt)
return;
}
heap = &c->lpt_heap[LPROPS_FREE - 1];
for (i = 0; i < heap->cnt; i++) {
c->lsave[cnt++] = heap->arr[i]->lnum;
if (cnt >= c->lsave_cnt)
return;
}
/* Fill it up completely */
while (cnt < c->lsave_cnt)
c->lsave[cnt++] = c->main_first;
}
/**
* nnode_lookup - lookup a nnode in the LPT.
* @c: UBIFS file-system description object
* @i: nnode number
*
* This function returns a pointer to the nnode on success or a negative
* error code on failure.
*/
static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
{
int err, iip;
struct ubifs_nnode *nnode;
if (!c->nroot) {
err = ubifs_read_nnode(c, NULL, 0);
if (err)
return ERR_PTR(err);
}
nnode = c->nroot;
while (1) {
iip = i & (UBIFS_LPT_FANOUT - 1);
i >>= UBIFS_LPT_FANOUT_SHIFT;
if (!i)
break;
nnode = ubifs_get_nnode(c, nnode, iip);
if (IS_ERR(nnode))
return nnode;
}
return nnode;
}
/**
* make_nnode_dirty - find a nnode and, if found, make it dirty.
* @c: UBIFS file-system description object
* @node_num: nnode number of nnode to make dirty
* @lnum: LEB number where nnode was written
* @offs: offset where nnode was written
*
* This function is used by LPT garbage collection. LPT garbage collection is
* used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
* simply involves marking all the nodes in the LEB being garbage-collected as
* dirty. The dirty nodes are written next commit, after which the LEB is free
* to be reused.
*
* This function returns %0 on success and a negative error code on failure.
*/
static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
int offs)
{
struct ubifs_nnode *nnode;
nnode = nnode_lookup(c, node_num);
if (IS_ERR(nnode))
return PTR_ERR(nnode);
if (nnode->parent) {
struct ubifs_nbranch *branch;
branch = &nnode->parent->nbranch[nnode->iip];
if (branch->lnum != lnum || branch->offs != offs)
return 0; /* nnode is obsolete */
} else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
return 0; /* nnode is obsolete */
/* Assumes cnext list is empty i.e. not called during commit */
if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
c->dirty_nn_cnt += 1;
ubifs_add_nnode_dirt(c, nnode);
/* Mark parent and ancestors dirty too */
nnode = nnode->parent;
while (nnode) {
if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
c->dirty_nn_cnt += 1;
ubifs_add_nnode_dirt(c, nnode);
nnode = nnode->parent;
} else
break;
}
}
return 0;
}
/**
* make_pnode_dirty - find a pnode and, if found, make it dirty.
* @c: UBIFS file-system description object
* @node_num: pnode number of pnode to make dirty
* @lnum: LEB number where pnode was written
* @offs: offset where pnode was written
*
* This function is used by LPT garbage collection. LPT garbage collection is
* used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
* simply involves marking all the nodes in the LEB being garbage-collected as
* dirty. The dirty nodes are written next commit, after which the LEB is free
* to be reused.
*
* This function returns %0 on success and a negative error code on failure.
*/
static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
int offs)
{
struct ubifs_pnode *pnode;
struct ubifs_nbranch *branch;
pnode = ubifs_pnode_lookup(c, node_num);
if (IS_ERR(pnode))
return PTR_ERR(pnode);
branch = &pnode->parent->nbranch[pnode->iip];
if (branch->lnum != lnum || branch->offs != offs)
return 0;
do_make_pnode_dirty(c, pnode);
return 0;
}
/**
* make_ltab_dirty - make ltab node dirty.
* @c: UBIFS file-system description object
* @lnum: LEB number where ltab was written
* @offs: offset where ltab was written
*
* This function is used by LPT garbage collection. LPT garbage collection is
* used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
* simply involves marking all the nodes in the LEB being garbage-collected as
* dirty. The dirty nodes are written next commit, after which the LEB is free
* to be reused.
*
* This function returns %0 on success and a negative error code on failure.
*/
static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
{
if (lnum != c->ltab_lnum || offs != c->ltab_offs)
return 0; /* This ltab node is obsolete */
if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
c->lpt_drty_flgs |= LTAB_DIRTY;
ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
}
return 0;
}
/**
* make_lsave_dirty - make lsave node dirty.
* @c: UBIFS file-system description object
* @lnum: LEB number where lsave was written
* @offs: offset where lsave was written
*
* This function is used by LPT garbage collection. LPT garbage collection is
* used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
* simply involves marking all the nodes in the LEB being garbage-collected as
* dirty. The dirty nodes are written next commit, after which the LEB is free
* to be reused.
*
* This function returns %0 on success and a negative error code on failure.
*/
static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
{
if (lnum != c->lsave_lnum || offs != c->lsave_offs)
return 0; /* This lsave node is obsolete */
if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
c->lpt_drty_flgs |= LSAVE_DIRTY;
ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
}
return 0;
}
/**
* make_node_dirty - make node dirty.
* @c: UBIFS file-system description object
* @node_type: LPT node type
* @node_num: node number
* @lnum: LEB number where node was written
* @offs: offset where node was written
*
* This function is used by LPT garbage collection. LPT garbage collection is
* used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
* simply involves marking all the nodes in the LEB being garbage-collected as
* dirty. The dirty nodes are written next commit, after which the LEB is free
* to be reused.
*
* This function returns %0 on success and a negative error code on failure.
*/
static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
int lnum, int offs)
{
switch (node_type) {
case UBIFS_LPT_NNODE:
return make_nnode_dirty(c, node_num, lnum, offs);
case UBIFS_LPT_PNODE:
return make_pnode_dirty(c, node_num, lnum, offs);
case UBIFS_LPT_LTAB:
return make_ltab_dirty(c, lnum, offs);