drbd_actlog.c 37 KB

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