drbd: Improve some function and variable naming
[deliverable/linux.git] / drivers / block / drbd / drbd_main.c
1 /*
2 drbd.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
12
13 drbd is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 drbd is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with drbd; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 */
28
29 #include <linux/module.h>
30 #include <linux/drbd.h>
31 #include <asm/uaccess.h>
32 #include <asm/types.h>
33 #include <net/sock.h>
34 #include <linux/ctype.h>
35 #include <linux/mutex.h>
36 #include <linux/fs.h>
37 #include <linux/file.h>
38 #include <linux/proc_fs.h>
39 #include <linux/init.h>
40 #include <linux/mm.h>
41 #include <linux/memcontrol.h>
42 #include <linux/mm_inline.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/notifier.h>
47 #include <linux/kthread.h>
48 #include <linux/workqueue.h>
49 #define __KERNEL_SYSCALLS__
50 #include <linux/unistd.h>
51 #include <linux/vmalloc.h>
52
53 #include <linux/drbd_limits.h>
54 #include "drbd_int.h"
55 #include "drbd_protocol.h"
56 #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
57
58 #include "drbd_vli.h"
59
60 static DEFINE_MUTEX(drbd_main_mutex);
61 int drbd_worker(struct drbd_thread *);
62
63 int drbd_init(void);
64 static int drbd_open(struct block_device *bdev, fmode_t mode);
65 static void drbd_release(struct gendisk *gd, fmode_t mode);
66 static int w_md_sync(struct drbd_work *w, int unused);
67 static void md_sync_timer_fn(unsigned long data);
68 static int w_bitmap_io(struct drbd_work *w, int unused);
69 static int w_go_diskless(struct drbd_work *w, int unused);
70
71 MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
72 "Lars Ellenberg <lars@linbit.com>");
73 MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
74 MODULE_VERSION(REL_VERSION);
75 MODULE_LICENSE("GPL");
76 MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
77 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
78 MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
79
80 #include <linux/moduleparam.h>
81 /* allow_open_on_secondary */
82 MODULE_PARM_DESC(allow_oos, "DONT USE!");
83 /* thanks to these macros, if compiled into the kernel (not-module),
84 * this becomes the boot parameter drbd.minor_count */
85 module_param(minor_count, uint, 0444);
86 module_param(disable_sendpage, bool, 0644);
87 module_param(allow_oos, bool, 0);
88 module_param(proc_details, int, 0644);
89
90 #ifdef CONFIG_DRBD_FAULT_INJECTION
91 int enable_faults;
92 int fault_rate;
93 static int fault_count;
94 int fault_devs;
95 /* bitmap of enabled faults */
96 module_param(enable_faults, int, 0664);
97 /* fault rate % value - applies to all enabled faults */
98 module_param(fault_rate, int, 0664);
99 /* count of faults inserted */
100 module_param(fault_count, int, 0664);
101 /* bitmap of devices to insert faults on */
102 module_param(fault_devs, int, 0644);
103 #endif
104
105 /* module parameter, defined */
106 unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
107 bool disable_sendpage;
108 bool allow_oos;
109 int proc_details; /* Detail level in proc drbd*/
110
111 /* Module parameter for setting the user mode helper program
112 * to run. Default is /sbin/drbdadm */
113 char usermode_helper[80] = "/sbin/drbdadm";
114
115 module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
116
117 /* in 2.6.x, our device mapping and config info contains our virtual gendisks
118 * as member "struct gendisk *vdisk;"
119 */
120 struct idr drbd_devices;
121 struct list_head drbd_connections; /* list of struct drbd_connection */
122
123 struct kmem_cache *drbd_request_cache;
124 struct kmem_cache *drbd_ee_cache; /* peer requests */
125 struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
126 struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
127 mempool_t *drbd_request_mempool;
128 mempool_t *drbd_ee_mempool;
129 mempool_t *drbd_md_io_page_pool;
130 struct bio_set *drbd_md_io_bio_set;
131
132 /* I do not use a standard mempool, because:
133 1) I want to hand out the pre-allocated objects first.
134 2) I want to be able to interrupt sleeping allocation with a signal.
135 Note: This is a single linked list, the next pointer is the private
136 member of struct page.
137 */
138 struct page *drbd_pp_pool;
139 spinlock_t drbd_pp_lock;
140 int drbd_pp_vacant;
141 wait_queue_head_t drbd_pp_wait;
142
143 DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
144
145 static const struct block_device_operations drbd_ops = {
146 .owner = THIS_MODULE,
147 .open = drbd_open,
148 .release = drbd_release,
149 };
150
151 struct bio *bio_alloc_drbd(gfp_t gfp_mask)
152 {
153 struct bio *bio;
154
155 if (!drbd_md_io_bio_set)
156 return bio_alloc(gfp_mask, 1);
157
158 bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
159 if (!bio)
160 return NULL;
161 return bio;
162 }
163
164 #ifdef __CHECKER__
165 /* When checking with sparse, and this is an inline function, sparse will
166 give tons of false positives. When this is a real functions sparse works.
167 */
168 int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
169 {
170 int io_allowed;
171
172 atomic_inc(&device->local_cnt);
173 io_allowed = (device->state.disk >= mins);
174 if (!io_allowed) {
175 if (atomic_dec_and_test(&device->local_cnt))
176 wake_up(&device->misc_wait);
177 }
178 return io_allowed;
179 }
180
181 #endif
182
183 /**
184 * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
185 * @connection: DRBD connection.
186 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
187 * @set_size: Expected number of requests before that barrier.
188 *
189 * In case the passed barrier_nr or set_size does not match the oldest
190 * epoch of not yet barrier-acked requests, this function will cause a
191 * termination of the connection.
192 */
193 void tl_release(struct drbd_connection *connection, unsigned int barrier_nr,
194 unsigned int set_size)
195 {
196 struct drbd_request *r;
197 struct drbd_request *req = NULL;
198 int expect_epoch = 0;
199 int expect_size = 0;
200
201 spin_lock_irq(&connection->req_lock);
202
203 /* find oldest not yet barrier-acked write request,
204 * count writes in its epoch. */
205 list_for_each_entry(r, &connection->transfer_log, tl_requests) {
206 const unsigned s = r->rq_state;
207 if (!req) {
208 if (!(s & RQ_WRITE))
209 continue;
210 if (!(s & RQ_NET_MASK))
211 continue;
212 if (s & RQ_NET_DONE)
213 continue;
214 req = r;
215 expect_epoch = req->epoch;
216 expect_size ++;
217 } else {
218 if (r->epoch != expect_epoch)
219 break;
220 if (!(s & RQ_WRITE))
221 continue;
222 /* if (s & RQ_DONE): not expected */
223 /* if (!(s & RQ_NET_MASK)): not expected */
224 expect_size++;
225 }
226 }
227
228 /* first some paranoia code */
229 if (req == NULL) {
230 conn_err(connection, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
231 barrier_nr);
232 goto bail;
233 }
234 if (expect_epoch != barrier_nr) {
235 conn_err(connection, "BAD! BarrierAck #%u received, expected #%u!\n",
236 barrier_nr, expect_epoch);
237 goto bail;
238 }
239
240 if (expect_size != set_size) {
241 conn_err(connection, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
242 barrier_nr, set_size, expect_size);
243 goto bail;
244 }
245
246 /* Clean up list of requests processed during current epoch. */
247 /* this extra list walk restart is paranoia,
248 * to catch requests being barrier-acked "unexpectedly".
249 * It usually should find the same req again, or some READ preceding it. */
250 list_for_each_entry(req, &connection->transfer_log, tl_requests)
251 if (req->epoch == expect_epoch)
252 break;
253 list_for_each_entry_safe_from(req, r, &connection->transfer_log, tl_requests) {
254 if (req->epoch != expect_epoch)
255 break;
256 _req_mod(req, BARRIER_ACKED);
257 }
258 spin_unlock_irq(&connection->req_lock);
259
260 return;
261
262 bail:
263 spin_unlock_irq(&connection->req_lock);
264 conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
265 }
266
267
268 /**
269 * _tl_restart() - Walks the transfer log, and applies an action to all requests
270 * @device: DRBD device.
271 * @what: The action/event to perform with all request objects
272 *
273 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
274 * RESTART_FROZEN_DISK_IO.
275 */
276 /* must hold resource->req_lock */
277 void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
278 {
279 struct drbd_request *req, *r;
280
281 list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests)
282 _req_mod(req, what);
283 }
284
285 void tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
286 {
287 spin_lock_irq(&connection->req_lock);
288 _tl_restart(connection, what);
289 spin_unlock_irq(&connection->req_lock);
290 }
291
292 /**
293 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
294 * @device: DRBD device.
295 *
296 * This is called after the connection to the peer was lost. The storage covered
297 * by the requests on the transfer gets marked as our of sync. Called from the
298 * receiver thread and the worker thread.
299 */
300 void tl_clear(struct drbd_connection *connection)
301 {
302 tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
303 }
304
305 /**
306 * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL
307 * @device: DRBD device.
308 */
309 void tl_abort_disk_io(struct drbd_device *device)
310 {
311 struct drbd_connection *connection = first_peer_device(device)->connection;
312 struct drbd_request *req, *r;
313
314 spin_lock_irq(&connection->req_lock);
315 list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) {
316 if (!(req->rq_state & RQ_LOCAL_PENDING))
317 continue;
318 if (req->w.device != device)
319 continue;
320 _req_mod(req, ABORT_DISK_IO);
321 }
322 spin_unlock_irq(&connection->req_lock);
323 }
324
325 static int drbd_thread_setup(void *arg)
326 {
327 struct drbd_thread *thi = (struct drbd_thread *) arg;
328 struct drbd_connection *connection = thi->connection;
329 unsigned long flags;
330 int retval;
331
332 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
333 thi->name[0], thi->connection->name);
334
335 restart:
336 retval = thi->function(thi);
337
338 spin_lock_irqsave(&thi->t_lock, flags);
339
340 /* if the receiver has been "EXITING", the last thing it did
341 * was set the conn state to "StandAlone",
342 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
343 * and receiver thread will be "started".
344 * drbd_thread_start needs to set "RESTARTING" in that case.
345 * t_state check and assignment needs to be within the same spinlock,
346 * so either thread_start sees EXITING, and can remap to RESTARTING,
347 * or thread_start see NONE, and can proceed as normal.
348 */
349
350 if (thi->t_state == RESTARTING) {
351 conn_info(connection, "Restarting %s thread\n", thi->name);
352 thi->t_state = RUNNING;
353 spin_unlock_irqrestore(&thi->t_lock, flags);
354 goto restart;
355 }
356
357 thi->task = NULL;
358 thi->t_state = NONE;
359 smp_mb();
360 complete_all(&thi->stop);
361 spin_unlock_irqrestore(&thi->t_lock, flags);
362
363 conn_info(connection, "Terminating %s\n", current->comm);
364
365 /* Release mod reference taken when thread was started */
366
367 kref_put(&connection->kref, drbd_destroy_connection);
368 module_put(THIS_MODULE);
369 return retval;
370 }
371
372 static void drbd_thread_init(struct drbd_connection *connection, struct drbd_thread *thi,
373 int (*func) (struct drbd_thread *), char *name)
374 {
375 spin_lock_init(&thi->t_lock);
376 thi->task = NULL;
377 thi->t_state = NONE;
378 thi->function = func;
379 thi->connection = connection;
380 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
381 }
382
383 int drbd_thread_start(struct drbd_thread *thi)
384 {
385 struct drbd_connection *connection = thi->connection;
386 struct task_struct *nt;
387 unsigned long flags;
388
389 /* is used from state engine doing drbd_thread_stop_nowait,
390 * while holding the req lock irqsave */
391 spin_lock_irqsave(&thi->t_lock, flags);
392
393 switch (thi->t_state) {
394 case NONE:
395 conn_info(connection, "Starting %s thread (from %s [%d])\n",
396 thi->name, current->comm, current->pid);
397
398 /* Get ref on module for thread - this is released when thread exits */
399 if (!try_module_get(THIS_MODULE)) {
400 conn_err(connection, "Failed to get module reference in drbd_thread_start\n");
401 spin_unlock_irqrestore(&thi->t_lock, flags);
402 return false;
403 }
404
405 kref_get(&thi->connection->kref);
406
407 init_completion(&thi->stop);
408 thi->reset_cpu_mask = 1;
409 thi->t_state = RUNNING;
410 spin_unlock_irqrestore(&thi->t_lock, flags);
411 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
412
413 nt = kthread_create(drbd_thread_setup, (void *) thi,
414 "drbd_%c_%s", thi->name[0], thi->connection->name);
415
416 if (IS_ERR(nt)) {
417 conn_err(connection, "Couldn't start thread\n");
418
419 kref_put(&connection->kref, drbd_destroy_connection);
420 module_put(THIS_MODULE);
421 return false;
422 }
423 spin_lock_irqsave(&thi->t_lock, flags);
424 thi->task = nt;
425 thi->t_state = RUNNING;
426 spin_unlock_irqrestore(&thi->t_lock, flags);
427 wake_up_process(nt);
428 break;
429 case EXITING:
430 thi->t_state = RESTARTING;
431 conn_info(connection, "Restarting %s thread (from %s [%d])\n",
432 thi->name, current->comm, current->pid);
433 /* fall through */
434 case RUNNING:
435 case RESTARTING:
436 default:
437 spin_unlock_irqrestore(&thi->t_lock, flags);
438 break;
439 }
440
441 return true;
442 }
443
444
445 void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
446 {
447 unsigned long flags;
448
449 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
450
451 /* may be called from state engine, holding the req lock irqsave */
452 spin_lock_irqsave(&thi->t_lock, flags);
453
454 if (thi->t_state == NONE) {
455 spin_unlock_irqrestore(&thi->t_lock, flags);
456 if (restart)
457 drbd_thread_start(thi);
458 return;
459 }
460
461 if (thi->t_state != ns) {
462 if (thi->task == NULL) {
463 spin_unlock_irqrestore(&thi->t_lock, flags);
464 return;
465 }
466
467 thi->t_state = ns;
468 smp_mb();
469 init_completion(&thi->stop);
470 if (thi->task != current)
471 force_sig(DRBD_SIGKILL, thi->task);
472 }
473
474 spin_unlock_irqrestore(&thi->t_lock, flags);
475
476 if (wait)
477 wait_for_completion(&thi->stop);
478 }
479
480 static struct drbd_thread *drbd_task_to_thread(struct drbd_connection *connection, struct task_struct *task)
481 {
482 struct drbd_thread *thi =
483 task == connection->receiver.task ? &connection->receiver :
484 task == connection->asender.task ? &connection->asender :
485 task == connection->worker.task ? &connection->worker : NULL;
486
487 return thi;
488 }
489
490 char *drbd_task_to_thread_name(struct drbd_connection *connection, struct task_struct *task)
491 {
492 struct drbd_thread *thi = drbd_task_to_thread(connection, task);
493 return thi ? thi->name : task->comm;
494 }
495
496 int conn_lowest_minor(struct drbd_connection *connection)
497 {
498 struct drbd_device *device;
499 int vnr = 0, m;
500
501 rcu_read_lock();
502 device = idr_get_next(&connection->volumes, &vnr);
503 m = device ? device_to_minor(device) : -1;
504 rcu_read_unlock();
505
506 return m;
507 }
508
509 #ifdef CONFIG_SMP
510 /**
511 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
512 * @device: DRBD device.
513 *
514 * Forces all threads of a device onto the same CPU. This is beneficial for
515 * DRBD's performance. May be overwritten by user's configuration.
516 */
517 void drbd_calc_cpu_mask(struct drbd_connection *connection)
518 {
519 int ord, cpu;
520
521 /* user override. */
522 if (cpumask_weight(connection->cpu_mask))
523 return;
524
525 ord = conn_lowest_minor(connection) % cpumask_weight(cpu_online_mask);
526 for_each_online_cpu(cpu) {
527 if (ord-- == 0) {
528 cpumask_set_cpu(cpu, connection->cpu_mask);
529 return;
530 }
531 }
532 /* should not be reached */
533 cpumask_setall(connection->cpu_mask);
534 }
535
536 /**
537 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
538 * @device: DRBD device.
539 * @thi: drbd_thread object
540 *
541 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
542 * prematurely.
543 */
544 void drbd_thread_current_set_cpu(struct drbd_thread *thi)
545 {
546 struct task_struct *p = current;
547
548 if (!thi->reset_cpu_mask)
549 return;
550 thi->reset_cpu_mask = 0;
551 set_cpus_allowed_ptr(p, thi->connection->cpu_mask);
552 }
553 #endif
554
555 /**
556 * drbd_header_size - size of a packet header
557 *
558 * The header size is a multiple of 8, so any payload following the header is
559 * word aligned on 64-bit architectures. (The bitmap send and receive code
560 * relies on this.)
561 */
562 unsigned int drbd_header_size(struct drbd_connection *connection)
563 {
564 if (connection->agreed_pro_version >= 100) {
565 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
566 return sizeof(struct p_header100);
567 } else {
568 BUILD_BUG_ON(sizeof(struct p_header80) !=
569 sizeof(struct p_header95));
570 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
571 return sizeof(struct p_header80);
572 }
573 }
574
575 static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
576 {
577 h->magic = cpu_to_be32(DRBD_MAGIC);
578 h->command = cpu_to_be16(cmd);
579 h->length = cpu_to_be16(size);
580 return sizeof(struct p_header80);
581 }
582
583 static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
584 {
585 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
586 h->command = cpu_to_be16(cmd);
587 h->length = cpu_to_be32(size);
588 return sizeof(struct p_header95);
589 }
590
591 static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
592 int size, int vnr)
593 {
594 h->magic = cpu_to_be32(DRBD_MAGIC_100);
595 h->volume = cpu_to_be16(vnr);
596 h->command = cpu_to_be16(cmd);
597 h->length = cpu_to_be32(size);
598 h->pad = 0;
599 return sizeof(struct p_header100);
600 }
601
602 static unsigned int prepare_header(struct drbd_connection *connection, int vnr,
603 void *buffer, enum drbd_packet cmd, int size)
604 {
605 if (connection->agreed_pro_version >= 100)
606 return prepare_header100(buffer, cmd, size, vnr);
607 else if (connection->agreed_pro_version >= 95 &&
608 size > DRBD_MAX_SIZE_H80_PACKET)
609 return prepare_header95(buffer, cmd, size);
610 else
611 return prepare_header80(buffer, cmd, size);
612 }
613
614 static void *__conn_prepare_command(struct drbd_connection *connection,
615 struct drbd_socket *sock)
616 {
617 if (!sock->socket)
618 return NULL;
619 return sock->sbuf + drbd_header_size(connection);
620 }
621
622 void *conn_prepare_command(struct drbd_connection *connection, struct drbd_socket *sock)
623 {
624 void *p;
625
626 mutex_lock(&sock->mutex);
627 p = __conn_prepare_command(connection, sock);
628 if (!p)
629 mutex_unlock(&sock->mutex);
630
631 return p;
632 }
633
634 void *drbd_prepare_command(struct drbd_device *device, struct drbd_socket *sock)
635 {
636 return conn_prepare_command(first_peer_device(device)->connection, sock);
637 }
638
639 static int __send_command(struct drbd_connection *connection, int vnr,
640 struct drbd_socket *sock, enum drbd_packet cmd,
641 unsigned int header_size, void *data,
642 unsigned int size)
643 {
644 int msg_flags;
645 int err;
646
647 /*
648 * Called with @data == NULL and the size of the data blocks in @size
649 * for commands that send data blocks. For those commands, omit the
650 * MSG_MORE flag: this will increase the likelihood that data blocks
651 * which are page aligned on the sender will end up page aligned on the
652 * receiver.
653 */
654 msg_flags = data ? MSG_MORE : 0;
655
656 header_size += prepare_header(connection, vnr, sock->sbuf, cmd,
657 header_size + size);
658 err = drbd_send_all(connection, sock->socket, sock->sbuf, header_size,
659 msg_flags);
660 if (data && !err)
661 err = drbd_send_all(connection, sock->socket, data, size, 0);
662 return err;
663 }
664
665 static int __conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
666 enum drbd_packet cmd, unsigned int header_size,
667 void *data, unsigned int size)
668 {
669 return __send_command(connection, 0, sock, cmd, header_size, data, size);
670 }
671
672 int conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
673 enum drbd_packet cmd, unsigned int header_size,
674 void *data, unsigned int size)
675 {
676 int err;
677
678 err = __conn_send_command(connection, sock, cmd, header_size, data, size);
679 mutex_unlock(&sock->mutex);
680 return err;
681 }
682
683 int drbd_send_command(struct drbd_device *device, struct drbd_socket *sock,
684 enum drbd_packet cmd, unsigned int header_size,
685 void *data, unsigned int size)
686 {
687 int err;
688
689 err = __send_command(first_peer_device(device)->connection, device->vnr, sock, cmd, header_size,
690 data, size);
691 mutex_unlock(&sock->mutex);
692 return err;
693 }
694
695 int drbd_send_ping(struct drbd_connection *connection)
696 {
697 struct drbd_socket *sock;
698
699 sock = &connection->meta;
700 if (!conn_prepare_command(connection, sock))
701 return -EIO;
702 return conn_send_command(connection, sock, P_PING, 0, NULL, 0);
703 }
704
705 int drbd_send_ping_ack(struct drbd_connection *connection)
706 {
707 struct drbd_socket *sock;
708
709 sock = &connection->meta;
710 if (!conn_prepare_command(connection, sock))
711 return -EIO;
712 return conn_send_command(connection, sock, P_PING_ACK, 0, NULL, 0);
713 }
714
715 int drbd_send_sync_param(struct drbd_device *device)
716 {
717 struct drbd_socket *sock;
718 struct p_rs_param_95 *p;
719 int size;
720 const int apv = first_peer_device(device)->connection->agreed_pro_version;
721 enum drbd_packet cmd;
722 struct net_conf *nc;
723 struct disk_conf *dc;
724
725 sock = &first_peer_device(device)->connection->data;
726 p = drbd_prepare_command(device, sock);
727 if (!p)
728 return -EIO;
729
730 rcu_read_lock();
731 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
732
733 size = apv <= 87 ? sizeof(struct p_rs_param)
734 : apv == 88 ? sizeof(struct p_rs_param)
735 + strlen(nc->verify_alg) + 1
736 : apv <= 94 ? sizeof(struct p_rs_param_89)
737 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
738
739 cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
740
741 /* initialize verify_alg and csums_alg */
742 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
743
744 if (get_ldev(device)) {
745 dc = rcu_dereference(device->ldev->disk_conf);
746 p->resync_rate = cpu_to_be32(dc->resync_rate);
747 p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
748 p->c_delay_target = cpu_to_be32(dc->c_delay_target);
749 p->c_fill_target = cpu_to_be32(dc->c_fill_target);
750 p->c_max_rate = cpu_to_be32(dc->c_max_rate);
751 put_ldev(device);
752 } else {
753 p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
754 p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
755 p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
756 p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
757 p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
758 }
759
760 if (apv >= 88)
761 strcpy(p->verify_alg, nc->verify_alg);
762 if (apv >= 89)
763 strcpy(p->csums_alg, nc->csums_alg);
764 rcu_read_unlock();
765
766 return drbd_send_command(device, sock, cmd, size, NULL, 0);
767 }
768
769 int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd)
770 {
771 struct drbd_socket *sock;
772 struct p_protocol *p;
773 struct net_conf *nc;
774 int size, cf;
775
776 sock = &connection->data;
777 p = __conn_prepare_command(connection, sock);
778 if (!p)
779 return -EIO;
780
781 rcu_read_lock();
782 nc = rcu_dereference(connection->net_conf);
783
784 if (nc->tentative && connection->agreed_pro_version < 92) {
785 rcu_read_unlock();
786 mutex_unlock(&sock->mutex);
787 conn_err(connection, "--dry-run is not supported by peer");
788 return -EOPNOTSUPP;
789 }
790
791 size = sizeof(*p);
792 if (connection->agreed_pro_version >= 87)
793 size += strlen(nc->integrity_alg) + 1;
794
795 p->protocol = cpu_to_be32(nc->wire_protocol);
796 p->after_sb_0p = cpu_to_be32(nc->after_sb_0p);
797 p->after_sb_1p = cpu_to_be32(nc->after_sb_1p);
798 p->after_sb_2p = cpu_to_be32(nc->after_sb_2p);
799 p->two_primaries = cpu_to_be32(nc->two_primaries);
800 cf = 0;
801 if (nc->discard_my_data)
802 cf |= CF_DISCARD_MY_DATA;
803 if (nc->tentative)
804 cf |= CF_DRY_RUN;
805 p->conn_flags = cpu_to_be32(cf);
806
807 if (connection->agreed_pro_version >= 87)
808 strcpy(p->integrity_alg, nc->integrity_alg);
809 rcu_read_unlock();
810
811 return __conn_send_command(connection, sock, cmd, size, NULL, 0);
812 }
813
814 int drbd_send_protocol(struct drbd_connection *connection)
815 {
816 int err;
817
818 mutex_lock(&connection->data.mutex);
819 err = __drbd_send_protocol(connection, P_PROTOCOL);
820 mutex_unlock(&connection->data.mutex);
821
822 return err;
823 }
824
825 static int _drbd_send_uuids(struct drbd_device *device, u64 uuid_flags)
826 {
827 struct drbd_socket *sock;
828 struct p_uuids *p;
829 int i;
830
831 if (!get_ldev_if_state(device, D_NEGOTIATING))
832 return 0;
833
834 sock = &first_peer_device(device)->connection->data;
835 p = drbd_prepare_command(device, sock);
836 if (!p) {
837 put_ldev(device);
838 return -EIO;
839 }
840 spin_lock_irq(&device->ldev->md.uuid_lock);
841 for (i = UI_CURRENT; i < UI_SIZE; i++)
842 p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
843 spin_unlock_irq(&device->ldev->md.uuid_lock);
844
845 device->comm_bm_set = drbd_bm_total_weight(device);
846 p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set);
847 rcu_read_lock();
848 uuid_flags |= rcu_dereference(first_peer_device(device)->connection->net_conf)->discard_my_data ? 1 : 0;
849 rcu_read_unlock();
850 uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0;
851 uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
852 p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
853
854 put_ldev(device);
855 return drbd_send_command(device, sock, P_UUIDS, sizeof(*p), NULL, 0);
856 }
857
858 int drbd_send_uuids(struct drbd_device *device)
859 {
860 return _drbd_send_uuids(device, 0);
861 }
862
863 int drbd_send_uuids_skip_initial_sync(struct drbd_device *device)
864 {
865 return _drbd_send_uuids(device, 8);
866 }
867
868 void drbd_print_uuids(struct drbd_device *device, const char *text)
869 {
870 if (get_ldev_if_state(device, D_NEGOTIATING)) {
871 u64 *uuid = device->ldev->md.uuid;
872 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
873 text,
874 (unsigned long long)uuid[UI_CURRENT],
875 (unsigned long long)uuid[UI_BITMAP],
876 (unsigned long long)uuid[UI_HISTORY_START],
877 (unsigned long long)uuid[UI_HISTORY_END]);
878 put_ldev(device);
879 } else {
880 dev_info(DEV, "%s effective data uuid: %016llX\n",
881 text,
882 (unsigned long long)device->ed_uuid);
883 }
884 }
885
886 void drbd_gen_and_send_sync_uuid(struct drbd_device *device)
887 {
888 struct drbd_socket *sock;
889 struct p_rs_uuid *p;
890 u64 uuid;
891
892 D_ASSERT(device->state.disk == D_UP_TO_DATE);
893
894 uuid = device->ldev->md.uuid[UI_BITMAP];
895 if (uuid && uuid != UUID_JUST_CREATED)
896 uuid = uuid + UUID_NEW_BM_OFFSET;
897 else
898 get_random_bytes(&uuid, sizeof(u64));
899 drbd_uuid_set(device, UI_BITMAP, uuid);
900 drbd_print_uuids(device, "updated sync UUID");
901 drbd_md_sync(device);
902
903 sock = &first_peer_device(device)->connection->data;
904 p = drbd_prepare_command(device, sock);
905 if (p) {
906 p->uuid = cpu_to_be64(uuid);
907 drbd_send_command(device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
908 }
909 }
910
911 int drbd_send_sizes(struct drbd_device *device, int trigger_reply, enum dds_flags flags)
912 {
913 struct drbd_socket *sock;
914 struct p_sizes *p;
915 sector_t d_size, u_size;
916 int q_order_type;
917 unsigned int max_bio_size;
918
919 if (get_ldev_if_state(device, D_NEGOTIATING)) {
920 D_ASSERT(device->ldev->backing_bdev);
921 d_size = drbd_get_max_capacity(device->ldev);
922 rcu_read_lock();
923 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
924 rcu_read_unlock();
925 q_order_type = drbd_queue_order_type(device);
926 max_bio_size = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
927 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
928 put_ldev(device);
929 } else {
930 d_size = 0;
931 u_size = 0;
932 q_order_type = QUEUE_ORDERED_NONE;
933 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
934 }
935
936 sock = &first_peer_device(device)->connection->data;
937 p = drbd_prepare_command(device, sock);
938 if (!p)
939 return -EIO;
940
941 if (first_peer_device(device)->connection->agreed_pro_version <= 94)
942 max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
943 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
944 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
945
946 p->d_size = cpu_to_be64(d_size);
947 p->u_size = cpu_to_be64(u_size);
948 p->c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(device->this_bdev));
949 p->max_bio_size = cpu_to_be32(max_bio_size);
950 p->queue_order_type = cpu_to_be16(q_order_type);
951 p->dds_flags = cpu_to_be16(flags);
952 return drbd_send_command(device, sock, P_SIZES, sizeof(*p), NULL, 0);
953 }
954
955 /**
956 * drbd_send_current_state() - Sends the drbd state to the peer
957 * @device: DRBD device.
958 */
959 int drbd_send_current_state(struct drbd_device *device)
960 {
961 struct drbd_socket *sock;
962 struct p_state *p;
963
964 sock = &first_peer_device(device)->connection->data;
965 p = drbd_prepare_command(device, sock);
966 if (!p)
967 return -EIO;
968 p->state = cpu_to_be32(device->state.i); /* Within the send mutex */
969 return drbd_send_command(device, sock, P_STATE, sizeof(*p), NULL, 0);
970 }
971
972 /**
973 * drbd_send_state() - After a state change, sends the new state to the peer
974 * @device: DRBD device.
975 * @state: the state to send, not necessarily the current state.
976 *
977 * Each state change queues an "after_state_ch" work, which will eventually
978 * send the resulting new state to the peer. If more state changes happen
979 * between queuing and processing of the after_state_ch work, we still
980 * want to send each intermediary state in the order it occurred.
981 */
982 int drbd_send_state(struct drbd_device *device, union drbd_state state)
983 {
984 struct drbd_socket *sock;
985 struct p_state *p;
986
987 sock = &first_peer_device(device)->connection->data;
988 p = drbd_prepare_command(device, sock);
989 if (!p)
990 return -EIO;
991 p->state = cpu_to_be32(state.i); /* Within the send mutex */
992 return drbd_send_command(device, sock, P_STATE, sizeof(*p), NULL, 0);
993 }
994
995 int drbd_send_state_req(struct drbd_device *device, union drbd_state mask, union drbd_state val)
996 {
997 struct drbd_socket *sock;
998 struct p_req_state *p;
999
1000 sock = &first_peer_device(device)->connection->data;
1001 p = drbd_prepare_command(device, sock);
1002 if (!p)
1003 return -EIO;
1004 p->mask = cpu_to_be32(mask.i);
1005 p->val = cpu_to_be32(val.i);
1006 return drbd_send_command(device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
1007 }
1008
1009 int conn_send_state_req(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
1010 {
1011 enum drbd_packet cmd;
1012 struct drbd_socket *sock;
1013 struct p_req_state *p;
1014
1015 cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
1016 sock = &connection->data;
1017 p = conn_prepare_command(connection, sock);
1018 if (!p)
1019 return -EIO;
1020 p->mask = cpu_to_be32(mask.i);
1021 p->val = cpu_to_be32(val.i);
1022 return conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
1023 }
1024
1025 void drbd_send_sr_reply(struct drbd_device *device, enum drbd_state_rv retcode)
1026 {
1027 struct drbd_socket *sock;
1028 struct p_req_state_reply *p;
1029
1030 sock = &first_peer_device(device)->connection->meta;
1031 p = drbd_prepare_command(device, sock);
1032 if (p) {
1033 p->retcode = cpu_to_be32(retcode);
1034 drbd_send_command(device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
1035 }
1036 }
1037
1038 void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode)
1039 {
1040 struct drbd_socket *sock;
1041 struct p_req_state_reply *p;
1042 enum drbd_packet cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
1043
1044 sock = &connection->meta;
1045 p = conn_prepare_command(connection, sock);
1046 if (p) {
1047 p->retcode = cpu_to_be32(retcode);
1048 conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
1049 }
1050 }
1051
1052 static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
1053 {
1054 BUG_ON(code & ~0xf);
1055 p->encoding = (p->encoding & ~0xf) | code;
1056 }
1057
1058 static void dcbp_set_start(struct p_compressed_bm *p, int set)
1059 {
1060 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
1061 }
1062
1063 static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
1064 {
1065 BUG_ON(n & ~0x7);
1066 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
1067 }
1068
1069 static int fill_bitmap_rle_bits(struct drbd_device *device,
1070 struct p_compressed_bm *p,
1071 unsigned int size,
1072 struct bm_xfer_ctx *c)
1073 {
1074 struct bitstream bs;
1075 unsigned long plain_bits;
1076 unsigned long tmp;
1077 unsigned long rl;
1078 unsigned len;
1079 unsigned toggle;
1080 int bits, use_rle;
1081
1082 /* may we use this feature? */
1083 rcu_read_lock();
1084 use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle;
1085 rcu_read_unlock();
1086 if (!use_rle || first_peer_device(device)->connection->agreed_pro_version < 90)
1087 return 0;
1088
1089 if (c->bit_offset >= c->bm_bits)
1090 return 0; /* nothing to do. */
1091
1092 /* use at most thus many bytes */
1093 bitstream_init(&bs, p->code, size, 0);
1094 memset(p->code, 0, size);
1095 /* plain bits covered in this code string */
1096 plain_bits = 0;
1097
1098 /* p->encoding & 0x80 stores whether the first run length is set.
1099 * bit offset is implicit.
1100 * start with toggle == 2 to be able to tell the first iteration */
1101 toggle = 2;
1102
1103 /* see how much plain bits we can stuff into one packet
1104 * using RLE and VLI. */
1105 do {
1106 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset)
1107 : _drbd_bm_find_next(device, c->bit_offset);
1108 if (tmp == -1UL)
1109 tmp = c->bm_bits;
1110 rl = tmp - c->bit_offset;
1111
1112 if (toggle == 2) { /* first iteration */
1113 if (rl == 0) {
1114 /* the first checked bit was set,
1115 * store start value, */
1116 dcbp_set_start(p, 1);
1117 /* but skip encoding of zero run length */
1118 toggle = !toggle;
1119 continue;
1120 }
1121 dcbp_set_start(p, 0);
1122 }
1123
1124 /* paranoia: catch zero runlength.
1125 * can only happen if bitmap is modified while we scan it. */
1126 if (rl == 0) {
1127 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1128 "t:%u bo:%lu\n", toggle, c->bit_offset);
1129 return -1;
1130 }
1131
1132 bits = vli_encode_bits(&bs, rl);
1133 if (bits == -ENOBUFS) /* buffer full */
1134 break;
1135 if (bits <= 0) {
1136 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1137 return 0;
1138 }
1139
1140 toggle = !toggle;
1141 plain_bits += rl;
1142 c->bit_offset = tmp;
1143 } while (c->bit_offset < c->bm_bits);
1144
1145 len = bs.cur.b - p->code + !!bs.cur.bit;
1146
1147 if (plain_bits < (len << 3)) {
1148 /* incompressible with this method.
1149 * we need to rewind both word and bit position. */
1150 c->bit_offset -= plain_bits;
1151 bm_xfer_ctx_bit_to_word_offset(c);
1152 c->bit_offset = c->word_offset * BITS_PER_LONG;
1153 return 0;
1154 }
1155
1156 /* RLE + VLI was able to compress it just fine.
1157 * update c->word_offset. */
1158 bm_xfer_ctx_bit_to_word_offset(c);
1159
1160 /* store pad_bits */
1161 dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
1162
1163 return len;
1164 }
1165
1166 /**
1167 * send_bitmap_rle_or_plain
1168 *
1169 * Return 0 when done, 1 when another iteration is needed, and a negative error
1170 * code upon failure.
1171 */
1172 static int
1173 send_bitmap_rle_or_plain(struct drbd_device *device, struct bm_xfer_ctx *c)
1174 {
1175 struct drbd_socket *sock = &first_peer_device(device)->connection->data;
1176 unsigned int header_size = drbd_header_size(first_peer_device(device)->connection);
1177 struct p_compressed_bm *p = sock->sbuf + header_size;
1178 int len, err;
1179
1180 len = fill_bitmap_rle_bits(device, p,
1181 DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
1182 if (len < 0)
1183 return -EIO;
1184
1185 if (len) {
1186 dcbp_set_code(p, RLE_VLI_Bits);
1187 err = __send_command(first_peer_device(device)->connection, device->vnr, sock,
1188 P_COMPRESSED_BITMAP, sizeof(*p) + len,
1189 NULL, 0);
1190 c->packets[0]++;
1191 c->bytes[0] += header_size + sizeof(*p) + len;
1192
1193 if (c->bit_offset >= c->bm_bits)
1194 len = 0; /* DONE */
1195 } else {
1196 /* was not compressible.
1197 * send a buffer full of plain text bits instead. */
1198 unsigned int data_size;
1199 unsigned long num_words;
1200 unsigned long *p = sock->sbuf + header_size;
1201
1202 data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
1203 num_words = min_t(size_t, data_size / sizeof(*p),
1204 c->bm_words - c->word_offset);
1205 len = num_words * sizeof(*p);
1206 if (len)
1207 drbd_bm_get_lel(device, c->word_offset, num_words, p);
1208 err = __send_command(first_peer_device(device)->connection, device->vnr, sock, P_BITMAP, len, NULL, 0);
1209 c->word_offset += num_words;
1210 c->bit_offset = c->word_offset * BITS_PER_LONG;
1211
1212 c->packets[1]++;
1213 c->bytes[1] += header_size + len;
1214
1215 if (c->bit_offset > c->bm_bits)
1216 c->bit_offset = c->bm_bits;
1217 }
1218 if (!err) {
1219 if (len == 0) {
1220 INFO_bm_xfer_stats(device, "send", c);
1221 return 0;
1222 } else
1223 return 1;
1224 }
1225 return -EIO;
1226 }
1227
1228 /* See the comment at receive_bitmap() */
1229 static int _drbd_send_bitmap(struct drbd_device *device)
1230 {
1231 struct bm_xfer_ctx c;
1232 int err;
1233
1234 if (!expect(device->bitmap))
1235 return false;
1236
1237 if (get_ldev(device)) {
1238 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) {
1239 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
1240 drbd_bm_set_all(device);
1241 if (drbd_bm_write(device)) {
1242 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1243 * but otherwise process as per normal - need to tell other
1244 * side that a full resync is required! */
1245 dev_err(DEV, "Failed to write bitmap to disk!\n");
1246 } else {
1247 drbd_md_clear_flag(device, MDF_FULL_SYNC);
1248 drbd_md_sync(device);
1249 }
1250 }
1251 put_ldev(device);
1252 }
1253
1254 c = (struct bm_xfer_ctx) {
1255 .bm_bits = drbd_bm_bits(device),
1256 .bm_words = drbd_bm_words(device),
1257 };
1258
1259 do {
1260 err = send_bitmap_rle_or_plain(device, &c);
1261 } while (err > 0);
1262
1263 return err == 0;
1264 }
1265
1266 int drbd_send_bitmap(struct drbd_device *device)
1267 {
1268 struct drbd_socket *sock = &first_peer_device(device)->connection->data;
1269 int err = -1;
1270
1271 mutex_lock(&sock->mutex);
1272 if (sock->socket)
1273 err = !_drbd_send_bitmap(device);
1274 mutex_unlock(&sock->mutex);
1275 return err;
1276 }
1277
1278 void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr, u32 set_size)
1279 {
1280 struct drbd_socket *sock;
1281 struct p_barrier_ack *p;
1282
1283 if (connection->cstate < C_WF_REPORT_PARAMS)
1284 return;
1285
1286 sock = &connection->meta;
1287 p = conn_prepare_command(connection, sock);
1288 if (!p)
1289 return;
1290 p->barrier = barrier_nr;
1291 p->set_size = cpu_to_be32(set_size);
1292 conn_send_command(connection, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0);
1293 }
1294
1295 /**
1296 * _drbd_send_ack() - Sends an ack packet
1297 * @device: DRBD device.
1298 * @cmd: Packet command code.
1299 * @sector: sector, needs to be in big endian byte order
1300 * @blksize: size in byte, needs to be in big endian byte order
1301 * @block_id: Id, big endian byte order
1302 */
1303 static int _drbd_send_ack(struct drbd_device *device, enum drbd_packet cmd,
1304 u64 sector, u32 blksize, u64 block_id)
1305 {
1306 struct drbd_socket *sock;
1307 struct p_block_ack *p;
1308
1309 if (device->state.conn < C_CONNECTED)
1310 return -EIO;
1311
1312 sock = &first_peer_device(device)->connection->meta;
1313 p = drbd_prepare_command(device, sock);
1314 if (!p)
1315 return -EIO;
1316 p->sector = sector;
1317 p->block_id = block_id;
1318 p->blksize = blksize;
1319 p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
1320 return drbd_send_command(device, sock, cmd, sizeof(*p), NULL, 0);
1321 }
1322
1323 /* dp->sector and dp->block_id already/still in network byte order,
1324 * data_size is payload size according to dp->head,
1325 * and may need to be corrected for digest size. */
1326 void drbd_send_ack_dp(struct drbd_device *device, enum drbd_packet cmd,
1327 struct p_data *dp, int data_size)
1328 {
1329 if (first_peer_device(device)->connection->peer_integrity_tfm)
1330 data_size -= crypto_hash_digestsize(first_peer_device(device)->connection->peer_integrity_tfm);
1331 _drbd_send_ack(device, cmd, dp->sector, cpu_to_be32(data_size),
1332 dp->block_id);
1333 }
1334
1335 void drbd_send_ack_rp(struct drbd_device *device, enum drbd_packet cmd,
1336 struct p_block_req *rp)
1337 {
1338 _drbd_send_ack(device, cmd, rp->sector, rp->blksize, rp->block_id);
1339 }
1340
1341 /**
1342 * drbd_send_ack() - Sends an ack packet
1343 * @device: DRBD device
1344 * @cmd: packet command code
1345 * @peer_req: peer request
1346 */
1347 int drbd_send_ack(struct drbd_device *device, enum drbd_packet cmd,
1348 struct drbd_peer_request *peer_req)
1349 {
1350 return _drbd_send_ack(device, cmd,
1351 cpu_to_be64(peer_req->i.sector),
1352 cpu_to_be32(peer_req->i.size),
1353 peer_req->block_id);
1354 }
1355
1356 /* This function misuses the block_id field to signal if the blocks
1357 * are is sync or not. */
1358 int drbd_send_ack_ex(struct drbd_device *device, enum drbd_packet cmd,
1359 sector_t sector, int blksize, u64 block_id)
1360 {
1361 return _drbd_send_ack(device, cmd,
1362 cpu_to_be64(sector),
1363 cpu_to_be32(blksize),
1364 cpu_to_be64(block_id));
1365 }
1366
1367 int drbd_send_drequest(struct drbd_device *device, int cmd,
1368 sector_t sector, int size, u64 block_id)
1369 {
1370 struct drbd_socket *sock;
1371 struct p_block_req *p;
1372
1373 sock = &first_peer_device(device)->connection->data;
1374 p = drbd_prepare_command(device, sock);
1375 if (!p)
1376 return -EIO;
1377 p->sector = cpu_to_be64(sector);
1378 p->block_id = block_id;
1379 p->blksize = cpu_to_be32(size);
1380 return drbd_send_command(device, sock, cmd, sizeof(*p), NULL, 0);
1381 }
1382
1383 int drbd_send_drequest_csum(struct drbd_device *device, sector_t sector, int size,
1384 void *digest, int digest_size, enum drbd_packet cmd)
1385 {
1386 struct drbd_socket *sock;
1387 struct p_block_req *p;
1388
1389 /* FIXME: Put the digest into the preallocated socket buffer. */
1390
1391 sock = &first_peer_device(device)->connection->data;
1392 p = drbd_prepare_command(device, sock);
1393 if (!p)
1394 return -EIO;
1395 p->sector = cpu_to_be64(sector);
1396 p->block_id = ID_SYNCER /* unused */;
1397 p->blksize = cpu_to_be32(size);
1398 return drbd_send_command(device, sock, cmd, sizeof(*p),
1399 digest, digest_size);
1400 }
1401
1402 int drbd_send_ov_request(struct drbd_device *device, sector_t sector, int size)
1403 {
1404 struct drbd_socket *sock;
1405 struct p_block_req *p;
1406
1407 sock = &first_peer_device(device)->connection->data;
1408 p = drbd_prepare_command(device, sock);
1409 if (!p)
1410 return -EIO;
1411 p->sector = cpu_to_be64(sector);
1412 p->block_id = ID_SYNCER /* unused */;
1413 p->blksize = cpu_to_be32(size);
1414 return drbd_send_command(device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0);
1415 }
1416
1417 /* called on sndtimeo
1418 * returns false if we should retry,
1419 * true if we think connection is dead
1420 */
1421 static int we_should_drop_the_connection(struct drbd_connection *connection, struct socket *sock)
1422 {
1423 int drop_it;
1424 /* long elapsed = (long)(jiffies - device->last_received); */
1425
1426 drop_it = connection->meta.socket == sock
1427 || !connection->asender.task
1428 || get_t_state(&connection->asender) != RUNNING
1429 || connection->cstate < C_WF_REPORT_PARAMS;
1430
1431 if (drop_it)
1432 return true;
1433
1434 drop_it = !--connection->ko_count;
1435 if (!drop_it) {
1436 conn_err(connection, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1437 current->comm, current->pid, connection->ko_count);
1438 request_ping(connection);
1439 }
1440
1441 return drop_it; /* && (device->state == R_PRIMARY) */;
1442 }
1443
1444 static void drbd_update_congested(struct drbd_connection *connection)
1445 {
1446 struct sock *sk = connection->data.socket->sk;
1447 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
1448 set_bit(NET_CONGESTED, &connection->flags);
1449 }
1450
1451 /* The idea of sendpage seems to be to put some kind of reference
1452 * to the page into the skb, and to hand it over to the NIC. In
1453 * this process get_page() gets called.
1454 *
1455 * As soon as the page was really sent over the network put_page()
1456 * gets called by some part of the network layer. [ NIC driver? ]
1457 *
1458 * [ get_page() / put_page() increment/decrement the count. If count
1459 * reaches 0 the page will be freed. ]
1460 *
1461 * This works nicely with pages from FSs.
1462 * But this means that in protocol A we might signal IO completion too early!
1463 *
1464 * In order not to corrupt data during a resync we must make sure
1465 * that we do not reuse our own buffer pages (EEs) to early, therefore
1466 * we have the net_ee list.
1467 *
1468 * XFS seems to have problems, still, it submits pages with page_count == 0!
1469 * As a workaround, we disable sendpage on pages
1470 * with page_count == 0 or PageSlab.
1471 */
1472 static int _drbd_no_send_page(struct drbd_device *device, struct page *page,
1473 int offset, size_t size, unsigned msg_flags)
1474 {
1475 struct socket *socket;
1476 void *addr;
1477 int err;
1478
1479 socket = first_peer_device(device)->connection->data.socket;
1480 addr = kmap(page) + offset;
1481 err = drbd_send_all(first_peer_device(device)->connection, socket, addr, size, msg_flags);
1482 kunmap(page);
1483 if (!err)
1484 device->send_cnt += size >> 9;
1485 return err;
1486 }
1487
1488 static int _drbd_send_page(struct drbd_device *device, struct page *page,
1489 int offset, size_t size, unsigned msg_flags)
1490 {
1491 struct socket *socket = first_peer_device(device)->connection->data.socket;
1492 mm_segment_t oldfs = get_fs();
1493 int len = size;
1494 int err = -EIO;
1495
1496 /* e.g. XFS meta- & log-data is in slab pages, which have a
1497 * page_count of 0 and/or have PageSlab() set.
1498 * we cannot use send_page for those, as that does get_page();
1499 * put_page(); and would cause either a VM_BUG directly, or
1500 * __page_cache_release a page that would actually still be referenced
1501 * by someone, leading to some obscure delayed Oops somewhere else. */
1502 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
1503 return _drbd_no_send_page(device, page, offset, size, msg_flags);
1504
1505 msg_flags |= MSG_NOSIGNAL;
1506 drbd_update_congested(first_peer_device(device)->connection);
1507 set_fs(KERNEL_DS);
1508 do {
1509 int sent;
1510
1511 sent = socket->ops->sendpage(socket, page, offset, len, msg_flags);
1512 if (sent <= 0) {
1513 if (sent == -EAGAIN) {
1514 if (we_should_drop_the_connection(first_peer_device(device)->connection, socket))
1515 break;
1516 continue;
1517 }
1518 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1519 __func__, (int)size, len, sent);
1520 if (sent < 0)
1521 err = sent;
1522 break;
1523 }
1524 len -= sent;
1525 offset += sent;
1526 } while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/);
1527 set_fs(oldfs);
1528 clear_bit(NET_CONGESTED, &first_peer_device(device)->connection->flags);
1529
1530 if (len == 0) {
1531 err = 0;
1532 device->send_cnt += size >> 9;
1533 }
1534 return err;
1535 }
1536
1537 static int _drbd_send_bio(struct drbd_device *device, struct bio *bio)
1538 {
1539 struct bio_vec bvec;
1540 struct bvec_iter iter;
1541
1542 /* hint all but last page with MSG_MORE */
1543 bio_for_each_segment(bvec, bio, iter) {
1544 int err;
1545
1546 err = _drbd_no_send_page(device, bvec.bv_page,
1547 bvec.bv_offset, bvec.bv_len,
1548 bio_iter_last(bvec, iter)
1549 ? 0 : MSG_MORE);
1550 if (err)
1551 return err;
1552 }
1553 return 0;
1554 }
1555
1556 static int _drbd_send_zc_bio(struct drbd_device *device, struct bio *bio)
1557 {
1558 struct bio_vec bvec;
1559 struct bvec_iter iter;
1560
1561 /* hint all but last page with MSG_MORE */
1562 bio_for_each_segment(bvec, bio, iter) {
1563 int err;
1564
1565 err = _drbd_send_page(device, bvec.bv_page,
1566 bvec.bv_offset, bvec.bv_len,
1567 bio_iter_last(bvec, iter) ? 0 : MSG_MORE);
1568 if (err)
1569 return err;
1570 }
1571 return 0;
1572 }
1573
1574 static int _drbd_send_zc_ee(struct drbd_device *device,
1575 struct drbd_peer_request *peer_req)
1576 {
1577 struct page *page = peer_req->pages;
1578 unsigned len = peer_req->i.size;
1579 int err;
1580
1581 /* hint all but last page with MSG_MORE */
1582 page_chain_for_each(page) {
1583 unsigned l = min_t(unsigned, len, PAGE_SIZE);
1584
1585 err = _drbd_send_page(device, page, 0, l,
1586 page_chain_next(page) ? MSG_MORE : 0);
1587 if (err)
1588 return err;
1589 len -= l;
1590 }
1591 return 0;
1592 }
1593
1594 static u32 bio_flags_to_wire(struct drbd_device *device, unsigned long bi_rw)
1595 {
1596 if (first_peer_device(device)->connection->agreed_pro_version >= 95)
1597 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
1598 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1599 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1600 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1601 else
1602 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
1603 }
1604
1605 /* Used to send write requests
1606 * R_PRIMARY -> Peer (P_DATA)
1607 */
1608 int drbd_send_dblock(struct drbd_device *device, struct drbd_request *req)
1609 {
1610 struct drbd_socket *sock;
1611 struct p_data *p;
1612 unsigned int dp_flags = 0;
1613 int dgs;
1614 int err;
1615
1616 sock = &first_peer_device(device)->connection->data;
1617 p = drbd_prepare_command(device, sock);
1618 dgs = first_peer_device(device)->connection->integrity_tfm ?
1619 crypto_hash_digestsize(first_peer_device(device)->connection->integrity_tfm) : 0;
1620
1621 if (!p)
1622 return -EIO;
1623 p->sector = cpu_to_be64(req->i.sector);
1624 p->block_id = (unsigned long)req;
1625 p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
1626 dp_flags = bio_flags_to_wire(device, req->master_bio->bi_rw);
1627 if (device->state.conn >= C_SYNC_SOURCE &&
1628 device->state.conn <= C_PAUSED_SYNC_T)
1629 dp_flags |= DP_MAY_SET_IN_SYNC;
1630 if (first_peer_device(device)->connection->agreed_pro_version >= 100) {
1631 if (req->rq_state & RQ_EXP_RECEIVE_ACK)
1632 dp_flags |= DP_SEND_RECEIVE_ACK;
1633 if (req->rq_state & RQ_EXP_WRITE_ACK)
1634 dp_flags |= DP_SEND_WRITE_ACK;
1635 }
1636 p->dp_flags = cpu_to_be32(dp_flags);
1637 if (dgs)
1638 drbd_csum_bio(device, first_peer_device(device)->connection->integrity_tfm, req->master_bio, p + 1);
1639 err = __send_command(first_peer_device(device)->connection, device->vnr, sock, P_DATA, sizeof(*p) + dgs, NULL, req->i.size);
1640 if (!err) {
1641 /* For protocol A, we have to memcpy the payload into
1642 * socket buffers, as we may complete right away
1643 * as soon as we handed it over to tcp, at which point the data
1644 * pages may become invalid.
1645 *
1646 * For data-integrity enabled, we copy it as well, so we can be
1647 * sure that even if the bio pages may still be modified, it
1648 * won't change the data on the wire, thus if the digest checks
1649 * out ok after sending on this side, but does not fit on the
1650 * receiving side, we sure have detected corruption elsewhere.
1651 */
1652 if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || dgs)
1653 err = _drbd_send_bio(device, req->master_bio);
1654 else
1655 err = _drbd_send_zc_bio(device, req->master_bio);
1656
1657 /* double check digest, sometimes buffers have been modified in flight. */
1658 if (dgs > 0 && dgs <= 64) {
1659 /* 64 byte, 512 bit, is the largest digest size
1660 * currently supported in kernel crypto. */
1661 unsigned char digest[64];
1662 drbd_csum_bio(device, first_peer_device(device)->connection->integrity_tfm, req->master_bio, digest);
1663 if (memcmp(p + 1, digest, dgs)) {
1664 dev_warn(DEV,
1665 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
1666 (unsigned long long)req->i.sector, req->i.size);
1667 }
1668 } /* else if (dgs > 64) {
1669 ... Be noisy about digest too large ...
1670 } */
1671 }
1672 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
1673
1674 return err;
1675 }
1676
1677 /* answer packet, used to send data back for read requests:
1678 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1679 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1680 */
1681 int drbd_send_block(struct drbd_device *device, enum drbd_packet cmd,
1682 struct drbd_peer_request *peer_req)
1683 {
1684 struct drbd_socket *sock;
1685 struct p_data *p;
1686 int err;
1687 int dgs;
1688
1689 sock = &first_peer_device(device)->connection->data;
1690 p = drbd_prepare_command(device, sock);
1691
1692 dgs = first_peer_device(device)->connection->integrity_tfm ?
1693 crypto_hash_digestsize(first_peer_device(device)->connection->integrity_tfm) : 0;
1694
1695 if (!p)
1696 return -EIO;
1697 p->sector = cpu_to_be64(peer_req->i.sector);
1698 p->block_id = peer_req->block_id;
1699 p->seq_num = 0; /* unused */
1700 p->dp_flags = 0;
1701 if (dgs)
1702 drbd_csum_ee(device, first_peer_device(device)->connection->integrity_tfm, peer_req, p + 1);
1703 err = __send_command(first_peer_device(device)->connection, device->vnr, sock, cmd, sizeof(*p) + dgs, NULL, peer_req->i.size);
1704 if (!err)
1705 err = _drbd_send_zc_ee(device, peer_req);
1706 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
1707
1708 return err;
1709 }
1710
1711 int drbd_send_out_of_sync(struct drbd_device *device, struct drbd_request *req)
1712 {
1713 struct drbd_socket *sock;
1714 struct p_block_desc *p;
1715
1716 sock = &first_peer_device(device)->connection->data;
1717 p = drbd_prepare_command(device, sock);
1718 if (!p)
1719 return -EIO;
1720 p->sector = cpu_to_be64(req->i.sector);
1721 p->blksize = cpu_to_be32(req->i.size);
1722 return drbd_send_command(device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
1723 }
1724
1725 /*
1726 drbd_send distinguishes two cases:
1727
1728 Packets sent via the data socket "sock"
1729 and packets sent via the meta data socket "msock"
1730
1731 sock msock
1732 -----------------+-------------------------+------------------------------
1733 timeout conf.timeout / 2 conf.timeout / 2
1734 timeout action send a ping via msock Abort communication
1735 and close all sockets
1736 */
1737
1738 /*
1739 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1740 */
1741 int drbd_send(struct drbd_connection *connection, struct socket *sock,
1742 void *buf, size_t size, unsigned msg_flags)
1743 {
1744 struct kvec iov;
1745 struct msghdr msg;
1746 int rv, sent = 0;
1747
1748 if (!sock)
1749 return -EBADR;
1750
1751 /* THINK if (signal_pending) return ... ? */
1752
1753 iov.iov_base = buf;
1754 iov.iov_len = size;
1755
1756 msg.msg_name = NULL;
1757 msg.msg_namelen = 0;
1758 msg.msg_control = NULL;
1759 msg.msg_controllen = 0;
1760 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1761
1762 if (sock == connection->data.socket) {
1763 rcu_read_lock();
1764 connection->ko_count = rcu_dereference(connection->net_conf)->ko_count;
1765 rcu_read_unlock();
1766 drbd_update_congested(connection);
1767 }
1768 do {
1769 /* STRANGE
1770 * tcp_sendmsg does _not_ use its size parameter at all ?
1771 *
1772 * -EAGAIN on timeout, -EINTR on signal.
1773 */
1774 /* THINK
1775 * do we need to block DRBD_SIG if sock == &meta.socket ??
1776 * otherwise wake_asender() might interrupt some send_*Ack !
1777 */
1778 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1779 if (rv == -EAGAIN) {
1780 if (we_should_drop_the_connection(connection, sock))
1781 break;
1782 else
1783 continue;
1784 }
1785 if (rv == -EINTR) {
1786 flush_signals(current);
1787 rv = 0;
1788 }
1789 if (rv < 0)
1790 break;
1791 sent += rv;
1792 iov.iov_base += rv;
1793 iov.iov_len -= rv;
1794 } while (sent < size);
1795
1796 if (sock == connection->data.socket)
1797 clear_bit(NET_CONGESTED, &connection->flags);
1798
1799 if (rv <= 0) {
1800 if (rv != -EAGAIN) {
1801 conn_err(connection, "%s_sendmsg returned %d\n",
1802 sock == connection->meta.socket ? "msock" : "sock",
1803 rv);
1804 conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
1805 } else
1806 conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
1807 }
1808
1809 return sent;
1810 }
1811
1812 /**
1813 * drbd_send_all - Send an entire buffer
1814 *
1815 * Returns 0 upon success and a negative error value otherwise.
1816 */
1817 int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer,
1818 size_t size, unsigned msg_flags)
1819 {
1820 int err;
1821
1822 err = drbd_send(connection, sock, buffer, size, msg_flags);
1823 if (err < 0)
1824 return err;
1825 if (err != size)
1826 return -EIO;
1827 return 0;
1828 }
1829
1830 static int drbd_open(struct block_device *bdev, fmode_t mode)
1831 {
1832 struct drbd_device *device = bdev->bd_disk->private_data;
1833 unsigned long flags;
1834 int rv = 0;
1835
1836 mutex_lock(&drbd_main_mutex);
1837 spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
1838 /* to have a stable device->state.role
1839 * and no race with updating open_cnt */
1840
1841 if (device->state.role != R_PRIMARY) {
1842 if (mode & FMODE_WRITE)
1843 rv = -EROFS;
1844 else if (!allow_oos)
1845 rv = -EMEDIUMTYPE;
1846 }
1847
1848 if (!rv)
1849 device->open_cnt++;
1850 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
1851 mutex_unlock(&drbd_main_mutex);
1852
1853 return rv;
1854 }
1855
1856 static void drbd_release(struct gendisk *gd, fmode_t mode)
1857 {
1858 struct drbd_device *device = gd->private_data;
1859 mutex_lock(&drbd_main_mutex);
1860 device->open_cnt--;
1861 mutex_unlock(&drbd_main_mutex);
1862 }
1863
1864 static void drbd_set_defaults(struct drbd_device *device)
1865 {
1866 /* Beware! The actual layout differs
1867 * between big endian and little endian */
1868 device->state = (union drbd_dev_state) {
1869 { .role = R_SECONDARY,
1870 .peer = R_UNKNOWN,
1871 .conn = C_STANDALONE,
1872 .disk = D_DISKLESS,
1873 .pdsk = D_UNKNOWN,
1874 } };
1875 }
1876
1877 void drbd_init_set_defaults(struct drbd_device *device)
1878 {
1879 /* the memset(,0,) did most of this.
1880 * note: only assignments, no allocation in here */
1881
1882 drbd_set_defaults(device);
1883
1884 atomic_set(&device->ap_bio_cnt, 0);
1885 atomic_set(&device->ap_pending_cnt, 0);
1886 atomic_set(&device->rs_pending_cnt, 0);
1887 atomic_set(&device->unacked_cnt, 0);
1888 atomic_set(&device->local_cnt, 0);
1889 atomic_set(&device->pp_in_use_by_net, 0);
1890 atomic_set(&device->rs_sect_in, 0);
1891 atomic_set(&device->rs_sect_ev, 0);
1892 atomic_set(&device->ap_in_flight, 0);
1893 atomic_set(&device->md_io_in_use, 0);
1894
1895 mutex_init(&device->own_state_mutex);
1896 device->state_mutex = &device->own_state_mutex;
1897
1898 spin_lock_init(&device->al_lock);
1899 spin_lock_init(&device->peer_seq_lock);
1900
1901 INIT_LIST_HEAD(&device->active_ee);
1902 INIT_LIST_HEAD(&device->sync_ee);
1903 INIT_LIST_HEAD(&device->done_ee);
1904 INIT_LIST_HEAD(&device->read_ee);
1905 INIT_LIST_HEAD(&device->net_ee);
1906 INIT_LIST_HEAD(&device->resync_reads);
1907 INIT_LIST_HEAD(&device->resync_work.list);
1908 INIT_LIST_HEAD(&device->unplug_work.list);
1909 INIT_LIST_HEAD(&device->go_diskless.list);
1910 INIT_LIST_HEAD(&device->md_sync_work.list);
1911 INIT_LIST_HEAD(&device->start_resync_work.list);
1912 INIT_LIST_HEAD(&device->bm_io_work.w.list);
1913
1914 device->resync_work.cb = w_resync_timer;
1915 device->unplug_work.cb = w_send_write_hint;
1916 device->go_diskless.cb = w_go_diskless;
1917 device->md_sync_work.cb = w_md_sync;
1918 device->bm_io_work.w.cb = w_bitmap_io;
1919 device->start_resync_work.cb = w_start_resync;
1920
1921 device->resync_work.device = device;
1922 device->unplug_work.device = device;
1923 device->go_diskless.device = device;
1924 device->md_sync_work.device = device;
1925 device->bm_io_work.w.device = device;
1926 device->start_resync_work.device = device;
1927
1928 init_timer(&device->resync_timer);
1929 init_timer(&device->md_sync_timer);
1930 init_timer(&device->start_resync_timer);
1931 init_timer(&device->request_timer);
1932 device->resync_timer.function = resync_timer_fn;
1933 device->resync_timer.data = (unsigned long) device;
1934 device->md_sync_timer.function = md_sync_timer_fn;
1935 device->md_sync_timer.data = (unsigned long) device;
1936 device->start_resync_timer.function = start_resync_timer_fn;
1937 device->start_resync_timer.data = (unsigned long) device;
1938 device->request_timer.function = request_timer_fn;
1939 device->request_timer.data = (unsigned long) device;
1940
1941 init_waitqueue_head(&device->misc_wait);
1942 init_waitqueue_head(&device->state_wait);
1943 init_waitqueue_head(&device->ee_wait);
1944 init_waitqueue_head(&device->al_wait);
1945 init_waitqueue_head(&device->seq_wait);
1946
1947 device->resync_wenr = LC_FREE;
1948 device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1949 device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1950 }
1951
1952 void drbd_device_cleanup(struct drbd_device *device)
1953 {
1954 int i;
1955 if (first_peer_device(device)->connection->receiver.t_state != NONE)
1956 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
1957 first_peer_device(device)->connection->receiver.t_state);
1958
1959 device->al_writ_cnt =
1960 device->bm_writ_cnt =
1961 device->read_cnt =
1962 device->recv_cnt =
1963 device->send_cnt =
1964 device->writ_cnt =
1965 device->p_size =
1966 device->rs_start =
1967 device->rs_total =
1968 device->rs_failed = 0;
1969 device->rs_last_events = 0;
1970 device->rs_last_sect_ev = 0;
1971 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1972 device->rs_mark_left[i] = 0;
1973 device->rs_mark_time[i] = 0;
1974 }
1975 D_ASSERT(first_peer_device(device)->connection->net_conf == NULL);
1976
1977 drbd_set_my_capacity(device, 0);
1978 if (device->bitmap) {
1979 /* maybe never allocated. */
1980 drbd_bm_resize(device, 0, 1);
1981 drbd_bm_cleanup(device);
1982 }
1983
1984 drbd_free_bc(device->ldev);
1985 device->ldev = NULL;
1986
1987 clear_bit(AL_SUSPENDED, &device->flags);
1988
1989 D_ASSERT(list_empty(&device->active_ee));
1990 D_ASSERT(list_empty(&device->sync_ee));
1991 D_ASSERT(list_empty(&device->done_ee));
1992 D_ASSERT(list_empty(&device->read_ee));
1993 D_ASSERT(list_empty(&device->net_ee));
1994 D_ASSERT(list_empty(&device->resync_reads));
1995 D_ASSERT(list_empty(&first_peer_device(device)->connection->sender_work.q));
1996 D_ASSERT(list_empty(&device->resync_work.list));
1997 D_ASSERT(list_empty(&device->unplug_work.list));
1998 D_ASSERT(list_empty(&device->go_diskless.list));
1999
2000 drbd_set_defaults(device);
2001 }
2002
2003
2004 static void drbd_destroy_mempools(void)
2005 {
2006 struct page *page;
2007
2008 while (drbd_pp_pool) {
2009 page = drbd_pp_pool;
2010 drbd_pp_pool = (struct page *)page_private(page);
2011 __free_page(page);
2012 drbd_pp_vacant--;
2013 }
2014
2015 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2016
2017 if (drbd_md_io_bio_set)
2018 bioset_free(drbd_md_io_bio_set);
2019 if (drbd_md_io_page_pool)
2020 mempool_destroy(drbd_md_io_page_pool);
2021 if (drbd_ee_mempool)
2022 mempool_destroy(drbd_ee_mempool);
2023 if (drbd_request_mempool)
2024 mempool_destroy(drbd_request_mempool);
2025 if (drbd_ee_cache)
2026 kmem_cache_destroy(drbd_ee_cache);
2027 if (drbd_request_cache)
2028 kmem_cache_destroy(drbd_request_cache);
2029 if (drbd_bm_ext_cache)
2030 kmem_cache_destroy(drbd_bm_ext_cache);
2031 if (drbd_al_ext_cache)
2032 kmem_cache_destroy(drbd_al_ext_cache);
2033
2034 drbd_md_io_bio_set = NULL;
2035 drbd_md_io_page_pool = NULL;
2036 drbd_ee_mempool = NULL;
2037 drbd_request_mempool = NULL;
2038 drbd_ee_cache = NULL;
2039 drbd_request_cache = NULL;
2040 drbd_bm_ext_cache = NULL;
2041 drbd_al_ext_cache = NULL;
2042
2043 return;
2044 }
2045
2046 static int drbd_create_mempools(void)
2047 {
2048 struct page *page;
2049 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
2050 int i;
2051
2052 /* prepare our caches and mempools */
2053 drbd_request_mempool = NULL;
2054 drbd_ee_cache = NULL;
2055 drbd_request_cache = NULL;
2056 drbd_bm_ext_cache = NULL;
2057 drbd_al_ext_cache = NULL;
2058 drbd_pp_pool = NULL;
2059 drbd_md_io_page_pool = NULL;
2060 drbd_md_io_bio_set = NULL;
2061
2062 /* caches */
2063 drbd_request_cache = kmem_cache_create(
2064 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
2065 if (drbd_request_cache == NULL)
2066 goto Enomem;
2067
2068 drbd_ee_cache = kmem_cache_create(
2069 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
2070 if (drbd_ee_cache == NULL)
2071 goto Enomem;
2072
2073 drbd_bm_ext_cache = kmem_cache_create(
2074 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2075 if (drbd_bm_ext_cache == NULL)
2076 goto Enomem;
2077
2078 drbd_al_ext_cache = kmem_cache_create(
2079 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2080 if (drbd_al_ext_cache == NULL)
2081 goto Enomem;
2082
2083 /* mempools */
2084 drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0);
2085 if (drbd_md_io_bio_set == NULL)
2086 goto Enomem;
2087
2088 drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
2089 if (drbd_md_io_page_pool == NULL)
2090 goto Enomem;
2091
2092 drbd_request_mempool = mempool_create(number,
2093 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2094 if (drbd_request_mempool == NULL)
2095 goto Enomem;
2096
2097 drbd_ee_mempool = mempool_create(number,
2098 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
2099 if (drbd_ee_mempool == NULL)
2100 goto Enomem;
2101
2102 /* drbd's page pool */
2103 spin_lock_init(&drbd_pp_lock);
2104
2105 for (i = 0; i < number; i++) {
2106 page = alloc_page(GFP_HIGHUSER);
2107 if (!page)
2108 goto Enomem;
2109 set_page_private(page, (unsigned long)drbd_pp_pool);
2110 drbd_pp_pool = page;
2111 }
2112 drbd_pp_vacant = number;
2113
2114 return 0;
2115
2116 Enomem:
2117 drbd_destroy_mempools(); /* in case we allocated some */
2118 return -ENOMEM;
2119 }
2120
2121 static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2122 void *unused)
2123 {
2124 /* just so we have it. you never know what interesting things we
2125 * might want to do here some day...
2126 */
2127
2128 return NOTIFY_DONE;
2129 }
2130
2131 static struct notifier_block drbd_notifier = {
2132 .notifier_call = drbd_notify_sys,
2133 };
2134
2135 static void drbd_release_all_peer_reqs(struct drbd_device *device)
2136 {
2137 int rr;
2138
2139 rr = drbd_free_peer_reqs(device, &device->active_ee);
2140 if (rr)
2141 dev_err(DEV, "%d EEs in active list found!\n", rr);
2142
2143 rr = drbd_free_peer_reqs(device, &device->sync_ee);
2144 if (rr)
2145 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2146
2147 rr = drbd_free_peer_reqs(device, &device->read_ee);
2148 if (rr)
2149 dev_err(DEV, "%d EEs in read list found!\n", rr);
2150
2151 rr = drbd_free_peer_reqs(device, &device->done_ee);
2152 if (rr)
2153 dev_err(DEV, "%d EEs in done list found!\n", rr);
2154
2155 rr = drbd_free_peer_reqs(device, &device->net_ee);
2156 if (rr)
2157 dev_err(DEV, "%d EEs in net list found!\n", rr);
2158 }
2159
2160 /* caution. no locking. */
2161 void drbd_destroy_device(struct kref *kref)
2162 {
2163 struct drbd_device *device = container_of(kref, struct drbd_device, kref);
2164 struct drbd_connection *connection = first_peer_device(device)->connection;
2165
2166 del_timer_sync(&device->request_timer);
2167
2168 /* paranoia asserts */
2169 D_ASSERT(device->open_cnt == 0);
2170 /* end paranoia asserts */
2171
2172 /* cleanup stuff that may have been allocated during
2173 * device (re-)configuration or state changes */
2174
2175 if (device->this_bdev)
2176 bdput(device->this_bdev);
2177
2178 drbd_free_bc(device->ldev);
2179 device->ldev = NULL;
2180
2181 drbd_release_all_peer_reqs(device);
2182
2183 lc_destroy(device->act_log);
2184 lc_destroy(device->resync);
2185
2186 kfree(device->p_uuid);
2187 /* device->p_uuid = NULL; */
2188
2189 if (device->bitmap) /* should no longer be there. */
2190 drbd_bm_cleanup(device);
2191 __free_page(device->md_io_page);
2192 put_disk(device->vdisk);
2193 blk_cleanup_queue(device->rq_queue);
2194 kfree(device->rs_plan_s);
2195 kfree(first_peer_device(device));
2196 kfree(device);
2197
2198 kref_put(&connection->kref, drbd_destroy_connection);
2199 }
2200
2201 /* One global retry thread, if we need to push back some bio and have it
2202 * reinserted through our make request function.
2203 */
2204 static struct retry_worker {
2205 struct workqueue_struct *wq;
2206 struct work_struct worker;
2207
2208 spinlock_t lock;
2209 struct list_head writes;
2210 } retry;
2211
2212 static void do_retry(struct work_struct *ws)
2213 {
2214 struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
2215 LIST_HEAD(writes);
2216 struct drbd_request *req, *tmp;
2217
2218 spin_lock_irq(&retry->lock);
2219 list_splice_init(&retry->writes, &writes);
2220 spin_unlock_irq(&retry->lock);
2221
2222 list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
2223 struct drbd_device *device = req->w.device;
2224 struct bio *bio = req->master_bio;
2225 unsigned long start_time = req->start_time;
2226 bool expected;
2227
2228 expected =
2229 expect(atomic_read(&req->completion_ref) == 0) &&
2230 expect(req->rq_state & RQ_POSTPONED) &&
2231 expect((req->rq_state & RQ_LOCAL_PENDING) == 0 ||
2232 (req->rq_state & RQ_LOCAL_ABORTED) != 0);
2233
2234 if (!expected)
2235 dev_err(DEV, "req=%p completion_ref=%d rq_state=%x\n",
2236 req, atomic_read(&req->completion_ref),
2237 req->rq_state);
2238
2239 /* We still need to put one kref associated with the
2240 * "completion_ref" going zero in the code path that queued it
2241 * here. The request object may still be referenced by a
2242 * frozen local req->private_bio, in case we force-detached.
2243 */
2244 kref_put(&req->kref, drbd_req_destroy);
2245
2246 /* A single suspended or otherwise blocking device may stall
2247 * all others as well. Fortunately, this code path is to
2248 * recover from a situation that "should not happen":
2249 * concurrent writes in multi-primary setup.
2250 * In a "normal" lifecycle, this workqueue is supposed to be
2251 * destroyed without ever doing anything.
2252 * If it turns out to be an issue anyways, we can do per
2253 * resource (replication group) or per device (minor) retry
2254 * workqueues instead.
2255 */
2256
2257 /* We are not just doing generic_make_request(),
2258 * as we want to keep the start_time information. */
2259 inc_ap_bio(device);
2260 __drbd_make_request(device, bio, start_time);
2261 }
2262 }
2263
2264 void drbd_restart_request(struct drbd_request *req)
2265 {
2266 unsigned long flags;
2267 spin_lock_irqsave(&retry.lock, flags);
2268 list_move_tail(&req->tl_requests, &retry.writes);
2269 spin_unlock_irqrestore(&retry.lock, flags);
2270
2271 /* Drop the extra reference that would otherwise
2272 * have been dropped by complete_master_bio.
2273 * do_retry() needs to grab a new one. */
2274 dec_ap_bio(req->w.device);
2275
2276 queue_work(retry.wq, &retry.worker);
2277 }
2278
2279
2280 static void drbd_cleanup(void)
2281 {
2282 unsigned int i;
2283 struct drbd_device *device;
2284 struct drbd_connection *connection, *tmp;
2285
2286 unregister_reboot_notifier(&drbd_notifier);
2287
2288 /* first remove proc,
2289 * drbdsetup uses it's presence to detect
2290 * whether DRBD is loaded.
2291 * If we would get stuck in proc removal,
2292 * but have netlink already deregistered,
2293 * some drbdsetup commands may wait forever
2294 * for an answer.
2295 */
2296 if (drbd_proc)
2297 remove_proc_entry("drbd", NULL);
2298
2299 if (retry.wq)
2300 destroy_workqueue(retry.wq);
2301
2302 drbd_genl_unregister();
2303
2304 idr_for_each_entry(&drbd_devices, device, i) {
2305 idr_remove(&drbd_devices, device_to_minor(device));
2306 idr_remove(&first_peer_device(device)->connection->volumes, device->vnr);
2307 destroy_workqueue(device->submit.wq);
2308 del_gendisk(device->vdisk);
2309 /* synchronize_rcu(); No other threads running at this point */
2310 kref_put(&device->kref, drbd_destroy_device);
2311 }
2312
2313 /* not _rcu since, no other updater anymore. Genl already unregistered */
2314 list_for_each_entry_safe(connection, tmp, &drbd_connections, connections) {
2315 list_del(&connection->connections); /* not _rcu no proc, not other threads */
2316 /* synchronize_rcu(); */
2317 kref_put(&connection->kref, drbd_destroy_connection);
2318 }
2319
2320 drbd_destroy_mempools();
2321 unregister_blkdev(DRBD_MAJOR, "drbd");
2322
2323 idr_destroy(&drbd_devices);
2324
2325 printk(KERN_INFO "drbd: module cleanup done.\n");
2326 }
2327
2328 /**
2329 * drbd_congested() - Callback for the flusher thread
2330 * @congested_data: User data
2331 * @bdi_bits: Bits the BDI flusher thread is currently interested in
2332 *
2333 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2334 */
2335 static int drbd_congested(void *congested_data, int bdi_bits)
2336 {
2337 struct drbd_device *device = congested_data;
2338 struct request_queue *q;
2339 char reason = '-';
2340 int r = 0;
2341
2342 if (!may_inc_ap_bio(device)) {
2343 /* DRBD has frozen IO */
2344 r = bdi_bits;
2345 reason = 'd';
2346 goto out;
2347 }
2348
2349 if (test_bit(CALLBACK_PENDING, &first_peer_device(device)->connection->flags)) {
2350 r |= (1 << BDI_async_congested);
2351 /* Without good local data, we would need to read from remote,
2352 * and that would need the worker thread as well, which is
2353 * currently blocked waiting for that usermode helper to
2354 * finish.
2355 */
2356 if (!get_ldev_if_state(device, D_UP_TO_DATE))
2357 r |= (1 << BDI_sync_congested);
2358 else
2359 put_ldev(device);
2360 r &= bdi_bits;
2361 reason = 'c';
2362 goto out;
2363 }
2364
2365 if (get_ldev(device)) {
2366 q = bdev_get_queue(device->ldev->backing_bdev);
2367 r = bdi_congested(&q->backing_dev_info, bdi_bits);
2368 put_ldev(device);
2369 if (r)
2370 reason = 'b';
2371 }
2372
2373 if (bdi_bits & (1 << BDI_async_congested) &&
2374 test_bit(NET_CONGESTED, &first_peer_device(device)->connection->flags)) {
2375 r |= (1 << BDI_async_congested);
2376 reason = reason == 'b' ? 'a' : 'n';
2377 }
2378
2379 out:
2380 device->congestion_reason = reason;
2381 return r;
2382 }
2383
2384 static void drbd_init_workqueue(struct drbd_work_queue* wq)
2385 {
2386 spin_lock_init(&wq->q_lock);
2387 INIT_LIST_HEAD(&wq->q);
2388 init_waitqueue_head(&wq->q_wait);
2389 }
2390
2391 struct drbd_connection *conn_get_by_name(const char *name)
2392 {
2393 struct drbd_connection *connection;
2394
2395 if (!name || !name[0])
2396 return NULL;
2397
2398 rcu_read_lock();
2399 list_for_each_entry_rcu(connection, &drbd_connections, connections) {
2400 if (!strcmp(connection->name, name)) {
2401 kref_get(&connection->kref);
2402 goto found;
2403 }
2404 }
2405 connection = NULL;
2406 found:
2407 rcu_read_unlock();
2408 return connection;
2409 }
2410
2411 struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
2412 void *peer_addr, int peer_addr_len)
2413 {
2414 struct drbd_connection *connection;
2415
2416 rcu_read_lock();
2417 list_for_each_entry_rcu(connection, &drbd_connections, connections) {
2418 if (connection->my_addr_len == my_addr_len &&
2419 connection->peer_addr_len == peer_addr_len &&
2420 !memcmp(&connection->my_addr, my_addr, my_addr_len) &&
2421 !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) {
2422 kref_get(&connection->kref);
2423 goto found;
2424 }
2425 }
2426 connection = NULL;
2427 found:
2428 rcu_read_unlock();
2429 return connection;
2430 }
2431
2432 static int drbd_alloc_socket(struct drbd_socket *socket)
2433 {
2434 socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
2435 if (!socket->rbuf)
2436 return -ENOMEM;
2437 socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
2438 if (!socket->sbuf)
2439 return -ENOMEM;
2440 return 0;
2441 }
2442
2443 static void drbd_free_socket(struct drbd_socket *socket)
2444 {
2445 free_page((unsigned long) socket->sbuf);
2446 free_page((unsigned long) socket->rbuf);
2447 }
2448
2449 void conn_free_crypto(struct drbd_connection *connection)
2450 {
2451 drbd_free_sock(connection);
2452
2453 crypto_free_hash(connection->csums_tfm);
2454 crypto_free_hash(connection->verify_tfm);
2455 crypto_free_hash(connection->cram_hmac_tfm);
2456 crypto_free_hash(connection->integrity_tfm);
2457 crypto_free_hash(connection->peer_integrity_tfm);
2458 kfree(connection->int_dig_in);
2459 kfree(connection->int_dig_vv);
2460
2461 connection->csums_tfm = NULL;
2462 connection->verify_tfm = NULL;
2463 connection->cram_hmac_tfm = NULL;
2464 connection->integrity_tfm = NULL;
2465 connection->peer_integrity_tfm = NULL;
2466 connection->int_dig_in = NULL;
2467 connection->int_dig_vv = NULL;
2468 }
2469
2470 int set_resource_options(struct drbd_connection *connection, struct res_opts *res_opts)
2471 {
2472 cpumask_var_t new_cpu_mask;
2473 int err;
2474
2475 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
2476 return -ENOMEM;
2477 /*
2478 retcode = ERR_NOMEM;
2479 drbd_msg_put_info("unable to allocate cpumask");
2480 */
2481
2482 /* silently ignore cpu mask on UP kernel */
2483 if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
2484 /* FIXME: Get rid of constant 32 here */
2485 err = bitmap_parse(res_opts->cpu_mask, 32,
2486 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2487 if (err) {
2488 conn_warn(connection, "bitmap_parse() failed with %d\n", err);
2489 /* retcode = ERR_CPU_MASK_PARSE; */
2490 goto fail;
2491 }
2492 }
2493 connection->res_opts = *res_opts;
2494 if (!cpumask_equal(connection->cpu_mask, new_cpu_mask)) {
2495 cpumask_copy(connection->cpu_mask, new_cpu_mask);
2496 drbd_calc_cpu_mask(connection);
2497 connection->receiver.reset_cpu_mask = 1;
2498 connection->asender.reset_cpu_mask = 1;
2499 connection->worker.reset_cpu_mask = 1;
2500 }
2501 err = 0;
2502
2503 fail:
2504 free_cpumask_var(new_cpu_mask);
2505 return err;
2506
2507 }
2508
2509 /* caller must be under genl_lock() */
2510 struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts)
2511 {
2512 struct drbd_connection *connection;
2513
2514 connection = kzalloc(sizeof(struct drbd_connection), GFP_KERNEL);
2515 if (!connection)
2516 return NULL;
2517
2518 connection->name = kstrdup(name, GFP_KERNEL);
2519 if (!connection->name)
2520 goto fail;
2521
2522 if (drbd_alloc_socket(&connection->data))
2523 goto fail;
2524 if (drbd_alloc_socket(&connection->meta))
2525 goto fail;
2526
2527 if (!zalloc_cpumask_var(&connection->cpu_mask, GFP_KERNEL))
2528 goto fail;
2529
2530 if (set_resource_options(connection, res_opts))
2531 goto fail;
2532
2533 connection->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2534 if (!connection->current_epoch)
2535 goto fail;
2536
2537 INIT_LIST_HEAD(&connection->transfer_log);
2538
2539 INIT_LIST_HEAD(&connection->current_epoch->list);
2540 connection->epochs = 1;
2541 spin_lock_init(&connection->epoch_lock);
2542 connection->write_ordering = WO_bdev_flush;
2543
2544 connection->send.seen_any_write_yet = false;
2545 connection->send.current_epoch_nr = 0;
2546 connection->send.current_epoch_writes = 0;
2547
2548 connection->cstate = C_STANDALONE;
2549 mutex_init(&connection->cstate_mutex);
2550 spin_lock_init(&connection->req_lock);
2551 mutex_init(&connection->conf_update);
2552 init_waitqueue_head(&connection->ping_wait);
2553 idr_init(&connection->volumes);
2554
2555 drbd_init_workqueue(&connection->sender_work);
2556 mutex_init(&connection->data.mutex);
2557 mutex_init(&connection->meta.mutex);
2558
2559 drbd_thread_init(connection, &connection->receiver, drbdd_init, "receiver");
2560 drbd_thread_init(connection, &connection->worker, drbd_worker, "worker");
2561 drbd_thread_init(connection, &connection->asender, drbd_asender, "asender");
2562
2563 kref_init(&connection->kref);
2564 list_add_tail_rcu(&connection->connections, &drbd_connections);
2565
2566 return connection;
2567
2568 fail:
2569 kfree(connection->current_epoch);
2570 free_cpumask_var(connection->cpu_mask);
2571 drbd_free_socket(&connection->meta);
2572 drbd_free_socket(&connection->data);
2573 kfree(connection->name);
2574 kfree(connection);
2575
2576 return NULL;
2577 }
2578
2579 void drbd_destroy_connection(struct kref *kref)
2580 {
2581 struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref);
2582
2583 if (atomic_read(&connection->current_epoch->epoch_size) != 0)
2584 conn_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size));
2585 kfree(connection->current_epoch);
2586
2587 idr_destroy(&connection->volumes);
2588
2589 free_cpumask_var(connection->cpu_mask);
2590 drbd_free_socket(&connection->meta);
2591 drbd_free_socket(&connection->data);
2592 kfree(connection->name);
2593 kfree(connection->int_dig_in);
2594 kfree(connection->int_dig_vv);
2595 kfree(connection);
2596 }
2597
2598 static int init_submitter(struct drbd_device *device)
2599 {
2600 /* opencoded create_singlethread_workqueue(),
2601 * to be able to say "drbd%d", ..., minor */
2602 device->submit.wq = alloc_workqueue("drbd%u_submit",
2603 WQ_UNBOUND | WQ_MEM_RECLAIM, 1, device->minor);
2604 if (!device->submit.wq)
2605 return -ENOMEM;
2606
2607 INIT_WORK(&device->submit.worker, do_submit);
2608 spin_lock_init(&device->submit.lock);
2609 INIT_LIST_HEAD(&device->submit.writes);
2610 return 0;
2611 }
2612
2613 enum drbd_ret_code drbd_create_minor(struct drbd_connection *connection, unsigned int minor, int vnr)
2614 {
2615 struct drbd_device *device;
2616 struct drbd_peer_device *peer_device;
2617 struct gendisk *disk;
2618 struct request_queue *q;
2619 int vnr_got = vnr;
2620 int minor_got = minor;
2621 enum drbd_ret_code err = ERR_NOMEM;
2622
2623 device = minor_to_device(minor);
2624 if (device)
2625 return ERR_MINOR_EXISTS;
2626
2627 /* GFP_KERNEL, we are outside of all write-out paths */
2628 device = kzalloc(sizeof(struct drbd_device), GFP_KERNEL);
2629 if (!device)
2630 return ERR_NOMEM;
2631 peer_device = kzalloc(sizeof(struct drbd_peer_device), GFP_KERNEL);
2632 if (!peer_device)
2633 goto out_no_peer_device;
2634
2635 INIT_LIST_HEAD(&device->peer_devices);
2636 list_add(&peer_device->peer_devices, &device->peer_devices);
2637 kref_get(&connection->kref);
2638 peer_device->connection = connection;
2639 peer_device->device = device;
2640
2641 device->minor = minor;
2642 device->vnr = vnr;
2643
2644 drbd_init_set_defaults(device);
2645
2646 q = blk_alloc_queue(GFP_KERNEL);
2647 if (!q)
2648 goto out_no_q;
2649 device->rq_queue = q;
2650 q->queuedata = device;
2651
2652 disk = alloc_disk(1);
2653 if (!disk)
2654 goto out_no_disk;
2655 device->vdisk = disk;
2656
2657 set_disk_ro(disk, true);
2658
2659 disk->queue = q;
2660 disk->major = DRBD_MAJOR;
2661 disk->first_minor = minor;
2662 disk->fops = &drbd_ops;
2663 sprintf(disk->disk_name, "drbd%d", minor);
2664 disk->private_data = device;
2665
2666 device->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
2667 /* we have no partitions. we contain only ourselves. */
2668 device->this_bdev->bd_contains = device->this_bdev;
2669
2670 q->backing_dev_info.congested_fn = drbd_congested;
2671 q->backing_dev_info.congested_data = device;
2672
2673 blk_queue_make_request(q, drbd_make_request);
2674 blk_queue_flush(q, REQ_FLUSH | REQ_FUA);
2675 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2676 This triggers a max_bio_size message upon first attach or connect */
2677 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
2678 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2679 blk_queue_merge_bvec(q, drbd_merge_bvec);
2680 q->queue_lock = &first_peer_device(device)->connection->req_lock; /* needed since we use */
2681
2682 device->md_io_page = alloc_page(GFP_KERNEL);
2683 if (!device->md_io_page)
2684 goto out_no_io_page;
2685
2686 if (drbd_bm_init(device))
2687 goto out_no_bitmap;
2688 device->read_requests = RB_ROOT;
2689 device->write_requests = RB_ROOT;
2690
2691 minor_got = idr_alloc(&drbd_devices, device, minor, minor + 1, GFP_KERNEL);
2692 if (minor_got < 0) {
2693 if (minor_got == -ENOSPC) {
2694 err = ERR_MINOR_EXISTS;
2695 drbd_msg_put_info("requested minor exists already");
2696 }
2697 goto out_no_minor_idr;
2698 }
2699
2700 vnr_got = idr_alloc(&connection->volumes, device, vnr, vnr + 1, GFP_KERNEL);
2701 if (vnr_got < 0) {
2702 if (vnr_got == -ENOSPC) {
2703 err = ERR_INVALID_REQUEST;
2704 drbd_msg_put_info("requested volume exists already");
2705 }
2706 goto out_idr_remove_minor;
2707 }
2708
2709 if (init_submitter(device)) {
2710 err = ERR_NOMEM;
2711 drbd_msg_put_info("unable to create submit workqueue");
2712 goto out_idr_remove_vol;
2713 }
2714
2715 add_disk(disk);
2716 kref_init(&device->kref); /* one ref for both idrs and the the add_disk */
2717
2718 /* inherit the connection state */
2719 device->state.conn = connection->cstate;
2720 if (device->state.conn == C_WF_REPORT_PARAMS)
2721 drbd_connected(device);
2722
2723 return NO_ERROR;
2724
2725 out_idr_remove_vol:
2726 idr_remove(&connection->volumes, vnr_got);
2727 out_idr_remove_minor:
2728 idr_remove(&drbd_devices, minor_got);
2729 synchronize_rcu();
2730 out_no_minor_idr:
2731 drbd_bm_cleanup(device);
2732 out_no_bitmap:
2733 __free_page(device->md_io_page);
2734 out_no_io_page:
2735 put_disk(disk);
2736 out_no_disk:
2737 blk_cleanup_queue(q);
2738 out_no_q:
2739 kref_put(&connection->kref, drbd_destroy_connection);
2740 out_no_peer_device:
2741 kfree(device);
2742 return err;
2743 }
2744
2745 int __init drbd_init(void)
2746 {
2747 int err;
2748
2749 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
2750 printk(KERN_ERR
2751 "drbd: invalid minor_count (%d)\n", minor_count);
2752 #ifdef MODULE
2753 return -EINVAL;
2754 #else
2755 minor_count = DRBD_MINOR_COUNT_DEF;
2756 #endif
2757 }
2758
2759 err = register_blkdev(DRBD_MAJOR, "drbd");
2760 if (err) {
2761 printk(KERN_ERR
2762 "drbd: unable to register block device major %d\n",
2763 DRBD_MAJOR);
2764 return err;
2765 }
2766
2767 register_reboot_notifier(&drbd_notifier);
2768
2769 /*
2770 * allocate all necessary structs
2771 */
2772 init_waitqueue_head(&drbd_pp_wait);
2773
2774 drbd_proc = NULL; /* play safe for drbd_cleanup */
2775 idr_init(&drbd_devices);
2776
2777 rwlock_init(&global_state_lock);
2778 INIT_LIST_HEAD(&drbd_connections);
2779
2780 err = drbd_genl_register();
2781 if (err) {
2782 printk(KERN_ERR "drbd: unable to register generic netlink family\n");
2783 goto fail;
2784 }
2785
2786 err = drbd_create_mempools();
2787 if (err)
2788 goto fail;
2789
2790 err = -ENOMEM;
2791 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
2792 if (!drbd_proc) {
2793 printk(KERN_ERR "drbd: unable to register proc file\n");
2794 goto fail;
2795 }
2796
2797 retry.wq = create_singlethread_workqueue("drbd-reissue");
2798 if (!retry.wq) {
2799 printk(KERN_ERR "drbd: unable to create retry workqueue\n");
2800 goto fail;
2801 }
2802 INIT_WORK(&retry.worker, do_retry);
2803 spin_lock_init(&retry.lock);
2804 INIT_LIST_HEAD(&retry.writes);
2805
2806 printk(KERN_INFO "drbd: initialized. "
2807 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2808 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2809 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2810 printk(KERN_INFO "drbd: registered as block device major %d\n",
2811 DRBD_MAJOR);
2812
2813 return 0; /* Success! */
2814
2815 fail:
2816 drbd_cleanup();
2817 if (err == -ENOMEM)
2818 printk(KERN_ERR "drbd: ran out of memory\n");
2819 else
2820 printk(KERN_ERR "drbd: initialization failure\n");
2821 return err;
2822 }
2823
2824 void drbd_free_bc(struct drbd_backing_dev *ldev)
2825 {
2826 if (ldev == NULL)
2827 return;
2828
2829 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2830 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2831
2832 kfree(ldev->disk_conf);
2833 kfree(ldev);
2834 }
2835
2836 void drbd_free_sock(struct drbd_connection *connection)
2837 {
2838 if (connection->data.socket) {
2839 mutex_lock(&connection->data.mutex);
2840 kernel_sock_shutdown(connection->data.socket, SHUT_RDWR);
2841 sock_release(connection->data.socket);
2842 connection->data.socket = NULL;
2843 mutex_unlock(&connection->data.mutex);
2844 }
2845 if (connection->meta.socket) {
2846 mutex_lock(&connection->meta.mutex);
2847 kernel_sock_shutdown(connection->meta.socket, SHUT_RDWR);
2848 sock_release(connection->meta.socket);
2849 connection->meta.socket = NULL;
2850 mutex_unlock(&connection->meta.mutex);
2851 }
2852 }
2853
2854 /* meta data management */
2855
2856 void conn_md_sync(struct drbd_connection *connection)
2857 {
2858 struct drbd_device *device;
2859 int vnr;
2860
2861 rcu_read_lock();
2862 idr_for_each_entry(&connection->volumes, device, vnr) {
2863 kref_get(&device->kref);
2864 rcu_read_unlock();
2865 drbd_md_sync(device);
2866 kref_put(&device->kref, drbd_destroy_device);
2867 rcu_read_lock();
2868 }
2869 rcu_read_unlock();
2870 }
2871
2872 /* aligned 4kByte */
2873 struct meta_data_on_disk {
2874 u64 la_size_sect; /* last agreed size. */
2875 u64 uuid[UI_SIZE]; /* UUIDs. */
2876 u64 device_uuid;
2877 u64 reserved_u64_1;
2878 u32 flags; /* MDF */
2879 u32 magic;
2880 u32 md_size_sect;
2881 u32 al_offset; /* offset to this block */
2882 u32 al_nr_extents; /* important for restoring the AL (userspace) */
2883 /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
2884 u32 bm_offset; /* offset to the bitmap, from here */
2885 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
2886 u32 la_peer_max_bio_size; /* last peer max_bio_size */
2887
2888 /* see al_tr_number_to_on_disk_sector() */
2889 u32 al_stripes;
2890 u32 al_stripe_size_4k;
2891
2892 u8 reserved_u8[4096 - (7*8 + 10*4)];
2893 } __packed;
2894
2895
2896
2897 void drbd_md_write(struct drbd_device *device, void *b)
2898 {
2899 struct meta_data_on_disk *buffer = b;
2900 sector_t sector;
2901 int i;
2902
2903 memset(buffer, 0, sizeof(*buffer));
2904
2905 buffer->la_size_sect = cpu_to_be64(drbd_get_capacity(device->this_bdev));
2906 for (i = UI_CURRENT; i < UI_SIZE; i++)
2907 buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
2908 buffer->flags = cpu_to_be32(device->ldev->md.flags);
2909 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
2910
2911 buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect);
2912 buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset);
2913 buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements);
2914 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
2915 buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid);
2916
2917 buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset);
2918 buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size);
2919
2920 buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes);
2921 buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k);
2922
2923 D_ASSERT(drbd_md_ss(device->ldev) == device->ldev->md.md_offset);
2924 sector = device->ldev->md.md_offset;
2925
2926 if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) {
2927 /* this was a try anyways ... */
2928 dev_err(DEV, "meta data update failed!\n");
2929 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
2930 }
2931 }
2932
2933 /**
2934 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2935 * @device: DRBD device.
2936 */
2937 void drbd_md_sync(struct drbd_device *device)
2938 {
2939 struct meta_data_on_disk *buffer;
2940
2941 /* Don't accidentally change the DRBD meta data layout. */
2942 BUILD_BUG_ON(UI_SIZE != 4);
2943 BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096);
2944
2945 del_timer(&device->md_sync_timer);
2946 /* timer may be rearmed by drbd_md_mark_dirty() now. */
2947 if (!test_and_clear_bit(MD_DIRTY, &device->flags))
2948 return;
2949
2950 /* We use here D_FAILED and not D_ATTACHING because we try to write
2951 * metadata even if we detach due to a disk failure! */
2952 if (!get_ldev_if_state(device, D_FAILED))
2953 return;
2954
2955 buffer = drbd_md_get_buffer(device);
2956 if (!buffer)
2957 goto out;
2958
2959 drbd_md_write(device, buffer);
2960
2961 /* Update device->ldev->md.la_size_sect,
2962 * since we updated it on metadata. */
2963 device->ldev->md.la_size_sect = drbd_get_capacity(device->this_bdev);
2964
2965 drbd_md_put_buffer(device);
2966 out:
2967 put_ldev(device);
2968 }
2969
2970 static int check_activity_log_stripe_size(struct drbd_device *device,
2971 struct meta_data_on_disk *on_disk,
2972 struct drbd_md *in_core)
2973 {
2974 u32 al_stripes = be32_to_cpu(on_disk->al_stripes);
2975 u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k);
2976 u64 al_size_4k;
2977
2978 /* both not set: default to old fixed size activity log */
2979 if (al_stripes == 0 && al_stripe_size_4k == 0) {
2980 al_stripes = 1;
2981 al_stripe_size_4k = MD_32kB_SECT/8;
2982 }
2983
2984 /* some paranoia plausibility checks */
2985
2986 /* we need both values to be set */
2987 if (al_stripes == 0 || al_stripe_size_4k == 0)
2988 goto err;
2989
2990 al_size_4k = (u64)al_stripes * al_stripe_size_4k;
2991
2992 /* Upper limit of activity log area, to avoid potential overflow
2993 * problems in al_tr_number_to_on_disk_sector(). As right now, more
2994 * than 72 * 4k blocks total only increases the amount of history,
2995 * limiting this arbitrarily to 16 GB is not a real limitation ;-) */
2996 if (al_size_4k > (16 * 1024 * 1024/4))
2997 goto err;
2998
2999 /* Lower limit: we need at least 8 transaction slots (32kB)
3000 * to not break existing setups */
3001 if (al_size_4k < MD_32kB_SECT/8)
3002 goto err;
3003
3004 in_core->al_stripe_size_4k = al_stripe_size_4k;
3005 in_core->al_stripes = al_stripes;
3006 in_core->al_size_4k = al_size_4k;
3007
3008 return 0;
3009 err:
3010 dev_err(DEV, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
3011 al_stripes, al_stripe_size_4k);
3012 return -EINVAL;
3013 }
3014
3015 static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev)
3016 {
3017 sector_t capacity = drbd_get_capacity(bdev->md_bdev);
3018 struct drbd_md *in_core = &bdev->md;
3019 s32 on_disk_al_sect;
3020 s32 on_disk_bm_sect;
3021
3022 /* The on-disk size of the activity log, calculated from offsets, and
3023 * the size of the activity log calculated from the stripe settings,
3024 * should match.
3025 * Though we could relax this a bit: it is ok, if the striped activity log
3026 * fits in the available on-disk activity log size.
3027 * Right now, that would break how resize is implemented.
3028 * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
3029 * of possible unused padding space in the on disk layout. */
3030 if (in_core->al_offset < 0) {
3031 if (in_core->bm_offset > in_core->al_offset)
3032 goto err;
3033 on_disk_al_sect = -in_core->al_offset;
3034 on_disk_bm_sect = in_core->al_offset - in_core->bm_offset;
3035 } else {
3036 if (in_core->al_offset != MD_4kB_SECT)
3037 goto err;
3038 if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT)
3039 goto err;
3040
3041 on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT;
3042 on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset;
3043 }
3044
3045 /* old fixed size meta data is exactly that: fixed. */
3046 if (in_core->meta_dev_idx >= 0) {
3047 if (in_core->md_size_sect != MD_128MB_SECT
3048 || in_core->al_offset != MD_4kB_SECT
3049 || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT
3050 || in_core->al_stripes != 1
3051 || in_core->al_stripe_size_4k != MD_32kB_SECT/8)
3052 goto err;
3053 }
3054
3055 if (capacity < in_core->md_size_sect)
3056 goto err;
3057 if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev))
3058 goto err;
3059
3060 /* should be aligned, and at least 32k */
3061 if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT))
3062 goto err;
3063
3064 /* should fit (for now: exactly) into the available on-disk space;
3065 * overflow prevention is in check_activity_log_stripe_size() above. */
3066 if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT)
3067 goto err;
3068
3069 /* again, should be aligned */
3070 if (in_core->bm_offset & 7)
3071 goto err;
3072
3073 /* FIXME check for device grow with flex external meta data? */
3074
3075 /* can the available bitmap space cover the last agreed device size? */
3076 if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512)
3077 goto err;
3078
3079 return 0;
3080
3081 err:
3082 dev_err(DEV, "meta data offsets don't make sense: idx=%d "
3083 "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
3084 "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
3085 in_core->meta_dev_idx,
3086 in_core->al_stripes, in_core->al_stripe_size_4k,
3087 in_core->al_offset, in_core->bm_offset, in_core->md_size_sect,
3088 (unsigned long long)in_core->la_size_sect,
3089 (unsigned long long)capacity);
3090
3091 return -EINVAL;
3092 }
3093
3094
3095 /**
3096 * drbd_md_read() - Reads in the meta data super block
3097 * @device: DRBD device.
3098 * @bdev: Device from which the meta data should be read in.
3099 *
3100 * Return NO_ERROR on success, and an enum drbd_ret_code in case
3101 * something goes wrong.
3102 *
3103 * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
3104 * even before @bdev is assigned to @device->ldev.
3105 */
3106 int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev)
3107 {
3108 struct meta_data_on_disk *buffer;
3109 u32 magic, flags;
3110 int i, rv = NO_ERROR;
3111
3112 if (device->state.disk != D_DISKLESS)
3113 return ERR_DISK_CONFIGURED;
3114
3115 buffer = drbd_md_get_buffer(device);
3116 if (!buffer)
3117 return ERR_NOMEM;
3118
3119 /* First, figure out where our meta data superblock is located,
3120 * and read it. */
3121 bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx;
3122 bdev->md.md_offset = drbd_md_ss(bdev);
3123
3124 if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset, READ)) {
3125 /* NOTE: can't do normal error processing here as this is
3126 called BEFORE disk is attached */
3127 dev_err(DEV, "Error while reading metadata.\n");
3128 rv = ERR_IO_MD_DISK;
3129 goto err;
3130 }
3131
3132 magic = be32_to_cpu(buffer->magic);
3133 flags = be32_to_cpu(buffer->flags);
3134 if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
3135 (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
3136 /* btw: that's Activity Log clean, not "all" clean. */
3137 dev_err(DEV, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
3138 rv = ERR_MD_UNCLEAN;
3139 goto err;
3140 }
3141
3142 rv = ERR_MD_INVALID;
3143 if (magic != DRBD_MD_MAGIC_08) {
3144 if (magic == DRBD_MD_MAGIC_07)
3145 dev_err(DEV, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
3146 else
3147 dev_err(DEV, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
3148 goto err;
3149 }
3150
3151 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
3152 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3153 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
3154 goto err;
3155 }
3156
3157
3158 /* convert to in_core endian */
3159 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect);
3160 for (i = UI_CURRENT; i < UI_SIZE; i++)
3161 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
3162 bdev->md.flags = be32_to_cpu(buffer->flags);
3163 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
3164
3165 bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect);
3166 bdev->md.al_offset = be32_to_cpu(buffer->al_offset);
3167 bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset);
3168
3169 if (check_activity_log_stripe_size(device, buffer, &bdev->md))
3170 goto err;
3171 if (check_offsets_and_sizes(device, bdev))
3172 goto err;
3173
3174 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
3175 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
3176 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
3177 goto err;
3178 }
3179 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
3180 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
3181 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
3182 goto err;
3183 }
3184
3185 rv = NO_ERROR;
3186
3187 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
3188 if (device->state.conn < C_CONNECTED) {
3189 unsigned int peer;
3190 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
3191 peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
3192 device->peer_max_bio_size = peer;
3193 }
3194 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
3195
3196 err:
3197 drbd_md_put_buffer(device);
3198
3199 return rv;
3200 }
3201
3202 /**
3203 * drbd_md_mark_dirty() - Mark meta data super block as dirty
3204 * @device: DRBD device.
3205 *
3206 * Call this function if you change anything that should be written to
3207 * the meta-data super block. This function sets MD_DIRTY, and starts a
3208 * timer that ensures that within five seconds you have to call drbd_md_sync().
3209 */
3210 #ifdef DEBUG
3211 void drbd_md_mark_dirty_(struct drbd_device *device, unsigned int line, const char *func)
3212 {
3213 if (!test_and_set_bit(MD_DIRTY, &device->flags)) {
3214 mod_timer(&device->md_sync_timer, jiffies + HZ);
3215 device->last_md_mark_dirty.line = line;
3216 device->last_md_mark_dirty.func = func;
3217 }
3218 }
3219 #else
3220 void drbd_md_mark_dirty(struct drbd_device *device)
3221 {
3222 if (!test_and_set_bit(MD_DIRTY, &device->flags))
3223 mod_timer(&device->md_sync_timer, jiffies + 5*HZ);
3224 }
3225 #endif
3226
3227 void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local)
3228 {
3229 int i;
3230
3231 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
3232 device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i];
3233 }
3234
3235 void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
3236 {
3237 if (idx == UI_CURRENT) {
3238 if (device->state.role == R_PRIMARY)
3239 val |= 1;
3240 else
3241 val &= ~((u64)1);
3242
3243 drbd_set_ed_uuid(device, val);
3244 }
3245
3246 device->ldev->md.uuid[idx] = val;
3247 drbd_md_mark_dirty(device);
3248 }
3249
3250 void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
3251 {
3252 unsigned long flags;
3253 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3254 __drbd_uuid_set(device, idx, val);
3255 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
3256 }
3257
3258 void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
3259 {
3260 unsigned long flags;
3261 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3262 if (device->ldev->md.uuid[idx]) {
3263 drbd_uuid_move_history(device);
3264 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx];
3265 }
3266 __drbd_uuid_set(device, idx, val);
3267 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
3268 }
3269
3270 /**
3271 * drbd_uuid_new_current() - Creates a new current UUID
3272 * @device: DRBD device.
3273 *
3274 * Creates a new current UUID, and rotates the old current UUID into
3275 * the bitmap slot. Causes an incremental resync upon next connect.
3276 */
3277 void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)
3278 {
3279 u64 val;
3280 unsigned long long bm_uuid;
3281
3282 get_random_bytes(&val, sizeof(u64));
3283
3284 spin_lock_irq(&device->ldev->md.uuid_lock);
3285 bm_uuid = device->ldev->md.uuid[UI_BITMAP];
3286
3287 if (bm_uuid)
3288 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
3289
3290 device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT];
3291 __drbd_uuid_set(device, UI_CURRENT, val);
3292 spin_unlock_irq(&device->ldev->md.uuid_lock);
3293
3294 drbd_print_uuids(device, "new current UUID");
3295 /* get it to stable storage _now_ */
3296 drbd_md_sync(device);
3297 }
3298
3299 void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)
3300 {
3301 unsigned long flags;
3302 if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
3303 return;
3304
3305 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3306 if (val == 0) {
3307 drbd_uuid_move_history(device);
3308 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
3309 device->ldev->md.uuid[UI_BITMAP] = 0;
3310 } else {
3311 unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP];
3312 if (bm_uuid)
3313 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
3314
3315 device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
3316 }
3317 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
3318
3319 drbd_md_mark_dirty(device);
3320 }
3321
3322 /**
3323 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3324 * @device: DRBD device.
3325 *
3326 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3327 */
3328 int drbd_bmio_set_n_write(struct drbd_device *device)
3329 {
3330 int rv = -EIO;
3331
3332 if (get_ldev_if_state(device, D_ATTACHING)) {
3333 drbd_md_set_flag(device, MDF_FULL_SYNC);
3334 drbd_md_sync(device);
3335 drbd_bm_set_all(device);
3336
3337 rv = drbd_bm_write(device);
3338
3339 if (!rv) {
3340 drbd_md_clear_flag(device, MDF_FULL_SYNC);
3341 drbd_md_sync(device);
3342 }
3343
3344 put_ldev(device);
3345 }
3346
3347 return rv;
3348 }
3349
3350 /**
3351 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3352 * @device: DRBD device.
3353 *
3354 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3355 */
3356 int drbd_bmio_clear_n_write(struct drbd_device *device)
3357 {
3358 int rv = -EIO;
3359
3360 drbd_resume_al(device);
3361 if (get_ldev_if_state(device, D_ATTACHING)) {
3362 drbd_bm_clear_all(device);
3363 rv = drbd_bm_write(device);
3364 put_ldev(device);
3365 }
3366
3367 return rv;
3368 }
3369
3370 static int w_bitmap_io(struct drbd_work *w, int unused)
3371 {
3372 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
3373 struct drbd_device *device = w->device;
3374 int rv = -EIO;
3375
3376 D_ASSERT(atomic_read(&device->ap_bio_cnt) == 0);
3377
3378 if (get_ldev(device)) {
3379 drbd_bm_lock(device, work->why, work->flags);
3380 rv = work->io_fn(device);
3381 drbd_bm_unlock(device);
3382 put_ldev(device);
3383 }
3384
3385 clear_bit_unlock(BITMAP_IO, &device->flags);
3386 wake_up(&device->misc_wait);
3387
3388 if (work->done)
3389 work->done(device, rv);
3390
3391 clear_bit(BITMAP_IO_QUEUED, &device->flags);
3392 work->why = NULL;
3393 work->flags = 0;
3394
3395 return 0;
3396 }
3397
3398 void drbd_ldev_destroy(struct drbd_device *device)
3399 {
3400 lc_destroy(device->resync);
3401 device->resync = NULL;
3402 lc_destroy(device->act_log);
3403 device->act_log = NULL;
3404 __no_warn(local,
3405 drbd_free_bc(device->ldev);
3406 device->ldev = NULL;);
3407
3408 clear_bit(GO_DISKLESS, &device->flags);
3409 }
3410
3411 static int w_go_diskless(struct drbd_work *w, int unused)
3412 {
3413 struct drbd_device *device = w->device;
3414
3415 D_ASSERT(device->state.disk == D_FAILED);
3416 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
3417 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
3418 * the protected members anymore, though, so once put_ldev reaches zero
3419 * again, it will be safe to free them. */
3420
3421 /* Try to write changed bitmap pages, read errors may have just
3422 * set some bits outside the area covered by the activity log.
3423 *
3424 * If we have an IO error during the bitmap writeout,
3425 * we will want a full sync next time, just in case.
3426 * (Do we want a specific meta data flag for this?)
3427 *
3428 * If that does not make it to stable storage either,
3429 * we cannot do anything about that anymore.
3430 *
3431 * We still need to check if both bitmap and ldev are present, we may
3432 * end up here after a failed attach, before ldev was even assigned.
3433 */
3434 if (device->bitmap && device->ldev) {
3435 /* An interrupted resync or similar is allowed to recounts bits
3436 * while we detach.
3437 * Any modifications would not be expected anymore, though.
3438 */
3439 if (drbd_bitmap_io_from_worker(device, drbd_bm_write,
3440 "detach", BM_LOCKED_TEST_ALLOWED)) {
3441 if (test_bit(WAS_READ_ERROR, &device->flags)) {
3442 drbd_md_set_flag(device, MDF_FULL_SYNC);
3443 drbd_md_sync(device);
3444 }
3445 }
3446 }
3447
3448 drbd_force_state(device, NS(disk, D_DISKLESS));
3449 return 0;
3450 }
3451
3452 /**
3453 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
3454 * @device: DRBD device.
3455 * @io_fn: IO callback to be called when bitmap IO is possible
3456 * @done: callback to be called after the bitmap IO was performed
3457 * @why: Descriptive text of the reason for doing the IO
3458 *
3459 * While IO on the bitmap happens we freeze application IO thus we ensure
3460 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3461 * called from worker context. It MUST NOT be used while a previous such
3462 * work is still pending!
3463 */
3464 void drbd_queue_bitmap_io(struct drbd_device *device,
3465 int (*io_fn)(struct drbd_device *),
3466 void (*done)(struct drbd_device *, int),
3467 char *why, enum bm_flag flags)
3468 {
3469 D_ASSERT(current == first_peer_device(device)->connection->worker.task);
3470
3471 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &device->flags));
3472 D_ASSERT(!test_bit(BITMAP_IO, &device->flags));
3473 D_ASSERT(list_empty(&device->bm_io_work.w.list));
3474 if (device->bm_io_work.why)
3475 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
3476 why, device->bm_io_work.why);
3477
3478 device->bm_io_work.io_fn = io_fn;
3479 device->bm_io_work.done = done;
3480 device->bm_io_work.why = why;
3481 device->bm_io_work.flags = flags;
3482
3483 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
3484 set_bit(BITMAP_IO, &device->flags);
3485 if (atomic_read(&device->ap_bio_cnt) == 0) {
3486 if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
3487 drbd_queue_work(&first_peer_device(device)->connection->sender_work, &device->bm_io_work.w);
3488 }
3489 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
3490 }
3491
3492 /**
3493 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
3494 * @device: DRBD device.
3495 * @io_fn: IO callback to be called when bitmap IO is possible
3496 * @why: Descriptive text of the reason for doing the IO
3497 *
3498 * freezes application IO while that the actual IO operations runs. This
3499 * functions MAY NOT be called from worker context.
3500 */
3501 int drbd_bitmap_io(struct drbd_device *device, int (*io_fn)(struct drbd_device *),
3502 char *why, enum bm_flag flags)
3503 {
3504 int rv;
3505
3506 D_ASSERT(current != first_peer_device(device)->connection->worker.task);
3507
3508 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
3509 drbd_suspend_io(device);
3510
3511 drbd_bm_lock(device, why, flags);
3512 rv = io_fn(device);
3513 drbd_bm_unlock(device);
3514
3515 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
3516 drbd_resume_io(device);
3517
3518 return rv;
3519 }
3520
3521 void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local)
3522 {
3523 if ((device->ldev->md.flags & flag) != flag) {
3524 drbd_md_mark_dirty(device);
3525 device->ldev->md.flags |= flag;
3526 }
3527 }
3528
3529 void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local)
3530 {
3531 if ((device->ldev->md.flags & flag) != 0) {
3532 drbd_md_mark_dirty(device);
3533 device->ldev->md.flags &= ~flag;
3534 }
3535 }
3536 int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
3537 {
3538 return (bdev->md.flags & flag) != 0;
3539 }
3540
3541 static void md_sync_timer_fn(unsigned long data)
3542 {
3543 struct drbd_device *device = (struct drbd_device *) data;
3544
3545 /* must not double-queue! */
3546 if (list_empty(&device->md_sync_work.list))
3547 drbd_queue_work_front(&first_peer_device(device)->connection->sender_work, &device->md_sync_work);
3548 }
3549
3550 static int w_md_sync(struct drbd_work *w, int unused)
3551 {
3552 struct drbd_device *device = w->device;
3553
3554 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
3555 #ifdef DEBUG
3556 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
3557 device->last_md_mark_dirty.func, device->last_md_mark_dirty.line);
3558 #endif
3559 drbd_md_sync(device);
3560 return 0;
3561 }
3562
3563 const char *cmdname(enum drbd_packet cmd)
3564 {
3565 /* THINK may need to become several global tables
3566 * when we want to support more than
3567 * one PRO_VERSION */
3568 static const char *cmdnames[] = {
3569 [P_DATA] = "Data",
3570 [P_DATA_REPLY] = "DataReply",
3571 [P_RS_DATA_REPLY] = "RSDataReply",
3572 [P_BARRIER] = "Barrier",
3573 [P_BITMAP] = "ReportBitMap",
3574 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
3575 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
3576 [P_UNPLUG_REMOTE] = "UnplugRemote",
3577 [P_DATA_REQUEST] = "DataRequest",
3578 [P_RS_DATA_REQUEST] = "RSDataRequest",
3579 [P_SYNC_PARAM] = "SyncParam",
3580 [P_SYNC_PARAM89] = "SyncParam89",
3581 [P_PROTOCOL] = "ReportProtocol",
3582 [P_UUIDS] = "ReportUUIDs",
3583 [P_SIZES] = "ReportSizes",
3584 [P_STATE] = "ReportState",
3585 [P_SYNC_UUID] = "ReportSyncUUID",
3586 [P_AUTH_CHALLENGE] = "AuthChallenge",
3587 [P_AUTH_RESPONSE] = "AuthResponse",
3588 [P_PING] = "Ping",
3589 [P_PING_ACK] = "PingAck",
3590 [P_RECV_ACK] = "RecvAck",
3591 [P_WRITE_ACK] = "WriteAck",
3592 [P_RS_WRITE_ACK] = "RSWriteAck",
3593 [P_SUPERSEDED] = "Superseded",
3594 [P_NEG_ACK] = "NegAck",
3595 [P_NEG_DREPLY] = "NegDReply",
3596 [P_NEG_RS_DREPLY] = "NegRSDReply",
3597 [P_BARRIER_ACK] = "BarrierAck",
3598 [P_STATE_CHG_REQ] = "StateChgRequest",
3599 [P_STATE_CHG_REPLY] = "StateChgReply",
3600 [P_OV_REQUEST] = "OVRequest",
3601 [P_OV_REPLY] = "OVReply",
3602 [P_OV_RESULT] = "OVResult",
3603 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3604 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3605 [P_COMPRESSED_BITMAP] = "CBitmap",
3606 [P_DELAY_PROBE] = "DelayProbe",
3607 [P_OUT_OF_SYNC] = "OutOfSync",
3608 [P_RETRY_WRITE] = "RetryWrite",
3609 [P_RS_CANCEL] = "RSCancel",
3610 [P_CONN_ST_CHG_REQ] = "conn_st_chg_req",
3611 [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply",
3612 [P_RETRY_WRITE] = "retry_write",
3613 [P_PROTOCOL_UPDATE] = "protocol_update",
3614
3615 /* enum drbd_packet, but not commands - obsoleted flags:
3616 * P_MAY_IGNORE
3617 * P_MAX_OPT_CMD
3618 */
3619 };
3620
3621 /* too big for the array: 0xfffX */
3622 if (cmd == P_INITIAL_META)
3623 return "InitialMeta";
3624 if (cmd == P_INITIAL_DATA)
3625 return "InitialData";
3626 if (cmd == P_CONNECTION_FEATURES)
3627 return "ConnectionFeatures";
3628 if (cmd >= ARRAY_SIZE(cmdnames))
3629 return "Unknown";
3630 return cmdnames[cmd];
3631 }
3632
3633 /**
3634 * drbd_wait_misc - wait for a request to make progress
3635 * @device: device associated with the request
3636 * @i: the struct drbd_interval embedded in struct drbd_request or
3637 * struct drbd_peer_request
3638 */
3639 int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i)
3640 {
3641 struct net_conf *nc;
3642 DEFINE_WAIT(wait);
3643 long timeout;
3644
3645 rcu_read_lock();
3646 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
3647 if (!nc) {
3648 rcu_read_unlock();
3649 return -ETIMEDOUT;
3650 }
3651 timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
3652 rcu_read_unlock();
3653
3654 /* Indicate to wake up device->misc_wait on progress. */
3655 i->waiting = true;
3656 prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE);
3657 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
3658 timeout = schedule_timeout(timeout);
3659 finish_wait(&device->misc_wait, &wait);
3660 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
3661 if (!timeout || device->state.conn < C_CONNECTED)
3662 return -ETIMEDOUT;
3663 if (signal_pending(current))
3664 return -ERESTARTSYS;
3665 return 0;
3666 }
3667
3668 #ifdef CONFIG_DRBD_FAULT_INJECTION
3669 /* Fault insertion support including random number generator shamelessly
3670 * stolen from kernel/rcutorture.c */
3671 struct fault_random_state {
3672 unsigned long state;
3673 unsigned long count;
3674 };
3675
3676 #define FAULT_RANDOM_MULT 39916801 /* prime */
3677 #define FAULT_RANDOM_ADD 479001701 /* prime */
3678 #define FAULT_RANDOM_REFRESH 10000
3679
3680 /*
3681 * Crude but fast random-number generator. Uses a linear congruential
3682 * generator, with occasional help from get_random_bytes().
3683 */
3684 static unsigned long
3685 _drbd_fault_random(struct fault_random_state *rsp)
3686 {
3687 long refresh;
3688
3689 if (!rsp->count--) {
3690 get_random_bytes(&refresh, sizeof(refresh));
3691 rsp->state += refresh;
3692 rsp->count = FAULT_RANDOM_REFRESH;
3693 }
3694 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3695 return swahw32(rsp->state);
3696 }
3697
3698 static char *
3699 _drbd_fault_str(unsigned int type) {
3700 static char *_faults[] = {
3701 [DRBD_FAULT_MD_WR] = "Meta-data write",
3702 [DRBD_FAULT_MD_RD] = "Meta-data read",
3703 [DRBD_FAULT_RS_WR] = "Resync write",
3704 [DRBD_FAULT_RS_RD] = "Resync read",
3705 [DRBD_FAULT_DT_WR] = "Data write",
3706 [DRBD_FAULT_DT_RD] = "Data read",
3707 [DRBD_FAULT_DT_RA] = "Data read ahead",
3708 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
3709 [DRBD_FAULT_AL_EE] = "EE allocation",
3710 [DRBD_FAULT_RECEIVE] = "receive data corruption",
3711 };
3712
3713 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3714 }
3715
3716 unsigned int
3717 _drbd_insert_fault(struct drbd_device *device, unsigned int type)
3718 {
3719 static struct fault_random_state rrs = {0, 0};
3720
3721 unsigned int ret = (
3722 (fault_devs == 0 ||
3723 ((1 << device_to_minor(device)) & fault_devs) != 0) &&
3724 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3725
3726 if (ret) {
3727 fault_count++;
3728
3729 if (__ratelimit(&drbd_ratelimit_state))
3730 dev_warn(DEV, "***Simulating %s failure\n",
3731 _drbd_fault_str(type));
3732 }
3733
3734 return ret;
3735 }
3736 #endif
3737
3738 const char *drbd_buildtag(void)
3739 {
3740 /* DRBD built from external sources has here a reference to the
3741 git hash of the source code. */
3742
3743 static char buildtag[38] = "\0uilt-in";
3744
3745 if (buildtag[0] == 0) {
3746 #ifdef MODULE
3747 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3748 #else
3749 buildtag[0] = 'b';
3750 #endif
3751 }
3752
3753 return buildtag;
3754 }
3755
3756 module_init(drbd_init)
3757 module_exit(drbd_cleanup)
3758
3759 EXPORT_SYMBOL(drbd_conn_str);
3760 EXPORT_SYMBOL(drbd_role_str);
3761 EXPORT_SYMBOL(drbd_disk_str);
3762 EXPORT_SYMBOL(drbd_set_st_err_str);
This page took 0.104084 seconds and 6 git commands to generate.