blob: c77e51a40926c1d58bae27886054559177e85561 [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 Reisner00d56942011-02-09 18:09:48 +010067static int w_md_sync(struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070068static void md_sync_timer_fn(unsigned long data);
Philipp Reisner00d56942011-02-09 18:09:48 +010069static int w_bitmap_io(struct drbd_work *w, int unused);
70static int w_go_diskless(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;
Philipp Reisner392c8802011-02-09 10:33:31 +0100445 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446 unsigned long flags;
447 int retval;
448
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100449 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
Philipp Reisner392c8802011-02-09 10:33:31 +0100450 thi->name[0], thi->tconn->name);
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100451
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 Reisner392c8802011-02-09 10:33:31 +0100468 conn_info(tconn, "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
Philipp Reisner392c8802011-02-09 10:33:31 +0100480 conn_info(tconn, "Terminating %s\n", current->comm);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481
482 /* Release mod reference taken when thread was started */
483 module_put(THIS_MODULE);
484 return retval;
485}
486
Philipp Reisner392c8802011-02-09 10:33:31 +0100487static void drbd_thread_init(struct drbd_tconn *tconn, 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;
Philipp Reisner392c8802011-02-09 10:33:31 +0100494 thi->tconn = tconn;
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{
Philipp Reisner392c8802011-02-09 10:33:31 +0100500 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 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 Reisner392c8802011-02-09 10:33:31 +0100510 conn_info(tconn, "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)) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100515 conn_err(tconn, "Failed to get module reference in drbd_thread_start\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516 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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 thi->reset_cpu_mask = 1;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100522 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700523 spin_unlock_irqrestore(&thi->t_lock, flags);
524 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
525
526 nt = kthread_create(drbd_thread_setup, (void *) thi,
Philipp Reisner392c8802011-02-09 10:33:31 +0100527 "drbd_%c_%s", thi->name[0], thi->tconn->name);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700528
529 if (IS_ERR(nt)) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100530 conn_err(tconn, "Couldn't start thread\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700531
532 module_put(THIS_MODULE);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100533 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 }
535 spin_lock_irqsave(&thi->t_lock, flags);
536 thi->task = nt;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100537 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538 spin_unlock_irqrestore(&thi->t_lock, flags);
539 wake_up_process(nt);
540 break;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100541 case EXITING:
542 thi->t_state = RESTARTING;
Philipp Reisner392c8802011-02-09 10:33:31 +0100543 conn_info(tconn, "Restarting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100544 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 /* fall through */
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100546 case RUNNING:
547 case RESTARTING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548 default:
549 spin_unlock_irqrestore(&thi->t_lock, flags);
550 break;
551 }
552
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100553 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554}
555
556
557void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
558{
559 unsigned long flags;
560
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100561 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562
563 /* may be called from state engine, holding the req lock irqsave */
564 spin_lock_irqsave(&thi->t_lock, flags);
565
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100566 if (thi->t_state == NONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 spin_unlock_irqrestore(&thi->t_lock, flags);
568 if (restart)
569 drbd_thread_start(thi);
570 return;
571 }
572
573 if (thi->t_state != ns) {
574 if (thi->task == NULL) {
575 spin_unlock_irqrestore(&thi->t_lock, flags);
576 return;
577 }
578
579 thi->t_state = ns;
580 smp_mb();
581 init_completion(&thi->stop);
582 if (thi->task != current)
583 force_sig(DRBD_SIGKILL, thi->task);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 }
585
586 spin_unlock_irqrestore(&thi->t_lock, flags);
587
588 if (wait)
589 wait_for_completion(&thi->stop);
590}
591
Philipp Reisner392c8802011-02-09 10:33:31 +0100592static struct drbd_thread *drbd_task_to_thread(struct drbd_tconn *tconn, struct task_struct *task)
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100593{
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100594 struct drbd_thread *thi =
595 task == tconn->receiver.task ? &tconn->receiver :
596 task == tconn->asender.task ? &tconn->asender :
597 task == tconn->worker.task ? &tconn->worker : NULL;
598
599 return thi;
600}
601
Philipp Reisner392c8802011-02-09 10:33:31 +0100602char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task)
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100603{
Philipp Reisner392c8802011-02-09 10:33:31 +0100604 struct drbd_thread *thi = drbd_task_to_thread(tconn, task);
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100605 return thi ? thi->name : task->comm;
606}
607
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608#ifdef CONFIG_SMP
Philipp Reisner80822282011-02-08 12:46:30 +0100609static int conn_lowest_minor(struct drbd_tconn *tconn)
610{
611 int minor = 0;
612 idr_get_next(&tconn->volumes, &minor);
613 return minor;
614}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615/**
616 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
617 * @mdev: DRBD device.
618 *
619 * Forces all threads of a device onto the same CPU. This is beneficial for
620 * DRBD's performance. May be overwritten by user's configuration.
621 */
Philipp Reisner80822282011-02-08 12:46:30 +0100622void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623{
624 int ord, cpu;
625
626 /* user override. */
Philipp Reisner80822282011-02-08 12:46:30 +0100627 if (cpumask_weight(tconn->cpu_mask))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628 return;
629
Philipp Reisner80822282011-02-08 12:46:30 +0100630 ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 for_each_online_cpu(cpu) {
632 if (ord-- == 0) {
Philipp Reisner80822282011-02-08 12:46:30 +0100633 cpumask_set_cpu(cpu, tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634 return;
635 }
636 }
637 /* should not be reached */
Philipp Reisner80822282011-02-08 12:46:30 +0100638 cpumask_setall(tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639}
640
641/**
642 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
643 * @mdev: DRBD device.
Philipp Reisnerbc31fe32011-02-07 11:14:38 +0100644 * @thi: drbd_thread object
Philipp Reisnerb411b362009-09-25 16:07:19 -0700645 *
646 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
647 * prematurely.
648 */
Philipp Reisner80822282011-02-08 12:46:30 +0100649void drbd_thread_current_set_cpu(struct drbd_thread *thi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650{
651 struct task_struct *p = current;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100652
Philipp Reisnerb411b362009-09-25 16:07:19 -0700653 if (!thi->reset_cpu_mask)
654 return;
655 thi->reset_cpu_mask = 0;
Philipp Reisner392c8802011-02-09 10:33:31 +0100656 set_cpus_allowed_ptr(p, thi->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700657}
658#endif
659
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100660static void prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100661{
662 h->magic = cpu_to_be32(DRBD_MAGIC);
663 h->command = cpu_to_be16(cmd);
664 h->length = cpu_to_be16(size);
665}
666
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100667static void prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100668{
669 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
670 h->command = cpu_to_be16(cmd);
671 h->length = cpu_to_be32(size);
672}
673
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100674static void _prepare_header(struct drbd_tconn *tconn, int vnr, struct p_header *h,
675 enum drbd_packet cmd, int size)
676{
677 if (tconn->agreed_pro_version >= 100 || size > DRBD_MAX_SIZE_H80_PACKET)
678 prepare_header95(&h->h95, cmd, size);
679 else
680 prepare_header80(&h->h80, cmd, size);
681}
682
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100683static void prepare_header(struct drbd_conf *mdev, struct p_header *h,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100684 enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100685{
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100686 _prepare_header(mdev->tconn, mdev->vnr, h, cmd, size);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100687}
688
Philipp Reisnerb411b362009-09-25 16:07:19 -0700689/* the appropriate socket mutex must be held already */
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100690int _conn_send_cmd(struct drbd_tconn *tconn, int vnr, struct socket *sock,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100691 enum drbd_packet cmd, struct p_header *h, size_t size,
692 unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700693{
694 int sent, ok;
695
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100696 _prepare_header(tconn, vnr, h, cmd, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100698 sent = drbd_send(tconn, sock, h, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700699
700 ok = (sent == size);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +0100701 if (!ok && !signal_pending(current))
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100702 conn_warn(tconn, "short sent %s size=%d sent=%d\n",
703 cmdname(cmd), (int)size, sent);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 return ok;
705}
706
707/* don't pass the socket. we may only look at it
708 * when we hold the appropriate socket mutex.
709 */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100710int conn_send_cmd(struct drbd_tconn *tconn, int vnr, int use_data_socket,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100711 enum drbd_packet cmd, struct p_header *h, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712{
713 int ok = 0;
714 struct socket *sock;
715
716 if (use_data_socket) {
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100717 mutex_lock(&tconn->data.mutex);
718 sock = tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719 } else {
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100720 mutex_lock(&tconn->meta.mutex);
721 sock = tconn->meta.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700722 }
723
724 /* drbd_disconnect() could have called drbd_free_sock()
725 * while we were waiting in down()... */
726 if (likely(sock != NULL))
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100727 ok = _conn_send_cmd(tconn, vnr, sock, cmd, h, size, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728
729 if (use_data_socket)
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100730 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731 else
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100732 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733 return ok;
734}
735
Philipp Reisner61120872011-02-08 09:50:54 +0100736int conn_send_cmd2(struct drbd_tconn *tconn, enum drbd_packet cmd, char *data,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700737 size_t size)
738{
Philipp Reisner61120872011-02-08 09:50:54 +0100739 struct p_header80 h;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 int ok;
741
Philipp Reisner61120872011-02-08 09:50:54 +0100742 prepare_header80(&h, cmd, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743
Philipp Reisner61120872011-02-08 09:50:54 +0100744 if (!drbd_get_data_sock(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745 return 0;
746
Philipp Reisnerb411b362009-09-25 16:07:19 -0700747 ok = (sizeof(h) ==
Philipp Reisner61120872011-02-08 09:50:54 +0100748 drbd_send(tconn, tconn->data.socket, &h, sizeof(h), 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700749 ok = ok && (size ==
Philipp Reisner61120872011-02-08 09:50:54 +0100750 drbd_send(tconn, tconn->data.socket, data, size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700751
Philipp Reisner61120872011-02-08 09:50:54 +0100752 drbd_put_data_sock(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753
754 return ok;
755}
756
757int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
758{
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200759 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760 struct socket *sock;
761 int size, rv;
Philipp Reisner31890f42011-01-19 14:12:51 +0100762 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763
764 size = apv <= 87 ? sizeof(struct p_rs_param)
765 : apv == 88 ? sizeof(struct p_rs_param)
766 + strlen(mdev->sync_conf.verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200767 : apv <= 94 ? sizeof(struct p_rs_param_89)
768 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769
770 /* used from admin command context and receiver/worker context.
771 * to avoid kmalloc, grab the socket right here,
772 * then use the pre-allocated sbuf there */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100773 mutex_lock(&mdev->tconn->data.mutex);
774 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700775
776 if (likely(sock != NULL)) {
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100777 enum drbd_packet cmd =
778 apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700779
Philipp Reisnere42325a2011-01-19 13:55:45 +0100780 p = &mdev->tconn->data.sbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781
782 /* initialize verify_alg and csums_alg */
783 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
784
785 p->rate = cpu_to_be32(sc->rate);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200786 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
787 p->c_delay_target = cpu_to_be32(sc->c_delay_target);
788 p->c_fill_target = cpu_to_be32(sc->c_fill_target);
789 p->c_max_rate = cpu_to_be32(sc->c_max_rate);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790
791 if (apv >= 88)
792 strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
793 if (apv >= 89)
794 strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
795
796 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
797 } else
798 rv = 0; /* not ok */
799
Philipp Reisnere42325a2011-01-19 13:55:45 +0100800 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700801
802 return rv;
803}
804
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100805int drbd_send_protocol(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806{
807 struct p_protocol *p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100808 int size, cf, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809
810 size = sizeof(struct p_protocol);
811
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100812 if (tconn->agreed_pro_version >= 87)
813 size += strlen(tconn->net_conf->integrity_alg) + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700814
815 /* we must not recurse into our own queue,
816 * as that is blocked during handshake */
817 p = kmalloc(size, GFP_NOIO);
818 if (p == NULL)
819 return 0;
820
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100821 p->protocol = cpu_to_be32(tconn->net_conf->wire_protocol);
822 p->after_sb_0p = cpu_to_be32(tconn->net_conf->after_sb_0p);
823 p->after_sb_1p = cpu_to_be32(tconn->net_conf->after_sb_1p);
824 p->after_sb_2p = cpu_to_be32(tconn->net_conf->after_sb_2p);
825 p->two_primaries = cpu_to_be32(tconn->net_conf->two_primaries);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100827 cf = 0;
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100828 if (tconn->net_conf->want_lose)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100829 cf |= CF_WANT_LOSE;
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100830 if (tconn->net_conf->dry_run) {
831 if (tconn->agreed_pro_version >= 92)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100832 cf |= CF_DRY_RUN;
833 else {
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100834 conn_err(tconn, "--dry-run is not supported by peer");
Dan Carpenter7ac314c2010-04-22 14:27:23 +0200835 kfree(p);
Philipp Reisner148efa12011-01-15 00:21:15 +0100836 return -1;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100837 }
838 }
839 p->conn_flags = cpu_to_be32(cf);
840
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100841 if (tconn->agreed_pro_version >= 87)
842 strcpy(p->integrity_alg, tconn->net_conf->integrity_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700843
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100844 rv = conn_send_cmd2(tconn, P_PROTOCOL, p->head.payload, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700845 kfree(p);
846 return rv;
847}
848
849int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
850{
851 struct p_uuids p;
852 int i;
853
854 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
855 return 1;
856
857 for (i = UI_CURRENT; i < UI_SIZE; i++)
858 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
859
860 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
861 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
Philipp Reisner89e58e72011-01-19 13:12:45 +0100862 uuid_flags |= mdev->tconn->net_conf->want_lose ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700863 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
864 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
865 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
866
867 put_ldev(mdev);
868
Philipp Reisnerc0129492011-01-19 16:58:16 +0100869 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870}
871
872int drbd_send_uuids(struct drbd_conf *mdev)
873{
874 return _drbd_send_uuids(mdev, 0);
875}
876
877int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
878{
879 return _drbd_send_uuids(mdev, 8);
880}
881
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100882void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
883{
884 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
885 u64 *uuid = mdev->ldev->md.uuid;
886 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
887 text,
888 (unsigned long long)uuid[UI_CURRENT],
889 (unsigned long long)uuid[UI_BITMAP],
890 (unsigned long long)uuid[UI_HISTORY_START],
891 (unsigned long long)uuid[UI_HISTORY_END]);
892 put_ldev(mdev);
893 } else {
894 dev_info(DEV, "%s effective data uuid: %016llX\n",
895 text,
896 (unsigned long long)mdev->ed_uuid);
897 }
898}
899
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100900int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901{
902 struct p_rs_uuid p;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100903 u64 uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100905 D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
906
Philipp Reisner4a23f262011-01-11 17:42:17 +0100907 uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100908 drbd_uuid_set(mdev, UI_BITMAP, uuid);
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100909 drbd_print_uuids(mdev, "updated sync UUID");
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100910 drbd_md_sync(mdev);
911 p.uuid = cpu_to_be64(uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912
Philipp Reisnerc0129492011-01-19 16:58:16 +0100913 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700914}
915
Philipp Reisnere89b5912010-03-24 17:11:33 +0100916int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700917{
918 struct p_sizes p;
919 sector_t d_size, u_size;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200920 int q_order_type, max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 int ok;
922
923 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
924 D_ASSERT(mdev->ldev->backing_bdev);
925 d_size = drbd_get_max_capacity(mdev->ldev);
926 u_size = mdev->ldev->dc.disk_size;
927 q_order_type = drbd_queue_order_type(mdev);
Philipp Reisner99432fc2011-05-20 16:39:13 +0200928 max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
929 max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700930 put_ldev(mdev);
931 } else {
932 d_size = 0;
933 u_size = 0;
934 q_order_type = QUEUE_ORDERED_NONE;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200935 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 }
937
938 p.d_size = cpu_to_be64(d_size);
939 p.u_size = cpu_to_be64(u_size);
940 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
Philipp Reisner99432fc2011-05-20 16:39:13 +0200941 p.max_bio_size = cpu_to_be32(max_bio_size);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100942 p.queue_order_type = cpu_to_be16(q_order_type);
943 p.dds_flags = cpu_to_be16(flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944
Philipp Reisnerc0129492011-01-19 16:58:16 +0100945 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 return ok;
947}
948
949/**
950 * drbd_send_state() - Sends the drbd state to the peer
951 * @mdev: DRBD device.
952 */
953int drbd_send_state(struct drbd_conf *mdev)
954{
955 struct socket *sock;
956 struct p_state p;
957 int ok = 0;
958
Philipp Reisnere42325a2011-01-19 13:55:45 +0100959 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960
961 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100962 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
964 if (likely(sock != NULL)) {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100965 ok = _drbd_send_cmd(mdev, sock, P_STATE, &p.head, sizeof(p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966 }
967
Philipp Reisnere42325a2011-01-19 13:55:45 +0100968 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970 return ok;
971}
972
Philipp Reisnercf29c9d2011-02-11 15:11:24 +0100973int _conn_send_state_req(struct drbd_tconn *tconn, int vnr, enum drbd_packet cmd,
974 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975{
976 struct p_req_state p;
977
978 p.mask = cpu_to_be32(mask.i);
979 p.val = cpu_to_be32(val.i);
980
Philipp Reisnercf29c9d2011-02-11 15:11:24 +0100981 return conn_send_cmd(tconn, vnr, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982}
983
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100984int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985{
986 struct p_req_state_reply p;
987
988 p.retcode = cpu_to_be32(retcode);
989
Philipp Reisnerc0129492011-01-19 16:58:16 +0100990 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991}
992
Philipp Reisner047cd4a2011-02-15 11:09:33 +0100993int conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode)
994{
995 struct p_req_state_reply p;
996 enum drbd_packet cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
997
998 p.retcode = cpu_to_be32(retcode);
999
1000 return conn_send_cmd(tconn, 0, USE_META_SOCKET, cmd, &p.head, sizeof(p));
1001}
1002
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003int fill_bitmap_rle_bits(struct drbd_conf *mdev,
1004 struct p_compressed_bm *p,
1005 struct bm_xfer_ctx *c)
1006{
1007 struct bitstream bs;
1008 unsigned long plain_bits;
1009 unsigned long tmp;
1010 unsigned long rl;
1011 unsigned len;
1012 unsigned toggle;
1013 int bits;
1014
1015 /* may we use this feature? */
1016 if ((mdev->sync_conf.use_rle == 0) ||
Philipp Reisner31890f42011-01-19 14:12:51 +01001017 (mdev->tconn->agreed_pro_version < 90))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 return 0;
1019
1020 if (c->bit_offset >= c->bm_bits)
1021 return 0; /* nothing to do. */
1022
1023 /* use at most thus many bytes */
1024 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1025 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1026 /* plain bits covered in this code string */
1027 plain_bits = 0;
1028
1029 /* p->encoding & 0x80 stores whether the first run length is set.
1030 * bit offset is implicit.
1031 * start with toggle == 2 to be able to tell the first iteration */
1032 toggle = 2;
1033
1034 /* see how much plain bits we can stuff into one packet
1035 * using RLE and VLI. */
1036 do {
1037 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1038 : _drbd_bm_find_next(mdev, c->bit_offset);
1039 if (tmp == -1UL)
1040 tmp = c->bm_bits;
1041 rl = tmp - c->bit_offset;
1042
1043 if (toggle == 2) { /* first iteration */
1044 if (rl == 0) {
1045 /* the first checked bit was set,
1046 * store start value, */
1047 DCBP_set_start(p, 1);
1048 /* but skip encoding of zero run length */
1049 toggle = !toggle;
1050 continue;
1051 }
1052 DCBP_set_start(p, 0);
1053 }
1054
1055 /* paranoia: catch zero runlength.
1056 * can only happen if bitmap is modified while we scan it. */
1057 if (rl == 0) {
1058 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1059 "t:%u bo:%lu\n", toggle, c->bit_offset);
1060 return -1;
1061 }
1062
1063 bits = vli_encode_bits(&bs, rl);
1064 if (bits == -ENOBUFS) /* buffer full */
1065 break;
1066 if (bits <= 0) {
1067 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1068 return 0;
1069 }
1070
1071 toggle = !toggle;
1072 plain_bits += rl;
1073 c->bit_offset = tmp;
1074 } while (c->bit_offset < c->bm_bits);
1075
1076 len = bs.cur.b - p->code + !!bs.cur.bit;
1077
1078 if (plain_bits < (len << 3)) {
1079 /* incompressible with this method.
1080 * we need to rewind both word and bit position. */
1081 c->bit_offset -= plain_bits;
1082 bm_xfer_ctx_bit_to_word_offset(c);
1083 c->bit_offset = c->word_offset * BITS_PER_LONG;
1084 return 0;
1085 }
1086
1087 /* RLE + VLI was able to compress it just fine.
1088 * update c->word_offset. */
1089 bm_xfer_ctx_bit_to_word_offset(c);
1090
1091 /* store pad_bits */
1092 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
1093
1094 return len;
1095}
1096
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001097/**
1098 * send_bitmap_rle_or_plain
1099 *
1100 * Return 0 when done, 1 when another iteration is needed, and a negative error
1101 * code upon failure.
1102 */
1103static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001105 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106{
1107 struct p_compressed_bm *p = (void*)h;
1108 unsigned long num_words;
1109 int len;
1110 int ok;
1111
1112 len = fill_bitmap_rle_bits(mdev, p, c);
1113
1114 if (len < 0)
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001115 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116
1117 if (len) {
1118 DCBP_set_code(p, RLE_VLI_Bits);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001119 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120 sizeof(*p) + len, 0);
1121
1122 c->packets[0]++;
1123 c->bytes[0] += sizeof(*p) + len;
1124
1125 if (c->bit_offset >= c->bm_bits)
1126 len = 0; /* DONE */
1127 } else {
1128 /* was not compressible.
1129 * send a buffer full of plain text bits instead. */
1130 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
1131 len = num_words * sizeof(long);
1132 if (len)
1133 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001134 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001135 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136 c->word_offset += num_words;
1137 c->bit_offset = c->word_offset * BITS_PER_LONG;
1138
1139 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02001140 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141
1142 if (c->bit_offset > c->bm_bits)
1143 c->bit_offset = c->bm_bits;
1144 }
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001145 if (ok) {
1146 if (len == 0) {
1147 INFO_bm_xfer_stats(mdev, "send", c);
1148 return 0;
1149 } else
1150 return 1;
1151 }
1152 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153}
1154
1155/* See the comment at receive_bitmap() */
1156int _drbd_send_bitmap(struct drbd_conf *mdev)
1157{
1158 struct bm_xfer_ctx c;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001159 struct p_header *p;
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001160 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001162 if (!expect(mdev->bitmap))
1163 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164
1165 /* maybe we should use some per thread scratch page,
1166 * and allocate that during initial device creation? */
Philipp Reisnerc0129492011-01-19 16:58:16 +01001167 p = (struct p_header *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001168 if (!p) {
1169 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001170 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001171 }
1172
1173 if (get_ldev(mdev)) {
1174 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1175 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
1176 drbd_bm_set_all(mdev);
1177 if (drbd_bm_write(mdev)) {
1178 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1179 * but otherwise process as per normal - need to tell other
1180 * side that a full resync is required! */
1181 dev_err(DEV, "Failed to write bitmap to disk!\n");
1182 } else {
1183 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
1184 drbd_md_sync(mdev);
1185 }
1186 }
1187 put_ldev(mdev);
1188 }
1189
1190 c = (struct bm_xfer_ctx) {
1191 .bm_bits = drbd_bm_bits(mdev),
1192 .bm_words = drbd_bm_words(mdev),
1193 };
1194
1195 do {
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001196 err = send_bitmap_rle_or_plain(mdev, p, &c);
1197 } while (err > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198
1199 free_page((unsigned long) p);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001200 return err == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201}
1202
1203int drbd_send_bitmap(struct drbd_conf *mdev)
1204{
1205 int err;
1206
Philipp Reisner61120872011-02-08 09:50:54 +01001207 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208 return -1;
1209 err = !_drbd_send_bitmap(mdev);
Philipp Reisner61120872011-02-08 09:50:54 +01001210 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211 return err;
1212}
1213
1214int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
1215{
1216 int ok;
1217 struct p_barrier_ack p;
1218
1219 p.barrier = barrier_nr;
1220 p.set_size = cpu_to_be32(set_size);
1221
1222 if (mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001223 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001224 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225 return ok;
1226}
1227
1228/**
1229 * _drbd_send_ack() - Sends an ack packet
1230 * @mdev: DRBD device.
1231 * @cmd: Packet command code.
1232 * @sector: sector, needs to be in big endian byte order
1233 * @blksize: size in byte, needs to be in big endian byte order
1234 * @block_id: Id, big endian byte order
1235 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001236static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
1237 u64 sector, u32 blksize, u64 block_id)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001238{
1239 int ok;
1240 struct p_block_ack p;
1241
1242 p.sector = sector;
1243 p.block_id = block_id;
1244 p.blksize = blksize;
1245 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
1246
Philipp Reisnere42325a2011-01-19 13:55:45 +01001247 if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001248 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001249 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001250 return ok;
1251}
1252
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001253/* dp->sector and dp->block_id already/still in network byte order,
1254 * data_size is payload size according to dp->head,
1255 * and may need to be corrected for digest size. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001256int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001257 struct p_data *dp, int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258{
Philipp Reisnera0638452011-01-19 14:31:32 +01001259 data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1260 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
1262 dp->block_id);
1263}
1264
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001265int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 struct p_block_req *rp)
1267{
1268 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
1269}
1270
1271/**
1272 * drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001273 * @mdev: DRBD device
1274 * @cmd: packet command code
1275 * @peer_req: peer request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001277int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001278 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279{
1280 return _drbd_send_ack(mdev, cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001281 cpu_to_be64(peer_req->i.sector),
1282 cpu_to_be32(peer_req->i.size),
1283 peer_req->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001284}
1285
1286/* This function misuses the block_id field to signal if the blocks
1287 * are is sync or not. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001288int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289 sector_t sector, int blksize, u64 block_id)
1290{
1291 return _drbd_send_ack(mdev, cmd,
1292 cpu_to_be64(sector),
1293 cpu_to_be32(blksize),
1294 cpu_to_be64(block_id));
1295}
1296
1297int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1298 sector_t sector, int size, u64 block_id)
1299{
1300 int ok;
1301 struct p_block_req p;
1302
1303 p.sector = cpu_to_be64(sector);
1304 p.block_id = block_id;
1305 p.blksize = cpu_to_be32(size);
1306
Philipp Reisnerc0129492011-01-19 16:58:16 +01001307 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308 return ok;
1309}
1310
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001311int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
1312 void *digest, int digest_size, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313{
1314 int ok;
1315 struct p_block_req p;
1316
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001317 prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001318 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001319 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320 p.blksize = cpu_to_be32(size);
1321
Philipp Reisnere42325a2011-01-19 13:55:45 +01001322 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001323
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001324 ok = (sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0));
1325 ok = ok && (digest_size == drbd_send(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001326
Philipp Reisnere42325a2011-01-19 13:55:45 +01001327 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328
1329 return ok;
1330}
1331
1332int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
1333{
1334 int ok;
1335 struct p_block_req p;
1336
1337 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001338 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339 p.blksize = cpu_to_be32(size);
1340
Philipp Reisnerc0129492011-01-19 16:58:16 +01001341 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001342 return ok;
1343}
1344
1345/* called on sndtimeo
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001346 * returns false if we should retry,
1347 * true if we think connection is dead
Philipp Reisnerb411b362009-09-25 16:07:19 -07001348 */
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001349static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001350{
1351 int drop_it;
1352 /* long elapsed = (long)(jiffies - mdev->last_received); */
1353
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001354 drop_it = tconn->meta.socket == sock
1355 || !tconn->asender.task
1356 || get_t_state(&tconn->asender) != RUNNING
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001357 || tconn->cstate < C_WF_REPORT_PARAMS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358
1359 if (drop_it)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001360 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001362 drop_it = !--tconn->ko_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001363 if (!drop_it) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001364 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1365 current->comm, current->pid, tconn->ko_count);
1366 request_ping(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001367 }
1368
1369 return drop_it; /* && (mdev->state == R_PRIMARY) */;
1370}
1371
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001372static void drbd_update_congested(struct drbd_tconn *tconn)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001373{
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001374 struct sock *sk = tconn->data.socket->sk;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001375 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001376 set_bit(NET_CONGESTED, &tconn->flags);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001377}
1378
Philipp Reisnerb411b362009-09-25 16:07:19 -07001379/* The idea of sendpage seems to be to put some kind of reference
1380 * to the page into the skb, and to hand it over to the NIC. In
1381 * this process get_page() gets called.
1382 *
1383 * As soon as the page was really sent over the network put_page()
1384 * gets called by some part of the network layer. [ NIC driver? ]
1385 *
1386 * [ get_page() / put_page() increment/decrement the count. If count
1387 * reaches 0 the page will be freed. ]
1388 *
1389 * This works nicely with pages from FSs.
1390 * But this means that in protocol A we might signal IO completion too early!
1391 *
1392 * In order not to corrupt data during a resync we must make sure
1393 * that we do not reuse our own buffer pages (EEs) to early, therefore
1394 * we have the net_ee list.
1395 *
1396 * XFS seems to have problems, still, it submits pages with page_count == 0!
1397 * As a workaround, we disable sendpage on pages
1398 * with page_count == 0 or PageSlab.
1399 */
1400static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001401 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402{
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001403 int sent = drbd_send(mdev->tconn, mdev->tconn->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404 kunmap(page);
1405 if (sent == size)
1406 mdev->send_cnt += size>>9;
1407 return sent == size;
1408}
1409
1410static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001411 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001412{
1413 mm_segment_t oldfs = get_fs();
1414 int sent, ok;
1415 int len = size;
1416
1417 /* e.g. XFS meta- & log-data is in slab pages, which have a
1418 * page_count of 0 and/or have PageSlab() set.
1419 * we cannot use send_page for those, as that does get_page();
1420 * put_page(); and would cause either a VM_BUG directly, or
1421 * __page_cache_release a page that would actually still be referenced
1422 * by someone, leading to some obscure delayed Oops somewhere else. */
1423 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001424 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001425
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001426 msg_flags |= MSG_NOSIGNAL;
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001427 drbd_update_congested(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428 set_fs(KERNEL_DS);
1429 do {
Philipp Reisnere42325a2011-01-19 13:55:45 +01001430 sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001432 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001433 if (sent == -EAGAIN) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001434 if (we_should_drop_the_connection(mdev->tconn,
Philipp Reisnere42325a2011-01-19 13:55:45 +01001435 mdev->tconn->data.socket))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001436 break;
1437 else
1438 continue;
1439 }
1440 if (sent <= 0) {
1441 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1442 __func__, (int)size, len, sent);
1443 break;
1444 }
1445 len -= sent;
1446 offset += sent;
1447 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
1448 set_fs(oldfs);
Philipp Reisner01a311a2011-02-07 14:30:33 +01001449 clear_bit(NET_CONGESTED, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001450
1451 ok = (len == 0);
1452 if (likely(ok))
1453 mdev->send_cnt += size>>9;
1454 return ok;
1455}
1456
1457static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
1458{
1459 struct bio_vec *bvec;
1460 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001461 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462 __bio_for_each_segment(bvec, bio, i, 0) {
1463 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001464 bvec->bv_offset, bvec->bv_len,
1465 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466 return 0;
1467 }
1468 return 1;
1469}
1470
1471static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
1472{
1473 struct bio_vec *bvec;
1474 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001475 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001476 __bio_for_each_segment(bvec, bio, i, 0) {
1477 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001478 bvec->bv_offset, bvec->bv_len,
1479 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 return 0;
1481 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 return 1;
1483}
1484
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001485static int _drbd_send_zc_ee(struct drbd_conf *mdev,
1486 struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001487{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001488 struct page *page = peer_req->pages;
1489 unsigned len = peer_req->i.size;
1490
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001491 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001492 page_chain_for_each(page) {
1493 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001494 if (!_drbd_send_page(mdev, page, 0, l,
1495 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001496 return 0;
1497 len -= l;
1498 }
1499 return 1;
1500}
1501
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001502static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
1503{
Philipp Reisner31890f42011-01-19 14:12:51 +01001504 if (mdev->tconn->agreed_pro_version >= 95)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001505 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001506 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1507 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1508 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1509 else
Jens Axboe721a9602011-03-09 11:56:30 +01001510 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001511}
1512
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513/* Used to send write requests
1514 * R_PRIMARY -> Peer (P_DATA)
1515 */
1516int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
1517{
1518 int ok = 1;
1519 struct p_data p;
1520 unsigned int dp_flags = 0;
1521 void *dgb;
1522 int dgs;
1523
Philipp Reisner61120872011-02-08 09:50:54 +01001524 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525 return 0;
1526
Philipp Reisnera0638452011-01-19 14:31:32 +01001527 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1528 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001530 prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size);
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001531 p.sector = cpu_to_be64(req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 p.block_id = (unsigned long)req;
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001533 p.seq_num = cpu_to_be32(req->seq_num = atomic_add_return(1, &mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001535 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
1536
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 if (mdev->state.conn >= C_SYNC_SOURCE &&
1538 mdev->state.conn <= C_PAUSED_SYNC_T)
1539 dp_flags |= DP_MAY_SET_IN_SYNC;
1540
1541 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 set_bit(UNPLUG_REMOTE, &mdev->flags);
1543 ok = (sizeof(p) ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001544 drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001545 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001546 dgb = mdev->tconn->int_dig_out;
1547 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001548 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001549 }
1550 if (ok) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001551 /* For protocol A, we have to memcpy the payload into
1552 * socket buffers, as we may complete right away
1553 * as soon as we handed it over to tcp, at which point the data
1554 * pages may become invalid.
1555 *
1556 * For data-integrity enabled, we copy it as well, so we can be
1557 * sure that even if the bio pages may still be modified, it
1558 * won't change the data on the wire, thus if the digest checks
1559 * out ok after sending on this side, but does not fit on the
1560 * receiving side, we sure have detected corruption elsewhere.
1561 */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001562 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001563 ok = _drbd_send_bio(mdev, req->master_bio);
1564 else
1565 ok = _drbd_send_zc_bio(mdev, req->master_bio);
Lars Ellenberg470be442010-11-10 10:36:52 +01001566
1567 /* double check digest, sometimes buffers have been modified in flight. */
1568 if (dgs > 0 && dgs <= 64) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001569 /* 64 byte, 512 bit, is the largest digest size
Lars Ellenberg470be442010-11-10 10:36:52 +01001570 * currently supported in kernel crypto. */
1571 unsigned char digest[64];
Philipp Reisnera0638452011-01-19 14:31:32 +01001572 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest);
1573 if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001574 dev_warn(DEV,
1575 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001576 (unsigned long long)req->i.sector, req->i.size);
Lars Ellenberg470be442010-11-10 10:36:52 +01001577 }
1578 } /* else if (dgs > 64) {
1579 ... Be noisy about digest too large ...
1580 } */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581 }
1582
Philipp Reisner61120872011-02-08 09:50:54 +01001583 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc2010-05-04 12:33:58 +02001584
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 return ok;
1586}
1587
1588/* answer packet, used to send data back for read requests:
1589 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1590 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1591 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001592int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001593 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594{
1595 int ok;
1596 struct p_data p;
1597 void *dgb;
1598 int dgs;
1599
Philipp Reisnera0638452011-01-19 14:31:32 +01001600 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1601 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001603 prepare_header(mdev, &p.head, cmd, sizeof(p) -
1604 sizeof(struct p_header80) +
1605 dgs + peer_req->i.size);
1606 p.sector = cpu_to_be64(peer_req->i.sector);
1607 p.block_id = peer_req->block_id;
Andreas Gruenbachercc378272011-01-26 18:01:50 +01001608 p.seq_num = 0; /* unused */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609
1610 /* Only called by our kernel thread.
1611 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
1612 * in response to admin command or module unload.
1613 */
Philipp Reisner61120872011-02-08 09:50:54 +01001614 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615 return 0;
1616
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001617 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 -07001618 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001619 dgb = mdev->tconn->int_dig_out;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001620 drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001621 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622 }
1623 if (ok)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001624 ok = _drbd_send_zc_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625
Philipp Reisner61120872011-02-08 09:50:54 +01001626 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc2010-05-04 12:33:58 +02001627
Philipp Reisnerb411b362009-09-25 16:07:19 -07001628 return ok;
1629}
1630
Philipp Reisner73a01a12010-10-27 14:33:00 +02001631int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
1632{
1633 struct p_block_desc p;
1634
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001635 p.sector = cpu_to_be64(req->i.sector);
1636 p.blksize = cpu_to_be32(req->i.size);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001637
1638 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p));
1639}
1640
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641/*
1642 drbd_send distinguishes two cases:
1643
1644 Packets sent via the data socket "sock"
1645 and packets sent via the meta data socket "msock"
1646
1647 sock msock
1648 -----------------+-------------------------+------------------------------
1649 timeout conf.timeout / 2 conf.timeout / 2
1650 timeout action send a ping via msock Abort communication
1651 and close all sockets
1652*/
1653
1654/*
1655 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1656 */
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001657int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001658 void *buf, size_t size, unsigned msg_flags)
1659{
1660 struct kvec iov;
1661 struct msghdr msg;
1662 int rv, sent = 0;
1663
1664 if (!sock)
1665 return -1000;
1666
1667 /* THINK if (signal_pending) return ... ? */
1668
1669 iov.iov_base = buf;
1670 iov.iov_len = size;
1671
1672 msg.msg_name = NULL;
1673 msg.msg_namelen = 0;
1674 msg.msg_control = NULL;
1675 msg.msg_controllen = 0;
1676 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1677
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001678 if (sock == tconn->data.socket) {
1679 tconn->ko_count = tconn->net_conf->ko_count;
1680 drbd_update_congested(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681 }
1682 do {
1683 /* STRANGE
1684 * tcp_sendmsg does _not_ use its size parameter at all ?
1685 *
1686 * -EAGAIN on timeout, -EINTR on signal.
1687 */
1688/* THINK
1689 * do we need to block DRBD_SIG if sock == &meta.socket ??
1690 * otherwise wake_asender() might interrupt some send_*Ack !
1691 */
1692 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1693 if (rv == -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001694 if (we_should_drop_the_connection(tconn, sock))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695 break;
1696 else
1697 continue;
1698 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699 if (rv == -EINTR) {
1700 flush_signals(current);
1701 rv = 0;
1702 }
1703 if (rv < 0)
1704 break;
1705 sent += rv;
1706 iov.iov_base += rv;
1707 iov.iov_len -= rv;
1708 } while (sent < size);
1709
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001710 if (sock == tconn->data.socket)
1711 clear_bit(NET_CONGESTED, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712
1713 if (rv <= 0) {
1714 if (rv != -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001715 conn_err(tconn, "%s_sendmsg returned %d\n",
1716 sock == tconn->meta.socket ? "msock" : "sock",
1717 rv);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001718 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719 } else
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001720 conn_request_state(tconn, NS(conn, C_TIMEOUT), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001721 }
1722
1723 return sent;
1724}
1725
1726static int drbd_open(struct block_device *bdev, fmode_t mode)
1727{
1728 struct drbd_conf *mdev = bdev->bd_disk->private_data;
1729 unsigned long flags;
1730 int rv = 0;
1731
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001732 mutex_lock(&drbd_main_mutex);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001733 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734 /* to have a stable mdev->state.role
1735 * and no race with updating open_cnt */
1736
1737 if (mdev->state.role != R_PRIMARY) {
1738 if (mode & FMODE_WRITE)
1739 rv = -EROFS;
1740 else if (!allow_oos)
1741 rv = -EMEDIUMTYPE;
1742 }
1743
1744 if (!rv)
1745 mdev->open_cnt++;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001746 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001747 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748
1749 return rv;
1750}
1751
1752static int drbd_release(struct gendisk *gd, fmode_t mode)
1753{
1754 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001755 mutex_lock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001756 mdev->open_cnt--;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001757 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001758 return 0;
1759}
1760
Philipp Reisnerb411b362009-09-25 16:07:19 -07001761static void drbd_set_defaults(struct drbd_conf *mdev)
1762{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001763 /* This way we get a compile error when sync_conf grows,
1764 and we forgot to initialize it here */
1765 mdev->sync_conf = (struct syncer_conf) {
1766 /* .rate = */ DRBD_RATE_DEF,
1767 /* .after = */ DRBD_AFTER_DEF,
1768 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001769 /* .verify_alg = */ {}, 0,
1770 /* .cpu_mask = */ {}, 0,
1771 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02001772 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02001773 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
1774 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
1775 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
1776 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001777 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
1778 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001779 };
1780
1781 /* Have to use that way, because the layout differs between
1782 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783 mdev->state = (union drbd_state) {
1784 { .role = R_SECONDARY,
1785 .peer = R_UNKNOWN,
1786 .conn = C_STANDALONE,
1787 .disk = D_DISKLESS,
1788 .pdsk = D_UNKNOWN,
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001789 .susp = 0,
1790 .susp_nod = 0,
1791 .susp_fen = 0
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792 } };
1793}
1794
1795void drbd_init_set_defaults(struct drbd_conf *mdev)
1796{
1797 /* the memset(,0,) did most of this.
1798 * note: only assignments, no allocation in here */
1799
1800 drbd_set_defaults(mdev);
1801
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802 atomic_set(&mdev->ap_bio_cnt, 0);
1803 atomic_set(&mdev->ap_pending_cnt, 0);
1804 atomic_set(&mdev->rs_pending_cnt, 0);
1805 atomic_set(&mdev->unacked_cnt, 0);
1806 atomic_set(&mdev->local_cnt, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001807 atomic_set(&mdev->pp_in_use, 0);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001808 atomic_set(&mdev->pp_in_use_by_net, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02001809 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001810 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001811 atomic_set(&mdev->ap_in_flight, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001812
1813 mutex_init(&mdev->md_io_mutex);
Philipp Reisner8410da82011-02-11 20:11:10 +01001814 mutex_init(&mdev->own_state_mutex);
1815 mdev->state_mutex = &mdev->own_state_mutex;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816
Philipp Reisnerb411b362009-09-25 16:07:19 -07001817 spin_lock_init(&mdev->al_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 Reisnerb411b362009-09-25 16:07:19 -07001827 INIT_LIST_HEAD(&mdev->resync_work.list);
1828 INIT_LIST_HEAD(&mdev->unplug_work.list);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001829 INIT_LIST_HEAD(&mdev->go_diskless.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001830 INIT_LIST_HEAD(&mdev->md_sync_work.list);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001831 INIT_LIST_HEAD(&mdev->start_resync_work.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02001833
Philipp Reisner794abb72010-12-27 11:51:23 +01001834 mdev->resync_work.cb = w_resync_timer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001835 mdev->unplug_work.cb = w_send_write_hint;
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001836 mdev->go_diskless.cb = w_go_diskless;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837 mdev->md_sync_work.cb = w_md_sync;
1838 mdev->bm_io_work.w.cb = w_bitmap_io;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001839 mdev->start_resync_work.cb = w_start_resync;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001840
1841 mdev->resync_work.mdev = mdev;
1842 mdev->unplug_work.mdev = mdev;
1843 mdev->go_diskless.mdev = mdev;
1844 mdev->md_sync_work.mdev = mdev;
1845 mdev->bm_io_work.w.mdev = mdev;
1846 mdev->start_resync_work.mdev = mdev;
1847
Philipp Reisnerb411b362009-09-25 16:07:19 -07001848 init_timer(&mdev->resync_timer);
1849 init_timer(&mdev->md_sync_timer);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001850 init_timer(&mdev->start_resync_timer);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001851 init_timer(&mdev->request_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001852 mdev->resync_timer.function = resync_timer_fn;
1853 mdev->resync_timer.data = (unsigned long) mdev;
1854 mdev->md_sync_timer.function = md_sync_timer_fn;
1855 mdev->md_sync_timer.data = (unsigned long) mdev;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001856 mdev->start_resync_timer.function = start_resync_timer_fn;
1857 mdev->start_resync_timer.data = (unsigned long) mdev;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001858 mdev->request_timer.function = request_timer_fn;
1859 mdev->request_timer.data = (unsigned long) mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860
1861 init_waitqueue_head(&mdev->misc_wait);
1862 init_waitqueue_head(&mdev->state_wait);
1863 init_waitqueue_head(&mdev->ee_wait);
1864 init_waitqueue_head(&mdev->al_wait);
1865 init_waitqueue_head(&mdev->seq_wait);
1866
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001867 /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */
Philipp Reisner2451fc32010-08-24 13:43:11 +02001868 mdev->write_ordering = WO_bdev_flush;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001869 mdev->resync_wenr = LC_FREE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001870 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1871 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872}
1873
1874void drbd_mdev_cleanup(struct drbd_conf *mdev)
1875{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001876 int i;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001877 if (mdev->tconn->receiver.t_state != NONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001879 mdev->tconn->receiver.t_state);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001880
1881 /* no need to lock it, I'm the only thread alive */
1882 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
1883 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
1884 mdev->al_writ_cnt =
1885 mdev->bm_writ_cnt =
1886 mdev->read_cnt =
1887 mdev->recv_cnt =
1888 mdev->send_cnt =
1889 mdev->writ_cnt =
1890 mdev->p_size =
1891 mdev->rs_start =
1892 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001893 mdev->rs_failed = 0;
1894 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001895 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001896 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1897 mdev->rs_mark_left[i] = 0;
1898 mdev->rs_mark_time[i] = 0;
1899 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01001900 D_ASSERT(mdev->tconn->net_conf == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001901
1902 drbd_set_my_capacity(mdev, 0);
1903 if (mdev->bitmap) {
1904 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01001905 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001906 drbd_bm_cleanup(mdev);
1907 }
1908
1909 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02001910 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001911
1912 /*
1913 * currently we drbd_init_ee only on module load, so
1914 * we may do drbd_release_ee only on module unload!
1915 */
1916 D_ASSERT(list_empty(&mdev->active_ee));
1917 D_ASSERT(list_empty(&mdev->sync_ee));
1918 D_ASSERT(list_empty(&mdev->done_ee));
1919 D_ASSERT(list_empty(&mdev->read_ee));
1920 D_ASSERT(list_empty(&mdev->net_ee));
1921 D_ASSERT(list_empty(&mdev->resync_reads));
Philipp Reisnere42325a2011-01-19 13:55:45 +01001922 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
1923 D_ASSERT(list_empty(&mdev->tconn->meta.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001924 D_ASSERT(list_empty(&mdev->resync_work.list));
1925 D_ASSERT(list_empty(&mdev->unplug_work.list));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001926 D_ASSERT(list_empty(&mdev->go_diskless.list));
Lars Ellenberg2265b472010-12-16 15:41:26 +01001927
1928 drbd_set_defaults(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001929}
1930
1931
1932static void drbd_destroy_mempools(void)
1933{
1934 struct page *page;
1935
1936 while (drbd_pp_pool) {
1937 page = drbd_pp_pool;
1938 drbd_pp_pool = (struct page *)page_private(page);
1939 __free_page(page);
1940 drbd_pp_vacant--;
1941 }
1942
1943 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
1944
1945 if (drbd_ee_mempool)
1946 mempool_destroy(drbd_ee_mempool);
1947 if (drbd_request_mempool)
1948 mempool_destroy(drbd_request_mempool);
1949 if (drbd_ee_cache)
1950 kmem_cache_destroy(drbd_ee_cache);
1951 if (drbd_request_cache)
1952 kmem_cache_destroy(drbd_request_cache);
1953 if (drbd_bm_ext_cache)
1954 kmem_cache_destroy(drbd_bm_ext_cache);
1955 if (drbd_al_ext_cache)
1956 kmem_cache_destroy(drbd_al_ext_cache);
1957
1958 drbd_ee_mempool = NULL;
1959 drbd_request_mempool = NULL;
1960 drbd_ee_cache = NULL;
1961 drbd_request_cache = NULL;
1962 drbd_bm_ext_cache = NULL;
1963 drbd_al_ext_cache = NULL;
1964
1965 return;
1966}
1967
1968static int drbd_create_mempools(void)
1969{
1970 struct page *page;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001971 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001972 int i;
1973
1974 /* prepare our caches and mempools */
1975 drbd_request_mempool = NULL;
1976 drbd_ee_cache = NULL;
1977 drbd_request_cache = NULL;
1978 drbd_bm_ext_cache = NULL;
1979 drbd_al_ext_cache = NULL;
1980 drbd_pp_pool = NULL;
1981
1982 /* caches */
1983 drbd_request_cache = kmem_cache_create(
1984 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
1985 if (drbd_request_cache == NULL)
1986 goto Enomem;
1987
1988 drbd_ee_cache = kmem_cache_create(
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001989 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001990 if (drbd_ee_cache == NULL)
1991 goto Enomem;
1992
1993 drbd_bm_ext_cache = kmem_cache_create(
1994 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
1995 if (drbd_bm_ext_cache == NULL)
1996 goto Enomem;
1997
1998 drbd_al_ext_cache = kmem_cache_create(
1999 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2000 if (drbd_al_ext_cache == NULL)
2001 goto Enomem;
2002
2003 /* mempools */
2004 drbd_request_mempool = mempool_create(number,
2005 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2006 if (drbd_request_mempool == NULL)
2007 goto Enomem;
2008
2009 drbd_ee_mempool = mempool_create(number,
2010 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
Nicolas Kaiser2027ae12010-10-28 06:15:26 -06002011 if (drbd_ee_mempool == NULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002012 goto Enomem;
2013
2014 /* drbd's page pool */
2015 spin_lock_init(&drbd_pp_lock);
2016
2017 for (i = 0; i < number; i++) {
2018 page = alloc_page(GFP_HIGHUSER);
2019 if (!page)
2020 goto Enomem;
2021 set_page_private(page, (unsigned long)drbd_pp_pool);
2022 drbd_pp_pool = page;
2023 }
2024 drbd_pp_vacant = number;
2025
2026 return 0;
2027
2028Enomem:
2029 drbd_destroy_mempools(); /* in case we allocated some */
2030 return -ENOMEM;
2031}
2032
2033static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2034 void *unused)
2035{
2036 /* just so we have it. you never know what interesting things we
2037 * might want to do here some day...
2038 */
2039
2040 return NOTIFY_DONE;
2041}
2042
2043static struct notifier_block drbd_notifier = {
2044 .notifier_call = drbd_notify_sys,
2045};
2046
2047static void drbd_release_ee_lists(struct drbd_conf *mdev)
2048{
2049 int rr;
2050
2051 rr = drbd_release_ee(mdev, &mdev->active_ee);
2052 if (rr)
2053 dev_err(DEV, "%d EEs in active list found!\n", rr);
2054
2055 rr = drbd_release_ee(mdev, &mdev->sync_ee);
2056 if (rr)
2057 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2058
2059 rr = drbd_release_ee(mdev, &mdev->read_ee);
2060 if (rr)
2061 dev_err(DEV, "%d EEs in read list found!\n", rr);
2062
2063 rr = drbd_release_ee(mdev, &mdev->done_ee);
2064 if (rr)
2065 dev_err(DEV, "%d EEs in done list found!\n", rr);
2066
2067 rr = drbd_release_ee(mdev, &mdev->net_ee);
2068 if (rr)
2069 dev_err(DEV, "%d EEs in net list found!\n", rr);
2070}
2071
2072/* caution. no locking.
2073 * currently only used from module cleanup code. */
2074static void drbd_delete_device(unsigned int minor)
2075{
2076 struct drbd_conf *mdev = minor_to_mdev(minor);
2077
2078 if (!mdev)
2079 return;
2080
2081 /* paranoia asserts */
Andreas Gruenbacher70dc65e2010-12-21 14:46:57 +01002082 D_ASSERT(mdev->open_cnt == 0);
Philipp Reisnere42325a2011-01-19 13:55:45 +01002083 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002084 /* end paranoia asserts */
2085
2086 del_gendisk(mdev->vdisk);
2087
2088 /* cleanup stuff that may have been allocated during
2089 * device (re-)configuration or state changes */
2090
2091 if (mdev->this_bdev)
2092 bdput(mdev->this_bdev);
2093
2094 drbd_free_resources(mdev);
Philipp Reisner21114382011-01-19 12:26:59 +01002095 drbd_free_tconn(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002096
2097 drbd_release_ee_lists(mdev);
2098
Philipp Reisnerb411b362009-09-25 16:07:19 -07002099 lc_destroy(mdev->act_log);
2100 lc_destroy(mdev->resync);
2101
2102 kfree(mdev->p_uuid);
2103 /* mdev->p_uuid = NULL; */
2104
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 /* cleanup the rest that has been
2106 * allocated from drbd_new_device
2107 * and actually free the mdev itself */
2108 drbd_free_mdev(mdev);
2109}
2110
2111static void drbd_cleanup(void)
2112{
2113 unsigned int i;
2114
2115 unregister_reboot_notifier(&drbd_notifier);
2116
Lars Ellenberg17a93f32010-11-24 10:37:35 +01002117 /* first remove proc,
2118 * drbdsetup uses it's presence to detect
2119 * whether DRBD is loaded.
2120 * If we would get stuck in proc removal,
2121 * but have netlink already deregistered,
2122 * some drbdsetup commands may wait forever
2123 * for an answer.
2124 */
2125 if (drbd_proc)
2126 remove_proc_entry("drbd", NULL);
2127
Philipp Reisnerb411b362009-09-25 16:07:19 -07002128 drbd_nl_cleanup();
2129
2130 if (minor_table) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002131 i = minor_count;
2132 while (i--)
2133 drbd_delete_device(i);
2134 drbd_destroy_mempools();
2135 }
2136
2137 kfree(minor_table);
2138
2139 unregister_blkdev(DRBD_MAJOR, "drbd");
2140
2141 printk(KERN_INFO "drbd: module cleanup done.\n");
2142}
2143
2144/**
2145 * drbd_congested() - Callback for pdflush
2146 * @congested_data: User data
2147 * @bdi_bits: Bits pdflush is currently interested in
2148 *
2149 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2150 */
2151static int drbd_congested(void *congested_data, int bdi_bits)
2152{
2153 struct drbd_conf *mdev = congested_data;
2154 struct request_queue *q;
2155 char reason = '-';
2156 int r = 0;
2157
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002158 if (!may_inc_ap_bio(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159 /* DRBD has frozen IO */
2160 r = bdi_bits;
2161 reason = 'd';
2162 goto out;
2163 }
2164
2165 if (get_ldev(mdev)) {
2166 q = bdev_get_queue(mdev->ldev->backing_bdev);
2167 r = bdi_congested(&q->backing_dev_info, bdi_bits);
2168 put_ldev(mdev);
2169 if (r)
2170 reason = 'b';
2171 }
2172
Philipp Reisner01a311a2011-02-07 14:30:33 +01002173 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002174 r |= (1 << BDI_async_congested);
2175 reason = reason == 'b' ? 'a' : 'n';
2176 }
2177
2178out:
2179 mdev->congestion_reason = reason;
2180 return r;
2181}
2182
Philipp Reisner6699b652011-02-09 11:10:24 +01002183static void drbd_init_workqueue(struct drbd_work_queue* wq)
2184{
2185 sema_init(&wq->s, 0);
2186 spin_lock_init(&wq->q_lock);
2187 INIT_LIST_HEAD(&wq->q);
2188}
2189
Philipp Reisner21114382011-01-19 12:26:59 +01002190struct drbd_tconn *drbd_new_tconn(char *name)
2191{
2192 struct drbd_tconn *tconn;
2193
2194 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2195 if (!tconn)
2196 return NULL;
2197
2198 tconn->name = kstrdup(name, GFP_KERNEL);
2199 if (!tconn->name)
2200 goto fail;
2201
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002202 tconn->cstate = C_STANDALONE;
Philipp Reisner8410da82011-02-11 20:11:10 +01002203 mutex_init(&tconn->cstate_mutex);
Philipp Reisner6699b652011-02-09 11:10:24 +01002204 spin_lock_init(&tconn->req_lock);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002205 atomic_set(&tconn->net_cnt, 0);
2206 init_waitqueue_head(&tconn->net_cnt_wait);
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01002207 init_waitqueue_head(&tconn->ping_wait);
Philipp Reisner062e8792011-02-08 11:09:18 +01002208 idr_init(&tconn->volumes);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002209
Philipp Reisner6699b652011-02-09 11:10:24 +01002210 drbd_init_workqueue(&tconn->data.work);
2211 mutex_init(&tconn->data.mutex);
2212
2213 drbd_init_workqueue(&tconn->meta.work);
2214 mutex_init(&tconn->meta.mutex);
2215
Philipp Reisner392c8802011-02-09 10:33:31 +01002216 drbd_thread_init(tconn, &tconn->receiver, drbdd_init, "receiver");
2217 drbd_thread_init(tconn, &tconn->worker, drbd_worker, "worker");
2218 drbd_thread_init(tconn, &tconn->asender, drbd_asender, "asender");
2219
Philipp Reisner21114382011-01-19 12:26:59 +01002220 write_lock_irq(&global_state_lock);
2221 list_add(&tconn->all_tconn, &drbd_tconns);
2222 write_unlock_irq(&global_state_lock);
2223
2224 return tconn;
2225
2226fail:
2227 kfree(tconn->name);
2228 kfree(tconn);
2229
2230 return NULL;
2231}
2232
2233void drbd_free_tconn(struct drbd_tconn *tconn)
2234{
2235 write_lock_irq(&global_state_lock);
2236 list_del(&tconn->all_tconn);
2237 write_unlock_irq(&global_state_lock);
Philipp Reisner062e8792011-02-08 11:09:18 +01002238 idr_destroy(&tconn->volumes);
Philipp Reisner21114382011-01-19 12:26:59 +01002239
2240 kfree(tconn->name);
Philipp Reisnerb42a70a2011-01-27 10:55:20 +01002241 kfree(tconn->int_dig_out);
2242 kfree(tconn->int_dig_in);
2243 kfree(tconn->int_dig_vv);
Philipp Reisner21114382011-01-19 12:26:59 +01002244 kfree(tconn);
2245}
2246
Philipp Reisnerb411b362009-09-25 16:07:19 -07002247struct drbd_conf *drbd_new_device(unsigned int minor)
2248{
2249 struct drbd_conf *mdev;
2250 struct gendisk *disk;
2251 struct request_queue *q;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002252 char conn_name[9]; /* drbd1234N */
Philipp Reisner062e8792011-02-08 11:09:18 +01002253 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002254
2255 /* GFP_KERNEL, we are outside of all write-out paths */
2256 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
2257 if (!mdev)
2258 return NULL;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002259 sprintf(conn_name, "drbd%d", minor);
2260 mdev->tconn = drbd_new_tconn(conn_name);
Philipp Reisner21114382011-01-19 12:26:59 +01002261 if (!mdev->tconn)
2262 goto out_no_tconn;
Philipp Reisner062e8792011-02-08 11:09:18 +01002263 if (!idr_pre_get(&mdev->tconn->volumes, GFP_KERNEL))
2264 goto out_no_cpumask;
2265 if (idr_get_new(&mdev->tconn->volumes, mdev, &vnr))
2266 goto out_no_cpumask;
2267 if (vnr != 0) {
2268 dev_err(DEV, "vnr = %d\n", vnr);
2269 goto out_no_cpumask;
2270 }
Philipp Reisner80822282011-02-08 12:46:30 +01002271 if (!zalloc_cpumask_var(&mdev->tconn->cpu_mask, GFP_KERNEL))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272 goto out_no_cpumask;
2273
2274 mdev->minor = minor;
2275
2276 drbd_init_set_defaults(mdev);
2277
2278 q = blk_alloc_queue(GFP_KERNEL);
2279 if (!q)
2280 goto out_no_q;
2281 mdev->rq_queue = q;
2282 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002283
2284 disk = alloc_disk(1);
2285 if (!disk)
2286 goto out_no_disk;
2287 mdev->vdisk = disk;
2288
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002289 set_disk_ro(disk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002290
2291 disk->queue = q;
2292 disk->major = DRBD_MAJOR;
2293 disk->first_minor = minor;
2294 disk->fops = &drbd_ops;
2295 sprintf(disk->disk_name, "drbd%d", minor);
2296 disk->private_data = mdev;
2297
2298 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
2299 /* we have no partitions. we contain only ourselves. */
2300 mdev->this_bdev->bd_contains = mdev->this_bdev;
2301
2302 q->backing_dev_info.congested_fn = drbd_congested;
2303 q->backing_dev_info.congested_data = mdev;
2304
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01002305 blk_queue_make_request(q, drbd_make_request);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002306 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2307 This triggers a max_bio_size message upon first attach or connect */
2308 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002309 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2310 blk_queue_merge_bvec(q, drbd_merge_bvec);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002311 q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002312
2313 mdev->md_io_page = alloc_page(GFP_KERNEL);
2314 if (!mdev->md_io_page)
2315 goto out_no_io_page;
2316
2317 if (drbd_bm_init(mdev))
2318 goto out_no_bitmap;
2319 /* no need to lock access, we are still initializing this minor device. */
2320 if (!tl_init(mdev))
2321 goto out_no_tl;
Andreas Gruenbacherdac13892011-01-21 17:18:39 +01002322 mdev->read_requests = RB_ROOT;
Andreas Gruenbacherde696712011-01-20 15:00:24 +01002323 mdev->write_requests = RB_ROOT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002324
Philipp Reisnerb411b362009-09-25 16:07:19 -07002325 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2326 if (!mdev->current_epoch)
2327 goto out_no_epoch;
2328
2329 INIT_LIST_HEAD(&mdev->current_epoch->list);
2330 mdev->epochs = 1;
2331
2332 return mdev;
2333
2334/* out_whatever_else:
2335 kfree(mdev->current_epoch); */
2336out_no_epoch:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002337 tl_cleanup(mdev);
2338out_no_tl:
2339 drbd_bm_cleanup(mdev);
2340out_no_bitmap:
2341 __free_page(mdev->md_io_page);
2342out_no_io_page:
2343 put_disk(disk);
2344out_no_disk:
2345 blk_cleanup_queue(q);
2346out_no_q:
Philipp Reisner80822282011-02-08 12:46:30 +01002347 free_cpumask_var(mdev->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002348out_no_cpumask:
Philipp Reisner21114382011-01-19 12:26:59 +01002349 drbd_free_tconn(mdev->tconn);
2350out_no_tconn:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002351 kfree(mdev);
2352 return NULL;
2353}
2354
2355/* counterpart of drbd_new_device.
2356 * last part of drbd_delete_device. */
2357void drbd_free_mdev(struct drbd_conf *mdev)
2358{
2359 kfree(mdev->current_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002360 tl_cleanup(mdev);
2361 if (mdev->bitmap) /* should no longer be there. */
2362 drbd_bm_cleanup(mdev);
2363 __free_page(mdev->md_io_page);
2364 put_disk(mdev->vdisk);
2365 blk_cleanup_queue(mdev->rq_queue);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 kfree(mdev);
2367}
2368
2369
2370int __init drbd_init(void)
2371{
2372 int err;
2373
Philipp Reisnerfd340c12011-01-19 16:57:39 +01002374 BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95));
2375 BUILD_BUG_ON(sizeof(struct p_handshake) != 80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002376
Philipp Reisner2b8a90b2011-01-10 11:15:17 +01002377 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378 printk(KERN_ERR
2379 "drbd: invalid minor_count (%d)\n", minor_count);
2380#ifdef MODULE
2381 return -EINVAL;
2382#else
2383 minor_count = 8;
2384#endif
2385 }
2386
2387 err = drbd_nl_init();
2388 if (err)
2389 return err;
2390
2391 err = register_blkdev(DRBD_MAJOR, "drbd");
2392 if (err) {
2393 printk(KERN_ERR
2394 "drbd: unable to register block device major %d\n",
2395 DRBD_MAJOR);
2396 return err;
2397 }
2398
2399 register_reboot_notifier(&drbd_notifier);
2400
2401 /*
2402 * allocate all necessary structs
2403 */
2404 err = -ENOMEM;
2405
2406 init_waitqueue_head(&drbd_pp_wait);
2407
2408 drbd_proc = NULL; /* play safe for drbd_cleanup */
2409 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
2410 GFP_KERNEL);
2411 if (!minor_table)
2412 goto Enomem;
2413
2414 err = drbd_create_mempools();
2415 if (err)
2416 goto Enomem;
2417
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01002418 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419 if (!drbd_proc) {
2420 printk(KERN_ERR "drbd: unable to register proc file\n");
2421 goto Enomem;
2422 }
2423
2424 rwlock_init(&global_state_lock);
Philipp Reisner21114382011-01-19 12:26:59 +01002425 INIT_LIST_HEAD(&drbd_tconns);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426
2427 printk(KERN_INFO "drbd: initialized. "
2428 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2429 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2430 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2431 printk(KERN_INFO "drbd: registered as block device major %d\n",
2432 DRBD_MAJOR);
2433 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
2434
2435 return 0; /* Success! */
2436
2437Enomem:
2438 drbd_cleanup();
2439 if (err == -ENOMEM)
2440 /* currently always the case */
2441 printk(KERN_ERR "drbd: ran out of memory\n");
2442 else
2443 printk(KERN_ERR "drbd: initialization failure\n");
2444 return err;
2445}
2446
2447void drbd_free_bc(struct drbd_backing_dev *ldev)
2448{
2449 if (ldev == NULL)
2450 return;
2451
Tejun Heoe525fd82010-11-13 11:55:17 +01002452 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2453 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002454
2455 kfree(ldev);
2456}
2457
Philipp Reisner360cc742011-02-08 14:29:53 +01002458void drbd_free_sock(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459{
Philipp Reisner360cc742011-02-08 14:29:53 +01002460 if (tconn->data.socket) {
2461 mutex_lock(&tconn->data.mutex);
2462 kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
2463 sock_release(tconn->data.socket);
2464 tconn->data.socket = NULL;
2465 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002466 }
Philipp Reisner360cc742011-02-08 14:29:53 +01002467 if (tconn->meta.socket) {
2468 mutex_lock(&tconn->meta.mutex);
2469 kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
2470 sock_release(tconn->meta.socket);
2471 tconn->meta.socket = NULL;
2472 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002473 }
2474}
2475
2476
2477void drbd_free_resources(struct drbd_conf *mdev)
2478{
2479 crypto_free_hash(mdev->csums_tfm);
2480 mdev->csums_tfm = NULL;
2481 crypto_free_hash(mdev->verify_tfm);
2482 mdev->verify_tfm = NULL;
Philipp Reisnera0638452011-01-19 14:31:32 +01002483 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
2484 mdev->tconn->cram_hmac_tfm = NULL;
2485 crypto_free_hash(mdev->tconn->integrity_w_tfm);
2486 mdev->tconn->integrity_w_tfm = NULL;
2487 crypto_free_hash(mdev->tconn->integrity_r_tfm);
2488 mdev->tconn->integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002489
Philipp Reisner360cc742011-02-08 14:29:53 +01002490 drbd_free_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002491
2492 __no_warn(local,
2493 drbd_free_bc(mdev->ldev);
2494 mdev->ldev = NULL;);
2495}
2496
2497/* meta data management */
2498
2499struct meta_data_on_disk {
2500 u64 la_size; /* last agreed size. */
2501 u64 uuid[UI_SIZE]; /* UUIDs. */
2502 u64 device_uuid;
2503 u64 reserved_u64_1;
2504 u32 flags; /* MDF */
2505 u32 magic;
2506 u32 md_size_sect;
2507 u32 al_offset; /* offset to this block */
2508 u32 al_nr_extents; /* important for restoring the AL */
2509 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
2510 u32 bm_offset; /* offset to the bitmap, from here */
2511 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
Philipp Reisner99432fc2011-05-20 16:39:13 +02002512 u32 la_peer_max_bio_size; /* last peer max_bio_size */
2513 u32 reserved_u32[3];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002514
2515} __packed;
2516
2517/**
2518 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2519 * @mdev: DRBD device.
2520 */
2521void drbd_md_sync(struct drbd_conf *mdev)
2522{
2523 struct meta_data_on_disk *buffer;
2524 sector_t sector;
2525 int i;
2526
Lars Ellenbergee15b032010-09-03 10:00:09 +02002527 del_timer(&mdev->md_sync_timer);
2528 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002529 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
2530 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002531
2532 /* We use here D_FAILED and not D_ATTACHING because we try to write
2533 * metadata even if we detach due to a disk failure! */
2534 if (!get_ldev_if_state(mdev, D_FAILED))
2535 return;
2536
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537 mutex_lock(&mdev->md_io_mutex);
2538 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2539 memset(buffer, 0, 512);
2540
2541 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
2542 for (i = UI_CURRENT; i < UI_SIZE; i++)
2543 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
2544 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
2545 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
2546
2547 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
2548 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
2549 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
2550 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
2551 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
2552
2553 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002554 buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555
2556 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
2557 sector = mdev->ldev->md.md_offset;
2558
Lars Ellenberg3f3a9b82010-09-01 15:12:12 +02002559 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002560 /* this was a try anyways ... */
2561 dev_err(DEV, "meta data update failed!\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002562 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002563 }
2564
2565 /* Update mdev->ldev->md.la_size_sect,
2566 * since we updated it on metadata. */
2567 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
2568
2569 mutex_unlock(&mdev->md_io_mutex);
2570 put_ldev(mdev);
2571}
2572
2573/**
2574 * drbd_md_read() - Reads in the meta data super block
2575 * @mdev: DRBD device.
2576 * @bdev: Device from which the meta data should be read in.
2577 *
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01002578 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
Philipp Reisnerb411b362009-09-25 16:07:19 -07002579 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
2580 */
2581int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
2582{
2583 struct meta_data_on_disk *buffer;
2584 int i, rv = NO_ERROR;
2585
2586 if (!get_ldev_if_state(mdev, D_ATTACHING))
2587 return ERR_IO_MD_DISK;
2588
Philipp Reisnerb411b362009-09-25 16:07:19 -07002589 mutex_lock(&mdev->md_io_mutex);
2590 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2591
2592 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002593 /* NOTE: can't do normal error processing here as this is
Philipp Reisnerb411b362009-09-25 16:07:19 -07002594 called BEFORE disk is attached */
2595 dev_err(DEV, "Error while reading metadata.\n");
2596 rv = ERR_IO_MD_DISK;
2597 goto err;
2598 }
2599
Andreas Gruenbachere7fad8a2011-01-11 13:54:02 +01002600 if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002601 dev_err(DEV, "Error while reading metadata, magic not found.\n");
2602 rv = ERR_MD_INVALID;
2603 goto err;
2604 }
2605 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
2606 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
2607 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
2608 rv = ERR_MD_INVALID;
2609 goto err;
2610 }
2611 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
2612 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
2613 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
2614 rv = ERR_MD_INVALID;
2615 goto err;
2616 }
2617 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
2618 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
2619 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
2620 rv = ERR_MD_INVALID;
2621 goto err;
2622 }
2623
2624 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
2625 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
2626 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
2627 rv = ERR_MD_INVALID;
2628 goto err;
2629 }
2630
2631 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
2632 for (i = UI_CURRENT; i < UI_SIZE; i++)
2633 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
2634 bdev->md.flags = be32_to_cpu(buffer->flags);
2635 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
2636 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
2637
Philipp Reisner87eeee42011-01-19 14:16:30 +01002638 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002639 if (mdev->state.conn < C_CONNECTED) {
2640 int peer;
2641 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
2642 peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
2643 mdev->peer_max_bio_size = peer;
2644 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002645 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002646
Philipp Reisnerb411b362009-09-25 16:07:19 -07002647 if (mdev->sync_conf.al_extents < 7)
2648 mdev->sync_conf.al_extents = 127;
2649
2650 err:
2651 mutex_unlock(&mdev->md_io_mutex);
2652 put_ldev(mdev);
2653
2654 return rv;
2655}
2656
2657/**
2658 * drbd_md_mark_dirty() - Mark meta data super block as dirty
2659 * @mdev: DRBD device.
2660 *
2661 * Call this function if you change anything that should be written to
2662 * the meta-data super block. This function sets MD_DIRTY, and starts a
2663 * timer that ensures that within five seconds you have to call drbd_md_sync().
2664 */
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002665#ifdef DEBUG
Lars Ellenbergee15b032010-09-03 10:00:09 +02002666void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
2667{
2668 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
2669 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
2670 mdev->last_md_mark_dirty.line = line;
2671 mdev->last_md_mark_dirty.func = func;
2672 }
2673}
2674#else
Philipp Reisnerb411b362009-09-25 16:07:19 -07002675void drbd_md_mark_dirty(struct drbd_conf *mdev)
2676{
Lars Ellenbergee15b032010-09-03 10:00:09 +02002677 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002678 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002679}
Lars Ellenbergee15b032010-09-03 10:00:09 +02002680#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002681
2682static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
2683{
2684 int i;
2685
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002686 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002687 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002688}
2689
2690void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2691{
2692 if (idx == UI_CURRENT) {
2693 if (mdev->state.role == R_PRIMARY)
2694 val |= 1;
2695 else
2696 val &= ~((u64)1);
2697
2698 drbd_set_ed_uuid(mdev, val);
2699 }
2700
2701 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002702 drbd_md_mark_dirty(mdev);
2703}
2704
2705
2706void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2707{
2708 if (mdev->ldev->md.uuid[idx]) {
2709 drbd_uuid_move_history(mdev);
2710 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002711 }
2712 _drbd_uuid_set(mdev, idx, val);
2713}
2714
2715/**
2716 * drbd_uuid_new_current() - Creates a new current UUID
2717 * @mdev: DRBD device.
2718 *
2719 * Creates a new current UUID, and rotates the old current UUID into
2720 * the bitmap slot. Causes an incremental resync upon next connect.
2721 */
2722void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
2723{
2724 u64 val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002725 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002726
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002727 if (bm_uuid)
2728 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
2729
Philipp Reisnerb411b362009-09-25 16:07:19 -07002730 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002731
2732 get_random_bytes(&val, sizeof(u64));
2733 _drbd_uuid_set(mdev, UI_CURRENT, val);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002734 drbd_print_uuids(mdev, "new current UUID");
Lars Ellenbergaaa8e2b2010-10-15 13:16:53 +02002735 /* get it to stable storage _now_ */
2736 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002737}
2738
2739void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
2740{
2741 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
2742 return;
2743
2744 if (val == 0) {
2745 drbd_uuid_move_history(mdev);
2746 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2747 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748 } else {
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002749 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
2750 if (bm_uuid)
2751 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002752
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002753 mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002754 }
2755 drbd_md_mark_dirty(mdev);
2756}
2757
2758/**
2759 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2760 * @mdev: DRBD device.
2761 *
2762 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
2763 */
2764int drbd_bmio_set_n_write(struct drbd_conf *mdev)
2765{
2766 int rv = -EIO;
2767
2768 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2769 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
2770 drbd_md_sync(mdev);
2771 drbd_bm_set_all(mdev);
2772
2773 rv = drbd_bm_write(mdev);
2774
2775 if (!rv) {
2776 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2777 drbd_md_sync(mdev);
2778 }
2779
2780 put_ldev(mdev);
2781 }
2782
2783 return rv;
2784}
2785
2786/**
2787 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2788 * @mdev: DRBD device.
2789 *
2790 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
2791 */
2792int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
2793{
2794 int rv = -EIO;
2795
Philipp Reisner07782862010-08-31 12:00:50 +02002796 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002797 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2798 drbd_bm_clear_all(mdev);
2799 rv = drbd_bm_write(mdev);
2800 put_ldev(mdev);
2801 }
2802
2803 return rv;
2804}
2805
Philipp Reisner00d56942011-02-09 18:09:48 +01002806static int w_bitmap_io(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002807{
2808 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01002809 struct drbd_conf *mdev = w->mdev;
Lars Ellenberg02851e92010-12-16 14:47:39 +01002810 int rv = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002811
2812 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
2813
Lars Ellenberg02851e92010-12-16 14:47:39 +01002814 if (get_ldev(mdev)) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002815 drbd_bm_lock(mdev, work->why, work->flags);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002816 rv = work->io_fn(mdev);
2817 drbd_bm_unlock(mdev);
2818 put_ldev(mdev);
2819 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002820
Lars Ellenberg4738fa12011-02-21 13:20:55 +01002821 clear_bit_unlock(BITMAP_IO, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002822 wake_up(&mdev->misc_wait);
2823
2824 if (work->done)
2825 work->done(mdev, rv);
2826
2827 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
2828 work->why = NULL;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002829 work->flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002830
2831 return 1;
2832}
2833
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002834void drbd_ldev_destroy(struct drbd_conf *mdev)
2835{
2836 lc_destroy(mdev->resync);
2837 mdev->resync = NULL;
2838 lc_destroy(mdev->act_log);
2839 mdev->act_log = NULL;
2840 __no_warn(local,
2841 drbd_free_bc(mdev->ldev);
2842 mdev->ldev = NULL;);
2843
2844 if (mdev->md_io_tmpp) {
2845 __free_page(mdev->md_io_tmpp);
2846 mdev->md_io_tmpp = NULL;
2847 }
2848 clear_bit(GO_DISKLESS, &mdev->flags);
2849}
2850
Philipp Reisner00d56942011-02-09 18:09:48 +01002851static int w_go_diskless(struct drbd_work *w, int unused)
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002852{
Philipp Reisner00d56942011-02-09 18:09:48 +01002853 struct drbd_conf *mdev = w->mdev;
2854
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002855 D_ASSERT(mdev->state.disk == D_FAILED);
Lars Ellenberg9d282872010-10-14 13:57:07 +02002856 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
2857 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002858 * the protected members anymore, though, so once put_ldev reaches zero
2859 * again, it will be safe to free them. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002860 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002861 return 1;
2862}
2863
2864void drbd_go_diskless(struct drbd_conf *mdev)
2865{
2866 D_ASSERT(mdev->state.disk == D_FAILED);
2867 if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002868 drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002869}
2870
Philipp Reisnerb411b362009-09-25 16:07:19 -07002871/**
2872 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
2873 * @mdev: DRBD device.
2874 * @io_fn: IO callback to be called when bitmap IO is possible
2875 * @done: callback to be called after the bitmap IO was performed
2876 * @why: Descriptive text of the reason for doing the IO
2877 *
2878 * While IO on the bitmap happens we freeze application IO thus we ensure
2879 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
2880 * called from worker context. It MUST NOT be used while a previous such
2881 * work is still pending!
2882 */
2883void drbd_queue_bitmap_io(struct drbd_conf *mdev,
2884 int (*io_fn)(struct drbd_conf *),
2885 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002886 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002887{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002888 D_ASSERT(current == mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002889
2890 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
2891 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
2892 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
2893 if (mdev->bm_io_work.why)
2894 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
2895 why, mdev->bm_io_work.why);
2896
2897 mdev->bm_io_work.io_fn = io_fn;
2898 mdev->bm_io_work.done = done;
2899 mdev->bm_io_work.why = why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002900 mdev->bm_io_work.flags = flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002901
Philipp Reisner87eeee42011-01-19 14:16:30 +01002902 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002903 set_bit(BITMAP_IO, &mdev->flags);
2904 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
Philipp Reisner127b3172010-11-16 10:07:53 +01002905 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002906 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002907 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002908 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002909}
2910
2911/**
2912 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
2913 * @mdev: DRBD device.
2914 * @io_fn: IO callback to be called when bitmap IO is possible
2915 * @why: Descriptive text of the reason for doing the IO
2916 *
2917 * freezes application IO while that the actual IO operations runs. This
2918 * functions MAY NOT be called from worker context.
2919 */
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002920int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
2921 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002922{
2923 int rv;
2924
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002925 D_ASSERT(current != mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002926
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002927 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2928 drbd_suspend_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002929
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002930 drbd_bm_lock(mdev, why, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002931 rv = io_fn(mdev);
2932 drbd_bm_unlock(mdev);
2933
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002934 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2935 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002936
2937 return rv;
2938}
2939
2940void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2941{
2942 if ((mdev->ldev->md.flags & flag) != flag) {
2943 drbd_md_mark_dirty(mdev);
2944 mdev->ldev->md.flags |= flag;
2945 }
2946}
2947
2948void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2949{
2950 if ((mdev->ldev->md.flags & flag) != 0) {
2951 drbd_md_mark_dirty(mdev);
2952 mdev->ldev->md.flags &= ~flag;
2953 }
2954}
2955int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
2956{
2957 return (bdev->md.flags & flag) != 0;
2958}
2959
2960static void md_sync_timer_fn(unsigned long data)
2961{
2962 struct drbd_conf *mdev = (struct drbd_conf *) data;
2963
Philipp Reisnere42325a2011-01-19 13:55:45 +01002964 drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002965}
2966
Philipp Reisner00d56942011-02-09 18:09:48 +01002967static int w_md_sync(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002968{
Philipp Reisner00d56942011-02-09 18:09:48 +01002969 struct drbd_conf *mdev = w->mdev;
2970
Philipp Reisnerb411b362009-09-25 16:07:19 -07002971 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02002972#ifdef DEBUG
2973 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
2974 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
2975#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002976 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002977 return 1;
2978}
2979
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002980const char *cmdname(enum drbd_packet cmd)
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01002981{
2982 /* THINK may need to become several global tables
2983 * when we want to support more than
2984 * one PRO_VERSION */
2985 static const char *cmdnames[] = {
2986 [P_DATA] = "Data",
2987 [P_DATA_REPLY] = "DataReply",
2988 [P_RS_DATA_REPLY] = "RSDataReply",
2989 [P_BARRIER] = "Barrier",
2990 [P_BITMAP] = "ReportBitMap",
2991 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
2992 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
2993 [P_UNPLUG_REMOTE] = "UnplugRemote",
2994 [P_DATA_REQUEST] = "DataRequest",
2995 [P_RS_DATA_REQUEST] = "RSDataRequest",
2996 [P_SYNC_PARAM] = "SyncParam",
2997 [P_SYNC_PARAM89] = "SyncParam89",
2998 [P_PROTOCOL] = "ReportProtocol",
2999 [P_UUIDS] = "ReportUUIDs",
3000 [P_SIZES] = "ReportSizes",
3001 [P_STATE] = "ReportState",
3002 [P_SYNC_UUID] = "ReportSyncUUID",
3003 [P_AUTH_CHALLENGE] = "AuthChallenge",
3004 [P_AUTH_RESPONSE] = "AuthResponse",
3005 [P_PING] = "Ping",
3006 [P_PING_ACK] = "PingAck",
3007 [P_RECV_ACK] = "RecvAck",
3008 [P_WRITE_ACK] = "WriteAck",
3009 [P_RS_WRITE_ACK] = "RSWriteAck",
3010 [P_DISCARD_ACK] = "DiscardAck",
3011 [P_NEG_ACK] = "NegAck",
3012 [P_NEG_DREPLY] = "NegDReply",
3013 [P_NEG_RS_DREPLY] = "NegRSDReply",
3014 [P_BARRIER_ACK] = "BarrierAck",
3015 [P_STATE_CHG_REQ] = "StateChgRequest",
3016 [P_STATE_CHG_REPLY] = "StateChgReply",
3017 [P_OV_REQUEST] = "OVRequest",
3018 [P_OV_REPLY] = "OVReply",
3019 [P_OV_RESULT] = "OVResult",
3020 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3021 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3022 [P_COMPRESSED_BITMAP] = "CBitmap",
3023 [P_DELAY_PROBE] = "DelayProbe",
3024 [P_OUT_OF_SYNC] = "OutOfSync",
3025 [P_MAX_CMD] = NULL,
3026 };
3027
3028 if (cmd == P_HAND_SHAKE_M)
3029 return "HandShakeM";
3030 if (cmd == P_HAND_SHAKE_S)
3031 return "HandShakeS";
3032 if (cmd == P_HAND_SHAKE)
3033 return "HandShake";
3034 if (cmd >= P_MAX_CMD)
3035 return "Unknown";
3036 return cmdnames[cmd];
3037}
3038
Philipp Reisnerb411b362009-09-25 16:07:19 -07003039#ifdef CONFIG_DRBD_FAULT_INJECTION
3040/* Fault insertion support including random number generator shamelessly
3041 * stolen from kernel/rcutorture.c */
3042struct fault_random_state {
3043 unsigned long state;
3044 unsigned long count;
3045};
3046
3047#define FAULT_RANDOM_MULT 39916801 /* prime */
3048#define FAULT_RANDOM_ADD 479001701 /* prime */
3049#define FAULT_RANDOM_REFRESH 10000
3050
3051/*
3052 * Crude but fast random-number generator. Uses a linear congruential
3053 * generator, with occasional help from get_random_bytes().
3054 */
3055static unsigned long
3056_drbd_fault_random(struct fault_random_state *rsp)
3057{
3058 long refresh;
3059
Roel Kluin49829ea2009-12-15 22:55:44 +01003060 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003061 get_random_bytes(&refresh, sizeof(refresh));
3062 rsp->state += refresh;
3063 rsp->count = FAULT_RANDOM_REFRESH;
3064 }
3065 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3066 return swahw32(rsp->state);
3067}
3068
3069static char *
3070_drbd_fault_str(unsigned int type) {
3071 static char *_faults[] = {
3072 [DRBD_FAULT_MD_WR] = "Meta-data write",
3073 [DRBD_FAULT_MD_RD] = "Meta-data read",
3074 [DRBD_FAULT_RS_WR] = "Resync write",
3075 [DRBD_FAULT_RS_RD] = "Resync read",
3076 [DRBD_FAULT_DT_WR] = "Data write",
3077 [DRBD_FAULT_DT_RD] = "Data read",
3078 [DRBD_FAULT_DT_RA] = "Data read ahead",
3079 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003080 [DRBD_FAULT_AL_EE] = "EE allocation",
3081 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003082 };
3083
3084 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3085}
3086
3087unsigned int
3088_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3089{
3090 static struct fault_random_state rrs = {0, 0};
3091
3092 unsigned int ret = (
3093 (fault_devs == 0 ||
3094 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3095 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3096
3097 if (ret) {
3098 fault_count++;
3099
Lars Ellenberg73835062010-05-27 11:51:56 +02003100 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101 dev_warn(DEV, "***Simulating %s failure\n",
3102 _drbd_fault_str(type));
3103 }
3104
3105 return ret;
3106}
3107#endif
3108
3109const char *drbd_buildtag(void)
3110{
3111 /* DRBD built from external sources has here a reference to the
3112 git hash of the source code. */
3113
3114 static char buildtag[38] = "\0uilt-in";
3115
3116 if (buildtag[0] == 0) {
3117#ifdef CONFIG_MODULES
3118 if (THIS_MODULE != NULL)
3119 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3120 else
3121#endif
3122 buildtag[0] = 'b';
3123 }
3124
3125 return buildtag;
3126}
3127
3128module_init(drbd_init)
3129module_exit(drbd_cleanup)
3130
Philipp Reisnerb411b362009-09-25 16:07:19 -07003131EXPORT_SYMBOL(drbd_conn_str);
3132EXPORT_SYMBOL(drbd_role_str);
3133EXPORT_SYMBOL(drbd_disk_str);
3134EXPORT_SYMBOL(drbd_set_st_err_str);