drbd_req.c 37 KB

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