1 // SPDX-License-Identifier: MIT or LGPL-2.1-only
2
3 #ifndef UBLKSRV_UTILS_INC_H
4 #define UBLKSRV_UTILS_INC_H
5
6 #include <syslog.h>
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <sys/syscall.h>
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
ublksrv_gettid(void)15 static inline int ublksrv_gettid(void)
16 {
17 return syscall(SYS_gettid);
18 }
19
20 /* The following two are obsolete, use new ublk_err/ublk_dbg/ublk_log */
21 static inline void ublksrv_log(int priority, const char *fmt, ...)
22 __attribute__ ((format (printf, 2, 3)));
23 static inline void ublksrv_printf(FILE *stream, const char *fmt, ...)
24 __attribute__ ((format (printf, 2, 3)));
25
26 #ifdef DEBUG
ublksrv_log(int priority,const char * fmt,...)27 static inline void ublksrv_log(int priority, const char *fmt, ...)
28 {
29 va_list ap;
30
31 va_start(ap, fmt);
32 vsyslog(priority, fmt, ap);
33 }
34
ublksrv_printf(FILE * stream,const char * fmt,...)35 static inline void ublksrv_printf(FILE *stream, const char *fmt, ...)
36 {
37 va_list ap;
38
39 va_start(ap, fmt);
40 vfprintf(stream, fmt, ap);
41 }
42 #else
ublksrv_log(int priority,const char * fmt,...)43 static inline void ublksrv_log(int priority, const char *fmt, ...) { }
ublksrv_printf(FILE * stream,const char * fmt,...)44 static inline void ublksrv_printf(FILE *stream, const char *fmt, ...) {}
45 #endif
46
47 /* 32bit debug mask, high 16 bits are for target code, and low 16 bits for lib */
48 #define UBLK_DBG_DEV (1U << 0)
49 #define UBLK_DBG_QUEUE (1U << 1)
50 #define UBLK_DBG_IO_CMD (1U << 2)
51 #define UBLK_DBG_IO (1U << 3)
52 #define UBLK_DBG_CTRL_CMD (1U << 4)
53
54 #ifdef DEBUG
55 extern void ublk_dbg(int level, const char *fmt, ...)
56 __attribute__ ((format (printf, 2, 3)));
57 extern void ublk_ctrl_dbg(int level, const char *fmt, ...)
58 __attribute__ ((format (printf, 2, 3)));
59 extern void ublk_set_debug_mask(unsigned mask);
60 extern unsigned ublk_get_debug_mask(unsigned mask);
61 #else
ublk_dbg(int level,const char * fmt,...)62 static inline void ublk_dbg(int level, const char *fmt, ...) { }
ublk_ctrl_dbg(int level,const char * fmt,...)63 static inline void ublk_ctrl_dbg(int level, const char *fmt, ...) { }
ublk_set_debug_mask(unsigned mask)64 static inline void ublk_set_debug_mask(unsigned mask) {}
ublk_get_debug_mask(unsigned mask)65 static inline unsigned ublk_get_debug_mask(unsigned mask) { return 0;}
66 #endif
67
68 extern void ublk_log(const char *fmt, ...)
69 __attribute__ ((format (printf, 1, 2)));
70 extern void ublk_err(const char *fmt, ...)
71 __attribute__ ((format (printf, 1, 2)));
72
73 #define round_up(val, rnd) \
74 (((val) + ((rnd) - 1)) & ~((rnd) - 1))
75
76 #ifndef offsetof
77 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
78 #endif
79
80 #ifndef container_of
81 #define container_of(ptr, type, member) ({ \
82 unsigned long __mptr = (unsigned long)(ptr); \
83 ((type *)(__mptr - offsetof(type, member))); })
84 #endif
85
86 #define ublk_ignore_result(x) ({ typeof(x) z = x; (void)sizeof z; })
87
88 #ifdef __cplusplus
89 }
90 #endif
91
92 #endif
93