drbd_req.c 36 KB

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