drbd_bitmap.c 47 KB

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