2 * ossp-util - OSS Proxy: Common utilities
4 * Copyright (C) 2008-2010 SUSE Linux Products GmbH
5 * Copyright (C) 2008-2010 Tejun Heo <tj@kernel.org>
7 * This file is released under the GPLv2.
16 #include <sys/types.h>
21 #define OSSP_LOG_NAME_LEN 128
28 OSSP_LOG_DFL = OSSP_LOG_INFO, /* default log level */
31 OSSP_LOG_MAX = OSSP_LOG_DBG1,
34 extern char ossp_log_name[OSSP_LOG_NAME_LEN];
35 extern int ossp_log_level;
36 extern int ossp_log_timestamp;
38 #define BITS_PER_BYTE 8
39 #define BITS_PER_LONG (BITS_PER_BYTE * sizeof(long))
40 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
41 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
43 /* ARRAY_SIZE and min/max macros stolen from linux/kernel.h */
44 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
46 #define min(x, y) ({ \
47 typeof(x) _min1 = (x); \
48 typeof(y) _min2 = (y); \
49 (void) (&_min1 == &_min2); \
50 _min1 < _min2 ? _min1 : _min2; })
52 #define max(x, y) ({ \
53 typeof(x) _max1 = (x); \
54 typeof(y) _max2 = (y); \
55 (void) (&_max1 == &_max2); \
56 _max1 > _max2 ? _max1 : _max2; })
58 #define min_t(type, x, y) ({ \
61 __min1 < __min2 ? __min1: __min2; })
63 #define max_t(type, x, y) ({ \
66 __max1 > __max2 ? __max1: __max2; })
68 void log_msg(int severity, const char *fmt, ...)
69 __attribute__ ((format (printf, 2, 3)));
71 #define fatal(fmt, args...) do { \
72 log_msg(OSSP_LOG_CRIT, fmt , ##args); \
75 #define err(fmt, args...) log_msg(OSSP_LOG_ERR, fmt , ##args)
76 #define warn(fmt, args...) log_msg(OSSP_LOG_WARN, fmt , ##args)
77 #define info(fmt, args...) log_msg(OSSP_LOG_INFO, fmt , ##args)
78 #define dbg0(fmt, args...) log_msg(OSSP_LOG_DBG0, fmt , ##args)
79 #define dbg1(fmt, args...) log_msg(OSSP_LOG_DBG1, fmt , ##args)
81 #define fatal_e(e, fmt, args...) \
82 fatal(fmt" (%s)" , ##args, strerror(-(e)))
83 #define err_e(e, fmt, args...) \
84 err(fmt" (%s)" , ##args, strerror(-(e)))
85 #define warn_e(e, fmt, args...) \
86 warn(fmt" (%s)" , ##args, strerror(-(e)))
87 #define info_e(e, fmt, args...) \
88 info(fmt" (%s)" , ##args, strerror(-(e)))
89 #define dbg0_e(e, fmt, args...) \
90 dbg0(fmt" (%s)" , ##args, strerror(-(e)))
91 #define dbg1_e(e, fmt, args...) \
92 dbg1(fmt" (%s)" , ##args, strerror(-(e)))
101 static inline size_t ring_size(struct ring_buf *ring)
106 static inline size_t ring_bytes(struct ring_buf *ring)
111 static inline size_t ring_space(struct ring_buf *ring)
113 return ring->size - ring->bytes;
116 static inline void ring_consume(struct ring_buf *ring, size_t size)
118 assert(ring->bytes >= size);
122 static inline void ring_manual_init(struct ring_buf *ring, void *buf,
123 size_t size, size_t head, size_t bytes)
131 void ring_fill(struct ring_buf *ring, const void *buf, size_t size);
132 void *ring_data(struct ring_buf *ring, size_t *sizep);
133 int ring_resize(struct ring_buf *ring, size_t new_size);
140 int ensure_sbuf_size(struct sized_buf *sbuf, size_t size);
142 int read_fill(int fd, void *buf, size_t size);
143 int write_fill(int fd, const void *buf, size_t size);
146 * Bitops lifted from linux asm-generic implementation.
148 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned
149 long size, unsigned long offset);
150 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
151 extern void __set_bit(int nr, volatile unsigned long *addr);
152 extern void __clear_bit(int nr, volatile unsigned long *addr);
154 typedef ssize_t (*ossp_action_fn_t)(enum ossp_opcode opcode,
155 void *carg, void *din, size_t din_sz,
156 void *rarg, void *dout, size_t *dout_szp,
159 int get_proc_self_info(pid_t tid, pid_t *pgrp,
160 char *cmd_buf, size_t cmd_buf_sz);
163 * Doubly linked list handling code shamelessly stolen from the Linux
164 * kernel 2.6.26 include/linux/list.h.
168 * container_of - cast a member of a structure out to the containing structure
169 * @ptr: the pointer to the member.
170 * @type: the type of the container struct this is embedded in.
171 * @member: the name of the member within the struct.
174 #define container_of(ptr, type, member) ({ \
175 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
176 (type *)( (char *)__mptr - offsetof(type,member) );})
178 #define LIST_POISON1 ((void *) 0x00100100)
179 #define LIST_POISON2 ((void *) 0x00200200)
182 * Simple doubly linked list implementation.
184 * Some of the internal functions ("__xxx") are useful when
185 * manipulating whole lists rather than single entries, as
186 * sometimes we already know the next/prev entries and we can
187 * generate better code by using them directly rather than
188 * using the generic single-entry routines.
192 struct list_head *next, *prev;
195 #define LIST_HEAD_INIT(name) { &(name), &(name) }
197 #define LIST_HEAD(name) \
198 struct list_head name = LIST_HEAD_INIT(name)
200 static inline void INIT_LIST_HEAD(struct list_head *list)
207 * Insert a new entry between two known consecutive entries.
209 * This is only for internal list manipulation where we know
210 * the prev/next entries already!
212 static inline void __list_add(struct list_head *new,
213 struct list_head *prev,
214 struct list_head *next)
223 * list_add - add a new entry
224 * @new: new entry to be added
225 * @head: list head to add it after
227 * Insert a new entry after the specified head.
228 * This is good for implementing stacks.
230 static inline void list_add(struct list_head *new, struct list_head *head)
232 __list_add(new, head, head->next);
236 * list_add_tail - add a new entry
237 * @new: new entry to be added
238 * @head: list head to add it before
240 * Insert a new entry before the specified head.
241 * This is useful for implementing queues.
243 static inline void list_add_tail(struct list_head *new, struct list_head *head)
245 __list_add(new, head->prev, head);
249 * Delete a list entry by making the prev/next entries
250 * point to each other.
252 * This is only for internal list manipulation where we know
253 * the prev/next entries already!
255 static inline void __list_del(struct list_head * prev, struct list_head * next)
262 * list_del - deletes entry from list.
263 * @entry: the element to delete from the list.
264 * Note: list_empty() on entry does not return true after this, the entry is
265 * in an undefined state.
267 static inline void list_del(struct list_head *entry)
269 __list_del(entry->prev, entry->next);
270 entry->next = LIST_POISON1;
271 entry->prev = LIST_POISON2;
275 * list_replace - replace old entry by new one
276 * @old : the element to be replaced
277 * @new : the new element to insert
279 * If @old was empty, it will be overwritten.
281 static inline void list_replace(struct list_head *old,
282 struct list_head *new)
284 new->next = old->next;
285 new->next->prev = new;
286 new->prev = old->prev;
287 new->prev->next = new;
290 static inline void list_replace_init(struct list_head *old,
291 struct list_head *new)
293 list_replace(old, new);
298 * list_del_init - deletes entry from list and reinitialize it.
299 * @entry: the element to delete from the list.
301 static inline void list_del_init(struct list_head *entry)
303 __list_del(entry->prev, entry->next);
304 INIT_LIST_HEAD(entry);
308 * list_move - delete from one list and add as another's head
309 * @list: the entry to move
310 * @head: the head that will precede our entry
312 static inline void list_move(struct list_head *list, struct list_head *head)
314 __list_del(list->prev, list->next);
315 list_add(list, head);
319 * list_move_tail - delete from one list and add as another's tail
320 * @list: the entry to move
321 * @head: the head that will follow our entry
323 static inline void list_move_tail(struct list_head *list,
324 struct list_head *head)
326 __list_del(list->prev, list->next);
327 list_add_tail(list, head);
331 * list_is_last - tests whether @list is the last entry in list @head
332 * @list: the entry to test
333 * @head: the head of the list
335 static inline int list_is_last(const struct list_head *list,
336 const struct list_head *head)
338 return list->next == head;
342 * list_empty - tests whether a list is empty
343 * @head: the list to test.
345 static inline int list_empty(const struct list_head *head)
347 return head->next == head;
351 * list_empty_careful - tests whether a list is empty and not being modified
352 * @head: the list to test
355 * tests whether a list is empty _and_ checks that no other CPU might be
356 * in the process of modifying either member (next or prev)
358 * NOTE: using list_empty_careful() without synchronization
359 * can only be safe if the only activity that can happen
360 * to the list entry is list_del_init(). Eg. it cannot be used
361 * if another CPU could re-list_add() it.
363 static inline int list_empty_careful(const struct list_head *head)
365 struct list_head *next = head->next;
366 return (next == head) && (next == head->prev);
370 * list_is_singular - tests whether a list has just one entry.
371 * @head: the list to test.
373 static inline int list_is_singular(const struct list_head *head)
375 return !list_empty(head) && (head->next == head->prev);
378 static inline void __list_splice(const struct list_head *list,
379 struct list_head *head)
381 struct list_head *first = list->next;
382 struct list_head *last = list->prev;
383 struct list_head *at = head->next;
393 * list_splice - join two lists
394 * @list: the new list to add.
395 * @head: the place to add it in the first list.
397 static inline void list_splice(const struct list_head *list,
398 struct list_head *head)
400 if (!list_empty(list))
401 __list_splice(list, head);
405 * list_splice_init - join two lists and reinitialise the emptied list.
406 * @list: the new list to add.
407 * @head: the place to add it in the first list.
409 * The list at @list is reinitialised
411 static inline void list_splice_init(struct list_head *list,
412 struct list_head *head)
414 if (!list_empty(list)) {
415 __list_splice(list, head);
416 INIT_LIST_HEAD(list);
421 * list_entry - get the struct for this entry
422 * @ptr: the &struct list_head pointer.
423 * @type: the type of the struct this is embedded in.
424 * @member: the name of the list_struct within the struct.
426 #define list_entry(ptr, type, member) \
427 container_of(ptr, type, member)
430 * list_first_entry - get the first element from a list
431 * @ptr: the list head to take the element from.
432 * @type: the type of the struct this is embedded in.
433 * @member: the name of the list_struct within the struct.
435 * Note, that list is expected to be not empty.
437 #define list_first_entry(ptr, type, member) \
438 list_entry((ptr)->next, type, member)
441 * list_for_each - iterate over a list
442 * @pos: the &struct list_head to use as a loop cursor.
443 * @head: the head for your list.
445 #define list_for_each(pos, head) \
446 for (pos = (head)->next; pos != (head); pos = pos->next)
449 * list_for_each_prev - iterate over a list backwards
450 * @pos: the &struct list_head to use as a loop cursor.
451 * @head: the head for your list.
453 #define list_for_each_prev(pos, head) \
454 for (pos = (head)->prev; pos != (head); pos = pos->prev)
457 * list_for_each_safe - iterate over a list safe against removal of list entry
458 * @pos: the &struct list_head to use as a loop cursor.
459 * @n: another &struct list_head to use as temporary storage
460 * @head: the head for your list.
462 #define list_for_each_safe(pos, n, head) \
463 for (pos = (head)->next, n = pos->next; pos != (head); \
464 pos = n, n = pos->next)
467 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
468 * @pos: the &struct list_head to use as a loop cursor.
469 * @n: another &struct list_head to use as temporary storage
470 * @head: the head for your list.
472 #define list_for_each_prev_safe(pos, n, head) \
473 for (pos = (head)->prev, n = pos->prev; \
474 pos != (head); pos = n, n = pos->prev)
477 * list_for_each_entry - iterate over list of given type
478 * @pos: the type * to use as a loop cursor.
479 * @head: the head for your list.
480 * @member: the name of the list_struct within the struct.
482 #define list_for_each_entry(pos, head, member) \
483 for (pos = list_entry((head)->next, typeof(*pos), member); \
484 &pos->member != (head); \
485 pos = list_entry(pos->member.next, typeof(*pos), member))
488 * list_for_each_entry_reverse - iterate backwards over list of given type.
489 * @pos: the type * to use as a loop cursor.
490 * @head: the head for your list.
491 * @member: the name of the list_struct within the struct.
493 #define list_for_each_entry_reverse(pos, head, member) \
494 for (pos = list_entry((head)->prev, typeof(*pos), member); \
495 &pos->member != (head); \
496 pos = list_entry(pos->member.prev, typeof(*pos), member))
499 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
500 * @pos: the type * to use as a start point
501 * @head: the head of the list
502 * @member: the name of the list_struct within the struct.
504 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
506 #define list_prepare_entry(pos, head, member) \
507 ((pos) ? : list_entry(head, typeof(*pos), member))
510 * list_for_each_entry_continue - continue iteration over list of given type
511 * @pos: the type * to use as a loop cursor.
512 * @head: the head for your list.
513 * @member: the name of the list_struct within the struct.
515 * Continue to iterate over list of given type, continuing after
516 * the current position.
518 #define list_for_each_entry_continue(pos, head, member) \
519 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
520 &pos->member != (head); \
521 pos = list_entry(pos->member.next, typeof(*pos), member))
524 * list_for_each_entry_continue_reverse - iterate backwards from the given point
525 * @pos: the type * to use as a loop cursor.
526 * @head: the head for your list.
527 * @member: the name of the list_struct within the struct.
529 * Start to iterate over list of given type backwards, continuing after
530 * the current position.
532 #define list_for_each_entry_continue_reverse(pos, head, member) \
533 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
534 &pos->member != (head); \
535 pos = list_entry(pos->member.prev, typeof(*pos), member))
538 * list_for_each_entry_from - iterate over list of given type from the current point
539 * @pos: the type * to use as a loop cursor.
540 * @head: the head for your list.
541 * @member: the name of the list_struct within the struct.
543 * Iterate over list of given type, continuing from current position.
545 #define list_for_each_entry_from(pos, head, member) \
546 for (; &pos->member != (head); \
547 pos = list_entry(pos->member.next, typeof(*pos), member))
550 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
551 * @pos: the type * to use as a loop cursor.
552 * @n: another type * to use as temporary storage
553 * @head: the head for your list.
554 * @member: the name of the list_struct within the struct.
556 #define list_for_each_entry_safe(pos, n, head, member) \
557 for (pos = list_entry((head)->next, typeof(*pos), member), \
558 n = list_entry(pos->member.next, typeof(*pos), member); \
559 &pos->member != (head); \
560 pos = n, n = list_entry(n->member.next, typeof(*n), member))
563 * list_for_each_entry_safe_continue
564 * @pos: the type * to use as a loop cursor.
565 * @n: another type * to use as temporary storage
566 * @head: the head for your list.
567 * @member: the name of the list_struct within the struct.
569 * Iterate over list of given type, continuing after current point,
570 * safe against removal of list entry.
572 #define list_for_each_entry_safe_continue(pos, n, head, member) \
573 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
574 n = list_entry(pos->member.next, typeof(*pos), member); \
575 &pos->member != (head); \
576 pos = n, n = list_entry(n->member.next, typeof(*n), member))
579 * list_for_each_entry_safe_from
580 * @pos: the type * to use as a loop cursor.
581 * @n: another type * to use as temporary storage
582 * @head: the head for your list.
583 * @member: the name of the list_struct within the struct.
585 * Iterate over list of given type from current point, safe against
586 * removal of list entry.
588 #define list_for_each_entry_safe_from(pos, n, head, member) \
589 for (n = list_entry(pos->member.next, typeof(*pos), member); \
590 &pos->member != (head); \
591 pos = n, n = list_entry(n->member.next, typeof(*n), member))
594 * list_for_each_entry_safe_reverse
595 * @pos: the type * to use as a loop cursor.
596 * @n: another type * to use as temporary storage
597 * @head: the head for your list.
598 * @member: the name of the list_struct within the struct.
600 * Iterate backwards over list of given type, safe against removal
603 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
604 for (pos = list_entry((head)->prev, typeof(*pos), member), \
605 n = list_entry(pos->member.prev, typeof(*pos), member); \
606 &pos->member != (head); \
607 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
609 #endif /*_OSSP_UTIL_H*/