bitmap.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * lib/bitmap.c
  3. * Helper functions for bitmap.h.
  4. *
  5. * This source code is licensed under the GNU General Public License,
  6. * Version 2. See the file COPYING for more details.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/ctype.h>
  10. #include <linux/errno.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/bitops.h>
  13. #include <asm/uaccess.h>
  14. /*
  15. * bitmaps provide an array of bits, implemented using an an
  16. * array of unsigned longs. The number of valid bits in a
  17. * given bitmap does _not_ need to be an exact multiple of
  18. * BITS_PER_LONG.
  19. *
  20. * The possible unused bits in the last, partially used word
  21. * of a bitmap are 'don't care'. The implementation makes
  22. * no particular effort to keep them zero. It ensures that
  23. * their value will not affect the results of any operation.
  24. * The bitmap operations that return Boolean (bitmap_empty,
  25. * for example) or scalar (bitmap_weight, for example) results
  26. * carefully filter out these unused bits from impacting their
  27. * results.
  28. *
  29. * These operations actually hold to a slightly stronger rule:
  30. * if you don't input any bitmaps to these ops that have some
  31. * unused bits set, then they won't output any set unused bits
  32. * in output bitmaps.
  33. *
  34. * The byte ordering of bitmaps is more natural on little
  35. * endian architectures. See the big-endian headers
  36. * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
  37. * for the best explanations of this ordering.
  38. */
  39. int __bitmap_empty(const unsigned long *bitmap, int bits)
  40. {
  41. int k, lim = bits/BITS_PER_LONG;
  42. for (k = 0; k < lim; ++k)
  43. if (bitmap[k])
  44. return 0;
  45. if (bits % BITS_PER_LONG)
  46. if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
  47. return 0;
  48. return 1;
  49. }
  50. EXPORT_SYMBOL(__bitmap_empty);
  51. int __bitmap_full(const unsigned long *bitmap, int bits)
  52. {
  53. int k, lim = bits/BITS_PER_LONG;
  54. for (k = 0; k < lim; ++k)
  55. if (~bitmap[k])
  56. return 0;
  57. if (bits % BITS_PER_LONG)
  58. if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
  59. return 0;
  60. return 1;
  61. }
  62. EXPORT_SYMBOL(__bitmap_full);
  63. int __bitmap_equal(const unsigned long *bitmap1,
  64. const unsigned long *bitmap2, int bits)
  65. {
  66. int k, lim = bits/BITS_PER_LONG;
  67. for (k = 0; k < lim; ++k)
  68. if (bitmap1[k] != bitmap2[k])
  69. return 0;
  70. if (bits % BITS_PER_LONG)
  71. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  72. return 0;
  73. return 1;
  74. }
  75. EXPORT_SYMBOL(__bitmap_equal);
  76. void __bitmap_complement(unsigned long *dst, const unsigned long *src, int bits)
  77. {
  78. int k, lim = bits/BITS_PER_LONG;
  79. for (k = 0; k < lim; ++k)
  80. dst[k] = ~src[k];
  81. if (bits % BITS_PER_LONG)
  82. dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits);
  83. }
  84. EXPORT_SYMBOL(__bitmap_complement);
  85. /**
  86. * __bitmap_shift_right - logical right shift of the bits in a bitmap
  87. * @dst : destination bitmap
  88. * @src : source bitmap
  89. * @shift : shift by this many bits
  90. * @bits : bitmap size, in bits
  91. *
  92. * Shifting right (dividing) means moving bits in the MS -> LS bit
  93. * direction. Zeros are fed into the vacated MS positions and the
  94. * LS bits shifted off the bottom are lost.
  95. */
  96. void __bitmap_shift_right(unsigned long *dst,
  97. const unsigned long *src, int shift, int bits)
  98. {
  99. int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
  100. int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  101. unsigned long mask = (1UL << left) - 1;
  102. for (k = 0; off + k < lim; ++k) {
  103. unsigned long upper, lower;
  104. /*
  105. * If shift is not word aligned, take lower rem bits of
  106. * word above and make them the top rem bits of result.
  107. */
  108. if (!rem || off + k + 1 >= lim)
  109. upper = 0;
  110. else {
  111. upper = src[off + k + 1];
  112. if (off + k + 1 == lim - 1 && left)
  113. upper &= mask;
  114. }
  115. lower = src[off + k];
  116. if (left && off + k == lim - 1)
  117. lower &= mask;
  118. dst[k] = upper << (BITS_PER_LONG - rem) | lower >> rem;
  119. if (left && k == lim - 1)
  120. dst[k] &= mask;
  121. }
  122. if (off)
  123. memset(&dst[lim - off], 0, off*sizeof(unsigned long));
  124. }
  125. EXPORT_SYMBOL(__bitmap_shift_right);
  126. /**
  127. * __bitmap_shift_left - logical left shift of the bits in a bitmap
  128. * @dst : destination bitmap
  129. * @src : source bitmap
  130. * @shift : shift by this many bits
  131. * @bits : bitmap size, in bits
  132. *
  133. * Shifting left (multiplying) means moving bits in the LS -> MS
  134. * direction. Zeros are fed into the vacated LS bit positions
  135. * and those MS bits shifted off the top are lost.
  136. */
  137. void __bitmap_shift_left(unsigned long *dst,
  138. const unsigned long *src, int shift, int bits)
  139. {
  140. int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
  141. int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  142. for (k = lim - off - 1; k >= 0; --k) {
  143. unsigned long upper, lower;
  144. /*
  145. * If shift is not word aligned, take upper rem bits of
  146. * word below and make them the bottom rem bits of result.
  147. */
  148. if (rem && k > 0)
  149. lower = src[k - 1];
  150. else
  151. lower = 0;
  152. upper = src[k];
  153. if (left && k == lim - 1)
  154. upper &= (1UL << left) - 1;
  155. dst[k + off] = lower >> (BITS_PER_LONG - rem) | upper << rem;
  156. if (left && k + off == lim - 1)
  157. dst[k + off] &= (1UL << left) - 1;
  158. }
  159. if (off)
  160. memset(dst, 0, off*sizeof(unsigned long));
  161. }
  162. EXPORT_SYMBOL(__bitmap_shift_left);
  163. void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  164. const unsigned long *bitmap2, int bits)
  165. {
  166. int k;
  167. int nr = BITS_TO_LONGS(bits);
  168. for (k = 0; k < nr; k++)
  169. dst[k] = bitmap1[k] & bitmap2[k];
  170. }
  171. EXPORT_SYMBOL(__bitmap_and);
  172. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  173. const unsigned long *bitmap2, int bits)
  174. {
  175. int k;
  176. int nr = BITS_TO_LONGS(bits);
  177. for (k = 0; k < nr; k++)
  178. dst[k] = bitmap1[k] | bitmap2[k];
  179. }
  180. EXPORT_SYMBOL(__bitmap_or);
  181. void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  182. const unsigned long *bitmap2, int bits)
  183. {
  184. int k;
  185. int nr = BITS_TO_LONGS(bits);
  186. for (k = 0; k < nr; k++)
  187. dst[k] = bitmap1[k] ^ bitmap2[k];
  188. }
  189. EXPORT_SYMBOL(__bitmap_xor);
  190. void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  191. const unsigned long *bitmap2, int bits)
  192. {
  193. int k;
  194. int nr = BITS_TO_LONGS(bits);
  195. for (k = 0; k < nr; k++)
  196. dst[k] = bitmap1[k] & ~bitmap2[k];
  197. }
  198. EXPORT_SYMBOL(__bitmap_andnot);
  199. int __bitmap_intersects(const unsigned long *bitmap1,
  200. const unsigned long *bitmap2, int bits)
  201. {
  202. int k, lim = bits/BITS_PER_LONG;
  203. for (k = 0; k < lim; ++k)
  204. if (bitmap1[k] & bitmap2[k])
  205. return 1;
  206. if (bits % BITS_PER_LONG)
  207. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  208. return 1;
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(__bitmap_intersects);
  212. int __bitmap_subset(const unsigned long *bitmap1,
  213. const unsigned long *bitmap2, int bits)
  214. {
  215. int k, lim = bits/BITS_PER_LONG;
  216. for (k = 0; k < lim; ++k)
  217. if (bitmap1[k] & ~bitmap2[k])
  218. return 0;
  219. if (bits % BITS_PER_LONG)
  220. if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  221. return 0;
  222. return 1;
  223. }
  224. EXPORT_SYMBOL(__bitmap_subset);
  225. int __bitmap_weight(const unsigned long *bitmap, int bits)
  226. {
  227. int k, w = 0, lim = bits/BITS_PER_LONG;
  228. for (k = 0; k < lim; k++)
  229. w += hweight_long(bitmap[k]);
  230. if (bits % BITS_PER_LONG)
  231. w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
  232. return w;
  233. }
  234. EXPORT_SYMBOL(__bitmap_weight);
  235. /*
  236. * Bitmap printing & parsing functions: first version by Bill Irwin,
  237. * second version by Paul Jackson, third by Joe Korty.
  238. */
  239. #define CHUNKSZ 32
  240. #define nbits_to_hold_value(val) fls(val)
  241. #define unhex(c) (isdigit(c) ? (c - '0') : (toupper(c) - 'A' + 10))
  242. #define BASEDEC 10 /* fancier cpuset lists input in decimal */
  243. /**
  244. * bitmap_scnprintf - convert bitmap to an ASCII hex string.
  245. * @buf: byte buffer into which string is placed
  246. * @buflen: reserved size of @buf, in bytes
  247. * @maskp: pointer to bitmap to convert
  248. * @nmaskbits: size of bitmap, in bits
  249. *
  250. * Exactly @nmaskbits bits are displayed. Hex digits are grouped into
  251. * comma-separated sets of eight digits per set.
  252. */
  253. int bitmap_scnprintf(char *buf, unsigned int buflen,
  254. const unsigned long *maskp, int nmaskbits)
  255. {
  256. int i, word, bit, len = 0;
  257. unsigned long val;
  258. const char *sep = "";
  259. int chunksz;
  260. u32 chunkmask;
  261. chunksz = nmaskbits & (CHUNKSZ - 1);
  262. if (chunksz == 0)
  263. chunksz = CHUNKSZ;
  264. i = ALIGN(nmaskbits, CHUNKSZ) - CHUNKSZ;
  265. for (; i >= 0; i -= CHUNKSZ) {
  266. chunkmask = ((1ULL << chunksz) - 1);
  267. word = i / BITS_PER_LONG;
  268. bit = i % BITS_PER_LONG;
  269. val = (maskp[word] >> bit) & chunkmask;
  270. len += scnprintf(buf+len, buflen-len, "%s%0*lx", sep,
  271. (chunksz+3)/4, val);
  272. chunksz = CHUNKSZ;
  273. sep = ",";
  274. }
  275. return len;
  276. }
  277. EXPORT_SYMBOL(bitmap_scnprintf);
  278. /**
  279. * bitmap_scnprintf_len - return buffer length needed to convert
  280. * bitmap to an ASCII hex string.
  281. * @len: number of bits to be converted
  282. */
  283. int bitmap_scnprintf_len(unsigned int len)
  284. {
  285. /* we need 9 chars per word for 32 bit words (8 hexdigits + sep/null) */
  286. int bitslen = ALIGN(len, CHUNKSZ);
  287. int wordlen = CHUNKSZ / 4;
  288. int buflen = (bitslen / wordlen) * (wordlen + 1) * sizeof(char);
  289. return buflen;
  290. }
  291. EXPORT_SYMBOL(bitmap_scnprintf_len);
  292. /**
  293. * __bitmap_parse - convert an ASCII hex string into a bitmap.
  294. * @buf: pointer to buffer containing string.
  295. * @buflen: buffer size in bytes. If string is smaller than this
  296. * then it must be terminated with a \0.
  297. * @is_user: location of buffer, 0 indicates kernel space
  298. * @maskp: pointer to bitmap array that will contain result.
  299. * @nmaskbits: size of bitmap, in bits.
  300. *
  301. * Commas group hex digits into chunks. Each chunk defines exactly 32
  302. * bits of the resultant bitmask. No chunk may specify a value larger
  303. * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
  304. * then leading 0-bits are prepended. %-EINVAL is returned for illegal
  305. * characters and for grouping errors such as "1,,5", ",44", "," and "".
  306. * Leading and trailing whitespace accepted, but not embedded whitespace.
  307. */
  308. int __bitmap_parse(const char *buf, unsigned int buflen,
  309. int is_user, unsigned long *maskp,
  310. int nmaskbits)
  311. {
  312. int c, old_c, totaldigits, ndigits, nchunks, nbits;
  313. u32 chunk;
  314. const char __user *ubuf = buf;
  315. bitmap_zero(maskp, nmaskbits);
  316. nchunks = nbits = totaldigits = c = 0;
  317. do {
  318. chunk = ndigits = 0;
  319. /* Get the next chunk of the bitmap */
  320. while (buflen) {
  321. old_c = c;
  322. if (is_user) {
  323. if (__get_user(c, ubuf++))
  324. return -EFAULT;
  325. }
  326. else
  327. c = *buf++;
  328. buflen--;
  329. if (isspace(c))
  330. continue;
  331. /*
  332. * If the last character was a space and the current
  333. * character isn't '\0', we've got embedded whitespace.
  334. * This is a no-no, so throw an error.
  335. */
  336. if (totaldigits && c && isspace(old_c))
  337. return -EINVAL;
  338. /* A '\0' or a ',' signal the end of the chunk */
  339. if (c == '\0' || c == ',')
  340. break;
  341. if (!isxdigit(c))
  342. return -EINVAL;
  343. /*
  344. * Make sure there are at least 4 free bits in 'chunk'.
  345. * If not, this hexdigit will overflow 'chunk', so
  346. * throw an error.
  347. */
  348. if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1))
  349. return -EOVERFLOW;
  350. chunk = (chunk << 4) | unhex(c);
  351. ndigits++; totaldigits++;
  352. }
  353. if (ndigits == 0)
  354. return -EINVAL;
  355. if (nchunks == 0 && chunk == 0)
  356. continue;
  357. __bitmap_shift_left(maskp, maskp, CHUNKSZ, nmaskbits);
  358. *maskp |= chunk;
  359. nchunks++;
  360. nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ;
  361. if (nbits > nmaskbits)
  362. return -EOVERFLOW;
  363. } while (buflen && c == ',');
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(__bitmap_parse);
  367. /**
  368. * bitmap_parse_user()
  369. *
  370. * @ubuf: pointer to user buffer containing string.
  371. * @ulen: buffer size in bytes. If string is smaller than this
  372. * then it must be terminated with a \0.
  373. * @maskp: pointer to bitmap array that will contain result.
  374. * @nmaskbits: size of bitmap, in bits.
  375. *
  376. * Wrapper for __bitmap_parse(), providing it with user buffer.
  377. *
  378. * We cannot have this as an inline function in bitmap.h because it needs
  379. * linux/uaccess.h to get the access_ok() declaration and this causes
  380. * cyclic dependencies.
  381. */
  382. int bitmap_parse_user(const char __user *ubuf,
  383. unsigned int ulen, unsigned long *maskp,
  384. int nmaskbits)
  385. {
  386. if (!access_ok(VERIFY_READ, ubuf, ulen))
  387. return -EFAULT;
  388. return __bitmap_parse((const char *)ubuf, ulen, 1, maskp, nmaskbits);
  389. }
  390. EXPORT_SYMBOL(bitmap_parse_user);
  391. /*
  392. * bscnl_emit(buf, buflen, rbot, rtop, bp)
  393. *
  394. * Helper routine for bitmap_scnlistprintf(). Write decimal number
  395. * or range to buf, suppressing output past buf+buflen, with optional
  396. * comma-prefix. Return len of what would be written to buf, if it
  397. * all fit.
  398. */
  399. static inline int bscnl_emit(char *buf, int buflen, int rbot, int rtop, int len)
  400. {
  401. if (len > 0)
  402. len += scnprintf(buf + len, buflen - len, ",");
  403. if (rbot == rtop)
  404. len += scnprintf(buf + len, buflen - len, "%d", rbot);
  405. else
  406. len += scnprintf(buf + len, buflen - len, "%d-%d", rbot, rtop);
  407. return len;
  408. }
  409. /**
  410. * bitmap_scnlistprintf - convert bitmap to list format ASCII string
  411. * @buf: byte buffer into which string is placed
  412. * @buflen: reserved size of @buf, in bytes
  413. * @maskp: pointer to bitmap to convert
  414. * @nmaskbits: size of bitmap, in bits
  415. *
  416. * Output format is a comma-separated list of decimal numbers and
  417. * ranges. Consecutively set bits are shown as two hyphen-separated
  418. * decimal numbers, the smallest and largest bit numbers set in
  419. * the range. Output format is compatible with the format
  420. * accepted as input by bitmap_parselist().
  421. *
  422. * The return value is the number of characters which would be
  423. * generated for the given input, excluding the trailing '\0', as
  424. * per ISO C99.
  425. */
  426. int bitmap_scnlistprintf(char *buf, unsigned int buflen,
  427. const unsigned long *maskp, int nmaskbits)
  428. {
  429. int len = 0;
  430. /* current bit is 'cur', most recently seen range is [rbot, rtop] */
  431. int cur, rbot, rtop;
  432. if (buflen == 0)
  433. return 0;
  434. buf[0] = 0;
  435. rbot = cur = find_first_bit(maskp, nmaskbits);
  436. while (cur < nmaskbits) {
  437. rtop = cur;
  438. cur = find_next_bit(maskp, nmaskbits, cur+1);
  439. if (cur >= nmaskbits || cur > rtop + 1) {
  440. len = bscnl_emit(buf, buflen, rbot, rtop, len);
  441. rbot = cur;
  442. }
  443. }
  444. return len;
  445. }
  446. EXPORT_SYMBOL(bitmap_scnlistprintf);
  447. /**
  448. * bitmap_parselist - convert list format ASCII string to bitmap
  449. * @bp: read nul-terminated user string from this buffer
  450. * @maskp: write resulting mask here
  451. * @nmaskbits: number of bits in mask to be written
  452. *
  453. * Input format is a comma-separated list of decimal numbers and
  454. * ranges. Consecutively set bits are shown as two hyphen-separated
  455. * decimal numbers, the smallest and largest bit numbers set in
  456. * the range.
  457. *
  458. * Returns 0 on success, -errno on invalid input strings.
  459. * Error values:
  460. * %-EINVAL: second number in range smaller than first
  461. * %-EINVAL: invalid character in string
  462. * %-ERANGE: bit number specified too large for mask
  463. */
  464. int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits)
  465. {
  466. unsigned a, b;
  467. bitmap_zero(maskp, nmaskbits);
  468. do {
  469. if (!isdigit(*bp))
  470. return -EINVAL;
  471. b = a = simple_strtoul(bp, (char **)&bp, BASEDEC);
  472. if (*bp == '-') {
  473. bp++;
  474. if (!isdigit(*bp))
  475. return -EINVAL;
  476. b = simple_strtoul(bp, (char **)&bp, BASEDEC);
  477. }
  478. if (!(a <= b))
  479. return -EINVAL;
  480. if (b >= nmaskbits)
  481. return -ERANGE;
  482. while (a <= b) {
  483. set_bit(a, maskp);
  484. a++;
  485. }
  486. if (*bp == ',')
  487. bp++;
  488. } while (*bp != '\0' && *bp != '\n');
  489. return 0;
  490. }
  491. EXPORT_SYMBOL(bitmap_parselist);
  492. /**
  493. * bitmap_pos_to_ord(buf, pos, bits)
  494. * @buf: pointer to a bitmap
  495. * @pos: a bit position in @buf (0 <= @pos < @bits)
  496. * @bits: number of valid bit positions in @buf
  497. *
  498. * Map the bit at position @pos in @buf (of length @bits) to the
  499. * ordinal of which set bit it is. If it is not set or if @pos
  500. * is not a valid bit position, map to -1.
  501. *
  502. * If for example, just bits 4 through 7 are set in @buf, then @pos
  503. * values 4 through 7 will get mapped to 0 through 3, respectively,
  504. * and other @pos values will get mapped to 0. When @pos value 7
  505. * gets mapped to (returns) @ord value 3 in this example, that means
  506. * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  507. *
  508. * The bit positions 0 through @bits are valid positions in @buf.
  509. */
  510. static int bitmap_pos_to_ord(const unsigned long *buf, int pos, int bits)
  511. {
  512. int i, ord;
  513. if (pos < 0 || pos >= bits || !test_bit(pos, buf))
  514. return -1;
  515. i = find_first_bit(buf, bits);
  516. ord = 0;
  517. while (i < pos) {
  518. i = find_next_bit(buf, bits, i + 1);
  519. ord++;
  520. }
  521. BUG_ON(i != pos);
  522. return ord;
  523. }
  524. /**
  525. * bitmap_ord_to_pos(buf, ord, bits)
  526. * @buf: pointer to bitmap
  527. * @ord: ordinal bit position (n-th set bit, n >= 0)
  528. * @bits: number of valid bit positions in @buf
  529. *
  530. * Map the ordinal offset of bit @ord in @buf to its position in @buf.
  531. * Value of @ord should be in range 0 <= @ord < weight(buf), else
  532. * results are undefined.
  533. *
  534. * If for example, just bits 4 through 7 are set in @buf, then @ord
  535. * values 0 through 3 will get mapped to 4 through 7, respectively,
  536. * and all other @ord values return undefined values. When @ord value 3
  537. * gets mapped to (returns) @pos value 7 in this example, that means
  538. * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
  539. *
  540. * The bit positions 0 through @bits are valid positions in @buf.
  541. */
  542. static int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
  543. {
  544. int pos = 0;
  545. if (ord >= 0 && ord < bits) {
  546. int i;
  547. for (i = find_first_bit(buf, bits);
  548. i < bits && ord > 0;
  549. i = find_next_bit(buf, bits, i + 1))
  550. ord--;
  551. if (i < bits && ord == 0)
  552. pos = i;
  553. }
  554. return pos;
  555. }
  556. /**
  557. * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  558. * @dst: remapped result
  559. * @src: subset to be remapped
  560. * @old: defines domain of map
  561. * @new: defines range of map
  562. * @bits: number of bits in each of these bitmaps
  563. *
  564. * Let @old and @new define a mapping of bit positions, such that
  565. * whatever position is held by the n-th set bit in @old is mapped
  566. * to the n-th set bit in @new. In the more general case, allowing
  567. * for the possibility that the weight 'w' of @new is less than the
  568. * weight of @old, map the position of the n-th set bit in @old to
  569. * the position of the m-th set bit in @new, where m == n % w.
  570. *
  571. * If either of the @old and @new bitmaps are empty, or if @src and
  572. * @dst point to the same location, then this routine copies @src
  573. * to @dst.
  574. *
  575. * The positions of unset bits in @old are mapped to themselves
  576. * (the identify map).
  577. *
  578. * Apply the above specified mapping to @src, placing the result in
  579. * @dst, clearing any bits previously set in @dst.
  580. *
  581. * For example, lets say that @old has bits 4 through 7 set, and
  582. * @new has bits 12 through 15 set. This defines the mapping of bit
  583. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  584. * bit positions unchanged. So if say @src comes into this routine
  585. * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
  586. * 13 and 15 set.
  587. */
  588. void bitmap_remap(unsigned long *dst, const unsigned long *src,
  589. const unsigned long *old, const unsigned long *new,
  590. int bits)
  591. {
  592. int oldbit, w;
  593. if (dst == src) /* following doesn't handle inplace remaps */
  594. return;
  595. bitmap_zero(dst, bits);
  596. w = bitmap_weight(new, bits);
  597. for (oldbit = find_first_bit(src, bits);
  598. oldbit < bits;
  599. oldbit = find_next_bit(src, bits, oldbit + 1)) {
  600. int n = bitmap_pos_to_ord(old, oldbit, bits);
  601. if (n < 0 || w == 0)
  602. set_bit(oldbit, dst); /* identity map */
  603. else
  604. set_bit(bitmap_ord_to_pos(new, n % w, bits), dst);
  605. }
  606. }
  607. EXPORT_SYMBOL(bitmap_remap);
  608. /**
  609. * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
  610. * @oldbit: bit position to be mapped
  611. * @old: defines domain of map
  612. * @new: defines range of map
  613. * @bits: number of bits in each of these bitmaps
  614. *
  615. * Let @old and @new define a mapping of bit positions, such that
  616. * whatever position is held by the n-th set bit in @old is mapped
  617. * to the n-th set bit in @new. In the more general case, allowing
  618. * for the possibility that the weight 'w' of @new is less than the
  619. * weight of @old, map the position of the n-th set bit in @old to
  620. * the position of the m-th set bit in @new, where m == n % w.
  621. *
  622. * The positions of unset bits in @old are mapped to themselves
  623. * (the identify map).
  624. *
  625. * Apply the above specified mapping to bit position @oldbit, returning
  626. * the new bit position.
  627. *
  628. * For example, lets say that @old has bits 4 through 7 set, and
  629. * @new has bits 12 through 15 set. This defines the mapping of bit
  630. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  631. * bit positions unchanged. So if say @oldbit is 5, then this routine
  632. * returns 13.
  633. */
  634. int bitmap_bitremap(int oldbit, const unsigned long *old,
  635. const unsigned long *new, int bits)
  636. {
  637. int w = bitmap_weight(new, bits);
  638. int n = bitmap_pos_to_ord(old, oldbit, bits);
  639. if (n < 0 || w == 0)
  640. return oldbit;
  641. else
  642. return bitmap_ord_to_pos(new, n % w, bits);
  643. }
  644. EXPORT_SYMBOL(bitmap_bitremap);
  645. /*
  646. * Common code for bitmap_*_region() routines.
  647. * bitmap: array of unsigned longs corresponding to the bitmap
  648. * pos: the beginning of the region
  649. * order: region size (log base 2 of number of bits)
  650. * reg_op: operation(s) to perform on that region of bitmap
  651. *
  652. * Can set, verify and/or release a region of bits in a bitmap,
  653. * depending on which combination of REG_OP_* flag bits is set.
  654. *
  655. * A region of a bitmap is a sequence of bits in the bitmap, of
  656. * some size '1 << order' (a power of two), aligned to that same
  657. * '1 << order' power of two.
  658. *
  659. * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
  660. * Returns 0 in all other cases and reg_ops.
  661. */
  662. enum {
  663. REG_OP_ISFREE, /* true if region is all zero bits */
  664. REG_OP_ALLOC, /* set all bits in region */
  665. REG_OP_RELEASE, /* clear all bits in region */
  666. };
  667. static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
  668. {
  669. int nbits_reg; /* number of bits in region */
  670. int index; /* index first long of region in bitmap */
  671. int offset; /* bit offset region in bitmap[index] */
  672. int nlongs_reg; /* num longs spanned by region in bitmap */
  673. int nbitsinlong; /* num bits of region in each spanned long */
  674. unsigned long mask; /* bitmask for one long of region */
  675. int i; /* scans bitmap by longs */
  676. int ret = 0; /* return value */
  677. /*
  678. * Either nlongs_reg == 1 (for small orders that fit in one long)
  679. * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
  680. */
  681. nbits_reg = 1 << order;
  682. index = pos / BITS_PER_LONG;
  683. offset = pos - (index * BITS_PER_LONG);
  684. nlongs_reg = BITS_TO_LONGS(nbits_reg);
  685. nbitsinlong = min(nbits_reg, BITS_PER_LONG);
  686. /*
  687. * Can't do "mask = (1UL << nbitsinlong) - 1", as that
  688. * overflows if nbitsinlong == BITS_PER_LONG.
  689. */
  690. mask = (1UL << (nbitsinlong - 1));
  691. mask += mask - 1;
  692. mask <<= offset;
  693. switch (reg_op) {
  694. case REG_OP_ISFREE:
  695. for (i = 0; i < nlongs_reg; i++) {
  696. if (bitmap[index + i] & mask)
  697. goto done;
  698. }
  699. ret = 1; /* all bits in region free (zero) */
  700. break;
  701. case REG_OP_ALLOC:
  702. for (i = 0; i < nlongs_reg; i++)
  703. bitmap[index + i] |= mask;
  704. break;
  705. case REG_OP_RELEASE:
  706. for (i = 0; i < nlongs_reg; i++)
  707. bitmap[index + i] &= ~mask;
  708. break;
  709. }
  710. done:
  711. return ret;
  712. }
  713. /**
  714. * bitmap_find_free_region - find a contiguous aligned mem region
  715. * @bitmap: array of unsigned longs corresponding to the bitmap
  716. * @bits: number of bits in the bitmap
  717. * @order: region size (log base 2 of number of bits) to find
  718. *
  719. * Find a region of free (zero) bits in a @bitmap of @bits bits and
  720. * allocate them (set them to one). Only consider regions of length
  721. * a power (@order) of two, aligned to that power of two, which
  722. * makes the search algorithm much faster.
  723. *
  724. * Return the bit offset in bitmap of the allocated region,
  725. * or -errno on failure.
  726. */
  727. int bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
  728. {
  729. int pos; /* scans bitmap by regions of size order */
  730. for (pos = 0; pos < bits; pos += (1 << order))
  731. if (__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  732. break;
  733. if (pos == bits)
  734. return -ENOMEM;
  735. __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  736. return pos;
  737. }
  738. EXPORT_SYMBOL(bitmap_find_free_region);
  739. /**
  740. * bitmap_release_region - release allocated bitmap region
  741. * @bitmap: array of unsigned longs corresponding to the bitmap
  742. * @pos: beginning of bit region to release
  743. * @order: region size (log base 2 of number of bits) to release
  744. *
  745. * This is the complement to __bitmap_find_free_region() and releases
  746. * the found region (by clearing it in the bitmap).
  747. *
  748. * No return value.
  749. */
  750. void bitmap_release_region(unsigned long *bitmap, int pos, int order)
  751. {
  752. __reg_op(bitmap, pos, order, REG_OP_RELEASE);
  753. }
  754. EXPORT_SYMBOL(bitmap_release_region);
  755. /**
  756. * bitmap_allocate_region - allocate bitmap region
  757. * @bitmap: array of unsigned longs corresponding to the bitmap
  758. * @pos: beginning of bit region to allocate
  759. * @order: region size (log base 2 of number of bits) to allocate
  760. *
  761. * Allocate (set bits in) a specified region of a bitmap.
  762. *
  763. * Return 0 on success, or %-EBUSY if specified region wasn't
  764. * free (not all bits were zero).
  765. */
  766. int bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
  767. {
  768. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  769. return -EBUSY;
  770. __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  771. return 0;
  772. }
  773. EXPORT_SYMBOL(bitmap_allocate_region);