drbd_actlog.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. drbd_actlog.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/drbd.h>
  22. #include <linux/drbd_limits.h>
  23. #include <linux/dynamic_debug.h>
  24. #include "drbd_int.h"
  25. #include "drbd_wrappers.h"
  26. enum al_transaction_types {
  27. AL_TR_UPDATE = 0,
  28. AL_TR_INITIALIZED = 0xffff
  29. };
  30. /* all fields on disc in big endian */
  31. struct __packed al_transaction_on_disk {
  32. /* don't we all like magic */
  33. __be32 magic;
  34. /* to identify the most recent transaction block
  35. * in the on disk ring buffer */
  36. __be32 tr_number;
  37. /* checksum on the full 4k block, with this field set to 0. */
  38. __be32 crc32c;
  39. /* type of transaction, special transaction types like:
  40. * purge-all, set-all-idle, set-all-active, ... to-be-defined
  41. * see also enum al_transaction_types */
  42. __be16 transaction_type;
  43. /* we currently allow only a few thousand extents,
  44. * so 16bit will be enough for the slot number. */
  45. /* how many updates in this transaction */
  46. __be16 n_updates;
  47. /* maximum slot number, "al-extents" in drbd.conf speak.
  48. * Having this in each transaction should make reconfiguration
  49. * of that parameter easier. */
  50. __be16 context_size;
  51. /* slot number the context starts with */
  52. __be16 context_start_slot_nr;
  53. /* Some reserved bytes. Expected usage is a 64bit counter of
  54. * sectors-written since device creation, and other data generation tag
  55. * supporting usage */
  56. __be32 __reserved[4];
  57. /* --- 36 byte used --- */
  58. /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
  59. * in one transaction, then use the remaining byte in the 4k block for
  60. * context information. "Flexible" number of updates per transaction
  61. * does not help, as we have to account for the case when all update
  62. * slots are used anyways, so it would only complicate code without
  63. * additional benefit.
  64. */
  65. __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
  66. /* but the extent number is 32bit, which at an extent size of 4 MiB
  67. * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
  68. __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
  69. /* --- 420 bytes used (36 + 64*6) --- */
  70. /* 4096 - 420 = 3676 = 919 * 4 */
  71. __be32 context[AL_CONTEXT_PER_TRANSACTION];
  72. };
  73. struct update_odbm_work {
  74. struct drbd_work w;
  75. unsigned int enr;
  76. };
  77. struct update_al_work {
  78. struct drbd_work w;
  79. struct completion event;
  80. int err;
  81. };
  82. struct drbd_atodb_wait {
  83. atomic_t count;
  84. struct completion io_done;
  85. struct drbd_conf *mdev;
  86. int error;
  87. };
  88. static int w_al_write_transaction(struct drbd_work *, int);
  89. static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
  90. struct drbd_backing_dev *bdev,
  91. struct page *page, sector_t sector,
  92. int rw, int size)
  93. {
  94. struct bio *bio;
  95. struct drbd_md_io md_io;
  96. int err;
  97. md_io.mdev = mdev;
  98. init_completion(&md_io.event);
  99. md_io.error = 0;
  100. if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
  101. rw |= REQ_FUA | REQ_FLUSH;
  102. rw |= REQ_SYNC;
  103. bio = bio_alloc_drbd(GFP_NOIO);
  104. bio->bi_bdev = bdev->md_bdev;
  105. bio->bi_sector = sector;
  106. err = -EIO;
  107. if (bio_add_page(bio, page, size, 0) != size)
  108. goto out;
  109. bio->bi_private = &md_io;
  110. bio->bi_end_io = drbd_md_io_complete;
  111. bio->bi_rw = rw;
  112. if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
  113. bio_endio(bio, -EIO);
  114. else
  115. submit_bio(rw, bio);
  116. wait_for_completion(&md_io.event);
  117. if (bio_flagged(bio, BIO_UPTODATE))
  118. err = md_io.error;
  119. out:
  120. bio_put(bio);
  121. return err;
  122. }
  123. int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
  124. sector_t sector, int rw)
  125. {
  126. int err;
  127. struct page *iop = mdev->md_io_page;
  128. D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
  129. BUG_ON(!bdev->md_bdev);
  130. dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
  131. current->comm, current->pid, __func__,
  132. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  133. if (sector < drbd_md_first_sector(bdev) ||
  134. sector + 7 > drbd_md_last_sector(bdev))
  135. dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
  136. current->comm, current->pid, __func__,
  137. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  138. err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
  139. if (err) {
  140. dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
  141. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  142. }
  143. return err;
  144. }
  145. static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
  146. {
  147. struct lc_element *al_ext;
  148. struct lc_element *tmp;
  149. int wake;
  150. spin_lock_irq(&mdev->al_lock);
  151. tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
  152. if (unlikely(tmp != NULL)) {
  153. struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
  154. if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
  155. wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
  156. spin_unlock_irq(&mdev->al_lock);
  157. if (wake)
  158. wake_up(&mdev->al_wait);
  159. return NULL;
  160. }
  161. }
  162. al_ext = lc_get(mdev->act_log, enr);
  163. spin_unlock_irq(&mdev->al_lock);
  164. return al_ext;
  165. }
  166. void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i)
  167. {
  168. /* for bios crossing activity log extent boundaries,
  169. * we may need to activate two extents in one go */
  170. unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
  171. unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
  172. unsigned enr;
  173. bool locked = false;
  174. D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
  175. for (enr = first; enr <= last; enr++)
  176. wait_event(mdev->al_wait, _al_get(mdev, enr) != NULL);
  177. /* Serialize multiple transactions.
  178. * This uses test_and_set_bit, memory barrier is implicit.
  179. */
  180. wait_event(mdev->al_wait,
  181. mdev->act_log->pending_changes == 0 ||
  182. (locked = lc_try_lock_for_transaction(mdev->act_log)));
  183. if (locked) {
  184. /* drbd_al_write_transaction(mdev,al_ext,enr);
  185. * recurses into generic_make_request(), which
  186. * disallows recursion, bios being serialized on the
  187. * current->bio_tail list now.
  188. * we have to delegate updates to the activity log
  189. * to the worker thread. */
  190. /* Double check: it may have been committed by someone else,
  191. * while we have been waiting for the lock. */
  192. if (mdev->act_log->pending_changes) {
  193. struct update_al_work al_work;
  194. init_completion(&al_work.event);
  195. al_work.w.cb = w_al_write_transaction;
  196. al_work.w.mdev = mdev;
  197. drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
  198. wait_for_completion(&al_work.event);
  199. mdev->al_writ_cnt++;
  200. spin_lock_irq(&mdev->al_lock);
  201. /* FIXME
  202. if (al_work.err)
  203. we need an "lc_cancel" here;
  204. */
  205. lc_committed(mdev->act_log);
  206. spin_unlock_irq(&mdev->al_lock);
  207. }
  208. lc_unlock(mdev->act_log);
  209. wake_up(&mdev->al_wait);
  210. }
  211. }
  212. void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i)
  213. {
  214. /* for bios crossing activity log extent boundaries,
  215. * we may need to activate two extents in one go */
  216. unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
  217. unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
  218. unsigned enr;
  219. struct lc_element *extent;
  220. unsigned long flags;
  221. bool wake = false;
  222. spin_lock_irqsave(&mdev->al_lock, flags);
  223. for (enr = first; enr <= last; enr++) {
  224. extent = lc_find(mdev->act_log, enr);
  225. if (!extent) {
  226. dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
  227. continue;
  228. }
  229. if (lc_put(mdev->act_log, extent) == 0)
  230. wake = true;
  231. }
  232. spin_unlock_irqrestore(&mdev->al_lock, flags);
  233. wake_up(&mdev->al_wait);
  234. }
  235. #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
  236. /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
  237. * are still coupled, or assume too much about their relation.
  238. * Code below will not work if this is violated.
  239. * Will be cleaned up with some followup patch.
  240. */
  241. # error FIXME
  242. #endif
  243. static unsigned int al_extent_to_bm_page(unsigned int al_enr)
  244. {
  245. return al_enr >>
  246. /* bit to page */
  247. ((PAGE_SHIFT + 3) -
  248. /* al extent number to bit */
  249. (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
  250. }
  251. static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
  252. {
  253. return rs_enr >>
  254. /* bit to page */
  255. ((PAGE_SHIFT + 3) -
  256. /* resync extent number to bit */
  257. (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
  258. }
  259. static int
  260. w_al_write_transaction(struct drbd_work *w, int unused)
  261. {
  262. struct update_al_work *aw = container_of(w, struct update_al_work, w);
  263. struct drbd_conf *mdev = w->mdev;
  264. struct al_transaction_on_disk *buffer;
  265. struct lc_element *e;
  266. sector_t sector;
  267. int i, mx;
  268. unsigned extent_nr;
  269. unsigned crc = 0;
  270. if (!get_ldev(mdev)) {
  271. dev_err(DEV, "disk is %s, cannot start al transaction\n",
  272. drbd_disk_str(mdev->state.disk));
  273. aw->err = -EIO;
  274. complete(&((struct update_al_work *)w)->event);
  275. return 0;
  276. }
  277. /* The bitmap write may have failed, causing a state change. */
  278. if (mdev->state.disk < D_INCONSISTENT) {
  279. dev_err(DEV,
  280. "disk is %s, cannot write al transaction\n",
  281. drbd_disk_str(mdev->state.disk));
  282. aw->err = -EIO;
  283. complete(&((struct update_al_work *)w)->event);
  284. put_ldev(mdev);
  285. return 0;
  286. }
  287. mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
  288. buffer = page_address(mdev->md_io_page);
  289. memset(buffer, 0, sizeof(*buffer));
  290. buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
  291. buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
  292. i = 0;
  293. /* Even though no one can start to change this list
  294. * once we set the LC_LOCKED -- from drbd_al_begin_io(),
  295. * lc_try_lock_for_transaction() --, someone may still
  296. * be in the process of changing it. */
  297. spin_lock_irq(&mdev->al_lock);
  298. list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
  299. if (i == AL_UPDATES_PER_TRANSACTION) {
  300. i++;
  301. break;
  302. }
  303. buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
  304. buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
  305. if (e->lc_number != LC_FREE)
  306. drbd_bm_mark_for_writeout(mdev,
  307. al_extent_to_bm_page(e->lc_number));
  308. i++;
  309. }
  310. spin_unlock_irq(&mdev->al_lock);
  311. BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
  312. buffer->n_updates = cpu_to_be16(i);
  313. for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
  314. buffer->update_slot_nr[i] = cpu_to_be16(-1);
  315. buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
  316. }
  317. buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
  318. buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
  319. mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
  320. mdev->act_log->nr_elements - mdev->al_tr_cycle);
  321. for (i = 0; i < mx; i++) {
  322. unsigned idx = mdev->al_tr_cycle + i;
  323. extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
  324. buffer->context[i] = cpu_to_be32(extent_nr);
  325. }
  326. for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
  327. buffer->context[i] = cpu_to_be32(LC_FREE);
  328. mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
  329. if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
  330. mdev->al_tr_cycle = 0;
  331. sector = mdev->ldev->md.md_offset
  332. + mdev->ldev->md.al_offset
  333. + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
  334. crc = crc32c(0, buffer, 4096);
  335. buffer->crc32c = cpu_to_be32(crc);
  336. if (drbd_bm_write_hinted(mdev))
  337. aw->err = -EIO;
  338. /* drbd_chk_io_error done already */
  339. else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
  340. aw->err = -EIO;
  341. drbd_chk_io_error(mdev, 1, true);
  342. } else {
  343. /* advance ringbuffer position and transaction counter */
  344. mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
  345. mdev->al_tr_number++;
  346. }
  347. mutex_unlock(&mdev->md_io_mutex);
  348. complete(&((struct update_al_work *)w)->event);
  349. put_ldev(mdev);
  350. return 0;
  351. }
  352. /* FIXME
  353. * reading of the activity log,
  354. * and potentially dirtying of the affected bitmap regions,
  355. * should be done from userland only.
  356. * DRBD would simply always attach with an empty activity log,
  357. * and refuse to attach to something that looks like a crashed primary.
  358. */
  359. /**
  360. * drbd_al_read_tr() - Read a single transaction from the on disk activity log
  361. * @mdev: DRBD device.
  362. * @bdev: Block device to read form.
  363. * @b: pointer to an al_transaction.
  364. * @index: On disk slot of the transaction to read.
  365. *
  366. * Returns -1 on IO error, 0 on checksum error and 1 upon success.
  367. */
  368. static int drbd_al_read_tr(struct drbd_conf *mdev,
  369. struct drbd_backing_dev *bdev,
  370. int index)
  371. {
  372. struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
  373. sector_t sector;
  374. u32 crc;
  375. sector = bdev->md.md_offset
  376. + bdev->md.al_offset
  377. + index * (MD_BLOCK_SIZE>>9);
  378. /* Dont process error normally,
  379. * as this is done before disk is attached! */
  380. if (drbd_md_sync_page_io(mdev, bdev, sector, READ))
  381. return -1;
  382. if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
  383. return 0;
  384. if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
  385. return 0;
  386. if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
  387. return 0;
  388. if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
  389. return 0;
  390. crc = be32_to_cpu(b->crc32c);
  391. b->crc32c = 0;
  392. if (!expect(crc == crc32c(0, b, 4096)))
  393. return 0;
  394. return 1;
  395. }
  396. /**
  397. * drbd_al_read_log() - Restores the activity log from its on disk representation.
  398. * @mdev: DRBD device.
  399. * @bdev: Block device to read form.
  400. *
  401. * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
  402. */
  403. int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  404. {
  405. struct al_transaction_on_disk *b;
  406. int i;
  407. int rv;
  408. int mx;
  409. int active_extents = 0;
  410. int transactions = 0;
  411. int found_valid = 0;
  412. int found_initialized = 0;
  413. int from = 0;
  414. int to = 0;
  415. u32 from_tnr = 0;
  416. u32 to_tnr = 0;
  417. u32 cnr;
  418. /* Note that this is expected to be called with a newly created,
  419. * clean and all unused activity log of the "expected size".
  420. */
  421. /* lock out all other meta data io for now,
  422. * and make sure the page is mapped.
  423. */
  424. mutex_lock(&mdev->md_io_mutex);
  425. b = page_address(mdev->md_io_page);
  426. /* Always use the full ringbuffer space for now.
  427. * possible optimization: read in all of it,
  428. * then scan the in-memory pages. */
  429. mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
  430. /* Find the valid transaction in the log */
  431. for (i = 0; i < mx; i++) {
  432. rv = drbd_al_read_tr(mdev, bdev, i);
  433. /* invalid data in that block */
  434. if (rv == 0)
  435. continue;
  436. if (be16_to_cpu(b->transaction_type) == AL_TR_INITIALIZED) {
  437. ++found_initialized;
  438. continue;
  439. }
  440. /* IO error */
  441. if (rv == -1) {
  442. mutex_unlock(&mdev->md_io_mutex);
  443. return 0;
  444. }
  445. cnr = be32_to_cpu(b->tr_number);
  446. if (++found_valid == 1) {
  447. from = i;
  448. to = i;
  449. from_tnr = cnr;
  450. to_tnr = cnr;
  451. continue;
  452. }
  453. D_ASSERT(cnr != to_tnr);
  454. D_ASSERT(cnr != from_tnr);
  455. if ((int)cnr - (int)from_tnr < 0) {
  456. D_ASSERT(from_tnr - cnr + i - from == mx);
  457. from = i;
  458. from_tnr = cnr;
  459. }
  460. if ((int)cnr - (int)to_tnr > 0) {
  461. D_ASSERT(cnr - to_tnr == i - to);
  462. to = i;
  463. to_tnr = cnr;
  464. }
  465. }
  466. if (!found_valid) {
  467. if (found_initialized != mx)
  468. dev_warn(DEV, "No usable activity log found.\n");
  469. mutex_unlock(&mdev->md_io_mutex);
  470. return 1;
  471. }
  472. /* Read the valid transactions.
  473. * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
  474. i = from;
  475. while (1) {
  476. struct lc_element *e;
  477. unsigned j, n, slot, extent_nr;
  478. rv = drbd_al_read_tr(mdev, bdev, i);
  479. if (!expect(rv != 0))
  480. goto cancel;
  481. if (rv == -1) {
  482. mutex_unlock(&mdev->md_io_mutex);
  483. return 0;
  484. }
  485. /* deal with different transaction types.
  486. * not yet implemented */
  487. if (!expect(b->transaction_type == 0))
  488. goto cancel;
  489. /* on the fly re-create/resize activity log?
  490. * will be a special transaction type flag. */
  491. if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
  492. goto cancel;
  493. if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
  494. goto cancel;
  495. /* We are the only user of the activity log right now,
  496. * don't actually need to take that lock. */
  497. spin_lock_irq(&mdev->al_lock);
  498. /* first, apply the context, ... */
  499. for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
  500. j < AL_CONTEXT_PER_TRANSACTION &&
  501. slot < mdev->act_log->nr_elements; j++, slot++) {
  502. extent_nr = be32_to_cpu(b->context[j]);
  503. e = lc_element_by_index(mdev->act_log, slot);
  504. if (e->lc_number != extent_nr) {
  505. if (extent_nr != LC_FREE)
  506. active_extents++;
  507. else
  508. active_extents--;
  509. }
  510. lc_set(mdev->act_log, extent_nr, slot);
  511. }
  512. /* ... then apply the updates,
  513. * which override the context information.
  514. * drbd_al_read_tr already did the rangecheck
  515. * on n <= AL_UPDATES_PER_TRANSACTION */
  516. n = be16_to_cpu(b->n_updates);
  517. for (j = 0; j < n; j++) {
  518. slot = be16_to_cpu(b->update_slot_nr[j]);
  519. extent_nr = be32_to_cpu(b->update_extent_nr[j]);
  520. if (!expect(slot < mdev->act_log->nr_elements))
  521. break;
  522. e = lc_element_by_index(mdev->act_log, slot);
  523. if (e->lc_number != extent_nr) {
  524. if (extent_nr != LC_FREE)
  525. active_extents++;
  526. else
  527. active_extents--;
  528. }
  529. lc_set(mdev->act_log, extent_nr, slot);
  530. }
  531. spin_unlock_irq(&mdev->al_lock);
  532. transactions++;
  533. cancel:
  534. if (i == to)
  535. break;
  536. i++;
  537. if (i >= mx)
  538. i = 0;
  539. }
  540. mdev->al_tr_number = to_tnr+1;
  541. mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
  542. /* ok, we are done with it */
  543. mutex_unlock(&mdev->md_io_mutex);
  544. dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
  545. transactions, active_extents);
  546. return 1;
  547. }
  548. /**
  549. * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
  550. * @mdev: DRBD device.
  551. */
  552. void drbd_al_apply_to_bm(struct drbd_conf *mdev)
  553. {
  554. unsigned int enr;
  555. unsigned long add = 0;
  556. char ppb[10];
  557. int i, tmp;
  558. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  559. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  560. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  561. if (enr == LC_FREE)
  562. continue;
  563. tmp = drbd_bm_ALe_set_all(mdev, enr);
  564. dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
  565. add += tmp;
  566. }
  567. lc_unlock(mdev->act_log);
  568. wake_up(&mdev->al_wait);
  569. dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
  570. ppsize(ppb, Bit2KB(add)));
  571. }
  572. static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
  573. {
  574. int rv;
  575. spin_lock_irq(&mdev->al_lock);
  576. rv = (al_ext->refcnt == 0);
  577. if (likely(rv))
  578. lc_del(mdev->act_log, al_ext);
  579. spin_unlock_irq(&mdev->al_lock);
  580. return rv;
  581. }
  582. /**
  583. * drbd_al_shrink() - Removes all active extents form the activity log
  584. * @mdev: DRBD device.
  585. *
  586. * Removes all active extents form the activity log, waiting until
  587. * the reference count of each entry dropped to 0 first, of course.
  588. *
  589. * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
  590. */
  591. void drbd_al_shrink(struct drbd_conf *mdev)
  592. {
  593. struct lc_element *al_ext;
  594. int i;
  595. D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
  596. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  597. al_ext = lc_element_by_index(mdev->act_log, i);
  598. if (al_ext->lc_number == LC_FREE)
  599. continue;
  600. wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
  601. }
  602. wake_up(&mdev->al_wait);
  603. }
  604. static int w_update_odbm(struct drbd_work *w, int unused)
  605. {
  606. struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
  607. struct drbd_conf *mdev = w->mdev;
  608. struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
  609. if (!get_ldev(mdev)) {
  610. if (__ratelimit(&drbd_ratelimit_state))
  611. dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
  612. kfree(udw);
  613. return 0;
  614. }
  615. drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
  616. put_ldev(mdev);
  617. kfree(udw);
  618. if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
  619. switch (mdev->state.conn) {
  620. case C_SYNC_SOURCE: case C_SYNC_TARGET:
  621. case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
  622. drbd_resync_finished(mdev);
  623. default:
  624. /* nothing to do */
  625. break;
  626. }
  627. }
  628. drbd_bcast_event(mdev, &sib);
  629. return 0;
  630. }
  631. /* ATTENTION. The AL's extents are 4MB each, while the extents in the
  632. * resync LRU-cache are 16MB each.
  633. * The caller of this function has to hold an get_ldev() reference.
  634. *
  635. * TODO will be obsoleted once we have a caching lru of the on disk bitmap
  636. */
  637. static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
  638. int count, int success)
  639. {
  640. struct lc_element *e;
  641. struct update_odbm_work *udw;
  642. unsigned int enr;
  643. D_ASSERT(atomic_read(&mdev->local_cnt));
  644. /* I simply assume that a sector/size pair never crosses
  645. * a 16 MB extent border. (Currently this is true...) */
  646. enr = BM_SECT_TO_EXT(sector);
  647. e = lc_get(mdev->resync, enr);
  648. if (e) {
  649. struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
  650. if (ext->lce.lc_number == enr) {
  651. if (success)
  652. ext->rs_left -= count;
  653. else
  654. ext->rs_failed += count;
  655. if (ext->rs_left < ext->rs_failed) {
  656. dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
  657. "rs_failed=%d count=%d\n",
  658. (unsigned long long)sector,
  659. ext->lce.lc_number, ext->rs_left,
  660. ext->rs_failed, count);
  661. dump_stack();
  662. lc_put(mdev->resync, &ext->lce);
  663. conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
  664. return;
  665. }
  666. } else {
  667. /* Normally this element should be in the cache,
  668. * since drbd_rs_begin_io() pulled it already in.
  669. *
  670. * But maybe an application write finished, and we set
  671. * something outside the resync lru_cache in sync.
  672. */
  673. int rs_left = drbd_bm_e_weight(mdev, enr);
  674. if (ext->flags != 0) {
  675. dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
  676. " -> %d[%u;00]\n",
  677. ext->lce.lc_number, ext->rs_left,
  678. ext->flags, enr, rs_left);
  679. ext->flags = 0;
  680. }
  681. if (ext->rs_failed) {
  682. dev_warn(DEV, "Kicking resync_lru element enr=%u "
  683. "out with rs_failed=%d\n",
  684. ext->lce.lc_number, ext->rs_failed);
  685. }
  686. ext->rs_left = rs_left;
  687. ext->rs_failed = success ? 0 : count;
  688. /* we don't keep a persistent log of the resync lru,
  689. * we can commit any change right away. */
  690. lc_committed(mdev->resync);
  691. }
  692. lc_put(mdev->resync, &ext->lce);
  693. /* no race, we are within the al_lock! */
  694. if (ext->rs_left == ext->rs_failed) {
  695. ext->rs_failed = 0;
  696. udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
  697. if (udw) {
  698. udw->enr = ext->lce.lc_number;
  699. udw->w.cb = w_update_odbm;
  700. udw->w.mdev = mdev;
  701. drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
  702. } else {
  703. dev_warn(DEV, "Could not kmalloc an udw\n");
  704. }
  705. }
  706. } else {
  707. dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
  708. mdev->resync_locked,
  709. mdev->resync->nr_elements,
  710. mdev->resync->flags);
  711. }
  712. }
  713. void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
  714. {
  715. unsigned long now = jiffies;
  716. unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
  717. int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
  718. if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
  719. if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
  720. mdev->state.conn != C_PAUSED_SYNC_T &&
  721. mdev->state.conn != C_PAUSED_SYNC_S) {
  722. mdev->rs_mark_time[next] = now;
  723. mdev->rs_mark_left[next] = still_to_go;
  724. mdev->rs_last_mark = next;
  725. }
  726. }
  727. }
  728. /* clear the bit corresponding to the piece of storage in question:
  729. * size byte of data starting from sector. Only clear a bits of the affected
  730. * one ore more _aligned_ BM_BLOCK_SIZE blocks.
  731. *
  732. * called by worker on C_SYNC_TARGET and receiver on SyncSource.
  733. *
  734. */
  735. void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
  736. const char *file, const unsigned int line)
  737. {
  738. /* Is called from worker and receiver context _only_ */
  739. unsigned long sbnr, ebnr, lbnr;
  740. unsigned long count = 0;
  741. sector_t esector, nr_sectors;
  742. int wake_up = 0;
  743. unsigned long flags;
  744. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  745. dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
  746. (unsigned long long)sector, size);
  747. return;
  748. }
  749. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  750. esector = sector + (size >> 9) - 1;
  751. if (!expect(sector < nr_sectors))
  752. return;
  753. if (!expect(esector < nr_sectors))
  754. esector = nr_sectors - 1;
  755. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  756. /* we clear it (in sync).
  757. * round up start sector, round down end sector. we make sure we only
  758. * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
  759. if (unlikely(esector < BM_SECT_PER_BIT-1))
  760. return;
  761. if (unlikely(esector == (nr_sectors-1)))
  762. ebnr = lbnr;
  763. else
  764. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  765. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  766. if (sbnr > ebnr)
  767. return;
  768. /*
  769. * ok, (capacity & 7) != 0 sometimes, but who cares...
  770. * we count rs_{total,left} in bits, not sectors.
  771. */
  772. count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
  773. if (count && get_ldev(mdev)) {
  774. drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
  775. spin_lock_irqsave(&mdev->al_lock, flags);
  776. drbd_try_clear_on_disk_bm(mdev, sector, count, true);
  777. spin_unlock_irqrestore(&mdev->al_lock, flags);
  778. /* just wake_up unconditional now, various lc_chaged(),
  779. * lc_put() in drbd_try_clear_on_disk_bm(). */
  780. wake_up = 1;
  781. put_ldev(mdev);
  782. }
  783. if (wake_up)
  784. wake_up(&mdev->al_wait);
  785. }
  786. /*
  787. * this is intended to set one request worth of data out of sync.
  788. * affects at least 1 bit,
  789. * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
  790. *
  791. * called by tl_clear and drbd_send_dblock (==drbd_make_request).
  792. * so this can be _any_ process.
  793. */
  794. int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
  795. const char *file, const unsigned int line)
  796. {
  797. unsigned long sbnr, ebnr, lbnr, flags;
  798. sector_t esector, nr_sectors;
  799. unsigned int enr, count = 0;
  800. struct lc_element *e;
  801. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  802. dev_err(DEV, "sector: %llus, size: %d\n",
  803. (unsigned long long)sector, size);
  804. return 0;
  805. }
  806. if (!get_ldev(mdev))
  807. return 0; /* no disk, no metadata, no bitmap to set bits in */
  808. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  809. esector = sector + (size >> 9) - 1;
  810. if (!expect(sector < nr_sectors))
  811. goto out;
  812. if (!expect(esector < nr_sectors))
  813. esector = nr_sectors - 1;
  814. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  815. /* we set it out of sync,
  816. * we do not need to round anything here */
  817. sbnr = BM_SECT_TO_BIT(sector);
  818. ebnr = BM_SECT_TO_BIT(esector);
  819. /* ok, (capacity & 7) != 0 sometimes, but who cares...
  820. * we count rs_{total,left} in bits, not sectors. */
  821. spin_lock_irqsave(&mdev->al_lock, flags);
  822. count = drbd_bm_set_bits(mdev, sbnr, ebnr);
  823. enr = BM_SECT_TO_EXT(sector);
  824. e = lc_find(mdev->resync, enr);
  825. if (e)
  826. lc_entry(e, struct bm_extent, lce)->rs_left += count;
  827. spin_unlock_irqrestore(&mdev->al_lock, flags);
  828. out:
  829. put_ldev(mdev);
  830. return count;
  831. }
  832. static
  833. struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
  834. {
  835. struct lc_element *e;
  836. struct bm_extent *bm_ext;
  837. int wakeup = 0;
  838. unsigned long rs_flags;
  839. spin_lock_irq(&mdev->al_lock);
  840. if (mdev->resync_locked > mdev->resync->nr_elements/2) {
  841. spin_unlock_irq(&mdev->al_lock);
  842. return NULL;
  843. }
  844. e = lc_get(mdev->resync, enr);
  845. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  846. if (bm_ext) {
  847. if (bm_ext->lce.lc_number != enr) {
  848. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  849. bm_ext->rs_failed = 0;
  850. lc_committed(mdev->resync);
  851. wakeup = 1;
  852. }
  853. if (bm_ext->lce.refcnt == 1)
  854. mdev->resync_locked++;
  855. set_bit(BME_NO_WRITES, &bm_ext->flags);
  856. }
  857. rs_flags = mdev->resync->flags;
  858. spin_unlock_irq(&mdev->al_lock);
  859. if (wakeup)
  860. wake_up(&mdev->al_wait);
  861. if (!bm_ext) {
  862. if (rs_flags & LC_STARVING)
  863. dev_warn(DEV, "Have to wait for element"
  864. " (resync LRU too small?)\n");
  865. BUG_ON(rs_flags & LC_LOCKED);
  866. }
  867. return bm_ext;
  868. }
  869. static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
  870. {
  871. int rv;
  872. spin_lock_irq(&mdev->al_lock);
  873. rv = lc_is_used(mdev->act_log, enr);
  874. spin_unlock_irq(&mdev->al_lock);
  875. return rv;
  876. }
  877. /**
  878. * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
  879. * @mdev: DRBD device.
  880. * @sector: The sector number.
  881. *
  882. * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
  883. */
  884. int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  885. {
  886. unsigned int enr = BM_SECT_TO_EXT(sector);
  887. struct bm_extent *bm_ext;
  888. int i, sig;
  889. int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
  890. 200 times -> 20 seconds. */
  891. retry:
  892. sig = wait_event_interruptible(mdev->al_wait,
  893. (bm_ext = _bme_get(mdev, enr)));
  894. if (sig)
  895. return -EINTR;
  896. if (test_bit(BME_LOCKED, &bm_ext->flags))
  897. return 0;
  898. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  899. sig = wait_event_interruptible(mdev->al_wait,
  900. !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
  901. test_bit(BME_PRIORITY, &bm_ext->flags));
  902. if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
  903. spin_lock_irq(&mdev->al_lock);
  904. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  905. bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
  906. mdev->resync_locked--;
  907. wake_up(&mdev->al_wait);
  908. }
  909. spin_unlock_irq(&mdev->al_lock);
  910. if (sig)
  911. return -EINTR;
  912. if (schedule_timeout_interruptible(HZ/10))
  913. return -EINTR;
  914. if (sa && --sa == 0)
  915. dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
  916. "Resync stalled?\n");
  917. goto retry;
  918. }
  919. }
  920. set_bit(BME_LOCKED, &bm_ext->flags);
  921. return 0;
  922. }
  923. /**
  924. * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
  925. * @mdev: DRBD device.
  926. * @sector: The sector number.
  927. *
  928. * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
  929. * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
  930. * if there is still application IO going on in this area.
  931. */
  932. int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  933. {
  934. unsigned int enr = BM_SECT_TO_EXT(sector);
  935. const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
  936. struct lc_element *e;
  937. struct bm_extent *bm_ext;
  938. int i;
  939. spin_lock_irq(&mdev->al_lock);
  940. if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
  941. /* in case you have very heavy scattered io, it may
  942. * stall the syncer undefined if we give up the ref count
  943. * when we try again and requeue.
  944. *
  945. * if we don't give up the refcount, but the next time
  946. * we are scheduled this extent has been "synced" by new
  947. * application writes, we'd miss the lc_put on the
  948. * extent we keep the refcount on.
  949. * so we remembered which extent we had to try again, and
  950. * if the next requested one is something else, we do
  951. * the lc_put here...
  952. * we also have to wake_up
  953. */
  954. e = lc_find(mdev->resync, mdev->resync_wenr);
  955. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  956. if (bm_ext) {
  957. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  958. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  959. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  960. mdev->resync_wenr = LC_FREE;
  961. if (lc_put(mdev->resync, &bm_ext->lce) == 0)
  962. mdev->resync_locked--;
  963. wake_up(&mdev->al_wait);
  964. } else {
  965. dev_alert(DEV, "LOGIC BUG\n");
  966. }
  967. }
  968. /* TRY. */
  969. e = lc_try_get(mdev->resync, enr);
  970. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  971. if (bm_ext) {
  972. if (test_bit(BME_LOCKED, &bm_ext->flags))
  973. goto proceed;
  974. if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
  975. mdev->resync_locked++;
  976. } else {
  977. /* we did set the BME_NO_WRITES,
  978. * but then could not set BME_LOCKED,
  979. * so we tried again.
  980. * drop the extra reference. */
  981. bm_ext->lce.refcnt--;
  982. D_ASSERT(bm_ext->lce.refcnt > 0);
  983. }
  984. goto check_al;
  985. } else {
  986. /* do we rather want to try later? */
  987. if (mdev->resync_locked > mdev->resync->nr_elements-3)
  988. goto try_again;
  989. /* Do or do not. There is no try. -- Yoda */
  990. e = lc_get(mdev->resync, enr);
  991. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  992. if (!bm_ext) {
  993. const unsigned long rs_flags = mdev->resync->flags;
  994. if (rs_flags & LC_STARVING)
  995. dev_warn(DEV, "Have to wait for element"
  996. " (resync LRU too small?)\n");
  997. BUG_ON(rs_flags & LC_LOCKED);
  998. goto try_again;
  999. }
  1000. if (bm_ext->lce.lc_number != enr) {
  1001. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  1002. bm_ext->rs_failed = 0;
  1003. lc_committed(mdev->resync);
  1004. wake_up(&mdev->al_wait);
  1005. D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
  1006. }
  1007. set_bit(BME_NO_WRITES, &bm_ext->flags);
  1008. D_ASSERT(bm_ext->lce.refcnt == 1);
  1009. mdev->resync_locked++;
  1010. goto check_al;
  1011. }
  1012. check_al:
  1013. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  1014. if (lc_is_used(mdev->act_log, al_enr+i))
  1015. goto try_again;
  1016. }
  1017. set_bit(BME_LOCKED, &bm_ext->flags);
  1018. proceed:
  1019. mdev->resync_wenr = LC_FREE;
  1020. spin_unlock_irq(&mdev->al_lock);
  1021. return 0;
  1022. try_again:
  1023. if (bm_ext)
  1024. mdev->resync_wenr = enr;
  1025. spin_unlock_irq(&mdev->al_lock);
  1026. return -EAGAIN;
  1027. }
  1028. void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
  1029. {
  1030. unsigned int enr = BM_SECT_TO_EXT(sector);
  1031. struct lc_element *e;
  1032. struct bm_extent *bm_ext;
  1033. unsigned long flags;
  1034. spin_lock_irqsave(&mdev->al_lock, flags);
  1035. e = lc_find(mdev->resync, enr);
  1036. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1037. if (!bm_ext) {
  1038. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1039. if (__ratelimit(&drbd_ratelimit_state))
  1040. dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
  1041. return;
  1042. }
  1043. if (bm_ext->lce.refcnt == 0) {
  1044. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1045. dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
  1046. "but refcnt is 0!?\n",
  1047. (unsigned long long)sector, enr);
  1048. return;
  1049. }
  1050. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  1051. bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
  1052. mdev->resync_locked--;
  1053. wake_up(&mdev->al_wait);
  1054. }
  1055. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1056. }
  1057. /**
  1058. * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
  1059. * @mdev: DRBD device.
  1060. */
  1061. void drbd_rs_cancel_all(struct drbd_conf *mdev)
  1062. {
  1063. spin_lock_irq(&mdev->al_lock);
  1064. if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
  1065. lc_reset(mdev->resync);
  1066. put_ldev(mdev);
  1067. }
  1068. mdev->resync_locked = 0;
  1069. mdev->resync_wenr = LC_FREE;
  1070. spin_unlock_irq(&mdev->al_lock);
  1071. wake_up(&mdev->al_wait);
  1072. }
  1073. /**
  1074. * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
  1075. * @mdev: DRBD device.
  1076. *
  1077. * Returns 0 upon success, -EAGAIN if at least one reference count was
  1078. * not zero.
  1079. */
  1080. int drbd_rs_del_all(struct drbd_conf *mdev)
  1081. {
  1082. struct lc_element *e;
  1083. struct bm_extent *bm_ext;
  1084. int i;
  1085. spin_lock_irq(&mdev->al_lock);
  1086. if (get_ldev_if_state(mdev, D_FAILED)) {
  1087. /* ok, ->resync is there. */
  1088. for (i = 0; i < mdev->resync->nr_elements; i++) {
  1089. e = lc_element_by_index(mdev->resync, i);
  1090. bm_ext = lc_entry(e, struct bm_extent, lce);
  1091. if (bm_ext->lce.lc_number == LC_FREE)
  1092. continue;
  1093. if (bm_ext->lce.lc_number == mdev->resync_wenr) {
  1094. dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
  1095. " got 'synced' by application io\n",
  1096. mdev->resync_wenr);
  1097. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1098. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  1099. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1100. mdev->resync_wenr = LC_FREE;
  1101. lc_put(mdev->resync, &bm_ext->lce);
  1102. }
  1103. if (bm_ext->lce.refcnt != 0) {
  1104. dev_info(DEV, "Retrying drbd_rs_del_all() later. "
  1105. "refcnt=%d\n", bm_ext->lce.refcnt);
  1106. put_ldev(mdev);
  1107. spin_unlock_irq(&mdev->al_lock);
  1108. return -EAGAIN;
  1109. }
  1110. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1111. D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
  1112. lc_del(mdev->resync, &bm_ext->lce);
  1113. }
  1114. D_ASSERT(mdev->resync->used == 0);
  1115. put_ldev(mdev);
  1116. }
  1117. spin_unlock_irq(&mdev->al_lock);
  1118. return 0;
  1119. }
  1120. /**
  1121. * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
  1122. * @mdev: DRBD device.
  1123. * @sector: The sector number.
  1124. * @size: Size of failed IO operation, in byte.
  1125. */
  1126. void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
  1127. {
  1128. /* Is called from worker and receiver context _only_ */
  1129. unsigned long sbnr, ebnr, lbnr;
  1130. unsigned long count;
  1131. sector_t esector, nr_sectors;
  1132. int wake_up = 0;
  1133. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  1134. dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
  1135. (unsigned long long)sector, size);
  1136. return;
  1137. }
  1138. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  1139. esector = sector + (size >> 9) - 1;
  1140. if (!expect(sector < nr_sectors))
  1141. return;
  1142. if (!expect(esector < nr_sectors))
  1143. esector = nr_sectors - 1;
  1144. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  1145. /*
  1146. * round up start sector, round down end sector. we make sure we only
  1147. * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
  1148. if (unlikely(esector < BM_SECT_PER_BIT-1))
  1149. return;
  1150. if (unlikely(esector == (nr_sectors-1)))
  1151. ebnr = lbnr;
  1152. else
  1153. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  1154. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  1155. if (sbnr > ebnr)
  1156. return;
  1157. /*
  1158. * ok, (capacity & 7) != 0 sometimes, but who cares...
  1159. * we count rs_{total,left} in bits, not sectors.
  1160. */
  1161. spin_lock_irq(&mdev->al_lock);
  1162. count = drbd_bm_count_bits(mdev, sbnr, ebnr);
  1163. if (count) {
  1164. mdev->rs_failed += count;
  1165. if (get_ldev(mdev)) {
  1166. drbd_try_clear_on_disk_bm(mdev, sector, count, false);
  1167. put_ldev(mdev);
  1168. }
  1169. /* just wake_up unconditional now, various lc_chaged(),
  1170. * lc_put() in drbd_try_clear_on_disk_bm(). */
  1171. wake_up = 1;
  1172. }
  1173. spin_unlock_irq(&mdev->al_lock);
  1174. if (wake_up)
  1175. wake_up(&mdev->al_wait);
  1176. }