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