drbd_req.c 36 KB

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