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 <linux/crc32.h>
34#include <linux/slab.h>
35#include "ubifs.h"
36
37static int try_read_node(const struct ubifs_info *c, void *buf, int type,
38 int len, int lnum, int offs);
39static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
40 struct ubifs_zbranch *zbr, void *node);
41
42
43
44
45
46
47
48
49
50
51
52enum {
53 NAME_LESS = 0,
54 NAME_MATCHES = 1,
55 NAME_GREATER = 2,
56 NOT_ON_MEDIA = 3,
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
82static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
83{
84 struct ubifs_old_idx *old_idx, *o;
85 struct rb_node **p, *parent = NULL;
86
87 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
88 if (unlikely(!old_idx))
89 return -ENOMEM;
90 old_idx->lnum = lnum;
91 old_idx->offs = offs;
92
93 p = &c->old_idx.rb_node;
94 while (*p) {
95 parent = *p;
96 o = rb_entry(parent, struct ubifs_old_idx, rb);
97 if (lnum < o->lnum)
98 p = &(*p)->rb_left;
99 else if (lnum > o->lnum)
100 p = &(*p)->rb_right;
101 else if (offs < o->offs)
102 p = &(*p)->rb_left;
103 else if (offs > o->offs)
104 p = &(*p)->rb_right;
105 else {
106 ubifs_err(c, "old idx added twice!");
107 kfree(old_idx);
108 return 0;
109 }
110 }
111 rb_link_node(&old_idx->rb, parent, p);
112 rb_insert_color(&old_idx->rb, &c->old_idx);
113 return 0;
114}
115
116
117
118
119
120
121
122
123int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
124{
125 if (znode->parent) {
126 struct ubifs_zbranch *zbr;
127
128 zbr = &znode->parent->zbranch[znode->iip];
129 if (zbr->len)
130 return insert_old_idx(c, zbr->lnum, zbr->offs);
131 } else
132 if (c->zroot.len)
133 return insert_old_idx(c, c->zroot.lnum,
134 c->zroot.offs);
135 return 0;
136}
137
138
139
140
141
142
143
144
145static int ins_clr_old_idx_znode(struct ubifs_info *c,
146 struct ubifs_znode *znode)
147{
148 int err;
149
150 if (znode->parent) {
151 struct ubifs_zbranch *zbr;
152
153 zbr = &znode->parent->zbranch[znode->iip];
154 if (zbr->len) {
155 err = insert_old_idx(c, zbr->lnum, zbr->offs);
156 if (err)
157 return err;
158 zbr->lnum = 0;
159 zbr->offs = 0;
160 zbr->len = 0;
161 }
162 } else
163 if (c->zroot.len) {
164 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
165 if (err)
166 return err;
167 c->zroot.lnum = 0;
168 c->zroot.offs = 0;
169 c->zroot.len = 0;
170 }
171 return 0;
172}
173
174
175
176
177
178
179
180
181
182
183
184void destroy_old_idx(struct ubifs_info *c)
185{
186 struct ubifs_old_idx *old_idx, *n;
187
188 rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb)
189 kfree(old_idx);
190
191 c->old_idx = RB_ROOT;
192}
193
194
195
196
197
198
199
200
201static struct ubifs_znode *copy_znode(struct ubifs_info *c,
202 struct ubifs_znode *znode)
203{
204 struct ubifs_znode *zn;
205
206 zn = kmemdup(znode, c->max_znode_sz, GFP_NOFS);
207 if (unlikely(!zn))
208 return ERR_PTR(-ENOMEM);
209
210 zn->cnext = NULL;
211 __set_bit(DIRTY_ZNODE, &zn->flags);
212 __clear_bit(COW_ZNODE, &zn->flags);
213
214 ubifs_assert(!ubifs_zn_obsolete(znode));
215 __set_bit(OBSOLETE_ZNODE, &znode->flags);
216
217 if (znode->level != 0) {
218 int i;
219 const int n = zn->child_cnt;
220
221
222 for (i = 0; i < n; i++) {
223 struct ubifs_zbranch *zbr = &zn->zbranch[i];
224
225 if (zbr->znode)
226 zbr->znode->parent = zn;
227 }
228 }
229
230 atomic_long_inc(&c->dirty_zn_cnt);
231 return zn;
232}
233
234
235
236
237
238
239
240
241
242static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
243{
244 c->calc_idx_sz -= ALIGN(dirt, 8);
245 return ubifs_add_dirt(c, lnum, dirt);
246}
247
248
249
250
251
252
253
254
255static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
256 struct ubifs_zbranch *zbr)
257{
258 struct ubifs_znode *znode = zbr->znode;
259 struct ubifs_znode *zn;
260 int err;
261
262 if (!ubifs_zn_cow(znode)) {
263
264 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
265 atomic_long_inc(&c->dirty_zn_cnt);
266 atomic_long_dec(&c->clean_zn_cnt);
267 atomic_long_dec(&ubifs_clean_zn_cnt);
268 err = add_idx_dirt(c, zbr->lnum, zbr->len);
269 if (unlikely(err))
270 return ERR_PTR(err);
271 }
272 return znode;
273 }
274
275 zn = copy_znode(c, znode);
276 if (IS_ERR(zn))
277 return zn;
278
279 if (zbr->len) {
280 err = insert_old_idx(c, zbr->lnum, zbr->offs);
281 if (unlikely(err))
282 return ERR_PTR(err);
283 err = add_idx_dirt(c, zbr->lnum, zbr->len);
284 } else
285 err = 0;
286
287 zbr->znode = zn;
288 zbr->lnum = 0;
289 zbr->offs = 0;
290 zbr->len = 0;
291
292 if (unlikely(err))
293 return ERR_PTR(err);
294 return zn;
295}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
318 const void *node)
319{
320 int err;
321 void *lnc_node;
322 const struct ubifs_dent_node *dent = node;
323
324 ubifs_assert(!zbr->leaf);
325 ubifs_assert(zbr->len != 0);
326 ubifs_assert(is_hash_key(c, &zbr->key));
327
328 err = ubifs_validate_entry(c, dent);
329 if (err) {
330 dump_stack();
331 ubifs_dump_node(c, dent);
332 return err;
333 }
334
335 lnc_node = kmemdup(node, zbr->len, GFP_NOFS);
336 if (!lnc_node)
337
338 return 0;
339
340 zbr->leaf = lnc_node;
341 return 0;
342}
343
344
345
346
347
348
349
350
351
352
353static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
354 void *node)
355{
356 int err;
357
358 ubifs_assert(!zbr->leaf);
359 ubifs_assert(zbr->len != 0);
360
361 err = ubifs_validate_entry(c, node);
362 if (err) {
363 dump_stack();
364 ubifs_dump_node(c, node);
365 return err;
366 }
367
368 zbr->leaf = node;
369 return 0;
370}
371
372
373
374
375
376
377static void lnc_free(struct ubifs_zbranch *zbr)
378{
379 if (!zbr->leaf)
380 return;
381 kfree(zbr->leaf);
382 zbr->leaf = NULL;
383}
384
385
386
387
388
389
390
391
392
393
394
395
396static int tnc_read_hashed_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
397 void *node)
398{
399 int err;
400
401 ubifs_assert(is_hash_key(c, &zbr->key));
402
403 if (zbr->leaf) {
404
405 ubifs_assert(zbr->len != 0);
406 memcpy(node, zbr->leaf, zbr->len);
407 return 0;
408 }
409
410 if (c->replaying) {
411 err = fallible_read_node(c, &zbr->key, zbr, node);
412
413
414
415
416 if (err == 0)
417 err = -ENOENT;
418 else if (err == 1)
419 err = 0;
420 } else {
421 err = ubifs_tnc_read_node(c, zbr, node);
422 }
423 if (err)
424 return err;
425
426
427 err = lnc_add(c, zbr, node);
428 return err;
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
455static int try_read_node(const struct ubifs_info *c, void *buf, int type,
456 int len, int lnum, int offs)
457{
458 int err, node_len;
459 struct ubifs_ch *ch = buf;
460 uint32_t crc, node_crc;
461
462 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
463
464 err = ubifs_leb_read(c, lnum, buf, offs, len, 1);
465 if (err) {
466 ubifs_err(c, "cannot read node type %d from LEB %d:%d, error %d",
467 type, lnum, offs, err);
468 return err;
469 }
470
471 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
472 return 0;
473
474 if (ch->node_type != type)
475 return 0;
476
477 node_len = le32_to_cpu(ch->len);
478 if (node_len != len)
479 return 0;
480
481 if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting &&
482 !c->remounting_rw)
483 return 1;
484
485 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
486 node_crc = le32_to_cpu(ch->crc);
487 if (crc != node_crc)
488 return 0;
489
490 return 1;
491}
492
493
494
495
496
497
498
499
500
501
502
503static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
504 struct ubifs_zbranch *zbr, void *node)
505{
506 int ret;
507
508 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
509
510 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
511 zbr->offs);
512 if (ret == 1) {
513 union ubifs_key node_key;
514 struct ubifs_dent_node *dent = node;
515
516
517 key_read(c, &dent->key, &node_key);
518 if (keys_cmp(c, key, &node_key) != 0)
519 ret = 0;
520 }
521 if (ret == 0 && c->replaying)
522 dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ",
523 zbr->lnum, zbr->offs, zbr->len);
524 return ret;
525}
526
527
528
529
530
531
532
533
534
535
536
537
538static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
539 const struct fscrypt_name *nm)
540{
541 struct ubifs_dent_node *dent;
542 int nlen, err;
543
544
545 if (!zbr->leaf) {
546 dent = kmalloc(zbr->len, GFP_NOFS);
547 if (!dent)
548 return -ENOMEM;
549
550 err = ubifs_tnc_read_node(c, zbr, dent);
551 if (err)
552 goto out_free;
553
554
555 err = lnc_add_directly(c, zbr, dent);
556 if (err)
557 goto out_free;
558 } else
559 dent = zbr->leaf;
560
561 nlen = le16_to_cpu(dent->nlen);
562 err = memcmp(dent->name, fname_name(nm), min_t(int, nlen, fname_len(nm)));
563 if (err == 0) {
564 if (nlen == fname_len(nm))
565 return NAME_MATCHES;
566 else if (nlen < fname_len(nm))
567 return NAME_LESS;
568 else
569 return NAME_GREATER;
570 } else if (err < 0)
571 return NAME_LESS;
572 else
573 return NAME_GREATER;
574
575out_free:
576 kfree(dent);
577 return err;
578}
579
580
581
582
583
584
585
586
587
588static struct ubifs_znode *get_znode(struct ubifs_info *c,
589 struct ubifs_znode *znode, int n)
590{
591 struct ubifs_zbranch *zbr;
592
593 zbr = &znode->zbranch[n];
594 if (zbr->znode)
595 znode = zbr->znode;
596 else
597 znode = ubifs_load_znode(c, zbr, znode, n);
598 return znode;
599}
600
601
602
603
604
605
606
607
608
609
610static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
611{
612 struct ubifs_znode *znode = *zn;
613 int nn = *n;
614
615 nn += 1;
616 if (nn < znode->child_cnt) {
617 *n = nn;
618 return 0;
619 }
620 while (1) {
621 struct ubifs_znode *zp;
622
623 zp = znode->parent;
624 if (!zp)
625 return -ENOENT;
626 nn = znode->iip + 1;
627 znode = zp;
628 if (nn < znode->child_cnt) {
629 znode = get_znode(c, znode, nn);
630 if (IS_ERR(znode))
631 return PTR_ERR(znode);
632 while (znode->level != 0) {
633 znode = get_znode(c, znode, 0);
634 if (IS_ERR(znode))
635 return PTR_ERR(znode);
636 }
637 nn = 0;
638 break;
639 }
640 }
641 *zn = znode;
642 *n = nn;
643 return 0;
644}
645
646
647
648
649
650
651
652
653
654
655static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
656{
657 struct ubifs_znode *znode = *zn;
658 int nn = *n;
659
660 if (nn > 0) {
661 *n = nn - 1;
662 return 0;
663 }
664 while (1) {
665 struct ubifs_znode *zp;
666
667 zp = znode->parent;
668 if (!zp)
669 return -ENOENT;
670 nn = znode->iip - 1;
671 znode = zp;
672 if (nn >= 0) {
673 znode = get_znode(c, znode, nn);
674 if (IS_ERR(znode))
675 return PTR_ERR(znode);
676 while (znode->level != 0) {
677 nn = znode->child_cnt - 1;
678 znode = get_znode(c, znode, nn);
679 if (IS_ERR(znode))
680 return PTR_ERR(znode);
681 }
682 nn = znode->child_cnt - 1;
683 break;
684 }
685 }
686 *zn = znode;
687 *n = nn;
688 return 0;
689}
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
708 struct ubifs_znode **zn, int *n,
709 const struct fscrypt_name *nm)
710{
711 int err;
712
713 err = matches_name(c, &(*zn)->zbranch[*n], nm);
714 if (unlikely(err < 0))
715 return err;
716 if (err == NAME_MATCHES)
717 return 1;
718
719 if (err == NAME_GREATER) {
720
721 while (1) {
722 err = tnc_prev(c, zn, n);
723 if (err == -ENOENT) {
724 ubifs_assert(*n == 0);
725 *n = -1;
726 return 0;
727 }
728 if (err < 0)
729 return err;
730 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
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 if (*n == (*zn)->child_cnt - 1) {
761 err = tnc_next(c, zn, n);
762 if (err) {
763
764 ubifs_assert(0);
765 if (err == -ENOENT)
766 err = -EINVAL;
767 return err;
768 }
769 ubifs_assert(*n == 0);
770 *n = -1;
771 }
772 return 0;
773 }
774 err = matches_name(c, &(*zn)->zbranch[*n], nm);
775 if (err < 0)
776 return err;
777 if (err == NAME_LESS)
778 return 0;
779 if (err == NAME_MATCHES)
780 return 1;
781 ubifs_assert(err == NAME_GREATER);
782 }
783 } else {
784 int nn = *n;
785 struct ubifs_znode *znode = *zn;
786
787
788 while (1) {
789 err = tnc_next(c, &znode, &nn);
790 if (err == -ENOENT)
791 return 0;
792 if (err < 0)
793 return err;
794 if (keys_cmp(c, &znode->zbranch[nn].key, key))
795 return 0;
796 err = matches_name(c, &znode->zbranch[nn], nm);
797 if (err < 0)
798 return err;
799 if (err == NAME_GREATER)
800 return 0;
801 *zn = znode;
802 *n = nn;
803 if (err == NAME_MATCHES)
804 return 1;
805 ubifs_assert(err == NAME_LESS);
806 }
807 }
808}
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825static int fallible_matches_name(struct ubifs_info *c,
826 struct ubifs_zbranch *zbr,
827 const struct fscrypt_name *nm)
828{
829 struct ubifs_dent_node *dent;
830 int nlen, err;
831
832
833 if (!zbr->leaf) {
834 dent = kmalloc(zbr->len, GFP_NOFS);
835 if (!dent)
836 return -ENOMEM;
837
838 err = fallible_read_node(c, &zbr->key, zbr, dent);
839 if (err < 0)
840 goto out_free;
841 if (err == 0) {
842
843 err = NOT_ON_MEDIA;
844 goto out_free;
845 }
846 ubifs_assert(err == 1);
847
848 err = lnc_add_directly(c, zbr, dent);
849 if (err)
850 goto out_free;
851 } else
852 dent = zbr->leaf;
853
854 nlen = le16_to_cpu(dent->nlen);
855 err = memcmp(dent->name, fname_name(nm), min_t(int, nlen, fname_len(nm)));
856 if (err == 0) {
857 if (nlen == fname_len(nm))
858 return NAME_MATCHES;
859 else if (nlen < fname_len(nm))
860 return NAME_LESS;
861 else
862 return NAME_GREATER;
863 } else if (err < 0)
864 return NAME_LESS;
865 else
866 return NAME_GREATER;
867
868out_free:
869 kfree(dent);
870 return err;
871}
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895static int fallible_resolve_collision(struct ubifs_info *c,
896 const union ubifs_key *key,
897 struct ubifs_znode **zn, int *n,
898 const struct fscrypt_name *nm,
899 int adding)
900{
901 struct ubifs_znode *o_znode = NULL, *znode = *zn;
902 int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
903
904 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
905 if (unlikely(cmp < 0))
906 return cmp;
907 if (cmp == NAME_MATCHES)
908 return 1;
909 if (cmp == NOT_ON_MEDIA) {
910 o_znode = znode;
911 o_n = nn;
912
913
914
915
916
917 unsure = 1;
918 } else if (!adding)
919 unsure = 1;
920
921 if (cmp == NAME_GREATER || unsure) {
922
923 while (1) {
924 err = tnc_prev(c, zn, n);
925 if (err == -ENOENT) {
926 ubifs_assert(*n == 0);
927 *n = -1;
928 break;
929 }
930 if (err < 0)
931 return err;
932 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
933
934 if (*n == (*zn)->child_cnt - 1) {
935 err = tnc_next(c, zn, n);
936 if (err) {
937
938 ubifs_assert(0);
939 if (err == -ENOENT)
940 err = -EINVAL;
941 return err;
942 }
943 ubifs_assert(*n == 0);
944 *n = -1;
945 }
946 break;
947 }
948 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
949 if (err < 0)
950 return err;
951 if (err == NAME_MATCHES)
952 return 1;
953 if (err == NOT_ON_MEDIA) {
954 o_znode = *zn;
955 o_n = *n;
956 continue;
957 }
958 if (!adding)
959 continue;
960 if (err == NAME_LESS)
961 break;
962 else
963 unsure = 0;
964 }
965 }
966
967 if (cmp == NAME_LESS || unsure) {
968
969 *zn = znode;
970 *n = nn;
971 while (1) {
972 err = tnc_next(c, &znode, &nn);
973 if (err == -ENOENT)
974 break;
975 if (err < 0)
976 return err;
977 if (keys_cmp(c, &znode->zbranch[nn].key, key))
978 break;
979 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
980 if (err < 0)
981 return err;
982 if (err == NAME_GREATER)
983 break;
984 *zn = znode;
985 *n = nn;
986 if (err == NAME_MATCHES)
987 return 1;
988 if (err == NOT_ON_MEDIA) {
989 o_znode = znode;
990 o_n = nn;
991 }
992 }
993 }
994
995
996 if (adding || !o_znode)
997 return 0;
998
999 dbg_mntk(key, "dangling match LEB %d:%d len %d key ",
1000 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
1001 o_znode->zbranch[o_n].len);
1002 *zn = o_znode;
1003 *n = o_n;
1004 return 1;
1005}
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
1016{
1017 if (zbr->lnum == lnum && zbr->offs == offs)
1018 return 1;
1019 else
1020 return 0;
1021}
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040static int resolve_collision_directly(struct ubifs_info *c,
1041 const union ubifs_key *key,
1042 struct ubifs_znode **zn, int *n,
1043 int lnum, int offs)
1044{
1045 struct ubifs_znode *znode;
1046 int nn, err;
1047
1048 znode = *zn;
1049 nn = *n;
1050 if (matches_position(&znode->zbranch[nn], lnum, offs))
1051 return 1;
1052
1053
1054 while (1) {
1055 err = tnc_prev(c, &znode, &nn);
1056 if (err == -ENOENT)
1057 break;
1058 if (err < 0)
1059 return err;
1060 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1061 break;
1062 if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1063 *zn = znode;
1064 *n = nn;
1065 return 1;
1066 }
1067 }
1068
1069
1070 znode = *zn;
1071 nn = *n;
1072 while (1) {
1073 err = tnc_next(c, &znode, &nn);
1074 if (err == -ENOENT)
1075 return 0;
1076 if (err < 0)
1077 return err;
1078 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1079 return 0;
1080 *zn = znode;
1081 *n = nn;
1082 if (matches_position(&znode->zbranch[nn], lnum, offs))
1083 return 1;
1084 }
1085}
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1098 struct ubifs_znode *znode)
1099{
1100 struct ubifs_znode *zp;
1101 int *path = c->bottom_up_buf, p = 0;
1102
1103 ubifs_assert(c->zroot.znode);
1104 ubifs_assert(znode);
1105 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
1106 kfree(c->bottom_up_buf);
1107 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
1108 GFP_NOFS);
1109 if (!c->bottom_up_buf)
1110 return ERR_PTR(-ENOMEM);
1111 path = c->bottom_up_buf;
1112 }
1113 if (c->zroot.znode->level) {
1114
1115 while (1) {
1116 int n;
1117
1118 zp = znode->parent;
1119 if (!zp)
1120 break;
1121 n = znode->iip;
1122 ubifs_assert(p < c->zroot.znode->level);
1123 path[p++] = n;
1124 if (!zp->cnext && ubifs_zn_dirty(znode))
1125 break;
1126 znode = zp;
1127 }
1128 }
1129
1130
1131 while (1) {
1132 struct ubifs_zbranch *zbr;
1133
1134 zp = znode->parent;
1135 if (zp) {
1136 ubifs_assert(path[p - 1] >= 0);
1137 ubifs_assert(path[p - 1] < zp->child_cnt);
1138 zbr = &zp->zbranch[path[--p]];
1139 znode = dirty_cow_znode(c, zbr);
1140 } else {
1141 ubifs_assert(znode == c->zroot.znode);
1142 znode = dirty_cow_znode(c, &c->zroot);
1143 }
1144 if (IS_ERR(znode) || !p)
1145 break;
1146 ubifs_assert(path[p - 1] >= 0);
1147 ubifs_assert(path[p - 1] < znode->child_cnt);
1148 znode = znode->zbranch[path[p - 1]].znode;
1149 }
1150
1151 return znode;
1152}
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1177 struct ubifs_znode **zn, int *n)
1178{
1179 int err, exact;
1180 struct ubifs_znode *znode;
1181 unsigned long time = get_seconds();
1182
1183 dbg_tnck(key, "search key ");
1184 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY);
1185
1186 znode = c->zroot.znode;
1187 if (unlikely(!znode)) {
1188 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1189 if (IS_ERR(znode))
1190 return PTR_ERR(znode);
1191 }
1192
1193 znode->time = time;
1194
1195 while (1) {
1196 struct ubifs_zbranch *zbr;
1197
1198 exact = ubifs_search_zbranch(c, znode, key, n);
1199
1200 if (znode->level == 0)
1201 break;
1202
1203 if (*n < 0)
1204 *n = 0;
1205 zbr = &znode->zbranch[*n];
1206
1207 if (zbr->znode) {
1208 znode->time = time;
1209 znode = zbr->znode;
1210 continue;
1211 }
1212
1213
1214 znode = ubifs_load_znode(c, zbr, znode, *n);
1215 if (IS_ERR(znode))
1216 return PTR_ERR(znode);
1217 }
1218
1219 *zn = znode;
1220 if (exact || !is_hash_key(c, key) || *n != -1) {
1221 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1222 return exact;
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
1262
1263
1264
1265
1266
1267
1268 err = tnc_prev(c, &znode, n);
1269 if (err == -ENOENT) {
1270 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1271 *n = -1;
1272 return 0;
1273 }
1274 if (unlikely(err < 0))
1275 return err;
1276 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1277 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1278 *n = -1;
1279 return 0;
1280 }
1281
1282 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1283 *zn = znode;
1284 return 1;
1285}
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1313 struct ubifs_znode **zn, int *n)
1314{
1315 int err, exact;
1316 struct ubifs_znode *znode;
1317 unsigned long time = get_seconds();
1318
1319 dbg_tnck(key, "search and dirty key ");
1320
1321 znode = c->zroot.znode;
1322 if (unlikely(!znode)) {
1323 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1324 if (IS_ERR(znode))
1325 return PTR_ERR(znode);
1326 }
1327
1328 znode = dirty_cow_znode(c, &c->zroot);
1329 if (IS_ERR(znode))
1330 return PTR_ERR(znode);
1331
1332 znode->time = time;
1333
1334 while (1) {
1335 struct ubifs_zbranch *zbr;
1336
1337 exact = ubifs_search_zbranch(c, znode, key, n);
1338
1339 if (znode->level == 0)
1340 break;
1341
1342 if (*n < 0)
1343 *n = 0;
1344 zbr = &znode->zbranch[*n];
1345
1346 if (zbr->znode) {
1347 znode->time = time;
1348 znode = dirty_cow_znode(c, zbr);
1349 if (IS_ERR(znode))
1350 return PTR_ERR(znode);
1351 continue;
1352 }
1353
1354
1355 znode = ubifs_load_znode(c, zbr, znode, *n);
1356 if (IS_ERR(znode))
1357 return PTR_ERR(znode);
1358 znode = dirty_cow_znode(c, zbr);
1359 if (IS_ERR(znode))
1360 return PTR_ERR(znode);
1361 }
1362
1363 *zn = znode;
1364 if (exact || !is_hash_key(c, key) || *n != -1) {
1365 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1366 return exact;
1367 }
1368
1369
1370
1371
1372
1373 err = tnc_prev(c, &znode, n);
1374 if (err == -ENOENT) {
1375 *n = -1;
1376 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1377 return 0;
1378 }
1379 if (unlikely(err < 0))
1380 return err;
1381 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1382 *n = -1;
1383 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1384 return 0;
1385 }
1386
1387 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1388 znode = dirty_cow_bottom_up(c, znode);
1389 if (IS_ERR(znode))
1390 return PTR_ERR(znode);
1391 }
1392
1393 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1394 *zn = znode;
1395 return 1;
1396}
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1409{
1410 int gc_seq2, gced_lnum;
1411
1412 gced_lnum = c->gced_lnum;
1413 smp_rmb();
1414 gc_seq2 = c->gc_seq;
1415
1416 if (gc_seq1 == gc_seq2)
1417 return 0;
1418
1419 if (gc_seq1 + 1 != gc_seq2)
1420 return 1;
1421
1422
1423
1424
1425 smp_rmb();
1426 if (gced_lnum != c->gced_lnum)
1427 return 1;
1428
1429 if (gced_lnum == lnum)
1430 return 1;
1431 return 0;
1432}
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1448 void *node, int *lnum, int *offs)
1449{
1450 int found, n, err, safely = 0, gc_seq1;
1451 struct ubifs_znode *znode;
1452 struct ubifs_zbranch zbr, *zt;
1453
1454again:
1455 mutex_lock(&c->tnc_mutex);
1456 found = ubifs_lookup_level0(c, key, &znode, &n);
1457 if (!found) {
1458 err = -ENOENT;
1459 goto out;
1460 } else if (found < 0) {
1461 err = found;
1462 goto out;
1463 }
1464 zt = &znode->zbranch[n];
1465 if (lnum) {
1466 *lnum = zt->lnum;
1467 *offs = zt->offs;
1468 }
1469 if (is_hash_key(c, key)) {
1470
1471
1472
1473
1474 err = tnc_read_hashed_node(c, zt, node);
1475 goto out;
1476 }
1477 if (safely) {
1478 err = ubifs_tnc_read_node(c, zt, node);
1479 goto out;
1480 }
1481
1482 zbr = znode->zbranch[n];
1483 gc_seq1 = c->gc_seq;
1484 mutex_unlock(&c->tnc_mutex);
1485
1486 if (ubifs_get_wbuf(c, zbr.lnum)) {
1487
1488 err = ubifs_tnc_read_node(c, &zbr, node);
1489 return err;
1490 }
1491
1492 err = fallible_read_node(c, key, &zbr, node);
1493 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1494
1495
1496
1497
1498 safely = 1;
1499 goto again;
1500 }
1501 return 0;
1502
1503out:
1504 mutex_unlock(&c->tnc_mutex);
1505 return err;
1506}
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1522{
1523 int n, err = 0, lnum = -1, uninitialized_var(offs);
1524 int uninitialized_var(len);
1525 unsigned int block = key_block(c, &bu->key);
1526 struct ubifs_znode *znode;
1527
1528 bu->cnt = 0;
1529 bu->blk_cnt = 0;
1530 bu->eof = 0;
1531
1532 mutex_lock(&c->tnc_mutex);
1533
1534 err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1535 if (err < 0)
1536 goto out;
1537 if (err) {
1538
1539 len = znode->zbranch[n].len;
1540
1541 if (len > bu->buf_len) {
1542 err = -EINVAL;
1543 goto out;
1544 }
1545
1546 bu->zbranch[bu->cnt++] = znode->zbranch[n];
1547 bu->blk_cnt += 1;
1548 lnum = znode->zbranch[n].lnum;
1549 offs = ALIGN(znode->zbranch[n].offs + len, 8);
1550 }
1551 while (1) {
1552 struct ubifs_zbranch *zbr;
1553 union ubifs_key *key;
1554 unsigned int next_block;
1555
1556
1557 err = tnc_next(c, &znode, &n);
1558 if (err)
1559 goto out;
1560 zbr = &znode->zbranch[n];
1561 key = &zbr->key;
1562
1563 if (key_inum(c, key) != key_inum(c, &bu->key) ||
1564 key_type(c, key) != UBIFS_DATA_KEY) {
1565 err = -ENOENT;
1566 goto out;
1567 }
1568 if (lnum < 0) {
1569
1570 lnum = zbr->lnum;
1571 offs = ALIGN(zbr->offs + zbr->len, 8);
1572 len = zbr->len;
1573 if (len > bu->buf_len) {
1574 err = -EINVAL;
1575 goto out;
1576 }
1577 } else {
1578
1579
1580
1581
1582 if (zbr->lnum != lnum || zbr->offs != offs)
1583 goto out;
1584 offs += ALIGN(zbr->len, 8);
1585 len = ALIGN(len, 8) + zbr->len;
1586
1587 if (len > bu->buf_len)
1588 goto out;
1589 }
1590
1591 next_block = key_block(c, key);
1592 bu->blk_cnt += (next_block - block - 1);
1593 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1594 goto out;
1595 block = next_block;
1596
1597 bu->zbranch[bu->cnt++] = *zbr;
1598 bu->blk_cnt += 1;
1599
1600 if (bu->cnt >= UBIFS_MAX_BULK_READ)
1601 goto out;
1602 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1603 goto out;
1604 }
1605out:
1606 if (err == -ENOENT) {
1607 bu->eof = 1;
1608 err = 0;
1609 }
1610 bu->gc_seq = c->gc_seq;
1611 mutex_unlock(&c->tnc_mutex);
1612 if (err)
1613 return err;
1614
1615
1616
1617
1618 if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1619 bu->blk_cnt = UBIFS_MAX_BULK_READ;
1620
1621
1622
1623
1624 if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1625 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1626 return 0;
1627 if (bu->eof) {
1628
1629 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
1630 return 0;
1631 }
1632
1633 block = key_block(c, &bu->key) + bu->blk_cnt;
1634 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
1635 while (bu->cnt) {
1636 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
1637 break;
1638 bu->cnt -= 1;
1639 }
1640 return 0;
1641}
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653static int read_wbuf(struct ubifs_wbuf *wbuf, void *buf, int len, int lnum,
1654 int offs)
1655{
1656 const struct ubifs_info *c = wbuf->c;
1657 int rlen, overlap;
1658
1659 dbg_io("LEB %d:%d, length %d", lnum, offs, len);
1660 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1661 ubifs_assert(!(offs & 7) && offs < c->leb_size);
1662 ubifs_assert(offs + len <= c->leb_size);
1663
1664 spin_lock(&wbuf->lock);
1665 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
1666 if (!overlap) {
1667
1668 spin_unlock(&wbuf->lock);
1669 return ubifs_leb_read(c, lnum, buf, offs, len, 0);
1670 }
1671
1672
1673 rlen = wbuf->offs - offs;
1674 if (rlen < 0)
1675 rlen = 0;
1676
1677
1678 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
1679 spin_unlock(&wbuf->lock);
1680
1681 if (rlen > 0)
1682
1683 return ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
1684
1685 return 0;
1686}
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696static int validate_data_node(struct ubifs_info *c, void *buf,
1697 struct ubifs_zbranch *zbr)
1698{
1699 union ubifs_key key1;
1700 struct ubifs_ch *ch = buf;
1701 int err, len;
1702
1703 if (ch->node_type != UBIFS_DATA_NODE) {
1704 ubifs_err(c, "bad node type (%d but expected %d)",
1705 ch->node_type, UBIFS_DATA_NODE);
1706 goto out_err;
1707 }
1708
1709 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1710 if (err) {
1711 ubifs_err(c, "expected node type %d", UBIFS_DATA_NODE);
1712 goto out;
1713 }
1714
1715 len = le32_to_cpu(ch->len);
1716 if (len != zbr->len) {
1717 ubifs_err(c, "bad node length %d, expected %d", len, zbr->len);
1718 goto out_err;
1719 }
1720
1721
1722 key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
1723 if (!keys_eq(c, &zbr->key, &key1)) {
1724 ubifs_err(c, "bad key in node at LEB %d:%d",
1725 zbr->lnum, zbr->offs);
1726 dbg_tnck(&zbr->key, "looked for key ");
1727 dbg_tnck(&key1, "found node's key ");
1728 goto out_err;
1729 }
1730
1731 return 0;
1732
1733out_err:
1734 err = -EINVAL;
1735out:
1736 ubifs_err(c, "bad node at LEB %d:%d", zbr->lnum, zbr->offs);
1737 ubifs_dump_node(c, buf);
1738 dump_stack();
1739 return err;
1740}
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1753{
1754 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
1755 struct ubifs_wbuf *wbuf;
1756 void *buf;
1757
1758 len = bu->zbranch[bu->cnt - 1].offs;
1759 len += bu->zbranch[bu->cnt - 1].len - offs;
1760 if (len > bu->buf_len) {
1761 ubifs_err(c, "buffer too small %d vs %d", bu->buf_len, len);
1762 return -EINVAL;
1763 }
1764
1765
1766 wbuf = ubifs_get_wbuf(c, lnum);
1767 if (wbuf)
1768 err = read_wbuf(wbuf, bu->buf, len, lnum, offs);
1769 else
1770 err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0);
1771
1772
1773 if (maybe_leb_gced(c, lnum, bu->gc_seq))
1774 return -EAGAIN;
1775
1776 if (err && err != -EBADMSG) {
1777 ubifs_err(c, "failed to read from LEB %d:%d, error %d",
1778 lnum, offs, err);
1779 dump_stack();
1780 dbg_tnck(&bu->key, "key ");
1781 return err;
1782 }
1783
1784
1785 buf = bu->buf;
1786 for (i = 0; i < bu->cnt; i++) {
1787 err = validate_data_node(c, buf, &bu->zbranch[i]);
1788 if (err)
1789 return err;
1790 buf = buf + ALIGN(bu->zbranch[i].len, 8);
1791 }
1792
1793 return 0;
1794}
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1810 void *node, const struct fscrypt_name *nm)
1811{
1812 int found, n, err;
1813 struct ubifs_znode *znode;
1814
1815 dbg_tnck(key, "key ");
1816 mutex_lock(&c->tnc_mutex);
1817 found = ubifs_lookup_level0(c, key, &znode, &n);
1818 if (!found) {
1819 err = -ENOENT;
1820 goto out_unlock;
1821 } else if (found < 0) {
1822 err = found;
1823 goto out_unlock;
1824 }
1825
1826 ubifs_assert(n >= 0);
1827
1828 err = resolve_collision(c, key, &znode, &n, nm);
1829 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
1830 if (unlikely(err < 0))
1831 goto out_unlock;
1832 if (err == 0) {
1833 err = -ENOENT;
1834 goto out_unlock;
1835 }
1836
1837 err = tnc_read_hashed_node(c, &znode->zbranch[n], node);
1838
1839out_unlock:
1840 mutex_unlock(&c->tnc_mutex);
1841 return err;
1842}
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1858 void *node, const struct fscrypt_name *nm)
1859{
1860 int err, len;
1861 const struct ubifs_dent_node *dent = node;
1862
1863
1864
1865
1866
1867 err = ubifs_tnc_lookup(c, key, node);
1868 if (err)
1869 return err;
1870
1871 len = le16_to_cpu(dent->nlen);
1872 if (fname_len(nm) == len && !memcmp(dent->name, fname_name(nm), len))
1873 return 0;
1874
1875
1876
1877
1878
1879
1880 return do_lookup_nm(c, key, node, nm);
1881}
1882
1883static int search_dh_cookie(struct ubifs_info *c, const union ubifs_key *key,
1884 struct ubifs_dent_node *dent, uint32_t cookie,
1885 struct ubifs_znode **zn, int *n)
1886{
1887 int err;
1888 struct ubifs_znode *znode = *zn;
1889 struct ubifs_zbranch *zbr;
1890 union ubifs_key *dkey;
1891
1892 for (;;) {
1893 if (!err) {
1894 err = tnc_next(c, &znode, n);
1895 if (err)
1896 goto out;
1897 }
1898
1899 zbr = &znode->zbranch[*n];
1900 dkey = &zbr->key;
1901
1902 if (key_inum(c, dkey) != key_inum(c, key) ||
1903 key_type(c, dkey) != key_type(c, key)) {
1904 err = -ENOENT;
1905 goto out;
1906 }
1907
1908 err = tnc_read_hashed_node(c, zbr, dent);
1909 if (err)
1910 goto out;
1911
1912 if (key_hash(c, key) == key_hash(c, dkey) &&
1913 le32_to_cpu(dent->cookie) == cookie) {
1914 *zn = znode;
1915 goto out;
1916 }
1917 }
1918
1919out:
1920
1921 return err;
1922}
1923
1924static int do_lookup_dh(struct ubifs_info *c, const union ubifs_key *key,
1925 struct ubifs_dent_node *dent, uint32_t cookie)
1926{
1927 int n, err;
1928 struct ubifs_znode *znode;
1929 union ubifs_key start_key;
1930
1931 ubifs_assert(is_hash_key(c, key));
1932
1933 lowest_dent_key(c, &start_key, key_inum(c, key));
1934
1935 mutex_lock(&c->tnc_mutex);
1936 err = ubifs_lookup_level0(c, &start_key, &znode, &n);
1937 if (unlikely(err < 0))
1938 goto out_unlock;
1939
1940 err = search_dh_cookie(c, key, dent, cookie, &znode, &n);
1941
1942out_unlock:
1943 mutex_unlock(&c->tnc_mutex);
1944 return err;
1945}
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961int ubifs_tnc_lookup_dh(struct ubifs_info *c, const union ubifs_key *key,
1962 void *node, uint32_t cookie)
1963{
1964 int err;
1965 const struct ubifs_dent_node *dent = node;
1966
1967 if (!c->double_hash)
1968 return -EOPNOTSUPP;
1969
1970
1971
1972
1973
1974 err = ubifs_tnc_lookup(c, key, node);
1975 if (err)
1976 return err;
1977
1978 if (le32_to_cpu(dent->cookie) == cookie)
1979 return 0;
1980
1981
1982
1983
1984
1985 return do_lookup_dh(c, key, node, cookie);
1986}
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997static void correct_parent_keys(const struct ubifs_info *c,
1998 struct ubifs_znode *znode)
1999{
2000 union ubifs_key *key, *key1;
2001
2002 ubifs_assert(znode->parent);
2003 ubifs_assert(znode->iip == 0);
2004
2005 key = &znode->zbranch[0].key;
2006 key1 = &znode->parent->zbranch[0].key;
2007
2008 while (keys_cmp(c, key, key1) < 0) {
2009 key_copy(c, key, key1);
2010 znode = znode->parent;
2011 znode->alt = 1;
2012 if (!znode->parent || znode->iip)
2013 break;
2014 key1 = &znode->parent->zbranch[0].key;
2015 }
2016}
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029static void insert_zbranch(struct ubifs_znode *znode,
2030 const struct ubifs_zbranch *zbr, int n)
2031{
2032 int i;
2033
2034 ubifs_assert(ubifs_zn_dirty(znode));
2035
2036 if (znode->level) {
2037 for (i = znode->child_cnt; i > n; i--) {
2038 znode->zbranch[i] = znode->zbranch[i - 1];
2039 if (znode->zbranch[i].znode)
2040 znode->zbranch[i].znode->iip = i;
2041 }
2042 if (zbr->znode)
2043 zbr->znode->iip = n;
2044 } else
2045 for (i = znode->child_cnt; i > n; i--)
2046 znode->zbranch[i] = znode->zbranch[i - 1];
2047
2048 znode->zbranch[n] = *zbr;
2049 znode->child_cnt += 1;
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065 if (n == 0)
2066 znode->alt = 1;
2067}
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
2082 struct ubifs_zbranch *zbr, int n)
2083{
2084 struct ubifs_znode *zn, *zi, *zp;
2085 int i, keep, move, appending = 0;
2086 union ubifs_key *key = &zbr->key, *key1;
2087
2088 ubifs_assert(n >= 0 && n <= c->fanout);
2089
2090
2091again:
2092 zp = znode->parent;
2093 if (znode->child_cnt < c->fanout) {
2094 ubifs_assert(n != c->fanout);
2095 dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level);
2096
2097 insert_zbranch(znode, zbr, n);
2098
2099
2100 if (n == 0 && zp && znode->iip == 0)
2101 correct_parent_keys(c, znode);
2102
2103 return 0;
2104 }
2105
2106
2107
2108
2109
2110 dbg_tnck(key, "splitting level %d, key ", znode->level);
2111
2112 if (znode->alt)
2113
2114
2115
2116
2117 ins_clr_old_idx_znode(c, znode);
2118
2119 zn = kzalloc(c->max_znode_sz, GFP_NOFS);
2120 if (!zn)
2121 return -ENOMEM;
2122 zn->parent = zp;
2123 zn->level = znode->level;
2124
2125
2126 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
2127
2128 if (n == c->fanout) {
2129 key1 = &znode->zbranch[n - 1].key;
2130 if (key_inum(c, key1) == key_inum(c, key) &&
2131 key_type(c, key1) == UBIFS_DATA_KEY)
2132 appending = 1;
2133 } else
2134 goto check_split;
2135 } else if (appending && n != c->fanout) {
2136
2137 appending = 0;
2138check_split:
2139 if (n >= (c->fanout + 1) / 2) {
2140 key1 = &znode->zbranch[0].key;
2141 if (key_inum(c, key1) == key_inum(c, key) &&
2142 key_type(c, key1) == UBIFS_DATA_KEY) {
2143 key1 = &znode->zbranch[n].key;
2144 if (key_inum(c, key1) != key_inum(c, key) ||
2145 key_type(c, key1) != UBIFS_DATA_KEY) {
2146 keep = n;
2147 move = c->fanout - keep;
2148 zi = znode;
2149 goto do_split;
2150 }
2151 }
2152 }
2153 }
2154
2155 if (appending) {
2156 keep = c->fanout;
2157 move = 0;
2158 } else {
2159 keep = (c->fanout + 1) / 2;
2160 move = c->fanout - keep;
2161 }
2162
2163
2164
2165
2166
2167
2168 if (n < keep) {
2169
2170 zi = znode;
2171 move += 1;
2172 keep -= 1;
2173 } else {
2174
2175 zi = zn;
2176 n -= keep;
2177
2178 if (zn->level != 0)
2179 zbr->znode->parent = zn;
2180 }
2181
2182do_split:
2183
2184 __set_bit(DIRTY_ZNODE, &zn->flags);
2185 atomic_long_inc(&c->dirty_zn_cnt);
2186
2187 zn->child_cnt = move;
2188 znode->child_cnt = keep;
2189
2190 dbg_tnc("moving %d, keeping %d", move, keep);
2191
2192
2193 for (i = 0; i < move; i++) {
2194 zn->zbranch[i] = znode->zbranch[keep + i];
2195
2196 if (zn->level != 0)
2197 if (zn->zbranch[i].znode) {
2198 zn->zbranch[i].znode->parent = zn;
2199 zn->zbranch[i].znode->iip = i;
2200 }
2201 }
2202
2203
2204 dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level);
2205
2206 insert_zbranch(zi, zbr, n);
2207
2208
2209 if (zp) {
2210 if (n == 0 && zi == znode && znode->iip == 0)
2211 correct_parent_keys(c, znode);
2212
2213
2214 n = znode->iip + 1;
2215
2216
2217 zbr->key = zn->zbranch[0].key;
2218 zbr->znode = zn;
2219 zbr->lnum = 0;
2220 zbr->offs = 0;
2221 zbr->len = 0;
2222 znode = zp;
2223
2224 goto again;
2225 }
2226
2227
2228 dbg_tnc("creating new zroot at level %d", znode->level + 1);
2229
2230 zi = kzalloc(c->max_znode_sz, GFP_NOFS);
2231 if (!zi)
2232 return -ENOMEM;
2233
2234 zi->child_cnt = 2;
2235 zi->level = znode->level + 1;
2236
2237 __set_bit(DIRTY_ZNODE, &zi->flags);
2238 atomic_long_inc(&c->dirty_zn_cnt);
2239
2240 zi->zbranch[0].key = znode->zbranch[0].key;
2241 zi->zbranch[0].znode = znode;
2242 zi->zbranch[0].lnum = c->zroot.lnum;
2243 zi->zbranch[0].offs = c->zroot.offs;
2244 zi->zbranch[0].len = c->zroot.len;
2245 zi->zbranch[1].key = zn->zbranch[0].key;
2246 zi->zbranch[1].znode = zn;
2247
2248 c->zroot.lnum = 0;
2249 c->zroot.offs = 0;
2250 c->zroot.len = 0;
2251 c->zroot.znode = zi;
2252
2253 zn->parent = zi;
2254 zn->iip = 1;
2255 znode->parent = zi;
2256 znode->iip = 0;
2257
2258 return 0;
2259}
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2274 int offs, int len)
2275{
2276 int found, n, err = 0;
2277 struct ubifs_znode *znode;
2278
2279 mutex_lock(&c->tnc_mutex);
2280 dbg_tnck(key, "%d:%d, len %d, key ", lnum, offs, len);
2281 found = lookup_level0_dirty(c, key, &znode, &n);
2282 if (!found) {
2283 struct ubifs_zbranch zbr;
2284
2285 zbr.znode = NULL;
2286 zbr.lnum = lnum;
2287 zbr.offs = offs;
2288 zbr.len = len;
2289 key_copy(c, key, &zbr.key);
2290 err = tnc_insert(c, znode, &zbr, n + 1);
2291 } else if (found == 1) {
2292 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2293
2294 lnc_free(zbr);
2295 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2296 zbr->lnum = lnum;
2297 zbr->offs = offs;
2298 zbr->len = len;
2299 } else
2300 err = found;
2301 if (!err)
2302 err = dbg_check_tnc(c, 0);
2303 mutex_unlock(&c->tnc_mutex);
2304
2305 return err;
2306}
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
2323 int old_lnum, int old_offs, int lnum, int offs, int len)
2324{
2325 int found, n, err = 0;
2326 struct ubifs_znode *znode;
2327
2328 mutex_lock(&c->tnc_mutex);
2329 dbg_tnck(key, "old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum,
2330 old_offs, lnum, offs, len);
2331 found = lookup_level0_dirty(c, key, &znode, &n);
2332 if (found < 0) {
2333 err = found;
2334 goto out_unlock;
2335 }
2336
2337 if (found == 1) {
2338 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2339
2340 found = 0;
2341 if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
2342 lnc_free(zbr);
2343 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2344 if (err)
2345 goto out_unlock;
2346 zbr->lnum = lnum;
2347 zbr->offs = offs;
2348 zbr->len = len;
2349 found = 1;
2350 } else if (is_hash_key(c, key)) {
2351 found = resolve_collision_directly(c, key, &znode, &n,
2352 old_lnum, old_offs);
2353 dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
2354 found, znode, n, old_lnum, old_offs);
2355 if (found < 0) {
2356 err = found;
2357 goto out_unlock;
2358 }
2359
2360 if (found) {
2361
2362 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2363 znode = dirty_cow_bottom_up(c, znode);
2364 if (IS_ERR(znode)) {
2365 err = PTR_ERR(znode);
2366 goto out_unlock;
2367 }
2368 }
2369 zbr = &znode->zbranch[n];
2370 lnc_free(zbr);
2371 err = ubifs_add_dirt(c, zbr->lnum,
2372 zbr->len);
2373 if (err)
2374 goto out_unlock;
2375 zbr->lnum = lnum;
2376 zbr->offs = offs;
2377 zbr->len = len;
2378 }
2379 }
2380 }
2381
2382 if (!found)
2383 err = ubifs_add_dirt(c, lnum, len);
2384
2385 if (!err)
2386 err = dbg_check_tnc(c, 0);
2387
2388out_unlock:
2389 mutex_unlock(&c->tnc_mutex);
2390 return err;
2391}
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2406 int lnum, int offs, int len,
2407 const struct fscrypt_name *nm)
2408{
2409 int found, n, err = 0;
2410 struct ubifs_znode *znode;
2411
2412 mutex_lock(&c->tnc_mutex);
2413 dbg_tnck(key, "LEB %d:%d, key ", lnum, offs);
2414 found = lookup_level0_dirty(c, key, &znode, &n);
2415 if (found < 0) {
2416 err = found;
2417 goto out_unlock;
2418 }
2419
2420 if (found == 1) {
2421 if (c->replaying)
2422 found = fallible_resolve_collision(c, key, &znode, &n,
2423 nm, 1);
2424 else
2425 found = resolve_collision(c, key, &znode, &n, nm);
2426 dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
2427 if (found < 0) {
2428 err = found;
2429 goto out_unlock;
2430 }
2431
2432
2433 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2434 znode = dirty_cow_bottom_up(c, znode);
2435 if (IS_ERR(znode)) {
2436 err = PTR_ERR(znode);
2437 goto out_unlock;
2438 }
2439 }
2440
2441 if (found == 1) {
2442 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2443
2444 lnc_free(zbr);
2445 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2446 zbr->lnum = lnum;
2447 zbr->offs = offs;
2448 zbr->len = len;
2449 goto out_unlock;
2450 }
2451 }
2452
2453 if (!found) {
2454 struct ubifs_zbranch zbr;
2455
2456 zbr.znode = NULL;
2457 zbr.lnum = lnum;
2458 zbr.offs = offs;
2459 zbr.len = len;
2460 key_copy(c, key, &zbr.key);
2461 err = tnc_insert(c, znode, &zbr, n + 1);
2462 if (err)
2463 goto out_unlock;
2464 if (c->replaying) {
2465
2466
2467
2468
2469
2470
2471 struct fscrypt_name noname = { .disk_name = { .name = "", .len = 1 } };
2472
2473 err = dbg_check_tnc(c, 0);
2474 mutex_unlock(&c->tnc_mutex);
2475 if (err)
2476 return err;
2477 return ubifs_tnc_remove_nm(c, key, &noname);
2478 }
2479 }
2480
2481out_unlock:
2482 if (!err)
2483 err = dbg_check_tnc(c, 0);
2484 mutex_unlock(&c->tnc_mutex);
2485 return err;
2486}
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2498{
2499 struct ubifs_zbranch *zbr;
2500 struct ubifs_znode *zp;
2501 int i, err;
2502
2503
2504 ubifs_assert(znode->level == 0);
2505 ubifs_assert(n >= 0 && n < c->fanout);
2506 dbg_tnck(&znode->zbranch[n].key, "deleting key ");
2507
2508 zbr = &znode->zbranch[n];
2509 lnc_free(zbr);
2510
2511 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2512 if (err) {
2513 ubifs_dump_znode(c, znode);
2514 return err;
2515 }
2516
2517
2518 for (i = n; i < znode->child_cnt - 1; i++)
2519 znode->zbranch[i] = znode->zbranch[i + 1];
2520 znode->child_cnt -= 1;
2521
2522 if (znode->child_cnt > 0)
2523 return 0;
2524
2525
2526
2527
2528
2529
2530 do {
2531 ubifs_assert(!ubifs_zn_obsolete(znode));
2532 ubifs_assert(ubifs_zn_dirty(znode));
2533
2534 zp = znode->parent;
2535 n = znode->iip;
2536
2537 atomic_long_dec(&c->dirty_zn_cnt);
2538
2539 err = insert_old_idx_znode(c, znode);
2540 if (err)
2541 return err;
2542
2543 if (znode->cnext) {
2544 __set_bit(OBSOLETE_ZNODE, &znode->flags);
2545 atomic_long_inc(&c->clean_zn_cnt);
2546 atomic_long_inc(&ubifs_clean_zn_cnt);
2547 } else
2548 kfree(znode);
2549 znode = zp;
2550 } while (znode->child_cnt == 1);
2551
2552
2553 znode->child_cnt -= 1;
2554 ubifs_assert(znode->level != 0);
2555 for (i = n; i < znode->child_cnt; i++) {
2556 znode->zbranch[i] = znode->zbranch[i + 1];
2557 if (znode->zbranch[i].znode)
2558 znode->zbranch[i].znode->iip = i;
2559 }
2560
2561
2562
2563
2564
2565 if (!znode->parent) {
2566 while (znode->child_cnt == 1 && znode->level != 0) {
2567 zp = znode;
2568 zbr = &znode->zbranch[0];
2569 znode = get_znode(c, znode, 0);
2570 if (IS_ERR(znode))
2571 return PTR_ERR(znode);
2572 znode = dirty_cow_znode(c, zbr);
2573 if (IS_ERR(znode))
2574 return PTR_ERR(znode);
2575 znode->parent = NULL;
2576 znode->iip = 0;
2577 if (c->zroot.len) {
2578 err = insert_old_idx(c, c->zroot.lnum,
2579 c->zroot.offs);
2580 if (err)
2581 return err;
2582 }
2583 c->zroot.lnum = zbr->lnum;
2584 c->zroot.offs = zbr->offs;
2585 c->zroot.len = zbr->len;
2586 c->zroot.znode = znode;
2587 ubifs_assert(!ubifs_zn_obsolete(zp));
2588 ubifs_assert(ubifs_zn_dirty(zp));
2589 atomic_long_dec(&c->dirty_zn_cnt);
2590
2591 if (zp->cnext) {
2592 __set_bit(OBSOLETE_ZNODE, &zp->flags);
2593 atomic_long_inc(&c->clean_zn_cnt);
2594 atomic_long_inc(&ubifs_clean_zn_cnt);
2595 } else
2596 kfree(zp);
2597 }
2598 }
2599
2600 return 0;
2601}
2602
2603
2604
2605
2606
2607
2608
2609
2610int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2611{
2612 int found, n, err = 0;
2613 struct ubifs_znode *znode;
2614
2615 mutex_lock(&c->tnc_mutex);
2616 dbg_tnck(key, "key ");
2617 found = lookup_level0_dirty(c, key, &znode, &n);
2618 if (found < 0) {
2619 err = found;
2620 goto out_unlock;
2621 }
2622 if (found == 1)
2623 err = tnc_delete(c, znode, n);
2624 if (!err)
2625 err = dbg_check_tnc(c, 0);
2626
2627out_unlock:
2628 mutex_unlock(&c->tnc_mutex);
2629 return err;
2630}
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2641 const struct fscrypt_name *nm)
2642{
2643 int n, err;
2644 struct ubifs_znode *znode;
2645
2646 mutex_lock(&c->tnc_mutex);
2647 dbg_tnck(key, "key ");
2648 err = lookup_level0_dirty(c, key, &znode, &n);
2649 if (err < 0)
2650 goto out_unlock;
2651
2652 if (err) {
2653 if (c->replaying)
2654 err = fallible_resolve_collision(c, key, &znode, &n,
2655 nm, 0);
2656 else
2657 err = resolve_collision(c, key, &znode, &n, nm);
2658 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
2659 if (err < 0)
2660 goto out_unlock;
2661 if (err) {
2662
2663 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2664 znode = dirty_cow_bottom_up(c, znode);
2665 if (IS_ERR(znode)) {
2666 err = PTR_ERR(znode);
2667 goto out_unlock;
2668 }
2669 }
2670 err = tnc_delete(c, znode, n);
2671 }
2672 }
2673
2674out_unlock:
2675 if (!err)
2676 err = dbg_check_tnc(c, 0);
2677 mutex_unlock(&c->tnc_mutex);
2678 return err;
2679}
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689int ubifs_tnc_remove_dh(struct ubifs_info *c, const union ubifs_key *key,
2690 uint32_t cookie)
2691{
2692 int n, err;
2693 struct ubifs_znode *znode;
2694 struct ubifs_dent_node *dent;
2695 struct ubifs_zbranch *zbr;
2696
2697 if (!c->double_hash)
2698 return -EOPNOTSUPP;
2699
2700 mutex_lock(&c->tnc_mutex);
2701 err = lookup_level0_dirty(c, key, &znode, &n);
2702 if (err <= 0)
2703 goto out_unlock;
2704
2705 zbr = &znode->zbranch[n];
2706 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
2707 if (!dent) {
2708 err = -ENOMEM;
2709 goto out_unlock;
2710 }
2711
2712 err = tnc_read_hashed_node(c, zbr, dent);
2713 if (err)
2714 goto out_free;
2715
2716
2717 if (le32_to_cpu(dent->cookie) != cookie) {
2718 union ubifs_key start_key;
2719
2720 lowest_dent_key(c, &start_key, key_inum(c, key));
2721
2722 err = ubifs_lookup_level0(c, &start_key, &znode, &n);
2723 if (unlikely(err < 0))
2724 goto out_free;
2725
2726 err = search_dh_cookie(c, key, dent, cookie, &znode, &n);
2727 if (err)
2728 goto out_free;
2729 }
2730
2731 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2732 znode = dirty_cow_bottom_up(c, znode);
2733 if (IS_ERR(znode)) {
2734 err = PTR_ERR(znode);
2735 goto out_free;
2736 }
2737 }
2738 err = tnc_delete(c, znode, n);
2739
2740out_free:
2741 kfree(dent);
2742out_unlock:
2743 if (!err)
2744 err = dbg_check_tnc(c, 0);
2745 mutex_unlock(&c->tnc_mutex);
2746 return err;
2747}
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
2759 union ubifs_key *from_key, union ubifs_key *to_key)
2760{
2761 if (keys_cmp(c, key, from_key) < 0)
2762 return 0;
2763 if (keys_cmp(c, key, to_key) > 0)
2764 return 0;
2765 return 1;
2766}
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2779 union ubifs_key *to_key)
2780{
2781 int i, n, k, err = 0;
2782 struct ubifs_znode *znode;
2783 union ubifs_key *key;
2784
2785 mutex_lock(&c->tnc_mutex);
2786 while (1) {
2787
2788 err = ubifs_lookup_level0(c, from_key, &znode, &n);
2789 if (err < 0)
2790 goto out_unlock;
2791
2792 if (err)
2793 key = from_key;
2794 else {
2795 err = tnc_next(c, &znode, &n);
2796 if (err == -ENOENT) {
2797 err = 0;
2798 goto out_unlock;
2799 }
2800 if (err < 0)
2801 goto out_unlock;
2802 key = &znode->zbranch[n].key;
2803 if (!key_in_range(c, key, from_key, to_key)) {
2804 err = 0;
2805 goto out_unlock;
2806 }
2807 }
2808
2809
2810 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2811 znode = dirty_cow_bottom_up(c, znode);
2812 if (IS_ERR(znode)) {
2813 err = PTR_ERR(znode);
2814 goto out_unlock;
2815 }
2816 }
2817
2818
2819 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
2820 key = &znode->zbranch[i].key;
2821 if (!key_in_range(c, key, from_key, to_key))
2822 break;
2823 lnc_free(&znode->zbranch[i]);
2824 err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2825 znode->zbranch[i].len);
2826 if (err) {
2827 ubifs_dump_znode(c, znode);
2828 goto out_unlock;
2829 }
2830 dbg_tnck(key, "removing key ");
2831 }
2832 if (k) {
2833 for (i = n + 1 + k; i < znode->child_cnt; i++)
2834 znode->zbranch[i - k] = znode->zbranch[i];
2835 znode->child_cnt -= k;
2836 }
2837
2838
2839 err = tnc_delete(c, znode, n);
2840 if (err)
2841 goto out_unlock;
2842 }
2843
2844out_unlock:
2845 if (!err)
2846 err = dbg_check_tnc(c, 0);
2847 mutex_unlock(&c->tnc_mutex);
2848 return err;
2849}
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2861{
2862 union ubifs_key key1, key2;
2863 struct ubifs_dent_node *xent, *pxent = NULL;
2864 struct fscrypt_name nm = {0};
2865
2866 dbg_tnc("ino %lu", (unsigned long)inum);
2867
2868
2869
2870
2871
2872 lowest_xent_key(c, &key1, inum);
2873 while (1) {
2874 ino_t xattr_inum;
2875 int err;
2876
2877 xent = ubifs_tnc_next_ent(c, &key1, &nm);
2878 if (IS_ERR(xent)) {
2879 err = PTR_ERR(xent);
2880 if (err == -ENOENT)
2881 break;
2882 return err;
2883 }
2884
2885 xattr_inum = le64_to_cpu(xent->inum);
2886 dbg_tnc("xent '%s', ino %lu", xent->name,
2887 (unsigned long)xattr_inum);
2888
2889 ubifs_evict_xattr_inode(c, xattr_inum);
2890
2891 fname_name(&nm) = xent->name;
2892 fname_len(&nm) = le16_to_cpu(xent->nlen);
2893 err = ubifs_tnc_remove_nm(c, &key1, &nm);
2894 if (err) {
2895 kfree(xent);
2896 return err;
2897 }
2898
2899 lowest_ino_key(c, &key1, xattr_inum);
2900 highest_ino_key(c, &key2, xattr_inum);
2901 err = ubifs_tnc_remove_range(c, &key1, &key2);
2902 if (err) {
2903 kfree(xent);
2904 return err;
2905 }
2906
2907 kfree(pxent);
2908 pxent = xent;
2909 key_read(c, &xent->key, &key1);
2910 }
2911
2912 kfree(pxent);
2913 lowest_ino_key(c, &key1, inum);
2914 highest_ino_key(c, &key2, inum);
2915
2916 return ubifs_tnc_remove_range(c, &key1, &key2);
2917}
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2943 union ubifs_key *key,
2944 const struct fscrypt_name *nm)
2945{
2946 int n, err, type = key_type(c, key);
2947 struct ubifs_znode *znode;
2948 struct ubifs_dent_node *dent;
2949 struct ubifs_zbranch *zbr;
2950 union ubifs_key *dkey;
2951
2952 dbg_tnck(key, "key ");
2953 ubifs_assert(is_hash_key(c, key));
2954
2955 mutex_lock(&c->tnc_mutex);
2956 err = ubifs_lookup_level0(c, key, &znode, &n);
2957 if (unlikely(err < 0))
2958 goto out_unlock;
2959
2960 if (fname_len(nm) > 0) {
2961 if (err) {
2962
2963 if (c->replaying)
2964 err = fallible_resolve_collision(c, key, &znode, &n,
2965 nm, 0);
2966 else
2967 err = resolve_collision(c, key, &znode, &n, nm);
2968 dbg_tnc("rc returned %d, znode %p, n %d",
2969 err, znode, n);
2970 if (unlikely(err < 0))
2971 goto out_unlock;
2972 }
2973
2974
2975 err = tnc_next(c, &znode, &n);
2976 if (unlikely(err))
2977 goto out_unlock;
2978 } else {
2979
2980
2981
2982
2983
2984 if (!err) {
2985
2986
2987
2988
2989
2990 err = tnc_next(c, &znode, &n);
2991 if (err)
2992 goto out_unlock;
2993 }
2994 }
2995
2996 zbr = &znode->zbranch[n];
2997 dent = kmalloc(zbr->len, GFP_NOFS);
2998 if (unlikely(!dent)) {
2999 err = -ENOMEM;
3000 goto out_unlock;
3001 }
3002
3003
3004
3005
3006
3007 dkey = &zbr->key;
3008 if (key_inum(c, dkey) != key_inum(c, key) ||
3009 key_type(c, dkey) != type) {
3010 err = -ENOENT;
3011 goto out_free;
3012 }
3013
3014 err = tnc_read_hashed_node(c, zbr, dent);
3015 if (unlikely(err))
3016 goto out_free;
3017
3018 mutex_unlock(&c->tnc_mutex);
3019 return dent;
3020
3021out_free:
3022 kfree(dent);
3023out_unlock:
3024 mutex_unlock(&c->tnc_mutex);
3025 return ERR_PTR(err);
3026}
3027
3028
3029
3030
3031
3032
3033
3034static void tnc_destroy_cnext(struct ubifs_info *c)
3035{
3036 struct ubifs_znode *cnext;
3037
3038 if (!c->cnext)
3039 return;
3040 ubifs_assert(c->cmt_state == COMMIT_BROKEN);
3041 cnext = c->cnext;
3042 do {
3043 struct ubifs_znode *znode = cnext;
3044
3045 cnext = cnext->cnext;
3046 if (ubifs_zn_obsolete(znode))
3047 kfree(znode);
3048 } while (cnext && cnext != c->cnext);
3049}
3050
3051
3052
3053
3054
3055void ubifs_tnc_close(struct ubifs_info *c)
3056{
3057 tnc_destroy_cnext(c);
3058 if (c->zroot.znode) {
3059 long n, freed;
3060
3061 n = atomic_long_read(&c->clean_zn_cnt);
3062 freed = ubifs_destroy_tnc_subtree(c->zroot.znode);
3063 ubifs_assert(freed == n);
3064 atomic_long_sub(n, &ubifs_clean_zn_cnt);
3065 }
3066 kfree(c->gap_lebs);
3067 kfree(c->ilebs);
3068 destroy_old_idx(c);
3069}
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079static struct ubifs_znode *left_znode(struct ubifs_info *c,
3080 struct ubifs_znode *znode)
3081{
3082 int level = znode->level;
3083
3084 while (1) {
3085 int n = znode->iip - 1;
3086
3087
3088 znode = znode->parent;
3089 if (!znode)
3090 return NULL;
3091 if (n >= 0) {
3092
3093 znode = get_znode(c, znode, n);
3094 if (IS_ERR(znode))
3095 return znode;
3096 while (znode->level != level) {
3097 n = znode->child_cnt - 1;
3098 znode = get_znode(c, znode, n);
3099 if (IS_ERR(znode))
3100 return znode;
3101 }
3102 break;
3103 }
3104 }
3105 return znode;
3106}
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116static struct ubifs_znode *right_znode(struct ubifs_info *c,
3117 struct ubifs_znode *znode)
3118{
3119 int level = znode->level;
3120
3121 while (1) {
3122 int n = znode->iip + 1;
3123
3124
3125 znode = znode->parent;
3126 if (!znode)
3127 return NULL;
3128 if (n < znode->child_cnt) {
3129
3130 znode = get_znode(c, znode, n);
3131 if (IS_ERR(znode))
3132 return znode;
3133 while (znode->level != level) {
3134 znode = get_znode(c, znode, 0);
3135 if (IS_ERR(znode))
3136 return znode;
3137 }
3138 break;
3139 }
3140 }
3141 return znode;
3142}
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169static struct ubifs_znode *lookup_znode(struct ubifs_info *c,
3170 union ubifs_key *key, int level,
3171 int lnum, int offs)
3172{
3173 struct ubifs_znode *znode, *zn;
3174 int n, nn;
3175
3176 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY);
3177
3178
3179
3180
3181
3182 if (level < 0)
3183 return ERR_PTR(-EINVAL);
3184
3185
3186 znode = c->zroot.znode;
3187 if (!znode) {
3188 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
3189 if (IS_ERR(znode))
3190 return znode;
3191 }
3192
3193 if (c->zroot.lnum == lnum && c->zroot.offs == offs)
3194 return znode;
3195
3196 if (level >= znode->level)
3197 return NULL;
3198 while (1) {
3199 ubifs_search_zbranch(c, znode, key, &n);
3200 if (n < 0) {
3201
3202
3203
3204
3205
3206
3207
3208
3209 znode = left_znode(c, znode);
3210 if (!znode)
3211 return NULL;
3212 if (IS_ERR(znode))
3213 return znode;
3214 ubifs_search_zbranch(c, znode, key, &n);
3215 ubifs_assert(n >= 0);
3216 }
3217 if (znode->level == level + 1)
3218 break;
3219 znode = get_znode(c, znode, n);
3220 if (IS_ERR(znode))
3221 return znode;
3222 }
3223
3224 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs)
3225 return get_znode(c, znode, n);
3226
3227 if (!is_hash_key(c, key))
3228 return NULL;
3229
3230
3231
3232
3233 zn = znode;
3234 nn = n;
3235
3236 while (1) {
3237
3238 if (n)
3239 n -= 1;
3240 else {
3241 znode = left_znode(c, znode);
3242 if (!znode)
3243 break;
3244 if (IS_ERR(znode))
3245 return znode;
3246 n = znode->child_cnt - 1;
3247 }
3248
3249 if (znode->zbranch[n].lnum == lnum &&
3250 znode->zbranch[n].offs == offs)
3251 return get_znode(c, znode, n);
3252
3253 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0)
3254 break;
3255 }
3256
3257 znode = zn;
3258 n = nn;
3259
3260 while (1) {
3261
3262 if (++n >= znode->child_cnt) {
3263 znode = right_znode(c, znode);
3264 if (!znode)
3265 break;
3266 if (IS_ERR(znode))
3267 return znode;
3268 n = 0;
3269 }
3270
3271 if (znode->zbranch[n].lnum == lnum &&
3272 znode->zbranch[n].offs == offs)
3273 return get_znode(c, znode, n);
3274
3275 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0)
3276 break;
3277 }
3278 return NULL;
3279}
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
3299 int lnum, int offs)
3300{
3301 struct ubifs_znode *znode;
3302
3303 znode = lookup_znode(c, key, level, lnum, offs);
3304 if (!znode)
3305 return 0;
3306 if (IS_ERR(znode))
3307 return PTR_ERR(znode);
3308
3309 return ubifs_zn_dirty(znode) ? 1 : 2;
3310}
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key,
3326 int lnum, int offs)
3327{
3328 struct ubifs_zbranch *zbr;
3329 struct ubifs_znode *znode, *zn;
3330 int n, found, err, nn;
3331 const int unique = !is_hash_key(c, key);
3332
3333 found = ubifs_lookup_level0(c, key, &znode, &n);
3334 if (found < 0)
3335 return found;
3336 if (!found)
3337 return 0;
3338 zbr = &znode->zbranch[n];
3339 if (lnum == zbr->lnum && offs == zbr->offs)
3340 return 1;
3341 if (unique)
3342 return 0;
3343
3344
3345
3346
3347 zn = znode;
3348 nn = n;
3349
3350 while (1) {
3351 err = tnc_prev(c, &znode, &n);
3352 if (err == -ENOENT)
3353 break;
3354 if (err)
3355 return err;
3356 if (keys_cmp(c, key, &znode->zbranch[n].key))
3357 break;
3358 zbr = &znode->zbranch[n];
3359 if (lnum == zbr->lnum && offs == zbr->offs)
3360 return 1;
3361 }
3362
3363 znode = zn;
3364 n = nn;
3365 while (1) {
3366 err = tnc_next(c, &znode, &n);
3367 if (err) {
3368 if (err == -ENOENT)
3369 return 0;
3370 return err;
3371 }
3372 if (keys_cmp(c, key, &znode->zbranch[n].key))
3373 break;
3374 zbr = &znode->zbranch[n];
3375 if (lnum == zbr->lnum && offs == zbr->offs)
3376 return 1;
3377 }
3378 return 0;
3379}
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
3396 int lnum, int offs, int is_idx)
3397{
3398 int err;
3399
3400 mutex_lock(&c->tnc_mutex);
3401 if (is_idx) {
3402 err = is_idx_node_in_tnc(c, key, level, lnum, offs);
3403 if (err < 0)
3404 goto out_unlock;
3405 if (err == 1)
3406
3407 err = 0;
3408 else if (err == 2)
3409
3410 err = 1;
3411 else
3412 BUG_ON(err != 0);
3413 } else
3414 err = is_leaf_node_in_tnc(c, key, lnum, offs);
3415
3416out_unlock:
3417 mutex_unlock(&c->tnc_mutex);
3418 return err;
3419}
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
3436 int lnum, int offs)
3437{
3438 struct ubifs_znode *znode;
3439 int err = 0;
3440
3441 mutex_lock(&c->tnc_mutex);
3442 znode = lookup_znode(c, key, level, lnum, offs);
3443 if (!znode)
3444 goto out_unlock;
3445 if (IS_ERR(znode)) {
3446 err = PTR_ERR(znode);
3447 goto out_unlock;
3448 }
3449 znode = dirty_cow_bottom_up(c, znode);
3450 if (IS_ERR(znode)) {
3451 err = PTR_ERR(znode);
3452 goto out_unlock;
3453 }
3454
3455out_unlock:
3456 mutex_unlock(&c->tnc_mutex);
3457 return err;
3458}
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
3472 loff_t size)
3473{
3474 int err, n;
3475 union ubifs_key from_key, to_key, *key;
3476 struct ubifs_znode *znode;
3477 unsigned int block;
3478
3479 if (!S_ISREG(inode->i_mode))
3480 return 0;
3481 if (!dbg_is_chk_gen(c))
3482 return 0;
3483
3484 block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
3485 data_key_init(c, &from_key, inode->i_ino, block);
3486 highest_data_key(c, &to_key, inode->i_ino);
3487
3488 mutex_lock(&c->tnc_mutex);
3489 err = ubifs_lookup_level0(c, &from_key, &znode, &n);
3490 if (err < 0)
3491 goto out_unlock;
3492
3493 if (err) {
3494 key = &from_key;
3495 goto out_dump;
3496 }
3497
3498 err = tnc_next(c, &znode, &n);
3499 if (err == -ENOENT) {
3500 err = 0;
3501 goto out_unlock;
3502 }
3503 if (err < 0)
3504 goto out_unlock;
3505
3506 ubifs_assert(err == 0);
3507 key = &znode->zbranch[n].key;
3508 if (!key_in_range(c, key, &from_key, &to_key))
3509 goto out_unlock;
3510
3511out_dump:
3512 block = key_block(c, key);
3513 ubifs_err(c, "inode %lu has size %lld, but there are data at offset %lld",
3514 (unsigned long)inode->i_ino, size,
3515 ((loff_t)block) << UBIFS_BLOCK_SHIFT);
3516 mutex_unlock(&c->tnc_mutex);
3517 ubifs_dump_inode(c, inode);
3518 dump_stack();
3519 return -EINVAL;
3520
3521out_unlock:
3522 mutex_unlock(&c->tnc_mutex);
3523 return err;
3524}
3525