blob: 0861746a747532dd29f5aac98cac88c859b8728f [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
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100449 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
450 thi->name[0], thi->mdev->tconn->name);
451
Philipp Reisnerb411b362009-09-25 16:07:19 -0700452restart:
453 retval = thi->function(thi);
454
455 spin_lock_irqsave(&thi->t_lock, flags);
456
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100457 /* if the receiver has been "EXITING", the last thing it did
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458 * was set the conn state to "StandAlone",
459 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
460 * and receiver thread will be "started".
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100461 * drbd_thread_start needs to set "RESTARTING" in that case.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462 * t_state check and assignment needs to be within the same spinlock,
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100463 * so either thread_start sees EXITING, and can remap to RESTARTING,
464 * or thread_start see NONE, and can proceed as normal.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465 */
466
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100467 if (thi->t_state == RESTARTING) {
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100468 dev_info(DEV, "Restarting %s thread\n", thi->name);
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100469 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700470 spin_unlock_irqrestore(&thi->t_lock, flags);
471 goto restart;
472 }
473
474 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100475 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700476 smp_mb();
477 complete(&thi->stop);
478 spin_unlock_irqrestore(&thi->t_lock, flags);
479
480 dev_info(DEV, "Terminating %s\n", current->comm);
481
482 /* Release mod reference taken when thread was started */
483 module_put(THIS_MODULE);
484 return retval;
485}
486
487static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100488 int (*func) (struct drbd_thread *), char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489{
490 spin_lock_init(&thi->t_lock);
491 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100492 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493 thi->function = func;
494 thi->mdev = mdev;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100495 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496}
497
498int drbd_thread_start(struct drbd_thread *thi)
499{
500 struct drbd_conf *mdev = thi->mdev;
501 struct task_struct *nt;
502 unsigned long flags;
503
Philipp Reisnerb411b362009-09-25 16:07:19 -0700504 /* is used from state engine doing drbd_thread_stop_nowait,
505 * while holding the req lock irqsave */
506 spin_lock_irqsave(&thi->t_lock, flags);
507
508 switch (thi->t_state) {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100509 case NONE:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510 dev_info(DEV, "Starting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100511 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700512
513 /* Get ref on module for thread - this is released when thread exits */
514 if (!try_module_get(THIS_MODULE)) {
515 dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
516 spin_unlock_irqrestore(&thi->t_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100517 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518 }
519
520 init_completion(&thi->stop);
521 D_ASSERT(thi->task == NULL);
522 thi->reset_cpu_mask = 1;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100523 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700524 spin_unlock_irqrestore(&thi->t_lock, flags);
525 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
526
527 nt = kthread_create(drbd_thread_setup, (void *) thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100528 "drbd%d_%s", mdev_to_minor(mdev), thi->name);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529
530 if (IS_ERR(nt)) {
531 dev_err(DEV, "Couldn't start thread\n");
532
533 module_put(THIS_MODULE);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100534 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700535 }
536 spin_lock_irqsave(&thi->t_lock, flags);
537 thi->task = nt;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100538 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700539 spin_unlock_irqrestore(&thi->t_lock, flags);
540 wake_up_process(nt);
541 break;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100542 case EXITING:
543 thi->t_state = RESTARTING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544 dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100545 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546 /* fall through */
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100547 case RUNNING:
548 case RESTARTING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 default:
550 spin_unlock_irqrestore(&thi->t_lock, flags);
551 break;
552 }
553
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100554 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700555}
556
557
558void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
559{
560 unsigned long flags;
561
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100562 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563
564 /* may be called from state engine, holding the req lock irqsave */
565 spin_lock_irqsave(&thi->t_lock, flags);
566
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100567 if (thi->t_state == NONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700568 spin_unlock_irqrestore(&thi->t_lock, flags);
569 if (restart)
570 drbd_thread_start(thi);
571 return;
572 }
573
574 if (thi->t_state != ns) {
575 if (thi->task == NULL) {
576 spin_unlock_irqrestore(&thi->t_lock, flags);
577 return;
578 }
579
580 thi->t_state = ns;
581 smp_mb();
582 init_completion(&thi->stop);
583 if (thi->task != current)
584 force_sig(DRBD_SIGKILL, thi->task);
585
586 }
587
588 spin_unlock_irqrestore(&thi->t_lock, flags);
589
590 if (wait)
591 wait_for_completion(&thi->stop);
592}
593
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100594static struct drbd_thread *drbd_task_to_thread(struct drbd_conf *mdev, struct task_struct *task)
595{
596 struct drbd_tconn *tconn = mdev->tconn;
597 struct drbd_thread *thi =
598 task == tconn->receiver.task ? &tconn->receiver :
599 task == tconn->asender.task ? &tconn->asender :
600 task == tconn->worker.task ? &tconn->worker : NULL;
601
602 return thi;
603}
604
605char *drbd_task_to_thread_name(struct drbd_conf *mdev, struct task_struct *task)
606{
607 struct drbd_thread *thi = drbd_task_to_thread(mdev, task);
608 return thi ? thi->name : task->comm;
609}
610
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611#ifdef CONFIG_SMP
Philipp Reisner80822282011-02-08 12:46:30 +0100612static int conn_lowest_minor(struct drbd_tconn *tconn)
613{
614 int minor = 0;
615 idr_get_next(&tconn->volumes, &minor);
616 return minor;
617}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618/**
619 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
620 * @mdev: DRBD device.
621 *
622 * Forces all threads of a device onto the same CPU. This is beneficial for
623 * DRBD's performance. May be overwritten by user's configuration.
624 */
Philipp Reisner80822282011-02-08 12:46:30 +0100625void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626{
627 int ord, cpu;
628
629 /* user override. */
Philipp Reisner80822282011-02-08 12:46:30 +0100630 if (cpumask_weight(tconn->cpu_mask))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 return;
632
Philipp Reisner80822282011-02-08 12:46:30 +0100633 ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634 for_each_online_cpu(cpu) {
635 if (ord-- == 0) {
Philipp Reisner80822282011-02-08 12:46:30 +0100636 cpumask_set_cpu(cpu, tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637 return;
638 }
639 }
640 /* should not be reached */
Philipp Reisner80822282011-02-08 12:46:30 +0100641 cpumask_setall(tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700642}
643
644/**
645 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
646 * @mdev: DRBD device.
Philipp Reisnerbc31fe32011-02-07 11:14:38 +0100647 * @thi: drbd_thread object
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648 *
649 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
650 * prematurely.
651 */
Philipp Reisner80822282011-02-08 12:46:30 +0100652void drbd_thread_current_set_cpu(struct drbd_thread *thi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700653{
654 struct task_struct *p = current;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100655
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656 if (!thi->reset_cpu_mask)
657 return;
658 thi->reset_cpu_mask = 0;
Philipp Reisner80822282011-02-08 12:46:30 +0100659 set_cpus_allowed_ptr(p, thi->mdev->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660}
661#endif
662
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100663static void prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100664{
665 h->magic = cpu_to_be32(DRBD_MAGIC);
666 h->command = cpu_to_be16(cmd);
667 h->length = cpu_to_be16(size);
668}
669
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100670static void prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100671{
672 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
673 h->command = cpu_to_be16(cmd);
674 h->length = cpu_to_be32(size);
675}
676
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100677static void _prepare_header(struct drbd_tconn *tconn, int vnr, struct p_header *h,
678 enum drbd_packet cmd, int size)
679{
680 if (tconn->agreed_pro_version >= 100 || size > DRBD_MAX_SIZE_H80_PACKET)
681 prepare_header95(&h->h95, cmd, size);
682 else
683 prepare_header80(&h->h80, cmd, size);
684}
685
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100686static void prepare_header(struct drbd_conf *mdev, struct p_header *h,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100687 enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100688{
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100689 _prepare_header(mdev->tconn, mdev->vnr, h, cmd, size);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100690}
691
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692/* the appropriate socket mutex must be held already */
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100693int _conn_send_cmd(struct drbd_tconn *tconn, int vnr, struct socket *sock,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100694 enum drbd_packet cmd, struct p_header *h, size_t size,
695 unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696{
697 int sent, ok;
698
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100699 _prepare_header(tconn, vnr, h, cmd, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100701 sent = drbd_send(tconn, sock, h, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700702
703 ok = (sent == size);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +0100704 if (!ok && !signal_pending(current))
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100705 conn_warn(tconn, "short sent %s size=%d sent=%d\n",
706 cmdname(cmd), (int)size, sent);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 return ok;
708}
709
710/* don't pass the socket. we may only look at it
711 * when we hold the appropriate socket mutex.
712 */
713int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100714 enum drbd_packet cmd, struct p_header *h, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715{
716 int ok = 0;
717 struct socket *sock;
718
719 if (use_data_socket) {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100720 mutex_lock(&mdev->tconn->data.mutex);
721 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700722 } else {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100723 mutex_lock(&mdev->tconn->meta.mutex);
724 sock = mdev->tconn->meta.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 }
726
727 /* drbd_disconnect() could have called drbd_free_sock()
728 * while we were waiting in down()... */
729 if (likely(sock != NULL))
730 ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
731
732 if (use_data_socket)
Philipp Reisnere42325a2011-01-19 13:55:45 +0100733 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700734 else
Philipp Reisnere42325a2011-01-19 13:55:45 +0100735 mutex_unlock(&mdev->tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700736 return ok;
737}
738
Philipp Reisner61120872011-02-08 09:50:54 +0100739int conn_send_cmd2(struct drbd_tconn *tconn, enum drbd_packet cmd, char *data,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 size_t size)
741{
Philipp Reisner61120872011-02-08 09:50:54 +0100742 struct p_header80 h;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743 int ok;
744
Philipp Reisner61120872011-02-08 09:50:54 +0100745 prepare_header80(&h, cmd, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746
Philipp Reisner61120872011-02-08 09:50:54 +0100747 if (!drbd_get_data_sock(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700748 return 0;
749
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750 ok = (sizeof(h) ==
Philipp Reisner61120872011-02-08 09:50:54 +0100751 drbd_send(tconn, tconn->data.socket, &h, sizeof(h), 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700752 ok = ok && (size ==
Philipp Reisner61120872011-02-08 09:50:54 +0100753 drbd_send(tconn, tconn->data.socket, data, size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754
Philipp Reisner61120872011-02-08 09:50:54 +0100755 drbd_put_data_sock(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756
757 return ok;
758}
759
760int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
761{
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200762 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763 struct socket *sock;
764 int size, rv;
Philipp Reisner31890f42011-01-19 14:12:51 +0100765 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700766
767 size = apv <= 87 ? sizeof(struct p_rs_param)
768 : apv == 88 ? sizeof(struct p_rs_param)
769 + strlen(mdev->sync_conf.verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200770 : apv <= 94 ? sizeof(struct p_rs_param_89)
771 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700772
773 /* used from admin command context and receiver/worker context.
774 * to avoid kmalloc, grab the socket right here,
775 * then use the pre-allocated sbuf there */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100776 mutex_lock(&mdev->tconn->data.mutex);
777 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778
779 if (likely(sock != NULL)) {
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100780 enum drbd_packet cmd =
781 apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782
Philipp Reisnere42325a2011-01-19 13:55:45 +0100783 p = &mdev->tconn->data.sbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784
785 /* initialize verify_alg and csums_alg */
786 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
787
788 p->rate = cpu_to_be32(sc->rate);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200789 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
790 p->c_delay_target = cpu_to_be32(sc->c_delay_target);
791 p->c_fill_target = cpu_to_be32(sc->c_fill_target);
792 p->c_max_rate = cpu_to_be32(sc->c_max_rate);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700793
794 if (apv >= 88)
795 strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
796 if (apv >= 89)
797 strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
798
799 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
800 } else
801 rv = 0; /* not ok */
802
Philipp Reisnere42325a2011-01-19 13:55:45 +0100803 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804
805 return rv;
806}
807
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100808int drbd_send_protocol(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809{
810 struct p_protocol *p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100811 int size, cf, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700812
813 size = sizeof(struct p_protocol);
814
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100815 if (tconn->agreed_pro_version >= 87)
816 size += strlen(tconn->net_conf->integrity_alg) + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817
818 /* we must not recurse into our own queue,
819 * as that is blocked during handshake */
820 p = kmalloc(size, GFP_NOIO);
821 if (p == NULL)
822 return 0;
823
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100824 p->protocol = cpu_to_be32(tconn->net_conf->wire_protocol);
825 p->after_sb_0p = cpu_to_be32(tconn->net_conf->after_sb_0p);
826 p->after_sb_1p = cpu_to_be32(tconn->net_conf->after_sb_1p);
827 p->after_sb_2p = cpu_to_be32(tconn->net_conf->after_sb_2p);
828 p->two_primaries = cpu_to_be32(tconn->net_conf->two_primaries);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100830 cf = 0;
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100831 if (tconn->net_conf->want_lose)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100832 cf |= CF_WANT_LOSE;
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100833 if (tconn->net_conf->dry_run) {
834 if (tconn->agreed_pro_version >= 92)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100835 cf |= CF_DRY_RUN;
836 else {
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100837 conn_err(tconn, "--dry-run is not supported by peer");
Dan Carpenter7ac314c2010-04-22 14:27:23 +0200838 kfree(p);
Philipp Reisner148efa12011-01-15 00:21:15 +0100839 return -1;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100840 }
841 }
842 p->conn_flags = cpu_to_be32(cf);
843
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100844 if (tconn->agreed_pro_version >= 87)
845 strcpy(p->integrity_alg, tconn->net_conf->integrity_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700846
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100847 rv = conn_send_cmd2(tconn, P_PROTOCOL, p->head.payload, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848 kfree(p);
849 return rv;
850}
851
852int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
853{
854 struct p_uuids p;
855 int i;
856
857 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
858 return 1;
859
860 for (i = UI_CURRENT; i < UI_SIZE; i++)
861 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
862
863 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
864 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
Philipp Reisner89e58e72011-01-19 13:12:45 +0100865 uuid_flags |= mdev->tconn->net_conf->want_lose ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700866 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
867 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
868 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
869
870 put_ldev(mdev);
871
Philipp Reisnerc0129492011-01-19 16:58:16 +0100872 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700873}
874
875int drbd_send_uuids(struct drbd_conf *mdev)
876{
877 return _drbd_send_uuids(mdev, 0);
878}
879
880int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
881{
882 return _drbd_send_uuids(mdev, 8);
883}
884
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100885void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
886{
887 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
888 u64 *uuid = mdev->ldev->md.uuid;
889 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
890 text,
891 (unsigned long long)uuid[UI_CURRENT],
892 (unsigned long long)uuid[UI_BITMAP],
893 (unsigned long long)uuid[UI_HISTORY_START],
894 (unsigned long long)uuid[UI_HISTORY_END]);
895 put_ldev(mdev);
896 } else {
897 dev_info(DEV, "%s effective data uuid: %016llX\n",
898 text,
899 (unsigned long long)mdev->ed_uuid);
900 }
901}
902
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100903int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904{
905 struct p_rs_uuid p;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100906 u64 uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100908 D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
909
Philipp Reisner4a23f262011-01-11 17:42:17 +0100910 uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100911 drbd_uuid_set(mdev, UI_BITMAP, uuid);
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100912 drbd_print_uuids(mdev, "updated sync UUID");
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100913 drbd_md_sync(mdev);
914 p.uuid = cpu_to_be64(uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700915
Philipp Reisnerc0129492011-01-19 16:58:16 +0100916 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700917}
918
Philipp Reisnere89b5912010-03-24 17:11:33 +0100919int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700920{
921 struct p_sizes p;
922 sector_t d_size, u_size;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200923 int q_order_type, max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924 int ok;
925
926 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
927 D_ASSERT(mdev->ldev->backing_bdev);
928 d_size = drbd_get_max_capacity(mdev->ldev);
929 u_size = mdev->ldev->dc.disk_size;
930 q_order_type = drbd_queue_order_type(mdev);
Philipp Reisner99432fc2011-05-20 16:39:13 +0200931 max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
932 max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 put_ldev(mdev);
934 } else {
935 d_size = 0;
936 u_size = 0;
937 q_order_type = QUEUE_ORDERED_NONE;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200938 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 }
940
941 p.d_size = cpu_to_be64(d_size);
942 p.u_size = cpu_to_be64(u_size);
943 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
Philipp Reisner99432fc2011-05-20 16:39:13 +0200944 p.max_bio_size = cpu_to_be32(max_bio_size);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100945 p.queue_order_type = cpu_to_be16(q_order_type);
946 p.dds_flags = cpu_to_be16(flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947
Philipp Reisnerc0129492011-01-19 16:58:16 +0100948 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 return ok;
950}
951
952/**
953 * drbd_send_state() - Sends the drbd state to the peer
954 * @mdev: DRBD device.
955 */
956int drbd_send_state(struct drbd_conf *mdev)
957{
958 struct socket *sock;
959 struct p_state p;
960 int ok = 0;
961
Philipp Reisnere42325a2011-01-19 13:55:45 +0100962 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
964 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100965 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
967 if (likely(sock != NULL)) {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100968 ok = _drbd_send_cmd(mdev, sock, P_STATE, &p.head, sizeof(p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969 }
970
Philipp Reisnere42325a2011-01-19 13:55:45 +0100971 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700972
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 return ok;
974}
975
976int drbd_send_state_req(struct drbd_conf *mdev,
977 union drbd_state mask, union drbd_state val)
978{
979 struct p_req_state p;
980
981 p.mask = cpu_to_be32(mask.i);
982 p.val = cpu_to_be32(val.i);
983
Philipp Reisnerc0129492011-01-19 16:58:16 +0100984 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985}
986
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100987int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700988{
989 struct p_req_state_reply p;
990
991 p.retcode = cpu_to_be32(retcode);
992
Philipp Reisnerc0129492011-01-19 16:58:16 +0100993 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994}
995
996int fill_bitmap_rle_bits(struct drbd_conf *mdev,
997 struct p_compressed_bm *p,
998 struct bm_xfer_ctx *c)
999{
1000 struct bitstream bs;
1001 unsigned long plain_bits;
1002 unsigned long tmp;
1003 unsigned long rl;
1004 unsigned len;
1005 unsigned toggle;
1006 int bits;
1007
1008 /* may we use this feature? */
1009 if ((mdev->sync_conf.use_rle == 0) ||
Philipp Reisner31890f42011-01-19 14:12:51 +01001010 (mdev->tconn->agreed_pro_version < 90))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011 return 0;
1012
1013 if (c->bit_offset >= c->bm_bits)
1014 return 0; /* nothing to do. */
1015
1016 /* use at most thus many bytes */
1017 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1018 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1019 /* plain bits covered in this code string */
1020 plain_bits = 0;
1021
1022 /* p->encoding & 0x80 stores whether the first run length is set.
1023 * bit offset is implicit.
1024 * start with toggle == 2 to be able to tell the first iteration */
1025 toggle = 2;
1026
1027 /* see how much plain bits we can stuff into one packet
1028 * using RLE and VLI. */
1029 do {
1030 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1031 : _drbd_bm_find_next(mdev, c->bit_offset);
1032 if (tmp == -1UL)
1033 tmp = c->bm_bits;
1034 rl = tmp - c->bit_offset;
1035
1036 if (toggle == 2) { /* first iteration */
1037 if (rl == 0) {
1038 /* the first checked bit was set,
1039 * store start value, */
1040 DCBP_set_start(p, 1);
1041 /* but skip encoding of zero run length */
1042 toggle = !toggle;
1043 continue;
1044 }
1045 DCBP_set_start(p, 0);
1046 }
1047
1048 /* paranoia: catch zero runlength.
1049 * can only happen if bitmap is modified while we scan it. */
1050 if (rl == 0) {
1051 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1052 "t:%u bo:%lu\n", toggle, c->bit_offset);
1053 return -1;
1054 }
1055
1056 bits = vli_encode_bits(&bs, rl);
1057 if (bits == -ENOBUFS) /* buffer full */
1058 break;
1059 if (bits <= 0) {
1060 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1061 return 0;
1062 }
1063
1064 toggle = !toggle;
1065 plain_bits += rl;
1066 c->bit_offset = tmp;
1067 } while (c->bit_offset < c->bm_bits);
1068
1069 len = bs.cur.b - p->code + !!bs.cur.bit;
1070
1071 if (plain_bits < (len << 3)) {
1072 /* incompressible with this method.
1073 * we need to rewind both word and bit position. */
1074 c->bit_offset -= plain_bits;
1075 bm_xfer_ctx_bit_to_word_offset(c);
1076 c->bit_offset = c->word_offset * BITS_PER_LONG;
1077 return 0;
1078 }
1079
1080 /* RLE + VLI was able to compress it just fine.
1081 * update c->word_offset. */
1082 bm_xfer_ctx_bit_to_word_offset(c);
1083
1084 /* store pad_bits */
1085 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
1086
1087 return len;
1088}
1089
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001090/**
1091 * send_bitmap_rle_or_plain
1092 *
1093 * Return 0 when done, 1 when another iteration is needed, and a negative error
1094 * code upon failure.
1095 */
1096static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001098 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099{
1100 struct p_compressed_bm *p = (void*)h;
1101 unsigned long num_words;
1102 int len;
1103 int ok;
1104
1105 len = fill_bitmap_rle_bits(mdev, p, c);
1106
1107 if (len < 0)
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001108 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109
1110 if (len) {
1111 DCBP_set_code(p, RLE_VLI_Bits);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001112 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113 sizeof(*p) + len, 0);
1114
1115 c->packets[0]++;
1116 c->bytes[0] += sizeof(*p) + len;
1117
1118 if (c->bit_offset >= c->bm_bits)
1119 len = 0; /* DONE */
1120 } else {
1121 /* was not compressible.
1122 * send a buffer full of plain text bits instead. */
1123 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
1124 len = num_words * sizeof(long);
1125 if (len)
1126 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001127 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001128 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 c->word_offset += num_words;
1130 c->bit_offset = c->word_offset * BITS_PER_LONG;
1131
1132 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02001133 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001134
1135 if (c->bit_offset > c->bm_bits)
1136 c->bit_offset = c->bm_bits;
1137 }
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001138 if (ok) {
1139 if (len == 0) {
1140 INFO_bm_xfer_stats(mdev, "send", c);
1141 return 0;
1142 } else
1143 return 1;
1144 }
1145 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146}
1147
1148/* See the comment at receive_bitmap() */
1149int _drbd_send_bitmap(struct drbd_conf *mdev)
1150{
1151 struct bm_xfer_ctx c;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001152 struct p_header *p;
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001153 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001155 if (!expect(mdev->bitmap))
1156 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001157
1158 /* maybe we should use some per thread scratch page,
1159 * and allocate that during initial device creation? */
Philipp Reisnerc0129492011-01-19 16:58:16 +01001160 p = (struct p_header *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 if (!p) {
1162 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001163 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164 }
1165
1166 if (get_ldev(mdev)) {
1167 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1168 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
1169 drbd_bm_set_all(mdev);
1170 if (drbd_bm_write(mdev)) {
1171 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1172 * but otherwise process as per normal - need to tell other
1173 * side that a full resync is required! */
1174 dev_err(DEV, "Failed to write bitmap to disk!\n");
1175 } else {
1176 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
1177 drbd_md_sync(mdev);
1178 }
1179 }
1180 put_ldev(mdev);
1181 }
1182
1183 c = (struct bm_xfer_ctx) {
1184 .bm_bits = drbd_bm_bits(mdev),
1185 .bm_words = drbd_bm_words(mdev),
1186 };
1187
1188 do {
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001189 err = send_bitmap_rle_or_plain(mdev, p, &c);
1190 } while (err > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191
1192 free_page((unsigned long) p);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001193 return err == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194}
1195
1196int drbd_send_bitmap(struct drbd_conf *mdev)
1197{
1198 int err;
1199
Philipp Reisner61120872011-02-08 09:50:54 +01001200 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 return -1;
1202 err = !_drbd_send_bitmap(mdev);
Philipp Reisner61120872011-02-08 09:50:54 +01001203 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001204 return err;
1205}
1206
1207int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
1208{
1209 int ok;
1210 struct p_barrier_ack p;
1211
1212 p.barrier = barrier_nr;
1213 p.set_size = cpu_to_be32(set_size);
1214
1215 if (mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001216 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001217 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001218 return ok;
1219}
1220
1221/**
1222 * _drbd_send_ack() - Sends an ack packet
1223 * @mdev: DRBD device.
1224 * @cmd: Packet command code.
1225 * @sector: sector, needs to be in big endian byte order
1226 * @blksize: size in byte, needs to be in big endian byte order
1227 * @block_id: Id, big endian byte order
1228 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001229static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
1230 u64 sector, u32 blksize, u64 block_id)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001231{
1232 int ok;
1233 struct p_block_ack p;
1234
1235 p.sector = sector;
1236 p.block_id = block_id;
1237 p.blksize = blksize;
1238 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
1239
Philipp Reisnere42325a2011-01-19 13:55:45 +01001240 if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001241 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001242 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 return ok;
1244}
1245
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001246/* dp->sector and dp->block_id already/still in network byte order,
1247 * data_size is payload size according to dp->head,
1248 * and may need to be corrected for digest size. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001249int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001250 struct p_data *dp, int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251{
Philipp Reisnera0638452011-01-19 14:31:32 +01001252 data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1253 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001254 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
1255 dp->block_id);
1256}
1257
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001258int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259 struct p_block_req *rp)
1260{
1261 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
1262}
1263
1264/**
1265 * drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001266 * @mdev: DRBD device
1267 * @cmd: packet command code
1268 * @peer_req: peer request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001270int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001271 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001272{
1273 return _drbd_send_ack(mdev, cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001274 cpu_to_be64(peer_req->i.sector),
1275 cpu_to_be32(peer_req->i.size),
1276 peer_req->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277}
1278
1279/* This function misuses the block_id field to signal if the blocks
1280 * are is sync or not. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001281int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001282 sector_t sector, int blksize, u64 block_id)
1283{
1284 return _drbd_send_ack(mdev, cmd,
1285 cpu_to_be64(sector),
1286 cpu_to_be32(blksize),
1287 cpu_to_be64(block_id));
1288}
1289
1290int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1291 sector_t sector, int size, u64 block_id)
1292{
1293 int ok;
1294 struct p_block_req p;
1295
1296 p.sector = cpu_to_be64(sector);
1297 p.block_id = block_id;
1298 p.blksize = cpu_to_be32(size);
1299
Philipp Reisnerc0129492011-01-19 16:58:16 +01001300 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001301 return ok;
1302}
1303
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001304int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
1305 void *digest, int digest_size, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001306{
1307 int ok;
1308 struct p_block_req p;
1309
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001310 prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001312 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313 p.blksize = cpu_to_be32(size);
1314
Philipp Reisnere42325a2011-01-19 13:55:45 +01001315 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001316
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001317 ok = (sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0));
1318 ok = ok && (digest_size == drbd_send(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001319
Philipp Reisnere42325a2011-01-19 13:55:45 +01001320 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001321
1322 return ok;
1323}
1324
1325int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
1326{
1327 int ok;
1328 struct p_block_req p;
1329
1330 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001331 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001332 p.blksize = cpu_to_be32(size);
1333
Philipp Reisnerc0129492011-01-19 16:58:16 +01001334 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001335 return ok;
1336}
1337
1338/* called on sndtimeo
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001339 * returns false if we should retry,
1340 * true if we think connection is dead
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341 */
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001342static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343{
1344 int drop_it;
1345 /* long elapsed = (long)(jiffies - mdev->last_received); */
1346
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001347 drop_it = tconn->meta.socket == sock
1348 || !tconn->asender.task
1349 || get_t_state(&tconn->asender) != RUNNING
1350 || tconn->volume0->state.conn < C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351
1352 if (drop_it)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001353 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001354
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001355 drop_it = !--tconn->ko_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356 if (!drop_it) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001357 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1358 current->comm, current->pid, tconn->ko_count);
1359 request_ping(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001360 }
1361
1362 return drop_it; /* && (mdev->state == R_PRIMARY) */;
1363}
1364
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001365static void drbd_update_congested(struct drbd_tconn *tconn)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001366{
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001367 struct sock *sk = tconn->data.socket->sk;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001368 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001369 set_bit(NET_CONGESTED, &tconn->flags);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001370}
1371
Philipp Reisnerb411b362009-09-25 16:07:19 -07001372/* The idea of sendpage seems to be to put some kind of reference
1373 * to the page into the skb, and to hand it over to the NIC. In
1374 * this process get_page() gets called.
1375 *
1376 * As soon as the page was really sent over the network put_page()
1377 * gets called by some part of the network layer. [ NIC driver? ]
1378 *
1379 * [ get_page() / put_page() increment/decrement the count. If count
1380 * reaches 0 the page will be freed. ]
1381 *
1382 * This works nicely with pages from FSs.
1383 * But this means that in protocol A we might signal IO completion too early!
1384 *
1385 * In order not to corrupt data during a resync we must make sure
1386 * that we do not reuse our own buffer pages (EEs) to early, therefore
1387 * we have the net_ee list.
1388 *
1389 * XFS seems to have problems, still, it submits pages with page_count == 0!
1390 * As a workaround, we disable sendpage on pages
1391 * with page_count == 0 or PageSlab.
1392 */
1393static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001394 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001395{
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001396 int sent = drbd_send(mdev->tconn, mdev->tconn->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 kunmap(page);
1398 if (sent == size)
1399 mdev->send_cnt += size>>9;
1400 return sent == size;
1401}
1402
1403static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001404 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405{
1406 mm_segment_t oldfs = get_fs();
1407 int sent, ok;
1408 int len = size;
1409
1410 /* e.g. XFS meta- & log-data is in slab pages, which have a
1411 * page_count of 0 and/or have PageSlab() set.
1412 * we cannot use send_page for those, as that does get_page();
1413 * put_page(); and would cause either a VM_BUG directly, or
1414 * __page_cache_release a page that would actually still be referenced
1415 * by someone, leading to some obscure delayed Oops somewhere else. */
1416 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001417 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001418
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001419 msg_flags |= MSG_NOSIGNAL;
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001420 drbd_update_congested(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421 set_fs(KERNEL_DS);
1422 do {
Philipp Reisnere42325a2011-01-19 13:55:45 +01001423 sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001425 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426 if (sent == -EAGAIN) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001427 if (we_should_drop_the_connection(mdev->tconn,
Philipp Reisnere42325a2011-01-19 13:55:45 +01001428 mdev->tconn->data.socket))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001429 break;
1430 else
1431 continue;
1432 }
1433 if (sent <= 0) {
1434 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1435 __func__, (int)size, len, sent);
1436 break;
1437 }
1438 len -= sent;
1439 offset += sent;
1440 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
1441 set_fs(oldfs);
Philipp Reisner01a311a2011-02-07 14:30:33 +01001442 clear_bit(NET_CONGESTED, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443
1444 ok = (len == 0);
1445 if (likely(ok))
1446 mdev->send_cnt += size>>9;
1447 return ok;
1448}
1449
1450static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
1451{
1452 struct bio_vec *bvec;
1453 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001454 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455 __bio_for_each_segment(bvec, bio, i, 0) {
1456 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001457 bvec->bv_offset, bvec->bv_len,
1458 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 return 0;
1460 }
1461 return 1;
1462}
1463
1464static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
1465{
1466 struct bio_vec *bvec;
1467 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001468 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001469 __bio_for_each_segment(bvec, bio, i, 0) {
1470 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001471 bvec->bv_offset, bvec->bv_len,
1472 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001473 return 0;
1474 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001475 return 1;
1476}
1477
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001478static int _drbd_send_zc_ee(struct drbd_conf *mdev,
1479 struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001480{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001481 struct page *page = peer_req->pages;
1482 unsigned len = peer_req->i.size;
1483
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001484 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001485 page_chain_for_each(page) {
1486 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001487 if (!_drbd_send_page(mdev, page, 0, l,
1488 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001489 return 0;
1490 len -= l;
1491 }
1492 return 1;
1493}
1494
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001495static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
1496{
Philipp Reisner31890f42011-01-19 14:12:51 +01001497 if (mdev->tconn->agreed_pro_version >= 95)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001498 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001499 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1500 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1501 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1502 else
Jens Axboe721a9602011-03-09 11:56:30 +01001503 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001504}
1505
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506/* Used to send write requests
1507 * R_PRIMARY -> Peer (P_DATA)
1508 */
1509int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
1510{
1511 int ok = 1;
1512 struct p_data p;
1513 unsigned int dp_flags = 0;
1514 void *dgb;
1515 int dgs;
1516
Philipp Reisner61120872011-02-08 09:50:54 +01001517 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518 return 0;
1519
Philipp Reisnera0638452011-01-19 14:31:32 +01001520 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1521 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001523 prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size);
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001524 p.sector = cpu_to_be64(req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525 p.block_id = (unsigned long)req;
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001526 p.seq_num = cpu_to_be32(req->seq_num = atomic_add_return(1, &mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001528 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
1529
Philipp Reisnerb411b362009-09-25 16:07:19 -07001530 if (mdev->state.conn >= C_SYNC_SOURCE &&
1531 mdev->state.conn <= C_PAUSED_SYNC_T)
1532 dp_flags |= DP_MAY_SET_IN_SYNC;
1533
1534 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 set_bit(UNPLUG_REMOTE, &mdev->flags);
1536 ok = (sizeof(p) ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001537 drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001539 dgb = mdev->tconn->int_dig_out;
1540 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001541 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 }
1543 if (ok) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001544 /* For protocol A, we have to memcpy the payload into
1545 * socket buffers, as we may complete right away
1546 * as soon as we handed it over to tcp, at which point the data
1547 * pages may become invalid.
1548 *
1549 * For data-integrity enabled, we copy it as well, so we can be
1550 * sure that even if the bio pages may still be modified, it
1551 * won't change the data on the wire, thus if the digest checks
1552 * out ok after sending on this side, but does not fit on the
1553 * receiving side, we sure have detected corruption elsewhere.
1554 */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001555 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 ok = _drbd_send_bio(mdev, req->master_bio);
1557 else
1558 ok = _drbd_send_zc_bio(mdev, req->master_bio);
Lars Ellenberg470be442010-11-10 10:36:52 +01001559
1560 /* double check digest, sometimes buffers have been modified in flight. */
1561 if (dgs > 0 && dgs <= 64) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001562 /* 64 byte, 512 bit, is the largest digest size
Lars Ellenberg470be442010-11-10 10:36:52 +01001563 * currently supported in kernel crypto. */
1564 unsigned char digest[64];
Philipp Reisnera0638452011-01-19 14:31:32 +01001565 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest);
1566 if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001567 dev_warn(DEV,
1568 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001569 (unsigned long long)req->i.sector, req->i.size);
Lars Ellenberg470be442010-11-10 10:36:52 +01001570 }
1571 } /* else if (dgs > 64) {
1572 ... Be noisy about digest too large ...
1573 } */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574 }
1575
Philipp Reisner61120872011-02-08 09:50:54 +01001576 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001577
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 return ok;
1579}
1580
1581/* answer packet, used to send data back for read requests:
1582 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1583 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1584 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001585int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001586 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587{
1588 int ok;
1589 struct p_data p;
1590 void *dgb;
1591 int dgs;
1592
Philipp Reisnera0638452011-01-19 14:31:32 +01001593 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1594 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001595
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001596 prepare_header(mdev, &p.head, cmd, sizeof(p) -
1597 sizeof(struct p_header80) +
1598 dgs + peer_req->i.size);
1599 p.sector = cpu_to_be64(peer_req->i.sector);
1600 p.block_id = peer_req->block_id;
Andreas Gruenbachercc378272011-01-26 18:01:50 +01001601 p.seq_num = 0; /* unused */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602
1603 /* Only called by our kernel thread.
1604 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
1605 * in response to admin command or module unload.
1606 */
Philipp Reisner61120872011-02-08 09:50:54 +01001607 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001608 return 0;
1609
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001610 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 -07001611 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001612 dgb = mdev->tconn->int_dig_out;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001613 drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001614 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615 }
1616 if (ok)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001617 ok = _drbd_send_zc_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618
Philipp Reisner61120872011-02-08 09:50:54 +01001619 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001620
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621 return ok;
1622}
1623
Philipp Reisner73a01a12010-10-27 14:33:00 +02001624int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
1625{
1626 struct p_block_desc p;
1627
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001628 p.sector = cpu_to_be64(req->i.sector);
1629 p.blksize = cpu_to_be32(req->i.size);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001630
1631 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p));
1632}
1633
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634/*
1635 drbd_send distinguishes two cases:
1636
1637 Packets sent via the data socket "sock"
1638 and packets sent via the meta data socket "msock"
1639
1640 sock msock
1641 -----------------+-------------------------+------------------------------
1642 timeout conf.timeout / 2 conf.timeout / 2
1643 timeout action send a ping via msock Abort communication
1644 and close all sockets
1645*/
1646
1647/*
1648 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1649 */
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001650int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651 void *buf, size_t size, unsigned msg_flags)
1652{
1653 struct kvec iov;
1654 struct msghdr msg;
1655 int rv, sent = 0;
1656
1657 if (!sock)
1658 return -1000;
1659
1660 /* THINK if (signal_pending) return ... ? */
1661
1662 iov.iov_base = buf;
1663 iov.iov_len = size;
1664
1665 msg.msg_name = NULL;
1666 msg.msg_namelen = 0;
1667 msg.msg_control = NULL;
1668 msg.msg_controllen = 0;
1669 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1670
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001671 if (sock == tconn->data.socket) {
1672 tconn->ko_count = tconn->net_conf->ko_count;
1673 drbd_update_congested(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674 }
1675 do {
1676 /* STRANGE
1677 * tcp_sendmsg does _not_ use its size parameter at all ?
1678 *
1679 * -EAGAIN on timeout, -EINTR on signal.
1680 */
1681/* THINK
1682 * do we need to block DRBD_SIG if sock == &meta.socket ??
1683 * otherwise wake_asender() might interrupt some send_*Ack !
1684 */
1685 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1686 if (rv == -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001687 if (we_should_drop_the_connection(tconn, sock))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688 break;
1689 else
1690 continue;
1691 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692 if (rv == -EINTR) {
1693 flush_signals(current);
1694 rv = 0;
1695 }
1696 if (rv < 0)
1697 break;
1698 sent += rv;
1699 iov.iov_base += rv;
1700 iov.iov_len -= rv;
1701 } while (sent < size);
1702
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001703 if (sock == tconn->data.socket)
1704 clear_bit(NET_CONGESTED, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705
1706 if (rv <= 0) {
1707 if (rv != -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001708 conn_err(tconn, "%s_sendmsg returned %d\n",
1709 sock == tconn->meta.socket ? "msock" : "sock",
1710 rv);
1711 drbd_force_state(tconn->volume0, NS(conn, C_BROKEN_PIPE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712 } else
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001713 drbd_force_state(tconn->volume0, NS(conn, C_TIMEOUT));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001714 }
1715
1716 return sent;
1717}
1718
1719static int drbd_open(struct block_device *bdev, fmode_t mode)
1720{
1721 struct drbd_conf *mdev = bdev->bd_disk->private_data;
1722 unsigned long flags;
1723 int rv = 0;
1724
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001725 mutex_lock(&drbd_main_mutex);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001726 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001727 /* to have a stable mdev->state.role
1728 * and no race with updating open_cnt */
1729
1730 if (mdev->state.role != R_PRIMARY) {
1731 if (mode & FMODE_WRITE)
1732 rv = -EROFS;
1733 else if (!allow_oos)
1734 rv = -EMEDIUMTYPE;
1735 }
1736
1737 if (!rv)
1738 mdev->open_cnt++;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001739 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001740 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741
1742 return rv;
1743}
1744
1745static int drbd_release(struct gendisk *gd, fmode_t mode)
1746{
1747 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001748 mutex_lock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001749 mdev->open_cnt--;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001750 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001751 return 0;
1752}
1753
Philipp Reisnerb411b362009-09-25 16:07:19 -07001754static void drbd_set_defaults(struct drbd_conf *mdev)
1755{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001756 /* This way we get a compile error when sync_conf grows,
1757 and we forgot to initialize it here */
1758 mdev->sync_conf = (struct syncer_conf) {
1759 /* .rate = */ DRBD_RATE_DEF,
1760 /* .after = */ DRBD_AFTER_DEF,
1761 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001762 /* .verify_alg = */ {}, 0,
1763 /* .cpu_mask = */ {}, 0,
1764 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02001765 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02001766 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
1767 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
1768 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
1769 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001770 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
1771 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001772 };
1773
1774 /* Have to use that way, because the layout differs between
1775 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776 mdev->state = (union drbd_state) {
1777 { .role = R_SECONDARY,
1778 .peer = R_UNKNOWN,
1779 .conn = C_STANDALONE,
1780 .disk = D_DISKLESS,
1781 .pdsk = D_UNKNOWN,
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001782 .susp = 0,
1783 .susp_nod = 0,
1784 .susp_fen = 0
Philipp Reisnerb411b362009-09-25 16:07:19 -07001785 } };
1786}
1787
1788void drbd_init_set_defaults(struct drbd_conf *mdev)
1789{
1790 /* the memset(,0,) did most of this.
1791 * note: only assignments, no allocation in here */
1792
1793 drbd_set_defaults(mdev);
1794
Philipp Reisnerb411b362009-09-25 16:07:19 -07001795 atomic_set(&mdev->ap_bio_cnt, 0);
1796 atomic_set(&mdev->ap_pending_cnt, 0);
1797 atomic_set(&mdev->rs_pending_cnt, 0);
1798 atomic_set(&mdev->unacked_cnt, 0);
1799 atomic_set(&mdev->local_cnt, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001800 atomic_set(&mdev->pp_in_use, 0);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001801 atomic_set(&mdev->pp_in_use_by_net, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02001802 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001803 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001804 atomic_set(&mdev->ap_in_flight, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805
1806 mutex_init(&mdev->md_io_mutex);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001807 mutex_init(&mdev->tconn->data.mutex);
1808 mutex_init(&mdev->tconn->meta.mutex);
1809 sema_init(&mdev->tconn->data.work.s, 0);
1810 sema_init(&mdev->tconn->meta.work.s, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 mutex_init(&mdev->state_mutex);
1812
Philipp Reisnere42325a2011-01-19 13:55:45 +01001813 spin_lock_init(&mdev->tconn->data.work.q_lock);
1814 spin_lock_init(&mdev->tconn->meta.work.q_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001815
1816 spin_lock_init(&mdev->al_lock);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001817 spin_lock_init(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818 spin_lock_init(&mdev->peer_seq_lock);
1819 spin_lock_init(&mdev->epoch_lock);
1820
1821 INIT_LIST_HEAD(&mdev->active_ee);
1822 INIT_LIST_HEAD(&mdev->sync_ee);
1823 INIT_LIST_HEAD(&mdev->done_ee);
1824 INIT_LIST_HEAD(&mdev->read_ee);
1825 INIT_LIST_HEAD(&mdev->net_ee);
1826 INIT_LIST_HEAD(&mdev->resync_reads);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001827 INIT_LIST_HEAD(&mdev->tconn->data.work.q);
1828 INIT_LIST_HEAD(&mdev->tconn->meta.work.q);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829 INIT_LIST_HEAD(&mdev->resync_work.list);
1830 INIT_LIST_HEAD(&mdev->unplug_work.list);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001831 INIT_LIST_HEAD(&mdev->go_diskless.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 INIT_LIST_HEAD(&mdev->md_sync_work.list);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001833 INIT_LIST_HEAD(&mdev->start_resync_work.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02001835
Philipp Reisner794abb72010-12-27 11:51:23 +01001836 mdev->resync_work.cb = w_resync_timer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837 mdev->unplug_work.cb = w_send_write_hint;
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001838 mdev->go_diskless.cb = w_go_diskless;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001839 mdev->md_sync_work.cb = w_md_sync;
1840 mdev->bm_io_work.w.cb = w_bitmap_io;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001841 mdev->start_resync_work.cb = w_start_resync;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001842
1843 mdev->resync_work.mdev = mdev;
1844 mdev->unplug_work.mdev = mdev;
1845 mdev->go_diskless.mdev = mdev;
1846 mdev->md_sync_work.mdev = mdev;
1847 mdev->bm_io_work.w.mdev = mdev;
1848 mdev->start_resync_work.mdev = mdev;
1849
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850 init_timer(&mdev->resync_timer);
1851 init_timer(&mdev->md_sync_timer);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001852 init_timer(&mdev->start_resync_timer);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001853 init_timer(&mdev->request_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001854 mdev->resync_timer.function = resync_timer_fn;
1855 mdev->resync_timer.data = (unsigned long) mdev;
1856 mdev->md_sync_timer.function = md_sync_timer_fn;
1857 mdev->md_sync_timer.data = (unsigned long) mdev;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001858 mdev->start_resync_timer.function = start_resync_timer_fn;
1859 mdev->start_resync_timer.data = (unsigned long) mdev;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001860 mdev->request_timer.function = request_timer_fn;
1861 mdev->request_timer.data = (unsigned long) mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001862
1863 init_waitqueue_head(&mdev->misc_wait);
1864 init_waitqueue_head(&mdev->state_wait);
1865 init_waitqueue_head(&mdev->ee_wait);
1866 init_waitqueue_head(&mdev->al_wait);
1867 init_waitqueue_head(&mdev->seq_wait);
1868
Philipp Reisnerbed879a2011-02-04 14:00:37 +01001869 drbd_thread_init(mdev, &mdev->tconn->receiver, drbdd_init, "receiver");
1870 drbd_thread_init(mdev, &mdev->tconn->worker, drbd_worker, "worker");
1871 drbd_thread_init(mdev, &mdev->tconn->asender, drbd_asender, "asender");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001873 /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */
Philipp Reisner2451fc32010-08-24 13:43:11 +02001874 mdev->write_ordering = WO_bdev_flush;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 mdev->resync_wenr = LC_FREE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001876 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1877 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878}
1879
1880void drbd_mdev_cleanup(struct drbd_conf *mdev)
1881{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001882 int i;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001883 if (mdev->tconn->receiver.t_state != NONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001884 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001885 mdev->tconn->receiver.t_state);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001886
1887 /* no need to lock it, I'm the only thread alive */
1888 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
1889 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
1890 mdev->al_writ_cnt =
1891 mdev->bm_writ_cnt =
1892 mdev->read_cnt =
1893 mdev->recv_cnt =
1894 mdev->send_cnt =
1895 mdev->writ_cnt =
1896 mdev->p_size =
1897 mdev->rs_start =
1898 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001899 mdev->rs_failed = 0;
1900 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001901 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001902 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1903 mdev->rs_mark_left[i] = 0;
1904 mdev->rs_mark_time[i] = 0;
1905 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01001906 D_ASSERT(mdev->tconn->net_conf == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001907
1908 drbd_set_my_capacity(mdev, 0);
1909 if (mdev->bitmap) {
1910 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01001911 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001912 drbd_bm_cleanup(mdev);
1913 }
1914
1915 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02001916 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001917
1918 /*
1919 * currently we drbd_init_ee only on module load, so
1920 * we may do drbd_release_ee only on module unload!
1921 */
1922 D_ASSERT(list_empty(&mdev->active_ee));
1923 D_ASSERT(list_empty(&mdev->sync_ee));
1924 D_ASSERT(list_empty(&mdev->done_ee));
1925 D_ASSERT(list_empty(&mdev->read_ee));
1926 D_ASSERT(list_empty(&mdev->net_ee));
1927 D_ASSERT(list_empty(&mdev->resync_reads));
Philipp Reisnere42325a2011-01-19 13:55:45 +01001928 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
1929 D_ASSERT(list_empty(&mdev->tconn->meta.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001930 D_ASSERT(list_empty(&mdev->resync_work.list));
1931 D_ASSERT(list_empty(&mdev->unplug_work.list));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001932 D_ASSERT(list_empty(&mdev->go_diskless.list));
Lars Ellenberg2265b472010-12-16 15:41:26 +01001933
1934 drbd_set_defaults(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001935}
1936
1937
1938static void drbd_destroy_mempools(void)
1939{
1940 struct page *page;
1941
1942 while (drbd_pp_pool) {
1943 page = drbd_pp_pool;
1944 drbd_pp_pool = (struct page *)page_private(page);
1945 __free_page(page);
1946 drbd_pp_vacant--;
1947 }
1948
1949 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
1950
1951 if (drbd_ee_mempool)
1952 mempool_destroy(drbd_ee_mempool);
1953 if (drbd_request_mempool)
1954 mempool_destroy(drbd_request_mempool);
1955 if (drbd_ee_cache)
1956 kmem_cache_destroy(drbd_ee_cache);
1957 if (drbd_request_cache)
1958 kmem_cache_destroy(drbd_request_cache);
1959 if (drbd_bm_ext_cache)
1960 kmem_cache_destroy(drbd_bm_ext_cache);
1961 if (drbd_al_ext_cache)
1962 kmem_cache_destroy(drbd_al_ext_cache);
1963
1964 drbd_ee_mempool = NULL;
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
1971 return;
1972}
1973
1974static int drbd_create_mempools(void)
1975{
1976 struct page *page;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001977 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978 int i;
1979
1980 /* prepare our caches and mempools */
1981 drbd_request_mempool = NULL;
1982 drbd_ee_cache = NULL;
1983 drbd_request_cache = NULL;
1984 drbd_bm_ext_cache = NULL;
1985 drbd_al_ext_cache = NULL;
1986 drbd_pp_pool = NULL;
1987
1988 /* caches */
1989 drbd_request_cache = kmem_cache_create(
1990 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
1991 if (drbd_request_cache == NULL)
1992 goto Enomem;
1993
1994 drbd_ee_cache = kmem_cache_create(
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001995 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001996 if (drbd_ee_cache == NULL)
1997 goto Enomem;
1998
1999 drbd_bm_ext_cache = kmem_cache_create(
2000 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2001 if (drbd_bm_ext_cache == NULL)
2002 goto Enomem;
2003
2004 drbd_al_ext_cache = kmem_cache_create(
2005 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2006 if (drbd_al_ext_cache == NULL)
2007 goto Enomem;
2008
2009 /* mempools */
2010 drbd_request_mempool = mempool_create(number,
2011 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2012 if (drbd_request_mempool == NULL)
2013 goto Enomem;
2014
2015 drbd_ee_mempool = mempool_create(number,
2016 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
Nicolas Kaiser2027ae12010-10-28 06:15:26 -06002017 if (drbd_ee_mempool == NULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002018 goto Enomem;
2019
2020 /* drbd's page pool */
2021 spin_lock_init(&drbd_pp_lock);
2022
2023 for (i = 0; i < number; i++) {
2024 page = alloc_page(GFP_HIGHUSER);
2025 if (!page)
2026 goto Enomem;
2027 set_page_private(page, (unsigned long)drbd_pp_pool);
2028 drbd_pp_pool = page;
2029 }
2030 drbd_pp_vacant = number;
2031
2032 return 0;
2033
2034Enomem:
2035 drbd_destroy_mempools(); /* in case we allocated some */
2036 return -ENOMEM;
2037}
2038
2039static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2040 void *unused)
2041{
2042 /* just so we have it. you never know what interesting things we
2043 * might want to do here some day...
2044 */
2045
2046 return NOTIFY_DONE;
2047}
2048
2049static struct notifier_block drbd_notifier = {
2050 .notifier_call = drbd_notify_sys,
2051};
2052
2053static void drbd_release_ee_lists(struct drbd_conf *mdev)
2054{
2055 int rr;
2056
2057 rr = drbd_release_ee(mdev, &mdev->active_ee);
2058 if (rr)
2059 dev_err(DEV, "%d EEs in active list found!\n", rr);
2060
2061 rr = drbd_release_ee(mdev, &mdev->sync_ee);
2062 if (rr)
2063 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2064
2065 rr = drbd_release_ee(mdev, &mdev->read_ee);
2066 if (rr)
2067 dev_err(DEV, "%d EEs in read list found!\n", rr);
2068
2069 rr = drbd_release_ee(mdev, &mdev->done_ee);
2070 if (rr)
2071 dev_err(DEV, "%d EEs in done list found!\n", rr);
2072
2073 rr = drbd_release_ee(mdev, &mdev->net_ee);
2074 if (rr)
2075 dev_err(DEV, "%d EEs in net list found!\n", rr);
2076}
2077
2078/* caution. no locking.
2079 * currently only used from module cleanup code. */
2080static void drbd_delete_device(unsigned int minor)
2081{
2082 struct drbd_conf *mdev = minor_to_mdev(minor);
2083
2084 if (!mdev)
2085 return;
2086
2087 /* paranoia asserts */
Andreas Gruenbacher70dc65e2010-12-21 14:46:57 +01002088 D_ASSERT(mdev->open_cnt == 0);
Philipp Reisnere42325a2011-01-19 13:55:45 +01002089 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002090 /* end paranoia asserts */
2091
2092 del_gendisk(mdev->vdisk);
2093
2094 /* cleanup stuff that may have been allocated during
2095 * device (re-)configuration or state changes */
2096
2097 if (mdev->this_bdev)
2098 bdput(mdev->this_bdev);
2099
2100 drbd_free_resources(mdev);
Philipp Reisner21114382011-01-19 12:26:59 +01002101 drbd_free_tconn(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002102
2103 drbd_release_ee_lists(mdev);
2104
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 lc_destroy(mdev->act_log);
2106 lc_destroy(mdev->resync);
2107
2108 kfree(mdev->p_uuid);
2109 /* mdev->p_uuid = NULL; */
2110
Philipp Reisnerb411b362009-09-25 16:07:19 -07002111 /* cleanup the rest that has been
2112 * allocated from drbd_new_device
2113 * and actually free the mdev itself */
2114 drbd_free_mdev(mdev);
2115}
2116
2117static void drbd_cleanup(void)
2118{
2119 unsigned int i;
2120
2121 unregister_reboot_notifier(&drbd_notifier);
2122
Lars Ellenberg17a93f32010-11-24 10:37:35 +01002123 /* first remove proc,
2124 * drbdsetup uses it's presence to detect
2125 * whether DRBD is loaded.
2126 * If we would get stuck in proc removal,
2127 * but have netlink already deregistered,
2128 * some drbdsetup commands may wait forever
2129 * for an answer.
2130 */
2131 if (drbd_proc)
2132 remove_proc_entry("drbd", NULL);
2133
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134 drbd_nl_cleanup();
2135
2136 if (minor_table) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002137 i = minor_count;
2138 while (i--)
2139 drbd_delete_device(i);
2140 drbd_destroy_mempools();
2141 }
2142
2143 kfree(minor_table);
2144
2145 unregister_blkdev(DRBD_MAJOR, "drbd");
2146
2147 printk(KERN_INFO "drbd: module cleanup done.\n");
2148}
2149
2150/**
2151 * drbd_congested() - Callback for pdflush
2152 * @congested_data: User data
2153 * @bdi_bits: Bits pdflush is currently interested in
2154 *
2155 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2156 */
2157static int drbd_congested(void *congested_data, int bdi_bits)
2158{
2159 struct drbd_conf *mdev = congested_data;
2160 struct request_queue *q;
2161 char reason = '-';
2162 int r = 0;
2163
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002164 if (!may_inc_ap_bio(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002165 /* DRBD has frozen IO */
2166 r = bdi_bits;
2167 reason = 'd';
2168 goto out;
2169 }
2170
2171 if (get_ldev(mdev)) {
2172 q = bdev_get_queue(mdev->ldev->backing_bdev);
2173 r = bdi_congested(&q->backing_dev_info, bdi_bits);
2174 put_ldev(mdev);
2175 if (r)
2176 reason = 'b';
2177 }
2178
Philipp Reisner01a311a2011-02-07 14:30:33 +01002179 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180 r |= (1 << BDI_async_congested);
2181 reason = reason == 'b' ? 'a' : 'n';
2182 }
2183
2184out:
2185 mdev->congestion_reason = reason;
2186 return r;
2187}
2188
Philipp Reisner21114382011-01-19 12:26:59 +01002189struct drbd_tconn *drbd_new_tconn(char *name)
2190{
2191 struct drbd_tconn *tconn;
2192
2193 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2194 if (!tconn)
2195 return NULL;
2196
2197 tconn->name = kstrdup(name, GFP_KERNEL);
2198 if (!tconn->name)
2199 goto fail;
2200
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002201 atomic_set(&tconn->net_cnt, 0);
2202 init_waitqueue_head(&tconn->net_cnt_wait);
Philipp Reisner062e8792011-02-08 11:09:18 +01002203 idr_init(&tconn->volumes);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002204
Philipp Reisner21114382011-01-19 12:26:59 +01002205 write_lock_irq(&global_state_lock);
2206 list_add(&tconn->all_tconn, &drbd_tconns);
2207 write_unlock_irq(&global_state_lock);
2208
2209 return tconn;
2210
2211fail:
2212 kfree(tconn->name);
2213 kfree(tconn);
2214
2215 return NULL;
2216}
2217
2218void drbd_free_tconn(struct drbd_tconn *tconn)
2219{
2220 write_lock_irq(&global_state_lock);
2221 list_del(&tconn->all_tconn);
2222 write_unlock_irq(&global_state_lock);
Philipp Reisner062e8792011-02-08 11:09:18 +01002223 idr_destroy(&tconn->volumes);
Philipp Reisner21114382011-01-19 12:26:59 +01002224
2225 kfree(tconn->name);
Philipp Reisnerb42a70a2011-01-27 10:55:20 +01002226 kfree(tconn->int_dig_out);
2227 kfree(tconn->int_dig_in);
2228 kfree(tconn->int_dig_vv);
Philipp Reisner21114382011-01-19 12:26:59 +01002229 kfree(tconn);
2230}
2231
Philipp Reisnerb411b362009-09-25 16:07:19 -07002232struct drbd_conf *drbd_new_device(unsigned int minor)
2233{
2234 struct drbd_conf *mdev;
2235 struct gendisk *disk;
2236 struct request_queue *q;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002237 char conn_name[9]; /* drbd1234N */
Philipp Reisner062e8792011-02-08 11:09:18 +01002238 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002239
2240 /* GFP_KERNEL, we are outside of all write-out paths */
2241 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
2242 if (!mdev)
2243 return NULL;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002244 sprintf(conn_name, "drbd%d", minor);
2245 mdev->tconn = drbd_new_tconn(conn_name);
Philipp Reisner21114382011-01-19 12:26:59 +01002246 if (!mdev->tconn)
2247 goto out_no_tconn;
Philipp Reisner062e8792011-02-08 11:09:18 +01002248 if (!idr_pre_get(&mdev->tconn->volumes, GFP_KERNEL))
2249 goto out_no_cpumask;
2250 if (idr_get_new(&mdev->tconn->volumes, mdev, &vnr))
2251 goto out_no_cpumask;
2252 if (vnr != 0) {
2253 dev_err(DEV, "vnr = %d\n", vnr);
2254 goto out_no_cpumask;
2255 }
Philipp Reisner80822282011-02-08 12:46:30 +01002256 if (!zalloc_cpumask_var(&mdev->tconn->cpu_mask, GFP_KERNEL))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002257 goto out_no_cpumask;
2258
Philipp Reisner21114382011-01-19 12:26:59 +01002259 mdev->tconn->volume0 = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002260 mdev->minor = minor;
2261
2262 drbd_init_set_defaults(mdev);
2263
2264 q = blk_alloc_queue(GFP_KERNEL);
2265 if (!q)
2266 goto out_no_q;
2267 mdev->rq_queue = q;
2268 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269
2270 disk = alloc_disk(1);
2271 if (!disk)
2272 goto out_no_disk;
2273 mdev->vdisk = disk;
2274
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002275 set_disk_ro(disk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276
2277 disk->queue = q;
2278 disk->major = DRBD_MAJOR;
2279 disk->first_minor = minor;
2280 disk->fops = &drbd_ops;
2281 sprintf(disk->disk_name, "drbd%d", minor);
2282 disk->private_data = mdev;
2283
2284 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
2285 /* we have no partitions. we contain only ourselves. */
2286 mdev->this_bdev->bd_contains = mdev->this_bdev;
2287
2288 q->backing_dev_info.congested_fn = drbd_congested;
2289 q->backing_dev_info.congested_data = mdev;
2290
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01002291 blk_queue_make_request(q, drbd_make_request);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002292 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2293 This triggers a max_bio_size message upon first attach or connect */
2294 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002295 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2296 blk_queue_merge_bvec(q, drbd_merge_bvec);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002297 q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298
2299 mdev->md_io_page = alloc_page(GFP_KERNEL);
2300 if (!mdev->md_io_page)
2301 goto out_no_io_page;
2302
2303 if (drbd_bm_init(mdev))
2304 goto out_no_bitmap;
2305 /* no need to lock access, we are still initializing this minor device. */
2306 if (!tl_init(mdev))
2307 goto out_no_tl;
Andreas Gruenbacherdac13892011-01-21 17:18:39 +01002308 mdev->read_requests = RB_ROOT;
Andreas Gruenbacherde696712011-01-20 15:00:24 +01002309 mdev->write_requests = RB_ROOT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310
Philipp Reisnerb411b362009-09-25 16:07:19 -07002311 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2312 if (!mdev->current_epoch)
2313 goto out_no_epoch;
2314
2315 INIT_LIST_HEAD(&mdev->current_epoch->list);
2316 mdev->epochs = 1;
2317
2318 return mdev;
2319
2320/* out_whatever_else:
2321 kfree(mdev->current_epoch); */
2322out_no_epoch:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002323 tl_cleanup(mdev);
2324out_no_tl:
2325 drbd_bm_cleanup(mdev);
2326out_no_bitmap:
2327 __free_page(mdev->md_io_page);
2328out_no_io_page:
2329 put_disk(disk);
2330out_no_disk:
2331 blk_cleanup_queue(q);
2332out_no_q:
Philipp Reisner80822282011-02-08 12:46:30 +01002333 free_cpumask_var(mdev->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334out_no_cpumask:
Philipp Reisner21114382011-01-19 12:26:59 +01002335 drbd_free_tconn(mdev->tconn);
2336out_no_tconn:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002337 kfree(mdev);
2338 return NULL;
2339}
2340
2341/* counterpart of drbd_new_device.
2342 * last part of drbd_delete_device. */
2343void drbd_free_mdev(struct drbd_conf *mdev)
2344{
2345 kfree(mdev->current_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002346 tl_cleanup(mdev);
2347 if (mdev->bitmap) /* should no longer be there. */
2348 drbd_bm_cleanup(mdev);
2349 __free_page(mdev->md_io_page);
2350 put_disk(mdev->vdisk);
2351 blk_cleanup_queue(mdev->rq_queue);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352 kfree(mdev);
2353}
2354
2355
2356int __init drbd_init(void)
2357{
2358 int err;
2359
Philipp Reisnerfd340c12011-01-19 16:57:39 +01002360 BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95));
2361 BUILD_BUG_ON(sizeof(struct p_handshake) != 80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002362
Philipp Reisner2b8a90b2011-01-10 11:15:17 +01002363 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364 printk(KERN_ERR
2365 "drbd: invalid minor_count (%d)\n", minor_count);
2366#ifdef MODULE
2367 return -EINVAL;
2368#else
2369 minor_count = 8;
2370#endif
2371 }
2372
2373 err = drbd_nl_init();
2374 if (err)
2375 return err;
2376
2377 err = register_blkdev(DRBD_MAJOR, "drbd");
2378 if (err) {
2379 printk(KERN_ERR
2380 "drbd: unable to register block device major %d\n",
2381 DRBD_MAJOR);
2382 return err;
2383 }
2384
2385 register_reboot_notifier(&drbd_notifier);
2386
2387 /*
2388 * allocate all necessary structs
2389 */
2390 err = -ENOMEM;
2391
2392 init_waitqueue_head(&drbd_pp_wait);
2393
2394 drbd_proc = NULL; /* play safe for drbd_cleanup */
2395 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
2396 GFP_KERNEL);
2397 if (!minor_table)
2398 goto Enomem;
2399
2400 err = drbd_create_mempools();
2401 if (err)
2402 goto Enomem;
2403
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01002404 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405 if (!drbd_proc) {
2406 printk(KERN_ERR "drbd: unable to register proc file\n");
2407 goto Enomem;
2408 }
2409
2410 rwlock_init(&global_state_lock);
Philipp Reisner21114382011-01-19 12:26:59 +01002411 INIT_LIST_HEAD(&drbd_tconns);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002412
2413 printk(KERN_INFO "drbd: initialized. "
2414 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2415 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2416 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2417 printk(KERN_INFO "drbd: registered as block device major %d\n",
2418 DRBD_MAJOR);
2419 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
2420
2421 return 0; /* Success! */
2422
2423Enomem:
2424 drbd_cleanup();
2425 if (err == -ENOMEM)
2426 /* currently always the case */
2427 printk(KERN_ERR "drbd: ran out of memory\n");
2428 else
2429 printk(KERN_ERR "drbd: initialization failure\n");
2430 return err;
2431}
2432
2433void drbd_free_bc(struct drbd_backing_dev *ldev)
2434{
2435 if (ldev == NULL)
2436 return;
2437
Tejun Heoe525fd82010-11-13 11:55:17 +01002438 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2439 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002440
2441 kfree(ldev);
2442}
2443
Philipp Reisner360cc742011-02-08 14:29:53 +01002444void drbd_free_sock(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002445{
Philipp Reisner360cc742011-02-08 14:29:53 +01002446 if (tconn->data.socket) {
2447 mutex_lock(&tconn->data.mutex);
2448 kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
2449 sock_release(tconn->data.socket);
2450 tconn->data.socket = NULL;
2451 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002452 }
Philipp Reisner360cc742011-02-08 14:29:53 +01002453 if (tconn->meta.socket) {
2454 mutex_lock(&tconn->meta.mutex);
2455 kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
2456 sock_release(tconn->meta.socket);
2457 tconn->meta.socket = NULL;
2458 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459 }
2460}
2461
2462
2463void drbd_free_resources(struct drbd_conf *mdev)
2464{
2465 crypto_free_hash(mdev->csums_tfm);
2466 mdev->csums_tfm = NULL;
2467 crypto_free_hash(mdev->verify_tfm);
2468 mdev->verify_tfm = NULL;
Philipp Reisnera0638452011-01-19 14:31:32 +01002469 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
2470 mdev->tconn->cram_hmac_tfm = NULL;
2471 crypto_free_hash(mdev->tconn->integrity_w_tfm);
2472 mdev->tconn->integrity_w_tfm = NULL;
2473 crypto_free_hash(mdev->tconn->integrity_r_tfm);
2474 mdev->tconn->integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475
Philipp Reisner360cc742011-02-08 14:29:53 +01002476 drbd_free_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002477
2478 __no_warn(local,
2479 drbd_free_bc(mdev->ldev);
2480 mdev->ldev = NULL;);
2481}
2482
2483/* meta data management */
2484
2485struct meta_data_on_disk {
2486 u64 la_size; /* last agreed size. */
2487 u64 uuid[UI_SIZE]; /* UUIDs. */
2488 u64 device_uuid;
2489 u64 reserved_u64_1;
2490 u32 flags; /* MDF */
2491 u32 magic;
2492 u32 md_size_sect;
2493 u32 al_offset; /* offset to this block */
2494 u32 al_nr_extents; /* important for restoring the AL */
2495 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
2496 u32 bm_offset; /* offset to the bitmap, from here */
2497 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
Philipp Reisner99432fc2011-05-20 16:39:13 +02002498 u32 la_peer_max_bio_size; /* last peer max_bio_size */
2499 u32 reserved_u32[3];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500
2501} __packed;
2502
2503/**
2504 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2505 * @mdev: DRBD device.
2506 */
2507void drbd_md_sync(struct drbd_conf *mdev)
2508{
2509 struct meta_data_on_disk *buffer;
2510 sector_t sector;
2511 int i;
2512
Lars Ellenbergee15b032010-09-03 10:00:09 +02002513 del_timer(&mdev->md_sync_timer);
2514 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002515 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
2516 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002517
2518 /* We use here D_FAILED and not D_ATTACHING because we try to write
2519 * metadata even if we detach due to a disk failure! */
2520 if (!get_ldev_if_state(mdev, D_FAILED))
2521 return;
2522
Philipp Reisnerb411b362009-09-25 16:07:19 -07002523 mutex_lock(&mdev->md_io_mutex);
2524 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2525 memset(buffer, 0, 512);
2526
2527 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
2528 for (i = UI_CURRENT; i < UI_SIZE; i++)
2529 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
2530 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
2531 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
2532
2533 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
2534 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
2535 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
2536 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
2537 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
2538
2539 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002540 buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002541
2542 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
2543 sector = mdev->ldev->md.md_offset;
2544
Lars Ellenberg3f3a9b82010-09-01 15:12:12 +02002545 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546 /* this was a try anyways ... */
2547 dev_err(DEV, "meta data update failed!\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002548 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002549 }
2550
2551 /* Update mdev->ldev->md.la_size_sect,
2552 * since we updated it on metadata. */
2553 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
2554
2555 mutex_unlock(&mdev->md_io_mutex);
2556 put_ldev(mdev);
2557}
2558
2559/**
2560 * drbd_md_read() - Reads in the meta data super block
2561 * @mdev: DRBD device.
2562 * @bdev: Device from which the meta data should be read in.
2563 *
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01002564 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
2566 */
2567int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
2568{
2569 struct meta_data_on_disk *buffer;
2570 int i, rv = NO_ERROR;
2571
2572 if (!get_ldev_if_state(mdev, D_ATTACHING))
2573 return ERR_IO_MD_DISK;
2574
Philipp Reisnerb411b362009-09-25 16:07:19 -07002575 mutex_lock(&mdev->md_io_mutex);
2576 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2577
2578 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002579 /* NOTE: can't do normal error processing here as this is
Philipp Reisnerb411b362009-09-25 16:07:19 -07002580 called BEFORE disk is attached */
2581 dev_err(DEV, "Error while reading metadata.\n");
2582 rv = ERR_IO_MD_DISK;
2583 goto err;
2584 }
2585
Andreas Gruenbachere7fad8a2011-01-11 13:54:02 +01002586 if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002587 dev_err(DEV, "Error while reading metadata, magic not found.\n");
2588 rv = ERR_MD_INVALID;
2589 goto err;
2590 }
2591 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
2592 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
2593 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
2594 rv = ERR_MD_INVALID;
2595 goto err;
2596 }
2597 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
2598 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
2599 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
2600 rv = ERR_MD_INVALID;
2601 goto err;
2602 }
2603 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
2604 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
2605 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
2606 rv = ERR_MD_INVALID;
2607 goto err;
2608 }
2609
2610 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
2611 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
2612 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
2613 rv = ERR_MD_INVALID;
2614 goto err;
2615 }
2616
2617 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
2618 for (i = UI_CURRENT; i < UI_SIZE; i++)
2619 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
2620 bdev->md.flags = be32_to_cpu(buffer->flags);
2621 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
2622 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
2623
Philipp Reisner87eeee42011-01-19 14:16:30 +01002624 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002625 if (mdev->state.conn < C_CONNECTED) {
2626 int peer;
2627 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
2628 peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
2629 mdev->peer_max_bio_size = peer;
2630 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002631 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002632
Philipp Reisnerb411b362009-09-25 16:07:19 -07002633 if (mdev->sync_conf.al_extents < 7)
2634 mdev->sync_conf.al_extents = 127;
2635
2636 err:
2637 mutex_unlock(&mdev->md_io_mutex);
2638 put_ldev(mdev);
2639
2640 return rv;
2641}
2642
2643/**
2644 * drbd_md_mark_dirty() - Mark meta data super block as dirty
2645 * @mdev: DRBD device.
2646 *
2647 * Call this function if you change anything that should be written to
2648 * the meta-data super block. This function sets MD_DIRTY, and starts a
2649 * timer that ensures that within five seconds you have to call drbd_md_sync().
2650 */
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002651#ifdef DEBUG
Lars Ellenbergee15b032010-09-03 10:00:09 +02002652void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
2653{
2654 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
2655 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
2656 mdev->last_md_mark_dirty.line = line;
2657 mdev->last_md_mark_dirty.func = func;
2658 }
2659}
2660#else
Philipp Reisnerb411b362009-09-25 16:07:19 -07002661void drbd_md_mark_dirty(struct drbd_conf *mdev)
2662{
Lars Ellenbergee15b032010-09-03 10:00:09 +02002663 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002664 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665}
Lars Ellenbergee15b032010-09-03 10:00:09 +02002666#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002667
2668static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
2669{
2670 int i;
2671
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002672 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002673 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002674}
2675
2676void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2677{
2678 if (idx == UI_CURRENT) {
2679 if (mdev->state.role == R_PRIMARY)
2680 val |= 1;
2681 else
2682 val &= ~((u64)1);
2683
2684 drbd_set_ed_uuid(mdev, val);
2685 }
2686
2687 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002688 drbd_md_mark_dirty(mdev);
2689}
2690
2691
2692void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2693{
2694 if (mdev->ldev->md.uuid[idx]) {
2695 drbd_uuid_move_history(mdev);
2696 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697 }
2698 _drbd_uuid_set(mdev, idx, val);
2699}
2700
2701/**
2702 * drbd_uuid_new_current() - Creates a new current UUID
2703 * @mdev: DRBD device.
2704 *
2705 * Creates a new current UUID, and rotates the old current UUID into
2706 * the bitmap slot. Causes an incremental resync upon next connect.
2707 */
2708void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
2709{
2710 u64 val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002711 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002712
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002713 if (bm_uuid)
2714 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
2715
Philipp Reisnerb411b362009-09-25 16:07:19 -07002716 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717
2718 get_random_bytes(&val, sizeof(u64));
2719 _drbd_uuid_set(mdev, UI_CURRENT, val);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002720 drbd_print_uuids(mdev, "new current UUID");
Lars Ellenbergaaa8e2b2010-10-15 13:16:53 +02002721 /* get it to stable storage _now_ */
2722 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002723}
2724
2725void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
2726{
2727 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
2728 return;
2729
2730 if (val == 0) {
2731 drbd_uuid_move_history(mdev);
2732 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2733 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002734 } else {
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002735 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
2736 if (bm_uuid)
2737 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002738
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002739 mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002740 }
2741 drbd_md_mark_dirty(mdev);
2742}
2743
2744/**
2745 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2746 * @mdev: DRBD device.
2747 *
2748 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
2749 */
2750int drbd_bmio_set_n_write(struct drbd_conf *mdev)
2751{
2752 int rv = -EIO;
2753
2754 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2755 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
2756 drbd_md_sync(mdev);
2757 drbd_bm_set_all(mdev);
2758
2759 rv = drbd_bm_write(mdev);
2760
2761 if (!rv) {
2762 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2763 drbd_md_sync(mdev);
2764 }
2765
2766 put_ldev(mdev);
2767 }
2768
2769 return rv;
2770}
2771
2772/**
2773 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2774 * @mdev: DRBD device.
2775 *
2776 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
2777 */
2778int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
2779{
2780 int rv = -EIO;
2781
Philipp Reisner07782862010-08-31 12:00:50 +02002782 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002783 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2784 drbd_bm_clear_all(mdev);
2785 rv = drbd_bm_write(mdev);
2786 put_ldev(mdev);
2787 }
2788
2789 return rv;
2790}
2791
2792static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2793{
2794 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002795 int rv = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796
2797 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
2798
Lars Ellenberg02851e92010-12-16 14:47:39 +01002799 if (get_ldev(mdev)) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002800 drbd_bm_lock(mdev, work->why, work->flags);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002801 rv = work->io_fn(mdev);
2802 drbd_bm_unlock(mdev);
2803 put_ldev(mdev);
2804 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002805
2806 clear_bit(BITMAP_IO, &mdev->flags);
Philipp Reisner127b3172010-11-16 10:07:53 +01002807 smp_mb__after_clear_bit();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002808 wake_up(&mdev->misc_wait);
2809
2810 if (work->done)
2811 work->done(mdev, rv);
2812
2813 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
2814 work->why = NULL;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002815 work->flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816
2817 return 1;
2818}
2819
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002820void drbd_ldev_destroy(struct drbd_conf *mdev)
2821{
2822 lc_destroy(mdev->resync);
2823 mdev->resync = NULL;
2824 lc_destroy(mdev->act_log);
2825 mdev->act_log = NULL;
2826 __no_warn(local,
2827 drbd_free_bc(mdev->ldev);
2828 mdev->ldev = NULL;);
2829
2830 if (mdev->md_io_tmpp) {
2831 __free_page(mdev->md_io_tmpp);
2832 mdev->md_io_tmpp = NULL;
2833 }
2834 clear_bit(GO_DISKLESS, &mdev->flags);
2835}
2836
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002837static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2838{
2839 D_ASSERT(mdev->state.disk == D_FAILED);
Lars Ellenberg9d282872010-10-14 13:57:07 +02002840 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
2841 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002842 * the protected members anymore, though, so once put_ldev reaches zero
2843 * again, it will be safe to free them. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002844 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002845 return 1;
2846}
2847
2848void drbd_go_diskless(struct drbd_conf *mdev)
2849{
2850 D_ASSERT(mdev->state.disk == D_FAILED);
2851 if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002852 drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002853}
2854
Philipp Reisnerb411b362009-09-25 16:07:19 -07002855/**
2856 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
2857 * @mdev: DRBD device.
2858 * @io_fn: IO callback to be called when bitmap IO is possible
2859 * @done: callback to be called after the bitmap IO was performed
2860 * @why: Descriptive text of the reason for doing the IO
2861 *
2862 * While IO on the bitmap happens we freeze application IO thus we ensure
2863 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
2864 * called from worker context. It MUST NOT be used while a previous such
2865 * work is still pending!
2866 */
2867void drbd_queue_bitmap_io(struct drbd_conf *mdev,
2868 int (*io_fn)(struct drbd_conf *),
2869 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002870 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002871{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002872 D_ASSERT(current == mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002873
2874 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
2875 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
2876 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
2877 if (mdev->bm_io_work.why)
2878 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
2879 why, mdev->bm_io_work.why);
2880
2881 mdev->bm_io_work.io_fn = io_fn;
2882 mdev->bm_io_work.done = done;
2883 mdev->bm_io_work.why = why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002884 mdev->bm_io_work.flags = flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002885
Philipp Reisner87eeee42011-01-19 14:16:30 +01002886 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002887 set_bit(BITMAP_IO, &mdev->flags);
2888 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
Philipp Reisner127b3172010-11-16 10:07:53 +01002889 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002890 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002891 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002892 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002893}
2894
2895/**
2896 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
2897 * @mdev: DRBD device.
2898 * @io_fn: IO callback to be called when bitmap IO is possible
2899 * @why: Descriptive text of the reason for doing the IO
2900 *
2901 * freezes application IO while that the actual IO operations runs. This
2902 * functions MAY NOT be called from worker context.
2903 */
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002904int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
2905 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002906{
2907 int rv;
2908
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002909 D_ASSERT(current != mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002910
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002911 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2912 drbd_suspend_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002913
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002914 drbd_bm_lock(mdev, why, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002915 rv = io_fn(mdev);
2916 drbd_bm_unlock(mdev);
2917
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002918 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2919 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002920
2921 return rv;
2922}
2923
2924void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2925{
2926 if ((mdev->ldev->md.flags & flag) != flag) {
2927 drbd_md_mark_dirty(mdev);
2928 mdev->ldev->md.flags |= flag;
2929 }
2930}
2931
2932void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2933{
2934 if ((mdev->ldev->md.flags & flag) != 0) {
2935 drbd_md_mark_dirty(mdev);
2936 mdev->ldev->md.flags &= ~flag;
2937 }
2938}
2939int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
2940{
2941 return (bdev->md.flags & flag) != 0;
2942}
2943
2944static void md_sync_timer_fn(unsigned long data)
2945{
2946 struct drbd_conf *mdev = (struct drbd_conf *) data;
2947
Philipp Reisnere42325a2011-01-19 13:55:45 +01002948 drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002949}
2950
2951static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2952{
2953 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02002954#ifdef DEBUG
2955 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
2956 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
2957#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002958 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002959 return 1;
2960}
2961
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002962const char *cmdname(enum drbd_packet cmd)
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01002963{
2964 /* THINK may need to become several global tables
2965 * when we want to support more than
2966 * one PRO_VERSION */
2967 static const char *cmdnames[] = {
2968 [P_DATA] = "Data",
2969 [P_DATA_REPLY] = "DataReply",
2970 [P_RS_DATA_REPLY] = "RSDataReply",
2971 [P_BARRIER] = "Barrier",
2972 [P_BITMAP] = "ReportBitMap",
2973 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
2974 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
2975 [P_UNPLUG_REMOTE] = "UnplugRemote",
2976 [P_DATA_REQUEST] = "DataRequest",
2977 [P_RS_DATA_REQUEST] = "RSDataRequest",
2978 [P_SYNC_PARAM] = "SyncParam",
2979 [P_SYNC_PARAM89] = "SyncParam89",
2980 [P_PROTOCOL] = "ReportProtocol",
2981 [P_UUIDS] = "ReportUUIDs",
2982 [P_SIZES] = "ReportSizes",
2983 [P_STATE] = "ReportState",
2984 [P_SYNC_UUID] = "ReportSyncUUID",
2985 [P_AUTH_CHALLENGE] = "AuthChallenge",
2986 [P_AUTH_RESPONSE] = "AuthResponse",
2987 [P_PING] = "Ping",
2988 [P_PING_ACK] = "PingAck",
2989 [P_RECV_ACK] = "RecvAck",
2990 [P_WRITE_ACK] = "WriteAck",
2991 [P_RS_WRITE_ACK] = "RSWriteAck",
2992 [P_DISCARD_ACK] = "DiscardAck",
2993 [P_NEG_ACK] = "NegAck",
2994 [P_NEG_DREPLY] = "NegDReply",
2995 [P_NEG_RS_DREPLY] = "NegRSDReply",
2996 [P_BARRIER_ACK] = "BarrierAck",
2997 [P_STATE_CHG_REQ] = "StateChgRequest",
2998 [P_STATE_CHG_REPLY] = "StateChgReply",
2999 [P_OV_REQUEST] = "OVRequest",
3000 [P_OV_REPLY] = "OVReply",
3001 [P_OV_RESULT] = "OVResult",
3002 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3003 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3004 [P_COMPRESSED_BITMAP] = "CBitmap",
3005 [P_DELAY_PROBE] = "DelayProbe",
3006 [P_OUT_OF_SYNC] = "OutOfSync",
3007 [P_MAX_CMD] = NULL,
3008 };
3009
3010 if (cmd == P_HAND_SHAKE_M)
3011 return "HandShakeM";
3012 if (cmd == P_HAND_SHAKE_S)
3013 return "HandShakeS";
3014 if (cmd == P_HAND_SHAKE)
3015 return "HandShake";
3016 if (cmd >= P_MAX_CMD)
3017 return "Unknown";
3018 return cmdnames[cmd];
3019}
3020
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021#ifdef CONFIG_DRBD_FAULT_INJECTION
3022/* Fault insertion support including random number generator shamelessly
3023 * stolen from kernel/rcutorture.c */
3024struct fault_random_state {
3025 unsigned long state;
3026 unsigned long count;
3027};
3028
3029#define FAULT_RANDOM_MULT 39916801 /* prime */
3030#define FAULT_RANDOM_ADD 479001701 /* prime */
3031#define FAULT_RANDOM_REFRESH 10000
3032
3033/*
3034 * Crude but fast random-number generator. Uses a linear congruential
3035 * generator, with occasional help from get_random_bytes().
3036 */
3037static unsigned long
3038_drbd_fault_random(struct fault_random_state *rsp)
3039{
3040 long refresh;
3041
Roel Kluin49829ea2009-12-15 22:55:44 +01003042 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003043 get_random_bytes(&refresh, sizeof(refresh));
3044 rsp->state += refresh;
3045 rsp->count = FAULT_RANDOM_REFRESH;
3046 }
3047 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3048 return swahw32(rsp->state);
3049}
3050
3051static char *
3052_drbd_fault_str(unsigned int type) {
3053 static char *_faults[] = {
3054 [DRBD_FAULT_MD_WR] = "Meta-data write",
3055 [DRBD_FAULT_MD_RD] = "Meta-data read",
3056 [DRBD_FAULT_RS_WR] = "Resync write",
3057 [DRBD_FAULT_RS_RD] = "Resync read",
3058 [DRBD_FAULT_DT_WR] = "Data write",
3059 [DRBD_FAULT_DT_RD] = "Data read",
3060 [DRBD_FAULT_DT_RA] = "Data read ahead",
3061 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003062 [DRBD_FAULT_AL_EE] = "EE allocation",
3063 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003064 };
3065
3066 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3067}
3068
3069unsigned int
3070_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3071{
3072 static struct fault_random_state rrs = {0, 0};
3073
3074 unsigned int ret = (
3075 (fault_devs == 0 ||
3076 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3077 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3078
3079 if (ret) {
3080 fault_count++;
3081
Lars Ellenberg73835062010-05-27 11:51:56 +02003082 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003083 dev_warn(DEV, "***Simulating %s failure\n",
3084 _drbd_fault_str(type));
3085 }
3086
3087 return ret;
3088}
3089#endif
3090
3091const char *drbd_buildtag(void)
3092{
3093 /* DRBD built from external sources has here a reference to the
3094 git hash of the source code. */
3095
3096 static char buildtag[38] = "\0uilt-in";
3097
3098 if (buildtag[0] == 0) {
3099#ifdef CONFIG_MODULES
3100 if (THIS_MODULE != NULL)
3101 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3102 else
3103#endif
3104 buildtag[0] = 'b';
3105 }
3106
3107 return buildtag;
3108}
3109
3110module_init(drbd_init)
3111module_exit(drbd_cleanup)
3112
Philipp Reisnerb411b362009-09-25 16:07:19 -07003113EXPORT_SYMBOL(drbd_conn_str);
3114EXPORT_SYMBOL(drbd_role_str);
3115EXPORT_SYMBOL(drbd_disk_str);
3116EXPORT_SYMBOL(drbd_set_st_err_str);