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