bitmap.c 35 KB

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