bitmap.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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/mm.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/page.h>
  21. /**
  22. * DOC: bitmap introduction
  23. *
  24. * bitmaps provide an array of bits, implemented using an an
  25. * array of unsigned longs. The number of valid bits in a
  26. * given bitmap does _not_ need to be an exact multiple of
  27. * BITS_PER_LONG.
  28. *
  29. * The possible unused bits in the last, partially used word
  30. * of a bitmap are 'don't care'. The implementation makes
  31. * no particular effort to keep them zero. It ensures that
  32. * their value will not affect the results of any operation.
  33. * The bitmap operations that return Boolean (bitmap_empty,
  34. * for example) or scalar (bitmap_weight, for example) results
  35. * carefully filter out these unused bits from impacting their
  36. * results.
  37. *
  38. * These operations actually hold to a slightly stronger rule:
  39. * if you don't input any bitmaps to these ops that have some
  40. * unused bits set, then they won't output any set unused bits
  41. * in output bitmaps.
  42. *
  43. * The byte ordering of bitmaps is more natural on little
  44. * endian architectures. See the big-endian headers
  45. * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
  46. * for the best explanations of this ordering.
  47. */
  48. int __bitmap_equal(const unsigned long *bitmap1,
  49. const unsigned long *bitmap2, unsigned int bits)
  50. {
  51. unsigned int k, lim = bits/BITS_PER_LONG;
  52. for (k = 0; k < lim; ++k)
  53. if (bitmap1[k] != bitmap2[k])
  54. return 0;
  55. if (bits % BITS_PER_LONG)
  56. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  57. return 0;
  58. return 1;
  59. }
  60. EXPORT_SYMBOL(__bitmap_equal);
  61. void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
  62. {
  63. unsigned int k, lim = BITS_TO_LONGS(bits);
  64. for (k = 0; k < lim; ++k)
  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, page-aligned
  420. * area and that sufficient storage remains at @buf to accommodate the
  421. * bitmap_print_to_pagebuf() output. Returns the number of characters
  422. * actually printed to @buf, excluding terminating '\0'.
  423. */
  424. int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
  425. int nmaskbits)
  426. {
  427. ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
  428. int n = 0;
  429. if (len > 1)
  430. n = list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
  431. scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
  432. return n;
  433. }
  434. EXPORT_SYMBOL(bitmap_print_to_pagebuf);
  435. /**
  436. * __bitmap_parselist - convert list format ASCII string to bitmap
  437. * @buf: read nul-terminated user string from this buffer
  438. * @buflen: buffer size in bytes. If string is smaller than this
  439. * then it must be terminated with a \0.
  440. * @is_user: location of buffer, 0 indicates kernel space
  441. * @maskp: write resulting mask here
  442. * @nmaskbits: number of bits in mask to be written
  443. *
  444. * Input format is a comma-separated list of decimal numbers and
  445. * ranges. Consecutively set bits are shown as two hyphen-separated
  446. * decimal numbers, the smallest and largest bit numbers set in
  447. * the range.
  448. * Optionally each range can be postfixed to denote that only parts of it
  449. * should be set. The range will divided to groups of specific size.
  450. * From each group will be used only defined amount of bits.
  451. * Syntax: range:used_size/group_size
  452. * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
  453. *
  454. * Returns: 0 on success, -errno on invalid input strings. Error values:
  455. *
  456. * - ``-EINVAL``: second number in range smaller than first
  457. * - ``-EINVAL``: invalid character in string
  458. * - ``-ERANGE``: bit number specified too large for mask
  459. */
  460. static int __bitmap_parselist(const char *buf, unsigned int buflen,
  461. int is_user, unsigned long *maskp,
  462. int nmaskbits)
  463. {
  464. unsigned int a, b, old_a, old_b;
  465. unsigned int group_size, used_size, off;
  466. int c, old_c, totaldigits, ndigits;
  467. const char __user __force *ubuf = (const char __user __force *)buf;
  468. int at_start, in_range, in_partial_range;
  469. totaldigits = c = 0;
  470. old_a = old_b = 0;
  471. group_size = used_size = 0;
  472. bitmap_zero(maskp, nmaskbits);
  473. do {
  474. at_start = 1;
  475. in_range = 0;
  476. in_partial_range = 0;
  477. a = b = 0;
  478. ndigits = totaldigits;
  479. /* Get the next cpu# or a range of cpu#'s */
  480. while (buflen) {
  481. old_c = c;
  482. if (is_user) {
  483. if (__get_user(c, ubuf++))
  484. return -EFAULT;
  485. } else
  486. c = *buf++;
  487. buflen--;
  488. if (isspace(c))
  489. continue;
  490. /* A '\0' or a ',' signal the end of a cpu# or range */
  491. if (c == '\0' || c == ',')
  492. break;
  493. /*
  494. * whitespaces between digits are not allowed,
  495. * but it's ok if whitespaces are on head or tail.
  496. * when old_c is whilespace,
  497. * if totaldigits == ndigits, whitespace is on head.
  498. * if whitespace is on tail, it should not run here.
  499. * as c was ',' or '\0',
  500. * the last code line has broken the current loop.
  501. */
  502. if ((totaldigits != ndigits) && isspace(old_c))
  503. return -EINVAL;
  504. if (c == '/') {
  505. used_size = a;
  506. at_start = 1;
  507. in_range = 0;
  508. a = b = 0;
  509. continue;
  510. }
  511. if (c == ':') {
  512. old_a = a;
  513. old_b = b;
  514. at_start = 1;
  515. in_range = 0;
  516. in_partial_range = 1;
  517. a = b = 0;
  518. continue;
  519. }
  520. if (c == '-') {
  521. if (at_start || in_range)
  522. return -EINVAL;
  523. b = 0;
  524. in_range = 1;
  525. at_start = 1;
  526. continue;
  527. }
  528. if (!isdigit(c))
  529. return -EINVAL;
  530. b = b * 10 + (c - '0');
  531. if (!in_range)
  532. a = b;
  533. at_start = 0;
  534. totaldigits++;
  535. }
  536. if (ndigits == totaldigits)
  537. continue;
  538. if (in_partial_range) {
  539. group_size = a;
  540. a = old_a;
  541. b = old_b;
  542. old_a = old_b = 0;
  543. } else {
  544. used_size = group_size = b - a + 1;
  545. }
  546. /* if no digit is after '-', it's wrong*/
  547. if (at_start && in_range)
  548. return -EINVAL;
  549. if (!(a <= b) || group_size == 0 || !(used_size <= group_size))
  550. return -EINVAL;
  551. if (b >= nmaskbits)
  552. return -ERANGE;
  553. while (a <= b) {
  554. off = min(b - a + 1, used_size);
  555. bitmap_set(maskp, a, off);
  556. a += group_size;
  557. }
  558. } while (buflen && c == ',');
  559. return 0;
  560. }
  561. int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits)
  562. {
  563. char *nl = strchrnul(bp, '\n');
  564. int len = nl - bp;
  565. return __bitmap_parselist(bp, len, 0, maskp, nmaskbits);
  566. }
  567. EXPORT_SYMBOL(bitmap_parselist);
  568. /**
  569. * bitmap_parselist_user()
  570. *
  571. * @ubuf: pointer to user buffer containing string.
  572. * @ulen: buffer size in bytes. If string is smaller than this
  573. * then it must be terminated with a \0.
  574. * @maskp: pointer to bitmap array that will contain result.
  575. * @nmaskbits: size of bitmap, in bits.
  576. *
  577. * Wrapper for bitmap_parselist(), providing it with user buffer.
  578. *
  579. * We cannot have this as an inline function in bitmap.h because it needs
  580. * linux/uaccess.h to get the access_ok() declaration and this causes
  581. * cyclic dependencies.
  582. */
  583. int bitmap_parselist_user(const char __user *ubuf,
  584. unsigned int ulen, unsigned long *maskp,
  585. int nmaskbits)
  586. {
  587. if (!access_ok(VERIFY_READ, ubuf, ulen))
  588. return -EFAULT;
  589. return __bitmap_parselist((const char __force *)ubuf,
  590. ulen, 1, maskp, nmaskbits);
  591. }
  592. EXPORT_SYMBOL(bitmap_parselist_user);
  593. /**
  594. * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
  595. * @buf: pointer to a bitmap
  596. * @pos: a bit position in @buf (0 <= @pos < @nbits)
  597. * @nbits: number of valid bit positions in @buf
  598. *
  599. * Map the bit at position @pos in @buf (of length @nbits) to the
  600. * ordinal of which set bit it is. If it is not set or if @pos
  601. * is not a valid bit position, map to -1.
  602. *
  603. * If for example, just bits 4 through 7 are set in @buf, then @pos
  604. * values 4 through 7 will get mapped to 0 through 3, respectively,
  605. * and other @pos values will get mapped to -1. When @pos value 7
  606. * gets mapped to (returns) @ord value 3 in this example, that means
  607. * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  608. *
  609. * The bit positions 0 through @bits are valid positions in @buf.
  610. */
  611. static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
  612. {
  613. if (pos >= nbits || !test_bit(pos, buf))
  614. return -1;
  615. return __bitmap_weight(buf, pos);
  616. }
  617. /**
  618. * bitmap_ord_to_pos - find position of n-th set bit in bitmap
  619. * @buf: pointer to bitmap
  620. * @ord: ordinal bit position (n-th set bit, n >= 0)
  621. * @nbits: number of valid bit positions in @buf
  622. *
  623. * Map the ordinal offset of bit @ord in @buf to its position in @buf.
  624. * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
  625. * >= weight(buf), returns @nbits.
  626. *
  627. * If for example, just bits 4 through 7 are set in @buf, then @ord
  628. * values 0 through 3 will get mapped to 4 through 7, respectively,
  629. * and all other @ord values returns @nbits. When @ord value 3
  630. * gets mapped to (returns) @pos value 7 in this example, that means
  631. * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
  632. *
  633. * The bit positions 0 through @nbits-1 are valid positions in @buf.
  634. */
  635. unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
  636. {
  637. unsigned int pos;
  638. for (pos = find_first_bit(buf, nbits);
  639. pos < nbits && ord;
  640. pos = find_next_bit(buf, nbits, pos + 1))
  641. ord--;
  642. return pos;
  643. }
  644. /**
  645. * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  646. * @dst: remapped result
  647. * @src: subset to be remapped
  648. * @old: defines domain of map
  649. * @new: defines range of map
  650. * @nbits: number of bits in each of these bitmaps
  651. *
  652. * Let @old and @new define a mapping of bit positions, such that
  653. * whatever position is held by the n-th set bit in @old is mapped
  654. * to the n-th set bit in @new. In the more general case, allowing
  655. * for the possibility that the weight 'w' of @new is less than the
  656. * weight of @old, map the position of the n-th set bit in @old to
  657. * the position of the m-th set bit in @new, where m == n % w.
  658. *
  659. * If either of the @old and @new bitmaps are empty, or if @src and
  660. * @dst point to the same location, then this routine copies @src
  661. * to @dst.
  662. *
  663. * The positions of unset bits in @old are mapped to themselves
  664. * (the identify map).
  665. *
  666. * Apply the above specified mapping to @src, placing the result in
  667. * @dst, clearing any bits previously set in @dst.
  668. *
  669. * For example, lets say that @old has bits 4 through 7 set, and
  670. * @new has bits 12 through 15 set. This defines the mapping of bit
  671. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  672. * bit positions unchanged. So if say @src comes into this routine
  673. * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
  674. * 13 and 15 set.
  675. */
  676. void bitmap_remap(unsigned long *dst, const unsigned long *src,
  677. const unsigned long *old, const unsigned long *new,
  678. unsigned int nbits)
  679. {
  680. unsigned int oldbit, w;
  681. if (dst == src) /* following doesn't handle inplace remaps */
  682. return;
  683. bitmap_zero(dst, nbits);
  684. w = bitmap_weight(new, nbits);
  685. for_each_set_bit(oldbit, src, nbits) {
  686. int n = bitmap_pos_to_ord(old, oldbit, nbits);
  687. if (n < 0 || w == 0)
  688. set_bit(oldbit, dst); /* identity map */
  689. else
  690. set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
  691. }
  692. }
  693. EXPORT_SYMBOL(bitmap_remap);
  694. /**
  695. * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
  696. * @oldbit: bit position to be mapped
  697. * @old: defines domain of map
  698. * @new: defines range of map
  699. * @bits: number of bits in each of these bitmaps
  700. *
  701. * Let @old and @new define a mapping of bit positions, such that
  702. * whatever position is held by the n-th set bit in @old is mapped
  703. * to the n-th set bit in @new. In the more general case, allowing
  704. * for the possibility that the weight 'w' of @new is less than the
  705. * weight of @old, map the position of the n-th set bit in @old to
  706. * the position of the m-th set bit in @new, where m == n % w.
  707. *
  708. * The positions of unset bits in @old are mapped to themselves
  709. * (the identify map).
  710. *
  711. * Apply the above specified mapping to bit position @oldbit, returning
  712. * the new bit position.
  713. *
  714. * For example, lets say that @old has bits 4 through 7 set, and
  715. * @new has bits 12 through 15 set. This defines the mapping of bit
  716. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  717. * bit positions unchanged. So if say @oldbit is 5, then this routine
  718. * returns 13.
  719. */
  720. int bitmap_bitremap(int oldbit, const unsigned long *old,
  721. const unsigned long *new, int bits)
  722. {
  723. int w = bitmap_weight(new, bits);
  724. int n = bitmap_pos_to_ord(old, oldbit, bits);
  725. if (n < 0 || w == 0)
  726. return oldbit;
  727. else
  728. return bitmap_ord_to_pos(new, n % w, bits);
  729. }
  730. EXPORT_SYMBOL(bitmap_bitremap);
  731. /**
  732. * bitmap_onto - translate one bitmap relative to another
  733. * @dst: resulting translated bitmap
  734. * @orig: original untranslated bitmap
  735. * @relmap: bitmap relative to which translated
  736. * @bits: number of bits in each of these bitmaps
  737. *
  738. * Set the n-th bit of @dst iff there exists some m such that the
  739. * n-th bit of @relmap is set, the m-th bit of @orig is set, and
  740. * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
  741. * (If you understood the previous sentence the first time your
  742. * read it, you're overqualified for your current job.)
  743. *
  744. * In other words, @orig is mapped onto (surjectively) @dst,
  745. * using the map { <n, m> | the n-th bit of @relmap is the
  746. * m-th set bit of @relmap }.
  747. *
  748. * Any set bits in @orig above bit number W, where W is the
  749. * weight of (number of set bits in) @relmap are mapped nowhere.
  750. * In particular, if for all bits m set in @orig, m >= W, then
  751. * @dst will end up empty. In situations where the possibility
  752. * of such an empty result is not desired, one way to avoid it is
  753. * to use the bitmap_fold() operator, below, to first fold the
  754. * @orig bitmap over itself so that all its set bits x are in the
  755. * range 0 <= x < W. The bitmap_fold() operator does this by
  756. * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
  757. *
  758. * Example [1] for bitmap_onto():
  759. * Let's say @relmap has bits 30-39 set, and @orig has bits
  760. * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
  761. * @dst will have bits 31, 33, 35, 37 and 39 set.
  762. *
  763. * When bit 0 is set in @orig, it means turn on the bit in
  764. * @dst corresponding to whatever is the first bit (if any)
  765. * that is turned on in @relmap. Since bit 0 was off in the
  766. * above example, we leave off that bit (bit 30) in @dst.
  767. *
  768. * When bit 1 is set in @orig (as in the above example), it
  769. * means turn on the bit in @dst corresponding to whatever
  770. * is the second bit that is turned on in @relmap. The second
  771. * bit in @relmap that was turned on in the above example was
  772. * bit 31, so we turned on bit 31 in @dst.
  773. *
  774. * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
  775. * because they were the 4th, 6th, 8th and 10th set bits
  776. * set in @relmap, and the 4th, 6th, 8th and 10th bits of
  777. * @orig (i.e. bits 3, 5, 7 and 9) were also set.
  778. *
  779. * When bit 11 is set in @orig, it means turn on the bit in
  780. * @dst corresponding to whatever is the twelfth bit that is
  781. * turned on in @relmap. In the above example, there were
  782. * only ten bits turned on in @relmap (30..39), so that bit
  783. * 11 was set in @orig had no affect on @dst.
  784. *
  785. * Example [2] for bitmap_fold() + bitmap_onto():
  786. * Let's say @relmap has these ten bits set::
  787. *
  788. * 40 41 42 43 45 48 53 61 74 95
  789. *
  790. * (for the curious, that's 40 plus the first ten terms of the
  791. * Fibonacci sequence.)
  792. *
  793. * Further lets say we use the following code, invoking
  794. * bitmap_fold() then bitmap_onto, as suggested above to
  795. * avoid the possibility of an empty @dst result::
  796. *
  797. * unsigned long *tmp; // a temporary bitmap's bits
  798. *
  799. * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
  800. * bitmap_onto(dst, tmp, relmap, bits);
  801. *
  802. * Then this table shows what various values of @dst would be, for
  803. * various @orig's. I list the zero-based positions of each set bit.
  804. * The tmp column shows the intermediate result, as computed by
  805. * using bitmap_fold() to fold the @orig bitmap modulo ten
  806. * (the weight of @relmap):
  807. *
  808. * =============== ============== =================
  809. * @orig tmp @dst
  810. * 0 0 40
  811. * 1 1 41
  812. * 9 9 95
  813. * 10 0 40 [#f1]_
  814. * 1 3 5 7 1 3 5 7 41 43 48 61
  815. * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
  816. * 0 9 18 27 0 9 8 7 40 61 74 95
  817. * 0 10 20 30 0 40
  818. * 0 11 22 33 0 1 2 3 40 41 42 43
  819. * 0 12 24 36 0 2 4 6 40 42 45 53
  820. * 78 102 211 1 2 8 41 42 74 [#f1]_
  821. * =============== ============== =================
  822. *
  823. * .. [#f1]
  824. *
  825. * For these marked lines, if we hadn't first done bitmap_fold()
  826. * into tmp, then the @dst result would have been empty.
  827. *
  828. * If either of @orig or @relmap is empty (no set bits), then @dst
  829. * will be returned empty.
  830. *
  831. * If (as explained above) the only set bits in @orig are in positions
  832. * m where m >= W, (where W is the weight of @relmap) then @dst will
  833. * once again be returned empty.
  834. *
  835. * All bits in @dst not set by the above rule are cleared.
  836. */
  837. void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  838. const unsigned long *relmap, unsigned int bits)
  839. {
  840. unsigned int n, m; /* same meaning as in above comment */
  841. if (dst == orig) /* following doesn't handle inplace mappings */
  842. return;
  843. bitmap_zero(dst, bits);
  844. /*
  845. * The following code is a more efficient, but less
  846. * obvious, equivalent to the loop:
  847. * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
  848. * n = bitmap_ord_to_pos(orig, m, bits);
  849. * if (test_bit(m, orig))
  850. * set_bit(n, dst);
  851. * }
  852. */
  853. m = 0;
  854. for_each_set_bit(n, relmap, bits) {
  855. /* m == bitmap_pos_to_ord(relmap, n, bits) */
  856. if (test_bit(m, orig))
  857. set_bit(n, dst);
  858. m++;
  859. }
  860. }
  861. EXPORT_SYMBOL(bitmap_onto);
  862. /**
  863. * bitmap_fold - fold larger bitmap into smaller, modulo specified size
  864. * @dst: resulting smaller bitmap
  865. * @orig: original larger bitmap
  866. * @sz: specified size
  867. * @nbits: number of bits in each of these bitmaps
  868. *
  869. * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
  870. * Clear all other bits in @dst. See further the comment and
  871. * Example [2] for bitmap_onto() for why and how to use this.
  872. */
  873. void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  874. unsigned int sz, unsigned int nbits)
  875. {
  876. unsigned int oldbit;
  877. if (dst == orig) /* following doesn't handle inplace mappings */
  878. return;
  879. bitmap_zero(dst, nbits);
  880. for_each_set_bit(oldbit, orig, nbits)
  881. set_bit(oldbit % sz, dst);
  882. }
  883. EXPORT_SYMBOL(bitmap_fold);
  884. /*
  885. * Common code for bitmap_*_region() routines.
  886. * bitmap: array of unsigned longs corresponding to the bitmap
  887. * pos: the beginning of the region
  888. * order: region size (log base 2 of number of bits)
  889. * reg_op: operation(s) to perform on that region of bitmap
  890. *
  891. * Can set, verify and/or release a region of bits in a bitmap,
  892. * depending on which combination of REG_OP_* flag bits is set.
  893. *
  894. * A region of a bitmap is a sequence of bits in the bitmap, of
  895. * some size '1 << order' (a power of two), aligned to that same
  896. * '1 << order' power of two.
  897. *
  898. * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
  899. * Returns 0 in all other cases and reg_ops.
  900. */
  901. enum {
  902. REG_OP_ISFREE, /* true if region is all zero bits */
  903. REG_OP_ALLOC, /* set all bits in region */
  904. REG_OP_RELEASE, /* clear all bits in region */
  905. };
  906. static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
  907. {
  908. int nbits_reg; /* number of bits in region */
  909. int index; /* index first long of region in bitmap */
  910. int offset; /* bit offset region in bitmap[index] */
  911. int nlongs_reg; /* num longs spanned by region in bitmap */
  912. int nbitsinlong; /* num bits of region in each spanned long */
  913. unsigned long mask; /* bitmask for one long of region */
  914. int i; /* scans bitmap by longs */
  915. int ret = 0; /* return value */
  916. /*
  917. * Either nlongs_reg == 1 (for small orders that fit in one long)
  918. * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
  919. */
  920. nbits_reg = 1 << order;
  921. index = pos / BITS_PER_LONG;
  922. offset = pos - (index * BITS_PER_LONG);
  923. nlongs_reg = BITS_TO_LONGS(nbits_reg);
  924. nbitsinlong = min(nbits_reg, BITS_PER_LONG);
  925. /*
  926. * Can't do "mask = (1UL << nbitsinlong) - 1", as that
  927. * overflows if nbitsinlong == BITS_PER_LONG.
  928. */
  929. mask = (1UL << (nbitsinlong - 1));
  930. mask += mask - 1;
  931. mask <<= offset;
  932. switch (reg_op) {
  933. case REG_OP_ISFREE:
  934. for (i = 0; i < nlongs_reg; i++) {
  935. if (bitmap[index + i] & mask)
  936. goto done;
  937. }
  938. ret = 1; /* all bits in region free (zero) */
  939. break;
  940. case REG_OP_ALLOC:
  941. for (i = 0; i < nlongs_reg; i++)
  942. bitmap[index + i] |= mask;
  943. break;
  944. case REG_OP_RELEASE:
  945. for (i = 0; i < nlongs_reg; i++)
  946. bitmap[index + i] &= ~mask;
  947. break;
  948. }
  949. done:
  950. return ret;
  951. }
  952. /**
  953. * bitmap_find_free_region - find a contiguous aligned mem region
  954. * @bitmap: array of unsigned longs corresponding to the bitmap
  955. * @bits: number of bits in the bitmap
  956. * @order: region size (log base 2 of number of bits) to find
  957. *
  958. * Find a region of free (zero) bits in a @bitmap of @bits bits and
  959. * allocate them (set them to one). Only consider regions of length
  960. * a power (@order) of two, aligned to that power of two, which
  961. * makes the search algorithm much faster.
  962. *
  963. * Return the bit offset in bitmap of the allocated region,
  964. * or -errno on failure.
  965. */
  966. int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
  967. {
  968. unsigned int pos, end; /* scans bitmap by regions of size order */
  969. for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
  970. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  971. continue;
  972. __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  973. return pos;
  974. }
  975. return -ENOMEM;
  976. }
  977. EXPORT_SYMBOL(bitmap_find_free_region);
  978. /**
  979. * bitmap_release_region - release allocated bitmap region
  980. * @bitmap: array of unsigned longs corresponding to the bitmap
  981. * @pos: beginning of bit region to release
  982. * @order: region size (log base 2 of number of bits) to release
  983. *
  984. * This is the complement to __bitmap_find_free_region() and releases
  985. * the found region (by clearing it in the bitmap).
  986. *
  987. * No return value.
  988. */
  989. void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
  990. {
  991. __reg_op(bitmap, pos, order, REG_OP_RELEASE);
  992. }
  993. EXPORT_SYMBOL(bitmap_release_region);
  994. /**
  995. * bitmap_allocate_region - allocate bitmap region
  996. * @bitmap: array of unsigned longs corresponding to the bitmap
  997. * @pos: beginning of bit region to allocate
  998. * @order: region size (log base 2 of number of bits) to allocate
  999. *
  1000. * Allocate (set bits in) a specified region of a bitmap.
  1001. *
  1002. * Return 0 on success, or %-EBUSY if specified region wasn't
  1003. * free (not all bits were zero).
  1004. */
  1005. int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
  1006. {
  1007. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  1008. return -EBUSY;
  1009. return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  1010. }
  1011. EXPORT_SYMBOL(bitmap_allocate_region);
  1012. /**
  1013. * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
  1014. * @dst: destination buffer
  1015. * @src: bitmap to copy
  1016. * @nbits: number of bits in the bitmap
  1017. *
  1018. * Require nbits % BITS_PER_LONG == 0.
  1019. */
  1020. #ifdef __BIG_ENDIAN
  1021. void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
  1022. {
  1023. unsigned int i;
  1024. for (i = 0; i < nbits/BITS_PER_LONG; i++) {
  1025. if (BITS_PER_LONG == 64)
  1026. dst[i] = cpu_to_le64(src[i]);
  1027. else
  1028. dst[i] = cpu_to_le32(src[i]);
  1029. }
  1030. }
  1031. EXPORT_SYMBOL(bitmap_copy_le);
  1032. #endif
  1033. unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
  1034. {
  1035. return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
  1036. flags);
  1037. }
  1038. EXPORT_SYMBOL(bitmap_alloc);
  1039. unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
  1040. {
  1041. return bitmap_alloc(nbits, flags | __GFP_ZERO);
  1042. }
  1043. EXPORT_SYMBOL(bitmap_zalloc);
  1044. void bitmap_free(const unsigned long *bitmap)
  1045. {
  1046. kfree(bitmap);
  1047. }
  1048. EXPORT_SYMBOL(bitmap_free);
  1049. #if BITS_PER_LONG == 64
  1050. /**
  1051. * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
  1052. * @bitmap: array of unsigned longs, the destination bitmap
  1053. * @buf: array of u32 (in host byte order), the source bitmap
  1054. * @nbits: number of bits in @bitmap
  1055. */
  1056. void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
  1057. {
  1058. unsigned int i, halfwords;
  1059. halfwords = DIV_ROUND_UP(nbits, 32);
  1060. for (i = 0; i < halfwords; i++) {
  1061. bitmap[i/2] = (unsigned long) buf[i];
  1062. if (++i < halfwords)
  1063. bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
  1064. }
  1065. /* Clear tail bits in last word beyond nbits. */
  1066. if (nbits % BITS_PER_LONG)
  1067. bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
  1068. }
  1069. EXPORT_SYMBOL(bitmap_from_arr32);
  1070. /**
  1071. * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
  1072. * @buf: array of u32 (in host byte order), the dest bitmap
  1073. * @bitmap: array of unsigned longs, the source bitmap
  1074. * @nbits: number of bits in @bitmap
  1075. */
  1076. void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
  1077. {
  1078. unsigned int i, halfwords;
  1079. halfwords = DIV_ROUND_UP(nbits, 32);
  1080. for (i = 0; i < halfwords; i++) {
  1081. buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
  1082. if (++i < halfwords)
  1083. buf[i] = (u32) (bitmap[i/2] >> 32);
  1084. }
  1085. /* Clear tail bits in last element of array beyond nbits. */
  1086. if (nbits % BITS_PER_LONG)
  1087. buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
  1088. }
  1089. EXPORT_SYMBOL(bitmap_to_arr32);
  1090. #endif