drbd_actlog.c 37 KB

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