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
|
#include <camellia/errno.h>
#include <camellia/flags.h>
#include <kernel/handleset.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
HandleSet *
hs_init(void)
{
HandleSet *hs = kzalloc(sizeof *hs, "handles");
hs->refcount = 1;
return hs;
}
HandleSet *
hs_copy(HandleSet *from)
{
HandleSet *hs = hs_init();
assert(from);
for (hid_t i = 0; i < HANDLE_MAX; i++) {
hs->h[i] = from->h[i];
if (hs->h[i]) {
hs->h[i]->refcount++;
}
}
return hs;
}
void
hs_unref(HandleSet *hs)
{
assert(hs);
assert(hs->refcount != 0);
hs->refcount--;
if (hs->refcount == 0) {
for (hid_t i = 0; i < HANDLE_MAX; i++) {
hs_close(hs, i);
}
kfree(hs);
}
}
hid_t
hs_findfree(HandleSet *hs, hid_t start)
{
if (start < 0) start = 0;
for (hid_t i = start; i < HANDLE_MAX; i++) {
if (hs->h[i] == NULL) {
return i;
}
}
return -1;
}
Handle *
hs_get(HandleSet *hs, hid_t id)
{
if (0 <= id && id < HANDLE_MAX) {
return hs->h[id];
} else {
return NULL;
}
}
hid_t
hs_hinit(HandleSet *hs, enum handle_type type, Handle **hp)
{
hid_t hid = hs_findfree(hs, 1);
if (hid < 0) return -1;
hs->h[hid] = handle_init(type);
if (hp) *hp = hs->h[hid];
return hid;
}
hid_t
hs_dup(HandleSet *hs, hid_t from, hid_t to, int flags)
{
Handle *fromh, **toh;
if (to < 0 || (flags & DUP_SEARCH)) {
to = hs_findfree(hs, to);
if (to < 0) return -EMFILE;
} else if (to >= HANDLE_MAX) {
return -EBADF;
}
if (to == from) return to;
toh = &hs->h[to];
fromh = hs_get(hs, from);
if (*toh) handle_close(*toh);
if (flags & (DUP_RDONLY | DUP_WRONLY)) {
if (!fromh || fromh->type != HANDLE_FILE) {
/* only errors out after closing the old handle */
*toh = NULL;
return -ENOSYS;
}
/* figure out if we need a need handle */
bool different = false;
different = different || ((flags & DUP_RDONLY) && fromh->writeable);
different = different || ((flags & DUP_WRONLY) && fromh->readable);
if (different) {
Handle *new = handle_init(HANDLE_FILE);
new->backend = fromh->backend;
if (new->backend) new->backend->usehcnt++;
new->file_id = fromh->file_id;
new->readable = fromh->readable && !(flags & DUP_WRONLY);
new->writeable = fromh->writeable && !(flags & DUP_RDONLY);
new->base = fromh;
fromh->refcount++;
*toh = new;
return to;
}
}
*toh = fromh;
if (fromh) fromh->refcount++;
return to;
}
Handle *
hs_take(HandleSet *hs, hid_t hid)
{
if (hid < 0 || hid >= HANDLE_MAX) {
return NULL;
}
Handle *h = hs->h[hid];
hs->h[hid] = NULL;
return h;
}
hid_t
hs_put(HandleSet *hs, Handle *h)
{
assert(h);
hid_t hid = hs_findfree(hs, 1);
if (hid < 0) {
handle_close(h);
return hid;
}
hs->h[hid] = h;
return hid;
}
|