drbd_bitmap.c 44 KB

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