aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/block_processor/frontend.c
blob: 8bd6cf2080ffe26797a6ab9215efa5cf11c836f6 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * frontend.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "internal.h"

static sqfs_block_t *get_new_block(sqfs_block_processor_t *proc)
{
	sqfs_block_t *blk;

	if (proc->free_list != NULL) {
		blk = proc->free_list;
		proc->free_list = blk->next;
	} else {
		blk = malloc(sizeof(*blk) + proc->max_block_size);
	}

	if (blk != NULL)
		memset(blk, 0, sizeof(*blk));

	return blk;
}

static int dequeue_block(sqfs_block_processor_t *proc)
{
	sqfs_block_t *blk, *fragblk, *it, *prev;
	bool have_dequeued = false;
	int status;
retry:
	while (proc->io_queue != NULL) {
		if (proc->io_queue->io_seq_num != proc->io_deq_seq_num)
			break;

		blk = proc->io_queue;
		proc->io_queue = blk->next;
		proc->io_deq_seq_num += 1;
		proc->backlog -= 1;
		have_dequeued = true;

		status = process_completed_block(proc, blk);
		if (status != 0)
			return status;
	}

	if (have_dequeued)
		return 0;

	blk = proc->pool->dequeue(proc->pool);

	if (blk == NULL) {
		status = proc->pool->get_status(proc->pool);
		if (status == 0)
			status = SQFS_ERROR_INTERNAL;

		return status;
	}

	proc->backlog -= 1;
	have_dequeued = true;

	if (blk->flags & SQFS_BLK_IS_FRAGMENT) {
		fragblk = NULL;
		status = process_completed_fragment(proc, blk, &fragblk);

		if (status != 0) {
			free(fragblk);
			return status;
		}

		if (fragblk != NULL) {
			fragblk->io_seq_num = proc->io_seq_num++;

			if (proc->pool->submit(proc->pool, fragblk) != 0) {
				free(fragblk);

				if (status == 0) {
					status = proc->pool->
						get_status(proc->pool);

					if (status == 0)
						status = SQFS_ERROR_ALLOC;
				}

				return status;
			}

			proc->backlog += 1;
			have_dequeued = false;
		}
	} else {
		if (!(blk->flags & SQFS_BLK_FRAGMENT_BLOCK))
			blk->io_seq_num = proc->io_seq_num++;

		prev = NULL;
		it = proc->io_queue;

		while (it != NULL) {
			if (it->io_seq_num >= blk->io_seq_num)
				break;

			prev = it;
			it = it->next;
		}

		if (prev == NULL) {
			blk->next = proc->io_queue;
			proc->io_queue = blk;
		} else {
			blk->next = prev->next;
			prev->next = blk;
		}

		proc->backlog += 1;
		have_dequeued = false;
	}

	if (!have_dequeued)
		goto retry;

	return 0;
}

static int enqueue_block(sqfs_block_processor_t *proc, sqfs_block_t *blk)
{
	int status;

	if (proc->pool->submit(proc->pool, blk) != 0) {
		status = proc->pool->get_status(proc->pool);

		if (status == 0)
			status = SQFS_ERROR_ALLOC;

		free(blk);
		return status;
	}

	proc->backlog += 1;
	return 0;
}

static int add_sentinel_block(sqfs_block_processor_t *proc)
{
	sqfs_block_t *blk;
	int ret;

	if (proc->backlog == proc->max_backlog) {
		ret = dequeue_block(proc);
		if (ret != 0)
			return ret;
	}

	blk = get_new_block(proc);
	if (blk == NULL)
		return SQFS_ERROR_ALLOC;

	blk->inode = proc->inode;
	blk->flags = proc->blk_flags | SQFS_BLK_LAST_BLOCK;

	return enqueue_block(proc, blk);
}

static int flush_block(sqfs_block_processor_t *proc)
{
	sqfs_block_t *block;
	int ret;

	if (proc->backlog == proc->max_backlog) {
		ret = dequeue_block(proc);
		if (ret != 0)
			return ret;
	}

	block = proc->blk_current;
	proc->blk_current = NULL;

	return enqueue_block(proc, block);
}

int sqfs_block_processor_begin_file(sqfs_block_processor_t *proc,
				    sqfs_inode_generic_t **inode,
				    void *user, sqfs_u32 flags)
{
	if (proc->begin_called)
		return SQFS_ERROR_SEQUENCE;

	if (flags & ~SQFS_BLK_USER_SETTABLE_FLAGS)
		return SQFS_ERROR_UNSUPPORTED;

	if (inode != NULL) {
		(*inode) = calloc(1, sizeof(sqfs_inode_generic_t));
		if ((*inode) == NULL)
			return SQFS_ERROR_ALLOC;

		(*inode)->base.type = SQFS_INODE_FILE;
		sqfs_inode_set_frag_location(*inode, 0xFFFFFFFF, 0xFFFFFFFF);
	}

	proc->begin_called = true;
	proc->inode = inode;
	proc->blk_flags = flags | SQFS_BLK_FIRST_BLOCK;
	proc->blk_index = 0;
	proc->user = user;
	return 0;
}

int sqfs_block_processor_append(sqfs_block_processor_t *proc, const void *data,
				size_t size)
{
	sqfs_block_t *new;
	sqfs_u64 filesize;
	size_t diff;
	int err;

	if (!proc->begin_called)
		return SQFS_ERROR_SEQUENCE;

	if (proc->inode != NULL) {
		sqfs_inode_get_file_size(*(proc->inode), &filesize);
		sqfs_inode_set_file_size(*(proc->inode), filesize + size);
	}

	while (size > 0) {
		if (proc->blk_current == NULL) {
			new = get_new_block(proc);
			if (new == NULL)
				return SQFS_ERROR_ALLOC;

			proc->blk_current = new;
			proc->blk_current->flags = proc->blk_flags;
			proc->blk_current->inode = proc->inode;
			proc->blk_current->user = proc->user;
			proc->blk_current->index = proc->blk_index++;
			proc->blk_flags &= ~SQFS_BLK_FIRST_BLOCK;
		}

		diff = proc->max_block_size - proc->blk_current->size;

		if (diff == 0) {
			err = flush_block(proc);
			if (err)
				return err;
			continue;
		}

		if (diff > size)
			diff = size;

		memcpy(proc->blk_current->data + proc->blk_current->size,
		       data, diff);

		size -= diff;
		proc->blk_current->size += diff;
		data = (const char *)data + diff;

		proc->stats.input_bytes_read += diff;
	}

	if (proc->blk_current != NULL &&
	    proc->blk_current->size == proc->max_block_size) {
		return flush_block(proc);
	}

	return 0;
}

int sqfs_block_processor_end_file(sqfs_block_processor_t *proc)
{
	int err;

	if (!proc->begin_called)
		return SQFS_ERROR_SEQUENCE;

	if (proc->blk_current == NULL) {
		if (!(proc->blk_flags & SQFS_BLK_FIRST_BLOCK)) {
			err = add_sentinel_block(proc);
			if (err)
				return err;
		}
	} else {
		if (proc->blk_flags & SQFS_BLK_DONT_FRAGMENT) {
			proc->blk_current->flags |= SQFS_BLK_LAST_BLOCK;
		} else {
			if (!(proc->blk_current->flags &
			      SQFS_BLK_FIRST_BLOCK)) {
				err = add_sentinel_block(proc);
				if (err)
					return err;
			}

			proc->blk_current->flags |= SQFS_BLK_IS_FRAGMENT;
		}

		err = flush_block(proc);
		if (err)
			return err;
	}

	proc->begin_called = false;
	proc->inode = NULL;
	proc->user = NULL;
	proc->blk_flags = 0;
	return 0;
}

int sqfs_block_processor_submit_block(sqfs_block_processor_t *proc, void *user,
				      sqfs_u32 flags, const void *data,
				      size_t size)
{
	sqfs_block_t *blk;
	int ret;

	if (proc->begin_called)
		return SQFS_ERROR_SEQUENCE;

	if (size > proc->max_block_size)
		return SQFS_ERROR_OVERFLOW;

	if (flags & ~SQFS_BLK_FLAGS_ALL)
		return SQFS_ERROR_UNSUPPORTED;

	if (proc->backlog == proc->max_backlog) {
		ret = dequeue_block(proc);
		if (ret != 0)
			return ret;
	}

	blk = get_new_block(proc);
	if (blk == NULL)
		return SQFS_ERROR_ALLOC;

	blk->flags = flags | BLK_FLAG_MANUAL_SUBMISSION;
	blk->user = user;
	blk->size = size;
	memcpy(blk->data, data, size);

	ret = proc->pool->submit(proc->pool, blk);
	if (ret != 0)
		free(blk);

	proc->backlog += 1;
	return ret;
}

int sqfs_block_processor_sync(sqfs_block_processor_t *proc)
{
	int ret;

	while (proc->backlog > 0) {
		ret = dequeue_block(proc);
		if (ret != 0)
			return ret;
	}

	return 0;
}

int sqfs_block_processor_finish(sqfs_block_processor_t *proc)
{
	sqfs_block_t *blk;
	int status;

	status = sqfs_block_processor_sync(proc);
	if (status != 0)
		return status;

	if (proc->frag_block != NULL) {
		blk = proc->frag_block;
		blk->next = NULL;
		proc->frag_block = NULL;

		blk->io_seq_num = proc->io_seq_num++;

		status = proc->pool->submit(proc->pool, blk);
		if (status != 0) {
			status = proc->pool->get_status(proc->pool);

			if (status == 0)
				status = SQFS_ERROR_ALLOC;

			free(blk);
			return status;
		}

		proc->backlog += 1;
		status = sqfs_block_processor_sync(proc);
	}

	return status;
}

const sqfs_block_processor_stats_t
*sqfs_block_processor_get_stats(const sqfs_block_processor_t *proc)
{
	return &proc->stats;
}