drbd_req.c 37 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. int rw = req->rq_state & RQ_WRITE ? WRITE : READ;
  226. /* we must not complete the master bio, while it is
  227. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  228. * not yet acknowledged by the peer
  229. * not yet completed by the local io subsystem
  230. * these flags may get cleared in any order by
  231. * the worker,
  232. * the receiver,
  233. * the bio_endio completion callbacks.
  234. */
  235. if (s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED))
  236. return;
  237. if (s & RQ_NET_QUEUED)
  238. return;
  239. if (s & RQ_NET_PENDING)
  240. return;
  241. if (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 (!(s & RQ_POSTPONED)) {
  281. m->error = ok ? 0 : (error ?: -EIO);
  282. m->bio = req->master_bio;
  283. req->master_bio = NULL;
  284. } else {
  285. /* Assert that this will be _req_is_done()
  286. * with this very invokation. */
  287. /* FIXME:
  288. * what about (RQ_LOCAL_PENDING | RQ_LOCAL_ABORTED)?
  289. */
  290. D_ASSERT(!(s & RQ_LOCAL_PENDING));
  291. D_ASSERT(s & RQ_NET_DONE);
  292. }
  293. }
  294. req_may_be_done(req);
  295. }
  296. static void req_may_be_completed_not_susp(struct drbd_request *req, struct bio_and_error *m)
  297. {
  298. struct drbd_conf *mdev = req->w.mdev;
  299. if (!drbd_suspended(mdev))
  300. req_may_be_completed(req, m);
  301. }
  302. /* obviously this could be coded as many single functions
  303. * instead of one huge switch,
  304. * or by putting the code directly in the respective locations
  305. * (as it has been before).
  306. *
  307. * but having it this way
  308. * enforces that it is all in this one place, where it is easier to audit,
  309. * it makes it obvious that whatever "event" "happens" to a request should
  310. * happen "atomically" within the req_lock,
  311. * and it enforces that we have to think in a very structured manner
  312. * about the "events" that may happen to a request during its life time ...
  313. */
  314. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  315. struct bio_and_error *m)
  316. {
  317. struct drbd_conf *mdev = req->w.mdev;
  318. struct net_conf *nc;
  319. int p, rv = 0;
  320. if (m)
  321. m->bio = NULL;
  322. switch (what) {
  323. default:
  324. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  325. break;
  326. /* does not happen...
  327. * initialization done in drbd_req_new
  328. case CREATED:
  329. break;
  330. */
  331. case TO_BE_SENT: /* via network */
  332. /* reached via __drbd_make_request
  333. * and from w_read_retry_remote */
  334. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  335. req->rq_state |= RQ_NET_PENDING;
  336. rcu_read_lock();
  337. nc = rcu_dereference(mdev->tconn->net_conf);
  338. p = nc->wire_protocol;
  339. rcu_read_unlock();
  340. req->rq_state |=
  341. p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
  342. p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
  343. inc_ap_pending(mdev);
  344. break;
  345. case TO_BE_SUBMITTED: /* locally */
  346. /* reached via __drbd_make_request */
  347. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  348. req->rq_state |= RQ_LOCAL_PENDING;
  349. break;
  350. case COMPLETED_OK:
  351. if (req->rq_state & RQ_WRITE)
  352. mdev->writ_cnt += req->i.size >> 9;
  353. else
  354. mdev->read_cnt += req->i.size >> 9;
  355. req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  356. req->rq_state &= ~RQ_LOCAL_PENDING;
  357. maybe_wakeup_conflicting_requests(req);
  358. req_may_be_completed_not_susp(req, m);
  359. break;
  360. case ABORT_DISK_IO:
  361. req->rq_state |= RQ_LOCAL_ABORTED;
  362. if (req->rq_state & RQ_WRITE)
  363. req_may_be_completed_not_susp(req, m);
  364. else
  365. goto goto_queue_for_net_read;
  366. break;
  367. case WRITE_COMPLETED_WITH_ERROR:
  368. req->rq_state |= RQ_LOCAL_COMPLETED;
  369. req->rq_state &= ~RQ_LOCAL_PENDING;
  370. __drbd_chk_io_error(mdev, false);
  371. maybe_wakeup_conflicting_requests(req);
  372. req_may_be_completed_not_susp(req, m);
  373. break;
  374. case READ_AHEAD_COMPLETED_WITH_ERROR:
  375. /* it is legal to fail READA */
  376. req->rq_state |= RQ_LOCAL_COMPLETED;
  377. req->rq_state &= ~RQ_LOCAL_PENDING;
  378. req_may_be_completed_not_susp(req, m);
  379. break;
  380. case READ_COMPLETED_WITH_ERROR:
  381. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  382. req->rq_state |= RQ_LOCAL_COMPLETED;
  383. req->rq_state &= ~RQ_LOCAL_PENDING;
  384. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  385. __drbd_chk_io_error(mdev, false);
  386. goto_queue_for_net_read:
  387. /* no point in retrying if there is no good remote data,
  388. * or we have no connection. */
  389. if (mdev->state.pdsk != D_UP_TO_DATE) {
  390. req_may_be_completed_not_susp(req, m);
  391. break;
  392. }
  393. /* _req_mod(req,TO_BE_SENT); oops, recursion... */
  394. req->rq_state |= RQ_NET_PENDING;
  395. inc_ap_pending(mdev);
  396. /* fall through: _req_mod(req,QUEUE_FOR_NET_READ); */
  397. case QUEUE_FOR_NET_READ:
  398. /* READ or READA, and
  399. * no local disk,
  400. * or target area marked as invalid,
  401. * or just got an io-error. */
  402. /* from __drbd_make_request
  403. * or from bio_endio during read io-error recovery */
  404. /* So we can verify the handle in the answer packet.
  405. * Corresponding drbd_remove_request_interval is in
  406. * req_may_be_completed() */
  407. D_ASSERT(drbd_interval_empty(&req->i));
  408. drbd_insert_interval(&mdev->read_requests, &req->i);
  409. set_bit(UNPLUG_REMOTE, &mdev->flags);
  410. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  411. req->rq_state |= RQ_NET_QUEUED;
  412. req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
  413. ? w_read_retry_remote
  414. : w_send_read_req;
  415. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  416. break;
  417. case QUEUE_FOR_NET_WRITE:
  418. /* assert something? */
  419. /* from __drbd_make_request only */
  420. /* Corresponding drbd_remove_request_interval is in
  421. * req_may_be_completed() */
  422. D_ASSERT(drbd_interval_empty(&req->i));
  423. drbd_insert_interval(&mdev->write_requests, &req->i);
  424. /* NOTE
  425. * In case the req ended up on the transfer log before being
  426. * queued on the worker, it could lead to this request being
  427. * missed during cleanup after connection loss.
  428. * So we have to do both operations here,
  429. * within the same lock that protects the transfer log.
  430. *
  431. * _req_add_to_epoch(req); this has to be after the
  432. * _maybe_start_new_epoch(req); which happened in
  433. * __drbd_make_request, because we now may set the bit
  434. * again ourselves to close the current epoch.
  435. *
  436. * Add req to the (now) current epoch (barrier). */
  437. /* otherwise we may lose an unplug, which may cause some remote
  438. * io-scheduler timeout to expire, increasing maximum latency,
  439. * hurting performance. */
  440. set_bit(UNPLUG_REMOTE, &mdev->flags);
  441. /* see __drbd_make_request,
  442. * just after it grabs the req_lock */
  443. D_ASSERT(test_bit(CREATE_BARRIER, &mdev->tconn->flags) == 0);
  444. req->epoch = mdev->tconn->newest_tle->br_number;
  445. /* increment size of current epoch */
  446. mdev->tconn->newest_tle->n_writes++;
  447. /* queue work item to send data */
  448. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  449. req->rq_state |= RQ_NET_QUEUED;
  450. req->w.cb = w_send_dblock;
  451. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  452. /* close the epoch, in case it outgrew the limit */
  453. rcu_read_lock();
  454. nc = rcu_dereference(mdev->tconn->net_conf);
  455. p = nc->max_epoch_size;
  456. rcu_read_unlock();
  457. if (mdev->tconn->newest_tle->n_writes >= p)
  458. queue_barrier(mdev);
  459. break;
  460. case QUEUE_FOR_SEND_OOS:
  461. req->rq_state |= RQ_NET_QUEUED;
  462. req->w.cb = w_send_out_of_sync;
  463. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  464. break;
  465. case READ_RETRY_REMOTE_CANCELED:
  466. case SEND_CANCELED:
  467. case SEND_FAILED:
  468. /* real cleanup will be done from tl_clear. just update flags
  469. * so it is no longer marked as on the worker queue */
  470. req->rq_state &= ~RQ_NET_QUEUED;
  471. /* if we did it right, tl_clear should be scheduled only after
  472. * this, so this should not be necessary! */
  473. req_may_be_completed_not_susp(req, m);
  474. break;
  475. case HANDED_OVER_TO_NETWORK:
  476. /* assert something? */
  477. if (bio_data_dir(req->master_bio) == WRITE)
  478. atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
  479. if (bio_data_dir(req->master_bio) == WRITE &&
  480. !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
  481. /* this is what is dangerous about protocol A:
  482. * pretend it was successfully written on the peer. */
  483. if (req->rq_state & RQ_NET_PENDING) {
  484. dec_ap_pending(mdev);
  485. req->rq_state &= ~RQ_NET_PENDING;
  486. req->rq_state |= RQ_NET_OK;
  487. } /* else: neg-ack was faster... */
  488. /* it is still not yet RQ_NET_DONE until the
  489. * corresponding epoch barrier got acked as well,
  490. * so we know what to dirty on connection loss */
  491. }
  492. req->rq_state &= ~RQ_NET_QUEUED;
  493. req->rq_state |= RQ_NET_SENT;
  494. req_may_be_completed_not_susp(req, m);
  495. break;
  496. case OOS_HANDED_TO_NETWORK:
  497. /* Was not set PENDING, no longer QUEUED, so is now DONE
  498. * as far as this connection is concerned. */
  499. req->rq_state &= ~RQ_NET_QUEUED;
  500. req->rq_state |= RQ_NET_DONE;
  501. req_may_be_completed_not_susp(req, m);
  502. break;
  503. case CONNECTION_LOST_WHILE_PENDING:
  504. /* transfer log cleanup after connection loss */
  505. /* assert something? */
  506. if (req->rq_state & RQ_NET_PENDING)
  507. dec_ap_pending(mdev);
  508. p = !(req->rq_state & RQ_WRITE) && req->rq_state & RQ_NET_PENDING;
  509. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  510. req->rq_state |= RQ_NET_DONE;
  511. if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE)
  512. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  513. /* if it is still queued, we may not complete it here.
  514. * it will be canceled soon. */
  515. if (!(req->rq_state & RQ_NET_QUEUED)) {
  516. if (p)
  517. goto goto_read_retry_local;
  518. req_may_be_completed(req, m); /* Allowed while state.susp */
  519. }
  520. break;
  521. case DISCARD_WRITE:
  522. /* for discarded conflicting writes of multiple primaries,
  523. * there is no need to keep anything in the tl, potential
  524. * node crashes are covered by the activity log. */
  525. req->rq_state |= RQ_NET_DONE;
  526. /* fall through */
  527. case WRITE_ACKED_BY_PEER_AND_SIS:
  528. case WRITE_ACKED_BY_PEER:
  529. if (what == WRITE_ACKED_BY_PEER_AND_SIS)
  530. req->rq_state |= RQ_NET_SIS;
  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. req->rq_state |= RQ_NET_OK;
  544. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  545. dec_ap_pending(mdev);
  546. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  547. req->rq_state &= ~RQ_NET_PENDING;
  548. maybe_wakeup_conflicting_requests(req);
  549. req_may_be_completed_not_susp(req, m);
  550. break;
  551. case POSTPONE_WRITE:
  552. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  553. /* If this node has already detected the write conflict, the
  554. * worker will be waiting on misc_wait. Wake it up once this
  555. * request has completed locally.
  556. */
  557. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  558. req->rq_state |= RQ_POSTPONED;
  559. maybe_wakeup_conflicting_requests(req);
  560. req_may_be_completed_not_susp(req, m);
  561. break;
  562. case NEG_ACKED:
  563. /* assert something? */
  564. if (req->rq_state & RQ_NET_PENDING) {
  565. dec_ap_pending(mdev);
  566. if (req->rq_state & RQ_WRITE)
  567. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  568. }
  569. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  570. req->rq_state |= RQ_NET_DONE;
  571. if (!(req->rq_state & RQ_WRITE))
  572. goto goto_read_retry_local;
  573. maybe_wakeup_conflicting_requests(req);
  574. req_may_be_completed_not_susp(req, m);
  575. /* else: done by HANDED_OVER_TO_NETWORK */
  576. break;
  577. goto_read_retry_local:
  578. if (!drbd_may_do_local_read(mdev, req->i.sector, req->i.size)) {
  579. req_may_be_completed_not_susp(req, m);
  580. break;
  581. }
  582. D_ASSERT(!(req->rq_state & RQ_LOCAL_PENDING));
  583. req->rq_state |= RQ_LOCAL_PENDING;
  584. get_ldev(mdev);
  585. req->w.cb = w_restart_disk_io;
  586. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  587. break;
  588. case FAIL_FROZEN_DISK_IO:
  589. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  590. break;
  591. req_may_be_completed(req, m); /* Allowed while state.susp */
  592. break;
  593. case RESTART_FROZEN_DISK_IO:
  594. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  595. break;
  596. req->rq_state &= ~RQ_LOCAL_COMPLETED;
  597. rv = MR_READ;
  598. if (bio_data_dir(req->master_bio) == WRITE)
  599. rv = MR_WRITE;
  600. get_ldev(mdev);
  601. req->w.cb = w_restart_disk_io;
  602. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  603. break;
  604. case RESEND:
  605. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  606. before the connection loss (B&C only); only P_BARRIER_ACK was missing.
  607. Throwing them out of the TL here by pretending we got a BARRIER_ACK.
  608. During connection handshake, we ensure that the peer was not rebooted. */
  609. if (!(req->rq_state & RQ_NET_OK)) {
  610. if (req->w.cb) {
  611. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  612. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  613. }
  614. break;
  615. }
  616. /* else, fall through to BARRIER_ACKED */
  617. case BARRIER_ACKED:
  618. if (!(req->rq_state & RQ_WRITE))
  619. break;
  620. if (req->rq_state & RQ_NET_PENDING) {
  621. /* barrier came in before all requests were acked.
  622. * this is bad, because if the connection is lost now,
  623. * we won't be able to clean them up... */
  624. dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
  625. list_move(&req->tl_requests, &mdev->tconn->out_of_sequence_requests);
  626. }
  627. if ((req->rq_state & RQ_NET_MASK) != 0) {
  628. req->rq_state |= RQ_NET_DONE;
  629. if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)))
  630. atomic_sub(req->i.size>>9, &mdev->ap_in_flight);
  631. }
  632. req_may_be_done(req); /* Allowed while state.susp */
  633. break;
  634. case DATA_RECEIVED:
  635. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  636. dec_ap_pending(mdev);
  637. req->rq_state &= ~RQ_NET_PENDING;
  638. req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
  639. req_may_be_completed_not_susp(req, m);
  640. break;
  641. };
  642. return rv;
  643. }
  644. /* we may do a local read if:
  645. * - we are consistent (of course),
  646. * - or we are generally inconsistent,
  647. * BUT we are still/already IN SYNC for this area.
  648. * since size may be bigger than BM_BLOCK_SIZE,
  649. * we may need to check several bits.
  650. */
  651. static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  652. {
  653. unsigned long sbnr, ebnr;
  654. sector_t esector, nr_sectors;
  655. if (mdev->state.disk == D_UP_TO_DATE)
  656. return true;
  657. if (mdev->state.disk != D_INCONSISTENT)
  658. return false;
  659. esector = sector + (size >> 9) - 1;
  660. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  661. D_ASSERT(sector < nr_sectors);
  662. D_ASSERT(esector < nr_sectors);
  663. sbnr = BM_SECT_TO_BIT(sector);
  664. ebnr = BM_SECT_TO_BIT(esector);
  665. return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
  666. }
  667. static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector)
  668. {
  669. enum drbd_read_balancing rbm;
  670. struct backing_dev_info *bdi;
  671. int stripe_shift;
  672. if (mdev->state.pdsk < D_UP_TO_DATE)
  673. return false;
  674. rcu_read_lock();
  675. rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
  676. rcu_read_unlock();
  677. switch (rbm) {
  678. case RB_CONGESTED_REMOTE:
  679. bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
  680. return bdi_read_congested(bdi);
  681. case RB_LEAST_PENDING:
  682. return atomic_read(&mdev->local_cnt) >
  683. atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
  684. case RB_32K_STRIPING: /* stripe_shift = 15 */
  685. case RB_64K_STRIPING:
  686. case RB_128K_STRIPING:
  687. case RB_256K_STRIPING:
  688. case RB_512K_STRIPING:
  689. case RB_1M_STRIPING: /* stripe_shift = 20 */
  690. stripe_shift = (rbm - RB_32K_STRIPING + 15);
  691. return (sector >> (stripe_shift - 9)) & 1;
  692. case RB_ROUND_ROBIN:
  693. return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
  694. case RB_PREFER_REMOTE:
  695. return true;
  696. case RB_PREFER_LOCAL:
  697. default:
  698. return false;
  699. }
  700. }
  701. /*
  702. * complete_conflicting_writes - wait for any conflicting write requests
  703. *
  704. * The write_requests tree contains all active write requests which we
  705. * currently know about. Wait for any requests to complete which conflict with
  706. * the new one.
  707. */
  708. static int complete_conflicting_writes(struct drbd_conf *mdev,
  709. sector_t sector, int size)
  710. {
  711. for(;;) {
  712. struct drbd_interval *i;
  713. int err;
  714. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  715. if (!i)
  716. return 0;
  717. err = drbd_wait_misc(mdev, i);
  718. if (err)
  719. return err;
  720. }
  721. }
  722. int __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  723. {
  724. const int rw = bio_rw(bio);
  725. const int size = bio->bi_size;
  726. const sector_t sector = bio->bi_sector;
  727. struct drbd_tl_epoch *b = NULL;
  728. struct drbd_request *req;
  729. struct net_conf *nc;
  730. int local, remote, send_oos = 0;
  731. int err;
  732. int ret = 0;
  733. union drbd_dev_state s;
  734. /* allocate outside of all locks; */
  735. req = drbd_req_new(mdev, bio);
  736. if (!req) {
  737. dec_ap_bio(mdev);
  738. /* only pass the error to the upper layers.
  739. * if user cannot handle io errors, that's not our business. */
  740. dev_err(DEV, "could not kmalloc() req\n");
  741. bio_endio(bio, -ENOMEM);
  742. return 0;
  743. }
  744. req->start_time = start_time;
  745. local = get_ldev(mdev);
  746. if (!local) {
  747. bio_put(req->private_bio); /* or we get a bio leak */
  748. req->private_bio = NULL;
  749. }
  750. if (rw == WRITE) {
  751. remote = 1;
  752. } else {
  753. /* READ || READA */
  754. if (local) {
  755. if (!drbd_may_do_local_read(mdev, sector, size) ||
  756. remote_due_to_read_balancing(mdev, sector)) {
  757. /* we could kick the syncer to
  758. * sync this extent asap, wait for
  759. * it, then continue locally.
  760. * Or just issue the request remotely.
  761. */
  762. local = 0;
  763. bio_put(req->private_bio);
  764. req->private_bio = NULL;
  765. put_ldev(mdev);
  766. }
  767. }
  768. remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
  769. }
  770. /* If we have a disk, but a READA request is mapped to remote,
  771. * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
  772. * Just fail that READA request right here.
  773. *
  774. * THINK: maybe fail all READA when not local?
  775. * or make this configurable...
  776. * if network is slow, READA won't do any good.
  777. */
  778. if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
  779. err = -EWOULDBLOCK;
  780. goto fail_and_free_req;
  781. }
  782. /* For WRITES going to the local disk, grab a reference on the target
  783. * extent. This waits for any resync activity in the corresponding
  784. * resync extent to finish, and, if necessary, pulls in the target
  785. * extent into the activity log, which involves further disk io because
  786. * of transactional on-disk meta data updates. */
  787. if (rw == WRITE && local && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  788. req->rq_state |= RQ_IN_ACT_LOG;
  789. drbd_al_begin_io(mdev, &req->i);
  790. }
  791. s = mdev->state;
  792. remote = remote && drbd_should_do_remote(s);
  793. send_oos = rw == WRITE && drbd_should_send_out_of_sync(s);
  794. D_ASSERT(!(remote && send_oos));
  795. if (!(local || remote) && !drbd_suspended(mdev)) {
  796. if (__ratelimit(&drbd_ratelimit_state))
  797. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  798. err = -EIO;
  799. goto fail_free_complete;
  800. }
  801. /* For WRITE request, we have to make sure that we have an
  802. * unused_spare_tle, in case we need to start a new epoch.
  803. * I try to be smart and avoid to pre-allocate always "just in case",
  804. * but there is a race between testing the bit and pointer outside the
  805. * spinlock, and grabbing the spinlock.
  806. * if we lost that race, we retry. */
  807. if (rw == WRITE && (remote || send_oos) &&
  808. mdev->tconn->unused_spare_tle == NULL &&
  809. test_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
  810. allocate_barrier:
  811. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
  812. if (!b) {
  813. dev_err(DEV, "Failed to alloc barrier.\n");
  814. err = -ENOMEM;
  815. goto fail_free_complete;
  816. }
  817. }
  818. /* GOOD, everything prepared, grab the spin_lock */
  819. spin_lock_irq(&mdev->tconn->req_lock);
  820. if (rw == WRITE) {
  821. err = complete_conflicting_writes(mdev, sector, size);
  822. if (err) {
  823. if (err != -ERESTARTSYS)
  824. _conn_request_state(mdev->tconn,
  825. NS(conn, C_TIMEOUT),
  826. CS_HARD);
  827. spin_unlock_irq(&mdev->tconn->req_lock);
  828. err = -EIO;
  829. goto fail_free_complete;
  830. }
  831. }
  832. if (drbd_suspended(mdev)) {
  833. /* If we got suspended, use the retry mechanism in
  834. drbd_make_request() to restart processing of this
  835. bio. In the next call to drbd_make_request
  836. we sleep in inc_ap_bio() */
  837. ret = 1;
  838. spin_unlock_irq(&mdev->tconn->req_lock);
  839. goto fail_free_complete;
  840. }
  841. if (remote || send_oos) {
  842. remote = drbd_should_do_remote(mdev->state);
  843. send_oos = rw == WRITE && drbd_should_send_out_of_sync(mdev->state);
  844. D_ASSERT(!(remote && send_oos));
  845. if (!(remote || send_oos))
  846. dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
  847. if (!(local || remote)) {
  848. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  849. spin_unlock_irq(&mdev->tconn->req_lock);
  850. err = -EIO;
  851. goto fail_free_complete;
  852. }
  853. }
  854. if (b && mdev->tconn->unused_spare_tle == NULL) {
  855. mdev->tconn->unused_spare_tle = b;
  856. b = NULL;
  857. }
  858. if (rw == WRITE && (remote || send_oos) &&
  859. mdev->tconn->unused_spare_tle == NULL &&
  860. test_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
  861. /* someone closed the current epoch
  862. * while we were grabbing the spinlock */
  863. spin_unlock_irq(&mdev->tconn->req_lock);
  864. goto allocate_barrier;
  865. }
  866. /* Update disk stats */
  867. _drbd_start_io_acct(mdev, req, bio);
  868. /* _maybe_start_new_epoch(mdev);
  869. * If we need to generate a write barrier packet, we have to add the
  870. * new epoch (barrier) object, and queue the barrier packet for sending,
  871. * and queue the req's data after it _within the same lock_, otherwise
  872. * we have race conditions were the reorder domains could be mixed up.
  873. *
  874. * Even read requests may start a new epoch and queue the corresponding
  875. * barrier packet. To get the write ordering right, we only have to
  876. * make sure that, if this is a write request and it triggered a
  877. * barrier packet, this request is queued within the same spinlock. */
  878. if ((remote || send_oos) && mdev->tconn->unused_spare_tle &&
  879. test_and_clear_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
  880. _tl_add_barrier(mdev->tconn, mdev->tconn->unused_spare_tle);
  881. mdev->tconn->unused_spare_tle = NULL;
  882. } else {
  883. D_ASSERT(!(remote && rw == WRITE &&
  884. test_bit(CREATE_BARRIER, &mdev->tconn->flags)));
  885. }
  886. /* NOTE
  887. * Actually, 'local' may be wrong here already, since we may have failed
  888. * to write to the meta data, and may become wrong anytime because of
  889. * local io-error for some other request, which would lead to us
  890. * "detaching" the local disk.
  891. *
  892. * 'remote' may become wrong any time because the network could fail.
  893. *
  894. * This is a harmless race condition, though, since it is handled
  895. * correctly at the appropriate places; so it just defers the failure
  896. * of the respective operation.
  897. */
  898. /* mark them early for readability.
  899. * this just sets some state flags. */
  900. if (remote)
  901. _req_mod(req, TO_BE_SENT);
  902. if (local)
  903. _req_mod(req, TO_BE_SUBMITTED);
  904. list_add_tail(&req->tl_requests, &mdev->tconn->newest_tle->requests);
  905. /* NOTE remote first: to get the concurrent write detection right,
  906. * we must register the request before start of local IO. */
  907. if (remote) {
  908. /* either WRITE and C_CONNECTED,
  909. * or READ, and no local disk,
  910. * or READ, but not in sync.
  911. */
  912. _req_mod(req, (rw == WRITE)
  913. ? QUEUE_FOR_NET_WRITE
  914. : QUEUE_FOR_NET_READ);
  915. }
  916. if (send_oos && drbd_set_out_of_sync(mdev, sector, size))
  917. _req_mod(req, QUEUE_FOR_SEND_OOS);
  918. rcu_read_lock();
  919. nc = rcu_dereference(mdev->tconn->net_conf);
  920. if (remote &&
  921. nc->on_congestion != OC_BLOCK && mdev->tconn->agreed_pro_version >= 96) {
  922. int congested = 0;
  923. if (nc->cong_fill &&
  924. atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
  925. dev_info(DEV, "Congestion-fill threshold reached\n");
  926. congested = 1;
  927. }
  928. if (mdev->act_log->used >= nc->cong_extents) {
  929. dev_info(DEV, "Congestion-extents threshold reached\n");
  930. congested = 1;
  931. }
  932. if (congested) {
  933. queue_barrier(mdev); /* last barrier, after mirrored writes */
  934. if (nc->on_congestion == OC_PULL_AHEAD)
  935. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  936. else /*nc->on_congestion == OC_DISCONNECT */
  937. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  938. }
  939. }
  940. rcu_read_unlock();
  941. spin_unlock_irq(&mdev->tconn->req_lock);
  942. kfree(b); /* if someone else has beaten us to it... */
  943. if (local) {
  944. req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
  945. /* State may have changed since we grabbed our reference on the
  946. * mdev->ldev member. Double check, and short-circuit to endio.
  947. * In case the last activity log transaction failed to get on
  948. * stable storage, and this is a WRITE, we may not even submit
  949. * this bio. */
  950. if (get_ldev(mdev)) {
  951. if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
  952. : rw == READ ? DRBD_FAULT_DT_RD
  953. : DRBD_FAULT_DT_RA))
  954. bio_endio(req->private_bio, -EIO);
  955. else
  956. generic_make_request(req->private_bio);
  957. put_ldev(mdev);
  958. } else
  959. bio_endio(req->private_bio, -EIO);
  960. }
  961. return 0;
  962. fail_free_complete:
  963. if (req->rq_state & RQ_IN_ACT_LOG)
  964. drbd_al_complete_io(mdev, &req->i);
  965. fail_and_free_req:
  966. if (local) {
  967. bio_put(req->private_bio);
  968. req->private_bio = NULL;
  969. put_ldev(mdev);
  970. }
  971. if (!ret)
  972. bio_endio(bio, err);
  973. drbd_req_free(req);
  974. dec_ap_bio(mdev);
  975. kfree(b);
  976. return ret;
  977. }
  978. int drbd_make_request(struct request_queue *q, struct bio *bio)
  979. {
  980. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  981. unsigned long start_time;
  982. start_time = jiffies;
  983. /*
  984. * what we "blindly" assume:
  985. */
  986. D_ASSERT(bio->bi_size > 0);
  987. D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
  988. do {
  989. inc_ap_bio(mdev);
  990. } while (__drbd_make_request(mdev, bio, start_time));
  991. return 0;
  992. }
  993. /* This is called by bio_add_page().
  994. *
  995. * q->max_hw_sectors and other global limits are already enforced there.
  996. *
  997. * We need to call down to our lower level device,
  998. * in case it has special restrictions.
  999. *
  1000. * We also may need to enforce configured max-bio-bvecs limits.
  1001. *
  1002. * As long as the BIO is empty we have to allow at least one bvec,
  1003. * regardless of size and offset, so no need to ask lower levels.
  1004. */
  1005. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  1006. {
  1007. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  1008. unsigned int bio_size = bvm->bi_size;
  1009. int limit = DRBD_MAX_BIO_SIZE;
  1010. int backing_limit;
  1011. if (bio_size && get_ldev(mdev)) {
  1012. struct request_queue * const b =
  1013. mdev->ldev->backing_bdev->bd_disk->queue;
  1014. if (b->merge_bvec_fn) {
  1015. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  1016. limit = min(limit, backing_limit);
  1017. }
  1018. put_ldev(mdev);
  1019. }
  1020. return limit;
  1021. }
  1022. void request_timer_fn(unsigned long data)
  1023. {
  1024. struct drbd_conf *mdev = (struct drbd_conf *) data;
  1025. struct drbd_tconn *tconn = mdev->tconn;
  1026. struct drbd_request *req; /* oldest request */
  1027. struct list_head *le;
  1028. struct net_conf *nc;
  1029. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  1030. rcu_read_lock();
  1031. nc = rcu_dereference(tconn->net_conf);
  1032. ent = nc ? nc->timeout * HZ/10 * nc->ko_count : 0;
  1033. if (get_ldev(mdev)) {
  1034. dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
  1035. put_ldev(mdev);
  1036. }
  1037. rcu_read_unlock();
  1038. et = min_not_zero(dt, ent);
  1039. if (!et || (mdev->state.conn < C_WF_REPORT_PARAMS && mdev->state.disk <= D_FAILED))
  1040. return; /* Recurring timer stopped */
  1041. spin_lock_irq(&tconn->req_lock);
  1042. le = &tconn->oldest_tle->requests;
  1043. if (list_empty(le)) {
  1044. spin_unlock_irq(&tconn->req_lock);
  1045. mod_timer(&mdev->request_timer, jiffies + et);
  1046. return;
  1047. }
  1048. le = le->prev;
  1049. req = list_entry(le, struct drbd_request, tl_requests);
  1050. if (ent && req->rq_state & RQ_NET_PENDING) {
  1051. if (time_is_before_eq_jiffies(req->start_time + ent)) {
  1052. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  1053. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
  1054. }
  1055. }
  1056. if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev) {
  1057. if (time_is_before_eq_jiffies(req->start_time + dt)) {
  1058. dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
  1059. __drbd_chk_io_error(mdev, 1);
  1060. }
  1061. }
  1062. nt = (time_is_before_eq_jiffies(req->start_time + et) ? jiffies : req->start_time) + et;
  1063. spin_unlock_irq(&tconn->req_lock);
  1064. mod_timer(&mdev->request_timer, nt);
  1065. }