drbd_bitmap.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. /*
  2. drbd_bitmap.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2004-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/string.h>
  22. #include <linux/drbd.h>
  23. #include <linux/slab.h>
  24. #include <asm/kmap_types.h>
  25. #include "drbd_int.h"
  26. /* OPAQUE outside this file!
  27. * interface defined in drbd_int.h
  28. * convention:
  29. * function name drbd_bm_... => used elsewhere, "public".
  30. * function name bm_... => internal to implementation, "private".
  31. */
  32. /*
  33. * LIMITATIONS:
  34. * We want to support >= peta byte of backend storage, while for now still using
  35. * a granularity of one bit per 4KiB of storage.
  36. * 1 << 50 bytes backend storage (1 PiB)
  37. * 1 << (50 - 12) bits needed
  38. * 38 --> we need u64 to index and count bits
  39. * 1 << (38 - 3) bitmap bytes needed
  40. * 35 --> we still need u64 to index and count bytes
  41. * (that's 32 GiB of bitmap for 1 PiB storage)
  42. * 1 << (35 - 2) 32bit longs needed
  43. * 33 --> we'd even need u64 to index and count 32bit long words.
  44. * 1 << (35 - 3) 64bit longs needed
  45. * 32 --> we could get away with a 32bit unsigned int to index and count
  46. * 64bit long words, but I rather stay with unsigned long for now.
  47. * We probably should neither count nor point to bytes or long words
  48. * directly, but either by bitnumber, or by page index and offset.
  49. * 1 << (35 - 12)
  50. * 22 --> we need that much 4KiB pages of bitmap.
  51. * 1 << (22 + 3) --> on a 64bit arch,
  52. * we need 32 MiB to store the array of page pointers.
  53. *
  54. * Because I'm lazy, and because the resulting patch was too large, too ugly
  55. * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
  56. * (1 << 32) bits * 4k storage.
  57. *
  58. * bitmap storage and IO:
  59. * Bitmap is stored little endian on disk, and is kept little endian in
  60. * core memory. Currently we still hold the full bitmap in core as long
  61. * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
  62. * seems excessive.
  63. *
  64. * We plan to reduce the amount of in-core bitmap pages by paging them in
  65. * and out against their on-disk location as necessary, but need to make
  66. * sure we don't cause too much meta data IO, and must not deadlock in
  67. * tight memory situations. This needs some more work.
  68. */
  69. /*
  70. * NOTE
  71. * Access to the *bm_pages is protected by bm_lock.
  72. * It is safe to read the other members within the lock.
  73. *
  74. * drbd_bm_set_bits is called from bio_endio callbacks,
  75. * We may be called with irq already disabled,
  76. * so we need spin_lock_irqsave().
  77. * And we need the kmap_atomic.
  78. */
  79. struct drbd_bitmap {
  80. struct page **bm_pages;
  81. spinlock_t bm_lock;
  82. /* see LIMITATIONS: above */
  83. unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
  84. unsigned long bm_bits;
  85. size_t bm_words;
  86. size_t bm_number_of_pages;
  87. sector_t bm_dev_capacity;
  88. struct mutex bm_change; /* serializes resize operations */
  89. wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
  90. enum bm_flag bm_flags;
  91. /* debugging aid, in case we are still racy somewhere */
  92. char *bm_why;
  93. struct task_struct *bm_task;
  94. };
  95. #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
  96. static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
  97. {
  98. struct drbd_bitmap *b = mdev->bitmap;
  99. if (!__ratelimit(&drbd_ratelimit_state))
  100. return;
  101. dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
  102. drbd_task_to_thread_name(mdev->tconn, current),
  103. func, b->bm_why ?: "?",
  104. drbd_task_to_thread_name(mdev->tconn, b->bm_task));
  105. }
  106. void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
  107. {
  108. struct drbd_bitmap *b = mdev->bitmap;
  109. int trylock_failed;
  110. if (!b) {
  111. dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
  112. return;
  113. }
  114. trylock_failed = !mutex_trylock(&b->bm_change);
  115. if (trylock_failed) {
  116. dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
  117. drbd_task_to_thread_name(mdev->tconn, current),
  118. why, b->bm_why ?: "?",
  119. drbd_task_to_thread_name(mdev->tconn, b->bm_task));
  120. mutex_lock(&b->bm_change);
  121. }
  122. if (BM_LOCKED_MASK & b->bm_flags)
  123. dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
  124. b->bm_flags |= flags & BM_LOCKED_MASK;
  125. b->bm_why = why;
  126. b->bm_task = current;
  127. }
  128. void drbd_bm_unlock(struct drbd_conf *mdev)
  129. {
  130. struct drbd_bitmap *b = mdev->bitmap;
  131. if (!b) {
  132. dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
  133. return;
  134. }
  135. if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
  136. dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
  137. b->bm_flags &= ~BM_LOCKED_MASK;
  138. b->bm_why = NULL;
  139. b->bm_task = NULL;
  140. mutex_unlock(&b->bm_change);
  141. }
  142. /* we store some "meta" info about our pages in page->private */
  143. /* at a granularity of 4k storage per bitmap bit:
  144. * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
  145. * 1<<38 bits,
  146. * 1<<23 4k bitmap pages.
  147. * Use 24 bits as page index, covers 2 peta byte storage
  148. * at a granularity of 4k per bit.
  149. * Used to report the failed page idx on io error from the endio handlers.
  150. */
  151. #define BM_PAGE_IDX_MASK ((1UL<<24)-1)
  152. /* this page is currently read in, or written back */
  153. #define BM_PAGE_IO_LOCK 31
  154. /* if there has been an IO error for this page */
  155. #define BM_PAGE_IO_ERROR 30
  156. /* this is to be able to intelligently skip disk IO,
  157. * set if bits have been set since last IO. */
  158. #define BM_PAGE_NEED_WRITEOUT 29
  159. /* to mark for lazy writeout once syncer cleared all clearable bits,
  160. * we if bits have been cleared since last IO. */
  161. #define BM_PAGE_LAZY_WRITEOUT 28
  162. /* pages marked with this "HINT" will be considered for writeout
  163. * on activity log transactions */
  164. #define BM_PAGE_HINT_WRITEOUT 27
  165. /* store_page_idx uses non-atomic assignment. It is only used directly after
  166. * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
  167. * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
  168. * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
  169. * requires it all to be atomic as well. */
  170. static void bm_store_page_idx(struct page *page, unsigned long idx)
  171. {
  172. BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
  173. set_page_private(page, idx);
  174. }
  175. static unsigned long bm_page_to_idx(struct page *page)
  176. {
  177. return page_private(page) & BM_PAGE_IDX_MASK;
  178. }
  179. /* As is very unlikely that the same page is under IO from more than one
  180. * context, we can get away with a bit per page and one wait queue per bitmap.
  181. */
  182. static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
  183. {
  184. struct drbd_bitmap *b = mdev->bitmap;
  185. void *addr = &page_private(b->bm_pages[page_nr]);
  186. wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
  187. }
  188. static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
  189. {
  190. struct drbd_bitmap *b = mdev->bitmap;
  191. void *addr = &page_private(b->bm_pages[page_nr]);
  192. clear_bit_unlock(BM_PAGE_IO_LOCK, addr);
  193. wake_up(&mdev->bitmap->bm_io_wait);
  194. }
  195. /* set _before_ submit_io, so it may be reset due to being changed
  196. * while this page is in flight... will get submitted later again */
  197. static void bm_set_page_unchanged(struct page *page)
  198. {
  199. /* use cmpxchg? */
  200. clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
  201. clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
  202. }
  203. static void bm_set_page_need_writeout(struct page *page)
  204. {
  205. set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
  206. }
  207. /**
  208. * drbd_bm_mark_for_writeout() - mark a page with a "hint" to be considered for writeout
  209. * @mdev: DRBD device.
  210. * @page_nr: the bitmap page to mark with the "hint" flag
  211. *
  212. * From within an activity log transaction, we mark a few pages with these
  213. * hints, then call drbd_bm_write_hinted(), which will only write out changed
  214. * pages which are flagged with this mark.
  215. */
  216. void drbd_bm_mark_for_writeout(struct drbd_conf *mdev, int page_nr)
  217. {
  218. struct page *page;
  219. if (page_nr >= mdev->bitmap->bm_number_of_pages) {
  220. dev_warn(DEV, "BAD: page_nr: %u, number_of_pages: %u\n",
  221. page_nr, (int)mdev->bitmap->bm_number_of_pages);
  222. return;
  223. }
  224. page = mdev->bitmap->bm_pages[page_nr];
  225. set_bit(BM_PAGE_HINT_WRITEOUT, &page_private(page));
  226. }
  227. static int bm_test_page_unchanged(struct page *page)
  228. {
  229. volatile const unsigned long *addr = &page_private(page);
  230. return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
  231. }
  232. static void bm_set_page_io_err(struct page *page)
  233. {
  234. set_bit(BM_PAGE_IO_ERROR, &page_private(page));
  235. }
  236. static void bm_clear_page_io_err(struct page *page)
  237. {
  238. clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
  239. }
  240. static void bm_set_page_lazy_writeout(struct page *page)
  241. {
  242. set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
  243. }
  244. static int bm_test_page_lazy_writeout(struct page *page)
  245. {
  246. return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
  247. }
  248. /* on a 32bit box, this would allow for exactly (2<<38) bits. */
  249. static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
  250. {
  251. /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
  252. unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
  253. BUG_ON(page_nr >= b->bm_number_of_pages);
  254. return page_nr;
  255. }
  256. static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
  257. {
  258. /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
  259. unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
  260. BUG_ON(page_nr >= b->bm_number_of_pages);
  261. return page_nr;
  262. }
  263. static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
  264. {
  265. struct page *page = b->bm_pages[idx];
  266. return (unsigned long *) kmap_atomic(page, km);
  267. }
  268. static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
  269. {
  270. return __bm_map_pidx(b, idx, KM_IRQ1);
  271. }
  272. static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
  273. {
  274. kunmap_atomic(p_addr, km);
  275. };
  276. static void bm_unmap(unsigned long *p_addr)
  277. {
  278. return __bm_unmap(p_addr, KM_IRQ1);
  279. }
  280. /* long word offset of _bitmap_ sector */
  281. #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
  282. /* word offset from start of bitmap to word number _in_page_
  283. * modulo longs per page
  284. #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
  285. hm, well, Philipp thinks gcc might not optimize the % into & (... - 1)
  286. so do it explicitly:
  287. */
  288. #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
  289. /* Long words per page */
  290. #define LWPP (PAGE_SIZE/sizeof(long))
  291. /*
  292. * actually most functions herein should take a struct drbd_bitmap*, not a
  293. * struct drbd_conf*, but for the debug macros I like to have the mdev around
  294. * to be able to report device specific.
  295. */
  296. static void bm_free_pages(struct page **pages, unsigned long number)
  297. {
  298. unsigned long i;
  299. if (!pages)
  300. return;
  301. for (i = 0; i < number; i++) {
  302. if (!pages[i]) {
  303. printk(KERN_ALERT "drbd: bm_free_pages tried to free "
  304. "a NULL pointer; i=%lu n=%lu\n",
  305. i, number);
  306. continue;
  307. }
  308. __free_page(pages[i]);
  309. pages[i] = NULL;
  310. }
  311. }
  312. static void bm_vk_free(void *ptr, int v)
  313. {
  314. if (v)
  315. vfree(ptr);
  316. else
  317. kfree(ptr);
  318. }
  319. /*
  320. * "have" and "want" are NUMBER OF PAGES.
  321. */
  322. static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
  323. {
  324. struct page **old_pages = b->bm_pages;
  325. struct page **new_pages, *page;
  326. unsigned int i, bytes, vmalloced = 0;
  327. unsigned long have = b->bm_number_of_pages;
  328. BUG_ON(have == 0 && old_pages != NULL);
  329. BUG_ON(have != 0 && old_pages == NULL);
  330. if (have == want)
  331. return old_pages;
  332. /* Trying kmalloc first, falling back to vmalloc.
  333. * GFP_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. kfree(ctx);
  934. return -ENODEV;
  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, mdev->ldev, &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. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  1004. return err;
  1005. }
  1006. /**
  1007. * drbd_bm_read() - Read the whole bitmap from its on disk location.
  1008. * @mdev: DRBD device.
  1009. */
  1010. int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
  1011. {
  1012. return bm_rw(mdev, READ, 0, 0);
  1013. }
  1014. /**
  1015. * drbd_bm_write() - Write the whole bitmap to its on disk location.
  1016. * @mdev: DRBD device.
  1017. *
  1018. * Will only write pages that have changed since last IO.
  1019. */
  1020. int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
  1021. {
  1022. return bm_rw(mdev, WRITE, 0, 0);
  1023. }
  1024. /**
  1025. * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
  1026. * @mdev: DRBD device.
  1027. * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
  1028. */
  1029. int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
  1030. {
  1031. return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, upper_idx);
  1032. }
  1033. /**
  1034. * drbd_bm_write_copy_pages() - Write the whole bitmap to its on disk location.
  1035. * @mdev: DRBD device.
  1036. *
  1037. * Will only write pages that have changed since last IO.
  1038. * In contrast to drbd_bm_write(), this will copy the bitmap pages
  1039. * to temporary writeout pages. It is intended to trigger a full write-out
  1040. * while still allowing the bitmap to change, for example if a resync or online
  1041. * verify is aborted due to a failed peer disk, while local IO continues, or
  1042. * pending resync acks are still being processed.
  1043. */
  1044. int drbd_bm_write_copy_pages(struct drbd_conf *mdev) __must_hold(local)
  1045. {
  1046. return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, 0);
  1047. }
  1048. /**
  1049. * drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed.
  1050. * @mdev: DRBD device.
  1051. */
  1052. int drbd_bm_write_hinted(struct drbd_conf *mdev) __must_hold(local)
  1053. {
  1054. return bm_rw(mdev, WRITE, BM_AIO_WRITE_HINTED | BM_AIO_COPY_PAGES, 0);
  1055. }
  1056. /**
  1057. * drbd_bm_write_page() - Writes a PAGE_SIZE aligned piece of bitmap
  1058. * @mdev: DRBD device.
  1059. * @idx: bitmap page index
  1060. *
  1061. * We don't want to special case on logical_block_size of the backend device,
  1062. * so we submit PAGE_SIZE aligned pieces.
  1063. * Note that on "most" systems, PAGE_SIZE is 4k.
  1064. *
  1065. * In case this becomes an issue on systems with larger PAGE_SIZE,
  1066. * we may want to change this again to write 4k aligned 4k pieces.
  1067. */
  1068. int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
  1069. {
  1070. struct bm_aio_ctx *ctx;
  1071. int err;
  1072. if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
  1073. dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
  1074. return 0;
  1075. }
  1076. ctx = kmalloc(sizeof(struct bm_aio_ctx), GFP_NOIO);
  1077. if (!ctx)
  1078. return -ENOMEM;
  1079. *ctx = (struct bm_aio_ctx) {
  1080. .mdev = mdev,
  1081. .in_flight = ATOMIC_INIT(1),
  1082. .done = 0,
  1083. .flags = BM_AIO_COPY_PAGES,
  1084. .error = 0,
  1085. .kref = { ATOMIC_INIT(2) },
  1086. };
  1087. if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* put is in bm_aio_ctx_destroy() */
  1088. dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in drbd_bm_write_page()\n");
  1089. kfree(ctx);
  1090. return -ENODEV;
  1091. }
  1092. bm_page_io_async(ctx, idx, WRITE_SYNC);
  1093. wait_until_done_or_disk_failure(mdev, mdev->ldev, &ctx->done);
  1094. if (ctx->error)
  1095. drbd_chk_io_error(mdev, 1, true);
  1096. /* that should force detach, so the in memory bitmap will be
  1097. * gone in a moment as well. */
  1098. mdev->bm_writ_cnt++;
  1099. err = atomic_read(&ctx->in_flight) ? -EIO : ctx->error;
  1100. kref_put(&ctx->kref, &bm_aio_ctx_destroy);
  1101. return err;
  1102. }
  1103. /* NOTE
  1104. * find_first_bit returns int, we return unsigned long.
  1105. * For this to work on 32bit arch with bitnumbers > (1<<32),
  1106. * we'd need to return u64, and get a whole lot of other places
  1107. * fixed where we still use unsigned long.
  1108. *
  1109. * this returns a bit number, NOT a sector!
  1110. */
  1111. static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
  1112. const int find_zero_bit, const enum km_type km)
  1113. {
  1114. struct drbd_bitmap *b = mdev->bitmap;
  1115. unsigned long *p_addr;
  1116. unsigned long bit_offset;
  1117. unsigned i;
  1118. if (bm_fo > b->bm_bits) {
  1119. dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
  1120. bm_fo = DRBD_END_OF_BITMAP;
  1121. } else {
  1122. while (bm_fo < b->bm_bits) {
  1123. /* bit offset of the first bit in the page */
  1124. bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
  1125. p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
  1126. if (find_zero_bit)
  1127. i = find_next_zero_bit_le(p_addr,
  1128. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1129. else
  1130. i = find_next_bit_le(p_addr,
  1131. PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
  1132. __bm_unmap(p_addr, km);
  1133. if (i < PAGE_SIZE*8) {
  1134. bm_fo = bit_offset + i;
  1135. if (bm_fo >= b->bm_bits)
  1136. break;
  1137. goto found;
  1138. }
  1139. bm_fo = bit_offset + PAGE_SIZE*8;
  1140. }
  1141. bm_fo = DRBD_END_OF_BITMAP;
  1142. }
  1143. found:
  1144. return bm_fo;
  1145. }
  1146. static unsigned long bm_find_next(struct drbd_conf *mdev,
  1147. unsigned long bm_fo, const int find_zero_bit)
  1148. {
  1149. struct drbd_bitmap *b = mdev->bitmap;
  1150. unsigned long i = DRBD_END_OF_BITMAP;
  1151. if (!expect(b))
  1152. return i;
  1153. if (!expect(b->bm_pages))
  1154. return i;
  1155. spin_lock_irq(&b->bm_lock);
  1156. if (BM_DONT_TEST & b->bm_flags)
  1157. bm_print_lock_info(mdev);
  1158. i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
  1159. spin_unlock_irq(&b->bm_lock);
  1160. return i;
  1161. }
  1162. unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1163. {
  1164. return bm_find_next(mdev, bm_fo, 0);
  1165. }
  1166. #if 0
  1167. /* not yet needed for anything. */
  1168. unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1169. {
  1170. return bm_find_next(mdev, bm_fo, 1);
  1171. }
  1172. #endif
  1173. /* does not spin_lock_irqsave.
  1174. * you must take drbd_bm_lock() first */
  1175. unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
  1176. {
  1177. /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
  1178. return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
  1179. }
  1180. unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
  1181. {
  1182. /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
  1183. return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
  1184. }
  1185. /* returns number of bits actually changed.
  1186. * for val != 0, we change 0 -> 1, return code positive
  1187. * for val == 0, we change 1 -> 0, return code negative
  1188. * wants bitnr, not sector.
  1189. * expected to be called for only a few bits (e - s about BITS_PER_LONG).
  1190. * Must hold bitmap lock already. */
  1191. static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1192. unsigned long e, int val)
  1193. {
  1194. struct drbd_bitmap *b = mdev->bitmap;
  1195. unsigned long *p_addr = NULL;
  1196. unsigned long bitnr;
  1197. unsigned int last_page_nr = -1U;
  1198. int c = 0;
  1199. int changed_total = 0;
  1200. if (e >= b->bm_bits) {
  1201. dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
  1202. s, e, b->bm_bits);
  1203. e = b->bm_bits ? b->bm_bits -1 : 0;
  1204. }
  1205. for (bitnr = s; bitnr <= e; bitnr++) {
  1206. unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
  1207. if (page_nr != last_page_nr) {
  1208. if (p_addr)
  1209. __bm_unmap(p_addr, KM_IRQ1);
  1210. if (c < 0)
  1211. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1212. else if (c > 0)
  1213. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1214. changed_total += c;
  1215. c = 0;
  1216. p_addr = __bm_map_pidx(b, page_nr, KM_IRQ1);
  1217. last_page_nr = page_nr;
  1218. }
  1219. if (val)
  1220. c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1221. else
  1222. c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
  1223. }
  1224. if (p_addr)
  1225. __bm_unmap(p_addr, KM_IRQ1);
  1226. if (c < 0)
  1227. bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
  1228. else if (c > 0)
  1229. bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
  1230. changed_total += c;
  1231. b->bm_set += changed_total;
  1232. return changed_total;
  1233. }
  1234. /* returns number of bits actually changed.
  1235. * for val != 0, we change 0 -> 1, return code positive
  1236. * for val == 0, we change 1 -> 0, return code negative
  1237. * wants bitnr, not sector */
  1238. static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
  1239. const unsigned long e, int val)
  1240. {
  1241. unsigned long flags;
  1242. struct drbd_bitmap *b = mdev->bitmap;
  1243. int c = 0;
  1244. if (!expect(b))
  1245. return 1;
  1246. if (!expect(b->bm_pages))
  1247. return 0;
  1248. spin_lock_irqsave(&b->bm_lock, flags);
  1249. if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
  1250. bm_print_lock_info(mdev);
  1251. c = __bm_change_bits_to(mdev, s, e, val);
  1252. spin_unlock_irqrestore(&b->bm_lock, flags);
  1253. return c;
  1254. }
  1255. /* returns number of bits changed 0 -> 1 */
  1256. int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1257. {
  1258. return bm_change_bits_to(mdev, s, e, 1);
  1259. }
  1260. /* returns number of bits changed 1 -> 0 */
  1261. int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1262. {
  1263. return -bm_change_bits_to(mdev, s, e, 0);
  1264. }
  1265. /* sets all bits in full words,
  1266. * from first_word up to, but not including, last_word */
  1267. static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
  1268. int page_nr, int first_word, int last_word)
  1269. {
  1270. int i;
  1271. int bits;
  1272. int changed = 0;
  1273. unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_IRQ1);
  1274. for (i = first_word; i < last_word; i++) {
  1275. bits = hweight_long(paddr[i]);
  1276. paddr[i] = ~0UL;
  1277. changed += BITS_PER_LONG - bits;
  1278. }
  1279. kunmap_atomic(paddr, KM_IRQ1);
  1280. if (changed) {
  1281. /* We only need lazy writeout, the information is still in the
  1282. * remote bitmap as well, and is reconstructed during the next
  1283. * bitmap exchange, if lost locally due to a crash. */
  1284. bm_set_page_lazy_writeout(b->bm_pages[page_nr]);
  1285. b->bm_set += changed;
  1286. }
  1287. }
  1288. /* Same thing as drbd_bm_set_bits,
  1289. * but more efficient for a large bit range.
  1290. * You must first drbd_bm_lock().
  1291. * Can be called to set the whole bitmap in one go.
  1292. * Sets bits from s to e _inclusive_. */
  1293. void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1294. {
  1295. /* First set_bit from the first bit (s)
  1296. * up to the next long boundary (sl),
  1297. * then assign full words up to the last long boundary (el),
  1298. * then set_bit up to and including the last bit (e).
  1299. *
  1300. * Do not use memset, because we must account for changes,
  1301. * so we need to loop over the words with hweight() anyways.
  1302. */
  1303. struct drbd_bitmap *b = mdev->bitmap;
  1304. unsigned long sl = ALIGN(s,BITS_PER_LONG);
  1305. unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
  1306. int first_page;
  1307. int last_page;
  1308. int page_nr;
  1309. int first_word;
  1310. int last_word;
  1311. if (e - s <= 3*BITS_PER_LONG) {
  1312. /* don't bother; el and sl may even be wrong. */
  1313. spin_lock_irq(&b->bm_lock);
  1314. __bm_change_bits_to(mdev, s, e, 1);
  1315. spin_unlock_irq(&b->bm_lock);
  1316. return;
  1317. }
  1318. /* difference is large enough that we can trust sl and el */
  1319. spin_lock_irq(&b->bm_lock);
  1320. /* bits filling the current long */
  1321. if (sl)
  1322. __bm_change_bits_to(mdev, s, sl-1, 1);
  1323. first_page = sl >> (3 + PAGE_SHIFT);
  1324. last_page = el >> (3 + PAGE_SHIFT);
  1325. /* MLPP: modulo longs per page */
  1326. /* LWPP: long words per page */
  1327. first_word = MLPP(sl >> LN2_BPL);
  1328. last_word = LWPP;
  1329. /* first and full pages, unless first page == last page */
  1330. for (page_nr = first_page; page_nr < last_page; page_nr++) {
  1331. bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
  1332. spin_unlock_irq(&b->bm_lock);
  1333. cond_resched();
  1334. first_word = 0;
  1335. spin_lock_irq(&b->bm_lock);
  1336. }
  1337. /* last page (respectively only page, for first page == last page) */
  1338. last_word = MLPP(el >> LN2_BPL);
  1339. /* consider bitmap->bm_bits = 32768, bitmap->bm_number_of_pages = 1. (or multiples).
  1340. * ==> e = 32767, el = 32768, last_page = 2,
  1341. * and now last_word = 0.
  1342. * We do not want to touch last_page in this case,
  1343. * as we did not allocate it, it is not present in bitmap->bm_pages.
  1344. */
  1345. if (last_word)
  1346. bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
  1347. /* possibly trailing bits.
  1348. * example: (e & 63) == 63, el will be e+1.
  1349. * if that even was the very last bit,
  1350. * it would trigger an assert in __bm_change_bits_to()
  1351. */
  1352. if (el <= e)
  1353. __bm_change_bits_to(mdev, el, e, 1);
  1354. spin_unlock_irq(&b->bm_lock);
  1355. }
  1356. /* returns bit state
  1357. * wants bitnr, NOT sector.
  1358. * inherently racy... area needs to be locked by means of {al,rs}_lru
  1359. * 1 ... bit set
  1360. * 0 ... bit not set
  1361. * -1 ... first out of bounds access, stop testing for bits!
  1362. */
  1363. int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
  1364. {
  1365. unsigned long flags;
  1366. struct drbd_bitmap *b = mdev->bitmap;
  1367. unsigned long *p_addr;
  1368. int i;
  1369. if (!expect(b))
  1370. return 0;
  1371. if (!expect(b->bm_pages))
  1372. return 0;
  1373. spin_lock_irqsave(&b->bm_lock, flags);
  1374. if (BM_DONT_TEST & b->bm_flags)
  1375. bm_print_lock_info(mdev);
  1376. if (bitnr < b->bm_bits) {
  1377. p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
  1378. i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
  1379. bm_unmap(p_addr);
  1380. } else if (bitnr == b->bm_bits) {
  1381. i = -1;
  1382. } else { /* (bitnr > b->bm_bits) */
  1383. dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
  1384. i = 0;
  1385. }
  1386. spin_unlock_irqrestore(&b->bm_lock, flags);
  1387. return i;
  1388. }
  1389. /* returns number of bits set in the range [s, e] */
  1390. int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
  1391. {
  1392. unsigned long flags;
  1393. struct drbd_bitmap *b = mdev->bitmap;
  1394. unsigned long *p_addr = NULL;
  1395. unsigned long bitnr;
  1396. unsigned int page_nr = -1U;
  1397. int c = 0;
  1398. /* If this is called without a bitmap, that is a bug. But just to be
  1399. * robust in case we screwed up elsewhere, in that case pretend there
  1400. * was one dirty bit in the requested area, so we won't try to do a
  1401. * local read there (no bitmap probably implies no disk) */
  1402. if (!expect(b))
  1403. return 1;
  1404. if (!expect(b->bm_pages))
  1405. return 1;
  1406. spin_lock_irqsave(&b->bm_lock, flags);
  1407. if (BM_DONT_TEST & b->bm_flags)
  1408. bm_print_lock_info(mdev);
  1409. for (bitnr = s; bitnr <= e; bitnr++) {
  1410. unsigned int idx = bm_bit_to_page_idx(b, bitnr);
  1411. if (page_nr != idx) {
  1412. page_nr = idx;
  1413. if (p_addr)
  1414. bm_unmap(p_addr);
  1415. p_addr = bm_map_pidx(b, idx);
  1416. }
  1417. if (expect(bitnr < b->bm_bits))
  1418. c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
  1419. else
  1420. dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
  1421. }
  1422. if (p_addr)
  1423. bm_unmap(p_addr);
  1424. spin_unlock_irqrestore(&b->bm_lock, flags);
  1425. return c;
  1426. }
  1427. /* inherently racy...
  1428. * return value may be already out-of-date when this function returns.
  1429. * but the general usage is that this is only use during a cstate when bits are
  1430. * only cleared, not set, and typically only care for the case when the return
  1431. * value is zero, or we already "locked" this "bitmap extent" by other means.
  1432. *
  1433. * enr is bm-extent number, since we chose to name one sector (512 bytes)
  1434. * worth of the bitmap a "bitmap extent".
  1435. *
  1436. * TODO
  1437. * I think since we use it like a reference count, we should use the real
  1438. * reference count of some bitmap extent element from some lru instead...
  1439. *
  1440. */
  1441. int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
  1442. {
  1443. struct drbd_bitmap *b = mdev->bitmap;
  1444. int count, s, e;
  1445. unsigned long flags;
  1446. unsigned long *p_addr, *bm;
  1447. if (!expect(b))
  1448. return 0;
  1449. if (!expect(b->bm_pages))
  1450. return 0;
  1451. spin_lock_irqsave(&b->bm_lock, flags);
  1452. if (BM_DONT_TEST & b->bm_flags)
  1453. bm_print_lock_info(mdev);
  1454. s = S2W(enr);
  1455. e = min((size_t)S2W(enr+1), b->bm_words);
  1456. count = 0;
  1457. if (s < b->bm_words) {
  1458. int n = e-s;
  1459. p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
  1460. bm = p_addr + MLPP(s);
  1461. while (n--)
  1462. count += hweight_long(*bm++);
  1463. bm_unmap(p_addr);
  1464. } else {
  1465. dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
  1466. }
  1467. spin_unlock_irqrestore(&b->bm_lock, flags);
  1468. return count;
  1469. }