2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _LINUX_BITOPS_H_
29 #define _LINUX_BITOPS_H_
32 #define BITS_PER_LONG 64
34 #define BITS_PER_LONG 32
36 #define BIT_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
37 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
38 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
43 return (ffs(mask) - 1);
49 return (fls(mask) - 1);
55 return (ffsl(mask) - 1);
61 return (flsl(mask) - 1);
65 #define ffz(mask) __ffs(~(mask))
67 static inline int get_count_order(unsigned int count)
71 order = fls(count) - 1;
72 if (count & (count - 1))
77 static inline unsigned long
78 find_first_bit(unsigned long *addr, unsigned long size)
83 for (bit = 0; size >= BITS_PER_LONG;
84 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
87 return (bit + __ffsl(*addr));
90 mask = (*addr) & BIT_MASK(size);
99 static inline unsigned long
100 find_first_zero_bit(unsigned long *addr, unsigned long size)
105 for (bit = 0; size >= BITS_PER_LONG;
106 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
109 return (bit + __ffsl(~(*addr)));
112 mask = ~(*addr) & BIT_MASK(size);
121 static inline unsigned long
122 find_last_bit(unsigned long *addr, unsigned long size)
129 pos = size / BITS_PER_LONG;
130 offs = size % BITS_PER_LONG;
131 bit = BITS_PER_LONG * pos;
134 mask = (*addr) & BIT_MASK(offs);
136 return (bit + __flsl(mask));
140 bit -= BITS_PER_LONG;
142 return (bit + __flsl(mask));
147 static inline unsigned long
148 find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
157 pos = offset / BITS_PER_LONG;
158 offs = offset % BITS_PER_LONG;
159 bit = BITS_PER_LONG * pos;
162 mask = (*addr) & ~BIT_MASK(offs);
164 return (bit + __ffsl(mask));
165 bit += BITS_PER_LONG;
168 for (size -= bit; size >= BITS_PER_LONG;
169 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
172 return (bit + __ffsl(*addr));
175 mask = (*addr) & BIT_MASK(size);
184 static inline unsigned long
185 find_next_zero_bit(unsigned long *addr, unsigned long size,
186 unsigned long offset)
195 pos = offset / BITS_PER_LONG;
196 offs = offset % BITS_PER_LONG;
197 bit = BITS_PER_LONG * pos;
200 mask = ~(*addr) & ~BIT_MASK(offs);
202 return (bit + __ffsl(mask));
203 bit += BITS_PER_LONG;
206 for (size -= bit; size >= BITS_PER_LONG;
207 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
210 return (bit + __ffsl(~(*addr)));
213 mask = ~(*addr) & BIT_MASK(size);
223 bitmap_zero(unsigned long *addr, int size)
227 len = BITS_TO_LONGS(size) * sizeof(long);
228 memset(addr, 0, len);
232 bitmap_fill(unsigned long *addr, int size)
237 len = (size / BITS_PER_LONG) * sizeof(long);
238 memset(addr, 0xff, len);
239 tail = size & (BITS_PER_LONG - 1);
241 addr[size / BITS_PER_LONG] = BIT_MASK(tail);
245 bitmap_full(unsigned long *addr, int size)
252 len = size / BITS_PER_LONG;
253 for (i = 0; i < len; i++)
256 tail = size & (BITS_PER_LONG - 1);
258 mask = BIT_MASK(tail);
259 if ((addr[i] & mask) != mask)
266 bitmap_empty(unsigned long *addr, int size)
273 len = size / BITS_PER_LONG;
274 for (i = 0; i < len; i++)
277 tail = size & (BITS_PER_LONG - 1);
279 mask = BIT_MASK(tail);
280 if ((addr[i] & mask) != 0)
286 #define NBLONG (NBBY * sizeof(long))
288 #define set_bit(i, a) \
289 atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1 << (i) % NBLONG)
291 #define clear_bit(i, a) \
292 atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1 << (i) % NBLONG)
294 #define test_bit(i, a) \
295 !!(atomic_load_acq_long(&((volatile long *)(a))[(i)/NBLONG]) & \
299 test_and_clear_bit(long bit, long *var)
303 var += bit / (sizeof(long) * NBBY);
304 bit %= sizeof(long) * NBBY;
307 val = *(volatile long *)var;
308 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
310 return !!(val & bit);
314 test_and_set_bit(long bit, volatile unsigned long *var)
318 var += bit / (sizeof(long) * NBBY);
319 bit %= sizeof(long) * NBBY;
322 val = *(volatile long *)var;
323 } while (atomic_cmpset_long(var, val, val | bit) == 0);
325 return !!(val & bit);
329 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
330 #define BITMAP_LAST_WORD_MASK(nbits) \
332 ((nbits) % BITS_PER_LONG) ? \
333 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
338 bitmap_set(unsigned long *map, int start, int nr)
340 unsigned long *p = map + BIT_WORD(start);
341 const int size = start + nr;
342 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
343 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
345 while (nr - bits_to_set >= 0) {
348 bits_to_set = BITS_PER_LONG;
353 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
359 bitmap_clear(unsigned long *map, int start, int nr)
361 unsigned long *p = map + BIT_WORD(start);
362 const int size = start + nr;
363 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
364 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
366 while (nr - bits_to_clear >= 0) {
367 *p &= ~mask_to_clear;
369 bits_to_clear = BITS_PER_LONG;
370 mask_to_clear = ~0UL;
374 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
375 *p &= ~mask_to_clear;
380 REG_OP_ISFREE, /* true if region is all zero bits */
381 REG_OP_ALLOC, /* set all bits in region */
382 REG_OP_RELEASE, /* clear all bits in region */
385 static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
387 int nbits_reg; /* number of bits in region */
388 int index; /* index first long of region in bitmap */
389 int offset; /* bit offset region in bitmap[index] */
390 int nlongs_reg; /* num longs spanned by region in bitmap */
391 int nbitsinlong; /* num bits of region in each spanned long */
392 unsigned long mask; /* bitmask for one long of region */
393 int i; /* scans bitmap by longs */
394 int ret = 0; /* return value */
397 * Either nlongs_reg == 1 (for small orders that fit in one long)
398 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
400 nbits_reg = 1 << order;
401 index = pos / BITS_PER_LONG;
402 offset = pos - (index * BITS_PER_LONG);
403 nlongs_reg = BITS_TO_LONGS(nbits_reg);
404 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
407 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
408 * overflows if nbitsinlong == BITS_PER_LONG.
410 mask = (1UL << (nbitsinlong - 1));
416 for (i = 0; i < nlongs_reg; i++) {
417 if (bitmap[index + i] & mask)
420 ret = 1; /* all bits in region free (zero) */
424 for (i = 0; i < nlongs_reg; i++)
425 bitmap[index + i] |= mask;
429 for (i = 0; i < nlongs_reg; i++)
430 bitmap[index + i] &= ~mask;
438 * bitmap_find_free_region - find a contiguous aligned mem region
439 * @bitmap: array of unsigned longs corresponding to the bitmap
440 * @bits: number of bits in the bitmap
441 * @order: region size (log base 2 of number of bits) to find
443 * Find a region of free (zero) bits in a @bitmap of @bits bits and
444 * allocate them (set them to one). Only consider regions of length
445 * a power (@order) of two, aligned to that power of two, which
446 * makes the search algorithm much faster.
448 * Return the bit offset in bitmap of the allocated region,
449 * or -errno on failure.
452 bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
454 int pos, end; /* scans bitmap by regions of size order */
456 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
457 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
459 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
466 * bitmap_release_region - release allocated bitmap region
467 * @bitmap: array of unsigned longs corresponding to the bitmap
468 * @pos: beginning of bit region to release
469 * @order: region size (log base 2 of number of bits) to release
471 * This is the complement to __bitmap_find_free_region() and releases
472 * the found region (by clearing it in the bitmap).
477 bitmap_release_region(unsigned long *bitmap, int pos, int order)
479 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
483 #endif /* _LINUX_BITOPS_H_ */