drbd_actlog.c 37 KB

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