drbd_actlog.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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. /**
  473. * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
  474. * @mdev: DRBD device.
  475. */
  476. void drbd_al_apply_to_bm(struct drbd_conf *mdev)
  477. {
  478. unsigned int enr;
  479. unsigned long add = 0;
  480. char ppb[10];
  481. int i, tmp;
  482. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  483. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  484. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  485. if (enr == LC_FREE)
  486. continue;
  487. tmp = drbd_bm_ALe_set_all(mdev, enr);
  488. dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
  489. add += tmp;
  490. }
  491. lc_unlock(mdev->act_log);
  492. wake_up(&mdev->al_wait);
  493. dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
  494. ppsize(ppb, Bit2KB(add)));
  495. }
  496. static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
  497. {
  498. int rv;
  499. spin_lock_irq(&mdev->al_lock);
  500. rv = (al_ext->refcnt == 0);
  501. if (likely(rv))
  502. lc_del(mdev->act_log, al_ext);
  503. spin_unlock_irq(&mdev->al_lock);
  504. return rv;
  505. }
  506. /**
  507. * drbd_al_shrink() - Removes all active extents form the activity log
  508. * @mdev: DRBD device.
  509. *
  510. * Removes all active extents form the activity log, waiting until
  511. * the reference count of each entry dropped to 0 first, of course.
  512. *
  513. * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
  514. */
  515. void drbd_al_shrink(struct drbd_conf *mdev)
  516. {
  517. struct lc_element *al_ext;
  518. int i;
  519. D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
  520. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  521. al_ext = lc_element_by_index(mdev->act_log, i);
  522. if (al_ext->lc_number == LC_FREE)
  523. continue;
  524. wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
  525. }
  526. wake_up(&mdev->al_wait);
  527. }
  528. static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  529. {
  530. struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
  531. if (!get_ldev(mdev)) {
  532. if (__ratelimit(&drbd_ratelimit_state))
  533. dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
  534. kfree(udw);
  535. return 1;
  536. }
  537. drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
  538. put_ldev(mdev);
  539. kfree(udw);
  540. if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
  541. switch (mdev->state.conn) {
  542. case C_SYNC_SOURCE: case C_SYNC_TARGET:
  543. case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
  544. drbd_resync_finished(mdev);
  545. default:
  546. /* nothing to do */
  547. break;
  548. }
  549. }
  550. drbd_bcast_sync_progress(mdev);
  551. return 1;
  552. }
  553. /* ATTENTION. The AL's extents are 4MB each, while the extents in the
  554. * resync LRU-cache are 16MB each.
  555. * The caller of this function has to hold an get_ldev() reference.
  556. *
  557. * TODO will be obsoleted once we have a caching lru of the on disk bitmap
  558. */
  559. static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
  560. int count, int success)
  561. {
  562. struct lc_element *e;
  563. struct update_odbm_work *udw;
  564. unsigned int enr;
  565. D_ASSERT(atomic_read(&mdev->local_cnt));
  566. /* I simply assume that a sector/size pair never crosses
  567. * a 16 MB extent border. (Currently this is true...) */
  568. enr = BM_SECT_TO_EXT(sector);
  569. e = lc_get(mdev->resync, enr);
  570. if (e) {
  571. struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
  572. if (ext->lce.lc_number == enr) {
  573. if (success)
  574. ext->rs_left -= count;
  575. else
  576. ext->rs_failed += count;
  577. if (ext->rs_left < ext->rs_failed) {
  578. dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
  579. "rs_failed=%d count=%d\n",
  580. (unsigned long long)sector,
  581. ext->lce.lc_number, ext->rs_left,
  582. ext->rs_failed, count);
  583. dump_stack();
  584. lc_put(mdev->resync, &ext->lce);
  585. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  586. return;
  587. }
  588. } else {
  589. /* Normally this element should be in the cache,
  590. * since drbd_rs_begin_io() pulled it already in.
  591. *
  592. * But maybe an application write finished, and we set
  593. * something outside the resync lru_cache in sync.
  594. */
  595. int rs_left = drbd_bm_e_weight(mdev, enr);
  596. if (ext->flags != 0) {
  597. dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
  598. " -> %d[%u;00]\n",
  599. ext->lce.lc_number, ext->rs_left,
  600. ext->flags, enr, rs_left);
  601. ext->flags = 0;
  602. }
  603. if (ext->rs_failed) {
  604. dev_warn(DEV, "Kicking resync_lru element enr=%u "
  605. "out with rs_failed=%d\n",
  606. ext->lce.lc_number, ext->rs_failed);
  607. }
  608. ext->rs_left = rs_left;
  609. ext->rs_failed = success ? 0 : count;
  610. lc_changed(mdev->resync, &ext->lce);
  611. }
  612. lc_put(mdev->resync, &ext->lce);
  613. /* no race, we are within the al_lock! */
  614. if (ext->rs_left == ext->rs_failed) {
  615. ext->rs_failed = 0;
  616. udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
  617. if (udw) {
  618. udw->enr = ext->lce.lc_number;
  619. udw->w.cb = w_update_odbm;
  620. drbd_queue_work_front(&mdev->data.work, &udw->w);
  621. } else {
  622. dev_warn(DEV, "Could not kmalloc an udw\n");
  623. }
  624. }
  625. } else {
  626. dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
  627. mdev->resync_locked,
  628. mdev->resync->nr_elements,
  629. mdev->resync->flags);
  630. }
  631. }
  632. void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
  633. {
  634. unsigned long now = jiffies;
  635. unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
  636. int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
  637. if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
  638. if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
  639. mdev->state.conn != C_PAUSED_SYNC_T &&
  640. mdev->state.conn != C_PAUSED_SYNC_S) {
  641. mdev->rs_mark_time[next] = now;
  642. mdev->rs_mark_left[next] = still_to_go;
  643. mdev->rs_last_mark = next;
  644. }
  645. }
  646. }
  647. /* clear the bit corresponding to the piece of storage in question:
  648. * size byte of data starting from sector. Only clear a bits of the affected
  649. * one ore more _aligned_ BM_BLOCK_SIZE blocks.
  650. *
  651. * called by worker on C_SYNC_TARGET and receiver on SyncSource.
  652. *
  653. */
  654. void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
  655. const char *file, const unsigned int line)
  656. {
  657. /* Is called from worker and receiver context _only_ */
  658. unsigned long sbnr, ebnr, lbnr;
  659. unsigned long count = 0;
  660. sector_t esector, nr_sectors;
  661. int wake_up = 0;
  662. unsigned long flags;
  663. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  664. dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
  665. (unsigned long long)sector, size);
  666. return;
  667. }
  668. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  669. esector = sector + (size >> 9) - 1;
  670. ERR_IF(sector >= nr_sectors) return;
  671. ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
  672. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  673. /* we clear it (in sync).
  674. * round up start sector, round down end sector. we make sure we only
  675. * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
  676. if (unlikely(esector < BM_SECT_PER_BIT-1))
  677. return;
  678. if (unlikely(esector == (nr_sectors-1)))
  679. ebnr = lbnr;
  680. else
  681. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  682. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  683. if (sbnr > ebnr)
  684. return;
  685. /*
  686. * ok, (capacity & 7) != 0 sometimes, but who cares...
  687. * we count rs_{total,left} in bits, not sectors.
  688. */
  689. count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
  690. if (count && get_ldev(mdev)) {
  691. drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
  692. spin_lock_irqsave(&mdev->al_lock, flags);
  693. drbd_try_clear_on_disk_bm(mdev, sector, count, true);
  694. spin_unlock_irqrestore(&mdev->al_lock, flags);
  695. /* just wake_up unconditional now, various lc_chaged(),
  696. * lc_put() in drbd_try_clear_on_disk_bm(). */
  697. wake_up = 1;
  698. put_ldev(mdev);
  699. }
  700. if (wake_up)
  701. wake_up(&mdev->al_wait);
  702. }
  703. /*
  704. * this is intended to set one request worth of data out of sync.
  705. * affects at least 1 bit,
  706. * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
  707. *
  708. * called by tl_clear and drbd_send_dblock (==drbd_make_request).
  709. * so this can be _any_ process.
  710. */
  711. int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
  712. const char *file, const unsigned int line)
  713. {
  714. unsigned long sbnr, ebnr, lbnr, flags;
  715. sector_t esector, nr_sectors;
  716. unsigned int enr, count = 0;
  717. struct lc_element *e;
  718. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  719. dev_err(DEV, "sector: %llus, size: %d\n",
  720. (unsigned long long)sector, size);
  721. return 0;
  722. }
  723. if (!get_ldev(mdev))
  724. return 0; /* no disk, no metadata, no bitmap to set bits in */
  725. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  726. esector = sector + (size >> 9) - 1;
  727. ERR_IF(sector >= nr_sectors)
  728. goto out;
  729. ERR_IF(esector >= nr_sectors)
  730. esector = (nr_sectors-1);
  731. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  732. /* we set it out of sync,
  733. * we do not need to round anything here */
  734. sbnr = BM_SECT_TO_BIT(sector);
  735. ebnr = BM_SECT_TO_BIT(esector);
  736. /* ok, (capacity & 7) != 0 sometimes, but who cares...
  737. * we count rs_{total,left} in bits, not sectors. */
  738. spin_lock_irqsave(&mdev->al_lock, flags);
  739. count = drbd_bm_set_bits(mdev, sbnr, ebnr);
  740. enr = BM_SECT_TO_EXT(sector);
  741. e = lc_find(mdev->resync, enr);
  742. if (e)
  743. lc_entry(e, struct bm_extent, lce)->rs_left += count;
  744. spin_unlock_irqrestore(&mdev->al_lock, flags);
  745. out:
  746. put_ldev(mdev);
  747. return count;
  748. }
  749. static
  750. struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
  751. {
  752. struct lc_element *e;
  753. struct bm_extent *bm_ext;
  754. int wakeup = 0;
  755. unsigned long rs_flags;
  756. spin_lock_irq(&mdev->al_lock);
  757. if (mdev->resync_locked > mdev->resync->nr_elements/2) {
  758. spin_unlock_irq(&mdev->al_lock);
  759. return NULL;
  760. }
  761. e = lc_get(mdev->resync, enr);
  762. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  763. if (bm_ext) {
  764. if (bm_ext->lce.lc_number != enr) {
  765. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  766. bm_ext->rs_failed = 0;
  767. lc_changed(mdev->resync, &bm_ext->lce);
  768. wakeup = 1;
  769. }
  770. if (bm_ext->lce.refcnt == 1)
  771. mdev->resync_locked++;
  772. set_bit(BME_NO_WRITES, &bm_ext->flags);
  773. }
  774. rs_flags = mdev->resync->flags;
  775. spin_unlock_irq(&mdev->al_lock);
  776. if (wakeup)
  777. wake_up(&mdev->al_wait);
  778. if (!bm_ext) {
  779. if (rs_flags & LC_STARVING)
  780. dev_warn(DEV, "Have to wait for element"
  781. " (resync LRU too small?)\n");
  782. BUG_ON(rs_flags & LC_DIRTY);
  783. }
  784. return bm_ext;
  785. }
  786. static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
  787. {
  788. struct lc_element *al_ext;
  789. int rv = 0;
  790. spin_lock_irq(&mdev->al_lock);
  791. if (unlikely(enr == mdev->act_log->new_number))
  792. rv = 1;
  793. else {
  794. al_ext = lc_find(mdev->act_log, enr);
  795. if (al_ext) {
  796. if (al_ext->refcnt)
  797. rv = 1;
  798. }
  799. }
  800. spin_unlock_irq(&mdev->al_lock);
  801. /*
  802. if (unlikely(rv)) {
  803. dev_info(DEV, "Delaying sync read until app's write is done\n");
  804. }
  805. */
  806. return rv;
  807. }
  808. /**
  809. * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
  810. * @mdev: DRBD device.
  811. * @sector: The sector number.
  812. *
  813. * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
  814. */
  815. int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  816. {
  817. unsigned int enr = BM_SECT_TO_EXT(sector);
  818. struct bm_extent *bm_ext;
  819. int i, sig;
  820. int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
  821. 200 times -> 20 seconds. */
  822. retry:
  823. sig = wait_event_interruptible(mdev->al_wait,
  824. (bm_ext = _bme_get(mdev, enr)));
  825. if (sig)
  826. return -EINTR;
  827. if (test_bit(BME_LOCKED, &bm_ext->flags))
  828. return 0;
  829. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  830. sig = wait_event_interruptible(mdev->al_wait,
  831. !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
  832. test_bit(BME_PRIORITY, &bm_ext->flags));
  833. if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
  834. spin_lock_irq(&mdev->al_lock);
  835. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  836. bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
  837. mdev->resync_locked--;
  838. wake_up(&mdev->al_wait);
  839. }
  840. spin_unlock_irq(&mdev->al_lock);
  841. if (sig)
  842. return -EINTR;
  843. if (schedule_timeout_interruptible(HZ/10))
  844. return -EINTR;
  845. if (sa && --sa == 0)
  846. dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
  847. "Resync stalled?\n");
  848. goto retry;
  849. }
  850. }
  851. set_bit(BME_LOCKED, &bm_ext->flags);
  852. return 0;
  853. }
  854. /**
  855. * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
  856. * @mdev: DRBD device.
  857. * @sector: The sector number.
  858. *
  859. * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
  860. * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
  861. * if there is still application IO going on in this area.
  862. */
  863. int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  864. {
  865. unsigned int enr = BM_SECT_TO_EXT(sector);
  866. const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
  867. struct lc_element *e;
  868. struct bm_extent *bm_ext;
  869. int i;
  870. spin_lock_irq(&mdev->al_lock);
  871. if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
  872. /* in case you have very heavy scattered io, it may
  873. * stall the syncer undefined if we give up the ref count
  874. * when we try again and requeue.
  875. *
  876. * if we don't give up the refcount, but the next time
  877. * we are scheduled this extent has been "synced" by new
  878. * application writes, we'd miss the lc_put on the
  879. * extent we keep the refcount on.
  880. * so we remembered which extent we had to try again, and
  881. * if the next requested one is something else, we do
  882. * the lc_put here...
  883. * we also have to wake_up
  884. */
  885. e = lc_find(mdev->resync, mdev->resync_wenr);
  886. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  887. if (bm_ext) {
  888. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  889. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  890. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  891. mdev->resync_wenr = LC_FREE;
  892. if (lc_put(mdev->resync, &bm_ext->lce) == 0)
  893. mdev->resync_locked--;
  894. wake_up(&mdev->al_wait);
  895. } else {
  896. dev_alert(DEV, "LOGIC BUG\n");
  897. }
  898. }
  899. /* TRY. */
  900. e = lc_try_get(mdev->resync, enr);
  901. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  902. if (bm_ext) {
  903. if (test_bit(BME_LOCKED, &bm_ext->flags))
  904. goto proceed;
  905. if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
  906. mdev->resync_locked++;
  907. } else {
  908. /* we did set the BME_NO_WRITES,
  909. * but then could not set BME_LOCKED,
  910. * so we tried again.
  911. * drop the extra reference. */
  912. bm_ext->lce.refcnt--;
  913. D_ASSERT(bm_ext->lce.refcnt > 0);
  914. }
  915. goto check_al;
  916. } else {
  917. /* do we rather want to try later? */
  918. if (mdev->resync_locked > mdev->resync->nr_elements-3)
  919. goto try_again;
  920. /* Do or do not. There is no try. -- Yoda */
  921. e = lc_get(mdev->resync, enr);
  922. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  923. if (!bm_ext) {
  924. const unsigned long rs_flags = mdev->resync->flags;
  925. if (rs_flags & LC_STARVING)
  926. dev_warn(DEV, "Have to wait for element"
  927. " (resync LRU too small?)\n");
  928. BUG_ON(rs_flags & LC_DIRTY);
  929. goto try_again;
  930. }
  931. if (bm_ext->lce.lc_number != enr) {
  932. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  933. bm_ext->rs_failed = 0;
  934. lc_changed(mdev->resync, &bm_ext->lce);
  935. wake_up(&mdev->al_wait);
  936. D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
  937. }
  938. set_bit(BME_NO_WRITES, &bm_ext->flags);
  939. D_ASSERT(bm_ext->lce.refcnt == 1);
  940. mdev->resync_locked++;
  941. goto check_al;
  942. }
  943. check_al:
  944. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  945. if (unlikely(al_enr+i == mdev->act_log->new_number))
  946. goto try_again;
  947. if (lc_is_used(mdev->act_log, al_enr+i))
  948. goto try_again;
  949. }
  950. set_bit(BME_LOCKED, &bm_ext->flags);
  951. proceed:
  952. mdev->resync_wenr = LC_FREE;
  953. spin_unlock_irq(&mdev->al_lock);
  954. return 0;
  955. try_again:
  956. if (bm_ext)
  957. mdev->resync_wenr = enr;
  958. spin_unlock_irq(&mdev->al_lock);
  959. return -EAGAIN;
  960. }
  961. void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
  962. {
  963. unsigned int enr = BM_SECT_TO_EXT(sector);
  964. struct lc_element *e;
  965. struct bm_extent *bm_ext;
  966. unsigned long flags;
  967. spin_lock_irqsave(&mdev->al_lock, flags);
  968. e = lc_find(mdev->resync, enr);
  969. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  970. if (!bm_ext) {
  971. spin_unlock_irqrestore(&mdev->al_lock, flags);
  972. if (__ratelimit(&drbd_ratelimit_state))
  973. dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
  974. return;
  975. }
  976. if (bm_ext->lce.refcnt == 0) {
  977. spin_unlock_irqrestore(&mdev->al_lock, flags);
  978. dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
  979. "but refcnt is 0!?\n",
  980. (unsigned long long)sector, enr);
  981. return;
  982. }
  983. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  984. bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
  985. mdev->resync_locked--;
  986. wake_up(&mdev->al_wait);
  987. }
  988. spin_unlock_irqrestore(&mdev->al_lock, flags);
  989. }
  990. /**
  991. * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
  992. * @mdev: DRBD device.
  993. */
  994. void drbd_rs_cancel_all(struct drbd_conf *mdev)
  995. {
  996. spin_lock_irq(&mdev->al_lock);
  997. if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
  998. lc_reset(mdev->resync);
  999. put_ldev(mdev);
  1000. }
  1001. mdev->resync_locked = 0;
  1002. mdev->resync_wenr = LC_FREE;
  1003. spin_unlock_irq(&mdev->al_lock);
  1004. wake_up(&mdev->al_wait);
  1005. }
  1006. /**
  1007. * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
  1008. * @mdev: DRBD device.
  1009. *
  1010. * Returns 0 upon success, -EAGAIN if at least one reference count was
  1011. * not zero.
  1012. */
  1013. int drbd_rs_del_all(struct drbd_conf *mdev)
  1014. {
  1015. struct lc_element *e;
  1016. struct bm_extent *bm_ext;
  1017. int i;
  1018. spin_lock_irq(&mdev->al_lock);
  1019. if (get_ldev_if_state(mdev, D_FAILED)) {
  1020. /* ok, ->resync is there. */
  1021. for (i = 0; i < mdev->resync->nr_elements; i++) {
  1022. e = lc_element_by_index(mdev->resync, i);
  1023. bm_ext = lc_entry(e, struct bm_extent, lce);
  1024. if (bm_ext->lce.lc_number == LC_FREE)
  1025. continue;
  1026. if (bm_ext->lce.lc_number == mdev->resync_wenr) {
  1027. dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
  1028. " got 'synced' by application io\n",
  1029. mdev->resync_wenr);
  1030. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1031. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  1032. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1033. mdev->resync_wenr = LC_FREE;
  1034. lc_put(mdev->resync, &bm_ext->lce);
  1035. }
  1036. if (bm_ext->lce.refcnt != 0) {
  1037. dev_info(DEV, "Retrying drbd_rs_del_all() later. "
  1038. "refcnt=%d\n", bm_ext->lce.refcnt);
  1039. put_ldev(mdev);
  1040. spin_unlock_irq(&mdev->al_lock);
  1041. return -EAGAIN;
  1042. }
  1043. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1044. D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
  1045. lc_del(mdev->resync, &bm_ext->lce);
  1046. }
  1047. D_ASSERT(mdev->resync->used == 0);
  1048. put_ldev(mdev);
  1049. }
  1050. spin_unlock_irq(&mdev->al_lock);
  1051. return 0;
  1052. }
  1053. /**
  1054. * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
  1055. * @mdev: DRBD device.
  1056. * @sector: The sector number.
  1057. * @size: Size of failed IO operation, in byte.
  1058. */
  1059. void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
  1060. {
  1061. /* Is called from worker and receiver context _only_ */
  1062. unsigned long sbnr, ebnr, lbnr;
  1063. unsigned long count;
  1064. sector_t esector, nr_sectors;
  1065. int wake_up = 0;
  1066. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  1067. dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
  1068. (unsigned long long)sector, size);
  1069. return;
  1070. }
  1071. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  1072. esector = sector + (size >> 9) - 1;
  1073. ERR_IF(sector >= nr_sectors) return;
  1074. ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
  1075. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  1076. /*
  1077. * round up start sector, round down end sector. we make sure we only
  1078. * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
  1079. if (unlikely(esector < BM_SECT_PER_BIT-1))
  1080. return;
  1081. if (unlikely(esector == (nr_sectors-1)))
  1082. ebnr = lbnr;
  1083. else
  1084. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  1085. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  1086. if (sbnr > ebnr)
  1087. return;
  1088. /*
  1089. * ok, (capacity & 7) != 0 sometimes, but who cares...
  1090. * we count rs_{total,left} in bits, not sectors.
  1091. */
  1092. spin_lock_irq(&mdev->al_lock);
  1093. count = drbd_bm_count_bits(mdev, sbnr, ebnr);
  1094. if (count) {
  1095. mdev->rs_failed += count;
  1096. if (get_ldev(mdev)) {
  1097. drbd_try_clear_on_disk_bm(mdev, sector, count, false);
  1098. put_ldev(mdev);
  1099. }
  1100. /* just wake_up unconditional now, various lc_chaged(),
  1101. * lc_put() in drbd_try_clear_on_disk_bm(). */
  1102. wake_up = 1;
  1103. }
  1104. spin_unlock_irq(&mdev->al_lock);
  1105. if (wake_up)
  1106. wake_up(&mdev->al_wait);
  1107. }