Branch data Line data Source code
1 : : #include <stdio.h>
2 : : #include <stdlib.h>
3 : : #include <stdarg.h>
4 : : #include <signal.h>
5 : : #include <limits.h>
6 : : #include <unistd.h>
7 : : #include <errno.h>
8 : : #include <string.h>
9 : : #include <sys/stat.h>
10 : : #include <sys/types.h>
11 : :
12 : : #include "asm/types.h"
13 : : #include "file-ids.h"
14 : : #include "rbtree.h"
15 : : #include "kcmp-ids.h"
16 : : #include "compiler.h"
17 : : #include "syscall.h"
18 : : #include "image.h"
19 : : #include "util.h"
20 : : #include "irmap.h"
21 : : #include "files.h"
22 : :
23 : : static DECLARE_KCMP_TREE(fd_tree, KCMP_FILE);
24 : :
25 : 448 : void fd_id_show_tree(void)
26 : : {
27 : 448 : kid_show_tree(&fd_tree);
28 : 448 : }
29 : :
30 : : #define FDID_BITS 5
31 : : #define FDID_SIZE (1 << FDID_BITS)
32 : : #define FDID_MASK (FDID_SIZE - 1)
33 : :
34 : : static inline int fdid_hashfn(unsigned int s_dev, unsigned long i_ino)
35 : : {
36 : 17079 : return (s_dev + i_ino) & FDID_MASK;
37 : : }
38 : :
39 : : struct fd_id {
40 : : int mnt_id;
41 : : unsigned int dev;
42 : : unsigned long ino;
43 : : u32 id;
44 : : struct fd_id *n;
45 : : };
46 : :
47 : : static struct fd_id *fd_id_cache[FDID_SIZE];
48 : :
49 : 4516 : static void fd_id_cache_one(u32 id, struct fd_parms *p)
50 : : {
51 : : struct fd_id *fi;
52 : : unsigned hv;
53 : :
54 [ - + ]: 4516 : fi = xmalloc(sizeof(*fi));
55 [ + - ]: 4516 : if (fi) {
56 : 4516 : fi->dev = p->stat.st_dev;
57 : 4516 : fi->ino = p->stat.st_ino;
58 : 4516 : fi->mnt_id = p->mnt_id;
59 : 4516 : fi->id = id;
60 : :
61 : 9032 : hv = fdid_hashfn(p->stat.st_dev, p->stat.st_ino);
62 : 4516 : fi->n = fd_id_cache[hv];
63 : 4516 : fd_id_cache[hv] = fi;
64 : : }
65 : 4516 : }
66 : :
67 : 12563 : static struct fd_id *fd_id_cache_lookup(struct fd_parms *p)
68 : : {
69 : : struct stat *st = &p->stat;
70 : : struct fd_id *fi;
71 : :
72 [ + + ]: 13670 : for (fi = fd_id_cache[fdid_hashfn(st->st_dev, st->st_ino)];
73 : 1107 : fi; fi = fi->n)
74 [ + + ][ + + ]: 11316 : if (fi->dev == st->st_dev &&
75 [ - + ]: 10209 : fi->ino == st->st_ino &&
76 : 10209 : fi->mnt_id == p->mnt_id)
77 : : return fi;
78 : :
79 : : return NULL;
80 : : }
81 : :
82 : 12569 : int fd_id_generate_special(struct fd_parms *p, u32 *id)
83 : : {
84 [ + + ]: 12569 : if (p) {
85 : : struct fd_id *fi;
86 : :
87 : 12563 : fi = fd_id_cache_lookup(p);
88 [ + + ]: 12563 : if (fi) {
89 : 10209 : *id = fi->id;
90 : 10209 : return 0;
91 : : }
92 : : }
93 : :
94 : 2360 : *id = fd_tree.subid++;
95 [ + + ]: 2360 : if (p)
96 : 2354 : fd_id_cache_one(*id, p);
97 : : return 1;
98 : : }
99 : :
100 : 3839 : int fd_id_generate(pid_t pid, FdinfoEntry *fe, struct fd_parms *p)
101 : : {
102 : : u32 id;
103 : : struct kid_elem e;
104 : 3839 : int new_id = 0;
105 : :
106 : 3839 : e.pid = pid;
107 : 3839 : e.genid = fe->id;
108 : 3839 : e.idx = fe->fd;
109 : :
110 : 3839 : id = kid_generate_gen(&fd_tree, &e, &new_id);
111 [ + - ]: 3839 : if (!id)
112 : : return -ENOMEM;
113 : :
114 [ + + ]: 3839 : if (new_id)
115 : 2162 : fd_id_cache_one(id, p);
116 : :
117 : 3839 : fe->id = id;
118 : 3839 : return new_id;
119 : : }
|