drbd_bitmap.c 48 KB

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