drbd_actlog.c 38 KB

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