drbd_bitmap.c 46 KB

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