drbd_actlog.c 37 KB

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