drbd_bitmap.c 46 KB

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