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