2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include <arpa/inet.h>
29 #include <shared/macro.h>
30 #include <shared/strbuf.h>
31 #include <shared/util.h>
33 #include "libkmod-internal.h"
34 #include "libkmod-index.h"
36 /* libkmod-index.c: module index file implementation
38 * Integers are stored as 32 bit unsigned in "network" order, i.e. MSB first.
39 * All files start with a magic number.
41 * Magic spells "BOOTFAST". Second one used on newer versioned binary files.
42 * #define INDEX_MAGIC_OLD 0xB007FA57
44 * We use a version string to keep track of changes to the binary format
45 * This is stored in the form: INDEX_MAJOR (hi) INDEX_MINOR (lo) just in
46 * case we ever decide to have minor changes that are not incompatible.
48 #define INDEX_MAGIC 0xB007F457
49 #define INDEX_VERSION_MAJOR 0x0002
50 #define INDEX_VERSION_MINOR 0x0001
51 #define INDEX_VERSION ((INDEX_VERSION_MAJOR<<16)|INDEX_VERSION_MINOR)
53 /* The index file maps keys to values. Both keys and values are ASCII strings.
54 * Each key can have multiple values. Values are sorted by an integer priority.
56 * The reader also implements a wildcard search (including range expressions)
57 * where the keys in the index are treated as patterns.
58 * This feature is required for module aliases.
60 #define INDEX_CHILDMAX 128
64 * uint32_t magic = INDEX_MAGIC;
65 * uint32_t version = INDEX_VERSION;
66 * uint32_t root_offset;
68 * (node_offset & INDEX_NODE_MASK) specifies the file offset of nodes:
70 * char[] prefix; // nul terminated
74 * uint32_t children[last - first + 1];
76 * uint32_t value_count;
79 * char[] value; // nul terminated
80 * } values[value_count];
82 * (node_offset & INDEX_NODE_FLAGS) indicates which fields are present.
83 * Empty prefixes are omitted, leaf nodes omit the three child-related fields.
85 * This could be optimised further by adding a sparse child format
86 * (indicated using a new flag).
89 * Implementation is based on a radix tree, or "trie".
90 * Each arc from parent to child is labelled with a character.
91 * Each path from the root represents a string.
93 * == Example strings ==
103 * * Marked node, representing a key and it's values.
114 * Naive implementations tend to be very space inefficient; child pointers
115 * are stored in arrays indexed by character, but most child pointers are null.
117 * Our implementation uses a scheme described by Wikipedia as a Patrica trie,
119 * "easiest to understand as a space-optimized trie where
120 * each node with only one child is merged with its child"
131 * We still use arrays of child pointers indexed by a single character;
132 * the remaining characters of the label are stored as a "prefix" in the child.
134 * The paper describing the original Patrica trie works on individiual bits -
135 * each node has a maximum of two children, which increases space efficiency.
136 * However for this application it is simpler to use the ASCII character set.
137 * Since the index file is read-only, it can be compressed by omitting null
138 * child pointers at the start and end of arrays.
141 /* Format of node offsets within index file */
143 INDEX_NODE_FLAGS
= 0xF0000000, /* Flags in high nibble */
144 INDEX_NODE_PREFIX
= 0x80000000,
145 INDEX_NODE_VALUES
= 0x40000000,
146 INDEX_NODE_CHILDS
= 0x20000000,
148 INDEX_NODE_MASK
= 0x0FFFFFFF, /* Offset value */
151 void index_values_free(struct index_value
*values
)
154 struct index_value
*value
= values
;
156 values
= value
->next
;
161 static int add_value(struct index_value
**values
,
162 const char *value
, unsigned len
, unsigned int priority
)
164 struct index_value
*v
;
166 /* find position to insert value */
167 while (*values
&& (*values
)->priority
< priority
)
168 values
= &(*values
)->next
;
170 v
= malloc(sizeof(struct index_value
) + len
+ 1);
174 v
->priority
= priority
;
176 memcpy(v
->value
, value
, len
);
177 v
->value
[len
] = '\0';
183 static void read_error(void)
185 fatal("Module index: unexpected error: %s\n"
186 "Try re-running depmod\n", errno
? strerror(errno
) : "EOF");
189 static int read_char(FILE *in
)
194 ch
= getc_unlocked(in
);
200 static uint32_t read_long(FILE *in
)
205 if (fread(&l
, sizeof(uint32_t), 1, in
) != sizeof(uint32_t))
210 static unsigned buf_freadchars(struct strbuf
*buf
, FILE *in
)
215 while ((ch
= read_char(in
))) {
216 if (!strbuf_pushchar(buf
, ch
))
225 * Index file searching
227 struct index_node_f
{
229 char *prefix
; /* path compression */
230 struct index_value
*values
;
231 unsigned char first
; /* range of child nodes */
233 uint32_t children
[0];
236 static struct index_node_f
*index_read(FILE *in
, uint32_t offset
)
238 struct index_node_f
*node
;
240 int i
, child_count
= 0;
242 if ((offset
& INDEX_NODE_MASK
) == 0)
245 fseek(in
, offset
& INDEX_NODE_MASK
, SEEK_SET
);
247 if (offset
& INDEX_NODE_PREFIX
) {
250 buf_freadchars(&buf
, in
);
251 prefix
= strbuf_steal(&buf
);
253 prefix
= NOFAIL(strdup(""));
255 if (offset
& INDEX_NODE_CHILDS
) {
256 char first
= read_char(in
);
257 char last
= read_char(in
);
258 child_count
= last
- first
+ 1;
260 node
= NOFAIL(malloc(sizeof(struct index_node_f
) +
261 sizeof(uint32_t) * child_count
));
266 for (i
= 0; i
< child_count
; i
++)
267 node
->children
[i
] = read_long(in
);
269 node
= NOFAIL(malloc(sizeof(struct index_node_f
)));
270 node
->first
= INDEX_CHILDMAX
;
275 if (offset
& INDEX_NODE_VALUES
) {
279 unsigned int priority
;
281 value_count
= read_long(in
);
284 while (value_count
--) {
285 priority
= read_long(in
);
286 buf_freadchars(&buf
, in
);
287 value
= strbuf_str(&buf
);
288 add_value(&node
->values
, value
, buf
.used
, priority
);
291 strbuf_release(&buf
);
294 node
->prefix
= prefix
;
299 static void index_close(struct index_node_f
*node
)
302 index_values_free(node
->values
);
308 uint32_t root_offset
;
311 struct index_file
*index_file_open(const char *filename
)
314 uint32_t magic
, version
;
315 struct index_file
*new;
317 file
= fopen(filename
, "re");
322 magic
= read_long(file
);
323 if (magic
!= INDEX_MAGIC
) {
328 version
= read_long(file
);
329 if (version
>> 16 != INDEX_VERSION_MAJOR
) {
334 new = NOFAIL(malloc(sizeof(struct index_file
)));
336 new->root_offset
= read_long(new->file
);
342 void index_file_close(struct index_file
*idx
)
348 static struct index_node_f
*index_readroot(struct index_file
*in
)
350 return index_read(in
->file
, in
->root_offset
);
353 static struct index_node_f
*index_readchild(const struct index_node_f
*parent
,
356 if (parent
->first
<= ch
&& ch
<= parent
->last
) {
357 return index_read(parent
->file
,
358 parent
->children
[ch
- parent
->first
]);
364 static void index_dump_node(struct index_node_f
*node
, struct strbuf
*buf
,
367 struct index_value
*v
;
370 pushed
= strbuf_pushchars(buf
, node
->prefix
);
372 for (v
= node
->values
; v
!= NULL
; v
= v
->next
) {
373 write_str_safe(fd
, buf
->bytes
, buf
->used
);
374 write_str_safe(fd
, " ", 1);
375 write_str_safe(fd
, v
->value
, strlen(v
->value
));
376 write_str_safe(fd
, "\n", 1);
379 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
380 struct index_node_f
*child
= index_readchild(node
, ch
);
385 strbuf_pushchar(buf
, ch
);
386 index_dump_node(child
, buf
, fd
);
390 strbuf_popchars(buf
, pushed
);
394 void index_dump(struct index_file
*in
, int fd
, const char *prefix
)
396 struct index_node_f
*root
;
399 root
= index_readroot(in
);
404 strbuf_pushchars(&buf
, prefix
);
405 index_dump_node(root
, &buf
, fd
);
406 strbuf_release(&buf
);
409 static char *index_search__node(struct index_node_f
*node
, const char *key
, int i
)
412 struct index_node_f
*child
;
417 for (j
= 0; node
->prefix
[j
]; j
++) {
418 ch
= node
->prefix
[j
];
420 if (ch
!= key
[i
+j
]) {
428 if (key
[i
] == '\0') {
429 value
= node
->values
!= NULL
430 ? strdup(node
->values
[0].value
)
437 child
= index_readchild(node
, key
[i
]);
447 * Search the index for a key
449 * Returns the value of the first match
451 * The recursive functions free their node argument (using index_close).
453 char *index_search(struct index_file
*in
, const char *key
)
455 // FIXME: return value by reference instead of strdup
456 struct index_node_f
*root
;
459 root
= index_readroot(in
);
460 value
= index_search__node(root
, key
, 0);
467 /* Level 4: add all the values from a matching node */
468 static void index_searchwild__allvalues(struct index_node_f
*node
,
469 struct index_value
**out
)
471 struct index_value
*v
;
473 for (v
= node
->values
; v
!= NULL
; v
= v
->next
)
474 add_value(out
, v
->value
, v
->len
, v
->priority
);
480 * Level 3: traverse a sub-keyspace which starts with a wildcard,
481 * looking for matches.
483 static void index_searchwild__all(struct index_node_f
*node
, int j
,
486 struct index_value
**out
)
491 while (node
->prefix
[j
]) {
492 ch
= node
->prefix
[j
];
494 strbuf_pushchar(buf
, ch
);
499 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
500 struct index_node_f
*child
= index_readchild(node
, ch
);
505 strbuf_pushchar(buf
, ch
);
506 index_searchwild__all(child
, 0, buf
, subkey
, out
);
511 if (fnmatch(strbuf_str(buf
), subkey
, 0) == 0)
512 index_searchwild__allvalues(node
, out
);
519 strbuf_popchars(buf
, pushed
);
522 /* Level 2: descend the tree (until we hit a wildcard) */
523 static void index_searchwild__node(struct index_node_f
*node
,
525 const char *key
, int i
,
526 struct index_value
**out
)
528 struct index_node_f
*child
;
533 for (j
= 0; node
->prefix
[j
]; j
++) {
534 ch
= node
->prefix
[j
];
536 if (ch
== '*' || ch
== '?' || ch
== '[') {
537 index_searchwild__all(node
, j
, buf
,
542 if (ch
!= key
[i
+j
]) {
550 child
= index_readchild(node
, '*');
552 strbuf_pushchar(buf
, '*');
553 index_searchwild__all(child
, 0, buf
, &key
[i
], out
);
557 child
= index_readchild(node
, '?');
559 strbuf_pushchar(buf
, '?');
560 index_searchwild__all(child
, 0, buf
, &key
[i
], out
);
564 child
= index_readchild(node
, '[');
566 strbuf_pushchar(buf
, '[');
567 index_searchwild__all(child
, 0, buf
, &key
[i
], out
);
571 if (key
[i
] == '\0') {
572 index_searchwild__allvalues(node
, out
);
577 child
= index_readchild(node
, key
[i
]);
585 * Search the index for a key. The index may contain wildcards.
587 * Returns a list of all the values of matching keys.
589 struct index_value
*index_searchwild(struct index_file
*in
, const char *key
)
591 struct index_node_f
*root
= index_readroot(in
);
593 struct index_value
*out
= NULL
;
596 index_searchwild__node(root
, &buf
, key
, 0, &out
);
597 strbuf_release(&buf
);
601 /**************************************************************************/
603 * Alternative implementation, using mmap to map all the file to memory when
606 #include <sys/mman.h>
607 #include <sys/stat.h>
610 static const char _idx_empty_str
[] = "";
613 struct kmod_ctx
*ctx
;
615 uint32_t root_offset
;
619 struct index_mm_value
{
620 unsigned int priority
;
625 struct index_mm_value_array
{
626 struct index_mm_value
*values
;
630 struct index_mm_node
{
631 struct index_mm
*idx
;
632 const char *prefix
; /* mmape'd value */
633 struct index_mm_value_array values
;
639 static inline uint32_t read_long_mm(void **p
)
641 uint8_t *addr
= *(uint8_t **)p
;
644 /* addr may be unalined to uint32_t */
645 v
= get_unaligned((uint32_t *) addr
);
647 *p
= addr
+ sizeof(uint32_t);
651 static inline uint8_t read_char_mm(void **p
)
653 uint8_t *addr
= *(uint8_t **)p
;
655 *p
= addr
+ sizeof(uint8_t);
659 static inline char *read_chars_mm(void **p
, unsigned *rlen
)
661 char *addr
= *(char **)p
;
662 size_t len
= *rlen
= strlen(addr
);
667 static struct index_mm_node
*index_mm_read_node(struct index_mm
*idx
,
670 struct index_mm_node
*node
;
672 int i
, child_count
, value_count
, children_padding
;
673 uint32_t children
[INDEX_CHILDMAX
];
676 if ((offset
& INDEX_NODE_MASK
) == 0)
679 p
= (char *)p
+ (offset
& INDEX_NODE_MASK
);
681 if (offset
& INDEX_NODE_PREFIX
) {
683 prefix
= read_chars_mm(&p
, &len
);
685 prefix
= _idx_empty_str
;
687 if (offset
& INDEX_NODE_CHILDS
) {
688 first
= read_char_mm(&p
);
689 last
= read_char_mm(&p
);
690 child_count
= last
- first
+ 1;
691 for (i
= 0; i
< child_count
; i
++)
692 children
[i
] = read_long_mm(&p
);
694 first
= INDEX_CHILDMAX
;
699 children_padding
= (sizeof(struct index_mm_node
) +
700 (sizeof(uint32_t) * child_count
)) % sizeof(void *);
702 if (offset
& INDEX_NODE_VALUES
)
703 value_count
= read_long_mm(&p
);
707 node
= malloc(sizeof(struct index_mm_node
)
708 + sizeof(uint32_t) * child_count
+ children_padding
709 + sizeof(struct index_mm_value
) * value_count
);
714 node
->prefix
= prefix
;
715 if (value_count
== 0)
716 node
->values
.values
= NULL
;
718 node
->values
.values
= (struct index_mm_value
*)
719 ((char *)node
+ sizeof(struct index_mm_node
) +
720 sizeof(uint32_t) * child_count
+ children_padding
);
722 node
->values
.len
= value_count
;
725 memcpy(node
->children
, children
, sizeof(uint32_t) * child_count
);
727 for (i
= 0; i
< value_count
; i
++) {
728 struct index_mm_value
*v
= node
->values
.values
+ i
;
729 v
->priority
= read_long_mm(&p
);
730 v
->value
= read_chars_mm(&p
, &v
->len
);
736 static void index_mm_free_node(struct index_mm_node
*node
)
741 struct index_mm
*index_mm_open(struct kmod_ctx
*ctx
, const char *filename
,
742 unsigned long long *stamp
)
746 struct index_mm
*idx
;
750 uint32_t root_offset
;
754 DBG(ctx
, "file=%s\n", filename
);
756 idx
= malloc(sizeof(*idx
));
758 ERR(ctx
, "malloc: %m\n");
762 if ((fd
= open(filename
, O_RDONLY
|O_CLOEXEC
)) < 0) {
763 DBG(ctx
, "open(%s, O_RDONLY|O_CLOEXEC): %m\n", filename
);
767 if (fstat(fd
, &st
) < 0)
769 if ((size_t) st
.st_size
< sizeof(hdr
))
772 if ((idx
->mm
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0))
774 ERR(ctx
, "mmap(NULL, %"PRIu64
", PROT_READ, %d, MAP_PRIVATE, 0): %m\n",
780 hdr
.magic
= read_long_mm(&p
);
781 hdr
.version
= read_long_mm(&p
);
782 hdr
.root_offset
= read_long_mm(&p
);
784 if (hdr
.magic
!= INDEX_MAGIC
) {
785 ERR(ctx
, "magic check fail: %x instead of %x\n", hdr
.magic
,
790 if (hdr
.version
>> 16 != INDEX_VERSION_MAJOR
) {
791 ERR(ctx
, "major version check fail: %u instead of %u\n",
792 hdr
.version
>> 16, INDEX_VERSION_MAJOR
);
796 idx
->root_offset
= hdr
.root_offset
;
797 idx
->size
= st
.st_size
;
801 *stamp
= stat_mstamp(&st
);
806 munmap(idx
->mm
, st
.st_size
);
814 void index_mm_close(struct index_mm
*idx
)
816 munmap(idx
->mm
, idx
->size
);
820 static struct index_mm_node
*index_mm_readroot(struct index_mm
*idx
)
822 return index_mm_read_node(idx
, idx
->root_offset
);
825 static struct index_mm_node
*index_mm_readchild(const struct index_mm_node
*parent
,
828 if (parent
->first
<= ch
&& ch
<= parent
->last
) {
829 return index_mm_read_node(parent
->idx
,
830 parent
->children
[ch
- parent
->first
]);
836 static void index_mm_dump_node(struct index_mm_node
*node
, struct strbuf
*buf
,
839 struct index_mm_value
*itr
, *itr_end
;
842 pushed
= strbuf_pushchars(buf
, node
->prefix
);
844 itr
= node
->values
.values
;
845 itr_end
= itr
+ node
->values
.len
;
846 for (; itr
< itr_end
; itr
++) {
847 write_str_safe(fd
, buf
->bytes
, buf
->used
);
848 write_str_safe(fd
, " ", 1);
849 write_str_safe(fd
, itr
->value
, itr
->len
);
850 write_str_safe(fd
, "\n", 1);
853 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
854 struct index_mm_node
*child
= index_mm_readchild(node
, ch
);
859 strbuf_pushchar(buf
, ch
);
860 index_mm_dump_node(child
, buf
, fd
);
864 strbuf_popchars(buf
, pushed
);
865 index_mm_free_node(node
);
868 void index_mm_dump(struct index_mm
*idx
, int fd
, const char *prefix
)
870 struct index_mm_node
*root
;
873 root
= index_mm_readroot(idx
);
878 strbuf_pushchars(&buf
, prefix
);
879 index_mm_dump_node(root
, &buf
, fd
);
880 strbuf_release(&buf
);
883 static char *index_mm_search_node(struct index_mm_node
*node
, const char *key
,
887 struct index_mm_node
*child
;
892 for (j
= 0; node
->prefix
[j
]; j
++) {
893 ch
= node
->prefix
[j
];
895 if (ch
!= key
[i
+j
]) {
896 index_mm_free_node(node
);
903 if (key
[i
] == '\0') {
904 value
= node
->values
.len
> 0
905 ? strdup(node
->values
.values
[0].value
)
908 index_mm_free_node(node
);
912 child
= index_mm_readchild(node
, key
[i
]);
913 index_mm_free_node(node
);
922 * Search the index for a key
924 * Returns the value of the first match
926 * The recursive functions free their node argument (using index_close).
928 char *index_mm_search(struct index_mm
*idx
, const char *key
)
930 // FIXME: return value by reference instead of strdup
931 struct index_mm_node
*root
;
934 root
= index_mm_readroot(idx
);
935 value
= index_mm_search_node(root
, key
, 0);
940 /* Level 4: add all the values from a matching node */
941 static void index_mm_searchwild_allvalues(struct index_mm_node
*node
,
942 struct index_value
**out
)
944 struct index_mm_value
*itr
, *itr_end
;
946 itr
= node
->values
.values
;
947 itr_end
= itr
+ node
->values
.len
;
948 for (; itr
< itr_end
; itr
++)
949 add_value(out
, itr
->value
, itr
->len
, itr
->priority
);
951 index_mm_free_node(node
);
955 * Level 3: traverse a sub-keyspace which starts with a wildcard,
956 * looking for matches.
958 static void index_mm_searchwild_all(struct index_mm_node
*node
, int j
,
961 struct index_value
**out
)
966 while (node
->prefix
[j
]) {
967 ch
= node
->prefix
[j
];
969 strbuf_pushchar(buf
, ch
);
974 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
975 struct index_mm_node
*child
= index_mm_readchild(node
, ch
);
980 strbuf_pushchar(buf
, ch
);
981 index_mm_searchwild_all(child
, 0, buf
, subkey
, out
);
985 if (node
->values
.len
> 0) {
986 if (fnmatch(strbuf_str(buf
), subkey
, 0) == 0)
987 index_mm_searchwild_allvalues(node
, out
);
989 index_mm_free_node(node
);
991 index_mm_free_node(node
);
994 strbuf_popchars(buf
, pushed
);
997 /* Level 2: descend the tree (until we hit a wildcard) */
998 static void index_mm_searchwild_node(struct index_mm_node
*node
,
1000 const char *key
, int i
,
1001 struct index_value
**out
)
1003 struct index_mm_node
*child
;
1008 for (j
= 0; node
->prefix
[j
]; j
++) {
1009 ch
= node
->prefix
[j
];
1011 if (ch
== '*' || ch
== '?' || ch
== '[') {
1012 index_mm_searchwild_all(node
, j
, buf
,
1017 if (ch
!= key
[i
+j
]) {
1018 index_mm_free_node(node
);
1025 child
= index_mm_readchild(node
, '*');
1027 strbuf_pushchar(buf
, '*');
1028 index_mm_searchwild_all(child
, 0, buf
, &key
[i
], out
);
1029 strbuf_popchar(buf
);
1032 child
= index_mm_readchild(node
, '?');
1034 strbuf_pushchar(buf
, '?');
1035 index_mm_searchwild_all(child
, 0, buf
, &key
[i
], out
);
1036 strbuf_popchar(buf
);
1039 child
= index_mm_readchild(node
, '[');
1041 strbuf_pushchar(buf
, '[');
1042 index_mm_searchwild_all(child
, 0, buf
, &key
[i
], out
);
1043 strbuf_popchar(buf
);
1046 if (key
[i
] == '\0') {
1047 index_mm_searchwild_allvalues(node
, out
);
1052 child
= index_mm_readchild(node
, key
[i
]);
1053 index_mm_free_node(node
);
1060 * Search the index for a key. The index may contain wildcards.
1062 * Returns a list of all the values of matching keys.
1064 struct index_value
*index_mm_searchwild(struct index_mm
*idx
, const char *key
)
1066 struct index_mm_node
*root
= index_mm_readroot(idx
);
1068 struct index_value
*out
= NULL
;
1071 index_mm_searchwild_node(root
, &buf
, key
, 0, &out
);
1072 strbuf_release(&buf
);