drbd_req.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. drbd_req.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-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/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/drbd.h>
  22. #include "drbd_int.h"
  23. #include "drbd_req.h"
  24. /* Update disk stats at start of I/O request */
  25. static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req, struct bio *bio)
  26. {
  27. const int rw = bio_data_dir(bio);
  28. int cpu;
  29. cpu = part_stat_lock();
  30. part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
  31. part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
  32. part_inc_in_flight(&mdev->vdisk->part0, rw);
  33. part_stat_unlock();
  34. }
  35. /* Update disk stats when completing request upwards */
  36. static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
  37. {
  38. int rw = bio_data_dir(req->master_bio);
  39. unsigned long duration = jiffies - req->start_time;
  40. int cpu;
  41. cpu = part_stat_lock();
  42. part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
  43. part_round_stats(cpu, &mdev->vdisk->part0);
  44. part_dec_in_flight(&mdev->vdisk->part0, rw);
  45. part_stat_unlock();
  46. }
  47. static struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
  48. struct bio *bio_src)
  49. {
  50. struct drbd_request *req;
  51. req = mempool_alloc(drbd_request_mempool, GFP_NOIO);
  52. if (!req)
  53. return NULL;
  54. drbd_req_make_private_bio(req, bio_src);
  55. req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
  56. req->mdev = mdev;
  57. req->master_bio = bio_src;
  58. req->epoch = 0;
  59. drbd_clear_interval(&req->i);
  60. req->i.sector = bio_src->bi_sector;
  61. req->i.size = bio_src->bi_size;
  62. INIT_LIST_HEAD(&req->tl_requests);
  63. INIT_LIST_HEAD(&req->w.list);
  64. return req;
  65. }
  66. static void drbd_req_free(struct drbd_request *req)
  67. {
  68. mempool_free(req, drbd_request_mempool);
  69. }
  70. /* rw is bio_data_dir(), only READ or WRITE */
  71. static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw)
  72. {
  73. const unsigned long s = req->rq_state;
  74. /* remove it from the transfer log.
  75. * well, only if it had been there in the first
  76. * place... if it had not (local only or conflicting
  77. * and never sent), it should still be "empty" as
  78. * initialized in drbd_req_new(), so we can list_del() it
  79. * here unconditionally */
  80. list_del(&req->tl_requests);
  81. /* if it was a write, we may have to set the corresponding
  82. * bit(s) out-of-sync first. If it had a local part, we need to
  83. * release the reference to the activity log. */
  84. if (rw == WRITE) {
  85. /* Set out-of-sync unless both OK flags are set
  86. * (local only or remote failed).
  87. * Other places where we set out-of-sync:
  88. * READ with local io-error */
  89. if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
  90. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  91. if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
  92. drbd_set_in_sync(mdev, req->i.sector, req->i.size);
  93. /* one might be tempted to move the drbd_al_complete_io
  94. * to the local io completion callback drbd_endio_pri.
  95. * but, if this was a mirror write, we may only
  96. * drbd_al_complete_io after this is RQ_NET_DONE,
  97. * otherwise the extent could be dropped from the al
  98. * before it has actually been written on the peer.
  99. * if we crash before our peer knows about the request,
  100. * but after the extent has been dropped from the al,
  101. * we would forget to resync the corresponding extent.
  102. */
  103. if (s & RQ_LOCAL_MASK) {
  104. if (get_ldev_if_state(mdev, D_FAILED)) {
  105. if (s & RQ_IN_ACT_LOG)
  106. drbd_al_complete_io(mdev, req->i.sector);
  107. put_ldev(mdev);
  108. } else if (__ratelimit(&drbd_ratelimit_state)) {
  109. dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu), "
  110. "but my Disk seems to have failed :(\n",
  111. (unsigned long long) req->i.sector);
  112. }
  113. }
  114. }
  115. drbd_req_free(req);
  116. }
  117. static void queue_barrier(struct drbd_conf *mdev)
  118. {
  119. struct drbd_tl_epoch *b;
  120. /* We are within the req_lock. Once we queued the barrier for sending,
  121. * we set the CREATE_BARRIER bit. It is cleared as soon as a new
  122. * barrier/epoch object is added. This is the only place this bit is
  123. * set. It indicates that the barrier for this epoch is already queued,
  124. * and no new epoch has been created yet. */
  125. if (test_bit(CREATE_BARRIER, &mdev->flags))
  126. return;
  127. b = mdev->tconn->newest_tle;
  128. b->w.cb = w_send_barrier;
  129. /* inc_ap_pending done here, so we won't
  130. * get imbalanced on connection loss.
  131. * dec_ap_pending will be done in got_BarrierAck
  132. * or (on connection loss) in tl_clear. */
  133. inc_ap_pending(mdev);
  134. drbd_queue_work(&mdev->tconn->data.work, &b->w);
  135. set_bit(CREATE_BARRIER, &mdev->flags);
  136. }
  137. static void _about_to_complete_local_write(struct drbd_conf *mdev,
  138. struct drbd_request *req)
  139. {
  140. const unsigned long s = req->rq_state;
  141. /* Before we can signal completion to the upper layers,
  142. * we may need to close the current epoch.
  143. * We can skip this, if this request has not even been sent, because we
  144. * did not have a fully established connection yet/anymore, during
  145. * bitmap exchange, or while we are C_AHEAD due to congestion policy.
  146. */
  147. if (mdev->state.conn >= C_CONNECTED &&
  148. (s & RQ_NET_SENT) != 0 &&
  149. req->epoch == mdev->tconn->newest_tle->br_number)
  150. queue_barrier(mdev);
  151. /* we need to do the conflict detection stuff,
  152. * if the epoch_entries tree is non-empty and
  153. * this request has completed on the network */
  154. if ((s & RQ_NET_DONE) && !RB_EMPTY_ROOT(&mdev->epoch_entries)) {
  155. const sector_t sector = req->i.sector;
  156. const int size = req->i.size;
  157. struct drbd_interval *i;
  158. /* ASSERT:
  159. * there must be no conflicting requests, since
  160. * they must have been failed on the spot */
  161. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  162. if (i) {
  163. struct drbd_request *req2 =
  164. container_of(i, struct drbd_request, i);
  165. dev_alert(DEV, "LOGIC BUG: completed: %p %llus +%u; "
  166. "other: %p %llus +%u\n",
  167. req, (unsigned long long)sector, size,
  168. i, (unsigned long long)req2->i.sector, req2->i.size);
  169. }
  170. /* maybe "wake" those conflicting epoch entries
  171. * that wait for this request to finish.
  172. *
  173. * currently, there can be only _one_ such ee
  174. * (well, or some more, which would be pending
  175. * P_DISCARD_ACK not yet sent by the asender...),
  176. * since we block the receiver thread upon the
  177. * first conflict detection, which will wait on
  178. * misc_wait. maybe we want to assert that?
  179. *
  180. * anyways, if we found one,
  181. * we just have to do a wake_up. */
  182. i = drbd_find_overlap(&mdev->epoch_entries, sector, size);
  183. if (i)
  184. wake_up(&mdev->misc_wait);
  185. }
  186. }
  187. void complete_master_bio(struct drbd_conf *mdev,
  188. struct bio_and_error *m)
  189. {
  190. bio_endio(m->bio, m->error);
  191. dec_ap_bio(mdev);
  192. }
  193. /* Helper for __req_mod().
  194. * Set m->bio to the master bio, if it is fit to be completed,
  195. * or leave it alone (it is initialized to NULL in __req_mod),
  196. * if it has already been completed, or cannot be completed yet.
  197. * If m->bio is set, the error status to be returned is placed in m->error.
  198. */
  199. void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m)
  200. {
  201. const unsigned long s = req->rq_state;
  202. struct drbd_conf *mdev = req->mdev;
  203. /* only WRITES may end up here without a master bio (on barrier ack) */
  204. int rw = req->master_bio ? bio_data_dir(req->master_bio) : WRITE;
  205. /* we must not complete the master bio, while it is
  206. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  207. * not yet acknowledged by the peer
  208. * not yet completed by the local io subsystem
  209. * these flags may get cleared in any order by
  210. * the worker,
  211. * the receiver,
  212. * the bio_endio completion callbacks.
  213. */
  214. if (s & RQ_NET_QUEUED)
  215. return;
  216. if (s & RQ_NET_PENDING)
  217. return;
  218. if (s & RQ_LOCAL_PENDING)
  219. return;
  220. if (req->master_bio) {
  221. /* this is DATA_RECEIVED (remote read)
  222. * or protocol C P_WRITE_ACK
  223. * or protocol B P_RECV_ACK
  224. * or protocol A "HANDED_OVER_TO_NETWORK" (SendAck)
  225. * or canceled or failed,
  226. * or killed from the transfer log due to connection loss.
  227. */
  228. /*
  229. * figure out whether to report success or failure.
  230. *
  231. * report success when at least one of the operations succeeded.
  232. * or, to put the other way,
  233. * only report failure, when both operations failed.
  234. *
  235. * what to do about the failures is handled elsewhere.
  236. * what we need to do here is just: complete the master_bio.
  237. *
  238. * local completion error, if any, has been stored as ERR_PTR
  239. * in private_bio within drbd_endio_pri.
  240. */
  241. int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
  242. int error = PTR_ERR(req->private_bio);
  243. /* remove the request from the conflict detection
  244. * respective block_id verification hash */
  245. if (!drbd_interval_empty(&req->i)) {
  246. struct rb_root *root;
  247. if (rw == WRITE)
  248. root = &mdev->write_requests;
  249. else
  250. root = &mdev->read_requests;
  251. drbd_remove_interval(root, &req->i);
  252. } else
  253. D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
  254. /* for writes we need to do some extra housekeeping */
  255. if (rw == WRITE)
  256. _about_to_complete_local_write(mdev, req);
  257. /* Update disk stats */
  258. _drbd_end_io_acct(mdev, req);
  259. m->error = ok ? 0 : (error ?: -EIO);
  260. m->bio = req->master_bio;
  261. req->master_bio = NULL;
  262. }
  263. if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) {
  264. /* this is disconnected (local only) operation,
  265. * or protocol C P_WRITE_ACK,
  266. * or protocol A or B P_BARRIER_ACK,
  267. * or killed from the transfer log due to connection loss. */
  268. _req_is_done(mdev, req, rw);
  269. }
  270. /* else: network part and not DONE yet. that is
  271. * protocol A or B, barrier ack still pending... */
  272. }
  273. static void _req_may_be_done_not_susp(struct drbd_request *req, struct bio_and_error *m)
  274. {
  275. struct drbd_conf *mdev = req->mdev;
  276. if (!is_susp(mdev->state))
  277. _req_may_be_done(req, m);
  278. }
  279. /*
  280. * checks whether there was an overlapping request
  281. * or ee already registered.
  282. *
  283. * if so, return 1, in which case this request is completed on the spot,
  284. * without ever being submitted or send.
  285. *
  286. * return 0 if it is ok to submit this request.
  287. *
  288. * NOTE:
  289. * paranoia: assume something above us is broken, and issues different write
  290. * requests for the same block simultaneously...
  291. *
  292. * To ensure these won't be reordered differently on both nodes, resulting in
  293. * diverging data sets, we discard the later one(s). Not that this is supposed
  294. * to happen, but this is the rationale why we also have to check for
  295. * conflicting requests with local origin, and why we have to do so regardless
  296. * of whether we allowed multiple primaries.
  297. *
  298. * In case we only have one primary, the epoch_entries tree is empty.
  299. */
  300. static int _req_conflicts(struct drbd_request *req)
  301. {
  302. struct drbd_conf *mdev = req->mdev;
  303. const sector_t sector = req->i.sector;
  304. const int size = req->i.size;
  305. struct drbd_interval *i;
  306. D_ASSERT(drbd_interval_empty(&req->i));
  307. if (!get_net_conf(mdev->tconn))
  308. return 0;
  309. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  310. if (i) {
  311. struct drbd_request *req2 =
  312. container_of(i, struct drbd_request, i);
  313. dev_alert(DEV, "%s[%u] Concurrent local write detected! "
  314. "[DISCARD L] new: %llus +%u; "
  315. "pending: %llus +%u\n",
  316. current->comm, current->pid,
  317. (unsigned long long)sector, size,
  318. (unsigned long long)req2->i.sector, req2->i.size);
  319. goto out_conflict;
  320. }
  321. if (!RB_EMPTY_ROOT(&mdev->epoch_entries)) {
  322. /* check for overlapping requests with remote origin */
  323. i = drbd_find_overlap(&mdev->epoch_entries, sector, size);
  324. if (i) {
  325. struct drbd_epoch_entry *e =
  326. container_of(i, struct drbd_epoch_entry, i);
  327. dev_alert(DEV, "%s[%u] Concurrent remote write detected!"
  328. " [DISCARD L] new: %llus +%u; "
  329. "pending: %llus +%u\n",
  330. current->comm, current->pid,
  331. (unsigned long long)sector, size,
  332. (unsigned long long)e->i.sector, e->i.size);
  333. goto out_conflict;
  334. }
  335. }
  336. /* this is like it should be, and what we expected.
  337. * our users do behave after all... */
  338. put_net_conf(mdev->tconn);
  339. return 0;
  340. out_conflict:
  341. put_net_conf(mdev->tconn);
  342. return 1;
  343. }
  344. /* obviously this could be coded as many single functions
  345. * instead of one huge switch,
  346. * or by putting the code directly in the respective locations
  347. * (as it has been before).
  348. *
  349. * but having it this way
  350. * enforces that it is all in this one place, where it is easier to audit,
  351. * it makes it obvious that whatever "event" "happens" to a request should
  352. * happen "atomically" within the req_lock,
  353. * and it enforces that we have to think in a very structured manner
  354. * about the "events" that may happen to a request during its life time ...
  355. */
  356. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  357. struct bio_and_error *m)
  358. {
  359. struct drbd_conf *mdev = req->mdev;
  360. int rv = 0;
  361. m->bio = NULL;
  362. switch (what) {
  363. default:
  364. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  365. break;
  366. /* does not happen...
  367. * initialization done in drbd_req_new
  368. case CREATED:
  369. break;
  370. */
  371. case TO_BE_SENT: /* via network */
  372. /* reached via drbd_make_request_common
  373. * and from w_read_retry_remote */
  374. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  375. req->rq_state |= RQ_NET_PENDING;
  376. inc_ap_pending(mdev);
  377. break;
  378. case TO_BE_SUBMITTED: /* locally */
  379. /* reached via drbd_make_request_common */
  380. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  381. req->rq_state |= RQ_LOCAL_PENDING;
  382. break;
  383. case COMPLETED_OK:
  384. if (bio_data_dir(req->master_bio) == WRITE)
  385. mdev->writ_cnt += req->i.size >> 9;
  386. else
  387. mdev->read_cnt += req->i.size >> 9;
  388. req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  389. req->rq_state &= ~RQ_LOCAL_PENDING;
  390. _req_may_be_done_not_susp(req, m);
  391. put_ldev(mdev);
  392. break;
  393. case WRITE_COMPLETED_WITH_ERROR:
  394. req->rq_state |= RQ_LOCAL_COMPLETED;
  395. req->rq_state &= ~RQ_LOCAL_PENDING;
  396. __drbd_chk_io_error(mdev, false);
  397. _req_may_be_done_not_susp(req, m);
  398. put_ldev(mdev);
  399. break;
  400. case READ_AHEAD_COMPLETED_WITH_ERROR:
  401. /* it is legal to fail READA */
  402. req->rq_state |= RQ_LOCAL_COMPLETED;
  403. req->rq_state &= ~RQ_LOCAL_PENDING;
  404. _req_may_be_done_not_susp(req, m);
  405. put_ldev(mdev);
  406. break;
  407. case READ_COMPLETED_WITH_ERROR:
  408. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  409. req->rq_state |= RQ_LOCAL_COMPLETED;
  410. req->rq_state &= ~RQ_LOCAL_PENDING;
  411. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  412. __drbd_chk_io_error(mdev, false);
  413. put_ldev(mdev);
  414. /* no point in retrying if there is no good remote data,
  415. * or we have no connection. */
  416. if (mdev->state.pdsk != D_UP_TO_DATE) {
  417. _req_may_be_done_not_susp(req, m);
  418. break;
  419. }
  420. /* _req_mod(req,TO_BE_SENT); oops, recursion... */
  421. req->rq_state |= RQ_NET_PENDING;
  422. inc_ap_pending(mdev);
  423. /* fall through: _req_mod(req,QUEUE_FOR_NET_READ); */
  424. case QUEUE_FOR_NET_READ:
  425. /* READ or READA, and
  426. * no local disk,
  427. * or target area marked as invalid,
  428. * or just got an io-error. */
  429. /* from drbd_make_request_common
  430. * or from bio_endio during read io-error recovery */
  431. /* so we can verify the handle in the answer packet
  432. * corresponding hlist_del is in _req_may_be_done() */
  433. drbd_insert_interval(&mdev->read_requests, &req->i);
  434. set_bit(UNPLUG_REMOTE, &mdev->flags);
  435. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  436. req->rq_state |= RQ_NET_QUEUED;
  437. req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
  438. ? w_read_retry_remote
  439. : w_send_read_req;
  440. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  441. break;
  442. case QUEUE_FOR_NET_WRITE:
  443. /* assert something? */
  444. /* from drbd_make_request_common only */
  445. /* corresponding hlist_del is in _req_may_be_done() */
  446. drbd_insert_interval(&mdev->write_requests, &req->i);
  447. /* NOTE
  448. * In case the req ended up on the transfer log before being
  449. * queued on the worker, it could lead to this request being
  450. * missed during cleanup after connection loss.
  451. * So we have to do both operations here,
  452. * within the same lock that protects the transfer log.
  453. *
  454. * _req_add_to_epoch(req); this has to be after the
  455. * _maybe_start_new_epoch(req); which happened in
  456. * drbd_make_request_common, because we now may set the bit
  457. * again ourselves to close the current epoch.
  458. *
  459. * Add req to the (now) current epoch (barrier). */
  460. /* otherwise we may lose an unplug, which may cause some remote
  461. * io-scheduler timeout to expire, increasing maximum latency,
  462. * hurting performance. */
  463. set_bit(UNPLUG_REMOTE, &mdev->flags);
  464. /* see drbd_make_request_common,
  465. * just after it grabs the req_lock */
  466. D_ASSERT(test_bit(CREATE_BARRIER, &mdev->flags) == 0);
  467. req->epoch = mdev->tconn->newest_tle->br_number;
  468. /* increment size of current epoch */
  469. mdev->tconn->newest_tle->n_writes++;
  470. /* queue work item to send data */
  471. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  472. req->rq_state |= RQ_NET_QUEUED;
  473. req->w.cb = w_send_dblock;
  474. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  475. /* close the epoch, in case it outgrew the limit */
  476. if (mdev->tconn->newest_tle->n_writes >= mdev->tconn->net_conf->max_epoch_size)
  477. queue_barrier(mdev);
  478. break;
  479. case QUEUE_FOR_SEND_OOS:
  480. req->rq_state |= RQ_NET_QUEUED;
  481. req->w.cb = w_send_oos;
  482. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  483. break;
  484. case OOS_HANDED_TO_NETWORK:
  485. /* actually the same */
  486. case SEND_CANCELED:
  487. /* treat it the same */
  488. case SEND_FAILED:
  489. /* real cleanup will be done from tl_clear. just update flags
  490. * so it is no longer marked as on the worker queue */
  491. req->rq_state &= ~RQ_NET_QUEUED;
  492. /* if we did it right, tl_clear should be scheduled only after
  493. * this, so this should not be necessary! */
  494. _req_may_be_done_not_susp(req, m);
  495. break;
  496. case HANDED_OVER_TO_NETWORK:
  497. /* assert something? */
  498. if (bio_data_dir(req->master_bio) == WRITE)
  499. atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
  500. if (bio_data_dir(req->master_bio) == WRITE &&
  501. mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A) {
  502. /* this is what is dangerous about protocol A:
  503. * pretend it was successfully written on the peer. */
  504. if (req->rq_state & RQ_NET_PENDING) {
  505. dec_ap_pending(mdev);
  506. req->rq_state &= ~RQ_NET_PENDING;
  507. req->rq_state |= RQ_NET_OK;
  508. } /* else: neg-ack was faster... */
  509. /* it is still not yet RQ_NET_DONE until the
  510. * corresponding epoch barrier got acked as well,
  511. * so we know what to dirty on connection loss */
  512. }
  513. req->rq_state &= ~RQ_NET_QUEUED;
  514. req->rq_state |= RQ_NET_SENT;
  515. /* because _drbd_send_zc_bio could sleep, and may want to
  516. * dereference the bio even after the "WRITE_ACKED_BY_PEER" and
  517. * "COMPLETED_OK" events came in, once we return from
  518. * _drbd_send_zc_bio (drbd_send_dblock), we have to check
  519. * whether it is done already, and end it. */
  520. _req_may_be_done_not_susp(req, m);
  521. break;
  522. case READ_RETRY_REMOTE_CANCELED:
  523. req->rq_state &= ~RQ_NET_QUEUED;
  524. /* fall through, in case we raced with drbd_disconnect */
  525. case CONNECTION_LOST_WHILE_PENDING:
  526. /* transfer log cleanup after connection loss */
  527. /* assert something? */
  528. if (req->rq_state & RQ_NET_PENDING)
  529. dec_ap_pending(mdev);
  530. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  531. req->rq_state |= RQ_NET_DONE;
  532. if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE)
  533. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  534. /* if it is still queued, we may not complete it here.
  535. * it will be canceled soon. */
  536. if (!(req->rq_state & RQ_NET_QUEUED))
  537. _req_may_be_done(req, m); /* Allowed while state.susp */
  538. break;
  539. case WRITE_ACKED_BY_PEER_AND_SIS:
  540. req->rq_state |= RQ_NET_SIS;
  541. case CONFLICT_DISCARDED_BY_PEER:
  542. /* for discarded conflicting writes of multiple primaries,
  543. * there is no need to keep anything in the tl, potential
  544. * node crashes are covered by the activity log. */
  545. if (what == CONFLICT_DISCARDED_BY_PEER)
  546. dev_alert(DEV, "Got DiscardAck packet %llus +%u!"
  547. " DRBD is not a random data generator!\n",
  548. (unsigned long long)req->i.sector, req->i.size);
  549. req->rq_state |= RQ_NET_DONE;
  550. /* fall through */
  551. case WRITE_ACKED_BY_PEER:
  552. /* protocol C; successfully written on peer.
  553. * Nothing to do here.
  554. * We want to keep the tl in place for all protocols, to cater
  555. * for volatile write-back caches on lower level devices.
  556. *
  557. * A barrier request is expected to have forced all prior
  558. * requests onto stable storage, so completion of a barrier
  559. * request could set NET_DONE right here, and not wait for the
  560. * P_BARRIER_ACK, but that is an unnecessary optimization. */
  561. /* this makes it effectively the same as for: */
  562. case RECV_ACKED_BY_PEER:
  563. /* protocol B; pretends to be successfully written on peer.
  564. * see also notes above in HANDED_OVER_TO_NETWORK about
  565. * protocol != C */
  566. req->rq_state |= RQ_NET_OK;
  567. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  568. dec_ap_pending(mdev);
  569. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  570. req->rq_state &= ~RQ_NET_PENDING;
  571. _req_may_be_done_not_susp(req, m);
  572. break;
  573. case NEG_ACKED:
  574. /* assert something? */
  575. if (req->rq_state & RQ_NET_PENDING) {
  576. dec_ap_pending(mdev);
  577. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  578. }
  579. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  580. req->rq_state |= RQ_NET_DONE;
  581. _req_may_be_done_not_susp(req, m);
  582. /* else: done by HANDED_OVER_TO_NETWORK */
  583. break;
  584. case FAIL_FROZEN_DISK_IO:
  585. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  586. break;
  587. _req_may_be_done(req, m); /* Allowed while state.susp */
  588. break;
  589. case RESTART_FROZEN_DISK_IO:
  590. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  591. break;
  592. req->rq_state &= ~RQ_LOCAL_COMPLETED;
  593. rv = MR_READ;
  594. if (bio_data_dir(req->master_bio) == WRITE)
  595. rv = MR_WRITE;
  596. get_ldev(mdev);
  597. req->w.cb = w_restart_disk_io;
  598. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  599. break;
  600. case RESEND:
  601. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  602. before the connection loss (B&C only); only P_BARRIER_ACK was missing.
  603. Trowing them out of the TL here by pretending we got a BARRIER_ACK
  604. We ensure that the peer was not rebooted */
  605. if (!(req->rq_state & RQ_NET_OK)) {
  606. if (req->w.cb) {
  607. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  608. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  609. }
  610. break;
  611. }
  612. /* else, fall through to BARRIER_ACKED */
  613. case BARRIER_ACKED:
  614. if (!(req->rq_state & RQ_WRITE))
  615. break;
  616. if (req->rq_state & RQ_NET_PENDING) {
  617. /* barrier came in before all requests have been acked.
  618. * this is bad, because if the connection is lost now,
  619. * we won't be able to clean them up... */
  620. dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
  621. list_move(&req->tl_requests, &mdev->tconn->out_of_sequence_requests);
  622. }
  623. if ((req->rq_state & RQ_NET_MASK) != 0) {
  624. req->rq_state |= RQ_NET_DONE;
  625. if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A)
  626. atomic_sub(req->i.size>>9, &mdev->ap_in_flight);
  627. }
  628. _req_may_be_done(req, m); /* Allowed while state.susp */
  629. break;
  630. case DATA_RECEIVED:
  631. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  632. dec_ap_pending(mdev);
  633. req->rq_state &= ~RQ_NET_PENDING;
  634. req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
  635. _req_may_be_done_not_susp(req, m);
  636. break;
  637. };
  638. return rv;
  639. }
  640. /* we may do a local read if:
  641. * - we are consistent (of course),
  642. * - or we are generally inconsistent,
  643. * BUT we are still/already IN SYNC for this area.
  644. * since size may be bigger than BM_BLOCK_SIZE,
  645. * we may need to check several bits.
  646. */
  647. static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  648. {
  649. unsigned long sbnr, ebnr;
  650. sector_t esector, nr_sectors;
  651. if (mdev->state.disk == D_UP_TO_DATE)
  652. return 1;
  653. if (mdev->state.disk >= D_OUTDATED)
  654. return 0;
  655. if (mdev->state.disk < D_INCONSISTENT)
  656. return 0;
  657. /* state.disk == D_INCONSISTENT We will have a look at the BitMap */
  658. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  659. esector = sector + (size >> 9) - 1;
  660. D_ASSERT(sector < nr_sectors);
  661. D_ASSERT(esector < nr_sectors);
  662. sbnr = BM_SECT_TO_BIT(sector);
  663. ebnr = BM_SECT_TO_BIT(esector);
  664. return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr);
  665. }
  666. static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  667. {
  668. const int rw = bio_rw(bio);
  669. const int size = bio->bi_size;
  670. const sector_t sector = bio->bi_sector;
  671. struct drbd_tl_epoch *b = NULL;
  672. struct drbd_request *req;
  673. int local, remote, send_oos = 0;
  674. int err = -EIO;
  675. int ret = 0;
  676. /* allocate outside of all locks; */
  677. req = drbd_req_new(mdev, bio);
  678. if (!req) {
  679. dec_ap_bio(mdev);
  680. /* only pass the error to the upper layers.
  681. * if user cannot handle io errors, that's not our business. */
  682. dev_err(DEV, "could not kmalloc() req\n");
  683. bio_endio(bio, -ENOMEM);
  684. return 0;
  685. }
  686. req->start_time = start_time;
  687. local = get_ldev(mdev);
  688. if (!local) {
  689. bio_put(req->private_bio); /* or we get a bio leak */
  690. req->private_bio = NULL;
  691. }
  692. if (rw == WRITE) {
  693. remote = 1;
  694. } else {
  695. /* READ || READA */
  696. if (local) {
  697. if (!drbd_may_do_local_read(mdev, sector, size)) {
  698. /* we could kick the syncer to
  699. * sync this extent asap, wait for
  700. * it, then continue locally.
  701. * Or just issue the request remotely.
  702. */
  703. local = 0;
  704. bio_put(req->private_bio);
  705. req->private_bio = NULL;
  706. put_ldev(mdev);
  707. }
  708. }
  709. remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
  710. }
  711. /* If we have a disk, but a READA request is mapped to remote,
  712. * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
  713. * Just fail that READA request right here.
  714. *
  715. * THINK: maybe fail all READA when not local?
  716. * or make this configurable...
  717. * if network is slow, READA won't do any good.
  718. */
  719. if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
  720. err = -EWOULDBLOCK;
  721. goto fail_and_free_req;
  722. }
  723. /* For WRITES going to the local disk, grab a reference on the target
  724. * extent. This waits for any resync activity in the corresponding
  725. * resync extent to finish, and, if necessary, pulls in the target
  726. * extent into the activity log, which involves further disk io because
  727. * of transactional on-disk meta data updates. */
  728. if (rw == WRITE && local && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  729. req->rq_state |= RQ_IN_ACT_LOG;
  730. drbd_al_begin_io(mdev, sector);
  731. }
  732. remote = remote && drbd_should_do_remote(mdev->state);
  733. send_oos = rw == WRITE && drbd_should_send_oos(mdev->state);
  734. D_ASSERT(!(remote && send_oos));
  735. if (!(local || remote) && !is_susp(mdev->state)) {
  736. if (__ratelimit(&drbd_ratelimit_state))
  737. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  738. goto fail_free_complete;
  739. }
  740. /* For WRITE request, we have to make sure that we have an
  741. * unused_spare_tle, in case we need to start a new epoch.
  742. * I try to be smart and avoid to pre-allocate always "just in case",
  743. * but there is a race between testing the bit and pointer outside the
  744. * spinlock, and grabbing the spinlock.
  745. * if we lost that race, we retry. */
  746. if (rw == WRITE && (remote || send_oos) &&
  747. mdev->tconn->unused_spare_tle == NULL &&
  748. test_bit(CREATE_BARRIER, &mdev->flags)) {
  749. allocate_barrier:
  750. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
  751. if (!b) {
  752. dev_err(DEV, "Failed to alloc barrier.\n");
  753. err = -ENOMEM;
  754. goto fail_free_complete;
  755. }
  756. }
  757. /* GOOD, everything prepared, grab the spin_lock */
  758. spin_lock_irq(&mdev->tconn->req_lock);
  759. if (is_susp(mdev->state)) {
  760. /* If we got suspended, use the retry mechanism of
  761. generic_make_request() to restart processing of this
  762. bio. In the next call to drbd_make_request
  763. we sleep in inc_ap_bio() */
  764. ret = 1;
  765. spin_unlock_irq(&mdev->tconn->req_lock);
  766. goto fail_free_complete;
  767. }
  768. if (remote || send_oos) {
  769. remote = drbd_should_do_remote(mdev->state);
  770. send_oos = rw == WRITE && drbd_should_send_oos(mdev->state);
  771. D_ASSERT(!(remote && send_oos));
  772. if (!(remote || send_oos))
  773. dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
  774. if (!(local || remote)) {
  775. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  776. spin_unlock_irq(&mdev->tconn->req_lock);
  777. goto fail_free_complete;
  778. }
  779. }
  780. if (b && mdev->tconn->unused_spare_tle == NULL) {
  781. mdev->tconn->unused_spare_tle = b;
  782. b = NULL;
  783. }
  784. if (rw == WRITE && (remote || send_oos) &&
  785. mdev->tconn->unused_spare_tle == NULL &&
  786. test_bit(CREATE_BARRIER, &mdev->flags)) {
  787. /* someone closed the current epoch
  788. * while we were grabbing the spinlock */
  789. spin_unlock_irq(&mdev->tconn->req_lock);
  790. goto allocate_barrier;
  791. }
  792. /* Update disk stats */
  793. _drbd_start_io_acct(mdev, req, bio);
  794. /* _maybe_start_new_epoch(mdev);
  795. * If we need to generate a write barrier packet, we have to add the
  796. * new epoch (barrier) object, and queue the barrier packet for sending,
  797. * and queue the req's data after it _within the same lock_, otherwise
  798. * we have race conditions were the reorder domains could be mixed up.
  799. *
  800. * Even read requests may start a new epoch and queue the corresponding
  801. * barrier packet. To get the write ordering right, we only have to
  802. * make sure that, if this is a write request and it triggered a
  803. * barrier packet, this request is queued within the same spinlock. */
  804. if ((remote || send_oos) && mdev->tconn->unused_spare_tle &&
  805. test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
  806. _tl_add_barrier(mdev, mdev->tconn->unused_spare_tle);
  807. mdev->tconn->unused_spare_tle = NULL;
  808. } else {
  809. D_ASSERT(!(remote && rw == WRITE &&
  810. test_bit(CREATE_BARRIER, &mdev->flags)));
  811. }
  812. /* NOTE
  813. * Actually, 'local' may be wrong here already, since we may have failed
  814. * to write to the meta data, and may become wrong anytime because of
  815. * local io-error for some other request, which would lead to us
  816. * "detaching" the local disk.
  817. *
  818. * 'remote' may become wrong any time because the network could fail.
  819. *
  820. * This is a harmless race condition, though, since it is handled
  821. * correctly at the appropriate places; so it just defers the failure
  822. * of the respective operation.
  823. */
  824. /* mark them early for readability.
  825. * this just sets some state flags. */
  826. if (remote)
  827. _req_mod(req, TO_BE_SENT);
  828. if (local)
  829. _req_mod(req, TO_BE_SUBMITTED);
  830. /* check this request on the collision detection hash tables.
  831. * if we have a conflict, just complete it here.
  832. * THINK do we want to check reads, too? (I don't think so...) */
  833. if (rw == WRITE && _req_conflicts(req))
  834. goto fail_conflicting;
  835. list_add_tail(&req->tl_requests, &mdev->tconn->newest_tle->requests);
  836. /* NOTE remote first: to get the concurrent write detection right,
  837. * we must register the request before start of local IO. */
  838. if (remote) {
  839. /* either WRITE and C_CONNECTED,
  840. * or READ, and no local disk,
  841. * or READ, but not in sync.
  842. */
  843. _req_mod(req, (rw == WRITE)
  844. ? QUEUE_FOR_NET_WRITE
  845. : QUEUE_FOR_NET_READ);
  846. }
  847. if (send_oos && drbd_set_out_of_sync(mdev, sector, size))
  848. _req_mod(req, QUEUE_FOR_SEND_OOS);
  849. if (remote &&
  850. mdev->tconn->net_conf->on_congestion != OC_BLOCK && mdev->tconn->agreed_pro_version >= 96) {
  851. int congested = 0;
  852. if (mdev->tconn->net_conf->cong_fill &&
  853. atomic_read(&mdev->ap_in_flight) >= mdev->tconn->net_conf->cong_fill) {
  854. dev_info(DEV, "Congestion-fill threshold reached\n");
  855. congested = 1;
  856. }
  857. if (mdev->act_log->used >= mdev->tconn->net_conf->cong_extents) {
  858. dev_info(DEV, "Congestion-extents threshold reached\n");
  859. congested = 1;
  860. }
  861. if (congested) {
  862. queue_barrier(mdev); /* last barrier, after mirrored writes */
  863. if (mdev->tconn->net_conf->on_congestion == OC_PULL_AHEAD)
  864. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  865. else /*mdev->tconn->net_conf->on_congestion == OC_DISCONNECT */
  866. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  867. }
  868. }
  869. spin_unlock_irq(&mdev->tconn->req_lock);
  870. kfree(b); /* if someone else has beaten us to it... */
  871. if (local) {
  872. req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
  873. /* State may have changed since we grabbed our reference on the
  874. * mdev->ldev member. Double check, and short-circuit to endio.
  875. * In case the last activity log transaction failed to get on
  876. * stable storage, and this is a WRITE, we may not even submit
  877. * this bio. */
  878. if (get_ldev(mdev)) {
  879. if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
  880. : rw == READ ? DRBD_FAULT_DT_RD
  881. : DRBD_FAULT_DT_RA))
  882. bio_endio(req->private_bio, -EIO);
  883. else
  884. generic_make_request(req->private_bio);
  885. put_ldev(mdev);
  886. } else
  887. bio_endio(req->private_bio, -EIO);
  888. }
  889. return 0;
  890. fail_conflicting:
  891. /* this is a conflicting request.
  892. * even though it may have been only _partially_
  893. * overlapping with one of the currently pending requests,
  894. * without even submitting or sending it, we will
  895. * pretend that it was successfully served right now.
  896. */
  897. _drbd_end_io_acct(mdev, req);
  898. spin_unlock_irq(&mdev->tconn->req_lock);
  899. if (remote)
  900. dec_ap_pending(mdev);
  901. /* THINK: do we want to fail it (-EIO), or pretend success?
  902. * this pretends success. */
  903. err = 0;
  904. fail_free_complete:
  905. if (req->rq_state & RQ_IN_ACT_LOG)
  906. drbd_al_complete_io(mdev, sector);
  907. fail_and_free_req:
  908. if (local) {
  909. bio_put(req->private_bio);
  910. req->private_bio = NULL;
  911. put_ldev(mdev);
  912. }
  913. if (!ret)
  914. bio_endio(bio, err);
  915. drbd_req_free(req);
  916. dec_ap_bio(mdev);
  917. kfree(b);
  918. return ret;
  919. }
  920. /* helper function for drbd_make_request
  921. * if we can determine just by the mdev (state) that this request will fail,
  922. * return 1
  923. * otherwise return 0
  924. */
  925. static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
  926. {
  927. if (mdev->state.role != R_PRIMARY &&
  928. (!allow_oos || is_write)) {
  929. if (__ratelimit(&drbd_ratelimit_state)) {
  930. dev_err(DEV, "Process %s[%u] tried to %s; "
  931. "since we are not in Primary state, "
  932. "we cannot allow this\n",
  933. current->comm, current->pid,
  934. is_write ? "WRITE" : "READ");
  935. }
  936. return 1;
  937. }
  938. return 0;
  939. }
  940. int drbd_make_request(struct request_queue *q, struct bio *bio)
  941. {
  942. unsigned int s_enr, e_enr;
  943. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  944. unsigned long start_time;
  945. if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
  946. bio_endio(bio, -EPERM);
  947. return 0;
  948. }
  949. start_time = jiffies;
  950. /*
  951. * what we "blindly" assume:
  952. */
  953. D_ASSERT(bio->bi_size > 0);
  954. D_ASSERT((bio->bi_size & 0x1ff) == 0);
  955. D_ASSERT(bio->bi_idx == 0);
  956. /* to make some things easier, force alignment of requests within the
  957. * granularity of our hash tables */
  958. s_enr = bio->bi_sector >> HT_SHIFT;
  959. e_enr = (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT;
  960. if (likely(s_enr == e_enr)) {
  961. inc_ap_bio(mdev, 1);
  962. return drbd_make_request_common(mdev, bio, start_time);
  963. }
  964. /* can this bio be split generically?
  965. * Maybe add our own split-arbitrary-bios function. */
  966. if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_BIO_SIZE) {
  967. /* rather error out here than BUG in bio_split */
  968. dev_err(DEV, "bio would need to, but cannot, be split: "
  969. "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
  970. bio->bi_vcnt, bio->bi_idx, bio->bi_size,
  971. (unsigned long long)bio->bi_sector);
  972. bio_endio(bio, -EINVAL);
  973. } else {
  974. /* This bio crosses some boundary, so we have to split it. */
  975. struct bio_pair *bp;
  976. /* works for the "do not cross hash slot boundaries" case
  977. * e.g. sector 262269, size 4096
  978. * s_enr = 262269 >> 6 = 4097
  979. * e_enr = (262269+8-1) >> 6 = 4098
  980. * HT_SHIFT = 6
  981. * sps = 64, mask = 63
  982. * first_sectors = 64 - (262269 & 63) = 3
  983. */
  984. const sector_t sect = bio->bi_sector;
  985. const int sps = 1 << HT_SHIFT; /* sectors per slot */
  986. const int mask = sps - 1;
  987. const sector_t first_sectors = sps - (sect & mask);
  988. bp = bio_split(bio, first_sectors);
  989. /* we need to get a "reference count" (ap_bio_cnt)
  990. * to avoid races with the disconnect/reconnect/suspend code.
  991. * In case we need to split the bio here, we need to get three references
  992. * atomically, otherwise we might deadlock when trying to submit the
  993. * second one! */
  994. inc_ap_bio(mdev, 3);
  995. D_ASSERT(e_enr == s_enr + 1);
  996. while (drbd_make_request_common(mdev, &bp->bio1, start_time))
  997. inc_ap_bio(mdev, 1);
  998. while (drbd_make_request_common(mdev, &bp->bio2, start_time))
  999. inc_ap_bio(mdev, 1);
  1000. dec_ap_bio(mdev);
  1001. bio_pair_release(bp);
  1002. }
  1003. return 0;
  1004. }
  1005. /* This is called by bio_add_page(). With this function we reduce
  1006. * the number of BIOs that span over multiple DRBD_MAX_BIO_SIZEs
  1007. * units (was AL_EXTENTs).
  1008. *
  1009. * we do the calculation within the lower 32bit of the byte offsets,
  1010. * since we don't care for actual offset, but only check whether it
  1011. * would cross "activity log extent" boundaries.
  1012. *
  1013. * As long as the BIO is empty we have to allow at least one bvec,
  1014. * regardless of size and offset. so the resulting bio may still
  1015. * cross extent boundaries. those are dealt with (bio_split) in
  1016. * drbd_make_request.
  1017. */
  1018. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  1019. {
  1020. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  1021. unsigned int bio_offset =
  1022. (unsigned int)bvm->bi_sector << 9; /* 32 bit */
  1023. unsigned int bio_size = bvm->bi_size;
  1024. int limit, backing_limit;
  1025. limit = DRBD_MAX_BIO_SIZE
  1026. - ((bio_offset & (DRBD_MAX_BIO_SIZE-1)) + bio_size);
  1027. if (limit < 0)
  1028. limit = 0;
  1029. if (bio_size == 0) {
  1030. if (limit <= bvec->bv_len)
  1031. limit = bvec->bv_len;
  1032. } else if (limit && get_ldev(mdev)) {
  1033. struct request_queue * const b =
  1034. mdev->ldev->backing_bdev->bd_disk->queue;
  1035. if (b->merge_bvec_fn) {
  1036. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  1037. limit = min(limit, backing_limit);
  1038. }
  1039. put_ldev(mdev);
  1040. }
  1041. return limit;
  1042. }
  1043. void request_timer_fn(unsigned long data)
  1044. {
  1045. struct drbd_conf *mdev = (struct drbd_conf *) data;
  1046. struct drbd_request *req; /* oldest request */
  1047. struct list_head *le;
  1048. unsigned long et = 0; /* effective timeout = ko_count * timeout */
  1049. if (get_net_conf(mdev->tconn)) {
  1050. et = mdev->tconn->net_conf->timeout*HZ/10 * mdev->tconn->net_conf->ko_count;
  1051. put_net_conf(mdev->tconn);
  1052. }
  1053. if (!et || mdev->state.conn < C_WF_REPORT_PARAMS)
  1054. return; /* Recurring timer stopped */
  1055. spin_lock_irq(&mdev->tconn->req_lock);
  1056. le = &mdev->tconn->oldest_tle->requests;
  1057. if (list_empty(le)) {
  1058. spin_unlock_irq(&mdev->tconn->req_lock);
  1059. mod_timer(&mdev->request_timer, jiffies + et);
  1060. return;
  1061. }
  1062. le = le->prev;
  1063. req = list_entry(le, struct drbd_request, tl_requests);
  1064. if (time_is_before_eq_jiffies(req->start_time + et)) {
  1065. if (req->rq_state & RQ_NET_PENDING) {
  1066. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  1067. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE, NULL);
  1068. } else {
  1069. dev_warn(DEV, "Local backing block device frozen?\n");
  1070. mod_timer(&mdev->request_timer, jiffies + et);
  1071. }
  1072. } else {
  1073. mod_timer(&mdev->request_timer, req->start_time + et);
  1074. }
  1075. spin_unlock_irq(&mdev->tconn->req_lock);
  1076. }