forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tnc.c
3326 lines (3008 loc) · 86.7 KB
/
tnc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of UBIFS.
*
* Copyright (C) 2006-2008 Nokia Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: Adrian Hunter
* Artem Bityutskiy (Битюцкий Артём)
*/
/*
* This file implements TNC (Tree Node Cache) which caches indexing nodes of
* the UBIFS B-tree.
*
* At the moment the locking rules of the TNC tree are quite simple and
* straightforward. We just have a mutex and lock it when we traverse the
* tree. If a znode is not in memory, we read it from flash while still having
* the mutex locked.
*/
#include <linux/crc32.h>
#include <linux/slab.h>
#include "ubifs.h"
/*
* Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
* @NAME_LESS: name corresponding to the first argument is less than second
* @NAME_MATCHES: names match
* @NAME_GREATER: name corresponding to the second argument is greater than
* first
* @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
*
* These constants were introduce to improve readability.
*/
enum {
NAME_LESS = 0,
NAME_MATCHES = 1,
NAME_GREATER = 2,
NOT_ON_MEDIA = 3,
};
/**
* insert_old_idx - record an index node obsoleted since the last commit start.
* @c: UBIFS file-system description object
* @lnum: LEB number of obsoleted index node
* @offs: offset of obsoleted index node
*
* Returns %0 on success, and a negative error code on failure.
*
* For recovery, there must always be a complete intact version of the index on
* flash at all times. That is called the "old index". It is the index as at the
* time of the last successful commit. Many of the index nodes in the old index
* may be dirty, but they must not be erased until the next successful commit
* (at which point that index becomes the old index).
*
* That means that the garbage collection and the in-the-gaps method of
* committing must be able to determine if an index node is in the old index.
* Most of the old index nodes can be found by looking up the TNC using the
* 'lookup_znode()' function. However, some of the old index nodes may have
* been deleted from the current index or may have been changed so much that
* they cannot be easily found. In those cases, an entry is added to an RB-tree.
* That is what this function does. The RB-tree is ordered by LEB number and
* offset because they uniquely identify the old index node.
*/
static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
{
struct ubifs_old_idx *old_idx, *o;
struct rb_node **p, *parent = NULL;
old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
if (unlikely(!old_idx))
return -ENOMEM;
old_idx->lnum = lnum;
old_idx->offs = offs;
p = &c->old_idx.rb_node;
while (*p) {
parent = *p;
o = rb_entry(parent, struct ubifs_old_idx, rb);
if (lnum < o->lnum)
p = &(*p)->rb_left;
else if (lnum > o->lnum)
p = &(*p)->rb_right;
else if (offs < o->offs)
p = &(*p)->rb_left;
else if (offs > o->offs)
p = &(*p)->rb_right;
else {
ubifs_err(c, "old idx added twice!");
kfree(old_idx);
return 0;
}
}
rb_link_node(&old_idx->rb, parent, p);
rb_insert_color(&old_idx->rb, &c->old_idx);
return 0;
}
/**
* insert_old_idx_znode - record a znode obsoleted since last commit start.
* @c: UBIFS file-system description object
* @znode: znode of obsoleted index node
*
* Returns %0 on success, and a negative error code on failure.
*/
int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
{
if (znode->parent) {
struct ubifs_zbranch *zbr;
zbr = &znode->parent->zbranch[znode->iip];
if (zbr->len)
return insert_old_idx(c, zbr->lnum, zbr->offs);
} else
if (c->zroot.len)
return insert_old_idx(c, c->zroot.lnum,
c->zroot.offs);
return 0;
}
/**
* ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
* @c: UBIFS file-system description object
* @znode: znode of obsoleted index node
*
* Returns %0 on success, and a negative error code on failure.
*/
static int ins_clr_old_idx_znode(struct ubifs_info *c,
struct ubifs_znode *znode)
{
int err;
if (znode->parent) {
struct ubifs_zbranch *zbr;
zbr = &znode->parent->zbranch[znode->iip];
if (zbr->len) {
err = insert_old_idx(c, zbr->lnum, zbr->offs);
if (err)
return err;
zbr->lnum = 0;
zbr->offs = 0;
zbr->len = 0;
}
} else
if (c->zroot.len) {
err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
if (err)
return err;
c->zroot.lnum = 0;
c->zroot.offs = 0;
c->zroot.len = 0;
}
return 0;
}
/**
* destroy_old_idx - destroy the old_idx RB-tree.
* @c: UBIFS file-system description object
*
* During start commit, the old_idx RB-tree is used to avoid overwriting index
* nodes that were in the index last commit but have since been deleted. This
* is necessary for recovery i.e. the old index must be kept intact until the
* new index is successfully written. The old-idx RB-tree is used for the
* in-the-gaps method of writing index nodes and is destroyed every commit.
*/
void destroy_old_idx(struct ubifs_info *c)
{
struct ubifs_old_idx *old_idx, *n;
rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb)
kfree(old_idx);
c->old_idx = RB_ROOT;
}
/**
* copy_znode - copy a dirty znode.
* @c: UBIFS file-system description object
* @znode: znode to copy
*
* A dirty znode being committed may not be changed, so it is copied.
*/
static struct ubifs_znode *copy_znode(struct ubifs_info *c,
struct ubifs_znode *znode)
{
struct ubifs_znode *zn;
zn = kmemdup(znode, c->max_znode_sz, GFP_NOFS);
if (unlikely(!zn))
return ERR_PTR(-ENOMEM);
zn->cnext = NULL;
__set_bit(DIRTY_ZNODE, &zn->flags);
__clear_bit(COW_ZNODE, &zn->flags);
ubifs_assert(!ubifs_zn_obsolete(znode));
__set_bit(OBSOLETE_ZNODE, &znode->flags);
if (znode->level != 0) {
int i;
const int n = zn->child_cnt;
/* The children now have new parent */
for (i = 0; i < n; i++) {
struct ubifs_zbranch *zbr = &zn->zbranch[i];
if (zbr->znode)
zbr->znode->parent = zn;
}
}
atomic_long_inc(&c->dirty_zn_cnt);
return zn;
}
/**
* add_idx_dirt - add dirt due to a dirty znode.
* @c: UBIFS file-system description object
* @lnum: LEB number of index node
* @dirt: size of index node
*
* This function updates lprops dirty space and the new size of the index.
*/
static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
{
c->calc_idx_sz -= ALIGN(dirt, 8);
return ubifs_add_dirt(c, lnum, dirt);
}
/**
* dirty_cow_znode - ensure a znode is not being committed.
* @c: UBIFS file-system description object
* @zbr: branch of znode to check
*
* Returns dirtied znode on success or negative error code on failure.
*/
static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
struct ubifs_zbranch *zbr)
{
struct ubifs_znode *znode = zbr->znode;
struct ubifs_znode *zn;
int err;
if (!ubifs_zn_cow(znode)) {
/* znode is not being committed */
if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
atomic_long_inc(&c->dirty_zn_cnt);
atomic_long_dec(&c->clean_zn_cnt);
atomic_long_dec(&ubifs_clean_zn_cnt);
err = add_idx_dirt(c, zbr->lnum, zbr->len);
if (unlikely(err))
return ERR_PTR(err);
}
return znode;
}
zn = copy_znode(c, znode);
if (IS_ERR(zn))
return zn;
if (zbr->len) {
err = insert_old_idx(c, zbr->lnum, zbr->offs);
if (unlikely(err))
return ERR_PTR(err);
err = add_idx_dirt(c, zbr->lnum, zbr->len);
} else
err = 0;
zbr->znode = zn;
zbr->lnum = 0;
zbr->offs = 0;
zbr->len = 0;
if (unlikely(err))
return ERR_PTR(err);
return zn;
}
/**
* lnc_add - add a leaf node to the leaf node cache.
* @c: UBIFS file-system description object
* @zbr: zbranch of leaf node
* @node: leaf node
*
* Leaf nodes are non-index nodes directory entry nodes or data nodes. The
* purpose of the leaf node cache is to save re-reading the same leaf node over
* and over again. Most things are cached by VFS, however the file system must
* cache directory entries for readdir and for resolving hash collisions. The
* present implementation of the leaf node cache is extremely simple, and
* allows for error returns that are not used but that may be needed if a more
* complex implementation is created.
*
* Note, this function does not add the @node object to LNC directly, but
* allocates a copy of the object and adds the copy to LNC. The reason for this
* is that @node has been allocated outside of the TNC subsystem and will be
* used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
* may be changed at any time, e.g. freed by the shrinker.
*/
static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
const void *node)
{
int err;
void *lnc_node;
const struct ubifs_dent_node *dent = node;
ubifs_assert(!zbr->leaf);
ubifs_assert(zbr->len != 0);
ubifs_assert(is_hash_key(c, &zbr->key));
err = ubifs_validate_entry(c, dent);
if (err) {
dump_stack();
ubifs_dump_node(c, dent);
return err;
}
lnc_node = kmemdup(node, zbr->len, GFP_NOFS);
if (!lnc_node)
/* We don't have to have the cache, so no error */
return 0;
zbr->leaf = lnc_node;
return 0;
}
/**
* lnc_add_directly - add a leaf node to the leaf-node-cache.
* @c: UBIFS file-system description object
* @zbr: zbranch of leaf node
* @node: leaf node
*
* This function is similar to 'lnc_add()', but it does not create a copy of
* @node but inserts @node to TNC directly.
*/
static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
void *node)
{
int err;
ubifs_assert(!zbr->leaf);
ubifs_assert(zbr->len != 0);
err = ubifs_validate_entry(c, node);
if (err) {
dump_stack();
ubifs_dump_node(c, node);
return err;
}
zbr->leaf = node;
return 0;
}
/**
* lnc_free - remove a leaf node from the leaf node cache.
* @zbr: zbranch of leaf node
* @node: leaf node
*/
static void lnc_free(struct ubifs_zbranch *zbr)
{
if (!zbr->leaf)
return;
kfree(zbr->leaf);
zbr->leaf = NULL;
}
/**
* tnc_read_node_nm - read a "hashed" leaf node.
* @c: UBIFS file-system description object
* @zbr: key and position of the node
* @node: node is returned here
*
* This function reads a "hashed" node defined by @zbr from the leaf node cache
* (in it is there) or from the hash media, in which case the node is also
* added to LNC. Returns zero in case of success or a negative negative error
* code in case of failure.
*/
static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
void *node)
{
int err;
ubifs_assert(is_hash_key(c, &zbr->key));
if (zbr->leaf) {
/* Read from the leaf node cache */
ubifs_assert(zbr->len != 0);
memcpy(node, zbr->leaf, zbr->len);
return 0;
}
err = ubifs_tnc_read_node(c, zbr, node);
if (err)
return err;
/* Add the node to the leaf node cache */
err = lnc_add(c, zbr, node);
return err;
}
/**
* try_read_node - read a node if it is a node.
* @c: UBIFS file-system description object
* @buf: buffer to read to
* @type: node type
* @len: node length (not aligned)
* @lnum: LEB number of node to read
* @offs: offset of node to read
*
* This function tries to read a node of known type and length, checks it and
* stores it in @buf. This function returns %1 if a node is present and %0 if
* a node is not present. A negative error code is returned for I/O errors.
* This function performs that same function as ubifs_read_node except that
* it does not require that there is actually a node present and instead
* the return code indicates if a node was read.
*
* Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
* is true (it is controlled by corresponding mount option). However, if
* @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
* R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
* because during mounting or re-mounting from R/O mode to R/W mode we may read
* journal nodes (when replying the journal or doing the recovery) and the
* journal nodes may potentially be corrupted, so checking is required.
*/
static int try_read_node(const struct ubifs_info *c, void *buf, int type,
int len, int lnum, int offs)
{
int err, node_len;
struct ubifs_ch *ch = buf;
uint32_t crc, node_crc;
dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
err = ubifs_leb_read(c, lnum, buf, offs, len, 1);
if (err) {
ubifs_err(c, "cannot read node type %d from LEB %d:%d, error %d",
type, lnum, offs, err);
return err;
}
if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
return 0;
if (ch->node_type != type)
return 0;
node_len = le32_to_cpu(ch->len);
if (node_len != len)
return 0;
if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting &&
!c->remounting_rw)
return 1;
crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
node_crc = le32_to_cpu(ch->crc);
if (crc != node_crc)
return 0;
return 1;
}
/**
* fallible_read_node - try to read a leaf node.
* @c: UBIFS file-system description object
* @key: key of node to read
* @zbr: position of node
* @node: node returned
*
* This function tries to read a node and returns %1 if the node is read, %0
* if the node is not present, and a negative error code in the case of error.
*/
static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
struct ubifs_zbranch *zbr, void *node)
{
int ret;
dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
zbr->offs);
if (ret == 1) {
union ubifs_key node_key;
struct ubifs_dent_node *dent = node;
/* All nodes have key in the same place */
key_read(c, &dent->key, &node_key);
if (keys_cmp(c, key, &node_key) != 0)
ret = 0;
}
if (ret == 0 && c->replaying)
dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ",
zbr->lnum, zbr->offs, zbr->len);
return ret;
}
/**
* matches_name - determine if a direntry or xattr entry matches a given name.
* @c: UBIFS file-system description object
* @zbr: zbranch of dent
* @nm: name to match
*
* This function checks if xentry/direntry referred by zbranch @zbr matches name
* @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
* @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
* of failure, a negative error code is returned.
*/
static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
const struct qstr *nm)
{
struct ubifs_dent_node *dent;
int nlen, err;
/* If possible, match against the dent in the leaf node cache */
if (!zbr->leaf) {
dent = kmalloc(zbr->len, GFP_NOFS);
if (!dent)
return -ENOMEM;
err = ubifs_tnc_read_node(c, zbr, dent);
if (err)
goto out_free;
/* Add the node to the leaf node cache */
err = lnc_add_directly(c, zbr, dent);
if (err)
goto out_free;
} else
dent = zbr->leaf;
nlen = le16_to_cpu(dent->nlen);
err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
if (err == 0) {
if (nlen == nm->len)
return NAME_MATCHES;
else if (nlen < nm->len)
return NAME_LESS;
else
return NAME_GREATER;
} else if (err < 0)
return NAME_LESS;
else
return NAME_GREATER;
out_free:
kfree(dent);
return err;
}
/**
* get_znode - get a TNC znode that may not be loaded yet.
* @c: UBIFS file-system description object
* @znode: parent znode
* @n: znode branch slot number
*
* This function returns the znode or a negative error code.
*/
static struct ubifs_znode *get_znode(struct ubifs_info *c,
struct ubifs_znode *znode, int n)
{
struct ubifs_zbranch *zbr;
zbr = &znode->zbranch[n];
if (zbr->znode)
znode = zbr->znode;
else
znode = ubifs_load_znode(c, zbr, znode, n);
return znode;
}
/**
* tnc_next - find next TNC entry.
* @c: UBIFS file-system description object
* @zn: znode is passed and returned here
* @n: znode branch slot number is passed and returned here
*
* This function returns %0 if the next TNC entry is found, %-ENOENT if there is
* no next entry, or a negative error code otherwise.
*/
static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
{
struct ubifs_znode *znode = *zn;
int nn = *n;
nn += 1;
if (nn < znode->child_cnt) {
*n = nn;
return 0;
}
while (1) {
struct ubifs_znode *zp;
zp = znode->parent;
if (!zp)
return -ENOENT;
nn = znode->iip + 1;
znode = zp;
if (nn < znode->child_cnt) {
znode = get_znode(c, znode, nn);
if (IS_ERR(znode))
return PTR_ERR(znode);
while (znode->level != 0) {
znode = get_znode(c, znode, 0);
if (IS_ERR(znode))
return PTR_ERR(znode);
}
nn = 0;
break;
}
}
*zn = znode;
*n = nn;
return 0;
}
/**
* tnc_prev - find previous TNC entry.
* @c: UBIFS file-system description object
* @zn: znode is returned here
* @n: znode branch slot number is passed and returned here
*
* This function returns %0 if the previous TNC entry is found, %-ENOENT if
* there is no next entry, or a negative error code otherwise.
*/
static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
{
struct ubifs_znode *znode = *zn;
int nn = *n;
if (nn > 0) {
*n = nn - 1;
return 0;
}
while (1) {
struct ubifs_znode *zp;
zp = znode->parent;
if (!zp)
return -ENOENT;
nn = znode->iip - 1;
znode = zp;
if (nn >= 0) {
znode = get_znode(c, znode, nn);
if (IS_ERR(znode))
return PTR_ERR(znode);
while (znode->level != 0) {
nn = znode->child_cnt - 1;
znode = get_znode(c, znode, nn);
if (IS_ERR(znode))
return PTR_ERR(znode);
}
nn = znode->child_cnt - 1;
break;
}
}
*zn = znode;
*n = nn;
return 0;
}
/**
* resolve_collision - resolve a collision.
* @c: UBIFS file-system description object
* @key: key of a directory or extended attribute entry
* @zn: znode is returned here
* @n: zbranch number is passed and returned here
* @nm: name of the entry
*
* This function is called for "hashed" keys to make sure that the found key
* really corresponds to the looked up node (directory or extended attribute
* entry). It returns %1 and sets @zn and @n if the collision is resolved.
* %0 is returned if @nm is not found and @zn and @n are set to the previous
* entry, i.e. to the entry after which @nm could follow if it were in TNC.
* This means that @n may be set to %-1 if the leftmost key in @zn is the
* previous one. A negative error code is returned on failures.
*/
static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
struct ubifs_znode **zn, int *n,
const struct qstr *nm)
{
int err;
err = matches_name(c, &(*zn)->zbranch[*n], nm);
if (unlikely(err < 0))
return err;
if (err == NAME_MATCHES)
return 1;
if (err == NAME_GREATER) {
/* Look left */
while (1) {
err = tnc_prev(c, zn, n);
if (err == -ENOENT) {
ubifs_assert(*n == 0);
*n = -1;
return 0;
}
if (err < 0)
return err;
if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
/*
* We have found the branch after which we would
* like to insert, but inserting in this znode
* may still be wrong. Consider the following 3
* znodes, in the case where we are resolving a
* collision with Key2.
*
* znode zp
* ----------------------
* level 1 | Key0 | Key1 |
* -----------------------
* | |
* znode za | | znode zb
* ------------ ------------
* level 0 | Key0 | | Key2 |
* ------------ ------------
*
* The lookup finds Key2 in znode zb. Lets say
* there is no match and the name is greater so
* we look left. When we find Key0, we end up
* here. If we return now, we will insert into
* znode za at slot n = 1. But that is invalid
* according to the parent's keys. Key2 must
* be inserted into znode zb.
*
* Note, this problem is not relevant for the
* case when we go right, because
* 'tnc_insert()' would correct the parent key.
*/
if (*n == (*zn)->child_cnt - 1) {
err = tnc_next(c, zn, n);
if (err) {
/* Should be impossible */
ubifs_assert(0);
if (err == -ENOENT)
err = -EINVAL;
return err;
}
ubifs_assert(*n == 0);
*n = -1;
}
return 0;
}
err = matches_name(c, &(*zn)->zbranch[*n], nm);
if (err < 0)
return err;
if (err == NAME_LESS)
return 0;
if (err == NAME_MATCHES)
return 1;
ubifs_assert(err == NAME_GREATER);
}
} else {
int nn = *n;
struct ubifs_znode *znode = *zn;
/* Look right */
while (1) {
err = tnc_next(c, &znode, &nn);
if (err == -ENOENT)
return 0;
if (err < 0)
return err;
if (keys_cmp(c, &znode->zbranch[nn].key, key))
return 0;
err = matches_name(c, &znode->zbranch[nn], nm);
if (err < 0)
return err;
if (err == NAME_GREATER)
return 0;
*zn = znode;
*n = nn;
if (err == NAME_MATCHES)
return 1;
ubifs_assert(err == NAME_LESS);
}
}
}
/**
* fallible_matches_name - determine if a dent matches a given name.
* @c: UBIFS file-system description object
* @zbr: zbranch of dent
* @nm: name to match
*
* This is a "fallible" version of 'matches_name()' function which does not
* panic if the direntry/xentry referred by @zbr does not exist on the media.
*
* This function checks if xentry/direntry referred by zbranch @zbr matches name
* @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
* is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
* if xentry/direntry referred by @zbr does not exist on the media. A negative
* error code is returned in case of failure.
*/
static int fallible_matches_name(struct ubifs_info *c,
struct ubifs_zbranch *zbr,
const struct qstr *nm)
{
struct ubifs_dent_node *dent;
int nlen, err;
/* If possible, match against the dent in the leaf node cache */
if (!zbr->leaf) {
dent = kmalloc(zbr->len, GFP_NOFS);
if (!dent)
return -ENOMEM;
err = fallible_read_node(c, &zbr->key, zbr, dent);
if (err < 0)
goto out_free;
if (err == 0) {
/* The node was not present */
err = NOT_ON_MEDIA;
goto out_free;
}
ubifs_assert(err == 1);
err = lnc_add_directly(c, zbr, dent);
if (err)
goto out_free;
} else
dent = zbr->leaf;
nlen = le16_to_cpu(dent->nlen);
err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
if (err == 0) {
if (nlen == nm->len)
return NAME_MATCHES;
else if (nlen < nm->len)
return NAME_LESS;
else
return NAME_GREATER;
} else if (err < 0)
return NAME_LESS;
else
return NAME_GREATER;
out_free:
kfree(dent);
return err;
}
/**
* fallible_resolve_collision - resolve a collision even if nodes are missing.
* @c: UBIFS file-system description object
* @key: key
* @zn: znode is returned here
* @n: branch number is passed and returned here
* @nm: name of directory entry
* @adding: indicates caller is adding a key to the TNC
*
* This is a "fallible" version of the 'resolve_collision()' function which
* does not panic if one of the nodes referred to by TNC does not exist on the
* media. This may happen when replaying the journal if a deleted node was
* Garbage-collected and the commit was not done. A branch that refers to a node
* that is not present is called a dangling branch. The following are the return
* codes for this function:
* o if @nm was found, %1 is returned and @zn and @n are set to the found
* branch;
* o if we are @adding and @nm was not found, %0 is returned;
* o if we are not @adding and @nm was not found, but a dangling branch was
* found, then %1 is returned and @zn and @n are set to the dangling branch;
* o a negative error code is returned in case of failure.
*/
static int fallible_resolve_collision(struct ubifs_info *c,
const union ubifs_key *key,
struct ubifs_znode **zn, int *n,
const struct qstr *nm, int adding)
{
struct ubifs_znode *o_znode = NULL, *znode = *zn;
int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
if (unlikely(cmp < 0))
return cmp;
if (cmp == NAME_MATCHES)
return 1;
if (cmp == NOT_ON_MEDIA) {
o_znode = znode;
o_n = nn;
/*
* We are unlucky and hit a dangling branch straight away.
* Now we do not really know where to go to find the needed
* branch - to the left or to the right. Well, let's try left.
*/
unsure = 1;
} else if (!adding)
unsure = 1; /* Remove a dangling branch wherever it is */
if (cmp == NAME_GREATER || unsure) {
/* Look left */
while (1) {
err = tnc_prev(c, zn, n);
if (err == -ENOENT) {
ubifs_assert(*n == 0);
*n = -1;
break;
}
if (err < 0)
return err;
if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
/* See comments in 'resolve_collision()' */
if (*n == (*zn)->child_cnt - 1) {
err = tnc_next(c, zn, n);
if (err) {
/* Should be impossible */
ubifs_assert(0);
if (err == -ENOENT)
err = -EINVAL;
return err;
}
ubifs_assert(*n == 0);
*n = -1;
}
break;
}
err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
if (err < 0)
return err;
if (err == NAME_MATCHES)
return 1;
if (err == NOT_ON_MEDIA) {
o_znode = *zn;
o_n = *n;
continue;
}
if (!adding)
continue;
if (err == NAME_LESS)
break;
else
unsure = 0;
}
}
if (cmp == NAME_LESS || unsure) {
/* Look right */
*zn = znode;
*n = nn;
while (1) {
err = tnc_next(c, &znode, &nn);
if (err == -ENOENT)
break;
if (err < 0)
return err;
if (keys_cmp(c, &znode->zbranch[nn].key, key))
break;
err = fallible_matches_name(c, &znode->zbranch[nn], nm);
if (err < 0)
return err;
if (err == NAME_GREATER)
break;
*zn = znode;
*n = nn;
if (err == NAME_MATCHES)
return 1;
if (err == NOT_ON_MEDIA) {
o_znode = znode;
o_n = nn;
}
}
}
/* Never match a dangling branch when adding */
if (adding || !o_znode)
return 0;
dbg_mntk(key, "dangling match LEB %d:%d len %d key ",
o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
o_znode->zbranch[o_n].len);
*zn = o_znode;
*n = o_n;
return 1;
}
/**
* matches_position - determine if a zbranch matches a given position.
* @zbr: zbranch of dent
* @lnum: LEB number of dent to match
* @offs: offset of dent to match
*
* This function returns %1 if @lnum:@offs matches, and %0 otherwise.
*/
static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
{
if (zbr->lnum == lnum && zbr->offs == offs)
return 1;