drbd_bitmap.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. set_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_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 = kmalloc(bytes, GFP_NOIO);
  339. if (!new_pages) {
  340. new_pages = __vmalloc(bytes,
  341. GFP_NOIO | __GFP_HIGHMEM,
  342. PAGE_KERNEL);
  343. if (!new_pages)
  344. return NULL;
  345. vmalloced = 1;
  346. }
  347. memset(new_pages, 0, bytes);
  348. if (want >= have) {
  349. for (i = 0; i < have; i++)
  350. new_pages[i] = old_pages[i];
  351. for (; i < want; i++) {
  352. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  353. if (!page) {
  354. bm_free_pages(new_pages + have, i - have);
  355. bm_vk_free(new_pages, vmalloced);
  356. return NULL;
  357. }
  358. /* we want to know which page it is
  359. * from the endio handlers */
  360. bm_store_page_idx(page, i);
  361. new_pages[i] = page;
  362. }
  363. } else {
  364. for (i = 0; i < want; i++)
  365. new_pages[i] = old_pages[i];
  366. /* NOT HERE, we are outside the spinlock!
  367. bm_free_pages(old_pages + want, have - want);
  368. */
  369. }
  370. if (vmalloced)
  371. b->bm_flags |= BM_P_VMALLOCED;
  372. else
  373. b->bm_flags &= ~BM_P_VMALLOCED;
  374. return new_pages;
  375. }
  376. /*
  377. * called on driver init only. TODO call when a device is created.
  378. * allocates the drbd_bitmap, and stores it in mdev->bitmap.
  379. */
  380. int drbd_bm_init(struct drbd_conf *mdev)
  381. {
  382. struct drbd_bitmap *b = mdev->bitmap;
  383. WARN_ON(b != NULL);
  384. b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
  385. if (!b)
  386. return -ENOMEM;
  387. spin_lock_init(&b->bm_lock);
  388. mutex_init(&b->bm_change);
  389. init_waitqueue_head(&b->bm_io_wait);
  390. mdev->bitmap = b;
  391. return 0;
  392. }
  393. sector_t drbd_bm_capacity(struct drbd_conf *mdev)
  394. {
  395. if (!expect(mdev->bitmap))
  396. return 0;
  397. return mdev->bitmap->bm_dev_capacity;
  398. }
  399. /* called on driver unload. TODO: call when a device is destroyed.
  400. */
  401. void drbd_bm_cleanup(struct drbd_conf *mdev)
  402. {
  403. if (!expect(mdev->bitmap))
  404. return;
  405. bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
  406. bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
  407. kfree(mdev->bitmap);
  408. mdev->bitmap = NULL;
  409. }
  410. /*
  411. * since (b->bm_bits % BITS_PER_LONG) != 0,
  412. * this masks out the remaining bits.
  413. * Returns the number of bits cleared.
  414. */
  415. #define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
  416. #define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
  417. #define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
  418. static int bm_clear_surplus(struct drbd_bitmap *b)
  419. {
  420. unsigned long mask;
  421. unsigned long *p_addr, *bm;
  422. int tmp;
  423. int cleared = 0;
  424. /* number of bits modulo bits per page */
  425. tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
  426. /* mask the used bits of the word containing the last bit */
  427. mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
  428. /* bitmap is always stored little endian,
  429. * on disk and in core memory alike */
  430. mask = cpu_to_lel(mask);
  431. p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
  432. bm = p_addr + (tmp/BITS_PER_LONG);
  433. if (mask) {
  434. /* If mask != 0, we are not exactly aligned, so bm now points
  435. * to the long containing the last bit.
  436. * If mask == 0, bm already points to the word immediately
  437. * after the last (long word aligned) bit. */
  438. cleared = hweight_long(*bm & ~mask);
  439. *bm &= mask;
  440. bm++;
  441. }
  442. if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
  443. /* on a 32bit arch, we may need to zero out
  444. * a padding long to align with a 64bit remote */
  445. cleared += hweight_long(*bm);
  446. *bm = 0;
  447. }
  448. bm_unmap(p_addr);
  449. return cleared;
  450. }
  451. static void bm_set_surplus(struct drbd_bitmap *b)
  452. {
  453. unsigned long mask;
  454. unsigned long *p_addr, *bm;
  455. int tmp;
  456. /* number of bits modulo bits per page */
  457. tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
  458. /* mask the used bits of the word containing the last bit */
  459. mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
  460. /* bitmap is always stored little endian,
  461. * on disk and in core memory alike */
  462. mask = cpu_to_lel(mask);
  463. p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
  464. bm = p_addr + (tmp/BITS_PER_LONG);
  465. if (mask) {
  466. /* If mask != 0, we are not exactly aligned, so bm now points
  467. * to the long containing the last bit.
  468. * If mask == 0, bm already points to the word immediately
  469. * after the last (long word aligned) bit. */
  470. *bm |= ~mask;
  471. bm++;
  472. }
  473. if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
  474. /* on a 32bit arch, we may need to zero out
  475. * a padding long to align with a 64bit remote */
  476. *bm = ~0UL;
  477. }
  478. bm_unmap(p_addr);
  479. }
  480. /* you better not modify the bitmap while this is running,
  481. * or its results will be stale */
  482. static unsigned long bm_count_bits(struct drbd_bitmap *b)
  483. {
  484. unsigned long *p_addr;
  485. unsigned long bits = 0;
  486. unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
  487. int idx, i, last_word;
  488. /* all but last page */
  489. for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
  490. p_addr = __bm_map_pidx(b, idx, KM_USER0);
  491. for (i = 0; i < LWPP; i++)
  492. bits += hweight_long(p_addr[i]);
  493. __bm_unmap(p_addr, KM_USER0);
  494. cond_resched();
  495. }
  496. /* last (or only) page */
  497. last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
  498. p_addr = __bm_map_pidx(b, idx, KM_USER0);
  499. for (i = 0; i < last_word; i++)
  500. bits += hweight_long(p_addr[i]);
  501. p_addr[last_word] &= cpu_to_lel(mask);
  502. bits += hweight_long(p_addr[last_word]);
  503. /* 32bit arch, may have an unused padding long */
  504. if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
  505. p_addr[last_word+1] = 0;
  506. __bm_unmap(p_addr, KM_USER0);
  507. return bits;
  508. }
  509. /* offset and len in long words.*/
  510. static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
  511. {
  512. unsigned long *p_addr, *bm;
  513. unsigned int idx;
  514. size_t do_now, end;
  515. end = offset + len;
  516. if (end > b->bm_words) {
  517. printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
  518. return;
  519. }
  520. while (offset < end) {
  521. do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
  522. idx = bm_word_to_page_idx(b, offset);
  523. p_addr = bm_map_pidx(b, idx);
  524. bm = p_addr + MLPP(offset);
  525. if (bm+do_now > p_addr + LWPP) {
  526. printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
  527. p_addr, bm, (int)do_now);
  528. } else
  529. memset(bm, c, do_now * sizeof(long));
  530. bm_unmap(p_addr);
  531. bm_set_page_need_writeout(b->bm_pages[idx]);
  532. offset += do_now;
  533. }
  534. }
  535. /*
  536. * make sure the bitmap has enough room for the attached storage,
  537. * if necessary, resize.
  538. * called whenever we may have changed the device size.
  539. * returns -ENOMEM if we could not allocate enough memory, 0 on success.
  540. * In case this is actually a resize, we copy the old bitmap into the new one.
  541. * Otherwise, the bitmap is initialized to all bits set.
  542. */
  543. int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
  544. {
  545. struct drbd_bitmap *b = mdev->bitmap;
  546. unsigned long bits, words, owords, obits;
  547. unsigned long want, have, onpages; /* number of pages */
  548. struct page **npages, **opages = NULL;
  549. int err = 0, growing;
  550. int opages_vmalloced;
  551. if (!expect(b))
  552. return -ENOMEM;
  553. drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
  554. dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
  555. (unsigned long long)capacity);
  556. if (capacity == b->bm_dev_capacity)
  557. goto out;
  558. opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
  559. if (capacity == 0) {
  560. spin_lock_irq(&b->bm_lock);
  561. opages = b->bm_pages;
  562. onpages = b->bm_number_of_pages;
  563. owords = b->bm_words;
  564. b->bm_pages = NULL;
  565. b->bm_number_of_pages =
  566. b->bm_set =
  567. b->bm_bits =
  568. b->bm_words =
  569. b->bm_dev_capacity = 0;
  570. spin_unlock_irq(&b->bm_lock);
  571. bm_free_pages(opages, onpages);
  572. bm_vk_free(opages, opages_vmalloced);
  573. goto out;
  574. }
  575. bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
  576. /* if we would use
  577. words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
  578. a 32bit host could present the wrong number of words
  579. to a 64bit host.
  580. */
  581. words = ALIGN(bits, 64) >> LN2_BPL;
  582. if (get_ldev(mdev)) {
  583. u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
  584. put_ldev(mdev);
  585. if (bits > bits_on_disk) {
  586. dev_info(DEV, "bits = %lu\n", bits);
  587. dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
  588. err = -ENOSPC;
  589. goto out;
  590. }
  591. }
  592. want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
  593. have = b->bm_number_of_pages;
  594. if (want == have) {
  595. D_ASSERT(b->bm_pages != NULL);
  596. npages = b->bm_pages;
  597. } else {
  598. if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
  599. npages = NULL;
  600. else
  601. npages = bm_realloc_pages(b, want);
  602. }
  603. if (!npages) {
  604. err = -ENOMEM;
  605. goto out;
  606. }
  607. spin_lock_irq(&b->bm_lock);
  608. opages = b->bm_pages;
  609. owords = b->bm_words;
  610. obits = b->bm_bits;
  611. growing = bits > obits;
  612. if (opages && growing && set_new_bits)
  613. bm_set_surplus(b);
  614. b->bm_pages = npages;
  615. b->bm_number_of_pages = want;
  616. b->bm_bits = bits;
  617. b->bm_words = words;
  618. b->bm_dev_capacity = capacity;
  619. if (growing) {
  620. if (set_new_bits) {
  621. bm_memset(b, owords, 0xff, words-owords);
  622. b->bm_set += bits - obits;
  623. } else
  624. bm_memset(b, owords, 0x00, words-owords);
  625. }
  626. if (want < have) {
  627. /* implicit: (opages != NULL) && (opages != npages) */
  628. bm_free_pages(opages + want, have - want);
  629. }
  630. (void)bm_clear_surplus(b);
  631. spin_unlock_irq(&b->bm_lock);
  632. if (opages != npages)
  633. bm_vk_free(opages, opages_vmalloced);
  634. if (!growing)
  635. b->bm_set = bm_count_bits(b);
  636. dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
  637. out:
  638. drbd_bm_unlock(mdev);
  639. return err;
  640. }
  641. /* inherently racy:
  642. * if not protected by other means, return value may be out of date when
  643. * leaving this function...
  644. * we still need to lock it, since it is important that this returns
  645. * bm_set == 0 precisely.
  646. *
  647. * maybe bm_set should be atomic_t ?
  648. */
  649. unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
  650. {
  651. struct drbd_bitmap *b = mdev->bitmap;
  652. unsigned long s;
  653. unsigned long flags;
  654. if (!expect(b))
  655. return 0;
  656. if (!expect(b->bm_pages))
  657. return 0;
  658. spin_lock_irqsave(&b->bm_lock, flags);
  659. s = b->bm_set;
  660. spin_unlock_irqrestore(&b->bm_lock, flags);
  661. return s;
  662. }
  663. unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
  664. {
  665. unsigned long s;
  666. /* if I don't have a disk, I don't know about out-of-sync status */
  667. if (!get_ldev_if_state(mdev, D_NEGOTIATING))
  668. return 0;
  669. s = _drbd_bm_total_weight(mdev);
  670. put_ldev(mdev);
  671. return s;
  672. }
  673. size_t drbd_bm_words(struct drbd_conf *mdev)
  674. {
  675. struct drbd_bitmap *b = mdev->bitmap;
  676. if (!expect(b))
  677. return 0;
  678. if (!expect(b->bm_pages))
  679. return 0;
  680. return b->bm_words;
  681. }
  682. unsigned long drbd_bm_bits(struct drbd_conf *mdev)
  683. {
  684. struct drbd_bitmap *b = mdev->bitmap;
  685. if (!expect(b))
  686. return 0;
  687. return b->bm_bits;
  688. }
  689. /* merge number words from buffer into the bitmap starting at offset.
  690. * buffer[i] is expected to be little endian unsigned long.
  691. * bitmap must be locked by drbd_bm_lock.
  692. * currently only used from receive_bitmap.
  693. */
  694. void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
  695. unsigned long *buffer)
  696. {
  697. struct drbd_bitmap *b = mdev->bitmap;
  698. unsigned long *p_addr, *bm;
  699. unsigned long word, bits;
  700. unsigned int idx;
  701. size_t end, do_now;
  702. end = offset + number;
  703. if (!expect(b))
  704. return;
  705. if (!expect(b->bm_pages))
  706. return;
  707. if (number == 0)
  708. return;
  709. WARN_ON(offset >= b->bm_words);
  710. WARN_ON(end > b->bm_words);
  711. spin_lock_irq(&b->bm_lock);
  712. while (offset < end) {
  713. do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
  714. idx = bm_word_to_page_idx(b, offset);
  715. p_addr = bm_map_pidx(b, idx);
  716. bm = p_addr + MLPP(offset);
  717. offset += do_now;
  718. while (do_now--) {
  719. bits = hweight_long(*bm);
  720. word = *bm | *buffer++;
  721. *bm++ = word;
  722. b->bm_set += hweight_long(word) - bits;
  723. }
  724. bm_unmap(p_addr);
  725. bm_set_page_need_writeout(b->bm_pages[idx]);
  726. }
  727. /* with 32bit <-> 64bit cross-platform connect
  728. * this is only correct for current usage,
  729. * where we _know_ that we are 64 bit aligned,
  730. * and know that this function is used in this way, too...
  731. */
  732. if (end == b->bm_words)
  733. b->bm_set -= bm_clear_surplus(b);
  734. spin_unlock_irq(&b->bm_lock);
  735. }
  736. /* copy number words from the bitmap starting at offset into the buffer.
  737. * buffer[i] will be little endian unsigned long.
  738. */
  739. void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
  740. unsigned long *buffer)
  741. {
  742. struct drbd_bitmap *b = mdev->bitmap;
  743. unsigned long *p_addr, *bm;
  744. size_t end, do_now;
  745. end = offset + number;
  746. if (!expect(b))
  747. return;
  748. if (!expect(b->bm_pages))
  749. return;
  750. spin_lock_irq(&b->bm_lock);
  751. if ((offset >= b->bm_words) ||
  752. (end > b->bm_words) ||
  753. (number <= 0))
  754. dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
  755. (unsigned long) offset,
  756. (unsigned long) number,
  757. (unsigned long) b->bm_words);
  758. else {
  759. while (offset < end) {
  760. do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
  761. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
  762. bm = p_addr + MLPP(offset);
  763. offset += do_now;
  764. while (do_now--)
  765. *buffer++ = *bm++;
  766. bm_unmap(p_addr);
  767. }
  768. }
  769. spin_unlock_irq(&b->bm_lock);
  770. }
  771. /* set all bits in the bitmap */
  772. void drbd_bm_set_all(struct drbd_conf *mdev)
  773. {
  774. struct drbd_bitmap *b = mdev->bitmap;
  775. if (!expect(b))
  776. return;
  777. if (!expect(b->bm_pages))
  778. return;
  779. spin_lock_irq(&b->bm_lock);
  780. bm_memset(b, 0, 0xff, b->bm_words);
  781. (void)bm_clear_surplus(b);
  782. b->bm_set = b->bm_bits;
  783. spin_unlock_irq(&b->bm_lock);
  784. }
  785. /* clear all bits in the bitmap */
  786. void drbd_bm_clear_all(struct drbd_conf *mdev)
  787. {
  788. struct drbd_bitmap *b = mdev->bitmap;
  789. if (!expect(b))
  790. return;
  791. if (!expect(b->bm_pages))
  792. return;
  793. spin_lock_irq(&b->bm_lock);
  794. bm_memset(b, 0, 0, b->bm_words);
  795. b->bm_set = 0;
  796. spin_unlock_irq(&b->bm_lock);
  797. }
  798. struct bm_aio_ctx {
  799. struct drbd_conf *mdev;
  800. atomic_t in_flight;
  801. unsigned int done;
  802. unsigned flags;
  803. #define BM_AIO_COPY_PAGES 1
  804. #define BM_AIO_WRITE_HINTED 2
  805. #define BM_WRITE_ALL_PAGES 4
  806. int error;
  807. struct kref kref;
  808. };
  809. static void bm_aio_ctx_destroy(struct kref *kref)
  810. {
  811. struct bm_aio_ctx *ctx = container_of(kref, struct bm_aio_ctx, kref);
  812. put_ldev(ctx->mdev);
  813. kfree(ctx);
  814. }
  815. /* bv_page may be a copy, or may be the original */
  816. static void bm_async_io_complete(struct bio *bio, int error)
  817. {
  818. struct bm_aio_ctx *ctx = bio->bi_private;
  819. struct drbd_conf *mdev = ctx->mdev;
  820. struct drbd_bitmap *b = mdev->bitmap;
  821. unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
  822. int uptodate = bio_flagged(bio, BIO_UPTODATE);
  823. /* strange behavior of some lower level drivers...
  824. * fail the request by clearing the uptodate flag,
  825. * but do not return any error?!
  826. * do we want to WARN() on this? */
  827. if (!error && !uptodate)
  828. error = -EIO;
  829. if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
  830. !bm_test_page_unchanged(b->bm_pages[idx]))
  831. dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
  832. if (error) {
  833. /* ctx error will hold the completed-last non-zero error code,
  834. * in case error codes differ. */
  835. ctx->error = error;
  836. bm_set_page_io_err(b->bm_pages[idx]);
  837. /* Not identical to on disk version of it.
  838. * Is BM_PAGE_IO_ERROR enough? */
  839. if (__ratelimit(&drbd_ratelimit_state))
  840. dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
  841. error, idx);
  842. } else {
  843. bm_clear_page_io_err(b->bm_pages[idx]);
  844. dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
  845. }
  846. bm_page_unlock_io(mdev, idx);
  847. if (ctx->flags & BM_AIO_COPY_PAGES)
  848. mempool_free(bio->bi_io_vec[0].bv_page, drbd_md_io_page_pool);
  849. bio_put(bio);
  850. if (atomic_dec_and_test(&ctx->in_flight)) {
  851. ctx->done = 1;
  852. wake_up(&mdev->misc_wait);
  853. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  854. }
  855. }
  856. static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
  857. {
  858. struct bio *bio = bio_alloc_drbd(GFP_NOIO);
  859. struct drbd_conf *mdev = ctx->mdev;
  860. struct drbd_bitmap *b = mdev->bitmap;
  861. struct page *page;
  862. unsigned int len;
  863. sector_t on_disk_sector =
  864. mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
  865. on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
  866. /* this might happen with very small
  867. * flexible external meta data device,
  868. * or with PAGE_SIZE > 4k */
  869. len = min_t(unsigned int, PAGE_SIZE,
  870. (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
  871. /* serialize IO on this page */
  872. bm_page_lock_io(mdev, page_nr);
  873. /* before memcpy and submit,
  874. * so it can be redirtied any time */
  875. bm_set_page_unchanged(b->bm_pages[page_nr]);
  876. if (ctx->flags & BM_AIO_COPY_PAGES) {
  877. void *src, *dest;
  878. page = mempool_alloc(drbd_md_io_page_pool, __GFP_HIGHMEM|__GFP_WAIT);
  879. dest = kmap_atomic(page, KM_USER0);
  880. src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
  881. memcpy(dest, src, PAGE_SIZE);
  882. kunmap_atomic(src, KM_USER1);
  883. kunmap_atomic(dest, KM_USER0);
  884. bm_store_page_idx(page, page_nr);
  885. } else
  886. page = b->bm_pages[page_nr];
  887. bio->bi_bdev = mdev->ldev->md_bdev;
  888. bio->bi_sector = on_disk_sector;
  889. /* bio_add_page of a single page to an empty bio will always succeed,
  890. * according to api. Do we want to assert that? */
  891. bio_add_page(bio, page, len, 0);
  892. bio->bi_private = ctx;
  893. bio->bi_end_io = bm_async_io_complete;
  894. if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
  895. bio->bi_rw |= rw;
  896. bio_endio(bio, -EIO);
  897. } else {
  898. submit_bio(rw, bio);
  899. /* this should not count as user activity and cause the
  900. * resync to throttle -- see drbd_rs_should_slow_down(). */
  901. atomic_add(len >> 9, &mdev->rs_sect_ev);
  902. }
  903. }
  904. /*
  905. * bm_rw: read/write the whole bitmap from/to its on disk location.
  906. */
  907. static int bm_rw(struct drbd_conf *mdev, int rw, unsigned flags, unsigned lazy_writeout_upper_idx) __must_hold(local)
  908. {
  909. struct bm_aio_ctx *ctx;
  910. struct drbd_bitmap *b = mdev->bitmap;
  911. int num_pages, i, count = 0;
  912. unsigned long now;
  913. char ppb[10];
  914. int err = 0;
  915. /*
  916. * We are protected against bitmap disappearing/resizing by holding an
  917. * ldev reference (caller must have called get_ldev()).
  918. * For read/write, we are protected against changes to the bitmap by
  919. * the bitmap lock (see drbd_bitmap_io).
  920. * For lazy writeout, we don't care for ongoing changes to the bitmap,
  921. * as we submit copies of pages anyways.
  922. */
  923. ctx = kmalloc(sizeof(struct bm_aio_ctx), GFP_NOIO);
  924. if (!ctx)
  925. return -ENOMEM;
  926. *ctx = (struct bm_aio_ctx) {
  927. .mdev = mdev,
  928. .in_flight = ATOMIC_INIT(1),
  929. .done = 0,
  930. .flags = flags,
  931. .error = 0,
  932. .kref = { ATOMIC_INIT(2) },
  933. };
  934. if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* put is in bm_aio_ctx_destroy() */
  935. dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in bm_rw()\n");
  936. kfree(ctx);
  937. return -ENODEV;
  938. }
  939. if (!ctx->flags)
  940. WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
  941. num_pages = b->bm_number_of_pages;
  942. now = jiffies;
  943. /* let the layers below us try to merge these bios... */
  944. for (i = 0; i < num_pages; i++) {
  945. /* ignore completely unchanged pages */
  946. if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
  947. break;
  948. if (rw & WRITE) {
  949. if ((flags & BM_AIO_WRITE_HINTED) &&
  950. !test_and_clear_bit(BM_PAGE_HINT_WRITEOUT,
  951. &page_private(b->bm_pages[i])))
  952. continue;
  953. if (!(flags & BM_WRITE_ALL_PAGES) &&
  954. bm_test_page_unchanged(b->bm_pages[i])) {
  955. dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
  956. continue;
  957. }
  958. /* during lazy writeout,
  959. * ignore those pages not marked for lazy writeout. */
  960. if (lazy_writeout_upper_idx &&
  961. !bm_test_page_lazy_writeout(b->bm_pages[i])) {
  962. dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
  963. continue;
  964. }
  965. }
  966. atomic_inc(&ctx->in_flight);
  967. bm_page_io_async(ctx, i, rw);
  968. ++count;
  969. cond_resched();
  970. }
  971. /*
  972. * We initialize ctx->in_flight to one to make sure bm_async_io_complete
  973. * will not set ctx->done early, and decrement / test it here. If there
  974. * are still some bios in flight, we need to wait for them here.
  975. * If all IO is done already (or nothing had been submitted), there is
  976. * no need to wait. Still, we need to put the kref associated with the
  977. * "in_flight reached zero, all done" event.
  978. */
  979. if (!atomic_dec_and_test(&ctx->in_flight))
  980. wait_until_done_or_force_detached(mdev, mdev->ldev, &ctx->done);
  981. else
  982. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  983. /* summary for global bitmap IO */
  984. if (flags == 0)
  985. dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
  986. rw == WRITE ? "WRITE" : "READ",
  987. count, jiffies - now);
  988. if (ctx->error) {
  989. dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
  990. drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
  991. err = -EIO; /* ctx->error ? */
  992. }
  993. if (atomic_read(&ctx->in_flight))
  994. err = -EIO; /* Disk timeout/force-detach during IO... */
  995. now = jiffies;
  996. if (rw == WRITE) {
  997. drbd_md_flush(mdev);
  998. } else /* rw == READ */ {
  999. b->bm_set = bm_count_bits(b);
  1000. dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
  1001. jiffies - now);
  1002. }
  1003. now = b->bm_set;
  1004. if (flags == 0)
  1005. dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
  1006. ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
  1007. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  1008. return err;
  1009. }
  1010. /**
  1011. * drbd_bm_read() - Read the whole bitmap from its on disk location.
  1012. * @mdev: DRBD device.
  1013. */
  1014. int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
  1015. {
  1016. return bm_rw(mdev, READ, 0, 0);
  1017. }
  1018. /**
  1019. * drbd_bm_write() - Write the whole bitmap to its on disk location.
  1020. * @mdev: DRBD device.
  1021. *
  1022. * Will only write pages that have changed since last IO.
  1023. */
  1024. int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
  1025. {
  1026. return bm_rw(mdev, WRITE, 0, 0);
  1027. }
  1028. /**
  1029. * drbd_bm_write_all() - Write the whole bitmap to its on disk location.
  1030. * @mdev: DRBD device.
  1031. *
  1032. * Will write all pages.
  1033. */
  1034. int drbd_bm_write_all(struct drbd_conf *mdev) __must_hold(local)
  1035. {
  1036. return bm_rw(mdev, WRITE, BM_WRITE_ALL_PAGES, 0);
  1037. }
  1038. /**
  1039. * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
  1040. * @mdev: DRBD device.
  1041. * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
  1042. */
  1043. int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
  1044. {
  1045. return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, upper_idx);
  1046. }
  1047. /**
  1048. * drbd_bm_write_copy_pages() - Write the whole bitmap to its on disk location.
  1049. * @mdev: DRBD device.
  1050. *
  1051. * Will only write pages that have changed since last IO.
  1052. * In contrast to drbd_bm_write(), this will copy the bitmap pages
  1053. * to temporary writeout pages. It is intended to trigger a full write-out
  1054. * while still allowing the bitmap to change, for example if a resync or online
  1055. * verify is aborted due to a failed peer disk, while local IO continues, or
  1056. * pending resync acks are still being processed.
  1057. */
  1058. int drbd_bm_write_copy_pages(struct drbd_conf *mdev) __must_hold(local)
  1059. {
  1060. return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, 0);
  1061. }
  1062. /**
  1063. * drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed.
  1064. * @mdev: DRBD device.
  1065. */
  1066. int drbd_bm_write_hinted(struct drbd_conf *mdev) __must_hold(local)
  1067. {
  1068. return bm_rw(mdev, WRITE, BM_AIO_WRITE_HINTED | BM_AIO_COPY_PAGES, 0);
  1069. }
  1070. /**
  1071. * drbd_bm_write_page() - Writes a PAGE_SIZE aligned piece of bitmap
  1072. * @mdev: DRBD device.
  1073. * @idx: bitmap page index
  1074. *
  1075. * We don't want to special case on logical_block_size of the backend device,
  1076. * so we submit PAGE_SIZE aligned pieces.
  1077. * Note that on "most" systems, PAGE_SIZE is 4k.
  1078. *
  1079. * In case this becomes an issue on systems with larger PAGE_SIZE,
  1080. * we may want to change this again to write 4k aligned 4k pieces.
  1081. */
  1082. int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
  1083. {
  1084. struct bm_aio_ctx *ctx;
  1085. int err;
  1086. if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
  1087. dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
  1088. return 0;
  1089. }
  1090. ctx = kmalloc(sizeof(struct bm_aio_ctx), GFP_NOIO);
  1091. if (!ctx)
  1092. return -ENOMEM;
  1093. *ctx = (struct bm_aio_ctx) {
  1094. .mdev = mdev,
  1095. .in_flight = ATOMIC_INIT(1),
  1096. .done = 0,
  1097. .flags = BM_AIO_COPY_PAGES,
  1098. .error = 0,
  1099. .kref = { ATOMIC_INIT(2) },
  1100. };
  1101. if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* put is in bm_aio_ctx_destroy() */
  1102. dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in drbd_bm_write_page()\n");
  1103. kfree(ctx);
  1104. return -ENODEV;
  1105. }
  1106. bm_page_io_async(ctx, idx, WRITE_SYNC);
  1107. wait_until_done_or_force_detached(mdev, mdev->ldev, &ctx->done);
  1108. if (ctx->error)
  1109. drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
  1110. /* that causes us to detach, so the in memory bitmap will be
  1111. * gone in a moment as well. */
  1112. mdev->bm_writ_cnt++;
  1113. err = atomic_read(&ctx->in_flight) ? -EIO : ctx->error;
  1114. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  1115. return err;
  1116. }
  1117. /* NOTE
  1118. * find_first_bit returns int, we return unsigned long.
  1119. * For this to work on 32bit arch with bitnumbers > (1<<32),
  1120. * we'd need to return u64, and get a whole lot of other places
  1121. * fixed where we still use unsigned long.
  1122. *
  1123. * this returns a bit number, NOT a sector!
  1124. */
  1125. static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
  1126. const int find_zero_bit, const enum km_type km)
  1127. {
  1128. struct drbd_bitmap *b = mdev->bitmap;
  1129. unsigned long *p_addr;
  1130. unsigned long bit_offset;
  1131. unsigned i;
  1132. if (bm_fo > b->bm_bits) {
  1133. dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
  1134. bm_fo = DRBD_END_OF_BITMAP;
  1135. } else {
  1136. while (bm_fo < b->bm_bits) {
  1137. /* bit offset of the first bit in the page */
  1138. bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
  1139. p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
  1140. if (find_zero_bit)
  1141. i = find_next_zero_bit_le(p_addr,
  1142. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1143. else
  1144. i = find_next_bit_le(p_addr,
  1145. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1146. __bm_unmap(p_addr, km);
  1147. if (i < PAGE_SIZE*8) {
  1148. bm_fo = bit_offset + i;
  1149. if (bm_fo >= b->bm_bits)
  1150. break;
  1151. goto found;
  1152. }
  1153. bm_fo = bit_offset + PAGE_SIZE*8;
  1154. }
  1155. bm_fo = DRBD_END_OF_BITMAP;
  1156. }
  1157. found:
  1158. return bm_fo;
  1159. }
  1160. static unsigned long bm_find_next(struct drbd_conf *mdev,
  1161. unsigned long bm_fo, const int find_zero_bit)
  1162. {
  1163. struct drbd_bitmap *b = mdev->bitmap;
  1164. unsigned long i = DRBD_END_OF_BITMAP;
  1165. if (!expect(b))
  1166. return i;
  1167. if (!expect(b->bm_pages))
  1168. return i;
  1169. spin_lock_irq(&b->bm_lock);
  1170. if (BM_DONT_TEST & b->bm_flags)
  1171. bm_print_lock_info(mdev);
  1172. i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
  1173. spin_unlock_irq(&b->bm_lock);
  1174. return i;
  1175. }
  1176. unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1177. {
  1178. return bm_find_next(mdev, bm_fo, 0);
  1179. }
  1180. #if 0
  1181. /* not yet needed for anything. */
  1182. unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1183. {
  1184. return bm_find_next(mdev, bm_fo, 1);
  1185. }
  1186. #endif
  1187. /* does not spin_lock_irqsave.
  1188. * you must take drbd_bm_lock() first */
  1189. unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1190. {
  1191. /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
  1192. return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
  1193. }
  1194. unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1195. {
  1196. /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
  1197. return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
  1198. }
  1199. /* returns number of bits actually changed.
  1200. * for val != 0, we change 0 -> 1, return code positive
  1201. * for val == 0, we change 1 -> 0, return code negative
  1202. * wants bitnr, not sector.
  1203. * expected to be called for only a few bits (e - s about BITS_PER_LONG).
  1204. * Must hold bitmap lock already. */
  1205. static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1206. unsigned long e, int val)
  1207. {
  1208. struct drbd_bitmap *b = mdev->bitmap;
  1209. unsigned long *p_addr = NULL;
  1210. unsigned long bitnr;
  1211. unsigned int last_page_nr = -1U;
  1212. int c = 0;
  1213. int changed_total = 0;
  1214. if (e >= b->bm_bits) {
  1215. dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
  1216. s, e, b->bm_bits);
  1217. e = b->bm_bits ? b->bm_bits -1 : 0;
  1218. }
  1219. for (bitnr = s; bitnr <= e; bitnr++) {
  1220. unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
  1221. if (page_nr != last_page_nr) {
  1222. if (p_addr)
  1223. __bm_unmap(p_addr, KM_IRQ1);
  1224. if (c < 0)
  1225. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1226. else if (c > 0)
  1227. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1228. changed_total += c;
  1229. c = 0;
  1230. p_addr = __bm_map_pidx(b, page_nr, KM_IRQ1);
  1231. last_page_nr = page_nr;
  1232. }
  1233. if (val)
  1234. c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1235. else
  1236. c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1237. }
  1238. if (p_addr)
  1239. __bm_unmap(p_addr, KM_IRQ1);
  1240. if (c < 0)
  1241. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1242. else if (c > 0)
  1243. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1244. changed_total += c;
  1245. b->bm_set += changed_total;
  1246. return changed_total;
  1247. }
  1248. /* returns number of bits actually changed.
  1249. * for val != 0, we change 0 -> 1, return code positive
  1250. * for val == 0, we change 1 -> 0, return code negative
  1251. * wants bitnr, not sector */
  1252. static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1253. const unsigned long e, int val)
  1254. {
  1255. unsigned long flags;
  1256. struct drbd_bitmap *b = mdev->bitmap;
  1257. int c = 0;
  1258. if (!expect(b))
  1259. return 1;
  1260. if (!expect(b->bm_pages))
  1261. return 0;
  1262. spin_lock_irqsave(&b->bm_lock, flags);
  1263. if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
  1264. bm_print_lock_info(mdev);
  1265. c = __bm_change_bits_to(mdev, s, e, val);
  1266. spin_unlock_irqrestore(&b->bm_lock, flags);
  1267. return c;
  1268. }
  1269. /* returns number of bits changed 0 -> 1 */
  1270. int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1271. {
  1272. return bm_change_bits_to(mdev, s, e, 1);
  1273. }
  1274. /* returns number of bits changed 1 -> 0 */
  1275. int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1276. {
  1277. return -bm_change_bits_to(mdev, s, e, 0);
  1278. }
  1279. /* sets all bits in full words,
  1280. * from first_word up to, but not including, last_word */
  1281. static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
  1282. int page_nr, int first_word, int last_word)
  1283. {
  1284. int i;
  1285. int bits;
  1286. int changed = 0;
  1287. unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_IRQ1);
  1288. for (i = first_word; i < last_word; i++) {
  1289. bits = hweight_long(paddr[i]);
  1290. paddr[i] = ~0UL;
  1291. changed += BITS_PER_LONG - bits;
  1292. }
  1293. kunmap_atomic(paddr, KM_IRQ1);
  1294. if (changed) {
  1295. /* We only need lazy writeout, the information is still in the
  1296. * remote bitmap as well, and is reconstructed during the next
  1297. * bitmap exchange, if lost locally due to a crash. */
  1298. bm_set_page_lazy_writeout(b->bm_pages[page_nr]);
  1299. b->bm_set += changed;
  1300. }
  1301. }
  1302. /* Same thing as drbd_bm_set_bits,
  1303. * but more efficient for a large bit range.
  1304. * You must first drbd_bm_lock().
  1305. * Can be called to set the whole bitmap in one go.
  1306. * Sets bits from s to e _inclusive_. */
  1307. void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1308. {
  1309. /* First set_bit from the first bit (s)
  1310. * up to the next long boundary (sl),
  1311. * then assign full words up to the last long boundary (el),
  1312. * then set_bit up to and including the last bit (e).
  1313. *
  1314. * Do not use memset, because we must account for changes,
  1315. * so we need to loop over the words with hweight() anyways.
  1316. */
  1317. struct drbd_bitmap *b = mdev->bitmap;
  1318. unsigned long sl = ALIGN(s,BITS_PER_LONG);
  1319. unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
  1320. int first_page;
  1321. int last_page;
  1322. int page_nr;
  1323. int first_word;
  1324. int last_word;
  1325. if (e - s <= 3*BITS_PER_LONG) {
  1326. /* don't bother; el and sl may even be wrong. */
  1327. spin_lock_irq(&b->bm_lock);
  1328. __bm_change_bits_to(mdev, s, e, 1);
  1329. spin_unlock_irq(&b->bm_lock);
  1330. return;
  1331. }
  1332. /* difference is large enough that we can trust sl and el */
  1333. spin_lock_irq(&b->bm_lock);
  1334. /* bits filling the current long */
  1335. if (sl)
  1336. __bm_change_bits_to(mdev, s, sl-1, 1);
  1337. first_page = sl >> (3 + PAGE_SHIFT);
  1338. last_page = el >> (3 + PAGE_SHIFT);
  1339. /* MLPP: modulo longs per page */
  1340. /* LWPP: long words per page */
  1341. first_word = MLPP(sl >> LN2_BPL);
  1342. last_word = LWPP;
  1343. /* first and full pages, unless first page == last page */
  1344. for (page_nr = first_page; page_nr < last_page; page_nr++) {
  1345. bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
  1346. spin_unlock_irq(&b->bm_lock);
  1347. cond_resched();
  1348. first_word = 0;
  1349. spin_lock_irq(&b->bm_lock);
  1350. }
  1351. /* last page (respectively only page, for first page == last page) */
  1352. last_word = MLPP(el >> LN2_BPL);
  1353. /* consider bitmap->bm_bits = 32768, bitmap->bm_number_of_pages = 1. (or multiples).
  1354. * ==> e = 32767, el = 32768, last_page = 2,
  1355. * and now last_word = 0.
  1356. * We do not want to touch last_page in this case,
  1357. * as we did not allocate it, it is not present in bitmap->bm_pages.
  1358. */
  1359. if (last_word)
  1360. bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
  1361. /* possibly trailing bits.
  1362. * example: (e & 63) == 63, el will be e+1.
  1363. * if that even was the very last bit,
  1364. * it would trigger an assert in __bm_change_bits_to()
  1365. */
  1366. if (el <= e)
  1367. __bm_change_bits_to(mdev, el, e, 1);
  1368. spin_unlock_irq(&b->bm_lock);
  1369. }
  1370. /* returns bit state
  1371. * wants bitnr, NOT sector.
  1372. * inherently racy... area needs to be locked by means of {al,rs}_lru
  1373. * 1 ... bit set
  1374. * 0 ... bit not set
  1375. * -1 ... first out of bounds access, stop testing for bits!
  1376. */
  1377. int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
  1378. {
  1379. unsigned long flags;
  1380. struct drbd_bitmap *b = mdev->bitmap;
  1381. unsigned long *p_addr;
  1382. int i;
  1383. if (!expect(b))
  1384. return 0;
  1385. if (!expect(b->bm_pages))
  1386. return 0;
  1387. spin_lock_irqsave(&b->bm_lock, flags);
  1388. if (BM_DONT_TEST & b->bm_flags)
  1389. bm_print_lock_info(mdev);
  1390. if (bitnr < b->bm_bits) {
  1391. p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
  1392. i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
  1393. bm_unmap(p_addr);
  1394. } else if (bitnr == b->bm_bits) {
  1395. i = -1;
  1396. } else { /* (bitnr > b->bm_bits) */
  1397. dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
  1398. i = 0;
  1399. }
  1400. spin_unlock_irqrestore(&b->bm_lock, flags);
  1401. return i;
  1402. }
  1403. /* returns number of bits set in the range [s, e] */
  1404. int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1405. {
  1406. unsigned long flags;
  1407. struct drbd_bitmap *b = mdev->bitmap;
  1408. unsigned long *p_addr = NULL;
  1409. unsigned long bitnr;
  1410. unsigned int page_nr = -1U;
  1411. int c = 0;
  1412. /* If this is called without a bitmap, that is a bug. But just to be
  1413. * robust in case we screwed up elsewhere, in that case pretend there
  1414. * was one dirty bit in the requested area, so we won't try to do a
  1415. * local read there (no bitmap probably implies no disk) */
  1416. if (!expect(b))
  1417. return 1;
  1418. if (!expect(b->bm_pages))
  1419. return 1;
  1420. spin_lock_irqsave(&b->bm_lock, flags);
  1421. if (BM_DONT_TEST & b->bm_flags)
  1422. bm_print_lock_info(mdev);
  1423. for (bitnr = s; bitnr <= e; bitnr++) {
  1424. unsigned int idx = bm_bit_to_page_idx(b, bitnr);
  1425. if (page_nr != idx) {
  1426. page_nr = idx;
  1427. if (p_addr)
  1428. bm_unmap(p_addr);
  1429. p_addr = bm_map_pidx(b, idx);
  1430. }
  1431. if (expect(bitnr < b->bm_bits))
  1432. c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
  1433. else
  1434. dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
  1435. }
  1436. if (p_addr)
  1437. bm_unmap(p_addr);
  1438. spin_unlock_irqrestore(&b->bm_lock, flags);
  1439. return c;
  1440. }
  1441. /* inherently racy...
  1442. * return value may be already out-of-date when this function returns.
  1443. * but the general usage is that this is only use during a cstate when bits are
  1444. * only cleared, not set, and typically only care for the case when the return
  1445. * value is zero, or we already "locked" this "bitmap extent" by other means.
  1446. *
  1447. * enr is bm-extent number, since we chose to name one sector (512 bytes)
  1448. * worth of the bitmap a "bitmap extent".
  1449. *
  1450. * TODO
  1451. * I think since we use it like a reference count, we should use the real
  1452. * reference count of some bitmap extent element from some lru instead...
  1453. *
  1454. */
  1455. int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
  1456. {
  1457. struct drbd_bitmap *b = mdev->bitmap;
  1458. int count, s, e;
  1459. unsigned long flags;
  1460. unsigned long *p_addr, *bm;
  1461. if (!expect(b))
  1462. return 0;
  1463. if (!expect(b->bm_pages))
  1464. return 0;
  1465. spin_lock_irqsave(&b->bm_lock, flags);
  1466. if (BM_DONT_TEST & b->bm_flags)
  1467. bm_print_lock_info(mdev);
  1468. s = S2W(enr);
  1469. e = min((size_t)S2W(enr+1), b->bm_words);
  1470. count = 0;
  1471. if (s < b->bm_words) {
  1472. int n = e-s;
  1473. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
  1474. bm = p_addr + MLPP(s);
  1475. while (n--)
  1476. count += hweight_long(*bm++);
  1477. bm_unmap(p_addr);
  1478. } else {
  1479. dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
  1480. }
  1481. spin_unlock_irqrestore(&b->bm_lock, flags);
  1482. return count;
  1483. }