drbd_bitmap.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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. set_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. unsigned int done;
  769. unsigned flags;
  770. #define BM_AIO_COPY_PAGES 1
  771. #define BM_WRITE_ALL_PAGES 2
  772. int error;
  773. struct kref kref;
  774. };
  775. static void bm_aio_ctx_destroy(struct kref *kref)
  776. {
  777. struct bm_aio_ctx *ctx = container_of(kref, struct bm_aio_ctx, kref);
  778. put_ldev(ctx->mdev);
  779. kfree(ctx);
  780. }
  781. /* bv_page may be a copy, or may be the original */
  782. static void bm_async_io_complete(struct bio *bio, int error)
  783. {
  784. struct bm_aio_ctx *ctx = bio->bi_private;
  785. struct drbd_conf *mdev = ctx->mdev;
  786. struct drbd_bitmap *b = mdev->bitmap;
  787. unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
  788. int uptodate = bio_flagged(bio, BIO_UPTODATE);
  789. /* strange behavior of some lower level drivers...
  790. * fail the request by clearing the uptodate flag,
  791. * but do not return any error?!
  792. * do we want to WARN() on this? */
  793. if (!error && !uptodate)
  794. error = -EIO;
  795. if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
  796. !bm_test_page_unchanged(b->bm_pages[idx]))
  797. dev_warn(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. if (ctx->flags & BM_AIO_COPY_PAGES)
  814. mempool_free(bio->bi_io_vec[0].bv_page, drbd_md_io_page_pool);
  815. bio_put(bio);
  816. if (atomic_dec_and_test(&ctx->in_flight)) {
  817. ctx->done = 1;
  818. wake_up(&mdev->misc_wait);
  819. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  820. }
  821. }
  822. static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
  823. {
  824. struct bio *bio = bio_alloc_drbd(GFP_NOIO);
  825. struct drbd_conf *mdev = ctx->mdev;
  826. struct drbd_bitmap *b = mdev->bitmap;
  827. struct page *page;
  828. unsigned int len;
  829. sector_t on_disk_sector =
  830. mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
  831. on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
  832. /* this might happen with very small
  833. * flexible external meta data device,
  834. * or with PAGE_SIZE > 4k */
  835. len = min_t(unsigned int, PAGE_SIZE,
  836. (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
  837. /* serialize IO on this page */
  838. bm_page_lock_io(mdev, page_nr);
  839. /* before memcpy and submit,
  840. * so it can be redirtied any time */
  841. bm_set_page_unchanged(b->bm_pages[page_nr]);
  842. if (ctx->flags & BM_AIO_COPY_PAGES) {
  843. void *src, *dest;
  844. page = mempool_alloc(drbd_md_io_page_pool, __GFP_HIGHMEM|__GFP_WAIT);
  845. dest = kmap_atomic(page);
  846. src = kmap_atomic(b->bm_pages[page_nr]);
  847. memcpy(dest, src, PAGE_SIZE);
  848. kunmap_atomic(src);
  849. kunmap_atomic(dest);
  850. bm_store_page_idx(page, page_nr);
  851. } else
  852. page = b->bm_pages[page_nr];
  853. bio->bi_bdev = mdev->ldev->md_bdev;
  854. bio->bi_sector = on_disk_sector;
  855. /* bio_add_page of a single page to an empty bio will always succeed,
  856. * according to api. Do we want to assert that? */
  857. bio_add_page(bio, page, len, 0);
  858. bio->bi_private = ctx;
  859. bio->bi_end_io = bm_async_io_complete;
  860. if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
  861. bio->bi_rw |= rw;
  862. bio_endio(bio, -EIO);
  863. } else {
  864. submit_bio(rw, bio);
  865. /* this should not count as user activity and cause the
  866. * resync to throttle -- see drbd_rs_should_slow_down(). */
  867. atomic_add(len >> 9, &mdev->rs_sect_ev);
  868. }
  869. }
  870. /*
  871. * bm_rw: read/write the whole bitmap from/to its on disk location.
  872. */
  873. static int bm_rw(struct drbd_conf *mdev, int rw, unsigned flags, unsigned lazy_writeout_upper_idx) __must_hold(local)
  874. {
  875. struct bm_aio_ctx *ctx;
  876. struct drbd_bitmap *b = mdev->bitmap;
  877. int num_pages, i, count = 0;
  878. unsigned long now;
  879. char ppb[10];
  880. int err = 0;
  881. /*
  882. * We are protected against bitmap disappearing/resizing by holding an
  883. * ldev reference (caller must have called get_ldev()).
  884. * For read/write, we are protected against changes to the bitmap by
  885. * the bitmap lock (see drbd_bitmap_io).
  886. * For lazy writeout, we don't care for ongoing changes to the bitmap,
  887. * as we submit copies of pages anyways.
  888. */
  889. ctx = kmalloc(sizeof(struct bm_aio_ctx), GFP_NOIO);
  890. if (!ctx)
  891. return -ENOMEM;
  892. *ctx = (struct bm_aio_ctx) {
  893. .mdev = mdev,
  894. .in_flight = ATOMIC_INIT(1),
  895. .done = 0,
  896. .flags = flags,
  897. .error = 0,
  898. .kref = { ATOMIC_INIT(2) },
  899. };
  900. if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* put is in bm_aio_ctx_destroy() */
  901. dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in bm_rw()\n");
  902. kfree(ctx);
  903. return -ENODEV;
  904. }
  905. if (!ctx->flags)
  906. WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
  907. num_pages = b->bm_number_of_pages;
  908. now = jiffies;
  909. /* let the layers below us try to merge these bios... */
  910. for (i = 0; i < num_pages; i++) {
  911. /* ignore completely unchanged pages */
  912. if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
  913. break;
  914. if (rw & WRITE) {
  915. if (!(flags & BM_WRITE_ALL_PAGES) &&
  916. bm_test_page_unchanged(b->bm_pages[i])) {
  917. dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
  918. continue;
  919. }
  920. /* during lazy writeout,
  921. * ignore those pages not marked for lazy writeout. */
  922. if (lazy_writeout_upper_idx &&
  923. !bm_test_page_lazy_writeout(b->bm_pages[i])) {
  924. dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
  925. continue;
  926. }
  927. }
  928. atomic_inc(&ctx->in_flight);
  929. bm_page_io_async(ctx, i, rw);
  930. ++count;
  931. cond_resched();
  932. }
  933. /*
  934. * We initialize ctx->in_flight to one to make sure bm_async_io_complete
  935. * will not set ctx->done early, and decrement / test it here. If there
  936. * are still some bios in flight, we need to wait for them here.
  937. * If all IO is done already (or nothing had been submitted), there is
  938. * no need to wait. Still, we need to put the kref associated with the
  939. * "in_flight reached zero, all done" event.
  940. */
  941. if (!atomic_dec_and_test(&ctx->in_flight))
  942. wait_until_done_or_disk_failure(mdev, mdev->ldev, &ctx->done);
  943. else
  944. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  945. dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
  946. rw == WRITE ? "WRITE" : "READ",
  947. count, jiffies - now);
  948. if (ctx->error) {
  949. dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
  950. drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
  951. err = -EIO; /* ctx->error ? */
  952. }
  953. if (atomic_read(&ctx->in_flight))
  954. err = -EIO; /* Disk failed during IO... */
  955. now = jiffies;
  956. if (rw == WRITE) {
  957. drbd_md_flush(mdev);
  958. } else /* rw == READ */ {
  959. b->bm_set = bm_count_bits(b);
  960. dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
  961. jiffies - now);
  962. }
  963. now = b->bm_set;
  964. dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
  965. ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
  966. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  967. return err;
  968. }
  969. /**
  970. * drbd_bm_read() - Read the whole bitmap from its on disk location.
  971. * @mdev: DRBD device.
  972. */
  973. int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
  974. {
  975. return bm_rw(mdev, READ, 0, 0);
  976. }
  977. /**
  978. * drbd_bm_write() - Write the whole bitmap to its on disk location.
  979. * @mdev: DRBD device.
  980. *
  981. * Will only write pages that have changed since last IO.
  982. */
  983. int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
  984. {
  985. return bm_rw(mdev, WRITE, 0, 0);
  986. }
  987. /**
  988. * drbd_bm_write_all() - Write the whole bitmap to its on disk location.
  989. * @mdev: DRBD device.
  990. *
  991. * Will write all pages.
  992. */
  993. int drbd_bm_write_all(struct drbd_conf *mdev) __must_hold(local)
  994. {
  995. return bm_rw(mdev, WRITE, BM_WRITE_ALL_PAGES, 0);
  996. }
  997. /**
  998. * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
  999. * @mdev: DRBD device.
  1000. * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
  1001. */
  1002. int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
  1003. {
  1004. return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, upper_idx);
  1005. }
  1006. /**
  1007. * drbd_bm_write_copy_pages() - Write the whole bitmap to its on disk location.
  1008. * @mdev: DRBD device.
  1009. *
  1010. * Will only write pages that have changed since last IO.
  1011. * In contrast to drbd_bm_write(), this will copy the bitmap pages
  1012. * to temporary writeout pages. It is intended to trigger a full write-out
  1013. * while still allowing the bitmap to change, for example if a resync or online
  1014. * verify is aborted due to a failed peer disk, while local IO continues, or
  1015. * pending resync acks are still being processed.
  1016. */
  1017. int drbd_bm_write_copy_pages(struct drbd_conf *mdev) __must_hold(local)
  1018. {
  1019. return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, 0);
  1020. }
  1021. /**
  1022. * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
  1023. * @mdev: DRBD device.
  1024. * @idx: bitmap page index
  1025. *
  1026. * We don't want to special case on logical_block_size of the backend device,
  1027. * so we submit PAGE_SIZE aligned pieces.
  1028. * Note that on "most" systems, PAGE_SIZE is 4k.
  1029. *
  1030. * In case this becomes an issue on systems with larger PAGE_SIZE,
  1031. * we may want to change this again to write 4k aligned 4k pieces.
  1032. */
  1033. int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
  1034. {
  1035. struct bm_aio_ctx *ctx;
  1036. int err;
  1037. if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
  1038. dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
  1039. return 0;
  1040. }
  1041. ctx = kmalloc(sizeof(struct bm_aio_ctx), GFP_NOIO);
  1042. if (!ctx)
  1043. return -ENOMEM;
  1044. *ctx = (struct bm_aio_ctx) {
  1045. .mdev = mdev,
  1046. .in_flight = ATOMIC_INIT(1),
  1047. .done = 0,
  1048. .flags = BM_AIO_COPY_PAGES,
  1049. .error = 0,
  1050. .kref = { ATOMIC_INIT(2) },
  1051. };
  1052. if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* put is in bm_aio_ctx_destroy() */
  1053. dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in drbd_bm_write_page()\n");
  1054. kfree(ctx);
  1055. return -ENODEV;
  1056. }
  1057. bm_page_io_async(ctx, idx, WRITE_SYNC);
  1058. wait_until_done_or_disk_failure(mdev, mdev->ldev, &ctx->done);
  1059. if (ctx->error)
  1060. drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
  1061. /* that should force detach, so the in memory bitmap will be
  1062. * gone in a moment as well. */
  1063. mdev->bm_writ_cnt++;
  1064. err = atomic_read(&ctx->in_flight) ? -EIO : ctx->error;
  1065. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  1066. return err;
  1067. }
  1068. /* NOTE
  1069. * find_first_bit returns int, we return unsigned long.
  1070. * For this to work on 32bit arch with bitnumbers > (1<<32),
  1071. * we'd need to return u64, and get a whole lot of other places
  1072. * fixed where we still use unsigned long.
  1073. *
  1074. * this returns a bit number, NOT a sector!
  1075. */
  1076. static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
  1077. const int find_zero_bit)
  1078. {
  1079. struct drbd_bitmap *b = mdev->bitmap;
  1080. unsigned long *p_addr;
  1081. unsigned long bit_offset;
  1082. unsigned i;
  1083. if (bm_fo > b->bm_bits) {
  1084. dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
  1085. bm_fo = DRBD_END_OF_BITMAP;
  1086. } else {
  1087. while (bm_fo < b->bm_bits) {
  1088. /* bit offset of the first bit in the page */
  1089. bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
  1090. p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo));
  1091. if (find_zero_bit)
  1092. i = find_next_zero_bit_le(p_addr,
  1093. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1094. else
  1095. i = find_next_bit_le(p_addr,
  1096. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1097. __bm_unmap(p_addr);
  1098. if (i < PAGE_SIZE*8) {
  1099. bm_fo = bit_offset + i;
  1100. if (bm_fo >= b->bm_bits)
  1101. break;
  1102. goto found;
  1103. }
  1104. bm_fo = bit_offset + PAGE_SIZE*8;
  1105. }
  1106. bm_fo = DRBD_END_OF_BITMAP;
  1107. }
  1108. found:
  1109. return bm_fo;
  1110. }
  1111. static unsigned long bm_find_next(struct drbd_conf *mdev,
  1112. unsigned long bm_fo, const int find_zero_bit)
  1113. {
  1114. struct drbd_bitmap *b = mdev->bitmap;
  1115. unsigned long i = DRBD_END_OF_BITMAP;
  1116. ERR_IF(!b) return i;
  1117. ERR_IF(!b->bm_pages) return i;
  1118. spin_lock_irq(&b->bm_lock);
  1119. if (BM_DONT_TEST & b->bm_flags)
  1120. bm_print_lock_info(mdev);
  1121. i = __bm_find_next(mdev, bm_fo, find_zero_bit);
  1122. spin_unlock_irq(&b->bm_lock);
  1123. return i;
  1124. }
  1125. unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1126. {
  1127. return bm_find_next(mdev, bm_fo, 0);
  1128. }
  1129. #if 0
  1130. /* not yet needed for anything. */
  1131. unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1132. {
  1133. return bm_find_next(mdev, bm_fo, 1);
  1134. }
  1135. #endif
  1136. /* does not spin_lock_irqsave.
  1137. * you must take drbd_bm_lock() first */
  1138. unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1139. {
  1140. /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
  1141. return __bm_find_next(mdev, bm_fo, 0);
  1142. }
  1143. unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1144. {
  1145. /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
  1146. return __bm_find_next(mdev, bm_fo, 1);
  1147. }
  1148. /* returns number of bits actually changed.
  1149. * for val != 0, we change 0 -> 1, return code positive
  1150. * for val == 0, we change 1 -> 0, return code negative
  1151. * wants bitnr, not sector.
  1152. * expected to be called for only a few bits (e - s about BITS_PER_LONG).
  1153. * Must hold bitmap lock already. */
  1154. static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1155. unsigned long e, int val)
  1156. {
  1157. struct drbd_bitmap *b = mdev->bitmap;
  1158. unsigned long *p_addr = NULL;
  1159. unsigned long bitnr;
  1160. unsigned int last_page_nr = -1U;
  1161. int c = 0;
  1162. int changed_total = 0;
  1163. if (e >= b->bm_bits) {
  1164. dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
  1165. s, e, b->bm_bits);
  1166. e = b->bm_bits ? b->bm_bits -1 : 0;
  1167. }
  1168. for (bitnr = s; bitnr <= e; bitnr++) {
  1169. unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
  1170. if (page_nr != last_page_nr) {
  1171. if (p_addr)
  1172. __bm_unmap(p_addr);
  1173. if (c < 0)
  1174. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1175. else if (c > 0)
  1176. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1177. changed_total += c;
  1178. c = 0;
  1179. p_addr = __bm_map_pidx(b, page_nr);
  1180. last_page_nr = page_nr;
  1181. }
  1182. if (val)
  1183. c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1184. else
  1185. c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1186. }
  1187. if (p_addr)
  1188. __bm_unmap(p_addr);
  1189. if (c < 0)
  1190. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1191. else if (c > 0)
  1192. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1193. changed_total += c;
  1194. b->bm_set += changed_total;
  1195. return changed_total;
  1196. }
  1197. /* returns number of bits actually changed.
  1198. * for val != 0, we change 0 -> 1, return code positive
  1199. * for val == 0, we change 1 -> 0, return code negative
  1200. * wants bitnr, not sector */
  1201. static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1202. const unsigned long e, int val)
  1203. {
  1204. unsigned long flags;
  1205. struct drbd_bitmap *b = mdev->bitmap;
  1206. int c = 0;
  1207. ERR_IF(!b) return 1;
  1208. ERR_IF(!b->bm_pages) return 0;
  1209. spin_lock_irqsave(&b->bm_lock, flags);
  1210. if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
  1211. bm_print_lock_info(mdev);
  1212. c = __bm_change_bits_to(mdev, s, e, val);
  1213. spin_unlock_irqrestore(&b->bm_lock, flags);
  1214. return c;
  1215. }
  1216. /* returns number of bits changed 0 -> 1 */
  1217. int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1218. {
  1219. return bm_change_bits_to(mdev, s, e, 1);
  1220. }
  1221. /* returns number of bits changed 1 -> 0 */
  1222. int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1223. {
  1224. return -bm_change_bits_to(mdev, s, e, 0);
  1225. }
  1226. /* sets all bits in full words,
  1227. * from first_word up to, but not including, last_word */
  1228. static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
  1229. int page_nr, int first_word, int last_word)
  1230. {
  1231. int i;
  1232. int bits;
  1233. unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr]);
  1234. for (i = first_word; i < last_word; i++) {
  1235. bits = hweight_long(paddr[i]);
  1236. paddr[i] = ~0UL;
  1237. b->bm_set += BITS_PER_LONG - bits;
  1238. }
  1239. kunmap_atomic(paddr);
  1240. }
  1241. /* Same thing as drbd_bm_set_bits,
  1242. * but more efficient for a large bit range.
  1243. * You must first drbd_bm_lock().
  1244. * Can be called to set the whole bitmap in one go.
  1245. * Sets bits from s to e _inclusive_. */
  1246. void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1247. {
  1248. /* First set_bit from the first bit (s)
  1249. * up to the next long boundary (sl),
  1250. * then assign full words up to the last long boundary (el),
  1251. * then set_bit up to and including the last bit (e).
  1252. *
  1253. * Do not use memset, because we must account for changes,
  1254. * so we need to loop over the words with hweight() anyways.
  1255. */
  1256. struct drbd_bitmap *b = mdev->bitmap;
  1257. unsigned long sl = ALIGN(s,BITS_PER_LONG);
  1258. unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
  1259. int first_page;
  1260. int last_page;
  1261. int page_nr;
  1262. int first_word;
  1263. int last_word;
  1264. if (e - s <= 3*BITS_PER_LONG) {
  1265. /* don't bother; el and sl may even be wrong. */
  1266. spin_lock_irq(&b->bm_lock);
  1267. __bm_change_bits_to(mdev, s, e, 1);
  1268. spin_unlock_irq(&b->bm_lock);
  1269. return;
  1270. }
  1271. /* difference is large enough that we can trust sl and el */
  1272. spin_lock_irq(&b->bm_lock);
  1273. /* bits filling the current long */
  1274. if (sl)
  1275. __bm_change_bits_to(mdev, s, sl-1, 1);
  1276. first_page = sl >> (3 + PAGE_SHIFT);
  1277. last_page = el >> (3 + PAGE_SHIFT);
  1278. /* MLPP: modulo longs per page */
  1279. /* LWPP: long words per page */
  1280. first_word = MLPP(sl >> LN2_BPL);
  1281. last_word = LWPP;
  1282. /* first and full pages, unless first page == last page */
  1283. for (page_nr = first_page; page_nr < last_page; page_nr++) {
  1284. bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
  1285. spin_unlock_irq(&b->bm_lock);
  1286. cond_resched();
  1287. first_word = 0;
  1288. spin_lock_irq(&b->bm_lock);
  1289. }
  1290. /* last page (respectively only page, for first page == last page) */
  1291. last_word = MLPP(el >> LN2_BPL);
  1292. /* consider bitmap->bm_bits = 32768, bitmap->bm_number_of_pages = 1. (or multiples).
  1293. * ==> e = 32767, el = 32768, last_page = 2,
  1294. * and now last_word = 0.
  1295. * We do not want to touch last_page in this case,
  1296. * as we did not allocate it, it is not present in bitmap->bm_pages.
  1297. */
  1298. if (last_word)
  1299. bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
  1300. /* possibly trailing bits.
  1301. * example: (e & 63) == 63, el will be e+1.
  1302. * if that even was the very last bit,
  1303. * it would trigger an assert in __bm_change_bits_to()
  1304. */
  1305. if (el <= e)
  1306. __bm_change_bits_to(mdev, el, e, 1);
  1307. spin_unlock_irq(&b->bm_lock);
  1308. }
  1309. /* returns bit state
  1310. * wants bitnr, NOT sector.
  1311. * inherently racy... area needs to be locked by means of {al,rs}_lru
  1312. * 1 ... bit set
  1313. * 0 ... bit not set
  1314. * -1 ... first out of bounds access, stop testing for bits!
  1315. */
  1316. int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
  1317. {
  1318. unsigned long flags;
  1319. struct drbd_bitmap *b = mdev->bitmap;
  1320. unsigned long *p_addr;
  1321. int i;
  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. if (bitnr < b->bm_bits) {
  1328. p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
  1329. i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
  1330. bm_unmap(p_addr);
  1331. } else if (bitnr == b->bm_bits) {
  1332. i = -1;
  1333. } else { /* (bitnr > b->bm_bits) */
  1334. dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
  1335. i = 0;
  1336. }
  1337. spin_unlock_irqrestore(&b->bm_lock, flags);
  1338. return i;
  1339. }
  1340. /* returns number of bits set in the range [s, e] */
  1341. int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1342. {
  1343. unsigned long flags;
  1344. struct drbd_bitmap *b = mdev->bitmap;
  1345. unsigned long *p_addr = NULL;
  1346. unsigned long bitnr;
  1347. unsigned int page_nr = -1U;
  1348. int c = 0;
  1349. /* If this is called without a bitmap, that is a bug. But just to be
  1350. * robust in case we screwed up elsewhere, in that case pretend there
  1351. * was one dirty bit in the requested area, so we won't try to do a
  1352. * local read there (no bitmap probably implies no disk) */
  1353. ERR_IF(!b) return 1;
  1354. ERR_IF(!b->bm_pages) return 1;
  1355. spin_lock_irqsave(&b->bm_lock, flags);
  1356. if (BM_DONT_TEST & b->bm_flags)
  1357. bm_print_lock_info(mdev);
  1358. for (bitnr = s; bitnr <= e; bitnr++) {
  1359. unsigned int idx = bm_bit_to_page_idx(b, bitnr);
  1360. if (page_nr != idx) {
  1361. page_nr = idx;
  1362. if (p_addr)
  1363. bm_unmap(p_addr);
  1364. p_addr = bm_map_pidx(b, idx);
  1365. }
  1366. ERR_IF (bitnr >= b->bm_bits) {
  1367. dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
  1368. } else {
  1369. c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
  1370. }
  1371. }
  1372. if (p_addr)
  1373. bm_unmap(p_addr);
  1374. spin_unlock_irqrestore(&b->bm_lock, flags);
  1375. return c;
  1376. }
  1377. /* inherently racy...
  1378. * return value may be already out-of-date when this function returns.
  1379. * but the general usage is that this is only use during a cstate when bits are
  1380. * only cleared, not set, and typically only care for the case when the return
  1381. * value is zero, or we already "locked" this "bitmap extent" by other means.
  1382. *
  1383. * enr is bm-extent number, since we chose to name one sector (512 bytes)
  1384. * worth of the bitmap a "bitmap extent".
  1385. *
  1386. * TODO
  1387. * I think since we use it like a reference count, we should use the real
  1388. * reference count of some bitmap extent element from some lru instead...
  1389. *
  1390. */
  1391. int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
  1392. {
  1393. struct drbd_bitmap *b = mdev->bitmap;
  1394. int count, s, e;
  1395. unsigned long flags;
  1396. unsigned long *p_addr, *bm;
  1397. ERR_IF(!b) return 0;
  1398. ERR_IF(!b->bm_pages) return 0;
  1399. spin_lock_irqsave(&b->bm_lock, flags);
  1400. if (BM_DONT_TEST & b->bm_flags)
  1401. bm_print_lock_info(mdev);
  1402. s = S2W(enr);
  1403. e = min((size_t)S2W(enr+1), b->bm_words);
  1404. count = 0;
  1405. if (s < b->bm_words) {
  1406. int n = e-s;
  1407. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
  1408. bm = p_addr + MLPP(s);
  1409. while (n--)
  1410. count += hweight_long(*bm++);
  1411. bm_unmap(p_addr);
  1412. } else {
  1413. dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
  1414. }
  1415. spin_unlock_irqrestore(&b->bm_lock, flags);
  1416. return count;
  1417. }
  1418. /* Set all bits covered by the AL-extent al_enr.
  1419. * Returns number of bits changed. */
  1420. unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
  1421. {
  1422. struct drbd_bitmap *b = mdev->bitmap;
  1423. unsigned long *p_addr, *bm;
  1424. unsigned long weight;
  1425. unsigned long s, e;
  1426. int count, i, do_now;
  1427. ERR_IF(!b) return 0;
  1428. ERR_IF(!b->bm_pages) return 0;
  1429. spin_lock_irq(&b->bm_lock);
  1430. if (BM_DONT_SET & b->bm_flags)
  1431. bm_print_lock_info(mdev);
  1432. weight = b->bm_set;
  1433. s = al_enr * BM_WORDS_PER_AL_EXT;
  1434. e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
  1435. /* assert that s and e are on the same page */
  1436. D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
  1437. == s >> (PAGE_SHIFT - LN2_BPL + 3));
  1438. count = 0;
  1439. if (s < b->bm_words) {
  1440. i = do_now = e-s;
  1441. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
  1442. bm = p_addr + MLPP(s);
  1443. while (i--) {
  1444. count += hweight_long(*bm);
  1445. *bm = -1UL;
  1446. bm++;
  1447. }
  1448. bm_unmap(p_addr);
  1449. b->bm_set += do_now*BITS_PER_LONG - count;
  1450. if (e == b->bm_words)
  1451. b->bm_set -= bm_clear_surplus(b);
  1452. } else {
  1453. dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
  1454. }
  1455. weight = b->bm_set - weight;
  1456. spin_unlock_irq(&b->bm_lock);
  1457. return weight;
  1458. }