drbd_actlog.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 checksum 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 losing 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_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 | REQ_FLUSH;
  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. al_work.w.mdev = mdev;
  191. drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
  192. wait_for_completion(&al_work.event);
  193. mdev->al_writ_cnt++;
  194. spin_lock_irq(&mdev->al_lock);
  195. lc_changed(mdev->act_log, al_ext);
  196. spin_unlock_irq(&mdev->al_lock);
  197. wake_up(&mdev->al_wait);
  198. }
  199. }
  200. void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
  201. {
  202. unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
  203. struct lc_element *extent;
  204. unsigned long flags;
  205. spin_lock_irqsave(&mdev->al_lock, flags);
  206. extent = lc_find(mdev->act_log, enr);
  207. if (!extent) {
  208. spin_unlock_irqrestore(&mdev->al_lock, flags);
  209. dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
  210. return;
  211. }
  212. if (lc_put(mdev->act_log, extent) == 0)
  213. wake_up(&mdev->al_wait);
  214. spin_unlock_irqrestore(&mdev->al_lock, flags);
  215. }
  216. #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
  217. /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
  218. * are still coupled, or assume too much about their relation.
  219. * Code below will not work if this is violated.
  220. * Will be cleaned up with some followup patch.
  221. */
  222. # error FIXME
  223. #endif
  224. static unsigned int al_extent_to_bm_page(unsigned int al_enr)
  225. {
  226. return al_enr >>
  227. /* bit to page */
  228. ((PAGE_SHIFT + 3) -
  229. /* al extent number to bit */
  230. (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
  231. }
  232. static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
  233. {
  234. return rs_enr >>
  235. /* bit to page */
  236. ((PAGE_SHIFT + 3) -
  237. /* al extent number to bit */
  238. (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
  239. }
  240. int
  241. w_al_write_transaction(struct drbd_work *w, int unused)
  242. {
  243. struct update_al_work *aw = container_of(w, struct update_al_work, w);
  244. struct drbd_conf *mdev = w->mdev;
  245. struct lc_element *updated = aw->al_ext;
  246. const unsigned int new_enr = aw->enr;
  247. const unsigned int evicted = aw->old_enr;
  248. struct al_transaction *buffer;
  249. sector_t sector;
  250. int i, n, mx;
  251. unsigned int extent_nr;
  252. u32 xor_sum = 0;
  253. if (!get_ldev(mdev)) {
  254. dev_err(DEV,
  255. "disk is %s, cannot start al transaction (-%d +%d)\n",
  256. drbd_disk_str(mdev->state.disk), evicted, new_enr);
  257. complete(&((struct update_al_work *)w)->event);
  258. return 1;
  259. }
  260. /* do we have to do a bitmap write, first?
  261. * TODO reduce maximum latency:
  262. * submit both bios, then wait for both,
  263. * instead of doing two synchronous sector writes.
  264. * For now, we must not write the transaction,
  265. * if we cannot write out the bitmap of the evicted extent. */
  266. if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
  267. drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted));
  268. /* The bitmap write may have failed, causing a state change. */
  269. if (mdev->state.disk < D_INCONSISTENT) {
  270. dev_err(DEV,
  271. "disk is %s, cannot write al transaction (-%d +%d)\n",
  272. drbd_disk_str(mdev->state.disk), evicted, new_enr);
  273. complete(&((struct update_al_work *)w)->event);
  274. put_ldev(mdev);
  275. return 1;
  276. }
  277. mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
  278. buffer = (struct al_transaction *)page_address(mdev->md_io_page);
  279. buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
  280. buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
  281. n = lc_index_of(mdev->act_log, updated);
  282. buffer->updates[0].pos = cpu_to_be32(n);
  283. buffer->updates[0].extent = cpu_to_be32(new_enr);
  284. xor_sum ^= new_enr;
  285. mx = min_t(int, AL_EXTENTS_PT,
  286. mdev->act_log->nr_elements - mdev->al_tr_cycle);
  287. for (i = 0; i < mx; i++) {
  288. unsigned idx = mdev->al_tr_cycle + i;
  289. extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
  290. buffer->updates[i+1].pos = cpu_to_be32(idx);
  291. buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
  292. xor_sum ^= extent_nr;
  293. }
  294. for (; i < AL_EXTENTS_PT; i++) {
  295. buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
  296. buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
  297. xor_sum ^= LC_FREE;
  298. }
  299. mdev->al_tr_cycle += AL_EXTENTS_PT;
  300. if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
  301. mdev->al_tr_cycle = 0;
  302. buffer->xor_sum = cpu_to_be32(xor_sum);
  303. sector = mdev->ldev->md.md_offset
  304. + mdev->ldev->md.al_offset + mdev->al_tr_pos;
  305. if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
  306. drbd_chk_io_error(mdev, 1, true);
  307. if (++mdev->al_tr_pos >
  308. div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
  309. mdev->al_tr_pos = 0;
  310. D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
  311. mdev->al_tr_number++;
  312. mutex_unlock(&mdev->md_io_mutex);
  313. complete(&((struct update_al_work *)w)->event);
  314. put_ldev(mdev);
  315. return 1;
  316. }
  317. /**
  318. * drbd_al_read_tr() - Read a single transaction from the on disk activity log
  319. * @mdev: DRBD device.
  320. * @bdev: Block device to read form.
  321. * @b: pointer to an al_transaction.
  322. * @index: On disk slot of the transaction to read.
  323. *
  324. * Returns -1 on IO error, 0 on checksum error and 1 upon success.
  325. */
  326. static int drbd_al_read_tr(struct drbd_conf *mdev,
  327. struct drbd_backing_dev *bdev,
  328. struct al_transaction *b,
  329. int index)
  330. {
  331. sector_t sector;
  332. int rv, i;
  333. u32 xor_sum = 0;
  334. sector = bdev->md.md_offset + bdev->md.al_offset + index;
  335. /* Dont process error normally,
  336. * as this is done before disk is attached! */
  337. if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
  338. return -1;
  339. rv = (b->magic == cpu_to_be32(DRBD_MAGIC));
  340. for (i = 0; i < AL_EXTENTS_PT + 1; i++)
  341. xor_sum ^= be32_to_cpu(b->updates[i].extent);
  342. rv &= (xor_sum == be32_to_cpu(b->xor_sum));
  343. return rv;
  344. }
  345. /**
  346. * drbd_al_read_log() - Restores the activity log from its on disk representation.
  347. * @mdev: DRBD device.
  348. * @bdev: Block device to read form.
  349. *
  350. * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
  351. */
  352. int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  353. {
  354. struct al_transaction *buffer;
  355. int i;
  356. int rv;
  357. int mx;
  358. int active_extents = 0;
  359. int transactions = 0;
  360. int found_valid = 0;
  361. int from = 0;
  362. int to = 0;
  363. u32 from_tnr = 0;
  364. u32 to_tnr = 0;
  365. u32 cnr;
  366. mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
  367. /* lock out all other meta data io for now,
  368. * and make sure the page is mapped.
  369. */
  370. mutex_lock(&mdev->md_io_mutex);
  371. buffer = page_address(mdev->md_io_page);
  372. /* Find the valid transaction in the log */
  373. for (i = 0; i <= mx; i++) {
  374. rv = drbd_al_read_tr(mdev, bdev, buffer, i);
  375. if (rv == 0)
  376. continue;
  377. if (rv == -1) {
  378. mutex_unlock(&mdev->md_io_mutex);
  379. return 0;
  380. }
  381. cnr = be32_to_cpu(buffer->tr_number);
  382. if (++found_valid == 1) {
  383. from = i;
  384. to = i;
  385. from_tnr = cnr;
  386. to_tnr = cnr;
  387. continue;
  388. }
  389. if ((int)cnr - (int)from_tnr < 0) {
  390. D_ASSERT(from_tnr - cnr + i - from == mx+1);
  391. from = i;
  392. from_tnr = cnr;
  393. }
  394. if ((int)cnr - (int)to_tnr > 0) {
  395. D_ASSERT(cnr - to_tnr == i - to);
  396. to = i;
  397. to_tnr = cnr;
  398. }
  399. }
  400. if (!found_valid) {
  401. dev_warn(DEV, "No usable activity log found.\n");
  402. mutex_unlock(&mdev->md_io_mutex);
  403. return 1;
  404. }
  405. /* Read the valid transactions.
  406. * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
  407. i = from;
  408. while (1) {
  409. int j, pos;
  410. unsigned int extent_nr;
  411. unsigned int trn;
  412. rv = drbd_al_read_tr(mdev, bdev, buffer, i);
  413. if (!expect(rv != 0))
  414. goto cancel;
  415. if (rv == -1) {
  416. mutex_unlock(&mdev->md_io_mutex);
  417. return 0;
  418. }
  419. trn = be32_to_cpu(buffer->tr_number);
  420. spin_lock_irq(&mdev->al_lock);
  421. /* This loop runs backwards because in the cyclic
  422. elements there might be an old version of the
  423. updated element (in slot 0). So the element in slot 0
  424. can overwrite old versions. */
  425. for (j = AL_EXTENTS_PT; j >= 0; j--) {
  426. pos = be32_to_cpu(buffer->updates[j].pos);
  427. extent_nr = be32_to_cpu(buffer->updates[j].extent);
  428. if (extent_nr == LC_FREE)
  429. continue;
  430. lc_set(mdev->act_log, extent_nr, pos);
  431. active_extents++;
  432. }
  433. spin_unlock_irq(&mdev->al_lock);
  434. transactions++;
  435. cancel:
  436. if (i == to)
  437. break;
  438. i++;
  439. if (i > mx)
  440. i = 0;
  441. }
  442. mdev->al_tr_number = to_tnr+1;
  443. mdev->al_tr_pos = to;
  444. if (++mdev->al_tr_pos >
  445. div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
  446. mdev->al_tr_pos = 0;
  447. /* ok, we are done with it */
  448. mutex_unlock(&mdev->md_io_mutex);
  449. dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
  450. transactions, active_extents);
  451. return 1;
  452. }
  453. /**
  454. * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
  455. * @mdev: DRBD device.
  456. */
  457. void drbd_al_apply_to_bm(struct drbd_conf *mdev)
  458. {
  459. unsigned int enr;
  460. unsigned long add = 0;
  461. char ppb[10];
  462. int i, tmp;
  463. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  464. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  465. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  466. if (enr == LC_FREE)
  467. continue;
  468. tmp = drbd_bm_ALe_set_all(mdev, enr);
  469. dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
  470. add += tmp;
  471. }
  472. lc_unlock(mdev->act_log);
  473. wake_up(&mdev->al_wait);
  474. dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
  475. ppsize(ppb, Bit2KB(add)));
  476. }
  477. static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
  478. {
  479. int rv;
  480. spin_lock_irq(&mdev->al_lock);
  481. rv = (al_ext->refcnt == 0);
  482. if (likely(rv))
  483. lc_del(mdev->act_log, al_ext);
  484. spin_unlock_irq(&mdev->al_lock);
  485. return rv;
  486. }
  487. /**
  488. * drbd_al_shrink() - Removes all active extents form the activity log
  489. * @mdev: DRBD device.
  490. *
  491. * Removes all active extents form the activity log, waiting until
  492. * the reference count of each entry dropped to 0 first, of course.
  493. *
  494. * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
  495. */
  496. void drbd_al_shrink(struct drbd_conf *mdev)
  497. {
  498. struct lc_element *al_ext;
  499. int i;
  500. D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
  501. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  502. al_ext = lc_element_by_index(mdev->act_log, i);
  503. if (al_ext->lc_number == LC_FREE)
  504. continue;
  505. wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
  506. }
  507. wake_up(&mdev->al_wait);
  508. }
  509. static int w_update_odbm(struct drbd_work *w, int unused)
  510. {
  511. struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
  512. struct drbd_conf *mdev = w->mdev;
  513. if (!get_ldev(mdev)) {
  514. if (__ratelimit(&drbd_ratelimit_state))
  515. dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
  516. kfree(udw);
  517. return 1;
  518. }
  519. drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
  520. put_ldev(mdev);
  521. kfree(udw);
  522. if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
  523. switch (mdev->state.conn) {
  524. case C_SYNC_SOURCE: case C_SYNC_TARGET:
  525. case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
  526. drbd_resync_finished(mdev);
  527. default:
  528. /* nothing to do */
  529. break;
  530. }
  531. }
  532. drbd_bcast_sync_progress(mdev);
  533. return 1;
  534. }
  535. /* ATTENTION. The AL's extents are 4MB each, while the extents in the
  536. * resync LRU-cache are 16MB each.
  537. * The caller of this function has to hold an get_ldev() reference.
  538. *
  539. * TODO will be obsoleted once we have a caching lru of the on disk bitmap
  540. */
  541. static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
  542. int count, int success)
  543. {
  544. struct lc_element *e;
  545. struct update_odbm_work *udw;
  546. unsigned int enr;
  547. D_ASSERT(atomic_read(&mdev->local_cnt));
  548. /* I simply assume that a sector/size pair never crosses
  549. * a 16 MB extent border. (Currently this is true...) */
  550. enr = BM_SECT_TO_EXT(sector);
  551. e = lc_get(mdev->resync, enr);
  552. if (e) {
  553. struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
  554. if (ext->lce.lc_number == enr) {
  555. if (success)
  556. ext->rs_left -= count;
  557. else
  558. ext->rs_failed += count;
  559. if (ext->rs_left < ext->rs_failed) {
  560. dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
  561. "rs_failed=%d count=%d\n",
  562. (unsigned long long)sector,
  563. ext->lce.lc_number, ext->rs_left,
  564. ext->rs_failed, count);
  565. dump_stack();
  566. lc_put(mdev->resync, &ext->lce);
  567. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  568. return;
  569. }
  570. } else {
  571. /* Normally this element should be in the cache,
  572. * since drbd_rs_begin_io() pulled it already in.
  573. *
  574. * But maybe an application write finished, and we set
  575. * something outside the resync lru_cache in sync.
  576. */
  577. int rs_left = drbd_bm_e_weight(mdev, enr);
  578. if (ext->flags != 0) {
  579. dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
  580. " -> %d[%u;00]\n",
  581. ext->lce.lc_number, ext->rs_left,
  582. ext->flags, enr, rs_left);
  583. ext->flags = 0;
  584. }
  585. if (ext->rs_failed) {
  586. dev_warn(DEV, "Kicking resync_lru element enr=%u "
  587. "out with rs_failed=%d\n",
  588. ext->lce.lc_number, ext->rs_failed);
  589. }
  590. ext->rs_left = rs_left;
  591. ext->rs_failed = success ? 0 : count;
  592. lc_changed(mdev->resync, &ext->lce);
  593. }
  594. lc_put(mdev->resync, &ext->lce);
  595. /* no race, we are within the al_lock! */
  596. if (ext->rs_left == ext->rs_failed) {
  597. ext->rs_failed = 0;
  598. udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
  599. if (udw) {
  600. udw->enr = ext->lce.lc_number;
  601. udw->w.cb = w_update_odbm;
  602. udw->w.mdev = mdev;
  603. drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
  604. } else {
  605. dev_warn(DEV, "Could not kmalloc an udw\n");
  606. }
  607. }
  608. } else {
  609. dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
  610. mdev->resync_locked,
  611. mdev->resync->nr_elements,
  612. mdev->resync->flags);
  613. }
  614. }
  615. void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
  616. {
  617. unsigned long now = jiffies;
  618. unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
  619. int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
  620. if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
  621. if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
  622. mdev->state.conn != C_PAUSED_SYNC_T &&
  623. mdev->state.conn != C_PAUSED_SYNC_S) {
  624. mdev->rs_mark_time[next] = now;
  625. mdev->rs_mark_left[next] = still_to_go;
  626. mdev->rs_last_mark = next;
  627. }
  628. }
  629. }
  630. /* clear the bit corresponding to the piece of storage in question:
  631. * size byte of data starting from sector. Only clear a bits of the affected
  632. * one ore more _aligned_ BM_BLOCK_SIZE blocks.
  633. *
  634. * called by worker on C_SYNC_TARGET and receiver on SyncSource.
  635. *
  636. */
  637. void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
  638. const char *file, const unsigned int line)
  639. {
  640. /* Is called from worker and receiver context _only_ */
  641. unsigned long sbnr, ebnr, lbnr;
  642. unsigned long count = 0;
  643. sector_t esector, nr_sectors;
  644. int wake_up = 0;
  645. unsigned long flags;
  646. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  647. dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
  648. (unsigned long long)sector, size);
  649. return;
  650. }
  651. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  652. esector = sector + (size >> 9) - 1;
  653. if (!expect(sector < nr_sectors))
  654. return;
  655. if (!expect(esector < nr_sectors))
  656. esector = nr_sectors - 1;
  657. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  658. /* we clear it (in sync).
  659. * round up start sector, round down end sector. we make sure we only
  660. * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
  661. if (unlikely(esector < BM_SECT_PER_BIT-1))
  662. return;
  663. if (unlikely(esector == (nr_sectors-1)))
  664. ebnr = lbnr;
  665. else
  666. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  667. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  668. if (sbnr > ebnr)
  669. return;
  670. /*
  671. * ok, (capacity & 7) != 0 sometimes, but who cares...
  672. * we count rs_{total,left} in bits, not sectors.
  673. */
  674. count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
  675. if (count && get_ldev(mdev)) {
  676. drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
  677. spin_lock_irqsave(&mdev->al_lock, flags);
  678. drbd_try_clear_on_disk_bm(mdev, sector, count, true);
  679. spin_unlock_irqrestore(&mdev->al_lock, flags);
  680. /* just wake_up unconditional now, various lc_chaged(),
  681. * lc_put() in drbd_try_clear_on_disk_bm(). */
  682. wake_up = 1;
  683. put_ldev(mdev);
  684. }
  685. if (wake_up)
  686. wake_up(&mdev->al_wait);
  687. }
  688. /*
  689. * this is intended to set one request worth of data out of sync.
  690. * affects at least 1 bit,
  691. * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
  692. *
  693. * called by tl_clear and drbd_send_dblock (==drbd_make_request).
  694. * so this can be _any_ process.
  695. */
  696. int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
  697. const char *file, const unsigned int line)
  698. {
  699. unsigned long sbnr, ebnr, lbnr, flags;
  700. sector_t esector, nr_sectors;
  701. unsigned int enr, count = 0;
  702. struct lc_element *e;
  703. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  704. dev_err(DEV, "sector: %llus, size: %d\n",
  705. (unsigned long long)sector, size);
  706. return 0;
  707. }
  708. if (!get_ldev(mdev))
  709. return 0; /* no disk, no metadata, no bitmap to set bits in */
  710. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  711. esector = sector + (size >> 9) - 1;
  712. if (!expect(sector < nr_sectors))
  713. goto out;
  714. if (!expect(esector < nr_sectors))
  715. esector = nr_sectors - 1;
  716. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  717. /* we set it out of sync,
  718. * we do not need to round anything here */
  719. sbnr = BM_SECT_TO_BIT(sector);
  720. ebnr = BM_SECT_TO_BIT(esector);
  721. /* ok, (capacity & 7) != 0 sometimes, but who cares...
  722. * we count rs_{total,left} in bits, not sectors. */
  723. spin_lock_irqsave(&mdev->al_lock, flags);
  724. count = drbd_bm_set_bits(mdev, sbnr, ebnr);
  725. enr = BM_SECT_TO_EXT(sector);
  726. e = lc_find(mdev->resync, enr);
  727. if (e)
  728. lc_entry(e, struct bm_extent, lce)->rs_left += count;
  729. spin_unlock_irqrestore(&mdev->al_lock, flags);
  730. out:
  731. put_ldev(mdev);
  732. return count;
  733. }
  734. static
  735. struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
  736. {
  737. struct lc_element *e;
  738. struct bm_extent *bm_ext;
  739. int wakeup = 0;
  740. unsigned long rs_flags;
  741. spin_lock_irq(&mdev->al_lock);
  742. if (mdev->resync_locked > mdev->resync->nr_elements/2) {
  743. spin_unlock_irq(&mdev->al_lock);
  744. return NULL;
  745. }
  746. e = lc_get(mdev->resync, enr);
  747. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  748. if (bm_ext) {
  749. if (bm_ext->lce.lc_number != enr) {
  750. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  751. bm_ext->rs_failed = 0;
  752. lc_changed(mdev->resync, &bm_ext->lce);
  753. wakeup = 1;
  754. }
  755. if (bm_ext->lce.refcnt == 1)
  756. mdev->resync_locked++;
  757. set_bit(BME_NO_WRITES, &bm_ext->flags);
  758. }
  759. rs_flags = mdev->resync->flags;
  760. spin_unlock_irq(&mdev->al_lock);
  761. if (wakeup)
  762. wake_up(&mdev->al_wait);
  763. if (!bm_ext) {
  764. if (rs_flags & LC_STARVING)
  765. dev_warn(DEV, "Have to wait for element"
  766. " (resync LRU too small?)\n");
  767. BUG_ON(rs_flags & LC_DIRTY);
  768. }
  769. return bm_ext;
  770. }
  771. static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
  772. {
  773. struct lc_element *al_ext;
  774. int rv = 0;
  775. spin_lock_irq(&mdev->al_lock);
  776. if (unlikely(enr == mdev->act_log->new_number))
  777. rv = 1;
  778. else {
  779. al_ext = lc_find(mdev->act_log, enr);
  780. if (al_ext) {
  781. if (al_ext->refcnt)
  782. rv = 1;
  783. }
  784. }
  785. spin_unlock_irq(&mdev->al_lock);
  786. /*
  787. if (unlikely(rv)) {
  788. dev_info(DEV, "Delaying sync read until app's write is done\n");
  789. }
  790. */
  791. return rv;
  792. }
  793. /**
  794. * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
  795. * @mdev: DRBD device.
  796. * @sector: The sector number.
  797. *
  798. * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
  799. */
  800. int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  801. {
  802. unsigned int enr = BM_SECT_TO_EXT(sector);
  803. struct bm_extent *bm_ext;
  804. int i, sig;
  805. int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
  806. 200 times -> 20 seconds. */
  807. retry:
  808. sig = wait_event_interruptible(mdev->al_wait,
  809. (bm_ext = _bme_get(mdev, enr)));
  810. if (sig)
  811. return -EINTR;
  812. if (test_bit(BME_LOCKED, &bm_ext->flags))
  813. return 0;
  814. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  815. sig = wait_event_interruptible(mdev->al_wait,
  816. !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
  817. test_bit(BME_PRIORITY, &bm_ext->flags));
  818. if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
  819. spin_lock_irq(&mdev->al_lock);
  820. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  821. bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
  822. mdev->resync_locked--;
  823. wake_up(&mdev->al_wait);
  824. }
  825. spin_unlock_irq(&mdev->al_lock);
  826. if (sig)
  827. return -EINTR;
  828. if (schedule_timeout_interruptible(HZ/10))
  829. return -EINTR;
  830. if (sa && --sa == 0)
  831. dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
  832. "Resync stalled?\n");
  833. goto retry;
  834. }
  835. }
  836. set_bit(BME_LOCKED, &bm_ext->flags);
  837. return 0;
  838. }
  839. /**
  840. * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
  841. * @mdev: DRBD device.
  842. * @sector: The sector number.
  843. *
  844. * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
  845. * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
  846. * if there is still application IO going on in this area.
  847. */
  848. int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  849. {
  850. unsigned int enr = BM_SECT_TO_EXT(sector);
  851. const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
  852. struct lc_element *e;
  853. struct bm_extent *bm_ext;
  854. int i;
  855. spin_lock_irq(&mdev->al_lock);
  856. if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
  857. /* in case you have very heavy scattered io, it may
  858. * stall the syncer undefined if we give up the ref count
  859. * when we try again and requeue.
  860. *
  861. * if we don't give up the refcount, but the next time
  862. * we are scheduled this extent has been "synced" by new
  863. * application writes, we'd miss the lc_put on the
  864. * extent we keep the refcount on.
  865. * so we remembered which extent we had to try again, and
  866. * if the next requested one is something else, we do
  867. * the lc_put here...
  868. * we also have to wake_up
  869. */
  870. e = lc_find(mdev->resync, mdev->resync_wenr);
  871. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  872. if (bm_ext) {
  873. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  874. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  875. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  876. mdev->resync_wenr = LC_FREE;
  877. if (lc_put(mdev->resync, &bm_ext->lce) == 0)
  878. mdev->resync_locked--;
  879. wake_up(&mdev->al_wait);
  880. } else {
  881. dev_alert(DEV, "LOGIC BUG\n");
  882. }
  883. }
  884. /* TRY. */
  885. e = lc_try_get(mdev->resync, enr);
  886. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  887. if (bm_ext) {
  888. if (test_bit(BME_LOCKED, &bm_ext->flags))
  889. goto proceed;
  890. if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
  891. mdev->resync_locked++;
  892. } else {
  893. /* we did set the BME_NO_WRITES,
  894. * but then could not set BME_LOCKED,
  895. * so we tried again.
  896. * drop the extra reference. */
  897. bm_ext->lce.refcnt--;
  898. D_ASSERT(bm_ext->lce.refcnt > 0);
  899. }
  900. goto check_al;
  901. } else {
  902. /* do we rather want to try later? */
  903. if (mdev->resync_locked > mdev->resync->nr_elements-3)
  904. goto try_again;
  905. /* Do or do not. There is no try. -- Yoda */
  906. e = lc_get(mdev->resync, enr);
  907. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  908. if (!bm_ext) {
  909. const unsigned long rs_flags = mdev->resync->flags;
  910. if (rs_flags & LC_STARVING)
  911. dev_warn(DEV, "Have to wait for element"
  912. " (resync LRU too small?)\n");
  913. BUG_ON(rs_flags & LC_DIRTY);
  914. goto try_again;
  915. }
  916. if (bm_ext->lce.lc_number != enr) {
  917. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  918. bm_ext->rs_failed = 0;
  919. lc_changed(mdev->resync, &bm_ext->lce);
  920. wake_up(&mdev->al_wait);
  921. D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
  922. }
  923. set_bit(BME_NO_WRITES, &bm_ext->flags);
  924. D_ASSERT(bm_ext->lce.refcnt == 1);
  925. mdev->resync_locked++;
  926. goto check_al;
  927. }
  928. check_al:
  929. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  930. if (unlikely(al_enr+i == mdev->act_log->new_number))
  931. goto try_again;
  932. if (lc_is_used(mdev->act_log, al_enr+i))
  933. goto try_again;
  934. }
  935. set_bit(BME_LOCKED, &bm_ext->flags);
  936. proceed:
  937. mdev->resync_wenr = LC_FREE;
  938. spin_unlock_irq(&mdev->al_lock);
  939. return 0;
  940. try_again:
  941. if (bm_ext)
  942. mdev->resync_wenr = enr;
  943. spin_unlock_irq(&mdev->al_lock);
  944. return -EAGAIN;
  945. }
  946. void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
  947. {
  948. unsigned int enr = BM_SECT_TO_EXT(sector);
  949. struct lc_element *e;
  950. struct bm_extent *bm_ext;
  951. unsigned long flags;
  952. spin_lock_irqsave(&mdev->al_lock, flags);
  953. e = lc_find(mdev->resync, enr);
  954. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  955. if (!bm_ext) {
  956. spin_unlock_irqrestore(&mdev->al_lock, flags);
  957. if (__ratelimit(&drbd_ratelimit_state))
  958. dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
  959. return;
  960. }
  961. if (bm_ext->lce.refcnt == 0) {
  962. spin_unlock_irqrestore(&mdev->al_lock, flags);
  963. dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
  964. "but refcnt is 0!?\n",
  965. (unsigned long long)sector, enr);
  966. return;
  967. }
  968. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  969. bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
  970. mdev->resync_locked--;
  971. wake_up(&mdev->al_wait);
  972. }
  973. spin_unlock_irqrestore(&mdev->al_lock, flags);
  974. }
  975. /**
  976. * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
  977. * @mdev: DRBD device.
  978. */
  979. void drbd_rs_cancel_all(struct drbd_conf *mdev)
  980. {
  981. spin_lock_irq(&mdev->al_lock);
  982. if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
  983. lc_reset(mdev->resync);
  984. put_ldev(mdev);
  985. }
  986. mdev->resync_locked = 0;
  987. mdev->resync_wenr = LC_FREE;
  988. spin_unlock_irq(&mdev->al_lock);
  989. wake_up(&mdev->al_wait);
  990. }
  991. /**
  992. * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
  993. * @mdev: DRBD device.
  994. *
  995. * Returns 0 upon success, -EAGAIN if at least one reference count was
  996. * not zero.
  997. */
  998. int drbd_rs_del_all(struct drbd_conf *mdev)
  999. {
  1000. struct lc_element *e;
  1001. struct bm_extent *bm_ext;
  1002. int i;
  1003. spin_lock_irq(&mdev->al_lock);
  1004. if (get_ldev_if_state(mdev, D_FAILED)) {
  1005. /* ok, ->resync is there. */
  1006. for (i = 0; i < mdev->resync->nr_elements; i++) {
  1007. e = lc_element_by_index(mdev->resync, i);
  1008. bm_ext = lc_entry(e, struct bm_extent, lce);
  1009. if (bm_ext->lce.lc_number == LC_FREE)
  1010. continue;
  1011. if (bm_ext->lce.lc_number == mdev->resync_wenr) {
  1012. dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
  1013. " got 'synced' by application io\n",
  1014. mdev->resync_wenr);
  1015. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1016. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  1017. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1018. mdev->resync_wenr = LC_FREE;
  1019. lc_put(mdev->resync, &bm_ext->lce);
  1020. }
  1021. if (bm_ext->lce.refcnt != 0) {
  1022. dev_info(DEV, "Retrying drbd_rs_del_all() later. "
  1023. "refcnt=%d\n", bm_ext->lce.refcnt);
  1024. put_ldev(mdev);
  1025. spin_unlock_irq(&mdev->al_lock);
  1026. return -EAGAIN;
  1027. }
  1028. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1029. D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
  1030. lc_del(mdev->resync, &bm_ext->lce);
  1031. }
  1032. D_ASSERT(mdev->resync->used == 0);
  1033. put_ldev(mdev);
  1034. }
  1035. spin_unlock_irq(&mdev->al_lock);
  1036. return 0;
  1037. }
  1038. /**
  1039. * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
  1040. * @mdev: DRBD device.
  1041. * @sector: The sector number.
  1042. * @size: Size of failed IO operation, in byte.
  1043. */
  1044. void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
  1045. {
  1046. /* Is called from worker and receiver context _only_ */
  1047. unsigned long sbnr, ebnr, lbnr;
  1048. unsigned long count;
  1049. sector_t esector, nr_sectors;
  1050. int wake_up = 0;
  1051. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  1052. dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
  1053. (unsigned long long)sector, size);
  1054. return;
  1055. }
  1056. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  1057. esector = sector + (size >> 9) - 1;
  1058. if (!expect(sector < nr_sectors))
  1059. return;
  1060. if (!expect(esector < nr_sectors))
  1061. esector = nr_sectors - 1;
  1062. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  1063. /*
  1064. * round up start sector, round down end sector. we make sure we only
  1065. * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
  1066. if (unlikely(esector < BM_SECT_PER_BIT-1))
  1067. return;
  1068. if (unlikely(esector == (nr_sectors-1)))
  1069. ebnr = lbnr;
  1070. else
  1071. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  1072. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  1073. if (sbnr > ebnr)
  1074. return;
  1075. /*
  1076. * ok, (capacity & 7) != 0 sometimes, but who cares...
  1077. * we count rs_{total,left} in bits, not sectors.
  1078. */
  1079. spin_lock_irq(&mdev->al_lock);
  1080. count = drbd_bm_count_bits(mdev, sbnr, ebnr);
  1081. if (count) {
  1082. mdev->rs_failed += count;
  1083. if (get_ldev(mdev)) {
  1084. drbd_try_clear_on_disk_bm(mdev, sector, count, false);
  1085. put_ldev(mdev);
  1086. }
  1087. /* just wake_up unconditional now, various lc_chaged(),
  1088. * lc_put() in drbd_try_clear_on_disk_bm(). */
  1089. wake_up = 1;
  1090. }
  1091. spin_unlock_irq(&mdev->al_lock);
  1092. if (wake_up)
  1093. wake_up(&mdev->al_wait);
  1094. }