bitmap.c 36 KB

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