drbd_req.c 35 KB

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