lib: bitmap: order includes alphabetically
[linux-block.git] / lib / bitmap.c
CommitLineData
40b0b3f8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * lib/bitmap.c
4 * Helper functions for bitmap.h.
1da177e4 5 */
c13656b9 6
1da177e4
LT
7#include <linux/bitmap.h>
8#include <linux/bitops.h>
50af5ead 9#include <linux/bug.h>
c13656b9
BG
10#include <linux/ctype.h>
11#include <linux/errno.h>
12#include <linux/export.h>
e52bc7c2 13#include <linux/kernel.h>
ce1091d4 14#include <linux/mm.h>
c42b65e3 15#include <linux/slab.h>
e52bc7c2 16#include <linux/string.h>
c13656b9 17#include <linux/thread_info.h>
13d4ea09 18#include <linux/uaccess.h>
5aaba363
SH
19
20#include <asm/page.h>
1da177e4 21
e371c481
YN
22#include "kstrtox.h"
23
7d7363e4
RD
24/**
25 * DOC: bitmap introduction
26 *
197d6c1d 27 * bitmaps provide an array of bits, implemented using an
1da177e4
LT
28 * array of unsigned longs. The number of valid bits in a
29 * given bitmap does _not_ need to be an exact multiple of
30 * BITS_PER_LONG.
31 *
32 * The possible unused bits in the last, partially used word
33 * of a bitmap are 'don't care'. The implementation makes
34 * no particular effort to keep them zero. It ensures that
35 * their value will not affect the results of any operation.
36 * The bitmap operations that return Boolean (bitmap_empty,
37 * for example) or scalar (bitmap_weight, for example) results
38 * carefully filter out these unused bits from impacting their
39 * results.
40 *
1da177e4
LT
41 * The byte ordering of bitmaps is more natural on little
42 * endian architectures. See the big-endian headers
43 * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
44 * for the best explanations of this ordering.
45 */
46
1da177e4 47int __bitmap_equal(const unsigned long *bitmap1,
5e068069 48 const unsigned long *bitmap2, unsigned int bits)
1da177e4 49{
5e068069 50 unsigned int k, lim = bits/BITS_PER_LONG;
1da177e4
LT
51 for (k = 0; k < lim; ++k)
52 if (bitmap1[k] != bitmap2[k])
53 return 0;
54
55 if (bits % BITS_PER_LONG)
56 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
57 return 0;
58
59 return 1;
60}
61EXPORT_SYMBOL(__bitmap_equal);
62
b9fa6442
TG
63bool __bitmap_or_equal(const unsigned long *bitmap1,
64 const unsigned long *bitmap2,
65 const unsigned long *bitmap3,
66 unsigned int bits)
67{
68 unsigned int k, lim = bits / BITS_PER_LONG;
69 unsigned long tmp;
70
71 for (k = 0; k < lim; ++k) {
72 if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
73 return false;
74 }
75
76 if (!(bits % BITS_PER_LONG))
77 return true;
78
79 tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
80 return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
81}
82
3d6684f4 83void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
1da177e4 84{
ca1250bb 85 unsigned int k, lim = BITS_TO_LONGS(bits);
1da177e4
LT
86 for (k = 0; k < lim; ++k)
87 dst[k] = ~src[k];
1da177e4
LT
88}
89EXPORT_SYMBOL(__bitmap_complement);
90
72fd4a35 91/**
1da177e4 92 * __bitmap_shift_right - logical right shift of the bits in a bitmap
05fb6bf0
RD
93 * @dst : destination bitmap
94 * @src : source bitmap
95 * @shift : shift by this many bits
2fbad299 96 * @nbits : bitmap size, in bits
1da177e4
LT
97 *
98 * Shifting right (dividing) means moving bits in the MS -> LS bit
99 * direction. Zeros are fed into the vacated MS positions and the
100 * LS bits shifted off the bottom are lost.
101 */
2fbad299
RV
102void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
103 unsigned shift, unsigned nbits)
1da177e4 104{
cfac1d08 105 unsigned k, lim = BITS_TO_LONGS(nbits);
2fbad299 106 unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
cfac1d08 107 unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
1da177e4
LT
108 for (k = 0; off + k < lim; ++k) {
109 unsigned long upper, lower;
110
111 /*
112 * If shift is not word aligned, take lower rem bits of
113 * word above and make them the top rem bits of result.
114 */
115 if (!rem || off + k + 1 >= lim)
116 upper = 0;
117 else {
118 upper = src[off + k + 1];
cfac1d08 119 if (off + k + 1 == lim - 1)
1da177e4 120 upper &= mask;
9d8a6b2a 121 upper <<= (BITS_PER_LONG - rem);
1da177e4
LT
122 }
123 lower = src[off + k];
cfac1d08 124 if (off + k == lim - 1)
1da177e4 125 lower &= mask;
9d8a6b2a
RV
126 lower >>= rem;
127 dst[k] = lower | upper;
1da177e4
LT
128 }
129 if (off)
130 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
131}
132EXPORT_SYMBOL(__bitmap_shift_right);
133
134
72fd4a35 135/**
1da177e4 136 * __bitmap_shift_left - logical left shift of the bits in a bitmap
05fb6bf0
RD
137 * @dst : destination bitmap
138 * @src : source bitmap
139 * @shift : shift by this many bits
dba94c25 140 * @nbits : bitmap size, in bits
1da177e4
LT
141 *
142 * Shifting left (multiplying) means moving bits in the LS -> MS
143 * direction. Zeros are fed into the vacated LS bit positions
144 * and those MS bits shifted off the top are lost.
145 */
146
dba94c25
RV
147void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
148 unsigned int shift, unsigned int nbits)
1da177e4 149{
dba94c25 150 int k;
7f590657 151 unsigned int lim = BITS_TO_LONGS(nbits);
dba94c25 152 unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
1da177e4
LT
153 for (k = lim - off - 1; k >= 0; --k) {
154 unsigned long upper, lower;
155
156 /*
157 * If shift is not word aligned, take upper rem bits of
158 * word below and make them the bottom rem bits of result.
159 */
160 if (rem && k > 0)
6d874eca 161 lower = src[k - 1] >> (BITS_PER_LONG - rem);
1da177e4
LT
162 else
163 lower = 0;
7f590657 164 upper = src[k] << rem;
6d874eca 165 dst[k + off] = lower | upper;
1da177e4
LT
166 }
167 if (off)
168 memset(dst, 0, off*sizeof(unsigned long));
169}
170EXPORT_SYMBOL(__bitmap_shift_left);
171
20927671
SB
172/**
173 * bitmap_cut() - remove bit region from bitmap and right shift remaining bits
174 * @dst: destination bitmap, might overlap with src
175 * @src: source bitmap
176 * @first: start bit of region to be removed
177 * @cut: number of bits to remove
178 * @nbits: bitmap size, in bits
179 *
180 * Set the n-th bit of @dst iff the n-th bit of @src is set and
181 * n is less than @first, or the m-th bit of @src is set for any
182 * m such that @first <= n < nbits, and m = n + @cut.
183 *
184 * In pictures, example for a big-endian 32-bit architecture:
185 *
4642289b 186 * The @src bitmap is::
20927671 187 *
4642289b
MCC
188 * 31 63
189 * | |
190 * 10000000 11000001 11110010 00010101 10000000 11000001 01110010 00010101
191 * | | | |
192 * 16 14 0 32
20927671 193 *
4642289b
MCC
194 * if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is::
195 *
196 * 31 63
197 * | |
198 * 10110000 00011000 00110010 00010101 00010000 00011000 00101110 01000010
199 * | | |
200 * 14 (bit 17 0 32
201 * from @src)
20927671
SB
202 *
203 * Note that @dst and @src might overlap partially or entirely.
204 *
205 * This is implemented in the obvious way, with a shift and carry
206 * step for each moved bit. Optimisation is left as an exercise
207 * for the compiler.
208 */
209void bitmap_cut(unsigned long *dst, const unsigned long *src,
210 unsigned int first, unsigned int cut, unsigned int nbits)
211{
212 unsigned int len = BITS_TO_LONGS(nbits);
213 unsigned long keep = 0, carry;
214 int i;
215
20927671
SB
216 if (first % BITS_PER_LONG) {
217 keep = src[first / BITS_PER_LONG] &
218 (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
219 }
220
5959f829
SB
221 memmove(dst, src, len * sizeof(*dst));
222
20927671
SB
223 while (cut--) {
224 for (i = first / BITS_PER_LONG; i < len; i++) {
225 if (i < len - 1)
226 carry = dst[i + 1] & 1UL;
227 else
228 carry = 0;
229
230 dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));
231 }
232 }
233
234 dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);
235 dst[first / BITS_PER_LONG] |= keep;
236}
237EXPORT_SYMBOL(bitmap_cut);
238
f4b0373b 239int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 240 const unsigned long *bitmap2, unsigned int bits)
1da177e4 241{
2f9305eb 242 unsigned int k;
7e5f97d1 243 unsigned int lim = bits/BITS_PER_LONG;
f4b0373b 244 unsigned long result = 0;
1da177e4 245
7e5f97d1 246 for (k = 0; k < lim; k++)
f4b0373b 247 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
7e5f97d1
RV
248 if (bits % BITS_PER_LONG)
249 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
250 BITMAP_LAST_WORD_MASK(bits));
f4b0373b 251 return result != 0;
1da177e4
LT
252}
253EXPORT_SYMBOL(__bitmap_and);
254
255void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 256 const unsigned long *bitmap2, unsigned int bits)
1da177e4 257{
2f9305eb
RV
258 unsigned int k;
259 unsigned int nr = BITS_TO_LONGS(bits);
1da177e4
LT
260
261 for (k = 0; k < nr; k++)
262 dst[k] = bitmap1[k] | bitmap2[k];
263}
264EXPORT_SYMBOL(__bitmap_or);
265
266void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 267 const unsigned long *bitmap2, unsigned int bits)
1da177e4 268{
2f9305eb
RV
269 unsigned int k;
270 unsigned int nr = BITS_TO_LONGS(bits);
1da177e4
LT
271
272 for (k = 0; k < nr; k++)
273 dst[k] = bitmap1[k] ^ bitmap2[k];
274}
275EXPORT_SYMBOL(__bitmap_xor);
276
f4b0373b 277int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 278 const unsigned long *bitmap2, unsigned int bits)
1da177e4 279{
2f9305eb 280 unsigned int k;
74e76531 281 unsigned int lim = bits/BITS_PER_LONG;
f4b0373b 282 unsigned long result = 0;
1da177e4 283
74e76531 284 for (k = 0; k < lim; k++)
f4b0373b 285 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
74e76531
RV
286 if (bits % BITS_PER_LONG)
287 result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
288 BITMAP_LAST_WORD_MASK(bits));
f4b0373b 289 return result != 0;
1da177e4
LT
290}
291EXPORT_SYMBOL(__bitmap_andnot);
292
30544ed5
AS
293void __bitmap_replace(unsigned long *dst,
294 const unsigned long *old, const unsigned long *new,
295 const unsigned long *mask, unsigned int nbits)
296{
297 unsigned int k;
298 unsigned int nr = BITS_TO_LONGS(nbits);
299
300 for (k = 0; k < nr; k++)
301 dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);
302}
303EXPORT_SYMBOL(__bitmap_replace);
304
1da177e4 305int __bitmap_intersects(const unsigned long *bitmap1,
6dfe9799 306 const unsigned long *bitmap2, unsigned int bits)
1da177e4 307{
6dfe9799 308 unsigned int k, lim = bits/BITS_PER_LONG;
1da177e4
LT
309 for (k = 0; k < lim; ++k)
310 if (bitmap1[k] & bitmap2[k])
311 return 1;
312
313 if (bits % BITS_PER_LONG)
314 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
315 return 1;
316 return 0;
317}
318EXPORT_SYMBOL(__bitmap_intersects);
319
320int __bitmap_subset(const unsigned long *bitmap1,
5be20213 321 const unsigned long *bitmap2, unsigned int bits)
1da177e4 322{
5be20213 323 unsigned int k, lim = bits/BITS_PER_LONG;
1da177e4
LT
324 for (k = 0; k < lim; ++k)
325 if (bitmap1[k] & ~bitmap2[k])
326 return 0;
327
328 if (bits % BITS_PER_LONG)
329 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
330 return 0;
331 return 1;
332}
333EXPORT_SYMBOL(__bitmap_subset);
334
877d9f3b 335int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
1da177e4 336{
877d9f3b
RV
337 unsigned int k, lim = bits/BITS_PER_LONG;
338 int w = 0;
1da177e4
LT
339
340 for (k = 0; k < lim; k++)
37d54111 341 w += hweight_long(bitmap[k]);
1da177e4
LT
342
343 if (bits % BITS_PER_LONG)
37d54111 344 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
1da177e4
LT
345
346 return w;
347}
1da177e4
LT
348EXPORT_SYMBOL(__bitmap_weight);
349
e5af323c 350void __bitmap_set(unsigned long *map, unsigned int start, int len)
c1a2a962
AM
351{
352 unsigned long *p = map + BIT_WORD(start);
fb5ac542 353 const unsigned int size = start + len;
c1a2a962
AM
354 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
355 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
356
fb5ac542 357 while (len - bits_to_set >= 0) {
c1a2a962 358 *p |= mask_to_set;
fb5ac542 359 len -= bits_to_set;
c1a2a962
AM
360 bits_to_set = BITS_PER_LONG;
361 mask_to_set = ~0UL;
362 p++;
363 }
fb5ac542 364 if (len) {
c1a2a962
AM
365 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
366 *p |= mask_to_set;
367 }
368}
e5af323c 369EXPORT_SYMBOL(__bitmap_set);
c1a2a962 370
e5af323c 371void __bitmap_clear(unsigned long *map, unsigned int start, int len)
c1a2a962
AM
372{
373 unsigned long *p = map + BIT_WORD(start);
154f5e38 374 const unsigned int size = start + len;
c1a2a962
AM
375 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
376 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
377
154f5e38 378 while (len - bits_to_clear >= 0) {
c1a2a962 379 *p &= ~mask_to_clear;
154f5e38 380 len -= bits_to_clear;
c1a2a962
AM
381 bits_to_clear = BITS_PER_LONG;
382 mask_to_clear = ~0UL;
383 p++;
384 }
154f5e38 385 if (len) {
c1a2a962
AM
386 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
387 *p &= ~mask_to_clear;
388 }
389}
e5af323c 390EXPORT_SYMBOL(__bitmap_clear);
c1a2a962 391
5e19b013
MN
392/**
393 * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
c1a2a962
AM
394 * @map: The address to base the search on
395 * @size: The bitmap size in bits
396 * @start: The bitnumber to start searching at
397 * @nr: The number of zeroed bits we're looking for
398 * @align_mask: Alignment mask for zero area
5e19b013 399 * @align_offset: Alignment offset for zero area.
c1a2a962
AM
400 *
401 * The @align_mask should be one less than a power of 2; the effect is that
5e19b013
MN
402 * the bit offset of all zero areas this function finds plus @align_offset
403 * is multiple of that power of 2.
c1a2a962 404 */
5e19b013
MN
405unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
406 unsigned long size,
407 unsigned long start,
408 unsigned int nr,
409 unsigned long align_mask,
410 unsigned long align_offset)
c1a2a962
AM
411{
412 unsigned long index, end, i;
413again:
414 index = find_next_zero_bit(map, size, start);
415
416 /* Align allocation */
5e19b013 417 index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
c1a2a962
AM
418
419 end = index + nr;
420 if (end > size)
421 return end;
422 i = find_next_bit(map, end, index);
423 if (i < end) {
424 start = i + 1;
425 goto again;
426 }
427 return index;
428}
5e19b013 429EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
c1a2a962 430
1da177e4 431/*
6d49e352 432 * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
1da177e4
LT
433 * second version by Paul Jackson, third by Joe Korty.
434 */
435
01a3ee2b 436/**
9a86e2ba 437 * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
01a3ee2b
RC
438 *
439 * @ubuf: pointer to user buffer containing string.
440 * @ulen: buffer size in bytes. If string is smaller than this
441 * then it must be terminated with a \0.
442 * @maskp: pointer to bitmap array that will contain result.
443 * @nmaskbits: size of bitmap, in bits.
01a3ee2b
RC
444 */
445int bitmap_parse_user(const char __user *ubuf,
446 unsigned int ulen, unsigned long *maskp,
447 int nmaskbits)
448{
e66eda06
YN
449 char *buf;
450 int ret;
451
452 buf = memdup_user_nul(ubuf, ulen);
453 if (IS_ERR(buf))
454 return PTR_ERR(buf);
455
2d626158 456 ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits);
b9c321fd 457
e66eda06
YN
458 kfree(buf);
459 return ret;
01a3ee2b
RC
460}
461EXPORT_SYMBOL(bitmap_parse_user);
1da177e4 462
5aaba363
SH
463/**
464 * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
465 * @list: indicates whether the bitmap must be list
466 * @buf: page aligned buffer into which string is placed
467 * @maskp: pointer to bitmap to convert
468 * @nmaskbits: size of bitmap, in bits
469 *
470 * Output format is a comma-separated list of decimal numbers and
471 * ranges if list is specified or hex digits grouped into comma-separated
472 * sets of 8 digits/set. Returns the number of characters written to buf.
9cf79d11 473 *
ce1091d4
RV
474 * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
475 * area and that sufficient storage remains at @buf to accommodate the
476 * bitmap_print_to_pagebuf() output. Returns the number of characters
477 * actually printed to @buf, excluding terminating '\0'.
5aaba363
SH
478 */
479int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
480 int nmaskbits)
481{
ce1091d4 482 ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
5aaba363 483
8ec3d768
RV
484 return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
485 scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
5aaba363
SH
486}
487EXPORT_SYMBOL(bitmap_print_to_pagebuf);
488
e371c481
YN
489/*
490 * Region 9-38:4/10 describes the following bitmap structure:
491 * 0 9 12 18 38
492 * .........****......****......****......
493 * ^ ^ ^ ^
494 * start off group_len end
495 */
496struct region {
497 unsigned int start;
498 unsigned int off;
499 unsigned int group_len;
500 unsigned int end;
501};
502
503static int bitmap_set_region(const struct region *r,
504 unsigned long *bitmap, int nbits)
505{
506 unsigned int start;
507
508 if (r->end >= nbits)
509 return -ERANGE;
510
511 for (start = r->start; start <= r->end; start += r->group_len)
512 bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
513
514 return 0;
515}
516
517static int bitmap_check_region(const struct region *r)
518{
519 if (r->start > r->end || r->group_len == 0 || r->off > r->group_len)
520 return -EINVAL;
521
522 return 0;
523}
524
525static const char *bitmap_getnum(const char *str, unsigned int *num)
526{
527 unsigned long long n;
528 unsigned int len;
529
530 len = _parse_integer(str, 10, &n);
531 if (!len)
532 return ERR_PTR(-EINVAL);
533 if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n)
534 return ERR_PTR(-EOVERFLOW);
535
536 *num = n;
537 return str + len;
538}
539
540static inline bool end_of_str(char c)
541{
542 return c == '\0' || c == '\n';
543}
544
545static inline bool __end_of_region(char c)
546{
547 return isspace(c) || c == ',';
548}
549
550static inline bool end_of_region(char c)
551{
552 return __end_of_region(c) || end_of_str(c);
553}
554
555/*
20607434 556 * The format allows commas and whitespaces at the beginning
e371c481
YN
557 * of the region.
558 */
559static const char *bitmap_find_region(const char *str)
560{
561 while (__end_of_region(*str))
562 str++;
563
564 return end_of_str(*str) ? NULL : str;
565}
566
2d626158
YN
567static const char *bitmap_find_region_reverse(const char *start, const char *end)
568{
569 while (start <= end && __end_of_region(*end))
570 end--;
571
572 return end;
573}
574
e371c481
YN
575static const char *bitmap_parse_region(const char *str, struct region *r)
576{
577 str = bitmap_getnum(str, &r->start);
578 if (IS_ERR(str))
579 return str;
580
581 if (end_of_region(*str))
582 goto no_end;
583
584 if (*str != '-')
585 return ERR_PTR(-EINVAL);
586
587 str = bitmap_getnum(str + 1, &r->end);
588 if (IS_ERR(str))
589 return str;
590
591 if (end_of_region(*str))
592 goto no_pattern;
593
594 if (*str != ':')
595 return ERR_PTR(-EINVAL);
596
597 str = bitmap_getnum(str + 1, &r->off);
598 if (IS_ERR(str))
599 return str;
600
601 if (*str != '/')
602 return ERR_PTR(-EINVAL);
603
604 return bitmap_getnum(str + 1, &r->group_len);
605
606no_end:
607 r->end = r->start;
608no_pattern:
609 r->off = r->end + 1;
610 r->group_len = r->end + 1;
611
612 return end_of_str(*str) ? NULL : str;
613}
614
1da177e4 615/**
e371c481
YN
616 * bitmap_parselist - convert list format ASCII string to bitmap
617 * @buf: read user string from this buffer; must be terminated
618 * with a \0 or \n.
6e1907ff 619 * @maskp: write resulting mask here
1da177e4
LT
620 * @nmaskbits: number of bits in mask to be written
621 *
622 * Input format is a comma-separated list of decimal numbers and
623 * ranges. Consecutively set bits are shown as two hyphen-separated
624 * decimal numbers, the smallest and largest bit numbers set in
625 * the range.
2d13e6ca
NC
626 * Optionally each range can be postfixed to denote that only parts of it
627 * should be set. The range will divided to groups of specific size.
628 * From each group will be used only defined amount of bits.
629 * Syntax: range:used_size/group_size
630 * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
1da177e4 631 *
40bf19a8 632 * Returns: 0 on success, -errno on invalid input strings. Error values:
633 *
e371c481 634 * - ``-EINVAL``: wrong region format
40bf19a8 635 * - ``-EINVAL``: invalid character in string
636 * - ``-ERANGE``: bit number specified too large for mask
e371c481 637 * - ``-EOVERFLOW``: integer overflow in the input parameters
1da177e4 638 */
e371c481 639int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits)
1da177e4 640{
e371c481
YN
641 struct region r;
642 long ret;
1da177e4
LT
643
644 bitmap_zero(maskp, nmaskbits);
4b060420 645
e371c481
YN
646 while (buf) {
647 buf = bitmap_find_region(buf);
648 if (buf == NULL)
649 return 0;
2d13e6ca 650
e371c481
YN
651 buf = bitmap_parse_region(buf, &r);
652 if (IS_ERR(buf))
653 return PTR_ERR(buf);
2d13e6ca 654
e371c481
YN
655 ret = bitmap_check_region(&r);
656 if (ret)
657 return ret;
4b060420 658
e371c481
YN
659 ret = bitmap_set_region(&r, maskp, nmaskbits);
660 if (ret)
661 return ret;
662 }
4b060420 663
1da177e4
LT
664 return 0;
665}
666EXPORT_SYMBOL(bitmap_parselist);
667
4b060420
MT
668
669/**
670 * bitmap_parselist_user()
671 *
672 * @ubuf: pointer to user buffer containing string.
673 * @ulen: buffer size in bytes. If string is smaller than this
674 * then it must be terminated with a \0.
675 * @maskp: pointer to bitmap array that will contain result.
676 * @nmaskbits: size of bitmap, in bits.
677 *
678 * Wrapper for bitmap_parselist(), providing it with user buffer.
4b060420
MT
679 */
680int bitmap_parselist_user(const char __user *ubuf,
681 unsigned int ulen, unsigned long *maskp,
682 int nmaskbits)
683{
281327c9
YN
684 char *buf;
685 int ret;
686
687 buf = memdup_user_nul(ubuf, ulen);
688 if (IS_ERR(buf))
689 return PTR_ERR(buf);
690
691 ret = bitmap_parselist(buf, maskp, nmaskbits);
692
693 kfree(buf);
694 return ret;
4b060420
MT
695}
696EXPORT_SYMBOL(bitmap_parselist_user);
697
2d626158
YN
698static const char *bitmap_get_x32_reverse(const char *start,
699 const char *end, u32 *num)
700{
701 u32 ret = 0;
702 int c, i;
703
704 for (i = 0; i < 32; i += 4) {
705 c = hex_to_bin(*end--);
706 if (c < 0)
707 return ERR_PTR(-EINVAL);
708
709 ret |= c << i;
710
711 if (start > end || __end_of_region(*end))
712 goto out;
713 }
714
715 if (hex_to_bin(*end--) >= 0)
716 return ERR_PTR(-EOVERFLOW);
717out:
718 *num = ret;
719 return end;
720}
721
722/**
723 * bitmap_parse - convert an ASCII hex string into a bitmap.
724 * @start: pointer to buffer containing string.
725 * @buflen: buffer size in bytes. If string is smaller than this
726 * then it must be terminated with a \0 or \n. In that case,
727 * UINT_MAX may be provided instead of string length.
728 * @maskp: pointer to bitmap array that will contain result.
729 * @nmaskbits: size of bitmap, in bits.
730 *
731 * Commas group hex digits into chunks. Each chunk defines exactly 32
732 * bits of the resultant bitmask. No chunk may specify a value larger
733 * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
734 * then leading 0-bits are prepended. %-EINVAL is returned for illegal
735 * characters. Grouping such as "1,,5", ",44", "," or "" is allowed.
736 * Leading, embedded and trailing whitespace accepted.
737 */
738int bitmap_parse(const char *start, unsigned int buflen,
739 unsigned long *maskp, int nmaskbits)
740{
741 const char *end = strnchrnul(start, buflen, '\n') - 1;
742 int chunks = BITS_TO_U32(nmaskbits);
743 u32 *bitmap = (u32 *)maskp;
744 int unset_bit;
81c4f4d9 745 int chunk;
2d626158 746
81c4f4d9 747 for (chunk = 0; ; chunk++) {
2d626158
YN
748 end = bitmap_find_region_reverse(start, end);
749 if (start > end)
750 break;
751
752 if (!chunks--)
753 return -EOVERFLOW;
754
81c4f4d9
AG
755#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
756 end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]);
757#else
758 end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]);
759#endif
2d626158
YN
760 if (IS_ERR(end))
761 return PTR_ERR(end);
762 }
763
764 unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32;
765 if (unset_bit < nmaskbits) {
766 bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit);
767 return 0;
768 }
769
770 if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit)
771 return -EOVERFLOW;
772
773 return 0;
774}
775EXPORT_SYMBOL(bitmap_parse);
776
4b060420 777
cdc90a18 778#ifdef CONFIG_NUMA
72fd4a35 779/**
9a86e2ba 780 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
fb5eeeee 781 * @buf: pointer to a bitmap
df1d80a9
RV
782 * @pos: a bit position in @buf (0 <= @pos < @nbits)
783 * @nbits: number of valid bit positions in @buf
fb5eeeee 784 *
df1d80a9 785 * Map the bit at position @pos in @buf (of length @nbits) to the
fb5eeeee 786 * ordinal of which set bit it is. If it is not set or if @pos
96b7f341 787 * is not a valid bit position, map to -1.
fb5eeeee
PJ
788 *
789 * If for example, just bits 4 through 7 are set in @buf, then @pos
790 * values 4 through 7 will get mapped to 0 through 3, respectively,
a8551748 791 * and other @pos values will get mapped to -1. When @pos value 7
fb5eeeee
PJ
792 * gets mapped to (returns) @ord value 3 in this example, that means
793 * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
794 *
795 * The bit positions 0 through @bits are valid positions in @buf.
796 */
df1d80a9 797static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
fb5eeeee 798{
df1d80a9 799 if (pos >= nbits || !test_bit(pos, buf))
96b7f341 800 return -1;
fb5eeeee 801
df1d80a9 802 return __bitmap_weight(buf, pos);
fb5eeeee
PJ
803}
804
805/**
9a86e2ba 806 * bitmap_ord_to_pos - find position of n-th set bit in bitmap
fb5eeeee
PJ
807 * @buf: pointer to bitmap
808 * @ord: ordinal bit position (n-th set bit, n >= 0)
f6a1f5db 809 * @nbits: number of valid bit positions in @buf
fb5eeeee
PJ
810 *
811 * Map the ordinal offset of bit @ord in @buf to its position in @buf.
f6a1f5db
RV
812 * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
813 * >= weight(buf), returns @nbits.
fb5eeeee
PJ
814 *
815 * If for example, just bits 4 through 7 are set in @buf, then @ord
816 * values 0 through 3 will get mapped to 4 through 7, respectively,
f6a1f5db 817 * and all other @ord values returns @nbits. When @ord value 3
fb5eeeee
PJ
818 * gets mapped to (returns) @pos value 7 in this example, that means
819 * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
820 *
f6a1f5db 821 * The bit positions 0 through @nbits-1 are valid positions in @buf.
fb5eeeee 822 */
f6a1f5db 823unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
fb5eeeee 824{
f6a1f5db 825 unsigned int pos;
fb5eeeee 826
f6a1f5db
RV
827 for (pos = find_first_bit(buf, nbits);
828 pos < nbits && ord;
829 pos = find_next_bit(buf, nbits, pos + 1))
830 ord--;
fb5eeeee
PJ
831
832 return pos;
833}
834
835/**
836 * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
fb5eeeee 837 * @dst: remapped result
96b7f341 838 * @src: subset to be remapped
fb5eeeee
PJ
839 * @old: defines domain of map
840 * @new: defines range of map
9814ec13 841 * @nbits: number of bits in each of these bitmaps
fb5eeeee
PJ
842 *
843 * Let @old and @new define a mapping of bit positions, such that
844 * whatever position is held by the n-th set bit in @old is mapped
845 * to the n-th set bit in @new. In the more general case, allowing
846 * for the possibility that the weight 'w' of @new is less than the
847 * weight of @old, map the position of the n-th set bit in @old to
848 * the position of the m-th set bit in @new, where m == n % w.
849 *
96b7f341
PJ
850 * If either of the @old and @new bitmaps are empty, or if @src and
851 * @dst point to the same location, then this routine copies @src
852 * to @dst.
fb5eeeee 853 *
96b7f341
PJ
854 * The positions of unset bits in @old are mapped to themselves
855 * (the identify map).
fb5eeeee
PJ
856 *
857 * Apply the above specified mapping to @src, placing the result in
858 * @dst, clearing any bits previously set in @dst.
859 *
fb5eeeee
PJ
860 * For example, lets say that @old has bits 4 through 7 set, and
861 * @new has bits 12 through 15 set. This defines the mapping of bit
862 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
96b7f341
PJ
863 * bit positions unchanged. So if say @src comes into this routine
864 * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
865 * 13 and 15 set.
fb5eeeee
PJ
866 */
867void bitmap_remap(unsigned long *dst, const unsigned long *src,
868 const unsigned long *old, const unsigned long *new,
9814ec13 869 unsigned int nbits)
fb5eeeee 870{
9814ec13 871 unsigned int oldbit, w;
fb5eeeee 872
fb5eeeee
PJ
873 if (dst == src) /* following doesn't handle inplace remaps */
874 return;
9814ec13 875 bitmap_zero(dst, nbits);
96b7f341 876
9814ec13
RV
877 w = bitmap_weight(new, nbits);
878 for_each_set_bit(oldbit, src, nbits) {
879 int n = bitmap_pos_to_ord(old, oldbit, nbits);
08564fb7 880
96b7f341
PJ
881 if (n < 0 || w == 0)
882 set_bit(oldbit, dst); /* identity map */
883 else
9814ec13 884 set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
fb5eeeee
PJ
885 }
886}
fb5eeeee
PJ
887
888/**
889 * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
6e1907ff
RD
890 * @oldbit: bit position to be mapped
891 * @old: defines domain of map
892 * @new: defines range of map
893 * @bits: number of bits in each of these bitmaps
fb5eeeee
PJ
894 *
895 * Let @old and @new define a mapping of bit positions, such that
896 * whatever position is held by the n-th set bit in @old is mapped
897 * to the n-th set bit in @new. In the more general case, allowing
898 * for the possibility that the weight 'w' of @new is less than the
899 * weight of @old, map the position of the n-th set bit in @old to
900 * the position of the m-th set bit in @new, where m == n % w.
901 *
96b7f341
PJ
902 * The positions of unset bits in @old are mapped to themselves
903 * (the identify map).
fb5eeeee
PJ
904 *
905 * Apply the above specified mapping to bit position @oldbit, returning
906 * the new bit position.
907 *
908 * For example, lets say that @old has bits 4 through 7 set, and
909 * @new has bits 12 through 15 set. This defines the mapping of bit
910 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
96b7f341
PJ
911 * bit positions unchanged. So if say @oldbit is 5, then this routine
912 * returns 13.
fb5eeeee
PJ
913 */
914int bitmap_bitremap(int oldbit, const unsigned long *old,
915 const unsigned long *new, int bits)
916{
96b7f341
PJ
917 int w = bitmap_weight(new, bits);
918 int n = bitmap_pos_to_ord(old, oldbit, bits);
919 if (n < 0 || w == 0)
920 return oldbit;
921 else
922 return bitmap_ord_to_pos(new, n % w, bits);
fb5eeeee 923}
fb5eeeee 924
7ea931c9
PJ
925/**
926 * bitmap_onto - translate one bitmap relative to another
927 * @dst: resulting translated bitmap
928 * @orig: original untranslated bitmap
929 * @relmap: bitmap relative to which translated
930 * @bits: number of bits in each of these bitmaps
931 *
932 * Set the n-th bit of @dst iff there exists some m such that the
933 * n-th bit of @relmap is set, the m-th bit of @orig is set, and
934 * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
935 * (If you understood the previous sentence the first time your
936 * read it, you're overqualified for your current job.)
937 *
938 * In other words, @orig is mapped onto (surjectively) @dst,
da3dae54 939 * using the map { <n, m> | the n-th bit of @relmap is the
7ea931c9
PJ
940 * m-th set bit of @relmap }.
941 *
942 * Any set bits in @orig above bit number W, where W is the
943 * weight of (number of set bits in) @relmap are mapped nowhere.
944 * In particular, if for all bits m set in @orig, m >= W, then
945 * @dst will end up empty. In situations where the possibility
946 * of such an empty result is not desired, one way to avoid it is
947 * to use the bitmap_fold() operator, below, to first fold the
948 * @orig bitmap over itself so that all its set bits x are in the
949 * range 0 <= x < W. The bitmap_fold() operator does this by
950 * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
951 *
952 * Example [1] for bitmap_onto():
953 * Let's say @relmap has bits 30-39 set, and @orig has bits
954 * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
955 * @dst will have bits 31, 33, 35, 37 and 39 set.
956 *
957 * When bit 0 is set in @orig, it means turn on the bit in
958 * @dst corresponding to whatever is the first bit (if any)
959 * that is turned on in @relmap. Since bit 0 was off in the
960 * above example, we leave off that bit (bit 30) in @dst.
961 *
962 * When bit 1 is set in @orig (as in the above example), it
963 * means turn on the bit in @dst corresponding to whatever
964 * is the second bit that is turned on in @relmap. The second
965 * bit in @relmap that was turned on in the above example was
966 * bit 31, so we turned on bit 31 in @dst.
967 *
968 * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
969 * because they were the 4th, 6th, 8th and 10th set bits
970 * set in @relmap, and the 4th, 6th, 8th and 10th bits of
971 * @orig (i.e. bits 3, 5, 7 and 9) were also set.
972 *
973 * When bit 11 is set in @orig, it means turn on the bit in
25985edc 974 * @dst corresponding to whatever is the twelfth bit that is
7ea931c9
PJ
975 * turned on in @relmap. In the above example, there were
976 * only ten bits turned on in @relmap (30..39), so that bit
977 * 11 was set in @orig had no affect on @dst.
978 *
979 * Example [2] for bitmap_fold() + bitmap_onto():
40bf19a8 980 * Let's say @relmap has these ten bits set::
981 *
7ea931c9 982 * 40 41 42 43 45 48 53 61 74 95
40bf19a8 983 *
7ea931c9
PJ
984 * (for the curious, that's 40 plus the first ten terms of the
985 * Fibonacci sequence.)
986 *
987 * Further lets say we use the following code, invoking
988 * bitmap_fold() then bitmap_onto, as suggested above to
40bf19a8 989 * avoid the possibility of an empty @dst result::
7ea931c9
PJ
990 *
991 * unsigned long *tmp; // a temporary bitmap's bits
992 *
993 * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
994 * bitmap_onto(dst, tmp, relmap, bits);
995 *
996 * Then this table shows what various values of @dst would be, for
997 * various @orig's. I list the zero-based positions of each set bit.
998 * The tmp column shows the intermediate result, as computed by
999 * using bitmap_fold() to fold the @orig bitmap modulo ten
40bf19a8 1000 * (the weight of @relmap):
7ea931c9 1001 *
40bf19a8 1002 * =============== ============== =================
7ea931c9
PJ
1003 * @orig tmp @dst
1004 * 0 0 40
1005 * 1 1 41
1006 * 9 9 95
40bf19a8 1007 * 10 0 40 [#f1]_
7ea931c9
PJ
1008 * 1 3 5 7 1 3 5 7 41 43 48 61
1009 * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
1010 * 0 9 18 27 0 9 8 7 40 61 74 95
1011 * 0 10 20 30 0 40
1012 * 0 11 22 33 0 1 2 3 40 41 42 43
1013 * 0 12 24 36 0 2 4 6 40 42 45 53
40bf19a8 1014 * 78 102 211 1 2 8 41 42 74 [#f1]_
1015 * =============== ============== =================
1016 *
1017 * .. [#f1]
7ea931c9 1018 *
40bf19a8 1019 * For these marked lines, if we hadn't first done bitmap_fold()
7ea931c9
PJ
1020 * into tmp, then the @dst result would have been empty.
1021 *
1022 * If either of @orig or @relmap is empty (no set bits), then @dst
1023 * will be returned empty.
1024 *
1025 * If (as explained above) the only set bits in @orig are in positions
1026 * m where m >= W, (where W is the weight of @relmap) then @dst will
1027 * once again be returned empty.
1028 *
1029 * All bits in @dst not set by the above rule are cleared.
1030 */
1031void bitmap_onto(unsigned long *dst, const unsigned long *orig,
eb569883 1032 const unsigned long *relmap, unsigned int bits)
7ea931c9 1033{
eb569883 1034 unsigned int n, m; /* same meaning as in above comment */
7ea931c9
PJ
1035
1036 if (dst == orig) /* following doesn't handle inplace mappings */
1037 return;
1038 bitmap_zero(dst, bits);
1039
1040 /*
1041 * The following code is a more efficient, but less
1042 * obvious, equivalent to the loop:
1043 * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
1044 * n = bitmap_ord_to_pos(orig, m, bits);
1045 * if (test_bit(m, orig))
1046 * set_bit(n, dst);
1047 * }
1048 */
1049
1050 m = 0;
08564fb7 1051 for_each_set_bit(n, relmap, bits) {
7ea931c9
PJ
1052 /* m == bitmap_pos_to_ord(relmap, n, bits) */
1053 if (test_bit(m, orig))
1054 set_bit(n, dst);
1055 m++;
1056 }
1057}
7ea931c9
PJ
1058
1059/**
1060 * bitmap_fold - fold larger bitmap into smaller, modulo specified size
1061 * @dst: resulting smaller bitmap
1062 * @orig: original larger bitmap
1063 * @sz: specified size
b26ad583 1064 * @nbits: number of bits in each of these bitmaps
7ea931c9
PJ
1065 *
1066 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
1067 * Clear all other bits in @dst. See further the comment and
1068 * Example [2] for bitmap_onto() for why and how to use this.
1069 */
1070void bitmap_fold(unsigned long *dst, const unsigned long *orig,
b26ad583 1071 unsigned int sz, unsigned int nbits)
7ea931c9 1072{
b26ad583 1073 unsigned int oldbit;
7ea931c9
PJ
1074
1075 if (dst == orig) /* following doesn't handle inplace mappings */
1076 return;
b26ad583 1077 bitmap_zero(dst, nbits);
7ea931c9 1078
b26ad583 1079 for_each_set_bit(oldbit, orig, nbits)
7ea931c9
PJ
1080 set_bit(oldbit % sz, dst);
1081}
cdc90a18 1082#endif /* CONFIG_NUMA */
7ea931c9 1083
3cf64b93
PJ
1084/*
1085 * Common code for bitmap_*_region() routines.
1086 * bitmap: array of unsigned longs corresponding to the bitmap
1087 * pos: the beginning of the region
1088 * order: region size (log base 2 of number of bits)
1089 * reg_op: operation(s) to perform on that region of bitmap
1da177e4 1090 *
3cf64b93
PJ
1091 * Can set, verify and/or release a region of bits in a bitmap,
1092 * depending on which combination of REG_OP_* flag bits is set.
1da177e4 1093 *
3cf64b93
PJ
1094 * A region of a bitmap is a sequence of bits in the bitmap, of
1095 * some size '1 << order' (a power of two), aligned to that same
1096 * '1 << order' power of two.
1097 *
1098 * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
1099 * Returns 0 in all other cases and reg_ops.
1da177e4 1100 */
3cf64b93
PJ
1101
1102enum {
1103 REG_OP_ISFREE, /* true if region is all zero bits */
1104 REG_OP_ALLOC, /* set all bits in region */
1105 REG_OP_RELEASE, /* clear all bits in region */
1106};
1107
9279d328 1108static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
1da177e4 1109{
3cf64b93
PJ
1110 int nbits_reg; /* number of bits in region */
1111 int index; /* index first long of region in bitmap */
1112 int offset; /* bit offset region in bitmap[index] */
1113 int nlongs_reg; /* num longs spanned by region in bitmap */
74373c6a 1114 int nbitsinlong; /* num bits of region in each spanned long */
3cf64b93 1115 unsigned long mask; /* bitmask for one long of region */
74373c6a 1116 int i; /* scans bitmap by longs */
3cf64b93 1117 int ret = 0; /* return value */
74373c6a 1118
3cf64b93
PJ
1119 /*
1120 * Either nlongs_reg == 1 (for small orders that fit in one long)
1121 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
1122 */
1123 nbits_reg = 1 << order;
1124 index = pos / BITS_PER_LONG;
1125 offset = pos - (index * BITS_PER_LONG);
1126 nlongs_reg = BITS_TO_LONGS(nbits_reg);
1127 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
1da177e4 1128
3cf64b93
PJ
1129 /*
1130 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
1131 * overflows if nbitsinlong == BITS_PER_LONG.
1132 */
74373c6a 1133 mask = (1UL << (nbitsinlong - 1));
1da177e4 1134 mask += mask - 1;
3cf64b93 1135 mask <<= offset;
1da177e4 1136
3cf64b93
PJ
1137 switch (reg_op) {
1138 case REG_OP_ISFREE:
1139 for (i = 0; i < nlongs_reg; i++) {
1140 if (bitmap[index + i] & mask)
1141 goto done;
1142 }
1143 ret = 1; /* all bits in region free (zero) */
1144 break;
1145
1146 case REG_OP_ALLOC:
1147 for (i = 0; i < nlongs_reg; i++)
1148 bitmap[index + i] |= mask;
1149 break;
1150
1151 case REG_OP_RELEASE:
1152 for (i = 0; i < nlongs_reg; i++)
1153 bitmap[index + i] &= ~mask;
1154 break;
1da177e4 1155 }
3cf64b93
PJ
1156done:
1157 return ret;
1158}
1159
1160/**
1161 * bitmap_find_free_region - find a contiguous aligned mem region
1162 * @bitmap: array of unsigned longs corresponding to the bitmap
1163 * @bits: number of bits in the bitmap
1164 * @order: region size (log base 2 of number of bits) to find
1165 *
1166 * Find a region of free (zero) bits in a @bitmap of @bits bits and
1167 * allocate them (set them to one). Only consider regions of length
1168 * a power (@order) of two, aligned to that power of two, which
1169 * makes the search algorithm much faster.
1170 *
1171 * Return the bit offset in bitmap of the allocated region,
1172 * or -errno on failure.
1173 */
9279d328 1174int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
3cf64b93 1175{
9279d328 1176 unsigned int pos, end; /* scans bitmap by regions of size order */
aa8e4fc6 1177
9279d328 1178 for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
aa8e4fc6
LT
1179 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1180 continue;
1181 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1182 return pos;
1183 }
1184 return -ENOMEM;
1da177e4
LT
1185}
1186EXPORT_SYMBOL(bitmap_find_free_region);
1187
1188/**
87e24802 1189 * bitmap_release_region - release allocated bitmap region
3cf64b93
PJ
1190 * @bitmap: array of unsigned longs corresponding to the bitmap
1191 * @pos: beginning of bit region to release
1192 * @order: region size (log base 2 of number of bits) to release
1da177e4 1193 *
72fd4a35 1194 * This is the complement to __bitmap_find_free_region() and releases
1da177e4 1195 * the found region (by clearing it in the bitmap).
3cf64b93
PJ
1196 *
1197 * No return value.
1da177e4 1198 */
9279d328 1199void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
1da177e4 1200{
3cf64b93 1201 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
1da177e4
LT
1202}
1203EXPORT_SYMBOL(bitmap_release_region);
1204
87e24802
PJ
1205/**
1206 * bitmap_allocate_region - allocate bitmap region
3cf64b93
PJ
1207 * @bitmap: array of unsigned longs corresponding to the bitmap
1208 * @pos: beginning of bit region to allocate
1209 * @order: region size (log base 2 of number of bits) to allocate
87e24802
PJ
1210 *
1211 * Allocate (set bits in) a specified region of a bitmap.
3cf64b93 1212 *
6e1907ff 1213 * Return 0 on success, or %-EBUSY if specified region wasn't
87e24802
PJ
1214 * free (not all bits were zero).
1215 */
9279d328 1216int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
1da177e4 1217{
3cf64b93
PJ
1218 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1219 return -EBUSY;
2ac521d3 1220 return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1da177e4
LT
1221}
1222EXPORT_SYMBOL(bitmap_allocate_region);
ccbe329b
DV
1223
1224/**
1225 * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
1226 * @dst: destination buffer
1227 * @src: bitmap to copy
1228 * @nbits: number of bits in the bitmap
1229 *
1230 * Require nbits % BITS_PER_LONG == 0.
1231 */
e8f24278 1232#ifdef __BIG_ENDIAN
9b6c2d2e 1233void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
ccbe329b 1234{
9b6c2d2e 1235 unsigned int i;
ccbe329b
DV
1236
1237 for (i = 0; i < nbits/BITS_PER_LONG; i++) {
1238 if (BITS_PER_LONG == 64)
9b6c2d2e 1239 dst[i] = cpu_to_le64(src[i]);
ccbe329b 1240 else
9b6c2d2e 1241 dst[i] = cpu_to_le32(src[i]);
ccbe329b
DV
1242 }
1243}
1244EXPORT_SYMBOL(bitmap_copy_le);
e8f24278 1245#endif
c724f193 1246
c42b65e3
AS
1247unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
1248{
1249 return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
1250 flags);
1251}
1252EXPORT_SYMBOL(bitmap_alloc);
1253
1254unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
1255{
1256 return bitmap_alloc(nbits, flags | __GFP_ZERO);
1257}
1258EXPORT_SYMBOL(bitmap_zalloc);
1259
1260void bitmap_free(const unsigned long *bitmap)
1261{
1262 kfree(bitmap);
1263}
1264EXPORT_SYMBOL(bitmap_free);
1265
c724f193
YN
1266#if BITS_PER_LONG == 64
1267/**
1268 * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
1269 * @bitmap: array of unsigned longs, the destination bitmap
1270 * @buf: array of u32 (in host byte order), the source bitmap
1271 * @nbits: number of bits in @bitmap
1272 */
ccf7a6d4 1273void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
c724f193
YN
1274{
1275 unsigned int i, halfwords;
1276
c724f193
YN
1277 halfwords = DIV_ROUND_UP(nbits, 32);
1278 for (i = 0; i < halfwords; i++) {
1279 bitmap[i/2] = (unsigned long) buf[i];
1280 if (++i < halfwords)
1281 bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
1282 }
1283
1284 /* Clear tail bits in last word beyond nbits. */
1285 if (nbits % BITS_PER_LONG)
1286 bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
1287}
1288EXPORT_SYMBOL(bitmap_from_arr32);
1289
1290/**
1291 * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
1292 * @buf: array of u32 (in host byte order), the dest bitmap
1293 * @bitmap: array of unsigned longs, the source bitmap
1294 * @nbits: number of bits in @bitmap
1295 */
1296void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
1297{
1298 unsigned int i, halfwords;
1299
c724f193
YN
1300 halfwords = DIV_ROUND_UP(nbits, 32);
1301 for (i = 0; i < halfwords; i++) {
1302 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
1303 if (++i < halfwords)
1304 buf[i] = (u32) (bitmap[i/2] >> 32);
1305 }
1306
1307 /* Clear tail bits in last element of array beyond nbits. */
1308 if (nbits % BITS_PER_LONG)
1309 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
1310}
1311EXPORT_SYMBOL(bitmap_to_arr32);
1312
1313#endif