drbd_actlog.c 34 KB

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