drbd_req.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. drbd_req.c
  4. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  5. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  6. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  7. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/drbd.h>
  12. #include "drbd_int.h"
  13. #include "drbd_req.h"
  14. static bool drbd_may_do_local_read(struct drbd_device *device, sector_t sector, int size);
  15. static struct drbd_request *drbd_req_new(struct drbd_device *device, struct bio *bio_src)
  16. {
  17. struct drbd_request *req;
  18. req = mempool_alloc(&drbd_request_mempool, GFP_NOIO);
  19. if (!req)
  20. return NULL;
  21. memset(req, 0, sizeof(*req));
  22. req->rq_state = (bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0)
  23. | (bio_op(bio_src) == REQ_OP_WRITE_ZEROES ? RQ_ZEROES : 0)
  24. | (bio_op(bio_src) == REQ_OP_DISCARD ? RQ_UNMAP : 0);
  25. req->device = device;
  26. req->master_bio = bio_src;
  27. req->epoch = 0;
  28. drbd_clear_interval(&req->i);
  29. req->i.sector = bio_src->bi_iter.bi_sector;
  30. req->i.size = bio_src->bi_iter.bi_size;
  31. req->i.local = true;
  32. req->i.waiting = false;
  33. INIT_LIST_HEAD(&req->tl_requests);
  34. INIT_LIST_HEAD(&req->w.list);
  35. INIT_LIST_HEAD(&req->req_pending_master_completion);
  36. INIT_LIST_HEAD(&req->req_pending_local);
  37. /* one reference to be put by __drbd_make_request */
  38. atomic_set(&req->completion_ref, 1);
  39. /* one kref as long as completion_ref > 0 */
  40. kref_init(&req->kref);
  41. return req;
  42. }
  43. static void drbd_remove_request_interval(struct rb_root *root,
  44. struct drbd_request *req)
  45. {
  46. struct drbd_device *device = req->device;
  47. struct drbd_interval *i = &req->i;
  48. drbd_remove_interval(root, i);
  49. /* Wake up any processes waiting for this request to complete. */
  50. if (i->waiting)
  51. wake_up(&device->misc_wait);
  52. }
  53. void drbd_req_destroy(struct kref *kref)
  54. {
  55. struct drbd_request *req = container_of(kref, struct drbd_request, kref);
  56. struct drbd_device *device = req->device;
  57. const unsigned s = req->rq_state;
  58. if ((req->master_bio && !(s & RQ_POSTPONED)) ||
  59. atomic_read(&req->completion_ref) ||
  60. (s & RQ_LOCAL_PENDING) ||
  61. ((s & RQ_NET_MASK) && !(s & RQ_NET_DONE))) {
  62. drbd_err(device, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
  63. s, atomic_read(&req->completion_ref));
  64. return;
  65. }
  66. /* If called from mod_rq_state (expected normal case) or
  67. * drbd_send_and_submit (the less likely normal path), this holds the
  68. * req_lock, and req->tl_requests will typicaly be on ->transfer_log,
  69. * though it may be still empty (never added to the transfer log).
  70. *
  71. * If called from do_retry(), we do NOT hold the req_lock, but we are
  72. * still allowed to unconditionally list_del(&req->tl_requests),
  73. * because it will be on a local on-stack list only. */
  74. list_del_init(&req->tl_requests);
  75. /* finally remove the request from the conflict detection
  76. * respective block_id verification interval tree. */
  77. if (!drbd_interval_empty(&req->i)) {
  78. struct rb_root *root;
  79. if (s & RQ_WRITE)
  80. root = &device->write_requests;
  81. else
  82. root = &device->read_requests;
  83. drbd_remove_request_interval(root, req);
  84. } else if (s & (RQ_NET_MASK & ~RQ_NET_DONE) && req->i.size != 0)
  85. drbd_err(device, "drbd_req_destroy: Logic BUG: interval empty, but: rq_state=0x%x, sect=%llu, size=%u\n",
  86. s, (unsigned long long)req->i.sector, req->i.size);
  87. /* if it was a write, we may have to set the corresponding
  88. * bit(s) out-of-sync first. If it had a local part, we need to
  89. * release the reference to the activity log. */
  90. if (s & RQ_WRITE) {
  91. /* Set out-of-sync unless both OK flags are set
  92. * (local only or remote failed).
  93. * Other places where we set out-of-sync:
  94. * READ with local io-error */
  95. /* There is a special case:
  96. * we may notice late that IO was suspended,
  97. * and postpone, or schedule for retry, a write,
  98. * before it even was submitted or sent.
  99. * In that case we do not want to touch the bitmap at all.
  100. */
  101. struct drbd_peer_device *peer_device = first_peer_device(device);
  102. if ((s & (RQ_POSTPONED|RQ_LOCAL_MASK|RQ_NET_MASK)) != RQ_POSTPONED) {
  103. if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
  104. drbd_set_out_of_sync(peer_device, req->i.sector, req->i.size);
  105. if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
  106. drbd_set_in_sync(peer_device, req->i.sector, req->i.size);
  107. }
  108. /* one might be tempted to move the drbd_al_complete_io
  109. * to the local io completion callback drbd_request_endio.
  110. * but, if this was a mirror write, we may only
  111. * drbd_al_complete_io after this is RQ_NET_DONE,
  112. * otherwise the extent could be dropped from the al
  113. * before it has actually been written on the peer.
  114. * if we crash before our peer knows about the request,
  115. * but after the extent has been dropped from the al,
  116. * we would forget to resync the corresponding extent.
  117. */
  118. if (s & RQ_IN_ACT_LOG) {
  119. if (get_ldev_if_state(device, D_FAILED)) {
  120. drbd_al_complete_io(device, &req->i);
  121. put_ldev(device);
  122. } else if (drbd_ratelimit()) {
  123. drbd_warn(device, "Should have called drbd_al_complete_io(, %llu, %u), "
  124. "but my Disk seems to have failed :(\n",
  125. (unsigned long long) req->i.sector, req->i.size);
  126. }
  127. }
  128. }
  129. mempool_free(req, &drbd_request_mempool);
  130. }
  131. static void wake_all_senders(struct drbd_connection *connection)
  132. {
  133. wake_up(&connection->sender_work.q_wait);
  134. }
  135. /* must hold resource->req_lock */
  136. void start_new_tl_epoch(struct drbd_connection *connection)
  137. {
  138. /* no point closing an epoch, if it is empty, anyways. */
  139. if (connection->current_tle_writes == 0)
  140. return;
  141. connection->current_tle_writes = 0;
  142. atomic_inc(&connection->current_tle_nr);
  143. wake_all_senders(connection);
  144. }
  145. void complete_master_bio(struct drbd_device *device,
  146. struct bio_and_error *m)
  147. {
  148. if (unlikely(m->error))
  149. m->bio->bi_status = errno_to_blk_status(m->error);
  150. bio_endio(m->bio);
  151. dec_ap_bio(device);
  152. }
  153. /* Helper for __req_mod().
  154. * Set m->bio to the master bio, if it is fit to be completed,
  155. * or leave it alone (it is initialized to NULL in __req_mod),
  156. * if it has already been completed, or cannot be completed yet.
  157. * If m->bio is set, the error status to be returned is placed in m->error.
  158. */
  159. static
  160. void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
  161. {
  162. const unsigned s = req->rq_state;
  163. struct drbd_device *device = req->device;
  164. int error, ok;
  165. /* we must not complete the master bio, while it is
  166. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  167. * not yet acknowledged by the peer
  168. * not yet completed by the local io subsystem
  169. * these flags may get cleared in any order by
  170. * the worker,
  171. * the receiver,
  172. * the bio_endio completion callbacks.
  173. */
  174. if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
  175. (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
  176. (s & RQ_COMPLETION_SUSP)) {
  177. drbd_err(device, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
  178. return;
  179. }
  180. if (!req->master_bio) {
  181. drbd_err(device, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
  182. return;
  183. }
  184. /*
  185. * figure out whether to report success or failure.
  186. *
  187. * report success when at least one of the operations succeeded.
  188. * or, to put the other way,
  189. * only report failure, when both operations failed.
  190. *
  191. * what to do about the failures is handled elsewhere.
  192. * what we need to do here is just: complete the master_bio.
  193. *
  194. * local completion error, if any, has been stored as ERR_PTR
  195. * in private_bio within drbd_request_endio.
  196. */
  197. ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
  198. error = PTR_ERR(req->private_bio);
  199. /* Before we can signal completion to the upper layers,
  200. * we may need to close the current transfer log epoch.
  201. * We are within the request lock, so we can simply compare
  202. * the request epoch number with the current transfer log
  203. * epoch number. If they match, increase the current_tle_nr,
  204. * and reset the transfer log epoch write_cnt.
  205. */
  206. if (op_is_write(bio_op(req->master_bio)) &&
  207. req->epoch == atomic_read(&first_peer_device(device)->connection->current_tle_nr))
  208. start_new_tl_epoch(first_peer_device(device)->connection);
  209. /* Update disk stats */
  210. bio_end_io_acct(req->master_bio, req->start_jif);
  211. /* If READ failed,
  212. * have it be pushed back to the retry work queue,
  213. * so it will re-enter __drbd_make_request(),
  214. * and be re-assigned to a suitable local or remote path,
  215. * or failed if we do not have access to good data anymore.
  216. *
  217. * Unless it was failed early by __drbd_make_request(),
  218. * because no path was available, in which case
  219. * it was not even added to the transfer_log.
  220. *
  221. * read-ahead may fail, and will not be retried.
  222. *
  223. * WRITE should have used all available paths already.
  224. */
  225. if (!ok &&
  226. bio_op(req->master_bio) == REQ_OP_READ &&
  227. !(req->master_bio->bi_opf & REQ_RAHEAD) &&
  228. !list_empty(&req->tl_requests))
  229. req->rq_state |= RQ_POSTPONED;
  230. if (!(req->rq_state & RQ_POSTPONED)) {
  231. m->error = ok ? 0 : (error ?: -EIO);
  232. m->bio = req->master_bio;
  233. req->master_bio = NULL;
  234. /* We leave it in the tree, to be able to verify later
  235. * write-acks in protocol != C during resync.
  236. * But we mark it as "complete", so it won't be counted as
  237. * conflict in a multi-primary setup. */
  238. req->i.completed = true;
  239. }
  240. if (req->i.waiting)
  241. wake_up(&device->misc_wait);
  242. /* Either we are about to complete to upper layers,
  243. * or we will restart this request.
  244. * In either case, the request object will be destroyed soon,
  245. * so better remove it from all lists. */
  246. list_del_init(&req->req_pending_master_completion);
  247. }
  248. /* still holds resource->req_lock */
  249. static void drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
  250. {
  251. struct drbd_device *device = req->device;
  252. D_ASSERT(device, m || (req->rq_state & RQ_POSTPONED));
  253. if (!put)
  254. return;
  255. if (!atomic_sub_and_test(put, &req->completion_ref))
  256. return;
  257. drbd_req_complete(req, m);
  258. /* local completion may still come in later,
  259. * we need to keep the req object around. */
  260. if (req->rq_state & RQ_LOCAL_ABORTED)
  261. return;
  262. if (req->rq_state & RQ_POSTPONED) {
  263. /* don't destroy the req object just yet,
  264. * but queue it for retry */
  265. drbd_restart_request(req);
  266. return;
  267. }
  268. kref_put(&req->kref, drbd_req_destroy);
  269. }
  270. static void set_if_null_req_next(struct drbd_peer_device *peer_device, struct drbd_request *req)
  271. {
  272. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  273. if (!connection)
  274. return;
  275. if (connection->req_next == NULL)
  276. connection->req_next = req;
  277. }
  278. static void advance_conn_req_next(struct drbd_peer_device *peer_device, struct drbd_request *req)
  279. {
  280. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  281. struct drbd_request *iter = req;
  282. if (!connection)
  283. return;
  284. if (connection->req_next != req)
  285. return;
  286. req = NULL;
  287. list_for_each_entry_continue(iter, &connection->transfer_log, tl_requests) {
  288. const unsigned int s = iter->rq_state;
  289. if (s & RQ_NET_QUEUED) {
  290. req = iter;
  291. break;
  292. }
  293. }
  294. connection->req_next = req;
  295. }
  296. static void set_if_null_req_ack_pending(struct drbd_peer_device *peer_device, struct drbd_request *req)
  297. {
  298. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  299. if (!connection)
  300. return;
  301. if (connection->req_ack_pending == NULL)
  302. connection->req_ack_pending = req;
  303. }
  304. static void advance_conn_req_ack_pending(struct drbd_peer_device *peer_device, struct drbd_request *req)
  305. {
  306. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  307. struct drbd_request *iter = req;
  308. if (!connection)
  309. return;
  310. if (connection->req_ack_pending != req)
  311. return;
  312. req = NULL;
  313. list_for_each_entry_continue(iter, &connection->transfer_log, tl_requests) {
  314. const unsigned int s = iter->rq_state;
  315. if ((s & RQ_NET_SENT) && (s & RQ_NET_PENDING)) {
  316. req = iter;
  317. break;
  318. }
  319. }
  320. connection->req_ack_pending = req;
  321. }
  322. static void set_if_null_req_not_net_done(struct drbd_peer_device *peer_device, struct drbd_request *req)
  323. {
  324. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  325. if (!connection)
  326. return;
  327. if (connection->req_not_net_done == NULL)
  328. connection->req_not_net_done = req;
  329. }
  330. static void advance_conn_req_not_net_done(struct drbd_peer_device *peer_device, struct drbd_request *req)
  331. {
  332. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  333. struct drbd_request *iter = req;
  334. if (!connection)
  335. return;
  336. if (connection->req_not_net_done != req)
  337. return;
  338. req = NULL;
  339. list_for_each_entry_continue(iter, &connection->transfer_log, tl_requests) {
  340. const unsigned int s = iter->rq_state;
  341. if ((s & RQ_NET_SENT) && !(s & RQ_NET_DONE)) {
  342. req = iter;
  343. break;
  344. }
  345. }
  346. connection->req_not_net_done = req;
  347. }
  348. /* I'd like this to be the only place that manipulates
  349. * req->completion_ref and req->kref. */
  350. static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
  351. int clear, int set)
  352. {
  353. struct drbd_device *device = req->device;
  354. struct drbd_peer_device *peer_device = first_peer_device(device);
  355. unsigned s = req->rq_state;
  356. int c_put = 0;
  357. if (drbd_suspended(device) && !((s | clear) & RQ_COMPLETION_SUSP))
  358. set |= RQ_COMPLETION_SUSP;
  359. /* apply */
  360. req->rq_state &= ~clear;
  361. req->rq_state |= set;
  362. /* no change? */
  363. if (req->rq_state == s)
  364. return;
  365. /* intent: get references */
  366. kref_get(&req->kref);
  367. if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
  368. atomic_inc(&req->completion_ref);
  369. if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
  370. inc_ap_pending(device);
  371. atomic_inc(&req->completion_ref);
  372. }
  373. if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED)) {
  374. atomic_inc(&req->completion_ref);
  375. set_if_null_req_next(peer_device, req);
  376. }
  377. if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
  378. kref_get(&req->kref); /* wait for the DONE */
  379. if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT)) {
  380. /* potentially already completed in the ack_receiver thread */
  381. if (!(s & RQ_NET_DONE)) {
  382. atomic_add(req->i.size >> 9, &device->ap_in_flight);
  383. set_if_null_req_not_net_done(peer_device, req);
  384. }
  385. if (req->rq_state & RQ_NET_PENDING)
  386. set_if_null_req_ack_pending(peer_device, req);
  387. }
  388. if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
  389. atomic_inc(&req->completion_ref);
  390. /* progress: put references */
  391. if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
  392. ++c_put;
  393. if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
  394. D_ASSERT(device, req->rq_state & RQ_LOCAL_PENDING);
  395. ++c_put;
  396. }
  397. if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
  398. if (req->rq_state & RQ_LOCAL_ABORTED)
  399. kref_put(&req->kref, drbd_req_destroy);
  400. else
  401. ++c_put;
  402. list_del_init(&req->req_pending_local);
  403. }
  404. if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
  405. dec_ap_pending(device);
  406. ++c_put;
  407. req->acked_jif = jiffies;
  408. advance_conn_req_ack_pending(peer_device, req);
  409. }
  410. if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED)) {
  411. ++c_put;
  412. advance_conn_req_next(peer_device, req);
  413. }
  414. if (!(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
  415. if (s & RQ_NET_SENT)
  416. atomic_sub(req->i.size >> 9, &device->ap_in_flight);
  417. if (s & RQ_EXP_BARR_ACK)
  418. kref_put(&req->kref, drbd_req_destroy);
  419. req->net_done_jif = jiffies;
  420. /* in ahead/behind mode, or just in case,
  421. * before we finally destroy this request,
  422. * the caching pointers must not reference it anymore */
  423. advance_conn_req_next(peer_device, req);
  424. advance_conn_req_ack_pending(peer_device, req);
  425. advance_conn_req_not_net_done(peer_device, req);
  426. }
  427. /* potentially complete and destroy */
  428. /* If we made progress, retry conflicting peer requests, if any. */
  429. if (req->i.waiting)
  430. wake_up(&device->misc_wait);
  431. drbd_req_put_completion_ref(req, m, c_put);
  432. kref_put(&req->kref, drbd_req_destroy);
  433. }
  434. static void drbd_report_io_error(struct drbd_device *device, struct drbd_request *req)
  435. {
  436. if (!drbd_ratelimit())
  437. return;
  438. drbd_warn(device, "local %s IO error sector %llu+%u on %pg\n",
  439. (req->rq_state & RQ_WRITE) ? "WRITE" : "READ",
  440. (unsigned long long)req->i.sector,
  441. req->i.size >> 9,
  442. device->ldev->backing_bdev);
  443. }
  444. /* Helper for HANDED_OVER_TO_NETWORK.
  445. * Is this a protocol A write (neither WRITE_ACK nor RECEIVE_ACK expected)?
  446. * Is it also still "PENDING"?
  447. * --> If so, clear PENDING and set NET_OK below.
  448. * If it is a protocol A write, but not RQ_PENDING anymore, neg-ack was faster
  449. * (and we must not set RQ_NET_OK) */
  450. static inline bool is_pending_write_protocol_A(struct drbd_request *req)
  451. {
  452. return (req->rq_state &
  453. (RQ_WRITE|RQ_NET_PENDING|RQ_EXP_WRITE_ACK|RQ_EXP_RECEIVE_ACK))
  454. == (RQ_WRITE|RQ_NET_PENDING);
  455. }
  456. /* obviously this could be coded as many single functions
  457. * instead of one huge switch,
  458. * or by putting the code directly in the respective locations
  459. * (as it has been before).
  460. *
  461. * but having it this way
  462. * enforces that it is all in this one place, where it is easier to audit,
  463. * it makes it obvious that whatever "event" "happens" to a request should
  464. * happen "atomically" within the req_lock,
  465. * and it enforces that we have to think in a very structured manner
  466. * about the "events" that may happen to a request during its life time ...
  467. *
  468. *
  469. * peer_device == NULL means local disk
  470. */
  471. int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  472. struct drbd_peer_device *peer_device,
  473. struct bio_and_error *m)
  474. {
  475. struct drbd_device *const device = req->device;
  476. struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
  477. struct net_conf *nc;
  478. int p, rv = 0;
  479. if (m)
  480. m->bio = NULL;
  481. switch (what) {
  482. default:
  483. drbd_err(device, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  484. break;
  485. /* does not happen...
  486. * initialization done in drbd_req_new
  487. case CREATED:
  488. break;
  489. */
  490. case TO_BE_SENT: /* via network */
  491. /* reached via __drbd_make_request
  492. * and from w_read_retry_remote */
  493. D_ASSERT(device, !(req->rq_state & RQ_NET_MASK));
  494. rcu_read_lock();
  495. nc = rcu_dereference(connection->net_conf);
  496. p = nc->wire_protocol;
  497. rcu_read_unlock();
  498. req->rq_state |=
  499. p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
  500. p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
  501. mod_rq_state(req, m, 0, RQ_NET_PENDING);
  502. break;
  503. case TO_BE_SUBMITTED: /* locally */
  504. /* reached via __drbd_make_request */
  505. D_ASSERT(device, !(req->rq_state & RQ_LOCAL_MASK));
  506. mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
  507. break;
  508. case COMPLETED_OK:
  509. if (req->rq_state & RQ_WRITE)
  510. device->writ_cnt += req->i.size >> 9;
  511. else
  512. device->read_cnt += req->i.size >> 9;
  513. mod_rq_state(req, m, RQ_LOCAL_PENDING,
  514. RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  515. break;
  516. case ABORT_DISK_IO:
  517. mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
  518. break;
  519. case WRITE_COMPLETED_WITH_ERROR:
  520. drbd_report_io_error(device, req);
  521. __drbd_chk_io_error(device, DRBD_WRITE_ERROR);
  522. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  523. break;
  524. case READ_COMPLETED_WITH_ERROR:
  525. drbd_set_out_of_sync(first_peer_device(device),
  526. req->i.sector, req->i.size);
  527. drbd_report_io_error(device, req);
  528. __drbd_chk_io_error(device, DRBD_READ_ERROR);
  529. fallthrough;
  530. case READ_AHEAD_COMPLETED_WITH_ERROR:
  531. /* it is legal to fail read-ahead, no __drbd_chk_io_error in that case. */
  532. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  533. break;
  534. case DISCARD_COMPLETED_NOTSUPP:
  535. case DISCARD_COMPLETED_WITH_ERROR:
  536. /* I'd rather not detach from local disk just because it
  537. * failed a REQ_OP_DISCARD. */
  538. mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
  539. break;
  540. case QUEUE_FOR_NET_READ:
  541. /* READ, and
  542. * no local disk,
  543. * or target area marked as invalid,
  544. * or just got an io-error. */
  545. /* from __drbd_make_request
  546. * or from bio_endio during read io-error recovery */
  547. /* So we can verify the handle in the answer packet.
  548. * Corresponding drbd_remove_request_interval is in
  549. * drbd_req_complete() */
  550. D_ASSERT(device, drbd_interval_empty(&req->i));
  551. drbd_insert_interval(&device->read_requests, &req->i);
  552. set_bit(UNPLUG_REMOTE, &device->flags);
  553. D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
  554. D_ASSERT(device, (req->rq_state & RQ_LOCAL_MASK) == 0);
  555. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  556. req->w.cb = w_send_read_req;
  557. drbd_queue_work(&connection->sender_work,
  558. &req->w);
  559. break;
  560. case QUEUE_FOR_NET_WRITE:
  561. /* assert something? */
  562. /* from __drbd_make_request only */
  563. /* Corresponding drbd_remove_request_interval is in
  564. * drbd_req_complete() */
  565. D_ASSERT(device, drbd_interval_empty(&req->i));
  566. drbd_insert_interval(&device->write_requests, &req->i);
  567. /* NOTE
  568. * In case the req ended up on the transfer log before being
  569. * queued on the worker, it could lead to this request being
  570. * missed during cleanup after connection loss.
  571. * So we have to do both operations here,
  572. * within the same lock that protects the transfer log.
  573. *
  574. * _req_add_to_epoch(req); this has to be after the
  575. * _maybe_start_new_epoch(req); which happened in
  576. * __drbd_make_request, because we now may set the bit
  577. * again ourselves to close the current epoch.
  578. *
  579. * Add req to the (now) current epoch (barrier). */
  580. /* otherwise we may lose an unplug, which may cause some remote
  581. * io-scheduler timeout to expire, increasing maximum latency,
  582. * hurting performance. */
  583. set_bit(UNPLUG_REMOTE, &device->flags);
  584. /* queue work item to send data */
  585. D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
  586. mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
  587. req->w.cb = w_send_dblock;
  588. drbd_queue_work(&connection->sender_work,
  589. &req->w);
  590. /* close the epoch, in case it outgrew the limit */
  591. rcu_read_lock();
  592. nc = rcu_dereference(connection->net_conf);
  593. p = nc->max_epoch_size;
  594. rcu_read_unlock();
  595. if (connection->current_tle_writes >= p)
  596. start_new_tl_epoch(connection);
  597. break;
  598. case QUEUE_FOR_SEND_OOS:
  599. mod_rq_state(req, m, 0, RQ_NET_QUEUED);
  600. req->w.cb = w_send_out_of_sync;
  601. drbd_queue_work(&connection->sender_work,
  602. &req->w);
  603. break;
  604. case READ_RETRY_REMOTE_CANCELED:
  605. case SEND_CANCELED:
  606. case SEND_FAILED:
  607. /* real cleanup will be done from tl_clear. just update flags
  608. * so it is no longer marked as on the worker queue */
  609. mod_rq_state(req, m, RQ_NET_QUEUED, 0);
  610. break;
  611. case HANDED_OVER_TO_NETWORK:
  612. /* assert something? */
  613. if (is_pending_write_protocol_A(req))
  614. /* this is what is dangerous about protocol A:
  615. * pretend it was successfully written on the peer. */
  616. mod_rq_state(req, m, RQ_NET_QUEUED|RQ_NET_PENDING,
  617. RQ_NET_SENT|RQ_NET_OK);
  618. else
  619. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
  620. /* It is still not yet RQ_NET_DONE until the
  621. * corresponding epoch barrier got acked as well,
  622. * so we know what to dirty on connection loss. */
  623. break;
  624. case OOS_HANDED_TO_NETWORK:
  625. /* Was not set PENDING, no longer QUEUED, so is now DONE
  626. * as far as this connection is concerned. */
  627. mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
  628. break;
  629. case CONNECTION_LOST_WHILE_PENDING:
  630. /* transfer log cleanup after connection loss */
  631. mod_rq_state(req, m,
  632. RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
  633. RQ_NET_DONE);
  634. break;
  635. case CONFLICT_RESOLVED:
  636. /* for superseded conflicting writes of multiple primaries,
  637. * there is no need to keep anything in the tl, potential
  638. * node crashes are covered by the activity log.
  639. *
  640. * If this request had been marked as RQ_POSTPONED before,
  641. * it will actually not be completed, but "restarted",
  642. * resubmitted from the retry worker context. */
  643. D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
  644. D_ASSERT(device, req->rq_state & RQ_EXP_WRITE_ACK);
  645. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
  646. break;
  647. case WRITE_ACKED_BY_PEER_AND_SIS:
  648. req->rq_state |= RQ_NET_SIS;
  649. fallthrough;
  650. case WRITE_ACKED_BY_PEER:
  651. /* Normal operation protocol C: successfully written on peer.
  652. * During resync, even in protocol != C,
  653. * we requested an explicit write ack anyways.
  654. * Which means we cannot even assert anything here.
  655. * Nothing more to do here.
  656. * We want to keep the tl in place for all protocols, to cater
  657. * for volatile write-back caches on lower level devices. */
  658. goto ack_common;
  659. case RECV_ACKED_BY_PEER:
  660. D_ASSERT(device, req->rq_state & RQ_EXP_RECEIVE_ACK);
  661. /* protocol B; pretends to be successfully written on peer.
  662. * see also notes above in HANDED_OVER_TO_NETWORK about
  663. * protocol != C */
  664. ack_common:
  665. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
  666. break;
  667. case POSTPONE_WRITE:
  668. D_ASSERT(device, req->rq_state & RQ_EXP_WRITE_ACK);
  669. /* If this node has already detected the write conflict, the
  670. * worker will be waiting on misc_wait. Wake it up once this
  671. * request has completed locally.
  672. */
  673. D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
  674. req->rq_state |= RQ_POSTPONED;
  675. if (req->i.waiting)
  676. wake_up(&device->misc_wait);
  677. /* Do not clear RQ_NET_PENDING. This request will make further
  678. * progress via restart_conflicting_writes() or
  679. * fail_postponed_requests(). Hopefully. */
  680. break;
  681. case NEG_ACKED:
  682. mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
  683. break;
  684. case FAIL_FROZEN_DISK_IO:
  685. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  686. break;
  687. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  688. break;
  689. case RESTART_FROZEN_DISK_IO:
  690. if (!(req->rq_state & RQ_LOCAL_COMPLETED))
  691. break;
  692. mod_rq_state(req, m,
  693. RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
  694. RQ_LOCAL_PENDING);
  695. rv = MR_READ;
  696. if (bio_data_dir(req->master_bio) == WRITE)
  697. rv = MR_WRITE;
  698. get_ldev(device); /* always succeeds in this call path */
  699. req->w.cb = w_restart_disk_io;
  700. drbd_queue_work(&connection->sender_work,
  701. &req->w);
  702. break;
  703. case RESEND:
  704. /* Simply complete (local only) READs. */
  705. if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
  706. mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
  707. break;
  708. }
  709. /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
  710. before the connection loss (B&C only); only P_BARRIER_ACK
  711. (or the local completion?) was missing when we suspended.
  712. Throwing them out of the TL here by pretending we got a BARRIER_ACK.
  713. During connection handshake, we ensure that the peer was not rebooted. */
  714. if (!(req->rq_state & RQ_NET_OK)) {
  715. /* FIXME could this possibly be a req->dw.cb == w_send_out_of_sync?
  716. * in that case we must not set RQ_NET_PENDING. */
  717. mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
  718. if (req->w.cb) {
  719. /* w.cb expected to be w_send_dblock, or w_send_read_req */
  720. drbd_queue_work(&connection->sender_work,
  721. &req->w);
  722. rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
  723. } /* else: FIXME can this happen? */
  724. break;
  725. }
  726. fallthrough; /* to BARRIER_ACKED */
  727. case BARRIER_ACKED:
  728. /* barrier ack for READ requests does not make sense */
  729. if (!(req->rq_state & RQ_WRITE))
  730. break;
  731. if (req->rq_state & RQ_NET_PENDING) {
  732. /* barrier came in before all requests were acked.
  733. * this is bad, because if the connection is lost now,
  734. * we won't be able to clean them up... */
  735. drbd_err(device, "FIXME (BARRIER_ACKED but pending)\n");
  736. }
  737. /* Allowed to complete requests, even while suspended.
  738. * As this is called for all requests within a matching epoch,
  739. * we need to filter, and only set RQ_NET_DONE for those that
  740. * have actually been on the wire. */
  741. mod_rq_state(req, m, RQ_COMPLETION_SUSP,
  742. (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
  743. break;
  744. case DATA_RECEIVED:
  745. D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
  746. mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
  747. break;
  748. case QUEUE_AS_DRBD_BARRIER:
  749. start_new_tl_epoch(connection);
  750. mod_rq_state(req, m, 0, RQ_NET_OK|RQ_NET_DONE);
  751. break;
  752. }
  753. return rv;
  754. }
  755. /* we may do a local read if:
  756. * - we are consistent (of course),
  757. * - or we are generally inconsistent,
  758. * BUT we are still/already IN SYNC for this area.
  759. * since size may be bigger than BM_BLOCK_SIZE,
  760. * we may need to check several bits.
  761. */
  762. static bool drbd_may_do_local_read(struct drbd_device *device, sector_t sector, int size)
  763. {
  764. unsigned long sbnr, ebnr;
  765. sector_t esector, nr_sectors;
  766. if (device->state.disk == D_UP_TO_DATE)
  767. return true;
  768. if (device->state.disk != D_INCONSISTENT)
  769. return false;
  770. esector = sector + (size >> 9) - 1;
  771. nr_sectors = get_capacity(device->vdisk);
  772. D_ASSERT(device, sector < nr_sectors);
  773. D_ASSERT(device, esector < nr_sectors);
  774. sbnr = BM_SECT_TO_BIT(sector);
  775. ebnr = BM_SECT_TO_BIT(esector);
  776. return drbd_bm_count_bits(device, sbnr, ebnr) == 0;
  777. }
  778. static bool remote_due_to_read_balancing(struct drbd_device *device, sector_t sector,
  779. enum drbd_read_balancing rbm)
  780. {
  781. int stripe_shift;
  782. switch (rbm) {
  783. case RB_CONGESTED_REMOTE:
  784. return false;
  785. case RB_LEAST_PENDING:
  786. return atomic_read(&device->local_cnt) >
  787. atomic_read(&device->ap_pending_cnt) + atomic_read(&device->rs_pending_cnt);
  788. case RB_32K_STRIPING: /* stripe_shift = 15 */
  789. case RB_64K_STRIPING:
  790. case RB_128K_STRIPING:
  791. case RB_256K_STRIPING:
  792. case RB_512K_STRIPING:
  793. case RB_1M_STRIPING: /* stripe_shift = 20 */
  794. stripe_shift = (rbm - RB_32K_STRIPING + 15);
  795. return (sector >> (stripe_shift - 9)) & 1;
  796. case RB_ROUND_ROBIN:
  797. return test_and_change_bit(READ_BALANCE_RR, &device->flags);
  798. case RB_PREFER_REMOTE:
  799. return true;
  800. case RB_PREFER_LOCAL:
  801. default:
  802. return false;
  803. }
  804. }
  805. /*
  806. * complete_conflicting_writes - wait for any conflicting write requests
  807. *
  808. * The write_requests tree contains all active write requests which we
  809. * currently know about. Wait for any requests to complete which conflict with
  810. * the new one.
  811. *
  812. * Only way out: remove the conflicting intervals from the tree.
  813. */
  814. static void complete_conflicting_writes(struct drbd_request *req)
  815. {
  816. DEFINE_WAIT(wait);
  817. struct drbd_device *device = req->device;
  818. struct drbd_interval *i;
  819. sector_t sector = req->i.sector;
  820. int size = req->i.size;
  821. for (;;) {
  822. drbd_for_each_overlap(i, &device->write_requests, sector, size) {
  823. /* Ignore, if already completed to upper layers. */
  824. if (i->completed)
  825. continue;
  826. /* Handle the first found overlap. After the schedule
  827. * we have to restart the tree walk. */
  828. break;
  829. }
  830. if (!i) /* if any */
  831. break;
  832. /* Indicate to wake up device->misc_wait on progress. */
  833. prepare_to_wait(&device->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
  834. i->waiting = true;
  835. spin_unlock_irq(&device->resource->req_lock);
  836. schedule();
  837. spin_lock_irq(&device->resource->req_lock);
  838. }
  839. finish_wait(&device->misc_wait, &wait);
  840. }
  841. /* called within req_lock */
  842. static void maybe_pull_ahead(struct drbd_device *device)
  843. {
  844. struct drbd_connection *connection = first_peer_device(device)->connection;
  845. struct net_conf *nc;
  846. bool congested = false;
  847. enum drbd_on_congestion on_congestion;
  848. rcu_read_lock();
  849. nc = rcu_dereference(connection->net_conf);
  850. on_congestion = nc ? nc->on_congestion : OC_BLOCK;
  851. rcu_read_unlock();
  852. if (on_congestion == OC_BLOCK ||
  853. connection->agreed_pro_version < 96)
  854. return;
  855. if (on_congestion == OC_PULL_AHEAD && device->state.conn == C_AHEAD)
  856. return; /* nothing to do ... */
  857. /* If I don't even have good local storage, we can not reasonably try
  858. * to pull ahead of the peer. We also need the local reference to make
  859. * sure device->act_log is there.
  860. */
  861. if (!get_ldev_if_state(device, D_UP_TO_DATE))
  862. return;
  863. if (nc->cong_fill &&
  864. atomic_read(&device->ap_in_flight) >= nc->cong_fill) {
  865. drbd_info(device, "Congestion-fill threshold reached\n");
  866. congested = true;
  867. }
  868. if (device->act_log->used >= nc->cong_extents) {
  869. drbd_info(device, "Congestion-extents threshold reached\n");
  870. congested = true;
  871. }
  872. if (congested) {
  873. /* start a new epoch for non-mirrored writes */
  874. start_new_tl_epoch(first_peer_device(device)->connection);
  875. if (on_congestion == OC_PULL_AHEAD)
  876. _drbd_set_state(_NS(device, conn, C_AHEAD), 0, NULL);
  877. else /*nc->on_congestion == OC_DISCONNECT */
  878. _drbd_set_state(_NS(device, conn, C_DISCONNECTING), 0, NULL);
  879. }
  880. put_ldev(device);
  881. }
  882. /* If this returns false, and req->private_bio is still set,
  883. * this should be submitted locally.
  884. *
  885. * If it returns false, but req->private_bio is not set,
  886. * we do not have access to good data :(
  887. *
  888. * Otherwise, this destroys req->private_bio, if any,
  889. * and returns true.
  890. */
  891. static bool do_remote_read(struct drbd_request *req)
  892. {
  893. struct drbd_device *device = req->device;
  894. enum drbd_read_balancing rbm;
  895. if (req->private_bio) {
  896. if (!drbd_may_do_local_read(device,
  897. req->i.sector, req->i.size)) {
  898. bio_put(req->private_bio);
  899. req->private_bio = NULL;
  900. put_ldev(device);
  901. }
  902. }
  903. if (device->state.pdsk != D_UP_TO_DATE)
  904. return false;
  905. if (req->private_bio == NULL)
  906. return true;
  907. /* TODO: improve read balancing decisions, take into account drbd
  908. * protocol, pending requests etc. */
  909. rcu_read_lock();
  910. rbm = rcu_dereference(device->ldev->disk_conf)->read_balancing;
  911. rcu_read_unlock();
  912. if (rbm == RB_PREFER_LOCAL && req->private_bio)
  913. return false; /* submit locally */
  914. if (remote_due_to_read_balancing(device, req->i.sector, rbm)) {
  915. if (req->private_bio) {
  916. bio_put(req->private_bio);
  917. req->private_bio = NULL;
  918. put_ldev(device);
  919. }
  920. return true;
  921. }
  922. return false;
  923. }
  924. bool drbd_should_do_remote(union drbd_dev_state s)
  925. {
  926. return s.pdsk == D_UP_TO_DATE ||
  927. (s.pdsk >= D_INCONSISTENT &&
  928. s.conn >= C_WF_BITMAP_T &&
  929. s.conn < C_AHEAD);
  930. /* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
  931. That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
  932. states. */
  933. }
  934. static bool drbd_should_send_out_of_sync(union drbd_dev_state s)
  935. {
  936. return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
  937. /* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
  938. since we enter state C_AHEAD only if proto >= 96 */
  939. }
  940. /* returns number of connections (== 1, for drbd 8.4)
  941. * expected to actually write this data,
  942. * which does NOT include those that we are L_AHEAD for. */
  943. static int drbd_process_write_request(struct drbd_request *req)
  944. {
  945. struct drbd_device *device = req->device;
  946. struct drbd_peer_device *peer_device = first_peer_device(device);
  947. int remote, send_oos;
  948. remote = drbd_should_do_remote(device->state);
  949. send_oos = drbd_should_send_out_of_sync(device->state);
  950. /* Need to replicate writes. Unless it is an empty flush,
  951. * which is better mapped to a DRBD P_BARRIER packet,
  952. * also for drbd wire protocol compatibility reasons.
  953. * If this was a flush, just start a new epoch.
  954. * Unless the current epoch was empty anyways, or we are not currently
  955. * replicating, in which case there is no point. */
  956. if (unlikely(req->i.size == 0)) {
  957. /* The only size==0 bios we expect are empty flushes. */
  958. D_ASSERT(device, req->master_bio->bi_opf & REQ_PREFLUSH);
  959. if (remote)
  960. _req_mod(req, QUEUE_AS_DRBD_BARRIER, peer_device);
  961. return remote;
  962. }
  963. if (!remote && !send_oos)
  964. return 0;
  965. D_ASSERT(device, !(remote && send_oos));
  966. if (remote) {
  967. _req_mod(req, TO_BE_SENT, peer_device);
  968. _req_mod(req, QUEUE_FOR_NET_WRITE, peer_device);
  969. } else if (drbd_set_out_of_sync(peer_device, req->i.sector, req->i.size))
  970. _req_mod(req, QUEUE_FOR_SEND_OOS, peer_device);
  971. return remote;
  972. }
  973. static void drbd_process_discard_or_zeroes_req(struct drbd_request *req, int flags)
  974. {
  975. int err = drbd_issue_discard_or_zero_out(req->device,
  976. req->i.sector, req->i.size >> 9, flags);
  977. if (err)
  978. req->private_bio->bi_status = BLK_STS_IOERR;
  979. bio_endio(req->private_bio);
  980. }
  981. static void
  982. drbd_submit_req_private_bio(struct drbd_request *req)
  983. {
  984. struct drbd_device *device = req->device;
  985. struct bio *bio = req->private_bio;
  986. unsigned int type;
  987. if (bio_op(bio) != REQ_OP_READ)
  988. type = DRBD_FAULT_DT_WR;
  989. else if (bio->bi_opf & REQ_RAHEAD)
  990. type = DRBD_FAULT_DT_RA;
  991. else
  992. type = DRBD_FAULT_DT_RD;
  993. /* State may have changed since we grabbed our reference on the
  994. * ->ldev member. Double check, and short-circuit to endio.
  995. * In case the last activity log transaction failed to get on
  996. * stable storage, and this is a WRITE, we may not even submit
  997. * this bio. */
  998. if (get_ldev(device)) {
  999. if (drbd_insert_fault(device, type))
  1000. bio_io_error(bio);
  1001. else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
  1002. drbd_process_discard_or_zeroes_req(req, EE_ZEROOUT |
  1003. ((bio->bi_opf & REQ_NOUNMAP) ? 0 : EE_TRIM));
  1004. else if (bio_op(bio) == REQ_OP_DISCARD)
  1005. drbd_process_discard_or_zeroes_req(req, EE_TRIM);
  1006. else
  1007. submit_bio_noacct(bio);
  1008. put_ldev(device);
  1009. } else
  1010. bio_io_error(bio);
  1011. }
  1012. static void drbd_queue_write(struct drbd_device *device, struct drbd_request *req)
  1013. {
  1014. spin_lock_irq(&device->resource->req_lock);
  1015. list_add_tail(&req->tl_requests, &device->submit.writes);
  1016. list_add_tail(&req->req_pending_master_completion,
  1017. &device->pending_master_completion[1 /* WRITE */]);
  1018. spin_unlock_irq(&device->resource->req_lock);
  1019. queue_work(device->submit.wq, &device->submit.worker);
  1020. /* do_submit() may sleep internally on al_wait, too */
  1021. wake_up(&device->al_wait);
  1022. }
  1023. /* returns the new drbd_request pointer, if the caller is expected to
  1024. * drbd_send_and_submit() it (to save latency), or NULL if we queued the
  1025. * request on the submitter thread.
  1026. * Returns ERR_PTR(-ENOMEM) if we cannot allocate a drbd_request.
  1027. */
  1028. static struct drbd_request *
  1029. drbd_request_prepare(struct drbd_device *device, struct bio *bio)
  1030. {
  1031. const int rw = bio_data_dir(bio);
  1032. struct drbd_request *req;
  1033. /* allocate outside of all locks; */
  1034. req = drbd_req_new(device, bio);
  1035. if (!req) {
  1036. dec_ap_bio(device);
  1037. /* only pass the error to the upper layers.
  1038. * if user cannot handle io errors, that's not our business. */
  1039. drbd_err(device, "could not kmalloc() req\n");
  1040. bio->bi_status = BLK_STS_RESOURCE;
  1041. bio_endio(bio);
  1042. return ERR_PTR(-ENOMEM);
  1043. }
  1044. /* Update disk stats */
  1045. req->start_jif = bio_start_io_acct(req->master_bio);
  1046. if (get_ldev(device)) {
  1047. req->private_bio = bio_alloc_clone(device->ldev->backing_bdev,
  1048. bio, GFP_NOIO,
  1049. &drbd_io_bio_set);
  1050. req->private_bio->bi_private = req;
  1051. req->private_bio->bi_end_io = drbd_request_endio;
  1052. }
  1053. /* process discards always from our submitter thread */
  1054. if (bio_op(bio) == REQ_OP_WRITE_ZEROES ||
  1055. bio_op(bio) == REQ_OP_DISCARD)
  1056. goto queue_for_submitter_thread;
  1057. if (rw == WRITE && req->private_bio && req->i.size
  1058. && !test_bit(AL_SUSPENDED, &device->flags)) {
  1059. if (!drbd_al_begin_io_fastpath(device, &req->i))
  1060. goto queue_for_submitter_thread;
  1061. req->rq_state |= RQ_IN_ACT_LOG;
  1062. req->in_actlog_jif = jiffies;
  1063. }
  1064. return req;
  1065. queue_for_submitter_thread:
  1066. atomic_inc(&device->ap_actlog_cnt);
  1067. drbd_queue_write(device, req);
  1068. return NULL;
  1069. }
  1070. /* Require at least one path to current data.
  1071. * We don't want to allow writes on C_STANDALONE D_INCONSISTENT:
  1072. * We would not allow to read what was written,
  1073. * we would not have bumped the data generation uuids,
  1074. * we would cause data divergence for all the wrong reasons.
  1075. *
  1076. * If we don't see at least one D_UP_TO_DATE, we will fail this request,
  1077. * which either returns EIO, or, if OND_SUSPEND_IO is set, suspends IO,
  1078. * and queues for retry later.
  1079. */
  1080. static bool may_do_writes(struct drbd_device *device)
  1081. {
  1082. const union drbd_dev_state s = device->state;
  1083. return s.disk == D_UP_TO_DATE || s.pdsk == D_UP_TO_DATE;
  1084. }
  1085. struct drbd_plug_cb {
  1086. struct blk_plug_cb cb;
  1087. struct drbd_request *most_recent_req;
  1088. /* do we need more? */
  1089. };
  1090. static void drbd_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1091. {
  1092. struct drbd_plug_cb *plug = container_of(cb, struct drbd_plug_cb, cb);
  1093. struct drbd_resource *resource = plug->cb.data;
  1094. struct drbd_request *req = plug->most_recent_req;
  1095. kfree(cb);
  1096. if (!req)
  1097. return;
  1098. spin_lock_irq(&resource->req_lock);
  1099. /* In case the sender did not process it yet, raise the flag to
  1100. * have it followed with P_UNPLUG_REMOTE just after. */
  1101. req->rq_state |= RQ_UNPLUG;
  1102. /* but also queue a generic unplug */
  1103. drbd_queue_unplug(req->device);
  1104. kref_put(&req->kref, drbd_req_destroy);
  1105. spin_unlock_irq(&resource->req_lock);
  1106. }
  1107. static struct drbd_plug_cb* drbd_check_plugged(struct drbd_resource *resource)
  1108. {
  1109. /* A lot of text to say
  1110. * return (struct drbd_plug_cb*)blk_check_plugged(); */
  1111. struct drbd_plug_cb *plug;
  1112. struct blk_plug_cb *cb = blk_check_plugged(drbd_unplug, resource, sizeof(*plug));
  1113. if (cb)
  1114. plug = container_of(cb, struct drbd_plug_cb, cb);
  1115. else
  1116. plug = NULL;
  1117. return plug;
  1118. }
  1119. static void drbd_update_plug(struct drbd_plug_cb *plug, struct drbd_request *req)
  1120. {
  1121. struct drbd_request *tmp = plug->most_recent_req;
  1122. /* Will be sent to some peer.
  1123. * Remember to tag it with UNPLUG_REMOTE on unplug */
  1124. kref_get(&req->kref);
  1125. plug->most_recent_req = req;
  1126. if (tmp)
  1127. kref_put(&tmp->kref, drbd_req_destroy);
  1128. }
  1129. static void drbd_send_and_submit(struct drbd_device *device, struct drbd_request *req)
  1130. {
  1131. struct drbd_resource *resource = device->resource;
  1132. struct drbd_peer_device *peer_device = first_peer_device(device);
  1133. const int rw = bio_data_dir(req->master_bio);
  1134. struct bio_and_error m = { NULL, };
  1135. bool no_remote = false;
  1136. bool submit_private_bio = false;
  1137. spin_lock_irq(&resource->req_lock);
  1138. if (rw == WRITE) {
  1139. /* This may temporarily give up the req_lock,
  1140. * but will re-aquire it before it returns here.
  1141. * Needs to be before the check on drbd_suspended() */
  1142. complete_conflicting_writes(req);
  1143. /* no more giving up req_lock from now on! */
  1144. /* check for congestion, and potentially stop sending
  1145. * full data updates, but start sending "dirty bits" only. */
  1146. maybe_pull_ahead(device);
  1147. }
  1148. if (drbd_suspended(device)) {
  1149. /* push back and retry: */
  1150. req->rq_state |= RQ_POSTPONED;
  1151. if (req->private_bio) {
  1152. bio_put(req->private_bio);
  1153. req->private_bio = NULL;
  1154. put_ldev(device);
  1155. }
  1156. goto out;
  1157. }
  1158. /* We fail READ early, if we can not serve it.
  1159. * We must do this before req is registered on any lists.
  1160. * Otherwise, drbd_req_complete() will queue failed READ for retry. */
  1161. if (rw != WRITE) {
  1162. if (!do_remote_read(req) && !req->private_bio)
  1163. goto nodata;
  1164. }
  1165. /* which transfer log epoch does this belong to? */
  1166. req->epoch = atomic_read(&first_peer_device(device)->connection->current_tle_nr);
  1167. /* no point in adding empty flushes to the transfer log,
  1168. * they are mapped to drbd barriers already. */
  1169. if (likely(req->i.size!=0)) {
  1170. if (rw == WRITE)
  1171. first_peer_device(device)->connection->current_tle_writes++;
  1172. list_add_tail(&req->tl_requests, &first_peer_device(device)->connection->transfer_log);
  1173. }
  1174. if (rw == WRITE) {
  1175. if (req->private_bio && !may_do_writes(device)) {
  1176. bio_put(req->private_bio);
  1177. req->private_bio = NULL;
  1178. put_ldev(device);
  1179. goto nodata;
  1180. }
  1181. if (!drbd_process_write_request(req))
  1182. no_remote = true;
  1183. } else {
  1184. /* We either have a private_bio, or we can read from remote.
  1185. * Otherwise we had done the goto nodata above. */
  1186. if (req->private_bio == NULL) {
  1187. _req_mod(req, TO_BE_SENT, peer_device);
  1188. _req_mod(req, QUEUE_FOR_NET_READ, peer_device);
  1189. } else
  1190. no_remote = true;
  1191. }
  1192. if (no_remote == false) {
  1193. struct drbd_plug_cb *plug = drbd_check_plugged(resource);
  1194. if (plug)
  1195. drbd_update_plug(plug, req);
  1196. }
  1197. /* If it took the fast path in drbd_request_prepare, add it here.
  1198. * The slow path has added it already. */
  1199. if (list_empty(&req->req_pending_master_completion))
  1200. list_add_tail(&req->req_pending_master_completion,
  1201. &device->pending_master_completion[rw == WRITE]);
  1202. if (req->private_bio) {
  1203. /* needs to be marked within the same spinlock */
  1204. req->pre_submit_jif = jiffies;
  1205. list_add_tail(&req->req_pending_local,
  1206. &device->pending_completion[rw == WRITE]);
  1207. _req_mod(req, TO_BE_SUBMITTED, NULL);
  1208. /* but we need to give up the spinlock to submit */
  1209. submit_private_bio = true;
  1210. } else if (no_remote) {
  1211. nodata:
  1212. if (drbd_ratelimit())
  1213. drbd_err(device, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
  1214. (unsigned long long)req->i.sector, req->i.size >> 9);
  1215. /* A write may have been queued for send_oos, however.
  1216. * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
  1217. }
  1218. out:
  1219. drbd_req_put_completion_ref(req, &m, 1);
  1220. spin_unlock_irq(&resource->req_lock);
  1221. /* Even though above is a kref_put(), this is safe.
  1222. * As long as we still need to submit our private bio,
  1223. * we hold a completion ref, and the request cannot disappear.
  1224. * If however this request did not even have a private bio to submit
  1225. * (e.g. remote read), req may already be invalid now.
  1226. * That's why we cannot check on req->private_bio. */
  1227. if (submit_private_bio)
  1228. drbd_submit_req_private_bio(req);
  1229. if (m.bio)
  1230. complete_master_bio(device, &m);
  1231. }
  1232. void __drbd_make_request(struct drbd_device *device, struct bio *bio)
  1233. {
  1234. struct drbd_request *req = drbd_request_prepare(device, bio);
  1235. if (IS_ERR_OR_NULL(req))
  1236. return;
  1237. drbd_send_and_submit(device, req);
  1238. }
  1239. static void submit_fast_path(struct drbd_device *device, struct list_head *incoming)
  1240. {
  1241. struct blk_plug plug;
  1242. struct drbd_request *req, *tmp;
  1243. blk_start_plug(&plug);
  1244. list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
  1245. const int rw = bio_data_dir(req->master_bio);
  1246. if (rw == WRITE /* rw != WRITE should not even end up here! */
  1247. && req->private_bio && req->i.size
  1248. && !test_bit(AL_SUSPENDED, &device->flags)) {
  1249. if (!drbd_al_begin_io_fastpath(device, &req->i))
  1250. continue;
  1251. req->rq_state |= RQ_IN_ACT_LOG;
  1252. req->in_actlog_jif = jiffies;
  1253. atomic_dec(&device->ap_actlog_cnt);
  1254. }
  1255. list_del_init(&req->tl_requests);
  1256. drbd_send_and_submit(device, req);
  1257. }
  1258. blk_finish_plug(&plug);
  1259. }
  1260. static bool prepare_al_transaction_nonblock(struct drbd_device *device,
  1261. struct list_head *incoming,
  1262. struct list_head *pending,
  1263. struct list_head *later)
  1264. {
  1265. struct drbd_request *req;
  1266. int wake = 0;
  1267. int err;
  1268. spin_lock_irq(&device->al_lock);
  1269. while ((req = list_first_entry_or_null(incoming, struct drbd_request, tl_requests))) {
  1270. err = drbd_al_begin_io_nonblock(device, &req->i);
  1271. if (err == -ENOBUFS)
  1272. break;
  1273. if (err == -EBUSY)
  1274. wake = 1;
  1275. if (err)
  1276. list_move_tail(&req->tl_requests, later);
  1277. else
  1278. list_move_tail(&req->tl_requests, pending);
  1279. }
  1280. spin_unlock_irq(&device->al_lock);
  1281. if (wake)
  1282. wake_up(&device->al_wait);
  1283. return !list_empty(pending);
  1284. }
  1285. static void send_and_submit_pending(struct drbd_device *device, struct list_head *pending)
  1286. {
  1287. struct blk_plug plug;
  1288. struct drbd_request *req;
  1289. blk_start_plug(&plug);
  1290. while ((req = list_first_entry_or_null(pending, struct drbd_request, tl_requests))) {
  1291. req->rq_state |= RQ_IN_ACT_LOG;
  1292. req->in_actlog_jif = jiffies;
  1293. atomic_dec(&device->ap_actlog_cnt);
  1294. list_del_init(&req->tl_requests);
  1295. drbd_send_and_submit(device, req);
  1296. }
  1297. blk_finish_plug(&plug);
  1298. }
  1299. void do_submit(struct work_struct *ws)
  1300. {
  1301. struct drbd_device *device = container_of(ws, struct drbd_device, submit.worker);
  1302. LIST_HEAD(incoming); /* from drbd_make_request() */
  1303. LIST_HEAD(pending); /* to be submitted after next AL-transaction commit */
  1304. LIST_HEAD(busy); /* blocked by resync requests */
  1305. /* grab new incoming requests */
  1306. spin_lock_irq(&device->resource->req_lock);
  1307. list_splice_tail_init(&device->submit.writes, &incoming);
  1308. spin_unlock_irq(&device->resource->req_lock);
  1309. for (;;) {
  1310. DEFINE_WAIT(wait);
  1311. /* move used-to-be-busy back to front of incoming */
  1312. list_splice_init(&busy, &incoming);
  1313. submit_fast_path(device, &incoming);
  1314. if (list_empty(&incoming))
  1315. break;
  1316. for (;;) {
  1317. prepare_to_wait(&device->al_wait, &wait, TASK_UNINTERRUPTIBLE);
  1318. list_splice_init(&busy, &incoming);
  1319. prepare_al_transaction_nonblock(device, &incoming, &pending, &busy);
  1320. if (!list_empty(&pending))
  1321. break;
  1322. schedule();
  1323. /* If all currently "hot" activity log extents are kept busy by
  1324. * incoming requests, we still must not totally starve new
  1325. * requests to "cold" extents.
  1326. * Something left on &incoming means there had not been
  1327. * enough update slots available, and the activity log
  1328. * has been marked as "starving".
  1329. *
  1330. * Try again now, without looking for new requests,
  1331. * effectively blocking all new requests until we made
  1332. * at least _some_ progress with what we currently have.
  1333. */
  1334. if (!list_empty(&incoming))
  1335. continue;
  1336. /* Nothing moved to pending, but nothing left
  1337. * on incoming: all moved to busy!
  1338. * Grab new and iterate. */
  1339. spin_lock_irq(&device->resource->req_lock);
  1340. list_splice_tail_init(&device->submit.writes, &incoming);
  1341. spin_unlock_irq(&device->resource->req_lock);
  1342. }
  1343. finish_wait(&device->al_wait, &wait);
  1344. /* If the transaction was full, before all incoming requests
  1345. * had been processed, skip ahead to commit, and iterate
  1346. * without splicing in more incoming requests from upper layers.
  1347. *
  1348. * Else, if all incoming have been processed,
  1349. * they have become either "pending" (to be submitted after
  1350. * next transaction commit) or "busy" (blocked by resync).
  1351. *
  1352. * Maybe more was queued, while we prepared the transaction?
  1353. * Try to stuff those into this transaction as well.
  1354. * Be strictly non-blocking here,
  1355. * we already have something to commit.
  1356. *
  1357. * Commit if we don't make any more progres.
  1358. */
  1359. while (list_empty(&incoming)) {
  1360. LIST_HEAD(more_pending);
  1361. LIST_HEAD(more_incoming);
  1362. bool made_progress;
  1363. /* It is ok to look outside the lock,
  1364. * it's only an optimization anyways */
  1365. if (list_empty(&device->submit.writes))
  1366. break;
  1367. spin_lock_irq(&device->resource->req_lock);
  1368. list_splice_tail_init(&device->submit.writes, &more_incoming);
  1369. spin_unlock_irq(&device->resource->req_lock);
  1370. if (list_empty(&more_incoming))
  1371. break;
  1372. made_progress = prepare_al_transaction_nonblock(device, &more_incoming, &more_pending, &busy);
  1373. list_splice_tail_init(&more_pending, &pending);
  1374. list_splice_tail_init(&more_incoming, &incoming);
  1375. if (!made_progress)
  1376. break;
  1377. }
  1378. drbd_al_begin_io_commit(device);
  1379. send_and_submit_pending(device, &pending);
  1380. }
  1381. }
  1382. void drbd_submit_bio(struct bio *bio)
  1383. {
  1384. struct drbd_device *device = bio->bi_bdev->bd_disk->private_data;
  1385. bio = bio_split_to_limits(bio);
  1386. if (!bio)
  1387. return;
  1388. /*
  1389. * what we "blindly" assume:
  1390. */
  1391. D_ASSERT(device, IS_ALIGNED(bio->bi_iter.bi_size, 512));
  1392. inc_ap_bio(device);
  1393. __drbd_make_request(device, bio);
  1394. }
  1395. static bool net_timeout_reached(struct drbd_request *net_req,
  1396. struct drbd_connection *connection,
  1397. unsigned long now, unsigned long ent,
  1398. unsigned int ko_count, unsigned int timeout)
  1399. {
  1400. struct drbd_device *device = net_req->device;
  1401. if (!time_after(now, net_req->pre_send_jif + ent))
  1402. return false;
  1403. if (time_in_range(now, connection->last_reconnect_jif, connection->last_reconnect_jif + ent))
  1404. return false;
  1405. if (net_req->rq_state & RQ_NET_PENDING) {
  1406. drbd_warn(device, "Remote failed to finish a request within %ums > ko-count (%u) * timeout (%u * 0.1s)\n",
  1407. jiffies_to_msecs(now - net_req->pre_send_jif), ko_count, timeout);
  1408. return true;
  1409. }
  1410. /* We received an ACK already (or are using protocol A),
  1411. * but are waiting for the epoch closing barrier ack.
  1412. * Check if we sent the barrier already. We should not blame the peer
  1413. * for being unresponsive, if we did not even ask it yet. */
  1414. if (net_req->epoch == connection->send.current_epoch_nr) {
  1415. drbd_warn(device,
  1416. "We did not send a P_BARRIER for %ums > ko-count (%u) * timeout (%u * 0.1s); drbd kernel thread blocked?\n",
  1417. jiffies_to_msecs(now - net_req->pre_send_jif), ko_count, timeout);
  1418. return false;
  1419. }
  1420. /* Worst case: we may have been blocked for whatever reason, then
  1421. * suddenly are able to send a lot of requests (and epoch separating
  1422. * barriers) in quick succession.
  1423. * The timestamp of the net_req may be much too old and not correspond
  1424. * to the sending time of the relevant unack'ed barrier packet, so
  1425. * would trigger a spurious timeout. The latest barrier packet may
  1426. * have a too recent timestamp to trigger the timeout, potentially miss
  1427. * a timeout. Right now we don't have a place to conveniently store
  1428. * these timestamps.
  1429. * But in this particular situation, the application requests are still
  1430. * completed to upper layers, DRBD should still "feel" responsive.
  1431. * No need yet to kill this connection, it may still recover.
  1432. * If not, eventually we will have queued enough into the network for
  1433. * us to block. From that point of view, the timestamp of the last sent
  1434. * barrier packet is relevant enough.
  1435. */
  1436. if (time_after(now, connection->send.last_sent_barrier_jif + ent)) {
  1437. drbd_warn(device, "Remote failed to answer a P_BARRIER (sent at %lu jif; now=%lu jif) within %ums > ko-count (%u) * timeout (%u * 0.1s)\n",
  1438. connection->send.last_sent_barrier_jif, now,
  1439. jiffies_to_msecs(now - connection->send.last_sent_barrier_jif), ko_count, timeout);
  1440. return true;
  1441. }
  1442. return false;
  1443. }
  1444. /* A request is considered timed out, if
  1445. * - we have some effective timeout from the configuration,
  1446. * with some state restrictions applied,
  1447. * - the oldest request is waiting for a response from the network
  1448. * resp. the local disk,
  1449. * - the oldest request is in fact older than the effective timeout,
  1450. * - the connection was established (resp. disk was attached)
  1451. * for longer than the timeout already.
  1452. * Note that for 32bit jiffies and very stable connections/disks,
  1453. * we may have a wrap around, which is catched by
  1454. * !time_in_range(now, last_..._jif, last_..._jif + timeout).
  1455. *
  1456. * Side effect: once per 32bit wrap-around interval, which means every
  1457. * ~198 days with 250 HZ, we have a window where the timeout would need
  1458. * to expire twice (worst case) to become effective. Good enough.
  1459. */
  1460. void request_timer_fn(struct timer_list *t)
  1461. {
  1462. struct drbd_device *device = timer_container_of(device, t,
  1463. request_timer);
  1464. struct drbd_connection *connection = first_peer_device(device)->connection;
  1465. struct drbd_request *req_read, *req_write, *req_peer; /* oldest request */
  1466. struct net_conf *nc;
  1467. unsigned long oldest_submit_jif;
  1468. unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
  1469. unsigned long now;
  1470. unsigned int ko_count = 0, timeout = 0;
  1471. rcu_read_lock();
  1472. nc = rcu_dereference(connection->net_conf);
  1473. if (nc && device->state.conn >= C_WF_REPORT_PARAMS) {
  1474. ko_count = nc->ko_count;
  1475. timeout = nc->timeout;
  1476. }
  1477. if (get_ldev(device)) { /* implicit state.disk >= D_INCONSISTENT */
  1478. dt = rcu_dereference(device->ldev->disk_conf)->disk_timeout * HZ / 10;
  1479. put_ldev(device);
  1480. }
  1481. rcu_read_unlock();
  1482. ent = timeout * HZ/10 * ko_count;
  1483. et = min_not_zero(dt, ent);
  1484. if (!et)
  1485. return; /* Recurring timer stopped */
  1486. now = jiffies;
  1487. nt = now + et;
  1488. spin_lock_irq(&device->resource->req_lock);
  1489. req_read = list_first_entry_or_null(&device->pending_completion[0], struct drbd_request, req_pending_local);
  1490. req_write = list_first_entry_or_null(&device->pending_completion[1], struct drbd_request, req_pending_local);
  1491. /* maybe the oldest request waiting for the peer is in fact still
  1492. * blocking in tcp sendmsg. That's ok, though, that's handled via the
  1493. * socket send timeout, requesting a ping, and bumping ko-count in
  1494. * we_should_drop_the_connection().
  1495. */
  1496. /* check the oldest request we did successfully sent,
  1497. * but which is still waiting for an ACK. */
  1498. req_peer = connection->req_ack_pending;
  1499. /* if we don't have such request (e.g. protocoll A)
  1500. * check the oldest requests which is still waiting on its epoch
  1501. * closing barrier ack. */
  1502. if (!req_peer)
  1503. req_peer = connection->req_not_net_done;
  1504. /* evaluate the oldest peer request only in one timer! */
  1505. if (req_peer && req_peer->device != device)
  1506. req_peer = NULL;
  1507. /* do we have something to evaluate? */
  1508. if (req_peer == NULL && req_write == NULL && req_read == NULL)
  1509. goto out;
  1510. oldest_submit_jif =
  1511. (req_write && req_read)
  1512. ? ( time_before(req_write->pre_submit_jif, req_read->pre_submit_jif)
  1513. ? req_write->pre_submit_jif : req_read->pre_submit_jif )
  1514. : req_write ? req_write->pre_submit_jif
  1515. : req_read ? req_read->pre_submit_jif : now;
  1516. if (ent && req_peer && net_timeout_reached(req_peer, connection, now, ent, ko_count, timeout))
  1517. _conn_request_state(connection, NS(conn, C_TIMEOUT), CS_VERBOSE | CS_HARD);
  1518. if (dt && oldest_submit_jif != now &&
  1519. time_after(now, oldest_submit_jif + dt) &&
  1520. !time_in_range(now, device->last_reattach_jif, device->last_reattach_jif + dt)) {
  1521. drbd_warn(device, "Local backing device failed to meet the disk-timeout\n");
  1522. __drbd_chk_io_error(device, DRBD_FORCE_DETACH);
  1523. }
  1524. /* Reschedule timer for the nearest not already expired timeout.
  1525. * Fallback to now + min(effective network timeout, disk timeout). */
  1526. ent = (ent && req_peer && time_before(now, req_peer->pre_send_jif + ent))
  1527. ? req_peer->pre_send_jif + ent : now + et;
  1528. dt = (dt && oldest_submit_jif != now && time_before(now, oldest_submit_jif + dt))
  1529. ? oldest_submit_jif + dt : now + et;
  1530. nt = time_before(ent, dt) ? ent : dt;
  1531. out:
  1532. spin_unlock_irq(&device->resource->req_lock);
  1533. mod_timer(&device->request_timer, nt);
  1534. }