drbd_actlog.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /*
  2. drbd_actlog.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/drbd.h>
  21. #include "drbd_int.h"
  22. #include "drbd_wrappers.h"
  23. /* We maintain a trivial check sum in our on disk activity log.
  24. * With that we can ensure correct operation even when the storage
  25. * device might do a partial (last) sector write while loosing power.
  26. */
  27. struct __packed al_transaction {
  28. u32 magic;
  29. u32 tr_number;
  30. struct __packed {
  31. u32 pos;
  32. u32 extent; } updates[1 + AL_EXTENTS_PT];
  33. u32 xor_sum;
  34. };
  35. struct update_odbm_work {
  36. struct drbd_work w;
  37. unsigned int enr;
  38. };
  39. struct update_al_work {
  40. struct drbd_work w;
  41. struct lc_element *al_ext;
  42. struct completion event;
  43. unsigned int enr;
  44. /* if old_enr != LC_FREE, write corresponding bitmap sector, too */
  45. unsigned int old_enr;
  46. };
  47. struct drbd_atodb_wait {
  48. atomic_t count;
  49. struct completion io_done;
  50. struct drbd_conf *mdev;
  51. int error;
  52. };
  53. int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
  54. static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
  55. struct drbd_backing_dev *bdev,
  56. struct page *page, sector_t sector,
  57. int rw, int size)
  58. {
  59. struct bio *bio;
  60. struct drbd_md_io md_io;
  61. int ok;
  62. md_io.mdev = mdev;
  63. init_completion(&md_io.event);
  64. md_io.error = 0;
  65. if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags))
  66. rw |= (1 << BIO_RW_BARRIER);
  67. rw |= ((1<<BIO_RW_UNPLUG) | (1<<BIO_RW_SYNCIO));
  68. retry:
  69. bio = bio_alloc(GFP_NOIO, 1);
  70. bio->bi_bdev = bdev->md_bdev;
  71. bio->bi_sector = sector;
  72. ok = (bio_add_page(bio, page, size, 0) == size);
  73. if (!ok)
  74. goto out;
  75. bio->bi_private = &md_io;
  76. bio->bi_end_io = drbd_md_io_complete;
  77. bio->bi_rw = rw;
  78. if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
  79. bio_endio(bio, -EIO);
  80. else
  81. submit_bio(rw, bio);
  82. wait_for_completion(&md_io.event);
  83. ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
  84. /* check for unsupported barrier op.
  85. * would rather check on EOPNOTSUPP, but that is not reliable.
  86. * don't try again for ANY return value != 0 */
  87. if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && !ok)) {
  88. /* Try again with no barrier */
  89. dev_warn(DEV, "Barriers not supported on meta data device - disabling\n");
  90. set_bit(MD_NO_BARRIER, &mdev->flags);
  91. rw &= ~(1 << BIO_RW_BARRIER);
  92. bio_put(bio);
  93. goto retry;
  94. }
  95. out:
  96. bio_put(bio);
  97. return ok;
  98. }
  99. int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
  100. sector_t sector, int rw)
  101. {
  102. int logical_block_size, mask, ok;
  103. int offset = 0;
  104. struct page *iop = mdev->md_io_page;
  105. D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
  106. BUG_ON(!bdev->md_bdev);
  107. logical_block_size = bdev_logical_block_size(bdev->md_bdev);
  108. if (logical_block_size == 0)
  109. logical_block_size = MD_SECTOR_SIZE;
  110. /* in case logical_block_size != 512 [ s390 only? ] */
  111. if (logical_block_size != MD_SECTOR_SIZE) {
  112. mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
  113. D_ASSERT(mask == 1 || mask == 3 || mask == 7);
  114. D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
  115. offset = sector & mask;
  116. sector = sector & ~mask;
  117. iop = mdev->md_io_tmpp;
  118. if (rw & WRITE) {
  119. /* these are GFP_KERNEL pages, pre-allocated
  120. * on device initialization */
  121. void *p = page_address(mdev->md_io_page);
  122. void *hp = page_address(mdev->md_io_tmpp);
  123. ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
  124. READ, logical_block_size);
  125. if (unlikely(!ok)) {
  126. dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
  127. "READ [logical_block_size!=512]) failed!\n",
  128. (unsigned long long)sector);
  129. return 0;
  130. }
  131. memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
  132. }
  133. }
  134. if (sector < drbd_md_first_sector(bdev) ||
  135. sector > drbd_md_last_sector(bdev))
  136. dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
  137. current->comm, current->pid, __func__,
  138. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  139. ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
  140. if (unlikely(!ok)) {
  141. dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
  142. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  143. return 0;
  144. }
  145. if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
  146. void *p = page_address(mdev->md_io_page);
  147. void *hp = page_address(mdev->md_io_tmpp);
  148. memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
  149. }
  150. return ok;
  151. }
  152. static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
  153. {
  154. struct lc_element *al_ext;
  155. struct lc_element *tmp;
  156. unsigned long al_flags = 0;
  157. spin_lock_irq(&mdev->al_lock);
  158. tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
  159. if (unlikely(tmp != NULL)) {
  160. struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
  161. if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
  162. spin_unlock_irq(&mdev->al_lock);
  163. return NULL;
  164. }
  165. }
  166. al_ext = lc_get(mdev->act_log, enr);
  167. al_flags = mdev->act_log->flags;
  168. spin_unlock_irq(&mdev->al_lock);
  169. /*
  170. if (!al_ext) {
  171. if (al_flags & LC_STARVING)
  172. dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
  173. if (al_flags & LC_DIRTY)
  174. dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
  175. }
  176. */
  177. return al_ext;
  178. }
  179. void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
  180. {
  181. unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
  182. struct lc_element *al_ext;
  183. struct update_al_work al_work;
  184. D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
  185. wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
  186. if (al_ext->lc_number != enr) {
  187. /* drbd_al_write_transaction(mdev,al_ext,enr);
  188. * recurses into generic_make_request(), which
  189. * disallows recursion, bios being serialized on the
  190. * current->bio_tail list now.
  191. * we have to delegate updates to the activity log
  192. * to the worker thread. */
  193. init_completion(&al_work.event);
  194. al_work.al_ext = al_ext;
  195. al_work.enr = enr;
  196. al_work.old_enr = al_ext->lc_number;
  197. al_work.w.cb = w_al_write_transaction;
  198. drbd_queue_work_front(&mdev->data.work, &al_work.w);
  199. wait_for_completion(&al_work.event);
  200. mdev->al_writ_cnt++;
  201. spin_lock_irq(&mdev->al_lock);
  202. lc_changed(mdev->act_log, al_ext);
  203. spin_unlock_irq(&mdev->al_lock);
  204. wake_up(&mdev->al_wait);
  205. }
  206. }
  207. void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
  208. {
  209. unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
  210. struct lc_element *extent;
  211. unsigned long flags;
  212. spin_lock_irqsave(&mdev->al_lock, flags);
  213. extent = lc_find(mdev->act_log, enr);
  214. if (!extent) {
  215. spin_unlock_irqrestore(&mdev->al_lock, flags);
  216. dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
  217. return;
  218. }
  219. if (lc_put(mdev->act_log, extent) == 0)
  220. wake_up(&mdev->al_wait);
  221. spin_unlock_irqrestore(&mdev->al_lock, flags);
  222. }
  223. int
  224. w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  225. {
  226. struct update_al_work *aw = container_of(w, struct update_al_work, w);
  227. struct lc_element *updated = aw->al_ext;
  228. const unsigned int new_enr = aw->enr;
  229. const unsigned int evicted = aw->old_enr;
  230. struct al_transaction *buffer;
  231. sector_t sector;
  232. int i, n, mx;
  233. unsigned int extent_nr;
  234. u32 xor_sum = 0;
  235. if (!get_ldev(mdev)) {
  236. dev_err(DEV, "get_ldev() failed in w_al_write_transaction\n");
  237. complete(&((struct update_al_work *)w)->event);
  238. return 1;
  239. }
  240. /* do we have to do a bitmap write, first?
  241. * TODO reduce maximum latency:
  242. * submit both bios, then wait for both,
  243. * instead of doing two synchronous sector writes. */
  244. if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
  245. drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
  246. mutex_lock(&mdev->md_io_mutex); /* protects md_io_page, al_tr_cycle, ... */
  247. buffer = (struct al_transaction *)page_address(mdev->md_io_page);
  248. buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
  249. buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
  250. n = lc_index_of(mdev->act_log, updated);
  251. buffer->updates[0].pos = cpu_to_be32(n);
  252. buffer->updates[0].extent = cpu_to_be32(new_enr);
  253. xor_sum ^= new_enr;
  254. mx = min_t(int, AL_EXTENTS_PT,
  255. mdev->act_log->nr_elements - mdev->al_tr_cycle);
  256. for (i = 0; i < mx; i++) {
  257. unsigned idx = mdev->al_tr_cycle + i;
  258. extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
  259. buffer->updates[i+1].pos = cpu_to_be32(idx);
  260. buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
  261. xor_sum ^= extent_nr;
  262. }
  263. for (; i < AL_EXTENTS_PT; i++) {
  264. buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
  265. buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
  266. xor_sum ^= LC_FREE;
  267. }
  268. mdev->al_tr_cycle += AL_EXTENTS_PT;
  269. if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
  270. mdev->al_tr_cycle = 0;
  271. buffer->xor_sum = cpu_to_be32(xor_sum);
  272. sector = mdev->ldev->md.md_offset
  273. + mdev->ldev->md.al_offset + mdev->al_tr_pos;
  274. if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
  275. drbd_chk_io_error(mdev, 1, TRUE);
  276. if (++mdev->al_tr_pos >
  277. div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
  278. mdev->al_tr_pos = 0;
  279. D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
  280. mdev->al_tr_number++;
  281. mutex_unlock(&mdev->md_io_mutex);
  282. complete(&((struct update_al_work *)w)->event);
  283. put_ldev(mdev);
  284. return 1;
  285. }
  286. /**
  287. * drbd_al_read_tr() - Read a single transaction from the on disk activity log
  288. * @mdev: DRBD device.
  289. * @bdev: Block device to read form.
  290. * @b: pointer to an al_transaction.
  291. * @index: On disk slot of the transaction to read.
  292. *
  293. * Returns -1 on IO error, 0 on checksum error and 1 upon success.
  294. */
  295. static int drbd_al_read_tr(struct drbd_conf *mdev,
  296. struct drbd_backing_dev *bdev,
  297. struct al_transaction *b,
  298. int index)
  299. {
  300. sector_t sector;
  301. int rv, i;
  302. u32 xor_sum = 0;
  303. sector = bdev->md.md_offset + bdev->md.al_offset + index;
  304. /* Dont process error normally,
  305. * as this is done before disk is attached! */
  306. if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
  307. return -1;
  308. rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
  309. for (i = 0; i < AL_EXTENTS_PT + 1; i++)
  310. xor_sum ^= be32_to_cpu(b->updates[i].extent);
  311. rv &= (xor_sum == be32_to_cpu(b->xor_sum));
  312. return rv;
  313. }
  314. /**
  315. * drbd_al_read_log() - Restores the activity log from its on disk representation.
  316. * @mdev: DRBD device.
  317. * @bdev: Block device to read form.
  318. *
  319. * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
  320. */
  321. int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  322. {
  323. struct al_transaction *buffer;
  324. int i;
  325. int rv;
  326. int mx;
  327. int active_extents = 0;
  328. int transactions = 0;
  329. int found_valid = 0;
  330. int from = 0;
  331. int to = 0;
  332. u32 from_tnr = 0;
  333. u32 to_tnr = 0;
  334. u32 cnr;
  335. mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
  336. /* lock out all other meta data io for now,
  337. * and make sure the page is mapped.
  338. */
  339. mutex_lock(&mdev->md_io_mutex);
  340. buffer = page_address(mdev->md_io_page);
  341. /* Find the valid transaction in the log */
  342. for (i = 0; i <= mx; i++) {
  343. rv = drbd_al_read_tr(mdev, bdev, buffer, i);
  344. if (rv == 0)
  345. continue;
  346. if (rv == -1) {
  347. mutex_unlock(&mdev->md_io_mutex);
  348. return 0;
  349. }
  350. cnr = be32_to_cpu(buffer->tr_number);
  351. if (++found_valid == 1) {
  352. from = i;
  353. to = i;
  354. from_tnr = cnr;
  355. to_tnr = cnr;
  356. continue;
  357. }
  358. if ((int)cnr - (int)from_tnr < 0) {
  359. D_ASSERT(from_tnr - cnr + i - from == mx+1);
  360. from = i;
  361. from_tnr = cnr;
  362. }
  363. if ((int)cnr - (int)to_tnr > 0) {
  364. D_ASSERT(cnr - to_tnr == i - to);
  365. to = i;
  366. to_tnr = cnr;
  367. }
  368. }
  369. if (!found_valid) {
  370. dev_warn(DEV, "No usable activity log found.\n");
  371. mutex_unlock(&mdev->md_io_mutex);
  372. return 1;
  373. }
  374. /* Read the valid transactions.
  375. * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
  376. i = from;
  377. while (1) {
  378. int j, pos;
  379. unsigned int extent_nr;
  380. unsigned int trn;
  381. rv = drbd_al_read_tr(mdev, bdev, buffer, i);
  382. ERR_IF(rv == 0) goto cancel;
  383. if (rv == -1) {
  384. mutex_unlock(&mdev->md_io_mutex);
  385. return 0;
  386. }
  387. trn = be32_to_cpu(buffer->tr_number);
  388. spin_lock_irq(&mdev->al_lock);
  389. /* This loop runs backwards because in the cyclic
  390. elements there might be an old version of the
  391. updated element (in slot 0). So the element in slot 0
  392. can overwrite old versions. */
  393. for (j = AL_EXTENTS_PT; j >= 0; j--) {
  394. pos = be32_to_cpu(buffer->updates[j].pos);
  395. extent_nr = be32_to_cpu(buffer->updates[j].extent);
  396. if (extent_nr == LC_FREE)
  397. continue;
  398. lc_set(mdev->act_log, extent_nr, pos);
  399. active_extents++;
  400. }
  401. spin_unlock_irq(&mdev->al_lock);
  402. transactions++;
  403. cancel:
  404. if (i == to)
  405. break;
  406. i++;
  407. if (i > mx)
  408. i = 0;
  409. }
  410. mdev->al_tr_number = to_tnr+1;
  411. mdev->al_tr_pos = to;
  412. if (++mdev->al_tr_pos >
  413. div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
  414. mdev->al_tr_pos = 0;
  415. /* ok, we are done with it */
  416. mutex_unlock(&mdev->md_io_mutex);
  417. dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
  418. transactions, active_extents);
  419. return 1;
  420. }
  421. static void atodb_endio(struct bio *bio, int error)
  422. {
  423. struct drbd_atodb_wait *wc = bio->bi_private;
  424. struct drbd_conf *mdev = wc->mdev;
  425. struct page *page;
  426. int uptodate = bio_flagged(bio, BIO_UPTODATE);
  427. /* strange behavior of some lower level drivers...
  428. * fail the request by clearing the uptodate flag,
  429. * but do not return any error?! */
  430. if (!error && !uptodate)
  431. error = -EIO;
  432. drbd_chk_io_error(mdev, error, TRUE);
  433. if (error && wc->error == 0)
  434. wc->error = error;
  435. if (atomic_dec_and_test(&wc->count))
  436. complete(&wc->io_done);
  437. page = bio->bi_io_vec[0].bv_page;
  438. put_page(page);
  439. bio_put(bio);
  440. mdev->bm_writ_cnt++;
  441. put_ldev(mdev);
  442. }
  443. #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
  444. /* activity log to on disk bitmap -- prepare bio unless that sector
  445. * is already covered by previously prepared bios */
  446. static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
  447. struct bio **bios,
  448. unsigned int enr,
  449. struct drbd_atodb_wait *wc) __must_hold(local)
  450. {
  451. struct bio *bio;
  452. struct page *page;
  453. sector_t on_disk_sector = enr + mdev->ldev->md.md_offset
  454. + mdev->ldev->md.bm_offset;
  455. unsigned int page_offset = PAGE_SIZE;
  456. int offset;
  457. int i = 0;
  458. int err = -ENOMEM;
  459. /* Check if that enr is already covered by an already created bio.
  460. * Caution, bios[] is not NULL terminated,
  461. * but only initialized to all NULL.
  462. * For completely scattered activity log,
  463. * the last invocation iterates over all bios,
  464. * and finds the last NULL entry.
  465. */
  466. while ((bio = bios[i])) {
  467. if (bio->bi_sector == on_disk_sector)
  468. return 0;
  469. i++;
  470. }
  471. /* bios[i] == NULL, the next not yet used slot */
  472. /* GFP_KERNEL, we are not in the write-out path */
  473. bio = bio_alloc(GFP_KERNEL, 1);
  474. if (bio == NULL)
  475. return -ENOMEM;
  476. if (i > 0) {
  477. const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
  478. page_offset = prev_bv->bv_offset + prev_bv->bv_len;
  479. page = prev_bv->bv_page;
  480. }
  481. if (page_offset == PAGE_SIZE) {
  482. page = alloc_page(__GFP_HIGHMEM);
  483. if (page == NULL)
  484. goto out_bio_put;
  485. page_offset = 0;
  486. } else {
  487. get_page(page);
  488. }
  489. offset = S2W(enr);
  490. drbd_bm_get_lel(mdev, offset,
  491. min_t(size_t, S2W(1), drbd_bm_words(mdev) - offset),
  492. kmap(page) + page_offset);
  493. kunmap(page);
  494. bio->bi_private = wc;
  495. bio->bi_end_io = atodb_endio;
  496. bio->bi_bdev = mdev->ldev->md_bdev;
  497. bio->bi_sector = on_disk_sector;
  498. if (bio_add_page(bio, page, MD_SECTOR_SIZE, page_offset) != MD_SECTOR_SIZE)
  499. goto out_put_page;
  500. atomic_inc(&wc->count);
  501. /* we already know that we may do this...
  502. * get_ldev_if_state(mdev,D_ATTACHING);
  503. * just get the extra reference, so that the local_cnt reflects
  504. * the number of pending IO requests DRBD at its backing device.
  505. */
  506. atomic_inc(&mdev->local_cnt);
  507. bios[i] = bio;
  508. return 0;
  509. out_put_page:
  510. err = -EINVAL;
  511. put_page(page);
  512. out_bio_put:
  513. bio_put(bio);
  514. return err;
  515. }
  516. /**
  517. * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents
  518. * @mdev: DRBD device.
  519. *
  520. * Called when we detach (unconfigure) local storage,
  521. * or when we go from R_PRIMARY to R_SECONDARY role.
  522. */
  523. void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
  524. {
  525. int i, nr_elements;
  526. unsigned int enr;
  527. struct bio **bios;
  528. struct drbd_atodb_wait wc;
  529. ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
  530. return; /* sorry, I don't have any act_log etc... */
  531. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  532. nr_elements = mdev->act_log->nr_elements;
  533. /* GFP_KERNEL, we are not in anyone's write-out path */
  534. bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
  535. if (!bios)
  536. goto submit_one_by_one;
  537. atomic_set(&wc.count, 0);
  538. init_completion(&wc.io_done);
  539. wc.mdev = mdev;
  540. wc.error = 0;
  541. for (i = 0; i < nr_elements; i++) {
  542. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  543. if (enr == LC_FREE)
  544. continue;
  545. /* next statement also does atomic_inc wc.count and local_cnt */
  546. if (atodb_prepare_unless_covered(mdev, bios,
  547. enr/AL_EXT_PER_BM_SECT,
  548. &wc))
  549. goto free_bios_submit_one_by_one;
  550. }
  551. /* unnecessary optimization? */
  552. lc_unlock(mdev->act_log);
  553. wake_up(&mdev->al_wait);
  554. /* all prepared, submit them */
  555. for (i = 0; i < nr_elements; i++) {
  556. if (bios[i] == NULL)
  557. break;
  558. if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
  559. bios[i]->bi_rw = WRITE;
  560. bio_endio(bios[i], -EIO);
  561. } else {
  562. submit_bio(WRITE, bios[i]);
  563. }
  564. }
  565. drbd_blk_run_queue(bdev_get_queue(mdev->ldev->md_bdev));
  566. /* always (try to) flush bitmap to stable storage */
  567. drbd_md_flush(mdev);
  568. /* In case we did not submit a single IO do not wait for
  569. * them to complete. ( Because we would wait forever here. )
  570. *
  571. * In case we had IOs and they are already complete, there
  572. * is not point in waiting anyways.
  573. * Therefore this if () ... */
  574. if (atomic_read(&wc.count))
  575. wait_for_completion(&wc.io_done);
  576. put_ldev(mdev);
  577. kfree(bios);
  578. return;
  579. free_bios_submit_one_by_one:
  580. /* free everything by calling the endio callback directly. */
  581. for (i = 0; i < nr_elements && bios[i]; i++)
  582. bio_endio(bios[i], 0);
  583. kfree(bios);
  584. submit_one_by_one:
  585. dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
  586. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  587. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  588. if (enr == LC_FREE)
  589. continue;
  590. /* Really slow: if we have al-extents 16..19 active,
  591. * sector 4 will be written four times! Synchronous! */
  592. drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
  593. }
  594. lc_unlock(mdev->act_log);
  595. wake_up(&mdev->al_wait);
  596. put_ldev(mdev);
  597. }
  598. /**
  599. * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
  600. * @mdev: DRBD device.
  601. */
  602. void drbd_al_apply_to_bm(struct drbd_conf *mdev)
  603. {
  604. unsigned int enr;
  605. unsigned long add = 0;
  606. char ppb[10];
  607. int i;
  608. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  609. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  610. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  611. if (enr == LC_FREE)
  612. continue;
  613. add += drbd_bm_ALe_set_all(mdev, enr);
  614. }
  615. lc_unlock(mdev->act_log);
  616. wake_up(&mdev->al_wait);
  617. dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
  618. ppsize(ppb, Bit2KB(add)));
  619. }
  620. static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
  621. {
  622. int rv;
  623. spin_lock_irq(&mdev->al_lock);
  624. rv = (al_ext->refcnt == 0);
  625. if (likely(rv))
  626. lc_del(mdev->act_log, al_ext);
  627. spin_unlock_irq(&mdev->al_lock);
  628. return rv;
  629. }
  630. /**
  631. * drbd_al_shrink() - Removes all active extents form the activity log
  632. * @mdev: DRBD device.
  633. *
  634. * Removes all active extents form the activity log, waiting until
  635. * the reference count of each entry dropped to 0 first, of course.
  636. *
  637. * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
  638. */
  639. void drbd_al_shrink(struct drbd_conf *mdev)
  640. {
  641. struct lc_element *al_ext;
  642. int i;
  643. D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
  644. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  645. al_ext = lc_element_by_index(mdev->act_log, i);
  646. if (al_ext->lc_number == LC_FREE)
  647. continue;
  648. wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
  649. }
  650. wake_up(&mdev->al_wait);
  651. }
  652. static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  653. {
  654. struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
  655. if (!get_ldev(mdev)) {
  656. if (__ratelimit(&drbd_ratelimit_state))
  657. dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
  658. kfree(udw);
  659. return 1;
  660. }
  661. drbd_bm_write_sect(mdev, udw->enr);
  662. put_ldev(mdev);
  663. kfree(udw);
  664. if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
  665. switch (mdev->state.conn) {
  666. case C_SYNC_SOURCE: case C_SYNC_TARGET:
  667. case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
  668. drbd_resync_finished(mdev);
  669. default:
  670. /* nothing to do */
  671. break;
  672. }
  673. }
  674. drbd_bcast_sync_progress(mdev);
  675. return 1;
  676. }
  677. /* ATTENTION. The AL's extents are 4MB each, while the extents in the
  678. * resync LRU-cache are 16MB each.
  679. * The caller of this function has to hold an get_ldev() reference.
  680. *
  681. * TODO will be obsoleted once we have a caching lru of the on disk bitmap
  682. */
  683. static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
  684. int count, int success)
  685. {
  686. struct lc_element *e;
  687. struct update_odbm_work *udw;
  688. unsigned int enr;
  689. D_ASSERT(atomic_read(&mdev->local_cnt));
  690. /* I simply assume that a sector/size pair never crosses
  691. * a 16 MB extent border. (Currently this is true...) */
  692. enr = BM_SECT_TO_EXT(sector);
  693. e = lc_get(mdev->resync, enr);
  694. if (e) {
  695. struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
  696. if (ext->lce.lc_number == enr) {
  697. if (success)
  698. ext->rs_left -= count;
  699. else
  700. ext->rs_failed += count;
  701. if (ext->rs_left < ext->rs_failed) {
  702. dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
  703. "rs_failed=%d count=%d\n",
  704. (unsigned long long)sector,
  705. ext->lce.lc_number, ext->rs_left,
  706. ext->rs_failed, count);
  707. dump_stack();
  708. lc_put(mdev->resync, &ext->lce);
  709. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  710. return;
  711. }
  712. } else {
  713. /* Normally this element should be in the cache,
  714. * since drbd_rs_begin_io() pulled it already in.
  715. *
  716. * But maybe an application write finished, and we set
  717. * something outside the resync lru_cache in sync.
  718. */
  719. int rs_left = drbd_bm_e_weight(mdev, enr);
  720. if (ext->flags != 0) {
  721. dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
  722. " -> %d[%u;00]\n",
  723. ext->lce.lc_number, ext->rs_left,
  724. ext->flags, enr, rs_left);
  725. ext->flags = 0;
  726. }
  727. if (ext->rs_failed) {
  728. dev_warn(DEV, "Kicking resync_lru element enr=%u "
  729. "out with rs_failed=%d\n",
  730. ext->lce.lc_number, ext->rs_failed);
  731. set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
  732. }
  733. ext->rs_left = rs_left;
  734. ext->rs_failed = success ? 0 : count;
  735. lc_changed(mdev->resync, &ext->lce);
  736. }
  737. lc_put(mdev->resync, &ext->lce);
  738. /* no race, we are within the al_lock! */
  739. if (ext->rs_left == ext->rs_failed) {
  740. ext->rs_failed = 0;
  741. udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
  742. if (udw) {
  743. udw->enr = ext->lce.lc_number;
  744. udw->w.cb = w_update_odbm;
  745. drbd_queue_work_front(&mdev->data.work, &udw->w);
  746. } else {
  747. dev_warn(DEV, "Could not kmalloc an udw\n");
  748. set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
  749. }
  750. }
  751. } else {
  752. dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
  753. mdev->resync_locked,
  754. mdev->resync->nr_elements,
  755. mdev->resync->flags);
  756. }
  757. }
  758. /* clear the bit corresponding to the piece of storage in question:
  759. * size byte of data starting from sector. Only clear a bits of the affected
  760. * one ore more _aligned_ BM_BLOCK_SIZE blocks.
  761. *
  762. * called by worker on C_SYNC_TARGET and receiver on SyncSource.
  763. *
  764. */
  765. void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
  766. const char *file, const unsigned int line)
  767. {
  768. /* Is called from worker and receiver context _only_ */
  769. unsigned long sbnr, ebnr, lbnr;
  770. unsigned long count = 0;
  771. sector_t esector, nr_sectors;
  772. int wake_up = 0;
  773. unsigned long flags;
  774. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
  775. dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
  776. (unsigned long long)sector, size);
  777. return;
  778. }
  779. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  780. esector = sector + (size >> 9) - 1;
  781. ERR_IF(sector >= nr_sectors) return;
  782. ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
  783. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  784. /* we clear it (in sync).
  785. * round up start sector, round down end sector. we make sure we only
  786. * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
  787. if (unlikely(esector < BM_SECT_PER_BIT-1))
  788. return;
  789. if (unlikely(esector == (nr_sectors-1)))
  790. ebnr = lbnr;
  791. else
  792. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  793. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  794. if (sbnr > ebnr)
  795. return;
  796. /*
  797. * ok, (capacity & 7) != 0 sometimes, but who cares...
  798. * we count rs_{total,left} in bits, not sectors.
  799. */
  800. spin_lock_irqsave(&mdev->al_lock, flags);
  801. count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
  802. if (count) {
  803. /* we need the lock for drbd_try_clear_on_disk_bm */
  804. if (jiffies - mdev->rs_mark_time > HZ*10) {
  805. /* should be rolling marks,
  806. * but we estimate only anyways. */
  807. if (mdev->rs_mark_left != drbd_bm_total_weight(mdev) &&
  808. mdev->state.conn != C_PAUSED_SYNC_T &&
  809. mdev->state.conn != C_PAUSED_SYNC_S) {
  810. mdev->rs_mark_time = jiffies;
  811. mdev->rs_mark_left = drbd_bm_total_weight(mdev);
  812. }
  813. }
  814. if (get_ldev(mdev)) {
  815. drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
  816. put_ldev(mdev);
  817. }
  818. /* just wake_up unconditional now, various lc_chaged(),
  819. * lc_put() in drbd_try_clear_on_disk_bm(). */
  820. wake_up = 1;
  821. }
  822. spin_unlock_irqrestore(&mdev->al_lock, flags);
  823. if (wake_up)
  824. wake_up(&mdev->al_wait);
  825. }
  826. /*
  827. * this is intended to set one request worth of data out of sync.
  828. * affects at least 1 bit,
  829. * and at most 1+DRBD_MAX_SEGMENT_SIZE/BM_BLOCK_SIZE bits.
  830. *
  831. * called by tl_clear and drbd_send_dblock (==drbd_make_request).
  832. * so this can be _any_ process.
  833. */
  834. void __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
  835. const char *file, const unsigned int line)
  836. {
  837. unsigned long sbnr, ebnr, lbnr, flags;
  838. sector_t esector, nr_sectors;
  839. unsigned int enr, count;
  840. struct lc_element *e;
  841. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
  842. dev_err(DEV, "sector: %llus, size: %d\n",
  843. (unsigned long long)sector, size);
  844. return;
  845. }
  846. if (!get_ldev(mdev))
  847. return; /* no disk, no metadata, no bitmap to set bits in */
  848. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  849. esector = sector + (size >> 9) - 1;
  850. ERR_IF(sector >= nr_sectors)
  851. goto out;
  852. ERR_IF(esector >= nr_sectors)
  853. esector = (nr_sectors-1);
  854. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  855. /* we set it out of sync,
  856. * we do not need to round anything here */
  857. sbnr = BM_SECT_TO_BIT(sector);
  858. ebnr = BM_SECT_TO_BIT(esector);
  859. /* ok, (capacity & 7) != 0 sometimes, but who cares...
  860. * we count rs_{total,left} in bits, not sectors. */
  861. spin_lock_irqsave(&mdev->al_lock, flags);
  862. count = drbd_bm_set_bits(mdev, sbnr, ebnr);
  863. enr = BM_SECT_TO_EXT(sector);
  864. e = lc_find(mdev->resync, enr);
  865. if (e)
  866. lc_entry(e, struct bm_extent, lce)->rs_left += count;
  867. spin_unlock_irqrestore(&mdev->al_lock, flags);
  868. out:
  869. put_ldev(mdev);
  870. }
  871. static
  872. struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
  873. {
  874. struct lc_element *e;
  875. struct bm_extent *bm_ext;
  876. int wakeup = 0;
  877. unsigned long rs_flags;
  878. spin_lock_irq(&mdev->al_lock);
  879. if (mdev->resync_locked > mdev->resync->nr_elements/2) {
  880. spin_unlock_irq(&mdev->al_lock);
  881. return NULL;
  882. }
  883. e = lc_get(mdev->resync, enr);
  884. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  885. if (bm_ext) {
  886. if (bm_ext->lce.lc_number != enr) {
  887. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  888. bm_ext->rs_failed = 0;
  889. lc_changed(mdev->resync, &bm_ext->lce);
  890. wakeup = 1;
  891. }
  892. if (bm_ext->lce.refcnt == 1)
  893. mdev->resync_locked++;
  894. set_bit(BME_NO_WRITES, &bm_ext->flags);
  895. }
  896. rs_flags = mdev->resync->flags;
  897. spin_unlock_irq(&mdev->al_lock);
  898. if (wakeup)
  899. wake_up(&mdev->al_wait);
  900. if (!bm_ext) {
  901. if (rs_flags & LC_STARVING)
  902. dev_warn(DEV, "Have to wait for element"
  903. " (resync LRU too small?)\n");
  904. BUG_ON(rs_flags & LC_DIRTY);
  905. }
  906. return bm_ext;
  907. }
  908. static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
  909. {
  910. struct lc_element *al_ext;
  911. int rv = 0;
  912. spin_lock_irq(&mdev->al_lock);
  913. if (unlikely(enr == mdev->act_log->new_number))
  914. rv = 1;
  915. else {
  916. al_ext = lc_find(mdev->act_log, enr);
  917. if (al_ext) {
  918. if (al_ext->refcnt)
  919. rv = 1;
  920. }
  921. }
  922. spin_unlock_irq(&mdev->al_lock);
  923. /*
  924. if (unlikely(rv)) {
  925. dev_info(DEV, "Delaying sync read until app's write is done\n");
  926. }
  927. */
  928. return rv;
  929. }
  930. /**
  931. * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
  932. * @mdev: DRBD device.
  933. * @sector: The sector number.
  934. *
  935. * This functions sleeps on al_wait. Returns 1 on success, 0 if interrupted.
  936. */
  937. int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  938. {
  939. unsigned int enr = BM_SECT_TO_EXT(sector);
  940. struct bm_extent *bm_ext;
  941. int i, sig;
  942. sig = wait_event_interruptible(mdev->al_wait,
  943. (bm_ext = _bme_get(mdev, enr)));
  944. if (sig)
  945. return 0;
  946. if (test_bit(BME_LOCKED, &bm_ext->flags))
  947. return 1;
  948. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  949. sig = wait_event_interruptible(mdev->al_wait,
  950. !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i));
  951. if (sig) {
  952. spin_lock_irq(&mdev->al_lock);
  953. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  954. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  955. mdev->resync_locked--;
  956. wake_up(&mdev->al_wait);
  957. }
  958. spin_unlock_irq(&mdev->al_lock);
  959. return 0;
  960. }
  961. }
  962. set_bit(BME_LOCKED, &bm_ext->flags);
  963. return 1;
  964. }
  965. /**
  966. * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
  967. * @mdev: DRBD device.
  968. * @sector: The sector number.
  969. *
  970. * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
  971. * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
  972. * if there is still application IO going on in this area.
  973. */
  974. int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  975. {
  976. unsigned int enr = BM_SECT_TO_EXT(sector);
  977. const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
  978. struct lc_element *e;
  979. struct bm_extent *bm_ext;
  980. int i;
  981. spin_lock_irq(&mdev->al_lock);
  982. if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
  983. /* in case you have very heavy scattered io, it may
  984. * stall the syncer undefined if we give up the ref count
  985. * when we try again and requeue.
  986. *
  987. * if we don't give up the refcount, but the next time
  988. * we are scheduled this extent has been "synced" by new
  989. * application writes, we'd miss the lc_put on the
  990. * extent we keep the refcount on.
  991. * so we remembered which extent we had to try again, and
  992. * if the next requested one is something else, we do
  993. * the lc_put here...
  994. * we also have to wake_up
  995. */
  996. e = lc_find(mdev->resync, mdev->resync_wenr);
  997. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  998. if (bm_ext) {
  999. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1000. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  1001. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1002. mdev->resync_wenr = LC_FREE;
  1003. if (lc_put(mdev->resync, &bm_ext->lce) == 0)
  1004. mdev->resync_locked--;
  1005. wake_up(&mdev->al_wait);
  1006. } else {
  1007. dev_alert(DEV, "LOGIC BUG\n");
  1008. }
  1009. }
  1010. /* TRY. */
  1011. e = lc_try_get(mdev->resync, enr);
  1012. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1013. if (bm_ext) {
  1014. if (test_bit(BME_LOCKED, &bm_ext->flags))
  1015. goto proceed;
  1016. if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
  1017. mdev->resync_locked++;
  1018. } else {
  1019. /* we did set the BME_NO_WRITES,
  1020. * but then could not set BME_LOCKED,
  1021. * so we tried again.
  1022. * drop the extra reference. */
  1023. bm_ext->lce.refcnt--;
  1024. D_ASSERT(bm_ext->lce.refcnt > 0);
  1025. }
  1026. goto check_al;
  1027. } else {
  1028. /* do we rather want to try later? */
  1029. if (mdev->resync_locked > mdev->resync->nr_elements-3)
  1030. goto try_again;
  1031. /* Do or do not. There is no try. -- Yoda */
  1032. e = lc_get(mdev->resync, enr);
  1033. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1034. if (!bm_ext) {
  1035. const unsigned long rs_flags = mdev->resync->flags;
  1036. if (rs_flags & LC_STARVING)
  1037. dev_warn(DEV, "Have to wait for element"
  1038. " (resync LRU too small?)\n");
  1039. BUG_ON(rs_flags & LC_DIRTY);
  1040. goto try_again;
  1041. }
  1042. if (bm_ext->lce.lc_number != enr) {
  1043. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  1044. bm_ext->rs_failed = 0;
  1045. lc_changed(mdev->resync, &bm_ext->lce);
  1046. wake_up(&mdev->al_wait);
  1047. D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
  1048. }
  1049. set_bit(BME_NO_WRITES, &bm_ext->flags);
  1050. D_ASSERT(bm_ext->lce.refcnt == 1);
  1051. mdev->resync_locked++;
  1052. goto check_al;
  1053. }
  1054. check_al:
  1055. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  1056. if (unlikely(al_enr+i == mdev->act_log->new_number))
  1057. goto try_again;
  1058. if (lc_is_used(mdev->act_log, al_enr+i))
  1059. goto try_again;
  1060. }
  1061. set_bit(BME_LOCKED, &bm_ext->flags);
  1062. proceed:
  1063. mdev->resync_wenr = LC_FREE;
  1064. spin_unlock_irq(&mdev->al_lock);
  1065. return 0;
  1066. try_again:
  1067. if (bm_ext)
  1068. mdev->resync_wenr = enr;
  1069. spin_unlock_irq(&mdev->al_lock);
  1070. return -EAGAIN;
  1071. }
  1072. void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
  1073. {
  1074. unsigned int enr = BM_SECT_TO_EXT(sector);
  1075. struct lc_element *e;
  1076. struct bm_extent *bm_ext;
  1077. unsigned long flags;
  1078. spin_lock_irqsave(&mdev->al_lock, flags);
  1079. e = lc_find(mdev->resync, enr);
  1080. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1081. if (!bm_ext) {
  1082. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1083. if (__ratelimit(&drbd_ratelimit_state))
  1084. dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
  1085. return;
  1086. }
  1087. if (bm_ext->lce.refcnt == 0) {
  1088. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1089. dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
  1090. "but refcnt is 0!?\n",
  1091. (unsigned long long)sector, enr);
  1092. return;
  1093. }
  1094. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  1095. clear_bit(BME_LOCKED, &bm_ext->flags);
  1096. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1097. mdev->resync_locked--;
  1098. wake_up(&mdev->al_wait);
  1099. }
  1100. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1101. }
  1102. /**
  1103. * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
  1104. * @mdev: DRBD device.
  1105. */
  1106. void drbd_rs_cancel_all(struct drbd_conf *mdev)
  1107. {
  1108. spin_lock_irq(&mdev->al_lock);
  1109. if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
  1110. lc_reset(mdev->resync);
  1111. put_ldev(mdev);
  1112. }
  1113. mdev->resync_locked = 0;
  1114. mdev->resync_wenr = LC_FREE;
  1115. spin_unlock_irq(&mdev->al_lock);
  1116. wake_up(&mdev->al_wait);
  1117. }
  1118. /**
  1119. * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
  1120. * @mdev: DRBD device.
  1121. *
  1122. * Returns 0 upon success, -EAGAIN if at least one reference count was
  1123. * not zero.
  1124. */
  1125. int drbd_rs_del_all(struct drbd_conf *mdev)
  1126. {
  1127. struct lc_element *e;
  1128. struct bm_extent *bm_ext;
  1129. int i;
  1130. spin_lock_irq(&mdev->al_lock);
  1131. if (get_ldev_if_state(mdev, D_FAILED)) {
  1132. /* ok, ->resync is there. */
  1133. for (i = 0; i < mdev->resync->nr_elements; i++) {
  1134. e = lc_element_by_index(mdev->resync, i);
  1135. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1136. if (bm_ext->lce.lc_number == LC_FREE)
  1137. continue;
  1138. if (bm_ext->lce.lc_number == mdev->resync_wenr) {
  1139. dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
  1140. " got 'synced' by application io\n",
  1141. mdev->resync_wenr);
  1142. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1143. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  1144. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1145. mdev->resync_wenr = LC_FREE;
  1146. lc_put(mdev->resync, &bm_ext->lce);
  1147. }
  1148. if (bm_ext->lce.refcnt != 0) {
  1149. dev_info(DEV, "Retrying drbd_rs_del_all() later. "
  1150. "refcnt=%d\n", bm_ext->lce.refcnt);
  1151. put_ldev(mdev);
  1152. spin_unlock_irq(&mdev->al_lock);
  1153. return -EAGAIN;
  1154. }
  1155. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1156. D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
  1157. lc_del(mdev->resync, &bm_ext->lce);
  1158. }
  1159. D_ASSERT(mdev->resync->used == 0);
  1160. put_ldev(mdev);
  1161. }
  1162. spin_unlock_irq(&mdev->al_lock);
  1163. return 0;
  1164. }
  1165. /**
  1166. * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
  1167. * @mdev: DRBD device.
  1168. * @sector: The sector number.
  1169. * @size: Size of failed IO operation, in byte.
  1170. */
  1171. void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
  1172. {
  1173. /* Is called from worker and receiver context _only_ */
  1174. unsigned long sbnr, ebnr, lbnr;
  1175. unsigned long count;
  1176. sector_t esector, nr_sectors;
  1177. int wake_up = 0;
  1178. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
  1179. dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
  1180. (unsigned long long)sector, size);
  1181. return;
  1182. }
  1183. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  1184. esector = sector + (size >> 9) - 1;
  1185. ERR_IF(sector >= nr_sectors) return;
  1186. ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
  1187. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  1188. /*
  1189. * round up start sector, round down end sector. we make sure we only
  1190. * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
  1191. if (unlikely(esector < BM_SECT_PER_BIT-1))
  1192. return;
  1193. if (unlikely(esector == (nr_sectors-1)))
  1194. ebnr = lbnr;
  1195. else
  1196. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  1197. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  1198. if (sbnr > ebnr)
  1199. return;
  1200. /*
  1201. * ok, (capacity & 7) != 0 sometimes, but who cares...
  1202. * we count rs_{total,left} in bits, not sectors.
  1203. */
  1204. spin_lock_irq(&mdev->al_lock);
  1205. count = drbd_bm_count_bits(mdev, sbnr, ebnr);
  1206. if (count) {
  1207. mdev->rs_failed += count;
  1208. if (get_ldev(mdev)) {
  1209. drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
  1210. put_ldev(mdev);
  1211. }
  1212. /* just wake_up unconditional now, various lc_chaged(),
  1213. * lc_put() in drbd_try_clear_on_disk_bm(). */
  1214. wake_up = 1;
  1215. }
  1216. spin_unlock_irq(&mdev->al_lock);
  1217. if (wake_up)
  1218. wake_up(&mdev->al_wait);
  1219. }