drbd_actlog.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /*
  2. drbd_actlog.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/drbd.h>
  22. #include <linux/drbd_limits.h>
  23. #include <linux/dynamic_debug.h>
  24. #include "drbd_int.h"
  25. #include "drbd_wrappers.h"
  26. /* all fields on disc in big endian */
  27. struct __packed al_transaction_on_disk {
  28. /* don't we all like magic */
  29. __be32 magic;
  30. /* to identify the most recent transaction block
  31. * in the on disk ring buffer */
  32. __be32 tr_number;
  33. /* checksum on the full 4k block, with this field set to 0. */
  34. __be32 crc32c;
  35. /* type of transaction, special transaction types like:
  36. * purge-all, set-all-idle, set-all-active, ... to-be-defined */
  37. __be16 transaction_type;
  38. /* we currently allow only a few thousand extents,
  39. * so 16bit will be enough for the slot number. */
  40. /* how many updates in this transaction */
  41. __be16 n_updates;
  42. /* maximum slot number, "al-extents" in drbd.conf speak.
  43. * Having this in each transaction should make reconfiguration
  44. * of that parameter easier. */
  45. __be16 context_size;
  46. /* slot number the context starts with */
  47. __be16 context_start_slot_nr;
  48. /* Some reserved bytes. Expected usage is a 64bit counter of
  49. * sectors-written since device creation, and other data generation tag
  50. * supporting usage */
  51. __be32 __reserved[4];
  52. /* --- 36 byte used --- */
  53. /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
  54. * in one transaction, then use the remaining byte in the 4k block for
  55. * context information. "Flexible" number of updates per transaction
  56. * does not help, as we have to account for the case when all update
  57. * slots are used anyways, so it would only complicate code without
  58. * additional benefit.
  59. */
  60. __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
  61. /* but the extent number is 32bit, which at an extent size of 4 MiB
  62. * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
  63. __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
  64. /* --- 420 bytes used (36 + 64*6) --- */
  65. /* 4096 - 420 = 3676 = 919 * 4 */
  66. __be32 context[AL_CONTEXT_PER_TRANSACTION];
  67. };
  68. struct update_odbm_work {
  69. struct drbd_work w;
  70. unsigned int enr;
  71. };
  72. struct update_al_work {
  73. struct drbd_work w;
  74. struct completion event;
  75. int err;
  76. };
  77. struct drbd_atodb_wait {
  78. atomic_t count;
  79. struct completion io_done;
  80. struct drbd_conf *mdev;
  81. int error;
  82. };
  83. static int w_al_write_transaction(struct drbd_work *, int);
  84. static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
  85. struct drbd_backing_dev *bdev,
  86. struct page *page, sector_t sector,
  87. int rw, int size)
  88. {
  89. struct bio *bio;
  90. struct drbd_md_io md_io;
  91. int err;
  92. md_io.mdev = mdev;
  93. init_completion(&md_io.event);
  94. md_io.error = 0;
  95. if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
  96. rw |= REQ_FUA | REQ_FLUSH;
  97. rw |= REQ_SYNC;
  98. bio = bio_alloc_drbd(GFP_NOIO);
  99. bio->bi_bdev = bdev->md_bdev;
  100. bio->bi_sector = sector;
  101. err = -EIO;
  102. if (bio_add_page(bio, page, size, 0) != size)
  103. goto out;
  104. bio->bi_private = &md_io;
  105. bio->bi_end_io = drbd_md_io_complete;
  106. bio->bi_rw = rw;
  107. if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
  108. bio_endio(bio, -EIO);
  109. else
  110. submit_bio(rw, bio);
  111. wait_for_completion(&md_io.event);
  112. if (bio_flagged(bio, BIO_UPTODATE))
  113. err = md_io.error;
  114. out:
  115. bio_put(bio);
  116. return err;
  117. }
  118. int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
  119. sector_t sector, int rw)
  120. {
  121. int err;
  122. struct page *iop = mdev->md_io_page;
  123. D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
  124. BUG_ON(!bdev->md_bdev);
  125. dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
  126. current->comm, current->pid, __func__,
  127. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  128. if (sector < drbd_md_first_sector(bdev) ||
  129. sector + 7 > drbd_md_last_sector(bdev))
  130. dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
  131. current->comm, current->pid, __func__,
  132. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  133. err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
  134. if (err) {
  135. dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
  136. (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
  137. }
  138. return err;
  139. }
  140. static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
  141. {
  142. struct lc_element *al_ext;
  143. struct lc_element *tmp;
  144. int wake;
  145. spin_lock_irq(&mdev->al_lock);
  146. tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
  147. if (unlikely(tmp != NULL)) {
  148. struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
  149. if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
  150. wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
  151. spin_unlock_irq(&mdev->al_lock);
  152. if (wake)
  153. wake_up(&mdev->al_wait);
  154. return NULL;
  155. }
  156. }
  157. al_ext = lc_get(mdev->act_log, enr);
  158. spin_unlock_irq(&mdev->al_lock);
  159. return al_ext;
  160. }
  161. void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
  162. {
  163. unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
  164. struct lc_element *al_ext;
  165. struct update_al_work al_work;
  166. D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
  167. wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
  168. if (al_ext->lc_number != enr) {
  169. /* drbd_al_write_transaction(mdev,al_ext,enr);
  170. * recurses into generic_make_request(), which
  171. * disallows recursion, bios being serialized on the
  172. * current->bio_tail list now.
  173. * we have to delegate updates to the activity log
  174. * to the worker thread. */
  175. /* Serialize multiple transactions.
  176. * This uses test_and_set_bit, memory barrier is implicit.
  177. * Optimization potential:
  178. * first check for transaction number > old transaction number,
  179. * so not all waiters have to lock/unlock. */
  180. wait_event(mdev->al_wait, lc_try_lock_for_transaction(mdev->act_log));
  181. /* Double check: it may have been committed by someone else,
  182. * while we have been waiting for the lock. */
  183. if (al_ext->lc_number != enr) {
  184. init_completion(&al_work.event);
  185. al_work.w.cb = w_al_write_transaction;
  186. al_work.w.mdev = mdev;
  187. drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
  188. wait_for_completion(&al_work.event);
  189. mdev->al_writ_cnt++;
  190. spin_lock_irq(&mdev->al_lock);
  191. /* FIXME
  192. if (al_work.err)
  193. we need an "lc_cancel" here;
  194. */
  195. lc_committed(mdev->act_log);
  196. spin_unlock_irq(&mdev->al_lock);
  197. }
  198. lc_unlock(mdev->act_log);
  199. wake_up(&mdev->al_wait);
  200. }
  201. }
  202. void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
  203. {
  204. unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
  205. struct lc_element *extent;
  206. unsigned long flags;
  207. spin_lock_irqsave(&mdev->al_lock, flags);
  208. extent = lc_find(mdev->act_log, enr);
  209. if (!extent) {
  210. spin_unlock_irqrestore(&mdev->al_lock, flags);
  211. dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
  212. return;
  213. }
  214. if (lc_put(mdev->act_log, extent) == 0)
  215. wake_up(&mdev->al_wait);
  216. spin_unlock_irqrestore(&mdev->al_lock, flags);
  217. }
  218. #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
  219. /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
  220. * are still coupled, or assume too much about their relation.
  221. * Code below will not work if this is violated.
  222. * Will be cleaned up with some followup patch.
  223. */
  224. # error FIXME
  225. #endif
  226. static unsigned int al_extent_to_bm_page(unsigned int al_enr)
  227. {
  228. return al_enr >>
  229. /* bit to page */
  230. ((PAGE_SHIFT + 3) -
  231. /* al extent number to bit */
  232. (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
  233. }
  234. static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
  235. {
  236. return rs_enr >>
  237. /* bit to page */
  238. ((PAGE_SHIFT + 3) -
  239. /* al extent number to bit */
  240. (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
  241. }
  242. static int
  243. w_al_write_transaction(struct drbd_work *w, int unused)
  244. {
  245. struct update_al_work *aw = container_of(w, struct update_al_work, w);
  246. struct drbd_conf *mdev = w->mdev;
  247. struct al_transaction_on_disk *buffer;
  248. struct lc_element *e;
  249. sector_t sector;
  250. int i, mx;
  251. unsigned extent_nr;
  252. unsigned crc = 0;
  253. if (!get_ldev(mdev)) {
  254. dev_err(DEV, "disk is %s, cannot start al transaction\n",
  255. drbd_disk_str(mdev->state.disk));
  256. aw->err = -EIO;
  257. complete(&((struct update_al_work *)w)->event);
  258. return 0;
  259. }
  260. /* The bitmap write may have failed, causing a state change. */
  261. if (mdev->state.disk < D_INCONSISTENT) {
  262. dev_err(DEV,
  263. "disk is %s, cannot write al transaction\n",
  264. drbd_disk_str(mdev->state.disk));
  265. aw->err = -EIO;
  266. complete(&((struct update_al_work *)w)->event);
  267. put_ldev(mdev);
  268. return 0;
  269. }
  270. mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
  271. buffer = page_address(mdev->md_io_page);
  272. memset(buffer, 0, sizeof(*buffer));
  273. buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
  274. buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
  275. i = 0;
  276. /* Even though no one can start to change this list
  277. * once we set the LC_LOCKED -- from drbd_al_begin_io(),
  278. * lc_try_lock_for_transaction() --, someone may still
  279. * be in the process of changing it. */
  280. spin_lock_irq(&mdev->al_lock);
  281. list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
  282. if (i == AL_UPDATES_PER_TRANSACTION) {
  283. i++;
  284. break;
  285. }
  286. buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
  287. buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
  288. if (e->lc_number != LC_FREE)
  289. drbd_bm_mark_for_writeout(mdev,
  290. al_extent_to_bm_page(e->lc_number));
  291. i++;
  292. }
  293. spin_unlock_irq(&mdev->al_lock);
  294. BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
  295. buffer->n_updates = cpu_to_be16(i);
  296. for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
  297. buffer->update_slot_nr[i] = cpu_to_be16(-1);
  298. buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
  299. }
  300. buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
  301. buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
  302. mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
  303. mdev->act_log->nr_elements - mdev->al_tr_cycle);
  304. for (i = 0; i < mx; i++) {
  305. unsigned idx = mdev->al_tr_cycle + i;
  306. extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
  307. buffer->context[i] = cpu_to_be32(extent_nr);
  308. }
  309. for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
  310. buffer->context[i] = cpu_to_be32(LC_FREE);
  311. mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
  312. if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
  313. mdev->al_tr_cycle = 0;
  314. sector = mdev->ldev->md.md_offset
  315. + mdev->ldev->md.al_offset
  316. + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
  317. crc = crc32c(0, buffer, 4096);
  318. buffer->crc32c = cpu_to_be32(crc);
  319. if (drbd_bm_write_hinted(mdev))
  320. aw->err = -EIO;
  321. /* drbd_chk_io_error done already */
  322. else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
  323. aw->err = -EIO;
  324. drbd_chk_io_error(mdev, 1, true);
  325. } else {
  326. /* advance ringbuffer position and transaction counter */
  327. mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
  328. mdev->al_tr_number++;
  329. }
  330. mutex_unlock(&mdev->md_io_mutex);
  331. complete(&((struct update_al_work *)w)->event);
  332. put_ldev(mdev);
  333. return 0;
  334. }
  335. /* FIXME
  336. * reading of the activity log,
  337. * and potentially dirtying of the affected bitmap regions,
  338. * should be done from userland only.
  339. * DRBD would simply always attach with an empty activity log,
  340. * and refuse to attach to something that looks like a crashed primary.
  341. */
  342. /**
  343. * drbd_al_read_tr() - Read a single transaction from the on disk activity log
  344. * @mdev: DRBD device.
  345. * @bdev: Block device to read form.
  346. * @b: pointer to an al_transaction.
  347. * @index: On disk slot of the transaction to read.
  348. *
  349. * Returns -1 on IO error, 0 on checksum error and 1 upon success.
  350. */
  351. static int drbd_al_read_tr(struct drbd_conf *mdev,
  352. struct drbd_backing_dev *bdev,
  353. int index)
  354. {
  355. struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
  356. sector_t sector;
  357. u32 crc;
  358. sector = bdev->md.md_offset
  359. + bdev->md.al_offset
  360. + index * (MD_BLOCK_SIZE>>9);
  361. /* Dont process error normally,
  362. * as this is done before disk is attached! */
  363. if (drbd_md_sync_page_io(mdev, bdev, sector, READ))
  364. return -1;
  365. if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
  366. return 0;
  367. if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
  368. return 0;
  369. if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
  370. return 0;
  371. if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
  372. return 0;
  373. crc = be32_to_cpu(b->crc32c);
  374. b->crc32c = 0;
  375. if (!expect(crc == crc32c(0, b, 4096)))
  376. return 0;
  377. return 1;
  378. }
  379. /**
  380. * drbd_al_read_log() - Restores the activity log from its on disk representation.
  381. * @mdev: DRBD device.
  382. * @bdev: Block device to read form.
  383. *
  384. * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
  385. */
  386. int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  387. {
  388. struct al_transaction_on_disk *b;
  389. int i;
  390. int rv;
  391. int mx;
  392. int active_extents = 0;
  393. int transactions = 0;
  394. int found_valid = 0;
  395. int from = 0;
  396. int to = 0;
  397. u32 from_tnr = 0;
  398. u32 to_tnr = 0;
  399. u32 cnr;
  400. /* Note that this is expected to be called with a newly created,
  401. * clean and all unused activity log of the "expected size".
  402. */
  403. /* lock out all other meta data io for now,
  404. * and make sure the page is mapped.
  405. */
  406. mutex_lock(&mdev->md_io_mutex);
  407. b = page_address(mdev->md_io_page);
  408. /* Always use the full ringbuffer space for now.
  409. * possible optimization: read in all of it,
  410. * then scan the in-memory pages. */
  411. mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
  412. /* Find the valid transaction in the log */
  413. for (i = 0; i < mx; i++) {
  414. rv = drbd_al_read_tr(mdev, bdev, i);
  415. /* invalid data in that block */
  416. if (rv == 0)
  417. continue;
  418. /* IO error */
  419. if (rv == -1) {
  420. mutex_unlock(&mdev->md_io_mutex);
  421. return 0;
  422. }
  423. cnr = be32_to_cpu(b->tr_number);
  424. if (++found_valid == 1) {
  425. from = i;
  426. to = i;
  427. from_tnr = cnr;
  428. to_tnr = cnr;
  429. continue;
  430. }
  431. D_ASSERT(cnr != to_tnr);
  432. D_ASSERT(cnr != from_tnr);
  433. if ((int)cnr - (int)from_tnr < 0) {
  434. D_ASSERT(from_tnr - cnr + i - from == mx);
  435. from = i;
  436. from_tnr = cnr;
  437. }
  438. if ((int)cnr - (int)to_tnr > 0) {
  439. D_ASSERT(cnr - to_tnr == i - to);
  440. to = i;
  441. to_tnr = cnr;
  442. }
  443. }
  444. if (!found_valid) {
  445. dev_warn(DEV, "No usable activity log found.\n");
  446. mutex_unlock(&mdev->md_io_mutex);
  447. return 1;
  448. }
  449. /* Read the valid transactions.
  450. * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
  451. i = from;
  452. while (1) {
  453. struct lc_element *e;
  454. unsigned j, n, slot, extent_nr;
  455. rv = drbd_al_read_tr(mdev, bdev, i);
  456. if (!expect(rv != 0))
  457. goto cancel;
  458. if (rv == -1) {
  459. mutex_unlock(&mdev->md_io_mutex);
  460. return 0;
  461. }
  462. /* deal with different transaction types.
  463. * not yet implemented */
  464. if (!expect(b->transaction_type == 0))
  465. goto cancel;
  466. /* on the fly re-create/resize activity log?
  467. * will be a special transaction type flag. */
  468. if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
  469. goto cancel;
  470. if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
  471. goto cancel;
  472. /* We are the only user of the activity log right now,
  473. * don't actually need to take that lock. */
  474. spin_lock_irq(&mdev->al_lock);
  475. /* first, apply the context, ... */
  476. for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
  477. j < AL_CONTEXT_PER_TRANSACTION &&
  478. slot < mdev->act_log->nr_elements; j++, slot++) {
  479. extent_nr = be32_to_cpu(b->context[j]);
  480. e = lc_element_by_index(mdev->act_log, slot);
  481. if (e->lc_number != extent_nr) {
  482. if (extent_nr != LC_FREE)
  483. active_extents++;
  484. else
  485. active_extents--;
  486. }
  487. lc_set(mdev->act_log, extent_nr, slot);
  488. }
  489. /* ... then apply the updates,
  490. * which override the context information.
  491. * drbd_al_read_tr already did the rangecheck
  492. * on n <= AL_UPDATES_PER_TRANSACTION */
  493. n = be16_to_cpu(b->n_updates);
  494. for (j = 0; j < n; j++) {
  495. slot = be16_to_cpu(b->update_slot_nr[j]);
  496. extent_nr = be32_to_cpu(b->update_extent_nr[j]);
  497. if (!expect(slot < mdev->act_log->nr_elements))
  498. break;
  499. e = lc_element_by_index(mdev->act_log, slot);
  500. if (e->lc_number != extent_nr) {
  501. if (extent_nr != LC_FREE)
  502. active_extents++;
  503. else
  504. active_extents--;
  505. }
  506. lc_set(mdev->act_log, extent_nr, slot);
  507. }
  508. spin_unlock_irq(&mdev->al_lock);
  509. transactions++;
  510. cancel:
  511. if (i == to)
  512. break;
  513. i++;
  514. if (i >= mx)
  515. i = 0;
  516. }
  517. mdev->al_tr_number = to_tnr+1;
  518. mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
  519. /* ok, we are done with it */
  520. mutex_unlock(&mdev->md_io_mutex);
  521. dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
  522. transactions, active_extents);
  523. return 1;
  524. }
  525. /**
  526. * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
  527. * @mdev: DRBD device.
  528. */
  529. void drbd_al_apply_to_bm(struct drbd_conf *mdev)
  530. {
  531. unsigned int enr;
  532. unsigned long add = 0;
  533. char ppb[10];
  534. int i, tmp;
  535. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  536. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  537. enr = lc_element_by_index(mdev->act_log, i)->lc_number;
  538. if (enr == LC_FREE)
  539. continue;
  540. tmp = drbd_bm_ALe_set_all(mdev, enr);
  541. dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
  542. add += tmp;
  543. }
  544. lc_unlock(mdev->act_log);
  545. wake_up(&mdev->al_wait);
  546. dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
  547. ppsize(ppb, Bit2KB(add)));
  548. }
  549. static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
  550. {
  551. int rv;
  552. spin_lock_irq(&mdev->al_lock);
  553. rv = (al_ext->refcnt == 0);
  554. if (likely(rv))
  555. lc_del(mdev->act_log, al_ext);
  556. spin_unlock_irq(&mdev->al_lock);
  557. return rv;
  558. }
  559. /**
  560. * drbd_al_shrink() - Removes all active extents form the activity log
  561. * @mdev: DRBD device.
  562. *
  563. * Removes all active extents form the activity log, waiting until
  564. * the reference count of each entry dropped to 0 first, of course.
  565. *
  566. * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
  567. */
  568. void drbd_al_shrink(struct drbd_conf *mdev)
  569. {
  570. struct lc_element *al_ext;
  571. int i;
  572. D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
  573. for (i = 0; i < mdev->act_log->nr_elements; i++) {
  574. al_ext = lc_element_by_index(mdev->act_log, i);
  575. if (al_ext->lc_number == LC_FREE)
  576. continue;
  577. wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
  578. }
  579. wake_up(&mdev->al_wait);
  580. }
  581. static int w_update_odbm(struct drbd_work *w, int unused)
  582. {
  583. struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
  584. struct drbd_conf *mdev = w->mdev;
  585. struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
  586. if (!get_ldev(mdev)) {
  587. if (__ratelimit(&drbd_ratelimit_state))
  588. dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
  589. kfree(udw);
  590. return 0;
  591. }
  592. drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
  593. put_ldev(mdev);
  594. kfree(udw);
  595. if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
  596. switch (mdev->state.conn) {
  597. case C_SYNC_SOURCE: case C_SYNC_TARGET:
  598. case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
  599. drbd_resync_finished(mdev);
  600. default:
  601. /* nothing to do */
  602. break;
  603. }
  604. }
  605. drbd_bcast_event(mdev, &sib);
  606. return 0;
  607. }
  608. /* ATTENTION. The AL's extents are 4MB each, while the extents in the
  609. * resync LRU-cache are 16MB each.
  610. * The caller of this function has to hold an get_ldev() reference.
  611. *
  612. * TODO will be obsoleted once we have a caching lru of the on disk bitmap
  613. */
  614. static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
  615. int count, int success)
  616. {
  617. struct lc_element *e;
  618. struct update_odbm_work *udw;
  619. unsigned int enr;
  620. D_ASSERT(atomic_read(&mdev->local_cnt));
  621. /* I simply assume that a sector/size pair never crosses
  622. * a 16 MB extent border. (Currently this is true...) */
  623. enr = BM_SECT_TO_EXT(sector);
  624. e = lc_get(mdev->resync, enr);
  625. if (e) {
  626. struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
  627. if (ext->lce.lc_number == enr) {
  628. if (success)
  629. ext->rs_left -= count;
  630. else
  631. ext->rs_failed += count;
  632. if (ext->rs_left < ext->rs_failed) {
  633. dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
  634. "rs_failed=%d count=%d\n",
  635. (unsigned long long)sector,
  636. ext->lce.lc_number, ext->rs_left,
  637. ext->rs_failed, count);
  638. dump_stack();
  639. lc_put(mdev->resync, &ext->lce);
  640. conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
  641. return;
  642. }
  643. } else {
  644. /* Normally this element should be in the cache,
  645. * since drbd_rs_begin_io() pulled it already in.
  646. *
  647. * But maybe an application write finished, and we set
  648. * something outside the resync lru_cache in sync.
  649. */
  650. int rs_left = drbd_bm_e_weight(mdev, enr);
  651. if (ext->flags != 0) {
  652. dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
  653. " -> %d[%u;00]\n",
  654. ext->lce.lc_number, ext->rs_left,
  655. ext->flags, enr, rs_left);
  656. ext->flags = 0;
  657. }
  658. if (ext->rs_failed) {
  659. dev_warn(DEV, "Kicking resync_lru element enr=%u "
  660. "out with rs_failed=%d\n",
  661. ext->lce.lc_number, ext->rs_failed);
  662. }
  663. ext->rs_left = rs_left;
  664. ext->rs_failed = success ? 0 : count;
  665. /* we don't keep a persistent log of the resync lru,
  666. * we can commit any change right away. */
  667. lc_committed(mdev->resync);
  668. }
  669. lc_put(mdev->resync, &ext->lce);
  670. /* no race, we are within the al_lock! */
  671. if (ext->rs_left == ext->rs_failed) {
  672. ext->rs_failed = 0;
  673. udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
  674. if (udw) {
  675. udw->enr = ext->lce.lc_number;
  676. udw->w.cb = w_update_odbm;
  677. udw->w.mdev = mdev;
  678. drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
  679. } else {
  680. dev_warn(DEV, "Could not kmalloc an udw\n");
  681. }
  682. }
  683. } else {
  684. dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
  685. mdev->resync_locked,
  686. mdev->resync->nr_elements,
  687. mdev->resync->flags);
  688. }
  689. }
  690. void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
  691. {
  692. unsigned long now = jiffies;
  693. unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
  694. int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
  695. if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
  696. if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
  697. mdev->state.conn != C_PAUSED_SYNC_T &&
  698. mdev->state.conn != C_PAUSED_SYNC_S) {
  699. mdev->rs_mark_time[next] = now;
  700. mdev->rs_mark_left[next] = still_to_go;
  701. mdev->rs_last_mark = next;
  702. }
  703. }
  704. }
  705. /* clear the bit corresponding to the piece of storage in question:
  706. * size byte of data starting from sector. Only clear a bits of the affected
  707. * one ore more _aligned_ BM_BLOCK_SIZE blocks.
  708. *
  709. * called by worker on C_SYNC_TARGET and receiver on SyncSource.
  710. *
  711. */
  712. void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
  713. const char *file, const unsigned int line)
  714. {
  715. /* Is called from worker and receiver context _only_ */
  716. unsigned long sbnr, ebnr, lbnr;
  717. unsigned long count = 0;
  718. sector_t esector, nr_sectors;
  719. int wake_up = 0;
  720. unsigned long flags;
  721. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  722. dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
  723. (unsigned long long)sector, size);
  724. return;
  725. }
  726. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  727. esector = sector + (size >> 9) - 1;
  728. if (!expect(sector < nr_sectors))
  729. return;
  730. if (!expect(esector < nr_sectors))
  731. esector = nr_sectors - 1;
  732. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  733. /* we clear it (in sync).
  734. * round up start sector, round down end sector. we make sure we only
  735. * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
  736. if (unlikely(esector < BM_SECT_PER_BIT-1))
  737. return;
  738. if (unlikely(esector == (nr_sectors-1)))
  739. ebnr = lbnr;
  740. else
  741. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  742. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  743. if (sbnr > ebnr)
  744. return;
  745. /*
  746. * ok, (capacity & 7) != 0 sometimes, but who cares...
  747. * we count rs_{total,left} in bits, not sectors.
  748. */
  749. count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
  750. if (count && get_ldev(mdev)) {
  751. drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
  752. spin_lock_irqsave(&mdev->al_lock, flags);
  753. drbd_try_clear_on_disk_bm(mdev, sector, count, true);
  754. spin_unlock_irqrestore(&mdev->al_lock, flags);
  755. /* just wake_up unconditional now, various lc_chaged(),
  756. * lc_put() in drbd_try_clear_on_disk_bm(). */
  757. wake_up = 1;
  758. put_ldev(mdev);
  759. }
  760. if (wake_up)
  761. wake_up(&mdev->al_wait);
  762. }
  763. /*
  764. * this is intended to set one request worth of data out of sync.
  765. * affects at least 1 bit,
  766. * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
  767. *
  768. * called by tl_clear and drbd_send_dblock (==drbd_make_request).
  769. * so this can be _any_ process.
  770. */
  771. int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
  772. const char *file, const unsigned int line)
  773. {
  774. unsigned long sbnr, ebnr, lbnr, flags;
  775. sector_t esector, nr_sectors;
  776. unsigned int enr, count = 0;
  777. struct lc_element *e;
  778. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  779. dev_err(DEV, "sector: %llus, size: %d\n",
  780. (unsigned long long)sector, size);
  781. return 0;
  782. }
  783. if (!get_ldev(mdev))
  784. return 0; /* no disk, no metadata, no bitmap to set bits in */
  785. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  786. esector = sector + (size >> 9) - 1;
  787. if (!expect(sector < nr_sectors))
  788. goto out;
  789. if (!expect(esector < nr_sectors))
  790. esector = nr_sectors - 1;
  791. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  792. /* we set it out of sync,
  793. * we do not need to round anything here */
  794. sbnr = BM_SECT_TO_BIT(sector);
  795. ebnr = BM_SECT_TO_BIT(esector);
  796. /* ok, (capacity & 7) != 0 sometimes, but who cares...
  797. * we count rs_{total,left} in bits, not sectors. */
  798. spin_lock_irqsave(&mdev->al_lock, flags);
  799. count = drbd_bm_set_bits(mdev, sbnr, ebnr);
  800. enr = BM_SECT_TO_EXT(sector);
  801. e = lc_find(mdev->resync, enr);
  802. if (e)
  803. lc_entry(e, struct bm_extent, lce)->rs_left += count;
  804. spin_unlock_irqrestore(&mdev->al_lock, flags);
  805. out:
  806. put_ldev(mdev);
  807. return count;
  808. }
  809. static
  810. struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
  811. {
  812. struct lc_element *e;
  813. struct bm_extent *bm_ext;
  814. int wakeup = 0;
  815. unsigned long rs_flags;
  816. spin_lock_irq(&mdev->al_lock);
  817. if (mdev->resync_locked > mdev->resync->nr_elements/2) {
  818. spin_unlock_irq(&mdev->al_lock);
  819. return NULL;
  820. }
  821. e = lc_get(mdev->resync, enr);
  822. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  823. if (bm_ext) {
  824. if (bm_ext->lce.lc_number != enr) {
  825. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  826. bm_ext->rs_failed = 0;
  827. lc_committed(mdev->resync);
  828. wakeup = 1;
  829. }
  830. if (bm_ext->lce.refcnt == 1)
  831. mdev->resync_locked++;
  832. set_bit(BME_NO_WRITES, &bm_ext->flags);
  833. }
  834. rs_flags = mdev->resync->flags;
  835. spin_unlock_irq(&mdev->al_lock);
  836. if (wakeup)
  837. wake_up(&mdev->al_wait);
  838. if (!bm_ext) {
  839. if (rs_flags & LC_STARVING)
  840. dev_warn(DEV, "Have to wait for element"
  841. " (resync LRU too small?)\n");
  842. BUG_ON(rs_flags & LC_LOCKED);
  843. }
  844. return bm_ext;
  845. }
  846. static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
  847. {
  848. int rv;
  849. spin_lock_irq(&mdev->al_lock);
  850. rv = lc_is_used(mdev->act_log, enr);
  851. spin_unlock_irq(&mdev->al_lock);
  852. return rv;
  853. }
  854. /**
  855. * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
  856. * @mdev: DRBD device.
  857. * @sector: The sector number.
  858. *
  859. * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
  860. */
  861. int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  862. {
  863. unsigned int enr = BM_SECT_TO_EXT(sector);
  864. struct bm_extent *bm_ext;
  865. int i, sig;
  866. int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
  867. 200 times -> 20 seconds. */
  868. retry:
  869. sig = wait_event_interruptible(mdev->al_wait,
  870. (bm_ext = _bme_get(mdev, enr)));
  871. if (sig)
  872. return -EINTR;
  873. if (test_bit(BME_LOCKED, &bm_ext->flags))
  874. return 0;
  875. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  876. sig = wait_event_interruptible(mdev->al_wait,
  877. !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
  878. test_bit(BME_PRIORITY, &bm_ext->flags));
  879. if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
  880. spin_lock_irq(&mdev->al_lock);
  881. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  882. bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
  883. mdev->resync_locked--;
  884. wake_up(&mdev->al_wait);
  885. }
  886. spin_unlock_irq(&mdev->al_lock);
  887. if (sig)
  888. return -EINTR;
  889. if (schedule_timeout_interruptible(HZ/10))
  890. return -EINTR;
  891. if (sa && --sa == 0)
  892. dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
  893. "Resync stalled?\n");
  894. goto retry;
  895. }
  896. }
  897. set_bit(BME_LOCKED, &bm_ext->flags);
  898. return 0;
  899. }
  900. /**
  901. * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
  902. * @mdev: DRBD device.
  903. * @sector: The sector number.
  904. *
  905. * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
  906. * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
  907. * if there is still application IO going on in this area.
  908. */
  909. int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
  910. {
  911. unsigned int enr = BM_SECT_TO_EXT(sector);
  912. const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
  913. struct lc_element *e;
  914. struct bm_extent *bm_ext;
  915. int i;
  916. spin_lock_irq(&mdev->al_lock);
  917. if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
  918. /* in case you have very heavy scattered io, it may
  919. * stall the syncer undefined if we give up the ref count
  920. * when we try again and requeue.
  921. *
  922. * if we don't give up the refcount, but the next time
  923. * we are scheduled this extent has been "synced" by new
  924. * application writes, we'd miss the lc_put on the
  925. * extent we keep the refcount on.
  926. * so we remembered which extent we had to try again, and
  927. * if the next requested one is something else, we do
  928. * the lc_put here...
  929. * we also have to wake_up
  930. */
  931. e = lc_find(mdev->resync, mdev->resync_wenr);
  932. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  933. if (bm_ext) {
  934. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  935. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  936. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  937. mdev->resync_wenr = LC_FREE;
  938. if (lc_put(mdev->resync, &bm_ext->lce) == 0)
  939. mdev->resync_locked--;
  940. wake_up(&mdev->al_wait);
  941. } else {
  942. dev_alert(DEV, "LOGIC BUG\n");
  943. }
  944. }
  945. /* TRY. */
  946. e = lc_try_get(mdev->resync, enr);
  947. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  948. if (bm_ext) {
  949. if (test_bit(BME_LOCKED, &bm_ext->flags))
  950. goto proceed;
  951. if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
  952. mdev->resync_locked++;
  953. } else {
  954. /* we did set the BME_NO_WRITES,
  955. * but then could not set BME_LOCKED,
  956. * so we tried again.
  957. * drop the extra reference. */
  958. bm_ext->lce.refcnt--;
  959. D_ASSERT(bm_ext->lce.refcnt > 0);
  960. }
  961. goto check_al;
  962. } else {
  963. /* do we rather want to try later? */
  964. if (mdev->resync_locked > mdev->resync->nr_elements-3)
  965. goto try_again;
  966. /* Do or do not. There is no try. -- Yoda */
  967. e = lc_get(mdev->resync, enr);
  968. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  969. if (!bm_ext) {
  970. const unsigned long rs_flags = mdev->resync->flags;
  971. if (rs_flags & LC_STARVING)
  972. dev_warn(DEV, "Have to wait for element"
  973. " (resync LRU too small?)\n");
  974. BUG_ON(rs_flags & LC_LOCKED);
  975. goto try_again;
  976. }
  977. if (bm_ext->lce.lc_number != enr) {
  978. bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
  979. bm_ext->rs_failed = 0;
  980. lc_committed(mdev->resync);
  981. wake_up(&mdev->al_wait);
  982. D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
  983. }
  984. set_bit(BME_NO_WRITES, &bm_ext->flags);
  985. D_ASSERT(bm_ext->lce.refcnt == 1);
  986. mdev->resync_locked++;
  987. goto check_al;
  988. }
  989. check_al:
  990. for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
  991. if (lc_is_used(mdev->act_log, al_enr+i))
  992. goto try_again;
  993. }
  994. set_bit(BME_LOCKED, &bm_ext->flags);
  995. proceed:
  996. mdev->resync_wenr = LC_FREE;
  997. spin_unlock_irq(&mdev->al_lock);
  998. return 0;
  999. try_again:
  1000. if (bm_ext)
  1001. mdev->resync_wenr = enr;
  1002. spin_unlock_irq(&mdev->al_lock);
  1003. return -EAGAIN;
  1004. }
  1005. void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
  1006. {
  1007. unsigned int enr = BM_SECT_TO_EXT(sector);
  1008. struct lc_element *e;
  1009. struct bm_extent *bm_ext;
  1010. unsigned long flags;
  1011. spin_lock_irqsave(&mdev->al_lock, flags);
  1012. e = lc_find(mdev->resync, enr);
  1013. bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
  1014. if (!bm_ext) {
  1015. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1016. if (__ratelimit(&drbd_ratelimit_state))
  1017. dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
  1018. return;
  1019. }
  1020. if (bm_ext->lce.refcnt == 0) {
  1021. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1022. dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
  1023. "but refcnt is 0!?\n",
  1024. (unsigned long long)sector, enr);
  1025. return;
  1026. }
  1027. if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
  1028. bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
  1029. mdev->resync_locked--;
  1030. wake_up(&mdev->al_wait);
  1031. }
  1032. spin_unlock_irqrestore(&mdev->al_lock, flags);
  1033. }
  1034. /**
  1035. * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
  1036. * @mdev: DRBD device.
  1037. */
  1038. void drbd_rs_cancel_all(struct drbd_conf *mdev)
  1039. {
  1040. spin_lock_irq(&mdev->al_lock);
  1041. if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
  1042. lc_reset(mdev->resync);
  1043. put_ldev(mdev);
  1044. }
  1045. mdev->resync_locked = 0;
  1046. mdev->resync_wenr = LC_FREE;
  1047. spin_unlock_irq(&mdev->al_lock);
  1048. wake_up(&mdev->al_wait);
  1049. }
  1050. /**
  1051. * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
  1052. * @mdev: DRBD device.
  1053. *
  1054. * Returns 0 upon success, -EAGAIN if at least one reference count was
  1055. * not zero.
  1056. */
  1057. int drbd_rs_del_all(struct drbd_conf *mdev)
  1058. {
  1059. struct lc_element *e;
  1060. struct bm_extent *bm_ext;
  1061. int i;
  1062. spin_lock_irq(&mdev->al_lock);
  1063. if (get_ldev_if_state(mdev, D_FAILED)) {
  1064. /* ok, ->resync is there. */
  1065. for (i = 0; i < mdev->resync->nr_elements; i++) {
  1066. e = lc_element_by_index(mdev->resync, i);
  1067. bm_ext = lc_entry(e, struct bm_extent, lce);
  1068. if (bm_ext->lce.lc_number == LC_FREE)
  1069. continue;
  1070. if (bm_ext->lce.lc_number == mdev->resync_wenr) {
  1071. dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
  1072. " got 'synced' by application io\n",
  1073. mdev->resync_wenr);
  1074. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1075. D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
  1076. clear_bit(BME_NO_WRITES, &bm_ext->flags);
  1077. mdev->resync_wenr = LC_FREE;
  1078. lc_put(mdev->resync, &bm_ext->lce);
  1079. }
  1080. if (bm_ext->lce.refcnt != 0) {
  1081. dev_info(DEV, "Retrying drbd_rs_del_all() later. "
  1082. "refcnt=%d\n", bm_ext->lce.refcnt);
  1083. put_ldev(mdev);
  1084. spin_unlock_irq(&mdev->al_lock);
  1085. return -EAGAIN;
  1086. }
  1087. D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
  1088. D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
  1089. lc_del(mdev->resync, &bm_ext->lce);
  1090. }
  1091. D_ASSERT(mdev->resync->used == 0);
  1092. put_ldev(mdev);
  1093. }
  1094. spin_unlock_irq(&mdev->al_lock);
  1095. return 0;
  1096. }
  1097. /**
  1098. * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
  1099. * @mdev: DRBD device.
  1100. * @sector: The sector number.
  1101. * @size: Size of failed IO operation, in byte.
  1102. */
  1103. void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
  1104. {
  1105. /* Is called from worker and receiver context _only_ */
  1106. unsigned long sbnr, ebnr, lbnr;
  1107. unsigned long count;
  1108. sector_t esector, nr_sectors;
  1109. int wake_up = 0;
  1110. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  1111. dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
  1112. (unsigned long long)sector, size);
  1113. return;
  1114. }
  1115. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  1116. esector = sector + (size >> 9) - 1;
  1117. if (!expect(sector < nr_sectors))
  1118. return;
  1119. if (!expect(esector < nr_sectors))
  1120. esector = nr_sectors - 1;
  1121. lbnr = BM_SECT_TO_BIT(nr_sectors-1);
  1122. /*
  1123. * round up start sector, round down end sector. we make sure we only
  1124. * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
  1125. if (unlikely(esector < BM_SECT_PER_BIT-1))
  1126. return;
  1127. if (unlikely(esector == (nr_sectors-1)))
  1128. ebnr = lbnr;
  1129. else
  1130. ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
  1131. sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
  1132. if (sbnr > ebnr)
  1133. return;
  1134. /*
  1135. * ok, (capacity & 7) != 0 sometimes, but who cares...
  1136. * we count rs_{total,left} in bits, not sectors.
  1137. */
  1138. spin_lock_irq(&mdev->al_lock);
  1139. count = drbd_bm_count_bits(mdev, sbnr, ebnr);
  1140. if (count) {
  1141. mdev->rs_failed += count;
  1142. if (get_ldev(mdev)) {
  1143. drbd_try_clear_on_disk_bm(mdev, sector, count, false);
  1144. put_ldev(mdev);
  1145. }
  1146. /* just wake_up unconditional now, various lc_chaged(),
  1147. * lc_put() in drbd_try_clear_on_disk_bm(). */
  1148. wake_up = 1;
  1149. }
  1150. spin_unlock_irq(&mdev->al_lock);
  1151. if (wake_up)
  1152. wake_up(&mdev->al_wait);
  1153. }