drbd_req.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. (void) cpu; /* The macro invocations above want the cpu argument, I do not like
  33. the compiler warning about cpu only assigned but never used... */
  34. part_inc_in_flight(&mdev->vdisk->part0, rw);
  35. part_stat_unlock();
  36. }
  37. /* Update disk stats when completing request upwards */
  38. static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
  39. {
  40. int rw = bio_data_dir(req->master_bio);
  41. unsigned long duration = jiffies - req->start_time;
  42. int cpu;
  43. cpu = part_stat_lock();
  44. part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
  45. part_round_stats(cpu, &mdev->vdisk->part0);
  46. part_dec_in_flight(&mdev->vdisk->part0, rw);
  47. part_stat_unlock();
  48. }
  49. static struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
  50. struct bio *bio_src)
  51. {
  52. struct drbd_request *req;
  53. req = mempool_alloc(drbd_request_mempool, GFP_NOIO);
  54. if (!req)
  55. return NULL;
  56. drbd_req_make_private_bio(req, bio_src);
  57. req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
  58. req->w.mdev = mdev;
  59. req->master_bio = bio_src;
  60. req->epoch = 0;
  61. drbd_clear_interval(&req->i);
  62. req->i.sector = bio_src->bi_sector;
  63. req->i.size = bio_src->bi_size;
  64. req->i.local = true;
  65. req->i.waiting = false;
  66. INIT_LIST_HEAD(&req->tl_requests);
  67. INIT_LIST_HEAD(&req->w.list);
  68. return req;
  69. }
  70. static void drbd_req_free(struct drbd_request *req)
  71. {
  72. mempool_free(req, drbd_request_mempool);
  73. }
  74. /* rw is bio_data_dir(), only READ or WRITE */
  75. static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw)
  76. {
  77. const unsigned long s = req->rq_state;
  78. /* remove it from the transfer log.
  79. * well, only if it had been there in the first
  80. * place... if it had not (local only or conflicting
  81. * and never sent), it should still be "empty" as
  82. * initialized in drbd_req_new(), so we can list_del() it
  83. * here unconditionally */
  84. list_del(&req->tl_requests);
  85. /* if it was a write, we may have to set the corresponding
  86. * bit(s) out-of-sync first. If it had a local part, we need to
  87. * release the reference to the activity log. */
  88. if (rw == WRITE) {
  89. /* Set out-of-sync unless both OK flags are set
  90. * (local only or remote failed).
  91. * Other places where we set out-of-sync:
  92. * READ with local io-error */
  93. if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
  94. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  95. if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
  96. drbd_set_in_sync(mdev, req->i.sector, req->i.size);
  97. /* one might be tempted to move the drbd_al_complete_io
  98. * to the local io completion callback drbd_request_endio.
  99. * but, if this was a mirror write, we may only
  100. * drbd_al_complete_io after this is RQ_NET_DONE,
  101. * otherwise the extent could be dropped from the al
  102. * before it has actually been written on the peer.
  103. * if we crash before our peer knows about the request,
  104. * but after the extent has been dropped from the al,
  105. * we would forget to resync the corresponding extent.
  106. */
  107. if (s & RQ_LOCAL_MASK) {
  108. if (get_ldev_if_state(mdev, D_FAILED)) {
  109. if (s & RQ_IN_ACT_LOG)
  110. drbd_al_complete_io(mdev, &req->i);
  111. put_ldev(mdev);
  112. } else if (__ratelimit(&drbd_ratelimit_state)) {
  113. dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
  114. "but my Disk seems to have failed :(\n",
  115. (unsigned long long) req->i.sector, req->i.size);
  116. }
  117. }
  118. }
  119. drbd_req_free(req);
  120. }
  121. static void queue_barrier(struct drbd_conf *mdev)
  122. {
  123. struct drbd_tl_epoch *b;
  124. struct drbd_tconn *tconn = mdev->tconn;
  125. /* We are within the req_lock. Once we queued the barrier for sending,
  126. * we set the CREATE_BARRIER bit. It is cleared as soon as a new
  127. * barrier/epoch object is added. This is the only place this bit is
  128. * set. It indicates that the barrier for this epoch is already queued,
  129. * and no new epoch has been created yet. */
  130. if (test_bit(CREATE_BARRIER, &tconn->flags))
  131. return;
  132. b = tconn->newest_tle;
  133. b->w.cb = w_send_barrier;
  134. b->w.mdev = mdev;
  135. /* inc_ap_pending done here, so we won't
  136. * get imbalanced on connection loss.
  137. * dec_ap_pending will be done in got_BarrierAck
  138. * or (on connection loss) in tl_clear. */
  139. inc_ap_pending(mdev);
  140. drbd_queue_work(&tconn->data.work, &b->w);
  141. set_bit(CREATE_BARRIER, &tconn->flags);
  142. }
  143. static void _about_to_complete_local_write(struct drbd_conf *mdev,
  144. struct drbd_request *req)
  145. {
  146. const unsigned long s = req->rq_state;
  147. /* Before we can signal completion to the upper layers,
  148. * we may need to close the current epoch.
  149. * We can skip this, if this request has not even been sent, because we
  150. * did not have a fully established connection yet/anymore, during
  151. * bitmap exchange, or while we are C_AHEAD due to congestion policy.
  152. */
  153. if (mdev->state.conn >= C_CONNECTED &&
  154. (s & RQ_NET_SENT) != 0 &&
  155. req->epoch == mdev->tconn->newest_tle->br_number)
  156. queue_barrier(mdev);
  157. }
  158. void complete_master_bio(struct drbd_conf *mdev,
  159. struct bio_and_error *m)
  160. {
  161. bio_endio(m->bio, m->error);
  162. dec_ap_bio(mdev);
  163. }
  164. static void drbd_remove_request_interval(struct rb_root *root,
  165. struct drbd_request *req)
  166. {
  167. struct drbd_conf *mdev = req->w.mdev;
  168. struct drbd_interval *i = &req->i;
  169. drbd_remove_interval(root, i);
  170. /* Wake up any processes waiting for this request to complete. */
  171. if (i->waiting)
  172. wake_up(&mdev->misc_wait);
  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->w.mdev;
  184. int rw = req->rq_state & RQ_WRITE ? WRITE : READ;
  185. /* we must not complete the master bio, while it is
  186. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  187. * not yet acknowledged by the peer
  188. * not yet completed by the local io subsystem
  189. * these flags may get cleared in any order by
  190. * the worker,
  191. * the receiver,
  192. * the bio_endio completion callbacks.
  193. */
  194. if (s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED))
  195. return;
  196. if (req->i.waiting) {
  197. /* Retry all conflicting peer requests. */
  198. wake_up(&mdev->misc_wait);
  199. }
  200. if (s & RQ_NET_QUEUED)
  201. return;
  202. if (s & RQ_NET_PENDING)
  203. return;
  204. if (req->master_bio) {
  205. /* this is DATA_RECEIVED (remote read)
  206. * or protocol C P_WRITE_ACK
  207. * or protocol B P_RECV_ACK
  208. * or protocol A "HANDED_OVER_TO_NETWORK" (SendAck)
  209. * or canceled or failed,
  210. * or killed from the transfer log due to connection loss.
  211. */
  212. /*
  213. * figure out whether to report success or failure.
  214. *
  215. * report success when at least one of the operations succeeded.
  216. * or, to put the other way,
  217. * only report failure, when both operations failed.
  218. *
  219. * what to do about the failures is handled elsewhere.
  220. * what we need to do here is just: complete the master_bio.
  221. *
  222. * local completion error, if any, has been stored as ERR_PTR
  223. * in private_bio within drbd_request_endio.
  224. */
  225. int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
  226. int error = PTR_ERR(req->private_bio);
  227. /* remove the request from the conflict detection
  228. * respective block_id verification hash */
  229. if (!drbd_interval_empty(&req->i)) {
  230. struct rb_root *root;
  231. if (rw == WRITE)
  232. root = &mdev->write_requests;
  233. else
  234. root = &mdev->read_requests;
  235. drbd_remove_request_interval(root, req);
  236. } else if (!(s & RQ_POSTPONED))
  237. D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
  238. /* for writes we need to do some extra housekeeping */
  239. if (rw == WRITE)
  240. _about_to_complete_local_write(mdev, req);
  241. /* Update disk stats */
  242. _drbd_end_io_acct(mdev, req);
  243. if (!(s & RQ_POSTPONED)) {
  244. m->error = ok ? 0 : (error ?: -EIO);
  245. m->bio = req->master_bio;
  246. }
  247. req->master_bio = NULL;
  248. }
  249. if (s & RQ_LOCAL_PENDING)
  250. return;
  251. if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) {
  252. /* this is disconnected (local only) operation,
  253. * or protocol A, B, or C P_BARRIER_ACK,
  254. * or killed from the transfer log due to connection loss. */
  255. _req_is_done(mdev, req, rw);
  256. }
  257. /* else: network part and not DONE yet. that is
  258. * protocol A, B, or C, barrier ack still pending... */
  259. }
  260. static void _req_may_be_done_not_susp(struct drbd_request *req, struct bio_and_error *m)
  261. {
  262. struct drbd_conf *mdev = req->w.mdev;
  263. if (!drbd_suspended(mdev))
  264. _req_may_be_done(req, m);
  265. }
  266. /* obviously this could be coded as many single functions
  267. * instead of one huge switch,
  268. * or by putting the code directly in the respective locations
  269. * (as it has been before).
  270. *
  271. * but having it this way
  272. * enforces that it is all in this one place, where it is easier to audit,
  273. * it makes it obvious that whatever "event" "happens" to a request should
  274. * happen "atomically" within the req_lock,
  275. * and it enforces that we have to think in a very structured manner
  276. * about the "events" that may happen to a request during its life time ...
  277. */
  278. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  279. struct bio_and_error *m)
  280. {
  281. struct drbd_conf *mdev = req->w.mdev;
  282. struct net_conf *nc;
  283. int p, rv = 0;
  284. if (m)
  285. m->bio = NULL;
  286. switch (what) {
  287. default:
  288. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  289. break;
  290. /* does not happen...
  291. * initialization done in drbd_req_new
  292. case CREATED:
  293. break;
  294. */
  295. case TO_BE_SENT: /* via network */
  296. /* reached via __drbd_make_request
  297. * and from w_read_retry_remote */
  298. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  299. req->rq_state |= RQ_NET_PENDING;
  300. rcu_read_lock();
  301. nc = rcu_dereference(mdev->tconn->net_conf);
  302. p = nc->wire_protocol;
  303. rcu_read_unlock();
  304. req->rq_state |=
  305. p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
  306. p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
  307. inc_ap_pending(mdev);
  308. break;
  309. case TO_BE_SUBMITTED: /* locally */
  310. /* reached via __drbd_make_request */
  311. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  312. req->rq_state |= RQ_LOCAL_PENDING;
  313. break;
  314. case COMPLETED_OK:
  315. if (req->rq_state & RQ_WRITE)
  316. mdev->writ_cnt += req->i.size >> 9;
  317. else
  318. mdev->read_cnt += req->i.size >> 9;
  319. req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  320. req->rq_state &= ~RQ_LOCAL_PENDING;
  321. _req_may_be_done_not_susp(req, m);
  322. put_ldev(mdev);
  323. break;
  324. case ABORT_DISK_IO:
  325. req->rq_state |= RQ_LOCAL_ABORTED;
  326. if (req->rq_state & RQ_WRITE)
  327. _req_may_be_done_not_susp(req, m);
  328. else
  329. goto goto_queue_for_net_read;
  330. break;
  331. case WRITE_COMPLETED_WITH_ERROR:
  332. req->rq_state |= RQ_LOCAL_COMPLETED;
  333. req->rq_state &= ~RQ_LOCAL_PENDING;
  334. __drbd_chk_io_error(mdev, false);
  335. _req_may_be_done_not_susp(req, m);
  336. put_ldev(mdev);
  337. break;
  338. case READ_AHEAD_COMPLETED_WITH_ERROR:
  339. /* it is legal to fail READA */
  340. req->rq_state |= RQ_LOCAL_COMPLETED;
  341. req->rq_state &= ~RQ_LOCAL_PENDING;
  342. _req_may_be_done_not_susp(req, m);
  343. put_ldev(mdev);
  344. break;
  345. case READ_COMPLETED_WITH_ERROR:
  346. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  347. req->rq_state |= RQ_LOCAL_COMPLETED;
  348. req->rq_state &= ~RQ_LOCAL_PENDING;
  349. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  350. __drbd_chk_io_error(mdev, false);
  351. put_ldev(mdev);
  352. goto_queue_for_net_read:
  353. /* no point in retrying if there is no good remote data,
  354. * or we have no connection. */
  355. if (mdev->state.pdsk != D_UP_TO_DATE) {
  356. _req_may_be_done_not_susp(req, m);
  357. break;
  358. }
  359. /* _req_mod(req,TO_BE_SENT); oops, recursion... */
  360. req->rq_state |= RQ_NET_PENDING;
  361. inc_ap_pending(mdev);
  362. /* fall through: _req_mod(req,QUEUE_FOR_NET_READ); */
  363. case QUEUE_FOR_NET_READ:
  364. /* READ or READA, and
  365. * no local disk,
  366. * or target area marked as invalid,
  367. * or just got an io-error. */
  368. /* from __drbd_make_request
  369. * or from bio_endio during read io-error recovery */
  370. /* so we can verify the handle in the answer packet
  371. * corresponding hlist_del is in _req_may_be_done() */
  372. D_ASSERT(drbd_interval_empty(&req->i));
  373. drbd_insert_interval(&mdev->read_requests, &req->i);
  374. set_bit(UNPLUG_REMOTE, &mdev->flags);
  375. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  376. req->rq_state |= RQ_NET_QUEUED;
  377. req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
  378. ? w_read_retry_remote
  379. : w_send_read_req;
  380. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  381. break;
  382. case QUEUE_FOR_NET_WRITE:
  383. /* assert something? */
  384. /* from __drbd_make_request only */
  385. /* corresponding hlist_del is in _req_may_be_done() */
  386. D_ASSERT(drbd_interval_empty(&req->i));
  387. drbd_insert_interval(&mdev->write_requests, &req->i);
  388. /* NOTE
  389. * In case the req ended up on the transfer log before being
  390. * queued on the worker, it could lead to this request being
  391. * missed during cleanup after connection loss.
  392. * So we have to do both operations here,
  393. * within the same lock that protects the transfer log.
  394. *
  395. * _req_add_to_epoch(req); this has to be after the
  396. * _maybe_start_new_epoch(req); which happened in
  397. * __drbd_make_request, because we now may set the bit
  398. * again ourselves to close the current epoch.
  399. *
  400. * Add req to the (now) current epoch (barrier). */
  401. /* otherwise we may lose an unplug, which may cause some remote
  402. * io-scheduler timeout to expire, increasing maximum latency,
  403. * hurting performance. */
  404. set_bit(UNPLUG_REMOTE, &mdev->flags);
  405. /* see __drbd_make_request,
  406. * just after it grabs the req_lock */
  407. D_ASSERT(test_bit(CREATE_BARRIER, &mdev->tconn->flags) == 0);
  408. req->epoch = mdev->tconn->newest_tle->br_number;
  409. /* increment size of current epoch */
  410. mdev->tconn->newest_tle->n_writes++;
  411. /* queue work item to send data */
  412. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  413. req->rq_state |= RQ_NET_QUEUED;
  414. req->w.cb = w_send_dblock;
  415. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  416. /* close the epoch, in case it outgrew the limit */
  417. rcu_read_lock();
  418. nc = rcu_dereference(mdev->tconn->net_conf);
  419. p = nc->max_epoch_size;
  420. rcu_read_unlock();
  421. if (mdev->tconn->newest_tle->n_writes >= p)
  422. queue_barrier(mdev);
  423. break;
  424. case QUEUE_FOR_SEND_OOS:
  425. req->rq_state |= RQ_NET_QUEUED;
  426. req->w.cb = w_send_out_of_sync;
  427. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  428. break;
  429. case OOS_HANDED_TO_NETWORK:
  430. /* actually the same */
  431. case SEND_CANCELED:
  432. /* treat it the same */
  433. case SEND_FAILED:
  434. /* real cleanup will be done from tl_clear. just update flags
  435. * so it is no longer marked as on the worker queue */
  436. req->rq_state &= ~RQ_NET_QUEUED;
  437. /* if we did it right, tl_clear should be scheduled only after
  438. * this, so this should not be necessary! */
  439. _req_may_be_done_not_susp(req, m);
  440. break;
  441. case HANDED_OVER_TO_NETWORK:
  442. /* assert something? */
  443. if (bio_data_dir(req->master_bio) == WRITE)
  444. atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
  445. if (bio_data_dir(req->master_bio) == WRITE &&
  446. !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
  447. /* this is what is dangerous about protocol A:
  448. * pretend it was successfully written on the peer. */
  449. if (req->rq_state & RQ_NET_PENDING) {
  450. dec_ap_pending(mdev);
  451. req->rq_state &= ~RQ_NET_PENDING;
  452. req->rq_state |= RQ_NET_OK;
  453. } /* else: neg-ack was faster... */
  454. /* it is still not yet RQ_NET_DONE until the
  455. * corresponding epoch barrier got acked as well,
  456. * so we know what to dirty on connection loss */
  457. }
  458. req->rq_state &= ~RQ_NET_QUEUED;
  459. req->rq_state |= RQ_NET_SENT;
  460. /* because _drbd_send_zc_bio could sleep, and may want to
  461. * dereference the bio even after the "WRITE_ACKED_BY_PEER" and
  462. * "COMPLETED_OK" events came in, once we return from
  463. * _drbd_send_zc_bio (drbd_send_dblock), we have to check
  464. * whether it is done already, and end it. */
  465. _req_may_be_done_not_susp(req, m);
  466. break;
  467. case READ_RETRY_REMOTE_CANCELED:
  468. req->rq_state &= ~RQ_NET_QUEUED;
  469. /* fall through, in case we raced with drbd_disconnect */
  470. case CONNECTION_LOST_WHILE_PENDING:
  471. /* transfer log cleanup after connection loss */
  472. /* assert something? */
  473. if (req->rq_state & RQ_NET_PENDING)
  474. dec_ap_pending(mdev);
  475. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  476. req->rq_state |= RQ_NET_DONE;
  477. if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE)
  478. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  479. /* if it is still queued, we may not complete it here.
  480. * it will be canceled soon. */
  481. if (!(req->rq_state & RQ_NET_QUEUED))
  482. _req_may_be_done(req, m); /* Allowed while state.susp */
  483. break;
  484. case WRITE_ACKED_BY_PEER_AND_SIS:
  485. req->rq_state |= RQ_NET_SIS;
  486. case DISCARD_WRITE:
  487. /* for discarded conflicting writes of multiple primaries,
  488. * there is no need to keep anything in the tl, potential
  489. * node crashes are covered by the activity log. */
  490. req->rq_state |= RQ_NET_DONE;
  491. /* fall through */
  492. case WRITE_ACKED_BY_PEER:
  493. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  494. /* protocol C; successfully written on peer.
  495. * Nothing to do here.
  496. * We want to keep the tl in place for all protocols, to cater
  497. * for volatile write-back caches on lower level devices.
  498. *
  499. * A barrier request is expected to have forced all prior
  500. * requests onto stable storage, so completion of a barrier
  501. * request could set NET_DONE right here, and not wait for the
  502. * P_BARRIER_ACK, but that is an unnecessary optimization. */
  503. goto ack_common;
  504. /* this makes it effectively the same as for: */
  505. case RECV_ACKED_BY_PEER:
  506. D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
  507. /* protocol B; pretends to be successfully written on peer.
  508. * see also notes above in HANDED_OVER_TO_NETWORK about
  509. * protocol != C */
  510. ack_common:
  511. req->rq_state |= RQ_NET_OK;
  512. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  513. dec_ap_pending(mdev);
  514. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  515. req->rq_state &= ~RQ_NET_PENDING;
  516. _req_may_be_done_not_susp(req, m);
  517. break;
  518. case POSTPONE_WRITE:
  519. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  520. /* If this node has already detected the write conflict, the
  521. * worker will be waiting on misc_wait. Wake it up once this
  522. * request has completed locally.
  523. */
  524. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  525. req->rq_state |= RQ_POSTPONED;
  526. _req_may_be_done_not_susp(req, m);
  527. break;
  528. case NEG_ACKED:
  529. /* assert something? */
  530. if (req->rq_state & RQ_NET_PENDING) {
  531. dec_ap_pending(mdev);
  532. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  533. }
  534. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  535. req->rq_state |= RQ_NET_DONE;
  536. _req_may_be_done_not_susp(req, m);
  537. /* else: done by HANDED_OVER_TO_NETWORK */
  538. break;
  539. case FAIL_FROZEN_DISK_IO:
  540. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  541. break;
  542. _req_may_be_done(req, m); /* Allowed while state.susp */
  543. break;
  544. case RESTART_FROZEN_DISK_IO:
  545. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  546. break;
  547. req->rq_state &= ~RQ_LOCAL_COMPLETED;
  548. rv = MR_READ;
  549. if (bio_data_dir(req->master_bio) == WRITE)
  550. rv = MR_WRITE;
  551. get_ldev(mdev);
  552. req->w.cb = w_restart_disk_io;
  553. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  554. break;
  555. case RESEND:
  556. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  557. before the connection loss (B&C only); only P_BARRIER_ACK was missing.
  558. Trowing them out of the TL here by pretending we got a BARRIER_ACK
  559. We ensure that the peer was not rebooted */
  560. if (!(req->rq_state & RQ_NET_OK)) {
  561. if (req->w.cb) {
  562. drbd_queue_work(&mdev->tconn->data.work, &req->w);
  563. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  564. }
  565. break;
  566. }
  567. /* else, fall through to BARRIER_ACKED */
  568. case BARRIER_ACKED:
  569. if (!(req->rq_state & RQ_WRITE))
  570. break;
  571. if (req->rq_state & RQ_NET_PENDING) {
  572. /* barrier came in before all requests were acked.
  573. * this is bad, because if the connection is lost now,
  574. * we won't be able to clean them up... */
  575. dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
  576. list_move(&req->tl_requests, &mdev->tconn->out_of_sequence_requests);
  577. }
  578. if ((req->rq_state & RQ_NET_MASK) != 0) {
  579. req->rq_state |= RQ_NET_DONE;
  580. if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)))
  581. atomic_sub(req->i.size>>9, &mdev->ap_in_flight);
  582. }
  583. _req_may_be_done(req, m); /* Allowed while state.susp */
  584. break;
  585. case DATA_RECEIVED:
  586. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  587. dec_ap_pending(mdev);
  588. req->rq_state &= ~RQ_NET_PENDING;
  589. req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
  590. _req_may_be_done_not_susp(req, m);
  591. break;
  592. };
  593. return rv;
  594. }
  595. /* we may do a local read if:
  596. * - we are consistent (of course),
  597. * - or we are generally inconsistent,
  598. * BUT we are still/already IN SYNC for this area.
  599. * since size may be bigger than BM_BLOCK_SIZE,
  600. * we may need to check several bits.
  601. */
  602. static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  603. {
  604. unsigned long sbnr, ebnr;
  605. sector_t esector, nr_sectors;
  606. if (mdev->state.disk == D_UP_TO_DATE)
  607. return true;
  608. if (mdev->state.disk != D_INCONSISTENT)
  609. return false;
  610. esector = sector + (size >> 9) - 1;
  611. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  612. D_ASSERT(sector < nr_sectors);
  613. D_ASSERT(esector < nr_sectors);
  614. sbnr = BM_SECT_TO_BIT(sector);
  615. ebnr = BM_SECT_TO_BIT(esector);
  616. return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
  617. }
  618. /*
  619. * complete_conflicting_writes - wait for any conflicting write requests
  620. *
  621. * The write_requests tree contains all active write requests which we
  622. * currently know about. Wait for any requests to complete which conflict with
  623. * the new one.
  624. */
  625. static int complete_conflicting_writes(struct drbd_conf *mdev,
  626. sector_t sector, int size)
  627. {
  628. for(;;) {
  629. struct drbd_interval *i;
  630. int err;
  631. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  632. if (!i)
  633. return 0;
  634. err = drbd_wait_misc(mdev, i);
  635. if (err)
  636. return err;
  637. }
  638. }
  639. int __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  640. {
  641. const int rw = bio_rw(bio);
  642. const int size = bio->bi_size;
  643. const sector_t sector = bio->bi_sector;
  644. struct drbd_tl_epoch *b = NULL;
  645. struct drbd_request *req;
  646. struct net_conf *nc;
  647. int local, remote, send_oos = 0;
  648. int err;
  649. int ret = 0;
  650. /* allocate outside of all locks; */
  651. req = drbd_req_new(mdev, bio);
  652. if (!req) {
  653. dec_ap_bio(mdev);
  654. /* only pass the error to the upper layers.
  655. * if user cannot handle io errors, that's not our business. */
  656. dev_err(DEV, "could not kmalloc() req\n");
  657. bio_endio(bio, -ENOMEM);
  658. return 0;
  659. }
  660. req->start_time = start_time;
  661. local = get_ldev(mdev);
  662. if (!local) {
  663. bio_put(req->private_bio); /* or we get a bio leak */
  664. req->private_bio = NULL;
  665. }
  666. if (rw == WRITE) {
  667. remote = 1;
  668. } else {
  669. /* READ || READA */
  670. if (local) {
  671. if (!drbd_may_do_local_read(mdev, sector, size)) {
  672. /* we could kick the syncer to
  673. * sync this extent asap, wait for
  674. * it, then continue locally.
  675. * Or just issue the request remotely.
  676. */
  677. local = 0;
  678. bio_put(req->private_bio);
  679. req->private_bio = NULL;
  680. put_ldev(mdev);
  681. }
  682. }
  683. remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
  684. }
  685. /* If we have a disk, but a READA request is mapped to remote,
  686. * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
  687. * Just fail that READA request right here.
  688. *
  689. * THINK: maybe fail all READA when not local?
  690. * or make this configurable...
  691. * if network is slow, READA won't do any good.
  692. */
  693. if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
  694. err = -EWOULDBLOCK;
  695. goto fail_and_free_req;
  696. }
  697. /* For WRITES going to the local disk, grab a reference on the target
  698. * extent. This waits for any resync activity in the corresponding
  699. * resync extent to finish, and, if necessary, pulls in the target
  700. * extent into the activity log, which involves further disk io because
  701. * of transactional on-disk meta data updates. */
  702. if (rw == WRITE && local && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  703. req->rq_state |= RQ_IN_ACT_LOG;
  704. drbd_al_begin_io(mdev, &req->i);
  705. }
  706. remote = remote && drbd_should_do_remote(mdev->state);
  707. send_oos = rw == WRITE && drbd_should_send_out_of_sync(mdev->state);
  708. D_ASSERT(!(remote && send_oos));
  709. if (!(local || remote) && !drbd_suspended(mdev)) {
  710. if (__ratelimit(&drbd_ratelimit_state))
  711. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  712. err = -EIO;
  713. goto fail_free_complete;
  714. }
  715. /* For WRITE request, we have to make sure that we have an
  716. * unused_spare_tle, in case we need to start a new epoch.
  717. * I try to be smart and avoid to pre-allocate always "just in case",
  718. * but there is a race between testing the bit and pointer outside the
  719. * spinlock, and grabbing the spinlock.
  720. * if we lost that race, we retry. */
  721. if (rw == WRITE && (remote || send_oos) &&
  722. mdev->tconn->unused_spare_tle == NULL &&
  723. test_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
  724. allocate_barrier:
  725. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
  726. if (!b) {
  727. dev_err(DEV, "Failed to alloc barrier.\n");
  728. err = -ENOMEM;
  729. goto fail_free_complete;
  730. }
  731. }
  732. /* GOOD, everything prepared, grab the spin_lock */
  733. spin_lock_irq(&mdev->tconn->req_lock);
  734. if (rw == WRITE) {
  735. err = complete_conflicting_writes(mdev, sector, size);
  736. if (err) {
  737. if (err != -ERESTARTSYS)
  738. _conn_request_state(mdev->tconn,
  739. NS(conn, C_TIMEOUT),
  740. CS_HARD);
  741. spin_unlock_irq(&mdev->tconn->req_lock);
  742. err = -EIO;
  743. goto fail_free_complete;
  744. }
  745. }
  746. if (drbd_suspended(mdev)) {
  747. /* If we got suspended, use the retry mechanism of
  748. generic_make_request() to restart processing of this
  749. bio. In the next call to drbd_make_request
  750. we sleep in inc_ap_bio() */
  751. ret = 1;
  752. spin_unlock_irq(&mdev->tconn->req_lock);
  753. goto fail_free_complete;
  754. }
  755. if (remote || send_oos) {
  756. remote = drbd_should_do_remote(mdev->state);
  757. send_oos = rw == WRITE && drbd_should_send_out_of_sync(mdev->state);
  758. D_ASSERT(!(remote && send_oos));
  759. if (!(remote || send_oos))
  760. dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
  761. if (!(local || remote)) {
  762. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  763. spin_unlock_irq(&mdev->tconn->req_lock);
  764. err = -EIO;
  765. goto fail_free_complete;
  766. }
  767. }
  768. if (b && mdev->tconn->unused_spare_tle == NULL) {
  769. mdev->tconn->unused_spare_tle = b;
  770. b = NULL;
  771. }
  772. if (rw == WRITE && (remote || send_oos) &&
  773. mdev->tconn->unused_spare_tle == NULL &&
  774. test_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
  775. /* someone closed the current epoch
  776. * while we were grabbing the spinlock */
  777. spin_unlock_irq(&mdev->tconn->req_lock);
  778. goto allocate_barrier;
  779. }
  780. /* Update disk stats */
  781. _drbd_start_io_acct(mdev, req, bio);
  782. /* _maybe_start_new_epoch(mdev);
  783. * If we need to generate a write barrier packet, we have to add the
  784. * new epoch (barrier) object, and queue the barrier packet for sending,
  785. * and queue the req's data after it _within the same lock_, otherwise
  786. * we have race conditions were the reorder domains could be mixed up.
  787. *
  788. * Even read requests may start a new epoch and queue the corresponding
  789. * barrier packet. To get the write ordering right, we only have to
  790. * make sure that, if this is a write request and it triggered a
  791. * barrier packet, this request is queued within the same spinlock. */
  792. if ((remote || send_oos) && mdev->tconn->unused_spare_tle &&
  793. test_and_clear_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
  794. _tl_add_barrier(mdev->tconn, mdev->tconn->unused_spare_tle);
  795. mdev->tconn->unused_spare_tle = NULL;
  796. } else {
  797. D_ASSERT(!(remote && rw == WRITE &&
  798. test_bit(CREATE_BARRIER, &mdev->tconn->flags)));
  799. }
  800. /* NOTE
  801. * Actually, 'local' may be wrong here already, since we may have failed
  802. * to write to the meta data, and may become wrong anytime because of
  803. * local io-error for some other request, which would lead to us
  804. * "detaching" the local disk.
  805. *
  806. * 'remote' may become wrong any time because the network could fail.
  807. *
  808. * This is a harmless race condition, though, since it is handled
  809. * correctly at the appropriate places; so it just defers the failure
  810. * of the respective operation.
  811. */
  812. /* mark them early for readability.
  813. * this just sets some state flags. */
  814. if (remote)
  815. _req_mod(req, TO_BE_SENT);
  816. if (local)
  817. _req_mod(req, TO_BE_SUBMITTED);
  818. list_add_tail(&req->tl_requests, &mdev->tconn->newest_tle->requests);
  819. /* NOTE remote first: to get the concurrent write detection right,
  820. * we must register the request before start of local IO. */
  821. if (remote) {
  822. /* either WRITE and C_CONNECTED,
  823. * or READ, and no local disk,
  824. * or READ, but not in sync.
  825. */
  826. _req_mod(req, (rw == WRITE)
  827. ? QUEUE_FOR_NET_WRITE
  828. : QUEUE_FOR_NET_READ);
  829. }
  830. if (send_oos && drbd_set_out_of_sync(mdev, sector, size))
  831. _req_mod(req, QUEUE_FOR_SEND_OOS);
  832. rcu_read_lock();
  833. nc = rcu_dereference(mdev->tconn->net_conf);
  834. if (remote &&
  835. nc->on_congestion != OC_BLOCK && mdev->tconn->agreed_pro_version >= 96) {
  836. int congested = 0;
  837. if (nc->cong_fill &&
  838. atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
  839. dev_info(DEV, "Congestion-fill threshold reached\n");
  840. congested = 1;
  841. }
  842. if (mdev->act_log->used >= nc->cong_extents) {
  843. dev_info(DEV, "Congestion-extents threshold reached\n");
  844. congested = 1;
  845. }
  846. if (congested) {
  847. queue_barrier(mdev); /* last barrier, after mirrored writes */
  848. if (nc->on_congestion == OC_PULL_AHEAD)
  849. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  850. else /*nc->on_congestion == OC_DISCONNECT */
  851. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  852. }
  853. }
  854. rcu_read_unlock();
  855. spin_unlock_irq(&mdev->tconn->req_lock);
  856. kfree(b); /* if someone else has beaten us to it... */
  857. if (local) {
  858. req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
  859. /* State may have changed since we grabbed our reference on the
  860. * mdev->ldev member. Double check, and short-circuit to endio.
  861. * In case the last activity log transaction failed to get on
  862. * stable storage, and this is a WRITE, we may not even submit
  863. * this bio. */
  864. if (get_ldev(mdev)) {
  865. if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
  866. : rw == READ ? DRBD_FAULT_DT_RD
  867. : DRBD_FAULT_DT_RA))
  868. bio_endio(req->private_bio, -EIO);
  869. else
  870. generic_make_request(req->private_bio);
  871. put_ldev(mdev);
  872. } else
  873. bio_endio(req->private_bio, -EIO);
  874. }
  875. return 0;
  876. fail_free_complete:
  877. if (req->rq_state & RQ_IN_ACT_LOG)
  878. drbd_al_complete_io(mdev, &req->i);
  879. fail_and_free_req:
  880. if (local) {
  881. bio_put(req->private_bio);
  882. req->private_bio = NULL;
  883. put_ldev(mdev);
  884. }
  885. if (!ret)
  886. bio_endio(bio, err);
  887. drbd_req_free(req);
  888. dec_ap_bio(mdev);
  889. kfree(b);
  890. return ret;
  891. }
  892. int drbd_make_request(struct request_queue *q, struct bio *bio)
  893. {
  894. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  895. unsigned long start_time;
  896. start_time = jiffies;
  897. /*
  898. * what we "blindly" assume:
  899. */
  900. D_ASSERT(bio->bi_size > 0);
  901. D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
  902. inc_ap_bio(mdev);
  903. return __drbd_make_request(mdev, bio, start_time);
  904. }
  905. /* This is called by bio_add_page().
  906. *
  907. * q->max_hw_sectors and other global limits are already enforced there.
  908. *
  909. * We need to call down to our lower level device,
  910. * in case it has special restrictions.
  911. *
  912. * We also may need to enforce configured max-bio-bvecs limits.
  913. *
  914. * As long as the BIO is empty we have to allow at least one bvec,
  915. * regardless of size and offset, so no need to ask lower levels.
  916. */
  917. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  918. {
  919. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  920. unsigned int bio_size = bvm->bi_size;
  921. int limit = DRBD_MAX_BIO_SIZE;
  922. int backing_limit;
  923. if (bio_size && get_ldev(mdev)) {
  924. struct request_queue * const b =
  925. mdev->ldev->backing_bdev->bd_disk->queue;
  926. if (b->merge_bvec_fn) {
  927. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  928. limit = min(limit, backing_limit);
  929. }
  930. put_ldev(mdev);
  931. }
  932. return limit;
  933. }
  934. void request_timer_fn(unsigned long data)
  935. {
  936. struct drbd_conf *mdev = (struct drbd_conf *) data;
  937. struct drbd_tconn *tconn = mdev->tconn;
  938. struct drbd_request *req; /* oldest request */
  939. struct list_head *le;
  940. struct net_conf *nc;
  941. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  942. rcu_read_lock();
  943. nc = rcu_dereference(tconn->net_conf);
  944. ent = nc ? nc->timeout * HZ/10 * nc->ko_count : 0;
  945. if (get_ldev(mdev)) {
  946. dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
  947. put_ldev(mdev);
  948. }
  949. rcu_read_unlock();
  950. et = min_not_zero(dt, ent);
  951. if (!et || (mdev->state.conn < C_WF_REPORT_PARAMS && mdev->state.disk <= D_FAILED))
  952. return; /* Recurring timer stopped */
  953. spin_lock_irq(&tconn->req_lock);
  954. le = &tconn->oldest_tle->requests;
  955. if (list_empty(le)) {
  956. spin_unlock_irq(&tconn->req_lock);
  957. mod_timer(&mdev->request_timer, jiffies + et);
  958. return;
  959. }
  960. le = le->prev;
  961. req = list_entry(le, struct drbd_request, tl_requests);
  962. if (ent && req->rq_state & RQ_NET_PENDING) {
  963. if (time_is_before_eq_jiffies(req->start_time + ent)) {
  964. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  965. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
  966. }
  967. }
  968. if (dt && req->rq_state & RQ_LOCAL_PENDING) {
  969. if (time_is_before_eq_jiffies(req->start_time + dt)) {
  970. dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
  971. __drbd_chk_io_error(mdev, 1);
  972. }
  973. }
  974. nt = (time_is_before_eq_jiffies(req->start_time + et) ? jiffies : req->start_time) + et;
  975. spin_unlock_irq(&tconn->req_lock);
  976. mod_timer(&mdev->request_timer, nt);
  977. }