drbd_req.c 38 KB

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