summaryrefslogtreecommitdiffstats
path: root/src/kernel/xloop_file_fmt.c
blob: 6c4902f1d95a3528e50dc3850cae0e4378c06da2 (plain) (blame)
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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * xloop_file_fmt.c
 *
 * File format subsystem for the xloop device module.
 *
 * Copyright (C) 2019 Manuel Bentele <development@manuel-bentele.de>
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kernel.h>
#include <linux/module.h>

#include "xloop_file_fmt.h"
#include "xloop_main.h"

/* storage for all registered file format drivers */
static struct xloop_file_fmt_driver *xloop_file_fmt_drivers[MAX_XLO_FILE_FMT] = {
	NULL
};

int xloop_file_fmt_register_driver(struct xloop_file_fmt_driver *drv)
{
	int ret = 0;

	if (drv == NULL)
		return -EFAULT;

	if (drv->file_fmt_type > MAX_XLO_FILE_FMT)
		return -EINVAL;

	if (xloop_file_fmt_drivers[drv->file_fmt_type] == NULL) {
		xloop_file_fmt_drivers[drv->file_fmt_type] = drv;
		pr_info("successfully registered file format driver %s\n", drv->name);
	} else {
		pr_warn("driver for file format already registered\n");
		ret = -EBUSY;
	}

	return ret;
}
EXPORT_SYMBOL(xloop_file_fmt_register_driver);

void xloop_file_fmt_unregister_driver(struct xloop_file_fmt_driver *drv)
{
	if (drv == NULL)
		return;

	if (drv->file_fmt_type > MAX_XLO_FILE_FMT)
		return;

	xloop_file_fmt_drivers[drv->file_fmt_type] = NULL;
	pr_info("successfully unregistered file format driver %s\n", drv->name);
}
EXPORT_SYMBOL(xloop_file_fmt_unregister_driver);

struct xloop_file_fmt *xloop_file_fmt_alloc(void)
{
	return kzalloc(sizeof(struct xloop_file_fmt), GFP_KERNEL);
}

void xloop_file_fmt_free(struct xloop_file_fmt *xlo_fmt)
{
	kfree(xlo_fmt);
}

int xloop_file_fmt_set_xlo(struct xloop_file_fmt *xlo_fmt, struct xloop_device *xlo)
{
	if (xlo_fmt == NULL)
		return -EINVAL;

	xlo_fmt->xlo = xlo;

	return 0;
}
EXPORT_SYMBOL(xloop_file_fmt_set_xlo);

struct xloop_device *xloop_file_fmt_get_xlo(struct xloop_file_fmt *xlo_fmt)
{
	return xlo_fmt->xlo;
}
EXPORT_SYMBOL(xloop_file_fmt_get_xlo);

struct device *xloop_file_fmt_to_dev(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
	return xloop_device_to_dev(xlo);
}
EXPORT_SYMBOL(xloop_file_fmt_to_dev);

int xloop_file_fmt_init(struct xloop_file_fmt *xlo_fmt,
		       u32 file_fmt_type)
{
	struct xloop_file_fmt_ops *ops;
	struct module *drv;
	int ret = 0;

	if (file_fmt_type > MAX_XLO_FILE_FMT)
		return -EINVAL;

	xlo_fmt->file_fmt_type = file_fmt_type;

	if (xlo_fmt->file_fmt_state != file_fmt_uninitialized) {
		dev_warn(xloop_file_fmt_to_dev(xlo_fmt), "file format is "
			"initialized already\n");
		return -EINVAL;
	}

	/* check if new file format driver is registered */
	if (xloop_file_fmt_drivers[xlo_fmt->file_fmt_type] == NULL) {
		dev_err(xloop_file_fmt_to_dev(xlo_fmt), "file format driver is "
			"not available\n");
		return -ENODEV;
	}

	dev_info(xloop_file_fmt_to_dev(xlo_fmt), "use file format driver %s\n",
		xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->name);

	drv = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->owner;
	if (!try_module_get(drv)) {
		dev_err(xloop_file_fmt_to_dev(xlo_fmt), "file format driver %s can "
			"not be accessed\n",
			xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->name);
		return -ENODEV;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->init)) {
		ret = ops->init(xlo_fmt);
		if (ret < 0)
			goto free_drv;
	}

	/* after increasing the refcount of file format driver module and
	 * the successful initialization, the file format is initialized */
	xlo_fmt->file_fmt_state = file_fmt_initialized;

	return ret;

free_drv:
	module_put(drv);
	xlo_fmt->file_fmt_state = file_fmt_uninitialized;
	return ret;
}

void xloop_file_fmt_exit(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_ops *ops;
	struct module *drv;

	if (xlo_fmt->file_fmt_state != file_fmt_initialized) {
		dev_warn(xloop_file_fmt_to_dev(xlo_fmt), "file format is "
		"uninitialized already\n");
		return;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->exit))
		ops->exit(xlo_fmt);

	drv = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->owner;
	module_put(drv);

	/* after decreasing the refcount of file format driver module,
	 * the file format is uninitialized */
	xlo_fmt->file_fmt_state = file_fmt_uninitialized;
}

int xloop_file_fmt_read(struct xloop_file_fmt *xlo_fmt,
		       struct request *rq)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not read\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->read))
		return ops->read(xlo_fmt, rq);
	else
		return -EIO;
}

int xloop_file_fmt_read_aio(struct xloop_file_fmt *xlo_fmt,
			   struct request *rq)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not read aio\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->read_aio))
		return ops->read_aio(xlo_fmt, rq);
	else
		return -EIO;
}

int xloop_file_fmt_write(struct xloop_file_fmt *xlo_fmt,
			struct request *rq)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not write\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->write))
		return ops->write(xlo_fmt, rq);
	else
		return -EIO;
}

int xloop_file_fmt_write_aio(struct xloop_file_fmt *xlo_fmt,
			    struct request *rq)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not write aio\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->write_aio))
		return ops->write_aio(xlo_fmt, rq);
	else
		return -EIO;
}

int xloop_file_fmt_write_zeros(struct xloop_file_fmt *xlo_fmt,
			  struct request *rq)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not write zeros\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->write_zeros))
		return ops->write_zeros(xlo_fmt, rq);
	else
		return -EIO;
}

int xloop_file_fmt_discard(struct xloop_file_fmt *xlo_fmt,
			  struct request *rq)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not discard\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->discard))
		return ops->discard(xlo_fmt, rq);
	else
		return -EIO;
}

int xloop_file_fmt_flush(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not flush\n");
		return -EINVAL;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->flush))
		return ops->flush(xlo_fmt);

	return 0;
}

loff_t xloop_file_fmt_sector_size(struct xloop_file_fmt *xlo_fmt,
			struct file *file, loff_t offset, loff_t sizelimit)
{
	struct xloop_file_fmt_ops *ops;

	if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) {
		dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format "
			"is not initialized, can not read sector size\n");
		return 0;
	}

	ops = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->ops;
	if (likely(ops->sector_size))
		return ops->sector_size(xlo_fmt, file, offset, sizelimit);
	else
		return 0;
}

int xloop_file_fmt_change(struct xloop_file_fmt *xlo_fmt,
			 u32 file_fmt_type_new)
{
	if (file_fmt_type_new > MAX_XLO_FILE_FMT)
		return -EINVAL;

	dev_info(xloop_file_fmt_to_dev(xlo_fmt), "change file format\n");

	/* Unload the old file format driver if the file format is
	 * initialized */
	if (xlo_fmt->file_fmt_state == file_fmt_initialized)
		xloop_file_fmt_exit(xlo_fmt);

	/* Load the new file format driver because the file format is
	 * uninitialized now */
	return xloop_file_fmt_init(xlo_fmt, file_fmt_type_new);
}

ssize_t xloop_file_fmt_print_type(u32 file_fmt_type, char *file_fmt_name)
{
	ssize_t len = 0;

	switch (file_fmt_type) {
	case XLO_FILE_FMT_RAW:
		len = sprintf(file_fmt_name, "%s", "RAW");
		break;
	case XLO_FILE_FMT_QCOW:
		len = sprintf(file_fmt_name, "%s", "QCOW");
		break;
	case XLO_FILE_FMT_VDI:
		len = sprintf(file_fmt_name, "%s", "VDI");
		break;
	case XLO_FILE_FMT_VMDK:
		len = sprintf(file_fmt_name, "%s", "VMDK");
		break;
	default:
		len = sprintf(file_fmt_name, "%s", "ERROR: Unsupported xloop "
			"file format!");
		break;
	}

	return len;
}
EXPORT_SYMBOL(xloop_file_fmt_print_type);