drbd_bitmap.c 47 KB

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