blob: d1bfbfcf8f24cdec23498c42fd3655ec38e16658 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
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
Philipp Reisnerb411b362009-09-25 16:07:19 -070029#include <linux/module.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070030#include <linux/drbd.h>
31#include <asm/uaccess.h>
32#include <asm/types.h>
33#include <net/sock.h>
34#include <linux/ctype.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020035#include <linux/mutex.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#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
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"
Philipp Reisnerb411b362009-09-25 16:07:19 -070055#include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
56
57#include "drbd_vli.h"
58
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020059static DEFINE_MUTEX(drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -070060int drbdd_init(struct drbd_thread *);
61int drbd_worker(struct drbd_thread *);
62int drbd_asender(struct drbd_thread *);
63
64int drbd_init(void);
65static int drbd_open(struct block_device *bdev, fmode_t mode);
66static int drbd_release(struct gendisk *gd, fmode_t mode);
Philipp Reisnerb411b362009-09-25 16:07:19 -070067static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
68static void md_sync_timer_fn(unsigned long data);
69static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +020070static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
Philipp Reisnerb411b362009-09-25 16:07:19 -070072MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
73 "Lars Ellenberg <lars@linbit.com>");
74MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
75MODULE_VERSION(REL_VERSION);
76MODULE_LICENSE("GPL");
Philipp Reisner2b8a90b2011-01-10 11:15:17 +010077MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices ("
78 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
Philipp Reisnerb411b362009-09-25 16:07:19 -070079MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
80
81#include <linux/moduleparam.h>
82/* allow_open_on_secondary */
83MODULE_PARM_DESC(allow_oos, "DONT USE!");
84/* thanks to these macros, if compiled into the kernel (not-module),
85 * this becomes the boot parameter drbd.minor_count */
86module_param(minor_count, uint, 0444);
87module_param(disable_sendpage, bool, 0644);
88module_param(allow_oos, bool, 0);
89module_param(cn_idx, uint, 0444);
90module_param(proc_details, int, 0644);
91
92#ifdef CONFIG_DRBD_FAULT_INJECTION
93int enable_faults;
94int fault_rate;
95static int fault_count;
96int fault_devs;
97/* bitmap of enabled faults */
98module_param(enable_faults, int, 0664);
99/* fault rate % value - applies to all enabled faults */
100module_param(fault_rate, int, 0664);
101/* count of faults inserted */
102module_param(fault_count, int, 0664);
103/* bitmap of devices to insert faults on */
104module_param(fault_devs, int, 0644);
105#endif
106
107/* module parameter, defined */
Philipp Reisner2b8a90b2011-01-10 11:15:17 +0100108unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109int disable_sendpage;
110int allow_oos;
111unsigned int cn_idx = CN_IDX_DRBD;
112int proc_details; /* Detail level in proc drbd*/
113
114/* Module parameter for setting the user mode helper program
115 * to run. Default is /sbin/drbdadm */
116char usermode_helper[80] = "/sbin/drbdadm";
117
118module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
119
120/* in 2.6.x, our device mapping and config info contains our virtual gendisks
121 * as member "struct gendisk *vdisk;"
122 */
123struct drbd_conf **minor_table;
Philipp Reisner21114382011-01-19 12:26:59 +0100124struct list_head drbd_tconns; /* list of struct drbd_tconn */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125
126struct kmem_cache *drbd_request_cache;
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +0100127struct kmem_cache *drbd_ee_cache; /* peer requests */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700128struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
129struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
130mempool_t *drbd_request_mempool;
131mempool_t *drbd_ee_mempool;
132
133/* I do not use a standard mempool, because:
134 1) I want to hand out the pre-allocated objects first.
135 2) I want to be able to interrupt sleeping allocation with a signal.
136 Note: This is a single linked list, the next pointer is the private
137 member of struct page.
138 */
139struct page *drbd_pp_pool;
140spinlock_t drbd_pp_lock;
141int drbd_pp_vacant;
142wait_queue_head_t drbd_pp_wait;
143
144DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
145
Emese Revfy7d4e9d02009-12-14 00:59:30 +0100146static const struct block_device_operations drbd_ops = {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700147 .owner = THIS_MODULE,
148 .open = drbd_open,
149 .release = drbd_release,
150};
151
152#define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
153
154#ifdef __CHECKER__
155/* When checking with sparse, and this is an inline function, sparse will
156 give tons of false positives. When this is a real functions sparse works.
157 */
158int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
159{
160 int io_allowed;
161
162 atomic_inc(&mdev->local_cnt);
163 io_allowed = (mdev->state.disk >= mins);
164 if (!io_allowed) {
165 if (atomic_dec_and_test(&mdev->local_cnt))
166 wake_up(&mdev->misc_wait);
167 }
168 return io_allowed;
169}
170
171#endif
172
173/**
174 * DOC: The transfer log
175 *
176 * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100177 * mdev->tconn->newest_tle points to the head, mdev->tconn->oldest_tle points to the tail
Philipp Reisnerb411b362009-09-25 16:07:19 -0700178 * of the list. There is always at least one &struct drbd_tl_epoch object.
179 *
180 * Each &struct drbd_tl_epoch has a circular double linked list of requests
181 * attached.
182 */
183static int tl_init(struct drbd_conf *mdev)
184{
185 struct drbd_tl_epoch *b;
186
187 /* during device minor initialization, we may well use GFP_KERNEL */
188 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
189 if (!b)
190 return 0;
191 INIT_LIST_HEAD(&b->requests);
192 INIT_LIST_HEAD(&b->w.list);
193 b->next = NULL;
194 b->br_number = 4711;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200195 b->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700196 b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
197
Philipp Reisner87eeee42011-01-19 14:16:30 +0100198 mdev->tconn->oldest_tle = b;
199 mdev->tconn->newest_tle = b;
200 INIT_LIST_HEAD(&mdev->tconn->out_of_sequence_requests);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201
Philipp Reisnerb411b362009-09-25 16:07:19 -0700202 return 1;
203}
204
205static void tl_cleanup(struct drbd_conf *mdev)
206{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100207 D_ASSERT(mdev->tconn->oldest_tle == mdev->tconn->newest_tle);
208 D_ASSERT(list_empty(&mdev->tconn->out_of_sequence_requests));
209 kfree(mdev->tconn->oldest_tle);
210 mdev->tconn->oldest_tle = NULL;
211 kfree(mdev->tconn->unused_spare_tle);
212 mdev->tconn->unused_spare_tle = NULL;
Andreas Gruenbacherd6287692011-01-13 23:05:39 +0100213}
214
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215/**
216 * _tl_add_barrier() - Adds a barrier to the transfer log
217 * @mdev: DRBD device.
218 * @new: Barrier to be added before the current head of the TL.
219 *
220 * The caller must hold the req_lock.
221 */
222void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
223{
224 struct drbd_tl_epoch *newest_before;
225
226 INIT_LIST_HEAD(&new->requests);
227 INIT_LIST_HEAD(&new->w.list);
228 new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
229 new->next = NULL;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200230 new->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700231
Philipp Reisner87eeee42011-01-19 14:16:30 +0100232 newest_before = mdev->tconn->newest_tle;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 /* never send a barrier number == 0, because that is special-cased
234 * when using TCQ for our write ordering code */
235 new->br_number = (newest_before->br_number+1) ?: 1;
Philipp Reisner87eeee42011-01-19 14:16:30 +0100236 if (mdev->tconn->newest_tle != new) {
237 mdev->tconn->newest_tle->next = new;
238 mdev->tconn->newest_tle = new;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700239 }
240}
241
242/**
243 * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
244 * @mdev: DRBD device.
245 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
246 * @set_size: Expected number of requests before that barrier.
247 *
248 * In case the passed barrier_nr or set_size does not match the oldest
249 * &struct drbd_tl_epoch objects this function will cause a termination
250 * of the connection.
251 */
252void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
253 unsigned int set_size)
254{
255 struct drbd_tl_epoch *b, *nob; /* next old barrier */
256 struct list_head *le, *tle;
257 struct drbd_request *r;
258
Philipp Reisner87eeee42011-01-19 14:16:30 +0100259 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Philipp Reisner87eeee42011-01-19 14:16:30 +0100261 b = mdev->tconn->oldest_tle;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262
263 /* first some paranoia code */
264 if (b == NULL) {
265 dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
266 barrier_nr);
267 goto bail;
268 }
269 if (b->br_number != barrier_nr) {
270 dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n",
271 barrier_nr, b->br_number);
272 goto bail;
273 }
Philipp Reisner7e602c02010-05-27 14:49:27 +0200274 if (b->n_writes != set_size) {
275 dev_err(DEV, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
276 barrier_nr, set_size, b->n_writes);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 goto bail;
278 }
279
280 /* Clean up list of requests processed during current epoch */
281 list_for_each_safe(le, tle, &b->requests) {
282 r = list_entry(le, struct drbd_request, tl_requests);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100283 _req_mod(r, BARRIER_ACKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700284 }
285 /* There could be requests on the list waiting for completion
286 of the write to the local disk. To avoid corruptions of
287 slab's data structures we have to remove the lists head.
288
289 Also there could have been a barrier ack out of sequence, overtaking
290 the write acks - which would be a bug and violating write ordering.
291 To not deadlock in case we lose connection while such requests are
292 still pending, we need some way to find them for the
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100293 _req_mode(CONNECTION_LOST_WHILE_PENDING).
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294
295 These have been list_move'd to the out_of_sequence_requests list in
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100296 _req_mod(, BARRIER_ACKED) above.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700297 */
298 list_del_init(&b->requests);
299
300 nob = b->next;
301 if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
302 _tl_add_barrier(mdev, b);
303 if (nob)
Philipp Reisner87eeee42011-01-19 14:16:30 +0100304 mdev->tconn->oldest_tle = nob;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305 /* if nob == NULL b was the only barrier, and becomes the new
Philipp Reisner87eeee42011-01-19 14:16:30 +0100306 barrier. Therefore mdev->tconn->oldest_tle points already to b */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 } else {
308 D_ASSERT(nob != NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100309 mdev->tconn->oldest_tle = nob;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 kfree(b);
311 }
312
Philipp Reisner87eeee42011-01-19 14:16:30 +0100313 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314 dec_ap_pending(mdev);
315
316 return;
317
318bail:
Philipp Reisner87eeee42011-01-19 14:16:30 +0100319 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700320 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
321}
322
Philipp Reisner617049a2010-12-22 12:48:31 +0100323
Philipp Reisner11b58e72010-05-12 17:08:26 +0200324/**
325 * _tl_restart() - Walks the transfer log, and applies an action to all requests
326 * @mdev: DRBD device.
327 * @what: The action/event to perform with all request objects
328 *
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100329 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
330 * RESTART_FROZEN_DISK_IO.
Philipp Reisner11b58e72010-05-12 17:08:26 +0200331 */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100332void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
Philipp Reisner11b58e72010-05-12 17:08:26 +0200333{
334 struct drbd_tl_epoch *b, *tmp, **pn;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200335 struct list_head *le, *tle, carry_reads;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200336 struct drbd_request *req;
337 int rv, n_writes, n_reads;
338
Philipp Reisner87eeee42011-01-19 14:16:30 +0100339 b = mdev->tconn->oldest_tle;
340 pn = &mdev->tconn->oldest_tle;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200341 while (b) {
342 n_writes = 0;
343 n_reads = 0;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200344 INIT_LIST_HEAD(&carry_reads);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200345 list_for_each_safe(le, tle, &b->requests) {
346 req = list_entry(le, struct drbd_request, tl_requests);
347 rv = _req_mod(req, what);
348
349 n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
350 n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
351 }
352 tmp = b->next;
353
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200354 if (n_writes) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100355 if (what == RESEND) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200356 b->n_writes = n_writes;
357 if (b->w.cb == NULL) {
358 b->w.cb = w_send_barrier;
359 inc_ap_pending(mdev);
360 set_bit(CREATE_BARRIER, &mdev->flags);
361 }
362
Philipp Reisnere42325a2011-01-19 13:55:45 +0100363 drbd_queue_work(&mdev->tconn->data.work, &b->w);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200364 }
365 pn = &b->next;
366 } else {
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200367 if (n_reads)
368 list_add(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200369 /* there could still be requests on that ring list,
370 * in case local io is still pending */
371 list_del(&b->requests);
372
373 /* dec_ap_pending corresponding to queue_barrier.
374 * the newest barrier may not have been queued yet,
375 * in which case w.cb is still NULL. */
376 if (b->w.cb != NULL)
377 dec_ap_pending(mdev);
378
Philipp Reisner87eeee42011-01-19 14:16:30 +0100379 if (b == mdev->tconn->newest_tle) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200380 /* recycle, but reinit! */
381 D_ASSERT(tmp == NULL);
382 INIT_LIST_HEAD(&b->requests);
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200383 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200384 INIT_LIST_HEAD(&b->w.list);
385 b->w.cb = NULL;
386 b->br_number = net_random();
387 b->n_writes = 0;
388
389 *pn = b;
390 break;
391 }
392 *pn = tmp;
393 kfree(b);
394 }
395 b = tmp;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200396 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200397 }
398}
399
Philipp Reisnerb411b362009-09-25 16:07:19 -0700400
401/**
402 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
403 * @mdev: DRBD device.
404 *
405 * This is called after the connection to the peer was lost. The storage covered
406 * by the requests on the transfer gets marked as our of sync. Called from the
407 * receiver thread and the worker thread.
408 */
409void tl_clear(struct drbd_conf *mdev)
410{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411 struct list_head *le, *tle;
412 struct drbd_request *r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100416 _tl_restart(mdev, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417
418 /* we expect this list to be empty. */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100419 D_ASSERT(list_empty(&mdev->tconn->out_of_sequence_requests));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
421 /* but just in case, clean it up anyways! */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100422 list_for_each_safe(le, tle, &mdev->tconn->out_of_sequence_requests) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423 r = list_entry(le, struct drbd_request, tl_requests);
424 /* It would be nice to complete outside of spinlock.
425 * But this is easier for now. */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100426 _req_mod(r, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427 }
428
429 /* ensure bit indicating barrier is required is clear */
430 clear_bit(CREATE_BARRIER, &mdev->flags);
431
Philipp Reisner87eeee42011-01-19 14:16:30 +0100432 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433}
434
Philipp Reisner11b58e72010-05-12 17:08:26 +0200435void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
436{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100437 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200438 _tl_restart(mdev, what);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100439 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700440}
441
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442static int drbd_thread_setup(void *arg)
443{
444 struct drbd_thread *thi = (struct drbd_thread *) arg;
445 struct drbd_conf *mdev = thi->mdev;
446 unsigned long flags;
447 int retval;
448
449restart:
450 retval = thi->function(thi);
451
452 spin_lock_irqsave(&thi->t_lock, flags);
453
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100454 /* if the receiver has been "EXITING", the last thing it did
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 * was set the conn state to "StandAlone",
456 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
457 * and receiver thread will be "started".
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100458 * drbd_thread_start needs to set "RESTARTING" in that case.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700459 * t_state check and assignment needs to be within the same spinlock,
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100460 * so either thread_start sees EXITING, and can remap to RESTARTING,
461 * or thread_start see NONE, and can proceed as normal.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462 */
463
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100464 if (thi->t_state == RESTARTING) {
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100465 dev_info(DEV, "Restarting %s thread\n", thi->name);
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100466 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467 spin_unlock_irqrestore(&thi->t_lock, flags);
468 goto restart;
469 }
470
471 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100472 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700473 smp_mb();
474 complete(&thi->stop);
475 spin_unlock_irqrestore(&thi->t_lock, flags);
476
477 dev_info(DEV, "Terminating %s\n", current->comm);
478
479 /* Release mod reference taken when thread was started */
480 module_put(THIS_MODULE);
481 return retval;
482}
483
484static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100485 int (*func) (struct drbd_thread *), char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486{
487 spin_lock_init(&thi->t_lock);
488 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100489 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490 thi->function = func;
491 thi->mdev = mdev;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100492 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493}
494
495int drbd_thread_start(struct drbd_thread *thi)
496{
497 struct drbd_conf *mdev = thi->mdev;
498 struct task_struct *nt;
499 unsigned long flags;
500
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 /* is used from state engine doing drbd_thread_stop_nowait,
502 * while holding the req lock irqsave */
503 spin_lock_irqsave(&thi->t_lock, flags);
504
505 switch (thi->t_state) {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100506 case NONE:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700507 dev_info(DEV, "Starting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100508 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509
510 /* Get ref on module for thread - this is released when thread exits */
511 if (!try_module_get(THIS_MODULE)) {
512 dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
513 spin_unlock_irqrestore(&thi->t_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100514 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700515 }
516
517 init_completion(&thi->stop);
518 D_ASSERT(thi->task == NULL);
519 thi->reset_cpu_mask = 1;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100520 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 spin_unlock_irqrestore(&thi->t_lock, flags);
522 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
523
524 nt = kthread_create(drbd_thread_setup, (void *) thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100525 "drbd%d_%s", mdev_to_minor(mdev), thi->name);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526
527 if (IS_ERR(nt)) {
528 dev_err(DEV, "Couldn't start thread\n");
529
530 module_put(THIS_MODULE);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100531 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 }
533 spin_lock_irqsave(&thi->t_lock, flags);
534 thi->task = nt;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100535 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 spin_unlock_irqrestore(&thi->t_lock, flags);
537 wake_up_process(nt);
538 break;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100539 case EXITING:
540 thi->t_state = RESTARTING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700541 dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100542 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 /* fall through */
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100544 case RUNNING:
545 case RESTARTING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546 default:
547 spin_unlock_irqrestore(&thi->t_lock, flags);
548 break;
549 }
550
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100551 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552}
553
554
555void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
556{
557 unsigned long flags;
558
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100559 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560
561 /* may be called from state engine, holding the req lock irqsave */
562 spin_lock_irqsave(&thi->t_lock, flags);
563
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100564 if (thi->t_state == NONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 spin_unlock_irqrestore(&thi->t_lock, flags);
566 if (restart)
567 drbd_thread_start(thi);
568 return;
569 }
570
571 if (thi->t_state != ns) {
572 if (thi->task == NULL) {
573 spin_unlock_irqrestore(&thi->t_lock, flags);
574 return;
575 }
576
577 thi->t_state = ns;
578 smp_mb();
579 init_completion(&thi->stop);
580 if (thi->task != current)
581 force_sig(DRBD_SIGKILL, thi->task);
582
583 }
584
585 spin_unlock_irqrestore(&thi->t_lock, flags);
586
587 if (wait)
588 wait_for_completion(&thi->stop);
589}
590
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100591static struct drbd_thread *drbd_task_to_thread(struct drbd_conf *mdev, struct task_struct *task)
592{
593 struct drbd_tconn *tconn = mdev->tconn;
594 struct drbd_thread *thi =
595 task == tconn->receiver.task ? &tconn->receiver :
596 task == tconn->asender.task ? &tconn->asender :
597 task == tconn->worker.task ? &tconn->worker : NULL;
598
599 return thi;
600}
601
602char *drbd_task_to_thread_name(struct drbd_conf *mdev, struct task_struct *task)
603{
604 struct drbd_thread *thi = drbd_task_to_thread(mdev, task);
605 return thi ? thi->name : task->comm;
606}
607
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608#ifdef CONFIG_SMP
609/**
610 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
611 * @mdev: DRBD device.
612 *
613 * Forces all threads of a device onto the same CPU. This is beneficial for
614 * DRBD's performance. May be overwritten by user's configuration.
615 */
616void drbd_calc_cpu_mask(struct drbd_conf *mdev)
617{
618 int ord, cpu;
619
620 /* user override. */
621 if (cpumask_weight(mdev->cpu_mask))
622 return;
623
624 ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask);
625 for_each_online_cpu(cpu) {
626 if (ord-- == 0) {
627 cpumask_set_cpu(cpu, mdev->cpu_mask);
628 return;
629 }
630 }
631 /* should not be reached */
632 cpumask_setall(mdev->cpu_mask);
633}
634
635/**
636 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
637 * @mdev: DRBD device.
Philipp Reisnerbc31fe32011-02-07 11:14:38 +0100638 * @thi: drbd_thread object
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639 *
640 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
641 * prematurely.
642 */
Philipp Reisnerbc31fe32011-02-07 11:14:38 +0100643void drbd_thread_current_set_cpu(struct drbd_conf *mdev, struct drbd_thread *thi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644{
645 struct task_struct *p = current;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100646
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647 if (!thi->reset_cpu_mask)
648 return;
649 thi->reset_cpu_mask = 0;
650 set_cpus_allowed_ptr(p, mdev->cpu_mask);
651}
652#endif
653
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100654static void prepare_header80(struct drbd_conf *mdev, struct p_header80 *h,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100655 enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100656{
657 h->magic = cpu_to_be32(DRBD_MAGIC);
658 h->command = cpu_to_be16(cmd);
659 h->length = cpu_to_be16(size);
660}
661
662static void prepare_header95(struct drbd_conf *mdev, struct p_header95 *h,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100663 enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100664{
665 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
666 h->command = cpu_to_be16(cmd);
667 h->length = cpu_to_be32(size);
668}
669
670static void prepare_header(struct drbd_conf *mdev, struct p_header *h,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100671 enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100672{
673 if (mdev->tconn->agreed_pro_version >= 100 || size > DRBD_MAX_SIZE_H80_PACKET)
674 prepare_header95(mdev, &h->h95, cmd, size);
675 else
676 prepare_header80(mdev, &h->h80, cmd, size);
677}
678
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679/* the appropriate socket mutex must be held already */
680int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100681 enum drbd_packet cmd, struct p_header *h, size_t size,
682 unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683{
684 int sent, ok;
685
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100686 if (!expect(h))
687 return false;
688 if (!expect(size))
689 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100691 prepare_header(mdev, h, cmd, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +0100693 sent = drbd_send(mdev->tconn, sock, h, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694
695 ok = (sent == size);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +0100696 if (!ok && !signal_pending(current))
697 dev_warn(DEV, "short sent %s size=%d sent=%d\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700698 cmdname(cmd), (int)size, sent);
699 return ok;
700}
701
702/* don't pass the socket. we may only look at it
703 * when we hold the appropriate socket mutex.
704 */
705int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100706 enum drbd_packet cmd, struct p_header *h, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707{
708 int ok = 0;
709 struct socket *sock;
710
711 if (use_data_socket) {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100712 mutex_lock(&mdev->tconn->data.mutex);
713 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700714 } else {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100715 mutex_lock(&mdev->tconn->meta.mutex);
716 sock = mdev->tconn->meta.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 }
718
719 /* drbd_disconnect() could have called drbd_free_sock()
720 * while we were waiting in down()... */
721 if (likely(sock != NULL))
722 ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
723
724 if (use_data_socket)
Philipp Reisnere42325a2011-01-19 13:55:45 +0100725 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726 else
Philipp Reisnere42325a2011-01-19 13:55:45 +0100727 mutex_unlock(&mdev->tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728 return ok;
729}
730
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100731int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packet cmd, char *data,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732 size_t size)
733{
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100734 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700735 int ok;
736
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100737 prepare_header(mdev, &h, cmd, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
739 if (!drbd_get_data_sock(mdev))
740 return 0;
741
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742 ok = (sizeof(h) ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +0100743 drbd_send(mdev->tconn, mdev->tconn->data.socket, &h, sizeof(h), 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700744 ok = ok && (size ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +0100745 drbd_send(mdev->tconn, mdev->tconn->data.socket, data, size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746
747 drbd_put_data_sock(mdev);
748
749 return ok;
750}
751
752int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
753{
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200754 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700755 struct socket *sock;
756 int size, rv;
Philipp Reisner31890f42011-01-19 14:12:51 +0100757 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758
759 size = apv <= 87 ? sizeof(struct p_rs_param)
760 : apv == 88 ? sizeof(struct p_rs_param)
761 + strlen(mdev->sync_conf.verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200762 : apv <= 94 ? sizeof(struct p_rs_param_89)
763 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700764
765 /* used from admin command context and receiver/worker context.
766 * to avoid kmalloc, grab the socket right here,
767 * then use the pre-allocated sbuf there */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100768 mutex_lock(&mdev->tconn->data.mutex);
769 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770
771 if (likely(sock != NULL)) {
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100772 enum drbd_packet cmd =
773 apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774
Philipp Reisnere42325a2011-01-19 13:55:45 +0100775 p = &mdev->tconn->data.sbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776
777 /* initialize verify_alg and csums_alg */
778 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
779
780 p->rate = cpu_to_be32(sc->rate);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200781 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
782 p->c_delay_target = cpu_to_be32(sc->c_delay_target);
783 p->c_fill_target = cpu_to_be32(sc->c_fill_target);
784 p->c_max_rate = cpu_to_be32(sc->c_max_rate);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785
786 if (apv >= 88)
787 strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
788 if (apv >= 89)
789 strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
790
791 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
792 } else
793 rv = 0; /* not ok */
794
Philipp Reisnere42325a2011-01-19 13:55:45 +0100795 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796
797 return rv;
798}
799
800int drbd_send_protocol(struct drbd_conf *mdev)
801{
802 struct p_protocol *p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100803 int size, cf, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804
805 size = sizeof(struct p_protocol);
806
Philipp Reisner31890f42011-01-19 14:12:51 +0100807 if (mdev->tconn->agreed_pro_version >= 87)
Philipp Reisner89e58e72011-01-19 13:12:45 +0100808 size += strlen(mdev->tconn->net_conf->integrity_alg) + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809
810 /* we must not recurse into our own queue,
811 * as that is blocked during handshake */
812 p = kmalloc(size, GFP_NOIO);
813 if (p == NULL)
814 return 0;
815
Philipp Reisner89e58e72011-01-19 13:12:45 +0100816 p->protocol = cpu_to_be32(mdev->tconn->net_conf->wire_protocol);
817 p->after_sb_0p = cpu_to_be32(mdev->tconn->net_conf->after_sb_0p);
818 p->after_sb_1p = cpu_to_be32(mdev->tconn->net_conf->after_sb_1p);
819 p->after_sb_2p = cpu_to_be32(mdev->tconn->net_conf->after_sb_2p);
820 p->two_primaries = cpu_to_be32(mdev->tconn->net_conf->two_primaries);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100822 cf = 0;
Philipp Reisner89e58e72011-01-19 13:12:45 +0100823 if (mdev->tconn->net_conf->want_lose)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100824 cf |= CF_WANT_LOSE;
Philipp Reisner89e58e72011-01-19 13:12:45 +0100825 if (mdev->tconn->net_conf->dry_run) {
Philipp Reisner31890f42011-01-19 14:12:51 +0100826 if (mdev->tconn->agreed_pro_version >= 92)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100827 cf |= CF_DRY_RUN;
828 else {
829 dev_err(DEV, "--dry-run is not supported by peer");
Dan Carpenter7ac314c2010-04-22 14:27:23 +0200830 kfree(p);
Philipp Reisner148efa12011-01-15 00:21:15 +0100831 return -1;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100832 }
833 }
834 p->conn_flags = cpu_to_be32(cf);
835
Philipp Reisner31890f42011-01-19 14:12:51 +0100836 if (mdev->tconn->agreed_pro_version >= 87)
Philipp Reisner89e58e72011-01-19 13:12:45 +0100837 strcpy(p->integrity_alg, mdev->tconn->net_conf->integrity_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700838
Philipp Reisnerc0129492011-01-19 16:58:16 +0100839 rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL, &p->head, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840 kfree(p);
841 return rv;
842}
843
844int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
845{
846 struct p_uuids p;
847 int i;
848
849 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
850 return 1;
851
852 for (i = UI_CURRENT; i < UI_SIZE; i++)
853 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
854
855 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
856 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
Philipp Reisner89e58e72011-01-19 13:12:45 +0100857 uuid_flags |= mdev->tconn->net_conf->want_lose ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
859 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
860 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
861
862 put_ldev(mdev);
863
Philipp Reisnerc0129492011-01-19 16:58:16 +0100864 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865}
866
867int drbd_send_uuids(struct drbd_conf *mdev)
868{
869 return _drbd_send_uuids(mdev, 0);
870}
871
872int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
873{
874 return _drbd_send_uuids(mdev, 8);
875}
876
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100877void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
878{
879 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
880 u64 *uuid = mdev->ldev->md.uuid;
881 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
882 text,
883 (unsigned long long)uuid[UI_CURRENT],
884 (unsigned long long)uuid[UI_BITMAP],
885 (unsigned long long)uuid[UI_HISTORY_START],
886 (unsigned long long)uuid[UI_HISTORY_END]);
887 put_ldev(mdev);
888 } else {
889 dev_info(DEV, "%s effective data uuid: %016llX\n",
890 text,
891 (unsigned long long)mdev->ed_uuid);
892 }
893}
894
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100895int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700896{
897 struct p_rs_uuid p;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100898 u64 uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100900 D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
901
Philipp Reisner4a23f262011-01-11 17:42:17 +0100902 uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100903 drbd_uuid_set(mdev, UI_BITMAP, uuid);
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100904 drbd_print_uuids(mdev, "updated sync UUID");
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100905 drbd_md_sync(mdev);
906 p.uuid = cpu_to_be64(uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907
Philipp Reisnerc0129492011-01-19 16:58:16 +0100908 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909}
910
Philipp Reisnere89b5912010-03-24 17:11:33 +0100911int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912{
913 struct p_sizes p;
914 sector_t d_size, u_size;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200915 int q_order_type, max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 int ok;
917
918 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
919 D_ASSERT(mdev->ldev->backing_bdev);
920 d_size = drbd_get_max_capacity(mdev->ldev);
921 u_size = mdev->ldev->dc.disk_size;
922 q_order_type = drbd_queue_order_type(mdev);
Philipp Reisner99432fc2011-05-20 16:39:13 +0200923 max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
924 max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700925 put_ldev(mdev);
926 } else {
927 d_size = 0;
928 u_size = 0;
929 q_order_type = QUEUE_ORDERED_NONE;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200930 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 }
932
933 p.d_size = cpu_to_be64(d_size);
934 p.u_size = cpu_to_be64(u_size);
935 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
Philipp Reisner99432fc2011-05-20 16:39:13 +0200936 p.max_bio_size = cpu_to_be32(max_bio_size);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100937 p.queue_order_type = cpu_to_be16(q_order_type);
938 p.dds_flags = cpu_to_be16(flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939
Philipp Reisnerc0129492011-01-19 16:58:16 +0100940 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 return ok;
942}
943
944/**
945 * drbd_send_state() - Sends the drbd state to the peer
946 * @mdev: DRBD device.
947 */
948int drbd_send_state(struct drbd_conf *mdev)
949{
950 struct socket *sock;
951 struct p_state p;
952 int ok = 0;
953
Philipp Reisnere42325a2011-01-19 13:55:45 +0100954 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955
956 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100957 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958
959 if (likely(sock != NULL)) {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100960 ok = _drbd_send_cmd(mdev, sock, P_STATE, &p.head, sizeof(p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961 }
962
Philipp Reisnere42325a2011-01-19 13:55:45 +0100963 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965 return ok;
966}
967
968int drbd_send_state_req(struct drbd_conf *mdev,
969 union drbd_state mask, union drbd_state val)
970{
971 struct p_req_state p;
972
973 p.mask = cpu_to_be32(mask.i);
974 p.val = cpu_to_be32(val.i);
975
Philipp Reisnerc0129492011-01-19 16:58:16 +0100976 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977}
978
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100979int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980{
981 struct p_req_state_reply p;
982
983 p.retcode = cpu_to_be32(retcode);
984
Philipp Reisnerc0129492011-01-19 16:58:16 +0100985 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986}
987
988int fill_bitmap_rle_bits(struct drbd_conf *mdev,
989 struct p_compressed_bm *p,
990 struct bm_xfer_ctx *c)
991{
992 struct bitstream bs;
993 unsigned long plain_bits;
994 unsigned long tmp;
995 unsigned long rl;
996 unsigned len;
997 unsigned toggle;
998 int bits;
999
1000 /* may we use this feature? */
1001 if ((mdev->sync_conf.use_rle == 0) ||
Philipp Reisner31890f42011-01-19 14:12:51 +01001002 (mdev->tconn->agreed_pro_version < 90))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 return 0;
1004
1005 if (c->bit_offset >= c->bm_bits)
1006 return 0; /* nothing to do. */
1007
1008 /* use at most thus many bytes */
1009 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1010 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1011 /* plain bits covered in this code string */
1012 plain_bits = 0;
1013
1014 /* p->encoding & 0x80 stores whether the first run length is set.
1015 * bit offset is implicit.
1016 * start with toggle == 2 to be able to tell the first iteration */
1017 toggle = 2;
1018
1019 /* see how much plain bits we can stuff into one packet
1020 * using RLE and VLI. */
1021 do {
1022 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1023 : _drbd_bm_find_next(mdev, c->bit_offset);
1024 if (tmp == -1UL)
1025 tmp = c->bm_bits;
1026 rl = tmp - c->bit_offset;
1027
1028 if (toggle == 2) { /* first iteration */
1029 if (rl == 0) {
1030 /* the first checked bit was set,
1031 * store start value, */
1032 DCBP_set_start(p, 1);
1033 /* but skip encoding of zero run length */
1034 toggle = !toggle;
1035 continue;
1036 }
1037 DCBP_set_start(p, 0);
1038 }
1039
1040 /* paranoia: catch zero runlength.
1041 * can only happen if bitmap is modified while we scan it. */
1042 if (rl == 0) {
1043 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1044 "t:%u bo:%lu\n", toggle, c->bit_offset);
1045 return -1;
1046 }
1047
1048 bits = vli_encode_bits(&bs, rl);
1049 if (bits == -ENOBUFS) /* buffer full */
1050 break;
1051 if (bits <= 0) {
1052 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1053 return 0;
1054 }
1055
1056 toggle = !toggle;
1057 plain_bits += rl;
1058 c->bit_offset = tmp;
1059 } while (c->bit_offset < c->bm_bits);
1060
1061 len = bs.cur.b - p->code + !!bs.cur.bit;
1062
1063 if (plain_bits < (len << 3)) {
1064 /* incompressible with this method.
1065 * we need to rewind both word and bit position. */
1066 c->bit_offset -= plain_bits;
1067 bm_xfer_ctx_bit_to_word_offset(c);
1068 c->bit_offset = c->word_offset * BITS_PER_LONG;
1069 return 0;
1070 }
1071
1072 /* RLE + VLI was able to compress it just fine.
1073 * update c->word_offset. */
1074 bm_xfer_ctx_bit_to_word_offset(c);
1075
1076 /* store pad_bits */
1077 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
1078
1079 return len;
1080}
1081
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001082/**
1083 * send_bitmap_rle_or_plain
1084 *
1085 * Return 0 when done, 1 when another iteration is needed, and a negative error
1086 * code upon failure.
1087 */
1088static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001090 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001091{
1092 struct p_compressed_bm *p = (void*)h;
1093 unsigned long num_words;
1094 int len;
1095 int ok;
1096
1097 len = fill_bitmap_rle_bits(mdev, p, c);
1098
1099 if (len < 0)
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001100 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001101
1102 if (len) {
1103 DCBP_set_code(p, RLE_VLI_Bits);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001104 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001105 sizeof(*p) + len, 0);
1106
1107 c->packets[0]++;
1108 c->bytes[0] += sizeof(*p) + len;
1109
1110 if (c->bit_offset >= c->bm_bits)
1111 len = 0; /* DONE */
1112 } else {
1113 /* was not compressible.
1114 * send a buffer full of plain text bits instead. */
1115 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
1116 len = num_words * sizeof(long);
1117 if (len)
1118 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001119 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001120 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001121 c->word_offset += num_words;
1122 c->bit_offset = c->word_offset * BITS_PER_LONG;
1123
1124 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02001125 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001126
1127 if (c->bit_offset > c->bm_bits)
1128 c->bit_offset = c->bm_bits;
1129 }
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001130 if (ok) {
1131 if (len == 0) {
1132 INFO_bm_xfer_stats(mdev, "send", c);
1133 return 0;
1134 } else
1135 return 1;
1136 }
1137 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138}
1139
1140/* See the comment at receive_bitmap() */
1141int _drbd_send_bitmap(struct drbd_conf *mdev)
1142{
1143 struct bm_xfer_ctx c;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001144 struct p_header *p;
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001145 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001147 if (!expect(mdev->bitmap))
1148 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149
1150 /* maybe we should use some per thread scratch page,
1151 * and allocate that during initial device creation? */
Philipp Reisnerc0129492011-01-19 16:58:16 +01001152 p = (struct p_header *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 if (!p) {
1154 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001155 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001156 }
1157
1158 if (get_ldev(mdev)) {
1159 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1160 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
1161 drbd_bm_set_all(mdev);
1162 if (drbd_bm_write(mdev)) {
1163 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1164 * but otherwise process as per normal - need to tell other
1165 * side that a full resync is required! */
1166 dev_err(DEV, "Failed to write bitmap to disk!\n");
1167 } else {
1168 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
1169 drbd_md_sync(mdev);
1170 }
1171 }
1172 put_ldev(mdev);
1173 }
1174
1175 c = (struct bm_xfer_ctx) {
1176 .bm_bits = drbd_bm_bits(mdev),
1177 .bm_words = drbd_bm_words(mdev),
1178 };
1179
1180 do {
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001181 err = send_bitmap_rle_or_plain(mdev, p, &c);
1182 } while (err > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183
1184 free_page((unsigned long) p);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001185 return err == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186}
1187
1188int drbd_send_bitmap(struct drbd_conf *mdev)
1189{
1190 int err;
1191
1192 if (!drbd_get_data_sock(mdev))
1193 return -1;
1194 err = !_drbd_send_bitmap(mdev);
1195 drbd_put_data_sock(mdev);
1196 return err;
1197}
1198
1199int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
1200{
1201 int ok;
1202 struct p_barrier_ack p;
1203
1204 p.barrier = barrier_nr;
1205 p.set_size = cpu_to_be32(set_size);
1206
1207 if (mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001208 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001209 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001210 return ok;
1211}
1212
1213/**
1214 * _drbd_send_ack() - Sends an ack packet
1215 * @mdev: DRBD device.
1216 * @cmd: Packet command code.
1217 * @sector: sector, needs to be in big endian byte order
1218 * @blksize: size in byte, needs to be in big endian byte order
1219 * @block_id: Id, big endian byte order
1220 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001221static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
1222 u64 sector, u32 blksize, u64 block_id)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223{
1224 int ok;
1225 struct p_block_ack p;
1226
1227 p.sector = sector;
1228 p.block_id = block_id;
1229 p.blksize = blksize;
1230 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
1231
Philipp Reisnere42325a2011-01-19 13:55:45 +01001232 if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001233 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001234 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 return ok;
1236}
1237
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001238/* dp->sector and dp->block_id already/still in network byte order,
1239 * data_size is payload size according to dp->head,
1240 * and may need to be corrected for digest size. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001241int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001242 struct p_data *dp, int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243{
Philipp Reisnera0638452011-01-19 14:31:32 +01001244 data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1245 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001246 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
1247 dp->block_id);
1248}
1249
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001250int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 struct p_block_req *rp)
1252{
1253 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
1254}
1255
1256/**
1257 * drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001258 * @mdev: DRBD device
1259 * @cmd: packet command code
1260 * @peer_req: peer request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001262int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001263 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264{
1265 return _drbd_send_ack(mdev, cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001266 cpu_to_be64(peer_req->i.sector),
1267 cpu_to_be32(peer_req->i.size),
1268 peer_req->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269}
1270
1271/* This function misuses the block_id field to signal if the blocks
1272 * are is sync or not. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001273int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274 sector_t sector, int blksize, u64 block_id)
1275{
1276 return _drbd_send_ack(mdev, cmd,
1277 cpu_to_be64(sector),
1278 cpu_to_be32(blksize),
1279 cpu_to_be64(block_id));
1280}
1281
1282int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1283 sector_t sector, int size, u64 block_id)
1284{
1285 int ok;
1286 struct p_block_req p;
1287
1288 p.sector = cpu_to_be64(sector);
1289 p.block_id = block_id;
1290 p.blksize = cpu_to_be32(size);
1291
Philipp Reisnerc0129492011-01-19 16:58:16 +01001292 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293 return ok;
1294}
1295
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001296int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
1297 void *digest, int digest_size, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298{
1299 int ok;
1300 struct p_block_req p;
1301
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001302 prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001303 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001304 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001305 p.blksize = cpu_to_be32(size);
1306
Philipp Reisnere42325a2011-01-19 13:55:45 +01001307 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001309 ok = (sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0));
1310 ok = ok && (digest_size == drbd_send(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311
Philipp Reisnere42325a2011-01-19 13:55:45 +01001312 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313
1314 return ok;
1315}
1316
1317int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
1318{
1319 int ok;
1320 struct p_block_req p;
1321
1322 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001323 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001324 p.blksize = cpu_to_be32(size);
1325
Philipp Reisnerc0129492011-01-19 16:58:16 +01001326 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001327 return ok;
1328}
1329
1330/* called on sndtimeo
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001331 * returns false if we should retry,
1332 * true if we think connection is dead
Philipp Reisnerb411b362009-09-25 16:07:19 -07001333 */
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001334static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001335{
1336 int drop_it;
1337 /* long elapsed = (long)(jiffies - mdev->last_received); */
1338
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001339 drop_it = tconn->meta.socket == sock
1340 || !tconn->asender.task
1341 || get_t_state(&tconn->asender) != RUNNING
1342 || tconn->volume0->state.conn < C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343
1344 if (drop_it)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001345 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001346
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001347 drop_it = !--tconn->ko_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001348 if (!drop_it) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001349 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1350 current->comm, current->pid, tconn->ko_count);
1351 request_ping(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001352 }
1353
1354 return drop_it; /* && (mdev->state == R_PRIMARY) */;
1355}
1356
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001357static void drbd_update_congested(struct drbd_tconn *tconn)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001358{
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001359 struct sock *sk = tconn->data.socket->sk;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001360 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001361 set_bit(NET_CONGESTED, &tconn->flags);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001362}
1363
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364/* The idea of sendpage seems to be to put some kind of reference
1365 * to the page into the skb, and to hand it over to the NIC. In
1366 * this process get_page() gets called.
1367 *
1368 * As soon as the page was really sent over the network put_page()
1369 * gets called by some part of the network layer. [ NIC driver? ]
1370 *
1371 * [ get_page() / put_page() increment/decrement the count. If count
1372 * reaches 0 the page will be freed. ]
1373 *
1374 * This works nicely with pages from FSs.
1375 * But this means that in protocol A we might signal IO completion too early!
1376 *
1377 * In order not to corrupt data during a resync we must make sure
1378 * that we do not reuse our own buffer pages (EEs) to early, therefore
1379 * we have the net_ee list.
1380 *
1381 * XFS seems to have problems, still, it submits pages with page_count == 0!
1382 * As a workaround, we disable sendpage on pages
1383 * with page_count == 0 or PageSlab.
1384 */
1385static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001386 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001387{
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001388 int sent = drbd_send(mdev->tconn, mdev->tconn->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001389 kunmap(page);
1390 if (sent == size)
1391 mdev->send_cnt += size>>9;
1392 return sent == size;
1393}
1394
1395static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001396 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397{
1398 mm_segment_t oldfs = get_fs();
1399 int sent, ok;
1400 int len = size;
1401
1402 /* e.g. XFS meta- & log-data is in slab pages, which have a
1403 * page_count of 0 and/or have PageSlab() set.
1404 * we cannot use send_page for those, as that does get_page();
1405 * put_page(); and would cause either a VM_BUG directly, or
1406 * __page_cache_release a page that would actually still be referenced
1407 * by someone, leading to some obscure delayed Oops somewhere else. */
1408 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001409 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001411 msg_flags |= MSG_NOSIGNAL;
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001412 drbd_update_congested(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001413 set_fs(KERNEL_DS);
1414 do {
Philipp Reisnere42325a2011-01-19 13:55:45 +01001415 sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001416 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001417 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001418 if (sent == -EAGAIN) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001419 if (we_should_drop_the_connection(mdev->tconn,
Philipp Reisnere42325a2011-01-19 13:55:45 +01001420 mdev->tconn->data.socket))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421 break;
1422 else
1423 continue;
1424 }
1425 if (sent <= 0) {
1426 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1427 __func__, (int)size, len, sent);
1428 break;
1429 }
1430 len -= sent;
1431 offset += sent;
1432 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
1433 set_fs(oldfs);
Philipp Reisner01a311a2011-02-07 14:30:33 +01001434 clear_bit(NET_CONGESTED, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435
1436 ok = (len == 0);
1437 if (likely(ok))
1438 mdev->send_cnt += size>>9;
1439 return ok;
1440}
1441
1442static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
1443{
1444 struct bio_vec *bvec;
1445 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001446 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001447 __bio_for_each_segment(bvec, bio, i, 0) {
1448 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001449 bvec->bv_offset, bvec->bv_len,
1450 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451 return 0;
1452 }
1453 return 1;
1454}
1455
1456static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
1457{
1458 struct bio_vec *bvec;
1459 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001460 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461 __bio_for_each_segment(bvec, bio, i, 0) {
1462 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001463 bvec->bv_offset, bvec->bv_len,
1464 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 return 0;
1466 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467 return 1;
1468}
1469
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001470static int _drbd_send_zc_ee(struct drbd_conf *mdev,
1471 struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001472{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001473 struct page *page = peer_req->pages;
1474 unsigned len = peer_req->i.size;
1475
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001476 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001477 page_chain_for_each(page) {
1478 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001479 if (!_drbd_send_page(mdev, page, 0, l,
1480 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001481 return 0;
1482 len -= l;
1483 }
1484 return 1;
1485}
1486
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001487static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
1488{
Philipp Reisner31890f42011-01-19 14:12:51 +01001489 if (mdev->tconn->agreed_pro_version >= 95)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001490 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001491 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1492 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1493 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1494 else
Jens Axboe721a9602011-03-09 11:56:30 +01001495 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001496}
1497
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498/* Used to send write requests
1499 * R_PRIMARY -> Peer (P_DATA)
1500 */
1501int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
1502{
1503 int ok = 1;
1504 struct p_data p;
1505 unsigned int dp_flags = 0;
1506 void *dgb;
1507 int dgs;
1508
1509 if (!drbd_get_data_sock(mdev))
1510 return 0;
1511
Philipp Reisnera0638452011-01-19 14:31:32 +01001512 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1513 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001515 prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size);
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001516 p.sector = cpu_to_be64(req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 p.block_id = (unsigned long)req;
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001518 p.seq_num = cpu_to_be32(req->seq_num = atomic_add_return(1, &mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001520 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
1521
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522 if (mdev->state.conn >= C_SYNC_SOURCE &&
1523 mdev->state.conn <= C_PAUSED_SYNC_T)
1524 dp_flags |= DP_MAY_SET_IN_SYNC;
1525
1526 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 set_bit(UNPLUG_REMOTE, &mdev->flags);
1528 ok = (sizeof(p) ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001529 drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001530 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001531 dgb = mdev->tconn->int_dig_out;
1532 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001533 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 }
1535 if (ok) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001536 /* For protocol A, we have to memcpy the payload into
1537 * socket buffers, as we may complete right away
1538 * as soon as we handed it over to tcp, at which point the data
1539 * pages may become invalid.
1540 *
1541 * For data-integrity enabled, we copy it as well, so we can be
1542 * sure that even if the bio pages may still be modified, it
1543 * won't change the data on the wire, thus if the digest checks
1544 * out ok after sending on this side, but does not fit on the
1545 * receiving side, we sure have detected corruption elsewhere.
1546 */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001547 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001548 ok = _drbd_send_bio(mdev, req->master_bio);
1549 else
1550 ok = _drbd_send_zc_bio(mdev, req->master_bio);
Lars Ellenberg470be442010-11-10 10:36:52 +01001551
1552 /* double check digest, sometimes buffers have been modified in flight. */
1553 if (dgs > 0 && dgs <= 64) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001554 /* 64 byte, 512 bit, is the largest digest size
Lars Ellenberg470be442010-11-10 10:36:52 +01001555 * currently supported in kernel crypto. */
1556 unsigned char digest[64];
Philipp Reisnera0638452011-01-19 14:31:32 +01001557 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest);
1558 if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001559 dev_warn(DEV,
1560 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001561 (unsigned long long)req->i.sector, req->i.size);
Lars Ellenberg470be442010-11-10 10:36:52 +01001562 }
1563 } /* else if (dgs > 64) {
1564 ... Be noisy about digest too large ...
1565 } */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566 }
1567
1568 drbd_put_data_sock(mdev);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001569
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 return ok;
1571}
1572
1573/* answer packet, used to send data back for read requests:
1574 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1575 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1576 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001577int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001578 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001579{
1580 int ok;
1581 struct p_data p;
1582 void *dgb;
1583 int dgs;
1584
Philipp Reisnera0638452011-01-19 14:31:32 +01001585 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1586 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001588 prepare_header(mdev, &p.head, cmd, sizeof(p) -
1589 sizeof(struct p_header80) +
1590 dgs + peer_req->i.size);
1591 p.sector = cpu_to_be64(peer_req->i.sector);
1592 p.block_id = peer_req->block_id;
Andreas Gruenbachercc378272011-01-26 18:01:50 +01001593 p.seq_num = 0; /* unused */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594
1595 /* Only called by our kernel thread.
1596 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
1597 * in response to admin command or module unload.
1598 */
1599 if (!drbd_get_data_sock(mdev))
1600 return 0;
1601
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001602 ok = sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001603 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001604 dgb = mdev->tconn->int_dig_out;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001605 drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001606 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607 }
1608 if (ok)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001609 ok = _drbd_send_zc_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001610
1611 drbd_put_data_sock(mdev);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001612
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613 return ok;
1614}
1615
Philipp Reisner73a01a12010-10-27 14:33:00 +02001616int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
1617{
1618 struct p_block_desc p;
1619
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001620 p.sector = cpu_to_be64(req->i.sector);
1621 p.blksize = cpu_to_be32(req->i.size);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001622
1623 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p));
1624}
1625
Philipp Reisnerb411b362009-09-25 16:07:19 -07001626/*
1627 drbd_send distinguishes two cases:
1628
1629 Packets sent via the data socket "sock"
1630 and packets sent via the meta data socket "msock"
1631
1632 sock msock
1633 -----------------+-------------------------+------------------------------
1634 timeout conf.timeout / 2 conf.timeout / 2
1635 timeout action send a ping via msock Abort communication
1636 and close all sockets
1637*/
1638
1639/*
1640 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1641 */
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001642int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001643 void *buf, size_t size, unsigned msg_flags)
1644{
1645 struct kvec iov;
1646 struct msghdr msg;
1647 int rv, sent = 0;
1648
1649 if (!sock)
1650 return -1000;
1651
1652 /* THINK if (signal_pending) return ... ? */
1653
1654 iov.iov_base = buf;
1655 iov.iov_len = size;
1656
1657 msg.msg_name = NULL;
1658 msg.msg_namelen = 0;
1659 msg.msg_control = NULL;
1660 msg.msg_controllen = 0;
1661 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1662
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001663 if (sock == tconn->data.socket) {
1664 tconn->ko_count = tconn->net_conf->ko_count;
1665 drbd_update_congested(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001666 }
1667 do {
1668 /* STRANGE
1669 * tcp_sendmsg does _not_ use its size parameter at all ?
1670 *
1671 * -EAGAIN on timeout, -EINTR on signal.
1672 */
1673/* THINK
1674 * do we need to block DRBD_SIG if sock == &meta.socket ??
1675 * otherwise wake_asender() might interrupt some send_*Ack !
1676 */
1677 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1678 if (rv == -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001679 if (we_should_drop_the_connection(tconn, sock))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001680 break;
1681 else
1682 continue;
1683 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684 if (rv == -EINTR) {
1685 flush_signals(current);
1686 rv = 0;
1687 }
1688 if (rv < 0)
1689 break;
1690 sent += rv;
1691 iov.iov_base += rv;
1692 iov.iov_len -= rv;
1693 } while (sent < size);
1694
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001695 if (sock == tconn->data.socket)
1696 clear_bit(NET_CONGESTED, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697
1698 if (rv <= 0) {
1699 if (rv != -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001700 conn_err(tconn, "%s_sendmsg returned %d\n",
1701 sock == tconn->meta.socket ? "msock" : "sock",
1702 rv);
1703 drbd_force_state(tconn->volume0, NS(conn, C_BROKEN_PIPE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001704 } else
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001705 drbd_force_state(tconn->volume0, NS(conn, C_TIMEOUT));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001706 }
1707
1708 return sent;
1709}
1710
1711static int drbd_open(struct block_device *bdev, fmode_t mode)
1712{
1713 struct drbd_conf *mdev = bdev->bd_disk->private_data;
1714 unsigned long flags;
1715 int rv = 0;
1716
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001717 mutex_lock(&drbd_main_mutex);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001718 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719 /* to have a stable mdev->state.role
1720 * and no race with updating open_cnt */
1721
1722 if (mdev->state.role != R_PRIMARY) {
1723 if (mode & FMODE_WRITE)
1724 rv = -EROFS;
1725 else if (!allow_oos)
1726 rv = -EMEDIUMTYPE;
1727 }
1728
1729 if (!rv)
1730 mdev->open_cnt++;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001731 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001732 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001733
1734 return rv;
1735}
1736
1737static int drbd_release(struct gendisk *gd, fmode_t mode)
1738{
1739 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001740 mutex_lock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741 mdev->open_cnt--;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001742 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001743 return 0;
1744}
1745
Philipp Reisnerb411b362009-09-25 16:07:19 -07001746static void drbd_set_defaults(struct drbd_conf *mdev)
1747{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001748 /* This way we get a compile error when sync_conf grows,
1749 and we forgot to initialize it here */
1750 mdev->sync_conf = (struct syncer_conf) {
1751 /* .rate = */ DRBD_RATE_DEF,
1752 /* .after = */ DRBD_AFTER_DEF,
1753 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001754 /* .verify_alg = */ {}, 0,
1755 /* .cpu_mask = */ {}, 0,
1756 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02001757 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02001758 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
1759 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
1760 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
1761 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001762 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
1763 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001764 };
1765
1766 /* Have to use that way, because the layout differs between
1767 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001768 mdev->state = (union drbd_state) {
1769 { .role = R_SECONDARY,
1770 .peer = R_UNKNOWN,
1771 .conn = C_STANDALONE,
1772 .disk = D_DISKLESS,
1773 .pdsk = D_UNKNOWN,
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001774 .susp = 0,
1775 .susp_nod = 0,
1776 .susp_fen = 0
Philipp Reisnerb411b362009-09-25 16:07:19 -07001777 } };
1778}
1779
1780void drbd_init_set_defaults(struct drbd_conf *mdev)
1781{
1782 /* the memset(,0,) did most of this.
1783 * note: only assignments, no allocation in here */
1784
1785 drbd_set_defaults(mdev);
1786
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787 atomic_set(&mdev->ap_bio_cnt, 0);
1788 atomic_set(&mdev->ap_pending_cnt, 0);
1789 atomic_set(&mdev->rs_pending_cnt, 0);
1790 atomic_set(&mdev->unacked_cnt, 0);
1791 atomic_set(&mdev->local_cnt, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792 atomic_set(&mdev->pp_in_use, 0);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001793 atomic_set(&mdev->pp_in_use_by_net, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02001794 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001795 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001796 atomic_set(&mdev->ap_in_flight, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797
1798 mutex_init(&mdev->md_io_mutex);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001799 mutex_init(&mdev->tconn->data.mutex);
1800 mutex_init(&mdev->tconn->meta.mutex);
1801 sema_init(&mdev->tconn->data.work.s, 0);
1802 sema_init(&mdev->tconn->meta.work.s, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803 mutex_init(&mdev->state_mutex);
1804
Philipp Reisnere42325a2011-01-19 13:55:45 +01001805 spin_lock_init(&mdev->tconn->data.work.q_lock);
1806 spin_lock_init(&mdev->tconn->meta.work.q_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001807
1808 spin_lock_init(&mdev->al_lock);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001809 spin_lock_init(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001810 spin_lock_init(&mdev->peer_seq_lock);
1811 spin_lock_init(&mdev->epoch_lock);
1812
1813 INIT_LIST_HEAD(&mdev->active_ee);
1814 INIT_LIST_HEAD(&mdev->sync_ee);
1815 INIT_LIST_HEAD(&mdev->done_ee);
1816 INIT_LIST_HEAD(&mdev->read_ee);
1817 INIT_LIST_HEAD(&mdev->net_ee);
1818 INIT_LIST_HEAD(&mdev->resync_reads);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001819 INIT_LIST_HEAD(&mdev->tconn->data.work.q);
1820 INIT_LIST_HEAD(&mdev->tconn->meta.work.q);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001821 INIT_LIST_HEAD(&mdev->resync_work.list);
1822 INIT_LIST_HEAD(&mdev->unplug_work.list);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001823 INIT_LIST_HEAD(&mdev->go_diskless.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001824 INIT_LIST_HEAD(&mdev->md_sync_work.list);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001825 INIT_LIST_HEAD(&mdev->start_resync_work.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001826 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02001827
Philipp Reisner794abb72010-12-27 11:51:23 +01001828 mdev->resync_work.cb = w_resync_timer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829 mdev->unplug_work.cb = w_send_write_hint;
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001830 mdev->go_diskless.cb = w_go_diskless;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 mdev->md_sync_work.cb = w_md_sync;
1832 mdev->bm_io_work.w.cb = w_bitmap_io;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001833 mdev->start_resync_work.cb = w_start_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 init_timer(&mdev->resync_timer);
1835 init_timer(&mdev->md_sync_timer);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001836 init_timer(&mdev->start_resync_timer);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001837 init_timer(&mdev->request_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001838 mdev->resync_timer.function = resync_timer_fn;
1839 mdev->resync_timer.data = (unsigned long) mdev;
1840 mdev->md_sync_timer.function = md_sync_timer_fn;
1841 mdev->md_sync_timer.data = (unsigned long) mdev;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001842 mdev->start_resync_timer.function = start_resync_timer_fn;
1843 mdev->start_resync_timer.data = (unsigned long) mdev;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001844 mdev->request_timer.function = request_timer_fn;
1845 mdev->request_timer.data = (unsigned long) mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846
1847 init_waitqueue_head(&mdev->misc_wait);
1848 init_waitqueue_head(&mdev->state_wait);
1849 init_waitqueue_head(&mdev->ee_wait);
1850 init_waitqueue_head(&mdev->al_wait);
1851 init_waitqueue_head(&mdev->seq_wait);
1852
Philipp Reisnerbed879a2011-02-04 14:00:37 +01001853 drbd_thread_init(mdev, &mdev->tconn->receiver, drbdd_init, "receiver");
1854 drbd_thread_init(mdev, &mdev->tconn->worker, drbd_worker, "worker");
1855 drbd_thread_init(mdev, &mdev->tconn->asender, drbd_asender, "asender");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001857 /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */
Philipp Reisner2451fc32010-08-24 13:43:11 +02001858 mdev->write_ordering = WO_bdev_flush;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859 mdev->resync_wenr = LC_FREE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001860 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1861 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001862}
1863
1864void drbd_mdev_cleanup(struct drbd_conf *mdev)
1865{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001866 int i;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001867 if (mdev->tconn->receiver.t_state != NONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001868 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001869 mdev->tconn->receiver.t_state);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001870
1871 /* no need to lock it, I'm the only thread alive */
1872 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
1873 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
1874 mdev->al_writ_cnt =
1875 mdev->bm_writ_cnt =
1876 mdev->read_cnt =
1877 mdev->recv_cnt =
1878 mdev->send_cnt =
1879 mdev->writ_cnt =
1880 mdev->p_size =
1881 mdev->rs_start =
1882 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001883 mdev->rs_failed = 0;
1884 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001885 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001886 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1887 mdev->rs_mark_left[i] = 0;
1888 mdev->rs_mark_time[i] = 0;
1889 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01001890 D_ASSERT(mdev->tconn->net_conf == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001891
1892 drbd_set_my_capacity(mdev, 0);
1893 if (mdev->bitmap) {
1894 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01001895 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001896 drbd_bm_cleanup(mdev);
1897 }
1898
1899 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02001900 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001901
1902 /*
1903 * currently we drbd_init_ee only on module load, so
1904 * we may do drbd_release_ee only on module unload!
1905 */
1906 D_ASSERT(list_empty(&mdev->active_ee));
1907 D_ASSERT(list_empty(&mdev->sync_ee));
1908 D_ASSERT(list_empty(&mdev->done_ee));
1909 D_ASSERT(list_empty(&mdev->read_ee));
1910 D_ASSERT(list_empty(&mdev->net_ee));
1911 D_ASSERT(list_empty(&mdev->resync_reads));
Philipp Reisnere42325a2011-01-19 13:55:45 +01001912 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
1913 D_ASSERT(list_empty(&mdev->tconn->meta.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001914 D_ASSERT(list_empty(&mdev->resync_work.list));
1915 D_ASSERT(list_empty(&mdev->unplug_work.list));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001916 D_ASSERT(list_empty(&mdev->go_diskless.list));
Lars Ellenberg2265b472010-12-16 15:41:26 +01001917
1918 drbd_set_defaults(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001919}
1920
1921
1922static void drbd_destroy_mempools(void)
1923{
1924 struct page *page;
1925
1926 while (drbd_pp_pool) {
1927 page = drbd_pp_pool;
1928 drbd_pp_pool = (struct page *)page_private(page);
1929 __free_page(page);
1930 drbd_pp_vacant--;
1931 }
1932
1933 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
1934
1935 if (drbd_ee_mempool)
1936 mempool_destroy(drbd_ee_mempool);
1937 if (drbd_request_mempool)
1938 mempool_destroy(drbd_request_mempool);
1939 if (drbd_ee_cache)
1940 kmem_cache_destroy(drbd_ee_cache);
1941 if (drbd_request_cache)
1942 kmem_cache_destroy(drbd_request_cache);
1943 if (drbd_bm_ext_cache)
1944 kmem_cache_destroy(drbd_bm_ext_cache);
1945 if (drbd_al_ext_cache)
1946 kmem_cache_destroy(drbd_al_ext_cache);
1947
1948 drbd_ee_mempool = NULL;
1949 drbd_request_mempool = NULL;
1950 drbd_ee_cache = NULL;
1951 drbd_request_cache = NULL;
1952 drbd_bm_ext_cache = NULL;
1953 drbd_al_ext_cache = NULL;
1954
1955 return;
1956}
1957
1958static int drbd_create_mempools(void)
1959{
1960 struct page *page;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001961 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001962 int i;
1963
1964 /* prepare our caches and mempools */
1965 drbd_request_mempool = NULL;
1966 drbd_ee_cache = NULL;
1967 drbd_request_cache = NULL;
1968 drbd_bm_ext_cache = NULL;
1969 drbd_al_ext_cache = NULL;
1970 drbd_pp_pool = NULL;
1971
1972 /* caches */
1973 drbd_request_cache = kmem_cache_create(
1974 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
1975 if (drbd_request_cache == NULL)
1976 goto Enomem;
1977
1978 drbd_ee_cache = kmem_cache_create(
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001979 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980 if (drbd_ee_cache == NULL)
1981 goto Enomem;
1982
1983 drbd_bm_ext_cache = kmem_cache_create(
1984 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
1985 if (drbd_bm_ext_cache == NULL)
1986 goto Enomem;
1987
1988 drbd_al_ext_cache = kmem_cache_create(
1989 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
1990 if (drbd_al_ext_cache == NULL)
1991 goto Enomem;
1992
1993 /* mempools */
1994 drbd_request_mempool = mempool_create(number,
1995 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
1996 if (drbd_request_mempool == NULL)
1997 goto Enomem;
1998
1999 drbd_ee_mempool = mempool_create(number,
2000 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
Nicolas Kaiser2027ae12010-10-28 06:15:26 -06002001 if (drbd_ee_mempool == NULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002002 goto Enomem;
2003
2004 /* drbd's page pool */
2005 spin_lock_init(&drbd_pp_lock);
2006
2007 for (i = 0; i < number; i++) {
2008 page = alloc_page(GFP_HIGHUSER);
2009 if (!page)
2010 goto Enomem;
2011 set_page_private(page, (unsigned long)drbd_pp_pool);
2012 drbd_pp_pool = page;
2013 }
2014 drbd_pp_vacant = number;
2015
2016 return 0;
2017
2018Enomem:
2019 drbd_destroy_mempools(); /* in case we allocated some */
2020 return -ENOMEM;
2021}
2022
2023static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2024 void *unused)
2025{
2026 /* just so we have it. you never know what interesting things we
2027 * might want to do here some day...
2028 */
2029
2030 return NOTIFY_DONE;
2031}
2032
2033static struct notifier_block drbd_notifier = {
2034 .notifier_call = drbd_notify_sys,
2035};
2036
2037static void drbd_release_ee_lists(struct drbd_conf *mdev)
2038{
2039 int rr;
2040
2041 rr = drbd_release_ee(mdev, &mdev->active_ee);
2042 if (rr)
2043 dev_err(DEV, "%d EEs in active list found!\n", rr);
2044
2045 rr = drbd_release_ee(mdev, &mdev->sync_ee);
2046 if (rr)
2047 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2048
2049 rr = drbd_release_ee(mdev, &mdev->read_ee);
2050 if (rr)
2051 dev_err(DEV, "%d EEs in read list found!\n", rr);
2052
2053 rr = drbd_release_ee(mdev, &mdev->done_ee);
2054 if (rr)
2055 dev_err(DEV, "%d EEs in done list found!\n", rr);
2056
2057 rr = drbd_release_ee(mdev, &mdev->net_ee);
2058 if (rr)
2059 dev_err(DEV, "%d EEs in net list found!\n", rr);
2060}
2061
2062/* caution. no locking.
2063 * currently only used from module cleanup code. */
2064static void drbd_delete_device(unsigned int minor)
2065{
2066 struct drbd_conf *mdev = minor_to_mdev(minor);
2067
2068 if (!mdev)
2069 return;
2070
2071 /* paranoia asserts */
Andreas Gruenbacher70dc65e2010-12-21 14:46:57 +01002072 D_ASSERT(mdev->open_cnt == 0);
Philipp Reisnere42325a2011-01-19 13:55:45 +01002073 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002074 /* end paranoia asserts */
2075
2076 del_gendisk(mdev->vdisk);
2077
2078 /* cleanup stuff that may have been allocated during
2079 * device (re-)configuration or state changes */
2080
2081 if (mdev->this_bdev)
2082 bdput(mdev->this_bdev);
2083
2084 drbd_free_resources(mdev);
Philipp Reisner21114382011-01-19 12:26:59 +01002085 drbd_free_tconn(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002086
2087 drbd_release_ee_lists(mdev);
2088
Philipp Reisnerb411b362009-09-25 16:07:19 -07002089 lc_destroy(mdev->act_log);
2090 lc_destroy(mdev->resync);
2091
2092 kfree(mdev->p_uuid);
2093 /* mdev->p_uuid = NULL; */
2094
Philipp Reisnerb411b362009-09-25 16:07:19 -07002095 /* cleanup the rest that has been
2096 * allocated from drbd_new_device
2097 * and actually free the mdev itself */
2098 drbd_free_mdev(mdev);
2099}
2100
2101static void drbd_cleanup(void)
2102{
2103 unsigned int i;
2104
2105 unregister_reboot_notifier(&drbd_notifier);
2106
Lars Ellenberg17a93f32010-11-24 10:37:35 +01002107 /* first remove proc,
2108 * drbdsetup uses it's presence to detect
2109 * whether DRBD is loaded.
2110 * If we would get stuck in proc removal,
2111 * but have netlink already deregistered,
2112 * some drbdsetup commands may wait forever
2113 * for an answer.
2114 */
2115 if (drbd_proc)
2116 remove_proc_entry("drbd", NULL);
2117
Philipp Reisnerb411b362009-09-25 16:07:19 -07002118 drbd_nl_cleanup();
2119
2120 if (minor_table) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002121 i = minor_count;
2122 while (i--)
2123 drbd_delete_device(i);
2124 drbd_destroy_mempools();
2125 }
2126
2127 kfree(minor_table);
2128
2129 unregister_blkdev(DRBD_MAJOR, "drbd");
2130
2131 printk(KERN_INFO "drbd: module cleanup done.\n");
2132}
2133
2134/**
2135 * drbd_congested() - Callback for pdflush
2136 * @congested_data: User data
2137 * @bdi_bits: Bits pdflush is currently interested in
2138 *
2139 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2140 */
2141static int drbd_congested(void *congested_data, int bdi_bits)
2142{
2143 struct drbd_conf *mdev = congested_data;
2144 struct request_queue *q;
2145 char reason = '-';
2146 int r = 0;
2147
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002148 if (!may_inc_ap_bio(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002149 /* DRBD has frozen IO */
2150 r = bdi_bits;
2151 reason = 'd';
2152 goto out;
2153 }
2154
2155 if (get_ldev(mdev)) {
2156 q = bdev_get_queue(mdev->ldev->backing_bdev);
2157 r = bdi_congested(&q->backing_dev_info, bdi_bits);
2158 put_ldev(mdev);
2159 if (r)
2160 reason = 'b';
2161 }
2162
Philipp Reisner01a311a2011-02-07 14:30:33 +01002163 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002164 r |= (1 << BDI_async_congested);
2165 reason = reason == 'b' ? 'a' : 'n';
2166 }
2167
2168out:
2169 mdev->congestion_reason = reason;
2170 return r;
2171}
2172
Philipp Reisner21114382011-01-19 12:26:59 +01002173struct drbd_tconn *drbd_new_tconn(char *name)
2174{
2175 struct drbd_tconn *tconn;
2176
2177 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2178 if (!tconn)
2179 return NULL;
2180
2181 tconn->name = kstrdup(name, GFP_KERNEL);
2182 if (!tconn->name)
2183 goto fail;
2184
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002185 atomic_set(&tconn->net_cnt, 0);
2186 init_waitqueue_head(&tconn->net_cnt_wait);
2187
Philipp Reisner21114382011-01-19 12:26:59 +01002188 write_lock_irq(&global_state_lock);
2189 list_add(&tconn->all_tconn, &drbd_tconns);
2190 write_unlock_irq(&global_state_lock);
2191
2192 return tconn;
2193
2194fail:
2195 kfree(tconn->name);
2196 kfree(tconn);
2197
2198 return NULL;
2199}
2200
2201void drbd_free_tconn(struct drbd_tconn *tconn)
2202{
2203 write_lock_irq(&global_state_lock);
2204 list_del(&tconn->all_tconn);
2205 write_unlock_irq(&global_state_lock);
2206
2207 kfree(tconn->name);
Philipp Reisnerb42a70a2011-01-27 10:55:20 +01002208 kfree(tconn->int_dig_out);
2209 kfree(tconn->int_dig_in);
2210 kfree(tconn->int_dig_vv);
Philipp Reisner21114382011-01-19 12:26:59 +01002211 kfree(tconn);
2212}
2213
Philipp Reisnerb411b362009-09-25 16:07:19 -07002214struct drbd_conf *drbd_new_device(unsigned int minor)
2215{
2216 struct drbd_conf *mdev;
2217 struct gendisk *disk;
2218 struct request_queue *q;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002219 char conn_name[9]; /* drbd1234N */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002220
2221 /* GFP_KERNEL, we are outside of all write-out paths */
2222 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
2223 if (!mdev)
2224 return NULL;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002225 sprintf(conn_name, "drbd%d", minor);
2226 mdev->tconn = drbd_new_tconn(conn_name);
Philipp Reisner21114382011-01-19 12:26:59 +01002227 if (!mdev->tconn)
2228 goto out_no_tconn;
2229
Philipp Reisnerb411b362009-09-25 16:07:19 -07002230 if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL))
2231 goto out_no_cpumask;
2232
Philipp Reisner21114382011-01-19 12:26:59 +01002233 mdev->tconn->volume0 = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002234 mdev->minor = minor;
2235
2236 drbd_init_set_defaults(mdev);
2237
2238 q = blk_alloc_queue(GFP_KERNEL);
2239 if (!q)
2240 goto out_no_q;
2241 mdev->rq_queue = q;
2242 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002243
2244 disk = alloc_disk(1);
2245 if (!disk)
2246 goto out_no_disk;
2247 mdev->vdisk = disk;
2248
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002249 set_disk_ro(disk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250
2251 disk->queue = q;
2252 disk->major = DRBD_MAJOR;
2253 disk->first_minor = minor;
2254 disk->fops = &drbd_ops;
2255 sprintf(disk->disk_name, "drbd%d", minor);
2256 disk->private_data = mdev;
2257
2258 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
2259 /* we have no partitions. we contain only ourselves. */
2260 mdev->this_bdev->bd_contains = mdev->this_bdev;
2261
2262 q->backing_dev_info.congested_fn = drbd_congested;
2263 q->backing_dev_info.congested_data = mdev;
2264
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01002265 blk_queue_make_request(q, drbd_make_request);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002266 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2267 This triggers a max_bio_size message upon first attach or connect */
2268 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2270 blk_queue_merge_bvec(q, drbd_merge_bvec);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002271 q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272
2273 mdev->md_io_page = alloc_page(GFP_KERNEL);
2274 if (!mdev->md_io_page)
2275 goto out_no_io_page;
2276
2277 if (drbd_bm_init(mdev))
2278 goto out_no_bitmap;
2279 /* no need to lock access, we are still initializing this minor device. */
2280 if (!tl_init(mdev))
2281 goto out_no_tl;
Andreas Gruenbacherdac13892011-01-21 17:18:39 +01002282 mdev->read_requests = RB_ROOT;
Andreas Gruenbacherde696712011-01-20 15:00:24 +01002283 mdev->write_requests = RB_ROOT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2286 if (!mdev->current_epoch)
2287 goto out_no_epoch;
2288
2289 INIT_LIST_HEAD(&mdev->current_epoch->list);
2290 mdev->epochs = 1;
2291
2292 return mdev;
2293
2294/* out_whatever_else:
2295 kfree(mdev->current_epoch); */
2296out_no_epoch:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002297 tl_cleanup(mdev);
2298out_no_tl:
2299 drbd_bm_cleanup(mdev);
2300out_no_bitmap:
2301 __free_page(mdev->md_io_page);
2302out_no_io_page:
2303 put_disk(disk);
2304out_no_disk:
2305 blk_cleanup_queue(q);
2306out_no_q:
2307 free_cpumask_var(mdev->cpu_mask);
2308out_no_cpumask:
Philipp Reisner21114382011-01-19 12:26:59 +01002309 drbd_free_tconn(mdev->tconn);
2310out_no_tconn:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002311 kfree(mdev);
2312 return NULL;
2313}
2314
2315/* counterpart of drbd_new_device.
2316 * last part of drbd_delete_device. */
2317void drbd_free_mdev(struct drbd_conf *mdev)
2318{
2319 kfree(mdev->current_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002320 tl_cleanup(mdev);
2321 if (mdev->bitmap) /* should no longer be there. */
2322 drbd_bm_cleanup(mdev);
2323 __free_page(mdev->md_io_page);
2324 put_disk(mdev->vdisk);
2325 blk_cleanup_queue(mdev->rq_queue);
2326 free_cpumask_var(mdev->cpu_mask);
2327 kfree(mdev);
2328}
2329
2330
2331int __init drbd_init(void)
2332{
2333 int err;
2334
Philipp Reisnerfd340c12011-01-19 16:57:39 +01002335 BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95));
2336 BUILD_BUG_ON(sizeof(struct p_handshake) != 80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002337
Philipp Reisner2b8a90b2011-01-10 11:15:17 +01002338 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002339 printk(KERN_ERR
2340 "drbd: invalid minor_count (%d)\n", minor_count);
2341#ifdef MODULE
2342 return -EINVAL;
2343#else
2344 minor_count = 8;
2345#endif
2346 }
2347
2348 err = drbd_nl_init();
2349 if (err)
2350 return err;
2351
2352 err = register_blkdev(DRBD_MAJOR, "drbd");
2353 if (err) {
2354 printk(KERN_ERR
2355 "drbd: unable to register block device major %d\n",
2356 DRBD_MAJOR);
2357 return err;
2358 }
2359
2360 register_reboot_notifier(&drbd_notifier);
2361
2362 /*
2363 * allocate all necessary structs
2364 */
2365 err = -ENOMEM;
2366
2367 init_waitqueue_head(&drbd_pp_wait);
2368
2369 drbd_proc = NULL; /* play safe for drbd_cleanup */
2370 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
2371 GFP_KERNEL);
2372 if (!minor_table)
2373 goto Enomem;
2374
2375 err = drbd_create_mempools();
2376 if (err)
2377 goto Enomem;
2378
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01002379 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002380 if (!drbd_proc) {
2381 printk(KERN_ERR "drbd: unable to register proc file\n");
2382 goto Enomem;
2383 }
2384
2385 rwlock_init(&global_state_lock);
Philipp Reisner21114382011-01-19 12:26:59 +01002386 INIT_LIST_HEAD(&drbd_tconns);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387
2388 printk(KERN_INFO "drbd: initialized. "
2389 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2390 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2391 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2392 printk(KERN_INFO "drbd: registered as block device major %d\n",
2393 DRBD_MAJOR);
2394 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
2395
2396 return 0; /* Success! */
2397
2398Enomem:
2399 drbd_cleanup();
2400 if (err == -ENOMEM)
2401 /* currently always the case */
2402 printk(KERN_ERR "drbd: ran out of memory\n");
2403 else
2404 printk(KERN_ERR "drbd: initialization failure\n");
2405 return err;
2406}
2407
2408void drbd_free_bc(struct drbd_backing_dev *ldev)
2409{
2410 if (ldev == NULL)
2411 return;
2412
Tejun Heoe525fd82010-11-13 11:55:17 +01002413 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2414 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415
2416 kfree(ldev);
2417}
2418
2419void drbd_free_sock(struct drbd_conf *mdev)
2420{
Philipp Reisnere42325a2011-01-19 13:55:45 +01002421 if (mdev->tconn->data.socket) {
2422 mutex_lock(&mdev->tconn->data.mutex);
2423 kernel_sock_shutdown(mdev->tconn->data.socket, SHUT_RDWR);
2424 sock_release(mdev->tconn->data.socket);
2425 mdev->tconn->data.socket = NULL;
2426 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427 }
Philipp Reisnere42325a2011-01-19 13:55:45 +01002428 if (mdev->tconn->meta.socket) {
2429 mutex_lock(&mdev->tconn->meta.mutex);
2430 kernel_sock_shutdown(mdev->tconn->meta.socket, SHUT_RDWR);
2431 sock_release(mdev->tconn->meta.socket);
2432 mdev->tconn->meta.socket = NULL;
2433 mutex_unlock(&mdev->tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434 }
2435}
2436
2437
2438void drbd_free_resources(struct drbd_conf *mdev)
2439{
2440 crypto_free_hash(mdev->csums_tfm);
2441 mdev->csums_tfm = NULL;
2442 crypto_free_hash(mdev->verify_tfm);
2443 mdev->verify_tfm = NULL;
Philipp Reisnera0638452011-01-19 14:31:32 +01002444 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
2445 mdev->tconn->cram_hmac_tfm = NULL;
2446 crypto_free_hash(mdev->tconn->integrity_w_tfm);
2447 mdev->tconn->integrity_w_tfm = NULL;
2448 crypto_free_hash(mdev->tconn->integrity_r_tfm);
2449 mdev->tconn->integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450
2451 drbd_free_sock(mdev);
2452
2453 __no_warn(local,
2454 drbd_free_bc(mdev->ldev);
2455 mdev->ldev = NULL;);
2456}
2457
2458/* meta data management */
2459
2460struct meta_data_on_disk {
2461 u64 la_size; /* last agreed size. */
2462 u64 uuid[UI_SIZE]; /* UUIDs. */
2463 u64 device_uuid;
2464 u64 reserved_u64_1;
2465 u32 flags; /* MDF */
2466 u32 magic;
2467 u32 md_size_sect;
2468 u32 al_offset; /* offset to this block */
2469 u32 al_nr_extents; /* important for restoring the AL */
2470 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
2471 u32 bm_offset; /* offset to the bitmap, from here */
2472 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
Philipp Reisner99432fc2011-05-20 16:39:13 +02002473 u32 la_peer_max_bio_size; /* last peer max_bio_size */
2474 u32 reserved_u32[3];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475
2476} __packed;
2477
2478/**
2479 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2480 * @mdev: DRBD device.
2481 */
2482void drbd_md_sync(struct drbd_conf *mdev)
2483{
2484 struct meta_data_on_disk *buffer;
2485 sector_t sector;
2486 int i;
2487
Lars Ellenbergee15b032010-09-03 10:00:09 +02002488 del_timer(&mdev->md_sync_timer);
2489 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
2491 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002492
2493 /* We use here D_FAILED and not D_ATTACHING because we try to write
2494 * metadata even if we detach due to a disk failure! */
2495 if (!get_ldev_if_state(mdev, D_FAILED))
2496 return;
2497
Philipp Reisnerb411b362009-09-25 16:07:19 -07002498 mutex_lock(&mdev->md_io_mutex);
2499 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2500 memset(buffer, 0, 512);
2501
2502 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
2503 for (i = UI_CURRENT; i < UI_SIZE; i++)
2504 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
2505 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
2506 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
2507
2508 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
2509 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
2510 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
2511 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
2512 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
2513
2514 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002515 buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516
2517 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
2518 sector = mdev->ldev->md.md_offset;
2519
Lars Ellenberg3f3a9b82010-09-01 15:12:12 +02002520 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521 /* this was a try anyways ... */
2522 dev_err(DEV, "meta data update failed!\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002523 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002524 }
2525
2526 /* Update mdev->ldev->md.la_size_sect,
2527 * since we updated it on metadata. */
2528 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
2529
2530 mutex_unlock(&mdev->md_io_mutex);
2531 put_ldev(mdev);
2532}
2533
2534/**
2535 * drbd_md_read() - Reads in the meta data super block
2536 * @mdev: DRBD device.
2537 * @bdev: Device from which the meta data should be read in.
2538 *
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01002539 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
2541 */
2542int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
2543{
2544 struct meta_data_on_disk *buffer;
2545 int i, rv = NO_ERROR;
2546
2547 if (!get_ldev_if_state(mdev, D_ATTACHING))
2548 return ERR_IO_MD_DISK;
2549
Philipp Reisnerb411b362009-09-25 16:07:19 -07002550 mutex_lock(&mdev->md_io_mutex);
2551 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2552
2553 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002554 /* NOTE: can't do normal error processing here as this is
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555 called BEFORE disk is attached */
2556 dev_err(DEV, "Error while reading metadata.\n");
2557 rv = ERR_IO_MD_DISK;
2558 goto err;
2559 }
2560
Andreas Gruenbachere7fad8a2011-01-11 13:54:02 +01002561 if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002562 dev_err(DEV, "Error while reading metadata, magic not found.\n");
2563 rv = ERR_MD_INVALID;
2564 goto err;
2565 }
2566 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
2567 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
2568 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
2569 rv = ERR_MD_INVALID;
2570 goto err;
2571 }
2572 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
2573 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
2574 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
2575 rv = ERR_MD_INVALID;
2576 goto err;
2577 }
2578 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
2579 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
2580 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
2581 rv = ERR_MD_INVALID;
2582 goto err;
2583 }
2584
2585 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
2586 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
2587 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
2588 rv = ERR_MD_INVALID;
2589 goto err;
2590 }
2591
2592 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
2593 for (i = UI_CURRENT; i < UI_SIZE; i++)
2594 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
2595 bdev->md.flags = be32_to_cpu(buffer->flags);
2596 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
2597 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
2598
Philipp Reisner87eeee42011-01-19 14:16:30 +01002599 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002600 if (mdev->state.conn < C_CONNECTED) {
2601 int peer;
2602 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
2603 peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
2604 mdev->peer_max_bio_size = peer;
2605 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002606 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002607
Philipp Reisnerb411b362009-09-25 16:07:19 -07002608 if (mdev->sync_conf.al_extents < 7)
2609 mdev->sync_conf.al_extents = 127;
2610
2611 err:
2612 mutex_unlock(&mdev->md_io_mutex);
2613 put_ldev(mdev);
2614
2615 return rv;
2616}
2617
2618/**
2619 * drbd_md_mark_dirty() - Mark meta data super block as dirty
2620 * @mdev: DRBD device.
2621 *
2622 * Call this function if you change anything that should be written to
2623 * the meta-data super block. This function sets MD_DIRTY, and starts a
2624 * timer that ensures that within five seconds you have to call drbd_md_sync().
2625 */
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002626#ifdef DEBUG
Lars Ellenbergee15b032010-09-03 10:00:09 +02002627void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
2628{
2629 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
2630 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
2631 mdev->last_md_mark_dirty.line = line;
2632 mdev->last_md_mark_dirty.func = func;
2633 }
2634}
2635#else
Philipp Reisnerb411b362009-09-25 16:07:19 -07002636void drbd_md_mark_dirty(struct drbd_conf *mdev)
2637{
Lars Ellenbergee15b032010-09-03 10:00:09 +02002638 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002639 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640}
Lars Ellenbergee15b032010-09-03 10:00:09 +02002641#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002642
2643static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
2644{
2645 int i;
2646
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002647 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002648 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002649}
2650
2651void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2652{
2653 if (idx == UI_CURRENT) {
2654 if (mdev->state.role == R_PRIMARY)
2655 val |= 1;
2656 else
2657 val &= ~((u64)1);
2658
2659 drbd_set_ed_uuid(mdev, val);
2660 }
2661
2662 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002663 drbd_md_mark_dirty(mdev);
2664}
2665
2666
2667void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2668{
2669 if (mdev->ldev->md.uuid[idx]) {
2670 drbd_uuid_move_history(mdev);
2671 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002672 }
2673 _drbd_uuid_set(mdev, idx, val);
2674}
2675
2676/**
2677 * drbd_uuid_new_current() - Creates a new current UUID
2678 * @mdev: DRBD device.
2679 *
2680 * Creates a new current UUID, and rotates the old current UUID into
2681 * the bitmap slot. Causes an incremental resync upon next connect.
2682 */
2683void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
2684{
2685 u64 val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002686 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002687
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002688 if (bm_uuid)
2689 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
2690
Philipp Reisnerb411b362009-09-25 16:07:19 -07002691 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002692
2693 get_random_bytes(&val, sizeof(u64));
2694 _drbd_uuid_set(mdev, UI_CURRENT, val);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002695 drbd_print_uuids(mdev, "new current UUID");
Lars Ellenbergaaa8e2b2010-10-15 13:16:53 +02002696 /* get it to stable storage _now_ */
2697 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002698}
2699
2700void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
2701{
2702 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
2703 return;
2704
2705 if (val == 0) {
2706 drbd_uuid_move_history(mdev);
2707 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2708 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002709 } else {
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002710 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
2711 if (bm_uuid)
2712 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002714 mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002715 }
2716 drbd_md_mark_dirty(mdev);
2717}
2718
2719/**
2720 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2721 * @mdev: DRBD device.
2722 *
2723 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
2724 */
2725int drbd_bmio_set_n_write(struct drbd_conf *mdev)
2726{
2727 int rv = -EIO;
2728
2729 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2730 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
2731 drbd_md_sync(mdev);
2732 drbd_bm_set_all(mdev);
2733
2734 rv = drbd_bm_write(mdev);
2735
2736 if (!rv) {
2737 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2738 drbd_md_sync(mdev);
2739 }
2740
2741 put_ldev(mdev);
2742 }
2743
2744 return rv;
2745}
2746
2747/**
2748 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2749 * @mdev: DRBD device.
2750 *
2751 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
2752 */
2753int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
2754{
2755 int rv = -EIO;
2756
Philipp Reisner07782862010-08-31 12:00:50 +02002757 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002758 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2759 drbd_bm_clear_all(mdev);
2760 rv = drbd_bm_write(mdev);
2761 put_ldev(mdev);
2762 }
2763
2764 return rv;
2765}
2766
2767static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2768{
2769 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002770 int rv = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002771
2772 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
2773
Lars Ellenberg02851e92010-12-16 14:47:39 +01002774 if (get_ldev(mdev)) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002775 drbd_bm_lock(mdev, work->why, work->flags);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002776 rv = work->io_fn(mdev);
2777 drbd_bm_unlock(mdev);
2778 put_ldev(mdev);
2779 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002780
2781 clear_bit(BITMAP_IO, &mdev->flags);
Philipp Reisner127b3172010-11-16 10:07:53 +01002782 smp_mb__after_clear_bit();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002783 wake_up(&mdev->misc_wait);
2784
2785 if (work->done)
2786 work->done(mdev, rv);
2787
2788 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
2789 work->why = NULL;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002790 work->flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002791
2792 return 1;
2793}
2794
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002795void drbd_ldev_destroy(struct drbd_conf *mdev)
2796{
2797 lc_destroy(mdev->resync);
2798 mdev->resync = NULL;
2799 lc_destroy(mdev->act_log);
2800 mdev->act_log = NULL;
2801 __no_warn(local,
2802 drbd_free_bc(mdev->ldev);
2803 mdev->ldev = NULL;);
2804
2805 if (mdev->md_io_tmpp) {
2806 __free_page(mdev->md_io_tmpp);
2807 mdev->md_io_tmpp = NULL;
2808 }
2809 clear_bit(GO_DISKLESS, &mdev->flags);
2810}
2811
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002812static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2813{
2814 D_ASSERT(mdev->state.disk == D_FAILED);
Lars Ellenberg9d282872010-10-14 13:57:07 +02002815 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
2816 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002817 * the protected members anymore, though, so once put_ldev reaches zero
2818 * again, it will be safe to free them. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002819 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002820 return 1;
2821}
2822
2823void drbd_go_diskless(struct drbd_conf *mdev)
2824{
2825 D_ASSERT(mdev->state.disk == D_FAILED);
2826 if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002827 drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002828}
2829
Philipp Reisnerb411b362009-09-25 16:07:19 -07002830/**
2831 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
2832 * @mdev: DRBD device.
2833 * @io_fn: IO callback to be called when bitmap IO is possible
2834 * @done: callback to be called after the bitmap IO was performed
2835 * @why: Descriptive text of the reason for doing the IO
2836 *
2837 * While IO on the bitmap happens we freeze application IO thus we ensure
2838 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
2839 * called from worker context. It MUST NOT be used while a previous such
2840 * work is still pending!
2841 */
2842void drbd_queue_bitmap_io(struct drbd_conf *mdev,
2843 int (*io_fn)(struct drbd_conf *),
2844 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002845 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002846{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002847 D_ASSERT(current == mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002848
2849 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
2850 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
2851 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
2852 if (mdev->bm_io_work.why)
2853 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
2854 why, mdev->bm_io_work.why);
2855
2856 mdev->bm_io_work.io_fn = io_fn;
2857 mdev->bm_io_work.done = done;
2858 mdev->bm_io_work.why = why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002859 mdev->bm_io_work.flags = flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002860
Philipp Reisner87eeee42011-01-19 14:16:30 +01002861 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002862 set_bit(BITMAP_IO, &mdev->flags);
2863 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
Philipp Reisner127b3172010-11-16 10:07:53 +01002864 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002865 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002866 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002867 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868}
2869
2870/**
2871 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
2872 * @mdev: DRBD device.
2873 * @io_fn: IO callback to be called when bitmap IO is possible
2874 * @why: Descriptive text of the reason for doing the IO
2875 *
2876 * freezes application IO while that the actual IO operations runs. This
2877 * functions MAY NOT be called from worker context.
2878 */
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002879int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
2880 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002881{
2882 int rv;
2883
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002884 D_ASSERT(current != mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002885
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002886 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2887 drbd_suspend_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002888
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002889 drbd_bm_lock(mdev, why, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002890 rv = io_fn(mdev);
2891 drbd_bm_unlock(mdev);
2892
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002893 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2894 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002895
2896 return rv;
2897}
2898
2899void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2900{
2901 if ((mdev->ldev->md.flags & flag) != flag) {
2902 drbd_md_mark_dirty(mdev);
2903 mdev->ldev->md.flags |= flag;
2904 }
2905}
2906
2907void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2908{
2909 if ((mdev->ldev->md.flags & flag) != 0) {
2910 drbd_md_mark_dirty(mdev);
2911 mdev->ldev->md.flags &= ~flag;
2912 }
2913}
2914int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
2915{
2916 return (bdev->md.flags & flag) != 0;
2917}
2918
2919static void md_sync_timer_fn(unsigned long data)
2920{
2921 struct drbd_conf *mdev = (struct drbd_conf *) data;
2922
Philipp Reisnere42325a2011-01-19 13:55:45 +01002923 drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002924}
2925
2926static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2927{
2928 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02002929#ifdef DEBUG
2930 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
2931 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
2932#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002933 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002934 return 1;
2935}
2936
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002937const char *cmdname(enum drbd_packet cmd)
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01002938{
2939 /* THINK may need to become several global tables
2940 * when we want to support more than
2941 * one PRO_VERSION */
2942 static const char *cmdnames[] = {
2943 [P_DATA] = "Data",
2944 [P_DATA_REPLY] = "DataReply",
2945 [P_RS_DATA_REPLY] = "RSDataReply",
2946 [P_BARRIER] = "Barrier",
2947 [P_BITMAP] = "ReportBitMap",
2948 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
2949 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
2950 [P_UNPLUG_REMOTE] = "UnplugRemote",
2951 [P_DATA_REQUEST] = "DataRequest",
2952 [P_RS_DATA_REQUEST] = "RSDataRequest",
2953 [P_SYNC_PARAM] = "SyncParam",
2954 [P_SYNC_PARAM89] = "SyncParam89",
2955 [P_PROTOCOL] = "ReportProtocol",
2956 [P_UUIDS] = "ReportUUIDs",
2957 [P_SIZES] = "ReportSizes",
2958 [P_STATE] = "ReportState",
2959 [P_SYNC_UUID] = "ReportSyncUUID",
2960 [P_AUTH_CHALLENGE] = "AuthChallenge",
2961 [P_AUTH_RESPONSE] = "AuthResponse",
2962 [P_PING] = "Ping",
2963 [P_PING_ACK] = "PingAck",
2964 [P_RECV_ACK] = "RecvAck",
2965 [P_WRITE_ACK] = "WriteAck",
2966 [P_RS_WRITE_ACK] = "RSWriteAck",
2967 [P_DISCARD_ACK] = "DiscardAck",
2968 [P_NEG_ACK] = "NegAck",
2969 [P_NEG_DREPLY] = "NegDReply",
2970 [P_NEG_RS_DREPLY] = "NegRSDReply",
2971 [P_BARRIER_ACK] = "BarrierAck",
2972 [P_STATE_CHG_REQ] = "StateChgRequest",
2973 [P_STATE_CHG_REPLY] = "StateChgReply",
2974 [P_OV_REQUEST] = "OVRequest",
2975 [P_OV_REPLY] = "OVReply",
2976 [P_OV_RESULT] = "OVResult",
2977 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
2978 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
2979 [P_COMPRESSED_BITMAP] = "CBitmap",
2980 [P_DELAY_PROBE] = "DelayProbe",
2981 [P_OUT_OF_SYNC] = "OutOfSync",
2982 [P_MAX_CMD] = NULL,
2983 };
2984
2985 if (cmd == P_HAND_SHAKE_M)
2986 return "HandShakeM";
2987 if (cmd == P_HAND_SHAKE_S)
2988 return "HandShakeS";
2989 if (cmd == P_HAND_SHAKE)
2990 return "HandShake";
2991 if (cmd >= P_MAX_CMD)
2992 return "Unknown";
2993 return cmdnames[cmd];
2994}
2995
Philipp Reisnerb411b362009-09-25 16:07:19 -07002996#ifdef CONFIG_DRBD_FAULT_INJECTION
2997/* Fault insertion support including random number generator shamelessly
2998 * stolen from kernel/rcutorture.c */
2999struct fault_random_state {
3000 unsigned long state;
3001 unsigned long count;
3002};
3003
3004#define FAULT_RANDOM_MULT 39916801 /* prime */
3005#define FAULT_RANDOM_ADD 479001701 /* prime */
3006#define FAULT_RANDOM_REFRESH 10000
3007
3008/*
3009 * Crude but fast random-number generator. Uses a linear congruential
3010 * generator, with occasional help from get_random_bytes().
3011 */
3012static unsigned long
3013_drbd_fault_random(struct fault_random_state *rsp)
3014{
3015 long refresh;
3016
Roel Kluin49829ea2009-12-15 22:55:44 +01003017 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018 get_random_bytes(&refresh, sizeof(refresh));
3019 rsp->state += refresh;
3020 rsp->count = FAULT_RANDOM_REFRESH;
3021 }
3022 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3023 return swahw32(rsp->state);
3024}
3025
3026static char *
3027_drbd_fault_str(unsigned int type) {
3028 static char *_faults[] = {
3029 [DRBD_FAULT_MD_WR] = "Meta-data write",
3030 [DRBD_FAULT_MD_RD] = "Meta-data read",
3031 [DRBD_FAULT_RS_WR] = "Resync write",
3032 [DRBD_FAULT_RS_RD] = "Resync read",
3033 [DRBD_FAULT_DT_WR] = "Data write",
3034 [DRBD_FAULT_DT_RD] = "Data read",
3035 [DRBD_FAULT_DT_RA] = "Data read ahead",
3036 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003037 [DRBD_FAULT_AL_EE] = "EE allocation",
3038 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003039 };
3040
3041 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3042}
3043
3044unsigned int
3045_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3046{
3047 static struct fault_random_state rrs = {0, 0};
3048
3049 unsigned int ret = (
3050 (fault_devs == 0 ||
3051 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3052 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3053
3054 if (ret) {
3055 fault_count++;
3056
Lars Ellenberg73835062010-05-27 11:51:56 +02003057 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003058 dev_warn(DEV, "***Simulating %s failure\n",
3059 _drbd_fault_str(type));
3060 }
3061
3062 return ret;
3063}
3064#endif
3065
3066const char *drbd_buildtag(void)
3067{
3068 /* DRBD built from external sources has here a reference to the
3069 git hash of the source code. */
3070
3071 static char buildtag[38] = "\0uilt-in";
3072
3073 if (buildtag[0] == 0) {
3074#ifdef CONFIG_MODULES
3075 if (THIS_MODULE != NULL)
3076 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3077 else
3078#endif
3079 buildtag[0] = 'b';
3080 }
3081
3082 return buildtag;
3083}
3084
3085module_init(drbd_init)
3086module_exit(drbd_cleanup)
3087
Philipp Reisnerb411b362009-09-25 16:07:19 -07003088EXPORT_SYMBOL(drbd_conn_str);
3089EXPORT_SYMBOL(drbd_role_str);
3090EXPORT_SYMBOL(drbd_disk_str);
3091EXPORT_SYMBOL(drbd_set_st_err_str);