drbd_bitmap.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /*
  2. drbd_bitmap.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2004-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/string.h>
  22. #include <linux/drbd.h>
  23. #include <linux/slab.h>
  24. #include <asm/kmap_types.h>
  25. #include "drbd_int.h"
  26. /* OPAQUE outside this file!
  27. * interface defined in drbd_int.h
  28. * convention:
  29. * function name drbd_bm_... => used elsewhere, "public".
  30. * function name bm_... => internal to implementation, "private".
  31. */
  32. /*
  33. * LIMITATIONS:
  34. * We want to support >= peta byte of backend storage, while for now still using
  35. * a granularity of one bit per 4KiB of storage.
  36. * 1 << 50 bytes backend storage (1 PiB)
  37. * 1 << (50 - 12) bits needed
  38. * 38 --> we need u64 to index and count bits
  39. * 1 << (38 - 3) bitmap bytes needed
  40. * 35 --> we still need u64 to index and count bytes
  41. * (that's 32 GiB of bitmap for 1 PiB storage)
  42. * 1 << (35 - 2) 32bit longs needed
  43. * 33 --> we'd even need u64 to index and count 32bit long words.
  44. * 1 << (35 - 3) 64bit longs needed
  45. * 32 --> we could get away with a 32bit unsigned int to index and count
  46. * 64bit long words, but I rather stay with unsigned long for now.
  47. * We probably should neither count nor point to bytes or long words
  48. * directly, but either by bitnumber, or by page index and offset.
  49. * 1 << (35 - 12)
  50. * 22 --> we need that much 4KiB pages of bitmap.
  51. * 1 << (22 + 3) --> on a 64bit arch,
  52. * we need 32 MiB to store the array of page pointers.
  53. *
  54. * Because I'm lazy, and because the resulting patch was too large, too ugly
  55. * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
  56. * (1 << 32) bits * 4k storage.
  57. *
  58. * bitmap storage and IO:
  59. * Bitmap is stored little endian on disk, and is kept little endian in
  60. * core memory. Currently we still hold the full bitmap in core as long
  61. * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
  62. * seems excessive.
  63. *
  64. * We plan to reduce the amount of in-core bitmap pages by pageing them in
  65. * and out against their on-disk location as necessary, but need to make
  66. * sure we don't cause too much meta data IO, and must not deadlock in
  67. * tight memory situations. This needs some more work.
  68. */
  69. /*
  70. * NOTE
  71. * Access to the *bm_pages is protected by bm_lock.
  72. * It is safe to read the other members within the lock.
  73. *
  74. * drbd_bm_set_bits is called from bio_endio callbacks,
  75. * We may be called with irq already disabled,
  76. * so we need spin_lock_irqsave().
  77. * And we need the kmap_atomic.
  78. */
  79. struct drbd_bitmap {
  80. struct page **bm_pages;
  81. spinlock_t bm_lock;
  82. /* see LIMITATIONS: above */
  83. unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
  84. unsigned long bm_bits;
  85. size_t bm_words;
  86. size_t bm_number_of_pages;
  87. sector_t bm_dev_capacity;
  88. struct mutex bm_change; /* serializes resize operations */
  89. wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
  90. unsigned long bm_flags;
  91. /* debugging aid, in case we are still racy somewhere */
  92. char *bm_why;
  93. struct task_struct *bm_task;
  94. };
  95. /* definition of bits in bm_flags */
  96. #define BM_LOCKED 0
  97. // #define BM_MD_IO_ERROR 1 unused now.
  98. #define BM_P_VMALLOCED 2
  99. static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  100. unsigned long e, int val, const enum km_type km);
  101. static int bm_is_locked(struct drbd_bitmap *b)
  102. {
  103. return test_bit(BM_LOCKED, &b->bm_flags);
  104. }
  105. #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
  106. static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
  107. {
  108. struct drbd_bitmap *b = mdev->bitmap;
  109. if (!__ratelimit(&drbd_ratelimit_state))
  110. return;
  111. dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
  112. current == mdev->receiver.task ? "receiver" :
  113. current == mdev->asender.task ? "asender" :
  114. current == mdev->worker.task ? "worker" : current->comm,
  115. func, b->bm_why ?: "?",
  116. b->bm_task == mdev->receiver.task ? "receiver" :
  117. b->bm_task == mdev->asender.task ? "asender" :
  118. b->bm_task == mdev->worker.task ? "worker" : "?");
  119. }
  120. void drbd_bm_lock(struct drbd_conf *mdev, char *why)
  121. {
  122. struct drbd_bitmap *b = mdev->bitmap;
  123. int trylock_failed;
  124. if (!b) {
  125. dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
  126. return;
  127. }
  128. trylock_failed = !mutex_trylock(&b->bm_change);
  129. if (trylock_failed) {
  130. dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
  131. current == mdev->receiver.task ? "receiver" :
  132. current == mdev->asender.task ? "asender" :
  133. current == mdev->worker.task ? "worker" : current->comm,
  134. why, b->bm_why ?: "?",
  135. b->bm_task == mdev->receiver.task ? "receiver" :
  136. b->bm_task == mdev->asender.task ? "asender" :
  137. b->bm_task == mdev->worker.task ? "worker" : "?");
  138. mutex_lock(&b->bm_change);
  139. }
  140. if (__test_and_set_bit(BM_LOCKED, &b->bm_flags))
  141. dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
  142. b->bm_why = why;
  143. b->bm_task = current;
  144. }
  145. void drbd_bm_unlock(struct drbd_conf *mdev)
  146. {
  147. struct drbd_bitmap *b = mdev->bitmap;
  148. if (!b) {
  149. dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
  150. return;
  151. }
  152. if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags))
  153. dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
  154. b->bm_why = NULL;
  155. b->bm_task = NULL;
  156. mutex_unlock(&b->bm_change);
  157. }
  158. /* we store some "meta" info about our pages in page->private */
  159. /* at a granularity of 4k storage per bitmap bit:
  160. * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
  161. * 1<<38 bits,
  162. * 1<<23 4k bitmap pages.
  163. * Use 24 bits as page index, covers 2 peta byte storage
  164. * at a granularity of 4k per bit.
  165. * Used to report the failed page idx on io error from the endio handlers.
  166. */
  167. #define BM_PAGE_IDX_MASK ((1UL<<24)-1)
  168. /* this page is currently read in, or written back */
  169. #define BM_PAGE_IO_LOCK 31
  170. /* if there has been an IO error for this page */
  171. #define BM_PAGE_IO_ERROR 30
  172. /* this is to be able to intelligently skip disk IO,
  173. * set if bits have been set since last IO. */
  174. #define BM_PAGE_NEED_WRITEOUT 29
  175. /* to mark for lazy writeout once syncer cleared all clearable bits,
  176. * we if bits have been cleared since last IO. */
  177. #define BM_PAGE_LAZY_WRITEOUT 28
  178. /* store_page_idx uses non-atomic assingment. It is only used directly after
  179. * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
  180. * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
  181. * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
  182. * requires it all to be atomic as well. */
  183. static void bm_store_page_idx(struct page *page, unsigned long idx)
  184. {
  185. BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
  186. page_private(page) |= idx;
  187. }
  188. static unsigned long bm_page_to_idx(struct page *page)
  189. {
  190. return page_private(page) & BM_PAGE_IDX_MASK;
  191. }
  192. /* As is very unlikely that the same page is under IO from more than one
  193. * context, we can get away with a bit per page and one wait queue per bitmap.
  194. */
  195. static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
  196. {
  197. struct drbd_bitmap *b = mdev->bitmap;
  198. void *addr = &page_private(b->bm_pages[page_nr]);
  199. wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
  200. }
  201. static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
  202. {
  203. struct drbd_bitmap *b = mdev->bitmap;
  204. void *addr = &page_private(b->bm_pages[page_nr]);
  205. clear_bit(BM_PAGE_IO_LOCK, addr);
  206. smp_mb__after_clear_bit();
  207. wake_up(&mdev->bitmap->bm_io_wait);
  208. }
  209. /* set _before_ submit_io, so it may be reset due to being changed
  210. * while this page is in flight... will get submitted later again */
  211. static void bm_set_page_unchanged(struct page *page)
  212. {
  213. /* use cmpxchg? */
  214. clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
  215. clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
  216. }
  217. static void bm_set_page_need_writeout(struct page *page)
  218. {
  219. set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
  220. }
  221. static int bm_test_page_unchanged(struct page *page)
  222. {
  223. volatile const unsigned long *addr = &page_private(page);
  224. return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
  225. }
  226. static void bm_set_page_io_err(struct page *page)
  227. {
  228. set_bit(BM_PAGE_IO_ERROR, &page_private(page));
  229. }
  230. static void bm_clear_page_io_err(struct page *page)
  231. {
  232. clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
  233. }
  234. static void bm_set_page_lazy_writeout(struct page *page)
  235. {
  236. set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
  237. }
  238. static int bm_test_page_lazy_writeout(struct page *page)
  239. {
  240. return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
  241. }
  242. /* on a 32bit box, this would allow for exactly (2<<38) bits. */
  243. static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
  244. {
  245. /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
  246. unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
  247. BUG_ON(page_nr >= b->bm_number_of_pages);
  248. return page_nr;
  249. }
  250. static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
  251. {
  252. /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
  253. unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
  254. BUG_ON(page_nr >= b->bm_number_of_pages);
  255. return page_nr;
  256. }
  257. static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
  258. {
  259. struct page *page = b->bm_pages[idx];
  260. return (unsigned long *) kmap_atomic(page, km);
  261. }
  262. static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
  263. {
  264. return __bm_map_pidx(b, idx, KM_IRQ1);
  265. }
  266. static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
  267. {
  268. kunmap_atomic(p_addr, km);
  269. };
  270. static void bm_unmap(unsigned long *p_addr)
  271. {
  272. return __bm_unmap(p_addr, KM_IRQ1);
  273. }
  274. /* long word offset of _bitmap_ sector */
  275. #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
  276. /* word offset from start of bitmap to word number _in_page_
  277. * modulo longs per page
  278. #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
  279. hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
  280. so do it explicitly:
  281. */
  282. #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
  283. /* Long words per page */
  284. #define LWPP (PAGE_SIZE/sizeof(long))
  285. /*
  286. * actually most functions herein should take a struct drbd_bitmap*, not a
  287. * struct drbd_conf*, but for the debug macros I like to have the mdev around
  288. * to be able to report device specific.
  289. */
  290. static void bm_free_pages(struct page **pages, unsigned long number)
  291. {
  292. unsigned long i;
  293. if (!pages)
  294. return;
  295. for (i = 0; i < number; i++) {
  296. if (!pages[i]) {
  297. printk(KERN_ALERT "drbd: bm_free_pages tried to free "
  298. "a NULL pointer; i=%lu n=%lu\n",
  299. i, number);
  300. continue;
  301. }
  302. __free_page(pages[i]);
  303. pages[i] = NULL;
  304. }
  305. }
  306. static void bm_vk_free(void *ptr, int v)
  307. {
  308. if (v)
  309. vfree(ptr);
  310. else
  311. kfree(ptr);
  312. }
  313. /*
  314. * "have" and "want" are NUMBER OF PAGES.
  315. */
  316. static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
  317. {
  318. struct page **old_pages = b->bm_pages;
  319. struct page **new_pages, *page;
  320. unsigned int i, bytes, vmalloced = 0;
  321. unsigned long have = b->bm_number_of_pages;
  322. BUG_ON(have == 0 && old_pages != NULL);
  323. BUG_ON(have != 0 && old_pages == NULL);
  324. if (have == want)
  325. return old_pages;
  326. /* Trying kmalloc first, falling back to vmalloc.
  327. * GFP_KERNEL is ok, as this is done when a lower level disk is
  328. * "attached" to the drbd. Context is receiver thread or cqueue
  329. * thread. As we have no disk yet, we are not in the IO path,
  330. * not even the IO path of the peer. */
  331. bytes = sizeof(struct page *)*want;
  332. new_pages = kmalloc(bytes, GFP_KERNEL);
  333. if (!new_pages) {
  334. new_pages = vmalloc(bytes);
  335. if (!new_pages)
  336. return NULL;
  337. vmalloced = 1;
  338. }
  339. memset(new_pages, 0, bytes);
  340. if (want >= have) {
  341. for (i = 0; i < have; i++)
  342. new_pages[i] = old_pages[i];
  343. for (; i < want; i++) {
  344. page = alloc_page(GFP_HIGHUSER);
  345. if (!page) {
  346. bm_free_pages(new_pages + have, i - have);
  347. bm_vk_free(new_pages, vmalloced);
  348. return NULL;
  349. }
  350. /* we want to know which page it is
  351. * from the endio handlers */
  352. bm_store_page_idx(page, i);
  353. new_pages[i] = page;
  354. }
  355. } else {
  356. for (i = 0; i < want; i++)
  357. new_pages[i] = old_pages[i];
  358. /* NOT HERE, we are outside the spinlock!
  359. bm_free_pages(old_pages + want, have - want);
  360. */
  361. }
  362. if (vmalloced)
  363. set_bit(BM_P_VMALLOCED, &b->bm_flags);
  364. else
  365. clear_bit(BM_P_VMALLOCED, &b->bm_flags);
  366. return new_pages;
  367. }
  368. /*
  369. * called on driver init only. TODO call when a device is created.
  370. * allocates the drbd_bitmap, and stores it in mdev->bitmap.
  371. */
  372. int drbd_bm_init(struct drbd_conf *mdev)
  373. {
  374. struct drbd_bitmap *b = mdev->bitmap;
  375. WARN_ON(b != NULL);
  376. b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
  377. if (!b)
  378. return -ENOMEM;
  379. spin_lock_init(&b->bm_lock);
  380. mutex_init(&b->bm_change);
  381. init_waitqueue_head(&b->bm_io_wait);
  382. mdev->bitmap = b;
  383. return 0;
  384. }
  385. sector_t drbd_bm_capacity(struct drbd_conf *mdev)
  386. {
  387. ERR_IF(!mdev->bitmap) return 0;
  388. return mdev->bitmap->bm_dev_capacity;
  389. }
  390. /* called on driver unload. TODO: call when a device is destroyed.
  391. */
  392. void drbd_bm_cleanup(struct drbd_conf *mdev)
  393. {
  394. ERR_IF (!mdev->bitmap) return;
  395. bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
  396. bm_vk_free(mdev->bitmap->bm_pages, test_bit(BM_P_VMALLOCED, &mdev->bitmap->bm_flags));
  397. kfree(mdev->bitmap);
  398. mdev->bitmap = NULL;
  399. }
  400. /*
  401. * since (b->bm_bits % BITS_PER_LONG) != 0,
  402. * this masks out the remaining bits.
  403. * Returns the number of bits cleared.
  404. */
  405. #define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
  406. #define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
  407. #define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
  408. static int bm_clear_surplus(struct drbd_bitmap *b)
  409. {
  410. unsigned long mask;
  411. unsigned long *p_addr, *bm;
  412. int tmp;
  413. int cleared = 0;
  414. /* number of bits modulo bits per page */
  415. tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
  416. /* mask the used bits of the word containing the last bit */
  417. mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
  418. /* bitmap is always stored little endian,
  419. * on disk and in core memory alike */
  420. mask = cpu_to_lel(mask);
  421. p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
  422. bm = p_addr + (tmp/BITS_PER_LONG);
  423. if (mask) {
  424. /* If mask != 0, we are not exactly aligned, so bm now points
  425. * to the long containing the last bit.
  426. * If mask == 0, bm already points to the word immediately
  427. * after the last (long word aligned) bit. */
  428. cleared = hweight_long(*bm & ~mask);
  429. *bm &= mask;
  430. bm++;
  431. }
  432. if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
  433. /* on a 32bit arch, we may need to zero out
  434. * a padding long to align with a 64bit remote */
  435. cleared += hweight_long(*bm);
  436. *bm = 0;
  437. }
  438. bm_unmap(p_addr);
  439. return cleared;
  440. }
  441. static void bm_set_surplus(struct drbd_bitmap *b)
  442. {
  443. unsigned long mask;
  444. unsigned long *p_addr, *bm;
  445. int tmp;
  446. /* number of bits modulo bits per page */
  447. tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
  448. /* mask the used bits of the word containing the last bit */
  449. mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
  450. /* bitmap is always stored little endian,
  451. * on disk and in core memory alike */
  452. mask = cpu_to_lel(mask);
  453. p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
  454. bm = p_addr + (tmp/BITS_PER_LONG);
  455. if (mask) {
  456. /* If mask != 0, we are not exactly aligned, so bm now points
  457. * to the long containing the last bit.
  458. * If mask == 0, bm already points to the word immediately
  459. * after the last (long word aligned) bit. */
  460. *bm |= ~mask;
  461. bm++;
  462. }
  463. if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
  464. /* on a 32bit arch, we may need to zero out
  465. * a padding long to align with a 64bit remote */
  466. *bm = ~0UL;
  467. }
  468. bm_unmap(p_addr);
  469. }
  470. /* you better not modify the bitmap while this is running,
  471. * or its results will be stale */
  472. static unsigned long bm_count_bits(struct drbd_bitmap *b)
  473. {
  474. unsigned long *p_addr;
  475. unsigned long bits = 0;
  476. unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
  477. int idx, i, last_word;
  478. /* all but last page */
  479. for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
  480. p_addr = __bm_map_pidx(b, idx, KM_USER0);
  481. for (i = 0; i < LWPP; i++)
  482. bits += hweight_long(p_addr[i]);
  483. __bm_unmap(p_addr, KM_USER0);
  484. cond_resched();
  485. }
  486. /* last (or only) page */
  487. last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
  488. p_addr = __bm_map_pidx(b, idx, KM_USER0);
  489. for (i = 0; i < last_word; i++)
  490. bits += hweight_long(p_addr[i]);
  491. p_addr[last_word] &= cpu_to_lel(mask);
  492. bits += hweight_long(p_addr[last_word]);
  493. /* 32bit arch, may have an unused padding long */
  494. if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
  495. p_addr[last_word+1] = 0;
  496. __bm_unmap(p_addr, KM_USER0);
  497. return bits;
  498. }
  499. /* offset and len in long words.*/
  500. static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
  501. {
  502. unsigned long *p_addr, *bm;
  503. unsigned int idx;
  504. size_t do_now, end;
  505. end = offset + len;
  506. if (end > b->bm_words) {
  507. printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
  508. return;
  509. }
  510. while (offset < end) {
  511. do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
  512. idx = bm_word_to_page_idx(b, offset);
  513. p_addr = bm_map_pidx(b, idx);
  514. bm = p_addr + MLPP(offset);
  515. if (bm+do_now > p_addr + LWPP) {
  516. printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
  517. p_addr, bm, (int)do_now);
  518. } else
  519. memset(bm, c, do_now * sizeof(long));
  520. bm_unmap(p_addr);
  521. bm_set_page_need_writeout(b->bm_pages[idx]);
  522. offset += do_now;
  523. }
  524. }
  525. /*
  526. * make sure the bitmap has enough room for the attached storage,
  527. * if necessary, resize.
  528. * called whenever we may have changed the device size.
  529. * returns -ENOMEM if we could not allocate enough memory, 0 on success.
  530. * In case this is actually a resize, we copy the old bitmap into the new one.
  531. * Otherwise, the bitmap is initialized to all bits set.
  532. */
  533. int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
  534. {
  535. struct drbd_bitmap *b = mdev->bitmap;
  536. unsigned long bits, words, owords, obits;
  537. unsigned long want, have, onpages; /* number of pages */
  538. struct page **npages, **opages = NULL;
  539. int err = 0, growing;
  540. int opages_vmalloced;
  541. ERR_IF(!b) return -ENOMEM;
  542. drbd_bm_lock(mdev, "resize");
  543. dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
  544. (unsigned long long)capacity);
  545. if (capacity == b->bm_dev_capacity)
  546. goto out;
  547. opages_vmalloced = test_bit(BM_P_VMALLOCED, &b->bm_flags);
  548. if (capacity == 0) {
  549. spin_lock_irq(&b->bm_lock);
  550. opages = b->bm_pages;
  551. onpages = b->bm_number_of_pages;
  552. owords = b->bm_words;
  553. b->bm_pages = NULL;
  554. b->bm_number_of_pages =
  555. b->bm_set =
  556. b->bm_bits =
  557. b->bm_words =
  558. b->bm_dev_capacity = 0;
  559. spin_unlock_irq(&b->bm_lock);
  560. bm_free_pages(opages, onpages);
  561. bm_vk_free(opages, opages_vmalloced);
  562. goto out;
  563. }
  564. bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
  565. /* if we would use
  566. words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
  567. a 32bit host could present the wrong number of words
  568. to a 64bit host.
  569. */
  570. words = ALIGN(bits, 64) >> LN2_BPL;
  571. if (get_ldev(mdev)) {
  572. u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
  573. put_ldev(mdev);
  574. if (bits > bits_on_disk) {
  575. dev_info(DEV, "bits = %lu\n", bits);
  576. dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
  577. err = -ENOSPC;
  578. goto out;
  579. }
  580. }
  581. want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
  582. have = b->bm_number_of_pages;
  583. if (want == have) {
  584. D_ASSERT(b->bm_pages != NULL);
  585. npages = b->bm_pages;
  586. } else {
  587. if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
  588. npages = NULL;
  589. else
  590. npages = bm_realloc_pages(b, want);
  591. }
  592. if (!npages) {
  593. err = -ENOMEM;
  594. goto out;
  595. }
  596. spin_lock_irq(&b->bm_lock);
  597. opages = b->bm_pages;
  598. owords = b->bm_words;
  599. obits = b->bm_bits;
  600. growing = bits > obits;
  601. if (opages && growing && set_new_bits)
  602. bm_set_surplus(b);
  603. b->bm_pages = npages;
  604. b->bm_number_of_pages = want;
  605. b->bm_bits = bits;
  606. b->bm_words = words;
  607. b->bm_dev_capacity = capacity;
  608. if (growing) {
  609. if (set_new_bits) {
  610. bm_memset(b, owords, 0xff, words-owords);
  611. b->bm_set += bits - obits;
  612. } else
  613. bm_memset(b, owords, 0x00, words-owords);
  614. }
  615. if (want < have) {
  616. /* implicit: (opages != NULL) && (opages != npages) */
  617. bm_free_pages(opages + want, have - want);
  618. }
  619. (void)bm_clear_surplus(b);
  620. spin_unlock_irq(&b->bm_lock);
  621. if (opages != npages)
  622. bm_vk_free(opages, opages_vmalloced);
  623. if (!growing)
  624. b->bm_set = bm_count_bits(b);
  625. dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
  626. out:
  627. drbd_bm_unlock(mdev);
  628. return err;
  629. }
  630. /* inherently racy:
  631. * if not protected by other means, return value may be out of date when
  632. * leaving this function...
  633. * we still need to lock it, since it is important that this returns
  634. * bm_set == 0 precisely.
  635. *
  636. * maybe bm_set should be atomic_t ?
  637. */
  638. unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
  639. {
  640. struct drbd_bitmap *b = mdev->bitmap;
  641. unsigned long s;
  642. unsigned long flags;
  643. ERR_IF(!b) return 0;
  644. ERR_IF(!b->bm_pages) return 0;
  645. spin_lock_irqsave(&b->bm_lock, flags);
  646. s = b->bm_set;
  647. spin_unlock_irqrestore(&b->bm_lock, flags);
  648. return s;
  649. }
  650. unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
  651. {
  652. unsigned long s;
  653. /* if I don't have a disk, I don't know about out-of-sync status */
  654. if (!get_ldev_if_state(mdev, D_NEGOTIATING))
  655. return 0;
  656. s = _drbd_bm_total_weight(mdev);
  657. put_ldev(mdev);
  658. return s;
  659. }
  660. size_t drbd_bm_words(struct drbd_conf *mdev)
  661. {
  662. struct drbd_bitmap *b = mdev->bitmap;
  663. ERR_IF(!b) return 0;
  664. ERR_IF(!b->bm_pages) return 0;
  665. return b->bm_words;
  666. }
  667. unsigned long drbd_bm_bits(struct drbd_conf *mdev)
  668. {
  669. struct drbd_bitmap *b = mdev->bitmap;
  670. ERR_IF(!b) return 0;
  671. return b->bm_bits;
  672. }
  673. /* merge number words from buffer into the bitmap starting at offset.
  674. * buffer[i] is expected to be little endian unsigned long.
  675. * bitmap must be locked by drbd_bm_lock.
  676. * currently only used from receive_bitmap.
  677. */
  678. void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
  679. unsigned long *buffer)
  680. {
  681. struct drbd_bitmap *b = mdev->bitmap;
  682. unsigned long *p_addr, *bm;
  683. unsigned long word, bits;
  684. unsigned int idx;
  685. size_t end, do_now;
  686. end = offset + number;
  687. ERR_IF(!b) return;
  688. ERR_IF(!b->bm_pages) return;
  689. if (number == 0)
  690. return;
  691. WARN_ON(offset >= b->bm_words);
  692. WARN_ON(end > b->bm_words);
  693. spin_lock_irq(&b->bm_lock);
  694. while (offset < end) {
  695. do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
  696. idx = bm_word_to_page_idx(b, offset);
  697. p_addr = bm_map_pidx(b, idx);
  698. bm = p_addr + MLPP(offset);
  699. offset += do_now;
  700. while (do_now--) {
  701. bits = hweight_long(*bm);
  702. word = *bm | *buffer++;
  703. *bm++ = word;
  704. b->bm_set += hweight_long(word) - bits;
  705. }
  706. bm_unmap(p_addr);
  707. bm_set_page_need_writeout(b->bm_pages[idx]);
  708. }
  709. /* with 32bit <-> 64bit cross-platform connect
  710. * this is only correct for current usage,
  711. * where we _know_ that we are 64 bit aligned,
  712. * and know that this function is used in this way, too...
  713. */
  714. if (end == b->bm_words)
  715. b->bm_set -= bm_clear_surplus(b);
  716. spin_unlock_irq(&b->bm_lock);
  717. }
  718. /* copy number words from the bitmap starting at offset into the buffer.
  719. * buffer[i] will be little endian unsigned long.
  720. */
  721. void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
  722. unsigned long *buffer)
  723. {
  724. struct drbd_bitmap *b = mdev->bitmap;
  725. unsigned long *p_addr, *bm;
  726. size_t end, do_now;
  727. end = offset + number;
  728. ERR_IF(!b) return;
  729. ERR_IF(!b->bm_pages) return;
  730. spin_lock_irq(&b->bm_lock);
  731. if ((offset >= b->bm_words) ||
  732. (end > b->bm_words) ||
  733. (number <= 0))
  734. dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
  735. (unsigned long) offset,
  736. (unsigned long) number,
  737. (unsigned long) b->bm_words);
  738. else {
  739. while (offset < end) {
  740. do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
  741. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
  742. bm = p_addr + MLPP(offset);
  743. offset += do_now;
  744. while (do_now--)
  745. *buffer++ = *bm++;
  746. bm_unmap(p_addr);
  747. }
  748. }
  749. spin_unlock_irq(&b->bm_lock);
  750. }
  751. /* set all bits in the bitmap */
  752. void drbd_bm_set_all(struct drbd_conf *mdev)
  753. {
  754. struct drbd_bitmap *b = mdev->bitmap;
  755. ERR_IF(!b) return;
  756. ERR_IF(!b->bm_pages) return;
  757. spin_lock_irq(&b->bm_lock);
  758. bm_memset(b, 0, 0xff, b->bm_words);
  759. (void)bm_clear_surplus(b);
  760. b->bm_set = b->bm_bits;
  761. spin_unlock_irq(&b->bm_lock);
  762. }
  763. /* clear all bits in the bitmap */
  764. void drbd_bm_clear_all(struct drbd_conf *mdev)
  765. {
  766. struct drbd_bitmap *b = mdev->bitmap;
  767. ERR_IF(!b) return;
  768. ERR_IF(!b->bm_pages) return;
  769. spin_lock_irq(&b->bm_lock);
  770. bm_memset(b, 0, 0, b->bm_words);
  771. b->bm_set = 0;
  772. spin_unlock_irq(&b->bm_lock);
  773. }
  774. struct bm_aio_ctx {
  775. struct drbd_conf *mdev;
  776. atomic_t in_flight;
  777. wait_queue_head_t io_wait;
  778. unsigned flags;
  779. #define BM_AIO_COPY_PAGES 1
  780. int error;
  781. };
  782. /* bv_page may be a copy, or may be the original */
  783. static void bm_async_io_complete(struct bio *bio, int error)
  784. {
  785. struct bm_aio_ctx *ctx = bio->bi_private;
  786. struct drbd_conf *mdev = ctx->mdev;
  787. struct drbd_bitmap *b = mdev->bitmap;
  788. unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
  789. int uptodate = bio_flagged(bio, BIO_UPTODATE);
  790. /* strange behavior of some lower level drivers...
  791. * fail the request by clearing the uptodate flag,
  792. * but do not return any error?!
  793. * do we want to WARN() on this? */
  794. if (!error && !uptodate)
  795. error = -EIO;
  796. if (!bm_test_page_unchanged(b->bm_pages[idx]))
  797. dev_info(DEV, "bitmap page idx %u changed during IO!\n", idx);
  798. if (error) {
  799. /* ctx error will hold the completed-last non-zero error code,
  800. * in case error codes differ. */
  801. ctx->error = error;
  802. bm_set_page_io_err(b->bm_pages[idx]);
  803. /* Not identical to on disk version of it.
  804. * Is BM_PAGE_IO_ERROR enough? */
  805. if (__ratelimit(&drbd_ratelimit_state))
  806. dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
  807. error, idx);
  808. } else {
  809. bm_clear_page_io_err(b->bm_pages[idx]);
  810. dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
  811. }
  812. bm_page_unlock_io(mdev, idx);
  813. /* FIXME give back to page pool */
  814. if (ctx->flags & BM_AIO_COPY_PAGES)
  815. put_page(bio->bi_io_vec[0].bv_page);
  816. bio_put(bio);
  817. if (atomic_dec_and_test(&ctx->in_flight))
  818. wake_up(&ctx->io_wait);
  819. }
  820. static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
  821. {
  822. /* we are process context. we always get a bio */
  823. struct bio *bio = bio_alloc(GFP_KERNEL, 1);
  824. struct drbd_conf *mdev = ctx->mdev;
  825. struct drbd_bitmap *b = mdev->bitmap;
  826. struct page *page;
  827. unsigned int len;
  828. sector_t on_disk_sector =
  829. mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
  830. on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
  831. /* this might happen with very small
  832. * flexible external meta data device,
  833. * or with PAGE_SIZE > 4k */
  834. len = min_t(unsigned int, PAGE_SIZE,
  835. (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
  836. /* serialize IO on this page */
  837. bm_page_lock_io(mdev, page_nr);
  838. /* before memcpy and submit,
  839. * so it can be redirtied any time */
  840. bm_set_page_unchanged(b->bm_pages[page_nr]);
  841. if (ctx->flags & BM_AIO_COPY_PAGES) {
  842. /* FIXME alloc_page is good enough for now, but actually needs
  843. * to use pre-allocated page pool */
  844. void *src, *dest;
  845. page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
  846. dest = kmap_atomic(page, KM_USER0);
  847. src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
  848. memcpy(dest, src, PAGE_SIZE);
  849. kunmap_atomic(src, KM_USER1);
  850. kunmap_atomic(dest, KM_USER0);
  851. bm_store_page_idx(page, page_nr);
  852. } else
  853. page = b->bm_pages[page_nr];
  854. bio->bi_bdev = mdev->ldev->md_bdev;
  855. bio->bi_sector = on_disk_sector;
  856. bio_add_page(bio, page, len, 0);
  857. bio->bi_private = ctx;
  858. bio->bi_end_io = bm_async_io_complete;
  859. if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
  860. bio->bi_rw |= rw;
  861. bio_endio(bio, -EIO);
  862. } else {
  863. submit_bio(rw, bio);
  864. }
  865. }
  866. /*
  867. * bm_rw: read/write the whole bitmap from/to its on disk location.
  868. */
  869. static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
  870. {
  871. struct bm_aio_ctx ctx =
  872. { .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0 };
  873. struct drbd_bitmap *b = mdev->bitmap;
  874. int num_pages, i, count = 0;
  875. unsigned long now;
  876. char ppb[10];
  877. int err = 0;
  878. /*
  879. * We are protected against bitmap disappearing/resizing by holding an
  880. * ldev reference (caller must have called get_ldev()).
  881. * For read/write, we are protected against changes to the bitmap by
  882. * the bitmap lock (see drbd_bitmap_io).
  883. * For lazy writeout, we don't care for ongoing changes to the bitmap,
  884. * as we submit copies of pages anyways.
  885. */
  886. if (!ctx.flags)
  887. WARN_ON(!bm_is_locked(b));
  888. num_pages = b->bm_number_of_pages;
  889. now = jiffies;
  890. ctx.mdev = mdev;
  891. atomic_set(&ctx.in_flight, 1); /* one extra ref */
  892. init_waitqueue_head(&ctx.io_wait);
  893. ctx.error = 0;
  894. /* let the layers below us try to merge these bios... */
  895. for (i = 0; i < num_pages; i++) {
  896. /* ignore completely unchanged pages */
  897. if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
  898. break;
  899. if (rw & WRITE) {
  900. if (bm_test_page_unchanged(b->bm_pages[i])) {
  901. dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
  902. continue;
  903. }
  904. /* during lazy writeout,
  905. * ignore those pages not marked for lazy writeout. */
  906. if (lazy_writeout_upper_idx &&
  907. !bm_test_page_lazy_writeout(b->bm_pages[i])) {
  908. dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
  909. continue;
  910. }
  911. }
  912. atomic_inc(&ctx.in_flight);
  913. bm_page_io_async(&ctx, i, rw);
  914. ++count;
  915. cond_resched();
  916. }
  917. atomic_dec(&ctx.in_flight); /* drop the extra ref */
  918. wait_event(ctx.io_wait, atomic_read(&ctx.in_flight) == 0);
  919. dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
  920. rw == WRITE ? "WRITE" : "READ",
  921. count, jiffies - now);
  922. if (ctx.error) {
  923. dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
  924. drbd_chk_io_error(mdev, 1, true);
  925. err = -EIO; /* ctx.error ? */
  926. }
  927. now = jiffies;
  928. if (rw == WRITE) {
  929. drbd_md_flush(mdev);
  930. } else /* rw == READ */ {
  931. b->bm_set = bm_count_bits(b);
  932. dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
  933. jiffies - now);
  934. }
  935. now = b->bm_set;
  936. dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
  937. ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
  938. return err;
  939. }
  940. /**
  941. * drbd_bm_read() - Read the whole bitmap from its on disk location.
  942. * @mdev: DRBD device.
  943. */
  944. int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
  945. {
  946. return bm_rw(mdev, READ, 0);
  947. }
  948. /**
  949. * drbd_bm_write() - Write the whole bitmap to its on disk location.
  950. * @mdev: DRBD device.
  951. *
  952. * Will only write pages that have changed since last IO.
  953. */
  954. int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
  955. {
  956. return bm_rw(mdev, WRITE, 0);
  957. }
  958. /**
  959. * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
  960. * @mdev: DRBD device.
  961. * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
  962. */
  963. int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
  964. {
  965. return bm_rw(mdev, WRITE, upper_idx);
  966. }
  967. /**
  968. * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
  969. * @mdev: DRBD device.
  970. * @idx: bitmap page index
  971. *
  972. * We don't want to special case on logical_block_size of the backend device,
  973. * so we submit PAGE_SIZE aligned pieces.
  974. * Note that on "most" systems, PAGE_SIZE is 4k.
  975. *
  976. * In case this becomes an issue on systems with larger PAGE_SIZE,
  977. * we may want to change this again to write 4k aligned 4k pieces.
  978. */
  979. int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
  980. {
  981. struct bm_aio_ctx ctx = { .flags = BM_AIO_COPY_PAGES, };
  982. if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
  983. dev_info(DEV, "skipped bm page write for idx %u\n", idx);
  984. return 0;
  985. }
  986. ctx.mdev = mdev;
  987. atomic_set(&ctx.in_flight, 1);
  988. init_waitqueue_head(&ctx.io_wait);
  989. bm_page_io_async(&ctx, idx, WRITE_SYNC);
  990. wait_event(ctx.io_wait, atomic_read(&ctx.in_flight) == 0);
  991. if (ctx.error)
  992. drbd_chk_io_error(mdev, 1, true);
  993. /* that should force detach, so the in memory bitmap will be
  994. * gone in a moment as well. */
  995. mdev->bm_writ_cnt++;
  996. return ctx.error;
  997. }
  998. /* NOTE
  999. * find_first_bit returns int, we return unsigned long.
  1000. * For this to work on 32bit arch with bitnumbers > (1<<32),
  1001. * we'd need to return u64, and get a whole lot of other places
  1002. * fixed where we still use unsigned long.
  1003. *
  1004. * this returns a bit number, NOT a sector!
  1005. */
  1006. static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
  1007. const int find_zero_bit, const enum km_type km)
  1008. {
  1009. struct drbd_bitmap *b = mdev->bitmap;
  1010. unsigned long *p_addr;
  1011. unsigned long bit_offset;
  1012. unsigned i;
  1013. if (bm_fo > b->bm_bits) {
  1014. dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
  1015. bm_fo = DRBD_END_OF_BITMAP;
  1016. } else {
  1017. while (bm_fo < b->bm_bits) {
  1018. /* bit offset of the first bit in the page */
  1019. bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
  1020. p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
  1021. if (find_zero_bit)
  1022. i = generic_find_next_zero_le_bit(p_addr,
  1023. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1024. else
  1025. i = generic_find_next_le_bit(p_addr,
  1026. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1027. __bm_unmap(p_addr, km);
  1028. if (i < PAGE_SIZE*8) {
  1029. bm_fo = bit_offset + i;
  1030. if (bm_fo >= b->bm_bits)
  1031. break;
  1032. goto found;
  1033. }
  1034. bm_fo = bit_offset + PAGE_SIZE*8;
  1035. }
  1036. bm_fo = DRBD_END_OF_BITMAP;
  1037. }
  1038. found:
  1039. return bm_fo;
  1040. }
  1041. static unsigned long bm_find_next(struct drbd_conf *mdev,
  1042. unsigned long bm_fo, const int find_zero_bit)
  1043. {
  1044. struct drbd_bitmap *b = mdev->bitmap;
  1045. unsigned long i = DRBD_END_OF_BITMAP;
  1046. ERR_IF(!b) return i;
  1047. ERR_IF(!b->bm_pages) return i;
  1048. spin_lock_irq(&b->bm_lock);
  1049. if (bm_is_locked(b))
  1050. bm_print_lock_info(mdev);
  1051. i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
  1052. spin_unlock_irq(&b->bm_lock);
  1053. return i;
  1054. }
  1055. unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1056. {
  1057. return bm_find_next(mdev, bm_fo, 0);
  1058. }
  1059. #if 0
  1060. /* not yet needed for anything. */
  1061. unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1062. {
  1063. return bm_find_next(mdev, bm_fo, 1);
  1064. }
  1065. #endif
  1066. /* does not spin_lock_irqsave.
  1067. * you must take drbd_bm_lock() first */
  1068. unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1069. {
  1070. /* WARN_ON(!bm_is_locked(mdev)); */
  1071. return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
  1072. }
  1073. unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1074. {
  1075. /* WARN_ON(!bm_is_locked(mdev)); */
  1076. return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
  1077. }
  1078. /* returns number of bits actually changed.
  1079. * for val != 0, we change 0 -> 1, return code positive
  1080. * for val == 0, we change 1 -> 0, return code negative
  1081. * wants bitnr, not sector.
  1082. * expected to be called for only a few bits (e - s about BITS_PER_LONG).
  1083. * Must hold bitmap lock already. */
  1084. static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1085. unsigned long e, int val, const enum km_type km)
  1086. {
  1087. struct drbd_bitmap *b = mdev->bitmap;
  1088. unsigned long *p_addr = NULL;
  1089. unsigned long bitnr;
  1090. unsigned int last_page_nr = -1U;
  1091. int c = 0;
  1092. int changed_total = 0;
  1093. if (e >= b->bm_bits) {
  1094. dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
  1095. s, e, b->bm_bits);
  1096. e = b->bm_bits ? b->bm_bits -1 : 0;
  1097. }
  1098. for (bitnr = s; bitnr <= e; bitnr++) {
  1099. unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
  1100. if (page_nr != last_page_nr) {
  1101. if (p_addr)
  1102. __bm_unmap(p_addr, km);
  1103. if (c < 0)
  1104. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1105. else if (c > 0)
  1106. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1107. changed_total += c;
  1108. c = 0;
  1109. p_addr = __bm_map_pidx(b, page_nr, km);
  1110. last_page_nr = page_nr;
  1111. }
  1112. if (val)
  1113. c += (0 == generic___test_and_set_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1114. else
  1115. c -= (0 != generic___test_and_clear_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1116. }
  1117. if (p_addr)
  1118. __bm_unmap(p_addr, km);
  1119. if (c < 0)
  1120. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1121. else if (c > 0)
  1122. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1123. changed_total += c;
  1124. b->bm_set += changed_total;
  1125. return changed_total;
  1126. }
  1127. /* returns number of bits actually changed.
  1128. * for val != 0, we change 0 -> 1, return code positive
  1129. * for val == 0, we change 1 -> 0, return code negative
  1130. * wants bitnr, not sector */
  1131. static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1132. const unsigned long e, int val)
  1133. {
  1134. unsigned long flags;
  1135. struct drbd_bitmap *b = mdev->bitmap;
  1136. int c = 0;
  1137. ERR_IF(!b) return 1;
  1138. ERR_IF(!b->bm_pages) return 0;
  1139. spin_lock_irqsave(&b->bm_lock, flags);
  1140. if (bm_is_locked(b))
  1141. bm_print_lock_info(mdev);
  1142. c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
  1143. spin_unlock_irqrestore(&b->bm_lock, flags);
  1144. return c;
  1145. }
  1146. /* returns number of bits changed 0 -> 1 */
  1147. int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1148. {
  1149. return bm_change_bits_to(mdev, s, e, 1);
  1150. }
  1151. /* returns number of bits changed 1 -> 0 */
  1152. int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1153. {
  1154. return -bm_change_bits_to(mdev, s, e, 0);
  1155. }
  1156. /* sets all bits in full words,
  1157. * from first_word up to, but not including, last_word */
  1158. static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
  1159. int page_nr, int first_word, int last_word)
  1160. {
  1161. int i;
  1162. int bits;
  1163. unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0);
  1164. for (i = first_word; i < last_word; i++) {
  1165. bits = hweight_long(paddr[i]);
  1166. paddr[i] = ~0UL;
  1167. b->bm_set += BITS_PER_LONG - bits;
  1168. }
  1169. kunmap_atomic(paddr, KM_USER0);
  1170. }
  1171. /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
  1172. * You must first drbd_bm_lock().
  1173. * Can be called to set the whole bitmap in one go.
  1174. * Sets bits from s to e _inclusive_. */
  1175. void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1176. {
  1177. /* First set_bit from the first bit (s)
  1178. * up to the next long boundary (sl),
  1179. * then assign full words up to the last long boundary (el),
  1180. * then set_bit up to and including the last bit (e).
  1181. *
  1182. * Do not use memset, because we must account for changes,
  1183. * so we need to loop over the words with hweight() anyways.
  1184. */
  1185. unsigned long sl = ALIGN(s,BITS_PER_LONG);
  1186. unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
  1187. int first_page;
  1188. int last_page;
  1189. int page_nr;
  1190. int first_word;
  1191. int last_word;
  1192. if (e - s <= 3*BITS_PER_LONG) {
  1193. /* don't bother; el and sl may even be wrong. */
  1194. __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
  1195. return;
  1196. }
  1197. /* difference is large enough that we can trust sl and el */
  1198. /* bits filling the current long */
  1199. if (sl)
  1200. __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0);
  1201. first_page = sl >> (3 + PAGE_SHIFT);
  1202. last_page = el >> (3 + PAGE_SHIFT);
  1203. /* MLPP: modulo longs per page */
  1204. /* LWPP: long words per page */
  1205. first_word = MLPP(sl >> LN2_BPL);
  1206. last_word = LWPP;
  1207. /* first and full pages, unless first page == last page */
  1208. for (page_nr = first_page; page_nr < last_page; page_nr++) {
  1209. bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
  1210. cond_resched();
  1211. first_word = 0;
  1212. }
  1213. /* last page (respectively only page, for first page == last page) */
  1214. last_word = MLPP(el >> LN2_BPL);
  1215. bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
  1216. /* possibly trailing bits.
  1217. * example: (e & 63) == 63, el will be e+1.
  1218. * if that even was the very last bit,
  1219. * it would trigger an assert in __bm_change_bits_to()
  1220. */
  1221. if (el <= e)
  1222. __bm_change_bits_to(mdev, el, e, 1, KM_USER0);
  1223. }
  1224. /* returns bit state
  1225. * wants bitnr, NOT sector.
  1226. * inherently racy... area needs to be locked by means of {al,rs}_lru
  1227. * 1 ... bit set
  1228. * 0 ... bit not set
  1229. * -1 ... first out of bounds access, stop testing for bits!
  1230. */
  1231. int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
  1232. {
  1233. unsigned long flags;
  1234. struct drbd_bitmap *b = mdev->bitmap;
  1235. unsigned long *p_addr;
  1236. int i;
  1237. ERR_IF(!b) return 0;
  1238. ERR_IF(!b->bm_pages) return 0;
  1239. spin_lock_irqsave(&b->bm_lock, flags);
  1240. if (bm_is_locked(b))
  1241. bm_print_lock_info(mdev);
  1242. if (bitnr < b->bm_bits) {
  1243. p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
  1244. i = generic_test_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
  1245. bm_unmap(p_addr);
  1246. } else if (bitnr == b->bm_bits) {
  1247. i = -1;
  1248. } else { /* (bitnr > b->bm_bits) */
  1249. dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
  1250. i = 0;
  1251. }
  1252. spin_unlock_irqrestore(&b->bm_lock, flags);
  1253. return i;
  1254. }
  1255. /* returns number of bits set in the range [s, e] */
  1256. int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1257. {
  1258. unsigned long flags;
  1259. struct drbd_bitmap *b = mdev->bitmap;
  1260. unsigned long *p_addr = NULL;
  1261. unsigned long bitnr;
  1262. unsigned int page_nr = -1U;
  1263. int c = 0;
  1264. /* If this is called without a bitmap, that is a bug. But just to be
  1265. * robust in case we screwed up elsewhere, in that case pretend there
  1266. * was one dirty bit in the requested area, so we won't try to do a
  1267. * local read there (no bitmap probably implies no disk) */
  1268. ERR_IF(!b) return 1;
  1269. ERR_IF(!b->bm_pages) return 1;
  1270. spin_lock_irqsave(&b->bm_lock, flags);
  1271. if (bm_is_locked(b))
  1272. bm_print_lock_info(mdev);
  1273. for (bitnr = s; bitnr <= e; bitnr++) {
  1274. unsigned int idx = bm_bit_to_page_idx(b, bitnr);
  1275. if (page_nr != idx) {
  1276. page_nr = idx;
  1277. if (p_addr)
  1278. bm_unmap(p_addr);
  1279. p_addr = bm_map_pidx(b, idx);
  1280. }
  1281. ERR_IF (bitnr >= b->bm_bits) {
  1282. dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
  1283. } else {
  1284. c += (0 != generic_test_le_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
  1285. }
  1286. }
  1287. if (p_addr)
  1288. bm_unmap(p_addr);
  1289. spin_unlock_irqrestore(&b->bm_lock, flags);
  1290. return c;
  1291. }
  1292. /* inherently racy...
  1293. * return value may be already out-of-date when this function returns.
  1294. * but the general usage is that this is only use during a cstate when bits are
  1295. * only cleared, not set, and typically only care for the case when the return
  1296. * value is zero, or we already "locked" this "bitmap extent" by other means.
  1297. *
  1298. * enr is bm-extent number, since we chose to name one sector (512 bytes)
  1299. * worth of the bitmap a "bitmap extent".
  1300. *
  1301. * TODO
  1302. * I think since we use it like a reference count, we should use the real
  1303. * reference count of some bitmap extent element from some lru instead...
  1304. *
  1305. */
  1306. int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
  1307. {
  1308. struct drbd_bitmap *b = mdev->bitmap;
  1309. int count, s, e;
  1310. unsigned long flags;
  1311. unsigned long *p_addr, *bm;
  1312. ERR_IF(!b) return 0;
  1313. ERR_IF(!b->bm_pages) return 0;
  1314. spin_lock_irqsave(&b->bm_lock, flags);
  1315. if (bm_is_locked(b))
  1316. bm_print_lock_info(mdev);
  1317. s = S2W(enr);
  1318. e = min((size_t)S2W(enr+1), b->bm_words);
  1319. count = 0;
  1320. if (s < b->bm_words) {
  1321. int n = e-s;
  1322. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
  1323. bm = p_addr + MLPP(s);
  1324. while (n--)
  1325. count += hweight_long(*bm++);
  1326. bm_unmap(p_addr);
  1327. } else {
  1328. dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
  1329. }
  1330. spin_unlock_irqrestore(&b->bm_lock, flags);
  1331. return count;
  1332. }
  1333. /* Set all bits covered by the AL-extent al_enr.
  1334. * Returns number of bits changed. */
  1335. unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
  1336. {
  1337. struct drbd_bitmap *b = mdev->bitmap;
  1338. unsigned long *p_addr, *bm;
  1339. unsigned long weight;
  1340. unsigned long s, e;
  1341. int count, i, do_now;
  1342. ERR_IF(!b) return 0;
  1343. ERR_IF(!b->bm_pages) return 0;
  1344. spin_lock_irq(&b->bm_lock);
  1345. if (bm_is_locked(b))
  1346. bm_print_lock_info(mdev);
  1347. weight = b->bm_set;
  1348. s = al_enr * BM_WORDS_PER_AL_EXT;
  1349. e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
  1350. /* assert that s and e are on the same page */
  1351. D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
  1352. == s >> (PAGE_SHIFT - LN2_BPL + 3));
  1353. count = 0;
  1354. if (s < b->bm_words) {
  1355. i = do_now = e-s;
  1356. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
  1357. bm = p_addr + MLPP(s);
  1358. while (i--) {
  1359. count += hweight_long(*bm);
  1360. *bm = -1UL;
  1361. bm++;
  1362. }
  1363. bm_unmap(p_addr);
  1364. b->bm_set += do_now*BITS_PER_LONG - count;
  1365. if (e == b->bm_words)
  1366. b->bm_set -= bm_clear_surplus(b);
  1367. } else {
  1368. dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
  1369. }
  1370. weight = b->bm_set - weight;
  1371. spin_unlock_irq(&b->bm_lock);
  1372. return weight;
  1373. }