Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | drbd_req.c |
| 3 | |
| 4 | This file is part of DRBD by Philipp Reisner and Lars Ellenberg. |
| 5 | |
| 6 | Copyright (C) 2001-2008, LINBIT Information Technologies GmbH. |
| 7 | Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>. |
| 8 | Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. |
| 9 | |
| 10 | drbd is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2, or (at your option) |
| 13 | any later version. |
| 14 | |
| 15 | drbd is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with drbd; see the file COPYING. If not, write to |
| 22 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | |
| 24 | */ |
| 25 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/drbd.h> |
| 30 | #include "drbd_int.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 31 | #include "drbd_req.h" |
| 32 | |
| 33 | |
| 34 | /* Update disk stats at start of I/O request */ |
| 35 | static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req, struct bio *bio) |
| 36 | { |
| 37 | const int rw = bio_data_dir(bio); |
| 38 | int cpu; |
| 39 | cpu = part_stat_lock(); |
Lars Ellenberg | 031a7c1 | 2012-01-21 16:50:25 +0100 | [diff] [blame] | 40 | part_round_stats(cpu, &mdev->vdisk->part0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 41 | part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]); |
| 42 | part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio)); |
Philipp Reisner | 753c891 | 2009-11-18 15:52:51 +0100 | [diff] [blame] | 43 | part_inc_in_flight(&mdev->vdisk->part0, rw); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 44 | part_stat_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* Update disk stats when completing request upwards */ |
| 48 | static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req) |
| 49 | { |
| 50 | int rw = bio_data_dir(req->master_bio); |
| 51 | unsigned long duration = jiffies - req->start_time; |
| 52 | int cpu; |
| 53 | cpu = part_stat_lock(); |
| 54 | part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration); |
| 55 | part_round_stats(cpu, &mdev->vdisk->part0); |
Philipp Reisner | 753c891 | 2009-11-18 15:52:51 +0100 | [diff] [blame] | 56 | part_dec_in_flight(&mdev->vdisk->part0, rw); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 57 | part_stat_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw) |
| 61 | { |
| 62 | const unsigned long s = req->rq_state; |
Philipp Reisner | 288f422 | 2010-05-27 15:07:43 +0200 | [diff] [blame] | 63 | |
| 64 | /* remove it from the transfer log. |
| 65 | * well, only if it had been there in the first |
| 66 | * place... if it had not (local only or conflicting |
| 67 | * and never sent), it should still be "empty" as |
| 68 | * initialized in drbd_req_new(), so we can list_del() it |
| 69 | * here unconditionally */ |
| 70 | list_del(&req->tl_requests); |
| 71 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 72 | /* if it was a write, we may have to set the corresponding |
| 73 | * bit(s) out-of-sync first. If it had a local part, we need to |
| 74 | * release the reference to the activity log. */ |
| 75 | if (rw == WRITE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 76 | /* Set out-of-sync unless both OK flags are set |
| 77 | * (local only or remote failed). |
| 78 | * Other places where we set out-of-sync: |
| 79 | * READ with local io-error */ |
| 80 | if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK)) |
| 81 | drbd_set_out_of_sync(mdev, req->sector, req->size); |
| 82 | |
| 83 | if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS)) |
| 84 | drbd_set_in_sync(mdev, req->sector, req->size); |
| 85 | |
| 86 | /* one might be tempted to move the drbd_al_complete_io |
| 87 | * to the local io completion callback drbd_endio_pri. |
| 88 | * but, if this was a mirror write, we may only |
| 89 | * drbd_al_complete_io after this is RQ_NET_DONE, |
| 90 | * otherwise the extent could be dropped from the al |
| 91 | * before it has actually been written on the peer. |
| 92 | * if we crash before our peer knows about the request, |
| 93 | * but after the extent has been dropped from the al, |
| 94 | * we would forget to resync the corresponding extent. |
| 95 | */ |
| 96 | if (s & RQ_LOCAL_MASK) { |
| 97 | if (get_ldev_if_state(mdev, D_FAILED)) { |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 98 | if (s & RQ_IN_ACT_LOG) |
| 99 | drbd_al_complete_io(mdev, req->sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 100 | put_ldev(mdev); |
| 101 | } else if (__ratelimit(&drbd_ratelimit_state)) { |
| 102 | dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu), " |
| 103 | "but my Disk seems to have failed :(\n", |
| 104 | (unsigned long long) req->sector); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Philipp Reisner | 32fa7e9 | 2010-05-26 17:13:18 +0200 | [diff] [blame] | 109 | drbd_req_free(req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void queue_barrier(struct drbd_conf *mdev) |
| 113 | { |
| 114 | struct drbd_tl_epoch *b; |
| 115 | |
| 116 | /* We are within the req_lock. Once we queued the barrier for sending, |
| 117 | * we set the CREATE_BARRIER bit. It is cleared as soon as a new |
| 118 | * barrier/epoch object is added. This is the only place this bit is |
| 119 | * set. It indicates that the barrier for this epoch is already queued, |
| 120 | * and no new epoch has been created yet. */ |
| 121 | if (test_bit(CREATE_BARRIER, &mdev->flags)) |
| 122 | return; |
| 123 | |
| 124 | b = mdev->newest_tle; |
| 125 | b->w.cb = w_send_barrier; |
| 126 | /* inc_ap_pending done here, so we won't |
| 127 | * get imbalanced on connection loss. |
| 128 | * dec_ap_pending will be done in got_BarrierAck |
| 129 | * or (on connection loss) in tl_clear. */ |
| 130 | inc_ap_pending(mdev); |
| 131 | drbd_queue_work(&mdev->data.work, &b->w); |
| 132 | set_bit(CREATE_BARRIER, &mdev->flags); |
| 133 | } |
| 134 | |
| 135 | static void _about_to_complete_local_write(struct drbd_conf *mdev, |
| 136 | struct drbd_request *req) |
| 137 | { |
| 138 | const unsigned long s = req->rq_state; |
| 139 | struct drbd_request *i; |
| 140 | struct drbd_epoch_entry *e; |
| 141 | struct hlist_node *n; |
| 142 | struct hlist_head *slot; |
| 143 | |
Lars Ellenberg | 8a3c104 | 2010-12-05 14:11:14 +0100 | [diff] [blame] | 144 | /* Before we can signal completion to the upper layers, |
| 145 | * we may need to close the current epoch. |
| 146 | * We can skip this, if this request has not even been sent, because we |
| 147 | * did not have a fully established connection yet/anymore, during |
| 148 | * bitmap exchange, or while we are C_AHEAD due to congestion policy. |
| 149 | */ |
| 150 | if (mdev->state.conn >= C_CONNECTED && |
| 151 | (s & RQ_NET_SENT) != 0 && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 152 | req->epoch == mdev->newest_tle->br_number) |
| 153 | queue_barrier(mdev); |
| 154 | |
| 155 | /* we need to do the conflict detection stuff, |
| 156 | * if we have the ee_hash (two_primaries) and |
| 157 | * this has been on the network */ |
| 158 | if ((s & RQ_NET_DONE) && mdev->ee_hash != NULL) { |
| 159 | const sector_t sector = req->sector; |
| 160 | const int size = req->size; |
| 161 | |
| 162 | /* ASSERT: |
| 163 | * there must be no conflicting requests, since |
| 164 | * they must have been failed on the spot */ |
| 165 | #define OVERLAPS overlaps(sector, size, i->sector, i->size) |
| 166 | slot = tl_hash_slot(mdev, sector); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 167 | hlist_for_each_entry(i, n, slot, collision) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 168 | if (OVERLAPS) { |
| 169 | dev_alert(DEV, "LOGIC BUG: completed: %p %llus +%u; " |
| 170 | "other: %p %llus +%u\n", |
| 171 | req, (unsigned long long)sector, size, |
| 172 | i, (unsigned long long)i->sector, i->size); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* maybe "wake" those conflicting epoch entries |
| 177 | * that wait for this request to finish. |
| 178 | * |
| 179 | * currently, there can be only _one_ such ee |
| 180 | * (well, or some more, which would be pending |
| 181 | * P_DISCARD_ACK not yet sent by the asender...), |
| 182 | * since we block the receiver thread upon the |
| 183 | * first conflict detection, which will wait on |
| 184 | * misc_wait. maybe we want to assert that? |
| 185 | * |
| 186 | * anyways, if we found one, |
| 187 | * we just have to do a wake_up. */ |
| 188 | #undef OVERLAPS |
| 189 | #define OVERLAPS overlaps(sector, size, e->sector, e->size) |
| 190 | slot = ee_hash_slot(mdev, req->sector); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 191 | hlist_for_each_entry(e, n, slot, collision) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 192 | if (OVERLAPS) { |
| 193 | wake_up(&mdev->misc_wait); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | #undef OVERLAPS |
| 199 | } |
| 200 | |
| 201 | void complete_master_bio(struct drbd_conf *mdev, |
| 202 | struct bio_and_error *m) |
| 203 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 204 | bio_endio(m->bio, m->error); |
| 205 | dec_ap_bio(mdev); |
| 206 | } |
| 207 | |
| 208 | /* Helper for __req_mod(). |
| 209 | * Set m->bio to the master bio, if it is fit to be completed, |
| 210 | * or leave it alone (it is initialized to NULL in __req_mod), |
| 211 | * if it has already been completed, or cannot be completed yet. |
| 212 | * If m->bio is set, the error status to be returned is placed in m->error. |
| 213 | */ |
| 214 | void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m) |
| 215 | { |
| 216 | const unsigned long s = req->rq_state; |
| 217 | struct drbd_conf *mdev = req->mdev; |
Philipp Reisner | 2b4dd36 | 2011-03-14 13:01:50 +0100 | [diff] [blame] | 218 | int rw = req->rq_state & RQ_WRITE ? WRITE : READ; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 219 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 220 | /* we must not complete the master bio, while it is |
| 221 | * still being processed by _drbd_send_zc_bio (drbd_send_dblock) |
| 222 | * not yet acknowledged by the peer |
| 223 | * not yet completed by the local io subsystem |
| 224 | * these flags may get cleared in any order by |
| 225 | * the worker, |
| 226 | * the receiver, |
| 227 | * the bio_endio completion callbacks. |
| 228 | */ |
| 229 | if (s & RQ_NET_QUEUED) |
| 230 | return; |
| 231 | if (s & RQ_NET_PENDING) |
| 232 | return; |
Philipp Reisner | 2b4dd36 | 2011-03-14 13:01:50 +0100 | [diff] [blame] | 233 | if (s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 234 | return; |
| 235 | |
| 236 | if (req->master_bio) { |
| 237 | /* this is data_received (remote read) |
| 238 | * or protocol C P_WRITE_ACK |
| 239 | * or protocol B P_RECV_ACK |
| 240 | * or protocol A "handed_over_to_network" (SendAck) |
| 241 | * or canceled or failed, |
| 242 | * or killed from the transfer log due to connection loss. |
| 243 | */ |
| 244 | |
| 245 | /* |
| 246 | * figure out whether to report success or failure. |
| 247 | * |
| 248 | * report success when at least one of the operations succeeded. |
| 249 | * or, to put the other way, |
| 250 | * only report failure, when both operations failed. |
| 251 | * |
| 252 | * what to do about the failures is handled elsewhere. |
| 253 | * what we need to do here is just: complete the master_bio. |
| 254 | * |
| 255 | * local completion error, if any, has been stored as ERR_PTR |
| 256 | * in private_bio within drbd_endio_pri. |
| 257 | */ |
| 258 | int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK); |
| 259 | int error = PTR_ERR(req->private_bio); |
| 260 | |
| 261 | /* remove the request from the conflict detection |
| 262 | * respective block_id verification hash */ |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 263 | if (!hlist_unhashed(&req->collision)) |
| 264 | hlist_del(&req->collision); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 265 | else |
Philipp Reisner | 8825f7c | 2010-10-21 17:21:19 +0200 | [diff] [blame] | 266 | D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 267 | |
| 268 | /* for writes we need to do some extra housekeeping */ |
| 269 | if (rw == WRITE) |
| 270 | _about_to_complete_local_write(mdev, req); |
| 271 | |
| 272 | /* Update disk stats */ |
| 273 | _drbd_end_io_acct(mdev, req); |
| 274 | |
| 275 | m->error = ok ? 0 : (error ?: -EIO); |
| 276 | m->bio = req->master_bio; |
| 277 | req->master_bio = NULL; |
| 278 | } |
| 279 | |
Philipp Reisner | 2b4dd36 | 2011-03-14 13:01:50 +0100 | [diff] [blame] | 280 | if (s & RQ_LOCAL_PENDING) |
| 281 | return; |
| 282 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 283 | if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) { |
| 284 | /* this is disconnected (local only) operation, |
| 285 | * or protocol C P_WRITE_ACK, |
| 286 | * or protocol A or B P_BARRIER_ACK, |
| 287 | * or killed from the transfer log due to connection loss. */ |
| 288 | _req_is_done(mdev, req, rw); |
| 289 | } |
| 290 | /* else: network part and not DONE yet. that is |
| 291 | * protocol A or B, barrier ack still pending... */ |
| 292 | } |
| 293 | |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 294 | static void _req_may_be_done_not_susp(struct drbd_request *req, struct bio_and_error *m) |
| 295 | { |
| 296 | struct drbd_conf *mdev = req->mdev; |
| 297 | |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 298 | if (!is_susp(mdev->state)) |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 299 | _req_may_be_done(req, m); |
| 300 | } |
| 301 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 302 | /* |
| 303 | * checks whether there was an overlapping request |
| 304 | * or ee already registered. |
| 305 | * |
| 306 | * if so, return 1, in which case this request is completed on the spot, |
| 307 | * without ever being submitted or send. |
| 308 | * |
| 309 | * return 0 if it is ok to submit this request. |
| 310 | * |
| 311 | * NOTE: |
| 312 | * paranoia: assume something above us is broken, and issues different write |
| 313 | * requests for the same block simultaneously... |
| 314 | * |
| 315 | * To ensure these won't be reordered differently on both nodes, resulting in |
| 316 | * diverging data sets, we discard the later one(s). Not that this is supposed |
| 317 | * to happen, but this is the rationale why we also have to check for |
| 318 | * conflicting requests with local origin, and why we have to do so regardless |
| 319 | * of whether we allowed multiple primaries. |
| 320 | * |
| 321 | * BTW, in case we only have one primary, the ee_hash is empty anyways, and the |
| 322 | * second hlist_for_each_entry becomes a noop. This is even simpler than to |
| 323 | * grab a reference on the net_conf, and check for the two_primaries flag... |
| 324 | */ |
| 325 | static int _req_conflicts(struct drbd_request *req) |
| 326 | { |
| 327 | struct drbd_conf *mdev = req->mdev; |
| 328 | const sector_t sector = req->sector; |
| 329 | const int size = req->size; |
| 330 | struct drbd_request *i; |
| 331 | struct drbd_epoch_entry *e; |
| 332 | struct hlist_node *n; |
| 333 | struct hlist_head *slot; |
| 334 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 335 | D_ASSERT(hlist_unhashed(&req->collision)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 336 | |
| 337 | if (!get_net_conf(mdev)) |
| 338 | return 0; |
| 339 | |
| 340 | /* BUG_ON */ |
| 341 | ERR_IF (mdev->tl_hash_s == 0) |
| 342 | goto out_no_conflict; |
| 343 | BUG_ON(mdev->tl_hash == NULL); |
| 344 | |
| 345 | #define OVERLAPS overlaps(i->sector, i->size, sector, size) |
| 346 | slot = tl_hash_slot(mdev, sector); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 347 | hlist_for_each_entry(i, n, slot, collision) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 348 | if (OVERLAPS) { |
| 349 | dev_alert(DEV, "%s[%u] Concurrent local write detected! " |
| 350 | "[DISCARD L] new: %llus +%u; " |
| 351 | "pending: %llus +%u\n", |
| 352 | current->comm, current->pid, |
| 353 | (unsigned long long)sector, size, |
| 354 | (unsigned long long)i->sector, i->size); |
| 355 | goto out_conflict; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (mdev->ee_hash_s) { |
| 360 | /* now, check for overlapping requests with remote origin */ |
| 361 | BUG_ON(mdev->ee_hash == NULL); |
| 362 | #undef OVERLAPS |
| 363 | #define OVERLAPS overlaps(e->sector, e->size, sector, size) |
| 364 | slot = ee_hash_slot(mdev, sector); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 365 | hlist_for_each_entry(e, n, slot, collision) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 366 | if (OVERLAPS) { |
| 367 | dev_alert(DEV, "%s[%u] Concurrent remote write detected!" |
| 368 | " [DISCARD L] new: %llus +%u; " |
| 369 | "pending: %llus +%u\n", |
| 370 | current->comm, current->pid, |
| 371 | (unsigned long long)sector, size, |
| 372 | (unsigned long long)e->sector, e->size); |
| 373 | goto out_conflict; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | #undef OVERLAPS |
| 378 | |
| 379 | out_no_conflict: |
| 380 | /* this is like it should be, and what we expected. |
| 381 | * our users do behave after all... */ |
| 382 | put_net_conf(mdev); |
| 383 | return 0; |
| 384 | |
| 385 | out_conflict: |
| 386 | put_net_conf(mdev); |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | /* obviously this could be coded as many single functions |
| 391 | * instead of one huge switch, |
| 392 | * or by putting the code directly in the respective locations |
| 393 | * (as it has been before). |
| 394 | * |
| 395 | * but having it this way |
| 396 | * enforces that it is all in this one place, where it is easier to audit, |
| 397 | * it makes it obvious that whatever "event" "happens" to a request should |
| 398 | * happen "atomically" within the req_lock, |
| 399 | * and it enforces that we have to think in a very structured manner |
| 400 | * about the "events" that may happen to a request during its life time ... |
| 401 | */ |
Philipp Reisner | 2a80699 | 2010-06-09 14:07:43 +0200 | [diff] [blame] | 402 | int __req_mod(struct drbd_request *req, enum drbd_req_event what, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 403 | struct bio_and_error *m) |
| 404 | { |
| 405 | struct drbd_conf *mdev = req->mdev; |
Philipp Reisner | 2a80699 | 2010-06-09 14:07:43 +0200 | [diff] [blame] | 406 | int rv = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 407 | m->bio = NULL; |
| 408 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 409 | switch (what) { |
| 410 | default: |
| 411 | dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__); |
| 412 | break; |
| 413 | |
| 414 | /* does not happen... |
| 415 | * initialization done in drbd_req_new |
| 416 | case created: |
| 417 | break; |
| 418 | */ |
| 419 | |
| 420 | case to_be_send: /* via network */ |
| 421 | /* reached via drbd_make_request_common |
| 422 | * and from w_read_retry_remote */ |
| 423 | D_ASSERT(!(req->rq_state & RQ_NET_MASK)); |
| 424 | req->rq_state |= RQ_NET_PENDING; |
| 425 | inc_ap_pending(mdev); |
| 426 | break; |
| 427 | |
| 428 | case to_be_submitted: /* locally */ |
| 429 | /* reached via drbd_make_request_common */ |
| 430 | D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK)); |
| 431 | req->rq_state |= RQ_LOCAL_PENDING; |
| 432 | break; |
| 433 | |
| 434 | case completed_ok: |
Philipp Reisner | 2b4dd36 | 2011-03-14 13:01:50 +0100 | [diff] [blame] | 435 | if (req->rq_state & RQ_WRITE) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 436 | mdev->writ_cnt += req->size>>9; |
| 437 | else |
| 438 | mdev->read_cnt += req->size>>9; |
| 439 | |
| 440 | req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK); |
| 441 | req->rq_state &= ~RQ_LOCAL_PENDING; |
| 442 | |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 443 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 444 | break; |
| 445 | |
Philipp Reisner | 2b4dd36 | 2011-03-14 13:01:50 +0100 | [diff] [blame] | 446 | case abort_disk_io: |
| 447 | req->rq_state |= RQ_LOCAL_ABORTED; |
| 448 | if (req->rq_state & RQ_WRITE) |
| 449 | _req_may_be_done_not_susp(req, m); |
| 450 | else |
| 451 | goto goto_queue_for_net_read; |
| 452 | break; |
| 453 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 454 | case write_completed_with_error: |
| 455 | req->rq_state |= RQ_LOCAL_COMPLETED; |
| 456 | req->rq_state &= ~RQ_LOCAL_PENDING; |
| 457 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 458 | __drbd_chk_io_error(mdev, false); |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 459 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 460 | break; |
| 461 | |
| 462 | case read_ahead_completed_with_error: |
| 463 | /* it is legal to fail READA */ |
| 464 | req->rq_state |= RQ_LOCAL_COMPLETED; |
| 465 | req->rq_state &= ~RQ_LOCAL_PENDING; |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 466 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 467 | break; |
| 468 | |
| 469 | case read_completed_with_error: |
| 470 | drbd_set_out_of_sync(mdev, req->sector, req->size); |
| 471 | |
| 472 | req->rq_state |= RQ_LOCAL_COMPLETED; |
| 473 | req->rq_state &= ~RQ_LOCAL_PENDING; |
| 474 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 475 | D_ASSERT(!(req->rq_state & RQ_NET_MASK)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 476 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 477 | __drbd_chk_io_error(mdev, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 478 | |
Philipp Reisner | 2b4dd36 | 2011-03-14 13:01:50 +0100 | [diff] [blame] | 479 | goto_queue_for_net_read: |
| 480 | |
Lars Ellenberg | d255e5f | 2010-05-27 09:45:45 +0200 | [diff] [blame] | 481 | /* no point in retrying if there is no good remote data, |
| 482 | * or we have no connection. */ |
| 483 | if (mdev->state.pdsk != D_UP_TO_DATE) { |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 484 | _req_may_be_done_not_susp(req, m); |
Lars Ellenberg | d255e5f | 2010-05-27 09:45:45 +0200 | [diff] [blame] | 485 | break; |
| 486 | } |
| 487 | |
| 488 | /* _req_mod(req,to_be_send); oops, recursion... */ |
| 489 | req->rq_state |= RQ_NET_PENDING; |
| 490 | inc_ap_pending(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 491 | /* fall through: _req_mod(req,queue_for_net_read); */ |
| 492 | |
| 493 | case queue_for_net_read: |
| 494 | /* READ or READA, and |
| 495 | * no local disk, |
| 496 | * or target area marked as invalid, |
| 497 | * or just got an io-error. */ |
| 498 | /* from drbd_make_request_common |
| 499 | * or from bio_endio during read io-error recovery */ |
| 500 | |
| 501 | /* so we can verify the handle in the answer packet |
| 502 | * corresponding hlist_del is in _req_may_be_done() */ |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 503 | hlist_add_head(&req->collision, ar_hash_slot(mdev, req->sector)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 504 | |
Lars Ellenberg | 83c3883 | 2009-11-03 02:22:06 +0100 | [diff] [blame] | 505 | set_bit(UNPLUG_REMOTE, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 506 | |
| 507 | D_ASSERT(req->rq_state & RQ_NET_PENDING); |
| 508 | req->rq_state |= RQ_NET_QUEUED; |
| 509 | req->w.cb = (req->rq_state & RQ_LOCAL_MASK) |
| 510 | ? w_read_retry_remote |
| 511 | : w_send_read_req; |
| 512 | drbd_queue_work(&mdev->data.work, &req->w); |
| 513 | break; |
| 514 | |
| 515 | case queue_for_net_write: |
| 516 | /* assert something? */ |
| 517 | /* from drbd_make_request_common only */ |
| 518 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 519 | hlist_add_head(&req->collision, tl_hash_slot(mdev, req->sector)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 520 | /* corresponding hlist_del is in _req_may_be_done() */ |
| 521 | |
| 522 | /* NOTE |
| 523 | * In case the req ended up on the transfer log before being |
| 524 | * queued on the worker, it could lead to this request being |
| 525 | * missed during cleanup after connection loss. |
| 526 | * So we have to do both operations here, |
| 527 | * within the same lock that protects the transfer log. |
| 528 | * |
| 529 | * _req_add_to_epoch(req); this has to be after the |
| 530 | * _maybe_start_new_epoch(req); which happened in |
| 531 | * drbd_make_request_common, because we now may set the bit |
| 532 | * again ourselves to close the current epoch. |
| 533 | * |
| 534 | * Add req to the (now) current epoch (barrier). */ |
| 535 | |
Lars Ellenberg | 83c3883 | 2009-11-03 02:22:06 +0100 | [diff] [blame] | 536 | /* otherwise we may lose an unplug, which may cause some remote |
| 537 | * io-scheduler timeout to expire, increasing maximum latency, |
| 538 | * hurting performance. */ |
| 539 | set_bit(UNPLUG_REMOTE, &mdev->flags); |
| 540 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 541 | /* see drbd_make_request_common, |
| 542 | * just after it grabs the req_lock */ |
| 543 | D_ASSERT(test_bit(CREATE_BARRIER, &mdev->flags) == 0); |
| 544 | |
| 545 | req->epoch = mdev->newest_tle->br_number; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 546 | |
| 547 | /* increment size of current epoch */ |
Philipp Reisner | 7e602c0 | 2010-05-27 14:49:27 +0200 | [diff] [blame] | 548 | mdev->newest_tle->n_writes++; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 549 | |
| 550 | /* queue work item to send data */ |
| 551 | D_ASSERT(req->rq_state & RQ_NET_PENDING); |
| 552 | req->rq_state |= RQ_NET_QUEUED; |
| 553 | req->w.cb = w_send_dblock; |
| 554 | drbd_queue_work(&mdev->data.work, &req->w); |
| 555 | |
| 556 | /* close the epoch, in case it outgrew the limit */ |
Philipp Reisner | 7e602c0 | 2010-05-27 14:49:27 +0200 | [diff] [blame] | 557 | if (mdev->newest_tle->n_writes >= mdev->net_conf->max_epoch_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 558 | queue_barrier(mdev); |
| 559 | |
| 560 | break; |
| 561 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 562 | case queue_for_send_oos: |
| 563 | req->rq_state |= RQ_NET_QUEUED; |
| 564 | req->w.cb = w_send_oos; |
| 565 | drbd_queue_work(&mdev->data.work, &req->w); |
| 566 | break; |
| 567 | |
Lars Ellenberg | 41c4a00 | 2012-01-11 09:46:48 +0100 | [diff] [blame] | 568 | case read_retry_remote_canceled: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 569 | case send_canceled: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 570 | case send_failed: |
| 571 | /* real cleanup will be done from tl_clear. just update flags |
| 572 | * so it is no longer marked as on the worker queue */ |
| 573 | req->rq_state &= ~RQ_NET_QUEUED; |
| 574 | /* if we did it right, tl_clear should be scheduled only after |
| 575 | * this, so this should not be necessary! */ |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 576 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 577 | break; |
| 578 | |
| 579 | case handed_over_to_network: |
| 580 | /* assert something? */ |
Philipp Reisner | 759fbdf | 2010-10-26 16:02:27 +0200 | [diff] [blame] | 581 | if (bio_data_dir(req->master_bio) == WRITE) |
| 582 | atomic_add(req->size>>9, &mdev->ap_in_flight); |
| 583 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 584 | if (bio_data_dir(req->master_bio) == WRITE && |
| 585 | mdev->net_conf->wire_protocol == DRBD_PROT_A) { |
| 586 | /* this is what is dangerous about protocol A: |
| 587 | * pretend it was successfully written on the peer. */ |
| 588 | if (req->rq_state & RQ_NET_PENDING) { |
| 589 | dec_ap_pending(mdev); |
| 590 | req->rq_state &= ~RQ_NET_PENDING; |
| 591 | req->rq_state |= RQ_NET_OK; |
| 592 | } /* else: neg-ack was faster... */ |
| 593 | /* it is still not yet RQ_NET_DONE until the |
| 594 | * corresponding epoch barrier got acked as well, |
| 595 | * so we know what to dirty on connection loss */ |
| 596 | } |
| 597 | req->rq_state &= ~RQ_NET_QUEUED; |
| 598 | req->rq_state |= RQ_NET_SENT; |
Lars Ellenberg | 6d49e10 | 2012-01-11 09:43:25 +0100 | [diff] [blame] | 599 | _req_may_be_done_not_susp(req, m); |
| 600 | break; |
| 601 | |
| 602 | case oos_handed_to_network: |
| 603 | /* Was not set PENDING, no longer QUEUED, so is now DONE |
| 604 | * as far as this connection is concerned. */ |
| 605 | req->rq_state &= ~RQ_NET_QUEUED; |
| 606 | req->rq_state |= RQ_NET_DONE; |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 607 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 608 | break; |
| 609 | |
| 610 | case connection_lost_while_pending: |
| 611 | /* transfer log cleanup after connection loss */ |
| 612 | /* assert something? */ |
| 613 | if (req->rq_state & RQ_NET_PENDING) |
| 614 | dec_ap_pending(mdev); |
| 615 | req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING); |
| 616 | req->rq_state |= RQ_NET_DONE; |
Philipp Reisner | 759fbdf | 2010-10-26 16:02:27 +0200 | [diff] [blame] | 617 | if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE) |
| 618 | atomic_sub(req->size>>9, &mdev->ap_in_flight); |
| 619 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 620 | /* if it is still queued, we may not complete it here. |
| 621 | * it will be canceled soon. */ |
| 622 | if (!(req->rq_state & RQ_NET_QUEUED)) |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 623 | _req_may_be_done(req, m); /* Allowed while state.susp */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 624 | break; |
| 625 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 626 | case conflict_discarded_by_peer: |
| 627 | /* for discarded conflicting writes of multiple primaries, |
| 628 | * there is no need to keep anything in the tl, potential |
| 629 | * node crashes are covered by the activity log. */ |
| 630 | if (what == conflict_discarded_by_peer) |
| 631 | dev_alert(DEV, "Got DiscardAck packet %llus +%u!" |
| 632 | " DRBD is not a random data generator!\n", |
| 633 | (unsigned long long)req->sector, req->size); |
| 634 | req->rq_state |= RQ_NET_DONE; |
| 635 | /* fall through */ |
Lars Ellenberg | d64957c | 2012-03-23 14:42:19 +0100 | [diff] [blame] | 636 | case write_acked_by_peer_and_sis: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 637 | case write_acked_by_peer: |
Lars Ellenberg | d64957c | 2012-03-23 14:42:19 +0100 | [diff] [blame] | 638 | if (what == write_acked_by_peer_and_sis) |
| 639 | req->rq_state |= RQ_NET_SIS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 640 | /* protocol C; successfully written on peer. |
Lars Ellenberg | d64957c | 2012-03-23 14:42:19 +0100 | [diff] [blame] | 641 | * Nothing more to do here. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 642 | * We want to keep the tl in place for all protocols, to cater |
Lars Ellenberg | d64957c | 2012-03-23 14:42:19 +0100 | [diff] [blame] | 643 | * for volatile write-back caches on lower level devices. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 644 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 645 | case recv_acked_by_peer: |
| 646 | /* protocol B; pretends to be successfully written on peer. |
| 647 | * see also notes above in handed_over_to_network about |
| 648 | * protocol != C */ |
| 649 | req->rq_state |= RQ_NET_OK; |
| 650 | D_ASSERT(req->rq_state & RQ_NET_PENDING); |
| 651 | dec_ap_pending(mdev); |
Philipp Reisner | 759fbdf | 2010-10-26 16:02:27 +0200 | [diff] [blame] | 652 | atomic_sub(req->size>>9, &mdev->ap_in_flight); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 653 | req->rq_state &= ~RQ_NET_PENDING; |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 654 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 655 | break; |
| 656 | |
| 657 | case neg_acked: |
| 658 | /* assert something? */ |
Philipp Reisner | 759fbdf | 2010-10-26 16:02:27 +0200 | [diff] [blame] | 659 | if (req->rq_state & RQ_NET_PENDING) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 660 | dec_ap_pending(mdev); |
Philipp Reisner | 759fbdf | 2010-10-26 16:02:27 +0200 | [diff] [blame] | 661 | atomic_sub(req->size>>9, &mdev->ap_in_flight); |
| 662 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 663 | req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING); |
| 664 | |
| 665 | req->rq_state |= RQ_NET_DONE; |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 666 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 667 | /* else: done by handed_over_to_network */ |
| 668 | break; |
| 669 | |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 670 | case fail_frozen_disk_io: |
| 671 | if (!(req->rq_state & RQ_LOCAL_COMPLETED)) |
| 672 | break; |
| 673 | |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 674 | _req_may_be_done(req, m); /* Allowed while state.susp */ |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | case restart_frozen_disk_io: |
| 678 | if (!(req->rq_state & RQ_LOCAL_COMPLETED)) |
| 679 | break; |
| 680 | |
| 681 | req->rq_state &= ~RQ_LOCAL_COMPLETED; |
| 682 | |
| 683 | rv = MR_READ; |
| 684 | if (bio_data_dir(req->master_bio) == WRITE) |
| 685 | rv = MR_WRITE; |
| 686 | |
| 687 | get_ldev(mdev); |
| 688 | req->w.cb = w_restart_disk_io; |
| 689 | drbd_queue_work(&mdev->data.work, &req->w); |
| 690 | break; |
| 691 | |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 692 | case resend: |
| 693 | /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 694 | before the connection loss (B&C only); only P_BARRIER_ACK was missing. |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 695 | Trowing them out of the TL here by pretending we got a BARRIER_ACK |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 696 | We ensure that the peer was not rebooted */ |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 697 | if (!(req->rq_state & RQ_NET_OK)) { |
| 698 | if (req->w.cb) { |
| 699 | drbd_queue_work(&mdev->data.work, &req->w); |
| 700 | rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ; |
| 701 | } |
| 702 | break; |
| 703 | } |
| 704 | /* else, fall through to barrier_acked */ |
| 705 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 706 | case barrier_acked: |
Philipp Reisner | 288f422 | 2010-05-27 15:07:43 +0200 | [diff] [blame] | 707 | if (!(req->rq_state & RQ_WRITE)) |
| 708 | break; |
| 709 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 710 | if (req->rq_state & RQ_NET_PENDING) { |
| 711 | /* barrier came in before all requests have been acked. |
| 712 | * this is bad, because if the connection is lost now, |
| 713 | * we won't be able to clean them up... */ |
| 714 | dev_err(DEV, "FIXME (barrier_acked but pending)\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 715 | list_move(&req->tl_requests, &mdev->out_of_sequence_requests); |
| 716 | } |
Lars Ellenberg | e636db5 | 2011-01-21 17:10:37 +0100 | [diff] [blame] | 717 | if ((req->rq_state & RQ_NET_MASK) != 0) { |
| 718 | req->rq_state |= RQ_NET_DONE; |
| 719 | if (mdev->net_conf->wire_protocol == DRBD_PROT_A) |
| 720 | atomic_sub(req->size>>9, &mdev->ap_in_flight); |
| 721 | } |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 722 | _req_may_be_done(req, m); /* Allowed while state.susp */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 723 | break; |
| 724 | |
| 725 | case data_received: |
| 726 | D_ASSERT(req->rq_state & RQ_NET_PENDING); |
| 727 | dec_ap_pending(mdev); |
| 728 | req->rq_state &= ~RQ_NET_PENDING; |
| 729 | req->rq_state |= (RQ_NET_OK|RQ_NET_DONE); |
Philipp Reisner | cfa0341 | 2010-06-23 17:18:51 +0200 | [diff] [blame] | 730 | _req_may_be_done_not_susp(req, m); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 731 | break; |
| 732 | }; |
Philipp Reisner | 2a80699 | 2010-06-09 14:07:43 +0200 | [diff] [blame] | 733 | |
| 734 | return rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* we may do a local read if: |
| 738 | * - we are consistent (of course), |
| 739 | * - or we are generally inconsistent, |
| 740 | * BUT we are still/already IN SYNC for this area. |
| 741 | * since size may be bigger than BM_BLOCK_SIZE, |
| 742 | * we may need to check several bits. |
| 743 | */ |
| 744 | static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size) |
| 745 | { |
| 746 | unsigned long sbnr, ebnr; |
| 747 | sector_t esector, nr_sectors; |
| 748 | |
| 749 | if (mdev->state.disk == D_UP_TO_DATE) |
| 750 | return 1; |
| 751 | if (mdev->state.disk >= D_OUTDATED) |
| 752 | return 0; |
| 753 | if (mdev->state.disk < D_INCONSISTENT) |
| 754 | return 0; |
| 755 | /* state.disk == D_INCONSISTENT We will have a look at the BitMap */ |
| 756 | nr_sectors = drbd_get_capacity(mdev->this_bdev); |
| 757 | esector = sector + (size >> 9) - 1; |
| 758 | |
| 759 | D_ASSERT(sector < nr_sectors); |
| 760 | D_ASSERT(esector < nr_sectors); |
| 761 | |
| 762 | sbnr = BM_SECT_TO_BIT(sector); |
| 763 | ebnr = BM_SECT_TO_BIT(esector); |
| 764 | |
| 765 | return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr); |
| 766 | } |
| 767 | |
Philipp Reisner | aeda1cd6 | 2010-11-09 17:45:06 +0100 | [diff] [blame] | 768 | static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 769 | { |
| 770 | const int rw = bio_rw(bio); |
| 771 | const int size = bio->bi_size; |
| 772 | const sector_t sector = bio->bi_sector; |
| 773 | struct drbd_tl_epoch *b = NULL; |
| 774 | struct drbd_request *req; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 775 | int local, remote, send_oos = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 776 | int err = -EIO; |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 777 | int ret = 0; |
Philipp Reisner | fc28845 | 2012-01-16 12:14:01 +0100 | [diff] [blame] | 778 | union drbd_state s; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 779 | |
| 780 | /* allocate outside of all locks; */ |
| 781 | req = drbd_req_new(mdev, bio); |
| 782 | if (!req) { |
| 783 | dec_ap_bio(mdev); |
| 784 | /* only pass the error to the upper layers. |
| 785 | * if user cannot handle io errors, that's not our business. */ |
| 786 | dev_err(DEV, "could not kmalloc() req\n"); |
| 787 | bio_endio(bio, -ENOMEM); |
| 788 | return 0; |
| 789 | } |
Philipp Reisner | aeda1cd6 | 2010-11-09 17:45:06 +0100 | [diff] [blame] | 790 | req->start_time = start_time; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 791 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 792 | local = get_ldev(mdev); |
| 793 | if (!local) { |
| 794 | bio_put(req->private_bio); /* or we get a bio leak */ |
| 795 | req->private_bio = NULL; |
| 796 | } |
| 797 | if (rw == WRITE) { |
| 798 | remote = 1; |
| 799 | } else { |
| 800 | /* READ || READA */ |
| 801 | if (local) { |
| 802 | if (!drbd_may_do_local_read(mdev, sector, size)) { |
| 803 | /* we could kick the syncer to |
| 804 | * sync this extent asap, wait for |
| 805 | * it, then continue locally. |
| 806 | * Or just issue the request remotely. |
| 807 | */ |
| 808 | local = 0; |
| 809 | bio_put(req->private_bio); |
| 810 | req->private_bio = NULL; |
| 811 | put_ldev(mdev); |
| 812 | } |
| 813 | } |
| 814 | remote = !local && mdev->state.pdsk >= D_UP_TO_DATE; |
| 815 | } |
| 816 | |
| 817 | /* If we have a disk, but a READA request is mapped to remote, |
| 818 | * we are R_PRIMARY, D_INCONSISTENT, SyncTarget. |
| 819 | * Just fail that READA request right here. |
| 820 | * |
| 821 | * THINK: maybe fail all READA when not local? |
| 822 | * or make this configurable... |
| 823 | * if network is slow, READA won't do any good. |
| 824 | */ |
| 825 | if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) { |
| 826 | err = -EWOULDBLOCK; |
| 827 | goto fail_and_free_req; |
| 828 | } |
| 829 | |
| 830 | /* For WRITES going to the local disk, grab a reference on the target |
| 831 | * extent. This waits for any resync activity in the corresponding |
| 832 | * resync extent to finish, and, if necessary, pulls in the target |
| 833 | * extent into the activity log, which involves further disk io because |
| 834 | * of transactional on-disk meta data updates. */ |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 835 | if (rw == WRITE && local && !test_bit(AL_SUSPENDED, &mdev->flags)) { |
| 836 | req->rq_state |= RQ_IN_ACT_LOG; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 837 | drbd_al_begin_io(mdev, sector); |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 838 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 839 | |
Philipp Reisner | fc28845 | 2012-01-16 12:14:01 +0100 | [diff] [blame] | 840 | s = mdev->state; |
| 841 | remote = remote && drbd_should_do_remote(s); |
| 842 | send_oos = rw == WRITE && drbd_should_send_oos(s); |
Philipp Reisner | 3719094 | 2010-11-10 12:08:37 +0100 | [diff] [blame] | 843 | D_ASSERT(!(remote && send_oos)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 844 | |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 845 | if (!(local || remote) && !is_susp(mdev->state)) { |
Lars Ellenberg | fb2c7a1 | 2010-10-19 12:08:13 +0200 | [diff] [blame] | 846 | if (__ratelimit(&drbd_ratelimit_state)) |
| 847 | dev_err(DEV, "IO ERROR: neither local nor remote disk\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 848 | goto fail_free_complete; |
| 849 | } |
| 850 | |
| 851 | /* For WRITE request, we have to make sure that we have an |
| 852 | * unused_spare_tle, in case we need to start a new epoch. |
| 853 | * I try to be smart and avoid to pre-allocate always "just in case", |
| 854 | * but there is a race between testing the bit and pointer outside the |
| 855 | * spinlock, and grabbing the spinlock. |
| 856 | * if we lost that race, we retry. */ |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 857 | if (rw == WRITE && (remote || send_oos) && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 858 | mdev->unused_spare_tle == NULL && |
| 859 | test_bit(CREATE_BARRIER, &mdev->flags)) { |
| 860 | allocate_barrier: |
| 861 | b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO); |
| 862 | if (!b) { |
| 863 | dev_err(DEV, "Failed to alloc barrier.\n"); |
| 864 | err = -ENOMEM; |
| 865 | goto fail_free_complete; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | /* GOOD, everything prepared, grab the spin_lock */ |
| 870 | spin_lock_irq(&mdev->req_lock); |
| 871 | |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 872 | if (is_susp(mdev->state)) { |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 873 | /* If we got suspended, use the retry mechanism of |
| 874 | generic_make_request() to restart processing of this |
Andreas Gruenbacher | 2f58dcf | 2010-12-13 17:48:19 +0100 | [diff] [blame] | 875 | bio. In the next call to drbd_make_request |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 876 | we sleep in inc_ap_bio() */ |
| 877 | ret = 1; |
| 878 | spin_unlock_irq(&mdev->req_lock); |
| 879 | goto fail_free_complete; |
| 880 | } |
| 881 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 882 | if (remote || send_oos) { |
Philipp Reisner | 6a35c45 | 2011-01-17 20:27:30 +0100 | [diff] [blame] | 883 | remote = drbd_should_do_remote(mdev->state); |
| 884 | send_oos = rw == WRITE && drbd_should_send_oos(mdev->state); |
Philipp Reisner | 3719094 | 2010-11-10 12:08:37 +0100 | [diff] [blame] | 885 | D_ASSERT(!(remote && send_oos)); |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 886 | |
| 887 | if (!(remote || send_oos)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 888 | dev_warn(DEV, "lost connection while grabbing the req_lock!\n"); |
| 889 | if (!(local || remote)) { |
| 890 | dev_err(DEV, "IO ERROR: neither local nor remote disk\n"); |
| 891 | spin_unlock_irq(&mdev->req_lock); |
| 892 | goto fail_free_complete; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | if (b && mdev->unused_spare_tle == NULL) { |
| 897 | mdev->unused_spare_tle = b; |
| 898 | b = NULL; |
| 899 | } |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 900 | if (rw == WRITE && (remote || send_oos) && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 901 | mdev->unused_spare_tle == NULL && |
| 902 | test_bit(CREATE_BARRIER, &mdev->flags)) { |
| 903 | /* someone closed the current epoch |
| 904 | * while we were grabbing the spinlock */ |
| 905 | spin_unlock_irq(&mdev->req_lock); |
| 906 | goto allocate_barrier; |
| 907 | } |
| 908 | |
| 909 | |
| 910 | /* Update disk stats */ |
| 911 | _drbd_start_io_acct(mdev, req, bio); |
| 912 | |
| 913 | /* _maybe_start_new_epoch(mdev); |
| 914 | * If we need to generate a write barrier packet, we have to add the |
| 915 | * new epoch (barrier) object, and queue the barrier packet for sending, |
| 916 | * and queue the req's data after it _within the same lock_, otherwise |
| 917 | * we have race conditions were the reorder domains could be mixed up. |
| 918 | * |
| 919 | * Even read requests may start a new epoch and queue the corresponding |
| 920 | * barrier packet. To get the write ordering right, we only have to |
| 921 | * make sure that, if this is a write request and it triggered a |
| 922 | * barrier packet, this request is queued within the same spinlock. */ |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 923 | if ((remote || send_oos) && mdev->unused_spare_tle && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 924 | test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) { |
| 925 | _tl_add_barrier(mdev, mdev->unused_spare_tle); |
| 926 | mdev->unused_spare_tle = NULL; |
| 927 | } else { |
| 928 | D_ASSERT(!(remote && rw == WRITE && |
| 929 | test_bit(CREATE_BARRIER, &mdev->flags))); |
| 930 | } |
| 931 | |
| 932 | /* NOTE |
| 933 | * Actually, 'local' may be wrong here already, since we may have failed |
| 934 | * to write to the meta data, and may become wrong anytime because of |
| 935 | * local io-error for some other request, which would lead to us |
| 936 | * "detaching" the local disk. |
| 937 | * |
| 938 | * 'remote' may become wrong any time because the network could fail. |
| 939 | * |
| 940 | * This is a harmless race condition, though, since it is handled |
| 941 | * correctly at the appropriate places; so it just defers the failure |
| 942 | * of the respective operation. |
| 943 | */ |
| 944 | |
| 945 | /* mark them early for readability. |
| 946 | * this just sets some state flags. */ |
| 947 | if (remote) |
| 948 | _req_mod(req, to_be_send); |
| 949 | if (local) |
| 950 | _req_mod(req, to_be_submitted); |
| 951 | |
| 952 | /* check this request on the collision detection hash tables. |
| 953 | * if we have a conflict, just complete it here. |
| 954 | * THINK do we want to check reads, too? (I don't think so...) */ |
Lars Ellenberg | d28fd09 | 2010-07-09 23:28:10 +0200 | [diff] [blame] | 955 | if (rw == WRITE && _req_conflicts(req)) |
| 956 | goto fail_conflicting; |
Philipp Reisner | 288f422 | 2010-05-27 15:07:43 +0200 | [diff] [blame] | 957 | |
| 958 | list_add_tail(&req->tl_requests, &mdev->newest_tle->requests); |
| 959 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 960 | /* NOTE remote first: to get the concurrent write detection right, |
| 961 | * we must register the request before start of local IO. */ |
| 962 | if (remote) { |
| 963 | /* either WRITE and C_CONNECTED, |
| 964 | * or READ, and no local disk, |
| 965 | * or READ, but not in sync. |
| 966 | */ |
| 967 | _req_mod(req, (rw == WRITE) |
| 968 | ? queue_for_net_write |
| 969 | : queue_for_net_read); |
| 970 | } |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 971 | if (send_oos && drbd_set_out_of_sync(mdev, sector, size)) |
| 972 | _req_mod(req, queue_for_send_oos); |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 973 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 974 | if (remote && |
| 975 | mdev->net_conf->on_congestion != OC_BLOCK && mdev->agreed_pro_version >= 96) { |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 976 | int congested = 0; |
| 977 | |
| 978 | if (mdev->net_conf->cong_fill && |
| 979 | atomic_read(&mdev->ap_in_flight) >= mdev->net_conf->cong_fill) { |
| 980 | dev_info(DEV, "Congestion-fill threshold reached\n"); |
| 981 | congested = 1; |
| 982 | } |
| 983 | |
| 984 | if (mdev->act_log->used >= mdev->net_conf->cong_extents) { |
| 985 | dev_info(DEV, "Congestion-extents threshold reached\n"); |
| 986 | congested = 1; |
| 987 | } |
| 988 | |
Philipp Reisner | 71c78cf | 2011-01-14 19:20:34 +0100 | [diff] [blame] | 989 | if (congested) { |
Philipp Reisner | 039312b | 2011-01-21 14:13:22 +0100 | [diff] [blame] | 990 | queue_barrier(mdev); /* last barrier, after mirrored writes */ |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 991 | |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 992 | if (mdev->net_conf->on_congestion == OC_PULL_AHEAD) |
| 993 | _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL); |
| 994 | else /*mdev->net_conf->on_congestion == OC_DISCONNECT */ |
| 995 | _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL); |
| 996 | } |
| 997 | } |
| 998 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 999 | spin_unlock_irq(&mdev->req_lock); |
| 1000 | kfree(b); /* if someone else has beaten us to it... */ |
| 1001 | |
| 1002 | if (local) { |
| 1003 | req->private_bio->bi_bdev = mdev->ldev->backing_bdev; |
| 1004 | |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 1005 | /* State may have changed since we grabbed our reference on the |
| 1006 | * mdev->ldev member. Double check, and short-circuit to endio. |
| 1007 | * In case the last activity log transaction failed to get on |
| 1008 | * stable storage, and this is a WRITE, we may not even submit |
| 1009 | * this bio. */ |
| 1010 | if (get_ldev(mdev)) { |
Andreas Gruenbacher | 0cf9d27 | 2010-12-07 10:43:29 +0100 | [diff] [blame] | 1011 | if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR |
| 1012 | : rw == READ ? DRBD_FAULT_DT_RD |
| 1013 | : DRBD_FAULT_DT_RA)) |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 1014 | bio_endio(req->private_bio, -EIO); |
| 1015 | else |
| 1016 | generic_make_request(req->private_bio); |
| 1017 | put_ldev(mdev); |
| 1018 | } else |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1019 | bio_endio(req->private_bio, -EIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1022 | return 0; |
| 1023 | |
Lars Ellenberg | d28fd09 | 2010-07-09 23:28:10 +0200 | [diff] [blame] | 1024 | fail_conflicting: |
| 1025 | /* this is a conflicting request. |
| 1026 | * even though it may have been only _partially_ |
| 1027 | * overlapping with one of the currently pending requests, |
| 1028 | * without even submitting or sending it, we will |
| 1029 | * pretend that it was successfully served right now. |
| 1030 | */ |
| 1031 | _drbd_end_io_acct(mdev, req); |
| 1032 | spin_unlock_irq(&mdev->req_lock); |
| 1033 | if (remote) |
| 1034 | dec_ap_pending(mdev); |
| 1035 | /* THINK: do we want to fail it (-EIO), or pretend success? |
| 1036 | * this pretends success. */ |
| 1037 | err = 0; |
| 1038 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1039 | fail_free_complete: |
Lars Ellenberg | 76727f6 | 2011-05-16 15:31:45 +0200 | [diff] [blame] | 1040 | if (req->rq_state & RQ_IN_ACT_LOG) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1041 | drbd_al_complete_io(mdev, sector); |
| 1042 | fail_and_free_req: |
| 1043 | if (local) { |
| 1044 | bio_put(req->private_bio); |
| 1045 | req->private_bio = NULL; |
| 1046 | put_ldev(mdev); |
| 1047 | } |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 1048 | if (!ret) |
| 1049 | bio_endio(bio, err); |
| 1050 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1051 | drbd_req_free(req); |
| 1052 | dec_ap_bio(mdev); |
| 1053 | kfree(b); |
| 1054 | |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 1055 | return ret; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* helper function for drbd_make_request |
| 1059 | * if we can determine just by the mdev (state) that this request will fail, |
| 1060 | * return 1 |
| 1061 | * otherwise return 0 |
| 1062 | */ |
| 1063 | static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write) |
| 1064 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1065 | if (mdev->state.role != R_PRIMARY && |
| 1066 | (!allow_oos || is_write)) { |
| 1067 | if (__ratelimit(&drbd_ratelimit_state)) { |
| 1068 | dev_err(DEV, "Process %s[%u] tried to %s; " |
| 1069 | "since we are not in Primary state, " |
| 1070 | "we cannot allow this\n", |
| 1071 | current->comm, current->pid, |
| 1072 | is_write ? "WRITE" : "READ"); |
| 1073 | } |
| 1074 | return 1; |
| 1075 | } |
| 1076 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1077 | return 0; |
| 1078 | } |
| 1079 | |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1080 | void drbd_make_request(struct request_queue *q, struct bio *bio) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1081 | { |
| 1082 | unsigned int s_enr, e_enr; |
| 1083 | struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata; |
Philipp Reisner | aeda1cd6 | 2010-11-09 17:45:06 +0100 | [diff] [blame] | 1084 | unsigned long start_time; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1085 | |
| 1086 | if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) { |
| 1087 | bio_endio(bio, -EPERM); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1088 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Philipp Reisner | aeda1cd6 | 2010-11-09 17:45:06 +0100 | [diff] [blame] | 1091 | start_time = jiffies; |
| 1092 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1093 | /* |
| 1094 | * what we "blindly" assume: |
| 1095 | */ |
| 1096 | D_ASSERT(bio->bi_size > 0); |
| 1097 | D_ASSERT((bio->bi_size & 0x1ff) == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1098 | |
| 1099 | /* to make some things easier, force alignment of requests within the |
| 1100 | * granularity of our hash tables */ |
| 1101 | s_enr = bio->bi_sector >> HT_SHIFT; |
| 1102 | e_enr = (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT; |
| 1103 | |
| 1104 | if (likely(s_enr == e_enr)) { |
| 1105 | inc_ap_bio(mdev, 1); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1106 | drbd_make_request_common(mdev, bio, start_time); |
| 1107 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /* can this bio be split generically? |
| 1111 | * Maybe add our own split-arbitrary-bios function. */ |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1112 | if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_BIO_SIZE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1113 | /* rather error out here than BUG in bio_split */ |
| 1114 | dev_err(DEV, "bio would need to, but cannot, be split: " |
| 1115 | "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n", |
| 1116 | bio->bi_vcnt, bio->bi_idx, bio->bi_size, |
| 1117 | (unsigned long long)bio->bi_sector); |
| 1118 | bio_endio(bio, -EINVAL); |
| 1119 | } else { |
| 1120 | /* This bio crosses some boundary, so we have to split it. */ |
| 1121 | struct bio_pair *bp; |
| 1122 | /* works for the "do not cross hash slot boundaries" case |
| 1123 | * e.g. sector 262269, size 4096 |
| 1124 | * s_enr = 262269 >> 6 = 4097 |
| 1125 | * e_enr = (262269+8-1) >> 6 = 4098 |
| 1126 | * HT_SHIFT = 6 |
| 1127 | * sps = 64, mask = 63 |
| 1128 | * first_sectors = 64 - (262269 & 63) = 3 |
| 1129 | */ |
| 1130 | const sector_t sect = bio->bi_sector; |
| 1131 | const int sps = 1 << HT_SHIFT; /* sectors per slot */ |
| 1132 | const int mask = sps - 1; |
| 1133 | const sector_t first_sectors = sps - (sect & mask); |
Or Gerlitz | 0356781 | 2011-01-13 10:43:40 +0100 | [diff] [blame] | 1134 | bp = bio_split(bio, first_sectors); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1135 | |
| 1136 | /* we need to get a "reference count" (ap_bio_cnt) |
| 1137 | * to avoid races with the disconnect/reconnect/suspend code. |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 1138 | * In case we need to split the bio here, we need to get three references |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1139 | * atomically, otherwise we might deadlock when trying to submit the |
| 1140 | * second one! */ |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 1141 | inc_ap_bio(mdev, 3); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1142 | |
| 1143 | D_ASSERT(e_enr == s_enr + 1); |
| 1144 | |
Philipp Reisner | aeda1cd6 | 2010-11-09 17:45:06 +0100 | [diff] [blame] | 1145 | while (drbd_make_request_common(mdev, &bp->bio1, start_time)) |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 1146 | inc_ap_bio(mdev, 1); |
| 1147 | |
Philipp Reisner | aeda1cd6 | 2010-11-09 17:45:06 +0100 | [diff] [blame] | 1148 | while (drbd_make_request_common(mdev, &bp->bio2, start_time)) |
Philipp Reisner | 9a25a04 | 2010-05-10 16:42:23 +0200 | [diff] [blame] | 1149 | inc_ap_bio(mdev, 1); |
| 1150 | |
| 1151 | dec_ap_bio(mdev); |
| 1152 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1153 | bio_pair_release(bp); |
| 1154 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* This is called by bio_add_page(). With this function we reduce |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1158 | * the number of BIOs that span over multiple DRBD_MAX_BIO_SIZEs |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1159 | * units (was AL_EXTENTs). |
| 1160 | * |
| 1161 | * we do the calculation within the lower 32bit of the byte offsets, |
| 1162 | * since we don't care for actual offset, but only check whether it |
| 1163 | * would cross "activity log extent" boundaries. |
| 1164 | * |
| 1165 | * As long as the BIO is empty we have to allow at least one bvec, |
| 1166 | * regardless of size and offset. so the resulting bio may still |
| 1167 | * cross extent boundaries. those are dealt with (bio_split) in |
Andreas Gruenbacher | 2f58dcf | 2010-12-13 17:48:19 +0100 | [diff] [blame] | 1168 | * drbd_make_request. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1169 | */ |
| 1170 | int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec) |
| 1171 | { |
| 1172 | struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata; |
| 1173 | unsigned int bio_offset = |
| 1174 | (unsigned int)bvm->bi_sector << 9; /* 32 bit */ |
| 1175 | unsigned int bio_size = bvm->bi_size; |
| 1176 | int limit, backing_limit; |
| 1177 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1178 | limit = DRBD_MAX_BIO_SIZE |
| 1179 | - ((bio_offset & (DRBD_MAX_BIO_SIZE-1)) + bio_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1180 | if (limit < 0) |
| 1181 | limit = 0; |
| 1182 | if (bio_size == 0) { |
| 1183 | if (limit <= bvec->bv_len) |
| 1184 | limit = bvec->bv_len; |
| 1185 | } else if (limit && get_ldev(mdev)) { |
| 1186 | struct request_queue * const b = |
| 1187 | mdev->ldev->backing_bdev->bd_disk->queue; |
Lars Ellenberg | a1c88d0 | 2010-05-14 19:16:41 +0200 | [diff] [blame] | 1188 | if (b->merge_bvec_fn) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1189 | backing_limit = b->merge_bvec_fn(b, bvm, bvec); |
| 1190 | limit = min(limit, backing_limit); |
| 1191 | } |
| 1192 | put_ldev(mdev); |
| 1193 | } |
| 1194 | return limit; |
| 1195 | } |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1196 | |
| 1197 | void request_timer_fn(unsigned long data) |
| 1198 | { |
| 1199 | struct drbd_conf *mdev = (struct drbd_conf *) data; |
| 1200 | struct drbd_request *req; /* oldest request */ |
| 1201 | struct list_head *le; |
Philipp Reisner | dfa8bed | 2011-06-29 14:06:08 +0200 | [diff] [blame] | 1202 | unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */ |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1203 | unsigned long now; |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1204 | |
| 1205 | if (get_net_conf(mdev)) { |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1206 | if (mdev->state.conn >= C_WF_REPORT_PARAMS) |
| 1207 | ent = mdev->net_conf->timeout*HZ/10 |
| 1208 | * mdev->net_conf->ko_count; |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1209 | put_net_conf(mdev); |
| 1210 | } |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1211 | if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */ |
Philipp Reisner | dfa8bed | 2011-06-29 14:06:08 +0200 | [diff] [blame] | 1212 | dt = mdev->ldev->dc.disk_timeout * HZ / 10; |
| 1213 | put_ldev(mdev); |
| 1214 | } |
| 1215 | et = min_not_zero(dt, ent); |
| 1216 | |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1217 | if (!et) |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1218 | return; /* Recurring timer stopped */ |
| 1219 | |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1220 | now = jiffies; |
| 1221 | |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1222 | spin_lock_irq(&mdev->req_lock); |
| 1223 | le = &mdev->oldest_tle->requests; |
| 1224 | if (list_empty(le)) { |
| 1225 | spin_unlock_irq(&mdev->req_lock); |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1226 | mod_timer(&mdev->request_timer, now + et); |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | le = le->prev; |
| 1231 | req = list_entry(le, struct drbd_request, tl_requests); |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1232 | |
| 1233 | /* The request is considered timed out, if |
| 1234 | * - we have some effective timeout from the configuration, |
| 1235 | * with above state restrictions applied, |
| 1236 | * - the oldest request is waiting for a response from the network |
| 1237 | * resp. the local disk, |
| 1238 | * - the oldest request is in fact older than the effective timeout, |
| 1239 | * - the connection was established (resp. disk was attached) |
| 1240 | * for longer than the timeout already. |
| 1241 | * Note that for 32bit jiffies and very stable connections/disks, |
| 1242 | * we may have a wrap around, which is catched by |
| 1243 | * !time_in_range(now, last_..._jif, last_..._jif + timeout). |
| 1244 | * |
| 1245 | * Side effect: once per 32bit wrap-around interval, which means every |
| 1246 | * ~198 days with 250 HZ, we have a window where the timeout would need |
| 1247 | * to expire twice (worst case) to become effective. Good enough. |
| 1248 | */ |
| 1249 | if (ent && req->rq_state & RQ_NET_PENDING && |
| 1250 | time_after(now, req->start_time + ent) && |
| 1251 | !time_in_range(now, mdev->last_reconnect_jif, mdev->last_reconnect_jif + ent)) { |
| 1252 | dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n"); |
| 1253 | _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL); |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1254 | } |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1255 | if (dt && req->rq_state & RQ_LOCAL_PENDING && |
| 1256 | time_after(now, req->start_time + dt) && |
| 1257 | !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) { |
| 1258 | dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n"); |
| 1259 | __drbd_chk_io_error(mdev, 1); |
Philipp Reisner | dfa8bed | 2011-06-29 14:06:08 +0200 | [diff] [blame] | 1260 | } |
Lars Ellenberg | ba280c0 | 2012-04-25 11:46:14 +0200 | [diff] [blame] | 1261 | nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et; |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1262 | spin_unlock_irq(&mdev->req_lock); |
Philipp Reisner | dfa8bed | 2011-06-29 14:06:08 +0200 | [diff] [blame] | 1263 | mod_timer(&mdev->request_timer, nt); |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1264 | } |