aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/drbd/drbd_main.c
diff options
context:
space:
mode:
authorPhilipp Reisner <philipp.reisner@linbit.com>2012-11-09 08:18:43 -0500
committerPhilipp Reisner <philipp.reisner@linbit.com>2012-11-09 08:20:23 -0500
commit986836503e49ccf7e84b813715d344964ec93566 (patch)
treeb3bea7428efde5b77096cef80e5b6bfee494cc12 /drivers/block/drbd/drbd_main.c
parentccae7868b0c5697508a541c531cf96b361d62c1c (diff)
parent328e0f125bf41f4f33f684db22015f92cb44fe56 (diff)
Merge branch 'drbd-8.4_ed6' into for-3.8-drivers-drbd-8.4_ed6
Diffstat (limited to 'drivers/block/drbd/drbd_main.c')
-rw-r--r--drivers/block/drbd/drbd_main.c3773
1 files changed, 1445 insertions, 2328 deletions
diff --git a/drivers/block/drbd/drbd_main.c b/drivers/block/drbd/drbd_main.c
index 9b833e0fb440..52de26daa1f6 100644
--- a/drivers/block/drbd/drbd_main.c
+++ b/drivers/block/drbd/drbd_main.c
@@ -56,14 +56,6 @@
56 56
57#include "drbd_vli.h" 57#include "drbd_vli.h"
58 58
59struct after_state_chg_work {
60 struct drbd_work w;
61 union drbd_state os;
62 union drbd_state ns;
63 enum chg_state_flags flags;
64 struct completion *done;
65};
66
67static DEFINE_MUTEX(drbd_main_mutex); 59static DEFINE_MUTEX(drbd_main_mutex);
68int drbdd_init(struct drbd_thread *); 60int drbdd_init(struct drbd_thread *);
69int drbd_worker(struct drbd_thread *); 61int drbd_worker(struct drbd_thread *);
@@ -72,21 +64,17 @@ int drbd_asender(struct drbd_thread *);
72int drbd_init(void); 64int drbd_init(void);
73static int drbd_open(struct block_device *bdev, fmode_t mode); 65static int drbd_open(struct block_device *bdev, fmode_t mode);
74static int drbd_release(struct gendisk *gd, fmode_t mode); 66static int drbd_release(struct gendisk *gd, fmode_t mode);
75static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused); 67static int w_md_sync(struct drbd_work *w, int unused);
76static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
77 union drbd_state ns, enum chg_state_flags flags);
78static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
79static void md_sync_timer_fn(unsigned long data); 68static void md_sync_timer_fn(unsigned long data);
80static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused); 69static int w_bitmap_io(struct drbd_work *w, int unused);
81static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused); 70static int w_go_diskless(struct drbd_work *w, int unused);
82static void _tl_clear(struct drbd_conf *mdev);
83 71
84MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, " 72MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
85 "Lars Ellenberg <lars@linbit.com>"); 73 "Lars Ellenberg <lars@linbit.com>");
86MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION); 74MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
87MODULE_VERSION(REL_VERSION); 75MODULE_VERSION(REL_VERSION);
88MODULE_LICENSE("GPL"); 76MODULE_LICENSE("GPL");
89MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices (" 77MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
90 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")"); 78 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
91MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR); 79MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
92 80
@@ -98,7 +86,6 @@ MODULE_PARM_DESC(allow_oos, "DONT USE!");
98module_param(minor_count, uint, 0444); 86module_param(minor_count, uint, 0444);
99module_param(disable_sendpage, bool, 0644); 87module_param(disable_sendpage, bool, 0644);
100module_param(allow_oos, bool, 0); 88module_param(allow_oos, bool, 0);
101module_param(cn_idx, uint, 0444);
102module_param(proc_details, int, 0644); 89module_param(proc_details, int, 0644);
103 90
104#ifdef CONFIG_DRBD_FAULT_INJECTION 91#ifdef CONFIG_DRBD_FAULT_INJECTION
@@ -120,7 +107,6 @@ module_param(fault_devs, int, 0644);
120unsigned int minor_count = DRBD_MINOR_COUNT_DEF; 107unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
121bool disable_sendpage; 108bool disable_sendpage;
122bool allow_oos; 109bool allow_oos;
123unsigned int cn_idx = CN_IDX_DRBD;
124int proc_details; /* Detail level in proc drbd*/ 110int proc_details; /* Detail level in proc drbd*/
125 111
126/* Module parameter for setting the user mode helper program 112/* Module parameter for setting the user mode helper program
@@ -132,10 +118,11 @@ module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0
132/* in 2.6.x, our device mapping and config info contains our virtual gendisks 118/* in 2.6.x, our device mapping and config info contains our virtual gendisks
133 * as member "struct gendisk *vdisk;" 119 * as member "struct gendisk *vdisk;"
134 */ 120 */
135struct drbd_conf **minor_table; 121struct idr minors;
122struct list_head drbd_tconns; /* list of struct drbd_tconn */
136 123
137struct kmem_cache *drbd_request_cache; 124struct kmem_cache *drbd_request_cache;
138struct kmem_cache *drbd_ee_cache; /* epoch entries */ 125struct kmem_cache *drbd_ee_cache; /* peer requests */
139struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */ 126struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
140struct kmem_cache *drbd_al_ext_cache; /* activity log extents */ 127struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
141mempool_t *drbd_request_mempool; 128mempool_t *drbd_request_mempool;
@@ -164,10 +151,15 @@ static const struct block_device_operations drbd_ops = {
164 151
165struct bio *bio_alloc_drbd(gfp_t gfp_mask) 152struct bio *bio_alloc_drbd(gfp_t gfp_mask)
166{ 153{
154 struct bio *bio;
155
167 if (!drbd_md_io_bio_set) 156 if (!drbd_md_io_bio_set)
168 return bio_alloc(gfp_mask, 1); 157 return bio_alloc(gfp_mask, 1);
169 158
170 return bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set); 159 bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
160 if (!bio)
161 return NULL;
162 return bio;
171} 163}
172 164
173#ifdef __CHECKER__ 165#ifdef __CHECKER__
@@ -190,158 +182,87 @@ int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
190#endif 182#endif
191 183
192/** 184/**
193 * DOC: The transfer log 185 * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
194 * 186 * @tconn: DRBD connection.
195 * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
196 * mdev->newest_tle points to the head, mdev->oldest_tle points to the tail
197 * of the list. There is always at least one &struct drbd_tl_epoch object.
198 *
199 * Each &struct drbd_tl_epoch has a circular double linked list of requests
200 * attached.
201 */
202static int tl_init(struct drbd_conf *mdev)
203{
204 struct drbd_tl_epoch *b;
205
206 /* during device minor initialization, we may well use GFP_KERNEL */
207 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
208 if (!b)
209 return 0;
210 INIT_LIST_HEAD(&b->requests);
211 INIT_LIST_HEAD(&b->w.list);
212 b->next = NULL;
213 b->br_number = 4711;
214 b->n_writes = 0;
215 b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
216
217 mdev->oldest_tle = b;
218 mdev->newest_tle = b;
219 INIT_LIST_HEAD(&mdev->out_of_sequence_requests);
220 INIT_LIST_HEAD(&mdev->barrier_acked_requests);
221
222 mdev->tl_hash = NULL;
223 mdev->tl_hash_s = 0;
224
225 return 1;
226}
227
228static void tl_cleanup(struct drbd_conf *mdev)
229{
230 D_ASSERT(mdev->oldest_tle == mdev->newest_tle);
231 D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
232 kfree(mdev->oldest_tle);
233 mdev->oldest_tle = NULL;
234 kfree(mdev->unused_spare_tle);
235 mdev->unused_spare_tle = NULL;
236 kfree(mdev->tl_hash);
237 mdev->tl_hash = NULL;
238 mdev->tl_hash_s = 0;
239}
240
241/**
242 * _tl_add_barrier() - Adds a barrier to the transfer log
243 * @mdev: DRBD device.
244 * @new: Barrier to be added before the current head of the TL.
245 *
246 * The caller must hold the req_lock.
247 */
248void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
249{
250 struct drbd_tl_epoch *newest_before;
251
252 INIT_LIST_HEAD(&new->requests);
253 INIT_LIST_HEAD(&new->w.list);
254 new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
255 new->next = NULL;
256 new->n_writes = 0;
257
258 newest_before = mdev->newest_tle;
259 new->br_number = newest_before->br_number+1;
260 if (mdev->newest_tle != new) {
261 mdev->newest_tle->next = new;
262 mdev->newest_tle = new;
263 }
264}
265
266/**
267 * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
268 * @mdev: DRBD device.
269 * @barrier_nr: Expected identifier of the DRBD write barrier packet. 187 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
270 * @set_size: Expected number of requests before that barrier. 188 * @set_size: Expected number of requests before that barrier.
271 * 189 *
272 * In case the passed barrier_nr or set_size does not match the oldest 190 * In case the passed barrier_nr or set_size does not match the oldest
273 * &struct drbd_tl_epoch objects this function will cause a termination 191 * epoch of not yet barrier-acked requests, this function will cause a
274 * of the connection. 192 * termination of the connection.
275 */ 193 */
276void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr, 194void tl_release(struct drbd_tconn *tconn, unsigned int barrier_nr,
277 unsigned int set_size) 195 unsigned int set_size)
278{ 196{
279 struct drbd_tl_epoch *b, *nob; /* next old barrier */
280 struct list_head *le, *tle;
281 struct drbd_request *r; 197 struct drbd_request *r;
282 198 struct drbd_request *req = NULL;
283 spin_lock_irq(&mdev->req_lock); 199 int expect_epoch = 0;
284 200 int expect_size = 0;
285 b = mdev->oldest_tle; 201
202 spin_lock_irq(&tconn->req_lock);
203
204 /* find oldest not yet barrier-acked write request,
205 * count writes in its epoch. */
206 list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
207 const unsigned s = r->rq_state;
208 if (!req) {
209 if (!(s & RQ_WRITE))
210 continue;
211 if (!(s & RQ_NET_MASK))
212 continue;
213 if (s & RQ_NET_DONE)
214 continue;
215 req = r;
216 expect_epoch = req->epoch;
217 expect_size ++;
218 } else {
219 if (r->epoch != expect_epoch)
220 break;
221 if (!(s & RQ_WRITE))
222 continue;
223 /* if (s & RQ_DONE): not expected */
224 /* if (!(s & RQ_NET_MASK)): not expected */
225 expect_size++;
226 }
227 }
286 228
287 /* first some paranoia code */ 229 /* first some paranoia code */
288 if (b == NULL) { 230 if (req == NULL) {
289 dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n", 231 conn_err(tconn, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
290 barrier_nr); 232 barrier_nr);
291 goto bail; 233 goto bail;
292 } 234 }
293 if (b->br_number != barrier_nr) { 235 if (expect_epoch != barrier_nr) {
294 dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n", 236 conn_err(tconn, "BAD! BarrierAck #%u received, expected #%u!\n",
295 barrier_nr, b->br_number); 237 barrier_nr, expect_epoch);
296 goto bail; 238 goto bail;
297 } 239 }
298 if (b->n_writes != set_size) { 240
299 dev_err(DEV, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n", 241 if (expect_size != set_size) {
300 barrier_nr, set_size, b->n_writes); 242 conn_err(tconn, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
243 barrier_nr, set_size, expect_size);
301 goto bail; 244 goto bail;
302 } 245 }
303 246
304 /* Clean up list of requests processed during current epoch */ 247 /* Clean up list of requests processed during current epoch. */
305 list_for_each_safe(le, tle, &b->requests) { 248 /* this extra list walk restart is paranoia,
306 r = list_entry(le, struct drbd_request, tl_requests); 249 * to catch requests being barrier-acked "unexpectedly".
307 _req_mod(r, barrier_acked); 250 * It usually should find the same req again, or some READ preceding it. */
308 } 251 list_for_each_entry(req, &tconn->transfer_log, tl_requests)
309 /* There could be requests on the list waiting for completion 252 if (req->epoch == expect_epoch)
310 of the write to the local disk. To avoid corruptions of 253 break;
311 slab's data structures we have to remove the lists head. 254 list_for_each_entry_safe_from(req, r, &tconn->transfer_log, tl_requests) {
312 255 if (req->epoch != expect_epoch)
313 Also there could have been a barrier ack out of sequence, overtaking 256 break;
314 the write acks - which would be a bug and violating write ordering. 257 _req_mod(req, BARRIER_ACKED);
315 To not deadlock in case we lose connection while such requests are
316 still pending, we need some way to find them for the
317 _req_mode(connection_lost_while_pending).
318
319 These have been list_move'd to the out_of_sequence_requests list in
320 _req_mod(, barrier_acked) above.
321 */
322 list_splice_init(&b->requests, &mdev->barrier_acked_requests);
323
324 nob = b->next;
325 if (drbd_test_and_clear_flag(mdev, CREATE_BARRIER)) {
326 _tl_add_barrier(mdev, b);
327 if (nob)
328 mdev->oldest_tle = nob;
329 /* if nob == NULL b was the only barrier, and becomes the new
330 barrier. Therefore mdev->oldest_tle points already to b */
331 } else {
332 D_ASSERT(nob != NULL);
333 mdev->oldest_tle = nob;
334 kfree(b);
335 } 258 }
336 259 spin_unlock_irq(&tconn->req_lock);
337 spin_unlock_irq(&mdev->req_lock);
338 dec_ap_pending(mdev);
339 260
340 return; 261 return;
341 262
342bail: 263bail:
343 spin_unlock_irq(&mdev->req_lock); 264 spin_unlock_irq(&tconn->req_lock);
344 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR)); 265 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
345} 266}
346 267
347 268
@@ -350,85 +271,24 @@ bail:
350 * @mdev: DRBD device. 271 * @mdev: DRBD device.
351 * @what: The action/event to perform with all request objects 272 * @what: The action/event to perform with all request objects
352 * 273 *
353 * @what might be one of connection_lost_while_pending, resend, fail_frozen_disk_io, 274 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
354 * restart_frozen_disk_io. 275 * RESTART_FROZEN_DISK_IO.
355 */ 276 */
356static void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what) 277/* must hold resource->req_lock */
357{ 278void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
358 struct drbd_tl_epoch *b, *tmp, **pn; 279{
359 struct list_head *le, *tle, carry_reads; 280 struct drbd_request *req, *r;
360 struct drbd_request *req;
361 int rv, n_writes, n_reads;
362
363 b = mdev->oldest_tle;
364 pn = &mdev->oldest_tle;
365 while (b) {
366 n_writes = 0;
367 n_reads = 0;
368 INIT_LIST_HEAD(&carry_reads);
369 list_for_each_safe(le, tle, &b->requests) {
370 req = list_entry(le, struct drbd_request, tl_requests);
371 rv = _req_mod(req, what);
372
373 n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
374 n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
375 }
376 tmp = b->next;
377
378 if (n_writes) {
379 if (what == resend) {
380 b->n_writes = n_writes;
381 if (b->w.cb == NULL) {
382 b->w.cb = w_send_barrier;
383 inc_ap_pending(mdev);
384 drbd_set_flag(mdev, CREATE_BARRIER);
385 }
386
387 drbd_queue_work(&mdev->data.work, &b->w);
388 }
389 pn = &b->next;
390 } else {
391 if (n_reads)
392 list_add(&carry_reads, &b->requests);
393 /* there could still be requests on that ring list,
394 * in case local io is still pending */
395 list_del(&b->requests);
396
397 /* dec_ap_pending corresponding to queue_barrier.
398 * the newest barrier may not have been queued yet,
399 * in which case w.cb is still NULL. */
400 if (b->w.cb != NULL)
401 dec_ap_pending(mdev);
402
403 if (b == mdev->newest_tle) {
404 /* recycle, but reinit! */
405 D_ASSERT(tmp == NULL);
406 INIT_LIST_HEAD(&b->requests);
407 list_splice(&carry_reads, &b->requests);
408 INIT_LIST_HEAD(&b->w.list);
409 b->w.cb = NULL;
410 b->br_number = net_random();
411 b->n_writes = 0;
412
413 *pn = b;
414 break;
415 }
416 *pn = tmp;
417 kfree(b);
418 }
419 b = tmp;
420 list_splice(&carry_reads, &b->requests);
421 }
422
423 /* Actions operating on the disk state, also want to work on
424 requests that got barrier acked. */
425 281
426 list_for_each_safe(le, tle, &mdev->barrier_acked_requests) { 282 list_for_each_entry_safe(req, r, &tconn->transfer_log, tl_requests)
427 req = list_entry(le, struct drbd_request, tl_requests);
428 _req_mod(req, what); 283 _req_mod(req, what);
429 }
430} 284}
431 285
286void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
287{
288 spin_lock_irq(&tconn->req_lock);
289 _tl_restart(tconn, what);
290 spin_unlock_irq(&tconn->req_lock);
291}
432 292
433/** 293/**
434 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL 294 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
@@ -438,43 +298,9 @@ static void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
438 * by the requests on the transfer gets marked as our of sync. Called from the 298 * by the requests on the transfer gets marked as our of sync. Called from the
439 * receiver thread and the worker thread. 299 * receiver thread and the worker thread.
440 */ 300 */
441void tl_clear(struct drbd_conf *mdev) 301void tl_clear(struct drbd_tconn *tconn)
442{ 302{
443 spin_lock_irq(&mdev->req_lock); 303 tl_restart(tconn, CONNECTION_LOST_WHILE_PENDING);
444 _tl_clear(mdev);
445 spin_unlock_irq(&mdev->req_lock);
446}
447
448static void _tl_clear(struct drbd_conf *mdev)
449{
450 struct list_head *le, *tle;
451 struct drbd_request *r;
452
453 _tl_restart(mdev, connection_lost_while_pending);
454
455 /* we expect this list to be empty. */
456 D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
457
458 /* but just in case, clean it up anyways! */
459 list_for_each_safe(le, tle, &mdev->out_of_sequence_requests) {
460 r = list_entry(le, struct drbd_request, tl_requests);
461 /* It would be nice to complete outside of spinlock.
462 * But this is easier for now. */
463 _req_mod(r, connection_lost_while_pending);
464 }
465
466 /* ensure bit indicating barrier is required is clear */
467 drbd_clear_flag(mdev, CREATE_BARRIER);
468
469 memset(mdev->app_reads_hash, 0, APP_R_HSIZE*sizeof(void *));
470
471}
472
473void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
474{
475 spin_lock_irq(&mdev->req_lock);
476 _tl_restart(mdev, what);
477 spin_unlock_irq(&mdev->req_lock);
478} 304}
479 305
480/** 306/**
@@ -483,1392 +309,131 @@ void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
483 */ 309 */
484void tl_abort_disk_io(struct drbd_conf *mdev) 310void tl_abort_disk_io(struct drbd_conf *mdev)
485{ 311{
486 struct drbd_tl_epoch *b; 312 struct drbd_tconn *tconn = mdev->tconn;
487 struct list_head *le, *tle; 313 struct drbd_request *req, *r;
488 struct drbd_request *req;
489 314
490 spin_lock_irq(&mdev->req_lock); 315 spin_lock_irq(&tconn->req_lock);
491 b = mdev->oldest_tle; 316 list_for_each_entry_safe(req, r, &tconn->transfer_log, tl_requests) {
492 while (b) {
493 list_for_each_safe(le, tle, &b->requests) {
494 req = list_entry(le, struct drbd_request, tl_requests);
495 if (!(req->rq_state & RQ_LOCAL_PENDING))
496 continue;
497 _req_mod(req, abort_disk_io);
498 }
499 b = b->next;
500 }
501
502 list_for_each_safe(le, tle, &mdev->barrier_acked_requests) {
503 req = list_entry(le, struct drbd_request, tl_requests);
504 if (!(req->rq_state & RQ_LOCAL_PENDING)) 317 if (!(req->rq_state & RQ_LOCAL_PENDING))
505 continue; 318 continue;
506 _req_mod(req, abort_disk_io); 319 if (req->w.mdev != mdev)
507 } 320 continue;
508 321 _req_mod(req, ABORT_DISK_IO);
509 spin_unlock_irq(&mdev->req_lock);
510}
511
512/**
513 * cl_wide_st_chg() - true if the state change is a cluster wide one
514 * @mdev: DRBD device.
515 * @os: old (current) state.
516 * @ns: new (wanted) state.
517 */
518static int cl_wide_st_chg(struct drbd_conf *mdev,
519 union drbd_state os, union drbd_state ns)
520{
521 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
522 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
523 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
524 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
525 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
526 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
527 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S);
528}
529
530enum drbd_state_rv
531drbd_change_state(struct drbd_conf *mdev, enum chg_state_flags f,
532 union drbd_state mask, union drbd_state val)
533{
534 unsigned long flags;
535 union drbd_state os, ns;
536 enum drbd_state_rv rv;
537
538 spin_lock_irqsave(&mdev->req_lock, flags);
539 os = mdev->state;
540 ns.i = (os.i & ~mask.i) | val.i;
541 rv = _drbd_set_state(mdev, ns, f, NULL);
542 ns = mdev->state;
543 spin_unlock_irqrestore(&mdev->req_lock, flags);
544
545 return rv;
546}
547
548/**
549 * drbd_force_state() - Impose a change which happens outside our control on our state
550 * @mdev: DRBD device.
551 * @mask: mask of state bits to change.
552 * @val: value of new state bits.
553 */
554void drbd_force_state(struct drbd_conf *mdev,
555 union drbd_state mask, union drbd_state val)
556{
557 drbd_change_state(mdev, CS_HARD, mask, val);
558}
559
560static enum drbd_state_rv is_valid_state(struct drbd_conf *, union drbd_state);
561static enum drbd_state_rv is_valid_state_transition(struct drbd_conf *,
562 union drbd_state,
563 union drbd_state);
564enum sanitize_state_warnings {
565 NO_WARNING,
566 ABORTED_ONLINE_VERIFY,
567 ABORTED_RESYNC,
568 CONNECTION_LOST_NEGOTIATING,
569 IMPLICITLY_UPGRADED_DISK,
570 IMPLICITLY_UPGRADED_PDSK,
571};
572static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
573 union drbd_state ns, enum sanitize_state_warnings *warn);
574int drbd_send_state_req(struct drbd_conf *,
575 union drbd_state, union drbd_state);
576
577static enum drbd_state_rv
578_req_st_cond(struct drbd_conf *mdev, union drbd_state mask,
579 union drbd_state val)
580{
581 union drbd_state os, ns;
582 unsigned long flags;
583 enum drbd_state_rv rv;
584
585 if (drbd_test_and_clear_flag(mdev, CL_ST_CHG_SUCCESS))
586 return SS_CW_SUCCESS;
587
588 if (drbd_test_and_clear_flag(mdev, CL_ST_CHG_FAIL))
589 return SS_CW_FAILED_BY_PEER;
590
591 rv = 0;
592 spin_lock_irqsave(&mdev->req_lock, flags);
593 os = mdev->state;
594 ns.i = (os.i & ~mask.i) | val.i;
595 ns = sanitize_state(mdev, os, ns, NULL);
596
597 if (!cl_wide_st_chg(mdev, os, ns))
598 rv = SS_CW_NO_NEED;
599 if (!rv) {
600 rv = is_valid_state(mdev, ns);
601 if (rv == SS_SUCCESS) {
602 rv = is_valid_state_transition(mdev, ns, os);
603 if (rv == SS_SUCCESS)
604 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
605 }
606 }
607 spin_unlock_irqrestore(&mdev->req_lock, flags);
608
609 return rv;
610}
611
612/**
613 * drbd_req_state() - Perform an eventually cluster wide state change
614 * @mdev: DRBD device.
615 * @mask: mask of state bits to change.
616 * @val: value of new state bits.
617 * @f: flags
618 *
619 * Should not be called directly, use drbd_request_state() or
620 * _drbd_request_state().
621 */
622static enum drbd_state_rv
623drbd_req_state(struct drbd_conf *mdev, union drbd_state mask,
624 union drbd_state val, enum chg_state_flags f)
625{
626 struct completion done;
627 unsigned long flags;
628 union drbd_state os, ns;
629 enum drbd_state_rv rv;
630
631 init_completion(&done);
632
633 if (f & CS_SERIALIZE)
634 mutex_lock(&mdev->state_mutex);
635
636 spin_lock_irqsave(&mdev->req_lock, flags);
637 os = mdev->state;
638 ns.i = (os.i & ~mask.i) | val.i;
639 ns = sanitize_state(mdev, os, ns, NULL);
640
641 if (cl_wide_st_chg(mdev, os, ns)) {
642 rv = is_valid_state(mdev, ns);
643 if (rv == SS_SUCCESS)
644 rv = is_valid_state_transition(mdev, ns, os);
645 spin_unlock_irqrestore(&mdev->req_lock, flags);
646
647 if (rv < SS_SUCCESS) {
648 if (f & CS_VERBOSE)
649 print_st_err(mdev, os, ns, rv);
650 goto abort;
651 }
652
653 drbd_state_lock(mdev);
654 if (!drbd_send_state_req(mdev, mask, val)) {
655 drbd_state_unlock(mdev);
656 rv = SS_CW_FAILED_BY_PEER;
657 if (f & CS_VERBOSE)
658 print_st_err(mdev, os, ns, rv);
659 goto abort;
660 }
661
662 if (mask.conn == C_MASK && val.conn == C_DISCONNECTING)
663 drbd_set_flag(mdev, DISCONNECT_SENT);
664
665 wait_event(mdev->state_wait,
666 (rv = _req_st_cond(mdev, mask, val)));
667
668 if (rv < SS_SUCCESS) {
669 drbd_state_unlock(mdev);
670 if (f & CS_VERBOSE)
671 print_st_err(mdev, os, ns, rv);
672 goto abort;
673 }
674 spin_lock_irqsave(&mdev->req_lock, flags);
675 os = mdev->state;
676 ns.i = (os.i & ~mask.i) | val.i;
677 rv = _drbd_set_state(mdev, ns, f, &done);
678 drbd_state_unlock(mdev);
679 } else {
680 rv = _drbd_set_state(mdev, ns, f, &done);
681 }
682
683 spin_unlock_irqrestore(&mdev->req_lock, flags);
684
685 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
686 D_ASSERT(current != mdev->worker.task);
687 wait_for_completion(&done);
688 }
689
690abort:
691 if (f & CS_SERIALIZE)
692 mutex_unlock(&mdev->state_mutex);
693
694 return rv;
695}
696
697/**
698 * _drbd_request_state() - Request a state change (with flags)
699 * @mdev: DRBD device.
700 * @mask: mask of state bits to change.
701 * @val: value of new state bits.
702 * @f: flags
703 *
704 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
705 * flag, or when logging of failed state change requests is not desired.
706 */
707enum drbd_state_rv
708_drbd_request_state(struct drbd_conf *mdev, union drbd_state mask,
709 union drbd_state val, enum chg_state_flags f)
710{
711 enum drbd_state_rv rv;
712
713 wait_event(mdev->state_wait,
714 (rv = drbd_req_state(mdev, mask, val, f)) != SS_IN_TRANSIENT_STATE);
715
716 return rv;
717}
718
719static void print_st(struct drbd_conf *mdev, char *name, union drbd_state ns)
720{
721 dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c }\n",
722 name,
723 drbd_conn_str(ns.conn),
724 drbd_role_str(ns.role),
725 drbd_role_str(ns.peer),
726 drbd_disk_str(ns.disk),
727 drbd_disk_str(ns.pdsk),
728 is_susp(ns) ? 's' : 'r',
729 ns.aftr_isp ? 'a' : '-',
730 ns.peer_isp ? 'p' : '-',
731 ns.user_isp ? 'u' : '-'
732 );
733}
734
735void print_st_err(struct drbd_conf *mdev, union drbd_state os,
736 union drbd_state ns, enum drbd_state_rv err)
737{
738 if (err == SS_IN_TRANSIENT_STATE)
739 return;
740 dev_err(DEV, "State change failed: %s\n", drbd_set_st_err_str(err));
741 print_st(mdev, " state", os);
742 print_st(mdev, "wanted", ns);
743}
744
745
746/**
747 * is_valid_state() - Returns an SS_ error code if ns is not valid
748 * @mdev: DRBD device.
749 * @ns: State to consider.
750 */
751static enum drbd_state_rv
752is_valid_state(struct drbd_conf *mdev, union drbd_state ns)
753{
754 /* See drbd_state_sw_errors in drbd_strings.c */
755
756 enum drbd_fencing_p fp;
757 enum drbd_state_rv rv = SS_SUCCESS;
758
759 fp = FP_DONT_CARE;
760 if (get_ldev(mdev)) {
761 fp = mdev->ldev->dc.fencing;
762 put_ldev(mdev);
763 }
764
765 if (get_net_conf(mdev)) {
766 if (!mdev->net_conf->two_primaries &&
767 ns.role == R_PRIMARY && ns.peer == R_PRIMARY)
768 rv = SS_TWO_PRIMARIES;
769 put_net_conf(mdev);
770 }
771
772 if (rv <= 0)
773 /* already found a reason to abort */;
774 else if (ns.role == R_SECONDARY && mdev->open_cnt)
775 rv = SS_DEVICE_IN_USE;
776
777 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
778 rv = SS_NO_UP_TO_DATE_DISK;
779
780 else if (fp >= FP_RESOURCE &&
781 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
782 rv = SS_PRIMARY_NOP;
783
784 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
785 rv = SS_NO_UP_TO_DATE_DISK;
786
787 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
788 rv = SS_NO_LOCAL_DISK;
789
790 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
791 rv = SS_NO_REMOTE_DISK;
792
793 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
794 rv = SS_NO_UP_TO_DATE_DISK;
795
796 else if ((ns.conn == C_CONNECTED ||
797 ns.conn == C_WF_BITMAP_S ||
798 ns.conn == C_SYNC_SOURCE ||
799 ns.conn == C_PAUSED_SYNC_S) &&
800 ns.disk == D_OUTDATED)
801 rv = SS_CONNECTED_OUTDATES;
802
803 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
804 (mdev->sync_conf.verify_alg[0] == 0))
805 rv = SS_NO_VERIFY_ALG;
806
807 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
808 mdev->agreed_pro_version < 88)
809 rv = SS_NOT_SUPPORTED;
810
811 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
812 rv = SS_CONNECTED_OUTDATES;
813
814 return rv;
815}
816
817/**
818 * is_valid_state_transition() - Returns an SS_ error code if the state transition is not possible
819 * @mdev: DRBD device.
820 * @ns: new state.
821 * @os: old state.
822 */
823static enum drbd_state_rv
824is_valid_state_transition(struct drbd_conf *mdev, union drbd_state ns,
825 union drbd_state os)
826{
827 enum drbd_state_rv rv = SS_SUCCESS;
828
829 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
830 os.conn > C_CONNECTED)
831 rv = SS_RESYNC_RUNNING;
832
833 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
834 rv = SS_ALREADY_STANDALONE;
835
836 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
837 rv = SS_IS_DISKLESS;
838
839 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
840 rv = SS_NO_NET_CONFIG;
841
842 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
843 rv = SS_LOWER_THAN_OUTDATED;
844
845 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
846 rv = SS_IN_TRANSIENT_STATE;
847
848 if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
849 rv = SS_IN_TRANSIENT_STATE;
850
851 /* While establishing a connection only allow cstate to change.
852 Delay/refuse role changes, detach attach etc... */
853 if (drbd_test_flag(mdev, STATE_SENT) &&
854 !(os.conn == C_WF_REPORT_PARAMS ||
855 (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
856 rv = SS_IN_TRANSIENT_STATE;
857
858 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
859 rv = SS_NEED_CONNECTION;
860
861 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
862 ns.conn != os.conn && os.conn > C_CONNECTED)
863 rv = SS_RESYNC_RUNNING;
864
865 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
866 os.conn < C_CONNECTED)
867 rv = SS_NEED_CONNECTION;
868
869 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
870 && os.conn < C_WF_REPORT_PARAMS)
871 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
872
873 return rv;
874}
875
876static void print_sanitize_warnings(struct drbd_conf *mdev, enum sanitize_state_warnings warn)
877{
878 static const char *msg_table[] = {
879 [NO_WARNING] = "",
880 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
881 [ABORTED_RESYNC] = "Resync aborted.",
882 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
883 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
884 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
885 };
886
887 if (warn != NO_WARNING)
888 dev_warn(DEV, "%s\n", msg_table[warn]);
889}
890
891/**
892 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
893 * @mdev: DRBD device.
894 * @os: old state.
895 * @ns: new state.
896 * @warn_sync_abort:
897 *
898 * When we loose connection, we have to set the state of the peers disk (pdsk)
899 * to D_UNKNOWN. This rule and many more along those lines are in this function.
900 */
901static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
902 union drbd_state ns, enum sanitize_state_warnings *warn)
903{
904 enum drbd_fencing_p fp;
905 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
906
907 if (warn)
908 *warn = NO_WARNING;
909
910 fp = FP_DONT_CARE;
911 if (get_ldev(mdev)) {
912 fp = mdev->ldev->dc.fencing;
913 put_ldev(mdev);
914 }
915
916 /* Disallow Network errors to configure a device's network part */
917 if ((ns.conn >= C_TIMEOUT && ns.conn <= C_TEAR_DOWN) &&
918 os.conn <= C_DISCONNECTING)
919 ns.conn = os.conn;
920
921 /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow.
922 * If you try to go into some Sync* state, that shall fail (elsewhere). */
923 if (os.conn >= C_TIMEOUT && os.conn <= C_TEAR_DOWN &&
924 ns.conn != C_UNCONNECTED && ns.conn != C_DISCONNECTING && ns.conn <= C_CONNECTED)
925 ns.conn = os.conn;
926
927 /* we cannot fail (again) if we already detached */
928 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
929 ns.disk = D_DISKLESS;
930
931 /* After C_DISCONNECTING only C_STANDALONE may follow */
932 if (os.conn == C_DISCONNECTING && ns.conn != C_STANDALONE)
933 ns.conn = os.conn;
934
935 if (ns.conn < C_CONNECTED) {
936 ns.peer_isp = 0;
937 ns.peer = R_UNKNOWN;
938 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
939 ns.pdsk = D_UNKNOWN;
940 }
941
942 /* Clear the aftr_isp when becoming unconfigured */
943 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
944 ns.aftr_isp = 0;
945
946 /* Abort resync if a disk fails/detaches */
947 if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
948 (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
949 if (warn)
950 *warn = os.conn == C_VERIFY_S || os.conn == C_VERIFY_T ?
951 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
952 ns.conn = C_CONNECTED;
953 }
954
955 /* Connection breaks down before we finished "Negotiating" */
956 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
957 get_ldev_if_state(mdev, D_NEGOTIATING)) {
958 if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
959 ns.disk = mdev->new_state_tmp.disk;
960 ns.pdsk = mdev->new_state_tmp.pdsk;
961 } else {
962 if (warn)
963 *warn = CONNECTION_LOST_NEGOTIATING;
964 ns.disk = D_DISKLESS;
965 ns.pdsk = D_UNKNOWN;
966 }
967 put_ldev(mdev);
968 }
969
970 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
971 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
972 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
973 ns.disk = D_UP_TO_DATE;
974 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
975 ns.pdsk = D_UP_TO_DATE;
976 }
977
978 /* Implications of the connection stat on the disk states */
979 disk_min = D_DISKLESS;
980 disk_max = D_UP_TO_DATE;
981 pdsk_min = D_INCONSISTENT;
982 pdsk_max = D_UNKNOWN;
983 switch ((enum drbd_conns)ns.conn) {
984 case C_WF_BITMAP_T:
985 case C_PAUSED_SYNC_T:
986 case C_STARTING_SYNC_T:
987 case C_WF_SYNC_UUID:
988 case C_BEHIND:
989 disk_min = D_INCONSISTENT;
990 disk_max = D_OUTDATED;
991 pdsk_min = D_UP_TO_DATE;
992 pdsk_max = D_UP_TO_DATE;
993 break;
994 case C_VERIFY_S:
995 case C_VERIFY_T:
996 disk_min = D_UP_TO_DATE;
997 disk_max = D_UP_TO_DATE;
998 pdsk_min = D_UP_TO_DATE;
999 pdsk_max = D_UP_TO_DATE;
1000 break;
1001 case C_CONNECTED:
1002 disk_min = D_DISKLESS;
1003 disk_max = D_UP_TO_DATE;
1004 pdsk_min = D_DISKLESS;
1005 pdsk_max = D_UP_TO_DATE;
1006 break;
1007 case C_WF_BITMAP_S:
1008 case C_PAUSED_SYNC_S:
1009 case C_STARTING_SYNC_S:
1010 case C_AHEAD:
1011 disk_min = D_UP_TO_DATE;
1012 disk_max = D_UP_TO_DATE;
1013 pdsk_min = D_INCONSISTENT;
1014 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
1015 break;
1016 case C_SYNC_TARGET:
1017 disk_min = D_INCONSISTENT;
1018 disk_max = D_INCONSISTENT;
1019 pdsk_min = D_UP_TO_DATE;
1020 pdsk_max = D_UP_TO_DATE;
1021 break;
1022 case C_SYNC_SOURCE:
1023 disk_min = D_UP_TO_DATE;
1024 disk_max = D_UP_TO_DATE;
1025 pdsk_min = D_INCONSISTENT;
1026 pdsk_max = D_INCONSISTENT;
1027 break;
1028 case C_STANDALONE:
1029 case C_DISCONNECTING:
1030 case C_UNCONNECTED:
1031 case C_TIMEOUT:
1032 case C_BROKEN_PIPE:
1033 case C_NETWORK_FAILURE:
1034 case C_PROTOCOL_ERROR:
1035 case C_TEAR_DOWN:
1036 case C_WF_CONNECTION:
1037 case C_WF_REPORT_PARAMS:
1038 case C_MASK:
1039 break;
1040 }
1041 if (ns.disk > disk_max)
1042 ns.disk = disk_max;
1043
1044 if (ns.disk < disk_min) {
1045 if (warn)
1046 *warn = IMPLICITLY_UPGRADED_DISK;
1047 ns.disk = disk_min;
1048 }
1049 if (ns.pdsk > pdsk_max)
1050 ns.pdsk = pdsk_max;
1051
1052 if (ns.pdsk < pdsk_min) {
1053 if (warn)
1054 *warn = IMPLICITLY_UPGRADED_PDSK;
1055 ns.pdsk = pdsk_min;
1056 }
1057
1058 if (fp == FP_STONITH &&
1059 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
1060 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
1061 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
1062
1063 if (mdev->sync_conf.on_no_data == OND_SUSPEND_IO &&
1064 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
1065 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
1066 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
1067
1068 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
1069 if (ns.conn == C_SYNC_SOURCE)
1070 ns.conn = C_PAUSED_SYNC_S;
1071 if (ns.conn == C_SYNC_TARGET)
1072 ns.conn = C_PAUSED_SYNC_T;
1073 } else {
1074 if (ns.conn == C_PAUSED_SYNC_S)
1075 ns.conn = C_SYNC_SOURCE;
1076 if (ns.conn == C_PAUSED_SYNC_T)
1077 ns.conn = C_SYNC_TARGET;
1078 }
1079
1080 return ns;
1081}
1082
1083/* helper for __drbd_set_state */
1084static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
1085{
1086 if (mdev->agreed_pro_version < 90)
1087 mdev->ov_start_sector = 0;
1088 mdev->rs_total = drbd_bm_bits(mdev);
1089 mdev->ov_position = 0;
1090 if (cs == C_VERIFY_T) {
1091 /* starting online verify from an arbitrary position
1092 * does not fit well into the existing protocol.
1093 * on C_VERIFY_T, we initialize ov_left and friends
1094 * implicitly in receive_DataRequest once the
1095 * first P_OV_REQUEST is received */
1096 mdev->ov_start_sector = ~(sector_t)0;
1097 } else {
1098 unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
1099 if (bit >= mdev->rs_total) {
1100 mdev->ov_start_sector =
1101 BM_BIT_TO_SECT(mdev->rs_total - 1);
1102 mdev->rs_total = 1;
1103 } else
1104 mdev->rs_total -= bit;
1105 mdev->ov_position = mdev->ov_start_sector;
1106 }
1107 mdev->ov_left = mdev->rs_total;
1108}
1109
1110static void drbd_resume_al(struct drbd_conf *mdev)
1111{
1112 if (drbd_test_and_clear_flag(mdev, AL_SUSPENDED))
1113 dev_info(DEV, "Resumed AL updates\n");
1114}
1115
1116/**
1117 * __drbd_set_state() - Set a new DRBD state
1118 * @mdev: DRBD device.
1119 * @ns: new state.
1120 * @flags: Flags
1121 * @done: Optional completion, that will get completed after the after_state_ch() finished
1122 *
1123 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
1124 */
1125enum drbd_state_rv
1126__drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1127 enum chg_state_flags flags, struct completion *done)
1128{
1129 union drbd_state os;
1130 enum drbd_state_rv rv = SS_SUCCESS;
1131 enum sanitize_state_warnings ssw;
1132 struct after_state_chg_work *ascw;
1133
1134 os = mdev->state;
1135
1136 ns = sanitize_state(mdev, os, ns, &ssw);
1137
1138 if (ns.i == os.i)
1139 return SS_NOTHING_TO_DO;
1140
1141 if (!(flags & CS_HARD)) {
1142 /* pre-state-change checks ; only look at ns */
1143 /* See drbd_state_sw_errors in drbd_strings.c */
1144
1145 rv = is_valid_state(mdev, ns);
1146 if (rv < SS_SUCCESS) {
1147 /* If the old state was illegal as well, then let
1148 this happen...*/
1149
1150 if (is_valid_state(mdev, os) == rv)
1151 rv = is_valid_state_transition(mdev, ns, os);
1152 } else
1153 rv = is_valid_state_transition(mdev, ns, os);
1154 }
1155
1156 if (rv < SS_SUCCESS) {
1157 if (flags & CS_VERBOSE)
1158 print_st_err(mdev, os, ns, rv);
1159 return rv;
1160 }
1161
1162 print_sanitize_warnings(mdev, ssw);
1163
1164 {
1165 char *pbp, pb[300];
1166 pbp = pb;
1167 *pbp = 0;
1168 if (ns.role != os.role)
1169 pbp += sprintf(pbp, "role( %s -> %s ) ",
1170 drbd_role_str(os.role),
1171 drbd_role_str(ns.role));
1172 if (ns.peer != os.peer)
1173 pbp += sprintf(pbp, "peer( %s -> %s ) ",
1174 drbd_role_str(os.peer),
1175 drbd_role_str(ns.peer));
1176 if (ns.conn != os.conn)
1177 pbp += sprintf(pbp, "conn( %s -> %s ) ",
1178 drbd_conn_str(os.conn),
1179 drbd_conn_str(ns.conn));
1180 if (ns.disk != os.disk)
1181 pbp += sprintf(pbp, "disk( %s -> %s ) ",
1182 drbd_disk_str(os.disk),
1183 drbd_disk_str(ns.disk));
1184 if (ns.pdsk != os.pdsk)
1185 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
1186 drbd_disk_str(os.pdsk),
1187 drbd_disk_str(ns.pdsk));
1188 if (is_susp(ns) != is_susp(os))
1189 pbp += sprintf(pbp, "susp( %d -> %d ) ",
1190 is_susp(os),
1191 is_susp(ns));
1192 if (ns.aftr_isp != os.aftr_isp)
1193 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
1194 os.aftr_isp,
1195 ns.aftr_isp);
1196 if (ns.peer_isp != os.peer_isp)
1197 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
1198 os.peer_isp,
1199 ns.peer_isp);
1200 if (ns.user_isp != os.user_isp)
1201 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
1202 os.user_isp,
1203 ns.user_isp);
1204 dev_info(DEV, "%s\n", pb);
1205 }
1206
1207 /* solve the race between becoming unconfigured,
1208 * worker doing the cleanup, and
1209 * admin reconfiguring us:
1210 * on (re)configure, first set CONFIG_PENDING,
1211 * then wait for a potentially exiting worker,
1212 * start the worker, and schedule one no_op.
1213 * then proceed with configuration.
1214 */
1215 if (ns.disk == D_DISKLESS &&
1216 ns.conn == C_STANDALONE &&
1217 ns.role == R_SECONDARY &&
1218 !drbd_test_and_set_flag(mdev, CONFIG_PENDING))
1219 drbd_set_flag(mdev, DEVICE_DYING);
1220
1221 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1222 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1223 * drbd_ldev_destroy() won't happen before our corresponding
1224 * after_state_ch works run, where we put_ldev again. */
1225 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1226 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
1227 atomic_inc(&mdev->local_cnt);
1228
1229 mdev->state = ns;
1230
1231 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
1232 drbd_print_uuids(mdev, "attached to UUIDs");
1233
1234 wake_up(&mdev->misc_wait);
1235 wake_up(&mdev->state_wait);
1236
1237 /* Aborted verify run, or we reached the stop sector.
1238 * Log the last position, unless end-of-device. */
1239 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
1240 ns.conn <= C_CONNECTED) {
1241 mdev->ov_start_sector =
1242 BM_BIT_TO_SECT(drbd_bm_bits(mdev) - mdev->ov_left);
1243 if (mdev->ov_left)
1244 dev_info(DEV, "Online Verify reached sector %llu\n",
1245 (unsigned long long)mdev->ov_start_sector);
1246 }
1247
1248 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1249 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
1250 dev_info(DEV, "Syncer continues.\n");
1251 mdev->rs_paused += (long)jiffies
1252 -(long)mdev->rs_mark_time[mdev->rs_last_mark];
1253 if (ns.conn == C_SYNC_TARGET)
1254 mod_timer(&mdev->resync_timer, jiffies);
1255 }
1256
1257 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1258 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
1259 dev_info(DEV, "Resync suspended\n");
1260 mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
1261 }
1262
1263 if (os.conn == C_CONNECTED &&
1264 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1265 unsigned long now = jiffies;
1266 int i;
1267
1268 set_ov_position(mdev, ns.conn);
1269 mdev->rs_start = now;
1270 mdev->rs_last_events = 0;
1271 mdev->rs_last_sect_ev = 0;
1272 mdev->ov_last_oos_size = 0;
1273 mdev->ov_last_oos_start = 0;
1274
1275 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1276 mdev->rs_mark_left[i] = mdev->ov_left;
1277 mdev->rs_mark_time[i] = now;
1278 }
1279
1280 drbd_rs_controller_reset(mdev);
1281
1282 if (ns.conn == C_VERIFY_S) {
1283 dev_info(DEV, "Starting Online Verify from sector %llu\n",
1284 (unsigned long long)mdev->ov_position);
1285 mod_timer(&mdev->resync_timer, jiffies);
1286 }
1287 }
1288
1289 if (get_ldev(mdev)) {
1290 u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
1291 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1292 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1293
1294 if (drbd_test_flag(mdev, CRASHED_PRIMARY))
1295 mdf |= MDF_CRASHED_PRIMARY;
1296 if (mdev->state.role == R_PRIMARY ||
1297 (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
1298 mdf |= MDF_PRIMARY_IND;
1299 if (mdev->state.conn > C_WF_REPORT_PARAMS)
1300 mdf |= MDF_CONNECTED_IND;
1301 if (mdev->state.disk > D_INCONSISTENT)
1302 mdf |= MDF_CONSISTENT;
1303 if (mdev->state.disk > D_OUTDATED)
1304 mdf |= MDF_WAS_UP_TO_DATE;
1305 if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
1306 mdf |= MDF_PEER_OUT_DATED;
1307 if (mdf != mdev->ldev->md.flags) {
1308 mdev->ldev->md.flags = mdf;
1309 drbd_md_mark_dirty(mdev);
1310 }
1311 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
1312 drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
1313 put_ldev(mdev);
1314 }
1315
1316 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1317 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1318 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
1319 drbd_set_flag(mdev, CONSIDER_RESYNC);
1320
1321 /* Receiver should clean up itself */
1322 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
1323 drbd_thread_stop_nowait(&mdev->receiver);
1324
1325 /* Now the receiver finished cleaning up itself, it should die */
1326 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
1327 drbd_thread_stop_nowait(&mdev->receiver);
1328
1329 /* Upon network failure, we need to restart the receiver. */
1330 if (os.conn > C_WF_CONNECTION &&
1331 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
1332 drbd_thread_restart_nowait(&mdev->receiver);
1333
1334 /* Resume AL writing if we get a connection */
1335 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
1336 drbd_resume_al(mdev);
1337
1338 /* remember last connect and attach times so request_timer_fn() won't
1339 * kill newly established sessions while we are still trying to thaw
1340 * previously frozen IO */
1341 if (os.conn != C_WF_REPORT_PARAMS && ns.conn == C_WF_REPORT_PARAMS)
1342 mdev->last_reconnect_jif = jiffies;
1343 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1344 ns.disk > D_NEGOTIATING)
1345 mdev->last_reattach_jif = jiffies;
1346
1347 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1348 if (ascw) {
1349 ascw->os = os;
1350 ascw->ns = ns;
1351 ascw->flags = flags;
1352 ascw->w.cb = w_after_state_ch;
1353 ascw->done = done;
1354 drbd_queue_work(&mdev->data.work, &ascw->w);
1355 } else {
1356 dev_warn(DEV, "Could not kmalloc an ascw\n");
1357 }
1358
1359 return rv;
1360}
1361
1362static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1363{
1364 struct after_state_chg_work *ascw =
1365 container_of(w, struct after_state_chg_work, w);
1366 after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
1367 if (ascw->flags & CS_WAIT_COMPLETE) {
1368 D_ASSERT(ascw->done != NULL);
1369 complete(ascw->done);
1370 }
1371 kfree(ascw);
1372
1373 return 1;
1374}
1375
1376static void abw_start_sync(struct drbd_conf *mdev, int rv)
1377{
1378 if (rv) {
1379 dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
1380 _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
1381 return;
1382 }
1383
1384 switch (mdev->state.conn) {
1385 case C_STARTING_SYNC_T:
1386 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
1387 break;
1388 case C_STARTING_SYNC_S:
1389 drbd_start_resync(mdev, C_SYNC_SOURCE);
1390 break;
1391 }
1392}
1393
1394int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
1395 int (*io_fn)(struct drbd_conf *),
1396 char *why, enum bm_flag flags)
1397{
1398 int rv;
1399
1400 D_ASSERT(current == mdev->worker.task);
1401
1402 /* open coded non-blocking drbd_suspend_io(mdev); */
1403 drbd_set_flag(mdev, SUSPEND_IO);
1404
1405 drbd_bm_lock(mdev, why, flags);
1406 rv = io_fn(mdev);
1407 drbd_bm_unlock(mdev);
1408
1409 drbd_resume_io(mdev);
1410
1411 return rv;
1412}
1413
1414/**
1415 * after_state_ch() - Perform after state change actions that may sleep
1416 * @mdev: DRBD device.
1417 * @os: old state.
1418 * @ns: new state.
1419 * @flags: Flags
1420 */
1421static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
1422 union drbd_state ns, enum chg_state_flags flags)
1423{
1424 enum drbd_fencing_p fp;
1425 enum drbd_req_event what = nothing;
1426 union drbd_state nsm = (union drbd_state){ .i = -1 };
1427
1428 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
1429 drbd_clear_flag(mdev, CRASHED_PRIMARY);
1430 if (mdev->p_uuid)
1431 mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
1432 }
1433
1434 fp = FP_DONT_CARE;
1435 if (get_ldev(mdev)) {
1436 fp = mdev->ldev->dc.fencing;
1437 put_ldev(mdev);
1438 }
1439
1440 /* Inform userspace about the change... */
1441 drbd_bcast_state(mdev, ns);
1442
1443 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1444 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
1445 drbd_khelper(mdev, "pri-on-incon-degr");
1446
1447 /* Here we have the actions that are performed after a
1448 state change. This function might sleep */
1449
1450 if (os.disk <= D_NEGOTIATING && ns.disk > D_NEGOTIATING)
1451 mod_timer(&mdev->request_timer, jiffies + HZ);
1452
1453 nsm.i = -1;
1454 if (ns.susp_nod) {
1455 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
1456 what = resend;
1457
1458 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1459 ns.disk > D_NEGOTIATING)
1460 what = restart_frozen_disk_io;
1461
1462 if (what != nothing)
1463 nsm.susp_nod = 0;
1464 }
1465
1466 if (ns.susp_fen) {
1467 /* case1: The outdate peer handler is successful: */
1468 if (os.pdsk > D_OUTDATED && ns.pdsk <= D_OUTDATED) {
1469 if (drbd_test_flag(mdev, NEW_CUR_UUID)) {
1470 drbd_uuid_new_current(mdev);
1471 drbd_clear_flag(mdev, NEW_CUR_UUID);
1472 }
1473 spin_lock_irq(&mdev->req_lock);
1474 _tl_clear(mdev);
1475 _drbd_set_state(_NS(mdev, susp_fen, 0), CS_VERBOSE, NULL);
1476 spin_unlock_irq(&mdev->req_lock);
1477 }
1478 /* case2: The connection was established again: */
1479 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
1480 drbd_clear_flag(mdev, NEW_CUR_UUID);
1481 what = resend;
1482 nsm.susp_fen = 0;
1483 }
1484 }
1485
1486 if (what != nothing) {
1487 spin_lock_irq(&mdev->req_lock);
1488 _tl_restart(mdev, what);
1489 nsm.i &= mdev->state.i;
1490 _drbd_set_state(mdev, nsm, CS_VERBOSE, NULL);
1491 spin_unlock_irq(&mdev->req_lock);
1492 }
1493
1494 /* Became sync source. With protocol >= 96, we still need to send out
1495 * the sync uuid now. Need to do that before any drbd_send_state, or
1496 * the other side may go "paused sync" before receiving the sync uuids,
1497 * which is unexpected. */
1498 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1499 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
1500 mdev->agreed_pro_version >= 96 && get_ldev(mdev)) {
1501 drbd_gen_and_send_sync_uuid(mdev);
1502 put_ldev(mdev);
1503 }
1504
1505 /* Do not change the order of the if above and the two below... */
1506 if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) { /* attach on the peer */
1507 /* we probably will start a resync soon.
1508 * make sure those things are properly reset. */
1509 mdev->rs_total = 0;
1510 mdev->rs_failed = 0;
1511 atomic_set(&mdev->rs_pending_cnt, 0);
1512 drbd_rs_cancel_all(mdev);
1513
1514 drbd_send_uuids(mdev);
1515 drbd_send_state(mdev, ns);
1516 }
1517 /* No point in queuing send_bitmap if we don't have a connection
1518 * anymore, so check also the _current_ state, not only the new state
1519 * at the time this work was queued. */
1520 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
1521 mdev->state.conn == C_WF_BITMAP_S)
1522 drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL,
1523 "send_bitmap (WFBitMapS)",
1524 BM_LOCKED_TEST_ALLOWED);
1525
1526 /* Lost contact to peer's copy of the data */
1527 if ((os.pdsk >= D_INCONSISTENT &&
1528 os.pdsk != D_UNKNOWN &&
1529 os.pdsk != D_OUTDATED)
1530 && (ns.pdsk < D_INCONSISTENT ||
1531 ns.pdsk == D_UNKNOWN ||
1532 ns.pdsk == D_OUTDATED)) {
1533 if (get_ldev(mdev)) {
1534 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
1535 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1536 if (is_susp(mdev->state)) {
1537 drbd_set_flag(mdev, NEW_CUR_UUID);
1538 } else {
1539 drbd_uuid_new_current(mdev);
1540 drbd_send_uuids(mdev);
1541 }
1542 }
1543 put_ldev(mdev);
1544 }
1545 }
1546
1547 if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
1548 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
1549 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1550 drbd_uuid_new_current(mdev);
1551 drbd_send_uuids(mdev);
1552 }
1553 /* D_DISKLESS Peer becomes secondary */
1554 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1555 /* We may still be Primary ourselves.
1556 * No harm done if the bitmap still changes,
1557 * redirtied pages will follow later. */
1558 drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
1559 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
1560 put_ldev(mdev);
1561 }
1562
1563 /* Write out all changed bits on demote.
1564 * Though, no need to da that just yet
1565 * if there is a resync going on still */
1566 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
1567 mdev->state.conn <= C_CONNECTED && get_ldev(mdev)) {
1568 /* No changes to the bitmap expected this time, so assert that,
1569 * even though no harm was done if it did change. */
1570 drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
1571 "demote", BM_LOCKED_TEST_ALLOWED);
1572 put_ldev(mdev);
1573 }
1574
1575 /* Last part of the attaching process ... */
1576 if (ns.conn >= C_CONNECTED &&
1577 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
1578 drbd_send_sizes(mdev, 0, 0); /* to start sync... */
1579 drbd_send_uuids(mdev);
1580 drbd_send_state(mdev, ns);
1581 }
1582
1583 /* We want to pause/continue resync, tell peer. */
1584 if (ns.conn >= C_CONNECTED &&
1585 ((os.aftr_isp != ns.aftr_isp) ||
1586 (os.user_isp != ns.user_isp)))
1587 drbd_send_state(mdev, ns);
1588
1589 /* In case one of the isp bits got set, suspend other devices. */
1590 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1591 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
1592 suspend_other_sg(mdev);
1593
1594 /* Make sure the peer gets informed about eventual state
1595 changes (ISP bits) while we were in WFReportParams. */
1596 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
1597 drbd_send_state(mdev, ns);
1598
1599 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
1600 drbd_send_state(mdev, ns);
1601
1602 /* We are in the progress to start a full sync... */
1603 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1604 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1605 /* no other bitmap changes expected during this phase */
1606 drbd_queue_bitmap_io(mdev,
1607 &drbd_bmio_set_n_write, &abw_start_sync,
1608 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1609
1610 /* We are invalidating our self... */
1611 if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
1612 os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
1613 /* other bitmap operation expected during this phase */
1614 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL,
1615 "set_n_write from invalidate", BM_LOCKED_MASK);
1616
1617 /* first half of local IO error, failure to attach,
1618 * or administrative detach */
1619 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
1620 /* corresponding get_ldev was in __drbd_set_state, to serialize
1621 * our cleanup here with the transition to D_DISKLESS.
1622 * But it is still not safe to dreference ldev here, we may end
1623 * up here from a failed attach, before ldev was even set. */
1624 if (mdev->ldev) {
1625 enum drbd_io_error_p eh = mdev->ldev->dc.on_io_error;
1626
1627 /* In some setups, this handler triggers a suicide,
1628 * basically mapping IO error to node failure, to
1629 * reduce the number of different failure scenarios.
1630 *
1631 * This handler intentionally runs before we abort IO,
1632 * notify the peer, or try to update our meta data. */
1633 if (eh == EP_CALL_HELPER && drbd_test_flag(mdev, WAS_IO_ERROR))
1634 drbd_khelper(mdev, "local-io-error");
1635
1636 /* Immediately allow completion of all application IO,
1637 * that waits for completion from the local disk,
1638 * if this was a force-detach due to disk_timeout
1639 * or administrator request (drbdsetup detach --force).
1640 * Do NOT abort otherwise.
1641 * Aborting local requests may cause serious problems,
1642 * if requests are completed to upper layers already,
1643 * and then later the already submitted local bio completes.
1644 * This can cause DMA into former bio pages that meanwhile
1645 * have been re-used for other things.
1646 * So aborting local requests may cause crashes,
1647 * or even worse, silent data corruption.
1648 */
1649 if (drbd_test_flag(mdev, FORCE_DETACH))
1650 tl_abort_disk_io(mdev);
1651
1652 /* current state still has to be D_FAILED,
1653 * there is only one way out: to D_DISKLESS,
1654 * and that may only happen after our put_ldev below. */
1655 if (mdev->state.disk != D_FAILED)
1656 dev_err(DEV,
1657 "ASSERT FAILED: disk is %s during detach\n",
1658 drbd_disk_str(mdev->state.disk));
1659
1660 if (ns.conn >= C_CONNECTED)
1661 drbd_send_state(mdev, ns);
1662
1663 drbd_rs_cancel_all(mdev);
1664
1665 /* In case we want to get something to stable storage still,
1666 * this may be the last chance.
1667 * Following put_ldev may transition to D_DISKLESS. */
1668 drbd_md_sync(mdev);
1669 }
1670 put_ldev(mdev);
1671 }
1672
1673 /* second half of local IO error, failure to attach,
1674 * or administrative detach,
1675 * after local_cnt references have reached zero again */
1676 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1677 /* We must still be diskless,
1678 * re-attach has to be serialized with this! */
1679 if (mdev->state.disk != D_DISKLESS)
1680 dev_err(DEV,
1681 "ASSERT FAILED: disk is %s while going diskless\n",
1682 drbd_disk_str(mdev->state.disk));
1683
1684 if (ns.conn >= C_CONNECTED)
1685 drbd_send_state(mdev, ns);
1686
1687 /* corresponding get_ldev in __drbd_set_state
1688 * this may finally trigger drbd_ldev_destroy. */
1689 put_ldev(mdev);
1690 }
1691
1692 /* Notify peer that I had a local IO error, and did not detached.. */
1693 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
1694 drbd_send_state(mdev, ns);
1695
1696 /* Disks got bigger while they were detached */
1697 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
1698 drbd_test_and_clear_flag(mdev, RESYNC_AFTER_NEG)) {
1699 if (ns.conn == C_CONNECTED)
1700 resync_after_online_grow(mdev);
1701 }
1702
1703 /* A resync finished or aborted, wake paused devices... */
1704 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1705 (os.peer_isp && !ns.peer_isp) ||
1706 (os.user_isp && !ns.user_isp))
1707 resume_next_sg(mdev);
1708
1709 /* sync target done with resync. Explicitly notify peer, even though
1710 * it should (at least for non-empty resyncs) already know itself. */
1711 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
1712 drbd_send_state(mdev, ns);
1713
1714 /* Verify finished, or reached stop sector. Peer did not know about
1715 * the stop sector, and we may even have changed the stop sector during
1716 * verify to interrupt/stop early. Send the new state. */
1717 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
1718 && mdev->agreed_pro_version >= 97)
1719 drbd_send_state(mdev, ns);
1720
1721 /* Wake up role changes, that were delayed because of connection establishing */
1722 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS) {
1723 drbd_clear_flag(mdev, STATE_SENT);
1724 wake_up(&mdev->state_wait);
1725 }
1726
1727 /* This triggers bitmap writeout of potentially still unwritten pages
1728 * if the resync finished cleanly, or aborted because of peer disk
1729 * failure, or because of connection loss.
1730 * For resync aborted because of local disk failure, we cannot do
1731 * any bitmap writeout anymore.
1732 * No harm done if some bits change during this phase.
1733 */
1734 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(mdev)) {
1735 drbd_queue_bitmap_io(mdev, &drbd_bm_write_copy_pages, NULL,
1736 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
1737 put_ldev(mdev);
1738 }
1739
1740 /* free tl_hash if we Got thawed and are C_STANDALONE */
1741 if (ns.conn == C_STANDALONE && !is_susp(ns) && mdev->tl_hash)
1742 drbd_free_tl_hash(mdev);
1743
1744 /* Upon network connection, we need to start the receiver */
1745 if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
1746 drbd_thread_start(&mdev->receiver);
1747
1748 /* Terminate worker thread if we are unconfigured - it will be
1749 restarted as needed... */
1750 if (ns.disk == D_DISKLESS &&
1751 ns.conn == C_STANDALONE &&
1752 ns.role == R_SECONDARY) {
1753 if (os.aftr_isp != ns.aftr_isp)
1754 resume_next_sg(mdev);
1755 /* set in __drbd_set_state, unless CONFIG_PENDING was set */
1756 if (drbd_test_flag(mdev, DEVICE_DYING))
1757 drbd_thread_stop_nowait(&mdev->worker);
1758 } 322 }
1759 323 spin_unlock_irq(&tconn->req_lock);
1760 drbd_md_sync(mdev);
1761} 324}
1762 325
1763
1764static int drbd_thread_setup(void *arg) 326static int drbd_thread_setup(void *arg)
1765{ 327{
1766 struct drbd_thread *thi = (struct drbd_thread *) arg; 328 struct drbd_thread *thi = (struct drbd_thread *) arg;
1767 struct drbd_conf *mdev = thi->mdev; 329 struct drbd_tconn *tconn = thi->tconn;
1768 unsigned long flags; 330 unsigned long flags;
1769 int retval; 331 int retval;
1770 332
333 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
334 thi->name[0], thi->tconn->name);
335
1771restart: 336restart:
1772 retval = thi->function(thi); 337 retval = thi->function(thi);
1773 338
1774 spin_lock_irqsave(&thi->t_lock, flags); 339 spin_lock_irqsave(&thi->t_lock, flags);
1775 340
1776 /* if the receiver has been "Exiting", the last thing it did 341 /* if the receiver has been "EXITING", the last thing it did
1777 * was set the conn state to "StandAlone", 342 * was set the conn state to "StandAlone",
1778 * if now a re-connect request comes in, conn state goes C_UNCONNECTED, 343 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
1779 * and receiver thread will be "started". 344 * and receiver thread will be "started".
1780 * drbd_thread_start needs to set "Restarting" in that case. 345 * drbd_thread_start needs to set "RESTARTING" in that case.
1781 * t_state check and assignment needs to be within the same spinlock, 346 * t_state check and assignment needs to be within the same spinlock,
1782 * so either thread_start sees Exiting, and can remap to Restarting, 347 * so either thread_start sees EXITING, and can remap to RESTARTING,
1783 * or thread_start see None, and can proceed as normal. 348 * or thread_start see NONE, and can proceed as normal.
1784 */ 349 */
1785 350
1786 if (thi->t_state == Restarting) { 351 if (thi->t_state == RESTARTING) {
1787 dev_info(DEV, "Restarting %s\n", current->comm); 352 conn_info(tconn, "Restarting %s thread\n", thi->name);
1788 thi->t_state = Running; 353 thi->t_state = RUNNING;
1789 spin_unlock_irqrestore(&thi->t_lock, flags); 354 spin_unlock_irqrestore(&thi->t_lock, flags);
1790 goto restart; 355 goto restart;
1791 } 356 }
1792 357
1793 thi->task = NULL; 358 thi->task = NULL;
1794 thi->t_state = None; 359 thi->t_state = NONE;
1795 smp_mb(); 360 smp_mb();
1796 complete(&thi->stop); 361 complete_all(&thi->stop);
1797 spin_unlock_irqrestore(&thi->t_lock, flags); 362 spin_unlock_irqrestore(&thi->t_lock, flags);
1798 363
1799 dev_info(DEV, "Terminating %s\n", current->comm); 364 conn_info(tconn, "Terminating %s\n", current->comm);
1800 365
1801 /* Release mod reference taken when thread was started */ 366 /* Release mod reference taken when thread was started */
367
368 kref_put(&tconn->kref, &conn_destroy);
1802 module_put(THIS_MODULE); 369 module_put(THIS_MODULE);
1803 return retval; 370 return retval;
1804} 371}
1805 372
1806static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi, 373static void drbd_thread_init(struct drbd_tconn *tconn, struct drbd_thread *thi,
1807 int (*func) (struct drbd_thread *)) 374 int (*func) (struct drbd_thread *), char *name)
1808{ 375{
1809 spin_lock_init(&thi->t_lock); 376 spin_lock_init(&thi->t_lock);
1810 thi->task = NULL; 377 thi->task = NULL;
1811 thi->t_state = None; 378 thi->t_state = NONE;
1812 thi->function = func; 379 thi->function = func;
1813 thi->mdev = mdev; 380 thi->tconn = tconn;
381 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
1814} 382}
1815 383
1816int drbd_thread_start(struct drbd_thread *thi) 384int drbd_thread_start(struct drbd_thread *thi)
1817{ 385{
1818 struct drbd_conf *mdev = thi->mdev; 386 struct drbd_tconn *tconn = thi->tconn;
1819 struct task_struct *nt; 387 struct task_struct *nt;
1820 unsigned long flags; 388 unsigned long flags;
1821 389
1822 const char *me =
1823 thi == &mdev->receiver ? "receiver" :
1824 thi == &mdev->asender ? "asender" :
1825 thi == &mdev->worker ? "worker" : "NONSENSE";
1826
1827 /* is used from state engine doing drbd_thread_stop_nowait, 390 /* is used from state engine doing drbd_thread_stop_nowait,
1828 * while holding the req lock irqsave */ 391 * while holding the req lock irqsave */
1829 spin_lock_irqsave(&thi->t_lock, flags); 392 spin_lock_irqsave(&thi->t_lock, flags);
1830 393
1831 switch (thi->t_state) { 394 switch (thi->t_state) {
1832 case None: 395 case NONE:
1833 dev_info(DEV, "Starting %s thread (from %s [%d])\n", 396 conn_info(tconn, "Starting %s thread (from %s [%d])\n",
1834 me, current->comm, current->pid); 397 thi->name, current->comm, current->pid);
1835 398
1836 /* Get ref on module for thread - this is released when thread exits */ 399 /* Get ref on module for thread - this is released when thread exits */
1837 if (!try_module_get(THIS_MODULE)) { 400 if (!try_module_get(THIS_MODULE)) {
1838 dev_err(DEV, "Failed to get module reference in drbd_thread_start\n"); 401 conn_err(tconn, "Failed to get module reference in drbd_thread_start\n");
1839 spin_unlock_irqrestore(&thi->t_lock, flags); 402 spin_unlock_irqrestore(&thi->t_lock, flags);
1840 return false; 403 return false;
1841 } 404 }
1842 405
406 kref_get(&thi->tconn->kref);
407
1843 init_completion(&thi->stop); 408 init_completion(&thi->stop);
1844 D_ASSERT(thi->task == NULL);
1845 thi->reset_cpu_mask = 1; 409 thi->reset_cpu_mask = 1;
1846 thi->t_state = Running; 410 thi->t_state = RUNNING;
1847 spin_unlock_irqrestore(&thi->t_lock, flags); 411 spin_unlock_irqrestore(&thi->t_lock, flags);
1848 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */ 412 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
1849 413
1850 nt = kthread_create(drbd_thread_setup, (void *) thi, 414 nt = kthread_create(drbd_thread_setup, (void *) thi,
1851 "drbd%d_%s", mdev_to_minor(mdev), me); 415 "drbd_%c_%s", thi->name[0], thi->tconn->name);
1852 416
1853 if (IS_ERR(nt)) { 417 if (IS_ERR(nt)) {
1854 dev_err(DEV, "Couldn't start thread\n"); 418 conn_err(tconn, "Couldn't start thread\n");
1855 419
420 kref_put(&tconn->kref, &conn_destroy);
1856 module_put(THIS_MODULE); 421 module_put(THIS_MODULE);
1857 return false; 422 return false;
1858 } 423 }
1859 spin_lock_irqsave(&thi->t_lock, flags); 424 spin_lock_irqsave(&thi->t_lock, flags);
1860 thi->task = nt; 425 thi->task = nt;
1861 thi->t_state = Running; 426 thi->t_state = RUNNING;
1862 spin_unlock_irqrestore(&thi->t_lock, flags); 427 spin_unlock_irqrestore(&thi->t_lock, flags);
1863 wake_up_process(nt); 428 wake_up_process(nt);
1864 break; 429 break;
1865 case Exiting: 430 case EXITING:
1866 thi->t_state = Restarting; 431 thi->t_state = RESTARTING;
1867 dev_info(DEV, "Restarting %s thread (from %s [%d])\n", 432 conn_info(tconn, "Restarting %s thread (from %s [%d])\n",
1868 me, current->comm, current->pid); 433 thi->name, current->comm, current->pid);
1869 /* fall through */ 434 /* fall through */
1870 case Running: 435 case RUNNING:
1871 case Restarting: 436 case RESTARTING:
1872 default: 437 default:
1873 spin_unlock_irqrestore(&thi->t_lock, flags); 438 spin_unlock_irqrestore(&thi->t_lock, flags);
1874 break; 439 break;
@@ -1882,12 +447,12 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
1882{ 447{
1883 unsigned long flags; 448 unsigned long flags;
1884 449
1885 enum drbd_thread_state ns = restart ? Restarting : Exiting; 450 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
1886 451
1887 /* may be called from state engine, holding the req lock irqsave */ 452 /* may be called from state engine, holding the req lock irqsave */
1888 spin_lock_irqsave(&thi->t_lock, flags); 453 spin_lock_irqsave(&thi->t_lock, flags);
1889 454
1890 if (thi->t_state == None) { 455 if (thi->t_state == NONE) {
1891 spin_unlock_irqrestore(&thi->t_lock, flags); 456 spin_unlock_irqrestore(&thi->t_lock, flags);
1892 if (restart) 457 if (restart)
1893 drbd_thread_start(thi); 458 drbd_thread_start(thi);
@@ -1905,7 +470,6 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
1905 init_completion(&thi->stop); 470 init_completion(&thi->stop);
1906 if (thi->task != current) 471 if (thi->task != current)
1907 force_sig(DRBD_SIGKILL, thi->task); 472 force_sig(DRBD_SIGKILL, thi->task);
1908
1909 } 473 }
1910 474
1911 spin_unlock_irqrestore(&thi->t_lock, flags); 475 spin_unlock_irqrestore(&thi->t_lock, flags);
@@ -1914,6 +478,35 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
1914 wait_for_completion(&thi->stop); 478 wait_for_completion(&thi->stop);
1915} 479}
1916 480
481static struct drbd_thread *drbd_task_to_thread(struct drbd_tconn *tconn, struct task_struct *task)
482{
483 struct drbd_thread *thi =
484 task == tconn->receiver.task ? &tconn->receiver :
485 task == tconn->asender.task ? &tconn->asender :
486 task == tconn->worker.task ? &tconn->worker : NULL;
487
488 return thi;
489}
490
491char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task)
492{
493 struct drbd_thread *thi = drbd_task_to_thread(tconn, task);
494 return thi ? thi->name : task->comm;
495}
496
497int conn_lowest_minor(struct drbd_tconn *tconn)
498{
499 struct drbd_conf *mdev;
500 int vnr = 0, m;
501
502 rcu_read_lock();
503 mdev = idr_get_next(&tconn->volumes, &vnr);
504 m = mdev ? mdev_to_minor(mdev) : -1;
505 rcu_read_unlock();
506
507 return m;
508}
509
1917#ifdef CONFIG_SMP 510#ifdef CONFIG_SMP
1918/** 511/**
1919 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs 512 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
@@ -1922,240 +515,345 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
1922 * Forces all threads of a device onto the same CPU. This is beneficial for 515 * Forces all threads of a device onto the same CPU. This is beneficial for
1923 * DRBD's performance. May be overwritten by user's configuration. 516 * DRBD's performance. May be overwritten by user's configuration.
1924 */ 517 */
1925void drbd_calc_cpu_mask(struct drbd_conf *mdev) 518void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
1926{ 519{
1927 int ord, cpu; 520 int ord, cpu;
1928 521
1929 /* user override. */ 522 /* user override. */
1930 if (cpumask_weight(mdev->cpu_mask)) 523 if (cpumask_weight(tconn->cpu_mask))
1931 return; 524 return;
1932 525
1933 ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask); 526 ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
1934 for_each_online_cpu(cpu) { 527 for_each_online_cpu(cpu) {
1935 if (ord-- == 0) { 528 if (ord-- == 0) {
1936 cpumask_set_cpu(cpu, mdev->cpu_mask); 529 cpumask_set_cpu(cpu, tconn->cpu_mask);
1937 return; 530 return;
1938 } 531 }
1939 } 532 }
1940 /* should not be reached */ 533 /* should not be reached */
1941 cpumask_setall(mdev->cpu_mask); 534 cpumask_setall(tconn->cpu_mask);
1942} 535}
1943 536
1944/** 537/**
1945 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread 538 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
1946 * @mdev: DRBD device. 539 * @mdev: DRBD device.
540 * @thi: drbd_thread object
1947 * 541 *
1948 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die 542 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
1949 * prematurely. 543 * prematurely.
1950 */ 544 */
1951void drbd_thread_current_set_cpu(struct drbd_conf *mdev) 545void drbd_thread_current_set_cpu(struct drbd_thread *thi)
1952{ 546{
1953 struct task_struct *p = current; 547 struct task_struct *p = current;
1954 struct drbd_thread *thi = 548
1955 p == mdev->asender.task ? &mdev->asender :
1956 p == mdev->receiver.task ? &mdev->receiver :
1957 p == mdev->worker.task ? &mdev->worker :
1958 NULL;
1959 ERR_IF(thi == NULL)
1960 return;
1961 if (!thi->reset_cpu_mask) 549 if (!thi->reset_cpu_mask)
1962 return; 550 return;
1963 thi->reset_cpu_mask = 0; 551 thi->reset_cpu_mask = 0;
1964 set_cpus_allowed_ptr(p, mdev->cpu_mask); 552 set_cpus_allowed_ptr(p, thi->tconn->cpu_mask);
1965} 553}
1966#endif 554#endif
1967 555
1968/* the appropriate socket mutex must be held already */ 556/**
1969int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock, 557 * drbd_header_size - size of a packet header
1970 enum drbd_packets cmd, struct p_header80 *h, 558 *
1971 size_t size, unsigned msg_flags) 559 * The header size is a multiple of 8, so any payload following the header is
560 * word aligned on 64-bit architectures. (The bitmap send and receive code
561 * relies on this.)
562 */
563unsigned int drbd_header_size(struct drbd_tconn *tconn)
1972{ 564{
1973 int sent, ok; 565 if (tconn->agreed_pro_version >= 100) {
566 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
567 return sizeof(struct p_header100);
568 } else {
569 BUILD_BUG_ON(sizeof(struct p_header80) !=
570 sizeof(struct p_header95));
571 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
572 return sizeof(struct p_header80);
573 }
574}
1974 575
1975 ERR_IF(!h) return false; 576static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
1976 ERR_IF(!size) return false; 577{
578 h->magic = cpu_to_be32(DRBD_MAGIC);
579 h->command = cpu_to_be16(cmd);
580 h->length = cpu_to_be16(size);
581 return sizeof(struct p_header80);
582}
1977 583
1978 h->magic = BE_DRBD_MAGIC; 584static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
585{
586 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
1979 h->command = cpu_to_be16(cmd); 587 h->command = cpu_to_be16(cmd);
1980 h->length = cpu_to_be16(size-sizeof(struct p_header80)); 588 h->length = cpu_to_be32(size);
589 return sizeof(struct p_header95);
590}
1981 591
1982 sent = drbd_send(mdev, sock, h, size, msg_flags); 592static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
593 int size, int vnr)
594{
595 h->magic = cpu_to_be32(DRBD_MAGIC_100);
596 h->volume = cpu_to_be16(vnr);
597 h->command = cpu_to_be16(cmd);
598 h->length = cpu_to_be32(size);
599 h->pad = 0;
600 return sizeof(struct p_header100);
601}
1983 602
1984 ok = (sent == size); 603static unsigned int prepare_header(struct drbd_tconn *tconn, int vnr,
1985 if (!ok && !signal_pending(current)) 604 void *buffer, enum drbd_packet cmd, int size)
1986 dev_warn(DEV, "short sent %s size=%d sent=%d\n", 605{
1987 cmdname(cmd), (int)size, sent); 606 if (tconn->agreed_pro_version >= 100)
1988 return ok; 607 return prepare_header100(buffer, cmd, size, vnr);
608 else if (tconn->agreed_pro_version >= 95 &&
609 size > DRBD_MAX_SIZE_H80_PACKET)
610 return prepare_header95(buffer, cmd, size);
611 else
612 return prepare_header80(buffer, cmd, size);
1989} 613}
1990 614
1991/* don't pass the socket. we may only look at it 615static void *__conn_prepare_command(struct drbd_tconn *tconn,
1992 * when we hold the appropriate socket mutex. 616 struct drbd_socket *sock)
1993 */
1994int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
1995 enum drbd_packets cmd, struct p_header80 *h, size_t size)
1996{ 617{
1997 int ok = 0; 618 if (!sock->socket)
1998 struct socket *sock; 619 return NULL;
620 return sock->sbuf + drbd_header_size(tconn);
621}
1999 622
2000 if (use_data_socket) { 623void *conn_prepare_command(struct drbd_tconn *tconn, struct drbd_socket *sock)
2001 mutex_lock(&mdev->data.mutex); 624{
2002 sock = mdev->data.socket; 625 void *p;
2003 } else {
2004 mutex_lock(&mdev->meta.mutex);
2005 sock = mdev->meta.socket;
2006 }
2007 626
2008 /* drbd_disconnect() could have called drbd_free_sock() 627 mutex_lock(&sock->mutex);
2009 * while we were waiting in down()... */ 628 p = __conn_prepare_command(tconn, sock);
2010 if (likely(sock != NULL)) 629 if (!p)
2011 ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0); 630 mutex_unlock(&sock->mutex);
2012 631
2013 if (use_data_socket) 632 return p;
2014 mutex_unlock(&mdev->data.mutex);
2015 else
2016 mutex_unlock(&mdev->meta.mutex);
2017 return ok;
2018} 633}
2019 634
2020int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd, char *data, 635void *drbd_prepare_command(struct drbd_conf *mdev, struct drbd_socket *sock)
2021 size_t size)
2022{ 636{
2023 struct p_header80 h; 637 return conn_prepare_command(mdev->tconn, sock);
2024 int ok; 638}
2025 639
2026 h.magic = BE_DRBD_MAGIC; 640static int __send_command(struct drbd_tconn *tconn, int vnr,
2027 h.command = cpu_to_be16(cmd); 641 struct drbd_socket *sock, enum drbd_packet cmd,
2028 h.length = cpu_to_be16(size); 642 unsigned int header_size, void *data,
643 unsigned int size)
644{
645 int msg_flags;
646 int err;
2029 647
2030 if (!drbd_get_data_sock(mdev)) 648 /*
2031 return 0; 649 * Called with @data == NULL and the size of the data blocks in @size
650 * for commands that send data blocks. For those commands, omit the
651 * MSG_MORE flag: this will increase the likelihood that data blocks
652 * which are page aligned on the sender will end up page aligned on the
653 * receiver.
654 */
655 msg_flags = data ? MSG_MORE : 0;
656
657 header_size += prepare_header(tconn, vnr, sock->sbuf, cmd,
658 header_size + size);
659 err = drbd_send_all(tconn, sock->socket, sock->sbuf, header_size,
660 msg_flags);
661 if (data && !err)
662 err = drbd_send_all(tconn, sock->socket, data, size, 0);
663 return err;
664}
2032 665
2033 ok = (sizeof(h) == 666static int __conn_send_command(struct drbd_tconn *tconn, struct drbd_socket *sock,
2034 drbd_send(mdev, mdev->data.socket, &h, sizeof(h), 0)); 667 enum drbd_packet cmd, unsigned int header_size,
2035 ok = ok && (size == 668 void *data, unsigned int size)
2036 drbd_send(mdev, mdev->data.socket, data, size, 0)); 669{
670 return __send_command(tconn, 0, sock, cmd, header_size, data, size);
671}
2037 672
2038 drbd_put_data_sock(mdev); 673int conn_send_command(struct drbd_tconn *tconn, struct drbd_socket *sock,
674 enum drbd_packet cmd, unsigned int header_size,
675 void *data, unsigned int size)
676{
677 int err;
2039 678
2040 return ok; 679 err = __conn_send_command(tconn, sock, cmd, header_size, data, size);
680 mutex_unlock(&sock->mutex);
681 return err;
2041} 682}
2042 683
2043int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc) 684int drbd_send_command(struct drbd_conf *mdev, struct drbd_socket *sock,
685 enum drbd_packet cmd, unsigned int header_size,
686 void *data, unsigned int size)
2044{ 687{
688 int err;
689
690 err = __send_command(mdev->tconn, mdev->vnr, sock, cmd, header_size,
691 data, size);
692 mutex_unlock(&sock->mutex);
693 return err;
694}
695
696int drbd_send_ping(struct drbd_tconn *tconn)
697{
698 struct drbd_socket *sock;
699
700 sock = &tconn->meta;
701 if (!conn_prepare_command(tconn, sock))
702 return -EIO;
703 return conn_send_command(tconn, sock, P_PING, 0, NULL, 0);
704}
705
706int drbd_send_ping_ack(struct drbd_tconn *tconn)
707{
708 struct drbd_socket *sock;
709
710 sock = &tconn->meta;
711 if (!conn_prepare_command(tconn, sock))
712 return -EIO;
713 return conn_send_command(tconn, sock, P_PING_ACK, 0, NULL, 0);
714}
715
716int drbd_send_sync_param(struct drbd_conf *mdev)
717{
718 struct drbd_socket *sock;
2045 struct p_rs_param_95 *p; 719 struct p_rs_param_95 *p;
2046 struct socket *sock; 720 int size;
2047 int size, rv; 721 const int apv = mdev->tconn->agreed_pro_version;
2048 const int apv = mdev->agreed_pro_version; 722 enum drbd_packet cmd;
723 struct net_conf *nc;
724 struct disk_conf *dc;
725
726 sock = &mdev->tconn->data;
727 p = drbd_prepare_command(mdev, sock);
728 if (!p)
729 return -EIO;
730
731 rcu_read_lock();
732 nc = rcu_dereference(mdev->tconn->net_conf);
2049 733
2050 size = apv <= 87 ? sizeof(struct p_rs_param) 734 size = apv <= 87 ? sizeof(struct p_rs_param)
2051 : apv == 88 ? sizeof(struct p_rs_param) 735 : apv == 88 ? sizeof(struct p_rs_param)
2052 + strlen(mdev->sync_conf.verify_alg) + 1 736 + strlen(nc->verify_alg) + 1
2053 : apv <= 94 ? sizeof(struct p_rs_param_89) 737 : apv <= 94 ? sizeof(struct p_rs_param_89)
2054 : /* apv >= 95 */ sizeof(struct p_rs_param_95); 738 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
2055 739
2056 /* used from admin command context and receiver/worker context. 740 cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
2057 * to avoid kmalloc, grab the socket right here,
2058 * then use the pre-allocated sbuf there */
2059 mutex_lock(&mdev->data.mutex);
2060 sock = mdev->data.socket;
2061
2062 if (likely(sock != NULL)) {
2063 enum drbd_packets cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
2064 741
2065 p = &mdev->data.sbuf.rs_param_95; 742 /* initialize verify_alg and csums_alg */
743 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2066 744
2067 /* initialize verify_alg and csums_alg */ 745 if (get_ldev(mdev)) {
2068 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX); 746 dc = rcu_dereference(mdev->ldev->disk_conf);
2069 747 p->resync_rate = cpu_to_be32(dc->resync_rate);
2070 p->rate = cpu_to_be32(sc->rate); 748 p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
2071 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead); 749 p->c_delay_target = cpu_to_be32(dc->c_delay_target);
2072 p->c_delay_target = cpu_to_be32(sc->c_delay_target); 750 p->c_fill_target = cpu_to_be32(dc->c_fill_target);
2073 p->c_fill_target = cpu_to_be32(sc->c_fill_target); 751 p->c_max_rate = cpu_to_be32(dc->c_max_rate);
2074 p->c_max_rate = cpu_to_be32(sc->c_max_rate); 752 put_ldev(mdev);
2075 753 } else {
2076 if (apv >= 88) 754 p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
2077 strcpy(p->verify_alg, mdev->sync_conf.verify_alg); 755 p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
2078 if (apv >= 89) 756 p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
2079 strcpy(p->csums_alg, mdev->sync_conf.csums_alg); 757 p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
2080 758 p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
2081 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0); 759 }
2082 } else
2083 rv = 0; /* not ok */
2084 760
2085 mutex_unlock(&mdev->data.mutex); 761 if (apv >= 88)
762 strcpy(p->verify_alg, nc->verify_alg);
763 if (apv >= 89)
764 strcpy(p->csums_alg, nc->csums_alg);
765 rcu_read_unlock();
2086 766
2087 return rv; 767 return drbd_send_command(mdev, sock, cmd, size, NULL, 0);
2088} 768}
2089 769
2090int drbd_send_protocol(struct drbd_conf *mdev) 770int __drbd_send_protocol(struct drbd_tconn *tconn, enum drbd_packet cmd)
2091{ 771{
772 struct drbd_socket *sock;
2092 struct p_protocol *p; 773 struct p_protocol *p;
2093 int size, cf, rv; 774 struct net_conf *nc;
775 int size, cf;
2094 776
2095 size = sizeof(struct p_protocol); 777 sock = &tconn->data;
778 p = __conn_prepare_command(tconn, sock);
779 if (!p)
780 return -EIO;
2096 781
2097 if (mdev->agreed_pro_version >= 87) 782 rcu_read_lock();
2098 size += strlen(mdev->net_conf->integrity_alg) + 1; 783 nc = rcu_dereference(tconn->net_conf);
2099 784
2100 /* we must not recurse into our own queue, 785 if (nc->tentative && tconn->agreed_pro_version < 92) {
2101 * as that is blocked during handshake */ 786 rcu_read_unlock();
2102 p = kmalloc(size, GFP_NOIO); 787 mutex_unlock(&sock->mutex);
2103 if (p == NULL) 788 conn_err(tconn, "--dry-run is not supported by peer");
2104 return 0; 789 return -EOPNOTSUPP;
790 }
2105 791
2106 p->protocol = cpu_to_be32(mdev->net_conf->wire_protocol); 792 size = sizeof(*p);
2107 p->after_sb_0p = cpu_to_be32(mdev->net_conf->after_sb_0p); 793 if (tconn->agreed_pro_version >= 87)
2108 p->after_sb_1p = cpu_to_be32(mdev->net_conf->after_sb_1p); 794 size += strlen(nc->integrity_alg) + 1;
2109 p->after_sb_2p = cpu_to_be32(mdev->net_conf->after_sb_2p);
2110 p->two_primaries = cpu_to_be32(mdev->net_conf->two_primaries);
2111 795
796 p->protocol = cpu_to_be32(nc->wire_protocol);
797 p->after_sb_0p = cpu_to_be32(nc->after_sb_0p);
798 p->after_sb_1p = cpu_to_be32(nc->after_sb_1p);
799 p->after_sb_2p = cpu_to_be32(nc->after_sb_2p);
800 p->two_primaries = cpu_to_be32(nc->two_primaries);
2112 cf = 0; 801 cf = 0;
2113 if (mdev->net_conf->want_lose) 802 if (nc->discard_my_data)
2114 cf |= CF_WANT_LOSE; 803 cf |= CF_DISCARD_MY_DATA;
2115 if (mdev->net_conf->dry_run) { 804 if (nc->tentative)
2116 if (mdev->agreed_pro_version >= 92) 805 cf |= CF_DRY_RUN;
2117 cf |= CF_DRY_RUN;
2118 else {
2119 dev_err(DEV, "--dry-run is not supported by peer");
2120 kfree(p);
2121 return -1;
2122 }
2123 }
2124 p->conn_flags = cpu_to_be32(cf); 806 p->conn_flags = cpu_to_be32(cf);
2125 807
2126 if (mdev->agreed_pro_version >= 87) 808 if (tconn->agreed_pro_version >= 87)
2127 strcpy(p->integrity_alg, mdev->net_conf->integrity_alg); 809 strcpy(p->integrity_alg, nc->integrity_alg);
810 rcu_read_unlock();
2128 811
2129 rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL, 812 return __conn_send_command(tconn, sock, cmd, size, NULL, 0);
2130 (struct p_header80 *)p, size); 813}
2131 kfree(p); 814
2132 return rv; 815int drbd_send_protocol(struct drbd_tconn *tconn)
816{
817 int err;
818
819 mutex_lock(&tconn->data.mutex);
820 err = __drbd_send_protocol(tconn, P_PROTOCOL);
821 mutex_unlock(&tconn->data.mutex);
822
823 return err;
2133} 824}
2134 825
2135int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags) 826int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
2136{ 827{
2137 struct p_uuids p; 828 struct drbd_socket *sock;
829 struct p_uuids *p;
2138 int i; 830 int i;
2139 831
2140 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) 832 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
2141 return 1; 833 return 0;
2142 834
835 sock = &mdev->tconn->data;
836 p = drbd_prepare_command(mdev, sock);
837 if (!p) {
838 put_ldev(mdev);
839 return -EIO;
840 }
2143 spin_lock_irq(&mdev->ldev->md.uuid_lock); 841 spin_lock_irq(&mdev->ldev->md.uuid_lock);
2144 for (i = UI_CURRENT; i < UI_SIZE; i++) 842 for (i = UI_CURRENT; i < UI_SIZE; i++)
2145 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0; 843 p->uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
2146 spin_unlock_irq(&mdev->ldev->md.uuid_lock); 844 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
2147 845
2148 mdev->comm_bm_set = drbd_bm_total_weight(mdev); 846 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
2149 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set); 847 p->uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
2150 uuid_flags |= mdev->net_conf->want_lose ? 1 : 0; 848 rcu_read_lock();
2151 uuid_flags |= drbd_test_flag(mdev, CRASHED_PRIMARY) ? 2 : 0; 849 uuid_flags |= rcu_dereference(mdev->tconn->net_conf)->discard_my_data ? 1 : 0;
850 rcu_read_unlock();
851 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
2152 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0; 852 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
2153 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags); 853 p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
2154 854
2155 put_ldev(mdev); 855 put_ldev(mdev);
2156 856 return drbd_send_command(mdev, sock, P_UUIDS, sizeof(*p), NULL, 0);
2157 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS,
2158 (struct p_header80 *)&p, sizeof(p));
2159} 857}
2160 858
2161int drbd_send_uuids(struct drbd_conf *mdev) 859int drbd_send_uuids(struct drbd_conf *mdev)
@@ -2186,9 +884,10 @@ void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
2186 } 884 }
2187} 885}
2188 886
2189int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev) 887void drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
2190{ 888{
2191 struct p_rs_uuid p; 889 struct drbd_socket *sock;
890 struct p_rs_uuid *p;
2192 u64 uuid; 891 u64 uuid;
2193 892
2194 D_ASSERT(mdev->state.disk == D_UP_TO_DATE); 893 D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
@@ -2201,24 +900,29 @@ int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
2201 drbd_uuid_set(mdev, UI_BITMAP, uuid); 900 drbd_uuid_set(mdev, UI_BITMAP, uuid);
2202 drbd_print_uuids(mdev, "updated sync UUID"); 901 drbd_print_uuids(mdev, "updated sync UUID");
2203 drbd_md_sync(mdev); 902 drbd_md_sync(mdev);
2204 p.uuid = cpu_to_be64(uuid);
2205 903
2206 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID, 904 sock = &mdev->tconn->data;
2207 (struct p_header80 *)&p, sizeof(p)); 905 p = drbd_prepare_command(mdev, sock);
906 if (p) {
907 p->uuid = cpu_to_be64(uuid);
908 drbd_send_command(mdev, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
909 }
2208} 910}
2209 911
2210int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags) 912int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
2211{ 913{
2212 struct p_sizes p; 914 struct drbd_socket *sock;
915 struct p_sizes *p;
2213 sector_t d_size, u_size; 916 sector_t d_size, u_size;
2214 int q_order_type; 917 int q_order_type;
2215 unsigned int max_bio_size; 918 unsigned int max_bio_size;
2216 int ok;
2217 919
2218 if (get_ldev_if_state(mdev, D_NEGOTIATING)) { 920 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
2219 D_ASSERT(mdev->ldev->backing_bdev); 921 D_ASSERT(mdev->ldev->backing_bdev);
2220 d_size = drbd_get_max_capacity(mdev->ldev); 922 d_size = drbd_get_max_capacity(mdev->ldev);
2221 u_size = mdev->ldev->dc.disk_size; 923 rcu_read_lock();
924 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
925 rcu_read_unlock();
2222 q_order_type = drbd_queue_order_type(mdev); 926 q_order_type = drbd_queue_order_type(mdev);
2223 max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9; 927 max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
2224 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE); 928 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
@@ -2230,20 +934,23 @@ int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags fl
2230 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */ 934 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
2231 } 935 }
2232 936
2233 /* Never allow old drbd (up to 8.3.7) to see more than 32KiB */ 937 sock = &mdev->tconn->data;
2234 if (mdev->agreed_pro_version <= 94) 938 p = drbd_prepare_command(mdev, sock);
2235 max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET); 939 if (!p)
940 return -EIO;
2236 941
2237 p.d_size = cpu_to_be64(d_size); 942 if (mdev->tconn->agreed_pro_version <= 94)
2238 p.u_size = cpu_to_be64(u_size); 943 max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
2239 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev)); 944 else if (mdev->tconn->agreed_pro_version < 100)
2240 p.max_bio_size = cpu_to_be32(max_bio_size); 945 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
2241 p.queue_order_type = cpu_to_be16(q_order_type);
2242 p.dds_flags = cpu_to_be16(flags);
2243 946
2244 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES, 947 p->d_size = cpu_to_be64(d_size);
2245 (struct p_header80 *)&p, sizeof(p)); 948 p->u_size = cpu_to_be64(u_size);
2246 return ok; 949 p->c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
950 p->max_bio_size = cpu_to_be32(max_bio_size);
951 p->queue_order_type = cpu_to_be16(q_order_type);
952 p->dds_flags = cpu_to_be16(flags);
953 return drbd_send_command(mdev, sock, P_SIZES, sizeof(*p), NULL, 0);
2247} 954}
2248 955
2249/** 956/**
@@ -2252,34 +959,21 @@ int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags fl
2252 */ 959 */
2253int drbd_send_current_state(struct drbd_conf *mdev) 960int drbd_send_current_state(struct drbd_conf *mdev)
2254{ 961{
2255 struct socket *sock; 962 struct drbd_socket *sock;
2256 struct p_state p; 963 struct p_state *p;
2257 int ok = 0;
2258
2259 /* Grab state lock so we wont send state if we're in the middle
2260 * of a cluster wide state change on another thread */
2261 drbd_state_lock(mdev);
2262
2263 mutex_lock(&mdev->data.mutex);
2264
2265 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
2266 sock = mdev->data.socket;
2267
2268 if (likely(sock != NULL)) {
2269 ok = _drbd_send_cmd(mdev, sock, P_STATE,
2270 (struct p_header80 *)&p, sizeof(p), 0);
2271 }
2272 964
2273 mutex_unlock(&mdev->data.mutex); 965 sock = &mdev->tconn->data;
2274 966 p = drbd_prepare_command(mdev, sock);
2275 drbd_state_unlock(mdev); 967 if (!p)
2276 return ok; 968 return -EIO;
969 p->state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
970 return drbd_send_command(mdev, sock, P_STATE, sizeof(*p), NULL, 0);
2277} 971}
2278 972
2279/** 973/**
2280 * drbd_send_state() - After a state change, sends the new state to the peer 974 * drbd_send_state() - After a state change, sends the new state to the peer
2281 * @mdev: DRBD device. 975 * @mdev: DRBD device.
2282 * @state: the state to send, not necessarily the current state. 976 * @state: the state to send, not necessarily the current state.
2283 * 977 *
2284 * Each state change queues an "after_state_ch" work, which will eventually 978 * Each state change queues an "after_state_ch" work, which will eventually
2285 * send the resulting new state to the peer. If more state changes happen 979 * send the resulting new state to the peer. If more state changes happen
@@ -2288,50 +982,95 @@ int drbd_send_current_state(struct drbd_conf *mdev)
2288 */ 982 */
2289int drbd_send_state(struct drbd_conf *mdev, union drbd_state state) 983int drbd_send_state(struct drbd_conf *mdev, union drbd_state state)
2290{ 984{
2291 struct socket *sock; 985 struct drbd_socket *sock;
2292 struct p_state p; 986 struct p_state *p;
2293 int ok = 0;
2294 987
2295 mutex_lock(&mdev->data.mutex); 988 sock = &mdev->tconn->data;
989 p = drbd_prepare_command(mdev, sock);
990 if (!p)
991 return -EIO;
992 p->state = cpu_to_be32(state.i); /* Within the send mutex */
993 return drbd_send_command(mdev, sock, P_STATE, sizeof(*p), NULL, 0);
994}
2296 995
2297 p.state = cpu_to_be32(state.i); 996int drbd_send_state_req(struct drbd_conf *mdev, union drbd_state mask, union drbd_state val)
2298 sock = mdev->data.socket; 997{
998 struct drbd_socket *sock;
999 struct p_req_state *p;
2299 1000
2300 if (likely(sock != NULL)) { 1001 sock = &mdev->tconn->data;
2301 ok = _drbd_send_cmd(mdev, sock, P_STATE, 1002 p = drbd_prepare_command(mdev, sock);
2302 (struct p_header80 *)&p, sizeof(p), 0); 1003 if (!p)
2303 } 1004 return -EIO;
1005 p->mask = cpu_to_be32(mask.i);
1006 p->val = cpu_to_be32(val.i);
1007 return drbd_send_command(mdev, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
1008}
2304 1009
2305 mutex_unlock(&mdev->data.mutex); 1010int conn_send_state_req(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val)
1011{
1012 enum drbd_packet cmd;
1013 struct drbd_socket *sock;
1014 struct p_req_state *p;
2306 1015
2307 return ok; 1016 cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
1017 sock = &tconn->data;
1018 p = conn_prepare_command(tconn, sock);
1019 if (!p)
1020 return -EIO;
1021 p->mask = cpu_to_be32(mask.i);
1022 p->val = cpu_to_be32(val.i);
1023 return conn_send_command(tconn, sock, cmd, sizeof(*p), NULL, 0);
2308} 1024}
2309 1025
2310int drbd_send_state_req(struct drbd_conf *mdev, 1026void drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
2311 union drbd_state mask, union drbd_state val)
2312{ 1027{
2313 struct p_req_state p; 1028 struct drbd_socket *sock;
1029 struct p_req_state_reply *p;
2314 1030
2315 p.mask = cpu_to_be32(mask.i); 1031 sock = &mdev->tconn->meta;
2316 p.val = cpu_to_be32(val.i); 1032 p = drbd_prepare_command(mdev, sock);
1033 if (p) {
1034 p->retcode = cpu_to_be32(retcode);
1035 drbd_send_command(mdev, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
1036 }
1037}
1038
1039void conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode)
1040{
1041 struct drbd_socket *sock;
1042 struct p_req_state_reply *p;
1043 enum drbd_packet cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
2317 1044
2318 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ, 1045 sock = &tconn->meta;
2319 (struct p_header80 *)&p, sizeof(p)); 1046 p = conn_prepare_command(tconn, sock);
1047 if (p) {
1048 p->retcode = cpu_to_be32(retcode);
1049 conn_send_command(tconn, sock, cmd, sizeof(*p), NULL, 0);
1050 }
2320} 1051}
2321 1052
2322int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode) 1053static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
2323{ 1054{
2324 struct p_req_state_reply p; 1055 BUG_ON(code & ~0xf);
1056 p->encoding = (p->encoding & ~0xf) | code;
1057}
2325 1058
2326 p.retcode = cpu_to_be32(retcode); 1059static void dcbp_set_start(struct p_compressed_bm *p, int set)
1060{
1061 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
1062}
2327 1063
2328 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY, 1064static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
2329 (struct p_header80 *)&p, sizeof(p)); 1065{
1066 BUG_ON(n & ~0x7);
1067 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
2330} 1068}
2331 1069
2332int fill_bitmap_rle_bits(struct drbd_conf *mdev, 1070int fill_bitmap_rle_bits(struct drbd_conf *mdev,
2333 struct p_compressed_bm *p, 1071 struct p_compressed_bm *p,
2334 struct bm_xfer_ctx *c) 1072 unsigned int size,
1073 struct bm_xfer_ctx *c)
2335{ 1074{
2336 struct bitstream bs; 1075 struct bitstream bs;
2337 unsigned long plain_bits; 1076 unsigned long plain_bits;
@@ -2339,19 +1078,21 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
2339 unsigned long rl; 1078 unsigned long rl;
2340 unsigned len; 1079 unsigned len;
2341 unsigned toggle; 1080 unsigned toggle;
2342 int bits; 1081 int bits, use_rle;
2343 1082
2344 /* may we use this feature? */ 1083 /* may we use this feature? */
2345 if ((mdev->sync_conf.use_rle == 0) || 1084 rcu_read_lock();
2346 (mdev->agreed_pro_version < 90)) 1085 use_rle = rcu_dereference(mdev->tconn->net_conf)->use_rle;
2347 return 0; 1086 rcu_read_unlock();
1087 if (!use_rle || mdev->tconn->agreed_pro_version < 90)
1088 return 0;
2348 1089
2349 if (c->bit_offset >= c->bm_bits) 1090 if (c->bit_offset >= c->bm_bits)
2350 return 0; /* nothing to do. */ 1091 return 0; /* nothing to do. */
2351 1092
2352 /* use at most thus many bytes */ 1093 /* use at most thus many bytes */
2353 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0); 1094 bitstream_init(&bs, p->code, size, 0);
2354 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX); 1095 memset(p->code, 0, size);
2355 /* plain bits covered in this code string */ 1096 /* plain bits covered in this code string */
2356 plain_bits = 0; 1097 plain_bits = 0;
2357 1098
@@ -2373,12 +1114,12 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
2373 if (rl == 0) { 1114 if (rl == 0) {
2374 /* the first checked bit was set, 1115 /* the first checked bit was set,
2375 * store start value, */ 1116 * store start value, */
2376 DCBP_set_start(p, 1); 1117 dcbp_set_start(p, 1);
2377 /* but skip encoding of zero run length */ 1118 /* but skip encoding of zero run length */
2378 toggle = !toggle; 1119 toggle = !toggle;
2379 continue; 1120 continue;
2380 } 1121 }
2381 DCBP_set_start(p, 0); 1122 dcbp_set_start(p, 0);
2382 } 1123 }
2383 1124
2384 /* paranoia: catch zero runlength. 1125 /* paranoia: catch zero runlength.
@@ -2418,7 +1159,7 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
2418 bm_xfer_ctx_bit_to_word_offset(c); 1159 bm_xfer_ctx_bit_to_word_offset(c);
2419 1160
2420 /* store pad_bits */ 1161 /* store pad_bits */
2421 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7); 1162 dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
2422 1163
2423 return len; 1164 return len;
2424} 1165}
@@ -2430,48 +1171,52 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
2430 * code upon failure. 1171 * code upon failure.
2431 */ 1172 */
2432static int 1173static int
2433send_bitmap_rle_or_plain(struct drbd_conf *mdev, 1174send_bitmap_rle_or_plain(struct drbd_conf *mdev, struct bm_xfer_ctx *c)
2434 struct p_header80 *h, struct bm_xfer_ctx *c)
2435{ 1175{
2436 struct p_compressed_bm *p = (void*)h; 1176 struct drbd_socket *sock = &mdev->tconn->data;
2437 unsigned long num_words; 1177 unsigned int header_size = drbd_header_size(mdev->tconn);
2438 int len; 1178 struct p_compressed_bm *p = sock->sbuf + header_size;
2439 int ok; 1179 int len, err;
2440
2441 len = fill_bitmap_rle_bits(mdev, p, c);
2442 1180
1181 len = fill_bitmap_rle_bits(mdev, p,
1182 DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
2443 if (len < 0) 1183 if (len < 0)
2444 return -EIO; 1184 return -EIO;
2445 1185
2446 if (len) { 1186 if (len) {
2447 DCBP_set_code(p, RLE_VLI_Bits); 1187 dcbp_set_code(p, RLE_VLI_Bits);
2448 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_COMPRESSED_BITMAP, h, 1188 err = __send_command(mdev->tconn, mdev->vnr, sock,
2449 sizeof(*p) + len, 0); 1189 P_COMPRESSED_BITMAP, sizeof(*p) + len,
2450 1190 NULL, 0);
2451 c->packets[0]++; 1191 c->packets[0]++;
2452 c->bytes[0] += sizeof(*p) + len; 1192 c->bytes[0] += header_size + sizeof(*p) + len;
2453 1193
2454 if (c->bit_offset >= c->bm_bits) 1194 if (c->bit_offset >= c->bm_bits)
2455 len = 0; /* DONE */ 1195 len = 0; /* DONE */
2456 } else { 1196 } else {
2457 /* was not compressible. 1197 /* was not compressible.
2458 * send a buffer full of plain text bits instead. */ 1198 * send a buffer full of plain text bits instead. */
2459 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset); 1199 unsigned int data_size;
2460 len = num_words * sizeof(long); 1200 unsigned long num_words;
1201 unsigned long *p = sock->sbuf + header_size;
1202
1203 data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
1204 num_words = min_t(size_t, data_size / sizeof(*p),
1205 c->bm_words - c->word_offset);
1206 len = num_words * sizeof(*p);
2461 if (len) 1207 if (len)
2462 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload); 1208 drbd_bm_get_lel(mdev, c->word_offset, num_words, p);
2463 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BITMAP, 1209 err = __send_command(mdev->tconn, mdev->vnr, sock, P_BITMAP, len, NULL, 0);
2464 h, sizeof(struct p_header80) + len, 0);
2465 c->word_offset += num_words; 1210 c->word_offset += num_words;
2466 c->bit_offset = c->word_offset * BITS_PER_LONG; 1211 c->bit_offset = c->word_offset * BITS_PER_LONG;
2467 1212
2468 c->packets[1]++; 1213 c->packets[1]++;
2469 c->bytes[1] += sizeof(struct p_header80) + len; 1214 c->bytes[1] += header_size + len;
2470 1215
2471 if (c->bit_offset > c->bm_bits) 1216 if (c->bit_offset > c->bm_bits)
2472 c->bit_offset = c->bm_bits; 1217 c->bit_offset = c->bm_bits;
2473 } 1218 }
2474 if (ok) { 1219 if (!err) {
2475 if (len == 0) { 1220 if (len == 0) {
2476 INFO_bm_xfer_stats(mdev, "send", c); 1221 INFO_bm_xfer_stats(mdev, "send", c);
2477 return 0; 1222 return 0;
@@ -2482,21 +1227,13 @@ send_bitmap_rle_or_plain(struct drbd_conf *mdev,
2482} 1227}
2483 1228
2484/* See the comment at receive_bitmap() */ 1229/* See the comment at receive_bitmap() */
2485int _drbd_send_bitmap(struct drbd_conf *mdev) 1230static int _drbd_send_bitmap(struct drbd_conf *mdev)
2486{ 1231{
2487 struct bm_xfer_ctx c; 1232 struct bm_xfer_ctx c;
2488 struct p_header80 *p;
2489 int err; 1233 int err;
2490 1234
2491 ERR_IF(!mdev->bitmap) return false; 1235 if (!expect(mdev->bitmap))
2492
2493 /* maybe we should use some per thread scratch page,
2494 * and allocate that during initial device creation? */
2495 p = (struct p_header80 *) __get_free_page(GFP_NOIO);
2496 if (!p) {
2497 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
2498 return false; 1236 return false;
2499 }
2500 1237
2501 if (get_ldev(mdev)) { 1238 if (get_ldev(mdev)) {
2502 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) { 1239 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
@@ -2521,37 +1258,39 @@ int _drbd_send_bitmap(struct drbd_conf *mdev)
2521 }; 1258 };
2522 1259
2523 do { 1260 do {
2524 err = send_bitmap_rle_or_plain(mdev, p, &c); 1261 err = send_bitmap_rle_or_plain(mdev, &c);
2525 } while (err > 0); 1262 } while (err > 0);
2526 1263
2527 free_page((unsigned long) p);
2528 return err == 0; 1264 return err == 0;
2529} 1265}
2530 1266
2531int drbd_send_bitmap(struct drbd_conf *mdev) 1267int drbd_send_bitmap(struct drbd_conf *mdev)
2532{ 1268{
2533 int err; 1269 struct drbd_socket *sock = &mdev->tconn->data;
1270 int err = -1;
2534 1271
2535 if (!drbd_get_data_sock(mdev)) 1272 mutex_lock(&sock->mutex);
2536 return -1; 1273 if (sock->socket)
2537 err = !_drbd_send_bitmap(mdev); 1274 err = !_drbd_send_bitmap(mdev);
2538 drbd_put_data_sock(mdev); 1275 mutex_unlock(&sock->mutex);
2539 return err; 1276 return err;
2540} 1277}
2541 1278
2542int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size) 1279void drbd_send_b_ack(struct drbd_tconn *tconn, u32 barrier_nr, u32 set_size)
2543{ 1280{
2544 int ok; 1281 struct drbd_socket *sock;
2545 struct p_barrier_ack p; 1282 struct p_barrier_ack *p;
2546 1283
2547 p.barrier = barrier_nr; 1284 if (tconn->cstate < C_WF_REPORT_PARAMS)
2548 p.set_size = cpu_to_be32(set_size); 1285 return;
2549 1286
2550 if (mdev->state.conn < C_CONNECTED) 1287 sock = &tconn->meta;
2551 return false; 1288 p = conn_prepare_command(tconn, sock);
2552 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, 1289 if (!p)
2553 (struct p_header80 *)&p, sizeof(p)); 1290 return;
2554 return ok; 1291 p->barrier = barrier_nr;
1292 p->set_size = cpu_to_be32(set_size);
1293 conn_send_command(tconn, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0);
2555} 1294}
2556 1295
2557/** 1296/**
@@ -2562,62 +1301,62 @@ int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
2562 * @blksize: size in byte, needs to be in big endian byte order 1301 * @blksize: size in byte, needs to be in big endian byte order
2563 * @block_id: Id, big endian byte order 1302 * @block_id: Id, big endian byte order
2564 */ 1303 */
2565static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd, 1304static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
2566 u64 sector, 1305 u64 sector, u32 blksize, u64 block_id)
2567 u32 blksize,
2568 u64 block_id)
2569{ 1306{
2570 int ok; 1307 struct drbd_socket *sock;
2571 struct p_block_ack p; 1308 struct p_block_ack *p;
2572 1309
2573 p.sector = sector; 1310 if (mdev->state.conn < C_CONNECTED)
2574 p.block_id = block_id; 1311 return -EIO;
2575 p.blksize = blksize;
2576 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
2577 1312
2578 if (!mdev->meta.socket || mdev->state.conn < C_CONNECTED) 1313 sock = &mdev->tconn->meta;
2579 return false; 1314 p = drbd_prepare_command(mdev, sock);
2580 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, 1315 if (!p)
2581 (struct p_header80 *)&p, sizeof(p)); 1316 return -EIO;
2582 return ok; 1317 p->sector = sector;
1318 p->block_id = block_id;
1319 p->blksize = blksize;
1320 p->seq_num = cpu_to_be32(atomic_inc_return(&mdev->packet_seq));
1321 return drbd_send_command(mdev, sock, cmd, sizeof(*p), NULL, 0);
2583} 1322}
2584 1323
2585/* dp->sector and dp->block_id already/still in network byte order, 1324/* dp->sector and dp->block_id already/still in network byte order,
2586 * data_size is payload size according to dp->head, 1325 * data_size is payload size according to dp->head,
2587 * and may need to be corrected for digest size. */ 1326 * and may need to be corrected for digest size. */
2588int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd, 1327void drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
2589 struct p_data *dp, int data_size) 1328 struct p_data *dp, int data_size)
2590{ 1329{
2591 data_size -= (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ? 1330 if (mdev->tconn->peer_integrity_tfm)
2592 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0; 1331 data_size -= crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
2593 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size), 1332 _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
2594 dp->block_id); 1333 dp->block_id);
2595} 1334}
2596 1335
2597int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd, 1336void drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
2598 struct p_block_req *rp) 1337 struct p_block_req *rp)
2599{ 1338{
2600 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id); 1339 _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
2601} 1340}
2602 1341
2603/** 1342/**
2604 * drbd_send_ack() - Sends an ack packet 1343 * drbd_send_ack() - Sends an ack packet
2605 * @mdev: DRBD device. 1344 * @mdev: DRBD device
2606 * @cmd: Packet command code. 1345 * @cmd: packet command code
2607 * @e: Epoch entry. 1346 * @peer_req: peer request
2608 */ 1347 */
2609int drbd_send_ack(struct drbd_conf *mdev, 1348int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
2610 enum drbd_packets cmd, struct drbd_epoch_entry *e) 1349 struct drbd_peer_request *peer_req)
2611{ 1350{
2612 return _drbd_send_ack(mdev, cmd, 1351 return _drbd_send_ack(mdev, cmd,
2613 cpu_to_be64(e->sector), 1352 cpu_to_be64(peer_req->i.sector),
2614 cpu_to_be32(e->size), 1353 cpu_to_be32(peer_req->i.size),
2615 e->block_id); 1354 peer_req->block_id);
2616} 1355}
2617 1356
2618/* This function misuses the block_id field to signal if the blocks 1357/* This function misuses the block_id field to signal if the blocks
2619 * are is sync or not. */ 1358 * are is sync or not. */
2620int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd, 1359int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
2621 sector_t sector, int blksize, u64 block_id) 1360 sector_t sector, int blksize, u64 block_id)
2622{ 1361{
2623 return _drbd_send_ack(mdev, cmd, 1362 return _drbd_send_ack(mdev, cmd,
@@ -2629,85 +1368,87 @@ int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
2629int drbd_send_drequest(struct drbd_conf *mdev, int cmd, 1368int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
2630 sector_t sector, int size, u64 block_id) 1369 sector_t sector, int size, u64 block_id)
2631{ 1370{
2632 int ok; 1371 struct drbd_socket *sock;
2633 struct p_block_req p; 1372 struct p_block_req *p;
2634 1373
2635 p.sector = cpu_to_be64(sector); 1374 sock = &mdev->tconn->data;
2636 p.block_id = block_id; 1375 p = drbd_prepare_command(mdev, sock);
2637 p.blksize = cpu_to_be32(size); 1376 if (!p)
2638 1377 return -EIO;
2639 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, 1378 p->sector = cpu_to_be64(sector);
2640 (struct p_header80 *)&p, sizeof(p)); 1379 p->block_id = block_id;
2641 return ok; 1380 p->blksize = cpu_to_be32(size);
1381 return drbd_send_command(mdev, sock, cmd, sizeof(*p), NULL, 0);
2642} 1382}
2643 1383
2644int drbd_send_drequest_csum(struct drbd_conf *mdev, 1384int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
2645 sector_t sector, int size, 1385 void *digest, int digest_size, enum drbd_packet cmd)
2646 void *digest, int digest_size,
2647 enum drbd_packets cmd)
2648{ 1386{
2649 int ok; 1387 struct drbd_socket *sock;
2650 struct p_block_req p; 1388 struct p_block_req *p;
2651
2652 p.sector = cpu_to_be64(sector);
2653 p.block_id = BE_DRBD_MAGIC + 0xbeef;
2654 p.blksize = cpu_to_be32(size);
2655
2656 p.head.magic = BE_DRBD_MAGIC;
2657 p.head.command = cpu_to_be16(cmd);
2658 p.head.length = cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + digest_size);
2659
2660 mutex_lock(&mdev->data.mutex);
2661 1389
2662 ok = (sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), 0)); 1390 /* FIXME: Put the digest into the preallocated socket buffer. */
2663 ok = ok && (digest_size == drbd_send(mdev, mdev->data.socket, digest, digest_size, 0));
2664 1391
2665 mutex_unlock(&mdev->data.mutex); 1392 sock = &mdev->tconn->data;
2666 1393 p = drbd_prepare_command(mdev, sock);
2667 return ok; 1394 if (!p)
1395 return -EIO;
1396 p->sector = cpu_to_be64(sector);
1397 p->block_id = ID_SYNCER /* unused */;
1398 p->blksize = cpu_to_be32(size);
1399 return drbd_send_command(mdev, sock, cmd, sizeof(*p),
1400 digest, digest_size);
2668} 1401}
2669 1402
2670int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size) 1403int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
2671{ 1404{
2672 int ok; 1405 struct drbd_socket *sock;
2673 struct p_block_req p; 1406 struct p_block_req *p;
2674
2675 p.sector = cpu_to_be64(sector);
2676 p.block_id = BE_DRBD_MAGIC + 0xbabe;
2677 p.blksize = cpu_to_be32(size);
2678 1407
2679 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, 1408 sock = &mdev->tconn->data;
2680 (struct p_header80 *)&p, sizeof(p)); 1409 p = drbd_prepare_command(mdev, sock);
2681 return ok; 1410 if (!p)
1411 return -EIO;
1412 p->sector = cpu_to_be64(sector);
1413 p->block_id = ID_SYNCER /* unused */;
1414 p->blksize = cpu_to_be32(size);
1415 return drbd_send_command(mdev, sock, P_OV_REQUEST, sizeof(*p), NULL, 0);
2682} 1416}
2683 1417
2684/* called on sndtimeo 1418/* called on sndtimeo
2685 * returns false if we should retry, 1419 * returns false if we should retry,
2686 * true if we think connection is dead 1420 * true if we think connection is dead
2687 */ 1421 */
2688static int we_should_drop_the_connection(struct drbd_conf *mdev, struct socket *sock) 1422static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
2689{ 1423{
2690 int drop_it; 1424 int drop_it;
2691 /* long elapsed = (long)(jiffies - mdev->last_received); */ 1425 /* long elapsed = (long)(jiffies - mdev->last_received); */
2692 1426
2693 drop_it = mdev->meta.socket == sock 1427 drop_it = tconn->meta.socket == sock
2694 || !mdev->asender.task 1428 || !tconn->asender.task
2695 || get_t_state(&mdev->asender) != Running 1429 || get_t_state(&tconn->asender) != RUNNING
2696 || mdev->state.conn < C_CONNECTED; 1430 || tconn->cstate < C_WF_REPORT_PARAMS;
2697 1431
2698 if (drop_it) 1432 if (drop_it)
2699 return true; 1433 return true;
2700 1434
2701 drop_it = !--mdev->ko_count; 1435 drop_it = !--tconn->ko_count;
2702 if (!drop_it) { 1436 if (!drop_it) {
2703 dev_err(DEV, "[%s/%d] sock_sendmsg time expired, ko = %u\n", 1437 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
2704 current->comm, current->pid, mdev->ko_count); 1438 current->comm, current->pid, tconn->ko_count);
2705 request_ping(mdev); 1439 request_ping(tconn);
2706 } 1440 }
2707 1441
2708 return drop_it; /* && (mdev->state == R_PRIMARY) */; 1442 return drop_it; /* && (mdev->state == R_PRIMARY) */;
2709} 1443}
2710 1444
1445static void drbd_update_congested(struct drbd_tconn *tconn)
1446{
1447 struct sock *sk = tconn->data.socket->sk;
1448 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
1449 set_bit(NET_CONGESTED, &tconn->flags);
1450}
1451
2711/* The idea of sendpage seems to be to put some kind of reference 1452/* The idea of sendpage seems to be to put some kind of reference
2712 * to the page into the skb, and to hand it over to the NIC. In 1453 * to the page into the skb, and to hand it over to the NIC. In
2713 * this process get_page() gets called. 1454 * this process get_page() gets called.
@@ -2730,21 +1471,28 @@ static int we_should_drop_the_connection(struct drbd_conf *mdev, struct socket *
2730 * with page_count == 0 or PageSlab. 1471 * with page_count == 0 or PageSlab.
2731 */ 1472 */
2732static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page, 1473static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
2733 int offset, size_t size, unsigned msg_flags) 1474 int offset, size_t size, unsigned msg_flags)
2734{ 1475{
2735 int sent = drbd_send(mdev, mdev->data.socket, kmap(page) + offset, size, msg_flags); 1476 struct socket *socket;
1477 void *addr;
1478 int err;
1479
1480 socket = mdev->tconn->data.socket;
1481 addr = kmap(page) + offset;
1482 err = drbd_send_all(mdev->tconn, socket, addr, size, msg_flags);
2736 kunmap(page); 1483 kunmap(page);
2737 if (sent == size) 1484 if (!err)
2738 mdev->send_cnt += size>>9; 1485 mdev->send_cnt += size >> 9;
2739 return sent == size; 1486 return err;
2740} 1487}
2741 1488
2742static int _drbd_send_page(struct drbd_conf *mdev, struct page *page, 1489static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
2743 int offset, size_t size, unsigned msg_flags) 1490 int offset, size_t size, unsigned msg_flags)
2744{ 1491{
1492 struct socket *socket = mdev->tconn->data.socket;
2745 mm_segment_t oldfs = get_fs(); 1493 mm_segment_t oldfs = get_fs();
2746 int sent, ok;
2747 int len = size; 1494 int len = size;
1495 int err = -EIO;
2748 1496
2749 /* e.g. XFS meta- & log-data is in slab pages, which have a 1497 /* e.g. XFS meta- & log-data is in slab pages, which have a
2750 * page_count of 0 and/or have PageSlab() set. 1498 * page_count of 0 and/or have PageSlab() set.
@@ -2756,34 +1504,35 @@ static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
2756 return _drbd_no_send_page(mdev, page, offset, size, msg_flags); 1504 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
2757 1505
2758 msg_flags |= MSG_NOSIGNAL; 1506 msg_flags |= MSG_NOSIGNAL;
2759 drbd_update_congested(mdev); 1507 drbd_update_congested(mdev->tconn);
2760 set_fs(KERNEL_DS); 1508 set_fs(KERNEL_DS);
2761 do { 1509 do {
2762 sent = mdev->data.socket->ops->sendpage(mdev->data.socket, page, 1510 int sent;
2763 offset, len, 1511
2764 msg_flags); 1512 sent = socket->ops->sendpage(socket, page, offset, len, msg_flags);
2765 if (sent == -EAGAIN) {
2766 if (we_should_drop_the_connection(mdev,
2767 mdev->data.socket))
2768 break;
2769 else
2770 continue;
2771 }
2772 if (sent <= 0) { 1513 if (sent <= 0) {
1514 if (sent == -EAGAIN) {
1515 if (we_should_drop_the_connection(mdev->tconn, socket))
1516 break;
1517 continue;
1518 }
2773 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n", 1519 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
2774 __func__, (int)size, len, sent); 1520 __func__, (int)size, len, sent);
1521 if (sent < 0)
1522 err = sent;
2775 break; 1523 break;
2776 } 1524 }
2777 len -= sent; 1525 len -= sent;
2778 offset += sent; 1526 offset += sent;
2779 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/); 1527 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
2780 set_fs(oldfs); 1528 set_fs(oldfs);
2781 drbd_clear_flag(mdev, NET_CONGESTED); 1529 clear_bit(NET_CONGESTED, &mdev->tconn->flags);
2782 1530
2783 ok = (len == 0); 1531 if (len == 0) {
2784 if (likely(ok)) 1532 err = 0;
2785 mdev->send_cnt += size>>9; 1533 mdev->send_cnt += size >> 9;
2786 return ok; 1534 }
1535 return err;
2787} 1536}
2788 1537
2789static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio) 1538static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
@@ -2792,12 +1541,15 @@ static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
2792 int i; 1541 int i;
2793 /* hint all but last page with MSG_MORE */ 1542 /* hint all but last page with MSG_MORE */
2794 bio_for_each_segment(bvec, bio, i) { 1543 bio_for_each_segment(bvec, bio, i) {
2795 if (!_drbd_no_send_page(mdev, bvec->bv_page, 1544 int err;
2796 bvec->bv_offset, bvec->bv_len, 1545
2797 i == bio->bi_vcnt -1 ? 0 : MSG_MORE)) 1546 err = _drbd_no_send_page(mdev, bvec->bv_page,
2798 return 0; 1547 bvec->bv_offset, bvec->bv_len,
1548 i == bio->bi_vcnt - 1 ? 0 : MSG_MORE);
1549 if (err)
1550 return err;
2799 } 1551 }
2800 return 1; 1552 return 0;
2801} 1553}
2802 1554
2803static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio) 1555static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
@@ -2806,32 +1558,40 @@ static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
2806 int i; 1558 int i;
2807 /* hint all but last page with MSG_MORE */ 1559 /* hint all but last page with MSG_MORE */
2808 bio_for_each_segment(bvec, bio, i) { 1560 bio_for_each_segment(bvec, bio, i) {
2809 if (!_drbd_send_page(mdev, bvec->bv_page, 1561 int err;
2810 bvec->bv_offset, bvec->bv_len, 1562
2811 i == bio->bi_vcnt -1 ? 0 : MSG_MORE)) 1563 err = _drbd_send_page(mdev, bvec->bv_page,
2812 return 0; 1564 bvec->bv_offset, bvec->bv_len,
1565 i == bio->bi_vcnt - 1 ? 0 : MSG_MORE);
1566 if (err)
1567 return err;
2813 } 1568 }
2814 return 1; 1569 return 0;
2815} 1570}
2816 1571
2817static int _drbd_send_zc_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e) 1572static int _drbd_send_zc_ee(struct drbd_conf *mdev,
1573 struct drbd_peer_request *peer_req)
2818{ 1574{
2819 struct page *page = e->pages; 1575 struct page *page = peer_req->pages;
2820 unsigned len = e->size; 1576 unsigned len = peer_req->i.size;
1577 int err;
1578
2821 /* hint all but last page with MSG_MORE */ 1579 /* hint all but last page with MSG_MORE */
2822 page_chain_for_each(page) { 1580 page_chain_for_each(page) {
2823 unsigned l = min_t(unsigned, len, PAGE_SIZE); 1581 unsigned l = min_t(unsigned, len, PAGE_SIZE);
2824 if (!_drbd_send_page(mdev, page, 0, l, 1582
2825 page_chain_next(page) ? MSG_MORE : 0)) 1583 err = _drbd_send_page(mdev, page, 0, l,
2826 return 0; 1584 page_chain_next(page) ? MSG_MORE : 0);
1585 if (err)
1586 return err;
2827 len -= l; 1587 len -= l;
2828 } 1588 }
2829 return 1; 1589 return 0;
2830} 1590}
2831 1591
2832static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw) 1592static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
2833{ 1593{
2834 if (mdev->agreed_pro_version >= 95) 1594 if (mdev->tconn->agreed_pro_version >= 95)
2835 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) | 1595 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
2836 (bi_rw & REQ_FUA ? DP_FUA : 0) | 1596 (bi_rw & REQ_FUA ? DP_FUA : 0) |
2837 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) | 1597 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
@@ -2845,50 +1605,36 @@ static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
2845 */ 1605 */
2846int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req) 1606int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
2847{ 1607{
2848 int ok = 1; 1608 struct drbd_socket *sock;
2849 struct p_data p; 1609 struct p_data *p;
2850 unsigned int dp_flags = 0; 1610 unsigned int dp_flags = 0;
2851 void *dgb;
2852 int dgs; 1611 int dgs;
1612 int err;
2853 1613
2854 if (!drbd_get_data_sock(mdev)) 1614 sock = &mdev->tconn->data;
2855 return 0; 1615 p = drbd_prepare_command(mdev, sock);
2856 1616 dgs = mdev->tconn->integrity_tfm ? crypto_hash_digestsize(mdev->tconn->integrity_tfm) : 0;
2857 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
2858 crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
2859
2860 if (req->size <= DRBD_MAX_SIZE_H80_PACKET) {
2861 p.head.h80.magic = BE_DRBD_MAGIC;
2862 p.head.h80.command = cpu_to_be16(P_DATA);
2863 p.head.h80.length =
2864 cpu_to_be16(sizeof(p) - sizeof(union p_header) + dgs + req->size);
2865 } else {
2866 p.head.h95.magic = BE_DRBD_MAGIC_BIG;
2867 p.head.h95.command = cpu_to_be16(P_DATA);
2868 p.head.h95.length =
2869 cpu_to_be32(sizeof(p) - sizeof(union p_header) + dgs + req->size);
2870 }
2871
2872 p.sector = cpu_to_be64(req->sector);
2873 p.block_id = (unsigned long)req;
2874 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
2875 1617
1618 if (!p)
1619 return -EIO;
1620 p->sector = cpu_to_be64(req->i.sector);
1621 p->block_id = (unsigned long)req;
1622 p->seq_num = cpu_to_be32(atomic_inc_return(&mdev->packet_seq));
2876 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw); 1623 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
2877
2878 if (mdev->state.conn >= C_SYNC_SOURCE && 1624 if (mdev->state.conn >= C_SYNC_SOURCE &&
2879 mdev->state.conn <= C_PAUSED_SYNC_T) 1625 mdev->state.conn <= C_PAUSED_SYNC_T)
2880 dp_flags |= DP_MAY_SET_IN_SYNC; 1626 dp_flags |= DP_MAY_SET_IN_SYNC;
2881 1627 if (mdev->tconn->agreed_pro_version >= 100) {
2882 p.dp_flags = cpu_to_be32(dp_flags); 1628 if (req->rq_state & RQ_EXP_RECEIVE_ACK)
2883 drbd_set_flag(mdev, UNPLUG_REMOTE); 1629 dp_flags |= DP_SEND_RECEIVE_ACK;
2884 ok = (sizeof(p) == 1630 if (req->rq_state & RQ_EXP_WRITE_ACK)
2885 drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0)); 1631 dp_flags |= DP_SEND_WRITE_ACK;
2886 if (ok && dgs) { 1632 }
2887 dgb = mdev->int_dig_out; 1633 p->dp_flags = cpu_to_be32(dp_flags);
2888 drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, dgb); 1634 if (dgs)
2889 ok = dgs == drbd_send(mdev, mdev->data.socket, dgb, dgs, 0); 1635 drbd_csum_bio(mdev, mdev->tconn->integrity_tfm, req->master_bio, p + 1);
2890 } 1636 err = __send_command(mdev->tconn, mdev->vnr, sock, P_DATA, sizeof(*p) + dgs, NULL, req->i.size);
2891 if (ok) { 1637 if (!err) {
2892 /* For protocol A, we have to memcpy the payload into 1638 /* For protocol A, we have to memcpy the payload into
2893 * socket buffers, as we may complete right away 1639 * socket buffers, as we may complete right away
2894 * as soon as we handed it over to tcp, at which point the data 1640 * as soon as we handed it over to tcp, at which point the data
@@ -2900,92 +1646,76 @@ int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
2900 * out ok after sending on this side, but does not fit on the 1646 * out ok after sending on this side, but does not fit on the
2901 * receiving side, we sure have detected corruption elsewhere. 1647 * receiving side, we sure have detected corruption elsewhere.
2902 */ 1648 */
2903 if (mdev->net_conf->wire_protocol == DRBD_PROT_A || dgs) 1649 if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || dgs)
2904 ok = _drbd_send_bio(mdev, req->master_bio); 1650 err = _drbd_send_bio(mdev, req->master_bio);
2905 else 1651 else
2906 ok = _drbd_send_zc_bio(mdev, req->master_bio); 1652 err = _drbd_send_zc_bio(mdev, req->master_bio);
2907 1653
2908 /* double check digest, sometimes buffers have been modified in flight. */ 1654 /* double check digest, sometimes buffers have been modified in flight. */
2909 if (dgs > 0 && dgs <= 64) { 1655 if (dgs > 0 && dgs <= 64) {
2910 /* 64 byte, 512 bit, is the largest digest size 1656 /* 64 byte, 512 bit, is the largest digest size
2911 * currently supported in kernel crypto. */ 1657 * currently supported in kernel crypto. */
2912 unsigned char digest[64]; 1658 unsigned char digest[64];
2913 drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, digest); 1659 drbd_csum_bio(mdev, mdev->tconn->integrity_tfm, req->master_bio, digest);
2914 if (memcmp(mdev->int_dig_out, digest, dgs)) { 1660 if (memcmp(p + 1, digest, dgs)) {
2915 dev_warn(DEV, 1661 dev_warn(DEV,
2916 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n", 1662 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
2917 (unsigned long long)req->sector, req->size); 1663 (unsigned long long)req->i.sector, req->i.size);
2918 } 1664 }
2919 } /* else if (dgs > 64) { 1665 } /* else if (dgs > 64) {
2920 ... Be noisy about digest too large ... 1666 ... Be noisy about digest too large ...
2921 } */ 1667 } */
2922 } 1668 }
1669 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
2923 1670
2924 drbd_put_data_sock(mdev); 1671 return err;
2925
2926 return ok;
2927} 1672}
2928 1673
2929/* answer packet, used to send data back for read requests: 1674/* answer packet, used to send data back for read requests:
2930 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY) 1675 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
2931 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY) 1676 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
2932 */ 1677 */
2933int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd, 1678int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
2934 struct drbd_epoch_entry *e) 1679 struct drbd_peer_request *peer_req)
2935{ 1680{
2936 int ok; 1681 struct drbd_socket *sock;
2937 struct p_data p; 1682 struct p_data *p;
2938 void *dgb; 1683 int err;
2939 int dgs; 1684 int dgs;
2940 1685
2941 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ? 1686 sock = &mdev->tconn->data;
2942 crypto_hash_digestsize(mdev->integrity_w_tfm) : 0; 1687 p = drbd_prepare_command(mdev, sock);
2943 1688
2944 if (e->size <= DRBD_MAX_SIZE_H80_PACKET) { 1689 dgs = mdev->tconn->integrity_tfm ? crypto_hash_digestsize(mdev->tconn->integrity_tfm) : 0;
2945 p.head.h80.magic = BE_DRBD_MAGIC;
2946 p.head.h80.command = cpu_to_be16(cmd);
2947 p.head.h80.length =
2948 cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
2949 } else {
2950 p.head.h95.magic = BE_DRBD_MAGIC_BIG;
2951 p.head.h95.command = cpu_to_be16(cmd);
2952 p.head.h95.length =
2953 cpu_to_be32(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
2954 }
2955 1690
2956 p.sector = cpu_to_be64(e->sector); 1691 if (!p)
2957 p.block_id = e->block_id; 1692 return -EIO;
2958 /* p.seq_num = 0; No sequence numbers here.. */ 1693 p->sector = cpu_to_be64(peer_req->i.sector);
2959 1694 p->block_id = peer_req->block_id;
2960 /* Only called by our kernel thread. 1695 p->seq_num = 0; /* unused */
2961 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL 1696 p->dp_flags = 0;
2962 * in response to admin command or module unload. 1697 if (dgs)
2963 */ 1698 drbd_csum_ee(mdev, mdev->tconn->integrity_tfm, peer_req, p + 1);
2964 if (!drbd_get_data_sock(mdev)) 1699 err = __send_command(mdev->tconn, mdev->vnr, sock, cmd, sizeof(*p) + dgs, NULL, peer_req->i.size);
2965 return 0; 1700 if (!err)
2966 1701 err = _drbd_send_zc_ee(mdev, peer_req);
2967 ok = sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0); 1702 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
2968 if (ok && dgs) {
2969 dgb = mdev->int_dig_out;
2970 drbd_csum_ee(mdev, mdev->integrity_w_tfm, e, dgb);
2971 ok = dgs == drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
2972 }
2973 if (ok)
2974 ok = _drbd_send_zc_ee(mdev, e);
2975
2976 drbd_put_data_sock(mdev);
2977 1703
2978 return ok; 1704 return err;
2979} 1705}
2980 1706
2981int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req) 1707int drbd_send_out_of_sync(struct drbd_conf *mdev, struct drbd_request *req)
2982{ 1708{
2983 struct p_block_desc p; 1709 struct drbd_socket *sock;
2984 1710 struct p_block_desc *p;
2985 p.sector = cpu_to_be64(req->sector);
2986 p.blksize = cpu_to_be32(req->size);
2987 1711
2988 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p)); 1712 sock = &mdev->tconn->data;
1713 p = drbd_prepare_command(mdev, sock);
1714 if (!p)
1715 return -EIO;
1716 p->sector = cpu_to_be64(req->i.sector);
1717 p->blksize = cpu_to_be32(req->i.size);
1718 return drbd_send_command(mdev, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
2989} 1719}
2990 1720
2991/* 1721/*
@@ -3004,7 +1734,7 @@ int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
3004/* 1734/*
3005 * you must have down()ed the appropriate [m]sock_mutex elsewhere! 1735 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
3006 */ 1736 */
3007int drbd_send(struct drbd_conf *mdev, struct socket *sock, 1737int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
3008 void *buf, size_t size, unsigned msg_flags) 1738 void *buf, size_t size, unsigned msg_flags)
3009{ 1739{
3010 struct kvec iov; 1740 struct kvec iov;
@@ -3012,7 +1742,7 @@ int drbd_send(struct drbd_conf *mdev, struct socket *sock,
3012 int rv, sent = 0; 1742 int rv, sent = 0;
3013 1743
3014 if (!sock) 1744 if (!sock)
3015 return -1000; 1745 return -EBADR;
3016 1746
3017 /* THINK if (signal_pending) return ... ? */ 1747 /* THINK if (signal_pending) return ... ? */
3018 1748
@@ -3025,9 +1755,11 @@ int drbd_send(struct drbd_conf *mdev, struct socket *sock,
3025 msg.msg_controllen = 0; 1755 msg.msg_controllen = 0;
3026 msg.msg_flags = msg_flags | MSG_NOSIGNAL; 1756 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
3027 1757
3028 if (sock == mdev->data.socket) { 1758 if (sock == tconn->data.socket) {
3029 mdev->ko_count = mdev->net_conf->ko_count; 1759 rcu_read_lock();
3030 drbd_update_congested(mdev); 1760 tconn->ko_count = rcu_dereference(tconn->net_conf)->ko_count;
1761 rcu_read_unlock();
1762 drbd_update_congested(tconn);
3031 } 1763 }
3032 do { 1764 do {
3033 /* STRANGE 1765 /* STRANGE
@@ -3041,12 +1773,11 @@ int drbd_send(struct drbd_conf *mdev, struct socket *sock,
3041 */ 1773 */
3042 rv = kernel_sendmsg(sock, &msg, &iov, 1, size); 1774 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
3043 if (rv == -EAGAIN) { 1775 if (rv == -EAGAIN) {
3044 if (we_should_drop_the_connection(mdev, sock)) 1776 if (we_should_drop_the_connection(tconn, sock))
3045 break; 1777 break;
3046 else 1778 else
3047 continue; 1779 continue;
3048 } 1780 }
3049 D_ASSERT(rv != 0);
3050 if (rv == -EINTR) { 1781 if (rv == -EINTR) {
3051 flush_signals(current); 1782 flush_signals(current);
3052 rv = 0; 1783 rv = 0;
@@ -3058,22 +1789,40 @@ int drbd_send(struct drbd_conf *mdev, struct socket *sock,
3058 iov.iov_len -= rv; 1789 iov.iov_len -= rv;
3059 } while (sent < size); 1790 } while (sent < size);
3060 1791
3061 if (sock == mdev->data.socket) 1792 if (sock == tconn->data.socket)
3062 drbd_clear_flag(mdev, NET_CONGESTED); 1793 clear_bit(NET_CONGESTED, &tconn->flags);
3063 1794
3064 if (rv <= 0) { 1795 if (rv <= 0) {
3065 if (rv != -EAGAIN) { 1796 if (rv != -EAGAIN) {
3066 dev_err(DEV, "%s_sendmsg returned %d\n", 1797 conn_err(tconn, "%s_sendmsg returned %d\n",
3067 sock == mdev->meta.socket ? "msock" : "sock", 1798 sock == tconn->meta.socket ? "msock" : "sock",
3068 rv); 1799 rv);
3069 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE)); 1800 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
3070 } else 1801 } else
3071 drbd_force_state(mdev, NS(conn, C_TIMEOUT)); 1802 conn_request_state(tconn, NS(conn, C_TIMEOUT), CS_HARD);
3072 } 1803 }
3073 1804
3074 return sent; 1805 return sent;
3075} 1806}
3076 1807
1808/**
1809 * drbd_send_all - Send an entire buffer
1810 *
1811 * Returns 0 upon success and a negative error value otherwise.
1812 */
1813int drbd_send_all(struct drbd_tconn *tconn, struct socket *sock, void *buffer,
1814 size_t size, unsigned msg_flags)
1815{
1816 int err;
1817
1818 err = drbd_send(tconn, sock, buffer, size, msg_flags);
1819 if (err < 0)
1820 return err;
1821 if (err != size)
1822 return -EIO;
1823 return 0;
1824}
1825
3077static int drbd_open(struct block_device *bdev, fmode_t mode) 1826static int drbd_open(struct block_device *bdev, fmode_t mode)
3078{ 1827{
3079 struct drbd_conf *mdev = bdev->bd_disk->private_data; 1828 struct drbd_conf *mdev = bdev->bd_disk->private_data;
@@ -3081,7 +1830,7 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
3081 int rv = 0; 1830 int rv = 0;
3082 1831
3083 mutex_lock(&drbd_main_mutex); 1832 mutex_lock(&drbd_main_mutex);
3084 spin_lock_irqsave(&mdev->req_lock, flags); 1833 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
3085 /* to have a stable mdev->state.role 1834 /* to have a stable mdev->state.role
3086 * and no race with updating open_cnt */ 1835 * and no race with updating open_cnt */
3087 1836
@@ -3094,7 +1843,7 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
3094 1843
3095 if (!rv) 1844 if (!rv)
3096 mdev->open_cnt++; 1845 mdev->open_cnt++;
3097 spin_unlock_irqrestore(&mdev->req_lock, flags); 1846 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
3098 mutex_unlock(&drbd_main_mutex); 1847 mutex_unlock(&drbd_main_mutex);
3099 1848
3100 return rv; 1849 return rv;
@@ -3111,35 +1860,14 @@ static int drbd_release(struct gendisk *gd, fmode_t mode)
3111 1860
3112static void drbd_set_defaults(struct drbd_conf *mdev) 1861static void drbd_set_defaults(struct drbd_conf *mdev)
3113{ 1862{
3114 /* This way we get a compile error when sync_conf grows, 1863 /* Beware! The actual layout differs
3115 and we forgot to initialize it here */ 1864 * between big endian and little endian */
3116 mdev->sync_conf = (struct syncer_conf) { 1865 mdev->state = (union drbd_dev_state) {
3117 /* .rate = */ DRBD_RATE_DEF,
3118 /* .after = */ DRBD_AFTER_DEF,
3119 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
3120 /* .verify_alg = */ {}, 0,
3121 /* .cpu_mask = */ {}, 0,
3122 /* .csums_alg = */ {}, 0,
3123 /* .use_rle = */ 0,
3124 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
3125 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
3126 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
3127 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
3128 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
3129 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
3130 };
3131
3132 /* Have to use that way, because the layout differs between
3133 big endian and little endian */
3134 mdev->state = (union drbd_state) {
3135 { .role = R_SECONDARY, 1866 { .role = R_SECONDARY,
3136 .peer = R_UNKNOWN, 1867 .peer = R_UNKNOWN,
3137 .conn = C_STANDALONE, 1868 .conn = C_STANDALONE,
3138 .disk = D_DISKLESS, 1869 .disk = D_DISKLESS,
3139 .pdsk = D_UNKNOWN, 1870 .pdsk = D_UNKNOWN,
3140 .susp = 0,
3141 .susp_nod = 0,
3142 .susp_fen = 0
3143 } }; 1871 } };
3144} 1872}
3145 1873
@@ -3155,28 +1883,17 @@ void drbd_init_set_defaults(struct drbd_conf *mdev)
3155 atomic_set(&mdev->rs_pending_cnt, 0); 1883 atomic_set(&mdev->rs_pending_cnt, 0);
3156 atomic_set(&mdev->unacked_cnt, 0); 1884 atomic_set(&mdev->unacked_cnt, 0);
3157 atomic_set(&mdev->local_cnt, 0); 1885 atomic_set(&mdev->local_cnt, 0);
3158 atomic_set(&mdev->net_cnt, 0);
3159 atomic_set(&mdev->packet_seq, 0);
3160 atomic_set(&mdev->pp_in_use, 0);
3161 atomic_set(&mdev->pp_in_use_by_net, 0); 1886 atomic_set(&mdev->pp_in_use_by_net, 0);
3162 atomic_set(&mdev->rs_sect_in, 0); 1887 atomic_set(&mdev->rs_sect_in, 0);
3163 atomic_set(&mdev->rs_sect_ev, 0); 1888 atomic_set(&mdev->rs_sect_ev, 0);
3164 atomic_set(&mdev->ap_in_flight, 0); 1889 atomic_set(&mdev->ap_in_flight, 0);
3165 atomic_set(&mdev->md_io_in_use, 0); 1890 atomic_set(&mdev->md_io_in_use, 0);
3166 1891
3167 mutex_init(&mdev->data.mutex); 1892 mutex_init(&mdev->own_state_mutex);
3168 mutex_init(&mdev->meta.mutex); 1893 mdev->state_mutex = &mdev->own_state_mutex;
3169 sema_init(&mdev->data.work.s, 0);
3170 sema_init(&mdev->meta.work.s, 0);
3171 mutex_init(&mdev->state_mutex);
3172
3173 spin_lock_init(&mdev->data.work.q_lock);
3174 spin_lock_init(&mdev->meta.work.q_lock);
3175 1894
3176 spin_lock_init(&mdev->al_lock); 1895 spin_lock_init(&mdev->al_lock);
3177 spin_lock_init(&mdev->req_lock);
3178 spin_lock_init(&mdev->peer_seq_lock); 1896 spin_lock_init(&mdev->peer_seq_lock);
3179 spin_lock_init(&mdev->epoch_lock);
3180 1897
3181 INIT_LIST_HEAD(&mdev->active_ee); 1898 INIT_LIST_HEAD(&mdev->active_ee);
3182 INIT_LIST_HEAD(&mdev->sync_ee); 1899 INIT_LIST_HEAD(&mdev->sync_ee);
@@ -3184,8 +1901,6 @@ void drbd_init_set_defaults(struct drbd_conf *mdev)
3184 INIT_LIST_HEAD(&mdev->read_ee); 1901 INIT_LIST_HEAD(&mdev->read_ee);
3185 INIT_LIST_HEAD(&mdev->net_ee); 1902 INIT_LIST_HEAD(&mdev->net_ee);
3186 INIT_LIST_HEAD(&mdev->resync_reads); 1903 INIT_LIST_HEAD(&mdev->resync_reads);
3187 INIT_LIST_HEAD(&mdev->data.work.q);
3188 INIT_LIST_HEAD(&mdev->meta.work.q);
3189 INIT_LIST_HEAD(&mdev->resync_work.list); 1904 INIT_LIST_HEAD(&mdev->resync_work.list);
3190 INIT_LIST_HEAD(&mdev->unplug_work.list); 1905 INIT_LIST_HEAD(&mdev->unplug_work.list);
3191 INIT_LIST_HEAD(&mdev->go_diskless.list); 1906 INIT_LIST_HEAD(&mdev->go_diskless.list);
@@ -3199,6 +1914,14 @@ void drbd_init_set_defaults(struct drbd_conf *mdev)
3199 mdev->md_sync_work.cb = w_md_sync; 1914 mdev->md_sync_work.cb = w_md_sync;
3200 mdev->bm_io_work.w.cb = w_bitmap_io; 1915 mdev->bm_io_work.w.cb = w_bitmap_io;
3201 mdev->start_resync_work.cb = w_start_resync; 1916 mdev->start_resync_work.cb = w_start_resync;
1917
1918 mdev->resync_work.mdev = mdev;
1919 mdev->unplug_work.mdev = mdev;
1920 mdev->go_diskless.mdev = mdev;
1921 mdev->md_sync_work.mdev = mdev;
1922 mdev->bm_io_work.w.mdev = mdev;
1923 mdev->start_resync_work.mdev = mdev;
1924
3202 init_timer(&mdev->resync_timer); 1925 init_timer(&mdev->resync_timer);
3203 init_timer(&mdev->md_sync_timer); 1926 init_timer(&mdev->md_sync_timer);
3204 init_timer(&mdev->start_resync_timer); 1927 init_timer(&mdev->start_resync_timer);
@@ -3214,17 +1937,10 @@ void drbd_init_set_defaults(struct drbd_conf *mdev)
3214 1937
3215 init_waitqueue_head(&mdev->misc_wait); 1938 init_waitqueue_head(&mdev->misc_wait);
3216 init_waitqueue_head(&mdev->state_wait); 1939 init_waitqueue_head(&mdev->state_wait);
3217 init_waitqueue_head(&mdev->net_cnt_wait);
3218 init_waitqueue_head(&mdev->ee_wait); 1940 init_waitqueue_head(&mdev->ee_wait);
3219 init_waitqueue_head(&mdev->al_wait); 1941 init_waitqueue_head(&mdev->al_wait);
3220 init_waitqueue_head(&mdev->seq_wait); 1942 init_waitqueue_head(&mdev->seq_wait);
3221 1943
3222 drbd_thread_init(mdev, &mdev->receiver, drbdd_init);
3223 drbd_thread_init(mdev, &mdev->worker, drbd_worker);
3224 drbd_thread_init(mdev, &mdev->asender, drbd_asender);
3225
3226 mdev->agreed_pro_version = PRO_VERSION_MAX;
3227 mdev->write_ordering = WO_bdev_flush;
3228 mdev->resync_wenr = LC_FREE; 1944 mdev->resync_wenr = LC_FREE;
3229 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; 1945 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
3230 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; 1946 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
@@ -3233,13 +1949,10 @@ void drbd_init_set_defaults(struct drbd_conf *mdev)
3233void drbd_mdev_cleanup(struct drbd_conf *mdev) 1949void drbd_mdev_cleanup(struct drbd_conf *mdev)
3234{ 1950{
3235 int i; 1951 int i;
3236 if (mdev->receiver.t_state != None) 1952 if (mdev->tconn->receiver.t_state != NONE)
3237 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n", 1953 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
3238 mdev->receiver.t_state); 1954 mdev->tconn->receiver.t_state);
3239 1955
3240 /* no need to lock it, I'm the only thread alive */
3241 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
3242 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
3243 mdev->al_writ_cnt = 1956 mdev->al_writ_cnt =
3244 mdev->bm_writ_cnt = 1957 mdev->bm_writ_cnt =
3245 mdev->read_cnt = 1958 mdev->read_cnt =
@@ -3256,7 +1969,7 @@ void drbd_mdev_cleanup(struct drbd_conf *mdev)
3256 mdev->rs_mark_left[i] = 0; 1969 mdev->rs_mark_left[i] = 0;
3257 mdev->rs_mark_time[i] = 0; 1970 mdev->rs_mark_time[i] = 0;
3258 } 1971 }
3259 D_ASSERT(mdev->net_conf == NULL); 1972 D_ASSERT(mdev->tconn->net_conf == NULL);
3260 1973
3261 drbd_set_my_capacity(mdev, 0); 1974 drbd_set_my_capacity(mdev, 0);
3262 if (mdev->bitmap) { 1975 if (mdev->bitmap) {
@@ -3265,21 +1978,18 @@ void drbd_mdev_cleanup(struct drbd_conf *mdev)
3265 drbd_bm_cleanup(mdev); 1978 drbd_bm_cleanup(mdev);
3266 } 1979 }
3267 1980
3268 drbd_free_resources(mdev); 1981 drbd_free_bc(mdev->ldev);
3269 drbd_clear_flag(mdev, AL_SUSPENDED); 1982 mdev->ldev = NULL;
1983
1984 clear_bit(AL_SUSPENDED, &mdev->flags);
3270 1985
3271 /*
3272 * currently we drbd_init_ee only on module load, so
3273 * we may do drbd_release_ee only on module unload!
3274 */
3275 D_ASSERT(list_empty(&mdev->active_ee)); 1986 D_ASSERT(list_empty(&mdev->active_ee));
3276 D_ASSERT(list_empty(&mdev->sync_ee)); 1987 D_ASSERT(list_empty(&mdev->sync_ee));
3277 D_ASSERT(list_empty(&mdev->done_ee)); 1988 D_ASSERT(list_empty(&mdev->done_ee));
3278 D_ASSERT(list_empty(&mdev->read_ee)); 1989 D_ASSERT(list_empty(&mdev->read_ee));
3279 D_ASSERT(list_empty(&mdev->net_ee)); 1990 D_ASSERT(list_empty(&mdev->net_ee));
3280 D_ASSERT(list_empty(&mdev->resync_reads)); 1991 D_ASSERT(list_empty(&mdev->resync_reads));
3281 D_ASSERT(list_empty(&mdev->data.work.q)); 1992 D_ASSERT(list_empty(&mdev->tconn->sender_work.q));
3282 D_ASSERT(list_empty(&mdev->meta.work.q));
3283 D_ASSERT(list_empty(&mdev->resync_work.list)); 1993 D_ASSERT(list_empty(&mdev->resync_work.list));
3284 D_ASSERT(list_empty(&mdev->unplug_work.list)); 1994 D_ASSERT(list_empty(&mdev->unplug_work.list));
3285 D_ASSERT(list_empty(&mdev->go_diskless.list)); 1995 D_ASSERT(list_empty(&mdev->go_diskless.list));
@@ -3353,7 +2063,7 @@ static int drbd_create_mempools(void)
3353 goto Enomem; 2063 goto Enomem;
3354 2064
3355 drbd_ee_cache = kmem_cache_create( 2065 drbd_ee_cache = kmem_cache_create(
3356 "drbd_ee", sizeof(struct drbd_epoch_entry), 0, 0, NULL); 2066 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
3357 if (drbd_ee_cache == NULL) 2067 if (drbd_ee_cache == NULL)
3358 goto Enomem; 2068 goto Enomem;
3359 2069
@@ -3368,11 +2078,9 @@ static int drbd_create_mempools(void)
3368 goto Enomem; 2078 goto Enomem;
3369 2079
3370 /* mempools */ 2080 /* mempools */
3371#ifdef COMPAT_HAVE_BIOSET_CREATE
3372 drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0); 2081 drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0);
3373 if (drbd_md_io_bio_set == NULL) 2082 if (drbd_md_io_bio_set == NULL)
3374 goto Enomem; 2083 goto Enomem;
3375#endif
3376 2084
3377 drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0); 2085 drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
3378 if (drbd_md_io_page_pool == NULL) 2086 if (drbd_md_io_page_pool == NULL)
@@ -3421,73 +2129,53 @@ static struct notifier_block drbd_notifier = {
3421 .notifier_call = drbd_notify_sys, 2129 .notifier_call = drbd_notify_sys,
3422}; 2130};
3423 2131
3424static void drbd_release_ee_lists(struct drbd_conf *mdev) 2132static void drbd_release_all_peer_reqs(struct drbd_conf *mdev)
3425{ 2133{
3426 int rr; 2134 int rr;
3427 2135
3428 rr = drbd_release_ee(mdev, &mdev->active_ee); 2136 rr = drbd_free_peer_reqs(mdev, &mdev->active_ee);
3429 if (rr) 2137 if (rr)
3430 dev_err(DEV, "%d EEs in active list found!\n", rr); 2138 dev_err(DEV, "%d EEs in active list found!\n", rr);
3431 2139
3432 rr = drbd_release_ee(mdev, &mdev->sync_ee); 2140 rr = drbd_free_peer_reqs(mdev, &mdev->sync_ee);
3433 if (rr) 2141 if (rr)
3434 dev_err(DEV, "%d EEs in sync list found!\n", rr); 2142 dev_err(DEV, "%d EEs in sync list found!\n", rr);
3435 2143
3436 rr = drbd_release_ee(mdev, &mdev->read_ee); 2144 rr = drbd_free_peer_reqs(mdev, &mdev->read_ee);
3437 if (rr) 2145 if (rr)
3438 dev_err(DEV, "%d EEs in read list found!\n", rr); 2146 dev_err(DEV, "%d EEs in read list found!\n", rr);
3439 2147
3440 rr = drbd_release_ee(mdev, &mdev->done_ee); 2148 rr = drbd_free_peer_reqs(mdev, &mdev->done_ee);
3441 if (rr) 2149 if (rr)
3442 dev_err(DEV, "%d EEs in done list found!\n", rr); 2150 dev_err(DEV, "%d EEs in done list found!\n", rr);
3443 2151
3444 rr = drbd_release_ee(mdev, &mdev->net_ee); 2152 rr = drbd_free_peer_reqs(mdev, &mdev->net_ee);
3445 if (rr) 2153 if (rr)
3446 dev_err(DEV, "%d EEs in net list found!\n", rr); 2154 dev_err(DEV, "%d EEs in net list found!\n", rr);
3447} 2155}
3448 2156
3449/* caution. no locking. 2157/* caution. no locking. */
3450 * currently only used from module cleanup code. */ 2158void drbd_minor_destroy(struct kref *kref)
3451static void drbd_delete_device(unsigned int minor)
3452{ 2159{
3453 struct drbd_conf *mdev = minor_to_mdev(minor); 2160 struct drbd_conf *mdev = container_of(kref, struct drbd_conf, kref);
3454 2161 struct drbd_tconn *tconn = mdev->tconn;
3455 if (!mdev)
3456 return;
3457 2162
3458 del_timer_sync(&mdev->request_timer); 2163 del_timer_sync(&mdev->request_timer);
3459 2164
3460 /* paranoia asserts */ 2165 /* paranoia asserts */
3461 if (mdev->open_cnt != 0) 2166 D_ASSERT(mdev->open_cnt == 0);
3462 dev_err(DEV, "open_cnt = %d in %s:%u", mdev->open_cnt,
3463 __FILE__ , __LINE__);
3464
3465 ERR_IF (!list_empty(&mdev->data.work.q)) {
3466 struct list_head *lp;
3467 list_for_each(lp, &mdev->data.work.q) {
3468 dev_err(DEV, "lp = %p\n", lp);
3469 }
3470 };
3471 /* end paranoia asserts */ 2167 /* end paranoia asserts */
3472 2168
3473 del_gendisk(mdev->vdisk);
3474
3475 /* cleanup stuff that may have been allocated during 2169 /* cleanup stuff that may have been allocated during
3476 * device (re-)configuration or state changes */ 2170 * device (re-)configuration or state changes */
3477 2171
3478 if (mdev->this_bdev) 2172 if (mdev->this_bdev)
3479 bdput(mdev->this_bdev); 2173 bdput(mdev->this_bdev);
3480 2174
3481 drbd_free_resources(mdev); 2175 drbd_free_bc(mdev->ldev);
2176 mdev->ldev = NULL;
3482 2177
3483 drbd_release_ee_lists(mdev); 2178 drbd_release_all_peer_reqs(mdev);
3484
3485 /* should be freed on disconnect? */
3486 kfree(mdev->ee_hash);
3487 /*
3488 mdev->ee_hash_s = 0;
3489 mdev->ee_hash = NULL;
3490 */
3491 2179
3492 lc_destroy(mdev->act_log); 2180 lc_destroy(mdev->act_log);
3493 lc_destroy(mdev->resync); 2181 lc_destroy(mdev->resync);
@@ -3495,19 +2183,101 @@ static void drbd_delete_device(unsigned int minor)
3495 kfree(mdev->p_uuid); 2183 kfree(mdev->p_uuid);
3496 /* mdev->p_uuid = NULL; */ 2184 /* mdev->p_uuid = NULL; */
3497 2185
3498 kfree(mdev->int_dig_out); 2186 if (mdev->bitmap) /* should no longer be there. */
3499 kfree(mdev->int_dig_in); 2187 drbd_bm_cleanup(mdev);
3500 kfree(mdev->int_dig_vv); 2188 __free_page(mdev->md_io_page);
2189 put_disk(mdev->vdisk);
2190 blk_cleanup_queue(mdev->rq_queue);
2191 kfree(mdev->rs_plan_s);
2192 kfree(mdev);
3501 2193
3502 /* cleanup the rest that has been 2194 kref_put(&tconn->kref, &conn_destroy);
3503 * allocated from drbd_new_device
3504 * and actually free the mdev itself */
3505 drbd_free_mdev(mdev);
3506} 2195}
3507 2196
2197/* One global retry thread, if we need to push back some bio and have it
2198 * reinserted through our make request function.
2199 */
2200static struct retry_worker {
2201 struct workqueue_struct *wq;
2202 struct work_struct worker;
2203
2204 spinlock_t lock;
2205 struct list_head writes;
2206} retry;
2207
2208static void do_retry(struct work_struct *ws)
2209{
2210 struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
2211 LIST_HEAD(writes);
2212 struct drbd_request *req, *tmp;
2213
2214 spin_lock_irq(&retry->lock);
2215 list_splice_init(&retry->writes, &writes);
2216 spin_unlock_irq(&retry->lock);
2217
2218 list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
2219 struct drbd_conf *mdev = req->w.mdev;
2220 struct bio *bio = req->master_bio;
2221 unsigned long start_time = req->start_time;
2222 bool expected;
2223
2224 expected =
2225 expect(atomic_read(&req->completion_ref) == 0) &&
2226 expect(req->rq_state & RQ_POSTPONED) &&
2227 expect((req->rq_state & RQ_LOCAL_PENDING) == 0 ||
2228 (req->rq_state & RQ_LOCAL_ABORTED) != 0);
2229
2230 if (!expected)
2231 dev_err(DEV, "req=%p completion_ref=%d rq_state=%x\n",
2232 req, atomic_read(&req->completion_ref),
2233 req->rq_state);
2234
2235 /* We still need to put one kref associated with the
2236 * "completion_ref" going zero in the code path that queued it
2237 * here. The request object may still be referenced by a
2238 * frozen local req->private_bio, in case we force-detached.
2239 */
2240 kref_put(&req->kref, drbd_req_destroy);
2241
2242 /* A single suspended or otherwise blocking device may stall
2243 * all others as well. Fortunately, this code path is to
2244 * recover from a situation that "should not happen":
2245 * concurrent writes in multi-primary setup.
2246 * In a "normal" lifecycle, this workqueue is supposed to be
2247 * destroyed without ever doing anything.
2248 * If it turns out to be an issue anyways, we can do per
2249 * resource (replication group) or per device (minor) retry
2250 * workqueues instead.
2251 */
2252
2253 /* We are not just doing generic_make_request(),
2254 * as we want to keep the start_time information. */
2255 inc_ap_bio(mdev);
2256 __drbd_make_request(mdev, bio, start_time);
2257 }
2258}
2259
2260void drbd_restart_request(struct drbd_request *req)
2261{
2262 unsigned long flags;
2263 spin_lock_irqsave(&retry.lock, flags);
2264 list_move_tail(&req->tl_requests, &retry.writes);
2265 spin_unlock_irqrestore(&retry.lock, flags);
2266
2267 /* Drop the extra reference that would otherwise
2268 * have been dropped by complete_master_bio.
2269 * do_retry() needs to grab a new one. */
2270 dec_ap_bio(req->w.mdev);
2271
2272 queue_work(retry.wq, &retry.worker);
2273}
2274
2275
3508static void drbd_cleanup(void) 2276static void drbd_cleanup(void)
3509{ 2277{
3510 unsigned int i; 2278 unsigned int i;
2279 struct drbd_conf *mdev;
2280 struct drbd_tconn *tconn, *tmp;
3511 2281
3512 unregister_reboot_notifier(&drbd_notifier); 2282 unregister_reboot_notifier(&drbd_notifier);
3513 2283
@@ -3522,19 +2292,31 @@ static void drbd_cleanup(void)
3522 if (drbd_proc) 2292 if (drbd_proc)
3523 remove_proc_entry("drbd", NULL); 2293 remove_proc_entry("drbd", NULL);
3524 2294
3525 drbd_nl_cleanup(); 2295 if (retry.wq)
2296 destroy_workqueue(retry.wq);
2297
2298 drbd_genl_unregister();
3526 2299
3527 if (minor_table) { 2300 idr_for_each_entry(&minors, mdev, i) {
3528 i = minor_count; 2301 idr_remove(&minors, mdev_to_minor(mdev));
3529 while (i--) 2302 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3530 drbd_delete_device(i); 2303 del_gendisk(mdev->vdisk);
3531 drbd_destroy_mempools(); 2304 /* synchronize_rcu(); No other threads running at this point */
2305 kref_put(&mdev->kref, &drbd_minor_destroy);
3532 } 2306 }
3533 2307
3534 kfree(minor_table); 2308 /* not _rcu since, no other updater anymore. Genl already unregistered */
2309 list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2310 list_del(&tconn->all_tconn); /* not _rcu no proc, not other threads */
2311 /* synchronize_rcu(); */
2312 kref_put(&tconn->kref, &conn_destroy);
2313 }
3535 2314
2315 drbd_destroy_mempools();
3536 unregister_blkdev(DRBD_MAJOR, "drbd"); 2316 unregister_blkdev(DRBD_MAJOR, "drbd");
3537 2317
2318 idr_destroy(&minors);
2319
3538 printk(KERN_INFO "drbd: module cleanup done.\n"); 2320 printk(KERN_INFO "drbd: module cleanup done.\n");
3539} 2321}
3540 2322
@@ -3559,7 +2341,7 @@ static int drbd_congested(void *congested_data, int bdi_bits)
3559 goto out; 2341 goto out;
3560 } 2342 }
3561 2343
3562 if (drbd_test_flag(mdev, CALLBACK_PENDING)) { 2344 if (test_bit(CALLBACK_PENDING, &mdev->tconn->flags)) {
3563 r |= (1 << BDI_async_congested); 2345 r |= (1 << BDI_async_congested);
3564 /* Without good local data, we would need to read from remote, 2346 /* Without good local data, we would need to read from remote,
3565 * and that would need the worker thread as well, which is 2347 * and that would need the worker thread as well, which is
@@ -3583,7 +2365,7 @@ static int drbd_congested(void *congested_data, int bdi_bits)
3583 reason = 'b'; 2365 reason = 'b';
3584 } 2366 }
3585 2367
3586 if (bdi_bits & (1 << BDI_async_congested) && drbd_test_flag(mdev, NET_CONGESTED)) { 2368 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
3587 r |= (1 << BDI_async_congested); 2369 r |= (1 << BDI_async_congested);
3588 reason = reason == 'b' ? 'a' : 'n'; 2370 reason = reason == 'b' ? 'a' : 'n';
3589 } 2371 }
@@ -3593,20 +2375,243 @@ out:
3593 return r; 2375 return r;
3594} 2376}
3595 2377
3596struct drbd_conf *drbd_new_device(unsigned int minor) 2378static void drbd_init_workqueue(struct drbd_work_queue* wq)
2379{
2380 spin_lock_init(&wq->q_lock);
2381 INIT_LIST_HEAD(&wq->q);
2382 init_waitqueue_head(&wq->q_wait);
2383}
2384
2385struct drbd_tconn *conn_get_by_name(const char *name)
2386{
2387 struct drbd_tconn *tconn;
2388
2389 if (!name || !name[0])
2390 return NULL;
2391
2392 rcu_read_lock();
2393 list_for_each_entry_rcu(tconn, &drbd_tconns, all_tconn) {
2394 if (!strcmp(tconn->name, name)) {
2395 kref_get(&tconn->kref);
2396 goto found;
2397 }
2398 }
2399 tconn = NULL;
2400found:
2401 rcu_read_unlock();
2402 return tconn;
2403}
2404
2405struct drbd_tconn *conn_get_by_addrs(void *my_addr, int my_addr_len,
2406 void *peer_addr, int peer_addr_len)
2407{
2408 struct drbd_tconn *tconn;
2409
2410 rcu_read_lock();
2411 list_for_each_entry_rcu(tconn, &drbd_tconns, all_tconn) {
2412 if (tconn->my_addr_len == my_addr_len &&
2413 tconn->peer_addr_len == peer_addr_len &&
2414 !memcmp(&tconn->my_addr, my_addr, my_addr_len) &&
2415 !memcmp(&tconn->peer_addr, peer_addr, peer_addr_len)) {
2416 kref_get(&tconn->kref);
2417 goto found;
2418 }
2419 }
2420 tconn = NULL;
2421found:
2422 rcu_read_unlock();
2423 return tconn;
2424}
2425
2426static int drbd_alloc_socket(struct drbd_socket *socket)
2427{
2428 socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
2429 if (!socket->rbuf)
2430 return -ENOMEM;
2431 socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
2432 if (!socket->sbuf)
2433 return -ENOMEM;
2434 return 0;
2435}
2436
2437static void drbd_free_socket(struct drbd_socket *socket)
2438{
2439 free_page((unsigned long) socket->sbuf);
2440 free_page((unsigned long) socket->rbuf);
2441}
2442
2443void conn_free_crypto(struct drbd_tconn *tconn)
2444{
2445 drbd_free_sock(tconn);
2446
2447 crypto_free_hash(tconn->csums_tfm);
2448 crypto_free_hash(tconn->verify_tfm);
2449 crypto_free_hash(tconn->cram_hmac_tfm);
2450 crypto_free_hash(tconn->integrity_tfm);
2451 crypto_free_hash(tconn->peer_integrity_tfm);
2452 kfree(tconn->int_dig_in);
2453 kfree(tconn->int_dig_vv);
2454
2455 tconn->csums_tfm = NULL;
2456 tconn->verify_tfm = NULL;
2457 tconn->cram_hmac_tfm = NULL;
2458 tconn->integrity_tfm = NULL;
2459 tconn->peer_integrity_tfm = NULL;
2460 tconn->int_dig_in = NULL;
2461 tconn->int_dig_vv = NULL;
2462}
2463
2464int set_resource_options(struct drbd_tconn *tconn, struct res_opts *res_opts)
2465{
2466 cpumask_var_t new_cpu_mask;
2467 int err;
2468
2469 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
2470 return -ENOMEM;
2471 /*
2472 retcode = ERR_NOMEM;
2473 drbd_msg_put_info("unable to allocate cpumask");
2474 */
2475
2476 /* silently ignore cpu mask on UP kernel */
2477 if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
2478 /* FIXME: Get rid of constant 32 here */
2479 err = bitmap_parse(res_opts->cpu_mask, 32,
2480 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2481 if (err) {
2482 conn_warn(tconn, "bitmap_parse() failed with %d\n", err);
2483 /* retcode = ERR_CPU_MASK_PARSE; */
2484 goto fail;
2485 }
2486 }
2487 tconn->res_opts = *res_opts;
2488 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2489 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2490 drbd_calc_cpu_mask(tconn);
2491 tconn->receiver.reset_cpu_mask = 1;
2492 tconn->asender.reset_cpu_mask = 1;
2493 tconn->worker.reset_cpu_mask = 1;
2494 }
2495 err = 0;
2496
2497fail:
2498 free_cpumask_var(new_cpu_mask);
2499 return err;
2500
2501}
2502
2503/* caller must be under genl_lock() */
2504struct drbd_tconn *conn_create(const char *name, struct res_opts *res_opts)
2505{
2506 struct drbd_tconn *tconn;
2507
2508 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2509 if (!tconn)
2510 return NULL;
2511
2512 tconn->name = kstrdup(name, GFP_KERNEL);
2513 if (!tconn->name)
2514 goto fail;
2515
2516 if (drbd_alloc_socket(&tconn->data))
2517 goto fail;
2518 if (drbd_alloc_socket(&tconn->meta))
2519 goto fail;
2520
2521 if (!zalloc_cpumask_var(&tconn->cpu_mask, GFP_KERNEL))
2522 goto fail;
2523
2524 if (set_resource_options(tconn, res_opts))
2525 goto fail;
2526
2527 tconn->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2528 if (!tconn->current_epoch)
2529 goto fail;
2530
2531 INIT_LIST_HEAD(&tconn->transfer_log);
2532
2533 INIT_LIST_HEAD(&tconn->current_epoch->list);
2534 tconn->epochs = 1;
2535 spin_lock_init(&tconn->epoch_lock);
2536 tconn->write_ordering = WO_bdev_flush;
2537
2538 tconn->send.seen_any_write_yet = false;
2539 tconn->send.current_epoch_nr = 0;
2540 tconn->send.current_epoch_writes = 0;
2541
2542 tconn->cstate = C_STANDALONE;
2543 mutex_init(&tconn->cstate_mutex);
2544 spin_lock_init(&tconn->req_lock);
2545 mutex_init(&tconn->conf_update);
2546 init_waitqueue_head(&tconn->ping_wait);
2547 idr_init(&tconn->volumes);
2548
2549 drbd_init_workqueue(&tconn->sender_work);
2550 mutex_init(&tconn->data.mutex);
2551 mutex_init(&tconn->meta.mutex);
2552
2553 drbd_thread_init(tconn, &tconn->receiver, drbdd_init, "receiver");
2554 drbd_thread_init(tconn, &tconn->worker, drbd_worker, "worker");
2555 drbd_thread_init(tconn, &tconn->asender, drbd_asender, "asender");
2556
2557 kref_init(&tconn->kref);
2558 list_add_tail_rcu(&tconn->all_tconn, &drbd_tconns);
2559
2560 return tconn;
2561
2562fail:
2563 kfree(tconn->current_epoch);
2564 free_cpumask_var(tconn->cpu_mask);
2565 drbd_free_socket(&tconn->meta);
2566 drbd_free_socket(&tconn->data);
2567 kfree(tconn->name);
2568 kfree(tconn);
2569
2570 return NULL;
2571}
2572
2573void conn_destroy(struct kref *kref)
2574{
2575 struct drbd_tconn *tconn = container_of(kref, struct drbd_tconn, kref);
2576
2577 if (atomic_read(&tconn->current_epoch->epoch_size) != 0)
2578 conn_err(tconn, "epoch_size:%d\n", atomic_read(&tconn->current_epoch->epoch_size));
2579 kfree(tconn->current_epoch);
2580
2581 idr_destroy(&tconn->volumes);
2582
2583 free_cpumask_var(tconn->cpu_mask);
2584 drbd_free_socket(&tconn->meta);
2585 drbd_free_socket(&tconn->data);
2586 kfree(tconn->name);
2587 kfree(tconn->int_dig_in);
2588 kfree(tconn->int_dig_vv);
2589 kfree(tconn);
2590}
2591
2592enum drbd_ret_code conn_new_minor(struct drbd_tconn *tconn, unsigned int minor, int vnr)
3597{ 2593{
3598 struct drbd_conf *mdev; 2594 struct drbd_conf *mdev;
3599 struct gendisk *disk; 2595 struct gendisk *disk;
3600 struct request_queue *q; 2596 struct request_queue *q;
2597 int vnr_got = vnr;
2598 int minor_got = minor;
2599 enum drbd_ret_code err = ERR_NOMEM;
2600
2601 mdev = minor_to_mdev(minor);
2602 if (mdev)
2603 return ERR_MINOR_EXISTS;
3601 2604
3602 /* GFP_KERNEL, we are outside of all write-out paths */ 2605 /* GFP_KERNEL, we are outside of all write-out paths */
3603 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL); 2606 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
3604 if (!mdev) 2607 if (!mdev)
3605 return NULL; 2608 return ERR_NOMEM;
3606 if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL)) 2609
3607 goto out_no_cpumask; 2610 kref_get(&tconn->kref);
2611 mdev->tconn = tconn;
3608 2612
3609 mdev->minor = minor; 2613 mdev->minor = minor;
2614 mdev->vnr = vnr;
3610 2615
3611 drbd_init_set_defaults(mdev); 2616 drbd_init_set_defaults(mdev);
3612 2617
@@ -3644,7 +2649,7 @@ struct drbd_conf *drbd_new_device(unsigned int minor)
3644 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8); 2649 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
3645 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY); 2650 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
3646 blk_queue_merge_bvec(q, drbd_merge_bvec); 2651 blk_queue_merge_bvec(q, drbd_merge_bvec);
3647 q->queue_lock = &mdev->req_lock; 2652 q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
3648 2653
3649 mdev->md_io_page = alloc_page(GFP_KERNEL); 2654 mdev->md_io_page = alloc_page(GFP_KERNEL);
3650 if (!mdev->md_io_page) 2655 if (!mdev->md_io_page)
@@ -3652,30 +2657,44 @@ struct drbd_conf *drbd_new_device(unsigned int minor)
3652 2657
3653 if (drbd_bm_init(mdev)) 2658 if (drbd_bm_init(mdev))
3654 goto out_no_bitmap; 2659 goto out_no_bitmap;
3655 /* no need to lock access, we are still initializing this minor device. */ 2660 mdev->read_requests = RB_ROOT;
3656 if (!tl_init(mdev)) 2661 mdev->write_requests = RB_ROOT;
3657 goto out_no_tl; 2662
3658 2663 if (!idr_pre_get(&minors, GFP_KERNEL))
3659 mdev->app_reads_hash = kzalloc(APP_R_HSIZE*sizeof(void *), GFP_KERNEL); 2664 goto out_no_minor_idr;
3660 if (!mdev->app_reads_hash) 2665 if (idr_get_new_above(&minors, mdev, minor, &minor_got))
3661 goto out_no_app_reads; 2666 goto out_no_minor_idr;
3662 2667 if (minor_got != minor) {
3663 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL); 2668 err = ERR_MINOR_EXISTS;
3664 if (!mdev->current_epoch) 2669 drbd_msg_put_info("requested minor exists already");
3665 goto out_no_epoch; 2670 goto out_idr_remove_minor;
3666 2671 }
3667 INIT_LIST_HEAD(&mdev->current_epoch->list); 2672
3668 mdev->epochs = 1; 2673 if (!idr_pre_get(&tconn->volumes, GFP_KERNEL))
3669 2674 goto out_idr_remove_minor;
3670 return mdev; 2675 if (idr_get_new_above(&tconn->volumes, mdev, vnr, &vnr_got))
3671 2676 goto out_idr_remove_minor;
3672/* out_whatever_else: 2677 if (vnr_got != vnr) {
3673 kfree(mdev->current_epoch); */ 2678 err = ERR_INVALID_REQUEST;
3674out_no_epoch: 2679 drbd_msg_put_info("requested volume exists already");
3675 kfree(mdev->app_reads_hash); 2680 goto out_idr_remove_vol;
3676out_no_app_reads: 2681 }
3677 tl_cleanup(mdev); 2682 add_disk(disk);
3678out_no_tl: 2683 kref_init(&mdev->kref); /* one ref for both idrs and the the add_disk */
2684
2685 /* inherit the connection state */
2686 mdev->state.conn = tconn->cstate;
2687 if (mdev->state.conn == C_WF_REPORT_PARAMS)
2688 drbd_connected(mdev);
2689
2690 return NO_ERROR;
2691
2692out_idr_remove_vol:
2693 idr_remove(&tconn->volumes, vnr_got);
2694out_idr_remove_minor:
2695 idr_remove(&minors, minor_got);
2696 synchronize_rcu();
2697out_no_minor_idr:
3679 drbd_bm_cleanup(mdev); 2698 drbd_bm_cleanup(mdev);
3680out_no_bitmap: 2699out_no_bitmap:
3681 __free_page(mdev->md_io_page); 2700 __free_page(mdev->md_io_page);
@@ -3684,55 +2703,25 @@ out_no_io_page:
3684out_no_disk: 2703out_no_disk:
3685 blk_cleanup_queue(q); 2704 blk_cleanup_queue(q);
3686out_no_q: 2705out_no_q:
3687 free_cpumask_var(mdev->cpu_mask);
3688out_no_cpumask:
3689 kfree(mdev);
3690 return NULL;
3691}
3692
3693/* counterpart of drbd_new_device.
3694 * last part of drbd_delete_device. */
3695void drbd_free_mdev(struct drbd_conf *mdev)
3696{
3697 kfree(mdev->current_epoch);
3698 kfree(mdev->app_reads_hash);
3699 tl_cleanup(mdev);
3700 if (mdev->bitmap) /* should no longer be there. */
3701 drbd_bm_cleanup(mdev);
3702 __free_page(mdev->md_io_page);
3703 put_disk(mdev->vdisk);
3704 blk_cleanup_queue(mdev->rq_queue);
3705 free_cpumask_var(mdev->cpu_mask);
3706 drbd_free_tl_hash(mdev);
3707 kfree(mdev); 2706 kfree(mdev);
2707 kref_put(&tconn->kref, &conn_destroy);
2708 return err;
3708} 2709}
3709 2710
3710
3711int __init drbd_init(void) 2711int __init drbd_init(void)
3712{ 2712{
3713 int err; 2713 int err;
3714 2714
3715 if (sizeof(struct p_handshake) != 80) {
3716 printk(KERN_ERR
3717 "drbd: never change the size or layout "
3718 "of the HandShake packet.\n");
3719 return -EINVAL;
3720 }
3721
3722 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) { 2715 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
3723 printk(KERN_ERR 2716 printk(KERN_ERR
3724 "drbd: invalid minor_count (%d)\n", minor_count); 2717 "drbd: invalid minor_count (%d)\n", minor_count);
3725#ifdef MODULE 2718#ifdef MODULE
3726 return -EINVAL; 2719 return -EINVAL;
3727#else 2720#else
3728 minor_count = 8; 2721 minor_count = DRBD_MINOR_COUNT_DEF;
3729#endif 2722#endif
3730 } 2723 }
3731 2724
3732 err = drbd_nl_init();
3733 if (err)
3734 return err;
3735
3736 err = register_blkdev(DRBD_MAJOR, "drbd"); 2725 err = register_blkdev(DRBD_MAJOR, "drbd");
3737 if (err) { 2726 if (err) {
3738 printk(KERN_ERR 2727 printk(KERN_ERR
@@ -3741,6 +2730,13 @@ int __init drbd_init(void)
3741 return err; 2730 return err;
3742 } 2731 }
3743 2732
2733 err = drbd_genl_register();
2734 if (err) {
2735 printk(KERN_ERR "drbd: unable to register generic netlink family\n");
2736 goto fail;
2737 }
2738
2739
3744 register_reboot_notifier(&drbd_notifier); 2740 register_reboot_notifier(&drbd_notifier);
3745 2741
3746 /* 2742 /*
@@ -3751,22 +2747,29 @@ int __init drbd_init(void)
3751 init_waitqueue_head(&drbd_pp_wait); 2747 init_waitqueue_head(&drbd_pp_wait);
3752 2748
3753 drbd_proc = NULL; /* play safe for drbd_cleanup */ 2749 drbd_proc = NULL; /* play safe for drbd_cleanup */
3754 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count, 2750 idr_init(&minors);
3755 GFP_KERNEL);
3756 if (!minor_table)
3757 goto Enomem;
3758 2751
3759 err = drbd_create_mempools(); 2752 err = drbd_create_mempools();
3760 if (err) 2753 if (err)
3761 goto Enomem; 2754 goto fail;
3762 2755
3763 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL); 2756 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
3764 if (!drbd_proc) { 2757 if (!drbd_proc) {
3765 printk(KERN_ERR "drbd: unable to register proc file\n"); 2758 printk(KERN_ERR "drbd: unable to register proc file\n");
3766 goto Enomem; 2759 goto fail;
3767 } 2760 }
3768 2761
3769 rwlock_init(&global_state_lock); 2762 rwlock_init(&global_state_lock);
2763 INIT_LIST_HEAD(&drbd_tconns);
2764
2765 retry.wq = create_singlethread_workqueue("drbd-reissue");
2766 if (!retry.wq) {
2767 printk(KERN_ERR "drbd: unable to create retry workqueue\n");
2768 goto fail;
2769 }
2770 INIT_WORK(&retry.worker, do_retry);
2771 spin_lock_init(&retry.lock);
2772 INIT_LIST_HEAD(&retry.writes);
3770 2773
3771 printk(KERN_INFO "drbd: initialized. " 2774 printk(KERN_INFO "drbd: initialized. "
3772 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n", 2775 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
@@ -3774,11 +2777,10 @@ int __init drbd_init(void)
3774 printk(KERN_INFO "drbd: %s\n", drbd_buildtag()); 2777 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
3775 printk(KERN_INFO "drbd: registered as block device major %d\n", 2778 printk(KERN_INFO "drbd: registered as block device major %d\n",
3776 DRBD_MAJOR); 2779 DRBD_MAJOR);
3777 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
3778 2780
3779 return 0; /* Success! */ 2781 return 0; /* Success! */
3780 2782
3781Enomem: 2783fail:
3782 drbd_cleanup(); 2784 drbd_cleanup();
3783 if (err == -ENOMEM) 2785 if (err == -ENOMEM)
3784 /* currently always the case */ 2786 /* currently always the case */
@@ -3799,47 +2801,42 @@ void drbd_free_bc(struct drbd_backing_dev *ldev)
3799 kfree(ldev); 2801 kfree(ldev);
3800} 2802}
3801 2803
3802void drbd_free_sock(struct drbd_conf *mdev) 2804void drbd_free_sock(struct drbd_tconn *tconn)
3803{ 2805{
3804 if (mdev->data.socket) { 2806 if (tconn->data.socket) {
3805 mutex_lock(&mdev->data.mutex); 2807 mutex_lock(&tconn->data.mutex);
3806 kernel_sock_shutdown(mdev->data.socket, SHUT_RDWR); 2808 kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
3807 sock_release(mdev->data.socket); 2809 sock_release(tconn->data.socket);
3808 mdev->data.socket = NULL; 2810 tconn->data.socket = NULL;
3809 mutex_unlock(&mdev->data.mutex); 2811 mutex_unlock(&tconn->data.mutex);
3810 } 2812 }
3811 if (mdev->meta.socket) { 2813 if (tconn->meta.socket) {
3812 mutex_lock(&mdev->meta.mutex); 2814 mutex_lock(&tconn->meta.mutex);
3813 kernel_sock_shutdown(mdev->meta.socket, SHUT_RDWR); 2815 kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
3814 sock_release(mdev->meta.socket); 2816 sock_release(tconn->meta.socket);
3815 mdev->meta.socket = NULL; 2817 tconn->meta.socket = NULL;
3816 mutex_unlock(&mdev->meta.mutex); 2818 mutex_unlock(&tconn->meta.mutex);
3817 } 2819 }
3818} 2820}
3819 2821
2822/* meta data management */
3820 2823
3821void drbd_free_resources(struct drbd_conf *mdev) 2824void conn_md_sync(struct drbd_tconn *tconn)
3822{ 2825{
3823 crypto_free_hash(mdev->csums_tfm); 2826 struct drbd_conf *mdev;
3824 mdev->csums_tfm = NULL; 2827 int vnr;
3825 crypto_free_hash(mdev->verify_tfm);
3826 mdev->verify_tfm = NULL;
3827 crypto_free_hash(mdev->cram_hmac_tfm);
3828 mdev->cram_hmac_tfm = NULL;
3829 crypto_free_hash(mdev->integrity_w_tfm);
3830 mdev->integrity_w_tfm = NULL;
3831 crypto_free_hash(mdev->integrity_r_tfm);
3832 mdev->integrity_r_tfm = NULL;
3833
3834 drbd_free_sock(mdev);
3835 2828
3836 __no_warn(local, 2829 rcu_read_lock();
3837 drbd_free_bc(mdev->ldev); 2830 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
3838 mdev->ldev = NULL;); 2831 kref_get(&mdev->kref);
2832 rcu_read_unlock();
2833 drbd_md_sync(mdev);
2834 kref_put(&mdev->kref, &drbd_minor_destroy);
2835 rcu_read_lock();
2836 }
2837 rcu_read_unlock();
3839} 2838}
3840 2839
3841/* meta data management */
3842
3843struct meta_data_on_disk { 2840struct meta_data_on_disk {
3844 u64 la_size; /* last agreed size. */ 2841 u64 la_size; /* last agreed size. */
3845 u64 uuid[UI_SIZE]; /* UUIDs. */ 2842 u64 uuid[UI_SIZE]; /* UUIDs. */
@@ -3850,7 +2847,7 @@ struct meta_data_on_disk {
3850 u32 md_size_sect; 2847 u32 md_size_sect;
3851 u32 al_offset; /* offset to this block */ 2848 u32 al_offset; /* offset to this block */
3852 u32 al_nr_extents; /* important for restoring the AL */ 2849 u32 al_nr_extents; /* important for restoring the AL */
3853 /* `-- act_log->nr_elements <-- sync_conf.al_extents */ 2850 /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
3854 u32 bm_offset; /* offset to the bitmap, from here */ 2851 u32 bm_offset; /* offset to the bitmap, from here */
3855 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */ 2852 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
3856 u32 la_peer_max_bio_size; /* last peer max_bio_size */ 2853 u32 la_peer_max_bio_size; /* last peer max_bio_size */
@@ -3870,7 +2867,7 @@ void drbd_md_sync(struct drbd_conf *mdev)
3870 2867
3871 del_timer(&mdev->md_sync_timer); 2868 del_timer(&mdev->md_sync_timer);
3872 /* timer may be rearmed by drbd_md_mark_dirty() now. */ 2869 /* timer may be rearmed by drbd_md_mark_dirty() now. */
3873 if (!drbd_test_and_clear_flag(mdev, MD_DIRTY)) 2870 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
3874 return; 2871 return;
3875 2872
3876 /* We use here D_FAILED and not D_ATTACHING because we try to write 2873 /* We use here D_FAILED and not D_ATTACHING because we try to write
@@ -3888,7 +2885,7 @@ void drbd_md_sync(struct drbd_conf *mdev)
3888 for (i = UI_CURRENT; i < UI_SIZE; i++) 2885 for (i = UI_CURRENT; i < UI_SIZE; i++)
3889 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]); 2886 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
3890 buffer->flags = cpu_to_be32(mdev->ldev->md.flags); 2887 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
3891 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC); 2888 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
3892 2889
3893 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect); 2890 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
3894 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset); 2891 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
@@ -3902,7 +2899,7 @@ void drbd_md_sync(struct drbd_conf *mdev)
3902 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset); 2899 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
3903 sector = mdev->ldev->md.md_offset; 2900 sector = mdev->ldev->md.md_offset;
3904 2901
3905 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) { 2902 if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
3906 /* this was a try anyways ... */ 2903 /* this was a try anyways ... */
3907 dev_err(DEV, "meta data update failed!\n"); 2904 dev_err(DEV, "meta data update failed!\n");
3908 drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR); 2905 drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
@@ -3923,11 +2920,12 @@ out:
3923 * @bdev: Device from which the meta data should be read in. 2920 * @bdev: Device from which the meta data should be read in.
3924 * 2921 *
3925 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case 2922 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
3926 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID. 2923 * something goes wrong.
3927 */ 2924 */
3928int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev) 2925int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
3929{ 2926{
3930 struct meta_data_on_disk *buffer; 2927 struct meta_data_on_disk *buffer;
2928 u32 magic, flags;
3931 int i, rv = NO_ERROR; 2929 int i, rv = NO_ERROR;
3932 2930
3933 if (!get_ldev_if_state(mdev, D_ATTACHING)) 2931 if (!get_ldev_if_state(mdev, D_ATTACHING))
@@ -3937,7 +2935,7 @@ int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
3937 if (!buffer) 2935 if (!buffer)
3938 goto out; 2936 goto out;
3939 2937
3940 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) { 2938 if (drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
3941 /* NOTE: can't do normal error processing here as this is 2939 /* NOTE: can't do normal error processing here as this is
3942 called BEFORE disk is attached */ 2940 called BEFORE disk is attached */
3943 dev_err(DEV, "Error while reading metadata.\n"); 2941 dev_err(DEV, "Error while reading metadata.\n");
@@ -3945,8 +2943,20 @@ int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
3945 goto err; 2943 goto err;
3946 } 2944 }
3947 2945
3948 if (be32_to_cpu(buffer->magic) != DRBD_MD_MAGIC) { 2946 magic = be32_to_cpu(buffer->magic);
3949 dev_err(DEV, "Error while reading metadata, magic not found.\n"); 2947 flags = be32_to_cpu(buffer->flags);
2948 if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
2949 (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
2950 /* btw: that's Activity Log clean, not "all" clean. */
2951 dev_err(DEV, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
2952 rv = ERR_MD_UNCLEAN;
2953 goto err;
2954 }
2955 if (magic != DRBD_MD_MAGIC_08) {
2956 if (magic == DRBD_MD_MAGIC_07)
2957 dev_err(DEV, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
2958 else
2959 dev_err(DEV, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
3950 rv = ERR_MD_INVALID; 2960 rv = ERR_MD_INVALID;
3951 goto err; 2961 goto err;
3952 } 2962 }
@@ -3980,20 +2990,16 @@ int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
3980 for (i = UI_CURRENT; i < UI_SIZE; i++) 2990 for (i = UI_CURRENT; i < UI_SIZE; i++)
3981 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]); 2991 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
3982 bdev->md.flags = be32_to_cpu(buffer->flags); 2992 bdev->md.flags = be32_to_cpu(buffer->flags);
3983 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
3984 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid); 2993 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
3985 2994
3986 spin_lock_irq(&mdev->req_lock); 2995 spin_lock_irq(&mdev->tconn->req_lock);
3987 if (mdev->state.conn < C_CONNECTED) { 2996 if (mdev->state.conn < C_CONNECTED) {
3988 unsigned int peer; 2997 unsigned int peer;
3989 peer = be32_to_cpu(buffer->la_peer_max_bio_size); 2998 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
3990 peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE); 2999 peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
3991 mdev->peer_max_bio_size = peer; 3000 mdev->peer_max_bio_size = peer;
3992 } 3001 }
3993 spin_unlock_irq(&mdev->req_lock); 3002 spin_unlock_irq(&mdev->tconn->req_lock);
3994
3995 if (mdev->sync_conf.al_extents < 7)
3996 mdev->sync_conf.al_extents = 127;
3997 3003
3998 err: 3004 err:
3999 drbd_md_put_buffer(mdev); 3005 drbd_md_put_buffer(mdev);
@@ -4014,7 +3020,7 @@ int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
4014#ifdef DEBUG 3020#ifdef DEBUG
4015void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func) 3021void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
4016{ 3022{
4017 if (!drbd_test_and_set_flag(mdev, MD_DIRTY)) { 3023 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
4018 mod_timer(&mdev->md_sync_timer, jiffies + HZ); 3024 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
4019 mdev->last_md_mark_dirty.line = line; 3025 mdev->last_md_mark_dirty.line = line;
4020 mdev->last_md_mark_dirty.func = func; 3026 mdev->last_md_mark_dirty.func = func;
@@ -4023,7 +3029,7 @@ void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *
4023#else 3029#else
4024void drbd_md_mark_dirty(struct drbd_conf *mdev) 3030void drbd_md_mark_dirty(struct drbd_conf *mdev)
4025{ 3031{
4026 if (!drbd_test_and_set_flag(mdev, MD_DIRTY)) 3032 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
4027 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ); 3033 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
4028} 3034}
4029#endif 3035#endif
@@ -4171,9 +3177,10 @@ int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
4171 return rv; 3177 return rv;
4172} 3178}
4173 3179
4174static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused) 3180static int w_bitmap_io(struct drbd_work *w, int unused)
4175{ 3181{
4176 struct bm_io_work *work = container_of(w, struct bm_io_work, w); 3182 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
3183 struct drbd_conf *mdev = w->mdev;
4177 int rv = -EIO; 3184 int rv = -EIO;
4178 3185
4179 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0); 3186 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
@@ -4185,18 +3192,17 @@ static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
4185 put_ldev(mdev); 3192 put_ldev(mdev);
4186 } 3193 }
4187 3194
4188 drbd_clear_flag(mdev, BITMAP_IO); 3195 clear_bit_unlock(BITMAP_IO, &mdev->flags);
4189 smp_mb__after_clear_bit();
4190 wake_up(&mdev->misc_wait); 3196 wake_up(&mdev->misc_wait);
4191 3197
4192 if (work->done) 3198 if (work->done)
4193 work->done(mdev, rv); 3199 work->done(mdev, rv);
4194 3200
4195 drbd_clear_flag(mdev, BITMAP_IO_QUEUED); 3201 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
4196 work->why = NULL; 3202 work->why = NULL;
4197 work->flags = 0; 3203 work->flags = 0;
4198 3204
4199 return 1; 3205 return 0;
4200} 3206}
4201 3207
4202void drbd_ldev_destroy(struct drbd_conf *mdev) 3208void drbd_ldev_destroy(struct drbd_conf *mdev)
@@ -4209,15 +3215,13 @@ void drbd_ldev_destroy(struct drbd_conf *mdev)
4209 drbd_free_bc(mdev->ldev); 3215 drbd_free_bc(mdev->ldev);
4210 mdev->ldev = NULL;); 3216 mdev->ldev = NULL;);
4211 3217
4212 if (mdev->md_io_tmpp) { 3218 clear_bit(GO_DISKLESS, &mdev->flags);
4213 __free_page(mdev->md_io_tmpp);
4214 mdev->md_io_tmpp = NULL;
4215 }
4216 drbd_clear_flag(mdev, GO_DISKLESS);
4217} 3219}
4218 3220
4219static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused) 3221static int w_go_diskless(struct drbd_work *w, int unused)
4220{ 3222{
3223 struct drbd_conf *mdev = w->mdev;
3224
4221 D_ASSERT(mdev->state.disk == D_FAILED); 3225 D_ASSERT(mdev->state.disk == D_FAILED);
4222 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will 3226 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
4223 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch 3227 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
@@ -4232,11 +3236,15 @@ static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused
4232 * (Do we want a specific meta data flag for this?) 3236 * (Do we want a specific meta data flag for this?)
4233 * 3237 *
4234 * If that does not make it to stable storage either, 3238 * If that does not make it to stable storage either,
4235 * we cannot do anything about that anymore. */ 3239 * we cannot do anything about that anymore.
4236 if (mdev->bitmap) { 3240 *
3241 * We still need to check if both bitmap and ldev are present, we may
3242 * end up here after a failed attach, before ldev was even assigned.
3243 */
3244 if (mdev->bitmap && mdev->ldev) {
4237 if (drbd_bitmap_io_from_worker(mdev, drbd_bm_write, 3245 if (drbd_bitmap_io_from_worker(mdev, drbd_bm_write,
4238 "detach", BM_LOCKED_MASK)) { 3246 "detach", BM_LOCKED_MASK)) {
4239 if (drbd_test_flag(mdev, WAS_READ_ERROR)) { 3247 if (test_bit(WAS_READ_ERROR, &mdev->flags)) {
4240 drbd_md_set_flag(mdev, MDF_FULL_SYNC); 3248 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
4241 drbd_md_sync(mdev); 3249 drbd_md_sync(mdev);
4242 } 3250 }
@@ -4244,14 +3252,14 @@ static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused
4244 } 3252 }
4245 3253
4246 drbd_force_state(mdev, NS(disk, D_DISKLESS)); 3254 drbd_force_state(mdev, NS(disk, D_DISKLESS));
4247 return 1; 3255 return 0;
4248} 3256}
4249 3257
4250void drbd_go_diskless(struct drbd_conf *mdev) 3258void drbd_go_diskless(struct drbd_conf *mdev)
4251{ 3259{
4252 D_ASSERT(mdev->state.disk == D_FAILED); 3260 D_ASSERT(mdev->state.disk == D_FAILED);
4253 if (!drbd_test_and_set_flag(mdev, GO_DISKLESS)) 3261 if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
4254 drbd_queue_work(&mdev->data.work, &mdev->go_diskless); 3262 drbd_queue_work(&mdev->tconn->sender_work, &mdev->go_diskless);
4255} 3263}
4256 3264
4257/** 3265/**
@@ -4271,10 +3279,10 @@ void drbd_queue_bitmap_io(struct drbd_conf *mdev,
4271 void (*done)(struct drbd_conf *, int), 3279 void (*done)(struct drbd_conf *, int),
4272 char *why, enum bm_flag flags) 3280 char *why, enum bm_flag flags)
4273{ 3281{
4274 D_ASSERT(current == mdev->worker.task); 3282 D_ASSERT(current == mdev->tconn->worker.task);
4275 3283
4276 D_ASSERT(!drbd_test_flag(mdev, BITMAP_IO_QUEUED)); 3284 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
4277 D_ASSERT(!drbd_test_flag(mdev, BITMAP_IO)); 3285 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
4278 D_ASSERT(list_empty(&mdev->bm_io_work.w.list)); 3286 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
4279 if (mdev->bm_io_work.why) 3287 if (mdev->bm_io_work.why)
4280 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n", 3288 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
@@ -4285,13 +3293,13 @@ void drbd_queue_bitmap_io(struct drbd_conf *mdev,
4285 mdev->bm_io_work.why = why; 3293 mdev->bm_io_work.why = why;
4286 mdev->bm_io_work.flags = flags; 3294 mdev->bm_io_work.flags = flags;
4287 3295
4288 spin_lock_irq(&mdev->req_lock); 3296 spin_lock_irq(&mdev->tconn->req_lock);
4289 drbd_set_flag(mdev, BITMAP_IO); 3297 set_bit(BITMAP_IO, &mdev->flags);
4290 if (atomic_read(&mdev->ap_bio_cnt) == 0) { 3298 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
4291 if (!drbd_test_and_set_flag(mdev, BITMAP_IO_QUEUED)) 3299 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
4292 drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w); 3300 drbd_queue_work(&mdev->tconn->sender_work, &mdev->bm_io_work.w);
4293 } 3301 }
4294 spin_unlock_irq(&mdev->req_lock); 3302 spin_unlock_irq(&mdev->tconn->req_lock);
4295} 3303}
4296 3304
4297/** 3305/**
@@ -4308,7 +3316,7 @@ int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
4308{ 3316{
4309 int rv; 3317 int rv;
4310 3318
4311 D_ASSERT(current != mdev->worker.task); 3319 D_ASSERT(current != mdev->tconn->worker.task);
4312 3320
4313 if ((flags & BM_LOCKED_SET_ALLOWED) == 0) 3321 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
4314 drbd_suspend_io(mdev); 3322 drbd_suspend_io(mdev);
@@ -4347,18 +3355,127 @@ static void md_sync_timer_fn(unsigned long data)
4347{ 3355{
4348 struct drbd_conf *mdev = (struct drbd_conf *) data; 3356 struct drbd_conf *mdev = (struct drbd_conf *) data;
4349 3357
4350 drbd_queue_work_front(&mdev->data.work, &mdev->md_sync_work); 3358 /* must not double-queue! */
3359 if (list_empty(&mdev->md_sync_work.list))
3360 drbd_queue_work_front(&mdev->tconn->sender_work, &mdev->md_sync_work);
4351} 3361}
4352 3362
4353static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused) 3363static int w_md_sync(struct drbd_work *w, int unused)
4354{ 3364{
3365 struct drbd_conf *mdev = w->mdev;
3366
4355 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n"); 3367 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
4356#ifdef DEBUG 3368#ifdef DEBUG
4357 dev_warn(DEV, "last md_mark_dirty: %s:%u\n", 3369 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
4358 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line); 3370 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
4359#endif 3371#endif
4360 drbd_md_sync(mdev); 3372 drbd_md_sync(mdev);
4361 return 1; 3373 return 0;
3374}
3375
3376const char *cmdname(enum drbd_packet cmd)
3377{
3378 /* THINK may need to become several global tables
3379 * when we want to support more than
3380 * one PRO_VERSION */
3381 static const char *cmdnames[] = {
3382 [P_DATA] = "Data",
3383 [P_DATA_REPLY] = "DataReply",
3384 [P_RS_DATA_REPLY] = "RSDataReply",
3385 [P_BARRIER] = "Barrier",
3386 [P_BITMAP] = "ReportBitMap",
3387 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
3388 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
3389 [P_UNPLUG_REMOTE] = "UnplugRemote",
3390 [P_DATA_REQUEST] = "DataRequest",
3391 [P_RS_DATA_REQUEST] = "RSDataRequest",
3392 [P_SYNC_PARAM] = "SyncParam",
3393 [P_SYNC_PARAM89] = "SyncParam89",
3394 [P_PROTOCOL] = "ReportProtocol",
3395 [P_UUIDS] = "ReportUUIDs",
3396 [P_SIZES] = "ReportSizes",
3397 [P_STATE] = "ReportState",
3398 [P_SYNC_UUID] = "ReportSyncUUID",
3399 [P_AUTH_CHALLENGE] = "AuthChallenge",
3400 [P_AUTH_RESPONSE] = "AuthResponse",
3401 [P_PING] = "Ping",
3402 [P_PING_ACK] = "PingAck",
3403 [P_RECV_ACK] = "RecvAck",
3404 [P_WRITE_ACK] = "WriteAck",
3405 [P_RS_WRITE_ACK] = "RSWriteAck",
3406 [P_SUPERSEDED] = "Superseded",
3407 [P_NEG_ACK] = "NegAck",
3408 [P_NEG_DREPLY] = "NegDReply",
3409 [P_NEG_RS_DREPLY] = "NegRSDReply",
3410 [P_BARRIER_ACK] = "BarrierAck",
3411 [P_STATE_CHG_REQ] = "StateChgRequest",
3412 [P_STATE_CHG_REPLY] = "StateChgReply",
3413 [P_OV_REQUEST] = "OVRequest",
3414 [P_OV_REPLY] = "OVReply",
3415 [P_OV_RESULT] = "OVResult",
3416 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3417 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3418 [P_COMPRESSED_BITMAP] = "CBitmap",
3419 [P_DELAY_PROBE] = "DelayProbe",
3420 [P_OUT_OF_SYNC] = "OutOfSync",
3421 [P_RETRY_WRITE] = "RetryWrite",
3422 [P_RS_CANCEL] = "RSCancel",
3423 [P_CONN_ST_CHG_REQ] = "conn_st_chg_req",
3424 [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply",
3425 [P_RETRY_WRITE] = "retry_write",
3426 [P_PROTOCOL_UPDATE] = "protocol_update",
3427
3428 /* enum drbd_packet, but not commands - obsoleted flags:
3429 * P_MAY_IGNORE
3430 * P_MAX_OPT_CMD
3431 */
3432 };
3433
3434 /* too big for the array: 0xfffX */
3435 if (cmd == P_INITIAL_META)
3436 return "InitialMeta";
3437 if (cmd == P_INITIAL_DATA)
3438 return "InitialData";
3439 if (cmd == P_CONNECTION_FEATURES)
3440 return "ConnectionFeatures";
3441 if (cmd >= ARRAY_SIZE(cmdnames))
3442 return "Unknown";
3443 return cmdnames[cmd];
3444}
3445
3446/**
3447 * drbd_wait_misc - wait for a request to make progress
3448 * @mdev: device associated with the request
3449 * @i: the struct drbd_interval embedded in struct drbd_request or
3450 * struct drbd_peer_request
3451 */
3452int drbd_wait_misc(struct drbd_conf *mdev, struct drbd_interval *i)
3453{
3454 struct net_conf *nc;
3455 DEFINE_WAIT(wait);
3456 long timeout;
3457
3458 rcu_read_lock();
3459 nc = rcu_dereference(mdev->tconn->net_conf);
3460 if (!nc) {
3461 rcu_read_unlock();
3462 return -ETIMEDOUT;
3463 }
3464 timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
3465 rcu_read_unlock();
3466
3467 /* Indicate to wake up mdev->misc_wait on progress. */
3468 i->waiting = true;
3469 prepare_to_wait(&mdev->misc_wait, &wait, TASK_INTERRUPTIBLE);
3470 spin_unlock_irq(&mdev->tconn->req_lock);
3471 timeout = schedule_timeout(timeout);
3472 finish_wait(&mdev->misc_wait, &wait);
3473 spin_lock_irq(&mdev->tconn->req_lock);
3474 if (!timeout || mdev->state.conn < C_CONNECTED)
3475 return -ETIMEDOUT;
3476 if (signal_pending(current))
3477 return -ERESTARTSYS;
3478 return 0;
4362} 3479}
4363 3480
4364#ifdef CONFIG_DRBD_FAULT_INJECTION 3481#ifdef CONFIG_DRBD_FAULT_INJECTION