drbd_req.c 39 KB

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