drbd_req.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. /* 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. static 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. /* obviously this could be coded as many single functions
  355. * instead of one huge switch,
  356. * or by putting the code directly in the respective locations
  357. * (as it has been before).
  358. *
  359. * but having it this way
  360. * enforces that it is all in this one place, where it is easier to audit,
  361. * it makes it obvious that whatever "event" "happens" to a request should
  362. * happen "atomically" within the req_lock,
  363. * and it enforces that we have to think in a very structured manner
  364. * about the "events" that may happen to a request during its life time ...
  365. */
  366. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  367. struct bio_and_error *m)
  368. {
  369. struct drbd_conf *mdev = req->w.mdev;
  370. struct net_conf *nc;
  371. int p, rv = 0;
  372. if (m)
  373. m->bio = NULL;
  374. switch (what) {
  375. default:
  376. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  377. break;
  378. /* does not happen...
  379. * initialization done in drbd_req_new
  380. case CREATED:
  381. break;
  382. */
  383. case TO_BE_SENT: /* via network */
  384. /* reached via __drbd_make_request
  385. * and from w_read_retry_remote */
  386. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  387. rcu_read_lock();
  388. nc = rcu_dereference(mdev->tconn->net_conf);
  389. p = nc->wire_protocol;
  390. rcu_read_unlock();
  391. req->rq_state |=
  392. p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
  393. p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
  394. mod_rq_state(req, m, 0, RQ_NET_PENDING);
  395. break;
  396. case TO_BE_SUBMITTED: /* locally */
  397. /* reached via __drbd_make_request */
  398. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  399. mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
  400. break;
  401. case COMPLETED_OK:
  402. if (req->rq_state & RQ_WRITE)
  403. mdev->writ_cnt += req->i.size >> 9;
  404. else
  405. mdev->read_cnt += req->i.size >> 9;
  406. mod_rq_state(req, m, RQ_LOCAL_PENDING,
  407. RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  408. break;
  409. case ABORT_DISK_IO:
  410. mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
  411. break;
  412. case READ_COMPLETED_WITH_ERROR:
  413. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  414. /* fall through. */
  415. case WRITE_COMPLETED_WITH_ERROR:
  416. __drbd_chk_io_error(mdev, DRBD_IO_ERROR);
  417. /* fall through. */
  418. case READ_AHEAD_COMPLETED_WITH_ERROR:
  419. /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
  420. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  421. break;
  422. case QUEUE_FOR_NET_READ:
  423. /* READ or READA, and
  424. * no local disk,
  425. * or target area marked as invalid,
  426. * or just got an io-error. */
  427. /* from __drbd_make_request
  428. * or from bio_endio during read io-error recovery */
  429. /* So we can verify the handle in the answer packet.
  430. * Corresponding drbd_remove_request_interval is in
  431. * drbd_req_complete() */
  432. D_ASSERT(drbd_interval_empty(&req->i));
  433. drbd_insert_interval(&mdev->read_requests, &req->i);
  434. set_bit(UNPLUG_REMOTE, &mdev->flags);
  435. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  436. D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
  437. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  438. req->w.cb = w_send_read_req;
  439. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  440. break;
  441. case QUEUE_FOR_NET_WRITE:
  442. /* assert something? */
  443. /* from __drbd_make_request only */
  444. /* Corresponding drbd_remove_request_interval is in
  445. * drbd_req_complete() */
  446. D_ASSERT(drbd_interval_empty(&req->i));
  447. drbd_insert_interval(&mdev->write_requests, &req->i);
  448. /* NOTE
  449. * In case the req ended up on the transfer log before being
  450. * queued on the worker, it could lead to this request being
  451. * missed during cleanup after connection loss.
  452. * So we have to do both operations here,
  453. * within the same lock that protects the transfer log.
  454. *
  455. * _req_add_to_epoch(req); this has to be after the
  456. * _maybe_start_new_epoch(req); which happened in
  457. * __drbd_make_request, because we now may set the bit
  458. * again ourselves to close the current epoch.
  459. *
  460. * Add req to the (now) current epoch (barrier). */
  461. /* otherwise we may lose an unplug, which may cause some remote
  462. * io-scheduler timeout to expire, increasing maximum latency,
  463. * hurting performance. */
  464. set_bit(UNPLUG_REMOTE, &mdev->flags);
  465. /* queue work item to send data */
  466. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  467. mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
  468. req->w.cb = w_send_dblock;
  469. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  470. /* close the epoch, in case it outgrew the limit */
  471. rcu_read_lock();
  472. nc = rcu_dereference(mdev->tconn->net_conf);
  473. p = nc->max_epoch_size;
  474. rcu_read_unlock();
  475. if (mdev->tconn->current_tle_writes >= p)
  476. start_new_tl_epoch(mdev->tconn);
  477. break;
  478. case QUEUE_FOR_SEND_OOS:
  479. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  480. req->w.cb = w_send_out_of_sync;
  481. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  482. break;
  483. case READ_RETRY_REMOTE_CANCELED:
  484. case SEND_CANCELED:
  485. case SEND_FAILED:
  486. /* real cleanup will be done from tl_clear. just update flags
  487. * so it is no longer marked as on the worker queue */
  488. mod_rq_state(req, m, RQ_NET_QUEUED, 0);
  489. break;
  490. case HANDED_OVER_TO_NETWORK:
  491. /* assert something? */
  492. if (bio_data_dir(req->master_bio) == WRITE &&
  493. !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
  494. /* this is what is dangerous about protocol A:
  495. * pretend it was successfully written on the peer. */
  496. if (req->rq_state & RQ_NET_PENDING)
  497. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  498. /* else: neg-ack was faster... */
  499. /* it is still not yet RQ_NET_DONE until the
  500. * corresponding epoch barrier got acked as well,
  501. * so we know what to dirty on connection loss */
  502. }
  503. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
  504. break;
  505. case OOS_HANDED_TO_NETWORK:
  506. /* Was not set PENDING, no longer QUEUED, so is now DONE
  507. * as far as this connection is concerned. */
  508. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
  509. break;
  510. case CONNECTION_LOST_WHILE_PENDING:
  511. /* transfer log cleanup after connection loss */
  512. mod_rq_state(req, m,
  513. RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
  514. RQ_NET_DONE);
  515. break;
  516. case CONFLICT_RESOLVED:
  517. /* for superseded conflicting writes of multiple primaries,
  518. * there is no need to keep anything in the tl, potential
  519. * node crashes are covered by the activity log.
  520. *
  521. * If this request had been marked as RQ_POSTPONED before,
  522. * it will actually not be completed, but "restarted",
  523. * resubmitted from the retry worker context. */
  524. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  525. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  526. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
  527. break;
  528. case WRITE_ACKED_BY_PEER_AND_SIS:
  529. req->rq_state |= RQ_NET_SIS;
  530. case WRITE_ACKED_BY_PEER:
  531. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  532. /* protocol C; successfully written on peer.
  533. * Nothing more to do here.
  534. * We want to keep the tl in place for all protocols, to cater
  535. * for volatile write-back caches on lower level devices. */
  536. goto ack_common;
  537. case RECV_ACKED_BY_PEER:
  538. D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
  539. /* protocol B; pretends to be successfully written on peer.
  540. * see also notes above in HANDED_OVER_TO_NETWORK about
  541. * protocol != C */
  542. ack_common:
  543. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  544. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  545. break;
  546. case POSTPONE_WRITE:
  547. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  548. /* If this node has already detected the write conflict, the
  549. * worker will be waiting on misc_wait. Wake it up once this
  550. * request has completed locally.
  551. */
  552. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  553. req->rq_state |= RQ_POSTPONED;
  554. if (req->i.waiting)
  555. wake_up(&mdev->misc_wait);
  556. /* Do not clear RQ_NET_PENDING. This request will make further
  557. * progress via restart_conflicting_writes() or
  558. * fail_postponed_requests(). Hopefully. */
  559. break;
  560. case NEG_ACKED:
  561. mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
  562. break;
  563. case FAIL_FROZEN_DISK_IO:
  564. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  565. break;
  566. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  567. break;
  568. case RESTART_FROZEN_DISK_IO:
  569. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  570. break;
  571. mod_rq_state(req, m,
  572. RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
  573. RQ_LOCAL_PENDING);
  574. rv = MR_READ;
  575. if (bio_data_dir(req->master_bio) == WRITE)
  576. rv = MR_WRITE;
  577. get_ldev(mdev); /* always succeeds in this call path */
  578. req->w.cb = w_restart_disk_io;
  579. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  580. break;
  581. case RESEND:
  582. /* Simply complete (local only) READs. */
  583. if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
  584. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  585. break;
  586. }
  587. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  588. before the connection loss (B&C only); only P_BARRIER_ACK
  589. (or the local completion?) was missing when we suspended.
  590. Throwing them out of the TL here by pretending we got a BARRIER_ACK.
  591. During connection handshake, we ensure that the peer was not rebooted. */
  592. if (!(req->rq_state & RQ_NET_OK)) {
  593. /* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
  594. * in that case we must not set RQ_NET_PENDING. */
  595. mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
  596. if (req->w.cb) {
  597. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  598. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  599. } /* else: FIXME can this happen? */
  600. break;
  601. }
  602. /* else, fall through to BARRIER_ACKED */
  603. case BARRIER_ACKED:
  604. /* barrier ack for READ requests does not make sense */
  605. if (!(req->rq_state & RQ_WRITE))
  606. break;
  607. if (req->rq_state & RQ_NET_PENDING) {
  608. /* barrier came in before all requests were acked.
  609. * this is bad, because if the connection is lost now,
  610. * we won't be able to clean them up... */
  611. dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
  612. }
  613. /* Allowed to complete requests, even while suspended.
  614. * As this is called for all requests within a matching epoch,
  615. * we need to filter, and only set RQ_NET_DONE for those that
  616. * have actually been on the wire. */
  617. mod_rq_state(req, m, RQ_COMPLETION_SUSP,
  618. (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
  619. break;
  620. case DATA_RECEIVED:
  621. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  622. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
  623. break;
  624. };
  625. return rv;
  626. }
  627. /* we may do a local read if:
  628. * - we are consistent (of course),
  629. * - or we are generally inconsistent,
  630. * BUT we are still/already IN SYNC for this area.
  631. * since size may be bigger than BM_BLOCK_SIZE,
  632. * we may need to check several bits.
  633. */
  634. static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  635. {
  636. unsigned long sbnr, ebnr;
  637. sector_t esector, nr_sectors;
  638. if (mdev->state.disk == D_UP_TO_DATE)
  639. return true;
  640. if (mdev->state.disk != D_INCONSISTENT)
  641. return false;
  642. esector = sector + (size >> 9) - 1;
  643. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  644. D_ASSERT(sector < nr_sectors);
  645. D_ASSERT(esector < nr_sectors);
  646. sbnr = BM_SECT_TO_BIT(sector);
  647. ebnr = BM_SECT_TO_BIT(esector);
  648. return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
  649. }
  650. static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
  651. enum drbd_read_balancing rbm)
  652. {
  653. struct backing_dev_info *bdi;
  654. int stripe_shift;
  655. switch (rbm) {
  656. case RB_CONGESTED_REMOTE:
  657. bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
  658. return bdi_read_congested(bdi);
  659. case RB_LEAST_PENDING:
  660. return atomic_read(&mdev->local_cnt) >
  661. atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
  662. case RB_32K_STRIPING: /* stripe_shift = 15 */
  663. case RB_64K_STRIPING:
  664. case RB_128K_STRIPING:
  665. case RB_256K_STRIPING:
  666. case RB_512K_STRIPING:
  667. case RB_1M_STRIPING: /* stripe_shift = 20 */
  668. stripe_shift = (rbm - RB_32K_STRIPING + 15);
  669. return (sector >> (stripe_shift - 9)) & 1;
  670. case RB_ROUND_ROBIN:
  671. return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
  672. case RB_PREFER_REMOTE:
  673. return true;
  674. case RB_PREFER_LOCAL:
  675. default:
  676. return false;
  677. }
  678. }
  679. /*
  680. * complete_conflicting_writes - wait for any conflicting write requests
  681. *
  682. * The write_requests tree contains all active write requests which we
  683. * currently know about. Wait for any requests to complete which conflict with
  684. * the new one.
  685. *
  686. * Only way out: remove the conflicting intervals from the tree.
  687. */
  688. static void complete_conflicting_writes(struct drbd_request *req)
  689. {
  690. DEFINE_WAIT(wait);
  691. struct drbd_conf *mdev = req->w.mdev;
  692. struct drbd_interval *i;
  693. sector_t sector = req->i.sector;
  694. int size = req->i.size;
  695. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  696. if (!i)
  697. return;
  698. for (;;) {
  699. prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
  700. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  701. if (!i)
  702. break;
  703. /* Indicate to wake up device->misc_wait on progress. */
  704. i->waiting = true;
  705. spin_unlock_irq(&mdev->tconn->req_lock);
  706. schedule();
  707. spin_lock_irq(&mdev->tconn->req_lock);
  708. }
  709. finish_wait(&mdev->misc_wait, &wait);
  710. }
  711. /* called within req_lock and rcu_read_lock() */
  712. static void maybe_pull_ahead(struct drbd_conf *mdev)
  713. {
  714. struct drbd_tconn *tconn = mdev->tconn;
  715. struct net_conf *nc;
  716. bool congested = false;
  717. enum drbd_on_congestion on_congestion;
  718. nc = rcu_dereference(tconn->net_conf);
  719. on_congestion = nc ? nc->on_congestion : OC_BLOCK;
  720. if (on_congestion == OC_BLOCK ||
  721. tconn->agreed_pro_version < 96)
  722. return;
  723. /* If I don't even have good local storage, we can not reasonably try
  724. * to pull ahead of the peer. We also need the local reference to make
  725. * sure mdev->act_log is there.
  726. */
  727. if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
  728. return;
  729. if (nc->cong_fill &&
  730. atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
  731. dev_info(DEV, "Congestion-fill threshold reached\n");
  732. congested = true;
  733. }
  734. if (mdev->act_log->used >= nc->cong_extents) {
  735. dev_info(DEV, "Congestion-extents threshold reached\n");
  736. congested = true;
  737. }
  738. if (congested) {
  739. /* start a new epoch for non-mirrored writes */
  740. start_new_tl_epoch(mdev->tconn);
  741. if (on_congestion == OC_PULL_AHEAD)
  742. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  743. else /*nc->on_congestion == OC_DISCONNECT */
  744. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  745. }
  746. put_ldev(mdev);
  747. }
  748. /* If this returns false, and req->private_bio is still set,
  749. * this should be submitted locally.
  750. *
  751. * If it returns false, but req->private_bio is not set,
  752. * we do not have access to good data :(
  753. *
  754. * Otherwise, this destroys req->private_bio, if any,
  755. * and returns true.
  756. */
  757. static bool do_remote_read(struct drbd_request *req)
  758. {
  759. struct drbd_conf *mdev = req->w.mdev;
  760. enum drbd_read_balancing rbm;
  761. if (req->private_bio) {
  762. if (!drbd_may_do_local_read(mdev,
  763. req->i.sector, req->i.size)) {
  764. bio_put(req->private_bio);
  765. req->private_bio = NULL;
  766. put_ldev(mdev);
  767. }
  768. }
  769. if (mdev->state.pdsk != D_UP_TO_DATE)
  770. return false;
  771. if (req->private_bio == NULL)
  772. return true;
  773. /* TODO: improve read balancing decisions, take into account drbd
  774. * protocol, pending requests etc. */
  775. rcu_read_lock();
  776. rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
  777. rcu_read_unlock();
  778. if (rbm == RB_PREFER_LOCAL && req->private_bio)
  779. return false; /* submit locally */
  780. if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
  781. if (req->private_bio) {
  782. bio_put(req->private_bio);
  783. req->private_bio = NULL;
  784. put_ldev(mdev);
  785. }
  786. return true;
  787. }
  788. return false;
  789. }
  790. /* returns number of connections (== 1, for drbd 8.4)
  791. * expected to actually write this data,
  792. * which does NOT include those that we are L_AHEAD for. */
  793. static int drbd_process_write_request(struct drbd_request *req)
  794. {
  795. struct drbd_conf *mdev = req->w.mdev;
  796. int remote, send_oos;
  797. rcu_read_lock();
  798. remote = drbd_should_do_remote(mdev->state);
  799. if (remote) {
  800. maybe_pull_ahead(mdev);
  801. remote = drbd_should_do_remote(mdev->state);
  802. }
  803. send_oos = drbd_should_send_out_of_sync(mdev->state);
  804. rcu_read_unlock();
  805. /* Need to replicate writes. Unless it is an empty flush,
  806. * which is better mapped to a DRBD P_BARRIER packet,
  807. * also for drbd wire protocol compatibility reasons.
  808. * If this was a flush, just start a new epoch.
  809. * Unless the current epoch was empty anyways, or we are not currently
  810. * replicating, in which case there is no point. */
  811. if (unlikely(req->i.size == 0)) {
  812. /* The only size==0 bios we expect are empty flushes. */
  813. D_ASSERT(req->master_bio->bi_rw & REQ_FLUSH);
  814. if (remote)
  815. start_new_tl_epoch(mdev->tconn);
  816. return 0;
  817. }
  818. if (!remote && !send_oos)
  819. return 0;
  820. D_ASSERT(!(remote && send_oos));
  821. if (remote) {
  822. _req_mod(req, TO_BE_SENT);
  823. _req_mod(req, QUEUE_FOR_NET_WRITE);
  824. } else if (drbd_set_out_of_sync(mdev, req->i.sector, req->i.size))
  825. _req_mod(req, QUEUE_FOR_SEND_OOS);
  826. return remote;
  827. }
  828. static void
  829. drbd_submit_req_private_bio(struct drbd_request *req)
  830. {
  831. struct drbd_conf *mdev = req->w.mdev;
  832. struct bio *bio = req->private_bio;
  833. const int rw = bio_rw(bio);
  834. bio->bi_bdev = mdev->ldev->backing_bdev;
  835. /* State may have changed since we grabbed our reference on the
  836. * ->ldev member. Double check, and short-circuit to endio.
  837. * In case the last activity log transaction failed to get on
  838. * stable storage, and this is a WRITE, we may not even submit
  839. * this bio. */
  840. if (get_ldev(mdev)) {
  841. if (drbd_insert_fault(mdev,
  842. rw == WRITE ? DRBD_FAULT_DT_WR
  843. : rw == READ ? DRBD_FAULT_DT_RD
  844. : DRBD_FAULT_DT_RA))
  845. bio_endio(bio, -EIO);
  846. else
  847. generic_make_request(bio);
  848. put_ldev(mdev);
  849. } else
  850. bio_endio(bio, -EIO);
  851. }
  852. void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  853. {
  854. const int rw = bio_rw(bio);
  855. struct bio_and_error m = { NULL, };
  856. struct drbd_request *req;
  857. bool no_remote = false;
  858. /* allocate outside of all locks; */
  859. req = drbd_req_new(mdev, bio);
  860. if (!req) {
  861. dec_ap_bio(mdev);
  862. /* only pass the error to the upper layers.
  863. * if user cannot handle io errors, that's not our business. */
  864. dev_err(DEV, "could not kmalloc() req\n");
  865. bio_endio(bio, -ENOMEM);
  866. return;
  867. }
  868. req->start_time = start_time;
  869. if (!get_ldev(mdev)) {
  870. bio_put(req->private_bio);
  871. req->private_bio = NULL;
  872. }
  873. /* For WRITES going to the local disk, grab a reference on the target
  874. * extent. This waits for any resync activity in the corresponding
  875. * resync extent to finish, and, if necessary, pulls in the target
  876. * extent into the activity log, which involves further disk io because
  877. * of transactional on-disk meta data updates.
  878. * Empty flushes don't need to go into the activity log, they can only
  879. * flush data for pending writes which are already in there. */
  880. if (rw == WRITE && req->private_bio && req->i.size
  881. && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  882. req->rq_state |= RQ_IN_ACT_LOG;
  883. drbd_al_begin_io(mdev, &req->i);
  884. }
  885. spin_lock_irq(&mdev->tconn->req_lock);
  886. if (rw == WRITE) {
  887. /* This may temporarily give up the req_lock,
  888. * but will re-aquire it before it returns here.
  889. * Needs to be before the check on drbd_suspended() */
  890. complete_conflicting_writes(req);
  891. }
  892. /* no more giving up req_lock from now on! */
  893. if (drbd_suspended(mdev)) {
  894. /* push back and retry: */
  895. req->rq_state |= RQ_POSTPONED;
  896. if (req->private_bio) {
  897. bio_put(req->private_bio);
  898. req->private_bio = NULL;
  899. put_ldev(mdev);
  900. }
  901. goto out;
  902. }
  903. /* Update disk stats */
  904. _drbd_start_io_acct(mdev, req, bio);
  905. /* We fail READ/READA early, if we can not serve it.
  906. * We must do this before req is registered on any lists.
  907. * Otherwise, drbd_req_complete() will queue failed READ for retry. */
  908. if (rw != WRITE) {
  909. if (!do_remote_read(req) && !req->private_bio)
  910. goto nodata;
  911. }
  912. /* which transfer log epoch does this belong to? */
  913. req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
  914. /* no point in adding empty flushes to the transfer log,
  915. * they are mapped to drbd barriers already. */
  916. if (likely(req->i.size!=0)) {
  917. if (rw == WRITE)
  918. mdev->tconn->current_tle_writes++;
  919. list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
  920. }
  921. if (rw == WRITE) {
  922. if (!drbd_process_write_request(req))
  923. no_remote = true;
  924. } else {
  925. /* We either have a private_bio, or we can read from remote.
  926. * Otherwise we had done the goto nodata above. */
  927. if (req->private_bio == NULL) {
  928. _req_mod(req, TO_BE_SENT);
  929. _req_mod(req, QUEUE_FOR_NET_READ);
  930. } else
  931. no_remote = true;
  932. }
  933. if (req->private_bio) {
  934. /* needs to be marked within the same spinlock */
  935. _req_mod(req, TO_BE_SUBMITTED);
  936. /* but we need to give up the spinlock to submit */
  937. spin_unlock_irq(&mdev->tconn->req_lock);
  938. drbd_submit_req_private_bio(req);
  939. spin_lock_irq(&mdev->tconn->req_lock);
  940. } else if (no_remote) {
  941. nodata:
  942. if (__ratelimit(&drbd_ratelimit_state))
  943. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  944. /* A write may have been queued for send_oos, however.
  945. * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
  946. }
  947. out:
  948. if (drbd_req_put_completion_ref(req, &m, 1))
  949. kref_put(&req->kref, drbd_req_destroy);
  950. spin_unlock_irq(&mdev->tconn->req_lock);
  951. if (m.bio)
  952. complete_master_bio(mdev, &m);
  953. return;
  954. }
  955. int drbd_make_request(struct request_queue *q, struct bio *bio)
  956. {
  957. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  958. unsigned long start_time;
  959. start_time = jiffies;
  960. /*
  961. * what we "blindly" assume:
  962. */
  963. D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
  964. inc_ap_bio(mdev);
  965. __drbd_make_request(mdev, bio, start_time);
  966. return 0;
  967. }
  968. /* This is called by bio_add_page().
  969. *
  970. * q->max_hw_sectors and other global limits are already enforced there.
  971. *
  972. * We need to call down to our lower level device,
  973. * in case it has special restrictions.
  974. *
  975. * We also may need to enforce configured max-bio-bvecs limits.
  976. *
  977. * As long as the BIO is empty we have to allow at least one bvec,
  978. * regardless of size and offset, so no need to ask lower levels.
  979. */
  980. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  981. {
  982. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  983. unsigned int bio_size = bvm->bi_size;
  984. int limit = DRBD_MAX_BIO_SIZE;
  985. int backing_limit;
  986. if (bio_size && get_ldev(mdev)) {
  987. struct request_queue * const b =
  988. mdev->ldev->backing_bdev->bd_disk->queue;
  989. if (b->merge_bvec_fn) {
  990. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  991. limit = min(limit, backing_limit);
  992. }
  993. put_ldev(mdev);
  994. }
  995. return limit;
  996. }
  997. struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
  998. {
  999. /* Walk the transfer log,
  1000. * and find the oldest not yet completed request */
  1001. struct drbd_request *r;
  1002. list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
  1003. if (atomic_read(&r->completion_ref))
  1004. return r;
  1005. }
  1006. return NULL;
  1007. }
  1008. void request_timer_fn(unsigned long data)
  1009. {
  1010. struct drbd_conf *mdev = (struct drbd_conf *) data;
  1011. struct drbd_tconn *tconn = mdev->tconn;
  1012. struct drbd_request *req; /* oldest request */
  1013. struct net_conf *nc;
  1014. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  1015. unsigned long now;
  1016. rcu_read_lock();
  1017. nc = rcu_dereference(tconn->net_conf);
  1018. if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
  1019. ent = nc->timeout * HZ/10 * nc->ko_count;
  1020. if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
  1021. dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
  1022. put_ldev(mdev);
  1023. }
  1024. rcu_read_unlock();
  1025. et = min_not_zero(dt, ent);
  1026. if (!et)
  1027. return; /* Recurring timer stopped */
  1028. now = jiffies;
  1029. spin_lock_irq(&tconn->req_lock);
  1030. req = find_oldest_request(tconn);
  1031. if (!req) {
  1032. spin_unlock_irq(&tconn->req_lock);
  1033. mod_timer(&mdev->request_timer, now + et);
  1034. return;
  1035. }
  1036. /* The request is considered timed out, if
  1037. * - we have some effective timeout from the configuration,
  1038. * with above state restrictions applied,
  1039. * - the oldest request is waiting for a response from the network
  1040. * resp. the local disk,
  1041. * - the oldest request is in fact older than the effective timeout,
  1042. * - the connection was established (resp. disk was attached)
  1043. * for longer than the timeout already.
  1044. * Note that for 32bit jiffies and very stable connections/disks,
  1045. * we may have a wrap around, which is catched by
  1046. * !time_in_range(now, last_..._jif, last_..._jif + timeout).
  1047. *
  1048. * Side effect: once per 32bit wrap-around interval, which means every
  1049. * ~198 days with 250 HZ, we have a window where the timeout would need
  1050. * to expire twice (worst case) to become effective. Good enough.
  1051. */
  1052. if (ent && req->rq_state & RQ_NET_PENDING &&
  1053. time_after(now, req->start_time + ent) &&
  1054. !time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
  1055. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  1056. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
  1057. }
  1058. if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
  1059. time_after(now, req->start_time + dt) &&
  1060. !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
  1061. dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
  1062. __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
  1063. }
  1064. nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
  1065. spin_unlock_irq(&tconn->req_lock);
  1066. mod_timer(&mdev->request_timer, nt);
  1067. }