drbd_req.c 37 KB

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