drbd_req.c 41 KB

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