Commit | Line | Data |
---|---|---|
243bfe19 JA |
1 | /* |
2 | * Really simple exclusive file locking based on filename. | |
3 | * No hash indexing, just a list, so only works well for < 100 files or | |
4 | * so. But that's more than what fio needs, so should be fine. | |
5 | */ | |
6 | #include <inttypes.h> | |
7 | #include <string.h> | |
529016bc | 8 | #include <unistd.h> |
243bfe19 JA |
9 | #include <assert.h> |
10 | ||
11 | #include "flist.h" | |
12 | #include "filelock.h" | |
13 | #include "smalloc.h" | |
971caeb1 | 14 | #include "fio_sem.h" |
243bfe19 JA |
15 | #include "hash.h" |
16 | #include "log.h" | |
17 | ||
18 | struct fio_filelock { | |
19 | uint32_t hash; | |
971caeb1 | 20 | struct fio_sem lock; |
243bfe19 JA |
21 | struct flist_head list; |
22 | unsigned int references; | |
23 | }; | |
529016bc | 24 | |
43661a66 | 25 | #define MAX_FILELOCKS 1024 |
243bfe19 | 26 | |
529016bc JA |
27 | static struct filelock_data { |
28 | struct flist_head list; | |
971caeb1 | 29 | struct fio_sem lock; |
529016bc JA |
30 | |
31 | struct flist_head free_list; | |
32 | struct fio_filelock ffs[MAX_FILELOCKS]; | |
33 | } *fld; | |
34 | ||
35 | static void put_filelock(struct fio_filelock *ff) | |
36 | { | |
37 | flist_add(&ff->list, &fld->free_list); | |
38 | } | |
39 | ||
40 | static struct fio_filelock *__get_filelock(void) | |
41 | { | |
42 | struct fio_filelock *ff; | |
43 | ||
44 | if (flist_empty(&fld->free_list)) | |
45 | return NULL; | |
46 | ||
47 | ff = flist_first_entry(&fld->free_list, struct fio_filelock, list); | |
48 | flist_del_init(&ff->list); | |
49 | return ff; | |
50 | } | |
51 | ||
52 | static struct fio_filelock *get_filelock(int trylock, int *retry) | |
53 | { | |
54 | struct fio_filelock *ff; | |
55 | ||
56 | do { | |
57 | ff = __get_filelock(); | |
58 | if (ff || trylock) | |
59 | break; | |
60 | ||
971caeb1 | 61 | fio_sem_up(&fld->lock); |
529016bc | 62 | usleep(1000); |
971caeb1 | 63 | fio_sem_down(&fld->lock); |
529016bc JA |
64 | *retry = 1; |
65 | } while (1); | |
66 | ||
67 | return ff; | |
68 | } | |
243bfe19 JA |
69 | |
70 | int fio_filelock_init(void) | |
71 | { | |
529016bc | 72 | int i; |
243bfe19 | 73 | |
529016bc JA |
74 | fld = smalloc(sizeof(*fld)); |
75 | if (!fld) | |
243bfe19 | 76 | return 1; |
529016bc JA |
77 | |
78 | INIT_FLIST_HEAD(&fld->list); | |
79 | INIT_FLIST_HEAD(&fld->free_list); | |
80 | ||
971caeb1 | 81 | if (__fio_sem_init(&fld->lock, FIO_SEM_UNLOCKED)) |
529016bc JA |
82 | goto err; |
83 | ||
84 | for (i = 0; i < MAX_FILELOCKS; i++) { | |
85 | struct fio_filelock *ff = &fld->ffs[i]; | |
86 | ||
971caeb1 | 87 | if (__fio_sem_init(&ff->lock, FIO_SEM_UNLOCKED)) |
529016bc JA |
88 | goto err; |
89 | flist_add_tail(&ff->list, &fld->free_list); | |
243bfe19 JA |
90 | } |
91 | ||
92 | return 0; | |
529016bc JA |
93 | err: |
94 | fio_filelock_exit(); | |
95 | return 1; | |
243bfe19 JA |
96 | } |
97 | ||
98 | void fio_filelock_exit(void) | |
99 | { | |
529016bc | 100 | if (!fld) |
243bfe19 JA |
101 | return; |
102 | ||
529016bc | 103 | assert(flist_empty(&fld->list)); |
971caeb1 | 104 | __fio_sem_remove(&fld->lock); |
529016bc JA |
105 | |
106 | while (!flist_empty(&fld->free_list)) { | |
107 | struct fio_filelock *ff; | |
108 | ||
109 | ff = flist_first_entry(&fld->free_list, struct fio_filelock, list); | |
110 | ||
111 | flist_del_init(&ff->list); | |
971caeb1 | 112 | __fio_sem_remove(&ff->lock); |
529016bc JA |
113 | } |
114 | ||
115 | sfree(fld); | |
116 | fld = NULL; | |
243bfe19 JA |
117 | } |
118 | ||
119 | static struct fio_filelock *fio_hash_find(uint32_t hash) | |
120 | { | |
121 | struct flist_head *entry; | |
122 | struct fio_filelock *ff; | |
123 | ||
529016bc | 124 | flist_for_each(entry, &fld->list) { |
243bfe19 JA |
125 | ff = flist_entry(entry, struct fio_filelock, list); |
126 | if (ff->hash == hash) | |
127 | return ff; | |
128 | } | |
129 | ||
130 | return NULL; | |
131 | } | |
132 | ||
529016bc | 133 | static struct fio_filelock *fio_hash_get(uint32_t hash, int trylock) |
243bfe19 JA |
134 | { |
135 | struct fio_filelock *ff; | |
136 | ||
137 | ff = fio_hash_find(hash); | |
138 | if (!ff) { | |
529016bc JA |
139 | int retry = 0; |
140 | ||
141 | ff = get_filelock(trylock, &retry); | |
142 | if (!ff) | |
143 | return NULL; | |
144 | ||
145 | /* | |
146 | * If we dropped the main lock, re-lookup the hash in case | |
147 | * someone else added it meanwhile. If it's now there, | |
148 | * just return that. | |
149 | */ | |
150 | if (retry) { | |
151 | struct fio_filelock *__ff; | |
152 | ||
153 | __ff = fio_hash_find(hash); | |
154 | if (__ff) { | |
155 | put_filelock(ff); | |
156 | return __ff; | |
157 | } | |
158 | } | |
159 | ||
243bfe19 | 160 | ff->hash = hash; |
243bfe19 | 161 | ff->references = 0; |
529016bc | 162 | flist_add(&ff->list, &fld->list); |
243bfe19 JA |
163 | } |
164 | ||
165 | return ff; | |
166 | } | |
167 | ||
375ac4fa | 168 | static bool __fio_lock_file(const char *fname, int trylock) |
243bfe19 JA |
169 | { |
170 | struct fio_filelock *ff; | |
171 | uint32_t hash; | |
172 | ||
173 | hash = jhash(fname, strlen(fname), 0); | |
174 | ||
971caeb1 | 175 | fio_sem_down(&fld->lock); |
529016bc JA |
176 | ff = fio_hash_get(hash, trylock); |
177 | if (ff) | |
178 | ff->references++; | |
971caeb1 | 179 | fio_sem_up(&fld->lock); |
529016bc JA |
180 | |
181 | if (!ff) { | |
77fa4987 | 182 | assert(trylock); |
375ac4fa | 183 | return true; |
529016bc JA |
184 | } |
185 | ||
186 | if (!trylock) { | |
971caeb1 | 187 | fio_sem_down(&ff->lock); |
375ac4fa | 188 | return false; |
529016bc | 189 | } |
243bfe19 | 190 | |
971caeb1 | 191 | if (!fio_sem_down_trylock(&ff->lock)) |
375ac4fa | 192 | return false; |
243bfe19 | 193 | |
971caeb1 | 194 | fio_sem_down(&fld->lock); |
243bfe19 JA |
195 | |
196 | /* | |
197 | * If we raced and the only reference to the lock is us, we can | |
198 | * grab it | |
199 | */ | |
200 | if (ff->references != 1) { | |
201 | ff->references--; | |
202 | ff = NULL; | |
203 | } | |
204 | ||
971caeb1 | 205 | fio_sem_up(&fld->lock); |
243bfe19 JA |
206 | |
207 | if (ff) { | |
971caeb1 | 208 | fio_sem_down(&ff->lock); |
375ac4fa | 209 | return false; |
243bfe19 JA |
210 | } |
211 | ||
375ac4fa | 212 | return true; |
243bfe19 JA |
213 | } |
214 | ||
375ac4fa | 215 | bool fio_trylock_file(const char *fname) |
243bfe19 | 216 | { |
529016bc JA |
217 | return __fio_lock_file(fname, 1); |
218 | } | |
243bfe19 | 219 | |
529016bc JA |
220 | void fio_lock_file(const char *fname) |
221 | { | |
222 | __fio_lock_file(fname, 0); | |
243bfe19 JA |
223 | } |
224 | ||
225 | void fio_unlock_file(const char *fname) | |
226 | { | |
227 | struct fio_filelock *ff; | |
228 | uint32_t hash; | |
229 | ||
230 | hash = jhash(fname, strlen(fname), 0); | |
231 | ||
971caeb1 | 232 | fio_sem_down(&fld->lock); |
243bfe19 JA |
233 | |
234 | ff = fio_hash_find(hash); | |
235 | if (ff) { | |
f5a42524 | 236 | int refs = --ff->references; |
971caeb1 | 237 | fio_sem_up(&ff->lock); |
f5a42524 | 238 | if (!refs) { |
529016bc JA |
239 | flist_del_init(&ff->list); |
240 | put_filelock(ff); | |
243bfe19 JA |
241 | } |
242 | } else | |
243 | log_err("fio: file not found for unlocking\n"); | |
244 | ||
971caeb1 | 245 | fio_sem_up(&fld->lock); |
243bfe19 | 246 | } |