drbd_actlog.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. drbd_actlog.c
  4. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  5. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
  6. Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  7. Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/crc32c.h>
  11. #include <linux/drbd.h>
  12. #include <linux/drbd_limits.h>
  13. #include "drbd_int.h"
  14. enum al_transaction_types {
  15. AL_TR_UPDATE = 0,
  16. AL_TR_INITIALIZED = 0xffff
  17. };
  18. /* all fields on disc in big endian */
  19. struct __packed al_transaction_on_disk {
  20. /* don't we all like magic */
  21. __be32 magic;
  22. /* to identify the most recent transaction block
  23. * in the on disk ring buffer */
  24. __be32 tr_number;
  25. /* checksum on the full 4k block, with this field set to 0. */
  26. __be32 crc32c;
  27. /* type of transaction, special transaction types like:
  28. * purge-all, set-all-idle, set-all-active, ... to-be-defined
  29. * see also enum al_transaction_types */
  30. __be16 transaction_type;
  31. /* we currently allow only a few thousand extents,
  32. * so 16bit will be enough for the slot number. */
  33. /* how many updates in this transaction */
  34. __be16 n_updates;
  35. /* maximum slot number, "al-extents" in drbd.conf speak.
  36. * Having this in each transaction should make reconfiguration
  37. * of that parameter easier. */
  38. __be16 context_size;
  39. /* slot number the context starts with */
  40. __be16 context_start_slot_nr;
  41. /* Some reserved bytes. Expected usage is a 64bit counter of
  42. * sectors-written since device creation, and other data generation tag
  43. * supporting usage */
  44. __be32 __reserved[4];
  45. /* --- 36 byte used --- */
  46. /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
  47. * in one transaction, then use the remaining byte in the 4k block for
  48. * context information. "Flexible" number of updates per transaction
  49. * does not help, as we have to account for the case when all update
  50. * slots are used anyways, so it would only complicate code without
  51. * additional benefit.
  52. */
  53. __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
  54. /* but the extent number is 32bit, which at an extent size of 4 MiB
  55. * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
  56. __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
  57. /* --- 420 bytes used (36 + 64*6) --- */
  58. /* 4096 - 420 = 3676 = 919 * 4 */
  59. __be32 context[AL_CONTEXT_PER_TRANSACTION];
  60. };
  61. void *drbd_md_get_buffer(struct drbd_device *device, const char *intent)
  62. {
  63. int r;
  64. wait_event(device->misc_wait,
  65. (r = atomic_cmpxchg(&device->md_io.in_use, 0, 1)) == 0 ||
  66. device->state.disk <= D_FAILED);
  67. if (r)
  68. return NULL;
  69. device->md_io.current_use = intent;
  70. device->md_io.start_jif = jiffies;
  71. device->md_io.submit_jif = device->md_io.start_jif - 1;
  72. return page_address(device->md_io.page);
  73. }
  74. void drbd_md_put_buffer(struct drbd_device *device)
  75. {
  76. if (atomic_dec_and_test(&device->md_io.in_use))
  77. wake_up(&device->misc_wait);
  78. }
  79. void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev,
  80. unsigned int *done)
  81. {
  82. long dt;
  83. rcu_read_lock();
  84. dt = rcu_dereference(bdev->disk_conf)->disk_timeout;
  85. rcu_read_unlock();
  86. dt = dt * HZ / 10;
  87. if (dt == 0)
  88. dt = MAX_SCHEDULE_TIMEOUT;
  89. dt = wait_event_timeout(device->misc_wait,
  90. *done || test_bit(FORCE_DETACH, &device->flags), dt);
  91. if (dt == 0) {
  92. drbd_err(device, "meta-data IO operation timed out\n");
  93. drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH);
  94. }
  95. }
  96. static int _drbd_md_sync_page_io(struct drbd_device *device,
  97. struct drbd_backing_dev *bdev,
  98. sector_t sector, enum req_op op)
  99. {
  100. struct bio *bio;
  101. /* we do all our meta data IO in aligned 4k blocks. */
  102. const int size = 4096;
  103. int err;
  104. blk_opf_t op_flags = 0;
  105. device->md_io.done = 0;
  106. device->md_io.error = -ENODEV;
  107. if ((op == REQ_OP_WRITE) && !test_bit(MD_NO_FUA, &device->flags))
  108. op_flags |= REQ_FUA | REQ_PREFLUSH;
  109. op_flags |= REQ_SYNC;
  110. bio = bio_alloc_bioset(bdev->md_bdev, 1, op | op_flags, GFP_NOIO,
  111. &drbd_md_io_bio_set);
  112. bio->bi_iter.bi_sector = sector;
  113. err = -EIO;
  114. if (bio_add_page(bio, device->md_io.page, size, 0) != size)
  115. goto out;
  116. bio->bi_private = device;
  117. bio->bi_end_io = drbd_md_endio;
  118. if (op != REQ_OP_WRITE && device->state.disk == D_DISKLESS && device->ldev == NULL)
  119. /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */
  120. ;
  121. else if (!get_ldev_if_state(device, D_ATTACHING)) {
  122. /* Corresponding put_ldev in drbd_md_endio() */
  123. drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n");
  124. err = -ENODEV;
  125. goto out;
  126. }
  127. bio_get(bio); /* one bio_put() is in the completion handler */
  128. atomic_inc(&device->md_io.in_use); /* drbd_md_put_buffer() is in the completion handler */
  129. device->md_io.submit_jif = jiffies;
  130. if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
  131. bio_io_error(bio);
  132. else
  133. submit_bio(bio);
  134. wait_until_done_or_force_detached(device, bdev, &device->md_io.done);
  135. if (!bio->bi_status)
  136. err = device->md_io.error;
  137. out:
  138. bio_put(bio);
  139. return err;
  140. }
  141. int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev,
  142. sector_t sector, enum req_op op)
  143. {
  144. int err;
  145. D_ASSERT(device, atomic_read(&device->md_io.in_use) == 1);
  146. BUG_ON(!bdev->md_bdev);
  147. dynamic_drbd_dbg(device, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n",
  148. current->comm, current->pid, __func__,
  149. (unsigned long long)sector, (op == REQ_OP_WRITE) ? "WRITE" : "READ",
  150. (void*)_RET_IP_ );
  151. if (sector < drbd_md_first_sector(bdev) ||
  152. sector + 7 > drbd_md_last_sector(bdev))
  153. drbd_alert(device, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
  154. current->comm, current->pid, __func__,
  155. (unsigned long long)sector,
  156. (op == REQ_OP_WRITE) ? "WRITE" : "READ");
  157. err = _drbd_md_sync_page_io(device, bdev, sector, op);
  158. if (err) {
  159. drbd_err(device, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n",
  160. (unsigned long long)sector,
  161. (op == REQ_OP_WRITE) ? "WRITE" : "READ", err);
  162. }
  163. return err;
  164. }
  165. static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr)
  166. {
  167. struct lc_element *tmp;
  168. tmp = lc_find(device->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. return bm_ext;
  173. }
  174. return NULL;
  175. }
  176. static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock)
  177. {
  178. struct lc_element *al_ext;
  179. struct bm_extent *bm_ext;
  180. int wake;
  181. spin_lock_irq(&device->al_lock);
  182. bm_ext = find_active_resync_extent(device, enr);
  183. if (bm_ext) {
  184. wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
  185. spin_unlock_irq(&device->al_lock);
  186. if (wake)
  187. wake_up(&device->al_wait);
  188. return NULL;
  189. }
  190. if (nonblock)
  191. al_ext = lc_try_get(device->act_log, enr);
  192. else
  193. al_ext = lc_get(device->act_log, enr);
  194. spin_unlock_irq(&device->al_lock);
  195. return al_ext;
  196. }
  197. bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i)
  198. {
  199. /* for bios crossing activity log extent boundaries,
  200. * we may need to activate two extents in one go */
  201. unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
  202. unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
  203. D_ASSERT(device, first <= last);
  204. D_ASSERT(device, atomic_read(&device->local_cnt) > 0);
  205. /* FIXME figure out a fast path for bios crossing AL extent boundaries */
  206. if (first != last)
  207. return false;
  208. return _al_get(device, first, true);
  209. }
  210. bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i)
  211. {
  212. /* for bios crossing activity log extent boundaries,
  213. * we may need to activate two extents in one go */
  214. unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
  215. unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
  216. unsigned enr;
  217. bool need_transaction = false;
  218. D_ASSERT(device, first <= last);
  219. D_ASSERT(device, atomic_read(&device->local_cnt) > 0);
  220. for (enr = first; enr <= last; enr++) {
  221. struct lc_element *al_ext;
  222. wait_event(device->al_wait,
  223. (al_ext = _al_get(device, enr, false)) != NULL);
  224. if (al_ext->lc_number != enr)
  225. need_transaction = true;
  226. }
  227. return need_transaction;
  228. }
  229. #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
  230. /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
  231. * are still coupled, or assume too much about their relation.
  232. * Code below will not work if this is violated.
  233. * Will be cleaned up with some followup patch.
  234. */
  235. # error FIXME
  236. #endif
  237. static unsigned int al_extent_to_bm_page(unsigned int al_enr)
  238. {
  239. return al_enr >>
  240. /* bit to page */
  241. ((PAGE_SHIFT + 3) -
  242. /* al extent number to bit */
  243. (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
  244. }
  245. static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device)
  246. {
  247. const unsigned int stripes = device->ldev->md.al_stripes;
  248. const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k;
  249. /* transaction number, modulo on-disk ring buffer wrap around */
  250. unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k);
  251. /* ... to aligned 4k on disk block */
  252. t = ((t % stripes) * stripe_size_4kB) + t/stripes;
  253. /* ... to 512 byte sector in activity log */
  254. t *= 8;
  255. /* ... plus offset to the on disk position */
  256. return device->ldev->md.md_offset + device->ldev->md.al_offset + t;
  257. }
  258. static int __al_write_transaction(struct drbd_device *device, struct al_transaction_on_disk *buffer)
  259. {
  260. struct lc_element *e;
  261. sector_t sector;
  262. int i, mx;
  263. unsigned extent_nr;
  264. unsigned crc = 0;
  265. int err = 0;
  266. memset(buffer, 0, sizeof(*buffer));
  267. buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
  268. buffer->tr_number = cpu_to_be32(device->al_tr_number);
  269. i = 0;
  270. drbd_bm_reset_al_hints(device);
  271. /* Even though no one can start to change this list
  272. * once we set the LC_LOCKED -- from drbd_al_begin_io(),
  273. * lc_try_lock_for_transaction() --, someone may still
  274. * be in the process of changing it. */
  275. spin_lock_irq(&device->al_lock);
  276. list_for_each_entry(e, &device->act_log->to_be_changed, list) {
  277. if (i == AL_UPDATES_PER_TRANSACTION) {
  278. i++;
  279. break;
  280. }
  281. buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
  282. buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
  283. if (e->lc_number != LC_FREE)
  284. drbd_bm_mark_for_writeout(device,
  285. al_extent_to_bm_page(e->lc_number));
  286. i++;
  287. }
  288. spin_unlock_irq(&device->al_lock);
  289. BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
  290. buffer->n_updates = cpu_to_be16(i);
  291. for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
  292. buffer->update_slot_nr[i] = cpu_to_be16(-1);
  293. buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
  294. }
  295. buffer->context_size = cpu_to_be16(device->act_log->nr_elements);
  296. buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle);
  297. mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
  298. device->act_log->nr_elements - device->al_tr_cycle);
  299. for (i = 0; i < mx; i++) {
  300. unsigned idx = device->al_tr_cycle + i;
  301. extent_nr = lc_element_by_index(device->act_log, idx)->lc_number;
  302. buffer->context[i] = cpu_to_be32(extent_nr);
  303. }
  304. for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
  305. buffer->context[i] = cpu_to_be32(LC_FREE);
  306. device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
  307. if (device->al_tr_cycle >= device->act_log->nr_elements)
  308. device->al_tr_cycle = 0;
  309. sector = al_tr_number_to_on_disk_sector(device);
  310. crc = crc32c(0, buffer, 4096);
  311. buffer->crc32c = cpu_to_be32(crc);
  312. if (drbd_bm_write_hinted(device))
  313. err = -EIO;
  314. else {
  315. bool write_al_updates;
  316. rcu_read_lock();
  317. write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates;
  318. rcu_read_unlock();
  319. if (write_al_updates) {
  320. if (drbd_md_sync_page_io(device, device->ldev, sector, REQ_OP_WRITE)) {
  321. err = -EIO;
  322. drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
  323. } else {
  324. device->al_tr_number++;
  325. device->al_writ_cnt++;
  326. }
  327. }
  328. }
  329. return err;
  330. }
  331. static int al_write_transaction(struct drbd_device *device)
  332. {
  333. struct al_transaction_on_disk *buffer;
  334. int err;
  335. if (!get_ldev(device)) {
  336. drbd_err(device, "disk is %s, cannot start al transaction\n",
  337. drbd_disk_str(device->state.disk));
  338. return -EIO;
  339. }
  340. /* The bitmap write may have failed, causing a state change. */
  341. if (device->state.disk < D_INCONSISTENT) {
  342. drbd_err(device,
  343. "disk is %s, cannot write al transaction\n",
  344. drbd_disk_str(device->state.disk));
  345. put_ldev(device);
  346. return -EIO;
  347. }
  348. /* protects md_io_buffer, al_tr_cycle, ... */
  349. buffer = drbd_md_get_buffer(device, __func__);
  350. if (!buffer) {
  351. drbd_err(device, "disk failed while waiting for md_io buffer\n");
  352. put_ldev(device);
  353. return -ENODEV;
  354. }
  355. err = __al_write_transaction(device, buffer);
  356. drbd_md_put_buffer(device);
  357. put_ldev(device);
  358. return err;
  359. }
  360. void drbd_al_begin_io_commit(struct drbd_device *device)
  361. {
  362. bool locked = false;
  363. /* Serialize multiple transactions.
  364. * This uses test_and_set_bit, memory barrier is implicit.
  365. */
  366. wait_event(device->al_wait,
  367. device->act_log->pending_changes == 0 ||
  368. (locked = lc_try_lock_for_transaction(device->act_log)));
  369. if (locked) {
  370. /* Double check: it may have been committed by someone else,
  371. * while we have been waiting for the lock. */
  372. if (device->act_log->pending_changes) {
  373. bool write_al_updates;
  374. rcu_read_lock();
  375. write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates;
  376. rcu_read_unlock();
  377. if (write_al_updates)
  378. al_write_transaction(device);
  379. spin_lock_irq(&device->al_lock);
  380. /* FIXME
  381. if (err)
  382. we need an "lc_cancel" here;
  383. */
  384. lc_committed(device->act_log);
  385. spin_unlock_irq(&device->al_lock);
  386. }
  387. lc_unlock(device->act_log);
  388. wake_up(&device->al_wait);
  389. }
  390. }
  391. /*
  392. * @delegate: delegate activity log I/O to the worker thread
  393. */
  394. void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i)
  395. {
  396. if (drbd_al_begin_io_prepare(device, i))
  397. drbd_al_begin_io_commit(device);
  398. }
  399. int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i)
  400. {
  401. struct lru_cache *al = device->act_log;
  402. /* for bios crossing activity log extent boundaries,
  403. * we may need to activate two extents in one go */
  404. unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
  405. unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
  406. unsigned nr_al_extents;
  407. unsigned available_update_slots;
  408. unsigned enr;
  409. D_ASSERT(device, first <= last);
  410. nr_al_extents = 1 + last - first; /* worst case: all touched extends are cold. */
  411. available_update_slots = min(al->nr_elements - al->used,
  412. al->max_pending_changes - al->pending_changes);
  413. /* We want all necessary updates for a given request within the same transaction
  414. * We could first check how many updates are *actually* needed,
  415. * and use that instead of the worst-case nr_al_extents */
  416. if (available_update_slots < nr_al_extents) {
  417. /* Too many activity log extents are currently "hot".
  418. *
  419. * If we have accumulated pending changes already,
  420. * we made progress.
  421. *
  422. * If we cannot get even a single pending change through,
  423. * stop the fast path until we made some progress,
  424. * or requests to "cold" extents could be starved. */
  425. if (!al->pending_changes)
  426. __set_bit(__LC_STARVING, &device->act_log->flags);
  427. return -ENOBUFS;
  428. }
  429. /* Is resync active in this area? */
  430. for (enr = first; enr <= last; enr++) {
  431. struct lc_element *tmp;
  432. tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT);
  433. if (unlikely(tmp != NULL)) {
  434. struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
  435. if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
  436. if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags))
  437. return -EBUSY;
  438. return -EWOULDBLOCK;
  439. }
  440. }
  441. }
  442. /* Checkout the refcounts.
  443. * Given that we checked for available elements and update slots above,
  444. * this has to be successful. */
  445. for (enr = first; enr <= last; enr++) {
  446. struct lc_element *al_ext;
  447. al_ext = lc_get_cumulative(device->act_log, enr);
  448. if (!al_ext)
  449. drbd_info(device, "LOGIC BUG for enr=%u\n", enr);
  450. }
  451. return 0;
  452. }
  453. void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i)
  454. {
  455. /* for bios crossing activity log extent boundaries,
  456. * we may need to activate two extents in one go */
  457. unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
  458. unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
  459. unsigned enr;
  460. struct lc_element *extent;
  461. unsigned long flags;
  462. D_ASSERT(device, first <= last);
  463. spin_lock_irqsave(&device->al_lock, flags);
  464. for (enr = first; enr <= last; enr++) {
  465. extent = lc_find(device->act_log, enr);
  466. if (!extent) {
  467. drbd_err(device, "al_complete_io() called on inactive extent %u\n", enr);
  468. continue;
  469. }
  470. lc_put(device->act_log, extent);
  471. }
  472. spin_unlock_irqrestore(&device->al_lock, flags);
  473. wake_up(&device->al_wait);
  474. }
  475. static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext)
  476. {
  477. int rv;
  478. spin_lock_irq(&device->al_lock);
  479. rv = (al_ext->refcnt == 0);
  480. if (likely(rv))
  481. lc_del(device->act_log, al_ext);
  482. spin_unlock_irq(&device->al_lock);
  483. return rv;
  484. }
  485. /**
  486. * drbd_al_shrink() - Removes all active extents form the activity log
  487. * @device: DRBD device.
  488. *
  489. * Removes all active extents form the activity log, waiting until
  490. * the reference count of each entry dropped to 0 first, of course.
  491. *
  492. * You need to lock device->act_log with lc_try_lock() / lc_unlock()
  493. */
  494. void drbd_al_shrink(struct drbd_device *device)
  495. {
  496. struct lc_element *al_ext;
  497. int i;
  498. D_ASSERT(device, test_bit(__LC_LOCKED, &device->act_log->flags));
  499. for (i = 0; i < device->act_log->nr_elements; i++) {
  500. al_ext = lc_element_by_index(device->act_log, i);
  501. if (al_ext->lc_number == LC_FREE)
  502. continue;
  503. wait_event(device->al_wait, _try_lc_del(device, al_ext));
  504. }
  505. wake_up(&device->al_wait);
  506. }
  507. int drbd_al_initialize(struct drbd_device *device, void *buffer)
  508. {
  509. struct al_transaction_on_disk *al = buffer;
  510. struct drbd_md *md = &device->ldev->md;
  511. int al_size_4k = md->al_stripes * md->al_stripe_size_4k;
  512. int i;
  513. __al_write_transaction(device, al);
  514. /* There may or may not have been a pending transaction. */
  515. spin_lock_irq(&device->al_lock);
  516. lc_committed(device->act_log);
  517. spin_unlock_irq(&device->al_lock);
  518. /* The rest of the transactions will have an empty "updates" list, and
  519. * are written out only to provide the context, and to initialize the
  520. * on-disk ring buffer. */
  521. for (i = 1; i < al_size_4k; i++) {
  522. int err = __al_write_transaction(device, al);
  523. if (err)
  524. return err;
  525. }
  526. return 0;
  527. }
  528. static const char *drbd_change_sync_fname[] = {
  529. [RECORD_RS_FAILED] = "drbd_rs_failed_io",
  530. [SET_IN_SYNC] = "drbd_set_in_sync",
  531. [SET_OUT_OF_SYNC] = "drbd_set_out_of_sync"
  532. };
  533. /* ATTENTION. The AL's extents are 4MB each, while the extents in the
  534. * resync LRU-cache are 16MB each.
  535. * The caller of this function has to hold an get_ldev() reference.
  536. *
  537. * Adjusts the caching members ->rs_left (success) or ->rs_failed (!success),
  538. * potentially pulling in (and recounting the corresponding bits)
  539. * this resync extent into the resync extent lru cache.
  540. *
  541. * Returns whether all bits have been cleared for this resync extent,
  542. * precisely: (rs_left <= rs_failed)
  543. *
  544. * TODO will be obsoleted once we have a caching lru of the on disk bitmap
  545. */
  546. static bool update_rs_extent(struct drbd_device *device,
  547. unsigned int enr, int count,
  548. enum update_sync_bits_mode mode)
  549. {
  550. struct lc_element *e;
  551. D_ASSERT(device, atomic_read(&device->local_cnt));
  552. /* When setting out-of-sync bits,
  553. * we don't need it cached (lc_find).
  554. * But if it is present in the cache,
  555. * we should update the cached bit count.
  556. * Otherwise, that extent should be in the resync extent lru cache
  557. * already -- or we want to pull it in if necessary -- (lc_get),
  558. * then update and check rs_left and rs_failed. */
  559. if (mode == SET_OUT_OF_SYNC)
  560. e = lc_find(device->resync, enr);
  561. else
  562. e = lc_get(device->resync, enr);
  563. if (e) {
  564. struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
  565. if (ext->lce.lc_number == enr) {
  566. if (mode == SET_IN_SYNC)
  567. ext->rs_left -= count;
  568. else if (mode == SET_OUT_OF_SYNC)
  569. ext->rs_left += count;
  570. else
  571. ext->rs_failed += count;
  572. if (ext->rs_left < ext->rs_failed) {
  573. drbd_warn(device, "BAD! enr=%u rs_left=%d "
  574. "rs_failed=%d count=%d cstate=%s\n",
  575. ext->lce.lc_number, ext->rs_left,
  576. ext->rs_failed, count,
  577. drbd_conn_str(device->state.conn));
  578. /* We don't expect to be able to clear more bits
  579. * than have been set when we originally counted
  580. * the set bits to cache that value in ext->rs_left.
  581. * Whatever the reason (disconnect during resync,
  582. * delayed local completion of an application write),
  583. * try to fix it up by recounting here. */
  584. ext->rs_left = drbd_bm_e_weight(device, enr);
  585. }
  586. } else {
  587. /* Normally this element should be in the cache,
  588. * since drbd_rs_begin_io() pulled it already in.
  589. *
  590. * But maybe an application write finished, and we set
  591. * something outside the resync lru_cache in sync.
  592. */
  593. int rs_left = drbd_bm_e_weight(device, enr);
  594. if (ext->flags != 0) {
  595. drbd_warn(device, "changing resync lce: %d[%u;%02lx]"
  596. " -> %d[%u;00]\n",
  597. ext->lce.lc_number, ext->rs_left,
  598. ext->flags, enr, rs_left);
  599. ext->flags = 0;
  600. }
  601. if (ext->rs_failed) {
  602. drbd_warn(device, "Kicking resync_lru element enr=%u "
  603. "out with rs_failed=%d\n",
  604. ext->lce.lc_number, ext->rs_failed);
  605. }
  606. ext->rs_left = rs_left;
  607. ext->rs_failed = (mode == RECORD_RS_FAILED) ? count : 0;
  608. /* we don't keep a persistent log of the resync lru,
  609. * we can commit any change right away. */
  610. lc_committed(device->resync);
  611. }
  612. if (mode != SET_OUT_OF_SYNC)
  613. lc_put(device->resync, &ext->lce);
  614. /* no race, we are within the al_lock! */
  615. if (ext->rs_left <= ext->rs_failed) {
  616. ext->rs_failed = 0;
  617. return true;
  618. }
  619. } else if (mode != SET_OUT_OF_SYNC) {
  620. /* be quiet if lc_find() did not find it. */
  621. drbd_err(device, "lc_get() failed! locked=%d/%d flags=%lu\n",
  622. device->resync_locked,
  623. device->resync->nr_elements,
  624. device->resync->flags);
  625. }
  626. return false;
  627. }
  628. void drbd_advance_rs_marks(struct drbd_peer_device *peer_device, unsigned long still_to_go)
  629. {
  630. struct drbd_device *device = peer_device->device;
  631. unsigned long now = jiffies;
  632. unsigned long last = device->rs_mark_time[device->rs_last_mark];
  633. int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS;
  634. if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
  635. if (device->rs_mark_left[device->rs_last_mark] != still_to_go &&
  636. device->state.conn != C_PAUSED_SYNC_T &&
  637. device->state.conn != C_PAUSED_SYNC_S) {
  638. device->rs_mark_time[next] = now;
  639. device->rs_mark_left[next] = still_to_go;
  640. device->rs_last_mark = next;
  641. }
  642. }
  643. }
  644. /* It is called lazy update, so don't do write-out too often. */
  645. static bool lazy_bitmap_update_due(struct drbd_device *device)
  646. {
  647. return time_after(jiffies, device->rs_last_bcast + 2*HZ);
  648. }
  649. static void maybe_schedule_on_disk_bitmap_update(struct drbd_device *device, bool rs_done)
  650. {
  651. if (rs_done) {
  652. struct drbd_connection *connection = first_peer_device(device)->connection;
  653. if (connection->agreed_pro_version <= 95 ||
  654. is_sync_target_state(device->state.conn))
  655. set_bit(RS_DONE, &device->flags);
  656. /* and also set RS_PROGRESS below */
  657. /* Else: rather wait for explicit notification via receive_state,
  658. * to avoid uuids-rotated-too-fast causing full resync
  659. * in next handshake, in case the replication link breaks
  660. * at the most unfortunate time... */
  661. } else if (!lazy_bitmap_update_due(device))
  662. return;
  663. drbd_device_post_work(device, RS_PROGRESS);
  664. }
  665. static int update_sync_bits(struct drbd_device *device,
  666. unsigned long sbnr, unsigned long ebnr,
  667. enum update_sync_bits_mode mode)
  668. {
  669. /*
  670. * We keep a count of set bits per resync-extent in the ->rs_left
  671. * caching member, so we need to loop and work within the resync extent
  672. * alignment. Typically this loop will execute exactly once.
  673. */
  674. unsigned long flags;
  675. unsigned long count = 0;
  676. unsigned int cleared = 0;
  677. while (sbnr <= ebnr) {
  678. /* set temporary boundary bit number to last bit number within
  679. * the resync extent of the current start bit number,
  680. * but cap at provided end bit number */
  681. unsigned long tbnr = min(ebnr, sbnr | BM_BLOCKS_PER_BM_EXT_MASK);
  682. unsigned long c;
  683. if (mode == RECORD_RS_FAILED)
  684. /* Only called from drbd_rs_failed_io(), bits
  685. * supposedly still set. Recount, maybe some
  686. * of the bits have been successfully cleared
  687. * by application IO meanwhile.
  688. */
  689. c = drbd_bm_count_bits(device, sbnr, tbnr);
  690. else if (mode == SET_IN_SYNC)
  691. c = drbd_bm_clear_bits(device, sbnr, tbnr);
  692. else /* if (mode == SET_OUT_OF_SYNC) */
  693. c = drbd_bm_set_bits(device, sbnr, tbnr);
  694. if (c) {
  695. spin_lock_irqsave(&device->al_lock, flags);
  696. cleared += update_rs_extent(device, BM_BIT_TO_EXT(sbnr), c, mode);
  697. spin_unlock_irqrestore(&device->al_lock, flags);
  698. count += c;
  699. }
  700. sbnr = tbnr + 1;
  701. }
  702. if (count) {
  703. if (mode == SET_IN_SYNC) {
  704. unsigned long still_to_go = drbd_bm_total_weight(device);
  705. bool rs_is_done = (still_to_go <= device->rs_failed);
  706. drbd_advance_rs_marks(first_peer_device(device), still_to_go);
  707. if (cleared || rs_is_done)
  708. maybe_schedule_on_disk_bitmap_update(device, rs_is_done);
  709. } else if (mode == RECORD_RS_FAILED)
  710. device->rs_failed += count;
  711. wake_up(&device->al_wait);
  712. }
  713. return count;
  714. }
  715. static bool plausible_request_size(int size)
  716. {
  717. return size > 0
  718. && size <= DRBD_MAX_BATCH_BIO_SIZE
  719. && IS_ALIGNED(size, 512);
  720. }
  721. /* clear the bit corresponding to the piece of storage in question:
  722. * size byte of data starting from sector. Only clear bits of the affected
  723. * one or more _aligned_ BM_BLOCK_SIZE blocks.
  724. *
  725. * called by worker on C_SYNC_TARGET and receiver on SyncSource.
  726. *
  727. */
  728. int __drbd_change_sync(struct drbd_peer_device *peer_device, sector_t sector, int size,
  729. enum update_sync_bits_mode mode)
  730. {
  731. /* Is called from worker and receiver context _only_ */
  732. struct drbd_device *device = peer_device->device;
  733. unsigned long sbnr, ebnr, lbnr;
  734. unsigned long count = 0;
  735. sector_t esector, nr_sectors;
  736. /* This would be an empty REQ_PREFLUSH, be silent. */
  737. if ((mode == SET_OUT_OF_SYNC) && size == 0)
  738. return 0;
  739. if (!plausible_request_size(size)) {
  740. drbd_err(device, "%s: sector=%llus size=%d nonsense!\n",
  741. drbd_change_sync_fname[mode],
  742. (unsigned long long)sector, size);
  743. return 0;
  744. }
  745. if (!get_ldev(device))
  746. return 0; /* no disk, no metadata, no bitmap to manipulate bits in */
  747. nr_sectors = get_capacity(device->vdisk);
  748. esector = sector + (size >> 9) - 1;
  749. if (!expect(device, sector < nr_sectors))
  750. goto out;
  751. if (!expect(device, esector < nr_sectors))
  752. esector = nr_sectors - 1;
  753. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  754. if (mode == SET_IN_SYNC) {
  755. /* Round up start sector, round down end sector. We make sure
  756. * we only clear full, aligned, BM_BLOCK_SIZE blocks. */
  757. if (unlikely(esector < BM_SECT_PER_BIT-1))
  758. goto out;
  759. if (unlikely(esector == (nr_sectors-1)))
  760. ebnr = lbnr;
  761. else
  762. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  763. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  764. } else {
  765. /* We set it out of sync, or record resync failure.
  766. * Should not round anything here. */
  767. sbnr = BM_SECT_TO_BIT(sector);
  768. ebnr = BM_SECT_TO_BIT(esector);
  769. }
  770. count = update_sync_bits(device, sbnr, ebnr, mode);
  771. out:
  772. put_ldev(device);
  773. return count;
  774. }
  775. static
  776. struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr)
  777. {
  778. struct lc_element *e;
  779. struct bm_extent *bm_ext;
  780. int wakeup = 0;
  781. unsigned long rs_flags;
  782. spin_lock_irq(&device->al_lock);
  783. if (device->resync_locked > device->resync->nr_elements/2) {
  784. spin_unlock_irq(&device->al_lock);
  785. return NULL;
  786. }
  787. e = lc_get(device->resync, enr);
  788. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  789. if (bm_ext) {
  790. if (bm_ext->lce.lc_number != enr) {
  791. bm_ext->rs_left = drbd_bm_e_weight(device, enr);
  792. bm_ext->rs_failed = 0;
  793. lc_committed(device->resync);
  794. wakeup = 1;
  795. }
  796. if (bm_ext->lce.refcnt == 1)
  797. device->resync_locked++;
  798. set_bit(BME_NO_WRITES, &bm_ext->flags);
  799. }
  800. rs_flags = device->resync->flags;
  801. spin_unlock_irq(&device->al_lock);
  802. if (wakeup)
  803. wake_up(&device->al_wait);
  804. if (!bm_ext) {
  805. if (rs_flags & LC_STARVING)
  806. drbd_warn(device, "Have to wait for element"
  807. " (resync LRU too small?)\n");
  808. BUG_ON(rs_flags & LC_LOCKED);
  809. }
  810. return bm_ext;
  811. }
  812. static int _is_in_al(struct drbd_device *device, unsigned int enr)
  813. {
  814. int rv;
  815. spin_lock_irq(&device->al_lock);
  816. rv = lc_is_used(device->act_log, enr);
  817. spin_unlock_irq(&device->al_lock);
  818. return rv;
  819. }
  820. /**
  821. * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
  822. * @device: DRBD device.
  823. * @sector: The sector number.
  824. *
  825. * This functions sleeps on al_wait.
  826. *
  827. * Returns: %0 on success, -EINTR if interrupted.
  828. */
  829. int drbd_rs_begin_io(struct drbd_device *device, sector_t sector)
  830. {
  831. unsigned int enr = BM_SECT_TO_EXT(sector);
  832. struct bm_extent *bm_ext;
  833. int i, sig;
  834. bool sa;
  835. retry:
  836. sig = wait_event_interruptible(device->al_wait,
  837. (bm_ext = _bme_get(device, enr)));
  838. if (sig)
  839. return -EINTR;
  840. if (test_bit(BME_LOCKED, &bm_ext->flags))
  841. return 0;
  842. /* step aside only while we are above c-min-rate; unless disabled. */
  843. sa = drbd_rs_c_min_rate_throttle(device);
  844. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  845. sig = wait_event_interruptible(device->al_wait,
  846. !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) ||
  847. (sa && test_bit(BME_PRIORITY, &bm_ext->flags)));
  848. if (sig || (sa && test_bit(BME_PRIORITY, &bm_ext->flags))) {
  849. spin_lock_irq(&device->al_lock);
  850. if (lc_put(device->resync, &bm_ext->lce) == 0) {
  851. bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
  852. device->resync_locked--;
  853. wake_up(&device->al_wait);
  854. }
  855. spin_unlock_irq(&device->al_lock);
  856. if (sig)
  857. return -EINTR;
  858. if (schedule_timeout_interruptible(HZ/10))
  859. return -EINTR;
  860. goto retry;
  861. }
  862. }
  863. set_bit(BME_LOCKED, &bm_ext->flags);
  864. return 0;
  865. }
  866. /**
  867. * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
  868. * @peer_device: DRBD device.
  869. * @sector: The sector number.
  870. *
  871. * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
  872. * tries to set it to BME_LOCKED.
  873. *
  874. * Returns: %0 upon success, and -EAGAIN
  875. * if there is still application IO going on in this area.
  876. */
  877. int drbd_try_rs_begin_io(struct drbd_peer_device *peer_device, sector_t sector)
  878. {
  879. struct drbd_device *device = peer_device->device;
  880. unsigned int enr = BM_SECT_TO_EXT(sector);
  881. const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
  882. struct lc_element *e;
  883. struct bm_extent *bm_ext;
  884. int i;
  885. bool throttle = drbd_rs_should_slow_down(peer_device, sector, true);
  886. /* If we need to throttle, a half-locked (only marked BME_NO_WRITES,
  887. * not yet BME_LOCKED) extent needs to be kicked out explicitly if we
  888. * need to throttle. There is at most one such half-locked extent,
  889. * which is remembered in resync_wenr. */
  890. if (throttle && device->resync_wenr != enr)
  891. return -EAGAIN;
  892. spin_lock_irq(&device->al_lock);
  893. if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) {
  894. /* in case you have very heavy scattered io, it may
  895. * stall the syncer undefined if we give up the ref count
  896. * when we try again and requeue.
  897. *
  898. * if we don't give up the refcount, but the next time
  899. * we are scheduled this extent has been "synced" by new
  900. * application writes, we'd miss the lc_put on the
  901. * extent we keep the refcount on.
  902. * so we remembered which extent we had to try again, and
  903. * if the next requested one is something else, we do
  904. * the lc_put here...
  905. * we also have to wake_up
  906. */
  907. e = lc_find(device->resync, device->resync_wenr);
  908. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  909. if (bm_ext) {
  910. D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
  911. D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
  912. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  913. device->resync_wenr = LC_FREE;
  914. if (lc_put(device->resync, &bm_ext->lce) == 0) {
  915. bm_ext->flags = 0;
  916. device->resync_locked--;
  917. }
  918. wake_up(&device->al_wait);
  919. } else {
  920. drbd_alert(device, "LOGIC BUG\n");
  921. }
  922. }
  923. /* TRY. */
  924. e = lc_try_get(device->resync, enr);
  925. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  926. if (bm_ext) {
  927. if (test_bit(BME_LOCKED, &bm_ext->flags))
  928. goto proceed;
  929. if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
  930. device->resync_locked++;
  931. } else {
  932. /* we did set the BME_NO_WRITES,
  933. * but then could not set BME_LOCKED,
  934. * so we tried again.
  935. * drop the extra reference. */
  936. bm_ext->lce.refcnt--;
  937. D_ASSERT(device, bm_ext->lce.refcnt > 0);
  938. }
  939. goto check_al;
  940. } else {
  941. /* do we rather want to try later? */
  942. if (device->resync_locked > device->resync->nr_elements-3)
  943. goto try_again;
  944. /* Do or do not. There is no try. -- Yoda */
  945. e = lc_get(device->resync, enr);
  946. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  947. if (!bm_ext) {
  948. const unsigned long rs_flags = device->resync->flags;
  949. if (rs_flags & LC_STARVING)
  950. drbd_warn(device, "Have to wait for element"
  951. " (resync LRU too small?)\n");
  952. BUG_ON(rs_flags & LC_LOCKED);
  953. goto try_again;
  954. }
  955. if (bm_ext->lce.lc_number != enr) {
  956. bm_ext->rs_left = drbd_bm_e_weight(device, enr);
  957. bm_ext->rs_failed = 0;
  958. lc_committed(device->resync);
  959. wake_up(&device->al_wait);
  960. D_ASSERT(device, test_bit(BME_LOCKED, &bm_ext->flags) == 0);
  961. }
  962. set_bit(BME_NO_WRITES, &bm_ext->flags);
  963. D_ASSERT(device, bm_ext->lce.refcnt == 1);
  964. device->resync_locked++;
  965. goto check_al;
  966. }
  967. check_al:
  968. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  969. if (lc_is_used(device->act_log, al_enr+i))
  970. goto try_again;
  971. }
  972. set_bit(BME_LOCKED, &bm_ext->flags);
  973. proceed:
  974. device->resync_wenr = LC_FREE;
  975. spin_unlock_irq(&device->al_lock);
  976. return 0;
  977. try_again:
  978. if (bm_ext) {
  979. if (throttle) {
  980. D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
  981. D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
  982. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  983. device->resync_wenr = LC_FREE;
  984. if (lc_put(device->resync, &bm_ext->lce) == 0) {
  985. bm_ext->flags = 0;
  986. device->resync_locked--;
  987. }
  988. wake_up(&device->al_wait);
  989. } else
  990. device->resync_wenr = enr;
  991. }
  992. spin_unlock_irq(&device->al_lock);
  993. return -EAGAIN;
  994. }
  995. void drbd_rs_complete_io(struct drbd_device *device, sector_t sector)
  996. {
  997. unsigned int enr = BM_SECT_TO_EXT(sector);
  998. struct lc_element *e;
  999. struct bm_extent *bm_ext;
  1000. unsigned long flags;
  1001. spin_lock_irqsave(&device->al_lock, flags);
  1002. e = lc_find(device->resync, enr);
  1003. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1004. if (!bm_ext) {
  1005. spin_unlock_irqrestore(&device->al_lock, flags);
  1006. if (drbd_ratelimit())
  1007. drbd_err(device, "drbd_rs_complete_io() called, but extent not found\n");
  1008. return;
  1009. }
  1010. if (bm_ext->lce.refcnt == 0) {
  1011. spin_unlock_irqrestore(&device->al_lock, flags);
  1012. drbd_err(device, "drbd_rs_complete_io(,%llu [=%u]) called, "
  1013. "but refcnt is 0!?\n",
  1014. (unsigned long long)sector, enr);
  1015. return;
  1016. }
  1017. if (lc_put(device->resync, &bm_ext->lce) == 0) {
  1018. bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
  1019. device->resync_locked--;
  1020. wake_up(&device->al_wait);
  1021. }
  1022. spin_unlock_irqrestore(&device->al_lock, flags);
  1023. }
  1024. /**
  1025. * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
  1026. * @device: DRBD device.
  1027. */
  1028. void drbd_rs_cancel_all(struct drbd_device *device)
  1029. {
  1030. spin_lock_irq(&device->al_lock);
  1031. if (get_ldev_if_state(device, D_FAILED)) { /* Makes sure ->resync is there. */
  1032. lc_reset(device->resync);
  1033. put_ldev(device);
  1034. }
  1035. device->resync_locked = 0;
  1036. device->resync_wenr = LC_FREE;
  1037. spin_unlock_irq(&device->al_lock);
  1038. wake_up(&device->al_wait);
  1039. }
  1040. /**
  1041. * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
  1042. * @device: DRBD device.
  1043. *
  1044. * Returns: %0 upon success, -EAGAIN if at least one reference count was
  1045. * not zero.
  1046. */
  1047. int drbd_rs_del_all(struct drbd_device *device)
  1048. {
  1049. struct lc_element *e;
  1050. struct bm_extent *bm_ext;
  1051. int i;
  1052. spin_lock_irq(&device->al_lock);
  1053. if (get_ldev_if_state(device, D_FAILED)) {
  1054. /* ok, ->resync is there. */
  1055. for (i = 0; i < device->resync->nr_elements; i++) {
  1056. e = lc_element_by_index(device->resync, i);
  1057. bm_ext = lc_entry(e, struct bm_extent, lce);
  1058. if (bm_ext->lce.lc_number == LC_FREE)
  1059. continue;
  1060. if (bm_ext->lce.lc_number == device->resync_wenr) {
  1061. drbd_info(device, "dropping %u in drbd_rs_del_all, apparently"
  1062. " got 'synced' by application io\n",
  1063. device->resync_wenr);
  1064. D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
  1065. D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
  1066. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1067. device->resync_wenr = LC_FREE;
  1068. lc_put(device->resync, &bm_ext->lce);
  1069. }
  1070. if (bm_ext->lce.refcnt != 0) {
  1071. drbd_info(device, "Retrying drbd_rs_del_all() later. "
  1072. "refcnt=%d\n", bm_ext->lce.refcnt);
  1073. put_ldev(device);
  1074. spin_unlock_irq(&device->al_lock);
  1075. return -EAGAIN;
  1076. }
  1077. D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
  1078. D_ASSERT(device, !test_bit(BME_NO_WRITES, &bm_ext->flags));
  1079. lc_del(device->resync, &bm_ext->lce);
  1080. }
  1081. D_ASSERT(device, device->resync->used == 0);
  1082. put_ldev(device);
  1083. }
  1084. spin_unlock_irq(&device->al_lock);
  1085. wake_up(&device->al_wait);
  1086. return 0;
  1087. }