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#include "ubifs.h"
34
35
36
37
38
39
40
41
42
43
44
45enum {
46 NAME_LESS = 0,
47 NAME_MATCHES = 1,
48 NAME_GREATER = 2,
49 NOT_ON_MEDIA = 3,
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
75static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
76{
77 struct ubifs_old_idx *old_idx, *o;
78 struct rb_node **p, *parent = NULL;
79
80 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
81 if (unlikely(!old_idx))
82 return -ENOMEM;
83 old_idx->lnum = lnum;
84 old_idx->offs = offs;
85
86 p = &c->old_idx.rb_node;
87 while (*p) {
88 parent = *p;
89 o = rb_entry(parent, struct ubifs_old_idx, rb);
90 if (lnum < o->lnum)
91 p = &(*p)->rb_left;
92 else if (lnum > o->lnum)
93 p = &(*p)->rb_right;
94 else if (offs < o->offs)
95 p = &(*p)->rb_left;
96 else if (offs > o->offs)
97 p = &(*p)->rb_right;
98 else {
99 ubifs_err("old idx added twice!");
100 kfree(old_idx);
101 return 0;
102 }
103 }
104 rb_link_node(&old_idx->rb, parent, p);
105 rb_insert_color(&old_idx->rb, &c->old_idx);
106 return 0;
107}
108
109
110
111
112
113
114
115
116int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
117{
118 if (znode->parent) {
119 struct ubifs_zbranch *zbr;
120
121 zbr = &znode->parent->zbranch[znode->iip];
122 if (zbr->len)
123 return insert_old_idx(c, zbr->lnum, zbr->offs);
124 } else
125 if (c->zroot.len)
126 return insert_old_idx(c, c->zroot.lnum,
127 c->zroot.offs);
128 return 0;
129}
130
131
132
133
134
135
136
137
138static int ins_clr_old_idx_znode(struct ubifs_info *c,
139 struct ubifs_znode *znode)
140{
141 int err;
142
143 if (znode->parent) {
144 struct ubifs_zbranch *zbr;
145
146 zbr = &znode->parent->zbranch[znode->iip];
147 if (zbr->len) {
148 err = insert_old_idx(c, zbr->lnum, zbr->offs);
149 if (err)
150 return err;
151 zbr->lnum = 0;
152 zbr->offs = 0;
153 zbr->len = 0;
154 }
155 } else
156 if (c->zroot.len) {
157 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
158 if (err)
159 return err;
160 c->zroot.lnum = 0;
161 c->zroot.offs = 0;
162 c->zroot.len = 0;
163 }
164 return 0;
165}
166
167
168
169
170
171
172
173
174
175
176
177void destroy_old_idx(struct ubifs_info *c)
178{
179 struct rb_node *this = c->old_idx.rb_node;
180 struct ubifs_old_idx *old_idx;
181
182 while (this) {
183 if (this->rb_left) {
184 this = this->rb_left;
185 continue;
186 } else if (this->rb_right) {
187 this = this->rb_right;
188 continue;
189 }
190 old_idx = rb_entry(this, struct ubifs_old_idx, rb);
191 this = rb_parent(this);
192 if (this) {
193 if (this->rb_left == &old_idx->rb)
194 this->rb_left = NULL;
195 else
196 this->rb_right = NULL;
197 }
198 kfree(old_idx);
199 }
200 c->old_idx = RB_ROOT;
201}
202
203
204
205
206
207
208
209
210static struct ubifs_znode *copy_znode(struct ubifs_info *c,
211 struct ubifs_znode *znode)
212{
213 struct ubifs_znode *zn;
214
215 zn = kmalloc(c->max_znode_sz, GFP_NOFS);
216 if (unlikely(!zn))
217 return ERR_PTR(-ENOMEM);
218
219 memcpy(zn, znode, c->max_znode_sz);
220 zn->cnext = NULL;
221 __set_bit(DIRTY_ZNODE, &zn->flags);
222 __clear_bit(COW_ZNODE, &zn->flags);
223
224 ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
225 __set_bit(OBSOLETE_ZNODE, &znode->flags);
226
227 if (znode->level != 0) {
228 int i;
229 const int n = zn->child_cnt;
230
231
232 for (i = 0; i < n; i++) {
233 struct ubifs_zbranch *zbr = &zn->zbranch[i];
234
235 if (zbr->znode)
236 zbr->znode->parent = zn;
237 }
238 }
239
240 atomic_long_inc(&c->dirty_zn_cnt);
241 return zn;
242}
243
244
245
246
247
248
249
250
251
252static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
253{
254 c->calc_idx_sz -= ALIGN(dirt, 8);
255 return ubifs_add_dirt(c, lnum, dirt);
256}
257
258
259
260
261
262
263
264
265static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
266 struct ubifs_zbranch *zbr)
267{
268 struct ubifs_znode *znode = zbr->znode;
269 struct ubifs_znode *zn;
270 int err;
271
272 if (!test_bit(COW_ZNODE, &znode->flags)) {
273
274 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
275 atomic_long_inc(&c->dirty_zn_cnt);
276 atomic_long_dec(&c->clean_zn_cnt);
277 atomic_long_dec(&ubifs_clean_zn_cnt);
278 err = add_idx_dirt(c, zbr->lnum, zbr->len);
279 if (unlikely(err))
280 return ERR_PTR(err);
281 }
282 return znode;
283 }
284
285 zn = copy_znode(c, znode);
286 if (IS_ERR(zn))
287 return zn;
288
289 if (zbr->len) {
290 err = insert_old_idx(c, zbr->lnum, zbr->offs);
291 if (unlikely(err))
292 return ERR_PTR(err);
293 err = add_idx_dirt(c, zbr->lnum, zbr->len);
294 } else
295 err = 0;
296
297 zbr->znode = zn;
298 zbr->lnum = 0;
299 zbr->offs = 0;
300 zbr->len = 0;
301
302 if (unlikely(err))
303 return ERR_PTR(err);
304 return zn;
305}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
328 const void *node)
329{
330 int err;
331 void *lnc_node;
332 const struct ubifs_dent_node *dent = node;
333
334 ubifs_assert(!zbr->leaf);
335 ubifs_assert(zbr->len != 0);
336 ubifs_assert(is_hash_key(c, &zbr->key));
337
338 err = ubifs_validate_entry(c, dent);
339 if (err) {
340 dbg_dump_stack();
341 dbg_dump_node(c, dent);
342 return err;
343 }
344
345 lnc_node = kmalloc(zbr->len, GFP_NOFS);
346 if (!lnc_node)
347
348 return 0;
349
350 memcpy(lnc_node, node, zbr->len);
351 zbr->leaf = lnc_node;
352 return 0;
353}
354
355
356
357
358
359
360
361
362
363
364static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
365 void *node)
366{
367 int err;
368
369 ubifs_assert(!zbr->leaf);
370 ubifs_assert(zbr->len != 0);
371
372 err = ubifs_validate_entry(c, node);
373 if (err) {
374 dbg_dump_stack();
375 dbg_dump_node(c, node);
376 return err;
377 }
378
379 zbr->leaf = node;
380 return 0;
381}
382
383
384
385
386
387
388static void lnc_free(struct ubifs_zbranch *zbr)
389{
390 if (!zbr->leaf)
391 return;
392 kfree(zbr->leaf);
393 zbr->leaf = NULL;
394}
395
396
397
398
399
400
401
402
403
404
405
406
407static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
408 void *node)
409{
410 int err;
411
412 ubifs_assert(is_hash_key(c, &zbr->key));
413
414 if (zbr->leaf) {
415
416 ubifs_assert(zbr->len != 0);
417 memcpy(node, zbr->leaf, zbr->len);
418 return 0;
419 }
420
421 err = ubifs_tnc_read_node(c, zbr, node);
422 if (err)
423 return err;
424
425
426 err = lnc_add(c, zbr, node);
427 return err;
428}
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451static int try_read_node(const struct ubifs_info *c, void *buf, int type,
452 int len, int lnum, int offs)
453{
454 int err, node_len;
455 struct ubifs_ch *ch = buf;
456 uint32_t crc, node_crc;
457
458 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
459
460 err = ubi_read(c->ubi, lnum, buf, offs, len);
461 if (err) {
462 ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
463 type, lnum, offs, err);
464 return err;
465 }
466
467 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
468 return 0;
469
470 if (ch->node_type != type)
471 return 0;
472
473 node_len = le32_to_cpu(ch->len);
474 if (node_len != len)
475 return 0;
476
477 if (type == UBIFS_DATA_NODE && !c->always_chk_crc && c->no_chk_data_crc)
478 return 1;
479
480 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
481 node_crc = le32_to_cpu(ch->crc);
482 if (crc != node_crc)
483 return 0;
484
485 return 1;
486}
487
488
489
490
491
492
493
494
495
496
497
498static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
499 struct ubifs_zbranch *zbr, void *node)
500{
501 int ret;
502
503 dbg_tnc("LEB %d:%d, key %s", zbr->lnum, zbr->offs, DBGKEY(key));
504
505 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
506 zbr->offs);
507 if (ret == 1) {
508 union ubifs_key node_key;
509 struct ubifs_dent_node *dent = node;
510
511
512 key_read(c, &dent->key, &node_key);
513 if (keys_cmp(c, key, &node_key) != 0)
514 ret = 0;
515 }
516 if (ret == 0 && c->replaying)
517 dbg_mnt("dangling branch LEB %d:%d len %d, key %s",
518 zbr->lnum, zbr->offs, zbr->len, DBGKEY(key));
519 return ret;
520}
521
522
523
524
525
526
527
528
529
530
531
532
533static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
534 const struct qstr *nm)
535{
536 struct ubifs_dent_node *dent;
537 int nlen, err;
538
539
540 if (!zbr->leaf) {
541 dent = kmalloc(zbr->len, GFP_NOFS);
542 if (!dent)
543 return -ENOMEM;
544
545 err = ubifs_tnc_read_node(c, zbr, dent);
546 if (err)
547 goto out_free;
548
549
550 err = lnc_add_directly(c, zbr, dent);
551 if (err)
552 goto out_free;
553 } else
554 dent = zbr->leaf;
555
556 nlen = le16_to_cpu(dent->nlen);
557 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
558 if (err == 0) {
559 if (nlen == nm->len)
560 return NAME_MATCHES;
561 else if (nlen < nm->len)
562 return NAME_LESS;
563 else
564 return NAME_GREATER;
565 } else if (err < 0)
566 return NAME_LESS;
567 else
568 return NAME_GREATER;
569
570out_free:
571 kfree(dent);
572 return err;
573}
574
575
576
577
578
579
580
581
582
583static struct ubifs_znode *get_znode(struct ubifs_info *c,
584 struct ubifs_znode *znode, int n)
585{
586 struct ubifs_zbranch *zbr;
587
588 zbr = &znode->zbranch[n];
589 if (zbr->znode)
590 znode = zbr->znode;
591 else
592 znode = ubifs_load_znode(c, zbr, znode, n);
593 return znode;
594}
595
596
597
598
599
600
601
602
603
604
605static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
606{
607 struct ubifs_znode *znode = *zn;
608 int nn = *n;
609
610 nn += 1;
611 if (nn < znode->child_cnt) {
612 *n = nn;
613 return 0;
614 }
615 while (1) {
616 struct ubifs_znode *zp;
617
618 zp = znode->parent;
619 if (!zp)
620 return -ENOENT;
621 nn = znode->iip + 1;
622 znode = zp;
623 if (nn < znode->child_cnt) {
624 znode = get_znode(c, znode, nn);
625 if (IS_ERR(znode))
626 return PTR_ERR(znode);
627 while (znode->level != 0) {
628 znode = get_znode(c, znode, 0);
629 if (IS_ERR(znode))
630 return PTR_ERR(znode);
631 }
632 nn = 0;
633 break;
634 }
635 }
636 *zn = znode;
637 *n = nn;
638 return 0;
639}
640
641
642
643
644
645
646
647
648
649
650static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
651{
652 struct ubifs_znode *znode = *zn;
653 int nn = *n;
654
655 if (nn > 0) {
656 *n = nn - 1;
657 return 0;
658 }
659 while (1) {
660 struct ubifs_znode *zp;
661
662 zp = znode->parent;
663 if (!zp)
664 return -ENOENT;
665 nn = znode->iip - 1;
666 znode = zp;
667 if (nn >= 0) {
668 znode = get_znode(c, znode, nn);
669 if (IS_ERR(znode))
670 return PTR_ERR(znode);
671 while (znode->level != 0) {
672 nn = znode->child_cnt - 1;
673 znode = get_znode(c, znode, nn);
674 if (IS_ERR(znode))
675 return PTR_ERR(znode);
676 }
677 nn = znode->child_cnt - 1;
678 break;
679 }
680 }
681 *zn = znode;
682 *n = nn;
683 return 0;
684}
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
703 struct ubifs_znode **zn, int *n,
704 const struct qstr *nm)
705{
706 int err;
707
708 err = matches_name(c, &(*zn)->zbranch[*n], nm);
709 if (unlikely(err < 0))
710 return err;
711 if (err == NAME_MATCHES)
712 return 1;
713
714 if (err == NAME_GREATER) {
715
716 while (1) {
717 err = tnc_prev(c, zn, n);
718 if (err == -ENOENT) {
719 ubifs_assert(*n == 0);
720 *n = -1;
721 return 0;
722 }
723 if (err < 0)
724 return err;
725 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
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 if (*n == (*zn)->child_cnt - 1) {
756 err = tnc_next(c, zn, n);
757 if (err) {
758
759 ubifs_assert(0);
760 if (err == -ENOENT)
761 err = -EINVAL;
762 return err;
763 }
764 ubifs_assert(*n == 0);
765 *n = -1;
766 }
767 return 0;
768 }
769 err = matches_name(c, &(*zn)->zbranch[*n], nm);
770 if (err < 0)
771 return err;
772 if (err == NAME_LESS)
773 return 0;
774 if (err == NAME_MATCHES)
775 return 1;
776 ubifs_assert(err == NAME_GREATER);
777 }
778 } else {
779 int nn = *n;
780 struct ubifs_znode *znode = *zn;
781
782
783 while (1) {
784 err = tnc_next(c, &znode, &nn);
785 if (err == -ENOENT)
786 return 0;
787 if (err < 0)
788 return err;
789 if (keys_cmp(c, &znode->zbranch[nn].key, key))
790 return 0;
791 err = matches_name(c, &znode->zbranch[nn], nm);
792 if (err < 0)
793 return err;
794 if (err == NAME_GREATER)
795 return 0;
796 *zn = znode;
797 *n = nn;
798 if (err == NAME_MATCHES)
799 return 1;
800 ubifs_assert(err == NAME_LESS);
801 }
802 }
803}
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820static int fallible_matches_name(struct ubifs_info *c,
821 struct ubifs_zbranch *zbr,
822 const struct qstr *nm)
823{
824 struct ubifs_dent_node *dent;
825 int nlen, err;
826
827
828 if (!zbr->leaf) {
829 dent = kmalloc(zbr->len, GFP_NOFS);
830 if (!dent)
831 return -ENOMEM;
832
833 err = fallible_read_node(c, &zbr->key, zbr, dent);
834 if (err < 0)
835 goto out_free;
836 if (err == 0) {
837
838 err = NOT_ON_MEDIA;
839 goto out_free;
840 }
841 ubifs_assert(err == 1);
842
843 err = lnc_add_directly(c, zbr, dent);
844 if (err)
845 goto out_free;
846 } else
847 dent = zbr->leaf;
848
849 nlen = le16_to_cpu(dent->nlen);
850 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
851 if (err == 0) {
852 if (nlen == nm->len)
853 return NAME_MATCHES;
854 else if (nlen < nm->len)
855 return NAME_LESS;
856 else
857 return NAME_GREATER;
858 } else if (err < 0)
859 return NAME_LESS;
860 else
861 return NAME_GREATER;
862
863out_free:
864 kfree(dent);
865 return err;
866}
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890static int fallible_resolve_collision(struct ubifs_info *c,
891 const union ubifs_key *key,
892 struct ubifs_znode **zn, int *n,
893 const struct qstr *nm, int adding)
894{
895 struct ubifs_znode *o_znode = NULL, *znode = *zn;
896 int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
897
898 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
899 if (unlikely(cmp < 0))
900 return cmp;
901 if (cmp == NAME_MATCHES)
902 return 1;
903 if (cmp == NOT_ON_MEDIA) {
904 o_znode = znode;
905 o_n = nn;
906
907
908
909
910
911 unsure = 1;
912 } else if (!adding)
913 unsure = 1;
914
915 if (cmp == NAME_GREATER || unsure) {
916
917 while (1) {
918 err = tnc_prev(c, zn, n);
919 if (err == -ENOENT) {
920 ubifs_assert(*n == 0);
921 *n = -1;
922 break;
923 }
924 if (err < 0)
925 return err;
926 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
927
928 if (*n == (*zn)->child_cnt - 1) {
929 err = tnc_next(c, zn, n);
930 if (err) {
931
932 ubifs_assert(0);
933 if (err == -ENOENT)
934 err = -EINVAL;
935 return err;
936 }
937 ubifs_assert(*n == 0);
938 *n = -1;
939 }
940 break;
941 }
942 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
943 if (err < 0)
944 return err;
945 if (err == NAME_MATCHES)
946 return 1;
947 if (err == NOT_ON_MEDIA) {
948 o_znode = *zn;
949 o_n = *n;
950 continue;
951 }
952 if (!adding)
953 continue;
954 if (err == NAME_LESS)
955 break;
956 else
957 unsure = 0;
958 }
959 }
960
961 if (cmp == NAME_LESS || unsure) {
962
963 *zn = znode;
964 *n = nn;
965 while (1) {
966 err = tnc_next(c, &znode, &nn);
967 if (err == -ENOENT)
968 break;
969 if (err < 0)
970 return err;
971 if (keys_cmp(c, &znode->zbranch[nn].key, key))
972 break;
973 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
974 if (err < 0)
975 return err;
976 if (err == NAME_GREATER)
977 break;
978 *zn = znode;
979 *n = nn;
980 if (err == NAME_MATCHES)
981 return 1;
982 if (err == NOT_ON_MEDIA) {
983 o_znode = znode;
984 o_n = nn;
985 }
986 }
987 }
988
989
990 if (adding || !o_znode)
991 return 0;
992
993 dbg_mnt("dangling match LEB %d:%d len %d %s",
994 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
995 o_znode->zbranch[o_n].len, DBGKEY(key));
996 *zn = o_znode;
997 *n = o_n;
998 return 1;
999}
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
1010{
1011 if (zbr->lnum == lnum && zbr->offs == offs)
1012 return 1;
1013 else
1014 return 0;
1015}
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034static int resolve_collision_directly(struct ubifs_info *c,
1035 const union ubifs_key *key,
1036 struct ubifs_znode **zn, int *n,
1037 int lnum, int offs)
1038{
1039 struct ubifs_znode *znode;
1040 int nn, err;
1041
1042 znode = *zn;
1043 nn = *n;
1044 if (matches_position(&znode->zbranch[nn], lnum, offs))
1045 return 1;
1046
1047
1048 while (1) {
1049 err = tnc_prev(c, &znode, &nn);
1050 if (err == -ENOENT)
1051 break;
1052 if (err < 0)
1053 return err;
1054 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1055 break;
1056 if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1057 *zn = znode;
1058 *n = nn;
1059 return 1;
1060 }
1061 }
1062
1063
1064 znode = *zn;
1065 nn = *n;
1066 while (1) {
1067 err = tnc_next(c, &znode, &nn);
1068 if (err == -ENOENT)
1069 return 0;
1070 if (err < 0)
1071 return err;
1072 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1073 return 0;
1074 *zn = znode;
1075 *n = nn;
1076 if (matches_position(&znode->zbranch[nn], lnum, offs))
1077 return 1;
1078 }
1079}
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1092 struct ubifs_znode *znode)
1093{
1094 struct ubifs_znode *zp;
1095 int *path = c->bottom_up_buf, p = 0;
1096
1097 ubifs_assert(c->zroot.znode);
1098 ubifs_assert(znode);
1099 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
1100 kfree(c->bottom_up_buf);
1101 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
1102 GFP_NOFS);
1103 if (!c->bottom_up_buf)
1104 return ERR_PTR(-ENOMEM);
1105 path = c->bottom_up_buf;
1106 }
1107 if (c->zroot.znode->level) {
1108
1109 while (1) {
1110 int n;
1111
1112 zp = znode->parent;
1113 if (!zp)
1114 break;
1115 n = znode->iip;
1116 ubifs_assert(p < c->zroot.znode->level);
1117 path[p++] = n;
1118 if (!zp->cnext && ubifs_zn_dirty(znode))
1119 break;
1120 znode = zp;
1121 }
1122 }
1123
1124
1125 while (1) {
1126 struct ubifs_zbranch *zbr;
1127
1128 zp = znode->parent;
1129 if (zp) {
1130 ubifs_assert(path[p - 1] >= 0);
1131 ubifs_assert(path[p - 1] < zp->child_cnt);
1132 zbr = &zp->zbranch[path[--p]];
1133 znode = dirty_cow_znode(c, zbr);
1134 } else {
1135 ubifs_assert(znode == c->zroot.znode);
1136 znode = dirty_cow_znode(c, &c->zroot);
1137 }
1138 if (IS_ERR(znode) || !p)
1139 break;
1140 ubifs_assert(path[p - 1] >= 0);
1141 ubifs_assert(path[p - 1] < znode->child_cnt);
1142 znode = znode->zbranch[path[p - 1]].znode;
1143 }
1144
1145 return znode;
1146}
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1171 struct ubifs_znode **zn, int *n)
1172{
1173 int err, exact;
1174 struct ubifs_znode *znode;
1175 unsigned long time = get_seconds();
1176
1177 dbg_tnc("search key %s", DBGKEY(key));
1178
1179 znode = c->zroot.znode;
1180 if (unlikely(!znode)) {
1181 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1182 if (IS_ERR(znode))
1183 return PTR_ERR(znode);
1184 }
1185
1186 znode->time = time;
1187
1188 while (1) {
1189 struct ubifs_zbranch *zbr;
1190
1191 exact = ubifs_search_zbranch(c, znode, key, n);
1192
1193 if (znode->level == 0)
1194 break;
1195
1196 if (*n < 0)
1197 *n = 0;
1198 zbr = &znode->zbranch[*n];
1199
1200 if (zbr->znode) {
1201 znode->time = time;
1202 znode = zbr->znode;
1203 continue;
1204 }
1205
1206
1207 znode = ubifs_load_znode(c, zbr, znode, *n);
1208 if (IS_ERR(znode))
1209 return PTR_ERR(znode);
1210 }
1211
1212 *zn = znode;
1213 if (exact || !is_hash_key(c, key) || *n != -1) {
1214 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1215 return exact;
1216 }
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261 err = tnc_prev(c, &znode, n);
1262 if (err == -ENOENT) {
1263 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1264 *n = -1;
1265 return 0;
1266 }
1267 if (unlikely(err < 0))
1268 return err;
1269 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1270 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1271 *n = -1;
1272 return 0;
1273 }
1274
1275 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1276 *zn = znode;
1277 return 1;
1278}
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1306 struct ubifs_znode **zn, int *n)
1307{
1308 int err, exact;
1309 struct ubifs_znode *znode;
1310 unsigned long time = get_seconds();
1311
1312 dbg_tnc("search and dirty key %s", DBGKEY(key));
1313
1314 znode = c->zroot.znode;
1315 if (unlikely(!znode)) {
1316 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1317 if (IS_ERR(znode))
1318 return PTR_ERR(znode);
1319 }
1320
1321 znode = dirty_cow_znode(c, &c->zroot);
1322 if (IS_ERR(znode))
1323 return PTR_ERR(znode);
1324
1325 znode->time = time;
1326
1327 while (1) {
1328 struct ubifs_zbranch *zbr;
1329
1330 exact = ubifs_search_zbranch(c, znode, key, n);
1331
1332 if (znode->level == 0)
1333 break;
1334
1335 if (*n < 0)
1336 *n = 0;
1337 zbr = &znode->zbranch[*n];
1338
1339 if (zbr->znode) {
1340 znode->time = time;
1341 znode = dirty_cow_znode(c, zbr);
1342 if (IS_ERR(znode))
1343 return PTR_ERR(znode);
1344 continue;
1345 }
1346
1347
1348 znode = ubifs_load_znode(c, zbr, znode, *n);
1349 if (IS_ERR(znode))
1350 return PTR_ERR(znode);
1351 znode = dirty_cow_znode(c, zbr);
1352 if (IS_ERR(znode))
1353 return PTR_ERR(znode);
1354 }
1355
1356 *zn = znode;
1357 if (exact || !is_hash_key(c, key) || *n != -1) {
1358 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1359 return exact;
1360 }
1361
1362
1363
1364
1365
1366 err = tnc_prev(c, &znode, n);
1367 if (err == -ENOENT) {
1368 *n = -1;
1369 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1370 return 0;
1371 }
1372 if (unlikely(err < 0))
1373 return err;
1374 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1375 *n = -1;
1376 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1377 return 0;
1378 }
1379
1380 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1381 znode = dirty_cow_bottom_up(c, znode);
1382 if (IS_ERR(znode))
1383 return PTR_ERR(znode);
1384 }
1385
1386 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1387 *zn = znode;
1388 return 1;
1389}
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1402{
1403
1404
1405
1406 return 0;
1407}
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1423 void *node, int *lnum, int *offs)
1424{
1425 int found, n, err, safely = 0, gc_seq1;
1426 struct ubifs_znode *znode;
1427 struct ubifs_zbranch zbr, *zt;
1428
1429again:
1430 mutex_lock(&c->tnc_mutex);
1431 found = ubifs_lookup_level0(c, key, &znode, &n);
1432 if (!found) {
1433 err = -ENOENT;
1434 goto out;
1435 } else if (found < 0) {
1436 err = found;
1437 goto out;
1438 }
1439 zt = &znode->zbranch[n];
1440 if (lnum) {
1441 *lnum = zt->lnum;
1442 *offs = zt->offs;
1443 }
1444 if (is_hash_key(c, key)) {
1445
1446
1447
1448
1449 err = tnc_read_node_nm(c, zt, node);
1450 goto out;
1451 }
1452 if (safely) {
1453 err = ubifs_tnc_read_node(c, zt, node);
1454 goto out;
1455 }
1456
1457 zbr = znode->zbranch[n];
1458 gc_seq1 = c->gc_seq;
1459 mutex_unlock(&c->tnc_mutex);
1460
1461 err = fallible_read_node(c, key, &zbr, node);
1462 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1463
1464
1465
1466
1467 safely = 1;
1468 goto again;
1469 }
1470 return 0;
1471
1472out:
1473 mutex_unlock(&c->tnc_mutex);
1474 return err;
1475}
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1491{
1492 int n, err = 0, lnum = -1, uninitialized_var(offs);
1493 int uninitialized_var(len);
1494 unsigned int block = key_block(c, &bu->key);
1495 struct ubifs_znode *znode;
1496
1497 bu->cnt = 0;
1498 bu->blk_cnt = 0;
1499 bu->eof = 0;
1500
1501 mutex_lock(&c->tnc_mutex);
1502
1503 err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1504 if (err < 0)
1505 goto out;
1506 if (err) {
1507
1508 len = znode->zbranch[n].len;
1509
1510 if (len > bu->buf_len) {
1511 err = -EINVAL;
1512 goto out;
1513 }
1514
1515 bu->zbranch[bu->cnt++] = znode->zbranch[n];
1516 bu->blk_cnt += 1;
1517 lnum = znode->zbranch[n].lnum;
1518 offs = ALIGN(znode->zbranch[n].offs + len, 8);
1519 }
1520 while (1) {
1521 struct ubifs_zbranch *zbr;
1522 union ubifs_key *key;
1523 unsigned int next_block;
1524
1525
1526 err = tnc_next(c, &znode, &n);
1527 if (err)
1528 goto out;
1529 zbr = &znode->zbranch[n];
1530 key = &zbr->key;
1531
1532 if (key_inum(c, key) != key_inum(c, &bu->key) ||
1533 key_type(c, key) != UBIFS_DATA_KEY) {
1534 err = -ENOENT;
1535 goto out;
1536 }
1537 if (lnum < 0) {
1538
1539 lnum = zbr->lnum;
1540 offs = ALIGN(zbr->offs + zbr->len, 8);
1541 len = zbr->len;
1542 if (len > bu->buf_len) {
1543 err = -EINVAL;
1544 goto out;
1545 }
1546 } else {
1547
1548
1549
1550
1551 if (zbr->lnum != lnum || zbr->offs != offs)
1552 goto out;
1553 offs += ALIGN(zbr->len, 8);
1554 len = ALIGN(len, 8) + zbr->len;
1555
1556 if (len > bu->buf_len)
1557 goto out;
1558 }
1559
1560 next_block = key_block(c, key);
1561 bu->blk_cnt += (next_block - block - 1);
1562 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1563 goto out;
1564 block = next_block;
1565
1566 bu->zbranch[bu->cnt++] = *zbr;
1567 bu->blk_cnt += 1;
1568
1569 if (bu->cnt >= UBIFS_MAX_BULK_READ)
1570 goto out;
1571 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1572 goto out;
1573 }
1574out:
1575 if (err == -ENOENT) {
1576 bu->eof = 1;
1577 err = 0;
1578 }
1579 bu->gc_seq = c->gc_seq;
1580 mutex_unlock(&c->tnc_mutex);
1581 if (err)
1582 return err;
1583
1584
1585
1586
1587 if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1588 bu->blk_cnt = UBIFS_MAX_BULK_READ;
1589
1590
1591
1592
1593 if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1594 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1595 return 0;
1596 if (bu->eof) {
1597
1598 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
1599 return 0;
1600 }
1601
1602 block = key_block(c, &bu->key) + bu->blk_cnt;
1603 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
1604 while (bu->cnt) {
1605 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
1606 break;
1607 bu->cnt -= 1;
1608 }
1609 return 0;
1610}
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620static int validate_data_node(struct ubifs_info *c, void *buf,
1621 struct ubifs_zbranch *zbr)
1622{
1623 union ubifs_key key1;
1624 struct ubifs_ch *ch = buf;
1625 int err, len;
1626
1627 if (ch->node_type != UBIFS_DATA_NODE) {
1628 ubifs_err("bad node type (%d but expected %d)",
1629 ch->node_type, UBIFS_DATA_NODE);
1630 goto out_err;
1631 }
1632
1633 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1634 if (err) {
1635 ubifs_err("expected node type %d", UBIFS_DATA_NODE);
1636 goto out;
1637 }
1638
1639 len = le32_to_cpu(ch->len);
1640 if (len != zbr->len) {
1641 ubifs_err("bad node length %d, expected %d", len, zbr->len);
1642 goto out_err;
1643 }
1644
1645
1646 key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
1647 if (!keys_eq(c, &zbr->key, &key1)) {
1648 ubifs_err("bad key in node at LEB %d:%d",
1649 zbr->lnum, zbr->offs);
1650 dbg_tnc("looked for key %s found node's key %s",
1651 DBGKEY(&zbr->key), DBGKEY1(&key1));
1652 goto out_err;
1653 }
1654
1655 return 0;
1656
1657out_err:
1658 err = -EINVAL;
1659out:
1660 ubifs_err("bad node at LEB %d:%d", zbr->lnum, zbr->offs);
1661 dbg_dump_node(c, buf);
1662 dbg_dump_stack();
1663 return err;
1664}
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1677{
1678 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
1679 void *buf;
1680
1681 len = bu->zbranch[bu->cnt - 1].offs;
1682 len += bu->zbranch[bu->cnt - 1].len - offs;
1683 if (len > bu->buf_len) {
1684 ubifs_err("buffer too small %d vs %d", bu->buf_len, len);
1685 return -EINVAL;
1686 }
1687
1688
1689 err = ubi_read(c->ubi, lnum, bu->buf, offs, len);
1690
1691
1692 if (maybe_leb_gced(c, lnum, bu->gc_seq))
1693 return -EAGAIN;
1694
1695 if (err && err != -EBADMSG) {
1696 ubifs_err("failed to read from LEB %d:%d, error %d",
1697 lnum, offs, err);
1698 dbg_dump_stack();
1699 dbg_tnc("key %s", DBGKEY(&bu->key));
1700 return err;
1701 }
1702
1703
1704 buf = bu->buf;
1705 for (i = 0; i < bu->cnt; i++) {
1706 err = validate_data_node(c, buf, &bu->zbranch[i]);
1707 if (err)
1708 return err;
1709 buf = buf + ALIGN(bu->zbranch[i].len, 8);
1710 }
1711
1712 return 0;
1713}
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1729 void *node, const struct qstr *nm)
1730{
1731 int found, n, err;
1732 struct ubifs_znode *znode;
1733
1734 dbg_tnc("name '%.*s' key %s", nm->len, nm->name, DBGKEY(key));
1735 mutex_lock(&c->tnc_mutex);
1736 found = ubifs_lookup_level0(c, key, &znode, &n);
1737 if (!found) {
1738 err = -ENOENT;
1739 goto out_unlock;
1740 } else if (found < 0) {
1741 err = found;
1742 goto out_unlock;
1743 }
1744
1745 ubifs_assert(n >= 0);
1746
1747 err = resolve_collision(c, key, &znode, &n, nm);
1748 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
1749 if (unlikely(err < 0))
1750 goto out_unlock;
1751 if (err == 0) {
1752 err = -ENOENT;
1753 goto out_unlock;
1754 }
1755
1756 err = tnc_read_node_nm(c, &znode->zbranch[n], node);
1757
1758out_unlock:
1759 mutex_unlock(&c->tnc_mutex);
1760 return err;
1761}
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1777 void *node, const struct qstr *nm)
1778{
1779 int err, len;
1780 const struct ubifs_dent_node *dent = node;
1781
1782
1783
1784
1785
1786 err = ubifs_tnc_lookup(c, key, node);
1787 if (err)
1788 return err;
1789
1790 len = le16_to_cpu(dent->nlen);
1791 if (nm->len == len && !memcmp(dent->name, nm->name, len))
1792 return 0;
1793
1794
1795
1796
1797
1798 return do_lookup_nm(c, key, node, nm);
1799}
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810static void correct_parent_keys(const struct ubifs_info *c,
1811 struct ubifs_znode *znode)
1812{
1813 union ubifs_key *key, *key1;
1814
1815 ubifs_assert(znode->parent);
1816 ubifs_assert(znode->iip == 0);
1817
1818 key = &znode->zbranch[0].key;
1819 key1 = &znode->parent->zbranch[0].key;
1820
1821 while (keys_cmp(c, key, key1) < 0) {
1822 key_copy(c, key, key1);
1823 znode = znode->parent;
1824 znode->alt = 1;
1825 if (!znode->parent || znode->iip)
1826 break;
1827 key1 = &znode->parent->zbranch[0].key;
1828 }
1829}
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842static void insert_zbranch(struct ubifs_znode *znode,
1843 const struct ubifs_zbranch *zbr, int n)
1844{
1845 int i;
1846
1847 ubifs_assert(ubifs_zn_dirty(znode));
1848
1849 if (znode->level) {
1850 for (i = znode->child_cnt; i > n; i--) {
1851 znode->zbranch[i] = znode->zbranch[i - 1];
1852 if (znode->zbranch[i].znode)
1853 znode->zbranch[i].znode->iip = i;
1854 }
1855 if (zbr->znode)
1856 zbr->znode->iip = n;
1857 } else
1858 for (i = znode->child_cnt; i > n; i--)
1859 znode->zbranch[i] = znode->zbranch[i - 1];
1860
1861 znode->zbranch[n] = *zbr;
1862 znode->child_cnt += 1;
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 if (n == 0)
1879 znode->alt = 1;
1880}
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1895 struct ubifs_zbranch *zbr, int n)
1896{
1897 struct ubifs_znode *zn, *zi, *zp;
1898 int i, keep, move, appending = 0;
1899 union ubifs_key *key = &zbr->key, *key1;
1900
1901 ubifs_assert(n >= 0 && n <= c->fanout);
1902
1903
1904again:
1905 zp = znode->parent;
1906 if (znode->child_cnt < c->fanout) {
1907 ubifs_assert(n != c->fanout);
1908 dbg_tnc("inserted at %d level %d, key %s", n, znode->level,
1909 DBGKEY(key));
1910
1911 insert_zbranch(znode, zbr, n);
1912
1913
1914 if (n == 0 && zp && znode->iip == 0)
1915 correct_parent_keys(c, znode);
1916
1917 return 0;
1918 }
1919
1920
1921
1922
1923
1924 dbg_tnc("splitting level %d, key %s", znode->level, DBGKEY(key));
1925
1926 if (znode->alt)
1927
1928
1929
1930
1931 ins_clr_old_idx_znode(c, znode);
1932
1933 zn = kzalloc(c->max_znode_sz, GFP_NOFS);
1934 if (!zn)
1935 return -ENOMEM;
1936 zn->parent = zp;
1937 zn->level = znode->level;
1938
1939
1940 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
1941
1942 if (n == c->fanout) {
1943 key1 = &znode->zbranch[n - 1].key;
1944 if (key_inum(c, key1) == key_inum(c, key) &&
1945 key_type(c, key1) == UBIFS_DATA_KEY)
1946 appending = 1;
1947 } else
1948 goto check_split;
1949 } else if (appending && n != c->fanout) {
1950
1951 appending = 0;
1952check_split:
1953 if (n >= (c->fanout + 1) / 2) {
1954 key1 = &znode->zbranch[0].key;
1955 if (key_inum(c, key1) == key_inum(c, key) &&
1956 key_type(c, key1) == UBIFS_DATA_KEY) {
1957 key1 = &znode->zbranch[n].key;
1958 if (key_inum(c, key1) != key_inum(c, key) ||
1959 key_type(c, key1) != UBIFS_DATA_KEY) {
1960 keep = n;
1961 move = c->fanout - keep;
1962 zi = znode;
1963 goto do_split;
1964 }
1965 }
1966 }
1967 }
1968
1969 if (appending) {
1970 keep = c->fanout;
1971 move = 0;
1972 } else {
1973 keep = (c->fanout + 1) / 2;
1974 move = c->fanout - keep;
1975 }
1976
1977
1978
1979
1980
1981
1982 if (n < keep) {
1983
1984 zi = znode;
1985 move += 1;
1986 keep -= 1;
1987 } else {
1988
1989 zi = zn;
1990 n -= keep;
1991
1992 if (zn->level != 0)
1993 zbr->znode->parent = zn;
1994 }
1995
1996do_split:
1997
1998 __set_bit(DIRTY_ZNODE, &zn->flags);
1999 atomic_long_inc(&c->dirty_zn_cnt);
2000
2001 zn->child_cnt = move;
2002 znode->child_cnt = keep;
2003
2004 dbg_tnc("moving %d, keeping %d", move, keep);
2005
2006
2007 for (i = 0; i < move; i++) {
2008 zn->zbranch[i] = znode->zbranch[keep + i];
2009
2010 if (zn->level != 0)
2011 if (zn->zbranch[i].znode) {
2012 zn->zbranch[i].znode->parent = zn;
2013 zn->zbranch[i].znode->iip = i;
2014 }
2015 }
2016
2017
2018 dbg_tnc("inserting at %d level %d, key %s", n, zn->level, DBGKEY(key));
2019
2020 insert_zbranch(zi, zbr, n);
2021
2022
2023 if (zp) {
2024 if (n == 0 && zi == znode && znode->iip == 0)
2025 correct_parent_keys(c, znode);
2026
2027
2028 n = znode->iip + 1;
2029
2030
2031 zbr->key = zn->zbranch[0].key;
2032 zbr->znode = zn;
2033 zbr->lnum = 0;
2034 zbr->offs = 0;
2035 zbr->len = 0;
2036 znode = zp;
2037
2038 goto again;
2039 }
2040
2041
2042 dbg_tnc("creating new zroot at level %d", znode->level + 1);
2043
2044 zi = kzalloc(c->max_znode_sz, GFP_NOFS);
2045 if (!zi)
2046 return -ENOMEM;
2047
2048 zi->child_cnt = 2;
2049 zi->level = znode->level + 1;
2050
2051 __set_bit(DIRTY_ZNODE, &zi->flags);
2052 atomic_long_inc(&c->dirty_zn_cnt);
2053
2054 zi->zbranch[0].key = znode->zbranch[0].key;
2055 zi->zbranch[0].znode = znode;
2056 zi->zbranch[0].lnum = c->zroot.lnum;
2057 zi->zbranch[0].offs = c->zroot.offs;
2058 zi->zbranch[0].len = c->zroot.len;
2059 zi->zbranch[1].key = zn->zbranch[0].key;
2060 zi->zbranch[1].znode = zn;
2061
2062 c->zroot.lnum = 0;
2063 c->zroot.offs = 0;
2064 c->zroot.len = 0;
2065 c->zroot.znode = zi;
2066
2067 zn->parent = zi;
2068 zn->iip = 1;
2069 znode->parent = zi;
2070 znode->iip = 0;
2071
2072 return 0;
2073}
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2088 int offs, int len)
2089{
2090 int found, n, err = 0;
2091 struct ubifs_znode *znode;
2092
2093 mutex_lock(&c->tnc_mutex);
2094 dbg_tnc("%d:%d, len %d, key %s", lnum, offs, len, DBGKEY(key));
2095 found = lookup_level0_dirty(c, key, &znode, &n);
2096 if (!found) {
2097 struct ubifs_zbranch zbr;
2098
2099 zbr.znode = NULL;
2100 zbr.lnum = lnum;
2101 zbr.offs = offs;
2102 zbr.len = len;
2103 key_copy(c, key, &zbr.key);
2104 err = tnc_insert(c, znode, &zbr, n + 1);
2105 } else if (found == 1) {
2106 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2107
2108 lnc_free(zbr);
2109 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2110 zbr->lnum = lnum;
2111 zbr->offs = offs;
2112 zbr->len = len;
2113 } else
2114 err = found;
2115 if (!err)
2116 err = dbg_check_tnc(c, 0);
2117 mutex_unlock(&c->tnc_mutex);
2118
2119 return err;
2120}
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
2137 int old_lnum, int old_offs, int lnum, int offs, int len)
2138{
2139 int found, n, err = 0;
2140 struct ubifs_znode *znode;
2141
2142 mutex_lock(&c->tnc_mutex);
2143 dbg_tnc("old LEB %d:%d, new LEB %d:%d, len %d, key %s", old_lnum,
2144 old_offs, lnum, offs, len, DBGKEY(key));
2145 found = lookup_level0_dirty(c, key, &znode, &n);
2146 if (found < 0) {
2147 err = found;
2148 goto out_unlock;
2149 }
2150
2151 if (found == 1) {
2152 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2153
2154 found = 0;
2155 if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
2156 lnc_free(zbr);
2157 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2158 if (err)
2159 goto out_unlock;
2160 zbr->lnum = lnum;
2161 zbr->offs = offs;
2162 zbr->len = len;
2163 found = 1;
2164 } else if (is_hash_key(c, key)) {
2165 found = resolve_collision_directly(c, key, &znode, &n,
2166 old_lnum, old_offs);
2167 dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
2168 found, znode, n, old_lnum, old_offs);
2169 if (found < 0) {
2170 err = found;
2171 goto out_unlock;
2172 }
2173
2174 if (found) {
2175
2176 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2177 znode = dirty_cow_bottom_up(c, znode);
2178 if (IS_ERR(znode)) {
2179 err = PTR_ERR(znode);
2180 goto out_unlock;
2181 }
2182 }
2183 zbr = &znode->zbranch[n];
2184 lnc_free(zbr);
2185 err = ubifs_add_dirt(c, zbr->lnum,
2186 zbr->len);
2187 if (err)
2188 goto out_unlock;
2189 zbr->lnum = lnum;
2190 zbr->offs = offs;
2191 zbr->len = len;
2192 }
2193 }
2194 }
2195
2196 if (!found)
2197 err = ubifs_add_dirt(c, lnum, len);
2198
2199 if (!err)
2200 err = dbg_check_tnc(c, 0);
2201
2202out_unlock:
2203 mutex_unlock(&c->tnc_mutex);
2204 return err;
2205}
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2220 int lnum, int offs, int len, const struct qstr *nm)
2221{
2222 int found, n, err = 0;
2223 struct ubifs_znode *znode;
2224
2225 mutex_lock(&c->tnc_mutex);
2226 dbg_tnc("LEB %d:%d, name '%.*s', key %s", lnum, offs, nm->len, nm->name,
2227 DBGKEY(key));
2228 found = lookup_level0_dirty(c, key, &znode, &n);
2229 if (found < 0) {
2230 err = found;
2231 goto out_unlock;
2232 }
2233
2234 if (found == 1) {
2235 if (c->replaying)
2236 found = fallible_resolve_collision(c, key, &znode, &n,
2237 nm, 1);
2238 else
2239 found = resolve_collision(c, key, &znode, &n, nm);
2240 dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
2241 if (found < 0) {
2242 err = found;
2243 goto out_unlock;
2244 }
2245
2246
2247 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2248 znode = dirty_cow_bottom_up(c, znode);
2249 if (IS_ERR(znode)) {
2250 err = PTR_ERR(znode);
2251 goto out_unlock;
2252 }
2253 }
2254
2255 if (found == 1) {
2256 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2257
2258 lnc_free(zbr);
2259 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2260 zbr->lnum = lnum;
2261 zbr->offs = offs;
2262 zbr->len = len;
2263 goto out_unlock;
2264 }
2265 }
2266
2267 if (!found) {
2268 struct ubifs_zbranch zbr;
2269
2270 zbr.znode = NULL;
2271 zbr.lnum = lnum;
2272 zbr.offs = offs;
2273 zbr.len = len;
2274 key_copy(c, key, &zbr.key);
2275 err = tnc_insert(c, znode, &zbr, n + 1);
2276 if (err)
2277 goto out_unlock;
2278 if (c->replaying) {
2279
2280
2281
2282
2283
2284
2285 struct qstr noname = { .len = 0, .name = "" };
2286
2287 err = dbg_check_tnc(c, 0);
2288 mutex_unlock(&c->tnc_mutex);
2289 if (err)
2290 return err;
2291 return ubifs_tnc_remove_nm(c, key, &noname);
2292 }
2293 }
2294
2295out_unlock:
2296 if (!err)
2297 err = dbg_check_tnc(c, 0);
2298 mutex_unlock(&c->tnc_mutex);
2299 return err;
2300}
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2312{
2313 struct ubifs_zbranch *zbr;
2314 struct ubifs_znode *zp;
2315 int i, err;
2316
2317
2318 ubifs_assert(znode->level == 0);
2319 ubifs_assert(n >= 0 && n < c->fanout);
2320 dbg_tnc("deleting %s", DBGKEY(&znode->zbranch[n].key));
2321
2322 zbr = &znode->zbranch[n];
2323 lnc_free(zbr);
2324
2325 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2326 if (err) {
2327 dbg_dump_znode(c, znode);
2328 return err;
2329 }
2330
2331
2332 for (i = n; i < znode->child_cnt - 1; i++)
2333 znode->zbranch[i] = znode->zbranch[i + 1];
2334 znode->child_cnt -= 1;
2335
2336 if (znode->child_cnt > 0)
2337 return 0;
2338
2339
2340
2341
2342
2343
2344 do {
2345 ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
2346 ubifs_assert(ubifs_zn_dirty(znode));
2347
2348 zp = znode->parent;
2349 n = znode->iip;
2350
2351 atomic_long_dec(&c->dirty_zn_cnt);
2352
2353 err = insert_old_idx_znode(c, znode);
2354 if (err)
2355 return err;
2356
2357 if (znode->cnext) {
2358 __set_bit(OBSOLETE_ZNODE, &znode->flags);
2359 atomic_long_inc(&c->clean_zn_cnt);
2360 atomic_long_inc(&ubifs_clean_zn_cnt);
2361 } else
2362 kfree(znode);
2363 znode = zp;
2364 } while (znode->child_cnt == 1);
2365
2366
2367 znode->child_cnt -= 1;
2368 ubifs_assert(znode->level != 0);
2369 for (i = n; i < znode->child_cnt; i++) {
2370 znode->zbranch[i] = znode->zbranch[i + 1];
2371 if (znode->zbranch[i].znode)
2372 znode->zbranch[i].znode->iip = i;
2373 }
2374
2375
2376
2377
2378
2379 if (!znode->parent) {
2380 while (znode->child_cnt == 1 && znode->level != 0) {
2381 zp = znode;
2382 zbr = &znode->zbranch[0];
2383 znode = get_znode(c, znode, 0);
2384 if (IS_ERR(znode))
2385 return PTR_ERR(znode);
2386 znode = dirty_cow_znode(c, zbr);
2387 if (IS_ERR(znode))
2388 return PTR_ERR(znode);
2389 znode->parent = NULL;
2390 znode->iip = 0;
2391 if (c->zroot.len) {
2392 err = insert_old_idx(c, c->zroot.lnum,
2393 c->zroot.offs);
2394 if (err)
2395 return err;
2396 }
2397 c->zroot.lnum = zbr->lnum;
2398 c->zroot.offs = zbr->offs;
2399 c->zroot.len = zbr->len;
2400 c->zroot.znode = znode;
2401 ubifs_assert(!test_bit(OBSOLETE_ZNODE,
2402 &zp->flags));
2403 ubifs_assert(test_bit(DIRTY_ZNODE, &zp->flags));
2404 atomic_long_dec(&c->dirty_zn_cnt);
2405
2406 if (zp->cnext) {
2407 __set_bit(OBSOLETE_ZNODE, &zp->flags);
2408 atomic_long_inc(&c->clean_zn_cnt);
2409 atomic_long_inc(&ubifs_clean_zn_cnt);
2410 } else
2411 kfree(zp);
2412 }
2413 }
2414
2415 return 0;
2416}
2417
2418
2419
2420
2421
2422
2423
2424
2425int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2426{
2427 int found, n, err = 0;
2428 struct ubifs_znode *znode;
2429
2430 mutex_lock(&c->tnc_mutex);
2431 dbg_tnc("key %s", DBGKEY(key));
2432 found = lookup_level0_dirty(c, key, &znode, &n);
2433 if (found < 0) {
2434 err = found;
2435 goto out_unlock;
2436 }
2437 if (found == 1)
2438 err = tnc_delete(c, znode, n);
2439 if (!err)
2440 err = dbg_check_tnc(c, 0);
2441
2442out_unlock:
2443 mutex_unlock(&c->tnc_mutex);
2444 return err;
2445}
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2456 const struct qstr *nm)
2457{
2458 int n, err;
2459 struct ubifs_znode *znode;
2460
2461 mutex_lock(&c->tnc_mutex);
2462 dbg_tnc("%.*s, key %s", nm->len, nm->name, DBGKEY(key));
2463 err = lookup_level0_dirty(c, key, &znode, &n);
2464 if (err < 0)
2465 goto out_unlock;
2466
2467 if (err) {
2468 if (c->replaying)
2469 err = fallible_resolve_collision(c, key, &znode, &n,
2470 nm, 0);
2471 else
2472 err = resolve_collision(c, key, &znode, &n, nm);
2473 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
2474 if (err < 0)
2475 goto out_unlock;
2476 if (err) {
2477
2478 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2479 znode = dirty_cow_bottom_up(c, znode);
2480 if (IS_ERR(znode)) {
2481 err = PTR_ERR(znode);
2482 goto out_unlock;
2483 }
2484 }
2485 err = tnc_delete(c, znode, n);
2486 }
2487 }
2488
2489out_unlock:
2490 if (!err)
2491 err = dbg_check_tnc(c, 0);
2492 mutex_unlock(&c->tnc_mutex);
2493 return err;
2494}
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
2506 union ubifs_key *from_key, union ubifs_key *to_key)
2507{
2508 if (keys_cmp(c, key, from_key) < 0)
2509 return 0;
2510 if (keys_cmp(c, key, to_key) > 0)
2511 return 0;
2512 return 1;
2513}
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2526 union ubifs_key *to_key)
2527{
2528 int i, n, k, err = 0;
2529 struct ubifs_znode *znode;
2530 union ubifs_key *key;
2531
2532 mutex_lock(&c->tnc_mutex);
2533 while (1) {
2534
2535 err = ubifs_lookup_level0(c, from_key, &znode, &n);
2536 if (err < 0)
2537 goto out_unlock;
2538
2539 if (err)
2540 key = from_key;
2541 else {
2542 err = tnc_next(c, &znode, &n);
2543 if (err == -ENOENT) {
2544 err = 0;
2545 goto out_unlock;
2546 }
2547 if (err < 0)
2548 goto out_unlock;
2549 key = &znode->zbranch[n].key;
2550 if (!key_in_range(c, key, from_key, to_key)) {
2551 err = 0;
2552 goto out_unlock;
2553 }
2554 }
2555
2556
2557 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2558 znode = dirty_cow_bottom_up(c, znode);
2559 if (IS_ERR(znode)) {
2560 err = PTR_ERR(znode);
2561 goto out_unlock;
2562 }
2563 }
2564
2565
2566 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
2567 key = &znode->zbranch[i].key;
2568 if (!key_in_range(c, key, from_key, to_key))
2569 break;
2570 lnc_free(&znode->zbranch[i]);
2571 err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2572 znode->zbranch[i].len);
2573 if (err) {
2574 dbg_dump_znode(c, znode);
2575 goto out_unlock;
2576 }
2577 dbg_tnc("removing %s", DBGKEY(key));
2578 }
2579 if (k) {
2580 for (i = n + 1 + k; i < znode->child_cnt; i++)
2581 znode->zbranch[i - k] = znode->zbranch[i];
2582 znode->child_cnt -= k;
2583 }
2584
2585
2586 err = tnc_delete(c, znode, n);
2587 if (err)
2588 goto out_unlock;
2589 }
2590
2591out_unlock:
2592 if (!err)
2593 err = dbg_check_tnc(c, 0);
2594 mutex_unlock(&c->tnc_mutex);
2595 return err;
2596}
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2608{
2609 union ubifs_key key1, key2;
2610 struct ubifs_dent_node *xent, *pxent = NULL;
2611 struct qstr nm = { .name = NULL };
2612
2613 dbg_tnc("ino %lu", (unsigned long)inum);
2614
2615
2616
2617
2618
2619 lowest_xent_key(c, &key1, inum);
2620 while (1) {
2621 ino_t xattr_inum;
2622 int err;
2623
2624 xent = ubifs_tnc_next_ent(c, &key1, &nm);
2625 if (IS_ERR(xent)) {
2626 err = PTR_ERR(xent);
2627 if (err == -ENOENT)
2628 break;
2629 return err;
2630 }
2631
2632 xattr_inum = le64_to_cpu(xent->inum);
2633 dbg_tnc("xent '%s', ino %lu", xent->name,
2634 (unsigned long)xattr_inum);
2635
2636 nm.name = (char *)xent->name;
2637 nm.len = le16_to_cpu(xent->nlen);
2638 err = ubifs_tnc_remove_nm(c, &key1, &nm);
2639 if (err) {
2640 kfree(xent);
2641 return err;
2642 }
2643
2644 lowest_ino_key(c, &key1, xattr_inum);
2645 highest_ino_key(c, &key2, xattr_inum);
2646 err = ubifs_tnc_remove_range(c, &key1, &key2);
2647 if (err) {
2648 kfree(xent);
2649 return err;
2650 }
2651
2652 kfree(pxent);
2653 pxent = xent;
2654 key_read(c, &xent->key, &key1);
2655 }
2656
2657 kfree(pxent);
2658 lowest_ino_key(c, &key1, inum);
2659 highest_ino_key(c, &key2, inum);
2660
2661 return ubifs_tnc_remove_range(c, &key1, &key2);
2662}
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2688 union ubifs_key *key,
2689 const struct qstr *nm)
2690{
2691 int n, err, type = key_type(c, key);
2692 struct ubifs_znode *znode;
2693 struct ubifs_dent_node *dent;
2694 struct ubifs_zbranch *zbr;
2695 union ubifs_key *dkey;
2696
2697 dbg_tnc("%s %s", nm->name ? (char *)nm->name : "(lowest)", DBGKEY(key));
2698 ubifs_assert(is_hash_key(c, key));
2699
2700 mutex_lock(&c->tnc_mutex);
2701 err = ubifs_lookup_level0(c, key, &znode, &n);
2702 if (unlikely(err < 0))
2703 goto out_unlock;
2704
2705 if (nm->name) {
2706 if (err) {
2707
2708 err = resolve_collision(c, key, &znode, &n, nm);
2709 dbg_tnc("rc returned %d, znode %p, n %d",
2710 err, znode, n);
2711 if (unlikely(err < 0))
2712 goto out_unlock;
2713 }
2714
2715
2716 err = tnc_next(c, &znode, &n);
2717 if (unlikely(err))
2718 goto out_unlock;
2719 } else {
2720
2721
2722
2723
2724
2725 if (!err) {
2726
2727
2728
2729
2730
2731 err = tnc_next(c, &znode, &n);
2732 if (err)
2733 goto out_unlock;
2734 }
2735 }
2736
2737 zbr = &znode->zbranch[n];
2738 dent = kmalloc(zbr->len, GFP_NOFS);
2739 if (unlikely(!dent)) {
2740 err = -ENOMEM;
2741 goto out_unlock;
2742 }
2743
2744
2745
2746
2747
2748 dkey = &zbr->key;
2749 if (key_inum(c, dkey) != key_inum(c, key) ||
2750 key_type(c, dkey) != type) {
2751 err = -ENOENT;
2752 goto out_free;
2753 }
2754
2755 err = tnc_read_node_nm(c, zbr, dent);
2756 if (unlikely(err))
2757 goto out_free;
2758
2759 mutex_unlock(&c->tnc_mutex);
2760 return dent;
2761
2762out_free:
2763 kfree(dent);
2764out_unlock:
2765 mutex_unlock(&c->tnc_mutex);
2766 return ERR_PTR(err);
2767}
2768