bitmap.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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/export.h>
  9. #include <linux/thread_info.h>
  10. #include <linux/ctype.h>
  11. #include <linux/errno.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/bitops.h>
  14. #include <linux/bug.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/page.h>
  19. /**
  20. * DOC: bitmap introduction
  21. *
  22. * bitmaps provide an array of bits, implemented using an an
  23. * array of unsigned longs. The number of valid bits in a
  24. * given bitmap does _not_ need to be an exact multiple of
  25. * BITS_PER_LONG.
  26. *
  27. * The possible unused bits in the last, partially used word
  28. * of a bitmap are 'don't care'. The implementation makes
  29. * no particular effort to keep them zero. It ensures that
  30. * their value will not affect the results of any operation.
  31. * The bitmap operations that return Boolean (bitmap_empty,
  32. * for example) or scalar (bitmap_weight, for example) results
  33. * carefully filter out these unused bits from impacting their
  34. * results.
  35. *
  36. * These operations actually hold to a slightly stronger rule:
  37. * if you don't input any bitmaps to these ops that have some
  38. * unused bits set, then they won't output any set unused bits
  39. * in output bitmaps.
  40. *
  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. int __bitmap_equal(const unsigned long *bitmap1,
  47. const unsigned long *bitmap2, unsigned int bits)
  48. {
  49. unsigned int k, lim = bits/BITS_PER_LONG;
  50. for (k = 0; k < lim; ++k)
  51. if (bitmap1[k] != bitmap2[k])
  52. return 0;
  53. if (bits % BITS_PER_LONG)
  54. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  55. return 0;
  56. return 1;
  57. }
  58. EXPORT_SYMBOL(__bitmap_equal);
  59. void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
  60. {
  61. unsigned int k, lim = bits/BITS_PER_LONG;
  62. for (k = 0; k < lim; ++k)
  63. dst[k] = ~src[k];
  64. if (bits % BITS_PER_LONG)
  65. dst[k] = ~src[k];
  66. }
  67. EXPORT_SYMBOL(__bitmap_complement);
  68. /**
  69. * __bitmap_shift_right - logical right shift of the bits in a bitmap
  70. * @dst : destination bitmap
  71. * @src : source bitmap
  72. * @shift : shift by this many bits
  73. * @nbits : bitmap size, in bits
  74. *
  75. * Shifting right (dividing) means moving bits in the MS -> LS bit
  76. * direction. Zeros are fed into the vacated MS positions and the
  77. * LS bits shifted off the bottom are lost.
  78. */
  79. void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  80. unsigned shift, unsigned nbits)
  81. {
  82. unsigned k, lim = BITS_TO_LONGS(nbits);
  83. unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  84. unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
  85. for (k = 0; off + k < lim; ++k) {
  86. unsigned long upper, lower;
  87. /*
  88. * If shift is not word aligned, take lower rem bits of
  89. * word above and make them the top rem bits of result.
  90. */
  91. if (!rem || off + k + 1 >= lim)
  92. upper = 0;
  93. else {
  94. upper = src[off + k + 1];
  95. if (off + k + 1 == lim - 1)
  96. upper &= mask;
  97. upper <<= (BITS_PER_LONG - rem);
  98. }
  99. lower = src[off + k];
  100. if (off + k == lim - 1)
  101. lower &= mask;
  102. lower >>= rem;
  103. dst[k] = lower | upper;
  104. }
  105. if (off)
  106. memset(&dst[lim - off], 0, off*sizeof(unsigned long));
  107. }
  108. EXPORT_SYMBOL(__bitmap_shift_right);
  109. /**
  110. * __bitmap_shift_left - logical left shift of the bits in a bitmap
  111. * @dst : destination bitmap
  112. * @src : source bitmap
  113. * @shift : shift by this many bits
  114. * @nbits : bitmap size, in bits
  115. *
  116. * Shifting left (multiplying) means moving bits in the LS -> MS
  117. * direction. Zeros are fed into the vacated LS bit positions
  118. * and those MS bits shifted off the top are lost.
  119. */
  120. void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  121. unsigned int shift, unsigned int nbits)
  122. {
  123. int k;
  124. unsigned int lim = BITS_TO_LONGS(nbits);
  125. unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  126. for (k = lim - off - 1; k >= 0; --k) {
  127. unsigned long upper, lower;
  128. /*
  129. * If shift is not word aligned, take upper rem bits of
  130. * word below and make them the bottom rem bits of result.
  131. */
  132. if (rem && k > 0)
  133. lower = src[k - 1] >> (BITS_PER_LONG - rem);
  134. else
  135. lower = 0;
  136. upper = src[k] << rem;
  137. dst[k + off] = lower | upper;
  138. }
  139. if (off)
  140. memset(dst, 0, off*sizeof(unsigned long));
  141. }
  142. EXPORT_SYMBOL(__bitmap_shift_left);
  143. int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  144. const unsigned long *bitmap2, unsigned int bits)
  145. {
  146. unsigned int k;
  147. unsigned int lim = bits/BITS_PER_LONG;
  148. unsigned long result = 0;
  149. for (k = 0; k < lim; k++)
  150. result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  151. if (bits % BITS_PER_LONG)
  152. result |= (dst[k] = bitmap1[k] & bitmap2[k] &
  153. BITMAP_LAST_WORD_MASK(bits));
  154. return result != 0;
  155. }
  156. EXPORT_SYMBOL(__bitmap_and);
  157. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  158. const unsigned long *bitmap2, unsigned int bits)
  159. {
  160. unsigned int k;
  161. unsigned int nr = BITS_TO_LONGS(bits);
  162. for (k = 0; k < nr; k++)
  163. dst[k] = bitmap1[k] | bitmap2[k];
  164. }
  165. EXPORT_SYMBOL(__bitmap_or);
  166. void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  167. const unsigned long *bitmap2, unsigned int bits)
  168. {
  169. unsigned int k;
  170. unsigned int nr = BITS_TO_LONGS(bits);
  171. for (k = 0; k < nr; k++)
  172. dst[k] = bitmap1[k] ^ bitmap2[k];
  173. }
  174. EXPORT_SYMBOL(__bitmap_xor);
  175. int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  176. const unsigned long *bitmap2, unsigned int bits)
  177. {
  178. unsigned int k;
  179. unsigned int lim = bits/BITS_PER_LONG;
  180. unsigned long result = 0;
  181. for (k = 0; k < lim; k++)
  182. result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  183. if (bits % BITS_PER_LONG)
  184. result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
  185. BITMAP_LAST_WORD_MASK(bits));
  186. return result != 0;
  187. }
  188. EXPORT_SYMBOL(__bitmap_andnot);
  189. int __bitmap_intersects(const unsigned long *bitmap1,
  190. const unsigned long *bitmap2, unsigned int bits)
  191. {
  192. unsigned int k, lim = bits/BITS_PER_LONG;
  193. for (k = 0; k < lim; ++k)
  194. if (bitmap1[k] & bitmap2[k])
  195. return 1;
  196. if (bits % BITS_PER_LONG)
  197. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  198. return 1;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(__bitmap_intersects);
  202. int __bitmap_subset(const unsigned long *bitmap1,
  203. const unsigned long *bitmap2, unsigned int bits)
  204. {
  205. unsigned int k, lim = bits/BITS_PER_LONG;
  206. for (k = 0; k < lim; ++k)
  207. if (bitmap1[k] & ~bitmap2[k])
  208. return 0;
  209. if (bits % BITS_PER_LONG)
  210. if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  211. return 0;
  212. return 1;
  213. }
  214. EXPORT_SYMBOL(__bitmap_subset);
  215. int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
  216. {
  217. unsigned int k, lim = bits/BITS_PER_LONG;
  218. int w = 0;
  219. for (k = 0; k < lim; k++)
  220. w += hweight_long(bitmap[k]);
  221. if (bits % BITS_PER_LONG)
  222. w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
  223. return w;
  224. }
  225. EXPORT_SYMBOL(__bitmap_weight);
  226. void __bitmap_set(unsigned long *map, unsigned int start, int len)
  227. {
  228. unsigned long *p = map + BIT_WORD(start);
  229. const unsigned int size = start + len;
  230. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  231. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  232. while (len - bits_to_set >= 0) {
  233. *p |= mask_to_set;
  234. len -= bits_to_set;
  235. bits_to_set = BITS_PER_LONG;
  236. mask_to_set = ~0UL;
  237. p++;
  238. }
  239. if (len) {
  240. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  241. *p |= mask_to_set;
  242. }
  243. }
  244. EXPORT_SYMBOL(__bitmap_set);
  245. void __bitmap_clear(unsigned long *map, unsigned int start, int len)
  246. {
  247. unsigned long *p = map + BIT_WORD(start);
  248. const unsigned int size = start + len;
  249. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  250. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  251. while (len - bits_to_clear >= 0) {
  252. *p &= ~mask_to_clear;
  253. len -= bits_to_clear;
  254. bits_to_clear = BITS_PER_LONG;
  255. mask_to_clear = ~0UL;
  256. p++;
  257. }
  258. if (len) {
  259. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  260. *p &= ~mask_to_clear;
  261. }
  262. }
  263. EXPORT_SYMBOL(__bitmap_clear);
  264. /**
  265. * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
  266. * @map: The address to base the search on
  267. * @size: The bitmap size in bits
  268. * @start: The bitnumber to start searching at
  269. * @nr: The number of zeroed bits we're looking for
  270. * @align_mask: Alignment mask for zero area
  271. * @align_offset: Alignment offset for zero area.
  272. *
  273. * The @align_mask should be one less than a power of 2; the effect is that
  274. * the bit offset of all zero areas this function finds plus @align_offset
  275. * is multiple of that power of 2.
  276. */
  277. unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  278. unsigned long size,
  279. unsigned long start,
  280. unsigned int nr,
  281. unsigned long align_mask,
  282. unsigned long align_offset)
  283. {
  284. unsigned long index, end, i;
  285. again:
  286. index = find_next_zero_bit(map, size, start);
  287. /* Align allocation */
  288. index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
  289. end = index + nr;
  290. if (end > size)
  291. return end;
  292. i = find_next_bit(map, end, index);
  293. if (i < end) {
  294. start = i + 1;
  295. goto again;
  296. }
  297. return index;
  298. }
  299. EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
  300. /*
  301. * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
  302. * second version by Paul Jackson, third by Joe Korty.
  303. */
  304. #define CHUNKSZ 32
  305. #define nbits_to_hold_value(val) fls(val)
  306. #define BASEDEC 10 /* fancier cpuset lists input in decimal */
  307. /**
  308. * __bitmap_parse - convert an ASCII hex string into a bitmap.
  309. * @buf: pointer to buffer containing string.
  310. * @buflen: buffer size in bytes. If string is smaller than this
  311. * then it must be terminated with a \0.
  312. * @is_user: location of buffer, 0 indicates kernel space
  313. * @maskp: pointer to bitmap array that will contain result.
  314. * @nmaskbits: size of bitmap, in bits.
  315. *
  316. * Commas group hex digits into chunks. Each chunk defines exactly 32
  317. * bits of the resultant bitmask. No chunk may specify a value larger
  318. * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
  319. * then leading 0-bits are prepended. %-EINVAL is returned for illegal
  320. * characters and for grouping errors such as "1,,5", ",44", "," and "".
  321. * Leading and trailing whitespace accepted, but not embedded whitespace.
  322. */
  323. int __bitmap_parse(const char *buf, unsigned int buflen,
  324. int is_user, unsigned long *maskp,
  325. int nmaskbits)
  326. {
  327. int c, old_c, totaldigits, ndigits, nchunks, nbits;
  328. u32 chunk;
  329. const char __user __force *ubuf = (const char __user __force *)buf;
  330. bitmap_zero(maskp, nmaskbits);
  331. nchunks = nbits = totaldigits = c = 0;
  332. do {
  333. chunk = 0;
  334. ndigits = totaldigits;
  335. /* Get the next chunk of the bitmap */
  336. while (buflen) {
  337. old_c = c;
  338. if (is_user) {
  339. if (__get_user(c, ubuf++))
  340. return -EFAULT;
  341. }
  342. else
  343. c = *buf++;
  344. buflen--;
  345. if (isspace(c))
  346. continue;
  347. /*
  348. * If the last character was a space and the current
  349. * character isn't '\0', we've got embedded whitespace.
  350. * This is a no-no, so throw an error.
  351. */
  352. if (totaldigits && c && isspace(old_c))
  353. return -EINVAL;
  354. /* A '\0' or a ',' signal the end of the chunk */
  355. if (c == '\0' || c == ',')
  356. break;
  357. if (!isxdigit(c))
  358. return -EINVAL;
  359. /*
  360. * Make sure there are at least 4 free bits in 'chunk'.
  361. * If not, this hexdigit will overflow 'chunk', so
  362. * throw an error.
  363. */
  364. if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1))
  365. return -EOVERFLOW;
  366. chunk = (chunk << 4) | hex_to_bin(c);
  367. totaldigits++;
  368. }
  369. if (ndigits == totaldigits)
  370. return -EINVAL;
  371. if (nchunks == 0 && chunk == 0)
  372. continue;
  373. __bitmap_shift_left(maskp, maskp, CHUNKSZ, nmaskbits);
  374. *maskp |= chunk;
  375. nchunks++;
  376. nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ;
  377. if (nbits > nmaskbits)
  378. return -EOVERFLOW;
  379. } while (buflen && c == ',');
  380. return 0;
  381. }
  382. EXPORT_SYMBOL(__bitmap_parse);
  383. /**
  384. * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
  385. *
  386. * @ubuf: pointer to user buffer containing string.
  387. * @ulen: buffer size in bytes. If string is smaller than this
  388. * then it must be terminated with a \0.
  389. * @maskp: pointer to bitmap array that will contain result.
  390. * @nmaskbits: size of bitmap, in bits.
  391. *
  392. * Wrapper for __bitmap_parse(), providing it with user buffer.
  393. *
  394. * We cannot have this as an inline function in bitmap.h because it needs
  395. * linux/uaccess.h to get the access_ok() declaration and this causes
  396. * cyclic dependencies.
  397. */
  398. int bitmap_parse_user(const char __user *ubuf,
  399. unsigned int ulen, unsigned long *maskp,
  400. int nmaskbits)
  401. {
  402. if (!access_ok(VERIFY_READ, ubuf, ulen))
  403. return -EFAULT;
  404. return __bitmap_parse((const char __force *)ubuf,
  405. ulen, 1, maskp, nmaskbits);
  406. }
  407. EXPORT_SYMBOL(bitmap_parse_user);
  408. /**
  409. * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
  410. * @list: indicates whether the bitmap must be list
  411. * @buf: page aligned buffer into which string is placed
  412. * @maskp: pointer to bitmap to convert
  413. * @nmaskbits: size of bitmap, in bits
  414. *
  415. * Output format is a comma-separated list of decimal numbers and
  416. * ranges if list is specified or hex digits grouped into comma-separated
  417. * sets of 8 digits/set. Returns the number of characters written to buf.
  418. *
  419. * It is assumed that @buf is a pointer into a PAGE_SIZE area and that
  420. * sufficient storage remains at @buf to accommodate the
  421. * bitmap_print_to_pagebuf() output.
  422. */
  423. int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
  424. int nmaskbits)
  425. {
  426. ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
  427. int n = 0;
  428. if (len > 1)
  429. n = list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
  430. scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
  431. return n;
  432. }
  433. EXPORT_SYMBOL(bitmap_print_to_pagebuf);
  434. /**
  435. * __bitmap_parselist - convert list format ASCII string to bitmap
  436. * @buf: read nul-terminated user string from this buffer
  437. * @buflen: buffer size in bytes. If string is smaller than this
  438. * then it must be terminated with a \0.
  439. * @is_user: location of buffer, 0 indicates kernel space
  440. * @maskp: write resulting mask here
  441. * @nmaskbits: number of bits in mask to be written
  442. *
  443. * Input format is a comma-separated list of decimal numbers and
  444. * ranges. Consecutively set bits are shown as two hyphen-separated
  445. * decimal numbers, the smallest and largest bit numbers set in
  446. * the range.
  447. * Optionally each range can be postfixed to denote that only parts of it
  448. * should be set. The range will divided to groups of specific size.
  449. * From each group will be used only defined amount of bits.
  450. * Syntax: range:used_size/group_size
  451. * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
  452. *
  453. * Returns: 0 on success, -errno on invalid input strings. Error values:
  454. *
  455. * - ``-EINVAL``: second number in range smaller than first
  456. * - ``-EINVAL``: invalid character in string
  457. * - ``-ERANGE``: bit number specified too large for mask
  458. */
  459. static int __bitmap_parselist(const char *buf, unsigned int buflen,
  460. int is_user, unsigned long *maskp,
  461. int nmaskbits)
  462. {
  463. unsigned int a, b, old_a, old_b;
  464. unsigned int group_size, used_size, off;
  465. int c, old_c, totaldigits, ndigits;
  466. const char __user __force *ubuf = (const char __user __force *)buf;
  467. int at_start, in_range, in_partial_range;
  468. totaldigits = c = 0;
  469. old_a = old_b = 0;
  470. group_size = used_size = 0;
  471. bitmap_zero(maskp, nmaskbits);
  472. do {
  473. at_start = 1;
  474. in_range = 0;
  475. in_partial_range = 0;
  476. a = b = 0;
  477. ndigits = totaldigits;
  478. /* Get the next cpu# or a range of cpu#'s */
  479. while (buflen) {
  480. old_c = c;
  481. if (is_user) {
  482. if (__get_user(c, ubuf++))
  483. return -EFAULT;
  484. } else
  485. c = *buf++;
  486. buflen--;
  487. if (isspace(c))
  488. continue;
  489. /* A '\0' or a ',' signal the end of a cpu# or range */
  490. if (c == '\0' || c == ',')
  491. break;
  492. /*
  493. * whitespaces between digits are not allowed,
  494. * but it's ok if whitespaces are on head or tail.
  495. * when old_c is whilespace,
  496. * if totaldigits == ndigits, whitespace is on head.
  497. * if whitespace is on tail, it should not run here.
  498. * as c was ',' or '\0',
  499. * the last code line has broken the current loop.
  500. */
  501. if ((totaldigits != ndigits) && isspace(old_c))
  502. return -EINVAL;
  503. if (c == '/') {
  504. used_size = a;
  505. at_start = 1;
  506. in_range = 0;
  507. a = b = 0;
  508. continue;
  509. }
  510. if (c == ':') {
  511. old_a = a;
  512. old_b = b;
  513. at_start = 1;
  514. in_range = 0;
  515. in_partial_range = 1;
  516. a = b = 0;
  517. continue;
  518. }
  519. if (c == '-') {
  520. if (at_start || in_range)
  521. return -EINVAL;
  522. b = 0;
  523. in_range = 1;
  524. at_start = 1;
  525. continue;
  526. }
  527. if (!isdigit(c))
  528. return -EINVAL;
  529. b = b * 10 + (c - '0');
  530. if (!in_range)
  531. a = b;
  532. at_start = 0;
  533. totaldigits++;
  534. }
  535. if (ndigits == totaldigits)
  536. continue;
  537. if (in_partial_range) {
  538. group_size = a;
  539. a = old_a;
  540. b = old_b;
  541. old_a = old_b = 0;
  542. } else {
  543. used_size = group_size = b - a + 1;
  544. }
  545. /* if no digit is after '-', it's wrong*/
  546. if (at_start && in_range)
  547. return -EINVAL;
  548. if (!(a <= b) || !(used_size <= group_size))
  549. return -EINVAL;
  550. if (b >= nmaskbits)
  551. return -ERANGE;
  552. while (a <= b) {
  553. off = min(b - a + 1, used_size);
  554. bitmap_set(maskp, a, off);
  555. a += group_size;
  556. }
  557. } while (buflen && c == ',');
  558. return 0;
  559. }
  560. int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits)
  561. {
  562. char *nl = strchrnul(bp, '\n');
  563. int len = nl - bp;
  564. return __bitmap_parselist(bp, len, 0, maskp, nmaskbits);
  565. }
  566. EXPORT_SYMBOL(bitmap_parselist);
  567. /**
  568. * bitmap_parselist_user()
  569. *
  570. * @ubuf: pointer to user buffer containing string.
  571. * @ulen: buffer size in bytes. If string is smaller than this
  572. * then it must be terminated with a \0.
  573. * @maskp: pointer to bitmap array that will contain result.
  574. * @nmaskbits: size of bitmap, in bits.
  575. *
  576. * Wrapper for bitmap_parselist(), providing it with user buffer.
  577. *
  578. * We cannot have this as an inline function in bitmap.h because it needs
  579. * linux/uaccess.h to get the access_ok() declaration and this causes
  580. * cyclic dependencies.
  581. */
  582. int bitmap_parselist_user(const char __user *ubuf,
  583. unsigned int ulen, unsigned long *maskp,
  584. int nmaskbits)
  585. {
  586. if (!access_ok(VERIFY_READ, ubuf, ulen))
  587. return -EFAULT;
  588. return __bitmap_parselist((const char __force *)ubuf,
  589. ulen, 1, maskp, nmaskbits);
  590. }
  591. EXPORT_SYMBOL(bitmap_parselist_user);
  592. /**
  593. * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
  594. * @buf: pointer to a bitmap
  595. * @pos: a bit position in @buf (0 <= @pos < @nbits)
  596. * @nbits: number of valid bit positions in @buf
  597. *
  598. * Map the bit at position @pos in @buf (of length @nbits) to the
  599. * ordinal of which set bit it is. If it is not set or if @pos
  600. * is not a valid bit position, map to -1.
  601. *
  602. * If for example, just bits 4 through 7 are set in @buf, then @pos
  603. * values 4 through 7 will get mapped to 0 through 3, respectively,
  604. * and other @pos values will get mapped to -1. When @pos value 7
  605. * gets mapped to (returns) @ord value 3 in this example, that means
  606. * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  607. *
  608. * The bit positions 0 through @bits are valid positions in @buf.
  609. */
  610. static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
  611. {
  612. if (pos >= nbits || !test_bit(pos, buf))
  613. return -1;
  614. return __bitmap_weight(buf, pos);
  615. }
  616. /**
  617. * bitmap_ord_to_pos - find position of n-th set bit in bitmap
  618. * @buf: pointer to bitmap
  619. * @ord: ordinal bit position (n-th set bit, n >= 0)
  620. * @nbits: number of valid bit positions in @buf
  621. *
  622. * Map the ordinal offset of bit @ord in @buf to its position in @buf.
  623. * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
  624. * >= weight(buf), returns @nbits.
  625. *
  626. * If for example, just bits 4 through 7 are set in @buf, then @ord
  627. * values 0 through 3 will get mapped to 4 through 7, respectively,
  628. * and all other @ord values returns @nbits. When @ord value 3
  629. * gets mapped to (returns) @pos value 7 in this example, that means
  630. * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
  631. *
  632. * The bit positions 0 through @nbits-1 are valid positions in @buf.
  633. */
  634. unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
  635. {
  636. unsigned int pos;
  637. for (pos = find_first_bit(buf, nbits);
  638. pos < nbits && ord;
  639. pos = find_next_bit(buf, nbits, pos + 1))
  640. ord--;
  641. return pos;
  642. }
  643. /**
  644. * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  645. * @dst: remapped result
  646. * @src: subset to be remapped
  647. * @old: defines domain of map
  648. * @new: defines range of map
  649. * @nbits: number of bits in each of these bitmaps
  650. *
  651. * Let @old and @new define a mapping of bit positions, such that
  652. * whatever position is held by the n-th set bit in @old is mapped
  653. * to the n-th set bit in @new. In the more general case, allowing
  654. * for the possibility that the weight 'w' of @new is less than the
  655. * weight of @old, map the position of the n-th set bit in @old to
  656. * the position of the m-th set bit in @new, where m == n % w.
  657. *
  658. * If either of the @old and @new bitmaps are empty, or if @src and
  659. * @dst point to the same location, then this routine copies @src
  660. * to @dst.
  661. *
  662. * The positions of unset bits in @old are mapped to themselves
  663. * (the identify map).
  664. *
  665. * Apply the above specified mapping to @src, placing the result in
  666. * @dst, clearing any bits previously set in @dst.
  667. *
  668. * For example, lets say that @old has bits 4 through 7 set, and
  669. * @new has bits 12 through 15 set. This defines the mapping of bit
  670. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  671. * bit positions unchanged. So if say @src comes into this routine
  672. * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
  673. * 13 and 15 set.
  674. */
  675. void bitmap_remap(unsigned long *dst, const unsigned long *src,
  676. const unsigned long *old, const unsigned long *new,
  677. unsigned int nbits)
  678. {
  679. unsigned int oldbit, w;
  680. if (dst == src) /* following doesn't handle inplace remaps */
  681. return;
  682. bitmap_zero(dst, nbits);
  683. w = bitmap_weight(new, nbits);
  684. for_each_set_bit(oldbit, src, nbits) {
  685. int n = bitmap_pos_to_ord(old, oldbit, nbits);
  686. if (n < 0 || w == 0)
  687. set_bit(oldbit, dst); /* identity map */
  688. else
  689. set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
  690. }
  691. }
  692. EXPORT_SYMBOL(bitmap_remap);
  693. /**
  694. * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
  695. * @oldbit: bit position to be mapped
  696. * @old: defines domain of map
  697. * @new: defines range of map
  698. * @bits: number of bits in each of these bitmaps
  699. *
  700. * Let @old and @new define a mapping of bit positions, such that
  701. * whatever position is held by the n-th set bit in @old is mapped
  702. * to the n-th set bit in @new. In the more general case, allowing
  703. * for the possibility that the weight 'w' of @new is less than the
  704. * weight of @old, map the position of the n-th set bit in @old to
  705. * the position of the m-th set bit in @new, where m == n % w.
  706. *
  707. * The positions of unset bits in @old are mapped to themselves
  708. * (the identify map).
  709. *
  710. * Apply the above specified mapping to bit position @oldbit, returning
  711. * the new bit position.
  712. *
  713. * For example, lets say that @old has bits 4 through 7 set, and
  714. * @new has bits 12 through 15 set. This defines the mapping of bit
  715. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  716. * bit positions unchanged. So if say @oldbit is 5, then this routine
  717. * returns 13.
  718. */
  719. int bitmap_bitremap(int oldbit, const unsigned long *old,
  720. const unsigned long *new, int bits)
  721. {
  722. int w = bitmap_weight(new, bits);
  723. int n = bitmap_pos_to_ord(old, oldbit, bits);
  724. if (n < 0 || w == 0)
  725. return oldbit;
  726. else
  727. return bitmap_ord_to_pos(new, n % w, bits);
  728. }
  729. EXPORT_SYMBOL(bitmap_bitremap);
  730. /**
  731. * bitmap_onto - translate one bitmap relative to another
  732. * @dst: resulting translated bitmap
  733. * @orig: original untranslated bitmap
  734. * @relmap: bitmap relative to which translated
  735. * @bits: number of bits in each of these bitmaps
  736. *
  737. * Set the n-th bit of @dst iff there exists some m such that the
  738. * n-th bit of @relmap is set, the m-th bit of @orig is set, and
  739. * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
  740. * (If you understood the previous sentence the first time your
  741. * read it, you're overqualified for your current job.)
  742. *
  743. * In other words, @orig is mapped onto (surjectively) @dst,
  744. * using the map { <n, m> | the n-th bit of @relmap is the
  745. * m-th set bit of @relmap }.
  746. *
  747. * Any set bits in @orig above bit number W, where W is the
  748. * weight of (number of set bits in) @relmap are mapped nowhere.
  749. * In particular, if for all bits m set in @orig, m >= W, then
  750. * @dst will end up empty. In situations where the possibility
  751. * of such an empty result is not desired, one way to avoid it is
  752. * to use the bitmap_fold() operator, below, to first fold the
  753. * @orig bitmap over itself so that all its set bits x are in the
  754. * range 0 <= x < W. The bitmap_fold() operator does this by
  755. * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
  756. *
  757. * Example [1] for bitmap_onto():
  758. * Let's say @relmap has bits 30-39 set, and @orig has bits
  759. * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
  760. * @dst will have bits 31, 33, 35, 37 and 39 set.
  761. *
  762. * When bit 0 is set in @orig, it means turn on the bit in
  763. * @dst corresponding to whatever is the first bit (if any)
  764. * that is turned on in @relmap. Since bit 0 was off in the
  765. * above example, we leave off that bit (bit 30) in @dst.
  766. *
  767. * When bit 1 is set in @orig (as in the above example), it
  768. * means turn on the bit in @dst corresponding to whatever
  769. * is the second bit that is turned on in @relmap. The second
  770. * bit in @relmap that was turned on in the above example was
  771. * bit 31, so we turned on bit 31 in @dst.
  772. *
  773. * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
  774. * because they were the 4th, 6th, 8th and 10th set bits
  775. * set in @relmap, and the 4th, 6th, 8th and 10th bits of
  776. * @orig (i.e. bits 3, 5, 7 and 9) were also set.
  777. *
  778. * When bit 11 is set in @orig, it means turn on the bit in
  779. * @dst corresponding to whatever is the twelfth bit that is
  780. * turned on in @relmap. In the above example, there were
  781. * only ten bits turned on in @relmap (30..39), so that bit
  782. * 11 was set in @orig had no affect on @dst.
  783. *
  784. * Example [2] for bitmap_fold() + bitmap_onto():
  785. * Let's say @relmap has these ten bits set::
  786. *
  787. * 40 41 42 43 45 48 53 61 74 95
  788. *
  789. * (for the curious, that's 40 plus the first ten terms of the
  790. * Fibonacci sequence.)
  791. *
  792. * Further lets say we use the following code, invoking
  793. * bitmap_fold() then bitmap_onto, as suggested above to
  794. * avoid the possibility of an empty @dst result::
  795. *
  796. * unsigned long *tmp; // a temporary bitmap's bits
  797. *
  798. * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
  799. * bitmap_onto(dst, tmp, relmap, bits);
  800. *
  801. * Then this table shows what various values of @dst would be, for
  802. * various @orig's. I list the zero-based positions of each set bit.
  803. * The tmp column shows the intermediate result, as computed by
  804. * using bitmap_fold() to fold the @orig bitmap modulo ten
  805. * (the weight of @relmap):
  806. *
  807. * =============== ============== =================
  808. * @orig tmp @dst
  809. * 0 0 40
  810. * 1 1 41
  811. * 9 9 95
  812. * 10 0 40 [#f1]_
  813. * 1 3 5 7 1 3 5 7 41 43 48 61
  814. * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
  815. * 0 9 18 27 0 9 8 7 40 61 74 95
  816. * 0 10 20 30 0 40
  817. * 0 11 22 33 0 1 2 3 40 41 42 43
  818. * 0 12 24 36 0 2 4 6 40 42 45 53
  819. * 78 102 211 1 2 8 41 42 74 [#f1]_
  820. * =============== ============== =================
  821. *
  822. * .. [#f1]
  823. *
  824. * For these marked lines, if we hadn't first done bitmap_fold()
  825. * into tmp, then the @dst result would have been empty.
  826. *
  827. * If either of @orig or @relmap is empty (no set bits), then @dst
  828. * will be returned empty.
  829. *
  830. * If (as explained above) the only set bits in @orig are in positions
  831. * m where m >= W, (where W is the weight of @relmap) then @dst will
  832. * once again be returned empty.
  833. *
  834. * All bits in @dst not set by the above rule are cleared.
  835. */
  836. void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  837. const unsigned long *relmap, unsigned int bits)
  838. {
  839. unsigned int n, m; /* same meaning as in above comment */
  840. if (dst == orig) /* following doesn't handle inplace mappings */
  841. return;
  842. bitmap_zero(dst, bits);
  843. /*
  844. * The following code is a more efficient, but less
  845. * obvious, equivalent to the loop:
  846. * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
  847. * n = bitmap_ord_to_pos(orig, m, bits);
  848. * if (test_bit(m, orig))
  849. * set_bit(n, dst);
  850. * }
  851. */
  852. m = 0;
  853. for_each_set_bit(n, relmap, bits) {
  854. /* m == bitmap_pos_to_ord(relmap, n, bits) */
  855. if (test_bit(m, orig))
  856. set_bit(n, dst);
  857. m++;
  858. }
  859. }
  860. EXPORT_SYMBOL(bitmap_onto);
  861. /**
  862. * bitmap_fold - fold larger bitmap into smaller, modulo specified size
  863. * @dst: resulting smaller bitmap
  864. * @orig: original larger bitmap
  865. * @sz: specified size
  866. * @nbits: number of bits in each of these bitmaps
  867. *
  868. * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
  869. * Clear all other bits in @dst. See further the comment and
  870. * Example [2] for bitmap_onto() for why and how to use this.
  871. */
  872. void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  873. unsigned int sz, unsigned int nbits)
  874. {
  875. unsigned int oldbit;
  876. if (dst == orig) /* following doesn't handle inplace mappings */
  877. return;
  878. bitmap_zero(dst, nbits);
  879. for_each_set_bit(oldbit, orig, nbits)
  880. set_bit(oldbit % sz, dst);
  881. }
  882. EXPORT_SYMBOL(bitmap_fold);
  883. /*
  884. * Common code for bitmap_*_region() routines.
  885. * bitmap: array of unsigned longs corresponding to the bitmap
  886. * pos: the beginning of the region
  887. * order: region size (log base 2 of number of bits)
  888. * reg_op: operation(s) to perform on that region of bitmap
  889. *
  890. * Can set, verify and/or release a region of bits in a bitmap,
  891. * depending on which combination of REG_OP_* flag bits is set.
  892. *
  893. * A region of a bitmap is a sequence of bits in the bitmap, of
  894. * some size '1 << order' (a power of two), aligned to that same
  895. * '1 << order' power of two.
  896. *
  897. * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
  898. * Returns 0 in all other cases and reg_ops.
  899. */
  900. enum {
  901. REG_OP_ISFREE, /* true if region is all zero bits */
  902. REG_OP_ALLOC, /* set all bits in region */
  903. REG_OP_RELEASE, /* clear all bits in region */
  904. };
  905. static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
  906. {
  907. int nbits_reg; /* number of bits in region */
  908. int index; /* index first long of region in bitmap */
  909. int offset; /* bit offset region in bitmap[index] */
  910. int nlongs_reg; /* num longs spanned by region in bitmap */
  911. int nbitsinlong; /* num bits of region in each spanned long */
  912. unsigned long mask; /* bitmask for one long of region */
  913. int i; /* scans bitmap by longs */
  914. int ret = 0; /* return value */
  915. /*
  916. * Either nlongs_reg == 1 (for small orders that fit in one long)
  917. * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
  918. */
  919. nbits_reg = 1 << order;
  920. index = pos / BITS_PER_LONG;
  921. offset = pos - (index * BITS_PER_LONG);
  922. nlongs_reg = BITS_TO_LONGS(nbits_reg);
  923. nbitsinlong = min(nbits_reg, BITS_PER_LONG);
  924. /*
  925. * Can't do "mask = (1UL << nbitsinlong) - 1", as that
  926. * overflows if nbitsinlong == BITS_PER_LONG.
  927. */
  928. mask = (1UL << (nbitsinlong - 1));
  929. mask += mask - 1;
  930. mask <<= offset;
  931. switch (reg_op) {
  932. case REG_OP_ISFREE:
  933. for (i = 0; i < nlongs_reg; i++) {
  934. if (bitmap[index + i] & mask)
  935. goto done;
  936. }
  937. ret = 1; /* all bits in region free (zero) */
  938. break;
  939. case REG_OP_ALLOC:
  940. for (i = 0; i < nlongs_reg; i++)
  941. bitmap[index + i] |= mask;
  942. break;
  943. case REG_OP_RELEASE:
  944. for (i = 0; i < nlongs_reg; i++)
  945. bitmap[index + i] &= ~mask;
  946. break;
  947. }
  948. done:
  949. return ret;
  950. }
  951. /**
  952. * bitmap_find_free_region - find a contiguous aligned mem region
  953. * @bitmap: array of unsigned longs corresponding to the bitmap
  954. * @bits: number of bits in the bitmap
  955. * @order: region size (log base 2 of number of bits) to find
  956. *
  957. * Find a region of free (zero) bits in a @bitmap of @bits bits and
  958. * allocate them (set them to one). Only consider regions of length
  959. * a power (@order) of two, aligned to that power of two, which
  960. * makes the search algorithm much faster.
  961. *
  962. * Return the bit offset in bitmap of the allocated region,
  963. * or -errno on failure.
  964. */
  965. int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
  966. {
  967. unsigned int pos, end; /* scans bitmap by regions of size order */
  968. for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
  969. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  970. continue;
  971. __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  972. return pos;
  973. }
  974. return -ENOMEM;
  975. }
  976. EXPORT_SYMBOL(bitmap_find_free_region);
  977. /**
  978. * bitmap_release_region - release allocated bitmap region
  979. * @bitmap: array of unsigned longs corresponding to the bitmap
  980. * @pos: beginning of bit region to release
  981. * @order: region size (log base 2 of number of bits) to release
  982. *
  983. * This is the complement to __bitmap_find_free_region() and releases
  984. * the found region (by clearing it in the bitmap).
  985. *
  986. * No return value.
  987. */
  988. void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
  989. {
  990. __reg_op(bitmap, pos, order, REG_OP_RELEASE);
  991. }
  992. EXPORT_SYMBOL(bitmap_release_region);
  993. /**
  994. * bitmap_allocate_region - allocate bitmap region
  995. * @bitmap: array of unsigned longs corresponding to the bitmap
  996. * @pos: beginning of bit region to allocate
  997. * @order: region size (log base 2 of number of bits) to allocate
  998. *
  999. * Allocate (set bits in) a specified region of a bitmap.
  1000. *
  1001. * Return 0 on success, or %-EBUSY if specified region wasn't
  1002. * free (not all bits were zero).
  1003. */
  1004. int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
  1005. {
  1006. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  1007. return -EBUSY;
  1008. return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  1009. }
  1010. EXPORT_SYMBOL(bitmap_allocate_region);
  1011. /**
  1012. * bitmap_from_u32array - copy the contents of a u32 array of bits to bitmap
  1013. * @bitmap: array of unsigned longs, the destination bitmap, non NULL
  1014. * @nbits: number of bits in @bitmap
  1015. * @buf: array of u32 (in host byte order), the source bitmap, non NULL
  1016. * @nwords: number of u32 words in @buf
  1017. *
  1018. * copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
  1019. * bits between nword and nbits in @bitmap (if any) are cleared. In
  1020. * last word of @bitmap, the bits beyond nbits (if any) are kept
  1021. * unchanged.
  1022. *
  1023. * Return the number of bits effectively copied.
  1024. */
  1025. unsigned int
  1026. bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
  1027. const u32 *buf, unsigned int nwords)
  1028. {
  1029. unsigned int dst_idx, src_idx;
  1030. for (src_idx = dst_idx = 0; dst_idx < BITS_TO_LONGS(nbits); ++dst_idx) {
  1031. unsigned long part = 0;
  1032. if (src_idx < nwords)
  1033. part = buf[src_idx++];
  1034. #if BITS_PER_LONG == 64
  1035. if (src_idx < nwords)
  1036. part |= ((unsigned long) buf[src_idx++]) << 32;
  1037. #endif
  1038. if (dst_idx < nbits/BITS_PER_LONG)
  1039. bitmap[dst_idx] = part;
  1040. else {
  1041. unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
  1042. bitmap[dst_idx] = (bitmap[dst_idx] & ~mask)
  1043. | (part & mask);
  1044. }
  1045. }
  1046. return min_t(unsigned int, nbits, 32*nwords);
  1047. }
  1048. EXPORT_SYMBOL(bitmap_from_u32array);
  1049. /**
  1050. * bitmap_to_u32array - copy the contents of bitmap to a u32 array of bits
  1051. * @buf: array of u32 (in host byte order), the dest bitmap, non NULL
  1052. * @nwords: number of u32 words in @buf
  1053. * @bitmap: array of unsigned longs, the source bitmap, non NULL
  1054. * @nbits: number of bits in @bitmap
  1055. *
  1056. * copy min(nbits, 32*nwords) bits from @bitmap to @buf. Remaining
  1057. * bits after nbits in @buf (if any) are cleared.
  1058. *
  1059. * Return the number of bits effectively copied.
  1060. */
  1061. unsigned int
  1062. bitmap_to_u32array(u32 *buf, unsigned int nwords,
  1063. const unsigned long *bitmap, unsigned int nbits)
  1064. {
  1065. unsigned int dst_idx = 0, src_idx = 0;
  1066. while (dst_idx < nwords) {
  1067. unsigned long part = 0;
  1068. if (src_idx < BITS_TO_LONGS(nbits)) {
  1069. part = bitmap[src_idx];
  1070. if (src_idx >= nbits/BITS_PER_LONG)
  1071. part &= BITMAP_LAST_WORD_MASK(nbits);
  1072. src_idx++;
  1073. }
  1074. buf[dst_idx++] = part & 0xffffffffUL;
  1075. #if BITS_PER_LONG == 64
  1076. if (dst_idx < nwords) {
  1077. part >>= 32;
  1078. buf[dst_idx++] = part & 0xffffffffUL;
  1079. }
  1080. #endif
  1081. }
  1082. return min_t(unsigned int, nbits, 32*nwords);
  1083. }
  1084. EXPORT_SYMBOL(bitmap_to_u32array);
  1085. /**
  1086. * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
  1087. * @dst: destination buffer
  1088. * @src: bitmap to copy
  1089. * @nbits: number of bits in the bitmap
  1090. *
  1091. * Require nbits % BITS_PER_LONG == 0.
  1092. */
  1093. #ifdef __BIG_ENDIAN
  1094. void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
  1095. {
  1096. unsigned int i;
  1097. for (i = 0; i < nbits/BITS_PER_LONG; i++) {
  1098. if (BITS_PER_LONG == 64)
  1099. dst[i] = cpu_to_le64(src[i]);
  1100. else
  1101. dst[i] = cpu_to_le32(src[i]);
  1102. }
  1103. }
  1104. EXPORT_SYMBOL(bitmap_copy_le);
  1105. #endif