drbd_bitmap.c 44 KB

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