drbd_bitmap.c 47 KB

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