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#include <linux/module.h>
28#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/sched.h>
31#include <linux/parser.h>
32#include <linux/idr.h>
33#include <net/9p/9p.h>
34
35
36
37
38
39
40
41
42struct p9_idpool {
43 spinlock_t lock;
44 struct idr pool;
45};
46
47
48
49
50
51
52struct p9_idpool *p9_idpool_create(void)
53{
54 struct p9_idpool *p;
55
56 p = kmalloc(sizeof(struct p9_idpool), GFP_KERNEL);
57 if (!p)
58 return ERR_PTR(-ENOMEM);
59
60 spin_lock_init(&p->lock);
61 idr_init(&p->pool);
62
63 return p;
64}
65EXPORT_SYMBOL(p9_idpool_create);
66
67
68
69
70
71
72void p9_idpool_destroy(struct p9_idpool *p)
73{
74 idr_destroy(&p->pool);
75 kfree(p);
76}
77EXPORT_SYMBOL(p9_idpool_destroy);
78
79
80
81
82
83
84
85
86
87int p9_idpool_get(struct p9_idpool *p)
88{
89 int i = 0;
90 int error;
91 unsigned long flags;
92
93retry:
94 if (idr_pre_get(&p->pool, GFP_KERNEL) == 0)
95 return 0;
96
97 spin_lock_irqsave(&p->lock, flags);
98
99
100 error = idr_get_new(&p->pool, p, &i);
101 spin_unlock_irqrestore(&p->lock, flags);
102
103 if (error == -EAGAIN)
104 goto retry;
105 else if (error)
106 return -1;
107
108 P9_DPRINTK(P9_DEBUG_MUX, " id %d pool %p\n", i, p);
109 return i;
110}
111EXPORT_SYMBOL(p9_idpool_get);
112
113
114
115
116
117
118
119
120
121
122void p9_idpool_put(int id, struct p9_idpool *p)
123{
124 unsigned long flags;
125
126 P9_DPRINTK(P9_DEBUG_MUX, " id %d pool %p\n", id, p);
127
128 spin_lock_irqsave(&p->lock, flags);
129 idr_remove(&p->pool, id);
130 spin_unlock_irqrestore(&p->lock, flags);
131}
132EXPORT_SYMBOL(p9_idpool_put);
133
134
135
136
137
138
139
140int p9_idpool_check(int id, struct p9_idpool *p)
141{
142 return idr_find(&p->pool, id) != NULL;
143}
144EXPORT_SYMBOL(p9_idpool_check);
145
146