bitmap.c 36 KB

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