bitmap.c 35 KB

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