bitmap.c 36 KB

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