blob: 72bab64ecf60a53bf79c85a86f254c79111184b1 [file] [log] [blame]
David Sterba602cbe92019-08-21 18:48:25 +02001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_MISC_H
4#define BTRFS_MISC_H
5
6#include <linux/sched.h>
7#include <linux/wait.h>
David Sterba784352f2019-08-21 18:54:28 +02008#include <asm/div64.h>
David Sterba602cbe92019-08-21 18:48:25 +02009
10#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
11
12static inline void cond_wake_up(struct wait_queue_head *wq)
13{
14 /*
15 * This implies a full smp_mb barrier, see comments for
16 * waitqueue_active why.
17 */
18 if (wq_has_sleeper(wq))
19 wake_up(wq);
20}
21
22static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
23{
24 /*
25 * Special case for conditional wakeup where the barrier required for
26 * waitqueue_active is implied by some of the preceding code. Eg. one
27 * of such atomic operations (atomic_dec_and_return, ...), or a
28 * unlock/lock sequence, etc.
29 */
30 if (waitqueue_active(wq))
31 wake_up(wq);
32}
33
David Sterba784352f2019-08-21 18:54:28 +020034static inline u64 div_factor(u64 num, int factor)
35{
36 if (factor == 10)
37 return num;
38 num *= factor;
39 return div_u64(num, 10);
40}
41
42static inline u64 div_factor_fine(u64 num, int factor)
43{
44 if (factor == 100)
45 return num;
46 num *= factor;
47 return div_u64(num, 100);
48}
49
David Sterba79c82642019-10-01 19:40:15 +020050/* Copy of is_power_of_two that is 64bit safe */
51static inline bool is_power_of_two_u64(u64 n)
52{
53 return n != 0 && (n & (n - 1)) == 0;
54}
55
56static inline bool has_single_bit_set(u64 n)
57{
58 return is_power_of_two_u64(n);
59}
60
David Sterba602cbe92019-08-21 18:48:25 +020061#endif