drbd_actlog.c 36 KB

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