drbd_actlog.c 39 KB

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