drbd_req.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size);
  25. /* Update disk stats at start of I/O request */
  26. static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
  27. {
  28. const int rw = bio_data_dir(req->master_bio);
  29. int cpu;
  30. cpu = part_stat_lock();
  31. part_round_stats(cpu, &mdev->vdisk->part0);
  32. part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
  33. part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], req->i.size >> 9);
  34. (void) cpu; /* The macro invocations above want the cpu argument, I do not like
  35. the compiler warning about cpu only assigned but never used... */
  36. part_inc_in_flight(&mdev->vdisk->part0, rw);
  37. part_stat_unlock();
  38. }
  39. /* Update disk stats when completing request upwards */
  40. static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
  41. {
  42. int rw = bio_data_dir(req->master_bio);
  43. unsigned long duration = jiffies - req->start_time;
  44. int cpu;
  45. cpu = part_stat_lock();
  46. part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
  47. part_round_stats(cpu, &mdev->vdisk->part0);
  48. part_dec_in_flight(&mdev->vdisk->part0, rw);
  49. part_stat_unlock();
  50. }
  51. static struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
  52. struct bio *bio_src)
  53. {
  54. struct drbd_request *req;
  55. req = mempool_alloc(drbd_request_mempool, GFP_NOIO);
  56. if (!req)
  57. return NULL;
  58. drbd_req_make_private_bio(req, bio_src);
  59. req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
  60. req->w.mdev = mdev;
  61. req->master_bio = bio_src;
  62. req->epoch = 0;
  63. drbd_clear_interval(&req->i);
  64. req->i.sector = bio_src->bi_sector;
  65. req->i.size = bio_src->bi_size;
  66. req->i.local = true;
  67. req->i.waiting = false;
  68. INIT_LIST_HEAD(&req->tl_requests);
  69. INIT_LIST_HEAD(&req->w.list);
  70. /* one reference to be put by __drbd_make_request */
  71. atomic_set(&req->completion_ref, 1);
  72. /* one kref as long as completion_ref > 0 */
  73. kref_init(&req->kref);
  74. return req;
  75. }
  76. void drbd_req_destroy(struct kref *kref)
  77. {
  78. struct drbd_request *req = container_of(kref, struct drbd_request, kref);
  79. struct drbd_conf *mdev = req->w.mdev;
  80. const unsigned s = req->rq_state;
  81. if ((req->master_bio && !(s & RQ_POSTPONED)) ||
  82. atomic_read(&req->completion_ref) ||
  83. (s & RQ_LOCAL_PENDING) ||
  84. ((s & RQ_NET_MASK) && !(s & RQ_NET_DONE))) {
  85. dev_err(DEV, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
  86. s, atomic_read(&req->completion_ref));
  87. return;
  88. }
  89. /* remove it from the transfer log.
  90. * well, only if it had been there in the first
  91. * place... if it had not (local only or conflicting
  92. * and never sent), it should still be "empty" as
  93. * initialized in drbd_req_new(), so we can list_del() it
  94. * here unconditionally */
  95. list_del_init(&req->tl_requests);
  96. /* if it was a write, we may have to set the corresponding
  97. * bit(s) out-of-sync first. If it had a local part, we need to
  98. * release the reference to the activity log. */
  99. if (s & RQ_WRITE) {
  100. /* Set out-of-sync unless both OK flags are set
  101. * (local only or remote failed).
  102. * Other places where we set out-of-sync:
  103. * READ with local io-error */
  104. /* There is a special case:
  105. * we may notice late that IO was suspended,
  106. * and postpone, or schedule for retry, a write,
  107. * before it even was submitted or sent.
  108. * In that case we do not want to touch the bitmap at all.
  109. */
  110. if ((s & (RQ_POSTPONED|RQ_LOCAL_MASK|RQ_NET_MASK)) != RQ_POSTPONED) {
  111. if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
  112. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  113. if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
  114. drbd_set_in_sync(mdev, req->i.sector, req->i.size);
  115. }
  116. /* one might be tempted to move the drbd_al_complete_io
  117. * to the local io completion callback drbd_request_endio.
  118. * but, if this was a mirror write, we may only
  119. * drbd_al_complete_io after this is RQ_NET_DONE,
  120. * otherwise the extent could be dropped from the al
  121. * before it has actually been written on the peer.
  122. * if we crash before our peer knows about the request,
  123. * but after the extent has been dropped from the al,
  124. * we would forget to resync the corresponding extent.
  125. */
  126. if (s & RQ_IN_ACT_LOG) {
  127. if (get_ldev_if_state(mdev, D_FAILED)) {
  128. drbd_al_complete_io(mdev, &req->i);
  129. put_ldev(mdev);
  130. } else if (__ratelimit(&drbd_ratelimit_state)) {
  131. dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
  132. "but my Disk seems to have failed :(\n",
  133. (unsigned long long) req->i.sector, req->i.size);
  134. }
  135. }
  136. }
  137. mempool_free(req, drbd_request_mempool);
  138. }
  139. static void wake_all_senders(struct drbd_tconn *tconn) {
  140. wake_up(&tconn->sender_work.q_wait);
  141. }
  142. /* must hold resource->req_lock */
  143. void start_new_tl_epoch(struct drbd_tconn *tconn)
  144. {
  145. /* no point closing an epoch, if it is empty, anyways. */
  146. if (tconn->current_tle_writes == 0)
  147. return;
  148. tconn->current_tle_writes = 0;
  149. atomic_inc(&tconn->current_tle_nr);
  150. wake_all_senders(tconn);
  151. }
  152. void complete_master_bio(struct drbd_conf *mdev,
  153. struct bio_and_error *m)
  154. {
  155. bio_endio(m->bio, m->error);
  156. dec_ap_bio(mdev);
  157. }
  158. static void drbd_remove_request_interval(struct rb_root *root,
  159. struct drbd_request *req)
  160. {
  161. struct drbd_conf *mdev = req->w.mdev;
  162. struct drbd_interval *i = &req->i;
  163. drbd_remove_interval(root, i);
  164. /* Wake up any processes waiting for this request to complete. */
  165. if (i->waiting)
  166. wake_up(&mdev->misc_wait);
  167. }
  168. /* Helper for __req_mod().
  169. * Set m->bio to the master bio, if it is fit to be completed,
  170. * or leave it alone (it is initialized to NULL in __req_mod),
  171. * if it has already been completed, or cannot be completed yet.
  172. * If m->bio is set, the error status to be returned is placed in m->error.
  173. */
  174. static
  175. void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
  176. {
  177. const unsigned s = req->rq_state;
  178. struct drbd_conf *mdev = req->w.mdev;
  179. int rw;
  180. int error, ok;
  181. /* we must not complete the master bio, while it is
  182. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  183. * not yet acknowledged by the peer
  184. * not yet completed by the local io subsystem
  185. * these flags may get cleared in any order by
  186. * the worker,
  187. * the receiver,
  188. * the bio_endio completion callbacks.
  189. */
  190. if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
  191. (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
  192. (s & RQ_COMPLETION_SUSP)) {
  193. dev_err(DEV, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
  194. return;
  195. }
  196. if (!req->master_bio) {
  197. dev_err(DEV, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
  198. return;
  199. }
  200. rw = bio_rw(req->master_bio);
  201. /*
  202. * figure out whether to report success or failure.
  203. *
  204. * report success when at least one of the operations succeeded.
  205. * or, to put the other way,
  206. * only report failure, when both operations failed.
  207. *
  208. * what to do about the failures is handled elsewhere.
  209. * what we need to do here is just: complete the master_bio.
  210. *
  211. * local completion error, if any, has been stored as ERR_PTR
  212. * in private_bio within drbd_request_endio.
  213. */
  214. ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
  215. error = PTR_ERR(req->private_bio);
  216. /* remove the request from the conflict detection
  217. * respective block_id verification hash */
  218. if (!drbd_interval_empty(&req->i)) {
  219. struct rb_root *root;
  220. if (rw == WRITE)
  221. root = &mdev->write_requests;
  222. else
  223. root = &mdev->read_requests;
  224. drbd_remove_request_interval(root, req);
  225. }
  226. /* Before we can signal completion to the upper layers,
  227. * we may need to close the current transfer log epoch.
  228. * We are within the request lock, so we can simply compare
  229. * the request epoch number with the current transfer log
  230. * epoch number. If they match, increase the current_tle_nr,
  231. * and reset the transfer log epoch write_cnt.
  232. */
  233. if (rw == WRITE &&
  234. req->epoch == atomic_read(&mdev->tconn->current_tle_nr))
  235. start_new_tl_epoch(mdev->tconn);
  236. /* Update disk stats */
  237. _drbd_end_io_acct(mdev, req);
  238. /* If READ failed,
  239. * have it be pushed back to the retry work queue,
  240. * so it will re-enter __drbd_make_request(),
  241. * and be re-assigned to a suitable local or remote path,
  242. * or failed if we do not have access to good data anymore.
  243. *
  244. * Unless it was failed early by __drbd_make_request(),
  245. * because no path was available, in which case
  246. * it was not even added to the transfer_log.
  247. *
  248. * READA may fail, and will not be retried.
  249. *
  250. * WRITE should have used all available paths already.
  251. */
  252. if (!ok && rw == READ && !list_empty(&req->tl_requests))
  253. req->rq_state |= RQ_POSTPONED;
  254. if (!(req->rq_state & RQ_POSTPONED)) {
  255. m->error = ok ? 0 : (error ?: -EIO);
  256. m->bio = req->master_bio;
  257. req->master_bio = NULL;
  258. }
  259. }
  260. static int drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
  261. {
  262. struct drbd_conf *mdev = req->w.mdev;
  263. D_ASSERT(m || (req->rq_state & RQ_POSTPONED));
  264. if (!atomic_sub_and_test(put, &req->completion_ref))
  265. return 0;
  266. drbd_req_complete(req, m);
  267. if (req->rq_state & RQ_POSTPONED) {
  268. /* don't destroy the req object just yet,
  269. * but queue it for retry */
  270. drbd_restart_request(req);
  271. return 0;
  272. }
  273. return 1;
  274. }
  275. /* I'd like this to be the only place that manipulates
  276. * req->completion_ref and req->kref. */
  277. static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
  278. int clear, int set)
  279. {
  280. struct drbd_conf *mdev = req->w.mdev;
  281. unsigned s = req->rq_state;
  282. int c_put = 0;
  283. int k_put = 0;
  284. if (drbd_suspended(mdev) && !((s | clear) & RQ_COMPLETION_SUSP))
  285. set |= RQ_COMPLETION_SUSP;
  286. /* apply */
  287. req->rq_state &= ~clear;
  288. req->rq_state |= set;
  289. /* no change? */
  290. if (req->rq_state == s)
  291. return;
  292. /* intent: get references */
  293. if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
  294. atomic_inc(&req->completion_ref);
  295. if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
  296. inc_ap_pending(mdev);
  297. atomic_inc(&req->completion_ref);
  298. }
  299. if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED))
  300. atomic_inc(&req->completion_ref);
  301. if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
  302. kref_get(&req->kref); /* wait for the DONE */
  303. if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT))
  304. atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
  305. if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
  306. atomic_inc(&req->completion_ref);
  307. /* progress: put references */
  308. if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
  309. ++c_put;
  310. if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
  311. D_ASSERT(req->rq_state & RQ_LOCAL_PENDING);
  312. /* local completion may still come in later,
  313. * we need to keep the req object around. */
  314. kref_get(&req->kref);
  315. ++c_put;
  316. }
  317. if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
  318. if (req->rq_state & RQ_LOCAL_ABORTED)
  319. ++k_put;
  320. else
  321. ++c_put;
  322. }
  323. if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
  324. dec_ap_pending(mdev);
  325. ++c_put;
  326. }
  327. if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED))
  328. ++c_put;
  329. if ((s & RQ_EXP_BARR_ACK) && !(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
  330. if (req->rq_state & RQ_NET_SENT)
  331. atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
  332. ++k_put;
  333. }
  334. /* potentially complete and destroy */
  335. if (k_put || c_put) {
  336. /* Completion does it's own kref_put. If we are going to
  337. * kref_sub below, we need req to be still around then. */
  338. int at_least = k_put + !!c_put;
  339. int refcount = atomic_read(&req->kref.refcount);
  340. if (refcount < at_least)
  341. dev_err(DEV,
  342. "mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
  343. s, req->rq_state, refcount, at_least);
  344. }
  345. /* If we made progress, retry conflicting peer requests, if any. */
  346. if (req->i.waiting)
  347. wake_up(&mdev->misc_wait);
  348. if (c_put)
  349. k_put += drbd_req_put_completion_ref(req, m, c_put);
  350. if (k_put)
  351. kref_sub(&req->kref, k_put, drbd_req_destroy);
  352. }
  353. static void drbd_report_io_error(struct drbd_conf *mdev, struct drbd_request *req)
  354. {
  355. char b[BDEVNAME_SIZE];
  356. if (!__ratelimit(&drbd_ratelimit_state))
  357. return;
  358. dev_warn(DEV, "local %s IO error sector %llu+%u on %s\n",
  359. (req->rq_state & RQ_WRITE) ? "WRITE" : "READ",
  360. (unsigned long long)req->i.sector,
  361. req->i.size >> 9,
  362. bdevname(mdev->ldev->backing_bdev, b));
  363. }
  364. /* obviously this could be coded as many single functions
  365. * instead of one huge switch,
  366. * or by putting the code directly in the respective locations
  367. * (as it has been before).
  368. *
  369. * but having it this way
  370. * enforces that it is all in this one place, where it is easier to audit,
  371. * it makes it obvious that whatever "event" "happens" to a request should
  372. * happen "atomically" within the req_lock,
  373. * and it enforces that we have to think in a very structured manner
  374. * about the "events" that may happen to a request during its life time ...
  375. */
  376. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  377. struct bio_and_error *m)
  378. {
  379. struct drbd_conf *mdev = req->w.mdev;
  380. struct net_conf *nc;
  381. int p, rv = 0;
  382. if (m)
  383. m->bio = NULL;
  384. switch (what) {
  385. default:
  386. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  387. break;
  388. /* does not happen...
  389. * initialization done in drbd_req_new
  390. case CREATED:
  391. break;
  392. */
  393. case TO_BE_SENT: /* via network */
  394. /* reached via __drbd_make_request
  395. * and from w_read_retry_remote */
  396. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  397. rcu_read_lock();
  398. nc = rcu_dereference(mdev->tconn->net_conf);
  399. p = nc->wire_protocol;
  400. rcu_read_unlock();
  401. req->rq_state |=
  402. p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
  403. p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
  404. mod_rq_state(req, m, 0, RQ_NET_PENDING);
  405. break;
  406. case TO_BE_SUBMITTED: /* locally */
  407. /* reached via __drbd_make_request */
  408. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  409. mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
  410. break;
  411. case COMPLETED_OK:
  412. if (req->rq_state & RQ_WRITE)
  413. mdev->writ_cnt += req->i.size >> 9;
  414. else
  415. mdev->read_cnt += req->i.size >> 9;
  416. mod_rq_state(req, m, RQ_LOCAL_PENDING,
  417. RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  418. break;
  419. case ABORT_DISK_IO:
  420. mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
  421. break;
  422. case WRITE_COMPLETED_WITH_ERROR:
  423. drbd_report_io_error(mdev, req);
  424. __drbd_chk_io_error(mdev, DRBD_WRITE_ERROR);
  425. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  426. break;
  427. case READ_COMPLETED_WITH_ERROR:
  428. drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
  429. drbd_report_io_error(mdev, req);
  430. __drbd_chk_io_error(mdev, DRBD_READ_ERROR);
  431. /* fall through. */
  432. case READ_AHEAD_COMPLETED_WITH_ERROR:
  433. /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
  434. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  435. break;
  436. case QUEUE_FOR_NET_READ:
  437. /* READ or READA, and
  438. * no local disk,
  439. * or target area marked as invalid,
  440. * or just got an io-error. */
  441. /* from __drbd_make_request
  442. * or from bio_endio during read io-error recovery */
  443. /* So we can verify the handle in the answer packet.
  444. * Corresponding drbd_remove_request_interval is in
  445. * drbd_req_complete() */
  446. D_ASSERT(drbd_interval_empty(&req->i));
  447. drbd_insert_interval(&mdev->read_requests, &req->i);
  448. set_bit(UNPLUG_REMOTE, &mdev->flags);
  449. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  450. D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
  451. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  452. req->w.cb = w_send_read_req;
  453. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  454. break;
  455. case QUEUE_FOR_NET_WRITE:
  456. /* assert something? */
  457. /* from __drbd_make_request only */
  458. /* Corresponding drbd_remove_request_interval is in
  459. * drbd_req_complete() */
  460. D_ASSERT(drbd_interval_empty(&req->i));
  461. drbd_insert_interval(&mdev->write_requests, &req->i);
  462. /* NOTE
  463. * In case the req ended up on the transfer log before being
  464. * queued on the worker, it could lead to this request being
  465. * missed during cleanup after connection loss.
  466. * So we have to do both operations here,
  467. * within the same lock that protects the transfer log.
  468. *
  469. * _req_add_to_epoch(req); this has to be after the
  470. * _maybe_start_new_epoch(req); which happened in
  471. * __drbd_make_request, because we now may set the bit
  472. * again ourselves to close the current epoch.
  473. *
  474. * Add req to the (now) current epoch (barrier). */
  475. /* otherwise we may lose an unplug, which may cause some remote
  476. * io-scheduler timeout to expire, increasing maximum latency,
  477. * hurting performance. */
  478. set_bit(UNPLUG_REMOTE, &mdev->flags);
  479. /* queue work item to send data */
  480. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  481. mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
  482. req->w.cb = w_send_dblock;
  483. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  484. /* close the epoch, in case it outgrew the limit */
  485. rcu_read_lock();
  486. nc = rcu_dereference(mdev->tconn->net_conf);
  487. p = nc->max_epoch_size;
  488. rcu_read_unlock();
  489. if (mdev->tconn->current_tle_writes >= p)
  490. start_new_tl_epoch(mdev->tconn);
  491. break;
  492. case QUEUE_FOR_SEND_OOS:
  493. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  494. req->w.cb = w_send_out_of_sync;
  495. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  496. break;
  497. case READ_RETRY_REMOTE_CANCELED:
  498. case SEND_CANCELED:
  499. case SEND_FAILED:
  500. /* real cleanup will be done from tl_clear. just update flags
  501. * so it is no longer marked as on the worker queue */
  502. mod_rq_state(req, m, RQ_NET_QUEUED, 0);
  503. break;
  504. case HANDED_OVER_TO_NETWORK:
  505. /* assert something? */
  506. if (bio_data_dir(req->master_bio) == WRITE &&
  507. !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
  508. /* this is what is dangerous about protocol A:
  509. * pretend it was successfully written on the peer. */
  510. if (req->rq_state & RQ_NET_PENDING)
  511. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  512. /* else: neg-ack was faster... */
  513. /* it is still not yet RQ_NET_DONE until the
  514. * corresponding epoch barrier got acked as well,
  515. * so we know what to dirty on connection loss */
  516. }
  517. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
  518. break;
  519. case OOS_HANDED_TO_NETWORK:
  520. /* Was not set PENDING, no longer QUEUED, so is now DONE
  521. * as far as this connection is concerned. */
  522. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
  523. break;
  524. case CONNECTION_LOST_WHILE_PENDING:
  525. /* transfer log cleanup after connection loss */
  526. mod_rq_state(req, m,
  527. RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
  528. RQ_NET_DONE);
  529. break;
  530. case CONFLICT_RESOLVED:
  531. /* for superseded conflicting writes of multiple primaries,
  532. * there is no need to keep anything in the tl, potential
  533. * node crashes are covered by the activity log.
  534. *
  535. * If this request had been marked as RQ_POSTPONED before,
  536. * it will actually not be completed, but "restarted",
  537. * resubmitted from the retry worker context. */
  538. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  539. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  540. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
  541. break;
  542. case WRITE_ACKED_BY_PEER_AND_SIS:
  543. req->rq_state |= RQ_NET_SIS;
  544. case WRITE_ACKED_BY_PEER:
  545. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  546. /* protocol C; successfully written on peer.
  547. * Nothing more to do here.
  548. * We want to keep the tl in place for all protocols, to cater
  549. * for volatile write-back caches on lower level devices. */
  550. goto ack_common;
  551. case RECV_ACKED_BY_PEER:
  552. D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
  553. /* protocol B; pretends to be successfully written on peer.
  554. * see also notes above in HANDED_OVER_TO_NETWORK about
  555. * protocol != C */
  556. ack_common:
  557. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  558. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  559. break;
  560. case POSTPONE_WRITE:
  561. D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
  562. /* If this node has already detected the write conflict, the
  563. * worker will be waiting on misc_wait. Wake it up once this
  564. * request has completed locally.
  565. */
  566. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  567. req->rq_state |= RQ_POSTPONED;
  568. if (req->i.waiting)
  569. wake_up(&mdev->misc_wait);
  570. /* Do not clear RQ_NET_PENDING. This request will make further
  571. * progress via restart_conflicting_writes() or
  572. * fail_postponed_requests(). Hopefully. */
  573. break;
  574. case NEG_ACKED:
  575. mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
  576. break;
  577. case FAIL_FROZEN_DISK_IO:
  578. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  579. break;
  580. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  581. break;
  582. case RESTART_FROZEN_DISK_IO:
  583. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  584. break;
  585. mod_rq_state(req, m,
  586. RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
  587. RQ_LOCAL_PENDING);
  588. rv = MR_READ;
  589. if (bio_data_dir(req->master_bio) == WRITE)
  590. rv = MR_WRITE;
  591. get_ldev(mdev); /* always succeeds in this call path */
  592. req->w.cb = w_restart_disk_io;
  593. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  594. break;
  595. case RESEND:
  596. /* Simply complete (local only) READs. */
  597. if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
  598. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  599. break;
  600. }
  601. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  602. before the connection loss (B&C only); only P_BARRIER_ACK
  603. (or the local completion?) was missing when we suspended.
  604. Throwing them out of the TL here by pretending we got a BARRIER_ACK.
  605. During connection handshake, we ensure that the peer was not rebooted. */
  606. if (!(req->rq_state & RQ_NET_OK)) {
  607. /* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
  608. * in that case we must not set RQ_NET_PENDING. */
  609. mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
  610. if (req->w.cb) {
  611. drbd_queue_work(&mdev->tconn->sender_work, &req->w);
  612. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  613. } /* else: FIXME can this happen? */
  614. break;
  615. }
  616. /* else, fall through to BARRIER_ACKED */
  617. case BARRIER_ACKED:
  618. /* barrier ack for READ requests does not make sense */
  619. if (!(req->rq_state & RQ_WRITE))
  620. break;
  621. if (req->rq_state & RQ_NET_PENDING) {
  622. /* barrier came in before all requests were acked.
  623. * this is bad, because if the connection is lost now,
  624. * we won't be able to clean them up... */
  625. dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
  626. }
  627. /* Allowed to complete requests, even while suspended.
  628. * As this is called for all requests within a matching epoch,
  629. * we need to filter, and only set RQ_NET_DONE for those that
  630. * have actually been on the wire. */
  631. mod_rq_state(req, m, RQ_COMPLETION_SUSP,
  632. (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
  633. break;
  634. case DATA_RECEIVED:
  635. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  636. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
  637. break;
  638. case QUEUE_AS_DRBD_BARRIER:
  639. start_new_tl_epoch(mdev->tconn);
  640. mod_rq_state(req, m, 0, RQ_NET_OK|RQ_NET_DONE);
  641. break;
  642. };
  643. return rv;
  644. }
  645. /* we may do a local read if:
  646. * - we are consistent (of course),
  647. * - or we are generally inconsistent,
  648. * BUT we are still/already IN SYNC for this area.
  649. * since size may be bigger than BM_BLOCK_SIZE,
  650. * we may need to check several bits.
  651. */
  652. static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  653. {
  654. unsigned long sbnr, ebnr;
  655. sector_t esector, nr_sectors;
  656. if (mdev->state.disk == D_UP_TO_DATE)
  657. return true;
  658. if (mdev->state.disk != D_INCONSISTENT)
  659. return false;
  660. esector = sector + (size >> 9) - 1;
  661. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  662. D_ASSERT(sector < nr_sectors);
  663. D_ASSERT(esector < nr_sectors);
  664. sbnr = BM_SECT_TO_BIT(sector);
  665. ebnr = BM_SECT_TO_BIT(esector);
  666. return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
  667. }
  668. static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
  669. enum drbd_read_balancing rbm)
  670. {
  671. struct backing_dev_info *bdi;
  672. int stripe_shift;
  673. switch (rbm) {
  674. case RB_CONGESTED_REMOTE:
  675. bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
  676. return bdi_read_congested(bdi);
  677. case RB_LEAST_PENDING:
  678. return atomic_read(&mdev->local_cnt) >
  679. atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
  680. case RB_32K_STRIPING: /* stripe_shift = 15 */
  681. case RB_64K_STRIPING:
  682. case RB_128K_STRIPING:
  683. case RB_256K_STRIPING:
  684. case RB_512K_STRIPING:
  685. case RB_1M_STRIPING: /* stripe_shift = 20 */
  686. stripe_shift = (rbm - RB_32K_STRIPING + 15);
  687. return (sector >> (stripe_shift - 9)) & 1;
  688. case RB_ROUND_ROBIN:
  689. return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
  690. case RB_PREFER_REMOTE:
  691. return true;
  692. case RB_PREFER_LOCAL:
  693. default:
  694. return false;
  695. }
  696. }
  697. /*
  698. * complete_conflicting_writes - wait for any conflicting write requests
  699. *
  700. * The write_requests tree contains all active write requests which we
  701. * currently know about. Wait for any requests to complete which conflict with
  702. * the new one.
  703. *
  704. * Only way out: remove the conflicting intervals from the tree.
  705. */
  706. static void complete_conflicting_writes(struct drbd_request *req)
  707. {
  708. DEFINE_WAIT(wait);
  709. struct drbd_conf *mdev = req->w.mdev;
  710. struct drbd_interval *i;
  711. sector_t sector = req->i.sector;
  712. int size = req->i.size;
  713. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  714. if (!i)
  715. return;
  716. for (;;) {
  717. prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
  718. i = drbd_find_overlap(&mdev->write_requests, sector, size);
  719. if (!i)
  720. break;
  721. /* Indicate to wake up device->misc_wait on progress. */
  722. i->waiting = true;
  723. spin_unlock_irq(&mdev->tconn->req_lock);
  724. schedule();
  725. spin_lock_irq(&mdev->tconn->req_lock);
  726. }
  727. finish_wait(&mdev->misc_wait, &wait);
  728. }
  729. /* called within req_lock and rcu_read_lock() */
  730. static void maybe_pull_ahead(struct drbd_conf *mdev)
  731. {
  732. struct drbd_tconn *tconn = mdev->tconn;
  733. struct net_conf *nc;
  734. bool congested = false;
  735. enum drbd_on_congestion on_congestion;
  736. rcu_read_lock();
  737. nc = rcu_dereference(tconn->net_conf);
  738. on_congestion = nc ? nc->on_congestion : OC_BLOCK;
  739. rcu_read_unlock();
  740. if (on_congestion == OC_BLOCK ||
  741. tconn->agreed_pro_version < 96)
  742. return;
  743. /* If I don't even have good local storage, we can not reasonably try
  744. * to pull ahead of the peer. We also need the local reference to make
  745. * sure mdev->act_log is there.
  746. */
  747. if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
  748. return;
  749. if (nc->cong_fill &&
  750. atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
  751. dev_info(DEV, "Congestion-fill threshold reached\n");
  752. congested = true;
  753. }
  754. if (mdev->act_log->used >= nc->cong_extents) {
  755. dev_info(DEV, "Congestion-extents threshold reached\n");
  756. congested = true;
  757. }
  758. if (congested) {
  759. /* start a new epoch for non-mirrored writes */
  760. start_new_tl_epoch(mdev->tconn);
  761. if (on_congestion == OC_PULL_AHEAD)
  762. _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
  763. else /*nc->on_congestion == OC_DISCONNECT */
  764. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
  765. }
  766. put_ldev(mdev);
  767. }
  768. /* If this returns false, and req->private_bio is still set,
  769. * this should be submitted locally.
  770. *
  771. * If it returns false, but req->private_bio is not set,
  772. * we do not have access to good data :(
  773. *
  774. * Otherwise, this destroys req->private_bio, if any,
  775. * and returns true.
  776. */
  777. static bool do_remote_read(struct drbd_request *req)
  778. {
  779. struct drbd_conf *mdev = req->w.mdev;
  780. enum drbd_read_balancing rbm;
  781. if (req->private_bio) {
  782. if (!drbd_may_do_local_read(mdev,
  783. req->i.sector, req->i.size)) {
  784. bio_put(req->private_bio);
  785. req->private_bio = NULL;
  786. put_ldev(mdev);
  787. }
  788. }
  789. if (mdev->state.pdsk != D_UP_TO_DATE)
  790. return false;
  791. if (req->private_bio == NULL)
  792. return true;
  793. /* TODO: improve read balancing decisions, take into account drbd
  794. * protocol, pending requests etc. */
  795. rcu_read_lock();
  796. rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
  797. rcu_read_unlock();
  798. if (rbm == RB_PREFER_LOCAL && req->private_bio)
  799. return false; /* submit locally */
  800. if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
  801. if (req->private_bio) {
  802. bio_put(req->private_bio);
  803. req->private_bio = NULL;
  804. put_ldev(mdev);
  805. }
  806. return true;
  807. }
  808. return false;
  809. }
  810. /* returns number of connections (== 1, for drbd 8.4)
  811. * expected to actually write this data,
  812. * which does NOT include those that we are L_AHEAD for. */
  813. static int drbd_process_write_request(struct drbd_request *req)
  814. {
  815. struct drbd_conf *mdev = req->w.mdev;
  816. int remote, send_oos;
  817. remote = drbd_should_do_remote(mdev->state);
  818. send_oos = drbd_should_send_out_of_sync(mdev->state);
  819. /* Need to replicate writes. Unless it is an empty flush,
  820. * which is better mapped to a DRBD P_BARRIER packet,
  821. * also for drbd wire protocol compatibility reasons.
  822. * If this was a flush, just start a new epoch.
  823. * Unless the current epoch was empty anyways, or we are not currently
  824. * replicating, in which case there is no point. */
  825. if (unlikely(req->i.size == 0)) {
  826. /* The only size==0 bios we expect are empty flushes. */
  827. D_ASSERT(req->master_bio->bi_rw & REQ_FLUSH);
  828. if (remote)
  829. _req_mod(req, QUEUE_AS_DRBD_BARRIER);
  830. return remote;
  831. }
  832. if (!remote && !send_oos)
  833. return 0;
  834. D_ASSERT(!(remote && send_oos));
  835. if (remote) {
  836. _req_mod(req, TO_BE_SENT);
  837. _req_mod(req, QUEUE_FOR_NET_WRITE);
  838. } else if (drbd_set_out_of_sync(mdev, req->i.sector, req->i.size))
  839. _req_mod(req, QUEUE_FOR_SEND_OOS);
  840. return remote;
  841. }
  842. static void
  843. drbd_submit_req_private_bio(struct drbd_request *req)
  844. {
  845. struct drbd_conf *mdev = req->w.mdev;
  846. struct bio *bio = req->private_bio;
  847. const int rw = bio_rw(bio);
  848. bio->bi_bdev = mdev->ldev->backing_bdev;
  849. /* State may have changed since we grabbed our reference on the
  850. * ->ldev member. Double check, and short-circuit to endio.
  851. * In case the last activity log transaction failed to get on
  852. * stable storage, and this is a WRITE, we may not even submit
  853. * this bio. */
  854. if (get_ldev(mdev)) {
  855. if (drbd_insert_fault(mdev,
  856. rw == WRITE ? DRBD_FAULT_DT_WR
  857. : rw == READ ? DRBD_FAULT_DT_RD
  858. : DRBD_FAULT_DT_RA))
  859. bio_endio(bio, -EIO);
  860. else
  861. generic_make_request(bio);
  862. put_ldev(mdev);
  863. } else
  864. bio_endio(bio, -EIO);
  865. }
  866. static void drbd_queue_write(struct drbd_conf *mdev, struct drbd_request *req)
  867. {
  868. spin_lock(&mdev->submit.lock);
  869. list_add_tail(&req->tl_requests, &mdev->submit.writes);
  870. spin_unlock(&mdev->submit.lock);
  871. queue_work(mdev->submit.wq, &mdev->submit.worker);
  872. }
  873. /* returns the new drbd_request pointer, if the caller is expected to
  874. * drbd_send_and_submit() it (to save latency), or NULL if we queued the
  875. * request on the submitter thread.
  876. * Returns ERR_PTR(-ENOMEM) if we cannot allocate a drbd_request.
  877. */
  878. struct drbd_request *
  879. drbd_request_prepare(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  880. {
  881. const int rw = bio_data_dir(bio);
  882. struct drbd_request *req;
  883. /* allocate outside of all locks; */
  884. req = drbd_req_new(mdev, bio);
  885. if (!req) {
  886. dec_ap_bio(mdev);
  887. /* only pass the error to the upper layers.
  888. * if user cannot handle io errors, that's not our business. */
  889. dev_err(DEV, "could not kmalloc() req\n");
  890. bio_endio(bio, -ENOMEM);
  891. return ERR_PTR(-ENOMEM);
  892. }
  893. req->start_time = start_time;
  894. if (!get_ldev(mdev)) {
  895. bio_put(req->private_bio);
  896. req->private_bio = NULL;
  897. }
  898. /* Update disk stats */
  899. _drbd_start_io_acct(mdev, req);
  900. if (rw == WRITE && req->private_bio && req->i.size
  901. && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  902. if (!drbd_al_begin_io_fastpath(mdev, &req->i)) {
  903. drbd_queue_write(mdev, req);
  904. return NULL;
  905. }
  906. req->rq_state |= RQ_IN_ACT_LOG;
  907. }
  908. return req;
  909. }
  910. static void drbd_send_and_submit(struct drbd_conf *mdev, struct drbd_request *req)
  911. {
  912. const int rw = bio_rw(req->master_bio);
  913. struct bio_and_error m = { NULL, };
  914. bool no_remote = false;
  915. spin_lock_irq(&mdev->tconn->req_lock);
  916. if (rw == WRITE) {
  917. /* This may temporarily give up the req_lock,
  918. * but will re-aquire it before it returns here.
  919. * Needs to be before the check on drbd_suspended() */
  920. complete_conflicting_writes(req);
  921. /* no more giving up req_lock from now on! */
  922. /* check for congestion, and potentially stop sending
  923. * full data updates, but start sending "dirty bits" only. */
  924. maybe_pull_ahead(mdev);
  925. }
  926. if (drbd_suspended(mdev)) {
  927. /* push back and retry: */
  928. req->rq_state |= RQ_POSTPONED;
  929. if (req->private_bio) {
  930. bio_put(req->private_bio);
  931. req->private_bio = NULL;
  932. put_ldev(mdev);
  933. }
  934. goto out;
  935. }
  936. /* We fail READ/READA early, if we can not serve it.
  937. * We must do this before req is registered on any lists.
  938. * Otherwise, drbd_req_complete() will queue failed READ for retry. */
  939. if (rw != WRITE) {
  940. if (!do_remote_read(req) && !req->private_bio)
  941. goto nodata;
  942. }
  943. /* which transfer log epoch does this belong to? */
  944. req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
  945. /* no point in adding empty flushes to the transfer log,
  946. * they are mapped to drbd barriers already. */
  947. if (likely(req->i.size!=0)) {
  948. if (rw == WRITE)
  949. mdev->tconn->current_tle_writes++;
  950. list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
  951. }
  952. if (rw == WRITE) {
  953. if (!drbd_process_write_request(req))
  954. no_remote = true;
  955. } else {
  956. /* We either have a private_bio, or we can read from remote.
  957. * Otherwise we had done the goto nodata above. */
  958. if (req->private_bio == NULL) {
  959. _req_mod(req, TO_BE_SENT);
  960. _req_mod(req, QUEUE_FOR_NET_READ);
  961. } else
  962. no_remote = true;
  963. }
  964. if (req->private_bio) {
  965. /* needs to be marked within the same spinlock */
  966. _req_mod(req, TO_BE_SUBMITTED);
  967. /* but we need to give up the spinlock to submit */
  968. spin_unlock_irq(&mdev->tconn->req_lock);
  969. drbd_submit_req_private_bio(req);
  970. spin_lock_irq(&mdev->tconn->req_lock);
  971. } else if (no_remote) {
  972. nodata:
  973. if (__ratelimit(&drbd_ratelimit_state))
  974. dev_err(DEV, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
  975. (unsigned long long)req->i.sector, req->i.size >> 9);
  976. /* A write may have been queued for send_oos, however.
  977. * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
  978. }
  979. out:
  980. if (drbd_req_put_completion_ref(req, &m, 1))
  981. kref_put(&req->kref, drbd_req_destroy);
  982. spin_unlock_irq(&mdev->tconn->req_lock);
  983. if (m.bio)
  984. complete_master_bio(mdev, &m);
  985. }
  986. void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
  987. {
  988. struct drbd_request *req = drbd_request_prepare(mdev, bio, start_time);
  989. if (IS_ERR_OR_NULL(req))
  990. return;
  991. drbd_send_and_submit(mdev, req);
  992. }
  993. static void submit_fast_path(struct drbd_conf *mdev, struct list_head *incoming)
  994. {
  995. struct drbd_request *req, *tmp;
  996. list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
  997. const int rw = bio_data_dir(req->master_bio);
  998. if (rw == WRITE /* rw != WRITE should not even end up here! */
  999. && req->private_bio && req->i.size
  1000. && !test_bit(AL_SUSPENDED, &mdev->flags)) {
  1001. if (!drbd_al_begin_io_fastpath(mdev, &req->i))
  1002. continue;
  1003. req->rq_state |= RQ_IN_ACT_LOG;
  1004. }
  1005. list_del_init(&req->tl_requests);
  1006. drbd_send_and_submit(mdev, req);
  1007. }
  1008. }
  1009. static bool prepare_al_transaction_nonblock(struct drbd_conf *mdev,
  1010. struct list_head *incoming,
  1011. struct list_head *pending)
  1012. {
  1013. struct drbd_request *req, *tmp;
  1014. int wake = 0;
  1015. int err;
  1016. spin_lock_irq(&mdev->al_lock);
  1017. list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
  1018. err = drbd_al_begin_io_nonblock(mdev, &req->i);
  1019. if (err == -EBUSY)
  1020. wake = 1;
  1021. if (err)
  1022. continue;
  1023. req->rq_state |= RQ_IN_ACT_LOG;
  1024. list_move_tail(&req->tl_requests, pending);
  1025. }
  1026. spin_unlock_irq(&mdev->al_lock);
  1027. if (wake)
  1028. wake_up(&mdev->al_wait);
  1029. return !list_empty(pending);
  1030. }
  1031. void do_submit(struct work_struct *ws)
  1032. {
  1033. struct drbd_conf *mdev = container_of(ws, struct drbd_conf, submit.worker);
  1034. LIST_HEAD(incoming);
  1035. LIST_HEAD(pending);
  1036. struct drbd_request *req, *tmp;
  1037. for (;;) {
  1038. spin_lock(&mdev->submit.lock);
  1039. list_splice_tail_init(&mdev->submit.writes, &incoming);
  1040. spin_unlock(&mdev->submit.lock);
  1041. submit_fast_path(mdev, &incoming);
  1042. if (list_empty(&incoming))
  1043. break;
  1044. wait_event(mdev->al_wait, prepare_al_transaction_nonblock(mdev, &incoming, &pending));
  1045. /* Maybe more was queued, while we prepared the transaction?
  1046. * Try to stuff them into this transaction as well.
  1047. * Be strictly non-blocking here, no wait_event, we already
  1048. * have something to commit.
  1049. * Stop if we don't make any more progres.
  1050. */
  1051. for (;;) {
  1052. LIST_HEAD(more_pending);
  1053. LIST_HEAD(more_incoming);
  1054. bool made_progress;
  1055. /* It is ok to look outside the lock,
  1056. * it's only an optimization anyways */
  1057. if (list_empty(&mdev->submit.writes))
  1058. break;
  1059. spin_lock(&mdev->submit.lock);
  1060. list_splice_tail_init(&mdev->submit.writes, &more_incoming);
  1061. spin_unlock(&mdev->submit.lock);
  1062. if (list_empty(&more_incoming))
  1063. break;
  1064. made_progress = prepare_al_transaction_nonblock(mdev, &more_incoming, &more_pending);
  1065. list_splice_tail_init(&more_pending, &pending);
  1066. list_splice_tail_init(&more_incoming, &incoming);
  1067. if (!made_progress)
  1068. break;
  1069. }
  1070. drbd_al_begin_io_commit(mdev, false);
  1071. list_for_each_entry_safe(req, tmp, &pending, tl_requests) {
  1072. list_del_init(&req->tl_requests);
  1073. drbd_send_and_submit(mdev, req);
  1074. }
  1075. }
  1076. }
  1077. void drbd_make_request(struct request_queue *q, struct bio *bio)
  1078. {
  1079. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  1080. unsigned long start_time;
  1081. start_time = jiffies;
  1082. /*
  1083. * what we "blindly" assume:
  1084. */
  1085. D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
  1086. inc_ap_bio(mdev);
  1087. __drbd_make_request(mdev, bio, start_time);
  1088. }
  1089. /* This is called by bio_add_page().
  1090. *
  1091. * q->max_hw_sectors and other global limits are already enforced there.
  1092. *
  1093. * We need to call down to our lower level device,
  1094. * in case it has special restrictions.
  1095. *
  1096. * We also may need to enforce configured max-bio-bvecs limits.
  1097. *
  1098. * As long as the BIO is empty we have to allow at least one bvec,
  1099. * regardless of size and offset, so no need to ask lower levels.
  1100. */
  1101. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  1102. {
  1103. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  1104. unsigned int bio_size = bvm->bi_size;
  1105. int limit = DRBD_MAX_BIO_SIZE;
  1106. int backing_limit;
  1107. if (bio_size && get_ldev(mdev)) {
  1108. unsigned int max_hw_sectors = queue_max_hw_sectors(q);
  1109. struct request_queue * const b =
  1110. mdev->ldev->backing_bdev->bd_disk->queue;
  1111. if (b->merge_bvec_fn) {
  1112. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  1113. limit = min(limit, backing_limit);
  1114. }
  1115. put_ldev(mdev);
  1116. if ((limit >> 9) > max_hw_sectors)
  1117. limit = max_hw_sectors << 9;
  1118. }
  1119. return limit;
  1120. }
  1121. struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
  1122. {
  1123. /* Walk the transfer log,
  1124. * and find the oldest not yet completed request */
  1125. struct drbd_request *r;
  1126. list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
  1127. if (atomic_read(&r->completion_ref))
  1128. return r;
  1129. }
  1130. return NULL;
  1131. }
  1132. void request_timer_fn(unsigned long data)
  1133. {
  1134. struct drbd_conf *mdev = (struct drbd_conf *) data;
  1135. struct drbd_tconn *tconn = mdev->tconn;
  1136. struct drbd_request *req; /* oldest request */
  1137. struct net_conf *nc;
  1138. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  1139. unsigned long now;
  1140. rcu_read_lock();
  1141. nc = rcu_dereference(tconn->net_conf);
  1142. if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
  1143. ent = nc->timeout * HZ/10 * nc->ko_count;
  1144. if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
  1145. dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
  1146. put_ldev(mdev);
  1147. }
  1148. rcu_read_unlock();
  1149. et = min_not_zero(dt, ent);
  1150. if (!et)
  1151. return; /* Recurring timer stopped */
  1152. now = jiffies;
  1153. spin_lock_irq(&tconn->req_lock);
  1154. req = find_oldest_request(tconn);
  1155. if (!req) {
  1156. spin_unlock_irq(&tconn->req_lock);
  1157. mod_timer(&mdev->request_timer, now + et);
  1158. return;
  1159. }
  1160. /* The request is considered timed out, if
  1161. * - we have some effective timeout from the configuration,
  1162. * with above state restrictions applied,
  1163. * - the oldest request is waiting for a response from the network
  1164. * resp. the local disk,
  1165. * - the oldest request is in fact older than the effective timeout,
  1166. * - the connection was established (resp. disk was attached)
  1167. * for longer than the timeout already.
  1168. * Note that for 32bit jiffies and very stable connections/disks,
  1169. * we may have a wrap around, which is catched by
  1170. * !time_in_range(now, last_..._jif, last_..._jif + timeout).
  1171. *
  1172. * Side effect: once per 32bit wrap-around interval, which means every
  1173. * ~198 days with 250 HZ, we have a window where the timeout would need
  1174. * to expire twice (worst case) to become effective. Good enough.
  1175. */
  1176. if (ent && req->rq_state & RQ_NET_PENDING &&
  1177. time_after(now, req->start_time + ent) &&
  1178. !time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
  1179. dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
  1180. _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
  1181. }
  1182. if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
  1183. time_after(now, req->start_time + dt) &&
  1184. !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
  1185. dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
  1186. __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
  1187. }
  1188. nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
  1189. spin_unlock_irq(&tconn->req_lock);
  1190. mod_timer(&mdev->request_timer, nt);
  1191. }