drbd_req.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. drbd_req.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/drbd.h>
  22. #include "drbd_int.h"
  23. #include "drbd_req.h"
  24. 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. /* one reference to be put by __drbd_make_request */
  71. atomic_set(&req->completion_ref, 1);
  72. /* one kref as long as completion_ref > 0 */
  73. kref_init(&req->kref);
  74. return req;
  75. }
  76. void drbd_req_destroy(struct kref *kref)
  77. {
  78. struct drbd_request *req = container_of(kref, struct drbd_request, kref);
  79. struct drbd_conf *mdev = req->w.mdev;
  80. const unsigned s = req->rq_state;
  81. if ((req->master_bio && !(s & RQ_POSTPONED)) ||
  82. atomic_read(&req->completion_ref) ||
  83. (s & RQ_LOCAL_PENDING) ||
  84. ((s & RQ_NET_MASK) && !(s & RQ_NET_DONE))) {
  85. dev_err(DEV, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
  86. s, atomic_read(&req->completion_ref));
  87. return;
  88. }
  89. /* remove it from the transfer log.
  90. * well, only if it had been there in the first
  91. * place... if it had not (local only or conflicting
  92. * and never sent), it should still be "empty" as
  93. * initialized in drbd_req_new(), so we can list_del() it
  94. * here unconditionally */
  95. list_del_init(&req->tl_requests);
  96. /* if it was a write, we may have to set the corresponding
  97. * bit(s) out-of-sync first. If it had a local part, we need to
  98. * release the reference to the activity log. */
  99. if (s & RQ_WRITE) {
  100. /* Set out-of-sync unless both OK flags are set
  101. * (local only or remote failed).
  102. * Other places where we set out-of-sync:
  103. * READ with local io-error */
  104. if (!(s & RQ_POSTPONED)) {
  105. if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
  106. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  107. if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
  108. drbd_set_in_sync(mdev, req->i.sector, req->i.size);
  109. }
  110. /* one might be tempted to move the drbd_al_complete_io
  111. * to the local io completion callback drbd_request_endio.
  112. * but, if this was a mirror write, we may only
  113. * drbd_al_complete_io after this is RQ_NET_DONE,
  114. * otherwise the extent could be dropped from the al
  115. * before it has actually been written on the peer.
  116. * if we crash before our peer knows about the request,
  117. * but after the extent has been dropped from the al,
  118. * we would forget to resync the corresponding extent.
  119. */
  120. if (s & RQ_IN_ACT_LOG) {
  121. if (get_ldev_if_state(mdev, D_FAILED)) {
  122. drbd_al_complete_io(mdev, &req->i);
  123. put_ldev(mdev);
  124. } else if (__ratelimit(&drbd_ratelimit_state)) {
  125. dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
  126. "but my Disk seems to have failed :(\n",
  127. (unsigned long long) req->i.sector, req->i.size);
  128. }
  129. }
  130. }
  131. mempool_free(req, drbd_request_mempool);
  132. }
  133. static void wake_all_senders(struct drbd_tconn *tconn) {
  134. wake_up(&tconn->sender_work.q_wait);
  135. }
  136. /* must hold resource->req_lock */
  137. static void start_new_tl_epoch(struct drbd_tconn *tconn)
  138. {
  139. /* no point closing an epoch, if it is empty, anyways. */
  140. if (tconn->current_tle_writes == 0)
  141. return;
  142. tconn->current_tle_writes = 0;
  143. atomic_inc(&tconn->current_tle_nr);
  144. wake_all_senders(tconn);
  145. }
  146. void complete_master_bio(struct drbd_conf *mdev,
  147. struct bio_and_error *m)
  148. {
  149. bio_endio(m->bio, m->error);
  150. dec_ap_bio(mdev);
  151. }
  152. static void drbd_remove_request_interval(struct rb_root *root,
  153. struct drbd_request *req)
  154. {
  155. struct drbd_conf *mdev = req->w.mdev;
  156. struct drbd_interval *i = &req->i;
  157. drbd_remove_interval(root, i);
  158. /* Wake up any processes waiting for this request to complete. */
  159. if (i->waiting)
  160. wake_up(&mdev->misc_wait);
  161. }
  162. /* Helper for __req_mod().
  163. * Set m->bio to the master bio, if it is fit to be completed,
  164. * or leave it alone (it is initialized to NULL in __req_mod),
  165. * if it has already been completed, or cannot be completed yet.
  166. * If m->bio is set, the error status to be returned is placed in m->error.
  167. */
  168. static
  169. void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
  170. {
  171. const unsigned s = req->rq_state;
  172. struct drbd_conf *mdev = req->w.mdev;
  173. int rw;
  174. int error, ok;
  175. /* we must not complete the master bio, while it is
  176. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  177. * not yet acknowledged by the peer
  178. * not yet completed by the local io subsystem
  179. * these flags may get cleared in any order by
  180. * the worker,
  181. * the receiver,
  182. * the bio_endio completion callbacks.
  183. */
  184. if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
  185. (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
  186. (s & RQ_COMPLETION_SUSP)) {
  187. dev_err(DEV, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
  188. return;
  189. }
  190. if (!req->master_bio) {
  191. dev_err(DEV, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
  192. return;
  193. }
  194. rw = bio_rw(req->master_bio);
  195. /*
  196. * figure out whether to report success or failure.
  197. *
  198. * report success when at least one of the operations succeeded.
  199. * or, to put the other way,
  200. * only report failure, when both operations failed.
  201. *
  202. * what to do about the failures is handled elsewhere.
  203. * what we need to do here is just: complete the master_bio.
  204. *
  205. * local completion error, if any, has been stored as ERR_PTR
  206. * in private_bio within drbd_request_endio.
  207. */
  208. ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
  209. error = PTR_ERR(req->private_bio);
  210. /* remove the request from the conflict detection
  211. * respective block_id verification hash */
  212. if (!drbd_interval_empty(&req->i)) {
  213. struct rb_root *root;
  214. if (rw == WRITE)
  215. root = &mdev->write_requests;
  216. else
  217. root = &mdev->read_requests;
  218. drbd_remove_request_interval(root, req);
  219. } else if (!(s & RQ_POSTPONED))
  220. D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
  221. /* Before we can signal completion to the upper layers,
  222. * we may need to close the current transfer log epoch.
  223. * We are within the request lock, so we can simply compare
  224. * the request epoch number with the current transfer log
  225. * epoch number. If they match, increase the current_tle_nr,
  226. * and reset the transfer log epoch write_cnt.
  227. */
  228. if (rw == WRITE &&
  229. req->epoch == atomic_read(&mdev->tconn->current_tle_nr))
  230. start_new_tl_epoch(mdev->tconn);
  231. /* Update disk stats */
  232. _drbd_end_io_acct(mdev, req);
  233. /* If READ failed,
  234. * have it be pushed back to the retry work queue,
  235. * so it will re-enter __drbd_make_request(),
  236. * and be re-assigned to a suitable local or remote path,
  237. * or failed if we do not have access to good data anymore.
  238. *
  239. * Unless it was failed early by __drbd_make_request(),
  240. * because no path was available, in which case
  241. * it was not even added to the transfer_log.
  242. *
  243. * READA may fail, and will not be retried.
  244. *
  245. * WRITE should have used all available paths already.
  246. */
  247. if (!ok && rw == READ && !list_empty(&req->tl_requests))
  248. req->rq_state |= RQ_POSTPONED;
  249. if (!(req->rq_state & RQ_POSTPONED)) {
  250. m->error = ok ? 0 : (error ?: -EIO);
  251. m->bio = req->master_bio;
  252. req->master_bio = NULL;
  253. }
  254. }
  255. static int drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
  256. {
  257. struct drbd_conf *mdev = req->w.mdev;
  258. D_ASSERT(m || (req->rq_state & RQ_POSTPONED));
  259. if (!atomic_sub_and_test(put, &req->completion_ref))
  260. return 0;
  261. drbd_req_complete(req, m);
  262. if (req->rq_state & RQ_POSTPONED) {
  263. /* don't destroy the req object just yet,
  264. * but queue it for retry */
  265. drbd_restart_request(req);
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. /* I'd like this to be the only place that manipulates
  271. * req->completion_ref and req->kref. */
  272. static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
  273. int clear, int set)
  274. {
  275. struct drbd_conf *mdev = req->w.mdev;
  276. unsigned s = req->rq_state;
  277. int c_put = 0;
  278. int k_put = 0;
  279. if (drbd_suspended(mdev) && !((s | clear) & RQ_COMPLETION_SUSP))
  280. set |= RQ_COMPLETION_SUSP;
  281. /* apply */
  282. req->rq_state &= ~clear;
  283. req->rq_state |= set;
  284. /* no change? */
  285. if (req->rq_state == s)
  286. return;
  287. /* intent: get references */
  288. if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
  289. atomic_inc(&req->completion_ref);
  290. if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
  291. inc_ap_pending(mdev);
  292. atomic_inc(&req->completion_ref);
  293. }
  294. if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED))
  295. atomic_inc(&req->completion_ref);
  296. if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
  297. kref_get(&req->kref); /* wait for the DONE */
  298. if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT))
  299. atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
  300. if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
  301. atomic_inc(&req->completion_ref);
  302. /* progress: put references */
  303. if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
  304. ++c_put;
  305. if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
  306. D_ASSERT(req->rq_state & RQ_LOCAL_PENDING);
  307. /* local completion may still come in later,
  308. * we need to keep the req object around. */
  309. kref_get(&req->kref);
  310. ++c_put;
  311. }
  312. if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
  313. if (req->rq_state & RQ_LOCAL_ABORTED)
  314. ++k_put;
  315. else
  316. ++c_put;
  317. }
  318. if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
  319. dec_ap_pending(mdev);
  320. ++c_put;
  321. }
  322. if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED))
  323. ++c_put;
  324. if ((s & RQ_EXP_BARR_ACK) && !(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
  325. if (req->rq_state & RQ_NET_SENT)
  326. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  327. ++k_put;
  328. }
  329. /* potentially complete and destroy */
  330. if (k_put || c_put) {
  331. /* Completion does it's own kref_put. If we are going to
  332. * kref_sub below, we need req to be still around then. */
  333. int at_least = k_put + !!c_put;
  334. int refcount = atomic_read(&req->kref.refcount);
  335. if (refcount < at_least)
  336. dev_err(DEV,
  337. "mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
  338. s, req->rq_state, refcount, at_least);
  339. }
  340. /* If we made progress, retry conflicting peer requests, if any. */
  341. if (req->i.waiting)
  342. wake_up(&mdev->misc_wait);
  343. if (c_put)
  344. k_put += drbd_req_put_completion_ref(req, m, c_put);
  345. if (k_put)
  346. kref_sub(&req->kref, k_put, drbd_req_destroy);
  347. }
  348. /* obviously this could be coded as many single functions
  349. * instead of one huge switch,
  350. * or by putting the code directly in the respective locations
  351. * (as it has been before).
  352. *
  353. * but having it this way
  354. * enforces that it is all in this one place, where it is easier to audit,
  355. * it makes it obvious that whatever "event" "happens" to a request should
  356. * happen "atomically" within the req_lock,
  357. * and it enforces that we have to think in a very structured manner
  358. * about the "events" that may happen to a request during its life time ...
  359. */
  360. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  361. struct bio_and_error *m)
  362. {
  363. struct drbd_conf *mdev = req->w.mdev;
  364. struct net_conf *nc;
  365. int p, rv = 0;
  366. if (m)
  367. m->bio = NULL;
  368. switch (what) {
  369. default:
  370. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  371. break;
  372. /* does not happen...
  373. * initialization done in drbd_req_new
  374. case CREATED:
  375. break;
  376. */
  377. case TO_BE_SENT: /* via network */
  378. /* reached via __drbd_make_request
  379. * and from w_read_retry_remote */
  380. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  381. rcu_read_lock();
  382. nc = rcu_dereference(mdev->tconn->net_conf);
  383. p = nc->wire_protocol;
  384. rcu_read_unlock();
  385. req->rq_state |=
  386. p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
  387. p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
  388. mod_rq_state(req, m, 0, RQ_NET_PENDING);
  389. break;
  390. case TO_BE_SUBMITTED: /* locally */
  391. /* reached via __drbd_make_request */
  392. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  393. mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
  394. break;
  395. case COMPLETED_OK:
  396. if (req->rq_state & RQ_WRITE)
  397. mdev->writ_cnt += req->i.size >> 9;
  398. else
  399. mdev->read_cnt += req->i.size >> 9;
  400. mod_rq_state(req, m, RQ_LOCAL_PENDING,
  401. RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  402. break;
  403. case ABORT_DISK_IO:
  404. mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
  405. break;
  406. case READ_COMPLETED_WITH_ERROR:
  407. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  408. /* fall through. */
  409. case WRITE_COMPLETED_WITH_ERROR:
  410. __drbd_chk_io_error(mdev, DRBD_IO_ERROR);
  411. /* fall through. */
  412. case READ_AHEAD_COMPLETED_WITH_ERROR:
  413. /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
  414. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  415. break;
  416. case QUEUE_FOR_NET_READ:
  417. /* READ or READA, and
  418. * no local disk,
  419. * or target area marked as invalid,
  420. * or just got an io-error. */
  421. /* from __drbd_make_request
  422. * or from bio_endio during read io-error recovery */
  423. /* So we can verify the handle in the answer packet.
  424. * Corresponding drbd_remove_request_interval is in
  425. * drbd_req_complete() */
  426. D_ASSERT(drbd_interval_empty(&req->i));
  427. drbd_insert_interval(&mdev->read_requests, &req->i);
  428. set_bit(UNPLUG_REMOTE, &mdev->flags);
  429. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  430. D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
  431. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  432. req->w.cb = w_send_read_req;
  433. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  434. break;
  435. case QUEUE_FOR_NET_WRITE:
  436. /* assert something? */
  437. /* from __drbd_make_request only */
  438. /* Corresponding drbd_remove_request_interval is in
  439. * drbd_req_complete() */
  440. D_ASSERT(drbd_interval_empty(&req->i));
  441. drbd_insert_interval(&mdev->write_requests, &req->i);
  442. /* NOTE
  443. * In case the req ended up on the transfer log before being
  444. * queued on the worker, it could lead to this request being
  445. * missed during cleanup after connection loss.
  446. * So we have to do both operations here,
  447. * within the same lock that protects the transfer log.
  448. *
  449. * _req_add_to_epoch(req); this has to be after the
  450. * _maybe_start_new_epoch(req); which happened in
  451. * __drbd_make_request, because we now may set the bit
  452. * again ourselves to close the current epoch.
  453. *
  454. * Add req to the (now) current epoch (barrier). */
  455. /* otherwise we may lose an unplug, which may cause some remote
  456. * io-scheduler timeout to expire, increasing maximum latency,
  457. * hurting performance. */
  458. set_bit(UNPLUG_REMOTE, &mdev->flags);
  459. /* queue work item to send data */
  460. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  461. mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
  462. req->w.cb = w_send_dblock;
  463. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  464. /* close the epoch, in case it outgrew the limit */
  465. rcu_read_lock();
  466. nc = rcu_dereference(mdev->tconn->net_conf);
  467. p = nc->max_epoch_size;
  468. rcu_read_unlock();
  469. if (mdev->tconn->current_tle_writes >= p)
  470. start_new_tl_epoch(mdev->tconn);
  471. break;
  472. case QUEUE_FOR_SEND_OOS:
  473. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  474. req->w.cb = w_send_out_of_sync;
  475. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  476. break;
  477. case READ_RETRY_REMOTE_CANCELED:
  478. case SEND_CANCELED:
  479. case SEND_FAILED:
  480. /* real cleanup will be done from tl_clear. just update flags
  481. * so it is no longer marked as on the worker queue */
  482. mod_rq_state(req, m, RQ_NET_QUEUED, 0);
  483. break;
  484. case HANDED_OVER_TO_NETWORK:
  485. /* assert something? */
  486. if (bio_data_dir(req->master_bio) == WRITE &&
  487. !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
  488. /* this is what is dangerous about protocol A:
  489. * pretend it was successfully written on the peer. */
  490. if (req->rq_state & RQ_NET_PENDING)
  491. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  492. /* else: neg-ack was faster... */
  493. /* it is still not yet RQ_NET_DONE until the
  494. * corresponding epoch barrier got acked as well,
  495. * so we know what to dirty on connection loss */
  496. }
  497. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
  498. break;
  499. case OOS_HANDED_TO_NETWORK:
  500. /* Was not set PENDING, no longer QUEUED, so is now DONE
  501. * as far as this connection is concerned. */
  502. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
  503. break;
  504. case CONNECTION_LOST_WHILE_PENDING:
  505. /* transfer log cleanup after connection loss */
  506. mod_rq_state(req, m,
  507. RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
  508. RQ_NET_DONE);
  509. break;
  510. case CONFLICT_RESOLVED:
  511. /* for superseded conflicting writes of multiple primaries,
  512. * there is no need to keep anything in the tl, potential
  513. * node crashes are covered by the activity log.
  514. *
  515. * If this request had been marked as RQ_POSTPONED before,
  516. * it will actually not be completed, but "restarted",
  517. * resubmitted from the retry worker context. */
  518. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  519. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  520. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
  521. break;
  522. case WRITE_ACKED_BY_PEER_AND_SIS:
  523. req->rq_state |= RQ_NET_SIS;
  524. case WRITE_ACKED_BY_PEER:
  525. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  526. /* protocol C; successfully written on peer.
  527. * Nothing more to do here.
  528. * We want to keep the tl in place for all protocols, to cater
  529. * for volatile write-back caches on lower level devices. */
  530. goto ack_common;
  531. case RECV_ACKED_BY_PEER:
  532. D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
  533. /* protocol B; pretends to be successfully written on peer.
  534. * see also notes above in HANDED_OVER_TO_NETWORK about
  535. * protocol != C */
  536. ack_common:
  537. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  538. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  539. break;
  540. case POSTPONE_WRITE:
  541. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  542. /* If this node has already detected the write conflict, the
  543. * worker will be waiting on misc_wait. Wake it up once this
  544. * request has completed locally.
  545. */
  546. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  547. req->rq_state |= RQ_POSTPONED;
  548. if (req->i.waiting)
  549. wake_up(&mdev->misc_wait);
  550. /* Do not clear RQ_NET_PENDING. This request will make further
  551. * progress via restart_conflicting_writes() or
  552. * fail_postponed_requests(). Hopefully. */
  553. break;
  554. case NEG_ACKED:
  555. mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
  556. break;
  557. case FAIL_FROZEN_DISK_IO:
  558. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  559. break;
  560. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  561. break;
  562. case RESTART_FROZEN_DISK_IO:
  563. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  564. break;
  565. mod_rq_state(req, m,
  566. RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
  567. RQ_LOCAL_PENDING);
  568. rv = MR_READ;
  569. if (bio_data_dir(req->master_bio) == WRITE)
  570. rv = MR_WRITE;
  571. get_ldev(mdev); /* always succeeds in this call path */
  572. req->w.cb = w_restart_disk_io;
  573. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  574. break;
  575. case RESEND:
  576. /* Simply complete (local only) READs. */
  577. if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
  578. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  579. break;
  580. }
  581. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  582. before the connection loss (B&C only); only P_BARRIER_ACK
  583. (or the local completion?) was missing when we suspended.
  584. Throwing them out of the TL here by pretending we got a BARRIER_ACK.
  585. During connection handshake, we ensure that the peer was not rebooted. */
  586. if (!(req->rq_state & RQ_NET_OK)) {
  587. /* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
  588. * in that case we must not set RQ_NET_PENDING. */
  589. mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
  590. if (req->w.cb) {
  591. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  592. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  593. } /* else: FIXME can this happen? */
  594. break;
  595. }
  596. /* else, fall through to BARRIER_ACKED */
  597. case BARRIER_ACKED:
  598. /* barrier ack for READ requests does not make sense */
  599. if (!(req->rq_state & RQ_WRITE))
  600. break;
  601. if (req->rq_state & RQ_NET_PENDING) {
  602. /* barrier came in before all requests were acked.
  603. * this is bad, because if the connection is lost now,
  604. * we won't be able to clean them up... */
  605. dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
  606. }
  607. /* Allowed to complete requests, even while suspended.
  608. * As this is called for all requests within a matching epoch,
  609. * we need to filter, and only set RQ_NET_DONE for those that
  610. * have actually been on the wire. */
  611. mod_rq_state(req, m, RQ_COMPLETION_SUSP,
  612. (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
  613. break;
  614. case DATA_RECEIVED:
  615. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  616. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
  617. break;
  618. };
  619. return rv;
  620. }
  621. /* we may do a local read if:
  622. * - we are consistent (of course),
  623. * - or we are generally inconsistent,
  624. * BUT we are still/already IN SYNC for this area.
  625. * since size may be bigger than BM_BLOCK_SIZE,
  626. * we may need to check several bits.
  627. */
  628. static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  629. {
  630. unsigned long sbnr, ebnr;
  631. sector_t esector, nr_sectors;
  632. if (mdev->state.disk == D_UP_TO_DATE)
  633. return true;
  634. if (mdev->state.disk != D_INCONSISTENT)
  635. return false;
  636. esector = sector + (size >> 9) - 1;
  637. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  638. D_ASSERT(sector < nr_sectors);
  639. D_ASSERT(esector < nr_sectors);
  640. sbnr = BM_SECT_TO_BIT(sector);
  641. ebnr = BM_SECT_TO_BIT(esector);
  642. return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
  643. }
  644. static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
  645. enum drbd_read_balancing rbm)
  646. {
  647. struct backing_dev_info *bdi;
  648. int stripe_shift;
  649. switch (rbm) {
  650. case RB_CONGESTED_REMOTE:
  651. bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
  652. return bdi_read_congested(bdi);
  653. case RB_LEAST_PENDING:
  654. return atomic_read(&mdev->local_cnt) >
  655. atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
  656. case RB_32K_STRIPING: /* stripe_shift = 15 */
  657. case RB_64K_STRIPING:
  658. case RB_128K_STRIPING:
  659. case RB_256K_STRIPING:
  660. case RB_512K_STRIPING:
  661. case RB_1M_STRIPING: /* stripe_shift = 20 */
  662. stripe_shift = (rbm - RB_32K_STRIPING + 15);
  663. return (sector >> (stripe_shift - 9)) & 1;
  664. case RB_ROUND_ROBIN:
  665. return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
  666. case RB_PREFER_REMOTE:
  667. return true;
  668. case RB_PREFER_LOCAL:
  669. default:
  670. return false;
  671. }
  672. }
  673. /*
  674. * complete_conflicting_writes - wait for any conflicting write requests
  675. *
  676. * The write_requests tree contains all active write requests which we
  677. * currently know about. Wait for any requests to complete which conflict with
  678. * the new one.
  679. *
  680. * Only way out: remove the conflicting intervals from the tree.
  681. */
  682. static void complete_conflicting_writes(struct drbd_request *req)
  683. {
  684. DEFINE_WAIT(wait);
  685. struct drbd_conf *mdev = req->w.mdev;
  686. struct drbd_interval *i;
  687. sector_t sector = req->i.sector;
  688. int size = req->i.size;
  689. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  690. if (!i)
  691. return;
  692. for (;;) {
  693. prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
  694. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  695. if (!i)
  696. break;
  697. /* Indicate to wake up device->misc_wait on progress. */
  698. i->waiting = true;
  699. spin_unlock_irq(&mdev->tconn->req_lock);
  700. schedule();
  701. spin_lock_irq(&mdev->tconn->req_lock);
  702. }
  703. finish_wait(&mdev->misc_wait, &wait);
  704. }
  705. /* called within req_lock and rcu_read_lock() */
  706. static void maybe_pull_ahead(struct drbd_conf *mdev)
  707. {
  708. struct drbd_tconn *tconn = mdev->tconn;
  709. struct net_conf *nc;
  710. bool congested = false;
  711. enum drbd_on_congestion on_congestion;
  712. nc = rcu_dereference(tconn->net_conf);
  713. on_congestion = nc ? nc->on_congestion : OC_BLOCK;
  714. if (on_congestion == OC_BLOCK ||
  715. tconn->agreed_pro_version < 96)
  716. return;
  717. /* If I don't even have good local storage, we can not reasonably try
  718. * to pull ahead of the peer. We also need the local reference to make
  719. * sure mdev->act_log is there.
  720. */
  721. if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
  722. return;
  723. if (nc->cong_fill &&
  724. atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
  725. dev_info(DEV, "Congestion-fill threshold reached\n");
  726. congested = true;
  727. }
  728. if (mdev->act_log->used >= nc->cong_extents) {
  729. dev_info(DEV, "Congestion-extents threshold reached\n");
  730. congested = true;
  731. }
  732. if (congested) {
  733. /* start a new epoch for non-mirrored writes */
  734. start_new_tl_epoch(mdev->tconn);
  735. if (on_congestion == OC_PULL_AHEAD)
  736. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  737. else /*nc->on_congestion == OC_DISCONNECT */
  738. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  739. }
  740. put_ldev(mdev);
  741. }
  742. /* If this returns false, and req->private_bio is still set,
  743. * this should be submitted locally.
  744. *
  745. * If it returns false, but req->private_bio is not set,
  746. * we do not have access to good data :(
  747. *
  748. * Otherwise, this destroys req->private_bio, if any,
  749. * and returns true.
  750. */
  751. static bool do_remote_read(struct drbd_request *req)
  752. {
  753. struct drbd_conf *mdev = req->w.mdev;
  754. enum drbd_read_balancing rbm;
  755. if (req->private_bio) {
  756. if (!drbd_may_do_local_read(mdev,
  757. req->i.sector, req->i.size)) {
  758. bio_put(req->private_bio);
  759. req->private_bio = NULL;
  760. put_ldev(mdev);
  761. }
  762. }
  763. if (mdev->state.pdsk != D_UP_TO_DATE)
  764. return false;
  765. if (req->private_bio == NULL)
  766. return true;
  767. /* TODO: improve read balancing decisions, take into account drbd
  768. * protocol, pending requests etc. */
  769. rcu_read_lock();
  770. rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
  771. rcu_read_unlock();
  772. if (rbm == RB_PREFER_LOCAL && req->private_bio)
  773. return false; /* submit locally */
  774. if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
  775. if (req->private_bio) {
  776. bio_put(req->private_bio);
  777. req->private_bio = NULL;
  778. put_ldev(mdev);
  779. }
  780. return true;
  781. }
  782. return false;
  783. }
  784. /* returns number of connections (== 1, for drbd 8.4)
  785. * expected to actually write this data,
  786. * which does NOT include those that we are L_AHEAD for. */
  787. static int drbd_process_write_request(struct drbd_request *req)
  788. {
  789. struct drbd_conf *mdev = req->w.mdev;
  790. int remote, send_oos;
  791. rcu_read_lock();
  792. remote = drbd_should_do_remote(mdev->state);
  793. if (remote) {
  794. maybe_pull_ahead(mdev);
  795. remote = drbd_should_do_remote(mdev->state);
  796. }
  797. send_oos = drbd_should_send_out_of_sync(mdev->state);
  798. rcu_read_unlock();
  799. /* Need to replicate writes. Unless it is an empty flush,
  800. * which is better mapped to a DRBD P_BARRIER packet,
  801. * also for drbd wire protocol compatibility reasons.
  802. * If this was a flush, just start a new epoch.
  803. * Unless the current epoch was empty anyways, or we are not currently
  804. * replicating, in which case there is no point. */
  805. if (unlikely(req->i.size == 0)) {
  806. /* The only size==0 bios we expect are empty flushes. */
  807. D_ASSERT(req->master_bio->bi_rw & REQ_FLUSH);
  808. if (remote)
  809. start_new_tl_epoch(mdev->tconn);
  810. return 0;
  811. }
  812. if (!remote && !send_oos)
  813. return 0;
  814. D_ASSERT(!(remote && send_oos));
  815. if (remote) {
  816. _req_mod(req, TO_BE_SENT);
  817. _req_mod(req, QUEUE_FOR_NET_WRITE);
  818. } else if (drbd_set_out_of_sync(mdev, req->i.sector, req->i.size))
  819. _req_mod(req, QUEUE_FOR_SEND_OOS);
  820. return remote;
  821. }
  822. static void
  823. drbd_submit_req_private_bio(struct drbd_request *req)
  824. {
  825. struct drbd_conf *mdev = req->w.mdev;
  826. struct bio *bio = req->private_bio;
  827. const int rw = bio_rw(bio);
  828. bio->bi_bdev = mdev->ldev->backing_bdev;
  829. /* State may have changed since we grabbed our reference on the
  830. * ->ldev member. Double check, and short-circuit to endio.
  831. * In case the last activity log transaction failed to get on
  832. * stable storage, and this is a WRITE, we may not even submit
  833. * this bio. */
  834. if (get_ldev(mdev)) {
  835. if (drbd_insert_fault(mdev,
  836. rw == WRITE ? DRBD_FAULT_DT_WR
  837. : rw == READ ? DRBD_FAULT_DT_RD
  838. : DRBD_FAULT_DT_RA))
  839. bio_endio(bio, -EIO);
  840. else
  841. generic_make_request(bio);
  842. put_ldev(mdev);
  843. } else
  844. bio_endio(bio, -EIO);
  845. }
  846. void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  847. {
  848. const int rw = bio_rw(bio);
  849. struct bio_and_error m = { NULL, };
  850. struct drbd_request *req;
  851. bool no_remote = false;
  852. /* allocate outside of all locks; */
  853. req = drbd_req_new(mdev, bio);
  854. if (!req) {
  855. dec_ap_bio(mdev);
  856. /* only pass the error to the upper layers.
  857. * if user cannot handle io errors, that's not our business. */
  858. dev_err(DEV, "could not kmalloc() req\n");
  859. bio_endio(bio, -ENOMEM);
  860. return;
  861. }
  862. req->start_time = start_time;
  863. if (!get_ldev(mdev)) {
  864. bio_put(req->private_bio);
  865. req->private_bio = NULL;
  866. }
  867. /* For WRITES going to the local disk, grab a reference on the target
  868. * extent. This waits for any resync activity in the corresponding
  869. * resync extent to finish, and, if necessary, pulls in the target
  870. * extent into the activity log, which involves further disk io because
  871. * of transactional on-disk meta data updates.
  872. * Empty flushes don't need to go into the activity log, they can only
  873. * flush data for pending writes which are already in there. */
  874. if (rw == WRITE && req->private_bio && req->i.size
  875. && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  876. req->rq_state |= RQ_IN_ACT_LOG;
  877. drbd_al_begin_io(mdev, &req->i);
  878. }
  879. spin_lock_irq(&mdev->tconn->req_lock);
  880. if (rw == WRITE) {
  881. /* This may temporarily give up the req_lock,
  882. * but will re-aquire it before it returns here.
  883. * Needs to be before the check on drbd_suspended() */
  884. complete_conflicting_writes(req);
  885. }
  886. /* no more giving up req_lock from now on! */
  887. if (drbd_suspended(mdev)) {
  888. /* push back and retry: */
  889. req->rq_state |= RQ_POSTPONED;
  890. if (req->private_bio) {
  891. bio_put(req->private_bio);
  892. req->private_bio = NULL;
  893. put_ldev(mdev);
  894. }
  895. goto out;
  896. }
  897. /* Update disk stats */
  898. _drbd_start_io_acct(mdev, req, bio);
  899. /* We fail READ/READA early, if we can not serve it.
  900. * We must do this before req is registered on any lists.
  901. * Otherwise, drbd_req_complete() will queue failed READ for retry. */
  902. if (rw != WRITE) {
  903. if (!do_remote_read(req) && !req->private_bio)
  904. goto nodata;
  905. }
  906. /* which transfer log epoch does this belong to? */
  907. req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
  908. /* no point in adding empty flushes to the transfer log,
  909. * they are mapped to drbd barriers already. */
  910. if (likely(req->i.size!=0)) {
  911. if (rw == WRITE)
  912. mdev->tconn->current_tle_writes++;
  913. list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
  914. }
  915. if (rw == WRITE) {
  916. if (!drbd_process_write_request(req))
  917. no_remote = true;
  918. } else {
  919. /* We either have a private_bio, or we can read from remote.
  920. * Otherwise we had done the goto nodata above. */
  921. if (req->private_bio == NULL) {
  922. _req_mod(req, TO_BE_SENT);
  923. _req_mod(req, QUEUE_FOR_NET_READ);
  924. } else
  925. no_remote = true;
  926. }
  927. if (req->private_bio) {
  928. /* needs to be marked within the same spinlock */
  929. _req_mod(req, TO_BE_SUBMITTED);
  930. /* but we need to give up the spinlock to submit */
  931. spin_unlock_irq(&mdev->tconn->req_lock);
  932. drbd_submit_req_private_bio(req);
  933. spin_lock_irq(&mdev->tconn->req_lock);
  934. } else if (no_remote) {
  935. nodata:
  936. if (__ratelimit(&drbd_ratelimit_state))
  937. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  938. /* A write may have been queued for send_oos, however.
  939. * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
  940. }
  941. out:
  942. if (drbd_req_put_completion_ref(req, &m, 1))
  943. kref_put(&req->kref, drbd_req_destroy);
  944. spin_unlock_irq(&mdev->tconn->req_lock);
  945. if (m.bio)
  946. complete_master_bio(mdev, &m);
  947. return;
  948. }
  949. int drbd_make_request(struct request_queue *q, struct bio *bio)
  950. {
  951. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  952. unsigned long start_time;
  953. start_time = jiffies;
  954. /*
  955. * what we "blindly" assume:
  956. */
  957. D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
  958. inc_ap_bio(mdev);
  959. __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. struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
  992. {
  993. /* Walk the transfer log,
  994. * and find the oldest not yet completed request */
  995. struct drbd_request *r;
  996. list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
  997. if (atomic_read(&r->completion_ref))
  998. return r;
  999. }
  1000. return NULL;
  1001. }
  1002. void request_timer_fn(unsigned long data)
  1003. {
  1004. struct drbd_conf *mdev = (struct drbd_conf *) data;
  1005. struct drbd_tconn *tconn = mdev->tconn;
  1006. struct drbd_request *req; /* oldest request */
  1007. struct net_conf *nc;
  1008. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  1009. unsigned long now;
  1010. rcu_read_lock();
  1011. nc = rcu_dereference(tconn->net_conf);
  1012. if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
  1013. ent = nc->timeout * HZ/10 * nc->ko_count;
  1014. if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
  1015. dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
  1016. put_ldev(mdev);
  1017. }
  1018. rcu_read_unlock();
  1019. et = min_not_zero(dt, ent);
  1020. if (!et)
  1021. return; /* Recurring timer stopped */
  1022. now = jiffies;
  1023. spin_lock_irq(&tconn->req_lock);
  1024. req = find_oldest_request(tconn);
  1025. if (!req) {
  1026. spin_unlock_irq(&tconn->req_lock);
  1027. mod_timer(&mdev->request_timer, now + et);
  1028. return;
  1029. }
  1030. /* The request is considered timed out, if
  1031. * - we have some effective timeout from the configuration,
  1032. * with above state restrictions applied,
  1033. * - the oldest request is waiting for a response from the network
  1034. * resp. the local disk,
  1035. * - the oldest request is in fact older than the effective timeout,
  1036. * - the connection was established (resp. disk was attached)
  1037. * for longer than the timeout already.
  1038. * Note that for 32bit jiffies and very stable connections/disks,
  1039. * we may have a wrap around, which is catched by
  1040. * !time_in_range(now, last_..._jif, last_..._jif + timeout).
  1041. *
  1042. * Side effect: once per 32bit wrap-around interval, which means every
  1043. * ~198 days with 250 HZ, we have a window where the timeout would need
  1044. * to expire twice (worst case) to become effective. Good enough.
  1045. */
  1046. if (ent && req->rq_state & RQ_NET_PENDING &&
  1047. time_after(now, req->start_time + ent) &&
  1048. !time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
  1049. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  1050. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
  1051. }
  1052. if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
  1053. time_after(now, req->start_time + dt) &&
  1054. !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
  1055. dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
  1056. __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
  1057. }
  1058. nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
  1059. spin_unlock_irq(&tconn->req_lock);
  1060. mod_timer(&mdev->request_timer, nt);
  1061. }