drbd_actlog.c 37 KB

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