blob: e89ec80395d3ad3ac31c23630e52c6edb6fe5981 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
12
13 drbd is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 drbd is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with drbd; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 */
28
Philipp Reisnerb411b362009-09-25 16:07:19 -070029#include <linux/module.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070030#include <linux/drbd.h>
31#include <asm/uaccess.h>
32#include <asm/types.h>
33#include <net/sock.h>
34#include <linux/ctype.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020035#include <linux/mutex.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#include <linux/fs.h>
37#include <linux/file.h>
38#include <linux/proc_fs.h>
39#include <linux/init.h>
40#include <linux/mm.h>
41#include <linux/memcontrol.h>
42#include <linux/mm_inline.h>
43#include <linux/slab.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
46#include <linux/notifier.h>
47#include <linux/kthread.h>
48
49#define __KERNEL_SYSCALLS__
50#include <linux/unistd.h>
51#include <linux/vmalloc.h>
52
53#include <linux/drbd_limits.h>
54#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070055#include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
56
57#include "drbd_vli.h"
58
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020059static DEFINE_MUTEX(drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -070060int drbdd_init(struct drbd_thread *);
61int drbd_worker(struct drbd_thread *);
62int drbd_asender(struct drbd_thread *);
63
64int drbd_init(void);
65static int drbd_open(struct block_device *bdev, fmode_t mode);
66static int drbd_release(struct gendisk *gd, fmode_t mode);
Philipp Reisnerb411b362009-09-25 16:07:19 -070067static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
68static void md_sync_timer_fn(unsigned long data);
69static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +020070static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
Philipp Reisnerb411b362009-09-25 16:07:19 -070072MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
73 "Lars Ellenberg <lars@linbit.com>");
74MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
75MODULE_VERSION(REL_VERSION);
76MODULE_LICENSE("GPL");
Philipp Reisner2b8a90b2011-01-10 11:15:17 +010077MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices ("
78 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
Philipp Reisnerb411b362009-09-25 16:07:19 -070079MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
80
81#include <linux/moduleparam.h>
82/* allow_open_on_secondary */
83MODULE_PARM_DESC(allow_oos, "DONT USE!");
84/* thanks to these macros, if compiled into the kernel (not-module),
85 * this becomes the boot parameter drbd.minor_count */
86module_param(minor_count, uint, 0444);
87module_param(disable_sendpage, bool, 0644);
88module_param(allow_oos, bool, 0);
89module_param(cn_idx, uint, 0444);
90module_param(proc_details, int, 0644);
91
92#ifdef CONFIG_DRBD_FAULT_INJECTION
93int enable_faults;
94int fault_rate;
95static int fault_count;
96int fault_devs;
97/* bitmap of enabled faults */
98module_param(enable_faults, int, 0664);
99/* fault rate % value - applies to all enabled faults */
100module_param(fault_rate, int, 0664);
101/* count of faults inserted */
102module_param(fault_count, int, 0664);
103/* bitmap of devices to insert faults on */
104module_param(fault_devs, int, 0644);
105#endif
106
107/* module parameter, defined */
Philipp Reisner2b8a90b2011-01-10 11:15:17 +0100108unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109int disable_sendpage;
110int allow_oos;
111unsigned int cn_idx = CN_IDX_DRBD;
112int proc_details; /* Detail level in proc drbd*/
113
114/* Module parameter for setting the user mode helper program
115 * to run. Default is /sbin/drbdadm */
116char usermode_helper[80] = "/sbin/drbdadm";
117
118module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
119
120/* in 2.6.x, our device mapping and config info contains our virtual gendisks
121 * as member "struct gendisk *vdisk;"
122 */
123struct drbd_conf **minor_table;
Philipp Reisner21114382011-01-19 12:26:59 +0100124struct list_head drbd_tconns; /* list of struct drbd_tconn */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125
126struct kmem_cache *drbd_request_cache;
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +0100127struct kmem_cache *drbd_ee_cache; /* peer requests */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700128struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
129struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
130mempool_t *drbd_request_mempool;
131mempool_t *drbd_ee_mempool;
132
133/* I do not use a standard mempool, because:
134 1) I want to hand out the pre-allocated objects first.
135 2) I want to be able to interrupt sleeping allocation with a signal.
136 Note: This is a single linked list, the next pointer is the private
137 member of struct page.
138 */
139struct page *drbd_pp_pool;
140spinlock_t drbd_pp_lock;
141int drbd_pp_vacant;
142wait_queue_head_t drbd_pp_wait;
143
144DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
145
Emese Revfy7d4e9d02009-12-14 00:59:30 +0100146static const struct block_device_operations drbd_ops = {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700147 .owner = THIS_MODULE,
148 .open = drbd_open,
149 .release = drbd_release,
150};
151
152#define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
153
154#ifdef __CHECKER__
155/* When checking with sparse, and this is an inline function, sparse will
156 give tons of false positives. When this is a real functions sparse works.
157 */
158int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
159{
160 int io_allowed;
161
162 atomic_inc(&mdev->local_cnt);
163 io_allowed = (mdev->state.disk >= mins);
164 if (!io_allowed) {
165 if (atomic_dec_and_test(&mdev->local_cnt))
166 wake_up(&mdev->misc_wait);
167 }
168 return io_allowed;
169}
170
171#endif
172
173/**
174 * DOC: The transfer log
175 *
176 * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100177 * mdev->tconn->newest_tle points to the head, mdev->tconn->oldest_tle points to the tail
Philipp Reisnerb411b362009-09-25 16:07:19 -0700178 * of the list. There is always at least one &struct drbd_tl_epoch object.
179 *
180 * Each &struct drbd_tl_epoch has a circular double linked list of requests
181 * attached.
182 */
183static int tl_init(struct drbd_conf *mdev)
184{
185 struct drbd_tl_epoch *b;
186
187 /* during device minor initialization, we may well use GFP_KERNEL */
188 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
189 if (!b)
190 return 0;
191 INIT_LIST_HEAD(&b->requests);
192 INIT_LIST_HEAD(&b->w.list);
193 b->next = NULL;
194 b->br_number = 4711;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200195 b->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700196 b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
197
Philipp Reisner87eeee42011-01-19 14:16:30 +0100198 mdev->tconn->oldest_tle = b;
199 mdev->tconn->newest_tle = b;
200 INIT_LIST_HEAD(&mdev->tconn->out_of_sequence_requests);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201
Philipp Reisnerb411b362009-09-25 16:07:19 -0700202 return 1;
203}
204
205static void tl_cleanup(struct drbd_conf *mdev)
206{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100207 D_ASSERT(mdev->tconn->oldest_tle == mdev->tconn->newest_tle);
208 D_ASSERT(list_empty(&mdev->tconn->out_of_sequence_requests));
209 kfree(mdev->tconn->oldest_tle);
210 mdev->tconn->oldest_tle = NULL;
211 kfree(mdev->tconn->unused_spare_tle);
212 mdev->tconn->unused_spare_tle = NULL;
Andreas Gruenbacherd6287692011-01-13 23:05:39 +0100213}
214
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215/**
216 * _tl_add_barrier() - Adds a barrier to the transfer log
217 * @mdev: DRBD device.
218 * @new: Barrier to be added before the current head of the TL.
219 *
220 * The caller must hold the req_lock.
221 */
222void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
223{
224 struct drbd_tl_epoch *newest_before;
225
226 INIT_LIST_HEAD(&new->requests);
227 INIT_LIST_HEAD(&new->w.list);
228 new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
229 new->next = NULL;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200230 new->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700231
Philipp Reisner87eeee42011-01-19 14:16:30 +0100232 newest_before = mdev->tconn->newest_tle;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 /* never send a barrier number == 0, because that is special-cased
234 * when using TCQ for our write ordering code */
235 new->br_number = (newest_before->br_number+1) ?: 1;
Philipp Reisner87eeee42011-01-19 14:16:30 +0100236 if (mdev->tconn->newest_tle != new) {
237 mdev->tconn->newest_tle->next = new;
238 mdev->tconn->newest_tle = new;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700239 }
240}
241
242/**
243 * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
244 * @mdev: DRBD device.
245 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
246 * @set_size: Expected number of requests before that barrier.
247 *
248 * In case the passed barrier_nr or set_size does not match the oldest
249 * &struct drbd_tl_epoch objects this function will cause a termination
250 * of the connection.
251 */
252void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
253 unsigned int set_size)
254{
255 struct drbd_tl_epoch *b, *nob; /* next old barrier */
256 struct list_head *le, *tle;
257 struct drbd_request *r;
258
Philipp Reisner87eeee42011-01-19 14:16:30 +0100259 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Philipp Reisner87eeee42011-01-19 14:16:30 +0100261 b = mdev->tconn->oldest_tle;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262
263 /* first some paranoia code */
264 if (b == NULL) {
265 dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
266 barrier_nr);
267 goto bail;
268 }
269 if (b->br_number != barrier_nr) {
270 dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n",
271 barrier_nr, b->br_number);
272 goto bail;
273 }
Philipp Reisner7e602c02010-05-27 14:49:27 +0200274 if (b->n_writes != set_size) {
275 dev_err(DEV, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
276 barrier_nr, set_size, b->n_writes);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 goto bail;
278 }
279
280 /* Clean up list of requests processed during current epoch */
281 list_for_each_safe(le, tle, &b->requests) {
282 r = list_entry(le, struct drbd_request, tl_requests);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100283 _req_mod(r, BARRIER_ACKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700284 }
285 /* There could be requests on the list waiting for completion
286 of the write to the local disk. To avoid corruptions of
287 slab's data structures we have to remove the lists head.
288
289 Also there could have been a barrier ack out of sequence, overtaking
290 the write acks - which would be a bug and violating write ordering.
291 To not deadlock in case we lose connection while such requests are
292 still pending, we need some way to find them for the
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100293 _req_mode(CONNECTION_LOST_WHILE_PENDING).
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294
295 These have been list_move'd to the out_of_sequence_requests list in
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100296 _req_mod(, BARRIER_ACKED) above.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700297 */
298 list_del_init(&b->requests);
299
300 nob = b->next;
301 if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
302 _tl_add_barrier(mdev, b);
303 if (nob)
Philipp Reisner87eeee42011-01-19 14:16:30 +0100304 mdev->tconn->oldest_tle = nob;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305 /* if nob == NULL b was the only barrier, and becomes the new
Philipp Reisner87eeee42011-01-19 14:16:30 +0100306 barrier. Therefore mdev->tconn->oldest_tle points already to b */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 } else {
308 D_ASSERT(nob != NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100309 mdev->tconn->oldest_tle = nob;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 kfree(b);
311 }
312
Philipp Reisner87eeee42011-01-19 14:16:30 +0100313 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314 dec_ap_pending(mdev);
315
316 return;
317
318bail:
Philipp Reisner87eeee42011-01-19 14:16:30 +0100319 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700320 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
321}
322
Philipp Reisner617049a2010-12-22 12:48:31 +0100323
Philipp Reisner11b58e72010-05-12 17:08:26 +0200324/**
325 * _tl_restart() - Walks the transfer log, and applies an action to all requests
326 * @mdev: DRBD device.
327 * @what: The action/event to perform with all request objects
328 *
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100329 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
330 * RESTART_FROZEN_DISK_IO.
Philipp Reisner11b58e72010-05-12 17:08:26 +0200331 */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100332void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
Philipp Reisner11b58e72010-05-12 17:08:26 +0200333{
334 struct drbd_tl_epoch *b, *tmp, **pn;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200335 struct list_head *le, *tle, carry_reads;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200336 struct drbd_request *req;
337 int rv, n_writes, n_reads;
338
Philipp Reisner87eeee42011-01-19 14:16:30 +0100339 b = mdev->tconn->oldest_tle;
340 pn = &mdev->tconn->oldest_tle;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200341 while (b) {
342 n_writes = 0;
343 n_reads = 0;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200344 INIT_LIST_HEAD(&carry_reads);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200345 list_for_each_safe(le, tle, &b->requests) {
346 req = list_entry(le, struct drbd_request, tl_requests);
347 rv = _req_mod(req, what);
348
349 n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
350 n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
351 }
352 tmp = b->next;
353
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200354 if (n_writes) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100355 if (what == RESEND) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200356 b->n_writes = n_writes;
357 if (b->w.cb == NULL) {
358 b->w.cb = w_send_barrier;
359 inc_ap_pending(mdev);
360 set_bit(CREATE_BARRIER, &mdev->flags);
361 }
362
Philipp Reisnere42325a2011-01-19 13:55:45 +0100363 drbd_queue_work(&mdev->tconn->data.work, &b->w);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200364 }
365 pn = &b->next;
366 } else {
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200367 if (n_reads)
368 list_add(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200369 /* there could still be requests on that ring list,
370 * in case local io is still pending */
371 list_del(&b->requests);
372
373 /* dec_ap_pending corresponding to queue_barrier.
374 * the newest barrier may not have been queued yet,
375 * in which case w.cb is still NULL. */
376 if (b->w.cb != NULL)
377 dec_ap_pending(mdev);
378
Philipp Reisner87eeee42011-01-19 14:16:30 +0100379 if (b == mdev->tconn->newest_tle) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200380 /* recycle, but reinit! */
381 D_ASSERT(tmp == NULL);
382 INIT_LIST_HEAD(&b->requests);
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200383 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200384 INIT_LIST_HEAD(&b->w.list);
385 b->w.cb = NULL;
386 b->br_number = net_random();
387 b->n_writes = 0;
388
389 *pn = b;
390 break;
391 }
392 *pn = tmp;
393 kfree(b);
394 }
395 b = tmp;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200396 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200397 }
398}
399
Philipp Reisnerb411b362009-09-25 16:07:19 -0700400
401/**
402 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
403 * @mdev: DRBD device.
404 *
405 * This is called after the connection to the peer was lost. The storage covered
406 * by the requests on the transfer gets marked as our of sync. Called from the
407 * receiver thread and the worker thread.
408 */
409void tl_clear(struct drbd_conf *mdev)
410{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411 struct list_head *le, *tle;
412 struct drbd_request *r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100416 _tl_restart(mdev, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417
418 /* we expect this list to be empty. */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100419 D_ASSERT(list_empty(&mdev->tconn->out_of_sequence_requests));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
421 /* but just in case, clean it up anyways! */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100422 list_for_each_safe(le, tle, &mdev->tconn->out_of_sequence_requests) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423 r = list_entry(le, struct drbd_request, tl_requests);
424 /* It would be nice to complete outside of spinlock.
425 * But this is easier for now. */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100426 _req_mod(r, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427 }
428
429 /* ensure bit indicating barrier is required is clear */
430 clear_bit(CREATE_BARRIER, &mdev->flags);
431
Philipp Reisner87eeee42011-01-19 14:16:30 +0100432 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433}
434
Philipp Reisner11b58e72010-05-12 17:08:26 +0200435void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
436{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100437 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200438 _tl_restart(mdev, what);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100439 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700440}
441
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442static int drbd_thread_setup(void *arg)
443{
444 struct drbd_thread *thi = (struct drbd_thread *) arg;
445 struct drbd_conf *mdev = thi->mdev;
446 unsigned long flags;
447 int retval;
448
449restart:
450 retval = thi->function(thi);
451
452 spin_lock_irqsave(&thi->t_lock, flags);
453
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100454 /* if the receiver has been "EXITING", the last thing it did
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 * was set the conn state to "StandAlone",
456 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
457 * and receiver thread will be "started".
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100458 * drbd_thread_start needs to set "RESTARTING" in that case.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700459 * t_state check and assignment needs to be within the same spinlock,
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100460 * so either thread_start sees EXITING, and can remap to RESTARTING,
461 * or thread_start see NONE, and can proceed as normal.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462 */
463
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100464 if (thi->t_state == RESTARTING) {
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100465 dev_info(DEV, "Restarting %s thread\n", thi->name);
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100466 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467 spin_unlock_irqrestore(&thi->t_lock, flags);
468 goto restart;
469 }
470
471 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100472 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700473 smp_mb();
474 complete(&thi->stop);
475 spin_unlock_irqrestore(&thi->t_lock, flags);
476
477 dev_info(DEV, "Terminating %s\n", current->comm);
478
479 /* Release mod reference taken when thread was started */
480 module_put(THIS_MODULE);
481 return retval;
482}
483
484static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100485 int (*func) (struct drbd_thread *), char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486{
487 spin_lock_init(&thi->t_lock);
488 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100489 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490 thi->function = func;
491 thi->mdev = mdev;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100492 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493}
494
495int drbd_thread_start(struct drbd_thread *thi)
496{
497 struct drbd_conf *mdev = thi->mdev;
498 struct task_struct *nt;
499 unsigned long flags;
500
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 /* is used from state engine doing drbd_thread_stop_nowait,
502 * while holding the req lock irqsave */
503 spin_lock_irqsave(&thi->t_lock, flags);
504
505 switch (thi->t_state) {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100506 case NONE:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700507 dev_info(DEV, "Starting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100508 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509
510 /* Get ref on module for thread - this is released when thread exits */
511 if (!try_module_get(THIS_MODULE)) {
512 dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
513 spin_unlock_irqrestore(&thi->t_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100514 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700515 }
516
517 init_completion(&thi->stop);
518 D_ASSERT(thi->task == NULL);
519 thi->reset_cpu_mask = 1;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100520 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 spin_unlock_irqrestore(&thi->t_lock, flags);
522 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
523
524 nt = kthread_create(drbd_thread_setup, (void *) thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100525 "drbd%d_%s", mdev_to_minor(mdev), thi->name);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526
527 if (IS_ERR(nt)) {
528 dev_err(DEV, "Couldn't start thread\n");
529
530 module_put(THIS_MODULE);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100531 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 }
533 spin_lock_irqsave(&thi->t_lock, flags);
534 thi->task = nt;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100535 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 spin_unlock_irqrestore(&thi->t_lock, flags);
537 wake_up_process(nt);
538 break;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100539 case EXITING:
540 thi->t_state = RESTARTING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700541 dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100542 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 /* fall through */
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100544 case RUNNING:
545 case RESTARTING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546 default:
547 spin_unlock_irqrestore(&thi->t_lock, flags);
548 break;
549 }
550
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100551 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552}
553
554
555void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
556{
557 unsigned long flags;
558
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100559 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560
561 /* may be called from state engine, holding the req lock irqsave */
562 spin_lock_irqsave(&thi->t_lock, flags);
563
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100564 if (thi->t_state == NONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 spin_unlock_irqrestore(&thi->t_lock, flags);
566 if (restart)
567 drbd_thread_start(thi);
568 return;
569 }
570
571 if (thi->t_state != ns) {
572 if (thi->task == NULL) {
573 spin_unlock_irqrestore(&thi->t_lock, flags);
574 return;
575 }
576
577 thi->t_state = ns;
578 smp_mb();
579 init_completion(&thi->stop);
580 if (thi->task != current)
581 force_sig(DRBD_SIGKILL, thi->task);
582
583 }
584
585 spin_unlock_irqrestore(&thi->t_lock, flags);
586
587 if (wait)
588 wait_for_completion(&thi->stop);
589}
590
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100591static struct drbd_thread *drbd_task_to_thread(struct drbd_conf *mdev, struct task_struct *task)
592{
593 struct drbd_tconn *tconn = mdev->tconn;
594 struct drbd_thread *thi =
595 task == tconn->receiver.task ? &tconn->receiver :
596 task == tconn->asender.task ? &tconn->asender :
597 task == tconn->worker.task ? &tconn->worker : NULL;
598
599 return thi;
600}
601
602char *drbd_task_to_thread_name(struct drbd_conf *mdev, struct task_struct *task)
603{
604 struct drbd_thread *thi = drbd_task_to_thread(mdev, task);
605 return thi ? thi->name : task->comm;
606}
607
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608#ifdef CONFIG_SMP
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 Reisner80822282011-02-08 12:46:30 +0100656 set_cpus_allowed_ptr(p, thi->mdev->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 */
710int drbd_send_cmd(struct drbd_conf *mdev, 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 Reisnere42325a2011-01-19 13:55:45 +0100717 mutex_lock(&mdev->tconn->data.mutex);
718 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719 } else {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100720 mutex_lock(&mdev->tconn->meta.mutex);
721 sock = mdev->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))
727 ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
728
729 if (use_data_socket)
Philipp Reisnere42325a2011-01-19 13:55:45 +0100730 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731 else
Philipp Reisnere42325a2011-01-19 13:55:45 +0100732 mutex_unlock(&mdev->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
973int drbd_send_state_req(struct drbd_conf *mdev,
974 union drbd_state mask, union drbd_state val)
975{
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 Reisnerc0129492011-01-19 16:58:16 +0100981 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ, &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
993int fill_bitmap_rle_bits(struct drbd_conf *mdev,
994 struct p_compressed_bm *p,
995 struct bm_xfer_ctx *c)
996{
997 struct bitstream bs;
998 unsigned long plain_bits;
999 unsigned long tmp;
1000 unsigned long rl;
1001 unsigned len;
1002 unsigned toggle;
1003 int bits;
1004
1005 /* may we use this feature? */
1006 if ((mdev->sync_conf.use_rle == 0) ||
Philipp Reisner31890f42011-01-19 14:12:51 +01001007 (mdev->tconn->agreed_pro_version < 90))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 return 0;
1009
1010 if (c->bit_offset >= c->bm_bits)
1011 return 0; /* nothing to do. */
1012
1013 /* use at most thus many bytes */
1014 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1015 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1016 /* plain bits covered in this code string */
1017 plain_bits = 0;
1018
1019 /* p->encoding & 0x80 stores whether the first run length is set.
1020 * bit offset is implicit.
1021 * start with toggle == 2 to be able to tell the first iteration */
1022 toggle = 2;
1023
1024 /* see how much plain bits we can stuff into one packet
1025 * using RLE and VLI. */
1026 do {
1027 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1028 : _drbd_bm_find_next(mdev, c->bit_offset);
1029 if (tmp == -1UL)
1030 tmp = c->bm_bits;
1031 rl = tmp - c->bit_offset;
1032
1033 if (toggle == 2) { /* first iteration */
1034 if (rl == 0) {
1035 /* the first checked bit was set,
1036 * store start value, */
1037 DCBP_set_start(p, 1);
1038 /* but skip encoding of zero run length */
1039 toggle = !toggle;
1040 continue;
1041 }
1042 DCBP_set_start(p, 0);
1043 }
1044
1045 /* paranoia: catch zero runlength.
1046 * can only happen if bitmap is modified while we scan it. */
1047 if (rl == 0) {
1048 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1049 "t:%u bo:%lu\n", toggle, c->bit_offset);
1050 return -1;
1051 }
1052
1053 bits = vli_encode_bits(&bs, rl);
1054 if (bits == -ENOBUFS) /* buffer full */
1055 break;
1056 if (bits <= 0) {
1057 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1058 return 0;
1059 }
1060
1061 toggle = !toggle;
1062 plain_bits += rl;
1063 c->bit_offset = tmp;
1064 } while (c->bit_offset < c->bm_bits);
1065
1066 len = bs.cur.b - p->code + !!bs.cur.bit;
1067
1068 if (plain_bits < (len << 3)) {
1069 /* incompressible with this method.
1070 * we need to rewind both word and bit position. */
1071 c->bit_offset -= plain_bits;
1072 bm_xfer_ctx_bit_to_word_offset(c);
1073 c->bit_offset = c->word_offset * BITS_PER_LONG;
1074 return 0;
1075 }
1076
1077 /* RLE + VLI was able to compress it just fine.
1078 * update c->word_offset. */
1079 bm_xfer_ctx_bit_to_word_offset(c);
1080
1081 /* store pad_bits */
1082 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
1083
1084 return len;
1085}
1086
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001087/**
1088 * send_bitmap_rle_or_plain
1089 *
1090 * Return 0 when done, 1 when another iteration is needed, and a negative error
1091 * code upon failure.
1092 */
1093static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001095 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096{
1097 struct p_compressed_bm *p = (void*)h;
1098 unsigned long num_words;
1099 int len;
1100 int ok;
1101
1102 len = fill_bitmap_rle_bits(mdev, p, c);
1103
1104 if (len < 0)
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001105 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106
1107 if (len) {
1108 DCBP_set_code(p, RLE_VLI_Bits);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001109 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 sizeof(*p) + len, 0);
1111
1112 c->packets[0]++;
1113 c->bytes[0] += sizeof(*p) + len;
1114
1115 if (c->bit_offset >= c->bm_bits)
1116 len = 0; /* DONE */
1117 } else {
1118 /* was not compressible.
1119 * send a buffer full of plain text bits instead. */
1120 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
1121 len = num_words * sizeof(long);
1122 if (len)
1123 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001124 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001125 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001126 c->word_offset += num_words;
1127 c->bit_offset = c->word_offset * BITS_PER_LONG;
1128
1129 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02001130 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131
1132 if (c->bit_offset > c->bm_bits)
1133 c->bit_offset = c->bm_bits;
1134 }
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001135 if (ok) {
1136 if (len == 0) {
1137 INFO_bm_xfer_stats(mdev, "send", c);
1138 return 0;
1139 } else
1140 return 1;
1141 }
1142 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143}
1144
1145/* See the comment at receive_bitmap() */
1146int _drbd_send_bitmap(struct drbd_conf *mdev)
1147{
1148 struct bm_xfer_ctx c;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001149 struct p_header *p;
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001150 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001152 if (!expect(mdev->bitmap))
1153 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154
1155 /* maybe we should use some per thread scratch page,
1156 * and allocate that during initial device creation? */
Philipp Reisnerc0129492011-01-19 16:58:16 +01001157 p = (struct p_header *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001158 if (!p) {
1159 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001160 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 }
1162
1163 if (get_ldev(mdev)) {
1164 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1165 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
1166 drbd_bm_set_all(mdev);
1167 if (drbd_bm_write(mdev)) {
1168 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1169 * but otherwise process as per normal - need to tell other
1170 * side that a full resync is required! */
1171 dev_err(DEV, "Failed to write bitmap to disk!\n");
1172 } else {
1173 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
1174 drbd_md_sync(mdev);
1175 }
1176 }
1177 put_ldev(mdev);
1178 }
1179
1180 c = (struct bm_xfer_ctx) {
1181 .bm_bits = drbd_bm_bits(mdev),
1182 .bm_words = drbd_bm_words(mdev),
1183 };
1184
1185 do {
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001186 err = send_bitmap_rle_or_plain(mdev, p, &c);
1187 } while (err > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188
1189 free_page((unsigned long) p);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001190 return err == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191}
1192
1193int drbd_send_bitmap(struct drbd_conf *mdev)
1194{
1195 int err;
1196
Philipp Reisner61120872011-02-08 09:50:54 +01001197 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198 return -1;
1199 err = !_drbd_send_bitmap(mdev);
Philipp Reisner61120872011-02-08 09:50:54 +01001200 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 return err;
1202}
1203
1204int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
1205{
1206 int ok;
1207 struct p_barrier_ack p;
1208
1209 p.barrier = barrier_nr;
1210 p.set_size = cpu_to_be32(set_size);
1211
1212 if (mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001213 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001214 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215 return ok;
1216}
1217
1218/**
1219 * _drbd_send_ack() - Sends an ack packet
1220 * @mdev: DRBD device.
1221 * @cmd: Packet command code.
1222 * @sector: sector, needs to be in big endian byte order
1223 * @blksize: size in byte, needs to be in big endian byte order
1224 * @block_id: Id, big endian byte order
1225 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001226static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
1227 u64 sector, u32 blksize, u64 block_id)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001228{
1229 int ok;
1230 struct p_block_ack p;
1231
1232 p.sector = sector;
1233 p.block_id = block_id;
1234 p.blksize = blksize;
1235 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
1236
Philipp Reisnere42325a2011-01-19 13:55:45 +01001237 if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001238 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001239 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240 return ok;
1241}
1242
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001243/* dp->sector and dp->block_id already/still in network byte order,
1244 * data_size is payload size according to dp->head,
1245 * and may need to be corrected for digest size. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001246int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001247 struct p_data *dp, int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248{
Philipp Reisnera0638452011-01-19 14:31:32 +01001249 data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1250 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
1252 dp->block_id);
1253}
1254
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001255int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 struct p_block_req *rp)
1257{
1258 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
1259}
1260
1261/**
1262 * drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001263 * @mdev: DRBD device
1264 * @cmd: packet command code
1265 * @peer_req: peer request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001267int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001268 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269{
1270 return _drbd_send_ack(mdev, cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001271 cpu_to_be64(peer_req->i.sector),
1272 cpu_to_be32(peer_req->i.size),
1273 peer_req->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274}
1275
1276/* This function misuses the block_id field to signal if the blocks
1277 * are is sync or not. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001278int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279 sector_t sector, int blksize, u64 block_id)
1280{
1281 return _drbd_send_ack(mdev, cmd,
1282 cpu_to_be64(sector),
1283 cpu_to_be32(blksize),
1284 cpu_to_be64(block_id));
1285}
1286
1287int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1288 sector_t sector, int size, u64 block_id)
1289{
1290 int ok;
1291 struct p_block_req p;
1292
1293 p.sector = cpu_to_be64(sector);
1294 p.block_id = block_id;
1295 p.blksize = cpu_to_be32(size);
1296
Philipp Reisnerc0129492011-01-19 16:58:16 +01001297 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 return ok;
1299}
1300
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001301int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
1302 void *digest, int digest_size, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001303{
1304 int ok;
1305 struct p_block_req p;
1306
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001307 prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001309 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001310 p.blksize = cpu_to_be32(size);
1311
Philipp Reisnere42325a2011-01-19 13:55:45 +01001312 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001314 ok = (sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0));
1315 ok = ok && (digest_size == drbd_send(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001316
Philipp Reisnere42325a2011-01-19 13:55:45 +01001317 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001318
1319 return ok;
1320}
1321
1322int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
1323{
1324 int ok;
1325 struct p_block_req p;
1326
1327 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001328 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329 p.blksize = cpu_to_be32(size);
1330
Philipp Reisnerc0129492011-01-19 16:58:16 +01001331 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001332 return ok;
1333}
1334
1335/* called on sndtimeo
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001336 * returns false if we should retry,
1337 * true if we think connection is dead
Philipp Reisnerb411b362009-09-25 16:07:19 -07001338 */
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001339static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001340{
1341 int drop_it;
1342 /* long elapsed = (long)(jiffies - mdev->last_received); */
1343
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001344 drop_it = tconn->meta.socket == sock
1345 || !tconn->asender.task
1346 || get_t_state(&tconn->asender) != RUNNING
1347 || tconn->volume0->state.conn < C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001348
1349 if (drop_it)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001350 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001352 drop_it = !--tconn->ko_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001353 if (!drop_it) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001354 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1355 current->comm, current->pid, tconn->ko_count);
1356 request_ping(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001357 }
1358
1359 return drop_it; /* && (mdev->state == R_PRIMARY) */;
1360}
1361
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001362static void drbd_update_congested(struct drbd_tconn *tconn)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001363{
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001364 struct sock *sk = tconn->data.socket->sk;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001365 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001366 set_bit(NET_CONGESTED, &tconn->flags);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001367}
1368
Philipp Reisnerb411b362009-09-25 16:07:19 -07001369/* The idea of sendpage seems to be to put some kind of reference
1370 * to the page into the skb, and to hand it over to the NIC. In
1371 * this process get_page() gets called.
1372 *
1373 * As soon as the page was really sent over the network put_page()
1374 * gets called by some part of the network layer. [ NIC driver? ]
1375 *
1376 * [ get_page() / put_page() increment/decrement the count. If count
1377 * reaches 0 the page will be freed. ]
1378 *
1379 * This works nicely with pages from FSs.
1380 * But this means that in protocol A we might signal IO completion too early!
1381 *
1382 * In order not to corrupt data during a resync we must make sure
1383 * that we do not reuse our own buffer pages (EEs) to early, therefore
1384 * we have the net_ee list.
1385 *
1386 * XFS seems to have problems, still, it submits pages with page_count == 0!
1387 * As a workaround, we disable sendpage on pages
1388 * with page_count == 0 or PageSlab.
1389 */
1390static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001391 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001392{
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001393 int sent = drbd_send(mdev->tconn, mdev->tconn->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 kunmap(page);
1395 if (sent == size)
1396 mdev->send_cnt += size>>9;
1397 return sent == size;
1398}
1399
1400static int _drbd_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{
1403 mm_segment_t oldfs = get_fs();
1404 int sent, ok;
1405 int len = size;
1406
1407 /* e.g. XFS meta- & log-data is in slab pages, which have a
1408 * page_count of 0 and/or have PageSlab() set.
1409 * we cannot use send_page for those, as that does get_page();
1410 * put_page(); and would cause either a VM_BUG directly, or
1411 * __page_cache_release a page that would actually still be referenced
1412 * by someone, leading to some obscure delayed Oops somewhere else. */
1413 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001414 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001415
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001416 msg_flags |= MSG_NOSIGNAL;
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001417 drbd_update_congested(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001418 set_fs(KERNEL_DS);
1419 do {
Philipp Reisnere42325a2011-01-19 13:55:45 +01001420 sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001422 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423 if (sent == -EAGAIN) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001424 if (we_should_drop_the_connection(mdev->tconn,
Philipp Reisnere42325a2011-01-19 13:55:45 +01001425 mdev->tconn->data.socket))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426 break;
1427 else
1428 continue;
1429 }
1430 if (sent <= 0) {
1431 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1432 __func__, (int)size, len, sent);
1433 break;
1434 }
1435 len -= sent;
1436 offset += sent;
1437 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
1438 set_fs(oldfs);
Philipp Reisner01a311a2011-02-07 14:30:33 +01001439 clear_bit(NET_CONGESTED, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440
1441 ok = (len == 0);
1442 if (likely(ok))
1443 mdev->send_cnt += size>>9;
1444 return ok;
1445}
1446
1447static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
1448{
1449 struct bio_vec *bvec;
1450 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001451 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001452 __bio_for_each_segment(bvec, bio, i, 0) {
1453 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001454 bvec->bv_offset, bvec->bv_len,
1455 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456 return 0;
1457 }
1458 return 1;
1459}
1460
1461static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
1462{
1463 struct bio_vec *bvec;
1464 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001465 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466 __bio_for_each_segment(bvec, bio, i, 0) {
1467 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001468 bvec->bv_offset, bvec->bv_len,
1469 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001470 return 0;
1471 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001472 return 1;
1473}
1474
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001475static int _drbd_send_zc_ee(struct drbd_conf *mdev,
1476 struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001477{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001478 struct page *page = peer_req->pages;
1479 unsigned len = peer_req->i.size;
1480
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001481 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001482 page_chain_for_each(page) {
1483 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001484 if (!_drbd_send_page(mdev, page, 0, l,
1485 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001486 return 0;
1487 len -= l;
1488 }
1489 return 1;
1490}
1491
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001492static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
1493{
Philipp Reisner31890f42011-01-19 14:12:51 +01001494 if (mdev->tconn->agreed_pro_version >= 95)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001495 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001496 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1497 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1498 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1499 else
Jens Axboe721a9602011-03-09 11:56:30 +01001500 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001501}
1502
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503/* Used to send write requests
1504 * R_PRIMARY -> Peer (P_DATA)
1505 */
1506int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
1507{
1508 int ok = 1;
1509 struct p_data p;
1510 unsigned int dp_flags = 0;
1511 void *dgb;
1512 int dgs;
1513
Philipp Reisner61120872011-02-08 09:50:54 +01001514 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001515 return 0;
1516
Philipp Reisnera0638452011-01-19 14:31:32 +01001517 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1518 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001520 prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size);
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001521 p.sector = cpu_to_be64(req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522 p.block_id = (unsigned long)req;
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001523 p.seq_num = cpu_to_be32(req->seq_num = atomic_add_return(1, &mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001525 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
1526
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 if (mdev->state.conn >= C_SYNC_SOURCE &&
1528 mdev->state.conn <= C_PAUSED_SYNC_T)
1529 dp_flags |= DP_MAY_SET_IN_SYNC;
1530
1531 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 set_bit(UNPLUG_REMOTE, &mdev->flags);
1533 ok = (sizeof(p) ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001534 drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001536 dgb = mdev->tconn->int_dig_out;
1537 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001538 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001539 }
1540 if (ok) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001541 /* For protocol A, we have to memcpy the payload into
1542 * socket buffers, as we may complete right away
1543 * as soon as we handed it over to tcp, at which point the data
1544 * pages may become invalid.
1545 *
1546 * For data-integrity enabled, we copy it as well, so we can be
1547 * sure that even if the bio pages may still be modified, it
1548 * won't change the data on the wire, thus if the digest checks
1549 * out ok after sending on this side, but does not fit on the
1550 * receiving side, we sure have detected corruption elsewhere.
1551 */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001552 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 ok = _drbd_send_bio(mdev, req->master_bio);
1554 else
1555 ok = _drbd_send_zc_bio(mdev, req->master_bio);
Lars Ellenberg470be442010-11-10 10:36:52 +01001556
1557 /* double check digest, sometimes buffers have been modified in flight. */
1558 if (dgs > 0 && dgs <= 64) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001559 /* 64 byte, 512 bit, is the largest digest size
Lars Ellenberg470be442010-11-10 10:36:52 +01001560 * currently supported in kernel crypto. */
1561 unsigned char digest[64];
Philipp Reisnera0638452011-01-19 14:31:32 +01001562 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest);
1563 if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001564 dev_warn(DEV,
1565 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001566 (unsigned long long)req->i.sector, req->i.size);
Lars Ellenberg470be442010-11-10 10:36:52 +01001567 }
1568 } /* else if (dgs > 64) {
1569 ... Be noisy about digest too large ...
1570 } */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571 }
1572
Philipp Reisner61120872011-02-08 09:50:54 +01001573 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001574
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575 return ok;
1576}
1577
1578/* answer packet, used to send data back for read requests:
1579 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1580 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1581 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001582int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001583 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584{
1585 int ok;
1586 struct p_data p;
1587 void *dgb;
1588 int dgs;
1589
Philipp Reisnera0638452011-01-19 14:31:32 +01001590 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1591 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001593 prepare_header(mdev, &p.head, cmd, sizeof(p) -
1594 sizeof(struct p_header80) +
1595 dgs + peer_req->i.size);
1596 p.sector = cpu_to_be64(peer_req->i.sector);
1597 p.block_id = peer_req->block_id;
Andreas Gruenbachercc378272011-01-26 18:01:50 +01001598 p.seq_num = 0; /* unused */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001599
1600 /* Only called by our kernel thread.
1601 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
1602 * in response to admin command or module unload.
1603 */
Philipp Reisner61120872011-02-08 09:50:54 +01001604 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001605 return 0;
1606
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001607 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 -07001608 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001609 dgb = mdev->tconn->int_dig_out;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001610 drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001611 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 }
1613 if (ok)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001614 ok = _drbd_send_zc_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615
Philipp Reisner61120872011-02-08 09:50:54 +01001616 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001617
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618 return ok;
1619}
1620
Philipp Reisner73a01a12010-10-27 14:33:00 +02001621int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
1622{
1623 struct p_block_desc p;
1624
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001625 p.sector = cpu_to_be64(req->i.sector);
1626 p.blksize = cpu_to_be32(req->i.size);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001627
1628 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p));
1629}
1630
Philipp Reisnerb411b362009-09-25 16:07:19 -07001631/*
1632 drbd_send distinguishes two cases:
1633
1634 Packets sent via the data socket "sock"
1635 and packets sent via the meta data socket "msock"
1636
1637 sock msock
1638 -----------------+-------------------------+------------------------------
1639 timeout conf.timeout / 2 conf.timeout / 2
1640 timeout action send a ping via msock Abort communication
1641 and close all sockets
1642*/
1643
1644/*
1645 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1646 */
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001647int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001648 void *buf, size_t size, unsigned msg_flags)
1649{
1650 struct kvec iov;
1651 struct msghdr msg;
1652 int rv, sent = 0;
1653
1654 if (!sock)
1655 return -1000;
1656
1657 /* THINK if (signal_pending) return ... ? */
1658
1659 iov.iov_base = buf;
1660 iov.iov_len = size;
1661
1662 msg.msg_name = NULL;
1663 msg.msg_namelen = 0;
1664 msg.msg_control = NULL;
1665 msg.msg_controllen = 0;
1666 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1667
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001668 if (sock == tconn->data.socket) {
1669 tconn->ko_count = tconn->net_conf->ko_count;
1670 drbd_update_congested(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671 }
1672 do {
1673 /* STRANGE
1674 * tcp_sendmsg does _not_ use its size parameter at all ?
1675 *
1676 * -EAGAIN on timeout, -EINTR on signal.
1677 */
1678/* THINK
1679 * do we need to block DRBD_SIG if sock == &meta.socket ??
1680 * otherwise wake_asender() might interrupt some send_*Ack !
1681 */
1682 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1683 if (rv == -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001684 if (we_should_drop_the_connection(tconn, sock))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685 break;
1686 else
1687 continue;
1688 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 if (rv == -EINTR) {
1690 flush_signals(current);
1691 rv = 0;
1692 }
1693 if (rv < 0)
1694 break;
1695 sent += rv;
1696 iov.iov_base += rv;
1697 iov.iov_len -= rv;
1698 } while (sent < size);
1699
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001700 if (sock == tconn->data.socket)
1701 clear_bit(NET_CONGESTED, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702
1703 if (rv <= 0) {
1704 if (rv != -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001705 conn_err(tconn, "%s_sendmsg returned %d\n",
1706 sock == tconn->meta.socket ? "msock" : "sock",
1707 rv);
1708 drbd_force_state(tconn->volume0, NS(conn, C_BROKEN_PIPE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001709 } else
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001710 drbd_force_state(tconn->volume0, NS(conn, C_TIMEOUT));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001711 }
1712
1713 return sent;
1714}
1715
1716static int drbd_open(struct block_device *bdev, fmode_t mode)
1717{
1718 struct drbd_conf *mdev = bdev->bd_disk->private_data;
1719 unsigned long flags;
1720 int rv = 0;
1721
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001722 mutex_lock(&drbd_main_mutex);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001723 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724 /* to have a stable mdev->state.role
1725 * and no race with updating open_cnt */
1726
1727 if (mdev->state.role != R_PRIMARY) {
1728 if (mode & FMODE_WRITE)
1729 rv = -EROFS;
1730 else if (!allow_oos)
1731 rv = -EMEDIUMTYPE;
1732 }
1733
1734 if (!rv)
1735 mdev->open_cnt++;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001736 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001737 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738
1739 return rv;
1740}
1741
1742static int drbd_release(struct gendisk *gd, fmode_t mode)
1743{
1744 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001745 mutex_lock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001746 mdev->open_cnt--;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001747 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748 return 0;
1749}
1750
Philipp Reisnerb411b362009-09-25 16:07:19 -07001751static void drbd_set_defaults(struct drbd_conf *mdev)
1752{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001753 /* This way we get a compile error when sync_conf grows,
1754 and we forgot to initialize it here */
1755 mdev->sync_conf = (struct syncer_conf) {
1756 /* .rate = */ DRBD_RATE_DEF,
1757 /* .after = */ DRBD_AFTER_DEF,
1758 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001759 /* .verify_alg = */ {}, 0,
1760 /* .cpu_mask = */ {}, 0,
1761 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02001762 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02001763 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
1764 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
1765 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
1766 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001767 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
1768 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001769 };
1770
1771 /* Have to use that way, because the layout differs between
1772 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773 mdev->state = (union drbd_state) {
1774 { .role = R_SECONDARY,
1775 .peer = R_UNKNOWN,
1776 .conn = C_STANDALONE,
1777 .disk = D_DISKLESS,
1778 .pdsk = D_UNKNOWN,
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001779 .susp = 0,
1780 .susp_nod = 0,
1781 .susp_fen = 0
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 } };
1783}
1784
1785void drbd_init_set_defaults(struct drbd_conf *mdev)
1786{
1787 /* the memset(,0,) did most of this.
1788 * note: only assignments, no allocation in here */
1789
1790 drbd_set_defaults(mdev);
1791
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792 atomic_set(&mdev->ap_bio_cnt, 0);
1793 atomic_set(&mdev->ap_pending_cnt, 0);
1794 atomic_set(&mdev->rs_pending_cnt, 0);
1795 atomic_set(&mdev->unacked_cnt, 0);
1796 atomic_set(&mdev->local_cnt, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797 atomic_set(&mdev->pp_in_use, 0);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001798 atomic_set(&mdev->pp_in_use_by_net, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02001799 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001800 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001801 atomic_set(&mdev->ap_in_flight, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802
1803 mutex_init(&mdev->md_io_mutex);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001804 mutex_init(&mdev->tconn->data.mutex);
1805 mutex_init(&mdev->tconn->meta.mutex);
1806 sema_init(&mdev->tconn->data.work.s, 0);
1807 sema_init(&mdev->tconn->meta.work.s, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 mutex_init(&mdev->state_mutex);
1809
Philipp Reisnere42325a2011-01-19 13:55:45 +01001810 spin_lock_init(&mdev->tconn->data.work.q_lock);
1811 spin_lock_init(&mdev->tconn->meta.work.q_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001812
1813 spin_lock_init(&mdev->al_lock);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001814 spin_lock_init(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001815 spin_lock_init(&mdev->peer_seq_lock);
1816 spin_lock_init(&mdev->epoch_lock);
1817
1818 INIT_LIST_HEAD(&mdev->active_ee);
1819 INIT_LIST_HEAD(&mdev->sync_ee);
1820 INIT_LIST_HEAD(&mdev->done_ee);
1821 INIT_LIST_HEAD(&mdev->read_ee);
1822 INIT_LIST_HEAD(&mdev->net_ee);
1823 INIT_LIST_HEAD(&mdev->resync_reads);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001824 INIT_LIST_HEAD(&mdev->tconn->data.work.q);
1825 INIT_LIST_HEAD(&mdev->tconn->meta.work.q);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001826 INIT_LIST_HEAD(&mdev->resync_work.list);
1827 INIT_LIST_HEAD(&mdev->unplug_work.list);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001828 INIT_LIST_HEAD(&mdev->go_diskless.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829 INIT_LIST_HEAD(&mdev->md_sync_work.list);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001830 INIT_LIST_HEAD(&mdev->start_resync_work.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02001832
Philipp Reisner794abb72010-12-27 11:51:23 +01001833 mdev->resync_work.cb = w_resync_timer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 mdev->unplug_work.cb = w_send_write_hint;
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001835 mdev->go_diskless.cb = w_go_diskless;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836 mdev->md_sync_work.cb = w_md_sync;
1837 mdev->bm_io_work.w.cb = w_bitmap_io;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001838 mdev->start_resync_work.cb = w_start_resync;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001839
1840 mdev->resync_work.mdev = mdev;
1841 mdev->unplug_work.mdev = mdev;
1842 mdev->go_diskless.mdev = mdev;
1843 mdev->md_sync_work.mdev = mdev;
1844 mdev->bm_io_work.w.mdev = mdev;
1845 mdev->start_resync_work.mdev = mdev;
1846
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847 init_timer(&mdev->resync_timer);
1848 init_timer(&mdev->md_sync_timer);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001849 init_timer(&mdev->start_resync_timer);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001850 init_timer(&mdev->request_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 mdev->resync_timer.function = resync_timer_fn;
1852 mdev->resync_timer.data = (unsigned long) mdev;
1853 mdev->md_sync_timer.function = md_sync_timer_fn;
1854 mdev->md_sync_timer.data = (unsigned long) mdev;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001855 mdev->start_resync_timer.function = start_resync_timer_fn;
1856 mdev->start_resync_timer.data = (unsigned long) mdev;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001857 mdev->request_timer.function = request_timer_fn;
1858 mdev->request_timer.data = (unsigned long) mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859
1860 init_waitqueue_head(&mdev->misc_wait);
1861 init_waitqueue_head(&mdev->state_wait);
1862 init_waitqueue_head(&mdev->ee_wait);
1863 init_waitqueue_head(&mdev->al_wait);
1864 init_waitqueue_head(&mdev->seq_wait);
1865
Philipp Reisnerbed879a2011-02-04 14:00:37 +01001866 drbd_thread_init(mdev, &mdev->tconn->receiver, drbdd_init, "receiver");
1867 drbd_thread_init(mdev, &mdev->tconn->worker, drbd_worker, "worker");
1868 drbd_thread_init(mdev, &mdev->tconn->asender, drbd_asender, "asender");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001869
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001870 /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */
Philipp Reisner2451fc32010-08-24 13:43:11 +02001871 mdev->write_ordering = WO_bdev_flush;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872 mdev->resync_wenr = LC_FREE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001873 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1874 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875}
1876
1877void drbd_mdev_cleanup(struct drbd_conf *mdev)
1878{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001879 int i;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001880 if (mdev->tconn->receiver.t_state != NONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001881 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001882 mdev->tconn->receiver.t_state);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001883
1884 /* no need to lock it, I'm the only thread alive */
1885 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
1886 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
1887 mdev->al_writ_cnt =
1888 mdev->bm_writ_cnt =
1889 mdev->read_cnt =
1890 mdev->recv_cnt =
1891 mdev->send_cnt =
1892 mdev->writ_cnt =
1893 mdev->p_size =
1894 mdev->rs_start =
1895 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001896 mdev->rs_failed = 0;
1897 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001898 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001899 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1900 mdev->rs_mark_left[i] = 0;
1901 mdev->rs_mark_time[i] = 0;
1902 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01001903 D_ASSERT(mdev->tconn->net_conf == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001904
1905 drbd_set_my_capacity(mdev, 0);
1906 if (mdev->bitmap) {
1907 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01001908 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001909 drbd_bm_cleanup(mdev);
1910 }
1911
1912 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02001913 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001914
1915 /*
1916 * currently we drbd_init_ee only on module load, so
1917 * we may do drbd_release_ee only on module unload!
1918 */
1919 D_ASSERT(list_empty(&mdev->active_ee));
1920 D_ASSERT(list_empty(&mdev->sync_ee));
1921 D_ASSERT(list_empty(&mdev->done_ee));
1922 D_ASSERT(list_empty(&mdev->read_ee));
1923 D_ASSERT(list_empty(&mdev->net_ee));
1924 D_ASSERT(list_empty(&mdev->resync_reads));
Philipp Reisnere42325a2011-01-19 13:55:45 +01001925 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
1926 D_ASSERT(list_empty(&mdev->tconn->meta.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001927 D_ASSERT(list_empty(&mdev->resync_work.list));
1928 D_ASSERT(list_empty(&mdev->unplug_work.list));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001929 D_ASSERT(list_empty(&mdev->go_diskless.list));
Lars Ellenberg2265b472010-12-16 15:41:26 +01001930
1931 drbd_set_defaults(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932}
1933
1934
1935static void drbd_destroy_mempools(void)
1936{
1937 struct page *page;
1938
1939 while (drbd_pp_pool) {
1940 page = drbd_pp_pool;
1941 drbd_pp_pool = (struct page *)page_private(page);
1942 __free_page(page);
1943 drbd_pp_vacant--;
1944 }
1945
1946 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
1947
1948 if (drbd_ee_mempool)
1949 mempool_destroy(drbd_ee_mempool);
1950 if (drbd_request_mempool)
1951 mempool_destroy(drbd_request_mempool);
1952 if (drbd_ee_cache)
1953 kmem_cache_destroy(drbd_ee_cache);
1954 if (drbd_request_cache)
1955 kmem_cache_destroy(drbd_request_cache);
1956 if (drbd_bm_ext_cache)
1957 kmem_cache_destroy(drbd_bm_ext_cache);
1958 if (drbd_al_ext_cache)
1959 kmem_cache_destroy(drbd_al_ext_cache);
1960
1961 drbd_ee_mempool = NULL;
1962 drbd_request_mempool = NULL;
1963 drbd_ee_cache = NULL;
1964 drbd_request_cache = NULL;
1965 drbd_bm_ext_cache = NULL;
1966 drbd_al_ext_cache = NULL;
1967
1968 return;
1969}
1970
1971static int drbd_create_mempools(void)
1972{
1973 struct page *page;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001974 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001975 int i;
1976
1977 /* prepare our caches and mempools */
1978 drbd_request_mempool = NULL;
1979 drbd_ee_cache = NULL;
1980 drbd_request_cache = NULL;
1981 drbd_bm_ext_cache = NULL;
1982 drbd_al_ext_cache = NULL;
1983 drbd_pp_pool = NULL;
1984
1985 /* caches */
1986 drbd_request_cache = kmem_cache_create(
1987 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
1988 if (drbd_request_cache == NULL)
1989 goto Enomem;
1990
1991 drbd_ee_cache = kmem_cache_create(
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001992 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993 if (drbd_ee_cache == NULL)
1994 goto Enomem;
1995
1996 drbd_bm_ext_cache = kmem_cache_create(
1997 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
1998 if (drbd_bm_ext_cache == NULL)
1999 goto Enomem;
2000
2001 drbd_al_ext_cache = kmem_cache_create(
2002 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2003 if (drbd_al_ext_cache == NULL)
2004 goto Enomem;
2005
2006 /* mempools */
2007 drbd_request_mempool = mempool_create(number,
2008 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2009 if (drbd_request_mempool == NULL)
2010 goto Enomem;
2011
2012 drbd_ee_mempool = mempool_create(number,
2013 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
Nicolas Kaiser2027ae12010-10-28 06:15:26 -06002014 if (drbd_ee_mempool == NULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002015 goto Enomem;
2016
2017 /* drbd's page pool */
2018 spin_lock_init(&drbd_pp_lock);
2019
2020 for (i = 0; i < number; i++) {
2021 page = alloc_page(GFP_HIGHUSER);
2022 if (!page)
2023 goto Enomem;
2024 set_page_private(page, (unsigned long)drbd_pp_pool);
2025 drbd_pp_pool = page;
2026 }
2027 drbd_pp_vacant = number;
2028
2029 return 0;
2030
2031Enomem:
2032 drbd_destroy_mempools(); /* in case we allocated some */
2033 return -ENOMEM;
2034}
2035
2036static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2037 void *unused)
2038{
2039 /* just so we have it. you never know what interesting things we
2040 * might want to do here some day...
2041 */
2042
2043 return NOTIFY_DONE;
2044}
2045
2046static struct notifier_block drbd_notifier = {
2047 .notifier_call = drbd_notify_sys,
2048};
2049
2050static void drbd_release_ee_lists(struct drbd_conf *mdev)
2051{
2052 int rr;
2053
2054 rr = drbd_release_ee(mdev, &mdev->active_ee);
2055 if (rr)
2056 dev_err(DEV, "%d EEs in active list found!\n", rr);
2057
2058 rr = drbd_release_ee(mdev, &mdev->sync_ee);
2059 if (rr)
2060 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2061
2062 rr = drbd_release_ee(mdev, &mdev->read_ee);
2063 if (rr)
2064 dev_err(DEV, "%d EEs in read list found!\n", rr);
2065
2066 rr = drbd_release_ee(mdev, &mdev->done_ee);
2067 if (rr)
2068 dev_err(DEV, "%d EEs in done list found!\n", rr);
2069
2070 rr = drbd_release_ee(mdev, &mdev->net_ee);
2071 if (rr)
2072 dev_err(DEV, "%d EEs in net list found!\n", rr);
2073}
2074
2075/* caution. no locking.
2076 * currently only used from module cleanup code. */
2077static void drbd_delete_device(unsigned int minor)
2078{
2079 struct drbd_conf *mdev = minor_to_mdev(minor);
2080
2081 if (!mdev)
2082 return;
2083
2084 /* paranoia asserts */
Andreas Gruenbacher70dc65e2010-12-21 14:46:57 +01002085 D_ASSERT(mdev->open_cnt == 0);
Philipp Reisnere42325a2011-01-19 13:55:45 +01002086 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002087 /* end paranoia asserts */
2088
2089 del_gendisk(mdev->vdisk);
2090
2091 /* cleanup stuff that may have been allocated during
2092 * device (re-)configuration or state changes */
2093
2094 if (mdev->this_bdev)
2095 bdput(mdev->this_bdev);
2096
2097 drbd_free_resources(mdev);
Philipp Reisner21114382011-01-19 12:26:59 +01002098 drbd_free_tconn(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002099
2100 drbd_release_ee_lists(mdev);
2101
Philipp Reisnerb411b362009-09-25 16:07:19 -07002102 lc_destroy(mdev->act_log);
2103 lc_destroy(mdev->resync);
2104
2105 kfree(mdev->p_uuid);
2106 /* mdev->p_uuid = NULL; */
2107
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108 /* cleanup the rest that has been
2109 * allocated from drbd_new_device
2110 * and actually free the mdev itself */
2111 drbd_free_mdev(mdev);
2112}
2113
2114static void drbd_cleanup(void)
2115{
2116 unsigned int i;
2117
2118 unregister_reboot_notifier(&drbd_notifier);
2119
Lars Ellenberg17a93f32010-11-24 10:37:35 +01002120 /* first remove proc,
2121 * drbdsetup uses it's presence to detect
2122 * whether DRBD is loaded.
2123 * If we would get stuck in proc removal,
2124 * but have netlink already deregistered,
2125 * some drbdsetup commands may wait forever
2126 * for an answer.
2127 */
2128 if (drbd_proc)
2129 remove_proc_entry("drbd", NULL);
2130
Philipp Reisnerb411b362009-09-25 16:07:19 -07002131 drbd_nl_cleanup();
2132
2133 if (minor_table) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134 i = minor_count;
2135 while (i--)
2136 drbd_delete_device(i);
2137 drbd_destroy_mempools();
2138 }
2139
2140 kfree(minor_table);
2141
2142 unregister_blkdev(DRBD_MAJOR, "drbd");
2143
2144 printk(KERN_INFO "drbd: module cleanup done.\n");
2145}
2146
2147/**
2148 * drbd_congested() - Callback for pdflush
2149 * @congested_data: User data
2150 * @bdi_bits: Bits pdflush is currently interested in
2151 *
2152 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2153 */
2154static int drbd_congested(void *congested_data, int bdi_bits)
2155{
2156 struct drbd_conf *mdev = congested_data;
2157 struct request_queue *q;
2158 char reason = '-';
2159 int r = 0;
2160
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002161 if (!may_inc_ap_bio(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002162 /* DRBD has frozen IO */
2163 r = bdi_bits;
2164 reason = 'd';
2165 goto out;
2166 }
2167
2168 if (get_ldev(mdev)) {
2169 q = bdev_get_queue(mdev->ldev->backing_bdev);
2170 r = bdi_congested(&q->backing_dev_info, bdi_bits);
2171 put_ldev(mdev);
2172 if (r)
2173 reason = 'b';
2174 }
2175
Philipp Reisner01a311a2011-02-07 14:30:33 +01002176 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002177 r |= (1 << BDI_async_congested);
2178 reason = reason == 'b' ? 'a' : 'n';
2179 }
2180
2181out:
2182 mdev->congestion_reason = reason;
2183 return r;
2184}
2185
Philipp Reisner21114382011-01-19 12:26:59 +01002186struct drbd_tconn *drbd_new_tconn(char *name)
2187{
2188 struct drbd_tconn *tconn;
2189
2190 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2191 if (!tconn)
2192 return NULL;
2193
2194 tconn->name = kstrdup(name, GFP_KERNEL);
2195 if (!tconn->name)
2196 goto fail;
2197
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002198 atomic_set(&tconn->net_cnt, 0);
2199 init_waitqueue_head(&tconn->net_cnt_wait);
Philipp Reisner062e8792011-02-08 11:09:18 +01002200 idr_init(&tconn->volumes);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002201
Philipp Reisner21114382011-01-19 12:26:59 +01002202 write_lock_irq(&global_state_lock);
2203 list_add(&tconn->all_tconn, &drbd_tconns);
2204 write_unlock_irq(&global_state_lock);
2205
2206 return tconn;
2207
2208fail:
2209 kfree(tconn->name);
2210 kfree(tconn);
2211
2212 return NULL;
2213}
2214
2215void drbd_free_tconn(struct drbd_tconn *tconn)
2216{
2217 write_lock_irq(&global_state_lock);
2218 list_del(&tconn->all_tconn);
2219 write_unlock_irq(&global_state_lock);
Philipp Reisner062e8792011-02-08 11:09:18 +01002220 idr_destroy(&tconn->volumes);
Philipp Reisner21114382011-01-19 12:26:59 +01002221
2222 kfree(tconn->name);
Philipp Reisnerb42a70a2011-01-27 10:55:20 +01002223 kfree(tconn->int_dig_out);
2224 kfree(tconn->int_dig_in);
2225 kfree(tconn->int_dig_vv);
Philipp Reisner21114382011-01-19 12:26:59 +01002226 kfree(tconn);
2227}
2228
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229struct drbd_conf *drbd_new_device(unsigned int minor)
2230{
2231 struct drbd_conf *mdev;
2232 struct gendisk *disk;
2233 struct request_queue *q;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002234 char conn_name[9]; /* drbd1234N */
Philipp Reisner062e8792011-02-08 11:09:18 +01002235 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236
2237 /* GFP_KERNEL, we are outside of all write-out paths */
2238 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
2239 if (!mdev)
2240 return NULL;
Philipp Reisner60ae4962011-02-07 14:01:51 +01002241 sprintf(conn_name, "drbd%d", minor);
2242 mdev->tconn = drbd_new_tconn(conn_name);
Philipp Reisner21114382011-01-19 12:26:59 +01002243 if (!mdev->tconn)
2244 goto out_no_tconn;
Philipp Reisner062e8792011-02-08 11:09:18 +01002245 if (!idr_pre_get(&mdev->tconn->volumes, GFP_KERNEL))
2246 goto out_no_cpumask;
2247 if (idr_get_new(&mdev->tconn->volumes, mdev, &vnr))
2248 goto out_no_cpumask;
2249 if (vnr != 0) {
2250 dev_err(DEV, "vnr = %d\n", vnr);
2251 goto out_no_cpumask;
2252 }
Philipp Reisner80822282011-02-08 12:46:30 +01002253 if (!zalloc_cpumask_var(&mdev->tconn->cpu_mask, GFP_KERNEL))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002254 goto out_no_cpumask;
2255
Philipp Reisner21114382011-01-19 12:26:59 +01002256 mdev->tconn->volume0 = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002257 mdev->minor = minor;
2258
2259 drbd_init_set_defaults(mdev);
2260
2261 q = blk_alloc_queue(GFP_KERNEL);
2262 if (!q)
2263 goto out_no_q;
2264 mdev->rq_queue = q;
2265 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266
2267 disk = alloc_disk(1);
2268 if (!disk)
2269 goto out_no_disk;
2270 mdev->vdisk = disk;
2271
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002272 set_disk_ro(disk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002273
2274 disk->queue = q;
2275 disk->major = DRBD_MAJOR;
2276 disk->first_minor = minor;
2277 disk->fops = &drbd_ops;
2278 sprintf(disk->disk_name, "drbd%d", minor);
2279 disk->private_data = mdev;
2280
2281 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
2282 /* we have no partitions. we contain only ourselves. */
2283 mdev->this_bdev->bd_contains = mdev->this_bdev;
2284
2285 q->backing_dev_info.congested_fn = drbd_congested;
2286 q->backing_dev_info.congested_data = mdev;
2287
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01002288 blk_queue_make_request(q, drbd_make_request);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002289 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2290 This triggers a max_bio_size message upon first attach or connect */
2291 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002292 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2293 blk_queue_merge_bvec(q, drbd_merge_bvec);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002294 q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002295
2296 mdev->md_io_page = alloc_page(GFP_KERNEL);
2297 if (!mdev->md_io_page)
2298 goto out_no_io_page;
2299
2300 if (drbd_bm_init(mdev))
2301 goto out_no_bitmap;
2302 /* no need to lock access, we are still initializing this minor device. */
2303 if (!tl_init(mdev))
2304 goto out_no_tl;
Andreas Gruenbacherdac13892011-01-21 17:18:39 +01002305 mdev->read_requests = RB_ROOT;
Andreas Gruenbacherde696712011-01-20 15:00:24 +01002306 mdev->write_requests = RB_ROOT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2309 if (!mdev->current_epoch)
2310 goto out_no_epoch;
2311
2312 INIT_LIST_HEAD(&mdev->current_epoch->list);
2313 mdev->epochs = 1;
2314
2315 return mdev;
2316
2317/* out_whatever_else:
2318 kfree(mdev->current_epoch); */
2319out_no_epoch:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002320 tl_cleanup(mdev);
2321out_no_tl:
2322 drbd_bm_cleanup(mdev);
2323out_no_bitmap:
2324 __free_page(mdev->md_io_page);
2325out_no_io_page:
2326 put_disk(disk);
2327out_no_disk:
2328 blk_cleanup_queue(q);
2329out_no_q:
Philipp Reisner80822282011-02-08 12:46:30 +01002330 free_cpumask_var(mdev->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002331out_no_cpumask:
Philipp Reisner21114382011-01-19 12:26:59 +01002332 drbd_free_tconn(mdev->tconn);
2333out_no_tconn:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334 kfree(mdev);
2335 return NULL;
2336}
2337
2338/* counterpart of drbd_new_device.
2339 * last part of drbd_delete_device. */
2340void drbd_free_mdev(struct drbd_conf *mdev)
2341{
2342 kfree(mdev->current_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002343 tl_cleanup(mdev);
2344 if (mdev->bitmap) /* should no longer be there. */
2345 drbd_bm_cleanup(mdev);
2346 __free_page(mdev->md_io_page);
2347 put_disk(mdev->vdisk);
2348 blk_cleanup_queue(mdev->rq_queue);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002349 kfree(mdev);
2350}
2351
2352
2353int __init drbd_init(void)
2354{
2355 int err;
2356
Philipp Reisnerfd340c12011-01-19 16:57:39 +01002357 BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95));
2358 BUILD_BUG_ON(sizeof(struct p_handshake) != 80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359
Philipp Reisner2b8a90b2011-01-10 11:15:17 +01002360 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361 printk(KERN_ERR
2362 "drbd: invalid minor_count (%d)\n", minor_count);
2363#ifdef MODULE
2364 return -EINVAL;
2365#else
2366 minor_count = 8;
2367#endif
2368 }
2369
2370 err = drbd_nl_init();
2371 if (err)
2372 return err;
2373
2374 err = register_blkdev(DRBD_MAJOR, "drbd");
2375 if (err) {
2376 printk(KERN_ERR
2377 "drbd: unable to register block device major %d\n",
2378 DRBD_MAJOR);
2379 return err;
2380 }
2381
2382 register_reboot_notifier(&drbd_notifier);
2383
2384 /*
2385 * allocate all necessary structs
2386 */
2387 err = -ENOMEM;
2388
2389 init_waitqueue_head(&drbd_pp_wait);
2390
2391 drbd_proc = NULL; /* play safe for drbd_cleanup */
2392 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
2393 GFP_KERNEL);
2394 if (!minor_table)
2395 goto Enomem;
2396
2397 err = drbd_create_mempools();
2398 if (err)
2399 goto Enomem;
2400
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01002401 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002402 if (!drbd_proc) {
2403 printk(KERN_ERR "drbd: unable to register proc file\n");
2404 goto Enomem;
2405 }
2406
2407 rwlock_init(&global_state_lock);
Philipp Reisner21114382011-01-19 12:26:59 +01002408 INIT_LIST_HEAD(&drbd_tconns);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002409
2410 printk(KERN_INFO "drbd: initialized. "
2411 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2412 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2413 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2414 printk(KERN_INFO "drbd: registered as block device major %d\n",
2415 DRBD_MAJOR);
2416 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
2417
2418 return 0; /* Success! */
2419
2420Enomem:
2421 drbd_cleanup();
2422 if (err == -ENOMEM)
2423 /* currently always the case */
2424 printk(KERN_ERR "drbd: ran out of memory\n");
2425 else
2426 printk(KERN_ERR "drbd: initialization failure\n");
2427 return err;
2428}
2429
2430void drbd_free_bc(struct drbd_backing_dev *ldev)
2431{
2432 if (ldev == NULL)
2433 return;
2434
Tejun Heoe525fd82010-11-13 11:55:17 +01002435 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2436 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437
2438 kfree(ldev);
2439}
2440
Philipp Reisner360cc742011-02-08 14:29:53 +01002441void drbd_free_sock(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002442{
Philipp Reisner360cc742011-02-08 14:29:53 +01002443 if (tconn->data.socket) {
2444 mutex_lock(&tconn->data.mutex);
2445 kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
2446 sock_release(tconn->data.socket);
2447 tconn->data.socket = NULL;
2448 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002449 }
Philipp Reisner360cc742011-02-08 14:29:53 +01002450 if (tconn->meta.socket) {
2451 mutex_lock(&tconn->meta.mutex);
2452 kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
2453 sock_release(tconn->meta.socket);
2454 tconn->meta.socket = NULL;
2455 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002456 }
2457}
2458
2459
2460void drbd_free_resources(struct drbd_conf *mdev)
2461{
2462 crypto_free_hash(mdev->csums_tfm);
2463 mdev->csums_tfm = NULL;
2464 crypto_free_hash(mdev->verify_tfm);
2465 mdev->verify_tfm = NULL;
Philipp Reisnera0638452011-01-19 14:31:32 +01002466 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
2467 mdev->tconn->cram_hmac_tfm = NULL;
2468 crypto_free_hash(mdev->tconn->integrity_w_tfm);
2469 mdev->tconn->integrity_w_tfm = NULL;
2470 crypto_free_hash(mdev->tconn->integrity_r_tfm);
2471 mdev->tconn->integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002472
Philipp Reisner360cc742011-02-08 14:29:53 +01002473 drbd_free_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002474
2475 __no_warn(local,
2476 drbd_free_bc(mdev->ldev);
2477 mdev->ldev = NULL;);
2478}
2479
2480/* meta data management */
2481
2482struct meta_data_on_disk {
2483 u64 la_size; /* last agreed size. */
2484 u64 uuid[UI_SIZE]; /* UUIDs. */
2485 u64 device_uuid;
2486 u64 reserved_u64_1;
2487 u32 flags; /* MDF */
2488 u32 magic;
2489 u32 md_size_sect;
2490 u32 al_offset; /* offset to this block */
2491 u32 al_nr_extents; /* important for restoring the AL */
2492 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
2493 u32 bm_offset; /* offset to the bitmap, from here */
2494 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
Philipp Reisner99432fc2011-05-20 16:39:13 +02002495 u32 la_peer_max_bio_size; /* last peer max_bio_size */
2496 u32 reserved_u32[3];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497
2498} __packed;
2499
2500/**
2501 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2502 * @mdev: DRBD device.
2503 */
2504void drbd_md_sync(struct drbd_conf *mdev)
2505{
2506 struct meta_data_on_disk *buffer;
2507 sector_t sector;
2508 int i;
2509
Lars Ellenbergee15b032010-09-03 10:00:09 +02002510 del_timer(&mdev->md_sync_timer);
2511 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002512 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
2513 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002514
2515 /* We use here D_FAILED and not D_ATTACHING because we try to write
2516 * metadata even if we detach due to a disk failure! */
2517 if (!get_ldev_if_state(mdev, D_FAILED))
2518 return;
2519
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520 mutex_lock(&mdev->md_io_mutex);
2521 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2522 memset(buffer, 0, 512);
2523
2524 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
2525 for (i = UI_CURRENT; i < UI_SIZE; i++)
2526 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
2527 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
2528 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
2529
2530 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
2531 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
2532 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
2533 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
2534 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
2535
2536 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002537 buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002538
2539 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
2540 sector = mdev->ldev->md.md_offset;
2541
Lars Ellenberg3f3a9b82010-09-01 15:12:12 +02002542 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002543 /* this was a try anyways ... */
2544 dev_err(DEV, "meta data update failed!\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002545 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546 }
2547
2548 /* Update mdev->ldev->md.la_size_sect,
2549 * since we updated it on metadata. */
2550 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
2551
2552 mutex_unlock(&mdev->md_io_mutex);
2553 put_ldev(mdev);
2554}
2555
2556/**
2557 * drbd_md_read() - Reads in the meta data super block
2558 * @mdev: DRBD device.
2559 * @bdev: Device from which the meta data should be read in.
2560 *
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01002561 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
Philipp Reisnerb411b362009-09-25 16:07:19 -07002562 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
2563 */
2564int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
2565{
2566 struct meta_data_on_disk *buffer;
2567 int i, rv = NO_ERROR;
2568
2569 if (!get_ldev_if_state(mdev, D_ATTACHING))
2570 return ERR_IO_MD_DISK;
2571
Philipp Reisnerb411b362009-09-25 16:07:19 -07002572 mutex_lock(&mdev->md_io_mutex);
2573 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2574
2575 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002576 /* NOTE: can't do normal error processing here as this is
Philipp Reisnerb411b362009-09-25 16:07:19 -07002577 called BEFORE disk is attached */
2578 dev_err(DEV, "Error while reading metadata.\n");
2579 rv = ERR_IO_MD_DISK;
2580 goto err;
2581 }
2582
Andreas Gruenbachere7fad8a2011-01-11 13:54:02 +01002583 if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002584 dev_err(DEV, "Error while reading metadata, magic not found.\n");
2585 rv = ERR_MD_INVALID;
2586 goto err;
2587 }
2588 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
2589 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
2590 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
2591 rv = ERR_MD_INVALID;
2592 goto err;
2593 }
2594 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
2595 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
2596 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
2597 rv = ERR_MD_INVALID;
2598 goto err;
2599 }
2600 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
2601 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
2602 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
2603 rv = ERR_MD_INVALID;
2604 goto err;
2605 }
2606
2607 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
2608 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
2609 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
2610 rv = ERR_MD_INVALID;
2611 goto err;
2612 }
2613
2614 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
2615 for (i = UI_CURRENT; i < UI_SIZE; i++)
2616 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
2617 bdev->md.flags = be32_to_cpu(buffer->flags);
2618 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
2619 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
2620
Philipp Reisner87eeee42011-01-19 14:16:30 +01002621 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002622 if (mdev->state.conn < C_CONNECTED) {
2623 int peer;
2624 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
2625 peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
2626 mdev->peer_max_bio_size = peer;
2627 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002628 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002629
Philipp Reisnerb411b362009-09-25 16:07:19 -07002630 if (mdev->sync_conf.al_extents < 7)
2631 mdev->sync_conf.al_extents = 127;
2632
2633 err:
2634 mutex_unlock(&mdev->md_io_mutex);
2635 put_ldev(mdev);
2636
2637 return rv;
2638}
2639
2640/**
2641 * drbd_md_mark_dirty() - Mark meta data super block as dirty
2642 * @mdev: DRBD device.
2643 *
2644 * Call this function if you change anything that should be written to
2645 * the meta-data super block. This function sets MD_DIRTY, and starts a
2646 * timer that ensures that within five seconds you have to call drbd_md_sync().
2647 */
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002648#ifdef DEBUG
Lars Ellenbergee15b032010-09-03 10:00:09 +02002649void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
2650{
2651 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
2652 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
2653 mdev->last_md_mark_dirty.line = line;
2654 mdev->last_md_mark_dirty.func = func;
2655 }
2656}
2657#else
Philipp Reisnerb411b362009-09-25 16:07:19 -07002658void drbd_md_mark_dirty(struct drbd_conf *mdev)
2659{
Lars Ellenbergee15b032010-09-03 10:00:09 +02002660 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002661 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002662}
Lars Ellenbergee15b032010-09-03 10:00:09 +02002663#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002664
2665static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
2666{
2667 int i;
2668
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002669 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002670 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002671}
2672
2673void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2674{
2675 if (idx == UI_CURRENT) {
2676 if (mdev->state.role == R_PRIMARY)
2677 val |= 1;
2678 else
2679 val &= ~((u64)1);
2680
2681 drbd_set_ed_uuid(mdev, val);
2682 }
2683
2684 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002685 drbd_md_mark_dirty(mdev);
2686}
2687
2688
2689void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2690{
2691 if (mdev->ldev->md.uuid[idx]) {
2692 drbd_uuid_move_history(mdev);
2693 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002694 }
2695 _drbd_uuid_set(mdev, idx, val);
2696}
2697
2698/**
2699 * drbd_uuid_new_current() - Creates a new current UUID
2700 * @mdev: DRBD device.
2701 *
2702 * Creates a new current UUID, and rotates the old current UUID into
2703 * the bitmap slot. Causes an incremental resync upon next connect.
2704 */
2705void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
2706{
2707 u64 val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002708 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002709
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002710 if (bm_uuid)
2711 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
2712
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002714
2715 get_random_bytes(&val, sizeof(u64));
2716 _drbd_uuid_set(mdev, UI_CURRENT, val);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002717 drbd_print_uuids(mdev, "new current UUID");
Lars Ellenbergaaa8e2b2010-10-15 13:16:53 +02002718 /* get it to stable storage _now_ */
2719 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002720}
2721
2722void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
2723{
2724 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
2725 return;
2726
2727 if (val == 0) {
2728 drbd_uuid_move_history(mdev);
2729 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2730 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002731 } else {
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002732 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
2733 if (bm_uuid)
2734 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002735
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002736 mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002737 }
2738 drbd_md_mark_dirty(mdev);
2739}
2740
2741/**
2742 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2743 * @mdev: DRBD device.
2744 *
2745 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
2746 */
2747int drbd_bmio_set_n_write(struct drbd_conf *mdev)
2748{
2749 int rv = -EIO;
2750
2751 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2752 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
2753 drbd_md_sync(mdev);
2754 drbd_bm_set_all(mdev);
2755
2756 rv = drbd_bm_write(mdev);
2757
2758 if (!rv) {
2759 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2760 drbd_md_sync(mdev);
2761 }
2762
2763 put_ldev(mdev);
2764 }
2765
2766 return rv;
2767}
2768
2769/**
2770 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2771 * @mdev: DRBD device.
2772 *
2773 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
2774 */
2775int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
2776{
2777 int rv = -EIO;
2778
Philipp Reisner07782862010-08-31 12:00:50 +02002779 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002780 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2781 drbd_bm_clear_all(mdev);
2782 rv = drbd_bm_write(mdev);
2783 put_ldev(mdev);
2784 }
2785
2786 return rv;
2787}
2788
2789static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2790{
2791 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002792 int rv = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002793
2794 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
2795
Lars Ellenberg02851e92010-12-16 14:47:39 +01002796 if (get_ldev(mdev)) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002797 drbd_bm_lock(mdev, work->why, work->flags);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002798 rv = work->io_fn(mdev);
2799 drbd_bm_unlock(mdev);
2800 put_ldev(mdev);
2801 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002802
2803 clear_bit(BITMAP_IO, &mdev->flags);
Philipp Reisner127b3172010-11-16 10:07:53 +01002804 smp_mb__after_clear_bit();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002805 wake_up(&mdev->misc_wait);
2806
2807 if (work->done)
2808 work->done(mdev, rv);
2809
2810 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
2811 work->why = NULL;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002812 work->flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002813
2814 return 1;
2815}
2816
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002817void drbd_ldev_destroy(struct drbd_conf *mdev)
2818{
2819 lc_destroy(mdev->resync);
2820 mdev->resync = NULL;
2821 lc_destroy(mdev->act_log);
2822 mdev->act_log = NULL;
2823 __no_warn(local,
2824 drbd_free_bc(mdev->ldev);
2825 mdev->ldev = NULL;);
2826
2827 if (mdev->md_io_tmpp) {
2828 __free_page(mdev->md_io_tmpp);
2829 mdev->md_io_tmpp = NULL;
2830 }
2831 clear_bit(GO_DISKLESS, &mdev->flags);
2832}
2833
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002834static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2835{
2836 D_ASSERT(mdev->state.disk == D_FAILED);
Lars Ellenberg9d282872010-10-14 13:57:07 +02002837 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
2838 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002839 * the protected members anymore, though, so once put_ldev reaches zero
2840 * again, it will be safe to free them. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002841 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002842 return 1;
2843}
2844
2845void drbd_go_diskless(struct drbd_conf *mdev)
2846{
2847 D_ASSERT(mdev->state.disk == D_FAILED);
2848 if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002849 drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002850}
2851
Philipp Reisnerb411b362009-09-25 16:07:19 -07002852/**
2853 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
2854 * @mdev: DRBD device.
2855 * @io_fn: IO callback to be called when bitmap IO is possible
2856 * @done: callback to be called after the bitmap IO was performed
2857 * @why: Descriptive text of the reason for doing the IO
2858 *
2859 * While IO on the bitmap happens we freeze application IO thus we ensure
2860 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
2861 * called from worker context. It MUST NOT be used while a previous such
2862 * work is still pending!
2863 */
2864void drbd_queue_bitmap_io(struct drbd_conf *mdev,
2865 int (*io_fn)(struct drbd_conf *),
2866 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002867 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002869 D_ASSERT(current == mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002870
2871 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
2872 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
2873 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
2874 if (mdev->bm_io_work.why)
2875 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
2876 why, mdev->bm_io_work.why);
2877
2878 mdev->bm_io_work.io_fn = io_fn;
2879 mdev->bm_io_work.done = done;
2880 mdev->bm_io_work.why = why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002881 mdev->bm_io_work.flags = flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002882
Philipp Reisner87eeee42011-01-19 14:16:30 +01002883 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002884 set_bit(BITMAP_IO, &mdev->flags);
2885 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
Philipp Reisner127b3172010-11-16 10:07:53 +01002886 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002887 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002888 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002889 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002890}
2891
2892/**
2893 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
2894 * @mdev: DRBD device.
2895 * @io_fn: IO callback to be called when bitmap IO is possible
2896 * @why: Descriptive text of the reason for doing the IO
2897 *
2898 * freezes application IO while that the actual IO operations runs. This
2899 * functions MAY NOT be called from worker context.
2900 */
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002901int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
2902 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002903{
2904 int rv;
2905
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002906 D_ASSERT(current != mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002907
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002908 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2909 drbd_suspend_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002910
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002911 drbd_bm_lock(mdev, why, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002912 rv = io_fn(mdev);
2913 drbd_bm_unlock(mdev);
2914
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002915 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2916 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917
2918 return rv;
2919}
2920
2921void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2922{
2923 if ((mdev->ldev->md.flags & flag) != flag) {
2924 drbd_md_mark_dirty(mdev);
2925 mdev->ldev->md.flags |= flag;
2926 }
2927}
2928
2929void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
2930{
2931 if ((mdev->ldev->md.flags & flag) != 0) {
2932 drbd_md_mark_dirty(mdev);
2933 mdev->ldev->md.flags &= ~flag;
2934 }
2935}
2936int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
2937{
2938 return (bdev->md.flags & flag) != 0;
2939}
2940
2941static void md_sync_timer_fn(unsigned long data)
2942{
2943 struct drbd_conf *mdev = (struct drbd_conf *) data;
2944
Philipp Reisnere42325a2011-01-19 13:55:45 +01002945 drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002946}
2947
2948static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
2949{
2950 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02002951#ifdef DEBUG
2952 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
2953 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
2954#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002955 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002956 return 1;
2957}
2958
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002959const char *cmdname(enum drbd_packet cmd)
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01002960{
2961 /* THINK may need to become several global tables
2962 * when we want to support more than
2963 * one PRO_VERSION */
2964 static const char *cmdnames[] = {
2965 [P_DATA] = "Data",
2966 [P_DATA_REPLY] = "DataReply",
2967 [P_RS_DATA_REPLY] = "RSDataReply",
2968 [P_BARRIER] = "Barrier",
2969 [P_BITMAP] = "ReportBitMap",
2970 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
2971 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
2972 [P_UNPLUG_REMOTE] = "UnplugRemote",
2973 [P_DATA_REQUEST] = "DataRequest",
2974 [P_RS_DATA_REQUEST] = "RSDataRequest",
2975 [P_SYNC_PARAM] = "SyncParam",
2976 [P_SYNC_PARAM89] = "SyncParam89",
2977 [P_PROTOCOL] = "ReportProtocol",
2978 [P_UUIDS] = "ReportUUIDs",
2979 [P_SIZES] = "ReportSizes",
2980 [P_STATE] = "ReportState",
2981 [P_SYNC_UUID] = "ReportSyncUUID",
2982 [P_AUTH_CHALLENGE] = "AuthChallenge",
2983 [P_AUTH_RESPONSE] = "AuthResponse",
2984 [P_PING] = "Ping",
2985 [P_PING_ACK] = "PingAck",
2986 [P_RECV_ACK] = "RecvAck",
2987 [P_WRITE_ACK] = "WriteAck",
2988 [P_RS_WRITE_ACK] = "RSWriteAck",
2989 [P_DISCARD_ACK] = "DiscardAck",
2990 [P_NEG_ACK] = "NegAck",
2991 [P_NEG_DREPLY] = "NegDReply",
2992 [P_NEG_RS_DREPLY] = "NegRSDReply",
2993 [P_BARRIER_ACK] = "BarrierAck",
2994 [P_STATE_CHG_REQ] = "StateChgRequest",
2995 [P_STATE_CHG_REPLY] = "StateChgReply",
2996 [P_OV_REQUEST] = "OVRequest",
2997 [P_OV_REPLY] = "OVReply",
2998 [P_OV_RESULT] = "OVResult",
2999 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3000 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3001 [P_COMPRESSED_BITMAP] = "CBitmap",
3002 [P_DELAY_PROBE] = "DelayProbe",
3003 [P_OUT_OF_SYNC] = "OutOfSync",
3004 [P_MAX_CMD] = NULL,
3005 };
3006
3007 if (cmd == P_HAND_SHAKE_M)
3008 return "HandShakeM";
3009 if (cmd == P_HAND_SHAKE_S)
3010 return "HandShakeS";
3011 if (cmd == P_HAND_SHAKE)
3012 return "HandShake";
3013 if (cmd >= P_MAX_CMD)
3014 return "Unknown";
3015 return cmdnames[cmd];
3016}
3017
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018#ifdef CONFIG_DRBD_FAULT_INJECTION
3019/* Fault insertion support including random number generator shamelessly
3020 * stolen from kernel/rcutorture.c */
3021struct fault_random_state {
3022 unsigned long state;
3023 unsigned long count;
3024};
3025
3026#define FAULT_RANDOM_MULT 39916801 /* prime */
3027#define FAULT_RANDOM_ADD 479001701 /* prime */
3028#define FAULT_RANDOM_REFRESH 10000
3029
3030/*
3031 * Crude but fast random-number generator. Uses a linear congruential
3032 * generator, with occasional help from get_random_bytes().
3033 */
3034static unsigned long
3035_drbd_fault_random(struct fault_random_state *rsp)
3036{
3037 long refresh;
3038
Roel Kluin49829ea2009-12-15 22:55:44 +01003039 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003040 get_random_bytes(&refresh, sizeof(refresh));
3041 rsp->state += refresh;
3042 rsp->count = FAULT_RANDOM_REFRESH;
3043 }
3044 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3045 return swahw32(rsp->state);
3046}
3047
3048static char *
3049_drbd_fault_str(unsigned int type) {
3050 static char *_faults[] = {
3051 [DRBD_FAULT_MD_WR] = "Meta-data write",
3052 [DRBD_FAULT_MD_RD] = "Meta-data read",
3053 [DRBD_FAULT_RS_WR] = "Resync write",
3054 [DRBD_FAULT_RS_RD] = "Resync read",
3055 [DRBD_FAULT_DT_WR] = "Data write",
3056 [DRBD_FAULT_DT_RD] = "Data read",
3057 [DRBD_FAULT_DT_RA] = "Data read ahead",
3058 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003059 [DRBD_FAULT_AL_EE] = "EE allocation",
3060 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003061 };
3062
3063 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3064}
3065
3066unsigned int
3067_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3068{
3069 static struct fault_random_state rrs = {0, 0};
3070
3071 unsigned int ret = (
3072 (fault_devs == 0 ||
3073 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3074 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3075
3076 if (ret) {
3077 fault_count++;
3078
Lars Ellenberg73835062010-05-27 11:51:56 +02003079 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003080 dev_warn(DEV, "***Simulating %s failure\n",
3081 _drbd_fault_str(type));
3082 }
3083
3084 return ret;
3085}
3086#endif
3087
3088const char *drbd_buildtag(void)
3089{
3090 /* DRBD built from external sources has here a reference to the
3091 git hash of the source code. */
3092
3093 static char buildtag[38] = "\0uilt-in";
3094
3095 if (buildtag[0] == 0) {
3096#ifdef CONFIG_MODULES
3097 if (THIS_MODULE != NULL)
3098 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3099 else
3100#endif
3101 buildtag[0] = 'b';
3102 }
3103
3104 return buildtag;
3105}
3106
3107module_init(drbd_init)
3108module_exit(drbd_cleanup)
3109
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110EXPORT_SYMBOL(drbd_conn_str);
3111EXPORT_SYMBOL(drbd_role_str);
3112EXPORT_SYMBOL(drbd_disk_str);
3113EXPORT_SYMBOL(drbd_set_st_err_str);