drbd_actlog.c 36 KB

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