drbd_req.c 38 KB

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