drbd_req.c 36 KB

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