drbd_actlog.c 35 KB

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