-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrofs_decompressor_data_queue.c
194 lines (155 loc) · 4.87 KB
/
microfs_decompressor_data_queue.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
/* microfs - Minimally Improved Compressed Read Only File System
* Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, ..., +%Y
* Erik Edlund <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "microfs.h"
#include <linux/cpumask.h>
struct microfs_decompressor_data_queue {
int dc_avail;
struct mutex dc_mutex;
struct list_head dc_freelist;
struct list_head dc_busylist;
wait_queue_head_t dc_waitqueue;
};
struct microfs_decompressor_data_queue_node {
void* dc_data;
struct list_head dc_list;
};
static inline int microfs_decompressor_data_queue_ceil(void)
{
return num_online_cpus() * 2;
}
static int microfs_decompressor_data_queue_get(struct microfs_sb_info* sbi,
void** data)
{
int err;
struct microfs_decompressor_data_queue_node* node;
struct microfs_decompressor_data_queue* queue = sbi
->si_decompressor_data->dd_private;
BUG_ON(*data != NULL);
while (1) {
mutex_lock(&queue->dc_mutex);
if (!list_empty(&queue->dc_freelist)) {
node = list_entry(queue->dc_freelist.prev, typeof(*node), dc_list);
list_del(&node->dc_list);
goto out;
}
if (queue->dc_avail >= microfs_decompressor_data_queue_ceil())
goto wait;
node = kmalloc(sizeof(*node), GFP_KERNEL);
if (!node) {
goto wait;
}
err = sbi->si_decompressor->dc_create(sbi, &node->dc_data);
if (err) {
kfree(node);
goto wait;
}
WARN_ON(++queue->dc_avail > microfs_decompressor_data_queue_ceil());
out:
*data = node->dc_data;
node->dc_data = NULL;
list_add(&node->dc_list, &queue->dc_busylist);
mutex_unlock(&queue->dc_mutex);
break;
wait:
mutex_unlock(&queue->dc_mutex);
wait_event(queue->dc_waitqueue, !list_empty(&queue->dc_freelist));
continue;
}
return 0;
}
static int microfs_decompressor_data_queue_put(struct microfs_sb_info* sbi,
void** data)
{
struct microfs_decompressor_data_queue_node* node;
struct microfs_decompressor_data_queue* queue = sbi
->si_decompressor_data->dd_private;
mutex_lock(&queue->dc_mutex);
BUG_ON(list_empty(&queue->dc_busylist));
BUG_ON(*data == NULL);
node = list_entry(queue->dc_busylist.prev, typeof(*node), dc_list);
node->dc_data = *data;
list_del(&node->dc_list);
list_add(&node->dc_list, &queue->dc_freelist);
mutex_unlock(&queue->dc_mutex);
wake_up(&queue->dc_waitqueue);
*data = NULL;
return 0;
}
static void microfs_decompressor_data_queue_destroy(struct microfs_sb_info* sbi,
void* data)
{
struct microfs_decompressor_data_queue_node* node;
struct microfs_decompressor_data_queue* queue = data;
if (queue) {
while (!list_empty(&queue->dc_freelist)) {
node = list_entry(queue->dc_freelist.prev, typeof(*node), dc_list);
list_del(&node->dc_list);
WARN_ON(sbi->si_decompressor->dc_destroy(sbi, node->dc_data));
kfree(node);
queue->dc_avail--;
}
WARN_ON(queue->dc_avail);
WARN_ON(!list_empty(&queue->dc_busylist));
kfree(queue);
}
}
int microfs_decompressor_data_queue_create(struct microfs_sb_info* sbi,
struct microfs_decompressor_data* data)
{
int err = 0;
struct microfs_decompressor_data_queue* queue = NULL;
struct microfs_decompressor_data_queue_node* node = NULL;
queue = kmalloc(sizeof(*queue), GFP_KERNEL);
if (!queue) {
pr_err("microfs_decompressor_queue_create:"
" failed to allocate the decompressor queue");
err = -ENOMEM;
goto err_mem_queue;
}
INIT_LIST_HEAD(&queue->dc_freelist);
INIT_LIST_HEAD(&queue->dc_busylist);
mutex_init(&queue->dc_mutex);
init_waitqueue_head(&queue->dc_waitqueue);
node = kmalloc(sizeof(*node), GFP_KERNEL);
if (!node) {
pr_err("microfs_decompressor_queue_create:"
" failed to allocate the decompressor list node");
err = -ENOMEM;
goto err_mem_node;
}
err = sbi->si_decompressor->dc_create(sbi, &node->dc_data);
if (err) {
pr_err("microfs_decompressor_queue_create:"
" failed to create the decompressor for the list node");
goto err_create;
}
queue->dc_avail = 1;
list_add(&node->dc_list, &queue->dc_freelist);
data->dd_private = queue;
data->dd_get = microfs_decompressor_data_queue_get;
data->dd_put = microfs_decompressor_data_queue_put;
data->dd_destroy = microfs_decompressor_data_queue_destroy;
return 0;
err_create:
kfree(node);
err_mem_node:
kfree(queue);
err_mem_queue:
return err;
}