drbd_actlog.c 34 KB

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