Skip to content

Commit a69e939

Browse files
Ming Leikawasaki
authored andcommitted
selftests: ublk: add utils.h
Signed-off-by: Ming Lei <[email protected]>
1 parent ce5d31e commit a69e939

2 files changed

Lines changed: 72 additions & 62 deletions

File tree

tools/testing/selftests/ublk/kublk.h

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@
2929
#include "ublk_dep.h"
3030
#include <linux/ublk_cmd.h>
3131

32-
#define __maybe_unused __attribute__((unused))
33-
#define MAX_BACK_FILES 4
34-
#ifndef min
35-
#define min(a, b) ((a) < (b) ? (a) : (b))
36-
#endif
32+
#include "utils.h"
3733

38-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
34+
#define MAX_BACK_FILES 4
3935

4036
/****************** part 1: libublk ********************/
4137

@@ -52,13 +48,6 @@
5248
#define UBLK_MAX_THREADS (1 << UBLK_MAX_THREADS_SHIFT)
5349
#define UBLK_QUEUE_DEPTH 1024
5450

55-
#define UBLK_DBG_DEV (1U << 0)
56-
#define UBLK_DBG_THREAD (1U << 1)
57-
#define UBLK_DBG_IO_CMD (1U << 2)
58-
#define UBLK_DBG_IO (1U << 3)
59-
#define UBLK_DBG_CTRL_CMD (1U << 4)
60-
#define UBLK_LOG (1U << 5)
61-
6251
struct ublk_dev;
6352
struct ublk_queue;
6453
struct ublk_thread;
@@ -211,21 +200,6 @@ struct ublk_dev {
211200
void *private_data;
212201
};
213202

214-
#ifndef offsetof
215-
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
216-
#endif
217-
218-
#ifndef container_of
219-
#define container_of(ptr, type, member) ({ \
220-
unsigned long __mptr = (unsigned long)(ptr); \
221-
((type *)(__mptr - offsetof(type, member))); })
222-
#endif
223-
224-
#define round_up(val, rnd) \
225-
(((val) + ((rnd) - 1)) & ~((rnd) - 1))
226-
227-
228-
extern unsigned int ublk_dbg_mask;
229203
extern int ublk_queue_io_cmd(struct ublk_thread *t, struct ublk_io *io);
230204

231205

@@ -275,34 +249,6 @@ static inline unsigned short ublk_cmd_op_nr(unsigned int op)
275249
return _IOC_NR(op);
276250
}
277251

278-
static inline void ublk_err(const char *fmt, ...)
279-
{
280-
va_list ap;
281-
282-
va_start(ap, fmt);
283-
vfprintf(stderr, fmt, ap);
284-
}
285-
286-
static inline void ublk_log(const char *fmt, ...)
287-
{
288-
if (ublk_dbg_mask & UBLK_LOG) {
289-
va_list ap;
290-
291-
va_start(ap, fmt);
292-
vfprintf(stdout, fmt, ap);
293-
}
294-
}
295-
296-
static inline void ublk_dbg(int level, const char *fmt, ...)
297-
{
298-
if (level & ublk_dbg_mask) {
299-
va_list ap;
300-
301-
va_start(ap, fmt);
302-
vfprintf(stdout, fmt, ap);
303-
}
304-
}
305-
306252
static inline struct ublk_queue *ublk_io_to_queue(const struct ublk_io *io)
307253
{
308254
return container_of(io, struct ublk_queue, ios[io->tag]);
@@ -458,10 +404,4 @@ extern const struct ublk_tgt_ops fault_inject_tgt_ops;
458404
void backing_file_tgt_deinit(struct ublk_dev *dev);
459405
int backing_file_tgt_init(struct ublk_dev *dev);
460406

461-
static inline unsigned int ilog2(unsigned int x)
462-
{
463-
if (x == 0)
464-
return 0;
465-
return (sizeof(x) * 8 - 1) - __builtin_clz(x);
466-
}
467407
#endif
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef KUBLK_UTILS_H
3+
#define KUBLK_UTILS_H
4+
5+
#define __maybe_unused __attribute__((unused))
6+
7+
#ifndef min
8+
#define min(a, b) ((a) < (b) ? (a) : (b))
9+
#endif
10+
11+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
12+
13+
#ifndef offsetof
14+
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
15+
#endif
16+
17+
#ifndef container_of
18+
#define container_of(ptr, type, member) ({ \
19+
unsigned long __mptr = (unsigned long)(ptr); \
20+
((type *)(__mptr - offsetof(type, member))); })
21+
#endif
22+
23+
#define round_up(val, rnd) \
24+
(((val) + ((rnd) - 1)) & ~((rnd) - 1))
25+
26+
static inline unsigned int ilog2(unsigned int x)
27+
{
28+
if (x == 0)
29+
return 0;
30+
return (sizeof(x) * 8 - 1) - __builtin_clz(x);
31+
}
32+
33+
#define UBLK_DBG_DEV (1U << 0)
34+
#define UBLK_DBG_THREAD (1U << 1)
35+
#define UBLK_DBG_IO_CMD (1U << 2)
36+
#define UBLK_DBG_IO (1U << 3)
37+
#define UBLK_DBG_CTRL_CMD (1U << 4)
38+
#define UBLK_LOG (1U << 5)
39+
40+
extern unsigned int ublk_dbg_mask;
41+
42+
static inline void ublk_err(const char *fmt, ...)
43+
{
44+
va_list ap;
45+
46+
va_start(ap, fmt);
47+
vfprintf(stderr, fmt, ap);
48+
}
49+
50+
static inline void ublk_log(const char *fmt, ...)
51+
{
52+
if (ublk_dbg_mask & UBLK_LOG) {
53+
va_list ap;
54+
55+
va_start(ap, fmt);
56+
vfprintf(stdout, fmt, ap);
57+
}
58+
}
59+
60+
static inline void ublk_dbg(int level, const char *fmt, ...)
61+
{
62+
if (level & ublk_dbg_mask) {
63+
va_list ap;
64+
65+
va_start(ap, fmt);
66+
vfprintf(stdout, fmt, ap);
67+
}
68+
}
69+
70+
#endif

0 commit comments

Comments
 (0)