drbd_req.c 33 KB

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