drbd_req.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. drbd_req.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/drbd.h>
  22. #include "drbd_int.h"
  23. #include "drbd_req.h"
  24. /* Update disk stats at start of I/O request */
  25. static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req, struct bio *bio)
  26. {
  27. const int rw = bio_data_dir(bio);
  28. int cpu;
  29. cpu = part_stat_lock();
  30. part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
  31. part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
  32. part_inc_in_flight(&mdev->vdisk->part0, rw);
  33. part_stat_unlock();
  34. }
  35. /* Update disk stats when completing request upwards */
  36. static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
  37. {
  38. int rw = bio_data_dir(req->master_bio);
  39. unsigned long duration = jiffies - req->start_time;
  40. int cpu;
  41. cpu = part_stat_lock();
  42. part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
  43. part_round_stats(cpu, &mdev->vdisk->part0);
  44. part_dec_in_flight(&mdev->vdisk->part0, rw);
  45. part_stat_unlock();
  46. }
  47. static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw)
  48. {
  49. const unsigned long s = req->rq_state;
  50. /* if it was a write, we may have to set the corresponding
  51. * bit(s) out-of-sync first. If it had a local part, we need to
  52. * release the reference to the activity log. */
  53. if (rw == WRITE) {
  54. /* remove it from the transfer log.
  55. * well, only if it had been there in the first
  56. * place... if it had not (local only or conflicting
  57. * and never sent), it should still be "empty" as
  58. * initialized in drbd_req_new(), so we can list_del() it
  59. * here unconditionally */
  60. list_del(&req->tl_requests);
  61. /* Set out-of-sync unless both OK flags are set
  62. * (local only or remote failed).
  63. * Other places where we set out-of-sync:
  64. * READ with local io-error */
  65. if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
  66. drbd_set_out_of_sync(mdev, req->sector, req->size);
  67. if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
  68. drbd_set_in_sync(mdev, req->sector, req->size);
  69. /* one might be tempted to move the drbd_al_complete_io
  70. * to the local io completion callback drbd_endio_pri.
  71. * but, if this was a mirror write, we may only
  72. * drbd_al_complete_io after this is RQ_NET_DONE,
  73. * otherwise the extent could be dropped from the al
  74. * before it has actually been written on the peer.
  75. * if we crash before our peer knows about the request,
  76. * but after the extent has been dropped from the al,
  77. * we would forget to resync the corresponding extent.
  78. */
  79. if (s & RQ_LOCAL_MASK) {
  80. if (get_ldev_if_state(mdev, D_FAILED)) {
  81. drbd_al_complete_io(mdev, req->sector);
  82. put_ldev(mdev);
  83. } else if (__ratelimit(&drbd_ratelimit_state)) {
  84. dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu), "
  85. "but my Disk seems to have failed :(\n",
  86. (unsigned long long) req->sector);
  87. }
  88. }
  89. }
  90. /* if it was a local io error, we want to notify our
  91. * peer about that, and see if we need to
  92. * detach the disk and stuff.
  93. * to avoid allocating some special work
  94. * struct, reuse the request. */
  95. /* THINK
  96. * why do we do this not when we detect the error,
  97. * but delay it until it is "done", i.e. possibly
  98. * until the next barrier ack? */
  99. if (rw == WRITE &&
  100. ((s & RQ_LOCAL_MASK) && !(s & RQ_LOCAL_OK))) {
  101. if (!(req->w.list.next == LIST_POISON1 ||
  102. list_empty(&req->w.list))) {
  103. /* DEBUG ASSERT only; if this triggers, we
  104. * probably corrupt the worker list here */
  105. dev_err(DEV, "req->w.list.next = %p\n", req->w.list.next);
  106. dev_err(DEV, "req->w.list.prev = %p\n", req->w.list.prev);
  107. }
  108. req->w.cb = w_io_error;
  109. drbd_queue_work(&mdev->data.work, &req->w);
  110. /* drbd_req_free() is done in w_io_error */
  111. } else {
  112. drbd_req_free(req);
  113. }
  114. }
  115. static void queue_barrier(struct drbd_conf *mdev)
  116. {
  117. struct drbd_tl_epoch *b;
  118. /* We are within the req_lock. Once we queued the barrier for sending,
  119. * we set the CREATE_BARRIER bit. It is cleared as soon as a new
  120. * barrier/epoch object is added. This is the only place this bit is
  121. * set. It indicates that the barrier for this epoch is already queued,
  122. * and no new epoch has been created yet. */
  123. if (test_bit(CREATE_BARRIER, &mdev->flags))
  124. return;
  125. b = mdev->newest_tle;
  126. b->w.cb = w_send_barrier;
  127. /* inc_ap_pending done here, so we won't
  128. * get imbalanced on connection loss.
  129. * dec_ap_pending will be done in got_BarrierAck
  130. * or (on connection loss) in tl_clear. */
  131. inc_ap_pending(mdev);
  132. drbd_queue_work(&mdev->data.work, &b->w);
  133. set_bit(CREATE_BARRIER, &mdev->flags);
  134. }
  135. static void _about_to_complete_local_write(struct drbd_conf *mdev,
  136. struct drbd_request *req)
  137. {
  138. const unsigned long s = req->rq_state;
  139. struct drbd_request *i;
  140. struct drbd_epoch_entry *e;
  141. struct hlist_node *n;
  142. struct hlist_head *slot;
  143. /* before we can signal completion to the upper layers,
  144. * we may need to close the current epoch */
  145. if (mdev->state.conn >= C_CONNECTED &&
  146. req->epoch == mdev->newest_tle->br_number)
  147. queue_barrier(mdev);
  148. /* we need to do the conflict detection stuff,
  149. * if we have the ee_hash (two_primaries) and
  150. * this has been on the network */
  151. if ((s & RQ_NET_DONE) && mdev->ee_hash != NULL) {
  152. const sector_t sector = req->sector;
  153. const int size = req->size;
  154. /* ASSERT:
  155. * there must be no conflicting requests, since
  156. * they must have been failed on the spot */
  157. #define OVERLAPS overlaps(sector, size, i->sector, i->size)
  158. slot = tl_hash_slot(mdev, sector);
  159. hlist_for_each_entry(i, n, slot, colision) {
  160. if (OVERLAPS) {
  161. dev_alert(DEV, "LOGIC BUG: completed: %p %llus +%u; "
  162. "other: %p %llus +%u\n",
  163. req, (unsigned long long)sector, size,
  164. i, (unsigned long long)i->sector, i->size);
  165. }
  166. }
  167. /* maybe "wake" those conflicting epoch entries
  168. * that wait for this request to finish.
  169. *
  170. * currently, there can be only _one_ such ee
  171. * (well, or some more, which would be pending
  172. * P_DISCARD_ACK not yet sent by the asender...),
  173. * since we block the receiver thread upon the
  174. * first conflict detection, which will wait on
  175. * misc_wait. maybe we want to assert that?
  176. *
  177. * anyways, if we found one,
  178. * we just have to do a wake_up. */
  179. #undef OVERLAPS
  180. #define OVERLAPS overlaps(sector, size, e->sector, e->size)
  181. slot = ee_hash_slot(mdev, req->sector);
  182. hlist_for_each_entry(e, n, slot, colision) {
  183. if (OVERLAPS) {
  184. wake_up(&mdev->misc_wait);
  185. break;
  186. }
  187. }
  188. }
  189. #undef OVERLAPS
  190. }
  191. void complete_master_bio(struct drbd_conf *mdev,
  192. struct bio_and_error *m)
  193. {
  194. bio_endio(m->bio, m->error);
  195. dec_ap_bio(mdev);
  196. }
  197. /* Helper for __req_mod().
  198. * Set m->bio to the master bio, if it is fit to be completed,
  199. * or leave it alone (it is initialized to NULL in __req_mod),
  200. * if it has already been completed, or cannot be completed yet.
  201. * If m->bio is set, the error status to be returned is placed in m->error.
  202. */
  203. void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m)
  204. {
  205. const unsigned long s = req->rq_state;
  206. struct drbd_conf *mdev = req->mdev;
  207. /* only WRITES may end up here without a master bio (on barrier ack) */
  208. int rw = req->master_bio ? bio_data_dir(req->master_bio) : WRITE;
  209. /* we must not complete the master bio, while it is
  210. * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
  211. * not yet acknowledged by the peer
  212. * not yet completed by the local io subsystem
  213. * these flags may get cleared in any order by
  214. * the worker,
  215. * the receiver,
  216. * the bio_endio completion callbacks.
  217. */
  218. if (s & RQ_NET_QUEUED)
  219. return;
  220. if (s & RQ_NET_PENDING)
  221. return;
  222. if (s & RQ_LOCAL_PENDING)
  223. return;
  224. if (req->master_bio) {
  225. /* this is data_received (remote read)
  226. * or protocol C P_WRITE_ACK
  227. * or protocol B P_RECV_ACK
  228. * or protocol A "handed_over_to_network" (SendAck)
  229. * or canceled or failed,
  230. * or killed from the transfer log due to connection loss.
  231. */
  232. /*
  233. * figure out whether to report success or failure.
  234. *
  235. * report success when at least one of the operations succeeded.
  236. * or, to put the other way,
  237. * only report failure, when both operations failed.
  238. *
  239. * what to do about the failures is handled elsewhere.
  240. * what we need to do here is just: complete the master_bio.
  241. *
  242. * local completion error, if any, has been stored as ERR_PTR
  243. * in private_bio within drbd_endio_pri.
  244. */
  245. int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
  246. int error = PTR_ERR(req->private_bio);
  247. /* remove the request from the conflict detection
  248. * respective block_id verification hash */
  249. if (!hlist_unhashed(&req->colision))
  250. hlist_del(&req->colision);
  251. else
  252. D_ASSERT((s & RQ_NET_MASK) == 0);
  253. /* for writes we need to do some extra housekeeping */
  254. if (rw == WRITE)
  255. _about_to_complete_local_write(mdev, req);
  256. /* Update disk stats */
  257. _drbd_end_io_acct(mdev, req);
  258. m->error = ok ? 0 : (error ?: -EIO);
  259. m->bio = req->master_bio;
  260. req->master_bio = NULL;
  261. }
  262. if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) {
  263. /* this is disconnected (local only) operation,
  264. * or protocol C P_WRITE_ACK,
  265. * or protocol A or B P_BARRIER_ACK,
  266. * or killed from the transfer log due to connection loss. */
  267. _req_is_done(mdev, req, rw);
  268. }
  269. /* else: network part and not DONE yet. that is
  270. * protocol A or B, barrier ack still pending... */
  271. }
  272. /*
  273. * checks whether there was an overlapping request
  274. * or ee already registered.
  275. *
  276. * if so, return 1, in which case this request is completed on the spot,
  277. * without ever being submitted or send.
  278. *
  279. * return 0 if it is ok to submit this request.
  280. *
  281. * NOTE:
  282. * paranoia: assume something above us is broken, and issues different write
  283. * requests for the same block simultaneously...
  284. *
  285. * To ensure these won't be reordered differently on both nodes, resulting in
  286. * diverging data sets, we discard the later one(s). Not that this is supposed
  287. * to happen, but this is the rationale why we also have to check for
  288. * conflicting requests with local origin, and why we have to do so regardless
  289. * of whether we allowed multiple primaries.
  290. *
  291. * BTW, in case we only have one primary, the ee_hash is empty anyways, and the
  292. * second hlist_for_each_entry becomes a noop. This is even simpler than to
  293. * grab a reference on the net_conf, and check for the two_primaries flag...
  294. */
  295. static int _req_conflicts(struct drbd_request *req)
  296. {
  297. struct drbd_conf *mdev = req->mdev;
  298. const sector_t sector = req->sector;
  299. const int size = req->size;
  300. struct drbd_request *i;
  301. struct drbd_epoch_entry *e;
  302. struct hlist_node *n;
  303. struct hlist_head *slot;
  304. D_ASSERT(hlist_unhashed(&req->colision));
  305. if (!get_net_conf(mdev))
  306. return 0;
  307. /* BUG_ON */
  308. ERR_IF (mdev->tl_hash_s == 0)
  309. goto out_no_conflict;
  310. BUG_ON(mdev->tl_hash == NULL);
  311. #define OVERLAPS overlaps(i->sector, i->size, sector, size)
  312. slot = tl_hash_slot(mdev, sector);
  313. hlist_for_each_entry(i, n, slot, colision) {
  314. if (OVERLAPS) {
  315. dev_alert(DEV, "%s[%u] Concurrent local write detected! "
  316. "[DISCARD L] new: %llus +%u; "
  317. "pending: %llus +%u\n",
  318. current->comm, current->pid,
  319. (unsigned long long)sector, size,
  320. (unsigned long long)i->sector, i->size);
  321. goto out_conflict;
  322. }
  323. }
  324. if (mdev->ee_hash_s) {
  325. /* now, check for overlapping requests with remote origin */
  326. BUG_ON(mdev->ee_hash == NULL);
  327. #undef OVERLAPS
  328. #define OVERLAPS overlaps(e->sector, e->size, sector, size)
  329. slot = ee_hash_slot(mdev, sector);
  330. hlist_for_each_entry(e, n, slot, colision) {
  331. if (OVERLAPS) {
  332. dev_alert(DEV, "%s[%u] Concurrent remote write detected!"
  333. " [DISCARD L] new: %llus +%u; "
  334. "pending: %llus +%u\n",
  335. current->comm, current->pid,
  336. (unsigned long long)sector, size,
  337. (unsigned long long)e->sector, e->size);
  338. goto out_conflict;
  339. }
  340. }
  341. }
  342. #undef OVERLAPS
  343. out_no_conflict:
  344. /* this is like it should be, and what we expected.
  345. * our users do behave after all... */
  346. put_net_conf(mdev);
  347. return 0;
  348. out_conflict:
  349. put_net_conf(mdev);
  350. return 1;
  351. }
  352. /* obviously this could be coded as many single functions
  353. * instead of one huge switch,
  354. * or by putting the code directly in the respective locations
  355. * (as it has been before).
  356. *
  357. * but having it this way
  358. * enforces that it is all in this one place, where it is easier to audit,
  359. * it makes it obvious that whatever "event" "happens" to a request should
  360. * happen "atomically" within the req_lock,
  361. * and it enforces that we have to think in a very structured manner
  362. * about the "events" that may happen to a request during its life time ...
  363. */
  364. void __req_mod(struct drbd_request *req, enum drbd_req_event what,
  365. struct bio_and_error *m)
  366. {
  367. struct drbd_conf *mdev = req->mdev;
  368. m->bio = NULL;
  369. switch (what) {
  370. default:
  371. dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
  372. break;
  373. /* does not happen...
  374. * initialization done in drbd_req_new
  375. case created:
  376. break;
  377. */
  378. case to_be_send: /* via network */
  379. /* reached via drbd_make_request_common
  380. * and from w_read_retry_remote */
  381. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  382. req->rq_state |= RQ_NET_PENDING;
  383. inc_ap_pending(mdev);
  384. break;
  385. case to_be_submitted: /* locally */
  386. /* reached via drbd_make_request_common */
  387. D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
  388. req->rq_state |= RQ_LOCAL_PENDING;
  389. break;
  390. case completed_ok:
  391. if (bio_data_dir(req->master_bio) == WRITE)
  392. mdev->writ_cnt += req->size>>9;
  393. else
  394. mdev->read_cnt += req->size>>9;
  395. req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
  396. req->rq_state &= ~RQ_LOCAL_PENDING;
  397. _req_may_be_done(req, m);
  398. put_ldev(mdev);
  399. break;
  400. case write_completed_with_error:
  401. req->rq_state |= RQ_LOCAL_COMPLETED;
  402. req->rq_state &= ~RQ_LOCAL_PENDING;
  403. dev_alert(DEV, "Local WRITE failed sec=%llus size=%u\n",
  404. (unsigned long long)req->sector, req->size);
  405. /* and now: check how to handle local io error. */
  406. __drbd_chk_io_error(mdev, FALSE);
  407. _req_may_be_done(req, m);
  408. put_ldev(mdev);
  409. break;
  410. case read_ahead_completed_with_error:
  411. /* it is legal to fail READA */
  412. req->rq_state |= RQ_LOCAL_COMPLETED;
  413. req->rq_state &= ~RQ_LOCAL_PENDING;
  414. _req_may_be_done(req, m);
  415. put_ldev(mdev);
  416. break;
  417. case read_completed_with_error:
  418. drbd_set_out_of_sync(mdev, req->sector, req->size);
  419. req->rq_state |= RQ_LOCAL_COMPLETED;
  420. req->rq_state &= ~RQ_LOCAL_PENDING;
  421. dev_alert(DEV, "Local READ failed sec=%llus size=%u\n",
  422. (unsigned long long)req->sector, req->size);
  423. /* _req_mod(req,to_be_send); oops, recursion... */
  424. D_ASSERT(!(req->rq_state & RQ_NET_MASK));
  425. req->rq_state |= RQ_NET_PENDING;
  426. inc_ap_pending(mdev);
  427. __drbd_chk_io_error(mdev, FALSE);
  428. put_ldev(mdev);
  429. /* NOTE: if we have no connection,
  430. * or know the peer has no good data either,
  431. * then we don't actually need to "queue_for_net_read",
  432. * but we do so anyways, since the drbd_io_error()
  433. * and the potential state change to "Diskless"
  434. * needs to be done from process context */
  435. /* fall through: _req_mod(req,queue_for_net_read); */
  436. case queue_for_net_read:
  437. /* READ or READA, and
  438. * no local disk,
  439. * or target area marked as invalid,
  440. * or just got an io-error. */
  441. /* from drbd_make_request_common
  442. * or from bio_endio during read io-error recovery */
  443. /* so we can verify the handle in the answer packet
  444. * corresponding hlist_del is in _req_may_be_done() */
  445. hlist_add_head(&req->colision, ar_hash_slot(mdev, req->sector));
  446. set_bit(UNPLUG_REMOTE, &mdev->flags);
  447. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  448. req->rq_state |= RQ_NET_QUEUED;
  449. req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
  450. ? w_read_retry_remote
  451. : w_send_read_req;
  452. drbd_queue_work(&mdev->data.work, &req->w);
  453. break;
  454. case queue_for_net_write:
  455. /* assert something? */
  456. /* from drbd_make_request_common only */
  457. hlist_add_head(&req->colision, tl_hash_slot(mdev, req->sector));
  458. /* corresponding hlist_del is in _req_may_be_done() */
  459. /* NOTE
  460. * In case the req ended up on the transfer log before being
  461. * queued on the worker, it could lead to this request being
  462. * missed during cleanup after connection loss.
  463. * So we have to do both operations here,
  464. * within the same lock that protects the transfer log.
  465. *
  466. * _req_add_to_epoch(req); this has to be after the
  467. * _maybe_start_new_epoch(req); which happened in
  468. * drbd_make_request_common, because we now may set the bit
  469. * again ourselves to close the current epoch.
  470. *
  471. * Add req to the (now) current epoch (barrier). */
  472. /* otherwise we may lose an unplug, which may cause some remote
  473. * io-scheduler timeout to expire, increasing maximum latency,
  474. * hurting performance. */
  475. set_bit(UNPLUG_REMOTE, &mdev->flags);
  476. /* see drbd_make_request_common,
  477. * just after it grabs the req_lock */
  478. D_ASSERT(test_bit(CREATE_BARRIER, &mdev->flags) == 0);
  479. req->epoch = mdev->newest_tle->br_number;
  480. list_add_tail(&req->tl_requests,
  481. &mdev->newest_tle->requests);
  482. /* increment size of current epoch */
  483. mdev->newest_tle->n_req++;
  484. /* queue work item to send data */
  485. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  486. req->rq_state |= RQ_NET_QUEUED;
  487. req->w.cb = w_send_dblock;
  488. drbd_queue_work(&mdev->data.work, &req->w);
  489. /* close the epoch, in case it outgrew the limit */
  490. if (mdev->newest_tle->n_req >= mdev->net_conf->max_epoch_size)
  491. queue_barrier(mdev);
  492. break;
  493. case send_canceled:
  494. /* treat it the same */
  495. case send_failed:
  496. /* real cleanup will be done from tl_clear. just update flags
  497. * so it is no longer marked as on the worker queue */
  498. req->rq_state &= ~RQ_NET_QUEUED;
  499. /* if we did it right, tl_clear should be scheduled only after
  500. * this, so this should not be necessary! */
  501. _req_may_be_done(req, m);
  502. break;
  503. case handed_over_to_network:
  504. /* assert something? */
  505. if (bio_data_dir(req->master_bio) == WRITE &&
  506. mdev->net_conf->wire_protocol == DRBD_PROT_A) {
  507. /* this is what is dangerous about protocol A:
  508. * pretend it was successfully written on the peer. */
  509. if (req->rq_state & RQ_NET_PENDING) {
  510. dec_ap_pending(mdev);
  511. req->rq_state &= ~RQ_NET_PENDING;
  512. req->rq_state |= RQ_NET_OK;
  513. } /* else: neg-ack was faster... */
  514. /* it is still not yet RQ_NET_DONE until the
  515. * corresponding epoch barrier got acked as well,
  516. * so we know what to dirty on connection loss */
  517. }
  518. req->rq_state &= ~RQ_NET_QUEUED;
  519. req->rq_state |= RQ_NET_SENT;
  520. /* because _drbd_send_zc_bio could sleep, and may want to
  521. * dereference the bio even after the "write_acked_by_peer" and
  522. * "completed_ok" events came in, once we return from
  523. * _drbd_send_zc_bio (drbd_send_dblock), we have to check
  524. * whether it is done already, and end it. */
  525. _req_may_be_done(req, m);
  526. break;
  527. case connection_lost_while_pending:
  528. /* transfer log cleanup after connection loss */
  529. /* assert something? */
  530. if (req->rq_state & RQ_NET_PENDING)
  531. dec_ap_pending(mdev);
  532. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  533. req->rq_state |= RQ_NET_DONE;
  534. /* if it is still queued, we may not complete it here.
  535. * it will be canceled soon. */
  536. if (!(req->rq_state & RQ_NET_QUEUED))
  537. _req_may_be_done(req, m);
  538. break;
  539. case write_acked_by_peer_and_sis:
  540. req->rq_state |= RQ_NET_SIS;
  541. case conflict_discarded_by_peer:
  542. /* for discarded conflicting writes of multiple primaries,
  543. * there is no need to keep anything in the tl, potential
  544. * node crashes are covered by the activity log. */
  545. if (what == conflict_discarded_by_peer)
  546. dev_alert(DEV, "Got DiscardAck packet %llus +%u!"
  547. " DRBD is not a random data generator!\n",
  548. (unsigned long long)req->sector, req->size);
  549. req->rq_state |= RQ_NET_DONE;
  550. /* fall through */
  551. case write_acked_by_peer:
  552. /* protocol C; successfully written on peer.
  553. * Nothing to do here.
  554. * We want to keep the tl in place for all protocols, to cater
  555. * for volatile write-back caches on lower level devices.
  556. *
  557. * A barrier request is expected to have forced all prior
  558. * requests onto stable storage, so completion of a barrier
  559. * request could set NET_DONE right here, and not wait for the
  560. * P_BARRIER_ACK, but that is an unnecessary optimization. */
  561. /* this makes it effectively the same as for: */
  562. case recv_acked_by_peer:
  563. /* protocol B; pretends to be successfully written on peer.
  564. * see also notes above in handed_over_to_network about
  565. * protocol != C */
  566. req->rq_state |= RQ_NET_OK;
  567. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  568. dec_ap_pending(mdev);
  569. req->rq_state &= ~RQ_NET_PENDING;
  570. _req_may_be_done(req, m);
  571. break;
  572. case neg_acked:
  573. /* assert something? */
  574. if (req->rq_state & RQ_NET_PENDING)
  575. dec_ap_pending(mdev);
  576. req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
  577. req->rq_state |= RQ_NET_DONE;
  578. _req_may_be_done(req, m);
  579. /* else: done by handed_over_to_network */
  580. break;
  581. case barrier_acked:
  582. if (req->rq_state & RQ_NET_PENDING) {
  583. /* barrier came in before all requests have been acked.
  584. * this is bad, because if the connection is lost now,
  585. * we won't be able to clean them up... */
  586. dev_err(DEV, "FIXME (barrier_acked but pending)\n");
  587. list_move(&req->tl_requests, &mdev->out_of_sequence_requests);
  588. }
  589. D_ASSERT(req->rq_state & RQ_NET_SENT);
  590. req->rq_state |= RQ_NET_DONE;
  591. _req_may_be_done(req, m);
  592. break;
  593. case data_received:
  594. D_ASSERT(req->rq_state & RQ_NET_PENDING);
  595. dec_ap_pending(mdev);
  596. req->rq_state &= ~RQ_NET_PENDING;
  597. req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
  598. _req_may_be_done(req, m);
  599. break;
  600. };
  601. }
  602. /* we may do a local read if:
  603. * - we are consistent (of course),
  604. * - or we are generally inconsistent,
  605. * BUT we are still/already IN SYNC for this area.
  606. * since size may be bigger than BM_BLOCK_SIZE,
  607. * we may need to check several bits.
  608. */
  609. static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
  610. {
  611. unsigned long sbnr, ebnr;
  612. sector_t esector, nr_sectors;
  613. if (mdev->state.disk == D_UP_TO_DATE)
  614. return 1;
  615. if (mdev->state.disk >= D_OUTDATED)
  616. return 0;
  617. if (mdev->state.disk < D_INCONSISTENT)
  618. return 0;
  619. /* state.disk == D_INCONSISTENT We will have a look at the BitMap */
  620. nr_sectors = drbd_get_capacity(mdev->this_bdev);
  621. esector = sector + (size >> 9) - 1;
  622. D_ASSERT(sector < nr_sectors);
  623. D_ASSERT(esector < nr_sectors);
  624. sbnr = BM_SECT_TO_BIT(sector);
  625. ebnr = BM_SECT_TO_BIT(esector);
  626. return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr);
  627. }
  628. static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio)
  629. {
  630. const int rw = bio_rw(bio);
  631. const int size = bio->bi_size;
  632. const sector_t sector = bio->bi_sector;
  633. struct drbd_tl_epoch *b = NULL;
  634. struct drbd_request *req;
  635. int local, remote;
  636. int err = -EIO;
  637. int ret = 0;
  638. /* allocate outside of all locks; */
  639. req = drbd_req_new(mdev, bio);
  640. if (!req) {
  641. dec_ap_bio(mdev);
  642. /* only pass the error to the upper layers.
  643. * if user cannot handle io errors, that's not our business. */
  644. dev_err(DEV, "could not kmalloc() req\n");
  645. bio_endio(bio, -ENOMEM);
  646. return 0;
  647. }
  648. local = get_ldev(mdev);
  649. if (!local) {
  650. bio_put(req->private_bio); /* or we get a bio leak */
  651. req->private_bio = NULL;
  652. }
  653. if (rw == WRITE) {
  654. remote = 1;
  655. } else {
  656. /* READ || READA */
  657. if (local) {
  658. if (!drbd_may_do_local_read(mdev, sector, size)) {
  659. /* we could kick the syncer to
  660. * sync this extent asap, wait for
  661. * it, then continue locally.
  662. * Or just issue the request remotely.
  663. */
  664. local = 0;
  665. bio_put(req->private_bio);
  666. req->private_bio = NULL;
  667. put_ldev(mdev);
  668. }
  669. }
  670. remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
  671. }
  672. /* If we have a disk, but a READA request is mapped to remote,
  673. * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
  674. * Just fail that READA request right here.
  675. *
  676. * THINK: maybe fail all READA when not local?
  677. * or make this configurable...
  678. * if network is slow, READA won't do any good.
  679. */
  680. if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
  681. err = -EWOULDBLOCK;
  682. goto fail_and_free_req;
  683. }
  684. /* For WRITES going to the local disk, grab a reference on the target
  685. * extent. This waits for any resync activity in the corresponding
  686. * resync extent to finish, and, if necessary, pulls in the target
  687. * extent into the activity log, which involves further disk io because
  688. * of transactional on-disk meta data updates. */
  689. if (rw == WRITE && local)
  690. drbd_al_begin_io(mdev, sector);
  691. remote = remote && (mdev->state.pdsk == D_UP_TO_DATE ||
  692. (mdev->state.pdsk == D_INCONSISTENT &&
  693. mdev->state.conn >= C_CONNECTED));
  694. if (!(local || remote) && !mdev->state.susp) {
  695. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  696. goto fail_free_complete;
  697. }
  698. /* For WRITE request, we have to make sure that we have an
  699. * unused_spare_tle, in case we need to start a new epoch.
  700. * I try to be smart and avoid to pre-allocate always "just in case",
  701. * but there is a race between testing the bit and pointer outside the
  702. * spinlock, and grabbing the spinlock.
  703. * if we lost that race, we retry. */
  704. if (rw == WRITE && remote &&
  705. mdev->unused_spare_tle == NULL &&
  706. test_bit(CREATE_BARRIER, &mdev->flags)) {
  707. allocate_barrier:
  708. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
  709. if (!b) {
  710. dev_err(DEV, "Failed to alloc barrier.\n");
  711. err = -ENOMEM;
  712. goto fail_free_complete;
  713. }
  714. }
  715. /* GOOD, everything prepared, grab the spin_lock */
  716. spin_lock_irq(&mdev->req_lock);
  717. if (mdev->state.susp) {
  718. /* If we got suspended, use the retry mechanism of
  719. generic_make_request() to restart processing of this
  720. bio. In the next call to drbd_make_request_26
  721. we sleep in inc_ap_bio() */
  722. ret = 1;
  723. spin_unlock_irq(&mdev->req_lock);
  724. goto fail_free_complete;
  725. }
  726. if (remote) {
  727. remote = (mdev->state.pdsk == D_UP_TO_DATE ||
  728. (mdev->state.pdsk == D_INCONSISTENT &&
  729. mdev->state.conn >= C_CONNECTED));
  730. if (!remote)
  731. dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
  732. if (!(local || remote)) {
  733. dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
  734. spin_unlock_irq(&mdev->req_lock);
  735. goto fail_free_complete;
  736. }
  737. }
  738. if (b && mdev->unused_spare_tle == NULL) {
  739. mdev->unused_spare_tle = b;
  740. b = NULL;
  741. }
  742. if (rw == WRITE && remote &&
  743. mdev->unused_spare_tle == NULL &&
  744. test_bit(CREATE_BARRIER, &mdev->flags)) {
  745. /* someone closed the current epoch
  746. * while we were grabbing the spinlock */
  747. spin_unlock_irq(&mdev->req_lock);
  748. goto allocate_barrier;
  749. }
  750. /* Update disk stats */
  751. _drbd_start_io_acct(mdev, req, bio);
  752. /* _maybe_start_new_epoch(mdev);
  753. * If we need to generate a write barrier packet, we have to add the
  754. * new epoch (barrier) object, and queue the barrier packet for sending,
  755. * and queue the req's data after it _within the same lock_, otherwise
  756. * we have race conditions were the reorder domains could be mixed up.
  757. *
  758. * Even read requests may start a new epoch and queue the corresponding
  759. * barrier packet. To get the write ordering right, we only have to
  760. * make sure that, if this is a write request and it triggered a
  761. * barrier packet, this request is queued within the same spinlock. */
  762. if (remote && mdev->unused_spare_tle &&
  763. test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
  764. _tl_add_barrier(mdev, mdev->unused_spare_tle);
  765. mdev->unused_spare_tle = NULL;
  766. } else {
  767. D_ASSERT(!(remote && rw == WRITE &&
  768. test_bit(CREATE_BARRIER, &mdev->flags)));
  769. }
  770. /* NOTE
  771. * Actually, 'local' may be wrong here already, since we may have failed
  772. * to write to the meta data, and may become wrong anytime because of
  773. * local io-error for some other request, which would lead to us
  774. * "detaching" the local disk.
  775. *
  776. * 'remote' may become wrong any time because the network could fail.
  777. *
  778. * This is a harmless race condition, though, since it is handled
  779. * correctly at the appropriate places; so it just defers the failure
  780. * of the respective operation.
  781. */
  782. /* mark them early for readability.
  783. * this just sets some state flags. */
  784. if (remote)
  785. _req_mod(req, to_be_send);
  786. if (local)
  787. _req_mod(req, to_be_submitted);
  788. /* check this request on the collision detection hash tables.
  789. * if we have a conflict, just complete it here.
  790. * THINK do we want to check reads, too? (I don't think so...) */
  791. if (rw == WRITE && _req_conflicts(req)) {
  792. /* this is a conflicting request.
  793. * even though it may have been only _partially_
  794. * overlapping with one of the currently pending requests,
  795. * without even submitting or sending it, we will
  796. * pretend that it was successfully served right now.
  797. */
  798. if (local) {
  799. bio_put(req->private_bio);
  800. req->private_bio = NULL;
  801. drbd_al_complete_io(mdev, req->sector);
  802. put_ldev(mdev);
  803. local = 0;
  804. }
  805. if (remote)
  806. dec_ap_pending(mdev);
  807. _drbd_end_io_acct(mdev, req);
  808. /* THINK: do we want to fail it (-EIO), or pretend success? */
  809. bio_endio(req->master_bio, 0);
  810. req->master_bio = NULL;
  811. dec_ap_bio(mdev);
  812. drbd_req_free(req);
  813. remote = 0;
  814. }
  815. /* NOTE remote first: to get the concurrent write detection right,
  816. * we must register the request before start of local IO. */
  817. if (remote) {
  818. /* either WRITE and C_CONNECTED,
  819. * or READ, and no local disk,
  820. * or READ, but not in sync.
  821. */
  822. _req_mod(req, (rw == WRITE)
  823. ? queue_for_net_write
  824. : queue_for_net_read);
  825. }
  826. spin_unlock_irq(&mdev->req_lock);
  827. kfree(b); /* if someone else has beaten us to it... */
  828. if (local) {
  829. req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
  830. if (FAULT_ACTIVE(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
  831. : rw == READ ? DRBD_FAULT_DT_RD
  832. : DRBD_FAULT_DT_RA))
  833. bio_endio(req->private_bio, -EIO);
  834. else
  835. generic_make_request(req->private_bio);
  836. }
  837. /* we need to plug ALWAYS since we possibly need to kick lo_dev.
  838. * we plug after submit, so we won't miss an unplug event */
  839. drbd_plug_device(mdev);
  840. return 0;
  841. fail_free_complete:
  842. if (rw == WRITE && local)
  843. drbd_al_complete_io(mdev, sector);
  844. fail_and_free_req:
  845. if (local) {
  846. bio_put(req->private_bio);
  847. req->private_bio = NULL;
  848. put_ldev(mdev);
  849. }
  850. if (!ret)
  851. bio_endio(bio, err);
  852. drbd_req_free(req);
  853. dec_ap_bio(mdev);
  854. kfree(b);
  855. return ret;
  856. }
  857. /* helper function for drbd_make_request
  858. * if we can determine just by the mdev (state) that this request will fail,
  859. * return 1
  860. * otherwise return 0
  861. */
  862. static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
  863. {
  864. if (mdev->state.role != R_PRIMARY &&
  865. (!allow_oos || is_write)) {
  866. if (__ratelimit(&drbd_ratelimit_state)) {
  867. dev_err(DEV, "Process %s[%u] tried to %s; "
  868. "since we are not in Primary state, "
  869. "we cannot allow this\n",
  870. current->comm, current->pid,
  871. is_write ? "WRITE" : "READ");
  872. }
  873. return 1;
  874. }
  875. /*
  876. * Paranoia: we might have been primary, but sync target, or
  877. * even diskless, then lost the connection.
  878. * This should have been handled (panic? suspend?) somewhere
  879. * else. But maybe it was not, so check again here.
  880. * Caution: as long as we do not have a read/write lock on mdev,
  881. * to serialize state changes, this is racy, since we may lose
  882. * the connection *after* we test for the cstate.
  883. */
  884. if (mdev->state.disk < D_UP_TO_DATE && mdev->state.pdsk < D_UP_TO_DATE) {
  885. if (__ratelimit(&drbd_ratelimit_state))
  886. dev_err(DEV, "Sorry, I have no access to good data anymore.\n");
  887. return 1;
  888. }
  889. return 0;
  890. }
  891. int drbd_make_request_26(struct request_queue *q, struct bio *bio)
  892. {
  893. unsigned int s_enr, e_enr;
  894. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  895. if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
  896. bio_endio(bio, -EPERM);
  897. return 0;
  898. }
  899. /* Reject barrier requests if we know the underlying device does
  900. * not support them.
  901. * XXX: Need to get this info from peer as well some how so we
  902. * XXX: reject if EITHER side/data/metadata area does not support them.
  903. *
  904. * because of those XXX, this is not yet enabled,
  905. * i.e. in drbd_init_set_defaults we set the NO_BARRIER_SUPP bit.
  906. */
  907. if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && test_bit(NO_BARRIER_SUPP, &mdev->flags))) {
  908. /* dev_warn(DEV, "Rejecting barrier request as underlying device does not support\n"); */
  909. bio_endio(bio, -EOPNOTSUPP);
  910. return 0;
  911. }
  912. /*
  913. * what we "blindly" assume:
  914. */
  915. D_ASSERT(bio->bi_size > 0);
  916. D_ASSERT((bio->bi_size & 0x1ff) == 0);
  917. D_ASSERT(bio->bi_idx == 0);
  918. /* to make some things easier, force alignment of requests within the
  919. * granularity of our hash tables */
  920. s_enr = bio->bi_sector >> HT_SHIFT;
  921. e_enr = (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT;
  922. if (likely(s_enr == e_enr)) {
  923. inc_ap_bio(mdev, 1);
  924. return drbd_make_request_common(mdev, bio);
  925. }
  926. /* can this bio be split generically?
  927. * Maybe add our own split-arbitrary-bios function. */
  928. if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_SEGMENT_SIZE) {
  929. /* rather error out here than BUG in bio_split */
  930. dev_err(DEV, "bio would need to, but cannot, be split: "
  931. "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
  932. bio->bi_vcnt, bio->bi_idx, bio->bi_size,
  933. (unsigned long long)bio->bi_sector);
  934. bio_endio(bio, -EINVAL);
  935. } else {
  936. /* This bio crosses some boundary, so we have to split it. */
  937. struct bio_pair *bp;
  938. /* works for the "do not cross hash slot boundaries" case
  939. * e.g. sector 262269, size 4096
  940. * s_enr = 262269 >> 6 = 4097
  941. * e_enr = (262269+8-1) >> 6 = 4098
  942. * HT_SHIFT = 6
  943. * sps = 64, mask = 63
  944. * first_sectors = 64 - (262269 & 63) = 3
  945. */
  946. const sector_t sect = bio->bi_sector;
  947. const int sps = 1 << HT_SHIFT; /* sectors per slot */
  948. const int mask = sps - 1;
  949. const sector_t first_sectors = sps - (sect & mask);
  950. bp = bio_split(bio,
  951. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  952. bio_split_pool,
  953. #endif
  954. first_sectors);
  955. /* we need to get a "reference count" (ap_bio_cnt)
  956. * to avoid races with the disconnect/reconnect/suspend code.
  957. * In case we need to split the bio here, we need to get three references
  958. * atomically, otherwise we might deadlock when trying to submit the
  959. * second one! */
  960. inc_ap_bio(mdev, 3);
  961. D_ASSERT(e_enr == s_enr + 1);
  962. while (drbd_make_request_common(mdev, &bp->bio1))
  963. inc_ap_bio(mdev, 1);
  964. while (drbd_make_request_common(mdev, &bp->bio2))
  965. inc_ap_bio(mdev, 1);
  966. dec_ap_bio(mdev);
  967. bio_pair_release(bp);
  968. }
  969. return 0;
  970. }
  971. /* This is called by bio_add_page(). With this function we reduce
  972. * the number of BIOs that span over multiple DRBD_MAX_SEGMENT_SIZEs
  973. * units (was AL_EXTENTs).
  974. *
  975. * we do the calculation within the lower 32bit of the byte offsets,
  976. * since we don't care for actual offset, but only check whether it
  977. * would cross "activity log extent" boundaries.
  978. *
  979. * As long as the BIO is empty we have to allow at least one bvec,
  980. * regardless of size and offset. so the resulting bio may still
  981. * cross extent boundaries. those are dealt with (bio_split) in
  982. * drbd_make_request_26.
  983. */
  984. int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
  985. {
  986. struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
  987. unsigned int bio_offset =
  988. (unsigned int)bvm->bi_sector << 9; /* 32 bit */
  989. unsigned int bio_size = bvm->bi_size;
  990. int limit, backing_limit;
  991. limit = DRBD_MAX_SEGMENT_SIZE
  992. - ((bio_offset & (DRBD_MAX_SEGMENT_SIZE-1)) + bio_size);
  993. if (limit < 0)
  994. limit = 0;
  995. if (bio_size == 0) {
  996. if (limit <= bvec->bv_len)
  997. limit = bvec->bv_len;
  998. } else if (limit && get_ldev(mdev)) {
  999. struct request_queue * const b =
  1000. mdev->ldev->backing_bdev->bd_disk->queue;
  1001. if (b->merge_bvec_fn) {
  1002. backing_limit = b->merge_bvec_fn(b, bvm, bvec);
  1003. limit = min(limit, backing_limit);
  1004. }
  1005. put_ldev(mdev);
  1006. }
  1007. return limit;
  1008. }