drbd_actlog.c 35 KB

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