drbd_bitmap.c 47 KB

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