drbd_req.c 38 KB

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