drbd_req.c 37 KB

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