drbd_req.c 51 KB

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