drbd_req.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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 (drbd_test_flag(mdev, CREATE_BARRIER))
  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. drbd_set_flag(mdev, CREATE_BARRIER);
  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. static void drbd_report_io_error(struct drbd_conf *mdev, struct drbd_request *req)
  344. {
  345. char b[BDEVNAME_SIZE];
  346. if (__ratelimit(&drbd_ratelimit_state))
  347. return;
  348. dev_warn(DEV, "local %s IO error sector %llu+%u on %s\n",
  349. (req->rq_state & RQ_WRITE) ? "WRITE" : "READ",
  350. (unsigned long long)req->sector,
  351. req->size >> 9,
  352. bdevname(mdev->ldev->backing_bdev, b));
  353. }
  354. /* obviously this could be coded as many single functions
  355. * instead of one huge switch,
  356. * or by putting the code directly in the respective locations
  357. * (as it has been before).
  358. *
  359. * but having it this way
  360. * enforces that it is all in this one place, where it is easier to audit,
  361. * it makes it obvious that whatever "event" "happens" to a request should
  362. * happen "atomically" within the req_lock,
  363. * and it enforces that we have to think in a very structured manner
  364. * about the "events" that may happen to a request during its life time ...
  365. */
  366. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  367. struct bio_and_error *m)
  368. {
  369. struct drbd_conf *mdev = req->mdev;
  370. int rv = 0;
  371. m->bio = NULL;
  372. switch (what) {
  373. default:
  374. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  375. break;
  376. /* does not happen...
  377. * initialization done in drbd_req_new
  378. case created:
  379. break;
  380. */
  381. case to_be_send: /* via network */
  382. /* reached via drbd_make_request_common
  383. * and from w_read_retry_remote */
  384. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  385. req->rq_state |= RQ_NET_PENDING;
  386. inc_ap_pending(mdev);
  387. break;
  388. case to_be_submitted: /* locally */
  389. /* reached via drbd_make_request_common */
  390. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  391. req->rq_state |= RQ_LOCAL_PENDING;
  392. break;
  393. case completed_ok:
  394. if (req->rq_state & RQ_WRITE)
  395. mdev->writ_cnt += req->size>>9;
  396. else
  397. mdev->read_cnt += req->size>>9;
  398. req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  399. req->rq_state &= ~RQ_LOCAL_PENDING;
  400. _req_may_be_done_not_susp(req, m);
  401. break;
  402. case abort_disk_io:
  403. req->rq_state |= RQ_LOCAL_ABORTED;
  404. if (req->rq_state & RQ_WRITE)
  405. _req_may_be_done_not_susp(req, m);
  406. else
  407. goto goto_queue_for_net_read;
  408. break;
  409. case write_completed_with_error:
  410. req->rq_state |= RQ_LOCAL_COMPLETED;
  411. req->rq_state &= ~RQ_LOCAL_PENDING;
  412. drbd_report_io_error(mdev, req);
  413. __drbd_chk_io_error(mdev, DRBD_WRITE_ERROR);
  414. _req_may_be_done_not_susp(req, m);
  415. break;
  416. case read_ahead_completed_with_error:
  417. /* it is legal to fail READA */
  418. req->rq_state |= RQ_LOCAL_COMPLETED;
  419. req->rq_state &= ~RQ_LOCAL_PENDING;
  420. _req_may_be_done_not_susp(req, m);
  421. break;
  422. case read_completed_with_error:
  423. drbd_set_out_of_sync(mdev, req->sector, req->size);
  424. req->rq_state |= RQ_LOCAL_COMPLETED;
  425. req->rq_state &= ~RQ_LOCAL_PENDING;
  426. if (req->rq_state & RQ_LOCAL_ABORTED) {
  427. _req_may_be_done(req, m);
  428. break;
  429. }
  430. drbd_report_io_error(mdev, req);
  431. __drbd_chk_io_error(mdev, DRBD_READ_ERROR);
  432. goto_queue_for_net_read:
  433. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  434. /* no point in retrying if there is no good remote data,
  435. * or we have no connection. */
  436. if (mdev->state.pdsk != D_UP_TO_DATE) {
  437. _req_may_be_done_not_susp(req, m);
  438. break;
  439. }
  440. /* _req_mod(req,to_be_send); oops, recursion... */
  441. req->rq_state |= RQ_NET_PENDING;
  442. inc_ap_pending(mdev);
  443. /* fall through: _req_mod(req,queue_for_net_read); */
  444. case queue_for_net_read:
  445. /* READ or READA, and
  446. * no local disk,
  447. * or target area marked as invalid,
  448. * or just got an io-error. */
  449. /* from drbd_make_request_common
  450. * or from bio_endio during read io-error recovery */
  451. /* so we can verify the handle in the answer packet
  452. * corresponding hlist_del is in _req_may_be_done() */
  453. hlist_add_head(&req->collision, ar_hash_slot(mdev, req->sector));
  454. drbd_set_flag(mdev, UNPLUG_REMOTE);
  455. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  456. req->rq_state |= RQ_NET_QUEUED;
  457. req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
  458. ? w_read_retry_remote
  459. : w_send_read_req;
  460. drbd_queue_work(&mdev->data.work, &req->w);
  461. break;
  462. case queue_for_net_write:
  463. /* assert something? */
  464. /* from drbd_make_request_common only */
  465. hlist_add_head(&req->collision, tl_hash_slot(mdev, req->sector));
  466. /* corresponding hlist_del is in _req_may_be_done() */
  467. /* NOTE
  468. * In case the req ended up on the transfer log before being
  469. * queued on the worker, it could lead to this request being
  470. * missed during cleanup after connection loss.
  471. * So we have to do both operations here,
  472. * within the same lock that protects the transfer log.
  473. *
  474. * _req_add_to_epoch(req); this has to be after the
  475. * _maybe_start_new_epoch(req); which happened in
  476. * drbd_make_request_common, because we now may set the bit
  477. * again ourselves to close the current epoch.
  478. *
  479. * Add req to the (now) current epoch (barrier). */
  480. /* otherwise we may lose an unplug, which may cause some remote
  481. * io-scheduler timeout to expire, increasing maximum latency,
  482. * hurting performance. */
  483. drbd_set_flag(mdev, UNPLUG_REMOTE);
  484. /* see drbd_make_request_common,
  485. * just after it grabs the req_lock */
  486. D_ASSERT(drbd_test_flag(mdev, CREATE_BARRIER) == 0);
  487. req->epoch = mdev->newest_tle->br_number;
  488. /* increment size of current epoch */
  489. mdev->newest_tle->n_writes++;
  490. /* queue work item to send data */
  491. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  492. req->rq_state |= RQ_NET_QUEUED;
  493. req->w.cb = w_send_dblock;
  494. drbd_queue_work(&mdev->data.work, &req->w);
  495. /* close the epoch, in case it outgrew the limit */
  496. if (mdev->newest_tle->n_writes >= mdev->net_conf->max_epoch_size)
  497. queue_barrier(mdev);
  498. break;
  499. case queue_for_send_oos:
  500. req->rq_state |= RQ_NET_QUEUED;
  501. req->w.cb = w_send_oos;
  502. drbd_queue_work(&mdev->data.work, &req->w);
  503. break;
  504. case read_retry_remote_canceled:
  505. case send_canceled:
  506. case send_failed:
  507. /* real cleanup will be done from tl_clear. just update flags
  508. * so it is no longer marked as on the worker queue */
  509. req->rq_state &= ~RQ_NET_QUEUED;
  510. /* if we did it right, tl_clear should be scheduled only after
  511. * this, so this should not be necessary! */
  512. _req_may_be_done_not_susp(req, m);
  513. break;
  514. case handed_over_to_network:
  515. /* assert something? */
  516. if (bio_data_dir(req->master_bio) == WRITE)
  517. atomic_add(req->size>>9, &mdev->ap_in_flight);
  518. if (bio_data_dir(req->master_bio) == WRITE &&
  519. mdev->net_conf->wire_protocol == DRBD_PROT_A) {
  520. /* this is what is dangerous about protocol A:
  521. * pretend it was successfully written on the peer. */
  522. if (req->rq_state & RQ_NET_PENDING) {
  523. dec_ap_pending(mdev);
  524. req->rq_state &= ~RQ_NET_PENDING;
  525. req->rq_state |= RQ_NET_OK;
  526. } /* else: neg-ack was faster... */
  527. /* it is still not yet RQ_NET_DONE until the
  528. * corresponding epoch barrier got acked as well,
  529. * so we know what to dirty on connection loss */
  530. }
  531. req->rq_state &= ~RQ_NET_QUEUED;
  532. req->rq_state |= RQ_NET_SENT;
  533. _req_may_be_done_not_susp(req, m);
  534. break;
  535. case oos_handed_to_network:
  536. /* Was not set PENDING, no longer QUEUED, so is now DONE
  537. * as far as this connection is concerned. */
  538. req->rq_state &= ~RQ_NET_QUEUED;
  539. req->rq_state |= RQ_NET_DONE;
  540. _req_may_be_done_not_susp(req, m);
  541. break;
  542. case connection_lost_while_pending:
  543. /* transfer log cleanup after connection loss */
  544. /* assert something? */
  545. if (req->rq_state & RQ_NET_PENDING)
  546. dec_ap_pending(mdev);
  547. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  548. req->rq_state |= RQ_NET_DONE;
  549. if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE)
  550. atomic_sub(req->size>>9, &mdev->ap_in_flight);
  551. /* if it is still queued, we may not complete it here.
  552. * it will be canceled soon. */
  553. if (!(req->rq_state & RQ_NET_QUEUED))
  554. _req_may_be_done(req, m); /* Allowed while state.susp */
  555. break;
  556. case conflict_discarded_by_peer:
  557. /* for discarded conflicting writes of multiple primaries,
  558. * there is no need to keep anything in the tl, potential
  559. * node crashes are covered by the activity log. */
  560. if (what == conflict_discarded_by_peer)
  561. dev_alert(DEV, "Got DiscardAck packet %llus +%u!"
  562. " DRBD is not a random data generator!\n",
  563. (unsigned long long)req->sector, req->size);
  564. req->rq_state |= RQ_NET_DONE;
  565. /* fall through */
  566. case write_acked_by_peer_and_sis:
  567. case write_acked_by_peer:
  568. if (what == write_acked_by_peer_and_sis)
  569. req->rq_state |= RQ_NET_SIS;
  570. /* protocol C; successfully written on peer.
  571. * Nothing more to do here.
  572. * We want to keep the tl in place for all protocols, to cater
  573. * for volatile write-back caches on lower level devices. */
  574. case recv_acked_by_peer:
  575. /* protocol B; pretends to be successfully written on peer.
  576. * see also notes above in handed_over_to_network about
  577. * protocol != C */
  578. req->rq_state |= RQ_NET_OK;
  579. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  580. dec_ap_pending(mdev);
  581. atomic_sub(req->size>>9, &mdev->ap_in_flight);
  582. req->rq_state &= ~RQ_NET_PENDING;
  583. _req_may_be_done_not_susp(req, m);
  584. break;
  585. case neg_acked:
  586. /* assert something? */
  587. if (req->rq_state & RQ_NET_PENDING) {
  588. dec_ap_pending(mdev);
  589. atomic_sub(req->size>>9, &mdev->ap_in_flight);
  590. }
  591. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  592. req->rq_state |= RQ_NET_DONE;
  593. _req_may_be_done_not_susp(req, m);
  594. /* else: done by handed_over_to_network */
  595. break;
  596. case fail_frozen_disk_io:
  597. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  598. break;
  599. _req_may_be_done(req, m); /* Allowed while state.susp */
  600. break;
  601. case restart_frozen_disk_io:
  602. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  603. break;
  604. req->rq_state &= ~RQ_LOCAL_COMPLETED;
  605. rv = MR_READ;
  606. if (bio_data_dir(req->master_bio) == WRITE)
  607. rv = MR_WRITE;
  608. get_ldev(mdev);
  609. req->w.cb = w_restart_disk_io;
  610. drbd_queue_work(&mdev->data.work, &req->w);
  611. break;
  612. case resend:
  613. /* Simply complete (local only) READs. */
  614. if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
  615. _req_may_be_done(req, m);
  616. break;
  617. }
  618. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  619. before the connection loss (B&C only); only P_BARRIER_ACK was missing.
  620. Trowing them out of the TL here by pretending we got a BARRIER_ACK
  621. We ensure that the peer was not rebooted */
  622. if (!(req->rq_state & RQ_NET_OK)) {
  623. if (req->w.cb) {
  624. drbd_queue_work(&mdev->data.work, &req->w);
  625. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  626. }
  627. break;
  628. }
  629. /* else, fall through to barrier_acked */
  630. case barrier_acked:
  631. if (!(req->rq_state & RQ_WRITE))
  632. break;
  633. if (req->rq_state & RQ_NET_PENDING) {
  634. /* barrier came in before all requests have been acked.
  635. * this is bad, because if the connection is lost now,
  636. * we won't be able to clean them up... */
  637. dev_err(DEV, "FIXME (barrier_acked but pending)\n");
  638. list_move(&req->tl_requests, &mdev->out_of_sequence_requests);
  639. }
  640. if ((req->rq_state & RQ_NET_MASK) != 0) {
  641. req->rq_state |= RQ_NET_DONE;
  642. if (mdev->net_conf->wire_protocol == DRBD_PROT_A)
  643. atomic_sub(req->size>>9, &mdev->ap_in_flight);
  644. }
  645. _req_may_be_done(req, m); /* Allowed while state.susp */
  646. break;
  647. case data_received:
  648. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  649. dec_ap_pending(mdev);
  650. req->rq_state &= ~RQ_NET_PENDING;
  651. req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
  652. _req_may_be_done_not_susp(req, m);
  653. break;
  654. };
  655. return rv;
  656. }
  657. /* we may do a local read if:
  658. * - we are consistent (of course),
  659. * - or we are generally inconsistent,
  660. * BUT we are still/already IN SYNC for this area.
  661. * since size may be bigger than BM_BLOCK_SIZE,
  662. * we may need to check several bits.
  663. */
  664. static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  665. {
  666. unsigned long sbnr, ebnr;
  667. sector_t esector, nr_sectors;
  668. if (mdev->state.disk == D_UP_TO_DATE)
  669. return 1;
  670. if (mdev->state.disk >= D_OUTDATED)
  671. return 0;
  672. if (mdev->state.disk < D_INCONSISTENT)
  673. return 0;
  674. /* state.disk == D_INCONSISTENT We will have a look at the BitMap */
  675. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  676. esector = sector + (size >> 9) - 1;
  677. D_ASSERT(sector < nr_sectors);
  678. D_ASSERT(esector < nr_sectors);
  679. sbnr = BM_SECT_TO_BIT(sector);
  680. ebnr = BM_SECT_TO_BIT(esector);
  681. return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr);
  682. }
  683. static void maybe_pull_ahead(struct drbd_conf *mdev)
  684. {
  685. int congested = 0;
  686. /* If I don't even have good local storage, we can not reasonably try
  687. * to pull ahead of the peer. We also need the local reference to make
  688. * sure mdev->act_log is there.
  689. * Note: caller has to make sure that net_conf is there.
  690. */
  691. if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
  692. return;
  693. if (mdev->net_conf->cong_fill &&
  694. atomic_read(&mdev->ap_in_flight) >= mdev->net_conf->cong_fill) {
  695. dev_info(DEV, "Congestion-fill threshold reached\n");
  696. congested = 1;
  697. }
  698. if (mdev->act_log->used >= mdev->net_conf->cong_extents) {
  699. dev_info(DEV, "Congestion-extents threshold reached\n");
  700. congested = 1;
  701. }
  702. if (congested) {
  703. queue_barrier(mdev); /* last barrier, after mirrored writes */
  704. if (mdev->net_conf->on_congestion == OC_PULL_AHEAD)
  705. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  706. else /*mdev->net_conf->on_congestion == OC_DISCONNECT */
  707. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  708. }
  709. put_ldev(mdev);
  710. }
  711. static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  712. {
  713. const int rw = bio_rw(bio);
  714. const int size = bio->bi_size;
  715. const sector_t sector = bio->bi_sector;
  716. struct drbd_tl_epoch *b = NULL;
  717. struct drbd_request *req;
  718. int local, remote, send_oos = 0;
  719. int err = -EIO;
  720. int ret = 0;
  721. union drbd_state s;
  722. /* allocate outside of all locks; */
  723. req = drbd_req_new(mdev, bio);
  724. if (!req) {
  725. dec_ap_bio(mdev);
  726. /* only pass the error to the upper layers.
  727. * if user cannot handle io errors, that's not our business. */
  728. dev_err(DEV, "could not kmalloc() req\n");
  729. bio_endio(bio, -ENOMEM);
  730. return 0;
  731. }
  732. req->start_time = start_time;
  733. local = get_ldev(mdev);
  734. if (!local) {
  735. bio_put(req->private_bio); /* or we get a bio leak */
  736. req->private_bio = NULL;
  737. }
  738. if (rw == WRITE) {
  739. /* Need to replicate writes. Unless it is an empty flush,
  740. * which is better mapped to a DRBD P_BARRIER packet,
  741. * also for drbd wire protocol compatibility reasons. */
  742. if (unlikely(size == 0)) {
  743. /* The only size==0 bios we expect are empty flushes. */
  744. D_ASSERT(bio->bi_rw & REQ_FLUSH);
  745. remote = 0;
  746. } else
  747. remote = 1;
  748. } else {
  749. /* READ || READA */
  750. if (local) {
  751. if (!drbd_may_do_local_read(mdev, sector, size)) {
  752. /* we could kick the syncer to
  753. * sync this extent asap, wait for
  754. * it, then continue locally.
  755. * Or just issue the request remotely.
  756. */
  757. local = 0;
  758. bio_put(req->private_bio);
  759. req->private_bio = NULL;
  760. put_ldev(mdev);
  761. }
  762. }
  763. remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
  764. }
  765. /* If we have a disk, but a READA request is mapped to remote,
  766. * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
  767. * Just fail that READA request right here.
  768. *
  769. * THINK: maybe fail all READA when not local?
  770. * or make this configurable...
  771. * if network is slow, READA won't do any good.
  772. */
  773. if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
  774. err = -EWOULDBLOCK;
  775. goto fail_and_free_req;
  776. }
  777. /* For WRITES going to the local disk, grab a reference on the target
  778. * extent. This waits for any resync activity in the corresponding
  779. * resync extent to finish, and, if necessary, pulls in the target
  780. * extent into the activity log, which involves further disk io because
  781. * of transactional on-disk meta data updates.
  782. * Empty flushes don't need to go into the activity log, they can only
  783. * flush data for pending writes which are already in there. */
  784. if (rw == WRITE && local && size
  785. && !drbd_test_flag(mdev, AL_SUSPENDED)) {
  786. req->rq_state |= RQ_IN_ACT_LOG;
  787. drbd_al_begin_io(mdev, sector);
  788. }
  789. s = mdev->state;
  790. remote = remote && drbd_should_do_remote(s);
  791. send_oos = rw == WRITE && drbd_should_send_oos(s);
  792. D_ASSERT(!(remote && send_oos));
  793. if (!(local || remote) && !is_susp(mdev->state)) {
  794. if (__ratelimit(&drbd_ratelimit_state))
  795. dev_err(DEV, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
  796. (unsigned long long)req->sector, req->size >> 9);
  797. goto fail_free_complete;
  798. }
  799. /* For WRITE request, we have to make sure that we have an
  800. * unused_spare_tle, in case we need to start a new epoch.
  801. * I try to be smart and avoid to pre-allocate always "just in case",
  802. * but there is a race between testing the bit and pointer outside the
  803. * spinlock, and grabbing the spinlock.
  804. * if we lost that race, we retry. */
  805. if (rw == WRITE && (remote || send_oos) &&
  806. mdev->unused_spare_tle == NULL &&
  807. drbd_test_flag(mdev, CREATE_BARRIER)) {
  808. allocate_barrier:
  809. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
  810. if (!b) {
  811. dev_err(DEV, "Failed to alloc barrier.\n");
  812. err = -ENOMEM;
  813. goto fail_free_complete;
  814. }
  815. }
  816. /* GOOD, everything prepared, grab the spin_lock */
  817. spin_lock_irq(&mdev->req_lock);
  818. if (is_susp(mdev->state)) {
  819. /* If we got suspended, use the retry mechanism of
  820. drbd_make_request() to restart processing of this
  821. bio. In the next call to drbd_make_request
  822. we sleep in inc_ap_bio() */
  823. ret = 1;
  824. spin_unlock_irq(&mdev->req_lock);
  825. goto fail_free_complete;
  826. }
  827. if (remote || send_oos) {
  828. remote = drbd_should_do_remote(mdev->state);
  829. send_oos = rw == WRITE && drbd_should_send_oos(mdev->state);
  830. D_ASSERT(!(remote && send_oos));
  831. if (!(remote || send_oos))
  832. dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
  833. if (!(local || remote)) {
  834. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  835. spin_unlock_irq(&mdev->req_lock);
  836. goto fail_free_complete;
  837. }
  838. }
  839. if (b && mdev->unused_spare_tle == NULL) {
  840. mdev->unused_spare_tle = b;
  841. b = NULL;
  842. }
  843. if (rw == WRITE && (remote || send_oos) &&
  844. mdev->unused_spare_tle == NULL &&
  845. drbd_test_flag(mdev, CREATE_BARRIER)) {
  846. /* someone closed the current epoch
  847. * while we were grabbing the spinlock */
  848. spin_unlock_irq(&mdev->req_lock);
  849. goto allocate_barrier;
  850. }
  851. /* Update disk stats */
  852. _drbd_start_io_acct(mdev, req, bio);
  853. /* _maybe_start_new_epoch(mdev);
  854. * If we need to generate a write barrier packet, we have to add the
  855. * new epoch (barrier) object, and queue the barrier packet for sending,
  856. * and queue the req's data after it _within the same lock_, otherwise
  857. * we have race conditions were the reorder domains could be mixed up.
  858. *
  859. * Even read requests may start a new epoch and queue the corresponding
  860. * barrier packet. To get the write ordering right, we only have to
  861. * make sure that, if this is a write request and it triggered a
  862. * barrier packet, this request is queued within the same spinlock. */
  863. if ((remote || send_oos) && mdev->unused_spare_tle &&
  864. drbd_test_and_clear_flag(mdev, CREATE_BARRIER)) {
  865. _tl_add_barrier(mdev, mdev->unused_spare_tle);
  866. mdev->unused_spare_tle = NULL;
  867. } else {
  868. D_ASSERT(!(remote && rw == WRITE &&
  869. drbd_test_flag(mdev, CREATE_BARRIER)));
  870. }
  871. /* NOTE
  872. * Actually, 'local' may be wrong here already, since we may have failed
  873. * to write to the meta data, and may become wrong anytime because of
  874. * local io-error for some other request, which would lead to us
  875. * "detaching" the local disk.
  876. *
  877. * 'remote' may become wrong any time because the network could fail.
  878. *
  879. * This is a harmless race condition, though, since it is handled
  880. * correctly at the appropriate places; so it just defers the failure
  881. * of the respective operation.
  882. */
  883. /* mark them early for readability.
  884. * this just sets some state flags. */
  885. if (remote)
  886. _req_mod(req, to_be_send);
  887. if (local)
  888. _req_mod(req, to_be_submitted);
  889. /* check this request on the collision detection hash tables.
  890. * if we have a conflict, just complete it here.
  891. * THINK do we want to check reads, too? (I don't think so...) */
  892. if (rw == WRITE && _req_conflicts(req))
  893. goto fail_conflicting;
  894. /* no point in adding empty flushes to the transfer log,
  895. * they are mapped to drbd barriers already. */
  896. if (likely(size!=0))
  897. list_add_tail(&req->tl_requests, &mdev->newest_tle->requests);
  898. /* NOTE remote first: to get the concurrent write detection right,
  899. * we must register the request before start of local IO. */
  900. if (remote) {
  901. /* either WRITE and C_CONNECTED,
  902. * or READ, and no local disk,
  903. * or READ, but not in sync.
  904. */
  905. _req_mod(req, (rw == WRITE)
  906. ? queue_for_net_write
  907. : queue_for_net_read);
  908. }
  909. if (send_oos && drbd_set_out_of_sync(mdev, sector, size))
  910. _req_mod(req, queue_for_send_oos);
  911. if (remote &&
  912. mdev->net_conf->on_congestion != OC_BLOCK && mdev->agreed_pro_version >= 96)
  913. maybe_pull_ahead(mdev);
  914. /* If this was a flush, queue a drbd barrier/start a new epoch.
  915. * Unless the current epoch was empty anyways, or we are not currently
  916. * replicating, in which case there is no point. */
  917. if (unlikely(bio->bi_rw & REQ_FLUSH)
  918. && mdev->newest_tle->n_writes
  919. && drbd_should_do_remote(mdev->state))
  920. queue_barrier(mdev);
  921. spin_unlock_irq(&mdev->req_lock);
  922. kfree(b); /* if someone else has beaten us to it... */
  923. if (local) {
  924. req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
  925. /* State may have changed since we grabbed our reference on the
  926. * mdev->ldev member. Double check, and short-circuit to endio.
  927. * In case the last activity log transaction failed to get on
  928. * stable storage, and this is a WRITE, we may not even submit
  929. * this bio. */
  930. if (get_ldev(mdev)) {
  931. if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
  932. : rw == READ ? DRBD_FAULT_DT_RD
  933. : DRBD_FAULT_DT_RA))
  934. bio_endio(req->private_bio, -EIO);
  935. else
  936. generic_make_request(req->private_bio);
  937. put_ldev(mdev);
  938. } else
  939. bio_endio(req->private_bio, -EIO);
  940. }
  941. return 0;
  942. fail_conflicting:
  943. /* this is a conflicting request.
  944. * even though it may have been only _partially_
  945. * overlapping with one of the currently pending requests,
  946. * without even submitting or sending it, we will
  947. * pretend that it was successfully served right now.
  948. */
  949. _drbd_end_io_acct(mdev, req);
  950. spin_unlock_irq(&mdev->req_lock);
  951. if (remote)
  952. dec_ap_pending(mdev);
  953. /* THINK: do we want to fail it (-EIO), or pretend success?
  954. * this pretends success. */
  955. err = 0;
  956. fail_free_complete:
  957. if (req->rq_state & RQ_IN_ACT_LOG)
  958. drbd_al_complete_io(mdev, sector);
  959. fail_and_free_req:
  960. if (local) {
  961. bio_put(req->private_bio);
  962. req->private_bio = NULL;
  963. put_ldev(mdev);
  964. }
  965. if (!ret)
  966. bio_endio(bio, err);
  967. drbd_req_free(req);
  968. dec_ap_bio(mdev);
  969. kfree(b);
  970. return ret;
  971. }
  972. /* helper function for drbd_make_request
  973. * if we can determine just by the mdev (state) that this request will fail,
  974. * return 1
  975. * otherwise return 0
  976. */
  977. static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
  978. {
  979. if (mdev->state.role != R_PRIMARY &&
  980. (!allow_oos || is_write)) {
  981. if (__ratelimit(&drbd_ratelimit_state)) {
  982. dev_err(DEV, "Process %s[%u] tried to %s; "
  983. "since we are not in Primary state, "
  984. "we cannot allow this\n",
  985. current->comm, current->pid,
  986. is_write ? "WRITE" : "READ");
  987. }
  988. return 1;
  989. }
  990. return 0;
  991. }
  992. void drbd_make_request(struct request_queue *q, struct bio *bio)
  993. {
  994. unsigned int s_enr, e_enr;
  995. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  996. unsigned long start_time;
  997. if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
  998. bio_endio(bio, -EPERM);
  999. return;
  1000. }
  1001. start_time = jiffies;
  1002. /*
  1003. * what we "blindly" assume:
  1004. */
  1005. D_ASSERT((bio->bi_size & 0x1ff) == 0);
  1006. /* to make some things easier, force alignment of requests within the
  1007. * granularity of our hash tables */
  1008. s_enr = bio->bi_sector >> HT_SHIFT;
  1009. e_enr = bio->bi_size ? (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT : s_enr;
  1010. if (likely(s_enr == e_enr)) {
  1011. do {
  1012. inc_ap_bio(mdev, 1);
  1013. } while (drbd_make_request_common(mdev, bio, start_time));
  1014. return;
  1015. }
  1016. /* can this bio be split generically?
  1017. * Maybe add our own split-arbitrary-bios function. */
  1018. if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_BIO_SIZE) {
  1019. /* rather error out here than BUG in bio_split */
  1020. dev_err(DEV, "bio would need to, but cannot, be split: "
  1021. "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
  1022. bio->bi_vcnt, bio->bi_idx, bio->bi_size,
  1023. (unsigned long long)bio->bi_sector);
  1024. bio_endio(bio, -EINVAL);
  1025. } else {
  1026. /* This bio crosses some boundary, so we have to split it. */
  1027. struct bio_pair *bp;
  1028. /* works for the "do not cross hash slot boundaries" case
  1029. * e.g. sector 262269, size 4096
  1030. * s_enr = 262269 >> 6 = 4097
  1031. * e_enr = (262269+8-1) >> 6 = 4098
  1032. * HT_SHIFT = 6
  1033. * sps = 64, mask = 63
  1034. * first_sectors = 64 - (262269 & 63) = 3
  1035. */
  1036. const sector_t sect = bio->bi_sector;
  1037. const int sps = 1 << HT_SHIFT; /* sectors per slot */
  1038. const int mask = sps - 1;
  1039. const sector_t first_sectors = sps - (sect & mask);
  1040. bp = bio_split(bio, first_sectors);
  1041. /* we need to get a "reference count" (ap_bio_cnt)
  1042. * to avoid races with the disconnect/reconnect/suspend code.
  1043. * In case we need to split the bio here, we need to get three references
  1044. * atomically, otherwise we might deadlock when trying to submit the
  1045. * second one! */
  1046. inc_ap_bio(mdev, 3);
  1047. D_ASSERT(e_enr == s_enr + 1);
  1048. while (drbd_make_request_common(mdev, &bp->bio1, start_time))
  1049. inc_ap_bio(mdev, 1);
  1050. while (drbd_make_request_common(mdev, &bp->bio2, start_time))
  1051. inc_ap_bio(mdev, 1);
  1052. dec_ap_bio(mdev);
  1053. bio_pair_release(bp);
  1054. }
  1055. }
  1056. /* This is called by bio_add_page(). With this function we reduce
  1057. * the number of BIOs that span over multiple DRBD_MAX_BIO_SIZEs
  1058. * units (was AL_EXTENTs).
  1059. *
  1060. * we do the calculation within the lower 32bit of the byte offsets,
  1061. * since we don't care for actual offset, but only check whether it
  1062. * would cross "activity log extent" boundaries.
  1063. *
  1064. * As long as the BIO is empty we have to allow at least one bvec,
  1065. * regardless of size and offset. so the resulting bio may still
  1066. * cross extent boundaries. those are dealt with (bio_split) in
  1067. * drbd_make_request.
  1068. */
  1069. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  1070. {
  1071. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  1072. unsigned int bio_offset =
  1073. (unsigned int)bvm->bi_sector << 9; /* 32 bit */
  1074. unsigned int bio_size = bvm->bi_size;
  1075. int limit, backing_limit;
  1076. limit = DRBD_MAX_BIO_SIZE
  1077. - ((bio_offset & (DRBD_MAX_BIO_SIZE-1)) + bio_size);
  1078. if (limit < 0)
  1079. limit = 0;
  1080. if (bio_size == 0) {
  1081. if (limit <= bvec->bv_len)
  1082. limit = bvec->bv_len;
  1083. } else if (limit && get_ldev(mdev)) {
  1084. struct request_queue * const b =
  1085. mdev->ldev->backing_bdev->bd_disk->queue;
  1086. if (b->merge_bvec_fn) {
  1087. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  1088. limit = min(limit, backing_limit);
  1089. }
  1090. put_ldev(mdev);
  1091. }
  1092. return limit;
  1093. }
  1094. void request_timer_fn(unsigned long data)
  1095. {
  1096. struct drbd_conf *mdev = (struct drbd_conf *) data;
  1097. struct drbd_request *req; /* oldest request */
  1098. struct list_head *le;
  1099. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  1100. unsigned long now;
  1101. if (get_net_conf(mdev)) {
  1102. if (mdev->state.conn >= C_WF_REPORT_PARAMS)
  1103. ent = mdev->net_conf->timeout*HZ/10
  1104. * mdev->net_conf->ko_count;
  1105. put_net_conf(mdev);
  1106. }
  1107. if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
  1108. dt = mdev->ldev->dc.disk_timeout * HZ / 10;
  1109. put_ldev(mdev);
  1110. }
  1111. et = min_not_zero(dt, ent);
  1112. if (!et)
  1113. return; /* Recurring timer stopped */
  1114. now = jiffies;
  1115. spin_lock_irq(&mdev->req_lock);
  1116. le = &mdev->oldest_tle->requests;
  1117. if (list_empty(le)) {
  1118. spin_unlock_irq(&mdev->req_lock);
  1119. mod_timer(&mdev->request_timer, now + et);
  1120. return;
  1121. }
  1122. le = le->prev;
  1123. req = list_entry(le, struct drbd_request, tl_requests);
  1124. /* The request is considered timed out, if
  1125. * - we have some effective timeout from the configuration,
  1126. * with above state restrictions applied,
  1127. * - the oldest request is waiting for a response from the network
  1128. * resp. the local disk,
  1129. * - the oldest request is in fact older than the effective timeout,
  1130. * - the connection was established (resp. disk was attached)
  1131. * for longer than the timeout already.
  1132. * Note that for 32bit jiffies and very stable connections/disks,
  1133. * we may have a wrap around, which is catched by
  1134. * !time_in_range(now, last_..._jif, last_..._jif + timeout).
  1135. *
  1136. * Side effect: once per 32bit wrap-around interval, which means every
  1137. * ~198 days with 250 HZ, we have a window where the timeout would need
  1138. * to expire twice (worst case) to become effective. Good enough.
  1139. */
  1140. if (ent && req->rq_state & RQ_NET_PENDING &&
  1141. time_after(now, req->start_time + ent) &&
  1142. !time_in_range(now, mdev->last_reconnect_jif, mdev->last_reconnect_jif + ent)) {
  1143. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  1144. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
  1145. }
  1146. if (dt && req->rq_state & RQ_LOCAL_PENDING &&
  1147. time_after(now, req->start_time + dt) &&
  1148. !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
  1149. dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
  1150. __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
  1151. }
  1152. nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
  1153. spin_unlock_irq(&mdev->req_lock);
  1154. mod_timer(&mdev->request_timer, nt);
  1155. }