drbd_actlog.c 36 KB

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