drbd_req.c 35 KB

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