drbd_actlog.c 36 KB

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