drbd_actlog.c 34 KB

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