-
Notifications
You must be signed in to change notification settings - Fork 3
/
open_file.c
208 lines (178 loc) · 4.21 KB
/
open_file.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
/*
* $Id$
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <gfarm/gfarm.h>
#include <pthread.h>
#include <assert.h>
#include "gfarm2fs_msg_enums.h"
#include "gfarm2fs.h"
#include "hash.h"
struct opening {
struct opening *next;
struct gfarm2fs_file *fp;
int writing;
};
struct inode_openings {
struct opening *openings;
struct gfarm2fs_file *fp_cached;
};
static struct gfarm_hash_table *open_file_table;
#define OPEN_FILE_TABLE_SIZE 256
static int open_file_hash(const void *k, int l)
{
gfarm_ino_t h = *(gfarm_ino_t *)k;
int i = (int)h;
return (i);
}
static int open_file_hash_equal(
const void *k1, int k1len, const void *k2, int k2len)
{
gfarm_ino_t h1 = *(gfarm_ino_t *)k1, h2 = *(gfarm_ino_t *)k2;
return (h1 == h2);
}
static pthread_rwlock_t open_file_table_rwlock;
static void
open_file_table_lock_init(void)
{
int rv;
rv = pthread_rwlock_init(&open_file_table_rwlock, NULL);
assert(rv == 0);
}
void
gfarm2fs_open_file_table_rdlock(void)
{
int rv;
rv = pthread_rwlock_rdlock(&open_file_table_rwlock);
assert(rv == 0);
}
void
gfarm2fs_open_file_table_wrlock(void)
{
int rv;
rv = pthread_rwlock_wrlock(&open_file_table_rwlock);
assert(rv == 0);
}
void
gfarm2fs_open_file_table_unlock(void)
{
int rv;
rv = pthread_rwlock_unlock(&open_file_table_rwlock);
assert(rv == 0);
}
void
gfarm2fs_open_file_init()
{
open_file_table = gfarm_hash_table_alloc(
OPEN_FILE_TABLE_SIZE, open_file_hash, open_file_hash_equal);
if (open_file_table == NULL)
gflog_fatal(GFARM_MSG_2000051, "no memory");
open_file_table_lock_init();
}
struct gfarm2fs_file *
gfarm2fs_open_file_lookup_unlocked(gfarm_ino_t ino)
{
struct gfarm_hash_entry *entry;
struct inode_openings *ios;
struct opening *o;
struct gfarm2fs_file *rv = NULL;
entry = gfarm_hash_lookup(open_file_table, &ino, sizeof(ino));
if (entry == NULL)
goto finish;
ios = gfarm_hash_entry_data(entry);
if (ios->fp_cached != NULL) {
rv = ios->fp_cached;
goto finish;
}
for (o = ios->openings; o != NULL; o = o->next) {
if (o->writing) {
ios->fp_cached = o->fp;
rv = ios->fp_cached;
goto finish;
}
}
ios->fp_cached = ios->openings->fp;
rv = ios->fp_cached;
finish:
return (rv);
}
void
gfarm2fs_open_file_enter(struct gfarm2fs_file *fp, int flags)
{
gfarm_ino_t ino = fp->inum;
struct gfarm_hash_entry *entry;
struct inode_openings *ios;
struct opening *o;
int created;
o = malloc(sizeof(*o));
if (o == NULL) {
gflog_error(GFARM_MSG_2000053,
"no memory to cache an opening for inode %lld",
(unsigned long long)ino);
return;
}
gfarm2fs_open_file_table_wrlock();
entry = gfarm_hash_enter(open_file_table, &ino, sizeof(ino),
sizeof(*ios), &created);
if (entry == NULL) {
gflog_error(GFARM_MSG_2000054,
"no memory to insert inode %lld to open file table",
(unsigned long long)ino);
gfarm2fs_open_file_table_unlock();
return;
}
o->fp = fp;
o->writing =
((flags & O_TRUNC) != 0 || (flags & O_ACCMODE) != O_RDONLY);
ios = gfarm_hash_entry_data(entry);
if (!created) {
o->next = ios->openings;
} else {
o->next = NULL;
ios->fp_cached = NULL;
}
ios->openings = o;
if (o->writing)
ios->fp_cached = fp;
gfarm2fs_open_file_table_unlock();
}
static int
open_file_remove_opening(struct inode_openings *ios, struct gfarm2fs_file *fp)
{
struct opening *o, **prev;
for (prev = &ios->openings; (o = *prev) != NULL; prev = &o->next) {
if (o->fp == fp)
break;
}
if (o == NULL)
return (1);
*prev = o->next;
free(o);
return (0);
}
void
gfarm2fs_open_file_remove_unlocked(struct gfarm2fs_file *fp)
{
gfarm_ino_t ino = fp->inum;
struct gfarm_hash_entry *entry;
struct inode_openings *ios = NULL;
entry = gfarm_hash_lookup(open_file_table, &ino, sizeof(ino));
if (entry == NULL) {
gflog_warning(GFARM_MSG_2000056,
"inode %lld is not found in open file table",
(unsigned long long)ino);
return;
}
ios = gfarm_hash_entry_data(entry);
if (open_file_remove_opening(ios, fp) != 0)
gflog_warning(GFARM_MSG_2000057,
"file %p is not found in the inode %lld openings",
fp, (unsigned long long)ino);
if (ios->fp_cached == fp)
ios->fp_cached = NULL;
if (ios->openings == NULL)
(void)gfarm_hash_purge(open_file_table, &ino, sizeof(ino));
}