drbd_actlog.c 37 KB

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