drbd_req.c 38 KB

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