drbd_req.c 35 KB

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