Fix misspellings using 'codespell' tool
[fio.git] / filehash.c
CommitLineData
380065aa
JA
1#include <stdlib.h>
2#include <assert.h>
3
4#include "fio.h"
01743ee1 5#include "flist.h"
dadf66c5 6#include "hash.h"
380065aa
JA
7
8#define HASH_BUCKETS 512
9#define HASH_MASK (HASH_BUCKETS - 1)
10
01743ee1 11unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
380065aa 12
01743ee1 13static struct flist_head *file_hash;
b950781e 14static struct fio_mutex *hash_lock;
380065aa 15
380065aa
JA
16static unsigned short hash(const char *name)
17{
dadf66c5 18 return jhash(name, strlen(name), 0) & HASH_MASK;
380065aa
JA
19}
20
21void remove_file_hash(struct fio_file *f)
22{
b950781e 23 fio_mutex_down(hash_lock);
380065aa 24
d6aed795 25 if (fio_file_hashed(f)) {
01743ee1
JA
26 assert(!flist_empty(&f->hash_list));
27 flist_del_init(&f->hash_list);
d6aed795 28 fio_file_clear_hashed(f);
380065aa
JA
29 }
30
b950781e 31 fio_mutex_up(hash_lock);
380065aa
JA
32}
33
34static struct fio_file *__lookup_file_hash(const char *name)
35{
01743ee1
JA
36 struct flist_head *bucket = &file_hash[hash(name)];
37 struct flist_head *n;
380065aa 38
01743ee1
JA
39 flist_for_each(n, bucket) {
40 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
380065aa 41
89541106
JA
42 if (!f->file_name)
43 continue;
44
380065aa
JA
45 if (!strcmp(f->file_name, name)) {
46 assert(f->fd != -1);
47 return f;
48 }
49 }
50
380065aa
JA
51 return NULL;
52}
53
54struct fio_file *lookup_file_hash(const char *name)
55{
56 struct fio_file *f;
57
b950781e 58 fio_mutex_down(hash_lock);
380065aa 59 f = __lookup_file_hash(name);
b950781e 60 fio_mutex_up(hash_lock);
380065aa
JA
61 return f;
62}
63
64struct fio_file *add_file_hash(struct fio_file *f)
65{
66 struct fio_file *alias;
67
d6aed795 68 if (fio_file_hashed(f))
380065aa
JA
69 return NULL;
70
01743ee1 71 INIT_FLIST_HEAD(&f->hash_list);
380065aa 72
b950781e 73 fio_mutex_down(hash_lock);
380065aa
JA
74
75 alias = __lookup_file_hash(f->file_name);
76 if (!alias) {
d6aed795 77 fio_file_set_hashed(f);
01743ee1 78 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
380065aa
JA
79 }
80
b950781e 81 fio_mutex_up(hash_lock);
380065aa
JA
82 return alias;
83}
84
5e1d306e
JA
85void file_hash_exit(void)
86{
87 unsigned int i, has_entries = 0;
88
b950781e 89 fio_mutex_down(hash_lock);
5e1d306e 90 for (i = 0; i < HASH_BUCKETS; i++)
01743ee1 91 has_entries += !flist_empty(&file_hash[i]);
b950781e 92 fio_mutex_up(hash_lock);
5e1d306e
JA
93
94 if (has_entries)
95 log_err("fio: file hash not empty on exit\n");
96
b950781e
JA
97 file_hash = NULL;
98 fio_mutex_remove(hash_lock);
5e1d306e
JA
99 hash_lock = NULL;
100}
101
380065aa
JA
102void file_hash_init(void *ptr)
103{
104 unsigned int i;
105
106 file_hash = ptr;
107 for (i = 0; i < HASH_BUCKETS; i++)
01743ee1 108 INIT_FLIST_HEAD(&file_hash[i]);
380065aa 109
521da527 110 hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
380065aa 111}