drbd_req.c 38 KB

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